[CP] [parser] Fix issue 52954 about nested record destructuring with shorthand #53352
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
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:
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 formidentifier ':' pattern
, but the parser was allowing theidentifier
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
The text was updated successfully, but these errors were encountered: