-
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"); | ||
|
||
} catch (_) { | ||
foo(); | ||
i = 42; | ||
} finally { | ||
i; // Possibly assigned | ||
|
||
} | ||
} | ||
|
||
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`. | ||
|
||
/// @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.