Skip to content

Commit b6e3630

Browse files
committed
parse guard patterns and use correct pattern parsers throughout rustc
1 parent f1117d1 commit b6e3630

15 files changed

+45
-38
lines changed

compiler/rustc_expand/src/expand.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -987,7 +987,7 @@ pub fn parse_ast_fragment<'a>(
987987
}
988988
}
989989
AstFragmentKind::Ty => AstFragment::Ty(this.parse_ty()?),
990-
AstFragmentKind::Pat => AstFragment::Pat(this.parse_pat_no_top_guard(
990+
AstFragmentKind::Pat => AstFragment::Pat(this.parse_pat_allow_top_guard(
991991
None,
992992
RecoverComma::No,
993993
RecoverColon::Yes,

compiler/rustc_parse/src/parser/expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2774,7 +2774,7 @@ impl<'a> Parser<'a> {
27742774
};
27752775
// Try to parse the pattern `for ($PAT) in $EXPR`.
27762776
let pat = match (
2777-
self.parse_pat_no_top_guard(
2777+
self.parse_pat_allow_top_guard(
27782778
None,
27792779
RecoverComma::Yes,
27802780
RecoverColon::Yes,

compiler/rustc_parse/src/parser/pat.rs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,19 @@ impl<'a> Parser<'a> {
9999
/// `PatternNoTopAlt` (see below) are used.
100100
pub fn parse_pat_allow_top_guard(
101101
&mut self,
102-
_expected: Option<Expected>,
103-
_rc: RecoverComma,
104-
_ra: RecoverColon,
105-
_rt: CommaRecoveryMode,
102+
expected: Option<Expected>,
103+
rc: RecoverComma,
104+
ra: RecoverColon,
105+
rt: CommaRecoveryMode,
106106
) -> PResult<'a, P<Pat>> {
107-
todo!()
107+
let pat = self.parse_pat_no_top_guard(expected, rc, ra, rt)?;
108+
109+
if self.eat_keyword(kw::If) {
110+
let cond = self.parse_expr()?;
111+
Ok(self.mk_pat(pat.span.to(cond.span), PatKind::Guard(pat, cond)))
112+
} else {
113+
Ok(pat)
114+
}
108115
}
109116

110117
/// Parses a pattern.
@@ -123,8 +130,8 @@ impl<'a> Parser<'a> {
123130
/// Parses a pattern.
124131
///
125132
/// Corresponds to `PatternNoTopGuard` in RFC 3637 and allows or-patterns, but not
126-
/// guard patterns, at the top level. Used for parsing patterns in `pat` fragments and
127-
/// `let`, `if let`, and `while let` expressions.
133+
/// guard patterns, at the top level. Used for parsing patterns in `pat` fragments (until
134+
/// the next edition) and `let`, `if let`, and `while let` expressions.
128135
///
129136
/// Note that after the FCP in <https://github.com/rust-lang/rust/issues/81415>,
130137
/// a leading vert is allowed in nested or-patterns, too. This allows us to
@@ -473,7 +480,7 @@ impl<'a> Parser<'a> {
473480
} else if self.check(&token::OpenDelim(Delimiter::Bracket)) {
474481
// Parse `[pat, pat,...]` as a slice pattern.
475482
let (pats, _) = self.parse_delim_comma_seq(Delimiter::Bracket, |p| {
476-
p.parse_pat_no_top_guard(
483+
p.parse_pat_allow_top_guard(
477484
None,
478485
RecoverComma::No,
479486
RecoverColon::No,
@@ -721,7 +728,7 @@ impl<'a> Parser<'a> {
721728
let open_paren = self.token.span;
722729

723730
let (fields, trailing_comma) = self.parse_paren_comma_seq(|p| {
724-
p.parse_pat_no_top_guard(
731+
p.parse_pat_allow_top_guard(
725732
None,
726733
RecoverComma::No,
727734
RecoverColon::No,
@@ -1130,7 +1137,7 @@ impl<'a> Parser<'a> {
11301137
path: Path,
11311138
) -> PResult<'a, PatKind> {
11321139
let (fields, _) = self.parse_paren_comma_seq(|p| {
1133-
p.parse_pat_no_top_guard(
1140+
p.parse_pat_allow_top_guard(
11341141
None,
11351142
RecoverComma::No,
11361143
RecoverColon::No,
@@ -1165,7 +1172,7 @@ impl<'a> Parser<'a> {
11651172
self.parse_builtin(|self_, _lo, ident| {
11661173
Ok(match ident.name {
11671174
// builtin#deref(PAT)
1168-
sym::deref => Some(ast::PatKind::Deref(self_.parse_pat_no_top_guard(
1175+
sym::deref => Some(ast::PatKind::Deref(self_.parse_pat_allow_top_guard(
11691176
None,
11701177
RecoverComma::Yes,
11711178
RecoverColon::Yes,
@@ -1415,7 +1422,7 @@ impl<'a> Parser<'a> {
14151422
// Parsing a pattern of the form `fieldname: pat`.
14161423
let fieldname = self.parse_field_name()?;
14171424
self.bump();
1418-
let pat = self.parse_pat_no_top_guard(
1425+
let pat = self.parse_pat_allow_top_guard(
14191426
None,
14201427
RecoverComma::No,
14211428
RecoverColon::No,

compiler/rustc_parse/src/parser/path.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ impl<'a> Parser<'a> {
454454
PathStyle::Pat
455455
if let Ok(_) = self
456456
.parse_paren_comma_seq(|p| {
457-
p.parse_pat_no_top_guard(
457+
p.parse_pat_allow_top_guard(
458458
None,
459459
RecoverComma::No,
460460
RecoverColon::No,

tests/ui/parser/issues/issue-72373.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ fn foo(c: &[u32], n: u32) -> u32 {
33
[h, ..] if h > n => 0,
44
[h, ..] if h == n => 1,
55
[h, ref ts..] => foo(c, n - h) + foo(ts, n),
6-
//~^ ERROR expected one of `,`, `@`, `]`, or `|`, found `..`
6+
//~^ ERROR expected one of `,`, `@`, `]`, `if`, or `|`, found `..`
77
[] => 0,
88
}
99
}

tests/ui/parser/issues/issue-72373.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: expected one of `,`, `@`, `]`, or `|`, found `..`
1+
error: expected one of `,`, `@`, `]`, `if`, or `|`, found `..`
22
--> $DIR/issue-72373.rs:5:19
33
|
44
LL | [h, ref ts..] => foo(c, n - h) + foo(ts, n),
5-
| ^^ expected one of `,`, `@`, `]`, or `|`
5+
| ^^ expected one of `,`, `@`, `]`, `if`, or `|`
66
|
77
help: if you meant to bind the contents of the rest of the array pattern into `ts`, use `@`
88
|

tests/ui/parser/pat-lt-bracket-7.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ fn main() {
33
let foo = core::iter::empty();
44

55
for Thing(x[]) in foo {}
6-
//~^ ERROR: expected one of `)`, `,`, `@`, or `|`, found `[`
6+
//~^ ERROR: expected one of `)`, `,`, `@`, `if`, or `|`, found `[`
77
}
88

99
const RECOVERY_WITNESS: () = 0; //~ ERROR mismatched types

tests/ui/parser/pat-lt-bracket-7.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
error: expected one of `)`, `,`, `@`, or `|`, found `[`
1+
error: expected one of `)`, `,`, `@`, `if`, or `|`, found `[`
22
--> $DIR/pat-lt-bracket-7.rs:5:16
33
|
44
LL | for Thing(x[]) in foo {}
55
| ^
66
| |
7-
| expected one of `)`, `,`, `@`, or `|`
7+
| expected one of `)`, `,`, `@`, `if`, or `|`
88
| help: missing `,`
99

1010
error[E0308]: mismatched types

tests/ui/parser/pat-recover-exprs.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ fn main() {
1717
f?() => (),
1818
//~^ error: expected a pattern, found an expression
1919
(_ + 1) => (),
20-
//~^ error: expected one of `)`, `,`, or `|`, found `+`
20+
//~^ error: expected one of `)`, `,`, `if`, or `|`, found `+`
2121
}
2222

2323
let 1 + 1 = 2;
2424
//~^ error: expected a pattern, found an expression
2525

2626
let b = matches!(x, (x * x | x.f()) | x[0]);
27-
//~^ error: expected one of `)`, `,`, `@`, or `|`, found `*`
27+
//~^ error: expected one of `)`, `,`, `@`, `if`, or `|`, found `*`
2828
}

tests/ui/parser/pat-recover-exprs.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,23 +51,23 @@ error: expected a pattern, found an expression
5151
LL | f?() => (),
5252
| ^^^^ arbitrary expressions are not allowed in patterns
5353

54-
error: expected one of `)`, `,`, or `|`, found `+`
54+
error: expected one of `)`, `,`, `if`, or `|`, found `+`
5555
--> $DIR/pat-recover-exprs.rs:19:12
5656
|
5757
LL | (_ + 1) => (),
58-
| ^ expected one of `)`, `,`, or `|`
58+
| ^ expected one of `)`, `,`, `if`, or `|`
5959

6060
error: expected a pattern, found an expression
6161
--> $DIR/pat-recover-exprs.rs:23:9
6262
|
6363
LL | let 1 + 1 = 2;
6464
| ^^^^^ arbitrary expressions are not allowed in patterns
6565

66-
error: expected one of `)`, `,`, `@`, or `|`, found `*`
66+
error: expected one of `)`, `,`, `@`, `if`, or `|`, found `*`
6767
--> $DIR/pat-recover-exprs.rs:26:28
6868
|
6969
LL | let b = matches!(x, (x * x | x.f()) | x[0]);
70-
| ^ expected one of `)`, `,`, `@`, or `|`
70+
| ^ expected one of `)`, `,`, `@`, `if`, or `|`
7171
--> $SRC_DIR/core/src/macros/mod.rs:LL:COL
7272
|
7373
= note: while parsing argument for this `pat` macro fragment

tests/ui/parser/pat-recover-methodcalls.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ fn main() {
3232
//~^ error: expected a pattern, found a method call
3333

3434
if let (-1.Some(4)) = (0, Some(4)) {}
35-
//~^ error: expected one of `)`, `,`, `...`, `..=`, `..`, or `|`, found `.`
35+
//~^ error: expected one of `)`, `,`, `...`, `..=`, `..`, `if`, or `|`, found `.`
3636
//~| help: missing `,`
3737
}

tests/ui/parser/pat-recover-methodcalls.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ error: expected a pattern, found a method call
2222
LL | if let (-1.some(4)) = (0, Some(4)) {}
2323
| ^^^^^^^^^^ method calls are not allowed in patterns
2424

25-
error: expected one of `)`, `,`, `...`, `..=`, `..`, or `|`, found `.`
25+
error: expected one of `)`, `,`, `...`, `..=`, `..`, `if`, or `|`, found `.`
2626
--> $DIR/pat-recover-methodcalls.rs:34:15
2727
|
2828
LL | if let (-1.Some(4)) = (0, Some(4)) {}
2929
| ^
3030
| |
31-
| expected one of `)`, `,`, `...`, `..=`, `..`, or `|`
31+
| expected one of 7 possible tokens
3232
| help: missing `,`
3333

3434
error: aborting due to 5 previous errors

tests/ui/parser/pat-recover-wildcards.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ fn a() {
88

99
fn b() {
1010
match 2 {
11-
(_ % 4) => () //~ error: expected one of `)`, `,`, or `|`, found `%`
11+
(_ % 4) => () //~ error: expected one of `)`, `,`, `if`, or `|`, found `%`
1212
}
1313
}
1414

@@ -40,7 +40,7 @@ fn f() {
4040

4141
fn g() {
4242
match 7 {
43-
(_ * 0)..5 => () //~ error: expected one of `)`, `,`, or `|`, found `*`
43+
(_ * 0)..5 => () //~ error: expected one of `)`, `,`, `if`, or `|`, found `*`
4444
}
4545
}
4646

tests/ui/parser/pat-recover-wildcards.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ error: expected one of `=>`, `if`, or `|`, found `+`
44
LL | _ + 1 => ()
55
| ^ expected one of `=>`, `if`, or `|`
66

7-
error: expected one of `)`, `,`, or `|`, found `%`
7+
error: expected one of `)`, `,`, `if`, or `|`, found `%`
88
--> $DIR/pat-recover-wildcards.rs:11:12
99
|
1010
LL | (_ % 4) => ()
11-
| ^ expected one of `)`, `,`, or `|`
11+
| ^ expected one of `)`, `,`, `if`, or `|`
1212

1313
error: expected one of `=>`, `if`, or `|`, found `.`
1414
--> $DIR/pat-recover-wildcards.rs:17:10
@@ -47,11 +47,11 @@ error: expected one of `=>`, `if`, or `|`, found reserved identifier `_`
4747
LL | 0..._ => ()
4848
| ^ expected one of `=>`, `if`, or `|`
4949

50-
error: expected one of `)`, `,`, or `|`, found `*`
50+
error: expected one of `)`, `,`, `if`, or `|`, found `*`
5151
--> $DIR/pat-recover-wildcards.rs:43:12
5252
|
5353
LL | (_ * 0)..5 => ()
54-
| ^ expected one of `)`, `,`, or `|`
54+
| ^ expected one of `)`, `,`, `if`, or `|`
5555

5656
error: expected one of `=>`, `if`, or `|`, found `(`
5757
--> $DIR/pat-recover-wildcards.rs:49:11

tests/ui/pattern/bindings-after-at/nested-type-ascription-syntactically-invalid.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ LL | let a: u8 @ b = 0;
66
| |
77
| while parsing the type for `a`
88

9-
error: expected one of `)`, `,`, `@`, or `|`, found `:`
9+
error: expected one of `)`, `,`, `@`, `if`, or `|`, found `:`
1010
--> $DIR/nested-type-ascription-syntactically-invalid.rs:24:15
1111
|
1212
LL | let a @ (b: u8);
13-
| ^ expected one of `)`, `,`, `@`, or `|`
13+
| ^ expected one of `)`, `,`, `@`, `if`, or `|`
1414
|
1515
= note: type ascription syntax has been removed, see issue #101728 <https://github.com/rust-lang/rust/issues/101728>
1616

0 commit comments

Comments
 (0)