Skip to content

Commit a5669e8

Browse files
committed
Stabilize -Zcheck-cfg as always enabled-by-default
1 parent b861629 commit a5669e8

File tree

8 files changed

+83
-251
lines changed

8 files changed

+83
-251
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ 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+
///
60+
/// Can be removed once the minimum supported rustc version of Cargo is
61+
/// at minimum 1.80.0.
62+
pub support_check_cfg: bool,
5863
}
5964

6065
/// Kind of each file generated by a Unit, part of `FileType`.
@@ -199,6 +204,13 @@ impl TargetInfo {
199204
process.arg("--crate-type").arg(crate_type.as_str());
200205
}
201206

207+
let support_check_cfg = rustc
208+
.cached_output(
209+
process.clone().arg("--check-cfg").arg("cfg()"),
210+
extra_fingerprint,
211+
)
212+
.is_ok();
213+
202214
process.arg("--print=sysroot");
203215
process.arg("--print=split-debuginfo");
204216
process.arg("--print=crate-name"); // `___` as a delimiter.
@@ -310,6 +322,7 @@ impl TargetInfo {
310322
)?,
311323
cfg,
312324
support_split_debuginfo,
325+
support_check_cfg,
313326
});
314327
}
315328
}

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: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,11 @@ 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;
411+
let extra_check_cfg = build_runner
412+
.bcx
413+
.target_data
414+
.info(unit.kind)
415+
.support_check_cfg;
412416
let targets: Vec<Target> = unit.pkg.targets().to_vec();
413417
let msrv = unit.pkg.rust_version().cloned();
414418
// Need a separate copy for the fresh closure.
@@ -665,9 +669,7 @@ impl BuildOutput {
665669
///
666670
/// * `pkg_descr` --- for error messages
667671
/// * `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
672+
/// * `extra_check_cfg` --- for `--check-cfg` (if supported)
671673
pub fn parse(
672674
input: &[u8],
673675
// Takes String instead of InternedString so passing `unit.pkg.name()` will give a compile error.
@@ -910,8 +912,8 @@ impl BuildOutput {
910912
if extra_check_cfg {
911913
check_cfgs.push(value.to_string());
912914
} else {
913-
// silently ignoring the instruction to try to
914-
// minimise MSRV annoyance when stabilizing -Zcheck-cfg
915+
// silently ignoring the instruction because the rustc version
916+
// we are using does not support --check-cfg stably
915917
}
916918
}
917919
"rustc-env" => {
@@ -1254,7 +1256,11 @@ fn prev_build_output(
12541256
&unit.pkg.to_string(),
12551257
&prev_script_out_dir,
12561258
&script_out_dir,
1257-
build_runner.bcx.gctx.cli_unstable().check_cfg,
1259+
build_runner
1260+
.bcx
1261+
.target_data
1262+
.info(unit.kind)
1263+
.support_check_cfg,
12581264
build_runner.bcx.gctx.nightly_features_allowed,
12591265
unit.pkg.targets(),
12601266
&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
@@ -753,7 +753,6 @@ unstable_cli_options!(
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"),
755755
cargo_lints: bool = ("Enable the `[lints.cargo]` table"),
756-
check_cfg: bool = ("Enable compile-time checking of `cfg` names/values/features"),
757756
codegen_backend: bool = ("Enable the `codegen-backend` option in profiles in .cargo/config.toml file"),
758757
config_include: bool = ("Enable the `include` key in config files"),
759758
direct_minimal_versions: bool = ("Resolve minimal dependency versions instead of maximum (direct dependencies only)"),
@@ -855,6 +854,9 @@ const STABILIZED_REGISTRY_AUTH: &str =
855854

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

857+
const STABILIZED_CHECK_CFG: &str =
858+
"Compile-time checking of conditional (a.k.a. `-Zcheck-cfg`) is now always enabled.";
859+
858860
fn deserialize_build_std<'de, D>(deserializer: D) -> Result<Option<Vec<String>>, D::Error>
859861
where
860862
D: serde::Deserializer<'de>,
@@ -1109,6 +1111,7 @@ impl CliUnstable {
11091111
"credential-process" => stabilized_warn(k, "1.74", STABILIZED_CREDENTIAL_PROCESS),
11101112
"lints" => stabilized_warn(k, "1.74", STABILIZED_LINTS),
11111113
"registry-auth" => stabilized_warn(k, "1.74", STABILIZED_REGISTRY_AUTH),
1114+
"check-cfg" => stabilized_warn(k, "1.80", STABILIZED_CHECK_CFG),
11121115

11131116
// Unstable features
11141117
// Sorted alphabetically:
@@ -1122,9 +1125,6 @@ impl CliUnstable {
11221125
}
11231126
"build-std-features" => self.build_std_features = Some(parse_features(v)),
11241127
"cargo-lints" => self.cargo_lints = parse_empty(k, v)?,
1125-
"check-cfg" => {
1126-
self.check_cfg = parse_empty(k, v)?;
1127-
}
11281128
"codegen-backend" => self.codegen_backend = parse_empty(k, v)?,
11291129
"config-include" => self.config_include = parse_empty(k, v)?,
11301130
"direct-minimal-versions" => self.direct_minimal_versions = parse_empty(k, v)?,

src/cargo/util/context/target.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ fn load_config_table(gctx: &GlobalContext, prefix: &str) -> CargoResult<TargetCo
120120
// Links do not support environment variables.
121121
let target_key = ConfigKey::from_str(prefix);
122122
let links_overrides = match gctx.get_table(&target_key)? {
123-
Some(links) => parse_links_overrides(&target_key, links.val, gctx)?,
123+
Some(links) => parse_links_overrides(&target_key, links.val)?,
124124
None => BTreeMap::new(),
125125
};
126126
Ok(TargetConfig {
@@ -135,7 +135,6 @@ 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,
139138
) -> CargoResult<BTreeMap<String, BuildOutput>> {
140139
let mut links_overrides = BTreeMap::new();
141140

@@ -204,13 +203,8 @@ fn parse_links_overrides(
204203
output.cfgs.extend(list.iter().map(|v| v.0.clone()));
205204
}
206205
"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-
}
206+
let list = value.list(key)?;
207+
output.check_cfgs.extend(list.iter().map(|v| v.0.clone()));
214208
}
215209
"rustc-env" => {
216210
for (name, val) in value.table(key)?.0 {

0 commit comments

Comments
 (0)