Skip to content

Commit

Permalink
[analyzer] Change VariableElementImpl.type setter to TypeImpl.
Browse files Browse the repository at this point in the history
The types of `LocalVariableElementImpl2.type=`,
`PropertyInducingElementImpl.type=`, and `VariableElementImpl.type=`
are all changed to accept `TypeImpl` rather than `DartType`.

To reduce the number of casts that need to be added, some field types,
getter types, method return types, and method parameter types are
changed to `Impl` types in the following classes:

- `DeclarationBuilder`
- `ElementFactory`
- `ElementsTypesMixin`
- `ForResolver`
- `FunctionExpressionResolver`
- `FunctionTypeBuilder`
- `FunctionTypeTest`
- `InstanceMemberInferrer`
- `LocalVariableElementImpl2`
- `NamedTypeBuilder`
- `NonCovariantTypeParameterPositionVisitorTest`
- `ReplacementVisitor`
- `ResolutionReader`
- `ResolutionVisitor`
- `TypeBuilder`
- `TypeReferencesAnyTest`
- `TypesBuilder`
- `_MockSdkElementsBuilder`
- `_Node`

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: I726bb1ce414eec3f360ae655c1f55e6be9e713fe
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/405242
Reviewed-by: Konstantin Shcheglov <[email protected]>
Commit-Queue: Paul Berry <[email protected]>
  • Loading branch information
stereotype441 authored and Commit Queue committed Jan 21, 2025
1 parent 751fe13 commit eba7e49
Show file tree
Hide file tree
Showing 25 changed files with 148 additions and 118 deletions.
16 changes: 8 additions & 8 deletions pkg/analyzer/lib/src/dart/element/element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7844,9 +7844,9 @@ class LocalVariableElementImpl2 extends PromotableElementImpl2
Metadata get metadata2 => wrappedElement.metadata2;

@override
DartType get type => _wrappedElement.type;
TypeImpl get type => _wrappedElement.type;

set type(DartType type) => _wrappedElement.type = type;
set type(TypeImpl type) => _wrappedElement.type = type;

LocalVariableElementImpl get wrappedElement {
return _wrappedElement;
Expand Down Expand Up @@ -9310,7 +9310,9 @@ class ParameterElementImpl extends VariableElementImpl
nameOffset: -1,
parameterKind: parameterKind,
);
element.type = type;
// TODO(paulberry): remove this cast by changing the type of the `type`
// parameter.
element.type = type as TypeImpl;
element.isSynthetic = true;
return element;
}
Expand Down Expand Up @@ -10357,7 +10359,7 @@ abstract class PropertyInducingElementImpl
}

