Skip to content

Commit 9585c28

Browse files
authored
#3057. Add more reachability tests for && and || operators (#3104)
Add more reachability tests for && and || operators
1 parent 762a771 commit 9585c28

File tree

4 files changed

+108
-0
lines changed

4 files changed

+108
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
/// @assertion - Shortcut and: If `N` is a shortcut "and" expression of the form
6+
/// `E1 && E2`, then:
7+
/// - Let `before(E1) = before(N)`.
8+
/// - Let `before(E2) = split(true(E1))`.
9+
/// - Let `true(N) = unsplit(true(E2))`.
10+
/// - Let `false(N) = merge(split(false(E1)), false(E2))`.
11+
///
12+
/// @description Checks that in an expression of the form `E1 && E2`, if `E1` is
13+
/// `false` and the static type of `E2` is `Never`, then the code after
14+
/// `E1 && E2` is not considered dead code.
15+
/// @author [email protected]
16+
17+
Never foo() => throw "Never";
18+
19+
main() {
20+
late int i;
21+
if (2 > 1) {
22+
false && foo();
23+
i = 42;
24+
}
25+
i; // Not definitely unassigned
26+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
/// @assertion - Shortcut and: If `N` is a shortcut "and" expression of the form
6+
/// `E1 && E2`, then:
7+
/// - Let `before(E1) = before(N)`.
8+
/// - Let `before(E2) = split(true(E1))`.
9+
/// - Let `true(N) = unsplit(true(E2))`.
10+
/// - Let `false(N) = merge(split(false(E1)), false(E2))`.
11+
///
12+
/// @description Checks that in an expression of the form `E1 && E2` if `E1` is
13+
/// `true` then `E2` is always executed.
14+
/// @author [email protected]
15+
16+
Never foo() => throw "Never";
17+
18+
main() {
19+
late int i;
20+
if (1 > 2) {
21+
true && foo();
22+
i = 42;
23+
}
24+
i; // Definitely unassigned
25+
//^
26+
// [analyzer] unspecified
27+
// [cfe] unspecified
28+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
/// @assertion - Shortcut or: If `N` is a shortcut "or" expression of the form
6+
/// `E1 || E2`, then:
7+
/// - Let `before(E1) = before(N)`.
8+
/// - Let `before(E2) = split(false(E1))`.
9+
/// - Let `false(N) = unsplit(false(E2))`.
10+
/// - Let `true(N) = merge(split(true(E1)), true(E2))`.
11+
///
12+
/// @description Checks that in an expression of the form `E1 || E2`, if `E1` is
13+
/// `true` and the static type of `E2` is `Never`, then the code after
14+
/// `E1 || E2` is not considered dead code.
15+
/// @author [email protected]
16+
17+
Never foo() => throw "Never";
18+
19+
main() {
20+
late int i;
21+
if (2 > 1) {
22+
true || foo();
23+
i = 42;
24+
}
25+
i; // Not definitely unassigned
26+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
/// @assertion - Shortcut or: If `N` is a shortcut "or" expression of the form
6+
/// `E1 || E2`, then:
7+
/// - Let `before(E1) = before(N)`.
8+
/// - Let `before(E2) = split(false(E1))`.
9+
/// - Let `false(N) = unsplit(false(E2))`.
10+
/// - Let `true(N) = merge(split(true(E1)), true(E2))`.
11+
///
12+
/// @description Checks that in an expression of the form `E1 || E2` if `E1` is
13+
/// `false` then `E2` is always executed.
14+
/// @author [email protected]
15+
16+
Never foo() => throw "Never";
17+
18+
main() {
19+
late int i;
20+
if (1 > 2) {
21+
false || foo();
22+
i = 42;
23+
}
24+
i; // Definitely unassigned
25+
//^
26+
// [analyzer] unspecified
27+
// [cfe] unspecified
28+
}

0 commit comments

Comments
 (0)