about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2024-01-31 08:17:53 +0000
committerbors <bors@rust-lang.org>2024-01-31 08:17:53 +0000
commite48bc04eaf1f45566295516947114627503ac090 (patch)
tree8ddf0c010ac4c426bda0ee4e7bdad2123c72c461
parente4146af29494768b0075e2fea2a56b699b0f9141 (diff)
parent604479c3734e2e6d44f668c69963e8d73879e0d8 (diff)
downloadrust-e48bc04eaf1f45566295516947114627503ac090.tar.gz
rust-e48bc04eaf1f45566295516947114627503ac090.zip
Auto merge of #16459 - Veykril:diagnostics, r=Veykril
internal: Use improved adjusted_display_range for all diagnostics
-rw-r--r--crates/ide-diagnostics/src/handlers/mismatched_arg_count.rs4
-rw-r--r--crates/ide-diagnostics/src/handlers/trait_impl_incorrect_safety.rs2
-rw-r--r--crates/ide-diagnostics/src/handlers/trait_impl_missing_assoc_item.rs2
-rw-r--r--crates/ide-diagnostics/src/handlers/type_mismatch.rs46
-rw-r--r--crates/ide-diagnostics/src/handlers/unresolved_field.rs4
-rw-r--r--crates/ide-diagnostics/src/handlers/unresolved_method.rs4
-rw-r--r--crates/ide-diagnostics/src/lib.rs19
7 files changed, 27 insertions, 54 deletions
diff --git a/crates/ide-diagnostics/src/handlers/mismatched_arg_count.rs b/crates/ide-diagnostics/src/handlers/mismatched_arg_count.rs
index e75d8973728..66ebf593505 100644
--- a/crates/ide-diagnostics/src/handlers/mismatched_arg_count.rs
+++ b/crates/ide-diagnostics/src/handlers/mismatched_arg_count.rs
@@ -6,7 +6,7 @@ use syntax::{
     AstNode, AstPtr,
 };
 
-use crate::{adjusted_display_range_new, Diagnostic, DiagnosticCode, DiagnosticsContext};
+use crate::{adjusted_display_range, Diagnostic, DiagnosticCode, DiagnosticsContext};
 
 // Diagnostic: mismatched-tuple-struct-pat-arg-count
 //
