Skip to content

Commit 8020558

Browse files
authored
Rollup merge of #138385 - nnethercote:keyword-tweaks, r=Noratrieb
Keyword tweaks r? ```@Noratrieb```
2 parents e61403a + a29e875 commit 8020558

File tree

3 files changed

+34
-37
lines changed

3 files changed

+34
-37
lines changed

Diff for: compiler/rustc_ast/src/token.rs

-5
Original file line numberDiff line numberDiff line change
@@ -928,11 +928,6 @@ impl Token {
928928
self.is_non_raw_ident_where(Ident::is_path_segment_keyword)
929929
}
930930

931-
/// Don't use this unless you're doing something very loose and heuristic-y.
932-
pub fn is_any_keyword(&self) -> bool {
933-
self.is_non_raw_ident_where(Ident::is_any_keyword)
934-
}
935-
936931
/// Returns true for reserved identifiers used internally for elided lifetimes,
937932
/// unnamed method parameters, crate root module, error recovery etc.
938933
pub fn is_special_ident(&self) -> bool {

Diff for: compiler/rustc_span/src/symbol.rs

+33-31
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ symbols! {
2626
// documents (such as the Rust Reference) about whether it is a keyword
2727
// (e.g. `_`).
2828
//
29-
// If you modify this list, adjust any relevant `Symbol::{is,can_be}_*` predicates and
30-
// `used_keywords`.
31-
// But this should rarely be necessary if the keywords are kept in alphabetic order.
29+
// If you modify this list, adjust any relevant `Symbol::{is,can_be}_*`
30+
// predicates and `used_keywords`. Also consider adding new keywords to the
31+
// `ui/parser/raw/raw-idents.rs` test.
3232
Keywords {
3333
// Special reserved identifiers used internally for elided lifetimes,
3434
// unnamed method parameters, crate root module, error recovery etc.
35-
// Matching predicates: `is_any_keyword`, `is_special`/`is_reserved`
35+
// Matching predicates: `is_special`/`is_reserved`
3636
//
3737
// Notes about `kw::Empty`:
3838
// - Its use can blur the lines between "empty symbol" and "no symbol".
@@ -42,13 +42,16 @@ symbols! {
4242
// present, it's better to use `sym::dummy` than `kw::Empty`, because
4343
// it's clearer that it's intended as a dummy value, and more likely
4444
// to be detected if it accidentally does get used.
45+
// tidy-alphabetical-start
46+
DollarCrate: "$crate",
4547
Empty: "",
4648
PathRoot: "{{root}}",
47-
DollarCrate: "$crate",
4849
Underscore: "_",
50+
// tidy-alphabetical-end
4951

5052
// Keywords that are used in stable Rust.
51-
// Matching predicates: `is_any_keyword`, `is_used_keyword_always`/`is_reserved`
53+
// Matching predicates: `is_used_keyword_always`/`is_reserved`
54+
// tidy-alphabetical-start
5255
As: "as",
5356
Break: "break",
5457
Const: "const",
@@ -84,9 +87,11 @@ symbols! {
8487
Use: "use",
8588
Where: "where",
8689
While: "while",
90+
// tidy-alphabetical-end
8791

8892
// Keywords that are used in unstable Rust or reserved for future use.
89-
// Matching predicates: `is_any_keyword`, `is_unused_keyword_always`/`is_reserved`
93+
// Matching predicates: `is_unused_keyword_always`/`is_reserved`
94+
// tidy-alphabetical-start
9095
Abstract: "abstract",
9196
Become: "become",
9297
Box: "box",
@@ -99,41 +104,48 @@ symbols! {
99104
Unsized: "unsized",
100105
Virtual: "virtual",
101106
Yield: "yield",
107+
// tidy-alphabetical-end
102108

103109
// Edition-specific keywords that are used in stable Rust.
104-
// Matching predicates: `is_any_keyword`, `is_used_keyword_conditional`/`is_reserved` (if
110+
// Matching predicates: `is_used_keyword_conditional`/`is_reserved` (if
105111
// the edition suffices)
112+
// tidy-alphabetical-start
106113
Async: "async", // >= 2018 Edition only
107114
Await: "await", // >= 2018 Edition only
108115
Dyn: "dyn", // >= 2018 Edition only
116+
// tidy-alphabetical-end
109117

110118
// Edition-specific keywords that are used in unstable Rust or reserved for future use.
111-
// Matching predicates: `is_any_keyword`, `is_unused_keyword_conditional`/`is_reserved` (if
119+
// Matching predicates: `is_unused_keyword_conditional`/`is_reserved` (if
112120
// the edition suffices)
121+
// tidy-alphabetical-start
113122
Gen: "gen", // >= 2024 Edition only
114123
Try: "try", // >= 2018 Edition only
115-
116-
// NOTE: When adding new keywords, consider adding them to the ui/parser/raw/raw-idents.rs test.
124+
// tidy-alphabetical-end
117125

118126
// "Lifetime keywords": regular keywords with a leading `'`.
119-
// Matching predicates: `is_any_keyword`
120-
UnderscoreLifetime: "'_",
127+
// Matching predicates: none
128+
// tidy-alphabetical-start
121129
StaticLifetime: "'static",
130+
UnderscoreLifetime: "'_",
131+
// tidy-alphabetical-end
122132

123133
// Weak keywords, have special meaning only in specific contexts.
124-
// Matching predicates: `is_any_keyword`
134+
// Matching predicates: none
135+
// tidy-alphabetical-start
125136
Auto: "auto",
126137
Builtin: "builtin",
127138
Catch: "catch",
139+
ContractEnsures: "contract_ensures",
140+
ContractRequires: "contract_requires",
128141
Default: "default",
129142
MacroRules: "macro_rules",
130143
Raw: "raw",
131144
Reuse: "reuse",
132-
ContractEnsures: "contract_ensures",
133-
ContractRequires: "contract_requires",
134145
Safe: "safe",
135146
Union: "union",
136147
Yeet: "yeet",
148+
// tidy-alphabetical-end
137149
}
138150

139151
// Pre-interned symbols that can be referred to with `rustc_span::sym::*`.
@@ -2677,11 +2689,6 @@ pub mod sym {
26772689
}
26782690

26792691
impl Symbol {
2680-
/// Don't use this unless you're doing something very loose and heuristic-y.
2681-
pub fn is_any_keyword(self) -> bool {
2682-
self >= kw::As && self <= kw::Yeet
2683-
}
2684-
26852692
fn is_special(self) -> bool {
26862693
self <= kw::Underscore
26872694
}
@@ -2690,14 +2697,14 @@ impl Symbol {
26902697
self >= kw::As && self <= kw::While
26912698
}
26922699

2693-
fn is_used_keyword_conditional(self, edition: impl FnOnce() -> Edition) -> bool {
2694-
(self >= kw::Async && self <= kw::Dyn) && edition() >= Edition::Edition2018
2695-
}
2696-
26972700
fn is_unused_keyword_always(self) -> bool {
26982701
self >= kw::Abstract && self <= kw::Yield
26992702
}
27002703

2704+
fn is_used_keyword_conditional(self, edition: impl FnOnce() -> Edition) -> bool {
2705+
(self >= kw::Async && self <= kw::Dyn) && edition() >= Edition::Edition2018
2706+
}
2707+
27012708
fn is_unused_keyword_conditional(self, edition: impl Copy + FnOnce() -> Edition) -> bool {
27022709
self == kw::Gen && edition().at_least_rust_2024()
27032710
|| self == kw::Try && edition().at_least_rust_2018()
@@ -2738,11 +2745,6 @@ impl Symbol {
27382745
}
27392746

27402747
impl Ident {
2741-
/// Don't use this unless you're doing something very loose and heuristic-y.
2742-
pub fn is_any_keyword(self) -> bool {
2743-
self.name.is_any_keyword()
2744-
}
2745-
27462748
/// Returns `true` for reserved identifiers used internally for elided lifetimes,
27472749
/// unnamed method parameters, crate root module, error recovery etc.
27482750
pub fn is_special(self) -> bool {
@@ -2792,7 +2794,7 @@ impl Ident {
27922794
/// *Note:* Please update this if a new keyword is added beyond the current
27932795
/// range.
27942796
pub fn used_keywords(edition: impl Copy + FnOnce() -> Edition) -> Vec<Symbol> {
2795-
(kw::Empty.as_u32()..kw::Yeet.as_u32())
2797+
(kw::DollarCrate.as_u32()..kw::Yeet.as_u32())
27962798
.filter_map(|kw| {
27972799
let kw = Symbol::new(kw);
27982800
if kw.is_used_keyword_always() || kw.is_used_keyword_conditional(edition) {

Diff for: src/tools/rustfmt/src/parse/macros/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ pub(crate) struct ParsedMacroArgs {
8181
}
8282

8383
fn check_keyword<'a, 'b: 'a>(parser: &'a mut Parser<'b>) -> Option<MacroArg> {
84-
if parser.token.is_any_keyword()
84+
if parser.token.is_reserved_ident()
8585
&& parser.look_ahead(1, |t| *t == TokenKind::Eof || *t == TokenKind::Comma)
8686
{
8787
let keyword = parser.token.ident().unwrap().0.name;

0 commit comments

Comments
 (0)