Skip to content

Commit ba01ff4

Browse files
committed
Fix diagnostics panicking when resolving to different files due to macros
1 parent 634d588 commit ba01ff4

15 files changed

+158
-77
lines changed

crates/ide-diagnostics/src/handlers/field_shorthand.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
//! Suggests shortening `Foo { field: field }` to `Foo { field }` in both
22
//! expressions and patterns.
33
4-
use ide_db::{base_db::FileId, source_change::SourceChange};
4+
use ide_db::{
5+
base_db::{FileId, FileRange},
6+
source_change::SourceChange,
7+
};
58
use syntax::{ast, match_ast, AstNode, SyntaxNode};
69
use text_edit::TextEdit;
710

@@ -49,7 +52,7 @@ fn check_expr_field_shorthand(
4952
Diagnostic::new(
5053
DiagnosticCode::Clippy("redundant_field_names"),
5154
"Shorthand struct initialization",
52-
field_range,
55+
FileRange { file_id, range: field_range },
5356
)
5457
.with_fixes(Some(vec![fix(
5558
"use_expr_field_shorthand",
@@ -93,7 +96,7 @@ fn check_pat_field_shorthand(
9396
Diagnostic::new(
9497
DiagnosticCode::Clippy("redundant_field_names"),
9598
"Shorthand struct pattern",
96-
field_range,
99+
FileRange { file_id, range: field_range },
97100
)
98101
.with_fixes(Some(vec![fix(
99102
"use_pat_field_shorthand",

crates/ide-diagnostics/src/handlers/inactive_code.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub(crate) fn inactive_code(
3131
let res = Diagnostic::new(
3232
DiagnosticCode::Ra("inactive-code", Severity::WeakWarning),
3333
message,
34-
ctx.sema.diagnostics_display_range(d.node.clone()).range,
34+
ctx.sema.diagnostics_display_range(d.node.clone()),
3535
)
3636
.with_unused(true);
3737
Some(res)

crates/ide-diagnostics/src/handlers/invalid_derive_target.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pub(crate) fn invalid_derive_target(
88
ctx: &DiagnosticsContext<'_>,
99
d: &hir::InvalidDeriveTarget,
1010
) -> Diagnostic {
11-
let display_range = ctx.sema.diagnostics_display_range(d.node.clone()).range;
11+
let display_range = ctx.sema.diagnostics_display_range(d.node.clone());
1212

1313
Diagnostic::new(
1414
DiagnosticCode::RustcHardError("E0774"),

crates/ide-diagnostics/src/handlers/json_is_not_rust.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
44
use hir::{PathResolution, Semantics};
55
use ide_db::{
6-
base_db::FileId,
6+
base_db::{FileId, FileRange},
77
helpers::mod_path_to_ast,
88
imports::insert_use::{insert_use, ImportScope},
99
source_change::SourceChangeBuilder,
@@ -119,7 +119,7 @@ pub(crate) fn json_in_items(
119119
Diagnostic::new(
120120
DiagnosticCode::Ra("json-is-not-rust", Severity::WeakWarning),
121121
"JSON syntax is not valid as a Rust item",
122-
range,
122+
FileRange { file_id, range },
123123
)
124124
.with_fixes(Some(vec![{
125125
let mut scb = SourceChangeBuilder::new(file_id);

crates/ide-diagnostics/src/handlers/macro_error.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,4 +264,24 @@ fn f() {
264264
"#,
265265
)
266266
}
267+
268+
#[test]
269+
fn include_does_not_break_diagnostics() {
270+
let mut config = DiagnosticsConfig::test_sample();
271+
config.disabled.insert("inactive-code".to_string());
272+
config.disabled.insert("unlinked-file".to_string());
273+
check_diagnostics_with_config(
274+
config,
275+
r#"
276+
//- minicore: include
277+
//- /lib.rs crate:lib
278+
include!("include-me.rs");
279+
//- /include-me.rs
280+
/// long doc that pushes the diagnostic range beyond the first file's text length
281+
#[err]
282+
//^^^^^^error: unresolved macro `err`
283+
mod prim_never {}
284+
"#,
285+
);
286+
}
267287
}

crates/ide-diagnostics/src/handlers/malformed_derive.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ pub(crate) fn malformed_derive(
77
ctx: &DiagnosticsContext<'_>,
88
d: &hir::MalformedDerive,
99
) -> Diagnostic {
10-
let display_range = ctx.sema.diagnostics_display_range(d.node.clone()).range;
10+
let display_range = ctx.sema.diagnostics_display_range(d.node.clone());
1111

1212
Diagnostic::new(
1313
DiagnosticCode::RustcHardError("E0777"),

crates/ide-diagnostics/src/handlers/mismatched_arg_count.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use either::Either;
22
use hir::InFile;
3+
use ide_db::base_db::FileRange;
34
use syntax::{
45
ast::{self, HasArgList},
5-
AstNode, SyntaxNodePtr, TextRange,
6+
AstNode, SyntaxNodePtr,
67
};
78

89
use crate::{adjusted_display_range, Diagnostic, DiagnosticCode, DiagnosticsContext};
@@ -48,7 +49,7 @@ fn invalid_args_range(
4849
source: InFile<SyntaxNodePtr>,
4950
expected: usize,
5051
found: usize,
51-
) -> TextRange {
52+
) -> FileRange {
5253
adjusted_display_range::<Either<ast::Expr, ast::TupleStructPat>>(ctx, source, &|expr| {
5354
let (text_range, r_paren_token, expected_arg) = match expr {
5455
Either::Left(ast::Expr::CallExpr(call)) => {

crates/ide-diagnostics/src/handlers/type_mismatch.rs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,10 @@ pub(crate) fn type_mismatch(ctx: &DiagnosticsContext<'_>, d: &hir::TypeMismatch)
3535
Some(salient_token_range)
3636
},
3737
),
38-
pat => {
39-
ctx.sema
40-
.diagnostics_display_range(InFile {
41-
file_id: d.expr_or_pat.file_id,
42-
value: pat.syntax_node_ptr(),
43-
})
44-
.range
45-
}
38+
pat => ctx.sema.diagnostics_display_range(InFile {
39+
file_id: d.expr_or_pat.file_id,
40+
value: pat.syntax_node_ptr(),
41+
}),
4642
};
4743
let mut diag = Diagnostic::new(
4844
DiagnosticCode::RustcHardError("E0308"),
@@ -84,7 +80,7 @@ fn add_reference(
8480
expr_ptr: &InFile<AstPtr<ast::Expr>>,
8581
acc: &mut Vec<Assist>,
8682
) -> Option<()> {
87-
let range = ctx.sema.diagnostics_display_range(expr_ptr.clone().map(|it| it.into())).range;
83+
let range = ctx.sema.diagnostics_display_range(expr_ptr.clone().map(|it| it.into()));
8884

8985
let (_, mutability) = d.expected.as_reference()?;
9086
let actual_with_ref = Type::reference(&d.actual, mutability);
@@ -94,10 +90,9 @@ fn add_reference(
9490

9591
let ampersands = format!("&{}", mutability.as_keyword_for_ref());
9692

97-
let edit = TextEdit::insert(range.start(), ampersands);
98-
let source_change =
99-
SourceChange::from_text_edit(expr_ptr.file_id.original_file(ctx.sema.db), edit);
100-
acc.push(fix("add_reference_here", "Add reference here", source_change, range));
93+
let edit = TextEdit::insert(range.range.start(), ampersands);
94+
let source_change = SourceChange::from_text_edit(range.file_id, edit);
95+
acc.push(fix("add_reference_here", "Add reference here", source_change, range.range));
10196
Some(())
10297
}
10398

crates/ide-diagnostics/src/handlers/typed_hole.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub(crate) fn typed_hole(ctx: &DiagnosticsContext<'_>, d: &hir::TypedHole) -> Di
2626
)
2727
};
2828

29-
Diagnostic::new(DiagnosticCode::RustcHardError("typed-hole"), message, display_range.range)
29+
Diagnostic::new(DiagnosticCode::RustcHardError("typed-hole"), message, display_range)
3030
.with_fixes(fixes)
3131
}
3232

crates/ide-diagnostics/src/handlers/unlinked_file.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::iter;
44

55
use hir::{db::DefDatabase, DefMap, InFile, ModuleSource};
66
use ide_db::{
7-
base_db::{FileId, FileLoader, SourceDatabase, SourceDatabaseExt},
7+
base_db::{FileId, FileLoader, FileRange, SourceDatabase, SourceDatabaseExt},
88
source_change::SourceChange,
99
RootDatabase,
1010
};
@@ -46,8 +46,12 @@ pub(crate) fn unlinked_file(
4646
.unwrap_or(range);
4747

4848
acc.push(
49-
Diagnostic::new(DiagnosticCode::Ra("unlinked-file", Severity::WeakWarning), message, range)
50-
.with_fixes(fixes),
49+
Diagnostic::new(
50+
DiagnosticCode::Ra("unlinked-file", Severity::WeakWarning),
51+
message,
52+
FileRange { file_id, range },
53+
)
54+
.with_fixes(fixes),
5155
);
5256
}
5357

0 commit comments

Comments
 (0)