Skip to content

Commit dfa6ff7

Browse files
authored
Rollup merge of rust-lang#117981 - Urgau:check-cfg-remove-deprecated-syntax, r=b-naber
Remove deprecated `--check-cfg` syntax This PR removes the deprecated `--check-cfg` `names(...)` and `values(...)` syntax. Follow up to rust-lang#111072 Part of rust-lang/compiler-team#636 r? compiler
2 parents 2e97bce + 623e99e commit dfa6ff7

37 files changed

+180
-403
lines changed

compiler/rustc_interface/src/interface.rs

+83-157
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;
@@ -104,7 +104,6 @@ pub(crate) fn parse_check_cfg(handler: &EarlyErrorHandler, specs: Vec<String>) -
104104
let exhaustive_values = !specs.is_empty();
105105
let mut check_cfg = CheckCfg { exhaustive_names, exhaustive_values, ..CheckCfg::default() };
106106

107-
let mut old_syntax = None;
108107
for s in specs {
109108
let sess = ParseSess::with_silent_emitter(Some(format!(
110109
"this error occurred on the command line: `--check-cfg={s}`"
@@ -142,174 +141,101 @@ pub(crate) fn parse_check_cfg(handler: &EarlyErrorHandler, specs: Vec<String>) -
142141
expected_error();
143142
};
144143

145-
let mut set_old_syntax = || {
146-
// defaults are flipped for the old syntax
147-
if old_syntax == None {
148-
check_cfg.exhaustive_names = false;
149-
check_cfg.exhaustive_values = false;
150-
}
151-
old_syntax = Some(true);
152-
};
153-
154-
if meta_item.has_name(sym::names) {
155-
set_old_syntax();
156-
157-
check_cfg.exhaustive_names = true;
158-
for arg in args {
159-
if arg.is_word()
160-
&& let Some(ident) = arg.ident()
161-
{
162-
check_cfg.expecteds.entry(ident.name).or_insert(ExpectedValues::Any);
163-
} else {
164-
error!("`names()` arguments must be simple identifiers");
165-
}
166-
}
167-
} else if meta_item.has_name(sym::values) {
168-
set_old_syntax();
169-
170-
if let Some((name, values)) = args.split_first() {
171-
if name.is_word()
172-
&& let Some(ident) = name.ident()
173-
{
174-
let expected_values = check_cfg
175-
.expecteds
176-
.entry(ident.name)
177-
.and_modify(|expected_values| match expected_values {
178-
ExpectedValues::Some(_) => {}
179-
ExpectedValues::Any => {
180-
// handle the case where names(...) was done
181-
// before values by changing to a list
182-
*expected_values = ExpectedValues::Some(FxHashSet::default());
183-
}
184-
})
185-
.or_insert_with(|| ExpectedValues::Some(FxHashSet::default()));
144+
if !meta_item.has_name(sym::cfg) {
145+
expected_error();
146+
}
186147

187-
let ExpectedValues::Some(expected_values) = expected_values else {
188-
bug!("`expected_values` should be a list a values")
189-
};
148+
let mut names = Vec::new();
149+
let mut values: FxHashSet<_> = Default::default();
190150

191-
for val in values {
192-
if let Some(LitKind::Str(s, _)) = val.lit().map(|lit| &lit.kind) {
193-
expected_values.insert(Some(*s));
194-
} else {
195-
error!("`values()` arguments must be string literals");
196-
}
197-
}
151+
let mut any_specified = false;
152+
let mut values_specified = false;
153+
let mut values_any_specified = false;
198154

199-
if values.is_empty() {
200-
expected_values.insert(None);
201-
}
202-
} else {
203-
error!("`values()` first argument must be a simple identifier");
155+
for arg in args {
156+
if arg.is_word()
157+
&& let Some(ident) = arg.ident()
158+
{
159+
if values_specified {
160+
error!("`cfg()` names cannot be after values");
204161
}
205-
} else if args.is_empty() {
206-
check_cfg.exhaustive_values = true;
207-
} else {
208-
expected_error();
209-
}
210-
} else if meta_item.has_name(sym::cfg) {
211-
old_syntax = Some(false);
212-
213-
let mut names = Vec::new();
214-
let mut values: FxHashSet<_> = Default::default();
215-
216-
let mut any_specified = false;
217-
let mut values_specified = false;
218-
let mut values_any_specified = false;
219-
220-
for arg in args {
221-
if arg.is_word()
222-
&& let Some(ident) = arg.ident()
223-
{
224-
if values_specified {
225-
error!("`cfg()` names cannot be after values");
226-
}
227-
names.push(ident);
228-
} else if arg.has_name(sym::any)
229-
&& let Some(args) = arg.meta_item_list()
230-
{
231-
if any_specified {
232-
error!("`any()` cannot be specified multiple times");
233-
}
234-
any_specified = true;
235-
if !args.is_empty() {
236-
error!("`any()` must be empty");
237-
}
238-
} else if arg.has_name(sym::values)
239-
&& let Some(args) = arg.meta_item_list()
240-
{
241-
if names.is_empty() {
242-
error!("`values()` cannot be specified before the names");
243-
} else if values_specified {
244-
error!("`values()` cannot be specified multiple times");
245-
}
246-
values_specified = true;
247-
248-
for arg in args {
249-
if let Some(LitKind::Str(s, _)) = arg.lit().map(|lit| &lit.kind) {
250-
values.insert(Some(*s));
251-
} else if arg.has_name(sym::any)
252-
&& let Some(args) = arg.meta_item_list()
253-
{
254-
if values_any_specified {
255-
error!("`any()` in `values()` cannot be specified multiple times");
256-
}
257-
values_any_specified = true;
258-
if !args.is_empty() {
259-
error!("`any()` must be empty");
260-
}
261-
} else {
262-
error!("`values()` arguments must be string literals or `any()`");
162+
names.push(ident);
163+
} else if arg.has_name(sym::any)
164+
&& let Some(args) = arg.meta_item_list()
165+
{
166+
if any_specified {
167+
error!("`any()` cannot be specified multiple times");
168+
}
169+
any_specified = true;
170+
if !args.is_empty() {
171+
error!("`any()` must be empty");
172+
}
173+
} else if arg.has_name(sym::values)
174+
&& let Some(args) = arg.meta_item_list()
175+
{
176+
if names.is_empty() {
177+
error!("`values()` cannot be specified before the names");
178+
} else if values_specified {
179+
error!("`values()` cannot be specified multiple times");
180+
}
181+
values_specified = true;
182+
183+
for arg in args {
184+
if let Some(LitKind::Str(s, _)) = arg.lit().map(|lit| &lit.kind) {
185+
values.insert(Some(*s));
186+
} else if arg.has_name(sym::any)
187+
&& let Some(args) = arg.meta_item_list()
188+
{
189+
if values_any_specified {
190+
error!("`any()` in `values()` cannot be specified multiple times");
191+
}
192+
values_any_specified = true;
193+
if !args.is_empty() {
194+
error!("`any()` must be empty");
263195
}
196+
} else {
197+
error!("`values()` arguments must be string literals or `any()`");
264198
}
265-
} else {
266-
error!(
267-
"`cfg()` arguments must be simple identifiers, `any()` or `values(...)`"
268-
);
269199
}
200+
} else {
201+
error!("`cfg()` arguments must be simple identifiers, `any()` or `values(...)`");
270202
}
203+
}
271204

272-
if values.is_empty() && !values_any_specified && !any_specified {
273-
values.insert(None);
274-
} else if !values.is_empty() && values_any_specified {
275-
error!(
276-
"`values()` arguments cannot specify string literals and `any()` at the same time"
277-
);
278-
}
205+
if values.is_empty() && !values_any_specified && !any_specified {
206+
values.insert(None);
207+
} else if !values.is_empty() && values_any_specified {
208+
error!(
209+
"`values()` arguments cannot specify string literals and `any()` at the same time"
210+
);
211+
}
279212

280-
if any_specified {
281-
if names.is_empty()
282-
&& values.is_empty()
283-
&& !values_specified
284-
&& !values_any_specified
285-
{
286-
check_cfg.exhaustive_names = false;
287-
} else {
288-
error!("`cfg(any())` can only be provided in isolation");
289-
}
213+
if any_specified {
214+
if names.is_empty() && values.is_empty() && !values_specified && !values_any_specified {
215+
check_cfg.exhaustive_names = false;
290216
} else {
291-
for name in names {
292-
check_cfg
293-
.expecteds
294-
.entry(name.name)
295-
.and_modify(|v| match v {
296-
ExpectedValues::Some(v) if !values_any_specified => {
297-
v.extend(values.clone())
298-
}
299-
ExpectedValues::Some(_) => *v = ExpectedValues::Any,
300-
ExpectedValues::Any => {}
301-
})
302-
.or_insert_with(|| {
303-
if values_any_specified {
304-
ExpectedValues::Any
305-
} else {
306-
ExpectedValues::Some(values.clone())
307-
}
308-
});
309-
}
217+
error!("`cfg(any())` can only be provided in isolation");
310218
}
311219
} else {
312-
expected_error();
220+
for name in names {
221+
check_cfg
222+
.expecteds
223+
.entry(name.name)
224+
.and_modify(|v| match v {
225+
ExpectedValues::Some(v) if !values_any_specified => {
226+
v.extend(values.clone())
227+
}
228+
ExpectedValues::Some(_) => *v = ExpectedValues::Any,
229+
ExpectedValues::Any => {}
230+
})
231+
.or_insert_with(|| {
232+
if values_any_specified {
233+
ExpectedValues::Any
234+
} else {
235+
ExpectedValues::Some(values.clone())
236+
}
237+
});
238+
}
313239
}
314240
}
315241

compiler/rustc_lint_defs/src/builtin.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -3135,7 +3135,7 @@ declare_lint! {
31353135
/// ### Example
31363136
///
31373137
/// ```text
3138-
/// rustc --check-cfg 'names()'
3138+
/// rustc --check-cfg 'cfg()'
31393139
/// ```
31403140
///
31413141
/// ```rust,ignore (needs command line option)
@@ -3146,7 +3146,7 @@ declare_lint! {
31463146
/// This will produce:
31473147
///
31483148
/// ```text
3149-
/// warning: unknown condition name used
3149+
/// warning: unexpected `cfg` condition name: `widnows`
31503150
/// --> lint_example.rs:1:7
31513151
/// |
31523152
/// 1 | #[cfg(widnows)]
@@ -3157,9 +3157,10 @@ declare_lint! {
31573157
///
31583158
/// ### Explanation
31593159
///
3160-
/// This lint is only active when a `--check-cfg='names(...)'` option has been passed
3161-
/// to the compiler and triggers whenever an unknown condition name or value is used.
3162-
/// The known condition include names or values passed in `--check-cfg`, `--cfg`, and some
3160+
/// This lint is only active when `--check-cfg` arguments are being passed
3161+
/// to the compiler and triggers whenever an unexpected condition name or value is used.
3162+
///
3163+
/// The known condition include names or values passed in `--check-cfg`, and some
31633164
/// well-knows names and values built into the compiler.
31643165
pub UNEXPECTED_CFGS,
31653166
Warn,

compiler/rustc_llvm/build.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ fn output(cmd: &mut Command) -> String {
102102

103103
fn main() {
104104
for component in REQUIRED_COMPONENTS.iter().chain(OPTIONAL_COMPONENTS.iter()) {
105-
println!("cargo:rustc-check-cfg=values(llvm_component,\"{component}\")");
105+
println!("cargo:rustc-check-cfg=cfg(llvm_component,values(\"{component}\"))");
106106
}
107107

108108
if tracked_env_var_os("RUST_CHECK").is_some() {

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

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

0 commit comments

Comments
 (0)