Skip to content

Commit

Permalink
Auto merge of rust-lang#16459 - Veykril:diagnostics, r=Veykril
Browse files Browse the repository at this point in the history
internal: Use improved adjusted_display_range for all diagnostics
  • Loading branch information
bors committed Jan 31, 2024
2 parents e4146af + 604479c commit e48bc04
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 54 deletions.
4 changes: 2 additions & 2 deletions crates/ide-diagnostics/src/handlers/mismatched_arg_count.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
//
Expand Down Expand Up @@ -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()?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
),
)
Expand Down
46 changes: 19 additions & 27 deletions crates/ide-diagnostics/src/handlers/type_mismatch.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand All @@ -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!(
Expand Down
4 changes: 2 additions & 2 deletions crates/ide-diagnostics/src/handlers/unresolved_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
//
Expand All @@ -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(),
Expand Down
4 changes: 2 additions & 2 deletions crates/ide-diagnostics/src/handlers/unresolved_method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
//
Expand All @@ -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(),
Expand Down
19 changes: 0 additions & 19 deletions crates/ide-diagnostics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -571,24 +570,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>,
Expand Down

0 comments on commit e48bc04

Please sign in to comment.