@@ -20,18 +20,26 @@ mod tests;
20
20
21
21
// The proc macro code for this is in `compiler/rustc_macros/src/symbols.rs`.
22
22
symbols ! {
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`.
25
31
// But this should rarely be necessary if the keywords are kept in alphabetic order.
26
32
Keywords {
27
33
// Special reserved identifiers used internally for elided lifetimes,
28
34
// unnamed method parameters, crate root module, error recovery etc.
35
+ // Matching predicates: `is_any_keyword`, `is_special`/`is_reserved`
29
36
Empty : "" ,
30
37
PathRoot : "{{root}}" ,
31
38
DollarCrate : "$crate" ,
32
39
Underscore : "_" ,
33
40
34
41
// Keywords that are used in stable Rust.
42
+ // Matching predicates: `is_any_keyword`, `is_used_keyword_always`/`is_reserved`
35
43
As : "as" ,
36
44
Break : "break" ,
37
45
Const : "const" ,
@@ -69,6 +77,7 @@ symbols! {
69
77
While : "while" ,
70
78
71
79
// Keywords that are used in unstable Rust or reserved for future use.
80
+ // Matching predicates: `is_any_keyword`, `is_unused_keyword_always`/`is_reserved`
72
81
Abstract : "abstract" ,
73
82
Become : "become" ,
74
83
Box : "box" ,
@@ -83,23 +92,29 @@ symbols! {
83
92
Yield : "yield" ,
84
93
85
94
// 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)
86
97
Async : "async" , // >= 2018 Edition only
87
98
Await : "await" , // >= 2018 Edition only
88
99
Dyn : "dyn" , // >= 2018 Edition only
89
100
90
101
// 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
91
105
Try : "try" , // >= 2018 Edition only
92
106
93
- // Special lifetime names
107
+ // "Lifetime keywords": regular keywords with a leading `'`.
108
+ // Matching predicates: `is_any_keyword`
94
109
UnderscoreLifetime : "'_" ,
95
110
StaticLifetime : "'static" ,
96
111
97
112
// Weak keywords, have special meaning only in specific contexts.
113
+ // Matching predicates: `is_any_keyword`
98
114
Auto : "auto" ,
99
115
Builtin : "builtin" ,
100
116
Catch : "catch" ,
101
117
Default : "default" ,
102
- Gen : "gen" ,
103
118
MacroRules : "macro_rules" ,
104
119
Raw : "raw" ,
105
120
Reuse : "reuse" ,
@@ -2589,6 +2604,11 @@ pub mod sym {
2589
2604
}
2590
2605
2591
2606
impl 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
+
2592
2612
fn is_special ( self ) -> bool {
2593
2613
self <= kw:: Underscore
2594
2614
}
@@ -2606,8 +2626,8 @@ impl Symbol {
2606
2626
}
2607
2627
2608
2628
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 ( )
2611
2631
}
2612
2632
2613
2633
pub fn is_reserved ( self , edition : impl Copy + FnOnce ( ) -> Edition ) -> bool {
@@ -2645,6 +2665,11 @@ impl Symbol {
2645
2665
}
2646
2666
2647
2667
impl 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
+
2648
2673
/// Returns `true` for reserved identifiers used internally for elided lifetimes,
2649
2674
/// unnamed method parameters, crate root module, error recovery etc.
2650
2675
pub fn is_special ( self ) -> bool {
@@ -2683,41 +2708,19 @@ impl Ident {
2683
2708
}
2684
2709
}
2685
2710
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
+ }
2706
2724
} )
2707
2725
. 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
- }
2723
2726
}
0 commit comments