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