Skip to content

Commit 113ab4f

Browse files
authored
#3057. Add while statements tests and update assignments (#3107)
Add while statements tests and update assignments
1 parent 398b637 commit 113ab4f

14 files changed

+203
-33
lines changed

TypeSystem/flow-analysis/definite_assignment_A05_t01.dart

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
/// @assertion assigned is a boolean value indicating whether the variable has
6-
/// definitely been assigned at the given point in the source code. When assigned
7-
/// is true, we say that the variable is definitely assigned at that point.
5+
/// @assertion `assigned` is a boolean value indicating whether the variable has
6+
/// definitely been assigned at the given point in the source code. When
7+
/// `assigned` is true, we say that the variable is definitely assigned at that
8+
/// point.
89
///
9-
/// @description Checks definite assignment and while loop
10+
/// @description Checks that if the condition of a `while` statement is not the
11+
/// `true` literal, then an assignment in the body of the loop is not treated by
12+
/// flow analysis as a definite assignment.
1013
/// @author [email protected]
1114
1215
main() {

TypeSystem/flow-analysis/definite_assignment_A05_t02.dart

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
/// @assertion assigned is a boolean value indicating whether the variable has
6-
/// definitely been assigned at the given point in the source code. When assigned
7-
/// is true, we say that the variable is definitely assigned at that point.
5+
/// @assertion `assigned` is a boolean value indicating whether the variable has
6+
/// definitely been assigned at the given point in the source code. When
7+
/// `assigned` is true, we say that the variable is definitely assigned at that
8+
/// point.
89
///
9-
/// @description Checks definite assignment and while loop
10+
/// @description Checks that if the condition of a `while` statement is the
11+
/// `true` literal, then an assignment in the body of the loop is treated by
12+
/// flow analysis as a definite assignment.
1013
/// @author [email protected]
1114
1215
main() {

TypeSystem/flow-analysis/definite_assignment_A05_t03.dart

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,19 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
/// @assertion assigned is a boolean value indicating whether the variable has
6-
/// definitely been assigned at the given point in the source code. When assigned
7-
/// is true, we say that the variable is definitely assigned at that point.
5+
/// @assertion `assigned` is a boolean value indicating whether the variable has
6+
/// definitely been assigned at the given point in the source code. When
7+
/// `assigned` is true, we say that the variable is definitely assigned at that
8+
/// point.
89
///
9-
/// @description Checks definite assignment and while loop
10+
/// @description Checks that if the condition of a `while` statement is the
11+
/// `false` literal, then an assignment in the body of the loop is treated by
12+
/// flow analysis as a definite unassignment.
1013
/// @author [email protected]
14+
/// @issue 60322
1115
1216
main() {
13-
int n;
17+
late int n;
1418
while (false) {
1519
n = 42;
1620
}

TypeSystem/flow-analysis/definite_assignment_A05_t04.dart

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
/// @assertion assigned is a boolean value indicating whether the variable has
6-
/// definitely been assigned at the given point in the source code. When assigned
7-
/// is true, we say that the variable is definitely assigned at that point.
5+
/// @assertion `assigned` is a boolean value indicating whether the variable has
6+
/// definitely been assigned at the given point in the source code. When
7+
/// `assigned` is true, we say that the variable is definitely assigned at that
8+
/// point.
89
///
9-
/// @description Checks definite assignment and while loop
10+
/// @description Checks that an assignment in the condition of a `while`
11+
/// statement is the is treated by flow analysis as a definite assignment.
1012
/// @author [email protected]
1113
1214
main() {

TypeSystem/flow-analysis/promotion_via_assignment_A01_t01.dart

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
/// and T <: S and not S <: T
1313
/// and T is a type of interest for x in tested
1414
///
15-
/// @description Checks that a variable is promotable to the type T if all
15+
/// @description Checks that a variable is promotable to the type `T` if all
1616
/// requirements above are met.
1717
/// @author [email protected]
1818
@@ -23,9 +23,7 @@ class T extends S {
2323

2424
main() {
2525
S x = new S();
26-
if (x is T) {
27-
// nothing
28-
}
26+
if (x is T) {} // make `T` a type of interest
2927
x = new T();
3028
x.foo();
3129
}

TypeSystem/flow-analysis/promotion_via_assignment_A02_t01.dart

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
/// and T <: S and not S <: T
1313
/// and T is a type of interest for x in tested
1414
///
15-
/// @description Checks that if captured is true then promotion via assignment is
16-
/// not performed
15+
/// @description Checks that if `captured` is `true` then promotion via
16+
/// assignment is not performed
1717
/// @author [email protected]
1818
1919
class S {}
@@ -29,9 +29,7 @@ main() {
2929
x = v;
3030
}
3131
f = bar;
32-
if (x is T) {
33-
// nothing
34-
}
32+
if (x is T) {} // make `T` a type of interest
3533
x = new T();
3634
x.foo();
3735
// ^^^

TypeSystem/flow-analysis/promotion_via_assignment_A03_t01.dart

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
/// and T <: S and not S <: T
1313
/// and T is a type of interest for x in tested
1414
///
15-
/// @description Checks that if T <: S and S <: T then promotion via assignment
16-
/// is not performed
15+
/// @description Checks that if `T <: S` and `S <: T` then promotion via
16+
/// assignment is not performed
1717
/// @author [email protected]
1818
1919
import '../../Utils/expect.dart';
@@ -25,9 +25,7 @@ class T extends S {
2525

2626
main() {
2727
dynamic x = new S();
28-
if (x is T) {
29-
// nothing
30-
}
28+
if (x is T) {} // make `T` a type of interest
3129
x = new T();
3230
x.foo();
3331
Expect.throws(() {

TypeSystem/flow-analysis/promotion_via_assignment_A04_t01.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
/// and T <: S and not S <: T
1313
/// and T is a type of interest for x in tested
1414
///
15-
/// @description Checks that if T is a type of interest for x in tested then
16-
/// promotion via assignment is not performed
15+
/// @description Checks that if `T` is not a type of interest for `x` in
16+
/// `tested` then promotion via assignment is not performed.
1717
/// @author [email protected]
1818
1919
class S {}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
/// @assertion while statement: If `N` is a while statement of the form
6+
/// `while (E) S` then:
7+
/// - Let `before(E) = conservativeJoin(before(N), assignedIn(N), capturedIn(N))`
8+
/// - Let `before(S) = split(true(E))`.
9+
/// - Let `after(N) = inheritTested(join(false(E), unsplit(break(S))), after(S))`
10+
///
11+
/// @description Checks that if the static type of `E` is `Never` then `S` is
12+
/// dead code.
13+
/// @author [email protected]
14+
15+
Never foo() => throw "Never";
16+
17+
main() {
18+
late int i;
19+
if (2 > 1) {
20+
while (foo()) {
21+
i = 42; // Assigned in dead code
22+
}
23+
}
24+
i; // Definitely unassigned
25+
//^
26+
// [analyzer] unspecified
27+
// [cfe] unspecified
28+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
/// @assertion while statement: If `N` is a while statement of the form
6+
/// `while (E) S` then:
7+
/// - Let `before(E) = conservativeJoin(before(N), assignedIn(N), capturedIn(N))`
8+
/// - Let `before(S) = split(true(E))`.
9+
/// - Let `after(N) = inheritTested(join(false(E), unsplit(break(S))), after(S))`
10+
///
11+
/// @description Checks that if the static type of `E` is `Never` then
12+
/// `after(N)` is dead code.
13+
/// @author [email protected]
14+
15+
Never foo() => throw "Never";
16+
17+
main() {
18+
late int i;
19+
if (2 > 1) {
20+
while (foo()) {
21+
break;
22+
}
23+
i = 42; // Assigned in dead code
24+
}
25+
i; // Definitely unassigned
26+
//^
27+
// [analyzer] unspecified
28+
// [cfe] unspecified
29+
}

0 commit comments

Comments
 (0)