Skip to content

Commit ebf4cc3

Browse files
committed
Remove implicit names and values from --cfg in --check-cfg
1 parent 03d488b commit ebf4cc3

File tree

7 files changed

+72
-79
lines changed

7 files changed

+72
-79
lines changed

compiler/rustc_interface/src/util.rs

-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ pub fn create_session(
117117

118118
let mut check_cfg = config::to_crate_check_config(check_cfg);
119119
check_cfg.fill_well_known();
120-
check_cfg.fill_actual(&cfg);
121120

122121
sess.parse_sess.config = cfg;
123122
sess.parse_sess.check_config = check_cfg;

compiler/rustc_session/src/config.rs

-14
Original file line numberDiff line numberDiff line change
@@ -1157,20 +1157,6 @@ impl CrateCheckConfig {
11571157
self.fill_well_known_names();
11581158
self.fill_well_known_values();
11591159
}
1160-
1161-
/// Fills a `CrateCheckConfig` with configuration names and values that are actually active.
1162-
pub fn fill_actual(&mut self, cfg: &CrateConfig) {
1163-
for &(k, v) in cfg {
1164-
if let Some(names_valid) = &mut self.names_valid {
1165-
names_valid.insert(k);
1166-
}
1167-
if let Some(v) = v {
1168-
self.values_valid.entry(k).and_modify(|values| {
1169-
values.insert(v);
1170-
});
1171-
}
1172-
}
1173-
}
11741160
}
11751161

