Skip to content
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

Go back to passing an empty values() when no features are declared #13316

Merged
merged 1 commit into from
Jan 18, 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
31 changes: 12 additions & 19 deletions src/cargo/core/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1270,34 +1270,27 @@ fn check_cfg_args(cx: &Context<'_, '_>, unit: &Unit) -> Vec<OsString> {
//
// 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:
// passed) so we don't emit it and just pass:
//
// --check-cfg=cfg(feature, values(...))
//
// expect when there are no features declared, where we can't generate the
// `cfg(feature, values())` argument since it would mean that it is somehow
// possible to have a `#[cfg(feature)]` without a feature name, which is
// impossible and not what we want, so we just generate:
//
// --check-cfg=cfg()
// 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);

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("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("\"");
arg_feature.push(feature);
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 @@ -221,7 +221,7 @@ fn well_known_names_values() {

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

Expand Down Expand Up @@ -284,7 +284,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"))
.with_stderr_contains(x!("rustc" => "cfg" of "feature" with))
.run();
}

Expand All @@ -297,8 +297,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"))
.with_stderr_contains(x!("rustdoc" => "cfg"))
.with_stderr_contains(x!("rustc" => "cfg" of "feature" with))
.with_stderr_contains(x!("rustdoc" => "cfg" of "feature" with))
.run();
}

Expand Down