Skip to content

Commit 403c6bd

Browse files
committed
Auto merge of #10486 - Urgau:check-cfg-well-known, r=weihanglo
Add support for rustc --check-cfg well known names and values This pull-request add support for `rustc` `--check-cfg` well known names and values. ### What does this PR try to resolve? This pull-request add support for `rustc` `--check-cfg` well known names and values: - `-Z check-cfg-well-known-names`: `--check-cfg names()` well known names - `-Z check-cfg-well-known-values`: `--check-cfg values()` well known values ### How should we test and review this PR? #### Testing `cargo check -Z check-cfg-well-known-names` and `cargo check -Z check-cfg-well-known-values` #### Review This PR contains one commit that split `features_args` into `check_cfg_args`, add the well known support in it, adds call to that new function and add documentation and test for those new flags. ### Additional information This was implemented as two new additional flags because it's most likely that these flags will not be stabilize at the same time and also because some of these flags may become the default. RFC: rust-lang/rfcs#3013 `-Z check-cfg-features`: #10408 `cargo doc` support: #10428
2 parents 50d52eb + 02cd944 commit 403c6bd

File tree

7 files changed

+303
-4
lines changed

7 files changed

+303
-4
lines changed

src/cargo/core/compiler/context/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
225225
let mut args = compiler::extern_args(&self, unit, &mut unstable_opts)?;
226226
args.extend(compiler::lto_args(&self, unit));
227227
args.extend(compiler::features_args(&self, unit));
228+
args.extend(compiler::check_cfg_args(&self, unit));
228229

