Skip to content

Commit eafaa11

Browse files
scheglovCommit Queue
authored andcommitted
Elements. Store InterfaceElementImpl2 in InterfaceTypeImpl.
Change-Id: Id7cf5dd51abb331c851b0244e9be0f0db15b73ee Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/404380 Commit-Queue: Konstantin Shcheglov <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent 6aeb1bf commit eafaa11

File tree

4 files changed

+135
-118
lines changed

4 files changed

+135
-118
lines changed

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

Lines changed: 49 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -6091,14 +6091,6 @@ abstract class InterfaceElementImpl extends InstanceElementImpl
60916091
/// of this class have been inferred.
60926092
bool hasBeenInferred = false;
60936093

6094-
/// The non-nullable instance of this element, without alias.
6095-
/// Should be used only when the element has no type parameters.
6096-
InterfaceTypeImpl? _nonNullableInstance;
6097-
6098-
/// The nullable instance of this element, without alias.
6099-
/// Should be used only when the element has no type parameters.
6100-
InterfaceTypeImpl? _nullableInstance;
6101-
61026094
List<ConstructorElementImpl> _constructors = _Sentinel.constructorElement;
61036095

61046096
/// Initialize a newly created class element to have the given [name] at the
@@ -6275,43 +6267,10 @@ abstract class InterfaceElementImpl extends InstanceElementImpl
62756267
required List<DartType> typeArguments,
62766268
required NullabilitySuffix nullabilitySuffix,
62776269
}) {
6278-
assert(typeArguments.length == typeParameters.length);
6279-
6280-
if (typeArguments.isEmpty) {
6281-
switch (nullabilitySuffix) {
6282-
case NullabilitySuffix.none:
6283-
if (_nonNullableInstance case var instance?) {
6284-
return instance;
6285-
}
6286-
case NullabilitySuffix.question:
6287-
if (_nullableInstance case var instance?) {
6288-
return instance;
6289-
}
6290-
case NullabilitySuffix.star:
6291-
// TODO(scheglov): remove together with `star`
6292-
break;
6293-
}
6294-
}
6295-
6296-
var result = InterfaceTypeImpl(
6297-
element: this,
6270+
return element.instantiate(
62986271
typeArguments: typeArguments,
62996272
nullabilitySuffix: nullabilitySuffix,
63006273
);
6301-
6302-
if (typeArguments.isEmpty) {
6303-
switch (nullabilitySuffix) {
6304-
case NullabilitySuffix.none:
6305-
_nonNullableInstance = result;
6306-
case NullabilitySuffix.question:
6307-
_nullableInstance = result;
6308-
case NullabilitySuffix.star:
6309-
// TODO(scheglov): remove together with `star`
6310-
break;
6311-
}
6312-
}
6313-
6314-
return result;
63156274
}
63166275

63176276
@override
@@ -6535,6 +6494,14 @@ abstract class InterfaceElementImpl extends InstanceElementImpl
65356494
abstract class InterfaceElementImpl2 extends InstanceElementImpl2
65366495
with _HasSinceSdkVersionMixin
65376496
implements AugmentedInterfaceElement, InterfaceElement2 {
6497+
/// The non-nullable instance of this element, without alias.
6498+
/// Should be used only when the element has no type parameters.
6499+
InterfaceTypeImpl? _nonNullableInstance;
6500+
6501+
/// The nullable instance of this element, without alias.
6502+
/// Should be used only when the element has no type parameters.
6503+
InterfaceTypeImpl? _nullableInstance;
6504+
65386505
@override
65396506
List<InterfaceType> interfaces = [];
65406507

@@ -6633,12 +6600,48 @@ abstract class InterfaceElementImpl2 extends InstanceElementImpl2
66336600
}
66346601

66356602
@override
6636-
InterfaceType instantiate({
6603+
InterfaceTypeImpl instantiate({
66376604
required List<DartType> typeArguments,
66386605
required NullabilitySuffix nullabilitySuffix,
6639-
}) =>
6640-
firstFragment.instantiate(
6641-
typeArguments: typeArguments, nullabilitySuffix: nullabilitySuffix);
6606+
}) {
6607+
assert(typeArguments.length == typeParameters2.length);
6608+
6609+
if (typeArguments.isEmpty) {
6610+
switch (nullabilitySuffix) {
6611+
case NullabilitySuffix.none:
6612+
if (_nonNullableInstance case var instance?) {
6613+
return instance;
6614+
}
6615+
case NullabilitySuffix.question:
6616+
if (_nullableInstance case var instance?) {
6617+
return instance;
6618+
}
6619+
case NullabilitySuffix.star:
6620+
// TODO(scheglov): remove together with `star`
6621+
break;
6622+
}
6623+
}
6624+
6625+
var result = InterfaceTypeImpl.v2(
6626+
element: this,
6627+
typeArguments: typeArguments,
6628+
nullabilitySuffix: nullabilitySuffix,
6629+
);
6630+
6631+
if (typeArguments.isEmpty) {
6632+
switch (nullabilitySuffix) {
6633+
case NullabilitySuffix.none:
6634+
_nonNullableInstance = result;
6635+
case NullabilitySuffix.question:
6636+
_nullableInstance = result;
6637+
case NullabilitySuffix.star:
6638+
// TODO(scheglov): remove together with `star`
6639+
break;
6640+
}
6641+
}
6642+
6643+
return result;
6644+
}
66426645

66436646
@override
66446647
MethodElement2? lookUpConcreteMethod(

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

Lines changed: 61 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -526,12 +526,12 @@ class FunctionTypeImpl extends TypeImpl
526526
/// A concrete implementation of [DartType] representing types of the form
527527
/// `FutureOr<...>`.
528528
class FutureOrTypeImpl extends InterfaceTypeImpl {
529-
FutureOrTypeImpl(
530-
{required super.element,
531-
required super.typeArgument,
532-
required super.nullabilitySuffix,
533-
super.alias})
534-
: super._futureOr();
529+
FutureOrTypeImpl({
530+
required super.element3,
531+
required super.typeArgument,
532+
required super.nullabilitySuffix,
533+
super.alias,
534+
}) : super._futureOr();
535535

536536
@override
537537
bool get isDartAsyncFutureOr => true;
@@ -543,7 +543,7 @@ class FutureOrTypeImpl extends InterfaceTypeImpl {
543543
if (this.nullabilitySuffix == nullabilitySuffix) return this;
544544

545545
return FutureOrTypeImpl(
546-
element: element,
546+
element3: element3,
547547
typeArgument: typeArgument,
548548
nullabilitySuffix: nullabilitySuffix,
549549
alias: alias,
@@ -570,7 +570,7 @@ class InstantiatedTypeAliasElementImpl implements InstantiatedTypeAliasElement {
570570
/// A concrete implementation of an [InterfaceType].
571571
class InterfaceTypeImpl extends TypeImpl implements InterfaceType {
572572
@override
573-
final InterfaceElementImpl element;
573+
final InterfaceElementImpl2 element3;
574574

575575
@override
576576
final List<DartType> typeArguments;
@@ -587,30 +587,20 @@ class InterfaceTypeImpl extends TypeImpl implements InterfaceType {
587587
/// Cached [MethodElement]s - members or raw elements.
588588
List<MethodElement>? _methods;
589589

590-
factory InterfaceTypeImpl(
591-
{required InterfaceElement element,
592-
required List<DartType> typeArguments,
593-
required NullabilitySuffix nullabilitySuffix,
594-
InstantiatedTypeAliasElement? alias}) {
590+
factory InterfaceTypeImpl({
591+
required InterfaceElement element,
592+
required List<DartType> typeArguments,
593+
required NullabilitySuffix nullabilitySuffix,
594+
InstantiatedTypeAliasElement? alias,
595+
}) {
595596
// TODO(paulberry): avoid this cast by changing the type of `element`
596597
element as InterfaceElementImpl;
597-
if (element.name == 'FutureOr' && element.library.isDartAsync) {
598-
return FutureOrTypeImpl(
599-
element: element,
600-
typeArgument: typeArguments.isNotEmpty
601-
? typeArguments[0]
602-
: InvalidTypeImpl.instance,
603-
nullabilitySuffix: nullabilitySuffix,
604-
alias: alias);
605-
} else if (element.name == 'Null' && element.library.isDartCore) {
606-
return NullTypeImpl(element: element, alias: alias);
607-
} else {
608-
return InterfaceTypeImpl._(
609-
element: element,
610-
typeArguments: typeArguments,
611-
nullabilitySuffix: nullabilitySuffix,
612-
alias: alias);
613-
}
598+
return InterfaceTypeImpl.v2(
599+
element: element.asElement2,
600+
typeArguments: typeArguments,
601+
nullabilitySuffix: nullabilitySuffix,
602+
alias: alias,
603+
);
614604
}
615605

616606
factory InterfaceTypeImpl.v2({
@@ -619,51 +609,53 @@ class InterfaceTypeImpl extends TypeImpl implements InterfaceType {
619609
required NullabilitySuffix nullabilitySuffix,
620610
InstantiatedTypeAliasElement? alias,
621611
}) {
622-
return InterfaceTypeImpl(
623-
element: element.asElement,
624-
typeArguments: typeArguments,
625-
nullabilitySuffix: nullabilitySuffix,
626-
alias: alias,
627-
);
612+
if (element.name3 == 'FutureOr' && element.library2.isDartAsync) {
613+
return FutureOrTypeImpl(
614+
element3: element,
615+
typeArgument: typeArguments.isNotEmpty
616+
? typeArguments[0]
617+
: InvalidTypeImpl.instance,
618+
nullabilitySuffix: nullabilitySuffix,
619+
alias: alias,
620+
);
621+
} else if (element.name3 == 'Null' && element.library2.isDartCore) {
622+
return NullTypeImpl(
623+
element3: element,
624+
alias: alias,
625+
);
626+
} else {
627+
return InterfaceTypeImpl._(
628+
element3: element,
629+
typeArguments: typeArguments,
630+
nullabilitySuffix: nullabilitySuffix,
631+
alias: alias,
632+
);
633+
}
628634
}
629635

630636
InterfaceTypeImpl._({
631-
required this.element,
637+
required this.element3,
632638
required this.typeArguments,
633639
required this.nullabilitySuffix,
634640
required super.alias,
635-
}) {
636-
if (element.augmentationTarget != null) {
637-
throw ArgumentError(
638-
'InterfaceType(s) can only be created for declarations',
639-
);
640-
}
641-
var typeParameters = element.typeParameters;
642-
if (typeArguments.length != typeParameters.length) {
643-
throw ArgumentError(
644-
'[typeParameters.length: ${typeParameters.length}]'
645-
'[typeArguments.length: ${typeArguments.length}]'
646-
'[element: $element]'
647-
'[typeParameters: $typeParameters]'
648-
'[typeArguments: $typeArguments]',
649-
);
650-
}
651-
}
641+
});
652642

653-
InterfaceTypeImpl._futureOr(
654-
{required this.element,
655-
required DartType typeArgument,
656-
required this.nullabilitySuffix,
657-
super.alias})
658-
: typeArguments = [typeArgument] {
659-
assert(element.name == 'FutureOr' && element.library.isDartAsync);
643+
InterfaceTypeImpl._futureOr({
644+
required this.element3,
645+
required DartType typeArgument,
646+
required this.nullabilitySuffix,
647+
super.alias,
648+
}) : typeArguments = [typeArgument] {
649+
assert(element3.name3 == 'FutureOr' && element3.library2.isDartAsync);
660650
assert(this is FutureOrTypeImpl);
661651
}
662652

663-
InterfaceTypeImpl._null({required this.element, super.alias})
664-
: typeArguments = const [],
653+
InterfaceTypeImpl._null({
654+
required this.element3,
655+
super.alias,
656+
}) : typeArguments = const [],
665657
nullabilitySuffix = NullabilitySuffix.none {
666-
assert(element.name == 'Null' && element.library.isDartCore);
658+
assert(element3.name3 == 'Null' && element3.library2.isDartCore);
667659
assert(this is NullTypeImpl);
668660
}
669661

@@ -708,7 +700,7 @@ class InterfaceTypeImpl extends TypeImpl implements InterfaceType {
708700
.toList();
709701

710702
@override
711-
InterfaceElementImpl2 get element3 => element.element;
703+
InterfaceElementImpl get element => element3.asElement;
712704

713705
@override
714706
List<GetterElement> get getters => accessors
@@ -1341,7 +1333,10 @@ class NeverTypeImpl extends TypeImpl implements NeverType {
13411333
/// no type parameters and no nullability suffix.
13421334
class NullTypeImpl extends InterfaceTypeImpl
13431335
implements SharedNullTypeStructure<DartType> {
1344-
NullTypeImpl({required super.element, super.alias}) : super._null();
1336+
NullTypeImpl({
1337+
required super.element3,
1338+
super.alias,
1339+
}) : super._null();
13451340

13461341
@override
13471342
bool get isDartCoreNull => true;

pkg/analyzer/lib/src/test_utilities/mock_sdk_elements.dart

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -943,7 +943,7 @@ class _MockSdkElementsBuilder {
943943
}
944944

945945
void _buildClassElement(ClassElementImpl fragment) {
946-
var element = ClassElementImpl2(Reference.root(), fragment);
946+
var element = fragment.element;
947947
element.mixins = fragment.mixins;
948948
element.interfaces = fragment.interfaces;
949949
element.fields = fragment.fields;
@@ -979,13 +979,14 @@ class _MockSdkElementsBuilder {
979979
List<TypeParameterElementImpl> typeParameters = const [],
980980
required CompilationUnitElementImpl unit,
981981
}) {
982-
var element = ClassElementImpl(name, 0);
983-
element.typeParameters = typeParameters;
984-
element.constructors = <ConstructorElementImpl>[
982+
var fragment = ClassElementImpl(name, 0);
983+
ClassElementImpl2(Reference.root(), fragment);
984+
fragment.typeParameters = typeParameters;
985+
fragment.constructors = <ConstructorElementImpl>[
985986
_constructor(),
986987
];
987-
unit.encloseElement(element);
988-
return element;
988+
unit.encloseElement(fragment);
989+
return fragment;
989990
}
990991

991992
ConstructorElementImpl _constructor({

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,12 @@ extension EnumElementExtension on EnumElement {
272272
}
273273
}
274274

275+
extension EnumElementImplExtension on EnumElementImpl {
276+
EnumElementImpl2 get asElement2 {
277+
return element;
278+
}
279+
}
280+
275281
extension ExecutableElement2Extension on ExecutableElement2 {
276282
ExecutableElement get asElement {
277283
if (this case ExecutableMember member) {
@@ -380,6 +386,18 @@ extension InterfaceElementExtension on InterfaceElement {
380386
}
381387
}
382388

389+
extension InterfaceElementImpl2Extension on InterfaceElementImpl2 {
390+
InterfaceElementImpl get asElement {
391+
return firstFragment;
392+
}
393+
}
394+
395+
extension InterfaceElementImplExtension on InterfaceElementImpl {
396+
InterfaceElementImpl2 get asElement2 {
397+
return element;
398+
}
399+
}
400+
383401
extension LabelElement2Extension on LabelElement2 {
384402
LabelElement get asElement {
385403
return firstFragment as LabelElement;

0 commit comments

Comments
 (0)