]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_const_eval/src/transform/validate.rs
69e457f0a1a86db132544894e35b09fdf53de430
[rust.git] / compiler / rustc_const_eval / src / transform / validate.rs
1 //! Validates the MIR to ensure that invariants are upheld.
2
3 use rustc_data_structures::fx::FxHashSet;
4 use rustc_index::bit_set::BitSet;
5 use rustc_infer::infer::{DefiningAnchor, TyCtxtInferExt};
6 use rustc_infer::traits::ObligationCause;
7 use rustc_middle::mir::interpret::Scalar;
8 use rustc_middle::mir::visit::NonUseContext::VarDebugInfo;
9 use rustc_middle::mir::visit::{PlaceContext, Visitor};
10 use rustc_middle::mir::{
11     traversal, AggregateKind, BasicBlock, BinOp, Body, BorrowKind, CastKind, CopyNonOverlapping,
12     Local, Location, MirPass, MirPhase, NonDivergingIntrinsic, Operand, Place, PlaceElem, PlaceRef,
13     ProjectionElem, RuntimePhase, Rvalue, SourceScope, Statement, StatementKind, Terminator,
14     TerminatorKind, UnOp, START_BLOCK,
15 };
16 use rustc_middle::ty::{self, InstanceDef, ParamEnv, Ty, TyCtxt, 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};
21 use rustc_trait_selection::traits::ObligationCtxt;
22
23 #[derive(Copy, Clone, Debug)]
24 enum EdgeKind {
25     Unwind,
26     Normal,
27 }
28
29 pub struct Validator {
30     /// Describes at which point in the pipeline this validation is happening.
31     pub when: String,
32     /// The phase for which we are upholding the dialect. If the given phase forbids a specific
33     /// element, this validator will now emit errors if that specific element is encountered.
34     /// Note that phases that change the dialect cause all *following* phases to check the
35     /// invariants of the new dialect. A phase that changes dialects never checks the new invariants
36     /// itself.
37     pub mir_phase: MirPhase,
38 }
39
40 impl<'tcx> MirPass<'tcx> for Validator {
41     fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
42         // FIXME(JakobDegen): These bodies never instantiated in codegend anyway, so it's not
43         // terribly important that they pass the validator. However, I think other passes might
44         // still see them, in which case they might be surprised. It would probably be better if we
45         // didn't put this through the MIR pipeline at all.
46         if matches!(body.source.instance, InstanceDef::Intrinsic(..) | InstanceDef::Virtual(..)) {
47             return;
48         }
49         let def_id = body.source.def_id();
50         let param_env = tcx.param_env(def_id);
51         let mir_phase = self.mir_phase;
52
53         let always_live_locals = always_storage_live_locals(body);
54         let storage_liveness = MaybeStorageLive::new(always_live_locals)
55             .into_engine(tcx, body)
56             .iterate_to_fixpoint()
57             .into_results_cursor(body);
58
59         TypeChecker {
60             when: &self.when,
61             body,
62             tcx,
63             param_env,
64             mir_phase,
65             reachable_blocks: traversal::reachable_as_bitset(body),
66             storage_liveness,
67             place_cache: Vec::new(),
68             value_cache: Vec::new(),
69         }
70         .visit_body(body);
71     }
72 }
73
74 /// Returns whether the two types are equal up to subtyping.
75 ///
76 /// This is used in case we don't know the expected subtyping direction
77 /// and still want to check whether anything is broken.
78 pub fn is_equal_up_to_subtyping<'tcx>(
79     tcx: TyCtxt<'tcx>,
80     param_env: ParamEnv<'tcx>,
81     src: Ty<'tcx>,
82     dest: Ty<'tcx>,
83 ) -> bool {
84     // Fast path.
85     if src == dest {
86         return true;
87     }
88
89     // Check for subtyping in either direction.
90     is_subtype(tcx, param_env, src, dest) || is_subtype(tcx, param_env, dest, src)
91 }
92
93 pub fn is_subtype<'tcx>(
94     tcx: TyCtxt<'tcx>,
95     param_env: ParamEnv<'tcx>,
96     src: Ty<'tcx>,
97     dest: Ty<'tcx>,
98 ) -> bool {
99     if src == dest {
100         return true;
101     }
102
103     let mut builder =
104         tcx.infer_ctxt().ignoring_regions().with_opaque_type_inference(DefiningAnchor::Bubble);
105     let infcx = builder.build();
106     let ocx = ObligationCtxt::new(&infcx);
107     let cause = ObligationCause::dummy();
108     let src = ocx.normalize(cause.clone(), param_env, src);
109     let dest = ocx.normalize(cause.clone(), param_env, dest);
110     let Ok(infer_ok) = infcx.at(&cause, param_env).sub(src, dest) else {
111         return false;
112     };
113     let () = ocx.register_infer_ok_obligations(infer_ok);
114     let errors = ocx.select_all_or_error();
115     // With `Reveal::All`, opaque types get normalized away, with `Reveal::UserFacing`
116     // we would get unification errors because we're unable to look into opaque types,
117     // even if they're constrained in our current function.
118     //
119     // It seems very unlikely that this hides any bugs.
120     let _ = infcx.inner.borrow_mut().opaque_type_storage.take_opaque_types();
121     errors.is_empty()
122 }
123 struct TypeChecker<'a, 'tcx> {
124     when: &'a str,
125     body: &'a Body<'tcx>,
126     tcx: TyCtxt<'tcx>,
127     param_env: ParamEnv<'tcx>,
128     mir_phase: MirPhase,
129     reachable_blocks: BitSet<BasicBlock>,
130     storage_liveness: ResultsCursor<'a, 'tcx, MaybeStorageLive>,
131     place_cache: Vec<PlaceRef<'tcx>>,
132     value_cache: Vec<u128>,
133 }
134
135 impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
136     fn fail(&self, location: Location, msg: impl AsRef<str>) {
137         let span = self.body.source_info(location).span;
138         // We use `delay_span_bug` as we might see broken MIR when other errors have already
139         // occurred.
140         self.tcx.sess.diagnostic().delay_span_bug(
141             span,
142             &format!(
143                 "broken MIR in {:?} ({}) at {:?}:\n{}",
144                 self.body.source.instance,
145                 self.when,
146                 location,
147                 msg.as_ref()
148             ),
149         );
150     }
151
152     fn check_edge(&self, location: Location, bb: BasicBlock, edge_kind: EdgeKind) {
153         if bb == START_BLOCK {
154             self.fail(location, "start block must not have predecessors")
155         }
156         if let Some(bb) = self.body.basic_blocks.get(bb) {
157             let src = self.body.basic_blocks.get(location.block).unwrap();
158             match (src.is_cleanup, bb.is_cleanup, edge_kind) {
159                 // Non-cleanup blocks can jump to non-cleanup blocks along non-unwind edges
160                 (false, false, EdgeKind::Normal)
161                 // Non-cleanup blocks can jump to cleanup blocks along unwind edges
162                 | (false, true, EdgeKind::Unwind)
163                 // Cleanup blocks can jump to cleanup blocks along non-unwind edges
164                 | (true, true, EdgeKind::Normal) => {}
165                 // All other jumps are invalid
166                 _ => {
167                     self.fail(
168                         location,
169                         format!(
170                             "{:?} edge to {:?} violates unwind invariants (cleanup {:?} -> {:?})",
171                             edge_kind,
172                             bb,
173                             src.is_cleanup,
174                             bb.is_cleanup,
175                         )
176                     )
177                 }
178             }
179         } else {
180             self.fail(location, format!("encountered jump to invalid basic block {:?}", bb))
181         }
182     }
183
184     /// Check if src can be assigned into dest.
185     /// This is not precise, it will accept some incorrect assignments.
186     fn mir_assign_valid_types(&self, src: Ty<'tcx>, dest: Ty<'tcx>) -> bool {
187         // Fast path before we normalize.
188         if src == dest {
189             // Equal types, all is good.
190             return true;
191         }
192         // Normalization reveals opaque types, but we may be validating MIR while computing
193         // said opaque types, causing cycles.
194         if (src, dest).has_opaque_types() {
195             return true;
196         }
197
198         is_subtype(self.tcx, self.param_env, src, dest)
199     }
200 }
201
202 impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
203     fn visit_local(&mut self, local: Local, context: PlaceContext, location: Location) {
204         if self.body.local_decls.get(local).is_none() {
205             self.fail(
206                 location,
207                 format!("local {:?} has no corresponding declaration in `body.local_decls`", local),
208             );
209         }
210
211         if self.reachable_blocks.contains(location.block) && context.is_use() {
212             // We check that the local is live whenever it is used. Technically, violating this
213             // restriction is only UB and not actually indicative of not well-formed MIR. This means
214             // that an optimization which turns MIR that already has UB into MIR that fails this
215             // check is not necessarily wrong. However, we have no such optimizations at the moment,
216             // and so we include this check anyway to help us catch bugs. If you happen to write an
217             // optimization that might cause this to incorrectly fire, feel free to remove this
218             // check.
219             self.storage_liveness.seek_after_primary_effect(location);
220             let locals_with_storage = self.storage_liveness.get();
221             if !locals_with_storage.contains(local) {
222                 self.fail(location, format!("use of local {:?}, which has no storage here", local));
223             }
224         }
225     }
226
227     fn visit_operand(&mut self, operand: &Operand<'tcx>, location: Location) {
228         // This check is somewhat expensive, so only run it when -Zvalidate-mir is passed.
229         if self.tcx.sess.opts.unstable_opts.validate_mir
230             && self.mir_phase < MirPhase::Runtime(RuntimePhase::Initial)
231         {
232             // `Operand::Copy` is only supposed to be used with `Copy` types.
233             if let Operand::Copy(place) = operand {
234                 let ty = place.ty(&self.body.local_decls, self.tcx).ty;
235
236                 if !ty.is_copy_modulo_regions(self.tcx, self.param_env) {
237                     self.fail(location, format!("`Operand::Copy` with non-`Copy` type {}", ty));
238                 }
239             }
240         }
241
242         self.super_operand(operand, location);
243     }
244
245     fn visit_projection_elem(
246         &mut self,
247         local: Local,
248         proj_base: &[PlaceElem<'tcx>],
249         elem: PlaceElem<'tcx>,
250         context: PlaceContext,
251         location: Location,
252     ) {
253         match elem {
254             ProjectionElem::Index(index) => {
255                 let index_ty = self.body.local_decls[index].ty;
256                 if index_ty != self.tcx.types.usize {
257                     self.fail(location, format!("bad index ({:?} != usize)", index_ty))
258                 }
259             }
260             ProjectionElem::Deref
261                 if self.mir_phase >= MirPhase::Runtime(RuntimePhase::PostCleanup) =>
262             {
263                 let base_ty = Place::ty_from(local, proj_base, &self.body.local_decls, self.tcx).ty;
264
265                 if base_ty.is_box() {
266                     self.fail(
267                         location,
268                         format!("{:?} dereferenced after ElaborateBoxDerefs", base_ty),
269                     )
270                 }
271             }
272             ProjectionElem::Field(f, ty) => {
273                 let parent = Place { local, projection: self.tcx.intern_place_elems(proj_base) };
274                 let parent_ty = parent.ty(&self.body.local_decls, self.tcx);
275                 let fail_out_of_bounds = |this: &Self, location| {
276                     this.fail(location, format!("Out of bounds field {:?} for {:?}", f, parent_ty));
277                 };
278                 let check_equal = |this: &Self, location, f_ty| {
279                     if !this.mir_assign_valid_types(ty, f_ty) {
280                         this.fail(
281                         location,
282                         format!(
283                             "Field projection `{:?}.{:?}` specified type `{:?}`, but actual type is `{:?}`",
284                             parent, f, ty, f_ty
285                         )
286                     )
287                     }
288                 };
289
290                 let kind = match parent_ty.ty.kind() {
291                     &ty::Opaque(def_id, substs) => {
292                         self.tcx.bound_type_of(def_id).subst(self.tcx, substs).kind()
293                     }
294                     kind => kind,
295                 };
296
297                 match kind {
298                     ty::Tuple(fields) => {
299                         let Some(f_ty) = fields.get(f.as_usize()) else {
300                             fail_out_of_bounds(self, location);
301                             return;
302                         };
303                         check_equal(self, location, *f_ty);
304                     }
305                     ty::Adt(adt_def, substs) => {
306                         let var = parent_ty.variant_index.unwrap_or(VariantIdx::from_u32(0));
307                         let Some(field) = adt_def.variant(var).fields.get(f.as_usize()) else {
308                             fail_out_of_bounds(self, location);
309                             return;
310                         };
311                         check_equal(self, location, field.ty(self.tcx, substs));
312                     }
313                     ty::Closure(_, substs) => {
314                         let substs = substs.as_closure();
315                         let Some(f_ty) = substs.upvar_tys().nth(f.as_usize()) else {
316                             fail_out_of_bounds(self, location);
317                             return;
318                         };
319                         check_equal(self, location, f_ty);
320                     }
321                     &ty::Generator(def_id, substs, _) => {
322                         let f_ty = if let Some(var) = parent_ty.variant_index {
323                             let gen_body = if def_id == self.body.source.def_id() {
324                                 self.body
325                             } else {
326                                 self.tcx.optimized_mir(def_id)
327                             };
328
329                             let Some(layout) = gen_body.generator_layout() else {
330                                 self.fail(location, format!("No generator layout for {:?}", parent_ty));
331                                 return;
332                             };
333
334                             let Some(&local) = layout.variant_fields[var].get(f) else {
335                                 fail_out_of_bounds(self, location);
336                                 return;
337                             };
338
339                             let Some(&f_ty) = layout.field_tys.get(local) else {
340                                 self.fail(location, format!("Out of bounds local {:?} for {:?}", local, parent_ty));
341                                 return;
342                             };
343
344                             f_ty
345                         } else {
346                             let Some(f_ty) = substs.as_generator().prefix_tys().nth(f.index()) else {
347                                 fail_out_of_bounds(self, location);
348                                 return;
349                             };
350
351                             f_ty
352                         };
353
354                         check_equal(self, location, f_ty);
355                     }
356                     _ => {
357                         self.fail(location, format!("{:?} does not have fields", parent_ty.ty));
358                     }
359                 }
360             }
361             _ => {}
362         }
363         self.super_projection_elem(local, proj_base, elem, context, location);
364     }
365
366     fn visit_place(&mut self, place: &Place<'tcx>, cntxt: PlaceContext, location: Location) {
367         // Set off any `bug!`s in the type computation code
368         let _ = place.ty(&self.body.local_decls, self.tcx);
369
370         if self.mir_phase >= MirPhase::Runtime(RuntimePhase::Initial)
371             && place.projection.len() > 1
372             && cntxt != PlaceContext::NonUse(VarDebugInfo)
373             && place.projection[1..].contains(&ProjectionElem::Deref)
374         {
375             self.fail(location, format!("{:?}, has deref at the wrong place", place));
376         }
377
378         self.super_place(place, cntxt, location);
379     }
380
381     fn visit_rvalue(&mut self, rvalue: &Rvalue<'tcx>, location: Location) {
382         macro_rules! check_kinds {
383             ($t:expr, $text:literal, $($patterns:tt)*) => {
384                 if !matches!(($t).kind(), $($patterns)*) {
385                     self.fail(location, format!($text, $t));
386                 }
387             };
388         }
389         match rvalue {
390             Rvalue::Use(_) | Rvalue::CopyForDeref(_) => {}
391             Rvalue::Aggregate(agg_kind, _) => {
392                 let disallowed = match **agg_kind {
393                     AggregateKind::Array(..) => false,
394                     _ => self.mir_phase >= MirPhase::Runtime(RuntimePhase::PostCleanup),
395                 };
396                 if disallowed {
397                     self.fail(
398                         location,
399                         format!("{:?} have been lowered to field assignments", rvalue),
400                     )
401                 }
402             }
403             Rvalue::Ref(_, BorrowKind::Shallow, _) => {
404                 if self.mir_phase >= MirPhase::Runtime(RuntimePhase::Initial) {
405                     self.fail(
406                         location,
407                         "`Assign` statement with a `Shallow` borrow should have been removed in runtime MIR",
408                     );
409                 }
410             }
411             Rvalue::Ref(..) => {}
412             Rvalue::Len(p) => {
413                 let pty = p.ty(&self.body.local_decls, self.tcx).ty;
414                 check_kinds!(
415                     pty,
416                     "Cannot compute length of non-array type {:?}",
417                     ty::Array(..) | ty::Slice(..)
418                 );
419             }
420             Rvalue::BinaryOp(op, vals) => {
421                 use BinOp::*;
422                 let a = vals.0.ty(&self.body.local_decls, self.tcx);
423                 let b = vals.1.ty(&self.body.local_decls, self.tcx);
424                 match op {
425                     Offset => {
426                         check_kinds!(a, "Cannot offset non-pointer type {:?}", ty::RawPtr(..));
427                         if b != self.tcx.types.isize && b != self.tcx.types.usize {
428                             self.fail(location, format!("Cannot offset by non-isize type {:?}", b));
429                         }
430                     }
431                     Eq | Lt | Le | Ne | Ge | Gt => {
432                         for x in [a, b] {
433                             check_kinds!(
434                                 x,
435                                 "Cannot compare type {:?}",
436                                 ty::Bool
437                                     | ty::Char
438                                     | ty::Int(..)
439                                     | ty::Uint(..)
440                                     | ty::Float(..)
441                                     | ty::RawPtr(..)
442                                     | ty::FnPtr(..)
443                             )
444                         }
445                         // The function pointer types can have lifetimes
446                         if !self.mir_assign_valid_types(a, b) {
447                             self.fail(
448                                 location,
449                                 format!("Cannot compare unequal types {:?} and {:?}", a, b),
450                             );
451                         }
452                     }
453                     Shl | Shr => {
454                         for x in [a, b] {
455                             check_kinds!(
456                                 x,
457                                 "Cannot shift non-integer type {:?}",
458                                 ty::Uint(..) | ty::Int(..)
459                             )
460                         }
461                     }
462                     BitAnd | BitOr | BitXor => {
463                         for x in [a, b] {
464                             check_kinds!(
465                                 x,
466                                 "Cannot perform bitwise op on type {:?}",
467                                 ty::Uint(..) | ty::Int(..) | ty::Bool
468                             )
469                         }
470                         if a != b {
471                             self.fail(
472                                 location,
473                                 format!(
474                                     "Cannot perform bitwise op on unequal types {:?} and {:?}",
475                                     a, b
476                                 ),
477                             );
478                         }
479                     }
480                     Add | Sub | Mul | Div | Rem => {
481                         for x in [a, b] {
482                             check_kinds!(
483                                 x,
484                                 "Cannot perform arithmetic on type {:?}",
485                                 ty::Uint(..) | ty::Int(..) | ty::Float(..)
486                             )
487                         }
488                         if a != b {
489                             self.fail(
490                                 location,
491                                 format!(
492                                     "Cannot perform arithmetic on unequal types {:?} and {:?}",
493                                     a, b
494                                 ),
495                             );
496                         }
497                     }
498                 }
499             }
500             Rvalue::CheckedBinaryOp(op, vals) => {
501                 use BinOp::*;
502                 let a = vals.0.ty(&self.body.local_decls, self.tcx);
503                 let b = vals.1.ty(&self.body.local_decls, self.tcx);
504                 match op {
505                     Add | Sub | Mul => {
506                         for x in [a, b] {
507                             check_kinds!(
508                                 x,
509                                 "Cannot perform checked arithmetic on type {:?}",
510                                 ty::Uint(..) | ty::Int(..)
511                             )
512                         }
513                         if a != b {
514                             self.fail(
515                                 location,
516                                 format!(
517                                     "Cannot perform checked arithmetic on unequal types {:?} and {:?}",
518                                     a, b
519                                 ),
520                             );
521                         }
522                     }
523                     Shl | Shr => {
524                         for x in [a, b] {
525                             check_kinds!(
526                                 x,
527                                 "Cannot perform checked shift on non-integer type {:?}",
528                                 ty::Uint(..) | ty::Int(..)
529                             )
530                         }
531                     }
532                     _ => self.fail(location, format!("There is no checked version of {:?}", op)),
533                 }
534             }
535             Rvalue::UnaryOp(op, operand) => {
536                 let a = operand.ty(&self.body.local_decls, self.tcx);
537                 match op {
538                     UnOp::Neg => {
539                         check_kinds!(a, "Cannot negate type {:?}", ty::Int(..) | ty::Float(..))
540                     }
541                     UnOp::Not => {
542                         check_kinds!(
543                             a,
544                             "Cannot binary not type {:?}",
545                             ty::Int(..) | ty::Uint(..) | ty::Bool
546                         );
547                     }
548                 }
549             }
550             Rvalue::ShallowInitBox(operand, _) => {
551                 let a = operand.ty(&self.body.local_decls, self.tcx);
552                 check_kinds!(a, "Cannot shallow init type {:?}", ty::RawPtr(..));
553             }
554             Rvalue::Cast(kind, operand, target_type) => {
555                 let op_ty = operand.ty(self.body, self.tcx);
556                 match kind {
557                     CastKind::DynStar => {
558                         // FIXME(dyn-star): make sure nothing needs to be done here.
559                     }
560                     // FIXME: Add Checks for these
561                     CastKind::PointerFromExposedAddress
562                     | CastKind::PointerExposeAddress
563                     | CastKind::Pointer(_) => {}
564                     CastKind::IntToInt | CastKind::IntToFloat => {
565                         let input_valid = op_ty.is_integral() || op_ty.is_char() || op_ty.is_bool();
566                         let target_valid = target_type.is_numeric() || target_type.is_char();
567                         if !input_valid || !target_valid {
568                             self.fail(
569                                 location,
570                                 format!("Wrong cast kind {kind:?} for the type {op_ty}",),
571                             );
572                         }
573                     }
574                     CastKind::FnPtrToPtr | CastKind::PtrToPtr => {
575                         if !(op_ty.is_any_ptr() && target_type.is_unsafe_ptr()) {
576                             self.fail(location, "Can't cast {op_ty} into 'Ptr'");
577                         }
578                     }
579                     CastKind::FloatToFloat | CastKind::FloatToInt => {
580                         if !op_ty.is_floating_point() || !target_type.is_numeric() {
581                             self.fail(
582                                 location,
583                                 format!(
584                                     "Trying to cast non 'Float' as {kind:?} into {target_type:?}"
585                                 ),
586                             );
587                         }
588                     }
589                 }
590             }
591             Rvalue::Repeat(_, _)
592             | Rvalue::ThreadLocalRef(_)
593             | Rvalue::AddressOf(_, _)
594             | Rvalue::NullaryOp(_, _)
595             | Rvalue::Discriminant(_) => {}
596         }
597         self.super_rvalue(rvalue, location);
598     }
599
600     fn visit_statement(&mut self, statement: &Statement<'tcx>, location: Location) {
601         match &statement.kind {
602             StatementKind::Assign(box (dest, rvalue)) => {
603                 // LHS and RHS of the assignment must have the same type.
604                 let left_ty = dest.ty(&self.body.local_decls, self.tcx).ty;
605                 let right_ty = rvalue.ty(&self.body.local_decls, self.tcx);
606                 if !self.mir_assign_valid_types(right_ty, left_ty) {
607                     self.fail(
608                         location,
609                         format!(
610                             "encountered `{:?}` with incompatible types:\n\
611                             left-hand side has type: {}\n\
612                             right-hand side has type: {}",
613                             statement.kind, left_ty, right_ty,
614                         ),
615                     );
616                 }
617                 if let Rvalue::CopyForDeref(place) = rvalue {
618                     if !place.ty(&self.body.local_decls, self.tcx).ty.builtin_deref(true).is_some()
619                     {
620                         self.fail(
621                             location,
622                             "`CopyForDeref` should only be used for dereferenceable types",
623                         )
624                     }
625                 }
626                 // FIXME(JakobDegen): Check this for all rvalues, not just this one.
627                 if let Rvalue::Use(Operand::Copy(src) | Operand::Move(src)) = rvalue {
628                     // The sides of an assignment must not alias. Currently this just checks whether
629                     // the places are identical.
630                     if dest == src {
631                         self.fail(
632                             location,
633                             "encountered `Assign` statement with overlapping memory",
634                         );
635                     }
636                 }
637             }
638             StatementKind::AscribeUserType(..) => {
639                 if self.mir_phase >= MirPhase::Runtime(RuntimePhase::Initial) {
640                     self.fail(
641                         location,
642                         "`AscribeUserType` should have been removed after drop lowering phase",
643                     );
644                 }
645             }
646             StatementKind::FakeRead(..) => {
647                 if self.mir_phase >= MirPhase::Runtime(RuntimePhase::Initial) {
648                     self.fail(
649                         location,
650                         "`FakeRead` should have been removed after drop lowering phase",
651                     );
652                 }
653             }
654             StatementKind::Intrinsic(box NonDivergingIntrinsic::Assume(op)) => {
655                 let ty = op.ty(&self.body.local_decls, self.tcx);
656                 if !ty.is_bool() {
657                     self.fail(
658                         location,
659                         format!("`assume` argument must be `bool`, but got: `{}`", ty),
660                     );
661                 }
662             }
663             StatementKind::Intrinsic(box NonDivergingIntrinsic::CopyNonOverlapping(
664                 CopyNonOverlapping { src, dst, count },
665             )) => {
666                 let src_ty = src.ty(&self.body.local_decls, self.tcx);
667                 let op_src_ty = if let Some(src_deref) = src_ty.builtin_deref(true) {
668                     src_deref.ty
669                 } else {
670                     self.fail(
671                         location,
672                         format!("Expected src to be ptr in copy_nonoverlapping, got: {}", src_ty),
673                     );
674                     return;
675                 };
676                 let dst_ty = dst.ty(&self.body.local_decls, self.tcx);
677                 let op_dst_ty = if let Some(dst_deref) = dst_ty.builtin_deref(true) {
678                     dst_deref.ty
679                 } else {
680                     self.fail(
681                         location,
682                         format!("Expected dst to be ptr in copy_nonoverlapping, got: {}", dst_ty),
683                     );
684                     return;
685                 };
686                 // since CopyNonOverlapping is parametrized by 1 type,
687                 // we only need to check that they are equal and not keep an extra parameter.
688                 if !self.mir_assign_valid_types(op_src_ty, op_dst_ty) {
689                     self.fail(location, format!("bad arg ({:?} != {:?})", op_src_ty, op_dst_ty));
690                 }
691
692                 let op_cnt_ty = count.ty(&self.body.local_decls, self.tcx);
693                 if op_cnt_ty != self.tcx.types.usize {
694                     self.fail(location, format!("bad arg ({:?} != usize)", op_cnt_ty))
695                 }
696             }
697             StatementKind::SetDiscriminant { place, .. } => {
698                 if self.mir_phase < MirPhase::Runtime(RuntimePhase::Initial) {
699                     self.fail(location, "`SetDiscriminant`is not allowed until deaggregation");
700                 }
701                 let pty = place.ty(&self.body.local_decls, self.tcx).ty.kind();
702                 if !matches!(pty, ty::Adt(..) | ty::Generator(..) | ty::Opaque(..)) {
703                     self.fail(
704                         location,
705                         format!(
706                             "`SetDiscriminant` is only allowed on ADTs and generators, not {:?}",
707                             pty
708                         ),
709                     );
710                 }
711             }
712             StatementKind::Deinit(..) => {
713                 if self.mir_phase < MirPhase::Runtime(RuntimePhase::Initial) {
714                     self.fail(location, "`Deinit`is not allowed until deaggregation");
715                 }
716             }
717             StatementKind::Retag(_, _) => {
718                 // FIXME(JakobDegen) The validator should check that `self.mir_phase <
719                 // DropsLowered`. However, this causes ICEs with generation of drop shims, which
720                 // seem to fail to set their `MirPhase` correctly.
721             }
722             StatementKind::StorageLive(..)
723             | StatementKind::StorageDead(..)
724             | StatementKind::Coverage(_)
725             | StatementKind::Nop => {}
726         }
727
728         self.super_statement(statement, location);
729     }
730
731     fn visit_terminator(&mut self, terminator: &Terminator<'tcx>, location: Location) {
732         match &terminator.kind {
733             TerminatorKind::Goto { target } => {
734                 self.check_edge(location, *target, EdgeKind::Normal);
735             }
736             TerminatorKind::SwitchInt { targets, switch_ty, discr } => {
737                 let ty = discr.ty(&self.body.local_decls, self.tcx);
738                 if ty != *switch_ty {
739                     self.fail(
740                         location,
741                         format!(
742                             "encountered `SwitchInt` terminator with type mismatch: {:?} != {:?}",
743                             ty, switch_ty,
744                         ),
745                     );
746                 }
747
748                 let target_width = self.tcx.sess.target.pointer_width;
749
750                 let size = Size::from_bits(match switch_ty.kind() {
751                     ty::Uint(uint) => uint.normalize(target_width).bit_width().unwrap(),
752                     ty::Int(int) => int.normalize(target_width).bit_width().unwrap(),
753                     ty::Char => 32,
754                     ty::Bool => 1,
755                     other => bug!("unhandled type: {:?}", other),
756                 });
757
758                 for (value, target) in targets.iter() {
759                     if Scalar::<()>::try_from_uint(value, size).is_none() {
760                         self.fail(
761                             location,
762                             format!("the value {:#x} is not a proper {:?}", value, switch_ty),
763                         )
764                     }
765
766                     self.check_edge(location, target, EdgeKind::Normal);
767                 }
768                 self.check_edge(location, targets.otherwise(), EdgeKind::Normal);
769
770                 self.value_cache.clear();
771                 self.value_cache.extend(targets.iter().map(|(value, _)| value));
772                 let all_len = self.value_cache.len();
773                 self.value_cache.sort_unstable();
774                 self.value_cache.dedup();
775                 let has_duplicates = all_len != self.value_cache.len();
776                 if has_duplicates {
777                     self.fail(
778                         location,
779                         format!(
780                             "duplicated values in `SwitchInt` terminator: {:?}",
781                             terminator.kind,
782                         ),
783                     );
784                 }
785             }
786             TerminatorKind::Drop { target, unwind, .. } => {
787                 self.check_edge(location, *target, EdgeKind::Normal);
788                 if let Some(unwind) = unwind {
789                     self.check_edge(location, *unwind, EdgeKind::Unwind);
790                 }
791             }
792             TerminatorKind::DropAndReplace { target, unwind, .. } => {
793                 if self.mir_phase >= MirPhase::Runtime(RuntimePhase::Initial) {
794                     self.fail(
795                         location,
796                         "`DropAndReplace` should have been removed during drop elaboration",
797                     );
798                 }
799                 self.check_edge(location, *target, EdgeKind::Normal);
800                 if let Some(unwind) = unwind {
801                     self.check_edge(location, *unwind, EdgeKind::Unwind);
802                 }
803             }
804             TerminatorKind::Call { func, args, destination, target, cleanup, .. } => {
805                 let func_ty = func.ty(&self.body.local_decls, self.tcx);
806                 match func_ty.kind() {
807                     ty::FnPtr(..) | ty::FnDef(..) => {}
808                     _ => self.fail(
809                         location,
810                         format!("encountered non-callable type {} in `Call` terminator", func_ty),
811                     ),
812                 }
813                 if let Some(target) = target {
814                     self.check_edge(location, *target, EdgeKind::Normal);
815                 }
816                 if let Some(cleanup) = cleanup {
817                     self.check_edge(location, *cleanup, EdgeKind::Unwind);
818                 }
819
820                 // The call destination place and Operand::Move place used as an argument might be
821                 // passed by a reference to the callee. Consequently they must be non-overlapping.
822                 // Currently this simply checks for duplicate places.
823                 self.place_cache.clear();
824                 self.place_cache.push(destination.as_ref());
825                 for arg in args {
826                     if let Operand::Move(place) = arg {
827                         self.place_cache.push(place.as_ref());
828                     }
829                 }
830                 let all_len = self.place_cache.len();
831                 let mut dedup = FxHashSet::default();
832                 self.place_cache.retain(|p| dedup.insert(*p));
833                 let has_duplicates = all_len != self.place_cache.len();
834                 if has_duplicates {
835                     self.fail(
836                         location,
837                         format!(
838                             "encountered overlapping memory in `Call` terminator: {:?}",
839                             terminator.kind,
840                         ),
841                     );
842                 }
843             }
844             TerminatorKind::Assert { cond, target, cleanup, .. } => {
845                 let cond_ty = cond.ty(&self.body.local_decls, self.tcx);
846                 if cond_ty != self.tcx.types.bool {
847                     self.fail(
848                         location,
849                         format!(
850                             "encountered non-boolean condition of type {} in `Assert` terminator",
851                             cond_ty
852                         ),
853                     );
854                 }
855                 self.check_edge(location, *target, EdgeKind::Normal);
856                 if let Some(cleanup) = cleanup {
857                     self.check_edge(location, *cleanup, EdgeKind::Unwind);
858                 }
859             }
860             TerminatorKind::Yield { resume, drop, .. } => {
861                 if self.body.generator.is_none() {
862                     self.fail(location, "`Yield` cannot appear outside generator bodies");
863                 }
864                 if self.mir_phase >= MirPhase::Runtime(RuntimePhase::Initial) {
865                     self.fail(location, "`Yield` should have been replaced by generator lowering");
866                 }
867                 self.check_edge(location, *resume, EdgeKind::Normal);
868                 if let Some(drop) = drop {
869                     self.check_edge(location, *drop, EdgeKind::Normal);
870                 }
871             }
872             TerminatorKind::FalseEdge { real_target, imaginary_target } => {
873                 if self.mir_phase >= MirPhase::Runtime(RuntimePhase::Initial) {
874                     self.fail(
875                         location,
876                         "`FalseEdge` should have been removed after drop elaboration",
877                     );
878                 }
879                 self.check_edge(location, *real_target, EdgeKind::Normal);
880                 self.check_edge(location, *imaginary_target, EdgeKind::Normal);
881             }
882             TerminatorKind::FalseUnwind { real_target, unwind } => {
883                 if self.mir_phase >= MirPhase::Runtime(RuntimePhase::Initial) {
884                     self.fail(
885                         location,
886                         "`FalseUnwind` should have been removed after drop elaboration",
887                     );
888                 }
889                 self.check_edge(location, *real_target, EdgeKind::Normal);
890                 if let Some(unwind) = unwind {
891                     self.check_edge(location, *unwind, EdgeKind::Unwind);
892                 }
893             }
894             TerminatorKind::InlineAsm { destination, cleanup, .. } => {
895                 if let Some(destination) = destination {
896                     self.check_edge(location, *destination, EdgeKind::Normal);
897                 }
898                 if let Some(cleanup) = cleanup {
899                     self.check_edge(location, *cleanup, EdgeKind::Unwind);
900                 }
901             }
902             TerminatorKind::GeneratorDrop => {
903                 if self.body.generator.is_none() {
904                     self.fail(location, "`GeneratorDrop` cannot appear outside generator bodies");
905                 }
906                 if self.mir_phase >= MirPhase::Runtime(RuntimePhase::Initial) {
907                     self.fail(
908                         location,
909                         "`GeneratorDrop` should have been replaced by generator lowering",
910                     );
911                 }
912             }
913             TerminatorKind::Resume | TerminatorKind::Abort => {
914                 let bb = location.block;
915                 if !self.body.basic_blocks[bb].is_cleanup {
916                     self.fail(location, "Cannot `Resume` or `Abort` from non-cleanup basic block")
917                 }
918             }
919             TerminatorKind::Return => {
920                 let bb = location.block;
921                 if self.body.basic_blocks[bb].is_cleanup {
922                     self.fail(location, "Cannot `Return` from cleanup basic block")
923                 }
924             }
925             TerminatorKind::Unreachable => {}
926         }
927
928         self.super_terminator(terminator, location);
929     }
930
931     fn visit_source_scope(&mut self, scope: SourceScope) {
932         if self.body.source_scopes.get(scope).is_none() {
933             self.tcx.sess.diagnostic().delay_span_bug(
934                 self.body.span,
935                 &format!(
936                     "broken MIR in {:?} ({}):\ninvalid source scope {:?}",
937                     self.body.source.instance, self.when, scope,
938                 ),
939             );
940         }
941     }
942 }