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

[CP] [_fe_analyzer_shared] Handle private names in exhaustiveness checking #52254

Closed
stereotype441 opened this issue May 3, 2023 · 5 comments
Assignees
Labels
area-fe-analyzer-shared Assigned by engineers; when triaging, prefer either area-front-end or area-analyzer. cherry-pick-approved Label for approved cherrypick request cherry-pick-merged Cherry-pick has been merged to the stable or beta branch. cherry-pick-review Issue that need cherry pick triage to approve merge-to-stable

Comments

@stereotype441
Copy link
Member

stereotype441 commented May 3, 2023

Commit(s) to merge

3a5b06a

Target

Beta Stable

Prepared changelist for beta/stable

https://dart-review.googlesource.com/c/sdk/+/301180 https://dart-review.googlesource.com/c/sdk/+/302845

Issue Description

When a record pattern in a switch refers to a private getter, the CFE implementation of exhaustiveness gets confused and fails to recognise the coverage of the pattern. As a result, users can get a bogus error stating that their switch isn't exhaustive.

What is the fix

The CFE's implementation of the exhaustiveness callback getFieldTypes was modified so that it considers private getters as legitimate targets for a field in an object pattern, provided that those getters are defined in the same library as the pattern. Previously, it ignored private getters entirely (essentially treating all private getters as inaccessible for the purpose of exhaustiveness checking).

The analyzer's implementation of getFieldTypes was also updated to match the CFE fix. Previously, the analyzer considered all private getters as accessible for purpose of exhaustiveness checking; this was incorrect, but it would require a very contrived piece of code to illustrate the flaw.

Why cherry-pick

Since sealed classes are designed to allow exhaustiveness checking on a class hierarchy that's entirely defined within a single library, it seems likely that users will create switch statements within that library and expect to be able to use object patterns that refer to private getters. (Indeed, this is how I discovered the bug in the first place).

Working around the error requires either adding additional cases (which won't ever be reached at runtime) or rewriting the switch statement so that it doesn't refer to the private getter (e.g. checking the getter in a when clause). Neither of these workarounds is particularly attractive.

The analyzer doesn't have this issue, so a user using an IDE doesn't see the problem until they try to run their code. At that point, if they try to fix their code by navigating to their IDE's "problems" view (in the hopes of then navigating to the place in the source code that experienced the problem), they won't find any errors in the "problems" view (since this view is populated with errors from the analyzer, not the CFE). This could lead to user confusion and frustration.

The fix is fairly low risk, since it is confined to the treatment of private getters in object patterns.

Risk

low

Issue link(s)

#52041

Extra Info

Since the cherry-pick window for beta has essentially closed, I'm not expecting this to actually get approved for a cherry-pick to beta. But I can't follow the cherry-pick process for stable yet (because the stable branch of Dart 3.0 hasn't been created yet). So I'm filing this as a cherry-pick-to-beta request and marking it with the cherry-pick-hold tag. I'm assuming that once the stable branch has been created, all cherry-picks with that tag will get re-evaluated for possibly cherry-picking to stable.

@stereotype441 stereotype441 added cherry-pick-review Issue that need cherry pick triage to approve cherry-pick-hold Hold off on this cherry-pick labels May 3, 2023
@itsjustkevin
Copy link
Contributor

Thanks @stereotype441, noting that this is for the first stable hotfix to avoid losing track of it.

@mraleph mraleph added the area-fe-analyzer-shared Assigned by engineers; when triaging, prefer either area-front-end or area-analyzer. label May 8, 2023
@itsjustkevin
Copy link
Contributor

@stereotype441 we have the stable branch available now, please rebase to the stable branch and we can get this pushed through the queue.

@stereotype441
Copy link
Member Author

@stereotype441 we have the stable branch available now, please rebase to the stable branch and we can get this pushed through the queue.

Thanks @itsjustkevin! For some reason Gerrit decided to assign a fresh CL id when I rebased it (https://dart-review.googlesource.com/c/sdk/+/302845), so I've updated the issue here to point to it, and I've sent it for code review again.

@stereotype441 stereotype441 removed the cherry-pick-hold Hold off on this cherry-pick label May 11, 2023
@itsjustkevin
Copy link
Contributor

Thanks @stereotype441, that happened on another cherry-pick CL also.

@stereotype441
Copy link
Member Author

Ok, the CL has been reviewed. I'm just waiting for cherry-pick approval now.

@itsjustkevin itsjustkevin added merge-to-stable cherry-pick-approved Label for approved cherrypick request labels May 15, 2023
copybara-service bot pushed a commit that referenced this issue May 15, 2023
… checking

Closes #52041

Cherry-pick: https://dart-review.googlesource.com/c/sdk/+/297462
Change-Id: I2bc2fc8fb38b30eb5e9c6e639c2603a5508dfec1
Fixes: #52254
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/302845
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
@itsjustkevin itsjustkevin added the cherry-pick-merged Cherry-pick has been merged to the stable or beta branch. label May 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-fe-analyzer-shared Assigned by engineers; when triaging, prefer either area-front-end or area-analyzer. cherry-pick-approved Label for approved cherrypick request cherry-pick-merged Cherry-pick has been merged to the stable or beta branch. cherry-pick-review Issue that need cherry pick triage to approve merge-to-stable
Projects
None yet
Development

No branches or pull requests

7 participants