Skip to content

Commit 42005ab

Browse files
authored
Rollup merge of rust-lang#73812 - petrochenkov:prettyref, r=varkor
ast_pretty: Pass some token streams and trees by reference Salvaged from an intermediate version of rust-lang#73345.
2 parents fa2a99a + 7055c23 commit 42005ab

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;
@@ -305,7 +305,7 @@ pub fn nonterminal_to_string(nt: &Nonterminal) -> String {
305305
token::NtIdent(e, is_raw) => IdentPrinter::for_ast_ident(e, is_raw).to_string(),
306306
token::NtLifetime(e) => e.to_string(),
307307
token::NtLiteral(ref e) => expr_to_string(e),
308-
token::NtTT(ref tree) => tt_to_string(tree.clone()),
308+
token::NtTT(ref tree) => tt_to_string(tree),
309309
token::NtVis(ref e) => vis_to_string(e),
310310
}
311311
}
@@ -326,11 +326,11 @@ pub fn expr_to_string(e: &ast::Expr) -> String {
326326
to_string(|s| s.print_expr(e))
327327
}
328328

329-
pub fn tt_to_string(tt: tokenstream::TokenTree) -> String {
329+
pub fn tt_to_string(tt: &TokenTree) -> String {
330330
to_string(|s| s.print_tt(tt, false))
331331
}
332332

333-
pub fn tts_to_string(tokens: TokenStream) -> String {
333+
pub fn tts_to_string(tokens: &TokenStream) -> String {
334334
to_string(|s| s.print_tts(tokens, false))
335335
}
336336

@@ -597,7 +597,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
597597
false,
598598
None,
599599
delim.to_token(),
600-
tokens.clone(),
600+
tokens,
601601
true,
602602
span,
603603
),
@@ -606,7 +606,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
606606
if let MacArgs::Eq(_, tokens) = &item.args {
607607
self.space();
608608
self.word_space("=");
609-
self.print_tts(tokens.clone(), true);
609+
self.print_tts(tokens, true);
610610
}
611611
}
612612
}
@@ -647,9 +647,9 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
647647
/// appropriate macro, transcribe back into the grammar we just parsed from,
648648
/// and then pretty-print the resulting AST nodes (so, e.g., we print
649649
/// expression arguments as expressions). It can be done! I think.
650-
fn print_tt(&mut self, tt: tokenstream::TokenTree, convert_dollar_crate: bool) {
650+
fn print_tt(&mut self, tt: &TokenTree, convert_dollar_crate: bool) {
651651
match tt {
652-
TokenTree::Token(ref token) => {
652+
TokenTree::Token(token) => {
653653
self.word(token_to_string_ext(&token, convert_dollar_crate));
654654
if let token::DocComment(..) = token.kind {
655655
self.hardbreak()
@@ -660,7 +660,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
660660
None,
661661
false,
662662
None,
663-
delim,
663+
*delim,
664664
tts,
665665
convert_dollar_crate,
666666
dspan.entire(),
@@ -669,14 +669,14 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
669669
}
670670
}
671671

672-
fn print_tts(&mut self, tts: tokenstream::TokenStream, convert_dollar_crate: bool) {
673-
let mut iter = tts.into_trees().peekable();
672+
fn print_tts(&mut self, tts: &TokenStream, convert_dollar_crate: bool) {
673+
let mut iter = tts.trees().peekable();
674674
while let Some(tt) = iter.next() {
675-
let show_space =
676-
if let Some(next) = iter.peek() { tt_prepend_space(next, &tt) } else { false };
677-
self.print_tt(tt, convert_dollar_crate);
678-
if show_space {
679-
self.space();
675+
self.print_tt(&tt, convert_dollar_crate);
676+
if let Some(next) = iter.peek() {
677+
if tt_prepend_space(next, &tt) {
678+
self.space();
679+
}
680680
}
681681
}
682682
}
@@ -687,7 +687,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
687687
has_bang: bool,
688688
ident: Option<Ident>,
689689
delim: DelimToken,
690-
tts: TokenStream,
690+
tts: &TokenStream,
691691
convert_dollar_crate: bool,
692692
span: Span,
693693
) {
@@ -1265,7 +1265,7 @@ impl<'a> State<'a> {
12651265
has_bang,
12661266
Some(item.ident),
12671267
macro_def.body.delim(),
1268-
macro_def.body.inner_tokens(),
1268+
&macro_def.body.inner_tokens(),
12691269
true,
12701270
item.span,
12711271
);
@@ -1589,7 +1589,7 @@ impl<'a> State<'a> {
15891589
true,
15901590
None,
15911591
m.args.delim(),
1592-
m.args.inner_tokens(),
1592+
&m.args.inner_tokens(),
15931593
true,
15941594
m.span(),
15951595
);

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
@@ -408,7 +408,7 @@ impl server::TokenStream for Rustc<'_> {
408408
)
409409
}
410410
fn to_string(&mut self, stream: &Self::TokenStream) -> String {
411-
pprust::tts_to_string(stream.clone())
411+
pprust::tts_to_string(stream)
412412
}
413413
fn from_token_tree(
414414
&mut self,

0 commit comments

Comments
 (0)