Skip to content

Commit bb63d75

Browse files
authored
Rollup merge of #129065 - nnethercote:PartialEq-TokenKind, r=spastorino
Use `impl PartialEq<TokenKind> for Token` more. This lets us compare a `Token` with a `TokenKind`. It's used a lot, but can be used even more, avoiding the need for some `.kind` uses. r? `@spastorino`
2 parents 3075644 + 7923b20 commit bb63d75

File tree

16 files changed

+122
-129
lines changed

16 files changed

+122
-129
lines changed

compiler/rustc_builtin_macros/src/asm.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ pub fn parse_asm_args<'a>(
328328
/// Otherwise, the suggestion will be incorrect.
329329
fn err_duplicate_option(p: &Parser<'_>, symbol: Symbol, span: Span) {
330330
// Tool-only output
331-
let full_span = if p.token.kind == token::Comma { span.to(p.token.span) } else { span };
331+
let full_span = if p.token == token::Comma { span.to(p.token.span) } else { span };
332332
p.dcx().emit_err(errors::AsmOptAlreadyprovided { span, symbol, full_span });
333333
}
334334

@@ -338,7 +338,7 @@ fn err_duplicate_option(p: &Parser<'_>, symbol: Symbol, span: Span) {
338338
/// Otherwise, the suggestion will be incorrect.
339339
fn err_unsupported_option(p: &Parser<'_>, symbol: Symbol, span: Span) {
340340
// Tool-only output
341-
let full_span = if p.token.kind == token::Comma { span.to(p.token.span) } else { span };
341+
let full_span = if p.token == token::Comma { span.to(p.token.span) } else { span };
342342
p.dcx().emit_err(errors::GlobalAsmUnsupportedOption { span, symbol, full_span });
343343
}
344344

compiler/rustc_expand/src/mbe/macro_rules.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1154,7 +1154,7 @@ fn check_matcher_core<'tt>(
11541154
&& matches!(kind, NonterminalKind::Pat(PatParam { inferred: true }))
11551155
&& matches!(
11561156
next_token,
1157-
TokenTree::Token(token) if token.kind == BinOp(token::BinOpToken::Or)
1157+
TokenTree::Token(token) if *token == BinOp(token::BinOpToken::Or)
11581158
)
11591159
{
11601160
// It is suggestion to use pat_param, for example: $x:pat -> $x:pat_param.

compiler/rustc_lint/src/builtin.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1853,7 +1853,7 @@ impl KeywordIdents {
18531853
if !prev_dollar {
18541854
self.check_ident_token(cx, UnderMacro(true), ident);
18551855
}
1856-
} else if token.kind == TokenKind::Dollar {
1856+
} else if *token == TokenKind::Dollar {
18571857
prev_dollar = true;
18581858
continue;
18591859
}

compiler/rustc_parse/src/lexer/tokentrees.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ impl<'psess, 'src> TokenTreesReader<'psess, 'src> {
229229
} else {
230230
let this_spacing = if next_tok.is_punct() {
231231
Spacing::Joint
232-
} else if next_tok.kind == token::Eof {
232+
} else if next_tok == token::Eof {
233233
Spacing::Alone
234234
} else {
235235
Spacing::JointHidden

compiler/rustc_parse/src/parser/attr.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ impl<'a> Parser<'a> {
162162
}
163163
loop {
164164
// skip any other attributes, we want the item
165-
if snapshot.token.kind == token::Pound {
165+
if snapshot.token == token::Pound {
166166
if let Err(err) = snapshot.parse_attribute(InnerAttrPolicy::Permitted) {
167167
err.cancel();
168168
return Some(replacement_span);
@@ -343,7 +343,7 @@ impl<'a> Parser<'a> {
343343

344344
// Presumably, the majority of the time there will only be one attr.
345345
let mut expanded_attrs = Vec::with_capacity(1);
346-
while self.token.kind != token::Eof {
346+
while self.token != token::Eof {
347347
let lo = self.token.span;
348348
let item = self.parse_attr_item(ForceCollect::Yes)?;
349349
expanded_attrs.push((item, lo.to(self.prev_token.span)));
@@ -359,7 +359,7 @@ impl<'a> Parser<'a> {
359359
pub(crate) fn parse_meta_seq_top(&mut self) -> PResult<'a, ThinVec<ast::NestedMetaItem>> {
360360
// Presumably, the majority of the time there will only be one attr.
361361
let mut nmis = ThinVec::with_capacity(1);
362-
while self.token.kind != token::Eof {
362+
while self.token != token::Eof {
363363
nmis.push(self.parse_meta_item_inner()?);
364364
if !self.eat(&token::Comma) {
365365
break;

compiler/rustc_parse/src/parser/diagnostics.rs

+26-29
Original file line numberDiff line numberDiff line change
@@ -474,8 +474,8 @@ impl<'a> Parser<'a> {
474474
// If this isn't the case however, and the suggestion is a token the
475475
// content of which is the same as the found token's, we remove it as well.
476476
if !eq {
477-
if let TokenType::Token(kind) = &token {
478-
if kind == &self.token.kind {
477+
if let TokenType::Token(kind) = token {
478+
if self.token == *kind {
479479
return false;
480480
}
481481
}
@@ -506,7 +506,7 @@ impl<'a> Parser<'a> {
506506
} else if !sm.is_multiline(self.prev_token.span.until(self.token.span)) {
507507
// The current token is in the same line as the prior token, not recoverable.
508508
} else if [token::Comma, token::Colon].contains(&self.token.kind)
509-
&& self.prev_token.kind == token::CloseDelim(Delimiter::Parenthesis)
509+
&& self.prev_token == token::CloseDelim(Delimiter::Parenthesis)
510510
{
511511
// Likely typo: The current token is on a new line and is expected to be
512512
// `.`, `;`, `?`, or an operator after a close delimiter token.
@@ -518,7 +518,7 @@ impl<'a> Parser<'a> {
518518
// https://github.com/rust-lang/rust/issues/72253
519519
} else if self.look_ahead(1, |t| {
520520
t == &token::CloseDelim(Delimiter::Brace)
521-
|| t.can_begin_expr() && t.kind != token::Colon
521+
|| t.can_begin_expr() && *t != token::Colon
522522
}) && [token::Comma, token::Colon].contains(&self.token.kind)
523523
{
524524
// Likely typo: `,` → `;` or `:` → `;`. This is triggered if the current token is
@@ -562,7 +562,7 @@ impl<'a> Parser<'a> {
562562
}
563563
}
564564

565-
if self.token.kind == TokenKind::EqEq
565+
if self.token == TokenKind::EqEq
566566
&& self.prev_token.is_ident()
567567
&& expected.iter().any(|tok| matches!(tok, TokenType::Token(TokenKind::Eq)))
568568
{
@@ -655,9 +655,9 @@ impl<'a> Parser<'a> {
655655
// positive for a `cr#` that wasn't intended to start a c-string literal, but identifying
656656
// that in the parser requires unbounded lookahead, so we only add a hint to the existing
657657
// error rather than replacing it entirely.
658-
if ((self.prev_token.kind == TokenKind::Ident(sym::c, IdentIsRaw::No)
658+
if ((self.prev_token == TokenKind::Ident(sym::c, IdentIsRaw::No)
659659
&& matches!(&self.token.kind, TokenKind::Literal(token::Lit { kind: token::Str, .. })))
660-
|| (self.prev_token.kind == TokenKind::Ident(sym::cr, IdentIsRaw::No)
660+
|| (self.prev_token == TokenKind::Ident(sym::cr, IdentIsRaw::No)
661661
&& matches!(
662662
&self.token.kind,
663663
TokenKind::Literal(token::Lit { kind: token::Str, .. }) | token::Pound
@@ -673,7 +673,7 @@ impl<'a> Parser<'a> {
673673
// `pub` may be used for an item or `pub(crate)`
674674
if self.prev_token.is_ident_named(sym::public)
675675
&& (self.token.can_begin_item()
676-
|| self.token.kind == TokenKind::OpenDelim(Delimiter::Parenthesis))
676+
|| self.token == TokenKind::OpenDelim(Delimiter::Parenthesis))
677677
{
678678
err.span_suggestion_short(
679679
self.prev_token.span,
@@ -772,7 +772,7 @@ impl<'a> Parser<'a> {
772772
),
773773
);
774774
if self.token == token::Pound
775-
&& self.look_ahead(1, |t| t.kind == token::OpenDelim(Delimiter::Bracket))
775+
&& self.look_ahead(1, |t| *t == token::OpenDelim(Delimiter::Bracket))
776776
{
777777
// We have
778778
// #[attr]
@@ -867,7 +867,7 @@ impl<'a> Parser<'a> {
867867
let str_span = self.prev_token.span;
868868
let mut span = self.token.span;
869869
let mut count = 0;
870-
while self.token.kind == TokenKind::Pound
870+
while self.token == TokenKind::Pound
871871
&& !sm.is_multiline(span.shrink_to_hi().until(self.token.span.shrink_to_lo()))
872872
{
873873
span = span.with_hi(self.token.span.hi());
@@ -1167,7 +1167,7 @@ impl<'a> Parser<'a> {
11671167
return;
11681168
}
11691169

1170-
if token::PathSep == self.token.kind && segment.args.is_none() {
1170+
if self.token == token::PathSep && segment.args.is_none() {
11711171
let snapshot = self.create_snapshot_for_diagnostic();
11721172
self.bump();
11731173
let lo = self.token.span;
@@ -1176,13 +1176,11 @@ impl<'a> Parser<'a> {
11761176
let span = lo.to(self.prev_token.span);
11771177
// Detect trailing `>` like in `x.collect::Vec<_>>()`.
11781178
let mut trailing_span = self.prev_token.span.shrink_to_hi();
1179-
while self.token.kind == token::BinOp(token::Shr)
1180-
|| self.token.kind == token::Gt
1181-
{
1179+
while self.token == token::BinOp(token::Shr) || self.token == token::Gt {
11821180
trailing_span = trailing_span.to(self.token.span);
11831181
self.bump();
11841182
}
1185-
if self.token.kind == token::OpenDelim(Delimiter::Parenthesis) {
1183+
if self.token == token::OpenDelim(Delimiter::Parenthesis) {
11861184
// Recover from bad turbofish: `foo.collect::Vec<_>()`.
11871185
segment.args = Some(AngleBracketedArgs { args, span }.into());
11881186

@@ -1430,7 +1428,7 @@ impl<'a> Parser<'a> {
14301428
self.restore_snapshot(snapshot);
14311429
}
14321430
}
1433-
return if token::PathSep == self.token.kind {
1431+
return if self.token == token::PathSep {
14341432
// We have some certainty that this was a bad turbofish at this point.
14351433
// `foo< bar >::`
14361434
if let ExprKind::Binary(o, ..) = inner_op.kind
@@ -1462,7 +1460,7 @@ impl<'a> Parser<'a> {
14621460
Err(self.dcx().create_err(err))
14631461
}
14641462
}
1465-
} else if token::OpenDelim(Delimiter::Parenthesis) == self.token.kind {
1463+
} else if self.token == token::OpenDelim(Delimiter::Parenthesis) {
14661464
// We have high certainty that this was a bad turbofish at this point.
14671465
// `foo< bar >(`
14681466
if let ExprKind::Binary(o, ..) = inner_op.kind
@@ -1528,7 +1526,7 @@ impl<'a> Parser<'a> {
15281526
];
15291527
self.consume_tts(1, &modifiers);
15301528

1531-
if self.token.kind == token::Eof {
1529+
if self.token == token::Eof {
15321530
// Not entirely sure that what we consumed were fn arguments, rollback.
15331531
self.restore_snapshot(snapshot);
15341532
Err(())
@@ -1811,7 +1809,7 @@ impl<'a> Parser<'a> {
18111809
/// This function gets called in places where a semicolon is NOT expected and if there's a
18121810
/// semicolon it emits the appropriate error and returns true.
18131811
pub fn maybe_consume_incorrect_semicolon(&mut self, previous_item: Option<&Item>) -> bool {
1814-
if self.token.kind != TokenKind::Semi {
1812+
if self.token != TokenKind::Semi {
18151813
return false;
18161814
}
18171815

@@ -2405,10 +2403,10 @@ impl<'a> Parser<'a> {
24052403
modifier: &[(token::TokenKind, i64)],
24062404
) {
24072405
while acc > 0 {
2408-
if let Some((_, val)) = modifier.iter().find(|(t, _)| *t == self.token.kind) {
2406+
if let Some((_, val)) = modifier.iter().find(|(t, _)| self.token == *t) {
24092407
acc += *val;
24102408
}
2411-
if self.token.kind == token::Eof {
2409+
if self.token == token::Eof {
24122410
break;
24132411
}
24142412
self.bump();
@@ -2598,7 +2596,7 @@ impl<'a> Parser<'a> {
25982596
}
25992597
})
26002598
.is_some()
2601-
|| self.token.kind == TokenKind::Dot;
2599+
|| self.token == TokenKind::Dot;
26022600
// This will be true when a trait object type `Foo +` or a path which was a `const fn` with
26032601
// type params has been parsed.
26042602
let was_op =
@@ -2617,7 +2615,7 @@ impl<'a> Parser<'a> {
26172615
})() {
26182616
Ok(expr) => {
26192617
// Find a mistake like `MyTrait<Assoc == S::Assoc>`.
2620-
if token::EqEq == snapshot.token.kind {
2618+
if snapshot.token == token::EqEq {
26212619
err.span_suggestion(
26222620
snapshot.token.span,
26232621
"if you meant to use an associated type binding, replace `==` with `=`",
@@ -2627,7 +2625,7 @@ impl<'a> Parser<'a> {
26272625
let guar = err.emit();
26282626
let value = self.mk_expr_err(start.to(expr.span), guar);
26292627
return Ok(GenericArg::Const(AnonConst { id: ast::DUMMY_NODE_ID, value }));
2630-
} else if token::Colon == snapshot.token.kind
2628+
} else if snapshot.token == token::Colon
26312629
&& expr.span.lo() == snapshot.token.span.hi()
26322630
&& matches!(expr.kind, ExprKind::Path(..))
26332631
{
@@ -2642,8 +2640,7 @@ impl<'a> Parser<'a> {
26422640
return Ok(GenericArg::Type(
26432641
self.mk_ty(start.to(expr.span), TyKind::Err(guar)),
26442642
));
2645-
} else if token::Comma == self.token.kind || self.token.kind.should_end_const_arg()
2646-
{
2643+
} else if self.token == token::Comma || self.token.kind.should_end_const_arg() {
26472644
// Avoid the following output by checking that we consumed a full const arg:
26482645
// help: expressions must be enclosed in braces to be used as const generic
26492646
// arguments
@@ -2846,8 +2843,8 @@ impl<'a> Parser<'a> {
28462843
pub(crate) fn maybe_recover_unexpected_block_label(&mut self) -> bool {
28472844
// Check for `'a : {`
28482845
if !(self.check_lifetime()
2849-
&& self.look_ahead(1, |tok| tok.kind == token::Colon)
2850-
&& self.look_ahead(2, |tok| tok.kind == token::OpenDelim(Delimiter::Brace)))
2846+
&& self.look_ahead(1, |t| *t == token::Colon)
2847+
&& self.look_ahead(2, |t| *t == token::OpenDelim(Delimiter::Brace)))
28512848
{
28522849
return false;
28532850
}
@@ -3001,7 +2998,7 @@ impl<'a> Parser<'a> {
30012998
// >>>>>>>
30022999
let mut end = None;
30033000
loop {
3004-
if self.token.kind == TokenKind::Eof {
3001+
if self.token == TokenKind::Eof {
30053002
break;
30063003
}
30073004
if let Some(span) = self.conflict_marker(&TokenKind::OrOr, &TokenKind::BinOp(token::Or))

0 commit comments

Comments
 (0)