diff options
author | Jonas Schievink <[email protected]> | 2020-10-22 18:19:18 +0100 |
---|---|---|
committer | Jonas Schievink <[email protected]> | 2020-10-22 18:19:18 +0100 |
commit | 3421b645e6f7d15ddad0e8e526d8a7db09b72516 (patch) | |
tree | e1ec7d37bd63ccc7f0e18b39479c0860b7fc5d9b /crates/hir_def | |
parent | 978cc936491d23bd38ef18aa98ddcc7472ef5f54 (diff) |
Emit better #[cfg] diagnostics
Diffstat (limited to 'crates/hir_def')
-rw-r--r-- | crates/hir_def/src/attr.rs | 14 | ||||
-rw-r--r-- | crates/hir_def/src/diagnostics.rs | 14 | ||||
-rw-r--r-- | crates/hir_def/src/nameres.rs | 16 | ||||
-rw-r--r-- | crates/hir_def/src/nameres/collector.rs | 29 | ||||
-rw-r--r-- | crates/hir_def/src/nameres/tests/diagnostics.rs | 22 |
5 files changed, 74 insertions, 21 deletions
diff --git a/crates/hir_def/src/attr.rs b/crates/hir_def/src/attr.rs index dea552a60..b2ce7ca3c 100644 --- a/crates/hir_def/src/attr.rs +++ b/crates/hir_def/src/attr.rs | |||
@@ -125,12 +125,20 @@ impl Attrs { | |||
125 | AttrQuery { attrs: self, key } | 125 | AttrQuery { attrs: self, key } |
126 | } | 126 | } |
127 | 127 | ||
128 | pub fn cfg(&self) -> impl Iterator<Item = CfgExpr> + '_ { | 128 | pub fn cfg(&self) -> Option<CfgExpr> { |
129 | // FIXME: handle cfg_attr :-) | 129 | // FIXME: handle cfg_attr :-) |
130 | self.by_key("cfg").tt_values().map(CfgExpr::parse) | 130 | let mut cfgs = self.by_key("cfg").tt_values().map(CfgExpr::parse).collect::<Vec<_>>(); |
131 | match cfgs.len() { | ||
132 | 0 => None, | ||
133 | 1 => Some(cfgs.pop().unwrap()), | ||
134 | _ => Some(CfgExpr::All(cfgs)), | ||
135 | } | ||
131 | } | 136 | } |
132 | pub(crate) fn is_cfg_enabled(&self, cfg_options: &CfgOptions) -> bool { | 137 | pub(crate) fn is_cfg_enabled(&self, cfg_options: &CfgOptions) -> bool { |
133 | self.cfg().all(|cfg| cfg_options.check(&cfg) != Some(false)) | 138 | match self.cfg() { |
139 | None => true, | ||
140 | Some(cfg) => cfg_options.check(&cfg) != Some(false), | ||
141 | } | ||
134 | } | 142 | } |
135 | } | 143 | } |
136 | 144 | ||
diff --git a/crates/hir_def/src/diagnostics.rs b/crates/hir_def/src/diagnostics.rs index c9c08b01f..34a6a8d4b 100644 --- a/crates/hir_def/src/diagnostics.rs +++ b/crates/hir_def/src/diagnostics.rs | |||
@@ -1,7 +1,9 @@ | |||
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 std::fmt::Write; | ||
4 | 5 | ||
6 | use cfg::{CfgExpr, CfgOptions, DnfExpr}; | ||
5 | use hir_expand::diagnostics::{Diagnostic, DiagnosticCode}; | 7 | use hir_expand::diagnostics::{Diagnostic, DiagnosticCode}; |
6 | use syntax::{ast, AstPtr, SyntaxNodePtr}; | 8 | use syntax::{ast, AstPtr, SyntaxNodePtr}; |
7 | 9 | ||
@@ -94,6 +96,8 @@ impl Diagnostic for UnresolvedImport { | |||
94 | pub struct InactiveCode { | 96 | pub struct InactiveCode { |
95 | pub file: HirFileId, | 97 | pub file: HirFileId, |
96 | pub node: SyntaxNodePtr, | 98 | pub node: SyntaxNodePtr, |
99 | pub cfg: CfgExpr, | ||
100 | pub opts: CfgOptions, | ||
97 | } | 101 | } |
98 | 102 | ||
99 | impl Diagnostic for InactiveCode { | 103 | impl Diagnostic for InactiveCode { |
@@ -101,8 +105,14 @@ impl Diagnostic for InactiveCode { | |||
101 | DiagnosticCode("inactive-code") | 105 | DiagnosticCode("inactive-code") |
102 | } | 106 | } |
103 | fn message(&self) -> String { | 107 | fn message(&self) -> String { |
104 | // FIXME: say *why* it is configured out | 108 | let inactive = DnfExpr::new(self.cfg.clone()).why_inactive(&self.opts); |
105 | "code is inactive due to #[cfg] directives".to_string() | 109 | let mut buf = "code is inactive due to #[cfg] directives".to_string(); |
110 | |||
111 | if let Some(inactive) = inactive { | ||
112 | write!(buf, ": {}", inactive).unwrap(); | ||
113 | } | ||
114 | |||
115 | buf | ||
106 | } | 116 | } |
107 | fn display_source(&self) -> InFile<SyntaxNodePtr> { | 117 | fn display_source(&self) -> InFile<SyntaxNodePtr> { |
108 | InFile::new(self.file, self.node.clone()) | 118 | InFile::new(self.file, self.node.clone()) |
diff --git a/crates/hir_def/src/nameres.rs b/crates/hir_def/src/nameres.rs index 01a28aeeb..eb41d324e 100644 --- a/crates/hir_def/src/nameres.rs +++ b/crates/hir_def/src/nameres.rs | |||
@@ -283,6 +283,7 @@ pub enum ModuleSource { | |||
283 | } | 283 | } |
284 | 284 | ||
285 | mod diagnostics { | 285 | mod diagnostics { |
286 | use cfg::{CfgExpr, CfgOptions}; | ||
286 | use hir_expand::diagnostics::DiagnosticSink; | 287 | use hir_expand::diagnostics::DiagnosticSink; |
287 | use hir_expand::hygiene::Hygiene; | 288 | use hir_expand::hygiene::Hygiene; |
288 | use hir_expand::InFile; | 289 | use hir_expand::InFile; |
@@ -299,7 +300,7 @@ mod diagnostics { | |||
299 | 300 | ||
300 | UnresolvedImport { ast: AstId<ast::Use>, index: usize }, | 301 | UnresolvedImport { ast: AstId<ast::Use>, index: usize }, |
301 | 302 | ||
302 | UnconfiguredCode { ast: InFile<SyntaxNodePtr> }, | 303 | UnconfiguredCode { ast: InFile<SyntaxNodePtr>, cfg: CfgExpr, opts: CfgOptions }, |
303 | } | 304 | } |
304 | 305 | ||
305 | #[derive(Debug, PartialEq, Eq)] | 306 | #[derive(Debug, PartialEq, Eq)] |
@@ -341,8 +342,10 @@ mod diagnostics { | |||
341 | pub(super) fn unconfigured_code( | 342 | pub(super) fn unconfigured_code( |
342 | container: LocalModuleId, | 343 | container: LocalModuleId, |
343 | ast: InFile<SyntaxNodePtr>, | 344 | ast: InFile<SyntaxNodePtr>, |
345 | cfg: CfgExpr, | ||
346 | opts: CfgOptions, | ||
344 | ) -> Self { | 347 | ) -> Self { |
345 | Self { in_module: container, kind: DiagnosticKind::UnconfiguredCode { ast } } | 348 | Self { in_module: container, kind: DiagnosticKind::UnconfiguredCode { ast, cfg, opts } } |
346 | } | 349 | } |
347 | 350 | ||
348 | pub(super) fn add_to( | 351 | pub(super) fn add_to( |
@@ -395,8 +398,13 @@ mod diagnostics { | |||
395 | } | 398 | } |
396 | } | 399 | } |
397 | 400 | ||
398 | DiagnosticKind::UnconfiguredCode { ast } => { | 401 | DiagnosticKind::UnconfiguredCode { ast, cfg, opts } => { |
399 | sink.push(InactiveCode { file: ast.file_id, node: ast.value.clone() }); | 402 | sink.push(InactiveCode { |
403 | file: ast.file_id, | ||
404 | node: ast.value.clone(), | ||
405 | cfg: cfg.clone(), | ||
406 | opts: opts.clone(), | ||
407 | }); | ||
400 | } | 408 | } |
401 | } | 409 | } |
402 | } | 410 | } |
diff --git a/crates/hir_def/src/nameres/collector.rs b/crates/hir_def/src/nameres/collector.rs index bff8edb62..f30172d90 100644 --- a/crates/hir_def/src/nameres/collector.rs +++ b/crates/hir_def/src/nameres/collector.rs | |||
@@ -6,7 +6,7 @@ | |||
6 | use std::iter; | 6 | use std::iter; |
7 | 7 | ||
8 | use base_db::{CrateId, FileId, ProcMacroId}; | 8 | use base_db::{CrateId, FileId, ProcMacroId}; |
9 | use cfg::CfgOptions; | 9 | use cfg::{CfgExpr, CfgOptions}; |
10 | use hir_expand::InFile; | 10 | use hir_expand::InFile; |
11 | use hir_expand::{ | 11 | use hir_expand::{ |
12 | ast_id_map::FileAstId, | 12 | ast_id_map::FileAstId, |
@@ -900,7 +900,8 @@ impl ModCollector<'_, '_> { | |||
900 | // `#[macro_use] extern crate` is hoisted to imports macros before collecting | 900 | // `#[macro_use] extern crate` is hoisted to imports macros before collecting |
901 | // any other items. | 901 | // any other items. |
902 | for item in items { | 902 | for item in items { |
903 | if self.is_cfg_enabled(self.item_tree.attrs((*item).into())) { | 903 | let attrs = self.item_tree.attrs((*item).into()); |
904 | if attrs.cfg().map_or(true, |cfg| self.is_cfg_enabled(&cfg)) { | ||
904 | if let ModItem::ExternCrate(id) = item { | 905 | if let ModItem::ExternCrate(id) = item { |
905 | let import = self.item_tree[*id].clone(); | 906 | let import = self.item_tree[*id].clone(); |
906 | if import.is_macro_use { | 907 | if import.is_macro_use { |
@@ -912,9 +913,11 @@ impl ModCollector<'_, '_> { | |||
912 | 913 | ||
913 | for &item in items { | 914 | for &item in items { |
914 | let attrs = self.item_tree.attrs(item.into()); | 915 | let attrs = self.item_tree.attrs(item.into()); |
915 | if !self.is_cfg_enabled(attrs) { | 916 | if let Some(cfg) = attrs.cfg() { |
916 | self.emit_unconfigured_diagnostic(item); | 917 | if !self.is_cfg_enabled(&cfg) { |
917 | continue; | 918 | self.emit_unconfigured_diagnostic(item, &cfg); |
919 | continue; | ||
920 | } | ||
918 | } | 921 | } |
919 | let module = | 922 | let module = |
920 | ModuleId { krate: self.def_collector.def_map.krate, local_id: self.module_id }; | 923 | ModuleId { krate: self.def_collector.def_map.krate, local_id: self.module_id }; |
@@ -1321,20 +1324,22 @@ impl ModCollector<'_, '_> { | |||
1321 | } | 1324 | } |
1322 | } | 1325 | } |
1323 | 1326 | ||
1324 | fn is_cfg_enabled(&self, attrs: &Attrs) -> bool { | 1327 | fn is_cfg_enabled(&self, cfg: &CfgExpr) -> bool { |
1325 | attrs.is_cfg_enabled(self.def_collector.cfg_options) | 1328 | self.def_collector.cfg_options.check(cfg) != Some(false) |
1326 | } | 1329 | } |
1327 | 1330 | ||
1328 | fn emit_unconfigured_diagnostic(&mut self, item: ModItem) { | 1331 | fn emit_unconfigured_diagnostic(&mut self, item: ModItem, cfg: &CfgExpr) { |
1329 | let ast_id = item.ast_id(self.item_tree); | 1332 | let ast_id = item.ast_id(self.item_tree); |
1330 | let id_map = self.def_collector.db.ast_id_map(self.file_id); | 1333 | let id_map = self.def_collector.db.ast_id_map(self.file_id); |
1331 | let syntax_ptr = id_map.get(ast_id).syntax_node_ptr(); | 1334 | let syntax_ptr = id_map.get(ast_id).syntax_node_ptr(); |
1332 | 1335 | ||
1333 | let ast_node = InFile::new(self.file_id, syntax_ptr); | 1336 | let ast_node = InFile::new(self.file_id, syntax_ptr); |
1334 | self.def_collector | 1337 | self.def_collector.def_map.diagnostics.push(DefDiagnostic::unconfigured_code( |
1335 | .def_map | 1338 | self.module_id, |
1336 | .diagnostics | 1339 | ast_node, |
1337 | .push(DefDiagnostic::unconfigured_code(self.module_id, ast_node)); | 1340 | cfg.clone(), |
1341 | self.def_collector.cfg_options.clone(), | ||
1342 | )); | ||
1338 | } | 1343 | } |
1339 | } | 1344 | } |
1340 | 1345 | ||
diff --git a/crates/hir_def/src/nameres/tests/diagnostics.rs b/crates/hir_def/src/nameres/tests/diagnostics.rs index 576b813d2..5972248de 100644 --- a/crates/hir_def/src/nameres/tests/diagnostics.rs +++ b/crates/hir_def/src/nameres/tests/diagnostics.rs | |||
@@ -129,3 +129,25 @@ fn unresolved_module() { | |||
129 | ", | 129 | ", |
130 | ); | 130 | ); |
131 | } | 131 | } |
132 | |||
133 | #[test] | ||
134 | fn inactive_item() { | ||
135 | // Additional tests in `cfg` crate. This only tests disabled cfgs. | ||
136 | |||
137 | check_diagnostics( | ||
138 | r#" | ||
139 | //- /lib.rs | ||
140 | #[cfg(no)] pub fn f() {} | ||
141 | //^^^^^^^^^^^^^^^^^^^^^^^^ code is inactive due to #[cfg] directives: no is disabled | ||
142 | |||
143 | #[cfg(no)] #[cfg(no2)] mod m; | ||
144 | //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ code is inactive due to #[cfg] directives: no and no2 are disabled | ||
145 | |||
146 | #[cfg(all(not(a), b))] enum E {} | ||
147 | //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ code is inactive due to #[cfg] directives: b is disabled | ||
148 | |||
149 | #[cfg(feature = "std")] use std; | ||
150 | //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ code is inactive due to #[cfg] directives: feature = "std" is disabled | ||
151 | "#, | ||
152 | ); | ||
153 | } | ||