Skip to content

Commit b3dc14a

Browse files
committed
XXX: rm RUST_KW
1 parent 28146f4 commit b3dc14a

File tree

4 files changed

+30
-83
lines changed

4 files changed

+30
-83
lines changed

compiler/rustc_ast/src/token.rs

+5
Original file line numberDiff line numberDiff line change
@@ -917,6 +917,11 @@ impl Token {
917917
self.is_non_raw_ident_where(Ident::is_path_segment_keyword)
918918
}
919919

920+
/// Returns true for any keyword.
921+
pub fn is_any_keyword(&self) -> bool {
922+
self.is_non_raw_ident_where(Ident::is_any_keyword)
923+
}
924+
920925
/// Returns true for reserved identifiers.
921926
pub fn is_reserved_ident(&self) -> bool {
922927
self.is_non_raw_ident_where(Ident::is_reserved_ident)

compiler/rustc_parse/src/parser/diagnostics.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -815,8 +815,8 @@ impl<'a> Parser<'a> {
815815

816816
// Otherwise, check the previous token with all the keywords as possible candidates.
817817
// This handles code like `Struct Human;` and `While a < b {}`.
818-
// We check the previous token only when the current token is an identifier to avoid false
819-
// positives like suggesting keyword `for` for `extern crate foo {}`.
818+
// We check the previous token only when the current token is an identifier to avoid
819+
// false positives like suggesting keyword `for` for `extern crate foo {}`.
820820
if let Some(misspelled_kw) = find_similar_kw(prev_ident, &all_keywords) {
821821
err.subdiagnostic(misspelled_kw);
822822
// We don't want other suggestions to be added as they are most likely meaningless

compiler/rustc_span/src/symbol.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ 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_reserved_ident`, `is_used_keyword`/`is_unused_keyword`
24-
// and `AllKeywords`.
23+
// If you modify this list, adjust `is_any_keyword`, `is_reserved_ident`,
24+
// `is_used_keyword`/`is_unused_keyword` and `AllKeywords`.
2525
// But this should rarely be necessary if the keywords are kept in alphabetic order.
2626
Keywords {
2727
// Keywords that are used in stable Rust.
@@ -2584,6 +2584,10 @@ pub mod sym {
25842584
}
25852585

25862586
impl Symbol {
2587+
fn is_any_keyword(self) -> bool {
2588+
self >= kw::As && self <= kw::Yeet
2589+
}
2590+
25872591
fn is_reserved_ident(self) -> bool {
25882592
self == sym::dollar_crate || self == sym::underscore
25892593
}
@@ -2640,6 +2644,11 @@ impl Symbol {
26402644
}
26412645

26422646
impl Ident {
2647+
/// Returns `true` for any keyword.
2648+
pub fn is_any_keyword(self) -> bool {
2649+
self.name.is_any_keyword()
2650+
}
2651+
26432652
/// Returns `true` for reserved identifiers.
26442653
pub fn is_reserved_ident(self) -> bool {
26452654
self.name.is_reserved_ident()

src/tools/rustfmt/src/parse/macros/mod.rs

+12-79
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ use rustc_ast::{ast, ptr};
44
use rustc_parse::MACRO_ARGUMENTS;
55
use rustc_parse::parser::{ForceCollect, Parser, Recovery};
66
use rustc_session::parse::ParseSess;
7-
use rustc_span::Symbol;
8-
use rustc_span::symbol::{self, kw};
7+
use rustc_span::symbol;
98

109
use crate::macros::MacroArg;
1110
use crate::rewrite::RewriteContext;
@@ -82,18 +81,18 @@ pub(crate) struct ParsedMacroArgs {
8281
}
8382

8483
fn check_keyword<'a, 'b: 'a>(parser: &'a mut Parser<'b>) -> Option<MacroArg> {
85-
for &keyword in RUST_KW.iter() {
86-
if parser.token.is_keyword(keyword)
87-
&& parser.look_ahead(1, |t| *t == TokenKind::Eof || *t == TokenKind::Comma)
88-
{
89-
parser.bump();
90-
return Some(MacroArg::Keyword(
91-
symbol::Ident::with_dummy_span(keyword),
92-
parser.prev_token.span,
93-
));
94-
}
84+
if parser.token.is_any_keyword()
85+
&& parser.look_ahead(1, |t| *t == TokenKind::Eof || *t == TokenKind::Comma)
86+
{
87+
let keyword = parser.token.ident().unwrap().0.name;
88+
parser.bump();
89+
Some(MacroArg::Keyword(
90+
symbol::Ident::with_dummy_span(keyword),
91+
parser.prev_token.span,
92+
))
93+
} else {
94+
None
9595
}
96-
None
9796
}
9897

9998
pub(crate) fn parse_macro_args(
@@ -169,69 +168,3 @@ pub(crate) fn parse_expr(
169168
let mut parser = build_parser(context, tokens);
170169
parser.parse_expr().ok()
171170
}
172-
173-
const RUST_KW: [Symbol; 63] = [
174-
kw::As,
175-
kw::Break,
176-
kw::Const,
177-
kw::Continue,
178-
kw::Crate,
179-
kw::Else,
180-
kw::Enum,
181-
kw::Extern,
182-
kw::False,
183-
kw::Fn,
184-
kw::For,
185-
kw::If,
186-
kw::Impl,
187-
kw::In,
188-
kw::Let,
189-
kw::Loop,
190-
kw::Match,
191-
kw::Mod,
192-
kw::Move,
193-
kw::Mut,
194-
kw::Pub,
195-
kw::Ref,
196-
kw::Return,
197-
kw::SelfLower,
198-
kw::SelfUpper,
199-
kw::Static,
200-
kw::Struct,
201-
kw::Super,
202-
kw::Trait,
203-
kw::True,
204-
kw::Type,
205-
kw::Unsafe,
206-
kw::Use,
207-
kw::Where,
208-
kw::While,
209-
kw::Abstract,
210-
kw::Become,
211-
kw::Box,
212-
kw::Do,
213-
kw::Final,
214-
kw::Macro,
215-
kw::Override,
216-
kw::Priv,
217-
kw::Typeof,
218-
kw::Unsized,
219-
kw::Virtual,
220-
kw::Yield,
221-
kw::Async,
222-
kw::Await,
223-
kw::Dyn,
224-
kw::Gen,
225-
kw::Try,
226-
kw::Auto,
227-
kw::Builtin,
228-
kw::Catch,
229-
kw::Default,
230-
kw::MacroRules,
231-
kw::Raw,
232-
kw::Reuse,
233-
kw::Safe,
234-
kw::StaticLifetime,
235-
kw::Union,
236-
kw::Yeet,
237-
];

0 commit comments

Comments
 (0)