Rearrange TokenTreesReader::parse_token_tree
.
`parse_token_tree` is basically a match with four arms: `Eof`, `OpenDelim`, `CloseDelim`, and "other". It has two call sites, and at each call site one of the arms is unreachable. It's also not inlined. This commit removes `parse_token_tree` by splitting it into four functions and inlining them. This avoids some repeated conditional tests and also some non-inlined function calls on the hot path.
This commit is contained in:
parent
f3fafbb006
commit
66e9b1149c
@ -63,7 +63,8 @@ pub mod translation;
|
|||||||
pub use diagnostic_builder::IntoDiagnostic;
|
pub use diagnostic_builder::IntoDiagnostic;
|
||||||
pub use snippet::Style;
|
pub use snippet::Style;
|
||||||
|
|
||||||
pub type PResult<'a, T> = Result<T, DiagnosticBuilder<'a, ErrorGuaranteed>>;
|
pub type PErr<'a> = DiagnosticBuilder<'a, ErrorGuaranteed>;
|
||||||
|
pub type PResult<'a, T> = Result<T, PErr<'a>>;
|
||||||
|
|
||||||
// `PResult` is used a lot. Make sure it doesn't unintentionally get bigger.
|
// `PResult` is used a lot. Make sure it doesn't unintentionally get bigger.
|
||||||
// (See also the comment on `DiagnosticBuilder`'s `diagnostic` field.)
|
// (See also the comment on `DiagnosticBuilder`'s `diagnostic` field.)
|
||||||
|
@ -4,7 +4,7 @@ use rustc_ast::token::{self, Delimiter, Token};
|
|||||||
use rustc_ast::tokenstream::{DelimSpan, Spacing, TokenStream, TokenTree};
|
use rustc_ast::tokenstream::{DelimSpan, Spacing, TokenStream, TokenTree};
|
||||||
use rustc_ast_pretty::pprust::token_to_string;
|
use rustc_ast_pretty::pprust::token_to_string;
|
||||||
use rustc_data_structures::fx::FxHashMap;
|
use rustc_data_structures::fx::FxHashMap;
|
||||||
use rustc_errors::PResult;
|
use rustc_errors::{PErr, PResult};
|
||||||
use rustc_span::Span;
|
use rustc_span::Span;
|
||||||
|
|
||||||
impl<'a> StringReader<'a> {
|
impl<'a> StringReader<'a> {
|
||||||
@ -48,39 +48,36 @@ impl<'a> TokenTreesReader<'a> {
|
|||||||
let mut buf = TokenStreamBuilder::default();
|
let mut buf = TokenStreamBuilder::default();
|
||||||
|
|
||||||
self.bump();
|
self.bump();
|
||||||
while self.token != token::Eof {
|
loop {
|
||||||
buf.push(self.parse_token_tree()?);
|
match self.token.kind {
|
||||||
|
token::OpenDelim(delim) => buf.push(self.parse_token_tree_open_delim(delim)),
|
||||||
|
token::CloseDelim(delim) => return Err(self.close_delim_err(delim)),
|
||||||
|
token::Eof => return Ok(buf.into_token_stream()),
|
||||||
|
_ => buf.push(self.parse_token_tree_other()),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(buf.into_token_stream())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse a stream of tokens into a list of `TokenTree`s, up to a `CloseDelim`.
|
// Parse a stream of tokens into a list of `TokenTree`s, up to a `CloseDelim`.
|
||||||
fn parse_token_trees_until_close_delim(&mut self) -> TokenStream {
|
fn parse_token_trees_until_close_delim(&mut self) -> TokenStream {
|
||||||
let mut buf = TokenStreamBuilder::default();
|
let mut buf = TokenStreamBuilder::default();
|
||||||
loop {
|
loop {
|
||||||
if let token::CloseDelim(..) = self.token.kind {
|
|
||||||
return buf.into_token_stream();
|
|
||||||
}
|
|
||||||
|
|
||||||
match self.parse_token_tree() {
|
|
||||||
Ok(tree) => buf.push(tree),
|
|
||||||
Err(mut e) => {
|
|
||||||
e.emit();
|
|
||||||
return buf.into_token_stream();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn parse_token_tree(&mut self) -> PResult<'a, TokenTree> {
|
|
||||||
let sm = self.string_reader.sess.source_map();
|
|
||||||
|
|
||||||
match self.token.kind {
|
match self.token.kind {
|
||||||
|
token::OpenDelim(delim) => buf.push(self.parse_token_tree_open_delim(delim)),
|
||||||
|
token::CloseDelim(..) => return buf.into_token_stream(),
|
||||||
token::Eof => {
|
token::Eof => {
|
||||||
|
let mut err = self.eof_err();
|
||||||
|
err.emit();
|
||||||
|
return buf.into_token_stream();
|
||||||
|
}
|
||||||
|
_ => buf.push(self.parse_token_tree_other()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn eof_err(&mut self) -> PErr<'a> {
|
||||||
let msg = "this file contains an unclosed delimiter";
|
let msg = "this file contains an unclosed delimiter";
|
||||||
let mut err =
|
let mut err = self.string_reader.sess.span_diagnostic.struct_span_err(self.token.span, msg);
|
||||||
self.string_reader.sess.span_diagnostic.struct_span_err(self.token.span, msg);
|
|
||||||
for &(_, sp) in &self.open_braces {
|
for &(_, sp) in &self.open_braces {
|
||||||
err.span_label(sp, "unclosed delimiter");
|
err.span_label(sp, "unclosed delimiter");
|
||||||
self.unmatched_braces.push(UnmatchedBrace {
|
self.unmatched_braces.push(UnmatchedBrace {
|
||||||
@ -95,6 +92,7 @@ impl<'a> TokenTreesReader<'a> {
|
|||||||
if let Some((delim, _)) = self.open_braces.last() {
|
if let Some((delim, _)) = self.open_braces.last() {
|
||||||
if let Some((_, open_sp, close_sp)) =
|
if let Some((_, open_sp, close_sp)) =
|
||||||
self.matching_delim_spans.iter().find(|(d, open_sp, close_sp)| {
|
self.matching_delim_spans.iter().find(|(d, open_sp, close_sp)| {
|
||||||
|
let sm = self.string_reader.sess.source_map();
|
||||||
if let Some(close_padding) = sm.span_to_margin(*close_sp) {
|
if let Some(close_padding) = sm.span_to_margin(*close_sp) {
|
||||||
if let Some(open_padding) = sm.span_to_margin(*open_sp) {
|
if let Some(open_padding) = sm.span_to_margin(*open_sp) {
|
||||||
return delim == d && close_padding != open_padding;
|
return delim == d && close_padding != open_padding;
|
||||||
@ -106,15 +104,13 @@ impl<'a> TokenTreesReader<'a> {
|
|||||||
{
|
{
|
||||||
// we want the last open/first close
|
// we want the last open/first close
|
||||||
err.span_label(*open_sp, "this delimiter might not be properly closed...");
|
err.span_label(*open_sp, "this delimiter might not be properly closed...");
|
||||||
err.span_label(
|
err.span_label(*close_sp, "...as it matches this but it has different indentation");
|
||||||
*close_sp,
|
|
||||||
"...as it matches this but it has different indentation",
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(err)
|
err
|
||||||
}
|
}
|
||||||
token::OpenDelim(delim) => {
|
|
||||||
|
fn parse_token_tree_open_delim(&mut self, delim: Delimiter) -> TokenTree {
|
||||||
// The span for beginning of the delimited section
|
// The span for beginning of the delimited section
|
||||||
let pre_span = self.token.span;
|
let pre_span = self.token.span;
|
||||||
|
|
||||||
@ -138,6 +134,7 @@ impl<'a> TokenTreesReader<'a> {
|
|||||||
|
|
||||||
if tts.is_empty() {
|
if tts.is_empty() {
|
||||||
let empty_block_span = open_brace_span.to(close_brace_span);
|
let empty_block_span = open_brace_span.to(close_brace_span);
|
||||||
|
let sm = self.string_reader.sess.source_map();
|
||||||
if !sm.is_multiline(empty_block_span) {
|
if !sm.is_multiline(empty_block_span) {
|
||||||
// Only track if the block is in the form of `{}`, otherwise it is
|
// Only track if the block is in the form of `{}`, otherwise it is
|
||||||
// likely that it was written on purpose.
|
// likely that it was written on purpose.
|
||||||
@ -155,11 +152,7 @@ impl<'a> TokenTreesReader<'a> {
|
|||||||
// properly matched delimiters so far for an entire block.
|
// properly matched delimiters so far for an entire block.
|
||||||
self.matching_delim_spans.clear();
|
self.matching_delim_spans.clear();
|
||||||
} else {
|
} else {
|
||||||
self.matching_delim_spans.push((
|
self.matching_delim_spans.push((open_brace, open_brace_span, close_brace_span));
|
||||||
open_brace,
|
|
||||||
open_brace_span,
|
|
||||||
close_brace_span,
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
// Parse the closing delimiter.
|
// Parse the closing delimiter.
|
||||||
self.bump();
|
self.bump();
|
||||||
@ -178,6 +171,7 @@ impl<'a> TokenTreesReader<'a> {
|
|||||||
if let Some(&(_, sp)) = self.open_braces.last() {
|
if let Some(&(_, sp)) = self.open_braces.last() {
|
||||||
unclosed_delimiter = Some(sp);
|
unclosed_delimiter = Some(sp);
|
||||||
};
|
};
|
||||||
|
let sm = self.string_reader.sess.source_map();
|
||||||
if let Some(current_padding) = sm.span_to_margin(self.token.span) {
|
if let Some(current_padding) = sm.span_to_margin(self.token.span) {
|
||||||
for (brace, brace_span) in &self.open_braces {
|
for (brace, brace_span) in &self.open_braces {
|
||||||
if let Some(padding) = sm.span_to_margin(*brace_span) {
|
if let Some(padding) = sm.span_to_margin(*brace_span) {
|
||||||
@ -219,9 +213,10 @@ impl<'a> TokenTreesReader<'a> {
|
|||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(TokenTree::Delimited(delim_span, delim, tts))
|
TokenTree::Delimited(delim_span, delim, tts)
|
||||||
}
|
}
|
||||||
token::CloseDelim(delim) => {
|
|
||||||
|
fn close_delim_err(&mut self, delim: Delimiter) -> PErr<'a> {
|
||||||
// An unexpected closing delimiter (i.e., there is no
|
// An unexpected closing delimiter (i.e., there is no
|
||||||
// matching opening delimiter).
|
// matching opening delimiter).
|
||||||
let token_str = token_to_string(&self.token);
|
let token_str = token_to_string(&self.token);
|
||||||
@ -234,34 +229,29 @@ impl<'a> TokenTreesReader<'a> {
|
|||||||
if let Some(span) = self.last_delim_empty_block_spans.remove(&delim) {
|
if let Some(span) = self.last_delim_empty_block_spans.remove(&delim) {
|
||||||
// Check if the (empty block) is in the last properly closed block
|
// Check if the (empty block) is in the last properly closed block
|
||||||
if (parent.0.to(parent.1)).contains(span) {
|
if (parent.0.to(parent.1)).contains(span) {
|
||||||
err.span_label(
|
err.span_label(span, "block is empty, you might have not meant to close it");
|
||||||
span,
|
|
||||||
"block is empty, you might have not meant to close it",
|
|
||||||
);
|
|
||||||
} else {
|
} else {
|
||||||
err.span_label(parent.0, "this opening brace...");
|
err.span_label(parent.0, "this opening brace...");
|
||||||
|
|
||||||
err.span_label(parent.1, "...matches this closing brace");
|
err.span_label(parent.1, "...matches this closing brace");
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
err.span_label(parent.0, "this opening brace...");
|
err.span_label(parent.0, "this opening brace...");
|
||||||
|
|
||||||
err.span_label(parent.1, "...matches this closing brace");
|
err.span_label(parent.1, "...matches this closing brace");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
err.span_label(self.token.span, "unexpected closing delimiter");
|
err.span_label(self.token.span, "unexpected closing delimiter");
|
||||||
Err(err)
|
err
|
||||||
}
|
}
|
||||||
_ => {
|
|
||||||
|
#[inline]
|
||||||
|
fn parse_token_tree_other(&mut self) -> TokenTree {
|
||||||
let tok = self.token.take();
|
let tok = self.token.take();
|
||||||
let mut spacing = self.bump();
|
let mut spacing = self.bump();
|
||||||
if !self.token.is_op() {
|
if !self.token.is_op() {
|
||||||
spacing = Spacing::Alone;
|
spacing = Spacing::Alone;
|
||||||
}
|
}
|
||||||
Ok(TokenTree::Token(tok, spacing))
|
TokenTree::Token(tok, spacing)
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn bump(&mut self) -> Spacing {
|
fn bump(&mut self) -> Spacing {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user