diff --git a/src/librustc_parse/lexer/tokentrees.rs b/src/librustc_parse/lexer/tokentrees.rs index e7bb8c7cc14..09bbfe10fe5 100644 --- a/src/librustc_parse/lexer/tokentrees.rs +++ b/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, *}, @@ -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, /// 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)>, } @@ -151,7 +151,13 @@ fn parse_token_tree(&mut self) -> PResult<'a, TreeAndJoint> { } } - 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 @@ -232,18 +238,42 @@ fn parse_token_tree(&mut self) -> PResult<'a, TreeAndJoint> { 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( - span, - "this block is empty, you might have not meant to close it", + parent.0, + "this opening brace...", + ); + + err.span_label( + parent.1, + "...matches this closing brace", ); } - } } + err.span_label(self.token.span, "unexpected closing delimiter"); Err(err) } diff --git a/src/test/ui/parser/issue-70583-block-is-empty-1.stderr b/src/test/ui/parser/issue-70583-block-is-empty-1.stderr index f4bbe9e86e9..39bf113ef83 100644 --- a/src/test/ui/parser/issue-70583-block-is-empty-1.stderr +++ b/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) { + | - this opening brace... ... +LL | } + | - ...matches this closing brace LL | } | ^ unexpected closing delimiter diff --git a/src/test/ui/parser/macro-mismatched-delim-paren-brace.stderr b/src/test/ui/parser/macro-mismatched-delim-paren-brace.stderr index 042142ac350..424c7a60c19 100644 --- a/src/test/ui/parser/macro-mismatched-delim-paren-brace.stderr +++ b/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 diff --git a/src/test/ui/parser/mismatched-delim-brace-empty-block.stderr b/src/test/ui/parser/mismatched-delim-brace-empty-block.stderr index f1be5dc5ba7..c871e549c9e 100644 --- a/src/test/ui/parser/mismatched-delim-brace-empty-block.stderr +++ b/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