1
Fork 0

libsyntax: Implement the impl Trait for Type syntax

This commit is contained in:
Patrick Walton 2013-01-29 11:14:53 -08:00
parent 1b021d5868
commit 4ead38bae7
2 changed files with 49 additions and 6 deletions

View file

@ -2791,8 +2791,9 @@ impl Parser {
(ident, item_trait(tps, traits, meths), None) (ident, item_trait(tps, traits, meths), None)
} }
// Parses four variants (with the region/type params always optional): // Parses two variants (with the region/type params always optional):
// impl<T> ~[T] : to_str { ... } // impl<T> ~[T] : to_str { ... }
// impl<T> to_str for ~[T] { ... }
fn parse_item_impl() -> item_info { fn parse_item_impl() -> item_info {
fn wrap_path(p: Parser, pt: @path) -> @Ty { fn wrap_path(p: Parser, pt: @path) -> @Ty {
@Ty { @Ty {
@ -2802,8 +2803,6 @@ impl Parser {
} }
} }
// We do two separate paths here: old-style impls and new-style impls.
// First, parse type parameters if necessary. // First, parse type parameters if necessary.
let mut tps; let mut tps;
if self.token == token::LT { if self.token == token::LT {
@ -2816,14 +2815,32 @@ impl Parser {
// XXX: clownshoes // XXX: clownshoes
let ident = special_idents::clownshoes_extensions; let ident = special_idents::clownshoes_extensions;
// Parse the type. // Parse the type. (If this is `impl trait for type`, however, this
let ty = self.parse_ty(false); // actually parses the trait.)
let mut ty = self.parse_ty(false);
// Parse traits, if necessary. // Parse traits, if necessary.
let opt_trait = if self.token == token::COLON { let opt_trait = if self.token == token::COLON {
// Old-style trait.
self.bump(); self.bump();
Some(self.parse_trait_ref()) Some(self.parse_trait_ref())
} else if self.eat_keyword(~"for") {
// New-style trait. Reinterpret the type as a trait.
let opt_trait_ref = match ty.node {
ty_path(path, node_id) => {
Some(@trait_ref {
path: path,
ref_id: node_id
})
}
_ => {
self.span_err(copy self.span, ~"not a trait");
None
}
};
ty = self.parse_ty(false);
opt_trait_ref
} else { } else {
None None
}; };

View file

@ -0,0 +1,26 @@
struct Thingy {
x: int,
y: int
}
impl ToStr for Thingy {
pure fn to_str() -> ~str {
fmt!("{ x: %d, y: %d }", self.x, self.y)
}
}
struct PolymorphicThingy<T> {
x: T
}
impl<T:ToStr> ToStr for PolymorphicThingy<T> {
pure fn to_str() -> ~str {
self.x.to_str()
}
}
fn main() {
io::println(Thingy { x: 1, y: 2 }.to_str());
io::println(PolymorphicThingy { x: Thingy { x: 1, y: 2 } }.to_str());
}