1 //! Validates the MIR to ensure that invariants are upheld.
3 use rustc_data_structures::fx::FxHashSet;
4 use rustc_index::bit_set::BitSet;
5 use rustc_infer::traits::Reveal;
6 use rustc_middle::mir::interpret::Scalar;
7 use rustc_middle::mir::visit::NonUseContext::VarDebugInfo;
8 use rustc_middle::mir::visit::{PlaceContext, Visitor};
9 use rustc_middle::mir::{
10 traversal, AggregateKind, BasicBlock, BinOp, Body, BorrowKind, CastKind, CopyNonOverlapping,
11 Local, Location, MirPass, MirPhase, NonDivergingIntrinsic, Operand, Place, PlaceElem, PlaceRef,
12 ProjectionElem, RetagKind, RuntimePhase, Rvalue, SourceScope, Statement, StatementKind,
13 Terminator, TerminatorKind, UnOp, START_BLOCK,
15 use rustc_middle::ty::{self, InstanceDef, ParamEnv, Ty, TyCtxt, TypeVisitable};
16 use rustc_mir_dataflow::impls::MaybeStorageLive;
17 use rustc_mir_dataflow::storage::always_storage_live_locals;
18 use rustc_mir_dataflow::{Analysis, ResultsCursor};
19 use rustc_target::abi::{Size, VariantIdx};
21 #[derive(Copy, Clone, Debug)]
27 pub struct Validator {
28 /// Describes at which point in the pipeline this validation is happening.
30 /// The phase for which we are upholding the dialect. If the given phase forbids a specific
31 /// element, this validator will now emit errors if that specific element is encountered.
32 /// Note that phases that change the dialect cause all *following* phases to check the
33 /// invariants of the new dialect. A phase that changes dialects never checks the new invariants
35 pub mir_phase: MirPhase,
38 impl<'tcx> MirPass<'tcx> for Validator {
39 fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
40 // FIXME(JakobDegen): These bodies never instantiated in codegend anyway, so it's not
41 // terribly important that they pass the validator. However, I think other passes might
42 // still see them, in which case they might be surprised. It would probably be better if we
43 // didn't put this through the MIR pipeline at all.
44 if matches!(body.source.instance, InstanceDef::Intrinsic(..) | InstanceDef::Virtual(..)) {
47 let def_id = body.source.def_id();
48 let mir_phase = self.mir_phase;
49 let param_env = match mir_phase.reveal() {
50 Reveal::UserFacing => tcx.param_env(def_id),
51 Reveal::All => tcx.param_env_reveal_all_normalized(def_id),
54 let always_live_locals = always_storage_live_locals(body);
55 let storage_liveness = MaybeStorageLive::new(std::borrow::Cow::Owned(always_live_locals))
56 .into_engine(tcx, body)
57 .iterate_to_fixpoint()
58 .into_results_cursor(body);
66 reachable_blocks: traversal::reachable_as_bitset(body),
68 place_cache: Vec::new(),
69 value_cache: Vec::new(),
75 struct TypeChecker<'a, 'tcx> {
79 param_env: ParamEnv<'tcx>,
81 reachable_blocks: BitSet<BasicBlock>,
82 storage_liveness: ResultsCursor<'a, 'tcx, MaybeStorageLive<'static>>,
83 place_cache: Vec<PlaceRef<'tcx>>,
84 value_cache: Vec<u128>,
87 impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
89 fn fail(&self, location: Location, msg: impl AsRef<str>) {
90 let span = self.body.source_info(location).span;
91 // We use `delay_span_bug` as we might see broken MIR when other errors have already
93 self.tcx.sess.diagnostic().delay_span_bug(
96 "broken MIR in {:?} ({}) at {:?}:\n{}",
97 self.body.source.instance,
105 fn check_edge(&self, location: Location, bb: BasicBlock, edge_kind: EdgeKind) {
106 if bb == START_BLOCK {
107 self.fail(location, "start block must not have predecessors")
109 if let Some(bb) = self.body.basic_blocks.get(bb) {
110 let src = self.body.basic_blocks.get(location.block).unwrap();
111 match (src.is_cleanup, bb.is_cleanup, edge_kind) {
112 // Non-cleanup blocks can jump to non-cleanup blocks along non-unwind edges
113 (false, false, EdgeKind::Normal)
114 // Non-cleanup blocks can jump to cleanup blocks along unwind edges
115 | (false, true, EdgeKind::Unwind)
116 // Cleanup blocks can jump to cleanup blocks along non-unwind edges
117 | (true, true, EdgeKind::Normal) => {}
118 // All other jumps are invalid
123 "{:?} edge to {:?} violates unwind invariants (cleanup {:?} -> {:?})",
133 self.fail(location, format!("encountered jump to invalid basic block {:?}", bb))
137 /// Check if src can be assigned into dest.
138 /// This is not precise, it will accept some incorrect assignments.
139 fn mir_assign_valid_types(&self, src: Ty<'tcx>, dest: Ty<'tcx>) -> bool {
140 // Fast path before we normalize.
142 // Equal types, all is good.
145 // Normalization reveals opaque types, but we may be validating MIR while computing
146 // said opaque types, causing cycles.
147 if (src, dest).has_opaque_types() {
151 crate::util::is_subtype(self.tcx, self.param_env, src, dest)
155 impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
156 fn visit_local(&mut self, local: Local, context: PlaceContext, location: Location) {
157 if self.body.local_decls.get(local).is_none() {
160 format!("local {:?} has no corresponding declaration in `body.local_decls`", local),
164 if self.reachable_blocks.contains(location.block) && context.is_use() {
165 // We check that the local is live whenever it is used. Technically, violating this
166 // restriction is only UB and not actually indicative of not well-formed MIR. This means
167 // that an optimization which turns MIR that already has UB into MIR that fails this
168 // check is not necessarily wrong. However, we have no such optimizations at the moment,
169 // and so we include this check anyway to help us catch bugs. If you happen to write an
170 // optimization that might cause this to incorrectly fire, feel free to remove this
172 self.storage_liveness.seek_after_primary_effect(location);
173 let locals_with_storage = self.storage_liveness.get();
174 if !locals_with_storage.contains(local) {
175 self.fail(location, format!("use of local {:?}, which has no storage here", local));
180 fn visit_operand(&mut self, operand: &Operand<'tcx>, location: Location) {
181 // This check is somewhat expensive, so only run it when -Zvalidate-mir is passed.
182 if self.tcx.sess.opts.unstable_opts.validate_mir
183 && self.mir_phase < MirPhase::Runtime(RuntimePhase::Initial)
185 // `Operand::Copy` is only supposed to be used with `Copy` types.
186 if let Operand::Copy(place) = operand {
187 let ty = place.ty(&self.body.local_decls, self.tcx).ty;
189 if !ty.is_copy_modulo_regions(self.tcx, self.param_env) {
190 self.fail(location, format!("`Operand::Copy` with non-`Copy` type {}", ty));
195 self.super_operand(operand, location);
198 fn visit_projection_elem(
201 proj_base: &[PlaceElem<'tcx>],
202 elem: PlaceElem<'tcx>,
203 context: PlaceContext,
207 ProjectionElem::Index(index) => {
208 let index_ty = self.body.local_decls[index].ty;
209 if index_ty != self.tcx.types.usize {
210 self.fail(location, format!("bad index ({:?} != usize)", index_ty))
213 ProjectionElem::Deref
214 if self.mir_phase >= MirPhase::Runtime(RuntimePhase::PostCleanup) =>
216 let base_ty = Place::ty_from(local, proj_base, &self.body.local_decls, self.tcx).ty;
218 if base_ty.is_box() {
221 format!("{:?} dereferenced after ElaborateBoxDerefs", base_ty),
225 ProjectionElem::Field(f, ty) => {
226 let parent = Place { local, projection: self.tcx.intern_place_elems(proj_base) };
227 let parent_ty = parent.ty(&self.body.local_decls, self.tcx);
228 let fail_out_of_bounds = |this: &Self, location| {
229 this.fail(location, format!("Out of bounds field {:?} for {:?}", f, parent_ty));
231 let check_equal = |this: &Self, location, f_ty| {
232 if !this.mir_assign_valid_types(ty, f_ty) {
236 "Field projection `{:?}.{:?}` specified type `{:?}`, but actual type is `{:?}`",
243 let kind = match parent_ty.ty.kind() {
244 &ty::Alias(ty::Opaque, ty::AliasTy { def_id, substs, .. }) => {
245 self.tcx.bound_type_of(def_id).subst(self.tcx, substs).kind()
251 ty::Tuple(fields) => {
252 let Some(f_ty) = fields.get(f.as_usize()) else {
253 fail_out_of_bounds(self, location);
256 check_equal(self, location, *f_ty);
258 ty::Adt(adt_def, substs) => {
259 let var = parent_ty.variant_index.unwrap_or(VariantIdx::from_u32(0));
260 let Some(field) = adt_def.variant(var).fields.get(f.as_usize()) else {
261 fail_out_of_bounds(self, location);
264 check_equal(self, location, field.ty(self.tcx, substs));
266 ty::Closure(_, substs) => {
267 let substs = substs.as_closure();
268 let Some(f_ty) = substs.upvar_tys().nth(f.as_usize()) else {
269 fail_out_of_bounds(self, location);
272 check_equal(self, location, f_ty);
274 &ty::Generator(def_id, substs, _) => {
275 let f_ty = if let Some(var) = parent_ty.variant_index {
276 let gen_body = if def_id == self.body.source.def_id() {
279 self.tcx.optimized_mir(def_id)
282 let Some(layout) = gen_body.generator_layout() else {
283 self.fail(location, format!("No generator layout for {:?}", parent_ty));
287 let Some(&local) = layout.variant_fields[var].get(f) else {
288 fail_out_of_bounds(self, location);
292 let Some(&f_ty) = layout.field_tys.get(local) else {
293 self.fail(location, format!("Out of bounds local {:?} for {:?}", local, parent_ty));
299 let Some(f_ty) = substs.as_generator().prefix_tys().nth(f.index()) else {
300 fail_out_of_bounds(self, location);
307 check_equal(self, location, f_ty);
310 self.fail(location, format!("{:?} does not have fields", parent_ty.ty));
316 self.super_projection_elem(local, proj_base, elem, context, location);
319 fn visit_place(&mut self, place: &Place<'tcx>, cntxt: PlaceContext, location: Location) {
320 // Set off any `bug!`s in the type computation code
321 let _ = place.ty(&self.body.local_decls, self.tcx);
323 if self.mir_phase >= MirPhase::Runtime(RuntimePhase::Initial)
324 && place.projection.len() > 1
325 && cntxt != PlaceContext::NonUse(VarDebugInfo)
326 && place.projection[1..].contains(&ProjectionElem::Deref)
328 self.fail(location, format!("{:?}, has deref at the wrong place", place));
331 self.super_place(place, cntxt, location);
334 fn visit_rvalue(&mut self, rvalue: &Rvalue<'tcx>, location: Location) {
335 macro_rules! check_kinds {
336 ($t:expr, $text:literal, $($patterns:tt)*) => {
337 if !matches!(($t).kind(), $($patterns)*) {
338 self.fail(location, format!($text, $t));
343 Rvalue::Use(_) | Rvalue::CopyForDeref(_) => {}
344 Rvalue::Aggregate(agg_kind, _) => {
345 let disallowed = match **agg_kind {
346 AggregateKind::Array(..) => false,
347 _ => self.mir_phase >= MirPhase::Runtime(RuntimePhase::PostCleanup),
352 format!("{:?} have been lowered to field assignments", rvalue),
356 Rvalue::Ref(_, BorrowKind::Shallow, _) => {
357 if self.mir_phase >= MirPhase::Runtime(RuntimePhase::Initial) {
360 "`Assign` statement with a `Shallow` borrow should have been removed in runtime MIR",
364 Rvalue::Ref(..) => {}
366 let pty = p.ty(&self.body.local_decls, self.tcx).ty;
369 "Cannot compute length of non-array type {:?}",
370 ty::Array(..) | ty::Slice(..)
373 Rvalue::BinaryOp(op, vals) => {
375 let a = vals.0.ty(&self.body.local_decls, self.tcx);
376 let b = vals.1.ty(&self.body.local_decls, self.tcx);
379 check_kinds!(a, "Cannot offset non-pointer type {:?}", ty::RawPtr(..));
380 if b != self.tcx.types.isize && b != self.tcx.types.usize {
381 self.fail(location, format!("Cannot offset by non-isize type {:?}", b));
384 Eq | Lt | Le | Ne | Ge | Gt => {
388 "Cannot compare type {:?}",
398 // The function pointer types can have lifetimes
399 if !self.mir_assign_valid_types(a, b) {
402 format!("Cannot compare unequal types {:?} and {:?}", a, b),
410 "Cannot shift non-integer type {:?}",
411 ty::Uint(..) | ty::Int(..)
415 BitAnd | BitOr | BitXor => {
419 "Cannot perform bitwise op on type {:?}",
420 ty::Uint(..) | ty::Int(..) | ty::Bool
427 "Cannot perform bitwise op on unequal types {:?} and {:?}",
433 Add | Sub | Mul | Div | Rem => {
437 "Cannot perform arithmetic on type {:?}",
438 ty::Uint(..) | ty::Int(..) | ty::Float(..)
445 "Cannot perform arithmetic on unequal types {:?} and {:?}",
453 Rvalue::CheckedBinaryOp(op, vals) => {
455 let a = vals.0.ty(&self.body.local_decls, self.tcx);
456 let b = vals.1.ty(&self.body.local_decls, self.tcx);
462 "Cannot perform checked arithmetic on type {:?}",
463 ty::Uint(..) | ty::Int(..)
470 "Cannot perform checked arithmetic on unequal types {:?} and {:?}",
480 "Cannot perform checked shift on non-integer type {:?}",
481 ty::Uint(..) | ty::Int(..)
485 _ => self.fail(location, format!("There is no checked version of {:?}", op)),
488 Rvalue::UnaryOp(op, operand) => {
489 let a = operand.ty(&self.body.local_decls, self.tcx);
492 check_kinds!(a, "Cannot negate type {:?}", ty::Int(..) | ty::Float(..))
497 "Cannot binary not type {:?}",
498 ty::Int(..) | ty::Uint(..) | ty::Bool
503 Rvalue::ShallowInitBox(operand, _) => {
504 let a = operand.ty(&self.body.local_decls, self.tcx);
505 check_kinds!(a, "Cannot shallow init type {:?}", ty::RawPtr(..));
507 Rvalue::Cast(kind, operand, target_type) => {
508 let op_ty = operand.ty(self.body, self.tcx);
510 CastKind::DynStar => {
511 // FIXME(dyn-star): make sure nothing needs to be done here.
513 // FIXME: Add Checks for these
514 CastKind::PointerFromExposedAddress
515 | CastKind::PointerExposeAddress
516 | CastKind::Pointer(_) => {}
517 CastKind::IntToInt | CastKind::IntToFloat => {
518 let input_valid = op_ty.is_integral() || op_ty.is_char() || op_ty.is_bool();
519 let target_valid = target_type.is_numeric() || target_type.is_char();
520 if !input_valid || !target_valid {
523 format!("Wrong cast kind {kind:?} for the type {op_ty}",),
527 CastKind::FnPtrToPtr | CastKind::PtrToPtr => {
528 if !(op_ty.is_any_ptr() && target_type.is_unsafe_ptr()) {
529 self.fail(location, "Can't cast {op_ty} into 'Ptr'");
532 CastKind::FloatToFloat | CastKind::FloatToInt => {
533 if !op_ty.is_floating_point() || !target_type.is_numeric() {
537 "Trying to cast non 'Float' as {kind:?} into {target_type:?}"
545 | Rvalue::ThreadLocalRef(_)
546 | Rvalue::AddressOf(_, _)
547 | Rvalue::NullaryOp(_, _)
548 | Rvalue::Discriminant(_) => {}
550 self.super_rvalue(rvalue, location);
553 fn visit_statement(&mut self, statement: &Statement<'tcx>, location: Location) {
554 match &statement.kind {
555 StatementKind::Assign(box (dest, rvalue)) => {
556 // LHS and RHS of the assignment must have the same type.
557 let left_ty = dest.ty(&self.body.local_decls, self.tcx).ty;
558 let right_ty = rvalue.ty(&self.body.local_decls, self.tcx);
559 if !self.mir_assign_valid_types(right_ty, left_ty) {
563 "encountered `{:?}` with incompatible types:\n\
564 left-hand side has type: {}\n\
565 right-hand side has type: {}",
566 statement.kind, left_ty, right_ty,
570 if let Rvalue::CopyForDeref(place) = rvalue {
571 if !place.ty(&self.body.local_decls, self.tcx).ty.builtin_deref(true).is_some()
575 "`CopyForDeref` should only be used for dereferenceable types",
579 // FIXME(JakobDegen): Check this for all rvalues, not just this one.
580 if let Rvalue::Use(Operand::Copy(src) | Operand::Move(src)) = rvalue {
581 // The sides of an assignment must not alias. Currently this just checks whether
582 // the places are identical.
586 "encountered `Assign` statement with overlapping memory",
591 StatementKind::AscribeUserType(..) => {
592 if self.mir_phase >= MirPhase::Runtime(RuntimePhase::Initial) {
595 "`AscribeUserType` should have been removed after drop lowering phase",
599 StatementKind::FakeRead(..) => {
600 if self.mir_phase >= MirPhase::Runtime(RuntimePhase::Initial) {
603 "`FakeRead` should have been removed after drop lowering phase",
607 StatementKind::Intrinsic(box NonDivergingIntrinsic::Assume(op)) => {
608 let ty = op.ty(&self.body.local_decls, self.tcx);
612 format!("`assume` argument must be `bool`, but got: `{}`", ty),
616 StatementKind::Intrinsic(box NonDivergingIntrinsic::CopyNonOverlapping(
617 CopyNonOverlapping { src, dst, count },
619 let src_ty = src.ty(&self.body.local_decls, self.tcx);
620 let op_src_ty = if let Some(src_deref) = src_ty.builtin_deref(true) {
625 format!("Expected src to be ptr in copy_nonoverlapping, got: {}", src_ty),
629 let dst_ty = dst.ty(&self.body.local_decls, self.tcx);
630 let op_dst_ty = if let Some(dst_deref) = dst_ty.builtin_deref(true) {
635 format!("Expected dst to be ptr in copy_nonoverlapping, got: {}", dst_ty),
639 // since CopyNonOverlapping is parametrized by 1 type,
640 // we only need to check that they are equal and not keep an extra parameter.
641 if !self.mir_assign_valid_types(op_src_ty, op_dst_ty) {
642 self.fail(location, format!("bad arg ({:?} != {:?})", op_src_ty, op_dst_ty));
645 let op_cnt_ty = count.ty(&self.body.local_decls, self.tcx);
646 if op_cnt_ty != self.tcx.types.usize {
647 self.fail(location, format!("bad arg ({:?} != usize)", op_cnt_ty))
650 StatementKind::SetDiscriminant { place, .. } => {
651 if self.mir_phase < MirPhase::Runtime(RuntimePhase::Initial) {
652 self.fail(location, "`SetDiscriminant`is not allowed until deaggregation");
654 let pty = place.ty(&self.body.local_decls, self.tcx).ty.kind();
655 if !matches!(pty, ty::Adt(..) | ty::Generator(..) | ty::Alias(ty::Opaque, ..)) {
659 "`SetDiscriminant` is only allowed on ADTs and generators, not {:?}",
665 StatementKind::Deinit(..) => {
666 if self.mir_phase < MirPhase::Runtime(RuntimePhase::Initial) {
667 self.fail(location, "`Deinit`is not allowed until deaggregation");
670 StatementKind::Retag(kind, _) => {
671 // FIXME(JakobDegen) The validator should check that `self.mir_phase <
672 // DropsLowered`. However, this causes ICEs with generation of drop shims, which
673 // seem to fail to set their `MirPhase` correctly.
674 if *kind == RetagKind::Raw || *kind == RetagKind::TwoPhase {
675 self.fail(location, format!("explicit `{:?}` is forbidden", kind));
678 StatementKind::StorageLive(..)
679 | StatementKind::StorageDead(..)
680 | StatementKind::Coverage(_)
681 | StatementKind::Nop => {}
684 self.super_statement(statement, location);
687 fn visit_terminator(&mut self, terminator: &Terminator<'tcx>, location: Location) {
688 match &terminator.kind {
689 TerminatorKind::Goto { target } => {
690 self.check_edge(location, *target, EdgeKind::Normal);
692 TerminatorKind::SwitchInt { targets, discr } => {
693 let switch_ty = discr.ty(&self.body.local_decls, self.tcx);
695 let target_width = self.tcx.sess.target.pointer_width;
697 let size = Size::from_bits(match switch_ty.kind() {
698 ty::Uint(uint) => uint.normalize(target_width).bit_width().unwrap(),
699 ty::Int(int) => int.normalize(target_width).bit_width().unwrap(),
702 other => bug!("unhandled type: {:?}", other),
705 for (value, target) in targets.iter() {
706 if Scalar::<()>::try_from_uint(value, size).is_none() {
709 format!("the value {:#x} is not a proper {:?}", value, switch_ty),
713 self.check_edge(location, target, EdgeKind::Normal);
715 self.check_edge(location, targets.otherwise(), EdgeKind::Normal);
717 self.value_cache.clear();
718 self.value_cache.extend(targets.iter().map(|(value, _)| value));
719 let all_len = self.value_cache.len();
720 self.value_cache.sort_unstable();
721 self.value_cache.dedup();
722 let has_duplicates = all_len != self.value_cache.len();
727 "duplicated values in `SwitchInt` terminator: {:?}",
733 TerminatorKind::Drop { target, unwind, .. } => {
734 self.check_edge(location, *target, EdgeKind::Normal);
735 if let Some(unwind) = unwind {
736 self.check_edge(location, *unwind, EdgeKind::Unwind);
739 TerminatorKind::DropAndReplace { target, unwind, .. } => {
740 if self.mir_phase >= MirPhase::Runtime(RuntimePhase::Initial) {
743 "`DropAndReplace` should have been removed during drop elaboration",
746 self.check_edge(location, *target, EdgeKind::Normal);
747 if let Some(unwind) = unwind {
748 self.check_edge(location, *unwind, EdgeKind::Unwind);
751 TerminatorKind::Call { func, args, destination, target, cleanup, .. } => {
752 let func_ty = func.ty(&self.body.local_decls, self.tcx);
753 match func_ty.kind() {
754 ty::FnPtr(..) | ty::FnDef(..) => {}
757 format!("encountered non-callable type {} in `Call` terminator", func_ty),
760 if let Some(target) = target {
761 self.check_edge(location, *target, EdgeKind::Normal);
763 if let Some(cleanup) = cleanup {
764 self.check_edge(location, *cleanup, EdgeKind::Unwind);
767 // The call destination place and Operand::Move place used as an argument might be
768 // passed by a reference to the callee. Consequently they must be non-overlapping.
769 // Currently this simply checks for duplicate places.
770 self.place_cache.clear();
771 self.place_cache.push(destination.as_ref());
773 if let Operand::Move(place) = arg {
774 self.place_cache.push(place.as_ref());
777 let all_len = self.place_cache.len();
778 let mut dedup = FxHashSet::default();
779 self.place_cache.retain(|p| dedup.insert(*p));
780 let has_duplicates = all_len != self.place_cache.len();
785 "encountered overlapping memory in `Call` terminator: {:?}",
791 TerminatorKind::Assert { cond, target, cleanup, .. } => {
792 let cond_ty = cond.ty(&self.body.local_decls, self.tcx);
793 if cond_ty != self.tcx.types.bool {
797 "encountered non-boolean condition of type {} in `Assert` terminator",
802 self.check_edge(location, *target, EdgeKind::Normal);
803 if let Some(cleanup) = cleanup {
804 self.check_edge(location, *cleanup, EdgeKind::Unwind);
807 TerminatorKind::Yield { resume, drop, .. } => {
808 if self.body.generator.is_none() {
809 self.fail(location, "`Yield` cannot appear outside generator bodies");
811 if self.mir_phase >= MirPhase::Runtime(RuntimePhase::Initial) {
812 self.fail(location, "`Yield` should have been replaced by generator lowering");
814 self.check_edge(location, *resume, EdgeKind::Normal);
815 if let Some(drop) = drop {
816 self.check_edge(location, *drop, EdgeKind::Normal);
819 TerminatorKind::FalseEdge { real_target, imaginary_target } => {
820 if self.mir_phase >= MirPhase::Runtime(RuntimePhase::Initial) {
823 "`FalseEdge` should have been removed after drop elaboration",
826 self.check_edge(location, *real_target, EdgeKind::Normal);
827 self.check_edge(location, *imaginary_target, EdgeKind::Normal);
829 TerminatorKind::FalseUnwind { real_target, unwind } => {
830 if self.mir_phase >= MirPhase::Runtime(RuntimePhase::Initial) {
833 "`FalseUnwind` should have been removed after drop elaboration",
836 self.check_edge(location, *real_target, EdgeKind::Normal);
837 if let Some(unwind) = unwind {
838 self.check_edge(location, *unwind, EdgeKind::Unwind);
841 TerminatorKind::InlineAsm { destination, cleanup, .. } => {
842 if let Some(destination) = destination {
843 self.check_edge(location, *destination, EdgeKind::Normal);
845 if let Some(cleanup) = cleanup {
846 self.check_edge(location, *cleanup, EdgeKind::Unwind);
849 TerminatorKind::GeneratorDrop => {
850 if self.body.generator.is_none() {
851 self.fail(location, "`GeneratorDrop` cannot appear outside generator bodies");
853 if self.mir_phase >= MirPhase::Runtime(RuntimePhase::Initial) {
856 "`GeneratorDrop` should have been replaced by generator lowering",
860 TerminatorKind::Resume | TerminatorKind::Abort => {
861 let bb = location.block;
862 if !self.body.basic_blocks[bb].is_cleanup {
863 self.fail(location, "Cannot `Resume` or `Abort` from non-cleanup basic block")
866 TerminatorKind::Return => {
867 let bb = location.block;
868 if self.body.basic_blocks[bb].is_cleanup {
869 self.fail(location, "Cannot `Return` from cleanup basic block")
872 TerminatorKind::Unreachable => {}
875 self.super_terminator(terminator, location);
878 fn visit_source_scope(&mut self, scope: SourceScope) {
879 if self.body.source_scopes.get(scope).is_none() {
880 self.tcx.sess.diagnostic().delay_span_bug(
883 "broken MIR in {:?} ({}):\ninvalid source scope {:?}",
884 self.body.source.instance, self.when, scope,