Skip to content

Commit 0197965

Browse files
committed
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.
1 parent 827dcc6 commit 0197965

File tree

4 files changed

+45
-36
lines changed

4 files changed

+45
-36
lines changed

proc_macro/src/bridge/client.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,6 @@ define_handles! {
175175
'owned:
176176
FreeFunctions,
177177
TokenStream,
178-
Group,
179178
Literal,
180179
SourceFile,
181180
MultiSpan,
@@ -198,12 +197,6 @@ impl Clone for TokenStream {
198197
}
199198
}
200199

201-
impl Clone for Group {
202-
fn clone(&self) -> Self {
203-
self.clone()
204-
}
205-
}
206-
207200
impl Clone for Literal {
208201
fn clone(&self) -> Self {
209202
self.clone()

proc_macro/src/bridge/mod.rs

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -65,30 +65,19 @@ macro_rules! with_api {
6565
fn from_str(src: &str) -> $S::TokenStream;
6666
fn to_string($self: &$S::TokenStream) -> String;
6767
fn from_token_tree(
68-
tree: TokenTree<$S::Span, $S::Group, $S::Ident, $S::Literal>,
68+
tree: TokenTree<$S::TokenStream, $S::Span, $S::Ident, $S::Literal>,
6969
) -> $S::TokenStream;
7070
fn concat_trees(
7171
base: Option<$S::TokenStream>,
72-
trees: Vec<TokenTree<$S::Span, $S::Group, $S::Ident, $S::Literal>>,
72+
trees: Vec<TokenTree<$S::TokenStream, $S::Span, $S::Ident, $S::Literal>>,
7373
) -> $S::TokenStream;
7474
fn concat_streams(
7575
base: Option<$S::TokenStream>,
7676
streams: Vec<$S::TokenStream>,
7777
) -> $S::TokenStream;
7878
fn into_trees(
7979
$self: $S::TokenStream
80-
) -> Vec<TokenTree<$S::Span, $S::Group, $S::Ident, $S::Literal>>;
81-
},
82-
Group {
83-
fn drop($self: $S::Group);
84-
fn clone($self: &$S::Group) -> $S::Group;
85-
fn new(delimiter: Delimiter, stream: Option<$S::TokenStream>) -> $S::Group;
86-
fn delimiter($self: &$S::Group) -> Delimiter;
87-
fn stream($self: &$S::Group) -> $S::TokenStream;
88-
fn span($self: &$S::Group) -> $S::Span;
89-
fn span_open($self: &$S::Group) -> $S::Span;
90-
fn span_close($self: &$S::Group) -> $S::Span;
91-
fn set_span($self: &mut $S::Group, span: $S::Span);
80+
) -> Vec<TokenTree<$S::TokenStream, $S::Span, $S::Ident, $S::Literal>>;
9281
},
9382
Ident {
9483
fn new(string: &str, span: $S::Span, is_raw: bool) -> $S::Ident;
@@ -441,6 +430,30 @@ compound_traits!(
441430
}
442431
);
443432

433+
#[derive(Copy, Clone)]
434+
pub struct DelimSpan<S> {
435+
pub open: S,
436+
pub close: S,
437+
pub entire: S,
438+
}
439+
440+
impl<S: Copy> DelimSpan<S> {
441+
pub fn from_single(span: S) -> Self {
442+
DelimSpan { open: span, close: span, entire: span }
443+
}
444+
}
445+
446+
compound_traits!(struct DelimSpan<Sp> { open, close, entire });
447+
448+
#[derive(Clone)]
449+
pub struct Group<T, S> {
450+
pub delimiter: Delimiter,
451+
pub stream: Option<T>,
452+
pub span: DelimSpan<S>,
453+
}
454+
455+
compound_traits!(struct Group<T, Sp> { delimiter, stream, span });
456+
444457
#[derive(Clone)]
445458
pub struct Punct<S> {
446459
pub ch: char,
@@ -451,15 +464,15 @@ pub struct Punct<S> {
451464
compound_traits!(struct Punct<Sp> { ch, joint, span });
452465

453466
#[derive(Clone)]
454-
pub enum TokenTree<S, G, I, L> {
455-
Group(G),
467+
pub enum TokenTree<T, S, I, L> {
468+
Group(Group<T, S>),
456469
Punct(Punct<S>),
457470
Ident(I),
458471
Literal(L),
459472
}
460473

461474
compound_traits!(
462-
enum TokenTree<Sp, G, I, L> {
475+
enum TokenTree<T, Sp, I, L> {
463476
Group(tt),
464477
Punct(tt),
465478
Ident(tt),

proc_macro/src/bridge/server.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use super::client::HandleStore;
88
pub trait Types {
99
type FreeFunctions: 'static;
1010
type TokenStream: 'static + Clone;
11-
type Group: 'static + Clone;
1211
type Ident: 'static + Copy + Eq + Hash;
1312
type Literal: 'static + Clone;
1413
type SourceFile: 'static + Clone;

proc_macro/src/lib.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,8 @@ pub use quote::{quote, quote_span};
212212
fn tree_to_bridge_tree(
213213
tree: TokenTree,
214214
) -> bridge::TokenTree<
215+
bridge::client::TokenStream,
215216
bridge::client::Span,
216-
bridge::client::Group,
217217
bridge::client::Ident,
218218
bridge::client::Literal,
219219
> {
@@ -238,8 +238,8 @@ impl From<TokenTree> for TokenStream {
238238
struct ConcatTreesHelper {
239239
trees: Vec<
240240
bridge::TokenTree<
241+
bridge::client::TokenStream,
241242
bridge::client::Span,
242-
bridge::client::Group,
243243
bridge::client::Ident,
244244
bridge::client::Literal,
245245
>,
@@ -365,8 +365,8 @@ pub mod token_stream {
365365
pub struct IntoIter(
366366
std::vec::IntoIter<
367367
bridge::TokenTree<
368+
bridge::client::TokenStream,
368369
bridge::client::Span,
369-
bridge::client::Group,
370370
bridge::client::Ident,
371371
bridge::client::Literal,
372372
>,
@@ -788,7 +788,7 @@ impl fmt::Display for TokenTree {
788788
/// A `Group` internally contains a `TokenStream` which is surrounded by `Delimiter`s.
789789
#[derive(Clone)]
790790
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
791-
pub struct Group(bridge::client::Group);
791+
pub struct Group(bridge::Group<bridge::client::TokenStream, bridge::client::Span>);
792792

793793
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
794794
impl !Send for Group {}
@@ -825,13 +825,17 @@ impl Group {
825825
/// method below.
826826
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
827827
pub fn new(delimiter: Delimiter, stream: TokenStream) -> Group {
828-
Group(bridge::client::Group::new(delimiter, stream.0))
828+
Group(bridge::Group {
829+
delimiter,
830+
stream: stream.0,
831+
span: bridge::DelimSpan::from_single(Span::call_site().0),
832+
})
829833
}
830834

831835
/// Returns the delimiter of this `Group`
832836
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
833837
pub fn delimiter(&self) -> Delimiter {
834-
self.0.delimiter()
838+
self.0.delimiter
835839
}
836840

837841
/// Returns the `TokenStream` of tokens that are delimited in this `Group`.
@@ -840,7 +844,7 @@ impl Group {
840844
/// returned above.
841845
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
842846
pub fn stream(&self) -> TokenStream {
843-
TokenStream(Some(self.0.stream()))
847+
TokenStream(self.0.stream.clone())
844848
}
845849

846850
/// Returns the span for the delimiters of this token stream, spanning the
@@ -852,7 +856,7 @@ impl Group {
852856
/// ```
853857
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
854858
pub fn span(&self) -> Span {
855-
Span(self.0.span())
859+
Span(self.0.span.entire)
856860
}
857861

858862
/// Returns the span pointing to the opening delimiter of this group.
@@ -863,7 +867,7 @@ impl Group {
863867
/// ```
864868
#[stable(feature = "proc_macro_group_span", since = "1.55.0")]
865869
pub fn span_open(&self) -> Span {
866-
Span(self.0.span_open())
870+
Span(self.0.span.open)
867871
}
868872

869873
/// Returns the span pointing to the closing delimiter of this group.
@@ -874,7 +878,7 @@ impl Group {
874878
/// ```
875879
#[stable(feature = "proc_macro_group_span", since = "1.55.0")]
876880
pub fn span_close(&self) -> Span {
877-
Span(self.0.span_close())
881+
Span(self.0.span.close)
878882
}
879883

880884
/// Configures the span for this `Group`'s delimiters, but not its internal
@@ -885,7 +889,7 @@ impl Group {
885889
/// tokens at the level of the `Group`.
886890
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
887891
pub fn set_span(&mut self, span: Span) {
888-
self.0.set_span(span.0);
892+
self.0.span = bridge::DelimSpan::from_single(span.0);
889893
}
890894
}
891895

0 commit comments

Comments
 (0)