@@ -8,7 +8,7 @@ use rustc_error_codes::*;
88use syntax:: ptr:: P ;
99use syntax:: ast:: { self , Ty , TyKind , MutTy , BareFnTy , FunctionRetTy , GenericParam , Lifetime , Ident } ;
1010use syntax:: ast:: { TraitBoundModifier , TraitObjectSyntax , GenericBound , GenericBounds , PolyTraitRef } ;
11- use syntax:: ast:: { Mutability , AnonConst , Mac } ;
11+ use syntax:: ast:: { Mutability , Mac } ;
1212use syntax:: token:: { self , Token } ;
1313use syntax:: struct_span_err;
1414use syntax_pos:: source_map:: Span ;
@@ -81,18 +81,7 @@ impl<'a> Parser<'a> {
8181 } else if self . eat ( & token:: BinOp ( token:: Star ) ) {
8282 self . parse_ty_ptr ( ) ?
8383 } else if self . eat ( & token:: OpenDelim ( token:: Bracket ) ) {
84- // Array or slice
85- let t = self . parse_ty ( ) ?;
86- // Parse optional `; EXPR` in `[TYPE; EXPR]`
87- let t = match self . maybe_parse_fixed_length_of_vec ( ) ? {
88- None => TyKind :: Slice ( t) ,
89- Some ( length) => TyKind :: Array ( t, AnonConst {
90- id : ast:: DUMMY_NODE_ID ,
91- value : length,
92- } ) ,
93- } ;
94- self . expect ( & token:: CloseDelim ( token:: Bracket ) ) ?;
95- t
84+ self . parse_array_or_slice_ty ( ) ?
9685 } else if self . check ( & token:: BinOp ( token:: And ) ) || self . check ( & token:: AndAnd ) {
9786 // Reference
9887 self . expect_and ( ) ?;
@@ -101,12 +90,9 @@ impl<'a> Parser<'a> {
10190 // `typeof(EXPR)`
10291 // In order to not be ambiguous, the type must be surrounded by parens.
10392 self . expect ( & token:: OpenDelim ( token:: Paren ) ) ?;
104- let e = AnonConst {
105- id : ast:: DUMMY_NODE_ID ,
106- value : self . parse_expr ( ) ?,
107- } ;
93+ let expr = self . parse_anon_const_expr ( ) ?;
10894 self . expect ( & token:: CloseDelim ( token:: Paren ) ) ?;
109- TyKind :: Typeof ( e )
95+ TyKind :: Typeof ( expr )
11096 } else if self . eat_keyword ( kw:: Underscore ) {
11197 // A type to be inferred `_`
11298 TyKind :: Infer
@@ -265,12 +251,17 @@ impl<'a> Parser<'a> {
265251 Ok ( TyKind :: Ptr ( MutTy { ty, mutbl } ) )
266252 }
267253
268- fn maybe_parse_fixed_length_of_vec ( & mut self ) -> PResult < ' a , Option < P < ast:: Expr > > > {
269- if self . eat ( & token:: Semi ) {
270- Ok ( Some ( self . parse_expr ( ) ?) )
254+ /// Parses an array (`[TYPE; EXPR]`) or slice (`[TYPE]`) type.
255+ /// The opening `[` bracket is already eaten.
256+ fn parse_array_or_slice_ty ( & mut self ) -> PResult < ' a , TyKind > {
257+ let elt_ty = self . parse_ty ( ) ?;
258+ let ty = if self . eat ( & token:: Semi ) {
259+ TyKind :: Array ( elt_ty, self . parse_anon_const_expr ( ) ?)
271260 } else {
272- Ok ( None )
273- }
261+ TyKind :: Slice ( elt_ty)
262+ } ;
263+ self . expect ( & token:: CloseDelim ( token:: Bracket ) ) ?;
264+ Ok ( ty)
274265 }
275266
276267 fn parse_borrowed_pointee ( & mut self ) -> PResult < ' a , TyKind > {
0 commit comments