Skip to content

Commit 403d439

Browse files
committed
#3057. Add more try-finally tests
1 parent 4ebc13c commit 403d439

9 files changed

+674
-1
lines changed

TypeSystem/flow-analysis/reachability_try_finally_A02_t04.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ test1(int? n) {
3535
test2(int? n) {
3636
if (n != null) {
3737
try {
38-
() {(n,) = (42,);};
38+
() {(n,) = (42,);};
3939
} finally {
4040
n.isEven;
4141
// ^^^^^^
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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 finally: If `N` is a try/finally statement of the form
6+
/// `try B1 finally B2` then:
7+
/// - Let `before(B1) = split(before(N))`
8+
/// - Let `before(B2) = split(join(drop(after(B1)),
9+
/// conservativeJoin(before(N), assignedIn(B1), capturedIn(B1))))`
10+
/// - Let `after(N) = restrict(after(B1), after(B2), assignedIn(B2))`
11+
///
12+
/// @description Checks that `before(B2) = split(join(drop(after(B1)),
13+
/// conservativeJoin(before(N), assignedIn(B1), capturedIn(B1))))`. Test that if
14+
/// some variable is assigned in a `catch` part of `B1` then it is
15+
/// "possibly assigned" in `B2`.
16+
/// @author [email protected]
17+
18+
class C {
19+
int v;
20+
C(this.v);
21+
}
22+
23+
Never foo() => throw "Never";
24+
25+
test1() {
26+
late int i;
27+
try {
28+
print("To avoid empty body");
29+
} catch (_) {
30+
foo();
31+
i = 42;
32+
} finally {
33+
i; // Possibly assigned
34+
}
35+
}
36+
37+
test2(Never n) {
38+
late int i;
39+
try {
40+
print("To avoid empty body");
41+
} catch (_) {
42+
n;
43+
(i,) = (42,);
44+
} finally {
45+
i;
46+
}
47+
}
48+
49+
test3<T extends Never>(T n) {
50+
late int i;
51+
try {
52+
print("To avoid empty body");
53+
} catch (_) {
54+
n;
55+
(x: i) = (x: 42);
56+
} finally {
57+
i;
58+
}
59+
}
60+
61+
test4() {
62+
late int i;
63+
try {
64+
print("To avoid empty body");
65+
} catch (_) {
66+
foo();
67+
C(v: i) = C(42);
68+
} finally {
69+
i;
70+
}
71+
}
72+
73+
main() {
74+
print(test1);
75+
print(test2);
76+
print(test3);
77+
print(test4);
78+
}
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 finally: If `N` is a try/finally statement of the form
6+
/// `try B1 finally B2` then:
7+
/// - Let `before(B1) = split(before(N))`
8+
/// - Let `before(B2) = split(join(drop(after(B1)),
9+
/// conservativeJoin(before(N), assignedIn(B1), capturedIn(B1))))`
10+
/// - Let `after(N) = restrict(after(B1), after(B2), assignedIn(B2))`
11+
///
12+
/// @description Checks that `before(B2) = split(join(drop(after(B1)),
13+
/// conservativeJoin(before(N), assignedIn(B1), capturedIn(B1))))`. Test that if
14+
/// some variable is assigned in a `catch` part of `B1` then it is
15+
/// "possibly assigned" in `B2`.
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+
print("To avoid empty body");
27+
} catch (_) {
28+
if (false) {
29+
i = 42;
30+
}
31+
} finally {
32+
i; // Possibly assigned
33+
}
34+
}
35+
36+
test2() {
37+
late int i;
38+
try {
39+
print("To avoid empty body");
40+
} catch (_) {
41+
if (false) {
42+
(i, ) = (42, );
43+
}
44+
} finally {
45+
i;
46+
}
47+
}
48+
49+
test3() {
50+
late int i;
51+
try {
52+
print("To avoid empty body");
53+
} catch (_) {
54+
if (false) {
55+
(x: i) = (x: 42);
56+
}
57+
} finally {
58+
i;
59+
}
60+
}
61+
62+
test4() {
63+
late int i;
64+
try {
65+
print("To avoid empty body");
66+
} catch (_) {
67+
if (false) {
68+
C(v: i) = C(42);
69+
}
70+
} finally {
71+
i;
72+
}
73+
}
74+
75+
main() {
76+
print(test1);
77+
print(test2);
78+
print(test3);
79+
print(test4);
80+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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 finally: If `N` is a try/finally statement of the form
6+
/// `try B1 finally B2` then:
7+
/// - Let `before(B1) = split(before(N))`
8+
/// - Let `before(B2) = split(join(drop(after(B1)),
9+
/// conservativeJoin(before(N), assignedIn(B1), capturedIn(B1))))`
10+
/// - Let `after(N) = restrict(after(B1), after(B2), assignedIn(B2))`
11+
///
12+
/// @description Checks that `before(B2) = split(join(drop(after(B1)),
13+
/// conservativeJoin(before(N), assignedIn(B1), capturedIn(B1))))`. Test that if
14+
/// some variable is assigned in a catch part of `B1` then it is
15+
/// "possibly assigned" in `B2`.
16+
/// @author [email protected]
17+
18+
class C {
19+
int v;
20+
C(this.v);
21+
}
22+
23+
test1() {
24+
int i;
25+
try {
26+
print("To avoid empty body");
27+
} catch (_) {
28+
i = 42;
29+
} finally {
30+
i; // Possibly assigned
31+
// ^
32+
// [analyzer] unspecified
33+
// [cfe] unspecified
34+
}
35+
}
36+
37+
test2() {
38+
int i;
39+
try {
40+
print("To avoid empty body");
41+
} catch (_) {
42+
(i,) = (42,);
43+
} finally {
44+
i;
45+
// ^
46+
// [analyzer] unspecified
47+
// [cfe] unspecified
48+
}
49+
}
50+
51+
test3() {
52+
int i;
53+
try {
54+
print("To avoid empty body");
55+
} catch (_) {
56+
(x: i) = (x: 42);
57+
} finally {
58+
i;
59+
// ^
60+
// [analyzer] unspecified
61+
// [cfe] unspecified
62+
}
63+
}
64+
65+
test4() {
66+
int i;
67+
try {
68+
print("To avoid empty body");
69+
} catch (_) {
70+
C(v: i) = C(42);
71+
} finally {
72+
i;
73+
// ^
74+
// [analyzer] unspecified
75+
// [cfe] unspecified
76+
}
77+
}
78+
79+
main() {
80+
print(test1);
81+
print(test2);
82+
print(test3);
83+
print(test4);
84+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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 finally: If `N` is a try/finally statement of the form
6+
/// `try B1 finally B2` then:
7+
/// - Let `before(B1) = split(before(N))`
8+
/// - Let `before(B2) = split(join(drop(after(B1)),
9+
/// conservativeJoin(before(N), assignedIn(B1), capturedIn(B1))))`
10+
/// - Let `after(N) = restrict(after(B1), after(B2), assignedIn(B2))`
11+
///
12+
/// @description Checks that `before(B2) = split(join(drop(after(B1)),
13+
/// conservativeJoin(before(N), assignedIn(B1), capturedIn(B1))))`. Test that if
14+
/// some promoted variable is captured in a `catch` part of `B1` then it is
15+
/// demoted in `B2`.
16+
/// @author [email protected]
17+
18+
class C {
19+
int v;
20+
C(this.v);
21+
}
22+
23+
test1(int? n) {
24+
if (n != null) { // `n` promoted to `int`
25+
try {
26+
print("To avoid empty body");
27+
} catch (_) {
28+
() {n = 42;}; // `n` demoted to `int?`
29+
} finally {
30+
n.isEven;
31+
// ^^^^^^
32+
// [analyzer] unspecified
33+
// [cfe] unspecified
34+
}
35+
}
36+
}
37+
38+
test2(int? n) {
39+
if (n != null) {
40+
try {
41+
print("To avoid empty body");
42+
} catch (_) {
43+
() {(n,) = (42,);};
44+
} finally {
45+
n.isEven;
46+
// ^^^^^^
47+
// [analyzer] unspecified
48+
// [cfe] unspecified
49+
}
50+
}
51+
}
52+
53+
test3(int? n) {
54+
if (n != null) {
55+
try {
56+
print("To avoid empty body");
57+
} catch (_) {
58+
() {(x: n) = (x: 42);};
59+
} finally {
60+
n.isEven;
61+
// ^^^^^^
62+
// [analyzer] unspecified
63+
// [cfe] unspecified
64+
}
65+
}
66+
}
67+
68+
test4(int? n) {
69+
if (n != null) {
70+
try {
71+
print("To avoid empty body");
72+
} catch (_) {
73+
() {C(v: n) = C(42);};
74+
} finally {
75+
n.isEven;
76+
// ^^^^^^
77+
// [analyzer] unspecified
78+
// [cfe] unspecified
79+
}
80+
}
81+
}
82+
83+
main() {
84+
print(test1);
85+
print(test2);
86+
print(test3);
87+
print(test4);
88+
}

0 commit comments

Comments
 (0)