Skip to content

#3057. Add null-aware method invocation tests #3162

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
38 changes: 38 additions & 0 deletions TypeSystem/flow-analysis/reachability_A24_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// 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 Null-aware method invocation
/// For now (April, 2025) the flow-analysis feature specification does not
/// specify the analysis of Null-aware method invocation.
///
/// @description Checks that flow analysis doesn't promote to `Null`.
/// @author [email protected]

// SharedOptions=--enable-experiment=sound-flow-analysis

class C {
void foo(int x) {}
}

test1(C? c) {
late int i;
if (c == null) { // This doesn't promote `c` to `Null`
c?.foo(i = 42);
}
i; // Possibly assigned
}

test2(C? c) {
late int i;
if (c == null) {
c?..foo(i = 0)
..foo(0);
}
i;
}

main() {
print(test1);
print(test2);
}
45 changes: 45 additions & 0 deletions TypeSystem/flow-analysis/reachability_A24_t02.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 Null-aware method invocation
/// For now (April, 2025) the flow-analysis feature specification does not
/// specify the analysis of Null-aware method invocation.
///
/// @description Checks that is come variable is promoted to non-nullable then
/// null-aware method invocation is treated as a regular invocation.
/// @author [email protected]

// SharedOptions=--enable-experiment=sound-flow-analysis

class C {
void foo(int x) {}
}

test1(C? c) {
int i;
if (c != null) {
c?.foo(i = 42); // ignore: invalid_null_aware_operator
} else {
i = 0;
}
i; // Definitely assigned
}

test2(C? c) {
int i;
if (c != null) {
c?..foo(0) // ignore: invalid_null_aware_operator
..foo(i = 42);
} else {
i = 1;
}
i; // Definitely assigned
}

main() {
test1(C());
test1(null);
test2(C());
test2(null);
}
53 changes: 53 additions & 0 deletions TypeSystem/flow-analysis/reachability_A24_t03.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright (c) 2020, 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 Null-aware method invocation
/// For now (April, 2025) the flow-analysis feature specification does not
/// specify the analysis of Null-aware method invocation.
///
/// @description Checks reachability after method call. Test return type `Never`
/// @author [email protected]

// SharedOptions=--enable-experiment=sound-flow-analysis

class C {
Never m1() => throw "Exception";
}

test1(C? c) {
try {
late int i;
if (c != null) {
c?.m1(); // ignore: invalid_null_aware_operator
i = 42; // This is dead code, which leaves `i` definitely unassigned.
}
i; // Definitely unassigned.
// ^
// [analyzer] unspecified
// [cfe] unspecified
} catch (_) {
}
}

test2(C? c) {
try {
late int i;
if (c != null) {
c?..m1(); // ignore: invalid_null_aware_operator
i = 42;
}
i;
// ^
// [analyzer] unspecified
// [cfe] unspecified
} catch (_) {
}
}

main() {
test1(C());
test1(null);
test2(C());
test2(null);
}
47 changes: 47 additions & 0 deletions TypeSystem/flow-analysis/reachability_A24_t04.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright (c) 2020, 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 Null-aware method invocation
/// For now (April, 2025) the flow-analysis feature specification does not
/// specify the analysis of Null-aware method invocation.
///
/// @description Checks reachability after method call. Test return type `Never`
/// @author [email protected]

// SharedOptions=--enable-experiment=sound-flow-analysis

class C {
Never m1() => throw "Exception";
}

test1(C? c) {
try {
late int i;
if (c == null) { // This doesn't promote `c` to `Null`
c?.m1();
i = 42;
}
i; // Possibly assigned
} catch (_) {
}
}

test2(C? c) {
try {
late int i;
if (c == null) {
c?..m1();
i = 42;
}
i;
} catch (_) {
}
}

main() {
test1(C());
test1(null);
test2(C());
test2(null);
}
46 changes: 46 additions & 0 deletions TypeSystem/flow-analysis/reachability_A24_t05.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// 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 Null-aware method invocation
/// For now (April, 2025) the flow-analysis feature specification does not
/// specify the analysis of Null-aware method invocation.
///
/// @description Checks that code after type `Never` is unreachable.
/// @author [email protected]
/// @issue 54820

// SharedOptions=--enable-experiment=sound-flow-analysis

class C {
Never? get foo => throw "Exception";
}

test1() {
C c = C();
late int i;
if (1 > 2) {
c.foo?.bar(i = 42); // ignore: receiver_of_type_never, dead_code
}
i; // Definitely unassigned
//^
// [analyzer] unspecified
// [cfe] unspecified
}

test2() {
C c = C();
late int i;
if (1 > 2) {
c.foo?..bar(i = 0); // ignore: receiver_of_type_never, dead_code
}
i;
//^
// [analyzer] unspecified
// [cfe] unspecified
}

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