Skip to content

Commit 0819678

Browse files
authored
[record_use] Add constructor invocations to recorded instances (dart-lang#3047)
Refactored `InstanceReference` into a sealed class hierarchy to support constructor invocations and tear-offs alongside constant instances. ### Changes - **Hierarchy:** Introduced `InstanceConstantReference`, `InstanceCreationReference`, and `ConstructorTearoffReference`. - **Schema:** Updated `record_use.schema.json` with a `type` discriminator for instances and regenerated `syntax.g.dart`. - **API:** Updated `Recordings` and `RecordedUsages` to handle the polymorphic types and improved semantic equality comparison. - **Migration:** Updated all JSON test data to include the required `"type": "constant"` field and ensured trailing newlines. - **Testing:** Added `test/instance_references_test.dart` and updated schema validation tests. Relevant issues: * dart-lang#2907 * dart-lang#2911
1 parent 918a5a8 commit 0819678

23 files changed

Lines changed: 647 additions & 81 deletions

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

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -235,16 +235,77 @@
235235
"Instance": {
236236
"type": "object",
237237
"properties": {
238-
"constant_index": {
239-
"type": "integer"
238+
"type": {
239+
"type": "string",
240+
"anyOf": [
241+
{
242+
"enum": [
243+
"constant",
244+
"creation",
245+
"tearoff"
246+
]
247+
},
248+
{
249+
"type": "string"
250+
}
251+
]
240252
},
241253
"loading_unit": {
242254
"type": "string"
243255
}
244256
},
245257
"required": [
246-
"constant_index",
247-
"loading_unit"
258+
"loading_unit",
259+
"type"
260+
],
261+
"allOf": [
262+
{
263+
"if": {
264+
"properties": {
265+
"type": {
266+
"const": "constant"
267+
}
268+
}
269+
},
270+
"then": {
271+
"properties": {
272+
"constant_index": {
273+
"type": "integer"
274+
}
275+
},
276+
"required": [
277+
"constant_index"
278+
]
279+
}
280+
},
281+
{
282+
"if": {
283+
"properties": {
284+
"type": {
285+
"const": "creation"
286+
}
287+
}
288+
},
289+
"then": {
290+
"properties": {
291+
"named": {
292+
"type": "object",
293+
"additionalProperties": {
294+
"type": "integer"
295+
}
296+
},
297+
"positional": {
298+
"type": "array",
299+
"items": {
300+
"type": [
301+
"integer",
302+
"null"
303+
]
304+
}
305+
}
306+
}
307+
}
308+
}
248309
]
249310
},
250311
"MapEntry": {

‎pkgs/record_use/lib/record_use_internal.dart‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,12 @@ export 'src/record_use.dart' show RecordedUsages;
2020
export 'src/recordings.dart'
2121
show FlattenConstantsExtension, MapifyIterableExtension, Recordings;
2222
export 'src/reference.dart'
23-
show CallReference, CallTearOff, CallWithArguments, InstanceReference;
23+
show
24+
CallReference,
25+
CallTearoff,
26+
CallWithArguments,
27+
ConstructorTearoffReference,
28+
InstanceConstantReference,
29+
InstanceCreationReference,
30+
InstanceReference;
2431
export 'src/version.dart' show version;

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,11 @@ extension type RecordedUsages._(Recordings _recordings) {
108108
/// What kinds of fields can be recorded depends on the implementation of
109109
/// https://dart-review.googlesource.com/c/sdk/+/369620/13/pkg/vm/lib/transformations/record_use/record_instance.dart
110110
Iterable<ConstantInstance> constantsOf(Identifier identifier) =>
111-
_recordings.instances[identifier]?.map(
112-
(reference) => ConstantInstance(reference.instanceConstant.fields),
113-
) ??
111+
_recordings.instances[identifier]
112+
?.whereType<InstanceConstantReference>()
113+
.map(
114+
(reference) => ConstantInstance(reference.instanceConstant.fields),
115+
) ??
114116
[];
115117

116118
/// Checks if any call to [identifier] has non-const arguments, or if any
@@ -124,7 +126,7 @@ extension type RecordedUsages._(Recordings _recordings) {
124126
bool hasNonConstArguments(Identifier identifier) =>
125127
(_recordings.calls[identifier] ?? []).any(
126128
(element) => switch (element) {
127-
CallTearOff() => true,
129+
CallTearoff() => true,
128130
final CallWithArguments call => call.positionalArguments.any(
129131
(argument) => argument == null,
130132
),

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,17 @@ Error: $e
134134
...instancesForDefinition.values
135135
.expand((instances) => instances)
136136
.expand(
137-
(instance) => {
138-
...instance.instanceConstant.fields.values,
139-
instance.instanceConstant,
137+
(instance) => switch (instance) {
138+
InstanceConstantReference(:final instanceConstant) => {
139+
...instanceConstant.fields.values,
140+
instanceConstant,
141+
},
142+
InstanceCreationReference(
143+
:final positionalArguments,
144+
:final namedArguments,
145+
) =>
146+
[...positionalArguments, ...namedArguments.values].nonNulls,
147+
ConstructorTearoffReference() => <Constant>[],
140148
},
141149
),
142150
}.flatten().asMapToIndices;
@@ -218,7 +226,7 @@ Error: $e
218226
/// a usage from [expected] cannot be found in `this`, simulating the effect
219227
/// of a compiler optimizing away a call entirely.
220228
///
221-
/// If [allowTearOffToStaticPromotion] is `true`, allows an [expected]
229+
/// If [allowTearoffToStaticPromotion] is `true`, allows an [expected]
222230
/// function tear-off to match an `actual` static call.
223231
///
224232
/// If [allowDefinitionLoadingUnitNull] is `true`, allows a definition's
@@ -237,7 +245,7 @@ Error: $e
237245
Recordings expected, {
238246
bool expectedIsSubset = false,
239247
bool allowDeadCodeElimination = false,
240-
bool allowTearOffToStaticPromotion = false,
248+
bool allowTearoffToStaticPromotion = false,
241249
bool allowDefinitionLoadingUnitNull = false,
242250
bool allowMoreConstArguments = false,
243251
bool allowMetadataMismatch = false,
@@ -264,7 +272,7 @@ Error: $e
264272
// ignore: invalid_use_of_visible_for_testing_member
265273
referenceMatches: (CallReference a, CallReference b) => a.semanticEquals(
266274
b,
267-
allowTearOffToStaticPromotion: allowTearOffToStaticPromotion,
275+
allowTearoffToStaticPromotion: allowTearoffToStaticPromotion,
268276
allowMoreConstArguments: allowMoreConstArguments,
269277
uriMapping: uriMapping,
270278
loadingUnitMapping: loadingUnitMapping,
@@ -285,6 +293,7 @@ Error: $e
285293
b,
286294
uriMapping: uriMapping,
287295
loadingUnitMapping: loadingUnitMapping,
296+
allowMoreConstArguments: allowMoreConstArguments,
288297
),
289298
)) {
290299
return false;

0 commit comments

Comments
 (0)