Skip to content

Commit

Permalink
Don't compute padding of braces unless they are unmatched
Browse files Browse the repository at this point in the history
  • Loading branch information
estebank committed Sep 9, 2018
1 parent f50b775 commit 014a56c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 26 deletions.
6 changes: 3 additions & 3 deletions src/libsyntax/parse/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Span>,
last_unclosed_found_span: Option<Span>,
}
Expand Down Expand Up @@ -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,
}
Expand Down
43 changes: 20 additions & 23 deletions src/libsyntax/parse/lexer/tokentrees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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)
Expand All @@ -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();
}
Expand Down

0 comments on commit 014a56c

Please sign in to comment.