-
Notifications
You must be signed in to change notification settings - Fork 207
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
Prohibit variable and identifier patterns from being named when
or as
, to simplify parsing.
#3033
Conversation
…`as`, to simplify parsing.
@munificent I decided not to change the behaviour of |
I would be very tempted to go farther and treat a pattern like an There's also a process question here. Now that the proposal has essentially shipped, does it make sense to update the proposal itself, or should we treat this as a language change with a separate little proposal document for it? I suspect the latter will be easier for the implementers and people writing tests, but I could be wrong. |
Hmm, I definitely see some value in that from a standpoint of language evolution, since it helps move us toward a world where Implementation difficulty aside, I'm also not convinced it would be doing a service to users. If I'm unlucky enough to be the consumer of an API that contains a getter named I think it's fine to stop people from naming things
That's a really good question! I don't know how best to handle this. |
Good point! OK, I'm sold on the approach in this PR.
cc @dart-lang/dart-team Maybe the larger team has thoughts. |
For the scale of change we seem to be settling on, I'd just suggest landing it as a change to the existing spec (i.e. this PR). |
Works for me! I'll go ahead and land as is. On a related note:
|
No.
No.
Yes. |
How does this change affect https://pub.dev/documentation/mockito/latest/mockito/when.html? |
With the narrower scope we agreed on in #3033 (comment), there's no effect on https://pub.dev/documentation/mockito/latest/mockito/when.html. The only things being prohibited here are: switch (...) {
case var when:
...
} The ability to assign to a local variable named String who, what, where, when, why, how; // This is ok
(who, what, where, when, why, how) = getQuestions(); // This isn't And the ability to refer to lone (unprefixed) constants named const when = 1;
switch (...) {
case when:
...
} None of which should interfere with the use of |
Thanks for the explanation! |
In https://dart-review.googlesource.com/c/sdk/+/299400, the parser was adjusted so that it no longer accepts `when` and `as` as the names for variable patterns in cases where there is a possible ambiguity (e.g. `int when` is not accepted as a pattern because `int` is a legitimate pattern, therefore `when` could introduce a guard clause). This change further prohibits `when` and `as` from being the names of variable patterns or identifier patterns even in the case where there is no ambiguity. This is in line with the discussion at #52199 (comment), and the spec change at dart-lang/language#3033. Three new error codes are introduced, to cover the three circumstances in which `when` or `as` might be used illegally: in a declared variable pattern, in an assigned variable pattern, or in an identifier pattern. I've also added analyzer tests to ensure that the parser recovers from these errors nicely. Unfortunately, nice error recovery is only feasible in the non-ambiguous cases. I've also updated the language test expectations in `tests/language/patterns/version_2_32_changes_error_test.dart` to reflect the new error messages, and added a few more examples of uses of `when` and `as` that are still permitted. Fixes #52260. Bug: #52260 Change-Id: I229f627aa639659c30b83c74895759207da279f7 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/301482 Reviewed-by: Konstantin Shcheglov <scheglov@google.com> Reviewed-by: Jens Johansen <jensj@google.com> Commit-Queue: Paul Berry <paulberry@google.com>
… when/as. In https://dart-review.googlesource.com/c/sdk/+/299400, the parser was adjusted so that it no longer accepts `when` and `as` as the names for variable patterns in cases where there is a possible ambiguity (e.g. `int when` is not accepted as a pattern because `int` is a legitimate pattern, therefore `when` could introduce a guard clause). This change further prohibits `when` and `as` from being the names of variable patterns or identifier patterns even in the case where there is no ambiguity. This is in line with the discussion at #52199 (comment), and the spec change at dart-lang/language#3033. Three new error codes are introduced, to cover the three circumstances in which `when` or `as` might be used illegally: in a declared variable pattern, in an assigned variable pattern, or in an identifier pattern. I've also added analyzer tests to ensure that the parser recovers from these errors nicely. Unfortunately, nice error recovery is only feasible in the non-ambiguous cases. I've also updated the language test expectations in `tests/language/patterns/version_2_32_changes_error_test.dart` to reflect the new error messages, and added a few more examples of uses of `when` and `as` that are still permitted. Fixes: #52311 Bug: #52260 Change-Id: I30cb3a1ca627c6db3d281740fb59de74c4118c2b Cherry-pick: https://dart-review.googlesource.com/c/sdk/+/301482 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/302100 Reviewed-by: Konstantin Shcheglov <scheglov@google.com> Reviewed-by: Jens Johansen <jensj@google.com> Commit-Queue: Paul Berry <paulberry@google.com>
This spec change reflects the discussion at dart-lang/sdk#52199 (comment).
Test cases are here: https://dart-review.googlesource.com/c/sdk/+/300320