-
Notifications
You must be signed in to change notification settings - Fork 28
#3057. Add switch expression/statement tests for promotion #3147
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
Open
sgrekhov
wants to merge
2
commits into
dart-lang:master
Choose a base branch
from
sgrekhov:co19-3057-46
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
/// @assertion switch statement: If `N` is a switch statement of the form | ||
/// `switch (E) {alternatives}` then: | ||
/// - Let `before(E) = before(N)`. | ||
/// - For each `C` in `alternatives` with statement body `S`: | ||
/// - If `C` is labelled let | ||
/// `before(S) = conservativeJoin(after(E), assignedIn(N), capturedIn(N))` | ||
/// otherwise let `before(S) = after(E)`. | ||
/// - If the cases are exhaustive, then let `after(N) = break(N)` otherwise let | ||
/// `after(N) = join(after(E), break(N))`. | ||
/// | ||
/// @description Checks that if a type `T` is made a type of interest in `E` | ||
/// for a variable `s` then `s` can be promoted in `alternatives` and `after(N)` | ||
/// @author [email protected] | ||
/// @issue 60479 | ||
/// @issue 60539 | ||
|
||
class S {} | ||
|
||
class T extends S { | ||
int answer() => 42; | ||
} | ||
|
||
void test1() { | ||
S s = S(); | ||
switch (s is T ? 1 : 2) { // Make `T` a type of interest | ||
case 1: | ||
s = T(); | ||
s.answer(); | ||
case 2: | ||
} | ||
} | ||
|
||
void test2() { | ||
S s = S(); | ||
switch (s is T ? 1 : 2) { // Make `T` a type of interest | ||
label: case 1: | ||
s = T(); | ||
s.answer(); | ||
case 2: | ||
} | ||
} | ||
|
||
void test3() { | ||
S s = S(); | ||
switch (s is T) { | ||
case true: | ||
case false: | ||
} | ||
s = T(); | ||
s.answer(); | ||
} | ||
|
||
void test4() { | ||
S s = S(); | ||
switch (s is T) { | ||
label: case true: | ||
case false: | ||
} | ||
s = T(); | ||
s.answer(); | ||
} | ||
|
||
main() { | ||
test1(); | ||
test2(); | ||
test3(); | ||
test4(); | ||
} |
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
/// @assertion switch statement: If `N` is a switch statement of the form | ||
/// `switch (E) {alternatives}` then: | ||
/// - Let `before(E) = before(N)`. | ||
/// - For each `C` in `alternatives` with statement body `S`: | ||
/// - If `C` is labelled let | ||
/// `before(S) = conservativeJoin(after(E), assignedIn(N), capturedIn(N))` | ||
/// otherwise let `before(S) = after(E)`. | ||
/// - If the cases are exhaustive, then let `after(N) = break(N)` otherwise let | ||
/// `after(N) = join(after(E), break(N))`. | ||
/// | ||
/// @description Checks that if a type `T` is made a type of interest in `E` | ||
/// for a variable `s` then `s` can be promoted in `after(N)`. | ||
/// @author [email protected] | ||
/// @issue 60479 | ||
/// @issue 60539 | ||
|
||
class S {} | ||
|
||
class T extends S { | ||
int answer() => 42; | ||
} | ||
|
||
test1() { | ||
S s = S(); | ||
switch (s is T) { | ||
case true: | ||
case false: | ||
} | ||
s = T(); | ||
s.answer(); | ||
} | ||
|
||
test2() { | ||
S s = S(); | ||
switch (s is T ? 1 : 2) { | ||
label: case 1: | ||
case 2: | ||
} | ||
s = T(); | ||
s.answer(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This works! |
||
} | ||
|
||
main() { | ||
test1(); | ||
test2(); | ||
} |
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
/// @assertion switch statement: If `N` is a switch statement of the form | ||
/// `switch (E) {alternatives}` then: | ||
/// - Let `before(E) = before(N)`. | ||
/// - For each `C` in `alternatives` with statement body `S`: | ||
/// - If `C` is labelled let | ||
/// `before(S) = conservativeJoin(after(E), assignedIn(N), capturedIn(N))` | ||
/// otherwise let `before(S) = after(E)`. | ||
/// - If the cases are exhaustive, then let `after(N) = break(N)` otherwise let | ||
/// `after(N) = join(after(E), break(N))`. | ||
/// | ||
/// @description Checks that if a type `T` is made a type of interest in `E` | ||
/// for a variable `s` then `s` cannot be promoted in other `alternatives`. | ||
/// @author [email protected] | ||
|
||
class S {} | ||
|
||
class T extends S { | ||
int answer() => 42; | ||
} | ||
|
||
test1() { | ||
S s = S(); | ||
switch (42) { | ||
case 1: | ||
s = T(); | ||
s.answer(); | ||
// ^^^^^^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
case 2: | ||
if (s is T) {} // Make `T` a type of interest | ||
} | ||
} | ||
|
||
test2() { | ||
S s = S(); | ||
switch (42) { | ||
label: case 1: | ||
s = T(); | ||
s.answer(); | ||
// ^^^^^^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
case 2: | ||
if (s is T) {} // Make `T` a type of interest | ||
} | ||
} | ||
|
||
test3() { | ||
S s = S(); | ||
switch (42) { | ||
case 1: | ||
if (s is T) {} | ||
case 2: | ||
s = T(); | ||
s.answer(); | ||
// ^^^^^^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
} | ||
} | ||
|
||
test4() { | ||
S s = S(); | ||
switch (42) { | ||
label: case 1: | ||
if (s is T) {} | ||
case 2: | ||
s = T(); | ||
s.answer(); | ||
// ^^^^^^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
} | ||
} | ||
|
||
main() { | ||
print(test1); | ||
print(test2); | ||
print(test3); | ||
print(test4); | ||
} |
48 changes: 48 additions & 0 deletions
48
TypeSystem/flow-analysis/reachability_switch_expression_A04_t01.dart
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
/// @assertion switch expression: If `N` is a switch expression of the form | ||
/// `switch (E) {alternatives}` then: | ||
/// - Let `before(E) = split(before(N))`. | ||
/// - For each case `Cj` in `alternatives` with expression `Ej`, for `j` in | ||
/// `1 .. k`: | ||
/// - Let `before(Ej) = after(E)`. | ||
/// - Let `after(N) = join(after(E1), after(E2), .. after(Ek))`. | ||
/// TODO (sgrekhov): there is no switch expression in the flow analysis spec yet | ||
/// (April 2025). Update the assertion after spec update. | ||
/// | ||
/// @description Checks that if a type `T` is made a type of interest in `E` | ||
/// for a variable `s` then `s` can be promoted in `alternatives` and `after(N)` | ||
/// @author [email protected] | ||
/// @issue 60479 | ||
/// @issue 60539 | ||
|
||
class S {} | ||
|
||
class T extends S { | ||
int answer() => 42; | ||
} | ||
|
||
void test1() { | ||
S s = S(); | ||
var x = switch (s is T ? 1 : 2) { // Make `T` a type of interest | ||
1 => [s = T(), s.answer()], | ||
_ => [] | ||
}; | ||
} | ||
|
||
void test2() { | ||
S s = S(); | ||
var x = switch (s is T) { | ||
true => 1, | ||
_ => 0 | ||
}; | ||
s = T(); | ||
s.answer(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This might fail, but it would be a bug, so the test is fine. |
||
} | ||
|
||
main() { | ||
test1(); | ||
test2(); | ||
} |
48 changes: 48 additions & 0 deletions
48
TypeSystem/flow-analysis/reachability_switch_expression_A04_t02.dart
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
/// @assertion switch expression: If `N` is a switch expression of the form | ||
/// `switch (E) {alternatives}` then: | ||
/// - Let `before(E) = split(before(N))`. | ||
/// - For each case `Cj` in `alternatives` with expression `Ej`, for `j` in | ||
/// `1 .. k`: | ||
/// - Let `before(Ej) = after(E)`. | ||
/// - Let `after(N) = join(after(E1), after(E2), .. after(Ek))`. | ||
/// TODO (sgrekhov): there is no switch expression in the flow analysis spec yet | ||
/// (April 2025). Update the assertion after spec update. | ||
/// | ||
/// @description Checks that if a type `T` is made a type of interest in | ||
/// `alternatives` for a variable `s` then `s` can be promoted in `alternatives` | ||
/// and `after(N)`. | ||
/// @author [email protected] | ||
/// @issue 60479 | ||
/// @issue 60539 | ||
|
||
class S {} | ||
|
||
class T extends S { | ||
int answer() => 42; | ||
} | ||
|
||
void test1() { | ||
S s = S(); | ||
var x = switch (42) { | ||
1 => [s is T, s = T(), s.answer()], | ||
_ => [] | ||
}; | ||
} | ||
|
||
void test2() { | ||
S s = S(); | ||
var x = switch (42) { | ||
_ => s is T // Make `T` a type of interest | ||
}; | ||
s = T(); | ||
s.answer(); | ||
} | ||
|
||
main() { | ||
test1(); | ||
test2(); | ||
} |
51 changes: 51 additions & 0 deletions
51
TypeSystem/flow-analysis/reachability_switch_expression_A04_t03.dart
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
/// @assertion switch expression: If `N` is a switch expression of the form | ||
/// `switch (E) {alternatives}` then: | ||
/// - Let `before(E) = split(before(N))`. | ||
/// - For each case `Cj` in `alternatives` with expression `Ej`, for `j` in | ||
/// `1 .. k`: | ||
/// - Let `before(Ej) = after(E)`. | ||
/// - Let `after(N) = join(after(E1), after(E2), .. after(Ek))`. | ||
/// TODO (sgrekhov): there is no switch expression in the flow analysis spec yet | ||
/// (April 2025). Update the assertion after spec update. | ||
/// | ||
/// @description Checks that if a type `T` is made a type of interest for `s` in | ||
/// an alternative then there is no guarantee that `T` is a type of interest for | ||
/// `s` in any other alternative. | ||
/// @author [email protected] | ||
|
||
class S {} | ||
|
||
class T extends S { | ||
int answer() => 42; | ||
} | ||
|
||
test1() { | ||
S s = S(); | ||
var x = switch (42) { | ||
1 => [s = T(), s.answer()], | ||
// ^^^^^^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
_ => [s is T ? 1: 2] // Make `T` a type of interest | ||
}; | ||
} | ||
|
||
test2() { | ||
S s = S(); | ||
var x = switch (42) { | ||
1 => [s is T ? 1: 2], | ||
_ => [s = T(), s.answer()], | ||
// ^^^^^^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
}; | ||
} | ||
|
||
main() { | ||
print(test1); | ||
print(test2); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, this might fail, but that's a bug, so this test is still fine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes and we should have a test checking that
switch (s is T)
also works.