]> git.lizzy.rs Git - rust.git/commitdiff
parser: `parse_pats` -> `parse_top_pat{_unpack}`.
authorMazdak Farrokhzad <twingoow@gmail.com>
Sun, 18 Aug 2019 20:04:28 +0000 (22:04 +0200)
committerMazdak Farrokhzad <twingoow@gmail.com>
Sat, 24 Aug 2019 19:53:55 +0000 (21:53 +0200)
src/libsyntax/parse/parser/expr.rs
src/libsyntax/parse/parser/pat.rs

index 5da9b75d53b045cfb2b8deb6bc1ee6ab56322197..b9dd851817160679c7eaa9ecd113ffe181fdadde 100644 (file)
@@ -1241,11 +1241,12 @@ fn parse_cond_expr(&mut self) -> PResult<'a, P<Expr>> {
         Ok(cond)
     }
 
-    /// Parses a `let $pats = $expr` pseudo-expression.
+    /// Parses a `let $pat = $expr` pseudo-expression.
     /// The `let` token has already been eaten.
     fn parse_let_expr(&mut self, attrs: ThinVec<Attribute>) -> PResult<'a, P<Expr>> {
         let lo = self.prev_span;
-        let pats = self.parse_pats()?;
+        // FIXME(or_patterns, Centril | dlrobertson): use `parse_top_pat` instead.
+        let pat = self.parse_top_pat_unpack(false)?;
         self.expect(&token::Eq)?;
         let expr = self.with_res(
             Restrictions::NO_STRUCT_LITERAL,
@@ -1253,7 +1254,7 @@ fn parse_let_expr(&mut self, attrs: ThinVec<Attribute>) -> PResult<'a, P<Expr>>
         )?;
         let span = lo.to(expr.span);
         self.sess.gated_spans.let_chains.borrow_mut().push(span);
-        Ok(self.mk_expr(span, ExprKind::Let(pats, expr), attrs))
+        Ok(self.mk_expr(span, ExprKind::Let(pat, expr), attrs))
     }
 
     /// `else` token already eaten
@@ -1387,7 +1388,8 @@ fn parse_match_expr(&mut self, mut attrs: ThinVec<Attribute>) -> PResult<'a, P<E
     crate fn parse_arm(&mut self) -> PResult<'a, Arm> {
         let attrs = self.parse_outer_attributes()?;
         let lo = self.token.span;
-        let pats = self.parse_pats()?;
+        // FIXME(or_patterns, Centril | dlrobertson): use `parse_top_pat` instead.
+        let pat = self.parse_top_pat_unpack(false)?;
         let guard = if self.eat_keyword(kw::If) {
             Some(self.parse_expr()?)
         } else {
@@ -1448,7 +1450,7 @@ fn parse_match_expr(&mut self, mut attrs: ThinVec<Attribute>) -> PResult<'a, P<E
 
         Ok(ast::Arm {
             attrs,
-            pats,
+            pats: pat, // FIXME(or_patterns, Centril | dlrobertson): this should just be `pat,`.
             guard,
             body: expr,
             span: lo.to(hi),
index 8fab8884ca018cd575f4b9b39a199dc203649561..e4a9dc0097752055bee591085b6162ced7c9b082 100644 (file)
@@ -20,19 +20,25 @@ pub fn parse_pat(&mut self, expected: Expected) -> PResult<'a, P<Pat>> {
         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)
     }
 
     pub(super) fn parse_top_level_pat(&mut self) -> PResult<'a, P<Pat>> {