Skip to content

Commit 9481890

Browse files
committed
Various simplifications after moving all bridge methods to a single type
1 parent 2f44019 commit 9481890

6 files changed

Lines changed: 60 additions & 75 deletions

File tree

compiler/rustc_expand/src/proc_macro_server.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -844,12 +844,6 @@ impl server::FreeFunctions for Rustc<'_, '_> {
844844
}
845845
}
846846

847-
impl server::TokenStream for Rustc<'_, '_> {}
848-
849-
impl server::Span for Rustc<'_, '_> {}
850-
851-
impl server::Symbol for Rustc<'_, '_> {}
852-
853847
impl server::Server for Rustc<'_, '_> {
854848
fn globals(&mut self) -> ExpnGlobals<Self::Span> {
855849
ExpnGlobals {

library/proc_macro/src/bridge/client.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,16 +103,19 @@ pub(crate) use super::FreeFunctions;
103103
pub(crate) use super::symbol::Symbol;
104104

105105
macro_rules! define_client_side {
106-
($($name:ident {
107-
$(fn $method:ident($($arg:ident: $arg_ty:ty),* $(,)?) $(-> $ret_ty:ty)?;)*
108-
}),* $(,)?) => {
109-
$(impl $name {
106+
(
107+
FreeFunctions {
108+
$(fn $method:ident($($arg:ident: $arg_ty:ty),* $(,)?) $(-> $ret_ty:ty)*;)*
109+
},
110+
$($name:ident),* $(,)?
111+
) => {
112+
impl FreeFunctions {
110113
$(pub(crate) fn $method($($arg: $arg_ty),*) $(-> $ret_ty)? {
111114
Bridge::with(|bridge| {
112115
let mut buf = bridge.cached_buffer.take();
113116

114117
buf.clear();
115-
api_tags::Method::$name(api_tags::$name::$method).encode(&mut buf, &mut ());
118+
api_tags::Method::$method.encode(&mut buf, &mut ());
116119
$($arg.encode(&mut buf, &mut ());)*
117120

118121
buf = bridge.dispatch.call(buf);
@@ -124,7 +127,7 @@ macro_rules! define_client_side {
124127
r.unwrap_or_else(|e| panic::resume_unwind(e.into()))
125128
})
126129
})*
127-
})*
130+
}
128131
}
129132
}
130133
with_api!(self, self, define_client_side);

library/proc_macro/src/bridge/mod.rs

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,15 @@ use crate::{Delimiter, Level, Spacing};
2121
/// `with_api!(MySelf, my_self, my_macro)` expands to:
2222
/// ```rust,ignore (pseudo-code)
2323
/// my_macro! {
24-
/// // ...
25-
/// Literal {
24+
/// FreeFunctions {
2625
/// // ...
27-
/// fn character(ch: char) -> MySelf::Literal;
26+
/// fn lit_character(ch: char) -> MySelf::Literal;
2827
/// // ...
29-
/// fn span(my_self: &MySelf::Literal) -> MySelf::Span;
30-
/// fn set_span(my_self: &mut MySelf::Literal, span: MySelf::Span);
28+
/// fn lit_span(my_self: &MySelf::Literal) -> MySelf::Span;
29+
/// fn lit_set_span(my_self: &mut MySelf::Literal, span: MySelf::Span);
3130
/// },
31+
/// Literal,
32+
/// Span,
3233
/// // ...
3334
/// }
3435
/// ```
@@ -95,9 +96,9 @@ macro_rules! with_api {
9596

9697
fn symbol_normalize_and_validate_ident(string: &str) -> Result<$S::Symbol, ()>;
9798
},
98-
TokenStream {},
99-
Span {},
100-
Symbol {},
99+
TokenStream,
100+
Span,
101+
Symbol,
101102
}
102103
};
103104
}
@@ -160,18 +161,12 @@ mod api_tags {
160161
FreeFunctions {
161162
$(fn $method:ident($($arg:ident: $arg_ty:ty),* $(,)?) $(-> $ret_ty:ty)*;)*
162163
},
163-
$($name:ident { $($x:tt)* }),* $(,)?
164+
$($name:ident),* $(,)?
164165
) => {
165-
pub(super) enum FreeFunctions {
166-
$($method),*
167-
}
168-
rpc_encode_decode!(enum FreeFunctions { $($method),* });
169-
170-
171166
pub(super) enum Method {
172-
FreeFunctions(FreeFunctions),
167+
$($method),*
173168
}
174-
rpc_encode_decode!(enum Method { FreeFunctions(m) });
169+
rpc_encode_decode!(enum Method { $($method),* });
175170
}
176171
}
177172
with_api!(self, self, declare_tags);

library/proc_macro/src/bridge/server.rs

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,17 @@ pub trait Types {
6161
}
6262

6363
macro_rules! declare_server_traits {
64-
($($name:ident {
65-
$(fn $method:ident($($arg:ident: $arg_ty:ty),* $(,)?) $(-> $ret_ty:ty)?;)*
66-
}),* $(,)?) => {
67-
$(pub trait $name: Types {
64+
(
65+
FreeFunctions {
66+
$(fn $method:ident($($arg:ident: $arg_ty:ty),* $(,)?) $(-> $ret_ty:ty)*;)*
67+
},
68+
$($name:ident),* $(,)?
69+
) => {
70+
pub trait FreeFunctions: Types {
6871
$(fn $method(&mut self, $($arg: $arg_ty),*) $(-> $ret_ty)?;)*
69-
})*
72+
}
7073

71-
pub trait Server: Types $(+ $name)* {
74+
pub trait Server: Types + FreeFunctions {
7275
fn globals(&mut self) -> ExpnGlobals<Self::Span>;
7376

7477
/// Intern a symbol received from RPC
@@ -96,18 +99,22 @@ impl<S: Server> Server for MarkedTypes<S> {
9699
}
97100

98101
macro_rules! define_mark_types_impls {
99-
($($name:ident {
100-
$(fn $method:ident($($arg:ident: $arg_ty:ty),* $(,)?) $(-> $ret_ty:ty)?;)*
101-
}),* $(,)?) => {
102+
(
103+
FreeFunctions {
104+
$(fn $method:ident($($arg:ident: $arg_ty:ty),* $(,)?) $(-> $ret_ty:ty)*;)*
105+
},
106+
$($name:ident),* $(,)?
107+
) => {
102108
impl<S: Types> Types for MarkedTypes<S> {
109+
type FreeFunctions = Marked<S::FreeFunctions, client::FreeFunctions>;
103110
$(type $name = Marked<S::$name, client::$name>;)*
104111
}
105112

106-
$(impl<S: $name> $name for MarkedTypes<S> {
113+
impl<S: FreeFunctions> FreeFunctions for MarkedTypes<S> {
107114
$(fn $method(&mut self, $($arg: $arg_ty),*) $(-> $ret_ty)? {
108-
<_>::mark($name::$method(&mut self.0, $($arg.unmark()),*))
115+
<_>::mark(FreeFunctions::$method(&mut self.0, $($arg.unmark()),*))
109116
})*
110-
})*
117+
}
111118
}
112119
}
113120
with_api!(Self, self_, define_mark_types_impls);
@@ -122,7 +129,7 @@ macro_rules! define_dispatcher_impl {
122129
FreeFunctions {
123130
$(fn $method:ident($($arg:ident: $arg_ty:ty),* $(,)?) $(-> $ret_ty:ty)*;)*
124131
},
125-
$($name:ident { $($x:tt)* }),* $(,)?
132+
$($name:ident),* $(,)?
126133
) => {
127134
// FIXME(eddyb) `pub` only for `ExecutionStrategy` below.
128135
pub trait DispatcherTrait {
@@ -142,27 +149,25 @@ macro_rules! define_dispatcher_impl {
142149

143150
let mut reader = &buf[..];
144151
match api_tags::Method::decode(&mut reader, &mut ()) {
145-
api_tags::Method::FreeFunctions(m) => match m {
146-
$(api_tags::FreeFunctions::$method => {
147-
let mut call_method = || {
148-
$(let $arg = <$arg_ty>::decode(&mut reader, handle_store);)*
149-
FreeFunctions::$method(server, $($arg),*)
150-
};
151-
// HACK(eddyb) don't use `panic::catch_unwind` in a panic.
152-
// If client and server happen to use the same `std`,
153-
// `catch_unwind` asserts that the panic counter was 0,
154-
// even when the closure passed to it didn't panic.
155-
let r = if thread::panicking() {
156-
Ok(call_method())
157-
} else {
158-
panic::catch_unwind(panic::AssertUnwindSafe(call_method))
159-
.map_err(PanicMessage::from)
160-
};
161-
162-
buf.clear();
163-
r.encode(&mut buf, handle_store);
164-
})*
165-
}
152+
$(api_tags::Method::$method => {
153+
let mut call_method = || {
154+
$(let $arg = <$arg_ty>::decode(&mut reader, handle_store);)*
155+
FreeFunctions::$method(server, $($arg),*)
156+
};
157+
// HACK(eddyb) don't use `panic::catch_unwind` in a panic.
158+
// If client and server happen to use the same `std`,
159+
// `catch_unwind` asserts that the panic counter was 0,
160+
// even when the closure passed to it didn't panic.
161+
let r = if thread::panicking() {
162+
Ok(call_method())
163+
} else {
164+
panic::catch_unwind(panic::AssertUnwindSafe(call_method))
165+
.map_err(PanicMessage::from)
166+
};
167+
168+
buf.clear();
169+
r.encode(&mut buf, handle_store);
170+
})*
166171
}
167172
buf
168173
}

src/tools/rust-analyzer/crates/proc-macro-srv/src/server_impl/rust_analyzer_span.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -277,12 +277,6 @@ impl server::FreeFunctions for RaSpanServer<'_> {
277277
}
278278
}
279279

280-
impl server::TokenStream for RaSpanServer<'_> {}
281-
282-
impl server::Span for RaSpanServer<'_> {}
283-
284-
impl server::Symbol for RaSpanServer<'_> {}
285-
286280
impl server::Server for RaSpanServer<'_> {
287281
fn globals(&mut self) -> ExpnGlobals<Self::Span> {
288282
ExpnGlobals {

src/tools/rust-analyzer/crates/proc-macro-srv/src/server_impl/token_id.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -196,12 +196,6 @@ impl server::FreeFunctions for SpanIdServer<'_> {
196196
}
197197
}
198198

199-
impl server::TokenStream for SpanIdServer<'_> {}
200-
201-
impl server::Span for SpanIdServer<'_> {}
202-
203-
impl server::Symbol for SpanIdServer<'_> {}
204-
205199
impl server::Server for SpanIdServer<'_> {
206200
fn globals(&mut self) -> ExpnGlobals<Self::Span> {
207201
ExpnGlobals {

0 commit comments

Comments
 (0)