about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMarijn Haverbeke <marijnh@gmail.com>2011-12-16 10:42:28 +0100
committerMarijn Haverbeke <marijnh@gmail.com>2011-12-16 11:46:57 +0100
commitcff6bdd03616b6f20028ec8568d03363ccc3f9f2 (patch)
treee24a03d847e02bea41bded20923757d64df93da0
parent4f826d81f611aa9486b25e1e123b24131659bd51 (diff)
downloadrust-cff6bdd03616b6f20028ec8568d03363ccc3f9f2.tar.gz
rust-cff6bdd03616b6f20028ec8568d03363ccc3f9f2.zip
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()) { ... }
    }
-rw-r--r--src/comp/syntax/ast.rs2
-rw-r--r--src/comp/syntax/fold.rs4
-rw-r--r--src/comp/syntax/parse/parser.rs12
-rw-r--r--src/comp/syntax/print/pprust.rs15
-rw-r--r--src/comp/syntax/visit.rs3
-rw-r--r--src/test/run-pass/static-impl.rs18
6 files changed, 34 insertions, 20 deletions
diff --git a/src/comp/syntax/ast.rs b/src/comp/syntax/ast.rs
index 8fb6d41c0a8..fd79869c27b 100644
--- a/src/comp/syntax/ast.rs
+++ b/src/comp/syntax/ast.rs
@@ -505,7 +505,7 @@ tag item_ {
              node_id /* dtor id */,
              [ty_param],
              node_id /* ctor id */);
-    item_impl(@path /* iface */, @ty /* self */, [@method]);
+    item_impl([ty_param], @ty /* self */, [@method]);
 }
 
 type native_item =
diff --git a/src/comp/syntax/fold.rs b/src/comp/syntax/fold.rs
index 6bb95f0cfc0..aa949b11f34 100644
--- a/src/comp/syntax/fold.rs
+++ b/src/comp/syntax/fold.rs
@@ -235,8 +235,8 @@ fn noop_fold_item_underscore(i: item_, fld: ast_fold) -> item_ {
                       methods: vec::map(fld.fold_method, o.methods)},
                      typms, d)
           }
-          item_impl(iface, ty, methods) {
-            item_impl(fld.fold_path(iface), fld.fold_ty(ty),
+          item_impl(tps, ty, methods) {
+            item_impl(tps, fld.fold_ty(ty),
                       vec::map(fld.fold_method, methods))
           }
           item_res(dtor, did, typms, cid) {
diff --git a/src/comp/syntax/parse/parser.rs b/src/comp/syntax/parse/parser.rs
index 94a7c3e01b2..3d00c333ad4 100644
--- a/src/comp/syntax/parse/parser.rs
+++ b/src/comp/syntax/parse/parser.rs
@@ -1859,14 +1859,14 @@ fn parse_item_obj(p: parser, attrs: [ast::attribute]) -> @ast::item {
 }
 
 fn parse_item_impl(p: parser, attrs: [ast::attribute]) -> @ast::item {
-    let lo = p.get_last_lo_pos(), ty = parse_ty(p, false);
-    expect(p, token::COLON);
-    let path = parse_path(p), meths = [];
+    let lo = p.get_last_lo_pos(), ident = parse_ident(p),
+        tps = parse_ty_params(p);
+    expect_word(p, "for");
+    let ty = parse_ty(p, false), meths = [];
     expect(p, token::LBRACE);
     while !eat(p, token::RBRACE) { meths += [parse_method(p)]; }
-    ret mk_item(p, lo, p.get_last_hi_pos(),
-                path.node.idents[vec::len(path.node.idents) - 1u],
-                ast::item_impl(path, ty, meths), attrs);
+    ret mk_item(p, lo, p.get_last_hi_pos(), ident,
+                ast::item_impl(tps, ty, meths), attrs);
 }
 
 fn parse_item_res(p: parser, attrs: [ast::attribute]) -> @ast::item {
diff --git a/src/comp/syntax/print/pprust.rs b/src/comp/syntax/print/pprust.rs
index 25ba7e082f3..54388102f53 100644
--- a/src/comp/syntax/print/pprust.rs
+++ b/src/comp/syntax/print/pprust.rs
@@ -347,12 +347,6 @@ fn print_native_item(s: ps, item: @ast::native_item) {
         end(s); // end the outer ibox
 
       }
-
-
-
-
-
-
       ast::native_item_fn(decl, typarams) {
         print_fn(s, decl, ast::proto_bare, item.ident, typarams,
                  decl.constraints);
@@ -483,11 +477,14 @@ fn print_item(s: ps, &&item: @ast::item) {
         }
         bclose(s, item.span);
       }
-      ast::item_impl(path, ty, methods) {
+      ast::item_impl(tps, ty, methods) {
         head(s, "impl");
+        word(s.s, item.ident);
+        print_type_params(s, tps);
+        nbsp(s);
+        word_nbsp(s, "for");
         print_type(s, ty);
-        word_space(s, ":");
-        print_path(s, path, false);
+        space(s.s);
         bopen(s);
         for meth in methods {
             hardbreak_if_not_bol(s);
diff --git a/src/comp/syntax/visit.rs b/src/comp/syntax/visit.rs
index c3e8c65b15a..5091c2d9ea0 100644
--- a/src/comp/syntax/visit.rs
+++ b/src/comp/syntax/visit.rs
@@ -105,8 +105,7 @@ fn visit_item<E>(i: @item, e: E, v: vt<E>) {
                        e, v);
         }
       }
-      item_impl(path, ty, methods) {
-        visit_path(path, e, v);
+      item_impl(_, ty, methods) {
         visit_ty(ty, e, v);
         for m in methods {
             v.visit_fn(m.node.meth, [], m.span, some(m.node.ident), m.node.id,
diff --git a/src/test/run-pass/static-impl.rs b/src/test/run-pass/static-impl.rs
new file mode 100644
index 00000000000..ab4fbc5915f
--- /dev/null
+++ b/src/test/run-pass/static-impl.rs
@@ -0,0 +1,18 @@
+import a::*;
+import b::baz;
+
+mod a {
+    impl foo for uint { fn plus() -> int { self as int + 20 } }
+}
+
+mod b {
+    impl baz for str { fn plus() -> int { 200 } }
+}
+
+fn main() {
+    impl foo for int { fn plus() -> int { self + 10 } }
+    assert 10.plus() == 20;
+    assert 10u.plus() == 30;
+    assert "hi".plus() == 200;
+}
+