Skip to content

Commit 2636e44

Browse files
committed
fix: simplify the usage of UnknownMismatch
1 parent 3d373fe commit 2636e44

File tree

3 files changed

+21
-41
lines changed

3 files changed

+21
-41
lines changed

crates/hir-ty/src/chalk_ext.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ pub trait TyExt {
2929
fn contains_unknown(&self) -> bool;
3030
fn is_ty_var(&self) -> bool;
3131
fn is_union(&self) -> bool;
32-
fn is_projection(&self) -> bool;
3332

3433
fn as_adt(&self) -> Option<(hir_def::AdtId, &Substitution)>;
3534
fn as_builtin(&self) -> Option<BuiltinType>;
@@ -102,13 +101,6 @@ impl TyExt for Ty {
102101
matches!(self.adt_id(Interner), Some(AdtId(hir_def::AdtId::UnionId(_))))
103102
}
104103

105-
fn is_projection(&self) -> bool {
106-
matches!(
107-
self.kind(Interner),
108-
TyKind::Alias(AliasTy::Projection(_)) | TyKind::AssociatedType(_, _)
109-
)
110-
}
111-
112104
fn as_adt(&self) -> Option<(hir_def::AdtId, &Substitution)> {
113105
match self.kind(Interner) {
114106
TyKind::Adt(AdtId(adt), parameters) => Some((*adt, parameters)),

crates/hir-ty/src/infer.rs

Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -704,27 +704,13 @@ impl<'a> InferenceContext<'a> {
704704
type_mismatches.retain(|_, mismatch| {
705705
mismatch.expected = table.resolve_completely(mismatch.expected.clone());
706706
mismatch.actual = table.resolve_completely(mismatch.actual.clone());
707-
let unresolved_ty_mismatch = || {
708-
chalk_ir::zip::Zip::zip_with(
709-
&mut UnknownMismatch(self.db, |ty| matches!(ty.kind(Interner), TyKind::Error)),
710-
Variance::Invariant,
711-
&mismatch.expected,
712-
&mismatch.actual,
713-
)
714-
.is_ok()
715-
};
716-
717-
let unresolved_projections_mismatch = || {
718-
chalk_ir::zip::Zip::zip_with(
719-
&mut UnknownMismatch(self.db, |ty| ty.contains_unknown() && ty.is_projection()),
720-
chalk_ir::Variance::Invariant,
721-
&mismatch.expected,
722-
&mismatch.actual,
723-
)
724-
.is_ok()
725-
};
726-
727-
unresolved_ty_mismatch() && unresolved_projections_mismatch()
707+
chalk_ir::zip::Zip::zip_with(
708+
&mut UnknownMismatch(self.db),
709+
Variance::Invariant,
710+
&mismatch.expected,
711+
&mismatch.actual,
712+
)
713+
.is_ok()
728714
});
729715
diagnostics.retain_mut(|diagnostic| {
730716
use InferenceDiagnostic::*;
@@ -1666,16 +1652,13 @@ impl std::ops::BitOrAssign for Diverges {
16661652
*self = *self | other;
16671653
}
16681654
}
1669-
/// A zipper that checks for unequal `{unknown}` occurrences in the two types.
1670-
/// Types that have different constructors are filtered out and tested by the
1671-
/// provided closure `F`. Commonly used to filter out mismatch diagnostics that
1672-
/// only differ in `{unknown}`. These mismatches are usually not helpful, as the
1673-
/// cause is usually an underlying name resolution problem.
1674-
///
1675-
/// E.g. when F is `|ty| matches!(ty.kind(Interer), TyKind::Unknown)`, the zipper
1676-
/// will skip over all mismatches that only differ in `{unknown}`.
1677-
struct UnknownMismatch<'db, F: Fn(&Ty) -> bool>(&'db dyn HirDatabase, F);
1678-
impl<F: Fn(&Ty) -> bool> chalk_ir::zip::Zipper<Interner> for UnknownMismatch<'_, F> {
1655+
1656+
/// A zipper that checks for unequal occurrences of `{unknown}` and unresolved projections
1657+
/// in the two types. Used to filter out mismatch diagnostics that only differ in
1658+
/// `{unknown}` and unresolved projections. These mismatches are usually not helpful.
1659+
/// As the cause is usually an underlying name resolution problem
1660+
struct UnknownMismatch<'db>(&'db dyn HirDatabase);
1661+
impl chalk_ir::zip::Zipper<Interner> for UnknownMismatch<'_> {
16791662
fn zip_tys(&mut self, variance: Variance, a: &Ty, b: &Ty) -> chalk_ir::Fallible<()> {
16801663
let zip_substs = |this: &mut Self,
16811664
variances,
@@ -1746,7 +1729,12 @@ impl<F: Fn(&Ty) -> bool> chalk_ir::zip::Zipper<Interner> for UnknownMismatch<'_,
17461729
zip_substs(self, None, &fn_ptr_a.substitution.0, &fn_ptr_b.substitution.0)?
17471730
}
17481731
(TyKind::Error, TyKind::Error) => (),
1749-
_ if (self.1)(a) || (self.1)(b) => return Err(chalk_ir::NoSolution),
1732+
(TyKind::Error, _)
1733+
| (_, TyKind::Error)
1734+
| (TyKind::Alias(AliasTy::Projection(_)) | TyKind::AssociatedType(_, _), _)
1735+
| (_, TyKind::Alias(AliasTy::Projection(_)) | TyKind::AssociatedType(_, _)) => {
1736+
return Err(chalk_ir::NoSolution)
1737+
}
17501738
_ => (),
17511739
}
17521740

crates/hir-ty/src/tests/diagnostics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ impl Trait for () {
141141
fn no_mismatches_with_unresolved_projections() {
142142
check_no_mismatches(
143143
r#"
144-
// Thing is {unknown}
144+
// `Thing` is `{unknown}`
145145
fn create() -> Option<(i32, Thing)> {
146146
Some((69420, Thing))
147147
}

0 commit comments

Comments
 (0)