Skip to content

Commit

Permalink
[analyzer] Use TypeImpl for context types.
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
stereotype441 authored and Commit Queue committed Jan 19, 2025
1 parent eb69196 commit 7198fb9
Show file tree
Hide file tree
Showing 21 changed files with 255 additions and 220 deletions.
90 changes: 45 additions & 45 deletions pkg/analyzer/lib/src/dart/ast/ast.dart

Large diffs are not rendered by default.

32 changes: 17 additions & 15 deletions pkg/analyzer/lib/src/dart/element/class_hierarchy.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/nullability_suffix.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:analyzer/src/dart/element/element.dart';
import 'package:analyzer/src/dart/element/type.dart';
import 'package:analyzer/src/dart/element/type_algebra.dart';
import 'package:analyzer/src/dart/element/type_system.dart';
import 'package:analyzer/src/utilities/extensions/collection.dart';
Expand Down Expand Up @@ -58,19 +59,19 @@ class ClassHierarchy {
var typeSystem = library.typeSystem;
var interfacesMerger = InterfacesMerger(typeSystem);

void append(InterfaceType? type) {
void append(InterfaceTypeImpl? type) {
if (type == null) {
return;
}

interfacesMerger.add(type);

var substitution = Substitution.fromInterfaceType(type);
var element = type.element as InterfaceElementImpl;
var element = type.element;
var rawInterfaces = implementedInterfaces(element);
for (var rawInterface in rawInterfaces) {
var newInterface =
substitution.substituteType(rawInterface) as InterfaceType;
substitution.substituteType(rawInterface) as InterfaceTypeImpl;
interfacesMerger.add(newInterface);
}
}
Expand Down Expand Up @@ -130,24 +131,24 @@ class InterfacesMerger {

InterfacesMerger(this._typeSystem);

List<InterfaceType> get typeList {
List<InterfaceTypeImpl> get typeList {
return _map.values.map((e) => e.type).toList();
}

void add(InterfaceType type) {
void add(InterfaceTypeImpl type) {
var element = type.element;
var classResult = _map[element];
if (classResult == null) {
classResult = _ClassInterfaceType(
_typeSystem,
element is ClassElement && element.isDartCoreObject,
element is ClassElementImpl && element.isDartCoreObject,
);
_map[element] = classResult;
}
classResult.update(type);
}

void addWithSupertypes(InterfaceType? type) {
void addWithSupertypes(InterfaceTypeImpl? type) {
if (type != null) {
for (var superType in type.allSupertypes) {
add(superType);
Expand All @@ -163,14 +164,14 @@ class _ClassInterfaceType {

ClassHierarchyError? _error;

InterfaceType? _singleType;
InterfaceType? _currentResult;
InterfaceTypeImpl? _singleType;
InterfaceTypeImpl? _currentResult;

_ClassInterfaceType(this._typeSystem, this._isDartCoreObject);

InterfaceType get type => (_currentResult ?? _singleType)!;
InterfaceTypeImpl get type => (_currentResult ?? _singleType)!;

void update(InterfaceType type) {
void update(InterfaceTypeImpl type) {
if (_error != null) {
return;
}
Expand All @@ -182,11 +183,12 @@ class _ClassInterfaceType {
} else if (type == _singleType) {
return;
} else {
_currentResult = _typeSystem.normalize(_singleType!) as InterfaceType;
_currentResult =
_typeSystem.normalize(_singleType!) as InterfaceTypeImpl;
}
}

var normType = _typeSystem.normalize(type) as InterfaceType;
var normType = _typeSystem.normalize(type) as InterfaceTypeImpl;
try {
_currentResult = _merge(_currentResult!, normType);
} catch (e) {
Expand All @@ -197,7 +199,7 @@ class _ClassInterfaceType {
}
}

InterfaceType _merge(InterfaceType T1, InterfaceType T2) {
InterfaceTypeImpl _merge(InterfaceTypeImpl T1, InterfaceTypeImpl T2) {
// Normally `Object?` cannot be a superinterface.
// However, it can happen for extension types.
if (_isDartCoreObject) {
Expand All @@ -211,7 +213,7 @@ class _ClassInterfaceType {
}
}

return _typeSystem.topMerge(T1, T2) as InterfaceType;
return _typeSystem.topMerge(T1, T2) as InterfaceTypeImpl;
}
}

Expand Down
68 changes: 45 additions & 23 deletions pkg/analyzer/lib/src/dart/element/element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6079,16 +6079,16 @@ abstract class InterfaceElementImpl extends InstanceElementImpl
implements InterfaceElement, InterfaceFragment {
/// A list containing all of the mixins that are applied to the class being
/// extended in order to derive the superclass of this class.
List<InterfaceType> _mixins = const [];
List<InterfaceTypeImpl> _mixins = const [];

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

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

InterfaceType? _supertype;
InterfaceTypeImpl? _supertype;

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

@override
List<InterfaceType> get interfaces {
List<InterfaceTypeImpl> get interfaces {
linkedData?.read(this);
return _interfaces;
}

set interfaces(List<InterfaceType> interfaces) {
_interfaces = interfaces;
// TODO(paulberry): eliminate this cast by changing the type of the
// `interfaces` parameter.
_interfaces = interfaces.cast();
}

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

@override
List<InterfaceType> get mixins {
List<InterfaceTypeImpl> get mixins {
if (mixinInferenceCallback != null) {
var mixins = mixinInferenceCallback!(this);
if (mixins != null) {
return _mixins = mixins;
// TODO(paulberry): eliminate this cast by changing the type of
// `InterfaceElementImpl.mixinInferenceCallback`.
return _mixins = mixins.cast();
}
}

Expand All @@ -6201,7 +6205,9 @@ abstract class InterfaceElementImpl extends InstanceElementImpl
}

set mixins(List<InterfaceType> mixins) {
_mixins = mixins;
// TODO(paulberry): eliminate this cast by changing the type of the `mixins`
// parameter.
_mixins = mixins.cast();
}

@override
Expand All @@ -6210,13 +6216,15 @@ abstract class InterfaceElementImpl extends InstanceElementImpl
}

@override
InterfaceType? get supertype {
InterfaceTypeImpl? get supertype {
linkedData?.read(this);
return _supertype;
}

set supertype(InterfaceType? value) {
_supertype = value;
// TODO(paulberry): eliminate this cast by changing the type of the `value`
// parameter.
_supertype = value as InterfaceTypeImpl?;
}

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

@override
List<InterfaceType> interfaces = [];
List<InterfaceTypeImpl> _interfaces = [];

List<InterfaceType> _mixins = [];
List<InterfaceTypeImpl> _mixins = [];

@override
List<ConstructorElement> constructors = [];
Expand Down Expand Up @@ -6547,23 +6554,36 @@ abstract class InterfaceElementImpl2 extends InstanceElementImpl2
}

@override
List<InterfaceType> get mixins {
List<InterfaceTypeImpl> get interfaces => _interfaces;

set interfaces(List<InterfaceType> values) {
// TODO(paulberry): eliminate this cast by changing the type of the `values`
// parameter
_interfaces = values.cast();
}

@override
List<InterfaceTypeImpl> get mixins {
if (firstFragment.mixinInferenceCallback case var callback?) {
var mixins = callback(firstFragment);
if (mixins != null) {
return _mixins = mixins;
// TODO(paulberry): eliminate this cast by changing the type of
// `InterfaceElementImpl.mixinInferenceCallback`.
return _mixins = mixins.cast();
}
}

return _mixins;
}

set mixins(List<InterfaceType> value) {
_mixins = value;
// TODO(paulberry): eliminate this cast by changing the type of the `value`
// parameter.
_mixins = value.cast();
}

@override
InterfaceType? get supertype => firstFragment.supertype;
InterfaceTypeImpl? get supertype => firstFragment.supertype;

@override
InterfaceTypeImpl get thisType {
Expand Down Expand Up @@ -8408,7 +8428,7 @@ class MethodElementImpl2 extends ExecutableElementImpl2
class MixinElementImpl extends ClassOrMixinElementImpl
with AugmentableElement<MixinElementImpl>
implements MixinElement, MixinFragment {
List<InterfaceType> _superclassConstraints = const [];
List<InterfaceTypeImpl> _superclassConstraints = const [];

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

@override
List<InterfaceType> get mixins => const [];
List<InterfaceTypeImpl> get mixins => const [];

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

@override
List<InterfaceType> get superclassConstraints {
List<InterfaceTypeImpl> get superclassConstraints {
linkedData?.read(this);
return _superclassConstraints;
}

set superclassConstraints(List<InterfaceType> superclassConstraints) {
_superclassConstraints = superclassConstraints;
// TODO(paulberry): eliminate this cast by changing the type of the
// `superclassConstraints` parameter.
_superclassConstraints = superclassConstraints.cast();
}

@override
InterfaceType? get supertype => null;
InterfaceTypeImpl? get supertype => null;

@override
set supertype(InterfaceType? supertype) {
Expand Down Expand Up @@ -8506,7 +8528,7 @@ class MixinElementImpl2 extends InterfaceElementImpl2
final MixinElementImpl firstFragment;

@override
List<InterfaceType> superclassConstraints = [];
List<InterfaceTypeImpl> superclassConstraints = [];

MixinElementImpl2(this.reference, this.firstFragment) {
reference.element2 = this;
Expand Down
4 changes: 2 additions & 2 deletions pkg/analyzer/lib/src/dart/element/generic_inferrer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ class GenericInferrer {
/// Constrain a universal function type [fnType] used in a context
/// [contextType].
void constrainGenericFunctionInContext(
FunctionType fnType, DartType contextType,
FunctionType fnType, TypeImpl contextType,
{required AstNodeImpl? nodeForTesting}) {
var origin = TypeConstraintFromFunctionContext(
functionType: fnType, contextType: contextType);
Expand All @@ -210,7 +210,7 @@ class GenericInferrer {

/// Apply a return type constraint, which asserts that the [declaredType]
/// is a subtype of the [contextType].
void constrainReturnType(DartType declaredType, DartType contextType,
void constrainReturnType(DartType declaredType, TypeImpl contextType,
{required AstNodeImpl? nodeForTesting}) {
var origin = TypeConstraintFromReturnType(
declaredType: declaredType, contextType: contextType);
Expand Down
10 changes: 5 additions & 5 deletions pkg/analyzer/lib/src/dart/element/type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ class InterfaceTypeImpl extends TypeImpl implements InterfaceType {
}

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

@override
List<InterfaceType> get mixins {
List<InterfaceType> mixins = element.mixins;
return _instantiateSuperTypes(mixins);
return _instantiateSuperTypes(element.mixins);
}

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

@override
List<InterfaceType> get superclassConstraints {
List<InterfaceTypeImpl> get superclassConstraints {
var element = this.element;
var augmented = element.augmented;
if (augmented is MixinElementImpl2) {
Expand Down Expand Up @@ -1169,7 +1168,8 @@ class InterfaceTypeImpl extends TypeImpl implements InterfaceType {
);
}

List<InterfaceType> _instantiateSuperTypes(List<InterfaceType> definedTypes) {
List<InterfaceTypeImpl> _instantiateSuperTypes(
List<InterfaceTypeImpl> definedTypes) {
if (definedTypes.isEmpty) return definedTypes;

MapSubstitution? substitution;
Expand Down
6 changes: 3 additions & 3 deletions pkg/analyzer/lib/src/dart/element/type_algebra.dart
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,11 @@ abstract class Substitution {

DartType? getSubstitute(TypeParameterElement parameter, bool upperBound);

InterfaceType mapInterfaceType(InterfaceType type) {
return substituteType(type) as InterfaceType;
InterfaceTypeImpl mapInterfaceType(InterfaceType type) {
return substituteType(type) as InterfaceTypeImpl;
}

Iterable<InterfaceType> mapInterfaceTypes(Iterable<InterfaceType> types) {
Iterable<InterfaceTypeImpl> mapInterfaceTypes(Iterable<InterfaceType> types) {
return types.map(mapInterfaceType);
}

Expand Down
Loading

0 comments on commit 7198fb9

Please sign in to comment.