Skip to content

#3057. Add more promotion tests for extension types #3184

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

Merged
merged 2 commits into from
May 15, 2025
Merged
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
66 changes: 66 additions & 0 deletions TypeSystem/flow-analysis/reachability_A08_t09.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// 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 operator== If `N` is an expression of the form `E1 == E2`, where
/// the static type of `E1` is `T1` and the static type of `E2` is `T2`, then:
/// - Let `before(E1) = before(N)`.
/// - Let `before(E2) = after(E1)`.
/// - If `equivalentToNull(T1)` and `equivalentToNull(T2)`, then:
/// - Let `true(N) = after(E2)`.
/// - Let `false(N) = unreachable(after(E2))`.
/// - Otherwise, if `equivalentToNull(T1)` and `T2` is non-nullable, or
/// `equivalentToNull(T2)` and `T1` is non-nullable, then:
/// - Let `after(N) = after(E2)`.
/// - Otherwise, if `stripParens(E1)` is a `null` literal, then:
/// - Let `true(N) = after(E2)`.
/// - Let `false(N) = promoteToNonNull(E2, after(E2))`.
/// - Otherwise, if `stripParens(E2)` is a `null` literal, then:
/// - Let `true(N) = after(E1)`.
/// - Let `false(N) = promoteToNonNull(E1, after(E2))`.
/// - Otherwise:
/// - Let after(N) = after(E2).
/// Note that it is tempting to generalize the two `null` literal cases to apply
/// to any expression whose type is `Null`, but this would be unsound in cases
/// where `E2` assigns to `x`. (Consider, for example,
/// `(int? x) => x == (x = null) ? true : x.isEven`, which tries to call
/// `null.isEven` in the event of a non-null input).
///
/// @description Checks that if an extension type which doesn't implement
/// `Object` is compared with the `null` literal, then a variable assigned in
/// any true or false branch is possibly assigned.
/// @author [email protected]
/// @issue 60114

extension type ET(num v) {}

test1() {
late int i;
late int j;
ET et = ET(0);
if (et == null) {
i = 42;
} else {
j = 42;
}
i; // Possibly assigned
j; // Possibly assigned
}

test2() {
late int i;
late int j;
ET et = ET(0);
if (null == et) {
i = 42;
} else {
j = 42;
}
i;
j;
}

main() {
print(test1);
print(test2);
}
78 changes: 78 additions & 0 deletions TypeSystem/flow-analysis/reachability_A08_t10.dart
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 operator== If `N` is an expression of the form `E1 == E2`, where
/// the static type of `E1` is `T1` and the static type of `E2` is `T2`, then:
/// - Let `before(E1) = before(N)`.
/// - Let `before(E2) = after(E1)`.
/// - If `equivalentToNull(T1)` and `equivalentToNull(T2)`, then:
/// - Let `true(N) = after(E2)`.
/// - Let `false(N) = unreachable(after(E2))`.
/// - Otherwise, if `equivalentToNull(T1)` and `T2` is non-nullable, or
/// `equivalentToNull(T2)` and `T1` is non-nullable, then:
/// - Let `after(N) = after(E2)`.
/// - Otherwise, if `stripParens(E1)` is a `null` literal, then:
/// - Let `true(N) = after(E2)`.
/// - Let `false(N) = promoteToNonNull(E2, after(E2))`.
/// - Otherwise, if `stripParens(E2)` is a `null` literal, then:
/// - Let `true(N) = after(E1)`.
/// - Let `false(N) = promoteToNonNull(E1, after(E2))`.
/// - Otherwise:
/// - Let after(N) = after(E2).
/// Note that it is tempting to generalize the two `null` literal cases to apply
/// to any expression whose type is `Null`, but this would be unsound in cases
/// where `E2` assigns to `x`. (Consider, for example,
/// `(int? x) => x == (x = null) ? true : x.isEven`, which tries to call
/// `null.isEven` in the event of a non-null input).
///
/// @description Checks that if an extension type which doesn't implement
/// `Object` is compared with the `null` literal, then a variable assigned in
/// any true or false branch is not definitely assigned.
/// @author [email protected]
/// @issue 60114

extension type ET(num v) {}

test1() {
int i;
int j;
ET et = ET(0);
if (et == null) {
i = 42;
} else {
j = 42;
}
i; // Not definitely assigned
//^
// [analyzer] unspecified
// [cfe] unspecified
j; // Not definitely assigned
//^
// [analyzer] unspecified
// [cfe] unspecified
}

test2() {
int i;
int j;
ET et = ET(0);
if (null == et) {
i = 42;
} else {
j = 42;
}
i;
//^
// [analyzer] unspecified
// [cfe] unspecified
j;
//^
// [analyzer] unspecified
// [cfe] unspecified
}

