Skip to content

Commit 4427af0

Browse files
Make external/local late-bound region registration more explicit
1 parent f27bdf1 commit 4427af0

File tree

1 file changed

+68
-25
lines changed

1 file changed

+68
-25
lines changed

compiler/rustc_borrowck/src/universal_regions.rs

+68-25
Original file line numberDiff line numberDiff line change
@@ -251,13 +251,9 @@ impl<'tcx> UniversalRegions<'tcx> {
251251
region_mapping.push(fr);
252252
});
253253

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+
});
261257

262258
assert_eq!(
263259
region_mapping.len(),
@@ -345,7 +341,7 @@ impl<'tcx> UniversalRegions<'tcx> {
345341
// tests, and the resulting print-outs include def-ids
346342
// and other things that are not stable across tests!
347343
// 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| {
349345
err.note(&format!("late-bound region is {:?}", self.to_region_vid(r)));
350346
});
351347
}
@@ -359,7 +355,7 @@ impl<'tcx> UniversalRegions<'tcx> {
359355
// FIXME: As above, we'd like to print out the region
360356
// `r` but doing so is not stable across architectures
361357
// 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| {
363359
err.note(&format!("late-bound region is {:?}", self.to_region_vid(r)));
364360
});
365361
}
@@ -430,10 +426,19 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> {
430426
// fn foo<'a>() {
431427
// let c = || { let x: &'a u32 = ...; }
432428
// }
433-
self.infcx.replace_late_bound_regions_with_nll_infer_vars(
429+
for_each_late_bound_region_in_recursive_scope(
430+
self.infcx.tcx,
434431
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+
},
436440
);
441+
437442
// Any regions created during the execution of `defining_ty` or during the above
438443
// late-bound region replacement are all considered 'extern' regions
439444
self.infcx.num_region_vars()
@@ -452,7 +457,14 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> {
452457
);
453458
// Converse of above, if this is a function/closure then the late-bound regions declared on its
454459
// 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+
});
456468

457469
let (unnormalized_output_ty, mut unnormalized_input_tys) =
458470
inputs_and_output.split_last().unwrap();
@@ -695,7 +707,13 @@ trait InferCtxtExt<'tcx> {
695707
where
696708
T: TypeFoldable<'tcx>;
697709

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(
699717
&self,
700718
mir_def_id: LocalDefId,
701719
indices: &mut UniversalRegionIndices<'tcx>,
@@ -749,12 +767,28 @@ impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> {
749767
/// set of late-bound regions and checks for any that we have not yet seen, adding them to the
750768
/// inputs vector.
751769
#[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(
753771
&self,
754772
mir_def_id: LocalDefId,
755773
indices: &mut UniversalRegionIndices<'tcx>,
756774
) {
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| {
758792
debug!(?r);
759793
if !indices.indices.contains_key(&r) {
760794
let region_vid = self.next_nll_region_var(FR);
@@ -805,10 +839,10 @@ impl<'tcx> UniversalRegionIndices<'tcx> {
805839
}
806840
}
807841

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
809843
/// parents, up to the typeck root, and invokes `f` with the liberated form
810844
/// of each one.
811-
fn for_each_late_bound_region_in_scope<'tcx>(
845+
fn for_each_late_bound_region_in_recursive_scope<'tcx>(
812846
tcx: TyCtxt<'tcx>,
813847
mut mir_def_id: LocalDefId,
814848
mut f: impl FnMut(ty::Region<'tcx>),
@@ -817,14 +851,7 @@ fn for_each_late_bound_region_in_scope<'tcx>(
817851

818852
// Walk up the tree, collecting late-bound regions until we hit the typeck root
819853
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);
828855

829856
if mir_def_id.to_def_id() == typeck_root_def_id {
830857
break;
@@ -833,3 +860,19 @@ fn for_each_late_bound_region_in_scope<'tcx>(
833860
}
834861
}
835862
}
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

Comments
 (0)