@override
set type(DartType type) {
set type(TypeImpl type) {
super.type = type;
// Reset cached types of synthetic getters and setters.
// TODO(scheglov): Consider not caching these types.
Expand Down Expand Up @@ -11630,10 +11632,8 @@ abstract class VariableElementImpl extends ElementImpl
@override
TypeImpl get type => _type!;

set type(DartType type) {
// TODO(paulberry): eliminate this cast by changing the setter parameter
// type to `TypeImpl`.
_type = type as TypeImpl;
set type(TypeImpl type) {
_type = type;
}

@override
Expand Down
8 changes: 5 additions & 3 deletions pkg/analyzer/lib/src/dart/element/replacement_visitor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class ReplacementVisitor
required FunctionTypeBuilder type,
required List<TypeParameterElement>? newTypeParameters,
required List<ParameterElement>? newParameters,
required DartType? newReturnType,
required TypeImpl? newReturnType,
required NullabilitySuffix? newNullability,
}) {
if (newNullability == null &&
Expand Down Expand Up @@ -311,13 +311,15 @@ class ReplacementVisitor
}
}

DartType? visitType(DartType? type) {
TypeImpl? visitType(DartType? type) {
if (type == null) return null;
var result = type.accept(this);
if (substitution != null) {
result = substitution.substituteType(result ?? type);
}
return result;
// TODO(paulberry): eliminate this cast by changing `ReplacementVisitor`
// to implement `TypeVisitor<TypeImpl?>`.
return result as TypeImpl?;
}

var newReturnType = visitType(node.returnType);
Expand Down
4 changes: 3 additions & 1 deletion pkg/analyzer/lib/src/dart/element/type_algebra.dart
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,9 @@ abstract class _TypeSubstitutor

inner.invertVariance();

var returnType = type.returnType.accept(inner);
// TODO(paulberry): eliminate this cast by changing `_TypeSubstitutor` to
// implement `TypeVisitor<TypeImpl>`.
var returnType = type.returnType.accept(inner) as TypeImpl;

if (useCounter == before) return type;

Expand Down
2 changes: 1 addition & 1 deletion pkg/analyzer/lib/src/dart/resolver/for_resolver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class ForResolver {
/// a type for the elements being iterated over. Inference is based
/// on the type of the iterator or stream over which the foreach loop
/// is defined.
DartType _computeForEachElementType(Expression iterable, bool isAsync) {
TypeImpl _computeForEachElementType(ExpressionImpl iterable, bool isAsync) {
var iterableType = iterable.staticType;
if (iterableType == null) {
return InvalidTypeImpl.instance;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class FunctionExpressionResolver {
return;
}

void inferType(ParameterElementImpl p, DartType inferredType) {
void inferType(ParameterElementImpl p, TypeImpl inferredType) {
// Check that there is no declared type, and that we have not already
// inferred a type in some fashion.
if (p.hasImplicitType && p.type is DynamicType) {
Expand Down Expand Up @@ -108,8 +108,11 @@ class FunctionExpressionResolver {
Iterator<ParameterElement> fnPositional =
contextType.parameters.where((p) => p.isPositional).iterator;
while (positional.moveNext() && fnPositional.moveNext()) {
inferType(positional.current as ParameterElementImpl,
fnPositional.current.type);
inferType(
positional.current as ParameterElementImpl,
// TODO(paulberry): eliminate this cast by changing the type of
// `contextType` to `FunctionTypeImpl`.
fnPositional.current.type as TypeImpl);
}
}

Expand All @@ -121,7 +124,11 @@ class FunctionExpressionResolver {
if (!namedParameterTypes.containsKey(p.name)) {
continue;
}
inferType(p as ParameterElementImpl, namedParameterTypes[p.name]!);
inferType(
p as ParameterElementImpl,
// TODO(paulberry): eliminate this cast by changing the type of
// `contextType` to `FunctionTypeImpl`.
namedParameterTypes[p.name]! as TypeImpl);
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions pkg/analyzer/lib/src/dart/resolver/resolution_visitor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import 'package:analyzer/src/dart/element/element.dart';
import 'package:analyzer/src/dart/element/scope.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_provider.dart';
import 'package:analyzer/src/dart/resolver/ast_rewrite.dart';
import 'package:analyzer/src/dart/resolver/flow_analysis_visitor.dart';
import 'package:analyzer/src/dart/resolver/named_type_resolver.dart';
Expand Down Expand Up @@ -73,7 +74,7 @@ class ElementHolder {
/// 5. Rewrite AST where resolution provides a more accurate understanding.
class ResolutionVisitor extends RecursiveAstVisitor<void> {
final LibraryElementImpl _libraryElement;
final TypeProvider _typeProvider;
final TypeProviderImpl _typeProvider;
final CompilationUnitElementImpl _unitElement;
final ErrorReporter _errorReporter;
final AstRewriter _astRewriter;
Expand Down Expand Up @@ -177,7 +178,7 @@ class ResolutionVisitor extends RecursiveAstVisitor<void> {
this.dataForTesting,
);

DartType get _dynamicType => _typeProvider.dynamicType;
TypeImpl get _dynamicType => _typeProvider.dynamicType;

@override
void visitAnnotation(covariant AnnotationImpl node) {
Expand Down Expand Up @@ -1402,7 +1403,7 @@ class ResolutionVisitor extends RecursiveAstVisitor<void> {
var localElement = node.declaredElement as LocalVariableElementImpl;
element = localElement;

var varList = node.parent as VariableDeclarationList;
var varList = node.parent as VariableDeclarationListImpl;
localElement.hasImplicitType = varList.type == null;
localElement.hasInitializer = initializerNode != null;
localElement.type = varList.type?.type ?? _dynamicType;
Expand Down
20 changes: 10 additions & 10 deletions pkg/analyzer/lib/src/generated/testing/element_factory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ class ElementFactory {

static ConstructorElementImpl constructorElement(
ClassElement definingClass, String? name, bool isConst,
[List<DartType> argumentTypes = const []]) {
[List<TypeImpl> argumentTypes = const []]) {
var offset = name == null ? -1 : 0;
// A constructor declared as `C.new` is unnamed, and is modeled as such.
var constructor = name == null || name == 'new'
Expand All @@ -146,11 +146,11 @@ class ElementFactory {

static ConstructorElementImpl constructorElement2(
ClassElement definingClass, String? name,
[List<DartType> argumentTypes = const []]) =>
[List<TypeImpl> argumentTypes = const []]) =>
constructorElement(definingClass, name, false, argumentTypes);

static FieldElementImpl fieldElement(
String name, bool isStatic, bool isFinal, bool isConst, DartType type,
String name, bool isStatic, bool isFinal, bool isConst, TypeImpl type,
{ExpressionImpl? initializer}) {
FieldElementImpl field =
isConst ? ConstFieldElementImpl(name, 0) : FieldElementImpl(name, 0);
Expand Down Expand Up @@ -184,7 +184,7 @@ class ElementFactory {
}

static PropertyAccessorElementImpl getterElement(
String name, bool isStatic, DartType type) {
String name, bool isStatic, TypeImpl type) {
FieldElementImpl field = FieldElementImpl(name, -1);
field.isStatic = isStatic;
field.isSynthetic = true;
Expand Down Expand Up @@ -227,7 +227,7 @@ class ElementFactory {
LocalVariableElementImpl(name, 0);

static MethodElementImpl methodElement(String methodName, DartType returnType,
[List<DartType> argumentTypes = const []]) {
[List<TypeImpl> argumentTypes = const []]) {
MethodElementImpl method = MethodElementImpl(methodName, 0);
method.parameters = _requiredParameters(argumentTypes);
method.returnType = returnType;
Expand Down Expand Up @@ -276,7 +276,7 @@ class ElementFactory {
);
}

static ParameterElementImpl namedParameter2(String name, DartType type) {
static ParameterElementImpl namedParameter2(String name, TypeImpl type) {
var parameter = ParameterElementImpl(
name: name,
nameOffset: 0,
Expand All @@ -294,7 +294,7 @@ class ElementFactory {
);
}

static ParameterElementImpl positionalParameter2(String name, DartType type) {
static ParameterElementImpl positionalParameter2(String name, TypeImpl type) {
var parameter = ParameterElementImpl(
name: name,
nameOffset: 0,
Expand All @@ -314,7 +314,7 @@ class ElementFactory {
);
}

static ParameterElementImpl requiredParameter2(String name, DartType type) {
static ParameterElementImpl requiredParameter2(String name, TypeImpl type) {
var parameter = ParameterElementImpl(
name: name,
nameOffset: 0,
Expand All @@ -325,7 +325,7 @@ class ElementFactory {
}

static PropertyAccessorElementImpl setterElement(
String name, bool isStatic, DartType type) {
String name, bool isStatic, TypeImpl type) {
FieldElementImpl field = FieldElementImpl(name, -1);
field.isStatic = isStatic;
field.isSynthetic = true;
Expand Down Expand Up @@ -367,7 +367,7 @@ class ElementFactory {
}

static List<ParameterElementImpl> _requiredParameters(
List<DartType> argumentTypes) {
List<TypeImpl> argumentTypes) {
var parameters = argumentTypes.mapIndexed((index, type) {
var parameter = ParameterElementImpl(
name: 'a$index',
Expand Down
2 changes: 1 addition & 1 deletion pkg/analyzer/lib/src/summary2/bundle_reader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2170,7 +2170,7 @@ class ResolutionReader {
}
}

DartType readRequiredType() {
TypeImpl readRequiredType() {
return readType()!;
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/analyzer/lib/src/summary2/extension_type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ class _Node extends graph.Node<_Node> {
_evaluateWithType(type);
}

void _evaluateWithType(DartType type) {
void _evaluateWithType(TypeImpl type) {
var typeSystem = element.library.typeSystem;

element.representation.type = type;
Expand Down
8 changes: 4 additions & 4 deletions pkg/analyzer/lib/src/summary2/function_type_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class FunctionTypeBuilder extends TypeBuilder {

final List<TypeParameterElement> typeFormals;
final List<ParameterElement> parameters;
final DartType returnType;
final TypeImpl returnType;

@override
final NullabilitySuffix nullabilitySuffix;
Expand Down Expand Up @@ -71,7 +71,7 @@ class FunctionTypeBuilder extends TypeBuilder {
}

@override
DartType build() {
TypeImpl build() {
var type = _type;
if (type != null) {
return type;
Expand Down Expand Up @@ -153,7 +153,7 @@ class FunctionTypeBuilder extends TypeBuilder {
}

/// If the [type] is a [TypeBuilder], build it; otherwise return as is.
static DartType _buildType(DartType type) {
static TypeImpl _buildType(TypeImpl type) {
if (type is TypeBuilder) {
return type.build();
} else {
Expand All @@ -162,7 +162,7 @@ class FunctionTypeBuilder extends TypeBuilder {
}

/// Return the type of the [node] as is, possibly a [TypeBuilder].
static DartType _getNodeType(TypeAnnotation? node) {
static TypeImpl _getNodeType(TypeAnnotation? node) {
if (node == null) {
return _dynamicType;
} else {
Expand Down
26 changes: 18 additions & 8 deletions pkg/analyzer/lib/src/summary2/instance_member_inferrer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -127,20 +127,22 @@ class InstanceMemberInferrer {
);
overriddenSetters ??= const [];

DartType combinedGetterType() {
TypeImpl combinedGetterType() {
var combinedGetter = inheritance.combineSignatures(
targetClass: currentInterfaceElement,
candidates: overriddenGetters!,
doTopMerge: true,
name: getterName,
);
if (combinedGetter != null) {
return combinedGetter.returnType;
// TODO(paulberry): eliminate this cast by changing the return type of
// `InheritanceManager3.combineSignatures`.
return combinedGetter.returnType as TypeImpl;
}
return DynamicTypeImpl.instance;
}

DartType combinedSetterType() {
TypeImpl combinedSetterType() {
var combinedSetter = inheritance.combineSignatures(
targetClass: currentInterfaceElement,
candidates: overriddenSetters!,
Expand All @@ -150,7 +152,9 @@ class InstanceMemberInferrer {
if (combinedSetter != null) {
var parameters = combinedSetter.parameters;
if (parameters.isNotEmpty) {
return parameters[0].type;
// TODO(paulberry): eliminate this cast by changing the return type of
// `InheritanceManager3.combineSignatures`.
return parameters[0].type as TypeImpl;
}
}
return DynamicTypeImpl.instance;
Expand Down Expand Up @@ -373,12 +377,16 @@ class InstanceMemberInferrer {
if (parameter is FieldFormalParameterElementImpl) {
var field = parameter.field;
if (field != null) {
parameter.type = field.type;
// TODO(paulberry): eliminate this cast by changing the type of
// `FieldFormalParameterElementImpl.field`.
parameter.type = field.type as TypeImpl;
}
} else if (parameter is SuperFormalParameterElementImpl) {
var superParameter = parameter.superConstructorParameter;
if (superParameter != null) {
parameter.type = superParameter.type;
// TODO(paulberry): eliminate this cast by changing the type of
// `SuperFormalParameterElementImpl.superConstructorParameter`.
parameter.type = superParameter.type as TypeImpl;
} else {
parameter.type = DynamicTypeImpl.instance;
}
Expand Down Expand Up @@ -544,7 +552,9 @@ class InstanceMemberInferrer {
combinedSignatureType.parameters,
);
if (matchingParameter != null) {
parameter.type = matchingParameter.type;
// TODO(paulberry): eliminate this cast by changing the return type of
// `_getCorrespondingParameter`.
parameter.type = matchingParameter.type as TypeImpl;
} else {
parameter.type = DynamicTypeImpl.instance;
}
Expand Down Expand Up @@ -724,7 +734,7 @@ class InstanceMemberInferrer {
return false;
}

static void _setFieldType(FieldElementImpl field, DartType type) {
static void _setFieldType(FieldElementImpl field, TypeImpl type) {
field.type = type;
}
}
Expand Down
Loading

0 comments on commit eba7e49

Please sign in to comment.