Skip to content

Commit d3f5a35

Browse files
authored
Unrolled build for rust-lang#125412
Rollup merge of rust-lang#125412 - Urgau:check-cfg-less-build-rs, r=wesleywiser Don't suggest adding the unexpected cfgs to the build-script it-self This PR adds a check to avoid suggesting to add the unexpected cfgs inside the build-script when building the build-script it-self, as it won't have any effect, since build-scripts applies to their descended target. Fixes rust-lang#125368
2 parents 78dd504 + 1c7859e commit d3f5a35

File tree

4 files changed

+107
-24
lines changed

4 files changed

+107
-24
lines changed

compiler/rustc_lint/src/context/diagnostics/check_cfg.rs

+18-12
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,22 @@ fn to_check_cfg_arg(name: Symbol, value: Option<Symbol>, quotes: EscapeQuotes) -
4242
}
4343
}
4444

45+
fn cargo_help_sub(
46+
sess: &Session,
47+
inst: &impl Fn(EscapeQuotes) -> String,
48+
) -> lints::UnexpectedCfgCargoHelp {
49+
// We don't want to suggest the `build.rs` way to expected cfgs if we are already in a
50+
// `build.rs`. We therefor do a best effort check (looking if the `--crate-name` is
51+
// `build_script_build`) to try to figure out if we are building a Cargo build script
52+
53+
let unescaped = &inst(EscapeQuotes::No);
54+
if matches!(&sess.opts.crate_name, Some(crate_name) if crate_name == "build_script_build") {
55+
lints::UnexpectedCfgCargoHelp::lint_cfg(unescaped)
56+
} else {
57+
lints::UnexpectedCfgCargoHelp::lint_cfg_and_build_rs(unescaped, &inst(EscapeQuotes::Yes))
58+
}
59+
}
60+
4561
pub(super) fn unexpected_cfg_name(
4662
sess: &Session,
4763
(name, name_span): (Symbol, Span),
@@ -162,14 +178,7 @@ pub(super) fn unexpected_cfg_name(
162178
let inst = |escape_quotes| to_check_cfg_arg(name, value.map(|(v, _s)| v), escape_quotes);
163179

164180
let invocation_help = if is_from_cargo {
165-
let sub = if !is_feature_cfg {
166-
Some(lints::UnexpectedCfgCargoHelp::new(
167-
&inst(EscapeQuotes::No),
168-
&inst(EscapeQuotes::Yes),
169-
))
170-
} else {
171-
None
172-
};
181+
let sub = if !is_feature_cfg { Some(cargo_help_sub(sess, &inst)) } else { None };
173182
lints::unexpected_cfg_name::InvocationHelp::Cargo { sub }
174183
} else {
175184
lints::unexpected_cfg_name::InvocationHelp::Rustc(lints::UnexpectedCfgRustcHelp::new(
@@ -267,10 +276,7 @@ pub(super) fn unexpected_cfg_value(
267276
Some(lints::unexpected_cfg_value::CargoHelp::DefineFeatures)
268277
}
269278
} else if !is_cfg_a_well_know_name {
270-
Some(lints::unexpected_cfg_value::CargoHelp::Other(lints::UnexpectedCfgCargoHelp::new(
271-
&inst(EscapeQuotes::No),
272-
&inst(EscapeQuotes::Yes),
273-
)))
279+
Some(lints::unexpected_cfg_value::CargoHelp::Other(cargo_help_sub(sess, &inst)))
274280
} else {
275281
None
276282
};

compiler/rustc_lint/src/lints.rs

+24-12
Original file line numberDiff line numberDiff line change
@@ -1962,21 +1962,33 @@ pub struct UnitBindingsDiag {
19621962
pub struct BuiltinNamedAsmLabel;
19631963

19641964
#[derive(Subdiagnostic)]
1965-
#[help(lint_unexpected_cfg_add_cargo_feature)]
1966-
#[help(lint_unexpected_cfg_add_cargo_toml_lint_cfg)]
1967-
#[help(lint_unexpected_cfg_add_build_rs_println)]
1968-
pub struct UnexpectedCfgCargoHelp {
1969-
pub build_rs_println: String,
1970-
pub cargo_toml_lint_cfg: String,
1965+
pub enum UnexpectedCfgCargoHelp {
1966+
#[help(lint_unexpected_cfg_add_cargo_feature)]
1967+
#[help(lint_unexpected_cfg_add_cargo_toml_lint_cfg)]
1968+
LintCfg { cargo_toml_lint_cfg: String },
1969+
#[help(lint_unexpected_cfg_add_cargo_feature)]
1970+
#[help(lint_unexpected_cfg_add_cargo_toml_lint_cfg)]
1971+
#[help(lint_unexpected_cfg_add_build_rs_println)]
1972+
LintCfgAndBuildRs { cargo_toml_lint_cfg: String, build_rs_println: String },
19711973
}
19721974

19731975
impl UnexpectedCfgCargoHelp {
1974-
pub fn new(unescaped: &str, escaped: &str) -> Self {
1975-
Self {
1976-
cargo_toml_lint_cfg: format!(
1977-
"\n [lints.rust]\n unexpected_cfgs = {{ level = \"warn\", check-cfg = ['{unescaped}'] }}",
1978-
),
1979-
build_rs_println: format!("println!(\"cargo::rustc-check-cfg={escaped}\");",),
1976+
fn cargo_toml_lint_cfg(unescaped: &str) -> String {
1977+
format!(
1978+
"\n [lints.rust]\n unexpected_cfgs = {{ level = \"warn\", check-cfg = ['{unescaped}'] }}"
1979+
)
1980+
}
1981+
1982+
pub fn lint_cfg(unescaped: &str) -> Self {
1983+
UnexpectedCfgCargoHelp::LintCfg {
1984+
cargo_toml_lint_cfg: Self::cargo_toml_lint_cfg(unescaped),
1985+
}
1986+
}
1987+
1988+
pub fn lint_cfg_and_build_rs(unescaped: &str, escaped: &str) -> Self {
1989+
UnexpectedCfgCargoHelp::LintCfgAndBuildRs {
1990+
cargo_toml_lint_cfg: Self::cargo_toml_lint_cfg(unescaped),
1991+
build_rs_println: format!("println!(\"cargo::rustc-check-cfg={escaped}\");"),
19801992
}
19811993
}
19821994
}
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// This test checks that when we are building a build script provided
2+
// by Cargo we only suggest expecting the unexpected cfgs in the Cargo.toml.
3+
//
4+
//@ check-pass
5+
//@ no-auto-check-cfg
6+
//@ rustc-env:CARGO_CRATE_NAME=build_script_build
7+
//@ compile-flags:--crate-name=build_script_build
8+
//@ compile-flags:--check-cfg=cfg(has_bar)
9+
10+
#[cfg(has_foo)]
11+
//~^ WARNING unexpected `cfg` condition name
12+
fn foo() {}
13+
14+
#[cfg(has_foo = "yes")]
15+
//~^ WARNING unexpected `cfg` condition name
16+
fn foo() {}
17+
18+
#[cfg(has_bar = "yes")]
19+
//~^ WARNING unexpected `cfg` condition value
20+
fn has_bar() {}
21+
22+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
warning: unexpected `cfg` condition name: `has_foo`
2+
--> $DIR/cargo-build-script.rs:10:7
3+
|
4+
LL | #[cfg(has_foo)]
5+
| ^^^^^^^
6+
|
7+
= help: expected names are: `clippy`, `debug_assertions`, `doc`, `doctest`, `has_bar`, `miri`, `overflow_checks`, `panic`, `proc_macro`, `relocation_model`, `rustfmt`, `sanitize`, `sanitizer_cfi_generalize_pointers`, `sanitizer_cfi_normalize_integers`, `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`, `ub_checks`, `unix`, and `windows`
8+
= help: consider using a Cargo feature instead
9+
= help: or consider adding in `Cargo.toml` the `check-cfg` lint config for the lint:
10+
[lints.rust]
11+
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(has_foo)'] }
12+
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg/cargo-specifics.html> for more information about checking conditional configuration
13+
= note: `#[warn(unexpected_cfgs)]` on by default
14+
15+
warning: unexpected `cfg` condition name: `has_foo`
16+
--> $DIR/cargo-build-script.rs:14:7
17+
|
18+
LL | #[cfg(has_foo = "yes")]
19+
| ^^^^^^^^^^^^^^^
20+
|
21+
= help: consider using a Cargo feature instead
22+
= help: or consider adding in `Cargo.toml` the `check-cfg` lint config for the lint:
23+
[lints.rust]
24+
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(has_foo, values("yes"))'] }
25+
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg/cargo-specifics.html> for more information about checking conditional configuration
26+
27+
warning: unexpected `cfg` condition value: `yes`
28+
--> $DIR/cargo-build-script.rs:18:7
29+
|
30+
LL | #[cfg(has_bar = "yes")]
31+
| ^^^^^^^--------
32+
| |
33+
| help: remove the value
34+
|
35+
= note: no expected value for `has_bar`
36+
= help: consider using a Cargo feature instead
37+
= help: or consider adding in `Cargo.toml` the `check-cfg` lint config for the lint:
38+
[lints.rust]
39+
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(has_bar, values("yes"))'] }
40+
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg/cargo-specifics.html> for more information about checking conditional configuration
41+
42+
warning: 3 warnings emitted
43+

0 commit comments

Comments
 (0)