Skip to content

Commit 3e5bc6b

Browse files
committed
Stabilize -Zcheck-cfg as always enabled-by-default
1 parent 6f06fe9 commit 3e5bc6b

File tree

8 files changed

+68
-259
lines changed

8 files changed

+68
-259
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ pub struct TargetInfo {
5555
pub rustflags: Vec<String>,
5656
/// Extra flags to pass to `rustdoc`, see [`extra_args`].
5757
pub rustdocflags: Vec<String>,
58+
/// Whether or not rustc (stably) supports the `--check-cfg` flag.
59+
pub support_check_cfg: bool,
5860
}
5961

6062
/// Kind of each file generated by a Unit, part of `FileType`.
@@ -199,6 +201,13 @@ impl TargetInfo {
199201
process.arg("--crate-type").arg(crate_type.as_str());
200202
}
201203

204+
let support_check_cfg = rustc
205+
.cached_output(
206+
process.clone().arg("--check-cfg").arg("cfg()"),
207+
extra_fingerprint,
208+
)
209+
.is_ok();
210+
202211
process.arg("--print=sysroot");
203212
process.arg("--print=split-debuginfo");
204213
process.arg("--print=crate-name"); // `___` as a delimiter.
@@ -310,6 +319,7 @@ impl TargetInfo {
310319
)?,
311320
cfg,
312321
support_split_debuginfo,
322+
support_check_cfg,
313323
});
314324
}
315325
}

src/cargo/core/compiler/build_runner/mod.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -256,12 +256,9 @@ impl<'a, 'gctx> BuildRunner<'a, 'gctx> {
256256
args.push(cfg.into());
257257
}
258258

