Skip to content

Commit

Permalink
[analyzer] Change type returned by TypeImpl.asInstanceOf.
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
stereotype441 authored and Commit Queue committed Jan 15, 2025
1 parent 2556d01 commit 99247f1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 22 deletions.
8 changes: 5 additions & 3 deletions pkg/analyzer/lib/src/dart/element/element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11142,7 +11142,7 @@ class TypeParameterElementImpl extends ElementImpl

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

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

@override
DartType? get bound {
TypeImpl? get bound {
return _bound;
}

set bound(DartType? bound) {
_bound = bound;
// TODO(paulberry): Change the type of the parameter `bound` so that this
// cast isn't needed.
_bound = bound as TypeImpl?;
if (_element case var element?) {
if (!identical(element.bound, bound)) {
element.bound = bound;
Expand Down
43 changes: 24 additions & 19 deletions pkg/analyzer/lib/src/dart/element/type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ class FunctionTypeImpl extends TypeImpl
var elementImpl = element as TypeParameterElementImpl;
assert(!parameters.contains(elementImpl));

var bound = elementImpl.bound as TypeImpl?;
var bound = elementImpl.bound;
if (bound != null && bound.referencesAny(parameters)) {
return true;
}
Expand All @@ -361,7 +361,7 @@ class FunctionTypeImpl extends TypeImpl
var elementImpl = element as TypeParameterElementImpl;
assert(!parameters.contains(elementImpl.asElement2));

var bound = elementImpl.bound as TypeImpl?;
var bound = elementImpl.bound;
if (bound != null && bound.referencesAny2(parameters)) {
return true;
}
Expand Down Expand Up @@ -928,23 +928,23 @@ class InterfaceTypeImpl extends TypeImpl implements InterfaceType {
}

@override
InterfaceType? asInstanceOf(InterfaceElement targetElement) {
InterfaceTypeImpl? asInstanceOf(InterfaceElement targetElement) {
if (element == targetElement) {
return this;
}

for (var rawInterface in element.allSupertypes) {
if (rawInterface.element == targetElement) {
var substitution = Substitution.fromInterfaceType(this);
return substitution.substituteType(rawInterface) as InterfaceType;
return substitution.substituteType(rawInterface) as InterfaceTypeImpl;
}
}

return null;
}

@override
InterfaceType? asInstanceOf2(InterfaceElement2 targetElement) {
InterfaceTypeImpl? asInstanceOf2(InterfaceElement2 targetElement) {
if ((element as InterfaceFragment).element == targetElement) {
return this;
}
Expand All @@ -953,7 +953,7 @@ class InterfaceTypeImpl extends TypeImpl implements InterfaceType {
var realElement = (rawInterface.element as InterfaceFragment).element;
if (realElement == targetElement) {
var substitution = Substitution.fromInterfaceType(this);
return substitution.substituteType(rawInterface) as InterfaceType;
return substitution.substituteType(rawInterface) as InterfaceTypeImpl;
}
}

Expand Down Expand Up @@ -1629,10 +1629,10 @@ abstract class TypeImpl implements DartType {
void appendTo(ElementDisplayStringBuilder builder);

@override
InterfaceType? asInstanceOf(InterfaceElement targetElement) => null;
InterfaceTypeImpl? asInstanceOf(InterfaceElement targetElement) => null;

@override
InterfaceType? asInstanceOf2(InterfaceElement2 targetElement) => null;
InterfaceTypeImpl? asInstanceOf2(InterfaceElement2 targetElement) => null;

@override
String getDisplayString({
Expand Down Expand Up @@ -1696,7 +1696,7 @@ abstract class TypeImpl implements DartType {
/// A concrete implementation of a [TypeParameterType].
class TypeParameterTypeImpl extends TypeImpl implements TypeParameterType {
@override
final TypeParameterElement element;
final TypeParameterElementImpl element;

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

/// Initialize a newly created type parameter type to be declared by the given
/// [element] and to have the given name.
TypeParameterTypeImpl({
required this.element,
required TypeParameterElement element,
required this.nullabilitySuffix,
this.promotedBound,
DartType? promotedBound,
super.alias,
});
}) :
// TODO(paulberry): change the type of the parameter `element` so
// that this cast isn't needed.
element = element as TypeParameterElementImpl,
// TODO(paulberry): change the type of the parameter `promotedBound` so
// that this cast isn't needed.
promotedBound = promotedBound as TypeImpl?;

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

@override
DartType get bound =>
TypeImpl get bound =>
promotedBound ?? element.bound ?? DynamicTypeImpl.instance;

@override
ElementLocation get definition => element.location!;
ElementLocation get definition => element.location;

@override
TypeParameterElementImpl2 get element3 =>
(element as TypeParameterElementImpl).element;
TypeParameterElementImpl2 get element3 => element.element;

@override
int get hashCode => element.hashCode;
Expand Down Expand Up @@ -1814,12 +1819,12 @@ class TypeParameterTypeImpl extends TypeImpl implements TypeParameterType {
}

@override
InterfaceType? asInstanceOf(InterfaceElement targetElement) {
InterfaceTypeImpl? asInstanceOf(InterfaceElement targetElement) {
return bound.asInstanceOf(targetElement);
}

@override
InterfaceType? asInstanceOf2(InterfaceElement2 targetElement) {
InterfaceTypeImpl? asInstanceOf2(InterfaceElement2 targetElement) {
return bound.asInstanceOf2(targetElement);
}

Expand Down

0 comments on commit 99247f1

Please sign in to comment.