Skip to content

Commit

Permalink
Merge pull request #18420 from ChayimFriedman2/cfg-true-false
Browse files Browse the repository at this point in the history
feat: Support `cfg(true)` and `cfg(false)`
  • Loading branch information
Veykril authored Oct 28, 2024
2 parents 8e97f40 + e970e07 commit af764db
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/tools/rust-analyzer/Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ dependencies = [
"rustc-hash 2.0.0",
"syntax",
"syntax-bridge",
"tracing",
"tt",
]

Expand Down
1 change: 1 addition & 0 deletions src/tools/rust-analyzer/crates/cfg/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ doctest = false

[dependencies]
rustc-hash.workspace = true
tracing.workspace = true

# locals deps
tt = { workspace = true, optional = true }
Expand Down
32 changes: 26 additions & 6 deletions src/tools/rust-analyzer/crates/cfg/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::fmt;

use rustc_hash::FxHashSet;

use intern::Symbol;
use intern::{sym, Symbol};

pub use cfg_expr::{CfgAtom, CfgExpr};
pub use dnf::DnfExpr;
Expand All @@ -24,11 +24,17 @@ pub use dnf::DnfExpr;
/// of key and value in `key_values`.
///
/// See: <https://doc.rust-lang.org/reference/conditional-compilation.html#set-configuration-options>
#[derive(Clone, PartialEq, Eq, Default)]
#[derive(Clone, PartialEq, Eq)]
pub struct CfgOptions {
enabled: FxHashSet<CfgAtom>,
}

impl Default for CfgOptions {
fn default() -> Self {
Self { enabled: FxHashSet::from_iter([CfgAtom::Flag(sym::true_.clone())]) }
}
}

