Skip to content

Commit 64abe8b

Browse files
committed
Simplify AllKeywords.
It's a verbose reinvention of a range type, and can be cut down a lot.
1 parent 37e7459 commit 64abe8b

File tree

2 files changed

+17
-39
lines changed

2 files changed

+17
-39
lines changed

compiler/rustc_parse/src/parser/diagnostics.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use rustc_errors::{
2222
use rustc_session::errors::ExprParenthesesNeeded;
2323
use rustc_span::edit_distance::find_best_match_for_name;
2424
use rustc_span::source_map::Spanned;
25-
use rustc_span::symbol::AllKeywords;
25+
use rustc_span::symbol::used_keywords;
2626
use rustc_span::{BytePos, DUMMY_SP, Ident, Span, SpanSnippetError, Symbol, kw, sym};
2727
use thin_vec::{ThinVec, thin_vec};
2828
use tracing::{debug, trace};
@@ -811,7 +811,7 @@ impl<'a> Parser<'a> {
811811
// so that it gets generated only when the diagnostic needs it.
812812
// Also, it is unlikely that this list is generated multiple times because the
813813
// parser halts after execution hits this path.
814-
let all_keywords = AllKeywords::new().collect_used(|| prev_ident.span.edition());
814+
let all_keywords = used_keywords(|| prev_ident.span.edition());
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 {}`.

compiler/rustc_span/src/symbol.rs

+15-37
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_special`, `is_used_keyword`/`is_unused_keyword`
24-
// and `AllKeywords`.
23+
// If you modify this list, adjust any relevant `Symbol::{is,can_be}_*` functions and
24+
// `used_keywords`.
2525
// But this should rarely be necessary if the keywords are kept in alphabetic order.
2626
Keywords {
2727
// Special reserved identifiers used internally for elided lifetimes,
@@ -2683,41 +2683,19 @@ impl Ident {
26832683
}
26842684
}
26852685

2686-
/// An iterator over all the keywords in Rust.
2687-
#[derive(Copy, Clone)]
2688-
pub struct AllKeywords {
2689-
curr_idx: u32,
2690-
end_idx: u32,
2691-
}
2692-
2693-
impl AllKeywords {
2694-
/// Initialize a new iterator over all the keywords.
2695-
///
2696-
/// *Note:* Please update this if a new keyword is added beyond the current
2697-
/// range.
2698-
pub fn new() -> Self {
2699-
AllKeywords { curr_idx: kw::Empty.as_u32(), end_idx: kw::Yeet.as_u32() }
2700-
}
2701-
2702-
/// Collect all the keywords in a given edition into a vector.
2703-
pub fn collect_used(&self, edition: impl Copy + FnOnce() -> Edition) -> Vec<Symbol> {
2704-
self.filter(|&keyword| {
2705-
keyword.is_used_keyword_always() || keyword.is_used_keyword_conditional(edition)
2686+
/// Collect all the keywords in a given edition into a vector.
2687+
///
2688+
/// *Note:* Please update this if a new keyword is added beyond the current
2689+
/// range.
2690+
pub fn used_keywords(edition: impl Copy + FnOnce() -> Edition) -> Vec<Symbol> {
2691+
(kw::Empty.as_u32()..kw::Yeet.as_u32())
2692+
.filter_map(|kw| {
2693+
let kw = Symbol::new(kw);
2694+
if kw.is_used_keyword_always() || kw.is_used_keyword_conditional(edition) {
2695+
Some(kw)
2696+
} else {
2697+
None
2698+
}
27062699
})
27072700
.collect()
2708-
}
2709-
}
2710-
2711-
impl Iterator for AllKeywords {
2712-
type Item = Symbol;
2713-
2714-
fn next(&mut self) -> Option<Self::Item> {
2715-
if self.curr_idx <= self.end_idx {
2716-
let keyword = Symbol::new(self.curr_idx);
2717-
self.curr_idx += 1;
2718-
Some(keyword)
2719-
} else {
2720-
None
2721-
}
2722-
}
27232701
}

0 commit comments

Comments
 (0)