Skip to content

Commit 1ccc1b7

Browse files
authored
[record_use] Instance constant definitions (dart-lang#3121)
Reland of dart-lang#3110 to the right branch. (GitHub sometimes rebases stacked PRs to main and sometimes doesn't...)
1 parent b88982b commit 1ccc1b7

19 files changed

Lines changed: 94 additions & 17 deletions

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,17 @@
114114
},
115115
"then": {
116116
"properties": {
117+
"definition_index": {
118+
"type": "integer"
119+
},
117120
"value": {
118121
"type": "object",
119122
"additionalProperties": true
120123
}
121-
}
124+
},
125+
"required": [
126+
"definition_index"
127+
]
122128
}
123129
},
124130
{

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

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import 'package:meta/meta.dart';
66

7+
import 'definition.dart';
78
import 'helper.dart';
89
import 'serialization_context.dart';
910
import 'syntax.g.dart';
@@ -80,11 +81,13 @@ abstract class Constant extends MaybeConstant {
8081
)
8182
.toList(),
8283
),
83-
InstanceConstantSyntax(value: final value) => InstanceConstant(
84-
fields: (value?.json ?? {}).map(
85-
(key, value) => MapEntry(key, context.constants[value as int]),
84+
InstanceConstantSyntax(value: final value, :final definitionIndex) =>
85+
InstanceConstant(
86+
definition: context.definitions[definitionIndex],
87+
fields: (value?.json ?? {}).map(
88+
(key, value) => MapEntry(key, context.constants[value as int]),
89+
),
8690
),
87-
),
8891
UnsupportedConstantSyntax(:final message) => UnsupportedConstant(message),
8992
_ => throw UnimplementedError(
9093
'"${syntax.type}" is not a supported constant type',
@@ -371,15 +374,20 @@ final class MapConstant extends Constant {
371374
/// Only as far as they can also be represented by constants. This is more or
372375
/// less the same as a [MapConstant].
373376
final class InstanceConstant extends Constant {
377+
/// The definition of the class of this instance.
378+
final Definition definition;
379+
374380
/// The fields of this instance, mapped from field name to [Constant] value.
375381
final Map<String, Constant> fields;
376382

377-
/// Creates an [InstanceConstant] object with the given [fields].
378-
const InstanceConstant({required this.fields});
383+
/// Creates an [InstanceConstant] object with the given [definition] and
384+
/// [fields].
385+
const InstanceConstant({required this.definition, required this.fields});
379386

380387
@override
381388
InstanceConstantSyntax _toSyntax(SerializationContext context) =>
382389
InstanceConstantSyntax(
390+
definitionIndex: context.definitions[definition]!,
383391
value: fields.isNotEmpty
384392
? JsonObjectSyntax.fromJson(
385393
fields.map(
@@ -394,15 +402,17 @@ final class InstanceConstant extends Constant {
394402
bool operator ==(Object other) {
395403
if (identical(this, other)) return true;
396404

397-
return other is InstanceConstant && deepEquals(other.fields, fields);
405+
return other is InstanceConstant &&
406+
other.definition == definition &&
407+
deepEquals(other.fields, fields);
398408
}
399409

400410
@override
401-
int get hashCode => deepHash(fields);
411+
int get hashCode => Object.hash(definition, deepHash(fields));
402412

403413
@override
404414
String toString() =>
405-
'InstanceConstant({'
415+
'InstanceConstant($definition, {'
406416
'${fields.entries.map((e) => '${e.key}: ${e.value}').join(', ')}})';
407417

408418
@override
@@ -411,6 +421,8 @@ final class InstanceConstant extends Constant {
411421
bool allowPromotionOfUnsupported,
412422
) {
413423
if (other is! InstanceConstant) return false;
424+
// ignore: invalid_use_of_visible_for_testing_member
425+
if (!definition.semanticEquals(other.definition)) return false;
414426
if (fields.length != other.fields.length) return false;
415427
for (final entry in fields.entries) {
416428
final otherField = other.fields[entry.key];

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ Error: $e
230230
final allDefinitions = {
231231
...calls.keys,
232232
...instances.keys,
233+
...allConstants.whereType<InstanceConstant>().map((c) => c.definition),
233234
}.toList();
234235
return DefinitionSerializationContext(
235236
allDefinitions.asMapToIndices,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
///
3232
/// 1. **Definitions**: [Definition] objects are deserialized from or
3333
/// serialized to the [RecordedUsesSyntax.definitions] pool first.
34-
/// 2. **Constants**: [Constant] objects are (de)serialized from or serialized
34+
/// 2. **Constants**: [Constant] objects are deserialized from or serialized
3535
/// to the [RecordedUsesSyntax.constants] pool second. They may contain
3636
/// references to the definitions pool (e.g. for [InstanceConstant]) and the
3737
/// constants pool (for recursive collections).

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

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,19 @@ class ConstantSyntax extends JsonObjectSyntax {
187187
List<String> _validateType() => _reader.validate<String>('type');
188188

189189
@override
190-
List<String> validate() => [...super.validate(), ..._validateType()];
190+
List<String> validate() => [
191+
...super.validate(),
192+
..._validateType(),
193+
..._validateExtraRulesConstant(),
194+
];
195+
196+
List<String> _validateExtraRulesConstant() {
197+
final result = <String>[];
198+
if (_reader.tryTraverse(['type']) == 'instance') {
199+
result.addAll(_reader.validate<Object>('definition_index'));
200+
}
201+
return result;
202+
}
191203

192204
@override
193205
String toString() => 'ConstantSyntax($json)';
@@ -584,19 +596,33 @@ class InstanceConstantSyntax extends ConstantSyntax {
584596
super.path,
585597
}) : super._fromJson();
586598

587-
InstanceConstantSyntax({JsonObjectSyntax? value, super.path = const []})
588-
: super(type: 'instance') {
599+
InstanceConstantSyntax({
600+
required int definitionIndex,
601+
JsonObjectSyntax? value,
602+
super.path = const [],
603+
}) : super(type: 'instance') {
604+
_definitionIndex = definitionIndex;
589605
_value = value;
590606
json.sortOnKey();
591607
}
592608

593609
/// Setup all fields for [InstanceConstantSyntax] that are not in
594610
/// [ConstantSyntax].
595-
void setup({required JsonObjectSyntax? value}) {
611+
void setup({required int definitionIndex, required JsonObjectSyntax? value}) {
612+
_definitionIndex = definitionIndex;
596613
_value = value;
597614
json.sortOnKey();
598615
}
599616

617+
int get definitionIndex => _reader.get<int>('definition_index');
618+
619+
set _definitionIndex(int value) {
620+
json.setOrRemove('definition_index', value);
621+
}
622+
623+
List<String> _validateDefinitionIndex() =>
624+
_reader.validate<int>('definition_index');
625+
600626
JsonObjectSyntax? get value {
601627
final jsonValue = _reader.optionalMap('value');
602628
if (jsonValue == null) return null;
@@ -616,7 +642,11 @@ class InstanceConstantSyntax extends ConstantSyntax {
616642
}
617643

618644
@override
619-
List<String> validate() => [...super.validate(), ..._validateValue()];
645+
List<String> validate() => [
646+
...super.validate(),
647+
..._validateDefinitionIndex(),
648+
..._validateValue(),
649+
];
620650

621651
@override
622652
String toString() => 'InstanceConstantSyntax($json)';

‎pkgs/record_use/test/complex_keys_test.dart‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,14 @@ import 'package:record_use/record_use_internal.dart';
77
import 'package:test/test.dart';
88

99
void main() {
10+
const classDefinition = Definition(
11+
'package:test/test.dart',
12+
[Name('MyClass')],
13+
);
14+
1015
test('MapConstant with InstanceConstant keys round-trip', () {
1116
const instanceKey = InstanceConstant(
17+
definition: classDefinition,
1218
fields: {
1319
'id': IntConstant(1),
1420
'tag': StringConstant('key'),
@@ -49,6 +55,7 @@ void main() {
4955

5056
test('MapConstant equality with InstanceConstant keys', () {
5157
const instanceKey = InstanceConstant(
58+
definition: classDefinition,
5259
fields: {
5360
'id': IntConstant(1),
5461
'tag': StringConstant('key'),
@@ -62,6 +69,7 @@ void main() {
6269
expect(
6370
mapConstant.entries.first.key,
6471
const InstanceConstant(
72+
definition: classDefinition,
6573
fields: {
6674
'id': IntConstant(1),
6775
'tag': StringConstant('key'),

‎pkgs/record_use/test/test_data.dart‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,13 @@ final recordedUses = Recordings(
6363
instanceId: [
6464
const InstanceConstantReference(
6565
instanceConstant: InstanceConstant(
66+
definition: instanceId,
6667
fields: {'a': IntConstant(42), 'b': NullConstant()},
6768
),
6869
loadingUnit: '3',
6970
),
7071
const InstanceConstantReference(
71-
instanceConstant: InstanceConstant(fields: {}),
72+
instanceConstant: InstanceConstant(definition: instanceId, fields: {}),
7273
loadingUnit: '3',
7374
),
7475
],
@@ -176,13 +177,15 @@ const recordedUsesJson = '''{
176177
"type": "null"
177178
},
178179
{
180+
"definition_index": 1,
179181
"type": "instance",
180182
"value": {
181183
"a": 14,
182184
"b": 15
183185
}
184186
},
185187
{
188+
"definition_index": 1,
186189
"type": "instance"
187190
}
188191
],

‎pkgs/record_use/test_data/json/const_argument_instance.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"value": 14
77
},
88
{
9+
"definition_index": 0,
910
"type": "instance",
1011
"value": {
1112
"i": 0

‎pkgs/record_use/test_data/json/enum_const_arg.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"value": "b"
1111
},
1212
{
13+
"definition_index": 0,
1314
"type": "instance",
1415
"value": {
1516
"index": 0,

‎pkgs/record_use/test_data/json/instance_class.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"value": 42
77
},
88
{
9+
"definition_index": 0,
910
"type": "instance",
1011
"value": {
1112
"i": 0

0 commit comments

Comments
 (0)