about summary refs log tree commit diff
path: root/src/libsyntax/parse
diff options
context:
space:
mode:
authorMarvin Löbel <loebel.marvin@gmail.com>2015-02-10 14:55:45 +0100
committerMarvin Löbel <loebel.marvin@gmail.com>2015-02-12 22:04:31 +0100
commit07d00deab22dc07ffc58b8e74d45596242ca8b15 (patch)
tree4171afb6d0be091c7a72224d326a558256ee30f0 /src/libsyntax/parse
parentde8bc44753881aacdaf435f5ba61de3c20916761 (diff)
downloadrust-07d00deab22dc07ffc58b8e74d45596242ca8b15.tar.gz
rust-07d00deab22dc07ffc58b8e74d45596242ca8b15.zip
Made `Self` a keyword.
It is only allowed in paths now, where it will either work inside a `trait`
or `impl` item, or not resolve outside of it.

[breaking-change]

Closes #22137
Diffstat (limited to 'src/libsyntax/parse')
-rw-r--r--src/libsyntax/parse/lexer/mod.rs2
-rw-r--r--src/libsyntax/parse/parser.rs73
-rw-r--r--src/libsyntax/parse/token.rs9
3 files changed, 59 insertions, 25 deletions
diff --git a/src/libsyntax/parse/lexer/mod.rs b/src/libsyntax/parse/lexer/mod.rs
index ecc39925a40..1a4afa4957c 100644
--- a/src/libsyntax/parse/lexer/mod.rs
+++ b/src/libsyntax/parse/lexer/mod.rs
@@ -1122,7 +1122,7 @@ impl<'a> StringReader<'a> {
                 let keyword_checking_token =
                     &token::Ident(keyword_checking_ident, token::Plain);
                 let last_bpos = self.last_pos;
-                if keyword_checking_token.is_keyword(token::keywords::Self) {
+                if keyword_checking_token.is_keyword(token::keywords::SelfValue) {
                     self.err_span_(start,
                                    last_bpos,
                                    "invalid lifetime name: 'self \
diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs
index fd2f0685cab..d2133f03335 100644
--- a/src/libsyntax/parse/parser.rs
+++ b/src/libsyntax/parse/parser.rs
@@ -516,13 +516,21 @@ impl<'a> Parser<'a> {
         }
     }
 
+    pub fn parse_ident_or_self_type(&mut self) -> ast::Ident {
+        if self.is_self_type_ident() {
+            self.expect_self_type_ident()
+        } else {
+            self.parse_ident()
+        }
+    }
+
     pub fn parse_path_list_item(&mut self) -> ast::PathListItem {
         let lo = self.span.lo;
         let node = if self.eat_keyword_noexpect(keywords::Mod) {
             let span = self.last_span;
             self.span_warn(span, "deprecated syntax; use the `self` keyword now");
             ast::PathListMod { id: ast::DUMMY_NODE_ID }
-        } else if self.eat_keyword(keywords::Self) {
+        } else if self.eat_keyword(keywords::SelfValue) {
             ast::PathListMod { id: ast::DUMMY_NODE_ID }
         } else {
             let ident = self.parse_ident();
@@ -1797,7 +1805,7 @@ impl<'a> Parser<'a> {
         let mut segments = Vec::new();
         loop {
             // First, parse an identifier.
-            let identifier = self.parse_ident();
+            let identifier = self.parse_ident_or_self_type();
 
             // Parse types, optionally.
             let parameters = if self.eat_lt() {
@@ -1850,7 +1858,7 @@ impl<'a> Parser<'a> {
         let mut segments = Vec::new();
         loop {
             // First, parse an identifier.
-            let identifier = self.parse_ident();
+            let identifier = self.parse_ident_or_self_type();
 
             // If we do not see a `::`, stop.
             if !self.eat(&token::ModSep) {
@@ -1895,7 +1903,7 @@ impl<'a> Parser<'a> {
         let mut segments = Vec::new();
         loop {
             // First, parse an identifier.
-            let identifier = self.parse_ident();
+            let identifier = self.parse_ident_or_self_type();
 
             // Assemble and push the result.
             segments.push(ast::PathSegment {
@@ -2166,10 +2174,8 @@ impl<'a> Parser<'a> {
             token::BinOp(token::Or) |  token::OrOr => {
                 return self.parse_lambda_expr(CaptureByRef);
             },
-            // FIXME #13626: Should be able to stick in
-            // token::SELF_KEYWORD_NAME
             token::Ident(id @ ast::Ident {
-                            name: ast::Name(token::SELF_KEYWORD_NAME_NUM),
+                            name: token::SELF_KEYWORD_NAME,
                             ctxt: _
                          }, token::Plain) => {
                 self.bump();
@@ -3411,7 +3417,7 @@ impl<'a> Parser<'a> {
               && self.token != token::ModSep)
                 || self.token.is_keyword(keywords::True)
                 || self.token.is_keyword(keywords::False) {
-            // Parse an expression pattern or exp .. exp.
+            // Parse an expression pattern or exp ... exp.
             //
             // These expressions are limited to literals (possibly
             // preceded by unary-minus) or identifiers.
@@ -3532,15 +3538,17 @@ impl<'a> Parser<'a> {
                                   enum_path.segments.len() == 1 &&
                                   enum_path.segments[0].parameters.is_empty()
                               {
-                                  // it could still be either an enum
-                                  // or an identifier pattern, resolve
-                                  // will sort it out:
-                                  pat = PatIdent(BindByValue(MutImmutable),
-                                                 codemap::Spanned{
-                                                    span: enum_path.span,
-                                                    node: enum_path.segments[0]
-                                                           .identifier},
-                                                 None);
+                                // NB: If enum_path is a single identifier,
+                                // this should not be reachable due to special
+                                // handling further above.
+                                //
+                                // However, previously a PatIdent got emitted
+                                // here, so we preserve the branch just in case.
+                                //
+                                // A rewrite of the logic in this function
+                                // would probably make this obvious.
+                                self.span_bug(enum_path.span,
+                                              "ident only path should have been covered already");
                               } else {
                                   pat = PatEnum(enum_path, Some(args));
                               }
@@ -4380,6 +4388,27 @@ impl<'a> Parser<'a> {
         }
     }
 
+    fn is_self_type_ident(&mut self) -> bool {
+        match self.token {
+          token::Ident(id, token::Plain) => id.name == special_idents::type_self.name,
+          _ => false
+        }
+    }
+
+    fn expect_self_type_ident(&mut self) -> ast::Ident {
+        match self.token {
+            token::Ident(id, token::Plain) if id.name == special_idents::type_self.name => {
+                self.bump();
+                id
+            },
+            _ => {
+                let token_str = self.this_token_to_string();
+                self.fatal(&format!("expected `Self`, found `{}`",
+                                   token_str)[])
+            }
+        }
+    }
+
     /// Parse the argument list and result type of a function
     /// that may have a self type.
     fn parse_fn_decl_with_self<F>(&mut self, parse_arg_fn: F) -> (ExplicitSelf, P<FnDecl>) where
@@ -4396,22 +4425,22 @@ impl<'a> Parser<'a> {
             //
             // We already know that the current token is `&`.
 
-            if this.look_ahead(1, |t| t.is_keyword(keywords::Self)) {
+            if this.look_ahead(1, |t| t.is_keyword(keywords::SelfValue)) {
                 this.bump();
                 SelfRegion(None, MutImmutable, this.expect_self_ident())
             } else if this.look_ahead(1, |t| t.is_mutability()) &&
-                      this.look_ahead(2, |t| t.is_keyword(keywords::Self)) {
+                      this.look_ahead(2, |t| t.is_keyword(keywords::SelfValue)) {
                 this.bump();
                 let mutability = this.parse_mutability();
                 SelfRegion(None, mutability, this.expect_self_ident())
             } else if this.look_ahead(1, |t| t.is_lifetime()) &&
-                      this.look_ahead(2, |t| t.is_keyword(keywords::Self)) {
+                      this.look_ahead(2, |t| t.is_keyword(keywords::SelfValue)) {
                 this.bump();
                 let lifetime = this.parse_lifetime();
                 SelfRegion(Some(lifetime), MutImmutable, this.expect_self_ident())
             } else if this.look_ahead(1, |t| t.is_lifetime()) &&
                       this.look_ahead(2, |t| t.is_mutability()) &&
-                      this.look_ahead(3, |t| t.is_keyword(keywords::Self)) {
+                      this.look_ahead(3, |t| t.is_keyword(keywords::SelfValue)) {
                 this.bump();
                 let lifetime = this.parse_lifetime();
                 let mutability = this.parse_mutability();
@@ -4466,7 +4495,7 @@ impl<'a> Parser<'a> {
                         SelfValue(self_ident)
                     }
                 } else if self.token.is_mutability() &&
-                        self.look_ahead(1, |t| t.is_keyword(keywords::Self)) {
+                        self.look_ahead(1, |t| t.is_keyword(keywords::SelfValue)) {
                     mutbl_self = self.parse_mutability();
                     let self_ident = self.expect_self_ident();
 
diff --git a/src/libsyntax/parse/token.rs b/src/libsyntax/parse/token.rs
index 45f4f044ea4..5e67b7cf1f3 100644
--- a/src/libsyntax/parse/token.rs
+++ b/src/libsyntax/parse/token.rs
@@ -300,6 +300,7 @@ impl Token {
                    n == SELF_KEYWORD_NAME
                 || n == STATIC_KEYWORD_NAME
                 || n == SUPER_KEYWORD_NAME
+                || n == SELF_TYPE_KEYWORD_NAME
                 || STRICT_KEYWORD_START <= n
                 && n <= RESERVED_KEYWORD_FINAL
             },
@@ -317,6 +318,7 @@ impl Token {
                    n == SELF_KEYWORD_NAME
                 || n == STATIC_KEYWORD_NAME
                 || n == SUPER_KEYWORD_NAME
+                || n == SELF_TYPE_KEYWORD_NAME
                 || STRICT_KEYWORD_START <= n
                 && n <= STRICT_KEYWORD_FINAL
             },
@@ -488,10 +490,12 @@ macro_rules! declare_special_idents_and_keywords {(
 pub const SELF_KEYWORD_NAME: ast::Name = ast::Name(SELF_KEYWORD_NAME_NUM);
 const STATIC_KEYWORD_NAME: ast::Name = ast::Name(STATIC_KEYWORD_NAME_NUM);
 const SUPER_KEYWORD_NAME: ast::Name = ast::Name(SUPER_KEYWORD_NAME_NUM);
+const SELF_TYPE_KEYWORD_NAME: ast::Name = ast::Name(SELF_TYPE_KEYWORD_NAME_NUM);
 
 pub const SELF_KEYWORD_NAME_NUM: u32 = 1;
 const STATIC_KEYWORD_NAME_NUM: u32 = 2;
 const SUPER_KEYWORD_NAME_NUM: u32 = 3;
+const SELF_TYPE_KEYWORD_NAME_NUM: u32 = 10;
 
 // NB: leaving holes in the ident table is bad! a different ident will get
 // interned with the id from the hole, but it will be between the min and max
@@ -514,7 +518,7 @@ declare_special_idents_and_keywords! {
         (7,                          clownshoe_abi,          "__rust_abi");
         (8,                          opaque,                 "<opaque>");
         (9,                          unnamed_field,          "<unnamed_field>");
-        (10,                         type_self,              "Self");
+        (super::SELF_TYPE_KEYWORD_NAME_NUM, type_self,       "Self");
         (11,                         prelude_import,         "prelude_import");
     }
 
@@ -545,7 +549,8 @@ declare_special_idents_and_keywords! {
         (32,                         Return,     "return");
         // Static and Self are also special idents (prefill de-dupes)
         (super::STATIC_KEYWORD_NAME_NUM, Static, "static");
-        (super::SELF_KEYWORD_NAME_NUM,   Self,   "self");
+        (super::SELF_KEYWORD_NAME_NUM, SelfValue, "self");
+        (super::SELF_TYPE_KEYWORD_NAME_NUM, SelfType, "Self");
         (33,                         Struct,     "struct");
         (super::SUPER_KEYWORD_NAME_NUM, Super,   "super");
         (34,                         True,       "true");