1use std::slice;
4
5use rustc_ast::InlineAsmOptions;
6use rustc_data_structures::packed::Pu128;
7use rustc_hir::LangItem;
8use rustc_macros::{HashStable, TyDecodable, TyEncodable, TypeFoldable, TypeVisitable};
9use smallvec::{SmallVec, smallvec};
10
11use super::*;
12
13impl SwitchTargets {
14 pub fn new(targets: impl Iterator<Item = (u128, BasicBlock)>, otherwise: BasicBlock) -> Self {
19 let (values, mut targets): (SmallVec<_>, SmallVec<_>) =
20 targets.map(|(v, t)| (Pu128(v), t)).unzip();
21 targets.push(otherwise);
22 Self { values, targets }
23 }
24
25 pub fn static_if(value: u128, then: BasicBlock, else_: BasicBlock) -> Self {
28 Self { values: {
let count = 0usize + 1usize;
let mut vec = ::smallvec::SmallVec::new();
if count <= vec.inline_size() {
vec.push(Pu128(value));
vec
} else {
::smallvec::SmallVec::from_vec(::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[Pu128(value)])))
}
}smallvec![Pu128(value)], targets: {
let count = 0usize + 1usize + 1usize;
let mut vec = ::smallvec::SmallVec::new();
if count <= vec.inline_size() {
vec.push(then);
vec.push(else_);
vec
} else {
::smallvec::SmallVec::from_vec(::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[then, else_])))
}
}smallvec![then, else_] }
29 }
30
31 #[inline]
33 pub fn as_static_if(&self) -> Option<(u128, BasicBlock, BasicBlock)> {
34 if let &[value] = &self.values[..]
35 && let &[then, else_] = &self.targets[..]
36 {
37 Some((value.get(), then, else_))
38 } else {
39 None
40 }
41 }
42
43 #[inline]
45 pub fn otherwise(&self) -> BasicBlock {
46 *self.targets.last().unwrap()
47 }
48
49 #[inline]
56 pub fn iter(&self) -> SwitchTargetsIter<'_> {
57 SwitchTargetsIter { inner: iter::zip(&self.values, &self.targets) }
58 }
59
60 #[inline]
62 pub fn all_targets(&self) -> &[BasicBlock] {
63 &self.targets
64 }
65
66 #[inline]
67 pub fn all_targets_mut(&mut self) -> &mut [BasicBlock] {
68 &mut self.targets
69 }
70
71 #[inline]
73 pub fn all_values(&self) -> &[Pu128] {
74 &self.values
75 }
76
77 #[inline]
78 pub fn all_values_mut(&mut self) -> &mut [Pu128] {
79 &mut self.values
80 }
81
82 #[inline]
86 pub fn target_for_value(&self, value: u128) -> BasicBlock {
87 self.iter().find_map(|(v, t)| (v == value).then_some(t)).unwrap_or_else(|| self.otherwise())
88 }
89
90 #[inline]
92 pub fn add_target(&mut self, value: u128, bb: BasicBlock) {
93 let value = Pu128(value);
94 if self.values.contains(&value) {
95 crate::util::bug::bug_fmt(format_args!("target value {0:?} already present",
value));bug!("target value {:?} already present", value);
96 }
97 self.values.push(value);
98 self.targets.insert(self.targets.len() - 1, bb);
99 }
100
101 #[inline]
103 pub fn is_distinct(&self) -> bool {
104 self.targets.iter().collect::<FxHashSet<_>>().len() == self.targets.len()
105 }
106}
107
108pub struct SwitchTargetsIter<'a> {
109 inner: iter::Zip<slice::Iter<'a, Pu128>, slice::Iter<'a, BasicBlock>>,
110}
111
112impl<'a> Iterator for SwitchTargetsIter<'a> {
113 type Item = (u128, BasicBlock);
114
115 #[inline]
116 fn next(&mut self) -> Option<Self::Item> {
117 self.inner.next().map(|(val, bb)| (val.get(), *bb))
118 }
119
120 #[inline]
121 fn size_hint(&self) -> (usize, Option<usize>) {
122 self.inner.size_hint()
123 }
124}
125
126impl<'a> ExactSizeIterator for SwitchTargetsIter<'a> {}
127
128impl UnwindAction {
129 fn cleanup_block(self) -> Option<BasicBlock> {
130 match self {
131 UnwindAction::Cleanup(bb) => Some(bb),
132 UnwindAction::Continue | UnwindAction::Unreachable | UnwindAction::Terminate(_) => None,
133 }
134 }
135}
136
137impl UnwindTerminateReason {
138 pub fn as_str(self) -> &'static str {
139 match self {
141 UnwindTerminateReason::Abi => "panic in a function that cannot unwind",
142 UnwindTerminateReason::InCleanup => "panic in a destructor during cleanup",
143 }
144 }
145
146 pub fn as_short_str(self) -> &'static str {
148 match self {
149 UnwindTerminateReason::Abi => "abi",
150 UnwindTerminateReason::InCleanup => "cleanup",
151 }
152 }
153
154 pub fn lang_item(self) -> LangItem {
155 match self {
156 UnwindTerminateReason::Abi => LangItem::PanicCannotUnwind,
157 UnwindTerminateReason::InCleanup => LangItem::PanicInCleanup,
158 }
159 }
160}
161
162impl<O> AssertKind<O> {
163 pub fn is_optional_overflow_check(&self) -> bool {
165 use AssertKind::*;
166 use BinOp::*;
167 #[allow(non_exhaustive_omitted_patterns)] match self {
OverflowNeg(..) | Overflow(Add | Sub | Mul | Shl | Shr, ..) => true,
_ => false,
}matches!(self, OverflowNeg(..) | Overflow(Add | Sub | Mul | Shl | Shr, ..))
168 }
169
170 pub fn panic_function(&self) -> LangItem {
177 use AssertKind::*;
178 match self {
179 Overflow(BinOp::Add, _, _) => LangItem::PanicAddOverflow,
180 Overflow(BinOp::Sub, _, _) => LangItem::PanicSubOverflow,
181 Overflow(BinOp::Mul, _, _) => LangItem::PanicMulOverflow,
182 Overflow(BinOp::Div, _, _) => LangItem::PanicDivOverflow,
183 Overflow(BinOp::Rem, _, _) => LangItem::PanicRemOverflow,
184 OverflowNeg(_) => LangItem::PanicNegOverflow,
185 Overflow(BinOp::Shr, _, _) => LangItem::PanicShrOverflow,
186 Overflow(BinOp::Shl, _, _) => LangItem::PanicShlOverflow,
187 Overflow(op, _, _) => crate::util::bug::bug_fmt(format_args!("{0:?} cannot overflow", op))bug!("{:?} cannot overflow", op),
188 DivisionByZero(_) => LangItem::PanicDivZero,
189 RemainderByZero(_) => LangItem::PanicRemZero,
190 ResumedAfterReturn(CoroutineKind::Coroutine(_)) => LangItem::PanicCoroutineResumed,
191 ResumedAfterReturn(CoroutineKind::Desugared(CoroutineDesugaring::Async, _)) => {
192 LangItem::PanicAsyncFnResumed
193 }
194 ResumedAfterReturn(CoroutineKind::Desugared(CoroutineDesugaring::AsyncGen, _)) => {
195 LangItem::PanicAsyncGenFnResumed
196 }
197 ResumedAfterReturn(CoroutineKind::Desugared(CoroutineDesugaring::Gen, _)) => {
198 LangItem::PanicGenFnNone
199 }
200 ResumedAfterPanic(CoroutineKind::Coroutine(_)) => LangItem::PanicCoroutineResumedPanic,
201 ResumedAfterPanic(CoroutineKind::Desugared(CoroutineDesugaring::Async, _)) => {
202 LangItem::PanicAsyncFnResumedPanic
203 }
204 ResumedAfterPanic(CoroutineKind::Desugared(CoroutineDesugaring::AsyncGen, _)) => {
205 LangItem::PanicAsyncGenFnResumedPanic
206 }
207 ResumedAfterPanic(CoroutineKind::Desugared(CoroutineDesugaring::Gen, _)) => {
208 LangItem::PanicGenFnNonePanic
209 }
210 NullPointerDereference => LangItem::PanicNullPointerDereference,
211 InvalidEnumConstruction(_) => LangItem::PanicInvalidEnumConstruction,
212 ResumedAfterDrop(CoroutineKind::Coroutine(_)) => LangItem::PanicCoroutineResumedDrop,
213 ResumedAfterDrop(CoroutineKind::Desugared(CoroutineDesugaring::Async, _)) => {
214 LangItem::PanicAsyncFnResumedDrop
215 }
216 ResumedAfterDrop(CoroutineKind::Desugared(CoroutineDesugaring::AsyncGen, _)) => {
217 LangItem::PanicAsyncGenFnResumedDrop
218 }
219 ResumedAfterDrop(CoroutineKind::Desugared(CoroutineDesugaring::Gen, _)) => {
220 LangItem::PanicGenFnNoneDrop
221 }
222
223 BoundsCheck { .. } | MisalignedPointerDereference { .. } => {
224 crate::util::bug::bug_fmt(format_args!("Unexpected AssertKind"))bug!("Unexpected AssertKind")
225 }
226 }
227 }
228
229 pub fn fmt_assert_args<W: fmt::Write>(&self, f: &mut W) -> fmt::Result
236 where
237 O: Debug,
238 {
239 use AssertKind::*;
240 match self {
241 BoundsCheck { len, index } => f.write_fmt(format_args!("\"index out of bounds: the length is {{}} but the index is {{}}\", {0:?}, {1:?}",
len, index))write!(
242 f,
243 "\"index out of bounds: the length is {{}} but the index is {{}}\", {len:?}, {index:?}"
244 ),
245
246 OverflowNeg(op) => {
247 f.write_fmt(format_args!("\"attempt to negate `{{}}`, which would overflow\", {0:?}",
op))write!(f, "\"attempt to negate `{{}}`, which would overflow\", {op:?}")
248 }
249 DivisionByZero(op) => f.write_fmt(format_args!("\"attempt to divide `{{}}` by zero\", {0:?}", op))write!(f, "\"attempt to divide `{{}}` by zero\", {op:?}"),
250 RemainderByZero(op) => f.write_fmt(format_args!("\"attempt to calculate the remainder of `{{}}` with a divisor of zero\", {0:?}",
op))write!(
251 f,
252 "\"attempt to calculate the remainder of `{{}}` with a divisor of zero\", {op:?}"
253 ),
254 Overflow(BinOp::Add, l, r) => f.write_fmt(format_args!("\"attempt to compute `{{}} + {{}}`, which would overflow\", {0:?}, {1:?}",
l, r))write!(
255 f,
256 "\"attempt to compute `{{}} + {{}}`, which would overflow\", {l:?}, {r:?}"
257 ),
258 Overflow(BinOp::Sub, l, r) => f.write_fmt(format_args!("\"attempt to compute `{{}} - {{}}`, which would overflow\", {0:?}, {1:?}",
l, r))write!(
259 f,
260 "\"attempt to compute `{{}} - {{}}`, which would overflow\", {l:?}, {r:?}"
261 ),
262 Overflow(BinOp::Mul, l, r) => f.write_fmt(format_args!("\"attempt to compute `{{}} * {{}}`, which would overflow\", {0:?}, {1:?}",
l, r))write!(
263 f,
264 "\"attempt to compute `{{}} * {{}}`, which would overflow\", {l:?}, {r:?}"
265 ),
266 Overflow(BinOp::Div, l, r) => f.write_fmt(format_args!("\"attempt to compute `{{}} / {{}}`, which would overflow\", {0:?}, {1:?}",
l, r))write!(
267 f,
268 "\"attempt to compute `{{}} / {{}}`, which would overflow\", {l:?}, {r:?}"
269 ),
270 Overflow(BinOp::Rem, l, r) => f.write_fmt(format_args!("\"attempt to compute the remainder of `{{}} % {{}}`, which would overflow\", {0:?}, {1:?}",
l, r))write!(
271 f,
272 "\"attempt to compute the remainder of `{{}} % {{}}`, which would overflow\", {l:?}, {r:?}"
273 ),
274 Overflow(BinOp::Shr, _, r) => {
275 f.write_fmt(format_args!("\"attempt to shift right by `{{}}`, which would overflow\", {0:?}",
r))write!(f, "\"attempt to shift right by `{{}}`, which would overflow\", {r:?}")
276 }
277 Overflow(BinOp::Shl, _, r) => {
278 f.write_fmt(format_args!("\"attempt to shift left by `{{}}`, which would overflow\", {0:?}",
r))write!(f, "\"attempt to shift left by `{{}}`, which would overflow\", {r:?}")
279 }
280 Overflow(op, _, _) => crate::util::bug::bug_fmt(format_args!("{0:?} cannot overflow", op))bug!("{:?} cannot overflow", op),
281 MisalignedPointerDereference { required, found } => {
282 f.write_fmt(format_args!("\"misaligned pointer dereference: address must be a multiple of {{}} but is {{}}\", {0:?}, {1:?}",
required, found))write!(
283 f,
284 "\"misaligned pointer dereference: address must be a multiple of {{}} but is {{}}\", {required:?}, {found:?}"
285 )
286 }
287 NullPointerDereference => f.write_fmt(format_args!("\"null pointer dereference occurred\""))write!(f, "\"null pointer dereference occurred\""),
288 InvalidEnumConstruction(source) => {
289 f.write_fmt(format_args!("\"trying to construct an enum from an invalid value {{}}\", {0:?}",
source))write!(f, "\"trying to construct an enum from an invalid value {{}}\", {source:?}")
290 }
291 ResumedAfterReturn(CoroutineKind::Coroutine(_)) => {
292 f.write_fmt(format_args!("\"coroutine resumed after completion\""))write!(f, "\"coroutine resumed after completion\"")
293 }
294 ResumedAfterReturn(CoroutineKind::Desugared(CoroutineDesugaring::Async, _)) => {
295 f.write_fmt(format_args!("\"`async fn` resumed after completion\""))write!(f, "\"`async fn` resumed after completion\"")
296 }
297 ResumedAfterReturn(CoroutineKind::Desugared(CoroutineDesugaring::AsyncGen, _)) => {
298 f.write_fmt(format_args!("\"`async gen fn` resumed after completion\""))write!(f, "\"`async gen fn` resumed after completion\"")
299 }
300 ResumedAfterReturn(CoroutineKind::Desugared(CoroutineDesugaring::Gen, _)) => {
301 f.write_fmt(format_args!("\"`gen fn` should just keep returning `None` after completion\""))write!(f, "\"`gen fn` should just keep returning `None` after completion\"")
302 }
303 ResumedAfterPanic(CoroutineKind::Coroutine(_)) => {
304 f.write_fmt(format_args!("\"coroutine resumed after panicking\""))write!(f, "\"coroutine resumed after panicking\"")
305 }
306 ResumedAfterPanic(CoroutineKind::Desugared(CoroutineDesugaring::Async, _)) => {
307 f.write_fmt(format_args!("\"`async fn` resumed after panicking\""))write!(f, "\"`async fn` resumed after panicking\"")
308 }
309 ResumedAfterPanic(CoroutineKind::Desugared(CoroutineDesugaring::AsyncGen, _)) => {
310 f.write_fmt(format_args!("\"`async gen fn` resumed after panicking\""))write!(f, "\"`async gen fn` resumed after panicking\"")
311 }
312 ResumedAfterPanic(CoroutineKind::Desugared(CoroutineDesugaring::Gen, _)) => {
313 f.write_fmt(format_args!("\"`gen fn` should just keep returning `None` after panicking\""))write!(f, "\"`gen fn` should just keep returning `None` after panicking\"")
314 }
315 ResumedAfterDrop(CoroutineKind::Coroutine(_)) => {
316 f.write_fmt(format_args!("\"coroutine resumed after async drop\""))write!(f, "\"coroutine resumed after async drop\"")
317 }
318 ResumedAfterDrop(CoroutineKind::Desugared(CoroutineDesugaring::Async, _)) => {
319 f.write_fmt(format_args!("\"`async fn` resumed after async drop\""))write!(f, "\"`async fn` resumed after async drop\"")
320 }
321 ResumedAfterDrop(CoroutineKind::Desugared(CoroutineDesugaring::AsyncGen, _)) => {
322 f.write_fmt(format_args!("\"`async gen fn` resumed after async drop\""))write!(f, "\"`async gen fn` resumed after async drop\"")
323 }
324 ResumedAfterDrop(CoroutineKind::Desugared(CoroutineDesugaring::Gen, _)) => {
325 f.write_fmt(format_args!("\"`gen fn` resumed after drop\""))write!(f, "\"`gen fn` resumed after drop\"")
326 }
327 }
328 }
329}
330
331impl<O: fmt::Debug> fmt::Display for AssertKind<O> {
338 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
339 use AssertKind::*;
340
341 match self {
342 BoundsCheck { len, index } => {
343 f.write_fmt(format_args!("index out of bounds: the length is {0:?} but the index is {1:?}",
len, index))write!(f, "index out of bounds: the length is {len:?} but the index is {index:?}")
344 }
345 Overflow(BinOp::Shl, _, val) => {
346 f.write_fmt(format_args!("attempt to shift left by `{0:#?}`, which would overflow",
val))write!(f, "attempt to shift left by `{val:#?}`, which would overflow")
347 }
348 Overflow(BinOp::Shr, _, val) => {
349 f.write_fmt(format_args!("attempt to shift right by `{0:#?}`, which would overflow",
val))write!(f, "attempt to shift right by `{val:#?}`, which would overflow")
350 }
351 Overflow(binop, left, right) => {
352 f.write_fmt(format_args!("attempt to compute `{1:#?} {0} {2:#?}`, which would overflow",
binop.to_hir_binop().as_str(), left, right))write!(
353 f,
354 "attempt to compute `{left:#?} {op} {right:#?}`, which would overflow",
355 op = binop.to_hir_binop().as_str()
356 )
357 }
358 OverflowNeg(val) => f.write_fmt(format_args!("attempt to negate `{0:#?}`, which would overflow",
val))write!(f, "attempt to negate `{val:#?}`, which would overflow"),
359 DivisionByZero(val) => f.write_fmt(format_args!("attempt to divide `{0:#?}` by zero", val))write!(f, "attempt to divide `{val:#?}` by zero"),
360 RemainderByZero(val) => {
361 f.write_fmt(format_args!("attempt to calculate the remainder of `{0:#?}` with a divisor of zero",
val))write!(f, "attempt to calculate the remainder of `{val:#?}` with a divisor of zero")
362 }
363 ResumedAfterReturn(CoroutineKind::Desugared(CoroutineDesugaring::Async, _)) => {
364 f.write_fmt(format_args!("`async fn` resumed after completion"))write!(f, "`async fn` resumed after completion")
365 }
366 ResumedAfterReturn(CoroutineKind::Desugared(CoroutineDesugaring::AsyncGen, _)) => {
367 ::core::panicking::panic("not yet implemented")todo!()
368 }
369 ResumedAfterReturn(CoroutineKind::Desugared(CoroutineDesugaring::Gen, _)) => {
370 crate::util::bug::bug_fmt(format_args!("gen blocks can be resumed after they return and will keep returning `None`"))bug!("gen blocks can be resumed after they return and will keep returning `None`")
371 }
372 ResumedAfterReturn(CoroutineKind::Coroutine(_)) => {
373 f.write_fmt(format_args!("coroutine resumed after completion"))write!(f, "coroutine resumed after completion")
374 }
375 ResumedAfterPanic(CoroutineKind::Desugared(CoroutineDesugaring::Async, _)) => {
376 f.write_fmt(format_args!("`async fn` resumed after panicking"))write!(f, "`async fn` resumed after panicking")
377 }
378 ResumedAfterPanic(CoroutineKind::Desugared(CoroutineDesugaring::AsyncGen, _)) => {
379 ::core::panicking::panic("not yet implemented")todo!()
380 }
381 ResumedAfterPanic(CoroutineKind::Desugared(CoroutineDesugaring::Gen, _)) => {
382 f.write_fmt(format_args!("`gen` fn or block cannot be further iterated on after it panicked"))write!(f, "`gen` fn or block cannot be further iterated on after it panicked")
383 }
384 ResumedAfterPanic(CoroutineKind::Coroutine(_)) => {
385 f.write_fmt(format_args!("coroutine resumed after panicking"))write!(f, "coroutine resumed after panicking")
386 }
387 NullPointerDereference => f.write_fmt(format_args!("null pointer dereference occurred"))write!(f, "null pointer dereference occurred"),
388 InvalidEnumConstruction(source) => {
389 f.write_fmt(format_args!("trying to construct an enum from an invalid value `{0:#?}`",
source))write!(f, "trying to construct an enum from an invalid value `{source:#?}`")
390 }
391 ResumedAfterDrop(CoroutineKind::Desugared(CoroutineDesugaring::Async, _)) => {
392 f.write_fmt(format_args!("`async fn` resumed after async drop"))write!(f, "`async fn` resumed after async drop")
393 }
394 ResumedAfterDrop(CoroutineKind::Desugared(CoroutineDesugaring::AsyncGen, _)) => {
395 ::core::panicking::panic("not yet implemented")todo!()
396 }
397 ResumedAfterDrop(CoroutineKind::Desugared(CoroutineDesugaring::Gen, _)) => {
398 f.write_fmt(format_args!("`gen` fn or block cannot be further iterated on after it async dropped"))write!(f, "`gen` fn or block cannot be further iterated on after it async dropped")
399 }
400 ResumedAfterDrop(CoroutineKind::Coroutine(_)) => {
401 f.write_fmt(format_args!("coroutine resumed after async drop"))write!(f, "coroutine resumed after async drop")
402 }
403
404 MisalignedPointerDereference { required, found } => f.write_fmt(format_args!("misaligned pointer dereference: address must be a multiple of {0:#?} but is {1:#?}",
required, found))write!(
405 f,
406 "misaligned pointer dereference: address must be a multiple of {required:#?} but is {found:#?}"
407 ),
408 }
409 }
410}
411
412#[derive(#[automatically_derived]
impl<'tcx> ::core::clone::Clone for Terminator<'tcx> {
#[inline]
fn clone(&self) -> Terminator<'tcx> {
Terminator {
source_info: ::core::clone::Clone::clone(&self.source_info),
kind: ::core::clone::Clone::clone(&self.kind),
}
}
}Clone, const _: () =
{
impl<'tcx, __E: ::rustc_middle::ty::codec::TyEncoder<'tcx>>
::rustc_serialize::Encodable<__E> for Terminator<'tcx> {
fn encode(&self, __encoder: &mut __E) {
match *self {
Terminator {
source_info: ref __binding_0, kind: ref __binding_1 } => {
::rustc_serialize::Encodable::<__E>::encode(__binding_0,
__encoder);
::rustc_serialize::Encodable::<__E>::encode(__binding_1,
__encoder);
}
}
}
}
};TyEncodable, const _: () =
{
impl<'tcx, __D: ::rustc_middle::ty::codec::TyDecoder<'tcx>>
::rustc_serialize::Decodable<__D> for Terminator<'tcx> {
fn decode(__decoder: &mut __D) -> Self {
Terminator {
source_info: ::rustc_serialize::Decodable::decode(__decoder),
kind: ::rustc_serialize::Decodable::decode(__decoder),
}
}
}
};TyDecodable, const _: () =
{
impl<'tcx, '__ctx>
::rustc_data_structures::stable_hasher::HashStable<::rustc_middle::ich::StableHashingContext<'__ctx>>
for Terminator<'tcx> {
#[inline]
fn hash_stable(&self,
__hcx: &mut ::rustc_middle::ich::StableHashingContext<'__ctx>,
__hasher:
&mut ::rustc_data_structures::stable_hasher::StableHasher) {
match *self {
Terminator {
source_info: ref __binding_0, kind: ref __binding_1 } => {
{ __binding_0.hash_stable(__hcx, __hasher); }
{ __binding_1.hash_stable(__hcx, __hasher); }
}
}
}
}
};HashStable, const _: () =
{
impl<'tcx>
::rustc_middle::ty::TypeFoldable<::rustc_middle::ty::TyCtxt<'tcx>>
for Terminator<'tcx> {
fn try_fold_with<__F: ::rustc_middle::ty::FallibleTypeFolder<::rustc_middle::ty::TyCtxt<'tcx>>>(self,
__folder: &mut __F) -> Result<Self, __F::Error> {
Ok(match self {
Terminator { source_info: __binding_0, kind: __binding_1 }
=> {
Terminator {
source_info: ::rustc_middle::ty::TypeFoldable::try_fold_with(__binding_0,
__folder)?,
kind: ::rustc_middle::ty::TypeFoldable::try_fold_with(__binding_1,
__folder)?,
}
}
})
}
fn fold_with<__F: ::rustc_middle::ty::TypeFolder<::rustc_middle::ty::TyCtxt<'tcx>>>(self,
__folder: &mut __F) -> Self {
match self {
Terminator { source_info: __binding_0, kind: __binding_1 }
=> {
Terminator {
source_info: ::rustc_middle::ty::TypeFoldable::fold_with(__binding_0,
__folder),
kind: ::rustc_middle::ty::TypeFoldable::fold_with(__binding_1,
__folder),
}
}
}
}
}
};TypeFoldable, const _: () =
{
impl<'tcx>
::rustc_middle::ty::TypeVisitable<::rustc_middle::ty::TyCtxt<'tcx>>
for Terminator<'tcx> {
fn visit_with<__V: ::rustc_middle::ty::TypeVisitor<::rustc_middle::ty::TyCtxt<'tcx>>>(&self,
__visitor: &mut __V) -> __V::Result {
match *self {
Terminator {
source_info: ref __binding_0, kind: ref __binding_1 } => {
{
match ::rustc_middle::ty::VisitorResult::branch(::rustc_middle::ty::TypeVisitable::visit_with(__binding_0,
__visitor)) {
::core::ops::ControlFlow::Continue(()) => {}
::core::ops::ControlFlow::Break(r) => {
return ::rustc_middle::ty::VisitorResult::from_residual(r);
}
}
}
{
match ::rustc_middle::ty::VisitorResult::branch(::rustc_middle::ty::TypeVisitable::visit_with(__binding_1,
__visitor)) {
::core::ops::ControlFlow::Continue(()) => {}
::core::ops::ControlFlow::Break(r) => {
return ::rustc_middle::ty::VisitorResult::from_residual(r);
}
}
}
}
}
<__V::Result as ::rustc_middle::ty::VisitorResult>::output()
}
}
};TypeVisitable)]
413pub struct Terminator<'tcx> {
414 pub source_info: SourceInfo,
415 pub kind: TerminatorKind<'tcx>,
416}
417
418impl<'tcx> Terminator<'tcx> {
419 #[inline]
420 pub fn successors(&self) -> Successors<'_> {
421 self.kind.successors()
422 }
423
424 #[inline]
426 pub fn identical_successor(&self) -> Option<BasicBlock> {
427 let mut successors = self.successors();
428 let first_succ = successors.next()?;
429 if successors.all(|succ| first_succ == succ) { Some(first_succ) } else { None }
430 }
431
432 #[inline]
433 pub fn successors_mut<'a>(&'a mut self, f: impl FnMut(&'a mut BasicBlock)) {
434 self.kind.successors_mut(f)
435 }
436
437 #[inline]
438 pub fn unwind(&self) -> Option<&UnwindAction> {
439 self.kind.unwind()
440 }
441
442 #[inline]
443 pub fn unwind_mut(&mut self) -> Option<&mut UnwindAction> {
444 self.kind.unwind_mut()
445 }
446}
447
448impl<'tcx> TerminatorKind<'tcx> {
449 pub const fn name(&self) -> &'static str {
452 match self {
453 TerminatorKind::Goto { .. } => "Goto",
454 TerminatorKind::SwitchInt { .. } => "SwitchInt",
455 TerminatorKind::UnwindResume => "UnwindResume",
456 TerminatorKind::UnwindTerminate(_) => "UnwindTerminate",
457 TerminatorKind::Return => "Return",
458 TerminatorKind::Unreachable => "Unreachable",
459 TerminatorKind::Drop { .. } => "Drop",
460 TerminatorKind::Call { .. } => "Call",
461 TerminatorKind::TailCall { .. } => "TailCall",
462 TerminatorKind::Assert { .. } => "Assert",
463 TerminatorKind::Yield { .. } => "Yield",
464 TerminatorKind::CoroutineDrop => "CoroutineDrop",
465 TerminatorKind::FalseEdge { .. } => "FalseEdge",
466 TerminatorKind::FalseUnwind { .. } => "FalseUnwind",
467 TerminatorKind::InlineAsm { .. } => "InlineAsm",
468 }
469 }
470
471 #[inline]
472 pub fn if_(cond: Operand<'tcx>, t: BasicBlock, f: BasicBlock) -> TerminatorKind<'tcx> {
473 TerminatorKind::SwitchInt { discr: cond, targets: SwitchTargets::static_if(0, f, t) }
474 }
475}
476
477pub use helper::*;
478
479mod helper {
480 use super::*;
481 pub type Successors<'a> = impl DoubleEndedIterator<Item = BasicBlock> + 'a;
482
483 #[inline]
485 #[define_opaque(Successors)]
486 fn mk_successors(
487 slice: &[BasicBlock],
488 option1: Option<BasicBlock>,
489 option2: Option<BasicBlock>,
490 ) -> Successors<'_> {
491 slice.iter().copied().chain(option1.into_iter().chain(option2))
492 }
493
494 impl SwitchTargets {
495 #[inline]
498 pub fn successors_for_value(&self, value: u128) -> Successors<'_> {
499 let target = self.target_for_value(value);
500 mk_successors(&[], Some(target), None)
501 }
502 }
503
504 impl<'tcx> TerminatorKind<'tcx> {
505 #[inline]
506 pub fn successors(&self) -> Successors<'_> {
507 use self::TerminatorKind::*;
508 match *self {
509 Drop { target: ref t, unwind: UnwindAction::Cleanup(u), drop: Some(d), .. } => {
511 mk_successors(slice::from_ref(t), Some(u), Some(d))
512 }
513 Call { target: Some(ref t), unwind: UnwindAction::Cleanup(u), .. }
515 | Yield { resume: ref t, drop: Some(u), .. }
516 | Drop { target: ref t, unwind: UnwindAction::Cleanup(u), drop: None, .. }
517 | Drop { target: ref t, unwind: _, drop: Some(u), .. }
518 | Assert { target: ref t, unwind: UnwindAction::Cleanup(u), .. }
519 | FalseUnwind { real_target: ref t, unwind: UnwindAction::Cleanup(u) } => {
520 mk_successors(slice::from_ref(t), Some(u), None)
521 }
522 Goto { target: ref t }
524 | Call { target: None, unwind: UnwindAction::Cleanup(ref t), .. }
525 | Call { target: Some(ref t), unwind: _, .. }
526 | Yield { resume: ref t, drop: None, .. }
527 | Drop { target: ref t, unwind: _, .. }
528 | Assert { target: ref t, unwind: _, .. }
529 | FalseUnwind { real_target: ref t, unwind: _ } => {
530 mk_successors(slice::from_ref(t), None, None)
531 }
532 UnwindResume
534 | UnwindTerminate(_)
535 | CoroutineDrop
536 | Return
537 | Unreachable
538 | TailCall { .. }
539 | Call { target: None, unwind: _, .. } => mk_successors(&[], None, None),
540 InlineAsm { ref targets, unwind: UnwindAction::Cleanup(u), .. } => {
542 mk_successors(targets, Some(u), None)
543 }
544 InlineAsm { ref targets, unwind: _, .. } => mk_successors(targets, None, None),
545 SwitchInt { ref targets, .. } => mk_successors(&targets.targets, None, None),
546 FalseEdge { ref real_target, imaginary_target } => {
548 mk_successors(slice::from_ref(real_target), Some(imaginary_target), None)
549 }
550 }
551 }
552
553 #[inline]
554 pub fn successors_mut<'a>(&'a mut self, mut f: impl FnMut(&'a mut BasicBlock)) {
555 use self::TerminatorKind::*;
556 match self {
557 Drop { target, unwind, drop, .. } => {
558 f(target);
559 if let UnwindAction::Cleanup(u) = unwind {
560 f(u)
561 }
562 if let Some(d) = drop {
563 f(d)
564 }
565 }
566 Call { target, unwind, .. } => {
567 if let Some(target) = target {
568 f(target);
569 }
570 if let UnwindAction::Cleanup(u) = unwind {
571 f(u)
572 }
573 }
574 Yield { resume, drop, .. } => {
575 f(resume);
576 if let Some(d) = drop {
577 f(d)
578 }
579 }
580 Assert { target, unwind, .. } | FalseUnwind { real_target: target, unwind } => {
581 f(target);
582 if let UnwindAction::Cleanup(u) = unwind {
583 f(u)
584 }
585 }
586 Goto { target } => {
587 f(target);
588 }
589 UnwindResume
590 | UnwindTerminate(_)
591 | CoroutineDrop
592 | Return
593 | Unreachable
594 | TailCall { .. } => {}
595 InlineAsm { targets, unwind, .. } => {
596 for target in targets {
597 f(target);
598 }
599 if let UnwindAction::Cleanup(u) = unwind {
600 f(u)
601 }
602 }
603 SwitchInt { targets, .. } => {
604 for target in &mut targets.targets {
605 f(target);
606 }
607 }
608 FalseEdge { real_target, imaginary_target } => {
609 f(real_target);
610 f(imaginary_target);
611 }
612 }
613 }
614 }
615}
616
617impl<'tcx> TerminatorKind<'tcx> {
618 #[inline]
619 pub fn unwind(&self) -> Option<&UnwindAction> {
620 match *self {
621 TerminatorKind::Goto { .. }
622 | TerminatorKind::UnwindResume
623 | TerminatorKind::UnwindTerminate(_)
624 | TerminatorKind::Return
625 | TerminatorKind::TailCall { .. }
626 | TerminatorKind::Unreachable
627 | TerminatorKind::CoroutineDrop
628 | TerminatorKind::Yield { .. }
629 | TerminatorKind::SwitchInt { .. }
630 | TerminatorKind::FalseEdge { .. } => None,
631 TerminatorKind::Call { ref unwind, .. }
632 | TerminatorKind::Assert { ref unwind, .. }
633 | TerminatorKind::Drop { ref unwind, .. }
634 | TerminatorKind::FalseUnwind { ref unwind, .. }
635 | TerminatorKind::InlineAsm { ref unwind, .. } => Some(unwind),
636 }
637 }
638
639 #[inline]
640 pub fn unwind_mut(&mut self) -> Option<&mut UnwindAction> {
641 match *self {
642 TerminatorKind::Goto { .. }
643 | TerminatorKind::UnwindResume
644 | TerminatorKind::UnwindTerminate(_)
645 | TerminatorKind::Return
646 | TerminatorKind::TailCall { .. }
647 | TerminatorKind::Unreachable
648 | TerminatorKind::CoroutineDrop
649 | TerminatorKind::Yield { .. }
650 | TerminatorKind::SwitchInt { .. }
651 | TerminatorKind::FalseEdge { .. } => None,
652 TerminatorKind::Call { ref mut unwind, .. }
653 | TerminatorKind::Assert { ref mut unwind, .. }
654 | TerminatorKind::Drop { ref mut unwind, .. }
655 | TerminatorKind::FalseUnwind { ref mut unwind, .. }
656 | TerminatorKind::InlineAsm { ref mut unwind, .. } => Some(unwind),
657 }
658 }
659
660 #[inline]
661 pub fn as_switch(&self) -> Option<(&Operand<'tcx>, &SwitchTargets)> {
662 match self {
663 TerminatorKind::SwitchInt { discr, targets } => Some((discr, targets)),
664 _ => None,
665 }
666 }
667
668 #[inline]
669 pub fn as_goto(&self) -> Option<BasicBlock> {
670 match self {
671 TerminatorKind::Goto { target } => Some(*target),
672 _ => None,
673 }
674 }
675}
676
677#[derive(#[automatically_derived]
impl<'mir, 'tcx> ::core::marker::Copy for TerminatorEdges<'mir, 'tcx> { }Copy, #[automatically_derived]
impl<'mir, 'tcx> ::core::clone::Clone for TerminatorEdges<'mir, 'tcx> {
#[inline]
fn clone(&self) -> TerminatorEdges<'mir, 'tcx> {
let _: ::core::clone::AssertParamIsClone<BasicBlock>;
let _: ::core::clone::AssertParamIsClone<&'mir [BasicBlock]>;
let _: ::core::clone::AssertParamIsClone<Option<BasicBlock>>;
let _:
::core::clone::AssertParamIsClone<CallReturnPlaces<'mir,
'tcx>>;
let _: ::core::clone::AssertParamIsClone<&'mir SwitchTargets>;
let _: ::core::clone::AssertParamIsClone<&'mir Operand<'tcx>>;
*self
}
}Clone, #[automatically_derived]
impl<'mir, 'tcx> ::core::fmt::Debug for TerminatorEdges<'mir, 'tcx> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
TerminatorEdges::None =>
::core::fmt::Formatter::write_str(f, "None"),
TerminatorEdges::Single(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f, "Single",
&__self_0),
TerminatorEdges::Double(__self_0, __self_1) =>
::core::fmt::Formatter::debug_tuple_field2_finish(f, "Double",
__self_0, &__self_1),
TerminatorEdges::AssignOnReturn {
return_: __self_0, cleanup: __self_1, place: __self_2 } =>
::core::fmt::Formatter::debug_struct_field3_finish(f,
"AssignOnReturn", "return_", __self_0, "cleanup", __self_1,
"place", &__self_2),
TerminatorEdges::SwitchInt { targets: __self_0, discr: __self_1 }
=>
::core::fmt::Formatter::debug_struct_field2_finish(f,
"SwitchInt", "targets", __self_0, "discr", &__self_1),
}
}
}Debug)]
678pub enum TerminatorEdges<'mir, 'tcx> {
679 None,
681 Single(BasicBlock),
684 Double(BasicBlock, BasicBlock),
687 AssignOnReturn {
689 return_: &'mir [BasicBlock],
690 cleanup: Option<BasicBlock>,
692 place: CallReturnPlaces<'mir, 'tcx>,
693 },
694 SwitchInt { targets: &'mir SwitchTargets, discr: &'mir Operand<'tcx> },
696}
697
698#[derive(#[automatically_derived]
impl<'a, 'tcx> ::core::marker::Copy for CallReturnPlaces<'a, 'tcx> { }Copy, #[automatically_derived]
impl<'a, 'tcx> ::core::clone::Clone for CallReturnPlaces<'a, 'tcx> {
#[inline]
fn clone(&self) -> CallReturnPlaces<'a, 'tcx> {
let _: ::core::clone::AssertParamIsClone<Place<'tcx>>;
let _: ::core::clone::AssertParamIsClone<Place<'tcx>>;
let _:
::core::clone::AssertParamIsClone<&'a [InlineAsmOperand<'tcx>]>;
*self
}
}Clone, #[automatically_derived]
impl<'a, 'tcx> ::core::fmt::Debug for CallReturnPlaces<'a, 'tcx> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
CallReturnPlaces::Call(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f, "Call",
&__self_0),
CallReturnPlaces::Yield(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f, "Yield",
&__self_0),
CallReturnPlaces::InlineAsm(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"InlineAsm", &__self_0),
}
}
}Debug)]
701pub enum CallReturnPlaces<'a, 'tcx> {
702 Call(Place<'tcx>),
703 Yield(Place<'tcx>),
704 InlineAsm(&'a [InlineAsmOperand<'tcx>]),
705}
706
707impl<'tcx> CallReturnPlaces<'_, 'tcx> {
708 pub fn for_each(&self, mut f: impl FnMut(Place<'tcx>)) {
709 match *self {
710 Self::Call(place) | Self::Yield(place) => f(place),
711 Self::InlineAsm(operands) => {
712 for op in operands {
713 match *op {
714 InlineAsmOperand::Out { place: Some(place), .. }
715 | InlineAsmOperand::InOut { out_place: Some(place), .. } => f(place),
716 _ => {}
717 }
718 }
719 }
720 }
721 }
722}
723
724impl<'tcx> Terminator<'tcx> {
725 pub fn edges(&self) -> TerminatorEdges<'_, 'tcx> {
726 self.kind.edges()
727 }
728}
729
730impl<'tcx> TerminatorKind<'tcx> {
731 pub fn edges(&self) -> TerminatorEdges<'_, 'tcx> {
732 use TerminatorKind::*;
733 match *self {
734 Return
735 | TailCall { .. }
736 | UnwindResume
737 | UnwindTerminate(_)
738 | CoroutineDrop
739 | Unreachable => TerminatorEdges::None,
740
741 Goto { target } => TerminatorEdges::Single(target),
742
743 Assert { target, unwind, expected: _, msg: _, cond: _ }
746 | Drop { target, unwind, place: _, replace: _, drop: _, async_fut: _ }
747 | FalseUnwind { real_target: target, unwind } => match unwind {
748 UnwindAction::Cleanup(unwind) => TerminatorEdges::Double(target, unwind),
749 UnwindAction::Continue | UnwindAction::Terminate(_) | UnwindAction::Unreachable => {
750 TerminatorEdges::Single(target)
751 }
752 },
753
754 FalseEdge { real_target, imaginary_target } => {
755 TerminatorEdges::Double(real_target, imaginary_target)
756 }
757
758 Yield { resume: ref target, drop, resume_arg, value: _ } => {
759 TerminatorEdges::AssignOnReturn {
760 return_: slice::from_ref(target),
761 cleanup: drop,
762 place: CallReturnPlaces::Yield(resume_arg),
763 }
764 }
765
766 Call {
767 unwind,
768 destination,
769 ref target,
770 func: _,
771 args: _,
772 fn_span: _,
773 call_source: _,
774 } => TerminatorEdges::AssignOnReturn {
775 return_: target.as_ref().map(slice::from_ref).unwrap_or_default(),
776 cleanup: unwind.cleanup_block(),
777 place: CallReturnPlaces::Call(destination),
778 },
779
780 InlineAsm {
781 asm_macro: _,
782 template: _,
783 ref operands,
784 options: _,
785 line_spans: _,
786 ref targets,
787 unwind,
788 } => TerminatorEdges::AssignOnReturn {
789 return_: targets,
790 cleanup: unwind.cleanup_block(),
791 place: CallReturnPlaces::InlineAsm(operands),
792 },
793
794 SwitchInt { ref targets, ref discr } => TerminatorEdges::SwitchInt { targets, discr },
795 }
796 }
797}
798
799impl CallSource {
800 pub fn from_hir_call(self) -> bool {
801 #[allow(non_exhaustive_omitted_patterns)] match self {
CallSource::Normal => true,
_ => false,
}matches!(self, CallSource::Normal)
802 }
803}
804
805impl InlineAsmMacro {
806 pub const fn diverges(self, options: InlineAsmOptions) -> bool {
807 match self {
808 InlineAsmMacro::Asm => options.contains(InlineAsmOptions::NORETURN),
809 InlineAsmMacro::NakedAsm => true,
810 }
811 }
812}