Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add limit for unclosed delimiters in lexer diagnostic #127955

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions compiler/rustc_parse/src/lexer/tokentrees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 1 addition & 7 deletions tests/ui/parser/brace-in-let-chain.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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 () = ()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// issue: rust-lang/rust#127868

fn main() {
let a = [[[[[[[[[[[[[[[[[[[[1, {, (, [,;
} //~ ERROR mismatched closing delimiter: `}`
//~ ERROR this file contains an unclosed delimiter
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
error: mismatched closing delimiter: `}`
--> $DIR/mismatched-delimiter-corner-case-issue-127868.rs:4:42
|
LL | fn main() {
| - closing delimiter possibly meant for this
LL | let a = [[[[[[[[[[[[[[[[[[[[1, {, (, [,;
| ^ unclosed delimiter
LL | }
| ^ mismatched closing delimiter

error: this file contains an unclosed delimiter
--> $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...
| |||||
| ||||another 16 unclosed delimiters begin from here
| |||unclosed delimiter
| ||unclosed delimiter
| |unclosed delimiter
| unclosed delimiter
LL | }
| - ...as it matches this but it has different indentation
LL |
| ^

error: aborting due to 2 previous errors

Loading