Skip to content

Commit dc613dc

Browse files
committed
proc_macro: stop using a remote object handle for Punct
This greatly reduces round-trips to fetch relevant extra information about the token in proc macro code, and avoids RPC messages to create Punct tokens.
1 parent b0ca197 commit dc613dc

File tree

5 files changed

+47
-94
lines changed

5 files changed

+47
-94
lines changed

compiler/rustc_expand/src/proc_macro_server.rs

Lines changed: 16 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ use rustc_span::hygiene::ExpnKind;
1919
use rustc_span::symbol::{self, kw, sym, Symbol};
2020
use rustc_span::{BytePos, FileName, MultiSpan, Pos, RealFileName, SourceFile, Span};
2121

22-
use pm::bridge::{server, TokenTree};
23-
use pm::{Delimiter, Level, LineColumn, Spacing};
22+
use pm::bridge::{server, Punct, TokenTree};
23+
use pm::{Delimiter, Level, LineColumn};
2424
use std::ops::Bound;
2525
use std::{ascii, panic};
2626

@@ -55,7 +55,7 @@ impl ToInternal<token::DelimToken> for Delimiter {
5555
}
5656

5757
impl FromInternal<(TreeAndSpacing, &'_ mut Vec<Self>, &mut Rustc<'_>)>
58-
for TokenTree<Group, Punct, Ident, Literal>
58+
for TokenTree<Span, Group, Ident, Literal>
5959
{
6060
fn from_internal(
6161
((tree, spacing), stack, rustc): (TreeAndSpacing, &mut Vec<Self>, &mut Rustc<'_>),
@@ -84,16 +84,16 @@ impl FromInternal<(TreeAndSpacing, &'_ mut Vec<Self>, &mut Rustc<'_>)>
8484
}
8585
macro_rules! op {
8686
($a:expr) => {
87-
tt!(Punct::new($a, joint))
87+
tt!(Punct { ch: $a, joint })
8888
};
8989
($a:expr, $b:expr) => {{
90-
stack.push(tt!(Punct::new($b, joint)));
91-
tt!(Punct::new($a, true))
90+
stack.push(tt!(Punct { ch: $b, joint }));
91+
tt!(Punct { ch: $a, joint: true })
9292
}};
9393
($a:expr, $b:expr, $c:expr) => {{
94-
stack.push(tt!(Punct::new($c, joint)));
95-
stack.push(tt!(Punct::new($b, true)));
96-
tt!(Punct::new($a, true))
94+
stack.push(tt!(Punct { ch: $c, joint }));
95+
stack.push(tt!(Punct { ch: $b, joint: true }));
96+
tt!(Punct { ch: $a, joint: true })
9797
}};
9898
}
9999

@@ -151,7 +151,7 @@ impl FromInternal<(TreeAndSpacing, &'_ mut Vec<Self>, &mut Rustc<'_>)>
151151
Lifetime(name) => {
152152
let ident = symbol::Ident::new(name, span).without_first_quote();
153153
stack.push(tt!(Ident::new(rustc.sess, ident.name, false)));
154-
tt!(Punct::new('\'', true))
154+
tt!(Punct { ch: '\'', joint: true })
155155
}
156156
Literal(lit) => tt!(Literal { lit }),
157157
DocComment(_, attr_style, data) => {
@@ -174,9 +174,9 @@ impl FromInternal<(TreeAndSpacing, &'_ mut Vec<Self>, &mut Rustc<'_>)>
174174
flatten: false,
175175
}));
176176
if attr_style == ast::AttrStyle::Inner {
177-
stack.push(tt!(Punct::new('!', false)));
177+
stack.push(tt!(Punct { ch: '!', joint: false }));
178178
}
179-
tt!(Punct::new('#', false))
179+
tt!(Punct { ch: '#', joint: false })
180180
}
181181

182182
Interpolated(nt) => {
@@ -199,7 +199,7 @@ impl FromInternal<(TreeAndSpacing, &'_ mut Vec<Self>, &mut Rustc<'_>)>
199199
}
200200
}
201201

