diff options
Diffstat (limited to 'crates/hir_def')
-rw-r--r-- | crates/hir_def/src/body.rs | 25 | ||||
-rw-r--r-- | crates/hir_def/src/body/diagnostics.rs | 32 | ||||
-rw-r--r-- | crates/hir_def/src/body/lower.rs | 45 | ||||
-rw-r--r-- | crates/hir_def/src/body/tests.rs | 16 | ||||
-rw-r--r-- | crates/hir_def/src/diagnostics.rs | 227 | ||||
-rw-r--r-- | crates/hir_def/src/lib.rs | 17 | ||||
-rw-r--r-- | crates/hir_def/src/nameres.rs | 252 | ||||
-rw-r--r-- | crates/hir_def/src/nameres/collector.rs | 7 | ||||
-rw-r--r-- | crates/hir_def/src/nameres/diagnostics.rs | 90 | ||||
-rw-r--r-- | crates/hir_def/src/nameres/tests/diagnostics.rs | 57 | ||||
-rw-r--r-- | crates/hir_def/src/path.rs | 2 | ||||
-rw-r--r-- | crates/hir_def/src/test_db.rs | 83 |
12 files changed, 232 insertions, 621 deletions
diff --git a/crates/hir_def/src/body.rs b/crates/hir_def/src/body.rs index 98b485b60..c521879c8 100644 --- a/crates/hir_def/src/body.rs +++ b/crates/hir_def/src/body.rs | |||
@@ -1,7 +1,6 @@ | |||
1 | //! Defines `Body`: a lowered representation of bodies of functions, statics and | 1 | //! Defines `Body`: a lowered representation of bodies of functions, statics and |
2 | //! consts. | 2 | //! consts. |
3 | mod lower; | 3 | mod lower; |
4 | mod diagnostics; | ||
5 | #[cfg(test)] | 4 | #[cfg(test)] |
6 | mod tests; | 5 | mod tests; |
7 | pub mod scope; | 6 | pub mod scope; |
@@ -9,17 +8,16 @@ pub mod scope; | |||
9 | use std::{mem, ops::Index, sync::Arc}; | 8 | use std::{mem, ops::Index, sync::Arc}; |
10 | 9 | ||
11 | use base_db::CrateId; | 10 | use base_db::CrateId; |
12 | use cfg::CfgOptions; | 11 | use cfg::{CfgExpr, CfgOptions}; |
13 | use drop_bomb::DropBomb; | 12 | use drop_bomb::DropBomb; |
14 | use either::Either; | 13 | use either::Either; |
15 | use hir_expand::{ | 14 | use hir_expand::{ |
16 | ast_id_map::AstIdMap, diagnostics::DiagnosticSink, hygiene::Hygiene, AstId, ExpandResult, | 15 | ast_id_map::AstIdMap, hygiene::Hygiene, AstId, ExpandResult, HirFileId, InFile, MacroDefId, |
17 | HirFileId, InFile, MacroDefId, | ||
18 | }; | 16 | }; |
19 | use la_arena::{Arena, ArenaMap}; | 17 | use la_arena::{Arena, ArenaMap}; |
20 | use profile::Count; | 18 | use profile::Count; |
21 | use rustc_hash::FxHashMap; | 19 | use rustc_hash::FxHashMap; |
22 | use syntax::{ast, AstNode, AstPtr}; | 20 | use syntax::{ast, AstNode, AstPtr, SyntaxNodePtr}; |
23 | 21 | ||
24 | use crate::{ | 22 | use crate::{ |
25 | attr::{Attrs, RawAttrs}, | 23 | attr::{Attrs, RawAttrs}, |
@@ -273,12 +271,20 @@ pub struct BodySourceMap { | |||
273 | 271 | ||
274 | /// Diagnostics accumulated during body lowering. These contain `AstPtr`s and so are stored in | 272 | /// Diagnostics accumulated during body lowering. These contain `AstPtr`s and so are stored in |
275 | /// the source map (since they're just as volatile). | 273 | /// the source map (since they're just as volatile). |
276 | diagnostics: Vec<diagnostics::BodyDiagnostic>, | 274 | diagnostics: Vec<BodyDiagnostic>, |
277 | } | 275 | } |
278 | 276 | ||
279 | #[derive(Default, Debug, Eq, PartialEq, Clone, Copy)] | 277 | #[derive(Default, Debug, Eq, PartialEq, Clone, Copy)] |
280 | pub struct SyntheticSyntax; | 278 | pub struct SyntheticSyntax; |
281 | 279 | ||
280 | #[derive(Debug, Eq, PartialEq)] | ||
281 | pub enum BodyDiagnostic { | ||
282 | InactiveCode { node: InFile<SyntaxNodePtr>, cfg: CfgExpr, opts: CfgOptions }, | ||
283 | MacroError { node: InFile<AstPtr<ast::MacroCall>>, message: String }, | ||
284 | UnresolvedProcMacro { node: InFile<AstPtr<ast::MacroCall>> }, | ||
285 | UnresolvedMacroCall { node: InFile<AstPtr<ast::MacroCall>>, path: ModPath }, | ||
286 | } | ||
287 | |||
282 | impl Body { | 288 | impl Body { |
283 | pub(crate) fn body_with_source_map_query( | 289 | pub(crate) fn body_with_source_map_query( |
284 | db: &dyn DefDatabase, | 290 | db: &dyn DefDatabase, |
@@ -416,9 +422,8 @@ impl BodySourceMap { | |||
416 | self.field_map.get(&src).cloned() | 422 | self.field_map.get(&src).cloned() |
417 | } | 423 | } |
418 | 424 | ||
419 | pub(crate) fn add_diagnostics(&self, _db: &dyn DefDatabase, sink: &mut DiagnosticSink<'_>) { | 425 | /// Get a reference to the body source map's diagnostics. |
420 | for diag in &self.diagnostics { | 426 | pub fn diagnostics(&self) -> &[BodyDiagnostic] { |
421 | diag.add_to(sink); | 427 | &self.diagnostics |
422 | } | ||
423 | } | 428 | } |
424 | } | 429 | } |
diff --git a/crates/hir_def/src/body/diagnostics.rs b/crates/hir_def/src/body/diagnostics.rs deleted file mode 100644 index f6992c9a8..000000000 --- a/crates/hir_def/src/body/diagnostics.rs +++ /dev/null | |||
@@ -1,32 +0,0 @@ | |||
1 | //! Diagnostics emitted during body lowering. | ||
2 | |||
3 | use hir_expand::diagnostics::DiagnosticSink; | ||
4 | |||
5 | use crate::diagnostics::{InactiveCode, MacroError, UnresolvedMacroCall, UnresolvedProcMacro}; | ||
6 | |||
7 | #[derive(Debug, Eq, PartialEq)] | ||
8 | pub(crate) enum BodyDiagnostic { | ||
9 | InactiveCode(InactiveCode), | ||
10 | MacroError(MacroError), | ||
11 | UnresolvedProcMacro(UnresolvedProcMacro), | ||
12 | UnresolvedMacroCall(UnresolvedMacroCall), | ||
13 | } | ||
14 | |||
15 | impl BodyDiagnostic { | ||
16 | pub(crate) fn add_to(&self, sink: &mut DiagnosticSink<'_>) { | ||
17 | match self { | ||
18 | BodyDiagnostic::InactiveCode(diag) => { | ||
19 | sink.push(diag.clone()); | ||
20 | } | ||
21 | BodyDiagnostic::MacroError(diag) => { | ||
22 | sink.push(diag.clone()); | ||
23 | } | ||
24 | BodyDiagnostic::UnresolvedProcMacro(diag) => { | ||
25 | sink.push(diag.clone()); | ||
26 | } | ||
27 | BodyDiagnostic::UnresolvedMacroCall(diag) => { | ||
28 | sink.push(diag.clone()); | ||
29 | } | ||
30 | } | ||
31 | } | ||
32 | } | ||
diff --git a/crates/hir_def/src/body/lower.rs b/crates/hir_def/src/body/lower.rs index 2a7e0205f..da1fdac33 100644 --- a/crates/hir_def/src/body/lower.rs +++ b/crates/hir_def/src/body/lower.rs | |||
@@ -8,7 +8,7 @@ use hir_expand::{ | |||
8 | ast_id_map::{AstIdMap, FileAstId}, | 8 | ast_id_map::{AstIdMap, FileAstId}, |
9 | hygiene::Hygiene, | 9 | hygiene::Hygiene, |
10 | name::{name, AsName, Name}, | 10 | name::{name, AsName, Name}, |
11 | ExpandError, HirFileId, | 11 | ExpandError, HirFileId, InFile, |
12 | }; | 12 | }; |
13 | use la_arena::Arena; | 13 | use la_arena::Arena; |
14 | use profile::Count; | 14 | use profile::Count; |
@@ -23,9 +23,9 @@ use syntax::{ | |||
23 | use crate::{ | 23 | use crate::{ |
24 | adt::StructKind, | 24 | adt::StructKind, |
25 | body::{Body, BodySourceMap, Expander, LabelSource, PatPtr, SyntheticSyntax}, | 25 | body::{Body, BodySourceMap, Expander, LabelSource, PatPtr, SyntheticSyntax}, |
26 | body::{BodyDiagnostic, ExprSource, PatSource}, | ||
26 | builtin_type::{BuiltinFloat, BuiltinInt, BuiltinUint}, | 27 | builtin_type::{BuiltinFloat, BuiltinInt, BuiltinUint}, |
27 | db::DefDatabase, | 28 | db::DefDatabase, |
28 | diagnostics::{InactiveCode, MacroError, UnresolvedMacroCall, UnresolvedProcMacro}, | ||
29 | expr::{ | 29 | expr::{ |
30 | dummy_expr_id, ArithOp, Array, BinaryOp, BindingAnnotation, CmpOp, Expr, ExprId, Label, | 30 | dummy_expr_id, ArithOp, Array, BinaryOp, BindingAnnotation, CmpOp, Expr, ExprId, Label, |
31 | LabelId, Literal, LogicOp, MatchArm, Ordering, Pat, PatId, RecordFieldPat, RecordLitField, | 31 | LabelId, Literal, LogicOp, MatchArm, Ordering, Pat, PatId, RecordFieldPat, RecordLitField, |
@@ -38,8 +38,6 @@ use crate::{ | |||
38 | AdtId, BlockLoc, ModuleDefId, UnresolvedMacro, | 38 | AdtId, BlockLoc, ModuleDefId, UnresolvedMacro, |
39 | }; | 39 | }; |
40 | 40 | ||
41 | use super::{diagnostics::BodyDiagnostic, ExprSource, PatSource}; | ||
42 | |||
43 | pub struct LowerCtx<'a> { | 41 | pub struct LowerCtx<'a> { |
44 | pub db: &'a dyn DefDatabase, | 42 | pub db: &'a dyn DefDatabase, |
45 | hygiene: Hygiene, | 43 | hygiene: Hygiene, |
@@ -592,13 +590,10 @@ impl ExprCollector<'_> { | |||
592 | let res = match res { | 590 | let res = match res { |
593 | Ok(res) => res, | 591 | Ok(res) => res, |
594 | Err(UnresolvedMacro { path }) => { | 592 | Err(UnresolvedMacro { path }) => { |
595 | self.source_map.diagnostics.push(BodyDiagnostic::UnresolvedMacroCall( | 593 | self.source_map.diagnostics.push(BodyDiagnostic::UnresolvedMacroCall { |
596 | UnresolvedMacroCall { | 594 | node: InFile::new(outer_file, syntax_ptr), |
597 | file: outer_file, | 595 | path, |
598 | node: syntax_ptr.cast().unwrap(), | 596 | }); |
599 | path, | ||
600 | }, | ||
601 | )); | ||
602 | collector(self, None); | 597 | collector(self, None); |
603 | return; | 598 | return; |
604 | } | 599 | } |
@@ -606,21 +601,15 @@ impl ExprCollector<'_> { | |||
606 | 601 | ||
607 | match &res.err { | 602 | match &res.err { |
608 | Some(ExpandError::UnresolvedProcMacro) => { | 603 | Some(ExpandError::UnresolvedProcMacro) => { |
609 | self.source_map.diagnostics.push(BodyDiagnostic::UnresolvedProcMacro( | 604 | self.source_map.diagnostics.push(BodyDiagnostic::UnresolvedProcMacro { |
610 | UnresolvedProcMacro { | 605 | node: InFile::new(outer_file, syntax_ptr), |
611 | file: outer_file, | 606 | }); |
612 | node: syntax_ptr.into(), | ||
613 | precise_location: None, | ||
614 | macro_name: None, | ||
615 | }, | ||
616 | )); | ||
617 | } | 607 | } |
618 | Some(err) => { | 608 | Some(err) => { |
619 | self.source_map.diagnostics.push(BodyDiagnostic::MacroError(MacroError { | 609 | self.source_map.diagnostics.push(BodyDiagnostic::MacroError { |
620 | file: outer_file, | 610 | node: InFile::new(outer_file, syntax_ptr), |
621 | node: syntax_ptr.into(), | ||
622 | message: err.to_string(), | 611 | message: err.to_string(), |
623 | })); | 612 | }); |
624 | } | 613 | } |
625 | None => {} | 614 | None => {} |
626 | } | 615 | } |
@@ -945,12 +934,14 @@ impl ExprCollector<'_> { | |||
945 | return Some(()); | 934 | return Some(()); |
946 | } | 935 | } |
947 | 936 | ||
948 | self.source_map.diagnostics.push(BodyDiagnostic::InactiveCode(InactiveCode { | 937 | self.source_map.diagnostics.push(BodyDiagnostic::InactiveCode { |
949 | file: self.expander.current_file_id, | 938 | node: InFile::new( |
950 | node: SyntaxNodePtr::new(owner.syntax()), | 939 | self.expander.current_file_id, |
940 | SyntaxNodePtr::new(owner.syntax()), | ||
941 | ), | ||
951 | cfg, | 942 | cfg, |
952 | opts: self.expander.cfg_options().clone(), | 943 | opts: self.expander.cfg_options().clone(), |
953 | })); | 944 | }); |
954 | 945 | ||
955 | None | 946 | None |
956 | } | 947 | } |
diff --git a/crates/hir_def/src/body/tests.rs b/crates/hir_def/src/body/tests.rs index 3e8f16306..d4fae05a6 100644 --- a/crates/hir_def/src/body/tests.rs +++ b/crates/hir_def/src/body/tests.rs | |||
@@ -96,26 +96,26 @@ fn f() { | |||
96 | // The three g̶e̶n̶d̶e̶r̶s̶ statements: | 96 | // The three g̶e̶n̶d̶e̶r̶s̶ statements: |
97 | 97 | ||
98 | #[cfg(a)] fn f() {} // Item statement | 98 | #[cfg(a)] fn f() {} // Item statement |
99 | //^^^^^^^^^^^^^^^^^^^ code is inactive due to #[cfg] directives: a is disabled | 99 | //^^^^^^^^^^^^^^^^^^^ InactiveCode |
100 | #[cfg(a)] {} // Expression statement | 100 | #[cfg(a)] {} // Expression statement |
101 | //^^^^^^^^^^^^ code is inactive due to #[cfg] directives: a is disabled | 101 | //^^^^^^^^^^^^ InactiveCode |
102 | #[cfg(a)] let x = 0; // let statement | 102 | #[cfg(a)] let x = 0; // let statement |
103 | //^^^^^^^^^^^^^^^^^^^^ code is inactive due to #[cfg] directives: a is disabled | 103 | //^^^^^^^^^^^^^^^^^^^^ InactiveCode |
104 | 104 | ||
105 | abc(#[cfg(a)] 0); | 105 | abc(#[cfg(a)] 0); |
106 | //^^^^^^^^^^^ code is inactive due to #[cfg] directives: a is disabled | 106 | //^^^^^^^^^^^ InactiveCode |
107 | let x = Struct { | 107 | let x = Struct { |
108 | #[cfg(a)] f: 0, | 108 | #[cfg(a)] f: 0, |
109 | //^^^^^^^^^^^^^^ code is inactive due to #[cfg] directives: a is disabled | 109 | //^^^^^^^^^^^^^^ InactiveCode |
110 | }; | 110 | }; |
111 | match () { | 111 | match () { |
112 | () => (), | 112 | () => (), |
113 | #[cfg(a)] () => (), | 113 | #[cfg(a)] () => (), |
114 | //^^^^^^^^^^^^^^^^^^ code is inactive due to #[cfg] directives: a is disabled | 114 | //^^^^^^^^^^^^^^^^^^ InactiveCode |
115 | } | 115 | } |
116 | 116 | ||
117 | #[cfg(a)] 0 // Trailing expression of block | 117 | #[cfg(a)] 0 // Trailing expression of block |
118 | //^^^^^^^^^^^ code is inactive due to #[cfg] directives: a is disabled | 118 | //^^^^^^^^^^^ InactiveCode |
119 | } | 119 | } |
120 | ", | 120 | ", |
121 | ); | 121 | ); |
@@ -188,7 +188,7 @@ fn unresolved_macro_diag() { | |||
188 | r#" | 188 | r#" |
189 | fn f() { | 189 | fn f() { |
190 | m!(); | 190 | m!(); |
191 | //^^^^ unresolved macro `m!` | 191 | //^^^^ UnresolvedMacroCall |
192 | } | 192 | } |
193 | "#, | 193 | "#, |
194 | ); | 194 | ); |
diff --git a/crates/hir_def/src/diagnostics.rs b/crates/hir_def/src/diagnostics.rs deleted file mode 100644 index a71ae2668..000000000 --- a/crates/hir_def/src/diagnostics.rs +++ /dev/null | |||
@@ -1,227 +0,0 @@ | |||
1 | //! Diagnostics produced by `hir_def`. | ||
2 | |||
3 | use std::any::Any; | ||
4 | use stdx::format_to; | ||
5 | |||
6 | use cfg::{CfgExpr, CfgOptions, DnfExpr}; | ||
7 | use hir_expand::diagnostics::{Diagnostic, DiagnosticCode, DiagnosticSink}; | ||
8 | use hir_expand::{HirFileId, InFile}; | ||
9 | use syntax::{ast, AstPtr, SyntaxNodePtr, TextRange}; | ||
10 | |||
11 | use crate::{db::DefDatabase, path::ModPath, DefWithBodyId}; | ||
12 | |||
13 | pub fn validate_body(db: &dyn DefDatabase, owner: DefWithBodyId, sink: &mut DiagnosticSink<'_>) { | ||
14 | let source_map = db.body_with_source_map(owner).1; | ||
15 | source_map.add_diagnostics(db, sink); | ||
16 | } | ||
17 | |||
18 | // Diagnostic: unresolved-module | ||
19 | // | ||
20 | // This diagnostic is triggered if rust-analyzer is unable to discover referred module. | ||
21 | #[derive(Debug)] | ||
22 | pub struct UnresolvedModule { | ||
23 | pub file: HirFileId, | ||
24 | pub decl: AstPtr<ast::Module>, | ||
25 | pub candidate: String, | ||
26 | } | ||
27 | |||
28 | impl Diagnostic for UnresolvedModule { | ||
29 | fn code(&self) -> DiagnosticCode { | ||
30 | DiagnosticCode("unresolved-module") | ||
31 | } | ||
32 | fn message(&self) -> String { | ||
33 | "unresolved module".to_string() | ||
34 | } | ||
35 | fn display_source(&self) -> InFile<SyntaxNodePtr> { | ||
36 | InFile::new(self.file, self.decl.clone().into()) | ||
37 | } | ||
38 | fn as_any(&self) -> &(dyn Any + Send + 'static) { | ||
39 | self | ||
40 | } | ||
41 | } | ||
42 | |||
43 | // Diagnostic: unresolved-extern-crate | ||
44 | // | ||
45 | // This diagnostic is triggered if rust-analyzer is unable to discover referred extern crate. | ||
46 | #[derive(Debug)] | ||
47 | pub struct UnresolvedExternCrate { | ||
48 | pub file: HirFileId, | ||
49 | pub item: AstPtr<ast::ExternCrate>, | ||
50 | } | ||
51 | |||
52 | impl Diagnostic for UnresolvedExternCrate { | ||
53 | fn code(&self) -> DiagnosticCode { | ||
54 | DiagnosticCode("unresolved-extern-crate") | ||
55 | } | ||
56 | fn message(&self) -> String { | ||
57 | "unresolved extern crate".to_string() | ||
58 | } | ||
59 | fn display_source(&self) -> InFile<SyntaxNodePtr> { | ||
60 | InFile::new(self.file, self.item.clone().into()) | ||
61 | } | ||
62 | fn as_any(&self) -> &(dyn Any + Send + 'static) { | ||
63 | self | ||
64 | } | ||
65 | } | ||
66 | |||
67 | // Diagnostic: unresolved-import | ||
68 | // | ||
69 | // This diagnostic is triggered if rust-analyzer is unable to discover imported module. | ||
70 | #[derive(Debug)] | ||
71 | pub struct UnresolvedImport { | ||
72 | pub file: HirFileId, | ||
73 | pub node: AstPtr<ast::UseTree>, | ||
74 | } | ||
75 | |||
76 | impl Diagnostic for UnresolvedImport { | ||
77 | fn code(&self) -> DiagnosticCode { | ||
78 | DiagnosticCode("unresolved-import") | ||
79 | } | ||
80 | fn message(&self) -> String { | ||
81 | "unresolved import".to_string() | ||
82 | } | ||
83 | fn display_source(&self) -> InFile<SyntaxNodePtr> { | ||
84 | InFile::new(self.file, self.node.clone().into()) | ||
85 | } | ||
86 | fn as_any(&self) -> &(dyn Any + Send + 'static) { | ||
87 | self | ||
88 | } | ||
89 | fn is_experimental(&self) -> bool { | ||
90 | // This currently results in false positives in the following cases: | ||
91 | // - `cfg_if!`-generated code in libstd (we don't load the sysroot correctly) | ||
92 | // - `core::arch` (we don't handle `#[path = "../<path>"]` correctly) | ||
93 | // - proc macros and/or proc macro generated code | ||
94 | true | ||
95 | } | ||
96 | } | ||
97 | |||
98 | // Diagnostic: unresolved-macro-call | ||
99 | // | ||
100 | // This diagnostic is triggered if rust-analyzer is unable to resolve the path to a | ||
101 | // macro in a macro invocation. | ||
102 | #[derive(Debug, Clone, Eq, PartialEq)] | ||
103 | pub struct UnresolvedMacroCall { | ||
104 | pub file: HirFileId, | ||
105 | pub node: AstPtr<ast::MacroCall>, | ||
106 | pub path: ModPath, | ||
107 | } | ||
108 | |||
109 | impl Diagnostic for UnresolvedMacroCall { | ||
110 | fn code(&self) -> DiagnosticCode { | ||
111 | DiagnosticCode("unresolved-macro-call") | ||
112 | } | ||
113 | fn message(&self) -> String { | ||
114 | format!("unresolved macro `{}!`", self.path) | ||
115 | } | ||
116 | fn display_source(&self) -> InFile<SyntaxNodePtr> { | ||
117 | InFile::new(self.file, self.node.clone().into()) | ||
118 | } | ||
119 | fn as_any(&self) -> &(dyn Any + Send + 'static) { | ||
120 | self | ||
121 | } | ||
122 | fn is_experimental(&self) -> bool { | ||
123 | true | ||
124 | } | ||
125 | } | ||
126 | |||
127 | // Diagnostic: inactive-code | ||
128 | // | ||
129 | // This diagnostic is shown for code with inactive `#[cfg]` attributes. | ||
130 | #[derive(Debug, Clone, Eq, PartialEq)] | ||
131 | pub struct InactiveCode { | ||
132 | pub file: HirFileId, | ||
133 | pub node: SyntaxNodePtr, | ||
134 | pub cfg: CfgExpr, | ||
135 | pub opts: CfgOptions, | ||
136 | } | ||
137 | |||
138 | impl Diagnostic for InactiveCode { | ||
139 | fn code(&self) -> DiagnosticCode { | ||
140 | DiagnosticCode("inactive-code") | ||
141 | } | ||
142 | fn message(&self) -> String { | ||
143 | let inactive = DnfExpr::new(self.cfg.clone()).why_inactive(&self.opts); | ||
144 | let mut buf = "code is inactive due to #[cfg] directives".to_string(); | ||
145 | |||
146 | if let Some(inactive) = inactive { | ||
147 | format_to!(buf, ": {}", inactive); | ||
148 | } | ||
149 | |||
150 | buf | ||
151 | } | ||
152 | fn display_source(&self) -> InFile<SyntaxNodePtr> { | ||
153 | InFile::new(self.file, self.node.clone()) | ||
154 | } | ||
155 | fn as_any(&self) -> &(dyn Any + Send + 'static) { | ||
156 | self | ||
157 | } | ||
158 | } | ||
159 | |||
160 | // Diagnostic: unresolved-proc-macro | ||
161 | // | ||
162 | // This diagnostic is shown when a procedural macro can not be found. This usually means that | ||
163 | // procedural macro support is simply disabled (and hence is only a weak hint instead of an error), | ||
164 | // but can also indicate project setup problems. | ||
165 | // | ||
166 | // If you are seeing a lot of "proc macro not expanded" warnings, you can add this option to the | ||
167 | // `rust-analyzer.diagnostics.disabled` list to prevent them from showing. Alternatively you can | ||
168 | // enable support for procedural macros (see `rust-analyzer.procMacro.enable`). | ||
169 | #[derive(Debug, Clone, Eq, PartialEq)] | ||
170 | pub struct UnresolvedProcMacro { | ||
171 | pub file: HirFileId, | ||
172 | pub node: SyntaxNodePtr, | ||
173 | /// If the diagnostic can be pinpointed more accurately than via `node`, this is the `TextRange` | ||
174 | /// to use instead. | ||
175 | pub precise_location: Option<TextRange>, | ||
176 | pub macro_name: Option<String>, | ||
177 | } | ||
178 | |||
179 | impl Diagnostic for UnresolvedProcMacro { | ||
180 | fn code(&self) -> DiagnosticCode { | ||
181 | DiagnosticCode("unresolved-proc-macro") | ||
182 | } | ||
183 | |||
184 | fn message(&self) -> String { | ||
185 | match &self.macro_name { | ||
186 | Some(name) => format!("proc macro `{}` not expanded", name), | ||
187 | None => "proc macro not expanded".to_string(), | ||
188 | } | ||
189 | } | ||
190 | |||
191 | fn display_source(&self) -> InFile<SyntaxNodePtr> { | ||
192 | InFile::new(self.file, self.node.clone()) | ||
193 | } | ||
194 | |||
195 | fn as_any(&self) -> &(dyn Any + Send + 'static) { | ||
196 | self | ||
197 | } | ||
198 | } | ||
199 | |||
200 | // Diagnostic: macro-error | ||
201 | // | ||
202 | // This diagnostic is shown for macro expansion errors. | ||
203 | #[derive(Debug, Clone, Eq, PartialEq)] | ||
204 | pub struct MacroError { | ||
205 | pub file: HirFileId, | ||
206 | pub node: SyntaxNodePtr, | ||
207 | pub message: String, | ||
208 | } | ||
209 | |||
210 | impl Diagnostic for MacroError { | ||
211 | fn code(&self) -> DiagnosticCode { | ||
212 | DiagnosticCode("macro-error") | ||
213 | } | ||
214 | fn message(&self) -> String { | ||
215 | self.message.clone() | ||
216 | } | ||
217 | fn display_source(&self) -> InFile<SyntaxNodePtr> { | ||
218 | InFile::new(self.file, self.node.clone()) | ||
219 | } | ||
220 | fn as_any(&self) -> &(dyn Any + Send + 'static) { | ||
221 | self | ||
222 | } | ||
223 | fn is_experimental(&self) -> bool { | ||
224 | // Newly added and not very well-tested, might contain false positives. | ||
225 | true | ||
226 | } | ||
227 | } | ||
diff --git a/crates/hir_def/src/lib.rs b/crates/hir_def/src/lib.rs index 70001cac8..9aa95720a 100644 --- a/crates/hir_def/src/lib.rs +++ b/crates/hir_def/src/lib.rs | |||
@@ -19,7 +19,6 @@ pub mod path; | |||
19 | pub mod type_ref; | 19 | pub mod type_ref; |
20 | pub mod builtin_type; | 20 | pub mod builtin_type; |
21 | pub mod builtin_attr; | 21 | pub mod builtin_attr; |
22 | pub mod diagnostics; | ||
23 | pub mod per_ns; | 22 | pub mod per_ns; |
24 | pub mod item_scope; | 23 | pub mod item_scope; |
25 | 24 | ||
@@ -56,7 +55,6 @@ use std::{ | |||
56 | sync::Arc, | 55 | sync::Arc, |
57 | }; | 56 | }; |
58 | 57 | ||
59 | use adt::VariantData; | ||
60 | use base_db::{impl_intern_key, salsa, CrateId}; | 58 | use base_db::{impl_intern_key, salsa, CrateId}; |
61 | use hir_expand::{ | 59 | use hir_expand::{ |
62 | ast_id_map::FileAstId, | 60 | ast_id_map::FileAstId, |
@@ -67,15 +65,18 @@ use hir_expand::{ | |||
67 | use la_arena::Idx; | 65 | use la_arena::Idx; |
68 | use nameres::DefMap; | 66 | use nameres::DefMap; |
69 | use path::ModPath; | 67 | use path::ModPath; |
68 | use stdx::impl_from; | ||
70 | use syntax::ast; | 69 | use syntax::ast; |
71 | 70 | ||
72 | use crate::attr::AttrId; | 71 | use crate::{ |
73 | use crate::builtin_type::BuiltinType; | 72 | adt::VariantData, |
74 | use item_tree::{ | 73 | attr::AttrId, |
75 | Const, Enum, Function, Impl, ItemTreeId, ItemTreeNode, ModItem, Static, Struct, Trait, | 74 | builtin_type::BuiltinType, |
76 | TypeAlias, Union, | 75 | item_tree::{ |
76 | Const, Enum, Function, Impl, ItemTreeId, ItemTreeNode, ModItem, Static, Struct, Trait, | ||
77 | TypeAlias, Union, | ||
78 | }, | ||
77 | }; | 79 | }; |
78 | use stdx::impl_from; | ||
79 | 80 | ||
80 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 81 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
81 | pub struct ModuleId { | 82 | pub struct ModuleId { |
diff --git a/crates/hir_def/src/nameres.rs b/crates/hir_def/src/nameres.rs index 249af6fc8..ebfcc26c4 100644 --- a/crates/hir_def/src/nameres.rs +++ b/crates/hir_def/src/nameres.rs | |||
@@ -47,18 +47,19 @@ | |||
47 | //! path and, upon success, we run macro expansion and "collect module" phase on | 47 | //! path and, upon success, we run macro expansion and "collect module" phase on |
48 | //! the result | 48 | //! the result |
49 | 49 | ||
50 | pub mod diagnostics; | ||
50 | mod collector; | 51 | mod collector; |
51 | mod mod_resolution; | 52 | mod mod_resolution; |
52 | mod path_resolution; | 53 | mod path_resolution; |
54 | mod proc_macro; | ||
53 | 55 | ||
54 | #[cfg(test)] | 56 | #[cfg(test)] |
55 | mod tests; | 57 | mod tests; |
56 | mod proc_macro; | ||
57 | 58 | ||
58 | use std::sync::Arc; | 59 | use std::sync::Arc; |
59 | 60 | ||
60 | use base_db::{CrateId, Edition, FileId}; | 61 | use base_db::{CrateId, Edition, FileId}; |
61 | use hir_expand::{diagnostics::DiagnosticSink, name::Name, InFile, MacroDefId}; | 62 | use hir_expand::{name::Name, InFile, MacroDefId}; |
62 | use la_arena::Arena; | 63 | use la_arena::Arena; |
63 | use profile::Count; | 64 | use profile::Count; |
64 | use rustc_hash::FxHashMap; | 65 | use rustc_hash::FxHashMap; |
@@ -254,15 +255,6 @@ impl DefMap { | |||
254 | } | 255 | } |
255 | } | 256 | } |
256 | 257 | ||
257 | pub fn add_diagnostics( | ||
258 | &self, | ||
259 | db: &dyn DefDatabase, | ||
260 | module: LocalModuleId, | ||
261 | sink: &mut DiagnosticSink, | ||
262 | ) { | ||
263 | self.diagnostics.iter().for_each(|it| it.add_to(db, module, sink)) | ||
264 | } | ||
265 | |||
266 | pub fn modules_for_file(&self, file_id: FileId) -> impl Iterator<Item = LocalModuleId> + '_ { | 258 | pub fn modules_for_file(&self, file_id: FileId) -> impl Iterator<Item = LocalModuleId> + '_ { |
267 | self.modules | 259 | self.modules |
268 | .iter() | 260 | .iter() |
@@ -448,6 +440,11 @@ impl DefMap { | |||
448 | module.scope.shrink_to_fit(); | 440 | module.scope.shrink_to_fit(); |
449 | } | 441 | } |
450 | } | 442 | } |
443 | |||
444 | /// Get a reference to the def map's diagnostics. | ||
445 | pub fn diagnostics(&self) -> &[DefDiagnostic] { | ||
446 | self.diagnostics.as_slice() | ||
447 | } | ||
451 | } | 448 | } |
452 | 449 | ||
453 | impl ModuleData { | 450 | impl ModuleData { |
@@ -471,236 +468,3 @@ pub enum ModuleSource { | |||
471 | Module(ast::Module), | 468 | Module(ast::Module), |
472 | BlockExpr(ast::BlockExpr), | 469 | BlockExpr(ast::BlockExpr), |
473 | } | 470 | } |
474 | |||
475 | mod diagnostics { | ||
476 | use cfg::{CfgExpr, CfgOptions}; | ||
477 | use hir_expand::diagnostics::DiagnosticSink; | ||
478 | use hir_expand::hygiene::Hygiene; | ||
479 | use hir_expand::{InFile, MacroCallKind}; | ||
480 | use syntax::ast::AttrsOwner; | ||
481 | use syntax::{ast, AstNode, AstPtr, SyntaxKind, SyntaxNodePtr}; | ||
482 | |||
483 | use crate::path::ModPath; | ||
484 | use crate::{db::DefDatabase, diagnostics::*, nameres::LocalModuleId, AstId}; | ||
485 | |||
486 | #[derive(Debug, PartialEq, Eq)] | ||
487 | enum DiagnosticKind { | ||
488 | UnresolvedModule { declaration: AstId<ast::Module>, candidate: String }, | ||
489 | |||
490 | UnresolvedExternCrate { ast: AstId<ast::ExternCrate> }, | ||
491 | |||
492 | UnresolvedImport { ast: AstId<ast::Use>, index: usize }, | ||
493 | |||
494 | UnconfiguredCode { ast: AstId<ast::Item>, cfg: CfgExpr, opts: CfgOptions }, | ||
495 | |||
496 | UnresolvedProcMacro { ast: MacroCallKind }, | ||
497 | |||
498 | UnresolvedMacroCall { ast: AstId<ast::MacroCall>, path: ModPath }, | ||
499 | |||
500 | MacroError { ast: MacroCallKind, message: String }, | ||
501 | } | ||
502 | |||
503 | #[derive(Debug, PartialEq, Eq)] | ||
504 | pub(super) struct DefDiagnostic { | ||
505 | in_module: LocalModuleId, | ||
506 | kind: DiagnosticKind, | ||
507 | } | ||
508 | |||
509 | impl DefDiagnostic { | ||
510 | pub(super) fn unresolved_module( | ||
511 | container: LocalModuleId, | ||
512 | declaration: AstId<ast::Module>, | ||
513 | candidate: String, | ||
514 | ) -> Self { | ||
515 | Self { | ||
516 | in_module: container, | ||
517 | kind: DiagnosticKind::UnresolvedModule { declaration, candidate }, | ||
518 | } | ||
519 | } | ||
520 | |||
521 | pub(super) fn unresolved_extern_crate( | ||
522 | container: LocalModuleId, | ||
523 | declaration: AstId<ast::ExternCrate>, | ||
524 | ) -> Self { | ||
525 | Self { | ||
526 | in_module: container, | ||
527 | kind: DiagnosticKind::UnresolvedExternCrate { ast: declaration }, | ||
528 | } | ||
529 | } | ||
530 | |||
531 | pub(super) fn unresolved_import( | ||
532 | container: LocalModuleId, | ||
533 | ast: AstId<ast::Use>, | ||
534 | index: usize, | ||
535 | ) -> Self { | ||
536 | Self { in_module: container, kind: DiagnosticKind::UnresolvedImport { ast, index } } | ||
537 | } | ||
538 | |||
539 | pub(super) fn unconfigured_code( | ||
540 | container: LocalModuleId, | ||
541 | ast: AstId<ast::Item>, | ||
542 | cfg: CfgExpr, | ||
543 | opts: CfgOptions, | ||
544 | ) -> Self { | ||
545 | Self { in_module: container, kind: DiagnosticKind::UnconfiguredCode { ast, cfg, opts } } | ||
546 | } | ||
547 | |||
548 | pub(super) fn unresolved_proc_macro(container: LocalModuleId, ast: MacroCallKind) -> Self { | ||
549 | Self { in_module: container, kind: DiagnosticKind::UnresolvedProcMacro { ast } } | ||
550 | } | ||
551 | |||
552 | pub(super) fn macro_error( | ||
553 | container: LocalModuleId, | ||
554 | ast: MacroCallKind, | ||
555 | message: String, | ||
556 | ) -> Self { | ||
557 | Self { in_module: container, kind: DiagnosticKind::MacroError { ast, message } } | ||
558 | } | ||
559 | |||
560 | pub(super) fn unresolved_macro_call( | ||
561 | container: LocalModuleId, | ||
562 | ast: AstId<ast::MacroCall>, | ||
563 | path: ModPath, | ||
564 | ) -> Self { | ||
565 | Self { in_module: container, kind: DiagnosticKind::UnresolvedMacroCall { ast, path } } | ||
566 | } | ||
567 | |||
568 | pub(super) fn add_to( | ||
569 | &self, | ||
570 | db: &dyn DefDatabase, | ||
571 | target_module: LocalModuleId, | ||
572 | sink: &mut DiagnosticSink, | ||
573 | ) { | ||
574 | if self.in_module != target_module { | ||
575 | return; | ||
576 | } | ||
577 | |||
578 | match &self.kind { | ||
579 | DiagnosticKind::UnresolvedModule { declaration, candidate } => { | ||
580 | let decl = declaration.to_node(db.upcast()); | ||
581 | sink.push(UnresolvedModule { | ||
582 | file: declaration.file_id, | ||
583 | decl: AstPtr::new(&decl), | ||
584 | candidate: candidate.clone(), | ||
585 | }) | ||
586 | } | ||
587 | |||
588 | DiagnosticKind::UnresolvedExternCrate { ast } => { | ||
589 | let item = ast.to_node(db.upcast()); | ||
590 | sink.push(UnresolvedExternCrate { | ||
591 | file: ast.file_id, | ||
592 | item: AstPtr::new(&item), | ||
593 | }); | ||
594 | } | ||
595 | |||
596 | DiagnosticKind::UnresolvedImport { ast, index } => { | ||
597 | let use_item = ast.to_node(db.upcast()); | ||
598 | let hygiene = Hygiene::new(db.upcast(), ast.file_id); | ||
599 | let mut cur = 0; | ||
600 | let mut tree = None; | ||
601 | ModPath::expand_use_item( | ||
602 | db, | ||
603 | InFile::new(ast.file_id, use_item), | ||
604 | &hygiene, | ||
605 | |_mod_path, use_tree, _is_glob, _alias| { | ||
606 | if cur == *index { | ||
607 | tree = Some(use_tree.clone()); | ||
608 | } | ||
609 | |||
610 | cur += 1; | ||
611 | }, | ||
612 | ); | ||
613 | |||
614 | if let Some(tree) = tree { | ||
615 | sink.push(UnresolvedImport { file: ast.file_id, node: AstPtr::new(&tree) }); | ||
616 | } | ||
617 | } | ||
618 | |||
619 | DiagnosticKind::UnconfiguredCode { ast, cfg, opts } => { | ||
620 | let item = ast.to_node(db.upcast()); | ||
621 | sink.push(InactiveCode { | ||
622 | file: ast.file_id, | ||
623 | node: AstPtr::new(&item).into(), | ||
624 | cfg: cfg.clone(), | ||
625 | opts: opts.clone(), | ||
626 | }); | ||
627 | } | ||
628 | |||
629 | DiagnosticKind::UnresolvedProcMacro { ast } => { | ||
630 | let mut precise_location = None; | ||
631 | let (file, ast, name) = match ast { | ||
632 | MacroCallKind::FnLike { ast_id, .. } => { | ||
633 | let node = ast_id.to_node(db.upcast()); | ||
634 | (ast_id.file_id, SyntaxNodePtr::from(AstPtr::new(&node)), None) | ||
635 | } | ||
636 | MacroCallKind::Derive { ast_id, derive_name, .. } => { | ||
637 | let node = ast_id.to_node(db.upcast()); | ||
638 | |||
639 | // Compute the precise location of the macro name's token in the derive | ||
640 | // list. | ||
641 | // FIXME: This does not handle paths to the macro, but neither does the | ||
642 | // rest of r-a. | ||
643 | let derive_attrs = | ||
644 | node.attrs().filter_map(|attr| match attr.as_simple_call() { | ||
645 | Some((name, args)) if name == "derive" => Some(args), | ||
646 | _ => None, | ||
647 | }); | ||
648 | 'outer: for attr in derive_attrs { | ||
649 | let tokens = | ||
650 | attr.syntax().children_with_tokens().filter_map(|elem| { | ||
651 | match elem { | ||
652 | syntax::NodeOrToken::Node(_) => None, | ||
653 | syntax::NodeOrToken::Token(tok) => Some(tok), | ||
654 | } | ||
655 | }); | ||
656 | for token in tokens { | ||
657 | if token.kind() == SyntaxKind::IDENT | ||
658 | && token.text() == derive_name.as_str() | ||
659 | { | ||
660 | precise_location = Some(token.text_range()); | ||
661 | break 'outer; | ||
662 | } | ||
663 | } | ||
664 | } | ||
665 | |||
666 | ( | ||
667 | ast_id.file_id, | ||
668 | SyntaxNodePtr::from(AstPtr::new(&node)), | ||
669 | Some(derive_name.clone()), | ||
670 | ) | ||
671 | } | ||
672 | }; | ||
673 | sink.push(UnresolvedProcMacro { | ||
674 | file, | ||
675 | node: ast, | ||
676 | precise_location, | ||
677 | macro_name: name, | ||
678 | }); | ||
679 | } | ||
680 | |||
681 | DiagnosticKind::UnresolvedMacroCall { ast, path } => { | ||
682 | let node = ast.to_node(db.upcast()); | ||
683 | sink.push(UnresolvedMacroCall { | ||
684 | file: ast.file_id, | ||
685 | node: AstPtr::new(&node), | ||
686 | path: path.clone(), | ||
687 | }); | ||
688 | } | ||
689 | |||
690 | DiagnosticKind::MacroError { ast, message } => { | ||
691 | let (file, ast) = match ast { | ||
692 | MacroCallKind::FnLike { ast_id, .. } => { | ||
693 | let node = ast_id.to_node(db.upcast()); | ||
694 | (ast_id.file_id, SyntaxNodePtr::from(AstPtr::new(&node))) | ||
695 | } | ||
696 | MacroCallKind::Derive { ast_id, .. } => { | ||
697 | let node = ast_id.to_node(db.upcast()); | ||
698 | (ast_id.file_id, SyntaxNodePtr::from(AstPtr::new(&node))) | ||
699 | } | ||
700 | }; | ||
701 | sink.push(MacroError { file, node: ast, message: message.clone() }); | ||
702 | } | ||
703 | } | ||
704 | } | ||
705 | } | ||
706 | } | ||
diff --git a/crates/hir_def/src/nameres/collector.rs b/crates/hir_def/src/nameres/collector.rs index 2ae740d0e..3ea472908 100644 --- a/crates/hir_def/src/nameres/collector.rs +++ b/crates/hir_def/src/nameres/collector.rs | |||
@@ -33,7 +33,10 @@ use crate::{ | |||
33 | }, | 33 | }, |
34 | macro_call_as_call_id, | 34 | macro_call_as_call_id, |
35 | nameres::{ | 35 | nameres::{ |
36 | diagnostics::DefDiagnostic, mod_resolution::ModDir, path_resolution::ReachedFixedPoint, | 36 | diagnostics::DefDiagnostic, |
37 | mod_resolution::ModDir, | ||
38 | path_resolution::ReachedFixedPoint, | ||
39 | proc_macro::{ProcMacroDef, ProcMacroKind}, | ||
37 | BuiltinShadowMode, DefMap, ModuleData, ModuleOrigin, ResolveMode, | 40 | BuiltinShadowMode, DefMap, ModuleData, ModuleOrigin, ResolveMode, |
38 | }, | 41 | }, |
39 | path::{ImportAlias, ModPath, PathKind}, | 42 | path::{ImportAlias, ModPath, PathKind}, |
@@ -44,8 +47,6 @@ use crate::{ | |||
44 | UnresolvedMacro, | 47 | UnresolvedMacro, |
45 | }; | 48 | }; |
46 | 49 | ||
47 | use super::proc_macro::{ProcMacroDef, ProcMacroKind}; | ||
48 | |||
49 | const GLOB_RECURSION_LIMIT: usize = 100; | 50 | const GLOB_RECURSION_LIMIT: usize = 100; |
50 | const EXPANSION_DEPTH_LIMIT: usize = 128; | 51 | const EXPANSION_DEPTH_LIMIT: usize = 128; |
51 | const FIXED_POINT_LIMIT: usize = 8192; | 52 | const FIXED_POINT_LIMIT: usize = 8192; |
diff --git a/crates/hir_def/src/nameres/diagnostics.rs b/crates/hir_def/src/nameres/diagnostics.rs new file mode 100644 index 000000000..8f2f0ff9f --- /dev/null +++ b/crates/hir_def/src/nameres/diagnostics.rs | |||
@@ -0,0 +1,90 @@ | |||
1 | //! Diagnostics emitted during DefMap construction. | ||
2 | |||
3 | use cfg::{CfgExpr, CfgOptions}; | ||
4 | use hir_expand::MacroCallKind; | ||
5 | use syntax::ast; | ||
6 | |||
7 | use crate::{nameres::LocalModuleId, path::ModPath, AstId}; | ||
8 | |||
9 | #[derive(Debug, PartialEq, Eq)] | ||
10 | pub enum DefDiagnosticKind { | ||
11 | UnresolvedModule { ast: AstId<ast::Module>, candidate: String }, | ||
12 | |||
13 | UnresolvedExternCrate { ast: AstId<ast::ExternCrate> }, | ||
14 | |||
15 | UnresolvedImport { ast: AstId<ast::Use>, index: usize }, | ||
16 | |||
17 | UnconfiguredCode { ast: AstId<ast::Item>, cfg: CfgExpr, opts: CfgOptions }, | ||
18 | |||
19 | UnresolvedProcMacro { ast: MacroCallKind }, | ||
20 | |||
21 | UnresolvedMacroCall { ast: AstId<ast::MacroCall>, path: ModPath }, | ||
22 | |||
23 | MacroError { ast: MacroCallKind, message: String }, | ||
24 | } | ||
25 | |||
26 | #[derive(Debug, PartialEq, Eq)] | ||
27 | pub struct DefDiagnostic { | ||
28 | pub in_module: LocalModuleId, | ||
29 | pub kind: DefDiagnosticKind, | ||
30 | } | ||
31 | |||
32 | impl DefDiagnostic { | ||
33 | pub(super) fn unresolved_module( | ||
34 | container: LocalModuleId, | ||
35 | declaration: AstId<ast::Module>, | ||
36 | candidate: String, | ||
37 | ) -> Self { | ||
38 | Self { | ||
39 | in_module: container, | ||
40 | kind: DefDiagnosticKind::UnresolvedModule { ast: declaration, candidate }, | ||
41 | } | ||
42 | } | ||
43 | |||
44 | pub(super) fn unresolved_extern_crate( | ||
45 | container: LocalModuleId, | ||
46 | declaration: AstId<ast::ExternCrate>, | ||
47 | ) -> Self { | ||
48 | Self { | ||
49 | in_module: container, | ||
50 | kind: DefDiagnosticKind::UnresolvedExternCrate { ast: declaration }, | ||
51 | } | ||
52 | } | ||
53 | |||
54 | pub(super) fn unresolved_import( | ||
55 | container: LocalModuleId, | ||
56 | ast: AstId<ast::Use>, | ||
57 | index: usize, | ||
58 | ) -> Self { | ||
59 | Self { in_module: container, kind: DefDiagnosticKind::UnresolvedImport { ast, index } } | ||
60 | } | ||
61 | |||
62 | pub(super) fn unconfigured_code( | ||
63 | container: LocalModuleId, | ||
64 | ast: AstId<ast::Item>, | ||
65 | cfg: CfgExpr, | ||
66 | opts: CfgOptions, | ||
67 | ) -> Self { | ||
68 | Self { in_module: container, kind: DefDiagnosticKind::UnconfiguredCode { ast, cfg, opts } } | ||
69 | } | ||
70 | |||
71 | pub(super) fn unresolved_proc_macro(container: LocalModuleId, ast: MacroCallKind) -> Self { | ||
72 | Self { in_module: container, kind: DefDiagnosticKind::UnresolvedProcMacro { ast } } | ||
73 | } | ||
74 | |||
75 | pub(super) fn macro_error( | ||
76 | container: LocalModuleId, | ||
77 | ast: MacroCallKind, | ||
78 | message: String, | ||
79 | ) -> Self { | ||
80 | Self { in_module: container, kind: DefDiagnosticKind::MacroError { ast, message } } | ||
81 | } | ||
82 | |||
83 | pub(super) fn unresolved_macro_call( | ||
84 | container: LocalModuleId, | ||
85 | ast: AstId<ast::MacroCall>, | ||
86 | path: ModPath, | ||
87 | ) -> Self { | ||
88 | Self { in_module: container, kind: DefDiagnosticKind::UnresolvedMacroCall { ast, path } } | ||
89 | } | ||
90 | } | ||
diff --git a/crates/hir_def/src/nameres/tests/diagnostics.rs b/crates/hir_def/src/nameres/tests/diagnostics.rs index 75147d973..ec6670952 100644 --- a/crates/hir_def/src/nameres/tests/diagnostics.rs +++ b/crates/hir_def/src/nameres/tests/diagnostics.rs | |||
@@ -18,7 +18,7 @@ fn unresolved_import() { | |||
18 | r" | 18 | r" |
19 | use does_exist; | 19 | use does_exist; |
20 | use does_not_exist; | 20 | use does_not_exist; |
21 | //^^^^^^^^^^^^^^ unresolved import | 21 | //^^^^^^^^^^^^^^^^^^^ UnresolvedImport |
22 | 22 | ||
23 | mod does_exist {} | 23 | mod does_exist {} |
24 | ", | 24 | ", |
@@ -26,40 +26,13 @@ fn unresolved_import() { | |||
26 | } | 26 | } |
27 | 27 | ||
28 | #[test] | 28 | #[test] |
29 | fn unresolved_import_in_use_tree() { | ||
30 | // Only the relevant part of a nested `use` item should be highlighted. | ||
31 | check_diagnostics( | ||
32 | r" | ||
33 | use does_exist::{Exists, DoesntExist}; | ||
34 | //^^^^^^^^^^^ unresolved import | ||
35 | |||
36 | use {does_not_exist::*, does_exist}; | ||
37 | //^^^^^^^^^^^^^^^^^ unresolved import | ||
38 | |||
39 | use does_not_exist::{ | ||
40 | a, | ||
41 | //^ unresolved import | ||
42 | b, | ||
43 | //^ unresolved import | ||
44 | c, | ||
45 | //^ unresolved import | ||
46 | }; | ||
47 | |||
48 | mod does_exist { | ||
49 | pub struct Exists; | ||
50 | } | ||
51 | ", | ||
52 | ); | ||
53 | } | ||
54 | |||
55 | #[test] | ||
56 | fn unresolved_extern_crate() { | 29 | fn unresolved_extern_crate() { |
57 | check_diagnostics( | 30 | check_diagnostics( |
58 | r" | 31 | r" |
59 | //- /main.rs crate:main deps:core | 32 | //- /main.rs crate:main deps:core |
60 | extern crate core; | 33 | extern crate core; |
61 | extern crate doesnotexist; | 34 | extern crate doesnotexist; |
62 | //^^^^^^^^^^^^^^^^^^^^^^^^^^ unresolved extern crate | 35 | //^^^^^^^^^^^^^^^^^^^^^^^^^^ UnresolvedExternCrate |
63 | //- /lib.rs crate:core | 36 | //- /lib.rs crate:core |
64 | ", | 37 | ", |
65 | ); | 38 | ); |
@@ -72,7 +45,7 @@ fn extern_crate_self_as() { | |||
72 | r" | 45 | r" |
73 | //- /lib.rs | 46 | //- /lib.rs |
74 | extern crate doesnotexist; | 47 | extern crate doesnotexist; |
75 | //^^^^^^^^^^^^^^^^^^^^^^^^^^ unresolved extern crate | 48 | //^^^^^^^^^^^^^^^^^^^^^^^^^^ UnresolvedExternCrate |
76 | // Should not error. | 49 | // Should not error. |
77 | extern crate self as foo; | 50 | extern crate self as foo; |
78 | struct Foo; | 51 | struct Foo; |
@@ -88,18 +61,18 @@ fn dedup_unresolved_import_from_unresolved_crate() { | |||
88 | //- /main.rs crate:main | 61 | //- /main.rs crate:main |
89 | mod a { | 62 | mod a { |
90 | extern crate doesnotexist; | 63 | extern crate doesnotexist; |
91 | //^^^^^^^^^^^^^^^^^^^^^^^^^^ unresolved extern crate | 64 | //^^^^^^^^^^^^^^^^^^^^^^^^^^ UnresolvedExternCrate |
92 | 65 | ||
93 | // Should not error, since we already errored for the missing crate. | 66 | // Should not error, since we already errored for the missing crate. |
94 | use doesnotexist::{self, bla, *}; | 67 | use doesnotexist::{self, bla, *}; |
95 | 68 | ||
96 | use crate::doesnotexist; | 69 | use crate::doesnotexist; |
97 | //^^^^^^^^^^^^^^^^^^^ unresolved import | 70 | //^^^^^^^^^^^^^^^^^^^^^^^^ UnresolvedImport |
98 | } | 71 | } |
99 | 72 | ||
100 | mod m { | 73 | mod m { |
101 | use super::doesnotexist; | 74 | use super::doesnotexist; |
102 | //^^^^^^^^^^^^^^^^^^^ unresolved import | 75 | //^^^^^^^^^^^^^^^^^^^^^^^^ UnresolvedImport |
103 | } | 76 | } |
104 | ", | 77 | ", |
105 | ); | 78 | ); |
@@ -112,7 +85,7 @@ fn unresolved_module() { | |||
112 | //- /lib.rs | 85 | //- /lib.rs |
113 | mod foo; | 86 | mod foo; |
114 | mod bar; | 87 | mod bar; |
115 | //^^^^^^^^ unresolved module | 88 | //^^^^^^^^ UnresolvedModule |
116 | mod baz {} | 89 | mod baz {} |
117 | //- /foo.rs | 90 | //- /foo.rs |
118 | ", | 91 | ", |
@@ -127,16 +100,16 @@ fn inactive_item() { | |||
127 | r#" | 100 | r#" |
128 | //- /lib.rs | 101 | //- /lib.rs |
129 | #[cfg(no)] pub fn f() {} | 102 | #[cfg(no)] pub fn f() {} |
130 | //^^^^^^^^^^^^^^^^^^^^^^^^ code is inactive due to #[cfg] directives: no is disabled | 103 | //^^^^^^^^^^^^^^^^^^^^^^^^ UnconfiguredCode |
131 | 104 | ||
132 | #[cfg(no)] #[cfg(no2)] mod m; | 105 | #[cfg(no)] #[cfg(no2)] mod m; |
133 | //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ code is inactive due to #[cfg] directives: no and no2 are disabled | 106 | //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ UnconfiguredCode |
134 | 107 | ||
135 | #[cfg(all(not(a), b))] enum E {} | 108 | #[cfg(all(not(a), b))] enum E {} |
136 | //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ code is inactive due to #[cfg] directives: b is disabled | 109 | //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ UnconfiguredCode |
137 | 110 | ||
138 | #[cfg(feature = "std")] use std; | 111 | #[cfg(feature = "std")] use std; |
139 | //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ code is inactive due to #[cfg] directives: feature = "std" is disabled | 112 | //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ UnconfiguredCode |
140 | "#, | 113 | "#, |
141 | ); | 114 | ); |
142 | } | 115 | } |
@@ -149,14 +122,14 @@ fn inactive_via_cfg_attr() { | |||
149 | r#" | 122 | r#" |
150 | //- /lib.rs | 123 | //- /lib.rs |
151 | #[cfg_attr(not(never), cfg(no))] fn f() {} | 124 | #[cfg_attr(not(never), cfg(no))] fn f() {} |
152 | //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ code is inactive due to #[cfg] directives: no is disabled | 125 | //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ UnconfiguredCode |
153 | 126 | ||
154 | #[cfg_attr(not(never), cfg(not(no)))] fn f() {} | 127 | #[cfg_attr(not(never), cfg(not(no)))] fn f() {} |
155 | 128 | ||
156 | #[cfg_attr(never, cfg(no))] fn g() {} | 129 | #[cfg_attr(never, cfg(no))] fn g() {} |
157 | 130 | ||
158 | #[cfg_attr(not(never), inline, cfg(no))] fn h() {} | 131 | #[cfg_attr(not(never), inline, cfg(no))] fn h() {} |
159 | //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ code is inactive due to #[cfg] directives: no is disabled | 132 | //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ UnconfiguredCode |
160 | "#, | 133 | "#, |
161 | ); | 134 | ); |
162 | } | 135 | } |
@@ -170,7 +143,7 @@ fn unresolved_legacy_scope_macro() { | |||
170 | 143 | ||
171 | m!(); | 144 | m!(); |
172 | m2!(); | 145 | m2!(); |
173 | //^^^^^^ unresolved macro `self::m2!` | 146 | //^^^^^^ UnresolvedMacroCall |
174 | "#, | 147 | "#, |
175 | ); | 148 | ); |
176 | } | 149 | } |
@@ -187,7 +160,7 @@ fn unresolved_module_scope_macro() { | |||
187 | 160 | ||
188 | self::m!(); | 161 | self::m!(); |
189 | self::m2!(); | 162 | self::m2!(); |
190 | //^^^^^^^^^^^^ unresolved macro `self::m2!` | 163 | //^^^^^^^^^^^^ UnresolvedMacroCall |
191 | "#, | 164 | "#, |
192 | ); | 165 | ); |
193 | } | 166 | } |
diff --git a/crates/hir_def/src/path.rs b/crates/hir_def/src/path.rs index 45ab9d0ff..d9ec03d2d 100644 --- a/crates/hir_def/src/path.rs +++ b/crates/hir_def/src/path.rs | |||
@@ -71,7 +71,7 @@ impl ModPath { | |||
71 | } | 71 | } |
72 | 72 | ||
73 | /// Calls `cb` with all paths, represented by this use item. | 73 | /// Calls `cb` with all paths, represented by this use item. |
74 | pub(crate) fn expand_use_item( | 74 | pub fn expand_use_item( |
75 | db: &dyn DefDatabase, | 75 | db: &dyn DefDatabase, |
76 | item_src: InFile<ast::Use>, | 76 | item_src: InFile<ast::Use>, |
77 | hygiene: &Hygiene, | 77 | hygiene: &Hygiene, |
diff --git a/crates/hir_def/src/test_db.rs b/crates/hir_def/src/test_db.rs index 8fa703a57..6c357c915 100644 --- a/crates/hir_def/src/test_db.rs +++ b/crates/hir_def/src/test_db.rs | |||
@@ -5,19 +5,20 @@ use std::{ | |||
5 | sync::{Arc, Mutex}, | 5 | sync::{Arc, Mutex}, |
6 | }; | 6 | }; |
7 | 7 | ||
8 | use base_db::{salsa, CrateId, FileId, FileLoader, FileLoaderDelegate, FilePosition, Upcast}; | 8 | use base_db::{ |
9 | salsa, CrateId, FileId, FileLoader, FileLoaderDelegate, FilePosition, FileRange, Upcast, | ||
10 | }; | ||
9 | use base_db::{AnchoredPath, SourceDatabase}; | 11 | use base_db::{AnchoredPath, SourceDatabase}; |
10 | use hir_expand::diagnostics::Diagnostic; | ||
11 | use hir_expand::diagnostics::DiagnosticSinkBuilder; | ||
12 | use hir_expand::{db::AstDatabase, InFile}; | 12 | use hir_expand::{db::AstDatabase, InFile}; |
13 | use rustc_hash::FxHashMap; | 13 | use rustc_hash::FxHashMap; |
14 | use rustc_hash::FxHashSet; | 14 | use rustc_hash::FxHashSet; |
15 | use syntax::{algo, ast, AstNode, TextRange, TextSize}; | 15 | use syntax::{algo, ast, AstNode, SyntaxNode, SyntaxNodePtr, TextRange, TextSize}; |
16 | use test_utils::extract_annotations; | 16 | use test_utils::extract_annotations; |
17 | 17 | ||
18 | use crate::{ | 18 | use crate::{ |
19 | body::BodyDiagnostic, | ||
19 | db::DefDatabase, | 20 | db::DefDatabase, |
20 | nameres::{DefMap, ModuleSource}, | 21 | nameres::{diagnostics::DefDiagnosticKind, DefMap, ModuleSource}, |
21 | src::HasSource, | 22 | src::HasSource, |
22 | LocalModuleId, Lookup, ModuleDefId, ModuleId, | 23 | LocalModuleId, Lookup, ModuleDefId, ModuleId, |
23 | }; | 24 | }; |
@@ -262,19 +263,70 @@ impl TestDB { | |||
262 | .collect() | 263 | .collect() |
263 | } | 264 | } |
264 | 265 | ||
265 | pub(crate) fn diagnostics<F: FnMut(&dyn Diagnostic)>(&self, mut cb: F) { | 266 | pub(crate) fn diagnostics(&self, cb: &mut dyn FnMut(FileRange, String)) { |
266 | let crate_graph = self.crate_graph(); | 267 | let crate_graph = self.crate_graph(); |
267 | for krate in crate_graph.iter() { | 268 | for krate in crate_graph.iter() { |
268 | let crate_def_map = self.crate_def_map(krate); | 269 | let crate_def_map = self.crate_def_map(krate); |
269 | 270 | ||
270 | let mut sink = DiagnosticSinkBuilder::new().build(&mut cb); | 271 | for diag in crate_def_map.diagnostics() { |
271 | for (module_id, module) in crate_def_map.modules() { | 272 | let (node, message): (InFile<SyntaxNode>, &str) = match &diag.kind { |
272 | crate_def_map.add_diagnostics(self, module_id, &mut sink); | 273 | DefDiagnosticKind::UnresolvedModule { ast, .. } => { |
274 | let node = ast.to_node(self.upcast()); | ||
275 | (InFile::new(ast.file_id, node.syntax().clone()), "UnresolvedModule") | ||
276 | } | ||
277 | DefDiagnosticKind::UnresolvedExternCrate { ast, .. } => { | ||
278 | let node = ast.to_node(self.upcast()); | ||
279 | (InFile::new(ast.file_id, node.syntax().clone()), "UnresolvedExternCrate") | ||
280 | } | ||
281 | DefDiagnosticKind::UnresolvedImport { ast, .. } => { | ||
282 | let node = ast.to_node(self.upcast()); | ||
283 | (InFile::new(ast.file_id, node.syntax().clone()), "UnresolvedImport") | ||
284 | } | ||
285 | DefDiagnosticKind::UnconfiguredCode { ast, .. } => { | ||
286 | let node = ast.to_node(self.upcast()); | ||
287 | (InFile::new(ast.file_id, node.syntax().clone()), "UnconfiguredCode") | ||
288 | } | ||
289 | DefDiagnosticKind::UnresolvedProcMacro { ast, .. } => { | ||
290 | (ast.to_node(self.upcast()), "UnresolvedProcMacro") | ||
291 | } | ||
292 | DefDiagnosticKind::UnresolvedMacroCall { ast, .. } => { | ||
293 | let node = ast.to_node(self.upcast()); | ||
294 | (InFile::new(ast.file_id, node.syntax().clone()), "UnresolvedMacroCall") | ||
295 | } | ||
296 | DefDiagnosticKind::MacroError { ast, message } => { | ||
297 | (ast.to_node(self.upcast()), message.as_str()) | ||
298 | } | ||
299 | }; | ||
300 | |||
301 | let frange = node.as_ref().original_file_range(self); | ||
302 | cb(frange, message.to_string()) | ||
303 | } | ||
273 | 304 | ||
305 | for (_module_id, module) in crate_def_map.modules() { | ||
274 | for decl in module.scope.declarations() { | 306 | for decl in module.scope.declarations() { |
275 | if let ModuleDefId::FunctionId(it) = decl { | 307 | if let ModuleDefId::FunctionId(it) = decl { |
276 | let source_map = self.body_with_source_map(it.into()).1; | 308 | let source_map = self.body_with_source_map(it.into()).1; |
277 | source_map.add_diagnostics(self, &mut sink); | 309 | for diag in source_map.diagnostics() { |
310 | let (ptr, message): (InFile<SyntaxNodePtr>, &str) = match diag { | ||
311 | BodyDiagnostic::InactiveCode { node, .. } => { | ||
312 | (node.clone().map(|it| it.into()), "InactiveCode") | ||
313 | } | ||
314 | BodyDiagnostic::MacroError { node, message } => { | ||
315 | (node.clone().map(|it| it.into()), message.as_str()) | ||
316 | } | ||
317 | BodyDiagnostic::UnresolvedProcMacro { node } => { | ||
318 | (node.clone().map(|it| it.into()), "UnresolvedProcMacro") | ||
319 | } | ||
320 | BodyDiagnostic::UnresolvedMacroCall { node, .. } => { | ||
321 | (node.clone().map(|it| it.into()), "UnresolvedMacroCall") | ||
322 | } | ||
323 | }; | ||
324 | |||
325 | let root = self.parse_or_expand(ptr.file_id).unwrap(); | ||
326 | let node = ptr.map(|ptr| ptr.to_node(&root)); | ||
327 | let frange = node.as_ref().original_file_range(self); | ||
328 | cb(frange, message.to_string()) | ||
329 | } | ||
278 | } | 330 | } |
279 | } | 331 | } |
280 | } | 332 | } |
@@ -287,14 +339,7 @@ impl TestDB { | |||
287 | assert!(!annotations.is_empty()); | 339 | assert!(!annotations.is_empty()); |
288 | 340 | ||
289 | let mut actual: FxHashMap<FileId, Vec<(TextRange, String)>> = FxHashMap::default(); | 341 | let mut actual: FxHashMap<FileId, Vec<(TextRange, String)>> = FxHashMap::default(); |
290 | db.diagnostics(|d| { | 342 | db.diagnostics(&mut |frange, message| { |
291 | let src = d.display_source(); | ||
292 | let root = db.parse_or_expand(src.file_id).unwrap(); | ||
293 | |||
294 | let node = src.map(|ptr| ptr.to_node(&root)); | ||
295 | let frange = node.as_ref().original_file_range(db); | ||
296 | |||
297 | let message = d.message(); | ||
298 | actual.entry(frange.file_id).or_default().push((frange.range, message)); | 343 | actual.entry(frange.file_id).or_default().push((frange.range, message)); |
299 | }); | 344 | }); |
300 | 345 | ||
@@ -319,7 +364,7 @@ impl TestDB { | |||
319 | assert!(annotations.is_empty()); | 364 | assert!(annotations.is_empty()); |
320 | 365 | ||
321 | let mut has_diagnostics = false; | 366 | let mut has_diagnostics = false; |
322 | db.diagnostics(|_| { | 367 | db.diagnostics(&mut |_, _| { |
323 | has_diagnostics = true; | 368 | has_diagnostics = true; |
324 | }); | 369 | }); |
325 | 370 | ||