Skip to content

Commit 99247f1

Browse files
stereotype441Commit Queue
authored and
Commit Queue
committed
[analyzer] Change type returned by TypeImpl.asInstanceOf.
The return types of `TypeImpl.asInstanceOf` and `TypeImpl.asInstanceOf2` are changed from `InterfaceType?` to `InterfaceTypeImpl?`. To reduce the number of casts that need to be added, the following changes are made in parallel: - The types of `TypeParameterTypeImpl.bound` and `TypeParameterTypeImpl.promotedBound` are changed to `TypeImpl`. - The type of `TypeParameterTypeImpl.element` is changed to `TypeParameterElementImpl`. - The type of `TypeParameterElementImpl.bound` is changed to `TypeImpl`. This allowed a null check and some type casts to be removed from methods in `FunctionTypeImpl` and `TypeParameterTypeImpl`. There is no change to the analyzer public API. This is part of a larger arc of work to change the analyzer's use of the shared code so that the type parameters it supplies are not part of the analyzer public API. See #59763. Change-Id: I65f84e1e27c20fcb320be4af0d131792d7396cce Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/404720 Commit-Queue: Paul Berry <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]>
1 parent 2556d01 commit 99247f1

File tree

2 files changed

+29
-22
lines changed

2 files changed

+29
-22
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11142,7 +11142,7 @@ class TypeParameterElementImpl extends ElementImpl
1114211142

1114311143
/// The type representing the bound associated with this parameter, or `null`
1114411144
/// if this parameter does not have an explicit bound.
11145-
DartType? _bound;
11145+
TypeImpl? _bound;
1114611146

1114711147
/// The value representing the variance modifier keyword, or `null` if
1114811148
/// there is no explicit variance modifier, meaning legacy covariance.
@@ -11162,12 +11162,14 @@ class TypeParameterElementImpl extends ElementImpl
1116211162
}
1116311163

