Files
sdk/tests/language/patterns/exhaustiveness/variable_pattern_switch_test.dart
T
Sergey G. Grekhov 82b78916fd [tests] Remove obsolete Dart 3.0 experiments from language tests
Change-Id: If31c487e3ebe2c1ae847aff7c8994580b8b6f2f6
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/309660
Reviewed-by: Alexander Thomas <athom@google.com>
Commit-Queue: Alexander Thomas <athom@google.com>
2023-06-15 08:26:27 +00:00

178 lines
4.1 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.
enum Enum {a, b}
void exhaustiveSwitch1((Enum, bool) r) {
switch (r) /* Ok */ {
case (Enum.a, var b):
print('(a, *)');
break;
case (Enum.b, bool b):
print('(b, *)');
break;
}
}
void exhaustiveSwitch2((Enum, bool) r) {
switch (r) /* Ok */ {
case (Enum.a, true):
print('(a, false)');
break;
case (Enum a, bool b):
print('(*, *)');
break;
}
}
void nonExhaustiveSwitch1((Enum, bool) r) {
switch (r) /* Error */ {
//^^^^^^
// [analyzer] COMPILE_TIME_ERROR.NON_EXHAUSTIVE_SWITCH_STATEMENT
// ^
// [cfe] The type '(Enum, bool)' is not exhaustively matched by the switch cases since it doesn't match '(Enum.b, false)'.
case (Enum.a, var b):
print('(a, *)');
break;
case (Enum.b, true):
print('(b, true)');
break;
}
}
void nonExhaustiveSwitch2((Enum, bool) r) {
switch (r) /* Error */ {
//^^^^^^
// [analyzer] COMPILE_TIME_ERROR.NON_EXHAUSTIVE_SWITCH_STATEMENT
// ^
// [cfe] The type '(Enum, bool)' is not exhaustively matched by the switch cases since it doesn't match '(Enum.b, true)'.
case (var a, false):
print('(*, false)');
break;
case (Enum.a, true):
print('(a, true)');
break;
}
}
void nonExhaustiveSwitch3((Enum, bool) r) {
switch (r) /* Error */ {
//^^^^^^
// [analyzer] COMPILE_TIME_ERROR.NON_EXHAUSTIVE_SWITCH_STATEMENT
// ^
// [cfe] The type '(Enum, bool)' is not exhaustively matched by the switch cases since it doesn't match '(Enum.b, true)'.
case (Enum a, false):
print('(*, false)');
break;
case (Enum.a, true):
print('(a, true)');
break;
}
}
void nonExhaustiveSwitchWithDefault((Enum, bool) r) {
switch (r) /* Ok */ {
case (Enum.a, var b):
print('(a, *)');
break;
default:
print('default');
break;
}
}
void exhaustiveNullableSwitch((Enum, bool)? r) {
switch (r) /* Ok */ {
case (Enum.a, var b):
print('(a, *)');
break;
case (Enum.b, bool b):
print('(b, *)');
break;
case null:
print('null');
break;
}
}
void nonExhaustiveNullableSwitch1((Enum, bool)? r) {
switch (r) /* Error */ {
//^^^^^^
// [analyzer] COMPILE_TIME_ERROR.NON_EXHAUSTIVE_SWITCH_STATEMENT
// ^
// [cfe] The type '(Enum, bool)?' is not exhaustively matched by the switch cases since it doesn't match 'null'.
case (Enum a, bool b):
print('(*, *)');
break;
}
}
void nonExhaustiveNullableSwitch2((Enum, bool)? r) {
switch (r) /* Error */ {
//^^^^^^
// [analyzer] COMPILE_TIME_ERROR.NON_EXHAUSTIVE_SWITCH_STATEMENT
// ^
// [cfe] The type '(Enum, bool)?' is not exhaustively matched by the switch cases since it doesn't match '(Enum.a, true)'.
case (Enum a, false):
print('(*, false)');
break;
case (Enum.b, true):
print('(b, true)');
break;
case null:
print('null');
break;
}
}
void unreachableCase1((Enum, bool) r) {
switch (r) /* Ok */ {
case (Enum.a, false):
print('(a, false)');
break;
case (Enum.b, false):
print('(b, false)');
break;
case (Enum.a, true):
print('(a, true)');
break;
case (Enum.b, true):
print('(b, true)');
break;
case (Enum a, bool b): // Unreachable
// ^^^^
// [analyzer] HINT.UNREACHABLE_SWITCH_CASE
print('(*, *)');
break;
}
}
void unreachableCase2((Enum, bool) r) {
// TODO(johnniwinther): Should we avoid the unreachable error here?
switch (r) /* Error */ {
case (Enum a, bool b):
print('(*, *)');
break;
case null: // Unreachable
print('null');
break;
}
}
void unreachableCase3((Enum, bool)? r) {
switch (r) /* Ok */ {
case (var a, var b):
print('(*, *)');
break;
case null:
print('null1');
break;
case null: // Unreachable
// ^^^^
// [analyzer] HINT.UNREACHABLE_SWITCH_CASE
print('null2');
break;
}
}