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, trivial cases #2423

Merged
merged 2 commits into from
Dec 12, 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
71 changes: 71 additions & 0 deletions LanguageFeatures/Extension-types/exhaustiveness_enum_A01_t04.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// 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 [email protected]

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

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

enum E {
a, b, c
}

extension type ET1(E _) {}
extension type ET2(E _) implements E {}

String testStatement1(ET1 e) {
switch (e) {
case E.a:
case E.b:
case E.c:
return "ok";
}
}

String testStatement2(ET2 e) {
switch (e) {
case E.a:
case E.b:
return "ok1";
case E.c:
return "ok2";
}
}

String testExpression1(ET1 e) =>
switch (e) {
E.a => "a",
E.b => "b",
E.c => "c"
};

String testExpression2(ET2<num> e) =>
switch (e) {
E.a => "a",
E.b => "b",
E.c => "c"
};

main() {
Expect.equals("ok", testStatement1(ET1(E.a)));
Expect.equals("ok", testStatement1(ET1(E.b)));
Expect.equals("ok", testStatement1(ET1(E.c)));
Expect.equals("a", testExpression1(ET1(E.a)));
Expect.equals("a", testExpression1(ET1(E.b)));
Expect.equals("c", testExpression1(ET1(E.c)));

Expect.equals("ok1", testStatement2(ET2(E.a)));
Expect.equals("ok1", testStatement2(ET2(E.b)));
Expect.equals("ok2", testStatement2(ET2(E.c)));
Expect.equals("a", testExpression2(ET2(E.a)));
Expect.equals("b", testExpression2(ET2(E.b)));
Expect.equals("c", testExpression2(ET2(E.c)));
}
56 changes: 56 additions & 0 deletions LanguageFeatures/Extension-types/exhaustiveness_enum_A01_t05.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// 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 [email protected]

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

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

enum E {
a, b, c
}

extension type ET1(E _) {}
extension type ET2(E _) implements E {}

String testStatement1(ET1 e) {
switch (e) {
case E.a || E.b || E.c:
return "ok";
}
}

String testStatement2(ET2 e) {
switch (e) {
case E.a || E.b || E.c:
return "ok";
}
}

String testExpression1(ET1 e) =>
switch (e) {
E.a || E.b || E.c => "ok"
};

String testExpression2(ET2 e) =>
switch (e) {
E.a || E.b || E.c => "ok"
};

main() {
Expect.equals("ok", testStatement1(ET1(E.a)));
Expect.equals("ok", testStatement1(ET1(E.b)));
Expect.equals("ok", testStatement1(ET1(E.c)));

Expect.equals("ok", testStatement2(ET2(E.a)));
Expect.equals("ok", testStatement2(ET2(E.b)));
Expect.equals("ok", testStatement2(ET2(E.c)));
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ String testExpression1<T extends num>(ET1<T> e) =>

String testExpression2<T extends num>(ET2<T> e) =>
switch (e) {
//^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
//^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
E.a => "a",
E.b => "b"
};
Expand Down
69 changes: 69 additions & 0 deletions LanguageFeatures/Extension-types/exhaustiveness_enum_A02_t02.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// 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 [email protected]

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

enum E {
a, b, c
}

extension type ET1(E _) {}
extension type ET2(E _) implements E {}

String testStatement1(ET1 e) {
switch (e) {
//^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
case E.a:
return "a";
case E.b:
return "b";
}
}

String testStatement2(ET2 e) {
switch (e) {
//^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
case E.a:
return "a";
case E.b:
return "b";
}
}

String testExpression1(ET1 e) =>
switch (e) {
//^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
E.a => "a",
E.b => "b"
};

String testExpression2(ET2 e) =>
switch (e) {
//^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
E.a => "a",
E.b => "b"
};

main() {
testStatement1(ET1(E.a));
testStatement2(ET2(E.a));
testExpression1(ET1(E.c));
testExpression2(ET2(E.c));
}