replace bound vars: make caching explicit

This commit is contained in:
lcnr 2022-06-02 12:42:57 +02:00
parent 434c7da6ea
commit 543ca7d9e7
4 changed files with 40 additions and 32 deletions

View File

@ -86,6 +86,6 @@ where
c => bug!("{:?} is a const but value is {:?}", bound_ct, c),
};
tcx.replace_escaping_bound_vars(value, fld_r, fld_t, fld_c)
tcx.replace_escaping_bound_vars_uncached(value, fld_r, fld_t, fld_c)
}
}

View File

@ -3,10 +3,10 @@
use super::combine::CombineFields;
use super::{HigherRankedType, InferCtxt};
use crate::infer::CombinedSnapshot;
use rustc_middle::ty::relate::{Relate, RelateResult, TypeRelation};
use rustc_middle::ty::{self, Binder, TypeFoldable};
use std::cell::Cell;
impl<'a, 'tcx> CombineFields<'a, 'tcx> {
#[instrument(skip(self), level = "debug")]
@ -65,6 +65,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
/// For more details visit the relevant sections of the [rustc dev guide].
///
/// [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/traits/hrtb.html
#[instrument(level = "debug", skip(self))]
pub fn replace_bound_vars_with_placeholders<T>(&self, binder: ty::Binder<'tcx, T>) -> T
where
T: TypeFoldable<'tcx>,
@ -76,7 +77,9 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
// (i.e., if there are no placeholders).
let next_universe = self.universe().next_universe();
let replaced_bound_var = Cell::new(false);
let fld_r = |br: ty::BoundRegion| {
replaced_bound_var.set(true);
self.tcx.mk_region(ty::RePlaceholder(ty::PlaceholderRegion {
universe: next_universe,
name: br.kind,
@ -84,6 +87,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
};
let fld_t = |bound_ty: ty::BoundTy| {
replaced_bound_var.set(true);
self.tcx.mk_ty(ty::Placeholder(ty::PlaceholderType {
universe: next_universe,
name: bound_ty.var,
@ -91,6 +95,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
};
let fld_c = |bound_var: ty::BoundVar, ty| {
replaced_bound_var.set(true);
self.tcx.mk_const(ty::ConstS {
val: ty::ConstKind::Placeholder(ty::PlaceholderConst {
universe: next_universe,
@ -100,22 +105,16 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
})
};
let (result, map) = self.tcx.replace_bound_vars(binder, fld_r, fld_t, fld_c);
let result = self.tcx.replace_bound_vars_uncached(binder, fld_r, fld_t, fld_c);
// If there were higher-ranked regions to replace, then actually create
// the next universe (this avoids needlessly creating universes).
if !map.is_empty() {
if replaced_bound_var.get() {
let n_u = self.create_next_universe();
assert_eq!(n_u, next_universe);
}
debug!(
"replace_bound_vars_with_placeholders(\
next_universe={:?}, \
result={:?}, \
map={:?})",
next_universe, result, map,
);
debug!(?next_universe, ?result);
result
}

View File

@ -1528,21 +1528,33 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
where
T: TypeFoldable<'tcx>,
{
let fld_r =
|br: ty::BoundRegion| self.next_region_var(LateBoundRegion(span, br.kind, lbrct));
let fld_t = |_| {
self.next_ty_var(TypeVariableOrigin {
kind: TypeVariableOriginKind::MiscVariable,
span,
let mut region_map = BTreeMap::new();
let fld_r = |br: ty::BoundRegion| {
*region_map
.entry(br)
.or_insert_with(|| self.next_region_var(LateBoundRegion(span, br.kind, lbrct)))
};
let mut ty_map = BTreeMap::new();
let fld_t = |bt: ty::BoundTy| {
*ty_map.entry(bt).or_insert_with(|| {
self.next_ty_var(TypeVariableOrigin {
kind: TypeVariableOriginKind::MiscVariable,
span,
})
})
};
let fld_c = |_, ty| {
self.next_const_var(
ty,
ConstVariableOrigin { kind: ConstVariableOriginKind::MiscVariable, span },
)
let mut ct_map = BTreeMap::new();
let fld_c = |bc: ty::BoundVar, ty| {
*ct_map.entry(bc).or_insert_with(|| {
self.next_const_var(
ty,
ConstVariableOrigin { kind: ConstVariableOriginKind::MiscVariable, span },
)
})
};
self.tcx.replace_bound_vars(value, fld_r, fld_t, fld_c)
let result = self.tcx.replace_bound_vars_uncached(value, fld_r, fld_t, fld_c);
(result, region_map)
}
/// See the [`region_constraints::RegionConstraintCollector::verify_generic_bound`] method.

View File

@ -771,7 +771,7 @@ impl<'tcx> TyCtxt<'tcx> {
/// Replaces all escaping bound vars. The `fld_r` closure replaces escaping
/// bound regions; the `fld_t` closure replaces escaping bound types and the `fld_c`
/// closure replaces escaping bound consts.
pub fn replace_escaping_bound_vars<T, F, G, H>(
pub fn replace_escaping_bound_vars_uncached<T, F, G, H>(
self,
value: T,
mut fld_r: F,
@ -795,23 +795,20 @@ impl<'tcx> TyCtxt<'tcx> {
/// Replaces all types or regions bound by the given `Binder`. The `fld_r`
/// closure replaces bound regions, the `fld_t` closure replaces bound
/// types, and `fld_c` replaces bound constants.
pub fn replace_bound_vars<T, F, G, H>(
pub fn replace_bound_vars_uncached<T, F, G, H>(
self,
value: Binder<'tcx, T>,
mut fld_r: F,
fld_r: F,
fld_t: G,
fld_c: H,
) -> (T, BTreeMap<ty::BoundRegion, ty::Region<'tcx>>)
) -> T
where
F: FnMut(ty::BoundRegion) -> ty::Region<'tcx>,
G: FnMut(ty::BoundTy) -> Ty<'tcx>,
H: FnMut(ty::BoundVar, Ty<'tcx>) -> ty::Const<'tcx>,
T: TypeFoldable<'tcx>,
{
let mut region_map = BTreeMap::new();
let real_fld_r = |br: ty::BoundRegion| *region_map.entry(br).or_insert_with(|| fld_r(br));
let value = self.replace_escaping_bound_vars(value.skip_binder(), real_fld_r, fld_t, fld_c);
(value, region_map)
self.replace_escaping_bound_vars_uncached(value.skip_binder(), fld_r, fld_t, fld_c)
}
/// Replaces any late-bound regions bound in `value` with
@ -837,7 +834,7 @@ impl<'tcx> TyCtxt<'tcx> {
where
T: TypeFoldable<'tcx>,
{
self.replace_escaping_bound_vars(
self.replace_escaping_bound_vars_uncached(
value,
|r| {
self.mk_region(ty::ReLateBound(