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

View file

@ -1528,21 +1528,33 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
where where
T: TypeFoldable<'tcx>, T: TypeFoldable<'tcx>,
{ {
let fld_r = let mut region_map = BTreeMap::new();
|br: ty::BoundRegion| self.next_region_var(LateBoundRegion(span, br.kind, lbrct)); let fld_r = |br: ty::BoundRegion| {
let fld_t = |_| { *region_map
self.next_ty_var(TypeVariableOrigin { .entry(br)
kind: TypeVariableOriginKind::MiscVariable, .or_insert_with(|| self.next_region_var(LateBoundRegion(span, br.kind, lbrct)))
span, };
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| { let mut ct_map = BTreeMap::new();
self.next_const_var( let fld_c = |bc: ty::BoundVar, ty| {
ty, *ct_map.entry(bc).or_insert_with(|| {
ConstVariableOrigin { kind: ConstVariableOriginKind::MiscVariable, span }, 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. /// 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 /// 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` /// bound regions; the `fld_t` closure replaces escaping bound types and the `fld_c`
/// closure replaces escaping bound consts. /// 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, self,
value: T, value: T,
mut fld_r: F, 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` /// Replaces all types or regions bound by the given `Binder`. The `fld_r`
/// closure replaces bound regions, the `fld_t` closure replaces bound /// closure replaces bound regions, the `fld_t` closure replaces bound
/// types, and `fld_c` replaces bound constants. /// 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, self,
value: Binder<'tcx, T>, value: Binder<'tcx, T>,
mut fld_r: F, fld_r: F,
fld_t: G, fld_t: G,
fld_c: H, fld_c: H,
) -> (T, BTreeMap<ty::BoundRegion, ty::Region<'tcx>>) ) -> T
where where
F: FnMut(ty::BoundRegion) -> ty::Region<'tcx>, F: FnMut(ty::BoundRegion) -> ty::Region<'tcx>,
G: FnMut(ty::BoundTy) -> Ty<'tcx>, G: FnMut(ty::BoundTy) -> Ty<'tcx>,
H: FnMut(ty::BoundVar, Ty<'tcx>) -> ty::Const<'tcx>, H: FnMut(ty::BoundVar, Ty<'tcx>) -> ty::Const<'tcx>,
T: TypeFoldable<'tcx>, T: TypeFoldable<'tcx>,
{ {
let mut region_map = BTreeMap::new(); self.replace_escaping_bound_vars_uncached(value.skip_binder(), fld_r, fld_t, fld_c)
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)
} }
/// Replaces any late-bound regions bound in `value` with /// Replaces any late-bound regions bound in `value` with
@ -837,7 +834,7 @@ impl<'tcx> TyCtxt<'tcx> {
where where
T: TypeFoldable<'tcx>, T: TypeFoldable<'tcx>,
{ {
self.replace_escaping_bound_vars( self.replace_escaping_bound_vars_uncached(
value, value,
|r| { |r| {
self.mk_region(ty::ReLateBound( self.mk_region(ty::ReLateBound(