Skip to content

Commit 57a2fb6

Browse files
scheglovCommit Queue
authored and
Commit Queue
committed
Augment. Allow augmentations set 'supertype' and append 'superclassConstraints'.
...and don't append `Object` is there are other constraints. Change-Id: I1640fa6290905e1d133d1e073d9cceb39e23bf91 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/335406 Reviewed-by: Phil Quitslund <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent 1176218 commit 57a2fb6

File tree

7 files changed

+262
-27
lines changed

7 files changed

+262
-27
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ abstract class AugmentedInterfaceElementImpl
232232

233233
@override
234234
// TODO: implement declaration
235-
InterfaceElement get declaration => throw UnimplementedError();
235+
InterfaceElementImpl get declaration => throw UnimplementedError();
236236

237237
@override
238238
// TODO: implement unnamedConstructor

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,12 @@ abstract class Substitution {
160160

161161
DartType? getSubstitute(TypeParameterElement parameter, bool upperBound);
162162

163+
InterfaceType mapInterfaceType(InterfaceType type) {
164+
return substituteType(type) as InterfaceType;
165+
}
166+
163167
Iterable<InterfaceType> mapInterfaceTypes(Iterable<InterfaceType> types) {
164-
return types.map(substituteType).whereType();
168+
return types.map(mapInterfaceType);
165169
}
166170

167171
DartType substituteType(DartType type, {bool contravariant = false}) {

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,35 @@ class LibraryBuilder {
642642
}
643643
}
644644

645+
void setDefaultSupertypes() {
646+
var shouldResetClassHierarchies = false;
647+
final objectType = element.typeProvider.objectType;
648+
for (final interface in element.topLevelElements) {
649+
switch (interface) {
650+
case ClassElementImpl():
651+
if (interface.isAugmentation) continue;
652+
if (interface.isDartCoreObject) continue;
653+
if (interface.supertype == null) {
654+
shouldResetClassHierarchies = true;
655+
interface.supertype = objectType;
656+
}
657+
case MixinElementImpl():
658+
if (interface.isAugmentation) continue;
659+
final augmented = interface.augmented!;
660+
if (augmented.superclassConstraints.isEmpty) {
661+
shouldResetClassHierarchies = true;
662+
interface.superclassConstraints = [objectType];
663+
if (augmented is AugmentedMixinElementImpl) {
664+
augmented.superclassConstraints = [objectType];
665+
}
666+
}
667+
}
668+
}
669+
if (shouldResetClassHierarchies) {
670+
element.session.classHierarchy.removeOfLibraries({uri});
671+
}
672+
}
673+
645674
void storeExportScope() {
646675
element.exportedReferences = exportScope.toReferences();
647676

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ class Linker {
161161

162162
_createTypeSystem();
163163
_resolveTypes();
164+
_setDefaultSupertypes();
164165

165166
await performance.runAsync(
166167
'executeMacroDeclarationsPhase',
@@ -374,6 +375,12 @@ class Linker {
374375
TypesBuilder(this).build(nodesToBuildType);
375376
}
376377

378+
void _setDefaultSupertypes() {
379+
for (final library in builders.values) {
380+
library.setDefaultSupertypes();
381+
}
382+
}
383+
377384
void _writeLibraries() {
378385
var bundleWriter = BundleWriter(
379386
elementFactory.dynamicRef,

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

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,9 @@ class TypesBuilder {
132132
var type = extendsClause.superclass.type;
133133
if (type is InterfaceType && _isInterfaceTypeClass(type)) {
134134
element.supertype = type;
135-
} else {
136-
element.supertype = _objectType(element);
137135
}
138-
} else if (element.library.isDartCore && element.name == 'Object') {
136+
} else if (element.isDartCoreObject) {
139137
element.setModifier(Modifier.DART_CORE_OBJECT, true);
140-
} else {
141-
element.supertype = _objectType(element);
142138
}
143139

144140
element.interfaces = _toInterfaceTypeList(
@@ -158,8 +154,6 @@ class TypesBuilder {
158154
var superType = node.superclass.type;
159155
if (superType is InterfaceType && _isInterfaceTypeClass(superType)) {
160156
element.supertype = superType;
161-
} else {
162-
element.supertype = _objectType(element);
163157
}
164158

165159
element.mixins = _toInterfaceTypeList(
@@ -346,9 +340,6 @@ class TypesBuilder {
346340
var constraints = _toInterfaceTypeList(
347341
node.onClause?.superclassConstraints,
348342
);
349-
if (!element.isAugmentation && constraints.isEmpty) {
350-
constraints = [_objectType(element)];
351-
}
352343
element.superclassConstraints = constraints;
353344

354345
element.interfaces = _toInterfaceTypeList(
@@ -449,7 +440,16 @@ class TypesBuilder {
449440
final typeProvider = element.library.typeProvider;
450441

451442
if (element is InterfaceElementImpl &&
452-
augmented is AugmentedInterfaceElementImpl) {
443+
augmented is AugmentedInterfaceElementImpl &&
444+
declaration is InterfaceElementImpl) {
445+
if (declaration.supertype == null) {
446+
final elementSuperType = element.supertype;
447+
if (elementSuperType != null) {
448+
final superType = toDeclaration.mapInterfaceType(elementSuperType);
449+
declaration.supertype = superType;
450+
}
451+
}
452+
453453
augmented.interfaces.addAll(
454454
toDeclaration.mapInterfaceTypes(element.interfaces),
455455
);
@@ -509,10 +509,6 @@ class TypesBuilder {
509509
nullabilitySuffix: NullabilitySuffix.none,
510510
);
511511
}
512-
513-
static InterfaceType _objectType(InterfaceElementImpl element) {
514-
return element.library.typeProvider.objectType;
515-
}
516512
}
517513

518514
/// Performs mixins inference in a [ClassDeclaration].

pkg/analyzer/test/src/summary/element_text.dart

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ String getLibraryText({
4141

4242
class ElementTextConfiguration {
4343
bool Function(Object) filter;
44+
bool withAllSupertypes = false;
4445
bool withAugmentedWithoutAugmentation = false;
4546
bool withCodeRanges = false;
4647
bool withConstantInitializers = true;
@@ -637,21 +638,20 @@ class _ElementWriter {
637638
}
638639

639640
if (e is MixinElementImpl) {
640-
final superclassConstraints = e.superclassConstraints;
641-
if (!e.isAugmentation) {
642-
if (superclassConstraints.isEmpty) {
643-
throw StateError('At least Object is expected.');
644-
}
645-
}
646641
_elementPrinter.writeTypeList(
647642
'superclassConstraints',
648-
superclassConstraints,
643+
e.superclassConstraints,
649644
);
650645
}
651646

652647
_elementPrinter.writeTypeList('mixins', e.mixins);
653648
_elementPrinter.writeTypeList('interfaces', e.interfaces);
654649

650+
if (configuration.withAllSupertypes) {
651+
final sorted = e.allSupertypes.sortedBy((t) => t.element.name);
652+
_elementPrinter.writeTypeList('allSupertypes', sorted);
653+
}
654+
655655
_writeElements('fields', e.fields, _writePropertyInducingElement);
656656

657657
var constructors = e.constructors;

0 commit comments

Comments
 (0)