From 014a56ca9c0ad4b0ab1e377a66236242b4329413 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Sun, 9 Sep 2018 14:55:46 -0700 Subject: [PATCH] Don't compute padding of braces unless they are unmatched --- src/libsyntax/parse/lexer/mod.rs | 6 ++-- src/libsyntax/parse/lexer/tokentrees.rs | 43 ++++++++++++------------- 2 files changed, 23 insertions(+), 26 deletions(-) diff --git a/src/libsyntax/parse/lexer/mod.rs b/src/libsyntax/parse/lexer/mod.rs index b7e8a880e7e50..aa47d5bf669bc 100644 --- a/src/libsyntax/parse/lexer/mod.rs +++ b/src/libsyntax/parse/lexer/mod.rs @@ -66,10 +66,10 @@ pub struct StringReader<'a> { /// The raw source span which *does not* take `override_span` into account span_src_raw: Span, open_braces: Vec<(token::DelimToken, Span)>, - /// The type and spans for all braces that have different indentation. + /// The type and spans for all braces /// /// Used only for error recovery when arriving to EOF with mismatched braces. - suspicious_open_spans: Vec<(token::DelimToken, Span, Span)>, + matching_delim_spans: Vec<(token::DelimToken, Span, Span)>, crate override_span: Option, last_unclosed_found_span: Option, } @@ -220,7 +220,7 @@ impl<'a> StringReader<'a> { span: syntax_pos::DUMMY_SP, span_src_raw: syntax_pos::DUMMY_SP, open_braces: Vec::new(), - suspicious_open_spans: Vec::new(), + matching_delim_spans: Vec::new(), override_span, last_unclosed_found_span: None, } diff --git a/src/libsyntax/parse/lexer/tokentrees.rs b/src/libsyntax/parse/lexer/tokentrees.rs index 9e08fa232f170..8047ab0146514 100644 --- a/src/libsyntax/parse/lexer/tokentrees.rs +++ b/src/libsyntax/parse/lexer/tokentrees.rs @@ -44,6 +44,7 @@ impl<'a> StringReader<'a> { } fn parse_token_tree(&mut self) -> PResult<'a, TokenStream> { + let sm = self.sess.source_map(); match self.token { token::Eof => { let msg = "this file contains an un-closed delimiter"; @@ -53,20 +54,25 @@ impl<'a> StringReader<'a> { } if let Some((delim, _)) = self.open_braces.last() { - if let Some((d, open_sp, close_sp)) = self.suspicious_open_spans.iter() - .filter(|(d, _, _)| delim == d) - .next() // these are in reverse order as they get inserted on close, but - { // we want the last open/first close - if d == delim { - err.span_label( - *open_sp, - "this delimiter might not be properly closed...", - ); - err.span_label( - *close_sp, - "...as it matches this but it has different indentation", - ); + if let Some((_, open_sp, close_sp)) = self.matching_delim_spans.iter() + .filter(|(d, open_sp, close_sp)| { + + if let Some(close_padding) = sm.span_to_margin(*close_sp) { + if let Some(open_padding) = sm.span_to_margin(*open_sp) { + return delim == d && close_padding != open_padding; + } } + false + }).next() // these are in reverse order as they get inserted on close, but + { // we want the last open/first close + err.span_label( + *open_sp, + "this delimiter might not be properly closed...", + ); + err.span_label( + *close_sp, + "...as it matches this but it has different indentation", + ); } } Err(err) @@ -87,20 +93,11 @@ impl<'a> StringReader<'a> { // Expand to cover the entire delimited token tree let delim_span = DelimSpan::from_pair(pre_span, self.span); - let sm = self.sess.source_map(); match self.token { // Correct delimiter. token::CloseDelim(d) if d == delim => { let (open_brace, open_brace_span) = self.open_braces.pop().unwrap(); - if let Some(current_padding) = sm.span_to_margin(self.span) { - if let Some(padding) = sm.span_to_margin(open_brace_span) { - if current_padding != padding { - self.suspicious_open_spans.push( - (open_brace, open_brace_span, self.span), - ); - } - } - } + self.matching_delim_spans.push((open_brace, open_brace_span, self.span)); // Parse the close delimiter. self.real_token(); }