From b069a139164339a6c78e872e2f454daf81211a25 Mon Sep 17 00:00:00 2001 From: Paul Berry Date: Sat, 18 Jan 2025 08:37:49 -0800 Subject: [PATCH] [analyzer] Use `TypeImpl` for expression types. The type of `ExpressionImpl.staticType` is changed from `DartType?` to `TypeImpl?`, and the type of `ExpressionExtension.typeOrThrow` is changed from `DartType` to `TypeImpl`. To reduce the amount of casting required by these changes, a few other changes are included: - An additional extension `ExpressionImplExtension.typeOrThrow` is added; this has the same behavior as `ExpressionExtension.typeOrThrow`, but it doesn't require a type cast. - The type of `ErrorVerifier._typeProvider` is changed from `TypeProvider` to `TypeProviderImpl`. This allows calling `TypeProviderImpl` methods that are known to return `TypeImpl`. 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 https://github.com/dart-lang/sdk/issues/59763. Change-Id: I066262462bad32f4715e9a4b78b5d6697480c036 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/405063 Commit-Queue: Paul Berry Reviewed-by: Konstantin Shcheglov --- pkg/analyzer/lib/src/dart/ast/ast.dart | 13 +++++++++---- pkg/analyzer/lib/src/dart/ast/extensions.dart | 12 +++++++++++- pkg/analyzer/lib/src/dart/constant/evaluation.dart | 2 +- .../function_expression_invocation_resolver.dart | 2 +- .../dart/resolver/function_reference_resolver.dart | 4 ++-- .../dart/resolver/method_invocation_resolver.dart | 3 ++- pkg/analyzer/lib/src/generated/error_verifier.dart | 5 ++--- pkg/analyzer/lib/src/generated/resolver.dart | 6 ++++-- .../lib/src/summary2/top_level_inference.dart | 4 +--- 9 files changed, 33 insertions(+), 18 deletions(-) diff --git a/pkg/analyzer/lib/src/dart/ast/ast.dart b/pkg/analyzer/lib/src/dart/ast/ast.dart index 949dae529ca3..214b6587980d 100644 --- a/pkg/analyzer/lib/src/dart/ast/ast.dart +++ b/pkg/analyzer/lib/src/dart/ast/ast.dart @@ -24,6 +24,7 @@ import 'package:analyzer/src/dart/ast/extensions.dart'; import 'package:analyzer/src/dart/ast/to_source_visitor.dart'; import 'package:analyzer/src/dart/element/element.dart'; import 'package:analyzer/src/dart/element/member.dart'; +import 'package:analyzer/src/dart/element/type.dart'; import 'package:analyzer/src/dart/element/type_schema.dart'; import 'package:analyzer/src/dart/resolver/body_inference_context.dart'; import 'package:analyzer/src/dart/resolver/typed_literal_resolver.dart'; @@ -6093,7 +6094,7 @@ final class ExpressionFunctionBodyImpl extends FunctionBodyImpl sealed class ExpressionImpl extends AstNodeImpl implements CollectionElementImpl, Expression { - DartType? _staticType; + TypeImpl? _staticType; @experimental @override @@ -6148,7 +6149,7 @@ sealed class ExpressionImpl extends AstNodeImpl } @override - DartType? get staticType => _staticType; + TypeImpl? get staticType => _staticType; @override ExpressionImpl get unParenthesized => this; @@ -6221,7 +6222,9 @@ sealed class ExpressionImpl extends AstNodeImpl /// @param expression the node whose type is to be recorded /// @param type the static type of the node void recordStaticType(DartType type, {required ResolverVisitor resolver}) { - _staticType = type; + // TODO(paulberry): remove this cast by changing the type of the parameter + // `type`. + _staticType = type as TypeImpl; if (type.isBottom) { resolver.flowAnalysis.flow?.handleExit(); } @@ -6253,7 +6256,9 @@ sealed class ExpressionImpl extends AstNodeImpl /// static type anyway (e.g. the [SimpleIdentifier] representing the method /// name in a method invocation). void setPseudoExpressionStaticType(DartType? type) { - _staticType = type; + // TODO(paulberry): remove this cast by changing the type of the parameter + // `type`. + _staticType = type as TypeImpl?; } } diff --git a/pkg/analyzer/lib/src/dart/ast/extensions.dart b/pkg/analyzer/lib/src/dart/ast/extensions.dart index 11ea40e5ecce..254d8aa48fbe 100644 --- a/pkg/analyzer/lib/src/dart/ast/extensions.dart +++ b/pkg/analyzer/lib/src/dart/ast/extensions.dart @@ -10,6 +10,7 @@ import 'package:analyzer/dart/element/element.dart'; 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/element/type.dart'; import 'package:analyzer/src/utilities/extensions/element.dart'; import 'package:collection/collection.dart'; @@ -172,7 +173,16 @@ extension ExpressionExtension on Expression { /// 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 ExpressionImpl).typeOrThrow; +} + +extension ExpressionImplExtension on ExpressionImpl { + /// Return the static type of this expression. + /// + /// 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 = staticType; if (type == null) { throw StateError('No type: $this'); diff --git a/pkg/analyzer/lib/src/dart/constant/evaluation.dart b/pkg/analyzer/lib/src/dart/constant/evaluation.dart index 5ec4d1c21098..9d01b872050d 100644 --- a/pkg/analyzer/lib/src/dart/constant/evaluation.dart +++ b/pkg/analyzer/lib/src/dart/constant/evaluation.dart @@ -808,7 +808,7 @@ class ConstantVisitor extends UnifyingAstVisitor { @override Constant visitConstructorReference(ConstructorReference node) { var constructorFunctionType = node.typeOrThrow; - if (constructorFunctionType is! FunctionType) { + if (constructorFunctionType is! FunctionTypeImpl) { return InvalidConstant.forEntity( node, CompileTimeErrorCode.INVALID_CONSTANT); } diff --git a/pkg/analyzer/lib/src/dart/resolver/function_expression_invocation_resolver.dart b/pkg/analyzer/lib/src/dart/resolver/function_expression_invocation_resolver.dart index 9ea594f8e485..d6cfe9049cd5 100644 --- a/pkg/analyzer/lib/src/dart/resolver/function_expression_invocation_resolver.dart +++ b/pkg/analyzer/lib/src/dart/resolver/function_expression_invocation_resolver.dart @@ -56,7 +56,7 @@ class FunctionExpressionInvocationResolver { } receiverType = _typeSystem.resolveToBound(receiverType); - if (receiverType is FunctionType) { + if (receiverType is FunctionTypeImpl) { _nullableDereferenceVerifier.expression( CompileTimeErrorCode.UNCHECKED_INVOCATION_OF_NULLABLE_VALUE, function, diff --git a/pkg/analyzer/lib/src/dart/resolver/function_reference_resolver.dart b/pkg/analyzer/lib/src/dart/resolver/function_reference_resolver.dart index 5b54dbbcb4c9..130d91f7b51c 100644 --- a/pkg/analyzer/lib/src/dart/resolver/function_reference_resolver.dart +++ b/pkg/analyzer/lib/src/dart/resolver/function_reference_resolver.dart @@ -896,9 +896,9 @@ class FunctionReferenceResolver { var receiverType = receiver.staticType; if (receiverType == null) { return null; - } else if (receiverType is TypeParameterType) { + } else if (receiverType is TypeParameterTypeImpl) { return null; - } else if (receiverType is FunctionType) { + } else if (receiverType is FunctionTypeImpl) { if (name.name == FunctionElement.CALL_METHOD_NAME) { return receiverType; } diff --git a/pkg/analyzer/lib/src/dart/resolver/method_invocation_resolver.dart b/pkg/analyzer/lib/src/dart/resolver/method_invocation_resolver.dart index e0108f70dbbb..23c5713fd77d 100644 --- a/pkg/analyzer/lib/src/dart/resolver/method_invocation_resolver.dart +++ b/pkg/analyzer/lib/src/dart/resolver/method_invocation_resolver.dart @@ -336,7 +336,8 @@ class MethodInvocationResolver with ScopeHelpers { argumentList: node.argumentList, contextType: contextType, whyNotPromotedArguments: whyNotPromotedArguments) - .resolveInvocation(rawType: rawType is FunctionType ? rawType : null); + .resolveInvocation( + rawType: rawType is FunctionTypeImpl ? rawType : null); node.recordStaticType(staticStaticType, resolver: _resolver); } diff --git a/pkg/analyzer/lib/src/generated/error_verifier.dart b/pkg/analyzer/lib/src/generated/error_verifier.dart index 89228fc4331c..995ecd0a7c84 100644 --- a/pkg/analyzer/lib/src/generated/error_verifier.dart +++ b/pkg/analyzer/lib/src/generated/error_verifier.dart @@ -19,7 +19,6 @@ import 'package:analyzer/dart/ast/visitor.dart'; import 'package:analyzer/dart/element/element.dart'; import 'package:analyzer/dart/element/element2.dart'; import 'package:analyzer/dart/element/type.dart'; -import 'package:analyzer/dart/element/type_provider.dart'; import 'package:analyzer/diagnostic/diagnostic.dart'; import 'package:analyzer/error/error.dart'; import 'package:analyzer/error/listener.dart'; @@ -189,7 +188,7 @@ class ErrorVerifier extends RecursiveAstVisitor final AnalysisOptions options; /// The object providing access to the types defined by the language. - final TypeProvider _typeProvider; + final TypeProviderImpl _typeProvider; /// The type system primitives @override @@ -296,7 +295,7 @@ class ErrorVerifier extends RecursiveAstVisitor _typeArgumentsVerifier = TypeArgumentsVerifier(options, _currentLibrary, errorReporter); _returnTypeVerifier = ReturnTypeVerifier( - typeProvider: _typeProvider as TypeProviderImpl, + typeProvider: _typeProvider, typeSystem: typeSystem, errorReporter: errorReporter, strictCasts: strictCasts, diff --git a/pkg/analyzer/lib/src/generated/resolver.dart b/pkg/analyzer/lib/src/generated/resolver.dart index 585aa68369ec..cab8dbcfd85f 100644 --- a/pkg/analyzer/lib/src/generated/resolver.dart +++ b/pkg/analyzer/lib/src/generated/resolver.dart @@ -810,7 +810,9 @@ class ResolverVisitor extends ThrowingAstVisitor '(${replacementExpression.runtimeType}) $replacementExpression', ); } - staticType = operations.unknownType.unwrapTypeSchemaView(); + // TODO(paulberry): remove this cast by changing the type of + // `operations.unknownType` to `SharedTypeSchemaView`. + staticType = operations.unknownType.unwrapTypeSchemaView() as TypeImpl; } return ExpressionTypeAnalysisResult( type: SharedTypeView(staticType)); @@ -1208,7 +1210,7 @@ class ResolverVisitor extends ThrowingAstVisitor } var staticType = expression.staticType; - if (staticType is! FunctionType || staticType.typeFormals.isEmpty) { + if (staticType is! FunctionTypeImpl || staticType.typeFormals.isEmpty) { return expression; } diff --git a/pkg/analyzer/lib/src/summary2/top_level_inference.dart b/pkg/analyzer/lib/src/summary2/top_level_inference.dart index 223055008516..6b83bd6b1b54 100644 --- a/pkg/analyzer/lib/src/summary2/top_level_inference.dart +++ b/pkg/analyzer/lib/src/summary2/top_level_inference.dart @@ -276,9 +276,7 @@ class _PropertyInducingElementTypeInference } var initializerType = _node.initializer!.typeOrThrow; - // TODO(paulberry): eliminate this cast by changing the return type of - // `typeOrThrow` to `TypeImpl`. - return _refineType(initializerType as TypeImpl); + return _refineType(initializerType); } TypeImpl _refineType(TypeImpl type) {