Skip to content

Commit f3dfa52

Browse files
committed
resolve to universal regions when possible
1 parent 13471d3 commit f3dfa52

File tree

4 files changed

+19
-38
lines changed

4 files changed

+19
-38
lines changed

compiler/rustc_infer/src/infer/canonical/canonicalizer.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -352,19 +352,17 @@ impl<'cx, 'tcx> TypeFolder<TyCtxt<'tcx>> for Canonicalizer<'cx, 'tcx> {
352352
}
353353

354354
ty::ReVar(vid) => {
355-
let resolved_vid = self
355+
let resolved = self
356356
.infcx
357357
.inner
358358
.borrow_mut()
359359
.unwrap_region_constraints()
360-
.opportunistic_resolve_var(vid);
360+
.opportunistic_resolve_var(self.tcx, vid);
361361
debug!(
362-
"canonical: region var found with vid {:?}, \
363-
opportunistically resolved to {:?}",
364-
vid, resolved_vid
362+
"canonical: region var found with vid {vid:?}, \
363+
opportunistically resolved to {resolved:?}",
365364
);
366-
let r = self.tcx.mk_re_var(resolved_vid);
367-
self.canonicalize_mode.canonicalize_free_region(self, r)
365+
self.canonicalize_mode.canonicalize_free_region(self, resolved)
368366
}
369367

370368
ty::ReStatic

compiler/rustc_infer/src/infer/region_constraints/mod.rs

+6-20
Original file line numberDiff line numberDiff line change
@@ -633,29 +633,15 @@ impl<'tcx> RegionConstraintCollector<'_, 'tcx> {
633633
}
634634
}
635635

636-
/// Resolves the passed RegionVid to the root RegionVid in the unification table
637-
pub(super) fn opportunistic_resolve_var(&mut self, rid: ty::RegionVid) -> ty::RegionVid {
638-
self.unification_table().find(rid).vid
639-
}
640-
641-
/// If the Region is a `ReVar`, then resolves it either to the root value in
642-
/// the unification table, if it exists, or to the root `ReVar` in the table.
643-
/// If the Region is not a `ReVar`, just returns the Region itself.
644-
pub fn opportunistic_resolve_region(
636+
/// Resolves a region var to its value in the unification table, if it exists.
637+
/// Otherwise, it is resolved to the root `ReVar` in the table.
638+
pub fn opportunistic_resolve_var(
645639
&mut self,
646640
tcx: TyCtxt<'tcx>,
647-
region: ty::Region<'tcx>,
641+
vid: ty::RegionVid,
648642
) -> ty::Region<'tcx> {
649-
match *region {
650-
ty::ReVar(rid) => {
651-
let unified_region = self.unification_table().probe_value(rid);
652-
unified_region.0.unwrap_or_else(|| {
653-
let root = self.unification_table().find(rid).vid;
654-
tcx.mk_re_var(root)
655-
})
656-
}
657-
_ => region,
658-
}
643+
let root_vid = self.unification_table().find(vid).vid;
644+
self.unification_table().probe_value(root_vid).0.unwrap_or_else(|| tcx.mk_re_var(root_vid))
659645
}
660646

661647
fn combine_map(&mut self, t: CombineMapType) -> &mut CombineMap<'tcx> {

compiler/rustc_infer/src/infer/resolve.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,12 @@ impl<'a, 'tcx> TypeFolder<TyCtxt<'tcx>> for OpportunisticRegionResolver<'a, 'tcx
8585

8686
fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
8787
match *r {
88-
ty::ReVar(rid) => {
89-
let resolved = self
90-
.infcx
91-
.inner
92-
.borrow_mut()
93-
.unwrap_region_constraints()
94-
.opportunistic_resolve_var(rid);
95-
TypeFolder::interner(self).mk_re_var(resolved)
96-
}
88+
ty::ReVar(vid) => self
89+
.infcx
90+
.inner
91+
.borrow_mut()
92+
.unwrap_region_constraints()
93+
.opportunistic_resolve_var(TypeFolder::interner(self), vid),
9794
_ => r,
9895
}
9996
}

compiler/rustc_trait_selection/src/traits/project.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -871,12 +871,12 @@ impl<'tcx> TypeFolder<TyCtxt<'tcx>> for PlaceholderReplacer<'_, 'tcx> {
871871

872872
fn fold_region(&mut self, r0: ty::Region<'tcx>) -> ty::Region<'tcx> {
873873
let r1 = match *r0 {
874-
ty::ReVar(_) => self
874+
ty::ReVar(vid) => self
875875
.infcx
876876
.inner
877877
.borrow_mut()
878878
.unwrap_region_constraints()
879-
.opportunistic_resolve_region(self.infcx.tcx, r0),
879+
.opportunistic_resolve_var(self.infcx.tcx, vid),
880880
_ => r0,
881881
};
882882

0 commit comments

Comments
 (0)