Skip to content

Commit

Permalink
libproc_macro => 2018
Browse files Browse the repository at this point in the history
  • Loading branch information
taiki-e committed Feb 3, 2019
1 parent fc6e9a2 commit 18da195
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 90 deletions.
1 change: 1 addition & 0 deletions src/libproc_macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
authors = ["The Rust Project Developers"]
name = "proc_macro"
version = "0.0.0"
edition = "2018"

[lib]
path = "lib.rs"
6 changes: 3 additions & 3 deletions src/libproc_macro/bridge/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::ops::{Deref, DerefMut};
use std::slice;

#[repr(C)]
struct Slice<'a, T: 'a> {
struct Slice<'a, T> {
data: &'a [T; 0],
len: usize,
}
Expand Down Expand Up @@ -42,7 +42,7 @@ pub struct Buffer<T: Copy> {
data: *mut T,
len: usize,
capacity: usize,
extend_from_slice: extern "C" fn(Buffer<T>, Slice<T>) -> Buffer<T>,
extend_from_slice: extern "C" fn(Buffer<T>, Slice<'_, T>) -> Buffer<T>,
drop: extern "C" fn(Buffer<T>),
}

Expand Down Expand Up @@ -139,7 +139,7 @@ impl<T: Copy> From<Vec<T>> for Buffer<T> {
}
}

