[CP] Issue 49188. Fixes for enhanced enum and switch.
Bug: https://github.com/dart-lang/sdk/issues/49188
Change-Id: I01ab5bb0cb279f40ce3c7917720e1232e00fddea
diff --git a/pkg/analyzer/lib/src/generated/error_verifier.dart b/pkg/analyzer/lib/src/generated/error_verifier.dart
index b93727e..bd4b330 100644
--- a/pkg/analyzer/lib/src/generated/error_verifier.dart
+++ b/pkg/analyzer/lib/src/generated/error_verifier.dart
@@ -3273,7 +3273,7 @@
var enumElement = expressionType.element;
if (enumElement.isEnum) {
var constantNames = enumElement.fields
- .where((field) => field.isStatic && !field.isSynthetic)
+ .where((field) => field.isEnumConstant)
.map((field) => field.name)
.toSet();
diff --git a/pkg/analyzer/lib/src/generated/resolver.dart b/pkg/analyzer/lib/src/generated/resolver.dart
index 78180a7..a0e5fa3 100644
--- a/pkg/analyzer/lib/src/generated/resolver.dart
+++ b/pkg/analyzer/lib/src/generated/resolver.dart
@@ -3824,6 +3824,8 @@
return expression.staticElement;
} else if (expression is PropertyAccess) {
return expression.propertyName.staticElement;
+ } else if (expression is SimpleIdentifier) {
+ return expression.staticElement;
}
return null;
}
diff --git a/pkg/analyzer/test/src/diagnostics/body_might_complete_normally_test.dart b/pkg/analyzer/test/src/diagnostics/body_might_complete_normally_test.dart
index 973910e..649225f 100644
--- a/pkg/analyzer/test/src/diagnostics/body_might_complete_normally_test.dart
+++ b/pkg/analyzer/test/src/diagnostics/body_might_complete_normally_test.dart
@@ -15,6 +15,42 @@
@reflectiveTest
class BodyMayCompleteNormallyTest extends PubPackageResolutionTest {
+ test_enum_method_nonNullable_blockBody_switchStatement_notNullable_exhaustive() async {
+ await assertNoErrorsInCode(r'''
+enum E {
+ a;
+
+ static const b = 0;
+ static final c = 0;
+
+ int get value {
+ switch (this) {
+ case a:
+ return 0;
+ }
+ }
+}
+''');
+ }
+
+ test_enum_method_nonNullable_blockBody_switchStatement_notNullable_notExhaustive() async {
+ await assertErrorsInCode(r'''
+enum E {
+ a, b;
+
+ int get value {
+ switch (this) {
+ case a:
+ return 0;
+ }
+ }
+}
+''', [
+ error(CompileTimeErrorCode.BODY_MIGHT_COMPLETE_NORMALLY, 28, 5),
+ error(StaticWarningCode.MISSING_ENUM_CONSTANT_IN_SWITCH, 40, 13),
+ ]);
+ }
+
test_factoryConstructor_named_blockBody() async {
await assertErrorsInCode(r'''
class A {
@@ -92,6 +128,24 @@
''');
}
+ test_function_nonNullable_blockBody_switchStatement_notNullable_exhaustive_enhanced() async {
+ await assertNoErrorsInCode(r'''
+enum E {
+ a;
+
+ static const b = 0;
+ static final c = 0;
+}
+
+int f(E e) {
+ switch (e) {
+ case E.a:
+ return 0;
+ }
+}
+''');
+ }
+
test_function_nonNullable_blockBody_switchStatement_notNullable_notExhaustive() async {
await assertErrorsInCode(r'''
enum Foo { a, b }
@@ -108,6 +162,26 @@
]);
}
+ test_function_nonNullable_blockBody_switchStatement_notNullable_notExhaustive_enhanced() async {
+ await assertErrorsInCode(r'''
+enum E {
+ a, b;
+
+ static const c = 0;
+}
+
+int f(E e) {
+ switch (e) {
+ case E.a:
+ return 0;
+ }
+}
+''', [
+ error(CompileTimeErrorCode.BODY_MIGHT_COMPLETE_NORMALLY, 47, 1),
+ error(StaticWarningCode.MISSING_ENUM_CONSTANT_IN_SWITCH, 58, 10),
+ ]);
+ }
+
test_function_nonNullable_blockBody_switchStatement_nullable_exhaustive_default() async {
await assertNoErrorsInCode(r'''
enum Foo { a, b }
diff --git a/pkg/analyzer/test/src/diagnostics/missing_enum_constant_in_switch_test.dart b/pkg/analyzer/test/src/diagnostics/missing_enum_constant_in_switch_test.dart
index 7ea8972..dd74912 100644
--- a/pkg/analyzer/test/src/diagnostics/missing_enum_constant_in_switch_test.dart
+++ b/pkg/analyzer/test/src/diagnostics/missing_enum_constant_in_switch_test.dart
@@ -17,6 +17,25 @@
@reflectiveTest
class MissingEnumConstantInSwitchTest extends PubPackageResolutionTest
with MissingEnumConstantInSwitchTestCases {
+ test_all_enhanced() async {
+ await assertNoErrorsInCode('''
+enum E {
+ one, two;
+
+ static const x = 0;
+}
+
+void f(E e) {
+ switch (e) {
+ case E.one:
+ break;
+ case E.two:
+ break;
+ }
+}
+''');
+ }
+
test_nullable() async {
await assertErrorsInCode('''
enum E { one, two }