diff options
| author | bors <bors@rust-lang.org> | 2023-05-24 16:06:09 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2023-05-24 16:06:09 +0000 |
| commit | 2f840c22369e0bdabb965bd7fa1729cb846fdb18 (patch) | |
| tree | a3a64101a5aa8d92f194303e2632f21c17f1bbd4 | |
| parent | 8ad70e7f99b6a5eca65f6ef76cedee6c40132d6f (diff) | |
| parent | 74d6826858bab734766048a670820d9961c70448 (diff) | |
| download | rust-2f840c22369e0bdabb965bd7fa1729cb846fdb18.tar.gz rust-2f840c22369e0bdabb965bd7fa1729cb846fdb18.zip | |
Auto merge of #14874 - Veykril:crate-cfg, r=Veykril
expand: Change how `#![cfg(FALSE)]` behaves on crate root Closes https://github.com/rust-lang/rust-analyzer/issues/14769
| -rw-r--r-- | crates/hir-def/src/attr.rs | 1 | ||||
| -rw-r--r-- | crates/hir-def/src/nameres/collector.rs | 10 | ||||
| -rw-r--r-- | crates/hir-expand/src/attrs.rs | 8 |
3 files changed, 14 insertions, 5 deletions
diff --git a/crates/hir-def/src/attr.rs b/crates/hir-def/src/attr.rs index de515b569df..bab3bbc2329 100644 --- a/crates/hir-def/src/attr.rs +++ b/crates/hir-def/src/attr.rs @@ -202,6 +202,7 @@ impl Attrs { None => Some(first), } } + pub(crate) fn is_cfg_enabled(&self, cfg_options: &CfgOptions) -> bool { match self.cfg() { None => true, diff --git a/crates/hir-def/src/nameres/collector.rs b/crates/hir-def/src/nameres/collector.rs index a47ee85da10..a528d238e37 100644 --- a/crates/hir-def/src/nameres/collector.rs +++ b/crates/hir-def/src/nameres/collector.rs @@ -290,16 +290,16 @@ impl DefCollector<'_> { let module_id = self.def_map.root; let attrs = item_tree.top_level_attrs(self.db, self.def_map.krate); - if let Some(cfg) = attrs.cfg() { - if self.cfg_options.check(&cfg) == Some(false) { - return; - } - } self.inject_prelude(&attrs); // Process other crate-level attributes. for attr in &*attrs { + if let Some(cfg) = attr.cfg() { + if self.cfg_options.check(&cfg) == Some(false) { + return; + } + } let attr_name = match attr.path.as_ident() { Some(name) => name, None => continue, diff --git a/crates/hir-expand/src/attrs.rs b/crates/hir-expand/src/attrs.rs index 3ff18e982e1..0c369a18bb9 100644 --- a/crates/hir-expand/src/attrs.rs +++ b/crates/hir-expand/src/attrs.rs @@ -309,6 +309,14 @@ impl Attr { Some(paths) } + + pub fn cfg(&self) -> Option<CfgExpr> { + if *self.path.as_ident()? == crate::name![cfg] { + self.token_tree_value().map(CfgExpr::parse) + } else { + None + } + } } pub fn collect_attrs( |