extern "C" fn extend_from_slice<T: Copy>(b: Buffer<T>, xs: Slice<T>) -> Buffer<T> {
extern "C" fn extend_from_slice<T: Copy>(b: Buffer<T>, xs: Slice<'_, T>) -> Buffer<T> {
let mut v = to_vec(b);
v.extend_from_slice(&xs);
Buffer::from(v)
Expand Down
62 changes: 35 additions & 27 deletions src/libproc_macro/bridge/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ macro_rules! define_handles {
impl<S: server::Types> DecodeMut<'_, '_, HandleStore<server::MarkedTypes<S>>>
for Marked<S::$oty, $oty>
{
fn decode(r: &mut Reader, s: &mut HandleStore<server::MarkedTypes<S>>) -> Self {
fn decode(r: &mut Reader<'_>, s: &mut HandleStore<server::MarkedTypes<S>>) -> Self {
s.$oty.take(handle::Handle::decode(r, &mut ()))
}
}
Expand All @@ -80,7 +80,7 @@ macro_rules! define_handles {
impl<S: server::Types> Decode<'_, 's, HandleStore<server::MarkedTypes<S>>>
for &'s Marked<S::$oty, $oty>
{
fn decode(r: &mut Reader, s: &'s HandleStore<server::MarkedTypes<S>>) -> Self {
fn decode(r: &mut Reader<'_>, s: &'s HandleStore<server::MarkedTypes<S>>) -> Self {
&s.$oty[handle::Handle::decode(r, &mut ())]
}
}
Expand All @@ -94,7 +94,10 @@ macro_rules! define_handles {
impl<S: server::Types> DecodeMut<'_, 's, HandleStore<server::MarkedTypes<S>>>
for &'s mut Marked<S::$oty, $oty>
{
fn decode(r: &mut Reader, s: &'s mut HandleStore<server::MarkedTypes<S>>) -> Self {
fn decode(
r: &mut Reader<'_>,
s: &'s mut HandleStore<server::MarkedTypes<S>>
) -> Self {
&mut s.$oty[handle::Handle::decode(r, &mut ())]
}
}
Expand All @@ -108,7 +111,7 @@ macro_rules! define_handles {
}

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))
}
}
Expand All @@ -130,7 +133,7 @@ macro_rules! define_handles {
impl<S: server::Types> DecodeMut<'_, '_, HandleStore<server::MarkedTypes<S>>>
for Marked<S::$ity, $ity>
{
fn decode(r: &mut Reader, s: &mut HandleStore<server::MarkedTypes<S>>) -> Self {
fn decode(r: &mut Reader<'_>, s: &mut HandleStore<server::MarkedTypes<S>>) -> Self {
s.$ity.copy(handle::Handle::decode(r, &mut ()))
}
}
Expand All @@ -144,7 +147,7 @@ macro_rules! define_handles {
}

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))
}
}
Expand Down Expand Up @@ -200,7 +203,7 @@ impl Clone for Literal {

// FIXME(eddyb) `Literal` should not expose internal `Debug` impls.
impl fmt::Debug for Literal {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.debug())
}
}
Expand All @@ -212,7 +215,7 @@ impl Clone for SourceFile {
}

impl fmt::Debug for Span {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.debug())
}
}
Expand Down Expand Up @@ -275,7 +278,7 @@ impl BridgeState<'_> {
///
/// N.B., while `f` is running, the thread-local state
/// is `BridgeState::InUse`.
fn with<R>(f: impl FnOnce(&mut BridgeState) -> R) -> R {
fn with<R>(f: impl FnOnce(&mut BridgeState<'_>) -> R) -> R {
BRIDGE_STATE.with(|state| {
state.replace(BridgeState::InUse, |mut state| {
// FIXME(#52812) pass `f` directly to `replace` when `RefMutL` is gone
Expand Down Expand Up @@ -306,7 +309,7 @@ impl Bridge<'_> {
BRIDGE_STATE.with(|state| state.set(BridgeState::Connected(self), f))
}

fn with<R>(f: impl FnOnce(&mut Bridge) -> R) -> R {
fn with<R>(f: impl FnOnce(&mut Bridge<'_>) -> R) -> R {
BridgeState::with(|state| match state {
BridgeState::NotConnected => {
panic!("procedural macro API is used outside of a procedural macro");
Expand All @@ -331,15 +334,15 @@ impl Bridge<'_> {
#[derive(Copy, Clone)]
pub struct Client<F> {
pub(super) get_handle_counters: extern "C" fn() -> &'static HandleCounters,
pub(super) run: extern "C" fn(Bridge, F) -> Buffer<u8>,
pub(super) run: extern "C" fn(Bridge<'_>, F) -> Buffer<u8>,
pub(super) f: F,
}

// FIXME(#53451) public to work around `Cannot create local mono-item` ICE,
// affecting not only the function itself, but also the `BridgeState` `thread_local!`.
pub extern "C" fn __run_expand1(
mut bridge: Bridge,
f: fn(::TokenStream) -> ::TokenStream,
mut bridge: Bridge<'_>,
f: fn(crate::TokenStream) -> crate::TokenStream,
) -> Buffer<u8> {
// The initial `cached_buffer` contains the input.
let mut b = bridge.cached_buffer.take();
Expand All @@ -352,7 +355,7 @@ pub extern "C" fn __run_expand1(
// Put the `cached_buffer` back in the `Bridge`, for requests.
Bridge::with(|bridge| bridge.cached_buffer = b.take());

let output = f(::TokenStream(input)).0;
let output = f(crate::TokenStream(input)).0;

// Take the `cached_buffer` back out, for the output value.
b = Bridge::with(|bridge| bridge.cached_buffer.take());
Expand All @@ -378,8 +381,8 @@ pub extern "C" fn __run_expand1(
b
}

impl Client<fn(::TokenStream) -> ::TokenStream> {
pub const fn expand1(f: fn(::TokenStream) -> ::TokenStream) -> Self {
impl Client<fn(crate::TokenStream) -> crate::TokenStream> {
pub const fn expand1(f: fn(crate::TokenStream) -> crate::TokenStream) -> Self {
Client {
get_handle_counters: HandleCounters::get,
run: __run_expand1,
Expand All @@ -391,8 +394,8 @@ impl Client<fn(::TokenStream) -> ::TokenStream> {
// FIXME(#53451) public to work around `Cannot create local mono-item` ICE,
// affecting not only the function itself, but also the `BridgeState` `thread_local!`.
pub extern "C" fn __run_expand2(
mut bridge: Bridge,
f: fn(::TokenStream, ::TokenStream) -> ::TokenStream,
mut bridge: Bridge<'_>,
f: fn(crate::TokenStream, crate::TokenStream) -> crate::TokenStream,
) -> Buffer<u8> {
// The initial `cached_buffer` contains the input.
let mut b = bridge.cached_buffer.take();
Expand All @@ -406,7 +409,7 @@ pub extern "C" fn __run_expand2(
// Put the `cached_buffer` back in the `Bridge`, for requests.
Bridge::with(|bridge| bridge.cached_buffer = b.take());

let output = f(::TokenStream(input), ::TokenStream(input2)).0;
let output = f(crate::TokenStream(input), crate::TokenStream(input2)).0;

// Take the `cached_buffer` back out, for the output value.
b = Bridge::with(|bridge| bridge.cached_buffer.take());
Expand All @@ -432,8 +435,10 @@ pub extern "C" fn __run_expand2(
b
}

impl Client<fn(::TokenStream, ::TokenStream) -> ::TokenStream> {
pub const fn expand2(f: fn(::TokenStream, ::TokenStream) -> ::TokenStream) -> Self {
impl Client<fn(crate::TokenStream, crate::TokenStream) -> crate::TokenStream> {
pub const fn expand2(
f: fn(crate::TokenStream, crate::TokenStream) -> crate::TokenStream
) -> Self {
Client {
get_handle_counters: HandleCounters::get,
run: __run_expand2,
Expand All @@ -448,25 +453,25 @@ pub enum ProcMacro {
CustomDerive {
trait_name: &'static str,
attributes: &'static [&'static str],
client: Client<fn(::TokenStream) -> ::TokenStream>,
client: Client<fn(crate::TokenStream) -> crate::TokenStream>,
},

Attr {
name: &'static str,
client: Client<fn(::TokenStream, ::TokenStream) -> ::TokenStream>,
client: Client<fn(crate::TokenStream, crate::TokenStream) -> crate::TokenStream>,
},

Bang {
name: &'static str,
client: Client<fn(::TokenStream) -> ::TokenStream>,
client: Client<fn(crate::TokenStream) -> crate::TokenStream>,
},
}

impl ProcMacro {
pub const fn custom_derive(
trait_name: &'static str,
attributes: &'static [&'static str],
expand: fn(::TokenStream) -> ::TokenStream,
expand: fn(crate::TokenStream) -> crate::TokenStream,
) -> Self {
ProcMacro::CustomDerive {
trait_name,
Expand All @@ -477,15 +482,18 @@ impl ProcMacro {

pub const fn attr(
name: &'static str,
expand: fn(::TokenStream, ::TokenStream) -> ::TokenStream,
expand: fn(crate::TokenStream, crate::TokenStream) -> crate::TokenStream,
) -> Self {
ProcMacro::Attr {
name,
client: Client::expand2(expand),
}
}

pub const fn bang(name: &'static str, expand: fn(::TokenStream) -> ::TokenStream) -> Self {
pub const fn bang(
name: &'static str,
expand: fn(crate::TokenStream) -> crate::TokenStream
) -> Self {
ProcMacro::Bang {
name,
client: Client::expand1(expand),
Expand Down
8 changes: 4 additions & 4 deletions src/libproc_macro/bridge/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use std::panic;
use std::sync::atomic::AtomicUsize;
use std::sync::Once;
use std::thread;
use {Delimiter, Level, LineColumn, Spacing};
use crate::{Delimiter, Level, LineColumn, Spacing};

/// Higher-order macro describing the server RPC API, allowing automatic
/// generation of type-safe Rust APIs, both client-side and server-side.
Expand Down Expand Up @@ -196,9 +196,9 @@ mod scoped_cell;
#[forbid(unsafe_code)]
pub mod server;

use self::buffer::Buffer;
pub use self::rpc::PanicMessage;
use self::rpc::{Decode, DecodeMut, Encode, Reader, Writer};
use buffer::Buffer;
pub use rpc::PanicMessage;
use rpc::{Decode, DecodeMut, Encode, Reader, Writer};

/// An active connection between a server and a client.
/// The server creates the bridge (`Bridge::run_server` in `server.rs`),
Expand Down
18 changes: 9 additions & 9 deletions src/libproc_macro/bridge/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ macro_rules! rpc_encode_decode {
}

impl<S> DecodeMut<'_, '_, S> for $ty {
fn decode(r: &mut Reader, s: &mut S) -> Self {
fn decode(r: &mut Reader<'_>, s: &mut S) -> Self {
let mut byte = 0x80;
let mut v = 0;
let mut shift = 0;
Expand All @@ -61,7 +61,7 @@ macro_rules! rpc_encode_decode {
}

impl<S> DecodeMut<'_, '_, S> for $name {
fn decode(r: &mut Reader, s: &mut S) -> Self {
fn decode(r: &mut Reader<'_>, s: &mut S) -> Self {
$name {
$($field: DecodeMut::decode(r, s)),*
}
Expand Down Expand Up @@ -119,7 +119,7 @@ impl<S> Encode<S> for () {
}

impl<S> DecodeMut<'_, '_, S> for () {
fn decode(_: &mut Reader, _: &mut S) -> Self {}
fn decode(_: &mut Reader<'_>, _: &mut S) -> Self {}
}

impl<S> Encode<S> for u8 {
Expand All @@ -129,7 +129,7 @@ impl<S> Encode<S> for u8 {
}

impl<S> DecodeMut<'_, '_, S> for u8 {
fn decode(r: &mut Reader, _: &mut S) -> Self {
fn decode(r: &mut Reader<'_>, _: &mut S) -> Self {
let x = r[0];
*r = &r[1..];
x
Expand All @@ -146,7 +146,7 @@ impl<S> Encode<S> for bool {
}

impl<S> DecodeMut<'_, '_, S> for bool {
fn decode(r: &mut Reader, s: &mut S) -> Self {
fn decode(r: &mut Reader<'_>, s: &mut S) -> Self {
match u8::decode(r, s) {
0 => false,
1 => true,
Expand All @@ -162,7 +162,7 @@ impl<S> Encode<S> for char {
}

impl<S> DecodeMut<'_, '_, S> for char {
fn decode(r: &mut Reader, s: &mut S) -> Self {
fn decode(r: &mut Reader<'_>, s: &mut S) -> Self {
char::from_u32(u32::decode(r, s)).unwrap()
}
}
Expand All @@ -174,7 +174,7 @@ impl<S> Encode<S> for NonZeroU32 {
}

impl<S> DecodeMut<'_, '_, S> for NonZeroU32 {
fn decode(r: &mut Reader, s: &mut S) -> Self {
fn decode(r: &mut Reader<'_>, s: &mut S) -> Self {
Self::new(u32::decode(r, s)).unwrap()
}
}
Expand Down Expand Up @@ -251,7 +251,7 @@ impl<S> Encode<S> for String {
}

impl<S> DecodeMut<'_, '_, S> for String {
fn decode(r: &mut Reader, s: &mut S) -> Self {
fn decode(r: &mut Reader<'_>, s: &mut S) -> Self {
<&str>::decode(r, s).to_string()
}
}
Expand Down Expand Up @@ -306,7 +306,7 @@ impl<S> Encode<S> for PanicMessage {
}

impl<S> DecodeMut<'_, '_, S> for PanicMessage {
fn decode(r: &mut Reader, s: &mut S) -> Self {
fn decode(r: &mut Reader<'_>, s: &mut S) -> Self {
match Option::<String>::decode(r, s) {
Some(s) => PanicMessage::String(s),
None => PanicMessage::Unknown,
Expand Down
Loading

0 comments on commit 18da195

Please sign in to comment.