Skip to content

Commit 801c77b

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 29d8b9c commit 801c77b

File tree

6 files changed

+48
-90
lines changed

6 files changed

+48
-90
lines changed

compiler/rustc_expand/src/proc_macro_server.rs

Lines changed: 16 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ use rustc_span::def_id::CrateNum;
1414
use rustc_span::symbol::{self, kw, sym, Symbol};
1515
use rustc_span::{BytePos, FileName, Pos, SourceFile, Span};
1616

17-
use pm::bridge::{server, TokenTree};
18-
use pm::{Delimiter, Level, LineColumn, Spacing};
17+
use pm::bridge::{server, Punct, TokenTree};
18+
use pm::{Delimiter, Level, LineColumn};
1919
use std::ops::Bound;
2020
use std::{ascii, panic};
2121

@@ -50,7 +50,7 @@ impl ToInternal<token::Delimiter> for Delimiter {
5050
}
5151

5252
impl FromInternal<(TreeAndSpacing, &'_ mut Vec<Self>, &mut Rustc<'_, '_>)>
53-
for TokenTree<Group, Punct, Ident, Literal>
53+
for TokenTree<Span, Group, Ident, Literal>
5454
{
5555
fn from_internal(
5656
((tree, spacing), stack, rustc): (TreeAndSpacing, &mut Vec<Self>, &mut Rustc<'_, '_>),
@@ -79,16 +79,16 @@ impl FromInternal<(TreeAndSpacing, &'_ mut Vec<Self>, &mut Rustc<'_, '_>)>
7979
}
8080
macro_rules! op {
8181
($a:expr) => {
82-
tt!(Punct::new($a, joint))
82+
tt!(Punct { ch: $a, joint })
8383
};
8484
($a:expr, $b:expr) => {{
85-
stack.push(tt!(Punct::new($b, joint)));
86-
tt!(Punct::new($a, true))
85+
stack.push(tt!(Punct { ch: $b, joint }));
86+
tt!(Punct { ch: $a, joint: true })
8787
}};
8888
($a:expr, $b:expr, $c:expr) => {{
89-
stack.push(tt!(Punct::new($c, joint)));
90-
stack.push(tt!(Punct::new($b, true)));
91-
tt!(Punct::new($a, true))
89+
stack.push(tt!(Punct { ch: $c, joint }));
90+
stack.push(tt!(Punct { ch: $b, joint: true }));
91+
tt!(Punct { ch: $a, joint: true })
9292
}};
9393
}
9494

@@ -146,7 +146,7 @@ impl FromInternal<(TreeAndSpacing, &'_ mut Vec<Self>, &mut Rustc<'_, '_>)>
146146
Lifetime(name) => {
147147
let ident = symbol::Ident::new(name, span).without_first_quote();
148148
stack.push(tt!(Ident::new(rustc.sess(), ident.name, false)));
149-
tt!(Punct::new('\'', true))
149+
tt!(Punct { ch: '\'', joint: true })
150150
}
151151
Literal(lit) => tt!(Literal { lit }),
152152
DocComment(_, attr_style, data) => {
@@ -169,9 +169,9 @@ impl FromInternal<(TreeAndSpacing, &'_ mut Vec<Self>, &mut Rustc<'_, '_>)>
169169
flatten: false,
170170
}));
171171
if attr_style == ast::AttrStyle::Inner {
172-
stack.push(tt!(Punct::new('!', false)));
172+
stack.push(tt!(Punct { ch: '!', joint: false }));
173173
}
174-
tt!(Punct::new('#', false))
174+
tt!(Punct { ch: '#', joint: false })
175175
}
176176

177177
Interpolated(nt) if let NtIdent(ident, is_raw) = *nt => {
@@ -192,7 +192,7 @@ impl FromInternal<(TreeAndSpacing, &'_ mut Vec<Self>, &mut Rustc<'_, '_>)>
192192
}
193193
}
194194

195-
impl ToInternal<TokenStream> for TokenTree<Group, Punct, Ident, Literal> {
195+
impl ToInternal<TokenStream> for TokenTree<Span, Group, Ident, Literal> {
196196
fn to_internal(self) -> TokenStream {
197197
use rustc_ast::token::*;
198198

@@ -288,27 +288,6 @@ pub struct Group {
288288
flatten: bool,
289289
}
290290

291-
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
292-
pub struct Punct {
293-
ch: char,
294-
// NB. not using `Spacing` here because it doesn't implement `Hash`.
295-
joint: bool,
296-
span: Span,
297-
}
298-
299-
impl Punct {
300-
fn new(ch: char, joint: bool, span: Span) -> Punct {
301-
const LEGAL_CHARS: &[char] = &[
302-
'=', '<', '>', '!', '~', '+', '-', '*', '/', '%', '^', '&', '|', '@', '.', ',', ';',
303-
':', '#', '$', '?', '\'',
304-
];
305-
if !LEGAL_CHARS.contains(&ch) {
306-
panic!("unsupported character `{:?}`", ch)
307-
}
308-
Punct { ch, joint, span }
309-
}
310-
}
311-
312291
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
313292
pub struct Ident {
314293
sym: Symbol,
@@ -380,7 +359,6 @@ impl server::Types for Rustc<'_, '_> {
380359
type FreeFunctions = FreeFunctions;
381360
type TokenStream = TokenStream;
382361
type Group = Group;
383-
type Punct = Punct;
384362
type Ident = Ident;
385363
type Literal = Literal;
386364
type SourceFile = Lrc<SourceFile>;
@@ -469,14 +447,14 @@ impl server::TokenStream for Rustc<'_, '_> {
469447
}
470448
fn from_token_tree(
471449
&mut self,
472-
tree: TokenTree<Self::Group, Self::Punct, Self::Ident, Self::Literal>,
450+
tree: TokenTree<Self::Span, Self::Group, Self::Ident, Self::Literal>,
473451
) -> Self::TokenStream {
474452
tree.to_internal()
475453
}
476454
fn concat_trees(
477455
&mut self,
478456
base: Option<Self::TokenStream>,
479-
trees: Vec<TokenTree<Self::Group, Self::Punct, Self::Ident, Self::Literal>>,
457+
trees: Vec<TokenTree<Self::Span, Self::Group, Self::Ident, Self::Literal>>,
480458
) -> Self::TokenStream {
481459
let mut builder = tokenstream::TokenStreamBuilder::new();
482460
if let Some(base) = base {
@@ -504,7 +482,7 @@ impl server::TokenStream for Rustc<'_, '_> {
504482
fn into_iter(
505483
&mut self,
506484
stream: Self::TokenStream,
507-
) -> Vec<TokenTree<Self::Group, Self::Punct, Self::Ident, Self::Literal>> {
485+
) -> Vec<TokenTree<Self::Span, Self::Group, Self::Ident, Self::Literal>> {
508486
// XXX: This is a raw port of the previous approach, and can probably be
509487
// optimized.
510488
let mut cursor = stream.into_trees();
@@ -565,24 +543,6 @@ impl server::Group for Rustc<'_, '_> {
565543
}
566544
}
567545

568-
impl server::Punct for Rustc<'_, '_> {
569-
fn new(&mut self, ch: char, spacing: Spacing) -> Self::Punct {
570-
Punct::new(ch, spacing == Spacing::Joint, server::Context::call_site(self))
571-
}
572-
fn as_char(&mut self, punct: Self::Punct) -> char {
573-
punct.ch
574-
}
575-
fn spacing(&mut self, punct: Self::Punct) -> Spacing {
576-
if punct.joint { Spacing::Joint } else { Spacing::Alone }
577-
}
578-
fn span(&mut self, punct: Self::Punct) -> Self::Span {
579-
punct.span
580-
}
581-
fn with_span(&mut self, punct: Self::Punct, span: Self::Span) -> Self::Punct {
582-
Punct { span, ..punct }
583-
}
584-
}
585-
586546
impl server::Ident for Rustc<'_, '_> {
587547
fn new(&mut self, string: &str, span: Self::Span, is_raw: bool) -> Self::Ident {
588548
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
@@ -185,7 +185,6 @@ define_handles! {
185185
Diagnostic,
186186

187187
'interned:
188-
Punct,
189188
Ident,
190189
Span,
191190
}

library/proc_macro/src/bridge/mod.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -65,19 +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::Group, $S::Punct, $S::Ident, $S::Literal>,
68+
tree: TokenTree<$S::Span, $S::Group, $S::Ident, $S::Literal>,
6969
) -> $S::TokenStream;
7070
fn concat_trees(
7171
base: Option<$S::TokenStream>,
72-
trees: Vec<TokenTree<$S::Group, $S::Punct, $S::Ident, $S::Literal>>,
72+
trees: Vec<TokenTree<$S::Span, $S::Group, $S::Ident, $S::Literal>>,
7373
) -> $S::TokenStream;
7474
fn concat_streams(
7575
base: Option<$S::TokenStream>,
7676
trees: Vec<$S::TokenStream>,
7777
) -> $S::TokenStream;
7878
fn into_iter(
7979
$self: $S::TokenStream
80-
) -> Vec<TokenTree<$S::Group, $S::Punct, $S::Ident, $S::Literal>>;
80+
) -> Vec<TokenTree<$S::Span, $S::Group, $S::Ident, $S::Literal>>;
8181
},
8282
Group {
8383
fn drop($self: $S::Group);
@@ -90,13 +90,6 @@ macro_rules! with_api {
9090
fn span_close($self: &$S::Group) -> $S::Span;
9191
fn set_span($self: &mut $S::Group, span: $S::Span);
9292
},
93-
Punct {
94-
fn new(ch: char, spacing: Spacing) -> $S::Punct;
95-
fn as_char($self: $S::Punct) -> char;
96-
fn spacing($self: $S::Punct) -> Spacing;
97-
fn span($self: $S::Punct) -> $S::Span;
98-
fn with_span($self: $S::Punct, span: $S::Span) -> $S::Punct;
99-
},
10093
Ident {
10194
fn new(string: &str, span: $S::Span, is_raw: bool) -> $S::Ident;
10295
fn span($self: $S::Ident) -> $S::Span;
@@ -454,15 +447,24 @@ macro_rules! compound_traits {
454447
}
455448

456449
#[derive(Clone)]
457-
pub enum TokenTree<G, P, I, L> {
450+
pub struct Punct<S> {
451+
pub ch: char,
452+
pub joint: bool,
453+
pub span: S,
454+
}
455+
456+
compound_traits!(struct Punct<Sp> { ch, joint, span });
457+
458+
#[derive(Clone)]
459+
pub enum TokenTree<S, G, I, L> {
458460
Group(G),
459-
Punct(P),
461+
Punct(Punct<S>),
460462
Ident(I),
461463
Literal(L),
462464
}
463465

464466
compound_traits!(
465-
enum TokenTree<G, P, I, L> {
467+
enum TokenTree<Sp, G, I, L> {
466468
Group(tt),
467469
Punct(tt),
468470
Ident(tt),

library/proc_macro/src/bridge/server.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ pub trait Types {
99
type FreeFunctions: 'static;
1010
type TokenStream: 'static + Clone;
1111
type Group: 'static + Clone;
12-
type Punct: 'static + Copy + Eq + Hash;
1312
type Ident: 'static + Copy + Eq + Hash;
1413
type Literal: 'static + Clone;
1514
type SourceFile: 'static + Clone;

library/proc_macro/src/lib.rs

Lines changed: 16 additions & 18 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::Span,
215216
bridge::client::Group,
216-
bridge::client::Punct,
217217
bridge::client::Ident,
218218
bridge::client::Literal,
219219
> {
@@ -289,8 +289,8 @@ pub mod token_stream {
289289
pub struct IntoIter(
290290
std::vec::IntoIter<
291291
bridge::TokenTree<
292+
bridge::client::Span,
292293
bridge::client::Group,
293-
bridge::client::Punct,
294294
bridge::client::Ident,
295295
bridge::client::Literal,
296296
>,
@@ -849,7 +849,7 @@ impl fmt::Debug for Group {
849849
/// forms of `Spacing` returned.
850850
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
851851
#[derive(Clone)]
852-
pub struct Punct(bridge::client::Punct);
852+
pub struct Punct(bridge::Punct<bridge::client::Span>);
853853

854854
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
855855
impl !Send for Punct {}
@@ -882,13 +882,20 @@ impl Punct {
882882
/// which can be further configured with the `set_span` method below.
883883
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
884884
pub fn new(ch: char, spacing: Spacing) -> Punct {
885-
Punct(bridge::client::Punct::new(ch, spacing))
885+
const LEGAL_CHARS: &[char] = &[
886+
'=', '<', '>', '!', '~', '+', '-', '*', '/', '%', '^', '&', '|', '@', '.', ',', ';',
887+
':', '#', '$', '?', '\'',
888+
];
889+
if !LEGAL_CHARS.contains(&ch) {
890+
panic!("unsupported character `{:?}`", ch);
891+
}
892+
Punct(bridge::Punct { ch, joint: spacing == Spacing::Joint, span: Span::call_site().0 })
886893
}
887894

888895
/// Returns the value of this punctuation character as `char`.
889896
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
890897
pub fn as_char(&self) -> char {
891-
self.0.as_char()
898+
self.0.ch
892899
}
893900

894901
/// Returns the spacing of this punctuation character, indicating whether it's immediately
@@ -897,28 +904,19 @@ impl Punct {
897904
/// (`Alone`) so the operator has certainly ended.
898905
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
899906
pub fn spacing(&self) -> Spacing {
900-
self.0.spacing()
907+
if self.0.joint { Spacing::Joint } else { Spacing::Alone }
901908
}
902909

903910
/// Returns the span for this punctuation character.
904911
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
905912
pub fn span(&self) -> Span {
906-
Span(self.0.span())
913+
Span(self.0.span)
907914
}
908915

909916
/// Configure the span for this punctuation character.
910917
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
911918
pub fn set_span(&mut self, span: Span) {
912-
self.0 = self.0.with_span(span.0);
913-
}
914-
}
915-
916-
// N.B., the bridge only provides `to_string`, implement `fmt::Display`
917-
// based on it (the reverse of the usual relationship between the two).
918-
#[stable(feature = "proc_macro_lib", since = "1.15.0")]
919-
impl ToString for Punct {
920-
fn to_string(&self) -> String {
921-
TokenStream::from(TokenTree::from(self.clone())).to_string()
919+
self.0.span = span.0;
922920
}
923921
}
924922

@@ -927,7 +925,7 @@ impl ToString for Punct {
927925
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
928926
impl fmt::Display for Punct {
929927
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
930-
f.write_str(&self.to_string())
928+
write!(f, "{}", self.as_char())
931929
}
932930
}
933931

src/test/ui/proc-macro/invalid-punct-ident-1.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// rustc-env:RUST_BACKTRACE=0
33

44
// FIXME https://github.com/rust-lang/rust/issues/59998
5-
// normalize-stderr-test "thread.*panicked.*proc_macro_server.rs.*\n" -> ""
5+
// normalize-stderr-test "thread.*panicked.*proc_macro.*lib.rs.*\n" -> ""
66
// normalize-stderr-test "note:.*RUST_BACKTRACE=1.*\n" -> ""
77
// normalize-stderr-test "\nerror: internal compiler error.*\n\n" -> ""
88
// normalize-stderr-test "note:.*unexpectedly panicked.*\n\n" -> ""

0 commit comments

Comments
 (0)