]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_borrowck/src/type_check/mod.rs
Rollup merge of #95953 - JakobDegen:repeat-leak, r=oli-obk
[rust.git] / compiler / rustc_borrowck / src / type_check / mod.rs
1 //! This pass type-checks the MIR to ensure it is not broken.
2
3 use std::rc::Rc;
4 use std::{fmt, iter, mem};
5
6 use either::Either;
7
8 use hir::OpaqueTyOrigin;
9 use rustc_data_structures::frozen::Frozen;
10 use rustc_data_structures::fx::{FxHashMap, FxHashSet};
11 use rustc_data_structures::vec_map::VecMap;
12 use rustc_errors::struct_span_err;
13 use rustc_hir as hir;
14 use rustc_hir::def::DefKind;
15 use rustc_hir::def_id::LocalDefId;
16 use rustc_hir::lang_items::LangItem;
17 use rustc_index::vec::{Idx, IndexVec};
18 use rustc_infer::infer::canonical::QueryRegionConstraints;
19 use rustc_infer::infer::outlives::env::RegionBoundPairs;
20 use rustc_infer::infer::region_constraints::RegionConstraintData;
21 use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
22 use rustc_infer::infer::{
23     InferCtxt, InferOk, LateBoundRegionConversionTime, NllRegionVariableOrigin,
24 };
25 use rustc_middle::mir::tcx::PlaceTy;
26 use rustc_middle::mir::visit::{NonMutatingUseContext, PlaceContext, Visitor};
27 use rustc_middle::mir::AssertKind;
28 use rustc_middle::mir::*;
29 use rustc_middle::ty::adjustment::PointerCast;
30 use rustc_middle::ty::cast::CastTy;
31 use rustc_middle::ty::fold::TypeFoldable;
32 use rustc_middle::ty::subst::{GenericArgKind, SubstsRef, UserSubsts};
33 use rustc_middle::ty::{
34     self, CanonicalUserTypeAnnotation, CanonicalUserTypeAnnotations, OpaqueHiddenType,
35     OpaqueTypeKey, RegionVid, ToPredicate, Ty, TyCtxt, UserType, UserTypeAnnotationIndex,
36 };
37 use rustc_span::def_id::CRATE_DEF_ID;
38 use rustc_span::{Span, DUMMY_SP};
39 use rustc_target::abi::VariantIdx;
40 use rustc_trait_selection::traits::query::type_op;
41 use rustc_trait_selection::traits::query::type_op::custom::scrape_region_constraints;
42 use rustc_trait_selection::traits::query::type_op::custom::CustomTypeOp;
43 use rustc_trait_selection::traits::query::type_op::{TypeOp, TypeOpOutput};
44 use rustc_trait_selection::traits::query::Fallible;
45 use rustc_trait_selection::traits::PredicateObligation;
46
47 use rustc_mir_dataflow::impls::MaybeInitializedPlaces;
48 use rustc_mir_dataflow::move_paths::MoveData;
49 use rustc_mir_dataflow::ResultsCursor;
50
51 use crate::{
52     borrow_set::BorrowSet,
53     constraints::{OutlivesConstraint, OutlivesConstraintSet},
54     diagnostics::UniverseInfo,
55     facts::AllFacts,
56     location::LocationTable,
57     member_constraints::MemberConstraintSet,
58     nll::ToRegionVid,
59     path_utils,
60     region_infer::values::{
61         LivenessValues, PlaceholderIndex, PlaceholderIndices, RegionValueElements,
62     },
63     region_infer::{ClosureRegionRequirementsExt, TypeTest},
64     type_check::free_region_relations::{CreateResult, UniversalRegionRelations},
65     universal_regions::{DefiningTy, UniversalRegions},
66     Upvar,
67 };
68
69 macro_rules! span_mirbug {
70     ($context:expr, $elem:expr, $($message:tt)*) => ({
71         $crate::type_check::mirbug(
72             $context.tcx(),
73             $context.last_span,
74             &format!(
75                 "broken MIR in {:?} ({:?}): {}",
76                 $context.body().source.def_id(),
77                 $elem,
78                 format_args!($($message)*),
79             ),
80         )
81     })
82 }
83
84 macro_rules! span_mirbug_and_err {
85     ($context:expr, $elem:expr, $($message:tt)*) => ({
86         {
87             span_mirbug!($context, $elem, $($message)*);
88             $context.error()
89         }
90     })
91 }
92
93 mod canonical;
94 mod constraint_conversion;
95 pub mod free_region_relations;
96 mod input_output;
97 pub(crate) mod liveness;
98 mod relate_tys;
99
100 /// Type checks the given `mir` in the context of the inference
101 /// context `infcx`. Returns any region constraints that have yet to
102 /// be proven. This result includes liveness constraints that
103 /// ensure that regions appearing in the types of all local variables
104 /// are live at all points where that local variable may later be
105 /// used.
106 ///
107 /// This phase of type-check ought to be infallible -- this is because
108 /// the original, HIR-based type-check succeeded. So if any errors
109 /// occur here, we will get a `bug!` reported.
110 ///
111 /// # Parameters
112 ///
113 /// - `infcx` -- inference context to use
114 /// - `param_env` -- parameter environment to use for trait solving
115 /// - `body` -- MIR body to type-check
116 /// - `promoted` -- map of promoted constants within `body`
117 /// - `universal_regions` -- the universal regions from `body`s function signature
118 /// - `location_table` -- MIR location map of `body`
119 /// - `borrow_set` -- information about borrows occurring in `body`
120 /// - `all_facts` -- when using Polonius, this is the generated set of Polonius facts
121 /// - `flow_inits` -- results of a maybe-init dataflow analysis
122 /// - `move_data` -- move-data constructed when performing the maybe-init dataflow analysis
123 /// - `elements` -- MIR region map
124 pub(crate) fn type_check<'mir, 'tcx>(
125     infcx: &InferCtxt<'_, 'tcx>,
126     param_env: ty::ParamEnv<'tcx>,
127     body: &Body<'tcx>,
128     promoted: &IndexVec<Promoted, Body<'tcx>>,
129     universal_regions: &Rc<UniversalRegions<'tcx>>,
130     location_table: &LocationTable,
131     borrow_set: &BorrowSet<'tcx>,
132     all_facts: &mut Option<AllFacts>,
133     flow_inits: &mut ResultsCursor<'mir, 'tcx, MaybeInitializedPlaces<'mir, 'tcx>>,
134     move_data: &MoveData<'tcx>,
135     elements: &Rc<RegionValueElements>,
136     upvars: &[Upvar<'tcx>],
137     use_polonius: bool,
138 ) -> MirTypeckResults<'tcx> {
139     let implicit_region_bound = infcx.tcx.mk_region(ty::ReVar(universal_regions.fr_fn_body));
140     let mut universe_causes = FxHashMap::default();
141     universe_causes.insert(ty::UniverseIndex::from_u32(0), UniverseInfo::other());
142     let mut constraints = MirTypeckRegionConstraints {
143         placeholder_indices: PlaceholderIndices::default(),
144         placeholder_index_to_region: IndexVec::default(),
145         liveness_constraints: LivenessValues::new(elements.clone()),
146         outlives_constraints: OutlivesConstraintSet::default(),
147         member_constraints: MemberConstraintSet::default(),
148         closure_bounds_mapping: Default::default(),
149         type_tests: Vec::default(),
150         universe_causes,
151     };
152
153     let CreateResult {
154         universal_region_relations,
155         region_bound_pairs,
156         normalized_inputs_and_output,
157     } = free_region_relations::create(
158         infcx,
159         param_env,
160         Some(implicit_region_bound),
161         universal_regions,
162         &mut constraints,
163     );
164
165     for u in ty::UniverseIndex::ROOT..infcx.universe() {
166         let info = UniverseInfo::other();
167         constraints.universe_causes.insert(u, info);
168     }
169
170     let mut borrowck_context = BorrowCheckContext {
171         universal_regions,
172         location_table,
173         borrow_set,
174         all_facts,
175         constraints: &mut constraints,
176         upvars,
177     };
178
179     let opaque_type_values = type_check_internal(
180         infcx,
181         param_env,
182         body,
183         promoted,
184         &region_bound_pairs,
185         implicit_region_bound,
186         &mut borrowck_context,
187         |mut cx| {
188             cx.equate_inputs_and_outputs(&body, universal_regions, &normalized_inputs_and_output);
189             liveness::generate(
190                 &mut cx,
191                 body,
192                 elements,
193                 flow_inits,
194                 move_data,
195                 location_table,
196                 use_polonius,
197             );
198
199             translate_outlives_facts(&mut cx);
200             let opaque_type_values =
201                 infcx.inner.borrow_mut().opaque_type_storage.take_opaque_types();
202
203             opaque_type_values
204                 .into_iter()
205                 .map(|(opaque_type_key, decl)| {
206                     cx.fully_perform_op(
207                         Locations::All(body.span),
208                         ConstraintCategory::OpaqueType,
209                         CustomTypeOp::new(
210                             |infcx| {
211                                 infcx.register_member_constraints(
212                                     param_env,
213                                     opaque_type_key,
214                                     decl.hidden_type.ty,
215                                     decl.hidden_type.span,
216                                 );
217                                 Ok(InferOk { value: (), obligations: vec![] })
218                             },
219                             || "opaque_type_map".to_string(),
220                         ),
221                     )
222                     .unwrap();
223                     let mut hidden_type = infcx.resolve_vars_if_possible(decl.hidden_type);
224                     trace!(
225                         "finalized opaque type {:?} to {:#?}",
226                         opaque_type_key,
227                         hidden_type.ty.kind()
228                     );
229                     if hidden_type.has_infer_types_or_consts() {
230                         infcx.tcx.sess.delay_span_bug(
231                             decl.hidden_type.span,
232                             &format!("could not resolve {:#?}", hidden_type.ty.kind()),
233                         );
234                         hidden_type.ty = infcx.tcx.ty_error();
235                     }
236
237                     (opaque_type_key, (hidden_type, decl.origin))
238                 })
239                 .collect()
240         },
241     );
242
243     MirTypeckResults { constraints, universal_region_relations, opaque_type_values }
244 }
245
246 #[instrument(
247     skip(infcx, body, promoted, region_bound_pairs, borrowck_context, extra),
248     level = "debug"
249 )]
250 fn type_check_internal<'a, 'tcx, R>(
251     infcx: &'a InferCtxt<'a, 'tcx>,
252     param_env: ty::ParamEnv<'tcx>,
253     body: &'a Body<'tcx>,
254     promoted: &'a IndexVec<Promoted, Body<'tcx>>,
255     region_bound_pairs: &'a RegionBoundPairs<'tcx>,
256     implicit_region_bound: ty::Region<'tcx>,
257     borrowck_context: &'a mut BorrowCheckContext<'a, 'tcx>,
258     extra: impl FnOnce(TypeChecker<'a, 'tcx>) -> R,
259 ) -> R {
260     let mut checker = TypeChecker::new(
261         infcx,
262         body,
263         param_env,
264         region_bound_pairs,
265         implicit_region_bound,
266         borrowck_context,
267     );
268     let errors_reported = {
269         let mut verifier = TypeVerifier::new(&mut checker, promoted);
270         verifier.visit_body(&body);
271         verifier.errors_reported
272     };
273
274     if !errors_reported {
275         // if verifier failed, don't do further checks to avoid ICEs
276         checker.typeck_mir(body);
277     }
278
279     extra(checker)
280 }
281
282 fn translate_outlives_facts(typeck: &mut TypeChecker<'_, '_>) {
283     let cx = &mut typeck.borrowck_context;
284     if let Some(facts) = cx.all_facts {
285         let _prof_timer = typeck.infcx.tcx.prof.generic_activity("polonius_fact_generation");
286         let location_table = cx.location_table;
287         facts.subset_base.extend(cx.constraints.outlives_constraints.outlives().iter().flat_map(
288             |constraint: &OutlivesConstraint<'_>| {
289                 if let Some(from_location) = constraint.locations.from_location() {
290                     Either::Left(iter::once((
291                         constraint.sup,
292                         constraint.sub,
293                         location_table.mid_index(from_location),
294                     )))
295                 } else {
296                     Either::Right(
297                         location_table
298                             .all_points()
299                             .map(move |location| (constraint.sup, constraint.sub, location)),
300                     )
301                 }
302             },
303         ));
304     }
305 }
306
307 #[track_caller]
308 fn mirbug(tcx: TyCtxt<'_>, span: Span, msg: &str) {
309     // We sometimes see MIR failures (notably predicate failures) due to
310     // the fact that we check rvalue sized predicates here. So use `delay_span_bug`
311     // to avoid reporting bugs in those cases.
312     tcx.sess.diagnostic().delay_span_bug(span, msg);
313 }
314
315 enum FieldAccessError {
316     OutOfRange { field_count: usize },
317 }
318
319 /// Verifies that MIR types are sane to not crash further checks.
320 ///
321 /// The sanitize_XYZ methods here take an MIR object and compute its
322 /// type, calling `span_mirbug` and returning an error type if there
323 /// is a problem.
324 struct TypeVerifier<'a, 'b, 'tcx> {
325     cx: &'a mut TypeChecker<'b, 'tcx>,
326     promoted: &'b IndexVec<Promoted, Body<'tcx>>,
327     last_span: Span,
328     errors_reported: bool,
329 }
330
331 impl<'a, 'b, 'tcx> Visitor<'tcx> for TypeVerifier<'a, 'b, 'tcx> {
332     fn visit_span(&mut self, span: &Span) {
333         if !span.is_dummy() {
334             self.last_span = *span;
335         }
336     }
337
338     fn visit_place(&mut self, place: &Place<'tcx>, context: PlaceContext, location: Location) {
339         self.sanitize_place(place, location, context);
340     }
341
342     fn visit_constant(&mut self, constant: &Constant<'tcx>, location: Location) {
343         self.super_constant(constant, location);
344         let ty = self.sanitize_type(constant, constant.literal.ty());
345
346         self.cx.infcx.tcx.for_each_free_region(&ty, |live_region| {
347             let live_region_vid =
348                 self.cx.borrowck_context.universal_regions.to_region_vid(live_region);
349             self.cx
350                 .borrowck_context
351                 .constraints
352                 .liveness_constraints
353                 .add_element(live_region_vid, location);
354         });
355
356         if let Some(annotation_index) = constant.user_ty {
357             if let Err(terr) = self.cx.relate_type_and_user_type(
358                 constant.literal.ty(),
359                 ty::Variance::Invariant,
360                 &UserTypeProjection { base: annotation_index, projs: vec![] },
361                 location.to_locations(),
362                 ConstraintCategory::Boring,
363             ) {
364                 let annotation = &self.cx.user_type_annotations[annotation_index];
365                 span_mirbug!(
366                     self,
367                     constant,
368                     "bad constant user type {:?} vs {:?}: {:?}",
369                     annotation,
370                     constant.literal.ty(),
371                     terr,
372                 );
373             }
374         } else {
375             let tcx = self.tcx();
376             let maybe_uneval = match constant.literal {
377                 ConstantKind::Ty(ct) => match ct.val() {
378                     ty::ConstKind::Unevaluated(uv) => Some(uv),
379                     _ => None,
380                 },
381                 _ => None,
382             };
383             if let Some(uv) = maybe_uneval {
384                 if let Some(promoted) = uv.promoted {
385                     let check_err = |verifier: &mut TypeVerifier<'a, 'b, 'tcx>,
386                                      promoted: &Body<'tcx>,
387                                      ty,
388                                      san_ty| {
389                         if let Err(terr) = verifier.cx.eq_types(
390                             ty,
391                             san_ty,
392                             location.to_locations(),
393                             ConstraintCategory::Boring,
394                         ) {
395                             span_mirbug!(
396                                 verifier,
397                                 promoted,
398                                 "bad promoted type ({:?}: {:?}): {:?}",
399                                 ty,
400                                 san_ty,
401                                 terr
402                             );
403                         };
404                     };
405
406                     if !self.errors_reported {
407                         let promoted_body = &self.promoted[promoted];
408                         self.sanitize_promoted(promoted_body, location);
409
410                         let promoted_ty = promoted_body.return_ty();
411                         check_err(self, promoted_body, ty, promoted_ty);
412                     }
413                 } else {
414                     if let Err(terr) = self.cx.fully_perform_op(
415                         location.to_locations(),
416                         ConstraintCategory::Boring,
417                         self.cx.param_env.and(type_op::ascribe_user_type::AscribeUserType::new(
418                             constant.literal.ty(),
419                             uv.def.did,
420                             UserSubsts { substs: uv.substs, user_self_ty: None },
421                         )),
422                     ) {
423                         span_mirbug!(
424                             self,
425                             constant,
426                             "bad constant type {:?} ({:?})",
427                             constant,
428                             terr
429                         );
430                     }
431                 }
432             } else if let Some(static_def_id) = constant.check_static_ptr(tcx) {
433                 let unnormalized_ty = tcx.type_of(static_def_id);
434                 let locations = location.to_locations();
435                 let normalized_ty = self.cx.normalize(unnormalized_ty, locations);
436                 let literal_ty = constant.literal.ty().builtin_deref(true).unwrap().ty;
437
438                 if let Err(terr) = self.cx.eq_types(
439                     literal_ty,
440                     normalized_ty,
441                     locations,
442                     ConstraintCategory::Boring,
443                 ) {
444                     span_mirbug!(self, constant, "bad static type {:?} ({:?})", constant, terr);
445                 }
446             }
447
448             if let ty::FnDef(def_id, substs) = *constant.literal.ty().kind() {
449                 let instantiated_predicates = tcx.predicates_of(def_id).instantiate(tcx, substs);
450                 self.cx.normalize_and_prove_instantiated_predicates(
451                     def_id,
452                     instantiated_predicates,
453                     location.to_locations(),
454                 );
455             }
456         }
457     }
458
459     fn visit_rvalue(&mut self, rvalue: &Rvalue<'tcx>, location: Location) {
460         self.super_rvalue(rvalue, location);
461         let rval_ty = rvalue.ty(self.body(), self.tcx());
462         self.sanitize_type(rvalue, rval_ty);
463     }
464
465     fn visit_local_decl(&mut self, local: Local, local_decl: &LocalDecl<'tcx>) {
466         self.super_local_decl(local, local_decl);
467         self.sanitize_type(local_decl, local_decl.ty);
468
469         if let Some(user_ty) = &local_decl.user_ty {
470             for (user_ty, span) in user_ty.projections_and_spans() {
471                 let ty = if !local_decl.is_nonref_binding() {
472                     // If we have a binding of the form `let ref x: T = ..`
473                     // then remove the outermost reference so we can check the
474                     // type annotation for the remaining type.
475                     if let ty::Ref(_, rty, _) = local_decl.ty.kind() {
476                         *rty
477                     } else {
478                         bug!("{:?} with ref binding has wrong type {}", local, local_decl.ty);
479                     }
480                 } else {
481                     local_decl.ty
482                 };
483
484                 if let Err(terr) = self.cx.relate_type_and_user_type(
485                     ty,
486                     ty::Variance::Invariant,
487                     user_ty,
488                     Locations::All(*span),
489                     ConstraintCategory::TypeAnnotation,
490                 ) {
491                     span_mirbug!(
492                         self,
493                         local,
494                         "bad user type on variable {:?}: {:?} != {:?} ({:?})",
495                         local,
496                         local_decl.ty,
497                         local_decl.user_ty,
498                         terr,
499                     );
500                 }
501             }
502         }
503     }
504
505     fn visit_body(&mut self, body: &Body<'tcx>) {
506         self.sanitize_type(&"return type", body.return_ty());
507         for local_decl in &body.local_decls {
508             self.sanitize_type(local_decl, local_decl.ty);
509         }
510         if self.errors_reported {
511             return;
512         }
513         self.super_body(body);
514     }
515 }
516
517 impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {
518     fn new(
519         cx: &'a mut TypeChecker<'b, 'tcx>,
520         promoted: &'b IndexVec<Promoted, Body<'tcx>>,
521     ) -> Self {
522         TypeVerifier { promoted, last_span: cx.body.span, cx, errors_reported: false }
523     }
524
525     fn body(&self) -> &Body<'tcx> {
526         self.cx.body
527     }
528
529     fn tcx(&self) -> TyCtxt<'tcx> {
530         self.cx.infcx.tcx
531     }
532
533     fn sanitize_type(&mut self, parent: &dyn fmt::Debug, ty: Ty<'tcx>) -> Ty<'tcx> {
534         if ty.has_escaping_bound_vars() || ty.references_error() {
535             span_mirbug_and_err!(self, parent, "bad type {:?}", ty)
536         } else {
537             ty
538         }
539     }
540
541     /// Checks that the types internal to the `place` match up with
542     /// what would be expected.
543     fn sanitize_place(
544         &mut self,
545         place: &Place<'tcx>,
546         location: Location,
547         context: PlaceContext,
548     ) -> PlaceTy<'tcx> {
549         debug!("sanitize_place: {:?}", place);
550
551         let mut place_ty = PlaceTy::from_ty(self.body().local_decls[place.local].ty);
552
553         for elem in place.projection.iter() {
554             if place_ty.variant_index.is_none() {
555                 if place_ty.ty.references_error() {
556                     assert!(self.errors_reported);
557                     return PlaceTy::from_ty(self.tcx().ty_error());
558                 }
559             }
560             place_ty = self.sanitize_projection(place_ty, elem, place, location);
561         }
562
563         if let PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy) = context {
564             let tcx = self.tcx();
565             let trait_ref = ty::TraitRef {
566                 def_id: tcx.require_lang_item(LangItem::Copy, Some(self.last_span)),
567                 substs: tcx.mk_substs_trait(place_ty.ty, &[]),
568             };
569
570             // To have a `Copy` operand, the type `T` of the
571             // value must be `Copy`. Note that we prove that `T: Copy`,
572             // rather than using the `is_copy_modulo_regions`
573             // test. This is important because
574             // `is_copy_modulo_regions` ignores the resulting region
575             // obligations and assumes they pass. This can result in
576             // bounds from `Copy` impls being unsoundly ignored (e.g.,
577             // #29149). Note that we decide to use `Copy` before knowing
578             // whether the bounds fully apply: in effect, the rule is
579             // that if a value of some type could implement `Copy`, then
580             // it must.
581             self.cx.prove_trait_ref(
582                 trait_ref,
583                 location.to_locations(),
584                 ConstraintCategory::CopyBound,
585             );
586         }
587
588         place_ty
589     }
590
591     fn sanitize_promoted(&mut self, promoted_body: &'b Body<'tcx>, location: Location) {
592         // Determine the constraints from the promoted MIR by running the type
593         // checker on the promoted MIR, then transfer the constraints back to
594         // the main MIR, changing the locations to the provided location.
595
596         let parent_body = mem::replace(&mut self.cx.body, promoted_body);
597
598         // Use new sets of constraints and closure bounds so that we can
599         // modify their locations.
600         let all_facts = &mut None;
601         let mut constraints = Default::default();
602         let mut closure_bounds = Default::default();
603         let mut liveness_constraints =
604             LivenessValues::new(Rc::new(RegionValueElements::new(&promoted_body)));
605         // Don't try to add borrow_region facts for the promoted MIR
606
607         let mut swap_constraints = |this: &mut Self| {
608             mem::swap(this.cx.borrowck_context.all_facts, all_facts);
609             mem::swap(
610                 &mut this.cx.borrowck_context.constraints.outlives_constraints,
611                 &mut constraints,
612             );
613             mem::swap(
614                 &mut this.cx.borrowck_context.constraints.closure_bounds_mapping,
615                 &mut closure_bounds,
616             );
617             mem::swap(
618                 &mut this.cx.borrowck_context.constraints.liveness_constraints,
619                 &mut liveness_constraints,
620             );
621         };
622
623         swap_constraints(self);
624
625         self.visit_body(&promoted_body);
626
627         if !self.errors_reported {
628             // if verifier failed, don't do further checks to avoid ICEs
629             self.cx.typeck_mir(promoted_body);
630         }
631
632         self.cx.body = parent_body;
633         // Merge the outlives constraints back in, at the given location.
634         swap_constraints(self);
635
636         let locations = location.to_locations();
637         for constraint in constraints.outlives().iter() {
638             let mut constraint = constraint.clone();
639             constraint.locations = locations;
640             if let ConstraintCategory::Return(_)
641             | ConstraintCategory::UseAsConst
642             | ConstraintCategory::UseAsStatic = constraint.category
643             {
644                 // "Returning" from a promoted is an assignment to a
645                 // temporary from the user's point of view.
646                 constraint.category = ConstraintCategory::Boring;
647             }
648             self.cx.borrowck_context.constraints.outlives_constraints.push(constraint)
649         }
650         for region in liveness_constraints.rows() {
651             // If the region is live at at least one location in the promoted MIR,
652             // then add a liveness constraint to the main MIR for this region
653             // at the location provided as an argument to this method
654             if liveness_constraints.get_elements(region).next().is_some() {
655                 self.cx
656                     .borrowck_context
657                     .constraints
658                     .liveness_constraints
659                     .add_element(region, location);
660             }
661         }
662
663         if !closure_bounds.is_empty() {
664             let combined_bounds_mapping =
665                 closure_bounds.into_iter().flat_map(|(_, value)| value).collect();
666             let existing = self
667                 .cx
668                 .borrowck_context
669                 .constraints
670                 .closure_bounds_mapping
671                 .insert(location, combined_bounds_mapping);
672             assert!(existing.is_none(), "Multiple promoteds/closures at the same location.");
673         }
674     }
675
676     fn sanitize_projection(
677         &mut self,
678         base: PlaceTy<'tcx>,
679         pi: PlaceElem<'tcx>,
680         place: &Place<'tcx>,
681         location: Location,
682     ) -> PlaceTy<'tcx> {
683         debug!("sanitize_projection: {:?} {:?} {:?}", base, pi, place);
684         let tcx = self.tcx();
685         let base_ty = base.ty;
686         match pi {
687             ProjectionElem::Deref => {
688                 let deref_ty = base_ty.builtin_deref(true);
689                 PlaceTy::from_ty(deref_ty.map(|t| t.ty).unwrap_or_else(|| {
690                     span_mirbug_and_err!(self, place, "deref of non-pointer {:?}", base_ty)
691                 }))
692             }
693             ProjectionElem::Index(i) => {
694                 let index_ty = Place::from(i).ty(self.body(), tcx).ty;
695                 if index_ty != tcx.types.usize {
696                     PlaceTy::from_ty(span_mirbug_and_err!(self, i, "index by non-usize {:?}", i))
697                 } else {
698                     PlaceTy::from_ty(base_ty.builtin_index().unwrap_or_else(|| {
699                         span_mirbug_and_err!(self, place, "index of non-array {:?}", base_ty)
700                     }))
701                 }
702             }
703             ProjectionElem::ConstantIndex { .. } => {
704                 // consider verifying in-bounds
705                 PlaceTy::from_ty(base_ty.builtin_index().unwrap_or_else(|| {
706                     span_mirbug_and_err!(self, place, "index of non-array {:?}", base_ty)
707                 }))
708             }
709             ProjectionElem::Subslice { from, to, from_end } => {
710                 PlaceTy::from_ty(match base_ty.kind() {
711                     ty::Array(inner, _) => {
712                         assert!(!from_end, "array subslices should not use from_end");
713                         tcx.mk_array(*inner, to - from)
714                     }
715                     ty::Slice(..) => {
716                         assert!(from_end, "slice subslices should use from_end");
717                         base_ty
718                     }
719                     _ => span_mirbug_and_err!(self, place, "slice of non-array {:?}", base_ty),
720                 })
721             }
722             ProjectionElem::Downcast(maybe_name, index) => match base_ty.kind() {
723                 ty::Adt(adt_def, _substs) if adt_def.is_enum() => {
724                     if index.as_usize() >= adt_def.variants().len() {
725                         PlaceTy::from_ty(span_mirbug_and_err!(
726                             self,
727                             place,
728                             "cast to variant #{:?} but enum only has {:?}",
729                             index,
730                             adt_def.variants().len()
731                         ))
732                     } else {
733                         PlaceTy { ty: base_ty, variant_index: Some(index) }
734                     }
735                 }
736                 // We do not need to handle generators here, because this runs
737                 // before the generator transform stage.
738                 _ => {
739                     let ty = if let Some(name) = maybe_name {
740                         span_mirbug_and_err!(
741                             self,
742                             place,
743                             "can't downcast {:?} as {:?}",
744                             base_ty,
745                             name
746                         )
747                     } else {
748                         span_mirbug_and_err!(self, place, "can't downcast {:?}", base_ty)
749                     };
750                     PlaceTy::from_ty(ty)
751                 }
752             },
753             ProjectionElem::Field(field, fty) => {
754                 let fty = self.sanitize_type(place, fty);
755                 let fty = self.cx.normalize(fty, location);
756                 match self.field_ty(place, base, field, location) {
757                     Ok(ty) => {
758                         let ty = self.cx.normalize(ty, location);
759                         if let Err(terr) = self.cx.eq_types(
760                             ty,
761                             fty,
762                             location.to_locations(),
763                             ConstraintCategory::Boring,
764                         ) {
765                             span_mirbug!(
766                                 self,
767                                 place,
768                                 "bad field access ({:?}: {:?}): {:?}",
769                                 ty,
770                                 fty,
771                                 terr
772                             );
773                         }
774                     }
775                     Err(FieldAccessError::OutOfRange { field_count }) => span_mirbug!(
776                         self,
777                         place,
778                         "accessed field #{} but variant only has {}",
779                         field.index(),
780                         field_count
781                     ),
782                 }
783                 PlaceTy::from_ty(fty)
784             }
785         }
786     }
787
788     fn error(&mut self) -> Ty<'tcx> {
789         self.errors_reported = true;
790         self.tcx().ty_error()
791     }
792
793     fn field_ty(
794         &mut self,
795         parent: &dyn fmt::Debug,
796         base_ty: PlaceTy<'tcx>,
797         field: Field,
798         location: Location,
799     ) -> Result<Ty<'tcx>, FieldAccessError> {
800         let tcx = self.tcx();
801
802         let (variant, substs) = match base_ty {
803             PlaceTy { ty, variant_index: Some(variant_index) } => match *ty.kind() {
804                 ty::Adt(adt_def, substs) => (adt_def.variant(variant_index), substs),
805                 ty::Generator(def_id, substs, _) => {
806                     let mut variants = substs.as_generator().state_tys(def_id, tcx);
807                     let Some(mut variant) = variants.nth(variant_index.into()) else {
808                         bug!(
809                             "variant_index of generator out of range: {:?}/{:?}",
810                             variant_index,
811                             substs.as_generator().state_tys(def_id, tcx).count()
812                         );
813                     };
814                     return match variant.nth(field.index()) {
815                         Some(ty) => Ok(ty),
816                         None => Err(FieldAccessError::OutOfRange { field_count: variant.count() }),
817                     };
818                 }
819                 _ => bug!("can't have downcast of non-adt non-generator type"),
820             },
821             PlaceTy { ty, variant_index: None } => match *ty.kind() {
822                 ty::Adt(adt_def, substs) if !adt_def.is_enum() => {
823                     (adt_def.variant(VariantIdx::new(0)), substs)
824                 }
825                 ty::Closure(_, substs) => {
826                     return match substs
827                         .as_closure()
828                         .tupled_upvars_ty()
829                         .tuple_fields()
830                         .get(field.index())
831                     {
832                         Some(&ty) => Ok(ty),
833                         None => Err(FieldAccessError::OutOfRange {
834                             field_count: substs.as_closure().upvar_tys().count(),
835                         }),
836                     };
837                 }
838                 ty::Generator(_, substs, _) => {
839                     // Only prefix fields (upvars and current state) are
840                     // accessible without a variant index.
841                     return match substs.as_generator().prefix_tys().nth(field.index()) {
842                         Some(ty) => Ok(ty),
843                         None => Err(FieldAccessError::OutOfRange {
844                             field_count: substs.as_generator().prefix_tys().count(),
845                         }),
846                     };
847                 }
848                 ty::Tuple(tys) => {
849                     return match tys.get(field.index()) {
850                         Some(&ty) => Ok(ty),
851                         None => Err(FieldAccessError::OutOfRange { field_count: tys.len() }),
852                     };
853                 }
854                 _ => {
855                     return Ok(span_mirbug_and_err!(
856                         self,
857                         parent,
858                         "can't project out of {:?}",
859                         base_ty
860                     ));
861                 }
862             },
863         };
864
865         if let Some(field) = variant.fields.get(field.index()) {
866             Ok(self.cx.normalize(field.ty(tcx, substs), location))
867         } else {
868             Err(FieldAccessError::OutOfRange { field_count: variant.fields.len() })
869         }
870     }
871 }
872
873 /// The MIR type checker. Visits the MIR and enforces all the
874 /// constraints needed for it to be valid and well-typed. Along the
875 /// way, it accrues region constraints -- these can later be used by
876 /// NLL region checking.
877 struct TypeChecker<'a, 'tcx> {
878     infcx: &'a InferCtxt<'a, 'tcx>,
879     param_env: ty::ParamEnv<'tcx>,
880     last_span: Span,
881     body: &'a Body<'tcx>,
882     /// User type annotations are shared between the main MIR and the MIR of
883     /// all of the promoted items.
884     user_type_annotations: &'a CanonicalUserTypeAnnotations<'tcx>,
885     region_bound_pairs: &'a RegionBoundPairs<'tcx>,
886     implicit_region_bound: ty::Region<'tcx>,
887     reported_errors: FxHashSet<(Ty<'tcx>, Span)>,
888     borrowck_context: &'a mut BorrowCheckContext<'a, 'tcx>,
889 }
890
891 struct BorrowCheckContext<'a, 'tcx> {
892     pub(crate) universal_regions: &'a UniversalRegions<'tcx>,
893     location_table: &'a LocationTable,
894     all_facts: &'a mut Option<AllFacts>,
895     borrow_set: &'a BorrowSet<'tcx>,
896     pub(crate) constraints: &'a mut MirTypeckRegionConstraints<'tcx>,
897     upvars: &'a [Upvar<'tcx>],
898 }
899
900 pub(crate) struct MirTypeckResults<'tcx> {
901     pub(crate) constraints: MirTypeckRegionConstraints<'tcx>,
902     pub(crate) universal_region_relations: Frozen<UniversalRegionRelations<'tcx>>,
903     pub(crate) opaque_type_values:
904         VecMap<OpaqueTypeKey<'tcx>, (OpaqueHiddenType<'tcx>, OpaqueTyOrigin)>,
905 }
906
907 /// A collection of region constraints that must be satisfied for the
908 /// program to be considered well-typed.
909 pub(crate) struct MirTypeckRegionConstraints<'tcx> {
910     /// Maps from a `ty::Placeholder` to the corresponding
911     /// `PlaceholderIndex` bit that we will use for it.
912     ///
913     /// To keep everything in sync, do not insert this set
914     /// directly. Instead, use the `placeholder_region` helper.
915     pub(crate) placeholder_indices: PlaceholderIndices,
916
917     /// Each time we add a placeholder to `placeholder_indices`, we
918     /// also create a corresponding "representative" region vid for
919     /// that wraps it. This vector tracks those. This way, when we
920     /// convert the same `ty::RePlaceholder(p)` twice, we can map to
921     /// the same underlying `RegionVid`.
922     pub(crate) placeholder_index_to_region: IndexVec<PlaceholderIndex, ty::Region<'tcx>>,
923
924     /// In general, the type-checker is not responsible for enforcing
925     /// liveness constraints; this job falls to the region inferencer,
926     /// which performs a liveness analysis. However, in some limited
927     /// cases, the MIR type-checker creates temporary regions that do
928     /// not otherwise appear in the MIR -- in particular, the
929     /// late-bound regions that it instantiates at call-sites -- and
930     /// hence it must report on their liveness constraints.
931     pub(crate) liveness_constraints: LivenessValues<RegionVid>,
932
933     pub(crate) outlives_constraints: OutlivesConstraintSet<'tcx>,
934
935     pub(crate) member_constraints: MemberConstraintSet<'tcx, RegionVid>,
936
937     pub(crate) closure_bounds_mapping:
938         FxHashMap<Location, FxHashMap<(RegionVid, RegionVid), (ConstraintCategory, Span)>>,
939
940     pub(crate) universe_causes: FxHashMap<ty::UniverseIndex, UniverseInfo<'tcx>>,
941
942     pub(crate) type_tests: Vec<TypeTest<'tcx>>,
943 }
944
945 impl<'tcx> MirTypeckRegionConstraints<'tcx> {
946     fn placeholder_region(
947         &mut self,
948         infcx: &InferCtxt<'_, 'tcx>,
949         placeholder: ty::PlaceholderRegion,
950     ) -> ty::Region<'tcx> {
951         let placeholder_index = self.placeholder_indices.insert(placeholder);
952         match self.placeholder_index_to_region.get(placeholder_index) {
953             Some(&v) => v,
954             None => {
955                 let origin = NllRegionVariableOrigin::Placeholder(placeholder);
956                 let region = infcx.next_nll_region_var_in_universe(origin, placeholder.universe);
957                 self.placeholder_index_to_region.push(region);
958                 region
959             }
960         }
961     }
962 }
963
964 /// The `Locations` type summarizes *where* region constraints are
965 /// required to hold. Normally, this is at a particular point which
966 /// created the obligation, but for constraints that the user gave, we
967 /// want the constraint to hold at all points.
968 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
969 pub enum Locations {
970     /// Indicates that a type constraint should always be true. This
971     /// is particularly important in the new borrowck analysis for
972     /// things like the type of the return slot. Consider this
973     /// example:
974     ///
975     /// ```compile_fail,E0515
976     /// fn foo<'a>(x: &'a u32) -> &'a u32 {
977     ///     let y = 22;
978     ///     return &y; // error
979     /// }
980     /// ```
981     ///
982     /// Here, we wind up with the signature from the return type being
983     /// something like `&'1 u32` where `'1` is a universal region. But
984     /// the type of the return slot `_0` is something like `&'2 u32`
985     /// where `'2` is an existential region variable. The type checker
986     /// requires that `&'2 u32 = &'1 u32` -- but at what point? In the
987     /// older NLL analysis, we required this only at the entry point
988     /// to the function. By the nature of the constraints, this wound
989     /// up propagating to all points reachable from start (because
990     /// `'1` -- as a universal region -- is live everywhere). In the
991     /// newer analysis, though, this doesn't work: `_0` is considered
992     /// dead at the start (it has no usable value) and hence this type
993     /// equality is basically a no-op. Then, later on, when we do `_0
994     /// = &'3 y`, that region `'3` never winds up related to the
995     /// universal region `'1` and hence no error occurs. Therefore, we
996     /// use Locations::All instead, which ensures that the `'1` and
997     /// `'2` are equal everything. We also use this for other
998     /// user-given type annotations; e.g., if the user wrote `let mut
999     /// x: &'static u32 = ...`, we would ensure that all values
1000     /// assigned to `x` are of `'static` lifetime.
1001     ///
1002     /// The span points to the place the constraint arose. For example,
1003     /// it points to the type in a user-given type annotation. If
1004     /// there's no sensible span then it's DUMMY_SP.
1005     All(Span),
1006
1007     /// An outlives constraint that only has to hold at a single location,
1008     /// usually it represents a point where references flow from one spot to
1009     /// another (e.g., `x = y`)
1010     Single(Location),
1011 }
1012
1013 impl Locations {
1014     pub fn from_location(&self) -> Option<Location> {
1015         match self {
1016             Locations::All(_) => None,
1017             Locations::Single(from_location) => Some(*from_location),
1018         }
1019     }
1020
1021     /// Gets a span representing the location.
1022     pub fn span(&self, body: &Body<'_>) -> Span {
1023         match self {
1024             Locations::All(span) => *span,
1025             Locations::Single(l) => body.source_info(*l).span,
1026         }
1027     }
1028 }
1029
1030 impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
1031     fn new(
1032         infcx: &'a InferCtxt<'a, 'tcx>,
1033         body: &'a Body<'tcx>,
1034         param_env: ty::ParamEnv<'tcx>,
1035         region_bound_pairs: &'a RegionBoundPairs<'tcx>,
1036         implicit_region_bound: ty::Region<'tcx>,
1037         borrowck_context: &'a mut BorrowCheckContext<'a, 'tcx>,
1038     ) -> Self {
1039         let mut checker = Self {
1040             infcx,
1041             last_span: DUMMY_SP,
1042             body,
1043             user_type_annotations: &body.user_type_annotations,
1044             param_env,
1045             region_bound_pairs,
1046             implicit_region_bound,
1047             borrowck_context,
1048             reported_errors: Default::default(),
1049         };
1050         checker.check_user_type_annotations();
1051         checker
1052     }
1053
1054     fn body(&self) -> &Body<'tcx> {
1055         self.body
1056     }
1057
1058     fn unsized_feature_enabled(&self) -> bool {
1059         let features = self.tcx().features();
1060         features.unsized_locals || features.unsized_fn_params
1061     }
1062
1063     /// Equate the inferred type and the annotated type for user type annotations
1064     #[instrument(skip(self), level = "debug")]
1065     fn check_user_type_annotations(&mut self) {
1066         debug!(?self.user_type_annotations);
1067         for user_annotation in self.user_type_annotations {
1068             let CanonicalUserTypeAnnotation { span, ref user_ty, inferred_ty } = *user_annotation;
1069             let inferred_ty = self.normalize(inferred_ty, Locations::All(span));
1070             let annotation = self.instantiate_canonical_with_fresh_inference_vars(span, user_ty);
1071             match annotation {
1072                 UserType::Ty(mut ty) => {
1073                     ty = self.normalize(ty, Locations::All(span));
1074
1075                     if let Err(terr) = self.eq_types(
1076                         ty,
1077                         inferred_ty,
1078                         Locations::All(span),
1079                         ConstraintCategory::BoringNoLocation,
1080                     ) {
1081                         span_mirbug!(
1082                             self,
1083                             user_annotation,
1084                             "bad user type ({:?} = {:?}): {:?}",
1085                             ty,
1086                             inferred_ty,
1087                             terr
1088                         );
1089                     }
1090
1091                     self.prove_predicate(
1092                         ty::Binder::dummy(ty::PredicateKind::WellFormed(inferred_ty.into()))
1093                             .to_predicate(self.tcx()),
1094                         Locations::All(span),
1095                         ConstraintCategory::TypeAnnotation,
1096                     );
1097                 }
1098                 UserType::TypeOf(def_id, user_substs) => {
1099                     if let Err(terr) = self.fully_perform_op(
1100                         Locations::All(span),
1101                         ConstraintCategory::BoringNoLocation,
1102                         self.param_env.and(type_op::ascribe_user_type::AscribeUserType::new(
1103                             inferred_ty,
1104                             def_id,
1105                             user_substs,
1106                         )),
1107                     ) {
1108                         span_mirbug!(
1109                             self,
1110                             user_annotation,
1111                             "bad user type AscribeUserType({:?}, {:?} {:?}, type_of={:?}): {:?}",
1112                             inferred_ty,
1113                             def_id,
1114                             user_substs,
1115                             self.tcx().type_of(def_id),
1116                             terr,
1117                         );
1118                     }
1119                 }
1120             }
1121         }
1122     }
1123
1124     #[instrument(skip(self, data), level = "debug")]
1125     fn push_region_constraints(
1126         &mut self,
1127         locations: Locations,
1128         category: ConstraintCategory,
1129         data: &QueryRegionConstraints<'tcx>,
1130     ) {
1131         debug!("constraints generated: {:#?}", data);
1132
1133         constraint_conversion::ConstraintConversion::new(
1134             self.infcx,
1135             self.borrowck_context.universal_regions,
1136             self.region_bound_pairs,
1137             Some(self.implicit_region_bound),
1138             self.param_env,
1139             locations,
1140             locations.span(self.body),
1141             category,
1142             &mut self.borrowck_context.constraints,
1143         )
1144         .convert_all(data);
1145     }
1146
1147     /// Try to relate `sub <: sup`
1148     fn sub_types(
1149         &mut self,
1150         sub: Ty<'tcx>,
1151         sup: Ty<'tcx>,
1152         locations: Locations,
1153         category: ConstraintCategory,
1154     ) -> Fallible<()> {
1155         // Use this order of parameters because the sup type is usually the
1156         // "expected" type in diagnostics.
1157         self.relate_types(sup, ty::Variance::Contravariant, sub, locations, category)
1158     }
1159
1160     #[instrument(skip(self, category), level = "debug")]
1161     fn eq_types(
1162         &mut self,
1163         expected: Ty<'tcx>,
1164         found: Ty<'tcx>,
1165         locations: Locations,
1166         category: ConstraintCategory,
1167     ) -> Fallible<()> {
1168         self.relate_types(expected, ty::Variance::Invariant, found, locations, category)
1169     }
1170
1171     #[instrument(skip(self), level = "debug")]
1172     fn relate_type_and_user_type(
1173         &mut self,
1174         a: Ty<'tcx>,
1175         v: ty::Variance,
1176         user_ty: &UserTypeProjection,
1177         locations: Locations,
1178         category: ConstraintCategory,
1179     ) -> Fallible<()> {
1180         let annotated_type = self.user_type_annotations[user_ty.base].inferred_ty;
1181         let mut curr_projected_ty = PlaceTy::from_ty(annotated_type);
1182
1183         let tcx = self.infcx.tcx;
1184
1185         for proj in &user_ty.projs {
1186             let projected_ty = curr_projected_ty.projection_ty_core(
1187                 tcx,
1188                 self.param_env,
1189                 proj,
1190                 |this, field, ()| {
1191                     let ty = this.field_ty(tcx, field);
1192                     self.normalize(ty, locations)
1193                 },
1194             );
1195             curr_projected_ty = projected_ty;
1196         }
1197         debug!(
1198             "user_ty base: {:?} freshened: {:?} projs: {:?} yields: {:?}",
1199             user_ty.base, annotated_type, user_ty.projs, curr_projected_ty
1200         );
1201
1202         let ty = curr_projected_ty.ty;
1203         self.relate_types(ty, v.xform(ty::Variance::Contravariant), a, locations, category)?;
1204
1205         Ok(())
1206     }
1207
1208     fn tcx(&self) -> TyCtxt<'tcx> {
1209         self.infcx.tcx
1210     }
1211
1212     #[instrument(skip(self, body, location), level = "debug")]
1213     fn check_stmt(&mut self, body: &Body<'tcx>, stmt: &Statement<'tcx>, location: Location) {
1214         let tcx = self.tcx();
1215         match stmt.kind {
1216             StatementKind::Assign(box (ref place, ref rv)) => {
1217                 // Assignments to temporaries are not "interesting";
1218                 // they are not caused by the user, but rather artifacts
1219                 // of lowering. Assignments to other sorts of places *are* interesting
1220                 // though.
1221                 let category = match place.as_local() {
1222                     Some(RETURN_PLACE) => {
1223                         let defining_ty = &self.borrowck_context.universal_regions.defining_ty;
1224                         if defining_ty.is_const() {
1225                             if tcx.is_static(defining_ty.def_id()) {
1226                                 ConstraintCategory::UseAsStatic
1227                             } else {
1228                                 ConstraintCategory::UseAsConst
1229                             }
1230                         } else {
1231                             ConstraintCategory::Return(ReturnConstraint::Normal)
1232                         }
1233                     }
1234                     Some(l)
1235                         if matches!(
1236                             body.local_decls[l].local_info,
1237                             Some(box LocalInfo::AggregateTemp)
1238                         ) =>
1239                     {
1240                         ConstraintCategory::Usage
1241                     }
1242                     Some(l) if !body.local_decls[l].is_user_variable() => {
1243                         ConstraintCategory::Boring
1244                     }
1245                     _ => ConstraintCategory::Assignment,
1246                 };
1247                 debug!(
1248                     "assignment category: {:?} {:?}",
1249                     category,
1250                     place.as_local().map(|l| &body.local_decls[l])
1251                 );
1252
1253                 let place_ty = place.ty(body, tcx).ty;
1254                 let place_ty = self.normalize(place_ty, location);
1255                 let rv_ty = rv.ty(body, tcx);
1256                 let rv_ty = self.normalize(rv_ty, location);
1257                 if let Err(terr) =
1258                     self.sub_types(rv_ty, place_ty, location.to_locations(), category)
1259                 {
1260                     span_mirbug!(
1261                         self,
1262                         stmt,
1263                         "bad assignment ({:?} = {:?}): {:?}",
1264                         place_ty,
1265                         rv_ty,
1266                         terr
1267                     );
1268                 }
1269
1270                 if let Some(annotation_index) = self.rvalue_user_ty(rv) {
1271                     if let Err(terr) = self.relate_type_and_user_type(
1272                         rv_ty,
1273                         ty::Variance::Invariant,
1274                         &UserTypeProjection { base: annotation_index, projs: vec![] },
1275                         location.to_locations(),
1276                         ConstraintCategory::Boring,
1277                     ) {
1278                         let annotation = &self.user_type_annotations[annotation_index];
1279                         span_mirbug!(
1280                             self,
1281                             stmt,
1282                             "bad user type on rvalue ({:?} = {:?}): {:?}",
1283                             annotation,
1284                             rv_ty,
1285                             terr
1286                         );
1287                     }
1288                 }
1289
1290                 self.check_rvalue(body, rv, location);
1291                 if !self.unsized_feature_enabled() {
1292                     let trait_ref = ty::TraitRef {
1293                         def_id: tcx.require_lang_item(LangItem::Sized, Some(self.last_span)),
1294                         substs: tcx.mk_substs_trait(place_ty, &[]),
1295                     };
1296                     self.prove_trait_ref(
1297                         trait_ref,
1298                         location.to_locations(),
1299                         ConstraintCategory::SizedBound,
1300                     );
1301                 }
1302             }
1303             StatementKind::AscribeUserType(box (ref place, ref projection), variance) => {
1304                 let place_ty = place.ty(body, tcx).ty;
1305                 if let Err(terr) = self.relate_type_and_user_type(
1306                     place_ty,
1307                     variance,
1308                     projection,
1309                     Locations::All(stmt.source_info.span),
1310                     ConstraintCategory::TypeAnnotation,
1311                 ) {
1312                     let annotation = &self.user_type_annotations[projection.base];
1313                     span_mirbug!(
1314                         self,
1315                         stmt,
1316                         "bad type assert ({:?} <: {:?} with projections {:?}): {:?}",
1317                         place_ty,
1318                         annotation,
1319                         projection.projs,
1320                         terr
1321                     );
1322                 }
1323             }
1324             StatementKind::CopyNonOverlapping(box rustc_middle::mir::CopyNonOverlapping {
1325                 ..
1326             }) => span_bug!(
1327                 stmt.source_info.span,
1328                 "Unexpected StatementKind::CopyNonOverlapping, should only appear after lowering_intrinsics",
1329             ),
1330             StatementKind::FakeRead(..)
1331             | StatementKind::StorageLive(..)
1332             | StatementKind::StorageDead(..)
1333             | StatementKind::Retag { .. }
1334             | StatementKind::Coverage(..)
1335             | StatementKind::Nop => {}
1336             StatementKind::Deinit(..) | StatementKind::SetDiscriminant { .. } => {
1337                 bug!("Statement not allowed in this MIR phase")
1338             }
1339         }
1340     }
1341
1342     #[instrument(skip(self, body, term_location), level = "debug")]
1343     fn check_terminator(
1344         &mut self,
1345         body: &Body<'tcx>,
1346         term: &Terminator<'tcx>,
1347         term_location: Location,
1348     ) {
1349         let tcx = self.tcx();
1350         match term.kind {
1351             TerminatorKind::Goto { .. }
1352             | TerminatorKind::Resume
1353             | TerminatorKind::Abort
1354             | TerminatorKind::Return
1355             | TerminatorKind::GeneratorDrop
1356             | TerminatorKind::Unreachable
1357             | TerminatorKind::Drop { .. }
1358             | TerminatorKind::FalseEdge { .. }
1359             | TerminatorKind::FalseUnwind { .. }
1360             | TerminatorKind::InlineAsm { .. } => {
1361                 // no checks needed for these
1362             }
1363
1364             TerminatorKind::DropAndReplace { ref place, ref value, target: _, unwind: _ } => {
1365                 let place_ty = place.ty(body, tcx).ty;
1366                 let rv_ty = value.ty(body, tcx);
1367
1368                 let locations = term_location.to_locations();
1369                 if let Err(terr) =
1370                     self.sub_types(rv_ty, place_ty, locations, ConstraintCategory::Assignment)
1371                 {
1372                     span_mirbug!(
1373                         self,
1374                         term,
1375                         "bad DropAndReplace ({:?} = {:?}): {:?}",
1376                         place_ty,
1377                         rv_ty,
1378                         terr
1379                     );
1380                 }
1381             }
1382             TerminatorKind::SwitchInt { ref discr, switch_ty, .. } => {
1383                 self.check_operand(discr, term_location);
1384
1385                 let discr_ty = discr.ty(body, tcx);
1386                 if let Err(terr) = self.sub_types(
1387                     discr_ty,
1388                     switch_ty,
1389                     term_location.to_locations(),
1390                     ConstraintCategory::Assignment,
1391                 ) {
1392                     span_mirbug!(
1393                         self,
1394                         term,
1395                         "bad SwitchInt ({:?} on {:?}): {:?}",
1396                         switch_ty,
1397                         discr_ty,
1398                         terr
1399                     );
1400                 }
1401                 if !switch_ty.is_integral() && !switch_ty.is_char() && !switch_ty.is_bool() {
1402                     span_mirbug!(self, term, "bad SwitchInt discr ty {:?}", switch_ty);
1403                 }
1404                 // FIXME: check the values
1405             }
1406             TerminatorKind::Call {
1407                 ref func, ref args, destination, target, from_hir_call, ..
1408             } => {
1409                 self.check_operand(func, term_location);
1410                 for arg in args {
1411                     self.check_operand(arg, term_location);
1412                 }
1413
1414                 let func_ty = func.ty(body, tcx);
1415                 debug!("check_terminator: call, func_ty={:?}", func_ty);
1416                 let sig = match func_ty.kind() {
1417                     ty::FnDef(..) | ty::FnPtr(_) => func_ty.fn_sig(tcx),
1418                     _ => {
1419                         span_mirbug!(self, term, "call to non-function {:?}", func_ty);
1420                         return;
1421                     }
1422                 };
1423                 let (sig, map) = self.infcx.replace_bound_vars_with_fresh_vars(
1424                     term.source_info.span,
1425                     LateBoundRegionConversionTime::FnCall,
1426                     sig,
1427                 );
1428                 let sig = self.normalize(sig, term_location);
1429                 self.check_call_dest(body, term, &sig, destination, target, term_location);
1430
1431                 self.prove_predicates(
1432                     sig.inputs_and_output
1433                         .iter()
1434                         .map(|ty| ty::Binder::dummy(ty::PredicateKind::WellFormed(ty.into()))),
1435                     term_location.to_locations(),
1436                     ConstraintCategory::Boring,
1437                 );
1438
1439                 // The ordinary liveness rules will ensure that all
1440                 // regions in the type of the callee are live here. We
1441                 // then further constrain the late-bound regions that
1442                 // were instantiated at the call site to be live as
1443                 // well. The resulting is that all the input (and
1444                 // output) types in the signature must be live, since
1445                 // all the inputs that fed into it were live.
1446                 for &late_bound_region in map.values() {
1447                     let region_vid =
1448                         self.borrowck_context.universal_regions.to_region_vid(late_bound_region);
1449                     self.borrowck_context
1450                         .constraints
1451                         .liveness_constraints
1452                         .add_element(region_vid, term_location);
1453                 }
1454
1455                 self.check_call_inputs(body, term, &sig, args, term_location, from_hir_call);
1456             }
1457             TerminatorKind::Assert { ref cond, ref msg, .. } => {
1458                 self.check_operand(cond, term_location);
1459
1460                 let cond_ty = cond.ty(body, tcx);
1461                 if cond_ty != tcx.types.bool {
1462                     span_mirbug!(self, term, "bad Assert ({:?}, not bool", cond_ty);
1463                 }
1464
1465                 if let AssertKind::BoundsCheck { ref len, ref index } = *msg {
1466                     if len.ty(body, tcx) != tcx.types.usize {
1467                         span_mirbug!(self, len, "bounds-check length non-usize {:?}", len)
1468                     }
1469                     if index.ty(body, tcx) != tcx.types.usize {
1470                         span_mirbug!(self, index, "bounds-check index non-usize {:?}", index)
1471                     }
1472                 }
1473             }
1474             TerminatorKind::Yield { ref value, .. } => {
1475                 self.check_operand(value, term_location);
1476
1477                 let value_ty = value.ty(body, tcx);
1478                 match body.yield_ty() {
1479                     None => span_mirbug!(self, term, "yield in non-generator"),
1480                     Some(ty) => {
1481                         if let Err(terr) = self.sub_types(
1482                             value_ty,
1483                             ty,
1484                             term_location.to_locations(),
1485                             ConstraintCategory::Yield,
1486                         ) {
1487                             span_mirbug!(
1488                                 self,
1489                                 term,
1490                                 "type of yield value is {:?}, but the yield type is {:?}: {:?}",
1491                                 value_ty,
1492                                 ty,
1493                                 terr
1494                             );
1495                         }
1496                     }
1497                 }
1498             }
1499         }
1500     }
1501
1502     fn check_call_dest(
1503         &mut self,
1504         body: &Body<'tcx>,
1505         term: &Terminator<'tcx>,
1506         sig: &ty::FnSig<'tcx>,
1507         destination: Place<'tcx>,
1508         target: Option<BasicBlock>,
1509         term_location: Location,
1510     ) {
1511         let tcx = self.tcx();
1512         match target {
1513             Some(_) => {
1514                 let dest_ty = destination.ty(body, tcx).ty;
1515                 let dest_ty = self.normalize(dest_ty, term_location);
1516                 let category = match destination.as_local() {
1517                     Some(RETURN_PLACE) => {
1518                         if let BorrowCheckContext {
1519                             universal_regions:
1520                                 UniversalRegions {
1521                                     defining_ty:
1522                                         DefiningTy::Const(def_id, _)
1523                                         | DefiningTy::InlineConst(def_id, _),
1524                                     ..
1525                                 },
1526                             ..
1527                         } = self.borrowck_context
1528                         {
1529                             if tcx.is_static(*def_id) {
1530                                 ConstraintCategory::UseAsStatic
1531                             } else {
1532                                 ConstraintCategory::UseAsConst
1533                             }
1534                         } else {
1535                             ConstraintCategory::Return(ReturnConstraint::Normal)
1536                         }
1537                     }
1538                     Some(l) if !body.local_decls[l].is_user_variable() => {
1539                         ConstraintCategory::Boring
1540                     }
1541                     _ => ConstraintCategory::Assignment,
1542                 };
1543
1544                 let locations = term_location.to_locations();
1545
1546                 if let Err(terr) = self.sub_types(sig.output(), dest_ty, locations, category) {
1547                     span_mirbug!(
1548                         self,
1549                         term,
1550                         "call dest mismatch ({:?} <- {:?}): {:?}",
1551                         dest_ty,
1552                         sig.output(),
1553                         terr
1554                     );
1555                 }
1556
1557                 // When `unsized_fn_params` and `unsized_locals` are both not enabled,
1558                 // this check is done at `check_local`.
1559                 if self.unsized_feature_enabled() {
1560                     let span = term.source_info.span;
1561                     self.ensure_place_sized(dest_ty, span);
1562                 }
1563             }
1564             None => {
1565                 if !self
1566                     .tcx()
1567                     .conservative_is_privately_uninhabited(self.param_env.and(sig.output()))
1568                 {
1569                     span_mirbug!(self, term, "call to converging function {:?} w/o dest", sig);
1570                 }
1571             }
1572         }
1573     }
1574
1575     fn check_call_inputs(
1576         &mut self,
1577         body: &Body<'tcx>,
1578         term: &Terminator<'tcx>,
1579         sig: &ty::FnSig<'tcx>,
1580         args: &[Operand<'tcx>],
1581         term_location: Location,
1582         from_hir_call: bool,
1583     ) {
1584         debug!("check_call_inputs({:?}, {:?})", sig, args);
1585         if args.len() < sig.inputs().len() || (args.len() > sig.inputs().len() && !sig.c_variadic) {
1586             span_mirbug!(self, term, "call to {:?} with wrong # of args", sig);
1587         }
1588         for (n, (fn_arg, op_arg)) in iter::zip(sig.inputs(), args).enumerate() {
1589             let op_arg_ty = op_arg.ty(body, self.tcx());
1590             let op_arg_ty = self.normalize(op_arg_ty, term_location);
1591             let category = if from_hir_call {
1592                 ConstraintCategory::CallArgument
1593             } else {
1594                 ConstraintCategory::Boring
1595             };
1596             if let Err(terr) =
1597                 self.sub_types(op_arg_ty, *fn_arg, term_location.to_locations(), category)
1598             {
1599                 span_mirbug!(
1600                     self,
1601                     term,
1602                     "bad arg #{:?} ({:?} <- {:?}): {:?}",
1603                     n,
1604                     fn_arg,
1605                     op_arg_ty,
1606                     terr
1607                 );
1608             }
1609         }
1610     }
1611
1612     fn check_iscleanup(&mut self, body: &Body<'tcx>, block_data: &BasicBlockData<'tcx>) {
1613         let is_cleanup = block_data.is_cleanup;
1614         self.last_span = block_data.terminator().source_info.span;
1615         match block_data.terminator().kind {
1616             TerminatorKind::Goto { target } => {
1617                 self.assert_iscleanup(body, block_data, target, is_cleanup)
1618             }
1619             TerminatorKind::SwitchInt { ref targets, .. } => {
1620                 for target in targets.all_targets() {
1621                     self.assert_iscleanup(body, block_data, *target, is_cleanup);
1622                 }
1623             }
1624             TerminatorKind::Resume => {
1625                 if !is_cleanup {
1626                     span_mirbug!(self, block_data, "resume on non-cleanup block!")
1627                 }
1628             }
1629             TerminatorKind::Abort => {
1630                 if !is_cleanup {
1631                     span_mirbug!(self, block_data, "abort on non-cleanup block!")
1632                 }
1633             }
1634             TerminatorKind::Return => {
1635                 if is_cleanup {
1636                     span_mirbug!(self, block_data, "return on cleanup block")
1637                 }
1638             }
1639             TerminatorKind::GeneratorDrop { .. } => {
1640                 if is_cleanup {
1641                     span_mirbug!(self, block_data, "generator_drop in cleanup block")
1642                 }
1643             }
1644             TerminatorKind::Yield { resume, drop, .. } => {
1645                 if is_cleanup {
1646                     span_mirbug!(self, block_data, "yield in cleanup block")
1647                 }
1648                 self.assert_iscleanup(body, block_data, resume, is_cleanup);
1649                 if let Some(drop) = drop {
1650                     self.assert_iscleanup(body, block_data, drop, is_cleanup);
1651                 }
1652             }
1653             TerminatorKind::Unreachable => {}
1654             TerminatorKind::Drop { target, unwind, .. }
1655             | TerminatorKind::DropAndReplace { target, unwind, .. }
1656             | TerminatorKind::Assert { target, cleanup: unwind, .. } => {
1657                 self.assert_iscleanup(body, block_data, target, is_cleanup);
1658                 if let Some(unwind) = unwind {
1659                     if is_cleanup {
1660                         span_mirbug!(self, block_data, "unwind on cleanup block")
1661                     }
1662                     self.assert_iscleanup(body, block_data, unwind, true);
1663                 }
1664             }
1665             TerminatorKind::Call { ref target, cleanup, .. } => {
1666                 if let &Some(target) = target {
1667                     self.assert_iscleanup(body, block_data, target, is_cleanup);
1668                 }
1669                 if let Some(cleanup) = cleanup {
1670                     if is_cleanup {
1671                         span_mirbug!(self, block_data, "cleanup on cleanup block")
1672                     }
1673                     self.assert_iscleanup(body, block_data, cleanup, true);
1674                 }
1675             }
1676             TerminatorKind::FalseEdge { real_target, imaginary_target } => {
1677                 self.assert_iscleanup(body, block_data, real_target, is_cleanup);
1678                 self.assert_iscleanup(body, block_data, imaginary_target, is_cleanup);
1679             }
1680             TerminatorKind::FalseUnwind { real_target, unwind } => {
1681                 self.assert_iscleanup(body, block_data, real_target, is_cleanup);
1682                 if let Some(unwind) = unwind {
1683                     if is_cleanup {
1684                         span_mirbug!(self, block_data, "cleanup in cleanup block via false unwind");
1685                     }
1686                     self.assert_iscleanup(body, block_data, unwind, true);
1687                 }
1688             }
1689             TerminatorKind::InlineAsm { destination, cleanup, .. } => {
1690                 if let Some(target) = destination {
1691                     self.assert_iscleanup(body, block_data, target, is_cleanup);
1692                 }
1693                 if let Some(cleanup) = cleanup {
1694                     if is_cleanup {
1695                         span_mirbug!(self, block_data, "cleanup on cleanup block")
1696                     }
1697                     self.assert_iscleanup(body, block_data, cleanup, true);
1698                 }
1699             }
1700         }
1701     }
1702
1703     fn assert_iscleanup(
1704         &mut self,
1705         body: &Body<'tcx>,
1706         ctxt: &dyn fmt::Debug,
1707         bb: BasicBlock,
1708         iscleanuppad: bool,
1709     ) {
1710         if body[bb].is_cleanup != iscleanuppad {
1711             span_mirbug!(self, ctxt, "cleanuppad mismatch: {:?} should be {:?}", bb, iscleanuppad);
1712         }
1713     }
1714
1715     fn check_local(&mut self, body: &Body<'tcx>, local: Local, local_decl: &LocalDecl<'tcx>) {
1716         match body.local_kind(local) {
1717             LocalKind::ReturnPointer | LocalKind::Arg => {
1718                 // return values of normal functions are required to be
1719                 // sized by typeck, but return values of ADT constructors are
1720                 // not because we don't include a `Self: Sized` bounds on them.
1721                 //
1722                 // Unbound parts of arguments were never required to be Sized
1723                 // - maybe we should make that a warning.
1724                 return;
1725             }
1726             LocalKind::Var | LocalKind::Temp => {}
1727         }
1728
1729         // When `unsized_fn_params` or `unsized_locals` is enabled, only function calls
1730         // and nullary ops are checked in `check_call_dest`.
1731         if !self.unsized_feature_enabled() {
1732             let span = local_decl.source_info.span;
1733             let ty = local_decl.ty;
1734             self.ensure_place_sized(ty, span);
1735         }
1736     }
1737
1738     fn ensure_place_sized(&mut self, ty: Ty<'tcx>, span: Span) {
1739         let tcx = self.tcx();
1740
1741         // Erase the regions from `ty` to get a global type.  The
1742         // `Sized` bound in no way depends on precise regions, so this
1743         // shouldn't affect `is_sized`.
1744         let erased_ty = tcx.erase_regions(ty);
1745         if !erased_ty.is_sized(tcx.at(span), self.param_env) {
1746             // in current MIR construction, all non-control-flow rvalue
1747             // expressions evaluate through `as_temp` or `into` a return
1748             // slot or local, so to find all unsized rvalues it is enough
1749             // to check all temps, return slots and locals.
1750             if self.reported_errors.replace((ty, span)).is_none() {
1751                 let mut diag = struct_span_err!(
1752                     self.tcx().sess,
1753                     span,
1754                     E0161,
1755                     "cannot move a value of type {0}: the size of {0} \
1756                      cannot be statically determined",
1757                     ty
1758                 );
1759
1760                 // While this is located in `nll::typeck` this error is not
1761                 // an NLL error, it's a required check to prevent creation
1762                 // of unsized rvalues in a call expression.
1763                 diag.emit();
1764             }
1765         }
1766     }
1767
1768     fn aggregate_field_ty(
1769         &mut self,
1770         ak: &AggregateKind<'tcx>,
1771         field_index: usize,
1772         location: Location,
1773     ) -> Result<Ty<'tcx>, FieldAccessError> {
1774         let tcx = self.tcx();
1775
1776         match *ak {
1777             AggregateKind::Adt(adt_did, variant_index, substs, _, active_field_index) => {
1778                 let def = tcx.adt_def(adt_did);
1779                 let variant = &def.variant(variant_index);
1780                 let adj_field_index = active_field_index.unwrap_or(field_index);
1781                 if let Some(field) = variant.fields.get(adj_field_index) {
1782                     Ok(self.normalize(field.ty(tcx, substs), location))
1783                 } else {
1784                     Err(FieldAccessError::OutOfRange { field_count: variant.fields.len() })
1785                 }
1786             }
1787             AggregateKind::Closure(_, substs) => {
1788                 match substs.as_closure().upvar_tys().nth(field_index) {
1789                     Some(ty) => Ok(ty),
1790                     None => Err(FieldAccessError::OutOfRange {
1791                         field_count: substs.as_closure().upvar_tys().count(),
1792                     }),
1793                 }
1794             }
1795             AggregateKind::Generator(_, substs, _) => {
1796                 // It doesn't make sense to look at a field beyond the prefix;
1797                 // these require a variant index, and are not initialized in
1798                 // aggregate rvalues.
1799                 match substs.as_generator().prefix_tys().nth(field_index) {
1800                     Some(ty) => Ok(ty),
1801                     None => Err(FieldAccessError::OutOfRange {
1802                         field_count: substs.as_generator().prefix_tys().count(),
1803                     }),
1804                 }
1805             }
1806             AggregateKind::Array(ty) => Ok(ty),
1807             AggregateKind::Tuple => {
1808                 unreachable!("This should have been covered in check_rvalues");
1809             }
1810         }
1811     }
1812
1813     fn check_operand(&mut self, op: &Operand<'tcx>, location: Location) {
1814         if let Operand::Constant(constant) = op {
1815             let maybe_uneval = match constant.literal {
1816                 ConstantKind::Ty(ct) => match ct.val() {
1817                     ty::ConstKind::Unevaluated(uv) => Some(uv),
1818                     _ => None,
1819                 },
1820                 _ => None,
1821             };
1822             if let Some(uv) = maybe_uneval {
1823                 if uv.promoted.is_none() {
1824                     let tcx = self.tcx();
1825                     let def_id = uv.def.def_id_for_type_of();
1826                     if tcx.def_kind(def_id) == DefKind::InlineConst {
1827                         let predicates = self.prove_closure_bounds(
1828                             tcx,
1829                             def_id.expect_local(),
1830                             uv.substs,
1831                             location,
1832                         );
1833                         self.normalize_and_prove_instantiated_predicates(
1834                             def_id,
1835                             predicates,
1836                             location.to_locations(),
1837                         );
1838                     }
1839                 }
1840             }
1841         }
1842     }
1843
1844     fn check_rvalue(&mut self, body: &Body<'tcx>, rvalue: &Rvalue<'tcx>, location: Location) {
1845         let tcx = self.tcx();
1846
1847         match rvalue {
1848             Rvalue::Aggregate(ak, ops) => {
1849                 for op in ops {
1850                     self.check_operand(op, location);
1851                 }
1852                 self.check_aggregate_rvalue(&body, rvalue, ak, ops, location)
1853             }
1854
1855             Rvalue::Repeat(operand, len) => {
1856                 self.check_operand(operand, location);
1857
1858                 // If the length cannot be evaluated we must assume that the length can be larger
1859                 // than 1.
1860                 // If the length is larger than 1, the repeat expression will need to copy the
1861                 // element, so we require the `Copy` trait.
1862                 if len.try_eval_usize(tcx, self.param_env).map_or(true, |len| len > 1) {
1863                     match operand {
1864                         Operand::Copy(..) | Operand::Constant(..) => {
1865                             // These are always okay: direct use of a const, or a value that can evidently be copied.
1866                         }
1867                         Operand::Move(place) => {
1868                             // Make sure that repeated elements implement `Copy`.
1869                             let span = body.source_info(location).span;
1870                             let ty = place.ty(body, tcx).ty;
1871                             let trait_ref = ty::TraitRef::new(
1872                                 tcx.require_lang_item(LangItem::Copy, Some(span)),
1873                                 tcx.mk_substs_trait(ty, &[]),
1874                             );
1875
1876                             self.prove_trait_ref(
1877                                 trait_ref,
1878                                 Locations::Single(location),
1879                                 ConstraintCategory::CopyBound,
1880                             );
1881                         }
1882                     }
1883                 }
1884             }
1885
1886             &Rvalue::NullaryOp(_, ty) => {
1887                 let trait_ref = ty::TraitRef {
1888                     def_id: tcx.require_lang_item(LangItem::Sized, Some(self.last_span)),
1889                     substs: tcx.mk_substs_trait(ty, &[]),
1890                 };
1891
1892                 self.prove_trait_ref(
1893                     trait_ref,
1894                     location.to_locations(),
1895                     ConstraintCategory::SizedBound,
1896                 );
1897             }
1898
1899             Rvalue::ShallowInitBox(operand, ty) => {
1900                 self.check_operand(operand, location);
1901
1902                 let trait_ref = ty::TraitRef {
1903                     def_id: tcx.require_lang_item(LangItem::Sized, Some(self.last_span)),
1904                     substs: tcx.mk_substs_trait(*ty, &[]),
1905                 };
1906
1907                 self.prove_trait_ref(
1908                     trait_ref,
1909                     location.to_locations(),
1910                     ConstraintCategory::SizedBound,
1911                 );
1912             }
1913
1914             Rvalue::Cast(cast_kind, op, ty) => {
1915                 self.check_operand(op, location);
1916
1917                 match cast_kind {
1918                     CastKind::Pointer(PointerCast::ReifyFnPointer) => {
1919                         let fn_sig = op.ty(body, tcx).fn_sig(tcx);
1920
1921                         // The type that we see in the fcx is like
1922                         // `foo::<'a, 'b>`, where `foo` is the path to a
1923                         // function definition. When we extract the
1924                         // signature, it comes from the `fn_sig` query,
1925                         // and hence may contain unnormalized results.
1926                         let fn_sig = self.normalize(fn_sig, location);
1927
1928                         let ty_fn_ptr_from = tcx.mk_fn_ptr(fn_sig);
1929
1930                         if let Err(terr) = self.eq_types(
1931                             *ty,
1932                             ty_fn_ptr_from,
1933                             location.to_locations(),
1934                             ConstraintCategory::Cast,
1935                         ) {
1936                             span_mirbug!(
1937                                 self,
1938                                 rvalue,
1939                                 "equating {:?} with {:?} yields {:?}",
1940                                 ty_fn_ptr_from,
1941                                 ty,
1942                                 terr
1943                             );
1944                         }
1945                     }
1946
1947                     CastKind::Pointer(PointerCast::ClosureFnPointer(unsafety)) => {
1948                         let sig = match op.ty(body, tcx).kind() {
1949                             ty::Closure(_, substs) => substs.as_closure().sig(),
1950                             _ => bug!(),
1951                         };
1952                         let ty_fn_ptr_from = tcx.mk_fn_ptr(tcx.signature_unclosure(sig, *unsafety));
1953
1954                         if let Err(terr) = self.eq_types(
1955                             *ty,
1956                             ty_fn_ptr_from,
1957                             location.to_locations(),
1958                             ConstraintCategory::Cast,
1959                         ) {
1960                             span_mirbug!(
1961                                 self,
1962                                 rvalue,
1963                                 "equating {:?} with {:?} yields {:?}",
1964                                 ty_fn_ptr_from,
1965                                 ty,
1966                                 terr
1967                             );
1968                         }
1969                     }
1970
1971                     CastKind::Pointer(PointerCast::UnsafeFnPointer) => {
1972                         let fn_sig = op.ty(body, tcx).fn_sig(tcx);
1973
1974                         // The type that we see in the fcx is like
1975                         // `foo::<'a, 'b>`, where `foo` is the path to a
1976                         // function definition. When we extract the
1977                         // signature, it comes from the `fn_sig` query,
1978                         // and hence may contain unnormalized results.
1979                         let fn_sig = self.normalize(fn_sig, location);
1980
1981                         let ty_fn_ptr_from = tcx.safe_to_unsafe_fn_ty(fn_sig);
1982
1983                         if let Err(terr) = self.eq_types(
1984                             *ty,
1985                             ty_fn_ptr_from,
1986                             location.to_locations(),
1987                             ConstraintCategory::Cast,
1988                         ) {
1989                             span_mirbug!(
1990                                 self,
1991                                 rvalue,
1992                                 "equating {:?} with {:?} yields {:?}",
1993                                 ty_fn_ptr_from,
1994                                 ty,
1995                                 terr
1996                             );
1997                         }
1998                     }
1999
2000                     CastKind::Pointer(PointerCast::Unsize) => {
2001                         let &ty = ty;
2002                         let trait_ref = ty::TraitRef {
2003                             def_id: tcx
2004                                 .require_lang_item(LangItem::CoerceUnsized, Some(self.last_span)),
2005                             substs: tcx.mk_substs_trait(op.ty(body, tcx), &[ty.into()]),
2006                         };
2007
2008                         self.prove_trait_ref(
2009                             trait_ref,
2010                             location.to_locations(),
2011                             ConstraintCategory::Cast,
2012                         );
2013                     }
2014
2015                     CastKind::Pointer(PointerCast::MutToConstPointer) => {
2016                         let ty::RawPtr(ty::TypeAndMut {
2017                             ty: ty_from,
2018                             mutbl: hir::Mutability::Mut,
2019                         }) = op.ty(body, tcx).kind() else {
2020                             span_mirbug!(
2021                                 self,
2022                                 rvalue,
2023                                 "unexpected base type for cast {:?}",
2024                                 ty,
2025                             );
2026                             return;
2027                         };
2028                         let ty::RawPtr(ty::TypeAndMut {
2029                             ty: ty_to,
2030                             mutbl: hir::Mutability::Not,
2031                         }) = ty.kind() else {
2032                             span_mirbug!(
2033                                 self,
2034                                 rvalue,
2035                                 "unexpected target type for cast {:?}",
2036                                 ty,
2037                             );
2038                             return;
2039                         };
2040                         if let Err(terr) = self.sub_types(
2041                             *ty_from,
2042                             *ty_to,
2043                             location.to_locations(),
2044                             ConstraintCategory::Cast,
2045                         ) {
2046                             span_mirbug!(
2047                                 self,
2048                                 rvalue,
2049                                 "relating {:?} with {:?} yields {:?}",
2050                                 ty_from,
2051                                 ty_to,
2052                                 terr
2053                             );
2054                         }
2055                     }
2056
2057                     CastKind::Pointer(PointerCast::ArrayToPointer) => {
2058                         let ty_from = op.ty(body, tcx);
2059
2060                         let opt_ty_elem_mut = match ty_from.kind() {
2061                             ty::RawPtr(ty::TypeAndMut { mutbl: array_mut, ty: array_ty }) => {
2062                                 match array_ty.kind() {
2063                                     ty::Array(ty_elem, _) => Some((ty_elem, *array_mut)),
2064                                     _ => None,
2065                                 }
2066                             }
2067                             _ => None,
2068                         };
2069
2070                         let Some((ty_elem, ty_mut)) = opt_ty_elem_mut else {
2071                             span_mirbug!(
2072                                 self,
2073                                 rvalue,
2074                                 "ArrayToPointer cast from unexpected type {:?}",
2075                                 ty_from,
2076                             );
2077                             return;
2078                         };
2079
2080                         let (ty_to, ty_to_mut) = match ty.kind() {
2081                             ty::RawPtr(ty::TypeAndMut { mutbl: ty_to_mut, ty: ty_to }) => {
2082                                 (ty_to, *ty_to_mut)
2083                             }
2084                             _ => {
2085                                 span_mirbug!(
2086                                     self,
2087                                     rvalue,
2088                                     "ArrayToPointer cast to unexpected type {:?}",
2089                                     ty,
2090                                 );
2091                                 return;
2092                             }
2093                         };
2094
2095                         if ty_to_mut == Mutability::Mut && ty_mut == Mutability::Not {
2096                             span_mirbug!(
2097                                 self,
2098                                 rvalue,
2099                                 "ArrayToPointer cast from const {:?} to mut {:?}",
2100                                 ty,
2101                                 ty_to
2102                             );
2103                             return;
2104                         }
2105
2106                         if let Err(terr) = self.sub_types(
2107                             *ty_elem,
2108                             *ty_to,
2109                             location.to_locations(),
2110                             ConstraintCategory::Cast,
2111                         ) {
2112                             span_mirbug!(
2113                                 self,
2114                                 rvalue,
2115                                 "relating {:?} with {:?} yields {:?}",
2116                                 ty_elem,
2117                                 ty_to,
2118                                 terr
2119                             )
2120                         }
2121                     }
2122
2123                     CastKind::Misc => {
2124                         let ty_from = op.ty(body, tcx);
2125                         let cast_ty_from = CastTy::from_ty(ty_from);
2126                         let cast_ty_to = CastTy::from_ty(*ty);
2127                         match (cast_ty_from, cast_ty_to) {
2128                             (None, _)
2129                             | (_, None | Some(CastTy::FnPtr))
2130                             | (Some(CastTy::Float), Some(CastTy::Ptr(_)))
2131                             | (Some(CastTy::Ptr(_) | CastTy::FnPtr), Some(CastTy::Float)) => {
2132                                 span_mirbug!(self, rvalue, "Invalid cast {:?} -> {:?}", ty_from, ty,)
2133                             }
2134                             (
2135                                 Some(CastTy::Int(_)),
2136                                 Some(CastTy::Int(_) | CastTy::Float | CastTy::Ptr(_)),
2137                             )
2138                             | (Some(CastTy::Float), Some(CastTy::Int(_) | CastTy::Float))
2139                             | (Some(CastTy::Ptr(_)), Some(CastTy::Int(_) | CastTy::Ptr(_)))
2140                             | (Some(CastTy::FnPtr), Some(CastTy::Int(_) | CastTy::Ptr(_))) => (),
2141                         }
2142                     }
2143                 }
2144             }
2145
2146             Rvalue::Ref(region, _borrow_kind, borrowed_place) => {
2147                 self.add_reborrow_constraint(&body, location, *region, borrowed_place);
2148             }
2149
2150             Rvalue::BinaryOp(
2151                 BinOp::Eq | BinOp::Ne | BinOp::Lt | BinOp::Le | BinOp::Gt | BinOp::Ge,
2152                 box (left, right),
2153             ) => {
2154                 self.check_operand(left, location);
2155                 self.check_operand(right, location);
2156
2157                 let ty_left = left.ty(body, tcx);
2158                 match ty_left.kind() {
2159                     // Types with regions are comparable if they have a common super-type.
2160                     ty::RawPtr(_) | ty::FnPtr(_) => {
2161                         let ty_right = right.ty(body, tcx);
2162                         let common_ty = self.infcx.next_ty_var(TypeVariableOrigin {
2163                             kind: TypeVariableOriginKind::MiscVariable,
2164                             span: body.source_info(location).span,
2165                         });
2166                         self.sub_types(
2167                             ty_left,
2168                             common_ty,
2169                             location.to_locations(),
2170                             ConstraintCategory::Boring,
2171                         )
2172                         .unwrap_or_else(|err| {
2173                             bug!("Could not equate type variable with {:?}: {:?}", ty_left, err)
2174                         });
2175                         if let Err(terr) = self.sub_types(
2176                             ty_right,
2177                             common_ty,
2178                             location.to_locations(),
2179                             ConstraintCategory::Boring,
2180                         ) {
2181                             span_mirbug!(
2182                                 self,
2183                                 rvalue,
2184                                 "unexpected comparison types {:?} and {:?} yields {:?}",
2185                                 ty_left,
2186                                 ty_right,
2187                                 terr
2188                             )
2189                         }
2190                     }
2191                     // For types with no regions we can just check that the
2192                     // both operands have the same type.
2193                     ty::Int(_) | ty::Uint(_) | ty::Bool | ty::Char | ty::Float(_)
2194                         if ty_left == right.ty(body, tcx) => {}
2195                     // Other types are compared by trait methods, not by
2196                     // `Rvalue::BinaryOp`.
2197                     _ => span_mirbug!(
2198                         self,
2199                         rvalue,
2200                         "unexpected comparison types {:?} and {:?}",
2201                         ty_left,
2202                         right.ty(body, tcx)
2203                     ),
2204                 }
2205             }
2206
2207             Rvalue::Use(operand) | Rvalue::UnaryOp(_, operand) => {
2208                 self.check_operand(operand, location);
2209             }
2210
2211             Rvalue::BinaryOp(_, box (left, right))
2212             | Rvalue::CheckedBinaryOp(_, box (left, right)) => {
2213                 self.check_operand(left, location);
2214                 self.check_operand(right, location);
2215             }
2216
2217             Rvalue::AddressOf(..)
2218             | Rvalue::ThreadLocalRef(..)
2219             | Rvalue::Len(..)
2220             | Rvalue::Discriminant(..) => {}
2221         }
2222     }
2223
2224     /// If this rvalue supports a user-given type annotation, then
2225     /// extract and return it. This represents the final type of the
2226     /// rvalue and will be unified with the inferred type.
2227     fn rvalue_user_ty(&self, rvalue: &Rvalue<'tcx>) -> Option<UserTypeAnnotationIndex> {
2228         match rvalue {
2229             Rvalue::Use(_)
2230             | Rvalue::ThreadLocalRef(_)
2231             | Rvalue::Repeat(..)
2232             | Rvalue::Ref(..)
2233             | Rvalue::AddressOf(..)
2234             | Rvalue::Len(..)
2235             | Rvalue::Cast(..)
2236             | Rvalue::ShallowInitBox(..)
2237             | Rvalue::BinaryOp(..)
2238             | Rvalue::CheckedBinaryOp(..)
2239             | Rvalue::NullaryOp(..)
2240             | Rvalue::UnaryOp(..)
2241             | Rvalue::Discriminant(..) => None,
2242
2243             Rvalue::Aggregate(aggregate, _) => match **aggregate {
2244                 AggregateKind::Adt(_, _, _, user_ty, _) => user_ty,
2245                 AggregateKind::Array(_) => None,
2246                 AggregateKind::Tuple => None,
2247                 AggregateKind::Closure(_, _) => None,
2248                 AggregateKind::Generator(_, _, _) => None,
2249             },
2250         }
2251     }
2252
2253     fn check_aggregate_rvalue(
2254         &mut self,
2255         body: &Body<'tcx>,
2256         rvalue: &Rvalue<'tcx>,
2257         aggregate_kind: &AggregateKind<'tcx>,
2258         operands: &[Operand<'tcx>],
2259         location: Location,
2260     ) {
2261         let tcx = self.tcx();
2262
2263         self.prove_aggregate_predicates(aggregate_kind, location);
2264
2265         if *aggregate_kind == AggregateKind::Tuple {
2266             // tuple rvalue field type is always the type of the op. Nothing to check here.
2267             return;
2268         }
2269
2270         for (i, operand) in operands.iter().enumerate() {
2271             let field_ty = match self.aggregate_field_ty(aggregate_kind, i, location) {
2272                 Ok(field_ty) => field_ty,
2273                 Err(FieldAccessError::OutOfRange { field_count }) => {
2274                     span_mirbug!(
2275                         self,
2276                         rvalue,
2277                         "accessed field #{} but variant only has {}",
2278                         i,
2279                         field_count
2280                     );
2281                     continue;
2282                 }
2283             };
2284             let operand_ty = operand.ty(body, tcx);
2285             let operand_ty = self.normalize(operand_ty, location);
2286
2287             if let Err(terr) = self.sub_types(
2288                 operand_ty,
2289                 field_ty,
2290                 location.to_locations(),
2291                 ConstraintCategory::Boring,
2292             ) {
2293                 span_mirbug!(
2294                     self,
2295                     rvalue,
2296                     "{:?} is not a subtype of {:?}: {:?}",
2297                     operand_ty,
2298                     field_ty,
2299                     terr
2300                 );
2301             }
2302         }
2303     }
2304
2305     /// Adds the constraints that arise from a borrow expression `&'a P` at the location `L`.
2306     ///
2307     /// # Parameters
2308     ///
2309     /// - `location`: the location `L` where the borrow expression occurs
2310     /// - `borrow_region`: the region `'a` associated with the borrow
2311     /// - `borrowed_place`: the place `P` being borrowed
2312     fn add_reborrow_constraint(
2313         &mut self,
2314         body: &Body<'tcx>,
2315         location: Location,
2316         borrow_region: ty::Region<'tcx>,
2317         borrowed_place: &Place<'tcx>,
2318     ) {
2319         // These constraints are only meaningful during borrowck:
2320         let BorrowCheckContext { borrow_set, location_table, all_facts, constraints, .. } =
2321             self.borrowck_context;
2322
2323         // In Polonius mode, we also push a `loan_issued_at` fact
2324         // linking the loan to the region (in some cases, though,
2325         // there is no loan associated with this borrow expression --
2326         // that occurs when we are borrowing an unsafe place, for
2327         // example).
2328         if let Some(all_facts) = all_facts {
2329             let _prof_timer = self.infcx.tcx.prof.generic_activity("polonius_fact_generation");
2330             if let Some(borrow_index) = borrow_set.get_index_of(&location) {
2331                 let region_vid = borrow_region.to_region_vid();
2332                 all_facts.loan_issued_at.push((
2333                     region_vid,
2334                     borrow_index,
2335                     location_table.mid_index(location),
2336                 ));
2337             }
2338         }
2339
2340         // If we are reborrowing the referent of another reference, we
2341         // need to add outlives relationships. In a case like `&mut
2342         // *p`, where the `p` has type `&'b mut Foo`, for example, we
2343         // need to ensure that `'b: 'a`.
2344
2345         debug!(
2346             "add_reborrow_constraint({:?}, {:?}, {:?})",
2347             location, borrow_region, borrowed_place
2348         );
2349
2350         let mut cursor = borrowed_place.projection.as_ref();
2351         let tcx = self.infcx.tcx;
2352         let field = path_utils::is_upvar_field_projection(
2353             tcx,
2354             &self.borrowck_context.upvars,
2355             borrowed_place.as_ref(),
2356             body,
2357         );
2358         let category = if let Some(field) = field {
2359             ConstraintCategory::ClosureUpvar(field)
2360         } else {
2361             ConstraintCategory::Boring
2362         };
2363
2364         while let [proj_base @ .., elem] = cursor {
2365             cursor = proj_base;
2366
2367             debug!("add_reborrow_constraint - iteration {:?}", elem);
2368
2369             match elem {
2370                 ProjectionElem::Deref => {
2371                     let base_ty = Place::ty_from(borrowed_place.local, proj_base, body, tcx).ty;
2372
2373                     debug!("add_reborrow_constraint - base_ty = {:?}", base_ty);
2374                     match base_ty.kind() {
2375                         ty::Ref(ref_region, _, mutbl) => {
2376                             constraints.outlives_constraints.push(OutlivesConstraint {
2377                                 sup: ref_region.to_region_vid(),
2378                                 sub: borrow_region.to_region_vid(),
2379                                 locations: location.to_locations(),
2380                                 span: location.to_locations().span(body),
2381                                 category,
2382                                 variance_info: ty::VarianceDiagInfo::default(),
2383                             });
2384
2385                             match mutbl {
2386                                 hir::Mutability::Not => {
2387                                     // Immutable reference. We don't need the base
2388                                     // to be valid for the entire lifetime of
2389                                     // the borrow.
2390                                     break;
2391                                 }
2392                                 hir::Mutability::Mut => {
2393                                     // Mutable reference. We *do* need the base
2394                                     // to be valid, because after the base becomes
2395                                     // invalid, someone else can use our mutable deref.
2396
2397                                     // This is in order to make the following function
2398                                     // illegal:
2399                                     // ```
2400                                     // fn unsafe_deref<'a, 'b>(x: &'a &'b mut T) -> &'b mut T {
2401                                     //     &mut *x
2402                                     // }
2403                                     // ```
2404                                     //
2405                                     // As otherwise you could clone `&mut T` using the
2406                                     // following function:
2407                                     // ```
2408                                     // fn bad(x: &mut T) -> (&mut T, &mut T) {
2409                                     //     let my_clone = unsafe_deref(&'a x);
2410                                     //     ENDREGION 'a;
2411                                     //     (my_clone, x)
2412                                     // }
2413                                     // ```
2414                                 }
2415                             }
2416                         }
2417                         ty::RawPtr(..) => {
2418                             // deref of raw pointer, guaranteed to be valid
2419                             break;
2420                         }
2421                         ty::Adt(def, _) if def.is_box() => {
2422                             // deref of `Box`, need the base to be valid - propagate
2423                         }
2424                         _ => bug!("unexpected deref ty {:?} in {:?}", base_ty, borrowed_place),
2425                     }
2426                 }
2427                 ProjectionElem::Field(..)
2428                 | ProjectionElem::Downcast(..)
2429                 | ProjectionElem::Index(..)
2430                 | ProjectionElem::ConstantIndex { .. }
2431                 | ProjectionElem::Subslice { .. } => {
2432                     // other field access
2433                 }
2434             }
2435         }
2436     }
2437
2438     fn prove_aggregate_predicates(
2439         &mut self,
2440         aggregate_kind: &AggregateKind<'tcx>,
2441         location: Location,
2442     ) {
2443         let tcx = self.tcx();
2444
2445         debug!(
2446             "prove_aggregate_predicates(aggregate_kind={:?}, location={:?})",
2447             aggregate_kind, location
2448         );
2449
2450         let (def_id, instantiated_predicates) = match aggregate_kind {
2451             AggregateKind::Adt(adt_did, _, substs, _, _) => {
2452                 (*adt_did, tcx.predicates_of(*adt_did).instantiate(tcx, substs))
2453             }
2454
2455             // For closures, we have some **extra requirements** we
2456             //
2457             // have to check. In particular, in their upvars and
2458             // signatures, closures often reference various regions
2459             // from the surrounding function -- we call those the
2460             // closure's free regions. When we borrow-check (and hence
2461             // region-check) closures, we may find that the closure
2462             // requires certain relationships between those free
2463             // regions. However, because those free regions refer to
2464             // portions of the CFG of their caller, the closure is not
2465             // in a position to verify those relationships. In that
2466             // case, the requirements get "propagated" to us, and so
2467             // we have to solve them here where we instantiate the
2468             // closure.
2469             //
2470             // Despite the opacity of the previous paragraph, this is
2471             // actually relatively easy to understand in terms of the
2472             // desugaring. A closure gets desugared to a struct, and
2473             // these extra requirements are basically like where
2474             // clauses on the struct.
2475             AggregateKind::Closure(def_id, substs)
2476             | AggregateKind::Generator(def_id, substs, _) => {
2477                 (*def_id, self.prove_closure_bounds(tcx, def_id.expect_local(), substs, location))
2478             }
2479
2480             AggregateKind::Array(_) | AggregateKind::Tuple => {
2481                 (CRATE_DEF_ID.to_def_id(), ty::InstantiatedPredicates::empty())
2482             }
2483         };
2484
2485         self.normalize_and_prove_instantiated_predicates(
2486             def_id,
2487             instantiated_predicates,
2488             location.to_locations(),
2489         );
2490     }
2491
2492     fn prove_closure_bounds(
2493         &mut self,
2494         tcx: TyCtxt<'tcx>,
2495         def_id: LocalDefId,
2496         substs: SubstsRef<'tcx>,
2497         location: Location,
2498     ) -> ty::InstantiatedPredicates<'tcx> {
2499         if let Some(ref closure_region_requirements) = tcx.mir_borrowck(def_id).closure_requirements
2500         {
2501             let closure_constraints = QueryRegionConstraints {
2502                 outlives: closure_region_requirements.apply_requirements(
2503                     tcx,
2504                     def_id.to_def_id(),
2505                     substs,
2506                 ),
2507
2508                 // Presently, closures never propagate member
2509                 // constraints to their parents -- they are enforced
2510                 // locally.  This is largely a non-issue as member
2511                 // constraints only come from `-> impl Trait` and
2512                 // friends which don't appear (thus far...) in
2513                 // closures.
2514                 member_constraints: vec![],
2515             };
2516
2517             let bounds_mapping = closure_constraints
2518                 .outlives
2519                 .iter()
2520                 .enumerate()
2521                 .filter_map(|(idx, constraint)| {
2522                     let ty::OutlivesPredicate(k1, r2) =
2523                         constraint.no_bound_vars().unwrap_or_else(|| {
2524                             bug!("query_constraint {:?} contained bound vars", constraint,);
2525                         });
2526
2527                     match k1.unpack() {
2528                         GenericArgKind::Lifetime(r1) => {
2529                             // constraint is r1: r2
2530                             let r1_vid = self.borrowck_context.universal_regions.to_region_vid(r1);
2531                             let r2_vid = self.borrowck_context.universal_regions.to_region_vid(r2);
2532                             let outlives_requirements =
2533                                 &closure_region_requirements.outlives_requirements[idx];
2534                             Some((
2535                                 (r1_vid, r2_vid),
2536                                 (outlives_requirements.category, outlives_requirements.blame_span),
2537                             ))
2538                         }
2539                         GenericArgKind::Type(_) | GenericArgKind::Const(_) => None,
2540                     }
2541                 })
2542                 .collect();
2543
2544             let existing = self
2545                 .borrowck_context
2546                 .constraints
2547                 .closure_bounds_mapping
2548                 .insert(location, bounds_mapping);
2549             assert!(existing.is_none(), "Multiple closures at the same location.");
2550
2551             self.push_region_constraints(
2552                 location.to_locations(),
2553                 ConstraintCategory::ClosureBounds,
2554                 &closure_constraints,
2555             );
2556         }
2557
2558         tcx.predicates_of(def_id).instantiate(tcx, substs)
2559     }
2560
2561     #[instrument(skip(self, body), level = "debug")]
2562     fn typeck_mir(&mut self, body: &Body<'tcx>) {
2563         self.last_span = body.span;
2564         debug!(?body.span);
2565
2566         for (local, local_decl) in body.local_decls.iter_enumerated() {
2567             self.check_local(&body, local, local_decl);
2568         }
2569
2570         for (block, block_data) in body.basic_blocks().iter_enumerated() {
2571             let mut location = Location { block, statement_index: 0 };
2572             for stmt in &block_data.statements {
2573                 if !stmt.source_info.span.is_dummy() {
2574                     self.last_span = stmt.source_info.span;
2575                 }
2576                 self.check_stmt(body, stmt, location);
2577                 location.statement_index += 1;
2578             }
2579
2580             self.check_terminator(&body, block_data.terminator(), location);
2581             self.check_iscleanup(&body, block_data);
2582         }
2583     }
2584 }
2585
2586 trait NormalizeLocation: fmt::Debug + Copy {
2587     fn to_locations(self) -> Locations;
2588 }
2589
2590 impl NormalizeLocation for Locations {
2591     fn to_locations(self) -> Locations {
2592         self
2593     }
2594 }
2595
2596 impl NormalizeLocation for Location {
2597     fn to_locations(self) -> Locations {
2598         Locations::Single(self)
2599     }
2600 }
2601
2602 /// Runs `infcx.instantiate_opaque_types`. Unlike other `TypeOp`s,
2603 /// this is not canonicalized - it directly affects the main `InferCtxt`
2604 /// that we use during MIR borrowchecking.
2605 #[derive(Debug)]
2606 pub(super) struct InstantiateOpaqueType<'tcx> {
2607     pub base_universe: Option<ty::UniverseIndex>,
2608     pub region_constraints: Option<RegionConstraintData<'tcx>>,
2609     pub obligations: Vec<PredicateObligation<'tcx>>,
2610 }
2611
2612 impl<'tcx> TypeOp<'tcx> for InstantiateOpaqueType<'tcx> {
2613     type Output = ();
2614     /// We use this type itself to store the information used
2615     /// when reporting errors. Since this is not a query, we don't
2616     /// re-run anything during error reporting - we just use the information
2617     /// we saved to help extract an error from the already-existing region
2618     /// constraints in our `InferCtxt`
2619     type ErrorInfo = InstantiateOpaqueType<'tcx>;
2620
2621     fn fully_perform(mut self, infcx: &InferCtxt<'_, 'tcx>) -> Fallible<TypeOpOutput<'tcx, Self>> {
2622         let (mut output, region_constraints) = scrape_region_constraints(infcx, || {
2623             Ok(InferOk { value: (), obligations: self.obligations.clone() })
2624         })?;
2625         self.region_constraints = Some(region_constraints);
2626         output.error_info = Some(self);
2627         Ok(output)
2628     }
2629 }