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. Enums #2422

Merged
merged 1 commit into from
Dec 11, 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
64 changes: 64 additions & 0 deletions LanguageFeatures/Extension-types/exhaustiveness_enum_A01_t01.dart
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";
Copy link
Member

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).

}
}

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 LanguageFeatures/Extension-types/exhaustiveness_enum_A01_t02.dart
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 LanguageFeatures/Extension-types/exhaustiveness_enum_A01_t03.dart
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 LanguageFeatures/Extension-types/exhaustiveness_enum_A02_t01.dart
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));
}