diff --git a/compiler/rustc_ast/src/token.rs b/compiler/rustc_ast/src/token.rs index db066d7c6a519..1ca7281619edc 100644 --- a/compiler/rustc_ast/src/token.rs +++ b/compiler/rustc_ast/src/token.rs @@ -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 { @@ -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), diff --git a/src/test/ui/macros/macro-interpolation.rs b/src/test/ui/macros/macro-interpolation.rs index 35003a79ad703..d6cc682797647 100644 --- a/src/test/ui/macros/macro-interpolation.rs +++ b/src/test/ui/macros/macro-interpolation.rs @@ -24,10 +24,19 @@ macro_rules! qpath { }; } +macro_rules! field { + ($var:ident . $field:literal) => { + $var . $field + }; +} + pub fn main() { let _: qpath!(path, ::Owned); let _: qpath!(ty, ::Owned); + let tuple = ('x',); + let _ = field!(tuple.0); + assert!(overly_complicated!(f, x, Option, { return Some(x); }, Some(8), Some(y), y) == 8) }