Skip to content

Commit 54fac38

Browse files
committed
don't always cache bound lts
1 parent cc013e0 commit 54fac38

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

compiler/rustc_middle/src/ty/fold.rs

+18-9
Original file line numberDiff line numberDiff line change
@@ -753,19 +753,29 @@ impl<'tcx> TyCtxt<'tcx> {
753753
T: TypeFoldable<'tcx>,
754754
{
755755
let mut region_map = BTreeMap::new();
756-
let mut real_fld_r =
757-
|br: ty::BoundRegion| *region_map.entry(br).or_insert_with(|| fld_r(br));
756+
let real_fld_r = |br: ty::BoundRegion| *region_map.entry(br).or_insert_with(|| fld_r(br));
757+
let value = self.replace_late_bound_regions_uncached(value, real_fld_r);
758+
(value, region_map)
759+
}
760+
761+
pub fn replace_late_bound_regions_uncached<T, F>(
762+
self,
763+
value: Binder<'tcx, T>,
764+
mut fld_r: F,
765+
) -> T
766+
where
767+
F: FnMut(ty::BoundRegion) -> ty::Region<'tcx>,
768+
T: TypeFoldable<'tcx>,
769+
{
758770
let mut fld_t = |b| bug!("unexpected bound ty in binder: {b:?}");
759771
let mut fld_c = |b, ty| bug!("unexpected bound ct in binder: {b:?} {ty}");
760-
761772
let value = value.skip_binder();
762-
let value = if !value.has_escaping_bound_vars() {
773+
if !value.has_escaping_bound_vars() {
763774
value
764775
} else {
765-
let mut replacer = BoundVarReplacer::new(self, &mut real_fld_r, &mut fld_t, &mut fld_c);
776+
let mut replacer = BoundVarReplacer::new(self, &mut fld_r, &mut fld_t, &mut fld_c);
766777
value.fold_with(&mut replacer)
767-
};
768-
(value, region_map)
778+
}
769779
}
770780

771781
/// Replaces all escaping bound vars. The `fld_r` closure replaces escaping
@@ -821,13 +831,12 @@ impl<'tcx> TyCtxt<'tcx> {
821831
where
822832
T: TypeFoldable<'tcx>,
823833
{
824-
self.replace_late_bound_regions(value, |br| {
834+
self.replace_late_bound_regions_uncached(value, |br| {
825835
self.mk_region(ty::ReFree(ty::FreeRegion {
826836
scope: all_outlive_scope,
827837
bound_region: br.kind,
828838
}))
829839
})
830-
.0
831840
}
832841

833842
pub fn shift_bound_var_indices<T>(self, bound_vars: usize, value: T) -> T

compiler/rustc_typeck/src/collect.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -449,17 +449,18 @@ impl<'tcx> AstConv<'tcx> for ItemCtxt<'tcx> {
449449
format!(
450450
"{}::",
451451
// Replace the existing lifetimes with a new named lifetime.
452-
self.tcx
453-
.replace_late_bound_regions(poly_trait_ref, |_| {
452+
self.tcx.replace_late_bound_regions_uncached(
453+
poly_trait_ref,
454+
|_| {
454455
self.tcx.mk_region(ty::ReEarlyBound(
455456
ty::EarlyBoundRegion {
456457
def_id: item_def_id,
457458
index: 0,
458459
name: Symbol::intern(&lt_name),
459460
},
460461
))
461-
})
462-
.0,
462+
}
463+
),
463464
),
464465
),
465466
];

0 commit comments

Comments
 (0)