Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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
  • Loading branch information
alexcrichton committed Jul 28, 2017
1 parent 9b2f762 commit 36f2816
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions src/libproc_macro/lib.rs
Expand Up @@ -509,14 +509,26 @@ impl TokenTree {
Ident(ident) | Lifetime(ident) => TokenNode::Term(Term(ident.name)),
Literal(..) | DocComment(..) => TokenNode::Literal(self::Literal(token)),

Interpolated(ref nt) => __internal::with_sess(|(sess, _)| {
TokenNode::Group(Delimiter::None, TokenStream(nt.1.force(|| {
// FIXME(jseyfried): Avoid this pretty-print + reparse hack
let name = "<macro expansion>".to_owned();
let source = pprust::token_to_string(&token);
parse_stream_from_source_str(name, source, sess, Some(span))
})))
}),
Interpolated(ref nt) => {
let mut node = None;
if let Nonterminal::NtItem(ref item) = nt.0 {
if let Some(ref tokens) = item.tokens {
node = Some(TokenNode::Group(Delimiter::None,
TokenStream(tokens.clone())));
}
}

node.unwrap_or_else(|| {
__internal::with_sess(|(sess, _)| {
TokenNode::Group(Delimiter::None, TokenStream(nt.1.force(|| {
// FIXME(jseyfried): Avoid this pretty-print + reparse hack
let name = "<macro expansion>".to_owned();
let source = pprust::token_to_string(&token);
parse_stream_from_source_str(name, source, sess, Some(span))
})))
})
})
}

OpenDelim(..) | CloseDelim(..) => unreachable!(),
Whitespace | Comment | Shebang(..) | Eof => unreachable!(),
Expand Down

0 comments on commit 36f2816

Please sign in to comment.