Skip to content

Commit

Permalink
Unrolled build for rust-lang#129065
Browse files Browse the repository at this point in the history
Rollup merge of rust-lang#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`
  • Loading branch information
rust-timer authored Aug 15, 2024
2 parents d2b5aa6 + 7923b20 commit 71f462d
Show file tree
Hide file tree
Showing 16 changed files with 122 additions and 129 deletions.
4 changes: 2 additions & 2 deletions compiler/rustc_builtin_macros/src/asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ pub fn parse_asm_args<'a>(
/// Otherwise, the suggestion will be incorrect.
fn err_duplicate_option(p: &Parser<'_>, symbol: Symbol, span: Span) {
// Tool-only output
let full_span = if p.token.kind == token::Comma { span.to(p.token.span) } else { span };
let full_span = if p.token == token::Comma { span.to(p.token.span) } else { span };
p.dcx().emit_err(errors::AsmOptAlreadyprovided { span, symbol, full_span });
}

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

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_expand/src/mbe/macro_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1154,7 +1154,7 @@ fn check_matcher_core<'tt>(
&& matches!(kind, NonterminalKind::Pat(PatParam { inferred: true }))
&& matches!(
next_token,
TokenTree::Token(token) if token.kind == BinOp(token::BinOpToken::Or)
TokenTree::Token(token) if *token == BinOp(token::BinOpToken::Or)
)
{
// It is suggestion to use pat_param, for example: $x:pat -> $x:pat_param.
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_lint/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1853,7 +1853,7 @@ impl KeywordIdents {
if !prev_dollar {
self.check_ident_token(cx, UnderMacro(true), ident);
}
} else if token.kind == TokenKind::Dollar {
} else if *token == TokenKind::Dollar {
prev_dollar = true;
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_parse/src/lexer/tokentrees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ impl<'psess, 'src> TokenTreesReader<'psess, 'src> {
} else {
let this_spacing = if next_tok.is_punct() {
Spacing::Joint
} else if next_tok.kind == token::Eof {
} else if next_tok == token::Eof {
Spacing::Alone
} else {
Spacing::JointHidden
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_parse/src/parser/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ impl<'a> Parser<'a> {
}
loop {
// skip any other attributes, we want the item
if snapshot.token.kind == token::Pound {
if snapshot.token == token::Pound {
if let Err(err) = snapshot.parse_attribute(InnerAttrPolicy::Permitted) {
err.cancel();
return Some(replacement_span);
Expand Down Expand Up @@ -343,7 +343,7 @@ impl<'a> Parser<'a> {

// Presumably, the majority of the time there will only be one attr.
let mut expanded_attrs = Vec::with_capacity(1);
while self.token.kind != token::Eof {
while self.token != token::Eof {
let lo = self.token.span;
let item = self.parse_attr_item(ForceCollect::Yes)?;
expanded_attrs.push((item, lo.to(self.prev_token.span)));
Expand All @@ -359,7 +359,7 @@ impl<'a> Parser<'a> {
pub(crate) fn parse_meta_seq_top(&mut self) -> PResult<'a, ThinVec<ast::NestedMetaItem>> {
// Presumably, the majority of the time there will only be one attr.
let mut nmis = ThinVec::with_capacity(1);
while self.token.kind != token::Eof {
while self.token != token::Eof {
nmis.push(self.parse_meta_item_inner()?);
if !self.eat(&token::Comma) {
break;
Expand Down
55 changes: 26 additions & 29 deletions compiler/rustc_parse/src/parser/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,8 +474,8 @@ impl<'a> Parser<'a> {
// If this isn't the case however, and the suggestion is a token the
// content of which is the same as the found token's, we remove it as well.
if !eq {
if let TokenType::Token(kind) = &token {
if kind == &self.token.kind {
if let TokenType::Token(kind) = token {
if self.token == *kind {
return false;
}
}
Expand Down Expand Up @@ -506,7 +506,7 @@ impl<'a> Parser<'a> {
} else if !sm.is_multiline(self.prev_token.span.until(self.token.span)) {
// The current token is in the same line as the prior token, not recoverable.
} else if [token::Comma, token::Colon].contains(&self.token.kind)
&& self.prev_token.kind == token::CloseDelim(Delimiter::Parenthesis)
&& self.prev_token == token::CloseDelim(Delimiter::Parenthesis)
{
// Likely typo: The current token is on a new line and is expected to be
// `.`, `;`, `?`, or an operator after a close delimiter token.
Expand All @@ -518,7 +518,7 @@ impl<'a> Parser<'a> {
// https://github.com/rust-lang/rust/issues/72253
} else if self.look_ahead(1, |t| {
t == &token::CloseDelim(Delimiter::Brace)
|| t.can_begin_expr() && t.kind != token::Colon
|| t.can_begin_expr() && *t != token::Colon
}) && [token::Comma, token::Colon].contains(&self.token.kind)
{
// Likely typo: `,` → `;` or `:` → `;`. This is triggered if the current token is
Expand Down Expand Up @@ -562,7 +562,7 @@ impl<'a> Parser<'a> {
}
}

if self.token.kind == TokenKind::EqEq
if self.token == TokenKind::EqEq
&& self.prev_token.is_ident()
&& expected.iter().any(|tok| matches!(tok, TokenType::Token(TokenKind::Eq)))
{
Expand Down Expand Up @@ -655,9 +655,9 @@ impl<'a> Parser<'a> {
// positive for a `cr#` that wasn't intended to start a c-string literal, but identifying
// that in the parser requires unbounded lookahead, so we only add a hint to the existing
// error rather than replacing it entirely.
if ((self.prev_token.kind == TokenKind::Ident(sym::c, IdentIsRaw::No)
if ((self.prev_token == TokenKind::Ident(sym::c, IdentIsRaw::No)
&& matches!(&self.token.kind, TokenKind::Literal(token::Lit { kind: token::Str, .. })))
|| (self.prev_token.kind == TokenKind::Ident(sym::cr, IdentIsRaw::No)
|| (self.prev_token == TokenKind::Ident(sym::cr, IdentIsRaw::No)
&& matches!(
&self.token.kind,
TokenKind::Literal(token::Lit { kind: token::Str, .. }) | token::Pound
Expand All @@ -673,7 +673,7 @@ impl<'a> Parser<'a> {
// `pub` may be used for an item or `pub(crate)`
if self.prev_token.is_ident_named(sym::public)
&& (self.token.can_begin_item()
|| self.token.kind == TokenKind::OpenDelim(Delimiter::Parenthesis))
|| self.token == TokenKind::OpenDelim(Delimiter::Parenthesis))
{
err.span_suggestion_short(
self.prev_token.span,
Expand Down Expand Up @@ -772,7 +772,7 @@ impl<'a> Parser<'a> {
),
);
if self.token == token::Pound
&& self.look_ahead(1, |t| t.kind == token::OpenDelim(Delimiter::Bracket))
&& self.look_ahead(1, |t| *t == token::OpenDelim(Delimiter::Bracket))
{
// We have
// #[attr]
Expand Down Expand Up @@ -867,7 +867,7 @@ impl<'a> Parser<'a> {
let str_span = self.prev_token.span;
let mut span = self.token.span;
let mut count = 0;
while self.token.kind == TokenKind::Pound
while self.token == TokenKind::Pound
&& !sm.is_multiline(span.shrink_to_hi().until(self.token.span.shrink_to_lo()))
{
span = span.with_hi(self.token.span.hi());
Expand Down Expand Up @@ -1167,7 +1167,7 @@ impl<'a> Parser<'a> {
return;
}

if token::PathSep == self.token.kind && segment.args.is_none() {
if self.token == token::PathSep && segment.args.is_none() {
let snapshot = self.create_snapshot_for_diagnostic();
self.bump();
let lo = self.token.span;
Expand All @@ -1176,13 +1176,11 @@ impl<'a> Parser<'a> {
let span = lo.to(self.prev_token.span);
// Detect trailing `>` like in `x.collect::Vec<_>>()`.
let mut trailing_span = self.prev_token.span.shrink_to_hi();
while self.token.kind == token::BinOp(token::Shr)
|| self.token.kind == token::Gt
{
while self.token == token::BinOp(token::Shr) || self.token == token::Gt {
trailing_span = trailing_span.to(self.token.span);
self.bump();
}
if self.token.kind == token::OpenDelim(Delimiter::Parenthesis) {
if self.token == token::OpenDelim(Delimiter::Parenthesis) {
// Recover from bad turbofish: `foo.collect::Vec<_>()`.
segment.args = Some(AngleBracketedArgs { args, span }.into());

Expand Down Expand Up @@ -1430,7 +1428,7 @@ impl<'a> Parser<'a> {
self.restore_snapshot(snapshot);
}
}
return if token::PathSep == self.token.kind {
return if self.token == token::PathSep {
// We have some certainty that this was a bad turbofish at this point.
// `foo< bar >::`
if let ExprKind::Binary(o, ..) = inner_op.kind
Expand Down Expand Up @@ -1462,7 +1460,7 @@ impl<'a> Parser<'a> {
Err(self.dcx().create_err(err))
}
}
} else if token::OpenDelim(Delimiter::Parenthesis) == self.token.kind {
} else if self.token == token::OpenDelim(Delimiter::Parenthesis) {
// We have high certainty that this was a bad turbofish at this point.
// `foo< bar >(`
if let ExprKind::Binary(o, ..) = inner_op.kind
Expand Down Expand Up @@ -1528,7 +1526,7 @@ impl<'a> Parser<'a> {
];
self.consume_tts(1, &modifiers);

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

Expand Down Expand Up @@ -2405,10 +2403,10 @@ impl<'a> Parser<'a> {
modifier: &[(token::TokenKind, i64)],
) {
while acc > 0 {
if let Some((_, val)) = modifier.iter().find(|(t, _)| *t == self.token.kind) {
if let Some((_, val)) = modifier.iter().find(|(t, _)| self.token == *t) {
acc += *val;
}
if self.token.kind == token::Eof {
if self.token == token::Eof {
break;
}
self.bump();
Expand Down Expand Up @@ -2598,7 +2596,7 @@ impl<'a> Parser<'a> {
}
})
.is_some()
|| self.token.kind == TokenKind::Dot;
|| self.token == TokenKind::Dot;
// This will be true when a trait object type `Foo +` or a path which was a `const fn` with
// type params has been parsed.
let was_op =
Expand All @@ -2617,7 +2615,7 @@ impl<'a> Parser<'a> {
})() {
Ok(expr) => {
// Find a mistake like `MyTrait<Assoc == S::Assoc>`.
if token::EqEq == snapshot.token.kind {
if snapshot.token == token::EqEq {
err.span_suggestion(
snapshot.token.span,
"if you meant to use an associated type binding, replace `==` with `=`",
Expand All @@ -2627,7 +2625,7 @@ impl<'a> Parser<'a> {
let guar = err.emit();
let value = self.mk_expr_err(start.to(expr.span), guar);
return Ok(GenericArg::Const(AnonConst { id: ast::DUMMY_NODE_ID, value }));
} else if token::Colon == snapshot.token.kind
} else if snapshot.token == token::Colon
&& expr.span.lo() == snapshot.token.span.hi()
&& matches!(expr.kind, ExprKind::Path(..))
{
Expand All @@ -2642,8 +2640,7 @@ impl<'a> Parser<'a> {
return Ok(GenericArg::Type(
self.mk_ty(start.to(expr.span), TyKind::Err(guar)),
));
} else if token::Comma == self.token.kind || self.token.kind.should_end_const_arg()
{
} else if self.token == token::Comma || self.token.kind.should_end_const_arg() {
// Avoid the following output by checking that we consumed a full const arg:
// help: expressions must be enclosed in braces to be used as const generic
// arguments
Expand Down Expand Up @@ -2846,8 +2843,8 @@ impl<'a> Parser<'a> {
pub(crate) fn maybe_recover_unexpected_block_label(&mut self) -> bool {
// Check for `'a : {`
if !(self.check_lifetime()
&& self.look_ahead(1, |tok| tok.kind == token::Colon)
&& self.look_ahead(2, |tok| tok.kind == token::OpenDelim(Delimiter::Brace)))
&& self.look_ahead(1, |t| *t == token::Colon)
&& self.look_ahead(2, |t| *t == token::OpenDelim(Delimiter::Brace)))
{
return false;
}
Expand Down Expand Up @@ -3001,7 +2998,7 @@ impl<'a> Parser<'a> {
// >>>>>>>
let mut end = None;
loop {
if self.token.kind == TokenKind::Eof {
if self.token == TokenKind::Eof {
break;
}
if let Some(span) = self.conflict_marker(&TokenKind::OrOr, &TokenKind::BinOp(token::Or))
Expand Down
Loading

0 comments on commit 71f462d

Please sign in to comment.