• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //! Code to extract the universally quantified regions declared on a
2 //! function and the relationships between them. For example:
3 //!
4 //! ```
5 //! fn foo<'a, 'b, 'c: 'b>() { }
6 //! ```
7 //!
8 //! here we would return a map assigning each of `{'a, 'b, 'c}`
9 //! to an index, as well as the `FreeRegionMap` which can compute
10 //! relationships between them.
11 //!
12 //! The code in this file doesn't *do anything* with those results; it
13 //! just returns them for other code to use.
14 
15 use either::Either;
16 use rustc_data_structures::fx::FxHashMap;
17 use rustc_errors::Diagnostic;
18 use rustc_hir as hir;
19 use rustc_hir::def_id::{DefId, LocalDefId};
20 use rustc_hir::lang_items::LangItem;
21 use rustc_hir::BodyOwnerKind;
22 use rustc_index::IndexVec;
23 use rustc_infer::infer::NllRegionVariableOrigin;
24 use rustc_middle::ty::fold::TypeFoldable;
25 use rustc_middle::ty::{self, InlineConstSubsts, InlineConstSubstsParts, RegionVid, Ty, TyCtxt};
26 use rustc_middle::ty::{InternalSubsts, SubstsRef};
27 use rustc_span::symbol::{kw, sym};
28 use rustc_span::Symbol;
29 use std::iter;
30 
31 use crate::renumber::{BoundRegionInfo, RegionCtxt};
32 use crate::BorrowckInferCtxt;
33 
34 #[derive(Debug)]
35 pub struct UniversalRegions<'tcx> {
36     indices: UniversalRegionIndices<'tcx>,
37 
38     /// The vid assigned to `'static`
39     pub fr_static: RegionVid,
40 
41     /// A special region vid created to represent the current MIR fn
42     /// body. It will outlive the entire CFG but it will not outlive
43     /// any other universal regions.
44     pub fr_fn_body: RegionVid,
45 
46     /// We create region variables such that they are ordered by their
47     /// `RegionClassification`. The first block are globals, then
48     /// externals, then locals. So, things from:
49     /// - `FIRST_GLOBAL_INDEX..first_extern_index` are global,
50     /// - `first_extern_index..first_local_index` are external,
51     /// - `first_local_index..num_universals` are local.
52     first_extern_index: usize,
53 
54     /// See `first_extern_index`.
55     first_local_index: usize,
56 
57     /// The total number of universal region variables instantiated.
58     num_universals: usize,
59 
60     /// The "defining" type for this function, with all universal
61     /// regions instantiated. For a closure or generator, this is the
62     /// closure type, but for a top-level function it's the `FnDef`.
63     pub defining_ty: DefiningTy<'tcx>,
64 
65     /// The return type of this function, with all regions replaced by
66     /// their universal `RegionVid` equivalents.
67     ///
68     /// N.B., associated types in this type have not been normalized,
69     /// as the name suggests. =)
70     pub unnormalized_output_ty: Ty<'tcx>,
71 
72     /// The fully liberated input types of this function, with all
73     /// regions replaced by their universal `RegionVid` equivalents.
74     ///
75     /// N.B., associated types in these types have not been normalized,
76     /// as the name suggests. =)
77     pub unnormalized_input_tys: &'tcx [Ty<'tcx>],
78 
79     pub yield_ty: Option<Ty<'tcx>>,
80 }
81 
82 /// The "defining type" for this MIR. The key feature of the "defining
83 /// type" is that it contains the information needed to derive all the
84 /// universal regions that are in scope as well as the types of the
85 /// inputs/output from the MIR. In general, early-bound universal
86 /// regions appear free in the defining type and late-bound regions
87 /// appear bound in the signature.
88 #[derive(Copy, Clone, Debug)]
89 pub enum DefiningTy<'tcx> {
90     /// The MIR is a closure. The signature is found via
91     /// `ClosureSubsts::closure_sig_ty`.
92     Closure(DefId, SubstsRef<'tcx>),
93 
94     /// The MIR is a generator. The signature is that generators take
95     /// no parameters and return the result of
96     /// `ClosureSubsts::generator_return_ty`.
97     Generator(DefId, SubstsRef<'tcx>, hir::Movability),
98 
99     /// The MIR is a fn item with the given `DefId` and substs. The signature
100     /// of the function can be bound then with the `fn_sig` query.
101     FnDef(DefId, SubstsRef<'tcx>),
102 
103     /// The MIR represents some form of constant. The signature then
104     /// is that it has no inputs and a single return value, which is
105     /// the value of the constant.
106     Const(DefId, SubstsRef<'tcx>),
107 
108     /// The MIR represents an inline const. The signature has no inputs and a
109     /// single return value found via `InlineConstSubsts::ty`.
110     InlineConst(DefId, SubstsRef<'tcx>),
111 }
112 
113 impl<'tcx> DefiningTy<'tcx> {
114     /// Returns a list of all the upvar types for this MIR. If this is
115     /// not a closure or generator, there are no upvars, and hence it
116     /// will be an empty list. The order of types in this list will
117     /// match up with the upvar order in the HIR, typesystem, and MIR.
upvar_tys(self) -> impl Iterator<Item = Ty<'tcx>> + 'tcx118     pub fn upvar_tys(self) -> impl Iterator<Item = Ty<'tcx>> + 'tcx {
119         match self {
120             DefiningTy::Closure(_, substs) => Either::Left(substs.as_closure().upvar_tys()),
121             DefiningTy::Generator(_, substs, _) => {
122                 Either::Right(Either::Left(substs.as_generator().upvar_tys()))
123             }
124             DefiningTy::FnDef(..) | DefiningTy::Const(..) | DefiningTy::InlineConst(..) => {
125                 Either::Right(Either::Right(iter::empty()))
126             }
127         }
128     }
129 
130     /// Number of implicit inputs -- notably the "environment"
131     /// parameter for closures -- that appear in MIR but not in the
132     /// user's code.
implicit_inputs(self) -> usize133     pub fn implicit_inputs(self) -> usize {
134         match self {
135             DefiningTy::Closure(..) | DefiningTy::Generator(..) => 1,
136             DefiningTy::FnDef(..) | DefiningTy::Const(..) | DefiningTy::InlineConst(..) => 0,
137         }
138     }
139 
is_fn_def(&self) -> bool140     pub fn is_fn_def(&self) -> bool {
141         matches!(*self, DefiningTy::FnDef(..))
142     }
143 
is_const(&self) -> bool144     pub fn is_const(&self) -> bool {
145         matches!(*self, DefiningTy::Const(..) | DefiningTy::InlineConst(..))
146     }
147 
def_id(&self) -> DefId148     pub fn def_id(&self) -> DefId {
149         match *self {
150             DefiningTy::Closure(def_id, ..)
151             | DefiningTy::Generator(def_id, ..)
152             | DefiningTy::FnDef(def_id, ..)
153             | DefiningTy::Const(def_id, ..)
154             | DefiningTy::InlineConst(def_id, ..) => def_id,
155         }
156     }
157 }
158 
159 #[derive(Debug)]
160 struct UniversalRegionIndices<'tcx> {
161     /// For those regions that may appear in the parameter environment
162     /// ('static and early-bound regions), we maintain a map from the
163     /// `ty::Region` to the internal `RegionVid` we are using. This is
164     /// used because trait matching and type-checking will feed us
165     /// region constraints that reference those regions and we need to
166     /// be able to map them to our internal `RegionVid`. This is
167     /// basically equivalent to an `InternalSubsts`, except that it also
168     /// contains an entry for `ReStatic` -- it might be nice to just
169     /// use a substs, and then handle `ReStatic` another way.
170     indices: FxHashMap<ty::Region<'tcx>, RegionVid>,
171 
172     /// The vid assigned to `'static`. Used only for diagnostics.
173     pub fr_static: RegionVid,
174 }
175 
176 #[derive(Debug, PartialEq)]
177 pub enum RegionClassification {
178     /// A **global** region is one that can be named from
179     /// anywhere. There is only one, `'static`.
180     Global,
181 
182     /// An **external** region is only relevant for
183     /// closures, generators, and inline consts. In that
184     /// case, it refers to regions that are free in the type
185     /// -- basically, something bound in the surrounding context.
186     ///
187     /// Consider this example:
188     ///
189     /// ```ignore (pseudo-rust)
190     /// fn foo<'a, 'b>(a: &'a u32, b: &'b u32, c: &'static u32) {
191     ///   let closure = for<'x> |x: &'x u32| { .. };
192     ///    //           ^^^^^^^ pretend this were legal syntax
193     ///    //                   for declaring a late-bound region in
194     ///    //                   a closure signature
195     /// }
196     /// ```
197     ///
198     /// Here, the lifetimes `'a` and `'b` would be **external** to the
199     /// closure.
200     ///
201     /// If we are not analyzing a closure/generator/inline-const,
202     /// there are no external lifetimes.
203     External,
204 
205     /// A **local** lifetime is one about which we know the full set
206     /// of relevant constraints (that is, relationships to other named
207     /// regions). For a closure, this includes any region bound in
208     /// the closure's signature. For a fn item, this includes all
209     /// regions other than global ones.
210     ///
211     /// Continuing with the example from `External`, if we were
212     /// analyzing the closure, then `'x` would be local (and `'a` and
213     /// `'b` are external). If we are analyzing the function item
214     /// `foo`, then `'a` and `'b` are local (and `'x` is not in
215     /// scope).
216     Local,
217 }
218 
219 const FIRST_GLOBAL_INDEX: usize = 0;
220 
221 impl<'tcx> UniversalRegions<'tcx> {
222     /// Creates a new and fully initialized `UniversalRegions` that
223     /// contains indices for all the free regions found in the given
224     /// MIR -- that is, all the regions that appear in the function's
225     /// signature. This will also compute the relationships that are
226     /// known between those regions.
new( infcx: &BorrowckInferCtxt<'_, 'tcx>, mir_def: LocalDefId, param_env: ty::ParamEnv<'tcx>, ) -> Self227     pub fn new(
228         infcx: &BorrowckInferCtxt<'_, 'tcx>,
229         mir_def: LocalDefId,
230         param_env: ty::ParamEnv<'tcx>,
231     ) -> Self {
232         UniversalRegionsBuilder { infcx, mir_def, param_env }.build()
233     }
234 
235     /// Given a reference to a closure type, extracts all the values
236     /// from its free regions and returns a vector with them. This is
237     /// used when the closure's creator checks that the
238     /// `ClosureRegionRequirements` are met. The requirements from
239     /// `ClosureRegionRequirements` are expressed in terms of
240     /// `RegionVid` entries that map into the returned vector `V`: so
241     /// if the `ClosureRegionRequirements` contains something like
242     /// `'1: '2`, then the caller would impose the constraint that
243     /// `V[1]: V[2]`.
closure_mapping( tcx: TyCtxt<'tcx>, closure_substs: SubstsRef<'tcx>, expected_num_vars: usize, closure_def_id: LocalDefId, ) -> IndexVec<RegionVid, ty::Region<'tcx>>244     pub fn closure_mapping(
245         tcx: TyCtxt<'tcx>,
246         closure_substs: SubstsRef<'tcx>,
247         expected_num_vars: usize,
248         closure_def_id: LocalDefId,
249     ) -> IndexVec<RegionVid, ty::Region<'tcx>> {
250         let mut region_mapping = IndexVec::with_capacity(expected_num_vars);
251         region_mapping.push(tcx.lifetimes.re_static);
252         tcx.for_each_free_region(&closure_substs, |fr| {
253             region_mapping.push(fr);
254         });
255 
256         for_each_late_bound_region_in_recursive_scope(tcx, tcx.local_parent(closure_def_id), |r| {
257             region_mapping.push(r);
258         });
259 
260         assert_eq!(
261             region_mapping.len(),
262             expected_num_vars,
263             "index vec had unexpected number of variables"
264         );
265 
266         region_mapping
267     }
268 
269     /// Returns `true` if `r` is a member of this set of universal regions.
is_universal_region(&self, r: RegionVid) -> bool270     pub fn is_universal_region(&self, r: RegionVid) -> bool {
271         (FIRST_GLOBAL_INDEX..self.num_universals).contains(&r.index())
272     }
273 
274     /// Classifies `r` as a universal region, returning `None` if this
275     /// is not a member of this set of universal regions.
region_classification(&self, r: RegionVid) -> Option<RegionClassification>276     pub fn region_classification(&self, r: RegionVid) -> Option<RegionClassification> {
277         let index = r.index();
278         if (FIRST_GLOBAL_INDEX..self.first_extern_index).contains(&index) {
279             Some(RegionClassification::Global)
280         } else if (self.first_extern_index..self.first_local_index).contains(&index) {
281             Some(RegionClassification::External)
282         } else if (self.first_local_index..self.num_universals).contains(&index) {
283             Some(RegionClassification::Local)
284         } else {
285             None
286         }
287     }
288 
289     /// Returns an iterator over all the RegionVids corresponding to
290     /// universally quantified free regions.
universal_regions(&self) -> impl Iterator<Item = RegionVid>291     pub fn universal_regions(&self) -> impl Iterator<Item = RegionVid> {
292         (FIRST_GLOBAL_INDEX..self.num_universals).map(RegionVid::from_usize)
293     }
294 
295     /// Returns `true` if `r` is classified as an local region.
is_local_free_region(&self, r: RegionVid) -> bool296     pub fn is_local_free_region(&self, r: RegionVid) -> bool {
297         self.region_classification(r) == Some(RegionClassification::Local)
298     }
299 
300     /// Returns the number of universal regions created in any category.
len(&self) -> usize301     pub fn len(&self) -> usize {
302         self.num_universals
303     }
304 
305     /// Returns the number of global plus external universal regions.
306     /// For closures, these are the regions that appear free in the
307     /// closure type (versus those bound in the closure
308     /// signature). They are therefore the regions between which the
309     /// closure may impose constraints that its creator must verify.
num_global_and_external_regions(&self) -> usize310     pub fn num_global_and_external_regions(&self) -> usize {
311         self.first_local_index
312     }
313 
314     /// Gets an iterator over all the early-bound regions that have names.
315     /// Iteration order may be unstable, so this should only be used when
316     /// iteration order doesn't affect anything
317     #[allow(rustc::potential_query_instability)]
named_universal_regions<'s>( &'s self, ) -> impl Iterator<Item = (ty::Region<'tcx>, ty::RegionVid)> + 's318     pub fn named_universal_regions<'s>(
319         &'s self,
320     ) -> impl Iterator<Item = (ty::Region<'tcx>, ty::RegionVid)> + 's {
321         self.indices.indices.iter().map(|(&r, &v)| (r, v))
322     }
323 
324     /// See `UniversalRegionIndices::to_region_vid`.
to_region_vid(&self, r: ty::Region<'tcx>) -> RegionVid325     pub fn to_region_vid(&self, r: ty::Region<'tcx>) -> RegionVid {
326         self.indices.to_region_vid(r)
327     }
328 
329     /// As part of the NLL unit tests, you can annotate a function with
330     /// `#[rustc_regions]`, and we will emit information about the region
331     /// inference context and -- in particular -- the external constraints
332     /// that this region imposes on others. The methods in this file
333     /// handle the part about dumping the inference context internal
334     /// state.
annotate(&self, tcx: TyCtxt<'tcx>, err: &mut Diagnostic)335     pub(crate) fn annotate(&self, tcx: TyCtxt<'tcx>, err: &mut Diagnostic) {
336         match self.defining_ty {
337             DefiningTy::Closure(def_id, substs) => {
338                 err.note(format!(
339                     "defining type: {} with closure substs {:#?}",
340                     tcx.def_path_str_with_substs(def_id, substs),
341                     &substs[tcx.generics_of(def_id).parent_count..],
342                 ));
343 
344                 // FIXME: It'd be nice to print the late-bound regions
345                 // here, but unfortunately these wind up stored into
346                 // tests, and the resulting print-outs include def-ids
347                 // and other things that are not stable across tests!
348                 // So we just include the region-vid. Annoying.
349                 for_each_late_bound_region_in_recursive_scope(tcx, def_id.expect_local(), |r| {
350                     err.note(format!("late-bound region is {:?}", self.to_region_vid(r)));
351                 });
352             }
353             DefiningTy::Generator(def_id, substs, _) => {
354                 err.note(format!(
355                     "defining type: {} with generator substs {:#?}",
356                     tcx.def_path_str_with_substs(def_id, substs),
357                     &substs[tcx.generics_of(def_id).parent_count..],
358                 ));
359 
360                 // FIXME: As above, we'd like to print out the region
361                 // `r` but doing so is not stable across architectures
362                 // and so forth.
363                 for_each_late_bound_region_in_recursive_scope(tcx, def_id.expect_local(), |r| {
364                     err.note(format!("late-bound region is {:?}", self.to_region_vid(r)));
365                 });
366             }
367             DefiningTy::FnDef(def_id, substs) => {
368                 err.note(format!(
369                     "defining type: {}",
370                     tcx.def_path_str_with_substs(def_id, substs),
371                 ));
372             }
373             DefiningTy::Const(def_id, substs) => {
374                 err.note(format!(
375                     "defining constant type: {}",
376                     tcx.def_path_str_with_substs(def_id, substs),
377                 ));
378             }
379             DefiningTy::InlineConst(def_id, substs) => {
380                 err.note(format!(
381                     "defining inline constant type: {}",
382                     tcx.def_path_str_with_substs(def_id, substs),
383                 ));
384             }
385         }
386     }
387 }
388 
389 struct UniversalRegionsBuilder<'cx, 'tcx> {
390     infcx: &'cx BorrowckInferCtxt<'cx, 'tcx>,
391     mir_def: LocalDefId,
392     param_env: ty::ParamEnv<'tcx>,
393 }
394 
395 const FR: NllRegionVariableOrigin = NllRegionVariableOrigin::FreeRegion;
396 
397 impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> {
build(self) -> UniversalRegions<'tcx>398     fn build(self) -> UniversalRegions<'tcx> {
399         debug!("build(mir_def={:?})", self.mir_def);
400 
401         let param_env = self.param_env;
402         debug!("build: param_env={:?}", param_env);
403 
404         assert_eq!(FIRST_GLOBAL_INDEX, self.infcx.num_region_vars());
405 
406         // Create the "global" region that is always free in all contexts: 'static.
407         let fr_static =
408             self.infcx.next_nll_region_var(FR, || RegionCtxt::Free(kw::Static)).as_var();
409 
410         // We've now added all the global regions. The next ones we
411         // add will be external.
412         let first_extern_index = self.infcx.num_region_vars();
413 
414         let defining_ty = self.defining_ty();
415         debug!("build: defining_ty={:?}", defining_ty);
416 
417         let mut indices = self.compute_indices(fr_static, defining_ty);
418         debug!("build: indices={:?}", indices);
419 
420         let typeck_root_def_id = self.infcx.tcx.typeck_root_def_id(self.mir_def.to_def_id());
421 
422         // If this is a 'root' body (not a closure/generator/inline const), then
423         // there are no extern regions, so the local regions start at the same
424         // position as the (empty) sub-list of extern regions
425         let first_local_index = if self.mir_def.to_def_id() == typeck_root_def_id {
426             first_extern_index
427         } else {
428             // If this is a closure, generator, or inline-const, then the late-bound regions from the enclosing
429             // function/closures are actually external regions to us. For example, here, 'a is not local
430             // to the closure c (although it is local to the fn foo):
431             // fn foo<'a>() {
432             //     let c = || { let x: &'a u32 = ...; }
433             // }
434             for_each_late_bound_region_in_recursive_scope(
435                 self.infcx.tcx,
436                 self.infcx.tcx.local_parent(self.mir_def),
437                 |r| {
438                     debug!(?r);
439                     if !indices.indices.contains_key(&r) {
440                         let region_vid = {
441                             let name = r.get_name_or_anon();
442                             self.infcx.next_nll_region_var(FR, || {
443                                 RegionCtxt::LateBound(BoundRegionInfo::Name(name))
444                             })
445                         };
446 
447                         debug!(?region_vid);
448                         indices.insert_late_bound_region(r, region_vid.as_var());
449                     }
450                 },
451             );
452 
453             // Any regions created during the execution of `defining_ty` or during the above
454             // late-bound region replacement are all considered 'extern' regions
455             self.infcx.num_region_vars()
456         };
457 
458         // "Liberate" the late-bound regions. These correspond to
459         // "local" free regions.
460 
461         let bound_inputs_and_output = self.compute_inputs_and_output(&indices, defining_ty);
462 
463         let inputs_and_output = self.infcx.replace_bound_regions_with_nll_infer_vars(
464             FR,
465             self.mir_def,
466             bound_inputs_and_output,
467             &mut indices,
468         );
469         // Converse of above, if this is a function/closure then the late-bound regions declared on its
470         // signature are local.
471         for_each_late_bound_region_in_item(self.infcx.tcx, self.mir_def, |r| {
472             debug!(?r);
473             if !indices.indices.contains_key(&r) {
474                 let region_vid = {
475                     let name = r.get_name_or_anon();
476                     self.infcx.next_nll_region_var(FR, || {
477                         RegionCtxt::LateBound(BoundRegionInfo::Name(name))
478                     })
479                 };
480 
481                 debug!(?region_vid);
482                 indices.insert_late_bound_region(r, region_vid.as_var());
483             }
484         });
485 
486         let (unnormalized_output_ty, mut unnormalized_input_tys) =
487             inputs_and_output.split_last().unwrap();
488 
489         // C-variadic fns also have a `VaList` input that's not listed in the signature
490         // (as it's created inside the body itself, not passed in from outside).
491         if let DefiningTy::FnDef(def_id, _) = defining_ty {
492             if self.infcx.tcx.fn_sig(def_id).skip_binder().c_variadic() {
493                 let va_list_did = self.infcx.tcx.require_lang_item(
494                     LangItem::VaList,
495                     Some(self.infcx.tcx.def_span(self.mir_def)),
496                 );
497 
498                 let reg_vid = self
499                     .infcx
500                     .next_nll_region_var(FR, || RegionCtxt::Free(Symbol::intern("c-variadic")))
501                     .as_var();
502 
503                 let region = ty::Region::new_var(self.infcx.tcx, reg_vid);
504                 let va_list_ty =
505                     self.infcx.tcx.type_of(va_list_did).subst(self.infcx.tcx, &[region.into()]);
506 
507                 unnormalized_input_tys = self.infcx.tcx.mk_type_list_from_iter(
508                     unnormalized_input_tys.iter().copied().chain(iter::once(va_list_ty)),
509                 );
510             }
511         }
512 
513         let fr_fn_body = self
514             .infcx
515             .next_nll_region_var(FR, || RegionCtxt::Free(Symbol::intern("fn_body")))
516             .as_var();
517 
518         let num_universals = self.infcx.num_region_vars();
519 
520         debug!("build: global regions = {}..{}", FIRST_GLOBAL_INDEX, first_extern_index);
521         debug!("build: extern regions = {}..{}", first_extern_index, first_local_index);
522         debug!("build: local regions  = {}..{}", first_local_index, num_universals);
523 
524         let yield_ty = match defining_ty {
525             DefiningTy::Generator(_, substs, _) => Some(substs.as_generator().yield_ty()),
526             _ => None,
527         };
528 
529         UniversalRegions {
530             indices,
531             fr_static,
532             fr_fn_body,
533             first_extern_index,
534             first_local_index,
535             num_universals,
536             defining_ty,
537             unnormalized_output_ty: *unnormalized_output_ty,
538             unnormalized_input_tys,
539             yield_ty,
540         }
541     }
542 
543     /// Returns the "defining type" of the current MIR;
544     /// see `DefiningTy` for details.
defining_ty(&self) -> DefiningTy<'tcx>545     fn defining_ty(&self) -> DefiningTy<'tcx> {
546         let tcx = self.infcx.tcx;
547         let typeck_root_def_id = tcx.typeck_root_def_id(self.mir_def.to_def_id());
548 
549         match tcx.hir().body_owner_kind(self.mir_def) {
550             BodyOwnerKind::Closure | BodyOwnerKind::Fn => {
551                 let defining_ty = tcx.type_of(self.mir_def).subst_identity();
552 
553                 debug!("defining_ty (pre-replacement): {:?}", defining_ty);
554 
555                 let defining_ty =
556                     self.infcx.replace_free_regions_with_nll_infer_vars(FR, defining_ty);
557 
558                 match *defining_ty.kind() {
559                     ty::Closure(def_id, substs) => DefiningTy::Closure(def_id, substs),
560                     ty::Generator(def_id, substs, movability) => {
561                         DefiningTy::Generator(def_id, substs, movability)
562                     }
563                     ty::FnDef(def_id, substs) => DefiningTy::FnDef(def_id, substs),
564                     _ => span_bug!(
565                         tcx.def_span(self.mir_def),
566                         "expected defining type for `{:?}`: `{:?}`",
567                         self.mir_def,
568                         defining_ty
569                     ),
570                 }
571             }
572 
573             BodyOwnerKind::Const | BodyOwnerKind::Static(..) => {
574                 let identity_substs = InternalSubsts::identity_for_item(tcx, typeck_root_def_id);
575                 if self.mir_def.to_def_id() == typeck_root_def_id {
576                     let substs =
577                         self.infcx.replace_free_regions_with_nll_infer_vars(FR, identity_substs);
578                     DefiningTy::Const(self.mir_def.to_def_id(), substs)
579                 } else {
580                     // FIXME this line creates a dependency between borrowck and typeck.
581                     //
582                     // This is required for `AscribeUserType` canonical query, which will call
583                     // `type_of(inline_const_def_id)`. That `type_of` would inject erased lifetimes
584                     // into borrowck, which is ICE #78174.
585                     //
586                     // As a workaround, inline consts have an additional generic param (`ty`
587                     // below), so that `type_of(inline_const_def_id).substs(substs)` uses the
588                     // proper type with NLL infer vars.
589                     let ty = tcx
590                         .typeck(self.mir_def)
591                         .node_type(tcx.local_def_id_to_hir_id(self.mir_def));
592                     let substs = InlineConstSubsts::new(
593                         tcx,
594                         InlineConstSubstsParts { parent_substs: identity_substs, ty },
595                     )
596                     .substs;
597                     let substs = self.infcx.replace_free_regions_with_nll_infer_vars(FR, substs);
598                     DefiningTy::InlineConst(self.mir_def.to_def_id(), substs)
599                 }
600             }
601         }
602     }
603 
604     /// Builds a hashmap that maps from the universal regions that are
605     /// in scope (as a `ty::Region<'tcx>`) to their indices (as a
606     /// `RegionVid`). The map returned by this function contains only
607     /// the early-bound regions.
compute_indices( &self, fr_static: RegionVid, defining_ty: DefiningTy<'tcx>, ) -> UniversalRegionIndices<'tcx>608     fn compute_indices(
609         &self,
610         fr_static: RegionVid,
611         defining_ty: DefiningTy<'tcx>,
612     ) -> UniversalRegionIndices<'tcx> {
613         let tcx = self.infcx.tcx;
614         let typeck_root_def_id = tcx.typeck_root_def_id(self.mir_def.to_def_id());
615         let identity_substs = InternalSubsts::identity_for_item(tcx, typeck_root_def_id);
616         let fr_substs = match defining_ty {
617             DefiningTy::Closure(_, substs)
618             | DefiningTy::Generator(_, substs, _)
619             | DefiningTy::InlineConst(_, substs) => {
620                 // In the case of closures, we rely on the fact that
621                 // the first N elements in the ClosureSubsts are
622                 // inherited from the `typeck_root_def_id`.
623                 // Therefore, when we zip together (below) with
624                 // `identity_substs`, we will get only those regions
625                 // that correspond to early-bound regions declared on
626                 // the `typeck_root_def_id`.
627                 assert!(substs.len() >= identity_substs.len());
628                 assert_eq!(substs.regions().count(), identity_substs.regions().count());
629                 substs
630             }
631 
632             DefiningTy::FnDef(_, substs) | DefiningTy::Const(_, substs) => substs,
633         };
634 
635         let global_mapping = iter::once((tcx.lifetimes.re_static, fr_static));
636         let subst_mapping =
637             iter::zip(identity_substs.regions(), fr_substs.regions().map(|r| r.as_var()));
638 
639         UniversalRegionIndices { indices: global_mapping.chain(subst_mapping).collect(), fr_static }
640     }
641 
compute_inputs_and_output( &self, indices: &UniversalRegionIndices<'tcx>, defining_ty: DefiningTy<'tcx>, ) -> ty::Binder<'tcx, &'tcx ty::List<Ty<'tcx>>>642     fn compute_inputs_and_output(
643         &self,
644         indices: &UniversalRegionIndices<'tcx>,
645         defining_ty: DefiningTy<'tcx>,
646     ) -> ty::Binder<'tcx, &'tcx ty::List<Ty<'tcx>>> {
647         let tcx = self.infcx.tcx;
648         match defining_ty {
649             DefiningTy::Closure(def_id, substs) => {
650                 assert_eq!(self.mir_def.to_def_id(), def_id);
651                 let closure_sig = substs.as_closure().sig();
652                 let inputs_and_output = closure_sig.inputs_and_output();
653                 let bound_vars = tcx.mk_bound_variable_kinds_from_iter(
654                     inputs_and_output
655                         .bound_vars()
656                         .iter()
657                         .chain(iter::once(ty::BoundVariableKind::Region(ty::BrEnv))),
658                 );
659                 let br = ty::BoundRegion {
660                     var: ty::BoundVar::from_usize(bound_vars.len() - 1),
661                     kind: ty::BrEnv,
662                 };
663                 let env_region = ty::Region::new_late_bound(tcx, ty::INNERMOST, br);
664                 let closure_ty = tcx.closure_env_ty(def_id, substs, env_region).unwrap();
665 
666                 // The "inputs" of the closure in the
667                 // signature appear as a tuple. The MIR side
668                 // flattens this tuple.
669                 let (&output, tuplized_inputs) =
670                     inputs_and_output.skip_binder().split_last().unwrap();
671                 assert_eq!(tuplized_inputs.len(), 1, "multiple closure inputs");
672                 let &ty::Tuple(inputs) = tuplized_inputs[0].kind() else {
673                     bug!("closure inputs not a tuple: {:?}", tuplized_inputs[0]);
674                 };
675 
676                 ty::Binder::bind_with_vars(
677                     tcx.mk_type_list_from_iter(
678                         iter::once(closure_ty).chain(inputs).chain(iter::once(output)),
679                     ),
680                     bound_vars,
681                 )
682             }
683 
684             DefiningTy::Generator(def_id, substs, movability) => {
685                 assert_eq!(self.mir_def.to_def_id(), def_id);
686                 let resume_ty = substs.as_generator().resume_ty();
687                 let output = substs.as_generator().return_ty();
688                 let generator_ty = Ty::new_generator(tcx, def_id, substs, movability);
689                 let inputs_and_output =
690                     self.infcx.tcx.mk_type_list(&[generator_ty, resume_ty, output]);
691                 ty::Binder::dummy(inputs_and_output)
692             }
693 
694             DefiningTy::FnDef(def_id, _) => {
695                 let sig = tcx.fn_sig(def_id).subst_identity();
696                 let sig = indices.fold_to_region_vids(tcx, sig);
697                 sig.inputs_and_output()
698             }
699 
700             DefiningTy::Const(def_id, _) => {
701                 // For a constant body, there are no inputs, and one
702                 // "output" (the type of the constant).
703                 assert_eq!(self.mir_def.to_def_id(), def_id);
704                 let ty = tcx.type_of(self.mir_def).subst_identity();
705                 let ty = indices.fold_to_region_vids(tcx, ty);
706                 ty::Binder::dummy(tcx.mk_type_list(&[ty]))
707             }
708 
709             DefiningTy::InlineConst(def_id, substs) => {
710                 assert_eq!(self.mir_def.to_def_id(), def_id);
711                 let ty = substs.as_inline_const().ty();
712                 ty::Binder::dummy(tcx.mk_type_list(&[ty]))
713             }
714         }
715     }
716 }
717 
718 trait InferCtxtExt<'tcx> {
replace_free_regions_with_nll_infer_vars<T>( &self, origin: NllRegionVariableOrigin, value: T, ) -> T where T: TypeFoldable<TyCtxt<'tcx>>719     fn replace_free_regions_with_nll_infer_vars<T>(
720         &self,
721         origin: NllRegionVariableOrigin,
722         value: T,
723     ) -> T
724     where
725         T: TypeFoldable<TyCtxt<'tcx>>;
726 
replace_bound_regions_with_nll_infer_vars<T>( &self, origin: NllRegionVariableOrigin, all_outlive_scope: LocalDefId, value: ty::Binder<'tcx, T>, indices: &mut UniversalRegionIndices<'tcx>, ) -> T where T: TypeFoldable<TyCtxt<'tcx>>727     fn replace_bound_regions_with_nll_infer_vars<T>(
728         &self,
729         origin: NllRegionVariableOrigin,
730         all_outlive_scope: LocalDefId,
731         value: ty::Binder<'tcx, T>,
732         indices: &mut UniversalRegionIndices<'tcx>,
733     ) -> T
734     where
735         T: TypeFoldable<TyCtxt<'tcx>>;
736 
replace_late_bound_regions_with_nll_infer_vars_in_recursive_scope( &self, mir_def_id: LocalDefId, indices: &mut UniversalRegionIndices<'tcx>, )737     fn replace_late_bound_regions_with_nll_infer_vars_in_recursive_scope(
738         &self,
739         mir_def_id: LocalDefId,
740         indices: &mut UniversalRegionIndices<'tcx>,
741     );
742 
replace_late_bound_regions_with_nll_infer_vars_in_item( &self, mir_def_id: LocalDefId, indices: &mut UniversalRegionIndices<'tcx>, )743     fn replace_late_bound_regions_with_nll_infer_vars_in_item(
744         &self,
745         mir_def_id: LocalDefId,
746         indices: &mut UniversalRegionIndices<'tcx>,
747     );
748 }
749 
750 impl<'cx, 'tcx> InferCtxtExt<'tcx> for BorrowckInferCtxt<'cx, 'tcx> {
751     #[instrument(skip(self), level = "debug")]
replace_free_regions_with_nll_infer_vars<T>( &self, origin: NllRegionVariableOrigin, value: T, ) -> T where T: TypeFoldable<TyCtxt<'tcx>>,752     fn replace_free_regions_with_nll_infer_vars<T>(
753         &self,
754         origin: NllRegionVariableOrigin,
755         value: T,
756     ) -> T
757     where
758         T: TypeFoldable<TyCtxt<'tcx>>,
759     {
760         self.infcx.tcx.fold_regions(value, |region, _depth| {
761             let name = region.get_name_or_anon();
762             debug!(?region, ?name);
763 
764             self.next_nll_region_var(origin, || RegionCtxt::Free(name))
765         })
766     }
767 
768     #[instrument(level = "debug", skip(self, indices))]
replace_bound_regions_with_nll_infer_vars<T>( &self, origin: NllRegionVariableOrigin, all_outlive_scope: LocalDefId, value: ty::Binder<'tcx, T>, indices: &mut UniversalRegionIndices<'tcx>, ) -> T where T: TypeFoldable<TyCtxt<'tcx>>,769     fn replace_bound_regions_with_nll_infer_vars<T>(
770         &self,
771         origin: NllRegionVariableOrigin,
772         all_outlive_scope: LocalDefId,
773         value: ty::Binder<'tcx, T>,
774         indices: &mut UniversalRegionIndices<'tcx>,
775     ) -> T
776     where
777         T: TypeFoldable<TyCtxt<'tcx>>,
778     {
779         let (value, _map) = self.tcx.replace_late_bound_regions(value, |br| {
780             debug!(?br);
781             let liberated_region =
782                 ty::Region::new_free(self.tcx, all_outlive_scope.to_def_id(), br.kind);
783             let region_vid = {
784                 let name = match br.kind.get_name() {
785                     Some(name) => name,
786                     _ => sym::anon,
787                 };
788 
789                 self.next_nll_region_var(origin, || RegionCtxt::Bound(BoundRegionInfo::Name(name)))
790             };
791 
792             indices.insert_late_bound_region(liberated_region, region_vid.as_var());
793             debug!(?liberated_region, ?region_vid);
794             region_vid
795         });
796         value
797     }
798 
799     /// Finds late-bound regions that do not appear in the parameter listing and adds them to the
800     /// indices vector. Typically, we identify late-bound regions as we process the inputs and
801     /// outputs of the closure/function. However, sometimes there are late-bound regions which do
802     /// not appear in the fn parameters but which are nonetheless in scope. The simplest case of
803     /// this are unused functions, like fn foo<'a>() { } (see e.g., #51351). Despite not being used,
804     /// users can still reference these regions (e.g., let x: &'a u32 = &22;), so we need to create
805     /// entries for them and store them in the indices map. This code iterates over the complete
806     /// set of late-bound regions and checks for any that we have not yet seen, adding them to the
807     /// inputs vector.
808     #[instrument(skip(self, indices))]
replace_late_bound_regions_with_nll_infer_vars_in_recursive_scope( &self, mir_def_id: LocalDefId, indices: &mut UniversalRegionIndices<'tcx>, )809     fn replace_late_bound_regions_with_nll_infer_vars_in_recursive_scope(
810         &self,
811         mir_def_id: LocalDefId,
812         indices: &mut UniversalRegionIndices<'tcx>,
813     ) {
814         for_each_late_bound_region_in_recursive_scope(self.tcx, mir_def_id, |r| {
815             debug!(?r);
816             if !indices.indices.contains_key(&r) {
817                 let region_vid = {
818                     let name = r.get_name_or_anon();
819                     self.next_nll_region_var(FR, || {
820                         RegionCtxt::LateBound(BoundRegionInfo::Name(name))
821                     })
822                 };
823 
824                 debug!(?region_vid);
825                 indices.insert_late_bound_region(r, region_vid.as_var());
826             }
827         });
828     }
829 
830     #[instrument(skip(self, indices))]
replace_late_bound_regions_with_nll_infer_vars_in_item( &self, mir_def_id: LocalDefId, indices: &mut UniversalRegionIndices<'tcx>, )831     fn replace_late_bound_regions_with_nll_infer_vars_in_item(
832         &self,
833         mir_def_id: LocalDefId,
834         indices: &mut UniversalRegionIndices<'tcx>,
835     ) {
836         for_each_late_bound_region_in_item(self.tcx, mir_def_id, |r| {
837             debug!(?r);
838             if !indices.indices.contains_key(&r) {
839                 let region_vid = {
840                     let name = r.get_name_or_anon();
841                     self.next_nll_region_var(FR, || {
842                         RegionCtxt::LateBound(BoundRegionInfo::Name(name))
843                     })
844                 };
845 
846                 indices.insert_late_bound_region(r, region_vid.as_var());
847             }
848         });
849     }
850 }
851 
852 impl<'tcx> UniversalRegionIndices<'tcx> {
853     /// Initially, the `UniversalRegionIndices` map contains only the
854     /// early-bound regions in scope. Once that is all setup, we come
855     /// in later and instantiate the late-bound regions, and then we
856     /// insert the `ReFree` version of those into the map as
857     /// well. These are used for error reporting.
insert_late_bound_region(&mut self, r: ty::Region<'tcx>, vid: ty::RegionVid)858     fn insert_late_bound_region(&mut self, r: ty::Region<'tcx>, vid: ty::RegionVid) {
859         debug!("insert_late_bound_region({:?}, {:?})", r, vid);
860         self.indices.insert(r, vid);
861     }
862 
863     /// Converts `r` into a local inference variable: `r` can either
864     /// be a `ReVar` (i.e., already a reference to an inference
865     /// variable) or it can be `'static` or some early-bound
866     /// region. This is useful when taking the results from
867     /// type-checking and trait-matching, which may sometimes
868     /// reference those regions from the `ParamEnv`. It is also used
869     /// during initialization. Relies on the `indices` map having been
870     /// fully initialized.
to_region_vid(&self, r: ty::Region<'tcx>) -> RegionVid871     pub fn to_region_vid(&self, r: ty::Region<'tcx>) -> RegionVid {
872         if let ty::ReVar(..) = *r {
873             r.as_var()
874         } else if r.is_error() {
875             // We use the `'static` `RegionVid` because `ReError` doesn't actually exist in the
876             // `UniversalRegionIndices`. This is fine because 1) it is a fallback only used if
877             // errors are being emitted and 2) it leaves the happy path unaffected.
878             self.fr_static
879         } else {
880             *self
881                 .indices
882                 .get(&r)
883                 .unwrap_or_else(|| bug!("cannot convert `{:?}` to a region vid", r))
884         }
885     }
886 
887     /// Replaces all free regions in `value` with region vids, as
888     /// returned by `to_region_vid`.
fold_to_region_vids<T>(&self, tcx: TyCtxt<'tcx>, value: T) -> T where T: TypeFoldable<TyCtxt<'tcx>>,889     pub fn fold_to_region_vids<T>(&self, tcx: TyCtxt<'tcx>, value: T) -> T
890     where
891         T: TypeFoldable<TyCtxt<'tcx>>,
892     {
893         tcx.fold_regions(value, |region, _| ty::Region::new_var(tcx, self.to_region_vid(region)))
894     }
895 }
896 
897 /// Iterates over the late-bound regions defined on `mir_def_id` and all of its
898 /// parents, up to the typeck root, and invokes `f` with the liberated form
899 /// of each one.
for_each_late_bound_region_in_recursive_scope<'tcx>( tcx: TyCtxt<'tcx>, mut mir_def_id: LocalDefId, mut f: impl FnMut(ty::Region<'tcx>), )900 fn for_each_late_bound_region_in_recursive_scope<'tcx>(
901     tcx: TyCtxt<'tcx>,
902     mut mir_def_id: LocalDefId,
903     mut f: impl FnMut(ty::Region<'tcx>),
904 ) {
905     let typeck_root_def_id = tcx.typeck_root_def_id(mir_def_id.to_def_id());
906 
907     // Walk up the tree, collecting late-bound regions until we hit the typeck root
908     loop {
909         for_each_late_bound_region_in_item(tcx, mir_def_id, &mut f);
910 
911         if mir_def_id.to_def_id() == typeck_root_def_id {
912             break;
913         } else {
914             mir_def_id = tcx.local_parent(mir_def_id);
915         }
916     }
917 }
918 
919 /// Iterates over the late-bound regions defined on `mir_def_id` and all of its
920 /// parents, up to the typeck root, and invokes `f` with the liberated form
921 /// of each one.
for_each_late_bound_region_in_item<'tcx>( tcx: TyCtxt<'tcx>, mir_def_id: LocalDefId, mut f: impl FnMut(ty::Region<'tcx>), )922 fn for_each_late_bound_region_in_item<'tcx>(
923     tcx: TyCtxt<'tcx>,
924     mir_def_id: LocalDefId,
925     mut f: impl FnMut(ty::Region<'tcx>),
926 ) {
927     if !tcx.def_kind(mir_def_id).is_fn_like() {
928         return;
929     }
930 
931     for bound_var in tcx.late_bound_vars(tcx.hir().local_def_id_to_hir_id(mir_def_id)) {
932         let ty::BoundVariableKind::Region(bound_region) = bound_var else { continue; };
933         let liberated_region = ty::Region::new_free(tcx, mir_def_id.to_def_id(), bound_region);
934         f(liberated_region);
935     }
936 }
937