Skip to content

Commit 6ba3ad8

Browse files
authored
#3057. Add more for-statement tests (#3110)
Add more for-statement tests
1 parent 97481d5 commit 6ba3ad8

12 files changed

+704
-0
lines changed
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 variable is assigned `after(D)` then it is definitely assigned
16+
/// `before(C)`.
17+
/// @author [email protected]
18+
19+
test1() {
20+
int n;
21+
for (n = 42; n > 0;) { // n definitely assigned
22+
break;
23+
}
24+
}
25+
26+
test2() {
27+
int n;
28+
[
29+
for (n = 42; n < 0;)
30+
0
31+
];
32+
}
33+
34+
test3() {
35+
int n;
36+
<int, int>{
37+
for (n = 42; n < 0;)
38+
0: 0
39+
};
40+
}
41+
42+
main() {
43+
test1();
44+
test2();
45+
test3();
46+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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 `assignedIn(U)` is detected by flow analysis.
16+
/// @author [email protected]
17+
18+
test1() {
19+
late int n;
20+
for (
21+
;
22+
() {
23+
if (1 > 2) {
24+
n; // possibly assigned
25+
}
26+
return true;
27+
}();
28+
n = 42
29+
) {}
30+
}
31+
32+
test2() {
33+
late int n;
34+
[
35+
for (
36+
;
37+
() {
38+
if (1 > 2) {
39+
n;
40+
}
41+
return true;
42+
}();
43+
n = 42
44+
)
45+
0,
46+
];
47+
}
48+
49+
test3() {
50+
late int n;
51+
<int, int>{
52+
for (
53+
;
54+
() {
55+
if (1 > 2) {
56+
n;
57+
}
58+
return true;
59+
}();
60+
n = 42
61+
)
62+
0: 0,
63+
};
64+
}
65+
66+
main() {
67+
print(test1);
68+
print(test2);
69+
print(test3);
70+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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 `assignedIn(S)` is detected by flow analysis.
16+
/// @author [email protected]
17+
18+
test1() {
19+
late int n;
20+
for (
21+
;
22+
() {
23+
if (1 > 2) {
24+
n; // possibly assigned
25+
}
26+
return true;
27+
}();
28+
) {
29+
n = 42;
30+
}
31+
}
32+
33+
test2() {
34+
late int n;
35+
[
36+
for (
37+
;
38+
() {
39+
if (1 > 2) {
40+
n;
41+
}
42+
return true;
43+
}();
44+
)
45+
n = 42,
46+
];
47+
}
48+
49+
test3() {
50+
late int n;
51+
<int, int>{
52+
for (
53+
;
54+
() {
55+
if (1 > 2) {
56+
n;
57+
}
58+
return true;
59+
}();
60+
)
61+
n = 42: 0,
62+
};
63+
}
64+
65+
test4() {
66+
late int n;
67+
<int, int>{
68+
for (
69+
;
70+
() {
71+
if (1 > 2) {
72+
n;
73+
}
74+
return true;
75+
}();
76+
)
77+
0: n = 42,
78+
};
79+
}
80+
81+
main() {
82+
print(test1);
83+
print(test2);
84+
print(test3);
85+
print(test4);
86+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 an assignment in `C` doesn't affect value of `before(D)`.
16+
/// @author [email protected]
17+
18+
test1() {
19+
late int n;
20+
for (n; (n = 42) < 0;) { // Definitely unassigned
21+
// ^
22+
// [analyzer] unspecified
23+
// [cfe] unspecified
24+
break;
25+
}
26+
}
27+
28+
test2() {
29+
int n;
30+
[
31+
for (n; (n = 42) < 0;) 0
32+
// ^
33+
// [analyzer] unspecified
34+
// [cfe] unspecified
35+
];
36+
}
37+
38+
test3() {
39+
int n;
40+
<int, int>{
41+
for (n; (n = 42) < 0;) 0: 0
42+
// ^
43+
// [analyzer] unspecified
44+
// [cfe] unspecified
45+
};
46+
}
47+
48+
main() {
49+
print(test1);
50+
print(test2);
51+
print(test3);
52+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 an assignment in `U` doesn't affect value of `before(D)`.
16+
/// @author [email protected]
17+
18+
test1() {
19+
late int n;
20+
for (n;; n = 42) { // Definitely unassigned
21+
// ^
22+
// [analyzer] unspecified
23+
// [cfe] unspecified
24+
break;
25+
}
26+
}
27+
28+
test2() {
29+
int n;
30+
[
31+
for (n; ;n = 42) 0
32+
// ^
33+
// [analyzer] unspecified
34+
// [cfe] unspecified
35+
];
36+
}
37+
38+
test3() {
39+
int n;
40+
<int, int>{
41+
for (n; ;n = 42) 0: 0
42+
// ^
43+
// [analyzer] unspecified
44+
// [cfe] unspecified
45+
};
46+
}
47+
48+
main() {
49+
print(test1);
50+
print(test2);
51+
print(test3);
52+
}
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 an assignment in `S` doesn't affect value of `before(D)`.
16+
/// @author [email protected]
17+
18+
test1() {
19+
late int n;
20+
for (n;;) { // Definitely unassigned
21+
// ^
22+
// [analyzer] unspecified
23+
// [cfe] unspecified
24+
n = 42;
25+
break;
26+
}
27+
}
28+
29+
test2() {
30+
int n;
31+
[
32+
for (n; ;) n = 42
33+
// ^
34+
// [analyzer] unspecified
35+
// [cfe] unspecified
36+
];
37+
}
38+
39+
test3() {
40+
int n;
41+
<int, int>{
42+
for (n; ;) n = 42: 0
43+
// ^
44+
// [analyzer] unspecified
45+
// [cfe] unspecified
46+
};
47+
}
48+
49+
test4() {
50+
int n;
51+
<int, int>{
52+
for (n; ;) 0: n = 42
53+
// ^
54+
// [analyzer] unspecified
55+
// [cfe] unspecified
56+
};
57+
}
58+
59+
main() {
60+
print(test1);
61+
print(test2);
62+
print(test3);
63+
print(test4);
64+
}

0 commit comments

Comments
 (0)