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                    if self.body.phase >= MirPhase::Runtime(RuntimePhase::Initial) {
371                        self.fail(
372                            location,
373                            "`async drop` should have been removed after drop elaboration",
374                        );
375                    }
376                }
377            }
378            TerminatorKind::Call { func, args, .. }
379            | TerminatorKind::TailCall { func, args, .. } => {
380                // FIXME(explicit_tail_calls): refactor this & add tail-call specific checks
381                if let TerminatorKind::Call { target, unwind, destination, .. } = terminator.kind {
382                    if let Some(target) = target {
383                        self.check_edge(location, target, EdgeKind::Normal);
384                    }
385                    self.check_unwind_edge(location, unwind);
386
387                    // The code generation assumes that there are no critical call edges. The
388                    // assumption is used to simplify inserting code that should be executed along
389                    // the return edge from the call. FIXME(tmiasko): Since this is a strictly code
390                    // generation concern, the code generation should be responsible for handling
391                    // it.
392                    if self.body.phase >= MirPhase::Runtime(RuntimePhase::Optimized)
393                        && self.is_critical_call_edge(target, unwind)
394                    {
395                        self.fail(
396                            location,
397                            format!(
398                                "encountered critical edge in `Call` terminator {:?}",
399                                terminator.kind,
400                            ),
401                        );
402                    }
403
404                    // The call destination place and Operand::Move place used as an argument might
405                    // be passed by a reference to the callee. Consequently they cannot be packed.
406                    if most_packed_projection(self.tcx, &self.body.local_decls, destination)
407                        .is_some()
408                    {
409                        // This is bad! The callee will expect the memory to be aligned.
410                        self.fail(
411                            location,
412                            format!(
413                                "encountered packed place in `Call` terminator destination: {:?}",
414                                terminator.kind,
415                            ),
416                        );
417                    }
418                }
419
420                for arg in args {
421                    if let Operand::Move(place) = &arg.node {
422                        if most_packed_projection(self.tcx, &self.body.local_decls, *place)
423                            .is_some()
424                        {
425                            // This is bad! The callee will expect the memory to be aligned.
426                            self.fail(
427                                location,
428                                format!(
429                                    "encountered `Move` of a packed place in `Call` terminator: {:?}",
430                                    terminator.kind,
431                                ),
432                            );
433                        }
434                    }
435                }
436
437                if let ty::FnDef(did, ..) = *func.ty(&self.body.local_decls, self.tcx).kind()
438                    && self.body.phase >= MirPhase::Runtime(RuntimePhase::Optimized)
439                    && matches!(self.tcx.codegen_fn_attrs(did).inline, InlineAttr::Force { .. })
440                {
441                    self.fail(location, "`#[rustc_force_inline]`-annotated function not inlined");
442                }
443            }
444            TerminatorKind::Assert { target, unwind, .. } => {
445                self.check_edge(location, *target, EdgeKind::Normal);
446                self.check_unwind_edge(location, *unwind);
447            }
448            TerminatorKind::Yield { resume, drop, .. } => {
449                if self.body.coroutine.is_none() {
450                    self.fail(location, "`Yield` cannot appear outside coroutine bodies");
451                }
452                if self.body.phase >= MirPhase::Runtime(RuntimePhase::Initial) {
453                    self.fail(location, "`Yield` should have been replaced by coroutine lowering");
454                }
455                self.check_edge(location, *resume, EdgeKind::Normal);
456                if let Some(drop) = drop {
457                    self.check_edge(location, *drop, EdgeKind::Normal);
458                }
459            }
460            TerminatorKind::FalseEdge { real_target, imaginary_target } => {
461                if self.body.phase >= MirPhase::Runtime(RuntimePhase::Initial) {
462                    self.fail(
463                        location,
464                        "`FalseEdge` should have been removed after drop elaboration",
465                    );
466                }
467                self.check_edge(location, *real_target, EdgeKind::Normal);
468                self.check_edge(location, *imaginary_target, EdgeKind::Normal);
469            }
470            TerminatorKind::FalseUnwind { real_target, unwind } => {
471                if self.body.phase >= MirPhase::Runtime(RuntimePhase::Initial) {
472                    self.fail(
473                        location,
474                        "`FalseUnwind` should have been removed after drop elaboration",
475                    );
476                }
477                self.check_edge(location, *real_target, EdgeKind::Normal);
478                self.check_unwind_edge(location, *unwind);
479            }
480            TerminatorKind::InlineAsm { targets, unwind, .. } => {
481                for &target in targets {
482                    self.check_edge(location, target, EdgeKind::Normal);
483                }
484                self.check_unwind_edge(location, *unwind);
485            }
486            TerminatorKind::CoroutineDrop => {
487                if self.body.coroutine.is_none() {
488                    self.fail(location, "`CoroutineDrop` cannot appear outside coroutine bodies");
489                }
490                if self.body.phase >= MirPhase::Runtime(RuntimePhase::Initial) {
491                    self.fail(
492                        location,
493                        "`CoroutineDrop` should have been replaced by coroutine lowering",
494                    );
495                }
496            }
497            TerminatorKind::UnwindResume => {
498                let bb = location.block;
499                if !self.body.basic_blocks[bb].is_cleanup {
500                    self.fail(location, "Cannot `UnwindResume` from non-cleanup basic block")
501                }
502                if !self.can_unwind {
503                    self.fail(location, "Cannot `UnwindResume` in a function that cannot unwind")
504                }
505            }
506            TerminatorKind::UnwindTerminate(_) => {
507                let bb = location.block;
508                if !self.body.basic_blocks[bb].is_cleanup {
509                    self.fail(location, "Cannot `UnwindTerminate` from non-cleanup basic block")
510                }
511            }
512            TerminatorKind::Return => {
513                let bb = location.block;
514                if self.body.basic_blocks[bb].is_cleanup {
515                    self.fail(location, "Cannot `Return` from cleanup basic block")
516                }
517            }
518            TerminatorKind::Unreachable => {}
519        }
520
521        self.super_terminator(terminator, location);
522    }
523
524    fn visit_source_scope(&mut self, scope: SourceScope) {
525        if self.body.source_scopes.get(scope).is_none() {
526            self.tcx.dcx().span_bug(
527                self.body.span,
528                format!(
529                    "broken MIR in {:?} ({}):\ninvalid source scope {:?}",
530                    self.body.source.instance, self.when, scope,
531                ),
532            );
533        }
534    }
535}
536
537/// A faster version of the validation pass that only checks those things which may break when
538/// instantiating any generic parameters.
539///
540/// `caller_body` is used to detect cycles in MIR inlining and MIR validation before
541/// `optimized_mir` is available.
542pub(super) fn validate_types<'tcx>(
543    tcx: TyCtxt<'tcx>,
544    typing_env: ty::TypingEnv<'tcx>,
545    body: &Body<'tcx>,
546    caller_body: &Body<'tcx>,
547) -> Vec<(Location, String)> {
548    let mut type_checker = TypeChecker { body, caller_body, tcx, typing_env, failures: Vec::new() };
549    // The type checker formats a bunch of strings with type names in it, but these strings
550    // are not always going to be encountered on the error path since the inliner also uses
551    // the validator, and there are certain kinds of inlining (even for valid code) that
552    // can cause validation errors (mostly around where clauses and rigid projections).
553    with_no_trimmed_paths!({
554        type_checker.visit_body(body);
555    });
556    type_checker.failures
557}
558
559struct TypeChecker<'a, 'tcx> {
560    body: &'a Body<'tcx>,
561    caller_body: &'a Body<'tcx>,
562    tcx: TyCtxt<'tcx>,
563    typing_env: ty::TypingEnv<'tcx>,
564    failures: Vec<(Location, String)>,
565}
566
567impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
568    fn fail(&mut self, location: Location, msg: impl Into<String>) {
569        self.failures.push((location, msg.into()));
570    }
571
572    /// Check if src can be assigned into dest.
573    /// This is not precise, it will accept some incorrect assignments.
574    fn mir_assign_valid_types(&self, src: Ty<'tcx>, dest: Ty<'tcx>) -> bool {
575        // Fast path before we normalize.
576        if src == dest {
577            // Equal types, all is good.
578            return true;
579        }
580
581        // We sometimes have to use `defining_opaque_types` for subtyping
582        // to succeed here and figuring out how exactly that should work
583        // is annoying. It is harmless enough to just not validate anything
584        // in that case. We still check this after analysis as all opaque
585        // types have been revealed at this point.
586        if (src, dest).has_opaque_types() {
587            return true;
588        }
589
590        // After borrowck subtyping should be fully explicit via
591        // `Subtype` projections.
592        let variance = if self.body.phase >= MirPhase::Runtime(RuntimePhase::Initial) {
593            Variance::Invariant
594        } else {
595            Variance::Covariant
596        };
597
598        crate::util::relate_types(self.tcx, self.typing_env, variance, src, dest)
599    }
600
601    /// Check that the given predicate definitely holds in the param-env of this MIR body.
602    fn predicate_must_hold_modulo_regions(
603        &self,
604        pred: impl Upcast<TyCtxt<'tcx>, ty::Predicate<'tcx>>,
605    ) -> bool {
606        let pred: ty::Predicate<'tcx> = pred.upcast(self.tcx);
607
608        // We sometimes have to use `defining_opaque_types` for predicates
609        // to succeed here and figuring out how exactly that should work
610        // is annoying. It is harmless enough to just not validate anything
611        // in that case. We still check this after analysis as all opaque
612        // types have been revealed at this point.
613        if pred.has_opaque_types() {
614            return true;
615        }
616
617        let (infcx, param_env) = self.tcx.infer_ctxt().build_with_typing_env(self.typing_env);
618        let ocx = ObligationCtxt::new(&infcx);
619        ocx.register_obligation(Obligation::new(
620            self.tcx,
621            ObligationCause::dummy(),
622            param_env,
623            pred,
624        ));
625        ocx.evaluate_obligations_error_on_ambiguity().is_empty()
626    }
627}
628
629impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
630    fn visit_operand(&mut self, operand: &Operand<'tcx>, location: Location) {
631        // This check is somewhat expensive, so only run it when -Zvalidate-mir is passed.
632        if self.tcx.sess.opts.unstable_opts.validate_mir
633            && self.body.phase < MirPhase::Runtime(RuntimePhase::Initial)
634        {
635            // `Operand::Copy` is only supposed to be used with `Copy` types.
636            if let Operand::Copy(place) = operand {
637                let ty = place.ty(&self.body.local_decls, self.tcx).ty;
638
639                if !self.tcx.type_is_copy_modulo_regions(self.typing_env, ty) {
640                    self.fail(location, format!("`Operand::Copy` with non-`Copy` type {ty}"));
641                }
642            }
643        }
644
645        self.super_operand(operand, location);
646    }
647
648    fn visit_projection_elem(
649        &mut self,
650        place_ref: PlaceRef<'tcx>,
651        elem: PlaceElem<'tcx>,
652        context: PlaceContext,
653        location: Location,
654    ) {
655        match elem {
656            ProjectionElem::Deref
657                if self.body.phase >= MirPhase::Runtime(RuntimePhase::Initial) =>
658            {
659                let base_ty = place_ref.ty(&self.body.local_decls, self.tcx).ty;
660
661                if base_ty.is_box() {
662                    self.fail(location, format!("{base_ty} dereferenced after ElaborateBoxDerefs"))
663                }
664            }
665            ProjectionElem::Field(f, ty) => {
666                let parent_ty = place_ref.ty(&self.body.local_decls, self.tcx);
667                let fail_out_of_bounds = |this: &mut Self, location| {
668                    this.fail(location, format!("Out of bounds field {f:?} for {parent_ty:?}"));
669                };
670                let check_equal = |this: &mut Self, location, f_ty| {
671                    if !this.mir_assign_valid_types(ty, f_ty) {
672                        this.fail(
673                            location,
674                            format!(
675                                "Field projection `{place_ref:?}.{f:?}` specified type `{ty}`, but actual type is `{f_ty}`"
676                            )
677                        )
678                    }
679                };
680
681                let kind = match parent_ty.ty.kind() {
682                    &ty::Alias(ty::AliasTy { kind: ty::Opaque { def_id }, args, .. }) => {
683                        self.tcx.type_of(def_id).instantiate(self.tcx, args).skip_norm_wip().kind()
684                    }
685                    kind => kind,
686                };
687
688                match kind {
689                    ty::Tuple(fields) => {
690                        let Some(f_ty) = fields.get(f.as_usize()) else {
691                            fail_out_of_bounds(self, location);
692                            return;
693                        };
694                        check_equal(self, location, *f_ty);
695                    }
696                    // Debug info is allowed to project into pattern types
697                    ty::Pat(base, _) => check_equal(self, location, *base),
698                    ty::Adt(adt_def, args) => {
699                        // see <https://github.com/rust-lang/rust/blob/7601adcc764d42c9f2984082b49948af652df986/compiler/rustc_middle/src/ty/layout.rs#L861-L864>
700                        if self.tcx.is_lang_item(adt_def.did(), LangItem::DynMetadata) {
701                            self.fail(
702                                location,
703                                format!(
704                                    "You can't project to field {f:?} of `DynMetadata` because \
705                                     layout is weird and thinks it doesn't have fields."
706                                ),
707                            );
708                        }
709
710                        if adt_def.repr().simd() {
711                            self.fail(
712                                location,
713                                format!(
714                                    "Projecting into SIMD type {adt_def:?} is banned by MCP#838"
715                                ),
716                            );
717                        }
718
719                        let var = parent_ty.variant_index.unwrap_or(FIRST_VARIANT);
720                        let Some(field) = adt_def.variant(var).fields.get(f) else {
721                            fail_out_of_bounds(self, location);
722                            return;
723                        };
724                        check_equal(self, location, field.ty(self.tcx, args).skip_norm_wip());
725                    }
726                    ty::Closure(_, args) => {
727                        let args = args.as_closure();
728                        let Some(&f_ty) = args.upvar_tys().get(f.as_usize()) else {
729                            fail_out_of_bounds(self, location);
730                            return;
731                        };
732                        check_equal(self, location, f_ty);
733                    }
734                    ty::CoroutineClosure(_, args) => {
735                        let args = args.as_coroutine_closure();
736                        let Some(&f_ty) = args.upvar_tys().get(f.as_usize()) else {
737                            fail_out_of_bounds(self, location);
738                            return;
739                        };
740                        check_equal(self, location, f_ty);
741                    }
742                    &ty::Coroutine(def_id, args) => {
743                        let f_ty = if let Some(var) = parent_ty.variant_index {
744                            // If we're currently validating an inlined copy of this body,
745                            // then it will no longer be parameterized over the original
746                            // args of the coroutine. Otherwise, we prefer to use this body
747                            // since we may be in the process of computing this MIR in the
748                            // first place.
749                            let layout = if def_id == self.caller_body.source.def_id() {
750                                self.caller_body
751                                    .coroutine_layout_raw()
752                                    .or_else(|| self.tcx.coroutine_layout(def_id, args).ok())
753                            } else if self.tcx.needs_coroutine_by_move_body_def_id(def_id)
754                                && let ty::ClosureKind::FnOnce =
755                                    args.as_coroutine().kind_ty().to_opt_closure_kind().unwrap()
756                                && self.caller_body.source.def_id()
757                                    == self.tcx.coroutine_by_move_body_def_id(def_id)
758                            {
759                                // Same if this is the by-move body of a coroutine-closure.
760                                self.caller_body.coroutine_layout_raw()
761                            } else {
762                                self.tcx.coroutine_layout(def_id, args).ok()
763                            };
764
765                            let Some(layout) = layout else {
766                                self.fail(
767                                    location,
768                                    format!("No coroutine layout for {parent_ty:?}"),
769                                );
770                                return;
771                            };
772
773                            let Some(&local) = layout.variant_fields[var].get(f) else {
774                                fail_out_of_bounds(self, location);
775                                return;
776                            };
777
778                            let Some(f_ty) = layout.field_tys.get(local) else {
779                                self.fail(
780                                    location,
781                                    format!("Out of bounds local {local:?} for {parent_ty:?}"),
782                                );
783                                return;
784                            };
785
786                            ty::EarlyBinder::bind(f_ty.ty)
787                                .instantiate(self.tcx, args)
788                                .skip_norm_wip()
789                        } else {
790                            let Some(&f_ty) = args.as_coroutine().prefix_tys().get(f.index())
791                            else {
792                                fail_out_of_bounds(self, location);
793                                return;
794                            };
795
796                            f_ty
797                        };
798
799                        check_equal(self, location, f_ty);
800                    }
801                    _ => {
802                        self.fail(location, format!("{:?} does not have fields", parent_ty.ty));
803                    }
804                }
805            }
806            ProjectionElem::Index(index) => {
807                let indexed_ty = place_ref.ty(&self.body.local_decls, self.tcx).ty;
808                match indexed_ty.kind() {
809                    ty::Array(_, _) | ty::Slice(_) => {}
810                    _ => self.fail(location, format!("{indexed_ty:?} cannot be indexed")),
811                }
812
813                let index_ty = self.body.local_decls[index].ty;
814                if index_ty != self.tcx.types.usize {
815                    self.fail(location, format!("bad index ({index_ty} != usize)"))
816                }
817            }
818            ProjectionElem::ConstantIndex { offset, min_length, from_end } => {
819                let indexed_ty = place_ref.ty(&self.body.local_decls, self.tcx).ty;
820                match indexed_ty.kind() {
821                    ty::Array(_, _) => {
822                        if from_end {
823                            self.fail(location, "arrays should not be indexed from end");
824                        }
825                    }
826                    ty::Slice(_) => {}
827                    _ => self.fail(location, format!("{indexed_ty:?} cannot be indexed")),
828                }
829
830                if from_end {
831                    if offset > min_length {
832                        self.fail(
833                            location,
834                            format!(
835                                "constant index with offset -{offset} out of bounds of min length {min_length}"
836                            ),
837                        );
838                    }
839                } else {
840                    if offset >= min_length {
841                        self.fail(
842                            location,
843                            format!(
844                                "constant index with offset {offset} out of bounds of min length {min_length}"
845                            ),
846                        );
847                    }
848                }
849            }
850            ProjectionElem::Subslice { from, to, from_end } => {
851                let indexed_ty = place_ref.ty(&self.body.local_decls, self.tcx).ty;
852                match indexed_ty.kind() {
853                    ty::Array(_, _) => {
854                        if from_end {
855                            self.fail(location, "arrays should not be subsliced from end");
856                        }
857                    }
858                    ty::Slice(_) => {
859                        if !from_end {
860                            self.fail(location, "slices should be subsliced from end");
861                        }
862                    }
863                    _ => self.fail(location, format!("{indexed_ty:?} cannot be indexed")),
864                }
865
866                if !from_end && from > to {
867                    self.fail(location, "backwards subslice {from}..{to}");
868                }
869            }
870            ProjectionElem::OpaqueCast(ty)
871                if self.body.phase >= MirPhase::Runtime(RuntimePhase::Initial) =>
872            {
873                self.fail(
874                    location,
875                    format!("explicit opaque type cast to `{ty}` after `PostAnalysisNormalize`"),
876                )
877            }
878            ProjectionElem::UnwrapUnsafeBinder(unwrapped_ty) => {
879                let binder_ty = place_ref.ty(&self.body.local_decls, self.tcx);
880                let ty::UnsafeBinder(binder_ty) = *binder_ty.ty.kind() else {
881                    self.fail(
882                        location,
883                        format!("WrapUnsafeBinder does not produce a ty::UnsafeBinder"),
884                    );
885                    return;
886                };
887                let binder_inner_ty = self.tcx.instantiate_bound_regions_with_erased(*binder_ty);
888                if !self.mir_assign_valid_types(unwrapped_ty, binder_inner_ty) {
889                    self.fail(
890                        location,
891                        format!(
892                            "Cannot unwrap unsafe binder {binder_ty:?} into type {unwrapped_ty}"
893                        ),
894                    );
895                }
896            }
897            _ => {}
898        }
899        self.super_projection_elem(place_ref, elem, context, location);
900    }
901
902    fn visit_var_debug_info(&mut self, debuginfo: &VarDebugInfo<'tcx>) {
903        if let Some(VarDebugInfoFragment { ty, ref projection }) = debuginfo.composite {
904            if ty.is_union() || ty.is_enum() {
905                self.fail(
906                    START_BLOCK.start_location(),
907                    format!("invalid type {ty} in debuginfo for {:?}", debuginfo.name),
908                );
909            }
910            if projection.is_empty() {
911                self.fail(
912                    START_BLOCK.start_location(),
913                    format!("invalid empty projection in debuginfo for {:?}", debuginfo.name),
914                );
915            }
916            if projection.iter().any(|p| !matches!(p, PlaceElem::Field(..))) {
917                self.fail(
918                    START_BLOCK.start_location(),
919                    format!(
920                        "illegal projection {:?} in debuginfo for {:?}",
921                        projection, debuginfo.name
922                    ),
923                );
924            }
925        }
926        match debuginfo.value {
927            VarDebugInfoContents::Const(_) => {}
928            VarDebugInfoContents::Place(place) => {
929                if place.projection.iter().any(|p| !p.can_use_in_debuginfo()) {
930                    self.fail(
931                        START_BLOCK.start_location(),
932                        format!("illegal place {:?} in debuginfo for {:?}", place, debuginfo.name),
933                    );
934                }
935            }
936        }
937        self.super_var_debug_info(debuginfo);
938    }
939
940    fn visit_place(&mut self, place: &Place<'tcx>, cntxt: PlaceContext, location: Location) {
941        // Set off any `bug!`s in the type computation code
942        let _ = place.ty(&self.body.local_decls, self.tcx);
943
944        if self.body.phase >= MirPhase::Runtime(RuntimePhase::Initial)
945            && place.projection.len() > 1
946            && cntxt != PlaceContext::NonUse(NonUseContext::VarDebugInfo)
947            && place.projection[1..].contains(&ProjectionElem::Deref)
948        {
949            self.fail(
950                location,
951                format!("place {place:?} has deref as a later projection (it is only permitted as the first projection)"),
952            );
953        }
954
955        // Ensure all downcast projections are followed by field projections.
956        let mut projections_iter = place.projection.iter();
957        while let Some(proj) = projections_iter.next() {
958            if matches!(proj, ProjectionElem::Downcast(..)) {
959                if !matches!(projections_iter.next(), Some(ProjectionElem::Field(..))) {
960                    self.fail(
961                        location,
962                        format!(
963                            "place {place:?} has `Downcast` projection not followed by `Field`"
964                        ),
965                    );
966                }
967            }
968        }
969
970        if let ClearCrossCrate::Set(LocalInfo::DerefTemp) =
971            self.body.local_decls[place.local].local_info
972            && !place.is_indirect_first_projection()
973        {
974            if cntxt != PlaceContext::MutatingUse(MutatingUseContext::Store)
975                || place.as_local().is_none()
976            {
977                self.fail(
978                    location,
979                    format!("`DerefTemp` locals must only be dereferenced or directly assigned to"),
980                );
981            }
982        }
983
984        if self.body.phase < MirPhase::Runtime(RuntimePhase::Initial)
985            && let Some(i) = place
986                .projection
987                .iter()
988                .position(|elem| matches!(elem, ProjectionElem::Subslice { .. }))
989            && let Some(tail) = place.projection.get(i + 1..)
990            && tail.iter().any(|elem| {
991                matches!(
992                    elem,
993                    ProjectionElem::ConstantIndex { .. } | ProjectionElem::Subslice { .. }
994                )
995            })
996        {
997            self.fail(
998                location,
999                format!("place {place:?} has `ConstantIndex` or `Subslice` after `Subslice`"),
1000            );
1001        }
1002
1003        self.super_place(place, cntxt, location);
1004    }
1005
1006    fn visit_rvalue(&mut self, rvalue: &Rvalue<'tcx>, location: Location) {
1007        macro_rules! check_kinds {
1008            ($t:expr, $text:literal, $typat:pat) => {
1009                if !matches!(($t).kind(), $typat) {
1010                    self.fail(location, format!($text, $t));
1011                }
1012            };
1013        }
1014        match rvalue {
1015            Rvalue::Use(_, _) => {}
1016            Rvalue::CopyForDeref(_) => {
1017                if self.body.phase >= MirPhase::Runtime(RuntimePhase::Initial) {
1018                    self.fail(location, "`CopyForDeref` should have been removed in runtime MIR");
1019                }
1020            }
1021            Rvalue::Aggregate(kind, fields) => match **kind {
1022                AggregateKind::Tuple => {}
1023                AggregateKind::Array(dest) => {
1024                    for src in fields {
1025                        if !self.mir_assign_valid_types(src.ty(self.body, self.tcx), dest) {
1026                            self.fail(location, "array field has the wrong type");
1027                        }
1028                    }
1029                }
1030                AggregateKind::Adt(def_id, idx, args, _, Some(field)) => {
1031                    let adt_def = self.tcx.adt_def(def_id);
1032                    assert!(adt_def.is_union());
1033                    assert_eq!(idx, FIRST_VARIANT);
1034                    let dest_ty = self.tcx.normalize_erasing_regions(
1035                        self.typing_env,
1036                        adt_def.non_enum_variant().fields[field].ty(self.tcx, args),
1037                    );
1038                    if let [field] = fields.raw.as_slice() {
1039                        let src_ty = field.ty(self.body, self.tcx);
1040                        if !self.mir_assign_valid_types(src_ty, dest_ty) {
1041                            self.fail(location, "union field has the wrong type");
1042                        }
1043                    } else {
1044                        self.fail(location, "unions should have one initialized field");
1045                    }
1046                }
1047                AggregateKind::Adt(def_id, idx, args, _, None) => {
1048                    let adt_def = self.tcx.adt_def(def_id);
1049                    assert!(!adt_def.is_union());
1050                    let variant = &adt_def.variants()[idx];
1051                    if variant.fields.len() != fields.len() {
1052                        self.fail(location, format!(
1053                            "adt {def_id:?} has the wrong number of initialized fields, expected {}, found {}",
1054                            fields.len(),
1055                            variant.fields.len(),
1056                        ));
1057                    }
1058                    for (src, dest) in std::iter::zip(fields, &variant.fields) {
1059                        let dest_ty = self
1060                            .tcx
1061                            .normalize_erasing_regions(self.typing_env, dest.ty(self.tcx, args));
1062                        if !self.mir_assign_valid_types(src.ty(self.body, self.tcx), dest_ty) {
1063                            self.fail(location, "adt field has the wrong type");
1064                        }
1065                    }
1066                }
1067                AggregateKind::Closure(_, args) => {
1068                    let upvars = args.as_closure().upvar_tys();
1069                    if upvars.len() != fields.len() {
1070                        self.fail(location, "closure has the wrong number of initialized fields");
1071                    }
1072                    for (src, dest) in std::iter::zip(fields, upvars) {
1073                        if !self.mir_assign_valid_types(src.ty(self.body, self.tcx), dest) {
1074                            self.fail(location, "closure field has the wrong type");
1075                        }
1076                    }
1077                }
1078                AggregateKind::Coroutine(_, args) => {
1079                    let upvars = args.as_coroutine().upvar_tys();
1080                    if upvars.len() != fields.len() {
1081                        self.fail(location, "coroutine has the wrong number of initialized fields");
1082                    }
1083                    for (src, dest) in std::iter::zip(fields, upvars) {
1084                        if !self.mir_assign_valid_types(src.ty(self.body, self.tcx), dest) {
1085                            self.fail(location, "coroutine field has the wrong type");
1086                        }
1087                    }
1088                }
1089                AggregateKind::CoroutineClosure(_, args) => {
1090                    let upvars = args.as_coroutine_closure().upvar_tys();
1091                    if upvars.len() != fields.len() {
1092                        self.fail(
1093                            location,
1094                            "coroutine-closure has the wrong number of initialized fields",
1095                        );
1096                    }
1097                    for (src, dest) in std::iter::zip(fields, upvars) {
1098                        if !self.mir_assign_valid_types(src.ty(self.body, self.tcx), dest) {
1099                            self.fail(location, "coroutine-closure field has the wrong type");
1100                        }
1101                    }
1102                }
1103                AggregateKind::RawPtr(pointee_ty, mutability) => {
1104                    if !matches!(self.body.phase, MirPhase::Runtime(_)) {
1105                        // It would probably be fine to support this in earlier phases, but at the
1106                        // time of writing it's only ever introduced from intrinsic lowering, so
1107                        // earlier things just `bug!` on it.
1108                        self.fail(location, "RawPtr should be in runtime MIR only");
1109                    }
1110
1111                    if let [data_ptr, metadata] = fields.raw.as_slice() {
1112                        let data_ptr_ty = data_ptr.ty(self.body, self.tcx);
1113                        let metadata_ty = metadata.ty(self.body, self.tcx);
1114                        if let ty::RawPtr(in_pointee, in_mut) = data_ptr_ty.kind() {
1115                            if *in_mut != mutability {
1116                                self.fail(location, "input and output mutability must match");
1117                            }
1118
1119                            // FIXME: check `Thin` instead of `Sized`
1120                            if !in_pointee.is_sized(self.tcx, self.typing_env) {
1121                                self.fail(location, "input pointer must be thin");
1122                            }
1123                        } else {
1124                            self.fail(
1125                                location,
1126                                "first operand to raw pointer aggregate must be a raw pointer",
1127                            );
1128                        }
1129
1130                        // FIXME: Check metadata more generally
1131                        if pointee_ty.is_slice() {
1132                            if !self.mir_assign_valid_types(metadata_ty, self.tcx.types.usize) {
1133                                self.fail(location, "slice metadata must be usize");
1134                            }
1135                        } else if pointee_ty.is_sized(self.tcx, self.typing_env) {
1136                            if metadata_ty != self.tcx.types.unit {
1137                                self.fail(location, "metadata for pointer-to-thin must be unit");
1138                            }
1139                        }
1140                    } else {
1141                        self.fail(location, "raw pointer aggregate must have 2 fields");
1142                    }
1143                }
1144            },
1145            Rvalue::Ref(_, BorrowKind::Fake(_), _) => {
1146                if self.body.phase >= MirPhase::Runtime(RuntimePhase::Initial) {
1147                    self.fail(
1148                        location,
1149                        "`Assign` statement with a `Fake` borrow should have been removed in runtime MIR",
1150                    );
1151                }
1152            }
1153            Rvalue::Ref(..) | Rvalue::Reborrow(..) => {}
1154            Rvalue::BinaryOp(op, vals) => {
1155                use BinOp::*;
1156                let a = vals.0.ty(&self.body.local_decls, self.tcx);
1157                let b = vals.1.ty(&self.body.local_decls, self.tcx);
1158                if crate::util::binop_right_homogeneous(*op) {
1159                    if let Eq | Lt | Le | Ne | Ge | Gt = op {
1160                        // The function pointer types can have lifetimes
1161                        if !self.mir_assign_valid_types(a, b) {
1162                            self.fail(
1163                                location,
1164                                format!("Cannot {op:?} compare incompatible types {a} and {b}"),
1165                            );
1166                        }
1167                    } else if a != b {
1168                        self.fail(
1169                            location,
1170                            format!("Cannot perform binary op {op:?} on unequal types {a} and {b}"),
1171                        );
1172                    }
1173                }
1174
1175                match op {
1176                    Offset => {
1177                        check_kinds!(a, "Cannot offset non-pointer type {:?}", ty::RawPtr(..));
1178                        if b != self.tcx.types.isize && b != self.tcx.types.usize {
1179                            self.fail(location, format!("Cannot offset by non-isize type {b}"));
1180                        }
1181                    }
1182                    Eq | Lt | Le | Ne | Ge | Gt => {
1183                        for x in [a, b] {
1184                            check_kinds!(
1185                                x,
1186                                "Cannot {op:?} compare type {:?}",
1187                                ty::Bool
1188                                    | ty::Char
1189                                    | ty::Int(..)
1190                                    | ty::Uint(..)
1191                                    | ty::Float(..)
1192                                    | ty::RawPtr(..)
1193                                    | ty::FnPtr(..)
1194                            )
1195                        }
1196                    }
1197                    Cmp => {
1198                        for x in [a, b] {
1199                            check_kinds!(
1200                                x,
1201                                "Cannot three-way compare non-integer type {:?}",
1202                                ty::Char | ty::Uint(..) | ty::Int(..)
1203                            )
1204                        }
1205                    }
1206                    AddUnchecked | AddWithOverflow | SubUnchecked | SubWithOverflow
1207                    | MulUnchecked | MulWithOverflow | Shl | ShlUnchecked | Shr | ShrUnchecked => {
1208                        for x in [a, b] {
1209                            check_kinds!(
1210                                x,
1211                                "Cannot {op:?} non-integer type {:?}",
1212                                ty::Uint(..) | ty::Int(..)
1213                            )
1214                        }
1215                    }
1216                    BitAnd | BitOr | BitXor => {
1217                        for x in [a, b] {
1218                            check_kinds!(
1219                                x,
1220                                "Cannot perform bitwise op {op:?} on type {:?}",
1221                                ty::Uint(..) | ty::Int(..) | ty::Bool
1222                            )
1223                        }
1224                    }
1225                    Add | Sub | Mul | Div | Rem => {
1226                        for x in [a, b] {
1227                            check_kinds!(
1228                                x,
1229                                "Cannot perform arithmetic {op:?} on type {:?}",
1230                                ty::Uint(..) | ty::Int(..) | ty::Float(..)
1231                            )
1232                        }
1233                    }
1234                }
1235            }
1236            Rvalue::UnaryOp(op, operand) => {
1237                let a = operand.ty(&self.body.local_decls, self.tcx);
1238                match op {
1239                    UnOp::Neg => {
1240                        check_kinds!(a, "Cannot negate type {:?}", ty::Int(..) | ty::Float(..))
1241                    }
1242                    UnOp::Not => {
1243                        check_kinds!(
1244                            a,
1245                            "Cannot binary not type {:?}",
1246                            ty::Int(..) | ty::Uint(..) | ty::Bool
1247                        );
1248                    }
1249                    UnOp::PtrMetadata => {
1250                        check_kinds!(
1251                            a,
1252                            "Cannot PtrMetadata non-pointer non-reference type {:?}",
1253                            ty::RawPtr(..) | ty::Ref(..)
1254                        );
1255                    }
1256                }
1257            }
1258            Rvalue::Cast(kind, operand, target_type) => {
1259                let op_ty = operand.ty(self.body, self.tcx);
1260                match kind {
1261                    // FIXME: Add Checks for these
1262                    CastKind::PointerWithExposedProvenance | CastKind::PointerExposeProvenance => {}
1263                    CastKind::PointerCoercion(PointerCoercion::ReifyFnPointer(_), _) => {
1264                        // FIXME: check signature compatibility.
1265                        check_kinds!(
1266                            op_ty,
1267                            "CastKind::{kind:?} input must be a fn item, not {:?}",
1268                            ty::FnDef(..)
1269                        );
1270                        check_kinds!(
1271                            target_type,
1272                            "CastKind::{kind:?} output must be a fn pointer, not {:?}",
1273                            ty::FnPtr(..)
1274                        );
1275                    }
1276                    CastKind::PointerCoercion(PointerCoercion::UnsafeFnPointer, _) => {
1277                        // FIXME: check safety and signature compatibility.
1278                        check_kinds!(
1279                            op_ty,
1280                            "CastKind::{kind:?} input must be a fn pointer, not {:?}",
1281                            ty::FnPtr(..)
1282                        );
1283                        check_kinds!(
1284                            target_type,
1285                            "CastKind::{kind:?} output must be a fn pointer, not {:?}",
1286                            ty::FnPtr(..)
1287                        );
1288                    }
1289                    CastKind::PointerCoercion(PointerCoercion::ClosureFnPointer(..), _) => {
1290                        // FIXME: check safety, captures, and signature compatibility.
1291                        check_kinds!(
1292                            op_ty,
1293                            "CastKind::{kind:?} input must be a closure, not {:?}",
1294                            ty::Closure(..)
1295                        );
1296                        check_kinds!(
1297                            target_type,
1298                            "CastKind::{kind:?} output must be a fn pointer, not {:?}",
1299                            ty::FnPtr(..)
1300                        );
1301                    }
1302                    CastKind::PointerCoercion(PointerCoercion::MutToConstPointer, _) => {
1303                        // FIXME: check same pointee?
1304                        check_kinds!(
1305                            op_ty,
1306                            "CastKind::{kind:?} input must be a raw mut pointer, not {:?}",
1307                            ty::RawPtr(_, Mutability::Mut)
1308                        );
1309                        check_kinds!(
1310                            target_type,
1311                            "CastKind::{kind:?} output must be a raw const pointer, not {:?}",
1312                            ty::RawPtr(_, Mutability::Not)
1313                        );
1314                        if self.body.phase >= MirPhase::Analysis(AnalysisPhase::PostCleanup) {
1315                            self.fail(location, format!("After borrowck, MIR disallows {kind:?}"));
1316                        }
1317                    }
1318                    CastKind::PointerCoercion(PointerCoercion::ArrayToPointer, _) => {
1319                        // FIXME: Check pointee types
1320                        check_kinds!(
1321                            op_ty,
1322                            "CastKind::{kind:?} input must be a raw pointer, not {:?}",
1323                            ty::RawPtr(..)
1324                        );
1325                        check_kinds!(
1326                            target_type,
1327                            "CastKind::{kind:?} output must be a raw pointer, not {:?}",
1328                            ty::RawPtr(..)
1329                        );
1330                        if self.body.phase >= MirPhase::Analysis(AnalysisPhase::PostCleanup) {
1331                            self.fail(location, format!("After borrowck, MIR disallows {kind:?}"));
1332                        }
1333                    }
1334                    CastKind::PointerCoercion(PointerCoercion::Unsize, _) => {
1335                        // Pointers being unsize coerced should at least implement
1336                        // `CoerceUnsized`.
1337                        if !self.predicate_must_hold_modulo_regions(ty::TraitRef::new(
1338                            self.tcx,
1339                            self.tcx.require_lang_item(
1340                                LangItem::CoerceUnsized,
1341                                self.body.source_info(location).span,
1342                            ),
1343                            [op_ty, *target_type],
1344                        )) {
1345                            self.fail(location, format!("Unsize coercion, but `{op_ty}` isn't coercible to `{target_type}`"));
1346                        }
1347                    }
1348                    CastKind::IntToInt | CastKind::IntToFloat => {
1349                        let input_valid = op_ty.is_integral() || op_ty.is_char() || op_ty.is_bool();
1350                        let target_valid = target_type.is_numeric() || target_type.is_char();
1351                        if !input_valid || !target_valid {
1352                            self.fail(
1353                                location,
1354                                format!("Wrong cast kind {kind:?} for the type {op_ty}"),
1355                            );
1356                        }
1357                    }
1358                    CastKind::FnPtrToPtr => {
1359                        check_kinds!(
1360                            op_ty,
1361                            "CastKind::{kind:?} input must be a fn pointer, not {:?}",
1362                            ty::FnPtr(..)
1363                        );
1364                        check_kinds!(
1365                            target_type,
1366                            "CastKind::{kind:?} output must be a raw pointer, not {:?}",
1367                            ty::RawPtr(..)
1368                        );
1369                    }
1370                    CastKind::PtrToPtr => {
1371                        check_kinds!(
1372                            op_ty,
1373                            "CastKind::{kind:?} input must be a raw pointer, not {:?}",
1374                            ty::RawPtr(..)
1375                        );
1376                        check_kinds!(
1377                            target_type,
1378                            "CastKind::{kind:?} output must be a raw pointer, not {:?}",
1379                            ty::RawPtr(..)
1380                        );
1381                    }
1382                    CastKind::FloatToFloat | CastKind::FloatToInt => {
1383                        if !op_ty.is_floating_point() || !target_type.is_numeric() {
1384                            self.fail(
1385                                location,
1386                                format!(
1387                                    "Trying to cast non 'Float' as {kind:?} into {target_type:?}"
1388                                ),
1389                            );
1390                        }
1391                    }
1392                    CastKind::Transmute => {
1393                        // Unlike `mem::transmute`, a MIR `Transmute` is well-formed
1394                        // for any two `Sized` types, just potentially UB to run.
1395
1396                        if !self
1397                            .tcx
1398                            .normalize_erasing_regions(
1399                                self.typing_env,
1400                                Unnormalized::new_wip(op_ty),
1401                            )
1402                            .is_sized(self.tcx, self.typing_env)
1403                        {
1404                            self.fail(
1405                                location,
1406                                format!("Cannot transmute from non-`Sized` type {op_ty}"),
1407                            );
1408                        }
1409                        if !self
1410                            .tcx
1411                            .normalize_erasing_regions(
1412                                self.typing_env,
1413                                Unnormalized::new_wip(*target_type),
1414                            )
1415                            .is_sized(self.tcx, self.typing_env)
1416                        {
1417                            self.fail(
1418                                location,
1419                                format!("Cannot transmute to non-`Sized` type {target_type:?}"),
1420                            );
1421                        }
1422                    }
1423                    CastKind::Subtype => {
1424                        if !util::sub_types(self.tcx, self.typing_env, op_ty, *target_type) {
1425                            self.fail(
1426                                location,
1427                                format!("Failed subtyping {op_ty} and {target_type}"),
1428                            )
1429                        }
1430                    }
1431                }
1432            }
1433            Rvalue::Repeat(_, _)
1434            | Rvalue::ThreadLocalRef(_)
1435            | Rvalue::RawPtr(_, _)
1436            | Rvalue::Discriminant(_) => {}
1437
1438            Rvalue::WrapUnsafeBinder(op, ty) => {
1439                let unwrapped_ty = op.ty(self.body, self.tcx);
1440                let ty::UnsafeBinder(binder_ty) = *ty.kind() else {
1441                    self.fail(
1442                        location,
1443                        format!("WrapUnsafeBinder does not produce a ty::UnsafeBinder"),
1444                    );
1445                    return;
1446                };
1447                let binder_inner_ty = self.tcx.instantiate_bound_regions_with_erased(*binder_ty);
1448                if !self.mir_assign_valid_types(unwrapped_ty, binder_inner_ty) {
1449                    self.fail(
1450                        location,
1451                        format!("Cannot wrap {unwrapped_ty} into unsafe binder {binder_ty:?}"),
1452                    );
1453                }
1454            }
1455        }
1456        self.super_rvalue(rvalue, location);
1457    }
1458
1459    fn visit_statement(&mut self, statement: &Statement<'tcx>, location: Location) {
1460        match &statement.kind {
1461            StatementKind::Assign((dest, rvalue)) => {
1462                // LHS and RHS of the assignment must have the same type.
1463                let left_ty = dest.ty(&self.body.local_decls, self.tcx).ty;
1464                let right_ty = rvalue.ty(&self.body.local_decls, self.tcx);
1465
1466                if !self.mir_assign_valid_types(right_ty, left_ty) {
1467                    self.fail(
1468                        location,
1469                        format!(
1470                            "encountered `{:?}` with incompatible types:\n\
1471                            left-hand side has type: {}\n\
1472                            right-hand side has type: {}",
1473                            statement.kind, left_ty, right_ty,
1474                        ),
1475                    );
1476                }
1477
1478                if let Some(local) = dest.as_local()
1479                    && let ClearCrossCrate::Set(LocalInfo::DerefTemp) =
1480                        self.body.local_decls[local].local_info
1481                    && !matches!(rvalue, Rvalue::CopyForDeref(_))
1482                {
1483                    self.fail(location, "assignment to a `DerefTemp` must use `CopyForDeref`")
1484                }
1485            }
1486            StatementKind::AscribeUserType(..) => {
1487                if self.body.phase >= MirPhase::Runtime(RuntimePhase::Initial) {
1488                    self.fail(
1489                        location,
1490                        "`AscribeUserType` should have been removed after drop lowering phase",
1491                    );
1492                }
1493            }
1494            StatementKind::FakeRead(..) => {
1495                if self.body.phase >= MirPhase::Runtime(RuntimePhase::Initial) {
1496                    self.fail(
1497                        location,
1498                        "`FakeRead` should have been removed after drop lowering phase",
1499                    );
1500                }
1501            }
1502            StatementKind::Intrinsic(NonDivergingIntrinsic::Assume(op)) => {
1503                let ty = op.ty(&self.body.local_decls, self.tcx);
1504                if !ty.is_bool() {
1505                    self.fail(
1506                        location,
1507                        format!("`assume` argument must be `bool`, but got: `{ty}`"),
1508                    );
1509                }
1510            }
1511            StatementKind::Intrinsic(NonDivergingIntrinsic::CopyNonOverlapping(
1512                CopyNonOverlapping { src, dst, count },
1513            )) => {
1514                let src_ty = src.ty(&self.body.local_decls, self.tcx);
1515                let op_src_ty = if let Some(src_deref) = src_ty.builtin_deref(true) {
1516                    src_deref
1517                } else {
1518                    self.fail(
1519                        location,
1520                        format!("Expected src to be ptr in copy_nonoverlapping, got: {src_ty}"),
1521                    );
1522                    return;
1523                };
1524                let dst_ty = dst.ty(&self.body.local_decls, self.tcx);
1525                let op_dst_ty = if let Some(dst_deref) = dst_ty.builtin_deref(true) {
1526                    dst_deref
1527                } else {
1528                    self.fail(
1529                        location,
1530                        format!("Expected dst to be ptr in copy_nonoverlapping, got: {dst_ty}"),
1531                    );
1532                    return;
1533                };
1534                // since CopyNonOverlapping is parametrized by 1 type,
1535                // we only need to check that they are equal and not keep an extra parameter.
1536                if !self.mir_assign_valid_types(op_src_ty, op_dst_ty) {
1537                    self.fail(location, format!("bad arg ({op_src_ty} != {op_dst_ty})"));
1538                }
1539
1540                let op_cnt_ty = count.ty(&self.body.local_decls, self.tcx);
1541                if op_cnt_ty != self.tcx.types.usize {
1542                    self.fail(location, format!("bad arg ({op_cnt_ty} != usize)"))
1543                }
1544            }
1545            StatementKind::SetDiscriminant { place, .. } => {
1546                if self.body.phase < MirPhase::Runtime(RuntimePhase::Initial) {
1547                    self.fail(location, "`SetDiscriminant`is not allowed until deaggregation");
1548                }
1549                let pty = place.ty(&self.body.local_decls, self.tcx).ty;
1550                if !matches!(
1551                    pty.kind(),
1552                    ty::Adt(..)
1553                        | ty::Coroutine(..)
1554                        | ty::Alias(ty::AliasTy { kind: ty::Opaque { .. }, .. })
1555                ) {
1556                    self.fail(
1557                        location,
1558                        format!(
1559                            "`SetDiscriminant` is only allowed on ADTs and coroutines, not {pty}"
1560                        ),
1561                    );
1562                }
1563            }
1564            StatementKind::StorageLive(_)
1565            | StatementKind::StorageDead(_)
1566            | StatementKind::Coverage(_)
1567            | StatementKind::ConstEvalCounter
1568            | StatementKind::PlaceMention(..)
1569            | StatementKind::BackwardIncompatibleDropHint { .. }
1570            | StatementKind::Nop => {}
1571        }
1572
1573        self.super_statement(statement, location);
1574    }
1575
1576    fn visit_terminator(&mut self, terminator: &Terminator<'tcx>, location: Location) {
1577        match &terminator.kind {
1578            TerminatorKind::SwitchInt { targets, discr } => {
1579                let switch_ty = discr.ty(&self.body.local_decls, self.tcx);
1580
1581                let target_width = self.tcx.sess.target.pointer_width;
1582
1583                let size = Size::from_bits(match switch_ty.kind() {
1584                    ty::Uint(uint) => uint.normalize(target_width).bit_width().unwrap(),
1585                    ty::Int(int) => int.normalize(target_width).bit_width().unwrap(),
1586                    ty::Char => 32,
1587                    ty::Bool => 1,
1588                    other => bug!("unhandled type: {:?}", other),
1589                });
1590
1591                for (value, _) in targets.iter() {
1592                    if ScalarInt::try_from_uint(value, size).is_none() {
1593                        self.fail(
1594                            location,
1595                            format!("the value {value:#x} is not a proper {switch_ty}"),
1596                        )
1597                    }
1598                }
1599            }
1600            TerminatorKind::Call { func, .. } | TerminatorKind::TailCall { func, .. } => {
1601                let func_ty = func.ty(&self.body.local_decls, self.tcx);
1602                match func_ty.kind() {
1603                    ty::FnPtr(..) | ty::FnDef(..) => {}
1604                    _ => self.fail(
1605                        location,
1606                        format!(
1607                            "encountered non-callable type {func_ty} in `{}` terminator",
1608                            terminator.kind.name()
1609                        ),
1610                    ),
1611                }
1612
1613                if let TerminatorKind::TailCall { .. } = terminator.kind {
1614                    // FIXME(explicit_tail_calls): implement tail-call specific checks here (such
1615                    // as signature matching, forbidding closures, etc)
1616                }
1617            }
1618            TerminatorKind::Assert { cond, .. } => {
1619                let cond_ty = cond.ty(&self.body.local_decls, self.tcx);
1620                if cond_ty != self.tcx.types.bool {
1621                    self.fail(
1622                        location,
1623                        format!(
1624                            "encountered non-boolean condition of type {cond_ty} in `Assert` terminator"
1625                        ),
1626                    );
1627                }
1628            }
1629            TerminatorKind::Goto { .. }
1630            | TerminatorKind::Drop { .. }
1631            | TerminatorKind::Yield { .. }
1632            | TerminatorKind::FalseEdge { .. }
1633            | TerminatorKind::FalseUnwind { .. }
1634            | TerminatorKind::InlineAsm { .. }
1635            | TerminatorKind::CoroutineDrop
1636            | TerminatorKind::UnwindResume
1637            | TerminatorKind::UnwindTerminate(_)
1638            | TerminatorKind::Return
1639            | TerminatorKind::Unreachable => {}
1640        }
1641
1642        self.super_terminator(terminator, location);
1643    }
1644
1645    fn visit_local_decl(&mut self, local: Local, local_decl: &LocalDecl<'tcx>) {
1646        if let ClearCrossCrate::Set(LocalInfo::DerefTemp) = local_decl.local_info {
1647            if self.body.phase >= MirPhase::Runtime(RuntimePhase::Initial) {
1648                self.fail(
1649                    START_BLOCK.start_location(),
1650                    "`DerefTemp` should have been removed in runtime MIR",
1651                );
1652            } else if local_decl.ty.builtin_deref(true).is_none() {
1653                self.fail(
1654                    START_BLOCK.start_location(),
1655                    "`DerefTemp` should only be used for dereferenceable types",
1656                )
1657            }
1658        }
1659
1660        self.super_local_decl(local, local_decl);
1661    }
1662}
1663
1664pub(super) fn validate_debuginfos<'tcx>(body: &Body<'tcx>) -> Vec<(Location, String)> {
1665    let mut debuginfo_checker =
1666        DebuginfoChecker { debuginfo_locals: debuginfo_locals(body), failures: Vec::new() };
1667    debuginfo_checker.visit_body(body);
1668    debuginfo_checker.failures
1669}
1670
1671struct DebuginfoChecker {
1672    debuginfo_locals: DenseBitSet<Local>,
1673    failures: Vec<(Location, String)>,
1674}
1675
1676impl<'tcx> Visitor<'tcx> for DebuginfoChecker {
1677    fn visit_statement_debuginfo(
1678        &mut self,
1679        stmt_debuginfo: &StmtDebugInfo<'tcx>,
1680        location: Location,
1681    ) {
1682        let local = match stmt_debuginfo {
1683            StmtDebugInfo::AssignRef(local, _) | StmtDebugInfo::InvalidAssign(local) => *local,
1684        };
1685        if !self.debuginfo_locals.contains(local) {
1686            self.failures.push((location, format!("{local:?} is not in debuginfo")));
1687        }
1688    }
1689}