Skip to content

Commit 459d194

Browse files
committed
Adjust -Zcheck-cfg for new rustc syntax and behavior
This commit removes all the -Zcheck-cfg options (features, names, values, output), as they don't make much sense now since features, names and values are all combine together for upstream rustc 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.
1 parent a275529 commit 459d194

File tree

5 files changed

+74
-251
lines changed

5 files changed

+74
-251
lines changed

src/cargo/core/compiler/custom_build.rs

Lines changed: 2 additions & 8 deletions
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

Lines changed: 23 additions & 30 deletions
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

Lines changed: 2 additions & 43 deletions
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

Lines changed: 1 addition & 5 deletions
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 {

0 commit comments

Comments
 (0)