Skip to content

Commit

Permalink
Stabilize -Zcheck-cfg as always enabled-by-default
Browse files Browse the repository at this point in the history
  • Loading branch information
Urgau committed Mar 10, 2024
1 parent d79fdf3 commit fbaea97
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 297 deletions.
11 changes: 4 additions & 7 deletions 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 All @@ -256,12 +256,9 @@ impl<'a, 'gctx> BuildRunner<'a, 'gctx> {
args.push(cfg.into());
}

if !output.check_cfgs.is_empty() {
args.push("-Zunstable-options".into());
for check_cfg in &output.check_cfgs {
args.push("--check-cfg".into());
args.push(check_cfg.into());
}
for check_cfg in &output.check_cfgs {
args.push("--check-cfg".into());
args.push(check_cfg.into());
}

for (lt, arg) in &output.linker_args {
Expand Down
17 changes: 1 addition & 16 deletions src/cargo/core/compiler/custom_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,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.gctx.cli_unstable().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 @@ -556,7 +555,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 @@ -585,7 +583,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 @@ -642,7 +639,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 @@ -654,7 +650,6 @@ impl BuildOutput {
pkg_descr,
script_out_dir_when_generated,
script_out_dir,
extra_check_cfg,
nightly_features_allowed,
targets,
msrv,
Expand All @@ -665,17 +660,13 @@ impl BuildOutput {
///
/// * `pkg_descr` --- for error messages
/// * `library_name` --- for determining if `RUSTC_BOOTSTRAP` should be allowed
/// * `extra_check_cfg` --- for unstable feature [`-Zcheck-cfg`]
///
/// [`-Zcheck-cfg`]: https://doc.rust-lang.org/cargo/reference/unstable.html#check-cfg
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 @@ -908,12 +899,7 @@ impl BuildOutput {
}
"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 to try to
// minimise MSRV annoyance when stabilizing -Zcheck-cfg
}
check_cfgs.push(value.to_string());
}
"rustc-env" => {
let (key, val) = BuildOutput::parse_rustc_env(&value, &whence)?;
Expand Down Expand Up @@ -1255,7 +1241,6 @@ fn prev_build_output(
&unit.pkg.to_string(),
&prev_script_out_dir,
&script_out_dir,
build_runner.bcx.gctx.cli_unstable().check_cfg,
build_runner.bcx.gctx.nightly_features_allowed,
unit.pkg.targets(),
&unit.pkg.rust_version().cloned(),
Expand Down
9 changes: 1 addition & 8 deletions src/cargo/core/compiler/fingerprint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1454,14 +1454,7 @@ fn calculate_normal(
// actually affect the output artifact so there's no need to hash it.
path: util::hash_u64(path_args(build_runner.bcx.ws, unit).0),
features: format!("{:?}", unit.features),
// Note we curently only populate `declared_features` when `-Zcheck-cfg`
// is passed since it's the only user-facing toggle that will make this
// fingerprint relevant.
declared_features: if build_runner.bcx.gctx.cli_unstable().check_cfg {
format!("{declared_features:?}")
} else {
"".to_string()
},
declared_features: format!("{declared_features:?}"),
deps,
local: Mutex::new(local),
memoized_hash: Mutex::new(None),
Expand Down
90 changes: 42 additions & 48 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,57 +1310,51 @@ fn trim_paths_args(
}

/// Generates the `--check-cfg` arguments for the `unit`.
/// See unstable feature [`check-cfg`].
///
/// [`check-cfg`]: https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#check-cfg
fn check_cfg_args(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> Vec<OsString> {
if build_runner.bcx.gctx.cli_unstable().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) -> 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.

vec![
OsString::from("-Zunstable-options"),
OsString::from("--check-cfg"),
OsString::from("cfg(docsrs)"),
OsString::from("--check-cfg"),
arg_feature,
]
} else {
Vec::new()
}
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.

vec![
OsString::from("--check-cfg"),
OsString::from("cfg(docsrs)"),
OsString::from("--check-cfg"),
arg_feature,
]
}

/// Adds LTO related codegen flags.
Expand Down
8 changes: 4 additions & 4 deletions src/cargo/core/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,6 @@ unstable_cli_options!(
#[serde(deserialize_with = "deserialize_build_std")]
build_std: Option<Vec<String>> = ("Enable Cargo to compile the standard library itself as part of a crate graph compilation"),
build_std_features: Option<Vec<String>> = ("Configure features enabled for the standard library itself when building the standard library"),
check_cfg: bool = ("Enable compile-time checking of `cfg` names/values/features"),
codegen_backend: bool = ("Enable the `codegen-backend` option in profiles in .cargo/config.toml file"),
config_include: bool = ("Enable the `include` key in config files"),
direct_minimal_versions: bool = ("Resolve minimal dependency versions instead of maximum (direct dependencies only)"),
Expand Down Expand Up @@ -854,6 +853,9 @@ const STABILIZED_CREDENTIAL_PROCESS: &str =
const STABILIZED_REGISTRY_AUTH: &str =
"Authenticated registries are available if a credential provider is configured.";

const STABILIZED_CHECK_CFG: &str =
"Compile-time checking of conditional (a.k.a. `-Zcheck-cfg`) is now always enabled.";

fn deserialize_build_std<'de, D>(deserializer: D) -> Result<Option<Vec<String>>, D::Error>
where
D: serde::Deserializer<'de>,
Expand Down Expand Up @@ -1107,6 +1109,7 @@ impl CliUnstable {
"doctest-in-workspace" => stabilized_warn(k, "1.72", STABILIZED_DOCTEST_IN_WORKSPACE),
"credential-process" => stabilized_warn(k, "1.74", STABILIZED_CREDENTIAL_PROCESS),
"registry-auth" => stabilized_warn(k, "1.74", STABILIZED_REGISTRY_AUTH),
"check-cfg" => stabilized_warn(k, "CURRENT_CARGO_VERSION", STABILIZED_CHECK_CFG),

// Unstable features
// Sorted alphabetically:
Expand All @@ -1119,9 +1122,6 @@ impl CliUnstable {
self.build_std = Some(crate::core::compiler::standard_lib::parse_unstable_flag(v))
}
"build-std-features" => self.build_std_features = Some(parse_features(v)),
"check-cfg" => {
self.check_cfg = parse_empty(k, v)?;
}
"codegen-backend" => self.codegen_backend = parse_empty(k, v)?,
"config-include" => self.config_include = parse_empty(k, v)?,
"direct-minimal-versions" => self.direct_minimal_versions = parse_empty(k, v)?,
Expand Down
11 changes: 3 additions & 8 deletions src/cargo/util/context/target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ fn load_config_table(gctx: &GlobalContext, prefix: &str) -> CargoResult<TargetCo
fn parse_links_overrides(
target_key: &ConfigKey,
links: HashMap<String, CV>,
gctx: &GlobalContext,
_gctx: &GlobalContext,
) -> CargoResult<BTreeMap<String, BuildOutput>> {
let mut links_overrides = BTreeMap::new();

Expand Down Expand Up @@ -204,13 +204,8 @@ fn parse_links_overrides(
output.cfgs.extend(list.iter().map(|v| v.0.clone()));
}
"rustc-check-cfg" => {
if gctx.cli_unstable().check_cfg {
let list = value.list(key)?;
output.check_cfgs.extend(list.iter().map(|v| v.0.clone()));
} else {
// silently ignoring the instruction to try to
// minimise MSRV annoyance when stabilizing -Zcheck-cfg
}
let list = value.list(key)?;
output.check_cfgs.extend(list.iter().map(|v| v.0.clone()));
}
"rustc-env" => {
for (name, val) in value.table(key)?.0 {
Expand Down
Loading

0 comments on commit fbaea97

Please sign in to comment.