1
Fork 0

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.
This commit is contained in:
Nika Layzell 2021-06-28 22:30:55 -04:00
parent 3b0d4813ab
commit 72bfe618fa
6 changed files with 49 additions and 95 deletions

View file

@ -182,7 +182,6 @@ define_handles! {
Diagnostic,
'interned:
Punct,
Ident,
Span,
}

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::Group, $S::Punct, $S::Ident, $S::Literal>,
tree: TokenTree<$S::Span, $S::Group, $S::Ident, $S::Literal>,
) -> $S::TokenStream;
fn concat_trees(
base: Option<$S::TokenStream>,
trees: Vec<TokenTree<$S::Group, $S::Punct, $S::Ident, $S::Literal>>,
trees: Vec<TokenTree<$S::Span, $S::Group, $S::Ident, $S::Literal>>,
) -> $S::TokenStream;
fn concat_streams(
base: Option<$S::TokenStream>,
@ -77,7 +77,7 @@ macro_rules! with_api {
) -> $S::TokenStream;
fn into_trees(
$self: $S::TokenStream
) -> Vec<TokenTree<$S::Group, $S::Punct, $S::Ident, $S::Literal>>;
) -> Vec<TokenTree<$S::Span, $S::Group, $S::Ident, $S::Literal>>;
},
Group {
fn drop($self: $S::Group);
@ -90,13 +90,6 @@ macro_rules! with_api {
fn span_close($self: &$S::Group) -> $S::Span;
fn set_span($self: &mut $S::Group, span: $S::Span);
},
Punct {
fn new(ch: char, spacing: Spacing) -> $S::Punct;
fn as_char($self: $S::Punct) -> char;
fn spacing($self: $S::Punct) -> Spacing;
fn span($self: $S::Punct) -> $S::Span;
fn with_span($self: $S::Punct, span: $S::Span) -> $S::Punct;
},
Ident {
fn new(string: &str, span: $S::Span, is_raw: bool) -> $S::Ident;
fn span($self: $S::Ident) -> $S::Span;
@ -449,15 +442,24 @@ compound_traits!(
);
#[derive(Clone)]
pub enum TokenTree<G, P, I, L> {
pub struct Punct<S> {
pub ch: char,
pub joint: bool,
pub span: S,
}
compound_traits!(struct Punct<Sp> { ch, joint, span });
#[derive(Clone)]
pub enum TokenTree<S, G, I, L> {
Group(G),
Punct(P),
Punct(Punct<S>),
Ident(I),
Literal(L),
}
compound_traits!(
enum TokenTree<G, P, I, L> {
enum TokenTree<Sp, G, I, L> {
Group(tt),
Punct(tt),
Ident(tt),

View file

@ -9,7 +9,6 @@ pub trait Types {
type FreeFunctions: 'static;
type TokenStream: 'static + Clone;
type Group: 'static + Clone;
type Punct: 'static + Copy + Eq + Hash;
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::Span,
bridge::client::Group,
bridge::client::Punct,
bridge::client::Ident,
bridge::client::Literal,
> {
@ -238,8 +238,8 @@ impl From<TokenTree> for TokenStream {
struct ConcatTreesHelper {
trees: Vec<
bridge::TokenTree<
bridge::client::Span,
bridge::client::Group,
bridge::client::Punct,
bridge::client::Ident,
bridge::client::Literal,
>,
@ -365,8 +365,8 @@ pub mod token_stream {
pub struct IntoIter(
std::vec::IntoIter<
bridge::TokenTree<
bridge::client::Span,
bridge::client::Group,
bridge::client::Punct,
bridge::client::Ident,
bridge::client::Literal,
>,
@ -925,7 +925,7 @@ impl fmt::Debug for Group {
/// forms of `Spacing` returned.
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
#[derive(Clone)]
pub struct Punct(bridge::client::Punct);
pub struct Punct(bridge::Punct<bridge::client::Span>);
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
impl !Send for Punct {}
@ -958,13 +958,20 @@ impl Punct {
/// which can be further configured with the `set_span` method below.
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
pub fn new(ch: char, spacing: Spacing) -> Punct {
Punct(bridge::client::Punct::new(ch, spacing))
const LEGAL_CHARS: &[char] = &[
'=', '<', '>', '!', '~', '+', '-', '*', '/', '%', '^', '&', '|', '@', '.', ',', ';',
':', '#', '$', '?', '\'',
];
if !LEGAL_CHARS.contains(&ch) {
panic!("unsupported character `{:?}`", ch);
}
Punct(bridge::Punct { ch, joint: spacing == Spacing::Joint, span: Span::call_site().0 })
}
/// Returns the value of this punctuation character as `char`.
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
pub fn as_char(&self) -> char {
self.0.as_char()
self.0.ch
}
/// Returns the spacing of this punctuation character, indicating whether it's immediately
@ -973,28 +980,19 @@ impl Punct {
/// (`Alone`) so the operator has certainly ended.
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
pub fn spacing(&self) -> Spacing {
self.0.spacing()
if self.0.joint { Spacing::Joint } else { Spacing::Alone }
}
/// Returns the span for this punctuation character.
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
pub fn span(&self) -> Span {
Span(self.0.span())
Span(self.0.span)
}
/// Configure the span for this punctuation character.
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
pub fn set_span(&mut self, span: Span) {
self.0 = self.0.with_span(span.0);
}
}
// N.B., the bridge only provides `to_string`, implement `fmt::Display`
// based on it (the reverse of the usual relationship between the two).
#[stable(feature = "proc_macro_lib", since = "1.15.0")]
impl ToString for Punct {
fn to_string(&self) -> String {
TokenStream::from(TokenTree::from(self.clone())).to_string()
self.0.span = span.0;
}
}
@ -1003,7 +1001,7 @@ impl ToString for Punct {
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
impl fmt::Display for Punct {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.to_string())
write!(f, "{}", self.as_char())
}
}