Skip to content

Commit 16629e6

Browse files
committed
Auto merge of #12845 - Urgau:check-cfg-adjust-for-rustc, r=epage
Adjust `-Zcheck-cfg` for new rustc syntax and behavior rust-lang/rust#111072 introduced a new syntax for `rustc` `--check-cfg` argument. This PR adjust cargo `-Zcheck-cfg` for new that new syntax and behavior. This PR removes all the `-Zcheck-cfg` options (`features`, `names`, `values`, `output`), as they don't make much sense now since with the new `rustc` behavior: `features`, `names` and `values` are all combine together and the `output` option was only here because the other were. Now the new behavior from cargo is to always pass one `--check-cfg` argument to rustc for the `feature`s which implicitly enables well known names and values.
2 parents 67271fd + 2f79f15 commit 16629e6

File tree

6 files changed

+92
-278
lines changed

6 files changed

+92
-278
lines changed

src/cargo/core/compiler/custom_build.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -403,10 +403,7 @@ fn build_work(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Job> {
403403
paths::create_dir_all(&script_out_dir)?;
404404

405405
let nightly_features_allowed = cx.bcx.config.nightly_features_allowed;
406-
let extra_check_cfg = match cx.bcx.config.cli_unstable().check_cfg {
407-
Some((_, _, _, output)) => output,
408-
None => false,
409-
};
406+
let extra_check_cfg = cx.bcx.config.cli_unstable().check_cfg;
410407
let targets: Vec<Target> = unit.pkg.targets().to_vec();
411408
// Need a separate copy for the fresh closure.
412409
let targets_fresh = targets.clone();
@@ -1126,10 +1123,7 @@ fn prev_build_output(cx: &mut Context<'_, '_>, unit: &Unit) -> (Option<BuildOutp
11261123
&unit.pkg.to_string(),
11271124
&prev_script_out_dir,
11281125
&script_out_dir,
1129-
match cx.bcx.config.cli_unstable().check_cfg {
1130-
Some((_, _, _, output)) => output,
1131-
None => false,
1132-
},
1126+
cx.bcx.config.cli_unstable().check_cfg,
11331127
cx.bcx.config.nightly_features_allowed,
11341128
unit.pkg.targets(),
11351129
)

src/cargo/core/compiler/mod.rs

+23-30
Original file line numberDiff line numberDiff line change
@@ -1167,39 +1167,32 @@ fn features_args(unit: &Unit) -> Vec<OsString> {
11671167
///
11681168
/// [`check-cfg`]: https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#check-cfg
11691169
fn check_cfg_args(cx: &Context<'_, '_>, unit: &Unit) -> Vec<OsString> {
1170-
if let Some((features, well_known_names, well_known_values, _output)) =
1171-
cx.bcx.config.cli_unstable().check_cfg
1172-
{
1173-
let mut args = Vec::with_capacity(unit.pkg.summary().features().len() * 2 + 4);
1174-
args.push(OsString::from("-Zunstable-options"));
1175-
1176-
if features {
1177-
// This generate something like this:
1178-
// - values(feature)
1179-
// - values(feature, "foo", "bar")
1180-
let mut arg = OsString::from("values(feature");
1181-
for (&feat, _) in unit.pkg.summary().features() {
1182-
arg.push(", \"");
1183-
arg.push(&feat);
1184-
arg.push("\"");
1170+
if cx.bcx.config.cli_unstable().check_cfg {
1171+
// This generate something like this:
1172+
// - cfg(feature, values())
1173+
// - cfg(feature, values("foo", "bar"))
1174+
//
1175+
// NOTE: Despite only explicitly specifying `feature`, well known names and values
1176+
// are implicitly enabled when one or more `--check-cfg` argument is passed.
1177+
1178+
let gross_cap_estimation = unit.pkg.summary().features().len() * 7 + 25;
1179+
let mut arg_feature = OsString::with_capacity(gross_cap_estimation);
1180+
arg_feature.push("cfg(feature, values(");
1181+
for (i, feature) in unit.pkg.summary().features().keys().enumerate() {
1182+
if i != 0 {
1183+
arg_feature.push(", ");
11851184
}
1186-
arg.push(")");
1187-
1188-
args.push(OsString::from("--check-cfg"));
1189-
args.push(arg);
1190-
}
1191-
1192-
if well_known_names {
1193-
args.push(OsString::from("--check-cfg"));
1194-
args.push(OsString::from("names()"));
1195-
}
1196-
1197-
if well_known_values {
1198-
args.push(OsString::from("--check-cfg"));
1199-
args.push(OsString::from("values()"));
1185+
arg_feature.push("\"");
1186+
arg_feature.push(feature);
1187+
arg_feature.push("\"");
12001188
}
1189+
arg_feature.push("))");
12011190

1202-
args
1191+
vec![
1192+
OsString::from("-Zunstable-options"),
1193+
OsString::from("--check-cfg"),
1194+
arg_feature,
1195+
]
12031196
} else {
12041197
Vec::new()
12051198
}

src/cargo/core/features.rs

+2-43
Original file line numberDiff line numberDiff line change
@@ -731,8 +731,7 @@ unstable_cli_options!(
731731
#[serde(deserialize_with = "deserialize_build_std")]
732732
build_std: Option<Vec<String>> = ("Enable Cargo to compile the standard library itself as part of a crate graph compilation"),
733733
build_std_features: Option<Vec<String>> = ("Configure features enabled for the standard library itself when building the standard library"),
734-
#[serde(deserialize_with = "deserialize_check_cfg")]
735-
check_cfg: Option<(/*features:*/ bool, /*well_known_names:*/ bool, /*well_known_values:*/ bool, /*output:*/ bool)> = ("Specify scope of compile-time checking of `cfg` names/values"),
734+
check_cfg: bool = ("Enable compile-time checking of `cfg` names/values/features"),
736735
codegen_backend: bool = ("Enable the `codegen-backend` option in profiles in .cargo/config.toml file"),
737736
config_include: bool = ("Enable the `include` key in config files"),
738737
direct_minimal_versions: bool = ("Resolve minimal dependency versions instead of maximum (direct dependencies only)"),
@@ -842,20 +841,6 @@ where
842841
))
843842
}
844843

845-
fn deserialize_check_cfg<'de, D>(
846-
deserializer: D,
847-
) -> Result<Option<(bool, bool, bool, bool)>, D::Error>
848-
where
849-
D: serde::Deserializer<'de>,
850-
{
851-
use serde::de::Error;
852-
let Some(crates) = <Option<Vec<String>>>::deserialize(deserializer)? else {
853-
return Ok(None);
854-
};
855-
856-
parse_check_cfg(crates.into_iter()).map_err(D::Error::custom)
857-
}
858-
859844
#[derive(Debug, Copy, Clone, Default, Deserialize)]
860845
pub struct GitoxideFeatures {
861846
/// All fetches are done with `gitoxide`, which includes git dependencies as well as the crates index.
@@ -924,32 +909,6 @@ fn parse_gitoxide(
924909
Ok(Some(out))
925910
}
926911

927-
fn parse_check_cfg(
928-
it: impl Iterator<Item = impl AsRef<str>>,
929-
) -> CargoResult<Option<(bool, bool, bool, bool)>> {
930-
let mut features = false;
931-
let mut well_known_names = false;
932-
let mut well_known_values = false;
933-
let mut output = false;
934-
935-
for e in it {
936-
match e.as_ref() {
937-
"features" => features = true,
938-
"names" => well_known_names = true,
939-
"values" => well_known_values = true,
940-
"output" => output = true,
941-
_ => bail!("unstable check-cfg only takes `features`, `names`, `values` or `output` as valid inputs"),
942-
}
943-
}
944-
945-
Ok(Some((
946-
features,
947-
well_known_names,
948-
well_known_values,
949-
output,
950-
)))
951-
}
952-
953912
impl CliUnstable {
954913
pub fn parse(
955914
&mut self,
@@ -1107,7 +1066,7 @@ impl CliUnstable {
11071066
}
11081067
"build-std-features" => self.build_std_features = Some(parse_features(v)),
11091068
"check-cfg" => {
1110-
self.check_cfg = v.map_or(Ok(None), |v| parse_check_cfg(v.split(',')))?
1069+
self.check_cfg = parse_empty(k, v)?;
11111070
}
11121071
"codegen-backend" => self.codegen_backend = parse_empty(k, v)?,
11131072
"config-include" => self.config_include = parse_empty(k, v)?,

src/cargo/util/config/target.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,6 @@ fn parse_links_overrides(
137137
config: &Config,
138138
) -> CargoResult<BTreeMap<String, BuildOutput>> {
139139
let mut links_overrides = BTreeMap::new();
140-
let extra_check_cfg = match config.cli_unstable().check_cfg {
141-
Some((_, _, _, output)) => output,
142-
None => false,
143-
};
144140

145141
for (lib_name, value) in links {
146142
// Skip these keys, it shares the namespace with `TargetConfig`.
@@ -207,7 +203,7 @@ fn parse_links_overrides(
207203
output.cfgs.extend(list.iter().map(|v| v.0.clone()));
208204
}
209205
"rustc-check-cfg" => {
210-
if extra_check_cfg {
206+
if config.cli_unstable().check_cfg {
211207
let list = value.list(key)?;
212208
output.check_cfgs.extend(list.iter().map(|v| v.0.clone()));
213209
} else {

src/doc/src/reference/unstable.md

+18-27
Original file line numberDiff line numberDiff line change
@@ -1080,34 +1080,14 @@ you are ok with dev-deps being build for `cargo doc`.
10801080
* RFC: [#3013](https://github.com/rust-lang/rfcs/pull/3013)
10811081
* Tracking Issue: [#10554](https://github.com/rust-lang/cargo/issues/10554)
10821082

1083-
`-Z check-cfg` command line enables compile time checking of name and values in `#[cfg]`, `cfg!`,
1084-
`#[link]` and `#[cfg_attr]` with the `rustc` and `rustdoc` unstable `--check-cfg` command line.
1083+
`-Z check-cfg` command line enables compile time checking of Cargo features as well as `rustc`
1084+
well known names and values in `#[cfg]`, `cfg!`, `#[link]` and `#[cfg_attr]` with the `rustc`
1085+
and `rustdoc` unstable `--check-cfg` command line.
10851086

1086-
It's values are:
1087-
- `features`: enables features checking via `--check-cfg=values(feature, ...)`.
1088-
Note than this command line options will probably become the default when stabilizing.
1089-
- `names`: enables well known names checking via `--check-cfg=names()`.
1090-
- `values`: enables well known values checking via `--check-cfg=values()`.
1091-
- `output`: enable the use of `rustc-check-cfg` in build script.
1092-
1093-
For instance:
1094-
1095-
```
1096-
cargo check -Z unstable-options -Z check-cfg=features
1097-
cargo check -Z unstable-options -Z check-cfg=names
1098-
cargo check -Z unstable-options -Z check-cfg=values
1099-
cargo check -Z unstable-options -Z check-cfg=features,names,values
1100-
```
1101-
1102-
Or for `output`:
1103-
1104-
```rust,no_run
1105-
// build.rs
1106-
println!("cargo:rustc-check-cfg=names(foo, bar)");
1107-
```
1087+
You can use the flag like this:
11081088

11091089
```
1110-
cargo check -Z unstable-options -Z check-cfg=output
1090+
cargo check -Z unstable-options -Z check-cfg
11111091
```
11121092

11131093
### `cargo:rustc-check-cfg=CHECK_CFG`
@@ -1116,12 +1096,23 @@ The `rustc-check-cfg` instruction tells Cargo to pass the given value to the
11161096
`--check-cfg` flag to the compiler. This may be used for compile-time
11171097
detection of unexpected conditional compilation name and/or values.
11181098

1119-
This can only be used in combination with `-Zcheck-cfg=output` otherwise it is ignored
1099+
This can only be used in combination with `-Zcheck-cfg` otherwise it is ignored
11201100
with a warning.
11211101

1122-
If you want to integrate with Cargo features, use `-Zcheck-cfg=features` instead of
1102+
If you want to integrate with Cargo features, only use `-Zcheck-cfg` instead of
11231103
trying to do it manually with this option.
11241104

1105+
You can use the instruction like this:
1106+
1107+
```rust,no_run
1108+
// build.rs
1109+
println!("cargo:rustc-check-cfg=cfg(foo, bar)");
1110+
```
1111+
1112+
```
1113+
cargo check -Z unstable-options -Z check-cfg
1114+
```
1115+
11251116
## codegen-backend
11261117

11271118
The `codegen-backend` feature makes it possible to select the codegen backend used by rustc using a profile.

0 commit comments

Comments
 (0)