Auto merge of #16459 - Veykril:diagnostics, r=Veykril

internal: Use improved adjusted_display_range for all diagnostics
This commit is contained in:
bors 2024-01-31 08:17:53 +00:00
commit e48bc04eaf
7 changed files with 26 additions and 53 deletions

View file

@ -6,7 +6,7 @@ use syntax::{
AstNode, AstPtr, 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 // Diagnostic: mismatched-tuple-struct-pat-arg-count
// //
@ -50,7 +50,7 @@ fn invalid_args_range(
expected: usize, expected: usize,
found: usize, found: usize,
) -> FileRange { ) -> FileRange {
adjusted_display_range_new(ctx, source, &|expr| { adjusted_display_range(ctx, source, &|expr| {
let (text_range, r_paren_token, expected_arg) = match expr { let (text_range, r_paren_token, expected_arg) = match expr {
Either::Left(ast::Expr::CallExpr(call)) => { Either::Left(ast::Expr::CallExpr(call)) => {
let arg_list = call.arg_list()?; let arg_list = call.arg_list()?;

View file

@ -19,7 +19,7 @@ pub(crate) fn trait_impl_incorrect_safety(
}, },
adjusted_display_range::<ast::Impl>( adjusted_display_range::<ast::Impl>(
ctx, ctx,
InFile { file_id: d.file_id, value: d.impl_.syntax_node_ptr() }, InFile { file_id: d.file_id, value: d.impl_ },
&|impl_| { &|impl_| {
if d.should_be_safe { if d.should_be_safe {
Some(match (impl_.unsafe_token(), impl_.impl_token()) { Some(match (impl_.unsafe_token(), impl_.impl_token()) {

View file

@ -25,7 +25,7 @@ pub(crate) fn trait_impl_missing_assoc_item(
format!("not all trait items implemented, missing: {missing}"), format!("not all trait items implemented, missing: {missing}"),
adjusted_display_range::<ast::Impl>( adjusted_display_range::<ast::Impl>(
ctx, 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()), &|impl_| impl_.trait_().map(|t| t.syntax().text_range()),
), ),
) )

View file

@ -1,3 +1,4 @@
use either::Either;
use hir::{db::ExpandDatabase, ClosureStyle, HirDisplay, HirFileIdExt, InFile, Type}; use hir::{db::ExpandDatabase, ClosureStyle, HirDisplay, HirFileIdExt, InFile, Type};
use ide_db::{famous_defs::FamousDefs, source_change::SourceChange}; use ide_db::{famous_defs::FamousDefs, source_change::SourceChange};
use syntax::{ 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 // This diagnostic is triggered when the type of an expression or pattern does not match
// the expected type. // the expected type.
pub(crate) fn type_mismatch(ctx: &DiagnosticsContext<'_>, d: &hir::TypeMismatch) -> Diagnostic { pub(crate) fn type_mismatch(ctx: &DiagnosticsContext<'_>, d: &hir::TypeMismatch) -> Diagnostic {
let display_range = match &d.expr_or_pat.value { let display_range = adjusted_display_range(ctx, d.expr_or_pat, &|node| {
expr if ast::Expr::can_cast(expr.kind()) => adjusted_display_range::<ast::Expr>( let Either::Left(expr) = node else { return None };
ctx, let salient_token_range = match expr {
InFile { file_id: d.expr_or_pat.file_id, value: expr.syntax_node_ptr() }, ast::Expr::IfExpr(it) => it.if_token()?.text_range(),
&|expr| { ast::Expr::LoopExpr(it) => it.loop_token()?.text_range(),
let salient_token_range = match expr { ast::Expr::ForExpr(it) => it.for_token()?.text_range(),
ast::Expr::IfExpr(it) => it.if_token()?.text_range(), ast::Expr::WhileExpr(it) => it.while_token()?.text_range(),
ast::Expr::LoopExpr(it) => it.loop_token()?.text_range(), ast::Expr::BlockExpr(it) => it.stmt_list()?.r_curly_token()?.text_range(),
ast::Expr::ForExpr(it) => it.for_token()?.text_range(), ast::Expr::MatchExpr(it) => it.match_token()?.text_range(),
ast::Expr::WhileExpr(it) => it.while_token()?.text_range(), ast::Expr::MethodCallExpr(it) => it.name_ref()?.ident_token()?.text_range(),
ast::Expr::BlockExpr(it) => it.stmt_list()?.r_curly_token()?.text_range(), ast::Expr::FieldExpr(it) => it.name_ref()?.ident_token()?.text_range(),
ast::Expr::MatchExpr(it) => it.match_token()?.text_range(), ast::Expr::AwaitExpr(it) => it.await_token()?.text_range(),
ast::Expr::MethodCallExpr(it) => it.name_ref()?.ident_token()?.text_range(), _ => return None,
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); cov_mark::hit!(type_mismatch_range_adjustment);
Some(salient_token_range) 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 mut diag = Diagnostic::new( let mut diag = Diagnostic::new(
DiagnosticCode::RustcHardError("E0308"), DiagnosticCode::RustcHardError("E0308"),
format!( format!(

View file

@ -8,7 +8,7 @@ use ide_db::{
use syntax::{ast, AstNode, AstPtr}; use syntax::{ast, AstNode, AstPtr};
use text_edit::TextEdit; use text_edit::TextEdit;
use crate::{adjusted_display_range_new, Diagnostic, DiagnosticCode, DiagnosticsContext}; use crate::{adjusted_display_range, Diagnostic, DiagnosticCode, DiagnosticsContext};
// Diagnostic: unresolved-field // Diagnostic: unresolved-field
// //
@ -29,7 +29,7 @@ pub(crate) fn unresolved_field(
d.name.display(ctx.sema.db), d.name.display(ctx.sema.db),
d.receiver.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( Some(
match expr { match expr {
ast::Expr::MethodCallExpr(it) => it.name_ref(), ast::Expr::MethodCallExpr(it) => it.name_ref(),

View file

@ -11,7 +11,7 @@ use syntax::{
}; };
use text_edit::TextEdit; use text_edit::TextEdit;
use crate::{adjusted_display_range_new, Diagnostic, DiagnosticCode, DiagnosticsContext}; use crate::{adjusted_display_range, Diagnostic, DiagnosticCode, DiagnosticsContext};
// Diagnostic: unresolved-method // Diagnostic: unresolved-method
// //
@ -34,7 +34,7 @@ pub(crate) fn unresolved_method(
d.name.display(ctx.sema.db), d.name.display(ctx.sema.db),
d.receiver.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( Some(
match expr { match expr {
ast::Expr::MethodCallExpr(it) => it.name_ref(), ast::Expr::MethodCallExpr(it) => it.name_ref(),

View file

@ -89,7 +89,6 @@ use ide_db::{
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use stdx::never; use stdx::never;
use syntax::{ use syntax::{
algo::find_node_at_range,
ast::{self, AstNode}, ast::{self, AstNode},
AstPtr, SyntaxNode, SyntaxNodePtr, TextRange, AstPtr, SyntaxNode, SyntaxNodePtr, TextRange,
}; };
@ -571,24 +570,6 @@ fn unresolved_fix(id: &'static str, label: &str, target: TextRange) -> Assist {
} }
fn adjusted_display_range<N: AstNode>( 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<'_>, ctx: &DiagnosticsContext<'_>,
diag_ptr: InFile<AstPtr<N>>, diag_ptr: InFile<AstPtr<N>>,
adj: &dyn Fn(N) -> Option<TextRange>, adj: &dyn Fn(N) -> Option<TextRange>,