diff options
| author | Mazdak Farrokhzad <twingoow@gmail.com> | 2019-12-08 08:19:53 +0100 |
|---|---|---|
| committer | Mazdak Farrokhzad <twingoow@gmail.com> | 2019-12-21 19:20:41 +0100 |
| commit | 211560d05c3be81b82d029d49cd23d0f6fef859e (patch) | |
| tree | 2140d27a0e88456b0465a0d65c1f1709ec34afe6 | |
| parent | 3838b602f5baa2fe37f8cf77dbf43b73a0777677 (diff) | |
| download | rust-211560d05c3be81b82d029d49cd23d0f6fef859e.tar.gz rust-211560d05c3be81b82d029d49cd23d0f6fef859e.zip | |
extract parse_array_or_slice_ty
| -rw-r--r-- | src/librustc_parse/parser/expr.rs | 11 | ||||
| -rw-r--r-- | src/librustc_parse/parser/item.rs | 7 | ||||
| -rw-r--r-- | src/librustc_parse/parser/ty.rs | 37 |
3 files changed, 22 insertions, 33 deletions
diff --git a/src/librustc_parse/parser/expr.rs b/src/librustc_parse/parser/expr.rs index 159c2121d36..71c9e58f58f 100644 --- a/src/librustc_parse/parser/expr.rs +++ b/src/librustc_parse/parser/expr.rs @@ -90,6 +90,10 @@ impl<'a> Parser<'a> { 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 @@ impl<'a> Parser<'a> { } } - /// 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 @@ impl<'a> Parser<'a> { 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) { diff --git a/src/librustc_parse/parser/item.rs b/src/librustc_parse/parser/item.rs index 229ca07f13b..2a4c6f110c4 100644 --- a/src/librustc_parse/parser/item.rs +++ b/src/librustc_parse/parser/item.rs @@ -5,7 +5,7 @@ use crate::maybe_whole; 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 @@ impl<'a> Parser<'a> { }; 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 }; diff --git a/src/librustc_parse/parser/ty.rs b/src/librustc_parse/parser/ty.rs index 9ea5a88ddad..c3d934d3503 100644 --- a/src/librustc_parse/parser/ty.rs +++ b/src/librustc_parse/parser/ty.rs @@ -8,7 +8,7 @@ use rustc_error_codes::*; 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 @@ impl<'a> Parser<'a> { } 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 @@ impl<'a> Parser<'a> { // `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 @@ impl<'a> Parser<'a> { 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> { |
