Files
sdk/tests/language/patterns/shared_case_variable_error_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

76 lines
2.5 KiB
Dart

// Copyright (c) 2022, 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.
import "package:expect/expect.dart";
main() {
// Variables in shared cases must agree on finality if used in the body.
switch ((0, 1)) {
case (0, int x):
case (1, final int x):
print(x);
// ^
// [analyzer] COMPILE_TIME_ERROR.INVALID_PATTERN_VARIABLE_IN_SHARED_CASE_SCOPE
// [cfe] Variable pattern 'x' doesn't have the same type or finality in all cases.
}
switch ((0, 1)) {
case (2, var x):
case (3, final x):
print(x);
// ^
// [analyzer] COMPILE_TIME_ERROR.INVALID_PATTERN_VARIABLE_IN_SHARED_CASE_SCOPE
// [cfe] Variable pattern 'x' doesn't have the same type or finality in all cases.
}
// Variables in shared cases must agree on type if used in the body.
switch ((0, 1)) {
case (0, int x):
case (1, num x):
print(x);
// ^
// [analyzer] COMPILE_TIME_ERROR.INVALID_PATTERN_VARIABLE_IN_SHARED_CASE_SCOPE
// [cfe] Variable pattern 'x' doesn't have the same type or finality in all cases.
}
switch ((0, 's')) {
case (0, int x):
case (2, var x): // Infer String.
print(x);
// ^
// [analyzer] COMPILE_TIME_ERROR.INVALID_PATTERN_VARIABLE_IN_SHARED_CASE_SCOPE
// [cfe] Variable pattern 'x' doesn't have the same type or finality in all cases.
}
// Variables must be defined in all cases if used in body.
switch ((0, 1)) {
case (0, var unique):
case (1, var inTwo):
case (2, var inTwo):
print(unique);
// ^^^^^^
// [analyzer] COMPILE_TIME_ERROR.INVALID_PATTERN_VARIABLE_IN_SHARED_CASE_SCOPE
// [cfe] The variable 'unique' is available in some, but not all cases that share this body.
print(inTwo);
// ^^^^^
// [analyzer] COMPILE_TIME_ERROR.INVALID_PATTERN_VARIABLE_IN_SHARED_CASE_SCOPE
// [cfe] The variable 'inTwo' is available in some, but not all cases that share this body.
}
// Mismatched variable types because of inference from a promoted type.
Object value = 1;
// Promote value to int.
if (value is int) {
switch ((0, value)) {
case (0, var a):
case (1, Object a):
print(a);
// ^
// [analyzer] COMPILE_TIME_ERROR.INVALID_PATTERN_VARIABLE_IN_SHARED_CASE_SCOPE
// [cfe] Variable pattern 'a' doesn't have the same type or finality in all cases.
}
}
}