Skip to content

Commit e970e07

Browse files
Support cfg(true) and cfg(false)
As per RFC 3695.
1 parent aaae1d4 commit e970e07

10 files changed

+87
-6
lines changed

src/tools/rust-analyzer/Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ dependencies = [
164164
"rustc-hash 2.0.0",
165165
"syntax",
166166
"syntax-bridge",
167+
"tracing",
167168
"tt",
168169
]
169170

src/tools/rust-analyzer/crates/cfg/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ doctest = false
1414

1515
[dependencies]
1616
rustc-hash.workspace = true
17+
tracing.workspace = true
1718

1819
# locals deps
1920
tt = { workspace = true, optional = true }

src/tools/rust-analyzer/crates/cfg/src/lib.rs

+26-6
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::fmt;
99

1010
use rustc_hash::FxHashSet;
1111

12-
use intern::Symbol;
12+
use intern::{sym, Symbol};
1313

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

32+
impl Default for CfgOptions {
33+
fn default() -> Self {
34+
Self { enabled: FxHashSet::from_iter([CfgAtom::Flag(sym::true_.clone())]) }
35+
}
36+
}
37+
3238
impl fmt::Debug for CfgOptions {
3339
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3440
let mut items = self
@@ -54,23 +60,37 @@ impl CfgOptions {
5460
}
5561

5662
pub fn insert_atom(&mut self, key: Symbol) {
57-
self.enabled.insert(CfgAtom::Flag(key));
63+
self.insert_any_atom(CfgAtom::Flag(key));
5864
}
5965

6066
pub fn insert_key_value(&mut self, key: Symbol, value: Symbol) {
61-
self.enabled.insert(CfgAtom::KeyValue { key, value });
67+
self.insert_any_atom(CfgAtom::KeyValue { key, value });
6268
}
6369

6470
pub fn apply_diff(&mut self, diff: CfgDiff) {
6571
for atom in diff.enable {
66-
self.enabled.insert(atom);
72+
self.insert_any_atom(atom);
6773
}
6874

6975
for atom in diff.disable {
76+
let (CfgAtom::Flag(sym) | CfgAtom::KeyValue { key: sym, .. }) = &atom;
77+
if *sym == sym::true_ || *sym == sym::false_ {
78+
tracing::error!("cannot remove `true` or `false` from cfg");
79+
continue;
80+
}
7081
self.enabled.remove(&atom);
7182
}
7283
}
7384

85+
fn insert_any_atom(&mut self, atom: CfgAtom) {
86+
let (CfgAtom::Flag(sym) | CfgAtom::KeyValue { key: sym, .. }) = &atom;
87+
if *sym == sym::true_ || *sym == sym::false_ {
88+
tracing::error!("cannot insert `true` or `false` to cfg");
89+
return;
90+
}
91+
self.enabled.insert(atom);
92+
}
93+
7494
pub fn get_cfg_keys(&self) -> impl Iterator<Item = &Symbol> {
7595
self.enabled.iter().map(|it| match it {
7696
CfgAtom::Flag(key) => key,
@@ -88,7 +108,7 @@ impl CfgOptions {
88108

89109
impl Extend<CfgAtom> for CfgOptions {
90110
fn extend<T: IntoIterator<Item = CfgAtom>>(&mut self, iter: T) {
91-
iter.into_iter().for_each(|cfg_flag| _ = self.enabled.insert(cfg_flag));
111+
iter.into_iter().for_each(|cfg_flag| self.insert_any_atom(cfg_flag));
92112
}
93113
}
94114

src/tools/rust-analyzer/crates/ide-completion/src/tests/attribute.rs

+2
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,7 @@ mod cfg {
663663
ba dbg
664664
ba opt_level
665665
ba test
666+
ba true
666667
"#]],
667668
);
668669
check(
@@ -674,6 +675,7 @@ mod cfg {
674675
ba dbg
675676
ba opt_level
676677
ba test
678+
ba true
677679
"#]],
678680
);
679681
}

src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/inactive_code.rs

+16
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,22 @@ union FooBar {
192192
//- /outline_inner.rs
193193
#![cfg(outline_inner)]
194194
//- /outline.rs
195+
"#,
196+
);
197+
}
198+
199+
#[test]
200+
fn cfg_true_false() {
201+
check(
202+
r#"
203+
#[cfg(false)] fn inactive() {}
204+
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ weak: code is inactive due to #[cfg] directives: false is disabled
205+
206+
#[cfg(true)] fn active() {}
207+
208+
#[cfg(any(not(true)), false)] fn inactive2() {}
209+
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ weak: code is inactive due to #[cfg] directives: true is enabled
210+
195211
"#,
196212
);
197213
}

src/tools/rust-analyzer/crates/project-model/test_data/output/cargo_hello_world_project_model.txt

+6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
[
2020
"rust_analyzer",
2121
"test",
22+
"true",
2223
],
2324
),
2425
potential_cfg_options: None,
@@ -81,6 +82,7 @@
8182
[
8283
"rust_analyzer",
8384
"test",
85+
"true",
8486
],
8587
),
8688
potential_cfg_options: None,
@@ -151,6 +153,7 @@
151153
[
152154
"rust_analyzer",
153155
"test",
156+
"true",
154157
],
155158
),
156159
potential_cfg_options: None,
@@ -221,6 +224,7 @@
221224
[
222225
"rust_analyzer",
223226
"test",
227+
"true",
224228
],
225229
),
226230
potential_cfg_options: None,
@@ -291,6 +295,7 @@
291295
[
292296
"feature=default",
293297
"feature=std",
298+
"true",
294299
],
295300
),
296301
potential_cfg_options: Some(
@@ -303,6 +308,7 @@
303308
"feature=rustc-dep-of-std",
304309
"feature=std",
305310
"feature=use_std",
311+
"true",
306312
],
307313
),
308314
),

