Remove the lifetime from ExpTokenPair/SeqSep.

`TokenKind` now impls `Copy`, so we can store it directly rather than a
reference.
This commit is contained in:
Nicholas Nethercote
2025-08-25 07:29:54 +10:00
parent 3776358beb
commit a06c3887bd
5 changed files with 49 additions and 49 deletions

View File

@@ -261,19 +261,19 @@ struct CaptureState {
/// A sequence separator.
#[derive(Debug)]
struct SeqSep<'a> {
struct SeqSep {
/// The separator token.
sep: Option<ExpTokenPair<'a>>,
sep: Option<ExpTokenPair>,
/// `true` if a trailing separator is allowed.
trailing_sep_allowed: bool,
}
impl<'a> SeqSep<'a> {
fn trailing_allowed(sep: ExpTokenPair<'a>) -> SeqSep<'a> {
impl SeqSep {
fn trailing_allowed(sep: ExpTokenPair) -> SeqSep {
SeqSep { sep: Some(sep), trailing_sep_allowed: true }
}
fn none() -> SeqSep<'a> {
fn none() -> SeqSep {
SeqSep { sep: None, trailing_sep_allowed: false }
}
}
@@ -425,13 +425,13 @@ 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, exp: ExpTokenPair<'_>) -> PResult<'a, Recovered> {
pub fn expect(&mut self, exp: ExpTokenPair) -> PResult<'a, Recovered> {
if self.expected_token_types.is_empty() {
if self.token == *exp.tok {
if self.token == exp.tok {
self.bump();
Ok(Recovered::No)
} else {
self.unexpected_try_recover(exp.tok)
self.unexpected_try_recover(&exp.tok)
}
} else {
self.expect_one_of(slice::from_ref(&exp), &[])
@@ -443,13 +443,13 @@ impl<'a> Parser<'a> {
/// anything. Signal a fatal error if next token is unexpected.
fn expect_one_of(
&mut self,
edible: &[ExpTokenPair<'_>],
inedible: &[ExpTokenPair<'_>],
edible: &[ExpTokenPair],
inedible: &[ExpTokenPair],
) -> PResult<'a, Recovered> {
if edible.iter().any(|exp| exp.tok == &self.token.kind) {
if edible.iter().any(|exp| exp.tok == self.token.kind) {
self.bump();
Ok(Recovered::No)
} else if inedible.iter().any(|exp| exp.tok == &self.token.kind) {
} else if inedible.iter().any(|exp| exp.tok == self.token.kind) {
// leave it in the input
Ok(Recovered::No)
} else if self.token != token::Eof
@@ -494,8 +494,8 @@ impl<'a> Parser<'a> {
/// This method will automatically add `tok` to `expected_token_types` if `tok` is not
/// encountered.
#[inline]
pub fn check(&mut self, exp: ExpTokenPair<'_>) -> bool {
let is_present = self.token == *exp.tok;
pub fn check(&mut self, exp: ExpTokenPair) -> bool {
let is_present = self.token == exp.tok;
if !is_present {
self.expected_token_types.insert(exp.token_type);
}
@@ -542,7 +542,7 @@ impl<'a> Parser<'a> {
/// Consumes a token 'tok' if it exists. Returns whether the given token was present.
#[inline]
#[must_use]
pub fn eat(&mut self, exp: ExpTokenPair<'_>) -> bool {
pub fn eat(&mut self, exp: ExpTokenPair) -> bool {
let is_present = self.check(exp);
if is_present {
self.bump()
@@ -745,13 +745,13 @@ impl<'a> Parser<'a> {
/// Eats the expected token if it's present possibly breaking
/// compound tokens like multi-character operators in process.
/// Returns `true` if the token was eaten.
fn break_and_eat(&mut self, exp: ExpTokenPair<'_>) -> bool {
if self.token == *exp.tok {
fn break_and_eat(&mut self, exp: ExpTokenPair) -> bool {
if self.token == exp.tok {
self.bump();
return true;
}
match self.token.kind.break_two_token_op(1) {
Some((first, second)) if first == *exp.tok => {
Some((first, second)) if first == exp.tok => {
let first_span = self.psess.source_map().start_point(self.token.span);
let second_span = self.token.span.with_lo(first_span.hi());
self.token = Token::new(first, first_span);
@@ -826,7 +826,7 @@ impl<'a> Parser<'a> {
/// Checks if the next token is contained within `closes`, and returns `true` if so.
fn expect_any_with_type(
&mut self,
closes_expected: &[ExpTokenPair<'_>],
closes_expected: &[ExpTokenPair],
closes_not_expected: &[&TokenKind],
) -> bool {
closes_expected.iter().any(|&close| self.check(close))
@@ -838,9 +838,9 @@ impl<'a> Parser<'a> {
/// closing bracket.
fn parse_seq_to_before_tokens<T>(
&mut self,
closes_expected: &[ExpTokenPair<'_>],
closes_expected: &[ExpTokenPair],
closes_not_expected: &[&TokenKind],
sep: SeqSep<'_>,
sep: SeqSep,
mut f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
) -> PResult<'a, (ThinVec<T>, Trailing, Recovered)> {
let mut first = true;
@@ -869,7 +869,7 @@ impl<'a> Parser<'a> {
}
Err(mut expect_err) => {
let sp = self.prev_token.span.shrink_to_hi();
let token_str = pprust::token_kind_to_string(exp.tok);
let token_str = pprust::token_kind_to_string(&exp.tok);
match self.current_closure.take() {
Some(closure_spans) if self.token == TokenKind::Semi => {
@@ -1039,8 +1039,8 @@ impl<'a> Parser<'a> {
/// closing bracket.
fn parse_seq_to_before_end<T>(
&mut self,
close: ExpTokenPair<'_>,
sep: SeqSep<'_>,
close: ExpTokenPair,
sep: SeqSep,
f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
) -> PResult<'a, (ThinVec<T>, Trailing, Recovered)> {
self.parse_seq_to_before_tokens(&[close], &[], sep, f)
@@ -1051,8 +1051,8 @@ impl<'a> Parser<'a> {
/// closing bracket.
fn parse_seq_to_end<T>(
&mut self,
close: ExpTokenPair<'_>,
sep: SeqSep<'_>,
close: ExpTokenPair,
sep: SeqSep,
f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
) -> PResult<'a, (ThinVec<T>, Trailing)> {
let (val, trailing, recovered) = self.parse_seq_to_before_end(close, sep, f)?;
@@ -1070,9 +1070,9 @@ impl<'a> Parser<'a> {
/// closing bracket.
fn parse_unspanned_seq<T>(
&mut self,
open: ExpTokenPair<'_>,
close: ExpTokenPair<'_>,
sep: SeqSep<'_>,
open: ExpTokenPair,
close: ExpTokenPair,
sep: SeqSep,
f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
) -> PResult<'a, (ThinVec<T>, Trailing)> {
self.expect(open)?;
@@ -1084,8 +1084,8 @@ impl<'a> Parser<'a> {
/// closing bracket.
fn parse_delim_comma_seq<T>(
&mut self,
open: ExpTokenPair<'_>,
close: ExpTokenPair<'_>,
open: ExpTokenPair,
close: ExpTokenPair,
f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
) -> PResult<'a, (ThinVec<T>, Trailing)> {
self.parse_unspanned_seq(open, close, SeqSep::trailing_allowed(exp!(Comma)), f)