Skip to content

fix: Update 1.63 proc macro ABI to match rustc #12747

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 12, 2022
Merged
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 @@ -178,8 +178,6 @@ define_handles! {
'owned:
FreeFunctions,
TokenStream,
TokenStreamBuilder,
TokenStreamIter,
Group,
Literal,
SourceFile,
Expand All @@ -204,12 +202,6 @@ impl Clone for TokenStream {
}
}

impl Clone for TokenStreamIter {
fn clone(&self) -> Self {
self.clone()
}
}

impl Clone for Group {
fn clone(&self) -> Self {
self.clone()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Server-side handles and storage for per-handle data.

use std::collections::{BTreeMap, HashMap};
use std::hash::Hash;
use std::hash::{BuildHasher, Hash};
use std::num::NonZeroU32;
use std::ops::{Index, IndexMut};
use std::sync::atomic::{AtomicUsize, Ordering};
Expand Down Expand Up @@ -51,15 +51,31 @@ impl<T> IndexMut<Handle> for OwnedStore<T> {
}
}

// HACK(eddyb) deterministic `std::collections::hash_map::RandomState` replacement
// that doesn't require adding any dependencies to `proc_macro` (like `rustc-hash`).
#[derive(Clone)]
struct NonRandomState;

impl BuildHasher for NonRandomState {
type Hasher = std::collections::hash_map::DefaultHasher;
#[inline]
fn build_hasher(&self) -> Self::Hasher {
Self::Hasher::new()
}
}

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

impl<T: Copy + Eq + Hash> InternedStore<T> {
pub(super) fn new(counter: &'static AtomicUsize) -> Self {
InternedStore { owned: OwnedStore::new(counter), interner: HashMap::new() }
InternedStore {
owned: OwnedStore::new(counter),
interner: HashMap::with_hasher(NonRandomState),
}
}

pub(super) fn alloc(&mut self, x: T) -> Handle {
Expand Down
136 changes: 76 additions & 60 deletions crates/proc-macro-srv/src/abis/abi_1_63/proc_macro/bridge/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,33 +60,29 @@ macro_rules! with_api {
TokenStream {
fn drop($self: $S::TokenStream);
fn clone($self: &$S::TokenStream) -> $S::TokenStream;
fn new() -> $S::TokenStream;
fn is_empty($self: &$S::TokenStream) -> bool;
fn expand_expr($self: &$S::TokenStream) -> Result<$S::TokenStream, ()>;
fn from_str(src: &str) -> $S::TokenStream;
fn to_string($self: &$S::TokenStream) -> String;
fn from_token_tree(
tree: TokenTree<$S::Group, $S::Punct, $S::Ident, $S::Literal>,
) -> $S::TokenStream;
fn into_iter($self: $S::TokenStream) -> $S::TokenStreamIter;
},
TokenStreamBuilder {
fn drop($self: $S::TokenStreamBuilder);
fn new() -> $S::TokenStreamBuilder;
fn push($self: &mut $S::TokenStreamBuilder, stream: $S::TokenStream);
fn build($self: $S::TokenStreamBuilder) -> $S::TokenStream;
},
TokenStreamIter {
fn drop($self: $S::TokenStreamIter);
fn clone($self: &$S::TokenStreamIter) -> $S::TokenStreamIter;
fn next(
$self: &mut $S::TokenStreamIter,
) -> Option<TokenTree<$S::Group, $S::Punct, $S::Ident, $S::Literal>>;
fn concat_trees(
base: Option<$S::TokenStream>,
trees: Vec<TokenTree<$S::Group, $S::Punct, $S::Ident, $S::Literal>>,
) -> $S::TokenStream;
fn concat_streams(
base: Option<$S::TokenStream>,
streams: Vec<$S::TokenStream>,
) -> $S::TokenStream;
fn into_trees(
$self: $S::TokenStream
) -> Vec<TokenTree<$S::Group, $S::Punct, $S::Ident, $S::Literal>>;
},
Group {
fn drop($self: $S::Group);
fn clone($self: &$S::Group) -> $S::Group;
fn new(delimiter: Delimiter, stream: $S::TokenStream) -> $S::Group;
fn new(delimiter: Delimiter, stream: Option<$S::TokenStream>) -> $S::Group;
fn delimiter($self: &$S::Group) -> Delimiter;
fn stream($self: &$S::Group) -> $S::TokenStream;
fn span($self: &$S::Group) -> $S::Span;
Expand Down Expand Up @@ -311,29 +307,18 @@ impl<'a, T, M> Unmark for &'a mut Marked<T, M> {
}
}

impl<T: Mark> Mark for Option<T> {
type Unmarked = Option<T::Unmarked>;
fn mark(unmarked: Self::Unmarked) -> Self {
unmarked.map(T::mark)
}
}
impl<T: Unmark> Unmark for Option<T> {
type Unmarked = Option<T::Unmarked>;
fn unmark(self) -> Self::Unmarked {
self.map(T::unmark)
}
}

impl<T: Mark, E: Mark> Mark for Result<T, E> {
type Unmarked = Result<T::Unmarked, E::Unmarked>;
impl<T: Mark> Mark for Vec<T> {
type Unmarked = Vec<T::Unmarked>;
fn mark(unmarked: Self::Unmarked) -> Self {
unmarked.map(T::mark).map_err(E::mark)
// Should be a no-op due to std's in-place collect optimizations.
unmarked.into_iter().map(T::mark).collect()
}
}
impl<T: Unmark, E: Unmark> Unmark for Result<T, E> {
type Unmarked = Result<T::Unmarked, E::Unmarked>;
impl<T: Unmark> Unmark for Vec<T> {
type Unmarked = Vec<T::Unmarked>;
fn unmark(self) -> Self::Unmarked {
self.map(T::unmark).map_err(E::unmark)
// Should be a no-op due to std's in-place collect optimizations.
self.into_iter().map(T::unmark).collect()
}
}

Expand Down Expand Up @@ -367,7 +352,6 @@ mark_noop! {
Level,
LineColumn,
Spacing,
Bound<usize>,
}

rpc_encode_decode!(
Expand All @@ -394,6 +378,61 @@ rpc_encode_decode!(
}
);

macro_rules! mark_compound {
(enum $name:ident <$($T:ident),+> { $($variant:ident $(($field:ident))?),* $(,)? }) => {
impl<$($T: Mark),+> Mark for $name <$($T),+> {
type Unmarked = $name <$($T::Unmarked),+>;
fn mark(unmarked: Self::Unmarked) -> Self {
match unmarked {
$($name::$variant $(($field))? => {
$name::$variant $((Mark::mark($field)))?
})*
}
}
}

impl<$($T: Unmark),+> Unmark for $name <$($T),+> {
type Unmarked = $name <$($T::Unmarked),+>;
fn unmark(self) -> Self::Unmarked {
match self {
$($name::$variant $(($field))? => {
$name::$variant $((Unmark::unmark($field)))?
})*
}
}
}
}
}

macro_rules! compound_traits {
($($t:tt)*) => {
rpc_encode_decode!($($t)*);
mark_compound!($($t)*);
};
}

compound_traits!(
enum Bound<T> {
Included(x),
Excluded(x),
Unbounded,
}
);

compound_traits!(
enum Option<T> {
Some(t),
None,
}
);

compound_traits!(
enum Result<T, E> {
Ok(t),
Err(e),
}
);

#[derive(Clone)]
pub enum TokenTree<G, P, I, L> {
Group(G),
Expand All @@ -402,30 +441,7 @@ pub enum TokenTree<G, P, I, L> {
Literal(L),
}

impl<G: Mark, P: Mark, I: Mark, L: Mark> Mark for TokenTree<G, P, I, L> {
type Unmarked = TokenTree<G::Unmarked, P::Unmarked, I::Unmarked, L::Unmarked>;
fn mark(unmarked: Self::Unmarked) -> Self {
match unmarked {
TokenTree::Group(tt) => TokenTree::Group(G::mark(tt)),
TokenTree::Punct(tt) => TokenTree::Punct(P::mark(tt)),
TokenTree::Ident(tt) => TokenTree::Ident(I::mark(tt)),
TokenTree::Literal(tt) => TokenTree::Literal(L::mark(tt)),
}
}
}
impl<G: Unmark, P: Unmark, I: Unmark, L: Unmark> Unmark for TokenTree<G, P, I, L> {
type Unmarked = TokenTree<G::Unmarked, P::Unmarked, I::Unmarked, L::Unmarked>;
fn unmark(self) -> Self::Unmarked {
match self {
TokenTree::Group(tt) => TokenTree::Group(tt.unmark()),
TokenTree::Punct(tt) => TokenTree::Punct(tt.unmark()),
TokenTree::Ident(tt) => TokenTree::Ident(tt.unmark()),
TokenTree::Literal(tt) => TokenTree::Literal(tt.unmark()),
}
}
}

rpc_encode_decode!(
compound_traits!(
enum TokenTree<G, P, I, L> {
Group(tt),
Punct(tt),
Expand Down
53 changes: 26 additions & 27 deletions crates/proc-macro-srv/src/abis/abi_1_63/proc_macro/bridge/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use std::any::Any;
use std::char;
use std::io::Write;
use std::num::NonZeroU32;
use std::ops::Bound;
use std::str;

pub(super) type Writer = super::buffer::Buffer;
Expand Down Expand Up @@ -43,15 +42,17 @@ macro_rules! rpc_encode_decode {
}
}
};
(struct $name:ident { $($field:ident),* $(,)? }) => {
impl<S> Encode<S> for $name {
(struct $name:ident $(<$($T:ident),+>)? { $($field:ident),* $(,)? }) => {
impl<S, $($($T: Encode<S>),+)?> Encode<S> for $name $(<$($T),+>)? {
fn encode(self, w: &mut Writer, s: &mut S) {
$(self.$field.encode(w, s);)*
}
}

impl<S> DecodeMut<'_, '_, S> for $name {
fn decode(r: &mut Reader<'_>, s: &mut S) -> Self {
impl<'a, S, $($($T: for<'s> DecodeMut<'a, 's, S>),+)?> DecodeMut<'a, '_, S>
for $name $(<$($T),+>)?
{
fn decode(r: &mut Reader<'a>, s: &mut S) -> Self {
$name {
$($field: DecodeMut::decode(r, s)),*
}
Expand Down Expand Up @@ -184,28 +185,6 @@ impl<'a, S, A: for<'s> DecodeMut<'a, 's, S>, B: for<'s> DecodeMut<'a, 's, S>> De
}
}

rpc_encode_decode!(
enum Bound<T> {
Included(x),
Excluded(x),
Unbounded,
}
);

rpc_encode_decode!(
enum Option<T> {
None,
Some(x),
}
);

rpc_encode_decode!(
enum Result<T, E> {
Ok(x),
Err(e),
}
);

impl<S> Encode<S> for &[u8] {
fn encode(self, w: &mut Writer, s: &mut S) {
self.len().encode(w, s);
Expand Down Expand Up @@ -246,6 +225,26 @@ impl<S> DecodeMut<'_, '_, S> for String {
}
}

impl<S, T: Encode<S>> Encode<S> for Vec<T> {
fn encode(self, w: &mut Writer, s: &mut S) {
self.len().encode(w, s);
for x in self {
x.encode(w, s);
}
}
}

impl<'a, S, T: for<'s> DecodeMut<'a, 's, S>> DecodeMut<'a, '_, S> for Vec<T> {
fn decode(r: &mut Reader<'a>, s: &mut S) -> Self {
let len = usize::decode(r, s);
let mut vec = Vec::with_capacity(len);
for _ in 0..len {
vec.push(T::decode(r, s));
}
vec
}
}

/// Simplified version of panic payloads, ignoring
/// types other than `&'static str` and `String`.
pub enum PanicMessage {
Expand Down
Loading