Skip to content

Commit f085637

Browse files
committed
extract parse_array_or_slice_ty
1 parent 6d678b1 commit f085637

File tree

3 files changed

+21
-32
lines changed

3 files changed

+21
-32
lines changed

src/librustc_parse/parser/expr.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ impl<'a> Parser<'a> {
9191
self.parse_expr_res(Restrictions::empty(), None)
9292
}
9393

94+
pub(super) fn parse_anon_const_expr(&mut self) -> PResult<'a, AnonConst> {
95+
self.parse_expr().map(|value| AnonConst { id: DUMMY_NODE_ID, value })
96+
}
97+
9498
fn parse_paren_expr_seq(&mut self) -> PResult<'a, Vec<P<Expr>>> {
9599
self.parse_paren_comma_seq(|p| {
96100
match p.parse_expr() {
@@ -883,10 +887,7 @@ impl<'a> Parser<'a> {
883887
let first_expr = self.parse_expr()?;
884888
if self.eat(&token::Semi) {
885889
// Repeating array syntax: `[ 0; 512 ]`
886-
let count = AnonConst {
887-
id: DUMMY_NODE_ID,
888-
value: self.parse_expr()?,
889-
};
890+
let count = self.parse_anon_const_expr()?;
890891
self.expect(&token::CloseDelim(token::Bracket))?;
891892
ex = ExprKind::Repeat(first_expr, count);
892893
} else if self.eat(&token::Comma) {

src/librustc_parse/parser/item.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::maybe_whole;
55

66
use rustc_errors::{PResult, Applicability, DiagnosticBuilder, StashKey};
77
use rustc_error_codes::*;
8-
use syntax::ast::{self, DUMMY_NODE_ID, Ident, Attribute, AttrKind, AttrStyle, AnonConst, Item};
8+
use syntax::ast::{self, DUMMY_NODE_ID, Ident, Attribute, AttrKind, AttrStyle, Item};
99
use syntax::ast::{AssocItem, AssocItemKind, ItemKind, UseTree, UseTreeKind};
1010
use syntax::ast::{PathSegment, IsAuto, Constness, IsAsync, Unsafety, Defaultness, Extern, StrLit};
1111
use syntax::ast::{Visibility, VisibilityKind, Mutability, FnHeader, ForeignItem, ForeignItemKind};
@@ -1318,10 +1318,7 @@ impl<'a> Parser<'a> {
13181318
};
13191319

13201320
let disr_expr = if self.eat(&token::Eq) {
1321-
Some(AnonConst {
1322-
id: DUMMY_NODE_ID,
1323-
value: self.parse_expr()?,
1324-
})
1321+
Some(self.parse_anon_const_expr()?)
13251322
} else {
13261323
None
13271324
};

src/librustc_parse/parser/ty.rs

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_error_codes::*;
88
use syntax::ptr::P;
99
use syntax::ast::{self, Ty, TyKind, MutTy, BareFnTy, FunctionRetTy, GenericParam, Lifetime, Ident};
1010
use syntax::ast::{TraitBoundModifier, TraitObjectSyntax, GenericBound, GenericBounds, PolyTraitRef};
11-
use syntax::ast::{Mutability, AnonConst, Mac};
11+
use syntax::ast::{Mutability, Mac};
1212
use syntax::token::{self, Token};
1313
use syntax::struct_span_err;
1414
use 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

Comments
 (0)