impl fmt::Debug for CfgOptions {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut items = self
Expand All @@ -54,23 +60,37 @@ impl CfgOptions {
}

pub fn insert_atom(&mut self, key: Symbol) {
self.enabled.insert(CfgAtom::Flag(key));
self.insert_any_atom(CfgAtom::Flag(key));
}

pub fn insert_key_value(&mut self, key: Symbol, value: Symbol) {
self.enabled.insert(CfgAtom::KeyValue { key, value });
self.insert_any_atom(CfgAtom::KeyValue { key, value });
}

pub fn apply_diff(&mut self, diff: CfgDiff) {
for atom in diff.enable {
self.enabled.insert(atom);
self.insert_any_atom(atom);
}

for atom in diff.disable {
let (CfgAtom::Flag(sym) | CfgAtom::KeyValue { key: sym, .. }) = &atom;
if *sym == sym::true_ || *sym == sym::false_ {
tracing::error!("cannot remove `true` or `false` from cfg");
continue;
}
self.enabled.remove(&atom);
}
}

fn insert_any_atom(&mut self, atom: CfgAtom) {
let (CfgAtom::Flag(sym) | CfgAtom::KeyValue { key: sym, .. }) = &atom;
if *sym == sym::true_ || *sym == sym::false_ {
tracing::error!("cannot insert `true` or `false` to cfg");
return;
}
self.enabled.insert(atom);
}

pub fn get_cfg_keys(&self) -> impl Iterator<Item = &Symbol> {
self.enabled.iter().map(|it| match it {
CfgAtom::Flag(key) => key,
Expand All @@ -88,7 +108,7 @@ impl CfgOptions {

impl Extend<CfgAtom> for CfgOptions {
fn extend<T: IntoIterator<Item = CfgAtom>>(&mut self, iter: T) {
iter.into_iter().for_each(|cfg_flag| _ = self.enabled.insert(cfg_flag));
iter.into_iter().for_each(|cfg_flag| self.insert_any_atom(cfg_flag));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,7 @@ mod cfg {
ba dbg
ba opt_level
ba test
ba true
"#]],
);
check(
Expand All @@ -674,6 +675,7 @@ mod cfg {
ba dbg
ba opt_level
ba test
ba true
"#]],
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,22 @@ union FooBar {
//- /outline_inner.rs
#![cfg(outline_inner)]
//- /outline.rs
"#,
);
}

#[test]
fn cfg_true_false() {
check(
r#"
#[cfg(false)] fn inactive() {}
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ weak: code is inactive due to #[cfg] directives: false is disabled
#[cfg(true)] fn active() {}
#[cfg(any(not(true)), false)] fn inactive2() {}
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ weak: code is inactive due to #[cfg] directives: true is enabled
"#,
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
[
"rust_analyzer",
"test",
"true",
],
),
potential_cfg_options: None,
Expand Down Expand Up @@ -81,6 +82,7 @@
[
"rust_analyzer",
"test",
"true",
],
),
potential_cfg_options: None,
Expand Down Expand Up @@ -151,6 +153,7 @@
[
"rust_analyzer",
"test",
"true",
],
),
potential_cfg_options: None,
Expand Down Expand Up @@ -221,6 +224,7 @@
[
"rust_analyzer",
"test",
"true",
],
),
potential_cfg_options: None,
Expand Down Expand Up @@ -291,6 +295,7 @@
[
"feature=default",
"feature=std",
"true",
],
),
potential_cfg_options: Some(
Expand All @@ -303,6 +308,7 @@
"feature=rustc-dep-of-std",
"feature=std",
"feature=use_std",
"true",
],
),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
[
"rust_analyzer",
"test",
"true",
],
),
potential_cfg_options: None,
Expand Down Expand Up @@ -81,6 +82,7 @@
[
"rust_analyzer",
"test",
"true",
],
),
potential_cfg_options: None,
Expand Down Expand Up @@ -151,6 +153,7 @@
[
"rust_analyzer",
"test",
"true",
],
),
potential_cfg_options: None,
Expand Down Expand Up @@ -221,6 +224,7 @@
[
"rust_analyzer",
"test",
"true",
],
),
potential_cfg_options: None,
Expand Down Expand Up @@ -291,6 +295,7 @@
[
"feature=default",
"feature=std",
"true",
],
),
potential_cfg_options: Some(
Expand All @@ -303,6 +308,7 @@
"feature=rustc-dep-of-std",
"feature=std",
"feature=use_std",
"true",
],
),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
cfg_options: CfgOptions(
[
"rust_analyzer",
"true",
],
),
potential_cfg_options: None,
Expand Down Expand Up @@ -79,6 +80,7 @@
cfg_options: CfgOptions(
[
"rust_analyzer",
"true",
],
),
potential_cfg_options: None,
Expand Down Expand Up @@ -148,6 +150,7 @@
cfg_options: CfgOptions(
[
"rust_analyzer",
"true",
],
),
potential_cfg_options: None,
Expand Down Expand Up @@ -217,6 +220,7 @@
cfg_options: CfgOptions(
[
"rust_analyzer",
"true",
],
),
potential_cfg_options: None,
Expand Down Expand Up @@ -287,6 +291,7 @@
[
"feature=default",
"feature=std",
"true",
],
),
potential_cfg_options: Some(
Expand All @@ -299,6 +304,7 @@
"feature=rustc-dep-of-std",
"feature=std",
"feature=use_std",
"true",
],
),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
[
"debug_assertions",
"miri",
"true",
],
),
potential_cfg_options: None,
Expand Down Expand Up @@ -56,6 +57,7 @@
[
"debug_assertions",
"miri",
"true",
],
),
potential_cfg_options: None,
Expand Down Expand Up @@ -86,6 +88,7 @@
[
"debug_assertions",
"miri",
"true",
],
),
potential_cfg_options: None,
Expand Down Expand Up @@ -116,6 +119,7 @@
[
"debug_assertions",
"miri",
"true",
],
),
potential_cfg_options: None,
Expand Down Expand Up @@ -146,6 +150,7 @@
[
"debug_assertions",
"miri",
"true",
],
),
potential_cfg_options: None,
Expand Down Expand Up @@ -193,6 +198,7 @@
[
"debug_assertions",
"miri",
"true",
],
),
potential_cfg_options: None,
Expand Down Expand Up @@ -223,6 +229,7 @@
[
"debug_assertions",
"miri",
"true",
],
),
potential_cfg_options: None,
Expand Down Expand Up @@ -318,6 +325,7 @@
[
"debug_assertions",
"miri",
"true",
],
),
potential_cfg_options: None,
Expand Down Expand Up @@ -348,6 +356,7 @@
[
"debug_assertions",
"miri",
"true",
],
),
potential_cfg_options: None,
Expand Down Expand Up @@ -378,6 +387,7 @@
[
"debug_assertions",
"miri",
"true",
],
),
potential_cfg_options: None,
Expand Down Expand Up @@ -410,6 +420,7 @@
"group1_other_cfg=other_config",
"group2_cfg=yet_another_config",
"rust_analyzer",
"true",
],
),
potential_cfg_options: None,
Expand Down Expand Up @@ -485,6 +496,7 @@
"group2_cfg=fourth_config",
"group2_cfg=yet_another_config",
"rust_analyzer",
"true",
"unrelated_cfg",
],
),
Expand Down
Loading

0 comments on commit af764db

Please sign in to comment.