diff --git a/LanguageFeatures/Extension-types/exhaustiveness_object_A01_t01.dart b/LanguageFeatures/Extension-types/exhaustiveness_object_A01_t01.dart new file mode 100644 index 0000000000..31e7710dfa --- /dev/null +++ b/LanguageFeatures/Extension-types/exhaustiveness_object_A01_t01.dart @@ -0,0 +1,67 @@ +// 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 a sealed class as a +/// matched type are always exhaustive +/// +/// @description Check that it is no compile-time error if the matched value +/// type of a switch expression is an extension type with a sealed class as a +/// representation type and the set of cases is an exhaustive set of object +/// patterns +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=inline-class + +import "../../Utils/expect.dart"; +import "../Patterns/Exhaustiveness/exhaustiveness_lib.dart"; + +extension type FaceET1(Face _) {} +extension type FaceET2(Face _) implements Face {} + +String test1_1(FaceET1 face) => switch (face) { + Jack() => 'Jack', + Queen() => 'Queen', + King(suit: _) => 'King' + }; + +String test1_2(FaceET2 face) => switch (face) { + Jack() => 'Jack', + Queen() => 'Queen', + King(suit: _) => 'King' + }; + +String test2_1(FaceET1 face) => switch (face) { + Jack() => 'Jack', + Queen() => 'Queen', + King(suit: Suit.club || Suit.diamond || Suit.heart || Suit.spade) => + 'King' + }; + +String test2_2(FaceET2 face) => switch (face) { + Jack() => 'Jack', + Queen() => 'Queen', + King(suit: Suit.club || Suit.diamond || Suit.heart || Suit.spade) => + 'King' + }; + +String test3_1(FaceET1 face) => switch (face) { + Jack() && Jack(oneEyed: _) => 'Jack', + Queen() => 'Queen', + King() => 'King' + }; + +String test3_2(FaceET2 face) => switch (face) { + Jack() && Jack(oneEyed: _) => 'Jack', + Queen() => 'Queen', + King() => 'King' + }; + +main() { + Expect.equals("King", test1_1(FaceET1(King(Suit.club)))); + Expect.equals("King", test1_2(FaceET2(King(Suit.club)))); + Expect.equals("King", test2_1(FaceET1(King(Suit.club)))); + Expect.equals("King", test2_2(FaceET2(King(Suit.club)))); + Expect.equals("King", test3_1(FaceET1(King(Suit.club)))); + Expect.equals("King", test3_2(FaceET2(King(Suit.club)))); +} diff --git a/LanguageFeatures/Extension-types/exhaustiveness_object_A01_t02.dart b/LanguageFeatures/Extension-types/exhaustiveness_object_A01_t02.dart new file mode 100644 index 0000000000..f38a8b1968 --- /dev/null +++ b/LanguageFeatures/Extension-types/exhaustiveness_object_A01_t02.dart @@ -0,0 +1,78 @@ +// 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 a sealed class as a +/// matched type are always exhaustive +/// +/// @description Check that it is no compile-time error if the matched value +/// type of a switch statement is an extension type with a sealed class as a +/// representation type and the set of cases is exhaustive +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=inline-class + +import "../../Utils/expect.dart"; +import "../Patterns/Exhaustiveness/exhaustiveness_lib.dart"; + +extension type FaceET1(Face _) {} +extension type FaceET2(Face _) implements Face {} + +String test1_1(FaceET1 face) { + switch (face) { + case Jack(): return 'Jack'; + case Queen(): return 'Queen'; + case King(suit: _): return 'King'; + } +} + +String test1_2(FaceET2 face) { + switch (face) { + case Jack(): return 'Jack'; + case Queen(): return 'Queen'; + case King(suit: _): return 'King'; + } +} + +String test2_1(FaceET1 face) { + switch (face) { + case Jack(): return 'Jack'; + case Queen(): return 'Queen'; + case King(suit: Suit.club || Suit.diamond || Suit.heart || Suit.spade): + return 'King'; + } +} + +String test2_2(FaceET2 face) { + switch (face) { + case Jack(): return 'Jack'; + case Queen(): return 'Queen'; + case King(suit: Suit.club || Suit.diamond || Suit.heart || Suit.spade): + return 'King'; + } +} + +String test3_1(FaceET1 face) { + switch (face) { + case Jack() && Jack(oneEyed: _): return 'Jack'; + case Queen(): return 'Queen'; + case King(): return 'King'; + } +} + +String test3_2(FaceET2 face) { + switch (face) { + case Jack() && Jack(oneEyed: _): return 'Jack'; + case Queen(): return 'Queen'; + case King(): return 'King'; + } +} + +main() { + Expect.equals("King", test1_1(FaceET1(King(Suit.club)))); + Expect.equals("King", test1_2(FaceET2(King(Suit.club)))); + Expect.equals("King", test2_1(FaceET1(King(Suit.club)))); + Expect.equals("King", test2_2(FaceET2(King(Suit.club)))); + Expect.equals("King", test3_1(FaceET1(King(Suit.club)))); + Expect.equals("King", test3_2(FaceET2(King(Suit.club)))); +} diff --git a/LanguageFeatures/Extension-types/exhaustiveness_object_A01_t03.dart b/LanguageFeatures/Extension-types/exhaustiveness_object_A01_t03.dart new file mode 100644 index 0000000000..4a60a71dbf --- /dev/null +++ b/LanguageFeatures/Extension-types/exhaustiveness_object_A01_t03.dart @@ -0,0 +1,44 @@ +// 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 a sealed class as a +/// matched type are always exhaustive +/// +/// @description Check that it is no compile-time error if the matched value +/// type of a switch expression is a sealed class as and the set of cases is an +/// exhaustive set of object patterns with extension types +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=inline-class + +import "../../Utils/expect.dart"; +import "../Patterns/Exhaustiveness/exhaustiveness_lib.dart"; + +extension type JackET(Jack _) implements Jack {} +extension type QueenET(Queen _) implements Queen {} +extension type KingET(King _) implements King {} + +String test1(Face face) => switch (face) { + JackET() || QueenET() => 'Jack or Queen', + KingET(suit: _) => 'King' + }; + +String test2(Face face) => switch (face) { + JackET() || QueenET() => 'Jack or Queen', + KingET( + suit: Suit.club || Suit.diamond || Suit.heart || Suit.spade + ) => 'King' + }; + +String test3(Face face) => switch (face) { + JackET() && JackET(oneEyed: _) => 'Jack', + QueenET() => 'Queen', + KingET() => 'King' + }; + +main() { + Expect.equals("Jack or Queen", test1(Jack(Suit.club))); + Expect.equals("Jack or Queen", test2(Jack(Suit.club))); + Expect.equals("King", test3(King(Suit.club))); +} diff --git a/LanguageFeatures/Extension-types/exhaustiveness_object_A01_t04.dart b/LanguageFeatures/Extension-types/exhaustiveness_object_A01_t04.dart new file mode 100644 index 0000000000..343085652e --- /dev/null +++ b/LanguageFeatures/Extension-types/exhaustiveness_object_A01_t04.dart @@ -0,0 +1,54 @@ +// 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 a sealed class as a +/// matched type are always exhaustive +/// +/// @description Check that it is no compile-time error if the matched value +/// type of a switch statement is a sealed class as and the set of cases is an +/// exhaustive set of object patterns with extension types +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=inline-class + +import "../../Utils/expect.dart"; +import "../Patterns/Exhaustiveness/exhaustiveness_lib.dart"; + +extension type JackET(Jack _) implements Jack {} +extension type QueenET(Queen _) implements Queen {} +extension type KingET(King _) implements King {} + +String test1(Face face) { + switch (face) { + case JackET() || QueenET(): + return 'Jack or Queen'; + case KingET(suit: _): + return 'King'; + } +} + +String test2(Face face) { + switch (face) { + case JackET() || QueenET(): + return 'Jack or Queen'; + case KingET( + suit: Suit.club || Suit.diamond || Suit.heart || Suit.spade + ): + return 'King'; + } +} + +String test3(Face face) { + switch (face) { + case JackET() && JackET(oneEyed: _): return 'Jack'; + case QueenET(): return 'Queen'; + case KingET(): return 'King'; + } +} + +main() { + Expect.equals("Jack or Queen", test1(Jack(Suit.club))); + Expect.equals("Jack or Queen", test2(Jack(Suit.club))); + Expect.equals("King", test3(King(Suit.club))); +} diff --git a/LanguageFeatures/Extension-types/exhaustiveness_object_A01_t05.dart b/LanguageFeatures/Extension-types/exhaustiveness_object_A01_t05.dart new file mode 100644 index 0000000000..61676f9e37 --- /dev/null +++ b/LanguageFeatures/Extension-types/exhaustiveness_object_A01_t05.dart @@ -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 a sealed class as a +/// matched type are always exhaustive +/// +/// @description Check that it is no compile-time error if the matched value +/// type of a switch is an extension type with a sealed class as a +/// representation type and the set of cases is an exhaustive set of object +/// patterns with extension types +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=inline-class + +import "../../Utils/expect.dart"; +import "../Patterns/Exhaustiveness/exhaustiveness_lib.dart"; + +extension type FaceET1(Face _) {} +extension type FaceET2(Face _) implements Face {} + +extension type JackET(Jack _) implements Jack {} +extension type QueenET(Queen _) implements Queen {} +extension type KingET(King _) implements King {} + +String test1_1(FaceET1 face) => switch (face) { + JackET() || QueenET() => 'Jack or Queen', + KingET(suit: _) => 'King' + }; + +String test1_2(FaceET2 face) => switch (face) { + JackET() || QueenET() => 'Jack or Queen', + KingET(suit: _) => 'King' + }; + +String test2_1(FaceET1 face) => switch (face) { + JackET() || QueenET() => 'Jack or Queen', + KingET( + suit: Suit.club || Suit.diamond || Suit.heart || Suit.spade + ) => 'King' + }; + +String test2_2(FaceET2 face) => switch (face) { + JackET() || QueenET() => 'Jack or Queen', + KingET( + suit: Suit.club || Suit.diamond || Suit.heart || Suit.spade + ) => 'King' + }; + +String test3_1(FaceET1 face) => switch (face) { + JackET() && JackET(oneEyed: _) => 'Jack', + QueenET() => 'Queen', + KingET() => 'King' + }; + +String test3_2(FaceET2 face) => switch (face) { + JackET() && JackET(oneEyed: _) => 'Jack', + QueenET() => 'Queen', + KingET() => 'King' + }; + +main() { + Expect.equals("Jack or Queen", test1_1(FaceET1(Jack(Suit.club)))); + Expect.equals("Jack or Queen", test1_2(FaceET2(Jack(Suit.club)))); + Expect.equals("Jack or Queen", test2_1(FaceET1(Jack(Suit.club)))); + Expect.equals("Jack or Queen", test2_2(FaceET2(Jack(Suit.club)))); + Expect.equals("King", test3_1(FaceET1(King(Suit.club)))); + Expect.equals("King", test3_2(FaceET2(King(Suit.club)))); +} diff --git a/LanguageFeatures/Extension-types/exhaustiveness_object_A01_t06.dart b/LanguageFeatures/Extension-types/exhaustiveness_object_A01_t06.dart new file mode 100644 index 0000000000..9ae5b0c251 --- /dev/null +++ b/LanguageFeatures/Extension-types/exhaustiveness_object_A01_t06.dart @@ -0,0 +1,90 @@ +// 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 a sealed class as a +/// matched type are always exhaustive +/// +/// @description Check that it is no compile-time error if the matched value +/// type of a switch is an statement type with a sealed class as a +/// representation type and the set of cases is an exhaustive set of object +/// patterns with extension types +/// @author sgrekhov22@gmail.com +/// @issue 54444 + +// SharedOptions=--enable-experiment=inline-class + +import "../../Utils/expect.dart"; +import "../Patterns/Exhaustiveness/exhaustiveness_lib.dart"; + +extension type FaceET1(Face _) {} +extension type FaceET2(Face _) implements Face {} + +extension type JackET(Jack _) implements Jack {} +extension type QueenET(Queen _) implements Queen {} +extension type KingET(King _) implements King {} + +String test1_1(FaceET1 face) { + switch (face) { + case JackET() || QueenET(): + return 'Jack or Queen'; + case KingET(suit: _): + return 'King'; + } +} + +String test1_2(FaceET2 face) { + switch (face) { + case JackET() || QueenET(): + return 'Jack or Queen'; + case KingET(suit: _): + return 'King'; + } +} + +String test2_1(FaceET1 face) { + switch (face) { + case JackET() || QueenET(): + return 'Jack or Queen'; + case KingET( + suit: Suit.club || Suit.diamond || Suit.heart || Suit.spade + ): + return 'King'; + } +} + +String test2_2(FaceET2 face) { + switch (face) { + case JackET() || QueenET(): + return 'Jack or Queen'; + case KingET( + suit: Suit.club || Suit.diamond || Suit.heart || Suit.spade + ): + return 'King'; + } +} + +String test3_1(FaceET1 face) { + switch (face) { + case JackET() && JackET(oneEyed: _): return 'Jack'; + case QueenET(): return 'Queen'; + case KingET(): return 'King'; + } +} + +String test3_2(FaceET2 face) { + switch (face) { + case JackET() && JackET(oneEyed: _): return 'Jack'; + case QueenET(): return 'Queen'; + case KingET(): return 'King'; + } +} + +main() { + Expect.equals("Jack or Queen", test1_1(FaceET1(Jack(Suit.club)))); + Expect.equals("Jack or Queen", test1_2(FaceET2(Jack(Suit.club)))); + Expect.equals("Jack or Queen", test2_1(FaceET1(Jack(Suit.club)))); + Expect.equals("Jack or Queen", test2_2(FaceET2(Jack(Suit.club)))); + Expect.equals("King", test3_1(FaceET1(King(Suit.club)))); + Expect.equals("King", test3_2(FaceET2(King(Suit.club)))); +}