Skip to main content

rustc_mir_transform/
validate.rs

1//! Validates the MIR to ensure that invariants are upheld.
2
3use rustc_abi::{ExternAbi, FIRST_VARIANT, Size};
4use rustc_data_structures::fx::{FxHashMap, FxHashSet};
5use rustc_hir::LangItem;
6use rustc_hir::attrs::InlineAttr;
7use rustc_index::IndexVec;
8use rustc_index::bit_set::DenseBitSet;
9use rustc_infer::infer::TyCtxtInferExt;
10use rustc_infer::traits::{Obligation, ObligationCause};
11use rustc_middle::mir::coverage::CoverageKind;
12use rustc_middle::mir::visit::{MutatingUseContext, NonUseContext, PlaceContext, Visitor};
13use rustc_middle::mir::*;
14use rustc_middle::ty::adjustment::PointerCoercion;
15use rustc_middle::ty::print::with_no_trimmed_paths;
16use rustc_middle::ty::{
17    self, CoroutineArgsExt, InstanceKind, ScalarInt, Ty, TyCtxt, TypeVisitableExt, Unnormalized,
18    Upcast, Variance,
19};
20use rustc_middle::{bug, span_bug};
21use rustc_mir_dataflow::debuginfo::debuginfo_locals;
22use rustc_trait_selection::traits::ObligationCtxt;
23
24use crate::util::{self, most_packed_projection};
25
26#[derive(Copy, Clone, Debug, PartialEq, Eq)]
27enum EdgeKind {
28    Unwind,
29    Normal,
30}
31
32pub(super) struct Validator {
33    /// Describes at which point in the pipeline this validation is happening.
34    pub when: String,
35}
36
37impl<'tcx> crate::MirPass<'tcx> for Validator {
38    fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
39        // FIXME(JakobDegen): These bodies never instantiated in codegend anyway, so it's not
40        // terribly important that they pass the validator. However, I think other passes might
41        // still see them, in which case they might be surprised. It would probably be better if we
42        // didn't put this through the MIR pipeline at all.
43        if matches!(body.source.instance, InstanceKind::Intrinsic(..) | InstanceKind::Virtual(..)) {
44            return;
45        }
46        let def_id = body.source.def_id();
47        let typing_env = body.typing_env(tcx);
48        let can_unwind = if body.phase <= MirPhase::Runtime(RuntimePhase::Initial) {
49            // In this case `AbortUnwindingCalls` haven't yet been executed.
50            true
51        } else if !tcx.def_kind(def_id).is_fn_like() {
52            true
53        } else {
54            let body_ty = tcx.type_of(def_id).skip_binder();
55            let body_abi = match body_ty.kind() {
56                ty::FnDef(..) => body_ty.fn_sig(tcx).abi(),
57                ty::Closure(..) => ExternAbi::RustCall,
58                ty::CoroutineClosure(..) => ExternAbi::RustCall,
59                ty::Coroutine(..) => ExternAbi::Rust,
60                // No need to do MIR validation on error bodies
61                ty::Error(_) => return,
62                _ => span_bug!(body.span, "unexpected body ty: {body_ty}"),
63            };
64
65            ty::layout::fn_can_unwind(tcx, Some(def_id), body_abi)
66        };
67
68        let mut cfg_checker = CfgChecker {
69            when: &self.when,
70            body,
71            tcx,
72            unwind_edge_count: 0,
73            reachable_blocks: traversal::reachable_as_bitset(body),
74            value_cache: FxHashSet::default(),
75            can_unwind,
76        };
77        cfg_checker.visit_body(body);
78        cfg_checker.check_cleanup_control_flow();
79
80        // Also run the TypeChecker.
81        for (location, msg) in validate_types(tcx, typing_env, body, body) {
82            cfg_checker.fail(location, msg);
83        }
84
85        // Ensure that debuginfo records are not emitted for locals that are not in debuginfo.
86        for (location, msg) in validate_debuginfos(body) {
87            cfg_checker.fail(location, msg);
88        }
89
90        if let MirPhase::Runtime(_) = body.phase
91            && let ty::InstanceKind::Item(_) = body.source.instance
92            && body.has_free_regions()
93        {
94            cfg_checker.fail(
95                Location::START,
96                format!("Free regions in optimized {} MIR", body.phase.name()),
97            );
98        }
99    }
100
101    fn is_required(&self) -> bool {
102        true
103    }
104}
105
106/// This checker covers basic properties of the control-flow graph, (dis)allowed statements and terminators.
107/// Everything checked here must be stable under substitution of generic parameters. In other words,
108/// this is about the *structure* of the MIR, not the *contents*.
109///
110/// Everything that depends on types, or otherwise can be affected by generic parameters,
111/// must be checked in `TypeChecker`.
112struct CfgChecker<'a, 'tcx> {
113    when: &'a str,
114    body: &'a Body<'tcx>,
115    tcx: TyCtxt<'tcx>,
116    unwind_edge_count: usize,
117    reachable_blocks: DenseBitSet<BasicBlock>,
118    value_cache: FxHashSet<u128>,
119    // If `false`, then the MIR must not contain `UnwindAction::Continue` or
120    // `TerminatorKind::Resume`.
121    can_unwind: bool,
122}
123
124impl<'a, 'tcx> CfgChecker<'a, 'tcx> {
125    #[track_caller]
126    fn fail(&self, location: Location, msg: impl AsRef<str>) {
127        // We might see broken MIR when other errors have already occurred.
128        if self.tcx.dcx().has_errors().is_none() {
129            span_bug!(
130                self.body.source_info(location).span,
131                "broken MIR in {:?} ({}) at {:?}:\n{}",
132                self.body.source.instance,
133                self.when,
134                location,
135                msg.as_ref(),
136            );
137        }
138    }
139
140    fn check_edge(&mut self, location: Location, bb: BasicBlock, edge_kind: EdgeKind) {
141        if bb == START_BLOCK {
142            self.fail(location, "start block must not have predecessors")
143        }
144        if let Some(bb) = self.body.basic_blocks.get(bb) {
145            let src = self.body.basic_blocks.get(location.block).unwrap();
146            match (src.is_cleanup, bb.is_cleanup, edge_kind) {
147                // Non-cleanup blocks can jump to non-cleanup blocks along non-unwind edges
148                (false, false, EdgeKind::Normal) => {}
149                // Cleanup blocks can jump to cleanup blocks along non-unwind edges
150                (true, true, EdgeKind::Normal) => {}
151                // Non-cleanup blocks can jump to cleanup blocks along unwind edges
152                (false, true, EdgeKind::Unwind) => {
153                    self.unwind_edge_count += 1;
154                }
155                // All other jumps are invalid
156                _ => self.fail(
157                    location,
158                    format!(
159                        "{:?} edge to {:?} violates unwind invariants (cleanup {:?} -> {:?})",
160                        edge_kind, bb, src.is_cleanup, bb.is_cleanup,
161                    ),
162                ),
163            }
164        } else {
165            self.fail(location, format!("encountered jump to invalid basic block {bb:?}"))
166        }
167    }
168
169    fn check_cleanup_control_flow(&self) {
170        if self.unwind_edge_count <= 1 {
171            return;
172        }
173        let doms = self.body.basic_blocks.dominators();
174        let mut post_contract_node = FxHashMap::default();
175        // Reusing the allocation across invocations of the closure
176        let mut dom_path = vec![];
177        let mut get_post_contract_node = |mut bb| {
178            let root = loop {
179                if let Some(root) = post_contract_node.get(&bb) {
180                    break *root;
181                }
182                let parent = doms.immediate_dominator(bb).unwrap();
183                dom_path.push(bb);
184                if !self.body.basic_blocks[parent].is_cleanup {
185                    break bb;
186                }
187                bb = parent;
188            };
189            for bb in dom_path.drain(..) {
190                post_contract_node.insert(bb, root);
191            }
192            root
193        };
194
195        let mut parent = IndexVec::from_elem(None, &self.body.basic_blocks);
196        for (bb, bb_data) in self.body.basic_blocks.iter_enumerated() {
197            if !bb_data.is_cleanup || !self.reachable_blocks.contains(bb) {
198                continue;
199            }
200            let bb = get_post_contract_node(bb);
201            for s in bb_data.terminator().successors() {
202                let s = get_post_contract_node(s);
203                if s == bb {
204                    continue;
205                }
206                let parent = &mut parent[bb];
207                match parent {
208                    None => {
209                        *parent = Some(s);
210                    }
211                    Some(e) if *e == s => (),
212                    Some(e) => self.fail(
213                        Location { block: bb, statement_index: 0 },
214                        format!(
215                            "Cleanup control flow violation: The blocks dominated by {:?} have edges to both {:?} and {:?}",
216                            bb,
217                            s,
218                            *e
219                        )
220                    ),
221                }
222            }
223        }
224
225        // Check for cycles
226        let mut stack = FxHashSet::default();
227        for (mut bb, parent) in parent.iter_enumerated_mut() {
228            stack.clear();
229            stack.insert(bb);
230            loop {
231                let Some(parent) = parent.take() else { break };
232                let no_cycle = stack.insert(parent);
233                if !no_cycle {
234                    self.fail(
235                        Location { block: bb, statement_index: 0 },
236                        format!(
237                            "Cleanup control flow violation: Cycle involving edge {bb:?} -> {parent:?}",
238                        ),
239                    );
240                    break;
241                }
242                bb = parent;
243            }
244        }
245    }
246
247    fn check_unwind_edge(&mut self, location: Location, unwind: UnwindAction) {
248        let is_cleanup = self.body.basic_blocks[location.block].is_cleanup;
249        match unwind {
250            UnwindAction::Cleanup(unwind) => {
251                if is_cleanup {
252                    self.fail(location, "`UnwindAction::Cleanup` in cleanup block");
253                }
254                self.check_edge(location, unwind, EdgeKind::Unwind);
255            }
256            UnwindAction::Continue => {
257                if is_cleanup {
258                    self.fail(location, "`UnwindAction::Continue` in cleanup block");
259                }
260
261                if !self.can_unwind {
262                    self.fail(location, "`UnwindAction::Continue` in no-unwind function");
263                }
264            }
265            UnwindAction::Terminate(UnwindTerminateReason::InCleanup) => {
266                if !is_cleanup {
267                    self.fail(
268                        location,
269                        "`UnwindAction::Terminate(InCleanup)` in a non-cleanup block",
270                    );
271                }
272            }
273            // These are allowed everywhere.
274            UnwindAction::Unreachable | UnwindAction::Terminate(UnwindTerminateReason::Abi) => (),
275        }
276    }
277
278    fn is_critical_call_edge(&self, target: Option<BasicBlock>, unwind: UnwindAction) -> bool {
279        let Some(target) = target else { return false };
280        matches!(unwind, UnwindAction::Cleanup(_) | UnwindAction::Terminate(_))
281            && self.body.basic_blocks.predecessors()[target].len() > 1
282    }
283}
284
285impl<'a, 'tcx> Visitor<'tcx> for CfgChecker<'a, 'tcx> {
286    fn visit_local(&mut self, local: Local, _context: PlaceContext, location: Location) {
287        if self.body.local_decls.get(local).is_none() {
288            self.fail(
289                location,
290                format!("local {local:?} has no corresponding declaration in `body.local_decls`"),
291            );
292        }
293    }
294
295    fn visit_statement(&mut self, statement: &Statement<'tcx>, location: Location) {
296        match &statement.kind {
297            StatementKind::AscribeUserType(..) => {
298                if self.body.phase >= MirPhase::Runtime(RuntimePhase::Initial) {
299                    self.fail(
300                        location,
301                        "`AscribeUserType` should have been removed after drop lowering phase",
302                    );
303                }
304            }
305            StatementKind::FakeRead(..) => {
306                if self.body.phase >= MirPhase::Runtime(RuntimePhase::Initial) {
307                    self.fail(
308                        location,
309                        "`FakeRead` should have been removed after drop lowering phase",
310                    );
311                }
312            }
313            StatementKind::SetDiscriminant { .. } => {
314                if self.body.phase < MirPhase::Runtime(RuntimePhase::Initial) {
315                    self.fail(location, "`SetDiscriminant`is not allowed until deaggregation");
316                }
317            }
318            StatementKind::Coverage(kind) => {
319                if self.body.phase >= MirPhase::Analysis(AnalysisPhase::PostCleanup)
320                    && let CoverageKind::BlockMarker { .. } | CoverageKind::SpanMarker { .. } = kind
321                {
322                    self.fail(
323                        location,
324                        format!("{kind:?} should have been removed after analysis"),
325                    );
326                }
327            }
328            StatementKind::Assign(..)
329            | StatementKind::StorageLive(_)
330            | StatementKind::StorageDead(_)
331            | StatementKind::Intrinsic(_)
332            | StatementKind::ConstEvalCounter
333            | StatementKind::PlaceMention(..)
334            | StatementKind::BackwardIncompatibleDropHint { .. }
335            | StatementKind::Nop => {}
336        }
337
338        self.super_statement(statement, location);
339    }
340
341    fn visit_terminator(&mut self, terminator: &Terminator<'tcx>, location: Location) {
342        match &terminator.kind {
343            TerminatorKind::Goto { target } => {
344                self.check_edge(location, *target, EdgeKind::Normal);
345            }
346            TerminatorKind::SwitchInt { targets, discr: _ } => {
347                for (_, target) in targets.iter() {
348                    self.check_edge(location, target, EdgeKind::Normal);
349                }
350                self.check_edge(location, targets.otherwise(), EdgeKind::Normal);
351
352                self.value_cache.clear();
353                self.value_cache.extend(targets.iter().map(|(value, _)| value));
354                let has_duplicates = targets.iter().len() != self.value_cache.len();
355                if has_duplicates {
356                    self.fail(
357                        location,
358                        format!(
359                            "duplicated values in `SwitchInt` terminator: {:?}",
360                            terminator.kind,
361                        ),
362                    );
363                }
364            }
365            TerminatorKind::Drop { target, unwind, drop, .. } => {
366                self.check_edge(location, *target, EdgeKind::Normal);
367                self.check_unwind_edge(location, *unwind);
368                if let Some(drop) = drop {
369                    self.check_edge(location, *drop, EdgeKind::Normal);
370                }
371            }
372            TerminatorKind::Call { func, args, .. }
373            | TerminatorKind::TailCall { func, args, .. } => {
374                // FIXME(explicit_tail_calls): refactor this & add tail-call specific checks
375                if let TerminatorKind::Call { target, unwind, destination, .. } = terminator.kind {
376                    if let Some(target) = target {
377                        self.check_edge(location, target, EdgeKind::Normal);
378                    }
379                    self.check_unwind_edge(location, unwind);
380
381                    // The code generation assumes that there are no critical call edges. The
382                    // assumption is used to simplify inserting code that should be executed along
383                    // the return edge from the call. FIXME(tmiasko): Since this is a strictly code
384                    // generation concern, the code generation should be responsible for handling
385                    // it.
386                    if self.body.phase >= MirPhase::Runtime(RuntimePhase::Optimized)
387                        && self.is_critical_call_edge(target, unwind)
388                    {
389                        self.fail(
390                            location,
391                            format!(
392                                "encountered critical edge in `Call` terminator {:?}",
393                                terminator.kind,
394                            ),
395                        );
396                    }
397
398                    // The call destination place and Operand::Move place used as an argument might
399                    // be passed by a reference to the callee. Consequently they cannot be packed.
400                    if most_packed_projection(self.tcx, &self.body.local_decls, destination)
401                        .is_some()
402                    {
403                        // This is bad! The callee will expect the memory to be aligned.
404                        self.fail(
405                            location,
406                            format!(
407                                "encountered packed place in `Call` terminator destination: {:?}",
408                                terminator.kind,
409                            ),
410                        );
411                    }
412                }
413
414                for arg in args {
415                    if let Operand::Move(place) = &arg.node {
416                        if most_packed_projection(self.tcx, &self.body.local_decls, *place)
417                            .is_some()
418                        {
419                            // This is bad! The callee will expect the memory to be aligned.
420                            self.fail(
421                                location,
422                                format!(
423                                    "encountered `Move` of a packed place in `Call` terminator: {:?}",
424                                    terminator.kind,
425                                ),
426                            );
427                        }
428                    }
429                }
430
431                if let ty::FnDef(did, ..) = *func.ty(&self.body.local_decls, self.tcx).kind()
432                    && self.body.phase >= MirPhase::Runtime(RuntimePhase::Optimized)
433                    && matches!(self.tcx.codegen_fn_attrs(did).inline, InlineAttr::Force { .. })
434                {
435                    self.fail(location, "`#[rustc_force_inline]`-annotated function not inlined");
436                }
437            }
438            TerminatorKind::Assert { target, unwind, .. } => {
439                self.check_edge(location, *target, EdgeKind::Normal);
440                self.check_unwind_edge(location, *unwind);
441            }
442            TerminatorKind::Yield { resume, drop, .. } => {
443                if self.body.coroutine.is_none() {
444                    self.fail(location, "`Yield` cannot appear outside coroutine bodies");
445                }
446                if self.body.phase >= MirPhase::Runtime(RuntimePhase::Initial) {
447                    self.fail(location, "`Yield` should have been replaced by coroutine lowering");
448                }
449                self.check_edge(location, *resume, EdgeKind::Normal);
450                if let Some(drop) = drop {
451                    self.check_edge(location, *drop, EdgeKind::Normal);
452                }
453            }
454            TerminatorKind::FalseEdge { real_target, imaginary_target } => {
455                if self.body.phase >= MirPhase::Runtime(RuntimePhase::Initial) {
456                    self.fail(
457                        location,
458                        "`FalseEdge` should have been removed after drop elaboration",
459                    );
460                }
461                self.check_edge(location, *real_target, EdgeKind::Normal);
462                self.check_edge(location, *imaginary_target, EdgeKind::Normal);
463            }
464            TerminatorKind::FalseUnwind { real_target, unwind } => {
465                if self.body.phase >= MirPhase::Runtime(RuntimePhase::Initial) {
466                    self.fail(
467                        location,
468                        "`FalseUnwind` should have been removed after drop elaboration",
469                    );
470                }
471                self.check_edge(location, *real_target, EdgeKind::Normal);
472                self.check_unwind_edge(location, *unwind);
473            }
474            TerminatorKind::InlineAsm { targets, unwind, .. } => {
475                for &target in targets {
476                    self.check_edge(location, target, EdgeKind::Normal);
477                }
478                self.check_unwind_edge(location, *unwind);
479            }
480            TerminatorKind::CoroutineDrop => {
481                if self.body.coroutine.is_none() {
482                    self.fail(location, "`CoroutineDrop` cannot appear outside coroutine bodies");
483                }
484                if self.body.phase >= MirPhase::Runtime(RuntimePhase::Initial) {
485                    self.fail(
486                        location,
487                        "`CoroutineDrop` should have been replaced by coroutine lowering",
488                    );
489                }
490            }
491            TerminatorKind::UnwindResume => {
492                let bb = location.block;
493                if !self.body.basic_blocks[bb].is_cleanup {
494                    self.fail(location, "Cannot `UnwindResume` from non-cleanup basic block")
495                }
496                if !self.can_unwind {
497                    self.fail(location, "Cannot `UnwindResume` in a function that cannot unwind")
498                }
499            }
500            TerminatorKind::UnwindTerminate(_) => {
501                let bb = location.block;
502                if !self.body.basic_blocks[bb].is_cleanup {
503                    self.fail(location, "Cannot `UnwindTerminate` from non-cleanup basic block")
504                }
505            }
506            TerminatorKind::Return => {
507                let bb = location.block;
508                if self.body.basic_blocks[bb].is_cleanup {
509                    self.fail(location, "Cannot `Return` from cleanup basic block")
510                }
511            }
512            TerminatorKind::Unreachable => {}
513        }
514
515        self.super_terminator(terminator, location);
516    }
517
518    fn visit_source_scope(&mut self, scope: SourceScope) {
519        if self.body.source_scopes.get(scope).is_none() {
520            self.tcx.dcx().span_bug(
521                self.body.span,
522                format!(
523                    "broken MIR in {:?} ({}):\ninvalid source scope {:?}",
524                    self.body.source.instance, self.when, scope,
525                ),
526            );
527        }
528    }
529}
530
531/// A faster version of the validation pass that only checks those things which may break when
532/// instantiating any generic parameters.
533///
534/// `caller_body` is used to detect cycles in MIR inlining and MIR validation before
535/// `optimized_mir` is available.
536pub(super) fn validate_types<'tcx>(
537    tcx: TyCtxt<'tcx>,
538    typing_env: ty::TypingEnv<'tcx>,
539    body: &Body<'tcx>,
540    caller_body: &Body<'tcx>,
541) -> Vec<(Location, String)> {
542    let mut type_checker = TypeChecker { body, caller_body, tcx, typing_env, failures: Vec::new() };
543    // The type checker formats a bunch of strings with type names in it, but these strings
544    // are not always going to be encountered on the error path since the inliner also uses
545    // the validator, and there are certain kinds of inlining (even for valid code) that
546    // can cause validation errors (mostly around where clauses and rigid projections).
547    with_no_trimmed_paths!({
548        type_checker.visit_body(body);
549    });
550    type_checker.failures
551}
552
553struct TypeChecker<'a, 'tcx> {
554    body: &'a Body<'tcx>,
555    caller_body: &'a Body<'tcx>,
556    tcx: TyCtxt<'tcx>,
557    typing_env: ty::TypingEnv<'tcx>,
558    failures: Vec<(Location, String)>,
559}
560
561impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
562    fn fail(&mut self, location: Location, msg: impl Into<String>) {
563        self.failures.push((location, msg.into()));
564    }
565
566    /// Check if src can be assigned into dest.
567    /// This is not precise, it will accept some incorrect assignments.
568    fn mir_assign_valid_types(&self, src: Ty<'tcx>, dest: Ty<'tcx>) -> bool {
569        // Fast path before we normalize.
570        if src == dest {
571            // Equal types, all is good.
572            return true;
573        }
574
575        // We sometimes have to use `defining_opaque_types` for subtyping
576        // to succeed here and figuring out how exactly that should work
577        // is annoying. It is harmless enough to just not validate anything
578        // in that case. We still check this after analysis as all opaque
579        // types have been revealed at this point.
580        if (src, dest).has_opaque_types() {
581            return true;
582        }
583
584        // After borrowck subtyping should be fully explicit via
585        // `Subtype` projections.
586        let variance = if self.body.phase >= MirPhase::Runtime(RuntimePhase::Initial) {
587            Variance::Invariant
588        } else {
589            Variance::Covariant
590        };
591
592        crate::util::relate_types(self.tcx, self.typing_env, variance, src, dest)
593    }
594
595    /// Check that the given predicate definitely holds in the param-env of this MIR body.
596    fn predicate_must_hold_modulo_regions(
597        &self,
598        pred: impl Upcast<TyCtxt<'tcx>, ty::Predicate<'tcx>>,
599    ) -> bool {
600        let pred: ty::Predicate<'tcx> = pred.upcast(self.tcx);
601
602        // We sometimes have to use `defining_opaque_types` for predicates
603        // to succeed here and figuring out how exactly that should work
604        // is annoying. It is harmless enough to just not validate anything
605        // in that case. We still check this after analysis as all opaque
606        // types have been revealed at this point.
607        if pred.has_opaque_types() {
608            return true;
609        }
610
611        let (infcx, param_env) = self.tcx.infer_ctxt().build_with_typing_env(self.typing_env);
612        let ocx = ObligationCtxt::new(&infcx);
613        ocx.register_obligation(Obligation::new(
614            self.tcx,
615            ObligationCause::dummy(),
616            param_env,
617            pred,
618        ));
619        ocx.evaluate_obligations_error_on_ambiguity().is_empty()
620    }
621}
622
623impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
624    fn visit_operand(&mut self, operand: &Operand<'tcx>, location: Location) {
625        // This check is somewhat expensive, so only run it when -Zvalidate-mir is passed.
626        if self.tcx.sess.opts.unstable_opts.validate_mir
627            && self.body.phase < MirPhase::Runtime(RuntimePhase::Initial)
628        {
629            // `Operand::Copy` is only supposed to be used with `Copy` types.
630            if let Operand::Copy(place) = operand {
631                let ty = place.ty(&self.body.local_decls, self.tcx).ty;
632
633                if !self.tcx.type_is_copy_modulo_regions(self.typing_env, ty) {
634                    self.fail(location, format!("`Operand::Copy` with non-`Copy` type {ty}"));
635                }
636            }
637        }
638
639        self.super_operand(operand, location);
640    }
641
642    fn visit_projection_elem(
643        &mut self,
644        place_ref: PlaceRef<'tcx>,
645        elem: PlaceElem<'tcx>,
646        context: PlaceContext,
647        location: Location,
648    ) {
649        match elem {
650            ProjectionElem::Deref
651                if self.body.phase >= MirPhase::Runtime(RuntimePhase::Initial) =>
652            {
653                let base_ty = place_ref.ty(&self.body.local_decls, self.tcx).ty;
654
655                if base_ty.is_box() {
656                    self.fail(location, format!("{base_ty} dereferenced after ElaborateBoxDerefs"))
657                }
658            }
659            ProjectionElem::Field(f, ty) => {
660                let parent_ty = place_ref.ty(&self.body.local_decls, self.tcx);
661                let fail_out_of_bounds = |this: &mut Self, location| {
662                    this.fail(location, format!("Out of bounds field {f:?} for {parent_ty:?}"));
663                };
664                let check_equal = |this: &mut Self, location, f_ty| {
665                    if !this.mir_assign_valid_types(ty, f_ty) {
666                        this.fail(
667                            location,
668                            format!(
669                                "Field projection `{place_ref:?}.{f:?}` specified type `{ty}`, but actual type is `{f_ty}`"
670                            )
671                        )
672                    }
673                };
674
675                let kind = match parent_ty.ty.kind() {
676                    &ty::Alias(ty::AliasTy { kind: ty::Opaque { def_id }, args, .. }) => {
677                        self.tcx.type_of(def_id).instantiate(self.tcx, args).skip_norm_wip().kind()
678                    }
679                    kind => kind,
680                };
681
682                match kind {
683                    ty::Tuple(fields) => {
684                        let Some(f_ty) = fields.get(f.as_usize()) else {
685                            fail_out_of_bounds(self, location);
686                            return;
687                        };
688                        check_equal(self, location, *f_ty);
689                    }
690                    // Debug info is allowed to project into pattern types
691                    ty::Pat(base, _) => check_equal(self, location, *base),
692                    ty::Adt(adt_def, args) => {
693                        // see <https://github.com/rust-lang/rust/blob/7601adcc764d42c9f2984082b49948af652df986/compiler/rustc_middle/src/ty/layout.rs#L861-L864>
694                        if self.tcx.is_lang_item(adt_def.did(), LangItem::DynMetadata) {
695                            self.fail(
696                                location,
697                                format!(
698                                    "You can't project to field {f:?} of `DynMetadata` because \
699                                     layout is weird and thinks it doesn't have fields."
700                                ),
701                            );
702                        }
703
704                        if adt_def.repr().simd() {
705                            self.fail(
706                                location,
707                                format!(
708                                    "Projecting into SIMD type {adt_def:?} is banned by MCP#838"
709                                ),
710                            );
711                        }
712
713                        let var = parent_ty.variant_index.unwrap_or(FIRST_VARIANT);
714                        let Some(field) = adt_def.variant(var).fields.get(f) else {
715                            fail_out_of_bounds(self, location);
716                            return;
717                        };
718                        check_equal(self, location, field.ty(self.tcx, args).skip_norm_wip());
719                    }
720                    ty::Closure(_, args) => {
721                        let args = args.as_closure();
722                        let Some(&f_ty) = args.upvar_tys().get(f.as_usize()) else {
723                            fail_out_of_bounds(self, location);
724                            return;
725                        };
726                        check_equal(self, location, f_ty);
727                    }
728                    ty::CoroutineClosure(_, args) => {
729                        let args = args.as_coroutine_closure();
730                        let Some(&f_ty) = args.upvar_tys().get(f.as_usize()) else {
731                            fail_out_of_bounds(self, location);
732                            return;
733                        };
734                        check_equal(self, location, f_ty);
735                    }
736                    &ty::Coroutine(def_id, args) => {
737                        let f_ty = if let Some(var) = parent_ty.variant_index {
738                            // If we're currently validating an inlined copy of this body,
739                            // then it will no longer be parameterized over the original
740                            // args of the coroutine. Otherwise, we prefer to use this body
741                            // since we may be in the process of computing this MIR in the
742                            // first place.
743                            let layout = if def_id == self.caller_body.source.def_id() {
744                                self.caller_body
745                                    .coroutine_layout_raw()
746                                    .or_else(|| self.tcx.coroutine_layout(def_id, args).ok())
747                            } else if self.tcx.needs_coroutine_by_move_body_def_id(def_id)
748                                && let ty::ClosureKind::FnOnce =
749                                    args.as_coroutine().kind_ty().to_opt_closure_kind().unwrap()
750                                && self.caller_body.source.def_id()
751                                    == self.tcx.coroutine_by_move_body_def_id(def_id)
752                            {
753                                // Same if this is the by-move body of a coroutine-closure.
754                                self.caller_body.coroutine_layout_raw()
755                            } else {
756                                self.tcx.coroutine_layout(def_id, args).ok()
757                            };
758
759                            let Some(layout) = layout else {
760                                self.fail(
761                                    location,
762                                    format!("No coroutine layout for {parent_ty:?}"),
763                                );
764                                return;
765                            };
766
767                            let Some(&local) = layout.variant_fields[var].get(f) else {
768                                fail_out_of_bounds(self, location);
769                                return;
770                            };
771
772                            let Some(f_ty) = layout.field_tys.get(local) else {
773                                self.fail(
774                                    location,
775                                    format!("Out of bounds local {local:?} for {parent_ty:?}"),
776                                );
777                                return;
778                            };
779
780                            ty::EarlyBinder::bind(f_ty.ty)
781                                .instantiate(self.tcx, args)
782                                .skip_norm_wip()
783                        } else {
784                            let Some(&f_ty) = args.as_coroutine().prefix_tys().get(f.index())
785                            else {
786                                fail_out_of_bounds(self, location);
787                                return;
788                            };
789
790                            f_ty
791                        };
792
793                        check_equal(self, location, f_ty);
794                    }
795                    _ => {
796                        self.fail(location, format!("{:?} does not have fields", parent_ty.ty));
797                    }
798                }
799            }
800            ProjectionElem::Index(index) => {
801                let indexed_ty = place_ref.ty(&self.body.local_decls, self.tcx).ty;
802                match indexed_ty.kind() {
803                    ty::Array(_, _) | ty::Slice(_) => {}
804                    _ => self.fail(location, format!("{indexed_ty:?} cannot be indexed")),
805                }
806
807                let index_ty = self.body.local_decls[index].ty;
808                if index_ty != self.tcx.types.usize {
809                    self.fail(location, format!("bad index ({index_ty} != usize)"))
810                }
811            }
812            ProjectionElem::ConstantIndex { offset, min_length, from_end } => {
813                let indexed_ty = place_ref.ty(&self.body.local_decls, self.tcx).ty;
814                match indexed_ty.kind() {
815                    ty::Array(_, _) => {
816                        if from_end {
817                            self.fail(location, "arrays should not be indexed from end");
818                        }
819                    }
820                    ty::Slice(_) => {}
821                    _ => self.fail(location, format!("{indexed_ty:?} cannot be indexed")),
822                }
823
824                if from_end {
825                    if offset > min_length {
826                        self.fail(
827                            location,
828                            format!(
829                                "constant index with offset -{offset} out of bounds of min length {min_length}"
830                            ),
831                        );
832                    }
833                } else {
834                    if offset >= min_length {
835                        self.fail(
836                            location,
837                            format!(
838                                "constant index with offset {offset} out of bounds of min length {min_length}"
839                            ),
840                        );
841                    }
842                }
843            }
844            ProjectionElem::Subslice { from, to, from_end } => {
845                let indexed_ty = place_ref.ty(&self.body.local_decls, self.tcx).ty;
846                match indexed_ty.kind() {
847                    ty::Array(_, _) => {
848                        if from_end {
849                            self.fail(location, "arrays should not be subsliced from end");
850                        }
851                    }
852                    ty::Slice(_) => {
853                        if !from_end {
854                            self.fail(location, "slices should be subsliced from end");
855                        }
856                    }
857                    _ => self.fail(location, format!("{indexed_ty:?} cannot be indexed")),
858                }
859
860                if !from_end && from > to {
861                    self.fail(location, "backwards subslice {from}..{to}");
862                }
863            }
864            ProjectionElem::OpaqueCast(ty)
865                if self.body.phase >= MirPhase::Runtime(RuntimePhase::Initial) =>
866            {
867                self.fail(
868                    location,
869                    format!("explicit opaque type cast to `{ty}` after `PostAnalysisNormalize`"),
870                )
871            }
872            ProjectionElem::UnwrapUnsafeBinder(unwrapped_ty) => {
873                let binder_ty = place_ref.ty(&self.body.local_decls, self.tcx);
874                let ty::UnsafeBinder(binder_ty) = *binder_ty.ty.kind() else {
875                    self.fail(
876                        location,
877                        format!("WrapUnsafeBinder does not produce a ty::UnsafeBinder"),
878                    );
879                    return;
880                };
881                let binder_inner_ty = self.tcx.instantiate_bound_regions_with_erased(*binder_ty);
882                if !self.mir_assign_valid_types(unwrapped_ty, binder_inner_ty) {
883                    self.fail(
884                        location,
885                        format!(
886                            "Cannot unwrap unsafe binder {binder_ty:?} into type {unwrapped_ty}"
887                        ),
888                    );
889                }
890            }
891            _ => {}
892        }
893        self.super_projection_elem(place_ref, elem, context, location);
894    }
895
896    fn visit_var_debug_info(&mut self, debuginfo: &VarDebugInfo<'tcx>) {
897        if let Some(VarDebugInfoFragment { ty, ref projection }) = debuginfo.composite {
898            if ty.is_union() || ty.is_enum() {
899                self.fail(
900                    START_BLOCK.start_location(),
901                    format!("invalid type {ty} in debuginfo for {:?}", debuginfo.name),
902                );
903            }
904            if projection.is_empty() {
905                self.fail(
906                    START_BLOCK.start_location(),
907                    format!("invalid empty projection in debuginfo for {:?}", debuginfo.name),
908                );
909            }
910            if projection.iter().any(|p| !matches!(p, PlaceElem::Field(..))) {
911                self.fail(
912                    START_BLOCK.start_location(),
913                    format!(
914                        "illegal projection {:?} in debuginfo for {:?}",
915                        projection, debuginfo.name
916                    ),
917                );
918            }
919        }
920        match debuginfo.value {
921            VarDebugInfoContents::Const(_) => {}
922            VarDebugInfoContents::Place(place) => {
923                if place.projection.iter().any(|p| !p.can_use_in_debuginfo()) {
924                    self.fail(
925                        START_BLOCK.start_location(),
926                        format!("illegal place {:?} in debuginfo for {:?}", place, debuginfo.name),
927                    );
928                }
929            }
930        }
931        self.super_var_debug_info(debuginfo);
932    }
933
934    fn visit_place(&mut self, place: &Place<'tcx>, cntxt: PlaceContext, location: Location) {
935        // Set off any `bug!`s in the type computation code
936        let _ = place.ty(&self.body.local_decls, self.tcx);
937
938        if self.body.phase >= MirPhase::Runtime(RuntimePhase::Initial)
939            && place.projection.len() > 1
940            && cntxt != PlaceContext::NonUse(NonUseContext::VarDebugInfo)
941            && place.projection[1..].contains(&ProjectionElem::Deref)
942        {
943            self.fail(
944                location,
945                format!("place {place:?} has deref as a later projection (it is only permitted as the first projection)"),
946            );
947        }
948
949        // Ensure all downcast projections are followed by field projections.
950        let mut projections_iter = place.projection.iter();
951        while let Some(proj) = projections_iter.next() {
952            if matches!(proj, ProjectionElem::Downcast(..)) {
953                if !matches!(projections_iter.next(), Some(ProjectionElem::Field(..))) {
954                    self.fail(
955                        location,
956                        format!(
957                            "place {place:?} has `Downcast` projection not followed by `Field`"
958                        ),
959                    );
960                }
961            }
962        }
963
964        if let ClearCrossCrate::Set(LocalInfo::DerefTemp) =
965            self.body.local_decls[place.local].local_info
966            && !place.is_indirect_first_projection()
967        {
968            if cntxt != PlaceContext::MutatingUse(MutatingUseContext::Store)
969                || place.as_local().is_none()
970            {
971                self.fail(
972                    location,
973                    format!("`DerefTemp` locals must only be dereferenced or directly assigned to"),
974                );
975            }
976        }
977
978        if self.body.phase < MirPhase::Runtime(RuntimePhase::Initial)
979            && let Some(i) = place
980                .projection
981                .iter()
982                .position(|elem| matches!(elem, ProjectionElem::Subslice { .. }))
983            && let Some(tail) = place.projection.get(i + 1..)
984            && tail.iter().any(|elem| {
985                matches!(
986                    elem,
987                    ProjectionElem::ConstantIndex { .. } | ProjectionElem::Subslice { .. }
988                )
989            })
990        {
991            self.fail(
992                location,
993                format!("place {place:?} has `ConstantIndex` or `Subslice` after `Subslice`"),
994            );
995        }
996
997        self.super_place(place, cntxt, location);
998    }
999
1000    fn visit_rvalue(&mut self, rvalue: &Rvalue<'tcx>, location: Location) {
1001        macro_rules! check_kinds {
1002            ($t:expr, $text:literal, $typat:pat) => {
1003                if !matches!(($t).kind(), $typat) {
1004                    self.fail(location, format!($text, $t));
1005                }
1006            };
1007        }
1008        match rvalue {
1009            Rvalue::Use(_, _) => {}
1010            Rvalue::CopyForDeref(_) => {
1011                if self.body.phase >= MirPhase::Runtime(RuntimePhase::Initial) {
1012                    self.fail(location, "`CopyForDeref` should have been removed in runtime MIR");
1013                }
1014            }
1015            Rvalue::Aggregate(kind, fields) => match **kind {
1016                AggregateKind::Tuple => {}
1017                AggregateKind::Array(dest) => {
1018                    for src in fields {
1019                        if !self.mir_assign_valid_types(src.ty(self.body, self.tcx), dest) {
1020                            self.fail(location, "array field has the wrong type");
1021                        }
1022                    }
1023                }
1024                AggregateKind::Adt(def_id, idx, args, _, Some(field)) => {
1025                    let adt_def = self.tcx.adt_def(def_id);
1026                    assert!(adt_def.is_union());
1027                    assert_eq!(idx, FIRST_VARIANT);
1028                    let dest_ty = self.tcx.normalize_erasing_regions(
1029                        self.typing_env,
1030                        adt_def.non_enum_variant().fields[field].ty(self.tcx, args),
1031                    );
1032                    if let [field] = fields.raw.as_slice() {
1033                        let src_ty = field.ty(self.body, self.tcx);
1034                        if !self.mir_assign_valid_types(src_ty, dest_ty) {
1035                            self.fail(location, "union field has the wrong type");
1036                        }
1037                    } else {
1038                        self.fail(location, "unions should have one initialized field");
1039                    }
1040                }
1041                AggregateKind::Adt(def_id, idx, args, _, None) => {
1042                    let adt_def = self.tcx.adt_def(def_id);
1043                    assert!(!adt_def.is_union());
1044                    let variant = &adt_def.variants()[idx];
1045                    if variant.fields.len() != fields.len() {
1046                        self.fail(location, format!(
1047                            "adt {def_id:?} has the wrong number of initialized fields, expected {}, found {}",
1048                            fields.len(),
1049                            variant.fields.len(),
1050                        ));
1051                    }
1052                    for (src, dest) in std::iter::zip(fields, &variant.fields) {
1053                        let dest_ty = self
1054                            .tcx
1055                            .normalize_erasing_regions(self.typing_env, dest.ty(self.tcx, args));
1056                        if !self.mir_assign_valid_types(src.ty(self.body, self.tcx), dest_ty) {
1057                            self.fail(location, "adt field has the wrong type");
1058                        }
1059                    }
1060                }
1061                AggregateKind::Closure(_, args) => {
1062                    let upvars = args.as_closure().upvar_tys();
1063                    if upvars.len() != fields.len() {
1064                        self.fail(location, "closure has the wrong number of initialized fields");
1065                    }
1066                    for (src, dest) in std::iter::zip(fields, upvars) {
1067                        if !self.mir_assign_valid_types(src.ty(self.body, self.tcx), dest) {
1068                            self.fail(location, "closure field has the wrong type");
1069                        }
1070                    }
1071                }
1072                AggregateKind::Coroutine(_, args) => {
1073                    let upvars = args.as_coroutine().upvar_tys();
1074                    if upvars.len() != fields.len() {
1075                        self.fail(location, "coroutine has the wrong number of initialized fields");
1076                    }
1077                    for (src, dest) in std::iter::zip(fields, upvars) {
1078                        if !self.mir_assign_valid_types(src.ty(self.body, self.tcx), dest) {
1079                            self.fail(location, "coroutine field has the wrong type");
1080                        }
1081                    }
1082                }
1083                AggregateKind::CoroutineClosure(_, args) => {
1084                    let upvars = args.as_coroutine_closure().upvar_tys();
1085                    if upvars.len() != fields.len() {
1086                        self.fail(
1087                            location,
1088                            "coroutine-closure has the wrong number of initialized fields",
1089                        );
1090                    }
1091                    for (src, dest) in std::iter::zip(fields, upvars) {
1092                        if !self.mir_assign_valid_types(src.ty(self.body, self.tcx), dest) {
1093                            self.fail(location, "coroutine-closure field has the wrong type");
1094                        }
1095                    }
1096                }
1097                AggregateKind::RawPtr(pointee_ty, mutability) => {
1098                    if !matches!(self.body.phase, MirPhase::Runtime(_)) {
1099                        // It would probably be fine to support this in earlier phases, but at the
1100                        // time of writing it's only ever introduced from intrinsic lowering, so
1101                        // earlier things just `bug!` on it.
1102                        self.fail(location, "RawPtr should be in runtime MIR only");
1103                    }
1104
1105                    if let [data_ptr, metadata] = fields.raw.as_slice() {
1106                        let data_ptr_ty = data_ptr.ty(self.body, self.tcx);
1107                        let metadata_ty = metadata.ty(self.body, self.tcx);
1108                        if let ty::RawPtr(in_pointee, in_mut) = data_ptr_ty.kind() {
1109                            if *in_mut != mutability {
1110                                self.fail(location, "input and output mutability must match");
1111                            }
1112
1113                            // FIXME: check `Thin` instead of `Sized`
1114                            if !in_pointee.is_sized(self.tcx, self.typing_env) {
1115                                self.fail(location, "input pointer must be thin");
1116                            }
1117                        } else {
1118                            self.fail(
1119                                location,
1120                                "first operand to raw pointer aggregate must be a raw pointer",
1121                            );
1122                        }
1123
1124                        // FIXME: Check metadata more generally
1125                        if pointee_ty.is_slice() {
1126                            if !self.mir_assign_valid_types(metadata_ty, self.tcx.types.usize) {
1127                                self.fail(location, "slice metadata must be usize");
1128                            }
1129                        } else if pointee_ty.is_sized(self.tcx, self.typing_env) {
1130                            if metadata_ty != self.tcx.types.unit {
1131                                self.fail(location, "metadata for pointer-to-thin must be unit");
1132                            }
1133                        }
1134                    } else {
1135                        self.fail(location, "raw pointer aggregate must have 2 fields");
1136                    }
1137                }
1138            },
1139            Rvalue::Ref(_, BorrowKind::Fake(_), _) => {
1140                if self.body.phase >= MirPhase::Runtime(RuntimePhase::Initial) {
1141                    self.fail(
1142                        location,
1143                        "`Assign` statement with a `Fake` borrow should have been removed in runtime MIR",
1144                    );
1145                }
1146            }
1147            Rvalue::Ref(..) | Rvalue::Reborrow(..) => {}
1148            Rvalue::BinaryOp(op, vals) => {
1149                use BinOp::*;
1150                let a = vals.0.ty(&self.body.local_decls, self.tcx);
1151                let b = vals.1.ty(&self.body.local_decls, self.tcx);
1152                if crate::util::binop_right_homogeneous(*op) {
1153                    if let Eq | Lt | Le | Ne | Ge | Gt = op {
1154                        // The function pointer types can have lifetimes
1155                        if !self.mir_assign_valid_types(a, b) {
1156                            self.fail(
1157                                location,
1158                                format!("Cannot {op:?} compare incompatible types {a} and {b}"),
1159                            );
1160                        }
1161                    } else if a != b {
1162                        self.fail(
1163                            location,
1164                            format!("Cannot perform binary op {op:?} on unequal types {a} and {b}"),
1165                        );
1166                    }
1167                }
1168
1169                match op {
1170                    Offset => {
1171                        check_kinds!(a, "Cannot offset non-pointer type {:?}", ty::RawPtr(..));
1172                        if b != self.tcx.types.isize && b != self.tcx.types.usize {
1173                            self.fail(location, format!("Cannot offset by non-isize type {b}"));
1174                        }
1175                    }
1176                    Eq | Lt | Le | Ne | Ge | Gt => {
1177                        for x in [a, b] {
1178                            check_kinds!(
1179                                x,
1180                                "Cannot {op:?} compare type {:?}",
1181                                ty::Bool
1182                                    | ty::Char
1183                                    | ty::Int(..)
1184                                    | ty::Uint(..)
1185                                    | ty::Float(..)
1186                                    | ty::RawPtr(..)
1187                                    | ty::FnPtr(..)
1188                            )
1189                        }
1190                    }
1191                    Cmp => {
1192                        for x in [a, b] {
1193                            check_kinds!(
1194                                x,
1195                                "Cannot three-way compare non-integer type {:?}",
1196                                ty::Char | ty::Uint(..) | ty::Int(..)
1197                            )
1198                        }
1199                    }
1200                    AddUnchecked | AddWithOverflow | SubUnchecked | SubWithOverflow
1201                    | MulUnchecked | MulWithOverflow | Shl | ShlUnchecked | Shr | ShrUnchecked => {
1202                        for x in [a, b] {
1203                            check_kinds!(
1204                                x,
1205                                "Cannot {op:?} non-integer type {:?}",
1206                                ty::Uint(..) | ty::Int(..)
1207                            )
1208                        }
1209                    }
1210                    BitAnd | BitOr | BitXor => {
1211                        for x in [a, b] {
1212                            check_kinds!(
1213                                x,
1214                                "Cannot perform bitwise op {op:?} on type {:?}",
1215                                ty::Uint(..) | ty::Int(..) | ty::Bool
1216                            )
1217                        }
1218                    }
1219                    Add | Sub | Mul | Div | Rem => {
1220                        for x in [a, b] {
1221                            check_kinds!(
1222                                x,
1223                                "Cannot perform arithmetic {op:?} on type {:?}",
1224                                ty::Uint(..) | ty::Int(..) | ty::Float(..)
1225                            )
1226                        }
1227                    }
1228                }
1229            }
1230            Rvalue::UnaryOp(op, operand) => {
1231                let a = operand.ty(&self.body.local_decls, self.tcx);
1232                match op {
1233                    UnOp::Neg => {
1234                        check_kinds!(a, "Cannot negate type {:?}", ty::Int(..) | ty::Float(..))
1235                    }
1236                    UnOp::Not => {
1237                        check_kinds!(
1238                            a,
1239                            "Cannot binary not type {:?}",
1240                            ty::Int(..) | ty::Uint(..) | ty::Bool
1241                        );
1242                    }
1243                    UnOp::PtrMetadata => {
1244                        check_kinds!(
1245                            a,
1246                            "Cannot PtrMetadata non-pointer non-reference type {:?}",
1247                            ty::RawPtr(..) | ty::Ref(..)
1248                        );
1249                    }
1250                }
1251            }
1252            Rvalue::Cast(kind, operand, target_type) => {
1253                let op_ty = operand.ty(self.body, self.tcx);
1254                match kind {
1255                    // FIXME: Add Checks for these
1256                    CastKind::PointerWithExposedProvenance | CastKind::PointerExposeProvenance => {}
1257                    CastKind::PointerCoercion(PointerCoercion::ReifyFnPointer(_), _) => {
1258                        // FIXME: check signature compatibility.
1259                        check_kinds!(
1260                            op_ty,
1261                            "CastKind::{kind:?} input must be a fn item, not {:?}",
1262                            ty::FnDef(..)
1263                        );
1264                        check_kinds!(
1265                            target_type,
1266                            "CastKind::{kind:?} output must be a fn pointer, not {:?}",
1267                            ty::FnPtr(..)
1268                        );
1269                    }
1270                    CastKind::PointerCoercion(PointerCoercion::UnsafeFnPointer, _) => {
1271                        // FIXME: check safety and signature compatibility.
1272                        check_kinds!(
1273                            op_ty,
1274                            "CastKind::{kind:?} input must be a fn pointer, not {:?}",
1275                            ty::FnPtr(..)
1276                        );
1277                        check_kinds!(
1278                            target_type,
1279                            "CastKind::{kind:?} output must be a fn pointer, not {:?}",
1280                            ty::FnPtr(..)
1281                        );
1282                    }
1283                    CastKind::PointerCoercion(PointerCoercion::ClosureFnPointer(..), _) => {
1284                        // FIXME: check safety, captures, and signature compatibility.
1285                        check_kinds!(
1286                            op_ty,
1287                            "CastKind::{kind:?} input must be a closure, not {:?}",
1288                            ty::Closure(..)
1289                        );
1290                        check_kinds!(
1291                            target_type,
1292                            "CastKind::{kind:?} output must be a fn pointer, not {:?}",
1293                            ty::FnPtr(..)
1294                        );
1295                    }
1296                    CastKind::PointerCoercion(PointerCoercion::MutToConstPointer, _) => {
1297                        // FIXME: check same pointee?
1298                        check_kinds!(
1299                            op_ty,
1300                            "CastKind::{kind:?} input must be a raw mut pointer, not {:?}",
1301                            ty::RawPtr(_, Mutability::Mut)
1302                        );
1303                        check_kinds!(
1304                            target_type,
1305                            "CastKind::{kind:?} output must be a raw const pointer, not {:?}",
1306                            ty::RawPtr(_, Mutability::Not)
1307                        );
1308                        if self.body.phase >= MirPhase::Analysis(AnalysisPhase::PostCleanup) {
1309                            self.fail(location, format!("After borrowck, MIR disallows {kind:?}"));
1310                        }
1311                    }
1312                    CastKind::PointerCoercion(PointerCoercion::ArrayToPointer, _) => {
1313                        // FIXME: Check pointee types
1314                        check_kinds!(
1315                            op_ty,
1316                            "CastKind::{kind:?} input must be a raw pointer, not {:?}",
1317                            ty::RawPtr(..)
1318                        );
1319                        check_kinds!(
1320                            target_type,
1321                            "CastKind::{kind:?} output must be a raw pointer, not {:?}",
1322                            ty::RawPtr(..)
1323                        );
1324                        if self.body.phase >= MirPhase::Analysis(AnalysisPhase::PostCleanup) {
1325                            self.fail(location, format!("After borrowck, MIR disallows {kind:?}"));
1326                        }
1327                    }
1328                    CastKind::PointerCoercion(PointerCoercion::Unsize, _) => {
1329                        // Pointers being unsize coerced should at least implement
1330                        // `CoerceUnsized`.
1331                        if !self.predicate_must_hold_modulo_regions(ty::TraitRef::new(
1332                            self.tcx,
1333                            self.tcx.require_lang_item(
1334                                LangItem::CoerceUnsized,
1335                                self.body.source_info(location).span,
1336                            ),
1337                            [op_ty, *target_type],
1338                        )) {
1339                            self.fail(location, format!("Unsize coercion, but `{op_ty}` isn't coercible to `{target_type}`"));
1340                        }
1341                    }
1342                    CastKind::IntToInt | CastKind::IntToFloat => {
1343                        let input_valid = op_ty.is_integral() || op_ty.is_char() || op_ty.is_bool();
1344                        let target_valid = target_type.is_numeric() || target_type.is_char();
1345                        if !input_valid || !target_valid {
1346                            self.fail(
1347                                location,
1348                                format!("Wrong cast kind {kind:?} for the type {op_ty}"),
1349                            );
1350                        }
1351                    }
1352                    CastKind::FnPtrToPtr => {
1353                        check_kinds!(
1354                            op_ty,
1355                            "CastKind::{kind:?} input must be a fn pointer, not {:?}",
1356                            ty::FnPtr(..)
1357                        );
1358                        check_kinds!(
1359                            target_type,
1360                            "CastKind::{kind:?} output must be a raw pointer, not {:?}",
1361                            ty::RawPtr(..)
1362                        );
1363                    }
1364                    CastKind::PtrToPtr => {
1365                        check_kinds!(
1366                            op_ty,
1367                            "CastKind::{kind:?} input must be a raw pointer, not {:?}",
1368                            ty::RawPtr(..)
1369                        );
1370                        check_kinds!(
1371                            target_type,
1372                            "CastKind::{kind:?} output must be a raw pointer, not {:?}",
1373                            ty::RawPtr(..)
1374                        );
1375                    }
1376                    CastKind::FloatToFloat | CastKind::FloatToInt => {
1377                        if !op_ty.is_floating_point() || !target_type.is_numeric() {
1378                            self.fail(
1379                                location,
1380                                format!(
1381                                    "Trying to cast non 'Float' as {kind:?} into {target_type:?}"
1382                                ),
1383                            );
1384                        }
1385                    }
1386                    CastKind::Transmute => {
1387                        // Unlike `mem::transmute`, a MIR `Transmute` is well-formed
1388                        // for any two `Sized` types, just potentially UB to run.
1389
1390                        if !self
1391                            .tcx
1392                            .normalize_erasing_regions(
1393                                self.typing_env,
1394                                Unnormalized::new_wip(op_ty),
1395                            )
1396                            .is_sized(self.tcx, self.typing_env)
1397                        {
1398                            self.fail(
1399                                location,
1400                                format!("Cannot transmute from non-`Sized` type {op_ty}"),
1401                            );
1402                        }
1403                        if !self
1404                            .tcx
1405                            .normalize_erasing_regions(
1406                                self.typing_env,
1407                                Unnormalized::new_wip(*target_type),
1408                            )
1409                            .is_sized(self.tcx, self.typing_env)
1410                        {
1411                            self.fail(
1412                                location,
1413                                format!("Cannot transmute to non-`Sized` type {target_type:?}"),
1414                            );
1415                        }
1416                    }
1417                    CastKind::Subtype => {
1418                        if !util::sub_types(self.tcx, self.typing_env, op_ty, *target_type) {
1419                            self.fail(
1420                                location,
1421                                format!("Failed subtyping {op_ty} and {target_type}"),
1422                            )
1423                        }
1424                    }
1425                }
1426            }
1427            Rvalue::Repeat(_, _)
1428            | Rvalue::ThreadLocalRef(_)
1429            | Rvalue::RawPtr(_, _)
1430            | Rvalue::Discriminant(_) => {}
1431
1432            Rvalue::WrapUnsafeBinder(op, ty) => {
1433                let unwrapped_ty = op.ty(self.body, self.tcx);
1434                let ty::UnsafeBinder(binder_ty) = *ty.kind() else {
1435                    self.fail(
1436                        location,
1437                        format!("WrapUnsafeBinder does not produce a ty::UnsafeBinder"),
1438                    );
1439                    return;
1440                };
1441                let binder_inner_ty = self.tcx.instantiate_bound_regions_with_erased(*binder_ty);
1442                if !self.mir_assign_valid_types(unwrapped_ty, binder_inner_ty) {
1443                    self.fail(
1444                        location,
1445                        format!("Cannot wrap {unwrapped_ty} into unsafe binder {binder_ty:?}"),
1446                    );
1447                }
1448            }
1449        }
1450        self.super_rvalue(rvalue, location);
1451    }
1452
1453    fn visit_statement(&mut self, statement: &Statement<'tcx>, location: Location) {
1454        match &statement.kind {
1455            StatementKind::Assign((dest, rvalue)) => {
1456                // LHS and RHS of the assignment must have the same type.
1457                let left_ty = dest.ty(&self.body.local_decls, self.tcx).ty;
1458                let right_ty = rvalue.ty(&self.body.local_decls, self.tcx);
1459
1460                if !self.mir_assign_valid_types(right_ty, left_ty) {
1461                    self.fail(
1462                        location,
1463                        format!(
1464                            "encountered `{:?}` with incompatible types:\n\
1465                            left-hand side has type: {}\n\
1466                            right-hand side has type: {}",
1467                            statement.kind, left_ty, right_ty,
1468                        ),
1469                    );
1470                }
1471
1472                if let Some(local) = dest.as_local()
1473                    && let ClearCrossCrate::Set(LocalInfo::DerefTemp) =
1474                        self.body.local_decls[local].local_info
1475                    && !matches!(rvalue, Rvalue::CopyForDeref(_))
1476                {
1477                    self.fail(location, "assignment to a `DerefTemp` must use `CopyForDeref`")
1478                }
1479            }
1480            StatementKind::AscribeUserType(..) => {
1481                if self.body.phase >= MirPhase::Runtime(RuntimePhase::Initial) {
1482                    self.fail(
1483                        location,
1484                        "`AscribeUserType` should have been removed after drop lowering phase",
1485                    );
1486                }
1487            }
1488            StatementKind::FakeRead(..) => {
1489                if self.body.phase >= MirPhase::Runtime(RuntimePhase::Initial) {
1490                    self.fail(
1491                        location,
1492                        "`FakeRead` should have been removed after drop lowering phase",
1493                    );
1494                }
1495            }
1496            StatementKind::Intrinsic(NonDivergingIntrinsic::Assume(op)) => {
1497                let ty = op.ty(&self.body.local_decls, self.tcx);
1498                if !ty.is_bool() {
1499                    self.fail(
1500                        location,
1501                        format!("`assume` argument must be `bool`, but got: `{ty}`"),
1502                    );
1503                }
1504            }
1505            StatementKind::Intrinsic(NonDivergingIntrinsic::CopyNonOverlapping(
1506                CopyNonOverlapping { src, dst, count },
1507            )) => {
1508                let src_ty = src.ty(&self.body.local_decls, self.tcx);
1509                let op_src_ty = if let Some(src_deref) = src_ty.builtin_deref(true) {
1510                    src_deref
1511                } else {
1512                    self.fail(
1513                        location,
1514                        format!("Expected src to be ptr in copy_nonoverlapping, got: {src_ty}"),
1515                    );
1516                    return;
1517                };
1518                let dst_ty = dst.ty(&self.body.local_decls, self.tcx);
1519                let op_dst_ty = if let Some(dst_deref) = dst_ty.builtin_deref(true) {
1520                    dst_deref
1521                } else {
1522                    self.fail(
1523                        location,
1524                        format!("Expected dst to be ptr in copy_nonoverlapping, got: {dst_ty}"),
1525                    );
1526                    return;
1527                };
1528                // since CopyNonOverlapping is parametrized by 1 type,
1529                // we only need to check that they are equal and not keep an extra parameter.
1530                if !self.mir_assign_valid_types(op_src_ty, op_dst_ty) {
1531                    self.fail(location, format!("bad arg ({op_src_ty} != {op_dst_ty})"));
1532                }
1533
1534                let op_cnt_ty = count.ty(&self.body.local_decls, self.tcx);
1535                if op_cnt_ty != self.tcx.types.usize {
1536                    self.fail(location, format!("bad arg ({op_cnt_ty} != usize)"))
1537                }
1538            }
1539            StatementKind::SetDiscriminant { place, .. } => {
1540                if self.body.phase < MirPhase::Runtime(RuntimePhase::Initial) {
1541                    self.fail(location, "`SetDiscriminant`is not allowed until deaggregation");
1542                }
1543                let pty = place.ty(&self.body.local_decls, self.tcx).ty;
1544                if !matches!(
1545                    pty.kind(),
1546                    ty::Adt(..)
1547                        | ty::Coroutine(..)
1548                        | ty::Alias(ty::AliasTy { kind: ty::Opaque { .. }, .. })
1549                ) {
1550                    self.fail(
1551                        location,
1552                        format!(
1553                            "`SetDiscriminant` is only allowed on ADTs and coroutines, not {pty}"
1554                        ),
1555                    );
1556                }
1557            }
1558            StatementKind::StorageLive(_)
1559            | StatementKind::StorageDead(_)
1560            | StatementKind::Coverage(_)
1561            | StatementKind::ConstEvalCounter
1562            | StatementKind::PlaceMention(..)
1563            | StatementKind::BackwardIncompatibleDropHint { .. }
1564            | StatementKind::Nop => {}
1565        }
1566
1567        self.super_statement(statement, location);
1568    }
1569
1570    fn visit_terminator(&mut self, terminator: &Terminator<'tcx>, location: Location) {
1571        match &terminator.kind {
1572            TerminatorKind::SwitchInt { targets, discr } => {
1573                let switch_ty = discr.ty(&self.body.local_decls, self.tcx);
1574
1575                let target_width = self.tcx.sess.target.pointer_width;
1576
1577                let size = Size::from_bits(match switch_ty.kind() {
1578                    ty::Uint(uint) => uint.normalize(target_width).bit_width().unwrap(),
1579                    ty::Int(int) => int.normalize(target_width).bit_width().unwrap(),
1580                    ty::Char => 32,
1581                    ty::Bool => 1,
1582                    other => bug!("unhandled type: {:?}", other),
1583                });
1584
1585                for (value, _) in targets.iter() {
1586                    if ScalarInt::try_from_uint(value, size).is_none() {
1587                        self.fail(
1588                            location,
1589                            format!("the value {value:#x} is not a proper {switch_ty}"),
1590                        )
1591                    }
1592                }
1593            }
1594            TerminatorKind::Call { func, .. } | TerminatorKind::TailCall { func, .. } => {
1595                let func_ty = func.ty(&self.body.local_decls, self.tcx);
1596                match func_ty.kind() {
1597                    ty::FnPtr(..) | ty::FnDef(..) => {}
1598                    _ => self.fail(
1599                        location,
1600                        format!(
1601                            "encountered non-callable type {func_ty} in `{}` terminator",
1602                            terminator.kind.name()
1603                        ),
1604                    ),
1605                }
1606
1607                if let TerminatorKind::TailCall { .. } = terminator.kind {
1608                    // FIXME(explicit_tail_calls): implement tail-call specific checks here (such
1609                    // as signature matching, forbidding closures, etc)
1610                }
1611            }
1612            TerminatorKind::Assert { cond, .. } => {
1613                let cond_ty = cond.ty(&self.body.local_decls, self.tcx);
1614                if cond_ty != self.tcx.types.bool {
1615                    self.fail(
1616                        location,
1617                        format!(
1618                            "encountered non-boolean condition of type {cond_ty} in `Assert` terminator"
1619                        ),
1620                    );
1621                }
1622            }
1623            TerminatorKind::Goto { .. }
1624            | TerminatorKind::Drop { .. }
1625            | TerminatorKind::Yield { .. }
1626            | TerminatorKind::FalseEdge { .. }
1627            | TerminatorKind::FalseUnwind { .. }
1628            | TerminatorKind::InlineAsm { .. }
1629            | TerminatorKind::CoroutineDrop
1630            | TerminatorKind::UnwindResume
1631            | TerminatorKind::UnwindTerminate(_)
1632            | TerminatorKind::Return
1633            | TerminatorKind::Unreachable => {}
1634        }
1635
1636        self.super_terminator(terminator, location);
1637    }
1638
1639    fn visit_local_decl(&mut self, local: Local, local_decl: &LocalDecl<'tcx>) {
1640        if let ClearCrossCrate::Set(LocalInfo::DerefTemp) = local_decl.local_info {
1641            if self.body.phase >= MirPhase::Runtime(RuntimePhase::Initial) {
1642                self.fail(
1643                    START_BLOCK.start_location(),
1644                    "`DerefTemp` should have been removed in runtime MIR",
1645                );
1646            } else if local_decl.ty.builtin_deref(true).is_none() {
1647                self.fail(
1648                    START_BLOCK.start_location(),
1649                    "`DerefTemp` should only be used for dereferenceable types",
1650                )
1651            }
1652        }
1653
1654        self.super_local_decl(local, local_decl);
1655    }
1656}
1657
1658pub(super) fn validate_debuginfos<'tcx>(body: &Body<'tcx>) -> Vec<(Location, String)> {
1659    let mut debuginfo_checker =
1660        DebuginfoChecker { debuginfo_locals: debuginfo_locals(body), failures: Vec::new() };
1661    debuginfo_checker.visit_body(body);
1662    debuginfo_checker.failures
1663}
1664
1665struct DebuginfoChecker {
1666    debuginfo_locals: DenseBitSet<Local>,
1667    failures: Vec<(Location, String)>,
1668}
1669
1670impl<'tcx> Visitor<'tcx> for DebuginfoChecker {
1671    fn visit_statement_debuginfo(
1672        &mut self,
1673        stmt_debuginfo: &StmtDebugInfo<'tcx>,
1674        location: Location,
1675    ) {
1676        let local = match stmt_debuginfo {
1677            StmtDebugInfo::AssignRef(local, _) | StmtDebugInfo::InvalidAssign(local) => *local,
1678        };
1679        if !self.debuginfo_locals.contains(local) {
1680            self.failures.push((location, format!("{local:?} is not in debuginfo")));
1681        }
1682    }
1683}