Skip to content

Commit 8a97f6b

Browse files
authored
[record_use] Serialize NonConstant in the constant pool (dart-lang#3126)
This simplifies code. (Especially for the follow up PR: dart-lang#3127)
1 parent 4ff7f4c commit 8a97f6b

17 files changed

Lines changed: 333 additions & 181 deletions

‎pkgs/json_syntax_generator/lib/src/generator/property_generator.dart‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ set $setterName($dartType value) {
429429
$sortOnKey
430430
}
431431
432-
List<String> $validateName() => _reader.validateMap<${dartType.valueType}>('$jsonKey', $keyPattern);
432+
List<String> $validateName() => _reader.validateOptionalMap<${dartType.valueType}>('$jsonKey', $keyPattern);
433433
''');
434434
}
435435
default:

‎pkgs/json_syntax_generator/lib/src/parser/schema_analyzer.dart‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -612,10 +612,13 @@ extension type JsonSchemas._(List<JsonSchema> _schemas) {
612612
}
613613

614614
SchemaType? get type {
615-
if (types.length > 1) {
616-
throw StateError('Multiple types found');
615+
if (types.length <= 1) {
616+
return types.singleOrNull;
617+
} else if (types.length == 2 && types.contains(SchemaType.nullValue)) {
618+
return types.firstWhere((t) => t != SchemaType.nullValue);
619+
} else {
620+
throw StateError('Multiple types found: $types');
617621
}
618-
return types.singleOrNull;
619622
}
620623

621624
(SchemaType?, bool) get typeAndNullable {

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

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,13 @@
4242
"named": {
4343
"type": "object",
4444
"additionalProperties": {
45-
"type": [
46-
"integer",
47-
"null"
48-
]
45+
"type": "integer"
4946
}
5047
},
5148
"positional": {
5249
"type": "array",
5350
"items": {
54-
"type": [
55-
"integer",
56-
"null"
57-
]
51+
"type": "integer"
5852
}
5953
}
6054
}
@@ -93,6 +87,7 @@
9387
"int",
9488
"list",
9589
"map",
90+
"non_constant",
9691
"null",
9792
"string",
9893
"unsupported"
@@ -210,6 +205,22 @@
210205
]
211206
}
212207
},
208+
{
209+
"if": {
210+
"properties": {
211+
"type": {
212+
"const": "non_constant"
213+
}
214+
}
215+
},
216+
"then": {
217+
"not": {
218+
"required": [
219+
"value"
220+
]
221+
}
222+
}
223+
},
213224
{
214225
"if": {
215226
"properties": {
@@ -348,19 +359,13 @@
348359
"named": {
349360
"type": "object",
350361
"additionalProperties": {
351-
"type": [
352-
"integer",
353-
"null"
354-
]
362+
"type": "integer"
355363
}
356364
},
357365
"positional": {
358366
"type": "array",
359367
"items": {
360-
"type": [
361-
"integer",
362-
"null"
363-
]
368+
"type": "integer"
364369
}
365370
}
366371
}

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

Lines changed: 73 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,81 @@ sealed class MaybeConstant {
2222
MaybeConstant other, {
2323
bool allowPromotionOfUnsupported = false,
2424
});
25+
26+
/// Converts this [MaybeConstant] object to a syntax representation.
27+
ConstantSyntax _toSyntax(SerializationContext context);
28+
29+
/// Creates a [MaybeConstant] object from its syntax representation.
30+
static MaybeConstant _fromSyntax(
31+
ConstantSyntax syntax,
32+
DeserializationContext context,
33+
) => switch (syntax) {
34+
NonConstantConstantSyntax() => const NonConstant(),
35+
NullConstantSyntax() => const NullConstant(),
36+
BoolConstantSyntax(:final value) => BoolConstant(value),
37+
IntConstantSyntax(:final value) => IntConstant(value),
38+
StringConstantSyntax(:final value) => StringConstant(value),
39+
ListConstantSyntax(:final value) => ListConstant(
40+
value!.cast<int>().map((i) {
41+
final constant = context.constants[i];
42+
if (constant is! Constant) {
43+
throw FormatException(
44+
'List constant element at index $i is not a constant',
45+
);
46+
}
47+
return constant;
48+
}).toList(),
49+
),
50+
MapConstantSyntax(:final value) => MapConstant(
51+
value.map(
52+
(e) {
53+
final key = context.constants[e.key];
54+
if (key is! Constant) {
55+
throw FormatException(
56+
'Map constant key at index ${e.key} is not a constant',
57+
);
58+
}
59+
final value = context.constants[e.value];
60+
if (value is! Constant) {
61+
throw FormatException(
62+
'Map constant value at index ${e.value} is not a constant',
63+
);
64+
}
65+
return MapEntry(key, value);
66+
},
67+
).toList(),
68+
),
69+
InstanceConstantSyntax(value: final value, :final definitionIndex) =>
70+
InstanceConstant(
71+
definition: context.definitions[definitionIndex],
72+
fields: (value?.json ?? {}).map(
73+
(key, index) {
74+
final constant = context.constants[index as int];
75+
if (constant is! Constant) {
76+
throw FormatException(
77+
'Instance constant field $key at index $index is not '
78+
'a constant',
79+
);
80+
}
81+
return MapEntry(key, constant);
82+
},
83+
),
84+
),
85+
UnsupportedConstantSyntax(:final message) => UnsupportedConstant(message),
86+
_ => throw UnimplementedError(
87+
'"${syntax.type}" is not a supported constant type',
88+
),
89+
};
2590
}
2691

2792
/// A value that is not a constant.
2893
final class NonConstant extends MaybeConstant {
2994
const NonConstant();
3095

96+
@override
97+
NonConstantConstantSyntax _toSyntax(SerializationContext context) =>
98+
NonConstantConstantSyntax();
99+
31100
@override
32101
bool operator ==(Object other) => other is NonConstant;
33102

@@ -56,44 +125,6 @@ abstract class Constant extends MaybeConstant {
56125
/// Creates a [Constant] object.
57126
const Constant();
58127

59-
/// Converts this [Constant] object to a syntax representation.
60-
ConstantSyntax _toSyntax(SerializationContext context);
61-
62-
/// Creates a [Constant] object from its syntax representation.
63-
static Constant _fromSyntax(
64-
ConstantSyntax syntax,
65-
DeserializationContext context,
66-
) => switch (syntax) {
67-
NullConstantSyntax() => const NullConstant(),
68-
BoolConstantSyntax(:final value) => BoolConstant(value),
69-
IntConstantSyntax(:final value) => IntConstant(value),
70-
StringConstantSyntax(:final value) => StringConstant(value),
71-
ListConstantSyntax(:final value) => ListConstant(
72-
value!.cast<int>().map((i) => context.constants[i]).toList(),
73-
),
74-
MapConstantSyntax(:final value) => MapConstant(
75-
value
76-
.map(
77-
(e) => MapEntry(
78-
context.constants[e.key],
79-
context.constants[e.value],
80-
),
81-
)
82-
.toList(),
83-
),
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-
),
90-
),
91-
UnsupportedConstantSyntax(:final message) => UnsupportedConstant(message),
92-
_ => throw UnimplementedError(
93-
'"${syntax.type}" is not a supported constant type',
94-
),
95-
};
96-
97128
@override
98129
@visibleForTesting
99130
bool semanticEquals(
@@ -438,15 +469,15 @@ final class InstanceConstant extends Constant {
438469
}
439470
}
440471

441-
/// Package private (protected) methods for [Constant].
472+
/// Package private (protected) methods for [MaybeConstant].
442473
///
443474
/// This avoids bloating the public API and public API docs and prevents
444475
/// internal types from leaking from the API.
445-
extension ConstantProtected on Constant {
476+
extension MaybeConstantProtected on MaybeConstant {
446477
ConstantSyntax toSyntax(SerializationContext context) => _toSyntax(context);
447478

448-
static Constant fromSyntax(
479+
static MaybeConstant fromSyntax(
449480
ConstantSyntax syntax,
450481
DeserializationContext context,
451-
) => Constant._fromSyntax(syntax, context);
482+
) => MaybeConstant._fromSyntax(syntax, context);
452483
}

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

Lines changed: 43 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ Error: $e
148148
RecordedUsesSyntax syntax,
149149
DefinitionDeserializationContext definitionContext,
150150
) {
151-
final constants = <Constant>[];
151+
final constants = <MaybeConstant>[];
152152
// Create a context that includes an empty list for the constants. This
153153
// list will be populated by [_deserializeConstants], providing the
154154
// self-referential access needed to resolve recursive constants (e.g.
@@ -158,7 +158,10 @@ Error: $e
158158
constants,
159159
);
160160
for (final constantSyntax in syntax.constants ?? <ConstantSyntax>[]) {
161-
final constant = ConstantProtected.fromSyntax(constantSyntax, context);
161+
final constant = MaybeConstantProtected.fromSyntax(
162+
constantSyntax,
163+
context,
164+
);
162165
if (!constants.contains(constant)) {
163166
constants.add(constant);
164167
}
@@ -237,44 +240,36 @@ Error: $e
237240
);
238241
}
239242

240-
Iterable<Constant> _collectConstants() => {
241-
...calls.values
242-
.expand((calls) => calls)
243-
.whereType<CallWithArguments>()
244-
.expand(
245-
(call) => [
246-
...call.positionalArguments,
247-
...call.namedArguments.values,
248-
],
249-
)
250-
.expand(
251-
(argument) => switch (argument) {
252-
final Constant c => [c],
253-
NonConstant() => <Constant>[],
254-
},
255-
),
256-
...instances.values
257-
.expand((instances) => instances)
258-
.expand(
259-
(instance) => switch (instance) {
260-
InstanceConstantReference(:final instanceConstant) => {
261-
...instanceConstant.fields.values,
262-
instanceConstant,
243+
Iterable<MaybeConstant> _collectConstants() {
244+
final values = <MaybeConstant>{
245+
...calls.values
246+
.expand((calls) => calls)
247+
.whereType<CallWithArguments>()
248+
.expand(
249+
(call) => [
250+
...call.positionalArguments,
251+
...call.namedArguments.values,
252+
],
253+
),
254+
...instances.values
255+
.expand((instances) => instances)
256+
.expand(
257+
(instance) => switch (instance) {
258+
InstanceConstantReference(:final instanceConstant) => {
259+
...instanceConstant.fields.values,
260+
instanceConstant,
261+
},
262+
InstanceCreationReference(
263+
:final positionalArguments,
264+
:final namedArguments,
265+
) =>
266+
[...positionalArguments, ...namedArguments.values],
267+
ConstructorTearoffReference() => <MaybeConstant>[],
263268
},
264-
InstanceCreationReference(
265-
:final positionalArguments,
266-
:final namedArguments,
267-
) =>
268-
[...positionalArguments, ...namedArguments.values].expand(
269-
(argument) => switch (argument) {
270-
final Constant c => [c],
271-
NonConstant() => <Constant>[],
272-
},
273-
),
274-
ConstructorTearoffReference() => <Constant>[],
275-
},
276-
),
277-
}.flatten();
269+
),
270+
};
271+
return values.flatten();
272+
}
278273

279274
Iterable<LoadingUnit> _collectLoadingUnits() => {
280275
...calls.values
@@ -292,7 +287,7 @@ Error: $e
292287
);
293288

294289
DefinitionSerializationContext _serializeDefinitions(
295-
Iterable<Constant> allConstants,
290+
Iterable<MaybeConstant> allConstants,
296291
LoadingUnitSerializationContext loadingUnitContext,
297292
) {
298293
final allDefinitions = {
@@ -307,7 +302,7 @@ Error: $e
307302
}
308303

309304
(SerializationContext, List<ConstantSyntax>) _serializeConstants(
310-
Iterable<Constant> allConstants,
305+
Iterable<MaybeConstant> allConstants,
311306
DefinitionSerializationContext definitionContext,
312307
) {
313308
final constantsMap = allConstants.asMapToIndices;
@@ -563,23 +558,24 @@ Error: $e
563558
}
564559
}
565560

566-
extension FlattenConstantsExtension on Iterable<Constant> {
567-
Set<Constant> flatten() {
568-
final constants = <Constant>{};
561+
extension FlattenConstantsExtension on Iterable<MaybeConstant> {
562+
Set<MaybeConstant> flatten() {
563+
final constants = <MaybeConstant>{};
569564
for (final constant in this) {
570565
depthFirstSearch(constant, constants);
571566
}
572567
return constants;
573568
}
574569

575-
void depthFirstSearch(Constant constant, Set<Constant> collected) {
576-
final children = switch (constant) {
570+
void depthFirstSearch(MaybeConstant constant, Set<MaybeConstant> collected) {
571+
// ignore: omit_local_variable_types
572+
final Iterable<MaybeConstant> children = switch (constant) {
577573
ListConstant() => constant.value,
578574
MapConstant() => constant.entries.expand(
579575
(e) => [e.key, e.value],
580576
),
581577
InstanceConstant() => constant.fields.values,
582-
_ => <Constant>[],
578+
_ => <MaybeConstant>[],
583579
};
584580
for (final child in children) {
585581
if (!collected.contains(child)) {

0 commit comments

Comments
 (0)