Skip to content

Commit 6aeb1bf

Browse files
stereotype441Commit Queue
authored andcommitted
[analyzer] Refine some internal getter types.
The types of the following getters are changed: - From `DartType` to `TypeImpl`: - `ExecutableElementImpl.returnType` - `FormalParameterElementImpl.type` - `FunctionTypeImpl.returnType` - `GenericFunctionTypeElementImpl.returnType` - `ParameterElementImpl_ofImplicitSetter.type` - `PropertyAccessorElementImpl_ImplicitGetter.returnType` - `PropertyAccessorElementImpl_ImplicitSetter.returnType` - `PropertyInducingElementImpl.type` - `RecordTypeFieldImpl.type` - `VariableElementImpl.type` - `VariableMember.type` - From `InterfaceType` to `InterfaceTypeImpl`: - `ConstructorElementImpl.returnType` - `ConstructorElementMixin.returnType` - `ConstructorMember.returnType` - `InterfaceElementImpl.thisType` - `InterfaceElementImpl2.thisType` - From `AugmentedInterfaceElement` to `InterfaceElementImpl2`: - `InterfaceElementImpl.augmented` There is no change to the analyzer public API. To make this possible, a few type casts had to be added, and the return type of some type inference methods had to be changed from `DartType` to `TypeImpl`. Some other type casts were able to be eliminated. I hope to eliminate the added type casts in future CLs. To ensure that type promotion continues to occur, a few `is` and `as` tests had to be changed so that they test against Impl types rather than public API types. 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: Ibf58396c46381aa49e465dcebc14877c6952aa01 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/403943 Reviewed-by: Konstantin Shcheglov <[email protected]> Commit-Queue: Paul Berry <[email protected]>
1 parent 52cc629 commit 6aeb1bf

File tree

7 files changed

+64
-44
lines changed

7 files changed

+64
-44
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class ClassHierarchy {
7676
}
7777

