Skip to content

Commit

Permalink
Issue 52197. Remove null assert from getEnumElementValue()
Browse files Browse the repository at this point in the history
I see a crash like this from external users.

Bug: #52197
Change-Id: I5b18552ae1db50c7a103ee6b04f36d0ade8c6f5d
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/299180
Commit-Queue: Konstantin Shcheglov <[email protected]>
Reviewed-by: Samuel Rawlins <[email protected]>
  • Loading branch information
scheglov authored and Commit Queue committed Apr 27, 2023
1 parent d838d1a commit 0f4275a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
20 changes: 11 additions & 9 deletions pkg/_fe_analyzer_shared/lib/src/exhaustiveness/types/enum.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ abstract class EnumOperations<Type extends Object, EnumClass extends Object,
/// Returns the value defined by the [enumElement]. The encoding is specific
/// the implementation of this interface but must ensure constant value
/// identity.
EnumElementValue getEnumElementValue(EnumElement enumElement);
EnumElementValue? getEnumElementValue(EnumElement enumElement);

/// Returns the declared name of the [enumElement].
String getEnumElementName(EnumElement enumElement);
Expand Down Expand Up @@ -57,14 +57,16 @@ class EnumInfo<Type extends Object, EnumClass extends Object,
Map<EnumElementValue, EnumElementStaticType<Type, EnumElement>> elements =
{};
for (EnumElement element in _enumOperations.getEnumElements(_enumClass)) {
EnumElementValue value = _enumOperations.getEnumElementValue(element);
elements[value] = new EnumElementStaticType<Type, EnumElement>(
_typeOperations,
_fieldLookup,
_enumOperations.getEnumElementType(element),
new IdentityRestriction<EnumElement>(element),
_enumOperations.getEnumElementName(element),
element);
EnumElementValue? value = _enumOperations.getEnumElementValue(element);
if (value != null) {
elements[value] = new EnumElementStaticType<Type, EnumElement>(
_typeOperations,
_fieldLookup,
_enumOperations.getEnumElementType(element),
new IdentityRestriction<EnumElement>(element),
_enumOperations.getEnumElementName(element),
element);
}
}
return elements;
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/analyzer/lib/src/generated/exhaustiveness.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ class AnalyzerEnumOperations
}

@override
DartObject getEnumElementValue(FieldElement enumField) {
return enumField.computeConstantValue()!;
DartObject? getEnumElementValue(FieldElement enumField) {
return enumField.computeConstantValue();
}
}

Expand Down
20 changes: 20 additions & 0 deletions pkg/analyzer/test/src/diagnostics/non_exhaustive_switch_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,26 @@ void f(E x) {
);
}

test_alwaysExhaustive_enum_cannotCompute() async {
await assertErrorsInCode(r'''
enum E {
v1(v2), v2(v1);
const E(Object f);
}
void f(E x) {
switch (x) {
case E.v1:
case E.v2:
break;
}
}
''', [
error(CompileTimeErrorCode.RECURSIVE_COMPILE_TIME_CONSTANT, 11, 2),
error(CompileTimeErrorCode.RECURSIVE_COMPILE_TIME_CONSTANT, 19, 2),
]);
}

test_alwaysExhaustive_Null_hasError() async {
await assertErrorsInCode(r'''
void f(Null x) {
Expand Down

0 comments on commit 0f4275a

Please sign in to comment.