Skip to content

Commit 5546335

Browse files
committed
Auto merge of #76696 - Aaron1011:tokenstream-avoid-clone, r=petrochenkov
Avoid cloning the contents of a `TokenStream` in a few places
2 parents c9b5210 + f6aec82 commit 5546335

File tree

3 files changed

+12
-11
lines changed

3 files changed

+12
-11
lines changed

Diff for: compiler/rustc_ast/src/tokenstream.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ where
125125
/// instead of a representation of the abstract syntax tree.
126126
/// Today's `TokenTree`s can still contain AST via `token::Interpolated` for back-compat.
127127
#[derive(Clone, Debug, Default, Encodable, Decodable)]
128-
pub struct TokenStream(pub Lrc<Vec<TreeAndSpacing>>);
128+
pub struct TokenStream(pub(crate) Lrc<Vec<TreeAndSpacing>>);
129129

130130
pub type TreeAndSpacing = (TokenTree, Spacing);
131131

@@ -286,12 +286,12 @@ impl TokenStream {
286286
t1.next().is_none() && t2.next().is_none()
287287
}
288288

289-
pub fn map_enumerated<F: FnMut(usize, TokenTree) -> TokenTree>(self, mut f: F) -> TokenStream {
289+
pub fn map_enumerated<F: FnMut(usize, &TokenTree) -> TokenTree>(self, mut f: F) -> TokenStream {
290290
TokenStream(Lrc::new(
291291
self.0
292292
.iter()
293293
.enumerate()
294-
.map(|(i, (tree, is_joint))| (f(i, tree.clone()), *is_joint))
294+
.map(|(i, (tree, is_joint))| (f(i, tree), *is_joint))
295295
.collect(),
296296
))
297297
}
@@ -394,8 +394,8 @@ impl Cursor {
394394
self.index = index;
395395
}
396396

397-
pub fn look_ahead(&self, n: usize) -> Option<TokenTree> {
398-
self.stream.0[self.index..].get(n).map(|(tree, _)| tree.clone())
397+
pub fn look_ahead(&self, n: usize) -> Option<&TokenTree> {
398+
self.stream.0[self.index..].get(n).map(|(tree, _)| tree)
399399
}
400400
}
401401

Diff for: compiler/rustc_expand/src/mbe/macro_rules.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,8 @@ fn generic_extension<'cx>(
288288
// Replace all the tokens for the corresponding positions in the macro, to maintain
289289
// proper positions in error reporting, while maintaining the macro_backtrace.
290290
if rhs_spans.len() == tts.len() {
291-
tts = tts.map_enumerated(|i, mut tt| {
291+
tts = tts.map_enumerated(|i, tt| {
292+
let mut tt = tt.clone();
292293
let mut sp = rhs_spans[i];
293294
sp = sp.with_ctxt(tt.span().ctxt());
294295
tt.set_span(sp);

Diff for: compiler/rustc_parse/src/parser/mod.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -833,15 +833,15 @@ impl<'a> Parser<'a> {
833833
}
834834

835835
let frame = &self.token_cursor.frame;
836-
looker(&match frame.tree_cursor.look_ahead(dist - 1) {
836+
match frame.tree_cursor.look_ahead(dist - 1) {
837837
Some(tree) => match tree {
838-
TokenTree::Token(token) => token,
838+
TokenTree::Token(token) => looker(token),
839839
TokenTree::Delimited(dspan, delim, _) => {
840-
Token::new(token::OpenDelim(delim), dspan.open)
840+
looker(&Token::new(token::OpenDelim(*delim), dspan.open))
841841
}
842842
},
843-
None => Token::new(token::CloseDelim(frame.delim), frame.span.close),
844-
})
843+
None => looker(&Token::new(token::CloseDelim(frame.delim), frame.span.close)),
844+
}
845845
}
846846

847847
/// Returns whether any of the given keywords are `dist` tokens ahead of the current one.

0 commit comments

Comments
 (0)