Skip to content

Commit 639e449

Browse files
authored
Rollup merge of rust-lang#69708 - estebank:tiny, r=petrochenkov
On mismatched delimiters, only point at empty blocks that are in the same line We point at empty blocks when we have mismatched braces to detect cases where editors auto insert `}` after writing `{`. Gate this to only the case where the entire span is in the same line so we never point at explicitly empty blocks.
2 parents d97d99f + 81f435d commit 639e449

File tree

2 files changed

+8
-9
lines changed

2 files changed

+8
-9
lines changed

src/librustc_parse/lexer/tokentrees.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ struct TokenTreesReader<'a> {
4040
/// Used only for error recovery when arriving to EOF with mismatched braces.
4141
matching_delim_spans: Vec<(token::DelimToken, Span, Span)>,
4242
last_unclosed_found_span: Option<Span>,
43+
/// Collect empty block spans that might have been auto-inserted by editors.
4344
last_delim_empty_block_spans: FxHashMap<token::DelimToken, Span>,
4445
}
4546

@@ -138,7 +139,11 @@ impl<'a> TokenTreesReader<'a> {
138139

139140
if tts.is_empty() {
140141
let empty_block_span = open_brace_span.to(close_brace_span);
141-
self.last_delim_empty_block_spans.insert(delim, empty_block_span);
142+
if !sm.is_multiline(empty_block_span) {
143+
// Only track if the block is in the form of `{}`, otherwise it is
144+
// likely that it was written on purpose.
145+
self.last_delim_empty_block_spans.insert(delim, empty_block_span);
146+
}
142147
}
143148

144149
if self.open_braces.is_empty() {
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
error: unexpected closing delimiter: `}`
22
--> $DIR/mismatched-delim-brace-empty-block.rs:5:1
33
|
4-
LL | fn main() {
5-
| ___________-
6-
LL | |
7-
LL | | }
8-
| |_- this block is empty, you might have not meant to close it
9-
LL | let _ = ();
10-
LL | }
11-
| ^ unexpected closing delimiter
4+
LL | }
5+
| ^ unexpected closing delimiter
126

137
error: aborting due to previous error
148

0 commit comments

Comments
 (0)