Skip to content

Commit 762a771

Browse files
authored
#3057. Add more reachability tests for true and false literals (#3072)
* #3057. Add more reachability tests for `true` and `false` literals * Excessive requirements removed * Apply review suggestions.
1 parent 54b6fd1 commit 762a771

11 files changed

+194
-32
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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
6+
/// - Variable or getter: If `N` is an expression of the form `x` where the
7+
/// type of `x` is `T` then:
8+
/// If `T <: Never` then:
9+
/// - Let `after(N) = unreachable(before(N))`.
10+
/// Otherwise:
11+
/// - Let `after(N) = before(N)`.
12+
///
13+
/// @description Checks that the code is unreachable after a variable of type
14+
/// `Never`. Test a for-in statement in collection literals.
15+
/// @author [email protected]
16+
17+
void test1(Never n) {
18+
late int i;
19+
bool b = (() => true)();
20+
if (b) {
21+
[
22+
for (var x in n) // The code after this point is unreachable
23+
i = 42 // Variable is initialized in a dead code. This leaves it definitely unassigned
24+
];
25+
}
26+
i; // It is an error to read a local late variable when it is definitely unassigned.
27+
//^
28+
// [analyzer] unspecified
29+
// [cfe] unspecified
30+
}
31+
32+
void test2(Never n) {
33+
late int i;
34+
bool b = (() => true)();
35+
if (b) {
36+
<int>{
37+
for (var x in n) // The code after this point is unreachable
38+
i = 42 // Variable is initialized in a dead code. This leaves it definitely unassigned
39+
};
40+
}
41+
i; // It is an error to read a local late variable when it is definitely unassigned.
42+
//^
43+
// [analyzer] unspecified
44+
// [cfe] unspecified
45+
}
46+
47+
void test3(Never n) {
48+
late int i;
49+
bool b = (() => true)();
50+
if (b) {
51+
<int, int>{
52+
for (var x in n) // The code after this point is unreachable
53+
1: i = 42 // Variable is initialized in a dead code. This leaves it definitely unassigned
54+
};
55+
}
56+
i; // It is an error to read a local late variable when it is definitely unassigned.
57+
//^
58+
// [analyzer] unspecified
59+
// [cfe] unspecified
60+
}
61+
62+
main() {
63+
print(test1);
64+
print(test2);
65+
print(test3);
66+
}

