Skip to content

Commit 3123d72

Browse files
authored
#3057. Add reachability tests for operators. Part 5. (#3105)
Add reachability tests for operators. Part 5.
1 parent 9585c28 commit 3123d72

24 files changed

+630
-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 - Binary operator: All binary operators other than `==`, `&&`,
6+
/// `||`, and `??` are handled as calls to the appropriate `operator` method.
7+
///
8+
/// @description Checks that for an expression of the form `E1 >>= E2`,
9+
/// `before(E2) = after(E1)`. Test that if `after(E1)` is unreachable then
10+
/// `before(E2)` is also unreachable.
11+
/// @author [email protected]
12+
13+
void test<T extends Never>(T n) {
14+
late int i;
15+
if (2 > 1) {
16+
n >>= (i = 42);
17+
}
18+
i; // Definitely unassigned
19+
//^
20+
// [analyzer] unspecified
21+
// [cfe] unspecified
22+
}
23+
24+
main() {
25+
print(test);
26+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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 - Binary operator: All binary operators other than `==`, `&&`,
6+
/// `||`, and `??` are handled as calls to the appropriate `operator` method.
7+
///
8+
/// @description Checks that if the static type of the expression of the form
9+
/// `E1 >>= E2` is `Never` then `after(N) = unreachable(after(E2))`.
10+
/// @author [email protected]
11+
12+
class C {
13+
Never operator >>(int other) => throw "";
14+
}
15+
16+
void test() {
17+
late int i;
18+
if (2 > 1) {
19+
C c = C();
20+
c >>= 1;
21+
i = 42;
22+
}
23+
i; // Definitely unassigned
24+
//^
25+
// [analyzer] unspecified
26+
// [cfe] unspecified
27+
}
28+
29+
main() {
30+
print(test);
31+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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 - Binary operator: All binary operators other than `==`, `&&`,
6+
/// `||`, and `??` are handled as calls to the appropriate `operator` method.
7+
///
8+
/// @description Checks that if the static type of the expression of the form
9+
/// `E1 >>= E2` is `Never` then `E2` is still reachable.
10+
/// @author [email protected]
11+
12+
class C {
13+
Never operator >>(int x) => throw "C";
14+
}
15+
16+
main() {
17+
late int i;
18+
try {
19+
C c = C();
20+
c >>= (i = 42);
21+
} catch (_) {}
22+
i; // Not definitely unassigned
23+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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 - Binary operator: All binary operators other than `==`, `&&`,
6+
/// `||`, and `??` are handled as calls to the appropriate `operator` method.
7+
///
8+
/// @description Checks that for an expression of the form `E1 >>= E2` if the
9+
/// static type of `E1`is not `Never` then `after(N) = after(E2)`. This is
10+
/// tested by detecting that `i = 42` is considered to be guaranteed to have
11+
/// been executed when `i;` is executed.
12+
/// @author [email protected]
13+
14+
class C {
15+
num n;
16+
C(this.n);
17+
C operator >>(int other) => C(n - other);
18+
}
19+
20+
main() {
21+
int i;
22+
C c = C(1);
23+
c >>= (i = 42);
24+
i; // Definitely assigned
25+
}
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 - Binary operator: All binary operators other than `==`, `&&`,
6+
/// `||`, and `??` are handled as calls to the appropriate `operator` method.
7+
///
8+
/// @description Checks that for an expression of the form `E1 >>>= E2`,
9+
/// `before(E2) = after(E1)`. Test that if `after(E1)` is unreachable then
10+
/// `before(E2)` is also unreachable.
11+
/// @author [email protected]
12+
13+
void test<T extends Never>(T n) {
14+
late int i;
15+
if (2 > 1) {
16+
n >>>= (i = 42);
17+
}
18+
i; // Definitely unassigned
19+
//^
20+
// [analyzer] unspecified
21+
// [cfe] unspecified
22+
}
23+
24+
main() {
25+
print(test);
26+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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 - Binary operator: All binary operators other than `==`, `&&`,
6+
/// `||`, and `??` are handled as calls to the appropriate `operator` method.
7+
///
8+
/// @description Checks that if the static type of the expression of the form
9+
/// `E1 >>>= E2` is `Never` then `after(N) = unreachable(after(E2))`.
10+
/// @author [email protected]
11+
12+
class C {
13+
Never operator >>>(int other) => throw "";
14+
}
15+
16+
void test() {
17+
late int i;
18+
if (2 > 1) {
19+
C c = C();
20+
c >>>= 1;
21+
i = 42;
22+
}
23+
i; // Definitely unassigned
24+
//^
25+
// [analyzer] unspecified
26+
// [cfe] unspecified
27+
}
28+
29+
main() {
30+
print(test);
31+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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 - Binary operator: All binary operators other than `==`, `&&`,
6+
/// `||`, and `??` are handled as calls to the appropriate `operator` method.
7+
///
8+
/// @description Checks that if the static type of the expression of the form
9+
/// `E1 >>>= E2` is `Never` then `E2` is still reachable.
10+
/// @author [email protected]
11+
12+
class C {
13+
Never operator >>>(int x) => throw "C";
14+
}
15+
16+
main() {
17+
late int i;
18+
try {
19+
C c = C();
20+
c >>>= (i = 42);
21+
} catch (_) {}
22+
i; // Not definitely unassigned
23+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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 - Binary operator: All binary operators other than `==`, `&&`,
6+
/// `||`, and `??` are handled as calls to the appropriate `operator` method.
7+
///
8+
/// @description Checks that for an expression of the form `E1 >>>= E2` if the
9+
/// static type of `E1`is not `Never` then `after(N) = after(E2)`. This is
10+
/// tested by detecting that `i = 42` is considered to be guaranteed to have
11+
/// been executed when `i;` is executed.
12+
/// @author [email protected]
13+
14+
class C {
15+
num n;
16+
C(this.n);
17+
C operator >>>(int other) => C(n - other);
18+
}
19+
20+
main() {
21+
int i;
22+
C c = C(1);
23+
c >>>= (i = 42);
24+
i; // Definitely assigned
25+
}
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 - Binary operator: All binary operators other than `==`, `&&`,
6+
/// `||`, and `??` are handled as calls to the appropriate `operator` method.
7+
///
8+
/// @description Checks that for an expression of the form `E1 <<= E2`,
9+
/// `before(E2) = after(E1)`. Test that if `after(E1)` is unreachable then
10+
/// `before(E2)` is also unreachable.
11+
/// @author [email protected]
12+
13+
void test<T extends Never>(T n) {
14+
late int i;
15+
if (2 > 1) {
16+
n <<= (i = 42);
17+
}
18+
i; // Definitely unassigned
19+
//^
20+
// [analyzer] unspecified
21+
// [cfe] unspecified
22+
}
23+
24+
main() {
25+
print(test);
26+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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 - Binary operator: All binary operators other than `==`, `&&`,
6+
/// `||`, and `??` are handled as calls to the appropriate `operator` method.
7+
///
8+
/// @description Checks that if the static type of the expression of the form
9+
/// `E1 <<= E2` is `Never` then `after(N) = unreachable(after(E2))`.
10+
/// @author [email protected]
11+
12+
class C {
13+
Never operator <<(int other) => throw "";
14+
}
15+
16+
void test() {
17+
late int i;
18+
if (2 > 1) {
19+
C c = C();
20+
c <<= 1;
21+
i = 42;
22+
}
23+
i; // Definitely unassigned
24+
//^
25+
// [analyzer] unspecified
26+
// [cfe] unspecified
27+
}
28+
29+
main() {
30+
print(test);
31+
}

0 commit comments

Comments
 (0)