Skip to content

Commit

Permalink
Auto merge of rust-lang#118071 - Urgau:check-cfg-cargo-feature, r=pet…
Browse files Browse the repository at this point in the history
…rochenkov

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`
  • Loading branch information
bors committed Nov 22, 2023
2 parents cc4bb0d + 54c122e commit 855c683
Show file tree
Hide file tree
Showing 15 changed files with 80 additions and 19 deletions.
2 changes: 2 additions & 0 deletions compiler/rustc_lint/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,8 @@ pub trait LintContext {
} else {
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() {
let mut possibilities = possibilities.iter()
.map(Symbol::as_str)
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_session/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1443,9 +1443,8 @@ impl CheckCfg {
let relocation_model_values = RelocModel::all();

// Unknown possible values:
// - `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);
}

Expand Down
2 changes: 1 addition & 1 deletion src/bootstrap/src/bin/rustc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ fn main() {
{
cmd.arg("-Ztls-model=initial-exec");
}
} else {
} else if std::env::var("MIRI").is_err() {
// Find any host flags that were passed by bootstrap.
// The flags are stored in a RUSTC_HOST_FLAGS variable, separated by spaces.
if let Ok(flags) = std::env::var("RUSTC_HOST_FLAGS") {
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/check-cfg/allow-same-level.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ warning: unexpected `cfg` condition name: `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

warning: 1 warning emitted
Expand Down
14 changes: 14 additions & 0 deletions tests/ui/check-cfg/cargo-feature.rs
Original file line number Diff line number Diff line change
@@ -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() {}
11 changes: 11 additions & 0 deletions tests/ui/check-cfg/cargo-feature.stderr
Original file line number Diff line number Diff line change
@@ -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

2 changes: 1 addition & 1 deletion tests/ui/check-cfg/compact-names.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ warning: unexpected `cfg` condition name: `target_architecture`
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

warning: 1 warning emitted
Expand Down
16 changes: 14 additions & 2 deletions tests/ui/check-cfg/exhaustive-names-values.empty_cfg.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ warning: unexpected `cfg` condition name: `unknown_key`
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

warning: unexpected `cfg` condition value: `value`
Expand All @@ -17,5 +17,17 @@ LL | #[cfg(test = "value")]
|
= 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

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ warning: unexpected `cfg` condition name: `unknown_key`
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

warning: unexpected `cfg` condition value: `value`
Expand All @@ -17,5 +17,17 @@ LL | #[cfg(test = "value")]
|
= 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

8 changes: 6 additions & 2 deletions tests/ui/check-cfg/exhaustive-names-values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ pub fn f() {}
pub fn f() {}

#[cfg(feature = "unk")]
//[feature]~^ WARNING unexpected `cfg` condition value
//[full]~^^ WARNING unexpected `cfg` condition value
//[empty_names_values]~^ WARNING unexpected `cfg` condition name
//[empty_cfg]~^^ WARNING unexpected `cfg` condition name
//[feature]~^^^ WARNING unexpected `cfg` condition value
//[full]~^^^^ WARNING unexpected `cfg` condition value
pub fn feat() {}

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

#[cfg(windows)]
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/check-cfg/exhaustive-names.empty_names.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ warning: unexpected `cfg` condition name: `unknown_key`
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

warning: 1 warning emitted
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ warning: unexpected `cfg` condition name: `unknown_key`
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

warning: 1 warning emitted
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/check-cfg/stmt-no-ice.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ warning: unexpected `cfg` condition name: `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

warning: 1 warning emitted
Expand Down
1 change: 1 addition & 0 deletions tests/ui/check-cfg/well-known-names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ fn target_os() {}
fn feature_misspell() {}

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

#[cfg(uniw)]
Expand Down
16 changes: 11 additions & 5 deletions tests/ui/check-cfg/well-known-names.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,21 @@ warning: unexpected `cfg` condition name: `features`
--> $DIR/well-known-names.rs:13:7
|
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`
--> $DIR/well-known-names.rs:20:7
--> $DIR/well-known-names.rs:21:7
|
LL | #[cfg(uniw)]
| ^^^^ help: there is a config with a similar name: `unix`

warning: 3 warnings emitted
warning: 4 warnings emitted

0 comments on commit 855c683

Please sign in to comment.