Skip to content

Commit efd7542

Browse files
committed
proc_macro: stop using a remote object handle for Literal
This builds on the symbol infrastructure built for `Ident` to replicate the `LitKind` and `Lit` structures in rustc within the `proc_macro` client, allowing literals to be fully created and interacted with from the client thread. Only parsing and subspan operations still require sync RPC.
1 parent 6ee950d commit efd7542

File tree

4 files changed

+156
-79
lines changed

4 files changed

+156
-79
lines changed

proc_macro/src/bridge/client.rs

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,6 @@ define_handles! {
175175
'owned:
176176
FreeFunctions,
177177
TokenStream,
178-
Literal,
179178
SourceFile,
180179
MultiSpan,
181180
Diagnostic,
@@ -196,25 +195,6 @@ impl Clone for TokenStream {
196195
}
197196
}
198197

199-
impl Clone for Literal {
200-
fn clone(&self) -> Self {
201-
self.clone()
202-
}
203-
}
204-
205-
impl fmt::Debug for Literal {
206-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
207-
f.debug_struct("Literal")
208-
// format the kind without quotes, as in `kind: Float`
209-
.field("kind", &format_args!("{}", &self.debug_kind()))
210-
.field("symbol", &self.symbol())
211-
// format `Some("...")` on one line even in {:#?} mode
212-
.field("suffix", &format_args!("{:?}", &self.suffix()))
213-
.field("span", &self.span())
214-
.finish()
215-
}
216-
}
217-
218198
impl Clone for SourceFile {
219199
fn clone(&self) -> Self {
220200
self.clone()

proc_macro/src/bridge/mod.rs

Lines changed: 46 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ macro_rules! with_api {
5656
fn drop($self: $S::FreeFunctions);
5757
fn track_env_var(var: &str, value: Option<&str>);
5858
fn track_path(path: &str);
59+
fn literal_from_str(s: &str) -> Result<Literal<$S::Span, $S::Symbol>, ()>;
60+
fn literal_subspan(lit: Literal<$S::Span, $S::Symbol>, start: Bound<usize>, end: Bound<usize>) -> Option<$S::Span>;
5961
},
6062
TokenStream {
6163
fn drop($self: $S::TokenStream);
@@ -65,43 +67,19 @@ macro_rules! with_api {
6567
fn from_str(src: &str) -> $S::TokenStream;
6668
fn to_string($self: &$S::TokenStream) -> String;
6769
fn from_token_tree(
68-
tree: TokenTree<$S::TokenStream, $S::Span, $S::Symbol, $S::Literal>,
70+
tree: TokenTree<$S::TokenStream, $S::Span, $S::Symbol>,
6971
) -> $S::TokenStream;
7072
fn concat_trees(
7173
base: Option<$S::TokenStream>,
72-
trees: Vec<TokenTree<$S::TokenStream, $S::Span, $S::Symbol, $S::Literal>>,
74+
trees: Vec<TokenTree<$S::TokenStream, $S::Span, $S::Symbol>>,
7375
) -> $S::TokenStream;
7476
fn concat_streams(
7577
base: Option<$S::TokenStream>,
7678
streams: Vec<$S::TokenStream>,
7779
) -> $S::TokenStream;
7880
fn into_trees(
7981
$self: $S::TokenStream
80-
) -> Vec<TokenTree<$S::TokenStream, $S::Span, $S::Symbol, $S::Literal>>;
81-
},
82-
Literal {
83-
fn drop($self: $S::Literal);
84-
fn clone($self: &$S::Literal) -> $S::Literal;
85-
fn from_str(s: &str) -> Result<$S::Literal, ()>;
86-
fn to_string($self: &$S::Literal) -> String;
87-
fn debug_kind($self: &$S::Literal) -> String;
88-
fn symbol($self: &$S::Literal) -> String;
89-
fn suffix($self: &$S::Literal) -> Option<String>;
90-
fn integer(n: &str) -> $S::Literal;
91-
fn typed_integer(n: &str, kind: &str) -> $S::Literal;
92-
fn float(n: &str) -> $S::Literal;
93-
fn f32(n: &str) -> $S::Literal;
94-
fn f64(n: &str) -> $S::Literal;
95-
fn string(string: &str) -> $S::Literal;
96-
fn character(ch: char) -> $S::Literal;
97-
fn byte_string(bytes: &[u8]) -> $S::Literal;
98-
fn span($self: &$S::Literal) -> $S::Span;
99-
fn set_span($self: &mut $S::Literal, span: $S::Span);
100-
fn subspan(
101-
$self: &$S::Literal,
102-
start: Bound<usize>,
103-
end: Bound<usize>,
104-
) -> Option<$S::Span>;
82+
) -> Vec<TokenTree<$S::TokenStream, $S::Span, $S::Symbol>>;
10583
},
10684
SourceFile {
10785
fn drop($self: $S::SourceFile);
@@ -332,6 +310,7 @@ mark_noop! {
332310
u8,
333311
usize,
334312
Delimiter,
313+
LitKind,
335314
Level,
336315
LineColumn,
337316
Spacing,
@@ -361,6 +340,33 @@ rpc_encode_decode!(
361340
}
362341
);
363342

343+
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
344+
pub enum LitKind {
345+
Byte,
346+
Char,
347+
Integer,
348+
Float,
349+
Str,
350+
StrRaw(u8),
351+
ByteStr,
352+
ByteStrRaw(u8),
353+
Err,
354+
}
355+
356+
rpc_encode_decode!(
357+
enum LitKind {
358+
Byte,
359+
Char,
360+
Integer,
361+
Float,
362+
Str,
363+
StrRaw(n),
364+
ByteStr,
365+
ByteStrRaw(n),
366+
Err,
367+
}
368+
);
369+
364370
macro_rules! mark_compound {
365371
(struct $name:ident <$($T:ident),+> { $($field:ident),* $(,)? }) => {
366372
impl<$($T: Mark),+> Mark for $name <$($T),+> {
@@ -477,16 +483,26 @@ pub struct Ident<Span, Symbol> {
477483

478484
compound_traits!(struct Ident<Span, Symbol> { sym, is_raw, span });
479485

486+
#[derive(Clone, Eq, PartialEq)]
487+
pub struct Literal<Span, Symbol> {
488+
pub kind: LitKind,
489+
pub symbol: Symbol,
490+
pub suffix: Option<Symbol>,
491+
pub span: Span,
492+
}
493+
494+
compound_traits!(struct Literal<Sp, Sy> { kind, symbol, suffix, span });
495+
480496
#[derive(Clone)]
481-
pub enum TokenTree<TokenStream, Span, Symbol, Literal> {
497+
pub enum TokenTree<TokenStream, Span, Symbol> {
482498
Group(Group<TokenStream, Span>),
483499
Punct(Punct<Span>),
484500
Ident(Ident<Span, Symbol>),
485-
Literal(Literal),
501+
Literal(Literal<Span, Symbol>),
486502
}
487503

488504
compound_traits!(
489-
enum TokenTree<TokenStream, Span, Symbol, Literal> {
505+
enum TokenTree<TokenStream, Span, Symbol> {
490506
Group(tt),
491507
Punct(tt),
492508
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 Literal: 'static + Clone;
1211
type SourceFile: 'static + Clone;
1312
type MultiSpan: 'static;
1413
type Diagnostic: 'static;

0 commit comments

Comments
 (0)