Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#2420. Add extension types exhaustiveness tests. Variables #2431

Merged
merged 2 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// 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 Switch statements and expressions with a sealed class as a
/// matched type are always exhaustive if the set of cases is exhaustive
///
/// @description Check that it is no compile-time error if the matched value
/// type of a switch expression is an extension type with a sealed class as a
/// representation type and the set of cases is an exhaustive set of variable
/// patterns
/// @author sgrekhov22@gmail.com

// SharedOptions=--enable-experiment=inline-class

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

sealed class A {}

class B extends A {}

class C extends B {}

extension type AET1(A _) {}
extension type AET2(A _) implements A {}
extension type BET1(B _) {}
extension type BET2(B _) implements B {}

test1(AET1 a) => switch (a) { B _ => 'B' };
test2(AET2 a) => switch (a) { B _ => 'B' };

main() {
Expect.equals("B", test1(AET1(B())));
Expect.equals("B", test1(AET1(C())));
Expect.equals("B", test2(AET2(B())));
Expect.equals("B", test2(AET2(C())));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// 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 Switch statements and expressions with a sealed class as a
/// matched type are always exhaustive if the set of cases is exhaustive
///
/// @description Check that it is no compile-time error if the matched value
/// type of a switch expression is a sealed class and the set of cases is an
/// exhaustive set of variable patterns with extension types
/// @author sgrekhov22@gmail.com

// SharedOptions=--enable-experiment=inline-class

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

sealed class A {}

class B extends A {}

class C extends A {}

extension type BET1(B _) {}
extension type BET2(B _) implements B {}
extension type CET1(C _) {}
extension type CET2(C _) implements A {} // Let's check implements A, not C

test1_1(A a) => switch (a) { CET1 _ => 'C', BET1 _ => 'B' };
test1_2(A a) => switch (a) { BET2 _ => 'B', CET2 _ => 'C' };

test2_1(A a) => switch (a) { BET1 _ => 'B', CET1 _ => 'C' };
test2_2(A a) => switch (a) { CET2 _ => 'C', BET2 _ => 'B' };

main() {
Expect.equals("B", test1_1(B()));
Expect.equals("C", test1_1(C()));
Expect.equals("B", test1_2(B()));
Expect.equals("C", test1_2(C()));

Expect.equals("B", test2_1(B()));
Expect.equals("C", test2_1(C()));
Expect.equals("B", test2_2(B()));
Expect.equals("C", test2_2(C()));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// 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 Switch statements and expressions with a sealed class as a
/// matched type are always exhaustive if the set of cases is exhaustive
///
/// @description Check that it is no compile-time error if the matched value
/// type of a switch expression is an extension type with a sealed class as a
/// representation type and the set of cases is an exhaustive set of variable
/// patterns with extension types and classes
/// @author sgrekhov22@gmail.com

// SharedOptions=--enable-experiment=inline-class

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

sealed class A {}

class B extends A {}

class C extends A {}

extension type AET1(A _) {}
extension type AET2(A _) implements A {}
extension type BET1(B _) {}
extension type BET2(B _) implements B {}
extension type CET1(C _) {}
extension type CET2(C _) implements C {}

test1_1(AET1 a) => switch (a) { BET1 _ => 'B', CET1 _ => 'C' };
test1_2(AET2 a) => switch (a) { BET2 _ => 'B', CET2 _ => 'C' };

test2_1(AET1 a) => switch (a) { BET1 _ => 'B', CET1 _ => 'C' };
test2_2(AET2 a) => switch (a) { CET2 _ => 'C', BET2 _ => 'B' };

test3_1(AET1 a) => switch (a) { BET1 _ => 'B', C _ => 'C' };
test3_2(AET2 a) => switch (a) { CET2 _ => 'C', B _ => 'B' };

main() {
Expect.equals("B", test1_1(AET1(B())));
Expect.equals("C", test1_1(AET1(C())));
Expect.equals("B", test1_2(AET2(B())));
Expect.equals("C", test1_2(AET2(C())));

Expect.equals("B", test2_1(AET1(B())));
Expect.equals("C", test2_1(AET1(C())));
Expect.equals("B", test2_2(AET2(B())));
Expect.equals("C", test2_2(AET2(C())));

Expect.equals("B", test3_1(AET1(B())));
Expect.equals("C", test3_1(AET1(C())));
Expect.equals("B", test3_2(AET2(B())));
Expect.equals("C", test3_2(AET2(C())));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// 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 Switch statements and expressions with a some class as a matched
/// type are always exhaustive if the set of cases is exhaustive
///
/// @description Check that it is no compile-time error if the matched value
/// type of a switch expression is an extension type with some class as a
/// representation type and the set of cases is an exhaustive set of variable
/// patterns
/// @author sgrekhov22@gmail.com

// SharedOptions=--enable-experiment=inline-class

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

class A {}

class B extends A {}

class C extends B {}

extension type AET1(A _) {}
extension type AET2(A _) implements A {}
extension type BET1(B _) {}
extension type BET2(B _) implements B {}

test1(BET1 b) => switch (b) { B _ => 'B' };
test2(BET2 b) => switch (b) { B _ => 'B' };

main() {
Expect.equals("B", test1(BET1(B())));
Expect.equals("B", test1(BET1(C())));
Expect.equals("B", test2(BET2(B())));
Expect.equals("B", test2(BET2(C())));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// 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 Switch statements and expressions with a sealed class as a
/// matched type are always exhaustive if the set of cases is exhaustive
///
/// @description Check that it is no compile-time error if the matched value
/// type of a switch expression is an extension type with a sealed class as a
/// representation type and the set of cases is an exhaustive set of variable
/// patterns. Test generic types
/// @author sgrekhov22@gmail.com

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

sealed class A<T> {}

class B<T> extends A<T> {}

class C extends A<int> {}

extension type AET1<T>(A<T> _) {}
extension type AET2<T>(A<T> _) implements A<T> {}
extension type BET1<T>(B<T> _) {}
extension type BET2<T>(B<T> _) implements B<T> {}

test1_1(AET1<String> a) => switch (a) { B _ => 'B'};
test1_2(AET2<String> a) => switch (a) { B _ => 'B'};

test2_1(AET1<int> a) => switch (a) { B _ => 'B', C _ => 'C'};
test2_2(AET2<int> a) => switch (a) { B _ => 'B', C _ => 'C'};

main() {
Expect.equals("B", test1_1(AET1(B<String>())));
Expect.equals("B", test1_2(AET2(B<String>())));
Expect.equals("B", test2_1(AET1(B<int>())));
Expect.equals("B", test2_2(AET2(B<int>())));
Expect.equals("C", test2_1(AET1(C())));
Expect.equals("C", test2_2(AET2(C())));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// 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 Switch statements and expressions with a sealed class as a
/// matched type are always exhaustive if the set of cases is exhaustive
///
/// @description Check that it is no compile-time error if the matched value
/// type of a switch expression is a sealed class and the set of cases is an
/// exhaustive set of variable patterns with extension types. Test generic types
/// @author sgrekhov22@gmail.com

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

sealed class A<T> {}

class B<T> extends A<T> {}

class C extends A<int> {}

extension type AET1<T>(A<T> _) {}
extension type AET2<T>(A<T> _) implements A<T> {}
extension type BET1<T>(B<T> _) {}
extension type BET2<T>(B<T> _) implements B<T> {}
extension type CET1(C _) {}
extension type CET2(C _) implements C {}

test1_1(A<String> a) => switch (a) { BET1 _ => 'B'};
test1_2(A<String> a) => switch (a) { BET2 _ => 'B'};

test2_1(A<int> a) => switch (a) { BET1 _ => 'B', CET1 _ => 'C'};
test2_2(A<int> a) => switch (a) { BET2 _ => 'B', CET2 _ => 'C'};

test3_1(A<int> a) => switch (a) { AET1 _ => 'A'};
test3_2(A<int> a) => switch (a) { AET2 _ => 'A'};

main() {
Expect.equals("B", test1_1(B<String>()));
Expect.equals("B", test1_2(B<String>()));
Expect.equals("B", test2_1(B<int>()));
Expect.equals("B", test2_2(B<int>()));
Expect.equals("C", test2_1(C()));
Expect.equals("C", test2_2(C()));
Expect.equals("A", test3_1(B()));
Expect.equals("A", test3_2(C()));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// 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 Switch statements and expressions with a sealed class as a
/// matched type are always exhaustive if the set of cases is exhaustive
///
/// @description Check that it is no compile-time error if the matched value
/// type of a switch expression is an extension type with a sealed class as a
/// representation type and the set of cases is an exhaustive set of variable
/// patterns with extension types and classes. Test generic types
/// @author sgrekhov22@gmail.com

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

sealed class A<T> {}

class B<T> extends A<T> {}

class C extends A<int> {}

extension type AET1<T>(A<T> _) {}
extension type AET2<T>(A<T> _) implements A<T> {}
extension type BET1<T>(B<T> _) {}
extension type BET2<T>(B<T> _) implements B<T> {}
extension type CET1(C _) {}
extension type CET2(C _) implements C {}

test1_1(AET1<String> a) => switch (a) { BET1 _ => 'B'};
test1_2(AET2<String> a) => switch (a) { BET2 _ => 'B'};

test2_1(AET1<int> a) => switch (a) { BET1 _ => 'B', C _ => 'C'};
test2_2(AET2<int> a) => switch (a) { B _ => 'B', CET2 _ => 'C'};

test3_1(AET1<int> a) => switch (a) { AET1 _ => 'A'};
test3_2(AET2<int> a) => switch (a) { AET2 _ => 'A'};

main() {
Expect.equals("B", test1_1(AET1(B<String>())));
Expect.equals("B", test1_2(AET2(B<String>())));
Expect.equals("B", test2_1(AET1(B<int>())));
Expect.equals("B", test2_2(AET2(B<int>())));
Expect.equals("C", test2_1(AET1(C())));
Expect.equals("C", test2_2(AET2(C())));
Expect.equals("A", test3_1(AET1(B())));
Expect.equals("A", test3_2(AET2(C())));
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class C extends A<int> {}

test1(A<String> a) => switch (a) { B _ => 'B'};

test2(A<String> a) => switch (a) { B _ => 'B', C _ => 'C'};
test2(A a) => switch (a) { B _ => 'B', C _ => 'C'};

main() {
Expect.equals("B", test1(B<String>()));
Expand Down