Skip to content

Commit 28146f4

Browse files
committed
Simplify AllKeywords.
It's a verbose reinvention of a range type, and can be cut down a lot.
1 parent e4cc8b1 commit 28146f4

File tree

2 files changed

+15
-37
lines changed

2 files changed

+15
-37
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, Ident, kw, sym};
25+
use rustc_span::symbol::{Ident, kw, sym, used_keywords};
2626
use rustc_span::{BytePos, DUMMY_SP, Span, SpanSnippetError, Symbol};
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

+13-35
Original file line numberDiff line numberDiff line change
@@ -2677,41 +2677,19 @@ impl Ident {
26772677
}
26782678
}
26792679

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

0 commit comments

Comments
 (0)