1
Fork 0

parser: extract ban_unexpected_or_or.

This commit is contained in:
Mazdak Farrokhzad 2019-08-18 15:28:14 +02:00
parent 5ade61a4f1
commit 5299d8a191

View file

@ -31,14 +31,7 @@ impl<'a> Parser<'a> {
pats.push(self.parse_top_level_pat()?); pats.push(self.parse_top_level_pat()?);
if self.token == token::OrOr { if self.token == token::OrOr {
self.struct_span_err(self.token.span, "unexpected token `||` after pattern") self.ban_unexpected_or_or();
.span_suggestion(
self.token.span,
"use a single `|` to specify multiple patterns",
"|".to_owned(),
Applicability::MachineApplicable
)
.emit();
self.bump(); self.bump();
} else if self.eat(&token::BinOp(token::Or)) { } else if self.eat(&token::BinOp(token::Or)) {
// This is a No-op. Continue the loop to parse the next // This is a No-op. Continue the loop to parse the next
@ -49,6 +42,17 @@ impl<'a> Parser<'a> {
}; };
} }
fn ban_unexpected_or_or(&mut self) {
self.struct_span_err(self.token.span, "unexpected token `||` after pattern")
.span_suggestion(
self.token.span,
"use a single `|` to specify multiple patterns",
"|".to_owned(),
Applicability::MachineApplicable
)
.emit();
}
/// A wrapper around `parse_pat` with some special error handling for the /// 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 /// "top-level" patterns in a match arm, `for` loop, `let`, &c. (in contrast
/// to subpatterns within such). /// to subpatterns within such).
@ -116,9 +120,7 @@ impl<'a> Parser<'a> {
let mut pats = vec![first_pat]; let mut pats = vec![first_pat];
while self.eat(&token::BinOp(token::Or)) { while self.eat(&token::BinOp(token::Or)) {
pats.push(self.parse_pat_with_range_pat( pats.push(self.parse_pat_with_range_pat(true, expected)?);
true, expected
)?);
} }
let or_pattern_span = lo.to(self.prev_span); let or_pattern_span = lo.to(self.prev_span);