Skip to content

Commit

Permalink
[analyzer] Use TypeImpl for type annotation types.
Browse files Browse the repository at this point in the history
The types of `TypeAnnotationImpl.type`, and the fields that override
it, are changed from `DartType?` to `TypeImpl?`. Also, the type of
`TypeAnnotationExtension.typeOrThrow` is changed from `DartType` to
`TypeImpl`.

To reduce the number of casts that need to be added, the following
changes are made in parallel:

- An additional extension `TypeAnnotationImplExtension.typeOrThrow` is
  added; this has the same behavior as
  `TypeAnnotationExtension.typeOrThrow`, but it doesn't require a type
  cast.

- Some field types, getter types, method return types, and method
  parameter types are changed to `Impl` types in the following
  classes:
  - `AstRewriter`
  - `EraseNonJSInteropTypes`
  - `ExtensionTypeErasure`
  - `FreshTypeParameters`
  - `FullInvocationInferrer`
  - `FunctionExpressionInvocationResolver`
  - `FunctionReferenceResolver`
  - `FunctionTypeBuilder`
  - `InstanceCreationInferrer`
  - `InvocationExpressionInferrer`
  - `InvocationInferrer`
  - `NamedTypeBuilder`
  - `NamedTypeResolver`
  - `ResolutionReader`
  - `TypeAliasElementImpl`
  - `TypeAliasElementImpl2`
  - `TypeImpl`
  - `TypeParameterElementImpl`
  - `TypeSystemImpl`

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: I7f753508b53f6744677fd18f66858d70eb974093
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/405221
Commit-Queue: Paul Berry <[email protected]>
Reviewed-by: Konstantin Shcheglov <[email protected]>
  • Loading branch information
stereotype441 authored and Commit Queue committed Jan 21, 2025
1 parent cf09edd commit 751fe13
Show file tree
Hide file tree
Showing 25 changed files with 132 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import 'package:analyzer/dart/element/element2.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:analyzer/src/dart/ast/ast.dart';
import 'package:analyzer/src/dart/ast/extensions.dart';
import 'package:analyzer/src/dart/element/type.dart';
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
import 'package:meta/meta.dart';
Expand Down Expand Up @@ -67,7 +68,7 @@ abstract class CreateFieldOrGetter extends ResolvedCorrectionProducer {
}

