diff options
Diffstat (limited to 'crates/hir_def/src/nameres/collector.rs')
-rw-r--r-- | crates/hir_def/src/nameres/collector.rs | 37 |
1 files changed, 29 insertions, 8 deletions
diff --git a/crates/hir_def/src/nameres/collector.rs b/crates/hir_def/src/nameres/collector.rs index c8cd04264..1ff45d244 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, |
@@ -218,15 +218,18 @@ impl DefCollector<'_> { | |||
218 | let item_tree = self.db.item_tree(file_id.into()); | 218 | let item_tree = self.db.item_tree(file_id.into()); |
219 | let module_id = self.def_map.root; | 219 | let module_id = self.def_map.root; |
220 | self.def_map.modules[module_id].origin = ModuleOrigin::CrateRoot { definition: file_id }; | 220 | self.def_map.modules[module_id].origin = ModuleOrigin::CrateRoot { definition: file_id }; |
221 | ModCollector { | 221 | let mut root_collector = ModCollector { |
222 | def_collector: &mut *self, | 222 | def_collector: &mut *self, |
223 | macro_depth: 0, | 223 | macro_depth: 0, |
224 | module_id, | 224 | module_id, |
225 | file_id: file_id.into(), | 225 | file_id: file_id.into(), |
226 | item_tree: &item_tree, | 226 | item_tree: &item_tree, |
227 | mod_dir: ModDir::root(), | 227 | mod_dir: ModDir::root(), |
228 | }; | ||
229 | if item_tree.top_level_attrs().cfg().map_or(true, |cfg| root_collector.is_cfg_enabled(&cfg)) | ||
230 | { | ||
231 | root_collector.collect(item_tree.top_level_items()); | ||
228 | } | 232 | } |
229 | .collect(item_tree.top_level_items()); | ||
230 | 233 | ||
231 | // main name resolution fixed-point loop. | 234 | // main name resolution fixed-point loop. |
232 | let mut i = 0; | 235 | let mut i = 0; |
@@ -900,7 +903,8 @@ impl ModCollector<'_, '_> { | |||
900 | // `#[macro_use] extern crate` is hoisted to imports macros before collecting | 903 | // `#[macro_use] extern crate` is hoisted to imports macros before collecting |
901 | // any other items. | 904 | // any other items. |
902 | for item in items { | 905 | for item in items { |
903 | if self.is_cfg_enabled(self.item_tree.attrs((*item).into())) { | 906 | let attrs = self.item_tree.attrs((*item).into()); |
907 | if attrs.cfg().map_or(true, |cfg| self.is_cfg_enabled(&cfg)) { | ||
904 | if let ModItem::ExternCrate(id) = item { | 908 | if let ModItem::ExternCrate(id) = item { |
905 | let import = self.item_tree[*id].clone(); | 909 | let import = self.item_tree[*id].clone(); |
906 | if import.is_macro_use { | 910 | if import.is_macro_use { |
@@ -912,8 +916,11 @@ impl ModCollector<'_, '_> { | |||
912 | 916 | ||
913 | for &item in items { | 917 | for &item in items { |
914 | let attrs = self.item_tree.attrs(item.into()); | 918 | let attrs = self.item_tree.attrs(item.into()); |
915 | if !self.is_cfg_enabled(attrs) { | 919 | if let Some(cfg) = attrs.cfg() { |
916 | continue; | 920 | if !self.is_cfg_enabled(&cfg) { |
921 | self.emit_unconfigured_diagnostic(item, &cfg); | ||
922 | continue; | ||
923 | } | ||
917 | } | 924 | } |
918 | let module = | 925 | let module = |
919 | ModuleId { krate: self.def_collector.def_map.krate, local_id: self.module_id }; | 926 | ModuleId { krate: self.def_collector.def_map.krate, local_id: self.module_id }; |
@@ -1320,8 +1327,22 @@ impl ModCollector<'_, '_> { | |||
1320 | } | 1327 | } |
1321 | } | 1328 | } |
1322 | 1329 | ||
1323 | fn is_cfg_enabled(&self, attrs: &Attrs) -> bool { | 1330 | fn is_cfg_enabled(&self, cfg: &CfgExpr) -> bool { |
1324 | attrs.is_cfg_enabled(self.def_collector.cfg_options) | 1331 | self.def_collector.cfg_options.check(cfg) != Some(false) |
1332 | } | ||
1333 | |||
1334 | fn emit_unconfigured_diagnostic(&mut self, item: ModItem, cfg: &CfgExpr) { | ||
1335 | let ast_id = item.ast_id(self.item_tree); | ||
1336 | let id_map = self.def_collector.db.ast_id_map(self.file_id); | ||
1337 | let syntax_ptr = id_map.get(ast_id).syntax_node_ptr(); | ||
1338 | |||
1339 | let ast_node = InFile::new(self.file_id, syntax_ptr); | ||
1340 | self.def_collector.def_map.diagnostics.push(DefDiagnostic::unconfigured_code( | ||
1341 | self.module_id, | ||
1342 | ast_node, | ||
1343 | cfg.clone(), | ||
1344 | self.def_collector.cfg_options.clone(), | ||
1345 | )); | ||
1325 | } | 1346 | } |
1326 | } | 1347 | } |
1327 | 1348 | ||