Skip to content

Commit 36f2816

Browse files
committed
proc_macro: Use an item's tokens if available
This partly resolves the `FIXME` located in `src/libproc_macro/lib.rs` when interpreting interpolated tokens. All instances of `ast::Item` which have a list of tokens attached to them now use that list of tokens to losslessly get converted into a `TokenTree` instead of going through stringification and losing span information. cc #43081
1 parent 9b2f762 commit 36f2816

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

src/libproc_macro/lib.rs

+20-8
Original file line numberDiff line numberDiff line change
@@ -509,14 +509,26 @@ impl TokenTree {
509509
Ident(ident) | Lifetime(ident) => TokenNode::Term(Term(ident.name)),
510510
Literal(..) | DocComment(..) => TokenNode::Literal(self::Literal(token)),
511511

512-
Interpolated(ref nt) => __internal::with_sess(|(sess, _)| {
513-
TokenNode::Group(Delimiter::None, TokenStream(nt.1.force(|| {
514-
// FIXME(jseyfried): Avoid this pretty-print + reparse hack
515-
let name = "<macro expansion>".to_owned();
516-
let source = pprust::token_to_string(&token);
517-
parse_stream_from_source_str(name, source, sess, Some(span))
518-
})))
519-
}),
512+
Interpolated(ref nt) => {
513+
let mut node = None;
514+
if let Nonterminal::NtItem(ref item) = nt.0 {
515+
if let Some(ref tokens) = item.tokens {
516+
node = Some(TokenNode::Group(Delimiter::None,
517+
TokenStream(tokens.clone())));
518+
}
519+
}
520+
521+
node.unwrap_or_else(|| {
522+
__internal::with_sess(|(sess, _)| {
523+
TokenNode::Group(Delimiter::None, TokenStream(nt.1.force(|| {
524+
// FIXME(jseyfried): Avoid this pretty-print + reparse hack
525+
let name = "<macro expansion>".to_owned();
526+
let source = pprust::token_to_string(&token);
527+
parse_stream_from_source_str(name, source, sess, Some(span))
528+
})))
529+
})
530+
})
531+
}
520532

521533
OpenDelim(..) | CloseDelim(..) => unreachable!(),
522534
Whitespace | Comment | Shebang(..) | Eof => unreachable!(),

0 commit comments

Comments
 (0)