11761162
pub fn build_configuration(sess: &Session, mut user_cfg: CrateConfig) -> CrateConfig {

src/doc/unstable-book/src/compiler-flags/check-cfg.md

+9-26
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ check cfg specification is parsed using the Rust metadata syntax, just as the `-
1818
These two options are independent. `names` checks only the namespace of condition names
1919
while `values` checks only the namespace of the values of list-valued conditions.
2020

21+
NOTE: No implicit expectation is added when using `--cfg` for both forms. Users are expected to
22+
pass all expected names and values using `names(...)` and `values(...)`.
23+
2124
## The `names(...)` form
2225

2326
The `names(...)` form enables checking the names. This form uses a named list:
@@ -53,27 +56,6 @@ The first form enables checking condition names, while specifying that there are
5356
condition names (outside of the set of well-known names defined by `rustc`). Omitting the
5457
`--check-cfg 'names(...)'` option does not enable checking condition names.
5558

56-
Conditions that are enabled are implicitly valid; it is unnecessary (but legal) to specify a
57-
condition name as both enabled and valid. For example, the following invocations are equivalent:
58-
59-
```bash
60-
# condition names will be checked, and 'has_time_travel' is valid
61-
rustc --cfg 'has_time_travel' --check-cfg 'names()'
62-
63-
# condition names will be checked, and 'has_time_travel' is valid
64-
rustc --cfg 'has_time_travel' --check-cfg 'names(has_time_travel)'
65-
```
66-
67-
In contrast, the following two invocations are _not_ equivalent:
68-
69-
```bash
70-
# condition names will not be checked (because there is no --check-cfg names(...))
71-
rustc --cfg 'has_time_travel'
72-
73-
# condition names will be checked, and 'has_time_travel' is both valid and enabled.
74-
rustc --cfg 'has_time_travel' --check-cfg 'names(has_time_travel)'
75-
```
76-
7759
## The `values(...)` form
7860

7961
The `values(...)` form enables checking the values within list-valued conditions. It has this
@@ -149,7 +131,7 @@ fn tame_lion() {}
149131
```bash
150132
# This turns on checking for condition names, but not values, such as 'feature' values.
151133
rustc --check-cfg 'names(is_embedded, has_feathers)' \
152-
--cfg has_feathers --cfg 'feature = "zapping"' -Z unstable-options
134+
--cfg has_feathers -Z unstable-options
153135
```
154136
155137
```rust
@@ -159,13 +141,14 @@ fn do_embedded() {}
159141
#[cfg(has_feathers)] // This is expected as "has_feathers" was provided in names()
160142
fn do_features() {}
161143
144+
#[cfg(has_feathers = "zapping")] // This is expected as "has_feathers" was provided in names()
145+
// and because no value checking was enable for "has_feathers"
146+
// no warning is emited for the value "zapping"
147+
fn do_zapping() {}
148+
162149
#[cfg(has_mumble_frotz)] // This is UNEXPECTED because names checking is enable and
163150
// "has_mumble_frotz" was not provided in names()
164151
fn do_mumble_frotz() {}
165-
166-
#[cfg(feature = "lasers")] // This doesn't raise a warning, because values checking for "feature"
167-
// was never used
168-
fn shoot_lasers() {}
169152
```
170153
171154
### Example: Checking feature values, but not condition names

src/test/ui/check-cfg/invalid-cfg-value.rs

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pub fn f() {}
1212
pub fn g() {}
1313

1414
#[cfg(feature = "rand")]
15+
//~^ WARNING unexpected `cfg` condition value
1516
pub fn h() {}
1617

1718
pub fn main() {}

src/test/ui/check-cfg/invalid-cfg-value.stderr

+10-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,15 @@ LL | #[cfg(feature = "sedre")]
55
| ^^^^^^^^^^^^^^^^^
66
|
77
= note: `#[warn(unexpected_cfgs)]` on by default
8-
= note: expected values for `feature` are: full, rand, serde
8+
= note: expected values for `feature` are: full, serde
99

10-
warning: 1 warning emitted
10+
warning: unexpected `cfg` condition value
11+
--> $DIR/invalid-cfg-value.rs:14:7
12+
|
13+
LL | #[cfg(feature = "rand")]
14+
| ^^^^^^^^^^^^^^^^
15+
|
16+
= note: expected values for `feature` are: full, serde
17+
18+
warning: 2 warnings emitted
1119

src/test/ui/check-cfg/mix.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
// This test checks the combination of well known names, their activation via names(), the usage of
2-
// partial values() with a --cfg and test that we also correctly lint on the `cfg!` macro and
3-
// `cfg_attr` attribute.
1+
// This test checks the combination of well known names, their activation via names(),
2+
// the usage of values(), and that no implicit is done with --cfg while also testing that
3+
// we correctly lint on the `cfg!` macro and `cfg_attr` attribute.
44
//
55
// check-pass
66
// compile-flags: --check-cfg=names() --check-cfg=values(feature,"foo") --cfg feature="bar" -Z unstable-options
@@ -16,6 +16,7 @@ fn do_windows_stuff() {}
1616
fn use_foo() {}
1717

1818
#[cfg(feature = "bar")]
19+
//~^ WARNING unexpected `cfg` condition value
1920
fn use_bar() {}
2021

2122
#[cfg(feature = "zebra")]
@@ -35,6 +36,7 @@ fn test_cfg_macro() {
3536
//~^ WARNING unexpected `cfg` condition name
3637
cfg!(feature = "foo");
3738
cfg!(feature = "bar");
39+
//~^ WARNING unexpected `cfg` condition value
3840
cfg!(feature = "zebra");
3941
//~^ WARNING unexpected `cfg` condition value
4042
cfg!(xxx = "foo");

src/test/ui/check-cfg/mix.stderr

+47-33
Original file line numberDiff line numberDiff line change
@@ -7,154 +7,168 @@ LL | #[cfg(widnows)]
77
= note: `#[warn(unexpected_cfgs)]` on by default
88

99
warning: unexpected `cfg` condition value
10-
--> $DIR/mix.rs:21:7
10+
--> $DIR/mix.rs:18:7
11+
|
12+
LL | #[cfg(feature = "bar")]
13+
| ^^^^^^^^^^^^^^^
14+
|
15+
= note: expected values for `feature` are: foo
16+
17+
warning: unexpected `cfg` condition value
18+
--> $DIR/mix.rs:22:7
1119
|
1220
LL | #[cfg(feature = "zebra")]
1321
| ^^^^^^^^^^^^^^^^^
1422
|
15-
= note: expected values for `feature` are: bar, foo
23+
= note: expected values for `feature` are: foo
1624

1725
warning: unexpected `cfg` condition name
18-
--> $DIR/mix.rs:25:12
26+
--> $DIR/mix.rs:26:12
1927
|
2028
LL | #[cfg_attr(uu, test)]
2129
| ^^
2230

2331
warning: unexpected `cfg` condition name
24-
--> $DIR/mix.rs:34:10
32+
--> $DIR/mix.rs:35:10
2533
|
2634
LL | cfg!(widnows);
2735
| ^^^^^^^ help: did you mean: `windows`
2836

2937
warning: unexpected `cfg` condition value
3038
--> $DIR/mix.rs:38:10
3139
|
40+
LL | cfg!(feature = "bar");
41+
| ^^^^^^^^^^^^^^^
42+
|
43+
= note: expected values for `feature` are: foo
44+
45+
warning: unexpected `cfg` condition value
46+
--> $DIR/mix.rs:40:10
47+
|
3248
LL | cfg!(feature = "zebra");
3349
| ^^^^^^^^^^^^^^^^^
3450
|
35-
= note: expected values for `feature` are: bar, foo
51+
= note: expected values for `feature` are: foo
3652

3753
warning: unexpected `cfg` condition name
38-
--> $DIR/mix.rs:40:10
54+
--> $DIR/mix.rs:42:10
3955
|
4056
LL | cfg!(xxx = "foo");
4157
| ^^^^^^^^^^^
4258

4359
warning: unexpected `cfg` condition name
44-
--> $DIR/mix.rs:42:10
60+
--> $DIR/mix.rs:44:10
4561
|
4662
LL | cfg!(xxx);
4763
| ^^^
4864

4965
warning: unexpected `cfg` condition name
50-
--> $DIR/mix.rs:44:14
66+
--> $DIR/mix.rs:46:14
5167
|
5268
LL | cfg!(any(xxx, windows));
5369
| ^^^
5470

5571
warning: unexpected `cfg` condition value
56-
--> $DIR/mix.rs:46:14
72+
--> $DIR/mix.rs:48:14
5773
|
5874
LL | cfg!(any(feature = "bad", windows));
59-
| ^^^^^^^^^^-----
60-
| |
61-
| help: did you mean: `"bar"`
75+
| ^^^^^^^^^^^^^^^
6276
|
63-
= note: expected values for `feature` are: bar, foo
77+
= note: expected values for `feature` are: foo
6478

6579
warning: unexpected `cfg` condition name
66-
--> $DIR/mix.rs:48:23
80+
--> $DIR/mix.rs:50:23
6781
|
6882
LL | cfg!(any(windows, xxx));
6983
| ^^^
7084

7185
warning: unexpected `cfg` condition name
72-
--> $DIR/mix.rs:50:20
86+
--> $DIR/mix.rs:52:20
7387
|
7488
LL | cfg!(all(unix, xxx));
7589
| ^^^
7690

7791
warning: unexpected `cfg` condition name
78-
--> $DIR/mix.rs:52:14
92+
--> $DIR/mix.rs:54:14
7993
|
8094
LL | cfg!(all(aa, bb));
8195
| ^^
8296

8397
warning: unexpected `cfg` condition name
84-
--> $DIR/mix.rs:52:18
98+
--> $DIR/mix.rs:54:18
8599
|
86100
LL | cfg!(all(aa, bb));
87101
| ^^
88102

89103
warning: unexpected `cfg` condition name
90-
--> $DIR/mix.rs:55:14
104+
--> $DIR/mix.rs:57:14
91105
|
92106
LL | cfg!(any(aa, bb));
93107
| ^^
94108

95109
warning: unexpected `cfg` condition name
96-
--> $DIR/mix.rs:55:18
110+
--> $DIR/mix.rs:57:18
97111
|
98112
LL | cfg!(any(aa, bb));
99113
| ^^
100114

101115
warning: unexpected `cfg` condition value
102-
--> $DIR/mix.rs:58:20
116+
--> $DIR/mix.rs:60:20
103117
|
104118
LL | cfg!(any(unix, feature = "zebra"));
105119
| ^^^^^^^^^^^^^^^^^
106120
|
107-
= note: expected values for `feature` are: bar, foo
121+
= note: expected values for `feature` are: foo
108122

109123
warning: unexpected `cfg` condition name
110-
--> $DIR/mix.rs:60:14
124+
--> $DIR/mix.rs:62:14
111125
|
112126
LL | cfg!(any(xxx, feature = "zebra"));
113127
| ^^^
114128

115129
warning: unexpected `cfg` condition value
116-
--> $DIR/mix.rs:60:19
130+
--> $DIR/mix.rs:62:19
117131
|
118132
LL | cfg!(any(xxx, feature = "zebra"));
119133
| ^^^^^^^^^^^^^^^^^
120134
|
121-
= note: expected values for `feature` are: bar, foo
135+
= note: expected values for `feature` are: foo
122136

123137
warning: unexpected `cfg` condition name
124-
--> $DIR/mix.rs:63:14
138+
--> $DIR/mix.rs:65:14
125139
|
126140
LL | cfg!(any(xxx, unix, xxx));
127141
| ^^^
128142

129143
warning: unexpected `cfg` condition name
130-
--> $DIR/mix.rs:63:25
144+
--> $DIR/mix.rs:65:25
131145
|
132146
LL | cfg!(any(xxx, unix, xxx));
133147
| ^^^
134148

135149
warning: unexpected `cfg` condition value
136-
--> $DIR/mix.rs:66:14
150+
--> $DIR/mix.rs:68:14
137151
|
138152
LL | cfg!(all(feature = "zebra", feature = "zebra", feature = "zebra"));
139153
| ^^^^^^^^^^^^^^^^^
140154
|
141-
= note: expected values for `feature` are: bar, foo
155+
= note: expected values for `feature` are: foo
142156

143157
warning: unexpected `cfg` condition value
144-
--> $DIR/mix.rs:66:33
158+
--> $DIR/mix.rs:68:33
145159
|
146160
LL | cfg!(all(feature = "zebra", feature = "zebra", feature = "zebra"));
147161
| ^^^^^^^^^^^^^^^^^
148162
|
149-
= note: expected values for `feature` are: bar, foo
163+
= note: expected values for `feature` are: foo
150164

151165
warning: unexpected `cfg` condition value
152-
--> $DIR/mix.rs:66:52
166+
--> $DIR/mix.rs:68:52
153167
|
154168
LL | cfg!(all(feature = "zebra", feature = "zebra", feature = "zebra"));
155169
| ^^^^^^^^^^^^^^^^^
156170
|
157-
= note: expected values for `feature` are: bar, foo
171+
= note: expected values for `feature` are: foo
158172

159-
warning: 23 warnings emitted
173+
warning: 25 warnings emitted
160174

0 commit comments

Comments
 (0)