Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proc_macro/bridge: remove client->server &HandleCounters passing. #98223

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 15 additions & 27 deletions library/proc_macro/src/bridge/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,6 @@ macro_rules! define_handles {
'owned: $($oty:ident,)*
'interned: $($ity:ident,)*
) => {
#[repr(C)]
#[allow(non_snake_case)]
pub struct HandleCounters {
$($oty: AtomicUsize,)*
$($ity: AtomicUsize,)*
}

impl HandleCounters {
// FIXME(eddyb) use a reference to the `static COUNTERS`, instead of
// a wrapper `fn` pointer, once `const fn` can reference `static`s.
extern "C" fn get() -> &'static Self {
static COUNTERS: HandleCounters = HandleCounters {
$($oty: AtomicUsize::new(1),)*
$($ity: AtomicUsize::new(1),)*
};
&COUNTERS
}
}

// FIXME(eddyb) generate the definition of `HandleStore` in `server.rs`.
#[repr(C)]
Expand All @@ -37,10 +19,22 @@ macro_rules! define_handles {
}

impl<S: server::Types> HandleStore<S> {
pub(super) fn new(handle_counters: &'static HandleCounters) -> Self {
pub(super) fn new() -> Self {
// FIXME(eddyb) these counters are server-side, so they don't
// protect against the same proc macro dylib being used with
// multiple servers - however, that's very unlikely, and should
// be protected against through other means (e.g. forcing a
// specific proc macro dylib to always talk to the same server
// that initially loaded it).
Comment on lines +23 to +28
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps mention "the same dylib" or something like that? This could be interpreted as "the proc_macro_server::Rustc object should be the same for every invocation of a macro from each crate", which I don't think is what you're suggesting (as ideally I'd imagine that we we want to throw out HandleStore after each invocation of a proc macro, and only keep around the handle counter to avoid conflicts, which is done here by using a static for the counter).

HandleStore {
$($oty: handle::OwnedStore::new(&handle_counters.$oty),)*
$($ity: handle::InternedStore::new(&handle_counters.$ity),)*
$($oty: handle::OwnedStore::new({
static COUNTER: AtomicUsize = AtomicUsize::new(1);
&COUNTER
}),)*
$($ity: handle::InternedStore::new({
static COUNTER: AtomicUsize = AtomicUsize::new(1);
&COUNTER
}),)*
}
}
}
Expand Down Expand Up @@ -370,10 +364,6 @@ impl Bridge<'_> {
/// and forcing the use of APIs that take/return `S::TokenStream`, server-side.
#[repr(C)]
pub struct Client<I, O> {
// FIXME(eddyb) use a reference to the `static COUNTERS`, instead of
// a wrapper `fn` pointer, once `const fn` can reference `static`s.
pub(super) get_handle_counters: extern "C" fn() -> &'static HandleCounters,

pub(super) run: extern "C" fn(Bridge<'_>) -> Buffer,

pub(super) _marker: PhantomData<fn(I) -> O>,
Expand Down Expand Up @@ -433,7 +423,6 @@ fn run_client<A: for<'a, 's> DecodeMut<'a, 's, ()>, R: Encode<()>>(
impl Client<crate::TokenStream, crate::TokenStream> {
pub const fn expand1(f: impl Fn(crate::TokenStream) -> crate::TokenStream + Copy) -> Self {
Client {
get_handle_counters: HandleCounters::get,
run: super::selfless_reify::reify_to_extern_c_fn_hrt_bridge(move |bridge| {
run_client(bridge, |input| f(crate::TokenStream(input)).0)
}),
Expand All @@ -447,7 +436,6 @@ impl Client<(crate::TokenStream, crate::TokenStream), crate::TokenStream> {
f: impl Fn(crate::TokenStream, crate::TokenStream) -> crate::TokenStream + Copy,
) -> Self {
Client {
get_handle_counters: HandleCounters::get,
run: super::selfless_reify::reify_to_extern_c_fn_hrt_bridge(move |bridge| {
run_client(bridge, |(input, input2)| {
f(crate::TokenStream(input), crate::TokenStream(input2)).0
Expand Down
9 changes: 3 additions & 6 deletions library/proc_macro/src/bridge/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,14 +257,13 @@ fn run_server<
O: for<'a, 's> DecodeMut<'a, 's, HandleStore<MarkedTypes<S>>>,
>(
strategy: &impl ExecutionStrategy,
handle_counters: &'static client::HandleCounters,
server: S,
input: I,
run_client: extern "C" fn(Bridge<'_>) -> Buffer,
force_show_panics: bool,
) -> Result<O, PanicMessage> {
let mut dispatcher =
Dispatcher { handle_store: HandleStore::new(handle_counters), server: MarkedTypes(server) };
Dispatcher { handle_store: HandleStore::new(), server: MarkedTypes(server) };

let mut buf = Buffer::new();
input.encode(&mut buf, &mut dispatcher.handle_store);
Expand All @@ -282,10 +281,9 @@ impl client::Client<crate::TokenStream, crate::TokenStream> {
input: S::TokenStream,
force_show_panics: bool,
) -> Result<S::TokenStream, PanicMessage> {
let client::Client { get_handle_counters, run, _marker } = *self;
let client::Client { run, _marker } = *self;
run_server(
strategy,
get_handle_counters(),
server,
<MarkedTypes<S> as Types>::TokenStream::mark(input),
run,
Expand All @@ -304,10 +302,9 @@ impl client::Client<(crate::TokenStream, crate::TokenStream), crate::TokenStream
input2: S::TokenStream,
force_show_panics: bool,
) -> Result<S::TokenStream, PanicMessage> {
let client::Client { get_handle_counters, run, _marker } = *self;
let client::Client { run, _marker } = *self;
run_server(
strategy,
get_handle_counters(),
server,
(
<MarkedTypes<S> as Types>::TokenStream::mark(input),
Expand Down