]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_borrowck/src/type_check/mod.rs
c4a190b44cbd4816c10bb732e84ab62f4b5a5680
[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 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 crate struct MirTypeckResults<'tcx> {
901     crate constraints: MirTypeckRegionConstraints<'tcx>,
902     crate universal_region_relations: Frozen<UniversalRegionRelations<'tcx>>,
903     crate opaque_type_values: VecMap<OpaqueTypeKey<'tcx>, (OpaqueHiddenType<'tcx>, OpaqueTyOrigin)>,
904 }
905
906 /// A collection of region constraints that must be satisfied for the
907 /// program to be considered well-typed.
908 crate struct MirTypeckRegionConstraints<'tcx> {
909     /// Maps from a `ty::Placeholder` to the corresponding
910     /// `PlaceholderIndex` bit that we will use for it.
911     ///
912     /// To keep everything in sync, do not insert this set
913     /// directly. Instead, use the `placeholder_region` helper.
914     crate placeholder_indices: PlaceholderIndices,
915
916     /// Each time we add a placeholder to `placeholder_indices`, we
917     /// also create a corresponding "representative" region vid for
918     /// that wraps it. This vector tracks those. This way, when we
919     /// convert the same `ty::RePlaceholder(p)` twice, we can map to
920     /// the same underlying `RegionVid`.
921     crate placeholder_index_to_region: IndexVec<PlaceholderIndex, ty::Region<'tcx>>,
922
923     /// In general, the type-checker is not responsible for enforcing
924     /// liveness constraints; this job falls to the region inferencer,
925     /// which performs a liveness analysis. However, in some limited
926     /// cases, the MIR type-checker creates temporary regions that do
927     /// not otherwise appear in the MIR -- in particular, the
928     /// late-bound regions that it instantiates at call-sites -- and
929     /// hence it must report on their liveness constraints.
930     crate liveness_constraints: LivenessValues<RegionVid>,
931
932     crate outlives_constraints: OutlivesConstraintSet<'tcx>,
933
934     crate member_constraints: MemberConstraintSet<'tcx, RegionVid>,
935
936     crate closure_bounds_mapping:
937         FxHashMap<Location, FxHashMap<(RegionVid, RegionVid), (ConstraintCategory, Span)>>,
938
939     crate universe_causes: FxHashMap<ty::UniverseIndex, UniverseInfo<'tcx>>,
940
941     crate type_tests: Vec<TypeTest<'tcx>>,
942 }
943
944 impl<'tcx> MirTypeckRegionConstraints<'tcx> {
945     fn placeholder_region(
946         &mut self,
947         infcx: &InferCtxt<'_, 'tcx>,
948         placeholder: ty::PlaceholderRegion,
949     ) -> ty::Region<'tcx> {
950         let placeholder_index = self.placeholder_indices.insert(placeholder);
951         match self.placeholder_index_to_region.get(placeholder_index) {
952             Some(&v) => v,
953             None => {
954                 let origin = NllRegionVariableOrigin::Placeholder(placeholder);
955                 let region = infcx.next_nll_region_var_in_universe(origin, placeholder.universe);
956                 self.placeholder_index_to_region.push(region);
957                 region
958             }
959         }
960     }
961 }
962
963 /// The `Locations` type summarizes *where* region constraints are
964 /// required to hold. Normally, this is at a particular point which
965 /// created the obligation, but for constraints that the user gave, we
966 /// want the constraint to hold at all points.
967 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
968 pub enum Locations {
969     /// Indicates that a type constraint should always be true. This
970     /// is particularly important in the new borrowck analysis for
971     /// things like the type of the return slot. Consider this
972     /// example:
973     ///
974     /// ```
975     /// fn foo<'a>(x: &'a u32) -> &'a u32 {
976     ///     let y = 22;
977     ///     return &y; // error
978     /// }
979     /// ```
980     ///
981     /// Here, we wind up with the signature from the return type being
982     /// something like `&'1 u32` where `'1` is a universal region. But
983     /// the type of the return slot `_0` is something like `&'2 u32`
984     /// where `'2` is an existential region variable. The type checker
985     /// requires that `&'2 u32 = &'1 u32` -- but at what point? In the
986     /// older NLL analysis, we required this only at the entry point
987     /// to the function. By the nature of the constraints, this wound
988     /// up propagating to all points reachable from start (because
989     /// `'1` -- as a universal region -- is live everywhere). In the
990     /// newer analysis, though, this doesn't work: `_0` is considered
991     /// dead at the start (it has no usable value) and hence this type
992     /// equality is basically a no-op. Then, later on, when we do `_0
993     /// = &'3 y`, that region `'3` never winds up related to the
994     /// universal region `'1` and hence no error occurs. Therefore, we
995     /// use Locations::All instead, which ensures that the `'1` and
996     /// `'2` are equal everything. We also use this for other
997     /// user-given type annotations; e.g., if the user wrote `let mut
998     /// x: &'static u32 = ...`, we would ensure that all values
999     /// assigned to `x` are of `'static` lifetime.
1000     ///
1001     /// The span points to the place the constraint arose. For example,
1002     /// it points to the type in a user-given type annotation. If
1003     /// there's no sensible span then it's DUMMY_SP.
1004     All(Span),
1005
1006     /// An outlives constraint that only has to hold at a single location,
1007     /// usually it represents a point where references flow from one spot to
1008     /// another (e.g., `x = y`)
1009     Single(Location),
1010 }
1011
1012 impl Locations {
1013     pub fn from_location(&self) -> Option<Location> {
1014         match self {
1015             Locations::All(_) => None,
1016             Locations::Single(from_location) => Some(*from_location),
1017         }
1018     }
1019
1020     /// Gets a span representing the location.
1021     pub fn span(&self, body: &Body<'_>) -> Span {
1022         match self {
1023             Locations::All(span) => *span,
1024             Locations::Single(l) => body.source_info(*l).span,
1025         }
1026     }
1027 }
1028
1029 impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
1030     fn new(
1031         infcx: &'a InferCtxt<'a, 'tcx>,
1032         body: &'a Body<'tcx>,
1033         param_env: ty::ParamEnv<'tcx>,
1034         region_bound_pairs: &'a RegionBoundPairs<'tcx>,
1035         implicit_region_bound: ty::Region<'tcx>,
1036         borrowck_context: &'a mut BorrowCheckContext<'a, 'tcx>,
1037     ) -> Self {
1038         let mut checker = Self {
1039             infcx,
1040             last_span: DUMMY_SP,
1041             body,
1042             user_type_annotations: &body.user_type_annotations,
1043             param_env,
1044             region_bound_pairs,
1045             implicit_region_bound,
1046             borrowck_context,
1047             reported_errors: Default::default(),
1048         };
1049         checker.check_user_type_annotations();
1050         checker
1051     }
1052
1053     fn body(&self) -> &Body<'tcx> {
1054         self.body
1055     }
1056
1057     fn unsized_feature_enabled(&self) -> bool {
1058         let features = self.tcx().features();
1059         features.unsized_locals || features.unsized_fn_params
1060     }
1061
1062     /// Equate the inferred type and the annotated type for user type annotations
1063     #[instrument(skip(self), level = "debug")]
1064     fn check_user_type_annotations(&mut self) {
1065         debug!(?self.user_type_annotations);
1066         for user_annotation in self.user_type_annotations {
1067             let CanonicalUserTypeAnnotation { span, ref user_ty, inferred_ty } = *user_annotation;
1068             let inferred_ty = self.normalize(inferred_ty, Locations::All(span));
1069             let annotation = self.instantiate_canonical_with_fresh_inference_vars(span, user_ty);
1070             match annotation {
1071                 UserType::Ty(mut ty) => {
1072                     ty = self.normalize(ty, Locations::All(span));
1073
1074                     if let Err(terr) = self.eq_types(
1075                         ty,
1076                         inferred_ty,
1077                         Locations::All(span),
1078                         ConstraintCategory::BoringNoLocation,
1079                     ) {
1080                         span_mirbug!(
1081                             self,
1082                             user_annotation,
1083                             "bad user type ({:?} = {:?}): {:?}",
1084                             ty,
1085                             inferred_ty,
1086                             terr
1087                         );
1088                     }
1089
1090                     self.prove_predicate(
1091                         ty::Binder::dummy(ty::PredicateKind::WellFormed(inferred_ty.into()))
1092                             .to_predicate(self.tcx()),
1093                         Locations::All(span),
1094                         ConstraintCategory::TypeAnnotation,
1095                     );
1096                 }
1097                 UserType::TypeOf(def_id, user_substs) => {
1098                     if let Err(terr) = self.fully_perform_op(
1099                         Locations::All(span),
1100                         ConstraintCategory::BoringNoLocation,
1101                         self.param_env.and(type_op::ascribe_user_type::AscribeUserType::new(
1102                             inferred_ty,
1103                             def_id,
1104                             user_substs,
1105                         )),
1106                     ) {
1107                         span_mirbug!(
1108                             self,
1109                             user_annotation,
1110                             "bad user type AscribeUserType({:?}, {:?} {:?}, type_of={:?}): {:?}",
1111                             inferred_ty,
1112                             def_id,
1113                             user_substs,
1114                             self.tcx().type_of(def_id),
1115                             terr,
1116                         );
1117                     }
1118                 }
1119             }
1120         }
1121     }
1122
1123     #[instrument(skip(self, data), level = "debug")]
1124     fn push_region_constraints(
1125         &mut self,
1126         locations: Locations,
1127         category: ConstraintCategory,
1128         data: &QueryRegionConstraints<'tcx>,
1129     ) {
1130         debug!("constraints generated: {:#?}", data);
1131
1132         constraint_conversion::ConstraintConversion::new(
1133             self.infcx,
1134             self.borrowck_context.universal_regions,
1135             self.region_bound_pairs,
1136             Some(self.implicit_region_bound),
1137             self.param_env,
1138             locations,
1139             locations.span(self.body),
1140             category,
1141             &mut self.borrowck_context.constraints,
1142         )
1143         .convert_all(data);
1144     }
1145
1146     /// Try to relate `sub <: sup`
1147     fn sub_types(
1148         &mut self,
1149         sub: Ty<'tcx>,
1150         sup: Ty<'tcx>,
1151         locations: Locations,
1152         category: ConstraintCategory,
1153     ) -> Fallible<()> {
1154         // Use this order of parameters because the sup type is usually the
1155         // "expected" type in diagnostics.
1156         self.relate_types(sup, ty::Variance::Contravariant, sub, locations, category)
1157     }
1158
1159     #[instrument(skip(self, category), level = "debug")]
1160     fn eq_types(
1161         &mut self,
1162         expected: Ty<'tcx>,
1163         found: Ty<'tcx>,
1164         locations: Locations,
1165         category: ConstraintCategory,
1166     ) -> Fallible<()> {
1167         self.relate_types(expected, ty::Variance::Invariant, found, locations, category)
1168     }
1169
1170     #[instrument(skip(self), level = "debug")]
1171     fn relate_type_and_user_type(
1172         &mut self,
1173         a: Ty<'tcx>,
1174         v: ty::Variance,
1175         user_ty: &UserTypeProjection,
1176         locations: Locations,
1177         category: ConstraintCategory,
1178     ) -> Fallible<()> {
1179         let annotated_type = self.user_type_annotations[user_ty.base].inferred_ty;
1180         let mut curr_projected_ty = PlaceTy::from_ty(annotated_type);
1181
1182         let tcx = self.infcx.tcx;
1183
1184         for proj in &user_ty.projs {
1185             let projected_ty = curr_projected_ty.projection_ty_core(
1186                 tcx,
1187                 self.param_env,
1188                 proj,
1189                 |this, field, ()| {
1190                     let ty = this.field_ty(tcx, field);
1191                     self.normalize(ty, locations)
1192                 },
1193             );
1194             curr_projected_ty = projected_ty;
1195         }
1196         debug!(
1197             "user_ty base: {:?} freshened: {:?} projs: {:?} yields: {:?}",
1198             user_ty.base, annotated_type, user_ty.projs, curr_projected_ty
1199         );
1200
1201         let ty = curr_projected_ty.ty;
1202         self.relate_types(ty, v.xform(ty::Variance::Contravariant), a, locations, category)?;
1203
1204         Ok(())
1205     }
1206
1207     fn tcx(&self) -> TyCtxt<'tcx> {
1208         self.infcx.tcx
1209     }
1210
1211     #[instrument(skip(self, body, location), level = "debug")]
1212     fn check_stmt(&mut self, body: &Body<'tcx>, stmt: &Statement<'tcx>, location: Location) {
1213         let tcx = self.tcx();
1214         match stmt.kind {
1215             StatementKind::Assign(box (ref place, ref rv)) => {
1216                 // Assignments to temporaries are not "interesting";
1217                 // they are not caused by the user, but rather artifacts
1218                 // of lowering. Assignments to other sorts of places *are* interesting
1219                 // though.
1220                 let category = match place.as_local() {
1221                     Some(RETURN_PLACE) => {
1222                         let defining_ty = &self.borrowck_context.universal_regions.defining_ty;
1223                         if defining_ty.is_const() {
1224                             if tcx.is_static(defining_ty.def_id()) {
1225                                 ConstraintCategory::UseAsStatic
1226                             } else {
1227                                 ConstraintCategory::UseAsConst
1228                             }
1229                         } else {
1230                             ConstraintCategory::Return(ReturnConstraint::Normal)
1231                         }
1232                     }
1233                     Some(l)
1234                         if matches!(
1235                             body.local_decls[l].local_info,
1236                             Some(box LocalInfo::AggregateTemp)
1237                         ) =>
1238                     {
1239                         ConstraintCategory::Usage
1240                     }
1241                     Some(l) if !body.local_decls[l].is_user_variable() => {
1242                         ConstraintCategory::Boring
1243                     }
1244                     _ => ConstraintCategory::Assignment,
1245                 };
1246                 debug!(
1247                     "assignment category: {:?} {:?}",
1248                     category,
1249                     place.as_local().map(|l| &body.local_decls[l])
1250                 );
1251
1252                 let place_ty = place.ty(body, tcx).ty;
1253                 let place_ty = self.normalize(place_ty, location);
1254                 let rv_ty = rv.ty(body, tcx);
1255                 let rv_ty = self.normalize(rv_ty, location);
1256                 if let Err(terr) =
1257                     self.sub_types(rv_ty, place_ty, location.to_locations(), category)
1258                 {
1259                     span_mirbug!(
1260                         self,
1261                         stmt,
1262                         "bad assignment ({:?} = {:?}): {:?}",
1263                         place_ty,
1264                         rv_ty,
1265                         terr
1266                     );
1267                 }
1268
1269                 if let Some(annotation_index) = self.rvalue_user_ty(rv) {
1270                     if let Err(terr) = self.relate_type_and_user_type(
1271                         rv_ty,
1272                         ty::Variance::Invariant,
1273                         &UserTypeProjection { base: annotation_index, projs: vec![] },
1274                         location.to_locations(),
1275                         ConstraintCategory::Boring,
1276                     ) {
1277                         let annotation = &self.user_type_annotations[annotation_index];
1278                         span_mirbug!(
1279                             self,
1280                             stmt,
1281                             "bad user type on rvalue ({:?} = {:?}): {:?}",
1282                             annotation,
1283                             rv_ty,
1284                             terr
1285                         );
1286                     }
1287                 }
1288
1289                 self.check_rvalue(body, rv, location);
1290                 if !self.unsized_feature_enabled() {
1291                     let trait_ref = ty::TraitRef {
1292                         def_id: tcx.require_lang_item(LangItem::Sized, Some(self.last_span)),
1293                         substs: tcx.mk_substs_trait(place_ty, &[]),
1294                     };
1295                     self.prove_trait_ref(
1296                         trait_ref,
1297                         location.to_locations(),
1298                         ConstraintCategory::SizedBound,
1299                     );
1300                 }
1301             }
1302             StatementKind::AscribeUserType(box (ref place, ref projection), variance) => {
1303                 let place_ty = place.ty(body, tcx).ty;
1304                 if let Err(terr) = self.relate_type_and_user_type(
1305                     place_ty,
1306                     variance,
1307                     projection,
1308                     Locations::All(stmt.source_info.span),
1309                     ConstraintCategory::TypeAnnotation,
1310                 ) {
1311                     let annotation = &self.user_type_annotations[projection.base];
1312                     span_mirbug!(
1313                         self,
1314                         stmt,
1315                         "bad type assert ({:?} <: {:?} with projections {:?}): {:?}",
1316                         place_ty,
1317                         annotation,
1318                         projection.projs,
1319                         terr
1320                     );
1321                 }
1322             }
1323             StatementKind::CopyNonOverlapping(box rustc_middle::mir::CopyNonOverlapping {
1324                 ..
1325             }) => span_bug!(
1326                 stmt.source_info.span,
1327                 "Unexpected StatementKind::CopyNonOverlapping, should only appear after lowering_intrinsics",
1328             ),
1329             StatementKind::FakeRead(..)
1330             | StatementKind::StorageLive(..)
1331             | StatementKind::StorageDead(..)
1332             | StatementKind::Retag { .. }
1333             | StatementKind::Coverage(..)
1334             | StatementKind::Nop => {}
1335             StatementKind::Deinit(..) | StatementKind::SetDiscriminant { .. } => {
1336                 bug!("Statement not allowed in this MIR phase")
1337             }
1338         }
1339     }
1340
1341     #[instrument(skip(self, body, term_location), level = "debug")]
1342     fn check_terminator(
1343         &mut self,
1344         body: &Body<'tcx>,
1345         term: &Terminator<'tcx>,
1346         term_location: Location,
1347     ) {
1348         let tcx = self.tcx();
1349         match term.kind {
1350             TerminatorKind::Goto { .. }
1351             | TerminatorKind::Resume
1352             | TerminatorKind::Abort
1353             | TerminatorKind::Return
1354             | TerminatorKind::GeneratorDrop
1355             | TerminatorKind::Unreachable
1356             | TerminatorKind::Drop { .. }
1357             | TerminatorKind::FalseEdge { .. }
1358             | TerminatorKind::FalseUnwind { .. }
1359             | TerminatorKind::InlineAsm { .. } => {
1360                 // no checks needed for these
1361             }
1362
1363             TerminatorKind::DropAndReplace { ref place, ref value, target: _, unwind: _ } => {
1364                 let place_ty = place.ty(body, tcx).ty;
1365                 let rv_ty = value.ty(body, tcx);
1366
1367                 let locations = term_location.to_locations();
1368                 if let Err(terr) =
1369                     self.sub_types(rv_ty, place_ty, locations, ConstraintCategory::Assignment)
1370                 {
1371                     span_mirbug!(
1372                         self,
1373                         term,
1374                         "bad DropAndReplace ({:?} = {:?}): {:?}",
1375                         place_ty,
1376                         rv_ty,
1377                         terr
1378                     );
1379                 }
1380             }
1381             TerminatorKind::SwitchInt { ref discr, switch_ty, .. } => {
1382                 self.check_operand(discr, term_location);
1383
1384                 let discr_ty = discr.ty(body, tcx);
1385                 if let Err(terr) = self.sub_types(
1386                     discr_ty,
1387                     switch_ty,
1388                     term_location.to_locations(),
1389                     ConstraintCategory::Assignment,
1390                 ) {
1391                     span_mirbug!(
1392                         self,
1393                         term,
1394                         "bad SwitchInt ({:?} on {:?}): {:?}",
1395                         switch_ty,
1396                         discr_ty,
1397                         terr
1398                     );
1399                 }
1400                 if !switch_ty.is_integral() && !switch_ty.is_char() && !switch_ty.is_bool() {
1401                     span_mirbug!(self, term, "bad SwitchInt discr ty {:?}", switch_ty);
1402                 }
1403                 // FIXME: check the values
1404             }
1405             TerminatorKind::Call { ref func, ref args, ref destination, from_hir_call, .. } => {
1406                 self.check_operand(func, term_location);
1407                 for arg in args {
1408                     self.check_operand(arg, term_location);
1409                 }
1410
1411                 let func_ty = func.ty(body, tcx);
1412                 debug!("check_terminator: call, func_ty={:?}", func_ty);
1413                 let sig = match func_ty.kind() {
1414                     ty::FnDef(..) | ty::FnPtr(_) => func_ty.fn_sig(tcx),
1415                     _ => {
1416                         span_mirbug!(self, term, "call to non-function {:?}", func_ty);
1417                         return;
1418                     }
1419                 };
1420                 let (sig, map) = self.infcx.replace_bound_vars_with_fresh_vars(
1421                     term.source_info.span,
1422                     LateBoundRegionConversionTime::FnCall,
1423                     sig,
1424                 );
1425                 let sig = self.normalize(sig, term_location);
1426                 self.check_call_dest(body, term, &sig, destination, term_location);
1427
1428                 self.prove_predicates(
1429                     sig.inputs_and_output
1430                         .iter()
1431                         .map(|ty| ty::Binder::dummy(ty::PredicateKind::WellFormed(ty.into()))),
1432                     term_location.to_locations(),
1433                     ConstraintCategory::Boring,
1434                 );
1435
1436                 // The ordinary liveness rules will ensure that all
1437                 // regions in the type of the callee are live here. We
1438                 // then further constrain the late-bound regions that
1439                 // were instantiated at the call site to be live as
1440                 // well. The resulting is that all the input (and
1441                 // output) types in the signature must be live, since
1442                 // all the inputs that fed into it were live.
1443                 for &late_bound_region in map.values() {
1444                     let region_vid =
1445                         self.borrowck_context.universal_regions.to_region_vid(late_bound_region);
1446                     self.borrowck_context
1447                         .constraints
1448                         .liveness_constraints
1449                         .add_element(region_vid, term_location);
1450                 }
1451
1452                 self.check_call_inputs(body, term, &sig, args, term_location, from_hir_call);
1453             }
1454             TerminatorKind::Assert { ref cond, ref msg, .. } => {
1455                 self.check_operand(cond, term_location);
1456
1457                 let cond_ty = cond.ty(body, tcx);
1458                 if cond_ty != tcx.types.bool {
1459                     span_mirbug!(self, term, "bad Assert ({:?}, not bool", cond_ty);
1460                 }
1461
1462                 if let AssertKind::BoundsCheck { ref len, ref index } = *msg {
1463                     if len.ty(body, tcx) != tcx.types.usize {
1464                         span_mirbug!(self, len, "bounds-check length non-usize {:?}", len)
1465                     }
1466                     if index.ty(body, tcx) != tcx.types.usize {
1467                         span_mirbug!(self, index, "bounds-check index non-usize {:?}", index)
1468                     }
1469                 }
1470             }
1471             TerminatorKind::Yield { ref value, .. } => {
1472                 self.check_operand(value, term_location);
1473
1474                 let value_ty = value.ty(body, tcx);
1475                 match body.yield_ty() {
1476                     None => span_mirbug!(self, term, "yield in non-generator"),
1477                     Some(ty) => {
1478                         if let Err(terr) = self.sub_types(
1479                             value_ty,
1480                             ty,
1481                             term_location.to_locations(),
1482                             ConstraintCategory::Yield,
1483                         ) {
1484                             span_mirbug!(
1485                                 self,
1486                                 term,
1487                                 "type of yield value is {:?}, but the yield type is {:?}: {:?}",
1488                                 value_ty,
1489                                 ty,
1490                                 terr
1491                             );
1492                         }
1493                     }
1494                 }
1495             }
1496         }
1497     }
1498
1499     fn check_call_dest(
1500         &mut self,
1501         body: &Body<'tcx>,
1502         term: &Terminator<'tcx>,
1503         sig: &ty::FnSig<'tcx>,
1504         destination: &Option<(Place<'tcx>, BasicBlock)>,
1505         term_location: Location,
1506     ) {
1507         let tcx = self.tcx();
1508         match *destination {
1509             Some((ref dest, _target_block)) => {
1510                 let dest_ty = dest.ty(body, tcx).ty;
1511                 let dest_ty = self.normalize(dest_ty, term_location);
1512                 let category = match dest.as_local() {
1513                     Some(RETURN_PLACE) => {
1514                         if let BorrowCheckContext {
1515                             universal_regions:
1516                                 UniversalRegions {
1517                                     defining_ty:
1518                                         DefiningTy::Const(def_id, _)
1519                                         | DefiningTy::InlineConst(def_id, _),
1520                                     ..
1521                                 },
1522                             ..
1523                         } = self.borrowck_context
1524                         {
1525                             if tcx.is_static(*def_id) {
1526                                 ConstraintCategory::UseAsStatic
1527                             } else {
1528                                 ConstraintCategory::UseAsConst
1529                             }
1530                         } else {
1531                             ConstraintCategory::Return(ReturnConstraint::Normal)
1532                         }
1533                     }
1534                     Some(l) if !body.local_decls[l].is_user_variable() => {
1535                         ConstraintCategory::Boring
1536                     }
1537                     _ => ConstraintCategory::Assignment,
1538                 };
1539
1540                 let locations = term_location.to_locations();
1541
1542                 if let Err(terr) = self.sub_types(sig.output(), dest_ty, locations, category) {
1543                     span_mirbug!(
1544                         self,
1545                         term,
1546                         "call dest mismatch ({:?} <- {:?}): {:?}",
1547                         dest_ty,
1548                         sig.output(),
1549                         terr
1550                     );
1551                 }
1552
1553                 // When `unsized_fn_params` and `unsized_locals` are both not enabled,
1554                 // this check is done at `check_local`.
1555                 if self.unsized_feature_enabled() {
1556                     let span = term.source_info.span;
1557                     self.ensure_place_sized(dest_ty, span);
1558                 }
1559             }
1560             None => {
1561                 if !self
1562                     .tcx()
1563                     .conservative_is_privately_uninhabited(self.param_env.and(sig.output()))
1564                 {
1565                     span_mirbug!(self, term, "call to converging function {:?} w/o dest", sig);
1566                 }
1567             }
1568         }
1569     }
1570
1571     fn check_call_inputs(
1572         &mut self,
1573         body: &Body<'tcx>,
1574         term: &Terminator<'tcx>,
1575         sig: &ty::FnSig<'tcx>,
1576         args: &[Operand<'tcx>],
1577         term_location: Location,
1578         from_hir_call: bool,
1579     ) {
1580         debug!("check_call_inputs({:?}, {:?})", sig, args);
1581         if args.len() < sig.inputs().len() || (args.len() > sig.inputs().len() && !sig.c_variadic) {
1582             span_mirbug!(self, term, "call to {:?} with wrong # of args", sig);
1583         }
1584         for (n, (fn_arg, op_arg)) in iter::zip(sig.inputs(), args).enumerate() {
1585             let op_arg_ty = op_arg.ty(body, self.tcx());
1586             let op_arg_ty = self.normalize(op_arg_ty, term_location);
1587             let category = if from_hir_call {
1588                 ConstraintCategory::CallArgument
1589             } else {
1590                 ConstraintCategory::Boring
1591             };
1592             if let Err(terr) =
1593                 self.sub_types(op_arg_ty, *fn_arg, term_location.to_locations(), category)
1594             {
1595                 span_mirbug!(
1596                     self,
1597                     term,
1598                     "bad arg #{:?} ({:?} <- {:?}): {:?}",
1599                     n,
1600                     fn_arg,
1601                     op_arg_ty,
1602                     terr
1603                 );
1604             }
1605         }
1606     }
1607
1608     fn check_iscleanup(&mut self, body: &Body<'tcx>, block_data: &BasicBlockData<'tcx>) {
1609         let is_cleanup = block_data.is_cleanup;
1610         self.last_span = block_data.terminator().source_info.span;
1611         match block_data.terminator().kind {
1612             TerminatorKind::Goto { target } => {
1613                 self.assert_iscleanup(body, block_data, target, is_cleanup)
1614             }
1615             TerminatorKind::SwitchInt { ref targets, .. } => {
1616                 for target in targets.all_targets() {
1617                     self.assert_iscleanup(body, block_data, *target, is_cleanup);
1618                 }
1619             }
1620             TerminatorKind::Resume => {
1621                 if !is_cleanup {
1622                     span_mirbug!(self, block_data, "resume on non-cleanup block!")
1623                 }
1624             }
1625             TerminatorKind::Abort => {
1626                 if !is_cleanup {
1627                     span_mirbug!(self, block_data, "abort on non-cleanup block!")
1628                 }
1629             }
1630             TerminatorKind::Return => {
1631                 if is_cleanup {
1632                     span_mirbug!(self, block_data, "return on cleanup block")
1633                 }
1634             }
1635             TerminatorKind::GeneratorDrop { .. } => {
1636                 if is_cleanup {
1637                     span_mirbug!(self, block_data, "generator_drop in cleanup block")
1638                 }
1639             }
1640             TerminatorKind::Yield { resume, drop, .. } => {
1641                 if is_cleanup {
1642                     span_mirbug!(self, block_data, "yield in cleanup block")
1643                 }
1644                 self.assert_iscleanup(body, block_data, resume, is_cleanup);
1645                 if let Some(drop) = drop {
1646                     self.assert_iscleanup(body, block_data, drop, is_cleanup);
1647                 }
1648             }
1649             TerminatorKind::Unreachable => {}
1650             TerminatorKind::Drop { target, unwind, .. }
1651             | TerminatorKind::DropAndReplace { target, unwind, .. }
1652             | TerminatorKind::Assert { target, cleanup: unwind, .. } => {
1653                 self.assert_iscleanup(body, block_data, target, is_cleanup);
1654                 if let Some(unwind) = unwind {
1655                     if is_cleanup {
1656                         span_mirbug!(self, block_data, "unwind on cleanup block")
1657                     }
1658                     self.assert_iscleanup(body, block_data, unwind, true);
1659                 }
1660             }
1661             TerminatorKind::Call { ref destination, cleanup, .. } => {
1662                 if let &Some((_, target)) = destination {
1663                     self.assert_iscleanup(body, block_data, target, is_cleanup);
1664                 }
1665                 if let Some(cleanup) = cleanup {
1666                     if is_cleanup {
1667                         span_mirbug!(self, block_data, "cleanup on cleanup block")
1668                     }
1669                     self.assert_iscleanup(body, block_data, cleanup, true);
1670                 }
1671             }
1672             TerminatorKind::FalseEdge { real_target, imaginary_target } => {
1673                 self.assert_iscleanup(body, block_data, real_target, is_cleanup);
1674                 self.assert_iscleanup(body, block_data, imaginary_target, is_cleanup);
1675             }
1676             TerminatorKind::FalseUnwind { real_target, unwind } => {
1677                 self.assert_iscleanup(body, block_data, real_target, is_cleanup);
1678                 if let Some(unwind) = unwind {
1679                     if is_cleanup {
1680                         span_mirbug!(self, block_data, "cleanup in cleanup block via false unwind");
1681                     }
1682                     self.assert_iscleanup(body, block_data, unwind, true);
1683                 }
1684             }
1685             TerminatorKind::InlineAsm { destination, cleanup, .. } => {
1686                 if let Some(target) = destination {
1687                     self.assert_iscleanup(body, block_data, target, is_cleanup);
1688                 }
1689                 if let Some(cleanup) = cleanup {
1690                     if is_cleanup {
1691                         span_mirbug!(self, block_data, "cleanup on cleanup block")
1692                     }
1693                     self.assert_iscleanup(body, block_data, cleanup, true);
1694                 }
1695             }
1696         }
1697     }
1698
1699     fn assert_iscleanup(
1700         &mut self,
1701         body: &Body<'tcx>,
1702         ctxt: &dyn fmt::Debug,
1703         bb: BasicBlock,
1704         iscleanuppad: bool,
1705     ) {
1706         if body[bb].is_cleanup != iscleanuppad {
1707             span_mirbug!(self, ctxt, "cleanuppad mismatch: {:?} should be {:?}", bb, iscleanuppad);
1708         }
1709     }
1710
1711     fn check_local(&mut self, body: &Body<'tcx>, local: Local, local_decl: &LocalDecl<'tcx>) {
1712         match body.local_kind(local) {
1713             LocalKind::ReturnPointer | LocalKind::Arg => {
1714                 // return values of normal functions are required to be
1715                 // sized by typeck, but return values of ADT constructors are
1716                 // not because we don't include a `Self: Sized` bounds on them.
1717                 //
1718                 // Unbound parts of arguments were never required to be Sized
1719                 // - maybe we should make that a warning.
1720                 return;
1721             }
1722             LocalKind::Var | LocalKind::Temp => {}
1723         }
1724
1725         // When `unsized_fn_params` or `unsized_locals` is enabled, only function calls
1726         // and nullary ops are checked in `check_call_dest`.
1727         if !self.unsized_feature_enabled() {
1728             let span = local_decl.source_info.span;
1729             let ty = local_decl.ty;
1730             self.ensure_place_sized(ty, span);
1731         }
1732     }
1733
1734     fn ensure_place_sized(&mut self, ty: Ty<'tcx>, span: Span) {
1735         let tcx = self.tcx();
1736
1737         // Erase the regions from `ty` to get a global type.  The
1738         // `Sized` bound in no way depends on precise regions, so this
1739         // shouldn't affect `is_sized`.
1740         let erased_ty = tcx.erase_regions(ty);
1741         if !erased_ty.is_sized(tcx.at(span), self.param_env) {
1742             // in current MIR construction, all non-control-flow rvalue
1743             // expressions evaluate through `as_temp` or `into` a return
1744             // slot or local, so to find all unsized rvalues it is enough
1745             // to check all temps, return slots and locals.
1746             if self.reported_errors.replace((ty, span)).is_none() {
1747                 let mut diag = struct_span_err!(
1748                     self.tcx().sess,
1749                     span,
1750                     E0161,
1751                     "cannot move a value of type {0}: the size of {0} \
1752                      cannot be statically determined",
1753                     ty
1754                 );
1755
1756                 // While this is located in `nll::typeck` this error is not
1757                 // an NLL error, it's a required check to prevent creation
1758                 // of unsized rvalues in a call expression.
1759                 diag.emit();
1760             }
1761         }
1762     }
1763
1764     fn aggregate_field_ty(
1765         &mut self,
1766         ak: &AggregateKind<'tcx>,
1767         field_index: usize,
1768         location: Location,
1769     ) -> Result<Ty<'tcx>, FieldAccessError> {
1770         let tcx = self.tcx();
1771
1772         match *ak {
1773             AggregateKind::Adt(adt_did, variant_index, substs, _, active_field_index) => {
1774                 let def = tcx.adt_def(adt_did);
1775                 let variant = &def.variant(variant_index);
1776                 let adj_field_index = active_field_index.unwrap_or(field_index);
1777                 if let Some(field) = variant.fields.get(adj_field_index) {
1778                     Ok(self.normalize(field.ty(tcx, substs), location))
1779                 } else {
1780                     Err(FieldAccessError::OutOfRange { field_count: variant.fields.len() })
1781                 }
1782             }
1783             AggregateKind::Closure(_, substs) => {
1784                 match substs.as_closure().upvar_tys().nth(field_index) {
1785                     Some(ty) => Ok(ty),
1786                     None => Err(FieldAccessError::OutOfRange {
1787                         field_count: substs.as_closure().upvar_tys().count(),
1788                     }),
1789                 }
1790             }
1791             AggregateKind::Generator(_, substs, _) => {
1792                 // It doesn't make sense to look at a field beyond the prefix;
1793                 // these require a variant index, and are not initialized in
1794                 // aggregate rvalues.
1795                 match substs.as_generator().prefix_tys().nth(field_index) {
1796                     Some(ty) => Ok(ty),
1797                     None => Err(FieldAccessError::OutOfRange {
1798                         field_count: substs.as_generator().prefix_tys().count(),
1799                     }),
1800                 }
1801             }
1802             AggregateKind::Array(ty) => Ok(ty),
1803             AggregateKind::Tuple => {
1804                 unreachable!("This should have been covered in check_rvalues");
1805             }
1806         }
1807     }
1808
1809     fn check_operand(&mut self, op: &Operand<'tcx>, location: Location) {
1810         if let Operand::Constant(constant) = op {
1811             let maybe_uneval = match constant.literal {
1812                 ConstantKind::Ty(ct) => match ct.val() {
1813                     ty::ConstKind::Unevaluated(uv) => Some(uv),
1814                     _ => None,
1815                 },
1816                 _ => None,
1817             };
1818             if let Some(uv) = maybe_uneval {
1819                 if uv.promoted.is_none() {
1820                     let tcx = self.tcx();
1821                     let def_id = uv.def.def_id_for_type_of();
1822                     if tcx.def_kind(def_id) == DefKind::InlineConst {
1823                         let predicates = self.prove_closure_bounds(
1824                             tcx,
1825                             def_id.expect_local(),
1826                             uv.substs,
1827                             location,
1828                         );
1829                         self.normalize_and_prove_instantiated_predicates(
1830                             def_id,
1831                             predicates,
1832                             location.to_locations(),
1833                         );
1834                     }
1835                 }
1836             }
1837         }
1838     }
1839
1840     fn check_rvalue(&mut self, body: &Body<'tcx>, rvalue: &Rvalue<'tcx>, location: Location) {
1841         let tcx = self.tcx();
1842
1843         match rvalue {
1844             Rvalue::Aggregate(ak, ops) => {
1845                 for op in ops {
1846                     self.check_operand(op, location);
1847                 }
1848                 self.check_aggregate_rvalue(&body, rvalue, ak, ops, location)
1849             }
1850
1851             Rvalue::Repeat(operand, len) => {
1852                 self.check_operand(operand, location);
1853
1854                 // If the length cannot be evaluated we must assume that the length can be larger
1855                 // than 1.
1856                 // If the length is larger than 1, the repeat expression will need to copy the
1857                 // element, so we require the `Copy` trait.
1858                 if len.try_eval_usize(tcx, self.param_env).map_or(true, |len| len > 1) {
1859                     match operand {
1860                         Operand::Copy(..) | Operand::Constant(..) => {
1861                             // These are always okay: direct use of a const, or a value that can evidently be copied.
1862                         }
1863                         Operand::Move(place) => {
1864                             // Make sure that repeated elements implement `Copy`.
1865                             let span = body.source_info(location).span;
1866                             let ty = place.ty(body, tcx).ty;
1867                             let trait_ref = ty::TraitRef::new(
1868                                 tcx.require_lang_item(LangItem::Copy, Some(span)),
1869                                 tcx.mk_substs_trait(ty, &[]),
1870                             );
1871
1872                             self.prove_trait_ref(
1873                                 trait_ref,
1874                                 Locations::Single(location),
1875                                 ConstraintCategory::CopyBound,
1876                             );
1877                         }
1878                     }
1879                 }
1880             }
1881
1882             &Rvalue::NullaryOp(_, ty) => {
1883                 let trait_ref = ty::TraitRef {
1884                     def_id: tcx.require_lang_item(LangItem::Sized, Some(self.last_span)),
1885                     substs: tcx.mk_substs_trait(ty, &[]),
1886                 };
1887
1888                 self.prove_trait_ref(
1889                     trait_ref,
1890                     location.to_locations(),
1891                     ConstraintCategory::SizedBound,
1892                 );
1893             }
1894
1895             Rvalue::ShallowInitBox(operand, ty) => {
1896                 self.check_operand(operand, location);
1897
1898                 let trait_ref = ty::TraitRef {
1899                     def_id: tcx.require_lang_item(LangItem::Sized, Some(self.last_span)),
1900                     substs: tcx.mk_substs_trait(*ty, &[]),
1901                 };
1902
1903                 self.prove_trait_ref(
1904                     trait_ref,
1905                     location.to_locations(),
1906                     ConstraintCategory::SizedBound,
1907                 );
1908             }
1909
1910             Rvalue::Cast(cast_kind, op, ty) => {
1911                 self.check_operand(op, location);
1912
1913                 match cast_kind {
1914                     CastKind::Pointer(PointerCast::ReifyFnPointer) => {
1915                         let fn_sig = op.ty(body, tcx).fn_sig(tcx);
1916
1917                         // The type that we see in the fcx is like
1918                         // `foo::<'a, 'b>`, where `foo` is the path to a
1919                         // function definition. When we extract the
1920                         // signature, it comes from the `fn_sig` query,
1921                         // and hence may contain unnormalized results.
1922                         let fn_sig = self.normalize(fn_sig, location);
1923
1924                         let ty_fn_ptr_from = tcx.mk_fn_ptr(fn_sig);
1925
1926                         if let Err(terr) = self.eq_types(
1927                             *ty,
1928                             ty_fn_ptr_from,
1929                             location.to_locations(),
1930                             ConstraintCategory::Cast,
1931                         ) {
1932                             span_mirbug!(
1933                                 self,
1934                                 rvalue,
1935                                 "equating {:?} with {:?} yields {:?}",
1936                                 ty_fn_ptr_from,
1937                                 ty,
1938                                 terr
1939                             );
1940                         }
1941                     }
1942
1943                     CastKind::Pointer(PointerCast::ClosureFnPointer(unsafety)) => {
1944                         let sig = match op.ty(body, tcx).kind() {
1945                             ty::Closure(_, substs) => substs.as_closure().sig(),
1946                             _ => bug!(),
1947                         };
1948                         let ty_fn_ptr_from = tcx.mk_fn_ptr(tcx.signature_unclosure(sig, *unsafety));
1949
1950                         if let Err(terr) = self.eq_types(
1951                             *ty,
1952                             ty_fn_ptr_from,
1953                             location.to_locations(),
1954                             ConstraintCategory::Cast,
1955                         ) {
1956                             span_mirbug!(
1957                                 self,
1958                                 rvalue,
1959                                 "equating {:?} with {:?} yields {:?}",
1960                                 ty_fn_ptr_from,
1961                                 ty,
1962                                 terr
1963                             );
1964                         }
1965                     }
1966
1967                     CastKind::Pointer(PointerCast::UnsafeFnPointer) => {
1968                         let fn_sig = op.ty(body, tcx).fn_sig(tcx);
1969
1970                         // The type that we see in the fcx is like
1971                         // `foo::<'a, 'b>`, where `foo` is the path to a
1972                         // function definition. When we extract the
1973                         // signature, it comes from the `fn_sig` query,
1974                         // and hence may contain unnormalized results.
1975                         let fn_sig = self.normalize(fn_sig, location);
1976
1977                         let ty_fn_ptr_from = tcx.safe_to_unsafe_fn_ty(fn_sig);
1978
1979                         if let Err(terr) = self.eq_types(
1980                             *ty,
1981                             ty_fn_ptr_from,
1982                             location.to_locations(),
1983                             ConstraintCategory::Cast,
1984                         ) {
1985                             span_mirbug!(
1986                                 self,
1987                                 rvalue,
1988                                 "equating {:?} with {:?} yields {:?}",
1989                                 ty_fn_ptr_from,
1990                                 ty,
1991                                 terr
1992                             );
1993                         }
1994                     }
1995
1996                     CastKind::Pointer(PointerCast::Unsize) => {
1997                         let &ty = ty;
1998                         let trait_ref = ty::TraitRef {
1999                             def_id: tcx
2000                                 .require_lang_item(LangItem::CoerceUnsized, Some(self.last_span)),
2001                             substs: tcx.mk_substs_trait(op.ty(body, tcx), &[ty.into()]),
2002                         };
2003
2004                         self.prove_trait_ref(
2005                             trait_ref,
2006                             location.to_locations(),
2007                             ConstraintCategory::Cast,
2008                         );
2009                     }
2010
2011                     CastKind::Pointer(PointerCast::MutToConstPointer) => {
2012                         let ty::RawPtr(ty::TypeAndMut {
2013                             ty: ty_from,
2014                             mutbl: hir::Mutability::Mut,
2015                         }) = op.ty(body, tcx).kind() else {
2016                             span_mirbug!(
2017                                 self,
2018                                 rvalue,
2019                                 "unexpected base type for cast {:?}",
2020                                 ty,
2021                             );
2022                             return;
2023                         };
2024                         let ty::RawPtr(ty::TypeAndMut {
2025                             ty: ty_to,
2026                             mutbl: hir::Mutability::Not,
2027                         }) = ty.kind() else {
2028                             span_mirbug!(
2029                                 self,
2030                                 rvalue,
2031                                 "unexpected target type for cast {:?}",
2032                                 ty,
2033                             );
2034                             return;
2035                         };
2036                         if let Err(terr) = self.sub_types(
2037                             *ty_from,
2038                             *ty_to,
2039                             location.to_locations(),
2040                             ConstraintCategory::Cast,
2041                         ) {
2042                             span_mirbug!(
2043                                 self,
2044                                 rvalue,
2045                                 "relating {:?} with {:?} yields {:?}",
2046                                 ty_from,
2047                                 ty_to,
2048                                 terr
2049                             );
2050                         }
2051                     }
2052
2053                     CastKind::Pointer(PointerCast::ArrayToPointer) => {
2054                         let ty_from = op.ty(body, tcx);
2055
2056                         let opt_ty_elem_mut = match ty_from.kind() {
2057                             ty::RawPtr(ty::TypeAndMut { mutbl: array_mut, ty: array_ty }) => {
2058                                 match array_ty.kind() {
2059                                     ty::Array(ty_elem, _) => Some((ty_elem, *array_mut)),
2060                                     _ => None,
2061                                 }
2062                             }
2063                             _ => None,
2064                         };
2065
2066                         let Some((ty_elem, ty_mut)) = opt_ty_elem_mut else {
2067                             span_mirbug!(
2068                                 self,
2069                                 rvalue,
2070                                 "ArrayToPointer cast from unexpected type {:?}",
2071                                 ty_from,
2072                             );
2073                             return;
2074                         };
2075
2076                         let (ty_to, ty_to_mut) = match ty.kind() {
2077                             ty::RawPtr(ty::TypeAndMut { mutbl: ty_to_mut, ty: ty_to }) => {
2078                                 (ty_to, *ty_to_mut)
2079                             }
2080                             _ => {
2081                                 span_mirbug!(
2082                                     self,
2083                                     rvalue,
2084                                     "ArrayToPointer cast to unexpected type {:?}",
2085                                     ty,
2086                                 );
2087                                 return;
2088                             }
2089                         };
2090
2091                         if ty_to_mut == Mutability::Mut && ty_mut == Mutability::Not {
2092                             span_mirbug!(
2093                                 self,
2094                                 rvalue,
2095                                 "ArrayToPointer cast from const {:?} to mut {:?}",
2096                                 ty,
2097                                 ty_to
2098                             );
2099                             return;
2100                         }
2101
2102                         if let Err(terr) = self.sub_types(
2103                             *ty_elem,
2104                             *ty_to,
2105                             location.to_locations(),
2106                             ConstraintCategory::Cast,
2107                         ) {
2108                             span_mirbug!(
2109                                 self,
2110                                 rvalue,
2111                                 "relating {:?} with {:?} yields {:?}",
2112                                 ty_elem,
2113                                 ty_to,
2114                                 terr
2115                             )
2116                         }
2117                     }
2118
2119                     CastKind::Misc => {
2120                         let ty_from = op.ty(body, tcx);
2121                         let cast_ty_from = CastTy::from_ty(ty_from);
2122                         let cast_ty_to = CastTy::from_ty(*ty);
2123                         match (cast_ty_from, cast_ty_to) {
2124                             (None, _)
2125                             | (_, None | Some(CastTy::FnPtr))
2126                             | (Some(CastTy::Float), Some(CastTy::Ptr(_)))
2127                             | (Some(CastTy::Ptr(_) | CastTy::FnPtr), Some(CastTy::Float)) => {
2128                                 span_mirbug!(self, rvalue, "Invalid cast {:?} -> {:?}", ty_from, ty,)
2129                             }
2130                             (
2131                                 Some(CastTy::Int(_)),
2132                                 Some(CastTy::Int(_) | CastTy::Float | CastTy::Ptr(_)),
2133                             )
2134                             | (Some(CastTy::Float), Some(CastTy::Int(_) | CastTy::Float))
2135                             | (Some(CastTy::Ptr(_)), Some(CastTy::Int(_) | CastTy::Ptr(_)))
2136                             | (Some(CastTy::FnPtr), Some(CastTy::Int(_) | CastTy::Ptr(_))) => (),
2137                         }
2138                     }
2139                 }
2140             }
2141
2142             Rvalue::Ref(region, _borrow_kind, borrowed_place) => {
2143                 self.add_reborrow_constraint(&body, location, *region, borrowed_place);
2144             }
2145
2146             Rvalue::BinaryOp(
2147                 BinOp::Eq | BinOp::Ne | BinOp::Lt | BinOp::Le | BinOp::Gt | BinOp::Ge,
2148                 box (left, right),
2149             ) => {
2150                 self.check_operand(left, location);
2151                 self.check_operand(right, location);
2152
2153                 let ty_left = left.ty(body, tcx);
2154                 match ty_left.kind() {
2155                     // Types with regions are comparable if they have a common super-type.
2156                     ty::RawPtr(_) | ty::FnPtr(_) => {
2157                         let ty_right = right.ty(body, tcx);
2158                         let common_ty = self.infcx.next_ty_var(TypeVariableOrigin {
2159                             kind: TypeVariableOriginKind::MiscVariable,
2160                             span: body.source_info(location).span,
2161                         });
2162                         self.sub_types(
2163                             ty_left,
2164                             common_ty,
2165                             location.to_locations(),
2166                             ConstraintCategory::Boring,
2167                         )
2168                         .unwrap_or_else(|err| {
2169                             bug!("Could not equate type variable with {:?}: {:?}", ty_left, err)
2170                         });
2171                         if let Err(terr) = self.sub_types(
2172                             ty_right,
2173                             common_ty,
2174                             location.to_locations(),
2175                             ConstraintCategory::Boring,
2176                         ) {
2177                             span_mirbug!(
2178                                 self,
2179                                 rvalue,
2180                                 "unexpected comparison types {:?} and {:?} yields {:?}",
2181                                 ty_left,
2182                                 ty_right,
2183                                 terr
2184                             )
2185                         }
2186                     }
2187                     // For types with no regions we can just check that the
2188                     // both operands have the same type.
2189                     ty::Int(_) | ty::Uint(_) | ty::Bool | ty::Char | ty::Float(_)
2190                         if ty_left == right.ty(body, tcx) => {}
2191                     // Other types are compared by trait methods, not by
2192                     // `Rvalue::BinaryOp`.
2193                     _ => span_mirbug!(
2194                         self,
2195                         rvalue,
2196                         "unexpected comparison types {:?} and {:?}",
2197                         ty_left,
2198                         right.ty(body, tcx)
2199                     ),
2200                 }
2201             }
2202
2203             Rvalue::Use(operand) | Rvalue::UnaryOp(_, operand) => {
2204                 self.check_operand(operand, location);
2205             }
2206
2207             Rvalue::BinaryOp(_, box (left, right))
2208             | Rvalue::CheckedBinaryOp(_, box (left, right)) => {
2209                 self.check_operand(left, location);
2210                 self.check_operand(right, location);
2211             }
2212
2213             Rvalue::AddressOf(..)
2214             | Rvalue::ThreadLocalRef(..)
2215             | Rvalue::Len(..)
2216             | Rvalue::Discriminant(..) => {}
2217         }
2218     }
2219
2220     /// If this rvalue supports a user-given type annotation, then
2221     /// extract and return it. This represents the final type of the
2222     /// rvalue and will be unified with the inferred type.
2223     fn rvalue_user_ty(&self, rvalue: &Rvalue<'tcx>) -> Option<UserTypeAnnotationIndex> {
2224         match rvalue {
2225             Rvalue::Use(_)
2226             | Rvalue::ThreadLocalRef(_)
2227             | Rvalue::Repeat(..)
2228             | Rvalue::Ref(..)
2229             | Rvalue::AddressOf(..)
2230             | Rvalue::Len(..)
2231             | Rvalue::Cast(..)
2232             | Rvalue::ShallowInitBox(..)
2233             | Rvalue::BinaryOp(..)
2234             | Rvalue::CheckedBinaryOp(..)
2235             | Rvalue::NullaryOp(..)
2236             | Rvalue::UnaryOp(..)
2237             | Rvalue::Discriminant(..) => None,
2238
2239             Rvalue::Aggregate(aggregate, _) => match **aggregate {
2240                 AggregateKind::Adt(_, _, _, user_ty, _) => user_ty,
2241                 AggregateKind::Array(_) => None,
2242                 AggregateKind::Tuple => None,
2243                 AggregateKind::Closure(_, _) => None,
2244                 AggregateKind::Generator(_, _, _) => None,
2245             },
2246         }
2247     }
2248
2249     fn check_aggregate_rvalue(
2250         &mut self,
2251         body: &Body<'tcx>,
2252         rvalue: &Rvalue<'tcx>,
2253         aggregate_kind: &AggregateKind<'tcx>,
2254         operands: &[Operand<'tcx>],
2255         location: Location,
2256     ) {
2257         let tcx = self.tcx();
2258
2259         self.prove_aggregate_predicates(aggregate_kind, location);
2260
2261         if *aggregate_kind == AggregateKind::Tuple {
2262             // tuple rvalue field type is always the type of the op. Nothing to check here.
2263             return;
2264         }
2265
2266         for (i, operand) in operands.iter().enumerate() {
2267             let field_ty = match self.aggregate_field_ty(aggregate_kind, i, location) {
2268                 Ok(field_ty) => field_ty,
2269                 Err(FieldAccessError::OutOfRange { field_count }) => {
2270                     span_mirbug!(
2271                         self,
2272                         rvalue,
2273                         "accessed field #{} but variant only has {}",
2274                         i,
2275                         field_count
2276                     );
2277                     continue;
2278                 }
2279             };
2280             let operand_ty = operand.ty(body, tcx);
2281             let operand_ty = self.normalize(operand_ty, location);
2282
2283             if let Err(terr) = self.sub_types(
2284                 operand_ty,
2285                 field_ty,
2286                 location.to_locations(),
2287                 ConstraintCategory::Boring,
2288             ) {
2289                 span_mirbug!(
2290                     self,
2291                     rvalue,
2292                     "{:?} is not a subtype of {:?}: {:?}",
2293                     operand_ty,
2294                     field_ty,
2295                     terr
2296                 );
2297             }
2298         }
2299     }
2300
2301     /// Adds the constraints that arise from a borrow expression `&'a P` at the location `L`.
2302     ///
2303     /// # Parameters
2304     ///
2305     /// - `location`: the location `L` where the borrow expression occurs
2306     /// - `borrow_region`: the region `'a` associated with the borrow
2307     /// - `borrowed_place`: the place `P` being borrowed
2308     fn add_reborrow_constraint(
2309         &mut self,
2310         body: &Body<'tcx>,
2311         location: Location,
2312         borrow_region: ty::Region<'tcx>,
2313         borrowed_place: &Place<'tcx>,
2314     ) {
2315         // These constraints are only meaningful during borrowck:
2316         let BorrowCheckContext { borrow_set, location_table, all_facts, constraints, .. } =
2317             self.borrowck_context;
2318
2319         // In Polonius mode, we also push a `loan_issued_at` fact
2320         // linking the loan to the region (in some cases, though,
2321         // there is no loan associated with this borrow expression --
2322         // that occurs when we are borrowing an unsafe place, for
2323         // example).
2324         if let Some(all_facts) = all_facts {
2325             let _prof_timer = self.infcx.tcx.prof.generic_activity("polonius_fact_generation");
2326             if let Some(borrow_index) = borrow_set.get_index_of(&location) {
2327                 let region_vid = borrow_region.to_region_vid();
2328                 all_facts.loan_issued_at.push((
2329                     region_vid,
2330                     borrow_index,
2331                     location_table.mid_index(location),
2332                 ));
2333             }
2334         }
2335
2336         // If we are reborrowing the referent of another reference, we
2337         // need to add outlives relationships. In a case like `&mut
2338         // *p`, where the `p` has type `&'b mut Foo`, for example, we
2339         // need to ensure that `'b: 'a`.
2340
2341         debug!(
2342             "add_reborrow_constraint({:?}, {:?}, {:?})",
2343             location, borrow_region, borrowed_place
2344         );
2345
2346         let mut cursor = borrowed_place.projection.as_ref();
2347         let tcx = self.infcx.tcx;
2348         let field = path_utils::is_upvar_field_projection(
2349             tcx,
2350             &self.borrowck_context.upvars,
2351             borrowed_place.as_ref(),
2352             body,
2353         );
2354         let category = if let Some(field) = field {
2355             ConstraintCategory::ClosureUpvar(field)
2356         } else {
2357             ConstraintCategory::Boring
2358         };
2359
2360         while let [proj_base @ .., elem] = cursor {
2361             cursor = proj_base;
2362
2363             debug!("add_reborrow_constraint - iteration {:?}", elem);
2364
2365             match elem {
2366                 ProjectionElem::Deref => {
2367                     let base_ty = Place::ty_from(borrowed_place.local, proj_base, body, tcx).ty;
2368
2369                     debug!("add_reborrow_constraint - base_ty = {:?}", base_ty);
2370                     match base_ty.kind() {
2371                         ty::Ref(ref_region, _, mutbl) => {
2372                             constraints.outlives_constraints.push(OutlivesConstraint {
2373                                 sup: ref_region.to_region_vid(),
2374                                 sub: borrow_region.to_region_vid(),
2375                                 locations: location.to_locations(),
2376                                 span: location.to_locations().span(body),
2377                                 category,
2378                                 variance_info: ty::VarianceDiagInfo::default(),
2379                             });
2380
2381                             match mutbl {
2382                                 hir::Mutability::Not => {
2383                                     // Immutable reference. We don't need the base
2384                                     // to be valid for the entire lifetime of
2385                                     // the borrow.
2386                                     break;
2387                                 }
2388                                 hir::Mutability::Mut => {
2389                                     // Mutable reference. We *do* need the base
2390                                     // to be valid, because after the base becomes
2391                                     // invalid, someone else can use our mutable deref.
2392
2393                                     // This is in order to make the following function
2394                                     // illegal:
2395                                     // ```
2396                                     // fn unsafe_deref<'a, 'b>(x: &'a &'b mut T) -> &'b mut T {
2397                                     //     &mut *x
2398                                     // }
2399                                     // ```
2400                                     //
2401                                     // As otherwise you could clone `&mut T` using the
2402                                     // following function:
2403                                     // ```
2404                                     // fn bad(x: &mut T) -> (&mut T, &mut T) {
2405                                     //     let my_clone = unsafe_deref(&'a x);
2406                                     //     ENDREGION 'a;
2407                                     //     (my_clone, x)
2408                                     // }
2409                                     // ```
2410                                 }
2411                             }
2412                         }
2413                         ty::RawPtr(..) => {
2414                             // deref of raw pointer, guaranteed to be valid
2415                             break;
2416                         }
2417                         ty::Adt(def, _) if def.is_box() => {
2418                             // deref of `Box`, need the base to be valid - propagate
2419                         }
2420                         _ => bug!("unexpected deref ty {:?} in {:?}", base_ty, borrowed_place),
2421                     }
2422                 }
2423                 ProjectionElem::Field(..)
2424                 | ProjectionElem::Downcast(..)
2425                 | ProjectionElem::Index(..)
2426                 | ProjectionElem::ConstantIndex { .. }
2427                 | ProjectionElem::Subslice { .. } => {
2428                     // other field access
2429                 }
2430             }
2431         }
2432     }
2433
2434     fn prove_aggregate_predicates(
2435         &mut self,
2436         aggregate_kind: &AggregateKind<'tcx>,
2437         location: Location,
2438     ) {
2439         let tcx = self.tcx();
2440
2441         debug!(
2442             "prove_aggregate_predicates(aggregate_kind={:?}, location={:?})",
2443             aggregate_kind, location
2444         );
2445
2446         let (def_id, instantiated_predicates) = match aggregate_kind {
2447             AggregateKind::Adt(adt_did, _, substs, _, _) => {
2448                 (*adt_did, tcx.predicates_of(*adt_did).instantiate(tcx, substs))
2449             }
2450
2451             // For closures, we have some **extra requirements** we
2452             //
2453             // have to check. In particular, in their upvars and
2454             // signatures, closures often reference various regions
2455             // from the surrounding function -- we call those the
2456             // closure's free regions. When we borrow-check (and hence
2457             // region-check) closures, we may find that the closure
2458             // requires certain relationships between those free
2459             // regions. However, because those free regions refer to
2460             // portions of the CFG of their caller, the closure is not
2461             // in a position to verify those relationships. In that
2462             // case, the requirements get "propagated" to us, and so
2463             // we have to solve them here where we instantiate the
2464             // closure.
2465             //
2466             // Despite the opacity of the previous paragraph, this is
2467             // actually relatively easy to understand in terms of the
2468             // desugaring. A closure gets desugared to a struct, and
2469             // these extra requirements are basically like where
2470             // clauses on the struct.
2471             AggregateKind::Closure(def_id, substs)
2472             | AggregateKind::Generator(def_id, substs, _) => {
2473                 (*def_id, self.prove_closure_bounds(tcx, def_id.expect_local(), substs, location))
2474             }
2475
2476             AggregateKind::Array(_) | AggregateKind::Tuple => {
2477                 (CRATE_DEF_ID.to_def_id(), ty::InstantiatedPredicates::empty())
2478             }
2479         };
2480
2481         self.normalize_and_prove_instantiated_predicates(
2482             def_id,
2483             instantiated_predicates,
2484             location.to_locations(),
2485         );
2486     }
2487
2488     fn prove_closure_bounds(
2489         &mut self,
2490         tcx: TyCtxt<'tcx>,
2491         def_id: LocalDefId,
2492         substs: SubstsRef<'tcx>,
2493         location: Location,
2494     ) -> ty::InstantiatedPredicates<'tcx> {
2495         if let Some(ref closure_region_requirements) = tcx.mir_borrowck(def_id).closure_requirements
2496         {
2497             let closure_constraints = QueryRegionConstraints {
2498                 outlives: closure_region_requirements.apply_requirements(
2499                     tcx,
2500                     def_id.to_def_id(),
2501                     substs,
2502                 ),
2503
2504                 // Presently, closures never propagate member
2505                 // constraints to their parents -- they are enforced
2506                 // locally.  This is largely a non-issue as member
2507                 // constraints only come from `-> impl Trait` and
2508                 // friends which don't appear (thus far...) in
2509                 // closures.
2510                 member_constraints: vec![],
2511             };
2512
2513             let bounds_mapping = closure_constraints
2514                 .outlives
2515                 .iter()
2516                 .enumerate()
2517                 .filter_map(|(idx, constraint)| {
2518                     let ty::OutlivesPredicate(k1, r2) =
2519                         constraint.no_bound_vars().unwrap_or_else(|| {
2520                             bug!("query_constraint {:?} contained bound vars", constraint,);
2521                         });
2522
2523                     match k1.unpack() {
2524                         GenericArgKind::Lifetime(r1) => {
2525                             // constraint is r1: r2
2526                             let r1_vid = self.borrowck_context.universal_regions.to_region_vid(r1);
2527                             let r2_vid = self.borrowck_context.universal_regions.to_region_vid(r2);
2528                             let outlives_requirements =
2529                                 &closure_region_requirements.outlives_requirements[idx];
2530                             Some((
2531                                 (r1_vid, r2_vid),
2532                                 (outlives_requirements.category, outlives_requirements.blame_span),
2533                             ))
2534                         }
2535                         GenericArgKind::Type(_) | GenericArgKind::Const(_) => None,
2536                     }
2537                 })
2538                 .collect();
2539
2540             let existing = self
2541                 .borrowck_context
2542                 .constraints
2543                 .closure_bounds_mapping
2544                 .insert(location, bounds_mapping);
2545             assert!(existing.is_none(), "Multiple closures at the same location.");
2546
2547             self.push_region_constraints(
2548                 location.to_locations(),
2549                 ConstraintCategory::ClosureBounds,
2550                 &closure_constraints,
2551             );
2552         }
2553
2554         tcx.predicates_of(def_id).instantiate(tcx, substs)
2555     }
2556
2557     #[instrument(skip(self, body), level = "debug")]
2558     fn typeck_mir(&mut self, body: &Body<'tcx>) {
2559         self.last_span = body.span;
2560         debug!(?body.span);
2561
2562         for (local, local_decl) in body.local_decls.iter_enumerated() {
2563             self.check_local(&body, local, local_decl);
2564         }
2565
2566         for (block, block_data) in body.basic_blocks().iter_enumerated() {
2567             let mut location = Location { block, statement_index: 0 };
2568             for stmt in &block_data.statements {
2569                 if !stmt.source_info.span.is_dummy() {
2570                     self.last_span = stmt.source_info.span;
2571                 }
2572                 self.check_stmt(body, stmt, location);
2573                 location.statement_index += 1;
2574             }
2575
2576             self.check_terminator(&body, block_data.terminator(), location);
2577             self.check_iscleanup(&body, block_data);
2578         }
2579     }
2580 }
2581
2582 trait NormalizeLocation: fmt::Debug + Copy {
2583     fn to_locations(self) -> Locations;
2584 }
2585
2586 impl NormalizeLocation for Locations {
2587     fn to_locations(self) -> Locations {
2588         self
2589     }
2590 }
2591
2592 impl NormalizeLocation for Location {
2593     fn to_locations(self) -> Locations {
2594         Locations::Single(self)
2595     }
2596 }
2597
2598 /// Runs `infcx.instantiate_opaque_types`. Unlike other `TypeOp`s,
2599 /// this is not canonicalized - it directly affects the main `InferCtxt`
2600 /// that we use during MIR borrowchecking.
2601 #[derive(Debug)]
2602 pub(super) struct InstantiateOpaqueType<'tcx> {
2603     pub base_universe: Option<ty::UniverseIndex>,
2604     pub region_constraints: Option<RegionConstraintData<'tcx>>,
2605     pub obligations: Vec<PredicateObligation<'tcx>>,
2606 }
2607
2608 impl<'tcx> TypeOp<'tcx> for InstantiateOpaqueType<'tcx> {
2609     type Output = ();
2610     /// We use this type itself to store the information used
2611     /// when reporting errors. Since this is not a query, we don't
2612     /// re-run anything during error reporting - we just use the information
2613     /// we saved to help extract an error from the already-existing region
2614     /// constraints in our `InferCtxt`
2615     type ErrorInfo = InstantiateOpaqueType<'tcx>;
2616
2617     fn fully_perform(mut self, infcx: &InferCtxt<'_, 'tcx>) -> Fallible<TypeOpOutput<'tcx, Self>> {
2618         let (mut output, region_constraints) = scrape_region_constraints(infcx, || {
2619             Ok(InferOk { value: (), obligations: self.obligations.clone() })
2620         })?;
2621         self.region_constraints = Some(region_constraints);
2622         output.error_info = Some(self);
2623         Ok(output)
2624     }
2625 }