Skip to content

Commit 7198fb9

Browse files
stereotype441Commit Queue
authored andcommitted
[analyzer] Use TypeImpl for context types.
The signatures of `ExpressionImpl.resolveExpression` and all of its overrides are changed so that they expect a context which is a `TypeImpl` rather than a `DartType`. The signatures of expression `visit` methods in `ResolverVisitor` are changed in a similar way. To reduce the number of casts that this introduces, several fields and methods in the following classes have their types changed to use "Impl" types: - `ElementResolver` - `FunctionExpressionInvocationResolver` - `GenericFunctionInferenceTest` - `GenericInferrer` - `InstanceCreationExpressionResolver` - `InterfaceElementImpl` - `InterfaceElementImpl2` - `InterfacesMerger` - `InterfaceTypeImpl` - `InvocationInferenceHelper` - `InvocationInferrer` - `MethodElementImpl2` - `MethodInvocationResolver` - `MixinElementImpl` - `MixinElementImpl2` - `NamedTypeResolver` - `ResolutionReader` - `ResolverVisitor` - `Substitution` - `TypedLiteralResolver` - `TypeSystemImpl` - `_ClassInterfaceType` - `_LiteralResolution` - `_MixinInference` 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: I11d476d712846c28b05e0fa1a5972fb7585e114a Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/405101 Reviewed-by: Konstantin Shcheglov <[email protected]> Commit-Queue: Paul Berry <[email protected]>
1 parent eb69196 commit 7198fb9

21 files changed

+255
-220
lines changed

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

