From cd3c632cfcbb4225c0325b229517a1bf38d83000 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Mon, 26 Oct 2020 16:04:08 +0100 Subject: Handle #![cfg] in crate root --- crates/hir_def/src/nameres/collector.rs | 7 +++++-- crates/hir_def/src/nameres/tests.rs | 17 +++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) (limited to 'crates') diff --git a/crates/hir_def/src/nameres/collector.rs b/crates/hir_def/src/nameres/collector.rs index f30172d90..1ff45d244 100644 --- a/crates/hir_def/src/nameres/collector.rs +++ b/crates/hir_def/src/nameres/collector.rs @@ -218,15 +218,18 @@ impl DefCollector<'_> { let item_tree = self.db.item_tree(file_id.into()); let module_id = self.def_map.root; self.def_map.modules[module_id].origin = ModuleOrigin::CrateRoot { definition: file_id }; - ModCollector { + let mut root_collector = ModCollector { def_collector: &mut *self, macro_depth: 0, module_id, file_id: file_id.into(), item_tree: &item_tree, mod_dir: ModDir::root(), + }; + if item_tree.top_level_attrs().cfg().map_or(true, |cfg| root_collector.is_cfg_enabled(&cfg)) + { + root_collector.collect(item_tree.top_level_items()); } - .collect(item_tree.top_level_items()); // main name resolution fixed-point loop. let mut i = 0; diff --git a/crates/hir_def/src/nameres/tests.rs b/crates/hir_def/src/nameres/tests.rs index 11d84f808..9c19bf572 100644 --- a/crates/hir_def/src/nameres/tests.rs +++ b/crates/hir_def/src/nameres/tests.rs @@ -691,3 +691,20 @@ mod tr { "#]], ); } + +#[test] +fn cfg_the_entire_crate() { + check( + r#" +//- /main.rs +#![cfg(never)] + +pub struct S; +pub enum E {} +pub fn f() {} + "#, + expect![[r#" + crate + "#]], + ); +} -- cgit v1.2.3