Skip to content

Commit aa11704

Browse files
committed
Add helper method for reusing an existing interned region
1 parent e9b0ce8 commit aa11704

File tree

3 files changed

+9
-8
lines changed

3 files changed

+9
-8
lines changed

src/librustc_infer/infer/canonical/canonicalizer.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -326,13 +326,7 @@ impl<'cx, 'tcx> TypeFolder<'tcx> for Canonicalizer<'cx, 'tcx> {
326326
opportunistically resolved to {:?}",
327327
vid, r
328328
);
329-
// micro-optimize -- avoid an interner look-up if the vid
330-
// hasn't changed.
331-
let r = if vid == resolved_vid {
332-
r
333-
} else {
334-
self.tcx.mk_region(ty::ReVar(resolved_vid))
335-
};
329+
let r = self.tcx.reuse_or_mk_region(r, ty::ReVar(resolved_vid));
336330
self.canonicalize_region_mode.canonicalize_free_region(self, r)
337331
}
338332

src/librustc_infer/infer/resolve.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ impl<'a, 'tcx> TypeFolder<'tcx> for OpportunisticRegionResolver<'a, 'tcx> {
8585
.borrow_mut()
8686
.unwrap_region_constraints()
8787
.opportunistic_resolve_var(rid);
88-
if resolved == rid { r } else { self.tcx().mk_region(ty::ReVar(resolved)) }
88+
self.tcx().reuse_or_mk_region(r, ty::ReVar(resolved))
8989
}
9090
_ => r,
9191
}

src/librustc_middle/ty/context.rs

+7
Original file line numberDiff line numberDiff line change
@@ -2081,6 +2081,13 @@ impl<'tcx> TyCtxt<'tcx> {
20812081
})
20822082
}
20832083

2084+
/// Same a `self.mk_region(kind)`, but avoids accessing the interners if
2085+
/// `*r == kind`.
2086+
#[inline]
2087+
pub fn reuse_or_mk_region(self, r: Region<'tcx>, kind: RegionKind) -> Region<'tcx> {
2088+
if *r == kind { r } else { self.mk_region(kind) }
2089+
}
2090+
20842091
#[allow(rustc::usage_of_ty_tykind)]
20852092
#[inline]
20862093
pub fn mk_ty(&self, st: TyKind<'tcx>) -> Ty<'tcx> {

0 commit comments

Comments
 (0)