self.parse_pat_with_range_pat(true, expected)
}
- /// Parses patterns, separated by '|' s.
- pub(super) fn parse_pats(&mut self) -> PResult<'a, Vec<P<Pat>>> {
- // Allow a '|' before the pats (RFCs 1925, 2530, and 2535).
- self.eat_or_separator();
-
- let mut pats = Vec::new();
- loop {
- pats.push(self.parse_top_level_pat()?);
+ // FIXME(or_patterns, Centril | dlrobertson):
+ // remove this and use `parse_top_pat` everywhere it is used instead.
+ pub(super) fn parse_top_pat_unpack(&mut self, gate_or: bool) -> PResult<'a, Vec<P<Pat>>> {
+ self.parse_top_pat(gate_or)
+ .map(|pat| pat.and_then(|pat| match pat.node {
+ PatKind::Or(pats) => pats,
+ node => vec![self.mk_pat(pat.span, node)],
+ }))
+ }
- if !self.eat_or_separator() {
- return Ok(pats);
- }
+ /// Entry point to the main pattern parser.
+ /// Corresponds to `top_pat` in RFC 2535 and allows or-pattern at the top level.
+ pub(super) fn parse_top_pat(&mut self, gate_or: bool) -> PResult<'a, P<Pat>> {
+ // Allow a '|' before the pats (RFCs 1925, 2530, and 2535).
+ if self.eat_or_separator() && gate_or {
+ self.sess.gated_spans.or_patterns.borrow_mut().push(self.prev_span);
}
+
+ self.parse_pat_with_or(None, gate_or, true)
}
- /// A wrapper around `parse_pat` with some special error handling for the
- /// "top-level" patterns in a match arm, `for` loop, `let`, &c. (in contrast
- /// to subpatterns within such).
pub(super) fn parse_top_level_pat(&mut self) -> PResult<'a, P<Pat>> {
let pat = self.parse_pat(None)?;
self.maybe_recover_unexpected_comma(pat.span, true)?;
Ok(pat)
}
- fn maybe_recover_unexpected_comma(&mut self, lo: Span, top_level: bool) -> PResult<'a, ()> {
- if !top_level || self.token != token::Comma {
- return Ok(());
- }
-
- // An unexpected comma after a top-level pattern is a clue that the
- // user (perhaps more accustomed to some other language) forgot the
- // parentheses in what should have been a tuple pattern; return a
- // suggestion-enhanced error here rather than choking on the comma later.
- let comma_span = self.token.span;
- self.bump();
- if let Err(mut err) = self.skip_pat_list() {
- // We didn't expect this to work anyway; we just wanted to advance to the
- // end of the comma-sequence so we know the span to suggest parenthesizing.
- err.cancel();
- }
- let seq_span = lo.to(self.prev_span);
- let mut err = self.struct_span_err(comma_span, "unexpected `,` in pattern");
- if let Ok(seq_snippet) = self.span_to_snippet(seq_span) {
- err.span_suggestion(
- seq_span,
- "try adding parentheses to match on a tuple..",
- format!("({})", seq_snippet),
- Applicability::MachineApplicable
- )
- .span_suggestion(
- seq_span,
- "..or a vertical bar to match on multiple alternatives",
- format!("{}", seq_snippet.replace(",", " |")),
- Applicability::MachineApplicable
- );
- }
- Err(err)
- }
-
- /// Parse and throw away a parentesized comma separated
- /// sequence of patterns until `)` is reached.
- fn skip_pat_list(&mut self) -> PResult<'a, ()> {
- while !self.check(&token::CloseDelim(token::Paren)) {
- self.parse_pat(None)?;
- if !self.eat(&token::Comma) {
- return Ok(())
- }
- }
- Ok(())
- }
-
/// Parses a pattern, that may be a or-pattern (e.g. `Foo | Bar` in `Some(Foo | Bar)`).
/// Corresponds to `pat<allow_top_alt>` in RFC 2535.
fn parse_pat_with_or(
}
}
+ /// We have parsed `||` instead of `|`. Error and suggest `|` instead.
fn ban_unexpected_or_or(&mut self) {
self.struct_span_err(self.token.span, "unexpected token `||` after pattern")
.span_suggestion(
.emit();
}
+ /// Some special error handling for the "top-level" patterns in a match arm,
+ /// `for` loop, `let`, &c. (in contrast to subpatterns within such).
+ fn maybe_recover_unexpected_comma(&mut self, lo: Span, top_level: bool) -> PResult<'a, ()> {
+ if !top_level || self.token != token::Comma {
+ return Ok(());
+ }
+
+ // An unexpected comma after a top-level pattern is a clue that the
+ // user (perhaps more accustomed to some other language) forgot the
+ // parentheses in what should have been a tuple pattern; return a
+ // suggestion-enhanced error here rather than choking on the comma later.
+ let comma_span = self.token.span;
+ self.bump();
+ if let Err(mut err) = self.skip_pat_list() {
+ // We didn't expect this to work anyway; we just wanted to advance to the
+ // end of the comma-sequence so we know the span to suggest parenthesizing.
+ err.cancel();
+ }
+ let seq_span = lo.to(self.prev_span);
+ let mut err = self.struct_span_err(comma_span, "unexpected `,` in pattern");
+ if let Ok(seq_snippet) = self.span_to_snippet(seq_span) {
+ err.span_suggestion(
+ seq_span,
+ "try adding parentheses to match on a tuple..",
+ format!("({})", seq_snippet),
+ Applicability::MachineApplicable
+ )
+ .span_suggestion(
+ seq_span,
+ "..or a vertical bar to match on multiple alternatives",
+ format!("{}", seq_snippet.replace(",", " |")),
+ Applicability::MachineApplicable
+ );
+ }
+ Err(err)
+ }
+
+ /// Parse and throw away a parentesized comma separated
+ /// sequence of patterns until `)` is reached.
+ fn skip_pat_list(&mut self) -> PResult<'a, ()> {
+ while !self.check(&token::CloseDelim(token::Paren)) {
+ self.parse_pat(None)?;
+ if !self.eat(&token::Comma) {
+ return Ok(())
+ }
+ }
+ Ok(())
+ }
+
/// Parses a pattern, with a setting whether modern range patterns (e.g., `a..=b`, `a..b` are
/// allowed).
fn parse_pat_with_range_pat(