Skip to content

Commit

Permalink
Show open and closed braces of last proper block
Browse files Browse the repository at this point in the history
  • Loading branch information
kper committed Apr 4, 2020
1 parent d5cba6c commit e21d101
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 11 deletions.
48 changes: 39 additions & 9 deletions src/librustc_parse/lexer/tokentrees.rs
@@ -1,6 +1,6 @@
use super::{StringReader, UnmatchedBrace};

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

Expand Down Expand Up @@ -151,7 +151,13 @@ impl<'a> TokenTreesReader<'a> {
}
}

self.matching_block_spans.push((open_brace_span, close_brace_span));
match (open_brace, delim) {
//only add braces
(DelimToken::Brace, DelimToken::Brace) => {
self.matching_block_spans.push((open_brace_span, close_brace_span));
}
_ => {}
}

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

if let Some(span) = self.last_delim_empty_block_spans.remove(&delim) {
// Braces are added at the end, so the last element is the biggest block
if let Some(parent) = self.matching_block_spans.last() {
// Check if the (empty block) is in the last properly closed block
if (parent.0.to(parent.1)).contains(span) {

if let Some(span) = self.last_delim_empty_block_spans.remove(&delim) {
// Check if the (empty block) is in the last properly closed block
if (parent.0.to(parent.1)).contains(span) {
err.span_label(
span,
"this block is empty, you might have not meant to close it",
);
}
else {
err.span_label(
parent.0,
"this opening brace...",
);

err.span_label(
parent.1,
"...matches this closing brace",
);
}
}
else {
err.span_label(
parent.0,
"this opening brace...",
);

err.span_label(
span,
"this block is empty, you might have not meant to close it",
parent.1,
"...matches this closing brace",
);
}
}
}

err.span_label(self.token.span, "unexpected closing delimiter");
Err(err)
}
Expand Down
6 changes: 4 additions & 2 deletions src/test/ui/parser/issue-70583-block-is-empty-1.stderr
@@ -1,9 +1,11 @@
error: unexpected closing delimiter: `}`
--> $DIR/issue-70583-block-is-empty-1.rs:20:1
|
LL | ErrorHandled::Reported => {}
| -- this block is empty, you might have not meant to close it
LL | fn struct_generic(x: Vec<i32>) {
| - this opening brace...
...
LL | }
| - ...matches this closing brace
LL | }
| ^ unexpected closing delimiter

Expand Down
5 changes: 5 additions & 0 deletions src/test/ui/parser/macro-mismatched-delim-paren-brace.stderr
@@ -1,6 +1,11 @@
error: unexpected closing delimiter: `}`
--> $DIR/macro-mismatched-delim-paren-brace.rs:5:1
|
LL | fn main() {
| - this opening brace...
...
LL | }
| - ...matches this closing brace
LL | }
| ^ unexpected closing delimiter

Expand Down
6 changes: 6 additions & 0 deletions src/test/ui/parser/mismatched-delim-brace-empty-block.stderr
@@ -1,6 +1,12 @@
error: unexpected closing delimiter: `}`
--> $DIR/mismatched-delim-brace-empty-block.rs:5:1
|
LL | fn main() {
| - this opening brace...
LL |
LL | }
| - ...matches this closing brace
LL | let _ = ();
LL | }
| ^ unexpected closing delimiter

Expand Down

0 comments on commit e21d101

Please sign in to comment.