Skip to content

Commit b069a13

Browse files
stereotype441Commit Queue
authored and
Commit Queue
committed
[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 #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]>
1 parent f61ecec commit b069a13

9 files changed

+33
-18
lines changed

pkg/analyzer/lib/src/dart/ast/ast.dart

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import 'package:analyzer/src/dart/ast/extensions.dart';
2424
import 'package:analyzer/src/dart/ast/to_source_visitor.dart';
2525
import 'package:analyzer/src/dart/element/element.dart';
2626
import 'package:analyzer/src/dart/element/member.dart';
27+
import 'package:analyzer/src/dart/element/type.dart';
2728
import 'package:analyzer/src/dart/element/type_schema.dart';
2829
import 'package:analyzer/src/dart/resolver/body_inference_context.dart';
2930
import 'package:analyzer/src/dart/resolver/typed_literal_resolver.dart';
@@ -6093,7 +6094,7 @@ final class ExpressionFunctionBodyImpl extends FunctionBodyImpl
60936094

60946095
sealed class ExpressionImpl extends AstNodeImpl
60956096
implements CollectionElementImpl, Expression {
6096-
DartType? _staticType;
6097+
TypeImpl? _staticType;
60976098

60986099
@experimental
60996100
@override
@@ -6148,7 +6149,7 @@ sealed class ExpressionImpl extends AstNodeImpl
61486149
}
61496150

61506151
@override
6151-
DartType? get staticType => _staticType;
6152+
TypeImpl? get staticType => _staticType;
61526153

61536154
@override
61546155
ExpressionImpl get unParenthesized => this;
@@ -6221,7 +6222,9 @@ sealed class ExpressionImpl extends AstNodeImpl
62216222
/// @param expression the node whose type is to be recorded
62226223
/// @param type the static type of the node
62236224
void recordStaticType(DartType type, {required ResolverVisitor resolver}) {
6224-
_staticType = type;
6225+
// TODO(paulberry): remove this cast by changing the type of the parameter
6226+
// `type`.
6227+
_staticType = type as TypeImpl;
62256228
if (type.isBottom) {
62266229
resolver.flowAnalysis.flow?.handleExit();
62276230
}
@@ -6253,7 +6256,9 @@ sealed class ExpressionImpl extends AstNodeImpl
62536256
/// static type anyway (e.g. the [SimpleIdentifier] representing the method
62546257
/// name in a method invocation).
62556258
void setPseudoExpressionStaticType(DartType? type) {
6256-
_staticType = type;
6259+
// TODO(paulberry): remove this cast by changing the type of the parameter
6260+
// `type`.
6261+
_staticType = type as TypeImpl?;
62576262
}
62586263
}
62596264

pkg/analyzer/lib/src/dart/ast/extensions.dart

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import 'package:analyzer/dart/element/element.dart';
1010
import 'package:analyzer/dart/element/element2.dart';
1111
import 'package:analyzer/dart/element/type.dart';
1212
import 'package:analyzer/src/dart/ast/ast.dart';
13+
import 'package:analyzer/src/dart/element/type.dart';
1314
import 'package:analyzer/src/utilities/extensions/element.dart';
1415
import 'package:collection/collection.dart';
1516

@@ -172,7 +173,16 @@ extension ExpressionExtension on Expression {
172173
/// This accessor should be used on expressions that are expected to
173174
/// be already resolved. Every such expression must have the type set,
174175
/// at least `dynamic`.
175-
DartType get typeOrThrow {
176+
TypeImpl get typeOrThrow => (this as ExpressionImpl).typeOrThrow;
177+
}
178+
179+
extension ExpressionImplExtension on ExpressionImpl {
180+
/// Return the static type of this expression.
181+
///
182+
/// This accessor should be used on expressions that are expected to
183+
/// be already resolved. Every such expression must have the type set,
184+
/// at least `dynamic`.
185+
TypeImpl get typeOrThrow {
176186
var type = staticType;
177187
if (type == null) {
178188
throw StateError('No type: $this');

pkg/analyzer/lib/src/dart/constant/evaluation.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,7 @@ class ConstantVisitor extends UnifyingAstVisitor<Constant> {
808808
@override
809809
Constant visitConstructorReference(ConstructorReference node) {
810810
var constructorFunctionType = node.typeOrThrow;
811-
if (constructorFunctionType is! FunctionType) {
811+
if (constructorFunctionType is! FunctionTypeImpl) {
812812
return InvalidConstant.forEntity(
813813
node, CompileTimeErrorCode.INVALID_CONSTANT);
814814
}

pkg/analyzer/lib/src/dart/resolver/function_expression_invocation_resolver.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class FunctionExpressionInvocationResolver {
5656
}
5757

5858
receiverType = _typeSystem.resolveToBound(receiverType);
59-
if (receiverType is FunctionType) {
59+
if (receiverType is FunctionTypeImpl) {
6060
_nullableDereferenceVerifier.expression(
6161
CompileTimeErrorCode.UNCHECKED_INVOCATION_OF_NULLABLE_VALUE,
6262
function,

pkg/analyzer/lib/src/dart/resolver/function_reference_resolver.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -896,9 +896,9 @@ class FunctionReferenceResolver {
896896
var receiverType = receiver.staticType;
897897
if (receiverType == null) {
898898
return null;
899-
} else if (receiverType is TypeParameterType) {
899+
} else if (receiverType is TypeParameterTypeImpl) {
900900
return null;
901-
} else if (receiverType is FunctionType) {
901+
} else if (receiverType is FunctionTypeImpl) {
902902
if (name.name == FunctionElement.CALL_METHOD_NAME) {
903903
return receiverType;
904904
}

pkg/analyzer/lib/src/dart/resolver/method_invocation_resolver.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,8 @@ class MethodInvocationResolver with ScopeHelpers {
336336
argumentList: node.argumentList,
337337
contextType: contextType,
338338
whyNotPromotedArguments: whyNotPromotedArguments)
339-
.resolveInvocation(rawType: rawType is FunctionType ? rawType : null);
339+
.resolveInvocation(
340+
rawType: rawType is FunctionTypeImpl ? rawType : null);
340341
node.recordStaticType(staticStaticType, resolver: _resolver);
341342
}
342343

pkg/analyzer/lib/src/generated/error_verifier.dart

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import 'package:analyzer/dart/ast/visitor.dart';
1919
import 'package:analyzer/dart/element/element.dart';
2020
import 'package:analyzer/dart/element/element2.dart';
2121
import 'package:analyzer/dart/element/type.dart';
22-
import 'package:analyzer/dart/element/type_provider.dart';
2322
import 'package:analyzer/diagnostic/diagnostic.dart';
2423
import 'package:analyzer/error/error.dart';
2524
import 'package:analyzer/error/listener.dart';
@@ -189,7 +188,7 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
189188
final AnalysisOptions options;
190189

191190
/// The object providing access to the types defined by the language.
192-
final TypeProvider _typeProvider;
191+
final TypeProviderImpl _typeProvider;
193192

194193
/// The type system primitives
195194
@override
@@ -296,7 +295,7 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
296295
_typeArgumentsVerifier =
297296
TypeArgumentsVerifier(options, _currentLibrary, errorReporter);
298297
_returnTypeVerifier = ReturnTypeVerifier(
299-
typeProvider: _typeProvider as TypeProviderImpl,
298+
typeProvider: _typeProvider,
300299
typeSystem: typeSystem,
301300
errorReporter: errorReporter,
302301
strictCasts: strictCasts,

pkg/analyzer/lib/src/generated/resolver.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -810,7 +810,9 @@ class ResolverVisitor extends ThrowingAstVisitor<void>
810810
'(${replacementExpression.runtimeType}) $replacementExpression',
811811
);
812812
}
813-
staticType = operations.unknownType.unwrapTypeSchemaView();
813+
// TODO(paulberry): remove this cast by changing the type of
814+
// `operations.unknownType` to `SharedTypeSchemaView<TypeImpl>`.
815+
staticType = operations.unknownType.unwrapTypeSchemaView() as TypeImpl;
814816
}
815817
return ExpressionTypeAnalysisResult<DartType>(
816818
type: SharedTypeView(staticType));
@@ -1208,7 +1210,7 @@ class ResolverVisitor extends ThrowingAstVisitor<void>
12081210
}
12091211

12101212
var staticType = expression.staticType;
1211-
if (staticType is! FunctionType || staticType.typeFormals.isEmpty) {
1213+
if (staticType is! FunctionTypeImpl || staticType.typeFormals.isEmpty) {
12121214
return expression;
12131215
}
12141216

pkg/analyzer/lib/src/summary2/top_level_inference.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,9 +276,7 @@ class _PropertyInducingElementTypeInference
276276
}
277277

278278
var initializerType = _node.initializer!.typeOrThrow;
279-
// TODO(paulberry): eliminate this cast by changing the return type of
280-
// `typeOrThrow` to `TypeImpl`.
281-
return _refineType(initializerType as TypeImpl);
279+
return _refineType(initializerType);
282280
}
283281

284282
TypeImpl _refineType(TypeImpl type) {

0 commit comments

Comments
 (0)