Skip to content

Commit 73aa198

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 b805b59 commit 73aa198

File tree

4 files changed

+32
-34
lines changed

4 files changed

+32
-34
lines changed

proc_macro/src/bridge/client.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,6 @@ define_handles! {
182182
Diagnostic,
183183

184184
'interned:
185-
Punct,
186185
Ident,
187186
Span,
188187
}

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
streams: Vec<$S::TokenStream>,
7777
) -> $S::TokenStream;
7878
fn into_trees(
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;
@@ -449,15 +442,24 @@ compound_traits!(
449442
);
450443

451444
#[derive(Clone)]
452-
pub enum TokenTree<G, P, I, L> {
445+
pub struct Punct<S> {
446+
pub ch: char,
447+
pub joint: bool,
448+
pub span: S,
449+
}
450+
451+
compound_traits!(struct Punct<Sp> { ch, joint, span });
452+
453+
#[derive(Clone)]
454+
pub enum TokenTree<S, G, I, L> {
453455
Group(G),
454-
Punct(P),
456+
Punct(Punct<S>),
455457
Ident(I),
456458
Literal(L),
457459
}
458460

459461
compound_traits!(
460-
enum TokenTree<G, P, I, L> {
462+
enum TokenTree<Sp, G, I, L> {
461463
Group(tt),
462464
Punct(tt),
463465
Ident(tt),

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;

proc_macro/src/lib.rs

Lines changed: 17 additions & 19 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
> {
@@ -238,8 +238,8 @@ impl From<TokenTree> for TokenStream {
238238
struct ConcatTreesHelper {
239239
trees: Vec<
240240
bridge::TokenTree<
241+
bridge::client::Span,
241242
bridge::client::Group,
242-
bridge::client::Punct,
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::Span,
368369
bridge::client::Group,
369-
bridge::client::Punct,
370370
bridge::client::Ident,
371371
bridge::client::Literal,
372372
>,
@@ -925,7 +925,7 @@ impl fmt::Debug for Group {
925925
/// forms of `Spacing` returned.
926926
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
927927
#[derive(Clone)]
928-
pub struct Punct(bridge::client::Punct);
928+
pub struct Punct(bridge::Punct<bridge::client::Span>);
929929

930930
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
931931
impl !Send for Punct {}
@@ -958,13 +958,20 @@ impl Punct {
958958
/// which can be further configured with the `set_span` method below.
959959
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
960960
pub fn new(ch: char, spacing: Spacing) -> Punct {
961-
Punct(bridge::client::Punct::new(ch, spacing))
961+
const LEGAL_CHARS: &[char] = &[
962+
'=', '<', '>', '!', '~', '+', '-', '*', '/', '%', '^', '&', '|', '@', '.', ',', ';',
963+
':', '#', '$', '?', '\'',
964+
];
965+
if !LEGAL_CHARS.contains(&ch) {
966+
panic!("unsupported character `{:?}`", ch);
967+
}
968+
Punct(bridge::Punct { ch, joint: spacing == Spacing::Joint, span: Span::call_site().0 })
962969
}
963970

964971
/// Returns the value of this punctuation character as `char`.
965972
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
966973
pub fn as_char(&self) -> char {
967-
self.0.as_char()
974+
self.0.ch
968975
}
969976

970977
/// Returns the spacing of this punctuation character, indicating whether it's immediately
@@ -973,28 +980,19 @@ impl Punct {
973980
/// (`Alone`) so the operator has certainly ended.
974981
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
975982
pub fn spacing(&self) -> Spacing {
976-
self.0.spacing()
983+
if self.0.joint { Spacing::Joint } else { Spacing::Alone }
977984
}
978985

979986
/// Returns the span for this punctuation character.
980987
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
981988
pub fn span(&self) -> Span {
982-
Span(self.0.span())
989+
Span(self.0.span)
983990
}
984991

985992
/// Configure the span for this punctuation character.
986993
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
987994
pub fn set_span(&mut self, span: Span) {
988-
self.0 = self.0.with_span(span.0);
989-
}
990-
}
991-
992-
// N.B., the bridge only provides `to_string`, implement `fmt::Display`
993-
// based on it (the reverse of the usual relationship between the two).
994-
#[stable(feature = "proc_macro_lib", since = "1.15.0")]
995-
impl ToString for Punct {
996-
fn to_string(&self) -> String {
997-
TokenStream::from(TokenTree::from(self.clone())).to_string()
995+
self.0.span = span.0;
998996
}
999997
}
1000998

@@ -1003,7 +1001,7 @@ impl ToString for Punct {
10031001
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
10041002
impl fmt::Display for Punct {
10051003
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1006-
f.write_str(&self.to_string())
1004+
write!(f, "{}", self.as_char())
10071005
}
10081006
}
10091007

0 commit comments

Comments
 (0)