Skip to content

Commit 4f19191

Browse files
committed
Auto merge of #14302 - weihanglo:check-cfg, r=epage
fix: remove rustc probe for `--check-cfg` support ### What does this PR try to resolve? This is a follow-up to #13571 as now everything for `-Z check-cfg` is stabilized. ### How should we test and review this PR? Existing test suite passes. ### Additional information
2 parents 0d0bab6 + acf64f7 commit 4f19191

File tree

4 files changed

+64
-107
lines changed

4 files changed

+64
-107
lines changed

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

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,6 @@ pub struct TargetInfo {
5454
pub rustflags: Rc<[String]>,
5555
/// Extra flags to pass to `rustdoc`, see [`extra_args`].
5656
pub rustdocflags: Rc<[String]>,
57-
/// Whether or not rustc (stably) supports the `--check-cfg` flag.
58-
///
59-
/// Can be removed once the minimum supported rustc version of Cargo is
60-
/// at minimum 1.80.0.
61-
pub support_check_cfg: bool,
6257
}
6358

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

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-
214202
process.arg("--print=sysroot");
215203
process.arg("--print=split-debuginfo");
216204
process.arg("--print=crate-name"); // `___` as a delimiter.
@@ -323,7 +311,6 @@ impl TargetInfo {
323311
.into(),
324312
cfg,
325313
support_split_debuginfo,
326-
support_check_cfg,
327314
});
328315
}
329316
}

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

