-
Notifications
You must be signed in to change notification settings - Fork 29
#3057. Add promotion tests for if and break statements
#3156
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 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,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 Break statement: If `N` is a statement of the form `break [L];`, | ||
| /// then: | ||
| /// - Let `S` be the statement targeted by the `break`. If `L` is not present, | ||
| /// this is the innermost `do`, `for`, `switch`, or `while` statement. | ||
| /// Otherwise it is the `do`, `for`, `switch`, or `while` statement with a | ||
| /// label matching L. | ||
| /// - Update `break(S) = join(break(S), before(N))`. | ||
| /// - Let `after(N) = unreachable(before(N))`. | ||
| /// | ||
| /// @description Checks that if some type `T` is made a type of interest | ||
| /// `before(N)` then the variable can be promoted to `T` in `after(N)`. | ||
| /// @author sgrekhov22@gmail.com | ||
|
|
||
| class S {} | ||
|
|
||
| class T extends S { | ||
| int answer() => 42; | ||
| } | ||
|
|
||
| test1() { | ||
| S s = S(); | ||
| for (int j = 0; j < 1; j++) { | ||
| if (s is T) {} // Make `T` a type of interest | ||
| break; | ||
| // Unreachable, but does support promotion. | ||
| s = T(); | ||
| s.answer(); | ||
| } | ||
| } | ||
|
|
||
| test2() { | ||
| S s = S(); | ||
| for (var j in []) { | ||
| if (s is T) {} | ||
| break; | ||
| s = T(); | ||
| s.answer(); | ||
| } | ||
| } | ||
|
|
||
| test3() { | ||
| S s = S(); | ||
| do { | ||
| if (s is T) {} | ||
| break; | ||
| s = T(); | ||
| s.answer(); | ||
| } while (1 > 2); | ||
| } | ||
|
|
||
| test4() { | ||
| S s = S(); | ||
| int i = 0; | ||
| while (i < 1) { | ||
| i++; | ||
| if (s is T) {} | ||
| break; | ||
| s = T(); | ||
| s.answer(); | ||
| } | ||
| } | ||
|
|
||
| test5() { | ||
| S s = S(); | ||
| switch (42) { | ||
| case 1: | ||
| break; | ||
| case 42: | ||
| if (s is T) {} | ||
| break; | ||
| s = T(); | ||
| s.answer(); | ||
| } | ||
| } | ||
|
|
||
| main() { | ||
| test1(); | ||
| test2(); | ||
| test3(); | ||
| test4(); | ||
| test5(); | ||
| } | ||
101 changes: 101 additions & 0 deletions
101
TypeSystem/flow-analysis/reachability_break_A05_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,101 @@ | ||
| // 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 Break statement: If `N` is a statement of the form `break [L];`, | ||
| /// then: | ||
| /// - Let `S` be the statement targeted by the `break`. If `L` is not present, | ||
| /// this is the innermost `do`, `for`, `switch`, or `while` statement. | ||
| /// Otherwise it is the `do`, `for`, `switch`, or `while` statement with a | ||
| /// label matching L. | ||
| /// - Update `break(S) = join(break(S), before(N))`. | ||
| /// - Let `after(N) = unreachable(before(N))`. | ||
| /// | ||
| /// @description Checks that if some type `T` is made a type of interest | ||
| /// `before(N)` then the variable can be promoted to `T` in `after(N)`. | ||
| /// @author sgrekhov22@gmail.com | ||
|
|
||
| class S {} | ||
|
|
||
| class T extends S { | ||
| int answer() => 42; | ||
| } | ||
|
|
||
| test1() { | ||
| S s = S(); | ||
| L: | ||
| for (int j = 0; j < 1; j++) { | ||
| for (int j = 0; j < 1; j++) { | ||
| if (s is T) {} // Make `T` a type of interest | ||
| break L; | ||
| // Unreachable, but does support promotion. | ||
| s = T(); | ||
| s.answer(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| test2() { | ||
| S s = S(); | ||
| L: | ||
| for (var i in []) { | ||
| for (var j in []) { | ||
| if (s is T) {} | ||
| break L; | ||
| s = T(); | ||
| s.answer(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| test3() { | ||
| S s = S(); | ||
| L: | ||
| do { | ||
| do { | ||
| if (s is T) {} | ||
| break L; | ||
| s = T(); | ||
| s.answer(); | ||
| } while (1 > 2); | ||
| } while (1 > 2); | ||
| } | ||
|
|
||
| test4() { | ||
| S s = S(); | ||
| int i = 0; | ||
| L: | ||
| while (i < 1) { | ||
| while (i < 1) { | ||
| i++; | ||
| if (s is T) {} | ||
| break L; | ||
| s = T(); | ||
| s.answer(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| test5() { | ||
| S s = S(); | ||
| L: | ||
| for (;;) { | ||
| switch (42) { | ||
| case 1: | ||
| break; | ||
| case 42: | ||
| if (s is T) {} | ||
| break L; | ||
| s = T(); | ||
| s.answer(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| main() { | ||
| test1(); | ||
| test2(); | ||
| test3(); | ||
| test4(); | ||
| test5(); | ||
| } |
101 changes: 101 additions & 0 deletions
101
TypeSystem/flow-analysis/reachability_break_A05_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,101 @@ | ||
| // 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 Break statement: If `N` is a statement of the form `break [L];`, | ||
| /// then: | ||
| /// - Let `S` be the statement targeted by the `break`. If `L` is not present, | ||
| /// this is the innermost `do`, `for`, `switch`, or `while` statement. | ||
| /// Otherwise it is the `do`, `for`, `switch`, or `while` statement with a | ||
| /// label matching L. | ||
| /// - Update `break(S) = join(break(S), before(N))`. | ||
| /// - Let `after(N) = unreachable(before(N))`. | ||
| /// | ||
| /// @description Checks that if some type `T` is made a type of interest | ||
| /// `after(N)` then the variable cannot be promoted to `T` in `before(N)`. | ||
| /// @author sgrekhov22@gmail.com | ||
|
|
||
| class S {} | ||
|
|
||
| class T extends S { | ||
| int answer() => 42; | ||
| } | ||
|
|
||
| test1() { | ||
| S s = S(); | ||
| for (int j = 0; j < 2; j++) { | ||
| s = T(); | ||
| s.answer(); | ||
| // ^^^^^^ | ||
| // [analyzer] unspecified | ||
| // [cfe] unspecified | ||
| break; | ||
| // Unreachable, but does support promotion. | ||
| if (s is T) {} // Make `T` a type of interest | ||
| } | ||
| } | ||
|
|
||
| test2() { | ||
| S s = S(); | ||
| for (var j in []) { | ||
| s = T(); | ||
| s.answer(); | ||
| // ^^^^^^ | ||
| // [analyzer] unspecified | ||
| // [cfe] unspecified | ||
| break; | ||
| if (s is T) {} | ||
| } | ||
| } | ||
|
|
||
| test3() { | ||
| S s = S(); | ||
| do { | ||
| s = T(); | ||
| s.answer(); | ||
| // ^^^^^^ | ||
| // [analyzer] unspecified | ||
| // [cfe] unspecified | ||
| break; | ||
| if (s is T) {} | ||
| } while (1 > 2); | ||
| } | ||
|
|
||
| test4() { | ||
| S s = S(); | ||
| int i = 0; | ||
| while (i < 2) { | ||
| i++; | ||
| s = T(); | ||
| s.answer(); | ||
| // ^^^^^^ | ||
| // [analyzer] unspecified | ||
| // [cfe] unspecified | ||
| break; | ||
| if (s is T) {} | ||
| } | ||
| } | ||
|
|
||
| test5() { | ||
| S s = S(); | ||
| switch (42) { | ||
| case 1: | ||
| break; | ||
| case 42: | ||
| s = T(); | ||
| s.answer(); | ||
| // ^^^^^^ | ||
| // [analyzer] unspecified | ||
| // [cfe] unspecified | ||
| break; | ||
| if (s is T) {} | ||
| } | ||
| } | ||
|
|
||
| main() { | ||
| print(test1); | ||
| print(test2); | ||
| print(test5); | ||
| print(test4); | ||
| print(test5); | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.