Skip to content

Commit aeca2c2

Browse files
authored
Rollup merge of #134253 - nnethercote:overhaul-keywords, r=petrochenkov
Overhaul keyword handling The compiler's list of keywords has some problems. - It contains several items that aren't keywords. - The order isn't quite right in a couple of places. - Some of the names of predicates relating to keywords are confusing. - rustdoc and rustfmt have their own (incorrect) versions of the keyword list. - `AllKeywords` is unnecessarily complex. r? ```@jieyouxu```
2 parents cbec1df + 3aadb31 commit aeca2c2

File tree

1 file changed

+12
-75
lines changed

1 file changed

+12
-75
lines changed

src/parse/macros/mod.rs

Lines changed: 12 additions & 75 deletions
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,65 +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; 59] = [
174-
kw::PathRoot,
175-
kw::DollarCrate,
176-
kw::Underscore,
177-
kw::As,
178-
kw::Box,
179-
kw::Break,
180-
kw::Const,
181-
kw::Continue,
182-
kw::Crate,
183-
kw::Else,
184-
kw::Enum,
185-
kw::Extern,
186-
kw::False,
187-
kw::Fn,
188-
kw::For,
189-
kw::If,
190-
kw::Impl,
191-
kw::In,
192-
kw::Let,
193-
kw::Loop,
194-
kw::Match,
195-
kw::Mod,
196-
kw::Move,
197-
kw::Mut,
198-
kw::Pub,
199-
kw::Ref,
200-
kw::Return,
201-
kw::SelfLower,
202-
kw::SelfUpper,
203-
kw::Static,
204-
kw::Struct,
205-
kw::Super,
206-
kw::Trait,
207-
kw::True,
208-
kw::Type,
209-
kw::Unsafe,
210-
kw::Use,
211-
kw::Where,
212-
kw::While,
213-
kw::Abstract,
214-
kw::Become,
215-
kw::Do,
216-
kw::Final,
217-
kw::Macro,
218-
kw::Override,
219-
kw::Priv,
220-
kw::Typeof,
221-
kw::Unsized,
222-
kw::Virtual,
223-
kw::Yield,
224-
kw::Dyn,
225-
kw::Async,
226-
kw::Try,
227-
kw::UnderscoreLifetime,
228-
kw::StaticLifetime,
229-
kw::Auto,
230-
kw::Catch,
231-
kw::Default,
232-
kw::Union,
233-
];

0 commit comments

Comments
 (0)