-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Distinguish delim kind to decide whether to emit unexpected closing delimiter #138554
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -77,12 +77,12 @@ impl<'psess, 'src> Lexer<'psess, 'src> { | |
); | ||
} | ||
|
||
if let Some((delim, _)) = self.diag_info.open_delimiters.last() { | ||
if let Some((delim, _)) = self.diag_info.open_delimiters.last().cloned() { | ||
report_suspicious_mismatch_block( | ||
&mut err, | ||
&self.diag_info, | ||
&mut self.diag_info, | ||
self.psess.source_map(), | ||
*delim, | ||
delim, | ||
) | ||
} | ||
err | ||
|
@@ -154,15 +154,12 @@ impl<'psess, 'src> Lexer<'psess, 'src> { | |
candidate = Some(*delimiter_span); | ||
} | ||
} | ||
let (_, _) = self.diag_info.open_delimiters.pop().unwrap(); | ||
self.diag_info.unmatched_delims.push(UnmatchedDelim { | ||
found_delim: Some(close_delim), | ||
found_span: self.token.span, | ||
unclosed_span: unclosed_delimiter, | ||
candidate_span: candidate, | ||
}); | ||
} else { | ||
self.diag_info.open_delimiters.pop(); | ||
} | ||
|
||
// If the incorrect delimiter matches an earlier opening | ||
|
@@ -238,13 +235,29 @@ impl<'psess, 'src> Lexer<'psess, 'src> { | |
this_spacing | ||
} | ||
|
||
fn close_delim_err(&mut self, delim: Delimiter) -> Diag<'psess> { | ||
fn close_delim_err(&mut self, close_delim: Delimiter) -> Diag<'psess> { | ||
// An unexpected closing delimiter (i.e., there is no matching opening delimiter). | ||
let token_str = token_to_string(&self.token); | ||
let msg = format!("unexpected closing delimiter: `{token_str}`"); | ||
let mut err = self.dcx().struct_span_err(self.token.span, msg); | ||
|
||
report_suspicious_mismatch_block(&mut err, &self.diag_info, self.psess.source_map(), delim); | ||
if let Some((open_delim, open_delim_span)) = self.diag_info.open_delimiters.last().cloned() | ||
{ | ||
if open_delim == close_delim { | ||
err.span_label( | ||
open_delim_span, | ||
format!("the mismatchd closing `{}` may be matched here", token_str), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This message has a typo, and I'd like to tweak the wording here a bit, I just don't have a good idea for what the ideal text would be in context. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe something "the mismatched closing |
||
); | ||
self.diag_info.open_delimiters.pop(); | ||
} | ||
} | ||
|
||
report_suspicious_mismatch_block( | ||
&mut err, | ||
&mut self.diag_info, | ||
self.psess.source_map(), | ||
close_delim, | ||
); | ||
err.span_label(self.token.span, "unexpected closing delimiter"); | ||
err | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,14 @@ | ||
error: mismatched closing delimiter: `]` | ||
--> $DIR/deli-ident-issue-2.rs:2:14 | ||
| | ||
LL | if 1 < 2 { | ||
| ^ unclosed delimiter | ||
LL | let _a = vec!]; | ||
| ^ mismatched closing delimiter | ||
|
||
error: unexpected closing delimiter: `}` | ||
--> $DIR/deli-ident-issue-2.rs:5:1 | ||
| | ||
LL | fn main() { | ||
| - the mismatchd closing `}` may be matched here | ||
LL | if 1 < 2 { | ||
LL | let _a = vec!]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel like we should still have a label pointing at this opening |
||
| - missing open `[` for this delimiter | ||
LL | } | ||
LL | } | ||
| ^ unexpected closing delimiter | ||
|
||
error: aborting due to 2 previous errors | ||
error: aborting due to 1 previous error | ||
|
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -1,3 +1,3 @@ | ||||||||||
//@ compile-flags: -Zunpretty=ast-tree | ||||||||||
#![c={#![c[)x //~ ERROR mismatched closing delimiter | ||||||||||
#![c={#![c[)x | ||||||||||
//~ ERROR this file contains an unclosed delimiter | ||||||||||
Comment on lines
+2
to
3
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should make the
Suggested change
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,15 @@ | ||
error: mismatched closing delimiter: `)` | ||
--> $DIR/issue-105209.rs:2:11 | ||
| | ||
LL | #![c={#![c[)x | ||
| ^^ mismatched closing delimiter | ||
| | | ||
| unclosed delimiter | ||
|
||
error: this file contains an unclosed delimiter | ||
--> $DIR/issue-105209.rs:3:68 | ||
| | ||
LL | #![c={#![c[)x | ||
| - - - - missing open `(` for this delimiter | ||
| | | | | ||
| - - - -- missing open `(` for this delimiter | ||
| | | | | | ||
| | | | unclosed delimiter | ||
| | | unclosed delimiter | ||
| | unclosed delimiter | ||
| unclosed delimiter | ||
LL | | ||
| ^ | ||
|
||
error: aborting due to 2 previous errors | ||
error: aborting due to 1 previous error | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,17 @@ | ||
error: mismatched closing delimiter: `)` | ||
--> $DIR/issue-62973.rs:8:27 | ||
| | ||
LL | fn p() { match s { v, E { [) {) } | ||
| ^^ mismatched closing delimiter | ||
| | | ||
| unclosed delimiter | ||
|
||
error: mismatched closing delimiter: `)` | ||
--> $DIR/issue-62973.rs:8:30 | ||
| | ||
LL | fn p() { match s { v, E { [) {) } | ||
| ^^ mismatched closing delimiter | ||
| | | ||
| unclosed delimiter | ||
|
||
error: this file contains an unclosed delimiter | ||
--> $DIR/issue-62973.rs:10:2 | ||
--> $DIR/issue-62973.rs:8:2 | ||
| | ||
LL | fn p() { match s { v, E { [) {) } | ||
| - - - - missing open `(` for this delimiter | ||
| | | | | ||
| | | missing open `(` for this delimiter | ||
| - - - -- - missing open `(` for this delimiter | ||
| | | | || | ||
| | | | |missing open `(` for this delimiter | ||
| | | | unclosed delimiter | ||
| | | unclosed delimiter | ||
| | unclosed delimiter | ||
| unclosed delimiter | ||
LL | | ||
LL | | ||
| ^ | ||
|
||
error: aborting due to 3 previous errors | ||
error: aborting due to 1 previous error | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
// fixed by #66361 | ||
//~vv ERROR mismatched closing delimiter: `]` | ||
//~v ERROR this file contains an unclosed delimiter | ||
impl W <s(f;Y(;] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,12 @@ | ||
error: mismatched closing delimiter: `]` | ||
--> $DIR/issue-63116.rs:4:14 | ||
| | ||
LL | impl W <s(f;Y(;] | ||
| ^ ^ mismatched closing delimiter | ||
| | | ||
| unclosed delimiter | ||
|
||
error: this file contains an unclosed delimiter | ||
--> $DIR/issue-63116.rs:4:18 | ||
--> $DIR/issue-63116.rs:3:18 | ||
| | ||
LL | impl W <s(f;Y(;] | ||
| - -^ | ||
| | | | ||
| | missing open `[` for this delimiter | ||
| - - -^ | ||
| | | | | ||
| | | missing open `[` for this delimiter | ||
| | unclosed delimiter | ||
| unclosed delimiter | ||
|
||
error: aborting due to 2 previous errors | ||
error: aborting due to 1 previous error | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,27 @@ | ||
error: mismatched closing delimiter: `]` | ||
--> $DIR/issue-67377-invalid-syntax-in-enum-discriminant.rs:5:27 | ||
| | ||
LL | V = [PhantomData; { [ () ].len() ].len() as isize, | ||
| - ^ ^ mismatched closing delimiter | ||
| | | | ||
| | unclosed delimiter | ||
| closing delimiter possibly meant for this | ||
|
||
error: mismatched closing delimiter: `]` | ||
--> $DIR/issue-67377-invalid-syntax-in-enum-discriminant.rs:12:24 | ||
| | ||
LL | V = [Vec::new; { [].len() ].len() as isize, | ||
| - ^ ^ mismatched closing delimiter | ||
| | | | ||
| | unclosed delimiter | ||
| closing delimiter possibly meant for this | ||
|
||
error: mismatched closing delimiter: `]` | ||
--> $DIR/issue-67377-invalid-syntax-in-enum-discriminant.rs:19:24 | ||
| | ||
LL | V = [Vec::new; { [0].len() ].len() as isize, | ||
| - ^ ^ mismatched closing delimiter | ||
| | | | ||
| | unclosed delimiter | ||
| closing delimiter possibly meant for this | ||
|
||
error: this file contains an unclosed delimiter | ||
--> $DIR/issue-67377-invalid-syntax-in-enum-discriminant.rs:23:65 | ||
--> $DIR/issue-67377-invalid-syntax-in-enum-discriminant.rs:20:65 | ||
| | ||
LL | mod a { | ||
| - unclosed delimiter | ||
... | ||
LL | V = [PhantomData; { [ () ].len() ].len() as isize, | ||
xizheyin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| - missing open `[` for this delimiter | ||
... | ||
LL | mod b { | ||
| - unclosed delimiter | ||
LL | enum Bug { | ||
LL | V = [Vec::new; { [].len() ].len() as isize, | ||
| - missing open `[` for this delimiter | ||
... | ||
LL | mod c { | ||
| - unclosed delimiter | ||
LL | enum Bug { | ||
| - unclosed delimiter | ||
LL | V = [Vec::new; { [0].len() ].len() as isize, | ||
| - missing open `[` for this delimiter | ||
... | ||
LL | fn main() {} | ||
| ^ | ||
|
||
error: aborting due to 4 previous errors | ||
error: aborting due to 1 previous error | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When
missing open xxx for this delimiter
is reported in theunexpected closing delimiter: xxx
error, I remove the correspondingunmatched_delims
so thatmake_unclosed_delims_error
doesn't get double report.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
First of all, thank you for all your changes and your patience to improve this output!
Do we have any test left where the lexer diagnostic is still emitted?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, such as
https://github.com/xizheyin/rust/blob/issue-138401/tests/ui/parser/issues/issue-58856-2.stderr
https://github.com/xizheyin/rust/blob/issue-138401/tests/ui/parser/issues/issue-58856-1.stderr
https://github.com/xizheyin/rust/blob/issue-138401/tests/ui/parser/unclosed-delimiter-in-dep.stderr
I found 27 results after searching.