Skip to content

Commit d6720cc

Browse files
committed
Auto merge of #54092 - estebank:gotta-go-fast, r=nikomatsakis
Don't compute padding of braces unless they are unmatched Follow up to #53949. Attempt to fix # #54083. r? @nikomatsakis
2 parents 551244f + 014a56c commit d6720cc

File tree

2 files changed

+23
-26
lines changed

2 files changed

+23
-26
lines changed

src/libsyntax/parse/lexer/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ pub struct StringReader<'a> {
6666
/// The raw source span which *does not* take `override_span` into account
6767
span_src_raw: Span,
6868
open_braces: Vec<(token::DelimToken, Span)>,
69-
/// The type and spans for all braces that have different indentation.
69+
/// The type and spans for all braces
7070
///
7171
/// Used only for error recovery when arriving to EOF with mismatched braces.
72-
suspicious_open_spans: Vec<(token::DelimToken, Span, Span)>,
72+
matching_delim_spans: Vec<(token::DelimToken, Span, Span)>,
7373
crate override_span: Option<Span>,
7474
last_unclosed_found_span: Option<Span>,
7575
}
@@ -220,7 +220,7 @@ impl<'a> StringReader<'a> {
220220
span: syntax_pos::DUMMY_SP,
221221
span_src_raw: syntax_pos::DUMMY_SP,
222222
open_braces: Vec::new(),
223-
suspicious_open_spans: Vec::new(),
223+
matching_delim_spans: Vec::new(),
224224
override_span,
225225
last_unclosed_found_span: None,
226226
}

src/libsyntax/parse/lexer/tokentrees.rs

+20-23
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ impl<'a> StringReader<'a> {
4444
}
4545

4646
fn parse_token_tree(&mut self) -> PResult<'a, TokenStream> {
47+
let sm = self.sess.source_map();
4748
match self.token {
4849
token::Eof => {
4950
let msg = "this file contains an un-closed delimiter";
@@ -53,20 +54,25 @@ impl<'a> StringReader<'a> {
5354
}
5455

5556
if let Some((delim, _)) = self.open_braces.last() {
56-
if let Some((d, open_sp, close_sp)) = self.suspicious_open_spans.iter()
57-
.filter(|(d, _, _)| delim == d)
58-
.next() // these are in reverse order as they get inserted on close, but
59-
{ // we want the last open/first close
60-
if d == delim {
61-
err.span_label(
62-
*open_sp,
63-
"this delimiter might not be properly closed...",
64-
);
65-
err.span_label(
66-
*close_sp,
67-
"...as it matches this but it has different indentation",
68-
);
57+
if let Some((_, open_sp, close_sp)) = self.matching_delim_spans.iter()
58+
.filter(|(d, open_sp, close_sp)| {
59+
60+
if let Some(close_padding) = sm.span_to_margin(*close_sp) {
61+
if let Some(open_padding) = sm.span_to_margin(*open_sp) {
62+
return delim == d && close_padding != open_padding;
63+
}
6964
}
65+
false
66+
}).next() // these are in reverse order as they get inserted on close, but
67+
{ // we want the last open/first close
68+
err.span_label(
69+
*open_sp,
70+
"this delimiter might not be properly closed...",
71+
);
72+
err.span_label(
73+
*close_sp,
74+
"...as it matches this but it has different indentation",
75+
);
7076
}
7177
}
7278
Err(err)
@@ -87,20 +93,11 @@ impl<'a> StringReader<'a> {
8793
// Expand to cover the entire delimited token tree
8894
let delim_span = DelimSpan::from_pair(pre_span, self.span);
8995

90-
let sm = self.sess.source_map();
9196
match self.token {
9297
// Correct delimiter.
9398
token::CloseDelim(d) if d == delim => {
9499
let (open_brace, open_brace_span) = self.open_braces.pop().unwrap();
95-
if let Some(current_padding) = sm.span_to_margin(self.span) {
96-
if let Some(padding) = sm.span_to_margin(open_brace_span) {
97-
if current_padding != padding {
98-
self.suspicious_open_spans.push(
99-
(open_brace, open_brace_span, self.span),
100-
);
101-
}
102-
}
103-
}
100+
self.matching_delim_spans.push((open_brace, open_brace_span, self.span));
104101
// Parse the close delimiter.
105102
self.real_token();
106103
}

0 commit comments

Comments
 (0)