Show open and closed braces of last proper block
This commit is contained in:
parent
d5cba6c895
commit
e21d101c45
4 changed files with 54 additions and 11 deletions
|
@ -1,6 +1,6 @@
|
||||||
use super::{StringReader, UnmatchedBrace};
|
use super::{StringReader, UnmatchedBrace};
|
||||||
|
|
||||||
use rustc_ast::token::{self, Token};
|
use rustc_ast::token::{self, Token, DelimToken};
|
||||||
use rustc_ast::tokenstream::{
|
use rustc_ast::tokenstream::{
|
||||||
DelimSpan,
|
DelimSpan,
|
||||||
IsJoint::{self, *},
|
IsJoint::{self, *},
|
||||||
|
@ -44,7 +44,7 @@ struct TokenTreesReader<'a> {
|
||||||
/// Collect empty block spans that might have been auto-inserted by editors.
|
/// Collect empty block spans that might have been auto-inserted by editors.
|
||||||
last_delim_empty_block_spans: FxHashMap<token::DelimToken, Span>,
|
last_delim_empty_block_spans: FxHashMap<token::DelimToken, Span>,
|
||||||
/// Collect the spans of braces (Open, Close). Used only
|
/// 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)>,
|
matching_block_spans: Vec<(Span, Span)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -151,7 +151,13 @@ impl<'a> TokenTreesReader<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
match (open_brace, delim) {
|
||||||
|
//only add braces
|
||||||
|
(DelimToken::Brace, DelimToken::Brace) => {
|
||||||
self.matching_block_spans.push((open_brace_span, close_brace_span));
|
self.matching_block_spans.push((open_brace_span, close_brace_span));
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
|
||||||
if self.open_braces.is_empty() {
|
if self.open_braces.is_empty() {
|
||||||
// Clear up these spans to avoid suggesting them as we've found
|
// Clear up these spans to avoid suggesting them as we've found
|
||||||
|
@ -232,9 +238,10 @@ impl<'a> TokenTreesReader<'a> {
|
||||||
let mut err =
|
let mut err =
|
||||||
self.string_reader.sess.span_diagnostic.struct_span_err(self.token.span, &msg);
|
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
|
// Braces are added at the end, so the last element is the biggest block
|
||||||
if let Some(parent) = self.matching_block_spans.last() {
|
if let Some(parent) = self.matching_block_spans.last() {
|
||||||
|
|
||||||
|
if let Some(span) = self.last_delim_empty_block_spans.remove(&delim) {
|
||||||
// Check if the (empty block) is in the last properly closed block
|
// Check if the (empty block) is in the last properly closed block
|
||||||
if (parent.0.to(parent.1)).contains(span) {
|
if (parent.0.to(parent.1)).contains(span) {
|
||||||
err.span_label(
|
err.span_label(
|
||||||
|
@ -242,8 +249,31 @@ impl<'a> TokenTreesReader<'a> {
|
||||||
"this block is empty, you might have not meant to close it",
|
"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(
|
||||||
|
parent.1,
|
||||||
|
"...matches this closing brace",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
err.span_label(self.token.span, "unexpected closing delimiter");
|
err.span_label(self.token.span, "unexpected closing delimiter");
|
||||||
Err(err)
|
Err(err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,11 @@
|
||||||
error: unexpected closing delimiter: `}`
|
error: unexpected closing delimiter: `}`
|
||||||
--> $DIR/issue-70583-block-is-empty-1.rs:20:1
|
--> $DIR/issue-70583-block-is-empty-1.rs:20:1
|
||||||
|
|
|
|
||||||
LL | ErrorHandled::Reported => {}
|
LL | fn struct_generic(x: Vec<i32>) {
|
||||||
| -- this block is empty, you might have not meant to close it
|
| - this opening brace...
|
||||||
...
|
...
|
||||||
|
LL | }
|
||||||
|
| - ...matches this closing brace
|
||||||
LL | }
|
LL | }
|
||||||
| ^ unexpected closing delimiter
|
| ^ unexpected closing delimiter
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,11 @@
|
||||||
error: unexpected closing delimiter: `}`
|
error: unexpected closing delimiter: `}`
|
||||||
--> $DIR/macro-mismatched-delim-paren-brace.rs:5:1
|
--> $DIR/macro-mismatched-delim-paren-brace.rs:5:1
|
||||||
|
|
|
|
||||||
|
LL | fn main() {
|
||||||
|
| - this opening brace...
|
||||||
|
...
|
||||||
|
LL | }
|
||||||
|
| - ...matches this closing brace
|
||||||
LL | }
|
LL | }
|
||||||
| ^ unexpected closing delimiter
|
| ^ unexpected closing delimiter
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,12 @@
|
||||||
error: unexpected closing delimiter: `}`
|
error: unexpected closing delimiter: `}`
|
||||||
--> $DIR/mismatched-delim-brace-empty-block.rs:5:1
|
--> $DIR/mismatched-delim-brace-empty-block.rs:5:1
|
||||||
|
|
|
|
||||||
|
LL | fn main() {
|
||||||
|
| - this opening brace...
|
||||||
|
LL |
|
||||||
|
LL | }
|
||||||
|
| - ...matches this closing brace
|
||||||
|
LL | let _ = ();
|
||||||
LL | }
|
LL | }
|
||||||
| ^ unexpected closing delimiter
|
| ^ unexpected closing delimiter
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue