Skip to content

Commit f3ef4a4

Browse files
committed
extract parse_ty_tuple_or_parens
1 parent c64eecf commit f3ef4a4

File tree

1 file changed

+44
-38
lines changed
  • src/librustc_parse/parser

1 file changed

+44
-38
lines changed

src/librustc_parse/parser/ty.rs

Lines changed: 44 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -74,44 +74,7 @@ impl<'a> Parser<'a> {
7474
let lo = self.token.span;
7575
let mut impl_dyn_multi = false;
7676
let kind = if self.eat(&token::OpenDelim(token::Paren)) {
77-
// `(TYPE)` is a parenthesized type.
78-
// `(TYPE,)` is a tuple with a single field of type TYPE.
79-
let mut ts = vec![];
80-
let mut last_comma = false;
81-
while self.token != token::CloseDelim(token::Paren) {
82-
ts.push(self.parse_ty()?);
83-
if self.eat(&token::Comma) {
84-
last_comma = true;
85-
} else {
86-
last_comma = false;
87-
break;
88-
}
89-
}
90-
let trailing_plus = self.prev_token_kind == PrevTokenKind::Plus;
91-
self.expect(&token::CloseDelim(token::Paren))?;
92-
93-
if ts.len() == 1 && !last_comma {
94-
let ty = ts.into_iter().nth(0).unwrap().into_inner();
95-
let maybe_bounds = allow_plus && self.token.is_like_plus();
96-
match ty.kind {
97-
// `(TY_BOUND_NOPAREN) + BOUND + ...`.
98-
TyKind::Path(None, ref path) if maybe_bounds => {
99-
self.parse_remaining_bounds(Vec::new(), path.clone(), lo, true)?
100-
}
101-
TyKind::TraitObject(ref bounds, TraitObjectSyntax::None)
102-
if maybe_bounds && bounds.len() == 1 && !trailing_plus => {
103-
let path = match bounds[0] {
104-
GenericBound::Trait(ref pt, ..) => pt.trait_ref.path.clone(),
105-
GenericBound::Outlives(..) => self.bug("unexpected lifetime bound"),
106-
};
107-
self.parse_remaining_bounds(Vec::new(), path, lo, true)?
108-
}
109-
// `(TYPE)`
110-
_ => TyKind::Paren(P(ty))
111-
}
112-
} else {
113-
TyKind::Tup(ts)
114-
}
77+
self.parse_ty_tuple_or_parens(allow_plus)?
11578
} else if self.eat(&token::Not) {
11679
// Never type `!`
11780
TyKind::Never
@@ -242,6 +205,49 @@ impl<'a> Parser<'a> {
242205
self.maybe_recover_from_bad_qpath(ty, allow_qpath_recovery)
243206
}
244207

208+
/// Parses either:
209+
/// - `(TYPE)`, a parenthesized type.
210+
/// - `(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))?;
226+
227+
if ts.len() == 1 && !last_comma {
228+
let ty = ts.into_iter().nth(0).unwrap().into_inner();
229+
let maybe_bounds = allow_plus && self.token.is_like_plus();
230+
match ty.kind {
231+
// `(TY_BOUND_NOPAREN) + BOUND + ...`.
232+
TyKind::Path(None, ref path) if maybe_bounds => {
233+
self.parse_remaining_bounds(Vec::new(), path.clone(), lo, true)
234+
}
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(),
239+
GenericBound::Outlives(..) => self.bug("unexpected lifetime bound"),
240+
};
241+
self.parse_remaining_bounds(Vec::new(), path, lo, true)
242+
}
243+
// `(TYPE)`
244+
_ => Ok(TyKind::Paren(P(ty)))
245+
}
246+
} else {
247+
Ok(TyKind::Tup(ts))
248+
}
249+
}
250+
245251
fn parse_remaining_bounds(&mut self, generic_params: Vec<GenericParam>, path: ast::Path,
246252
lo: Span, parse_plus: bool) -> PResult<'a, TyKind> {
247253
let poly_trait_ref = PolyTraitRef::new(generic_params, path, lo.to(self.prev_span));

0 commit comments

Comments
 (0)