Unnamed repository; edit this file 'description' to name the repository.
Merge pull request #21942 from Amit5601/fix-unknown-mismatch
fix: silence type mismatch diagnostic when type is unknown
Chayim Refael Friedman 7 weeks ago
parent 8f20499 · parent dc20744 · commit d165944
-rw-r--r--crates/ide-diagnostics/src/handlers/type_mismatch.rs58
-rw-r--r--crates/ide-diagnostics/src/lib.rs5
2 files changed, 47 insertions, 16 deletions
diff --git a/crates/ide-diagnostics/src/handlers/type_mismatch.rs b/crates/ide-diagnostics/src/handlers/type_mismatch.rs
index f443dc08f5..4b653709f0 100644
--- a/crates/ide-diagnostics/src/handlers/type_mismatch.rs
+++ b/crates/ide-diagnostics/src/handlers/type_mismatch.rs
@@ -20,7 +20,14 @@ use crate::{Assist, Diagnostic, DiagnosticCode, DiagnosticsContext, adjusted_dis
//
// This diagnostic is triggered when the type of an expression or pattern does not match
// the expected type.
-pub(crate) fn type_mismatch(ctx: &DiagnosticsContext<'_>, d: &hir::TypeMismatch<'_>) -> Diagnostic {
+pub(crate) fn type_mismatch(
+ ctx: &DiagnosticsContext<'_>,
+ d: &hir::TypeMismatch<'_>,
+) -> Option<Diagnostic> {
+ if d.expected.is_unknown() || d.actual.is_unknown() {
+ return None;
+ }
+
let display_range = adjusted_display_range(ctx, d.expr_or_pat, &|node| {
let Either::Left(expr) = node else { return None };
let salient_token_range = match expr {
@@ -39,21 +46,23 @@ pub(crate) fn type_mismatch(ctx: &DiagnosticsContext<'_>, d: &hir::TypeMismatch<
cov_mark::hit!(type_mismatch_range_adjustment);
Some(salient_token_range)
});
- Diagnostic::new(
- DiagnosticCode::RustcHardError("E0308"),
- format!(
- "expected {}, found {}",
- d.expected
- .display(ctx.sema.db, ctx.display_target)
- .with_closure_style(ClosureStyle::ClosureWithId),
- d.actual
- .display(ctx.sema.db, ctx.display_target)
- .with_closure_style(ClosureStyle::ClosureWithId),
- ),
- display_range,
+ Some(
+ Diagnostic::new(
+ DiagnosticCode::RustcHardError("E0308"),
+ format!(
+ "expected {}, found {}",
+ d.expected
+ .display(ctx.sema.db, ctx.display_target)
+ .with_closure_style(ClosureStyle::ClosureWithId),
+ d.actual
+ .display(ctx.sema.db, ctx.display_target)
+ .with_closure_style(ClosureStyle::ClosureWithId),
+ ),
+ display_range,
+ )
+ .stable()
+ .with_fixes(fixes(ctx, d)),
)
- .stable()
- .with_fixes(fixes(ctx, d))
}
fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::TypeMismatch<'_>) -> Option<Vec<Assist>> {
@@ -1253,4 +1262,23 @@ fn main() {
"#,
);
}
+
+ #[test]
+ fn test_ignore_unknown_mismatch() {
+ check_diagnostics(
+ r#"
+pub trait Foo {
+ type Out;
+}
+impl Foo for [i32; 1] {
+ type Out = ();
+}
+pub fn foo<T: Foo>(_: T) -> (T::Out,) { loop { } }
+
+fn main() {
+ let _x = foo(2);
+}
+"#,
+ );
+ }
}
diff --git a/crates/ide-diagnostics/src/lib.rs b/crates/ide-diagnostics/src/lib.rs
index 0c6953419f..a74a52ceec 100644
--- a/crates/ide-diagnostics/src/lib.rs
+++ b/crates/ide-diagnostics/src/lib.rs
@@ -430,7 +430,10 @@ pub fn semantic_diagnostics(
AnyDiagnostic::TraitImplRedundantAssocItems(d) => handlers::trait_impl_redundant_assoc_item::trait_impl_redundant_assoc_item(&ctx, &d),
AnyDiagnostic::TraitImplOrphan(d) => handlers::trait_impl_orphan::trait_impl_orphan(&ctx, &d),
AnyDiagnostic::TypedHole(d) => handlers::typed_hole::typed_hole(&ctx, &d),
- AnyDiagnostic::TypeMismatch(d) => handlers::type_mismatch::type_mismatch(&ctx, &d),
+ AnyDiagnostic::TypeMismatch(d) => match handlers::type_mismatch::type_mismatch(&ctx, &d) {
+ Some(diag) => diag,
+ None => continue,
+ },
AnyDiagnostic::UndeclaredLabel(d) => handlers::undeclared_label::undeclared_label(&ctx, &d),
AnyDiagnostic::UnimplementedBuiltinMacro(d) => handlers::unimplemented_builtin_macro::unimplemented_builtin_macro(&ctx, &d),
AnyDiagnostic::UnreachableLabel(d) => handlers::unreachable_label::unreachable_label(&ctx, &d),