resolve to universal regions when possible

This commit is contained in:
Ali MJ Al-Nasrawy 2023-02-16 14:40:53 +03:00
parent 13471d3b20
commit f3dfa52fd1
4 changed files with 19 additions and 38 deletions

View File

@ -352,19 +352,17 @@ fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
} }
ty::ReVar(vid) => { ty::ReVar(vid) => {
let resolved_vid = self let resolved = self
.infcx .infcx
.inner .inner
.borrow_mut() .borrow_mut()
.unwrap_region_constraints() .unwrap_region_constraints()
.opportunistic_resolve_var(vid); .opportunistic_resolve_var(self.tcx, vid);
debug!( debug!(
"canonical: region var found with vid {:?}, \ "canonical: region var found with vid {vid:?}, \
opportunistically resolved to {:?}", opportunistically resolved to {resolved:?}",
vid, resolved_vid
); );
let r = self.tcx.mk_re_var(resolved_vid); self.canonicalize_mode.canonicalize_free_region(self, resolved)
self.canonicalize_mode.canonicalize_free_region(self, r)
} }
ty::ReStatic ty::ReStatic

View File

@ -633,29 +633,15 @@ pub(super) fn glb_regions(
} }
} }
/// Resolves the passed RegionVid to the root RegionVid in the unification table /// Resolves a region var to its value in the unification table, if it exists.
pub(super) fn opportunistic_resolve_var(&mut self, rid: ty::RegionVid) -> ty::RegionVid { /// Otherwise, it is resolved to the root `ReVar` in the table.
self.unification_table().find(rid).vid pub fn opportunistic_resolve_var(
}
/// If the Region is a `ReVar`, then resolves it either to the root value in
/// the unification table, if it exists, or to the root `ReVar` in the table.
/// If the Region is not a `ReVar`, just returns the Region itself.
pub fn opportunistic_resolve_region(
&mut self, &mut self,
tcx: TyCtxt<'tcx>, tcx: TyCtxt<'tcx>,
region: ty::Region<'tcx>, vid: ty::RegionVid,
) -> ty::Region<'tcx> { ) -> ty::Region<'tcx> {
match *region { let root_vid = self.unification_table().find(vid).vid;
ty::ReVar(rid) => { self.unification_table().probe_value(root_vid).0.unwrap_or_else(|| tcx.mk_re_var(root_vid))
let unified_region = self.unification_table().probe_value(rid);
unified_region.0.unwrap_or_else(|| {
let root = self.unification_table().find(rid).vid;
tcx.mk_re_var(root)
})
}
_ => region,
}
} }
fn combine_map(&mut self, t: CombineMapType) -> &mut CombineMap<'tcx> { fn combine_map(&mut self, t: CombineMapType) -> &mut CombineMap<'tcx> {

View File

@ -85,15 +85,12 @@ fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> { fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
match *r { match *r {
ty::ReVar(rid) => { ty::ReVar(vid) => self
let resolved = self .infcx
.infcx .inner
.inner .borrow_mut()
.borrow_mut() .unwrap_region_constraints()
.unwrap_region_constraints() .opportunistic_resolve_var(TypeFolder::interner(self), vid),
.opportunistic_resolve_var(rid);
TypeFolder::interner(self).mk_re_var(resolved)
}
_ => r, _ => r,
} }
} }

View File

@ -871,12 +871,12 @@ fn fold_binder<T: TypeFoldable<TyCtxt<'tcx>>>(
fn fold_region(&mut self, r0: ty::Region<'tcx>) -> ty::Region<'tcx> { fn fold_region(&mut self, r0: ty::Region<'tcx>) -> ty::Region<'tcx> {
let r1 = match *r0 { let r1 = match *r0 {
ty::ReVar(_) => self ty::ReVar(vid) => self
.infcx .infcx
.inner .inner
.borrow_mut() .borrow_mut()
.unwrap_region_constraints() .unwrap_region_constraints()
.opportunistic_resolve_region(self.infcx.tcx, r0), .opportunistic_resolve_var(self.infcx.tcx, vid),
_ => r0, _ => r0,
}; };