@@ -50,7 +50,7 @@ fn invalid_args_range(
     expected: usize,
     found: usize,
 ) -> FileRange {
-    adjusted_display_range_new(ctx, source, &|expr| {
+    adjusted_display_range(ctx, source, &|expr| {
         let (text_range, r_paren_token, expected_arg) = match expr {
             Either::Left(ast::Expr::CallExpr(call)) => {
                 let arg_list = call.arg_list()?;
diff --git a/crates/ide-diagnostics/src/handlers/trait_impl_incorrect_safety.rs b/crates/ide-diagnostics/src/handlers/trait_impl_incorrect_safety.rs
index 251a645292e..6be2c54e603 100644
--- a/crates/ide-diagnostics/src/handlers/trait_impl_incorrect_safety.rs
+++ b/crates/ide-diagnostics/src/handlers/trait_impl_incorrect_safety.rs
@@ -19,7 +19,7 @@ pub(crate) fn trait_impl_incorrect_safety(
         },
         adjusted_display_range::<ast::Impl>(
             ctx,
-            InFile { file_id: d.file_id, value: d.impl_.syntax_node_ptr() },
+            InFile { file_id: d.file_id, value: d.impl_ },
             &|impl_| {
                 if d.should_be_safe {
                     Some(match (impl_.unsafe_token(), impl_.impl_token()) {
diff --git a/crates/ide-diagnostics/src/handlers/trait_impl_missing_assoc_item.rs b/crates/ide-diagnostics/src/handlers/trait_impl_missing_assoc_item.rs
index 56188cddf0b..58d1b7f31d2 100644
--- a/crates/ide-diagnostics/src/handlers/trait_impl_missing_assoc_item.rs
+++ b/crates/ide-diagnostics/src/handlers/trait_impl_missing_assoc_item.rs
@@ -25,7 +25,7 @@ pub(crate) fn trait_impl_missing_assoc_item(
         format!("not all trait items implemented, missing: {missing}"),
         adjusted_display_range::<ast::Impl>(
             ctx,
-            InFile { file_id: d.file_id, value: d.impl_.syntax_node_ptr() },
+            InFile { file_id: d.file_id, value: d.impl_ },
             &|impl_| impl_.trait_().map(|t| t.syntax().text_range()),
         ),
     )
diff --git a/crates/ide-diagnostics/src/handlers/type_mismatch.rs b/crates/ide-diagnostics/src/handlers/type_mismatch.rs
index 23042e222b5..750189beecb 100644
--- a/crates/ide-diagnostics/src/handlers/type_mismatch.rs
+++ b/crates/ide-diagnostics/src/handlers/type_mismatch.rs
@@ -1,3 +1,4 @@
+use either::Either;
 use hir::{db::ExpandDatabase, ClosureStyle, HirDisplay, HirFileIdExt, InFile, Type};
 use ide_db::{famous_defs::FamousDefs, source_change::SourceChange};
 use syntax::{
@@ -13,33 +14,24 @@ use crate::{adjusted_display_range, fix, Assist, Diagnostic, DiagnosticCode, Dia
 // 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 {
-    let display_range = match &d.expr_or_pat.value {
-        expr if ast::Expr::can_cast(expr.kind()) => adjusted_display_range::<ast::Expr>(
-            ctx,
-            InFile { file_id: d.expr_or_pat.file_id, value: expr.syntax_node_ptr() },
-            &|expr| {
-                let salient_token_range = match expr {
-                    ast::Expr::IfExpr(it) => it.if_token()?.text_range(),
-                    ast::Expr::LoopExpr(it) => it.loop_token()?.text_range(),
-                    ast::Expr::ForExpr(it) => it.for_token()?.text_range(),
-                    ast::Expr::WhileExpr(it) => it.while_token()?.text_range(),
-                    ast::Expr::BlockExpr(it) => it.stmt_list()?.r_curly_token()?.text_range(),
-                    ast::Expr::MatchExpr(it) => it.match_token()?.text_range(),
-                    ast::Expr::MethodCallExpr(it) => it.name_ref()?.ident_token()?.text_range(),
-                    ast::Expr::FieldExpr(it) => it.name_ref()?.ident_token()?.text_range(),
-                    ast::Expr::AwaitExpr(it) => it.await_token()?.text_range(),
-                    _ => return None,
-                };
-
-                cov_mark::hit!(type_mismatch_range_adjustment);
-                Some(salient_token_range)
-            },
-        ),
-        pat => ctx.sema.diagnostics_display_range(InFile {
-            file_id: d.expr_or_pat.file_id,
-            value: pat.syntax_node_ptr(),
-        }),
-    };
+    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 {
+            ast::Expr::IfExpr(it) => it.if_token()?.text_range(),
+            ast::Expr::LoopExpr(it) => it.loop_token()?.text_range(),
+            ast::Expr::ForExpr(it) => it.for_token()?.text_range(),
+            ast::Expr::WhileExpr(it) => it.while_token()?.text_range(),
+            ast::Expr::BlockExpr(it) => it.stmt_list()?.r_curly_token()?.text_range(),
+            ast::Expr::MatchExpr(it) => it.match_token()?.text_range(),
+            ast::Expr::MethodCallExpr(it) => it.name_ref()?.ident_token()?.text_range(),
+            ast::Expr::FieldExpr(it) => it.name_ref()?.ident_token()?.text_range(),
+            ast::Expr::AwaitExpr(it) => it.await_token()?.text_range(),
+            _ => return None,
+        };
+
+        cov_mark::hit!(type_mismatch_range_adjustment);
+        Some(salient_token_range)
+    });
     let mut diag = Diagnostic::new(
         DiagnosticCode::RustcHardError("E0308"),
         format!(
diff --git a/crates/ide-diagnostics/src/handlers/unresolved_field.rs b/crates/ide-diagnostics/src/handlers/unresolved_field.rs
index 32145941218..0e7a5720d4d 100644
--- a/crates/ide-diagnostics/src/handlers/unresolved_field.rs
+++ b/crates/ide-diagnostics/src/handlers/unresolved_field.rs
@@ -8,7 +8,7 @@ use ide_db::{
 use syntax::{ast, AstNode, AstPtr};
 use text_edit::TextEdit;
 
-use crate::{adjusted_display_range_new, Diagnostic, DiagnosticCode, DiagnosticsContext};
+use crate::{adjusted_display_range, Diagnostic, DiagnosticCode, DiagnosticsContext};
 
 // Diagnostic: unresolved-field
 //
@@ -29,7 +29,7 @@ pub(crate) fn unresolved_field(
             d.name.display(ctx.sema.db),
             d.receiver.display(ctx.sema.db)
         ),
-        adjusted_display_range_new(ctx, d.expr, &|expr| {
+        adjusted_display_range(ctx, d.expr, &|expr| {
             Some(
                 match expr {
                     ast::Expr::MethodCallExpr(it) => it.name_ref(),
diff --git a/crates/ide-diagnostics/src/handlers/unresolved_method.rs b/crates/ide-diagnostics/src/handlers/unresolved_method.rs
index 41fb6729085..9f8fee67f31 100644
--- a/crates/ide-diagnostics/src/handlers/unresolved_method.rs
+++ b/crates/ide-diagnostics/src/handlers/unresolved_method.rs
@@ -11,7 +11,7 @@ use syntax::{
 };
 use text_edit::TextEdit;
 
-use crate::{adjusted_display_range_new, Diagnostic, DiagnosticCode, DiagnosticsContext};
+use crate::{adjusted_display_range, Diagnostic, DiagnosticCode, DiagnosticsContext};
 
 // Diagnostic: unresolved-method
 //
@@ -34,7 +34,7 @@ pub(crate) fn unresolved_method(
             d.name.display(ctx.sema.db),
             d.receiver.display(ctx.sema.db)
         ),
-        adjusted_display_range_new(ctx, d.expr, &|expr| {
+        adjusted_display_range(ctx, d.expr, &|expr| {
             Some(
                 match expr {
                     ast::Expr::MethodCallExpr(it) => it.name_ref(),
diff --git a/crates/ide-diagnostics/src/lib.rs b/crates/ide-diagnostics/src/lib.rs
index 535fb45cd68..5ad7069e317 100644
--- a/crates/ide-diagnostics/src/lib.rs
+++ b/crates/ide-diagnostics/src/lib.rs
@@ -89,7 +89,6 @@ use ide_db::{
 use once_cell::sync::Lazy;
 use stdx::never;
 use syntax::{
-    algo::find_node_at_range,
     ast::{self, AstNode},
     AstPtr, SyntaxNode, SyntaxNodePtr, TextRange,
 };
@@ -572,24 +571,6 @@ fn unresolved_fix(id: &'static str, label: &str, target: TextRange) -> Assist {
 
 fn adjusted_display_range<N: AstNode>(
     ctx: &DiagnosticsContext<'_>,
-    diag_ptr: InFile<SyntaxNodePtr>,
-    adj: &dyn Fn(N) -> Option<TextRange>,
-) -> FileRange {
-    let FileRange { file_id, range } = ctx.sema.diagnostics_display_range(diag_ptr);
-
-    let source_file = ctx.sema.db.parse(file_id);
-    FileRange {
-        file_id,
-        range: find_node_at_range::<N>(&source_file.syntax_node(), range)
-            .filter(|it| it.syntax().text_range() == range)
-            .and_then(adj)
-            .unwrap_or(range),
-    }
-}
-
-// FIXME Replace the one above with this one?
-fn adjusted_display_range_new<N: AstNode>(
-    ctx: &DiagnosticsContext<'_>,
     diag_ptr: InFile<AstPtr<N>>,
     adj: &dyn Fn(N) -> Option<TextRange>,
 ) -> FileRange {