Skip to content

Commit e1134d8

Browse files
committed
clone reductions
1 parent 402373c commit e1134d8

4 files changed

Lines changed: 32 additions & 45 deletions

File tree

compiler/rustc_expand/src/proc_macro_server.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -467,8 +467,7 @@ impl<'a, 'b> Rustc<'a, 'b> {
467467
}
468468

469469
pub(crate) fn ts_rustc_to_pm(&mut self, ts: tokenstream::TokenStream) -> BridgeTokenStream {
470-
let trees: Vec<TokenTree<Span, Symbol>> = FromInternal::from_internal((ts, self));
471-
BridgeTokenStream { trees: Rc::new(trees) }
470+
BridgeTokenStream::new(<Vec<TokenTree<Span, Symbol>>>::from_internal((ts, self)))
472471
}
473472
}
474473

library/proc_macro/src/bridge/client.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,9 @@ impl Client<crate::TokenStream, crate::TokenStream> {
258258
handle_counters: &COUNTERS,
259259
run: super::selfless_reify::reify_to_extern_c_fn_hrt_bridge(move |bridge| {
260260
run_client(bridge, |input| {
261-
Rc::unwrap_or_clone(f(crate::TokenStream(Rc::new(input))).0)
261+
Rc::unwrap_or_clone(
262+
f(crate::TokenStream(crate::bridge::TokenStream::new(input))).0.trees,
263+
)
262264
})
263265
}),
264266
_marker: PhantomData,
@@ -275,8 +277,12 @@ impl Client<(crate::TokenStream, crate::TokenStream), crate::TokenStream> {
275277
run: super::selfless_reify::reify_to_extern_c_fn_hrt_bridge(move |bridge| {
276278
run_client(bridge, |(input, input2)| {
277279
Rc::unwrap_or_clone(
278-
f(crate::TokenStream(Rc::new(input)), crate::TokenStream(Rc::new(input2)))
279-
.0,
280+
f(
281+
crate::TokenStream(crate::bridge::TokenStream::new(input)),
282+
crate::TokenStream(crate::bridge::TokenStream::new(input2)),
283+
)
284+
.0
285+
.trees,
280286
)
281287
})
282288
}),

library/proc_macro/src/bridge/mod.rs

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,7 @@ impl<T: Mark> Mark for Vec<T> {
179179
}
180180
}
181181

182-
impl<T: Mark + Clone> Mark for Rc<T>
183-
where
184-
T::Unmarked: Clone,
185-
{
182+
impl<T: Mark> Mark for Rc<T> {
186183
type Unmarked = Rc<T::Unmarked>;
187184
fn mark(unmarked: Self::Unmarked) -> Self {
188185
Rc::new(Mark::mark(Rc::unwrap_or_clone(unmarked)))
@@ -417,24 +414,15 @@ impl<Span, Symbol> Default for TokenStream<Span, Symbol> {
417414
}
418415
}
419416

420-
compound_traits!(
421-
struct TokenStream<Span, Symbol> { trees }
422-
);
423-
/*
424-
#[derive(Clone)]
425-
pub struct RcTokenStream<Span, Symbol> {
426-
pub trees: Rc<Vec<TokenTree<Span, Symbol>>>,
427-
}
428-
429-
impl<Span, Symbol> Default for RcTokenStream<Span, Symbol> {
430-
fn default() -> Self {
431-
Self { trees: Rc::new(Vec::new()) }
417+
impl<Span, Symbol> TokenStream<Span, Symbol> {
418+
pub fn new(tts: Vec<TokenTree<Span, Symbol>>) -> Self {
419+
Self { trees: Rc::new(tts) }
432420
}
433421
}
434422

435423
compound_traits!(
436-
struct RcTokenStream<Span, Symbol> { trees }
437-
);*/
424+
struct TokenStream<Span, Symbol> { trees }
425+
);
438426

439427
#[derive(Clone, Debug)]
440428
pub struct Diagnostic<Span> {

library/proc_macro/src/lib.rs

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ pub fn is_available() -> bool {
100100
#[cfg_attr(feature = "rustc-dep-of-std", rustc_diagnostic_item = "TokenStream")]
101101
#[stable(feature = "proc_macro_lib", since = "1.15.0")]
102102
#[derive(Clone)]
103-
pub struct TokenStream(Rc<Vec<bridge::TokenTree<bridge::client::Span, bridge::client::Symbol>>>);
103+
pub struct TokenStream(bridge::TokenStream<bridge::client::Span, bridge::client::Symbol>);
104104

105105
#[stable(feature = "proc_macro_lib", since = "1.15.0")]
106106
impl !Send for TokenStream {}
@@ -154,13 +154,13 @@ impl TokenStream {
154154
/// Returns an empty `TokenStream` containing no token trees.
155155
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
156156
pub fn new() -> TokenStream {
157-
TokenStream(Rc::new(Vec::new()))
157+
TokenStream(bridge::TokenStream::new(Vec::new()))
158158
}
159159

160160
/// Checks if this `TokenStream` is empty.
161161
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
162162
pub fn is_empty(&self) -> bool {
163-
self.0.is_empty()
163+
self.0.trees.is_empty()
164164
}
165165

166166
/// Parses this `TokenStream` as an expression and attempts to expand any
@@ -175,8 +175,8 @@ impl TokenStream {
175175
/// considered errors, is unspecified and may change in the future.
176176
#[unstable(feature = "proc_macro_expand", issue = "90765")]
177177
pub fn expand_expr(&self) -> Result<TokenStream, ExpandError> {
178-
match BridgeMethods::ts_expand_expr(stream_to_bridge_stream(self.clone())) {
179-
Ok(stream) => Ok(TokenStream(Rc::clone(&stream.trees))),
178+
match BridgeMethods::ts_expand_expr(self.0.clone()) {
179+
Ok(stream) => Ok(TokenStream(stream)),
180180
Err(_) => Err(ExpandError),
181181
}
182182
}
@@ -194,7 +194,7 @@ impl FromStr for TokenStream {
194194
type Err = LexError;
195195

196196
fn from_str(src: &str) -> Result<TokenStream, LexError> {
197-
Ok(TokenStream(Rc::clone(&BridgeMethods::ts_from_str(src).trees)))
197+
Ok(TokenStream(BridgeMethods::ts_from_str(src)))
198198
}
199199
}
200200

@@ -212,7 +212,7 @@ impl FromStr for TokenStream {
212212
#[stable(feature = "proc_macro_lib", since = "1.15.0")]
213213
impl fmt::Display for TokenStream {
214214
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
215-
write!(f, "{}", BridgeMethods::ts_to_string(stream_to_bridge_stream(self.clone())))
215+
write!(f, "{}", BridgeMethods::ts_to_string(self.0.clone()))
216216
}
217217
}
218218

@@ -235,12 +235,6 @@ impl Default for TokenStream {
235235
#[unstable(feature = "proc_macro_quote", issue = "54722")]
236236
pub use quote::{HasIterator, RepInterp, ThereIsNoIteratorInRepetition, ext, quote, quote_span};
237237

238-
fn stream_to_bridge_stream(
239-
stream: TokenStream,
240-
) -> bridge::TokenStream<bridge::client::Span, bridge::client::Symbol> {
241-
bridge::TokenStream { trees: stream.0.clone() }
242-
}
243-
244238
fn tree_to_bridge_tree(
245239
tree: TokenTree,
246240
) -> bridge::TokenTree<bridge::client::Span, bridge::client::Symbol> {
@@ -256,7 +250,7 @@ fn tree_to_bridge_tree(
256250
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
257251
impl From<TokenTree> for TokenStream {
258252
fn from(tree: TokenTree) -> TokenStream {
259-
TokenStream(Rc::new(vec![tree_to_bridge_tree(tree)]))
253+
TokenStream(bridge::TokenStream::new(vec![tree_to_bridge_tree(tree)]))
260254
}
261255
}
262256

@@ -276,14 +270,14 @@ impl ConcatTreesHelper {
276270
}
277271

278272
fn build(self) -> TokenStream {
279-
TokenStream(Rc::new(self.trees))
273+
TokenStream(bridge::TokenStream::new(self.trees))
280274
}
281275

282276
fn append_to(mut self, stream: &mut TokenStream) {
283277
if self.trees.is_empty() {
284278
return;
285279
}
286-
Rc::make_mut(&mut stream.0).append(&mut self.trees);
280+
Rc::make_mut(&mut stream.0.trees).append(&mut self.trees);
287281
}
288282
}
289283

@@ -314,9 +308,9 @@ impl ConcatStreamsHelper {
314308
if self.streams.is_empty() {
315309
return;
316310
}
317-
let this = Rc::make_mut(&mut stream.0);
311+
let this = Rc::make_mut(&mut stream.0.trees);
318312
for mut s in self.streams {
319-
this.append(Rc::make_mut(&mut s.0));
313+
this.append(Rc::make_mut(&mut s.0.trees));
320314
}
321315
}
322316
}
@@ -423,7 +417,7 @@ pub mod token_stream {
423417
type IntoIter = IntoIter;
424418

425419
fn into_iter(self) -> IntoIter {
426-
IntoIter(Rc::unwrap_or_clone(self.0).into_iter())
420+
IntoIter(Rc::unwrap_or_clone(self.0.trees).into_iter())
427421
}
428422
}
429423
}
@@ -801,7 +795,7 @@ impl Group {
801795
pub fn new(delimiter: Delimiter, stream: TokenStream) -> Group {
802796
Group(bridge::Group {
803797
delimiter,
804-
stream: Some(stream_to_bridge_stream(stream)),
798+
stream: Some(stream.0),
805799
span: bridge::DelimSpan::from_single(Span::call_site().0),
806800
})
807801
}
@@ -819,8 +813,8 @@ impl Group {
819813
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
820814
pub fn stream(&self) -> TokenStream {
821815
match &self.0.stream {
822-
Some(stream) => TokenStream(Rc::clone(&stream.trees)),
823-
None => TokenStream(Rc::new(vec![])),
816+
Some(stream) => TokenStream(stream.clone()),
817+
None => TokenStream(bridge::TokenStream::new(vec![])),
824818
}
825819
}
826820

0 commit comments

Comments
 (0)