-
Notifications
You must be signed in to change notification settings - Fork 28
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. Enums #2422
Merged
Merged
Changes from all commits
Commits
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
64 changes: 64 additions & 0 deletions
64
LanguageFeatures/Extension-types/exhaustiveness_enum_A01_t01.dart
This file contains 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,64 @@ | ||
// 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 enum as a matched type are | ||
/// always exhaustive | ||
/// | ||
/// @description Check that it is no compile-time error if a matched type of a | ||
/// switch expression is an extension type with an enum as a representation type | ||
/// and the set of cases is exhaustive | ||
/// @author sgrekhov22@gmail.com | ||
|
||
// SharedOptions=--enable-experiment=inline-class | ||
|
||
import "../../Utils/expect.dart"; | ||
|
||
enum E<T> { | ||
a<int>(), | ||
b<String>(), | ||
c<double>(), | ||
} | ||
|
||
extension type ET1<T>(E<T> _) {} | ||
extension type ET2<T>(E<T> _) implements E<T> {} | ||
|
||
String testStatement1(ET1<num> e) { | ||
switch (e) { | ||
case E.a: | ||
case E.c: | ||
return "ok"; | ||
} | ||
} | ||
|
||
String testStatement2(ET2<num> e) { | ||
switch (e) { | ||
case E.a: | ||
case E.c: | ||
return "ok"; | ||
} | ||
} | ||
|
||
String testExpression1(ET1<num> e) => | ||
switch (e) { | ||
E.a => "a", | ||
E.c => "c" | ||
}; | ||
|
||
String testExpression2(ET2<num> e) => | ||
switch (e) { | ||
E.a => "a", | ||
E.c => "c" | ||
}; | ||
|
||
main() { | ||
Expect.equals("ok", testStatement1(ET1<num>(E.a))); | ||
Expect.equals("ok", testStatement1(ET1(E.c))); | ||
Expect.equals("a", testExpression1(ET1<num>(E.a))); | ||
Expect.equals("c", testExpression1(ET1(E.c))); | ||
|
||
Expect.equals("ok", testStatement2(ET2<num>(E.a))); | ||
Expect.equals("ok", testStatement2(ET2(E.c))); | ||
Expect.equals("a", testExpression2(ET2<num>(E.a))); | ||
Expect.equals("c", testExpression2(ET2(E.c))); | ||
} |
72 changes: 72 additions & 0 deletions
72
LanguageFeatures/Extension-types/exhaustiveness_enum_A01_t02.dart
This file contains 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,72 @@ | ||
// 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 enum as a matched type are | ||
/// always exhaustive | ||
/// | ||
/// @description Check that it is no compile-time error if a matched type of a | ||
/// switch expression is an extension type with an enum as a representation type | ||
/// and the set of cases is exhaustive | ||
/// @author sgrekhov22@gmail.com | ||
|
||
// SharedOptions=--enable-experiment=inline-class | ||
|
||
import "../../Utils/expect.dart"; | ||
|
||
enum E<T> { | ||
a<int>(), | ||
b<String>(), | ||
c<double>(), | ||
} | ||
|
||
extension type ET1<T>(E<T> _) {} | ||
extension type ET2<T>(E<T> _) implements E<T> {} | ||
|
||
String testStatement1<T extends num>(ET1<T> e) { | ||
switch (e) { | ||
case E.a: | ||
case E.c: | ||
return "ok"; | ||
} | ||
} | ||
|
||
String testStatement2<T extends num>(ET2<T> e) { | ||
switch (e) { | ||
case E.a: | ||
case E.c: | ||
return "ok"; | ||
} | ||
} | ||
|
||
String testExpression1<T extends num>(ET1<T> e) => | ||
switch (e) { | ||
E.a => "a", | ||
E.c => "c" | ||
}; | ||
|
||
String testExpression2<T extends num>(ET2<T> e) => | ||
switch (e) { | ||
E.a => "a", | ||
E.c => "c" | ||
}; | ||
|
||
main() { | ||
Expect.equals("ok", testStatement1(ET1<num>(E.a))); | ||
Expect.equals("ok", testStatement1(ET1(E.c))); | ||
Expect.equals("ok", testStatement1<int>(ET1<int>(E.a))); | ||
Expect.equals("ok", testStatement1<double>(ET1(E.c))); | ||
Expect.equals("a", testExpression1(ET1<num>(E.a))); | ||
Expect.equals("c", testExpression1(ET1(E.c))); | ||
Expect.equals("a", testExpression1<int>(ET1<int>(E.a))); | ||
Expect.equals("c", testExpression1<double>(ET1(E.c))); | ||
|
||
Expect.equals("ok", testStatement2(ET2<num>(E.a))); | ||
Expect.equals("ok", testStatement2(ET2(E.c))); | ||
Expect.equals("ok", testStatement2<int>(ET2<int>(E.a))); | ||
Expect.equals("ok", testStatement2<double>(ET2(E.c))); | ||
Expect.equals("a", testExpression2(ET2<num>(E.a))); | ||
Expect.equals("c", testExpression2(ET2(E.c))); | ||
Expect.equals("a", testExpression2<int>(ET2<int>(E.a))); | ||
Expect.equals("c", testExpression2<double>(ET2(E.c))); | ||
} |
68 changes: 68 additions & 0 deletions
68
LanguageFeatures/Extension-types/exhaustiveness_enum_A01_t03.dart
This file contains 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,68 @@ | ||
// 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 enum as a matched type are | ||
/// always exhaustive | ||
/// | ||
/// @description Check that it is no compile-time error if a matched type of a | ||
/// switch expression is an extension type with an enum as a representation type | ||
/// and the set of cases is exhaustive | ||
/// @author sgrekhov22@gmail.com | ||
|
||
// SharedOptions=--enable-experiment=inline-class | ||
|
||
import "../../Utils/expect.dart"; | ||
|
||
enum E<T> { | ||
a<int>(), | ||
b<String>(), | ||
c<double>(), | ||
} | ||
|
||
extension type ET1<T>(E<T> _) {} | ||
extension type ET2<T>(E<T> _) implements E<T> {} | ||
|
||
String testStatement1<T extends num>(ET1<T> e) { | ||
switch (e) { | ||
case E.a || E.c: | ||
return "ok"; | ||
} | ||
} | ||
|
||
String testStatement2<T extends num>(ET2<T> e) { | ||
switch (e) { | ||
case E.a || E.c: | ||
return "ok"; | ||
} | ||
} | ||
|
||
String testExpression1<T extends num>(ET1<T> e) => | ||
switch (e) { | ||
E.a || E.c => "ok" | ||
}; | ||
|
||
String testExpression2<T extends num>(ET2<T> e) => | ||
switch (e) { | ||
E.a || E.c => "ok" | ||
}; | ||
|
||
main() { | ||
Expect.equals("ok", testStatement1(ET1<num>(E.a))); | ||
Expect.equals("ok", testStatement1(ET1(E.c))); | ||
Expect.equals("ok", testStatement1<int>(ET1<int>(E.a))); | ||
Expect.equals("ok", testStatement1<double>(ET1(E.c))); | ||
Expect.equals("ok", testExpression1(ET1<num>(E.a))); | ||
Expect.equals("ok", testExpression1(ET1(E.c))); | ||
Expect.equals("ok", testExpression1<int>(ET1<int>(E.a))); | ||
Expect.equals("ok", testExpression1<double>(ET1(E.c))); | ||
|
||
Expect.equals("ok", testStatement2(ET2<num>(E.a))); | ||
Expect.equals("ok", testStatement2(ET2(E.c))); | ||
Expect.equals("ok", testStatement2<int>(ET2<int>(E.a))); | ||
Expect.equals("ok", testStatement2<double>(ET2(E.c))); | ||
Expect.equals("ok", testExpression2(ET2<num>(E.a))); | ||
Expect.equals("ok", testExpression2(ET2(E.c))); | ||
Expect.equals("ok", testExpression2<int>(ET2<int>(E.a))); | ||
Expect.equals("ok", testExpression2<double>(ET2(E.c))); | ||
} |
111 changes: 111 additions & 0 deletions
111
LanguageFeatures/Extension-types/exhaustiveness_enum_A02_t01.dart
This file contains 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,111 @@ | ||
// 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 enum as a matched type are | ||
/// always exhaustive | ||
/// | ||
/// @description Check that it is a compile-time error if a matched type of a | ||
/// switch expression is an extension type with an enum as a representation type | ||
/// and the set of cases is not exhaustive | ||
/// @author sgrekhov22@gmail.com | ||
|
||
// SharedOptions=--enable-experiment=inline-class | ||
|
||
enum E<T> { | ||
a<int>(), | ||
b<String>(), | ||
c<double>(), | ||
} | ||
|
||
extension type ET1<T>(E<T> _) {} | ||
extension type ET2<T>(E<T> _) implements E<T> {} | ||
|
||
String testStatement1<T extends num>(ET1<T> e) { | ||
switch (e) { | ||
//^^^^^^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
case E.a: | ||
return "a"; | ||
case E.b: | ||
return "b"; | ||
} | ||
} | ||
|
||
String testStatement2<T extends num>(ET2<T> e) { | ||
switch (e) { | ||
//^^^^^^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
case E.a: | ||
return "a"; | ||
case E.b: | ||
return "b"; | ||
} | ||
} | ||
|
||
String testExpression1<T extends num>(ET1<T> e) => | ||
switch (e) { | ||
//^^^^^^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
E.a => "a", | ||
E.b => "b" | ||
}; | ||
|
||
String testExpression2<T extends num>(ET2<T> e) => | ||
switch (e) { | ||
//^^^^^^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
E.a => "a", | ||
E.b => "b" | ||
}; | ||
|
||
String testStatement3(ET1<num> e) { | ||
switch (e) { | ||
//^^^^^^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
case E.a: | ||
return "ok"; | ||
} | ||
} | ||
|
||
String testStatement4(ET2<num> e) { | ||
switch (e) { | ||
//^^^^^^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
case E.a: | ||
return "ok"; | ||
} | ||
} | ||
|
||
String testExpression3(ET1<num> e) => | ||
switch (e) { | ||
//^^^^^^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
E.a => "a" | ||
}; | ||
|
||
String testExpression4(ET2<num> e) => | ||
switch (e) { | ||
//^^^^^^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
E.a => "a" | ||
}; | ||
|
||
main() { | ||
testStatement1(ET1(E.a)); | ||
testStatement2(ET2(E.a)); | ||
testExpression1(ET1(E.c)); | ||
testExpression2(ET2(E.c)); | ||
testStatement3(ET1(E.a)); | ||
testStatement4(ET2(E.a)); | ||
testExpression3(ET1(E.a)); | ||
testExpression4(ET2(E.a)); | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems to work, but it would be somewhat esoteric (and this has nothing to do with extension types, the situation exists after erasure).
The reason why this is a very special exception is that we (presumably) rely on the fact that this is a switch over enumerated values. This means that we can iterate over all the elements, and it is guaranteed that we won't encounter any other objects than the ones that are declared in the
enum
declaration itself, and they have a statically known run-time type.Lots and lots of stuff to know!
OK, I was worried that this would be on the very edge of the capabilities of the exhaustiveness analysis, but it seems to work fine.
In any case, we should probably also have a simpler test where the enumerated type isn't generic, such that it is easy to see that we have an extension type on top of a very straightforward setup (which will be a test that is only about being an extension type, so it's very safe to assume that it will test just that).