Handle #![cfg] in crate root

This commit is contained in:
Jonas Schievink 2020-10-26 16:04:08 +01:00
parent a0f1346864
commit cd3c632cfc
2 changed files with 22 additions and 2 deletions

View file

@ -218,15 +218,18 @@ impl DefCollector<'_> {
let item_tree = self.db.item_tree(file_id.into()); let item_tree = self.db.item_tree(file_id.into());
let module_id = self.def_map.root; let module_id = self.def_map.root;
self.def_map.modules[module_id].origin = ModuleOrigin::CrateRoot { definition: file_id }; self.def_map.modules[module_id].origin = ModuleOrigin::CrateRoot { definition: file_id };
ModCollector { let mut root_collector = ModCollector {
def_collector: &mut *self, def_collector: &mut *self,
macro_depth: 0, macro_depth: 0,
module_id, module_id,
file_id: file_id.into(), file_id: file_id.into(),
item_tree: &item_tree, item_tree: &item_tree,
mod_dir: ModDir::root(), 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. // main name resolution fixed-point loop.
let mut i = 0; let mut i = 0;

View file

@ -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
"#]],
);
}