Skip to content

Commit 211560d

Browse files
committed
extract parse_array_or_slice_ty
1 parent 3838b60 commit 211560d

File tree

3 files changed

+22
-33
lines changed

3 files changed

+22
-33
lines changed

src/librustc_parse/parser/expr.rs

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

93+
pub(super) fn parse_anon_const_expr(&mut self) -> PResult<'a, AnonConst> {
94+
self.parse_expr().map(|value| AnonConst { id: DUMMY_NODE_ID, value })
95+
}
96+
9397
fn parse_expr_catch_underscore(&mut self) -> PResult<'a, P<Expr>> {
9498
match self.parse_expr() {
9599
Ok(expr) => Ok(expr),
@@ -109,7 +113,7 @@ impl<'a> Parser<'a> {
109113
}
110114
}
111115

112-
/// Parses a sequence of expressions bounded by parentheses.
116+
/// Parses a sequence of expressions delimited by parentheses.
113117
fn parse_paren_expr_seq(&mut self) -> PResult<'a, Vec<P<Expr>>> {
114118
self.parse_paren_comma_seq(|p| {
115119
p.parse_expr_catch_underscore()
@@ -955,10 +959,7 @@ impl<'a> Parser<'a> {
955959
let first_expr = self.parse_expr()?;
956960
if self.eat(&token::Semi) {
957961
// Repeating array syntax: `[ 0; 512 ]`
958-
let count = AnonConst {
959-
id: DUMMY_NODE_ID,
960-
value: self.parse_expr()?,
961-
};
962+
let count = self.parse_anon_const_expr()?;
962963
self.expect(close)?;
963964
ExprKind::Repeat(first_expr, count)
964965
} 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, AttrVec, Attribute, AttrKind, AttrStyle, AnonConst};
8+
use syntax::ast::{self, DUMMY_NODE_ID, Ident, AttrVec, Attribute, AttrKind, AttrStyle};
99
use syntax::ast::{AssocItem, AssocItemKind, Item, 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};
@@ -1317,10 +1317,7 @@ impl<'a> Parser<'a> {
13171317
};
13181318

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

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)