var matchedType = objectPattern.type.typeOrThrow;
if (matchedType is! InterfaceType) {
if (matchedType is! InterfaceTypeImpl) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import 'package:analysis_server/src/services/correction/fix.dart';
import 'package:analysis_server_plugin/edit/dart/correction_producer.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:analyzer/src/dart/ast/extensions.dart';
import 'package:analyzer/src/dart/element/type.dart';
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
import 'package:analyzer_plugin/utilities/range_factory.dart';
Expand Down Expand Up @@ -41,7 +41,7 @@ class MoveTypeArgumentsToClass extends ResolvedCorrectionProducer {
}

var type = namedType.typeOrThrow;
if (type is InterfaceType) {
if (type is InterfaceTypeImpl) {
var element = type.element3;
if (element.typeParameters2.length == typeArguments.arguments.length) {
await builder.addDartFileEdit(file, (builder) {
Expand Down
11 changes: 7 additions & 4 deletions pkg/analyzer/lib/src/dart/ast/ast.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9022,7 +9022,7 @@ final class GenericFunctionTypeImpl extends TypeAnnotationImpl
final Token? question;

@override
DartType? type;
TypeImpl? type;

/// The element associated with the function type, or `null` if the AST
/// structure hasn't been resolved.
Expand Down Expand Up @@ -12546,7 +12546,7 @@ final class NamedTypeImpl extends TypeAnnotationImpl implements NamedType {
final Token? question;

@override
DartType? type;
TypeImpl? type;

/// Initializes a newly created type name.
///
Expand Down Expand Up @@ -14860,7 +14860,7 @@ final class RecordTypeAnnotationImpl extends TypeAnnotationImpl
final Token? question;

@override
DartType? type;
TypeImpl? type;

RecordTypeAnnotationImpl({
required this.leftParenthesis,
Expand Down Expand Up @@ -17834,7 +17834,10 @@ sealed class TypeAnnotation implements AstNode {
DartType? get type;
}

sealed class TypeAnnotationImpl extends AstNodeImpl implements TypeAnnotation {}
sealed class TypeAnnotationImpl extends AstNodeImpl implements TypeAnnotation {
@override
TypeImpl? get type;
}

/// A list of type arguments.
///
Expand Down
11 changes: 10 additions & 1 deletion pkg/analyzer/lib/src/dart/ast/extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,16 @@ extension TypeAnnotationExtension on TypeAnnotation {
/// This accessor should be used on expressions that are expected to
/// be already resolved. Every such expression must have the type set,
/// at least `dynamic`.
DartType get typeOrThrow {
TypeImpl get typeOrThrow => (this as TypeAnnotationImpl).typeOrThrow;
}

extension TypeAnnotationImplExtension on TypeAnnotationImpl {
/// Return the static type of this type annotation.
///
/// This accessor should be used on expressions that are expected to
/// be already resolved. Every such expression must have the type set,
/// at least `dynamic`.
TypeImpl get typeOrThrow {
var type = this.type;
if (type == null) {
throw StateError('No type: $this');
Expand Down
14 changes: 8 additions & 6 deletions pkg/analyzer/lib/src/dart/element/element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10859,7 +10859,7 @@ class TypeAliasElementImpl extends _ExistingElementImpl
ElementLinkedData? linkedData;

ElementImpl? _aliasedElement;
DartType? _aliasedType;
TypeImpl? _aliasedType;

@override
late TypeAliasElementImpl2 element;
Expand All @@ -10878,13 +10878,15 @@ class TypeAliasElementImpl extends _ExistingElementImpl
}

@override
DartType get aliasedType {
TypeImpl get aliasedType {
linkedData?.read(this);
return _aliasedType!;
}

set aliasedType(DartType rawType) {
_aliasedType = rawType;
// TODO(paulberry): eliminate this cast by changing the type of the
// `rawType` parameter.
_aliasedType = rawType as TypeImpl;
}

/// The aliased type, might be `null` if not yet linked.
Expand All @@ -10910,7 +10912,7 @@ class TypeAliasElementImpl extends _ExistingElementImpl
/// the constructor-tearoffs specification.
bool get isProperRename {
var aliasedType_ = aliasedType;
if (aliasedType_ is! InterfaceType) {
if (aliasedType_ is! InterfaceTypeImpl) {
return false;
}
var aliasedClass = aliasedType_.element;
Expand Down Expand Up @@ -11157,7 +11159,7 @@ class TypeAliasElementImpl2 extends TypeDefiningElementImpl2
}

@override
DartType instantiate(
TypeImpl instantiate(
{required List<DartType> typeArguments,
required NullabilitySuffix nullabilitySuffix}) =>
firstFragment.instantiate(
Expand Down Expand Up @@ -11347,7 +11349,7 @@ class TypeParameterElementImpl extends ElementImpl
}

@override
TypeParameterType instantiate({
TypeParameterTypeImpl instantiate({
required NullabilitySuffix nullabilitySuffix,
}) {
return TypeParameterTypeImpl(
Expand Down
2 changes: 1 addition & 1 deletion pkg/analyzer/lib/src/dart/element/type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1536,7 +1536,7 @@ abstract class TypeImpl implements DartType {
const TypeImpl({this.alias});

@override
DartType get extensionTypeErasure {
TypeImpl get extensionTypeErasure {
return const ExtensionTypeErasure().perform(this);
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/analyzer/lib/src/dart/element/type_algebra.dart
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ class FreshTypeParameters {

FreshTypeParameters(this.freshTypeParameters, this.substitution);

FunctionType applyToFunctionType(FunctionType type) {
FunctionTypeImpl applyToFunctionType(FunctionType type) {
return FunctionTypeImpl(
typeFormals: freshTypeParameters.map((e) => e.firstFragment).toList(),
parameters: type.parameters.map((parameter) {
Expand Down
14 changes: 8 additions & 6 deletions pkg/analyzer/lib/src/dart/element/type_system.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ import 'package:meta/meta.dart';
class ExtensionTypeErasure extends ReplacementVisitor {
const ExtensionTypeErasure();

DartType perform(DartType type) {
return type.accept(this) ?? type;
TypeImpl perform(TypeImpl type) {
// TODO(paulberry): eliminate this cast by changing `ReplacementVisitor` so
// that it implements `TypeVisitor<TypeImpl?>`.
return (type.accept(this) ?? type) as TypeImpl;
}

@override
Expand Down Expand Up @@ -700,8 +702,8 @@ class TypeSystemImpl implements TypeSystem {
}

@override
InterfaceType instantiateInterfaceToBounds({
required InterfaceElement element,
InterfaceTypeImpl instantiateInterfaceToBounds({
required covariant InterfaceElementImpl element,
required NullabilitySuffix nullabilitySuffix,
}) {
var typeParameters = element.typeParameters;
Expand Down Expand Up @@ -746,8 +748,8 @@ class TypeSystemImpl implements TypeSystem {
}

@override
DartType instantiateTypeAliasToBounds({
required TypeAliasElement element,
TypeImpl instantiateTypeAliasToBounds({
required covariant TypeAliasElementImpl element,
required NullabilitySuffix nullabilitySuffix,
}) {
var typeParameters = element.typeParameters;
Expand Down
6 changes: 5 additions & 1 deletion pkg/analyzer/lib/src/dart/resolver/annotation_resolver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:analyzer/dart/element/nullability_suffix.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:analyzer/error/listener.dart';
import 'package:analyzer/src/dart/ast/ast.dart';
import 'package:analyzer/src/dart/element/type.dart';
import 'package:analyzer/src/dart/element/type_constraint_gatherer.dart';
import 'package:analyzer/src/dart/element/type_schema.dart';
import 'package:analyzer/src/dart/resolver/invocation_inference_helper.dart';
Expand Down Expand Up @@ -139,7 +140,10 @@ class AnnotationResolver {
contextType: UnknownInferredType.instance,
whyNotPromotedArguments: whyNotPromotedArguments,
constructorName: constructorName)
.resolveInvocation(rawType: constructorRawType);
.resolveInvocation(
// TODO(paulberry): eliminate this cast by changing the type of
// `ConstructorElementToInfer.asType`.
rawType: constructorRawType as FunctionTypeImpl);
}

void _extensionGetter(
Expand Down
7 changes: 4 additions & 3 deletions pkg/analyzer/lib/src/dart/resolver/ast_rewrite.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'package:analyzer/error/listener.dart';
import 'package:analyzer/src/dart/ast/ast.dart';
import 'package:analyzer/src/dart/ast/extensions.dart';
import 'package:analyzer/src/dart/ast/utilities.dart';
import 'package:analyzer/src/dart/element/element.dart';
import 'package:analyzer/src/error/codes.dart';

/// Handles possible rewrites of AST.
Expand Down Expand Up @@ -54,7 +55,7 @@ class AstRewriter {
node: node,
function: SimpleIdentifierImpl(typeNode.name2),
);
} else if (element is TypeAliasElement &&
} else if (element is TypeAliasElementImpl &&
element.aliasedElement is GenericFunctionTypeElement) {
return _toMethodInvocationOfAliasedTypeLiteral(
node: node,
Expand All @@ -77,7 +78,7 @@ class AstRewriter {
identifier: SimpleIdentifierImpl(typeNode.name2),
),
);
} else if (element is TypeAliasElement &&
} else if (element is TypeAliasElementImpl &&
element.aliasedElement is GenericFunctionTypeElement) {
return _toMethodInvocationOfAliasedTypeLiteral(
node: node,
Expand Down Expand Up @@ -635,7 +636,7 @@ class AstRewriter {
MethodInvocation _toMethodInvocationOfAliasedTypeLiteral({
required InstanceCreationExpressionImpl node,
required Identifier function,
required TypeAliasElement element,
required TypeAliasElementImpl element,
}) {
var typeName = NamedTypeImpl(
importPrefix: node.constructorName.type.importPrefix,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,10 @@ class FunctionExpressionInvocationResolver {
argumentList: node.argumentList,
whyNotPromotedArguments: whyNotPromotedArguments,
contextType: contextType,
).resolveInvocation(rawType: rawType);
).resolveInvocation(
// TODO(paulberry): eliminate this cast by changing the type of
// `rawType`.
rawType: rawType as FunctionTypeImpl);

node.recordStaticType(returnType, resolver: _resolver);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'package:analyzer/dart/element/type.dart';
import 'package:analyzer/error/listener.dart';
import 'package:analyzer/src/dart/ast/ast.dart';
import 'package:analyzer/src/dart/ast/extensions.dart';
import 'package:analyzer/src/dart/element/element.dart';
import 'package:analyzer/src/dart/element/inheritance_manager3.dart';
import 'package:analyzer/src/dart/element/type.dart';
import 'package:analyzer/src/dart/resolver/extension_member_resolver.dart';
Expand Down Expand Up @@ -318,7 +319,7 @@ class FunctionReferenceResolver {
/// Resolves [node] as a [TypeLiteral] referencing an interface type directly
/// (not through a type alias).
void _resolveDirectTypeLiteral(FunctionReferenceImpl node,
IdentifierImpl name, InterfaceElement element) {
IdentifierImpl name, InterfaceElementImpl element) {
var typeArguments = _checkTypeArguments(
// `node.typeArguments`, coming from the parser, is never null.
node.typeArguments!, name.name, element.typeParameters,
Expand Down Expand Up @@ -622,13 +623,13 @@ class FunctionReferenceResolver {
if (node.parent is PropertyAccess) {
_resolveConstructorReference(node);
return;
} else if (element is InterfaceElement) {
} else if (element is InterfaceElementImpl) {
_resolver.analyzeExpression(
node.function, _resolver.operations.unknownType);
_resolver.popRewrite();
_resolveDirectTypeLiteral(node, prefix, element);
return;
} else if (element is TypeAliasElement) {
} else if (element is TypeAliasElementImpl) {
_resolver.analyzeExpression(prefix, _resolver.operations.unknownType);
_resolver.popRewrite();
_resolveTypeAlias(node: node, element: element, typeAlias: prefix);
Expand Down Expand Up @@ -743,19 +744,19 @@ class FunctionReferenceResolver {
// `prefix.C<int>.name` is initially represented as a [PropertyAccess]
// with a [FunctionReference] target.
if (node.parent is PropertyAccess) {
if (element is TypeAliasElement &&
if (element is TypeAliasElementImpl &&
element.aliasedType is FunctionType) {
function.staticElement = element;
_resolveTypeAlias(node: node, element: element, typeAlias: function);
} else {
_resolveConstructorReference(node);
}
return;
} else if (element is InterfaceElement) {
} else if (element is InterfaceElementImpl) {
function.staticElement = element;
_resolveDirectTypeLiteral(node, function, element);
return;
} else if (element is TypeAliasElement) {
} else if (element is TypeAliasElementImpl) {
function.staticElement = element;
_resolveTypeAlias(node: node, element: element, typeAlias: function);
return;
Expand Down Expand Up @@ -830,7 +831,7 @@ class FunctionReferenceResolver {

void _resolveTypeAlias({
required FunctionReferenceImpl node,
required TypeAliasElement element,
required TypeAliasElementImpl element,
required IdentifierImpl typeAlias,
}) {
var typeArguments = _checkTypeArguments(
Expand All @@ -847,7 +848,7 @@ class FunctionReferenceResolver {

void _resolveTypeLiteral({
required FunctionReferenceImpl node,
required DartType instantiatedType,
required TypeImpl instantiatedType,
required IdentifierImpl name,
}) {
// TODO(srawlins): set the static element of [typeName].
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ class InstanceCreationExpressionResolver {
argumentList: node.argumentList,
contextType: contextType,
whyNotPromotedArguments: whyNotPromotedArguments)
.resolveInvocation(rawType: elementToInfer?.asType);
.resolveInvocation(
// TODO(paulberry): eliminate this cast by changing the type of
// `ConstructorElementToInfer.asType`.
rawType: elementToInfer?.asType as FunctionTypeImpl?);
node.recordStaticType(node.constructorName.type.type!, resolver: _resolver);
_resolver.checkForArgumentTypesNotAssignableInList(
node.argumentList, whyNotPromotedArguments);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,10 @@ class InvocationInferenceHelper {
argumentList: node.argumentList,
contextType: contextType,
whyNotPromotedArguments: whyNotPromotedArguments,
).resolveInvocation(rawType: rawType);
).resolveInvocation(
// TODO(paulberry): eliminate this cast by changing the type of
// `rawType`.
rawType: rawType as FunctionTypeImpl);

node.recordStaticType(returnType, resolver: _resolver);
}
Expand Down
Loading

0 comments on commit 751fe13

Please sign in to comment.