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] [parser] Fix issue 52954 about nested record destructuring with shorthand #53352

Closed
stereotype441 opened this issue Aug 25, 2023 · 2 comments
Assignees
Labels
area-front-end Use area-front-end for front end / CFE / kernel format related issues. cherry-pick-approved Label for approved cherrypick request cherry-pick-review Issue that need cherry pick triage to approve merge-to-stable

Comments

@stereotype441
Copy link
Member

Commit(s) to merge

a445fae

Target

Stable

Prepared changelist for beta/stable

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

Issue Description

When a record pattern is nested inside another record pattern, and the inner record pattern uses "shorthand" syntax, the parser sometimes becomes confused and reports syntax errors. Examples include:

final ((:a, :b), c) = record;
final (c, (:a, :b)) = record;

The problem affects all platforms, including the analyzer.

What is the fix

The solution is to modify the parsing method parseParenthesizedPatternOrRecordPattern, which was using an overly general rule to accept named record fields. Technically a named record field should take the form identifier ':' pattern, but the parser was allowing the identifier to be any token for error recovery purposes. This meant that when parsing a pattern like ((:a, :b), c), instead of recognizing (:a, :b) as a nested record pattern, it was assuming it was a malformed named record field, with the ( being the malformed name.

The fix excludes ( from being considered as a record field name, even during error recovery, so that (:a, :b) is properly recognized as an unnamed field.

Why cherry-pick

Without this fix, any user trying to pattern match a record pattern containing a nested record pattern, where the nested record pattern begins with (:, will see obscure and confusing syntax errors using Dart 3.1. Any user doing this sort of pattern match using Dart 3.2 will get correct behavior, but if they publish their code on pub, it won't work with Dart 3.1.

The fix is very low risk, since it only affects an error recovery path.

Risk

low

Issue link(s)

#52954

Extra Info

No response

@itsjustkevin
Copy link
Contributor

@leafpetersen and @jakemac53 could you take a look at this cherry-pick request?

@leafpetersen
Copy link
Member

LGTM

@itsjustkevin itsjustkevin added merge-to-stable cherry-pick-approved Label for approved cherrypick request labels Aug 29, 2023
@a-siva a-siva added the area-front-end Use area-front-end for front end / CFE / kernel format related issues. label Aug 30, 2023
copybara-service bot pushed a commit that referenced this issue Aug 31, 2023
…ith shorthand

Cherry-pick: https://dart-review.googlesource.com/c/sdk/+/317681
Cherry-pick-request: #53352
Fixes: #53352
Change-Id: If30df5cecf4fa478f3717dbac67e4ebee1faeff0
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/322587
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-front-end Use area-front-end for front end / CFE / kernel format related issues. cherry-pick-approved Label for approved cherrypick request cherry-pick-review Issue that need cherry pick triage to approve merge-to-stable
Projects
None yet
Development

No branches or pull requests

8 participants