Skip to content

Commit c329a17

Browse files
committed
auto merge of #13079 : alexcrichton/rust/colons, r=cmr
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
2 parents c83994e + 8118406 commit c329a17

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
@@ -111,13 +111,6 @@ pub enum PathParsingMode {
111111
LifetimeAndTypesAndBounds,
112112
}
113113

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

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

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

15601540
// We're done if we don't see a '::', unless the mode required
@@ -1567,42 +1547,25 @@ impl<'a> Parser<'a> {
15671547
}
15681548
}
15691549

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

1573-
// Assemble the path segments.
1574-
let mut path_segments = Vec::new();
1575-
let mut bounds = None;
1576-
let last_segment_index = segments.len() - 1;
1577-
for (i, segment_and_bounds) in segments.move_iter().enumerate() {
1578-
let PathSegmentAndBoundSet {
1579-
segment: segment,
1580-
bound_set: bound_set
1581-
} = segment_and_bounds;
1582-
path_segments.push(segment);
1583-
1584-
if bound_set.is_some() {
1585-
if i != last_segment_index {
1586-
self.span_err(span,
1587-
"type parameter bounds are allowed only \
1588-
before the last segment in a path")
1589-
}
1590-
1591-
bounds = bound_set
1592-
}
1593-
}
1594-
15951560
// Assemble the result.
1596-
let path_and_bounds = PathAndBounds {
1561+
PathAndBounds {
15971562
path: ast::Path {
15981563
span: span,
15991564
global: is_global,
1600-
segments: path_segments,
1565+
segments: segments,
16011566
},
16021567
bounds: bounds,
1603-
};
1604-
1605-
path_and_bounds
1568+
}
16061569
}
16071570

16081571
/// parses 0 or 1 lifetime

src/libsyntax/print/pprust.rs

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

15281528
let mut first = true;
1529-
for (i, segment) in path.segments.iter().enumerate() {
1529+
for segment in path.segments.iter() {
15301530
if first {
15311531
first = false
15321532
} else {
@@ -1535,14 +1535,6 @@ impl<'a> State<'a> {
15351535

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

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

15771573
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)