@@ -251,13 +251,9 @@ impl<'tcx> UniversalRegions<'tcx> {
251
251
region_mapping. push ( fr) ;
252
252
} ) ;
253
253
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
- ) ;
254
+ for_each_late_bound_region_in_recursive_scope ( tcx, tcx. local_parent ( closure_def_id) , |r| {
255
+ region_mapping. push ( r) ;
256
+ } ) ;
261
257
262
258
assert_eq ! (
263
259
region_mapping. len( ) ,
@@ -345,7 +341,7 @@ impl<'tcx> UniversalRegions<'tcx> {
345
341
// tests, and the resulting print-outs include def-ids
346
342
// and other things that are not stable across tests!
347
343
// So we just include the region-vid. Annoying.
348
- for_each_late_bound_region_in_scope ( tcx, def_id. expect_local ( ) , |r| {
344
+ for_each_late_bound_region_in_recursive_scope ( tcx, def_id. expect_local ( ) , |r| {
349
345
err. note ( & format ! ( "late-bound region is {:?}" , self . to_region_vid( r) ) ) ;
350
346
} ) ;
351
347
}
@@ -359,7 +355,7 @@ impl<'tcx> UniversalRegions<'tcx> {
359
355
// FIXME: As above, we'd like to print out the region
360
356
// `r` but doing so is not stable across architectures
361
357
// and so forth.
362
- for_each_late_bound_region_in_scope ( tcx, def_id. expect_local ( ) , |r| {
358
+ for_each_late_bound_region_in_recursive_scope ( tcx, def_id. expect_local ( ) , |r| {
363
359
err. note ( & format ! ( "late-bound region is {:?}" , self . to_region_vid( r) ) ) ;
364
360
} ) ;
365
361
}
@@ -430,10 +426,19 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> {
430
426
// fn foo<'a>() {
431
427
// let c = || { let x: &'a u32 = ...; }
432
428
// }
433
- self . infcx . replace_late_bound_regions_with_nll_infer_vars (
429
+ for_each_late_bound_region_in_recursive_scope (
430
+ self . infcx . tcx ,
434
431
self . infcx . tcx . local_parent ( self . mir_def . did ) ,
435
- & mut indices,
432
+ |r| {
433
+ debug ! ( ?r) ;
434
+ if !indices. indices . contains_key ( & r) {
435
+ let region_vid = self . infcx . next_nll_region_var ( FR ) ;
436
+ debug ! ( ?region_vid) ;
437
+ indices. insert_late_bound_region ( r, region_vid. to_region_vid ( ) ) ;
438
+ }
439
+ } ,
436
440
) ;
441
+
437
442
// Any regions created during the execution of `defining_ty` or during the above
438
443
// late-bound region replacement are all considered 'extern' regions
439
444
self . infcx . num_region_vars ( )
@@ -452,7 +457,14 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> {
452
457
) ;
453
458
// Converse of above, if this is a function/closure then the late-bound regions declared on its
454
459
// signature are local.
455
- self . infcx . replace_late_bound_regions_with_nll_infer_vars ( self . mir_def . did , & mut indices) ;
460
+ for_each_late_bound_region_in_item ( self . infcx . tcx , self . mir_def . did , |r| {
461
+ debug ! ( ?r) ;
462
+ if !indices. indices . contains_key ( & r) {
463
+ let region_vid = self . infcx . next_nll_region_var ( FR ) ;
464
+ debug ! ( ?region_vid) ;
465
+ indices. insert_late_bound_region ( r, region_vid. to_region_vid ( ) ) ;
466
+ }
467
+ } ) ;
456
468
457
469
let ( unnormalized_output_ty, mut unnormalized_input_tys) =
458
470
inputs_and_output. split_last ( ) . unwrap ( ) ;
@@ -695,7 +707,13 @@ trait InferCtxtExt<'tcx> {
695
707
where
696
708
T : TypeFoldable < ' tcx > ;
697
709
698
- fn replace_late_bound_regions_with_nll_infer_vars (
710
+ fn replace_late_bound_regions_with_nll_infer_vars_in_recursive_scope (
711
+ & self ,
712
+ mir_def_id : LocalDefId ,
713
+ indices : & mut UniversalRegionIndices < ' tcx > ,
714
+ ) ;
715
+
716
+ fn replace_late_bound_regions_with_nll_infer_vars_in_item (
699
717
& self ,
700
718
mir_def_id : LocalDefId ,
701
719
indices : & mut UniversalRegionIndices < ' tcx > ,
@@ -749,12 +767,28 @@ impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> {
749
767
/// set of late-bound regions and checks for any that we have not yet seen, adding them to the
750
768
/// inputs vector.
751
769
#[ instrument( skip( self , indices) ) ]
752
- fn replace_late_bound_regions_with_nll_infer_vars (
770
+ fn replace_late_bound_regions_with_nll_infer_vars_in_recursive_scope (
753
771
& self ,
754
772
mir_def_id : LocalDefId ,
755
773
indices : & mut UniversalRegionIndices < ' tcx > ,
756
774
) {
757
- for_each_late_bound_region_in_scope ( self . tcx , mir_def_id, |r| {
775
+ for_each_late_bound_region_in_recursive_scope ( self . tcx , mir_def_id, |r| {
776
+ debug ! ( ?r) ;
777
+ if !indices. indices . contains_key ( & r) {
778
+ let region_vid = self . next_nll_region_var ( FR ) ;
779
+ debug ! ( ?region_vid) ;
780
+ indices. insert_late_bound_region ( r, region_vid. to_region_vid ( ) ) ;
781
+ }
782
+ } ) ;
783
+ }
784
+
785
+ #[ instrument( skip( self , indices) ) ]
786
+ fn replace_late_bound_regions_with_nll_infer_vars_in_item (
787
+ & self ,
788
+ mir_def_id : LocalDefId ,
789
+ indices : & mut UniversalRegionIndices < ' tcx > ,
790
+ ) {
791
+ for_each_late_bound_region_in_item ( self . tcx , mir_def_id, |r| {
758
792
debug ! ( ?r) ;
759
793
if !indices. indices . contains_key ( & r) {
760
794
let region_vid = self . next_nll_region_var ( FR ) ;
@@ -805,10 +839,10 @@ impl<'tcx> UniversalRegionIndices<'tcx> {
805
839
}
806
840
}
807
841
808
- /// Iterates over the late-bound regions defined on fn_def_id and all of its
842
+ /// Iterates over the late-bound regions defined on `mir_def_id` and all of its
809
843
/// parents, up to the typeck root, and invokes `f` with the liberated form
810
844
/// of each one.
811
- fn for_each_late_bound_region_in_scope < ' tcx > (
845
+ fn for_each_late_bound_region_in_recursive_scope < ' tcx > (
812
846
tcx : TyCtxt < ' tcx > ,
813
847
mut mir_def_id : LocalDefId ,
814
848
mut f : impl FnMut ( ty:: Region < ' tcx > ) ,
@@ -817,14 +851,7 @@ fn for_each_late_bound_region_in_scope<'tcx>(
817
851
818
852
// Walk up the tree, collecting late-bound regions until we hit the typeck root
819
853
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
- }
854
+ for_each_late_bound_region_in_item ( tcx, mir_def_id, & mut f) ;
828
855
829
856
if mir_def_id. to_def_id ( ) == typeck_root_def_id {
830
857
break ;
@@ -833,3 +860,19 @@ fn for_each_late_bound_region_in_scope<'tcx>(
833
860
}
834
861
}
835
862
}
863
+
864
+ /// Iterates over the late-bound regions defined on `mir_def_id` and all of its
865
+ /// parents, up to the typeck root, and invokes `f` with the liberated form
866
+ /// of each one.
867
+ fn for_each_late_bound_region_in_item < ' tcx > (
868
+ tcx : TyCtxt < ' tcx > ,
869
+ mir_def_id : LocalDefId ,
870
+ mut f : impl FnMut ( ty:: Region < ' tcx > ) ,
871
+ ) {
872
+ for bound_var in tcx. late_bound_vars ( tcx. hir ( ) . local_def_id_to_hir_id ( mir_def_id) ) {
873
+ let ty:: BoundVariableKind :: Region ( bound_region) = bound_var else { continue ; } ;
874
+ let liberated_region = tcx
875
+ . mk_region ( ty:: ReFree ( ty:: FreeRegion { scope : mir_def_id. to_def_id ( ) , bound_region } ) ) ;
876
+ f ( liberated_region) ;
877
+ }
878
+ }
0 commit comments