259-
if !output.check_cfgs.is_empty() {
260-
args.push("-Zunstable-options".into());
261-
for check_cfg in &output.check_cfgs {
262-
args.push("--check-cfg".into());
263-
args.push(check_cfg.into());
264-
}
259+
for check_cfg in &output.check_cfgs {
260+
args.push("--check-cfg".into());
261+
args.push(check_cfg.into());
265262
}
266263

267264
for (lt, arg) in &output.linker_args {

src/cargo/core/compiler/custom_build.rs

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,6 @@ fn build_work(build_runner: &mut BuildRunner<'_, '_>, unit: &Unit) -> CargoResul
408408
paths::create_dir_all(&script_out_dir)?;
409409

410410
let nightly_features_allowed = build_runner.bcx.gctx.nightly_features_allowed;
411-
let extra_check_cfg = build_runner.bcx.gctx.cli_unstable().check_cfg;
412411
let targets: Vec<Target> = unit.pkg.targets().to_vec();
413412
let msrv = unit.pkg.rust_version().cloned();
414413
// Need a separate copy for the fresh closure.
@@ -556,7 +555,6 @@ fn build_work(build_runner: &mut BuildRunner<'_, '_>, unit: &Unit) -> CargoResul
556555
&pkg_descr,
557556
&script_out_dir,
558557
&script_out_dir,
559-
extra_check_cfg,
560558
nightly_features_allowed,
561559
&targets,
562560
&msrv,
@@ -585,7 +583,6 @@ fn build_work(build_runner: &mut BuildRunner<'_, '_>, unit: &Unit) -> CargoResul
585583
&pkg_descr,
586584
&prev_script_out_dir,
587585
&script_out_dir,
588-
extra_check_cfg,
589586
nightly_features_allowed,
590587
&targets_fresh,
591588
&msrv_fresh,
@@ -642,7 +639,6 @@ impl BuildOutput {
642639
pkg_descr: &str,
643640
script_out_dir_when_generated: &Path,
644641
script_out_dir: &Path,
645-
extra_check_cfg: bool,
646642
nightly_features_allowed: bool,
647643
targets: &[Target],
648644
msrv: &Option<RustVersion>,
@@ -654,7 +650,6 @@ impl BuildOutput {
654650
pkg_descr,
655651
script_out_dir_when_generated,
656652
script_out_dir,
657-
extra_check_cfg,
658653
nightly_features_allowed,
659654
targets,
660655
msrv,
@@ -665,17 +660,13 @@ impl BuildOutput {
665660
///
666661
/// * `pkg_descr` --- for error messages
667662
/// * `library_name` --- for determining if `RUSTC_BOOTSTRAP` should be allowed
668-
/// * `extra_check_cfg` --- for unstable feature [`-Zcheck-cfg`]
669-
///
670-
/// [`-Zcheck-cfg`]: https://doc.rust-lang.org/cargo/reference/unstable.html#check-cfg
671663
pub fn parse(
672664
input: &[u8],
673665
// Takes String instead of InternedString so passing `unit.pkg.name()` will give a compile error.
674666
library_name: Option<String>,
675667
pkg_descr: &str,
676668
script_out_dir_when_generated: &Path,
677669
script_out_dir: &Path,
678-
extra_check_cfg: bool,
679670
nightly_features_allowed: bool,
680671
targets: &[Target],
681672
msrv: &Option<RustVersion>,
@@ -908,12 +899,7 @@ impl BuildOutput {
908899
}
909900
"rustc-cfg" => cfgs.push(value.to_string()),
910901
"rustc-check-cfg" => {
911-
if extra_check_cfg {
912-
check_cfgs.push(value.to_string());
913-
} else {
914-
// silently ignoring the instruction to try to
915-
// minimise MSRV annoyance when stabilizing -Zcheck-cfg
916-
}
902+
check_cfgs.push(value.to_string());
917903
}
918904
"rustc-env" => {
919905
let (key, val) = BuildOutput::parse_rustc_env(&value, &whence)?;
@@ -1255,7 +1241,6 @@ fn prev_build_output(
12551241
&unit.pkg.to_string(),
12561242
&prev_script_out_dir,
12571243
&script_out_dir,
1258-
build_runner.bcx.gctx.cli_unstable().check_cfg,
12591244
build_runner.bcx.gctx.nightly_features_allowed,
12601245
unit.pkg.targets(),
12611246
&unit.pkg.rust_version().cloned(),

src/cargo/core/compiler/fingerprint/mod.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1454,14 +1454,7 @@ fn calculate_normal(
14541454
// actually affect the output artifact so there's no need to hash it.
14551455
path: util::hash_u64(path_args(build_runner.bcx.ws, unit).0),
14561456
features: format!("{:?}", unit.features),
1457-
// Note we curently only populate `declared_features` when `-Zcheck-cfg`
1458-
// is passed since it's the only user-facing toggle that will make this
1459-
// fingerprint relevant.
1460-
declared_features: if build_runner.bcx.gctx.cli_unstable().check_cfg {
1461-
format!("{declared_features:?}")
1462-
} else {
1463-
"".to_string()
1464-
},
1457+
declared_features: format!("{declared_features:?}"),
14651458
deps,
14661459
local: Mutex::new(local),
14671460
memoized_hash: Mutex::new(None),

src/cargo/core/compiler/mod.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1310,11 +1310,13 @@ fn trim_paths_args(
13101310
}
13111311

13121312
/// Generates the `--check-cfg` arguments for the `unit`.
1313-
/// See unstable feature [`check-cfg`].
1314-
///
1315-
/// [`check-cfg`]: https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#check-cfg
13161313
fn check_cfg_args(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> Vec<OsString> {
1317-
if build_runner.bcx.gctx.cli_unstable().check_cfg {
1314+
if build_runner
1315+
.bcx
1316+
.target_data
1317+
.info(unit.kind)
1318+
.support_check_cfg
1319+
{
13181320
// The routine below generates the --check-cfg arguments. Our goals here are to
13191321
// enable the checking of conditionals and pass the list of declared features.
13201322
//
@@ -1352,7 +1354,6 @@ fn check_cfg_args(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> Vec<OsStri
13521354
// Cargo, but not all users of rustc (like Rust-for-Linux) use docs.rs.
13531355

13541356
vec![
1355-
OsString::from("-Zunstable-options"),
13561357
OsString::from("--check-cfg"),
13571358
OsString::from("cfg(docsrs)"),
13581359
OsString::from("--check-cfg"),
@@ -1476,11 +1477,8 @@ fn add_custom_flags(
14761477
for cfg in output.cfgs.iter() {
14771478
cmd.arg("--cfg").arg(cfg);
14781479
}
1479-
if !output.check_cfgs.is_empty() {
1480-
cmd.arg("-Zunstable-options");
1481-
for check_cfg in &output.check_cfgs {
1482-
cmd.arg("--check-cfg").arg(check_cfg);
1483-
}
1480+
for check_cfg in &output.check_cfgs {
1481+
cmd.arg("--check-cfg").arg(check_cfg);
14841482
}
14851483
for (name, value) in output.env.iter() {
14861484
cmd.env(name, value);

src/cargo/core/features.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -751,7 +751,6 @@ unstable_cli_options!(
751751
build_std: Option<Vec<String>> = ("Enable Cargo to compile the standard library itself as part of a crate graph compilation"),
752752
build_std_features: Option<Vec<String>> = ("Configure features enabled for the standard library itself when building the standard library"),
753753
cargo_lints: bool = ("Enable the `[lints.cargo]` table"),
754-
check_cfg: bool = ("Enable compile-time checking of `cfg` names/values/features"),
755754
codegen_backend: bool = ("Enable the `codegen-backend` option in profiles in .cargo/config.toml file"),
756755
config_include: bool = ("Enable the `include` key in config files"),
757756
direct_minimal_versions: bool = ("Resolve minimal dependency versions instead of maximum (direct dependencies only)"),
@@ -853,6 +852,9 @@ const STABILIZED_REGISTRY_AUTH: &str =
853852

854853
const STABILIZED_LINTS: &str = "The `[lints]` table is now always available.";
855854

855+
const STABILIZED_CHECK_CFG: &str =
856+
"Compile-time checking of conditional (a.k.a. `-Zcheck-cfg`) is now always enabled.";
857+
856858
fn deserialize_build_std<'de, D>(deserializer: D) -> Result<Option<Vec<String>>, D::Error>
857859
where
858860
D: serde::Deserializer<'de>,
@@ -1107,6 +1109,7 @@ impl CliUnstable {
11071109
"credential-process" => stabilized_warn(k, "1.74", STABILIZED_CREDENTIAL_PROCESS),
11081110
"lints" => stabilized_warn(k, "1.74", STABILIZED_LINTS),
11091111
"registry-auth" => stabilized_warn(k, "1.74", STABILIZED_REGISTRY_AUTH),
1112+
"check-cfg" => stabilized_warn(k, "1.80", STABILIZED_CHECK_CFG),
11101113

11111114
// Unstable features
11121115
// Sorted alphabetically:
@@ -1120,9 +1123,6 @@ impl CliUnstable {
11201123
}
11211124
"build-std-features" => self.build_std_features = Some(parse_features(v)),
11221125
"cargo-lints" => self.cargo_lints = parse_empty(k, v)?,
1123-
"check-cfg" => {
1124-
self.check_cfg = parse_empty(k, v)?;
1125-
}
11261126
"codegen-backend" => self.codegen_backend = parse_empty(k, v)?,
11271127
"config-include" => self.config_include = parse_empty(k, v)?,
11281128
"direct-minimal-versions" => self.direct_minimal_versions = parse_empty(k, v)?,

src/cargo/util/context/target.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ fn load_config_table(gctx: &GlobalContext, prefix: &str) -> CargoResult<TargetCo
135135
fn parse_links_overrides(
136136
target_key: &ConfigKey,
137137
links: HashMap<String, CV>,
138-
gctx: &GlobalContext,
138+
_gctx: &GlobalContext,
139139
) -> CargoResult<BTreeMap<String, BuildOutput>> {
140140
let mut links_overrides = BTreeMap::new();
141141

@@ -204,13 +204,8 @@ fn parse_links_overrides(
204204
output.cfgs.extend(list.iter().map(|v| v.0.clone()));
205205
}
206206
"rustc-check-cfg" => {
207-
if gctx.cli_unstable().check_cfg {
208-
let list = value.list(key)?;
209-
output.check_cfgs.extend(list.iter().map(|v| v.0.clone()));
210-
} else {
211-
// silently ignoring the instruction to try to
212-
// minimise MSRV annoyance when stabilizing -Zcheck-cfg
213-
}
207+
let list = value.list(key)?;
208+
output.check_cfgs.extend(list.iter().map(|v| v.0.clone()));
214209
}
215210
"rustc-env" => {
216211
for (name, val) in value.table(key)?.0 {

0 commit comments

Comments
 (0)