src/tools/rust-analyzer/crates/project-model/test_data/output/cargo_hello_world_project_model_with_selective_overrides.txt

+6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
[
2020
"rust_analyzer",
2121
"test",
22+
"true",
2223
],
2324
),
2425
potential_cfg_options: None,
@@ -81,6 +82,7 @@
8182
[
8283
"rust_analyzer",
8384
"test",
85+
"true",
8486
],
8587
),
8688
potential_cfg_options: None,
@@ -151,6 +153,7 @@
151153
[
152154
"rust_analyzer",
153155
"test",
156+
"true",
154157
],
155158
),
156159
potential_cfg_options: None,
@@ -221,6 +224,7 @@
221224
[
222225
"rust_analyzer",
223226
"test",
227+
"true",
224228
],
225229
),
226230
potential_cfg_options: None,
@@ -291,6 +295,7 @@
291295
[
292296
"feature=default",
293297
"feature=std",
298+
"true",
294299
],
295300
),
296301
potential_cfg_options: Some(
@@ -303,6 +308,7 @@
303308
"feature=rustc-dep-of-std",
304309
"feature=std",
305310
"feature=use_std",
311+
"true",
306312
],
307313
),
308314
),

src/tools/rust-analyzer/crates/project-model/test_data/output/cargo_hello_world_project_model_with_wildcard_overrides.txt

+6
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
cfg_options: CfgOptions(
1919
[
2020
"rust_analyzer",
21+
"true",
2122
],
2223
),
2324
potential_cfg_options: None,
@@ -79,6 +80,7 @@
7980
cfg_options: CfgOptions(
8081
[
8182
"rust_analyzer",
83+
"true",
8284
],
8385
),
8486
potential_cfg_options: None,
@@ -148,6 +150,7 @@
148150
cfg_options: CfgOptions(
149151
[
150152
"rust_analyzer",
153+
"true",
151154
],
152155
),
153156
potential_cfg_options: None,
@@ -217,6 +220,7 @@
217220
cfg_options: CfgOptions(
218221
[
219222
"rust_analyzer",
223+
"true",
220224
],
221225
),
222226
potential_cfg_options: None,
@@ -287,6 +291,7 @@
287291
[
288292
"feature=default",
289293
"feature=std",
294+
"true",
290295
],
291296
),
292297
potential_cfg_options: Some(
@@ -299,6 +304,7 @@
299304
"feature=rustc-dep-of-std",
300305
"feature=std",
301306
"feature=use_std",
307+
"true",
302308
],
303309
),
304310
),

src/tools/rust-analyzer/crates/project-model/test_data/output/rust_project_cfg_groups.txt

+12
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
[
1818
"debug_assertions",
1919
"miri",
20+
"true",
2021
],
2122
),
2223
potential_cfg_options: None,
@@ -56,6 +57,7 @@
5657
[
5758
"debug_assertions",
5859
"miri",
60+
"true",
5961
],
6062
),
6163
potential_cfg_options: None,
@@ -86,6 +88,7 @@
8688
[
8789
"debug_assertions",
8890
"miri",
91+
"true",
8992
],
9093
),
9194
potential_cfg_options: None,
@@ -116,6 +119,7 @@
116119
[
117120
"debug_assertions",
118121
"miri",
122+
"true",
119123
],
120124
),
121125
potential_cfg_options: None,
@@ -146,6 +150,7 @@
146150
[
147151
"debug_assertions",
148152
"miri",
153+
"true",
149154
],
150155
),
151156
potential_cfg_options: None,
@@ -193,6 +198,7 @@
193198
[
194199
"debug_assertions",
195200
"miri",
201+
"true",
196202
],
197203
),
198204
potential_cfg_options: None,
@@ -223,6 +229,7 @@
223229
[
224230
"debug_assertions",
225231
"miri",
232+
"true",
226233
],
227234
),
228235
potential_cfg_options: None,
@@ -318,6 +325,7 @@
318325
[
319326
"debug_assertions",
320327
"miri",
328+
"true",
321329
],
322330
),
323331
potential_cfg_options: None,
@@ -348,6 +356,7 @@
348356
[
349357
"debug_assertions",
350358
"miri",
359+
"true",
351360
],
352361
),
353362
potential_cfg_options: None,
@@ -378,6 +387,7 @@
378387
[
379388
"debug_assertions",
380389
"miri",
390+
"true",
381391
],
382392
),
383393
potential_cfg_options: None,
@@ -410,6 +420,7 @@
410420
"group1_other_cfg=other_config",
411421
"group2_cfg=yet_another_config",
412422
"rust_analyzer",
423+
"true",
413424
],
414425
),
415426
potential_cfg_options: None,
@@ -485,6 +496,7 @@
485496
"group2_cfg=fourth_config",
486497
"group2_cfg=yet_another_config",
487498
"rust_analyzer",
499+
"true",
488500
"unrelated_cfg",
489501
],
490502
),

0 commit comments

Comments
 (0)