[CP] Patterns parser: prohibit variable/identifier patterns named when/as. #52311
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-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
dc639c1
Target
Stable
Prepared changelist for beta/stable
https://dart-review.googlesource.com/c/sdk/+/302100
Issue Description
Before the release of Dart 3.0, it was discovered that if the variable name in a variable pattern, or the identifier in an identifier pattern, was
when
oras
, that would lead to a parsing ambiguity which could not be resolved with the current amount of lookahead the parser possesses. This caused parse errors for patterns like:(The parser would erroneously interpret
Enum.value
as a type andwhen
as the name of a pattern variable).To reduce risk to the Dart 3.0 release, we fixed this in a minimal way, by prohibiting the variable name in a variable pattern from being called
when
oras
, but only in those circumstances where the ambiguity arose.At the same time, we made a change on the main development branch that prohibits
when
oras
from being used as the variable name in any variable pattern, and simultaneously prohibitswhen
oras
from being used as the identifier in an identifier pattern. (We also updated the spec in dart-lang/language#3033). This cherry-pick brings over that change to the stable branch, bringing the implementation in line with the spec.What is the fix
Three new error codes are introduced, to cover the three circumstances in which
when
oras
might be used illegally: in a declared variable pattern, in an assigned variable pattern, or in an identifier pattern. The parser is modified so that it generates these errors in appropriate circumstances.Why cherry-pick
This change makes the implementation more consistent (both with itself and with the spec), and helps users avoid future trouble by preventing people from writing new code with patterns that refer ambiguously to identifiers named
when
oras
.If we don't do this cherry-pick, we'll have to accept in perpetuity that Dart 3.0 allows these ambiguous identifier names, and any efforts to prohibit these names in the future will require a language-versioned change (which is significantly more maintenance effort for the Dart team).
Risk
low
Issue link(s)
#52260
Extra Info
No response
The text was updated successfully, but these errors were encountered: