1
Fork 0

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.