Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support cfg(true) and cfg(false) #18420

Merged
merged 1 commit into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions 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 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
2 changes: 2 additions & 0 deletions crates/ide-completion/src/tests/attribute.rs
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
16 changes: 16 additions & 0 deletions crates/ide-diagnostics/src/handlers/inactive_code.rs
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
12 changes: 12 additions & 0 deletions crates/project-model/test_data/output/rust_project_cfg_groups.txt
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