Skip to content

Commit e038f58

Browse files
committed
syntax: Support parentheses around trait bounds
1 parent 5695c3e commit e038f58

File tree

4 files changed

+84
-4
lines changed

4 files changed

+84
-4
lines changed

src/libsyntax/parse/parser.rs

+25-4
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ fn maybe_append(mut lhs: Vec<Attribute>, rhs: Option<Vec<Attribute>>)
152152
enum PrevTokenKind {
153153
DocComment,
154154
Comma,
155+
Plus,
155156
Interpolated,
156157
Eof,
157158
Other,
@@ -1061,6 +1062,7 @@ impl<'a> Parser<'a> {
10611062
self.prev_token_kind = match self.token {
10621063
token::DocComment(..) => PrevTokenKind::DocComment,
10631064
token::Comma => PrevTokenKind::Comma,
1065+
token::BinOp(token::Plus) => PrevTokenKind::Plus,
10641066
token::Interpolated(..) => PrevTokenKind::Interpolated,
10651067
token::Eof => PrevTokenKind::Eof,
10661068
_ => PrevTokenKind::Other,
@@ -1354,20 +1356,29 @@ impl<'a> Parser<'a> {
13541356
break;
13551357
}
13561358
}
1359+
let trailing_plus = self.prev_token_kind == PrevTokenKind::Plus;
13571360
self.expect(&token::CloseDelim(token::Paren))?;
13581361

13591362
if ts.len() == 1 && !last_comma {
13601363
let ty = ts.into_iter().nth(0).unwrap().unwrap();
1364+
let maybe_bounds = allow_plus && self.token == token::BinOp(token::Plus);
13611365
match ty.node {
1362-
// Accept `(Trait1) + Trait2 + 'a` for backward compatibility (#39318).
1363-
TyKind::Path(None, ref path)
1364-
if allow_plus && self.token == token::BinOp(token::Plus) => {
1366+
// `(TY_BOUND_NOPAREN) + BOUND + ...`.
1367+
TyKind::Path(None, ref path) if maybe_bounds => {
13651368
self.bump(); // `+`
13661369
let pt = PolyTraitRef::new(Vec::new(), path.clone(), lo.to(self.prev_span));
13671370
let mut bounds = vec![TraitTyParamBound(pt, TraitBoundModifier::None)];
13681371
bounds.append(&mut self.parse_ty_param_bounds()?);
13691372
TyKind::TraitObject(bounds)
13701373
}
1374+
TyKind::TraitObject(ref bounds)
1375+
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)
1380+
}
1381+
// `(TYPE)`
13711382
_ => TyKind::Paren(P(ty))
13721383
}
13731384
} else {
@@ -4070,17 +4081,24 @@ impl<'a> Parser<'a> {
40704081
// Parse bounds of a type parameter `BOUND + BOUND + BOUND`, possibly with trailing `+`.
40714082
// BOUND = TY_BOUND | LT_BOUND
40724083
// LT_BOUND = LIFETIME (e.g. `'a`)
4073-
// TY_BOUND = [?] [for<LT_PARAM_DEFS>] SIMPLE_PATH (e.g. `?for<'a: 'b> m::Trait<'a>`)
4084+
// TY_BOUND = TY_BOUND_NOPAREN | (TY_BOUND_NOPAREN)
4085+
// TY_BOUND_NOPAREN = [?] [for<LT_PARAM_DEFS>] SIMPLE_PATH (e.g. `?for<'a: 'b> m::Trait<'a>`)
40744086
fn parse_ty_param_bounds_common(&mut self, allow_plus: bool) -> PResult<'a, TyParamBounds> {
40754087
let mut bounds = Vec::new();
40764088
loop {
4089+
let has_parens = self.eat(&token::OpenDelim(token::Paren));
40774090
let question = if self.eat(&token::Question) { Some(self.prev_span) } else { None };
40784091
if self.check_lifetime() {
40794092
if let Some(question_span) = question {
40804093
self.span_err(question_span,
40814094
"`?` may only modify trait bounds, not lifetime bounds");
40824095
}
40834096
bounds.push(RegionTyParamBound(self.expect_lifetime()));
4097+
if has_parens {
4098+
self.expect(&token::CloseDelim(token::Paren))?;
4099+
self.span_err(self.prev_span,
4100+
"parenthesized lifetime bounds are not supported");
4101+
}
40844102
} else if self.check_keyword(keywords::For) || self.check_path() {
40854103
let lo = self.span;
40864104
let lifetime_defs = self.parse_late_bound_lifetime_defs()?;
@@ -4092,6 +4110,9 @@ impl<'a> Parser<'a> {
40924110
TraitBoundModifier::None
40934111
};
40944112
bounds.push(TraitTyParamBound(poly_trait, modifier));
4113+
if has_parens {
4114+
self.expect(&token::CloseDelim(token::Paren))?;
4115+
}
40954116
} else {
40964117
break
40974118
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// compile-flags: -Z parse-only -Z continue-parse-after-error
12+
13+
fn main() {
14+
let _: Box<((Copy)) + Copy>;
15+
//~^ ERROR expected a path on the left-hand side of `+`, not `((Copy))`
16+
let _: Box<(Copy + Copy) + Copy>;
17+
//~^ ERROR expected a path on the left-hand side of `+`, not `( Copy + Copy)`
18+
let _: Box<(Copy +) + Copy>;
19+
//~^ ERROR expected a path on the left-hand side of `+`, not `( Copy)`
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// compile-flags: -Z parse-only -Z continue-parse-after-error
12+
13+
fn f<T: Copy + ('a)>() {} //~ ERROR parenthesized lifetime bounds are not supported
14+
15+
fn main() {
16+
let _: Box<Copy + ('a)>; //~ ERROR parenthesized lifetime bounds are not supported
17+
let _: Box<('a) + Copy>; //~ ERROR expected type, found `'a`
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// compile-flags: -Z parse-only
12+
13+
fn f<T: (Copy) + (?Sized) + (for<'a> Trait<'a>)>() {}
14+
15+
fn main() {
16+
let _: Box<(Copy) + (?Sized) + (for<'a> Trait<'a>)>;
17+
let _: Box<(?Sized) + (for<'a> Trait<'a>) + (Copy)>;
18+
let _: Box<(for<'a> Trait<'a>) + (Copy) + (?Sized)>;
19+
}
20+
21+
FAIL //~ ERROR

0 commit comments

Comments
 (0)