about summary refs log tree commit diff
path: root/src/libsyntax
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2013-03-19 12:43:14 -0700
committerbors <bors@rust-lang.org>2013-03-19 12:43:14 -0700
commite1888948c640f8b8396091b336c2677cb82fdcce (patch)
treeff3d02994510a741e117adf3d0f57d39b3623a1f /src/libsyntax
parenta14ec73cd2d15a2454113011835557ccf447f14d (diff)
parenta6187c62e93ba96dce8f19849fb7016f4f941f8c (diff)
downloadrust-e1888948c640f8b8396091b336c2677cb82fdcce.tar.gz
rust-e1888948c640f8b8396091b336c2677cb82fdcce.zip
auto merge of #5426 : nikomatsakis/rust/issue-4846-lifetimes-in-expl-self, r=pcwalton
(this will be needed for snapshotting at some point)

r? @pcwalton
Diffstat (limited to 'src/libsyntax')
-rw-r--r--src/libsyntax/ast.rs14
-rw-r--r--src/libsyntax/ext/auto_encode.rs6
-rw-r--r--src/libsyntax/ext/deriving.rs6
-rw-r--r--src/libsyntax/parse/parser.rs63
-rw-r--r--src/libsyntax/print/pprust.rs25
5 files changed, 90 insertions, 24 deletions
diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs
index 65eeff5bbab..a2d82bb8ccd 100644
--- a/src/libsyntax/ast.rs
+++ b/src/libsyntax/ast.rs
@@ -1001,18 +1001,18 @@ impl to_bytes::IterBytes for ret_style {
 #[auto_decode]
 #[deriving_eq]
 pub enum self_ty_ {
-    sty_static,                         // no self: static method
-    sty_by_ref,                         // old by-reference self: ``
-    sty_value,                          // by-value self: `self`
-    sty_region(mutability),             // by-region self: `&self`
-    sty_box(mutability),                // by-managed-pointer self: `@self`
-    sty_uniq(mutability)                // by-unique-pointer self: `~self`
+    sty_static,                                // no self
+    sty_by_ref,                                // ``
+    sty_value,                                 // `self`
+    sty_region(Option<@Lifetime>, mutability), // `&'lt self`
+    sty_box(mutability),                       // `@self`
+    sty_uniq(mutability)                       // `~self`
 }
 
 impl self_ty_ {
     fn is_borrowed(&self) -> bool {
         match *self {
-            sty_region(_) => true,
+            sty_region(*) => true,
             _ => false
         }
     }
diff --git a/src/libsyntax/ext/auto_encode.rs b/src/libsyntax/ext/auto_encode.rs
index 8c02b432371..294174dc8f9 100644
--- a/src/libsyntax/ext/auto_encode.rs
+++ b/src/libsyntax/ext/auto_encode.rs
@@ -634,8 +634,10 @@ fn mk_ser_method(
         ident: cx.ident_of(~"encode"),
         attrs: ~[],
         generics: ast_util::empty_generics(),
-        self_ty: codemap::spanned { node: ast::sty_region(ast::m_imm),
-                                span: span },
+        self_ty: codemap::spanned {
+            node: ast::sty_region(None, ast::m_imm),
+            span: span
+        },
         purity: ast::impure_fn,
         decl: ser_decl,
         body: ser_body,
diff --git a/src/libsyntax/ext/deriving.rs b/src/libsyntax/ext/deriving.rs
index 76ab21f403b..a6165e50c85 100644
--- a/src/libsyntax/ext/deriving.rs
+++ b/src/libsyntax/ext/deriving.rs
@@ -220,7 +220,7 @@ fn create_eq_method(cx: @ext_ctxt,
     let body_block = build::mk_simple_block(cx, span, body);
 
     // Create the method.
-    let self_ty = spanned { node: sty_region(m_imm), span: span };
+    let self_ty = spanned { node: sty_region(None, m_imm), span: span };
     @ast::method {
         ident: method_ident,
         attrs: ~[],
@@ -398,7 +398,7 @@ fn create_iter_bytes_method(cx: @ext_ctxt,
     let body_block = build::mk_block_(cx, span, statements);
 
     // Create the method.
-    let self_ty = spanned { node: sty_region(m_imm), span: span };
+    let self_ty = spanned { node: sty_region(None, m_imm), span: span };
     let method_ident = cx.ident_of(~"iter_bytes");
     @ast::method {
         ident: method_ident,
@@ -448,7 +448,7 @@ fn create_clone_method(cx: @ext_ctxt,
     let body_block = build::mk_simple_block(cx, span, expr);
 
     // Create the self type and method identifier.
-    let self_ty = spanned { node: sty_region(m_imm), span: span };
+    let self_ty = spanned { node: sty_region(None, m_imm), span: span };
     let method_ident = cx.ident_of(~"clone");
 
     // Create the method.
diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs
index c7e93635d4c..6f4d9332df8 100644
--- a/src/libsyntax/parse/parser.rs
+++ b/src/libsyntax/parse/parser.rs
@@ -966,6 +966,13 @@ pub impl Parser {
         }
     }
 
+    fn token_is_lifetime(&self, tok: &token::Token) -> bool {
+        match *tok {
+            token::LIFETIME(_) => true,
+            _ => false
+        }
+    }
+
     fn parse_lifetime(&self) -> ast::Lifetime {
         /*!
          *
@@ -1033,6 +1040,11 @@ pub impl Parser {
         }
     }
 
+    fn token_is_mutability(&self, tok: &token::Token) -> bool {
+        self.token_is_keyword(&~"mut", tok) ||
+        self.token_is_keyword(&~"const", tok)
+    }
+
     fn parse_mutability(&self) -> mutability {
         if self.eat_keyword(&~"mut") {
             m_mutbl
@@ -2837,6 +2849,55 @@ pub impl Parser {
             }
         }
 
+        fn maybe_parse_borrowed_self_ty(
+            self: &Parser
+        ) -> ast::self_ty_ {
+            // The following things are possible to see here:
+            //
+            //     fn(&self)
+            //     fn(&mut self)
+            //     fn(&'lt self)
+            //     fn(&'lt mut self)
+            //
+            // We already know that the current token is `&`.
+
+            if (
+                self.token_is_keyword(&~"self", &self.look_ahead(1)))
+            {
+                self.bump();
+                self.expect_self_ident();
+                sty_region(None, m_imm)
+            } else if (
+                self.token_is_mutability(&self.look_ahead(1)) &&
+                self.token_is_keyword(&~"self", &self.look_ahead(2)))
+            {
+                self.bump();
+                let mutability = self.parse_mutability();
+                self.expect_self_ident();
+                sty_region(None, mutability)
+            } else if (
+                self.token_is_lifetime(&self.look_ahead(1)) &&
+                self.token_is_keyword(&~"self", &self.look_ahead(2)))
+            {
+                self.bump();
+                let lifetime = @self.parse_lifetime();
+                self.expect_self_ident();
+                sty_region(Some(lifetime), m_imm)
+            } else if (
+                self.token_is_lifetime(&self.look_ahead(1)) &&
+                self.token_is_mutability(&self.look_ahead(2)) &&
+                self.token_is_keyword(&~"self", &self.look_ahead(3)))
+            {
+                self.bump();
+                let lifetime = @self.parse_lifetime();
+                let mutability = self.parse_mutability();
+                self.expect_self_ident();
+                sty_region(Some(lifetime), mutability)
+            } else {
+                sty_by_ref
+            }
+        }
+
         self.expect(&token::LPAREN);
 
         // A bit of complexity and lookahead is needed here in order to to be
@@ -2844,7 +2905,7 @@ pub impl Parser {
         let lo = self.span.lo;
         let self_ty = match *self.token {
           token::BINOP(token::AND) => {
-            maybe_parse_self_ty(sty_region, self)
+            maybe_parse_borrowed_self_ty(self)
           }
           token::AT => {
             maybe_parse_self_ty(sty_box, self)
diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs
index 93583a1487a..9b8448bc2d9 100644
--- a/src/libsyntax/print/pprust.rs
+++ b/src/libsyntax/print/pprust.rs
@@ -1647,17 +1647,20 @@ pub fn print_pat(s: @ps, &&pat: @ast::pat, refutable: bool) {
 // Returns whether it printed anything
 pub fn print_self_ty(s: @ps, self_ty: ast::self_ty_) -> bool {
     match self_ty {
-      ast::sty_static | ast::sty_by_ref => { return false; }
-      ast::sty_value => { word(s.s, ~"self"); }
-      ast::sty_region(m) => {
-        word(s.s, ~"&"); print_mutability(s, m); word(s.s, ~"self");
-      }
-      ast::sty_box(m) => {
-        word(s.s, ~"@"); print_mutability(s, m); word(s.s, ~"self");
-      }
-      ast::sty_uniq(m) => {
-        word(s.s, ~"~"); print_mutability(s, m); word(s.s, ~"self");
-      }
+        ast::sty_static | ast::sty_by_ref => { return false; }
+        ast::sty_value => { word(s.s, ~"self"); }
+        ast::sty_region(lt, m) => {
+            word(s.s, ~"&");
+            print_opt_lifetime(s, lt);
+            print_mutability(s, m);
+            word(s.s, ~"self");
+        }
+        ast::sty_box(m) => {
+            word(s.s, ~"@"); print_mutability(s, m); word(s.s, ~"self");
+        }
+        ast::sty_uniq(m) => {
+            word(s.s, ~"~"); print_mutability(s, m); word(s.s, ~"self");
+        }
     }
     return true;
 }