From 02317542ebceef780ddac660d751cbe268f56105 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Tue, 19 Apr 2022 13:41:02 +1000 Subject: [PATCH] Rearrange `TokenCursor::inlined_next()`. In particular, avoid wrapping a token within `TokenTree::Token` and then immediately matching it and returning the token within. Just return the token immediately. --- compiler/rustc_parse/src/parser/mod.rs | 37 ++++++++++++++------------ 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/compiler/rustc_parse/src/parser/mod.rs b/compiler/rustc_parse/src/parser/mod.rs index b6f4cd119e0..513a95234ab 100644 --- a/compiler/rustc_parse/src/parser/mod.rs +++ b/compiler/rustc_parse/src/parser/mod.rs @@ -262,29 +262,32 @@ fn next(&mut self, desugar_doc_comments: bool) -> (Token, Spacing) { #[inline(always)] fn inlined_next(&mut self, desugar_doc_comments: bool) -> (Token, Spacing) { let (token, spacing) = loop { - let (tree, spacing) = if !self.frame.open_delim { + if !self.frame.open_delim { self.frame.open_delim = true; - TokenTree::token(token::OpenDelim(self.frame.delim), self.frame.span.open).into() - } else if let Some(tree) = self.frame.tree_cursor.next_with_spacing() { - tree + return ( + Token::new(token::OpenDelim(self.frame.delim), self.frame.span.open), + Spacing::Alone, + ); + } else if let Some((tree, spacing)) = self.frame.tree_cursor.next_with_spacing() { + match tree { + TokenTree::Token(token) => { + break (token, spacing); + } + TokenTree::Delimited(sp, delim, tts) => { + let frame = TokenCursorFrame::new(sp, delim, tts); + self.stack.push(mem::replace(&mut self.frame, frame)); + } + } } else if !self.frame.close_delim { self.frame.close_delim = true; - TokenTree::token(token::CloseDelim(self.frame.delim), self.frame.span.close).into() + return ( + Token::new(token::CloseDelim(self.frame.delim), self.frame.span.close), + Spacing::Alone, + ); } else if let Some(frame) = self.stack.pop() { self.frame = frame; - continue; } else { - (TokenTree::Token(Token::new(token::Eof, DUMMY_SP)), Spacing::Alone) - }; - - match tree { - TokenTree::Token(token) => { - break (token, spacing); - } - TokenTree::Delimited(sp, delim, tts) => { - let frame = TokenCursorFrame::new(sp, delim, tts); - self.stack.push(mem::replace(&mut self.frame, frame)); - } + return (Token::new(token::Eof, DUMMY_SP), Spacing::Alone); } };