提交 c329a174 编写于 作者: B bors

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
......@@ -111,13 +111,6 @@ pub enum PathParsingMode {
LifetimeAndTypesAndBounds,
}
/// A pair of a path segment and group of type parameter bounds. (See `ast.rs`
/// for the definition of a path segment.)
struct PathSegmentAndBoundSet {
segment: ast::PathSegment,
bound_set: Option<OwnedSlice<TyParamBound>>,
}
/// A path paired with optional type bounds.
pub struct PathAndBounds {
path: ast::Path,
......@@ -1514,24 +1507,14 @@ pub fn parse_path(&mut self, mode: PathParsingMode) -> PathAndBounds {
// First, parse an identifier.
let identifier = self.parse_ident();
// Next, parse a colon and bounded type parameters, if applicable.
let bound_set = if mode == LifetimeAndTypesAndBounds {
self.parse_optional_ty_param_bounds()
} else {
None
};
// Parse the '::' before type parameters if it's required. If
// it is required and wasn't present, then we're done.
if mode == LifetimeAndTypesWithColons &&
!self.eat(&token::MOD_SEP) {
segments.push(PathSegmentAndBoundSet {
segment: ast::PathSegment {
identifier: identifier,
lifetimes: Vec::new(),
types: OwnedSlice::empty(),
},
bound_set: bound_set
segments.push(ast::PathSegment {
identifier: identifier,
lifetimes: Vec::new(),
types: OwnedSlice::empty(),
});
break
}
......@@ -1548,13 +1531,10 @@ pub fn parse_path(&mut self, mode: PathParsingMode) -> PathAndBounds {
};
// Assemble and push the result.
segments.push(PathSegmentAndBoundSet {
segment: ast::PathSegment {
identifier: identifier,
lifetimes: lifetimes,
types: types,
},
bound_set: bound_set
segments.push(ast::PathSegment {
identifier: identifier,
lifetimes: lifetimes,
types: types,
});
// We're done if we don't see a '::', unless the mode required
......@@ -1567,42 +1547,25 @@ pub fn parse_path(&mut self, mode: PathParsingMode) -> PathAndBounds {
}
}
// Next, parse a colon and bounded type parameters, if applicable.
let bounds = if mode == LifetimeAndTypesAndBounds {
self.parse_optional_ty_param_bounds()
} else {
None
};
// Assemble the span.
let span = mk_sp(lo, self.last_span.hi);
// Assemble the path segments.
let mut path_segments = Vec::new();
let mut bounds = None;
let last_segment_index = segments.len() - 1;
for (i, segment_and_bounds) in segments.move_iter().enumerate() {
let PathSegmentAndBoundSet {
segment: segment,
bound_set: bound_set
} = segment_and_bounds;
path_segments.push(segment);
if bound_set.is_some() {
if i != last_segment_index {
self.span_err(span,
"type parameter bounds are allowed only \
before the last segment in a path")
}
bounds = bound_set
}
}
// Assemble the result.
let path_and_bounds = PathAndBounds {
PathAndBounds {
path: ast::Path {
span: span,
global: is_global,
segments: path_segments,
segments: segments,
},
bounds: bounds,
};
path_and_bounds
}
}
/// parses 0 or 1 lifetime
......
......@@ -1526,7 +1526,7 @@ fn print_path_(&mut self,
}
let mut first = true;
for (i, segment) in path.segments.iter().enumerate() {
for segment in path.segments.iter() {
if first {
first = false
} else {
......@@ -1535,14 +1535,6 @@ fn print_path_(&mut self,
try!(self.print_ident(segment.identifier));
// If this is the last segment, print the bounds.
if i == path.segments.len() - 1 {
match *opt_bounds {
None => {}
Some(ref bounds) => try!(self.print_bounds(bounds, true)),
}
}
if !segment.lifetimes.is_empty() || !segment.types.is_empty() {
if colons_before_params {
try!(word(&mut self.s, "::"))
......@@ -1571,7 +1563,11 @@ fn print_path_(&mut self,
try!(word(&mut self.s, ">"))
}
}
Ok(())
match *opt_bounds {
None => Ok(()),
Some(ref bounds) => self.print_bounds(bounds, true),
}
}
fn print_path(&mut self, path: &ast::Path,
......
......@@ -14,8 +14,8 @@ impl<A:Clone> Repeat<A> for A {
fn get(&self) -> A { self.clone() }
}
fn repeater<A:Clone>(v: A) -> ~Repeat:<A> {
~v as ~Repeat:<A> // No
fn repeater<A:Clone>(v: A) -> ~Repeat<A>: {
~v as ~Repeat<A>: // No
}
fn main() {
......
......@@ -27,11 +27,11 @@ fn f(&self) -> (A, u16) {
}
}
fn f<A:Clone + 'static>(a: A, b: u16) -> ~Invokable:<A> {
fn f<A:Clone + 'static>(a: A, b: u16) -> ~Invokable<A>: {
~Invoker {
a: a,
b: b,
} as ~Invokable:<A>
} as ~Invokable<A>:
}
pub fn main() {
......
......@@ -31,11 +31,11 @@ fn f(&self) -> (A, u16) {
}
}
fn f<A:Clone + 'static>(a: A, b: u16) -> ~Invokable:<A> {
fn f<A:Clone + 'static>(a: A, b: u16) -> ~Invokable<A>: {
~Invoker {
a: a,
b: b,
} as ~Invokable:<A>
} as ~Invokable<A>:
}
pub fn main() {
......
......@@ -16,9 +16,9 @@ fn get(&self) -> A {
}
}
fn repeater<A:Clone + 'static>(v: ~A) -> ~repeat:<A> {
fn repeater<A:Clone + 'static>(v: ~A) -> ~repeat<A>: {
// Note: owned kind is not necessary as A appears in the trait type
~v as ~repeat:<A> // No
~v as ~repeat<A>: // No
}
pub fn main() {
......
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#[allow(dead_code)];
trait A<T> {}
trait B<T, U> {}
trait C<'a, U> {}
mod foo {
pub trait D<'a, T> {}
}
fn foo1<T>(_: &A<T>: Send) {}
fn foo2<T>(_: ~A<T>: Send + Share) {}
fn foo3<T>(_: ~B<int, uint>: 'static) {}
fn foo4<'a, T>(_: ~C<'a, T>: 'static + Send) {}
fn foo5<'a, T>(_: ~foo::D<'a, T>: 'static + Send) {}
pub fn main() {}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册