From 30b841dce0a1c0f26588f4b5791a9eda1c1f42f4 Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Sun, 18 Aug 2019 16:35:19 +0200 Subject: [PATCH] parser: improve or-patterns recovery. --- src/libsyntax/parse/parser/pat.rs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/libsyntax/parse/parser/pat.rs b/src/libsyntax/parse/parser/pat.rs index ca5a9f2a5a8..e52d0bc9d48 100644 --- a/src/libsyntax/parse/parser/pat.rs +++ b/src/libsyntax/parse/parser/pat.rs @@ -110,18 +110,25 @@ impl<'a> Parser<'a> { // If the next token is not a `|`, // this is not an or-pattern and we should exit here. - if !self.check(&token::BinOp(token::Or)) { + if !self.check(&token::BinOp(token::Or)) && self.token != token::OrOr { return Ok(first_pat) } let lo = first_pat.span; - let mut pats = vec![first_pat]; + loop { + if self.token == token::OrOr { + // Found `||`; Recover and pretend we parsed `|`. + self.ban_unexpected_or_or(); + self.bump(); + } else if self.eat(&token::BinOp(token::Or)) { + // Found `|`. Working towards a proper or-pattern. + } else { + break; + } - while self.eat(&token::BinOp(token::Or)) { pats.push(self.parse_pat_with_range_pat(true, expected)?); } - let or_pattern_span = lo.to(self.prev_span); // Feature gate the or-pattern if instructed: