Skip to content

Commit

Permalink
Rollup merge of rust-lang#73812 - petrochenkov:prettyref, r=varkor
Browse files Browse the repository at this point in the history
ast_pretty: Pass some token streams and trees by reference

Salvaged from an intermediate version of rust-lang#73345.
  • Loading branch information
Manishearth authored Jul 2, 2020
2 parents fa2a99a + 7055c23 commit 42005ab
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 24 deletions.
38 changes: 19 additions & 19 deletions src/librustc_ast_pretty/pprust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use rustc_ast::ast::{InlineAsmOptions, InlineAsmTemplatePiece};
use rustc_ast::attr;
use rustc_ast::ptr::P;
use rustc_ast::token::{self, BinOpToken, DelimToken, Nonterminal, Token, TokenKind};
use rustc_ast::tokenstream::{self, TokenStream, TokenTree};
use rustc_ast::tokenstream::{TokenStream, TokenTree};
use rustc_ast::util::parser::{self, AssocOp, Fixity};
use rustc_ast::util::{classify, comments};
use rustc_span::edition::Edition;
Expand Down Expand Up @@ -305,7 +305,7 @@ pub fn nonterminal_to_string(nt: &Nonterminal) -> String {
token::NtIdent(e, is_raw) => IdentPrinter::for_ast_ident(e, is_raw).to_string(),
token::NtLifetime(e) => e.to_string(),
token::NtLiteral(ref e) => expr_to_string(e),
token::NtTT(ref tree) => tt_to_string(tree.clone()),
token::NtTT(ref tree) => tt_to_string(tree),
token::NtVis(ref e) => vis_to_string(e),
}
}
Expand All @@ -326,11 +326,11 @@ pub fn expr_to_string(e: &ast::Expr) -> String {
to_string(|s| s.print_expr(e))
}

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

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

Expand Down Expand Up @@ -597,7 +597,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
false,
None,
delim.to_token(),
tokens.clone(),
tokens,
true,
span,
),
Expand All @@ -606,7 +606,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
if let MacArgs::Eq(_, tokens) = &item.args {
self.space();
self.word_space("=");
self.print_tts(tokens.clone(), true);
self.print_tts(tokens, true);
}
}
}
Expand Down Expand Up @@ -647,9 +647,9 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
/// appropriate macro, transcribe back into the grammar we just parsed from,
/// and then pretty-print the resulting AST nodes (so, e.g., we print
/// expression arguments as expressions). It can be done! I think.
fn print_tt(&mut self, tt: tokenstream::TokenTree, convert_dollar_crate: bool) {
fn print_tt(&mut self, tt: &TokenTree, convert_dollar_crate: bool) {
match tt {
TokenTree::Token(ref token) => {
TokenTree::Token(token) => {
self.word(token_to_string_ext(&token, convert_dollar_crate));
if let token::DocComment(..) = token.kind {
self.hardbreak()
Expand All @@ -660,7 +660,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
None,
false,
None,
delim,
*delim,
tts,
convert_dollar_crate,
dspan.entire(),
Expand All @@ -669,14 +669,14 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
}
}

fn print_tts(&mut self, tts: tokenstream::TokenStream, convert_dollar_crate: bool) {
let mut iter = tts.into_trees().peekable();
fn print_tts(&mut self, tts: &TokenStream, convert_dollar_crate: bool) {
let mut iter = tts.trees().peekable();
while let Some(tt) = iter.next() {
let show_space =
if let Some(next) = iter.peek() { tt_prepend_space(next, &tt) } else { false };
self.print_tt(tt, convert_dollar_crate);
if show_space {
self.space();
self.print_tt(&tt, convert_dollar_crate);
if let Some(next) = iter.peek() {
if tt_prepend_space(next, &tt) {
self.space();
}
}
}
}
Expand All @@ -687,7 +687,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
has_bang: bool,
ident: Option<Ident>,
delim: DelimToken,
tts: TokenStream,
tts: &TokenStream,
convert_dollar_crate: bool,
span: Span,
) {
Expand Down Expand Up @@ -1265,7 +1265,7 @@ impl<'a> State<'a> {
has_bang,
Some(item.ident),
macro_def.body.delim(),
macro_def.body.inner_tokens(),
&macro_def.body.inner_tokens(),
true,
item.span,
);
Expand Down Expand Up @@ -1589,7 +1589,7 @@ impl<'a> State<'a> {
true,
None,
m.args.delim(),
m.args.inner_tokens(),
&m.args.inner_tokens(),
true,
m.span(),
);
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_builtin_macros/log_syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub fn expand_log_syntax<'cx>(
sp: rustc_span::Span,
tts: TokenStream,
) -> Box<dyn base::MacResult + 'cx> {
println!("{}", pprust::tts_to_string(tts));
println!("{}", pprust::tts_to_string(&tts));

// any so that `log_syntax` can be invoked as an expression and item.
base::DummyResult::any_valid(sp)
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_builtin_macros/source_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ pub fn expand_stringify(
tts: TokenStream,
) -> Box<dyn base::MacResult + 'static> {
let sp = cx.with_def_site_ctxt(sp);
let s = pprust::tts_to_string(tts);
let s = pprust::tts_to_string(&tts);
base::MacEager::expr(cx.expr_str(sp, Symbol::intern(&s)))
}

Expand Down
4 changes: 2 additions & 2 deletions src/librustc_expand/mbe/macro_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ fn generic_extension<'cx>(
let sess = cx.parse_sess;

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

Expand Down Expand Up @@ -300,7 +300,7 @@ fn generic_extension<'cx>(
}

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

Expand Down
2 changes: 1 addition & 1 deletion src/librustc_expand/proc_macro_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ impl server::TokenStream for Rustc<'_> {
)
}
fn to_string(&mut self, stream: &Self::TokenStream) -> String {
pprust::tts_to_string(stream.clone())
pprust::tts_to_string(stream)
}
fn from_token_tree(
&mut self,
Expand Down

0 comments on commit 42005ab

Please sign in to comment.