]> git.lizzy.rs Git - rust.git/commitdiff
extract parse_array_or_slice_ty
authorMazdak Farrokhzad <twingoow@gmail.com>
Sun, 8 Dec 2019 07:19:53 +0000 (08:19 +0100)
committerMazdak Farrokhzad <twingoow@gmail.com>
Sat, 21 Dec 2019 18:20:41 +0000 (19:20 +0100)
src/librustc_parse/parser/expr.rs
src/librustc_parse/parser/item.rs
src/librustc_parse/parser/ty.rs

index 159c2121d36c8978f56ef610fdb0101bfd83337d..71c9e58f58fd7e579e7b7cd91aa9ec9deaaf0860 100644 (file)
@@ -90,6 +90,10 @@ pub fn parse_expr(&mut self) -> PResult<'a, P<Expr>> {
         self.parse_expr_res(Restrictions::empty(), None)
     }
 
+    pub(super) fn parse_anon_const_expr(&mut self) -> PResult<'a, AnonConst> {
+        self.parse_expr().map(|value| AnonConst { id: DUMMY_NODE_ID, value })
+    }
+
     fn parse_expr_catch_underscore(&mut self) -> PResult<'a, P<Expr>> {
         match self.parse_expr() {
             Ok(expr) => Ok(expr),
@@ -109,7 +113,7 @@ fn parse_expr_catch_underscore(&mut self) -> PResult<'a, P<Expr>> {
         }
     }
 
-    /// Parses a sequence of expressions bounded by parentheses.
+    /// Parses a sequence of expressions delimited by parentheses.
     fn parse_paren_expr_seq(&mut self) -> PResult<'a, Vec<P<Expr>>> {
         self.parse_paren_comma_seq(|p| {
             p.parse_expr_catch_underscore()
@@ -955,10 +959,7 @@ fn parse_array_or_repeat_expr(&mut self, mut attrs: AttrVec) -> PResult<'a, P<Ex
             let first_expr = self.parse_expr()?;
             if self.eat(&token::Semi) {
                 // Repeating array syntax: `[ 0; 512 ]`
-                let count = AnonConst {
-                    id: DUMMY_NODE_ID,
-                    value: self.parse_expr()?,
-                };
+                let count = self.parse_anon_const_expr()?;
                 self.expect(close)?;
                 ExprKind::Repeat(first_expr, count)
             } else if self.eat(&token::Comma) {
index 229ca07f13b51a4063610e4249f36c257b7ec986..2a4c6f110c4e811256f29938badc03fe4d652b90 100644 (file)
@@ -5,7 +5,7 @@
 
 use rustc_errors::{PResult, Applicability, DiagnosticBuilder, StashKey};
 use rustc_error_codes::*;
-use syntax::ast::{self, DUMMY_NODE_ID, Ident, AttrVec, Attribute, AttrKind, AttrStyle, AnonConst};
+use syntax::ast::{self, DUMMY_NODE_ID, Ident, AttrVec, Attribute, AttrKind, AttrStyle};
 use syntax::ast::{AssocItem, AssocItemKind, Item, ItemKind, UseTree, UseTreeKind};
 use syntax::ast::{PathSegment, IsAuto, Constness, IsAsync, Unsafety, Defaultness, Extern, StrLit};
 use syntax::ast::{Visibility, VisibilityKind, Mutability, FnHeader, ForeignItem, ForeignItemKind};
@@ -1317,10 +1317,7 @@ fn parse_enum_variant(&mut self) -> PResult<'a, Option<Variant>> {
         };
 
         let disr_expr = if self.eat(&token::Eq) {
-            Some(AnonConst {
-                id: DUMMY_NODE_ID,
-                value: self.parse_expr()?,
-            })
+            Some(self.parse_anon_const_expr()?)
         } else {
             None
         };
index 9ea5a88ddadf9e44f9fa63ce325dc17a1da5b97f..c3d934d3503270e822fbe012fcc5bdb7768d7ac5 100644 (file)
@@ -8,7 +8,7 @@
 use syntax::ptr::P;
 use syntax::ast::{self, Ty, TyKind, MutTy, BareFnTy, FunctionRetTy, GenericParam, Lifetime, Ident};
 use syntax::ast::{TraitBoundModifier, TraitObjectSyntax, GenericBound, GenericBounds, PolyTraitRef};
-use syntax::ast::{Mutability, AnonConst, Mac};
+use syntax::ast::{Mutability, Mac};
 use syntax::token::{self, Token};
 use syntax::struct_span_err;
 use syntax_pos::source_map::Span;
@@ -81,18 +81,7 @@ fn parse_ty_common(
         } else if self.eat(&token::BinOp(token::Star)) {
             self.parse_ty_ptr()?
         } else if self.eat(&token::OpenDelim(token::Bracket)) {
-            // Array or slice
-            let t = self.parse_ty()?;
-            // Parse optional `; EXPR` in `[TYPE; EXPR]`
-            let t = match self.maybe_parse_fixed_length_of_vec()? {
-                None => TyKind::Slice(t),
-                Some(length) => TyKind::Array(t, AnonConst {
-                    id: ast::DUMMY_NODE_ID,
-                    value: length,
-                }),
-            };
-            self.expect(&token::CloseDelim(token::Bracket))?;
-            t
+            self.parse_array_or_slice_ty()?
         } else if self.check(&token::BinOp(token::And)) || self.check(&token::AndAnd) {
             // Reference
             self.expect_and()?;
@@ -101,12 +90,9 @@ fn parse_ty_common(
             // `typeof(EXPR)`
             // In order to not be ambiguous, the type must be surrounded by parens.
             self.expect(&token::OpenDelim(token::Paren))?;
-            let e = AnonConst {
-                id: ast::DUMMY_NODE_ID,
-                value: self.parse_expr()?,
-            };
+            let expr = self.parse_anon_const_expr()?;
             self.expect(&token::CloseDelim(token::Paren))?;
-            TyKind::Typeof(e)
+            TyKind::Typeof(expr)
         } else if self.eat_keyword(kw::Underscore) {
             // A type to be inferred `_`
             TyKind::Infer
@@ -265,12 +251,17 @@ fn parse_ty_ptr(&mut self) -> PResult<'a, TyKind> {
         Ok(TyKind::Ptr(MutTy { ty, mutbl }))
     }
 
-    fn maybe_parse_fixed_length_of_vec(&mut self) -> PResult<'a, Option<P<ast::Expr>>> {
-        if self.eat(&token::Semi) {
-            Ok(Some(self.parse_expr()?))
+    /// Parses an array (`[TYPE; EXPR]`) or slice (`[TYPE]`) type.
+    /// The opening `[` bracket is already eaten.
+    fn parse_array_or_slice_ty(&mut self) -> PResult<'a, TyKind> {
+        let elt_ty = self.parse_ty()?;
+        let ty = if self.eat(&token::Semi) {
+            TyKind::Array(elt_ty, self.parse_anon_const_expr()?)
         } else {
-            Ok(None)
-        }
+            TyKind::Slice(elt_ty)
+        };
+        self.expect(&token::CloseDelim(token::Bracket))?;
+        Ok(ty)
     }
 
     fn parse_borrowed_pointee(&mut self) -> PResult<'a, TyKind> {