Skip to content

Commit

Permalink
Uninterpolate literal to use as field index
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Jan 19, 2022
1 parent e2ba9d1 commit aadf994
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions compiler/rustc_ast/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -452,8 +452,9 @@ impl Token {
}

// A convenience function for matching on identifiers during parsing.
// Turns interpolated identifier (`$i: ident`) or lifetime (`$l: lifetime`) token
// into the regular identifier or lifetime token it refers to,
// Turns interpolated identifier ($i:ident), lifetime ($l:lifetime), and
// integer literal ($l:literal) into the regular identifier or lifetime or
// literal token it refers to,
// otherwise returns the original token.
pub fn uninterpolate(&self) -> Cow<'_, Token> {
match &self.kind {
Expand All @@ -462,6 +463,11 @@ impl Token {
Cow::Owned(Token::new(Ident(ident.name, is_raw), ident.span))
}
NtLifetime(ident) => Cow::Owned(Token::new(Lifetime(ident.name), ident.span)),
NtLiteral(ref literal)
if let ast::ExprKind::Lit(ast::Lit { token, kind: ast::LitKind::Int(..), .. }) = literal.kind =>
{
Cow::Owned(Token::new(Literal(token), literal.span))
}
_ => Cow::Borrowed(self),
},
_ => Cow::Borrowed(self),
Expand Down

0 comments on commit aadf994

Please sign in to comment.