Skip to content

Commit 080c704

Browse files
Rollup merge of #150840 - print-check-cfg-rework-output, r=nnethercote
Make `--print=check-cfg` output compatible `--check-cfg` arguments This PR changes significantly the output of the unstable `--print=check-cfg` option. Specifically it removes the ad-hoc resemblance with `--print=cfg` in order to output a simplified but still compatible `--check-cfg` arguments. The goal is to future proof the output of `--print=check-cfg` like `--check-cfg` is, and the best way to do that is to use it's syntax. This is particularly relevant for [RFC3905](rust-lang/rfcs#3905) which wants to introduce a new predicate: `version(...)`.
2 parents 66f25e9 + dd2b0a8 commit 080c704

File tree

4 files changed

+90
-58
lines changed

4 files changed

+90
-58
lines changed

‎compiler/rustc_driver_impl/src/lib.rs‎

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -765,30 +765,35 @@ fn print_crate_info(
765765
for (name, expected_values) in &sess.psess.check_config.expecteds {
766766
use crate::config::ExpectedValues;
767767
match expected_values {
768-
ExpectedValues::Any => check_cfgs.push(format!("{name}=any()")),
768+
ExpectedValues::Any => {
769+
check_cfgs.push(format!("cfg({name}, values(any()))"))
770+
}
769771
ExpectedValues::Some(values) => {
770-
if !values.is_empty() {
771-
check_cfgs.extend(values.iter().map(|value| {
772+
let mut values: Vec<_> = values
773+
.iter()
774+
.map(|value| {
772775
if let Some(value) = value {
773-
format!("{name}=\"{value}\"")
776+
format!("\"{value}\"")
774777
} else {
775-
name.to_string()
778+
"none()".to_string()
776779
}
777-
}))
778-
} else {
779-
check_cfgs.push(format!("{name}="))
780-
}
780+
})
781+
.collect();
782+
783+
values.sort_unstable();
784+
785+
let values = values.join(", ");
786+
787+
check_cfgs.push(format!("cfg({name}, values({values}))"))
781788
}
782789
}
783790
}
784791

785792
check_cfgs.sort_unstable();
786-
if !sess.psess.check_config.exhaustive_names {
787-
if !sess.psess.check_config.exhaustive_values {
788-
println_info!("any()=any()");
789-
} else {
790-
println_info!("any()");
791-
}
793+
if !sess.psess.check_config.exhaustive_names
794+
&& sess.psess.check_config.exhaustive_values
795+
{
796+
println_info!("cfg(any())");
792797
}
793798
for check_cfg in check_cfgs {
794799
println_info!("{check_cfg}");

‎src/doc/unstable-book/src/compiler-flags/print-check-cfg.md‎

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,28 @@ This option of the `--print` flag print the list of all the expected cfgs.
99
This is related to the [`--check-cfg` flag][check-cfg] which allows specifying arbitrary expected
1010
names and values.
1111

12-
This print option works similarly to `--print=cfg` (modulo check-cfg specifics).
13-
14-
| `--check-cfg` | `--print=check-cfg` |
15-
|-----------------------------------|-----------------------------|
16-
| `cfg(foo)` | `foo` |
17-
| `cfg(foo, values("bar"))` | `foo="bar"` |
18-
| `cfg(foo, values(none(), "bar"))` | `foo` & `foo="bar"` |
19-
| | *check-cfg specific syntax* |
20-
| `cfg(foo, values(any())` | `foo=any()` |
21-
| `cfg(foo, values())` | `foo=` |
22-
| `cfg(any())` | `any()` |
23-
| *none* | `any()=any()` |
12+
This print option outputs compatible `--check-cfg` arguments with a reduced syntax where all the
13+
expected values are on the same line and `values(...)` is always explicit.
14+
15+
| `--check-cfg` | `--print=check-cfg` |
16+
|-----------------------------------|-----------------------------------|
17+
| `cfg(foo)` | `cfg(foo, values(none())) |
18+
| `cfg(foo, values("bar"))` | `cfg(foo, values("bar"))` |
19+
| `cfg(foo, values(none(), "bar"))` | `cfg(foo, values(none(), "bar"))` |
20+
| `cfg(foo, values(any())` | `cfg(foo, values(any())` |
21+
| `cfg(foo, values())` | `cfg(foo, values())` |
22+
| `cfg(any())` | `cfg(any())` |
23+
| *nothing* | *nothing* |
24+
25+
The print option includes well known cfgs.
2426

2527
To be used like this:
2628

2729
```bash
2830
rustc --print=check-cfg -Zunstable-options lib.rs
2931
```
3032

33+
> **Note:** Users should be resilient when parsing, in particular against new predicates that
34+
may be added in the future.
35+
3136
[check-cfg]: https://doc.rust-lang.org/nightly/rustc/check-cfg.html

‎src/tools/compiletest/src/common.rs‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1070,11 +1070,27 @@ fn builtin_cfg_names(config: &Config) -> HashSet<String> {
10701070
Default::default(),
10711071
)
10721072
.lines()
1073-
.map(|l| if let Some((name, _)) = l.split_once('=') { name.to_string() } else { l.to_string() })
1073+
.map(|l| extract_cfg_name(&l).unwrap().to_string())
10741074
.chain(std::iter::once(String::from("test")))
10751075
.collect()
10761076
}
10771077

1078+
/// Extract the cfg name from `cfg(name, values(...))` lines
1079+
fn extract_cfg_name(check_cfg_line: &str) -> Result<&str, &'static str> {
1080+
let trimmed = check_cfg_line.trim();
1081+
1082+
#[rustfmt::skip]
1083+
let inner = trimmed
1084+
.strip_prefix("cfg(")
1085+
.ok_or("missing cfg(")?
1086+
.strip_suffix(")")
1087+
.ok_or("missing )")?;
1088+
1089+
let first_comma = inner.find(',').ok_or("no comma found")?;
1090+
1091+
Ok(inner[..first_comma].trim())
1092+
}
1093+
10781094
pub const KNOWN_CRATE_TYPES: &[&str] =
10791095
&["bin", "cdylib", "dylib", "lib", "proc-macro", "rlib", "staticlib"];
10801096

‎tests/run-make/print-check-cfg/rmake.rs‎

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,51 +14,55 @@ struct CheckCfg {
1414

1515
enum Contains {
1616
Some { contains: &'static [&'static str], doesnt_contain: &'static [&'static str] },
17-
Only(&'static str),
17+
Nothing,
1818
}
1919

2020
fn main() {
21-
check(CheckCfg { args: &[], contains: Contains::Only("any()=any()") });
21+
check(CheckCfg { args: &[], contains: Contains::Nothing });
2222
check(CheckCfg {
2323
args: &["--check-cfg=cfg()"],
2424
contains: Contains::Some {
25-
contains: &["unix", "miri"],
26-
doesnt_contain: &["any()", "any()=any()"],
25+
contains: &["cfg(unix, values(none()))", "cfg(miri, values(none()))"],
26+
doesnt_contain: &["cfg(any())"],
2727
},
2828
});
2929
check(CheckCfg {
3030
args: &["--check-cfg=cfg(any())"],
3131
contains: Contains::Some {
32-
contains: &["any()", "unix", r#"target_feature="crt-static""#],
32+
contains: &["cfg(any())", "cfg(unix, values(none()))"],
3333
doesnt_contain: &["any()=any()"],
3434
},
3535
});
3636
check(CheckCfg {
3737
args: &["--check-cfg=cfg(feature)"],
3838
contains: Contains::Some {
39-
contains: &["unix", "miri", "feature"],
40-
doesnt_contain: &["any()", "any()=any()", "feature=none()", "feature="],
39+
contains: &[
40+
"cfg(unix, values(none()))",
41+
"cfg(miri, values(none()))",
42+
"cfg(feature, values(none()))",
43+
],
44+
doesnt_contain: &["cfg(any())", "cfg(feature)"],
4145
},
4246
});
4347
check(CheckCfg {
4448
args: &[r#"--check-cfg=cfg(feature, values(none(), "", "test", "lol"))"#],
4549
contains: Contains::Some {
46-
contains: &["feature", "feature=\"\"", "feature=\"test\"", "feature=\"lol\""],
47-
doesnt_contain: &["any()", "any()=any()", "feature=none()", "feature="],
50+
contains: &[r#"cfg(feature, values("", "lol", "test", none()))"#],
51+
doesnt_contain: &["cfg(any())", "cfg(feature, values(none()))", "cfg(feature)"],
4852
},
4953
});
5054
check(CheckCfg {
5155
args: &["--check-cfg=cfg(feature, values())"],
5256
contains: Contains::Some {
53-
contains: &["feature="],
54-
doesnt_contain: &["any()", "any()=any()", "feature=none()", "feature"],
57+
contains: &["cfg(feature, values())"],
58+
doesnt_contain: &["cfg(any())", "cfg(feature, values(none()))", "cfg(feature)"],
5559
},
5660
});
5761
check(CheckCfg {
5862
args: &["--check-cfg=cfg(feature, values())", "--check-cfg=cfg(feature, values(none()))"],
5963
contains: Contains::Some {
60-
contains: &["feature"],
61-
doesnt_contain: &["any()", "any()=any()", "feature=none()", "feature="],
64+
contains: &["cfg(feature, values(none()))"],
65+
doesnt_contain: &["cfg(any())", "cfg(feature, values())"],
6266
},
6367
});
6468
check(CheckCfg {
@@ -67,8 +71,8 @@ fn main() {
6771
r#"--check-cfg=cfg(feature, values("tmp"))"#,
6872
],
6973
contains: Contains::Some {
70-
contains: &["unix", "miri", "feature=any()"],
71-
doesnt_contain: &["any()", "any()=any()", "feature", "feature=", "feature=\"tmp\""],
74+
contains: &["cfg(feature, values(any()))"],
75+
doesnt_contain: &["cfg(any())", r#"cfg(feature, values("tmp"))"#],
7276
},
7377
});
7478
check(CheckCfg {
@@ -78,8 +82,12 @@ fn main() {
7882
r#"--check-cfg=cfg(feature, values("tmp"))"#,
7983
],
8084
contains: Contains::Some {
81-
contains: &["has_foo", "has_bar", "feature=\"tmp\""],
82-
doesnt_contain: &["any()", "any()=any()", "feature"],
85+
contains: &[
86+
"cfg(has_foo, values(none()))",
87+
"cfg(has_bar, values(none()))",
88+
r#"cfg(feature, values("tmp"))"#,
89+
],
90+
doesnt_contain: &["cfg(any())", "cfg(feature)"],
8391
},
8492
});
8593
}
@@ -94,16 +102,15 @@ fn check(CheckCfg { args, contains }: CheckCfg) {
94102

95103
for l in stdout.lines() {
96104
assert!(l == l.trim());
97-
if let Some((left, right)) = l.split_once('=') {
98-
if right != "any()" && right != "" {
99-
assert!(right.starts_with("\""));
100-
assert!(right.ends_with("\""));
101-
}
102-
assert!(!left.contains("\""));
103-
} else {
104-
assert!(!l.contains("\""));
105-
}
106-
assert!(found.insert(l.to_string()), "{}", &l);
105+
assert!(l.starts_with("cfg("), "{l}");
106+
assert!(l.ends_with(")"), "{l}");
107+
assert_eq!(
108+
l.chars().filter(|c| *c == '(').count(),
109+
l.chars().filter(|c| *c == ')').count(),
110+
"{l}"
111+
);
112+
assert!(l.chars().filter(|c| *c == '"').count() % 2 == 0, "{l}");
113+
assert!(found.insert(l.to_string()), "{l}");
107114
}
108115

109116
match contains {
@@ -131,9 +138,8 @@ fn check(CheckCfg { args, contains }: CheckCfg) {
131138
);
132139
}
133140
}
134-
Contains::Only(only) => {
135-
assert!(found.contains(&only.to_string()), "{:?} != {:?}", &only, &found);
136-
assert!(found.len() == 1, "len: {}, instead of 1", found.len());
141+
Contains::Nothing => {
142+
assert!(found.len() == 0, "len: {}, instead of 0", found.len());
137143
}
138144
}
139145
}

0 commit comments

Comments
 (0)