Skip to content

Commit caee3b6

Browse files
committed
Replace some fold calls
1 parent b25b147 commit caee3b6

File tree

4 files changed

+53
-47
lines changed

4 files changed

+53
-47
lines changed

crates/hir_ty/src/infer/unify.rs

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -118,21 +118,10 @@ impl<'a, 'b> Canonicalizer<'a, 'b> {
118118

119119
impl<T: HasInterner<Interner = Interner>> Canonicalized<T> {
120120
pub(super) fn decanonicalize_ty(&self, ty: Ty) -> Ty {
121-
ty.fold_binders(
122-
&mut |ty, binders| {
123-
if let TyKind::BoundVar(bound) = ty.kind(&Interner) {
124-
if bound.debruijn >= binders {
125-
let (v, k) = self.free_vars[bound.index];
126-
TyKind::InferenceVar(v, k).intern(&Interner)
127-
} else {
128-
ty
129-
}
130-
} else {
131-
ty
132-
}
133-
},
134-
DebruijnIndex::INNERMOST,
135-
)
121+
crate::fold_free_vars(ty, |bound, _binders| {
122+
let (v, k) = self.free_vars[bound.index];
123+
TyKind::InferenceVar(v, k).intern(&Interner)
124+
})
136125
}
137126

138127
pub(super) fn apply_solution(

crates/hir_ty/src/lib.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,3 +302,29 @@ pub fn dummy_usize_const() -> Const {
302302
}
303303
.intern(&Interner)
304304
}
305+
306+
pub(crate) fn fold_free_vars<T: HasInterner<Interner = Interner> + Fold<Interner>>(
307+
t: T,
308+
f: impl FnMut(BoundVar, DebruijnIndex) -> Ty,
309+
) -> T::Result {
310+
use chalk_ir::{fold::Folder, Fallible};
311+
struct FreeVarFolder<F>(F);
312+
impl<'i, F: FnMut(BoundVar, DebruijnIndex) -> Ty + 'i> Folder<'i, Interner> for FreeVarFolder<F> {
313+
fn as_dyn(&mut self) -> &mut dyn Folder<'i, Interner> {
314+
self
315+
}
316+
317+
fn interner(&self) -> &'i Interner {
318+
&Interner
319+
}
320+
321+
fn fold_free_var_ty(
322+
&mut self,
323+
bound_var: BoundVar,
324+
outer_binder: DebruijnIndex,
325+
) -> Fallible<Ty> {
326+
Ok(self.0(bound_var, outer_binder))
327+
}
328+
}
329+
t.fold_with(&mut FreeVarFolder(f), DebruijnIndex::INNERMOST).expect("fold failed unexpectedly")
330+
}

crates/hir_ty/src/lower.rs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,22 +1016,16 @@ pub(crate) fn generic_defaults_query(
10161016
p.default.as_ref().map_or(TyKind::Error.intern(&Interner), |t| ctx.lower_ty(t));
10171017

10181018
// Each default can only refer to previous parameters.
1019-
ty = ty.fold_binders(
1020-
&mut |ty, binders| match ty.kind(&Interner) {
1021-
TyKind::BoundVar(BoundVar { debruijn, index }) if *debruijn == binders => {
1022-
if *index >= idx {
1023-
// type variable default referring to parameter coming
1024-
// after it. This is forbidden (FIXME: report
1025-
// diagnostic)
1026-
TyKind::Error.intern(&Interner)
1027-
} else {
1028-
ty
1029-
}
1030-
}
1031-
_ => ty,
1032-
},
1033-
DebruijnIndex::INNERMOST,
1034-
);
1019+
ty = crate::fold_free_vars(ty, |bound, binders| {
1020+
if bound.index >= idx && bound.debruijn == DebruijnIndex::INNERMOST {
1021+
// type variable default referring to parameter coming
1022+
// after it. This is forbidden (FIXME: report
1023+
// diagnostic)
1024+
TyKind::Error.intern(&Interner)
1025+
} else {
1026+
bound.shifted_in_from(binders).to_ty(&Interner)
1027+
}
1028+
});
10351029

10361030
crate::make_only_type_binders(idx, ty)
10371031
})

crates/hir_ty/src/method_resolution.rs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ use std::{iter, sync::Arc};
66

77
use arrayvec::ArrayVec;
88
use base_db::CrateId;
9-
use chalk_ir::{cast::Cast, Mutability, UniverseIndex};
9+
use chalk_ir::{
10+
cast::Cast,
11+
fold::{Fold, Folder},
12+
Fallible, Mutability, UniverseIndex,
13+
};
1014
use hir_def::{
1115
lang_item::LangItemTarget, nameres::DefMap, AssocContainerId, AssocItemId, FunctionId,
1216
GenericDefId, HasModule, ImplId, Lookup, ModuleId, TraitId,
@@ -21,7 +25,7 @@ use crate::{
2125
primitive::{self, FloatTy, IntTy, UintTy},
2226
static_lifetime,
2327
utils::all_super_traits,
24-
AdtId, Canonical, CanonicalVarKinds, DebruijnIndex, FnPointer, FnSig, ForeignDefId,
28+
AdtId, BoundVar, Canonical, CanonicalVarKinds, DebruijnIndex, FnPointer, FnSig, ForeignDefId,
2529
InEnvironment, Interner, Scalar, Substitution, TraitEnvironment, TraitRefExt, Ty, TyBuilder,
2630
TyExt, TyKind, TypeWalk,
2731
};
@@ -757,20 +761,13 @@ pub(crate) fn inherent_impl_substs(
757761
/// This replaces any 'free' Bound vars in `s` (i.e. those with indices past
758762
/// num_vars_to_keep) by `TyKind::Unknown`.
759763
fn fallback_bound_vars(s: Substitution, num_vars_to_keep: usize) -> Substitution {
760-
s.fold_binders(
761-
&mut |ty, binders| {
762-
if let TyKind::BoundVar(bound) = ty.kind(&Interner) {
763-
if bound.index >= num_vars_to_keep && bound.debruijn >= binders {
764-
TyKind::Error.intern(&Interner)
765-
} else {
766-
ty
767-
}
768-
} else {
769-
ty
770-
}
771-
},
772-
DebruijnIndex::INNERMOST,
773-
)
764+
crate::fold_free_vars(s, |bound, binders| {
765+
if bound.index >= num_vars_to_keep && bound.debruijn == DebruijnIndex::INNERMOST {
766+
TyKind::Error.intern(&Interner)
767+
} else {
768+
bound.shifted_in_from(binders).to_ty(&Interner)
769+
}
770+
})
774771
}
775772

776773
fn transform_receiver_ty(

0 commit comments

Comments
 (0)