Closed
Description
Take the following code:
macro_rules! foo {
($p:expr) => {
if let $p = Some(42) {
return;
}
};
}
fn main() {
foo!(Some(3));
}
Currently it fails with:
error: arbitrary expressions aren't allowed in patterns
--> src/main.rs:10:10
|
10 | foo!(Some(3));
| ^^^^^^^
If you expanded the macro manually, Some(3)
would be parsed as a pattern, but the $p:expr
above changes it to be parsed as an expression. So what you need to do here is to change the $p:expr
above to $p:pat
. But this is very non-obvious.
Ideally, the compiler would try to check if the input is a valid pattern, and if so, add a note like "Some(3)
is a valid pattern but the metavariable $p is specified to be an expression", and maybe also point to the metavariable's matcher.
@rustbot label A-macros