Skip to content

Commit e232cb4

Browse files
authored
Rollup merge of #95308 - bjorn3:more_stable_proc_macro, r=Mark-Simulacrum
Reduce the amount of unstable features used in libproc_macro This makes it easier to adapt the source for stable when copying it into rust-analyzer to load rustc compiled proc macros.
2 parents 399dd80 + 7eda975 commit e232cb4

File tree

5 files changed

+54
-32
lines changed

5 files changed

+54
-32
lines changed

library/proc_macro/src/bridge/client.rs

+31-15
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
)*
@@ -310,15 +325,16 @@ impl Bridge<'_> {
310325
// NB. the server can't do this because it may use a different libstd.
311326
static HIDE_PANICS_DURING_EXPANSION: Once = Once::new();
312327
HIDE_PANICS_DURING_EXPANSION.call_once(|| {
313-
panic::update_hook(move |prev, info| {
328+
let prev = panic::take_hook();
329+
panic::set_hook(Box::new(move |info| {
314330
let show = BridgeState::with(|state| match state {
315331
BridgeState::NotConnected => true,
316332
BridgeState::Connected(_) | BridgeState::InUse => force_show_panics,
317333
});
318334
if show {
319335
prev(info)
320336
}
321-
});
337+
}));
322338
});
323339

324340
BRIDGE_STATE.with(|state| state.set(BridgeState::Connected(self), f))

library/proc_macro/src/bridge/closure.rs

+9-10
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
11
//! Closure type (equivalent to `&mut dyn FnMut(A) -> R`) that's `repr(C)`.
22
3+
use std::marker::PhantomData;
4+
35
#[repr(C)]
46
pub struct Closure<'a, A, R> {
5-
call: unsafe extern "C" fn(&mut Env, A) -> R,
6-
env: &'a mut Env,
7-
}
8-
9-
extern "C" {
10-
type Env;
7+
call: unsafe extern "C" fn(*mut Env, A) -> R,
8+
env: *mut Env,
9+
// Ensure Closure is !Send and !Sync
10+
_marker: PhantomData<*mut &'a mut ()>,
1111
}
1212

13-
impl<'a, A, R> !Sync for Closure<'a, A, R> {}
14-
impl<'a, A, R> !Send for Closure<'a, A, R> {}
13+
struct Env;
1514

1615
impl<'a, A, R, F: FnMut(A) -> R> From<&'a mut F> for Closure<'a, A, R> {
1716
fn from(f: &'a mut F) -> Self {
18-
unsafe extern "C" fn call<A, R, F: FnMut(A) -> R>(env: &mut Env, arg: A) -> R {
17+
unsafe extern "C" fn call<A, R, F: FnMut(A) -> R>(env: *mut Env, arg: A) -> R {
1918
(*(env as *mut _ as *mut F))(arg)
2019
}
21-
Closure { call: call::<A, R, F>, env: unsafe { &mut *(f as *mut _ as *mut Env) } }
20+
Closure { call: call::<A, R, F>, env: f as *mut _ as *mut Env, _marker: PhantomData }
2221
}
2322
}
2423

library/proc_macro/src/bridge/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -231,10 +231,10 @@ pub struct Bridge<'a> {
231231

232232
/// If 'true', always invoke the default panic hook
233233
force_show_panics: bool,
234-
}
235234

236-
impl<'a> !Sync for Bridge<'a> {}
237-
impl<'a> !Send for Bridge<'a> {}
235+
// Prevent Send and Sync impls
236+
_marker: marker::PhantomData<*mut ()>,
237+
}
238238

239239
#[forbid(unsafe_code)]
240240
#[allow(non_camel_case_types)]

library/proc_macro/src/bridge/server.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,12 @@ impl ExecutionStrategy for SameThread {
153153
let mut dispatch = |b| dispatcher.dispatch(b);
154154

155155
run_client(
156-
Bridge { cached_buffer: input, dispatch: (&mut dispatch).into(), force_show_panics },
156+
Bridge {
157+
cached_buffer: input,
158+
dispatch: (&mut dispatch).into(),
159+
force_show_panics,
160+
_marker: marker::PhantomData,
161+
},
157162
client_data,
158163
)
159164
}
@@ -189,6 +194,7 @@ impl ExecutionStrategy for CrossThread1 {
189194
cached_buffer: input,
190195
dispatch: (&mut dispatch).into(),
191196
force_show_panics,
197+
_marker: marker::PhantomData,
192198
},
193199
client_data,
194200
)
@@ -241,6 +247,7 @@ impl ExecutionStrategy for CrossThread2 {
241247
cached_buffer: input,
242248
dispatch: (&mut dispatch).into(),
243249
force_show_panics,
250+
_marker: marker::PhantomData,
244251
},
245252
client_data,
246253
);

library/proc_macro/src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,18 @@
1717
test(no_crate_inject, attr(deny(warnings))),
1818
test(attr(allow(dead_code, deprecated, unused_variables, unused_mut)))
1919
)]
20+
// This library is copied into rust-analyzer to allow loading rustc compiled proc macros.
21+
// Please avoid unstable features where possible to minimize the amount of changes necessary
22+
// to make it compile with rust-analyzer on stable.
2023
#![feature(rustc_allow_const_fn_unstable)]
2124
#![feature(nll)]
2225
#![feature(staged_api)]
2326
#![feature(allow_internal_unstable)]
2427
#![feature(decl_macro)]
25-
#![feature(extern_types)]
2628
#![feature(negative_impls)]
27-
#![feature(auto_traits)]
2829
#![feature(restricted_std)]
2930
#![feature(rustc_attrs)]
3031
#![feature(min_specialization)]
31-
#![feature(panic_update_hook)]
3232
#![recursion_limit = "256"]
3333

3434
#[unstable(feature = "proc_macro_internals", issue = "27812")]

0 commit comments

Comments
 (0)