Skip to content

Commit 17052c7

Browse files
committed
make cfg parser suggest any or all on #[cfg(a, b)]
1 parent c8f151a commit 17052c7

3 files changed

Lines changed: 60 additions & 10 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 {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/ui/cfg/suggest-any-or-all.stderr

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,20 @@ 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: aborting due to 1 previous error
1322

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 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)