Skip to content

Commit ec7efa7

Browse files
committed
Avoid negative impls in the bridge
1 parent 4b67506 commit ec7efa7

File tree

3 files changed

+32
-20
lines changed

3 files changed

+32
-20
lines changed

library/proc_macro/src/bridge/client.rs

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
33
use super::*;
44

5+
use std::marker::PhantomData;
6+
57
macro_rules! define_handles {
68
(
79
'owned: $($oty:ident,)*
@@ -45,20 +47,25 @@ macro_rules! define_handles {
4547

4648
$(
4749
#[repr(C)]
48-
pub(crate) struct $oty(handle::Handle);
49-
impl !Send for $oty {}
50-
impl !Sync for $oty {}
50+
pub(crate) struct $oty {
51+
handle: handle::Handle,
52+
// Prevent Send and Sync impls
53+
_marker: PhantomData<*mut ()>,
54+
}
5155

5256
// Forward `Drop::drop` to the inherent `drop` method.
5357
impl Drop for $oty {
5458
fn drop(&mut self) {
55-
$oty(self.0).drop();
59+
$oty {
60+
handle: self.handle,
61+
_marker: PhantomData,
62+
}.drop();
5663
}
5764
}
5865

5966
impl<S> Encode<S> for $oty {
6067
fn encode(self, w: &mut Writer, s: &mut S) {
61-
let handle = self.0;
68+
let handle = self.handle;
6269
mem::forget(self);
6370
handle.encode(w, s);
6471
}
@@ -74,7 +81,7 @@ macro_rules! define_handles {
7481

7582
impl<S> Encode<S> for &$oty {
7683
fn encode(self, w: &mut Writer, s: &mut S) {
77-
self.0.encode(w, s);
84+
self.handle.encode(w, s);
7885
}
7986
}
8087

@@ -88,7 +95,7 @@ macro_rules! define_handles {
8895

8996
impl<S> Encode<S> for &mut $oty {
9097
fn encode(self, w: &mut Writer, s: &mut S) {
91-
self.0.encode(w, s);
98+
self.handle.encode(w, s);
9299
}
93100
}
94101

@@ -113,21 +120,26 @@ macro_rules! define_handles {
113120

114121
impl<S> DecodeMut<'_, '_, S> for $oty {
115122
fn decode(r: &mut Reader<'_>, s: &mut S) -> Self {
116-
$oty(handle::Handle::decode(r, s))
123+
$oty {
124+
handle: handle::Handle::decode(r, s),
125+
_marker: PhantomData,
126+
}
117127
}
118128
}
119129
)*
120130

121131
$(
122132
#[repr(C)]
123133
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
124-
pub(crate) struct $ity(handle::Handle);
125-
impl !Send for $ity {}
126-
impl !Sync for $ity {}
134+
pub(crate) struct $ity {
135+
handle: handle::Handle,
136+
// Prevent Send and Sync impls
137+
_marker: PhantomData<*mut ()>,
138+
}
127139

128140
impl<S> Encode<S> for $ity {
129141
fn encode(self, w: &mut Writer, s: &mut S) {
130-
self.0.encode(w, s);
142+
self.handle.encode(w, s);
131143
}
132144
}
133145

@@ -149,7 +161,10 @@ macro_rules! define_handles {
149161

150162
impl<S> DecodeMut<'_, '_, S> for $ity {
151163
fn decode(r: &mut Reader<'_>, s: &mut S) -> Self {
152-
$ity(handle::Handle::decode(r, s))
164+
$ity {
165+
handle: handle::Handle::decode(r, s),
166+
_marker: PhantomData,
167+
}
153168
}
154169
}
155170
)*

library/proc_macro/src/bridge/closure.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,12 @@ use std::marker::PhantomData;
66
pub struct Closure<'a, A, R> {
77
call: unsafe extern "C" fn(*mut Env, A) -> R,
88
env: *mut Env,
9-
_marker: PhantomData<&'a mut ()>,
9+
// Ensure Closure is !Send and !Sync
10+
_marker: PhantomData<*mut &'a mut ()>,
1011
}
1112

1213
struct Env;
1314

14-
impl<'a, A, R> !Sync for Closure<'a, A, R> {}
15-
impl<'a, A, R> !Send for Closure<'a, A, R> {}
16-
1715
impl<'a, A, R, F: FnMut(A) -> R> From<&'a mut F> for Closure<'a, A, R> {
1816
fn from(f: &'a mut F) -> Self {
1917
unsafe extern "C" fn call<A, R, F: FnMut(A) -> R>(env: *mut Env, arg: A) -> R {

library/proc_macro/src/bridge/mod.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,8 @@ use rpc::{Decode, DecodeMut, Encode, Reader, Writer};
220220
/// then passes it to the client through the function pointer in the `run`
221221
/// field of `client::Client`. The client holds its copy of the `Bridge`
222222
/// in TLS during its execution (`Bridge::{enter, with}` in `client.rs`).
223+
// Note: Bridge is !Send and !Sync due to containg a `Closure`. If this
224+
// ever changes, make sure to preserve the !Send and !Sync property.
223225
#[repr(C)]
224226
pub struct Bridge<'a> {
225227
/// Reusable buffer (only `clear`-ed, never shrunk), primarily
@@ -233,9 +235,6 @@ pub struct Bridge<'a> {
233235
force_show_panics: bool,
234236
}
235237

236-
impl<'a> !Sync for Bridge<'a> {}
237-
impl<'a> !Send for Bridge<'a> {}
238-
239238
#[forbid(unsafe_code)]
240239
#[allow(non_camel_case_types)]
241240
mod api_tags {

0 commit comments

Comments
 (0)