Skip to content

Commit

Permalink
Improve comments on keywords.
Browse files Browse the repository at this point in the history
To make it clear which predicates apply to which keywords.

Also reorder the predicates a little to match the order of the keyword
list.
  • Loading branch information
nnethercote committed Dec 16, 2024
1 parent 4616ac2 commit a6679b4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
3 changes: 2 additions & 1 deletion compiler/rustc_ast/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -904,7 +904,8 @@ impl Token {
self.is_non_raw_ident_where(|id| id.name == kw)
}

/// 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.
/// 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.
pub fn is_keyword_case(&self, kw: Symbol, case: Case) -> bool {
self.is_keyword(kw)
|| (case == Case::Insensitive
Expand Down
29 changes: 18 additions & 11 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ mod tests;

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

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

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

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

// Weak keywords, have special meaning only in specific contexts.
// Predicates: `is_any_keyword`
Auto: "auto",
Builtin: "builtin",
Catch: "catch",
Expand Down Expand Up @@ -2587,27 +2594,27 @@ impl Symbol {
self >= kw::As && self <= kw::Yeet
}

fn is_reserved_ident(self) -> bool {
self == sym::dollar_crate || self == sym::underscore
}

fn is_used_keyword_always(self) -> bool {
self >= kw::As && self <= kw::While
}

fn is_used_keyword_conditional(self, edition: impl FnOnce() -> Edition) -> bool {
(self >= kw::Async && self <= kw::Dyn) && edition() >= Edition::Edition2018
}

fn is_unused_keyword_always(self) -> bool {
self >= kw::Abstract && self <= kw::Yield
}

fn is_used_keyword_conditional(self, edition: impl FnOnce() -> Edition) -> bool {
(self >= kw::Async && self <= kw::Dyn) && edition() >= Edition::Edition2018
}

fn is_unused_keyword_conditional(self, edition: impl Copy + FnOnce() -> Edition) -> bool {
self == kw::Gen && edition().at_least_rust_2024()
|| self == kw::Try && edition().at_least_rust_2018()
}

fn is_reserved_ident(self) -> bool {
self == sym::dollar_crate || self == sym::underscore
}

pub fn is_reserved(self, edition: impl Copy + FnOnce() -> Edition) -> bool {
self.is_reserved_ident()
|| self.is_used_keyword_always()
Expand Down

0 comments on commit a6679b4

Please sign in to comment.