main() {
print(test1);
print(test2);
}
74 changes: 74 additions & 0 deletions TypeSystem/flow-analysis/reachability_A08_t11.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// 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 operator== If `N` is an expression of the form `E1 == E2`, where
/// the static type of `E1` is `T1` and the static type of `E2` is `T2`, then:
/// - Let `before(E1) = before(N)`.
/// - Let `before(E2) = after(E1)`.
/// - If `equivalentToNull(T1)` and `equivalentToNull(T2)`, then:
/// - Let `true(N) = after(E2)`.
/// - Let `false(N) = unreachable(after(E2))`.
/// - Otherwise, if `equivalentToNull(T1)` and `T2` is non-nullable, or
/// `equivalentToNull(T2)` and `T1` is non-nullable, then:
/// - Let `after(N) = after(E2)`.
/// - Otherwise, if `stripParens(E1)` is a `null` literal, then:
/// - Let `true(N) = after(E2)`.
/// - Let `false(N) = promoteToNonNull(E2, after(E2))`.
/// - Otherwise, if `stripParens(E2)` is a `null` literal, then:
/// - Let `true(N) = after(E1)`.
/// - Let `false(N) = promoteToNonNull(E1, after(E2))`.
/// - Otherwise:
/// - Let after(N) = after(E2).
/// Note that it is tempting to generalize the two `null` literal cases to apply
/// to any expression whose type is `Null`, but this would be unsound in cases
/// where `E2` assigns to `x`. (Consider, for example,
/// `(int? x) => x == (x = null) ? true : x.isEven`, which tries to call
/// `null.isEven` in the event of a non-null input).
///
/// @description Checks that if a non-nullable extension type which implements
/// `Object` is compared with the `null` literal, then a variable assigned in
/// true or false branch is definitely (un)assigned.
/// @author [email protected]
/// @issue 60114

extension type ET(num v) implements Object {}

test1() {
int j;
late int i;
ET et = ET(0);

if (et == null) { // ignore: unnecessary_null_comparison
i = 42;
} else {
j = 42;
}
j; // Definitely assigned
i; // Definitely unassigned
//^
// [analyzer] unspecified
// [cfe] unspecified
}

test2() {
int j;
late int i;
ET et = ET(0);

if (null == et) { // ignore: unnecessary_null_comparison
i = 42;
} else {
j = 42;
}
j;
i;
//^
// [analyzer] unspecified
// [cfe] unspecified
}

main() {
print(test1);
print(test2);
}
72 changes: 72 additions & 0 deletions TypeSystem/flow-analysis/reachability_A08_t12.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// 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 operator== If `N` is an expression of the form `E1 == E2`, where
/// the static type of `E1` is `T1` and the static type of `E2` is `T2`, then:
/// - Let `before(E1) = before(N)`.
/// - Let `before(E2) = after(E1)`.
/// - If `equivalentToNull(T1)` and `equivalentToNull(T2)`, then:
/// - Let `true(N) = after(E2)`.
/// - Let `false(N) = unreachable(after(E2))`.
/// - Otherwise, if `equivalentToNull(T1)` and `T2` is non-nullable, or
/// `equivalentToNull(T2)` and `T1` is non-nullable, then:
/// - Let `after(N) = after(E2)`.
/// - Otherwise, if `stripParens(E1)` is a `null` literal, then:
/// - Let `true(N) = after(E2)`.
/// - Let `false(N) = promoteToNonNull(E2, after(E2))`.
/// - Otherwise, if `stripParens(E2)` is a `null` literal, then:
/// - Let `true(N) = after(E1)`.
/// - Let `false(N) = promoteToNonNull(E1, after(E2))`.
/// - Otherwise:
/// - Let after(N) = after(E2).
/// Note that it is tempting to generalize the two `null` literal cases to apply
/// to any expression whose type is `Null`, but this would be unsound in cases
/// where `E2` assigns to `x`. (Consider, for example,
/// `(int? x) => x == (x = null) ? true : x.isEven`, which tries to call
/// `null.isEven` in the event of a non-null input).
///
/// @description Checks that if a nullable extension type which implements
/// `Object` is compared with the `null` literal, then a variable assigned in
/// true or false branch is possibly assigned.
/// @author [email protected]
/// @issue 60114

extension type ET(num v) implements Object {}

test1() {
late int j;
late int i;
ET? et = 2 > 1 ? null : ET(0);

if (et == null) {
i = 42;
} else {
j = 42;
}
j; // Possibly assigned
i; // Possibly unassigned
}

test2() {
late int j;
late int i;
ET? et = 2 > 1 ? ET(0) : null;

if (null == et) {
i = 42;
} else {
j = 42;
}
j;
i;
}

main() {
try {
test1();
} catch (_) {}
try {
test2();
} catch (_) {}
}
45 changes: 45 additions & 0 deletions TypeSystem/flow-analysis/reachability_A09_t09.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// 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 operator!= If `N` is an expression of the form `E1 != E2`, it is
/// treated as equivalent to the expression `!(E1 == E2)`.
///
/// @description Checks that if an extension type which doesn't implement
/// `Object` is compared with the `null` literal, then a variable assigned in
/// any true or false branch is possibly assigned.
/// @author [email protected]
/// @issue 60114

extension type ET(num v) {}

test1() {
late int i;
late int j;
ET et = ET(0);
if (et != null) {
i = 42;
} else {
j = 42;
}
i; // Possibly assigned
j; // Possibly assigned
}

test2() {
late int i;
late int j;
ET et = ET(0);
if (null != et) {
i = 42;
} else {
j = 42;
}
i;
j;
}

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