Skip to content

Commit 7d68fee

Browse files
authored
[record_use] Enum value constants (dart-lang#3138)
1 parent 8b2540a commit 7d68fee

9 files changed

Lines changed: 288 additions & 3 deletions

File tree

‎pkgs/record_use/doc/schema/record_use.schema.json‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
{
9090
"enum": [
9191
"bool",
92+
"enum",
9293
"instance",
9394
"int",
9495
"list",
@@ -129,6 +130,39 @@
129130
]
130131
}
131132
},
133+
{
134+
"if": {
135+
"properties": {
136+
"type": {
137+
"const": "enum"
138+
}
139+
}
140+
},
141+
"then": {
142+
"properties": {
143+
"definition_index": {
144+
"type": "integer"
145+
},
146+
"index": {
147+
"type": "integer"
148+
},
149+
"name": {
150+
"type": "string"
151+
},
152+
"value": {
153+
"type": "object",
154+
"additionalProperties": {
155+
"type": "integer"
156+
}
157+
}
158+
},
159+
"required": [
160+
"definition_index",
161+
"index",
162+
"name"
163+
]
164+
}
165+
},
132166
{
133167
"if": {
134168
"properties": {

‎pkgs/record_use/lib/record_use_internal.dart‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export 'src/constant.dart'
66
show
77
BoolConstant,
88
Constant,
9+
EnumConstant,
910
InstanceConstant,
1011
IntConstant,
1112
ListConstant,

‎pkgs/record_use/lib/src/constant.dart‎

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,29 @@ sealed class MaybeConstant {
8282
},
8383
),
8484
),
85+
EnumConstantSyntax(
86+
value: final value,
87+
:final definitionIndex,
88+
:final index,
89+
:final name,
90+
) =>
91+
EnumConstant(
92+
definition: context.definitions[definitionIndex],
93+
index: index,
94+
name: name,
95+
fields: (value ?? {}).map(
96+
(key, index) {
97+
final constant = context.constants[index];
98+
if (constant is! Constant) {
99+
throw FormatException(
100+
'Enum constant field $key at index $index is not '
101+
'a constant',
102+
);
103+
}
104+
return MapEntry(key, constant);
105+
},
106+
),
107+
),
85108
RecordConstantSyntax(:final positional, :final named) => RecordConstant(
86109
positional: (positional ?? const []).map((i) {
87110
final constant = context.constants[i];
@@ -493,6 +516,87 @@ final class InstanceConstant extends Constant {
493516
}
494517
}
495518

519+
/// A constant enum value.
520+
final class EnumConstant extends Constant {
521+
/// The definition of the enum class of this value.
522+
final Definition definition;
523+
524+
/// The index of the enum member.
525+
final int index;
526+
527+
/// The name of the enum member.
528+
final String name;
529+
530+
/// The fields of this instance, mapped from field name to [Constant] value.
531+
///
532+
/// This includes additional fields from enhanced enums.
533+
final Map<String, Constant> fields;
534+
535+
/// Creates an [EnumConstant] object with the given [definition], [index],
536+
/// [name], and [fields].
537+
const EnumConstant({
538+
required this.definition,
539+
required this.index,
540+
required this.name,
541+
this.fields = const {},
542+
});
543+
544+
@override
545+
EnumConstantSyntax _toSyntax(SerializationContext context) =>
546+
EnumConstantSyntax(
547+
definitionIndex: context.definitions[definition]!,
548+
index: index,
549+
name: name,
550+
value: fields.isNotEmpty
551+
? fields.map(
552+
(key, value) => MapEntry(key, context.constants[value]!),
553+
)
554+
: null,
555+
);
556+
557+
@override
558+
bool operator ==(Object other) {
559+
if (identical(this, other)) return true;
560+
561+
return other is EnumConstant &&
562+
other.definition == definition &&
563+
other.index == index &&
564+
other.name == name &&
565+
deepEquals(other.fields, fields);
566+
}
567+
568+
@override
569+
int get hashCode => Object.hash(definition, index, name, deepHash(fields));
570+
571+
@override
572+
String toString() =>
573+
'EnumConstant($definition, index: $index, name: $name, fields: {'
574+
'${fields.entries.map((e) => '${e.key}: ${e.value}').join(', ')}})';
575+
576+
@override
577+
bool _semanticEqualsInternal(
578+
MaybeConstant other,
579+
bool allowPromotionOfUnsupported,
580+
) {
581+
if (other is! EnumConstant) return false;
582+
// ignore: invalid_use_of_visible_for_testing_member
583+
if (!definition.semanticEquals(other.definition)) return false;
584+
if (index != other.index || name != other.name) return false;
585+
if (fields.length != other.fields.length) return false;
586+
for (final entry in fields.entries) {
587+
final otherField = other.fields[entry.key];
588+
if (otherField == null ||
589+
!entry.value.semanticEquals(
590+
otherField,
591+
allowPromotionOfUnsupported: allowPromotionOfUnsupported,
592+
)) {
593+
return false;
594+
}
595+
}
596+
return true;
597+
}
598+
}
599+
496600
/// A constant record value.
497601
final class RecordConstant extends Constant {
498602
/// The positional fields of this record.

‎pkgs/record_use/lib/src/recordings.dart‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ Error: $e
296296
...calls.keys,
297297
...instances.keys,
298298
...allConstants.whereType<InstanceConstant>().map((c) => c.definition),
299+
...allConstants.whereType<EnumConstant>().map((c) => c.definition),
299300
};
300301
return DefinitionSerializationContext.fromPrevious(
301302
loadingUnitContext,
@@ -577,6 +578,9 @@ extension FlattenConstantsExtension on Iterable<MaybeConstant> {
577578
(e) => [e.key, e.value],
578579
),
579580
InstanceConstant() => constant.fields.values,
581+
EnumConstant() => [
582+
...constant.fields.values,
583+
],
580584
RecordConstant() => [
581585
...constant.positional,
582586
...constant.named.values,

‎pkgs/record_use/lib/src/reference.dart‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,12 @@ final class CallTearoff extends CallReference {
341341
}
342342
}
343343

344+
// TODO(https://github.com/dart-lang/native/issues/2908): Support enum
345+
// constant instances here as well. Enums cannot have constructor calls or
346+
// constructor tearoffs though. So how to do the type hierarchy here?
347+
// TODO(https://github.com/dart-lang/native/issues/3057): Extension type const
348+
// instances?
349+
//
344350
sealed class InstanceReference extends Reference {
345351
const InstanceReference({required super.loadingUnits});
346352

‎pkgs/record_use/lib/src/syntax.g.dart‎

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,9 @@ class ConstantSyntax extends JsonObjectSyntax {
217217
if (result.isBoolConstant) {
218218
return result.asBoolConstant;
219219
}
220+
if (result.isEnumConstant) {
221+
return result.asEnumConstant;
222+
}
220223
if (result.isInstanceConstant) {
221224
return result.asInstanceConstant;
222225
}
@@ -497,6 +500,101 @@ class DefinitionSyntax extends JsonObjectSyntax {
497500
String toString() => 'DefinitionSyntax($json)';
498501
}
499502

503+
class EnumConstantSyntax extends ConstantSyntax {
504+
EnumConstantSyntax.fromJson(
505+
super.json, {
506+
super.path,
507+
}) : super._fromJson();
508+
509+
EnumConstantSyntax({
510+
required int definitionIndex,
511+
required int index,
512+
required String name,
513+
Map<String, int>? value,
514+
super.path = const [],
515+
}) : super(type: 'enum') {
516+
_definitionIndex = definitionIndex;
517+
_index = index;
518+
_name = name;
519+
_value = value;
520+
json.sortOnKey();
521+
}
522+
523+
/// Setup all fields for [EnumConstantSyntax] that are not in
524+
/// [ConstantSyntax].
525+
void setup({
526+
required int definitionIndex,
527+
required int index,
528+
required String name,
529+
required Map<String, int>? value,
530+
}) {
531+
_definitionIndex = definitionIndex;
532+
_index = index;
533+
_name = name;
534+
_value = value;
535+
json.sortOnKey();
536+
}
537+
538+
int get definitionIndex => _reader.get<int>('definition_index');
539+
540+
set _definitionIndex(int value) {
541+
json.setOrRemove('definition_index', value);
542+
}
543+
544+
List<String> _validateDefinitionIndex() =>
545+
_reader.validate<int>('definition_index');
546+
547+
int get index => _reader.get<int>('index');
548+
549+
set _index(int value) {
550+
json.setOrRemove('index', value);
551+
}
552+
553+
List<String> _validateIndex() => _reader.validate<int>('index');
554+
555+
String get name => _reader.get<String>('name');
556+
557+
set _name(String value) {
558+
json.setOrRemove('name', value);
559+
}
560+
561+
List<String> _validateName() => _reader.validate<String>('name');
562+
563+
Map<String, int>? get value => _reader.optionalMap<int>(
564+
'value',
565+
);
566+
567+
set _value(Map<String, int>? value) {
568+
_checkArgumentMapKeys(
569+
value,
570+
);
571+
json.setOrRemove('value', value);
572+
}
573+
574+
List<String> _validateValue() => _reader.validateOptionalMap<int>(
575+
'value',
576+
);
577+
578+
@override
579+
List<String> validate() => [
580+
...super.validate(),
581+
..._validateDefinitionIndex(),
582+
..._validateIndex(),
583+
..._validateName(),
584+
..._validateValue(),
585+
];
586+
587+
@override
588+
String toString() => 'EnumConstantSyntax($json)';
589+
}
590+
591+
extension EnumConstantSyntaxExtension on ConstantSyntax {
592+
bool get isEnumConstant => type == 'enum';
593+
594+
EnumConstantSyntax get asEnumConstant =>
595+
EnumConstantSyntax.fromJson(json, path: path);
596+
}
597+
500598
class EnumNameSyntax extends NameSyntax {
501599
EnumNameSyntax.fromJson(
502600
super.json, {

‎pkgs/record_use/test/json_schema/schema_test.dart‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,23 +55,31 @@ const constInstanceIndex = 7;
5555
const constMapIndex = 3;
5656
const constUnsupportedIndex = 9;
5757
const constRecordIndex = 10;
58+
const constEnumIndex = 11;
5859
typedef SchemaTestField = (
5960
List<Object> path,
6061
void Function(ValidationResults result) missingExpectations,
6162
);
6263

6364
List<SchemaTestField> recordUseFields = [
6465
(['constants'], expectOptionalFieldMissing),
65-
for (var index = 0; index < 11; index++) ...[
66+
for (var index = 0; index < 12; index++) ...[
6667
(['constants', index, 'type'], expectRequiredFieldMissing),
6768
if (index != constNullIndex &&
6869
index != constNonConstantIndex &&
6970
index != constInstanceIndex &&
7071
index != constUnsupportedIndex &&
71-
index != constRecordIndex)
72+
index != constRecordIndex &&
73+
index != constEnumIndex)
7274
(['constants', index, 'value'], expectRequiredFieldMissing),
7375
if (index == constInstanceIndex)
7476
(['constants', index, 'value'], expectOptionalFieldMissing),
77+
if (index == constEnumIndex) ...[
78+
(['constants', index, 'definition_index'], expectRequiredFieldMissing),
79+
(['constants', index, 'index'], expectRequiredFieldMissing),
80+
(['constants', index, 'name'], expectRequiredFieldMissing),
81+
(['constants', index, 'value'], expectOptionalFieldMissing),
82+
],
7583
if (index == constMapIndex) ...[
7684
(['constants', index, 'value', 0, 'key'], expectRequiredFieldMissing),
7785
(['constants', index, 'value', 0, 'value'], expectRequiredFieldMissing),

‎pkgs/record_use/test/maybe_constant_test.dart‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ void main() {
8181
positional: [IntConstant(1)],
8282
named: {'a': IntConstant(2)},
8383
),
84+
EnumConstant(
85+
definition: definition,
86+
index: 0,
87+
name: 'red',
88+
fields: {'hex': IntConstant(0xff0000)},
89+
),
8490
],
8591
namedArguments: {
8692
'a': IntConstant(42),
@@ -90,6 +96,11 @@ void main() {
9096
positional: [IntConstant(3)],
9197
named: {'b': IntConstant(4)},
9298
),
99+
'e': EnumConstant(
100+
definition: definition,
101+
index: 1,
102+
name: 'green',
103+
),
93104
},
94105
loadingUnits: [loadingUnit1],
95106
),

0 commit comments

Comments
 (0)