Skip to content

Commit a4a34ab

Browse files
committed
parser: integrate maybe_recover_unexpected_comma in parse_pat_with_or.
1 parent 21d9b85 commit a4a34ab

File tree

1 file changed

+17
-9
lines changed
  • src/libsyntax/parse/parser

1 file changed

+17
-9
lines changed

src/libsyntax/parse/parser/pat.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@ impl<'a> Parser<'a> {
5757
/// to subpatterns within such).
5858
pub(super) fn parse_top_level_pat(&mut self) -> PResult<'a, P<Pat>> {
5959
let pat = self.parse_pat(None)?;
60-
self.maybe_recover_unexpected_comma(pat.span)?;
60+
self.maybe_recover_unexpected_comma(pat.span, true)?;
6161
Ok(pat)
6262
}
6363

64-
fn maybe_recover_unexpected_comma(&mut self, lo: Span) -> PResult<'a, ()> {
65-
if self.token != token::Comma {
64+
fn maybe_recover_unexpected_comma(&mut self, lo: Span, top_level: bool) -> PResult<'a, ()> {
65+
if !top_level || self.token != token::Comma {
6666
return Ok(());
6767
}
6868

@@ -109,9 +109,15 @@ impl<'a> Parser<'a> {
109109
}
110110

111111
/// Parses a pattern, that may be a or-pattern (e.g. `Some(Foo | Bar)`).
112-
fn parse_pat_with_or(&mut self, expected: Expected, gate_or: bool) -> PResult<'a, P<Pat>> {
112+
fn parse_pat_with_or(
113+
&mut self,
114+
expected: Expected,
115+
gate_or: bool,
116+
top_level: bool
117+
) -> PResult<'a, P<Pat>> {
113118
// Parse the first pattern.
114119
let first_pat = self.parse_pat(expected)?;
120+
self.maybe_recover_unexpected_comma(first_pat.span, top_level)?;
115121

116122
// If the next token is not a `|`,
117123
// this is not an or-pattern and we should exit here.
@@ -132,7 +138,9 @@ impl<'a> Parser<'a> {
132138
break;
133139
}
134140

135-
pats.push(self.parse_pat(expected)?);
141+
let pat = self.parse_pat(expected)?;
142+
self.maybe_recover_unexpected_comma(pat.span, top_level)?;
143+
pats.push(pat);
136144
}
137145
let or_pattern_span = lo.to(self.prev_span);
138146

@@ -162,7 +170,7 @@ impl<'a> Parser<'a> {
162170
// Parse `[pat, pat,...]` as a slice pattern.
163171
let (pats, _) = self.parse_delim_comma_seq(
164172
token::Bracket,
165-
|p| p.parse_pat_with_or(None, true),
173+
|p| p.parse_pat_with_or(None, true, false),
166174
)?;
167175
PatKind::Slice(pats)
168176
}
@@ -292,7 +300,7 @@ impl<'a> Parser<'a> {
292300
/// Parse a tuple or parenthesis pattern.
293301
fn parse_pat_tuple_or_parens(&mut self) -> PResult<'a, PatKind> {
294302
let (fields, trailing_comma) = self.parse_paren_comma_seq(|p| {
295-
p.parse_pat_with_or(None, true)
303+
p.parse_pat_with_or(None, true, false)
296304
})?;
297305

298306
// Here, `(pat,)` is a tuple pattern.
@@ -536,7 +544,7 @@ impl<'a> Parser<'a> {
536544
err.span_label(self.token.span, msg);
537545
return Err(err);
538546
}
539-
let (fields, _) = self.parse_paren_comma_seq(|p| p.parse_pat_with_or(None, true))?;
547+
let (fields, _) = self.parse_paren_comma_seq(|p| p.parse_pat_with_or(None, true, false))?;
540548
Ok(PatKind::TupleStruct(path, fields))
541549
}
542550

@@ -680,7 +688,7 @@ impl<'a> Parser<'a> {
680688
// Parsing a pattern of the form "fieldname: pat"
681689
let fieldname = self.parse_field_name()?;
682690
self.bump();
683-
let pat = self.parse_pat_with_or(None, true)?;
691+
let pat = self.parse_pat_with_or(None, true, false)?;
684692
hi = pat.span;
685693
(pat, fieldname, false)
686694
} else {

0 commit comments

Comments
 (0)