Skip to content

Commit 6ff5ec3

Browse files
authored
#3057. Add more try-catch(-finally) tests (#3149)
1 parent 2b38fe4 commit 6ff5ec3

File tree

3 files changed

+136
-0
lines changed

3 files changed

+136
-0
lines changed
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 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 if `B` is empty then assignment in `alternatives`
14+
/// is not treated as unreachable.
15+
/// @author [email protected]
16+
17+
test() {
18+
late int i;
19+
try {
20+
} on Exception catch (_) {
21+
i = 42;
22+
}
23+
i; // Possibly assigned
24+
}
25+
26+
main() {
27+
print(test);
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 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 if `B` is empty and some promoted variable is
14+
/// captured in `alternatives` then this variable is demoted.
15+
/// @author [email protected]
16+
17+
main() {
18+
int? i = 1 > 2 ? 42 : null;
19+
if (i != null) { // `i` promoted to `int`
20+
try {
21+
} on Exception catch (_) {
22+
() {i = 42;}; // `i` demoted to `int?`
23+
}
24+
i.isEven;
25+
// ^^^^^^
26+
// [analyzer] unspecified
27+
// [cfe] unspecified
28+
}
29+
}
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 finally: If `N` is a try/catch/finally statement of the
6+
/// form `try B1 alternatives finally F` then:
7+
/// - Let `before(B1) = before(N)`
8+
/// - Let `before(Si) = join(after(B1), conservativeJoin(before(N),
9+
/// assignedIn(B1), capturedIn(B1)))`, where `Si`
10+
/// is the body of the i'th alternative
11+
/// - Let
12+
/// `after(N) = join(attachFinally(after(B1), before(F), after(F)), M1 .. Mk)`
13+
/// where `Mj = attachFinally(after(Sj), before(F), after(F))`
14+
///
15+
/// @description Checks that if a type `T` is made a type of interest in
16+
/// `before(N)` then it can be promoted in `B1`, `Si`, `F` and `after(N)`.
17+
/// @author [email protected]
18+
19+
class S {}
20+
21+
class T extends S {
22+
int answer() => 42;
23+
}
24+
25+
class C {
26+
T v;
27+
C(this.v);
28+
}
29+
30+
test1() {
31+
S s = S();
32+
if (s is T) {} // make `T` a type of interest
33+
try {
34+
s = T();
35+
s.answer();
36+
} catch (_) {
37+
} finally {
38+
}
39+
}
40+
41+
test2() {
42+
S s = S();
43+
if (s is T) {}
44+
try {
45+
} catch (_) {
46+
(s,) = (T(),);
47+
s.answer();
48+
} finally {
49+
}
50+
}
51+
52+
test3() {
53+
S s = S();
54+
if (s is T) {}
55+
try {
56+
} catch (_) {
57+
} finally {
58+
(x: s) = (x: T());
59+
s.answer();
60+
}
61+
}
62+
63+
test4() {
64+
S s = S();
65+
if (s is T) {}
66+
try {
67+
} catch (_) {
68+
} finally {
69+
}
70+
C(v: s) = C(T());
71+
s.answer();
72+
}
73+
74+
main() {
75+
print(test1);
76+
print(test2);
77+
print(test3);
78+
print(test4);
79+
}

0 commit comments

Comments
 (0)