Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ macro_rules! define_client_handles {

$(
pub(crate) struct $oty {
handle: handle::Handle,
handle: Handle,
}

impl !Send for $oty {}
Expand Down Expand Up @@ -61,7 +61,7 @@ macro_rules! define_client_handles {
impl<S> DecodeMut<'_, '_, S> for $oty {
fn decode(r: &mut Reader<'_>, s: &mut S) -> Self {
$oty {
handle: handle::Handle::decode(r, s),
handle: Handle::decode(r, s),
}
}
}
Expand All @@ -70,7 +70,7 @@ macro_rules! define_client_handles {
$(
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub(crate) struct $ity {
handle: handle::Handle,
handle: Handle,
}

impl !Send for $ity {}
Expand All @@ -85,7 +85,7 @@ macro_rules! define_client_handles {
impl<S> DecodeMut<'_, '_, S> for $ity {
fn decode(r: &mut Reader<'_>, s: &mut S) -> Self {
$ity {
handle: handle::Handle::decode(r, s),
handle: Handle::decode(r, s),
}
}
}
Expand Down Expand Up @@ -161,7 +161,7 @@ struct Bridge<'a> {
cached_buffer: Buffer,

/// Server-side function that the client uses to make requests.
dispatch: closure::Closure<'a, Buffer, Buffer>,
dispatch: Closure<'a, Buffer, Buffer>,

/// Provided globals for this macro expansion.
globals: ExpnGlobals<Span>,
Expand Down Expand Up @@ -327,7 +327,7 @@ impl Client<crate::TokenStream, crate::TokenStream> {
pub const fn expand1(f: impl Fn(crate::TokenStream) -> crate::TokenStream + Copy) -> Self {
Client {
handle_counters: &COUNTERS,
run: super::selfless_reify::reify_to_extern_c_fn_hrt_bridge(move |bridge| {
run: reify_to_extern_c_fn_hrt_bridge(move |bridge| {
run_client(bridge, |input| f(crate::TokenStream(Some(input))).0)
}),
_marker: PhantomData,
Expand All @@ -341,7 +341,7 @@ impl Client<(crate::TokenStream, crate::TokenStream), crate::TokenStream> {
) -> Self {
Client {
handle_counters: &COUNTERS,
run: super::selfless_reify::reify_to_extern_c_fn_hrt_bridge(move |bridge| {
run: reify_to_extern_c_fn_hrt_bridge(move |bridge| {
run_client(bridge, |(input, input2)| {
f(crate::TokenStream(Some(input)), crate::TokenStream(Some(input2))).0
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,31 +119,24 @@ macro_rules! with_api_handle_types {
};
}

#[allow(unsafe_code)]
mod arena;
#[allow(unsafe_code)]
mod buffer;
#[deny(unsafe_code)]
pub mod client;
#[allow(unsafe_code)]
mod closure;
#[forbid(unsafe_code)]
mod fxhash;
#[forbid(unsafe_code)]
mod handle;
#[macro_use]
#[forbid(unsafe_code)]
mod rpc;
#[allow(unsafe_code)]
mod selfless_reify;
#[forbid(unsafe_code)]
pub mod server;
mod support;
#[allow(unsafe_code)]
mod symbol;

use buffer::Buffer;
pub use rpc::PanicMessage;
use rpc::{Decode, DecodeMut, Encode, Reader, Writer};
pub(crate) use support::arena::Arena;
pub(crate) use support::buffer::Buffer;
pub(crate) use support::closure::Closure;
pub(crate) use support::fxhash::FxHashMap;
pub(crate) use support::handle::{Handle, InternedStore, OwnedStore};
pub(crate) use support::selfless_reify::reify_to_extern_c_fn_hrt_bridge;

/// Configuration for establishing an active connection between a server and a
/// client. The server creates the bridge config (`run_server` in `server.rs`),
Expand All @@ -156,7 +149,7 @@ pub struct BridgeConfig<'a> {
input: Buffer,

/// Server-side function that the client uses to make requests.
dispatch: closure::Closure<'a, Buffer, Buffer>,
dispatch: Closure<'a, Buffer, Buffer>,

/// If 'true', always invoke the default panic hook
force_show_panics: bool,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::io::Write;
use std::num::NonZero;
use std::str;

pub(super) type Writer = super::buffer::Buffer;
pub(super) type Writer = super::Buffer;

pub(super) trait Encode<S>: Sized {
fn encode(self, w: &mut Writer, s: &mut S);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ macro_rules! define_server_handles {
) => {
#[allow(non_snake_case)]
pub(super) struct HandleStore<S: Types> {
$($oty: handle::OwnedStore<S::$oty>,)*
$($ity: handle::InternedStore<S::$ity>,)*
$($oty: OwnedStore<S::$oty>,)*
$($ity: InternedStore<S::$ity>,)*
}

impl<S: Types> HandleStore<S> {
fn new(handle_counters: &'static client::HandleCounters) -> Self {
HandleStore {
$($oty: handle::OwnedStore::new(&handle_counters.$oty),)*
$($ity: handle::InternedStore::new(&handle_counters.$ity),)*
$($oty: OwnedStore::new(&handle_counters.$oty),)*
$($ity: InternedStore::new(&handle_counters.$ity),)*
}
}
}
Expand All @@ -36,15 +36,15 @@ macro_rules! define_server_handles {
for Marked<S::$oty, client::$oty>
{
fn decode(r: &mut Reader<'_>, s: &mut HandleStore<MarkedTypes<S>>) -> Self {
s.$oty.take(handle::Handle::decode(r, &mut ()))
s.$oty.take(Handle::decode(r, &mut ()))
}
}

impl<'s, S: Types> Decode<'_, 's, HandleStore<MarkedTypes<S>>>
for &'s Marked<S::$oty, client::$oty>
{
fn decode(r: &mut Reader<'_>, s: &'s HandleStore<MarkedTypes<S>>) -> Self {
&s.$oty[handle::Handle::decode(r, &mut ())]
&s.$oty[Handle::decode(r, &mut ())]
}
}

Expand All @@ -55,7 +55,7 @@ macro_rules! define_server_handles {
r: &mut Reader<'_>,
s: &'s mut HandleStore<MarkedTypes<S>>
) -> Self {
&mut s.$oty[handle::Handle::decode(r, &mut ())]
&mut s.$oty[Handle::decode(r, &mut ())]
}
}
)*
Expand All @@ -71,7 +71,7 @@ macro_rules! define_server_handles {
for Marked<S::$ity, client::$ity>
{
fn decode(r: &mut Reader<'_>, s: &mut HandleStore<MarkedTypes<S>>) -> Self {
s.$ity.copy(handle::Handle::decode(r, &mut ()))
s.$ity.copy(Handle::decode(r, &mut ()))
}
}
)*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,17 @@ impl DerefMut for Buffer {

impl Buffer {
#[inline]
pub(super) fn new() -> Self {
pub(crate) fn new() -> Self {
Self::default()
}

#[inline]
pub(super) fn clear(&mut self) {
pub(crate) fn clear(&mut self) {
self.len = 0;
}

#[inline]
pub(super) fn take(&mut self) -> Self {
pub(crate) fn take(&mut self) -> Self {
mem::take(self)
}

Expand All @@ -60,7 +60,7 @@ impl Buffer {
// (avoiding a memmove call). With extend_from_slice, LLVM at least
// currently is not able to make that optimization.
#[inline]
pub(super) fn extend_from_array<const N: usize>(&mut self, xs: &[u8; N]) {
pub(crate) fn extend_from_array<const N: usize>(&mut self, xs: &[u8; N]) {
if xs.len() > (self.capacity - self.len) {
let b = self.take();
*self = (b.reserve)(b, xs.len());
Expand All @@ -72,7 +72,7 @@ impl Buffer {
}

#[inline]
pub(super) fn extend_from_slice(&mut self, xs: &[u8]) {
pub(crate) fn extend_from_slice(&mut self, xs: &[u8]) {
if xs.len() > (self.capacity - self.len) {
let b = self.take();
*self = (b.reserve)(b, xs.len());
Expand All @@ -84,7 +84,7 @@ impl Buffer {
}

#[inline]
pub(super) fn push(&mut self, v: u8) {
pub(crate) fn push(&mut self, v: u8) {
// The code here is taken from Vec::push, and we know that reserve()
// will panic if we're exceeding isize::MAX bytes and so there's no need
// to check for overflow.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use std::marker::PhantomData;

#[repr(C)]
pub(super) struct Closure<'a, A, R> {
pub(crate) struct Closure<'a, A, R> {
call: unsafe extern "C" fn(*mut Env, A) -> R,
env: *mut Env,
// Prevent Send and Sync impls.
Expand All @@ -24,7 +24,7 @@ impl<'a, A, R, F: FnMut(A) -> R> From<&'a mut F> for Closure<'a, A, R> {
}

impl<'a, A, R> Closure<'a, A, R> {
pub(super) fn call(&mut self, arg: A) -> R {
pub(crate) fn call(&mut self, arg: A) -> R {
unsafe { (self.call)(self.env, arg) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::hash::{BuildHasherDefault, Hasher};
use std::ops::BitXor;

/// Type alias for a hashmap using the `fx` hash algorithm.
pub(super) type FxHashMap<K, V> = HashMap<K, V, BuildHasherDefault<FxHasher>>;
pub(crate) type FxHashMap<K, V> = HashMap<K, V, BuildHasherDefault<FxHasher>>;

/// A speedy hash algorithm for use within rustc. The hashmap in alloc by
/// default uses SipHash which isn't quite as speedy as we want. In the compiler
Expand All @@ -23,7 +23,7 @@ pub(super) type FxHashMap<K, V> = HashMap<K, V, BuildHasherDefault<FxHasher>>;
/// similar or slightly worse than FNV, but the speed of the hash function
/// itself is much higher because it works on up to 8 bytes at a time.
#[derive(Default)]
pub(super) struct FxHasher {
pub(crate) struct FxHasher {
hash: usize,
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ use std::sync::atomic::{AtomicU32, Ordering};

use super::fxhash::FxHashMap;

pub(super) type Handle = NonZero<u32>;
pub(crate) type Handle = NonZero<u32>;

/// A store that associates values of type `T` with numeric handles. A value can
/// be looked up using its handle.
pub(super) struct OwnedStore<T: 'static> {
pub(crate) struct OwnedStore<T: 'static> {
counter: &'static AtomicU32,
data: BTreeMap<Handle, T>,
}

impl<T> OwnedStore<T> {
pub(super) fn new(counter: &'static AtomicU32) -> Self {
pub(crate) fn new(counter: &'static AtomicU32) -> Self {
// Ensure the handle counter isn't 0, which would panic later,
// when `NonZero::new` (aka `Handle::new`) is called in `alloc`.
assert_ne!(counter.load(Ordering::Relaxed), 0);
Expand All @@ -28,14 +28,14 @@ impl<T> OwnedStore<T> {
}

impl<T> OwnedStore<T> {
pub(super) fn alloc(&mut self, x: T) -> Handle {
pub(crate) fn alloc(&mut self, x: T) -> Handle {
let counter = self.counter.fetch_add(1, Ordering::Relaxed);
let handle = Handle::new(counter).expect("`proc_macro` handle counter overflowed");
assert!(self.data.insert(handle, x).is_none());
handle
}

pub(super) fn take(&mut self, h: Handle) -> T {
pub(crate) fn take(&mut self, h: Handle) -> T {
self.data.remove(&h).expect("use-after-free in `proc_macro` handle")
}
}
Expand All @@ -54,22 +54,22 @@ impl<T> IndexMut<Handle> for OwnedStore<T> {
}

/// Like `OwnedStore`, but avoids storing any value more than once.
pub(super) struct InternedStore<T: 'static> {
pub(crate) struct InternedStore<T: 'static> {
owned: OwnedStore<T>,
interner: FxHashMap<T, Handle>,
}

impl<T: Copy + Eq + Hash> InternedStore<T> {
pub(super) fn new(counter: &'static AtomicU32) -> Self {
pub(crate) fn new(counter: &'static AtomicU32) -> Self {
InternedStore { owned: OwnedStore::new(counter), interner: FxHashMap::default() }
}

pub(super) fn alloc(&mut self, x: T) -> Handle {
pub(crate) fn alloc(&mut self, x: T) -> Handle {
let owned = &mut self.owned;
*self.interner.entry(x).or_insert_with(|| owned.alloc(x))
}

pub(super) fn copy(&mut self, h: Handle) -> T {
pub(crate) fn copy(&mut self, h: Handle) -> T {
self.owned[h]
}
}
12 changes: 12 additions & 0 deletions library/proc_macro/src/backend/support/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#[allow(unsafe_code)]
pub(crate) mod arena;
#[allow(unsafe_code)]
pub(crate) mod buffer;
#[allow(unsafe_code)]
pub(crate) mod closure;
#[forbid(unsafe_code)]
pub(crate) mod fxhash;
#[forbid(unsafe_code)]
pub(crate) mod handle;
#[allow(unsafe_code)]
pub(crate) mod selfless_reify;
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ macro_rules! define_reify_functions {
fn $name:ident $(<$($param:ident),*>)?
for $(extern $abi:tt)? fn($($arg:ident: $arg_ty:ty),*) -> $ret_ty:ty;
)+) => {
$(pub(super) const fn $name<
$(pub(crate) const fn $name<
$($($param,)*)?
F: Fn($($arg_ty),*) -> $ret_ty + Copy
>(f: F) -> $(extern $abi)? fn($($arg_ty),*) -> $ret_ty {
Expand Down Expand Up @@ -80,5 +80,5 @@ define_reify_functions! {
// because of the `fn` pointer type being "higher-ranked" (i.e. the
// `for<'a>` binder).
// FIXME(eddyb) try to remove the lifetime from `BridgeConfig`, that'd help.
fn reify_to_extern_c_fn_hrt_bridge<R> for extern "C" fn(bridge: super::BridgeConfig<'_>) -> R;
fn reify_to_extern_c_fn_hrt_bridge<R> for extern "C" fn(bridge: crate::backend::BridgeConfig<'_>) -> R;
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ impl<S> DecodeMut<'_, '_, S> for Symbol {

thread_local! {
static INTERNER: RefCell<Interner> = RefCell::new(Interner {
arena: arena::Arena::new(),
names: fxhash::FxHashMap::default(),
arena: Arena::new(),
names: FxHashMap::default(),
strings: Vec::new(),
// Start with a base of 1 to make sure that `NonZero<u32>` works.
sym_base: NonZero::new(1).unwrap(),
Expand All @@ -137,11 +137,11 @@ thread_local! {

/// Basic interner for a `Symbol`, inspired by the one in `rustc_span`.
struct Interner {
arena: arena::Arena,
arena: Arena,
// SAFETY: These `'static` lifetimes are actually references to data owned
// by the Arena. This is safe, as we never return them as static references
// from `Interner`.
names: fxhash::FxHashMap<&'static str, Symbol>,
names: FxHashMap<&'static str, Symbol>,
strings: Vec<&'static str>,
// The offset to apply to symbol names stored in the interner. This is used
// to ensure that symbol names are not re-used after the interner is
Expand Down Expand Up @@ -194,6 +194,6 @@ impl Interner {

// SAFETY: This is cleared after the names and strings tables are
// cleared out, so no references into the arena should remain.
self.arena = arena::Arena::new();
self.arena = Arena::new();
}
}
Loading
Loading