202-
impl ToInternal<TokenStream> for TokenTree<Group, Punct, Ident, Literal> {
202+
impl ToInternal<TokenStream> for TokenTree<Span, Group, Ident, Literal> {
203203
fn to_internal(self) -> TokenStream {
204204
use rustc_ast::token::*;
205205

@@ -295,27 +295,6 @@ pub struct Group {
295295
flatten: bool,
296296
}
297297

298-
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
299-
pub struct Punct {
300-
ch: char,
301-
// NB. not using `Spacing` here because it doesn't implement `Hash`.
302-
joint: bool,
303-
span: Span,
304-
}
305-
306-
impl Punct {
307-
fn new(ch: char, joint: bool, span: Span) -> Punct {
308-
const LEGAL_CHARS: &[char] = &[
309-
'=', '<', '>', '!', '~', '+', '-', '*', '/', '%', '^', '&', '|', '@', '.', ',', ';',
310-
':', '#', '$', '?', '\'',
311-
];
312-
if !LEGAL_CHARS.contains(&ch) {
313-
panic!("unsupported character `{:?}`", ch)
314-
}
315-
Punct { ch, joint, span }
316-
}
317-
}
318-
319298
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
320299
pub struct Ident {
321300
sym: Symbol,
@@ -393,7 +372,6 @@ impl server::Types for Rustc<'_> {
393372
type FreeFunctions = FreeFunctions;
394373
type TokenStream = TokenStream;
395374
type Group = Group;
396-
type Punct = Punct;
397375
type Ident = Ident;
398376
type Literal = Literal;
399377
type SourceFile = Lrc<SourceFile>;
@@ -425,14 +403,14 @@ impl server::TokenStream for Rustc<'_> {
425403
}
426404
fn from_token_tree(
427405
&mut self,
428-
tree: TokenTree<Self::Group, Self::Punct, Self::Ident, Self::Literal>,
406+
tree: TokenTree<Self::Span, Self::Group, Self::Ident, Self::Literal>,
429407
) -> Self::TokenStream {
430408
tree.to_internal()
431409
}
432410
fn concat_trees(
433411
&mut self,
434412
base: Option<Self::TokenStream>,
435-
trees: Vec<TokenTree<Self::Group, Self::Punct, Self::Ident, Self::Literal>>,
413+
trees: Vec<TokenTree<Self::Span, Self::Group, Self::Ident, Self::Literal>>,
436414
) -> Self::TokenStream {
437415
let mut builder = tokenstream::TokenStreamBuilder::new();
438416
if let Some(base) = base {
@@ -460,7 +438,7 @@ impl server::TokenStream for Rustc<'_> {
460438
fn into_iter(
461439
&mut self,
462440
stream: Self::TokenStream,
463-
) -> Vec<TokenTree<Self::Group, Self::Punct, Self::Ident, Self::Literal>> {
441+
) -> Vec<TokenTree<Self::Span, Self::Group, Self::Ident, Self::Literal>> {
464442
// XXX: This is a raw port of the previous approach, and can probably be
465443
// optimized.
466444
let mut cursor = stream.trees();
@@ -521,28 +499,6 @@ impl server::Group for Rustc<'_> {
521499
}
522500
}
523501

524-
impl server::Punct for Rustc<'_> {
525-
fn new(&mut self, ch: char, spacing: Spacing) -> Self::Punct {
526-
Punct::new(ch, spacing == Spacing::Joint, server::Context::call_site(self))
527-
}
528-
fn as_char(&mut self, punct: Self::Punct) -> char {
529-
punct.ch
530-
}
531-
fn spacing(&mut self, punct: Self::Punct) -> Spacing {
532-
if punct.joint {
533-
Spacing::Joint
534-
} else {
535-
Spacing::Alone
536-
}
537-
}
538-
fn span(&mut self, punct: Self::Punct) -> Self::Span {
539-
punct.span
540-
}
541-
fn with_span(&mut self, punct: Self::Punct, span: Self::Span) -> Self::Punct {
542-
Punct { span, ..punct }
543-
}
544-
}
545-
546502
impl server::Ident for Rustc<'_> {
547503
fn new(&mut self, string: &str, span: Self::Span, is_raw: bool) -> Self::Ident {
548504
Ident::new(self.sess, Symbol::intern(string), is_raw, span)

library/proc_macro/src/bridge/client.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,6 @@ define_handles! {
202202
Diagnostic,
203203

204204
'interned:
205-
Punct,
206205
Ident,
207206
Span,
208207
}

library/proc_macro/src/bridge/mod.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -69,19 +69,19 @@ macro_rules! with_api {
6969
wait fn from_str(src: &str) -> $S::TokenStream;
7070
wait fn to_string($self: &$S::TokenStream) -> String;
7171
nowait fn from_token_tree(
72-
tree: TokenTree<$S::Group, $S::Punct, $S::Ident, $S::Literal>,
72+
tree: TokenTree<$S::Span, $S::Group, $S::Ident, $S::Literal>,
7373
) -> $S::TokenStream;
7474
nowait fn concat_trees(
7575
base: Option<$S::TokenStream>,
76-
trees: Vec<TokenTree<$S::Group, $S::Punct, $S::Ident, $S::Literal>>,
76+
trees: Vec<TokenTree<$S::Span, $S::Group, $S::Ident, $S::Literal>>,
7777
) -> $S::TokenStream;
7878
nowait fn concat_streams(
7979
base: Option<$S::TokenStream>,
8080
trees: Vec<$S::TokenStream>,
8181
) -> $S::TokenStream;
8282
wait fn into_iter(
8383
$self: $S::TokenStream
84-
) -> Vec<TokenTree<$S::Group, $S::Punct, $S::Ident, $S::Literal>>;
84+
) -> Vec<TokenTree<$S::Span, $S::Group, $S::Ident, $S::Literal>>;
8585
},
8686
Group {
8787
nowait fn drop($self: $S::Group);
@@ -94,13 +94,6 @@ macro_rules! with_api {
9494
wait fn span_close($self: &$S::Group) -> $S::Span;
9595
nowait fn set_span($self: &mut $S::Group, span: $S::Span);
9696
},
97-
Punct {
98-
wait fn new(ch: char, spacing: Spacing) -> $S::Punct;
99-
wait fn as_char($self: $S::Punct) -> char;
100-
wait fn spacing($self: $S::Punct) -> Spacing;
101-
wait fn span($self: $S::Punct) -> $S::Span;
102-
wait fn with_span($self: $S::Punct, span: $S::Span) -> $S::Punct;
103-
},
10497
Ident {
10598
wait fn new(string: &str, span: $S::Span, is_raw: bool) -> $S::Ident;
10699
wait fn span($self: $S::Ident) -> $S::Span;
@@ -471,15 +464,24 @@ macro_rules! compound_traits {
471464
}
472465

473466
#[derive(Clone)]
474-
pub enum TokenTree<G, P, I, L> {
467+
pub struct Punct<S> {
468+
pub ch: char,
469+
pub joint: bool,
470+
pub span: S,
471+
}
472+
473+
compound_traits!(struct Punct<Sp> { ch, joint, span });
474+
475+
#[derive(Clone)]
476+
pub enum TokenTree<S, G, I, L> {
475477
Group(G),
476-
Punct(P),
478+
Punct(Punct<S>),
477479
Ident(I),
478480
Literal(L),
479481
}
480482

481483
compound_traits!(
482-
enum TokenTree<G, P, I, L> {
484+
enum TokenTree<Sp, G, I, L> {
483485
Group(tt),
484486
Punct(tt),
485487
Ident(tt),

library/proc_macro/src/bridge/server.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ macro_rules! associated_item {
1616
(type TokenStream: 'static + Clone;);
1717
(type Group) =>
1818
(type Group: 'static + Clone;);
19-
(type Punct) =>
20-
(type Punct: 'static + Copy + Eq + Hash;);
2119
(type Ident) =>
2220
(type Ident: 'static + Copy + Eq + Hash;);
2321
(type Literal) =>

library/proc_macro/src/lib.rs

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,8 @@ pub use quote::{quote, quote_span};
180180
fn tree_to_bridge_tree(
181181
tree: TokenTree,
182182
) -> bridge::TokenTree<
183+
bridge::client::Span,
183184
bridge::client::Group,
184-
bridge::client::Punct,
185185
bridge::client::Ident,
186186
bridge::client::Literal,
187187
> {
@@ -257,8 +257,8 @@ pub mod token_stream {
257257
pub struct IntoIter(
258258
std::vec::IntoIter<
259259
bridge::TokenTree<
260+
bridge::client::Span,
260261
bridge::client::Group,
261-
bridge::client::Punct,
262262
bridge::client::Ident,
263263
bridge::client::Literal,
264264
>,
@@ -799,7 +799,7 @@ impl fmt::Debug for Group {
799799
/// forms of `Spacing` returned.
800800
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
801801
#[derive(Clone)]
802-
pub struct Punct(bridge::client::Punct);
802+
pub struct Punct(bridge::Punct<bridge::client::Span>);
803803

804804
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
805805
impl !Send for Punct {}
@@ -832,13 +832,20 @@ impl Punct {
832832
/// which can be further configured with the `set_span` method below.
833833
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
834834
pub fn new(ch: char, spacing: Spacing) -> Punct {
835-
Punct(bridge::client::Punct::new(ch, spacing))
835+
const LEGAL_CHARS: &[char] = &[
836+
'=', '<', '>', '!', '~', '+', '-', '*', '/', '%', '^', '&', '|', '@', '.', ',', ';',
837+
':', '#', '$', '?', '\'',
838+
];
839+
if !LEGAL_CHARS.contains(&ch) {
840+
panic!("unsupported character `{:?}`", ch);
841+
}
842+
Punct(bridge::Punct { ch, joint: spacing == Spacing::Joint, span: Span::call_site().0 })
836843
}
837844

838845
/// Returns the value of this punctuation character as `char`.
839846
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
840847
pub fn as_char(&self) -> char {
841-
self.0.as_char()
848+
self.0.ch
842849
}
843850

844851
/// Returns the spacing of this punctuation character, indicating whether it's immediately
@@ -847,28 +854,19 @@ impl Punct {
847854
/// (`Alone`) so the operator has certainly ended.
848855
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
849856
pub fn spacing(&self) -> Spacing {
850-
self.0.spacing()
857+
if self.0.joint { Spacing::Joint } else { Spacing::Alone }
851858
}
852859

853860
/// Returns the span for this punctuation character.
854861
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
855862
pub fn span(&self) -> Span {
856-
Span(self.0.span())
863+
Span(self.0.span)
857864
}
858865

859866
/// Configure the span for this punctuation character.
860867
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
861868
pub fn set_span(&mut self, span: Span) {
862-
self.0 = self.0.with_span(span.0);
863-
}
864-
}
865-
866-
// N.B., the bridge only provides `to_string`, implement `fmt::Display`
867-
// based on it (the reverse of the usual relationship between the two).
868-
#[stable(feature = "proc_macro_lib", since = "1.15.0")]
869-
impl ToString for Punct {
870-
fn to_string(&self) -> String {
871-
TokenStream::from(TokenTree::from(self.clone())).to_string()
869+
self.0.span = span.0;
872870
}
873871
}
874872

@@ -877,7 +875,7 @@ impl ToString for Punct {
877875
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
878876
impl fmt::Display for Punct {
879877
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
880-
f.write_str(&self.to_string())
878+
write!(f, "{}", self.as_char())
881879
}
882880
}
883881

0 commit comments

Comments
 (0)