]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_const_eval/src/transform/validate.rs
Rollup merge of #105975 - jeremystucki:rustc-remove-needless-lifetimes, r=eholk
[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::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,
14 };
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};
20
21 #[derive(Copy, Clone, Debug)]
22 enum EdgeKind {
23     Unwind,
24     Normal,
25 }
26
27 pub struct Validator {
28     /// Describes at which point in the pipeline this validation is happening.
29     pub when: String,
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
34     /// itself.
35     pub mir_phase: MirPhase,
36 }
37
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(..)) {
45             return;
46         }
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),
52         };
53
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);
59
60         TypeChecker {
61             when: &self.when,
62             body,
63             tcx,
64             param_env,
65             mir_phase,
66             reachable_blocks: traversal::reachable_as_bitset(body),
67             storage_liveness,
68             place_cache: Vec::new(),
69             value_cache: Vec::new(),
70         }
71         .visit_body(body);
72     }
73 }
74
75 struct TypeChecker<'a, 'tcx> {
76     when: &'a str,
77     body: &'a Body<'tcx>,
78     tcx: TyCtxt<'tcx>,
79     param_env: ParamEnv<'tcx>,
80     mir_phase: MirPhase,
81     reachable_blocks: BitSet<BasicBlock>,
82     storage_liveness: ResultsCursor<'a, 'tcx, MaybeStorageLive<'static>>,
83     place_cache: Vec<PlaceRef<'tcx>>,
84     value_cache: Vec<u128>,
85 }
86
87 impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
88     #[track_caller]
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
92         // occurred.
93         self.tcx.sess.diagnostic().delay_span_bug(
94             span,
95             &format!(
96                 "broken MIR in {:?} ({}) at {:?}:\n{}",
97                 self.body.source.instance,
98                 self.when,
99                 location,
100                 msg.as_ref()
101             ),
102         );
103     }
104
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")
108         }
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
119                 _ => {
120                     self.fail(
121                         location,
122                         format!(
123                             "{:?} edge to {:?} violates unwind invariants (cleanup {:?} -> {:?})",
124                             edge_kind,
125                             bb,
126                             src.is_cleanup,
127                             bb.is_cleanup,
128                         )
129                     )
130                 }
131             }
132         } else {
133             self.fail(location, format!("encountered jump to invalid basic block {:?}", bb))
134         }
135     }
136
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.
141         if src == dest {
142             // Equal types, all is good.
143             return true;
144         }
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() {
148             return true;
149         }
150
151         crate::util::is_subtype(self.tcx, self.param_env, src, dest)
152     }
153 }
154
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() {
158             self.fail(
159                 location,
160                 format!("local {:?} has no corresponding declaration in `body.local_decls`", local),
161             );
162         }
163
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
171             // check.
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));
176             }
177         }
178     }
179
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)
184         {
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;
188
189                 if !ty.is_copy_modulo_regions(self.tcx, self.param_env) {
190                     self.fail(location, format!("`Operand::Copy` with non-`Copy` type {}", ty));
191                 }
192             }
193         }
194
195         self.super_operand(operand, location);
196     }
197
198     fn visit_projection_elem(
199         &mut self,
200         local: Local,
201         proj_base: &[PlaceElem<'tcx>],
202         elem: PlaceElem<'tcx>,
203         context: PlaceContext,
204         location: Location,
205     ) {
206         match elem {
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))
211                 }
212             }
213             ProjectionElem::Deref
214                 if self.mir_phase >= MirPhase::Runtime(RuntimePhase::PostCleanup) =>
215             {
216                 let base_ty = Place::ty_from(local, proj_base, &self.body.local_decls, self.tcx).ty;
217
218                 if base_ty.is_box() {
219                     self.fail(
220                         location,
221                         format!("{:?} dereferenced after ElaborateBoxDerefs", base_ty),
222                     )
223                 }
224             }
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));
230                 };
231                 let check_equal = |this: &Self, location, f_ty| {
232                     if !this.mir_assign_valid_types(ty, f_ty) {
233                         this.fail(
234                             location,
235                             format!(
236                                 "Field projection `{:?}.{:?}` specified type `{:?}`, but actual type is `{:?}`",
237                                 parent, f, ty, f_ty
238                             )
239                         )
240                     }
241                 };
242
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()
246                     }
247                     kind => kind,
248                 };
249
250                 match kind {
251                     ty::Tuple(fields) => {
252                         let Some(f_ty) = fields.get(f.as_usize()) else {
253                             fail_out_of_bounds(self, location);
254                             return;
255                         };
256                         check_equal(self, location, *f_ty);
257                     }
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);
262                             return;
263                         };
264                         check_equal(self, location, field.ty(self.tcx, substs));
265                     }
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);
270                             return;
271                         };
272                         check_equal(self, location, f_ty);
273                     }
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() {
277                                 self.body
278                             } else {
279                                 self.tcx.optimized_mir(def_id)
280                             };
281
282                             let Some(layout) = gen_body.generator_layout() else {
283                                 self.fail(location, format!("No generator layout for {:?}", parent_ty));
284                                 return;
285                             };
286
287                             let Some(&local) = layout.variant_fields[var].get(f) else {
288                                 fail_out_of_bounds(self, location);
289                                 return;
290                             };
291
292                             let Some(&f_ty) = layout.field_tys.get(local) else {
293                                 self.fail(location, format!("Out of bounds local {:?} for {:?}", local, parent_ty));
294                                 return;
295                             };
296
297                             f_ty
298                         } else {
299                             let Some(f_ty) = substs.as_generator().prefix_tys().nth(f.index()) else {
300                                 fail_out_of_bounds(self, location);
301                                 return;
302                             };
303
304                             f_ty
305                         };
306
307                         check_equal(self, location, f_ty);
308                     }
309                     _ => {
310                         self.fail(location, format!("{:?} does not have fields", parent_ty.ty));
311                     }
312                 }
313             }
314             _ => {}
315         }
316         self.super_projection_elem(local, proj_base, elem, context, location);
317     }
318
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);
322
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)
327         {
328             self.fail(location, format!("{:?}, has deref at the wrong place", place));
329         }
330
331         self.super_place(place, cntxt, location);
332     }
333
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));
339                 }
340             };
341         }
342         match rvalue {
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),
348                 };
349                 if disallowed {
350                     self.fail(
351                         location,
352                         format!("{:?} have been lowered to field assignments", rvalue),
353                     )
354                 }
355             }
356             Rvalue::Ref(_, BorrowKind::Shallow, _) => {
357                 if self.mir_phase >= MirPhase::Runtime(RuntimePhase::Initial) {
358                     self.fail(
359                         location,
360                         "`Assign` statement with a `Shallow` borrow should have been removed in runtime MIR",
361                     );
362                 }
363             }
364             Rvalue::Ref(..) => {}
365             Rvalue::Len(p) => {
366                 let pty = p.ty(&self.body.local_decls, self.tcx).ty;
367                 check_kinds!(
368                     pty,
369                     "Cannot compute length of non-array type {:?}",
370                     ty::Array(..) | ty::Slice(..)
371                 );
372             }
373             Rvalue::BinaryOp(op, vals) => {
374                 use BinOp::*;
375                 let a = vals.0.ty(&self.body.local_decls, self.tcx);
376                 let b = vals.1.ty(&self.body.local_decls, self.tcx);
377                 match op {
378                     Offset => {
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));
382                         }
383                     }
384                     Eq | Lt | Le | Ne | Ge | Gt => {
385                         for x in [a, b] {
386                             check_kinds!(
387                                 x,
388                                 "Cannot compare type {:?}",
389                                 ty::Bool
390                                     | ty::Char
391                                     | ty::Int(..)
392                                     | ty::Uint(..)
393                                     | ty::Float(..)
394                                     | ty::RawPtr(..)
395                                     | ty::FnPtr(..)
396                             )
397                         }
398                         // The function pointer types can have lifetimes
399                         if !self.mir_assign_valid_types(a, b) {
400                             self.fail(
401                                 location,
402                                 format!("Cannot compare unequal types {:?} and {:?}", a, b),
403                             );
404                         }
405                     }
406                     Shl | Shr => {
407                         for x in [a, b] {
408                             check_kinds!(
409                                 x,
410                                 "Cannot shift non-integer type {:?}",
411                                 ty::Uint(..) | ty::Int(..)
412                             )
413                         }
414                     }
415                     BitAnd | BitOr | BitXor => {
416                         for x in [a, b] {
417                             check_kinds!(
418                                 x,
419                                 "Cannot perform bitwise op on type {:?}",
420                                 ty::Uint(..) | ty::Int(..) | ty::Bool
421                             )
422                         }
423                         if a != b {
424                             self.fail(
425                                 location,
426                                 format!(
427                                     "Cannot perform bitwise op on unequal types {:?} and {:?}",
428                                     a, b
429                                 ),
430                             );
431                         }
432                     }
433                     Add | Sub | Mul | Div | Rem => {
434                         for x in [a, b] {
435                             check_kinds!(
436                                 x,
437                                 "Cannot perform arithmetic on type {:?}",
438                                 ty::Uint(..) | ty::Int(..) | ty::Float(..)
439                             )
440                         }
441                         if a != b {
442                             self.fail(
443                                 location,
444                                 format!(
445                                     "Cannot perform arithmetic on unequal types {:?} and {:?}",
446                                     a, b
447                                 ),
448                             );
449                         }
450                     }
451                 }
452             }
453             Rvalue::CheckedBinaryOp(op, vals) => {
454                 use BinOp::*;
455                 let a = vals.0.ty(&self.body.local_decls, self.tcx);
456                 let b = vals.1.ty(&self.body.local_decls, self.tcx);
457                 match op {
458                     Add | Sub | Mul => {
459                         for x in [a, b] {
460                             check_kinds!(
461                                 x,
462                                 "Cannot perform checked arithmetic on type {:?}",
463                                 ty::Uint(..) | ty::Int(..)
464                             )
465                         }
466                         if a != b {
467                             self.fail(
468                                 location,
469                                 format!(
470                                     "Cannot perform checked arithmetic on unequal types {:?} and {:?}",
471                                     a, b
472                                 ),
473                             );
474                         }
475                     }
476                     Shl | Shr => {
477                         for x in [a, b] {
478                             check_kinds!(
479                                 x,
480                                 "Cannot perform checked shift on non-integer type {:?}",
481                                 ty::Uint(..) | ty::Int(..)
482                             )
483                         }
484                     }
485                     _ => self.fail(location, format!("There is no checked version of {:?}", op)),
486                 }
487             }
488             Rvalue::UnaryOp(op, operand) => {
489                 let a = operand.ty(&self.body.local_decls, self.tcx);
490                 match op {
491                     UnOp::Neg => {
492                         check_kinds!(a, "Cannot negate type {:?}", ty::Int(..) | ty::Float(..))
493                     }
494                     UnOp::Not => {
495                         check_kinds!(
496                             a,
497                             "Cannot binary not type {:?}",
498                             ty::Int(..) | ty::Uint(..) | ty::Bool
499                         );
500                     }
501                 }
502             }
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(..));
506             }
507             Rvalue::Cast(kind, operand, target_type) => {
508                 let op_ty = operand.ty(self.body, self.tcx);
509                 match kind {
510                     CastKind::DynStar => {
511                         // FIXME(dyn-star): make sure nothing needs to be done here.
512                     }
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 {
521                             self.fail(
522                                 location,
523                                 format!("Wrong cast kind {kind:?} for the type {op_ty}",),
524                             );
525                         }
526                     }
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'");
530                         }
531                     }
532                     CastKind::FloatToFloat | CastKind::FloatToInt => {
533                         if !op_ty.is_floating_point() || !target_type.is_numeric() {
534                             self.fail(
535                                 location,
536                                 format!(
537                                     "Trying to cast non 'Float' as {kind:?} into {target_type:?}"
538                                 ),
539                             );
540                         }
541                     }
542                 }
543             }
544             Rvalue::Repeat(_, _)
545             | Rvalue::ThreadLocalRef(_)
546             | Rvalue::AddressOf(_, _)
547             | Rvalue::NullaryOp(_, _)
548             | Rvalue::Discriminant(_) => {}
549         }
550         self.super_rvalue(rvalue, location);
551     }
552
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) {
560                     self.fail(
561                         location,
562                         format!(
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,
567                         ),
568                     );
569                 }
570                 if let Rvalue::CopyForDeref(place) = rvalue {
571                     if !place.ty(&self.body.local_decls, self.tcx).ty.builtin_deref(true).is_some()
572                     {
573                         self.fail(
574                             location,
575                             "`CopyForDeref` should only be used for dereferenceable types",
576                         )
577                     }
578                 }
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.
583                     if dest == src {
584                         self.fail(
585                             location,
586                             "encountered `Assign` statement with overlapping memory",
587                         );
588                     }
589                 }
590             }
591             StatementKind::AscribeUserType(..) => {
592                 if self.mir_phase >= MirPhase::Runtime(RuntimePhase::Initial) {
593                     self.fail(
594                         location,
595                         "`AscribeUserType` should have been removed after drop lowering phase",
596                     );
597                 }
598             }
599             StatementKind::FakeRead(..) => {
600                 if self.mir_phase >= MirPhase::Runtime(RuntimePhase::Initial) {
601                     self.fail(
602                         location,
603                         "`FakeRead` should have been removed after drop lowering phase",
604                     );
605                 }
606             }
607             StatementKind::Intrinsic(box NonDivergingIntrinsic::Assume(op)) => {
608                 let ty = op.ty(&self.body.local_decls, self.tcx);
609                 if !ty.is_bool() {
610                     self.fail(
611                         location,
612                         format!("`assume` argument must be `bool`, but got: `{}`", ty),
613                     );
614                 }
615             }
616             StatementKind::Intrinsic(box NonDivergingIntrinsic::CopyNonOverlapping(
617                 CopyNonOverlapping { src, dst, count },
618             )) => {
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) {
621                     src_deref.ty
622                 } else {
623                     self.fail(
624                         location,
625                         format!("Expected src to be ptr in copy_nonoverlapping, got: {}", src_ty),
626                     );
627                     return;
628                 };
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) {
631                     dst_deref.ty
632                 } else {
633                     self.fail(
634                         location,
635                         format!("Expected dst to be ptr in copy_nonoverlapping, got: {}", dst_ty),
636                     );
637                     return;
638                 };
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));
643                 }
644
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))
648                 }
649             }
650             StatementKind::SetDiscriminant { place, .. } => {
651                 if self.mir_phase < MirPhase::Runtime(RuntimePhase::Initial) {
652                     self.fail(location, "`SetDiscriminant`is not allowed until deaggregation");
653                 }
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, ..)) {
656                     self.fail(
657                         location,
658                         format!(
659                             "`SetDiscriminant` is only allowed on ADTs and generators, not {:?}",
660                             pty
661                         ),
662                     );
663                 }
664             }
665             StatementKind::Deinit(..) => {
666                 if self.mir_phase < MirPhase::Runtime(RuntimePhase::Initial) {
667                     self.fail(location, "`Deinit`is not allowed until deaggregation");
668                 }
669             }
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));
676                 }
677             }
678             StatementKind::StorageLive(..)
679             | StatementKind::StorageDead(..)
680             | StatementKind::Coverage(_)
681             | StatementKind::Nop => {}
682         }
683
684         self.super_statement(statement, location);
685     }
686
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);
691             }
692             TerminatorKind::SwitchInt { targets, discr } => {
693                 let switch_ty = discr.ty(&self.body.local_decls, self.tcx);
694
695                 let target_width = self.tcx.sess.target.pointer_width;
696
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(),
700                     ty::Char => 32,
701                     ty::Bool => 1,
702                     other => bug!("unhandled type: {:?}", other),
703                 });
704
705                 for (value, target) in targets.iter() {
706                     if Scalar::<()>::try_from_uint(value, size).is_none() {
707                         self.fail(
708                             location,
709                             format!("the value {:#x} is not a proper {:?}", value, switch_ty),
710                         )
711                     }
712
713                     self.check_edge(location, target, EdgeKind::Normal);
714                 }
715                 self.check_edge(location, targets.otherwise(), EdgeKind::Normal);
716
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();
723                 if has_duplicates {
724                     self.fail(
725                         location,
726                         format!(
727                             "duplicated values in `SwitchInt` terminator: {:?}",
728                             terminator.kind,
729                         ),
730                     );
731                 }
732             }
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);
737                 }
738             }
739             TerminatorKind::DropAndReplace { target, unwind, .. } => {
740                 if self.mir_phase >= MirPhase::Runtime(RuntimePhase::Initial) {
741                     self.fail(
742                         location,
743                         "`DropAndReplace` should have been removed during drop elaboration",
744                     );
745                 }
746                 self.check_edge(location, *target, EdgeKind::Normal);
747                 if let Some(unwind) = unwind {
748                     self.check_edge(location, *unwind, EdgeKind::Unwind);
749                 }
750             }
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(..) => {}
755                     _ => self.fail(
756                         location,
757                         format!("encountered non-callable type {} in `Call` terminator", func_ty),
758                     ),
759                 }
760                 if let Some(target) = target {
761                     self.check_edge(location, *target, EdgeKind::Normal);
762                 }
763                 if let Some(cleanup) = cleanup {
764                     self.check_edge(location, *cleanup, EdgeKind::Unwind);
765                 }
766
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());
772                 for arg in args {
773                     if let Operand::Move(place) = arg {
774                         self.place_cache.push(place.as_ref());
775                     }
776                 }
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();
781                 if has_duplicates {
782                     self.fail(
783                         location,
784                         format!(
785                             "encountered overlapping memory in `Call` terminator: {:?}",
786                             terminator.kind,
787                         ),
788                     );
789                 }
790             }
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 {
794                     self.fail(
795                         location,
796                         format!(
797                             "encountered non-boolean condition of type {} in `Assert` terminator",
798                             cond_ty
799                         ),
800                     );
801                 }
802                 self.check_edge(location, *target, EdgeKind::Normal);
803                 if let Some(cleanup) = cleanup {
804                     self.check_edge(location, *cleanup, EdgeKind::Unwind);
805                 }
806             }
807             TerminatorKind::Yield { resume, drop, .. } => {
808                 if self.body.generator.is_none() {
809                     self.fail(location, "`Yield` cannot appear outside generator bodies");
810                 }
811                 if self.mir_phase >= MirPhase::Runtime(RuntimePhase::Initial) {
812                     self.fail(location, "`Yield` should have been replaced by generator lowering");
813                 }
814                 self.check_edge(location, *resume, EdgeKind::Normal);
815                 if let Some(drop) = drop {
816                     self.check_edge(location, *drop, EdgeKind::Normal);
817                 }
818             }
819             TerminatorKind::FalseEdge { real_target, imaginary_target } => {
820                 if self.mir_phase >= MirPhase::Runtime(RuntimePhase::Initial) {
821                     self.fail(
822                         location,
823                         "`FalseEdge` should have been removed after drop elaboration",
824                     );
825                 }
826                 self.check_edge(location, *real_target, EdgeKind::Normal);
827                 self.check_edge(location, *imaginary_target, EdgeKind::Normal);
828             }
829             TerminatorKind::FalseUnwind { real_target, unwind } => {
830                 if self.mir_phase >= MirPhase::Runtime(RuntimePhase::Initial) {
831                     self.fail(
832                         location,
833                         "`FalseUnwind` should have been removed after drop elaboration",
834                     );
835                 }
836                 self.check_edge(location, *real_target, EdgeKind::Normal);
837                 if let Some(unwind) = unwind {
838                     self.check_edge(location, *unwind, EdgeKind::Unwind);
839                 }
840             }
841             TerminatorKind::InlineAsm { destination, cleanup, .. } => {
842                 if let Some(destination) = destination {
843                     self.check_edge(location, *destination, EdgeKind::Normal);
844                 }
845                 if let Some(cleanup) = cleanup {
846                     self.check_edge(location, *cleanup, EdgeKind::Unwind);
847                 }
848             }
849             TerminatorKind::GeneratorDrop => {
850                 if self.body.generator.is_none() {
851                     self.fail(location, "`GeneratorDrop` cannot appear outside generator bodies");
852                 }
853                 if self.mir_phase >= MirPhase::Runtime(RuntimePhase::Initial) {
854                     self.fail(
855                         location,
856                         "`GeneratorDrop` should have been replaced by generator lowering",
857                     );
858                 }
859             }
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")
864                 }
865             }
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")
870                 }
871             }
872             TerminatorKind::Unreachable => {}
873         }
874
875         self.super_terminator(terminator, location);
876     }
877
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(
881                 self.body.span,
882                 &format!(
883                     "broken MIR in {:?} ({}):\ninvalid source scope {:?}",
884                     self.body.source.instance, self.when, scope,
885                 ),
886             );
887         }
888     }
889 }