Skip to content

Commit dddb43e

Browse files
authored
#3057. Add for(;;) statements tests (#3108)
* #3057. Add for(;;) statements tests * Apply review suggestions.
1 parent 113ab4f commit dddb43e

21 files changed

+735
-35
lines changed

TypeSystem/flow-analysis/definite_assignment_A03_t01.dart

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@
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 for loop
10+
/// @description Checks that assignment in the first operand of `for(;;) loop is
11+
/// treated by the flow analysis as a definite assignment.
1012
/// @author [email protected]
1113
12-
1314
main() {
1415
int n;
15-
for (n = 42; n < 1;) {
16+
for (n = 42; false;) {
1617
}
1718
n; // It's not an error to read local non-nullable variable when it is definitely assigned
1819
}

TypeSystem/flow-analysis/definite_assignment_A03_t02.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 for loop
10+
/// @description Checks that assignment in the body of`for(;;) loop with empty
11+
/// operands is treated by the flow analysis as a definite assignment.
1012
/// @author [email protected]
1113
1214
main() {

TypeSystem/flow-analysis/definite_assignment_A03_t03.dart

Lines changed: 0 additions & 17 deletions
This file was deleted.

TypeSystem/flow-analysis/definite_assignment_A03_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 for loop
10+
/// @description Checks that assignment in the third operand of `for(;;) loop is
11+
/// not treated by the flow analysis as a definite assignment.
1012
/// @author [email protected]
1113
1214
main() {

TypeSystem/flow-analysis/definite_assignment_A03_t05.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 for loop
10+
/// @description Checks that assignment in the body of`for(;;) loop with not
11+
/// empty operands is not treated by the flow analysis as a definite assignment.
1012
/// @author [email protected]
1113
1214
main() {
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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 for statement: If `N` is a for statement of the form
6+
/// `for (D; C; U) S,` then:
7+
/// - Let `before(D) = before(N)`.
8+
/// - Let `before(C) = conservativeJoin(after(D), assignedIn(N), capturedIn(N))`
9+
/// - Let `before(S) = split(true(C))`.
10+
/// - Let `before(U) = merge(after(S), continue(S))`.
11+
/// - Let `after(N) = inheritTested(join(false(C), unsplit(break(S))), after(U))`
12+
///
13+
/// @description Checks that
14+
/// `before(C) = conservativeJoin(after(D), assignedIn(N), capturedIn(N))`. Test
15+
/// that if the static type of `D` is `Never` then `C` is dead code and an
16+
/// assignment in it will never happen.
17+
/// @author [email protected]
18+
/// @issue 60089
19+
20+
Never foo() => throw "Never";
21+
22+
test1() {
23+
late int i;
24+
if (2 > 1) {
25+
for (foo(); (i = 42) < 0;) {}
26+
}
27+
i; // Definitely unassigned.
28+
//^
29+
// [analyzer] unspecified
30+
// [cfe] unspecified
31+
}
32+
33+
test2() {
34+
late int i;
35+
if (2 > 1) {
36+
[
37+
for (foo(); (i = 42) < 0;) 0
38+
];
39+
}
40+
i; // Definitely unassigned.
41+
//^
42+
// [analyzer] unspecified
43+
// [cfe] unspecified
44+
}
45+
46+
test3() {
47+
late int i;
48+
if (2 > 1) {
49+
<int, int>{
50+
for (foo(); (i = 42) < 0;)
51+
0: 0
52+
};
53+
}
54+
i; // Definitely unassigned.
55+
//^
56+
// [analyzer] unspecified
57+
// [cfe] unspecified
58+
}
59+
60+
main() {
61+
print(test1);
62+
print(test2);
63+
print(test3);
64+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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 for statement: If `N` is a for statement of the form
6+
/// `for (D; C; U) S,` then:
7+
/// - Let `before(D) = before(N)`.
8+
/// - Let `before(C) = conservativeJoin(after(D), assignedIn(N), capturedIn(N))`
9+
/// - Let `before(S) = split(true(C))`.
10+
/// - Let `before(U) = merge(after(S), continue(S))`.
11+
/// - Let `after(N) = inheritTested(join(false(C), unsplit(break(S))), after(U))`
12+
///
13+
/// @description Checks that
14+
/// `before(C) = conservativeJoin(after(D), assignedIn(N), capturedIn(N))`. Test
15+
/// that if the static type of `D` is other than `Never` then an assignment in
16+
/// `C` is a definite assignment.
17+
/// @author [email protected]
18+
19+
test1() {
20+
int i;
21+
for (; (i = 42) < 0;) {
22+
}
23+
i; // Definitely assigned.
24+
}
25+
26+
test2() {
27+
int i;
28+
[
29+
for (; (i = 42) < 0;) 0
30+
];
31+
i; // Definitely assigned.
32+
}
33+
34+
test3() {
35+
int i;
36+
<int, int>{
37+
for (; (i = 42) < 0;) 0: 0
38+
};
39+
i; // Definitely assigned.
40+
}
41+
42+
main() {
43+
test1();
44+
test2();
45+
test3();
46+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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 for statement: If `N` is a for statement of the form
6+
/// `for (D; C; U) S,` then:
7+
/// - Let `before(D) = before(N)`.
8+
/// - Let `before(C) = conservativeJoin(after(D), assignedIn(N), capturedIn(N))`
9+
/// - Let `before(S) = split(true(C))`.
10+
/// - Let `before(U) = merge(after(S), continue(S))`.
11+
/// - Let `after(N) = inheritTested(join(false(C), unsplit(break(S))), after(U))`
12+
///
13+
/// @description Checks that `before(S) = split(true(C))`. Test that if the
14+
/// static type of `C` is `Never`, then an assignment in `S` will never happen.
15+
/// @author [email protected]
16+
/// @issue 60089
17+
18+
Never foo() => throw "Never";
19+
20+
test1() {
21+
late int i;
22+
if (2 > 1) {
23+
for (; foo();) {
24+
i = 42;
25+
}
26+
}
27+
i; // Definitely unassigned.
28+
//^
29+
// [analyzer] unspecified
30+
// [cfe] unspecified
31+
}
32+
33+
test2() {
34+
late int i;
35+
if (2 > 1) {
36+
[
37+
for (; foo();) i = 42
38+
];
39+
}
40+
i; // Definitely unassigned.
41+
//^
42+
// [analyzer] unspecified
43+
// [cfe] unspecified
44+
}
45+
46+
test3() {
47+
late int i;
48+
if (2 > 1) {
49+
<int, int>{
50+
for (; foo();)
51+
0: i = 42
52+
};
53+
}
54+
i; // Definitely unassigned.
55+
//^
56+
// [analyzer] unspecified
57+
// [cfe] unspecified
58+
}
59+
60+
test4() {
61+
late int i;
62+
if (2 > 1) {
63+
<int, int>{
64+
for (; foo();)
65+
i = 42: 0
66+
};
67+
}
68+
i; // Definitely unassigned.
69+
//^
70+
// [analyzer] unspecified
71+
// [cfe] unspecified
72+
}
73+
74+
main() {
75+
print(test1);
76+
print(test2);
77+
print(test3);
78+
print(test4);
79+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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 for statement: If `N` is a for statement of the form
6+
/// `for (D; C; U) S,` then:
7+
/// - Let `before(D) = before(N)`.
8+
/// - Let `before(C) = conservativeJoin(after(D), assignedIn(N), capturedIn(N))`
9+
/// - Let `before(S) = split(true(C))`.
10+
/// - Let `before(U) = merge(after(S), continue(S))`.
11+
/// - Let `after(N) = inheritTested(join(false(C), unsplit(break(S))), after(U))`
12+
///
13+
/// @description Checks that `before(S) = split(true(C))`. Test that if `C` is a
14+
/// `true` literal then assignment in `S` is a definite assignment.
15+
/// @author [email protected]
16+
17+
main() {
18+
int i;
19+
for (; true;) {
20+
i = 42;
21+
break;
22+
}
23+
i; // Definitely assigned.
24+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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 for statement: If `N` is a for statement of the form
6+
/// `for (D; C; U) S,` then:
7+
/// - Let `before(D) = before(N)`.
8+
/// - Let `before(C) = conservativeJoin(after(D), assignedIn(N), capturedIn(N))`
9+
/// - Let `before(S) = split(true(C))`.
10+
/// - Let `before(U) = merge(after(S), continue(S))`.
11+
/// - Let `after(N) = inheritTested(join(false(C), unsplit(break(S))), after(U))`
12+
///
13+
/// @description Checks that `before(S) = split(true(C))`. Test that if `C` is a
14+
/// `false` literal then assignment in `S` will never happen.
15+
/// @author [email protected]
16+
/// @issue 60089
17+
18+
main() {
19+
late int i;
20+
for (; false;) {
21+
i = 42;
22+
}
23+
i; // Definitely unassigned.
24+
//^
25+
// [analyzer] unspecified
26+
// [cfe] unspecified
27+
}

0 commit comments

Comments
 (0)