@@ -20,18 +20,26 @@ mod tests;
2020
2121// The proc macro code for this is in `compiler/rustc_macros/src/symbols.rs`. 
2222symbols !  { 
23-     // If you modify this list, adjust `is_special`, `is_used_keyword`/`is_unused_keyword` 
24-     // and `AllKeywords`. 
23+     // This list includes things that are definitely keywords (e.g. `if`), 
24+     // a few things that are definitely not keywords (e.g. the empty symbol, 
25+     // `{{root}}`) and things where there is disagreement between people and/or 
26+     // documents (such as the Rust Reference) about whether it is a keyword 
27+     // (e.g. `_`). 
28+     // 
29+     // If you modify this list, adjust any relevant `Symbol::{is,can_be}_*` predicates and 
30+     // `used_keywords`. 
2531    // But this should rarely be necessary if the keywords are kept in alphabetic order. 
2632    Keywords  { 
2733        // Special reserved identifiers used internally for elided lifetimes, 
2834        // unnamed method parameters, crate root module, error recovery etc. 
35+         // Matching predicates: `is_any_keyword`, `is_special`/`is_reserved` 
2936        Empty :               "" , 
3037        PathRoot :            "{{root}}" , 
3138        DollarCrate :         "$crate" , 
3239        Underscore :          "_" , 
3340
3441        // Keywords that are used in stable Rust. 
42+         // Matching predicates: `is_any_keyword`, `is_used_keyword_always`/`is_reserved` 
3543        As :                  "as" , 
3644        Break :               "break" , 
3745        Const :               "const" , 
@@ -69,6 +77,7 @@ symbols! {
6977        While :               "while" , 
7078
7179        // Keywords that are used in unstable Rust or reserved for future use. 
80+         // Matching predicates: `is_any_keyword`, `is_unused_keyword_always`/`is_reserved` 
7281        Abstract :            "abstract" , 
7382        Become :              "become" , 
7483        Box :                 "box" , 
@@ -83,23 +92,29 @@ symbols! {
8392        Yield :               "yield" , 
8493
8594        // Edition-specific keywords that are used in stable Rust. 
95+         // Matching predicates: `is_any_keyword`, `is_used_keyword_conditional`/`is_reserved` (if 
96+         // the edition suffices) 
8697        Async :               "async" ,  // >= 2018 Edition only 
8798        Await :               "await" ,  // >= 2018 Edition only 
8899        Dyn :                 "dyn" ,  // >= 2018 Edition only 
89100
90101        // Edition-specific keywords that are used in unstable Rust or reserved for future use. 
102+         // Matching predicates: `is_any_keyword`, `is_unused_keyword_conditional`/`is_reserved` (if 
103+         // the edition suffices) 
104+         Gen :                 "gen" ,  // >= 2024 Edition only 
91105        Try :                 "try" ,  // >= 2018 Edition only 
92106
93-         // Special lifetime names 
107+         // "Lifetime keywords": regular keywords with a leading `'`. 
108+         // Matching predicates: `is_any_keyword` 
94109        UnderscoreLifetime :  "'_" , 
95110        StaticLifetime :      "'static" , 
96111
97112        // Weak keywords, have special meaning only in specific contexts. 
113+         // Matching predicates: `is_any_keyword` 
98114        Auto :                "auto" , 
99115        Builtin :             "builtin" , 
100116        Catch :               "catch" , 
101117        Default :             "default" , 
102-         Gen :                 "gen" , 
103118        MacroRules :          "macro_rules" , 
104119        Raw :                 "raw" , 
105120        Reuse :               "reuse" , 
@@ -2589,6 +2604,11 @@ pub mod sym {
25892604} 
25902605
25912606impl  Symbol  { 
2607+     /// Don't use this unless you're doing something very loose and heuristic-y. 
2608+      pub  fn  is_any_keyword ( self )  -> bool  { 
2609+         self  >= kw:: As  && self  <= kw:: Yeet 
2610+     } 
2611+ 
25922612    fn  is_special ( self )  -> bool  { 
25932613        self  <= kw:: Underscore 
25942614    } 
@@ -2606,8 +2626,8 @@ impl Symbol {
26062626    } 
26072627
26082628    fn  is_unused_keyword_conditional ( self ,  edition :  impl  Copy  + FnOnce ( )  -> Edition )  -> bool  { 
2609-         self  == kw:: Try  && edition ( ) . at_least_rust_2018 ( ) 
2610-             || self  == kw:: Gen  && edition ( ) . at_least_rust_2024 ( ) 
2629+         self  == kw:: Gen  && edition ( ) . at_least_rust_2024 ( ) 
2630+             || self  == kw:: Try  && edition ( ) . at_least_rust_2018 ( ) 
26112631    } 
26122632
26132633    pub  fn  is_reserved ( self ,  edition :  impl  Copy  + FnOnce ( )  -> Edition )  -> bool  { 
@@ -2645,6 +2665,11 @@ impl Symbol {
26452665} 
26462666
26472667impl  Ident  { 
2668+     /// Don't use this unless you're doing something very loose and heuristic-y. 
2669+      pub  fn  is_any_keyword ( self )  -> bool  { 
2670+         self . name . is_any_keyword ( ) 
2671+     } 
2672+ 
26482673    /// Returns `true` for reserved identifiers used internally for elided lifetimes, 
26492674     /// unnamed method parameters, crate root module, error recovery etc. 
26502675     pub  fn  is_special ( self )  -> bool  { 
@@ -2683,41 +2708,19 @@ impl Ident {
26832708    } 
26842709} 
26852710
2686- /// An iterator over all the keywords in Rust. 
2687- #[ derive( Copy ,  Clone ) ]  
2688- pub  struct  AllKeywords  { 
2689-     curr_idx :  u32 , 
2690-     end_idx :  u32 , 
2691- } 
2692- 
2693- impl  AllKeywords  { 
2694-     /// Initialize a new iterator over all the keywords. 
2695-      /// 
2696-      /// *Note:* Please update this if a new keyword is added beyond the current 
2697-      /// range. 
2698-      pub  fn  new ( )  -> Self  { 
2699-         AllKeywords  {  curr_idx :  kw:: Empty . as_u32 ( ) ,  end_idx :  kw:: Yeet . as_u32 ( )  } 
2700-     } 
2701- 
2702-     /// Collect all the keywords in a given edition into a vector. 
2703-      pub  fn  collect_used ( & self ,  edition :  impl  Copy  + FnOnce ( )  -> Edition )  -> Vec < Symbol >  { 
2704-         self . filter ( |& keyword| { 
2705-             keyword. is_used_keyword_always ( )  || keyword. is_used_keyword_conditional ( edition) 
2711+ /// Collect all the keywords in a given edition into a vector. 
2712+ /// 
2713+ /// *Note:* Please update this if a new keyword is added beyond the current 
2714+ /// range. 
2715+ pub  fn  used_keywords ( edition :  impl  Copy  + FnOnce ( )  -> Edition )  -> Vec < Symbol >  { 
2716+     ( kw:: Empty . as_u32 ( ) ..kw:: Yeet . as_u32 ( ) ) 
2717+         . filter_map ( |kw| { 
2718+             let  kw = Symbol :: new ( kw) ; 
2719+             if  kw. is_used_keyword_always ( )  || kw. is_used_keyword_conditional ( edition)  { 
2720+                 Some ( kw) 
2721+             }  else  { 
2722+                 None 
2723+             } 
27062724        } ) 
27072725        . collect ( ) 
2708-     } 
2709- } 
2710- 
2711- impl  Iterator  for  AllKeywords  { 
2712-     type  Item  = Symbol ; 
2713- 
2714-     fn  next ( & mut  self )  -> Option < Self :: Item >  { 
2715-         if  self . curr_idx  <= self . end_idx  { 
2716-             let  keyword = Symbol :: new ( self . curr_idx ) ; 
2717-             self . curr_idx  += 1 ; 
2718-             Some ( keyword) 
2719-         }  else  { 
2720-             None 
2721-         } 
2722-     } 
27232726} 
0 commit comments