From e21d101c45022ab34d5ad10605a16d6ce8116c94 Mon Sep 17 00:00:00 2001 From: Kevin Per Date: Sat, 4 Apr 2020 07:55:07 +0000 Subject: [PATCH] Show open and closed braces of last proper block --- src/librustc_parse/lexer/tokentrees.rs | 48 +++++++++++++++---- .../issue-70583-block-is-empty-1.stderr | 6 ++- .../macro-mismatched-delim-paren-brace.stderr | 5 ++ .../mismatched-delim-brace-empty-block.stderr | 6 +++ 4 files changed, 54 insertions(+), 11 deletions(-) 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