Skip to content

Commit

Permalink
Issue 49188. Fixes for enhanced enum and switch.
Browse files Browse the repository at this point in the history
Bug: #49188
Change-Id: Ie23327fe813de45b2ad63d25f038d070de243d53
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/247421
Reviewed-by: Samuel Rawlins <[email protected]>
Commit-Queue: Konstantin Shcheglov <[email protected]>
  • Loading branch information
scheglov authored and Commit Bot committed Jun 7, 2022
1 parent d76e36f commit a893703
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/analyzer/lib/src/generated/error_verifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3280,7 +3280,7 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
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();

Expand Down
2 changes: 2 additions & 0 deletions pkg/analyzer/lib/src/generated/resolver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3844,6 +3844,8 @@ class _SwitchExhaustiveness {
return expression.staticElement;
} else if (expression is PropertyAccess) {
return expression.propertyName.staticElement;
} else if (expression is SimpleIdentifier) {
return expression.staticElement;
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,42 @@ main() {

@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 {
Expand Down Expand Up @@ -92,6 +128,24 @@ int f(Foo foo) {
''');
}

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_exhaustive_parenthesis() async {
await assertNoErrorsInCode(r'''
enum Foo { a, b }
Expand Down Expand Up @@ -123,6 +177,26 @@ int f(Foo foo) {
]);
}

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 }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,25 @@ main() {
@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 }
Expand Down

0 comments on commit a893703

Please sign in to comment.