Skip to content

Commit 2f840c2

Browse files
committed
Auto merge of rust-lang#14874 - Veykril:crate-cfg, r=Veykril
expand: Change how `#![cfg(FALSE)]` behaves on crate root Closes rust-lang/rust-analyzer#14769
2 parents 8ad70e7 + 74d6826 commit 2f840c2

File tree

3 files changed

+14
-5
lines changed

3 files changed

+14
-5
lines changed

crates/hir-def/src/attr.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ impl Attrs {
202202
None => Some(first),
203203
}
204204
}
205+
205206
pub(crate) fn is_cfg_enabled(&self, cfg_options: &CfgOptions) -> bool {
206207
match self.cfg() {
207208
None => true,

crates/hir-def/src/nameres/collector.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -290,16 +290,16 @@ impl DefCollector<'_> {
290290
let module_id = self.def_map.root;
291291

292292
let attrs = item_tree.top_level_attrs(self.db, self.def_map.krate);
293-
if let Some(cfg) = attrs.cfg() {
294-
if self.cfg_options.check(&cfg) == Some(false) {
295-
return;
296-
}
297-
}
298293

299294
self.inject_prelude(&attrs);
300295

301296
// Process other crate-level attributes.
302297
for attr in &*attrs {
298+
if let Some(cfg) = attr.cfg() {
299+
if self.cfg_options.check(&cfg) == Some(false) {
300+
return;
301+
}
302+
}
303303
let attr_name = match attr.path.as_ident() {
304304
Some(name) => name,
305305
None => continue,

crates/hir-expand/src/attrs.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,14 @@ impl Attr {
309309

310310
Some(paths)
311311
}
312+
313+
pub fn cfg(&self) -> Option<CfgExpr> {
314+
if *self.path.as_ident()? == crate::name![cfg] {
315+
self.token_tree_value().map(CfgExpr::parse)
316+
} else {
317+
None
318+
}
319+
}
312320
}
313321

314322
pub fn collect_attrs(

0 commit comments

Comments
 (0)