Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
9 changes: 9 additions & 0 deletions src/test/ui/macros/macro-interpolation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,19 @@ macro_rules! qpath {
};
}

macro_rules! field {
($var:ident . $field:literal) => {
$var . $field
};
}

pub fn main() {
let _: qpath!(path, <str as ToOwned>::Owned);
let _: qpath!(ty, <str as ToOwned>::Owned);

let tuple = ('x',);
let _ = field!(tuple.0);

assert!(overly_complicated!(f, x, Option<usize>, { return Some(x); },
Some(8), Some(y), y) == 8)
}