Skip to content

Commit 0bbbe89

Browse files
committed
Improve comments on keywords.
To make it clear which predicates apply to which keywords. Also reorder the predicates a little to match the order of the keyword list.
1 parent af44a52 commit 0bbbe89

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

compiler/rustc_ast/src/token.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -904,7 +904,8 @@ impl Token {
904904
self.is_non_raw_ident_where(|id| id.name == kw)
905905
}
906906

907-
/// Returns `true` if the token is a given keyword, `kw` or if `case` is `Insensitive` and this token is an identifier equal to `kw` ignoring the case.
907+
/// Returns `true` if the token is a given keyword, `kw` or if `case` is `Insensitive` and this
908+
/// token is an identifier equal to `kw` ignoring the case.
908909
pub fn is_keyword_case(&self, kw: Symbol, case: Case) -> bool {
909910
self.is_keyword(kw)
910911
|| (case == Case::Insensitive

compiler/rustc_span/src/symbol.rs

+18-11
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@ mod tests;
2020

2121
// The proc macro code for this is in `compiler/rustc_macros/src/symbols.rs`.
2222
symbols! {
23-
// If you modify this list, adjust `is_any_keyword`, `is_reserved_ident`,
24-
// `is_used_keyword`/`is_unused_keyword` and `AllKeywords`.
25-
// But this should rarely be necessary if the keywords are kept in alphabetic order.
23+
// If you modify this list, adjust the predicates mentioned in comments
24+
// below. But this should rarely be necessary if the keywords are kept in
25+
// alphabetic order.
2626
Keywords {
2727
// Keywords that are used in stable Rust.
28+
// Predicates: `is_any_keyword`, `is_used_keyword_always`/`is_reserved`
2829
As: "as",
2930
Break: "break",
3031
Const: "const",
@@ -62,6 +63,7 @@ symbols! {
6263
While: "while",
6364

6465
// Keywords that are used in unstable Rust or reserved for future use.
66+
// Predicates: `is_any_keyword`, `is_unused_keyword_always`/`is_reserved`
6567
Abstract: "abstract",
6668
Become: "become",
6769
Box: "box",
@@ -76,15 +78,20 @@ symbols! {
7678
Yield: "yield",
7779

7880
// Edition-specific keywords that are used in stable Rust.
81+
// Predicates: `is_any_keyword`, `is_used_keyword_conditional`/`is_reserved` (if the
82+
// edition suffices)
7983
Async: "async", // >= 2018 Edition only
8084
Await: "await", // >= 2018 Edition only
8185
Dyn: "dyn", // >= 2018 Edition only
8286

8387
// Edition-specific keywords that are used in unstable Rust or reserved for future use.
88+
// Predicates: `is_any_keyword`, `is_unused_keyword_conditional`/`is_reserved` (if the
89+
// edition suffices)
8490
Gen: "gen", // >= 2024 Edition only
8591
Try: "try", // >= 2018 Edition only
8692

8793
// Weak keywords, have special meaning only in specific contexts.
94+
// Predicates: `is_any_keyword`
8895
Auto: "auto",
8996
Builtin: "builtin",
9097
Catch: "catch",
@@ -2586,27 +2593,27 @@ impl Symbol {
25862593
self >= kw::As && self <= kw::Yeet
25872594
}
25882595

2589-
fn is_reserved_ident(self) -> bool {
2590-
self == sym::dollar_crate || self == sym::underscore
2591-
}
2592-
25932596
fn is_used_keyword_always(self) -> bool {
25942597
self >= kw::As && self <= kw::While
25952598
}
25962599

2597-
fn is_used_keyword_conditional(self, edition: impl FnOnce() -> Edition) -> bool {
2598-
(self >= kw::Async && self <= kw::Dyn) && edition() >= Edition::Edition2018
2599-
}
2600-
26012600
fn is_unused_keyword_always(self) -> bool {
26022601
self >= kw::Abstract && self <= kw::Yield
26032602
}
26042603

2604+
fn is_used_keyword_conditional(self, edition: impl FnOnce() -> Edition) -> bool {
2605+
(self >= kw::Async && self <= kw::Dyn) && edition() >= Edition::Edition2018
2606+
}
2607+
26052608
fn is_unused_keyword_conditional(self, edition: impl Copy + FnOnce() -> Edition) -> bool {
26062609
self == kw::Gen && edition().at_least_rust_2024()
26072610
|| self == kw::Try && edition().at_least_rust_2018()
26082611
}
26092612

2613+
fn is_reserved_ident(self) -> bool {
2614+
self == sym::dollar_crate || self == sym::underscore
2615+
}
2616+
26102617
pub fn is_reserved(self, edition: impl Copy + FnOnce() -> Edition) -> bool {
26112618
self.is_reserved_ident()
26122619
|| self.is_used_keyword_always()

0 commit comments

Comments
 (0)