TypeSystem/flow-analysis/reachability_A02_t01.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
/// @assertion True literal: If N is the literal true, then:
6-
/// Let true(N) = before(N).
7-
/// Let false(N) = unreachable(before(N)).
5+
/// @assertion True literal: If `N` is the literal `true`, then:
6+
/// Let `true(N) = before(N)`.
7+
/// Let `false(N) = unreachable(before(N))`.
88
///
9-
/// @description Checks reachability after true literal
9+
/// @description Checks reachability after `true` literal
1010
/// @author [email protected]
1111
1212
main() {

TypeSystem/flow-analysis/reachability_A02_t02.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
/// @assertion True literal: If N is the literal true, then:
6-
/// Let true(N) = before(N).
7-
/// Let false(N) = unreachable(before(N)).
5+
/// @assertion True literal: If `N` is the literal `true`, then:
6+
/// Let `true(N) = before(N)`.
7+
/// Let `false(N) = unreachable(before(N))`.
88
///
9-
/// @description Checks reachability after true literal
9+
/// @description Checks reachability after `true` literal
1010
/// @author [email protected]
1111
1212
main() {

TypeSystem/flow-analysis/reachability_A02_t03.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
/// @assertion True literal: If N is the literal true, then:
6-
/// Let true(N) = before(N).
7-
/// Let false(N) = unreachable(before(N)).
5+
/// @assertion True literal: If `N` is the literal `true`, then:
6+
/// Let `true(N) = before(N)`.
7+
/// Let `false(N) = unreachable(before(N))`.
88
///
9-
/// @description Checks reachability after true literal
9+
/// @description Checks reachability after `true` literal
1010
/// @author [email protected]
1111
1212
main() {

TypeSystem/flow-analysis/reachability_A02_t04.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
/// @assertion True literal: If N is the literal true, then:
6-
/// Let true(N) = before(N).
7-
/// Let false(N) = unreachable(before(N)).
5+
/// @assertion True literal: If `N` is the literal `true`, then:
6+
/// Let `true(N) = before(N)`.
7+
/// Let `false(N) = unreachable(before(N))`.
88
///
9-
/// @description Checks reachability after true literal
9+
/// @description Checks reachability after `true` literal
1010
/// @author [email protected]
1111
1212
main() {
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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 True literal: If `N` is the literal `true`, then:
6+
/// Let `true(N) = before(N)`.
7+
/// Let `false(N) = unreachable(before(N))`.
8+
///
9+
/// @description Checks reachability after `true` literal in collection literals
10+
/// @author [email protected]
11+
12+
void test1() {
13+
int i;
14+
[
15+
if (true)
16+
i = 42
17+
];
18+
i; // Definitely assigned, not an error
19+
}
20+
21+
void test2() {
22+
int i;
23+
<int>{
24+
if (true)
25+
i = 42
26+
};
27+
i; // Definitely assigned, not an error
28+
}
29+
30+
void test3() {
31+
int i;
32+
<int, int>{
33+
if (true)
34+
1: i = 42
35+
};
36+
i; // Definitely assigned, not an error
37+
}
38+
39+
main() {
40+
test1();
41+
test2();
42+
test3();
43+
}

TypeSystem/flow-analysis/reachability_A03_t01.dart

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
/// @assertion False literal: If N is the literal false, then:
6-
/// Let true(N) = unreachable(before(N)).
7-
/// Let false(N) = before(N)
8-
/// @description Checks reachability after false literal
5+
/// @assertion False literal: If `N` is the literal `false`, then:
6+
/// Let `true(N) = unreachable(before(N))`.
7+
/// Let `false(N) = before(N)`
8+
///
9+
/// @description Checks reachability after `false` literal
910
/// @author [email protected]
1011
1112
main() {

TypeSystem/flow-analysis/reachability_A03_t02.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
/// @assertion False literal: If N is the literal false, then:
6-
/// Let true(N) = unreachable(before(N)).
7-
/// Let false(N) = before(N)
5+
/// @assertion False literal: If `N` is the literal `false`, then:
6+
/// Let `true(N) = unreachable(before(N))`.
7+
/// Let `false(N) = before(N)`
88
///
9-
/// @description Checks reachability after false literal
9+
/// @description Checks reachability after `false` literal
1010
/// @author [email protected]
1111
1212
main() {

TypeSystem/flow-analysis/reachability_A03_t03.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
/// @assertion False literal: If N is the literal false, then:
6-
/// Let true(N) = unreachable(before(N)).
7-
/// Let false(N) = before(N)
5+
/// @assertion False literal: If `N` is the literal `false`, then:
6+
/// Let `true(N) = unreachable(before(N))`.
7+
/// Let `false(N) = before(N)`
88
///
9-
/// @description Checks reachability after false literal
9+
/// @description Checks reachability after `false` literal
1010
/// @author [email protected]
1111
1212
main() {

TypeSystem/flow-analysis/reachability_A03_t04.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
/// @assertion False literal: If N is the literal false, then:
6-
/// Let true(N) = unreachable(before(N)).
7-
/// Let false(N) = before(N)
5+
/// @assertion False literal: If `N` is the literal `false`, then:
6+
/// Let `true(N) = unreachable(before(N))`.
7+
/// Let `false(N) = before(N)`
88
///
9-
/// @description Checks reachability after false literal
9+
/// @description Checks reachability after `false` literal
1010
/// @author [email protected]
1111
1212
main() {

0 commit comments

Comments
 (0)