Skip to content

Commit 2c4f3b8

Browse files
committed
Remove deprecated --check-cfg names() and values() syntax
1 parent 1be1e84 commit 2c4f3b8

39 files changed

+171
-657
lines changed

compiler/rustc_interface/src/interface.rs

Lines changed: 83 additions & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use rustc_data_structures::sync::Lrc;
1010
use rustc_errors::registry::Registry;
1111
use rustc_errors::{ErrorGuaranteed, Handler};
1212
use rustc_lint::LintStore;
13+
use rustc_middle::ty;
1314
use rustc_middle::util::Providers;
14-
use rustc_middle::{bug, ty};
1515
use rustc_parse::maybe_new_parser_from_source_str;
1616
use rustc_query_impl::QueryCtxt;
1717
use rustc_query_system::query::print_query_stack;
@@ -126,7 +126,6 @@ pub(crate) fn parse_check_cfg(handler: &EarlyErrorHandler, specs: Vec<String>) -
126126
let exhaustive_values = !specs.is_empty();
127127
let mut check_cfg = CheckCfg { exhaustive_names, exhaustive_values, ..CheckCfg::default() };
128128

129-
let mut old_syntax = None;
130129
for s in specs {
131130
let sess = ParseSess::with_silent_emitter(Some(format!(
132131
"this error occurred on the command line: `--check-cfg={s}`"
@@ -164,174 +163,101 @@ pub(crate) fn parse_check_cfg(handler: &EarlyErrorHandler, specs: Vec<String>) -
164163
expected_error();
165164
};
166165

167-
let mut set_old_syntax = || {
168-
// defaults are flipped for the old syntax
169-
if old_syntax == None {
170-
check_cfg.exhaustive_names = false;
171-
check_cfg.exhaustive_values = false;
172-
}
173-
old_syntax = Some(true);
174-
};
175-
176-
if meta_item.has_name(sym::names) {
177-
set_old_syntax();
178-
179-
check_cfg.exhaustive_names = true;
180-
for arg in args {
181-
if arg.is_word()
182-
&& let Some(ident) = arg.ident()
183-
{
184-
check_cfg.expecteds.entry(ident.name).or_insert(ExpectedValues::Any);
185-
} else {
186-
error!("`names()` arguments must be simple identifiers");
187-
}
188-
}
189-
} else if meta_item.has_name(sym::values) {
190-
set_old_syntax();
191-
192-
if let Some((name, values)) = args.split_first() {
193-
if name.is_word()
194-
&& let Some(ident) = name.ident()
195-
{
196-
let expected_values = check_cfg
197-
.expecteds
198-
.entry(ident.name)
199-
.and_modify(|expected_values| match expected_values {
200-
ExpectedValues::Some(_) => {}
201-
ExpectedValues::Any => {
202-
// handle the case where names(...) was done
203-
// before values by changing to a list
204-
*expected_values = ExpectedValues::Some(FxHashSet::default());
205-
}
206-
})
207-
.or_insert_with(|| ExpectedValues::Some(FxHashSet::default()));
166+
if !meta_item.has_name(sym::cfg) {
167+
expected_error();
168+
}
208169

209-
let ExpectedValues::Some(expected_values) = expected_values else {
210-
bug!("`expected_values` should be a list a values")
211-
};
170+
let mut names = Vec::new();
171+
let mut values: FxHashSet<_> = Default::default();
212172

213-
for val in values {
214-
if let Some(LitKind::Str(s, _)) = val.lit().map(|lit| &lit.kind) {
215-
expected_values.insert(Some(*s));
216-
} else {
217-
error!("`values()` arguments must be string literals");
218-
}
219-
}
173+
let mut any_specified = false;
174+
let mut values_specified = false;
175+
let mut values_any_specified = false;
220176

221-
if values.is_empty() {
222-
expected_values.insert(None);
223-
}
224-
} else {
225-
error!("`values()` first argument must be a simple identifier");
177+
for arg in args {
178+
if arg.is_word()
179+
&& let Some(ident) = arg.ident()
180+
{
181+
if values_specified {
182+
error!("`cfg()` names cannot be after values");
226183
}
227-
} else if args.is_empty() {
228-
check_cfg.exhaustive_values = true;
229-
} else {
230-
expected_error();
231-
}
232-
} else if meta_item.has_name(sym::cfg) {
233-
old_syntax = Some(false);
234-
235-
let mut names = Vec::new();
236-
let mut values: FxHashSet<_> = Default::default();
237-
238-
let mut any_specified = false;
239-
let mut values_specified = false;
240-
let mut values_any_specified = false;
241-
242-
for arg in args {
243-
if arg.is_word()
244-
&& let Some(ident) = arg.ident()
245-
{
246-
if values_specified {
247-
error!("`cfg()` names cannot be after values");
248-
}
249-
names.push(ident);
250-
} else if arg.has_name(sym::any)
251-
&& let Some(args) = arg.meta_item_list()
252-
{
253-
if any_specified {
254-
error!("`any()` cannot be specified multiple times");
255-
}
256-
any_specified = true;
257-
if !args.is_empty() {
258-
error!("`any()` must be empty");
259-
}
260-
} else if arg.has_name(sym::values)
261-
&& let Some(args) = arg.meta_item_list()
262-
{
263-
if names.is_empty() {
264-
error!("`values()` cannot be specified before the names");
265-
} else if values_specified {
266-
error!("`values()` cannot be specified multiple times");
267-
}
268-
values_specified = true;
269-
270-
for arg in args {
271-
if let Some(LitKind::Str(s, _)) = arg.lit().map(|lit| &lit.kind) {
272-
values.insert(Some(*s));
273-
} else if arg.has_name(sym::any)
274-
&& let Some(args) = arg.meta_item_list()
275-
{
276-
if values_any_specified {
277-
error!("`any()` in `values()` cannot be specified multiple times");
278-
}
279-
values_any_specified = true;
280-
if !args.is_empty() {
281-
error!("`any()` must be empty");
282-
}
283-
} else {
284-
error!("`values()` arguments must be string literals or `any()`");
184+
names.push(ident);
185+
} else if arg.has_name(sym::any)
186+
&& let Some(args) = arg.meta_item_list()
187+
{
188+
if any_specified {
189+
error!("`any()` cannot be specified multiple times");
190+
}
191+
any_specified = true;
192+
if !args.is_empty() {
193+
error!("`any()` must be empty");
194+
}
195+
} else if arg.has_name(sym::values)
196+
&& let Some(args) = arg.meta_item_list()
197+
{
198+
if names.is_empty() {
199+
error!("`values()` cannot be specified before the names");
200+
} else if values_specified {
201+
error!("`values()` cannot be specified multiple times");
202+
}
203+
values_specified = true;
204+
205+
for arg in args {
206+
if let Some(LitKind::Str(s, _)) = arg.lit().map(|lit| &lit.kind) {
207+
values.insert(Some(*s));
208+
} else if arg.has_name(sym::any)
209+
&& let Some(args) = arg.meta_item_list()
210+
{
211+
if values_any_specified {
212+
error!("`any()` in `values()` cannot be specified multiple times");
213+
}
214+
values_any_specified = true;
215+
if !args.is_empty() {
216+
error!("`any()` must be empty");
285217
}
218+
} else {
219+
error!("`values()` arguments must be string literals or `any()`");
286220
}
287-
} else {
288-
error!(
289-
"`cfg()` arguments must be simple identifiers, `any()` or `values(...)`"
290-
);
291221
}
222+
} else {
223+
error!("`cfg()` arguments must be simple identifiers, `any()` or `values(...)`");
292224
}
225+
}
293226

294-
if values.is_empty() && !values_any_specified && !any_specified {
295-
values.insert(None);
296-
} else if !values.is_empty() && values_any_specified {
297-
error!(
298-
"`values()` arguments cannot specify string literals and `any()` at the same time"
299-
);
300-
}
227+
if values.is_empty() && !values_any_specified && !any_specified {
228+
values.insert(None);
229+
} else if !values.is_empty() && values_any_specified {
230+
error!(
231+
"`values()` arguments cannot specify string literals and `any()` at the same time"
232+
);
233+
}
301234

302-
if any_specified {
303-
if names.is_empty()
304-
&& values.is_empty()
305-
&& !values_specified
306-
&& !values_any_specified
307-
{
308-
check_cfg.exhaustive_names = false;
309-
} else {
310-
error!("`cfg(any())` can only be provided in isolation");
311-
}
235+
if any_specified {
236+
if names.is_empty() && values.is_empty() && !values_specified && !values_any_specified {
237+
check_cfg.exhaustive_names = false;
312238
} else {
313-
for name in names {
314-
check_cfg
315-
.expecteds
316-
.entry(name.name)
317-
.and_modify(|v| match v {
318-
ExpectedValues::Some(v) if !values_any_specified => {
319-
v.extend(values.clone())
320-
}
321-
ExpectedValues::Some(_) => *v = ExpectedValues::Any,
322-
ExpectedValues::Any => {}
323-
})
324-
.or_insert_with(|| {
325-
if values_any_specified {
326-
ExpectedValues::Any
327-
} else {
328-
ExpectedValues::Some(values.clone())
329-
}
330-
});
331-
}
239+
error!("`cfg(any())` can only be provided in isolation");
332240
}
333241
} else {
334-
expected_error();
242+
for name in names {
243+
check_cfg
244+
.expecteds
245+
.entry(name.name)
246+
.and_modify(|v| match v {
247+
ExpectedValues::Some(v) if !values_any_specified => {
248+
v.extend(values.clone())
249+
}
250+
ExpectedValues::Some(_) => *v = ExpectedValues::Any,
251+
ExpectedValues::Any => {}
252+
})
253+
.or_insert_with(|| {
254+
if values_any_specified {
255+
ExpectedValues::Any
256+
} else {
257+
ExpectedValues::Some(values.clone())
258+
}
259+
});
260+
}
335261
}
336262
}
337263

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

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -187,70 +187,3 @@ fn shoot_lasers() {}
187187
// the cfg(feature) list
188188
fn write_shakespeare() {}
189189
```
190-
191-
## The deprecated `names(...)` form
192-
193-
The `names(...)` form enables checking the names. This form uses a named list:
194-
195-
```bash
196-
rustc --check-cfg 'names(name1, name2, ... nameN)'
197-
```
198-
199-
where each `name` is a bare identifier (has no quotes). The order of the names is not significant.
200-
201-
If `--check-cfg names(...)` is specified at least once, then `rustc` will check all references to
202-
condition names. `rustc` will check every `#[cfg]` attribute, `#[cfg_attr]` attribute, `cfg` clause
203-
inside `#[link]` attribute and `cfg!(...)` call against the provided list of expected condition
204-
names. If a name is not present in this list, then `rustc` will report an `unexpected_cfgs` lint
205-
diagnostic. The default diagnostic level for this lint is `Warn`.
206-
207-
If `--check-cfg names(...)` is not specified, then `rustc` will not check references to condition
208-
names.
209-
210-
`--check-cfg names(...)` may be specified more than once. The result is that the list of valid
211-
condition names is merged across all options. It is legal for a condition name to be specified
212-
more than once; redundantly specifying a condition name has no effect.
213-
214-
To enable checking condition names with an empty set of valid condition names, use the following
215-
form. The parentheses are required.
216-
217-
```bash
218-
rustc --check-cfg 'names()'
219-
```
220-
221-
Note that `--check-cfg 'names()'` is _not_ equivalent to omitting the option entirely.
222-
The first form enables checking condition names, while specifying that there are no valid
223-
condition names (outside of the set of well-known names defined by `rustc`). Omitting the
224-
`--check-cfg 'names(...)'` option does not enable checking condition names.
225-
226-
## The deprecated `values(...)` form
227-
228-
The `values(...)` form enables checking the values within list-valued conditions. It has this
229-
form:
230-
231-
```bash
232-
rustc --check-cfg `values(name, "value1", "value2", ... "valueN")'
233-
```
234-
235-
where `name` is a bare identifier (has no quotes) and each `"value"` term is a quoted literal
236-
string. `name` specifies the name of the condition, such as `feature` or `target_os`.
237-
238-
When the `values(...)` option is specified, `rustc` will check every `#[cfg(name = "value")]`
239-
attribute, `#[cfg_attr(name = "value")]` attribute, `#[link(name = "a", cfg(name = "value"))]`
240-
and `cfg!(name = "value")` call. It will check that the `"value"` specified is present in the
241-
list of expected values. If `"value"` is not in it, then `rustc` will report an `unexpected_cfgs`
242-
lint diagnostic. The default diagnostic level for this lint is `Warn`.
243-
244-
To enable checking of values, but to provide an empty set of valid values, use this form:
245-
246-
```bash
247-
rustc --check-cfg `values(name)`
248-
```
249-
250-
The `--check-cfg values(...)` option can be repeated, both for the same condition name and for
251-
different names. If it is repeated for the same condition name, then the sets of values for that
252-
condition are merged together.
253-
254-
If `values()` is specified, then `rustc` will enable the checking of well-known values defined
255-
by itself. Note that it's necessary to specify the `values()` form to enable the checking of
256-
well known values, specifying the other forms doesn't implicitly enable it.

tests/ui/check-cfg/exhaustive-names-values.empty_cfg.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
warning: unexpected `cfg` condition name: `unknown_key`
2-
--> $DIR/exhaustive-names-values.rs:12:7
2+
--> $DIR/exhaustive-names-values.rs:11:7
33
|
44
LL | #[cfg(unknown_key = "value")]
55
| ^^^^^^^^^^^^^^^^^^^^^
@@ -8,7 +8,7 @@ LL | #[cfg(unknown_key = "value")]
88
= note: `#[warn(unexpected_cfgs)]` on by default
99

1010
warning: unexpected `cfg` condition value: `value`
11-
--> $DIR/exhaustive-names-values.rs:16:7
11+
--> $DIR/exhaustive-names-values.rs:15:7
1212
|
1313
LL | #[cfg(test = "value")]
1414
| ^^^^----------

tests/ui/check-cfg/exhaustive-names-values.empty_names_values.stderr

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)