Skip to content

Commit 4e1412b

Browse files
authored
[record_use] Constructor definitions (dart-lang#3170)
1 parent dd59094 commit 4e1412b

7 files changed

Lines changed: 226 additions & 4 deletions

File tree

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,9 @@
449449
},
450450
"then": {
451451
"properties": {
452+
"definition_index": {
453+
"type": "integer"
454+
},
452455
"named": {
453456
"type": "object",
454457
"additionalProperties": {
@@ -461,7 +464,29 @@
461464
"type": "integer"
462465
}
463466
}
467+
},
468+
"required": [
469+
"definition_index"
470+
]
471+
}
472+
},
473+
{
474+
"if": {
475+
"properties": {
476+
"type": {
477+
"const": "tearoff"
478+
}
464479
}
480+
},
481+
"then": {
482+
"properties": {
483+
"definition_index": {
484+
"type": "integer"
485+
}
486+
},
487+
"required": [
488+
"definition_index"
489+
]
465490
}
466491
}
467492
]

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

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -436,8 +436,10 @@ sealed class InstanceReference extends Reference {
436436
:final named,
437437
:final positional,
438438
:final loadingUnitIndices,
439+
:final definitionIndex,
439440
) =>
440441
InstanceCreationReference(
442+
definition: context.definitions[definitionIndex],
441443
positionalArguments: (positional ?? [])
442444
.map((index) => CallReference._argumentFromSyntax(index, context))
443445
.toList(),
@@ -451,8 +453,12 @@ sealed class InstanceReference extends Reference {
451453
.map((index) => context.loadingUnits[index])
452454
.toList(),
453455
),
454-
TearoffInstanceSyntax(:final loadingUnitIndices) =>
456+
TearoffInstanceSyntax(
457+
:final loadingUnitIndices,
458+
:final definitionIndex,
459+
) =>
455460
ConstructorTearoffReference(
461+
definition: context.definitions[definitionIndex],
456462
loadingUnits: loadingUnitIndices
457463
.map((index) => context.loadingUnits[index])
458464
.toList(),
@@ -567,10 +573,12 @@ final class InstanceConstantReference extends InstanceReference {
567573
}
568574

569575
final class InstanceCreationReference extends InstanceReference {
576+
final Definition definition;
570577
final List<MaybeConstant> positionalArguments;
571578
final Map<String, MaybeConstant> namedArguments;
572579

573580
const InstanceCreationReference({
581+
required this.definition,
574582
required this.positionalArguments,
575583
required this.namedArguments,
576584
required super.loadingUnits,
@@ -581,6 +589,7 @@ final class InstanceCreationReference extends InstanceReference {
581589
final sortedNamedArgs = namedArguments.entries.toList()
582590
..sort((a, b) => a.key.compareTo(b.key));
583591
return InstanceCreationReference(
592+
definition: context.canonicalizeDefinition(definition),
584593
loadingUnits: [
585594
for (final u in loadingUnits) context.canonicalizeLoadingUnit(u),
586595
],
@@ -597,6 +606,7 @@ final class InstanceCreationReference extends InstanceReference {
597606
@override
598607
InstanceReference _filter({String? definitionPackageName}) =>
599608
InstanceCreationReference(
609+
definition: definition,
600610
loadingUnits: loadingUnits,
601611
positionalArguments: [
602612
for (final c in positionalArguments)
@@ -618,6 +628,7 @@ final class InstanceCreationReference extends InstanceReference {
618628
}
619629

620630
return CreationInstanceSyntax(
631+
definitionIndex: context.definitions[definition]!,
621632
loadingUnitIndices: [
622633
for (final unit in loadingUnits) context.loadingUnits[unit]!,
623634
],
@@ -637,13 +648,15 @@ final class InstanceCreationReference extends InstanceReference {
637648
if (!(super == other)) return false;
638649

639650
return other is InstanceCreationReference &&
651+
other.definition == definition &&
640652
deepEquals(other.positionalArguments, positionalArguments) &&
641653
deepEquals(other.namedArguments, namedArguments);
642654
}
643655

644656
@override
645657
int get hashCode => cacheHashCode(
646658
() => Object.hash(
659+
definition,
647660
deepHash(positionalArguments),
648661
deepHash(namedArguments),
649662
super.hashCode,
@@ -660,6 +673,10 @@ final class InstanceCreationReference extends InstanceReference {
660673
bool allowPromotionOfUnsupported = false,
661674
}) {
662675
if (other is! InstanceCreationReference) return false;
676+
// ignore: invalid_use_of_visible_for_testing_member
677+
if (!definition.semanticEquals(other.definition, uriMapping: uriMapping)) {
678+
return false;
679+
}
663680
if (positionalArguments.length != other.positionalArguments.length) {
664681
return false;
665682
}
@@ -699,6 +716,7 @@ final class InstanceCreationReference extends InstanceReference {
699716
@override
700717
String toString() {
701718
final parts = <String>[];
719+
parts.add('definition: $definition');
702720
if (positionalArguments.isNotEmpty) {
703721
parts.add('positional: ${positionalArguments.join(', ')}');
704722
}
@@ -718,11 +736,17 @@ final class InstanceCreationReference extends InstanceReference {
718736
}
719737

720738
final class ConstructorTearoffReference extends InstanceReference {
721-
const ConstructorTearoffReference({required super.loadingUnits});
739+
final Definition definition;
740+
741+
const ConstructorTearoffReference({
742+
required this.definition,
743+
required super.loadingUnits,
744+
});
722745

723746
@override
724747
Reference _canonicalizeChildren(CanonicalizationContext context) =>
725748
ConstructorTearoffReference(
749+
definition: context.canonicalizeDefinition(definition),
726750
loadingUnits: [
727751
for (final u in loadingUnits) context.canonicalizeLoadingUnit(u),
728752
],
@@ -734,11 +758,25 @@ final class ConstructorTearoffReference extends InstanceReference {
734758
@override
735759
TearoffInstanceSyntax _toSyntax(SerializationContext context) =>
736760
TearoffInstanceSyntax(
761+
definitionIndex: context.definitions[definition]!,
737762
loadingUnitIndices: [
738763
for (final unit in loadingUnits) context.loadingUnits[unit]!,
739764
],
740765
);
741766

767+
@override
768+
bool operator ==(Object other) {
769+
if (identical(this, other)) return true;
770+
if (!(super == other)) return false;
771+
772+
return other is ConstructorTearoffReference &&
773+
other.definition == definition;
774+
}
775+
776+
@override
777+
int get hashCode =>
778+
cacheHashCode(() => Object.hash(definition, super.hashCode));
779+
742780
@override
743781
@visibleForTesting
744782
bool semanticEquals(
@@ -749,6 +787,10 @@ final class ConstructorTearoffReference extends InstanceReference {
749787
bool allowPromotionOfUnsupported = false,
750788
}) {
751789
if (other is! ConstructorTearoffReference) return false;
790+
// ignore: invalid_use_of_visible_for_testing_member
791+
if (!definition.semanticEquals(other.definition, uriMapping: uriMapping)) {
792+
return false;
793+
}
752794
return _semanticEqualsShared(
753795
other,
754796
uriMapping: uriMapping,
@@ -759,6 +801,7 @@ final class ConstructorTearoffReference extends InstanceReference {
759801
@override
760802
String toString() {
761803
final parts = <String>[];
804+
parts.add('definition: $definition');
762805
if (loadingUnits.isNotEmpty) {
763806
parts.add('loadingUnits: ${loadingUnits.map((u) => u.name).join(', ')}');
764807
}

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

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,11 +373,13 @@ class CreationInstanceSyntax extends InstanceSyntax {
373373
}) : super._fromJson();
374374

375375
CreationInstanceSyntax({
376+
required int definitionIndex,
376377
required super.loadingUnitIndices,
377378
Map<String, int>? named,
378379
List<int>? positional,
379380
super.path = const [],
380381
}) : super(type: 'creation') {
382+
_definitionIndex = definitionIndex;
381383
_named = named;
382384
_positional = positional;
383385
json.sortOnKey();
@@ -386,14 +388,25 @@ class CreationInstanceSyntax extends InstanceSyntax {
386388
/// Setup all fields for [CreationInstanceSyntax] that are not in
387389
/// [InstanceSyntax].
388390
void setup({
391+
required int definitionIndex,
389392
required Map<String, int>? named,
390393
required List<int>? positional,
391394
}) {
395+
_definitionIndex = definitionIndex;
392396
_named = named;
393397
_positional = positional;
394398
json.sortOnKey();
395399
}
396400

401+
int get definitionIndex => _reader.get<int>('definition_index');
402+
403+
set _definitionIndex(int value) {
404+
json.setOrRemove('definition_index', value);
405+
}
406+
407+
List<String> _validateDefinitionIndex() =>
408+
_reader.validate<int>('definition_index');
409+
397410
Map<String, int>? get named => _reader.optionalMap<int>(
398411
'named',
399412
);
@@ -421,6 +434,7 @@ class CreationInstanceSyntax extends InstanceSyntax {
421434
@override
422435
List<String> validate() => [
423436
...super.validate(),
437+
..._validateDefinitionIndex(),
424438
..._validateNamed(),
425439
..._validatePositional(),
426440
];
@@ -767,8 +781,17 @@ class InstanceSyntax extends JsonObjectSyntax {
767781
...super.validate(),
768782
..._validateLoadingUnitIndices(),
769783
..._validateType(),
784+
..._validateExtraRulesInstance(),
770785
];
771786

787+
List<String> _validateExtraRulesInstance() {
788+
final result = <String>[];
789+
if (_reader.tryTraverse(['type']) == 'creation') {
790+
result.addAll(_reader.validate<Object>('definition_index'));
791+
}
792+
return result;
793+
}
794+
772795
@override
773796
String toString() => 'InstanceSyntax($json)';
774797
}
@@ -1798,13 +1821,34 @@ class TearoffInstanceSyntax extends InstanceSyntax {
17981821
}) : super._fromJson();
17991822

18001823
TearoffInstanceSyntax({
1824+
required int definitionIndex,
18011825
required super.loadingUnitIndices,
18021826
super.path = const [],
1803-
}) : super(type: 'tearoff');
1827+
}) : super(type: 'tearoff') {
1828+
_definitionIndex = definitionIndex;
1829+
json.sortOnKey();
1830+
}
1831+
1832+
/// Setup all fields for [TearoffInstanceSyntax] that are not in
1833+
/// [InstanceSyntax].
1834+
void setup({required int definitionIndex}) {
1835+
_definitionIndex = definitionIndex;
1836+
json.sortOnKey();
1837+
}
1838+
1839+
int get definitionIndex => _reader.get<int>('definition_index');
1840+
1841+
set _definitionIndex(int value) {
1842+
json.setOrRemove('definition_index', value);
1843+
}
1844+
1845+
List<String> _validateDefinitionIndex() =>
1846+
_reader.validate<int>('definition_index');
18041847

18051848
@override
18061849
List<String> validate() => [
18071850
...super.validate(),
1851+
..._validateDefinitionIndex(),
18081852
];
18091853

18101854
@override

‎pkgs/record_use/test/instance_references_test.dart‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ void main() {
1212
[Name('MyClass')],
1313
);
1414

15+
const constructorDefinition = Definition(
16+
'package:test/test.dart',
17+
[Name('MyClass'), Name('', kind: DefinitionKind.constructorKind)],
18+
);
19+
1520
const loadingUnitRoot = LoadingUnit('root');
1621
const loadingUnitOther = LoadingUnit('other');
1722

@@ -26,11 +31,15 @@ void main() {
2631
instances: {
2732
definition: [
2833
const InstanceCreationReference(
34+
definition: constructorDefinition,
2935
positionalArguments: [IntConstant(1), IntConstant(2)],
3036
namedArguments: {'param': StringConstant('named_arg_value')},
3137
loadingUnits: [loadingUnitRoot],
3238
),
33-
const ConstructorTearoffReference(loadingUnits: [loadingUnitOther]),
39+
const ConstructorTearoffReference(
40+
definition: constructorDefinition,
41+
loadingUnits: [loadingUnitOther],
42+
),
3443
const InstanceConstantReference(
3544
instanceConstant: EnumConstant(
3645
definition: definition,
@@ -67,6 +76,7 @@ void main() {
6776
final creation = instances![0];
6877
expect(creation, isA<InstanceCreationReference>());
6978
if (creation is InstanceCreationReference) {
79+
expect(creation.definition, constructorDefinition);
7080
expect(creation.loadingUnits.first.name, loadingUnitRoot.name);
7181
expect(creation.positionalArguments, hasLength(2));
7282
expect(creation.positionalArguments[0], isA<IntConstant>());
@@ -83,6 +93,7 @@ void main() {
8393
final tearoff = instances[1];
8494
expect(tearoff, isA<ConstructorTearoffReference>());
8595
if (tearoff is ConstructorTearoffReference) {
96+
expect(tearoff.definition, constructorDefinition);
8697
expect(tearoff.loadingUnits.first.name, loadingUnitOther.name);
8798
}
8899

0 commit comments

Comments
 (0)