-
Notifications
You must be signed in to change notification settings - Fork 28
#3057. Add more try-finally tests #3145
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// 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 `before(B2) = split(join(drop(after(B1)), | ||
/// conservativeJoin(before(N), assignedIn(B1), capturedIn(B1))))`. Test that if | ||
/// some variable is assigned in a `catch` part of `B1` then it is | ||
/// "possibly assigned" in `B2`. | ||
/// @author [email protected] | ||
|
||
class C { | ||
int v; | ||
C(this.v); | ||
} | ||
|
||
Never foo() => throw "Never"; | ||
|
||
test1() { | ||
late int i; | ||
try { | ||
print("To avoid empty body"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is new, why would that suddenly be needed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is new, why would that suddenly be needed? It would make sense to have it if we have any indications that an empty try block would be treated differently than a non-empty one (e.g., it might be accepted as a guaranteed property of the empty There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here |
||
} catch (_) { | ||
foo(); | ||
i = 42; | ||
} finally { | ||
i; // Possibly assigned | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd expect So we'd not use Note that Paul also mentioned that there is no However, in the Right now I'll just try to use informal reasoning about which classification should apply (such as 'assigned' or not, or 'unreachable', whatever the test is focusing on). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With this, I'd say that I can see that the analyzer and CFE think otherwise, though. I don't really understand why there would be a need for a conservative join involving each of the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This means that the outcome of the following cases is similarly in need of a clarification. |
||
} | ||
} | ||
|
||
test2(Never n) { | ||
late int i; | ||
try { | ||
print("To avoid empty body"); | ||
} catch (_) { | ||
n; | ||
(i,) = (42,); | ||
} finally { | ||
i; | ||
} | ||
} | ||
|
||
test3<T extends Never>(T n) { | ||
late int i; | ||
try { | ||
print("To avoid empty body"); | ||
} catch (_) { | ||
n; | ||
(x: i) = (x: 42); | ||
} finally { | ||
i; | ||
} | ||
} | ||
|
||
test4() { | ||
late int i; | ||
try { | ||
print("To avoid empty body"); | ||
} catch (_) { | ||
foo(); | ||
C(v: i) = C(42); | ||
} finally { | ||
i; | ||
} | ||
} | ||
|
||
main() { | ||
print(test1); | ||
print(test2); | ||
print(test3); | ||
print(test4); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// 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 `before(B2) = split(join(drop(after(B1)), | ||
/// conservativeJoin(before(N), assignedIn(B1), capturedIn(B1))))`. Test that if | ||
/// some variable is assigned in a `catch` part of `B1` then it is | ||
/// "possibly assigned" in `B2`. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the try/catch/finally tests would need to be blocked for a little while as we're clarifying how they are treated. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This would apply for all the following test libraries as well. |
||
/// @author [email protected] | ||
|
||
class C { | ||
int v; | ||
C(this.v); | ||
} | ||
|
||
test1() { | ||
late int i; | ||
try { | ||
print("To avoid empty body"); | ||
} catch (_) { | ||
if (false) { | ||
i = 42; | ||
} | ||
} finally { | ||
i; // Possibly assigned | ||
} | ||
} | ||
|
||
test2() { | ||
late int i; | ||
try { | ||
print("To avoid empty body"); | ||
} catch (_) { | ||
if (false) { | ||
(i, ) = (42, ); | ||
} | ||
} finally { | ||
i; | ||
} | ||
} | ||
|
||
test3() { | ||
late int i; | ||
try { | ||
print("To avoid empty body"); | ||
} catch (_) { | ||
if (false) { | ||
(x: i) = (x: 42); | ||
} | ||
} finally { | ||
i; | ||
} | ||
} | ||
|
||
test4() { | ||
late int i; | ||
try { | ||
print("To avoid empty body"); | ||
} catch (_) { | ||
if (false) { | ||
C(v: i) = C(42); | ||
} | ||
} finally { | ||
i; | ||
} | ||
} | ||
|
||
main() { | ||
print(test1); | ||
print(test2); | ||
print(test3); | ||
print(test4); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// 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 `before(B2) = split(join(drop(after(B1)), | ||
/// conservativeJoin(before(N), assignedIn(B1), capturedIn(B1))))`. Test that if | ||
/// some variable is assigned in a catch part of `B1` then it is | ||
/// "possibly assigned" in `B2`. | ||
/// @author [email protected] | ||
|
||
class C { | ||
int v; | ||
C(this.v); | ||
} | ||
|
||
test1() { | ||
int i; | ||
try { | ||
print("To avoid empty body"); | ||
} catch (_) { | ||
i = 42; | ||
} finally { | ||
i; // Possibly assigned | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
} | ||
} | ||
|
||
test2() { | ||
int i; | ||
try { | ||
print("To avoid empty body"); | ||
} catch (_) { | ||
(i,) = (42,); | ||
} finally { | ||
i; | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
} | ||
} | ||
|
||
test3() { | ||
int i; | ||
try { | ||
print("To avoid empty body"); | ||
} catch (_) { | ||
(x: i) = (x: 42); | ||
} finally { | ||
i; | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
} | ||
} | ||
|
||
test4() { | ||
int i; | ||
try { | ||
print("To avoid empty body"); | ||
} catch (_) { | ||
C(v: i) = C(42); | ||
} finally { | ||
i; | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
} | ||
} | ||
|
||
main() { | ||
print(test1); | ||
print(test2); | ||
print(test3); | ||
print(test4); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// 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 `before(B2) = split(join(drop(after(B1)), | ||
/// conservativeJoin(before(N), assignedIn(B1), capturedIn(B1))))`. Test that if | ||
/// some promoted variable is captured in a `catch` part of `B1` then it is | ||
/// demoted in `B2`. | ||
/// @author [email protected] | ||
|
||
class C { | ||
int v; | ||
C(this.v); | ||
} | ||
|
||
test1(int? n) { | ||
if (n != null) { // `n` promoted to `int` | ||
try { | ||
print("To avoid empty body"); | ||
} catch (_) { | ||
() {n = 42;}; // `n` demoted to `int?` | ||
} finally { | ||
n.isEven; | ||
// ^^^^^^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
} | ||
} | ||
} | ||
|
||
test2(int? n) { | ||
if (n != null) { | ||
try { | ||
print("To avoid empty body"); | ||
} catch (_) { | ||
() {(n,) = (42,);}; | ||
} finally { | ||
n.isEven; | ||
// ^^^^^^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
} | ||
} | ||
} | ||
|
||
test3(int? n) { | ||
if (n != null) { | ||
try { | ||
print("To avoid empty body"); | ||
} catch (_) { | ||
() {(x: n) = (x: 42);}; | ||
} finally { | ||
n.isEven; | ||
// ^^^^^^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
} | ||
} | ||
} | ||
|
||
test4(int? n) { | ||
if (n != null) { | ||
try { | ||
print("To avoid empty body"); | ||
} catch (_) { | ||
() {C(v: n) = C(42);}; | ||
} finally { | ||
n.isEven; | ||
// ^^^^^^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
} | ||
} | ||
} | ||
|
||
main() { | ||
print(test1); | ||
print(test2); | ||
print(test3); | ||
print(test4); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same issue: This is
try
-catch
-finally
, which needs a separate rule that isn't written at this time.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I created dart-lang/language#4327 in order to clarify how to deal with try/catch/finally.