Auto merge of #123537 - compiler-errors:shallow, r=lcnr
Simplify shallow resolver to just fold ty/consts Probably faster than using a whole folder?
This commit is contained in:
commit
3fba278231
13 changed files with 96 additions and 125 deletions
|
@ -802,7 +802,7 @@ impl<'cx, 'tcx> Canonicalizer<'cx, 'tcx> {
|
|||
const_var: ty::Const<'tcx>,
|
||||
) -> ty::Const<'tcx> {
|
||||
debug_assert!(
|
||||
!self.infcx.is_some_and(|infcx| const_var != infcx.shallow_resolve(const_var))
|
||||
!self.infcx.is_some_and(|infcx| const_var != infcx.shallow_resolve_const(const_var))
|
||||
);
|
||||
let var = self.canonical_var(info, const_var.into());
|
||||
ty::Const::new_bound(self.tcx, self.binder_index, var, self.fold_ty(const_var.ty()))
|
||||
|
|
|
@ -1254,19 +1254,76 @@ impl<'tcx> InferCtxt<'tcx> {
|
|||
}
|
||||
}
|
||||
|
||||
/// Resolve any type variables found in `value` -- but only one
|
||||
/// level. So, if the variable `?X` is bound to some type
|
||||
/// `Foo<?Y>`, then this would return `Foo<?Y>` (but `?Y` may
|
||||
/// itself be bound to a type).
|
||||
///
|
||||
/// Useful when you only need to inspect the outermost level of
|
||||
/// the type and don't care about nested types (or perhaps you
|
||||
/// will be resolving them as well, e.g. in a loop).
|
||||
pub fn shallow_resolve<T>(&self, value: T) -> T
|
||||
where
|
||||
T: TypeFoldable<TyCtxt<'tcx>>,
|
||||
{
|
||||
value.fold_with(&mut ShallowResolver { infcx: self })
|
||||
pub fn shallow_resolve(&self, ty: Ty<'tcx>) -> Ty<'tcx> {
|
||||
if let ty::Infer(v) = ty.kind() { self.fold_infer_ty(*v).unwrap_or(ty) } else { ty }
|
||||
}
|
||||
|
||||
// This is separate from `shallow_resolve` to keep that method small and inlinable.
|
||||
#[inline(never)]
|
||||
fn fold_infer_ty(&self, v: InferTy) -> Option<Ty<'tcx>> {
|
||||
match v {
|
||||
ty::TyVar(v) => {
|
||||
// Not entirely obvious: if `typ` is a type variable,
|
||||
// it can be resolved to an int/float variable, which
|
||||
// can then be recursively resolved, hence the
|
||||
// recursion. Note though that we prevent type
|
||||
// variables from unifying to other type variables
|
||||
// directly (though they may be embedded
|
||||
// structurally), and we prevent cycles in any case,
|
||||
// so this recursion should always be of very limited
|
||||
// depth.
|
||||
//
|
||||
// Note: if these two lines are combined into one we get
|
||||
// dynamic borrow errors on `self.inner`.
|
||||
let known = self.inner.borrow_mut().type_variables().probe(v).known();
|
||||
known.map(|t| self.shallow_resolve(t))
|
||||
}
|
||||
|
||||
ty::IntVar(v) => self
|
||||
.inner
|
||||
.borrow_mut()
|
||||
.int_unification_table()
|
||||
.probe_value(v)
|
||||
.map(|v| v.to_type(self.tcx)),
|
||||
|
||||
ty::FloatVar(v) => self
|
||||
.inner
|
||||
.borrow_mut()
|
||||
.float_unification_table()
|
||||
.probe_value(v)
|
||||
.map(|v| v.to_type(self.tcx)),
|
||||
|
||||
ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn shallow_resolve_const(&self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
|
||||
match ct.kind() {
|
||||
ty::ConstKind::Infer(infer_ct) => match infer_ct {
|
||||
InferConst::Var(vid) => self
|
||||
.inner
|
||||
.borrow_mut()
|
||||
.const_unification_table()
|
||||
.probe_value(vid)
|
||||
.known()
|
||||
.unwrap_or(ct),
|
||||
InferConst::EffectVar(vid) => self
|
||||
.inner
|
||||
.borrow_mut()
|
||||
.effect_unification_table()
|
||||
.probe_value(vid)
|
||||
.known()
|
||||
.unwrap_or(ct),
|
||||
InferConst::Fresh(_) => ct,
|
||||
},
|
||||
ty::ConstKind::Param(_)
|
||||
| ty::ConstKind::Bound(_, _)
|
||||
| ty::ConstKind::Placeholder(_)
|
||||
| ty::ConstKind::Unevaluated(_)
|
||||
| ty::ConstKind::Value(_)
|
||||
| ty::ConstKind::Error(_)
|
||||
| ty::ConstKind::Expr(_) => ct,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn root_var(&self, var: ty::TyVid) -> ty::TyVid {
|
||||
|
@ -1777,89 +1834,6 @@ impl<'tcx> TypeFolder<TyCtxt<'tcx>> for InferenceLiteralEraser<'tcx> {
|
|||
}
|
||||
}
|
||||
|
||||
struct ShallowResolver<'a, 'tcx> {
|
||||
infcx: &'a InferCtxt<'tcx>,
|
||||
}
|
||||
|
||||
impl<'a, 'tcx> TypeFolder<TyCtxt<'tcx>> for ShallowResolver<'a, 'tcx> {
|
||||
fn interner(&self) -> TyCtxt<'tcx> {
|
||||
self.infcx.tcx
|
||||
}
|
||||
|
||||
/// If `ty` is a type variable of some kind, resolve it one level
|
||||
/// (but do not resolve types found in the result). If `typ` is
|
||||
/// not a type variable, just return it unmodified.
|
||||
#[inline]
|
||||
fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
|
||||
if let ty::Infer(v) = ty.kind() { self.fold_infer_ty(*v).unwrap_or(ty) } else { ty }
|
||||
}
|
||||
|
||||
fn fold_const(&mut self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
|
||||
match ct.kind() {
|
||||
ty::ConstKind::Infer(InferConst::Var(vid)) => self
|
||||
.infcx
|
||||
.inner
|
||||
.borrow_mut()
|
||||
.const_unification_table()
|
||||
.probe_value(vid)
|
||||
.known()
|
||||
.unwrap_or(ct),
|
||||
ty::ConstKind::Infer(InferConst::EffectVar(vid)) => self
|
||||
.infcx
|
||||
.inner
|
||||
.borrow_mut()
|
||||
.effect_unification_table()
|
||||
.probe_value(vid)
|
||||
.known()
|
||||
.unwrap_or(ct),
|
||||
_ => ct,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'tcx> ShallowResolver<'a, 'tcx> {
|
||||
// This is separate from `fold_ty` to keep that method small and inlinable.
|
||||
#[inline(never)]
|
||||
fn fold_infer_ty(&mut self, v: InferTy) -> Option<Ty<'tcx>> {
|
||||
match v {
|
||||
ty::TyVar(v) => {
|
||||
// Not entirely obvious: if `typ` is a type variable,
|
||||
// it can be resolved to an int/float variable, which
|
||||
// can then be recursively resolved, hence the
|
||||
// recursion. Note though that we prevent type
|
||||
// variables from unifying to other type variables
|
||||
// directly (though they may be embedded
|
||||
// structurally), and we prevent cycles in any case,
|
||||
// so this recursion should always be of very limited
|
||||
// depth.
|
||||
//
|
||||
// Note: if these two lines are combined into one we get
|
||||
// dynamic borrow errors on `self.inner`.
|
||||
let known = self.infcx.inner.borrow_mut().type_variables().probe(v).known();
|
||||
known.map(|t| self.fold_ty(t))
|
||||
}
|
||||
|
||||
ty::IntVar(v) => self
|
||||
.infcx
|
||||
.inner
|
||||
.borrow_mut()
|
||||
.int_unification_table()
|
||||
.probe_value(v)
|
||||
.map(|v| v.to_type(self.infcx.tcx)),
|
||||
|
||||
ty::FloatVar(v) => self
|
||||
.infcx
|
||||
.inner
|
||||
.borrow_mut()
|
||||
.float_unification_table()
|
||||
.probe_value(v)
|
||||
.map(|v| v.to_type(self.infcx.tcx)),
|
||||
|
||||
ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_) => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx> TypeTrace<'tcx> {
|
||||
pub fn span(&self) -> Span {
|
||||
self.cause.span
|
||||
|
|
|
@ -155,8 +155,8 @@ impl<'tcx> InferCtxt<'tcx> {
|
|||
return Ok(a);
|
||||
}
|
||||
|
||||
let a = self.shallow_resolve(a);
|
||||
let b = self.shallow_resolve(b);
|
||||
let a = self.shallow_resolve_const(a);
|
||||
let b = self.shallow_resolve_const(b);
|
||||
|
||||
// We should never have to relate the `ty` field on `Const` as it is checked elsewhere that consts have the
|
||||
// correct type for the generic param they are an argument for. However there have been a number of cases
|
||||
|
|
|
@ -12,21 +12,19 @@ use rustc_middle::ty::{self, Const, InferConst, Ty, TyCtxt, TypeFoldable};
|
|||
/// useful for printing messages etc but also required at various
|
||||
/// points for correctness.
|
||||
pub struct OpportunisticVarResolver<'a, 'tcx> {
|
||||
// The shallow resolver is used to resolve inference variables at every
|
||||
// level of the type.
|
||||
shallow_resolver: crate::infer::ShallowResolver<'a, 'tcx>,
|
||||
infcx: &'a InferCtxt<'tcx>,
|
||||
}
|
||||
|
||||
impl<'a, 'tcx> OpportunisticVarResolver<'a, 'tcx> {
|
||||
#[inline]
|
||||
pub fn new(infcx: &'a InferCtxt<'tcx>) -> Self {
|
||||
OpportunisticVarResolver { shallow_resolver: crate::infer::ShallowResolver { infcx } }
|
||||
OpportunisticVarResolver { infcx }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'tcx> TypeFolder<TyCtxt<'tcx>> for OpportunisticVarResolver<'a, 'tcx> {
|
||||
fn interner(&self) -> TyCtxt<'tcx> {
|
||||
TypeFolder::interner(&self.shallow_resolver)
|
||||
self.infcx.tcx
|
||||
}
|
||||
|
||||
#[inline]
|
||||
|
@ -34,7 +32,7 @@ impl<'a, 'tcx> TypeFolder<TyCtxt<'tcx>> for OpportunisticVarResolver<'a, 'tcx> {
|
|||
if !t.has_non_region_infer() {
|
||||
t // micro-optimize -- if there is nothing in this type that this fold affects...
|
||||
} else {
|
||||
let t = self.shallow_resolver.fold_ty(t);
|
||||
let t = self.infcx.shallow_resolve(t);
|
||||
t.super_fold_with(self)
|
||||
}
|
||||
}
|
||||
|
@ -43,7 +41,7 @@ impl<'a, 'tcx> TypeFolder<TyCtxt<'tcx>> for OpportunisticVarResolver<'a, 'tcx> {
|
|||
if !ct.has_non_region_infer() {
|
||||
ct // micro-optimize -- if there is nothing in this const that this fold affects...
|
||||
} else {
|
||||
let ct = self.shallow_resolver.fold_const(ct);
|
||||
let ct = self.infcx.shallow_resolve_const(ct);
|
||||
ct.super_fold_with(self)
|
||||
}
|
||||
}
|
||||
|
@ -160,7 +158,7 @@ impl<'a, 'tcx> FallibleTypeFolder<TyCtxt<'tcx>> for FullTypeResolver<'a, 'tcx> {
|
|||
if !c.has_infer() {
|
||||
Ok(c) // micro-optimize -- if there is nothing in this const that this fold affects...
|
||||
} else {
|
||||
let c = self.infcx.shallow_resolve(c);
|
||||
let c = self.infcx.shallow_resolve_const(c);
|
||||
match c.kind() {
|
||||
ty::ConstKind::Infer(InferConst::Var(vid)) => {
|
||||
return Err(FixupError::UnresolvedConst(vid));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue