Skip to content

Commit c67c30d

Browse files
committed
refactor parse_ty_tuple_or_parens
1 parent f3ef4a4 commit c67c30d

File tree

1 file changed

+16
-24
lines changed
  • src/librustc_parse/parser

1 file changed

+16
-24
lines changed

src/librustc_parse/parser/ty.rs

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ impl<'a> Parser<'a> {
7373

7474
let lo = self.token.span;
7575
let mut impl_dyn_multi = false;
76-
let kind = if self.eat(&token::OpenDelim(token::Paren)) {
77-
self.parse_ty_tuple_or_parens(allow_plus)?
76+
let kind = if self.check(&token::OpenDelim(token::Paren)) {
77+
self.parse_ty_tuple_or_parens(lo, allow_plus)?
7878
} else if self.eat(&token::Not) {
7979
// Never type `!`
8080
TyKind::Never
@@ -208,34 +208,26 @@ impl<'a> Parser<'a> {
208208
/// Parses either:
209209
/// - `(TYPE)`, a parenthesized type.
210210
/// - `(TYPE,)`, a tuple with a single field of type TYPE.
211-
fn parse_ty_tuple_or_parens(&mut self, allow_plus: bool) -> PResult<'a, TyKind> {
212-
let lo = self.token.span;
213-
let mut ts = vec![];
214-
let mut last_comma = false;
215-
while self.token != token::CloseDelim(token::Paren) {
216-
ts.push(self.parse_ty()?);
217-
if self.eat(&token::Comma) {
218-
last_comma = true;
219-
} else {
220-
last_comma = false;
221-
break;
222-
}
223-
}
224-
let trailing_plus = self.prev_token_kind == PrevTokenKind::Plus;
225-
self.expect(&token::CloseDelim(token::Paren))?;
211+
fn parse_ty_tuple_or_parens(&mut self, lo: Span, allow_plus: bool) -> PResult<'a, TyKind> {
212+
let mut trailing_plus = false;
213+
let (ts, trailing) = self.parse_paren_comma_seq(|p| {
214+
let ty = p.parse_ty()?;
215+
trailing_plus = p.prev_token_kind == PrevTokenKind::Plus;
216+
Ok(ty)
217+
})?;
226218

227-
if ts.len() == 1 && !last_comma {
219+
if ts.len() == 1 && !trailing {
228220
let ty = ts.into_iter().nth(0).unwrap().into_inner();
229221
let maybe_bounds = allow_plus && self.token.is_like_plus();
230222
match ty.kind {
231223
// `(TY_BOUND_NOPAREN) + BOUND + ...`.
232-
TyKind::Path(None, ref path) if maybe_bounds => {
233-
self.parse_remaining_bounds(Vec::new(), path.clone(), lo, true)
224+
TyKind::Path(None, path) if maybe_bounds => {
225+
self.parse_remaining_bounds(Vec::new(), path, lo, true)
234226
}
235-
TyKind::TraitObject(ref bounds, TraitObjectSyntax::None)
236-
if maybe_bounds && bounds.len() == 1 && !trailing_plus => {
237-
let path = match bounds[0] {
238-
GenericBound::Trait(ref pt, ..) => pt.trait_ref.path.clone(),
227+
TyKind::TraitObject(mut bounds, TraitObjectSyntax::None)
228+
if maybe_bounds && bounds.len() == 1 && !trailing_plus => {
229+
let path = match bounds.remove(0) {
230+
GenericBound::Trait(pt, ..) => pt.trait_ref.path,
239231
GenericBound::Outlives(..) => self.bug("unexpected lifetime bound"),
240232
};
241233
self.parse_remaining_bounds(Vec::new(), path, lo, true)

0 commit comments

Comments
 (0)