Skip to content

Commit 8fcb1a3

Browse files
Rollup merge of #69034 - petrochenkov:notokind, r=Centril
parser: Remove `Parser::prev_token_kind` Follow-up to #69006. r? @Centril
2 parents 3cf7cd8 + 8d79921 commit 8fcb1a3

File tree

4 files changed

+25
-56
lines changed

4 files changed

+25
-56
lines changed

src/librustc_parse/parser/expr.rs

+12-17
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::pat::{GateOr, PARAM_EXPECTED};
22
use super::ty::{AllowPlus, RecoverQPath};
3-
use super::{BlockMode, Parser, PathStyle, PrevTokenKind, Restrictions, TokenType};
3+
use super::{BlockMode, Parser, PathStyle, Restrictions, TokenType};
44
use super::{SemiColonMode, SeqSep, TokenExpectType};
55
use crate::maybe_recover_from_interpolated_ty_qpath;
66

@@ -166,17 +166,10 @@ impl<'a> Parser<'a> {
166166

167167
self.expected_tokens.push(TokenType::Operator);
168168
while let Some(op) = self.check_assoc_op() {
169-
// Adjust the span for interpolated LHS to point to the `$lhs` token and not to what
170-
// it refers to. Interpolated identifiers are unwrapped early and never show up here
171-
// as `PrevTokenKind::Interpolated` so if LHS is a single identifier we always process
172-
// it as "interpolated", it doesn't change the answer for non-interpolated idents.
173-
let lhs_span = match (self.prev_token_kind, &lhs.kind) {
174-
(PrevTokenKind::Interpolated, _) => self.prev_span,
175-
(PrevTokenKind::Ident, &ExprKind::Path(None, ref path))
176-
if path.segments.len() == 1 =>
177-
{
178-
self.prev_span
179-
}
169+
// Adjust the span for interpolated LHS to point to the `$lhs` token
170+
// and not to what it refers to.
171+
let lhs_span = match self.unnormalized_prev_token().kind {
172+
TokenKind::Interpolated(..) => self.prev_span,
180173
_ => lhs.span,
181174
};
182175

@@ -535,11 +528,13 @@ impl<'a> Parser<'a> {
535528
expr: PResult<'a, P<Expr>>,
536529
) -> PResult<'a, (Span, P<Expr>)> {
537530
expr.map(|e| {
538-
if self.prev_token_kind == PrevTokenKind::Interpolated {
539-
(self.prev_span, e)
540-
} else {
541-
(e.span, e)
542-
}
531+
(
532+
match self.unnormalized_prev_token().kind {
533+
TokenKind::Interpolated(..) => self.prev_span,
534+
_ => e.span,
535+
},
536+
e,
537+
)
543538
})
544539
}
545540

src/librustc_parse/parser/mod.rs

+6-32
Original file line numberDiff line numberDiff line change
@@ -82,18 +82,6 @@ macro_rules! maybe_recover_from_interpolated_ty_qpath {
8282
};
8383
}
8484

85-
#[derive(Debug, Clone, Copy, PartialEq)]
86-
enum PrevTokenKind {
87-
DocComment,
88-
Comma,
89-
Plus,
90-
Interpolated,
91-
Eof,
92-
Ident,
93-
BitOr,
94-
Other,
95-
}
96-
9785
#[derive(Clone)]
9886
pub struct Parser<'a> {
9987
pub sess: &'a ParseSess,
@@ -114,9 +102,6 @@ pub struct Parser<'a> {
114102
/// Preferable use is through the `unnormalized_prev_token()` getter.
115103
/// Use span from this token if you need to concatenate it with some neighbouring spans.
116104
unnormalized_prev_token: Option<Token>,
117-
/// Equivalent to `prev_token.kind` in simplified form.
118-
/// FIXME: Remove in favor of `(unnormalized_)prev_token().kind`.
119-
prev_token_kind: PrevTokenKind,
120105
/// Equivalent to `unnormalized_prev_token().span`.
121106
/// FIXME: Remove in favor of `(unnormalized_)prev_token().span`.
122107
pub prev_span: Span,
@@ -395,7 +380,6 @@ impl<'a> Parser<'a> {
395380
unnormalized_token: None,
396381
prev_token: Token::dummy(),
397382
unnormalized_prev_token: None,
398-
prev_token_kind: PrevTokenKind::Other,
399383
prev_span: DUMMY_SP,
400384
restrictions: Restrictions::empty(),
401385
recurse_into_file_modules,
@@ -522,10 +506,11 @@ impl<'a> Parser<'a> {
522506
self.bump();
523507
Ok(Ident::new(name, span))
524508
}
525-
_ => Err(if self.prev_token_kind == PrevTokenKind::DocComment {
526-
self.span_fatal_err(self.prev_span, Error::UselessDocComment)
527-
} else {
528-
self.expected_ident_found()
509+
_ => Err(match self.prev_token.kind {
510+
TokenKind::DocComment(..) => {
511+
self.span_fatal_err(self.prev_span, Error::UselessDocComment)
512+
}
513+
_ => self.expected_ident_found(),
529514
}),
530515
}
531516
}
@@ -907,7 +892,7 @@ impl<'a> Parser<'a> {
907892

908893
/// Advance the parser by one token.
909894
pub fn bump(&mut self) {
910-
if self.prev_token_kind == PrevTokenKind::Eof {
895+
if self.prev_token.kind == TokenKind::Eof {
911896
// Bumping after EOF is a bad sign, usually an infinite loop.
912897
let msg = "attempted to bump the parser past EOF (may be stuck in a loop)";
913898
self.span_bug(self.token.span, msg);
@@ -919,16 +904,6 @@ impl<'a> Parser<'a> {
919904
self.unnormalized_prev_token = self.unnormalized_token.take();
920905

921906
// Update fields derived from the previous token.
922-
self.prev_token_kind = match self.prev_token.kind {
923-
token::DocComment(..) => PrevTokenKind::DocComment,
924-
token::Comma => PrevTokenKind::Comma,
925-
token::BinOp(token::Plus) => PrevTokenKind::Plus,
926-
token::BinOp(token::Or) => PrevTokenKind::BitOr,
927-
token::Interpolated(..) => PrevTokenKind::Interpolated,
928-
token::Eof => PrevTokenKind::Eof,
929-
token::Ident(..) => PrevTokenKind::Ident,
930-
_ => PrevTokenKind::Other,
931-
};
932907
self.prev_span = self.unnormalized_prev_token().span;
933908

934909
self.expected_tokens.clear();
@@ -948,7 +923,6 @@ impl<'a> Parser<'a> {
948923
self.unnormalized_prev_token = self.unnormalized_token.take();
949924

950925
// Update fields derived from the previous token.
951-
self.prev_token_kind = PrevTokenKind::Other;
952926
self.prev_span = self.unnormalized_prev_token().span.with_hi(span.lo());
953927

954928
self.expected_tokens.clear();

src/librustc_parse/parser/stmt.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use super::diagnostics::Error;
22
use super::expr::LhsExpr;
33
use super::pat::GateOr;
44
use super::path::PathStyle;
5-
use super::{BlockMode, Parser, PrevTokenKind, Restrictions, SemiColonMode};
5+
use super::{BlockMode, Parser, Restrictions, SemiColonMode};
66
use crate::maybe_whole;
77
use crate::DirectoryOwnership;
88

@@ -190,7 +190,7 @@ impl<'a> Parser<'a> {
190190
/// Also error if the previous token was a doc comment.
191191
fn error_outer_attrs(&self, attrs: &[Attribute]) {
192192
if !attrs.is_empty() {
193-
if self.prev_token_kind == PrevTokenKind::DocComment {
193+
if matches!(self.prev_token.kind, TokenKind::DocComment(..)) {
194194
self.span_fatal_err(self.prev_span, Error::UselessDocComment).emit();
195195
} else if attrs.iter().any(|a| a.style == AttrStyle::Outer) {
196196
self.struct_span_err(self.token.span, "expected statement after outer attribute")

src/librustc_parse/parser/ty.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use super::item::ParamCfg;
2-
use super::{Parser, PathStyle, PrevTokenKind, TokenType};
2+
use super::{Parser, PathStyle, TokenType};
33

44
use crate::{maybe_recover_from_interpolated_ty_qpath, maybe_whole};
55

@@ -14,7 +14,7 @@ use syntax::ast::{
1414
};
1515
use syntax::ast::{Mac, Mutability};
1616
use syntax::ptr::P;
17-
use syntax::token::{self, Token};
17+
use syntax::token::{self, Token, TokenKind};
1818

1919
/// Any `?` or `?const` modifiers that appear at the start of a bound.
2020
struct BoundModifiers {
@@ -196,7 +196,7 @@ impl<'a> Parser<'a> {
196196
let mut trailing_plus = false;
197197
let (ts, trailing) = self.parse_paren_comma_seq(|p| {
198198
let ty = p.parse_ty()?;
199-
trailing_plus = p.prev_token_kind == PrevTokenKind::Plus;
199+
trailing_plus = p.prev_token.kind == TokenKind::BinOp(token::Plus);
200200
Ok(ty)
201201
})?;
202202

@@ -320,7 +320,7 @@ impl<'a> Parser<'a> {
320320
fn parse_impl_ty(&mut self, impl_dyn_multi: &mut bool) -> PResult<'a, TyKind> {
321321
// Always parse bounds greedily for better error recovery.
322322
let bounds = self.parse_generic_bounds(None)?;
323-
*impl_dyn_multi = bounds.len() > 1 || self.prev_token_kind == PrevTokenKind::Plus;
323+
*impl_dyn_multi = bounds.len() > 1 || self.prev_token.kind == TokenKind::BinOp(token::Plus);
324324
Ok(TyKind::ImplTrait(ast::DUMMY_NODE_ID, bounds))
325325
}
326326

@@ -340,7 +340,7 @@ impl<'a> Parser<'a> {
340340
self.bump(); // `dyn`
341341
// Always parse bounds greedily for better error recovery.
342342
let bounds = self.parse_generic_bounds(None)?;
343-
*impl_dyn_multi = bounds.len() > 1 || self.prev_token_kind == PrevTokenKind::Plus;
343+
*impl_dyn_multi = bounds.len() > 1 || self.prev_token.kind == TokenKind::BinOp(token::Plus);
344344
Ok(TyKind::TraitObject(bounds, TraitObjectSyntax::Dyn))
345345
}
346346

0 commit comments

Comments
 (0)