Replace some fold calls

This commit is contained in:
Florian Diebold 2021-04-07 21:26:37 +02:00
parent b25b147e86
commit caee3b6c2d
4 changed files with 53 additions and 47 deletions

View File

@ -118,21 +118,10 @@ impl<'a, 'b> Canonicalizer<'a, 'b> {
impl<T: HasInterner<Interner = Interner>> Canonicalized<T> {
pub(super) fn decanonicalize_ty(&self, ty: Ty) -> Ty {
ty.fold_binders(
&mut |ty, binders| {
if let TyKind::BoundVar(bound) = ty.kind(&Interner) {
if bound.debruijn >= binders {
let (v, k) = self.free_vars[bound.index];
TyKind::InferenceVar(v, k).intern(&Interner)
} else {
ty
}
} else {
ty
}
},
DebruijnIndex::INNERMOST,
)
crate::fold_free_vars(ty, |bound, _binders| {
let (v, k) = self.free_vars[bound.index];
TyKind::InferenceVar(v, k).intern(&Interner)
})
}
pub(super) fn apply_solution(

View File

@ -302,3 +302,29 @@ pub fn dummy_usize_const() -> Const {
}
.intern(&Interner)
}
pub(crate) fn fold_free_vars<T: HasInterner<Interner = Interner> + Fold<Interner>>(
t: T,
f: impl FnMut(BoundVar, DebruijnIndex) -> Ty,
) -> T::Result {
use chalk_ir::{fold::Folder, Fallible};
struct FreeVarFolder<F>(F);
impl<'i, F: FnMut(BoundVar, DebruijnIndex) -> Ty + 'i> Folder<'i, Interner> for FreeVarFolder<F> {
fn as_dyn(&mut self) -> &mut dyn Folder<'i, Interner> {
self
}
fn interner(&self) -> &'i Interner {
&Interner
}
fn fold_free_var_ty(
&mut self,
bound_var: BoundVar,
outer_binder: DebruijnIndex,
) -> Fallible<Ty> {
Ok(self.0(bound_var, outer_binder))
}
}
t.fold_with(&mut FreeVarFolder(f), DebruijnIndex::INNERMOST).expect("fold failed unexpectedly")
}

View File

@ -1016,22 +1016,16 @@ pub(crate) fn generic_defaults_query(
p.default.as_ref().map_or(TyKind::Error.intern(&Interner), |t| ctx.lower_ty(t));
// Each default can only refer to previous parameters.
ty = ty.fold_binders(
&mut |ty, binders| match ty.kind(&Interner) {
TyKind::BoundVar(BoundVar { debruijn, index }) if *debruijn == binders => {
if *index >= idx {
// type variable default referring to parameter coming
// after it. This is forbidden (FIXME: report
// diagnostic)
TyKind::Error.intern(&Interner)
} else {
ty
}
}
_ => ty,
},
DebruijnIndex::INNERMOST,
);
ty = crate::fold_free_vars(ty, |bound, binders| {
if bound.index >= idx && bound.debruijn == DebruijnIndex::INNERMOST {
// type variable default referring to parameter coming
// after it. This is forbidden (FIXME: report
// diagnostic)
TyKind::Error.intern(&Interner)
} else {
bound.shifted_in_from(binders).to_ty(&Interner)
}
});
crate::make_only_type_binders(idx, ty)
})

View File

@ -6,7 +6,11 @@ use std::{iter, sync::Arc};
use arrayvec::ArrayVec;
use base_db::CrateId;
use chalk_ir::{cast::Cast, Mutability, UniverseIndex};
use chalk_ir::{
cast::Cast,
fold::{Fold, Folder},
Fallible, Mutability, UniverseIndex,
};
use hir_def::{
lang_item::LangItemTarget, nameres::DefMap, AssocContainerId, AssocItemId, FunctionId,
GenericDefId, HasModule, ImplId, Lookup, ModuleId, TraitId,
@ -21,7 +25,7 @@ use crate::{
primitive::{self, FloatTy, IntTy, UintTy},
static_lifetime,
utils::all_super_traits,
AdtId, Canonical, CanonicalVarKinds, DebruijnIndex, FnPointer, FnSig, ForeignDefId,
AdtId, BoundVar, Canonical, CanonicalVarKinds, DebruijnIndex, FnPointer, FnSig, ForeignDefId,
InEnvironment, Interner, Scalar, Substitution, TraitEnvironment, TraitRefExt, Ty, TyBuilder,
TyExt, TyKind, TypeWalk,
};
@ -757,20 +761,13 @@ pub(crate) fn inherent_impl_substs(
/// This replaces any 'free' Bound vars in `s` (i.e. those with indices past
/// num_vars_to_keep) by `TyKind::Unknown`.
fn fallback_bound_vars(s: Substitution, num_vars_to_keep: usize) -> Substitution {
s.fold_binders(
&mut |ty, binders| {
if let TyKind::BoundVar(bound) = ty.kind(&Interner) {
if bound.index >= num_vars_to_keep && bound.debruijn >= binders {
TyKind::Error.intern(&Interner)
} else {
ty
}
} else {
ty
}
},
DebruijnIndex::INNERMOST,
)
crate::fold_free_vars(s, |bound, binders| {
if bound.index >= num_vars_to_keep && bound.debruijn == DebruijnIndex::INNERMOST {
TyKind::Error.intern(&Interner)
} else {
bound.shifted_in_from(binders).to_ty(&Interner)
}
})
}
fn transform_receiver_ty(