1
Fork 0

proc_macro: stop using a remote object handle for Group

This greatly reduces round-trips to fetch relevant extra information about the
token in proc macro code, and avoids RPC messages to create Group tokens.
This commit is contained in:
Nika Layzell 2021-07-01 17:36:38 -04:00
parent 72bfe618fa
commit f28dfdf1c7
5 changed files with 204 additions and 249 deletions

View file

@ -175,7 +175,6 @@ define_handles! {
'owned:
FreeFunctions,
TokenStream,
Group,
Literal,
SourceFile,
MultiSpan,
@ -198,12 +197,6 @@ impl Clone for TokenStream {
}
}
impl Clone for Group {
fn clone(&self) -> Self {
self.clone()
}
}
impl Clone for Literal {
fn clone(&self) -> Self {
self.clone()

View file

@ -65,11 +65,11 @@ macro_rules! with_api {
fn from_str(src: &str) -> $S::TokenStream;
fn to_string($self: &$S::TokenStream) -> String;
fn from_token_tree(
tree: TokenTree<$S::Span, $S::Group, $S::Ident, $S::Literal>,
tree: TokenTree<$S::TokenStream, $S::Span, $S::Ident, $S::Literal>,
) -> $S::TokenStream;
fn concat_trees(
base: Option<$S::TokenStream>,
trees: Vec<TokenTree<$S::Span, $S::Group, $S::Ident, $S::Literal>>,
trees: Vec<TokenTree<$S::TokenStream, $S::Span, $S::Ident, $S::Literal>>,
) -> $S::TokenStream;
fn concat_streams(
base: Option<$S::TokenStream>,
@ -77,18 +77,7 @@ macro_rules! with_api {
) -> $S::TokenStream;
fn into_trees(
$self: $S::TokenStream
) -> Vec<TokenTree<$S::Span, $S::Group, $S::Ident, $S::Literal>>;
},
Group {
fn drop($self: $S::Group);
fn clone($self: &$S::Group) -> $S::Group;
fn new(delimiter: Delimiter, stream: Option<$S::TokenStream>) -> $S::Group;
fn delimiter($self: &$S::Group) -> Delimiter;
fn stream($self: &$S::Group) -> $S::TokenStream;
fn span($self: &$S::Group) -> $S::Span;
fn span_open($self: &$S::Group) -> $S::Span;
fn span_close($self: &$S::Group) -> $S::Span;
fn set_span($self: &mut $S::Group, span: $S::Span);
) -> Vec<TokenTree<$S::TokenStream, $S::Span, $S::Ident, $S::Literal>>;
},
Ident {
fn new(string: &str, span: $S::Span, is_raw: bool) -> $S::Ident;
@ -441,6 +430,30 @@ compound_traits!(
}
);
#[derive(Copy, Clone)]
pub struct DelimSpan<S> {
pub open: S,
pub close: S,
pub entire: S,
}
impl<S: Copy> DelimSpan<S> {
pub fn from_single(span: S) -> Self {
DelimSpan { open: span, close: span, entire: span }
}
}
compound_traits!(struct DelimSpan<Sp> { open, close, entire });
#[derive(Clone)]
pub struct Group<T, S> {
pub delimiter: Delimiter,
pub stream: Option<T>,
pub span: DelimSpan<S>,
}
compound_traits!(struct Group<T, Sp> { delimiter, stream, span });
#[derive(Clone)]
pub struct Punct<S> {
pub ch: char,
@ -451,15 +464,15 @@ pub struct Punct<S> {
compound_traits!(struct Punct<Sp> { ch, joint, span });
#[derive(Clone)]
pub enum TokenTree<S, G, I, L> {
Group(G),
pub enum TokenTree<T, S, I, L> {
Group(Group<T, S>),
Punct(Punct<S>),
Ident(I),
Literal(L),
}
compound_traits!(
enum TokenTree<Sp, G, I, L> {
enum TokenTree<T, Sp, I, L> {
Group(tt),
Punct(tt),
Ident(tt),

View file

@ -8,7 +8,6 @@ use super::client::HandleStore;
pub trait Types {
type FreeFunctions: 'static;
type TokenStream: 'static + Clone;
type Group: 'static + Clone;
type Ident: 'static + Copy + Eq + Hash;
type Literal: 'static + Clone;
type SourceFile: 'static + Clone;

View file

@ -212,8 +212,8 @@ pub use quote::{quote, quote_span};
fn tree_to_bridge_tree(
tree: TokenTree,
) -> bridge::TokenTree<
bridge::client::TokenStream,
bridge::client::Span,
bridge::client::Group,
bridge::client::Ident,
bridge::client::Literal,
> {
@ -238,8 +238,8 @@ impl From<TokenTree> for TokenStream {
struct ConcatTreesHelper {
trees: Vec<
bridge::TokenTree<
bridge::client::TokenStream,
bridge::client::Span,
bridge::client::Group,
bridge::client::Ident,
bridge::client::Literal,
>,
@ -365,8 +365,8 @@ pub mod token_stream {
pub struct IntoIter(
std::vec::IntoIter<
bridge::TokenTree<
bridge::client::TokenStream,
bridge::client::Span,
bridge::client::Group,
bridge::client::Ident,
bridge::client::Literal,
>,
@ -788,7 +788,7 @@ impl fmt::Display for TokenTree {
/// A `Group` internally contains a `TokenStream` which is surrounded by `Delimiter`s.
#[derive(Clone)]
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
pub struct Group(bridge::client::Group);
pub struct Group(bridge::Group<bridge::client::TokenStream, bridge::client::Span>);
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
impl !Send for Group {}
@ -825,13 +825,17 @@ impl Group {
/// method below.
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
pub fn new(delimiter: Delimiter, stream: TokenStream) -> Group {
Group(bridge::client::Group::new(delimiter, stream.0))
Group(bridge::Group {
delimiter,
stream: stream.0,
span: bridge::DelimSpan::from_single(Span::call_site().0),
})
}
/// Returns the delimiter of this `Group`
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
pub fn delimiter(&self) -> Delimiter {
self.0.delimiter()
self.0.delimiter
}
/// Returns the `TokenStream` of tokens that are delimited in this `Group`.
@ -840,7 +844,7 @@ impl Group {
/// returned above.
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
pub fn stream(&self) -> TokenStream {
TokenStream(Some(self.0.stream()))
TokenStream(self.0.stream.clone())
}
/// Returns the span for the delimiters of this token stream, spanning the
@ -852,7 +856,7 @@ impl Group {
/// ```
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
pub fn span(&self) -> Span {
Span(self.0.span())
Span(self.0.span.entire)
}
/// Returns the span pointing to the opening delimiter of this group.
@ -863,7 +867,7 @@ impl Group {
/// ```
#[stable(feature = "proc_macro_group_span", since = "1.55.0")]
pub fn span_open(&self) -> Span {
Span(self.0.span_open())
Span(self.0.span.open)
}
/// Returns the span pointing to the closing delimiter of this group.
@ -874,7 +878,7 @@ impl Group {
/// ```
#[stable(feature = "proc_macro_group_span", since = "1.55.0")]
pub fn span_close(&self) -> Span {
Span(self.0.span_close())
Span(self.0.span.close)
}
/// Configures the span for this `Group`'s delimiters, but not its internal
@ -885,7 +889,7 @@ impl Group {
/// tokens at the level of the `Group`.
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
pub fn set_span(&mut self, span: Span) {
self.0.set_span(span.0);
self.0.span = bridge::DelimSpan::from_single(span.0);
}
}