Files
sdk/pkg/front_end/testcases/nnbd/switch_nullable_enum.dart
T
Johnni Winther eba2188053 [cfe] Insert synthetic break statements
With the patterns feature, switch cases without a break now implicitly
break instead of falling through to the next case (which was an error
anyway). We now insert a synthetic break statement in such cases.

To avoid crashes, the implementation to `TypeAnalyzerisAlwaysExhaustiveType` is also provided. This function
is not explicit test by this CL.

Closes #50624

Change-Id: I5d467072e26d79d0469e216bd21b460237af931b
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/278746
Reviewed-by: Jens Johansen <jensj@google.com>
Commit-Queue: Johnni Winther <johnniwinther@google.com>
2023-01-11 10:53:17 +00:00

71 lines
1.1 KiB
Dart

// Copyright (c) 2020, 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.
// @dart=2.19
enum Enum { e1, e2 }
int method1(Enum? e) {
switch (e) {
case Enum.e1:
case Enum.e2:
return 0;
}
}
int method2(Enum? e) {
switch (e) {
case Enum.e1:
case Enum.e2:
return 0;
case null:
return 1;
}
}
int method3(Enum? e) {
switch (e) {
case Enum.e1:
case Enum.e2:
return 0;
default:
return 1;
}
}
int method4(Enum? e) {
switch (e) {
case Enum.e1:
case Enum.e2:
return 0;
case null:
default:
return 1;
}
}
test() {
method1(Enum.e1);
}
main() {
expect(0, method2(Enum.e1));
expect(0, method2(Enum.e2));
expect(1, method2(null));
expect(0, method3(Enum.e1));
expect(0, method3(Enum.e2));
expect(1, method3(null));
expect(0, method4(Enum.e1));
expect(0, method4(Enum.e2));
expect(1, method4(null));
}
expect(expected, actual) {
if (expected != actual) {
throw 'Expected $expected, actual $actual.';
}
}