Issue 52197. Remove null assert from getEnumElementValue()

I see a crash like this from external users.

Bug: https://github.com/dart-lang/sdk/issues/52197
Change-Id: I5b18552ae1db50c7a103ee6b04f36d0ade8c6f5d
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/299180
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Samuel Rawlins <srawlins@google.com>
This commit is contained in:
Konstantin Shcheglov 2023-04-27 21:22:48 +00:00 committed by Commit Queue
parent d838d1a08a
commit 0f4275a443
3 changed files with 33 additions and 11 deletions

View file

@ -17,7 +17,7 @@ abstract class EnumOperations<Type extends Object, EnumClass extends Object,
/// Returns the value defined by the [enumElement]. The encoding is specific /// Returns the value defined by the [enumElement]. The encoding is specific
/// the implementation of this interface but must ensure constant value /// the implementation of this interface but must ensure constant value
/// identity. /// identity.
EnumElementValue getEnumElementValue(EnumElement enumElement); EnumElementValue? getEnumElementValue(EnumElement enumElement);
/// Returns the declared name of the [enumElement]. /// Returns the declared name of the [enumElement].
String getEnumElementName(EnumElement enumElement); String getEnumElementName(EnumElement enumElement);
@ -57,14 +57,16 @@ class EnumInfo<Type extends Object, EnumClass extends Object,
Map<EnumElementValue, EnumElementStaticType<Type, EnumElement>> elements = Map<EnumElementValue, EnumElementStaticType<Type, EnumElement>> elements =
{}; {};
for (EnumElement element in _enumOperations.getEnumElements(_enumClass)) { for (EnumElement element in _enumOperations.getEnumElements(_enumClass)) {
EnumElementValue value = _enumOperations.getEnumElementValue(element); EnumElementValue? value = _enumOperations.getEnumElementValue(element);
elements[value] = new EnumElementStaticType<Type, EnumElement>( if (value != null) {
_typeOperations, elements[value] = new EnumElementStaticType<Type, EnumElement>(
_fieldLookup, _typeOperations,
_enumOperations.getEnumElementType(element), _fieldLookup,
new IdentityRestriction<EnumElement>(element), _enumOperations.getEnumElementType(element),
_enumOperations.getEnumElementName(element), new IdentityRestriction<EnumElement>(element),
element); _enumOperations.getEnumElementName(element),
element);
}
} }
return elements; return elements;
} }

View file

@ -56,8 +56,8 @@ class AnalyzerEnumOperations
} }
@override @override
DartObject getEnumElementValue(FieldElement enumField) { DartObject? getEnumElementValue(FieldElement enumField) {
return enumField.computeConstantValue()!; return enumField.computeConstantValue();
} }
} }

View file

@ -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 { test_alwaysExhaustive_Null_hasError() async {
await assertErrorsInCode(r''' await assertErrorsInCode(r'''
void f(Null x) { void f(Null x) {