diff options
Diffstat (limited to 'crates/hir/src/diagnostics.rs')
-rw-r--r-- | crates/hir/src/diagnostics.rs | 92 |
1 files changed, 7 insertions, 85 deletions
diff --git a/crates/hir/src/diagnostics.rs b/crates/hir/src/diagnostics.rs index 718c86b3a..28580eeb4 100644 --- a/crates/hir/src/diagnostics.rs +++ b/crates/hir/src/diagnostics.rs | |||
@@ -5,11 +5,10 @@ | |||
5 | //! be expressed in terms of hir types themselves. | 5 | //! be expressed in terms of hir types themselves. |
6 | use std::any::Any; | 6 | use std::any::Any; |
7 | 7 | ||
8 | use cfg::{CfgExpr, CfgOptions, DnfExpr}; | 8 | use cfg::{CfgExpr, CfgOptions}; |
9 | use either::Either; | 9 | use either::Either; |
10 | use hir_def::path::ModPath; | 10 | use hir_def::path::ModPath; |
11 | use hir_expand::{name::Name, HirFileId, InFile}; | 11 | use hir_expand::{name::Name, HirFileId, InFile}; |
12 | use stdx::format_to; | ||
13 | use syntax::{ast, AstPtr, SyntaxNodePtr, TextRange}; | 12 | use syntax::{ast, AstPtr, SyntaxNodePtr, TextRange}; |
14 | 13 | ||
15 | pub use crate::diagnostics_sink::{ | 14 | pub use crate::diagnostics_sink::{ |
@@ -37,7 +36,10 @@ diagnostics![ | |||
37 | UnresolvedExternCrate, | 36 | UnresolvedExternCrate, |
38 | UnresolvedImport, | 37 | UnresolvedImport, |
39 | UnresolvedMacroCall, | 38 | UnresolvedMacroCall, |
39 | UnresolvedProcMacro, | ||
40 | MacroError, | ||
40 | MissingFields, | 41 | MissingFields, |
42 | InactiveCode, | ||
41 | ]; | 43 | ]; |
42 | 44 | ||
43 | #[derive(Debug)] | 45 | #[derive(Debug)] |
@@ -62,108 +64,28 @@ pub struct UnresolvedMacroCall { | |||
62 | pub path: ModPath, | 64 | pub path: ModPath, |
63 | } | 65 | } |
64 | 66 | ||
65 | // Diagnostic: inactive-code | ||
66 | // | ||
67 | // This diagnostic is shown for code with inactive `#[cfg]` attributes. | ||
68 | #[derive(Debug, Clone, Eq, PartialEq)] | 67 | #[derive(Debug, Clone, Eq, PartialEq)] |
69 | pub struct InactiveCode { | 68 | pub struct InactiveCode { |
70 | pub file: HirFileId, | 69 | pub node: InFile<SyntaxNodePtr>, |
71 | pub node: SyntaxNodePtr, | ||
72 | pub cfg: CfgExpr, | 70 | pub cfg: CfgExpr, |
73 | pub opts: CfgOptions, | 71 | pub opts: CfgOptions, |
74 | } | 72 | } |
75 | 73 | ||
76 | impl Diagnostic for InactiveCode { | ||
77 | fn code(&self) -> DiagnosticCode { | ||
78 | DiagnosticCode("inactive-code") | ||
79 | } | ||
80 | fn message(&self) -> String { | ||
81 | let inactive = DnfExpr::new(self.cfg.clone()).why_inactive(&self.opts); | ||
82 | let mut buf = "code is inactive due to #[cfg] directives".to_string(); | ||
83 | |||
84 | if let Some(inactive) = inactive { | ||
85 | format_to!(buf, ": {}", inactive); | ||
86 | } | ||
87 | |||
88 | buf | ||
89 | } | ||
90 | fn display_source(&self) -> InFile<SyntaxNodePtr> { | ||
91 | InFile::new(self.file, self.node.clone()) | ||
92 | } | ||
93 | fn as_any(&self) -> &(dyn Any + Send + 'static) { | ||
94 | self | ||
95 | } | ||
96 | } | ||
97 | |||
98 | // Diagnostic: unresolved-proc-macro | ||
99 | // | ||
100 | // This diagnostic is shown when a procedural macro can not be found. This usually means that | ||
101 | // procedural macro support is simply disabled (and hence is only a weak hint instead of an error), | ||
102 | // but can also indicate project setup problems. | ||
103 | // | ||
104 | // If you are seeing a lot of "proc macro not expanded" warnings, you can add this option to the | ||
105 | // `rust-analyzer.diagnostics.disabled` list to prevent them from showing. Alternatively you can | ||
106 | // enable support for procedural macros (see `rust-analyzer.procMacro.enable`). | ||
107 | #[derive(Debug, Clone, Eq, PartialEq)] | 74 | #[derive(Debug, Clone, Eq, PartialEq)] |
108 | pub struct UnresolvedProcMacro { | 75 | pub struct UnresolvedProcMacro { |
109 | pub file: HirFileId, | 76 | pub node: InFile<SyntaxNodePtr>, |
110 | pub node: SyntaxNodePtr, | ||
111 | /// If the diagnostic can be pinpointed more accurately than via `node`, this is the `TextRange` | 77 | /// If the diagnostic can be pinpointed more accurately than via `node`, this is the `TextRange` |
112 | /// to use instead. | 78 | /// to use instead. |
113 | pub precise_location: Option<TextRange>, | 79 | pub precise_location: Option<TextRange>, |
114 | pub macro_name: Option<String>, | 80 | pub macro_name: Option<String>, |
115 | } | 81 | } |
116 | 82 | ||
117 | impl Diagnostic for UnresolvedProcMacro { | ||
118 | fn code(&self) -> DiagnosticCode { | ||
119 | DiagnosticCode("unresolved-proc-macro") | ||
120 | } | ||
121 | |||
122 | fn message(&self) -> String { | ||
123 | match &self.macro_name { | ||
124 | Some(name) => format!("proc macro `{}` not expanded", name), | ||
125 | None => "proc macro not expanded".to_string(), | ||
126 | } | ||
127 | } | ||
128 | |||
129 | fn display_source(&self) -> InFile<SyntaxNodePtr> { | ||
130 | InFile::new(self.file, self.node.clone()) | ||
131 | } | ||
132 | |||
133 | fn as_any(&self) -> &(dyn Any + Send + 'static) { | ||
134 | self | ||
135 | } | ||
136 | } | ||
137 | |||
138 | // Diagnostic: macro-error | ||
139 | // | ||
140 | // This diagnostic is shown for macro expansion errors. | ||
141 | #[derive(Debug, Clone, Eq, PartialEq)] | 83 | #[derive(Debug, Clone, Eq, PartialEq)] |
142 | pub struct MacroError { | 84 | pub struct MacroError { |
143 | pub file: HirFileId, | 85 | pub node: InFile<SyntaxNodePtr>, |
144 | pub node: SyntaxNodePtr, | ||
145 | pub message: String, | 86 | pub message: String, |
146 | } | 87 | } |
147 | 88 | ||
148 | impl Diagnostic for MacroError { | ||
149 | fn code(&self) -> DiagnosticCode { | ||
150 | DiagnosticCode("macro-error") | ||
151 | } | ||
152 | fn message(&self) -> String { | ||
153 | self.message.clone() | ||
154 | } | ||
155 | fn display_source(&self) -> InFile<SyntaxNodePtr> { | ||
156 | InFile::new(self.file, self.node.clone()) | ||
157 | } | ||
158 | fn as_any(&self) -> &(dyn Any + Send + 'static) { | ||
159 | self | ||
160 | } | ||
161 | fn is_experimental(&self) -> bool { | ||
162 | // Newly added and not very well-tested, might contain false positives. | ||
163 | true | ||
164 | } | ||
165 | } | ||
166 | |||
167 | #[derive(Debug)] | 89 | #[derive(Debug)] |
168 | pub struct UnimplementedBuiltinMacro { | 90 | pub struct UnimplementedBuiltinMacro { |
169 | pub file: HirFileId, | 91 | pub file: HirFileId, |