diff --git a/LanguageFeatures/Extension-types/exhaustiveness_enum_A01_t01.dart b/LanguageFeatures/Extension-types/exhaustiveness_enum_A01_t01.dart new file mode 100644 index 0000000000..f13c64ce9e --- /dev/null +++ b/LanguageFeatures/Extension-types/exhaustiveness_enum_A01_t01.dart @@ -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 { + a(), + b(), + c(), +} + +extension type ET1(E _) {} +extension type ET2(E _) implements E {} + +String testStatement1(ET1 e) { + switch (e) { + case E.a: + case E.c: + return "ok"; + } +} + +String testStatement2(ET2 e) { + switch (e) { + case E.a: + case E.c: + return "ok"; + } +} + +String testExpression1(ET1 e) => + switch (e) { + E.a => "a", + E.c => "c" + }; + +String testExpression2(ET2 e) => + switch (e) { + E.a => "a", + E.c => "c" + }; + +main() { + Expect.equals("ok", testStatement1(ET1(E.a))); + Expect.equals("ok", testStatement1(ET1(E.c))); + Expect.equals("a", testExpression1(ET1(E.a))); + Expect.equals("c", testExpression1(ET1(E.c))); + + Expect.equals("ok", testStatement2(ET2(E.a))); + Expect.equals("ok", testStatement2(ET2(E.c))); + Expect.equals("a", testExpression2(ET2(E.a))); + Expect.equals("c", testExpression2(ET2(E.c))); +} diff --git a/LanguageFeatures/Extension-types/exhaustiveness_enum_A01_t02.dart b/LanguageFeatures/Extension-types/exhaustiveness_enum_A01_t02.dart new file mode 100644 index 0000000000..21be2a1ce5 --- /dev/null +++ b/LanguageFeatures/Extension-types/exhaustiveness_enum_A01_t02.dart @@ -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 { + a(), + b(), + c(), +} + +extension type ET1(E _) {} +extension type ET2(E _) implements E {} + +String testStatement1(ET1 e) { + switch (e) { + case E.a: + case E.c: + return "ok"; + } +} + +String testStatement2(ET2 e) { + switch (e) { + case E.a: + case E.c: + return "ok"; + } +} + +String testExpression1(ET1 e) => + switch (e) { + E.a => "a", + E.c => "c" + }; + +String testExpression2(ET2 e) => + switch (e) { + E.a => "a", + E.c => "c" + }; + +main() { + Expect.equals("ok", testStatement1(ET1(E.a))); + Expect.equals("ok", testStatement1(ET1(E.c))); + Expect.equals("ok", testStatement1(ET1(E.a))); + Expect.equals("ok", testStatement1(ET1(E.c))); + Expect.equals("a", testExpression1(ET1(E.a))); + Expect.equals("c", testExpression1(ET1(E.c))); + Expect.equals("a", testExpression1(ET1(E.a))); + Expect.equals("c", testExpression1(ET1(E.c))); + + Expect.equals("ok", testStatement2(ET2(E.a))); + Expect.equals("ok", testStatement2(ET2(E.c))); + Expect.equals("ok", testStatement2(ET2(E.a))); + Expect.equals("ok", testStatement2(ET2(E.c))); + Expect.equals("a", testExpression2(ET2(E.a))); + Expect.equals("c", testExpression2(ET2(E.c))); + Expect.equals("a", testExpression2(ET2(E.a))); + Expect.equals("c", testExpression2(ET2(E.c))); +} diff --git a/LanguageFeatures/Extension-types/exhaustiveness_enum_A01_t03.dart b/LanguageFeatures/Extension-types/exhaustiveness_enum_A01_t03.dart new file mode 100644 index 0000000000..a336b4cee7 --- /dev/null +++ b/LanguageFeatures/Extension-types/exhaustiveness_enum_A01_t03.dart @@ -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 { + a(), + b(), + c(), +} + +extension type ET1(E _) {} +extension type ET2(E _) implements E {} + +String testStatement1(ET1 e) { + switch (e) { + case E.a || E.c: + return "ok"; + } +} + +String testStatement2(ET2 e) { + switch (e) { + case E.a || E.c: + return "ok"; + } +} + +String testExpression1(ET1 e) => + switch (e) { + E.a || E.c => "ok" + }; + +String testExpression2(ET2 e) => + switch (e) { + E.a || E.c => "ok" + }; + +main() { + Expect.equals("ok", testStatement1(ET1(E.a))); + Expect.equals("ok", testStatement1(ET1(E.c))); + Expect.equals("ok", testStatement1(ET1(E.a))); + Expect.equals("ok", testStatement1(ET1(E.c))); + Expect.equals("ok", testExpression1(ET1(E.a))); + Expect.equals("ok", testExpression1(ET1(E.c))); + Expect.equals("ok", testExpression1(ET1(E.a))); + Expect.equals("ok", testExpression1(ET1(E.c))); + + Expect.equals("ok", testStatement2(ET2(E.a))); + Expect.equals("ok", testStatement2(ET2(E.c))); + Expect.equals("ok", testStatement2(ET2(E.a))); + Expect.equals("ok", testStatement2(ET2(E.c))); + Expect.equals("ok", testExpression2(ET2(E.a))); + Expect.equals("ok", testExpression2(ET2(E.c))); + Expect.equals("ok", testExpression2(ET2(E.a))); + Expect.equals("ok", testExpression2(ET2(E.c))); +} diff --git a/LanguageFeatures/Extension-types/exhaustiveness_enum_A02_t01.dart b/LanguageFeatures/Extension-types/exhaustiveness_enum_A02_t01.dart new file mode 100644 index 0000000000..772feaad5e --- /dev/null +++ b/LanguageFeatures/Extension-types/exhaustiveness_enum_A02_t01.dart @@ -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 { + 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" + }; + +String testStatement3(ET1 e) { + switch (e) { +//^^^^^^ +// [analyzer] unspecified +// [cfe] unspecified + case E.a: + return "ok"; + } +} + +String testStatement4(ET2 e) { + switch (e) { +//^^^^^^ +// [analyzer] unspecified +// [cfe] unspecified + case E.a: + return "ok"; + } +} + +String testExpression3(ET1 e) => + switch (e) { +//^^^^^^ +// [analyzer] unspecified +// [cfe] unspecified + E.a => "a" + }; + +String testExpression4(ET2 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)); +}