diff --git a/compiler/rustc_expand/src/mbe/macro_rules.rs b/compiler/rustc_expand/src/mbe/macro_rules.rs index f8491654f39e3..537a10e98e59d 100644 --- a/compiler/rustc_expand/src/mbe/macro_rules.rs +++ b/compiler/rustc_expand/src/mbe/macro_rules.rs @@ -1027,6 +1027,24 @@ fn check_matcher_core( ), ); err.span_label(sp, format!("not allowed after `{}` fragments", kind)); + + if kind == NonterminalKind::PatWithOr + && sess.edition == Edition::Edition2021 + && next_token.is_token(&BinOp(token::BinOpToken::Or)) + { + let suggestion = quoted_tt_to_string(&TokenTree::MetaVarDecl( + span, + name, + Some(NonterminalKind::PatParam { inferred: false }), + )); + err.span_suggestion( + span, + &format!("try a `pat_param` fragment specifier instead"), + suggestion, + Applicability::MaybeIncorrect, + ); + } + let msg = "allowed there are: "; match possible { &[] => {} diff --git a/src/test/ui/macros/macro-pat-pattern-followed-by-or-in-2021.stderr b/src/test/ui/macros/macro-pat-pattern-followed-by-or-in-2021.stderr index a5987a25551d2..a06487be3d601 100644 --- a/src/test/ui/macros/macro-pat-pattern-followed-by-or-in-2021.stderr +++ b/src/test/ui/macros/macro-pat-pattern-followed-by-or-in-2021.stderr @@ -2,7 +2,9 @@ error: `$x:pat` is followed by `|`, which is not allowed for `pat` fragments --> $DIR/macro-pat-pattern-followed-by-or-in-2021.rs:3:28 | LL | macro_rules! foo { ($x:pat | $y:pat) => {} } - | ^ not allowed after `pat` fragments + | ------ ^ not allowed after `pat` fragments + | | + | help: try a `pat_param` fragment specifier instead: `$x:pat_param` | = note: allowed there are: `=>`, `,`, `=`, `if` or `in` @@ -10,7 +12,9 @@ error: `$x:pat` is followed by `|`, which is not allowed for `pat` fragments --> $DIR/macro-pat-pattern-followed-by-or-in-2021.rs:4:32 | LL | macro_rules! bar { ($($x:pat)+ | $($y:pat)+) => {} } - | ^ not allowed after `pat` fragments + | ------ ^ not allowed after `pat` fragments + | | + | help: try a `pat_param` fragment specifier instead: `$x:pat_param` | = note: allowed there are: `=>`, `,`, `=`, `if` or `in` @@ -18,7 +22,9 @@ error: `$pat:pat` may be followed by `|`, which is not allowed for `pat` fragmen --> $DIR/macro-pat-pattern-followed-by-or-in-2021.rs:7:36 | LL | ( $expr:expr , $( $( $pat:pat )|+ => $expr_arm:expr ),+ ) => { - | ^ not allowed after `pat` fragments + | -------- ^ not allowed after `pat` fragments + | | + | help: try a `pat_param` fragment specifier instead: `$pat:pat_param` | = note: allowed there are: `=>`, `,`, `=`, `if` or `in` diff --git a/src/test/ui/macros/macro-pat2021-pattern-followed-by-or.stderr b/src/test/ui/macros/macro-pat2021-pattern-followed-by-or.stderr index 8aebe98515f4d..c3754dde080a3 100644 --- a/src/test/ui/macros/macro-pat2021-pattern-followed-by-or.stderr +++ b/src/test/ui/macros/macro-pat2021-pattern-followed-by-or.stderr @@ -2,7 +2,9 @@ error: `$x:pat` is followed by `|`, which is not allowed for `pat` fragments --> $DIR/macro-pat2021-pattern-followed-by-or.rs:4:28 | LL | macro_rules! foo { ($x:pat | $y:pat) => {} } - | ^ not allowed after `pat` fragments + | ------ ^ not allowed after `pat` fragments + | | + | help: try a `pat_param` fragment specifier instead: `$x:pat_param` | = note: allowed there are: `=>`, `,`, `=`, `if` or `in` @@ -10,7 +12,9 @@ error: `$x:pat` is followed by `|`, which is not allowed for `pat` fragments --> $DIR/macro-pat2021-pattern-followed-by-or.rs:7:28 | LL | macro_rules! ogg { ($x:pat | $y:pat_param) => {} } - | ^ not allowed after `pat` fragments + | ------ ^ not allowed after `pat` fragments + | | + | help: try a `pat_param` fragment specifier instead: `$x:pat_param` | = note: allowed there are: `=>`, `,`, `=`, `if` or `in` @@ -18,7 +22,9 @@ error: `$pat:pat` may be followed by `|`, which is not allowed for `pat` fragmen --> $DIR/macro-pat2021-pattern-followed-by-or.rs:9:35 | LL | ( $expr:expr , $( $( $pat:pat)|+ => $expr_arm:pat),+ ) => { - | ^ not allowed after `pat` fragments + | -------- ^ not allowed after `pat` fragments + | | + | help: try a `pat_param` fragment specifier instead: `$pat:pat_param` | = note: allowed there are: `=>`, `,`, `=`, `if` or `in`