Skip to content

Commit

Permalink
Auto merge of #13011 - Urgau:check-cfg-zero-features, r=weihanglo
Browse files Browse the repository at this point in the history
Fix `--check-cfg` invocations with zero features

When generating the `--check-cfg` arguments for `-Zcheck-cfg` we currently generate `cfg(feature, values())` when there is 0 features. This is wrong since a empty `values()` would mean that it's possible to have `cfg(feature)` without a feature name which is impossible.

We replace this by a simple `cfg()` to still enable well known names and values.

----

Note that currently `rustc` defines `feature` as a well known name with ANY values if it's not overridden by Cargo. I plan on submitting a PR to `rustc` to remove `feature` from being a well known name so that Cargo is the only source of truth.

*This doesn't block this PR from being merged*
  • Loading branch information
bors committed Nov 20, 2023
2 parents 90a2428 + 4781592 commit 79acbdb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
25 changes: 16 additions & 9 deletions src/cargo/core/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1243,24 +1243,31 @@ fn trim_paths_args(
fn check_cfg_args(cx: &Context<'_, '_>, unit: &Unit) -> Vec<OsString> {
if cx.bcx.config.cli_unstable().check_cfg {
// This generate something like this:
// - cfg(feature, values())
// - cfg()
// - cfg(feature, values("foo", "bar"))
//
// NOTE: Despite only explicitly specifying `feature`, well known names and values
// are implicitly enabled when one or more `--check-cfg` argument is passed.
// NOTE: Never generate a empty `values()` since it would mean that it's possible
// to have `cfg(feature)` without a feature name which is impossible.

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("cfg(");
if !unit.pkg.summary().features().is_empty() {
arg_feature.push("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("\"");
arg_feature.push(feature);
arg_feature.push("\"");
arg_feature.push(")");
}
arg_feature.push("))");
arg_feature.push(")");

vec![
OsString::from("-Zunstable-options"),
Expand Down
14 changes: 7 additions & 7 deletions tests/testsuite/check_cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ macro_rules! x {
$what, '(', $($who,)* ')', "'", "[..]")
}
}};
($tool:tt => $what:tt of $who:tt with $($first_value:tt $($other_values:tt)*)?) => {{
($tool:tt => $what:tt of $who:tt with $first_value:tt $($other_values:tt)*) => {{
#[cfg(windows)]
{
concat!("[RUNNING] [..]", $tool, "[..] --check-cfg \"",
$what, '(', $who, ", values(", $("/\"", $first_value, "/\"", $(", ", "/\"", $other_values, "/\"",)*)* "))", '"', "[..]")
$what, '(', $who, ", values(", "/\"", $first_value, "/\"", $(", ", "/\"", $other_values, "/\"",)* "))", '"', "[..]")
}
#[cfg(not(windows))]
{
concat!("[RUNNING] [..]", $tool, "[..] --check-cfg '",
$what, '(', $who, ", values(", $("\"", $first_value, "\"", $(", ", "\"", $other_values, "\"",)*)* "))", "'", "[..]")
$what, '(', $who, ", values(", "\"", $first_value, "\"", $(", ", "\"", $other_values, "\"",)* "))", "'", "[..]")
}
}};
}
Expand Down Expand Up @@ -150,7 +150,7 @@ fn well_known_names_values() {

p.cargo("check -v -Zcheck-cfg")
.masquerade_as_nightly_cargo(&["check-cfg"])
.with_stderr_contains(x!("rustc" => "cfg" of "feature" with))
.with_stderr_contains(x!("rustc" => "cfg"))
.run();
}

Expand Down Expand Up @@ -213,7 +213,7 @@ fn well_known_names_values_test() {

p.cargo("test -v -Zcheck-cfg")
.masquerade_as_nightly_cargo(&["check-cfg"])
.with_stderr_contains(x!("rustc" => "cfg" of "feature" with))
.with_stderr_contains(x!("rustc" => "cfg"))
.run();
}

Expand All @@ -226,8 +226,8 @@ fn well_known_names_values_doctest() {

p.cargo("test -v --doc -Zcheck-cfg")
.masquerade_as_nightly_cargo(&["check-cfg"])
.with_stderr_contains(x!("rustc" => "cfg" of "feature" with))
.with_stderr_contains(x!("rustdoc" => "cfg" of "feature" with))
.with_stderr_contains(x!("rustc" => "cfg"))
.with_stderr_contains(x!("rustdoc" => "cfg"))
.run();
}

Expand Down

0 comments on commit 79acbdb

Please sign in to comment.