Skip to content

Commit fbaea97

Browse files
committed
Stabilize -Zcheck-cfg as always enabled-by-default
1 parent d79fdf3 commit fbaea97

File tree

7 files changed

+88
-297
lines changed

7 files changed

+88
-297
lines changed

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

+4-7
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ impl<'a, 'gctx> BuildRunner<'a, 'gctx> {
246246
let mut args = compiler::extern_args(&self, unit, &mut unstable_opts)?;
247247
args.extend(compiler::lto_args(&self, unit));
248248
args.extend(compiler::features_args(unit));
249-
args.extend(compiler::check_cfg_args(&self, unit));
249+
args.extend(compiler::check_cfg_args(unit));
250250

251251
let script_meta = self.find_build_script_metadata(unit);
252252
if let Some(meta) = script_meta {
@@ -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

+1-16
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

+1-8
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

+42-48
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,7 @@ fn prepare_rustdoc(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> CargoResu
732732
let doc_dir = build_runner.files().out_dir(unit);
733733
rustdoc.arg("-o").arg(&doc_dir);
734734
rustdoc.args(&features_args(unit));
735-
rustdoc.args(&check_cfg_args(build_runner, unit));
735+
rustdoc.args(&check_cfg_args(unit));
736736

737737
add_error_format_and_color(build_runner, &mut rustdoc);
738738
add_allow_features(build_runner, &mut rustdoc);
@@ -1125,7 +1125,7 @@ fn build_base_args(
11251125
}
11261126

11271127
cmd.args(&features_args(unit));
1128-
cmd.args(&check_cfg_args(build_runner, unit));
1128+
cmd.args(&check_cfg_args(unit));
11291129

11301130
let meta = build_runner.files().metadata(unit);
11311131
cmd.arg("-C").arg(&format!("metadata={}", meta));
@@ -1310,57 +1310,51 @@ fn trim_paths_args(
13101310
}
13111311

13121312
/// Generates the `--check-cfg` arguments for the `unit`.
1313-
/// See unstable feature [`check-cfg`].
13141313
///
13151314
/// [`check-cfg`]: https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#check-cfg
1316-
fn check_cfg_args(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> Vec<OsString> {
1317-
if build_runner.bcx.gctx.cli_unstable().check_cfg {
1318-
// The routine below generates the --check-cfg arguments. Our goals here are to
1319-
// enable the checking of conditionals and pass the list of declared features.
1320-
//
1321-
// In the simplified case, it would resemble something like this:
1322-
//
1323-
// --check-cfg=cfg() --check-cfg=cfg(feature, values(...))
1324-
//
1325-
// but having `cfg()` is redundant with the second argument (as well-known names
1326-
// and values are implicitly enabled when one or more `--check-cfg` argument is
1327-
// passed) so we don't emit it and just pass:
1328-
//
1329-
// --check-cfg=cfg(feature, values(...))
1330-
//
1331-
// This way, even if there are no declared features, the config `feature` will
1332-
// still be expected, meaning users would get "unexpected value" instead of name.
1333-
// This wasn't always the case, see rust-lang#119930 for some details.
1315+
fn check_cfg_args(unit: &Unit) -> Vec<OsString> {
1316+
// The routine below generates the --check-cfg arguments. Our goals here are to
1317+
// enable the checking of conditionals and pass the list of declared features.
1318+
//
1319+
// In the simplified case, it would resemble something like this:
1320+
//
1321+
// --check-cfg=cfg() --check-cfg=cfg(feature, values(...))
1322+
//
1323+
// but having `cfg()` is redundant with the second argument (as well-known names
1324+
// and values are implicitly enabled when one or more `--check-cfg` argument is
1325+
// passed) so we don't emit it and just pass:
1326+
//
1327+
// --check-cfg=cfg(feature, values(...))
1328+
//
1329+
// This way, even if there are no declared features, the config `feature` will
1330+
// still be expected, meaning users would get "unexpected value" instead of name.
1331+
// This wasn't always the case, see rust-lang#119930 for some details.
13341332

1335-
let gross_cap_estimation = unit.pkg.summary().features().len() * 7 + 25;
1336-
let mut arg_feature = OsString::with_capacity(gross_cap_estimation);
1333+
let gross_cap_estimation = unit.pkg.summary().features().len() * 7 + 25;
1334+
let mut arg_feature = OsString::with_capacity(gross_cap_estimation);
13371335

1338-
arg_feature.push("cfg(feature, values(");
1339-
for (i, feature) in unit.pkg.summary().features().keys().enumerate() {
1340-
if i != 0 {
1341-
arg_feature.push(", ");
1342-
}
1343-
arg_feature.push("\"");
1344-
arg_feature.push(feature);
1345-
arg_feature.push("\"");
1336+
arg_feature.push("cfg(feature, values(");
1337+
for (i, feature) in unit.pkg.summary().features().keys().enumerate() {
1338+
if i != 0 {
1339+
arg_feature.push(", ");
13461340
}
1347-
arg_feature.push("))");
1348-
1349-
// We also include the `docsrs` cfg from the docs.rs service. We include it here
1350-
// (in Cargo) instead of rustc, since there is a much closer relationship between
1351-
// Cargo and docs.rs than rustc and docs.rs. In particular, all users of docs.rs use
1352-
// Cargo, but not all users of rustc (like Rust-for-Linux) use docs.rs.
1353-
1354-
vec![
1355-
OsString::from("-Zunstable-options"),
1356-
OsString::from("--check-cfg"),
1357-
OsString::from("cfg(docsrs)"),
1358-
OsString::from("--check-cfg"),
1359-
arg_feature,
1360-
]
1361-
} else {
1362-
Vec::new()
1363-
}
1341+
arg_feature.push("\"");
1342+
arg_feature.push(feature);
1343+
arg_feature.push("\"");
1344+
}
1345+
arg_feature.push("))");
1346+
1347+
// We also include the `docsrs` cfg from the docs.rs service. We include it here
1348+
// (in Cargo) instead of rustc, since there is a much closer relationship between
1349+
// Cargo and docs.rs than rustc and docs.rs. In particular, all users of docs.rs use
1350+
// Cargo, but not all users of rustc (like Rust-for-Linux) use docs.rs.
1351+
1352+
vec![
1353+
OsString::from("--check-cfg"),
1354+
OsString::from("cfg(docsrs)"),
1355+
OsString::from("--check-cfg"),
1356+
arg_feature,
1357+
]
13641358
}
13651359

13661360
/// Adds LTO related codegen flags.

src/cargo/core/features.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -752,7 +752,6 @@ unstable_cli_options!(
752752
#[serde(deserialize_with = "deserialize_build_std")]
753753
build_std: Option<Vec<String>> = ("Enable Cargo to compile the standard library itself as part of a crate graph compilation"),
754754
build_std_features: Option<Vec<String>> = ("Configure features enabled for the standard library itself when building the standard library"),
755-
check_cfg: bool = ("Enable compile-time checking of `cfg` names/values/features"),
756755
codegen_backend: bool = ("Enable the `codegen-backend` option in profiles in .cargo/config.toml file"),
757756
config_include: bool = ("Enable the `include` key in config files"),
758757
direct_minimal_versions: bool = ("Resolve minimal dependency versions instead of maximum (direct dependencies only)"),
@@ -854,6 +853,9 @@ const STABILIZED_CREDENTIAL_PROCESS: &str =
854853
const STABILIZED_REGISTRY_AUTH: &str =
855854
"Authenticated registries are available if a credential provider is configured.";
856855

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

11111114
// Unstable features
11121115
// Sorted alphabetically:
@@ -1119,9 +1122,6 @@ impl CliUnstable {
11191122
self.build_std = Some(crate::core::compiler::standard_lib::parse_unstable_flag(v))
11201123
}
11211124
"build-std-features" => self.build_std_features = Some(parse_features(v)),
1122-
"check-cfg" => {
1123-
self.check_cfg = parse_empty(k, v)?;
1124-
}
11251125
"codegen-backend" => self.codegen_backend = parse_empty(k, v)?,
11261126
"config-include" => self.config_include = parse_empty(k, v)?,
11271127
"direct-minimal-versions" => self.direct_minimal_versions = parse_empty(k, v)?,

src/cargo/util/context/target.rs

+3-8
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)