1
Fork 0

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.
This commit is contained in:
Dylan DPC 2022-04-09 12:52:02 +02:00 committed by GitHub
commit e232cb42e6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 54 additions and 32 deletions

View file

@ -2,6 +2,8 @@
use super::*; use super::*;
use std::marker::PhantomData;
macro_rules! define_handles { macro_rules! define_handles {
( (
'owned: $($oty:ident,)* 'owned: $($oty:ident,)*
@ -45,20 +47,25 @@ macro_rules! define_handles {
$( $(
#[repr(C)] #[repr(C)]
pub(crate) struct $oty(handle::Handle); pub(crate) struct $oty {
impl !Send for $oty {} handle: handle::Handle,
impl !Sync for $oty {} // Prevent Send and Sync impls
_marker: PhantomData<*mut ()>,
}
// Forward `Drop::drop` to the inherent `drop` method. // Forward `Drop::drop` to the inherent `drop` method.
impl Drop for $oty { impl Drop for $oty {
fn drop(&mut self) { fn drop(&mut self) {
$oty(self.0).drop(); $oty {
handle: self.handle,
_marker: PhantomData,
}.drop();
} }
} }
impl<S> Encode<S> for $oty { impl<S> Encode<S> for $oty {
fn encode(self, w: &mut Writer, s: &mut S) { fn encode(self, w: &mut Writer, s: &mut S) {
let handle = self.0; let handle = self.handle;
mem::forget(self); mem::forget(self);
handle.encode(w, s); handle.encode(w, s);
} }
@ -74,7 +81,7 @@ macro_rules! define_handles {
impl<S> Encode<S> for &$oty { impl<S> Encode<S> for &$oty {
fn encode(self, w: &mut Writer, s: &mut S) { fn encode(self, w: &mut Writer, s: &mut S) {
self.0.encode(w, s); self.handle.encode(w, s);
} }
} }
@ -88,7 +95,7 @@ macro_rules! define_handles {
impl<S> Encode<S> for &mut $oty { impl<S> Encode<S> for &mut $oty {
fn encode(self, w: &mut Writer, s: &mut S) { fn encode(self, w: &mut Writer, s: &mut S) {
self.0.encode(w, s); self.handle.encode(w, s);
} }
} }
@ -113,7 +120,10 @@ macro_rules! define_handles {
impl<S> DecodeMut<'_, '_, S> for $oty { impl<S> DecodeMut<'_, '_, S> for $oty {
fn decode(r: &mut Reader<'_>, s: &mut S) -> Self { fn decode(r: &mut Reader<'_>, s: &mut S) -> Self {
$oty(handle::Handle::decode(r, s)) $oty {
handle: handle::Handle::decode(r, s),
_marker: PhantomData,
}
} }
} }
)* )*
@ -121,13 +131,15 @@ macro_rules! define_handles {
$( $(
#[repr(C)] #[repr(C)]
#[derive(Copy, Clone, PartialEq, Eq, Hash)] #[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub(crate) struct $ity(handle::Handle); pub(crate) struct $ity {
impl !Send for $ity {} handle: handle::Handle,
impl !Sync for $ity {} // Prevent Send and Sync impls
_marker: PhantomData<*mut ()>,
}
impl<S> Encode<S> for $ity { impl<S> Encode<S> for $ity {
fn encode(self, w: &mut Writer, s: &mut S) { fn encode(self, w: &mut Writer, s: &mut S) {
self.0.encode(w, s); self.handle.encode(w, s);
} }
} }
@ -149,7 +161,10 @@ macro_rules! define_handles {
impl<S> DecodeMut<'_, '_, S> for $ity { impl<S> DecodeMut<'_, '_, S> for $ity {
fn decode(r: &mut Reader<'_>, s: &mut S) -> Self { fn decode(r: &mut Reader<'_>, s: &mut S) -> Self {
$ity(handle::Handle::decode(r, s)) $ity {
handle: handle::Handle::decode(r, s),
_marker: PhantomData,
}
} }
} }
)* )*
@ -310,7 +325,8 @@ impl Bridge<'_> {
// NB. the server can't do this because it may use a different libstd. // NB. the server can't do this because it may use a different libstd.
static HIDE_PANICS_DURING_EXPANSION: Once = Once::new(); static HIDE_PANICS_DURING_EXPANSION: Once = Once::new();
HIDE_PANICS_DURING_EXPANSION.call_once(|| { HIDE_PANICS_DURING_EXPANSION.call_once(|| {
panic::update_hook(move |prev, info| { let prev = panic::take_hook();
panic::set_hook(Box::new(move |info| {
let show = BridgeState::with(|state| match state { let show = BridgeState::with(|state| match state {
BridgeState::NotConnected => true, BridgeState::NotConnected => true,
BridgeState::Connected(_) | BridgeState::InUse => force_show_panics, BridgeState::Connected(_) | BridgeState::InUse => force_show_panics,
@ -318,7 +334,7 @@ impl Bridge<'_> {
if show { if show {
prev(info) prev(info)
} }
}); }));
}); });
BRIDGE_STATE.with(|state| state.set(BridgeState::Connected(self), f)) BRIDGE_STATE.with(|state| state.set(BridgeState::Connected(self), f))

View file

@ -1,24 +1,23 @@
//! Closure type (equivalent to `&mut dyn FnMut(A) -> R`) that's `repr(C)`. //! Closure type (equivalent to `&mut dyn FnMut(A) -> R`) that's `repr(C)`.
use std::marker::PhantomData;
#[repr(C)] #[repr(C)]
pub struct Closure<'a, A, R> { pub struct Closure<'a, A, R> {
call: unsafe extern "C" fn(&mut Env, A) -> R, call: unsafe extern "C" fn(*mut Env, A) -> R,
env: &'a mut Env, env: *mut Env,
// Ensure Closure is !Send and !Sync
_marker: PhantomData<*mut &'a mut ()>,
} }
extern "C" { struct Env;
type Env;
}
impl<'a, A, R> !Sync for Closure<'a, A, R> {}
impl<'a, A, R> !Send for Closure<'a, A, R> {}
impl<'a, A, R, F: FnMut(A) -> R> From<&'a mut F> for Closure<'a, A, R> { impl<'a, A, R, F: FnMut(A) -> R> From<&'a mut F> for Closure<'a, A, R> {
fn from(f: &'a mut F) -> Self { fn from(f: &'a mut F) -> Self {
unsafe extern "C" fn call<A, R, F: FnMut(A) -> R>(env: &mut Env, arg: A) -> R { unsafe extern "C" fn call<A, R, F: FnMut(A) -> R>(env: *mut Env, arg: A) -> R {
(*(env as *mut _ as *mut F))(arg) (*(env as *mut _ as *mut F))(arg)
} }
Closure { call: call::<A, R, F>, env: unsafe { &mut *(f as *mut _ as *mut Env) } } Closure { call: call::<A, R, F>, env: f as *mut _ as *mut Env, _marker: PhantomData }
} }
} }

View file

@ -231,10 +231,10 @@ pub struct Bridge<'a> {
/// If 'true', always invoke the default panic hook /// If 'true', always invoke the default panic hook
force_show_panics: bool, force_show_panics: bool,
}
impl<'a> !Sync for Bridge<'a> {} // Prevent Send and Sync impls
impl<'a> !Send for Bridge<'a> {} _marker: marker::PhantomData<*mut ()>,
}
#[forbid(unsafe_code)] #[forbid(unsafe_code)]
#[allow(non_camel_case_types)] #[allow(non_camel_case_types)]

View file

@ -153,7 +153,12 @@ impl ExecutionStrategy for SameThread {
let mut dispatch = |b| dispatcher.dispatch(b); let mut dispatch = |b| dispatcher.dispatch(b);
run_client( run_client(
Bridge { cached_buffer: input, dispatch: (&mut dispatch).into(), force_show_panics }, Bridge {
cached_buffer: input,
dispatch: (&mut dispatch).into(),
force_show_panics,
_marker: marker::PhantomData,
},
client_data, client_data,
) )
} }
@ -189,6 +194,7 @@ impl ExecutionStrategy for CrossThread1 {
cached_buffer: input, cached_buffer: input,
dispatch: (&mut dispatch).into(), dispatch: (&mut dispatch).into(),
force_show_panics, force_show_panics,
_marker: marker::PhantomData,
}, },
client_data, client_data,
) )
@ -241,6 +247,7 @@ impl ExecutionStrategy for CrossThread2 {
cached_buffer: input, cached_buffer: input,
dispatch: (&mut dispatch).into(), dispatch: (&mut dispatch).into(),
force_show_panics, force_show_panics,
_marker: marker::PhantomData,
}, },
client_data, client_data,
); );

View file

@ -17,18 +17,18 @@
test(no_crate_inject, attr(deny(warnings))), test(no_crate_inject, attr(deny(warnings))),
test(attr(allow(dead_code, deprecated, unused_variables, unused_mut))) test(attr(allow(dead_code, deprecated, unused_variables, unused_mut)))
)] )]
// This library is copied into rust-analyzer to allow loading rustc compiled proc macros.
// Please avoid unstable features where possible to minimize the amount of changes necessary
// to make it compile with rust-analyzer on stable.
#![feature(rustc_allow_const_fn_unstable)] #![feature(rustc_allow_const_fn_unstable)]
#![feature(nll)] #![feature(nll)]
#![feature(staged_api)] #![feature(staged_api)]
#![feature(allow_internal_unstable)] #![feature(allow_internal_unstable)]
#![feature(decl_macro)] #![feature(decl_macro)]
#![feature(extern_types)]
#![feature(negative_impls)] #![feature(negative_impls)]
#![feature(auto_traits)]
#![feature(restricted_std)] #![feature(restricted_std)]
#![feature(rustc_attrs)] #![feature(rustc_attrs)]
#![feature(min_specialization)] #![feature(min_specialization)]
#![feature(panic_update_hook)]
#![recursion_limit = "256"] #![recursion_limit = "256"]
#[unstable(feature = "proc_macro_internals", issue = "27812")] #[unstable(feature = "proc_macro_internals", issue = "27812")]