Skip to content

Commit 8a119c3

Browse files
committed
Merge FreeFunctions trait into Server trait
And rename FreeFunctions struct to Methods.
1 parent 9481890 commit 8a119c3

9 files changed

Lines changed: 112 additions & 137 deletions

File tree

compiler/rustc_expand/src/proc_macro_server.rs

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -431,8 +431,6 @@ impl ToInternal<rustc_errors::Level> for Level {
431431
}
432432
}
433433

434-
pub(crate) struct FreeFunctions;
435-
436434
pub(crate) struct Rustc<'a, 'b> {
437435
ecx: &'a mut ExtCtxt<'b>,
438436
def_site: Span,
@@ -461,13 +459,28 @@ impl<'a, 'b> Rustc<'a, 'b> {
461459
}
462460

463461
impl server::Types for Rustc<'_, '_> {
464-
type FreeFunctions = FreeFunctions;
465462
type TokenStream = TokenStream;
466463
type Span = Span;
467464
type Symbol = Symbol;
468465
}
469466

470-
impl server::FreeFunctions for Rustc<'_, '_> {
467+
impl server::Server for Rustc<'_, '_> {
468+
fn globals(&mut self) -> ExpnGlobals<Self::Span> {
469+
ExpnGlobals {
470+
def_site: self.def_site,
471+
call_site: self.call_site,
472+
mixed_site: self.mixed_site,
473+
}
474+
}
475+
476+
fn intern_symbol(string: &str) -> Self::Symbol {
477+
Symbol::intern(string)
478+
}
479+
480+
fn with_symbol_string(symbol: &Self::Symbol, f: impl FnOnce(&str)) {
481+
f(symbol.as_str())
482+
}
483+
471484
fn injected_env_var(&mut self, var: &str) -> Option<String> {
472485
self.ecx.sess.opts.logical_env.get(var).cloned()
473486
}
@@ -843,21 +856,3 @@ impl server::FreeFunctions for Rustc<'_, '_> {
843856
if rustc_lexer::is_ident(sym.as_str()) { Ok(sym) } else { Err(()) }
844857
}
845858
}
846-
847-
impl server::Server for Rustc<'_, '_> {
848-
fn globals(&mut self) -> ExpnGlobals<Self::Span> {
849-
ExpnGlobals {
850-
def_site: self.def_site,
851-
call_site: self.call_site,
852-
mixed_site: self.mixed_site,
853-
}
854-
}
855-
856-
fn intern_symbol(string: &str) -> Self::Symbol {
857-
Symbol::intern(string)
858-
}
859-
860-
fn with_symbol_string(symbol: &Self::Symbol, f: impl FnOnce(&str)) {
861-
f(symbol.as_str())
862-
}
863-
}

library/proc_macro/src/bridge/client.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ impl !Sync for TokenStream {}
2525
// Forward `Drop::drop` to the inherent `drop` method.
2626
impl Drop for TokenStream {
2727
fn drop(&mut self) {
28-
FreeFunctions::tt_drop(TokenStream { handle: self.handle });
28+
Methods::tt_drop(TokenStream { handle: self.handle });
2929
}
3030
}
3131

@@ -75,7 +75,7 @@ impl<S> Decode<'_, '_, S> for Span {
7575

7676
impl Clone for TokenStream {
7777
fn clone(&self) -> Self {
78-
FreeFunctions::tt_clone(self)
78+
Methods::tt_clone(self)
7979
}
8080
}
8181

@@ -95,21 +95,21 @@ impl Span {
9595

9696
impl fmt::Debug for Span {
9797
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
98-
f.write_str(&FreeFunctions::span_debug(*self))
98+
f.write_str(&Methods::span_debug(*self))
9999
}
100100
}
101101

102-
pub(crate) use super::FreeFunctions;
102+
pub(crate) use super::Methods;
103103
pub(crate) use super::symbol::Symbol;
104104

105105
macro_rules! define_client_side {
106106
(
107-
FreeFunctions {
107+
Methods {
108108
$(fn $method:ident($($arg:ident: $arg_ty:ty),* $(,)?) $(-> $ret_ty:ty)*;)*
109109
},
110110
$($name:ident),* $(,)?
111111
) => {
112-
impl FreeFunctions {
112+
impl Methods {
113113
$(pub(crate) fn $method($($arg: $arg_ty),*) $(-> $ret_ty)? {
114114
Bridge::with(|bridge| {
115115
let mut buf = bridge.cached_buffer.take();

library/proc_macro/src/bridge/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use crate::{Delimiter, Level, Spacing};
2121
/// `with_api!(MySelf, my_self, my_macro)` expands to:
2222
/// ```rust,ignore (pseudo-code)
2323
/// my_macro! {
24-
/// FreeFunctions {
24+
/// Methods {
2525
/// // ...
2626
/// fn lit_character(ch: char) -> MySelf::Literal;
2727
/// // ...
@@ -49,7 +49,7 @@ use crate::{Delimiter, Level, Spacing};
4949
macro_rules! with_api {
5050
($S:ident, $self:ident, $m:ident) => {
5151
$m! {
52-
FreeFunctions {
52+
Methods {
5353
fn injected_env_var(var: &str) -> Option<String>;
5454
fn track_env_var(var: &str, value: Option<&str>);
5555
fn track_path(path: &str);
@@ -103,7 +103,7 @@ macro_rules! with_api {
103103
};
104104
}
105105

106-
pub(crate) struct FreeFunctions;
106+
pub(crate) struct Methods;
107107

108108
#[allow(unsafe_code)]
109109
mod arena;
@@ -158,7 +158,7 @@ mod api_tags {
158158

159159
macro_rules! declare_tags {
160160
(
161-
FreeFunctions {
161+
Methods {
162162
$(fn $method:ident($($arg:ident: $arg_ty:ty),* $(,)?) $(-> $ret_ty:ty)*;)*
163163
},
164164
$($name:ident),* $(,)?

library/proc_macro/src/bridge/server.rs

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -54,65 +54,59 @@ impl<S: Types> Decode<'_, '_, HandleStore<MarkedTypes<S>>> for Marked<S::Span, c
5454
}
5555

5656
pub trait Types {
57-
type FreeFunctions: 'static;
5857
type TokenStream: 'static + Clone;
5958
type Span: 'static + Copy + Eq + Hash;
6059
type Symbol: 'static;
6160
}
6261

6362
macro_rules! declare_server_traits {
6463
(
65-
FreeFunctions {
64+
Methods {
6665
$(fn $method:ident($($arg:ident: $arg_ty:ty),* $(,)?) $(-> $ret_ty:ty)*;)*
6766
},
6867
$($name:ident),* $(,)?
6968
) => {
70-
pub trait FreeFunctions: Types {
71-
$(fn $method(&mut self, $($arg: $arg_ty),*) $(-> $ret_ty)?;)*
72-
}
73-
74-
pub trait Server: Types + FreeFunctions {
69+
pub trait Server: Types {
7570
fn globals(&mut self) -> ExpnGlobals<Self::Span>;
7671

7772
/// Intern a symbol received from RPC
7873
fn intern_symbol(ident: &str) -> Self::Symbol;
7974

8075
/// Recover the string value of a symbol, and invoke a callback with it.
8176
fn with_symbol_string(symbol: &Self::Symbol, f: impl FnOnce(&str));
77+
78+
$(fn $method(&mut self, $($arg: $arg_ty),*) $(-> $ret_ty)?;)*
8279
}
8380
}
8481
}
8582
with_api!(Self, self_, declare_server_traits);
8683

8784
pub(super) struct MarkedTypes<S: Types>(S);
8885

89-
impl<S: Server> Server for MarkedTypes<S> {
90-
fn globals(&mut self) -> ExpnGlobals<Self::Span> {
91-
<_>::mark(Server::globals(&mut self.0))
92-
}
93-
fn intern_symbol(ident: &str) -> Self::Symbol {
94-
<_>::mark(S::intern_symbol(ident))
95-
}
96-
fn with_symbol_string(symbol: &Self::Symbol, f: impl FnOnce(&str)) {
97-
S::with_symbol_string(symbol.unmark(), f)
98-
}
99-
}
100-
10186
macro_rules! define_mark_types_impls {
10287
(
103-
FreeFunctions {
88+
Methods {
10489
$(fn $method:ident($($arg:ident: $arg_ty:ty),* $(,)?) $(-> $ret_ty:ty)*;)*
10590
},
10691
$($name:ident),* $(,)?
10792
) => {
10893
impl<S: Types> Types for MarkedTypes<S> {
109-
type FreeFunctions = Marked<S::FreeFunctions, client::FreeFunctions>;
11094
$(type $name = Marked<S::$name, client::$name>;)*
11195
}
11296

113-
impl<S: FreeFunctions> FreeFunctions for MarkedTypes<S> {
97+
impl<S: Server> Server for MarkedTypes<S> {
98+
fn globals(&mut self) -> ExpnGlobals<Self::Span> {
99+
<_>::mark(Server::globals(&mut self.0))
100+
}
101+
fn intern_symbol(ident: &str) -> Self::Symbol {
102+
<_>::mark(S::intern_symbol(ident))
103+
}
104+
fn with_symbol_string(symbol: &Self::Symbol, f: impl FnOnce(&str)) {
105+
S::with_symbol_string(symbol.unmark(), f)
106+
}
107+
114108
$(fn $method(&mut self, $($arg: $arg_ty),*) $(-> $ret_ty)? {
115-
<_>::mark(FreeFunctions::$method(&mut self.0, $($arg.unmark()),*))
109+
<_>::mark(S::$method(&mut self.0, $($arg.unmark()),*))
116110
})*
117111
}
118112
}
@@ -126,22 +120,20 @@ struct Dispatcher<S: Types> {
126120

127121
macro_rules! define_dispatcher_impl {
128122
(
129-
FreeFunctions {
123+
Methods {
130124
$(fn $method:ident($($arg:ident: $arg_ty:ty),* $(,)?) $(-> $ret_ty:ty)*;)*
131125
},
132126
$($name:ident),* $(,)?
133127
) => {
134128
// FIXME(eddyb) `pub` only for `ExecutionStrategy` below.
135129
pub trait DispatcherTrait {
136130
// HACK(eddyb) these are here to allow `Self::$name` to work below.
137-
type FreeFunctions;
138131
$(type $name;)*
139132

140133
fn dispatch(&mut self, buf: Buffer) -> Buffer;
141134
}
142135

143136
impl<S: Server> DispatcherTrait for Dispatcher<MarkedTypes<S>> {
144-
type FreeFunctions = <MarkedTypes<S> as Types>::FreeFunctions;
145137
$(type $name = <MarkedTypes<S> as Types>::$name;)*
146138

147139
fn dispatch(&mut self, mut buf: Buffer) -> Buffer {
@@ -152,7 +144,7 @@ macro_rules! define_dispatcher_impl {
152144
$(api_tags::Method::$method => {
153145
let mut call_method = || {
154146
$(let $arg = <$arg_ty>::decode(&mut reader, handle_store);)*
155-
FreeFunctions::$method(server, $($arg),*)
147+
server.$method($($arg),*)
156148
};
157149
// HACK(eddyb) don't use `panic::catch_unwind` in a panic.
158150
// If client and server happen to use the same `std`,

library/proc_macro/src/bridge/symbol.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ impl Symbol {
4646
if string.is_ascii() {
4747
Err(())
4848
} else {
49-
client::FreeFunctions::symbol_normalize_and_validate_ident(string)
49+
client::Methods::symbol_normalize_and_validate_ident(string)
5050
}
5151
.unwrap_or_else(|_| panic!("`{:?}` is not a valid identifier", string))
5252
}

library/proc_macro/src/diagnostic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,6 @@ impl Diagnostic {
170170
}
171171
}
172172

173-
crate::bridge::client::FreeFunctions::emit_diagnostic(to_internal(self));
173+
crate::bridge::client::Methods::emit_diagnostic(to_internal(self));
174174
}
175175
}

0 commit comments

Comments
 (0)