@@ -22,7 +22,9 @@ use rustc_hir::{BodyOwnerKind, HirId};
22
22
use rustc_index:: vec:: { Idx , IndexVec } ;
23
23
use rustc_infer:: infer:: { InferCtxt , NllRegionVariableOrigin } ;
24
24
use rustc_middle:: ty:: fold:: TypeFoldable ;
25
- use rustc_middle:: ty:: { self , InlineConstSubsts , InlineConstSubstsParts , RegionVid , Ty , TyCtxt } ;
25
+ use rustc_middle:: ty:: {
26
+ self , DefIdTree , InlineConstSubsts , InlineConstSubstsParts , RegionVid , Ty , TyCtxt ,
27
+ } ;
26
28
use rustc_middle:: ty:: { InternalSubsts , SubstsRef } ;
27
29
use std:: iter;
28
30
@@ -421,13 +423,15 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> {
421
423
first_extern_index
422
424
} else {
423
425
// If this is a closure, generator, or inline-const, then the late-bound regions from the enclosing
424
- // function are actually external regions to us. For example, here, 'a is not local
426
+ // function/closures are actually external regions to us. For example, here, 'a is not local
425
427
// to the closure c (although it is local to the fn foo):
426
428
// fn foo<'a>() {
427
429
// let c = || { let x: &'a u32 = ...; }
428
430
// }
429
- self . infcx
430
- . replace_late_bound_regions_with_nll_infer_vars ( self . mir_def . did , & mut indices) ;
431
+ self . infcx . replace_late_bound_regions_with_nll_infer_vars (
432
+ self . infcx . tcx . local_parent ( self . mir_def . did ) ,
433
+ & mut indices,
434
+ ) ;
431
435
// Any regions created during the execution of `defining_ty` or during the above
432
436
// late-bound region replacement are all considered 'extern' regions
433
437
self . infcx . num_region_vars ( )
@@ -444,12 +448,9 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> {
444
448
bound_inputs_and_output,
445
449
& mut indices,
446
450
) ;
447
- // Converse of above, if this is a function then the late-bound regions declared on its
448
- // signature are local to the fn.
449
- if self . mir_def . did . to_def_id ( ) == typeck_root_def_id {
450
- self . infcx
451
- . replace_late_bound_regions_with_nll_infer_vars ( self . mir_def . did , & mut indices) ;
452
- }
451
+ // Converse of above, if this is a function/closure then the late-bound regions declared on its
452
+ // signature are local.
453
+ self . infcx . replace_late_bound_regions_with_nll_infer_vars ( self . mir_def . did , & mut indices) ;
453
454
454
455
let ( unnormalized_output_ty, mut unnormalized_input_tys) =
455
456
inputs_and_output. split_last ( ) . unwrap ( ) ;
@@ -748,18 +749,28 @@ impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> {
748
749
#[ instrument( skip( self , indices) ) ]
749
750
fn replace_late_bound_regions_with_nll_infer_vars (
750
751
& self ,
751
- mir_def_id : LocalDefId ,
752
+ mut mir_def_id : LocalDefId ,
752
753
indices : & mut UniversalRegionIndices < ' tcx > ,
753
754
) {
754
755
let typeck_root_def_id = self . tcx . typeck_root_def_id ( mir_def_id. to_def_id ( ) ) ;
755
- for_each_late_bound_region_defined_on ( self . tcx , typeck_root_def_id, |r| {
756
- debug ! ( ?r) ;
757
- if !indices. indices . contains_key ( & r) {
758
- let region_vid = self . next_nll_region_var ( FR ) ;
759
- debug ! ( ?region_vid) ;
760
- indices. insert_late_bound_region ( r, region_vid. to_region_vid ( ) ) ;
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 ( ) ;
761
772
}
762
- } ) ;
773
+ }
763
774
}
764
775
}
765
776
@@ -810,14 +821,11 @@ fn for_each_late_bound_region_defined_on<'tcx>(
810
821
fn_def_id : DefId ,
811
822
mut f : impl FnMut ( ty:: Region < ' tcx > ) ,
812
823
) {
813
- if let Some ( late_bounds) = tcx. is_late_bound_map ( fn_def_id. expect_local ( ) ) {
814
- for & region_def_id in late_bounds. iter ( ) {
815
- let name = tcx. item_name ( region_def_id. to_def_id ( ) ) ;
816
- let liberated_region = tcx. mk_region ( ty:: ReFree ( ty:: FreeRegion {
817
- scope : fn_def_id,
818
- bound_region : ty:: BoundRegionKind :: BrNamed ( region_def_id. to_def_id ( ) , name) ,
819
- } ) ) ;
820
- f ( liberated_region) ;
821
- }
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) ;
822
830
}
823
831
}
0 commit comments