BoundVarReplacer
: trait object instead of 3 fns
This commit is contained in:
parent
05e678ccca
commit
fd59d058ec
5 changed files with 164 additions and 128 deletions
|
@ -7,7 +7,7 @@
|
|||
//! [c]: https://rust-lang.github.io/chalk/book/canonical_queries/canonicalization.html
|
||||
|
||||
use crate::infer::canonical::{Canonical, CanonicalVarValues};
|
||||
use rustc_middle::ty::fold::TypeFoldable;
|
||||
use rustc_middle::ty::fold::{FnMutDelegate, TypeFoldable};
|
||||
use rustc_middle::ty::subst::GenericArgKind;
|
||||
use rustc_middle::ty::{self, TyCtxt};
|
||||
|
||||
|
@ -71,21 +71,21 @@ where
|
|||
if var_values.var_values.is_empty() {
|
||||
value
|
||||
} else {
|
||||
let fld_r = |br: ty::BoundRegion| match var_values.var_values[br.var].unpack() {
|
||||
GenericArgKind::Lifetime(l) => l,
|
||||
r => bug!("{:?} is a region but value is {:?}", br, r),
|
||||
let delegate = FnMutDelegate {
|
||||
regions: |br: ty::BoundRegion| match var_values.var_values[br.var].unpack() {
|
||||
GenericArgKind::Lifetime(l) => l,
|
||||
r => bug!("{:?} is a region but value is {:?}", br, r),
|
||||
},
|
||||
types: |bound_ty: ty::BoundTy| match var_values.var_values[bound_ty.var].unpack() {
|
||||
GenericArgKind::Type(ty) => ty,
|
||||
r => bug!("{:?} is a type but value is {:?}", bound_ty, r),
|
||||
},
|
||||
consts: |bound_ct: ty::BoundVar, _| match var_values.var_values[bound_ct].unpack() {
|
||||
GenericArgKind::Const(ct) => ct,
|
||||
c => bug!("{:?} is a const but value is {:?}", bound_ct, c),
|
||||
},
|
||||
};
|
||||
|
||||
let fld_t = |bound_ty: ty::BoundTy| match var_values.var_values[bound_ty.var].unpack() {
|
||||
GenericArgKind::Type(ty) => ty,
|
||||
r => bug!("{:?} is a type but value is {:?}", bound_ty, r),
|
||||
};
|
||||
|
||||
let fld_c = |bound_ct: ty::BoundVar, _| match var_values.var_values[bound_ct].unpack() {
|
||||
GenericArgKind::Const(ct) => ct,
|
||||
c => bug!("{:?} is a const but value is {:?}", bound_ct, c),
|
||||
};
|
||||
|
||||
tcx.replace_escaping_bound_vars_uncached(value, fld_r, fld_t, fld_c)
|
||||
tcx.replace_escaping_bound_vars_uncached(value, delegate)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
use super::combine::CombineFields;
|
||||
use super::{HigherRankedType, InferCtxt};
|
||||
use crate::infer::CombinedSnapshot;
|
||||
use rustc_middle::ty::fold::FnMutDelegate;
|
||||
use rustc_middle::ty::relate::{Relate, RelateResult, TypeRelation};
|
||||
use rustc_middle::ty::{self, Binder, TypeFoldable};
|
||||
|
||||
|
@ -79,31 +80,31 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
|
|||
|
||||
let next_universe = self.create_next_universe();
|
||||
|
||||
let fld_r = |br: ty::BoundRegion| {
|
||||
self.tcx.mk_region(ty::RePlaceholder(ty::PlaceholderRegion {
|
||||
universe: next_universe,
|
||||
name: br.kind,
|
||||
}))
|
||||
};
|
||||
|
||||
let fld_t = |bound_ty: ty::BoundTy| {
|
||||
self.tcx.mk_ty(ty::Placeholder(ty::PlaceholderType {
|
||||
universe: next_universe,
|
||||
name: bound_ty.var,
|
||||
}))
|
||||
};
|
||||
|
||||
let fld_c = |bound_var: ty::BoundVar, ty| {
|
||||
self.tcx.mk_const(ty::ConstS {
|
||||
kind: ty::ConstKind::Placeholder(ty::PlaceholderConst {
|
||||
let delegate = FnMutDelegate {
|
||||
regions: |br: ty::BoundRegion| {
|
||||
self.tcx.mk_region(ty::RePlaceholder(ty::PlaceholderRegion {
|
||||
universe: next_universe,
|
||||
name: ty::BoundConst { var: bound_var, ty },
|
||||
}),
|
||||
ty,
|
||||
})
|
||||
name: br.kind,
|
||||
}))
|
||||
},
|
||||
types: |bound_ty: ty::BoundTy| {
|
||||
self.tcx.mk_ty(ty::Placeholder(ty::PlaceholderType {
|
||||
universe: next_universe,
|
||||
name: bound_ty.var,
|
||||
}))
|
||||
},
|
||||
consts: |bound_var: ty::BoundVar, ty| {
|
||||
self.tcx.mk_const(ty::ConstS {
|
||||
kind: ty::ConstKind::Placeholder(ty::PlaceholderConst {
|
||||
universe: next_universe,
|
||||
name: ty::BoundConst { var: bound_var, ty },
|
||||
}),
|
||||
ty,
|
||||
})
|
||||
},
|
||||
};
|
||||
|
||||
let result = self.tcx.replace_bound_vars_uncached(binder, fld_r, fld_t, fld_c);
|
||||
let result = self.tcx.replace_bound_vars_uncached(binder, delegate);
|
||||
debug!(?next_universe, ?result);
|
||||
result
|
||||
}
|
||||
|
|
|
@ -23,6 +23,7 @@ use rustc_middle::mir::interpret::{ErrorHandled, EvalToValTreeResult};
|
|||
use rustc_middle::traits::select;
|
||||
use rustc_middle::ty::abstract_const::{AbstractConst, FailureKind};
|
||||
use rustc_middle::ty::error::{ExpectedFound, TypeError};
|
||||
use rustc_middle::ty::fold::BoundVarReplacerDelegate;
|
||||
use rustc_middle::ty::fold::{TypeFoldable, TypeFolder, TypeSuperFoldable};
|
||||
use rustc_middle::ty::relate::RelateResult;
|
||||
use rustc_middle::ty::subst::{GenericArg, GenericArgKind, InternalSubsts, SubstsRef};
|
||||
|
@ -1564,32 +1565,56 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
|
|||
return inner;
|
||||
}
|
||||
|
||||
let mut region_map = FxHashMap::default();
|
||||
let fld_r = |br: ty::BoundRegion| {
|
||||
*region_map
|
||||
.entry(br)
|
||||
.or_insert_with(|| self.next_region_var(LateBoundRegion(span, br.kind, lbrct)))
|
||||
};
|
||||
struct ToFreshVars<'a, 'tcx> {
|
||||
infcx: &'a InferCtxt<'a, 'tcx>,
|
||||
span: Span,
|
||||
lbrct: LateBoundRegionConversionTime,
|
||||
map: FxHashMap<ty::BoundVar, ty::GenericArg<'tcx>>,
|
||||
}
|
||||
|
||||
let mut ty_map = FxHashMap::default();
|
||||
let fld_t = |bt: ty::BoundTy| {
|
||||
*ty_map.entry(bt).or_insert_with(|| {
|
||||
self.next_ty_var(TypeVariableOrigin {
|
||||
kind: TypeVariableOriginKind::MiscVariable,
|
||||
span,
|
||||
})
|
||||
})
|
||||
};
|
||||
let mut ct_map = FxHashMap::default();
|
||||
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_uncached(value, fld_r, fld_t, fld_c)
|
||||
impl<'tcx> BoundVarReplacerDelegate<'tcx> for ToFreshVars<'_, 'tcx> {
|
||||
fn replace_region(&mut self, br: ty::BoundRegion) -> ty::Region<'tcx> {
|
||||
self.map
|
||||
.entry(br.var)
|
||||
.or_insert_with(|| {
|
||||
self.infcx
|
||||
.next_region_var(LateBoundRegion(self.span, br.kind, self.lbrct))
|
||||
.into()
|
||||
})
|
||||
.expect_region()
|
||||
}
|
||||
fn replace_ty(&mut self, bt: ty::BoundTy) -> Ty<'tcx> {
|
||||
self.map
|
||||
.entry(bt.var)
|
||||
.or_insert_with(|| {
|
||||
self.infcx
|
||||
.next_ty_var(TypeVariableOrigin {
|
||||
kind: TypeVariableOriginKind::MiscVariable,
|
||||
span: self.span,
|
||||
})
|
||||
.into()
|
||||
})
|
||||
.expect_ty()
|
||||
}
|
||||
fn replace_const(&mut self, bv: ty::BoundVar, ty: Ty<'tcx>) -> ty::Const<'tcx> {
|
||||
self.map
|
||||
.entry(bv)
|
||||
.or_insert_with(|| {
|
||||
self.infcx
|
||||
.next_const_var(
|
||||
ty,
|
||||
ConstVariableOrigin {
|
||||
kind: ConstVariableOriginKind::MiscVariable,
|
||||
span: self.span,
|
||||
},
|
||||
)
|
||||
.into()
|
||||
})
|
||||
.expect_const()
|
||||
}
|
||||
}
|
||||
let delegate = ToFreshVars { infcx: self, span, lbrct, map: Default::default() };
|
||||
self.tcx.replace_bound_vars_uncached(value, delegate)
|
||||
}
|
||||
|
||||
/// See the [`region_constraints::RegionConstraintCollector::verify_generic_bound`] method.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue