Skip to content

Commit 1e3ce76

Browse files
committed
Auto merge of #12747 - jonas-schievink:proc-macro-abi-1.63, r=jonas-schievink
fix: Update 1.61 proc macro ABI to match rustc This updates us to the ABI used by rustc 1.63.0-beta.5, which will likely be the ABI of the next stable Rust release. It should also work on nightly (for now, but future changes won't be supported until the rustc version is bumped). cc #12600
2 parents caf23f2 + f29f01c commit 1e3ce76

File tree

7 files changed

+264
-160
lines changed

7 files changed

+264
-160
lines changed

crates/proc-macro-srv/src/abis/abi_1_63/proc_macro/bridge/client.rs

-8
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,6 @@ define_handles! {
178178
'owned:
179179
FreeFunctions,
180180
TokenStream,
181-
TokenStreamBuilder,
182-
TokenStreamIter,
183181
Group,
184182
Literal,
185183
SourceFile,
@@ -204,12 +202,6 @@ impl Clone for TokenStream {
204202
}
205203
}
206204

207-
impl Clone for TokenStreamIter {
208-
fn clone(&self) -> Self {
209-
self.clone()
210-
}
211-
}
212-
213205
impl Clone for Group {
214206
fn clone(&self) -> Self {
215207
self.clone()

crates/proc-macro-srv/src/abis/abi_1_63/proc_macro/bridge/handle.rs

+19-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Server-side handles and storage for per-handle data.
22
33
use std::collections::{BTreeMap, HashMap};
4-
use std::hash::Hash;
4+
use std::hash::{BuildHasher, Hash};
55
use std::num::NonZeroU32;
66
use std::ops::{Index, IndexMut};
77
use std::sync::atomic::{AtomicUsize, Ordering};
@@ -51,15 +51,31 @@ impl<T> IndexMut<Handle> for OwnedStore<T> {
5151
}
5252
}
5353

54+
// HACK(eddyb) deterministic `std::collections::hash_map::RandomState` replacement
55+
// that doesn't require adding any dependencies to `proc_macro` (like `rustc-hash`).
56+
#[derive(Clone)]
57+
struct NonRandomState;
58+
59+
impl BuildHasher for NonRandomState {
60+
type Hasher = std::collections::hash_map::DefaultHasher;
61+
#[inline]
62+
fn build_hasher(&self) -> Self::Hasher {
63+
Self::Hasher::new()
64+
}
65+
}
66+
5467
/// Like `OwnedStore`, but avoids storing any value more than once.
5568
pub(super) struct InternedStore<T: 'static> {
5669
owned: OwnedStore<T>,
57-
interner: HashMap<T, Handle>,
70+
interner: HashMap<T, Handle, NonRandomState>,
5871
}
5972

6073
impl<T: Copy + Eq + Hash> InternedStore<T> {
6174
pub(super) fn new(counter: &'static AtomicUsize) -> Self {
62-
InternedStore { owned: OwnedStore::new(counter), interner: HashMap::new() }
75+
InternedStore {
76+
owned: OwnedStore::new(counter),
77+
interner: HashMap::with_hasher(NonRandomState),
78+
}
6379
}
6480

6581
pub(super) fn alloc(&mut self, x: T) -> Handle {

crates/proc-macro-srv/src/abis/abi_1_63/proc_macro/bridge/mod.rs

+76-60
Original file line numberDiff line numberDiff line change
@@ -60,33 +60,29 @@ macro_rules! with_api {
6060
TokenStream {
6161
fn drop($self: $S::TokenStream);
6262
fn clone($self: &$S::TokenStream) -> $S::TokenStream;
63-
fn new() -> $S::TokenStream;
6463
fn is_empty($self: &$S::TokenStream) -> bool;
6564
fn expand_expr($self: &$S::TokenStream) -> Result<$S::TokenStream, ()>;
6665
fn from_str(src: &str) -> $S::TokenStream;
6766
fn to_string($self: &$S::TokenStream) -> String;
6867
fn from_token_tree(
6968
tree: TokenTree<$S::Group, $S::Punct, $S::Ident, $S::Literal>,
7069
) -> $S::TokenStream;
71-
fn into_iter($self: $S::TokenStream) -> $S::TokenStreamIter;
72-
},
73-
TokenStreamBuilder {
74-
fn drop($self: $S::TokenStreamBuilder);
75-
fn new() -> $S::TokenStreamBuilder;
76-
fn push($self: &mut $S::TokenStreamBuilder, stream: $S::TokenStream);
77-
fn build($self: $S::TokenStreamBuilder) -> $S::TokenStream;
78-
},
79-
TokenStreamIter {
80-
fn drop($self: $S::TokenStreamIter);
81-
fn clone($self: &$S::TokenStreamIter) -> $S::TokenStreamIter;
82-
fn next(
83-
$self: &mut $S::TokenStreamIter,
84-
) -> Option<TokenTree<$S::Group, $S::Punct, $S::Ident, $S::Literal>>;
70+
fn concat_trees(
71+
base: Option<$S::TokenStream>,
72+
trees: Vec<TokenTree<$S::Group, $S::Punct, $S::Ident, $S::Literal>>,
73+
) -> $S::TokenStream;
74+
fn concat_streams(
75+
base: Option<$S::TokenStream>,
76+
streams: Vec<$S::TokenStream>,
77+
) -> $S::TokenStream;
78+
fn into_trees(
79+
$self: $S::TokenStream
80+
) -> Vec<TokenTree<$S::Group, $S::Punct, $S::Ident, $S::Literal>>;
8581
},
8682
Group {
8783
fn drop($self: $S::Group);
8884
fn clone($self: &$S::Group) -> $S::Group;
89-
fn new(delimiter: Delimiter, stream: $S::TokenStream) -> $S::Group;
85+
fn new(delimiter: Delimiter, stream: Option<$S::TokenStream>) -> $S::Group;
9086
fn delimiter($self: &$S::Group) -> Delimiter;
9187
fn stream($self: &$S::Group) -> $S::TokenStream;
9288
fn span($self: &$S::Group) -> $S::Span;
@@ -311,29 +307,18 @@ impl<'a, T, M> Unmark for &'a mut Marked<T, M> {
311307
}
312308
}
313309

314-
impl<T: Mark> Mark for Option<T> {
315-
type Unmarked = Option<T::Unmarked>;
316-
fn mark(unmarked: Self::Unmarked) -> Self {
317-
unmarked.map(T::mark)
318-
}
319-
}
320-
impl<T: Unmark> Unmark for Option<T> {
321-
type Unmarked = Option<T::Unmarked>;
322-
fn unmark(self) -> Self::Unmarked {
323-
self.map(T::unmark)
324-
}
325-
}
326-
327-
impl<T: Mark, E: Mark> Mark for Result<T, E> {
328-
type Unmarked = Result<T::Unmarked, E::Unmarked>;
310+
impl<T: Mark> Mark for Vec<T> {
311+
type Unmarked = Vec<T::Unmarked>;
329312
fn mark(unmarked: Self::Unmarked) -> Self {
330-
unmarked.map(T::mark).map_err(E::mark)
313+
// Should be a no-op due to std's in-place collect optimizations.
314+
unmarked.into_iter().map(T::mark).collect()
331315
}
332316
}
333-
impl<T: Unmark, E: Unmark> Unmark for Result<T, E> {
334-
type Unmarked = Result<T::Unmarked, E::Unmarked>;
317+
impl<T: Unmark> Unmark for Vec<T> {
318+
type Unmarked = Vec<T::Unmarked>;
335319
fn unmark(self) -> Self::Unmarked {
336-
self.map(T::unmark).map_err(E::unmark)
320+
// Should be a no-op due to std's in-place collect optimizations.
321+
self.into_iter().map(T::unmark).collect()
337322
}
338323
}
339324

@@ -367,7 +352,6 @@ mark_noop! {
367352
Level,
368353
LineColumn,
369354
Spacing,
370-
Bound<usize>,
371355
}
372356

373357
rpc_encode_decode!(
@@ -394,6 +378,61 @@ rpc_encode_decode!(
394378
}
395379
);
396380

381+
macro_rules! mark_compound {
382+
(enum $name:ident <$($T:ident),+> { $($variant:ident $(($field:ident))?),* $(,)? }) => {
383+
impl<$($T: Mark),+> Mark for $name <$($T),+> {
384+
type Unmarked = $name <$($T::Unmarked),+>;
385+
fn mark(unmarked: Self::Unmarked) -> Self {
386+
match unmarked {
387+
$($name::$variant $(($field))? => {
388+
$name::$variant $((Mark::mark($field)))?
389+
})*
390+
}
391+
}
392+
}
393+
394+
impl<$($T: Unmark),+> Unmark for $name <$($T),+> {
395+
type Unmarked = $name <$($T::Unmarked),+>;
396+
fn unmark(self) -> Self::Unmarked {
397+
match self {
398+
$($name::$variant $(($field))? => {
399+
$name::$variant $((Unmark::unmark($field)))?
400+
})*
401+
}
402+
}
403+
}
404+
}
405+
}
406+
407+
macro_rules! compound_traits {
408+
($($t:tt)*) => {
409+
rpc_encode_decode!($($t)*);
410+
mark_compound!($($t)*);
411+
};
412+
}
413+
414+
compound_traits!(
415+
enum Bound<T> {
416+
Included(x),
417+
Excluded(x),
418+
Unbounded,
419+
}
420+
);
421+
422+
compound_traits!(
423+
enum Option<T> {
424+
Some(t),
425+
None,
426+
}
427+
);
428+
429+
compound_traits!(
430+
enum Result<T, E> {
431+
Ok(t),
432+
Err(e),
433+
}
434+
);
435+
397436
#[derive(Clone)]
398437
pub enum TokenTree<G, P, I, L> {
399438
Group(G),
@@ -402,30 +441,7 @@ pub enum TokenTree<G, P, I, L> {
402441
Literal(L),
403442
}
404443

405-
impl<G: Mark, P: Mark, I: Mark, L: Mark> Mark for TokenTree<G, P, I, L> {
406-
type Unmarked = TokenTree<G::Unmarked, P::Unmarked, I::Unmarked, L::Unmarked>;
407-
fn mark(unmarked: Self::Unmarked) -> Self {
408-
match unmarked {
409-
TokenTree::Group(tt) => TokenTree::Group(G::mark(tt)),
410-
TokenTree::Punct(tt) => TokenTree::Punct(P::mark(tt)),
411-
TokenTree::Ident(tt) => TokenTree::Ident(I::mark(tt)),
412-
TokenTree::Literal(tt) => TokenTree::Literal(L::mark(tt)),
413-
}
414-
}
415-
}
416-
impl<G: Unmark, P: Unmark, I: Unmark, L: Unmark> Unmark for TokenTree<G, P, I, L> {
417-
type Unmarked = TokenTree<G::Unmarked, P::Unmarked, I::Unmarked, L::Unmarked>;
418-
fn unmark(self) -> Self::Unmarked {
419-
match self {
420-
TokenTree::Group(tt) => TokenTree::Group(tt.unmark()),
421-
TokenTree::Punct(tt) => TokenTree::Punct(tt.unmark()),
422-
TokenTree::Ident(tt) => TokenTree::Ident(tt.unmark()),
423-
TokenTree::Literal(tt) => TokenTree::Literal(tt.unmark()),
424-
}
425-
}
426-
}
427-
428-
rpc_encode_decode!(
444+
compound_traits!(
429445
enum TokenTree<G, P, I, L> {
430446
Group(tt),
431447
Punct(tt),

crates/proc-macro-srv/src/abis/abi_1_63/proc_macro/bridge/rpc.rs

+26-27
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use std::any::Any;
44
use std::char;
55
use std::io::Write;
66
use std::num::NonZeroU32;
7-
use std::ops::Bound;
87
use std::str;
98

109
pub(super) type Writer = super::buffer::Buffer;
@@ -43,15 +42,17 @@ macro_rules! rpc_encode_decode {
4342
}
4443
}
4544
};
46-
(struct $name:ident { $($field:ident),* $(,)? }) => {
47-
impl<S> Encode<S> for $name {
45+
(struct $name:ident $(<$($T:ident),+>)? { $($field:ident),* $(,)? }) => {
46+
impl<S, $($($T: Encode<S>),+)?> Encode<S> for $name $(<$($T),+>)? {
4847
fn encode(self, w: &mut Writer, s: &mut S) {
4948
$(self.$field.encode(w, s);)*
5049
}
5150
}
5251

53-
impl<S> DecodeMut<'_, '_, S> for $name {
54-
fn decode(r: &mut Reader<'_>, s: &mut S) -> Self {
52+
impl<'a, S, $($($T: for<'s> DecodeMut<'a, 's, S>),+)?> DecodeMut<'a, '_, S>
53+
for $name $(<$($T),+>)?
54+
{
55+
fn decode(r: &mut Reader<'a>, s: &mut S) -> Self {
5556
$name {
5657
$($field: DecodeMut::decode(r, s)),*
5758
}
@@ -184,28 +185,6 @@ impl<'a, S, A: for<'s> DecodeMut<'a, 's, S>, B: for<'s> DecodeMut<'a, 's, S>> De
184185
}
185186
}
186187

187-
rpc_encode_decode!(
188-
enum Bound<T> {
189-
Included(x),
190-
Excluded(x),
191-
Unbounded,
192-
}
193-
);
194-
195-
rpc_encode_decode!(
196-
enum Option<T> {
197-
None,
198-
Some(x),
199-
}
200-
);
201-
202-
rpc_encode_decode!(
203-
enum Result<T, E> {
204-
Ok(x),
205-
Err(e),
206-
}
207-
);
208-
209188
impl<S> Encode<S> for &[u8] {
210189
fn encode(self, w: &mut Writer, s: &mut S) {
211190
self.len().encode(w, s);
@@ -246,6 +225,26 @@ impl<S> DecodeMut<'_, '_, S> for String {
246225
}
247226
}
248227

228+
impl<S, T: Encode<S>> Encode<S> for Vec<T> {
229+
fn encode(self, w: &mut Writer, s: &mut S) {
230+
self.len().encode(w, s);
231+
for x in self {
232+
x.encode(w, s);
233+
}
234+
}
235+
}
236+
237+
impl<'a, S, T: for<'s> DecodeMut<'a, 's, S>> DecodeMut<'a, '_, S> for Vec<T> {
238+
fn decode(r: &mut Reader<'a>, s: &mut S) -> Self {
239+
let len = usize::decode(r, s);
240+
let mut vec = Vec::with_capacity(len);
241+
for _ in 0..len {
242+
vec.push(T::decode(r, s));
243+
}
244+
vec
245+
}
246+
}
247+
249248
/// Simplified version of panic payloads, ignoring
250249
/// types other than `&'static str` and `String`.
251250
pub enum PanicMessage {

0 commit comments

Comments
 (0)