Skip to content

Commit 2813afd

Browse files
Merge #8108
8108: Fix handling of `#![cfg]` in outline module file r=jonas-schievink a=jonas-schievink bors r+ Co-authored-by: Jonas Schievink <[email protected]>
2 parents dc8e2fe + cf494a5 commit 2813afd

File tree

2 files changed

+48
-21
lines changed

2 files changed

+48
-21
lines changed

crates/hir_def/src/nameres/collector.rs

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1294,29 +1294,37 @@ impl ModCollector<'_, '_> {
12941294
let db = self.def_collector.db;
12951295
match self.mod_dir.resolve_declaration(db, self.file_id, &module.name, path_attr) {
12961296
Ok((file_id, is_mod_rs, mod_dir)) => {
1297-
let module_id = self.push_child_module(
1298-
module.name.clone(),
1299-
ast_id,
1300-
Some((file_id, is_mod_rs)),
1301-
&self.item_tree[module.visibility],
1302-
);
13031297
let item_tree = db.file_item_tree(file_id.into());
1304-
ModCollector {
1305-
def_collector: &mut *self.def_collector,
1306-
macro_depth: self.macro_depth,
1307-
module_id,
1308-
file_id: file_id.into(),
1309-
item_tree: &item_tree,
1310-
mod_dir,
1311-
}
1312-
.collect(item_tree.top_level_items());
1313-
if is_macro_use
1314-
|| item_tree
1315-
.top_level_attrs(db, self.def_collector.def_map.krate)
1316-
.by_key("macro_use")
1317-
.exists()
1298+
if item_tree
1299+
.top_level_attrs(db, self.def_collector.def_map.krate)
1300+
.cfg()
1301+
.map_or(true, |cfg| {
1302+
self.def_collector.cfg_options.check(&cfg) != Some(false)
1303+
})
13181304
{
1319-
self.import_all_legacy_macros(module_id);
1305+
let module_id = self.push_child_module(
1306+
module.name.clone(),
1307+
ast_id,
1308+
Some((file_id, is_mod_rs)),
1309+
&self.item_tree[module.visibility],
1310+
);
1311+
ModCollector {
1312+
def_collector: &mut *self.def_collector,
1313+
macro_depth: self.macro_depth,
1314+
module_id,
1315+
file_id: file_id.into(),
1316+
item_tree: &item_tree,
1317+
mod_dir,
1318+
}
1319+
.collect(item_tree.top_level_items());
1320+
if is_macro_use
1321+
|| item_tree
1322+
.top_level_attrs(db, self.def_collector.def_map.krate)
1323+
.by_key("macro_use")
1324+
.exists()
1325+
{
1326+
self.import_all_legacy_macros(module_id);
1327+
}
13201328
}
13211329
}
13221330
Err(candidate) => {

crates/hir_def/src/nameres/tests/mod_resolution.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -819,3 +819,22 @@ pub mod hash { pub trait Hash {} }
819819
"#]],
820820
);
821821
}
822+
823+
#[test]
824+
fn cfg_in_module_file() {
825+
// Inner `#![cfg]` in a module file makes the whole module disappear.
826+
check(
827+
r#"
828+
//- /main.rs
829+
mod module;
830+
831+
//- /module.rs
832+
#![cfg(NEVER)]
833+
834+
struct AlsoShoulntAppear;
835+
"#,
836+
expect![[r#"
837+
crate
838+
"#]],
839+
)
840+
}

0 commit comments

Comments
 (0)