Auto merge of #114115 - nnethercote:less-token-tree-cloning, r=petrochenkov

Less `TokenTree` cloning

`TokenTreeCursor` has this comment on it:
```
// FIXME: Many uses of this can be replaced with by-reference iterator to avoid clones.
```
This PR completes that FIXME. It doesn't have much perf effect, but at least we now know that.

r? `@petrochenkov`
This commit is contained in:
bors 2023-07-28 01:21:27 +00:00
commit 1f369f8b6a

View File

@ -13,7 +13,7 @@
use std::panic::{catch_unwind, AssertUnwindSafe}; use std::panic::{catch_unwind, AssertUnwindSafe};
use rustc_ast::token::{BinOpToken, Delimiter, Token, TokenKind}; use rustc_ast::token::{BinOpToken, Delimiter, Token, TokenKind};
use rustc_ast::tokenstream::{TokenStream, TokenTree, TokenTreeCursor}; use rustc_ast::tokenstream::{RefTokenTreeCursor, TokenStream, TokenTree};
use rustc_ast::{ast, ptr}; use rustc_ast::{ast, ptr};
use rustc_ast_pretty::pprust; use rustc_ast_pretty::pprust;
use rustc_span::{ use rustc_span::{
@ -394,7 +394,7 @@ pub(crate) fn rewrite_macro_def(
} }
let ts = def.body.tokens.clone(); let ts = def.body.tokens.clone();
let mut parser = MacroParser::new(ts.into_trees()); let mut parser = MacroParser::new(ts.trees());
let parsed_def = match parser.parse() { let parsed_def = match parser.parse() {
Some(def) => def, Some(def) => def,
None => return snippet, None => return snippet,
@ -736,9 +736,9 @@ fn add_other(&mut self) {
self.buf.clear(); self.buf.clear();
} }
fn add_meta_variable(&mut self, iter: &mut TokenTreeCursor) -> Option<()> { fn add_meta_variable(&mut self, iter: &mut RefTokenTreeCursor<'_>) -> Option<()> {
match iter.next() { match iter.next() {
Some(TokenTree::Token( Some(&TokenTree::Token(
Token { Token {
kind: TokenKind::Ident(name, _), kind: TokenKind::Ident(name, _),
.. ..
@ -768,7 +768,7 @@ fn add_repeat(
&mut self, &mut self,
inner: Vec<ParsedMacroArg>, inner: Vec<ParsedMacroArg>,
delim: Delimiter, delim: Delimiter,
iter: &mut TokenTreeCursor, iter: &mut RefTokenTreeCursor<'_>,
) -> Option<()> { ) -> Option<()> {
let mut buffer = String::new(); let mut buffer = String::new();
let mut first = true; let mut first = true;
@ -868,11 +868,11 @@ fn need_space_prefix(&self) -> bool {
/// Returns a collection of parsed macro def's arguments. /// Returns a collection of parsed macro def's arguments.
fn parse(mut self, tokens: TokenStream) -> Option<Vec<ParsedMacroArg>> { fn parse(mut self, tokens: TokenStream) -> Option<Vec<ParsedMacroArg>> {
let mut iter = tokens.into_trees(); let mut iter = tokens.trees();
while let Some(tok) = iter.next() { while let Some(tok) = iter.next() {
match tok { match tok {
TokenTree::Token( &TokenTree::Token(
Token { Token {
kind: TokenKind::Dollar, kind: TokenKind::Dollar,
span, span,
@ -901,7 +901,7 @@ fn parse(mut self, tokens: TokenStream) -> Option<Vec<ParsedMacroArg>> {
self.add_meta_variable(&mut iter)?; self.add_meta_variable(&mut iter)?;
} }
TokenTree::Token(ref t, _) => self.update_buffer(t), TokenTree::Token(ref t, _) => self.update_buffer(t),
TokenTree::Delimited(_delimited_span, delimited, ref tts) => { &TokenTree::Delimited(_delimited_span, delimited, ref tts) => {
if !self.buf.is_empty() { if !self.buf.is_empty() {
if next_space(&self.last_tok.kind) == SpaceState::Always { if next_space(&self.last_tok.kind) == SpaceState::Always {
self.add_separator(); self.add_separator();
@ -1119,12 +1119,12 @@ pub(crate) fn macro_style(mac: &ast::MacCall, context: &RewriteContext<'_>) -> D
// A very simple parser that just parses a macros 2.0 definition into its branches. // A very simple parser that just parses a macros 2.0 definition into its branches.
// Currently we do not attempt to parse any further than that. // Currently we do not attempt to parse any further than that.
struct MacroParser { struct MacroParser<'a> {
toks: TokenTreeCursor, toks: RefTokenTreeCursor<'a>,
} }
impl MacroParser { impl<'a> MacroParser<'a> {
const fn new(toks: TokenTreeCursor) -> Self { const fn new(toks: RefTokenTreeCursor<'a>) -> Self {
Self { toks } Self { toks }
} }
@ -1143,9 +1143,9 @@ fn parse_branch(&mut self) -> Option<MacroBranch> {
let tok = self.toks.next()?; let tok = self.toks.next()?;
let (lo, args_paren_kind) = match tok { let (lo, args_paren_kind) = match tok {
TokenTree::Token(..) => return None, TokenTree::Token(..) => return None,
TokenTree::Delimited(delimited_span, d, _) => (delimited_span.open.lo(), d), &TokenTree::Delimited(delimited_span, d, _) => (delimited_span.open.lo(), d),
}; };
let args = TokenStream::new(vec![tok]); let args = TokenStream::new(vec![tok.clone()]);
match self.toks.next()? { match self.toks.next()? {
TokenTree::Token( TokenTree::Token(
Token { Token {