-
Notifications
You must be signed in to change notification settings - Fork 28
#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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
LanguageFeatures/Extension-types/exhaustiveness_list_A01_t01.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
86
LanguageFeatures/Extension-types/exhaustiveness_list_A01_t02.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
86
LanguageFeatures/Extension-types/exhaustiveness_list_A01_t03.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
48
LanguageFeatures/Extension-types/exhaustiveness_list_A02_t01.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
eernstg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
String test2(ET2<bool> l) { | ||
// ^^^^^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
switch (l) { | ||
case []: | ||
case [_]: | ||
case [_, _]: | ||
case [_, _, ...]: | ||
return "ok"; | ||
} | ||
} | ||
|
||
main() { | ||
test1(ET1([])); | ||
test2(ET2([])); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.