7878
append(element.supertype);
79-
if (augmented is AugmentedMixinElement) {
79+
if (augmented is MixinElementImpl2) {
8080
for (var type in augmented.superclassConstraints) {
8181
append(type);
8282
}

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

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1561,15 +1561,15 @@ class ConstructorElementImpl extends ExecutableElementImpl
15611561
}
15621562

15631563
@override
1564-
InterfaceType get returnType {
1564+
InterfaceTypeImpl get returnType {
15651565
var result = _returnType;
15661566
if (result != null) {
1567-
return result as InterfaceType;
1567+
return result as InterfaceTypeImpl;
15681568
}
15691569

15701570
var augmentedDeclaration = enclosingElement3.augmented.firstFragment;
15711571
result = augmentedDeclaration.thisType;
1572-
return _returnType = result as InterfaceType;
1572+
return _returnType = result as InterfaceTypeImpl;
15731573
}
15741574

15751575
@override
@@ -1763,6 +1763,9 @@ mixin ConstructorElementMixin implements ConstructorElement {
17631763
bool get isGenerative {
17641764
return !isFactory;
17651765
}
1766+
1767+
@override
1768+
InterfaceTypeImpl get returnType;
17661769
}
17671770

17681771
/// A [TopLevelVariableElement] for a top-level 'const' variable that has an
@@ -3722,7 +3725,7 @@ abstract class ExecutableElementImpl extends _ExistingElementImpl
37223725
List<ParameterElementImpl> _parameters = const [];
37233726

37243727
/// The inferred return type of this executable element.
3725-
DartType? _returnType;
3728+
TypeImpl? _returnType;
37263729

37273730
/// The type of function defined by this executable element.
37283731
FunctionType? _type;
@@ -3860,13 +3863,15 @@ abstract class ExecutableElementImpl extends _ExistingElementImpl
38603863
}
38613864

38623865
@override
3863-
DartType get returnType {
3866+
TypeImpl get returnType {
38643867
linkedData?.read(this);
38653868
return _returnType!;
38663869
}
38673870

38683871
set returnType(DartType returnType) {
3869-
_returnType = returnType;
3872+
// TODO(paulberry): eliminate this cast by changing the setter parameter
3873+
// type to `TypeImpl`.
3874+
_returnType = returnType as TypeImpl;
38703875
// We do this because of return type inference. At the moment when we
38713876
// create a local function element we don't know yet its return type,
38723877
// because we have not done static type analysis yet.
@@ -4619,7 +4624,7 @@ class FormalParameterElementImpl extends PromotableElementImpl2
46194624

46204625
@override
46214626
// TODO(augmentations): Implement the merge of formal parameters.
4622-
DartType get type => wrappedElement.type;
4627+
TypeImpl get type => wrappedElement.type;
46234628

46244629
@override
46254630
// TODO(augmentations): Implement the merge of formal parameters.
@@ -5264,7 +5269,7 @@ class GenericFunctionTypeElementImpl extends _ExistingElementImpl
52645269
FunctionTypedElementImpl,
52655270
GenericFunctionTypeFragment {
52665271
/// The declared return type of the function.
5267-
DartType? _returnType;
5272+
TypeImpl? _returnType;
52685273

52695274
/// The elements representing the parameters of the function.
52705275
List<ParameterElement> _parameters = const [];
@@ -5343,15 +5348,17 @@ class GenericFunctionTypeElementImpl extends _ExistingElementImpl
53435348
GenericFunctionTypeElementImpl? get previousFragment => null;
53445349

53455350
@override
5346-
DartType get returnType {
5351+
TypeImpl get returnType {
53475352
return _returnType!;
53485353
}
53495354

53505355
/// Set the return type defined by this function type element to the given
53515356
/// [returnType].
53525357
@override
53535358
set returnType(DartType returnType) {
5354-
_returnType = returnType;
5359+
// TODO(paulberry): eliminate this cast by changing the setter parameter
5360+
// type to `TypeImpl`.
5361+
_returnType = returnType as TypeImpl;
53555362
}
53565363

53575364
@override
@@ -6111,7 +6118,7 @@ abstract class InterfaceElementImpl extends InstanceElementImpl
61116118
InterfaceElementImpl? get augmentationTarget;
61126119

61136120
@override
6114-
AugmentedInterfaceElement get augmented;
6121+
InterfaceElementImpl2 get augmented;
61156122

61166123
@override
61176124
List<Element> get children => [
@@ -6215,7 +6222,7 @@ abstract class InterfaceElementImpl extends InstanceElementImpl
62156222
}
62166223

62176224
@override
6218-
InterfaceType get thisType {
6225+
InterfaceTypeImpl get thisType {
62196226
return augmented.thisType;
62206227
}
62216228

@@ -6536,7 +6543,7 @@ abstract class InterfaceElementImpl2 extends InstanceElementImpl2
65366543
@override
65376544
List<ConstructorElement> constructors = [];
65386545

6539-
InterfaceType? _thisType;
6546+
InterfaceTypeImpl? _thisType;
65406547

65416548
@override
65426549
List<InterfaceType> get allSupertypes => firstFragment.allSupertypes;
@@ -6586,7 +6593,7 @@ abstract class InterfaceElementImpl2 extends InstanceElementImpl2
65866593
InterfaceType? get supertype => firstFragment.supertype;
65876594

65886595
@override
6589-
InterfaceType get thisType {
6596+
InterfaceTypeImpl get thisType {
65906597
if (_thisType == null) {
65916598
List<DartType> typeArguments;
65926599
var typeParameters = firstFragment.typeParameters;
@@ -9441,7 +9448,7 @@ class ParameterElementImpl_ofImplicitSetter extends ParameterElementImpl {
94419448
}
94429449

94439450
@override
9444-
DartType get type => setter.variable2.type;
9451+
TypeImpl get type => setter.variable2.type;
94459452

94469453
@override
94479454
set type(DartType type) {
@@ -10075,7 +10082,7 @@ class PropertyAccessorElementImpl_ImplicitGetter
1007510082
}
1007610083

1007710084
@override
10078-
DartType get returnType => variable2.type;
10085+
TypeImpl get returnType => variable2.type;
1007910086

1008010087
@override
1008110088
set returnType(DartType returnType) {
@@ -10141,7 +10148,7 @@ class PropertyAccessorElementImpl_ImplicitSetter
1014110148
}
1014210149

1014310150
@override
10144-
DartType get returnType => VoidTypeImpl.instance;
10151+
TypeImpl get returnType => VoidTypeImpl.instance;
1014510152

1014610153
@override
1014710154
set returnType(DartType returnType) {
@@ -10280,15 +10287,15 @@ abstract class PropertyInducingElementImpl
1028010287
}
1028110288

1028210289
@override
10283-
DartType get type {
10290+
TypeImpl get type {
1028410291
linkedData?.read(this);
1028510292
if (_type != null) return _type!;
1028610293

1028710294
if (isSynthetic) {
1028810295
if (getter != null) {
1028910296
return _type = getter!.returnType;
1029010297
} else if (setter != null) {
10291-
List<ParameterElement> parameters = setter!.parameters;
10298+
var parameters = setter!.parameters;
1029210299
return _type = parameters.isNotEmpty
1029310300
? parameters[0].type
1029410301
: DynamicTypeImpl.instance;
@@ -10378,7 +10385,7 @@ abstract class PropertyInducingElementImpl2 extends VariableElementImpl2
1037810385
/// Instances of this class are set for fields and top-level variables
1037910386
/// to perform top-level type inference during linking.
1038010387
abstract class PropertyInducingElementTypeInference {
10381-
DartType perform();
10388+
TypeImpl perform();
1038210389
}
1038310390

1038410391
class SetterElementImpl extends PropertyAccessorElementImpl2
@@ -11477,7 +11484,7 @@ abstract class UriReferencedElementImpl extends _ExistingElementImpl
1147711484
abstract class VariableElementImpl extends ElementImpl
1147811485
implements VariableElement {
1147911486
/// The type of this variable.
11480-
DartType? _type;
11487+
TypeImpl? _type;
1148111488

1148211489
/// Initialize a newly created variable element to have the given [name] and
1148311490
/// [offset].
@@ -11569,10 +11576,12 @@ abstract class VariableElementImpl extends ElementImpl
1156911576
String get name => super.name!;
1157011577

1157111578
@override
11572-
DartType get type => _type!;
11579+
TypeImpl get type => _type!;
1157311580

1157411581
set type(DartType type) {
11575-
_type = type;
11582+
// TODO(paulberry): eliminate this cast by changing the setter parameter
11583+
// type to `TypeImpl`.
11584+
_type = type as TypeImpl;
1157611585
}
1157711586

1157811587
@override

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import 'package:analyzer/dart/element/type.dart';
1313
import 'package:analyzer/source/source.dart';
1414
import 'package:analyzer/src/dart/element/display_string_builder.dart';
1515
import 'package:analyzer/src/dart/element/element.dart';
16+
import 'package:analyzer/src/dart/element/type.dart';
1617
import 'package:analyzer/src/dart/element/type_algebra.dart';
1718
import 'package:analyzer/src/generated/engine.dart' show AnalysisContext;
1819
import 'package:analyzer/src/generated/utilities_dart.dart';
@@ -111,7 +112,10 @@ class ConstructorMember extends ExecutableMember
111112
}
112113

113114
@override
114-
InterfaceType get returnType => type.returnType as InterfaceType;
115+
InterfaceTypeImpl get returnType =>
116+
// TODO(paulberry): eliminate this cast by changing the type of `type` to
117+
// `FunctionTypeImpl`.
118+
type.returnType as InterfaceTypeImpl;
115119

116120
@override
117121
Source get source => _declaration.source!;
@@ -1244,7 +1248,7 @@ class ParameterMember extends VariableMember
12441248
@override
12451249
List<ParameterElement> get parameters {
12461250
var type = this.type;
1247-
if (type is FunctionType) {
1251+
if (type is FunctionTypeImpl) {
12481252
return type.parameters;
12491253
}
12501254
return const <ParameterElement>[];
@@ -1643,7 +1647,7 @@ class TopLevelVariableMember extends VariableMember
16431647
/// A variable element defined in a parameterized type where the values of the
16441648
/// type parameters are known.
16451649
abstract class VariableMember extends Member implements VariableElement {
1646-
DartType? _type;
1650+
TypeImpl? _type;
16471651

16481652
/// Initialize a newly created element to represent a variable, based on the
16491653
/// [declaration], with applied [substitution].
@@ -1675,13 +1679,15 @@ abstract class VariableMember extends Member implements VariableElement {
16751679
bool get isStatic => declaration.isStatic;
16761680

16771681
@override
1678-
DartType get type {
1682+
TypeImpl get type {
16791683
if (_type != null) return _type!;
16801684

16811685
var result = declaration.type;
16821686
result = augmentationSubstitution.substituteType(result);
16831687
result = _substitution.substituteType(result);
1684-
return _type = result;
1688+
// TODO(paulberry): remove this cast by changing the type of `declaration`
1689+
// and the return type of `substituteType`
1690+
return _type = result as TypeImpl;
16851691
}
16861692

16871693
@override

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

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ class FunctionTypeImpl extends TypeImpl
9696
late int hashCode = _computeHashCode();
9797

9898
@override
99-
final DartType returnType;
99+
final TypeImpl returnType;
100100

101101
@override
102102
final List<TypeParameterElement> typeFormals;
@@ -163,7 +163,9 @@ class FunctionTypeImpl extends TypeImpl
163163
return FunctionTypeImpl._(
164164
typeFormals: typeFormals,
165165
parameters: parameters,
166-
returnType: returnType,
166+
// TODO(paulberry): eliminate this cast by changing the type of
167+
// `returnType`.
168+
returnType: returnType as TypeImpl,
167169
nullabilitySuffix: nullabilitySuffix,
168170
positionalParameterTypes: positionalParameterTypes,
169171
requiredPositionalParameterCount: requiredPositionalParameterCount,
@@ -348,7 +350,7 @@ class FunctionTypeImpl extends TypeImpl
348350
return true;
349351
}
350352

351-
return (returnType as TypeImpl).referencesAny(parameters);
353+
return returnType.referencesAny(parameters);
352354
}
353355

354356
@override
@@ -375,7 +377,7 @@ class FunctionTypeImpl extends TypeImpl
375377
return true;
376378
}
377379

378-
return (returnType as TypeImpl).referencesAny2(parameters);
380+
return returnType.referencesAny2(parameters);
379381
}
380382

381383
@override
@@ -881,7 +883,7 @@ class InterfaceTypeImpl extends TypeImpl implements InterfaceType {
881883
List<InterfaceType> get superclassConstraints {
882884
var element = this.element;
883885
var augmented = element.augmented;
884-
if (augmented is AugmentedMixinElement) {
886+
if (augmented is MixinElementImpl2) {
885887
var constraints = augmented.superclassConstraints;
886888
return _instantiateSuperTypes(constraints);
887889
} else {
@@ -1350,11 +1352,14 @@ class NullTypeImpl extends InterfaceTypeImpl
13501352

13511353
abstract class RecordTypeFieldImpl implements RecordTypeField {
13521354
@override
1353-
final DartType type;
1355+
final TypeImpl type;
13541356

13551357
RecordTypeFieldImpl({
1356-
required this.type,
1357-
});
1358+
required DartType type,
1359+
}) :
1360+
// TODO(paulberry): eliminate this cast by changing the type of the
1361+
// constructor parameter to `TypeImpl`.
1362+
type = type as TypeImpl;
13581363
}
13591364

13601365
class RecordTypeImpl extends TypeImpl

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3288,7 +3288,7 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
32883288
EnumElementImpl element,
32893289
) {
32903290
var valuesFieldType = element.valuesField?.type;
3291-
if (valuesFieldType is InterfaceType) {
3291+
if (valuesFieldType is InterfaceTypeImpl) {
32923292
var isWellBounded = typeSystem.isWellBounded(
32933293
valuesFieldType.typeArguments.single,
32943294
allowSuperBounded: true,

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ class ConstructorInitializerResolver {
3030
for (var constructorElement in interfaceElement.constructors) {
3131
_constructor(
3232
unitElement,
33-
// TODO(scheglov): Avoid cast.
34-
interfaceElement.augmented.firstFragment as InterfaceElementImpl,
33+
interfaceElement.augmented.firstFragment,
3534
constructorElement,
3635
);
3736
}

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
import 'package:analyzer/dart/element/element.dart';
88
import 'package:analyzer/dart/element/scope.dart';
9-
import 'package:analyzer/dart/element/type.dart';
109
import 'package:analyzer/src/dart/ast/ast.dart';
1110
import 'package:analyzer/src/dart/ast/extensions.dart';
1211
import 'package:analyzer/src/dart/element/element.dart';
@@ -214,7 +213,7 @@ class _PropertyInducingElementTypeInference
214213
this._libraryBuilder);
215214

216215
@override
217-
DartType perform() {
216+
TypeImpl perform() {
218217
if (_node.initializer == null) {
219218
_status = _InferenceStatus.inferred;
220219
return DynamicTypeImpl.instance;
@@ -277,10 +276,12 @@ class _PropertyInducingElementTypeInference
277276
}
278277

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

283-
DartType _refineType(DartType type) {
284+
TypeImpl _refineType(TypeImpl type) {
284285
if (type.isDartCoreNull) {
285286
return DynamicTypeImpl.instance;
286287
}

0 commit comments

Comments
 (0)