Skip to content

Commit 543ca7d

Browse files
committed
replace bound vars: make caching explicit
1 parent 434c7da commit 543ca7d

File tree

4 files changed

+40
-32
lines changed

4 files changed

+40
-32
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,6 @@ where
8686
c => bug!("{:?} is a const but value is {:?}", bound_ct, c),
8787
};
8888

89-
tcx.replace_escaping_bound_vars(value, fld_r, fld_t, fld_c)
89+
tcx.replace_escaping_bound_vars_uncached(value, fld_r, fld_t, fld_c)
9090
}
9191
}

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

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
44
use super::combine::CombineFields;
55
use super::{HigherRankedType, InferCtxt};
6-
76
use crate::infer::CombinedSnapshot;
87
use rustc_middle::ty::relate::{Relate, RelateResult, TypeRelation};
98
use rustc_middle::ty::{self, Binder, TypeFoldable};
9+
use std::cell::Cell;
1010

1111
impl<'a, 'tcx> CombineFields<'a, 'tcx> {
1212
#[instrument(skip(self), level = "debug")]
@@ -65,6 +65,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
6565
/// For more details visit the relevant sections of the [rustc dev guide].
6666
///
6767
/// [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/traits/hrtb.html
68+
#[instrument(level = "debug", skip(self))]
6869
pub fn replace_bound_vars_with_placeholders<T>(&self, binder: ty::Binder<'tcx, T>) -> T
6970
where
7071
T: TypeFoldable<'tcx>,
@@ -76,21 +77,25 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
7677
// (i.e., if there are no placeholders).
7778
let next_universe = self.universe().next_universe();
7879

80+
let replaced_bound_var = Cell::new(false);
7981
let fld_r = |br: ty::BoundRegion| {
82+
replaced_bound_var.set(true);
8083
self.tcx.mk_region(ty::RePlaceholder(ty::PlaceholderRegion {
8184
universe: next_universe,
8285
name: br.kind,
8386
}))
8487
};
8588

8689
let fld_t = |bound_ty: ty::BoundTy| {
90+
replaced_bound_var.set(true);
8791
self.tcx.mk_ty(ty::Placeholder(ty::PlaceholderType {
8892
universe: next_universe,
8993
name: bound_ty.var,
9094
}))
9195
};
9296

9397
let fld_c = |bound_var: ty::BoundVar, ty| {
98+
replaced_bound_var.set(true);
9499
self.tcx.mk_const(ty::ConstS {
95100
val: ty::ConstKind::Placeholder(ty::PlaceholderConst {
96101
universe: next_universe,
@@ -100,22 +105,16 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
100105
})
101106
};
102107

103-
let (result, map) = self.tcx.replace_bound_vars(binder, fld_r, fld_t, fld_c);
108+
let result = self.tcx.replace_bound_vars_uncached(binder, fld_r, fld_t, fld_c);
104109

105110
// If there were higher-ranked regions to replace, then actually create
106111
// the next universe (this avoids needlessly creating universes).
107-
if !map.is_empty() {
112+
if replaced_bound_var.get() {
108113
let n_u = self.create_next_universe();
109114
assert_eq!(n_u, next_universe);
110115
}
111116

112-
debug!(
113-
"replace_bound_vars_with_placeholders(\
114-
next_universe={:?}, \
115-
result={:?}, \
116-
map={:?})",
117-
next_universe, result, map,
118-
);
117+
debug!(?next_universe, ?result);
119118

120119
result
121120
}

compiler/rustc_infer/src/infer/mod.rs

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1528,21 +1528,33 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
15281528
where
15291529
T: TypeFoldable<'tcx>,
15301530
{
1531-
let fld_r =
1532-
|br: ty::BoundRegion| self.next_region_var(LateBoundRegion(span, br.kind, lbrct));
1533-
let fld_t = |_| {
1534-
self.next_ty_var(TypeVariableOrigin {
1535-
kind: TypeVariableOriginKind::MiscVariable,
1536-
span,
1531+
let mut region_map = BTreeMap::new();
1532+
let fld_r = |br: ty::BoundRegion| {
1533+
*region_map
1534+
.entry(br)
1535+
.or_insert_with(|| self.next_region_var(LateBoundRegion(span, br.kind, lbrct)))
1536+
};
1537+
1538+
let mut ty_map = BTreeMap::new();
1539+
let fld_t = |bt: ty::BoundTy| {
1540+
*ty_map.entry(bt).or_insert_with(|| {
1541+
self.next_ty_var(TypeVariableOrigin {
1542+
kind: TypeVariableOriginKind::MiscVariable,
1543+
span,
1544+
})
15371545
})
15381546
};
1539-
let fld_c = |_, ty| {
1540-
self.next_const_var(
1541-
ty,
1542-
ConstVariableOrigin { kind: ConstVariableOriginKind::MiscVariable, span },
1543-
)
1547+
let mut ct_map = BTreeMap::new();
1548+
let fld_c = |bc: ty::BoundVar, ty| {
1549+
*ct_map.entry(bc).or_insert_with(|| {
1550+
self.next_const_var(
1551+
ty,
1552+
ConstVariableOrigin { kind: ConstVariableOriginKind::MiscVariable, span },
1553+
)
1554+
})
15441555
};
1545-
self.tcx.replace_bound_vars(value, fld_r, fld_t, fld_c)
1556+
let result = self.tcx.replace_bound_vars_uncached(value, fld_r, fld_t, fld_c);
1557+
(result, region_map)
15461558
}
15471559

15481560
/// See the [`region_constraints::RegionConstraintCollector::verify_generic_bound`] method.

compiler/rustc_middle/src/ty/fold.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -771,7 +771,7 @@ impl<'tcx> TyCtxt<'tcx> {
771771
/// Replaces all escaping bound vars. The `fld_r` closure replaces escaping
772772
/// bound regions; the `fld_t` closure replaces escaping bound types and the `fld_c`
773773
/// closure replaces escaping bound consts.
774-
pub fn replace_escaping_bound_vars<T, F, G, H>(
774+
pub fn replace_escaping_bound_vars_uncached<T, F, G, H>(
775775
self,
776776
value: T,
777777
mut fld_r: F,
@@ -795,23 +795,20 @@ impl<'tcx> TyCtxt<'tcx> {
795795
/// Replaces all types or regions bound by the given `Binder`. The `fld_r`
796796
/// closure replaces bound regions, the `fld_t` closure replaces bound
797797
/// types, and `fld_c` replaces bound constants.
798-
pub fn replace_bound_vars<T, F, G, H>(
798+
pub fn replace_bound_vars_uncached<T, F, G, H>(
799799
self,
800800
value: Binder<'tcx, T>,
801-
mut fld_r: F,
801+
fld_r: F,
802802
fld_t: G,
803803
fld_c: H,
804-
) -> (T, BTreeMap<ty::BoundRegion, ty::Region<'tcx>>)
804+
) -> T
805805
where
806806
F: FnMut(ty::BoundRegion) -> ty::Region<'tcx>,
807807
G: FnMut(ty::BoundTy) -> Ty<'tcx>,
808808
H: FnMut(ty::BoundVar, Ty<'tcx>) -> ty::Const<'tcx>,
809809
T: TypeFoldable<'tcx>,
810810
{
811-
let mut region_map = BTreeMap::new();
812-
let real_fld_r = |br: ty::BoundRegion| *region_map.entry(br).or_insert_with(|| fld_r(br));
813-
let value = self.replace_escaping_bound_vars(value.skip_binder(), real_fld_r, fld_t, fld_c);
814-
(value, region_map)
811+
self.replace_escaping_bound_vars_uncached(value.skip_binder(), fld_r, fld_t, fld_c)
815812
}
816813

817814
/// Replaces any late-bound regions bound in `value` with
@@ -837,7 +834,7 @@ impl<'tcx> TyCtxt<'tcx> {
837834
where
838835
T: TypeFoldable<'tcx>,
839836
{
840-
self.replace_escaping_bound_vars(
837+
self.replace_escaping_bound_vars_uncached(
841838
value,
842839
|r| {
843840
self.mk_region(ty::ReLateBound(

0 commit comments

Comments
 (0)