Skip to content
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
31 changes: 31 additions & 0 deletions TypeSystem/flow-analysis/reachability_A14_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// 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 - Local variable conditional assignment: If `N` is an expression
/// of the form `x ??= E1` where `x` is a local variable, then:
/// - Let `before(E1) = split(promote(x, Null, before(N)))`.
/// - Let `M1 = assign(x, E1, after(E1))`
/// - Let `M2 = split(promoteToNonNull(x, before(N)))`
/// - Let `after(N) = merge(M1, M2)`
///
/// @description Checks that flow analysis promotes a local variable after a
/// conditional assignment.
/// @author sgrekhov22@gmail.com

import '../../Utils/static_type_helper.dart';

class A {}
class C extends A {
void foo() {}
}

void test(A? o) {
o ??= C();
o.expectStaticType<Exactly<A>>();
}

main() {
test(A());
test(C());
}
27 changes: 27 additions & 0 deletions TypeSystem/flow-analysis/reachability_A14_t02.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// 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 - Local variable conditional assignment: If `N` is an expression
/// of the form `x ??= E1` where `x` is a local variable, then:
/// - Let `before(E1) = split(promote(x, Null, before(N)))`.
/// - Let `M1 = assign(x, E1, after(E1))`
/// - Let `M2 = split(promoteToNonNull(x, before(N)))`
/// - Let `after(N) = merge(M1, M2)`
///
/// @description Checks that if `x` is non-nullable then `E1` is dead code.
/// @author sgrekhov22@gmail.com
/// @issue 60114

void test(int o) {
late int i;
o ??= (i = 42); // `i` is initialized in dead code
i; // Definitely unassigned
//^
// [analyzer] unspecified
// [cfe] unspecified
}

main() {
print(test);
}
26 changes: 26 additions & 0 deletions TypeSystem/flow-analysis/reachability_A14_t03.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// 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 - Local variable conditional assignment: If `N` is an expression
/// of the form `x ??= E1` where `x` is a local variable, then:
/// - Let `before(E1) = split(promote(x, Null, before(N)))`.
/// - Let `M1 = assign(x, E1, after(E1))`
/// - Let `M2 = split(promoteToNonNull(x, before(N)))`
/// - Let `after(N) = merge(M1, M2)`
///
/// @description Checks that if `x` is nullable then `E1` is not dead code.
/// @author sgrekhov22@gmail.com

void test(int? o) {
late int i;
o ??= (i = 42);
try {
i;
} catch (_) {}
}

main() {
test(1);
test(null);
}
28 changes: 28 additions & 0 deletions TypeSystem/flow-analysis/reachability_A14_t04.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// 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 - Local variable conditional assignment: If `N` is an expression
/// of the form `x ??= E1` where `x` is a local variable, then:
/// - Let `before(E1) = split(promote(x, Null, before(N)))`.
/// - Let `M1 = assign(x, E1, after(E1))`
/// - Let `M2 = split(promoteToNonNull(x, before(N)))`
/// - Let `after(N) = merge(M1, M2)`
///
/// @description Checks that if `x` is a subtype of `Null` then `E1` is
/// always executed.
/// @author sgrekhov22@gmail.com

import '../../Utils/expect.dart';

void test<T extends Null>(T n) {
int i;
n ??= (i = 42) as dynamic;
i; // Definitely assigned
}

main() {
Expect.throws(() {
test(null);
});
}
43 changes: 43 additions & 0 deletions TypeSystem/flow-analysis/reachability_A15_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// 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 - Conditional assignment to a non local-variable: If `N` is an
/// expression of the form `E1 ??= E2` where `E1` is not a local variable, and
/// the type of `E1` is `T1`, then:
/// - Let `before(E1) = before(N)`.
/// - If `T1` is strictly non-nullable, then:
/// - Let `before(E2) = unreachable(after(E1))`.
/// - Let `after(N) = after(E1)`.
/// - Otherwise:
/// - Let before(E2) = split(after(E1)).
/// - Let after(N) = merge(after(E2), split(after(E1))).
///
/// @description Checks that if `E1` is not a local variable and is strictly
/// non-nullable, then `before(E2) = unreachable(after(E1))`.
/// @author sgrekhov22@gmail.com
/// @issue 60114

class C {
int m = 0;
test() {
late int i;
m ??= (i = 42);
i; // Definitely unassigned
// ^
// [analyzer] unspecified
// [cfe] unspecified
}
}

int n = 0;

main() {
late int i;
n ??= (i = 42);
i;
//^
// [analyzer] unspecified
// [cfe] unspecified
print(C);
}
42 changes: 42 additions & 0 deletions TypeSystem/flow-analysis/reachability_A15_t02.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// 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 - Conditional assignment to a non local-variable: If `N` is an
/// expression of the form `E1 ??= E2` where `E1` is not a local variable, and
/// the type of `E1` is `T1`, then:
/// - Let `before(E1) = before(N)`.
/// - If `T1` is strictly non-nullable, then:
/// - Let `before(E2) = unreachable(after(E1))`.
/// - Let `after(N) = after(E1)`.
/// - Otherwise:
/// - Let before(E2) = split(after(E1)).
/// - Let after(N) = merge(after(E2), split(after(E1))).
///
/// @description Checks that if `E1` is not a local variable and is nullable,
/// then `E2` is not dead code.
/// @author sgrekhov22@gmail.com

