Skip to content

Commit d1efea3

Browse files
authored
#3057. Add flow analysis try-catch tests (#3137)
Add flow analysis try-catch tests.
1 parent b02ea05 commit d1efea3

9 files changed

+723
-0
lines changed
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 try catch: If `N` is a try/catch statement of the form
6+
/// `try B alternatives` then:
7+
/// - Let `before(B) = before(N)`
8+
/// - For each catch block on `Ti Si` in alternatives:
9+
/// - Let
10+
/// `before(Si) = conservativeJoin(before(N), assignedIn(B), capturedIn(B))`
11+
/// - Let `after(N) = join(after(B), after(C0), ..., after(Ck))`
12+
///
13+
/// @description Checks that
14+
/// `before(Si) = conservativeJoin(before(N), assignedIn(B), capturedIn(B))`.
15+
/// Test that if some variable is assigned in `B` then it is "possibly assigned"
16+
/// in each `Si`.
17+
/// @author [email protected]
18+
19+
class C {
20+
int v;
21+
C(this.v);
22+
}
23+
24+
Never foo() => throw "Never";
25+
26+
test1() {
27+
late int i;
28+
try {
29+
foo();
30+
i = 42;
31+
} on Exception catch (_) {
32+
i; // Possibly assigned
33+
} catch (_) {
34+
i; // Possibly assigned
35+
}
36+
}
37+
38+
test2(Never n) {
39+
late int i;
40+
try {
41+
n;
42+
(i,) = (42,);
43+
} on Exception catch (_) {
44+
i;
45+
} catch (_) {
46+
i;
47+
}
48+
}
49+
50+
test3<T extends Never>(T n) {
51+
late int i;
52+
try {
53+
n;
54+
(x: i) = (x: 42);
55+
} on Exception catch (_) {
56+
i;
57+
} catch (_) {
58+
i;
59+
}
60+
}
61+
62+
test4() {
63+
late int i;
64+
try {
65+
foo();
66+
C(v: i) = C(42);
67+
} on Exception catch (_) {
68+
i;
69+
} catch (_) {
70+
i;
71+
}
72+
}
73+
74+
main() {
75+
print(test1);
76+
print(test2);
77+
print(test3);
78+
print(test4);
79+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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 try catch: If `N` is a try/catch statement of the form
6+
/// `try B alternatives` then:
7+
/// - Let `before(B) = before(N)`
8+
/// - For each catch block on `Ti Si` in alternatives:
9+
/// - Let
10+
/// `before(Si) = conservativeJoin(before(N), assignedIn(B), capturedIn(B))`
11+
/// - Let `after(N) = join(after(B), after(C0), ..., after(Ck))`
12+
///
13+
/// @description Checks that
14+
/// `before(Si) = conservativeJoin(before(N), assignedIn(B), capturedIn(B))`.
15+
/// Test that if some variable is assigned in `B` then it is "possibly assigned"
16+
/// in each `Si`.
17+
/// @author [email protected]
18+
19+
class C {
20+
int v;
21+
C(this.v);
22+
}
23+
24+
test1() {
25+
int i;
26+
try {
27+
i = 42;
28+
} on Exception catch (_) {
29+
i; // Not definitely assigned
30+
// ^
31+
// [analyzer] unspecified
32+
// [cfe] unspecified
33+
} catch (_) {
34+
i;
35+
// ^
36+
// [analyzer] unspecified
37+
// [cfe] unspecified
38+
}
39+
}
40+
41+
test2() {
42+
int i;
43+
try {
44+
(i,) = (42,);
45+
} on Exception catch (_) {
46+
i;
47+
// ^
48+
// [analyzer] unspecified
49+
// [cfe] unspecified
50+
} catch (_) {
51+
i;
52+
// ^
53+
// [analyzer] unspecified
54+
// [cfe] unspecified
55+
}
56+
}
57+
58+
test3() {
59+
int i;
60+
try {
61+
(x: i) = (x: 42);
62+
} on Exception catch (_) {
63+
i;
64+
// ^
65+
// [analyzer] unspecified
66+
// [cfe] unspecified
67+
} catch (_) {
68+
i;
69+
// ^
70+
// [analyzer] unspecified
71+
// [cfe] unspecified
72+
}
73+
}
74+
75+
test4() {
76+
int i;
77+
try {
78+
C(v: i) = C(42);
79+
} on Exception catch (_) {
80+
i;
81+
// ^
82+
// [analyzer] unspecified
83+
// [cfe] unspecified
84+
} catch (_) {
85+
i;
86+
// ^
87+
// [analyzer] unspecified
88+
// [cfe] unspecified
89+
}
90+
}
91+
92+
main() {
93+
print(test1);
94+
print(test2);
95+
print(test3);
96+
print(test4);
97+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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 try catch: If `N` is a try/catch statement of the form
6+
/// `try B alternatives` then:
7+
/// - Let `before(B) = before(N)`
8+
/// - For each catch block on `Ti Si` in alternatives:
9+
/// - Let
10+
/// `before(Si) = conservativeJoin(before(N), assignedIn(B), capturedIn(B))`
11+
/// - Let `after(N) = join(after(B), after(C0), ..., after(Ck))`
12+
///
13+
/// @description Checks that
14+
/// `before(Si) = conservativeJoin(before(N), assignedIn(B), capturedIn(B))`.
15+
/// Test that if some promoted variable is `capturedIn(B)` then it is demoted in
16+
/// each `Si`.
17+
/// @author [email protected]
18+
19+
class C {
20+
int v;
21+
C(this.v);
22+
}
23+
24+
test1(int? n) {
25+
if (n != null) { // n promoted to int
26+
try {
27+
() {n = 42;}; // n demoted to int?
28+
} on Exception catch (_) {
29+
n.isEven;
30+
// ^^^^^^
31+
// [analyzer] unspecified
32+
// [cfe] unspecified
33+
} catch (_) {
34+
n.isEven;
35+
// ^^^^^^
36+
// [analyzer] unspecified
37+
// [cfe] unspecified
38+
}
39+
}
40+
}
41+
42+
test2(int? n) {
43+
if (n != null) {
44+
try {
45+
() {(n,) = (42,);};
46+
} on Exception catch (_) {
47+
n.isEven;
48+
// ^^^^^^
49+
// [analyzer] unspecified
50+
// [cfe] unspecified
51+
} catch (_) {
52+
n.isEven;
53+
// ^^^^^^
54+
// [analyzer] unspecified
55+
// [cfe] unspecified
56+
}
57+
}
58+
}
59+
60+
test3(int? n) {
61+
if (n != null) {
62+
try {
63+
() {(x: n) = (x: 42);};
64+
} on Exception catch (_) {
65+
n.isEven;
66+
// ^^^^^^
67+
// [analyzer] unspecified
68+
// [cfe] unspecified
69+
} catch (_) {
70+
n.isEven;
71+
// ^^^^^^
72+
// [analyzer] unspecified
73+
// [cfe] unspecified
74+
}
75+
}
76+
}
77+
78+
79+
test4(int? n) {
80+
if (n != null) {
81+
try {
82+
() {C(v: n) = C(42);};
83+
} on Exception catch (_) {
84+
n.isEven;
85+
// ^^^^^^
86+
// [analyzer] unspecified
87+
// [cfe] unspecified
88+
} catch (_) {
89+
n.isEven;
90+
// ^^^^^^
91+
// [analyzer] unspecified
92+
// [cfe] unspecified
93+
}
94+
}
95+
}
96+
97+
main() {
98+
print(test1);
99+
print(test2);
100+
print(test3);
101+
print(test4);
102+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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 try catch: If `N` is a try/catch statement of the form
6+
/// `try B alternatives` then:
7+
/// - Let `before(B) = before(N)`
8+
/// - For each catch block on `Ti Si` in alternatives:
9+
/// - Let
10+
/// `before(Si) = conservativeJoin(before(N), assignedIn(B), capturedIn(B))`
11+
/// - Let `after(N) = join(after(B), after(C0), ..., after(Ck))`
12+
///
13+
/// @description Checks that
14+
/// `before(Si) = conservativeJoin(before(N), assignedIn(B), capturedIn(B))`.
15+
/// Test that if some variable is assigned in `Si` it doesn't affect other `Sj`.
16+
/// @author [email protected]
17+
18+
class C {
19+
int v;
20+
C(this.v);
21+
}
22+
23+
test1() {
24+
late int i;
25+
try {
26+
} on Exception catch (_) {
27+
i = 42;
28+
} catch (_) {
29+
i; // Definitely unassigned
30+
// ^
31+
// [analyzer] unspecified
32+
// [cfe] unspecified
33+
}
34+
}
35+
36+
test2() {
37+
late int i;
38+
try {
39+
} on Exception catch (_) {
40+
i;
41+
// ^
42+
// [analyzer] unspecified
43+
// [cfe] unspecified
44+
} catch (_) {
45+
(i,) = (42,);
46+
}
47+
}
48+
49+
test3() {
50+
late int i;
51+
try {
52+
} on Exception catch (_) {
53+
(x: i) = (x: 42);
54+
} catch (_) {
55+
i;
56+
// ^
57+
// [analyzer] unspecified
58+
// [cfe] unspecified
59+
}
60+
}
61+
62+
test4() {
63+
late int i;
64+
try {
65+
} on Exception catch (_) {
66+
i;
67+
// ^
68+
// [analyzer] unspecified
69+
// [cfe] unspecified
70+
} catch (_) {
71+
C(v: i) = C(42);
72+
}
73+
}
74+
75+
main() {
76+
print(test1);
77+
print(test2);
78+
print(test3);
79+
print(test4);
80+
}

0 commit comments

Comments
 (0)