Skip to content

fix: remove rustc probe for --check-cfg support #14302

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 0 additions & 13 deletions src/cargo/core/compiler/build_context/target_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,6 @@ pub struct TargetInfo {
pub rustflags: Rc<[String]>,
/// Extra flags to pass to `rustdoc`, see [`extra_args`].
pub rustdocflags: Rc<[String]>,
/// Whether or not rustc (stably) supports the `--check-cfg` flag.
///
/// Can be removed once the minimum supported rustc version of Cargo is
/// at minimum 1.80.0.
pub support_check_cfg: bool,
}

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

let support_check_cfg = rustc
.cached_output(
process.clone().arg("--check-cfg").arg("cfg()"),
extra_fingerprint,
)
.is_ok();

process.arg("--print=sysroot");
process.arg("--print=split-debuginfo");
process.arg("--print=crate-name"); // `___` as a delimiter.
Expand Down Expand Up @@ -323,7 +311,6 @@ impl TargetInfo {
.into(),
cfg,
support_split_debuginfo,
support_check_cfg,
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/core/compiler/build_runner/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ impl<'a, 'gctx> BuildRunner<'a, 'gctx> {
let mut args = compiler::extern_args(&self, unit, &mut unstable_opts)?;
args.extend(compiler::lto_args(&self, unit));
args.extend(compiler::features_args(unit));
args.extend(compiler::check_cfg_args(&self, unit)?);
args.extend(compiler::check_cfg_args(unit)?);

let script_meta = self.find_build_script_metadata(unit);
if let Some(meta) = script_meta {
Expand Down
25 changes: 1 addition & 24 deletions src/cargo/core/compiler/custom_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,11 +405,6 @@ fn build_work(build_runner: &mut BuildRunner<'_, '_>, unit: &Unit) -> CargoResul
paths::create_dir_all(&script_out_dir)?;

let nightly_features_allowed = build_runner.bcx.gctx.nightly_features_allowed;
let extra_check_cfg = build_runner
.bcx
.target_data
.info(unit.kind)
.support_check_cfg;
let targets: Vec<Target> = unit.pkg.targets().to_vec();
let msrv = unit.pkg.rust_version().cloned();
// Need a separate copy for the fresh closure.
Expand Down Expand Up @@ -557,7 +552,6 @@ fn build_work(build_runner: &mut BuildRunner<'_, '_>, unit: &Unit) -> CargoResul
&pkg_descr,
&script_out_dir,
&script_out_dir,
extra_check_cfg,
nightly_features_allowed,
&targets,
&msrv,
Expand Down Expand Up @@ -586,7 +580,6 @@ fn build_work(build_runner: &mut BuildRunner<'_, '_>, unit: &Unit) -> CargoResul
&pkg_descr,
&prev_script_out_dir,
&script_out_dir,
extra_check_cfg,
nightly_features_allowed,
&targets_fresh,
&msrv_fresh,
Expand Down Expand Up @@ -643,7 +636,6 @@ impl BuildOutput {
pkg_descr: &str,
script_out_dir_when_generated: &Path,
script_out_dir: &Path,
extra_check_cfg: bool,
nightly_features_allowed: bool,
targets: &[Target],
msrv: &Option<RustVersion>,
Expand All @@ -655,7 +647,6 @@ impl BuildOutput {
pkg_descr,
script_out_dir_when_generated,
script_out_dir,
extra_check_cfg,
nightly_features_allowed,
targets,
msrv,
Expand All @@ -666,15 +657,13 @@ impl BuildOutput {
///
/// * `pkg_descr` --- for error messages
/// * `library_name` --- for determining if `RUSTC_BOOTSTRAP` should be allowed
/// * `extra_check_cfg` --- for `--check-cfg` (if supported)
pub fn parse(
input: &[u8],
// Takes String instead of InternedString so passing `unit.pkg.name()` will give a compile error.
library_name: Option<String>,
pkg_descr: &str,
script_out_dir_when_generated: &Path,
script_out_dir: &Path,
extra_check_cfg: bool,
nightly_features_allowed: bool,
targets: &[Target],
msrv: &Option<RustVersion>,
Expand Down Expand Up @@ -921,14 +910,7 @@ impl BuildOutput {
linker_args.push((LinkArgTarget::All, value));
}
"rustc-cfg" => cfgs.push(value.to_string()),
"rustc-check-cfg" => {
if extra_check_cfg {
check_cfgs.push(value.to_string());
} else {
// silently ignoring the instruction because the rustc version
// we are using does not support --check-cfg stably
}
}
"rustc-check-cfg" => check_cfgs.push(value.to_string()),
"rustc-env" => {
let (key, val) = BuildOutput::parse_rustc_env(&value, &whence)?;
// Build scripts aren't allowed to set RUSTC_BOOTSTRAP.
Expand Down Expand Up @@ -1265,11 +1247,6 @@ fn prev_build_output(
&unit.pkg.to_string(),
&prev_script_out_dir,
&script_out_dir,
build_runner
.bcx
.target_data
.info(unit.kind)
.support_check_cfg,
build_runner.bcx.gctx.nightly_features_allowed,
unit.pkg.targets(),
&unit.pkg.rust_version().cloned(),
Expand Down
131 changes: 62 additions & 69 deletions src/cargo/core/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,7 @@ fn prepare_rustdoc(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> CargoResu
let doc_dir = build_runner.files().out_dir(unit);
rustdoc.arg("-o").arg(&doc_dir);
rustdoc.args(&features_args(unit));
rustdoc.args(&check_cfg_args(build_runner, unit)?);
rustdoc.args(&check_cfg_args(unit)?);

add_error_format_and_color(build_runner, &mut rustdoc);
add_allow_features(build_runner, &mut rustdoc);
Expand Down Expand Up @@ -1125,7 +1125,7 @@ fn build_base_args(
}

cmd.args(&features_args(unit));
cmd.args(&check_cfg_args(build_runner, unit)?);
cmd.args(&check_cfg_args(unit)?);

let meta = build_runner.files().metadata(unit);
cmd.arg("-C").arg(&format!("metadata={}", meta));
Expand Down Expand Up @@ -1310,83 +1310,76 @@ fn trim_paths_args(
}

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

let gross_cap_estimation = unit.pkg.summary().features().len() * 7 + 25;
let mut arg_feature = OsString::with_capacity(gross_cap_estimation);
let gross_cap_estimation = unit.pkg.summary().features().len() * 7 + 25;
let mut arg_feature = OsString::with_capacity(gross_cap_estimation);

arg_feature.push("cfg(feature, values(");
for (i, feature) in unit.pkg.summary().features().keys().enumerate() {
if i != 0 {
arg_feature.push(", ");
}
arg_feature.push("\"");
arg_feature.push(feature);
arg_feature.push("\"");
arg_feature.push("cfg(feature, values(");
for (i, feature) in unit.pkg.summary().features().keys().enumerate() {
if i != 0 {
arg_feature.push(", ");
}
arg_feature.push("))");

// We also include the `docsrs` cfg from the docs.rs service. We include it here
// (in Cargo) instead of rustc, since there is a much closer relationship between
// Cargo and docs.rs than rustc and docs.rs. In particular, all users of docs.rs use
// Cargo, but not all users of rustc (like Rust-for-Linux) use docs.rs.

let mut args = vec![
OsString::from("--check-cfg"),
OsString::from("cfg(docsrs)"),
OsString::from("--check-cfg"),
arg_feature,
];

// Also include the custom arguments specified in `[lints.rust.unexpected_cfgs.check_cfg]`
if let Ok(Some(lints)) = unit.pkg.manifest().resolved_toml().resolved_lints() {
if let Some(rust_lints) = lints.get("rust") {
if let Some(unexpected_cfgs) = rust_lints.get("unexpected_cfgs") {
if let Some(config) = unexpected_cfgs.config() {
if let Some(check_cfg) = config.get("check-cfg") {
if let Ok(check_cfgs) =
toml::Value::try_into::<Vec<String>>(check_cfg.clone())
{
for check_cfg in check_cfgs {
args.push(OsString::from("--check-cfg"));
args.push(OsString::from(check_cfg));
}
// error about `check-cfg` not being a list-of-string
} else {
bail!("`lints.rust.unexpected_cfgs.check-cfg` must be a list of string");
arg_feature.push("\"");
arg_feature.push(feature);
arg_feature.push("\"");
}
arg_feature.push("))");

// We also include the `docsrs` cfg from the docs.rs service. We include it here
// (in Cargo) instead of rustc, since there is a much closer relationship between
// Cargo and docs.rs than rustc and docs.rs. In particular, all users of docs.rs use
// Cargo, but not all users of rustc (like Rust-for-Linux) use docs.rs.

let mut args = vec![
OsString::from("--check-cfg"),
OsString::from("cfg(docsrs)"),
OsString::from("--check-cfg"),
arg_feature,
];

// Also include the custom arguments specified in `[lints.rust.unexpected_cfgs.check_cfg]`
if let Ok(Some(lints)) = unit.pkg.manifest().resolved_toml().resolved_lints() {
if let Some(rust_lints) = lints.get("rust") {
if let Some(unexpected_cfgs) = rust_lints.get("unexpected_cfgs") {
if let Some(config) = unexpected_cfgs.config() {
if let Some(check_cfg) = config.get("check-cfg") {
if let Ok(check_cfgs) =
toml::Value::try_into::<Vec<String>>(check_cfg.clone())
{
for check_cfg in check_cfgs {
args.push(OsString::from("--check-cfg"));
args.push(OsString::from(check_cfg));
}
// error about `check-cfg` not being a list-of-string
} else {
bail!(
"`lints.rust.unexpected_cfgs.check-cfg` must be a list of string"
);
}
}
}
}
}

Ok(args)
} else {
Ok(Vec::new())
}

Ok(args)
}

/// Adds LTO related codegen flags.
Expand Down