Skip to content

#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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions TypeSystem/flow-analysis/reachability_break_A05_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// 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 is some type `T` is made a type of interest
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo:

Suggested change
/// @description Checks that is some type `T` is made a type of interest
/// @description Checks that if some type `T` is made a type of interest

Same issue in about 4 other libraries.

/// `before(N)` then the variable can be promoted to `T` in `after(N)`.
/// @author [email protected]

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;
s = T();
s.answer();
}
}

test2() {
S s = S();
for (var j in []) {
if (s is T) {}
break;
s = T();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As usual, I think it would be nice to keep track of test cases that are concerned with the properties of dead code. Perhaps:

Suggested change
s = T();
// Unreachable, but does support promotion.
s = T();

It should be fine if there's one such comment in each of the tests that are dealing with unreachable code.

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();
}
100 changes: 100 additions & 0 deletions TypeSystem/flow-analysis/reachability_break_A05_t02.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// 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 is some type `T` is made a type of interest
/// `before(N)` then the variable can be promoted to `T` in `after(N)`.
/// @author [email protected]

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;
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();
}
100 changes: 100 additions & 0 deletions TypeSystem/flow-analysis/reachability_break_A05_t03.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// 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 is some type `T` is made a type of interest
/// `after(N)` then the variable cannot be promoted to `T` in `before(N)`.
/// @author [email protected]

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;
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);
}
Loading