Skip to content

Commit f601b29

Browse files
committed
inline SameThread and CrossThread
1 parent 3105348 commit f601b29

File tree

3 files changed

+40
-64
lines changed

3 files changed

+40
-64
lines changed

compiler/rustc_expand/src/proc_macro.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ use crate::base::{self, *};
1212
use crate::{errors, proc_macro_server};
1313

1414
fn exec_strategy(sess: &Session) -> impl pm::bridge::server::ExecutionStrategy + 'static {
15-
pm::bridge::server::MaybeCrossThread::new(
16-
sess.opts.unstable_opts.proc_macro_execution_strategy
15+
pm::bridge::server::MaybeCrossThread {
16+
cross_thread: sess.opts.unstable_opts.proc_macro_execution_strategy
1717
== ProcMacroExecutionStrategy::CrossThread,
18-
)
18+
}
1919
}
2020

2121
pub struct BangProcMacro {

library/proc_macro/src/bridge/server.rs

Lines changed: 34 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,13 @@ macro_rules! define_dispatcher {
121121
}
122122
with_api!(define_dispatcher, MarkedTokenStream<S>, MarkedSpan<S>, MarkedSymbol<S>);
123123

124+
// This trait is currently only implemented and used once, inside of this crate.
125+
// We keep it public to allow implementing more complex execution strategies in
126+
// the future, such as wasm proc-macros.
124127
pub trait ExecutionStrategy {
125-
fn run_bridge_and_client<S: Server>(
128+
fn run_bridge_and_client(
126129
&self,
127-
dispatcher: &mut Dispatcher<S>,
130+
dispatcher: &mut Dispatcher<impl Server>,
128131
input: Buffer,
129132
run_client: extern "C" fn(BridgeConfig<'_>) -> Buffer,
130133
force_show_panics: bool,
@@ -164,82 +167,55 @@ impl Drop for RunningSameThreadGuard {
164167
}
165168

166169
pub struct MaybeCrossThread {
167-
cross_thread: bool,
170+
pub cross_thread: bool,
168171
}
169172

170-
impl MaybeCrossThread {
171-
pub const fn new(cross_thread: bool) -> Self {
172-
MaybeCrossThread { cross_thread }
173-
}
174-
}
173+
pub const SAME_THREAD: MaybeCrossThread = MaybeCrossThread { cross_thread: false };
174+
pub const CROSS_THREAD: MaybeCrossThread = MaybeCrossThread { cross_thread: true };
175175

176176
impl ExecutionStrategy for MaybeCrossThread {
177-
fn run_bridge_and_client<S: Server>(
177+
fn run_bridge_and_client(
178178
&self,
179-
dispatcher: &mut Dispatcher<S>,
179+
dispatcher: &mut Dispatcher<impl Server>,
180180
input: Buffer,
181181
run_client: extern "C" fn(BridgeConfig<'_>) -> Buffer,
182182
force_show_panics: bool,
183183
) -> Buffer {
184184
if self.cross_thread || ALREADY_RUNNING_SAME_THREAD.get() {
185-
CrossThread.run_bridge_and_client(dispatcher, input, run_client, force_show_panics)
186-
} else {
187-
SameThread.run_bridge_and_client(dispatcher, input, run_client, force_show_panics)
188-
}
189-
}
190-
}
191-
192-
pub struct SameThread;
193-
194-
impl ExecutionStrategy for SameThread {
195-
fn run_bridge_and_client<S: Server>(
196-
&self,
197-
dispatcher: &mut Dispatcher<S>,
198-
input: Buffer,
199-
run_client: extern "C" fn(BridgeConfig<'_>) -> Buffer,
200-
force_show_panics: bool,
201-
) -> Buffer {
202-
let _guard = RunningSameThreadGuard::new();
203-
204-
let mut dispatch = |buf| dispatcher.dispatch(buf);
205-
206-
run_client(BridgeConfig { input, dispatch: (&mut dispatch).into(), force_show_panics })
207-
}
208-
}
209-
210-
pub struct CrossThread;
185+
let (mut server, mut client) = MessagePipe::new();
186+
187+
let join_handle = thread::spawn(move || {
188+
let mut dispatch = |b: Buffer| -> Buffer {
189+
client.send(b);
190+
client.recv().expect("server died while client waiting for reply")
191+
};
192+
193+
run_client(BridgeConfig {
194+
input,
195+
dispatch: (&mut dispatch).into(),
196+
force_show_panics,
197+
})
198+
});
199+
200+
while let Some(b) = server.recv() {
201+
server.send(dispatcher.dispatch(b));
202+
}
211203

212-
impl ExecutionStrategy for CrossThread {
213-
fn run_bridge_and_client<S: Server>(
214-
&self,
215-
dispatcher: &mut Dispatcher<S>,
216-
input: Buffer,
217-
run_client: extern "C" fn(BridgeConfig<'_>) -> Buffer,
218-
force_show_panics: bool,
219-
) -> Buffer {
220-
let (mut server, mut client) = MessagePipe::new();
204+
join_handle.join().unwrap()
205+
} else {
206+
let _guard = RunningSameThreadGuard::new();
221207

222-
let join_handle = thread::spawn(move || {
223-
let mut dispatch = |b: Buffer| -> Buffer {
224-
client.send(b);
225-
client.recv().expect("server died while client waiting for reply")
226-
};
208+
let mut dispatch = |buf| dispatcher.dispatch(buf);
227209

228210
run_client(BridgeConfig { input, dispatch: (&mut dispatch).into(), force_show_panics })
229-
});
230-
231-
while let Some(b) = server.recv() {
232-
server.send(dispatcher.dispatch(b));
233211
}
234-
235-
join_handle.join().unwrap()
236212
}
237213
}
238214

239215
/// A message pipe used for communicating between server and client threads.
240216
struct MessagePipe<T> {
241-
tx: std::sync::mpsc::SyncSender<T>,
242-
rx: std::sync::mpsc::Receiver<T>,
217+
tx: mpsc::SyncSender<T>,
218+
rx: mpsc::Receiver<T>,
243219
}
244220

245221
impl<T> MessagePipe<T> {

src/tools/rust-analyzer/crates/proc-macro-srv/src/dylib/proc_macros.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ impl ProcMacros {
3030
if *trait_name == macro_name =>
3131
{
3232
let res = client.run(
33-
&bridge::server::SameThread,
33+
&bridge::server::SAME_THREAD,
3434
S::make_server(call_site, def_site, mixed_site, callback),
3535
macro_body,
3636
cfg!(debug_assertions),
@@ -39,7 +39,7 @@ impl ProcMacros {
3939
}
4040
bridge::client::ProcMacro::Bang { name, client } if *name == macro_name => {
4141
let res = client.run(
42-
&bridge::server::SameThread,
42+
&bridge::server::SAME_THREAD,
4343
S::make_server(call_site, def_site, mixed_site, callback),
4444
macro_body,
4545
cfg!(debug_assertions),
@@ -48,7 +48,7 @@ impl ProcMacros {
4848
}
4949
bridge::client::ProcMacro::Attr { name, client } if *name == macro_name => {
5050
let res = client.run(
51-
&bridge::server::SameThread,
51+
&bridge::server::SAME_THREAD,
5252
S::make_server(call_site, def_site, mixed_site, callback),
5353
parsed_attributes,
5454
macro_body,

0 commit comments

Comments
 (0)