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::infer::TyCtxtInferExt;
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, Local, Location,
11 MirPass, MirPhase, Operand, Place, PlaceElem, PlaceRef, ProjectionElem, Rvalue, SourceScope,
12 Statement, StatementKind, Terminator, TerminatorKind, UnOp, START_BLOCK,
14 use rustc_middle::ty::fold::BottomUpFolder;
15 use rustc_middle::ty::subst::Subst;
16 use rustc_middle::ty::{self, InstanceDef, ParamEnv, Ty, TyCtxt, TypeFoldable, TypeVisitable};
17 use rustc_mir_dataflow::impls::MaybeStorageLive;
18 use rustc_mir_dataflow::storage::always_storage_live_locals;
19 use rustc_mir_dataflow::{Analysis, ResultsCursor};
20 use rustc_target::abi::{Size, VariantIdx};
22 #[derive(Copy, Clone, Debug)]
28 pub struct Validator {
29 /// Describes at which point in the pipeline this validation is happening.
31 /// The phase for which we are upholding the dialect. If the given phase forbids a specific
32 /// element, this validator will now emit errors if that specific element is encountered.
33 /// Note that phases that change the dialect cause all *following* phases to check the
34 /// invariants of the new dialect. A phase that changes dialects never checks the new invariants
36 pub mir_phase: MirPhase,
39 impl<'tcx> MirPass<'tcx> for Validator {
40 fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
41 // FIXME(JakobDegen): These bodies never instantiated in codegend anyway, so it's not
42 // terribly important that they pass the validator. However, I think other passes might
43 // still see them, in which case they might be surprised. It would probably be better if we
44 // didn't put this through the MIR pipeline at all.
45 if matches!(body.source.instance, InstanceDef::Intrinsic(..) | InstanceDef::Virtual(..)) {
48 let def_id = body.source.def_id();
49 let param_env = tcx.param_env(def_id);
50 let mir_phase = self.mir_phase;
52 let always_live_locals = always_storage_live_locals(body);
53 let storage_liveness = MaybeStorageLive::new(always_live_locals)
54 .into_engine(tcx, body)
55 .iterate_to_fixpoint()
56 .into_results_cursor(body);
64 reachable_blocks: traversal::reachable_as_bitset(body),
66 place_cache: Vec::new(),
67 value_cache: Vec::new(),
73 /// Returns whether the two types are equal up to lifetimes.
74 /// All lifetimes, including higher-ranked ones, get ignored for this comparison.
75 /// (This is unlike the `erasing_regions` methods, which keep higher-ranked lifetimes for soundness reasons.)
77 /// The point of this function is to approximate "equal up to subtyping". However,
78 /// the approximation is incorrect as variance is ignored.
79 pub fn equal_up_to_regions<'tcx>(
81 param_env: ParamEnv<'tcx>,
90 // Normalize lifetimes away on both sides, then compare.
91 let normalize = |ty: Ty<'tcx>| {
92 tcx.normalize_erasing_regions(
94 ty.fold_with(&mut BottomUpFolder {
96 // FIXME: We erase all late-bound lifetimes, but this is not fully correct.
97 // If you have a type like `<for<'a> fn(&'a u32) as SomeTrait>::Assoc`,
98 // this is not necessarily equivalent to `<fn(&'static u32) as SomeTrait>::Assoc`,
99 // since one may have an `impl SomeTrait for fn(&32)` and
100 // `impl SomeTrait for fn(&'static u32)` at the same time which
101 // specify distinct values for Assoc. (See also #56105)
102 lt_op: |_| tcx.lifetimes.re_erased,
103 // Leave consts and types unchanged.
109 tcx.infer_ctxt().enter(|infcx| infcx.can_eq(param_env, normalize(src), normalize(dest)).is_ok())
112 struct TypeChecker<'a, 'tcx> {
114 body: &'a Body<'tcx>,
116 param_env: ParamEnv<'tcx>,
118 reachable_blocks: BitSet<BasicBlock>,
119 storage_liveness: ResultsCursor<'a, 'tcx, MaybeStorageLive>,
120 place_cache: Vec<PlaceRef<'tcx>>,
121 value_cache: Vec<u128>,
124 impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
125 fn fail(&self, location: Location, msg: impl AsRef<str>) {
126 let span = self.body.source_info(location).span;
127 // We use `delay_span_bug` as we might see broken MIR when other errors have already
129 self.tcx.sess.diagnostic().delay_span_bug(
132 "broken MIR in {:?} ({}) at {:?}:\n{}",
133 self.body.source.instance,
141 fn check_edge(&self, location: Location, bb: BasicBlock, edge_kind: EdgeKind) {
142 if bb == START_BLOCK {
143 self.fail(location, "start block must not have predecessors")
145 if let Some(bb) = self.body.basic_blocks().get(bb) {
146 let src = self.body.basic_blocks().get(location.block).unwrap();
147 match (src.is_cleanup, bb.is_cleanup, edge_kind) {
148 // Non-cleanup blocks can jump to non-cleanup blocks along non-unwind edges
149 (false, false, EdgeKind::Normal)
150 // Non-cleanup blocks can jump to cleanup blocks along unwind edges
151 | (false, true, EdgeKind::Unwind)
152 // Cleanup blocks can jump to cleanup blocks along non-unwind edges
153 | (true, true, EdgeKind::Normal) => {}
154 // All other jumps are invalid
159 "{:?} edge to {:?} violates unwind invariants (cleanup {:?} -> {:?})",
169 self.fail(location, format!("encountered jump to invalid basic block {:?}", bb))
173 /// Check if src can be assigned into dest.
174 /// This is not precise, it will accept some incorrect assignments.
175 fn mir_assign_valid_types(&self, src: Ty<'tcx>, dest: Ty<'tcx>) -> bool {
176 // Fast path before we normalize.
178 // Equal types, all is good.
181 // Normalization reveals opaque types, but we may be validating MIR while computing
182 // said opaque types, causing cycles.
183 if (src, dest).has_opaque_types() {
186 // Normalize projections and things like that.
187 let param_env = self.param_env.with_reveal_all_normalized(self.tcx);
188 let src = self.tcx.normalize_erasing_regions(param_env, src);
189 let dest = self.tcx.normalize_erasing_regions(param_env, dest);
191 // Type-changing assignments can happen when subtyping is used. While
192 // all normal lifetimes are erased, higher-ranked types with their
193 // late-bound lifetimes are still around and can lead to type
194 // differences. So we compare ignoring lifetimes.
195 equal_up_to_regions(self.tcx, param_env, src, dest)
199 impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
200 fn visit_local(&mut self, local: Local, context: PlaceContext, location: Location) {
201 if self.body.local_decls.get(local).is_none() {
204 format!("local {:?} has no corresponding declaration in `body.local_decls`", local),
208 if self.reachable_blocks.contains(location.block) && context.is_use() {
209 // We check that the local is live whenever it is used. Technically, violating this
210 // restriction is only UB and not actually indicative of not well-formed MIR. This means
211 // that an optimization which turns MIR that already has UB into MIR that fails this
212 // check is not necessarily wrong. However, we have no such optimizations at the moment,
213 // and so we include this check anyway to help us catch bugs. If you happen to write an
214 // optimization that might cause this to incorrectly fire, feel free to remove this
216 self.storage_liveness.seek_after_primary_effect(location);
217 let locals_with_storage = self.storage_liveness.get();
218 if !locals_with_storage.contains(local) {
219 self.fail(location, format!("use of local {:?}, which has no storage here", local));
224 fn visit_operand(&mut self, operand: &Operand<'tcx>, location: Location) {
225 // This check is somewhat expensive, so only run it when -Zvalidate-mir is passed.
226 if self.tcx.sess.opts.unstable_opts.validate_mir && self.mir_phase < MirPhase::DropsLowered
228 // `Operand::Copy` is only supposed to be used with `Copy` types.
229 if let Operand::Copy(place) = operand {
230 let ty = place.ty(&self.body.local_decls, self.tcx).ty;
231 let span = self.body.source_info(location).span;
233 if !ty.is_copy_modulo_regions(self.tcx.at(span), self.param_env) {
234 self.fail(location, format!("`Operand::Copy` with non-`Copy` type {}", ty));
239 self.super_operand(operand, location);
242 fn visit_projection_elem(
245 proj_base: &[PlaceElem<'tcx>],
246 elem: PlaceElem<'tcx>,
247 context: PlaceContext,
251 ProjectionElem::Index(index) => {
252 let index_ty = self.body.local_decls[index].ty;
253 if index_ty != self.tcx.types.usize {
254 self.fail(location, format!("bad index ({:?} != usize)", index_ty))
257 ProjectionElem::Deref if self.mir_phase >= MirPhase::GeneratorsLowered => {
258 let base_ty = Place::ty_from(local, proj_base, &self.body.local_decls, self.tcx).ty;
260 if base_ty.is_box() {
263 format!("{:?} dereferenced after ElaborateBoxDerefs", base_ty),
267 ProjectionElem::Field(f, ty) => {
268 let parent = Place { local, projection: self.tcx.intern_place_elems(proj_base) };
269 let parent_ty = parent.ty(&self.body.local_decls, self.tcx);
270 let fail_out_of_bounds = |this: &Self, location| {
271 this.fail(location, format!("Out of bounds field {:?} for {:?}", f, parent_ty));
273 let check_equal = |this: &Self, location, f_ty| {
274 if !this.mir_assign_valid_types(ty, f_ty) {
278 "Field projection `{:?}.{:?}` specified type `{:?}`, but actual type is {:?}",
285 let kind = match parent_ty.ty.kind() {
286 &ty::Opaque(def_id, substs) => {
287 self.tcx.bound_type_of(def_id).subst(self.tcx, substs).kind()
293 ty::Tuple(fields) => {
294 let Some(f_ty) = fields.get(f.as_usize()) else {
295 fail_out_of_bounds(self, location);
298 check_equal(self, location, *f_ty);
300 ty::Adt(adt_def, substs) => {
301 let var = parent_ty.variant_index.unwrap_or(VariantIdx::from_u32(0));
302 let Some(field) = adt_def.variant(var).fields.get(f.as_usize()) else {
303 fail_out_of_bounds(self, location);
306 check_equal(self, location, field.ty(self.tcx, substs));
308 ty::Closure(_, substs) => {
309 let substs = substs.as_closure();
310 let Some(f_ty) = substs.upvar_tys().nth(f.as_usize()) else {
311 fail_out_of_bounds(self, location);
314 check_equal(self, location, f_ty);
316 &ty::Generator(def_id, substs, _) => {
317 let f_ty = if let Some(var) = parent_ty.variant_index {
318 let gen_body = if def_id == self.body.source.def_id() {
321 self.tcx.optimized_mir(def_id)
324 let Some(layout) = gen_body.generator_layout() else {
325 self.fail(location, format!("No generator layout for {:?}", parent_ty));
329 let Some(&local) = layout.variant_fields[var].get(f) else {
330 fail_out_of_bounds(self, location);
334 let Some(&f_ty) = layout.field_tys.get(local) else {
335 self.fail(location, format!("Out of bounds local {:?} for {:?}", local, parent_ty));
341 let Some(f_ty) = substs.as_generator().prefix_tys().nth(f.index()) else {
342 fail_out_of_bounds(self, location);
349 check_equal(self, location, f_ty);
352 self.fail(location, format!("{:?} does not have fields", parent_ty.ty));
358 self.super_projection_elem(local, proj_base, elem, context, location);
361 fn visit_place(&mut self, place: &Place<'tcx>, cntxt: PlaceContext, location: Location) {
362 // Set off any `bug!`s in the type computation code
363 let _ = place.ty(&self.body.local_decls, self.tcx);
365 if self.mir_phase >= MirPhase::Derefered
366 && place.projection.len() > 1
367 && cntxt != PlaceContext::NonUse(VarDebugInfo)
368 && place.projection[1..].contains(&ProjectionElem::Deref)
370 self.fail(location, format!("{:?}, has deref at the wrong place", place));
373 self.super_place(place, cntxt, location);
376 fn visit_rvalue(&mut self, rvalue: &Rvalue<'tcx>, location: Location) {
377 macro_rules! check_kinds {
378 ($t:expr, $text:literal, $($patterns:tt)*) => {
379 if !matches!(($t).kind(), $($patterns)*) {
380 self.fail(location, format!($text, $t));
385 Rvalue::Use(_) | Rvalue::CopyForDeref(_) => {}
386 Rvalue::Aggregate(agg_kind, _) => {
387 let disallowed = match **agg_kind {
388 AggregateKind::Array(..) => false,
389 AggregateKind::Generator(..) => self.mir_phase >= MirPhase::GeneratorsLowered,
390 _ => self.mir_phase >= MirPhase::Deaggregated,
395 format!("{:?} have been lowered to field assignments", rvalue),
399 Rvalue::Ref(_, BorrowKind::Shallow, _) => {
400 if self.mir_phase >= MirPhase::DropsLowered {
403 "`Assign` statement with a `Shallow` borrow should have been removed after drop lowering phase",
407 Rvalue::Ref(..) => {}
409 let pty = p.ty(&self.body.local_decls, self.tcx).ty;
412 "Cannot compute length of non-array type {:?}",
413 ty::Array(..) | ty::Slice(..)
416 Rvalue::BinaryOp(op, vals) => {
418 let a = vals.0.ty(&self.body.local_decls, self.tcx);
419 let b = vals.1.ty(&self.body.local_decls, self.tcx);
422 check_kinds!(a, "Cannot offset non-pointer type {:?}", ty::RawPtr(..));
423 if b != self.tcx.types.isize && b != self.tcx.types.usize {
424 self.fail(location, format!("Cannot offset by non-isize type {:?}", b));
427 Eq | Lt | Le | Ne | Ge | Gt => {
431 "Cannot compare type {:?}",
441 // The function pointer types can have lifetimes
442 if !self.mir_assign_valid_types(a, b) {
445 format!("Cannot compare unequal types {:?} and {:?}", a, b),
453 "Cannot shift non-integer type {:?}",
454 ty::Uint(..) | ty::Int(..)
458 BitAnd | BitOr | BitXor => {
462 "Cannot perform bitwise op on type {:?}",
463 ty::Uint(..) | ty::Int(..) | ty::Bool
470 "Cannot perform bitwise op on unequal types {:?} and {:?}",
476 Add | Sub | Mul | Div | Rem => {
480 "Cannot perform arithmetic on type {:?}",
481 ty::Uint(..) | ty::Int(..) | ty::Float(..)
488 "Cannot perform arithmetic on unequal types {:?} and {:?}",
496 Rvalue::CheckedBinaryOp(op, vals) => {
498 let a = vals.0.ty(&self.body.local_decls, self.tcx);
499 let b = vals.1.ty(&self.body.local_decls, self.tcx);
505 "Cannot perform checked arithmetic on type {:?}",
506 ty::Uint(..) | ty::Int(..)
513 "Cannot perform checked arithmetic on unequal types {:?} and {:?}",
523 "Cannot perform checked shift on non-integer type {:?}",
524 ty::Uint(..) | ty::Int(..)
528 _ => self.fail(location, format!("There is no checked version of {:?}", op)),
531 Rvalue::UnaryOp(op, operand) => {
532 let a = operand.ty(&self.body.local_decls, self.tcx);
535 check_kinds!(a, "Cannot negate type {:?}", ty::Int(..) | ty::Float(..))
540 "Cannot binary not type {:?}",
541 ty::Int(..) | ty::Uint(..) | ty::Bool
546 Rvalue::ShallowInitBox(operand, _) => {
547 let a = operand.ty(&self.body.local_decls, self.tcx);
548 check_kinds!(a, "Cannot shallow init type {:?}", ty::RawPtr(..));
550 Rvalue::Cast(kind, operand, target_type) => {
553 let op_ty = operand.ty(self.body, self.tcx);
558 "enum -> int casts should go through `Rvalue::Discriminant`: {operand:?}:{op_ty} as {target_type}",
563 // Nothing to check here
564 CastKind::PointerFromExposedAddress
565 | CastKind::PointerExposeAddress
566 | CastKind::Pointer(_) => {}
570 | Rvalue::ThreadLocalRef(_)
571 | Rvalue::AddressOf(_, _)
572 | Rvalue::NullaryOp(_, _)
573 | Rvalue::Discriminant(_) => {}
575 self.super_rvalue(rvalue, location);
578 fn visit_statement(&mut self, statement: &Statement<'tcx>, location: Location) {
579 match &statement.kind {
580 StatementKind::Assign(box (dest, rvalue)) => {
581 // LHS and RHS of the assignment must have the same type.
582 let left_ty = dest.ty(&self.body.local_decls, self.tcx).ty;
583 let right_ty = rvalue.ty(&self.body.local_decls, self.tcx);
584 if !self.mir_assign_valid_types(right_ty, left_ty) {
588 "encountered `{:?}` with incompatible types:\n\
589 left-hand side has type: {}\n\
590 right-hand side has type: {}",
591 statement.kind, left_ty, right_ty,
595 if let Rvalue::CopyForDeref(place) = rvalue {
596 if !place.ty(&self.body.local_decls, self.tcx).ty.builtin_deref(true).is_some()
600 "`CopyForDeref` should only be used for dereferenceable types",
604 // FIXME(JakobDegen): Check this for all rvalues, not just this one.
605 if let Rvalue::Use(Operand::Copy(src) | Operand::Move(src)) = rvalue {
606 // The sides of an assignment must not alias. Currently this just checks whether
607 // the places are identical.
611 "encountered `Assign` statement with overlapping memory",
616 StatementKind::AscribeUserType(..) => {
617 if self.mir_phase >= MirPhase::DropsLowered {
620 "`AscribeUserType` should have been removed after drop lowering phase",
624 StatementKind::FakeRead(..) => {
625 if self.mir_phase >= MirPhase::DropsLowered {
628 "`FakeRead` should have been removed after drop lowering phase",
632 StatementKind::CopyNonOverlapping(box rustc_middle::mir::CopyNonOverlapping {
637 let src_ty = src.ty(&self.body.local_decls, self.tcx);
638 let op_src_ty = if let Some(src_deref) = src_ty.builtin_deref(true) {
643 format!("Expected src to be ptr in copy_nonoverlapping, got: {}", src_ty),
647 let dst_ty = dst.ty(&self.body.local_decls, self.tcx);
648 let op_dst_ty = if let Some(dst_deref) = dst_ty.builtin_deref(true) {
653 format!("Expected dst to be ptr in copy_nonoverlapping, got: {}", dst_ty),
657 // since CopyNonOverlapping is parametrized by 1 type,
658 // we only need to check that they are equal and not keep an extra parameter.
659 if !self.mir_assign_valid_types(op_src_ty, op_dst_ty) {
660 self.fail(location, format!("bad arg ({:?} != {:?})", op_src_ty, op_dst_ty));
663 let op_cnt_ty = count.ty(&self.body.local_decls, self.tcx);
664 if op_cnt_ty != self.tcx.types.usize {
665 self.fail(location, format!("bad arg ({:?} != usize)", op_cnt_ty))
668 StatementKind::SetDiscriminant { place, .. } => {
669 if self.mir_phase < MirPhase::Deaggregated {
670 self.fail(location, "`SetDiscriminant`is not allowed until deaggregation");
672 let pty = place.ty(&self.body.local_decls, self.tcx).ty.kind();
673 if !matches!(pty, ty::Adt(..) | ty::Generator(..) | ty::Opaque(..)) {
677 "`SetDiscriminant` is only allowed on ADTs and generators, not {:?}",
683 StatementKind::Deinit(..) => {
684 if self.mir_phase < MirPhase::Deaggregated {
685 self.fail(location, "`Deinit`is not allowed until deaggregation");
688 StatementKind::Retag(_, _) => {
689 // FIXME(JakobDegen) The validator should check that `self.mir_phase <
690 // DropsLowered`. However, this causes ICEs with generation of drop shims, which
691 // seem to fail to set their `MirPhase` correctly.
693 StatementKind::StorageLive(..)
694 | StatementKind::StorageDead(..)
695 | StatementKind::Coverage(_)
696 | StatementKind::Nop => {}
699 self.super_statement(statement, location);
702 fn visit_terminator(&mut self, terminator: &Terminator<'tcx>, location: Location) {
703 match &terminator.kind {
704 TerminatorKind::Goto { target } => {
705 self.check_edge(location, *target, EdgeKind::Normal);
707 TerminatorKind::SwitchInt { targets, switch_ty, discr } => {
708 let ty = discr.ty(&self.body.local_decls, self.tcx);
709 if ty != *switch_ty {
713 "encountered `SwitchInt` terminator with type mismatch: {:?} != {:?}",
719 let target_width = self.tcx.sess.target.pointer_width;
721 let size = Size::from_bits(match switch_ty.kind() {
722 ty::Uint(uint) => uint.normalize(target_width).bit_width().unwrap(),
723 ty::Int(int) => int.normalize(target_width).bit_width().unwrap(),
726 other => bug!("unhandled type: {:?}", other),
729 for (value, target) in targets.iter() {
730 if Scalar::<()>::try_from_uint(value, size).is_none() {
733 format!("the value {:#x} is not a proper {:?}", value, switch_ty),
737 self.check_edge(location, target, EdgeKind::Normal);
739 self.check_edge(location, targets.otherwise(), EdgeKind::Normal);
741 self.value_cache.clear();
742 self.value_cache.extend(targets.iter().map(|(value, _)| value));
743 let all_len = self.value_cache.len();
744 self.value_cache.sort_unstable();
745 self.value_cache.dedup();
746 let has_duplicates = all_len != self.value_cache.len();
751 "duplicated values in `SwitchInt` terminator: {:?}",
757 TerminatorKind::Drop { target, unwind, .. } => {
758 self.check_edge(location, *target, EdgeKind::Normal);
759 if let Some(unwind) = unwind {
760 self.check_edge(location, *unwind, EdgeKind::Unwind);
763 TerminatorKind::DropAndReplace { target, unwind, .. } => {
764 if self.mir_phase >= MirPhase::DropsLowered {
767 "`DropAndReplace` should have been removed during drop elaboration",
770 self.check_edge(location, *target, EdgeKind::Normal);
771 if let Some(unwind) = unwind {
772 self.check_edge(location, *unwind, EdgeKind::Unwind);
775 TerminatorKind::Call { func, args, destination, target, cleanup, .. } => {
776 let func_ty = func.ty(&self.body.local_decls, self.tcx);
777 match func_ty.kind() {
778 ty::FnPtr(..) | ty::FnDef(..) => {}
781 format!("encountered non-callable type {} in `Call` terminator", func_ty),
784 if let Some(target) = target {
785 self.check_edge(location, *target, EdgeKind::Normal);
787 if let Some(cleanup) = cleanup {
788 self.check_edge(location, *cleanup, EdgeKind::Unwind);
791 // The call destination place and Operand::Move place used as an argument might be
792 // passed by a reference to the callee. Consequently they must be non-overlapping.
793 // Currently this simply checks for duplicate places.
794 self.place_cache.clear();
795 self.place_cache.push(destination.as_ref());
797 if let Operand::Move(place) = arg {
798 self.place_cache.push(place.as_ref());
801 let all_len = self.place_cache.len();
802 let mut dedup = FxHashSet::default();
803 self.place_cache.retain(|p| dedup.insert(*p));
804 let has_duplicates = all_len != self.place_cache.len();
809 "encountered overlapping memory in `Call` terminator: {:?}",
815 TerminatorKind::Assert { cond, target, cleanup, .. } => {
816 let cond_ty = cond.ty(&self.body.local_decls, self.tcx);
817 if cond_ty != self.tcx.types.bool {
821 "encountered non-boolean condition of type {} in `Assert` terminator",
826 self.check_edge(location, *target, EdgeKind::Normal);
827 if let Some(cleanup) = cleanup {
828 self.check_edge(location, *cleanup, EdgeKind::Unwind);
831 TerminatorKind::Yield { resume, drop, .. } => {
832 if self.body.generator.is_none() {
833 self.fail(location, "`Yield` cannot appear outside generator bodies");
835 if self.mir_phase >= MirPhase::GeneratorsLowered {
836 self.fail(location, "`Yield` should have been replaced by generator lowering");
838 self.check_edge(location, *resume, EdgeKind::Normal);
839 if let Some(drop) = drop {
840 self.check_edge(location, *drop, EdgeKind::Normal);
843 TerminatorKind::FalseEdge { real_target, imaginary_target } => {
844 if self.mir_phase >= MirPhase::DropsLowered {
847 "`FalseEdge` should have been removed after drop elaboration",
850 self.check_edge(location, *real_target, EdgeKind::Normal);
851 self.check_edge(location, *imaginary_target, EdgeKind::Normal);
853 TerminatorKind::FalseUnwind { real_target, unwind } => {
854 if self.mir_phase >= MirPhase::DropsLowered {
857 "`FalseUnwind` should have been removed after drop elaboration",
860 self.check_edge(location, *real_target, EdgeKind::Normal);
861 if let Some(unwind) = unwind {
862 self.check_edge(location, *unwind, EdgeKind::Unwind);
865 TerminatorKind::InlineAsm { destination, cleanup, .. } => {
866 if let Some(destination) = destination {
867 self.check_edge(location, *destination, EdgeKind::Normal);
869 if let Some(cleanup) = cleanup {
870 self.check_edge(location, *cleanup, EdgeKind::Unwind);
873 TerminatorKind::GeneratorDrop => {
874 if self.body.generator.is_none() {
875 self.fail(location, "`GeneratorDrop` cannot appear outside generator bodies");
877 if self.mir_phase >= MirPhase::GeneratorsLowered {
880 "`GeneratorDrop` should have been replaced by generator lowering",
884 TerminatorKind::Resume | TerminatorKind::Abort => {
885 let bb = location.block;
886 if !self.body.basic_blocks()[bb].is_cleanup {
887 self.fail(location, "Cannot `Resume` or `Abort` from non-cleanup basic block")
890 TerminatorKind::Return => {
891 let bb = location.block;
892 if self.body.basic_blocks()[bb].is_cleanup {
893 self.fail(location, "Cannot `Return` from cleanup basic block")
896 TerminatorKind::Unreachable => {}
899 self.super_terminator(terminator, location);
902 fn visit_source_scope(&mut self, scope: SourceScope) {
903 if self.body.source_scopes.get(scope).is_none() {
904 self.tcx.sess.diagnostic().delay_span_bug(
907 "broken MIR in {:?} ({}):\ninvalid source scope {:?}",
908 self.body.source.instance, self.when, scope,