aboutsummaryrefslogtreecommitdiff
path: root/crates/ide
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2021-06-13 15:06:36 +0100
committerAleksey Kladov <[email protected]>2021-06-13 15:08:54 +0100
commitfa9ed4e0ce633e51d1411951bf044719e6837457 (patch)
tree527881b07c885f01132c023426bc8592a4318fd3 /crates/ide
parent6d104de15aee6a24a442871c59528c39d410c161 (diff)
internal: refactor unresolved macro call diagnostic
Diffstat (limited to 'crates/ide')
-rw-r--r--crates/ide/src/diagnostics.rs29
-rw-r--r--crates/ide/src/diagnostics/unresolved_macro_call.rs72
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 @@
7mod unresolved_module; 7mod unresolved_module;
8mod unresolved_extern_crate; 8mod unresolved_extern_crate;
9mod unresolved_import; 9mod unresolved_import;
10mod unresolved_macro_call;
10mod missing_fields; 11mod missing_fields;
11 12
12mod fixes; 13mod fixes;
@@ -16,9 +17,8 @@ mod unlinked_file;
16use std::cell::RefCell; 17use std::cell::RefCell;
17 18
18use hir::{ 19use 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};
23use ide_assists::AssistResolveStrategy; 23use ide_assists::AssistResolveStrategy;
24use ide_db::{base_db::SourceDatabase, RootDatabase}; 24use 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#"
487foo::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 @@
1use hir::{db::AstDatabase, InFile};
2use syntax::{AstNode, SyntaxNodePtr};
3
4use 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.
10pub(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)]
34mod tests {
35 use crate::diagnostics::tests::check_diagnostics;
36
37 #[test]
38 fn test_unresolved_macro_range() {
39 check_diagnostics(
40 r#"
41foo::bar!(92);
42 //^^^ unresolved macro `foo::bar!`
43"#,
44 );
45 }
46
47 #[test]
48 fn unresolved_legacy_scope_macro() {
49 check_diagnostics(
50 r#"
51macro_rules! m { () => {} }
52
53m!(); m2!();
54 //^^ unresolved macro `self::m2!`
55"#,
56 );
57 }
58
59 #[test]
60 fn unresolved_module_scope_macro() {
61 check_diagnostics(
62 r#"
63mod mac {
64#[macro_export]
65macro_rules! m { () => {} } }
66
67self::m!(); self::m2!();
68 //^^ unresolved macro `self::m2!`
69"#,
70 );
71 }
72}