Skip to content

Commit e21d101

Browse files
committed
Show open and closed braces of last proper block
1 parent d5cba6c commit e21d101

File tree

4 files changed

+54
-11
lines changed

4 files changed

+54
-11
lines changed

src/librustc_parse/lexer/tokentrees.rs

+39-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::{StringReader, UnmatchedBrace};
22

3-
use rustc_ast::token::{self, Token};
3+
use rustc_ast::token::{self, Token, DelimToken};
44
use rustc_ast::tokenstream::{
55
DelimSpan,
66
IsJoint::{self, *},
@@ -44,7 +44,7 @@ struct TokenTreesReader<'a> {
4444
/// Collect empty block spans that might have been auto-inserted by editors.
4545
last_delim_empty_block_spans: FxHashMap<token::DelimToken, Span>,
4646
/// Collect the spans of braces (Open, Close). Used only
47-
/// for detecting if blocks are empty
47+
/// for detecting if blocks are empty and only braces.
4848
matching_block_spans: Vec<(Span, Span)>,
4949
}
5050

@@ -151,7 +151,13 @@ impl<'a> TokenTreesReader<'a> {
151151
}
152152
}
153153

154-
self.matching_block_spans.push((open_brace_span, close_brace_span));
154+
match (open_brace, delim) {
155+
//only add braces
156+
(DelimToken::Brace, DelimToken::Brace) => {
157+
self.matching_block_spans.push((open_brace_span, close_brace_span));
158+
}
159+
_ => {}
160+
}
155161

156162
if self.open_braces.is_empty() {
157163
// Clear up these spans to avoid suggesting them as we've found
@@ -232,18 +238,42 @@ impl<'a> TokenTreesReader<'a> {
232238
let mut err =
233239
self.string_reader.sess.span_diagnostic.struct_span_err(self.token.span, &msg);
234240

235-
if let Some(span) = self.last_delim_empty_block_spans.remove(&delim) {
236241
// Braces are added at the end, so the last element is the biggest block
237242
if let Some(parent) = self.matching_block_spans.last() {
238-
// Check if the (empty block) is in the last properly closed block
239-
if (parent.0.to(parent.1)).contains(span) {
243+
244+
if let Some(span) = self.last_delim_empty_block_spans.remove(&delim) {
245+
// Check if the (empty block) is in the last properly closed block
246+
if (parent.0.to(parent.1)).contains(span) {
247+
err.span_label(
248+
span,
249+
"this block is empty, you might have not meant to close it",
250+
);
251+
}
252+
else {
253+
err.span_label(
254+
parent.0,
255+
"this opening brace...",
256+
);
257+
258+
err.span_label(
259+
parent.1,
260+
"...matches this closing brace",
261+
);
262+
}
263+
}
264+
else {
265+
err.span_label(
266+
parent.0,
267+
"this opening brace...",
268+
);
269+
240270
err.span_label(
241-
span,
242-
"this block is empty, you might have not meant to close it",
271+
parent.1,
272+
"...matches this closing brace",
243273
);
244274
}
245-
}
246275
}
276+
247277
err.span_label(self.token.span, "unexpected closing delimiter");
248278
Err(err)
249279
}

src/test/ui/parser/issue-70583-block-is-empty-1.stderr

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
error: unexpected closing delimiter: `}`
22
--> $DIR/issue-70583-block-is-empty-1.rs:20:1
33
|
4-
LL | ErrorHandled::Reported => {}
5-
| -- this block is empty, you might have not meant to close it
4+
LL | fn struct_generic(x: Vec<i32>) {
5+
| - this opening brace...
66
...
7+
LL | }
8+
| - ...matches this closing brace
79
LL | }
810
| ^ unexpected closing delimiter
911

src/test/ui/parser/macro-mismatched-delim-paren-brace.stderr

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
error: unexpected closing delimiter: `}`
22
--> $DIR/macro-mismatched-delim-paren-brace.rs:5:1
33
|
4+
LL | fn main() {
5+
| - this opening brace...
6+
...
7+
LL | }
8+
| - ...matches this closing brace
49
LL | }
510
| ^ unexpected closing delimiter
611

src/test/ui/parser/mismatched-delim-brace-empty-block.stderr

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
error: unexpected closing delimiter: `}`
22
--> $DIR/mismatched-delim-brace-empty-block.rs:5:1
33
|
4+
LL | fn main() {
5+
| - this opening brace...
6+
LL |
7+
LL | }
8+
| - ...matches this closing brace
9+
LL | let _ = ();
410
LL | }
511
| ^ unexpected closing delimiter
612

0 commit comments

Comments
 (0)