Skip to content

Commit 751fe13

Browse files
stereotype441Commit Queue
authored and
Commit Queue
committed
[analyzer] Use TypeImpl for type annotation types.
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]>
1 parent cf09edd commit 751fe13

25 files changed

+132
-85
lines changed

pkg/analysis_server/lib/src/services/correction/dart/create_getter.dart

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import 'package:analyzer/dart/element/element2.dart';
1010
import 'package:analyzer/dart/element/type.dart';
1111
import 'package:analyzer/src/dart/ast/ast.dart';
1212
import 'package:analyzer/src/dart/ast/extensions.dart';
13+
import 'package:analyzer/src/dart/element/type.dart';
1314
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
1415
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
1516
import 'package:meta/meta.dart';
@@ -67,7 +68,7 @@ abstract class CreateFieldOrGetter extends ResolvedCorrectionProducer {
6768
}
6869

6970
var matchedType = objectPattern.type.typeOrThrow;
70-
if (matchedType is! InterfaceType) {
71+
if (matchedType is! InterfaceTypeImpl) {
7172
return;
7273
}
7374

pkg/analysis_server/lib/src/services/correction/dart/move_type_arguments_to_class.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
import 'package:analysis_server/src/services/correction/fix.dart';
66
import 'package:analysis_server_plugin/edit/dart/correction_producer.dart';
77
import 'package:analyzer/dart/ast/ast.dart';
8-
import 'package:analyzer/dart/element/type.dart';
98
import 'package:analyzer/src/dart/ast/extensions.dart';
9+
import 'package:analyzer/src/dart/element/type.dart';
1010
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
1111
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
1212
import 'package:analyzer_plugin/utilities/range_factory.dart';
@@ -41,7 +41,7 @@ class MoveTypeArgumentsToClass extends ResolvedCorrectionProducer {
4141
}
4242

4343
var type = namedType.typeOrThrow;
44-
if (type is InterfaceType) {
44+
if (type is InterfaceTypeImpl) {
4545
var element = type.element3;
4646
if (element.typeParameters2.length == typeArguments.arguments.length) {
4747
await builder.addDartFileEdit(file, (builder) {

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

+7-4
Original file line numberDiff line numberDiff line change
@@ -9022,7 +9022,7 @@ final class GenericFunctionTypeImpl extends TypeAnnotationImpl
90229022
final Token? question;
90239023

90249024
@override
9025-
DartType? type;
9025+
TypeImpl? type;
90269026

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

1254812548
@override
12549-
DartType? type;
12549+
TypeImpl? type;
1255012550

1255112551
/// Initializes a newly created type name.
1255212552
///
@@ -14860,7 +14860,7 @@ final class RecordTypeAnnotationImpl extends TypeAnnotationImpl
1486014860
final Token? question;
1486114861

1486214862
@override
14863-
DartType? type;
14863+
TypeImpl? type;
1486414864

1486514865
RecordTypeAnnotationImpl({
1486614866
required this.leftParenthesis,
@@ -17834,7 +17834,10 @@ sealed class TypeAnnotation implements AstNode {
1783417834
DartType? get type;
1783517835
}
1783617836

17837-
sealed class TypeAnnotationImpl extends AstNodeImpl implements TypeAnnotation {}
17837+
sealed class TypeAnnotationImpl extends AstNodeImpl implements TypeAnnotation {
17838+
@override
17839+
TypeImpl? get type;
17840+
}
1783817841

1783917842
/// A list of type arguments.
1784017843
///

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

+10-1
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,16 @@ extension TypeAnnotationExtension on TypeAnnotation {
376376
/// This accessor should be used on expressions that are expected to
377377
/// be already resolved. Every such expression must have the type set,
378378
/// at least `dynamic`.
379-
DartType get typeOrThrow {
379+
TypeImpl get typeOrThrow => (this as TypeAnnotationImpl).typeOrThrow;
380+
}
381+
382+
extension TypeAnnotationImplExtension on TypeAnnotationImpl {
383+
/// Return the static type of this type annotation.
384+
///
385+
/// This accessor should be used on expressions that are expected to
386+
/// be already resolved. Every such expression must have the type set,
387+
/// at least `dynamic`.
388+
TypeImpl get typeOrThrow {
380389
var type = this.type;
381390
if (type == null) {
382391
throw StateError('No type: $this');

pkg/analyzer/lib/src/dart/element/element.dart

+8-6
Original file line numberDiff line numberDiff line change
@@ -10859,7 +10859,7 @@ class TypeAliasElementImpl extends _ExistingElementImpl
1085910859
ElementLinkedData? linkedData;
1086010860

1086110861
ElementImpl? _aliasedElement;
10862-
DartType? _aliasedType;
10862+
TypeImpl? _aliasedType;
1086310863

1086410864
@override
1086510865
late TypeAliasElementImpl2 element;
@@ -10878,13 +10878,15 @@ class TypeAliasElementImpl extends _ExistingElementImpl
1087810878
}
1087910879

1088010880
@override
10881-
DartType get aliasedType {
10881+
TypeImpl get aliasedType {
1088210882
linkedData?.read(this);
1088310883
return _aliasedType!;
1088410884
}
1088510885

1088610886
set aliasedType(DartType rawType) {
10887-
_aliasedType = rawType;
10887+
// TODO(paulberry): eliminate this cast by changing the type of the
10888+
// `rawType` parameter.
10889+
_aliasedType = rawType as TypeImpl;
1088810890
}
1088910891

1089010892
/// The aliased type, might be `null` if not yet linked.
@@ -10910,7 +10912,7 @@ class TypeAliasElementImpl extends _ExistingElementImpl
1091010912
/// the constructor-tearoffs specification.
1091110913
bool get isProperRename {
1091210914
var aliasedType_ = aliasedType;
10913-
if (aliasedType_ is! InterfaceType) {
10915+
if (aliasedType_ is! InterfaceTypeImpl) {
1091410916
return false;
1091510917
}
1091610918
var aliasedClass = aliasedType_.element;
@@ -11157,7 +11159,7 @@ class TypeAliasElementImpl2 extends TypeDefiningElementImpl2
1115711159
}
1115811160

1115911161
@override
11160-
DartType instantiate(
11162+
TypeImpl instantiate(
1116111163
{required List<DartType> typeArguments,
1116211164
required NullabilitySuffix nullabilitySuffix}) =>
1116311165
firstFragment.instantiate(
@@ -11347,7 +11349,7 @@ class TypeParameterElementImpl extends ElementImpl
1134711349
}
1134811350

1134911351
@override
11350-
TypeParameterType instantiate({
11352+
TypeParameterTypeImpl instantiate({
1135111353
required NullabilitySuffix nullabilitySuffix,
1135211354
}) {
1135311355
return TypeParameterTypeImpl(

pkg/analyzer/lib/src/dart/element/type.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -1536,7 +1536,7 @@ abstract class TypeImpl implements DartType {
15361536
const TypeImpl({this.alias});
15371537

15381538
@override
1539-
DartType get extensionTypeErasure {
1539+
TypeImpl get extensionTypeErasure {
15401540
return const ExtensionTypeErasure().perform(this);
15411541
}
15421542

pkg/analyzer/lib/src/dart/element/type_algebra.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ class FreshTypeParameters {
168168

169169
FreshTypeParameters(this.freshTypeParameters, this.substitution);
170170

171-
FunctionType applyToFunctionType(FunctionType type) {
171+
FunctionTypeImpl applyToFunctionType(FunctionType type) {
172172
return FunctionTypeImpl(
173173
typeFormals: freshTypeParameters.map((e) => e.firstFragment).toList(),
174174
parameters: type.parameters.map((parameter) {

pkg/analyzer/lib/src/dart/element/type_system.dart

+8-6
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,10 @@ import 'package:meta/meta.dart';
4545
class ExtensionTypeErasure extends ReplacementVisitor {
4646
const ExtensionTypeErasure();
4747

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

5254
@override
@@ -700,8 +702,8 @@ class TypeSystemImpl implements TypeSystem {
700702
}
701703

702704
@override
703-
InterfaceType instantiateInterfaceToBounds({
704-
required InterfaceElement element,
705+
InterfaceTypeImpl instantiateInterfaceToBounds({
706+
required covariant InterfaceElementImpl element,
705707
required NullabilitySuffix nullabilitySuffix,
706708
}) {
707709
var typeParameters = element.typeParameters;
@@ -746,8 +748,8 @@ class TypeSystemImpl implements TypeSystem {
746748
}
747749

748750
@override
749-
DartType instantiateTypeAliasToBounds({
750-
required TypeAliasElement element,
751+
TypeImpl instantiateTypeAliasToBounds({
752+
required covariant TypeAliasElementImpl element,
751753
required NullabilitySuffix nullabilitySuffix,
752754
}) {
753755
var typeParameters = element.typeParameters;

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import 'package:analyzer/dart/element/nullability_suffix.dart';
99
import 'package:analyzer/dart/element/type.dart';
1010
import 'package:analyzer/error/listener.dart';
1111
import 'package:analyzer/src/dart/ast/ast.dart';
12+
import 'package:analyzer/src/dart/element/type.dart';
1213
import 'package:analyzer/src/dart/element/type_constraint_gatherer.dart';
1314
import 'package:analyzer/src/dart/element/type_schema.dart';
1415
import 'package:analyzer/src/dart/resolver/invocation_inference_helper.dart';
@@ -139,7 +140,10 @@ class AnnotationResolver {
139140
contextType: UnknownInferredType.instance,
140141
whyNotPromotedArguments: whyNotPromotedArguments,
141142
constructorName: constructorName)
142-
.resolveInvocation(rawType: constructorRawType);
143+
.resolveInvocation(
144+
// TODO(paulberry): eliminate this cast by changing the type of
145+
// `ConstructorElementToInfer.asType`.
146+
rawType: constructorRawType as FunctionTypeImpl);
143147
}
144148

145149
void _extensionGetter(

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import 'package:analyzer/error/listener.dart';
1111
import 'package:analyzer/src/dart/ast/ast.dart';
1212
import 'package:analyzer/src/dart/ast/extensions.dart';
1313
import 'package:analyzer/src/dart/ast/utilities.dart';
14+
import 'package:analyzer/src/dart/element/element.dart';
1415
import 'package:analyzer/src/error/codes.dart';
1516

1617
/// Handles possible rewrites of AST.
@@ -54,7 +55,7 @@ class AstRewriter {
5455
node: node,
5556
function: SimpleIdentifierImpl(typeNode.name2),
5657
);
57-
} else if (element is TypeAliasElement &&
58+
} else if (element is TypeAliasElementImpl &&
5859
element.aliasedElement is GenericFunctionTypeElement) {
5960
return _toMethodInvocationOfAliasedTypeLiteral(
6061
node: node,
@@ -77,7 +78,7 @@ class AstRewriter {
7778
identifier: SimpleIdentifierImpl(typeNode.name2),
7879
),
7980
);
80-
} else if (element is TypeAliasElement &&
81+
} else if (element is TypeAliasElementImpl &&
8182
element.aliasedElement is GenericFunctionTypeElement) {
8283
return _toMethodInvocationOfAliasedTypeLiteral(
8384
node: node,
@@ -635,7 +636,7 @@ class AstRewriter {
635636
MethodInvocation _toMethodInvocationOfAliasedTypeLiteral({
636637
required InstanceCreationExpressionImpl node,
637638
required Identifier function,
638-
required TypeAliasElement element,
639+
required TypeAliasElementImpl element,
639640
}) {
640641
var typeName = NamedTypeImpl(
641642
importPrefix: node.constructorName.type.importPrefix,

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,10 @@ class FunctionExpressionInvocationResolver {
152152
argumentList: node.argumentList,
153153
whyNotPromotedArguments: whyNotPromotedArguments,
154154
contextType: contextType,
155-
).resolveInvocation(rawType: rawType);
155+
).resolveInvocation(
156+
// TODO(paulberry): eliminate this cast by changing the type of
157+
// `rawType`.
158+
rawType: rawType as FunctionTypeImpl);
156159

157160
node.recordStaticType(returnType, resolver: _resolver);
158161
}

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

+9-8
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import 'package:analyzer/dart/element/type.dart';
1111
import 'package:analyzer/error/listener.dart';
1212
import 'package:analyzer/src/dart/ast/ast.dart';
1313
import 'package:analyzer/src/dart/ast/extensions.dart';
14+
import 'package:analyzer/src/dart/element/element.dart';
1415
import 'package:analyzer/src/dart/element/inheritance_manager3.dart';
1516
import 'package:analyzer/src/dart/element/type.dart';
1617
import 'package:analyzer/src/dart/resolver/extension_member_resolver.dart';
@@ -318,7 +319,7 @@ class FunctionReferenceResolver {
318319
/// Resolves [node] as a [TypeLiteral] referencing an interface type directly
319320
/// (not through a type alias).
320321
void _resolveDirectTypeLiteral(FunctionReferenceImpl node,
321-
IdentifierImpl name, InterfaceElement element) {
322+
IdentifierImpl name, InterfaceElementImpl element) {
322323
var typeArguments = _checkTypeArguments(
323324
// `node.typeArguments`, coming from the parser, is never null.
324325
node.typeArguments!, name.name, element.typeParameters,
@@ -622,13 +623,13 @@ class FunctionReferenceResolver {
622623
if (node.parent is PropertyAccess) {
623624
_resolveConstructorReference(node);
624625
return;
625-
} else if (element is InterfaceElement) {
626+
} else if (element is InterfaceElementImpl) {
626627
_resolver.analyzeExpression(
627628
node.function, _resolver.operations.unknownType);
628629
_resolver.popRewrite();
629630
_resolveDirectTypeLiteral(node, prefix, element);
630631
return;
631-
} else if (element is TypeAliasElement) {
632+
} else if (element is TypeAliasElementImpl) {
632633
_resolver.analyzeExpression(prefix, _resolver.operations.unknownType);
633634
_resolver.popRewrite();
634635
_resolveTypeAlias(node: node, element: element, typeAlias: prefix);
@@ -743,19 +744,19 @@ class FunctionReferenceResolver {
743744
// `prefix.C<int>.name` is initially represented as a [PropertyAccess]
744745
// with a [FunctionReference] target.
745746
if (node.parent is PropertyAccess) {
746-
if (element is TypeAliasElement &&
747+
if (element is TypeAliasElementImpl &&
747748
element.aliasedType is FunctionType) {
748749
function.staticElement = element;
749750
_resolveTypeAlias(node: node, element: element, typeAlias: function);
750751
} else {
751752
_resolveConstructorReference(node);
752753
}
753754
return;
754-
} else if (element is InterfaceElement) {
755+
} else if (element is InterfaceElementImpl) {
755756
function.staticElement = element;
756757
_resolveDirectTypeLiteral(node, function, element);
757758
return;
758-
} else if (element is TypeAliasElement) {
759+
} else if (element is TypeAliasElementImpl) {
759760
function.staticElement = element;
760761
_resolveTypeAlias(node: node, element: element, typeAlias: function);
761762
return;
@@ -830,7 +831,7 @@ class FunctionReferenceResolver {
830831

831832
void _resolveTypeAlias({
832833
required FunctionReferenceImpl node,
833-
required TypeAliasElement element,
834+
required TypeAliasElementImpl element,
834835
required IdentifierImpl typeAlias,
835836
}) {
836837
var typeArguments = _checkTypeArguments(
@@ -847,7 +848,7 @@ class FunctionReferenceResolver {
847848

848849
void _resolveTypeLiteral({
849850
required FunctionReferenceImpl node,
850-
required DartType instantiatedType,
851+
required TypeImpl instantiatedType,
851852
required IdentifierImpl name,
852853
}) {
853854
// TODO(srawlins): set the static element of [typeName].

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,10 @@ class InstanceCreationExpressionResolver {
6767
argumentList: node.argumentList,
6868
contextType: contextType,
6969
whyNotPromotedArguments: whyNotPromotedArguments)
70-
.resolveInvocation(rawType: elementToInfer?.asType);
70+
.resolveInvocation(
71+
// TODO(paulberry): eliminate this cast by changing the type of
72+
// `ConstructorElementToInfer.asType`.
73+
rawType: elementToInfer?.asType as FunctionTypeImpl?);
7174
node.recordStaticType(node.constructorName.type.type!, resolver: _resolver);
7275
_resolver.checkForArgumentTypesNotAssignableInList(
7376
node.argumentList, whyNotPromotedArguments);

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,10 @@ class InvocationInferenceHelper {
164164
argumentList: node.argumentList,
165165
contextType: contextType,
166166
whyNotPromotedArguments: whyNotPromotedArguments,
167-
).resolveInvocation(rawType: rawType);
167+
).resolveInvocation(
168+
// TODO(paulberry): eliminate this cast by changing the type of
169+
// `rawType`.
170+
rawType: rawType as FunctionTypeImpl);
168171

169172
node.recordStaticType(returnType, resolver: _resolver);
170173
}

0 commit comments

Comments
 (0)