@@ -243,17 +243,21 @@ impl<'tcx> UniversalRegions<'tcx> {
243
243
tcx : TyCtxt < ' tcx > ,
244
244
closure_substs : SubstsRef < ' tcx > ,
245
245
expected_num_vars : usize ,
246
- typeck_root_def_id : DefId ,
246
+ closure_def_id : LocalDefId ,
247
247
) -> IndexVec < RegionVid , ty:: Region < ' tcx > > {
248
248
let mut region_mapping = IndexVec :: with_capacity ( expected_num_vars) ;
249
249
region_mapping. push ( tcx. lifetimes . re_static ) ;
250
250
tcx. for_each_free_region ( & closure_substs, |fr| {
251
251
region_mapping. push ( fr) ;
252
252
} ) ;
253
253
254
- for_each_late_bound_region_defined_on ( tcx, typeck_root_def_id, |r| {
255
- region_mapping. push ( r) ;
256
- } ) ;
254
+ for_each_late_bound_region_in_scope (
255
+ tcx,
256
+ tcx. local_parent ( closure_def_id) ,
257
+ |r| {
258
+ region_mapping. push ( r) ;
259
+ } ,
260
+ ) ;
257
261
258
262
assert_eq ! (
259
263
region_mapping. len( ) ,
@@ -341,9 +345,8 @@ impl<'tcx> UniversalRegions<'tcx> {
341
345
// tests, and the resulting print-outs include def-ids
342
346
// and other things that are not stable across tests!
343
347
// So we just include the region-vid. Annoying.
344
- let typeck_root_def_id = tcx. typeck_root_def_id ( def_id) ;
345
- for_each_late_bound_region_defined_on ( tcx, typeck_root_def_id, |r| {
346
- err. note ( & format ! ( "late-bound region is {:?}" , self . to_region_vid( r) , ) ) ;
348
+ for_each_late_bound_region_in_scope ( tcx, def_id. expect_local ( ) , |r| {
349
+ err. note ( & format ! ( "late-bound region is {:?}" , self . to_region_vid( r) ) ) ;
347
350
} ) ;
348
351
}
349
352
DefiningTy :: Generator ( def_id, substs, _) => {
@@ -356,9 +359,8 @@ impl<'tcx> UniversalRegions<'tcx> {
356
359
// FIXME: As above, we'd like to print out the region
357
360
// `r` but doing so is not stable across architectures
358
361
// and so forth.
359
- let typeck_root_def_id = tcx. typeck_root_def_id ( def_id) ;
360
- for_each_late_bound_region_defined_on ( tcx, typeck_root_def_id, |r| {
361
- err. note ( & format ! ( "late-bound region is {:?}" , self . to_region_vid( r) , ) ) ;
362
+ for_each_late_bound_region_in_scope ( tcx, def_id. expect_local ( ) , |r| {
363
+ err. note ( & format ! ( "late-bound region is {:?}" , self . to_region_vid( r) ) ) ;
362
364
} ) ;
363
365
}
364
366
DefiningTy :: FnDef ( def_id, substs) => {
@@ -749,28 +751,17 @@ impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> {
749
751
#[ instrument( skip( self , indices) ) ]
750
752
fn replace_late_bound_regions_with_nll_infer_vars (
751
753
& self ,
752
- mut mir_def_id : LocalDefId ,
754
+ mir_def_id : LocalDefId ,
753
755
indices : & mut UniversalRegionIndices < ' tcx > ,
754
756
) {
755
- let typeck_root_def_id = self . tcx . typeck_root_def_id ( mir_def_id. to_def_id ( ) ) ;
756
-
757
- // Walk up the tree, collecting late-bound regions until we hit the typeck root
758
- loop {
759
- for_each_late_bound_region_defined_on ( self . tcx , mir_def_id. to_def_id ( ) , |r| {
760
- debug ! ( ?r) ;
761
- if !indices. indices . contains_key ( & r) {
762
- let region_vid = self . next_nll_region_var ( FR ) ;
763
- debug ! ( ?region_vid) ;
764
- indices. insert_late_bound_region ( r, region_vid. to_region_vid ( ) ) ;
765
- }
766
- } ) ;
767
-
768
- if mir_def_id. to_def_id ( ) == typeck_root_def_id {
769
- break ;
770
- } else {
771
- mir_def_id = self . tcx . parent ( mir_def_id. to_def_id ( ) ) . expect_local ( ) ;
757
+ for_each_late_bound_region_in_scope ( self . tcx , mir_def_id, |r| {
758
+ debug ! ( ?r) ;
759
+ if !indices. indices . contains_key ( & r) {
760
+ let region_vid = self . next_nll_region_var ( FR ) ;
761
+ debug ! ( ?region_vid) ;
762
+ indices. insert_late_bound_region ( r, region_vid. to_region_vid ( ) ) ;
772
763
}
773
- }
764
+ } ) ;
774
765
}
775
766
}
776
767
@@ -814,18 +805,31 @@ impl<'tcx> UniversalRegionIndices<'tcx> {
814
805
}
815
806
}
816
807
817
- /// Iterates over the late-bound regions defined on fn_def_id and
818
- /// invokes `f` with the liberated form of each one.
819
- fn for_each_late_bound_region_defined_on < ' tcx > (
808
+ /// Iterates over the late-bound regions defined on fn_def_id and all of its
809
+ /// parents, up to the typeck root, and invokes `f` with the liberated form
810
+ /// of each one.
811
+ fn for_each_late_bound_region_in_scope < ' tcx > (
820
812
tcx : TyCtxt < ' tcx > ,
821
- fn_def_id : DefId ,
813
+ mut mir_def_id : LocalDefId ,
822
814
mut f : impl FnMut ( ty:: Region < ' tcx > ) ,
823
815
) {
824
- for bound_var in tcx. late_bound_vars ( tcx. hir ( ) . local_def_id_to_hir_id ( fn_def_id. expect_local ( ) ) )
825
- {
826
- let ty:: BoundVariableKind :: Region ( bound_region) = bound_var else { continue ; } ;
827
- let liberated_region =
828
- tcx. mk_region ( ty:: ReFree ( ty:: FreeRegion { scope : fn_def_id, bound_region } ) ) ;
829
- f ( liberated_region) ;
816
+ let typeck_root_def_id = tcx. typeck_root_def_id ( mir_def_id. to_def_id ( ) ) ;
817
+
818
+ // Walk up the tree, collecting late-bound regions until we hit the typeck root
819
+ loop {
820
+ for bound_var in tcx. late_bound_vars ( tcx. hir ( ) . local_def_id_to_hir_id ( mir_def_id) ) {
821
+ let ty:: BoundVariableKind :: Region ( bound_region) = bound_var else { continue ; } ;
822
+ let liberated_region = tcx. mk_region ( ty:: ReFree ( ty:: FreeRegion {
823
+ scope : mir_def_id. to_def_id ( ) ,
824
+ bound_region,
825
+ } ) ) ;
826
+ f ( liberated_region) ;
827
+ }
828
+
829
+ if mir_def_id. to_def_id ( ) == typeck_root_def_id {
830
+ break ;
831
+ } else {
832
+ mir_def_id = tcx. local_parent ( mir_def_id) ;
833
+ }
830
834
}
831
835
}
0 commit comments