Skip to content

Commit ed6da2f

Browse files
authoredDec 21, 2023
Fixes dart-lang#2446. Update cast-pattern exhaustiveness tests according to the changed spec (dart-lang#2448)
Update cast-pattern exhaustiveness tests according to the changed spec
1 parent a1a5931 commit ed6da2f

9 files changed

+280
-39
lines changed
 

‎LanguageFeatures/Patterns/Exhaustiveness/lifting_cast_pattern_A01_t01.dart

+7-5
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
/// @assertion The lifted space union for a pattern with matched value type M is
66
/// ...
77
/// Cast pattern:
8-
/// The space union spaces for a cast pattern with cast type C is a union of:
9-
/// - The lifted space union of the cast's subpattern in context C.
10-
/// - For each space E in the expanded spaces of M:
11-
/// a. If E is not a subset of C and C is not a subset of M, then the lifted
12-
/// space union of E.
8+
/// ...
9+
/// Let S be the lifted space union of the cast's subpattern in context C.
10+
/// i. If C is a subset (see below about type subsetting) of S then the result
11+
/// spaces is the lifted space union of M.
12+
/// ii. Otherwise, the result spaces is S plus the lifted space union of Null
13+
/// when C is a non-nullable type, and spaces is S when C is potentially
14+
/// nullable.
1315
///
1416
/// @description Check a lifted space of a cast pattern in case of not sealed
1517
/// type. Test switch statement

‎LanguageFeatures/Patterns/Exhaustiveness/lifting_cast_pattern_A01_t02.dart

+17-9
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,33 @@
55
/// @assertion The lifted space union for a pattern with matched value type M is
66
/// ...
77
/// Cast pattern:
8-
/// The space union spaces for a cast pattern with cast type C is a union of:
9-
/// - The lifted space union of the cast's subpattern in context C.
10-
/// - For each space E in the expanded spaces of M:
11-
/// a. If E is not a subset of C and C is not a subset of M, then the lifted
12-
/// space union of E.
8+
/// ...
9+
/// Let S be the lifted space union of the cast's subpattern in context C.
10+
/// i. If C is a subset (see below about type subsetting) of S then the result
11+
/// spaces is the lifted space union of M.
12+
/// ii. Otherwise, the result spaces is S plus the lifted space union of Null
13+
/// when C is a non-nullable type, and spaces is S when C is potentially
14+
/// nullable.
1315
///
1416
/// @description Check a lifted space of a cast pattern in case of not sealed
1517
/// type. Test switch element
1618
/// @author sgrekhov22@gmail.com
1719
/// @issue 51986
1820
19-
import "../../../Utils/expect.dart";
20-
21+
// The corresponding switch statement (can be found in
22+
// `lifting_cast_pattern_A01_t01.dart`) will not complete normally in this case
23+
// (which means that there is no "returns null" error), but this switch
24+
// expression is an error because it can not be recognized as exhaustive. This
25+
// discrepancy is expected. For more details see
26+
// https://github.com/dart-lang/sdk/issues/51986#issuecomment-1864237801
2127
int test(Object obj) => switch (obj) {
28+
// ^^^^^^
29+
// [analyzer] unspecified
30+
// [cfe] unspecified
2231
int(isEven: true) as int => 1,
2332
int _ => 2
2433
};
2534

2635
main() {
27-
Expect.equals(2 ,test(1));
28-
Expect.equals(1 ,test(2));
36+
print(test);
2937
}

‎LanguageFeatures/Patterns/Exhaustiveness/lifting_cast_pattern_A02_t01.dart

+19-20
Original file line numberDiff line numberDiff line change
@@ -5,52 +5,51 @@
55
/// @assertion The lifted space union for a pattern with matched value type M is
66
/// ...
77
/// Cast pattern:
8-
/// The space union spaces for a cast pattern with cast type C is a union of:
9-
/// - The lifted space union of the cast's subpattern in context C.
10-
/// - For each space E in the expanded spaces of M:
11-
/// a. If E is not a subset of C and C is not a subset of M, then the lifted
12-
/// space union of E.
8+
/// ...
9+
/// Let S be the lifted space union of the cast's subpattern in context C.
10+
/// i. If C is a subset (see below about type subsetting) of S then the result
11+
/// spaces is the lifted space union of M.
12+
/// ii. Otherwise, the result spaces is S plus the lifted space union of Null
13+
/// when C is a non-nullable type, and spaces is S when C is potentially
14+
/// nullable.
1315
///
1416
/// @description Check a lifted space of a cast pattern in case of a sealed type
1517
/// @author sgrekhov22@gmail.com
16-
/// @issue 51877
17-
18-
import "../../../Utils/expect.dart";
1918
2019
sealed class A {
2120
final int field;
2221
A(this.field);
2322
}
23+
2424
class B extends A {
2525
B(int field) : super(field);
2626
}
27+
2728
class C extends A {
2829
C(int field) : super(field);
2930
}
3031

