Skip to content

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

xizheyin
Copy link
Contributor

@xizheyin xizheyin commented Mar 16, 2025

Fixes #138401

@rustbot
Copy link
Collaborator

rustbot commented Mar 16, 2025

r? @compiler-errors

rustbot has assigned @compiler-errors.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 16, 2025
@compiler-errors
Copy link
Member

r? compiler

@rustbot rustbot assigned estebank and unassigned compiler-errors Mar 21, 2025
@chenyukang
Copy link
Member

I changed this part last year, will review this PR later.

r? @chenyukang

@rustbot rustbot assigned chenyukang and unassigned estebank Apr 7, 2025
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 11, 2025
@chenyukang
Copy link
Member

I think this solution introduces some kind of regression on other scenarios(such as the cases I commented).

Maybe we should consider this as a special corner case and provide a special diagnostic for it, it is the pattern that one delimiter is missing the openning delimiter(it's in the inner part), except for this everything is ok, so we should suggest something like "missing openning delimiter ....".

such as:

{ ... {. ) ... }...} missing a inner (
{ ... [ ], ] ...} missing a inner [

@chenyukang
Copy link
Member

@rustbot author

@rustbot
Copy link
Collaborator

rustbot commented Apr 11, 2025

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@xizheyin
Copy link
Contributor Author

Thanks, I will revise it soon!

@xizheyin xizheyin changed the title [Lexer] Remove spurious unexpected delimiter error by matching remain… Distinguish delim kind to decide whether to emit unexpected closing delimiter Apr 12, 2025
@xizheyin
Copy link
Contributor Author

This seems like quite a lot of changes, I'll comment in the code to make it easier to REVIEW.

In the first commit, I added the test.

In the second commit I did some clean. I changed a variable name, the original name was inaccurate, I used open_delimiters instead of open_braces for identifying blocks, one should compare spans, not just brace'{'.

In the third commit, I make a distinction between the different delimiters before triggering a unexpected closing delimiter error. But I found that missing open delim note is what triggers in unexpected closing delimiter error.
Therefore, to emit missing open delim note, I also moved missing open delim note to
mismatched closing delimiter error, which can be emitted without delaying to
unexpected closing delimiter error.
And I moved function make_unclosed_delims_error
to src/lexer/diagnostics.rs for clean. And some other clean.

@xizheyin xizheyin requested a review from chenyukang April 12, 2025 11:02
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Apr 12, 2025
@xizheyin
Copy link
Contributor Author

I've combined them into one line so it's more aesthetically pleasing.

mismatched closing delimiter, may missing open `(`

Since the mismatched closing delimiter note and the unclosed delimiter note are related, it may be the better to keep it.

@rust-log-analyzer

This comment has been minimized.

@xizheyin
Copy link
Contributor Author

xizheyin commented Jul 9, 2025

Thanks for the guidance, I've learned much, this diagnosis involves a lot of regression testing so it's a little harder to deal with.

error: unexpected closing delimiter: `}`
--> $DIR/issue-68987-unmatch-issue-3.rs:8:1
|
LL | fn f(i: u32, j: u32) {
| - the closing `}` may be intended to match this
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this new hint is unnecessary, how can we remove it?
since the root cause is only a missing '(', which we have already reported.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

estebank suggest me to add some hint. the closing ) may match this {, so we can get the general scope of how to add [. But I understood it. It should be "the mismatched closing ) may be meant for this opening delimiter".
See #138554 (comment) #138554 (comment)

Copy link
Member

@chenyukang chenyukang Jul 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

even if we change the words into the mismatched closing ) may be meant for this opening delimiter, it will also make a similar confusion like the origin issue #138401 described?

user will be confused why ')' meant for this opennning '{' ?

Copy link
Member

@chenyukang chenyukang Jul 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this remind me maybe we should reconsider whether the original issue is worth to be resolved, the original hint in the issue description:

error: mismatched closing delimiter: `)`
 --> ...\test.rs:1:27
  |
1 | pub fn foo(x: i64) -> i64 {
  |                           ^ unclosed delimiter
2 |     x.abs)
  |          ^ mismatched closing delimiter

seems somehow confused, but it helps narrow the scope of span, if there are multiple nested scope, I think it's helpful in those scenarios.

error: unexpected closing delimiter: `}`
--> $DIR/issue-68987-unmatch-issue-2.rs:14:1
|
LL | async fn obstest() -> Result<> {
| - the closing `}` may be intended to match this
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

similar one.
it's better to remove it.

LL | V = [PhantomData; { [ () ].len() ].len() as isize,
| - missing open `[` for this delimiter
...
LL | mod b {
| - unclosed delimiter
LL | enum Bug {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this hint may makes user try to add a '}', while the root cause is we should remove the { where old diagnostic reported.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is because we merged the mismatched diagnostic into another diagnostic. The old diagnostic gave the wrong information because the real reason was the missing {} in { [].len() }. However, I think this PR is meant to address the duplicate error message, this false message can be addressed in another PR. :)

@xizheyin
Copy link
Contributor Author

xizheyin commented Jul 9, 2025

I need to clarify the direction of the modification.
As I understand it, for the current modification, I need to change the hint as suggested in this #138554 (comment) .
For the other, such as #138554 (comment) , it seems to need to be addressed in another PR, as this is not what #138401 is proposing, and it seems to be a new issue.

@chenyukang
Copy link
Member

I need to clarify the direction of the modification. As I understand it, for the current modification, I need to change the hint as suggested in this #138554 (comment) . For the other, such as #138554 (comment) , it seems to need to be addressed in another PR, as this is not what #138401 is proposing, and it seems to be a new issue.

per my understanding, the issue #138554 (comment) refers to is introduced by the code in this PR, not a old one?

@xizheyin
Copy link
Contributor Author

Oh! That makes sense. This seems to require finer control.

Copy link
Contributor Author

@xizheyin xizheyin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be no regression this time.

The only thing I did was this: if the missing open hint is reported in report_missing_open_delim, it will not generate a separate mismatched error, reducing a lot of confusing errors. Meanwhile, this doesn't eat up the information in the mismatched error.

@rust-log-analyzer

This comment has been minimized.

@xizheyin xizheyin marked this pull request as draft July 12, 2025 09:45
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 12, 2025
xizheyin added 2 commits July 12, 2025 17:46
Signed-off-by: xizheyin <[email protected]>
Signed-off-by: xizheyin <[email protected]>
@xizheyin xizheyin marked this pull request as ready for review July 12, 2025 09:57
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 12, 2025
Copy link
Contributor Author

@xizheyin xizheyin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This time, I reserve the mismatched error when the situation is like {[)}.
And I remove the mismatched error only when close_delim_err, which means double close delims.

Meanwhile, I remove duplicate missing open ... for this delimiter in this file contains an unclosed delimiter error because mismatched errors have already emitted it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Better handling of missing closing or opening delimiters, well distinct by type of delimiter
8 participants