Skip to content

Commit 26f7400

Browse files
committed
proc-macro-srv: Reimplement token trees via ropes
1 parent 51d4227 commit 26f7400

File tree

13 files changed

+1208
-1073
lines changed

13 files changed

+1208
-1073
lines changed

Cargo.lock

Lines changed: 0 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/proc-macro-srv/Cargo.toml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ libloading.workspace = true
1818
memmap2.workspace = true
1919
temp-dir.workspace = true
2020

21-
tt.workspace = true
22-
syntax-bridge.workspace = true
2321
paths.workspace = true
2422
# span = {workspace = true, default-features = false} does not work
2523
span = { path = "../span", version = "0.0.0", default-features = false}
@@ -38,8 +36,9 @@ expect-test.workspace = true
3836
proc-macro-test.path = "./proc-macro-test"
3937

4038
[features]
39+
default = ["sysroot-abi"]
4140
sysroot-abi = []
42-
in-rust-tree = ["syntax-bridge/in-rust-tree", "tt/in-rust-tree", "sysroot-abi"]
41+
in-rust-tree = ["sysroot-abi"]
4342

4443
[lints]
4544
workspace = true

crates/proc-macro-srv/src/dylib.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ use object::Object;
1212
use paths::{Utf8Path, Utf8PathBuf};
1313

1414
use crate::{
15-
PanicMessage, ProcMacroKind, ProcMacroSrvSpan, dylib::proc_macros::ProcMacros,
16-
server_impl::TopSubtree,
15+
PanicMessage, ProcMacroKind, ProcMacroSrvSpan, dylib::proc_macros::ProcMacros, tt::TokenStream,
1716
};
1817