229230
let script_meta = self.find_build_script_metadata(unit);
230231
if let Some(meta) = script_meta {

src/cargo/core/compiler/mod.rs

+30-4
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,7 @@ fn rustdoc(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Work> {
646646

647647
rustdoc.arg("-o").arg(&doc_dir);
648648
rustdoc.args(&features_args(cx, unit));
649+
rustdoc.args(&check_cfg_args(cx, unit));
649650

650651
add_error_format_and_color(cx, &mut rustdoc);
651652
add_allow_features(cx, &mut rustdoc);
@@ -966,6 +967,7 @@ fn build_base_args(
966967
}
967968

968969
cmd.args(&features_args(cx, unit));
970+
cmd.args(&check_cfg_args(cx, unit));
969971

970972
let meta = cx.files().metadata(unit);
971973
cmd.arg("-C").arg(&format!("metadata={}", meta));
@@ -1039,15 +1041,30 @@ fn build_base_args(
10391041
Ok(())
10401042
}
10411043

1042-
/// Features with --cfg and all features with --check-cfg
1043-
fn features_args(cx: &Context<'_, '_>, unit: &Unit) -> Vec<OsString> {
1044-
let mut args = Vec::with_capacity(unit.features.len() + 2);
1044+
/// All active features for the unit passed as --cfg
1045+
fn features_args(_cx: &Context<'_, '_>, unit: &Unit) -> Vec<OsString> {
1046+
let mut args = Vec::with_capacity(unit.features.len() * 2);
10451047

10461048
for feat in &unit.features {
10471049
args.push(OsString::from("--cfg"));
10481050
args.push(OsString::from(format!("feature=\"{}\"", feat)));
10491051
}
10501052

1053+
args
1054+
}
1055+
1056+
/// Generate the --check-cfg arguments for the unit
1057+
fn check_cfg_args(cx: &Context<'_, '_>, unit: &Unit) -> Vec<OsString> {
1058+
if !cx.bcx.config.cli_unstable().check_cfg_features
1059+
&& !cx.bcx.config.cli_unstable().check_cfg_well_known_names
1060+
&& !cx.bcx.config.cli_unstable().check_cfg_well_known_values
1061+
{
1062+
return Vec::new();
1063+
}
1064+
1065+
let mut args = Vec::with_capacity(unit.pkg.summary().features().len() * 2 + 4);
1066+
args.push(OsString::from("-Zunstable-options"));
1067+
10511068
if cx.bcx.config.cli_unstable().check_cfg_features {
10521069
// This generate something like this:
10531070
// - values(feature)
@@ -1060,11 +1077,20 @@ fn features_args(cx: &Context<'_, '_>, unit: &Unit) -> Vec<OsString> {
10601077
}
10611078
arg.push(")");
10621079

1063-
args.push(OsString::from("-Zunstable-options"));
10641080
args.push(OsString::from("--check-cfg"));
10651081
args.push(arg);
10661082
}
10671083

1084+
if cx.bcx.config.cli_unstable().check_cfg_well_known_names {
1085+
args.push(OsString::from("--check-cfg"));
1086+
args.push(OsString::from("names()"));
1087+
}
1088+
1089+
if cx.bcx.config.cli_unstable().check_cfg_well_known_values {
1090+
args.push(OsString::from("--check-cfg"));
1091+
args.push(OsString::from("values()"));
1092+
}
1093+
10681094
args
10691095
}
10701096

src/cargo/core/features.rs

+4
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,8 @@ unstable_cli_options!(
642642
config_include: bool = ("Enable the `include` key in config files"),
643643
credential_process: bool = ("Add a config setting to fetch registry authentication tokens by calling an external process"),
644644
check_cfg_features: bool = ("Enable compile-time checking of features in `cfg`"),
645+
check_cfg_well_known_names: bool = ("Enable compile-time checking of well known names in `cfg`"),
646+
check_cfg_well_known_values: bool = ("Enable compile-time checking of well known values in `cfg`"),
645647
doctest_in_workspace: bool = ("Compile doctests with paths relative to the workspace root"),
646648
doctest_xcompile: bool = ("Compile and run doctests for non-host target using runner config"),
647649
dual_proc_macros: bool = ("Build proc-macros for both the host and the target"),
@@ -841,6 +843,8 @@ impl CliUnstable {
841843
"advanced-env" => self.advanced_env = parse_empty(k, v)?,
842844
"config-include" => self.config_include = parse_empty(k, v)?,
843845
"check-cfg-features" => self.check_cfg_features = parse_empty(k, v)?,
846+
"check-cfg-well-known-names" => self.check_cfg_well_known_names = parse_empty(k, v)?,
847+
"check-cfg-well-known-values" => self.check_cfg_well_known_values = parse_empty(k, v)?,
844848
"dual-proc-macros" => self.dual_proc_macros = parse_empty(k, v)?,
845849
// can also be set in .cargo/config or with and ENV
846850
"mtime-on-use" => self.mtime_on_use = parse_empty(k, v)?,

src/doc/src/reference/unstable.md

+26
Original file line numberDiff line numberDiff line change
@@ -1202,6 +1202,32 @@ For instance:
12021202
cargo check -Z unstable-options -Z check-cfg-features
12031203
```
12041204

1205+
### check-cfg-well-known-names
1206+
1207+
* RFC: [#3013](https://github.com/rust-lang/rfcs/pull/3013)
1208+
1209+
The `-Z check-cfg-well-known-names` argument tells Cargo to activate `rustc` and `rustdoc` unstable
1210+
`--check-cfg` command line as `--check-cfg=names()`.
1211+
This enables compile time checking of well known names in `#[cfg]`, `cfg!` and `#[cfg_attr]`.
1212+
For instance:
1213+
1214+
```
1215+
cargo check -Z unstable-options -Z check-cfg-well-known-names
1216+
```
1217+
1218+
### check-cfg-well-known-values
1219+
1220+
* RFC: [#3013](https://github.com/rust-lang/rfcs/pull/3013)
1221+
1222+
The `-Z check-cfg-well-known-values` argument tells Cargo to activate `rustc` and `rustdoc` unstable
1223+
`--check-cfg` command line as `--check-cfg=values()`.
1224+
This enables compile time checking of well known values in `#[cfg]`, `cfg!` and `#[cfg_attr]`.
1225+
For instance:
1226+
1227+
```
1228+
cargo check -Z unstable-options -Z check-cfg-well-known-values
1229+
```
1230+
12051231
## Stabilized and removed features
12061232

12071233
### Compile progress

tests/testsuite/build.rs

+86
Original file line numberDiff line numberDiff line change
@@ -6388,3 +6388,89 @@ fn check_cfg_features_with_namespaced_features() {
63886388
)
63896389
.run();
63906390
}
6391+
6392+
#[cfg_attr(windows, ignore)] // weird normalization issue with windows and cargo-test-support
6393+
#[cargo_test]
6394+
fn check_cfg_well_known_names() {
6395+
if !is_nightly() {
6396+
// --check-cfg is a nightly only rustc command line
6397+
return;
6398+
}
6399+
6400+
let p = project()
6401+
.file("Cargo.toml", &basic_manifest("foo", "0.1.0"))
6402+
.file("src/main.rs", "fn main() {}")
6403+
.build();
6404+
6405+
p.cargo("build -v -Z check-cfg-well-known-names")
6406+
.masquerade_as_nightly_cargo()
6407+
.with_stderr(
6408+
"\
6409+
[COMPILING] foo v0.1.0 [..]
6410+
[RUNNING] `rustc [..] --check-cfg 'names()' [..]
6411+
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
6412+
",
6413+
)
6414+
.run();
6415+
}
6416+
6417+
#[cfg_attr(windows, ignore)] // weird normalization issue with windows and cargo-test-support
6418+
#[cargo_test]
6419+
fn check_cfg_well_known_values() {
6420+
if !is_nightly() {
6421+
// --check-cfg is a nightly only rustc command line
6422+
return;
6423+
}
6424+
6425+
let p = project()
6426+
.file("Cargo.toml", &basic_manifest("foo", "0.1.0"))
6427+
.file("src/main.rs", "fn main() {}")
6428+
.build();
6429+
6430+
p.cargo("build -v -Z check-cfg-well-known-values")
6431+
.masquerade_as_nightly_cargo()
6432+
.with_stderr(
6433+
"\
6434+
[COMPILING] foo v0.1.0 [..]
6435+
[RUNNING] `rustc [..] --check-cfg 'values()' [..]
6436+
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
6437+
",
6438+
)
6439+
.run();
6440+
}
6441+
6442+
#[cfg_attr(windows, ignore)] // weird normalization issue with windows and cargo-test-support
6443+
#[cargo_test]
6444+
fn check_cfg_all() {
6445+
if !is_nightly() {
6446+
// --check-cfg is a nightly only rustc command line
6447+
return;
6448+
}
6449+
6450+
let p = project()
6451+
.file(
6452+
"Cargo.toml",
6453+
r#"
6454+
[project]
6455+
name = "foo"
6456+
version = "0.1.0"
6457+
6458+
[features]
6459+
f_a = []
6460+
f_b = []
6461+
"#,
6462+
)
6463+
.file("src/main.rs", "fn main() {}")
6464+
.build();
6465+
6466+
p.cargo("build -v -Z check-cfg-features -Z check-cfg-well-known-names -Z check-cfg-well-known-values")
6467+
.masquerade_as_nightly_cargo()
6468+
.with_stderr(
6469+
"\
6470+
[COMPILING] foo v0.1.0 [..]
6471+
[RUNNING] `rustc [..] --check-cfg 'values(feature, \"f_a\", \"f_b\")' --check-cfg 'names()' --check-cfg 'values()' [..]
6472+
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
6473+
",
6474+
)
6475+
.run();
6476+
}

tests/testsuite/check.rs

+50
Original file line numberDiff line numberDiff line change
@@ -1050,3 +1050,53 @@ fn check_cfg_features() {
10501050
)
10511051
.run();
10521052
}
1053+
1054+
#[cfg_attr(windows, ignore)] // weird normalization issue with windows and cargo-test-support
1055+
#[cargo_test]
1056+
fn check_cfg_well_known_names() {
1057+
if !is_nightly() {
1058+
// --check-cfg is a nightly only rustc command line
1059+
return;
1060+
}
1061+
1062+
let p = project()
1063+
.file("Cargo.toml", &basic_manifest("foo", "0.1.0"))
1064+
.file("src/main.rs", "fn main() {}")
1065+
.build();
1066+
1067+
p.cargo("check -v -Z check-cfg-well-known-names")
1068+
.masquerade_as_nightly_cargo()
1069+
.with_stderr(
1070+
"\
1071+
[CHECKING] foo v0.1.0 [..]
1072+
[RUNNING] `rustc [..] --check-cfg 'names()' [..]
1073+
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
1074+
",
1075+
)
1076+
.run();
1077+
}
1078+
1079+
#[cfg_attr(windows, ignore)] // weird normalization issue with windows and cargo-test-support
1080+
#[cargo_test]
1081+
fn check_cfg_well_known_values() {
1082+
if !is_nightly() {
1083+
// --check-cfg is a nightly only rustc command line
1084+
return;
1085+
}
1086+
1087+
let p = project()
1088+
.file("Cargo.toml", &basic_manifest("foo", "0.1.0"))
1089+
.file("src/main.rs", "fn main() {}")
1090+
.build();
1091+
1092+
p.cargo("check -v -Z check-cfg-well-known-values")
1093+
.masquerade_as_nightly_cargo()
1094+
.with_stderr(
1095+
"\
1096+
[CHECKING] foo v0.1.0 [..]
1097+
[RUNNING] `rustc [..] --check-cfg 'values()' [..]
1098+
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
1099+
",
1100+
)
1101+
.run();
1102+
}

tests/testsuite/test.rs

+106
Original file line numberDiff line numberDiff line change
@@ -4575,3 +4575,109 @@ fn check_cfg_features_doc() {
45754575
)
45764576
.run();
45774577
}
4578+
4579+
#[cfg_attr(windows, ignore)] // weird normalization issue with windows and cargo-test-support
4580+
#[cargo_test]
4581+
fn check_cfg_well_known_names() {
4582+
if !is_nightly() {
4583+
// --check-cfg is a nightly only rustc command line
4584+
return;
4585+
}
4586+
4587+
let p = project()
4588+
.file("Cargo.toml", &basic_manifest("foo", "0.1.0"))
4589+
.file("src/main.rs", "fn main() {}")
4590+
.build();
4591+
4592+
p.cargo("test -v -Z check-cfg-well-known-names")
4593+
.masquerade_as_nightly_cargo()
4594+
.with_stderr(
4595+
"\
4596+
[COMPILING] foo v0.1.0 [..]
4597+
[RUNNING] `rustc [..] --check-cfg 'names()' [..]
4598+
[FINISHED] test [unoptimized + debuginfo] target(s) in [..]
4599+
[RUNNING] [..]
4600+
",
4601+
)
4602+
.run();
4603+
}
4604+
4605+
#[cfg_attr(windows, ignore)] // weird normalization issue with windows and cargo-test-support
4606+
#[cargo_test]
4607+
fn check_cfg_well_known_values() {
4608+
if !is_nightly() {
4609+
// --check-cfg is a nightly only rustc command line
4610+
return;
4611+
}
4612+
4613+
let p = project()
4614+
.file("Cargo.toml", &basic_manifest("foo", "0.1.0"))
4615+
.file("src/main.rs", "fn main() {}")
4616+
.build();
4617+
4618+
p.cargo("test -v -Z check-cfg-well-known-values")
4619+
.masquerade_as_nightly_cargo()
4620+
.with_stderr(
4621+
"\
4622+
[COMPILING] foo v0.1.0 [..]
4623+
[RUNNING] `rustc [..] --check-cfg 'values()' [..]
4624+
[FINISHED] test [unoptimized + debuginfo] target(s) in [..]
4625+
[RUNNING] [..]
4626+
",
4627+
)
4628+
.run();
4629+
}
4630+
4631+
#[cfg_attr(windows, ignore)] // weird normalization issue with windows and cargo-test-support
4632+
#[cargo_test]
4633+
fn check_cfg_well_known_names_doc() {
4634+
if !is_nightly() {
4635+
// --check-cfg is a nightly only rustc command line
4636+
return;
4637+
}
4638+
4639+
let p = project()
4640+
.file("Cargo.toml", &basic_manifest("foo", "0.1.0"))
4641+
.file("src/lib.rs", "#[allow(dead_code)] fn foo() {}")
4642+
.build();
4643+
4644+
p.cargo("test -v --doc -Z check-cfg-well-known-names")
4645+
.masquerade_as_nightly_cargo()
4646+
.with_stderr(
4647+
"\
4648+
[COMPILING] foo v0.1.0 [..]
4649+
[RUNNING] `rustc [..] --check-cfg 'names()' [..]
4650+
[FINISHED] test [unoptimized + debuginfo] target(s) in [..]
4651+
[DOCTEST] foo
4652+
[RUNNING] `rustdoc [..] --check-cfg 'names()' [..]
4653+
",
4654+
)
4655+
.run();
4656+
}
4657+
4658+
#[cfg_attr(windows, ignore)] // weird normalization issue with windows and cargo-test-support
4659+
#[cargo_test]
4660+
fn check_cfg_well_known_values_doc() {
4661+
if !is_nightly() {
4662+
// --check-cfg is a nightly only rustc command line
4663+
return;
4664+
}
4665+
4666+
let p = project()
4667+
.file("Cargo.toml", &basic_manifest("foo", "0.1.0"))
4668+
.file("src/lib.rs", "#[allow(dead_code)] fn foo() {}")
4669+
.build();
4670+
4671+
p.cargo("test -v --doc -Z check-cfg-well-known-values")
4672+
.masquerade_as_nightly_cargo()
4673+
.with_stderr(
4674+
"\
4675+
[COMPILING] foo v0.1.0 [..]
4676+
[RUNNING] `rustc [..] --check-cfg 'values()' [..]
4677+
[FINISHED] test [unoptimized + debuginfo] target(s) in [..]
4678+
[DOCTEST] foo
4679+
[RUNNING] `rustdoc [..] --check-cfg 'values()' [..]
4680+
",
4681+
)
4682+
.run();
4683+
}

0 commit comments

Comments
 (0)