1116411164
@override
11165-
DartType? get bound {
11165+
TypeImpl? get bound {
1116611166
return _bound;
1116711167
}
1116811168

1116911169
set bound(DartType? bound) {
11170-
_bound = bound;
11170+
// TODO(paulberry): Change the type of the parameter `bound` so that this
11171+
// cast isn't needed.
11172+
_bound = bound as TypeImpl?;
1117111173
if (_element case var element?) {
1117211174
if (!identical(element.bound, bound)) {
1117311175
element.bound = bound;

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

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ class FunctionTypeImpl extends TypeImpl
334334
var elementImpl = element as TypeParameterElementImpl;
335335
assert(!parameters.contains(elementImpl));
336336

337-
var bound = elementImpl.bound as TypeImpl?;
337+
var bound = elementImpl.bound;
338338
if (bound != null && bound.referencesAny(parameters)) {
339339
return true;
340340
}
@@ -361,7 +361,7 @@ class FunctionTypeImpl extends TypeImpl
361361
var elementImpl = element as TypeParameterElementImpl;
362362
assert(!parameters.contains(elementImpl.asElement2));
363363

364-
var bound = elementImpl.bound as TypeImpl?;
364+
var bound = elementImpl.bound;
365365
if (bound != null && bound.referencesAny2(parameters)) {
366366
return true;
367367
}
@@ -928,23 +928,23 @@ class InterfaceTypeImpl extends TypeImpl implements InterfaceType {
928928
}
929929

930930
@override
931-
InterfaceType? asInstanceOf(InterfaceElement targetElement) {
931+
InterfaceTypeImpl? asInstanceOf(InterfaceElement targetElement) {
932932
if (element == targetElement) {
933933
return this;
934934
}
935935

936936
for (var rawInterface in element.allSupertypes) {
937937
if (rawInterface.element == targetElement) {
938938
var substitution = Substitution.fromInterfaceType(this);
939-
return substitution.substituteType(rawInterface) as InterfaceType;
939+
return substitution.substituteType(rawInterface) as InterfaceTypeImpl;
940940
}
941941
}
942942

943943
return null;
944944
}
945945

946946
@override
947-
InterfaceType? asInstanceOf2(InterfaceElement2 targetElement) {
947+
InterfaceTypeImpl? asInstanceOf2(InterfaceElement2 targetElement) {
948948
if ((element as InterfaceFragment).element == targetElement) {
949949
return this;
950950
}
@@ -953,7 +953,7 @@ class InterfaceTypeImpl extends TypeImpl implements InterfaceType {
953953
var realElement = (rawInterface.element as InterfaceFragment).element;
954954
if (realElement == targetElement) {
955955
var substitution = Substitution.fromInterfaceType(this);
956-
return substitution.substituteType(rawInterface) as InterfaceType;
956+
return substitution.substituteType(rawInterface) as InterfaceTypeImpl;
957957
}
958958
}
959959

@@ -1629,10 +1629,10 @@ abstract class TypeImpl implements DartType {
16291629
void appendTo(ElementDisplayStringBuilder builder);
16301630

16311631
@override
1632-
InterfaceType? asInstanceOf(InterfaceElement targetElement) => null;
1632+
InterfaceTypeImpl? asInstanceOf(InterfaceElement targetElement) => null;
16331633

16341634
@override
1635-
InterfaceType? asInstanceOf2(InterfaceElement2 targetElement) => null;
1635+
InterfaceTypeImpl? asInstanceOf2(InterfaceElement2 targetElement) => null;
16361636

16371637
@override
16381638
String getDisplayString({
@@ -1696,7 +1696,7 @@ abstract class TypeImpl implements DartType {
16961696
/// A concrete implementation of a [TypeParameterType].
16971697
class TypeParameterTypeImpl extends TypeImpl implements TypeParameterType {
16981698
@override
1699-
final TypeParameterElement element;
1699+
final TypeParameterElementImpl element;
17001700

17011701
@override
17021702
final NullabilitySuffix nullabilitySuffix;
@@ -1705,16 +1705,22 @@ class TypeParameterTypeImpl extends TypeImpl implements TypeParameterType {
17051705
///
17061706
/// 'null' indicates that the type parameter's bound has not been promoted and
17071707
/// is therefore the same as the bound of [element].
1708-
final DartType? promotedBound;
1708+
final TypeImpl? promotedBound;
17091709

17101710
/// Initialize a newly created type parameter type to be declared by the given
17111711
/// [element] and to have the given name.
17121712
TypeParameterTypeImpl({
1713-
required this.element,
1713+
required TypeParameterElement element,
17141714
required this.nullabilitySuffix,
1715-
this.promotedBound,
1715+
DartType? promotedBound,
17161716
super.alias,
1717-
});
1717+
}) :
1718+
// TODO(paulberry): change the type of the parameter `element` so
1719+
// that this cast isn't needed.
1720+
element = element as TypeParameterElementImpl,
1721+
// TODO(paulberry): change the type of the parameter `promotedBound` so
1722+
// that this cast isn't needed.
1723+
promotedBound = promotedBound as TypeImpl?;
17181724

17191725
/// Initialize a newly created type parameter type to be declared by the given
17201726
/// [element] and to have the given name.
@@ -1733,15 +1739,14 @@ class TypeParameterTypeImpl extends TypeImpl implements TypeParameterType {
17331739
}
17341740

17351741
@override
1736-
DartType get bound =>
1742+
TypeImpl get bound =>
17371743
promotedBound ?? element.bound ?? DynamicTypeImpl.instance;
17381744

17391745
@override
1740-
ElementLocation get definition => element.location!;
1746+
ElementLocation get definition => element.location;
17411747

17421748
@override
1743-
TypeParameterElementImpl2 get element3 =>
1744-
(element as TypeParameterElementImpl).element;
1749+
TypeParameterElementImpl2 get element3 => element.element;
17451750

17461751
@override
17471752
int get hashCode => element.hashCode;
@@ -1814,12 +1819,12 @@ class TypeParameterTypeImpl extends TypeImpl implements TypeParameterType {
18141819
}
18151820

18161821
@override
1817-
InterfaceType? asInstanceOf(InterfaceElement targetElement) {
1822+
InterfaceTypeImpl? asInstanceOf(InterfaceElement targetElement) {
18181823
return bound.asInstanceOf(targetElement);
18191824
}
18201825

18211826
@override
1822-
InterfaceType? asInstanceOf2(InterfaceElement2 targetElement) {
1827+
InterfaceTypeImpl? asInstanceOf2(InterfaceElement2 targetElement) {
18231828
return bound.asInstanceOf2(targetElement);
18241829
}
18251830

0 commit comments

Comments
 (0)