1918
pub(crate) struct Expander {
@@ -40,18 +39,18 @@ impl Expander {
4039
pub(crate) fn expand<S: ProcMacroSrvSpan>(
4140
&self,
4241
macro_name: &str,
43-
macro_body: TopSubtree<S>,
44-
attributes: Option<TopSubtree<S>>,
42+
macro_body: TokenStream<S>,
43+
attribute: Option<TokenStream<S>>,
4544
def_site: S,
4645
call_site: S,
4746
mixed_site: S,
48-
) -> Result<TopSubtree<S>, PanicMessage>
47+
) -> Result<TokenStream<S>, PanicMessage>
4948
where
5049
<S::Server as bridge::server::Types>::TokenStream: Default,
5150
{
5251
self.inner
5352
.proc_macros
54-
.expand(macro_name, macro_body, attributes, def_site, call_site, mixed_site)
53+
.expand(macro_name, macro_body, attribute, def_site, call_site, mixed_site)
5554
}
5655

5756
pub(crate) fn list_macros(&self) -> impl Iterator<Item = (&str, ProcMacroKind)> {

crates/proc-macro-srv/src/dylib/proc_macros.rs

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use proc_macro::bridge;
44

5-
use crate::{ProcMacroKind, ProcMacroSrvSpan, server_impl::TopSubtree};
5+
use crate::{ProcMacroKind, ProcMacroSrvSpan, tt::TokenStream};
66

77
#[repr(transparent)]
88
pub(crate) struct ProcMacros([bridge::client::ProcMacro]);
@@ -17,18 +17,13 @@ impl ProcMacros {
1717
pub(crate) fn expand<S: ProcMacroSrvSpan>(
1818
&self,
1919
macro_name: &str,
20-
macro_body: TopSubtree<S>,
21-
attributes: Option<TopSubtree<S>>,
20+
macro_body: TokenStream<S>,
21+
attribute: Option<TokenStream<S>>,
2222
def_site: S,
2323
call_site: S,
2424
mixed_site: S,
25-
) -> Result<TopSubtree<S>, crate::PanicMessage> {
26-
let parsed_body = crate::server_impl::TokenStream::with_subtree(macro_body);
27-
28-
let parsed_attributes = attributes
29-
.map_or_else(crate::server_impl::TokenStream::default, |attr| {
30-
crate::server_impl::TokenStream::with_subtree(attr)
31-
});
25+
) -> Result<TokenStream<S>, crate::PanicMessage> {
26+
let parsed_attributes = attribute.unwrap_or_default();
3227

3328
for proc_macro in &self.0 {
3429
match proc_macro {
@@ -38,35 +33,29 @@ impl ProcMacros {
3833
let res = client.run(
3934
&bridge::server::SameThread,
4035
S::make_server(call_site, def_site, mixed_site),
41-
parsed_body,
36+
macro_body,
4237
cfg!(debug_assertions),
4338
);
44-
return res
45-
.map(|it| it.into_subtree(call_site))
46-
.map_err(crate::PanicMessage::from);
39+
return res.map_err(crate::PanicMessage::from);
4740
}
4841
bridge::client::ProcMacro::Bang { name, client } if *name == macro_name => {
4942
let res = client.run(
5043
&bridge::server::SameThread,
5144
S::make_server(call_site, def_site, mixed_site),
52-
parsed_body,
45+
macro_body,
5346
cfg!(debug_assertions),
5447
);
55-
return res
56-
.map(|it| it.into_subtree(call_site))
57-
.map_err(crate::PanicMessage::from);
48+
return res.map_err(crate::PanicMessage::from);
5849
}
5950
bridge::client::ProcMacro::Attr { name, client } if *name == macro_name => {
6051
let res = client.run(
6152
&bridge::server::SameThread,
6253
S::make_server(call_site, def_site, mixed_site),
6354
parsed_attributes,
64-
parsed_body,
55+
macro_body,
6556
cfg!(debug_assertions),
6657
);
67-
return res
68-
.map(|it| it.into_subtree(call_site))
69-
.map_err(crate::PanicMessage::from);
58+
return res.map_err(crate::PanicMessage::from);
7059
}
7160
_ => continue,
7261
}

crates/proc-macro-srv/src/lib.rs

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ extern crate rustc_lexer;
2828

2929
mod dylib;
3030
mod server_impl;
31+
mod tt;
3132

3233
use std::{
3334
collections::{HashMap, hash_map::Entry},
@@ -43,8 +44,6 @@ use paths::{Utf8Path, Utf8PathBuf};
4344
use span::Span;
4445
use temp_dir::TempDir;
4546

46-
use crate::server_impl::TokenStream;
47-
4847
pub use crate::server_impl::token_id::SpanId;
4948

5049
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
@@ -81,12 +80,12 @@ impl ProcMacroSrv<'_> {
8180
env: &[(String, String)],
8281
current_dir: Option<impl AsRef<Path>>,
8382
macro_name: &str,
84-
macro_body: tt::TopSubtree<S>,
85-
attribute: Option<tt::TopSubtree<S>>,
83+
macro_body: tt::TokenStream<S>,
84+
attribute: Option<tt::TokenStream<S>>,
8685
def_site: S,
8786
call_site: S,
8887
mixed_site: S,
89-
) -> Result<Vec<tt::TokenTree<S>>, PanicMessage> {
88+
) -> Result<tt::TokenStream<S>, PanicMessage> {
9089
let snapped_env = self.env;
9190
let expander = self.expander(lib.as_ref()).map_err(|err| PanicMessage {
9291
message: Some(format!("failed to load macro: {err}")),
@@ -102,15 +101,7 @@ impl ProcMacroSrv<'_> {
102101
.name(macro_name.to_owned())
103102
.spawn_scoped(s, move || {
104103
expander
105-
.expand(
106-
macro_name,
107-
server_impl::TopSubtree(macro_body.0.into_vec()),
108-
attribute.map(|it| server_impl::TopSubtree(it.0.into_vec())),
109-
def_site,
110-
call_site,
111-
mixed_site,
112-
)
113-
.map(|tt| tt.0)
104+
.expand(macro_name, macro_body, attribute, def_site, call_site, mixed_site)
114105
});
115106
match thread.unwrap().join() {
116107
Ok(res) => res,
@@ -157,8 +148,8 @@ impl ProcMacroSrv<'_> {
157148
}
158149
}
159150

160-
pub trait ProcMacroSrvSpan: Copy + Send {
161-
type Server: proc_macro::bridge::server::Server<TokenStream = TokenStream<Self>>;
151+
pub trait ProcMacroSrvSpan: Copy + Send + Sync {
152+
type Server: proc_macro::bridge::server::Server<TokenStream = crate::tt::TokenStream<Self>>;
162153
fn make_server(call_site: Self, def_site: Self, mixed_site: Self) -> Self::Server;
163154
}
164155

0 commit comments

Comments
 (0)