Skip to content

Commit 8838cd1

Browse files
committed
Move parse_remaining_bounds into a separate function
1 parent 6e75def commit 8838cd1

File tree

1 file changed

+21
-20
lines changed

1 file changed

+21
-20
lines changed

Diff for: src/libsyntax/parse/parser.rs

+21-20
Original file line numberDiff line numberDiff line change
@@ -1365,18 +1365,15 @@ impl<'a> Parser<'a> {
13651365
match ty.node {
13661366
// `(TY_BOUND_NOPAREN) + BOUND + ...`.
13671367
TyKind::Path(None, ref path) if maybe_bounds => {
1368-
self.bump(); // `+`
1369-
let pt = PolyTraitRef::new(Vec::new(), path.clone(), lo.to(self.prev_span));
1370-
let mut bounds = vec![TraitTyParamBound(pt, TraitBoundModifier::None)];
1371-
bounds.append(&mut self.parse_ty_param_bounds()?);
1372-
TyKind::TraitObject(bounds)
1368+
self.parse_remaining_bounds(Vec::new(), path.clone(), lo, true)?
13731369
}
13741370
TyKind::TraitObject(ref bounds)
13751371
if maybe_bounds && bounds.len() == 1 && !trailing_plus => {
1376-
self.bump(); // `+`
1377-
let mut bounds = bounds.clone();
1378-
bounds.append(&mut self.parse_ty_param_bounds()?);
1379-
TyKind::TraitObject(bounds)
1372+
let path = match bounds[0] {
1373+
TraitTyParamBound(ref pt, ..) => pt.trait_ref.path.clone(),
1374+
_ => self.bug("unexpected lifetime bound"),
1375+
};
1376+
self.parse_remaining_bounds(Vec::new(), path, lo, true)?
13801377
}
13811378
// `(TYPE)`
13821379
_ => TyKind::Paren(P(ty))
@@ -1429,11 +1426,8 @@ impl<'a> Parser<'a> {
14291426
// Just a type path or bound list (trait object type) starting with a trait.
14301427
// `Type`
14311428
// `Trait1 + Trait2 + 'a`
1432-
if allow_plus && self.eat(&token::BinOp(token::Plus)) {
1433-
let poly_trait = PolyTraitRef::new(Vec::new(), path, lo.to(self.prev_span));
1434-
let mut bounds = vec![TraitTyParamBound(poly_trait, TraitBoundModifier::None)];
1435-
bounds.append(&mut self.parse_ty_param_bounds()?);
1436-
TyKind::TraitObject(bounds)
1429+
if allow_plus && self.check(&token::BinOp(token::Plus)) {
1430+
self.parse_remaining_bounds(Vec::new(), path, lo, true)?
14371431
} else {
14381432
TyKind::Path(None, path)
14391433
}
@@ -1451,12 +1445,8 @@ impl<'a> Parser<'a> {
14511445
self.parse_ty_bare_fn(lifetime_defs)?
14521446
} else {
14531447
let path = self.parse_path(PathStyle::Type)?;
1454-
let poly_trait = PolyTraitRef::new(lifetime_defs, path, lo.to(self.prev_span));
1455-
let mut bounds = vec![TraitTyParamBound(poly_trait, TraitBoundModifier::None)];
1456-
if allow_plus && self.eat(&token::BinOp(token::Plus)) {
1457-
bounds.append(&mut self.parse_ty_param_bounds()?)
1458-
}
1459-
TyKind::TraitObject(bounds)
1448+
let parse_plus = allow_plus && self.check(&token::BinOp(token::Plus));
1449+
self.parse_remaining_bounds(lifetime_defs, path, lo, parse_plus)?
14601450
}
14611451
} else if self.eat_keyword(keywords::Impl) {
14621452
// FIXME: figure out priority of `+` in `impl Trait1 + Trait2` (#34511).
@@ -1479,6 +1469,17 @@ impl<'a> Parser<'a> {
14791469
Ok(P(ty))
14801470
}
14811471

1472+
fn parse_remaining_bounds(&mut self, lifetime_defs: Vec<LifetimeDef>, path: ast::Path,
1473+
lo: Span, parse_plus: bool) -> PResult<'a, TyKind> {
1474+
let poly_trait_ref = PolyTraitRef::new(lifetime_defs, path, lo.to(self.prev_span));
1475+
let mut bounds = vec![TraitTyParamBound(poly_trait_ref, TraitBoundModifier::None)];
1476+
if parse_plus {
1477+
self.bump(); // `+`
1478+
bounds.append(&mut self.parse_ty_param_bounds()?);
1479+
}
1480+
Ok(TyKind::TraitObject(bounds))
1481+
}
1482+
14821483
fn maybe_recover_from_bad_type_plus(&mut self, allow_plus: bool, ty: &Ty) -> PResult<'a, ()> {
14831484
// Do not add `+` to expected tokens.
14841485
if !allow_plus || self.token != token::BinOp(token::Plus) {

0 commit comments

Comments
 (0)