Skip to content

Commit

Permalink
XXX: rm RUST_KW
Browse files Browse the repository at this point in the history
  • Loading branch information
nnethercote committed Dec 13, 2024
1 parent 28146f4 commit b3dc14a
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 83 deletions.
5 changes: 5 additions & 0 deletions compiler/rustc_ast/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -917,6 +917,11 @@ impl Token {
self.is_non_raw_ident_where(Ident::is_path_segment_keyword)
}

/// Returns true for any keyword.
pub fn is_any_keyword(&self) -> bool {
self.is_non_raw_ident_where(Ident::is_any_keyword)
}

/// Returns true for reserved identifiers.
pub fn is_reserved_ident(&self) -> bool {
self.is_non_raw_ident_where(Ident::is_reserved_ident)
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_parse/src/parser/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -815,8 +815,8 @@ impl<'a> Parser<'a> {

// Otherwise, check the previous token with all the keywords as possible candidates.
// This handles code like `Struct Human;` and `While a < b {}`.
// We check the previous token only when the current token is an identifier to avoid false
// positives like suggesting keyword `for` for `extern crate foo {}`.
// We check the previous token only when the current token is an identifier to avoid
// false positives like suggesting keyword `for` for `extern crate foo {}`.
if let Some(misspelled_kw) = find_similar_kw(prev_ident, &all_keywords) {
err.subdiagnostic(misspelled_kw);
// We don't want other suggestions to be added as they are most likely meaningless
Expand Down
13 changes: 11 additions & 2 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ mod tests;

// The proc macro code for this is in `compiler/rustc_macros/src/symbols.rs`.
symbols! {
// If you modify this list, adjust `is_reserved_ident`, `is_used_keyword`/`is_unused_keyword`
// and `AllKeywords`.
// 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.
Keywords {
// Keywords that are used in stable Rust.
Expand Down Expand Up @@ -2584,6 +2584,10 @@ pub mod sym {
}

impl Symbol {
fn is_any_keyword(self) -> bool {
self >= kw::As && self <= kw::Yeet
}

fn is_reserved_ident(self) -> bool {
self == sym::dollar_crate || self == sym::underscore
}
Expand Down Expand Up @@ -2640,6 +2644,11 @@ impl Symbol {
}

impl Ident {
/// Returns `true` for any keyword.
pub fn is_any_keyword(self) -> bool {
self.name.is_any_keyword()
}

/// Returns `true` for reserved identifiers.
pub fn is_reserved_ident(self) -> bool {
self.name.is_reserved_ident()
Expand Down
91 changes: 12 additions & 79 deletions src/tools/rustfmt/src/parse/macros/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use rustc_ast::{ast, ptr};
use rustc_parse::MACRO_ARGUMENTS;
use rustc_parse::parser::{ForceCollect, Parser, Recovery};
use rustc_session::parse::ParseSess;
use rustc_span::Symbol;
use rustc_span::symbol::{self, kw};
use rustc_span::symbol;

use crate::macros::MacroArg;
use crate::rewrite::RewriteContext;
Expand Down Expand Up @@ -82,18 +81,18 @@ pub(crate) struct ParsedMacroArgs {
}

fn check_keyword<'a, 'b: 'a>(parser: &'a mut Parser<'b>) -> Option<MacroArg> {
for &keyword in RUST_KW.iter() {
if parser.token.is_keyword(keyword)
&& parser.look_ahead(1, |t| *t == TokenKind::Eof || *t == TokenKind::Comma)
{
parser.bump();
return Some(MacroArg::Keyword(
symbol::Ident::with_dummy_span(keyword),
parser.prev_token.span,
));
}
if parser.token.is_any_keyword()
&& parser.look_ahead(1, |t| *t == TokenKind::Eof || *t == TokenKind::Comma)
{
let keyword = parser.token.ident().unwrap().0.name;
parser.bump();
Some(MacroArg::Keyword(
symbol::Ident::with_dummy_span(keyword),
parser.prev_token.span,
))
} else {
None
}
None
}

pub(crate) fn parse_macro_args(
Expand Down Expand Up @@ -169,69 +168,3 @@ pub(crate) fn parse_expr(
let mut parser = build_parser(context, tokens);
parser.parse_expr().ok()
}

const RUST_KW: [Symbol; 63] = [
kw::As,
kw::Break,
kw::Const,
kw::Continue,
kw::Crate,
kw::Else,
kw::Enum,
kw::Extern,
kw::False,
kw::Fn,
kw::For,
kw::If,
kw::Impl,
kw::In,
kw::Let,
kw::Loop,
kw::Match,
kw::Mod,
kw::Move,
kw::Mut,
kw::Pub,
kw::Ref,
kw::Return,
kw::SelfLower,
kw::SelfUpper,
kw::Static,
kw::Struct,
kw::Super,
kw::Trait,
kw::True,
kw::Type,
kw::Unsafe,
kw::Use,
kw::Where,
kw::While,
kw::Abstract,
kw::Become,
kw::Box,
kw::Do,
kw::Final,
kw::Macro,
kw::Override,
kw::Priv,
kw::Typeof,
kw::Unsized,
kw::Virtual,
kw::Yield,
kw::Async,
kw::Await,
kw::Dyn,
kw::Gen,
kw::Try,
kw::Auto,
kw::Builtin,
kw::Catch,
kw::Default,
kw::MacroRules,
kw::Raw,
kw::Reuse,
kw::Safe,
kw::StaticLifetime,
kw::Union,
kw::Yeet,
];

0 comments on commit b3dc14a

Please sign in to comment.