1use std::rc::Rc;
4use std::{fmt, iter, mem};
5
6use rustc_abi::FieldIdx;
7use rustc_data_structures::frozen::Frozen;
8use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
9use rustc_errors::ErrorGuaranteed;
10use rustc_hir as hir;
11use rustc_hir::def::DefKind;
12use rustc_hir::def_id::LocalDefId;
13use rustc_hir::lang_items::LangItem;
14use rustc_index::{IndexSlice, IndexVec};
15use rustc_infer::infer::canonical::QueryRegionConstraints;
16use rustc_infer::infer::outlives::env::RegionBoundPairs;
17use rustc_infer::infer::region_constraints::RegionConstraintData;
18use rustc_infer::infer::{
19 BoundRegion, BoundRegionConversionTime, InferCtxt, NllRegionVariableOrigin,
20};
21use rustc_infer::traits::PredicateObligations;
22use rustc_middle::mir::visit::{NonMutatingUseContext, PlaceContext, Visitor};
23use rustc_middle::mir::*;
24use rustc_middle::traits::query::NoSolution;
25use rustc_middle::ty::adjustment::PointerCoercion;
26use rustc_middle::ty::cast::CastTy;
27use rustc_middle::ty::{
28 self, Binder, CanonicalUserTypeAnnotation, CanonicalUserTypeAnnotations, CoroutineArgsExt,
29 Dynamic, GenericArgsRef, OpaqueHiddenType, OpaqueTypeKey, RegionVid, Ty, TyCtxt,
30 TypeVisitableExt, UserArgs, UserTypeAnnotationIndex, fold_regions,
31};
32use rustc_middle::{bug, span_bug};
33use rustc_mir_dataflow::move_paths::MoveData;
34use rustc_mir_dataflow::points::DenseLocationMap;
35use rustc_span::def_id::CRATE_DEF_ID;
36use rustc_span::source_map::Spanned;
37use rustc_span::{Span, sym};
38use rustc_trait_selection::traits::query::type_op::custom::scrape_region_constraints;
39use rustc_trait_selection::traits::query::type_op::{TypeOp, TypeOpOutput};
40use tracing::{debug, instrument, trace};
41
42use crate::borrow_set::BorrowSet;
43use crate::constraints::{OutlivesConstraint, OutlivesConstraintSet};
44use crate::diagnostics::UniverseInfo;
45use crate::member_constraints::MemberConstraintSet;
46use crate::polonius::legacy::{PoloniusFacts, PoloniusLocationTable};
47use crate::polonius::{PoloniusContext, PoloniusLivenessContext};
48use crate::region_infer::TypeTest;
49use crate::region_infer::values::{LivenessValues, PlaceholderIndex, PlaceholderIndices};
50use crate::session_diagnostics::{MoveUnsized, SimdIntrinsicArgConst};
51use crate::type_check::free_region_relations::{CreateResult, UniversalRegionRelations};
52use crate::universal_regions::{DefiningTy, UniversalRegions};
53use crate::{BorrowCheckRootCtxt, BorrowckInferCtxt, path_utils};
54
55macro_rules! span_mirbug {
56 ($context:expr, $elem:expr, $($message:tt)*) => ({
57 $crate::type_check::mirbug(
58 $context.tcx(),
59 $context.last_span,
60 format!(
61 "broken MIR in {:?} ({:?}): {}",
62 $context.body().source.def_id(),
63 $elem,
64 format_args!($($message)*),
65 ),
66 )
67 })
68}
69
70mod canonical;
71mod constraint_conversion;
72pub(crate) mod free_region_relations;
73mod input_output;
74pub(crate) mod liveness;
75mod opaque_types;
76mod relate_tys;
77
78pub(crate) fn type_check<'tcx>(
101 root_cx: &mut BorrowCheckRootCtxt<'tcx>,
102 infcx: &BorrowckInferCtxt<'tcx>,
103 body: &Body<'tcx>,
104 promoted: &IndexSlice<Promoted, Body<'tcx>>,
105 universal_regions: UniversalRegions<'tcx>,
106 location_table: &PoloniusLocationTable,
107 borrow_set: &BorrowSet<'tcx>,
108 polonius_facts: &mut Option<PoloniusFacts>,
109 move_data: &MoveData<'tcx>,
110 location_map: Rc<DenseLocationMap>,
111) -> MirTypeckResults<'tcx> {
112 let mut constraints = MirTypeckRegionConstraints {
113 placeholder_indices: PlaceholderIndices::default(),
114 placeholder_index_to_region: IndexVec::default(),
115 liveness_constraints: LivenessValues::with_specific_points(Rc::clone(&location_map)),
116 outlives_constraints: OutlivesConstraintSet::default(),
117 member_constraints: MemberConstraintSet::default(),
118 type_tests: Vec::default(),
119 universe_causes: FxIndexMap::default(),
120 };
121
122 let CreateResult {
123 universal_region_relations,
124 region_bound_pairs,
125 normalized_inputs_and_output,
126 known_type_outlives_obligations,
127 } = free_region_relations::create(infcx, infcx.param_env, universal_regions, &mut constraints);
128
129 let pre_obligations = infcx.take_registered_region_obligations();
130 assert!(
131 pre_obligations.is_empty(),
132 "there should be no incoming region obligations = {pre_obligations:#?}",
133 );
134
135 debug!(?normalized_inputs_and_output);
136
137 let polonius_liveness = if infcx.tcx.sess.opts.unstable_opts.polonius.is_next_enabled() {
138 Some(PoloniusLivenessContext::default())
139 } else {
140 None
141 };
142
143 let mut typeck = TypeChecker {
144 root_cx,
145 infcx,
146 last_span: body.span,
147 body,
148 promoted,
149 user_type_annotations: &body.user_type_annotations,
150 region_bound_pairs: ®ion_bound_pairs,
151 known_type_outlives_obligations: &known_type_outlives_obligations,
152 reported_errors: Default::default(),
153 universal_regions: &universal_region_relations.universal_regions,
154 location_table,
155 polonius_facts,
156 borrow_set,
157 constraints: &mut constraints,
158 polonius_liveness,
159 };
160
161 typeck.check_user_type_annotations();
162 typeck.visit_body(body);
163 typeck.equate_inputs_and_outputs(&normalized_inputs_and_output);
164 typeck.check_signature_annotation();
165
166 liveness::generate(&mut typeck, &location_map, move_data);
167
168 let opaque_type_values =
169 opaque_types::take_opaques_and_register_member_constraints(&mut typeck);
170
171 let polonius_context = typeck.polonius_liveness.take().map(|liveness_context| {
173 PoloniusContext::create_from_liveness(
174 liveness_context,
175 infcx.num_region_vars(),
176 typeck.constraints.liveness_constraints.points(),
177 )
178 });
179
180 MirTypeckResults {
181 constraints,
182 universal_region_relations,
183 opaque_type_values,
184 polonius_context,
185 }
186}
187
188#[track_caller]
189fn mirbug(tcx: TyCtxt<'_>, span: Span, msg: String) {
190 tcx.dcx().span_delayed_bug(span, msg);
194}
195
196enum FieldAccessError {
197 OutOfRange { field_count: usize },
198}
199
200struct TypeChecker<'a, 'tcx> {
205 root_cx: &'a mut BorrowCheckRootCtxt<'tcx>,
206 infcx: &'a BorrowckInferCtxt<'tcx>,
207 last_span: Span,
208 body: &'a Body<'tcx>,
209 promoted: &'a IndexSlice<Promoted, Body<'tcx>>,
212 user_type_annotations: &'a CanonicalUserTypeAnnotations<'tcx>,
215 region_bound_pairs: &'a RegionBoundPairs<'tcx>,
216 known_type_outlives_obligations: &'a [ty::PolyTypeOutlivesPredicate<'tcx>],
217 reported_errors: FxIndexSet<(Ty<'tcx>, Span)>,
218 universal_regions: &'a UniversalRegions<'tcx>,
219 location_table: &'a PoloniusLocationTable,
220 polonius_facts: &'a mut Option<PoloniusFacts>,
221 borrow_set: &'a BorrowSet<'tcx>,
222 constraints: &'a mut MirTypeckRegionConstraints<'tcx>,
223 polonius_liveness: Option<PoloniusLivenessContext>,
225}
226
227pub(crate) struct MirTypeckResults<'tcx> {
230 pub(crate) constraints: MirTypeckRegionConstraints<'tcx>,
231 pub(crate) universal_region_relations: Frozen<UniversalRegionRelations<'tcx>>,
232 pub(crate) opaque_type_values: FxIndexMap<OpaqueTypeKey<'tcx>, OpaqueHiddenType<'tcx>>,
233 pub(crate) polonius_context: Option<PoloniusContext>,
234}
235
236pub(crate) struct MirTypeckRegionConstraints<'tcx> {
239 pub(crate) placeholder_indices: PlaceholderIndices,
245
246 pub(crate) placeholder_index_to_region: IndexVec<PlaceholderIndex, ty::Region<'tcx>>,
252
253 pub(crate) liveness_constraints: LivenessValues,
261
262 pub(crate) outlives_constraints: OutlivesConstraintSet<'tcx>,
263
264 pub(crate) member_constraints: MemberConstraintSet<'tcx, RegionVid>,
265
266 pub(crate) universe_causes: FxIndexMap<ty::UniverseIndex, UniverseInfo<'tcx>>,
267
268 pub(crate) type_tests: Vec<TypeTest<'tcx>>,
269}
270
271impl<'tcx> MirTypeckRegionConstraints<'tcx> {
272 fn placeholder_region(
275 &mut self,
276 infcx: &InferCtxt<'tcx>,
277 placeholder: ty::PlaceholderRegion,
278 ) -> ty::Region<'tcx> {
279 let placeholder_index = self.placeholder_indices.insert(placeholder);
280 match self.placeholder_index_to_region.get(placeholder_index) {
281 Some(&v) => v,
282 None => {
283 let origin = NllRegionVariableOrigin::Placeholder(placeholder);
284 let region = infcx.next_nll_region_var_in_universe(origin, placeholder.universe);
285 self.placeholder_index_to_region.push(region);
286 region
287 }
288 }
289 }
290}
291
292#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
297pub enum Locations {
298 All(Span),
334
335 Single(Location),
339}
340
341impl Locations {
342 pub fn from_location(&self) -> Option<Location> {
343 match self {
344 Locations::All(_) => None,
345 Locations::Single(from_location) => Some(*from_location),
346 }
347 }
348
349 pub fn span(&self, body: &Body<'_>) -> Span {
351 match self {
352 Locations::All(span) => *span,
353 Locations::Single(l) => body.source_info(*l).span,
354 }
355 }
356}
357
358impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
359 fn tcx(&self) -> TyCtxt<'tcx> {
360 self.infcx.tcx
361 }
362
363 fn body(&self) -> &Body<'tcx> {
364 self.body
365 }
366
367 fn to_region_vid(&mut self, r: ty::Region<'tcx>) -> RegionVid {
368 if let ty::RePlaceholder(placeholder) = r.kind() {
369 self.constraints.placeholder_region(self.infcx, placeholder).as_var()
370 } else {
371 self.universal_regions.to_region_vid(r)
372 }
373 }
374
375 fn unsized_feature_enabled(&self) -> bool {
376 let features = self.tcx().features();
377 features.unsized_locals() || features.unsized_fn_params()
378 }
379
380 #[instrument(skip(self), level = "debug")]
382 fn check_user_type_annotations(&mut self) {
383 debug!(?self.user_type_annotations);
384 let tcx = self.tcx();
385 for user_annotation in self.user_type_annotations {
386 let CanonicalUserTypeAnnotation { span, ref user_ty, inferred_ty } = *user_annotation;
387 let annotation = self.instantiate_canonical(span, user_ty);
388 if let ty::UserTypeKind::TypeOf(def, args) = annotation.kind
389 && let DefKind::InlineConst = tcx.def_kind(def)
390 {
391 assert!(annotation.bounds.is_empty());
392 self.check_inline_const(inferred_ty, def.expect_local(), args, span);
393 } else {
394 self.ascribe_user_type(inferred_ty, annotation, span);
395 }
396 }
397 }
398
399 #[instrument(skip(self, data), level = "debug")]
400 fn push_region_constraints(
401 &mut self,
402 locations: Locations,
403 category: ConstraintCategory<'tcx>,
404 data: &QueryRegionConstraints<'tcx>,
405 ) {
406 debug!("constraints generated: {:#?}", data);
407
408 constraint_conversion::ConstraintConversion::new(
409 self.infcx,
410 self.universal_regions,
411 self.region_bound_pairs,
412 self.infcx.param_env,
413 self.known_type_outlives_obligations,
414 locations,
415 locations.span(self.body),
416 category,
417 self.constraints,
418 )
419 .convert_all(data);
420 }
421
422 fn sub_types(
424 &mut self,
425 sub: Ty<'tcx>,
426 sup: Ty<'tcx>,
427 locations: Locations,
428 category: ConstraintCategory<'tcx>,
429 ) -> Result<(), NoSolution> {
430 self.relate_types(sup, ty::Contravariant, sub, locations, category)
433 }
434
435 #[instrument(skip(self, category), level = "debug")]
436 fn eq_types(
437 &mut self,
438 expected: Ty<'tcx>,
439 found: Ty<'tcx>,
440 locations: Locations,
441 category: ConstraintCategory<'tcx>,
442 ) -> Result<(), NoSolution> {
443 self.relate_types(expected, ty::Invariant, found, locations, category)
444 }
445
446 #[instrument(skip(self), level = "debug")]
447 fn relate_type_and_user_type(
448 &mut self,
449 a: Ty<'tcx>,
450 v: ty::Variance,
451 user_ty: &UserTypeProjection,
452 locations: Locations,
453 category: ConstraintCategory<'tcx>,
454 ) -> Result<(), NoSolution> {
455 let annotated_type = self.user_type_annotations[user_ty.base].inferred_ty;
456 trace!(?annotated_type);
457 let mut curr_projected_ty = PlaceTy::from_ty(annotated_type);
458
459 let tcx = self.infcx.tcx;
460
461 for proj in &user_ty.projs {
462 if !self.infcx.next_trait_solver()
463 && let ty::Alias(ty::Opaque, ..) = curr_projected_ty.ty.kind()
464 {
465 return Ok(());
469 }
470 let projected_ty = curr_projected_ty.projection_ty_core(
471 tcx,
472 proj,
473 |ty| self.structurally_resolve(ty, locations),
474 |ty, variant_index, field, ()| PlaceTy::field_ty(tcx, ty, variant_index, field),
475 |_| unreachable!(),
476 );
477 curr_projected_ty = projected_ty;
478 }
479 trace!(?curr_projected_ty);
480
481 let a = self.normalize(a, locations);
484 let ty = self.normalize(curr_projected_ty.ty, locations);
485 self.relate_types(ty, v.xform(ty::Contravariant), a, locations, category)?;
486
487 Ok(())
488 }
489
490 fn check_promoted(&mut self, promoted_body: &'a Body<'tcx>, location: Location) {
491 let parent_body = mem::replace(&mut self.body, promoted_body);
496
497 let polonius_facts = &mut None;
500 let mut constraints = Default::default();
501 let mut liveness_constraints =
502 LivenessValues::without_specific_points(Rc::new(DenseLocationMap::new(promoted_body)));
503
504 let mut swap_constraints = |this: &mut Self| {
507 mem::swap(this.polonius_facts, polonius_facts);
508 mem::swap(&mut this.constraints.outlives_constraints, &mut constraints);
509 mem::swap(&mut this.constraints.liveness_constraints, &mut liveness_constraints);
510 };
511
512 swap_constraints(self);
513
514 self.visit_body(promoted_body);
515
516 self.body = parent_body;
517
518 swap_constraints(self);
520 let locations = location.to_locations();
521 for constraint in constraints.outlives().iter() {
522 let mut constraint = *constraint;
523 constraint.locations = locations;
524 if let ConstraintCategory::Return(_)
525 | ConstraintCategory::UseAsConst
526 | ConstraintCategory::UseAsStatic = constraint.category
527 {
528 constraint.category = ConstraintCategory::Boring;
531 }
532 self.constraints.outlives_constraints.push(constraint)
533 }
534 #[allow(rustc::potential_query_instability)]
541 for region in liveness_constraints.live_regions_unordered() {
542 self.constraints.liveness_constraints.add_location(region, location);
543 }
544 }
545
546 fn check_inline_const(
547 &mut self,
548 inferred_ty: Ty<'tcx>,
549 def_id: LocalDefId,
550 args: UserArgs<'tcx>,
551 span: Span,
552 ) {
553 assert!(args.user_self_ty.is_none());
554 let tcx = self.tcx();
555 let const_ty = tcx.type_of(def_id).instantiate(tcx, args.args);
556 if let Err(terr) =
557 self.eq_types(const_ty, inferred_ty, Locations::All(span), ConstraintCategory::Boring)
558 {
559 span_bug!(
560 span,
561 "bad inline const pattern: ({:?} = {:?}) {:?}",
562 const_ty,
563 inferred_ty,
564 terr
565 );
566 }
567 let args = self.infcx.resolve_vars_if_possible(args.args);
568 let predicates = self.prove_closure_bounds(tcx, def_id, args, Locations::All(span));
569 self.normalize_and_prove_instantiated_predicates(
570 def_id.to_def_id(),
571 predicates,
572 Locations::All(span),
573 );
574 }
575}
576
577impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
578 fn visit_span(&mut self, span: Span) {
579 if !span.is_dummy() {
580 debug!(?span);
581 self.last_span = span;
582 }
583 }
584
585 #[instrument(skip(self, body), level = "debug")]
586 fn visit_body(&mut self, body: &Body<'tcx>) {
587 debug_assert!(std::ptr::eq(self.body, body));
588
589 for (local, local_decl) in body.local_decls.iter_enumerated() {
590 self.visit_local_decl(local, local_decl);
591 }
592
593 for (block, block_data) in body.basic_blocks.iter_enumerated() {
594 let mut location = Location { block, statement_index: 0 };
595 for stmt in &block_data.statements {
596 self.visit_statement(stmt, location);
597 location.statement_index += 1;
598 }
599
600 self.visit_terminator(block_data.terminator(), location);
601 self.check_iscleanup(block_data);
602 }
603 }
604
605 #[instrument(skip(self), level = "debug")]
606 fn visit_statement(&mut self, stmt: &Statement<'tcx>, location: Location) {
607 self.super_statement(stmt, location);
608 let tcx = self.tcx();
609 match &stmt.kind {
610 StatementKind::Assign(box (place, rv)) => {
611 let category = match place.as_local() {
616 Some(RETURN_PLACE) => {
617 let defining_ty = &self.universal_regions.defining_ty;
618 if defining_ty.is_const() {
619 if tcx.is_static(defining_ty.def_id()) {
620 ConstraintCategory::UseAsStatic
621 } else {
622 ConstraintCategory::UseAsConst
623 }
624 } else {
625 ConstraintCategory::Return(ReturnConstraint::Normal)
626 }
627 }
628 Some(l)
629 if matches!(
630 self.body.local_decls[l].local_info(),
631 LocalInfo::AggregateTemp
632 ) =>
633 {
634 ConstraintCategory::Usage
635 }
636 Some(l) if !self.body.local_decls[l].is_user_variable() => {
637 ConstraintCategory::Boring
638 }
639 _ => ConstraintCategory::Assignment,
640 };
641 debug!(
642 "assignment category: {:?} {:?}",
643 category,
644 place.as_local().map(|l| &self.body.local_decls[l])
645 );
646
647 let place_ty = place.ty(self.body, tcx).ty;
648 debug!(?place_ty);
649 let place_ty = self.normalize(place_ty, location);
650 debug!("place_ty normalized: {:?}", place_ty);
651 let rv_ty = rv.ty(self.body, tcx);
652 debug!(?rv_ty);
653 let rv_ty = self.normalize(rv_ty, location);
654 debug!("normalized rv_ty: {:?}", rv_ty);
655 if let Err(terr) =
656 self.sub_types(rv_ty, place_ty, location.to_locations(), category)
657 {
658 span_mirbug!(
659 self,
660 stmt,
661 "bad assignment ({:?} = {:?}): {:?}",
662 place_ty,
663 rv_ty,
664 terr
665 );
666 }
667
668 if let Some(annotation_index) = self.rvalue_user_ty(rv) {
669 if let Err(terr) = self.relate_type_and_user_type(
670 rv_ty,
671 ty::Invariant,
672 &UserTypeProjection { base: annotation_index, projs: vec![] },
673 location.to_locations(),
674 ConstraintCategory::TypeAnnotation(AnnotationSource::GenericArg),
675 ) {
676 let annotation = &self.user_type_annotations[annotation_index];
677 span_mirbug!(
678 self,
679 stmt,
680 "bad user type on rvalue ({:?} = {:?}): {:?}",
681 annotation,
682 rv_ty,
683 terr
684 );
685 }
686 }
687
688 if !self.unsized_feature_enabled() {
689 let trait_ref = ty::TraitRef::new(
690 tcx,
691 tcx.require_lang_item(LangItem::Sized, self.last_span),
692 [place_ty],
693 );
694 self.prove_trait_ref(
695 trait_ref,
696 location.to_locations(),
697 ConstraintCategory::SizedBound,
698 );
699 }
700 }
701 StatementKind::AscribeUserType(box (place, projection), variance) => {
702 let place_ty = place.ty(self.body, tcx).ty;
703 if let Err(terr) = self.relate_type_and_user_type(
704 place_ty,
705 *variance,
706 projection,
707 Locations::All(stmt.source_info.span),
708 ConstraintCategory::TypeAnnotation(AnnotationSource::Ascription),
709 ) {
710 let annotation = &self.user_type_annotations[projection.base];
711 span_mirbug!(
712 self,
713 stmt,
714 "bad type assert ({:?} <: {:?} with projections {:?}): {:?}",
715 place_ty,
716 annotation,
717 projection.projs,
718 terr
719 );
720 }
721 }
722 StatementKind::Intrinsic(box NonDivergingIntrinsic::Assume(..))
723 | StatementKind::FakeRead(..)
724 | StatementKind::StorageLive(..)
725 | StatementKind::StorageDead(..)
726 | StatementKind::Retag { .. }
727 | StatementKind::Coverage(..)
728 | StatementKind::ConstEvalCounter
729 | StatementKind::PlaceMention(..)
730 | StatementKind::BackwardIncompatibleDropHint { .. }
731 | StatementKind::Nop => {}
732 StatementKind::Intrinsic(box NonDivergingIntrinsic::CopyNonOverlapping(..))
733 | StatementKind::Deinit(..)
734 | StatementKind::SetDiscriminant { .. } => {
735 bug!("Statement not allowed in this MIR phase")
736 }
737 }
738 }
739
740 #[instrument(skip(self), level = "debug")]
741 fn visit_terminator(&mut self, term: &Terminator<'tcx>, term_location: Location) {
742 self.super_terminator(term, term_location);
743 let tcx = self.tcx();
744 debug!("terminator kind: {:?}", term.kind);
745 match &term.kind {
746 TerminatorKind::Goto { .. }
747 | TerminatorKind::UnwindResume
748 | TerminatorKind::UnwindTerminate(_)
749 | TerminatorKind::Return
750 | TerminatorKind::CoroutineDrop
751 | TerminatorKind::Unreachable
752 | TerminatorKind::Drop { .. }
753 | TerminatorKind::FalseEdge { .. }
754 | TerminatorKind::FalseUnwind { .. }
755 | TerminatorKind::InlineAsm { .. } => {
756 }
758
759 TerminatorKind::SwitchInt { discr, .. } => {
760 let switch_ty = discr.ty(self.body, tcx);
761 if !switch_ty.is_integral() && !switch_ty.is_char() && !switch_ty.is_bool() {
762 span_mirbug!(self, term, "bad SwitchInt discr ty {:?}", switch_ty);
763 }
764 }
766 TerminatorKind::Call { func, args, .. }
767 | TerminatorKind::TailCall { func, args, .. } => {
768 let call_source = match term.kind {
769 TerminatorKind::Call { call_source, .. } => call_source,
770 TerminatorKind::TailCall { .. } => CallSource::Normal,
771 _ => unreachable!(),
772 };
773
774 let func_ty = func.ty(self.body, tcx);
775 debug!("func_ty.kind: {:?}", func_ty.kind());
776
777 let sig = match func_ty.kind() {
778 ty::FnDef(..) | ty::FnPtr(..) => func_ty.fn_sig(tcx),
779 _ => {
780 span_mirbug!(self, term, "call to non-function {:?}", func_ty);
781 return;
782 }
783 };
784 let (unnormalized_sig, map) = tcx.instantiate_bound_regions(sig, |br| {
785 use crate::renumber::RegionCtxt;
786
787 let region_ctxt_fn = || {
788 let reg_info = match br.kind {
789 ty::BoundRegionKind::Anon => sym::anon,
790 ty::BoundRegionKind::Named(_, name) => name,
791 ty::BoundRegionKind::ClosureEnv => sym::env,
792 };
793
794 RegionCtxt::LateBound(reg_info)
795 };
796
797 self.infcx.next_region_var(
798 BoundRegion(
799 term.source_info.span,
800 br.kind,
801 BoundRegionConversionTime::FnCall,
802 ),
803 region_ctxt_fn,
804 )
805 });
806 debug!(?unnormalized_sig);
807 self.prove_predicates(
816 unnormalized_sig.inputs_and_output.iter().map(|ty| {
817 ty::Binder::dummy(ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(
818 ty.into(),
819 )))
820 }),
821 term_location.to_locations(),
822 ConstraintCategory::Boring,
823 );
824
825 let sig = self.deeply_normalize(unnormalized_sig, term_location);
826 if sig != unnormalized_sig {
830 self.prove_predicates(
831 sig.inputs_and_output.iter().map(|ty| {
832 ty::Binder::dummy(ty::PredicateKind::Clause(
833 ty::ClauseKind::WellFormed(ty.into()),
834 ))
835 }),
836 term_location.to_locations(),
837 ConstraintCategory::Boring,
838 );
839 }
840
841 if let TerminatorKind::Call { destination, target, .. } = term.kind {
842 self.check_call_dest(term, &sig, destination, target, term_location);
843 }
844
845 for &late_bound_region in map.values() {
853 let region_vid = self.universal_regions.to_region_vid(late_bound_region);
854 self.constraints.liveness_constraints.add_location(region_vid, term_location);
855 }
856
857 self.check_call_inputs(term, func, &sig, args, term_location, call_source);
858 }
859 TerminatorKind::Assert { cond, msg, .. } => {
860 let cond_ty = cond.ty(self.body, tcx);
861 if cond_ty != tcx.types.bool {
862 span_mirbug!(self, term, "bad Assert ({:?}, not bool", cond_ty);
863 }
864
865 if let AssertKind::BoundsCheck { len, index } = &**msg {
866 if len.ty(self.body, tcx) != tcx.types.usize {
867 span_mirbug!(self, len, "bounds-check length non-usize {:?}", len)
868 }
869 if index.ty(self.body, tcx) != tcx.types.usize {
870 span_mirbug!(self, index, "bounds-check index non-usize {:?}", index)
871 }
872 }
873 }
874 TerminatorKind::Yield { value, resume_arg, .. } => {
875 match self.body.yield_ty() {
876 None => span_mirbug!(self, term, "yield in non-coroutine"),
877 Some(ty) => {
878 let value_ty = value.ty(self.body, tcx);
879 if let Err(terr) = self.sub_types(
880 value_ty,
881 ty,
882 term_location.to_locations(),
883 ConstraintCategory::Yield,
884 ) {
885 span_mirbug!(
886 self,
887 term,
888 "type of yield value is {:?}, but the yield type is {:?}: {:?}",
889 value_ty,
890 ty,
891 terr
892 );
893 }
894 }
895 }
896
897 match self.body.resume_ty() {
898 None => span_mirbug!(self, term, "yield in non-coroutine"),
899 Some(ty) => {
900 let resume_ty = resume_arg.ty(self.body, tcx);
901 if let Err(terr) = self.sub_types(
902 ty,
903 resume_ty.ty,
904 term_location.to_locations(),
905 ConstraintCategory::Yield,
906 ) {
907 span_mirbug!(
908 self,
909 term,
910 "type of resume place is {:?}, but the resume type is {:?}: {:?}",
911 resume_ty,
912 ty,
913 terr
914 );
915 }
916 }
917 }
918 }
919 }
920 }
921
922 fn visit_local_decl(&mut self, local: Local, local_decl: &LocalDecl<'tcx>) {
923 self.super_local_decl(local, local_decl);
924
925 for user_ty in
926 local_decl.user_ty.as_deref().into_iter().flat_map(UserTypeProjections::projections)
927 {
928 let span = self.user_type_annotations[user_ty.base].span;
929
930 let ty = if local_decl.is_nonref_binding() {
931 local_decl.ty
932 } else if let &ty::Ref(_, rty, _) = local_decl.ty.kind() {
933 rty
937 } else {
938 bug!("{:?} with ref binding has wrong type {}", local, local_decl.ty);
939 };
940
941 if let Err(terr) = self.relate_type_and_user_type(
942 ty,
943 ty::Invariant,
944 user_ty,
945 Locations::All(span),
946 ConstraintCategory::TypeAnnotation(AnnotationSource::Declaration),
947 ) {
948 span_mirbug!(
949 self,
950 local,
951 "bad user type on variable {:?}: {:?} != {:?} ({:?})",
952 local,
953 local_decl.ty,
954 local_decl.user_ty,
955 terr,
956 );
957 }
958 }
959
960 if !self.unsized_feature_enabled() {
963 match self.body.local_kind(local) {
964 LocalKind::ReturnPointer | LocalKind::Arg => {
965 return;
972 }
973 LocalKind::Temp => {
974 let span = local_decl.source_info.span;
975 let ty = local_decl.ty;
976 self.ensure_place_sized(ty, span);
977 }
978 }
979 }
980 }
981
982 #[instrument(skip(self), level = "debug")]
983 fn visit_rvalue(&mut self, rvalue: &Rvalue<'tcx>, location: Location) {
984 self.super_rvalue(rvalue, location);
985 let tcx = self.tcx();
986 let span = self.body.source_info(location).span;
987 match rvalue {
988 Rvalue::Aggregate(ak, ops) => self.check_aggregate_rvalue(rvalue, ak, ops, location),
989
990 Rvalue::Repeat(operand, len) => {
991 let array_ty = rvalue.ty(self.body.local_decls(), tcx);
992 self.prove_predicate(
993 ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(array_ty.into())),
994 Locations::Single(location),
995 ConstraintCategory::Boring,
996 );
997
998 if len.try_to_target_usize(tcx).is_none_or(|len| len > 1) {
1003 match operand {
1004 Operand::Copy(..) | Operand::Constant(..) => {
1005 }
1008 Operand::Move(place) => {
1009 let ty = place.ty(self.body, tcx).ty;
1011 let trait_ref = ty::TraitRef::new(
1012 tcx,
1013 tcx.require_lang_item(LangItem::Copy, span),
1014 [ty],
1015 );
1016
1017 self.prove_trait_ref(
1018 trait_ref,
1019 Locations::Single(location),
1020 ConstraintCategory::CopyBound,
1021 );
1022 }
1023 }
1024 }
1025 }
1026
1027 &Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf, ty) => {
1028 let trait_ref =
1029 ty::TraitRef::new(tcx, tcx.require_lang_item(LangItem::Sized, span), [ty]);
1030
1031 self.prove_trait_ref(
1032 trait_ref,
1033 location.to_locations(),
1034 ConstraintCategory::SizedBound,
1035 );
1036 }
1037 &Rvalue::NullaryOp(NullOp::ContractChecks, _) => {}
1038 &Rvalue::NullaryOp(NullOp::UbChecks, _) => {}
1039
1040 Rvalue::ShallowInitBox(_operand, ty) => {
1041 let trait_ref =
1042 ty::TraitRef::new(tcx, tcx.require_lang_item(LangItem::Sized, span), [*ty]);
1043
1044 self.prove_trait_ref(
1045 trait_ref,
1046 location.to_locations(),
1047 ConstraintCategory::SizedBound,
1048 );
1049 }
1050
1051 Rvalue::Cast(cast_kind, op, ty) => {
1052 match *cast_kind {
1053 CastKind::PointerCoercion(PointerCoercion::ReifyFnPointer, coercion_source) => {
1054 let is_implicit_coercion = coercion_source == CoercionSource::Implicit;
1055 let src_ty = op.ty(self.body, tcx);
1056 let mut src_sig = src_ty.fn_sig(tcx);
1057 if let ty::FnDef(def_id, _) = src_ty.kind()
1058 && let ty::FnPtr(_, target_hdr) = *ty.kind()
1059 && tcx.codegen_fn_attrs(def_id).safe_target_features
1060 && target_hdr.safety.is_safe()
1061 && let Some(safe_sig) = tcx.adjust_target_feature_sig(
1062 *def_id,
1063 src_sig,
1064 self.body.source.def_id(),
1065 )
1066 {
1067 src_sig = safe_sig;
1068 }
1069
1070 if src_sig.has_bound_regions()
1085 && let ty::FnPtr(target_fn_tys, target_hdr) = *ty.kind()
1086 && let target_sig = target_fn_tys.with(target_hdr)
1087 && let Some(target_sig) = target_sig.no_bound_vars()
1088 {
1089 let src_sig = self.infcx.instantiate_binder_with_fresh_vars(
1090 span,
1091 BoundRegionConversionTime::HigherRankedType,
1092 src_sig,
1093 );
1094 let src_ty = Ty::new_fn_ptr(self.tcx(), ty::Binder::dummy(src_sig));
1095 self.prove_predicate(
1096 ty::ClauseKind::WellFormed(src_ty.into()),
1097 location.to_locations(),
1098 ConstraintCategory::Cast { is_implicit_coercion, unsize_to: None },
1099 );
1100
1101 let src_ty = self.normalize(src_ty, location);
1102 if let Err(terr) = self.sub_types(
1103 src_ty,
1104 *ty,
1105 location.to_locations(),
1106 ConstraintCategory::Cast { is_implicit_coercion, unsize_to: None },
1107 ) {
1108 span_mirbug!(
1109 self,
1110 rvalue,
1111 "equating {:?} with {:?} yields {:?}",
1112 target_sig,
1113 src_sig,
1114 terr
1115 );
1116 };
1117 }
1118
1119 let src_ty = Ty::new_fn_ptr(tcx, src_sig);
1120 self.prove_predicate(
1125 ty::ClauseKind::WellFormed(src_ty.into()),
1126 location.to_locations(),
1127 ConstraintCategory::Cast { is_implicit_coercion, unsize_to: None },
1128 );
1129
1130 let src_ty = self.normalize(src_ty, location);
1136 if let Err(terr) = self.sub_types(
1137 src_ty,
1138 *ty,
1139 location.to_locations(),
1140 ConstraintCategory::Cast { is_implicit_coercion, unsize_to: None },
1141 ) {
1142 span_mirbug!(
1143 self,
1144 rvalue,
1145 "equating {:?} with {:?} yields {:?}",
1146 src_ty,
1147 ty,
1148 terr
1149 );
1150 }
1151 }
1152
1153 CastKind::PointerCoercion(
1154 PointerCoercion::ClosureFnPointer(safety),
1155 coercion_source,
1156 ) => {
1157 let sig = match op.ty(self.body, tcx).kind() {
1158 ty::Closure(_, args) => args.as_closure().sig(),
1159 _ => bug!(),
1160 };
1161 let ty_fn_ptr_from =
1162 Ty::new_fn_ptr(tcx, tcx.signature_unclosure(sig, safety));
1163
1164 let is_implicit_coercion = coercion_source == CoercionSource::Implicit;
1165 if let Err(terr) = self.sub_types(
1166 ty_fn_ptr_from,
1167 *ty,
1168 location.to_locations(),
1169 ConstraintCategory::Cast { is_implicit_coercion, unsize_to: None },
1170 ) {
1171 span_mirbug!(
1172 self,
1173 rvalue,
1174 "equating {:?} with {:?} yields {:?}",
1175 ty_fn_ptr_from,
1176 ty,
1177 terr
1178 );
1179 }
1180 }
1181
1182 CastKind::PointerCoercion(
1183 PointerCoercion::UnsafeFnPointer,
1184 coercion_source,
1185 ) => {
1186 let fn_sig = op.ty(self.body, tcx).fn_sig(tcx);
1187
1188 let fn_sig = self.normalize(fn_sig, location);
1194
1195 let ty_fn_ptr_from = tcx.safe_to_unsafe_fn_ty(fn_sig);
1196
1197 let is_implicit_coercion = coercion_source == CoercionSource::Implicit;
1198 if let Err(terr) = self.sub_types(
1199 ty_fn_ptr_from,
1200 *ty,
1201 location.to_locations(),
1202 ConstraintCategory::Cast { is_implicit_coercion, unsize_to: None },
1203 ) {
1204 span_mirbug!(
1205 self,
1206 rvalue,
1207 "equating {:?} with {:?} yields {:?}",
1208 ty_fn_ptr_from,
1209 ty,
1210 terr
1211 );
1212 }
1213 }
1214
1215 CastKind::PointerCoercion(PointerCoercion::Unsize, coercion_source) => {
1216 let &ty = ty;
1217 let trait_ref = ty::TraitRef::new(
1218 tcx,
1219 tcx.require_lang_item(LangItem::CoerceUnsized, span),
1220 [op.ty(self.body, tcx), ty],
1221 );
1222
1223 let is_implicit_coercion = coercion_source == CoercionSource::Implicit;
1224 let unsize_to = fold_regions(tcx, ty, |r, _| {
1225 if let ty::ReVar(_) = r.kind() { tcx.lifetimes.re_erased } else { r }
1226 });
1227 self.prove_trait_ref(
1228 trait_ref,
1229 location.to_locations(),
1230 ConstraintCategory::Cast {
1231 is_implicit_coercion,
1232 unsize_to: Some(unsize_to),
1233 },
1234 );
1235 }
1236
1237 CastKind::PointerCoercion(PointerCoercion::DynStar, coercion_source) => {
1238 let (existential_predicates, region) = match ty.kind() {
1242 Dynamic(predicates, region, ty::DynStar) => (predicates, region),
1243 _ => panic!("Invalid dyn* cast_ty"),
1244 };
1245
1246 let self_ty = op.ty(self.body, tcx);
1247
1248 let is_implicit_coercion = coercion_source == CoercionSource::Implicit;
1249 self.prove_predicates(
1250 existential_predicates
1251 .iter()
1252 .map(|predicate| predicate.with_self_ty(tcx, self_ty)),
1253 location.to_locations(),
1254 ConstraintCategory::Cast { is_implicit_coercion, unsize_to: None },
1255 );
1256
1257 let outlives_predicate = tcx.mk_predicate(Binder::dummy(
1258 ty::PredicateKind::Clause(ty::ClauseKind::TypeOutlives(
1259 ty::OutlivesPredicate(self_ty, *region),
1260 )),
1261 ));
1262 self.prove_predicate(
1263 outlives_predicate,
1264 location.to_locations(),
1265 ConstraintCategory::Cast { is_implicit_coercion, unsize_to: None },
1266 );
1267 }
1268
1269 CastKind::PointerCoercion(
1270 PointerCoercion::MutToConstPointer,
1271 coercion_source,
1272 ) => {
1273 let ty::RawPtr(ty_from, hir::Mutability::Mut) =
1274 op.ty(self.body, tcx).kind()
1275 else {
1276 span_mirbug!(self, rvalue, "unexpected base type for cast {:?}", ty,);
1277 return;
1278 };
1279 let ty::RawPtr(ty_to, hir::Mutability::Not) = ty.kind() else {
1280 span_mirbug!(self, rvalue, "unexpected target type for cast {:?}", ty,);
1281 return;
1282 };
1283 let is_implicit_coercion = coercion_source == CoercionSource::Implicit;
1284 if let Err(terr) = self.sub_types(
1285 *ty_from,
1286 *ty_to,
1287 location.to_locations(),
1288 ConstraintCategory::Cast { is_implicit_coercion, unsize_to: None },
1289 ) {
1290 span_mirbug!(
1291 self,
1292 rvalue,
1293 "relating {:?} with {:?} yields {:?}",
1294 ty_from,
1295 ty_to,
1296 terr
1297 );
1298 }
1299 }
1300
1301 CastKind::PointerCoercion(PointerCoercion::ArrayToPointer, coercion_source) => {
1302 let ty_from = op.ty(self.body, tcx);
1303
1304 let opt_ty_elem_mut = match ty_from.kind() {
1305 ty::RawPtr(array_ty, array_mut) => match array_ty.kind() {
1306 ty::Array(ty_elem, _) => Some((ty_elem, *array_mut)),
1307 _ => None,
1308 },
1309 _ => None,
1310 };
1311
1312 let Some((ty_elem, ty_mut)) = opt_ty_elem_mut else {
1313 span_mirbug!(
1314 self,
1315 rvalue,
1316 "ArrayToPointer cast from unexpected type {:?}",
1317 ty_from,
1318 );
1319 return;
1320 };
1321
1322 let (ty_to, ty_to_mut) = match ty.kind() {
1323 ty::RawPtr(ty_to, ty_to_mut) => (ty_to, *ty_to_mut),
1324 _ => {
1325 span_mirbug!(
1326 self,
1327 rvalue,
1328 "ArrayToPointer cast to unexpected type {:?}",
1329 ty,
1330 );
1331 return;
1332 }
1333 };
1334
1335 if ty_to_mut.is_mut() && ty_mut.is_not() {
1336 span_mirbug!(
1337 self,
1338 rvalue,
1339 "ArrayToPointer cast from const {:?} to mut {:?}",
1340 ty,
1341 ty_to
1342 );
1343 return;
1344 }
1345
1346 let is_implicit_coercion = coercion_source == CoercionSource::Implicit;
1347 if let Err(terr) = self.sub_types(
1348 *ty_elem,
1349 *ty_to,
1350 location.to_locations(),
1351 ConstraintCategory::Cast { is_implicit_coercion, unsize_to: None },
1352 ) {
1353 span_mirbug!(
1354 self,
1355 rvalue,
1356 "relating {:?} with {:?} yields {:?}",
1357 ty_elem,
1358 ty_to,
1359 terr
1360 )
1361 }
1362 }
1363
1364 CastKind::PointerExposeProvenance => {
1365 let ty_from = op.ty(self.body, tcx);
1366 let cast_ty_from = CastTy::from_ty(ty_from);
1367 let cast_ty_to = CastTy::from_ty(*ty);
1368 match (cast_ty_from, cast_ty_to) {
1369 (Some(CastTy::Ptr(_) | CastTy::FnPtr), Some(CastTy::Int(_))) => (),
1370 _ => {
1371 span_mirbug!(
1372 self,
1373 rvalue,
1374 "Invalid PointerExposeProvenance cast {:?} -> {:?}",
1375 ty_from,
1376 ty
1377 )
1378 }
1379 }
1380 }
1381
1382 CastKind::PointerWithExposedProvenance => {
1383 let ty_from = op.ty(self.body, tcx);
1384 let cast_ty_from = CastTy::from_ty(ty_from);
1385 let cast_ty_to = CastTy::from_ty(*ty);
1386 match (cast_ty_from, cast_ty_to) {
1387 (Some(CastTy::Int(_)), Some(CastTy::Ptr(_))) => (),
1388 _ => {
1389 span_mirbug!(
1390 self,
1391 rvalue,
1392 "Invalid PointerWithExposedProvenance cast {:?} -> {:?}",
1393 ty_from,
1394 ty
1395 )
1396 }
1397 }
1398 }
1399 CastKind::IntToInt => {
1400 let ty_from = op.ty(self.body, tcx);
1401 let cast_ty_from = CastTy::from_ty(ty_from);
1402 let cast_ty_to = CastTy::from_ty(*ty);
1403 match (cast_ty_from, cast_ty_to) {
1404 (Some(CastTy::Int(_)), Some(CastTy::Int(_))) => (),
1405 _ => {
1406 span_mirbug!(
1407 self,
1408 rvalue,
1409 "Invalid IntToInt cast {:?} -> {:?}",
1410 ty_from,
1411 ty
1412 )
1413 }
1414 }
1415 }
1416 CastKind::IntToFloat => {
1417 let ty_from = op.ty(self.body, tcx);
1418 let cast_ty_from = CastTy::from_ty(ty_from);
1419 let cast_ty_to = CastTy::from_ty(*ty);
1420 match (cast_ty_from, cast_ty_to) {
1421 (Some(CastTy::Int(_)), Some(CastTy::Float)) => (),
1422 _ => {
1423 span_mirbug!(
1424 self,
1425 rvalue,
1426 "Invalid IntToFloat cast {:?} -> {:?}",
1427 ty_from,
1428 ty
1429 )
1430 }
1431 }
1432 }
1433 CastKind::FloatToInt => {
1434 let ty_from = op.ty(self.body, tcx);
1435 let cast_ty_from = CastTy::from_ty(ty_from);
1436 let cast_ty_to = CastTy::from_ty(*ty);
1437 match (cast_ty_from, cast_ty_to) {
1438 (Some(CastTy::Float), Some(CastTy::Int(_))) => (),
1439 _ => {
1440 span_mirbug!(
1441 self,
1442 rvalue,
1443 "Invalid FloatToInt cast {:?} -> {:?}",
1444 ty_from,
1445 ty
1446 )
1447 }
1448 }
1449 }
1450 CastKind::FloatToFloat => {
1451 let ty_from = op.ty(self.body, tcx);
1452 let cast_ty_from = CastTy::from_ty(ty_from);
1453 let cast_ty_to = CastTy::from_ty(*ty);
1454 match (cast_ty_from, cast_ty_to) {
1455 (Some(CastTy::Float), Some(CastTy::Float)) => (),
1456 _ => {
1457 span_mirbug!(
1458 self,
1459 rvalue,
1460 "Invalid FloatToFloat cast {:?} -> {:?}",
1461 ty_from,
1462 ty
1463 )
1464 }
1465 }
1466 }
1467 CastKind::FnPtrToPtr => {
1468 let ty_from = op.ty(self.body, tcx);
1469 let cast_ty_from = CastTy::from_ty(ty_from);
1470 let cast_ty_to = CastTy::from_ty(*ty);
1471 match (cast_ty_from, cast_ty_to) {
1472 (Some(CastTy::FnPtr), Some(CastTy::Ptr(_))) => (),
1473 _ => {
1474 span_mirbug!(
1475 self,
1476 rvalue,
1477 "Invalid FnPtrToPtr cast {:?} -> {:?}",
1478 ty_from,
1479 ty
1480 )
1481 }
1482 }
1483 }
1484 CastKind::PtrToPtr => {
1485 let ty_from = op.ty(self.body, tcx);
1486 let cast_ty_from = CastTy::from_ty(ty_from);
1487 let cast_ty_to = CastTy::from_ty(*ty);
1488 match (cast_ty_from, cast_ty_to) {
1489 (Some(CastTy::Ptr(src)), Some(CastTy::Ptr(dst))) => {
1490 let src_tail = self.struct_tail(src.ty, location);
1491 let dst_tail = self.struct_tail(dst.ty, location);
1492
1493 if let ty::Dynamic(src_tty, _src_lt, ty::Dyn) = *src_tail.kind()
1500 && let ty::Dynamic(dst_tty, dst_lt, ty::Dyn) = *dst_tail.kind()
1501 && src_tty.principal().is_some()
1502 && dst_tty.principal().is_some()
1503 {
1504 let src_obj = Ty::new_dynamic(
1507 tcx,
1508 tcx.mk_poly_existential_predicates(
1509 &src_tty.without_auto_traits().collect::<Vec<_>>(),
1510 ),
1511 dst_lt,
1514 ty::Dyn,
1515 );
1516 let dst_obj = Ty::new_dynamic(
1517 tcx,
1518 tcx.mk_poly_existential_predicates(
1519 &dst_tty.without_auto_traits().collect::<Vec<_>>(),
1520 ),
1521 dst_lt,
1522 ty::Dyn,
1523 );
1524
1525 debug!(?src_tty, ?dst_tty, ?src_obj, ?dst_obj);
1526
1527 self.sub_types(
1528 src_obj,
1529 dst_obj,
1530 location.to_locations(),
1531 ConstraintCategory::Cast {
1532 is_implicit_coercion: false,
1533 unsize_to: None,
1534 },
1535 )
1536 .unwrap();
1537 }
1538 }
1539 _ => {
1540 span_mirbug!(
1541 self,
1542 rvalue,
1543 "Invalid PtrToPtr cast {:?} -> {:?}",
1544 ty_from,
1545 ty
1546 )
1547 }
1548 }
1549 }
1550 CastKind::Transmute => {
1551 let ty_from = op.ty(self.body, tcx);
1552 match ty_from.kind() {
1553 ty::Pat(base, _) if base == ty => {}
1554 _ => span_mirbug!(
1555 self,
1556 rvalue,
1557 "Unexpected CastKind::Transmute {ty_from:?} -> {ty:?}, which is not permitted in Analysis MIR",
1558 ),
1559 }
1560 }
1561 }
1562 }
1563
1564 Rvalue::Ref(region, _borrow_kind, borrowed_place) => {
1565 self.add_reborrow_constraint(location, *region, borrowed_place);
1566 }
1567
1568 Rvalue::BinaryOp(
1569 BinOp::Eq | BinOp::Ne | BinOp::Lt | BinOp::Le | BinOp::Gt | BinOp::Ge,
1570 box (left, right),
1571 ) => {
1572 let ty_left = left.ty(self.body, tcx);
1573 match ty_left.kind() {
1574 ty::RawPtr(_, _) | ty::FnPtr(..) => {
1576 let ty_right = right.ty(self.body, tcx);
1577 let common_ty =
1578 self.infcx.next_ty_var(self.body.source_info(location).span);
1579 self.sub_types(
1580 ty_left,
1581 common_ty,
1582 location.to_locations(),
1583 ConstraintCategory::CallArgument(None),
1584 )
1585 .unwrap_or_else(|err| {
1586 bug!("Could not equate type variable with {:?}: {:?}", ty_left, err)
1587 });
1588 if let Err(terr) = self.sub_types(
1589 ty_right,
1590 common_ty,
1591 location.to_locations(),
1592 ConstraintCategory::CallArgument(None),
1593 ) {
1594 span_mirbug!(
1595 self,
1596 rvalue,
1597 "unexpected comparison types {:?} and {:?} yields {:?}",
1598 ty_left,
1599 ty_right,
1600 terr
1601 )
1602 }
1603 }
1604 ty::Int(_) | ty::Uint(_) | ty::Bool | ty::Char | ty::Float(_)
1607 if ty_left == right.ty(self.body, tcx) => {}
1608 _ => span_mirbug!(
1611 self,
1612 rvalue,
1613 "unexpected comparison types {:?} and {:?}",
1614 ty_left,
1615 right.ty(self.body, tcx)
1616 ),
1617 }
1618 }
1619
1620 Rvalue::WrapUnsafeBinder(op, ty) => {
1621 let operand_ty = op.ty(self.body, self.tcx());
1622 let ty::UnsafeBinder(binder_ty) = *ty.kind() else {
1623 unreachable!();
1624 };
1625 let expected_ty = self.infcx.instantiate_binder_with_fresh_vars(
1626 self.body().source_info(location).span,
1627 BoundRegionConversionTime::HigherRankedType,
1628 binder_ty.into(),
1629 );
1630 self.sub_types(
1631 operand_ty,
1632 expected_ty,
1633 location.to_locations(),
1634 ConstraintCategory::Boring,
1635 )
1636 .unwrap();
1637 }
1638
1639 Rvalue::Use(_)
1640 | Rvalue::UnaryOp(_, _)
1641 | Rvalue::CopyForDeref(_)
1642 | Rvalue::BinaryOp(..)
1643 | Rvalue::RawPtr(..)
1644 | Rvalue::ThreadLocalRef(..)
1645 | Rvalue::Len(..)
1646 | Rvalue::Discriminant(..)
1647 | Rvalue::NullaryOp(NullOp::OffsetOf(..), _) => {}
1648 }
1649 }
1650
1651 #[instrument(level = "debug", skip(self))]
1652 fn visit_operand(&mut self, op: &Operand<'tcx>, location: Location) {
1653 self.super_operand(op, location);
1654 if let Operand::Constant(constant) = op {
1655 let maybe_uneval = match constant.const_ {
1656 Const::Val(..) | Const::Ty(_, _) => None,
1657 Const::Unevaluated(uv, _) => Some(uv),
1658 };
1659
1660 if let Some(uv) = maybe_uneval {
1661 if uv.promoted.is_none() {
1662 let tcx = self.tcx();
1663 let def_id = uv.def;
1664 if tcx.def_kind(def_id) == DefKind::InlineConst {
1665 let def_id = def_id.expect_local();
1666 let predicates = self.prove_closure_bounds(
1667 tcx,
1668 def_id,
1669 uv.args,
1670 location.to_locations(),
1671 );
1672 self.normalize_and_prove_instantiated_predicates(
1673 def_id.to_def_id(),
1674 predicates,
1675 location.to_locations(),
1676 );
1677 }
1678 }
1679 }
1680 }
1681 }
1682
1683 #[instrument(level = "debug", skip(self))]
1684 fn visit_const_operand(&mut self, constant: &ConstOperand<'tcx>, location: Location) {
1685 self.super_const_operand(constant, location);
1686 let ty = constant.const_.ty();
1687
1688 self.infcx.tcx.for_each_free_region(&ty, |live_region| {
1689 let live_region_vid = self.universal_regions.to_region_vid(live_region);
1690 self.constraints.liveness_constraints.add_location(live_region_vid, location);
1691 });
1692
1693 let locations = location.to_locations();
1694 if let Some(annotation_index) = constant.user_ty {
1695 if let Err(terr) = self.relate_type_and_user_type(
1696 constant.const_.ty(),
1697 ty::Invariant,
1698 &UserTypeProjection { base: annotation_index, projs: vec![] },
1699 locations,
1700 ConstraintCategory::TypeAnnotation(AnnotationSource::GenericArg),
1701 ) {
1702 let annotation = &self.user_type_annotations[annotation_index];
1703 span_mirbug!(
1704 self,
1705 constant,
1706 "bad constant user type {:?} vs {:?}: {:?}",
1707 annotation,
1708 constant.const_.ty(),
1709 terr,
1710 );
1711 }
1712 } else {
1713 let tcx = self.tcx();
1714 let maybe_uneval = match constant.const_ {
1715 Const::Ty(_, ct) => match ct.kind() {
1716 ty::ConstKind::Unevaluated(uv) => {
1717 Some(UnevaluatedConst { def: uv.def, args: uv.args, promoted: None })
1718 }
1719 _ => None,
1720 },
1721 Const::Unevaluated(uv, _) => Some(uv),
1722 _ => None,
1723 };
1724
1725 if let Some(uv) = maybe_uneval {
1726 if let Some(promoted) = uv.promoted {
1727 let promoted_body = &self.promoted[promoted];
1728 self.check_promoted(promoted_body, location);
1729 let promoted_ty = promoted_body.return_ty();
1730 if let Err(terr) =
1731 self.eq_types(ty, promoted_ty, locations, ConstraintCategory::Boring)
1732 {
1733 span_mirbug!(
1734 self,
1735 promoted,
1736 "bad promoted type ({:?}: {:?}): {:?}",
1737 ty,
1738 promoted_ty,
1739 terr
1740 );
1741 };
1742 } else {
1743 self.ascribe_user_type(
1744 constant.const_.ty(),
1745 ty::UserType::new(ty::UserTypeKind::TypeOf(
1746 uv.def,
1747 UserArgs { args: uv.args, user_self_ty: None },
1748 )),
1749 locations.span(self.body),
1750 );
1751 }
1752 } else if let Some(static_def_id) = constant.check_static_ptr(tcx) {
1753 let unnormalized_ty = tcx.type_of(static_def_id).instantiate_identity();
1754 let normalized_ty = self.normalize(unnormalized_ty, locations);
1755 let literal_ty = constant.const_.ty().builtin_deref(true).unwrap();
1756
1757 if let Err(terr) =
1758 self.eq_types(literal_ty, normalized_ty, locations, ConstraintCategory::Boring)
1759 {
1760 span_mirbug!(self, constant, "bad static type {:?} ({:?})", constant, terr);
1761 }
1762 } else if let Const::Ty(_, ct) = constant.const_
1763 && let ty::ConstKind::Param(p) = ct.kind()
1764 {
1765 let body_def_id = self.universal_regions.defining_ty.def_id();
1766 let const_param = tcx.generics_of(body_def_id).const_param(p, tcx);
1767 self.ascribe_user_type(
1768 constant.const_.ty(),
1769 ty::UserType::new(ty::UserTypeKind::TypeOf(
1770 const_param.def_id,
1771 UserArgs {
1772 args: self.universal_regions.defining_ty.args(),
1773 user_self_ty: None,
1774 },
1775 )),
1776 locations.span(self.body),
1777 );
1778 }
1779
1780 if let ty::FnDef(def_id, args) = *constant.const_.ty().kind() {
1781 let instantiated_predicates = tcx.predicates_of(def_id).instantiate(tcx, args);
1782 self.normalize_and_prove_instantiated_predicates(
1783 def_id,
1784 instantiated_predicates,
1785 locations,
1786 );
1787
1788 assert!(!matches!(
1789 tcx.impl_of_method(def_id).map(|imp| tcx.def_kind(imp)),
1790 Some(DefKind::Impl { of_trait: true })
1791 ));
1792 self.prove_predicates(
1793 args.types().map(|ty| ty::ClauseKind::WellFormed(ty.into())),
1794 locations,
1795 ConstraintCategory::Boring,
1796 );
1797 }
1798 }
1799 }
1800
1801 fn visit_place(&mut self, place: &Place<'tcx>, context: PlaceContext, location: Location) {
1802 self.super_place(place, context, location);
1803 let tcx = self.tcx();
1804 let place_ty = place.ty(self.body, tcx);
1805 if let PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy) = context {
1806 let trait_ref = ty::TraitRef::new(
1807 tcx,
1808 tcx.require_lang_item(LangItem::Copy, self.last_span),
1809 [place_ty.ty],
1810 );
1811
1812 self.prove_trait_ref(trait_ref, location.to_locations(), ConstraintCategory::CopyBound);
1824 }
1825 }
1826
1827 fn visit_projection_elem(
1828 &mut self,
1829 place: PlaceRef<'tcx>,
1830 elem: PlaceElem<'tcx>,
1831 context: PlaceContext,
1832 location: Location,
1833 ) {
1834 let tcx = self.tcx();
1835 let base_ty = place.ty(self.body(), tcx);
1836 match elem {
1837 ProjectionElem::Deref
1840 | ProjectionElem::Index(_)
1841 | ProjectionElem::ConstantIndex { .. }
1842 | ProjectionElem::Subslice { .. }
1843 | ProjectionElem::Downcast(..) => {}
1844 ProjectionElem::Field(field, fty) => {
1845 let fty = self.normalize(fty, location);
1846 let ty = PlaceTy::field_ty(tcx, base_ty.ty, base_ty.variant_index, field);
1847 let ty = self.normalize(ty, location);
1848 debug!(?fty, ?ty);
1849
1850 if let Err(terr) = self.relate_types(
1851 ty,
1852 context.ambient_variance(),
1853 fty,
1854 location.to_locations(),
1855 ConstraintCategory::Boring,
1856 ) {
1857 span_mirbug!(self, place, "bad field access ({:?}: {:?}): {:?}", ty, fty, terr);
1858 }
1859 }
1860 ProjectionElem::OpaqueCast(ty) => {
1861 let ty = self.normalize(ty, location);
1862 self.relate_types(
1863 ty,
1864 context.ambient_variance(),
1865 base_ty.ty,
1866 location.to_locations(),
1867 ConstraintCategory::TypeAnnotation(AnnotationSource::OpaqueCast),
1868 )
1869 .unwrap();
1870 }
1871 ProjectionElem::UnwrapUnsafeBinder(ty) => {
1872 let ty::UnsafeBinder(binder_ty) = *base_ty.ty.kind() else {
1873 unreachable!();
1874 };
1875 let found_ty = self.infcx.instantiate_binder_with_fresh_vars(
1876 self.body.source_info(location).span,
1877 BoundRegionConversionTime::HigherRankedType,
1878 binder_ty.into(),
1879 );
1880 self.relate_types(
1881 ty,
1882 context.ambient_variance(),
1883 found_ty,
1884 location.to_locations(),
1885 ConstraintCategory::Boring,
1886 )
1887 .unwrap();
1888 }
1889 ProjectionElem::Subtype(_) => {
1890 bug!("ProjectionElem::Subtype shouldn't exist in borrowck")
1891 }
1892 }
1893 }
1894}
1895
1896impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
1897 fn check_call_dest(
1898 &mut self,
1899 term: &Terminator<'tcx>,
1900 sig: &ty::FnSig<'tcx>,
1901 destination: Place<'tcx>,
1902 target: Option<BasicBlock>,
1903 term_location: Location,
1904 ) {
1905 let tcx = self.tcx();
1906 match target {
1907 Some(_) => {
1908 let dest_ty = destination.ty(self.body, tcx).ty;
1909 let dest_ty = self.normalize(dest_ty, term_location);
1910 let category = match destination.as_local() {
1911 Some(RETURN_PLACE) => {
1912 if let DefiningTy::Const(def_id, _) | DefiningTy::InlineConst(def_id, _) =
1913 self.universal_regions.defining_ty
1914 {
1915 if tcx.is_static(def_id) {
1916 ConstraintCategory::UseAsStatic
1917 } else {
1918 ConstraintCategory::UseAsConst
1919 }
1920 } else {
1921 ConstraintCategory::Return(ReturnConstraint::Normal)
1922 }
1923 }
1924 Some(l) if !self.body.local_decls[l].is_user_variable() => {
1925 ConstraintCategory::Boring
1926 }
1927 _ => ConstraintCategory::Assignment,
1929 };
1930
1931 let locations = term_location.to_locations();
1932
1933 if let Err(terr) = self.sub_types(sig.output(), dest_ty, locations, category) {
1934 span_mirbug!(
1935 self,
1936 term,
1937 "call dest mismatch ({:?} <- {:?}): {:?}",
1938 dest_ty,
1939 sig.output(),
1940 terr
1941 );
1942 }
1943
1944 if self.unsized_feature_enabled() {
1947 let span = term.source_info.span;
1948 self.ensure_place_sized(dest_ty, span);
1949 }
1950 }
1951 None => {
1952 let output_ty = self.tcx().erase_regions(sig.output());
1955 if !output_ty.is_privately_uninhabited(
1956 self.tcx(),
1957 self.infcx.typing_env(self.infcx.param_env),
1958 ) {
1959 span_mirbug!(self, term, "call to converging function {:?} w/o dest", sig);
1960 }
1961 }
1962 }
1963 }
1964
1965 #[instrument(level = "debug", skip(self, term, func, term_location, call_source))]
1966 fn check_call_inputs(
1967 &mut self,
1968 term: &Terminator<'tcx>,
1969 func: &Operand<'tcx>,
1970 sig: &ty::FnSig<'tcx>,
1971 args: &[Spanned<Operand<'tcx>>],
1972 term_location: Location,
1973 call_source: CallSource,
1974 ) {
1975 if args.len() < sig.inputs().len() || (args.len() > sig.inputs().len() && !sig.c_variadic) {
1976 span_mirbug!(self, term, "call to {:?} with wrong # of args", sig);
1977 }
1978
1979 let func_ty = func.ty(self.body, self.infcx.tcx);
1980 if let ty::FnDef(def_id, _) = *func_ty.kind() {
1981 if let Some(name @ (sym::simd_shuffle | sym::simd_insert | sym::simd_extract)) =
1985 self.tcx().intrinsic(def_id).map(|i| i.name)
1986 {
1987 let idx = match name {
1988 sym::simd_shuffle => 2,
1989 _ => 1,
1990 };
1991 if !matches!(args[idx], Spanned { node: Operand::Constant(_), .. }) {
1992 self.tcx().dcx().emit_err(SimdIntrinsicArgConst {
1993 span: term.source_info.span,
1994 arg: idx + 1,
1995 intrinsic: name.to_string(),
1996 });
1997 }
1998 }
1999 }
2000 debug!(?func_ty);
2001
2002 for (n, (fn_arg, op_arg)) in iter::zip(sig.inputs(), args).enumerate() {
2003 let op_arg_ty = op_arg.node.ty(self.body, self.tcx());
2004
2005 let op_arg_ty = self.normalize(op_arg_ty, term_location);
2006 let category = if call_source.from_hir_call() {
2007 ConstraintCategory::CallArgument(Some(self.infcx.tcx.erase_regions(func_ty)))
2008 } else {
2009 ConstraintCategory::Boring
2010 };
2011 if let Err(terr) =
2012 self.sub_types(op_arg_ty, *fn_arg, term_location.to_locations(), category)
2013 {
2014 span_mirbug!(
2015 self,
2016 term,
2017 "bad arg #{:?} ({:?} <- {:?}): {:?}",
2018 n,
2019 fn_arg,
2020 op_arg_ty,
2021 terr
2022 );
2023 }
2024 }
2025 }
2026
2027 fn check_iscleanup(&mut self, block_data: &BasicBlockData<'tcx>) {
2028 let is_cleanup = block_data.is_cleanup;
2029 match block_data.terminator().kind {
2030 TerminatorKind::Goto { target } => {
2031 self.assert_iscleanup(block_data, target, is_cleanup)
2032 }
2033 TerminatorKind::SwitchInt { ref targets, .. } => {
2034 for target in targets.all_targets() {
2035 self.assert_iscleanup(block_data, *target, is_cleanup);
2036 }
2037 }
2038 TerminatorKind::UnwindResume => {
2039 if !is_cleanup {
2040 span_mirbug!(self, block_data, "resume on non-cleanup block!")
2041 }
2042 }
2043 TerminatorKind::UnwindTerminate(_) => {
2044 if !is_cleanup {
2045 span_mirbug!(self, block_data, "terminate on non-cleanup block!")
2046 }
2047 }
2048 TerminatorKind::Return => {
2049 if is_cleanup {
2050 span_mirbug!(self, block_data, "return on cleanup block")
2051 }
2052 }
2053 TerminatorKind::TailCall { .. } => {
2054 if is_cleanup {
2055 span_mirbug!(self, block_data, "tailcall on cleanup block")
2056 }
2057 }
2058 TerminatorKind::CoroutineDrop { .. } => {
2059 if is_cleanup {
2060 span_mirbug!(self, block_data, "coroutine_drop in cleanup block")
2061 }
2062 }
2063 TerminatorKind::Yield { resume, drop, .. } => {
2064 if is_cleanup {
2065 span_mirbug!(self, block_data, "yield in cleanup block")
2066 }
2067 self.assert_iscleanup(block_data, resume, is_cleanup);
2068 if let Some(drop) = drop {
2069 self.assert_iscleanup(block_data, drop, is_cleanup);
2070 }
2071 }
2072 TerminatorKind::Unreachable => {}
2073 TerminatorKind::Drop { target, unwind, drop, .. } => {
2074 self.assert_iscleanup(block_data, target, is_cleanup);
2075 self.assert_iscleanup_unwind(block_data, unwind, is_cleanup);
2076 if let Some(drop) = drop {
2077 self.assert_iscleanup(block_data, drop, is_cleanup);
2078 }
2079 }
2080 TerminatorKind::Assert { target, unwind, .. } => {
2081 self.assert_iscleanup(block_data, target, is_cleanup);
2082 self.assert_iscleanup_unwind(block_data, unwind, is_cleanup);
2083 }
2084 TerminatorKind::Call { ref target, unwind, .. } => {
2085 if let &Some(target) = target {
2086 self.assert_iscleanup(block_data, target, is_cleanup);
2087 }
2088 self.assert_iscleanup_unwind(block_data, unwind, is_cleanup);
2089 }
2090 TerminatorKind::FalseEdge { real_target, imaginary_target } => {
2091 self.assert_iscleanup(block_data, real_target, is_cleanup);
2092 self.assert_iscleanup(block_data, imaginary_target, is_cleanup);
2093 }
2094 TerminatorKind::FalseUnwind { real_target, unwind } => {
2095 self.assert_iscleanup(block_data, real_target, is_cleanup);
2096 self.assert_iscleanup_unwind(block_data, unwind, is_cleanup);
2097 }
2098 TerminatorKind::InlineAsm { ref targets, unwind, .. } => {
2099 for &target in targets {
2100 self.assert_iscleanup(block_data, target, is_cleanup);
2101 }
2102 self.assert_iscleanup_unwind(block_data, unwind, is_cleanup);
2103 }
2104 }
2105 }
2106
2107 fn assert_iscleanup(&mut self, ctxt: &dyn fmt::Debug, bb: BasicBlock, iscleanuppad: bool) {
2108 if self.body[bb].is_cleanup != iscleanuppad {
2109 span_mirbug!(self, ctxt, "cleanuppad mismatch: {:?} should be {:?}", bb, iscleanuppad);
2110 }
2111 }
2112
2113 fn assert_iscleanup_unwind(
2114 &mut self,
2115 ctxt: &dyn fmt::Debug,
2116 unwind: UnwindAction,
2117 is_cleanup: bool,
2118 ) {
2119 match unwind {
2120 UnwindAction::Cleanup(unwind) => {
2121 if is_cleanup {
2122 span_mirbug!(self, ctxt, "unwind on cleanup block")
2123 }
2124 self.assert_iscleanup(ctxt, unwind, true);
2125 }
2126 UnwindAction::Continue => {
2127 if is_cleanup {
2128 span_mirbug!(self, ctxt, "unwind on cleanup block")
2129 }
2130 }
2131 UnwindAction::Unreachable | UnwindAction::Terminate(_) => (),
2132 }
2133 }
2134
2135 fn ensure_place_sized(&mut self, ty: Ty<'tcx>, span: Span) {
2136 let tcx = self.tcx();
2137
2138 let erased_ty = tcx.erase_regions(ty);
2142 if !erased_ty.is_sized(tcx, self.infcx.typing_env(self.infcx.param_env)) {
2144 if self.reported_errors.replace((ty, span)).is_none() {
2149 self.tcx().dcx().emit_err(MoveUnsized { ty, span });
2153 }
2154 }
2155 }
2156
2157 fn aggregate_field_ty(
2158 &mut self,
2159 ak: &AggregateKind<'tcx>,
2160 field_index: FieldIdx,
2161 location: Location,
2162 ) -> Result<Ty<'tcx>, FieldAccessError> {
2163 let tcx = self.tcx();
2164
2165 match *ak {
2166 AggregateKind::Adt(adt_did, variant_index, args, _, active_field_index) => {
2167 let def = tcx.adt_def(adt_did);
2168 let variant = &def.variant(variant_index);
2169 let adj_field_index = active_field_index.unwrap_or(field_index);
2170 if let Some(field) = variant.fields.get(adj_field_index) {
2171 Ok(self.normalize(field.ty(tcx, args), location))
2172 } else {
2173 Err(FieldAccessError::OutOfRange { field_count: variant.fields.len() })
2174 }
2175 }
2176 AggregateKind::Closure(_, args) => {
2177 match args.as_closure().upvar_tys().get(field_index.as_usize()) {
2178 Some(ty) => Ok(*ty),
2179 None => Err(FieldAccessError::OutOfRange {
2180 field_count: args.as_closure().upvar_tys().len(),
2181 }),
2182 }
2183 }
2184 AggregateKind::Coroutine(_, args) => {
2185 match args.as_coroutine().prefix_tys().get(field_index.as_usize()) {
2189 Some(ty) => Ok(*ty),
2190 None => Err(FieldAccessError::OutOfRange {
2191 field_count: args.as_coroutine().prefix_tys().len(),
2192 }),
2193 }
2194 }
2195 AggregateKind::CoroutineClosure(_, args) => {
2196 match args.as_coroutine_closure().upvar_tys().get(field_index.as_usize()) {
2197 Some(ty) => Ok(*ty),
2198 None => Err(FieldAccessError::OutOfRange {
2199 field_count: args.as_coroutine_closure().upvar_tys().len(),
2200 }),
2201 }
2202 }
2203 AggregateKind::Array(ty) => Ok(ty),
2204 AggregateKind::Tuple | AggregateKind::RawPtr(..) => {
2205 unreachable!("This should have been covered in check_rvalues");
2206 }
2207 }
2208 }
2209
2210 fn rvalue_user_ty(&self, rvalue: &Rvalue<'tcx>) -> Option<UserTypeAnnotationIndex> {
2214 match rvalue {
2215 Rvalue::Use(_)
2216 | Rvalue::ThreadLocalRef(_)
2217 | Rvalue::Repeat(..)
2218 | Rvalue::Ref(..)
2219 | Rvalue::RawPtr(..)
2220 | Rvalue::Len(..)
2221 | Rvalue::Cast(..)
2222 | Rvalue::ShallowInitBox(..)
2223 | Rvalue::BinaryOp(..)
2224 | Rvalue::NullaryOp(..)
2225 | Rvalue::CopyForDeref(..)
2226 | Rvalue::UnaryOp(..)
2227 | Rvalue::Discriminant(..)
2228 | Rvalue::WrapUnsafeBinder(..) => None,
2229
2230 Rvalue::Aggregate(aggregate, _) => match **aggregate {
2231 AggregateKind::Adt(_, _, _, user_ty, _) => user_ty,
2232 AggregateKind::Array(_) => None,
2233 AggregateKind::Tuple => None,
2234 AggregateKind::Closure(_, _) => None,
2235 AggregateKind::Coroutine(_, _) => None,
2236 AggregateKind::CoroutineClosure(_, _) => None,
2237 AggregateKind::RawPtr(_, _) => None,
2238 },
2239 }
2240 }
2241
2242 fn check_aggregate_rvalue(
2243 &mut self,
2244 rvalue: &Rvalue<'tcx>,
2245 aggregate_kind: &AggregateKind<'tcx>,
2246 operands: &IndexSlice<FieldIdx, Operand<'tcx>>,
2247 location: Location,
2248 ) {
2249 let tcx = self.tcx();
2250
2251 self.prove_aggregate_predicates(aggregate_kind, location);
2252
2253 if *aggregate_kind == AggregateKind::Tuple {
2254 return;
2256 }
2257
2258 if let AggregateKind::RawPtr(..) = aggregate_kind {
2259 bug!("RawPtr should only be in runtime MIR");
2260 }
2261
2262 for (i, operand) in operands.iter_enumerated() {
2263 let field_ty = match self.aggregate_field_ty(aggregate_kind, i, location) {
2264 Ok(field_ty) => field_ty,
2265 Err(FieldAccessError::OutOfRange { field_count }) => {
2266 span_mirbug!(
2267 self,
2268 rvalue,
2269 "accessed field #{} but variant only has {}",
2270 i.as_u32(),
2271 field_count,
2272 );
2273 continue;
2274 }
2275 };
2276 let operand_ty = operand.ty(self.body, tcx);
2277 let operand_ty = self.normalize(operand_ty, location);
2278
2279 if let Err(terr) = self.sub_types(
2280 operand_ty,
2281 field_ty,
2282 location.to_locations(),
2283 ConstraintCategory::Boring,
2284 ) {
2285 span_mirbug!(
2286 self,
2287 rvalue,
2288 "{:?} is not a subtype of {:?}: {:?}",
2289 operand_ty,
2290 field_ty,
2291 terr
2292 );
2293 }
2294 }
2295 }
2296
2297 fn add_reborrow_constraint(
2305 &mut self,
2306 location: Location,
2307 borrow_region: ty::Region<'tcx>,
2308 borrowed_place: &Place<'tcx>,
2309 ) {
2310 let Self { borrow_set, location_table, polonius_facts, constraints, .. } = self;
2312
2313 if let Some(polonius_facts) = polonius_facts {
2319 let _prof_timer = self.infcx.tcx.prof.generic_activity("polonius_fact_generation");
2320 if let Some(borrow_index) = borrow_set.get_index_of(&location) {
2321 let region_vid = borrow_region.as_var();
2322 polonius_facts.loan_issued_at.push((
2323 region_vid.into(),
2324 borrow_index,
2325 location_table.mid_index(location),
2326 ));
2327 }
2328 }
2329
2330 debug!(
2336 "add_reborrow_constraint({:?}, {:?}, {:?})",
2337 location, borrow_region, borrowed_place
2338 );
2339
2340 let tcx = self.infcx.tcx;
2341 let def = self.body.source.def_id().expect_local();
2342 let upvars = tcx.closure_captures(def);
2343 let field =
2344 path_utils::is_upvar_field_projection(tcx, upvars, borrowed_place.as_ref(), self.body);
2345 let category = if let Some(field) = field {
2346 ConstraintCategory::ClosureUpvar(field)
2347 } else {
2348 ConstraintCategory::Boring
2349 };
2350
2351 for (base, elem) in borrowed_place.as_ref().iter_projections().rev() {
2352 debug!("add_reborrow_constraint - iteration {:?}", elem);
2353
2354 match elem {
2355 ProjectionElem::Deref => {
2356 let base_ty = base.ty(self.body, tcx).ty;
2357
2358 debug!("add_reborrow_constraint - base_ty = {:?}", base_ty);
2359 match base_ty.kind() {
2360 ty::Ref(ref_region, _, mutbl) => {
2361 constraints.outlives_constraints.push(OutlivesConstraint {
2362 sup: ref_region.as_var(),
2363 sub: borrow_region.as_var(),
2364 locations: location.to_locations(),
2365 span: location.to_locations().span(self.body),
2366 category,
2367 variance_info: ty::VarianceDiagInfo::default(),
2368 from_closure: false,
2369 });
2370
2371 match mutbl {
2372 hir::Mutability::Not => {
2373 break;
2377 }
2378 hir::Mutability::Mut => {
2379 }
2401 }
2402 }
2403 ty::RawPtr(..) => {
2404 break;
2406 }
2407 ty::Adt(def, _) if def.is_box() => {
2408 }
2410 _ => bug!("unexpected deref ty {:?} in {:?}", base_ty, borrowed_place),
2411 }
2412 }
2413 ProjectionElem::Field(..)
2414 | ProjectionElem::Downcast(..)
2415 | ProjectionElem::OpaqueCast(..)
2416 | ProjectionElem::Index(..)
2417 | ProjectionElem::ConstantIndex { .. }
2418 | ProjectionElem::Subslice { .. }
2419 | ProjectionElem::UnwrapUnsafeBinder(_) => {
2420 }
2422 ProjectionElem::Subtype(_) => {
2423 bug!("ProjectionElem::Subtype shouldn't exist in borrowck")
2424 }
2425 }
2426 }
2427 }
2428
2429 fn prove_aggregate_predicates(
2430 &mut self,
2431 aggregate_kind: &AggregateKind<'tcx>,
2432 location: Location,
2433 ) {
2434 let tcx = self.tcx();
2435
2436 debug!(
2437 "prove_aggregate_predicates(aggregate_kind={:?}, location={:?})",
2438 aggregate_kind, location
2439 );
2440
2441 let (def_id, instantiated_predicates) = match *aggregate_kind {
2442 AggregateKind::Adt(adt_did, _, args, _, _) => {
2443 (adt_did, tcx.predicates_of(adt_did).instantiate(tcx, args))
2444 }
2445
2446 AggregateKind::Closure(def_id, args)
2466 | AggregateKind::CoroutineClosure(def_id, args)
2467 | AggregateKind::Coroutine(def_id, args) => (
2468 def_id,
2469 self.prove_closure_bounds(
2470 tcx,
2471 def_id.expect_local(),
2472 args,
2473 location.to_locations(),
2474 ),
2475 ),
2476
2477 AggregateKind::Array(_) | AggregateKind::Tuple | AggregateKind::RawPtr(..) => {
2478 (CRATE_DEF_ID.to_def_id(), ty::InstantiatedPredicates::empty())
2479 }
2480 };
2481
2482 self.normalize_and_prove_instantiated_predicates(
2483 def_id,
2484 instantiated_predicates,
2485 location.to_locations(),
2486 );
2487 }
2488
2489 fn prove_closure_bounds(
2490 &mut self,
2491 tcx: TyCtxt<'tcx>,
2492 def_id: LocalDefId,
2493 args: GenericArgsRef<'tcx>,
2494 locations: Locations,
2495 ) -> ty::InstantiatedPredicates<'tcx> {
2496 if let Some(closure_requirements) = &self.root_cx.closure_requirements(def_id) {
2497 constraint_conversion::ConstraintConversion::new(
2498 self.infcx,
2499 self.universal_regions,
2500 self.region_bound_pairs,
2501 self.infcx.param_env,
2502 self.known_type_outlives_obligations,
2503 locations,
2504 self.body.span, ConstraintCategory::Boring, self.constraints,
2507 )
2508 .apply_closure_requirements(closure_requirements, def_id, args);
2509 }
2510
2511 let typeck_root_def_id = tcx.typeck_root_def_id(self.body.source.def_id());
2513 let typeck_root_args = ty::GenericArgs::identity_for_item(tcx, typeck_root_def_id);
2514
2515 let parent_args = match tcx.def_kind(def_id) {
2516 DefKind::Closure => {
2520 &args[..typeck_root_args.len()]
2524 }
2525 DefKind::InlineConst => args.as_inline_const().parent_args(),
2526 other => bug!("unexpected item {:?}", other),
2527 };
2528 let parent_args = tcx.mk_args(parent_args);
2529
2530 assert_eq!(typeck_root_args.len(), parent_args.len());
2531 if let Err(_) = self.eq_args(
2532 typeck_root_args,
2533 parent_args,
2534 locations,
2535 ConstraintCategory::BoringNoLocation,
2536 ) {
2537 span_mirbug!(
2538 self,
2539 def_id,
2540 "could not relate closure to parent {:?} != {:?}",
2541 typeck_root_args,
2542 parent_args
2543 );
2544 }
2545
2546 tcx.predicates_of(def_id).instantiate(tcx, args)
2547 }
2548}
2549
2550trait NormalizeLocation: fmt::Debug + Copy {
2551 fn to_locations(self) -> Locations;
2552}
2553
2554impl NormalizeLocation for Locations {
2555 fn to_locations(self) -> Locations {
2556 self
2557 }
2558}
2559
2560impl NormalizeLocation for Location {
2561 fn to_locations(self) -> Locations {
2562 Locations::Single(self)
2563 }
2564}
2565
2566#[derive(Debug)]
2570pub(super) struct InstantiateOpaqueType<'tcx> {
2571 pub base_universe: Option<ty::UniverseIndex>,
2572 pub region_constraints: Option<RegionConstraintData<'tcx>>,
2573 pub obligations: PredicateObligations<'tcx>,
2574}
2575
2576impl<'tcx> TypeOp<'tcx> for InstantiateOpaqueType<'tcx> {
2577 type Output = ();
2578 type ErrorInfo = InstantiateOpaqueType<'tcx>;
2584
2585 fn fully_perform(
2586 mut self,
2587 infcx: &InferCtxt<'tcx>,
2588 span: Span,
2589 ) -> Result<TypeOpOutput<'tcx, Self>, ErrorGuaranteed> {
2590 let (mut output, region_constraints) = scrape_region_constraints(
2591 infcx,
2592 |ocx| {
2593 ocx.register_obligations(self.obligations.clone());
2594 Ok(())
2595 },
2596 "InstantiateOpaqueType",
2597 span,
2598 )?;
2599 self.region_constraints = Some(region_constraints);
2600 output.error_info = Some(self);
2601 Ok(output)
2602 }
2603}