Deduplicate token stream respanner

This commit is contained in:
David Tolnay 2021-01-24 23:01:50 -08:00
parent 033114a4ae
commit 1f423580a5
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
3 changed files with 20 additions and 37 deletions

View File

@ -1,6 +1,7 @@
use internals::respan::respan;
use internals::symbol::*;
use internals::{ungroup, Ctxt};
use proc_macro2::{Group, Spacing, Span, TokenStream, TokenTree};
use proc_macro2::{Spacing, Span, TokenStream, TokenTree};
use quote::ToTokens;
use std::borrow::Cow;
use std::collections::BTreeSet;
@ -1949,20 +1950,5 @@ where
fn spanned_tokens(s: &syn::LitStr) -> parse::Result<TokenStream> {
let stream = syn::parse_str(&s.value())?;
Ok(respan_token_stream(stream, s.span()))
}
fn respan_token_stream(stream: TokenStream, span: Span) -> TokenStream {
stream
.into_iter()
.map(|token| respan_token_tree(token, span))
.collect()
}
fn respan_token_tree(mut token: TokenTree, span: Span) -> TokenTree {
if let TokenTree::Group(g) = &mut token {
*g = Group::new(g.delimiter(), respan_token_stream(g.stream(), span));
}
token.set_span(span);
token
Ok(respan(stream, s.span()))
}

View File

@ -1,5 +1,6 @@
use super::respan::respan;
use internals::respan::respan;
use proc_macro2::Span;
use quote::ToTokens;
use std::mem;
use syn::punctuated::Punctuated;
use syn::visit_mut::{self, VisitMut};
@ -20,7 +21,9 @@ struct ReplaceReceiver<'a>(&'a TypePath);
impl ReplaceReceiver<'_> {
fn self_ty(&self, span: Span) -> TypePath {
respan(self.0, span)
let tokens = self.0.to_token_stream();
let respanned = respan(tokens, span);
syn::parse2(respanned).unwrap()
}
fn self_to_qself(&self, qself: &mut Option<QSelf>, path: &mut Path) {

View File

@ -1,22 +1,16 @@
use proc_macro2::{Span, TokenStream};
use quote::ToTokens;
use syn::parse::Parse;
use proc_macro2::{Group, Span, TokenStream, TokenTree};
pub(crate) fn respan<T>(node: &T, span: Span) -> T
where
T: ToTokens + Parse,
{
let tokens = node.to_token_stream();
let respanned = respan_tokens(tokens, span);
syn::parse2(respanned).unwrap()
}
fn respan_tokens(tokens: TokenStream, span: Span) -> TokenStream {
tokens
pub(crate) fn respan(stream: TokenStream, span: Span) -> TokenStream {
stream
.into_iter()
.map(|mut token| {
token.set_span(span);
token
})
.map(|token| respan_token(token, span))
.collect()
}
fn respan_token(mut token: TokenTree, span: Span) -> TokenTree {
if let TokenTree::Group(g) = &mut token {
*g = Group::new(g.delimiter(), respan(g.stream(), span));
}
token.set_span(span);
token
}