diff options
author | Aleksey Kladov <[email protected]> | 2021-06-13 15:06:36 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2021-06-13 15:08:54 +0100 |
commit | fa9ed4e0ce633e51d1411951bf044719e6837457 (patch) | |
tree | 527881b07c885f01132c023426bc8592a4318fd3 /crates/ide | |
parent | 6d104de15aee6a24a442871c59528c39d410c161 (diff) |
internal: refactor unresolved macro call diagnostic
Diffstat (limited to 'crates/ide')
-rw-r--r-- | crates/ide/src/diagnostics.rs | 29 | ||||
-rw-r--r-- | crates/ide/src/diagnostics/unresolved_macro_call.rs | 72 |
2 files changed, 75 insertions, 26 deletions
diff --git a/crates/ide/src/diagnostics.rs b/crates/ide/src/diagnostics.rs index 3d05dd093..1a4800832 100644 --- a/crates/ide/src/diagnostics.rs +++ b/crates/ide/src/diagnostics.rs | |||
@@ -7,6 +7,7 @@ | |||
7 | mod unresolved_module; | 7 | mod unresolved_module; |
8 | mod unresolved_extern_crate; | 8 | mod unresolved_extern_crate; |
9 | mod unresolved_import; | 9 | mod unresolved_import; |
10 | mod unresolved_macro_call; | ||
10 | mod missing_fields; | 11 | mod missing_fields; |
11 | 12 | ||
12 | mod fixes; | 13 | mod fixes; |
@@ -16,9 +17,8 @@ mod unlinked_file; | |||
16 | use std::cell::RefCell; | 17 | use std::cell::RefCell; |
17 | 18 | ||
18 | use hir::{ | 19 | use hir::{ |
19 | db::AstDatabase, | ||
20 | diagnostics::{AnyDiagnostic, Diagnostic as _, DiagnosticCode, DiagnosticSinkBuilder}, | 20 | diagnostics::{AnyDiagnostic, Diagnostic as _, DiagnosticCode, DiagnosticSinkBuilder}, |
21 | InFile, Semantics, | 21 | Semantics, |
22 | }; | 22 | }; |
23 | use ide_assists::AssistResolveStrategy; | 23 | use ide_assists::AssistResolveStrategy; |
24 | use ide_db::{base_db::SourceDatabase, RootDatabase}; | 24 | use ide_db::{base_db::SourceDatabase, RootDatabase}; |
@@ -203,20 +203,6 @@ pub(crate) fn diagnostics( | |||
203 | res.borrow_mut() | 203 | res.borrow_mut() |
204 | .push(Diagnostic::hint(display_range, d.message()).with_code(Some(d.code()))); | 204 | .push(Diagnostic::hint(display_range, d.message()).with_code(Some(d.code()))); |
205 | }) | 205 | }) |
206 | .on::<hir::diagnostics::UnresolvedMacroCall, _>(|d| { | ||
207 | let last_path_segment = sema.db.parse_or_expand(d.file).and_then(|root| { | ||
208 | d.node | ||
209 | .to_node(&root) | ||
210 | .path() | ||
211 | .and_then(|it| it.segment()) | ||
212 | .and_then(|it| it.name_ref()) | ||
213 | .map(|it| InFile::new(d.file, SyntaxNodePtr::new(it.syntax()))) | ||
214 | }); | ||
215 | let diagnostics = last_path_segment.unwrap_or_else(|| d.display_source()); | ||
216 | let display_range = sema.diagnostics_display_range(diagnostics).range; | ||
217 | res.borrow_mut() | ||
218 | .push(Diagnostic::error(display_range, d.message()).with_code(Some(d.code()))); | ||
219 | }) | ||
220 | .on::<hir::diagnostics::UnimplementedBuiltinMacro, _>(|d| { | 206 | .on::<hir::diagnostics::UnimplementedBuiltinMacro, _>(|d| { |
221 | let display_range = sema.diagnostics_display_range(d.display_source()).range; | 207 | let display_range = sema.diagnostics_display_range(d.display_source()).range; |
222 | res.borrow_mut() | 208 | res.borrow_mut() |
@@ -259,6 +245,7 @@ pub(crate) fn diagnostics( | |||
259 | AnyDiagnostic::UnresolvedModule(d) => unresolved_module::unresolved_module(&ctx, &d), | 245 | AnyDiagnostic::UnresolvedModule(d) => unresolved_module::unresolved_module(&ctx, &d), |
260 | AnyDiagnostic::UnresolvedExternCrate(d) => unresolved_extern_crate::unresolved_extern_crate(&ctx, &d), | 246 | AnyDiagnostic::UnresolvedExternCrate(d) => unresolved_extern_crate::unresolved_extern_crate(&ctx, &d), |
261 | AnyDiagnostic::UnresolvedImport(d) => unresolved_import::unresolved_import(&ctx, &d), | 247 | AnyDiagnostic::UnresolvedImport(d) => unresolved_import::unresolved_import(&ctx, &d), |
248 | AnyDiagnostic::UnresolvedMacroCall(d) => unresolved_macro_call::unresolved_macro_call(&ctx, &d), | ||
262 | AnyDiagnostic::MissingFields(d) => missing_fields::missing_fields(&ctx, &d), | 249 | AnyDiagnostic::MissingFields(d) => missing_fields::missing_fields(&ctx, &d), |
263 | }; | 250 | }; |
264 | if let Some(code) = d.code { | 251 | if let Some(code) = d.code { |
@@ -481,16 +468,6 @@ mod tests { | |||
481 | } | 468 | } |
482 | 469 | ||
483 | #[test] | 470 | #[test] |
484 | fn test_unresolved_macro_range() { | ||
485 | check_diagnostics( | ||
486 | r#" | ||
487 | foo::bar!(92); | ||
488 | //^^^ unresolved macro `foo::bar!` | ||
489 | "#, | ||
490 | ); | ||
491 | } | ||
492 | |||
493 | #[test] | ||
494 | fn range_mapping_out_of_macros() { | 471 | fn range_mapping_out_of_macros() { |
495 | // FIXME: this is very wrong, but somewhat tricky to fix. | 472 | // FIXME: this is very wrong, but somewhat tricky to fix. |
496 | check_fix( | 473 | check_fix( |
diff --git a/crates/ide/src/diagnostics/unresolved_macro_call.rs b/crates/ide/src/diagnostics/unresolved_macro_call.rs new file mode 100644 index 000000000..a3af332a4 --- /dev/null +++ b/crates/ide/src/diagnostics/unresolved_macro_call.rs | |||
@@ -0,0 +1,72 @@ | |||
1 | use hir::{db::AstDatabase, InFile}; | ||
2 | use syntax::{AstNode, SyntaxNodePtr}; | ||
3 | |||
4 | use crate::diagnostics::{Diagnostic, DiagnosticsContext}; | ||
5 | |||
6 | // Diagnostic: unresolved-macro-call | ||
7 | // | ||
8 | // This diagnostic is triggered if rust-analyzer is unable to resolve the path | ||
9 | // to a macro in a macro invocation. | ||
10 | pub(super) fn unresolved_macro_call( | ||
11 | ctx: &DiagnosticsContext<'_>, | ||
12 | d: &hir::UnresolvedMacroCall, | ||
13 | ) -> Diagnostic { | ||
14 | let last_path_segment = ctx.sema.db.parse_or_expand(d.macro_call.file_id).and_then(|root| { | ||
15 | d.macro_call | ||
16 | .value | ||
17 | .to_node(&root) | ||
18 | .path() | ||
19 | .and_then(|it| it.segment()) | ||
20 | .and_then(|it| it.name_ref()) | ||
21 | .map(|it| InFile::new(d.macro_call.file_id, SyntaxNodePtr::new(it.syntax()))) | ||
22 | }); | ||
23 | let diagnostics = last_path_segment.unwrap_or_else(|| d.macro_call.clone().map(|it| it.into())); | ||
24 | |||
25 | Diagnostic::new( | ||
26 | "unresolved-macro-call", | ||
27 | format!("unresolved macro `{}!`", d.path), | ||
28 | ctx.sema.diagnostics_display_range(diagnostics).range, | ||
29 | ) | ||
30 | .experimental() | ||
31 | } | ||
32 | |||
33 | #[cfg(test)] | ||
34 | mod tests { | ||
35 | use crate::diagnostics::tests::check_diagnostics; | ||
36 | |||
37 | #[test] | ||
38 | fn test_unresolved_macro_range() { | ||
39 | check_diagnostics( | ||
40 | r#" | ||
41 | foo::bar!(92); | ||
42 | //^^^ unresolved macro `foo::bar!` | ||
43 | "#, | ||
44 | ); | ||
45 | } | ||
46 | |||
47 | #[test] | ||
48 | fn unresolved_legacy_scope_macro() { | ||
49 | check_diagnostics( | ||
50 | r#" | ||
51 | macro_rules! m { () => {} } | ||
52 | |||
53 | m!(); m2!(); | ||
54 | //^^ unresolved macro `self::m2!` | ||
55 | "#, | ||
56 | ); | ||
57 | } | ||
58 | |||
59 | #[test] | ||
60 | fn unresolved_module_scope_macro() { | ||
61 | check_diagnostics( | ||
62 | r#" | ||
63 | mod mac { | ||
64 | #[macro_export] | ||
65 | macro_rules! m { () => {} } } | ||
66 | |||
67 | self::m!(); self::m2!(); | ||
68 | //^^ unresolved macro `self::m2!` | ||
69 | "#, | ||
70 | ); | ||
71 | } | ||
72 | } | ||