Merge pull request #199 from dtolnay/respan

Recursively respan tokens interpolated from a macro_rules metavariable
This commit is contained in:
David Tolnay 2021-12-26 22:48:08 -08:00 committed by GitHub
commit d74545e82b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -184,10 +184,24 @@ pub fn parse(tokens: &mut TokenStream, s: &str) {
pub fn parse_spanned(tokens: &mut TokenStream, span: Span, s: &str) {
let s: TokenStream = s.parse().expect("invalid token stream");
tokens.extend(s.into_iter().map(|mut t| {
t.set_span(span);
t
}));
tokens.extend(s.into_iter().map(|t| respan_token_tree(t, span)));
}
// Token tree with every span replaced by the given one.
fn respan_token_tree(mut token: TokenTree, span: Span) -> TokenTree {
match &mut token {
TokenTree::Group(g) => {
let stream = g
.stream()
.into_iter()
.map(|token| respan_token_tree(token, span))
.collect();
*g = Group::new(g.delimiter(), stream);
g.set_span(span);
}
other => other.set_span(span),
}
token
}
pub fn push_ident(tokens: &mut TokenStream, s: &str) {