|
| 1 | +// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +/// @assertion Switch statements and expressions with a sealed class as a |
| 6 | +/// matched type are always exhaustive if the set of cases is exhaustive |
| 7 | +/// |
| 8 | +/// @description Check that it is no compile-time error if the matched value |
| 9 | +/// type of a switch statement is an extension type with a sealed class as a |
| 10 | +/// representation type and the set of cases is an exhaustive set of variable |
| 11 | +/// patterns with extension types and classes |
| 12 | + |
| 13 | +
|
| 14 | +// SharedOptions=--enable-experiment=inline-class |
| 15 | + |
| 16 | +import "../../Utils/expect.dart"; |
| 17 | + |
| 18 | +sealed class A {} |
| 19 | + |
| 20 | +class B extends A {} |
| 21 | + |
| 22 | +class C extends A {} |
| 23 | + |
| 24 | +extension type AET1(A _) {} |
| 25 | +extension type AET2(A _) implements A {} |
| 26 | +extension type BET1(B _) {} |
| 27 | +extension type BET2(B _) implements B {} |
| 28 | +extension type CET1(C _) {} |
| 29 | +extension type CET2(C _) implements C {} |
| 30 | + |
| 31 | +String test1_1(AET1 a) { |
| 32 | + switch (a) { |
| 33 | + case BET1 _: return 'B'; |
| 34 | + case CET1 _: return 'C'; |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +String test1_2(AET2 a) { |
| 39 | + switch (a) { |
| 40 | + case BET2 _: return 'B'; |
| 41 | + case CET2 _: return 'C'; |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +String test2_1(AET1 a) { |
| 46 | + switch (a) { |
| 47 | + case BET1 _: return 'B'; |
| 48 | + case CET1 _: return 'C'; |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +String test2_2(AET2 a) { |
| 53 | + switch (a) { |
| 54 | + case CET2 _: return 'C'; |
| 55 | + case BET2 _: return 'B'; |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +String test3_1(AET1 a) { |
| 60 | + switch (a) { |
| 61 | + case BET1 _: return 'B'; |
| 62 | + case C _: return 'C'; |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +String test3_2(AET2 a) { |
| 67 | + switch (a) { |
| 68 | + case CET2 _: return 'C'; |
| 69 | + case B _: return 'B'; |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +main() { |
| 74 | + Expect.equals("B", test1_1(AET1(B()))); |
| 75 | + Expect.equals("C", test1_1(AET1(C()))); |
| 76 | + Expect.equals("B", test1_2(AET2(B()))); |
| 77 | + Expect.equals("C", test1_2(AET2(C()))); |
| 78 | + |
| 79 | + Expect.equals("B", test2_1(AET1(B()))); |
| 80 | + Expect.equals("C", test2_1(AET1(C()))); |
| 81 | + Expect.equals("B", test2_2(AET2(B()))); |
| 82 | + Expect.equals("C", test2_2(AET2(C()))); |
| 83 | + |
| 84 | + Expect.equals("B", test3_1(AET1(B()))); |
| 85 | + Expect.equals("C", test3_1(AET1(C()))); |
| 86 | + Expect.equals("B", test3_2(AET2(B()))); |
| 87 | + Expect.equals("C", test3_2(AET2(C()))); |
| 88 | +} |
0 commit comments