Skip to content

Commit 855c683

Browse files
committed
Auto merge of rust-lang#118071 - Urgau:check-cfg-cargo-feature, r=petrochenkov
Remove `feature` from the list of well known check-cfg name This PR removes `feature` from the list of well known check-cfg. This is done for multiple reasons: - Cargo is the source of truth, rustc shouldn't have any knowledge of it - It creates a conflict between Cargo and rustc when there are no features defined. In this case Cargo won't pass any `--check-cfg` for `feature` since no feature will ever be passed, but rustc by having in it's list adds a implicit `cfg(feature, values(any()))` which is completely wrong. Having any cfg `feature` is unexpected not allow any `feature` value. While doing this, I took the opportunity to specialise the diagnostic a bit for the case above. r? `@petrochenkov`
2 parents cc4bb0d + 54c122e commit 855c683

15 files changed

+80
-19
lines changed

compiler/rustc_lint/src/context.rs

+2
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,8 @@ pub trait LintContext {
739739
} else {
740740
db.span_suggestion(name_span, "there is a config with a similar name", best_match, Applicability::MaybeIncorrect);
741741
}
742+
} else if name == sym::feature && std::env::var_os("CARGO").is_some() {
743+
db.help("consider defining some features in `Cargo.toml`");
742744
} else if !possibilities.is_empty() {
743745
let mut possibilities = possibilities.iter()
744746
.map(Symbol::as_str)

compiler/rustc_session/src/config.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1443,9 +1443,8 @@ impl CheckCfg {
14431443
let relocation_model_values = RelocModel::all();
14441444

14451445
// Unknown possible values:
1446-
// - `feature`
14471446
// - `target_feature`
1448-
for name in [sym::feature, sym::target_feature] {
1447+
for name in [sym::target_feature] {
14491448
self.expecteds.entry(name).or_insert(ExpectedValues::Any);
14501449
}
14511450

src/bootstrap/src/bin/rustc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ fn main() {
114114
{
115115
cmd.arg("-Ztls-model=initial-exec");
116116
}
117-
} else {
117+
} else if std::env::var("MIRI").is_err() {
118118
// Find any host flags that were passed by bootstrap.
119119
// The flags are stored in a RUSTC_HOST_FLAGS variable, separated by spaces.
120120
if let Ok(flags) = std::env::var("RUSTC_HOST_FLAGS") {

tests/ui/check-cfg/allow-same-level.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ warning: unexpected `cfg` condition name: `FALSE`
44
LL | #[cfg(FALSE)]
55
| ^^^^^
66
|
7-
= help: expected names are: `debug_assertions`, `doc`, `doctest`, `feature`, `miri`, `overflow_checks`, `panic`, `proc_macro`, `relocation_model`, `sanitize`, `target_abi`, `target_arch`, `target_endian`, `target_env`, `target_family`, `target_feature`, `target_has_atomic`, `target_has_atomic_equal_alignment`, `target_has_atomic_load_store`, `target_os`, `target_pointer_width`, `target_thread_local`, `target_vendor`, `test`, `unix`, `windows`
7+
= help: expected names are: `debug_assertions`, `doc`, `doctest`, `miri`, `overflow_checks`, `panic`, `proc_macro`, `relocation_model`, `sanitize`, `target_abi`, `target_arch`, `target_endian`, `target_env`, `target_family`, `target_feature`, `target_has_atomic`, `target_has_atomic_equal_alignment`, `target_has_atomic_load_store`, `target_os`, `target_pointer_width`, `target_thread_local`, `target_vendor`, `test`, `unix`, `windows`
88
= note: `#[warn(unexpected_cfgs)]` on by default
99

1010
warning: 1 warning emitted

tests/ui/check-cfg/cargo-feature.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// This test checks that when no features are passed by Cargo we
2+
// suggest adding some in the Cargo.toml instead of vomitting a
3+
// list of all the expected names
4+
//
5+
// check-pass
6+
// rustc-env:CARGO=/usr/bin/cargo
7+
// compile-flags: --check-cfg=cfg() -Z unstable-options
8+
// error-pattern:Cargo.toml
9+
10+
#[cfg(feature = "serde")]
11+
//~^ WARNING unexpected `cfg` condition name
12+
fn ser() {}
13+
14+
fn main() {}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
warning: unexpected `cfg` condition name: `feature`
2+
--> $DIR/cargo-feature.rs:10:7
3+
|
4+
LL | #[cfg(feature = "serde")]
5+
| ^^^^^^^^^^^^^^^^^
6+
|
7+
= help: consider defining some features in `Cargo.toml`
8+
= note: `#[warn(unexpected_cfgs)]` on by default
9+
10+
warning: 1 warning emitted
11+

tests/ui/check-cfg/compact-names.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ warning: unexpected `cfg` condition name: `target_architecture`
44
LL | #[cfg(target(os = "linux", architecture = "arm"))]
55
| ^^^^^^^^^^^^^^^^^^^^
66
|
7-
= help: expected names are: `debug_assertions`, `doc`, `doctest`, `feature`, `miri`, `overflow_checks`, `panic`, `proc_macro`, `relocation_model`, `sanitize`, `target_abi`, `target_arch`, `target_endian`, `target_env`, `target_family`, `target_feature`, `target_has_atomic`, `target_has_atomic_equal_alignment`, `target_has_atomic_load_store`, `target_os`, `target_pointer_width`, `target_thread_local`, `target_vendor`, `test`, `unix`, `windows`
7+
= help: expected names are: `debug_assertions`, `doc`, `doctest`, `miri`, `overflow_checks`, `panic`, `proc_macro`, `relocation_model`, `sanitize`, `target_abi`, `target_arch`, `target_endian`, `target_env`, `target_family`, `target_feature`, `target_has_atomic`, `target_has_atomic_equal_alignment`, `target_has_atomic_load_store`, `target_os`, `target_pointer_width`, `target_thread_local`, `target_vendor`, `test`, `unix`, `windows`
88
= note: `#[warn(unexpected_cfgs)]` on by default
99

1010
warning: 1 warning emitted

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

+14-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ warning: unexpected `cfg` condition name: `unknown_key`
44
LL | #[cfg(unknown_key = "value")]
55
| ^^^^^^^^^^^^^^^^^^^^^
66
|
7-
= help: expected names are: `debug_assertions`, `doc`, `doctest`, `feature`, `miri`, `overflow_checks`, `panic`, `proc_macro`, `relocation_model`, `sanitize`, `target_abi`, `target_arch`, `target_endian`, `target_env`, `target_family`, `target_feature`, `target_has_atomic`, `target_has_atomic_equal_alignment`, `target_has_atomic_load_store`, `target_os`, `target_pointer_width`, `target_thread_local`, `target_vendor`, `test`, `unix`, `windows`
7+
= help: expected names are: `debug_assertions`, `doc`, `doctest`, `miri`, `overflow_checks`, `panic`, `proc_macro`, `relocation_model`, `sanitize`, `target_abi`, `target_arch`, `target_endian`, `target_env`, `target_family`, `target_feature`, `target_has_atomic`, `target_has_atomic_equal_alignment`, `target_has_atomic_load_store`, `target_os`, `target_pointer_width`, `target_thread_local`, `target_vendor`, `test`, `unix`, `windows`
88
= note: `#[warn(unexpected_cfgs)]` on by default
99

1010
warning: unexpected `cfg` condition value: `value`
@@ -17,5 +17,17 @@ LL | #[cfg(test = "value")]
1717
|
1818
= note: no expected value for `test`
1919

20-
warning: 2 warnings emitted
20+
warning: unexpected `cfg` condition name: `feature`
21+
--> $DIR/exhaustive-names-values.rs:19:7
22+
|
23+
LL | #[cfg(feature = "unk")]
24+
| ^^^^^^^^^^^^^^^
25+
26+
warning: unexpected `cfg` condition name: `feature`
27+
--> $DIR/exhaustive-names-values.rs:26:7
28+
|
29+
LL | #[cfg(feature = "std")]
30+
| ^^^^^^^^^^^^^^^
31+
32+
warning: 4 warnings emitted
2133

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

+14-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ warning: unexpected `cfg` condition name: `unknown_key`
44
LL | #[cfg(unknown_key = "value")]
55
| ^^^^^^^^^^^^^^^^^^^^^
66
|
7-
= help: expected names are: `debug_assertions`, `doc`, `doctest`, `feature`, `miri`, `overflow_checks`, `panic`, `proc_macro`, `relocation_model`, `sanitize`, `target_abi`, `target_arch`, `target_endian`, `target_env`, `target_family`, `target_feature`, `target_has_atomic`, `target_has_atomic_equal_alignment`, `target_has_atomic_load_store`, `target_os`, `target_pointer_width`, `target_thread_local`, `target_vendor`, `test`, `unix`, `windows`
7+
= help: expected names are: `debug_assertions`, `doc`, `doctest`, `miri`, `overflow_checks`, `panic`, `proc_macro`, `relocation_model`, `sanitize`, `target_abi`, `target_arch`, `target_endian`, `target_env`, `target_family`, `target_feature`, `target_has_atomic`, `target_has_atomic_equal_alignment`, `target_has_atomic_load_store`, `target_os`, `target_pointer_width`, `target_thread_local`, `target_vendor`, `test`, `unix`, `windows`
88
= note: `#[warn(unexpected_cfgs)]` on by default
99

1010
warning: unexpected `cfg` condition value: `value`
@@ -17,5 +17,17 @@ LL | #[cfg(test = "value")]
1717
|
1818
= note: no expected value for `test`
1919

20-
warning: 2 warnings emitted
20+
warning: unexpected `cfg` condition name: `feature`
21+
--> $DIR/exhaustive-names-values.rs:19:7
22+
|
23+
LL | #[cfg(feature = "unk")]
24+
| ^^^^^^^^^^^^^^^
25+
26+
warning: unexpected `cfg` condition name: `feature`
27+
--> $DIR/exhaustive-names-values.rs:26:7
28+
|
29+
LL | #[cfg(feature = "std")]
30+
| ^^^^^^^^^^^^^^^
31+
32+
warning: 4 warnings emitted
2133

tests/ui/check-cfg/exhaustive-names-values.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,15 @@ pub fn f() {}
1717
pub fn f() {}
1818

1919
#[cfg(feature = "unk")]
20-
//[feature]~^ WARNING unexpected `cfg` condition value
21-
//[full]~^^ WARNING unexpected `cfg` condition value
20+
//[empty_names_values]~^ WARNING unexpected `cfg` condition name
21+
//[empty_cfg]~^^ WARNING unexpected `cfg` condition name
22+
//[feature]~^^^ WARNING unexpected `cfg` condition value
23+
//[full]~^^^^ WARNING unexpected `cfg` condition value
2224
pub fn feat() {}
2325

2426
#[cfg(feature = "std")]
27+
//[empty_names_values]~^ WARNING unexpected `cfg` condition name
28+
//[empty_cfg]~^^ WARNING unexpected `cfg` condition name
2529
pub fn feat() {}
2630

2731
#[cfg(windows)]

tests/ui/check-cfg/exhaustive-names.empty_names.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ warning: unexpected `cfg` condition name: `unknown_key`
44
LL | #[cfg(unknown_key = "value")]
55
| ^^^^^^^^^^^^^^^^^^^^^
66
|
7-
= help: expected names are: `debug_assertions`, `doc`, `doctest`, `feature`, `miri`, `overflow_checks`, `panic`, `proc_macro`, `relocation_model`, `sanitize`, `target_abi`, `target_arch`, `target_endian`, `target_env`, `target_family`, `target_feature`, `target_has_atomic`, `target_has_atomic_equal_alignment`, `target_has_atomic_load_store`, `target_os`, `target_pointer_width`, `target_thread_local`, `target_vendor`, `test`, `unix`, `windows`
7+
= help: expected names are: `debug_assertions`, `doc`, `doctest`, `miri`, `overflow_checks`, `panic`, `proc_macro`, `relocation_model`, `sanitize`, `target_abi`, `target_arch`, `target_endian`, `target_env`, `target_family`, `target_feature`, `target_has_atomic`, `target_has_atomic_equal_alignment`, `target_has_atomic_load_store`, `target_os`, `target_pointer_width`, `target_thread_local`, `target_vendor`, `test`, `unix`, `windows`
88
= note: `#[warn(unexpected_cfgs)]` on by default
99

1010
warning: 1 warning emitted

tests/ui/check-cfg/exhaustive-names.exhaustive_names.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ warning: unexpected `cfg` condition name: `unknown_key`
44
LL | #[cfg(unknown_key = "value")]
55
| ^^^^^^^^^^^^^^^^^^^^^
66
|
7-
= help: expected names are: `debug_assertions`, `doc`, `doctest`, `feature`, `miri`, `overflow_checks`, `panic`, `proc_macro`, `relocation_model`, `sanitize`, `target_abi`, `target_arch`, `target_endian`, `target_env`, `target_family`, `target_feature`, `target_has_atomic`, `target_has_atomic_equal_alignment`, `target_has_atomic_load_store`, `target_os`, `target_pointer_width`, `target_thread_local`, `target_vendor`, `test`, `unix`, `windows`
7+
= help: expected names are: `debug_assertions`, `doc`, `doctest`, `miri`, `overflow_checks`, `panic`, `proc_macro`, `relocation_model`, `sanitize`, `target_abi`, `target_arch`, `target_endian`, `target_env`, `target_family`, `target_feature`, `target_has_atomic`, `target_has_atomic_equal_alignment`, `target_has_atomic_load_store`, `target_os`, `target_pointer_width`, `target_thread_local`, `target_vendor`, `test`, `unix`, `windows`
88
= note: `#[warn(unexpected_cfgs)]` on by default
99

1010
warning: 1 warning emitted

tests/ui/check-cfg/stmt-no-ice.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ warning: unexpected `cfg` condition name: `crossbeam_loom`
44
LL | #[cfg(crossbeam_loom)]
55
| ^^^^^^^^^^^^^^
66
|
7-
= help: expected names are: `debug_assertions`, `doc`, `doctest`, `feature`, `miri`, `overflow_checks`, `panic`, `proc_macro`, `relocation_model`, `sanitize`, `target_abi`, `target_arch`, `target_endian`, `target_env`, `target_family`, `target_feature`, `target_has_atomic`, `target_has_atomic_equal_alignment`, `target_has_atomic_load_store`, `target_os`, `target_pointer_width`, `target_thread_local`, `target_vendor`, `test`, `unix`, `windows`
7+
= help: expected names are: `debug_assertions`, `doc`, `doctest`, `miri`, `overflow_checks`, `panic`, `proc_macro`, `relocation_model`, `sanitize`, `target_abi`, `target_arch`, `target_endian`, `target_env`, `target_family`, `target_feature`, `target_has_atomic`, `target_has_atomic_equal_alignment`, `target_has_atomic_load_store`, `target_os`, `target_pointer_width`, `target_thread_local`, `target_vendor`, `test`, `unix`, `windows`
88
= note: `#[warn(unexpected_cfgs)]` on by default
99

1010
warning: 1 warning emitted

tests/ui/check-cfg/well-known-names.rs

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ fn target_os() {}
1515
fn feature_misspell() {}
1616

1717
#[cfg(feature = "foo")]
18+
//~^ WARNING unexpected `cfg` condition name
1819
fn feature() {}
1920

2021
#[cfg(uniw)]

tests/ui/check-cfg/well-known-names.stderr

+11-5
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,21 @@ warning: unexpected `cfg` condition name: `features`
1414
--> $DIR/well-known-names.rs:13:7
1515
|
1616
LL | #[cfg(features = "foo")]
17-
| --------^^^^^^^^
18-
| |
19-
| help: there is a config with a similar name: `feature`
17+
| ^^^^^^^^^^^^^^^^
18+
|
19+
= help: expected names are: `debug_assertions`, `doc`, `doctest`, `miri`, `overflow_checks`, `panic`, `proc_macro`, `relocation_model`, `sanitize`, `target_abi`, `target_arch`, `target_endian`, `target_env`, `target_family`, `target_feature`, `target_has_atomic`, `target_has_atomic_equal_alignment`, `target_has_atomic_load_store`, `target_os`, `target_pointer_width`, `target_thread_local`, `target_vendor`, `test`, `unix`, `windows`
20+
21+
warning: unexpected `cfg` condition name: `feature`
22+
--> $DIR/well-known-names.rs:17:7
23+
|
24+
LL | #[cfg(feature = "foo")]
25+
| ^^^^^^^^^^^^^^^
2026

2127
warning: unexpected `cfg` condition name: `uniw`
22-
--> $DIR/well-known-names.rs:20:7
28+
--> $DIR/well-known-names.rs:21:7
2329
|
2430
LL | #[cfg(uniw)]
2531
| ^^^^ help: there is a config with a similar name: `unix`
2632

27-
warning: 3 warnings emitted
33+
warning: 4 warnings emitted
2834

0 commit comments

Comments
 (0)