Lines changed: 1 addition & 1 deletion
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 {

src/cargo/core/compiler/custom_build.rs

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -405,11 +405,6 @@ fn build_work(build_runner: &mut BuildRunner<'_, '_>, unit: &Unit) -> CargoResul
405405
paths::create_dir_all(&script_out_dir)?;
406406

407407
let nightly_features_allowed = build_runner.bcx.gctx.nightly_features_allowed;
408-
let extra_check_cfg = build_runner
409-
.bcx
410-
.target_data
411-
.info(unit.kind)
412-
.support_check_cfg;
413408
let targets: Vec<Target> = unit.pkg.targets().to_vec();
414409
let msrv = unit.pkg.rust_version().cloned();
415410
// Need a separate copy for the fresh closure.
@@ -557,7 +552,6 @@ fn build_work(build_runner: &mut BuildRunner<'_, '_>, unit: &Unit) -> CargoResul
557552
&pkg_descr,
558553
&script_out_dir,
559554
&script_out_dir,
560-
extra_check_cfg,
561555
nightly_features_allowed,
562556
&targets,
563557
&msrv,
@@ -586,7 +580,6 @@ fn build_work(build_runner: &mut BuildRunner<'_, '_>, unit: &Unit) -> CargoResul
586580
&pkg_descr,
587581
&prev_script_out_dir,
588582
&script_out_dir,
589-
extra_check_cfg,
590583
nightly_features_allowed,
591584
&targets_fresh,
592585
&msrv_fresh,
@@ -643,7 +636,6 @@ impl BuildOutput {
643636
pkg_descr: &str,
644637
script_out_dir_when_generated: &Path,
645638
script_out_dir: &Path,
646-
extra_check_cfg: bool,
647639
nightly_features_allowed: bool,
648640
targets: &[Target],
649641
msrv: &Option<RustVersion>,
@@ -655,7 +647,6 @@ impl BuildOutput {
655647
pkg_descr,
656648
script_out_dir_when_generated,
657649
script_out_dir,
658-
extra_check_cfg,
659650
nightly_features_allowed,
660651
targets,
661652
msrv,
@@ -666,15 +657,13 @@ impl BuildOutput {
666657
///
667658
/// * `pkg_descr` --- for error messages
668659
/// * `library_name` --- for determining if `RUSTC_BOOTSTRAP` should be allowed
669-
/// * `extra_check_cfg` --- for `--check-cfg` (if supported)
670660
pub fn parse(
671661
input: &[u8],
672662
// Takes String instead of InternedString so passing `unit.pkg.name()` will give a compile error.
673663
library_name: Option<String>,
674664
pkg_descr: &str,
675665
script_out_dir_when_generated: &Path,
676666
script_out_dir: &Path,
677-
extra_check_cfg: bool,
678667
nightly_features_allowed: bool,
679668
targets: &[Target],
680669
msrv: &Option<RustVersion>,
@@ -921,14 +910,7 @@ impl BuildOutput {
921910
linker_args.push((LinkArgTarget::All, value));
922911
}
923912
"rustc-cfg" => cfgs.push(value.to_string()),
924-
"rustc-check-cfg" => {
925-
if extra_check_cfg {
926-
check_cfgs.push(value.to_string());
927-
} else {
928-
// silently ignoring the instruction because the rustc version
929-
// we are using does not support --check-cfg stably
930-
}
931-
}
913+
"rustc-check-cfg" => check_cfgs.push(value.to_string()),
932914
"rustc-env" => {
933915
let (key, val) = BuildOutput::parse_rustc_env(&value, &whence)?;
934916
// Build scripts aren't allowed to set RUSTC_BOOTSTRAP.
@@ -1265,11 +1247,6 @@ fn prev_build_output(
12651247
&unit.pkg.to_string(),
12661248
&prev_script_out_dir,
12671249
&script_out_dir,
1268-
build_runner
1269-
.bcx
1270-
.target_data
1271-
.info(unit.kind)
1272-
.support_check_cfg,
12731250
build_runner.bcx.gctx.nightly_features_allowed,
12741251
unit.pkg.targets(),
12751252
&unit.pkg.rust_version().cloned(),

src/cargo/core/compiler/mod.rs

Lines changed: 62 additions & 69 deletions
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,83 +1310,76 @@ fn trim_paths_args(
13101310
}
13111311

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

1337-
let gross_cap_estimation = unit.pkg.summary().features().len() * 7 + 25;
1338-
let mut arg_feature = OsString::with_capacity(gross_cap_estimation);
1331+
let gross_cap_estimation = unit.pkg.summary().features().len() * 7 + 25;
1332+
let mut arg_feature = OsString::with_capacity(gross_cap_estimation);
13391333

1340-
arg_feature.push("cfg(feature, values(");
1341-
for (i, feature) in unit.pkg.summary().features().keys().enumerate() {
1342-
if i != 0 {
1343-
arg_feature.push(", ");
1344-
}
1345-
arg_feature.push("\"");
1346-
arg_feature.push(feature);
1347-
arg_feature.push("\"");
1334+
arg_feature.push("cfg(feature, values(");
1335+
for (i, feature) in unit.pkg.summary().features().keys().enumerate() {
1336+
if i != 0 {
1337+
arg_feature.push(", ");
13481338
}
1349-
arg_feature.push("))");
1350-
1351-
// We also include the `docsrs` cfg from the docs.rs service. We include it here
1352-
// (in Cargo) instead of rustc, since there is a much closer relationship between
1353-
// Cargo and docs.rs than rustc and docs.rs. In particular, all users of docs.rs use
1354-
// Cargo, but not all users of rustc (like Rust-for-Linux) use docs.rs.
1355-
1356-
let mut args = vec![
1357-
OsString::from("--check-cfg"),
1358-
OsString::from("cfg(docsrs)"),
1359-
OsString::from("--check-cfg"),
1360-
arg_feature,
1361-
];
1362-
1363-
// Also include the custom arguments specified in `[lints.rust.unexpected_cfgs.check_cfg]`
1364-
if let Ok(Some(lints)) = unit.pkg.manifest().resolved_toml().resolved_lints() {
1365-
if let Some(rust_lints) = lints.get("rust") {
1366-
if let Some(unexpected_cfgs) = rust_lints.get("unexpected_cfgs") {
1367-
if let Some(config) = unexpected_cfgs.config() {
1368-
if let Some(check_cfg) = config.get("check-cfg") {
1369-
if let Ok(check_cfgs) =
1370-
toml::Value::try_into::<Vec<String>>(check_cfg.clone())
1371-
{
1372-
for check_cfg in check_cfgs {
1373-
args.push(OsString::from("--check-cfg"));
1374-
args.push(OsString::from(check_cfg));
1375-
}
1376-
// error about `check-cfg` not being a list-of-string
1377-
} else {
1378-
bail!("`lints.rust.unexpected_cfgs.check-cfg` must be a list of string");
1339+
arg_feature.push("\"");
1340+
arg_feature.push(feature);
1341+
arg_feature.push("\"");
1342+
}
1343+
arg_feature.push("))");
1344+
1345+
// We also include the `docsrs` cfg from the docs.rs service. We include it here
1346+
// (in Cargo) instead of rustc, since there is a much closer relationship between
1347+
// Cargo and docs.rs than rustc and docs.rs. In particular, all users of docs.rs use
1348+
// Cargo, but not all users of rustc (like Rust-for-Linux) use docs.rs.
1349+
1350+
let mut args = vec![
1351+
OsString::from("--check-cfg"),
1352+
OsString::from("cfg(docsrs)"),
1353+
OsString::from("--check-cfg"),
1354+
arg_feature,
1355+
];
1356+
1357+
// Also include the custom arguments specified in `[lints.rust.unexpected_cfgs.check_cfg]`
1358+
if let Ok(Some(lints)) = unit.pkg.manifest().resolved_toml().resolved_lints() {
1359+
if let Some(rust_lints) = lints.get("rust") {
1360+
if let Some(unexpected_cfgs) = rust_lints.get("unexpected_cfgs") {
1361+
if let Some(config) = unexpected_cfgs.config() {
1362+
if let Some(check_cfg) = config.get("check-cfg") {
1363+
if let Ok(check_cfgs) =
1364+
toml::Value::try_into::<Vec<String>>(check_cfg.clone())
1365+
{
1366+
for check_cfg in check_cfgs {
1367+
args.push(OsString::from("--check-cfg"));
1368+
args.push(OsString::from(check_cfg));
13791369
}
1370+
// error about `check-cfg` not being a list-of-string
1371+
} else {
1372+
bail!(
1373+
"`lints.rust.unexpected_cfgs.check-cfg` must be a list of string"
1374+
);
13801375
}
13811376
}
13821377
}
13831378
}
13841379
}
1385-
1386-
Ok(args)
1387-
} else {
1388-
Ok(Vec::new())
13891380
}
1381+
1382+
Ok(args)
13901383
}
13911384

13921385
/// Adds LTO related codegen flags.

0 commit comments

Comments
 (0)