Skip to content

Commit 936cac1

Browse files
committed
Gate unexpected target cfgs check behind -Zcheck-target-cfgs
1 parent 9d0e1c6 commit 936cac1

File tree

6 files changed

+87
-44
lines changed

6 files changed

+87
-44
lines changed

src/cargo/core/compiler/build_context/target_info.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -946,7 +946,7 @@ pub struct RustcTargetData<'gctx> {
946946
target_info: HashMap<CompileTarget, TargetInfo>,
947947

948948
/// `--print=cfg`
949-
check_cfg: CheckCfg,
949+
check_cfg: Option<CheckCfg>,
950950
}
951951

952952
impl<'gctx> RustcTargetData<'gctx> {
@@ -993,7 +993,7 @@ impl<'gctx> RustcTargetData<'gctx> {
993993
}
994994
};
995995

996-
let check_cfg = {
996+
let check_cfg = if gctx.cli_unstable().check_target_cfgs {
997997
let mut process = rustc.workspace_process();
998998

999999
apply_env_config(gctx, &mut process)?;
@@ -1016,10 +1016,12 @@ impl<'gctx> RustcTargetData<'gctx> {
10161016

10171017
let lines = output.lines();
10181018

1019-
lines.fold(CheckCfg::default(), |mut check_cfg, line| {
1019+
Some(lines.fold(CheckCfg::default(), |mut check_cfg, line| {
10201020
check_cfg.process_line(line);
10211021
check_cfg
1022-
})
1022+
}))
1023+
} else {
1024+
None
10231025
};
10241026

10251027
let mut res = RustcTargetData {
@@ -1108,7 +1110,7 @@ impl<'gctx> RustcTargetData<'gctx> {
11081110
}
11091111

11101112
/// Gets the list of check-cfg
1111-
pub fn check_cfg(&self) -> &CheckCfg {
1113+
pub fn check_cfg(&self) -> &Option<CheckCfg> {
11121114
&self.check_cfg
11131115
}
11141116

src/cargo/core/features.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,7 @@ unstable_cli_options!(
760760
build_std: Option<Vec<String>> = ("Enable Cargo to compile the standard library itself as part of a crate graph compilation"),
761761
build_std_features: Option<Vec<String>> = ("Configure features enabled for the standard library itself when building the standard library"),
762762
cargo_lints: bool = ("Enable the `[lints.cargo]` table"),
763+
check_target_cfgs: bool = ("Enable unexpected cfgs checking in `[target.'cfg(...)']` tables"),
763764
codegen_backend: bool = ("Enable the `codegen-backend` option in profiles in .cargo/config.toml file"),
764765
config_include: bool = ("Enable the `include` key in config files"),
765766
direct_minimal_versions: bool = ("Resolve minimal dependency versions instead of maximum (direct dependencies only)"),
@@ -1255,6 +1256,7 @@ impl CliUnstable {
12551256
}
12561257
"build-std-features" => self.build_std_features = Some(parse_features(v)),
12571258
"cargo-lints" => self.cargo_lints = parse_empty(k, v)?,
1259+
"check-target-cfgs" => self.check_target_cfgs = parse_empty(k, v)?,
12581260
"codegen-backend" => self.codegen_backend = parse_empty(k, v)?,
12591261
"config-include" => self.config_include = parse_empty(k, v)?,
12601262
"direct-minimal-versions" => self.direct_minimal_versions = parse_empty(k, v)?,

src/cargo/ops/cargo_compile/mod.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -480,9 +480,7 @@ pub fn create_bcx<'a, 'gctx>(
480480
.extend(args);
481481
}
482482

483-
{
484-
let check_cfg = target_data.check_cfg();
485-
483+
if let Some(check_cfg) = target_data.check_cfg() {
486484
if check_cfg.exhaustive {
487485
let target_cfgs = gctx.target_cfgs()?;
488486
for (key, _fields) in target_cfgs {

tests/testsuite/cargo/z_help/stdout.term.svg

Lines changed: 32 additions & 30 deletions
Loading

tests/testsuite/cfg.rs

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -515,16 +515,14 @@ fn exclusive_dep_kinds() {
515515
.with_status(101)
516516
// can't find crate for `bar`
517517
.with_stderr_data(str![[r#"
518-
[WARNING] [ROOT]/foo/Cargo.toml: unexpected `cfg` condition name: `abc` in `[target.'cfg(abc)'.dependencies]`
519-
[WARNING] [ROOT]/foo/Cargo.toml: unexpected `cfg` condition name: `abc` in `[target.'cfg(not(abc))'.dependencies]`
520518
[COMPILING] foo v0.1.0 ([ROOT]/foo)
521519
error[E0463]: can't find crate for `bar`
522520
...
523521
"#]])
524522
.run();
525523
}
526524

527-
#[cargo_test]
525+
#[cargo_test(nightly, reason = "--print=check-cfg is unstable in rustc")]
528526
fn unexpected_cfgs_target() {
529527
let p = project()
530528
.file(
@@ -566,7 +564,8 @@ fn unexpected_cfgs_target() {
566564
.file("c/src/lib.rs", "")
567565
.build();
568566

569-
p.cargo("check")
567+
p.cargo("check -Zcheck-target-cfgs")
568+
.masquerade_as_nightly_cargo(&["requires -Zcheck-target-cfgs"])
570569
.with_stderr_data(str![[r#"
571570
[LOCKING] 2 packages to latest compatible versions
572571
[WARNING] unexpected `cfg` condition name: `foo` in `[target.'cfg(any(foo, all(bar)))']`
@@ -585,3 +584,45 @@ fn unexpected_cfgs_target() {
585584
"#]])
586585
.run();
587586
}
587+
588+
#[cargo_test]
589+
fn no_unexpected_cfgs_target() {
590+
let p = project()
591+
.file(
592+
"Cargo.toml",
593+
r#"
594+
[package]
595+
name = "a"
596+
version = "0.0.1"
597+
edition = "2015"
598+
authors = []
599+
600+
[target."cfg(any(windows, unix))".dependencies]
601+
b = { path = 'b' }
602+
603+
[target.'cfg(unix = "zoo")'.dependencies]
604+
b = { path = 'b' }
605+
"#,
606+
)
607+
.file(
608+
".cargo/config.toml",
609+
r#"
610+
[target."cfg(any(windows, unix))"]
611+
[target.'cfg(unix = "zoo")']
612+
"#,
613+
)
614+
.file("src/lib.rs", "extern crate b;")
615+
.file("b/Cargo.toml", &basic_manifest("b", "0.0.1"))
616+
.file("b/src/lib.rs", "")
617+
.build();
618+
619+
p.cargo("check")
620+
.with_stderr_data(str![[r#"
621+
[LOCKING] 1 package to latest compatible version
622+
[CHECKING] b v0.0.1 ([ROOT]/foo/b)
623+
[CHECKING] a v0.0.1 ([ROOT]/foo)
624+
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
625+
626+
"#]])
627+
.run();
628+
}

tests/testsuite/tool_paths.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -479,8 +479,6 @@ fn cfg_ignored_fields() {
479479
[WARNING] unused key `ar` in [target] config table `cfg(not(target_os = "none"))`
480480
[WARNING] unused key `foo` in [target] config table `cfg(not(target_os = "none"))`
481481
[WARNING] unused key `invalid` in [target] config table `cfg(not(target_os = "none"))`
482-
[WARNING] unexpected `cfg` condition name: `bar` in `[target.'cfg(not(bar))']`
483-
[WARNING] unexpected `cfg` condition name: `foo` in `[target.'cfg(not(foo))']`
484482
[CHECKING] foo v0.0.1 ([ROOT]/foo)
485483
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
486484

0 commit comments

Comments
 (0)