Skip to content

Commit aeca7cb

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

9 files changed

+578
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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 a type `T` is made a type of interest in
14+
/// `before(N)` then it can be promoted in `B`, `alternatives` and `after(N)`.
15+
/// @author [email protected]
16+
17+
class S {}
18+
19+
class T extends S {
20+
int answer() => 42;
21+
}
22+
23+
class C {
24+
T v;
25+
C(this.v);
26+
}
27+
28+
test1() {
29+
S s = S();
30+
if (s is T) {} // make `T` a type of interest
31+
try {
32+
s = T();
33+
s.answer();
34+
} catch (_) {
35+
}
36+
}
37+
38+
test2() {
39+
S s = S();
40+
if (s is T) {}
41+
try {
42+
} catch (_) {
43+
(s,) = (T(),);
44+
s.answer();
45+
}
46+
}
47+
48+
test3() {
49+
S s = S();
50+
if (s is T) {}
51+
try {
52+
} catch (_) {
53+
}
54+
C(v: s) = C(T());
55+
s.answer();
56+
}
57+
58+
main() {
59+
print(test1);
60+
print(test2);
61+
print(test3);
62+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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 a type `T` is made a type of interest in `B`
14+
/// then it can be promoted in `B`, `alternatives` and `after(N)`.
15+
/// @author [email protected]
16+
/// @issue 60519
17+
18+
class S {}
19+
20+
class T extends S {
21+
int answer() => 42;
22+
}
23+
24+
class C {
25+
T v;
26+
C(this.v);
27+
}
28+
29+
test1() {
30+
S s = S();
31+
try {
32+
if (s is T) {} // make `T` a type of interest
33+
s = T();
34+
s.answer();
35+
} catch (_) {
36+
}
37+
}
38+
39+
test2() {
40+
S s = S();
41+
try {
42+
if (s is T) {}
43+
} catch (_) {
44+
(s,) = (T(),);
45+
s.answer();
46+
}
47+
}
48+
49+
test3() {
50+
S s = S();
51+
try {
52+
if (s is T) {}
53+
} catch (_) {
54+
}
55+
C(v: s) = C(T());
56+
s.answer();
57+
}
58+
59+
main() {
60+
print(test1);
61+
print(test2);
62+
print(test3);
63+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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 a type `T` is made a type of interest in
14+
/// `alternatives` then it can be promoted in `alternatives` and `after(N)`.
15+
/// @author [email protected]
16+
/// @issue 60519
17+
18+
class S {}
19+
20+
class T extends S {
21+
int answer() => 42;
22+
}
23+
24+
class C {
25+
T v;
26+
C(this.v);
27+
}
28+
29+
test1() {
30+
S s = S();
31+
try {} catch (_) {
32+
if (s is T) {} // make `T` a type of interest
33+
s = T();
34+
s.answer();
35+
}
36+
}
37+
38+
test2() {
39+
S s = S();
40+
try {
41+
} on Exception catch (_) {
42+
if (s is T) {}
43+
} catch (_) {
44+
(s,) = (T(),);
45+
s.answer();
46+
}
47+
}
48+
49+
test3() {
50+
S s = S();
51+
try {
52+
} on Exception catch (_) {
53+
if (s is T) {}
54+
}
55+
C(v: s) = C(T());
56+
s.answer();
57+
}
58+
59+
main() {
60+
print(test1);
61+
print(test2);
62+
print(test3);
63+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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 a type `T` is made a type of interest in
14+
/// `alternatives` then it cannot be promoted in `B`.
15+
/// @author [email protected]
16+
17+
class S {}
18+
19+
class T extends S {
20+
int answer() => 42;
21+
}
22+
23+
test1() {
24+
S s = S();
25+
try {
26+
s = T();
27+
s.answer();
28+
// ^^^^^^
29+
// [analyzer] unspecified
30+
// [cfe] unspecified
31+
} catch (_) {
32+
if (s is T) {} // make `T` a type of interest
33+
}
34+
}
35+
36+
test2() {
37+
S s = S();
38+
try {
39+
} on Exception catch (_) {
40+
s = T();
41+
s.answer();
42+
// ^^^^^^
43+
// [analyzer] unspecified
44+
// [cfe] unspecified
45+
} catch (_) {
46+
if (s is T) {}
47+
}
48+
}
49+
50+
51+
main() {
52+
print(test1);
53+
print(test2);
54+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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 if a type `T` is made a type of interest in
13+
/// `before(N)` then it can be promoted in `B1`, `B2` and `after(N)`.
14+
/// @author [email protected]
15+
16+
class S {}
17+
18+
class T extends S {
19+
int answer() => 42;
20+
}
21+
22+
class C {
23+
T v;
24+
C(this.v);
25+
}
26+
27+
test1() {
28+
S s = S();
29+
if (s is T) {} // make `T` a type of interest
30+
try {
31+
s = T();
32+
s.answer();
33+
} finally {
34+
}
35+
}
36+
37+
test2() {
38+
S s = S();
39+
if (s is T) {}
40+
try {
41+
} catch (_) {
42+
(s,) = (T(),);
43+
s.answer();
44+
} finally {
45+
}
46+
}
47+
48+
test3() {
49+
S s = S();
50+
if (s is T) {}
51+
try {
52+
} catch (_) {
53+
} finally {
54+
(x: s) = (x: T());
55+
s.answer();
56+
}
57+
}
58+
59+
test4() {
60+
S s = S();
61+
if (s is T) {}
62+
try {
63+
} catch (_) {
64+
} finally {
65+
}
66+
C(v: s) = C(T());
67+
s.answer();
68+
}
69+
70+
main() {
71+
print(test1);
72+
print(test2);
73+
print(test3);
74+
print(test4);
75+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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 if a type `T` is made a type of interest in `B1`
13+
/// then it can be promoted in `B1`, `B2` and `after(N)`.
14+
/// @author [email protected]
15+
/// @issue 60519
16+
17+
class S {}
18+
19+
class T extends S {
20+
int answer() => 42;
21+
}
22+
23+
class C {
24+
T v;
25+
C(this.v);
26+
}
27+
28+
test1() {
29+
S s = S();
30+
try {
31+
if (s is T) {} // make `T` a type of interest
32+
s = T();
33+
s.answer();
34+
} finally {
35+
}
36+
}
37+
38+
test2() {
39+
S s = S();
40+
try {
41+
if (s is T) {}
42+
} catch (_) {
43+
(s,) = (T(),);
44+
s.answer();
45+
} finally {
46+
}
47+
}
48+
49+
test3() {
50+
S s = S();
51+
try {
52+
if (s is T) {}
53+
} catch (_) {
54+
} finally {
55+
(x: s) = (x: T());
56+
s.answer();
57+
}
58+
}
59+
60+
test4() {
61+
S s = S();
62+
try {
63+
if (s is T) {}
64+
} catch (_) {
65+
} finally {
66+
}
67+
C(v: s) = C(T());
68+
s.answer();
69+
}
70+
71+
main() {
72+
print(test1);
73+
print(test2);
74+
print(test3);
75+
print(test4);
76+
}

0 commit comments

Comments
 (0)