about summary refs log tree commit diff
path: root/src/libsyntax/parse
diff options
context:
space:
mode:
authorPatrick Walton <pcwalton@mimiga.net>2013-01-29 11:14:53 -0800
committerPatrick Walton <pcwalton@mimiga.net>2013-01-29 11:48:21 -0800
commit4ead38bae7ed244f5df9e71ebd1f739d5de0feb7 (patch)
tree5a1423be681f5d028bf9b95ad910cb2fea68c271 /src/libsyntax/parse
parent1b021d5868411341b969bfb684c7f1892cb7e092 (diff)
downloadrust-4ead38bae7ed244f5df9e71ebd1f739d5de0feb7.tar.gz
rust-4ead38bae7ed244f5df9e71ebd1f739d5de0feb7.zip
libsyntax: Implement the `impl Trait for Type` syntax
Diffstat (limited to 'src/libsyntax/parse')
-rw-r--r--src/libsyntax/parse/parser.rs29
1 files changed, 23 insertions, 6 deletions
diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs
index 0bd08250617..19a52c3550f 100644
--- a/src/libsyntax/parse/parser.rs
+++ b/src/libsyntax/parse/parser.rs
@@ -2791,8 +2791,9 @@ impl Parser {
         (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> to_str for ~[T] { ... }
     fn parse_item_impl() -> item_info {
         fn wrap_path(p: Parser, pt: @path) -> @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.
         let mut tps;
         if self.token == token::LT {
@@ -2816,14 +2815,32 @@ impl Parser {
         // XXX: clownshoes
         let ident = special_idents::clownshoes_extensions;
 
-        // Parse the type.
-        let ty = self.parse_ty(false);
-
+        // Parse the type. (If this is `impl trait for type`, however, this
+        // actually parses the trait.)
+        let mut ty = self.parse_ty(false);
 
         // Parse traits, if necessary.
         let opt_trait = if self.token == token::COLON {
+            // Old-style trait.
             self.bump();
             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 {
             None
         };