Skip to content

#3057. Add more try-catch-finally tests #3146

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions TypeSystem/flow-analysis/reachability_try_catch_A03_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

/// @assertion try catch: If `N` is a try/catch statement of the form
/// `try B alternatives` then:
/// - Let `before(B) = before(N)`
/// - For each catch block on `Ti Si` in alternatives:
/// - Let
/// `before(Si) = conservativeJoin(before(N), assignedIn(B), capturedIn(B))`
/// - Let `after(N) = join(after(B), after(C0), ..., after(Ck))`
///
/// @description Checks that if a type `T` is made a type of interest in
/// `before(N)` then it can be promoted in `B`, `alternatives` and `after(N)`.
/// @author [email protected]

class S {}

class T extends S {
int answer() => 42;
}

class C {
T v;
C(this.v);
}

test1() {
S s = S();
if (s is T) {} // make `T` a type of interest
try {
s = T();
s.answer();
} catch (_) {
}
}

test2() {
S s = S();
if (s is T) {}
try {
} catch (_) {
(s,) = (T(),);
s.answer();
}
}

test3() {
S s = S();
if (s is T) {}
try {
} catch (_) {
}
C(v: s) = C(T());
s.answer();
}

main() {
print(test1);
print(test2);
print(test3);
}
63 changes: 63 additions & 0 deletions TypeSystem/flow-analysis/reachability_try_catch_A03_t02.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

/// @assertion try catch: If `N` is a try/catch statement of the form
/// `try B alternatives` then:
/// - Let `before(B) = before(N)`
/// - For each catch block on `Ti Si` in alternatives:
/// - Let
/// `before(Si) = conservativeJoin(before(N), assignedIn(B), capturedIn(B))`
/// - Let `after(N) = join(after(B), after(C0), ..., after(Ck))`
///
/// @description Checks that if a type `T` is made a type of interest in `B`
/// then it can be promoted in `B`, `alternatives` and `after(N)`.
/// @author [email protected]
/// @issue 60519

class S {}

class T extends S {
int answer() => 42;
}

class C {
T v;
C(this.v);
}

test1() {
S s = S();
try {
if (s is T) {} // make `T` a type of interest
s = T();
s.answer();
} catch (_) {
}
}

test2() {
S s = S();
try {
if (s is T) {}
} catch (_) {
(s,) = (T(),);
s.answer();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would expect this one to fail because before(S0) = conservativeJoin(before(N), ...), where T is not a type of interest for s.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But according to the dart-lang/sdk#60519 this should be an error.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly, it should be an error because T is not a type of interest for s at the location of the pattern assignment.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

However, I just created dart-lang/language#4328 in order to clarify the situation: The implemented behavior appears to be different from the specified behavior.

}
}

test3() {
S s = S();
try {
if (s is T) {}
} catch (_) {
}
C(v: s) = C(T());
s.answer();
}

main() {
print(test1);
print(test2);
print(test3);
}
63 changes: 63 additions & 0 deletions TypeSystem/flow-analysis/reachability_try_catch_A03_t03.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

/// @assertion try catch: If `N` is a try/catch statement of the form
/// `try B alternatives` then:
/// - Let `before(B) = before(N)`
/// - For each catch block on `Ti Si` in alternatives:
/// - Let
/// `before(Si) = conservativeJoin(before(N), assignedIn(B), capturedIn(B))`
/// - Let `after(N) = join(after(B), after(C0), ..., after(Ck))`
///
/// @description Checks that if a type `T` is made a type of interest in
/// `alternatives` then it can be promoted in `alternatives` and `after(N)`.
/// @author [email protected]
/// @issue 60519

class S {}

class T extends S {
int answer() => 42;
}

class C {
T v;
C(this.v);
}

test1() {
S s = S();
try {} catch (_) {
if (s is T) {} // make `T` a type of interest
s = T();
s.answer();
}
}

