Skip to content

Commit 832f1e9

Browse files
committed
make cfg parser suggest any or all on #[cfg(a, b)]
1 parent 9497813 commit 832f1e9

5 files changed

Lines changed: 142 additions & 13 deletions

File tree

compiler/rustc_attr_parsing/src/attributes/cfg.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,37 @@ pub fn parse_cfg<S: Stage>(
4848
cx.adcx().expected_list(attr_span, args);
4949
return None;
5050
};
51+
5152
let Some(single) = list.single() else {
52-
cx.adcx().expected_single_argument(list.span);
53+
let target = cx.target;
54+
let mut adcx = cx.adcx();
55+
if list.is_empty() {
56+
// `#[cfg()]`
57+
let message = format!("if the {target} should be disabled, use `#[cfg(false)]`");
58+
adcx.push_suggestion(message, list.span, "(false)".to_string());
59+
} else {
60+
// `#[cfg(foo, bar)]`
61+
if let Ok(args) = adcx
62+
.sess()
63+
.source_map()
64+
.span_to_source(list.span, |src, start, end| Ok(src[start..end].to_string()))
65+
{
66+
let all = format!("(all{args})");
67+
let any = format!("(any{args})");
68+
69+
let all_msg = format!(
70+
"if the {target} should be enabled when all these predicates are, wrap them in `all`"
71+
);
72+
let any_msg = format!(
73+
"alternately, if the {target} should be enabled when any these predicates are, wrap them in `any`"
74+
);
75+
76+
adcx.push_suggestion(all_msg, list.span, all);
77+
adcx.push_suggestion(any_msg, list.span, any);
78+
}
79+
}
80+
81+
adcx.expected_single_argument(list.span);
5382
return None;
5483
};
5584
parse_cfg_entry(cx, single).ok()

tests/rustdoc-ui/doc-cfg.stderr

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ LL | #[doc(cfg(), cfg(foo, bar))]
55
| ^^^^^^^^^--^^^^^^^^^^^^^^^^^
66
| |
77
| expected a single argument here
8+
|
9+
help: if the function should be disabled, use `#[cfg(false)]`
10+
|
11+
LL | #[doc(cfg(false), cfg(foo, bar))]
12+
| +++++
813

914
error[E0805]: malformed `doc` attribute input
1015
--> $DIR/doc-cfg.rs:4:1
@@ -13,6 +18,17 @@ LL | #[doc(cfg(), cfg(foo, bar))]
1318
| ^^^^^^^^^^^^^^^^----------^^
1419
| |
1520
| expected a single argument here
21+
|
22+
help: if the function should be enabled when all these predicates are, wrap them in `all`
23+
|
24+
LL - #[doc(cfg(), cfg(foo, bar))]
25+
LL + #[doc(cfg(), cfg(all(foo, bar)))]
26+
|
27+
help: alternately, if the function should be enabled when any these predicates are, wrap them in `any`
28+
|
29+
LL - #[doc(cfg(), cfg(foo, bar))]
30+
LL + #[doc(cfg(), cfg(any(foo, bar)))]
31+
|
1632

1733
error[E0805]: malformed `doc` attribute input
1834
--> $DIR/doc-cfg.rs:7:1
@@ -21,6 +37,11 @@ LL | #[doc(cfg())]
2137
| ^^^^^^^^^--^^
2238
| |
2339
| expected a single argument here
40+
|
41+
help: if the function should be disabled, use `#[cfg(false)]`
42+
|
43+
LL | #[doc(cfg(false))]
44+
| +++++
2445

2546
error[E0805]: malformed `doc` attribute input
2647
--> $DIR/doc-cfg.rs:8:1
@@ -29,6 +50,17 @@ LL | #[doc(cfg(foo, bar))]
2950
| ^^^^^^^^^----------^^
3051
| |
3152
| expected a single argument here
53+
|
54+
help: if the function should be enabled when all these predicates are, wrap them in `all`
55+
|
56+
LL - #[doc(cfg(foo, bar))]
57+
LL + #[doc(cfg(all(foo, bar)))]
58+
|
59+
help: alternately, if the function should be enabled when any these predicates are, wrap them in `any`
60+
|
61+
LL - #[doc(cfg(foo, bar))]
62+
LL + #[doc(cfg(any(foo, bar)))]
63+
|
3264

3365
error: aborting due to 4 previous errors
3466

tests/rustdoc-ui/invalid-cfg.stderr

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,17 @@ LL | #[doc(cfg(x, y))]
1313
| ^^^^^^^^^------^^
1414
| |
1515
| expected a single argument here
16+
|
17+
help: if the struct should be enabled when all these predicates are, wrap them in `all`
18+
|
19+
LL - #[doc(cfg(x, y))]
20+
LL + #[doc(cfg(all(x, y)))]
21+
|
22+
help: alternately, if the struct should be enabled when any these predicates are, wrap them in `any`
23+
|
24+
LL - #[doc(cfg(x, y))]
25+
LL + #[doc(cfg(any(x, y)))]
26+
|
1627

