diff --git a/compiler/rustc_expand/src/proc_macro_server.rs b/compiler/rustc_expand/src/proc_macro_server.rs index cc66eefac3e8..af0b5639d613 100644 --- a/compiler/rustc_expand/src/proc_macro_server.rs +++ b/compiler/rustc_expand/src/proc_macro_server.rs @@ -498,12 +498,13 @@ impl server::TokenStream for Rustc<'_, '_> { } builder.build() } - fn into_iter( + fn into_trees( &mut self, stream: Self::TokenStream, ) -> Vec> { - // FIXME: This is a raw port of the previous approach, and can probably - // be optimized. + // FIXME: This is a raw port of the previous approach (which had a + // `TokenStreamIter` server-side object with a single `next` method), + // and can probably be optimized (for bulk conversion). let mut cursor = stream.into_trees(); let mut stack = Vec::new(); let mut tts = Vec::new(); diff --git a/library/proc_macro/src/bridge/mod.rs b/library/proc_macro/src/bridge/mod.rs index c6d0635df576..4e931569ef63 100644 --- a/library/proc_macro/src/bridge/mod.rs +++ b/library/proc_macro/src/bridge/mod.rs @@ -73,9 +73,9 @@ macro_rules! with_api { ) -> $S::TokenStream; fn concat_streams( base: Option<$S::TokenStream>, - trees: Vec<$S::TokenStream>, + streams: Vec<$S::TokenStream>, ) -> $S::TokenStream; - fn into_iter( + fn into_trees( $self: $S::TokenStream ) -> Vec>; }, @@ -307,32 +307,6 @@ impl<'a, T, M> Unmark for &'a mut Marked { } } -impl Mark for Option { - type Unmarked = Option; - fn mark(unmarked: Self::Unmarked) -> Self { - unmarked.map(T::mark) - } -} -impl Unmark for Option { - type Unmarked = Option; - fn unmark(self) -> Self::Unmarked { - self.map(T::unmark) - } -} - -impl Mark for Result { - type Unmarked = Result; - fn mark(unmarked: Self::Unmarked) -> Self { - unmarked.map(T::mark).map_err(E::mark) - } -} -impl Unmark for Result { - type Unmarked = Result; - fn unmark(self) -> Self::Unmarked { - self.map(T::unmark).map_err(E::unmark) - } -} - impl Mark for Vec { type Unmarked = Vec; fn mark(unmarked: Self::Unmarked) -> Self { @@ -378,7 +352,6 @@ mark_noop! { Level, LineColumn, Spacing, - Bound, } rpc_encode_decode!( @@ -438,6 +411,28 @@ macro_rules! compound_traits { }; } +compound_traits!( + enum Bound { + Included(x), + Excluded(x), + Unbounded, + } +); + +compound_traits!( + enum Option { + Some(t), + None, + } +); + +compound_traits!( + enum Result { + Ok(t), + Err(e), + } +); + #[derive(Clone)] pub enum TokenTree { Group(G), diff --git a/library/proc_macro/src/bridge/rpc.rs b/library/proc_macro/src/bridge/rpc.rs index a94334e07362..e9d7a46c06f6 100644 --- a/library/proc_macro/src/bridge/rpc.rs +++ b/library/proc_macro/src/bridge/rpc.rs @@ -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; @@ -186,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 { - Included(x), - Excluded(x), - Unbounded, - } -); - -rpc_encode_decode!( - enum Option { - None, - Some(x), - } -); - -rpc_encode_decode!( - enum Result { - Ok(x), - Err(e), - } -); - impl Encode for &[u8] { fn encode(self, w: &mut Writer, s: &mut S) { self.len().encode(w, s); diff --git a/library/proc_macro/src/lib.rs b/library/proc_macro/src/lib.rs index 6e645216c8dd..5e1289ec79d3 100644 --- a/library/proc_macro/src/lib.rs +++ b/library/proc_macro/src/lib.rs @@ -235,7 +235,7 @@ impl From for TokenStream { /// Non-generic helper for implementing `FromIterator` and /// `Extend` with less monomorphization in calling crates. -struct ExtendStreamWithTreesHelper { +struct ConcatTreesHelper { trees: Vec< bridge::TokenTree< bridge::client::Group, @@ -246,9 +246,9 @@ struct ExtendStreamWithTreesHelper { >, } -impl ExtendStreamWithTreesHelper { +impl ConcatTreesHelper { fn new(capacity: usize) -> Self { - ExtendStreamWithTreesHelper { trees: Vec::with_capacity(capacity) } + ConcatTreesHelper { trees: Vec::with_capacity(capacity) } } fn push(&mut self, tree: TokenTree) { @@ -263,7 +263,7 @@ impl ExtendStreamWithTreesHelper { } } - fn extend(self, stream: &mut TokenStream) { + fn append_to(self, stream: &mut TokenStream) { if self.trees.is_empty() { return; } @@ -273,13 +273,13 @@ impl ExtendStreamWithTreesHelper { /// Non-generic helper for implementing `FromIterator` and /// `Extend` with less monomorphization in calling crates. -struct ExtendStreamWithStreamsHelper { +struct ConcatStreamsHelper { streams: Vec, } -impl ExtendStreamWithStreamsHelper { +impl ConcatStreamsHelper { fn new(capacity: usize) -> Self { - ExtendStreamWithStreamsHelper { streams: Vec::with_capacity(capacity) } + ConcatStreamsHelper { streams: Vec::with_capacity(capacity) } } fn push(&mut self, stream: TokenStream) { @@ -296,7 +296,7 @@ impl ExtendStreamWithStreamsHelper { } } - fn extend(mut self, stream: &mut TokenStream) { + fn append_to(mut self, stream: &mut TokenStream) { if self.streams.is_empty() { return; } @@ -314,7 +314,7 @@ impl ExtendStreamWithStreamsHelper { impl iter::FromIterator for TokenStream { fn from_iter>(trees: I) -> Self { let iter = trees.into_iter(); - let mut builder = ExtendStreamWithTreesHelper::new(iter.size_hint().0); + let mut builder = ConcatTreesHelper::new(iter.size_hint().0); iter.for_each(|tree| builder.push(tree)); builder.build() } @@ -326,7 +326,7 @@ impl iter::FromIterator for TokenStream { impl iter::FromIterator for TokenStream { fn from_iter>(streams: I) -> Self { let iter = streams.into_iter(); - let mut builder = ExtendStreamWithStreamsHelper::new(iter.size_hint().0); + let mut builder = ConcatStreamsHelper::new(iter.size_hint().0); iter.for_each(|stream| builder.push(stream)); builder.build() } @@ -336,9 +336,9 @@ impl iter::FromIterator for TokenStream { impl Extend for TokenStream { fn extend>(&mut self, trees: I) { let iter = trees.into_iter(); - let mut builder = ExtendStreamWithTreesHelper::new(iter.size_hint().0); + let mut builder = ConcatTreesHelper::new(iter.size_hint().0); iter.for_each(|tree| builder.push(tree)); - builder.extend(self); + builder.append_to(self); } } @@ -346,9 +346,9 @@ impl Extend for TokenStream { impl Extend for TokenStream { fn extend>(&mut self, streams: I) { let iter = streams.into_iter(); - let mut builder = ExtendStreamWithStreamsHelper::new(iter.size_hint().0); + let mut builder = ConcatStreamsHelper::new(iter.size_hint().0); iter.for_each(|stream| builder.push(stream)); - builder.extend(self); + builder.append_to(self); } } @@ -393,7 +393,7 @@ pub mod token_stream { type IntoIter = IntoIter; fn into_iter(self) -> IntoIter { - IntoIter(self.0.map(|v| v.into_iter()).unwrap_or_default().into_iter()) + IntoIter(self.0.map(|v| v.into_trees()).unwrap_or_default().into_iter()) } } }