1
Fork 0

Auto merge of #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`
This commit is contained in:
bors 2023-11-22 07:31:13 +00:00
commit 855c6836b7
15 changed files with 80 additions and 19 deletions

View file

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

View file

@ -1443,9 +1443,8 @@ impl CheckCfg {
let relocation_model_values = RelocModel::all(); let relocation_model_values = RelocModel::all();
// Unknown possible values: // Unknown possible values:
// - `feature`
// - `target_feature` // - `target_feature`
for name in [sym::feature, sym::target_feature] { for name in [sym::target_feature] {
self.expecteds.entry(name).or_insert(ExpectedValues::Any); self.expecteds.entry(name).or_insert(ExpectedValues::Any);
} }

View file

@ -114,7 +114,7 @@ fn main() {
{ {
cmd.arg("-Ztls-model=initial-exec"); cmd.arg("-Ztls-model=initial-exec");
} }
} else { } else if std::env::var("MIRI").is_err() {
// Find any host flags that were passed by bootstrap. // Find any host flags that were passed by bootstrap.
// The flags are stored in a RUSTC_HOST_FLAGS variable, separated by spaces. // The flags are stored in a RUSTC_HOST_FLAGS variable, separated by spaces.
if let Ok(flags) = std::env::var("RUSTC_HOST_FLAGS") { if let Ok(flags) = std::env::var("RUSTC_HOST_FLAGS") {

View file

@ -4,7 +4,7 @@ warning: unexpected `cfg` condition name: `FALSE`
LL | #[cfg(FALSE)] LL | #[cfg(FALSE)]
| ^^^^^ | ^^^^^
| |
= 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` = 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`
= note: `#[warn(unexpected_cfgs)]` on by default = note: `#[warn(unexpected_cfgs)]` on by default
warning: 1 warning emitted warning: 1 warning emitted

View file

@ -0,0 +1,14 @@
// This test checks that when no features are passed by Cargo we
// suggest adding some in the Cargo.toml instead of vomitting a
// list of all the expected names
//
// check-pass
// rustc-env:CARGO=/usr/bin/cargo
// compile-flags: --check-cfg=cfg() -Z unstable-options
// error-pattern:Cargo.toml
#[cfg(feature = "serde")]
//~^ WARNING unexpected `cfg` condition name
fn ser() {}
fn main() {}

View file

@ -0,0 +1,11 @@
warning: unexpected `cfg` condition name: `feature`
--> $DIR/cargo-feature.rs:10:7
|
LL | #[cfg(feature = "serde")]
| ^^^^^^^^^^^^^^^^^
|
= help: consider defining some features in `Cargo.toml`
= note: `#[warn(unexpected_cfgs)]` on by default
warning: 1 warning emitted

View file

@ -4,7 +4,7 @@ warning: unexpected `cfg` condition name: `target_architecture`
LL | #[cfg(target(os = "linux", architecture = "arm"))] LL | #[cfg(target(os = "linux", architecture = "arm"))]
| ^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^
| |
= 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` = 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`
= note: `#[warn(unexpected_cfgs)]` on by default = note: `#[warn(unexpected_cfgs)]` on by default
warning: 1 warning emitted warning: 1 warning emitted

View file

@ -4,7 +4,7 @@ warning: unexpected `cfg` condition name: `unknown_key`
LL | #[cfg(unknown_key = "value")] LL | #[cfg(unknown_key = "value")]
| ^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^
| |
= 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` = 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`
= note: `#[warn(unexpected_cfgs)]` on by default = note: `#[warn(unexpected_cfgs)]` on by default
warning: unexpected `cfg` condition value: `value` warning: unexpected `cfg` condition value: `value`
@ -17,5 +17,17 @@ LL | #[cfg(test = "value")]
| |
= note: no expected value for `test` = note: no expected value for `test`
warning: 2 warnings emitted warning: unexpected `cfg` condition name: `feature`
--> $DIR/exhaustive-names-values.rs:19:7
|
LL | #[cfg(feature = "unk")]
| ^^^^^^^^^^^^^^^
warning: unexpected `cfg` condition name: `feature`
--> $DIR/exhaustive-names-values.rs:26:7
|
LL | #[cfg(feature = "std")]
| ^^^^^^^^^^^^^^^
warning: 4 warnings emitted

View file

@ -4,7 +4,7 @@ warning: unexpected `cfg` condition name: `unknown_key`
LL | #[cfg(unknown_key = "value")] LL | #[cfg(unknown_key = "value")]
| ^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^
| |
= 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` = 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`
= note: `#[warn(unexpected_cfgs)]` on by default = note: `#[warn(unexpected_cfgs)]` on by default
warning: unexpected `cfg` condition value: `value` warning: unexpected `cfg` condition value: `value`
@ -17,5 +17,17 @@ LL | #[cfg(test = "value")]
| |
= note: no expected value for `test` = note: no expected value for `test`
warning: 2 warnings emitted warning: unexpected `cfg` condition name: `feature`
--> $DIR/exhaustive-names-values.rs:19:7
|
LL | #[cfg(feature = "unk")]
| ^^^^^^^^^^^^^^^
warning: unexpected `cfg` condition name: `feature`
--> $DIR/exhaustive-names-values.rs:26:7
|
LL | #[cfg(feature = "std")]
| ^^^^^^^^^^^^^^^
warning: 4 warnings emitted

View file

@ -17,11 +17,15 @@ pub fn f() {}
pub fn f() {} pub fn f() {}
#[cfg(feature = "unk")] #[cfg(feature = "unk")]
//[feature]~^ WARNING unexpected `cfg` condition value //[empty_names_values]~^ WARNING unexpected `cfg` condition name
//[full]~^^ WARNING unexpected `cfg` condition value //[empty_cfg]~^^ WARNING unexpected `cfg` condition name
//[feature]~^^^ WARNING unexpected `cfg` condition value
//[full]~^^^^ WARNING unexpected `cfg` condition value
pub fn feat() {} pub fn feat() {}
#[cfg(feature = "std")] #[cfg(feature = "std")]
//[empty_names_values]~^ WARNING unexpected `cfg` condition name
//[empty_cfg]~^^ WARNING unexpected `cfg` condition name
pub fn feat() {} pub fn feat() {}
#[cfg(windows)] #[cfg(windows)]

View file

@ -4,7 +4,7 @@ warning: unexpected `cfg` condition name: `unknown_key`
LL | #[cfg(unknown_key = "value")] LL | #[cfg(unknown_key = "value")]
| ^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^
| |
= 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` = 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`
= note: `#[warn(unexpected_cfgs)]` on by default = note: `#[warn(unexpected_cfgs)]` on by default
warning: 1 warning emitted warning: 1 warning emitted

View file

@ -4,7 +4,7 @@ warning: unexpected `cfg` condition name: `unknown_key`
LL | #[cfg(unknown_key = "value")] LL | #[cfg(unknown_key = "value")]
| ^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^
| |
= 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` = 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`
= note: `#[warn(unexpected_cfgs)]` on by default = note: `#[warn(unexpected_cfgs)]` on by default
warning: 1 warning emitted warning: 1 warning emitted

View file

@ -4,7 +4,7 @@ warning: unexpected `cfg` condition name: `crossbeam_loom`
LL | #[cfg(crossbeam_loom)] LL | #[cfg(crossbeam_loom)]
| ^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^
| |
= 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` = 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`
= note: `#[warn(unexpected_cfgs)]` on by default = note: `#[warn(unexpected_cfgs)]` on by default
warning: 1 warning emitted warning: 1 warning emitted

View file

@ -15,6 +15,7 @@ fn target_os() {}
fn feature_misspell() {} fn feature_misspell() {}
#[cfg(feature = "foo")] #[cfg(feature = "foo")]
//~^ WARNING unexpected `cfg` condition name
fn feature() {} fn feature() {}
#[cfg(uniw)] #[cfg(uniw)]

View file

@ -14,15 +14,21 @@ warning: unexpected `cfg` condition name: `features`
--> $DIR/well-known-names.rs:13:7 --> $DIR/well-known-names.rs:13:7
| |
LL | #[cfg(features = "foo")] LL | #[cfg(features = "foo")]
| --------^^^^^^^^ | ^^^^^^^^^^^^^^^^
| | |
| help: there is a config with a similar name: `feature` = 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`
warning: unexpected `cfg` condition name: `feature`
--> $DIR/well-known-names.rs:17:7
|
LL | #[cfg(feature = "foo")]
| ^^^^^^^^^^^^^^^
warning: unexpected `cfg` condition name: `uniw` warning: unexpected `cfg` condition name: `uniw`
--> $DIR/well-known-names.rs:20:7 --> $DIR/well-known-names.rs:21:7
| |
LL | #[cfg(uniw)] LL | #[cfg(uniw)]
| ^^^^ help: there is a config with a similar name: `unix` | ^^^^ help: there is a config with a similar name: `unix`
warning: 3 warnings emitted warning: 4 warnings emitted