test2() {
S s = S();
try {
} on Exception catch (_) {
if (s is T) {}
} catch (_) {
(s,) = (T(),);
s.answer();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd expect this to fail because each alternative starts with before(N) (plus some assigned and some captured bits, but they won't change anything about types of interest).

}
}

test3() {
S s = S();
try {
} on Exception catch (_) {
if (s is T) {}
}
C(v: s) = C(T());
s.answer();
}

main() {
print(test1);
print(test2);
print(test3);
}
54 changes: 54 additions & 0 deletions TypeSystem/flow-analysis/reachability_try_catch_A03_t04.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

/// @assertion try catch: If `N` is a try/catch statement of the form
/// `try B alternatives` then:
/// - Let `before(B) = before(N)`
/// - For each catch block on `Ti Si` in alternatives:
/// - Let
/// `before(Si) = conservativeJoin(before(N), assignedIn(B), capturedIn(B))`
/// - Let `after(N) = join(after(B), after(C0), ..., after(Ck))`
///
/// @description Checks that if a type `T` is made a type of interest in
/// `alternatives` then it cannot be promoted in `B`.
/// @author [email protected]

class S {}

class T extends S {
int answer() => 42;
}

test1() {
S s = S();
try {
s = T();
s.answer();
// ^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
} catch (_) {
if (s is T) {} // make `T` a type of interest
}
}

test2() {
S s = S();
try {
} on Exception catch (_) {
s = T();
s.answer();
// ^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
} catch (_) {
if (s is T) {}
}
}


main() {
print(test1);
print(test2);
}
75 changes: 75 additions & 0 deletions TypeSystem/flow-analysis/reachability_try_finally_A04_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

/// @assertion try finally: If `N` is a try/finally statement of the form
/// `try B1 finally B2` then:
/// - Let `before(B1) = split(before(N))`
/// - Let `before(B2) = split(join(drop(after(B1)),
/// conservativeJoin(before(N), assignedIn(B1), capturedIn(B1))))`
/// - Let `after(N) = restrict(after(B1), after(B2), assignedIn(B2))`
///
/// @description Checks that if a type `T` is made a type of interest in
/// `before(N)` then it can be promoted in `B1`, `B2` and `after(N)`.
/// @author [email protected]

class S {}

class T extends S {
int answer() => 42;
}

class C {
T v;
C(this.v);
}

test1() {
S s = S();
if (s is T) {} // make `T` a type of interest
try {
s = T();
s.answer();
} finally {
}
}

test2() {
S s = S();
if (s is T) {}
try {
} catch (_) {
(s,) = (T(),);
s.answer();
} finally {
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, we should certainly test try/catch/finally even though it's not yet in the spec. Best effort, as usual. ;-)

But I guess they'd fit better in a series of tests named reachability_try_catch_finally_A*_t*.dart.


test3() {
S s = S();
if (s is T) {}
try {
} catch (_) {
} finally {
(x: s) = (x: T());
s.answer();
}
}

test4() {
S s = S();
if (s is T) {}
try {
} catch (_) {
} finally {
}
C(v: s) = C(T());
s.answer();
}

main() {
print(test1);
print(test2);
print(test3);
print(test4);
}
76 changes: 76 additions & 0 deletions TypeSystem/flow-analysis/reachability_try_finally_A04_t02.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

/// @assertion try finally: If `N` is a try/finally statement of the form
/// `try B1 finally B2` then:
/// - Let `before(B1) = split(before(N))`
/// - Let `before(B2) = split(join(drop(after(B1)),
/// conservativeJoin(before(N), assignedIn(B1), capturedIn(B1))))`
/// - Let `after(N) = restrict(after(B1), after(B2), assignedIn(B2))`
///
/// @description Checks that if a type `T` is made a type of interest in `B1`
/// then it can be promoted in `B1`, `B2` and `after(N)`.
/// @author [email protected]
/// @issue 60519

class S {}

class T extends S {
int answer() => 42;
}

class C {
T v;
C(this.v);
}

test1() {
S s = S();
try {
if (s is T) {} // make `T` a type of interest
s = T();
s.answer();
} finally {
}
}

test2() {
S s = S();
try {
if (s is T) {}
} catch (_) {
(s,) = (T(),);
s.answer();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one should again fail, considering that the catch blocks have no more than before(N) when it comes to types of interest.

} finally {
}
}

test3() {
S s = S();
try {
if (s is T) {}
} catch (_) {
} finally {
(x: s) = (x: T());
s.answer();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto.

}
}

test4() {
S s = S();
try {
if (s is T) {}
} catch (_) {
} finally {
}
C(v: s) = C(T());
s.answer();
}

main() {
print(test1);
print(test2);
print(test3);
print(test4);
}
Loading