From fcdbd214fd4fec2e5455f3864712f4166f092969 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 | 20 +++++++++++++++--- ...-delimiter-corner-case-issue-127868.stderr | 21 +++---------------- 2 files changed, 20 insertions(+), 21 deletions(-) diff --git a/compiler/rustc_parse/src/lexer/tokentrees.rs b/compiler/rustc_parse/src/lexer/tokentrees.rs index 8e54345469133..bfe0947769886 100644 --- a/compiler/rustc_parse/src/lexer/tokentrees.rs +++ b/compiler/rustc_parse/src/lexer/tokentrees.rs @@ -72,16 +72,30 @@ 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_limits = 5; + for &(_, span) in &self.diag_info.open_braces[..unclosed_delimiter_show_limits] { + 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_limits) + && self.diag_info.open_braces.len() >= unclosed_delimiter_show_limits + 2 + { + err.span_label( + *span, + format!( + "another {} unclosed delimiters begin from here", + self.diag_info.open_braces.len() - unclosed_delimiter_show_limits + ), + ); + } + if let Some((delim, _)) = self.diag_info.open_braces.last() { report_suspicious_mismatch_block( &mut err, 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..b3f9b7c8c8062 100644 --- a/tests/ui/parser/mismatched-delimiter-corner-case-issue-127868.stderr +++ b/tests/ui/parser/mismatched-delimiter-corner-case-issue-127868.stderr @@ -14,24 +14,9 @@ error: this file contains an unclosed delimiter 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