Skip to content

#2420. Add extension types exhaustiveness tests. Lists #2424

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

Merged
merged 2 commits into from
Dec 14, 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
86 changes: 86 additions & 0 deletions LanguageFeatures/Extension-types/exhaustiveness_list_A01_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// 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 A switch expression with `List` as the matched value type can be
/// exhaustive, which can also make a switch expression with an extension type
/// whose representation type is `List<...>` exhaustive.
///
/// @description Check that it is no compile-time error if a matched type of a
/// switch expression is an extension type with a `List` as a representation
/// type and all cases are exhaustive. Test a rest element at the end of the
/// list pattern
/// @author [email protected]

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

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

extension type ET1<T>(List<T> _) {}
extension type ET2<T>(List<T> _) implements List<T> {}

String test1_1(ET1<int> l) =>
switch (l) {
<int?>[] => "0",
[_] => "1",
[_, _] => "2",
[_, _, ...] => "2+"
};

String test1_2(ET2<int> l) =>
switch (l) {
<int?>[] => "0",
[_] => "1",
[_, _] => "2",
[_, _, ...] => "2+"
};

String test2_1(ET1<bool> l) =>
switch (l) {
[] => "0",
[true] => "1_1",
[false] => "1_2",
[_, true] => "2_1",
[_, false] => "2_2",
[_, true, ...var r1] => "3_1",
[_, false, ...final r2] => "3_2"
};

String test2_2(ET2<bool> l) =>
switch (l) {
[] => "0",
[true] => "1_1",
[false] => "1_2",
[_, true] => "2_1",
[_, false] => "2_2",
[_, true, ...var r1] => "3_1",
[_, false, ...final r2] => "3_2"
};

main() {
Expect.equals("0", test1_1(ET1([])));
Expect.equals("1", test1_1(ET1([1])));
Expect.equals("2", test1_1(ET1<int>([1, 2])));
Expect.equals("2+", test1_1(ET1([1, 2, 3])));

Expect.equals("0", test1_2(ET2([])));
Expect.equals("1", test1_2(ET2([1])));
Expect.equals("2", test1_2(ET2<int>([1, 2])));
Expect.equals("2+", test1_2(ET2([1, 2, 3])));

Expect.equals("0", test2_1(ET1([])));
Expect.equals("1_1", test2_1(ET1([true])));
Expect.equals("1_2", test2_1(ET1([false])));
Expect.equals("2_1", test2_1(ET1([true, true])));
Expect.equals("2_2", test2_1(ET1([true, false])));
Expect.equals("3_1", test2_1(ET1([true, true, false])));
Expect.equals("3_2", test2_1(ET1([true, false, false])));

Expect.equals("0", test2_2(ET2([])));
Expect.equals("1_1", test2_2(ET2([true])));
Expect.equals("1_2", test2_2(ET2([false])));
Expect.equals("2_1", test2_2(ET2([true, true])));
Expect.equals("2_2", test2_2(ET2([true, false])));
Expect.equals("3_1", test2_2(ET2([true, true, false])));
Expect.equals("3_2", test2_2(ET2([true, false, false])));
}
86 changes: 86 additions & 0 deletions LanguageFeatures/Extension-types/exhaustiveness_list_A01_t02.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// 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 A switch expression with `List` as the matched value type can be
/// exhaustive, which can also make a switch expression with an extension type
/// whose representation type is `List<...>` exhaustive.
///
/// @description Check that it is no compile-time error if a matched type of a
/// switch expression is an extension type with a `List` as a representation
/// type and all cases are exhaustive. Test a rest element at the middle of the
/// list pattern
/// @author [email protected]

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

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

extension type ET1<T>(List<T> _) {}
extension type ET2<T>(List<T> _) implements List<T> {}

String test1_1(ET1<int> l) =>
switch (l) {
[] => "0",
<int?>[_] => "1",
[_, _] => "2",
[_, ..., _] => "2+"
};

String test1_2(ET2<int> l) =>
switch (l) {
[] => "0",
<int?>[_] => "1",
[_, _] => "2",
[_, ..., _] => "2+"
};

String test2_1(ET1<bool> l) =>
switch (l) {
[] => "0",
[true] => "1_1",
[false] => "1_2",
[_, true] => "2_1",
[_, false] => "2_2",
[_, ... var r1, true] => "3_1",
[_, ... final r2, false] => "3_2"
};

String test2_2(ET2<bool> l) =>
switch (l) {
[] => "0",
[true] => "1_1",
[false] => "1_2",
[_, true] => "2_1",
[_, false] => "2_2",
[_, ... var r1, true] => "3_1",
[_, ... final r2, false] => "3_2"
};

