Skip to content

Commit 2b64476

Browse files
authored
Rollup merge of rust-lang#91385 - ecstatic-morse:pat-param-spec-suggest, r=estebank
Suggest the `pat_param` specifier before `|` on 2021 edition Ran into this today after writing some Rust for the first time in a while. r? `@estebank`
2 parents 420ddd0 + bfd95e1 commit 2b64476

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed

compiler/rustc_expand/src/mbe/macro_rules.rs

+18
Original file line numberDiff line numberDiff line change
@@ -1027,6 +1027,24 @@ fn check_matcher_core(
10271027
),
10281028
);
10291029
err.span_label(sp, format!("not allowed after `{}` fragments", kind));
1030+
1031+
if kind == NonterminalKind::PatWithOr
1032+
&& sess.edition == Edition::Edition2021
1033+
&& next_token.is_token(&BinOp(token::BinOpToken::Or))
1034+
{
1035+
let suggestion = quoted_tt_to_string(&TokenTree::MetaVarDecl(
1036+
span,
1037+
name,
1038+
Some(NonterminalKind::PatParam { inferred: false }),
1039+
));
1040+
err.span_suggestion(
1041+
span,
1042+
&format!("try a `pat_param` fragment specifier instead"),
1043+
suggestion,
1044+
Applicability::MaybeIncorrect,
1045+
);
1046+
}
1047+
10301048
let msg = "allowed there are: ";
10311049
match possible {
10321050
&[] => {}

src/test/ui/macros/macro-pat-pattern-followed-by-or-in-2021.stderr

+9-3
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,29 @@ error: `$x:pat` is followed by `|`, which is not allowed for `pat` fragments
22
--> $DIR/macro-pat-pattern-followed-by-or-in-2021.rs:3:28
33
|
44
LL | macro_rules! foo { ($x:pat | $y:pat) => {} }
5-
| ^ not allowed after `pat` fragments
5+
| ------ ^ not allowed after `pat` fragments
6+
| |
7+
| help: try a `pat_param` fragment specifier instead: `$x:pat_param`
68
|
79
= note: allowed there are: `=>`, `,`, `=`, `if` or `in`
810

911
error: `$x:pat` is followed by `|`, which is not allowed for `pat` fragments
1012
--> $DIR/macro-pat-pattern-followed-by-or-in-2021.rs:4:32
1113
|
1214
LL | macro_rules! bar { ($($x:pat)+ | $($y:pat)+) => {} }
13-
| ^ not allowed after `pat` fragments
15+
| ------ ^ not allowed after `pat` fragments
16+
| |
17+
| help: try a `pat_param` fragment specifier instead: `$x:pat_param`
1418
|
1519
= note: allowed there are: `=>`, `,`, `=`, `if` or `in`
1620

1721
error: `$pat:pat` may be followed by `|`, which is not allowed for `pat` fragments
1822
--> $DIR/macro-pat-pattern-followed-by-or-in-2021.rs:7:36
1923
|
2024
LL | ( $expr:expr , $( $( $pat:pat )|+ => $expr_arm:expr ),+ ) => {
21-
| ^ not allowed after `pat` fragments
25+
| -------- ^ not allowed after `pat` fragments
26+
| |
27+
| help: try a `pat_param` fragment specifier instead: `$pat:pat_param`
2228
|
2329
= note: allowed there are: `=>`, `,`, `=`, `if` or `in`
2430

src/test/ui/macros/macro-pat2021-pattern-followed-by-or.stderr

+9-3
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,29 @@ error: `$x:pat` is followed by `|`, which is not allowed for `pat` fragments
22
--> $DIR/macro-pat2021-pattern-followed-by-or.rs:4:28
33
|
44
LL | macro_rules! foo { ($x:pat | $y:pat) => {} }
5-
| ^ not allowed after `pat` fragments
5+
| ------ ^ not allowed after `pat` fragments
6+
| |
7+
| help: try a `pat_param` fragment specifier instead: `$x:pat_param`
68
|
79
= note: allowed there are: `=>`, `,`, `=`, `if` or `in`
810

911
error: `$x:pat` is followed by `|`, which is not allowed for `pat` fragments
1012
--> $DIR/macro-pat2021-pattern-followed-by-or.rs:7:28
1113
|
1214
LL | macro_rules! ogg { ($x:pat | $y:pat_param) => {} }
13-
| ^ not allowed after `pat` fragments
15+
| ------ ^ not allowed after `pat` fragments
16+
| |
17+
| help: try a `pat_param` fragment specifier instead: `$x:pat_param`
1418
|
1519
= note: allowed there are: `=>`, `,`, `=`, `if` or `in`
1620

1721
error: `$pat:pat` may be followed by `|`, which is not allowed for `pat` fragments
1822
--> $DIR/macro-pat2021-pattern-followed-by-or.rs:9:35
1923
|
2024
LL | ( $expr:expr , $( $( $pat:pat)|+ => $expr_arm:pat),+ ) => {
21-
| ^ not allowed after `pat` fragments
25+
| -------- ^ not allowed after `pat` fragments
26+
| |
27+
| help: try a `pat_param` fragment specifier instead: `$pat:pat_param`
2228
|
2329
= note: allowed there are: `=>`, `,`, `=`, `if` or `in`
2430

0 commit comments

Comments
 (0)