[CP] [parser] Support nullable record types in patterns #52518
Labels
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
Commit(s) to merge
284296f
Target
Stable
Prepared changelist for beta/stable
https://dart-review.googlesource.com/c/sdk/+/305824
Issue Description
If a variable pattern or wildcard pattern has an explicit type which is a nullable record type, there is a parse error. For example, this code:
is rejected with the error message:
What is the fix
The parser's disambiguation rules are modified so that if a pattern starts with
(
, and the matching)
is followed by? identifier
, an attempt is made to parse the pattern as a variable pattern. Previously, the disambiguation rules only attempted to parse a pattern starting with(
as a variable pattern if the matching)
was followed by an identifier (i.e. a variable pattern with a non-nullable record type).Why cherry-pick
Without this fix, users cannot express a pattern match using a variable or wildcard pattern with a nullable record type. It is not clear from the error message why the code is being rejected.
If we don't cherry-pick this fix, the fix will be released as part of Dart 3.1, so there will be a danger of a package author releasing code that is intended to be compatible with Dart 3.0, but which fails to work due to its use of a variable pattern with an explicit nullable record type.
The fix is low risk, because it is confined to the disambiguation logic that fires specifically for patterns that begin with
(
. The only syntax that is newly recognized as a variable pattern is(...)? identifier
. Any ambiguities that might arise from this interpretation are also problems for the syntax(...) identifier
(e.g.(...) when
or(...) as
), and the resolution of these ambiguities is already taken care of and well tested.Risk
low
Issue link(s)
#52439
Extra Info
No response
The text was updated successfully, but these errors were encountered: