diff --git a/compiler/rustc_expand/src/proc_macro_server.rs b/compiler/rustc_expand/src/proc_macro_server.rs index 4f8ff5588581..1bf2edafd7fd 100644 --- a/compiler/rustc_expand/src/proc_macro_server.rs +++ b/compiler/rustc_expand/src/proc_macro_server.rs @@ -844,12 +844,6 @@ impl server::FreeFunctions for Rustc<'_, '_> { } } -impl server::TokenStream for Rustc<'_, '_> {} - -impl server::Span for Rustc<'_, '_> {} - -impl server::Symbol for Rustc<'_, '_> {} - impl server::Server for Rustc<'_, '_> { fn globals(&mut self) -> ExpnGlobals { ExpnGlobals { diff --git a/library/proc_macro/src/bridge/client.rs b/library/proc_macro/src/bridge/client.rs index d954177e7df5..ffbf6767833f 100644 --- a/library/proc_macro/src/bridge/client.rs +++ b/library/proc_macro/src/bridge/client.rs @@ -103,16 +103,19 @@ pub(crate) use super::FreeFunctions; pub(crate) use super::symbol::Symbol; macro_rules! define_client_side { - ($($name:ident { - $(fn $method:ident($($arg:ident: $arg_ty:ty),* $(,)?) $(-> $ret_ty:ty)?;)* - }),* $(,)?) => { - $(impl $name { + ( + FreeFunctions { + $(fn $method:ident($($arg:ident: $arg_ty:ty),* $(,)?) $(-> $ret_ty:ty)*;)* + }, + $($name:ident),* $(,)? + ) => { + impl FreeFunctions { $(pub(crate) fn $method($($arg: $arg_ty),*) $(-> $ret_ty)? { Bridge::with(|bridge| { let mut buf = bridge.cached_buffer.take(); buf.clear(); - api_tags::Method::$name(api_tags::$name::$method).encode(&mut buf, &mut ()); + api_tags::Method::$method.encode(&mut buf, &mut ()); $($arg.encode(&mut buf, &mut ());)* buf = bridge.dispatch.call(buf); @@ -124,7 +127,7 @@ macro_rules! define_client_side { r.unwrap_or_else(|e| panic::resume_unwind(e.into())) }) })* - })* + } } } with_api!(self, self, define_client_side); diff --git a/library/proc_macro/src/bridge/mod.rs b/library/proc_macro/src/bridge/mod.rs index 027f94c36ff2..ae0f5974de52 100644 --- a/library/proc_macro/src/bridge/mod.rs +++ b/library/proc_macro/src/bridge/mod.rs @@ -21,14 +21,15 @@ use crate::{Delimiter, Level, Spacing}; /// `with_api!(MySelf, my_self, my_macro)` expands to: /// ```rust,ignore (pseudo-code) /// my_macro! { -/// // ... -/// Literal { +/// FreeFunctions { /// // ... -/// fn character(ch: char) -> MySelf::Literal; +/// fn lit_character(ch: char) -> MySelf::Literal; /// // ... -/// fn span(my_self: &MySelf::Literal) -> MySelf::Span; -/// fn set_span(my_self: &mut MySelf::Literal, span: MySelf::Span); +/// fn lit_span(my_self: &MySelf::Literal) -> MySelf::Span; +/// fn lit_set_span(my_self: &mut MySelf::Literal, span: MySelf::Span); /// }, +/// Literal, +/// Span, /// // ... /// } /// ``` @@ -95,9 +96,9 @@ macro_rules! with_api { fn symbol_normalize_and_validate_ident(string: &str) -> Result<$S::Symbol, ()>; }, - TokenStream {}, - Span {}, - Symbol {}, + TokenStream, + Span, + Symbol, } }; } @@ -160,18 +161,12 @@ mod api_tags { FreeFunctions { $(fn $method:ident($($arg:ident: $arg_ty:ty),* $(,)?) $(-> $ret_ty:ty)*;)* }, - $($name:ident { $($x:tt)* }),* $(,)? + $($name:ident),* $(,)? ) => { - pub(super) enum FreeFunctions { + pub(super) enum Method { $($method),* } - rpc_encode_decode!(enum FreeFunctions { $($method),* }); - - - pub(super) enum Method { - FreeFunctions(FreeFunctions), - } - rpc_encode_decode!(enum Method { FreeFunctions(m) }); + rpc_encode_decode!(enum Method { $($method),* }); } } with_api!(self, self, declare_tags); diff --git a/library/proc_macro/src/bridge/server.rs b/library/proc_macro/src/bridge/server.rs index 16037ed46f06..7701ba6899f6 100644 --- a/library/proc_macro/src/bridge/server.rs +++ b/library/proc_macro/src/bridge/server.rs @@ -61,14 +61,17 @@ pub trait Types { } macro_rules! declare_server_traits { - ($($name:ident { - $(fn $method:ident($($arg:ident: $arg_ty:ty),* $(,)?) $(-> $ret_ty:ty)?;)* - }),* $(,)?) => { - $(pub trait $name: Types { + ( + FreeFunctions { + $(fn $method:ident($($arg:ident: $arg_ty:ty),* $(,)?) $(-> $ret_ty:ty)*;)* + }, + $($name:ident),* $(,)? + ) => { + pub trait FreeFunctions: Types { $(fn $method(&mut self, $($arg: $arg_ty),*) $(-> $ret_ty)?;)* - })* + } - pub trait Server: Types $(+ $name)* { + pub trait Server: Types + FreeFunctions { fn globals(&mut self) -> ExpnGlobals; /// Intern a symbol received from RPC @@ -96,18 +99,22 @@ impl Server for MarkedTypes { } macro_rules! define_mark_types_impls { - ($($name:ident { - $(fn $method:ident($($arg:ident: $arg_ty:ty),* $(,)?) $(-> $ret_ty:ty)?;)* - }),* $(,)?) => { + ( + FreeFunctions { + $(fn $method:ident($($arg:ident: $arg_ty:ty),* $(,)?) $(-> $ret_ty:ty)*;)* + }, + $($name:ident),* $(,)? + ) => { impl Types for MarkedTypes { + type FreeFunctions = Marked; $(type $name = Marked;)* } - $(impl $name for MarkedTypes { + impl FreeFunctions for MarkedTypes { $(fn $method(&mut self, $($arg: $arg_ty),*) $(-> $ret_ty)? { - <_>::mark($name::$method(&mut self.0, $($arg.unmark()),*)) + <_>::mark(FreeFunctions::$method(&mut self.0, $($arg.unmark()),*)) })* - })* + } } } with_api!(Self, self_, define_mark_types_impls); @@ -122,7 +129,7 @@ macro_rules! define_dispatcher_impl { FreeFunctions { $(fn $method:ident($($arg:ident: $arg_ty:ty),* $(,)?) $(-> $ret_ty:ty)*;)* }, - $($name:ident { $($x:tt)* }),* $(,)? + $($name:ident),* $(,)? ) => { // FIXME(eddyb) `pub` only for `ExecutionStrategy` below. pub trait DispatcherTrait { @@ -142,27 +149,25 @@ macro_rules! define_dispatcher_impl { let mut reader = &buf[..]; match api_tags::Method::decode(&mut reader, &mut ()) { - api_tags::Method::FreeFunctions(m) => match m { - $(api_tags::FreeFunctions::$method => { - let mut call_method = || { - $(let $arg = <$arg_ty>::decode(&mut reader, handle_store);)* - FreeFunctions::$method(server, $($arg),*) - }; - // HACK(eddyb) don't use `panic::catch_unwind` in a panic. - // If client and server happen to use the same `std`, - // `catch_unwind` asserts that the panic counter was 0, - // even when the closure passed to it didn't panic. - let r = if thread::panicking() { - Ok(call_method()) - } else { - panic::catch_unwind(panic::AssertUnwindSafe(call_method)) - .map_err(PanicMessage::from) - }; + $(api_tags::Method::$method => { + let mut call_method = || { + $(let $arg = <$arg_ty>::decode(&mut reader, handle_store);)* + FreeFunctions::$method(server, $($arg),*) + }; + // HACK(eddyb) don't use `panic::catch_unwind` in a panic. + // If client and server happen to use the same `std`, + // `catch_unwind` asserts that the panic counter was 0, + // even when the closure passed to it didn't panic. + let r = if thread::panicking() { + Ok(call_method()) + } else { + panic::catch_unwind(panic::AssertUnwindSafe(call_method)) + .map_err(PanicMessage::from) + }; - buf.clear(); - r.encode(&mut buf, handle_store); - })* - } + buf.clear(); + r.encode(&mut buf, handle_store); + })* } buf } diff --git a/src/tools/rust-analyzer/crates/proc-macro-srv/src/server_impl/rust_analyzer_span.rs b/src/tools/rust-analyzer/crates/proc-macro-srv/src/server_impl/rust_analyzer_span.rs index 46891639744d..f76a07bc5d6a 100644 --- a/src/tools/rust-analyzer/crates/proc-macro-srv/src/server_impl/rust_analyzer_span.rs +++ b/src/tools/rust-analyzer/crates/proc-macro-srv/src/server_impl/rust_analyzer_span.rs @@ -277,12 +277,6 @@ impl server::FreeFunctions for RaSpanServer<'_> { } } -impl server::TokenStream for RaSpanServer<'_> {} - -impl server::Span for RaSpanServer<'_> {} - -impl server::Symbol for RaSpanServer<'_> {} - impl server::Server for RaSpanServer<'_> { fn globals(&mut self) -> ExpnGlobals { ExpnGlobals { diff --git a/src/tools/rust-analyzer/crates/proc-macro-srv/src/server_impl/token_id.rs b/src/tools/rust-analyzer/crates/proc-macro-srv/src/server_impl/token_id.rs index 804f6ce614c6..b3f765026273 100644 --- a/src/tools/rust-analyzer/crates/proc-macro-srv/src/server_impl/token_id.rs +++ b/src/tools/rust-analyzer/crates/proc-macro-srv/src/server_impl/token_id.rs @@ -196,12 +196,6 @@ impl server::FreeFunctions for SpanIdServer<'_> { } } -impl server::TokenStream for SpanIdServer<'_> {} - -impl server::Span for SpanIdServer<'_> {} - -impl server::Symbol for SpanIdServer<'_> {} - impl server::Server for SpanIdServer<'_> { fn globals(&mut self) -> ExpnGlobals { ExpnGlobals {