Skip to content

Commit

Permalink
Fixes #2010. Fix exhaustiveness tests for a cast-pattern (#2018)
Browse files Browse the repository at this point in the history
  • Loading branch information
sgrekhov authored Apr 11, 2023
1 parent 79b9b74 commit c2d7658
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@
/// space union of E.
///
/// @description Check a lifted space of a cast pattern in case of not sealed
/// type
/// type. Test switch statement
/// @author [email protected]
// SharedOptions=--enable-experiment=patterns

int test1(Object obj) {
// ^^^^^
// [analyzer] unspecified
// [cfe] unspecified
import "../../../Utils/expect.dart";

int test(Object obj) {
switch (obj) {
case int(isEven: true) as int:
return 1;
Expand All @@ -29,15 +28,7 @@ int test1(Object obj) {
}
}

int test2(Object obj) => switch (obj) {
// ^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
int(isEven: true) as int => 1,
int _ => 2
};

main() {
test1(1);
test2(2);
Expect.equals(2 ,test(1));
Expect.equals(1 ,test(2));
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,50 +9,23 @@
/// - The lifted space union of the cast's subpattern in context C.
/// - For each space E in the expanded spaces of M:
/// a. If E is not a subset of C and C is not a subset of M, then the lifted
/// space union of E.
/// space union of E.
///
/// @description Check a lifted space of a cast pattern in case of a sealed type
/// @description Check a lifted space of a cast pattern in case of not sealed
/// type. Test switch element
/// @author [email protected]
/// @issue 51877
/// @issue 51986
// SharedOptions=--enable-experiment=patterns,class-modifiers
// SharedOptions=--enable-experiment=patterns

import "../../../Utils/expect.dart";

sealed class A {
final int field;
A(this.field);
}
class B extends A {
B(int field) : super(field);
}
class C extends A {
C(int field) : super(field);
}

test1(A a) {
switch (a) {
case C(field: 0) as C:
return 0;
case C _:
return 1;
}
}

test2(A a) => switch (a) {
C(field: 0) as C => 0,
C _ => 1
};
int test(Object obj) => switch (obj) {
int(isEven: true) as int => 1,
int _ => 2
};

main() {
Expect.equals(0, test1(C(0)));
Expect.equals(1, test1(C(1)));
Expect.throws(() {
test1(B(0));
});
Expect.equals(0, test2(C(0)));
Expect.equals(1, test2(C(1)));
Expect.throws(() {
test2(B(0));
});
Expect.equals(2 ,test(1));
Expect.equals(1 ,test(2));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright (c) 2023, 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 The lifted space union for a pattern with matched value type M is
/// ...
/// Cast pattern:
/// The space union spaces for a cast pattern with cast type C is a union of:
/// - The lifted space union of the cast's subpattern in context C.
/// - For each space E in the expanded spaces of M:
/// a. If E is not a subset of C and C is not a subset of M, then the lifted
/// space union of E.
///
/// @description Check a lifted space of a cast pattern in case of a sealed type
/// @author [email protected]
/// @issue 51877
// SharedOptions=--enable-experiment=patterns,class-modifiers

import "../../../Utils/expect.dart";

sealed class A {
final int field;
A(this.field);
}
class B extends A {
B(int field) : super(field);
}
class C extends A {
C(int field) : super(field);
}

test1(A a) {
switch (a) {
case C(field: 0) as C:
return 0;
case C _:
return 1;
}
}

test2(A a) => switch (a) {
C(field: 0) as C => 0,
C _ => 1
};

main() {
Expect.equals(0, test1(C(0)));
Expect.equals(1, test1(C(1)));
Expect.throws(() {
test1(B(0));
});
Expect.equals(0, test2(C(0)));
Expect.equals(1, test2(C(1)));
Expect.throws(() {
test2(B(0));
});
}

0 comments on commit c2d7658

Please sign in to comment.