-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Flow analysis: properly model if/else nature of switches.
A switch statement like this one: switch (E) { case P1 when G1: S1; case P2 when G2: S2; case P3 when G3: } Is equivalent to an if/else chain like this: var tmp = E; if (tmp case P1 when G1) { S1; } else if (tmp case P2 when G2) { S2; } else if (tmp case P3 when G3) { S3; } Therefore, if the failure of a particular pattern/guard combination to match implies a type promotion, it makes sense for that promotion to be carried into later cases. For example: int? x = ...; switch (E) { case _ when x == null: break; default: x.isEven; // OK because `x` known to be non-null. } This enabled some more thorough testing of type promotion in switches, which then caught a bug introduced in a previous CL: when the switch scrutinee is a variable reference, and we are trying to determine whether it is safe for a pattern to promote the scrutinee variable, we were checking the wrong SSA node to determine whether the variable had been reassigned. For example: Object x; switch (x) { case _ when f(x = ...); break; case int _: // `x` is not promoted to `int` because it is no longer the // same as the cached scrutinee. break; } Bug: #50419 Change-Id: Ie8d6cf0fc662aa5ef0ac81eb2343952028dd2abb Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/278533 Reviewed-by: Johnni Winther <johnniwinther@google.com> Commit-Queue: Paul Berry <paulberry@google.com> Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
- Loading branch information
1 parent
773bd2f
commit 5b234e4
Showing
6 changed files
with
267 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.