1
Fork 0

Remove is_any_keyword methods.

They're dodgy, covering all the keywords, including weak ones, and
edition-specific ones without considering the edition. They have a
single use in rustfmt. This commit changes that use to
`is_reserved_ident`, which is a much more widely used alternative and is
good enough, judging by the lack of effect on the test suite.
This commit is contained in:
Nicholas Nethercote 2025-03-12 16:14:32 +11:00
parent aa8f0fd716
commit 9dd5340d3c
3 changed files with 8 additions and 23 deletions

View file

@ -928,11 +928,6 @@ impl Token {
self.is_non_raw_ident_where(Ident::is_path_segment_keyword) self.is_non_raw_ident_where(Ident::is_path_segment_keyword)
} }
/// Don't use this unless you're doing something very loose and heuristic-y.
pub fn is_any_keyword(&self) -> bool {
self.is_non_raw_ident_where(Ident::is_any_keyword)
}
/// Returns true for reserved identifiers used internally for elided lifetimes, /// Returns true for reserved identifiers used internally for elided lifetimes,
/// unnamed method parameters, crate root module, error recovery etc. /// unnamed method parameters, crate root module, error recovery etc.
pub fn is_special_ident(&self) -> bool { pub fn is_special_ident(&self) -> bool {

View file

@ -32,7 +32,7 @@ symbols! {
Keywords { Keywords {
// Special reserved identifiers used internally for elided lifetimes, // Special reserved identifiers used internally for elided lifetimes,
// unnamed method parameters, crate root module, error recovery etc. // unnamed method parameters, crate root module, error recovery etc.
// Matching predicates: `is_any_keyword`, `is_special`/`is_reserved` // Matching predicates: `is_special`/`is_reserved`
// //
// Notes about `kw::Empty`: // Notes about `kw::Empty`:
// - Its use can blur the lines between "empty symbol" and "no symbol". // - Its use can blur the lines between "empty symbol" and "no symbol".
@ -48,7 +48,7 @@ symbols! {
Underscore: "_", Underscore: "_",
// Keywords that are used in stable Rust. // Keywords that are used in stable Rust.
// Matching predicates: `is_any_keyword`, `is_used_keyword_always`/`is_reserved` // Matching predicates: `is_used_keyword_always`/`is_reserved`
As: "as", As: "as",
Break: "break", Break: "break",
Const: "const", Const: "const",
@ -86,7 +86,7 @@ symbols! {
While: "while", While: "while",
// Keywords that are used in unstable Rust or reserved for future use. // Keywords that are used in unstable Rust or reserved for future use.
// Matching predicates: `is_any_keyword`, `is_unused_keyword_always`/`is_reserved` // Matching predicates: `is_unused_keyword_always`/`is_reserved`
Abstract: "abstract", Abstract: "abstract",
Become: "become", Become: "become",
Box: "box", Box: "box",
@ -101,14 +101,14 @@ symbols! {
Yield: "yield", Yield: "yield",
// Edition-specific keywords that are used in stable Rust. // Edition-specific keywords that are used in stable Rust.
// Matching predicates: `is_any_keyword`, `is_used_keyword_conditional`/`is_reserved` (if // Matching predicates: `is_used_keyword_conditional`/`is_reserved` (if
// the edition suffices) // the edition suffices)
Async: "async", // >= 2018 Edition only Async: "async", // >= 2018 Edition only
Await: "await", // >= 2018 Edition only Await: "await", // >= 2018 Edition only
Dyn: "dyn", // >= 2018 Edition only Dyn: "dyn", // >= 2018 Edition only
// Edition-specific keywords that are used in unstable Rust or reserved for future use. // Edition-specific keywords that are used in unstable Rust or reserved for future use.
// Matching predicates: `is_any_keyword`, `is_unused_keyword_conditional`/`is_reserved` (if // Matching predicates: `is_unused_keyword_conditional`/`is_reserved` (if
// the edition suffices) // the edition suffices)
Gen: "gen", // >= 2024 Edition only Gen: "gen", // >= 2024 Edition only
Try: "try", // >= 2018 Edition only Try: "try", // >= 2018 Edition only
@ -116,12 +116,12 @@ symbols! {
// NOTE: When adding new keywords, consider adding them to the ui/parser/raw/raw-idents.rs test. // NOTE: When adding new keywords, consider adding them to the ui/parser/raw/raw-idents.rs test.
// "Lifetime keywords": regular keywords with a leading `'`. // "Lifetime keywords": regular keywords with a leading `'`.
// Matching predicates: `is_any_keyword` // Matching predicates: none
UnderscoreLifetime: "'_", UnderscoreLifetime: "'_",
StaticLifetime: "'static", StaticLifetime: "'static",
// Weak keywords, have special meaning only in specific contexts. // Weak keywords, have special meaning only in specific contexts.
// Matching predicates: `is_any_keyword` // Matching predicates: none
Auto: "auto", Auto: "auto",
Builtin: "builtin", Builtin: "builtin",
Catch: "catch", Catch: "catch",
@ -2677,11 +2677,6 @@ pub mod sym {
} }
impl Symbol { impl Symbol {
/// Don't use this unless you're doing something very loose and heuristic-y.
pub fn is_any_keyword(self) -> bool {
self >= kw::As && self <= kw::Yeet
}
fn is_special(self) -> bool { fn is_special(self) -> bool {
self <= kw::Underscore self <= kw::Underscore
} }
@ -2738,11 +2733,6 @@ impl Symbol {
} }
impl Ident { impl Ident {
/// Don't use this unless you're doing something very loose and heuristic-y.
pub fn is_any_keyword(self) -> bool {
self.name.is_any_keyword()
}
/// Returns `true` for reserved identifiers used internally for elided lifetimes, /// Returns `true` for reserved identifiers used internally for elided lifetimes,
/// unnamed method parameters, crate root module, error recovery etc. /// unnamed method parameters, crate root module, error recovery etc.
pub fn is_special(self) -> bool { pub fn is_special(self) -> bool {

View file

@ -81,7 +81,7 @@ pub(crate) struct ParsedMacroArgs {
} }
fn check_keyword<'a, 'b: 'a>(parser: &'a mut Parser<'b>) -> Option<MacroArg> { fn check_keyword<'a, 'b: 'a>(parser: &'a mut Parser<'b>) -> Option<MacroArg> {
if parser.token.is_any_keyword() if parser.token.is_reserved_ident()
&& parser.look_ahead(1, |t| *t == TokenKind::Eof || *t == TokenKind::Comma) && parser.look_ahead(1, |t| *t == TokenKind::Eof || *t == TokenKind::Comma)
{ {
let keyword = parser.token.ident().unwrap().0.name; let keyword = parser.token.ident().unwrap().0.name;