Skip to content

Commit cff6bdd

Browse files
committed
Change syntax for impl
Move the name of the bundle to the front, allow type parameters (not handled yet), and add a 'for' keyword: impl utils for int { fn str() -> str { int::str(self) } fn times(f: block()) { ... } }
1 parent 4f826d8 commit cff6bdd

File tree

6 files changed

+34
-20
lines changed

6 files changed

+34
-20
lines changed

src/comp/syntax/ast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ tag item_ {
505505
node_id /* dtor id */,
506506
[ty_param],
507507
node_id /* ctor id */);
508-
item_impl(@path /* iface */, @ty /* self */, [@method]);
508+
item_impl([ty_param], @ty /* self */, [@method]);
509509
}
510510

511511
type native_item =

src/comp/syntax/fold.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,8 @@ fn noop_fold_item_underscore(i: item_, fld: ast_fold) -> item_ {
235235
methods: vec::map(fld.fold_method, o.methods)},
236236
typms, d)
237237
}
238-
item_impl(iface, ty, methods) {
239-
item_impl(fld.fold_path(iface), fld.fold_ty(ty),
238+
item_impl(tps, ty, methods) {
239+
item_impl(tps, fld.fold_ty(ty),
240240
vec::map(fld.fold_method, methods))
241241
}
242242
item_res(dtor, did, typms, cid) {

src/comp/syntax/parse/parser.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1859,14 +1859,14 @@ fn parse_item_obj(p: parser, attrs: [ast::attribute]) -> @ast::item {
18591859
}
18601860

18611861
fn parse_item_impl(p: parser, attrs: [ast::attribute]) -> @ast::item {
1862-
let lo = p.get_last_lo_pos(), ty = parse_ty(p, false);
1863-
expect(p, token::COLON);
1864-
let path = parse_path(p), meths = [];
1862+
let lo = p.get_last_lo_pos(), ident = parse_ident(p),
1863+
tps = parse_ty_params(p);
1864+
expect_word(p, "for");
1865+
let ty = parse_ty(p, false), meths = [];
18651866
expect(p, token::LBRACE);
18661867
while !eat(p, token::RBRACE) { meths += [parse_method(p)]; }
1867-
ret mk_item(p, lo, p.get_last_hi_pos(),
1868-
path.node.idents[vec::len(path.node.idents) - 1u],
1869-
ast::item_impl(path, ty, meths), attrs);
1868+
ret mk_item(p, lo, p.get_last_hi_pos(), ident,
1869+
ast::item_impl(tps, ty, meths), attrs);
18701870
}
18711871

18721872
fn parse_item_res(p: parser, attrs: [ast::attribute]) -> @ast::item {

src/comp/syntax/print/pprust.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -347,12 +347,6 @@ fn print_native_item(s: ps, item: @ast::native_item) {
347347
end(s); // end the outer ibox
348348

349349
}
350-
351-
352-
353-
354-
355-
356350
ast::native_item_fn(decl, typarams) {
357351
print_fn(s, decl, ast::proto_bare, item.ident, typarams,
358352
decl.constraints);
@@ -483,11 +477,14 @@ fn print_item(s: ps, &&item: @ast::item) {
483477
}
484478
bclose(s, item.span);
485479
}
486-
ast::item_impl(path, ty, methods) {
480+
ast::item_impl(tps, ty, methods) {
487481
head(s, "impl");
482+
word(s.s, item.ident);
483+
print_type_params(s, tps);
484+
nbsp(s);
485+
word_nbsp(s, "for");
488486
print_type(s, ty);
489-
word_space(s, ":");
490-
print_path(s, path, false);
487+
space(s.s);
491488
bopen(s);
492489
for meth in methods {
493490
hardbreak_if_not_bol(s);

src/comp/syntax/visit.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,7 @@ fn visit_item<E>(i: @item, e: E, v: vt<E>) {
105105
e, v);
106106
}
107107
}
108-
item_impl(path, ty, methods) {
109-
visit_path(path, e, v);
108+
item_impl(_, ty, methods) {
110109
visit_ty(ty, e, v);
111110
for m in methods {
112111
v.visit_fn(m.node.meth, [], m.span, some(m.node.ident), m.node.id,

src/test/run-pass/static-impl.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import a::*;
2+
import b::baz;
3+
4+
mod a {
5+
impl foo for uint { fn plus() -> int { self as int + 20 } }
6+
}
7+
8+
mod b {
9+
impl baz for str { fn plus() -> int { 200 } }
10+
}
11+
12+
fn main() {
13+
impl foo for int { fn plus() -> int { self + 10 } }
14+
assert 10.plus() == 20;
15+
assert 10u.plus() == 30;
16+
assert "hi".plus() == 200;
17+
}
18+

0 commit comments

Comments
 (0)