Skip to content

Commit e1a118f

Browse files
committed
---
yaml --- r: 42092 b: refs/heads/master c: 4ead38b h: refs/heads/master v: v3
1 parent b2ce5b8 commit e1a118f

File tree

3 files changed

+50
-7
lines changed

3 files changed

+50
-7
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: 1b021d5868411341b969bfb684c7f1892cb7e092
2+
refs/heads/master: 4ead38bae7ed244f5df9e71ebd1f739d5de0feb7
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 2f46b763da2c098913884f101b6d71d69af41b49
55
refs/heads/try: 3d5418789064fdb463e872a4e651af1c628a3650

trunk/src/libsyntax/parse/parser.rs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2791,8 +2791,9 @@ impl Parser {
27912791
(ident, item_trait(tps, traits, meths), None)
27922792
}
27932793

2794-
// Parses four variants (with the region/type params always optional):
2794+
// Parses two variants (with the region/type params always optional):
27952795
// impl<T> ~[T] : to_str { ... }
2796+
// impl<T> to_str for ~[T] { ... }
27962797
fn parse_item_impl() -> item_info {
27972798
fn wrap_path(p: Parser, pt: @path) -> @Ty {
27982799
@Ty {
@@ -2802,8 +2803,6 @@ impl Parser {
28022803
}
28032804
}
28042805

2805-
// We do two separate paths here: old-style impls and new-style impls.
2806-
28072806
// First, parse type parameters if necessary.
28082807
let mut tps;
28092808
if self.token == token::LT {
@@ -2816,14 +2815,32 @@ impl Parser {
28162815
// XXX: clownshoes
28172816
let ident = special_idents::clownshoes_extensions;
28182817

2819-
// Parse the type.
2820-
let ty = self.parse_ty(false);
2821-
2818+
// Parse the type. (If this is `impl trait for type`, however, this
2819+
// actually parses the trait.)
2820+
let mut ty = self.parse_ty(false);
28222821

28232822
// Parse traits, if necessary.
28242823
let opt_trait = if self.token == token::COLON {
2824+
// Old-style trait.
28252825
self.bump();
28262826
Some(self.parse_trait_ref())
2827+
} else if self.eat_keyword(~"for") {
2828+
// New-style trait. Reinterpret the type as a trait.
2829+
let opt_trait_ref = match ty.node {
2830+
ty_path(path, node_id) => {
2831+
Some(@trait_ref {
2832+
path: path,
2833+
ref_id: node_id
2834+
})
2835+
}
2836+
_ => {
2837+
self.span_err(copy self.span, ~"not a trait");
2838+
None
2839+
}
2840+
};
2841+
2842+
ty = self.parse_ty(false);
2843+
opt_trait_ref
28272844
} else {
28282845
None
28292846
};
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
struct Thingy {
2+
x: int,
3+
y: int
4+
}
5+
6+
impl ToStr for Thingy {
7+
pure fn to_str() -> ~str {
8+
fmt!("{ x: %d, y: %d }", self.x, self.y)
9+
}
10+
}
11+
12+
struct PolymorphicThingy<T> {
13+
x: T
14+
}
15+
16+
impl<T:ToStr> ToStr for PolymorphicThingy<T> {
17+
pure fn to_str() -> ~str {
18+
self.x.to_str()
19+
}
20+
}
21+
22+
fn main() {
23+
io::println(Thingy { x: 1, y: 2 }.to_str());
24+
io::println(PolymorphicThingy { x: Thingy { x: 1, y: 2 } }.to_str());
25+
}
26+

0 commit comments

Comments
 (0)