From 52bc90c86f55a045f11665d78e93be8efe64b6a7 Mon Sep 17 00:00:00 2001 From: yukang Date: Fri, 19 Jul 2024 14:58:16 +0800 Subject: [PATCH] add limit for unclosed delimiters in lexer diagnostic --- compiler/rustc_parse/src/lexer/tokentrees.rs | 21 ++++++++++++--- tests/ui/parser/brace-in-let-chain.stderr | 8 +----- ...ched-delimiter-corner-case-issue-127868.rs | 3 +-- ...-delimiter-corner-case-issue-127868.stderr | 27 +++++-------------- 4 files changed, 26 insertions(+), 33 deletions(-) diff --git a/compiler/rustc_parse/src/lexer/tokentrees.rs b/compiler/rustc_parse/src/lexer/tokentrees.rs index 8e54345469133..fc6257d809062 100644 --- a/compiler/rustc_parse/src/lexer/tokentrees.rs +++ b/compiler/rustc_parse/src/lexer/tokentrees.rs @@ -72,16 +72,31 @@ impl<'psess, 'src> TokenTreesReader<'psess, 'src> { fn eof_err(&mut self) -> PErr<'psess> { let msg = "this file contains an unclosed delimiter"; let mut err = self.string_reader.dcx().struct_span_err(self.token.span, msg); - for &(_, sp) in &self.diag_info.open_braces { - err.span_label(sp, "unclosed delimiter"); + + let unclosed_delimiter_show_limit = 5; + let len = usize::min(unclosed_delimiter_show_limit, self.diag_info.open_braces.len()); + for &(_, span) in &self.diag_info.open_braces[..len] { + err.span_label(span, "unclosed delimiter"); self.diag_info.unmatched_delims.push(UnmatchedDelim { found_delim: None, found_span: self.token.span, - unclosed_span: Some(sp), + unclosed_span: Some(span), candidate_span: None, }); } + if let Some((_, span)) = self.diag_info.open_braces.get(unclosed_delimiter_show_limit) + && self.diag_info.open_braces.len() >= unclosed_delimiter_show_limit + 2 + { + err.span_label( + *span, + format!( + "another {} unclosed delimiters begin from here", + self.diag_info.open_braces.len() - unclosed_delimiter_show_limit + ), + ); + } + if let Some((delim, _)) = self.diag_info.open_braces.last() { report_suspicious_mismatch_block( &mut err, diff --git a/tests/ui/parser/brace-in-let-chain.stderr b/tests/ui/parser/brace-in-let-chain.stderr index 7182d86d001d0..44cdde8cd2a4e 100644 --- a/tests/ui/parser/brace-in-let-chain.stderr +++ b/tests/ui/parser/brace-in-let-chain.stderr @@ -17,14 +17,8 @@ LL | fn qux() { | - unclosed delimiter ... LL | fn foo() { - | - unclosed delimiter -... -LL | fn bar() { - | - unclosed delimiter + | - another 3 unclosed delimiters begin from here ... -LL | fn baz() { - | - unclosed delimiter -LL | if false { LL | { | - this delimiter might not be properly closed... LL | && let () = () diff --git a/tests/ui/parser/mismatched-delimiter-corner-case-issue-127868.rs b/tests/ui/parser/mismatched-delimiter-corner-case-issue-127868.rs index edf619664e32d..5dcaa2663252b 100644 --- a/tests/ui/parser/mismatched-delimiter-corner-case-issue-127868.rs +++ b/tests/ui/parser/mismatched-delimiter-corner-case-issue-127868.rs @@ -1,7 +1,6 @@ -// ignore-tidy-trailing-newlines // issue: rust-lang/rust#127868 fn main() { let a = [[[[[[[[[[[[[[[[[[[[1, {, (, [,; } //~ ERROR mismatched closing delimiter: `}` -//~ ERROR this file contains an unclosed delimiter \ No newline at end of file +//~ ERROR this file contains an unclosed delimiter diff --git a/tests/ui/parser/mismatched-delimiter-corner-case-issue-127868.stderr b/tests/ui/parser/mismatched-delimiter-corner-case-issue-127868.stderr index 95e2bf916425a..a019eed81a5e2 100644 --- a/tests/ui/parser/mismatched-delimiter-corner-case-issue-127868.stderr +++ b/tests/ui/parser/mismatched-delimiter-corner-case-issue-127868.stderr @@ -1,5 +1,5 @@ error: mismatched closing delimiter: `}` - --> $DIR/mismatched-delimiter-corner-case-issue-127868.rs:5:42 + --> $DIR/mismatched-delimiter-corner-case-issue-127868.rs:4:42 | LL | fn main() { | - closing delimiter possibly meant for this @@ -9,29 +9,14 @@ LL | } | ^ mismatched closing delimiter error: this file contains an unclosed delimiter - --> $DIR/mismatched-delimiter-corner-case-issue-127868.rs:7:51 + --> $DIR/mismatched-delimiter-corner-case-issue-127868.rs:6:52 | LL | fn main() { | - unclosed delimiter LL | let a = [[[[[[[[[[[[[[[[[[[[1, {, (, [,; - | -------------------- - this delimiter might not be properly closed... - | |||||||||||||||||||| - | |||||||||||||||||||unclosed delimiter - | ||||||||||||||||||unclosed delimiter - | |||||||||||||||||unclosed delimiter - | ||||||||||||||||unclosed delimiter - | |||||||||||||||unclosed delimiter - | ||||||||||||||unclosed delimiter - | |||||||||||||unclosed delimiter - | ||||||||||||unclosed delimiter - | |||||||||||unclosed delimiter - | ||||||||||unclosed delimiter - | |||||||||unclosed delimiter - | ||||||||unclosed delimiter - | |||||||unclosed delimiter - | ||||||unclosed delimiter - | |||||unclosed delimiter - | ||||unclosed delimiter + | ----- - this delimiter might not be properly closed... + | ||||| + | ||||another 16 unclosed delimiters begin from here | |||unclosed delimiter | ||unclosed delimiter | |unclosed delimiter @@ -39,7 +24,7 @@ LL | let a = [[[[[[[[[[[[[[[[[[[[1, {, (, [,; LL | } | - ...as it matches this but it has different indentation LL | - | ^ + | ^ error: aborting due to 2 previous errors