make cfg parser suggest any or all on #[cfg(a, b)]

This commit is contained in:
Sasha Pourcelot
2026-03-29 10:32:11 +00:00
parent 3bde2a7165
commit 4e019ac160
5 changed files with 142 additions and 13 deletions
@@ -48,8 +48,37 @@ pub fn parse_cfg<S: Stage>(
cx.adcx().expected_list(attr_span, args);
return None;
};
let Some(single) = list.single() else {
cx.adcx().expected_single_argument(list.span);
let target = cx.target;
let mut adcx = cx.adcx();
if list.is_empty() {
// `#[cfg()]`
let message = format!("if the {target} should be disabled, use `#[cfg(false)]`");
adcx.push_suggestion(message, list.span, "(false)".to_string());
} else {
// `#[cfg(foo, bar)]`
if let Ok(args) = adcx
.sess()
.source_map()
.span_to_source(list.span, |src, start, end| Ok(src[start..end].to_string()))
{
let all = format!("(all{args})");
let any = format!("(any{args})");
let all_msg = format!(
"if the {target} should be enabled when all these predicates are, wrap them in `all`"
);
let any_msg = format!(
"alternately, if the {target} should be enabled when any of these predicates are, wrap them in `any`"
);
adcx.push_suggestion(all_msg, list.span, all);
adcx.push_suggestion(any_msg, list.span, any);
}
}
adcx.expected_single_argument(list.span);
return None;
};
parse_cfg_entry(cx, single).ok()
+32
View File
@@ -5,6 +5,11 @@ LL | #[doc(cfg(), cfg(foo, bar))]
| ^^^^^^^^^--^^^^^^^^^^^^^^^^^
| |
| expected a single argument here
|
help: if the function should be disabled, use `#[cfg(false)]`
|
LL | #[doc(cfg(false), cfg(foo, bar))]
| +++++
error[E0805]: malformed `doc` attribute input
--> $DIR/doc-cfg.rs:4:1
@@ -13,6 +18,17 @@ LL | #[doc(cfg(), cfg(foo, bar))]
| ^^^^^^^^^^^^^^^^----------^^
| |
| expected a single argument here
|
help: if the function should be enabled when all these predicates are, wrap them in `all`
|
LL - #[doc(cfg(), cfg(foo, bar))]
LL + #[doc(cfg(), cfg(all(foo, bar)))]
|
help: alternately, if the function should be enabled when any of these predicates are, wrap them in `any`
|
LL - #[doc(cfg(), cfg(foo, bar))]
LL + #[doc(cfg(), cfg(any(foo, bar)))]
|
error[E0805]: malformed `doc` attribute input
--> $DIR/doc-cfg.rs:7:1
@@ -21,6 +37,11 @@ LL | #[doc(cfg())]
| ^^^^^^^^^--^^
| |
| expected a single argument here
|
help: if the function should be disabled, use `#[cfg(false)]`
|
LL | #[doc(cfg(false))]
| +++++
error[E0805]: malformed `doc` attribute input
--> $DIR/doc-cfg.rs:8:1
@@ -29,6 +50,17 @@ LL | #[doc(cfg(foo, bar))]
| ^^^^^^^^^----------^^
| |
| expected a single argument here
|
help: if the function should be enabled when all these predicates are, wrap them in `all`
|
LL - #[doc(cfg(foo, bar))]
LL + #[doc(cfg(all(foo, bar)))]
|
help: alternately, if the function should be enabled when any of these predicates are, wrap them in `any`
|
LL - #[doc(cfg(foo, bar))]
LL + #[doc(cfg(any(foo, bar)))]
|
error: aborting due to 4 previous errors
+44
View File
@@ -13,6 +13,17 @@ LL | #[doc(cfg(x, y))]
| ^^^^^^^^^------^^
| |
| expected a single argument here
|
help: if the struct should be enabled when all these predicates are, wrap them in `all`
|
LL - #[doc(cfg(x, y))]
LL + #[doc(cfg(all(x, y)))]
|
help: alternately, if the struct should be enabled when any of these predicates are, wrap them in `any`
|
LL - #[doc(cfg(x, y))]
LL + #[doc(cfg(any(x, y)))]
|
error[E0539]: malformed `doc` attribute input
--> $DIR/invalid-cfg.rs:7:1
@@ -29,6 +40,17 @@ LL | #[doc(cfg(x, y))]
| ^^^^^^^^^------^^
| |
| expected a single argument here
|
help: if the struct should be enabled when all these predicates are, wrap them in `all`
|
LL - #[doc(cfg(x, y))]
LL + #[doc(cfg(all(x, y)))]
|
help: alternately, if the struct should be enabled when any of these predicates are, wrap them in `any`
|
LL - #[doc(cfg(x, y))]
LL + #[doc(cfg(any(x, y)))]
|
error[E0539]: malformed `doc` attribute input
--> $DIR/invalid-cfg.rs:12:1
@@ -45,6 +67,17 @@ LL | #[doc(cfg(x, y))]
| ^^^^^^^^^------^^
| |
| expected a single argument here
|
help: if the struct should be enabled when all these predicates are, wrap them in `all`
|
LL - #[doc(cfg(x, y))]
LL + #[doc(cfg(all(x, y)))]
|
help: alternately, if the struct should be enabled when any of these predicates are, wrap them in `any`
|
LL - #[doc(cfg(x, y))]
LL + #[doc(cfg(any(x, y)))]
|
error[E0539]: malformed `doc` attribute input
--> $DIR/invalid-cfg.rs:18:1
@@ -61,6 +94,17 @@ LL | #[doc(cfg(x, y))]
| ^^^^^^^^^------^^
| |
| expected a single argument here
|
help: if the struct should be enabled when all these predicates are, wrap them in `all`
|
LL - #[doc(cfg(x, y))]
LL + #[doc(cfg(all(x, y)))]
|
help: alternately, if the struct should be enabled when any of these predicates are, wrap them in `any`
|
LL - #[doc(cfg(x, y))]
LL + #[doc(cfg(any(x, y)))]
|
error: aborting due to 8 previous errors
+18 -6
View File
@@ -3,22 +3,34 @@ error[E0805]: malformed `cfg` attribute input
|
LL | #[cfg(foo, bar)]
| ^^^^^----------^
| | |
| | expected a single argument here
| help: must be of the form: `#[cfg(predicate)]`
| |
| expected a single argument here
|
= note: for more information, visit <https://doc.rust-lang.org/reference/conditional-compilation.html#the-cfg-attribute>
help: if the crate should be enabled when all these predicates are, wrap them in `all`
|
LL - #[cfg(foo, bar)]
LL + #[cfg(all(foo, bar))]
|
help: alternately, if the crate should be enabled when any of these predicates are, wrap them in `any`
|
LL - #[cfg(foo, bar)]
LL + #[cfg(any(foo, bar))]
|
error[E0805]: malformed `cfg` attribute input
--> $DIR/suggest-any-or-all.rs:5:1
|
LL | #[cfg()]
| ^^^^^--^
| | |
| | expected a single argument here
| help: must be of the form: `#[cfg(predicate)]`
| |
| expected a single argument here
|
= note: for more information, visit <https://doc.rust-lang.org/reference/conditional-compilation.html#the-cfg-attribute>
help: if the crate should be disabled, use `#[cfg(false)]`
|
LL | #[cfg(false)]
| +++++
error: aborting due to 2 previous errors
@@ -25,22 +25,34 @@ error[E0805]: malformed `cfg` attribute input
|
LL | #[cfg()]
| ^^^^^--^
| | |
| | expected a single argument here
| help: must be of the form: `#[cfg(predicate)]`
| |
| expected a single argument here
|
= note: for more information, visit <https://doc.rust-lang.org/reference/conditional-compilation.html#the-cfg-attribute>
help: if the crate should be disabled, use `#[cfg(false)]`
|
LL | #[cfg(false)]
| +++++
error[E0805]: malformed `cfg` attribute input
--> $DIR/cfg-attr-syntax-validation.rs:19:1
|
LL | #[cfg(a, b)]
| ^^^^^------^
| | |
| | expected a single argument here
| help: must be of the form: `#[cfg(predicate)]`
| |
| expected a single argument here
|
= note: for more information, visit <https://doc.rust-lang.org/reference/conditional-compilation.html#the-cfg-attribute>
help: if the crate should be enabled when all these predicates are, wrap them in `all`
|
LL - #[cfg(a, b)]
LL + #[cfg(all(a, b))]
|
help: alternately, if the crate should be enabled when any of these predicates are, wrap them in `any`
|
LL - #[cfg(a, b)]
LL + #[cfg(any(a, b))]
|
error[E0539]: malformed `cfg` attribute input
--> $DIR/cfg-attr-syntax-validation.rs:25:1