Skip to content

Commit 74da69e

Browse files
committed
parse guard patterns and use correct pattern parsers throughout rustc
1 parent 893d86f commit 74da69e

File tree

14 files changed

+47
-40
lines changed

14 files changed

+47
-40
lines changed

compiler/rustc_expand/src/expand.rs

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

compiler/rustc_parse/src/parser/expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2767,7 +2767,7 @@ impl<'a> Parser<'a> {
27672767
};
27682768
// Try to parse the pattern `for ($PAT) in $EXPR`.
27692769
let pat = match (
2770-
self.parse_pat_no_top_guard(
2770+
self.parse_pat_allow_top_guard(
27712771
None,
27722772
RecoverComma::Yes,
27732773
RecoverColon::Yes,

compiler/rustc_parse/src/parser/pat.rs

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

113120
/// Parses a pattern.
@@ -126,8 +133,8 @@ impl<'a> Parser<'a> {
126133
/// Parses a pattern.
127134
///
128135
/// Corresponds to `PatternNoTopGuard` in RFC 3637 and allows or-patterns, but not
129-
/// guard patterns, at the top level. Used for parsing patterns in `pat` fragments and
130-
/// `let`, `if let`, and `while let` expressions.
136+
/// guard patterns, at the top level. Used for parsing patterns in `pat` fragments (until
137+
/// the next edition) and `let`, `if let`, and `while let` expressions.
131138
///
132139
/// Note that after the FCP in <https://github.com/rust-lang/rust/issues/81415>,
133140
/// a leading vert is allowed in nested or-patterns, too. This allows us to
@@ -710,7 +717,7 @@ impl<'a> Parser<'a> {
710717
} else if self.check(&token::OpenDelim(Delimiter::Bracket)) {
711718
// Parse `[pat, pat,...]` as a slice pattern.
712719
let (pats, _) = self.parse_delim_comma_seq(Delimiter::Bracket, |p| {
713-
p.parse_pat_no_top_guard(
720+
p.parse_pat_allow_top_guard(
714721
None,
715722
RecoverComma::No,
716723
RecoverColon::No,
@@ -958,7 +965,7 @@ impl<'a> Parser<'a> {
958965
let open_paren = self.token.span;
959966

960967
let (fields, trailing_comma) = self.parse_paren_comma_seq(|p| {
961-
p.parse_pat_no_top_guard(
968+
p.parse_pat_allow_top_guard(
962969
None,
963970
RecoverComma::No,
964971
RecoverColon::No,
@@ -1373,7 +1380,7 @@ impl<'a> Parser<'a> {
13731380
path: Path,
13741381
) -> PResult<'a, PatKind> {
13751382
let (fields, _) = self.parse_paren_comma_seq(|p| {
1376-
p.parse_pat_no_top_guard(
1383+
p.parse_pat_allow_top_guard(
13771384
None,
13781385
RecoverComma::No,
13791386
RecoverColon::No,
@@ -1408,7 +1415,7 @@ impl<'a> Parser<'a> {
14081415
self.parse_builtin(|self_, _lo, ident| {
14091416
Ok(match ident.name {
14101417
// builtin#deref(PAT)
1411-
sym::deref => Some(ast::PatKind::Deref(self_.parse_pat_no_top_guard(
1418+
sym::deref => Some(ast::PatKind::Deref(self_.parse_pat_allow_top_guard(
14121419
None,
14131420
RecoverComma::Yes,
14141421
RecoverColon::Yes,
@@ -1683,7 +1690,7 @@ impl<'a> Parser<'a> {
16831690
// Parsing a pattern of the form `fieldname: pat`.
16841691
let fieldname = self.parse_field_name()?;
16851692
self.bump();
1686-
let pat = self.parse_pat_no_top_guard(
1693+
let pat = self.parse_pat_allow_top_guard(
16871694
None,
16881695
RecoverComma::No,
16891696
RecoverColon::No,

compiler/rustc_parse/src/parser/path.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ impl<'a> Parser<'a> {
469469
PathStyle::Pat
470470
if let Ok(_) = self
471471
.parse_paren_comma_seq(|p| {
472-
p.parse_pat_no_top_guard(
472+
p.parse_pat_allow_top_guard(
473473
None,
474474
RecoverComma::No,
475475
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/misspelled-keywords/ref.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 `list`
1+
error: expected one of `)`, `,`, `@`, `if`, or `|`, found `list`
22
--> $DIR/ref.rs:4:19
33
|
44
LL | Some(refe list) => println!("{list:?}"),
5-
| ^^^^ expected one of `)`, `,`, `@`, or `|`
5+
| ^^^^ expected one of `)`, `,`, `@`, `if`, or `|`
66
|
77
help: there is a keyword `ref` with a similar name
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/recover/recover-pat-exprs.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ fn array_indexing() {
2727
{ let x[0, 1, 2]; } //~ error: expected one of `:`, `;`, `=`, `@`, or `|`, found `[`
2828
{ let x[0; 20]; } //~ error: expected one of `:`, `;`, `=`, `@`, or `|`, found `[`
2929
{ let x[]; } //~ error: expected one of `:`, `;`, `=`, `@`, or `|`, found `[`
30-
{ let (x[]); } //~ error: expected one of `)`, `,`, `@`, or `|`, found `[`
30+
{ let (x[]); } //~ error: expected one of `)`, `,`, `@`, `if`, or `|`, found `[`
3131
//~^ missing `,`
3232
}
3333

@@ -95,12 +95,12 @@ fn main() {
9595
f?() => (),
9696
//~^ error: expected a pattern, found an expression
9797
(_ + 1) => (),
98-
//~^ error: expected one of `)`, `,`, or `|`, found `+`
98+
//~^ error: expected one of `)`, `,`, `if`, or `|`, found `+`
9999
}
100100

101101
let 1 + 1 = 2;
102102
//~^ error: expected a pattern, found an expression
103103

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

0 commit comments

Comments
 (0)