aboutsummaryrefslogtreecommitdiff
path: root/crates/hir/src
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2021-06-13 15:09:41 +0100
committerGitHub <[email protected]>2021-06-13 15:09:41 +0100
commit3d8df2aef87bca7ec3f0994d799462f08d1ad449 (patch)
treea43b94510193f1f58ec7bfc39814ae58f673ceca /crates/hir/src
parente6fa9b016fab4bf38f4e2a798fcabcc13b58e9ab (diff)
parentfa9ed4e0ce633e51d1411951bf044719e6837457 (diff)
Merge #9248
9248: internal: refactor unresolved macro call diagnostic r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/hir/src')
-rw-r--r--crates/hir/src/diagnostics.rs60
-rw-r--r--crates/hir/src/lib.rs27
2 files changed, 24 insertions, 63 deletions
diff --git a/crates/hir/src/diagnostics.rs b/crates/hir/src/diagnostics.rs
index ec0a8fe41..718c86b3a 100644
--- a/crates/hir/src/diagnostics.rs
+++ b/crates/hir/src/diagnostics.rs
@@ -17,7 +17,7 @@ pub use crate::diagnostics_sink::{
17}; 17};
18 18
19macro_rules! diagnostics { 19macro_rules! diagnostics {
20 ($($diag:ident),*) => { 20 ($($diag:ident,)*) => {
21 pub enum AnyDiagnostic {$( 21 pub enum AnyDiagnostic {$(
22 $diag(Box<$diag>), 22 $diag(Box<$diag>),
23 )*} 23 )*}
@@ -32,7 +32,13 @@ macro_rules! diagnostics {
32 }; 32 };
33} 33}
34 34
35diagnostics![UnresolvedModule, UnresolvedExternCrate, MissingFields]; 35diagnostics![
36 UnresolvedModule,
37 UnresolvedExternCrate,
38 UnresolvedImport,
39 UnresolvedMacroCall,
40 MissingFields,
41];
36 42
37#[derive(Debug)] 43#[derive(Debug)]
38pub struct UnresolvedModule { 44pub struct UnresolvedModule {
@@ -47,61 +53,15 @@ pub struct UnresolvedExternCrate {
47 53
48#[derive(Debug)] 54#[derive(Debug)]
49pub struct UnresolvedImport { 55pub struct UnresolvedImport {
50 pub file: HirFileId, 56 pub decl: InFile<AstPtr<ast::UseTree>>,
51 pub node: AstPtr<ast::UseTree>,
52}
53
54impl Diagnostic for UnresolvedImport {
55 fn code(&self) -> DiagnosticCode {
56 DiagnosticCode("unresolved-import")
57 }
58 fn message(&self) -> String {
59 "unresolved import".to_string()
60 }
61 fn display_source(&self) -> InFile<SyntaxNodePtr> {
62 InFile::new(self.file, self.node.clone().into())
63 }
64 fn as_any(&self) -> &(dyn Any + Send + 'static) {
65 self
66 }
67 fn is_experimental(&self) -> bool {
68 // This currently results in false positives in the following cases:
69 // - `cfg_if!`-generated code in libstd (we don't load the sysroot correctly)
70 // - `core::arch` (we don't handle `#[path = "../<path>"]` correctly)
71 // - proc macros and/or proc macro generated code
72 true
73 }
74} 57}
75 58
76// Diagnostic: unresolved-macro-call
77//
78// This diagnostic is triggered if rust-analyzer is unable to resolve the path to a
79// macro in a macro invocation.
80#[derive(Debug, Clone, Eq, PartialEq)] 59#[derive(Debug, Clone, Eq, PartialEq)]
81pub struct UnresolvedMacroCall { 60pub struct UnresolvedMacroCall {
82 pub file: HirFileId, 61 pub macro_call: InFile<AstPtr<ast::MacroCall>>,
83 pub node: AstPtr<ast::MacroCall>,
84 pub path: ModPath, 62 pub path: ModPath,
85} 63}
86 64
87impl Diagnostic for UnresolvedMacroCall {
88 fn code(&self) -> DiagnosticCode {
89 DiagnosticCode("unresolved-macro-call")
90 }
91 fn message(&self) -> String {
92 format!("unresolved macro `{}!`", self.path)
93 }
94 fn display_source(&self) -> InFile<SyntaxNodePtr> {
95 InFile::new(self.file, self.node.clone().into())
96 }
97 fn as_any(&self) -> &(dyn Any + Send + 'static) {
98 self
99 }
100 fn is_experimental(&self) -> bool {
101 true
102 }
103}
104
105// Diagnostic: inactive-code 65// Diagnostic: inactive-code
106// 66//
107// This diagnostic is shown for code with inactive `#[cfg]` attributes. 67// This diagnostic is shown for code with inactive `#[cfg]` attributes.
diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs
index f7883c469..0a9414013 100644
--- a/crates/hir/src/lib.rs
+++ b/crates/hir/src/lib.rs
@@ -498,7 +498,10 @@ impl Module {
498 let import = &item_tree[id.value]; 498 let import = &item_tree[id.value];
499 499
500 let use_tree = import.use_tree_to_ast(db.upcast(), file_id, *index); 500 let use_tree = import.use_tree_to_ast(db.upcast(), file_id, *index);
501 sink.push(UnresolvedImport { file: file_id, node: AstPtr::new(&use_tree) }); 501 acc.push(
502 UnresolvedImport { decl: InFile::new(file_id, AstPtr::new(&use_tree)) }
503 .into(),
504 );
502 } 505 }
503 506
504 DefDiagnosticKind::UnconfiguredCode { ast, cfg, opts } => { 507 DefDiagnosticKind::UnconfiguredCode { ast, cfg, opts } => {
@@ -577,11 +580,13 @@ impl Module {
577 580
578 DefDiagnosticKind::UnresolvedMacroCall { ast, path } => { 581 DefDiagnosticKind::UnresolvedMacroCall { ast, path } => {
579 let node = ast.to_node(db.upcast()); 582 let node = ast.to_node(db.upcast());
580 sink.push(UnresolvedMacroCall { 583 acc.push(
581 file: ast.file_id, 584 UnresolvedMacroCall {
582 node: AstPtr::new(&node), 585 macro_call: InFile::new(ast.file_id, AstPtr::new(&node)),
583 path: path.clone(), 586 path: path.clone(),
584 }); 587 }
588 .into(),
589 );
585 } 590 }
586 591
587 DefDiagnosticKind::MacroError { ast, message } => { 592 DefDiagnosticKind::MacroError { ast, message } => {
@@ -1057,13 +1062,9 @@ impl Function {
1057 precise_location: None, 1062 precise_location: None,
1058 macro_name: None, 1063 macro_name: None,
1059 }), 1064 }),
1060 BodyDiagnostic::UnresolvedMacroCall { node, path } => { 1065 BodyDiagnostic::UnresolvedMacroCall { node, path } => acc.push(
1061 sink.push(UnresolvedMacroCall { 1066 UnresolvedMacroCall { macro_call: node.clone(), path: path.clone() }.into(),
1062 file: node.file_id, 1067 ),
1063 node: node.value.clone(),
1064 path: path.clone(),
1065 })
1066 }
1067 } 1068 }
1068 } 1069 }
1069 1070