Skip to content

Commit 7055c23

Browse files
committed
ast_pretty: Pass some token streams and trees by reference
1 parent 394e1b4 commit 7055c23

File tree

5 files changed

+24
-24
lines changed

5 files changed

+24
-24
lines changed

src/librustc_ast_pretty/pprust.rs

+19-19
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_ast::ast::{InlineAsmOptions, InlineAsmTemplatePiece};
99
use rustc_ast::attr;
1010
use rustc_ast::ptr::P;
1111
use rustc_ast::token::{self, BinOpToken, DelimToken, Nonterminal, Token, TokenKind};
12-
use rustc_ast::tokenstream::{self, TokenStream, TokenTree};
12+
use rustc_ast::tokenstream::{TokenStream, TokenTree};
1313
use rustc_ast::util::parser::{self, AssocOp, Fixity};
1414
use rustc_ast::util::{classify, comments};
1515
use rustc_span::edition::Edition;
@@ -293,7 +293,7 @@ pub fn nonterminal_to_string(nt: &Nonterminal) -> String {
293293
token::NtIdent(e, is_raw) => IdentPrinter::for_ast_ident(e, is_raw).to_string(),
294294
token::NtLifetime(e) => e.to_string(),
295295
token::NtLiteral(ref e) => expr_to_string(e),
296-
token::NtTT(ref tree) => tt_to_string(tree.clone()),
296+
token::NtTT(ref tree) => tt_to_string(tree),
297297
token::NtVis(ref e) => vis_to_string(e),
298298
}
299299
}
@@ -314,11 +314,11 @@ pub fn expr_to_string(e: &ast::Expr) -> String {
314314
to_string(|s| s.print_expr(e))
315315
}
316316

317-
pub fn tt_to_string(tt: tokenstream::TokenTree) -> String {
317+
pub fn tt_to_string(tt: &TokenTree) -> String {
318318
to_string(|s| s.print_tt(tt, false))
319319
}
320320

321-
pub fn tts_to_string(tokens: TokenStream) -> String {
321+
pub fn tts_to_string(tokens: &TokenStream) -> String {
322322
to_string(|s| s.print_tts(tokens, false))
323323
}
324324

@@ -585,7 +585,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
585585
false,
586586
None,
587587
delim.to_token(),
588-
tokens.clone(),
588+
tokens,
589589
true,
590590
span,
591591
),
@@ -594,7 +594,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
594594
if let MacArgs::Eq(_, tokens) = &item.args {
595595
self.space();
596596
self.word_space("=");
597-
self.print_tts(tokens.clone(), true);
597+
self.print_tts(tokens, true);
598598
}
599599
}
600600
}
@@ -635,9 +635,9 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
635635
/// appropriate macro, transcribe back into the grammar we just parsed from,
636636
/// and then pretty-print the resulting AST nodes (so, e.g., we print
637637
/// expression arguments as expressions). It can be done! I think.
638-
fn print_tt(&mut self, tt: tokenstream::TokenTree, convert_dollar_crate: bool) {
638+
fn print_tt(&mut self, tt: &TokenTree, convert_dollar_crate: bool) {
639639
match tt {
640-
TokenTree::Token(ref token) => {
640+
TokenTree::Token(token) => {
641641
self.word(token_to_string_ext(&token, convert_dollar_crate));
642642
if let token::DocComment(..) = token.kind {
643643
self.hardbreak()
@@ -648,7 +648,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
648648
None,
649649
false,
650650
None,
651-
delim,
651+
*delim,
652652
tts,
653653
convert_dollar_crate,
654654
dspan.entire(),
@@ -657,14 +657,14 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
657657
}
658658
}
659659

660-
fn print_tts(&mut self, tts: tokenstream::TokenStream, convert_dollar_crate: bool) {
661-
let mut iter = tts.into_trees().peekable();
660+
fn print_tts(&mut self, tts: &TokenStream, convert_dollar_crate: bool) {
661+
let mut iter = tts.trees().peekable();
662662
while let Some(tt) = iter.next() {
663-
let show_space =
664-
if let Some(next) = iter.peek() { tt_prepend_space(next, &tt) } else { false };
665-
self.print_tt(tt, convert_dollar_crate);
666-
if show_space {
667-
self.space();
663+
self.print_tt(&tt, convert_dollar_crate);
664+
if let Some(next) = iter.peek() {
665+
if tt_prepend_space(next, &tt) {
666+
self.space();
667+
}
668668
}
669669
}
670670
}
@@ -675,7 +675,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
675675
has_bang: bool,
676676
ident: Option<Ident>,
677677
delim: DelimToken,
678-
tts: TokenStream,
678+
tts: &TokenStream,
679679
convert_dollar_crate: bool,
680680
span: Span,
681681
) {
@@ -1253,7 +1253,7 @@ impl<'a> State<'a> {
12531253
has_bang,
12541254
Some(item.ident),
12551255
macro_def.body.delim(),
1256-
macro_def.body.inner_tokens(),
1256+
&macro_def.body.inner_tokens(),
12571257
true,
12581258
item.span,
12591259
);
@@ -1577,7 +1577,7 @@ impl<'a> State<'a> {
15771577
true,
15781578
None,
15791579
m.args.delim(),
1580-
m.args.inner_tokens(),
1580+
&m.args.inner_tokens(),
15811581
true,
15821582
m.span(),
15831583
);

src/librustc_builtin_macros/log_syntax.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ pub fn expand_log_syntax<'cx>(
77
sp: rustc_span::Span,
88
tts: TokenStream,
99
) -> Box<dyn base::MacResult + 'cx> {
10-
println!("{}", pprust::tts_to_string(tts));
10+
println!("{}", pprust::tts_to_string(&tts));
1111

1212
// any so that `log_syntax` can be invoked as an expression and item.
1313
base::DummyResult::any_valid(sp)

src/librustc_builtin_macros/source_util.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ pub fn expand_stringify(
7171
tts: TokenStream,
7272
) -> Box<dyn base::MacResult + 'static> {
7373
let sp = cx.with_def_site_ctxt(sp);
74-
let s = pprust::tts_to_string(tts);
74+
let s = pprust::tts_to_string(&tts);
7575
base::MacEager::expr(cx.expr_str(sp, Symbol::intern(&s)))
7676
}
7777

src/librustc_expand/mbe/macro_rules.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ fn generic_extension<'cx>(
224224
let sess = cx.parse_sess;
225225

226226
if cx.trace_macros() {
227-
let msg = format!("expanding `{}! {{ {} }}`", name, pprust::tts_to_string(arg.clone()));
227+
let msg = format!("expanding `{}! {{ {} }}`", name, pprust::tts_to_string(&arg));
228228
trace_macros_note(&mut cx.expansions, sp, msg);
229229
}
230230

@@ -300,7 +300,7 @@ fn generic_extension<'cx>(
300300
}
301301

302302
if cx.trace_macros() {
303-
let msg = format!("to `{}`", pprust::tts_to_string(tts.clone()));
303+
let msg = format!("to `{}`", pprust::tts_to_string(&tts));
304304
trace_macros_note(&mut cx.expansions, sp, msg);
305305
}
306306

src/librustc_expand/proc_macro_server.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ impl server::TokenStream for Rustc<'_> {
413413
)
414414
}
415415
fn to_string(&mut self, stream: &Self::TokenStream) -> String {
416-
pprust::tts_to_string(stream.clone())
416+
pprust::tts_to_string(stream)
417417
}
418418
fn from_token_tree(
419419
&mut self,

0 commit comments

Comments
 (0)