Skip to content

Commit 31607cc

Browse files
scheglovCommit Queue
authored andcommitted
Elements. Write more ElementImpl as references, not as fragments.
Change-Id: Id41267ca30bb04a79156d106b87bdc08f0566673 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/431781 Commit-Queue: Konstantin Shcheglov <[email protected]> Reviewed-by: Paul Berry <[email protected]>
1 parent b414df2 commit 31607cc

File tree

12 files changed

+130
-86
lines changed

12 files changed

+130
-86
lines changed

pkg/analyzer/lib/src/dart/analysis/driver.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ testFineAfterLibraryAnalyzerHook;
110110
// TODO(scheglov): Clean up the list of implicitly analyzed files.
111111
class AnalysisDriver {
112112
/// The version of data format, should be incremented on every format change.
113-
static const int DATA_VERSION = 461;
113+
static const int DATA_VERSION = 462;
114114

115115
/// The number of exception contexts allowed to write. Once this field is
116116
/// zero, we stop writing any new exception contexts in this process.

pkg/analyzer/lib/src/dart/constant/evaluation.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3026,6 +3026,7 @@ class _InstanceCreationEvaluator {
30263026
///
30273027
/// Returns an [InvalidConstant] if one is found, or `null` otherwise.
30283028
InvalidConstant? _checkFields() {
3029+
var substitution = Substitution.fromInterfaceType(_constructor.returnType);
30293030
var fields = _constructor.declaration.enclosingElement3.fields;
30303031
for (var field in fields) {
30313032
if ((field.isFinal || field.isConst) &&
@@ -3040,7 +3041,7 @@ class _InstanceCreationEvaluator {
30403041
continue;
30413042
}
30423043
// Match the value and the type.
3043-
var fieldType = FieldMember.from(field, _constructor.returnType).type;
3044+
var fieldType = substitution.substituteType(field.type);
30443045
if (!typeSystem.runtimeTypeMatch(fieldValue, fieldType)) {
30453046
var isRuntimeException = hasTypeParameterReference(field.type);
30463047
var errorNode = field.constantInitializer ?? _errorNode;

pkg/analyzer/lib/src/dart/element/element.dart

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7642,9 +7642,6 @@ class MixinElementImpl2 extends InterfaceElementImpl2 implements MixinElement {
76427642
@override
76437643
final MixinFragmentImpl firstFragment;
76447644

7645-
@override
7646-
List<InterfaceTypeImpl> superclassConstraints = [];
7647-
76487645
MixinElementImpl2(this.reference, this.firstFragment) {
76497646
reference.element2 = this;
76507647
firstFragment.augmentedInternal = this;
@@ -7665,6 +7662,11 @@ class MixinElementImpl2 extends InterfaceElementImpl2 implements MixinElement {
76657662
@override
76667663
bool get isBase => firstFragment.isBase;
76677664

7665+
@override
7666+
List<InterfaceTypeImpl> get superclassConstraints {
7667+
return [for (var fragment in fragments) ...fragment.superclassConstraints];
7668+
}
7669+
76687670
/// Names of methods, getters, setters, and operators that this mixin
76697671
/// declaration super-invokes. For setters this includes the trailing "=".
76707672
/// The list will be empty if this class is not a mixin declaration.

pkg/analyzer/lib/src/dart/element/member.dart

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -615,21 +615,16 @@ class FieldMember extends VariableMember
615615
@override
616616
void visitChildren2<T>(ElementVisitor2<T> visitor) {}
617617

618-
/// If the given [element]'s type is different when any type parameters from the
619-
/// defining type's declaration are replaced with the actual type arguments
620-
/// from the [definingType], create a field member representing the given
621-
/// field. Return the member that was created, or the base field if no member
622-
/// was created.
623-
static FieldElementOrMember from(
624-
FieldFragmentImpl element,
625-
InterfaceType definingType,
618+
static FieldElement2OrMember from(
619+
FieldElementImpl2 element,
620+
MapSubstitution substitution,
626621
) {
627-
if (definingType.typeArguments.isEmpty) {
622+
if (substitution.map.isEmpty) {
628623
return element;
629624
}
630625
return FieldMember(
631-
declaration: element,
632-
substitution: Substitution.fromInterfaceType(definingType),
626+
declaration: element.firstFragment,
627+
substitution: substitution,
633628
);
634629
}
635630

pkg/analyzer/lib/src/summary2/ast_binary_tag.dart

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,16 @@ enum DirectiveUriKind {
1616
withNothing,
1717
}
1818

19-
// TODO(scheglov): remove once we untangle Element2 from Fragment in storage.
20-
enum ElementKind2 { importPrefix, other }
19+
enum ElementTag {
20+
null_,
21+
dynamic_,
22+
never_,
23+
multiplyDefined,
24+
memberWithTypeArguments,
25+
elementImpl,
26+
// TODO(scheglov): remove once we untangle Element2 from Fragment in storage.
27+
viaFragment,
28+
}
2129

2230
enum ImportElementPrefixKind { isDeferred, isNotDeferred, isNull }
2331

pkg/analyzer/lib/src/summary2/augmentation.dart

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -230,16 +230,16 @@ abstract class InstanceElementBuilder<
230230
}
231231

232232
void _addFirstFragment() {
233-
var firstFragment = this.firstFragment;
234-
var element = firstFragment.element;
235-
236-
if (element is MixinElementImpl2) {
237-
if (firstFragment is MixinFragmentImpl) {
238-
element.superclassConstraints.addAll(
239-
firstFragment.superclassConstraints,
240-
);
241-
}
242-
}
233+
// TODO(scheglov): restore eventually
234+
// var firstFragment = this.firstFragment;
235+
// var element = firstFragment.element;
236+
// if (element is MixinElementImpl2) {
237+
// if (firstFragment is MixinFragmentImpl) {
238+
// element.superclassConstraints.addAll(
239+
// firstFragment.superclassConstraints,
240+
// );
241+
// }
242+
// }
243243
}
244244
}
245245

pkg/analyzer/lib/src/summary2/bundle_reader.dart

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1894,9 +1894,6 @@ class MixinElementLinkedData extends ElementLinkedData<MixinFragmentImpl> {
18941894
element.superclassConstraints = reader._readInterfaceTypeList();
18951895
element.interfaces = reader._readInterfaceTypeList();
18961896

1897-
var augmented = element.augmentedInternal;
1898-
augmented.superclassConstraints = reader._readInterfaceTypeList();
1899-
19001897
applyConstantOffsets?.perform();
19011898
}
19021899
}
@@ -1962,13 +1959,37 @@ class ResolutionReader {
19621959
}
19631960

19641961
Element? readElement() {
1965-
var kind = readEnum(ElementKind2.values);
1962+
var kind = readEnum(ElementTag.values);
19661963
switch (kind) {
1967-
case ElementKind2.importPrefix:
1964+
case ElementTag.null_:
1965+
return null;
1966+
case ElementTag.dynamic_:
1967+
return DynamicElementImpl2.instance;
1968+
case ElementTag.never_:
1969+
return NeverElementImpl2.instance;
1970+
case ElementTag.multiplyDefined:
1971+
return null;
1972+
case ElementTag.memberWithTypeArguments:
1973+
var elementImpl = readElement() as ElementImpl2;
1974+
var enclosing = elementImpl.enclosingElement as InstanceElementImpl2;
1975+
1976+
var typeArguments = _readTypeList();
1977+
var substitution = Substitution.fromPairs2(
1978+
enclosing.typeParameters2,
1979+
typeArguments,
1980+
);
1981+
1982+
if (elementImpl is ExecutableElementImpl2) {
1983+
return ExecutableMember.from(elementImpl, substitution);
1984+
} else {
1985+
elementImpl as FieldElementImpl2;
1986+
return FieldMember.from(elementImpl, substitution);
1987+
}
1988+
case ElementTag.elementImpl:
19681989
var referenceIndex = _reader.readUInt30();
19691990
var reference = _referenceReader.referenceOfIndex(referenceIndex);
1970-
return reference.element2 as PrefixElementImpl2;
1971-
case ElementKind2.other:
1991+
return _elementFactory.elementOfReference3(reference);
1992+
case ElementTag.viaFragment:
19721993
// TODO(scheglov): eventually stop using fragments here.
19731994
var fragment = readFragmentOrMember();
19741995
switch (fragment) {

pkg/analyzer/lib/src/summary2/bundle_writer.dart

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -451,9 +451,6 @@ class BundleWriter {
451451
_resolutionSink._writeTypeList(fragment.superclassConstraints);
452452
_resolutionSink._writeTypeList(fragment.interfaces);
453453

454-
var element = fragment.element;
455-
_resolutionSink._writeTypeList(element.superclassConstraints);
456-
457454
_writeList(
458455
fragment.fields.where((e) => !e.isSynthetic).toList(),
459456
_writeFieldElement,
@@ -698,33 +695,46 @@ class ResolutionSink extends _SummaryDataWriter {
698695
required _BundleWriterReferences references,
699696
}) : _references = references;
700697

701-
// TODO(scheglov): Triage places where we write elements.
702-
// Some of then cannot be members, e.g. type names.
703698
void writeElement(Element? element) {
704699
switch (element) {
705-
case PrefixElementImpl2():
706-
writeEnum(ElementKind2.importPrefix);
707-
var reference = element.reference;
700+
case null:
701+
writeEnum(ElementTag.null_);
702+
case DynamicElementImpl2():
703+
writeEnum(ElementTag.dynamic_);
704+
case NeverElementImpl2():
705+
writeEnum(ElementTag.never_);
706+
case MultiplyDefinedElementImpl2():
707+
writeEnum(ElementTag.multiplyDefined);
708+
case Member element:
709+
writeEnum(ElementTag.memberWithTypeArguments);
710+
711+
var baseElement = element.baseElement;
712+
writeElement(baseElement);
713+
714+
var typeArguments = _enclosingClassTypeArguments(
715+
baseElement,
716+
element.substitution.map,
717+
);
718+
_writeTypeList(typeArguments);
719+
// TODO(scheglov): give reference to each element below
720+
case ConstructorElementImpl2():
721+
case FieldElementImpl2():
722+
case FormalParameterElementImpl():
723+
case GetterElementImpl():
724+
case MethodElementImpl2():
725+
case SetterElementImpl():
726+
case TypeParameterElementImpl2():
727+
// TODO(scheglov): eventually stop using fragments here.
728+
writeEnum(ElementTag.viaFragment);
729+
writeByte(Tag.RawElement);
730+
_writeElement(element);
731+
case ElementImpl2():
732+
writeEnum(ElementTag.elementImpl);
733+
var reference = element.reference!;
708734
var referenceIndex = _references._indexOfReference(reference);
709735
writeUInt30(referenceIndex);
710736
default:
711-
// TODO(scheglov): eventually stop using fragments here.
712-
writeEnum(ElementKind2.other);
713-
if (element case Member element) {
714-
var baseElement = element.baseElement;
715-
716-
var typeArguments = _enclosingClassTypeArguments(
717-
baseElement,
718-
element.substitution.map,
719-
);
720-
721-
writeByte(Tag.MemberWithTypeArguments);
722-
_writeElement(baseElement);
723-
_writeTypeList(typeArguments);
724-
} else {
725-
writeByte(Tag.RawElement);
726-
_writeElement(element);
727-
}
737+
throw StateError('${element.runtimeType}');
728738
}
729739
}
730740

pkg/analyzer/lib/src/summary2/library_builder.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,6 @@ class LibraryBuilder {
354354
if (element.superclassConstraints.isEmpty) {
355355
shouldResetClassHierarchies = true;
356356
interfaceFragment.superclassConstraints = [objectType];
357-
element.superclassConstraints = [objectType];
358357
}
359358
}
360359
}

pkg/analyzer/lib/src/summary2/linked_element_factory.dart

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,9 @@ class LinkedElementFactory {
106106
return Namespace(exportedNames);
107107
}
108108

109-
LibraryElementImpl? createLibraryElementForReading(Uri uri) {
109+
LibraryElementImpl createLibraryElementForReading(Uri uri) {
110110
var sourceFactory = analysisContext.sourceFactory;
111-
var librarySource = sourceFactory.forUri2(uri);
112-
113-
// The URI cannot be resolved, we don't know the library.
114-
if (librarySource == null) return null;
111+
var librarySource = sourceFactory.forUri2(uri)!;
115112

116113
var reader = _libraryReaders[uri];
117114
if (reader == null) {
@@ -206,6 +203,31 @@ class LinkedElementFactory {
206203
return elementOfReference(reference)?.asElement2;
207204
}
208205

206+
Element elementOfReference3(Reference reference) {
207+
if (reference.element2 case var element?) {
208+
return element;
209+
}
210+
211+
if (reference.isLibrary) {
212+
var uri = uriCache.parse(reference.name);
213+
return createLibraryElementForReading(uri);
214+
}
215+
216+
var parentRef = reference.parentNotContainer;
217+
var parentElement = elementOfReference(parentRef);
218+
219+
// Only classes delay creating children.
220+
if (parentElement is ClassFragmentImpl) {
221+
parentElement.linkedData?.readMembers(parentElement);
222+
}
223+
224+
var element = reference.element2;
225+
if (element == null) {
226+
throw StateError('Expected existing element: $reference');
227+
}
228+
return element;
229+
}
230+
209231
bool hasLibrary(Uri uri) {
210232
// We already have the element, linked or read.
211233
if (rootReference['$uri']?.element is LibraryElementImpl) {

0 commit comments

Comments
 (0)