class C {
int? m;
C(this.m);

test() {
late int i;
m ??= (i = 42);
try {
i;
} catch (_) {}
}
}

int? n;

main() {
late int i;
n ??= (i = 42);
i;

C(0);
C(null);
}
41 changes: 41 additions & 0 deletions TypeSystem/flow-analysis/reachability_A15_t03.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// 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 - Conditional assignment to a non local-variable: If `N` is an
/// expression of the form `E1 ??= E2` where `E1` is not a local variable, and
/// the type of `E1` is `T1`, then:
/// - Let `before(E1) = before(N)`.
/// - If `T1` is strictly non-nullable, then:
/// - Let `before(E2) = unreachable(after(E1))`.
/// - Let `after(N) = after(E1)`.
/// - Otherwise:
/// - Let before(E2) = split(after(E1)).
/// - Let after(N) = merge(after(E2), split(after(E1))).
///
/// @description Checks that if `E1` is not a local variable and is nullable,
/// then flow analysis doesn't affect the static type of `E1`.
/// @author sgrekhov22@gmail.com

import '../../Utils/static_type_helper.dart';

class A {}
class B extends A {
void foo() {}
}

A? o;

class C {
A? m;
test() {
m ??= B();
m.expectStaticType<Exactly<A?>>();
}
}

main() {
o ??= B();
o.expectStaticType<Exactly<A?>>();
C().test();
}
37 changes: 37 additions & 0 deletions TypeSystem/flow-analysis/reachability_A15_t04.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// 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 - Conditional assignment to a non local-variable: If `N` is an
/// expression of the form `E1 ??= E2` where `E1` is not a local variable, and
/// the type of `E1` is `T1`, then:
/// - Let `before(E1) = before(N)`.
/// - If `T1` is strictly non-nullable, then:
/// - Let `before(E2) = unreachable(after(E1))`.
/// - Let `after(N) = after(E1)`.
/// - Otherwise:
/// - Let before(E2) = split(after(E1)).
/// - Let after(N) = merge(after(E2), split(after(E1))).
///
/// @description Checks that if `E1` is not a local variable and is a subtype
/// of `Null`, then `E2` is always executed.
/// @author sgrekhov22@gmail.com

import '../../Utils/expect.dart';

class C<T extends Null> {
T m;
C(this.m);

test() {
int i;
m ??= (i = 42) as dynamic;
i; // Definitely assigned
}
}

main() {
Expect.throws(() {
C(null).test();
});
}
42 changes: 42 additions & 0 deletions TypeSystem/flow-analysis/reachability_A16_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// 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 - Conditional expression: If `N` is a conditional expression of
/// the form `E1 ? E2 : E3`, then:
/// - Let `before(E1) = before(N)`.
/// - Let `before(E2) = split(true(E1))`.
/// - Let before(E3) = split(false(E1))`.
/// - Let `after(N) = merge(after(E2), after(E3))`.
/// - Let `true(N) = merge(true(E2), true(E3))`.
/// - Let `false(N) = merge(false(E2), false(E3))`.
///
/// @description Checks flow analysis after conditional expression.
/// @author sgrekhov22@gmail.com

void test1() {
late int i;
int j;
true ? j = 42 : i = 42;
j; // Ok, j is definitely assigned
i;
//^
// [analyzer] unspecified
// [cfe] unspecified
}

void test2() {
late int i;
int j;
false ? i = 42 : j = 42;
j; // Ok, j is definitely assigned
i;
//^
// [analyzer] unspecified
// [cfe] unspecified
}

main() {
print(test1);
print(test2);
}
39 changes: 39 additions & 0 deletions TypeSystem/flow-analysis/reachability_A17_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// 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 - If-null: If `N` is an if-null expression of the form
/// `E1 ?? E2`, where the type of E1 is T1, then:
/// - Let `before(E1) = before(N)`.
/// - If `T1` is strictly non-nullable, then:
/// - Let `before(E2) = unreachable(after(E1))`.
/// - Let `after(N) = after(E1)`.
/// - Otherwise:
/// - Let `before(E2) = split(after(E1))`.
/// - Let `after(N) = merge(after(E2), split(after(E1)))`.
///
/// @description Checks that if `E1` is non-nullable then `E2` is unreachable.
/// @author sgrekhov22@gmail.com
/// @issue 60114

void test(int x) {
late int i;
x ?? (i = 42); // `i` is initialized in dead code
i; // Definitely unassigned
//^
// [analyzer] unspecified
// [cfe] unspecified
}

int y = 0;

main() {
late int i;
y ?? (i = 42);
i;
//^
// [analyzer] unspecified
// [cfe] unspecified

print(test);
}
Loading