Skip to content

#3057. Add more promotion tests #3148

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
49 changes: 49 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,49 @@
// 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 `Si` and `after(N)`.
/// @author [email protected]
/// @issue 60519

// TODO (sgrekhov) https://github.com/dart-lang/language/issues/4328

class S {}

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

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

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

main() {
test1();
test2();
}
36 changes: 36 additions & 0 deletions TypeSystem/flow-analysis/reachability_try_catch_A04_t03.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// 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 `B` is empty and a type `T` is made a type of
/// interest in `alternatives` then it cannot be promoted in `after(N)`.
/// @author [email protected]

// TODO (sgrekhov) https://github.com/dart-lang/language/issues/4328

class S {}

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

main() {
S s = S();
try {
} on Exception catch (_) {
if (s is T) {} // Make `T` a type of interest
}
s = T();
s.answer(); // ???
// ^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
}
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 catch finally: If `N` is a try/catch/finally statement of the
/// form `try B1 alternatives finally F` then:
/// - Let `before(B1) = before(N)`
/// - Let `before(Si) = join(after(B1), conservativeJoin(before(N),
/// assignedIn(B1), capturedIn(B1)))`, where `Si`
/// is the body of the i'th alternative
/// - Let
/// `after(N) = join(attachFinally(after(B1), before(F), after(F)), M1 .. Mk)`
/// where `Mj = attachFinally(after(Sj), before(F), after(F))`
///
/// @description Checks that if a type `T` is made a type of interest in `B1`
/// then it cannot be promoted in `Si`, `F` and `after(N)`.
/// @author [email protected]
/// @issue 60519

// TODO (sgrekhov) https://github.com/dart-lang/language/issues/4328

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) {}
} catch (_) {
s = T();
s.answer();
// ^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
} finally {
}
}

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

test3() {
S s = S();
try {
if (s is T) {}
} catch (_) {
} finally {
}
C(v: s) = C(T());
s.answer(); // ??? Error or not? https://github.com/dart-lang/language/issues/4328
}

main() {
print(test1);
print(test2);
print(test3);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// 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 finally: If `N` is a try/catch/finally statement of the
/// form `try B1 alternatives finally F` then:
/// - Let `before(B1) = before(N)`
/// - Let `before(Si) = join(after(B1), conservativeJoin(before(N),
/// assignedIn(B1), capturedIn(B1)))`, where `Si`
/// is the body of the i'th alternative
/// - Let
/// `after(N) = join(attachFinally(after(B1), before(F), after(F)), M1 .. Mk)`
/// where `Mj = attachFinally(after(Sj), before(F), after(F))`
///
/// @description Checks that if a type `T` is made a type of interest in `Si`
/// then it cannot be promoted in `Sj`, where `i <> j` and `after(N)`.
/// @author [email protected]
/// @issue 60519

// TODO (sgrekhov) https://github.com/dart-lang/language/issues/4328

class S {}

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

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

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

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

test3() {
S s = S();
try {
} catch (_) {
if (s is T) {}
} finally {
}
C(v: s) = C(T());
s.answer(); // ? https://github.com/dart-lang/language/issues/4328
}

main() {
print(test1);
print(test2);
print(test3);
}
47 changes: 47 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,47 @@
// 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 `B2` and `after(N)`.
/// @author [email protected]

// TODO (sgrekhov) https://github.com/dart-lang/language/issues/4328

class S {}

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

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

test2() {
S s = S();
try {
if (s is T) {}
} finally {
}
s = T();
s.answer();
}

main() {
test1();
test2();
}