diff options
Diffstat (limited to 'crates/hir_def/src/diagnostics.rs')
-rw-r--r-- | crates/hir_def/src/diagnostics.rs | 54 |
1 files changed, 52 insertions, 2 deletions
diff --git a/crates/hir_def/src/diagnostics.rs b/crates/hir_def/src/diagnostics.rs index 001b3c5db..b221b290c 100644 --- a/crates/hir_def/src/diagnostics.rs +++ b/crates/hir_def/src/diagnostics.rs | |||
@@ -1,12 +1,23 @@ | |||
1 | //! Diagnostics produced by `hir_def`. | 1 | //! Diagnostics produced by `hir_def`. |
2 | 2 | ||
3 | use std::any::Any; | 3 | use std::any::Any; |
4 | use stdx::format_to; | ||
4 | 5 | ||
5 | use hir_expand::diagnostics::{Diagnostic, DiagnosticCode}; | 6 | use cfg::{CfgExpr, CfgOptions, DnfExpr}; |
7 | use hir_expand::diagnostics::{Diagnostic, DiagnosticCode, DiagnosticSink}; | ||
8 | use hir_expand::{HirFileId, InFile}; | ||
6 | use syntax::{ast, AstPtr, SyntaxNodePtr}; | 9 | use syntax::{ast, AstPtr, SyntaxNodePtr}; |
7 | 10 | ||
8 | use hir_expand::{HirFileId, InFile}; | 11 | use crate::{db::DefDatabase, DefWithBodyId}; |
9 | 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. | ||
10 | #[derive(Debug)] | 21 | #[derive(Debug)] |
11 | pub struct UnresolvedModule { | 22 | pub struct UnresolvedModule { |
12 | pub file: HirFileId, | 23 | pub file: HirFileId, |
@@ -29,6 +40,9 @@ impl Diagnostic for UnresolvedModule { | |||
29 | } | 40 | } |
30 | } | 41 | } |
31 | 42 | ||
43 | // Diagnostic: unresolved-extern-crate | ||
44 | // | ||
45 | // This diagnostic is triggered if rust-analyzer is unable to discover referred extern crate. | ||
32 | #[derive(Debug)] | 46 | #[derive(Debug)] |
33 | pub struct UnresolvedExternCrate { | 47 | pub struct UnresolvedExternCrate { |
34 | pub file: HirFileId, | 48 | pub file: HirFileId, |
@@ -50,6 +64,9 @@ impl Diagnostic for UnresolvedExternCrate { | |||
50 | } | 64 | } |
51 | } | 65 | } |
52 | 66 | ||
67 | // Diagnostic: unresolved-import | ||
68 | // | ||
69 | // This diagnostic is triggered if rust-analyzer is unable to discover imported module. | ||
53 | #[derive(Debug)] | 70 | #[derive(Debug)] |
54 | pub struct UnresolvedImport { | 71 | pub struct UnresolvedImport { |
55 | pub file: HirFileId, | 72 | pub file: HirFileId, |
@@ -77,3 +94,36 @@ impl Diagnostic for UnresolvedImport { | |||
77 | true | 94 | true |
78 | } | 95 | } |
79 | } | 96 | } |
97 | |||
98 | // Diagnostic: inactive-code | ||
99 | // | ||
100 | // This diagnostic is shown for code with inactive `#[cfg]` attributes. | ||
101 | #[derive(Debug, Clone, Eq, PartialEq)] | ||
102 | pub struct InactiveCode { | ||
103 | pub file: HirFileId, | ||
104 | pub node: SyntaxNodePtr, | ||
105 | pub cfg: CfgExpr, | ||
106 | pub opts: CfgOptions, | ||
107 | } | ||
108 | |||
109 | impl Diagnostic for InactiveCode { | ||
110 | fn code(&self) -> DiagnosticCode { | ||
111 | DiagnosticCode("inactive-code") | ||
112 | } | ||
113 | fn message(&self) -> String { | ||
114 | let inactive = DnfExpr::new(self.cfg.clone()).why_inactive(&self.opts); | ||
115 | let mut buf = "code is inactive due to #[cfg] directives".to_string(); | ||
116 | |||
117 | if let Some(inactive) = inactive { | ||
118 | format_to!(buf, ": {}", inactive); | ||
119 | } | ||
120 | |||
121 | buf | ||
122 | } | ||
123 | fn display_source(&self) -> InFile<SyntaxNodePtr> { | ||
124 | InFile::new(self.file, self.node.clone()) | ||
125 | } | ||
126 | fn as_any(&self) -> &(dyn Any + Send + 'static) { | ||
127 | self | ||
128 | } | ||
129 | } | ||