aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_def/src/diagnostics.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/hir_def/src/diagnostics.rs')
-rw-r--r--crates/hir_def/src/diagnostics.rs54
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
3use std::any::Any; 3use std::any::Any;
4use stdx::format_to;
4 5
5use hir_expand::diagnostics::{Diagnostic, DiagnosticCode}; 6use cfg::{CfgExpr, CfgOptions, DnfExpr};
7use hir_expand::diagnostics::{Diagnostic, DiagnosticCode, DiagnosticSink};
8use hir_expand::{HirFileId, InFile};
6use syntax::{ast, AstPtr, SyntaxNodePtr}; 9use syntax::{ast, AstPtr, SyntaxNodePtr};
7 10
8use hir_expand::{HirFileId, InFile}; 11use crate::{db::DefDatabase, DefWithBodyId};
9 12
13pub 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)]
11pub struct UnresolvedModule { 22pub 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)]
33pub struct UnresolvedExternCrate { 47pub 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)]
54pub struct UnresolvedImport { 71pub 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)]
102pub struct InactiveCode {
103 pub file: HirFileId,
104 pub node: SyntaxNodePtr,
105 pub cfg: CfgExpr,
106 pub opts: CfgOptions,
107}
108
109impl 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}