31-
test1(A a) {
32+
int test1(A a) {
3233
switch (a) {
34+
//^^^^^^
35+
// [analyzer] unspecified
36+
// [cfe] unspecified
3337
case C(field: 0) as C:
3438
return 0;
3539
case C _:
3640
return 1;
3741
}
3842
}
3943

40-
test2(A a) => switch (a) {
44+
int test2(A a) => switch (a) {
45+
// ^^^^^^
46+
// [analyzer] unspecified
47+
// [cfe] unspecified
4148
C(field: 0) as C => 0,
4249
C _ => 1
4350
};
4451

4552
main() {
46-
Expect.equals(0, test1(C(0)));
47-
Expect.equals(1, test1(C(1)));
48-
Expect.throws(() {
49-
test1(B(0));
50-
});
51-
Expect.equals(0, test2(C(0)));
52-
Expect.equals(1, test2(C(1)));
53-
Expect.throws(() {
54-
test2(B(0));
55-
});
53+
test1(C(0));
54+
test2(C(0));
5655
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright (c) 2023, 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 The lifted space union for a pattern with matched value type M is
6+
/// ...
7+
/// Cast pattern:
8+
/// ...
9+
/// Let S be the lifted space union of the cast's subpattern in context C.
10+
/// i. If C is a subset (see below about type subsetting) of S then the result
11+
/// spaces is the lifted space union of M.
12+
/// ii. Otherwise, the result spaces is S plus the lifted space union of Null
13+
/// when C is a non-nullable type, and spaces is S when C is potentially
14+
/// nullable.
15+
///
16+
/// @description Check a lifted space of a cast pattern in case of a sealed type
17+
/// @author sgrekhov22@gmail.com
18+
19+
import "../../../Utils/expect.dart";
20+
21+
sealed class A {}
22+
23+
class B extends A {}
24+
25+
class C extends A {}
26+
27+
int test1(A a) {
28+
switch (a) {
29+
case B() as B:
30+
return 0;
31+
}
32+
}
33+
34+
int test2(A a) => switch (a) { C() as C => 0 };
35+
36+
main() {
37+
Expect.equals(0, test1(B()));
38+
Expect.throws(() {
39+
test1(C());
40+
});
41+
Expect.equals(0, test2(C()));
42+
Expect.throws(() {
43+
test2(B());
44+
});
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright (c) 2023, 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 The lifted space union for a pattern with matched value type M is
6+
/// ...
7+
/// Cast pattern:
8+
/// ...
9+
/// Let S be the lifted space union of the cast's subpattern in context C.
10+
/// i. If C is a subset (see below about type subsetting) of S then the result
11+
/// spaces is the lifted space union of M.
12+
/// ii. Otherwise, the result spaces is S plus the lifted space union of Null
13+
/// when C is a non-nullable type, and spaces is S when C is potentially
14+
/// nullable.
15+
///
16+
/// @description Check a lifted space of a cast pattern in case of a sealed type
17+
/// @author sgrekhov22@gmail.com
18+
19+
sealed class A {}
20+
21+
class B extends A {}
22+
23+
class C extends A {}
24+
25+
int test1(A a) {
26+
switch (a) {
27+
//^^^^^^
28+
// [analyzer] unspecified
29+
// [cfe] unspecified
30+
case B() as C:
31+
return 0;
32+
}
33+
}
34+
35+
int test2(A a) => switch (a) { B() as C => 0 };
36+
// ^^^^^^
37+
// [analyzer] unspecified
38+
// [cfe] unspecified
39+
40+
main() {
41+
print(test1);
42+
print(test2);
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright (c) 2023, 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 The lifted space union for a pattern with matched value type M is
6+
/// ...
7+
/// Cast pattern:
8+
/// ...
9+
/// Let S be the lifted space union of the cast's subpattern in context C.
10+
/// i. If C is a subset (see below about type subsetting) of S then the result
11+
/// spaces is the lifted space union of M.
12+
/// ii. Otherwise, the result spaces is S plus the lifted space union of Null
13+
/// when C is a non-nullable type, and spaces is S when C is potentially
14+
/// nullable.
15+
///
16+
/// @description Check a lifted space of a cast pattern in case of a not sealed
17+
/// type
18+
/// @author sgrekhov22@gmail.com
19+
20+
class A {
21+
final int field;
22+
A(this.field);
23+
}
24+
25+
class B extends A {
26+
B(int field) : super(field);
27+
}
28+
29+
class C extends A {
30+
C(int field) : super(field);
31+
}
32+
33+
int test1(A a) {
34+
switch (a) {
35+
case C(field: 0) as C:
36+
return 0;
37+
case C _:
38+
return 1;
39+
}
40+
}
41+
42+
// The corresponding switch statement above will not complete normally in this
43+
// case (which means that there is no "returns null" error), but this switch
44+
// expression is an error because it can not be recognized as exhaustive. This
45+
// discrepancy is expected. For more details see
46+
// https://github.com/dart-lang/sdk/issues/51986#issuecomment-1864237801
47+
int test2(A a) => switch (a) {
48+
// ^^^^^^
49+
// [analyzer] unspecified
50+
// [cfe] unspecified
51+
C(field: 0) as C => 0,
52+
C _ => 1
53+
};
54+
55+
main() {
56+
test1(C(0));
57+
test2(C(0));
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright (c) 2023, 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 The lifted space union for a pattern with matched value type M is
6+
/// ...
7+
/// Cast pattern:
8+
/// ...
9+
/// Let S be the lifted space union of the cast's subpattern in context C.
10+
/// i. If C is a subset (see below about type subsetting) of S then the result
11+
/// spaces is the lifted space union of M.
12+
/// ii. Otherwise, the result spaces is S plus the lifted space union of Null
13+
/// when C is a non-nullable type, and spaces is S when C is potentially
14+
/// nullable.
15+
///
16+
/// @description Check a lifted space of a cast pattern in case of a not sealed
17+
/// type
18+
/// @author sgrekhov22@gmail.com
19+
20+
import "../../../Utils/expect.dart";
21+
22+
class A {}
23+
24+
class B extends A {}
25+
26+
class C extends A {}
27+
28+
test1(A a) {
29+
switch (a) {
30+
case B() as B:
31+
return 0;
32+
}
33+
}
34+
35+
test2(A a) => switch (a) { C() as C => 0 };
36+
37+
main() {
38+
Expect.equals(0, test1(B()));
39+
Expect.throws(() {
40+
test1(C());
41+
});
42+
Expect.equals(0, test2(C()));
43+
Expect.throws(() {
44+
test2(B());
45+
});
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright (c) 2023, 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 The lifted space union for a pattern with matched value type M is
6+
/// ...
7+
/// Cast pattern:
8+
/// ...
9+
/// Let S be the lifted space union of the cast's subpattern in context C.
10+
/// i. If C is a subset (see below about type subsetting) of S then the result
11+
/// spaces is the lifted space union of M.
12+
/// ii. Otherwise, the result spaces is S plus the lifted space union of Null
13+
/// when C is a non-nullable type, and spaces is S when C is potentially
14+
/// nullable.
15+
///
16+
/// @description Check a lifted space of a cast pattern in case of a not sealed
17+
/// type
18+
/// @author sgrekhov22@gmail.com
19+
20+
class A {}
21+
22+
class B extends A {}
23+
24+
class C extends A {}
25+
26+
int test1(A a) {
27+
// ^^^^^^
28+
// [analyzer] unspecified
29+
// [cfe] unspecified
30+
switch (a) {
31+
case B() as C:
32+
return 0;
33+
}
34+
}
35+
36+
int test2(A a) => switch (a) { B() as C => 0 };
37+
// ^^^^^^
38+
// [analyzer] unspecified
39+
// [cfe] unspecified
40+
41+
main() {
42+
print(test1);
43+
print(test2);
44+
}

0 commit comments

Comments
 (0)
Please sign in to comment.