Skip to content

Commit 8118406

Browse files
committed
syntax: Tweak parsing bounds on generics paths
The previous syntax was `Foo:Bound<trait-parameters>`, but this is a little ambiguous because it was being parsed as `Foo: (Bound<trait-parameters)` rather than `Foo: (Bound) <trait-parameters>` This commit changes the syntax to `Foo<trait-parameters>: Bound` in order to be clear where the trait parameters are going. Closes #9265
1 parent e233a43 commit 8118406

7 files changed

+59
-73
lines changed

src/libsyntax/parse/parser.rs

Lines changed: 18 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,6 @@ pub enum PathParsingMode {
112112
LifetimeAndTypesAndBounds,
113113
}
114114

115-
/// A pair of a path segment and group of type parameter bounds. (See `ast.rs`
116-
/// for the definition of a path segment.)
117-
struct PathSegmentAndBoundSet {
118-
segment: ast::PathSegment,
119-
bound_set: Option<OwnedSlice<TyParamBound>>,
120-
}
121-
122115
/// A path paired with optional type bounds.
123116
pub struct PathAndBounds {
124117
path: ast::Path,
@@ -1515,24 +1508,14 @@ impl<'a> Parser<'a> {
15151508
// First, parse an identifier.
15161509
let identifier = self.parse_ident();
15171510

1518-
// Next, parse a colon and bounded type parameters, if applicable.
1519-
let bound_set = if mode == LifetimeAndTypesAndBounds {
1520-
self.parse_optional_ty_param_bounds()
1521-
} else {
1522-
None
1523-
};
1524-
15251511
// Parse the '::' before type parameters if it's required. If
15261512
// it is required and wasn't present, then we're done.
15271513
if mode == LifetimeAndTypesWithColons &&
15281514
!self.eat(&token::MOD_SEP) {
1529-
segments.push(PathSegmentAndBoundSet {
1530-
segment: ast::PathSegment {
1531-
identifier: identifier,
1532-
lifetimes: Vec::new(),
1533-
types: OwnedSlice::empty(),
1534-
},
1535-
bound_set: bound_set
1515+
segments.push(ast::PathSegment {
1516+
identifier: identifier,
1517+
lifetimes: Vec::new(),
1518+
types: OwnedSlice::empty(),
15361519
});
15371520
break
15381521
}
@@ -1549,13 +1532,10 @@ impl<'a> Parser<'a> {
15491532
};
15501533

15511534
// Assemble and push the result.
1552-
segments.push(PathSegmentAndBoundSet {
1553-
segment: ast::PathSegment {
1554-
identifier: identifier,
1555-
lifetimes: lifetimes,
1556-
types: types,
1557-
},
1558-
bound_set: bound_set
1535+
segments.push(ast::PathSegment {
1536+
identifier: identifier,
1537+
lifetimes: lifetimes,
1538+
types: types,
15591539
});
15601540

15611541
// We're done if we don't see a '::', unless the mode required
@@ -1568,42 +1548,25 @@ impl<'a> Parser<'a> {
15681548
}
15691549
}
15701550

1551+
// Next, parse a colon and bounded type parameters, if applicable.
1552+
let bounds = if mode == LifetimeAndTypesAndBounds {
1553+
self.parse_optional_ty_param_bounds()
1554+
} else {
1555+
None
1556+
};
1557+
15711558
// Assemble the span.
15721559
let span = mk_sp(lo, self.last_span.hi);
15731560

1574-
// Assemble the path segments.
1575-
let mut path_segments = Vec::new();
1576-
let mut bounds = None;
1577-
let last_segment_index = segments.len() - 1;
1578-
for (i, segment_and_bounds) in segments.move_iter().enumerate() {
1579-
let PathSegmentAndBoundSet {
1580-
segment: segment,
1581-
bound_set: bound_set
1582-
} = segment_and_bounds;
1583-
path_segments.push(segment);
1584-
1585-
if bound_set.is_some() {
1586-
if i != last_segment_index {
1587-
self.span_err(span,
1588-
"type parameter bounds are allowed only \
1589-
before the last segment in a path")
1590-
}
1591-
1592-
bounds = bound_set
1593-
}
1594-
}
1595-
15961561
// Assemble the result.
1597-
let path_and_bounds = PathAndBounds {
1562+
PathAndBounds {
15981563
path: ast::Path {
15991564
span: span,
16001565
global: is_global,
1601-
segments: path_segments,
1566+
segments: segments,
16021567
},
16031568
bounds: bounds,
1604-
};
1605-
1606-
path_and_bounds
1569+
}
16071570
}
16081571

16091572
/// parses 0 or 1 lifetime

src/libsyntax/print/pprust.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1525,7 +1525,7 @@ impl<'a> State<'a> {
15251525
}
15261526

15271527
let mut first = true;
1528-
for (i, segment) in path.segments.iter().enumerate() {
1528+
for segment in path.segments.iter() {
15291529
if first {
15301530
first = false
15311531
} else {
@@ -1534,14 +1534,6 @@ impl<'a> State<'a> {
15341534

15351535
try!(self.print_ident(segment.identifier));
15361536

1537-
// If this is the last segment, print the bounds.
1538-
if i == path.segments.len() - 1 {
1539-
match *opt_bounds {
1540-
None => {}
1541-
Some(ref bounds) => try!(self.print_bounds(bounds, true)),
1542-
}
1543-
}
1544-
15451537
if !segment.lifetimes.is_empty() || !segment.types.is_empty() {
15461538
if colons_before_params {
15471539
try!(word(&mut self.s, "::"))
@@ -1570,7 +1562,11 @@ impl<'a> State<'a> {
15701562
try!(word(&mut self.s, ">"))
15711563
}
15721564
}
1573-
Ok(())
1565+
1566+
match *opt_bounds {
1567+
None => Ok(()),
1568+
Some(ref bounds) => self.print_bounds(bounds, true),
1569+
}
15741570
}
15751571

15761572
fn print_path(&mut self, path: &ast::Path,

src/test/compile-fail/kindck-owned-trait-contains.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ impl<A:Clone> Repeat<A> for A {
1414
fn get(&self) -> A { self.clone() }
1515
}
1616

17-
fn repeater<A:Clone>(v: A) -> ~Repeat:<A> {
18-
~v as ~Repeat:<A> // No
17+
fn repeater<A:Clone>(v: A) -> ~Repeat<A>: {
18+
~v as ~Repeat<A>: // No
1919
}
2020

2121
fn main() {

src/test/run-pass/alignment-gep-tup-like-1.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ impl<A:Clone> Invokable<A> for Invoker<A> {
2727
}
2828
}
2929

30-
fn f<A:Clone + 'static>(a: A, b: u16) -> ~Invokable:<A> {
30+
fn f<A:Clone + 'static>(a: A, b: u16) -> ~Invokable<A>: {
3131
~Invoker {
3232
a: a,
3333
b: b,
34-
} as ~Invokable:<A>
34+
} as ~Invokable<A>:
3535
}
3636

3737
pub fn main() {

src/test/run-pass/close-over-big-then-small-data.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ impl<A:Clone> Invokable<A> for Invoker<A> {
3131
}
3232
}
3333

34-
fn f<A:Clone + 'static>(a: A, b: u16) -> ~Invokable:<A> {
34+
fn f<A:Clone + 'static>(a: A, b: u16) -> ~Invokable<A>: {
3535
~Invoker {
3636
a: a,
3737
b: b,
38-
} as ~Invokable:<A>
38+
} as ~Invokable<A>:
3939
}
4040

4141
pub fn main() {

src/test/run-pass/kindck-owned-trait-contains-1.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ impl<A:Clone + 'static> repeat<A> for ~A {
1616
}
1717
}
1818

19-
fn repeater<A:Clone + 'static>(v: ~A) -> ~repeat:<A> {
19+
fn repeater<A:Clone + 'static>(v: ~A) -> ~repeat<A>: {
2020
// Note: owned kind is not necessary as A appears in the trait type
21-
~v as ~repeat:<A> // No
21+
~v as ~repeat<A>: // No
2222
}
2323

2424
pub fn main() {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2014 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+
#[allow(dead_code)];
12+
13+
trait A<T> {}
14+
trait B<T, U> {}
15+
trait C<'a, U> {}
16+
17+
mod foo {
18+
pub trait D<'a, T> {}
19+
}
20+
21+
fn foo1<T>(_: &A<T>: Send) {}
22+
fn foo2<T>(_: ~A<T>: Send + Share) {}
23+
fn foo3<T>(_: ~B<int, uint>: 'static) {}
24+
fn foo4<'a, T>(_: ~C<'a, T>: 'static + Send) {}
25+
fn foo5<'a, T>(_: ~foo::D<'a, T>: 'static + Send) {}
26+
27+
pub fn main() {}

0 commit comments

Comments
 (0)