]> git.lizzy.rs Git - rust.git/blobdiff - src/libsyntax_pos/symbol.rs
Add `try` to syntax_pos as an edition-2018-only keyword
[rust.git] / src / libsyntax_pos / symbol.rs
index bb64dad12085dfaa0e18e8674efef1a10c7c060d..dc92ce56c791e6b0a46936b63a66584133adc527 100644 (file)
@@ -68,6 +68,15 @@ pub fn modern(self) -> Ident {
         Ident::new(self.name, self.span.modern())
     }
 
+    /// "Normalize" ident for use in comparisons using "local variable hygiene".
+    /// Identifiers with same string value become same if they came from the same non-transparent
+    /// macro (e.g. `macro` or `macro_rules!` items) and stay different if they came from different
+    /// non-transparent macros.
+    /// Technically, this operation strips all transparent marks from ident's syntactic context.
+    pub fn modern_and_legacy(self) -> Ident {
+        Ident::new(self.name, self.span.modern_and_legacy())
+    }
+
     pub fn gensym(self) -> Ident {
         Ident::new(self.name.gensymed(), self.span)
     }
@@ -404,22 +413,30 @@ pub fn fresh() -> Self {
     (49, Virtual,            "virtual")
     (50, Yield,              "yield")
 
+    // Edition-specific keywords currently in use.
+    (51, Try,                "try") // >= 2018 Edition Only
+
     // Edition-specific keywords reserved for future use.
-    (51, Async,              "async") // >= 2018 Edition Only
+    (52, Async,              "async") // >= 2018 Edition Only
 
     // Special lifetime names
-    (52, UnderscoreLifetime, "'_")
-    (53, StaticLifetime,     "'static")
+    (53, UnderscoreLifetime, "'_")
+    (54, StaticLifetime,     "'static")
 
     // Weak keywords, have special meaning only in specific contexts.
-    (54, Auto,               "auto")
-    (55, Catch,              "catch")
-    (56, Default,            "default")
-    (57, Dyn,                "dyn")
-    (58, Union,              "union")
+    (55, Auto,               "auto")
+    (56, Catch,              "catch")
+    (57, Default,            "default")
+    (58, Dyn,                "dyn")
+    (59, Union,              "union")
+    (60, Existential,        "existential")
 }
 
 impl Symbol {
+    fn is_used_keyword_2018(self) -> bool {
+        self == keywords::Try.name()
+    }
+
     fn is_unused_keyword_2018(self) -> bool {
         self == keywords::Async.name()
     }
@@ -434,7 +451,9 @@ pub fn is_special(self) -> bool {
 
     /// Returns `true` if the token is a keyword used in the language.
     pub fn is_used_keyword(self) -> bool {
-        self.name >= keywords::As.name() && self.name <= keywords::While.name()
+        // Note: `span.edition()` is relatively expensive, don't call it unless necessary.
+        self.name >= keywords::As.name() && self.name <= keywords::While.name() ||
+        self.name.is_used_keyword_2018() && self.span.edition() == Edition::Edition2018
     }
 
     /// Returns `true` if the token is a keyword reserved for possible future use.