Skip to content

Commit

Permalink
[analyzer] Use TypeImpl for expression types.
Browse files Browse the repository at this point in the history
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
#59763.

Change-Id: I066262462bad32f4715e9a4b78b5d6697480c036
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/405063
Commit-Queue: Paul Berry <[email protected]>
Reviewed-by: Konstantin Shcheglov <[email protected]>
  • Loading branch information
stereotype441 authored and Commit Queue committed Jan 18, 2025
1 parent f61ecec commit b069a13
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 18 deletions.
13 changes: 9 additions & 4 deletions pkg/analyzer/lib/src/dart/ast/ast.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -6093,7 +6094,7 @@ final class ExpressionFunctionBodyImpl extends FunctionBodyImpl

sealed class ExpressionImpl extends AstNodeImpl
implements CollectionElementImpl, Expression {
DartType? _staticType;
TypeImpl? _staticType;

@experimental
@override
Expand Down Expand Up @@ -6148,7 +6149,7 @@ sealed class ExpressionImpl extends AstNodeImpl
}

@override
DartType? get staticType => _staticType;
TypeImpl? get staticType => _staticType;

@override
ExpressionImpl get unParenthesized => this;
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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?;
}
}

Expand Down
12 changes: 11 additions & 1 deletion pkg/analyzer/lib/src/dart/ast/extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion pkg/analyzer/lib/src/dart/constant/evaluation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,7 @@ class ConstantVisitor extends UnifyingAstVisitor<Constant> {
@override
Constant visitConstructorReference(ConstructorReference node) {
var constructorFunctionType = node.typeOrThrow;
if (constructorFunctionType is! FunctionType) {
if (constructorFunctionType is! FunctionTypeImpl) {
return InvalidConstant.forEntity(
node, CompileTimeErrorCode.INVALID_CONSTANT);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
5 changes: 2 additions & 3 deletions pkg/analyzer/lib/src/generated/error_verifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -189,7 +188,7 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
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
Expand Down Expand Up @@ -296,7 +295,7 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
_typeArgumentsVerifier =
TypeArgumentsVerifier(options, _currentLibrary, errorReporter);
_returnTypeVerifier = ReturnTypeVerifier(
typeProvider: _typeProvider as TypeProviderImpl,
typeProvider: _typeProvider,
typeSystem: typeSystem,
errorReporter: errorReporter,
strictCasts: strictCasts,
Expand Down
6 changes: 4 additions & 2 deletions pkg/analyzer/lib/src/generated/resolver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -810,7 +810,9 @@ class ResolverVisitor extends ThrowingAstVisitor<void>
'(${replacementExpression.runtimeType}) $replacementExpression',
);
}
staticType = operations.unknownType.unwrapTypeSchemaView();
// TODO(paulberry): remove this cast by changing the type of
// `operations.unknownType` to `SharedTypeSchemaView<TypeImpl>`.
staticType = operations.unknownType.unwrapTypeSchemaView() as TypeImpl;
}
return ExpressionTypeAnalysisResult<DartType>(
type: SharedTypeView(staticType));
Expand Down Expand Up @@ -1208,7 +1210,7 @@ class ResolverVisitor extends ThrowingAstVisitor<void>
}

var staticType = expression.staticType;
if (staticType is! FunctionType || staticType.typeFormals.isEmpty) {
if (staticType is! FunctionTypeImpl || staticType.typeFormals.isEmpty) {
return expression;
}

Expand Down
4 changes: 1 addition & 3 deletions pkg/analyzer/lib/src/summary2/top_level_inference.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit b069a13

Please sign in to comment.