Skip to content

Commit 0bbea47

Browse files
committed
parser: refactor parse_pat_with_or + use it in [p0, p1, ..] pats.
1 parent 1ba7550 commit 0bbea47

File tree

1 file changed

+15
-8
lines changed
  • src/libsyntax/parse/parser

1 file changed

+15
-8
lines changed

src/libsyntax/parse/parser/pat.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,12 @@ impl<'a> Parser<'a> {
104104
}
105105

106106
/// Parses a pattern, that may be a or-pattern (e.g. `Some(Foo | Bar)`).
107-
fn parse_pat_with_or(&mut self, expected: Expected) -> PResult<'a, P<Pat>> {
107+
fn parse_pat_with_or(&mut self, expected: Expected, gate_or: bool) -> PResult<'a, P<Pat>> {
108108
// Parse the first pattern.
109109
let first_pat = self.parse_pat(expected)?;
110110

111-
// If the next token is not a `|`, this is not an or-pattern and
112-
// we should exit here.
111+
// If the next token is not a `|`,
112+
// this is not an or-pattern and we should exit here.
113113
if !self.check(&token::BinOp(token::Or)) {
114114
return Ok(first_pat)
115115
}
@@ -124,7 +124,10 @@ impl<'a> Parser<'a> {
124124

125125
let or_pattern_span = lo.to(self.prev_span);
126126

127-
self.sess.gated_spans.or_patterns.borrow_mut().push(or_pattern_span);
127+
// Feature gate the or-pattern if instructed:
128+
if gate_or {
129+
self.sess.gated_spans.or_patterns.borrow_mut().push(or_pattern_span);
130+
}
128131

129132
Ok(self.mk_pat(or_pattern_span, PatKind::Or(pats)))
130133
}
@@ -145,7 +148,11 @@ impl<'a> Parser<'a> {
145148
token::OpenDelim(token::Paren) => self.parse_pat_tuple_or_parens()?,
146149
token::OpenDelim(token::Bracket) => {
147150
// Parse `[pat, pat,...]` as a slice pattern.
148-
PatKind::Slice(self.parse_delim_comma_seq(token::Bracket, |p| p.parse_pat(None))?.0)
151+
let (pats, _) = self.parse_delim_comma_seq(
152+
token::Bracket,
153+
|p| p.parse_pat_with_or(None, true),
154+
)?;
155+
PatKind::Slice(pats)
149156
}
150157
token::DotDot => {
151158
self.bump();
@@ -273,7 +280,7 @@ impl<'a> Parser<'a> {
273280
/// Parse a tuple or parenthesis pattern.
274281
fn parse_pat_tuple_or_parens(&mut self) -> PResult<'a, PatKind> {
275282
let (fields, trailing_comma) = self.parse_paren_comma_seq(|p| {
276-
p.parse_pat_with_or(None)
283+
p.parse_pat_with_or(None, true)
277284
})?;
278285

279286
// Here, `(pat,)` is a tuple pattern.
@@ -517,7 +524,7 @@ impl<'a> Parser<'a> {
517524
err.span_label(self.token.span, msg);
518525
return Err(err);
519526
}
520-
let (fields, _) = self.parse_paren_comma_seq(|p| p.parse_pat_with_or(None))?;
527+
let (fields, _) = self.parse_paren_comma_seq(|p| p.parse_pat_with_or(None, true))?;
521528
Ok(PatKind::TupleStruct(path, fields))
522529
}
523530

@@ -661,7 +668,7 @@ impl<'a> Parser<'a> {
661668
// Parsing a pattern of the form "fieldname: pat"
662669
let fieldname = self.parse_field_name()?;
663670
self.bump();
664-
let pat = self.parse_pat_with_or(None)?;
671+
let pat = self.parse_pat_with_or(None, true)?;
665672
hi = pat.span;
666673
(pat, fieldname, false)
667674
} else {

0 commit comments

Comments
 (0)