Lines changed: 45 additions & 45 deletions
Large diffs are not rendered by default.

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

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import 'package:analyzer/dart/element/element.dart';
88
import 'package:analyzer/dart/element/nullability_suffix.dart';
99
import 'package:analyzer/dart/element/type.dart';
1010
import 'package:analyzer/src/dart/element/element.dart';
11+
import 'package:analyzer/src/dart/element/type.dart';
1112
import 'package:analyzer/src/dart/element/type_algebra.dart';
1213
import 'package:analyzer/src/dart/element/type_system.dart';
1314
import 'package:analyzer/src/utilities/extensions/collection.dart';
@@ -58,19 +59,19 @@ class ClassHierarchy {
5859
var typeSystem = library.typeSystem;
5960
var interfacesMerger = InterfacesMerger(typeSystem);
6061

61-
void append(InterfaceType? type) {
62+
void append(InterfaceTypeImpl? type) {
6263
if (type == null) {
6364
return;
6465
}
6566

6667
interfacesMerger.add(type);
6768

6869
var substitution = Substitution.fromInterfaceType(type);
69-
var element = type.element as InterfaceElementImpl;
70+
var element = type.element;
7071
var rawInterfaces = implementedInterfaces(element);
7172
for (var rawInterface in rawInterfaces) {
7273
var newInterface =
73-
substitution.substituteType(rawInterface) as InterfaceType;
74+
substitution.substituteType(rawInterface) as InterfaceTypeImpl;
7475
interfacesMerger.add(newInterface);
7576
}
7677
}
@@ -130,24 +131,24 @@ class InterfacesMerger {
130131

131132
InterfacesMerger(this._typeSystem);
132133

133-
List<InterfaceType> get typeList {
134+
List<InterfaceTypeImpl> get typeList {
134135
return _map.values.map((e) => e.type).toList();
135136
}
136137

137-
void add(InterfaceType type) {
138+
void add(InterfaceTypeImpl type) {
138139
var element = type.element;
139140
var classResult = _map[element];
140141
if (classResult == null) {
141142
classResult = _ClassInterfaceType(
142143
_typeSystem,
143-
element is ClassElement && element.isDartCoreObject,
144+
element is ClassElementImpl && element.isDartCoreObject,
144145
);
145146
_map[element] = classResult;
146147
}
147148
classResult.update(type);
148149
}
149150

150-
void addWithSupertypes(InterfaceType? type) {
151+
void addWithSupertypes(InterfaceTypeImpl? type) {
151152
if (type != null) {
152153
for (var superType in type.allSupertypes) {
153154
add(superType);
@@ -163,14 +164,14 @@ class _ClassInterfaceType {
163164

164165
ClassHierarchyError? _error;
165166

166-
InterfaceType? _singleType;
167-
InterfaceType? _currentResult;
167+
InterfaceTypeImpl? _singleType;
168+
InterfaceTypeImpl? _currentResult;
168169

169170
_ClassInterfaceType(this._typeSystem, this._isDartCoreObject);
170171

171-
InterfaceType get type => (_currentResult ?? _singleType)!;
172+
InterfaceTypeImpl get type => (_currentResult ?? _singleType)!;
172173

173-
void update(InterfaceType type) {
174+
void update(InterfaceTypeImpl type) {
174175
if (_error != null) {
175176
return;
176177
}
@@ -182,11 +183,12 @@ class _ClassInterfaceType {
182183
} else if (type == _singleType) {
183184
return;
184185
} else {
185-
_currentResult = _typeSystem.normalize(_singleType!) as InterfaceType;
186+
_currentResult =
187+
_typeSystem.normalize(_singleType!) as InterfaceTypeImpl;
186188
}
187189
}
188190

189-
var normType = _typeSystem.normalize(type) as InterfaceType;
191+
var normType = _typeSystem.normalize(type) as InterfaceTypeImpl;
190192
try {
191193
_currentResult = _merge(_currentResult!, normType);
192194
} catch (e) {
@@ -197,7 +199,7 @@ class _ClassInterfaceType {
197199
}
198200
}
199201

200-
InterfaceType _merge(InterfaceType T1, InterfaceType T2) {
202+
InterfaceTypeImpl _merge(InterfaceTypeImpl T1, InterfaceTypeImpl T2) {
201203
// Normally `Object?` cannot be a superinterface.
202204
// However, it can happen for extension types.
203205
if (_isDartCoreObject) {
@@ -211,7 +213,7 @@ class _ClassInterfaceType {
211213
}
212214
}
213215

214-
return _typeSystem.topMerge(T1, T2) as InterfaceType;
216+
return _typeSystem.topMerge(T1, T2) as InterfaceTypeImpl;
215217
}
216218
}
217219

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

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6079,16 +6079,16 @@ abstract class InterfaceElementImpl extends InstanceElementImpl
60796079
implements InterfaceElement, InterfaceFragment {
60806080
/// A list containing all of the mixins that are applied to the class being
60816081
/// extended in order to derive the superclass of this class.
6082-
List<InterfaceType> _mixins = const [];
6082+
List<InterfaceTypeImpl> _mixins = const [];
60836083

60846084
/// A list containing all of the interfaces that are implemented by this
60856085
/// class.
6086-
List<InterfaceType> _interfaces = const [];
6086+
List<InterfaceTypeImpl> _interfaces = const [];
60876087

60886088
/// This callback is set during mixins inference to handle reentrant calls.
60896089
List<InterfaceType>? Function(InterfaceElementImpl)? mixinInferenceCallback;
60906090

6091-
InterfaceType? _supertype;
6091+
InterfaceTypeImpl? _supertype;
60926092

60936093
/// The cached result of [allSupertypes].
60946094
List<InterfaceType>? _allSupertypes;
@@ -6157,13 +6157,15 @@ abstract class InterfaceElementImpl extends InstanceElementImpl
61576157
InterfaceElementImpl2 get element;
61586158

61596159
@override
6160-
List<InterfaceType> get interfaces {
6160+
List<InterfaceTypeImpl> get interfaces {
61616161
linkedData?.read(this);
61626162
return _interfaces;
61636163
}
61646164

61656165
set interfaces(List<InterfaceType> interfaces) {
6166-
_interfaces = interfaces;
6166+
// TODO(paulberry): eliminate this cast by changing the type of the
6167+
// `interfaces` parameter.
6168+
_interfaces = interfaces.cast();
61676169
}
61686170

61696171
/// Return `true` if this class represents the class '_Enum' defined in the
@@ -6188,11 +6190,13 @@ abstract class InterfaceElementImpl extends InstanceElementImpl
61886190
}
61896191

61906192
@override
6191-
List<InterfaceType> get mixins {
6193+
List<InterfaceTypeImpl> get mixins {
61926194
if (mixinInferenceCallback != null) {
61936195
var mixins = mixinInferenceCallback!(this);
61946196
if (mixins != null) {
6195-
return _mixins = mixins;
6197+
// TODO(paulberry): eliminate this cast by changing the type of
6198+
// `InterfaceElementImpl.mixinInferenceCallback`.
6199+
return _mixins = mixins.cast();
61966200
}
61976201
}
61986202

@@ -6201,7 +6205,9 @@ abstract class InterfaceElementImpl extends InstanceElementImpl
62016205
}
62026206

62036207
set mixins(List<InterfaceType> mixins) {
6204-
_mixins = mixins;
6208+
// TODO(paulberry): eliminate this cast by changing the type of the `mixins`
6209+
// parameter.
6210+
_mixins = mixins.cast();
62056211
}
62066212

62076213
@override
@@ -6210,13 +6216,15 @@ abstract class InterfaceElementImpl extends InstanceElementImpl
62106216
}
62116217

62126218
@override
6213-
InterfaceType? get supertype {
6219+
InterfaceTypeImpl? get supertype {
62146220
linkedData?.read(this);
62156221
return _supertype;
62166222
}
62176223

62186224
set supertype(InterfaceType? value) {
6219-
_supertype = value;
6225+
// TODO(paulberry): eliminate this cast by changing the type of the `value`
6226+
// parameter.
6227+
_supertype = value as InterfaceTypeImpl?;
62206228
}
62216229

62226230
@override
@@ -6508,10 +6516,9 @@ abstract class InterfaceElementImpl2 extends InstanceElementImpl2
65086516
/// Should be used only when the element has no type parameters.
65096517
InterfaceTypeImpl? _nullableInstance;
65106518

6511-
@override
6512-
List<InterfaceType> interfaces = [];
6519+
List<InterfaceTypeImpl> _interfaces = [];
65136520

6514-
List<InterfaceType> _mixins = [];
6521+
List<InterfaceTypeImpl> _mixins = [];
65156522

65166523
@override
65176524
List<ConstructorElement> constructors = [];
@@ -6547,23 +6554,36 @@ abstract class InterfaceElementImpl2 extends InstanceElementImpl2
65476554
}
65486555

65496556
@override
6550-
List<InterfaceType> get mixins {
6557+
List<InterfaceTypeImpl> get interfaces => _interfaces;
6558+
6559+
set interfaces(List<InterfaceType> values) {
6560+
// TODO(paulberry): eliminate this cast by changing the type of the `values`
6561+
// parameter
6562+
_interfaces = values.cast();
6563+
}
6564+
6565+
@override
6566+
List<InterfaceTypeImpl> get mixins {
65516567
if (firstFragment.mixinInferenceCallback case var callback?) {
65526568
var mixins = callback(firstFragment);
65536569
if (mixins != null) {
6554-
return _mixins = mixins;
6570+
// TODO(paulberry): eliminate this cast by changing the type of
6571+
// `InterfaceElementImpl.mixinInferenceCallback`.
6572+
return _mixins = mixins.cast();
65556573
}
65566574
}
65576575

65586576
return _mixins;
65596577
}
65606578

65616579
set mixins(List<InterfaceType> value) {
6562-
_mixins = value;
6580+
// TODO(paulberry): eliminate this cast by changing the type of the `value`
6581+
// parameter.
6582+
_mixins = value.cast();
65636583
}
65646584

65656585
@override
6566-
InterfaceType? get supertype => firstFragment.supertype;
6586+
InterfaceTypeImpl? get supertype => firstFragment.supertype;
65676587

65686588
@override
65696589
InterfaceTypeImpl get thisType {
@@ -8408,7 +8428,7 @@ class MethodElementImpl2 extends ExecutableElementImpl2
84088428
class MixinElementImpl extends ClassOrMixinElementImpl
84098429
with AugmentableElement<MixinElementImpl>
84108430
implements MixinElement, MixinFragment {
8411-
List<InterfaceType> _superclassConstraints = const [];
8431+
List<InterfaceTypeImpl> _superclassConstraints = const [];
84128432

84138433
/// Names of methods, getters, setters, and operators that this mixin
84148434
/// declaration super-invokes. For setters this includes the trailing "=".
@@ -8446,7 +8466,7 @@ class MixinElementImpl extends ClassOrMixinElementImpl
84468466
ElementKind get kind => ElementKind.MIXIN;
84478467

84488468
@override
8449-
List<InterfaceType> get mixins => const [];
8469+
List<InterfaceTypeImpl> get mixins => const [];
84508470

84518471
@override
84528472
set mixins(List<InterfaceType> mixins) {
@@ -8461,17 +8481,19 @@ class MixinElementImpl extends ClassOrMixinElementImpl
84618481
super.previousFragment as MixinElementImpl?;
84628482

84638483
@override
8464-
List<InterfaceType> get superclassConstraints {
8484+
List<InterfaceTypeImpl> get superclassConstraints {
84658485
linkedData?.read(this);
84668486
return _superclassConstraints;
84678487
}
84688488

84698489
set superclassConstraints(List<InterfaceType> superclassConstraints) {
8470-
_superclassConstraints = superclassConstraints;
8490+
// TODO(paulberry): eliminate this cast by changing the type of the
8491+
// `superclassConstraints` parameter.
8492+
_superclassConstraints = superclassConstraints.cast();
84718493
}
84728494

84738495
@override
8474-
InterfaceType? get supertype => null;
8496+
InterfaceTypeImpl? get supertype => null;
84758497

84768498
@override
84778499
set supertype(InterfaceType? supertype) {
@@ -8506,7 +8528,7 @@ class MixinElementImpl2 extends InterfaceElementImpl2
85068528
final MixinElementImpl firstFragment;
85078529

85088530
@override
8509-
List<InterfaceType> superclassConstraints = [];
8531+
List<InterfaceTypeImpl> superclassConstraints = [];
85108532

85118533
MixinElementImpl2(this.reference, this.firstFragment) {
85128534
reference.element2 = this;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ class GenericInferrer {
186186
/// Constrain a universal function type [fnType] used in a context
187187
/// [contextType].
188188
void constrainGenericFunctionInContext(
189-
FunctionType fnType, DartType contextType,
189+
FunctionType fnType, TypeImpl contextType,
190190
{required AstNodeImpl? nodeForTesting}) {
191191
var origin = TypeConstraintFromFunctionContext(
192192
functionType: fnType, contextType: contextType);
@@ -210,7 +210,7 @@ class GenericInferrer {
210210

211211
/// Apply a return type constraint, which asserts that the [declaredType]
212212
/// is a subtype of the [contextType].
213-
void constrainReturnType(DartType declaredType, DartType contextType,
213+
void constrainReturnType(DartType declaredType, TypeImpl contextType,
214214
{required AstNodeImpl? nodeForTesting}) {
215215
var origin = TypeConstraintFromReturnType(
216216
declaredType: declaredType, contextType: contextType);

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,7 @@ class InterfaceTypeImpl extends TypeImpl implements InterfaceType {
663663
}
664664

665665
@override
666-
List<InterfaceType> get allSupertypes {
666+
List<InterfaceTypeImpl> get allSupertypes {
667667
var substitution = Substitution.fromInterfaceType(this);
668668
return element.allSupertypes
669669
.map((t) => (substitution.substituteType(t) as InterfaceTypeImpl)
@@ -819,8 +819,7 @@ class InterfaceTypeImpl extends TypeImpl implements InterfaceType {
819819

820820
@override
821821
List<InterfaceType> get mixins {
822-
List<InterfaceType> mixins = element.mixins;
823-
return _instantiateSuperTypes(mixins);
822+
return _instantiateSuperTypes(element.mixins);
824823
}
825824

826825
@Deprecated('Check element, or use getDisplayString()')
@@ -862,7 +861,7 @@ class InterfaceTypeImpl extends TypeImpl implements InterfaceType {
862861
}
863862

864863
@override
865-
List<InterfaceType> get superclassConstraints {
864+
List<InterfaceTypeImpl> get superclassConstraints {
866865
var element = this.element;
867866
var augmented = element.augmented;
868867
if (augmented is MixinElementImpl2) {
@@ -1169,7 +1168,8 @@ class InterfaceTypeImpl extends TypeImpl implements InterfaceType {
11691168
);
11701169
}
11711170

1172-
List<InterfaceType> _instantiateSuperTypes(List<InterfaceType> definedTypes) {
1171+
List<InterfaceTypeImpl> _instantiateSuperTypes(
1172+
List<InterfaceTypeImpl> definedTypes) {
11731173
if (definedTypes.isEmpty) return definedTypes;
11741174

11751175
MapSubstitution? substitution;

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,11 +197,11 @@ abstract class Substitution {
197197

198198
DartType? getSubstitute(TypeParameterElement parameter, bool upperBound);
199199

200-
InterfaceType mapInterfaceType(InterfaceType type) {
201-
return substituteType(type) as InterfaceType;
200+
InterfaceTypeImpl mapInterfaceType(InterfaceType type) {
201+
return substituteType(type) as InterfaceTypeImpl;
202202
}
203203

204-
Iterable<InterfaceType> mapInterfaceTypes(Iterable<InterfaceType> types) {
204+
Iterable<InterfaceTypeImpl> mapInterfaceTypes(Iterable<InterfaceType> types) {
205205
return types.map(mapInterfaceType);
206206
}
207207

0 commit comments

Comments
 (0)