1728
error[E0539]: malformed `doc` attribute input
1829
--> $DIR/invalid-cfg.rs:7:1
@@ -29,6 +40,17 @@ LL | #[doc(cfg(x, y))]
2940
| ^^^^^^^^^------^^
3041
| |
3142
| expected a single argument here
43+
|
44+
help: if the struct should be enabled when all these predicates are, wrap them in `all`
45+
|
46+
LL - #[doc(cfg(x, y))]
47+
LL + #[doc(cfg(all(x, y)))]
48+
|
49+
help: alternately, if the struct should be enabled when any these predicates are, wrap them in `any`
50+
|
51+
LL - #[doc(cfg(x, y))]
52+
LL + #[doc(cfg(any(x, y)))]
53+
|
3254

3355
error[E0539]: malformed `doc` attribute input
3456
--> $DIR/invalid-cfg.rs:12:1
@@ -45,6 +67,17 @@ LL | #[doc(cfg(x, y))]
4567
| ^^^^^^^^^------^^
4668
| |
4769
| expected a single argument here
70+
|
71+
help: if the struct should be enabled when all these predicates are, wrap them in `all`
72+
|
73+
LL - #[doc(cfg(x, y))]
74+
LL + #[doc(cfg(all(x, y)))]
75+
|
76+
help: alternately, if the struct should be enabled when any these predicates are, wrap them in `any`
77+
|
78+
LL - #[doc(cfg(x, y))]
79+
LL + #[doc(cfg(any(x, y)))]
80+
|
4881

4982
error[E0539]: malformed `doc` attribute input
5083
--> $DIR/invalid-cfg.rs:18:1
@@ -61,6 +94,17 @@ LL | #[doc(cfg(x, y))]
6194
| ^^^^^^^^^------^^
6295
| |
6396
| expected a single argument here
97+
|
98+
help: if the struct should be enabled when all these predicates are, wrap them in `all`
99+
|
100+
LL - #[doc(cfg(x, y))]
101+
LL + #[doc(cfg(all(x, y)))]
102+
|
103+
help: alternately, if the struct should be enabled when any these predicates are, wrap them in `any`
104+
|
105+
LL - #[doc(cfg(x, y))]
106+
LL + #[doc(cfg(any(x, y)))]
107+
|
64108

65109
error: aborting due to 8 previous errors
66110

tests/ui/cfg/suggest-any-or-all.stderr

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,34 @@ error[E0805]: malformed `cfg` attribute input
33
|
44
LL | #[cfg(foo, bar)]
55
| ^^^^^----------^
6-
| | |
7-
| | expected a single argument here
8-
| help: must be of the form: `#[cfg(predicate)]`
6+
| |
7+
| expected a single argument here
98
|
109
= note: for more information, visit <https://doc.rust-lang.org/reference/conditional-compilation.html#the-cfg-attribute>
10+
help: if the crate should be enabled when all these predicates are, wrap them in `all`
11+
|
12+
LL - #[cfg(foo, bar)]
13+
LL + #[cfg(all(foo, bar))]
14+
|
15+
help: alternately, if the crate should be enabled when any these predicates are, wrap them in `any`
16+
|
17+
LL - #[cfg(foo, bar)]
18+
LL + #[cfg(any(foo, bar))]
19+
|
1120

1221
error[E0805]: malformed `cfg` attribute input
1322
--> $DIR/suggest-any-or-all.rs:5:1
1423
|
1524
LL | #[cfg()]
1625
| ^^^^^--^
17-
| | |
18-
| | expected a single argument here
19-
| help: must be of the form: `#[cfg(predicate)]`
26+
| |
27+
| expected a single argument here
2028
|
2129
= note: for more information, visit <https://doc.rust-lang.org/reference/conditional-compilation.html#the-cfg-attribute>
30+
help: if the crate should be disabled, use `#[cfg(false)]`
31+
|
32+
LL | #[cfg(false)]
33+
| +++++
2234

2335
error: aborting due to 2 previous errors
2436

tests/ui/conditional-compilation/cfg-attr-syntax-validation.stderr

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,34 @@ error[E0805]: malformed `cfg` attribute input
2525
|
2626
LL | #[cfg()]
2727
| ^^^^^--^
28-
| | |
29-
| | expected a single argument here
30-
| help: must be of the form: `#[cfg(predicate)]`
28+
| |
29+
| expected a single argument here
3130
|
3231
= note: for more information, visit <https://doc.rust-lang.org/reference/conditional-compilation.html#the-cfg-attribute>
32+
help: if the crate should be disabled, use `#[cfg(false)]`
33+
|
34+
LL | #[cfg(false)]
35+
| +++++
3336

3437
error[E0805]: malformed `cfg` attribute input
3538
--> $DIR/cfg-attr-syntax-validation.rs:19:1
3639
|
3740
LL | #[cfg(a, b)]
3841
| ^^^^^------^
39-
| | |
40-
| | expected a single argument here
41-
| help: must be of the form: `#[cfg(predicate)]`
42+
| |
43+
| expected a single argument here
4244
|
4345
= note: for more information, visit <https://doc.rust-lang.org/reference/conditional-compilation.html#the-cfg-attribute>
46+
help: if the crate should be enabled when all these predicates are, wrap them in `all`
47+
|
48+
LL - #[cfg(a, b)]
49+
LL + #[cfg(all(a, b))]
50+
|
51+
help: alternately, if the crate should be enabled when any these predicates are, wrap them in `any`
52+
|
53+
LL - #[cfg(a, b)]
54+
LL + #[cfg(any(a, b))]
55+
|
4456

4557
error[E0539]: malformed `cfg` attribute input
4658
--> $DIR/cfg-attr-syntax-validation.rs:25:1

0 commit comments

Comments
 (0)