Skip to content

Commit 9574120

Browse files
committed
Auto merge of #13316 - Urgau:check-cfg-zero-features-2, r=epage
Go back to passing an empty `values()` when no features are declared This PR is basically a revert of #13011, which made the `--check-cfg` invocation be `cfg()` when no features are declared instead of `cfg(feature, values())` since it meant declaring that the config `feature` were to be available in this form: `#[cfg(feature)]`, which is not the case. Thankfully after some brainstorming, I [proposed](rust-lang/rust#119930) changing the behavior of empty `values()` in `rustc` to no longer imply the _none_ value and simply create an empty set of expected values. 😃 For Cargo, always using `cfg(feature, values(...))` means that the config `feature` is always expected, regardless of the number of features. This makes the warning better, as it will now always be `unexpected config value` (instead of `unexpected config name`). 🎉 Fixes #13011 (comment) as well as the concern in the [tracking issue](#10554). r? `@epage`
2 parents c9d5cb3 + fecb8ff commit 9574120

File tree

2 files changed

+19
-26
lines changed

2 files changed

+19
-26
lines changed

src/cargo/core/compiler/mod.rs

+12-19
Original file line numberDiff line numberDiff line change
@@ -1270,34 +1270,27 @@ fn check_cfg_args(cx: &Context<'_, '_>, unit: &Unit) -> Vec<OsString> {
12701270
//
12711271
// but having `cfg()` is redundant with the second argument (as well-known names
12721272
// and values are implicitly enabled when one or more `--check-cfg` argument is
1273-
// passed) so we don't emit it:
1273+
// passed) so we don't emit it and just pass:
12741274
//
12751275
// --check-cfg=cfg(feature, values(...))
12761276
//
1277-
// expect when there are no features declared, where we can't generate the
1278-
// `cfg(feature, values())` argument since it would mean that it is somehow
1279-
// possible to have a `#[cfg(feature)]` without a feature name, which is
1280-
// impossible and not what we want, so we just generate:
1281-
//
1282-
// --check-cfg=cfg()
1277+
// This way, even if there are no declared features, the config `feature` will
1278+
// still be expected, meaning users would get "unexpected value" instead of name.
1279+
// This wasn't always the case, see rust-lang#119930 for some details.
12831280

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

1287-
arg_feature.push("cfg(");
1288-
if !unit.pkg.summary().features().is_empty() {
1289-
arg_feature.push("feature, values(");
1290-
for (i, feature) in unit.pkg.summary().features().keys().enumerate() {
1291-
if i != 0 {
1292-
arg_feature.push(", ");
1293-
}
1294-
arg_feature.push("\"");
1295-
arg_feature.push(feature);
1296-
arg_feature.push("\"");
1284+
arg_feature.push("cfg(feature, values(");
1285+
for (i, feature) in unit.pkg.summary().features().keys().enumerate() {
1286+
if i != 0 {
1287+
arg_feature.push(", ");
12971288
}
1298-
arg_feature.push(")");
1289+
arg_feature.push("\"");
1290+
arg_feature.push(feature);
1291+
arg_feature.push("\"");
12991292
}
1300-
arg_feature.push(")");
1293+
arg_feature.push("))");
13011294

13021295
vec![
13031296
OsString::from("-Zunstable-options"),

tests/testsuite/check_cfg.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@ macro_rules! x {
1515
$what, '(', $($who,)* ')', "'", "[..]")
1616
}
1717
}};
18-
($tool:tt => $what:tt of $who:tt with $first_value:tt $($other_values:tt)*) => {{
18+
($tool:tt => $what:tt of $who:tt with $($first_value:tt $($other_values:tt)*)?) => {{
1919
#[cfg(windows)]
2020
{
2121
concat!("[RUNNING] [..]", $tool, "[..] --check-cfg \"",
22-
$what, '(', $who, ", values(", "/\"", $first_value, "/\"", $(", ", "/\"", $other_values, "/\"",)* "))", '"', "[..]")
22+
$what, '(', $who, ", values(", $("/\"", $first_value, "/\"", $(", ", "/\"", $other_values, "/\"",)*)* "))", '"', "[..]")
2323
}
2424
#[cfg(not(windows))]
2525
{
2626
concat!("[RUNNING] [..]", $tool, "[..] --check-cfg '",
27-
$what, '(', $who, ", values(", "\"", $first_value, "\"", $(", ", "\"", $other_values, "\"",)* "))", "'", "[..]")
27+
$what, '(', $who, ", values(", $("\"", $first_value, "\"", $(", ", "\"", $other_values, "\"",)*)* "))", "'", "[..]")
2828
}
2929
}};
3030
}
@@ -221,7 +221,7 @@ fn well_known_names_values() {
221221

222222
p.cargo("check -v -Zcheck-cfg")
223223
.masquerade_as_nightly_cargo(&["check-cfg"])
224-
.with_stderr_contains(x!("rustc" => "cfg"))
224+
.with_stderr_contains(x!("rustc" => "cfg" of "feature" with))
225225
.run();
226226
}
227227

@@ -284,7 +284,7 @@ fn well_known_names_values_test() {
284284

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

@@ -297,8 +297,8 @@ fn well_known_names_values_doctest() {
297297

298298
p.cargo("test -v --doc -Zcheck-cfg")
299299
.masquerade_as_nightly_cargo(&["check-cfg"])
300-
.with_stderr_contains(x!("rustc" => "cfg"))
301-
.with_stderr_contains(x!("rustdoc" => "cfg"))
300+
.with_stderr_contains(x!("rustc" => "cfg" of "feature" with))
301+
.with_stderr_contains(x!("rustdoc" => "cfg" of "feature" with))
302302
.run();
303303
}
304304

0 commit comments

Comments
 (0)