From 9f15acec474dafe50617aa1623c987e3b6354246 Mon Sep 17 00:00:00 2001 From: Urgau Date: Sun, 19 Nov 2023 20:43:50 +0100 Subject: [PATCH 1/3] Remove `feature` from the list of well known check-cfg name --- compiler/rustc_session/src/config.rs | 3 +-- tests/ui/check-cfg/allow-same-level.stderr | 2 +- tests/ui/check-cfg/compact-names.stderr | 2 +- .../exhaustive-names-values.empty_cfg.stderr | 16 ++++++++++++++-- ...ustive-names-values.empty_names_values.stderr | 16 ++++++++++++++-- tests/ui/check-cfg/exhaustive-names-values.rs | 8 ++++++-- .../exhaustive-names.empty_names.stderr | 2 +- .../exhaustive-names.exhaustive_names.stderr | 2 +- tests/ui/check-cfg/stmt-no-ice.stderr | 2 +- tests/ui/check-cfg/well-known-names.rs | 1 + tests/ui/check-cfg/well-known-names.stderr | 16 +++++++++++----- 11 files changed, 52 insertions(+), 18 deletions(-) diff --git a/compiler/rustc_session/src/config.rs b/compiler/rustc_session/src/config.rs index d4f9122e7e384..e288a43c0e0aa 100644 --- a/compiler/rustc_session/src/config.rs +++ b/compiler/rustc_session/src/config.rs @@ -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); } diff --git a/tests/ui/check-cfg/allow-same-level.stderr b/tests/ui/check-cfg/allow-same-level.stderr index b0c459fabf8eb..19d9443d477a4 100644 --- a/tests/ui/check-cfg/allow-same-level.stderr +++ b/tests/ui/check-cfg/allow-same-level.stderr @@ -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 diff --git a/tests/ui/check-cfg/compact-names.stderr b/tests/ui/check-cfg/compact-names.stderr index b0228774b751b..ffde972a25ef8 100644 --- a/tests/ui/check-cfg/compact-names.stderr +++ b/tests/ui/check-cfg/compact-names.stderr @@ -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 diff --git a/tests/ui/check-cfg/exhaustive-names-values.empty_cfg.stderr b/tests/ui/check-cfg/exhaustive-names-values.empty_cfg.stderr index 12a055d02a7b4..971abb1a21a41 100644 --- a/tests/ui/check-cfg/exhaustive-names-values.empty_cfg.stderr +++ b/tests/ui/check-cfg/exhaustive-names-values.empty_cfg.stderr @@ -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` @@ -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 diff --git a/tests/ui/check-cfg/exhaustive-names-values.empty_names_values.stderr b/tests/ui/check-cfg/exhaustive-names-values.empty_names_values.stderr index 12a055d02a7b4..971abb1a21a41 100644 --- a/tests/ui/check-cfg/exhaustive-names-values.empty_names_values.stderr +++ b/tests/ui/check-cfg/exhaustive-names-values.empty_names_values.stderr @@ -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` @@ -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 diff --git a/tests/ui/check-cfg/exhaustive-names-values.rs b/tests/ui/check-cfg/exhaustive-names-values.rs index ef1e458b2498f..ceb4831e22d39 100644 --- a/tests/ui/check-cfg/exhaustive-names-values.rs +++ b/tests/ui/check-cfg/exhaustive-names-values.rs @@ -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)] diff --git a/tests/ui/check-cfg/exhaustive-names.empty_names.stderr b/tests/ui/check-cfg/exhaustive-names.empty_names.stderr index 6bc7845c8328a..7d01c73cc09b5 100644 --- a/tests/ui/check-cfg/exhaustive-names.empty_names.stderr +++ b/tests/ui/check-cfg/exhaustive-names.empty_names.stderr @@ -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 diff --git a/tests/ui/check-cfg/exhaustive-names.exhaustive_names.stderr b/tests/ui/check-cfg/exhaustive-names.exhaustive_names.stderr index 6bc7845c8328a..7d01c73cc09b5 100644 --- a/tests/ui/check-cfg/exhaustive-names.exhaustive_names.stderr +++ b/tests/ui/check-cfg/exhaustive-names.exhaustive_names.stderr @@ -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 diff --git a/tests/ui/check-cfg/stmt-no-ice.stderr b/tests/ui/check-cfg/stmt-no-ice.stderr index 900ea4e4da06c..3fb3ae27ec487 100644 --- a/tests/ui/check-cfg/stmt-no-ice.stderr +++ b/tests/ui/check-cfg/stmt-no-ice.stderr @@ -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 diff --git a/tests/ui/check-cfg/well-known-names.rs b/tests/ui/check-cfg/well-known-names.rs index 1dcb419b4a73b..32c14703d25b5 100644 --- a/tests/ui/check-cfg/well-known-names.rs +++ b/tests/ui/check-cfg/well-known-names.rs @@ -15,6 +15,7 @@ fn target_os() {} fn feature_misspell() {} #[cfg(feature = "foo")] +//~^ WARNING unexpected `cfg` condition name fn feature() {} #[cfg(uniw)] diff --git a/tests/ui/check-cfg/well-known-names.stderr b/tests/ui/check-cfg/well-known-names.stderr index 3001289b7e06f..a986e61bcdc30 100644 --- a/tests/ui/check-cfg/well-known-names.stderr +++ b/tests/ui/check-cfg/well-known-names.stderr @@ -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 From 1c37997fa70129efea84c02cf4d863a44660ab2e Mon Sep 17 00:00:00 2001 From: Urgau Date: Sun, 19 Nov 2023 21:41:55 +0100 Subject: [PATCH 2/3] Improve diagnostic for unexpected feature config name from Cargo --- compiler/rustc_lint/src/context.rs | 2 ++ tests/ui/check-cfg/cargo-feature.rs | 14 ++++++++++++++ tests/ui/check-cfg/cargo-feature.stderr | 11 +++++++++++ 3 files changed, 27 insertions(+) create mode 100644 tests/ui/check-cfg/cargo-feature.rs create mode 100644 tests/ui/check-cfg/cargo-feature.stderr diff --git a/compiler/rustc_lint/src/context.rs b/compiler/rustc_lint/src/context.rs index d500c3b66d662..ad2047c1903e7 100644 --- a/compiler/rustc_lint/src/context.rs +++ b/compiler/rustc_lint/src/context.rs @@ -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) diff --git a/tests/ui/check-cfg/cargo-feature.rs b/tests/ui/check-cfg/cargo-feature.rs new file mode 100644 index 0000000000000..ea48c6ea20154 --- /dev/null +++ b/tests/ui/check-cfg/cargo-feature.rs @@ -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() {} diff --git a/tests/ui/check-cfg/cargo-feature.stderr b/tests/ui/check-cfg/cargo-feature.stderr new file mode 100644 index 0000000000000..619410a28f370 --- /dev/null +++ b/tests/ui/check-cfg/cargo-feature.stderr @@ -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 + From 54c122ec0d45799e795f8b2aca38227ce9b05fa5 Mon Sep 17 00:00:00 2001 From: Urgau Date: Sun, 19 Nov 2023 23:17:27 +0100 Subject: [PATCH 3/3] [Miri] Do not respect RUSTC_HOST_FLAGS since RUSTFLAGS isn't When building the standard library with Miri, it appears that cargo-miri does not respect the RUSTFLAGS and even if they did it would be wrong since they are created for ToolRustc not Std. To avoid errors ignore RUSTC_HOST_FLAGS for Miri. --- src/bootstrap/src/bin/rustc.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bootstrap/src/bin/rustc.rs b/src/bootstrap/src/bin/rustc.rs index 070a2da6afb8d..af66cb3ffd7c2 100644 --- a/src/bootstrap/src/bin/rustc.rs +++ b/src/bootstrap/src/bin/rustc.rs @@ -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") {