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 pub fn diagnostic_message(&self) -> DiagMessage {
337 use AssertKind::*;
338
339 match self {
340 BoundsCheck { .. } => {
341 rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("index out of bounds: the length is {$len} but the index is {$index}"))msg!("index out of bounds: the length is {$len} but the index is {$index}")
342 }
343 Overflow(BinOp::Shl, _, _) => {
344 rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("attempt to shift left by `{$val}`, which would overflow"))msg!("attempt to shift left by `{$val}`, which would overflow")
345 }
346 Overflow(BinOp::Shr, _, _) => {
347 rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("attempt to shift right by `{$val}`, which would overflow"))msg!("attempt to shift right by `{$val}`, which would overflow")
348 }
349 Overflow(_, _, _) => {
350 rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("attempt to compute `{$left} {$op} {$right}`, which would overflow"))msg!("attempt to compute `{$left} {$op} {$right}`, which would overflow")
351 }
352 OverflowNeg(_) => rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("attempt to negate `{$val}`, which would overflow"))msg!("attempt to negate `{$val}`, which would overflow"),
353 DivisionByZero(_) => rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("attempt to divide `{$val}` by zero"))msg!("attempt to divide `{$val}` by zero"),
354 RemainderByZero(_) => {
355 rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("attempt to calculate the remainder of `{$val}` with a divisor of zero"))msg!("attempt to calculate the remainder of `{$val}` with a divisor of zero")
356 }
357 ResumedAfterReturn(CoroutineKind::Desugared(CoroutineDesugaring::Async, _)) => {
358 rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("`async fn` resumed after completion"))msg!("`async fn` resumed after completion")
359 }
360 ResumedAfterReturn(CoroutineKind::Desugared(CoroutineDesugaring::AsyncGen, _)) => {
361 ::core::panicking::panic("not yet implemented")todo!()
362 }
363 ResumedAfterReturn(CoroutineKind::Desugared(CoroutineDesugaring::Gen, _)) => {
364 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`")
365 }
366 ResumedAfterReturn(CoroutineKind::Coroutine(_)) => {
367 rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("coroutine resumed after completion"))msg!("coroutine resumed after completion")
368 }
369 ResumedAfterPanic(CoroutineKind::Desugared(CoroutineDesugaring::Async, _)) => {
370 rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("`async fn` resumed after panicking"))msg!("`async fn` resumed after panicking")
371 }
372 ResumedAfterPanic(CoroutineKind::Desugared(CoroutineDesugaring::AsyncGen, _)) => {
373 ::core::panicking::panic("not yet implemented")todo!()
374 }
375 ResumedAfterPanic(CoroutineKind::Desugared(CoroutineDesugaring::Gen, _)) => {
376 rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("`gen` fn or block cannot be further iterated on after it panicked"))msg!("`gen` fn or block cannot be further iterated on after it panicked")
377 }
378 ResumedAfterPanic(CoroutineKind::Coroutine(_)) => {
379 rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("coroutine resumed after panicking"))msg!("coroutine resumed after panicking")
380 }
381 NullPointerDereference => rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("null pointer dereference occurred"))msg!("null pointer dereference occurred"),
382 InvalidEnumConstruction(_) => {
383 rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("trying to construct an enum from an invalid value `{$source}`"))msg!("trying to construct an enum from an invalid value `{$source}`")
384 }
385 ResumedAfterDrop(CoroutineKind::Desugared(CoroutineDesugaring::Async, _)) => {
386 rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("`async fn` resumed after async drop"))msg!("`async fn` resumed after async drop")
387 }
388 ResumedAfterDrop(CoroutineKind::Desugared(CoroutineDesugaring::AsyncGen, _)) => {
389 ::core::panicking::panic("not yet implemented")todo!()
390 }
391 ResumedAfterDrop(CoroutineKind::Desugared(CoroutineDesugaring::Gen, _)) => {
392 rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("`gen` fn or block cannot be further iterated on after it async dropped"))msg!("`gen` fn or block cannot be further iterated on after it async dropped")
393 }
394 ResumedAfterDrop(CoroutineKind::Coroutine(_)) => {
395 rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("coroutine resumed after async drop"))msg!("coroutine resumed after async drop")
396 }
397
398 MisalignedPointerDereference { .. } => rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("misaligned pointer dereference: address must be a multiple of {$required} but is {$found}"))msg!(
399 "misaligned pointer dereference: address must be a multiple of {$required} but is {$found}"
400 ),
401 }
402 }
403
404 pub fn add_args(self, adder: &mut dyn FnMut(DiagArgName, DiagArgValue))
405 where
406 O: fmt::Debug,
407 {
408 use AssertKind::*;
409
410 macro_rules! add {
411 ($name: expr, $value: expr) => {
412 adder($name.into(), $value.into_diag_arg(&mut None));
413 };
414 }
415
416 match self {
417 BoundsCheck { len, index } => {
418 adder("len".into(),
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0:?}", len))
}).into_diag_arg(&mut None));add!("len", format!("{len:?}"));
419 adder("index".into(),
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0:?}", index))
}).into_diag_arg(&mut None));add!("index", format!("{index:?}"));
420 }
421 Overflow(BinOp::Shl | BinOp::Shr, _, val)
422 | DivisionByZero(val)
423 | RemainderByZero(val)
424 | OverflowNeg(val) => {
425 adder("val".into(),
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0:#?}", val))
}).into_diag_arg(&mut None));add!("val", format!("{val:#?}"));
426 }
427 Overflow(binop, left, right) => {
428 adder("op".into(), binop.to_hir_binop().as_str().into_diag_arg(&mut None));add!("op", binop.to_hir_binop().as_str());
429 adder("left".into(),
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0:#?}", left))
}).into_diag_arg(&mut None));add!("left", format!("{left:#?}"));
430 adder("right".into(),
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0:#?}", right))
}).into_diag_arg(&mut None));add!("right", format!("{right:#?}"));
431 }
432 ResumedAfterReturn(_)
433 | ResumedAfterPanic(_)
434 | NullPointerDereference
435 | ResumedAfterDrop(_) => {}
436 MisalignedPointerDereference { required, found } => {
437 adder("required".into(),
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0:#?}", required))
}).into_diag_arg(&mut None));add!("required", format!("{required:#?}"));
438 adder("found".into(),
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0:#?}", found))
}).into_diag_arg(&mut None));add!("found", format!("{found:#?}"));
439 }
440 InvalidEnumConstruction(source) => {
441 adder("source".into(),
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0:#?}", source))
}).into_diag_arg(&mut None));add!("source", format!("{source:#?}"));
442 }
443 }
444 }
445}
446
447#[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, #[automatically_derived]
impl<'tcx> ::core::fmt::Debug for Terminator<'tcx> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field2_finish(f, "Terminator",
"source_info", &self.source_info, "kind", &&self.kind)
}
}Debug, 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)]
448pub struct Terminator<'tcx> {
449 pub source_info: SourceInfo,
450 pub kind: TerminatorKind<'tcx>,
451}
452
453impl<'tcx> Terminator<'tcx> {
454 #[inline]
455 pub fn successors(&self) -> Successors<'_> {
456 self.kind.successors()
457 }
458
459 #[inline]
461 pub fn identical_successor(&self) -> Option<BasicBlock> {
462 let mut successors = self.successors();
463 let first_succ = successors.next()?;
464 if successors.all(|succ| first_succ == succ) { Some(first_succ) } else { None }
465 }
466
467 #[inline]
468 pub fn successors_mut<'a>(&'a mut self, f: impl FnMut(&'a mut BasicBlock)) {
469 self.kind.successors_mut(f)
470 }
471
472 #[inline]
473 pub fn unwind(&self) -> Option<&UnwindAction> {
474 self.kind.unwind()
475 }
476
477 #[inline]
478 pub fn unwind_mut(&mut self) -> Option<&mut UnwindAction> {
479 self.kind.unwind_mut()
480 }
481}
482
483impl<'tcx> TerminatorKind<'tcx> {
484 pub const fn name(&self) -> &'static str {
487 match self {
488 TerminatorKind::Goto { .. } => "Goto",
489 TerminatorKind::SwitchInt { .. } => "SwitchInt",
490 TerminatorKind::UnwindResume => "UnwindResume",
491 TerminatorKind::UnwindTerminate(_) => "UnwindTerminate",
492 TerminatorKind::Return => "Return",
493 TerminatorKind::Unreachable => "Unreachable",
494 TerminatorKind::Drop { .. } => "Drop",
495 TerminatorKind::Call { .. } => "Call",
496 TerminatorKind::TailCall { .. } => "TailCall",
497 TerminatorKind::Assert { .. } => "Assert",
498 TerminatorKind::Yield { .. } => "Yield",
499 TerminatorKind::CoroutineDrop => "CoroutineDrop",
500 TerminatorKind::FalseEdge { .. } => "FalseEdge",
501 TerminatorKind::FalseUnwind { .. } => "FalseUnwind",
502 TerminatorKind::InlineAsm { .. } => "InlineAsm",
503 }
504 }
505
506 #[inline]
507 pub fn if_(cond: Operand<'tcx>, t: BasicBlock, f: BasicBlock) -> TerminatorKind<'tcx> {
508 TerminatorKind::SwitchInt { discr: cond, targets: SwitchTargets::static_if(0, f, t) }
509 }
510}
511
512pub use helper::*;
513use rustc_errors::msg;
514
515mod helper {
516 use super::*;
517 pub type Successors<'a> = impl DoubleEndedIterator<Item = BasicBlock> + 'a;
518
519 #[inline]
521 #[define_opaque(Successors)]
522 fn mk_successors(
523 slice: &[BasicBlock],
524 option1: Option<BasicBlock>,
525 option2: Option<BasicBlock>,
526 ) -> Successors<'_> {
527 slice.iter().copied().chain(option1.into_iter().chain(option2))
528 }
529
530 impl SwitchTargets {
531 #[inline]
534 pub fn successors_for_value(&self, value: u128) -> Successors<'_> {
535 let target = self.target_for_value(value);
536 mk_successors(&[], Some(target), None)
537 }
538 }
539
540 impl<'tcx> TerminatorKind<'tcx> {
541 #[inline]
542 pub fn successors(&self) -> Successors<'_> {
543 use self::TerminatorKind::*;
544 match *self {
545 Drop { target: ref t, unwind: UnwindAction::Cleanup(u), drop: Some(d), .. } => {
547 mk_successors(slice::from_ref(t), Some(u), Some(d))
548 }
549 Call { target: Some(ref t), unwind: UnwindAction::Cleanup(u), .. }
551 | Yield { resume: ref t, drop: Some(u), .. }
552 | Drop { target: ref t, unwind: UnwindAction::Cleanup(u), drop: None, .. }
553 | Drop { target: ref t, unwind: _, drop: Some(u), .. }
554 | Assert { target: ref t, unwind: UnwindAction::Cleanup(u), .. }
555 | FalseUnwind { real_target: ref t, unwind: UnwindAction::Cleanup(u) } => {
556 mk_successors(slice::from_ref(t), Some(u), None)
557 }
558 Goto { target: ref t }
560 | Call { target: None, unwind: UnwindAction::Cleanup(ref t), .. }
561 | Call { target: Some(ref t), unwind: _, .. }
562 | Yield { resume: ref t, drop: None, .. }
563 | Drop { target: ref t, unwind: _, .. }
564 | Assert { target: ref t, unwind: _, .. }
565 | FalseUnwind { real_target: ref t, unwind: _ } => {
566 mk_successors(slice::from_ref(t), None, None)
567 }
568 UnwindResume
570 | UnwindTerminate(_)
571 | CoroutineDrop
572 | Return
573 | Unreachable
574 | TailCall { .. }
575 | Call { target: None, unwind: _, .. } => mk_successors(&[], None, None),
576 InlineAsm { ref targets, unwind: UnwindAction::Cleanup(u), .. } => {
578 mk_successors(targets, Some(u), None)
579 }
580 InlineAsm { ref targets, unwind: _, .. } => mk_successors(targets, None, None),
581 SwitchInt { ref targets, .. } => mk_successors(&targets.targets, None, None),
582 FalseEdge { ref real_target, imaginary_target } => {
584 mk_successors(slice::from_ref(real_target), Some(imaginary_target), None)
585 }
586 }
587 }
588
589 #[inline]
590 pub fn successors_mut<'a>(&'a mut self, mut f: impl FnMut(&'a mut BasicBlock)) {
591 use self::TerminatorKind::*;
592 match self {
593 Drop { target, unwind, drop, .. } => {
594 f(target);
595 if let UnwindAction::Cleanup(u) = unwind {
596 f(u)
597 }
598 if let Some(d) = drop {
599 f(d)
600 }
601 }
602 Call { target, unwind, .. } => {
603 if let Some(target) = target {
604 f(target);
605 }
606 if let UnwindAction::Cleanup(u) = unwind {
607 f(u)
608 }
609 }
610 Yield { resume, drop, .. } => {
611 f(resume);
612 if let Some(d) = drop {
613 f(d)
614 }
615 }
616 Assert { target, unwind, .. } | FalseUnwind { real_target: target, unwind } => {
617 f(target);
618 if let UnwindAction::Cleanup(u) = unwind {
619 f(u)
620 }
621 }
622 Goto { target } => {
623 f(target);
624 }
625 UnwindResume
626 | UnwindTerminate(_)
627 | CoroutineDrop
628 | Return
629 | Unreachable
630 | TailCall { .. } => {}
631 InlineAsm { targets, unwind, .. } => {
632 for target in targets {
633 f(target);
634 }
635 if let UnwindAction::Cleanup(u) = unwind {
636 f(u)
637 }
638 }
639 SwitchInt { targets, .. } => {
640 for target in &mut targets.targets {
641 f(target);
642 }
643 }
644 FalseEdge { real_target, imaginary_target } => {
645 f(real_target);
646 f(imaginary_target);
647 }
648 }
649 }
650 }
651}
652
653impl<'tcx> TerminatorKind<'tcx> {
654 #[inline]
655 pub fn unwind(&self) -> Option<&UnwindAction> {
656 match *self {
657 TerminatorKind::Goto { .. }
658 | TerminatorKind::UnwindResume
659 | TerminatorKind::UnwindTerminate(_)
660 | TerminatorKind::Return
661 | TerminatorKind::TailCall { .. }
662 | TerminatorKind::Unreachable
663 | TerminatorKind::CoroutineDrop
664 | TerminatorKind::Yield { .. }
665 | TerminatorKind::SwitchInt { .. }
666 | TerminatorKind::FalseEdge { .. } => None,
667 TerminatorKind::Call { ref unwind, .. }
668 | TerminatorKind::Assert { ref unwind, .. }
669 | TerminatorKind::Drop { ref unwind, .. }
670 | TerminatorKind::FalseUnwind { ref unwind, .. }
671 | TerminatorKind::InlineAsm { ref unwind, .. } => Some(unwind),
672 }
673 }
674
675 #[inline]
676 pub fn unwind_mut(&mut self) -> Option<&mut UnwindAction> {
677 match *self {
678 TerminatorKind::Goto { .. }
679 | TerminatorKind::UnwindResume
680 | TerminatorKind::UnwindTerminate(_)
681 | TerminatorKind::Return
682 | TerminatorKind::TailCall { .. }
683 | TerminatorKind::Unreachable
684 | TerminatorKind::CoroutineDrop
685 | TerminatorKind::Yield { .. }
686 | TerminatorKind::SwitchInt { .. }
687 | TerminatorKind::FalseEdge { .. } => None,
688 TerminatorKind::Call { ref mut unwind, .. }
689 | TerminatorKind::Assert { ref mut unwind, .. }
690 | TerminatorKind::Drop { ref mut unwind, .. }
691 | TerminatorKind::FalseUnwind { ref mut unwind, .. }
692 | TerminatorKind::InlineAsm { ref mut unwind, .. } => Some(unwind),
693 }
694 }
695
696 #[inline]
697 pub fn as_switch(&self) -> Option<(&Operand<'tcx>, &SwitchTargets)> {
698 match self {
699 TerminatorKind::SwitchInt { discr, targets } => Some((discr, targets)),
700 _ => None,
701 }
702 }
703
704 #[inline]
705 pub fn as_goto(&self) -> Option<BasicBlock> {
706 match self {
707 TerminatorKind::Goto { target } => Some(*target),
708 _ => None,
709 }
710 }
711}
712
713#[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)]
714pub enum TerminatorEdges<'mir, 'tcx> {
715 None,
717 Single(BasicBlock),
720 Double(BasicBlock, BasicBlock),
723 AssignOnReturn {
725 return_: &'mir [BasicBlock],
726 cleanup: Option<BasicBlock>,
728 place: CallReturnPlaces<'mir, 'tcx>,
729 },
730 SwitchInt { targets: &'mir SwitchTargets, discr: &'mir Operand<'tcx> },
732}
733
734#[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)]
737pub enum CallReturnPlaces<'a, 'tcx> {
738 Call(Place<'tcx>),
739 Yield(Place<'tcx>),
740 InlineAsm(&'a [InlineAsmOperand<'tcx>]),
741}
742
743impl<'tcx> CallReturnPlaces<'_, 'tcx> {
744 pub fn for_each(&self, mut f: impl FnMut(Place<'tcx>)) {
745 match *self {
746 Self::Call(place) | Self::Yield(place) => f(place),
747 Self::InlineAsm(operands) => {
748 for op in operands {
749 match *op {
750 InlineAsmOperand::Out { place: Some(place), .. }
751 | InlineAsmOperand::InOut { out_place: Some(place), .. } => f(place),
752 _ => {}
753 }
754 }
755 }
756 }
757 }
758}
759
760impl<'tcx> Terminator<'tcx> {
761 pub fn edges(&self) -> TerminatorEdges<'_, 'tcx> {
762 self.kind.edges()
763 }
764}
765
766impl<'tcx> TerminatorKind<'tcx> {
767 pub fn edges(&self) -> TerminatorEdges<'_, 'tcx> {
768 use TerminatorKind::*;
769 match *self {
770 Return
771 | TailCall { .. }
772 | UnwindResume
773 | UnwindTerminate(_)
774 | CoroutineDrop
775 | Unreachable => TerminatorEdges::None,
776
777 Goto { target } => TerminatorEdges::Single(target),
778
779 Assert { target, unwind, expected: _, msg: _, cond: _ }
782 | Drop { target, unwind, place: _, replace: _, drop: _, async_fut: _ }
783 | FalseUnwind { real_target: target, unwind } => match unwind {
784 UnwindAction::Cleanup(unwind) => TerminatorEdges::Double(target, unwind),
785 UnwindAction::Continue | UnwindAction::Terminate(_) | UnwindAction::Unreachable => {
786 TerminatorEdges::Single(target)
787 }
788 },
789
790 FalseEdge { real_target, imaginary_target } => {
791 TerminatorEdges::Double(real_target, imaginary_target)
792 }
793
794 Yield { resume: ref target, drop, resume_arg, value: _ } => {
795 TerminatorEdges::AssignOnReturn {
796 return_: slice::from_ref(target),
797 cleanup: drop,
798 place: CallReturnPlaces::Yield(resume_arg),
799 }
800 }
801
802 Call {
803 unwind,
804 destination,
805 ref target,
806 func: _,
807 args: _,
808 fn_span: _,
809 call_source: _,
810 } => TerminatorEdges::AssignOnReturn {
811 return_: target.as_ref().map(slice::from_ref).unwrap_or_default(),
812 cleanup: unwind.cleanup_block(),
813 place: CallReturnPlaces::Call(destination),
814 },
815
816 InlineAsm {
817 asm_macro: _,
818 template: _,
819 ref operands,
820 options: _,
821 line_spans: _,
822 ref targets,
823 unwind,
824 } => TerminatorEdges::AssignOnReturn {
825 return_: targets,
826 cleanup: unwind.cleanup_block(),
827 place: CallReturnPlaces::InlineAsm(operands),
828 },
829
830 SwitchInt { ref targets, ref discr } => TerminatorEdges::SwitchInt { targets, discr },
831 }
832 }
833}
834
835impl CallSource {
836 pub fn from_hir_call(self) -> bool {
837 #[allow(non_exhaustive_omitted_patterns)] match self {
CallSource::Normal => true,
_ => false,
}matches!(self, CallSource::Normal)
838 }
839}
840
841impl InlineAsmMacro {
842 pub const fn diverges(self, options: InlineAsmOptions) -> bool {
843 match self {
844 InlineAsmMacro::Asm => options.contains(InlineAsmOptions::NORETURN),
845 InlineAsmMacro::NakedAsm => true,
846 }
847 }
848}