dart-sdk/tests/language/extension_type/exhaustiveness_error_test.dart
Lasse R.H. Nielsen 9d933d1281 Retire 3.3 experiments in the 3.4 release.
Tested: No new tests.
Change-Id: Idf19ce8b6743b221841e6cef6b2a80e8ab37860e
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/354260
Auto-Submit: Lasse Nielsen <lrn@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Reviewed-by: Nate Bosch <nbosch@google.com>
Reviewed-by: Daco Harkes <dacoharkes@google.com>
Commit-Queue: Lasse Nielsen <lrn@google.com>
2024-03-04 16:09:31 +00:00

99 lines
3.4 KiB
Dart

// 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.
sealed class A {}
class B extends A {}
class C extends A {}
extension type ExtensionTypeList<T>(List<T> it) implements List<T> {}
extension type ExtensionTypeMap<K, V>(Map<K, V> it) implements Map<K, V> {
V method(K key) => it[key]!;
V get getter => it.values.first;
void genericMethod<T>(K key, V value, void Function<T>(T) f) {}
}
nonExhaustiveSealedSubtype(ExtensionTypeList<A> list) {
return switch (list) {
// ^^^^^^
// [analyzer] COMPILE_TIME_ERROR.NON_EXHAUSTIVE_SWITCH_EXPRESSION
// ^
// [cfe] The type 'ExtensionTypeList<A>' is not exhaustively matched by the switch cases since it doesn't match '[B(), C()]'.
[] => 0,
[_] => 1,
[_, B()] => 2,
[_, _, _, ...] => 3,
};
}
nonExhaustiveMapExtensionType1(ExtensionTypeMap<int, A> map) {
var a = switch (map) {
// ^^^^^^
// [analyzer] COMPILE_TIME_ERROR.NON_EXHAUSTIVE_SWITCH_EXPRESSION
// ^
// [cfe] The type 'ExtensionTypeMap<int, A>' is not exhaustively matched by the switch cases since it doesn't match 'Map<int, A>(isEmpty: false)'.
Map(isEmpty: true) => 0,
{0: B b, 1: _} => 4,
{0: C c, 1: _} => 5,
{0: _, 1: _} => 3,
// ^^
// [analyzer] HINT.UNREACHABLE_SWITCH_CASE
{0: B b} => 1,
{0: C c} => 2,
};
}
nonExhaustiveMapExtensionType2(ExtensionTypeMap<int, A> map) {
var c = switch (map) {
// ^^^^^^
// [analyzer] COMPILE_TIME_ERROR.NON_EXHAUSTIVE_SWITCH_EXPRESSION
// ^
// [cfe] The type 'ExtensionTypeMap<int, A>' is not exhaustively matched by the switch cases since it doesn't match 'Map<int, A>()'.
Map<int, B>() => 0,
};
}
nonExhaustiveMapMethod(ExtensionTypeMap<int, A> map) {
return switch (map) {
// ^^^^^^
// [analyzer] COMPILE_TIME_ERROR.NON_EXHAUSTIVE_SWITCH_EXPRESSION
// ^
// [cfe] The type 'ExtensionTypeMap<int, A>' is not exhaustively matched by the switch cases since it doesn't match 'Map<int, A>(method: A Function(int) _)'.
ExtensionTypeMap(:B Function(int) method) => 0,
};
}
nonExhaustiveMapGetter(ExtensionTypeMap<int, A> map) {
return switch (map) {
// ^^^^^^
// [analyzer] COMPILE_TIME_ERROR.NON_EXHAUSTIVE_SWITCH_EXPRESSION
// ^
// [cfe] The type 'ExtensionTypeMap<int, A>' is not exhaustively matched by the switch cases since it doesn't match 'Map<int, A>(getter: C())'.
ExtensionTypeMap(:B getter) => 0,
};
}
nonExhaustiveMapGenericMethod(ExtensionTypeMap<int, A> map) {
return switch (map) {
// ^^^^^^
// [analyzer] COMPILE_TIME_ERROR.NON_EXHAUSTIVE_SWITCH_EXPRESSION
// ^
// [cfe] The type 'ExtensionTypeMap<int, A>' is not exhaustively matched by the switch cases since it doesn't match 'Map<int, A>(genericMethod: void Function<T>(int, A, void Function<T>(T)) _)'.
ExtensionTypeMap(
:void Function<X>(int, B, void Function(X)) genericMethod) => 0,
};
}
nonExhaustiveMapField(ExtensionTypeMap<int, A> map) {
return switch (map) {
// ^^^^^^
// [analyzer] COMPILE_TIME_ERROR.NON_EXHAUSTIVE_SWITCH_EXPRESSION
// ^
// [cfe] The type 'ExtensionTypeMap<int, A>' is not exhaustively matched by the switch cases since it doesn't match 'Map<int, A>(it: Map<int, A>())'.
ExtensionTypeMap(:Map<int, B> it) => 0,
};
}