Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rename Parser::expected_tokens as Parser::expected_token_types.
Browse files Browse the repository at this point in the history
Because the `Token` type is similar to but different to the `TokenType`
type, and the difference is important, so we want to avoid confusion.
nnethercote committed Dec 3, 2024
1 parent e93e096 commit 584d04e
Showing 6 changed files with 24 additions and 23 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_builtin_macros/src/format.rs
Original file line number Diff line number Diff line change
@@ -96,7 +96,7 @@ fn parse_args<'a>(ecx: &ExtCtxt<'a>, sp: Span, tts: TokenStream) -> PResult<'a,
while p.token != token::Eof {
if !p.eat(&token::Comma) {
if first {
p.clear_expected_tokens();
p.clear_expected_token_types();
}

match p.expect(&token::Comma) {
19 changes: 10 additions & 9 deletions compiler/rustc_parse/src/parser/diagnostics.rs
Original file line number Diff line number Diff line change
@@ -483,9 +483,10 @@ impl<'a> Parser<'a> {
})
}

self.expected_tokens.extend(edible.iter().chain(inedible).cloned().map(TokenType::Token));
self.expected_token_types
.extend(edible.iter().chain(inedible).cloned().map(TokenType::Token));
let mut expected = self
.expected_tokens
.expected_token_types
.iter()
.filter(|token| {
// Filter out suggestions that suggest the same token which was found and deemed incorrect.
@@ -785,17 +786,17 @@ impl<'a> Parser<'a> {
let Some((curr_ident, _)) = self.token.ident() else {
return;
};
let expected_tokens: &[TokenType] =
let expected_token_types: &[TokenType] =
expected.len().checked_sub(10).map_or(&expected, |index| &expected[index..]);
let expected_keywords: Vec<Symbol> = expected_tokens
let expected_keywords: Vec<Symbol> = expected_token_types
.iter()
.filter_map(|token| if let TokenType::Keyword(kw) = token { Some(*kw) } else { None })
.collect();

// When there are a few keywords in the last ten elements of `self.expected_tokens` and the current
// token is an identifier, it's probably a misspelled keyword.
// This handles code like `async Move {}`, misspelled `if` in match guard, misspelled `else` in `if`-`else`
// and mispelled `where` in a where clause.
// When there are a few keywords in the last ten elements of `self.expected_token_types`
// and the current token is an identifier, it's probably a misspelled keyword. This handles
// code like `async Move {}`, misspelled `if` in match guard, misspelled `else` in
// `if`-`else` and mispelled `where` in a where clause.
if !expected_keywords.is_empty()
&& !curr_ident.is_used_keyword()
&& let Some(misspelled_kw) = find_similar_kw(curr_ident, &expected_keywords)
@@ -3016,7 +3017,7 @@ impl<'a> Parser<'a> {
/// Check for exclusive ranges written as `..<`
pub(crate) fn maybe_err_dotdotlt_syntax(&self, maybe_lt: Token, mut err: PErr<'a>) -> PErr<'a> {
if maybe_lt == token::Lt
&& (self.expected_tokens.contains(&TokenType::Token(token::Gt))
&& (self.expected_token_types.contains(&TokenType::Token(token::Gt))
|| matches!(self.token.kind, token::Literal(..)))
{
err.span_suggestion(
2 changes: 1 addition & 1 deletion compiler/rustc_parse/src/parser/expr.rs
Original file line number Diff line number Diff line change
@@ -153,7 +153,7 @@ impl<'a> Parser<'a> {
return Ok((lhs, parsed_something));
}

self.expected_tokens.push(TokenType::Operator);
self.expected_token_types.push(TokenType::Operator);
while let Some(op) = self.check_assoc_op() {
let lhs_span = self.interpolated_or_expr_span(&lhs);
let cur_op_span = self.token.span;
20 changes: 10 additions & 10 deletions compiler/rustc_parse/src/parser/mod.rs
Original file line number Diff line number Diff line change
@@ -144,7 +144,7 @@ pub struct Parser<'a> {
pub prev_token: Token,
pub capture_cfg: bool,
restrictions: Restrictions,
expected_tokens: Vec<TokenType>,
expected_token_types: Vec<TokenType>,
token_cursor: TokenCursor,
// The number of calls to `bump`, i.e. the position in the token stream.
num_bump_calls: u32,
@@ -465,7 +465,7 @@ impl<'a> Parser<'a> {
prev_token: Token::dummy(),
capture_cfg: false,
restrictions: Restrictions::empty(),
expected_tokens: Vec::new(),
expected_token_types: Vec::new(),
token_cursor: TokenCursor { tree_cursor: stream.into_trees(), stack: Vec::new() },
num_bump_calls: 0,
break_last_token: 0,
@@ -529,7 +529,7 @@ impl<'a> Parser<'a> {

/// Expects and consumes the token `t`. Signals an error if the next token is not `t`.
pub fn expect(&mut self, t: &TokenKind) -> PResult<'a, Recovered> {
if self.expected_tokens.is_empty() {
if self.expected_token_types.is_empty() {
if self.token == *t {
self.bump();
Ok(Recovered::No)
@@ -600,7 +600,7 @@ impl<'a> Parser<'a> {
fn check(&mut self, tok: &TokenKind) -> bool {
let is_present = self.token == *tok;
if !is_present {
self.expected_tokens.push(TokenType::Token(tok.clone()));
self.expected_token_types.push(TokenType::Token(tok.clone()));
}
is_present
}
@@ -641,7 +641,7 @@ impl<'a> Parser<'a> {
#[inline]
#[must_use]
fn check_keyword(&mut self, kw: Symbol) -> bool {
self.expected_tokens.push(TokenType::Keyword(kw));
self.expected_token_types.push(TokenType::Keyword(kw));
self.token.is_keyword(kw)
}

@@ -730,7 +730,7 @@ impl<'a> Parser<'a> {
if ok {
true
} else {
self.expected_tokens.push(typ);
self.expected_token_types.push(typ);
false
}
}
@@ -807,7 +807,7 @@ impl<'a> Parser<'a> {
true
}
_ => {
self.expected_tokens.push(TokenType::Token(expected));
self.expected_token_types.push(TokenType::Token(expected));
false
}
}
@@ -1155,7 +1155,7 @@ impl<'a> Parser<'a> {
self.token_spacing = next_spacing;

// Diagnostics.
self.expected_tokens.clear();
self.expected_token_types.clear();
}

/// Advance the parser by one token.
@@ -1643,8 +1643,8 @@ impl<'a> Parser<'a> {
DebugParser { parser: self, lookahead }
}

pub fn clear_expected_tokens(&mut self) {
self.expected_tokens.clear();
pub fn clear_expected_token_types(&mut self) {
self.expected_token_types.clear();
}

pub fn approx_token_stream_pos(&self) -> u32 {
2 changes: 1 addition & 1 deletion compiler/rustc_parse/src/parser/path.rs
Original file line number Diff line number Diff line change
@@ -301,7 +301,7 @@ impl<'a> Parser<'a> {
)
};
let check_args_start = |this: &mut Self| {
this.expected_tokens.extend_from_slice(&[
this.expected_token_types.extend_from_slice(&[
TokenType::Token(token::Lt),
TokenType::Token(token::OpenDelim(Delimiter::Parenthesis)),
]);
2 changes: 1 addition & 1 deletion compiler/rustc_parse/src/parser/ty.rs
Original file line number Diff line number Diff line change
@@ -1264,7 +1264,7 @@ impl<'a> Parser<'a> {
}

pub(super) fn check_lifetime(&mut self) -> bool {
self.expected_tokens.push(TokenType::Lifetime);
self.expected_token_types.push(TokenType::Lifetime);
self.token.is_lifetime()
}

0 comments on commit 584d04e

Please sign in to comment.