main() {
Expect.equals("0", test1_1(ET1([])));
Expect.equals("1", test1_1(ET1([1])));
Expect.equals("2", test1_1(ET1<int>([1, 2])));
Expect.equals("2+", test1_1(ET1([1, 2, 3])));

Expect.equals("0", test1_2(ET2([])));
Expect.equals("1", test1_2(ET2([1])));
Expect.equals("2", test1_2(ET2<int>([1, 2])));
Expect.equals("2+", test1_2(ET2([1, 2, 3])));

Expect.equals("0", test2_1(ET1([])));
Expect.equals("1_1", test2_1(ET1([true])));
Expect.equals("1_2", test2_1(ET1([false])));
Expect.equals("2_1", test2_1(ET1([true, true])));
Expect.equals("2_2", test2_1(ET1([true, false])));
Expect.equals("3_1", test2_1(ET1([true, true, true])));
Expect.equals("3_2", test2_1(ET1([true, false, false])));

Expect.equals("0", test2_2(ET2([])));
Expect.equals("1_1", test2_2(ET2([true])));
Expect.equals("1_2", test2_2(ET2([false])));
Expect.equals("2_1", test2_2(ET2([true, true])));
Expect.equals("2_2", test2_2(ET2([true, false])));
Expect.equals("3_1", test2_2(ET2([true, true, true])));
Expect.equals("3_2", test2_2(ET2([true, false, false])));
}
86 changes: 86 additions & 0 deletions LanguageFeatures/Extension-types/exhaustiveness_list_A01_t03.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// 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 A switch expression with `List` as the matched value type can be
/// exhaustive, which can also make a switch expression with an extension type
/// whose representation type is `List<...>` exhaustive.
///
/// @description Check that it is no compile-time error if a matched type of a
/// switch expression is an extension type with a `List` as a representation
/// type and all cases are exhaustive. Test rest element at the beginning of
/// the list pattern
/// @author [email protected]

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

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

extension type ET1<T>(List<T> _) {}
extension type ET2<T>(List<T> _) implements List<T> {}

String test1_1(ET1<int> l) =>
switch (l) {
[] => "0",
[_] => "1",
[_, _] => "2",
<int?>[..., _, _] => "2+"
};

String test1_2(ET2<int> l) =>
switch (l) {
[] => "0",
[_] => "1",
[_, _] => "2",
<int?>[..., _, _] => "2+"
};

String test2_1(ET1<bool> l) =>
switch (l) {
[] => "0",
[true] => "1_1",
[false] => "1_2",
[_, true] => "2_1",
[_, false] => "2_2",
[... var r1, true] => "3_1",
[... final r2, false] => "3_2"
};

String test2_2(ET2<bool> l) =>
switch (l) {
[] => "0",
[true] => "1_1",
[false] => "1_2",
[_, true] => "2_1",
[_, false] => "2_2",
[... var r1, true] => "3_1",
[... final r2, false] => "3_2"
};

main() {
Expect.equals("0", test1_1(ET1([])));
Expect.equals("1", test1_1(ET1([1])));
Expect.equals("2", test1_1(ET1([1, 2])));
Expect.equals("2+", test1_1(ET1([1, 2, 3])));

Expect.equals("0", test1_2(ET2([])));
Expect.equals("1", test1_2(ET2([1])));
Expect.equals("2", test1_2(ET2([1, 2])));
Expect.equals("2+", test1_2(ET2([1, 2, 3])));

Expect.equals("0", test2_1(ET1([])));
Expect.equals("1_1", test2_1(ET1([true])));
Expect.equals("1_2", test2_1(ET1([false])));
Expect.equals("2_1", test2_1(ET1([true, true])));
Expect.equals("2_2", test2_1(ET1([true, false])));
Expect.equals("3_1", test2_1(ET1([true, true, true])));
Expect.equals("3_2", test2_1(ET1([true, false, false])));

Expect.equals("0", test2_2(ET2([])));
Expect.equals("1_1", test2_2(ET2([true])));
Expect.equals("1_2", test2_2(ET2([false])));
Expect.equals("2_1", test2_2(ET2([true, true])));
Expect.equals("2_2", test2_2(ET2([true, false])));
Expect.equals("3_1", test2_2(ET2([true, true, true])));
Expect.equals("3_2", test2_2(ET2([true, false, false])));
}
48 changes: 48 additions & 0 deletions LanguageFeatures/Extension-types/exhaustiveness_list_A02_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// 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 with a list as a matched type can never be
/// exhaustive
///
/// @description Check that exhaustiveness check is not performed for a switch
/// statement with a matched value type an extension type with the
/// representation type`List`.
/// @author [email protected]

extension type ET1<T>(List<T> _) {}
extension type ET2<T>(List<T> _) implements List<T> {}

String test1(ET1<bool> l) {
// ^^^^^
// [analyzer] unspecified
// [cfe] unspecified
switch (l) {
case []:
case [_]:
case [_, _]:
case [_, _, ...]:
return "ok";
}
// There is no return statement here, and static analysis doesn't know if the
// switch statement exhaustive or not, so an error above occurs because function
// return type cannot be null
}

String test2(ET2<bool> l) {
// ^^^^^
// [analyzer] unspecified
// [cfe] unspecified
switch (l) {
case []:
case [_]:
case [_, _]:
case [_, _, ...]:
return "ok";
}
}

main() {
test1(ET1([]));
test2(ET2([]));
}
Loading