Add DartType.element2

Unfortunately my idea about checking for specific `DartType` subtype,
and only then asking for the element is too punitive. It almost
works in google3, but the amount and the kind of changes I had to
do make me realize that we should keep `get elementX` in `DartType`.

I guess this is the same as we had in AST when `get constructors`
does not make sense for mixins (?), but works for classes and enums,
and it is easier to pull it into the superclass.

Change-Id: Ibc4fac0b95d63748fa65de96d29300f477fdfc76
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/254482
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
This commit is contained in:
Konstantin Shcheglov 2022-08-11 16:01:48 +00:00 committed by Commit Bot
parent e0954f8e16
commit 82e33cc598
48 changed files with 172 additions and 147 deletions

View file

@ -74,7 +74,7 @@ class TypeDefinitionHandler extends MessageHandler<TypeDefinitionParams,
if (type is InterfaceType) {
element = type.element2;
} else if (type is TypeParameterType) {
element = type.element;
element = type.element2;
}
if (element is! analyzer.ElementImpl) {
return success(_emptyResult);

View file

@ -915,7 +915,7 @@ class CorrectionUtils {
}
if (type is TypeParameterType) {
var element = type.element;
var element = type.element2;
if (_isTypeParameterVisible(element)) {
return element.name;
} else {

View file

@ -1,3 +1,6 @@
## 4.6.0-dev
* Added `DartType.element2`, so that `InterfaceType.element2` overrides it.
## 4.5.0
* Update deprecation message for `FormalParameter.identifier`.
* Deprecated `ClassOrMixinDeclaration`, use `ClassDeclaration` and `MixinDeclaration` directly.

View file

@ -44,9 +44,13 @@ abstract class DartType {
/// the type has not, or cannot, be associated with an element. The former
/// case will occur if the element model is not yet complete; the latter case
/// will occur if this object represents an undefined type.
@Deprecated('Check for specific DartType subtype and use element2 instead')
@Deprecated('Use element2 instead')
Element? get element;
/// Return the element representing the declaration of this type, or `null`
/// if the type is not associated with an element.
Element? get element2;
/// Return `true` if this type represents the bottom type.
bool get isBottom;
@ -282,7 +286,7 @@ abstract class InterfaceType implements ParameterizedType {
@override
ClassElement get element;
/// Return the element representing the declaration of this type.
@override
InterfaceElement get element2;
/// Return a list containing all of the interfaces that are implemented by
@ -396,10 +400,7 @@ abstract class InterfaceType implements ParameterizedType {
}
/// The type `Never` represents the uninhabited bottom type.
abstract class NeverType implements DartType {
@override
Element get element;
}
abstract class NeverType implements DartType {}
/// A type that can track substituted type parameters, either for itself after
/// instantiation, or from a surrounding context.
@ -438,8 +439,12 @@ abstract class TypeParameterType implements DartType {
/// Always consult the bound if that could be relevant.
ElementLocation get definition;
@Deprecated('Use element2 instead')
@override
TypeParameterElement get element;
@override
TypeParameterElement get element2;
}
/// The special type `void` is used to indicate that the value of an

View file

@ -730,7 +730,7 @@ class ConstantVisitor extends UnifyingAstVisitor<DartObjectImpl> {
if (typeArgumentTypes != null) {
var instantiatedTypeArgumentTypes = typeArgumentTypes.map((type) {
if (type is TypeParameterType) {
return _lexicalTypeEnvironment?[type.element] ?? type;
return _lexicalTypeEnvironment?[type.element2] ?? type;
} else {
return type;
}

View file

@ -205,7 +205,7 @@ class ElementDisplayStringBuilder {
}
void writeTypeParameterType(TypeParameterTypeImpl type) {
_write(type.element.displayName);
_write(type.element2.displayName);
_writeNullability(type.nullabilitySuffix);
var promotedBound = type.promotedBound;
@ -411,7 +411,7 @@ class ElementDisplayStringBuilder {
void collectTypeParameters(DartType? type) {
if (type is TypeParameterType) {
referencedTypeParameters.add(type.element);
referencedTypeParameters.add(type.element2);
} else if (type is FunctionType) {
for (var typeParameter in type.typeFormals) {
collectTypeParameters(typeParameter.bound);

View file

@ -1877,8 +1877,7 @@ class DirectiveUriWithUnitImpl extends DirectiveUriWithRelativeUriImpl
/// The synthetic element representing the declaration of the type `dynamic`.
class DynamicElementImpl extends ElementImpl implements TypeDefiningElement {
/// Return the unique instance of this class.
static DynamicElementImpl get instance =>
DynamicTypeImpl.instance.element as DynamicElementImpl;
static DynamicElementImpl get instance => DynamicTypeImpl.instance.element2;
/// Initialize a newly created instance of this class. Instances of this class
/// should <b>not</b> be created except as part of creating the type
@ -6381,7 +6380,7 @@ class TypeAliasElementImpl extends _ExistingElementImpl
}
final typeArgument = typeArguments[i];
if (typeArgument is TypeParameterType &&
typeParameters[i] != typeArgument.element) {
typeParameters[i] != typeArgument.element2) {
return false;
}
}
@ -6501,7 +6500,7 @@ class TypeAliasElementImpl extends _ExistingElementImpl
);
} else if (type is TypeParameterType) {
return TypeParameterTypeImpl(
element: type.element,
element: type.element2,
nullabilitySuffix: resultNullability,
alias: InstantiatedTypeAliasElementImpl(
element: this,

View file

@ -82,7 +82,7 @@ class LeastGreatestClosureHelper extends ReplacementVisitor {
@override
DartType? visitTypeParameterType(TypeParameterType type) {
if (eliminationTargets.contains(type.element)) {
if (eliminationTargets.contains(type.element2)) {
var replacement = _typeParameterReplacement as TypeImpl;
return replacement.withNullability(
uniteNullabilities(

View file

@ -661,7 +661,7 @@ class LeastUpperBoundHelper {
// otherwise UP(B1a, T2)
// where B1a is the greatest closure of B1 with respect to X1
var bound = _typeParameterBound(T1);
var closure = _typeSystem.greatestClosure(bound, [T1.element]);
var closure = _typeSystem.greatestClosure(bound, [T1.element2]);
return getLeastUpperBound(closure, T2);
}
@ -680,7 +680,7 @@ class LeastUpperBoundHelper {
// otherwise UP(T1, B2a)
// where B2a is the greatest closure of B2 with respect to X2
var bound = _typeParameterBound(T2);
var closure = _typeSystem.greatestClosure(bound, [T2.element]);
var closure = _typeSystem.greatestClosure(bound, [T2.element2]);
return getLeastUpperBound(T1, closure);
}
@ -898,7 +898,7 @@ class LeastUpperBoundHelper {
/// Return the promoted or declared bound of the type parameter.
DartType _typeParameterBound(TypeParameterTypeImpl type) {
var bound = type.promotedBound ?? type.element.bound;
var bound = type.promotedBound ?? type.element2.bound;
if (bound != null) {
return bound;
}

View file

@ -235,7 +235,7 @@ class NormalizeHelper {
/// NORM(X & T)
/// NORM(X extends T)
DartType _typeParameterType(TypeParameterTypeImpl T) {
var element = T.element;
var element = T.element2;
// NORM(X & T)
var promotedBound = T.promotedBound;
@ -286,7 +286,7 @@ class NormalizeHelper {
// * if S is X then X
if (S is TypeParameterType &&
S.nullabilitySuffix == NullabilitySuffix.none &&
S.element == X.declaration) {
S.element2 == X.declaration) {
return X.declaration.instantiate(
nullabilitySuffix: NullabilitySuffix.none,
);

View file

@ -132,7 +132,7 @@ class ReplacementVisitor
var promotedBound = (type as TypeParameterTypeImpl).promotedBound;
return TypeParameterTypeImpl(
element: type.element,
element: type.element2,
nullabilitySuffix: newNullability ?? type.nullabilitySuffix,
promotedBound: newPromotedBound ?? promotedBound,
alias: type.alias,
@ -148,7 +148,7 @@ class ReplacementVisitor
}
return TypeParameterTypeImpl(
element: type.element,
element: type.element2,
nullabilitySuffix: newNullability,
alias: type.alias,
);

View file

@ -118,7 +118,7 @@ class RuntimeTypeEqualityVisitor
bool visitTypeParameterType(TypeParameterType T1, DartType T2) {
return T2 is TypeParameterType &&
_compatibleNullability(T1, T2) &&
T1.element == T2.element;
T1.element2 == T2.element2;
}
@override

View file

@ -83,7 +83,7 @@ class SubtypeHelper {
T0 is TypeParameterTypeImpl) {
var S = T0.promotedBound;
if (S == null) {
var B = T0.element.bound ?? _objectQuestion;
var B = T0.element2.bound ?? _objectQuestion;
return isSubtypeOf(B, _objectNone);
} else {
return isSubtypeOf(S, _objectNone);
@ -185,7 +185,7 @@ class SubtypeHelper {
if (T0 is TypeParameterTypeImpl &&
T1 is TypeParameterTypeImpl &&
T1.promotedBound == null &&
T0.element == T1.element) {
T0.element2 == T1.element2) {
return true;
}
@ -195,7 +195,7 @@ class SubtypeHelper {
var T1_promotedBound = T1.promotedBound;
if (T1_promotedBound != null) {
var X1 = TypeParameterTypeImpl(
element: T1.element,
element: T1.element2,
nullabilitySuffix: T1.nullabilitySuffix,
);
return isSubtypeOf(T0, X1) && isSubtypeOf(T0, T1_promotedBound);
@ -227,7 +227,7 @@ class SubtypeHelper {
if (S0 != null && isSubtypeOf(S0, T1)) {
return true;
}
var B0 = T0.element.bound;
var B0 = T0.element2.bound;
if (B0 != null && isSubtypeOf(B0, T1)) {
return true;
}
@ -255,7 +255,7 @@ class SubtypeHelper {
if (S0 != null && isSubtypeOf(S0, T1)) {
return true;
}
var B0 = T0.element.bound;
var B0 = T0.element2.bound;
if (B0 != null && isSubtypeOf(B0, T1)) {
return true;
}
@ -281,7 +281,7 @@ class SubtypeHelper {
return true;
}
var B0 = T0.element.bound;
var B0 = T0.element2.bound;
if (B0 != null && isSubtypeOf(B0, T1)) {
return true;
}

View file

@ -160,7 +160,7 @@ class TopMergeHelper {
}
if (T is TypeParameterType && S is TypeParameterType) {
if (T.element == S.element) {
if (T.element2 == S.element2) {
return T;
} else {
throw _TopMergeStateError(T, S, 'Not the same type parameters');

View file

@ -25,11 +25,18 @@ class DynamicTypeImpl extends TypeImpl implements DynamicType {
/// Prevent the creation of instances of this class.
DynamicTypeImpl._() : super(DynamicElementImpl());
@Deprecated('Use element2 instead')
@override
Element get element {
return super.element!;
}
@override
DynamicElementImpl get element2 {
// ignore: deprecated_member_use_from_same_package
return super.element as DynamicElementImpl;
}
@override
int get hashCode => 1;
@ -94,6 +101,9 @@ class FunctionTypeImpl extends TypeImpl implements FunctionType {
}) : parameters = _sortNamedParameters(parameters),
super(null, alias: alias);
@override
Element? get element2 => null;
@override
int get hashCode {
// Reference the arrays of parameters
@ -500,7 +510,7 @@ class InterfaceTypeImpl extends TypeImpl implements InterfaceType {
return _constructors!;
}
@Deprecated('Check for specific DartType subtype and use element2 instead')
@Deprecated('Use element2 instead')
@override
ClassElement get element => super.element as ClassElement;
@ -904,12 +914,16 @@ class NeverTypeImpl extends TypeImpl implements NeverType {
/// The unique instance of this class, non-nullable.
static final NeverTypeImpl instance = NeverTypeImpl._(NullabilitySuffix.none);
@override
final NeverElementImpl element2 = NeverElementImpl.instance;
@override
final NullabilitySuffix nullabilitySuffix;
/// Prevent the creation of instances of this class.
NeverTypeImpl._(this.nullabilitySuffix) : super(NeverElementImpl.instance);
@Deprecated('Use element2 instead')
@override
NeverElementImpl get element => super.element as NeverElementImpl;
@ -973,7 +987,7 @@ abstract class TypeImpl implements DartType {
/// type has not, or cannot, be associated with an element.
final Element? _element;
/// Initialize a newly created type to be declared by the given [element].
/// Initialize a newly created type to be declared by the given [element2].
TypeImpl(this._element, {this.alias});
@deprecated
@ -985,6 +999,7 @@ abstract class TypeImpl implements DartType {
);
}
@Deprecated('Use element2 instead')
@override
Element? get element => _element;
@ -1119,7 +1134,7 @@ class TypeParameterTypeImpl extends TypeImpl implements TypeParameterType {
/// An optional promoted bound on the type parameter.
///
/// 'null' indicates that the type parameter's bound has not been promoted and
/// is therefore the same as the bound of [element].
/// is therefore the same as the bound of [element2].
final DartType? promotedBound;
/// Initialize a newly created type parameter type to be declared by the given
@ -1136,16 +1151,23 @@ class TypeParameterTypeImpl extends TypeImpl implements TypeParameterType {
@override
DartType get bound =>
promotedBound ?? element.bound ?? DynamicTypeImpl.instance;
promotedBound ?? element2.bound ?? DynamicTypeImpl.instance;
@override
ElementLocation get definition => element.location!;
ElementLocation get definition => element2.location!;
@Deprecated('Use element2 instead')
@override
TypeParameterElement get element => super.element as TypeParameterElement;
@override
int get hashCode => element.hashCode;
TypeParameterElement get element2 {
// ignore: deprecated_member_use_from_same_package
return super.element as TypeParameterElement;
}
@override
int get hashCode => element2.hashCode;
@override
bool get isBottom {
@ -1154,7 +1176,7 @@ class TypeParameterTypeImpl extends TypeImpl implements TypeParameterType {
// bounds form a loop. So we have to be more careful.
Set<TypeParameterElement> seenTypes = {};
TypeParameterType type = this;
while (seenTypes.add(type.element)) {
while (seenTypes.add(type.element2)) {
var bound = type.bound;
if (bound is TypeParameterType) {
type = bound;
@ -1176,7 +1198,7 @@ class TypeParameterTypeImpl extends TypeImpl implements TypeParameterType {
return true;
}
if (other is TypeParameterTypeImpl && other.element == element) {
if (other is TypeParameterTypeImpl && other.element2 == element2) {
if (other.nullabilitySuffix != nullabilitySuffix) {
return false;
}
@ -1211,7 +1233,7 @@ class TypeParameterTypeImpl extends TypeImpl implements TypeParameterType {
@override
bool referencesAny(Set<TypeParameterElement> parameters) {
return parameters.contains(element);
return parameters.contains(element2);
}
@Deprecated('Use TypeSystem.resolveToBound() instead')
@ -1246,7 +1268,7 @@ class TypeParameterTypeImpl extends TypeImpl implements TypeParameterType {
TypeImpl withNullability(NullabilitySuffix nullabilitySuffix) {
if (this.nullabilitySuffix == nullabilitySuffix) return this;
return TypeParameterTypeImpl(
element: element,
element: element2,
nullabilitySuffix: nullabilitySuffix,
promotedBound: promotedBound,
);
@ -1261,6 +1283,9 @@ class VoidTypeImpl extends TypeImpl implements VoidType {
/// Prevent the creation of instances of this class.
VoidTypeImpl._() : super(null);
@override
Element? get element2 => null;
@override
int get hashCode => 2;

View file

@ -547,7 +547,7 @@ abstract class _TypeSubstitutor
@override
DartType visitTypeParameterType(TypeParameterType type) {
var argument = getSubstitute(type.element);
var argument = getSubstitute(type.element2);
if (argument == null) {
return type;
}

View file

@ -109,8 +109,8 @@ class TypeConstraintGatherer {
var P_nullability = P.nullabilitySuffix;
if (P is TypeParameterType &&
P_nullability == NullabilitySuffix.none &&
_typeParameters.contains(P.element)) {
_addUpper(P.element, Q);
_typeParameters.contains(P.element2)) {
_addUpper(P.element2, Q);
return true;
}
@ -119,8 +119,8 @@ class TypeConstraintGatherer {
var Q_nullability = Q.nullabilitySuffix;
if (Q is TypeParameterType &&
Q_nullability == NullabilitySuffix.none &&
_typeParameters.contains(Q.element)) {
_addLower(Q.element, P);
_typeParameters.contains(Q.element2)) {
_addLower(Q.element2, P);
return true;
}
@ -308,7 +308,7 @@ class TypeConstraintGatherer {
// Note: we have already eliminated the case that `X` is a variable in `L`.
if (P_nullability == NullabilitySuffix.none && P is TypeParameterTypeImpl) {
var rewind = _constraints.length;
var B = P.promotedBound ?? P.element.bound;
var B = P.promotedBound ?? P.element2.bound;
if (B != null && trySubtypeMatch(B, Q, leftSchema)) {
return true;
}

View file

@ -36,7 +36,7 @@ class DemotionNonNullificationVisitor extends ReplacementVisitor {
var typeImpl = type as TypeParameterTypeImpl;
if (typeImpl.promotedBound != null) {
return TypeParameterTypeImpl(
element: type.element,
element: type.element2,
nullabilitySuffix: newNullability ?? type.nullabilitySuffix,
alias: type.alias,
);

View file

@ -3,6 +3,7 @@
// BSD-style license that can be found in the LICENSE file.
import 'package:analyzer/dart/ast/token.dart' show Keyword;
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/dart/element/type_visitor.dart';
@ -21,6 +22,9 @@ class UnknownInferredType extends TypeImpl {
UnknownInferredType._() : super(null);
@override
Element? get element2 => null;
@override
int get hashCode => 1;

View file

@ -372,7 +372,7 @@ class TypeSystemImpl implements TypeSystem {
}
visitedTypes.add(type);
if (type is TypeParameterType) {
var element = type.element;
var element = type.element2;
if ((candidates == null || candidates.contains(element)) &&
!boundTypeParameters.contains(element)) {
parameters ??= <TypeParameterElement>[];
@ -740,7 +740,7 @@ class TypeSystemImpl implements TypeSystem {
return result;
}
T = type.element.bound;
T = type.element2.bound;
if (T != null) {
var result = isBottom(T);
assert(type.isBottom == result);
@ -762,7 +762,7 @@ class TypeSystemImpl implements TypeSystem {
}
if (type is TypeParameterTypeImpl) {
var bound = type.element.bound;
var bound = type.element2.bound;
if (bound != null && isDynamicBounded(bound)) {
return true;
}
@ -798,7 +798,7 @@ class TypeSystemImpl implements TypeSystem {
}
if (type is TypeParameterTypeImpl) {
var bound = type.element.bound;
var bound = type.element2.bound;
if (bound != null && isFunctionBounded(bound)) {
return true;
}
@ -884,8 +884,8 @@ class TypeSystemImpl implements TypeSystem {
// is anything except none by this point.
assert(T_nullability == NullabilitySuffix.none);
assert(S_nullability == NullabilitySuffix.none);
var T_element = T.element;
var S_element = S.element;
var T_element = T.element2;
var S_element = S.element2;
// MOREBOTTOM(X&T, Y&S) = MOREBOTTOM(T, S)
var T_promotedBound = T.promotedBound;
@ -1026,7 +1026,7 @@ class TypeSystemImpl implements TypeSystem {
} else if (type is InterfaceType && type.isDartAsyncFutureOr) {
return isNonNullable(type.typeArguments[0]);
} else if (type is TypeParameterType) {
var bound = type.element.bound;
var bound = type.element2.bound;
return bound != null && isNonNullable(bound);
}
return true;
@ -1313,7 +1313,7 @@ class TypeSystemImpl implements TypeSystem {
if (type.isDartCoreNull) return NeverTypeImpl.instance;
if (type is TypeParameterTypeImpl) {
var element = type.element;
var element = type.element2;
// NonNull(X & T) = X & NonNull(T)
if (type.promotedBound != null) {
@ -1497,7 +1497,7 @@ class TypeSystemImpl implements TypeSystem {
return resolveToBound(promotedBound);
}
final bound = type.element.bound;
final bound = type.element2.bound;
if (bound == null) {
return isNonNullableByDefault ? objectQuestion : objectStar;
}
@ -1592,7 +1592,7 @@ class TypeSystemImpl implements TypeSystem {
// `U` to `S` where `S <: U`, yielding a type parameter `T extends S`.
if (from is TypeParameterType) {
if (isSubtypeOf(to, from.bound)) {
var declaration = from.element.declaration;
var declaration = from.element2.declaration;
return TypeParameterTypeImpl(
element: declaration,
nullabilitySuffix: _promotedTypeParameterTypeNullability(

View file

@ -234,7 +234,7 @@ class FunctionReferenceResolver {
// If the type of the function is a type parameter, the tearoff is
// disallowed, reported in [_resolveDisallowedExpression]. Use the type
// parameter's bound here in an attempt to assign the intended types.
rawType = rawType.element.bound;
rawType = rawType.element2.bound;
}
if (rawType is FunctionType) {

View file

@ -185,7 +185,7 @@ class NamespaceBuilder {
// which is not possible for `dynamic`.
if (library.isDartCore) {
definedNames['dynamic'] = DynamicElementImpl.instance;
definedNames['Never'] = NeverTypeImpl.instance.element;
definedNames['Never'] = NeverTypeImpl.instance.element2;
}
return Namespace(definedNames);

View file

@ -27,7 +27,7 @@ class Variance {
/// Computes the variance of the [typeParameter] in the [type].
factory Variance(TypeParameterElement typeParameter, DartType type) {
if (type is TypeParameterType) {
if (type.element == typeParameter) {
if (type.element2 == typeParameter) {
return covariant;
} else {
return unrelated;

View file

@ -269,7 +269,7 @@ class SdkConstraintVerifier extends RecursiveAstVisitor<void> {
}
_errorReporter.reportErrorForNode(
HintCode.SDK_VERSION_ASYNC_EXPORTED_FROM_CORE, node, [element.name]);
} else if (checkNnbd && element == _typeProvider.neverType.element) {
} else if (checkNnbd && element == _typeProvider.neverType.element2) {
_errorReporter.reportErrorForNode(HintCode.SDK_VERSION_NEVER, node);
}
}

View file

@ -1825,7 +1825,7 @@ class ResolutionReader {
);
} else if (type is TypeParameterType) {
return TypeParameterTypeImpl(
element: type.element,
element: type.element2,
nullabilitySuffix: type.nullabilitySuffix,
alias: InstantiatedTypeAliasElementImpl(
element: aliasElement,

View file

@ -676,7 +676,7 @@ class ResolutionSink extends _SummaryDataWriter {
_writeTypeAliasElementArguments(type);
} else if (type is TypeParameterType) {
writeByte(Tag.TypeParameterType);
writeElement(type.element);
writeElement(type.element2);
_writeNullabilitySuffix(type.nullabilitySuffix);
_writeTypeAliasElementArguments(type);
} else if (type is VoidType) {

View file

@ -387,7 +387,7 @@ class _TypeParametersGraph implements Graph<int> {
_collectReferencesFrom(index, argument);
}
} else if (type is TypeParameterType) {
var typeIndex = _parameterToIndex[type.element];
var typeIndex = _parameterToIndex[type.element2];
if (typeIndex != null) {
_edges[typeIndex].add(index);
}
@ -440,9 +440,9 @@ class _UpperLowerReplacementVisitor extends ReplacementVisitor {
@override
DartType? visitTypeParameterType(TypeParameterType type) {
if (_variance == Variance.contravariant) {
return _lower[type.element];
return _lower[type.element2];
} else {
return _upper[type.element];
return _upper[type.element2];
}
}
}

View file

@ -105,7 +105,7 @@ class DeclarationBuilderFromElement {
return macro.NamedTypeAnnotationImpl(
id: macro.RemoteInstance.uniqueId,
isNullable: type.nullabilitySuffix == NullabilitySuffix.question,
identifier: identifier(type.element),
identifier: identifier(type.element2),
typeArguments: const [],
);
} else {

View file

@ -55,7 +55,7 @@ class VarianceBuilder {
Variance _compute(TypeParameterElement variable, DartType? type) {
if (type is TypeParameterType) {
if (type.element == variable) {
if (type.element2 == variable) {
return Variance.covariant;
} else {
return Variance.unrelated;

View file

@ -1,5 +1,5 @@
name: analyzer
version: 4.5.0
version: 4.6.0-dev
description: This package provides a library that performs static analysis of Dart code.
repository: https://github.com/dart-lang/sdk/tree/main/pkg/analyzer

View file

@ -485,7 +485,7 @@ class TryPromoteToTest extends AbstractTypeSystemTest {
NullabilitySuffix nullability,
DartType promotedBound,
) {
expect(type.element, element);
expect(type.element2, element);
expect(type.nullabilitySuffix, nullability);
expect(type.promotedBound, promotedBound);
}

View file

@ -1983,7 +1983,7 @@ main() {
var fType = findElement.localVar('f').type as FunctionType;
var fTypeTypeParameter = fType.typeFormals[0];
var fTypeParameter = fType.normalParameterTypes[0] as TypeParameterType;
expect(fTypeParameter.element, same(fTypeTypeParameter));
expect(fTypeParameter.element2, same(fTypeTypeParameter));
var tRef = findNode.simple('T>');
var functionTypeNode = tRef.parent!.parent!.parent as GenericFunctionType;
var functionType = functionTypeNode.type as FunctionType;
@ -4132,8 +4132,8 @@ void main() {
var tElement = fElement.typeParameters[0];
var uElement = fElement.typeParameters[1];
var vElement = fElement.typeParameters[2];
expect((tElement.bound as TypeParameterType).element, same(uElement));
expect((vElement.bound as TypeParameterType).element, same(uElement));
expect((tElement.bound as TypeParameterType).element2, same(uElement));
expect((vElement.bound as TypeParameterType).element2, same(uElement));
}
test_local_function_generic_with_named_parameter() async {
@ -4152,7 +4152,7 @@ void main() {
assertType(fElement.type, 'void Function<T>({T x})');
var tElement = fElement.typeParameters[0];
expect(fElement.type.typeFormals[0], same(tElement));
expect((fElement.type.parameters[0].type as TypeParameterType).element,
expect((fElement.type.parameters[0].type as TypeParameterType).element2,
same(tElement));
}
@ -4172,7 +4172,7 @@ void main() {
assertType(fElement.type, 'void Function<T>([T])');
var tElement = fElement.typeParameters[0];
expect(fElement.type.typeFormals[0], same(tElement));
expect((fElement.type.parameters[0].type as TypeParameterType).element,
expect((fElement.type.parameters[0].type as TypeParameterType).element2,
same(tElement));
}
@ -4505,7 +4505,7 @@ void main() {
var gTypeType = gType.type as FunctionType;
var gTypeParameterType =
gTypeType.namedParameterTypes['t'] as TypeParameterType;
expect(gTypeParameterType.element, same(tElement));
expect(gTypeParameterType.element2, same(tElement));
var gParameterType =
((gType.parameters.parameters[0] as DefaultFormalParameter).parameter
as SimpleFormalParameter)
@ -4537,7 +4537,7 @@ void main() {
var gTypeType = gType.type as FunctionType;
var gTypeParameterType =
gTypeType.normalParameterTypes[0] as TypeParameterType;
expect(gTypeParameterType.element, same(tElement));
expect(gTypeParameterType.element2, same(tElement));
var gParameterType =
(gType.parameters.parameters[0] as SimpleFormalParameter).type
as NamedType;
@ -4568,7 +4568,7 @@ void main() {
var gTypeType = gType.type as FunctionType;
var gTypeParameterType =
gTypeType.optionalParameterTypes[0] as TypeParameterType;
expect(gTypeParameterType.element, same(tElement));
expect(gTypeParameterType.element2, same(tElement));
var gParameterType =
((gType.parameters.parameters[0] as DefaultFormalParameter).parameter
as SimpleFormalParameter)
@ -4599,7 +4599,7 @@ void main() {
var gType = gDeclaration.variables.type as GenericFunctionType;
var gTypeType = gType.type as FunctionType;
var gTypeReturnType = gTypeType.returnType as TypeParameterType;
expect(gTypeReturnType.element, same(tElement));
expect(gTypeReturnType.element2, same(tElement));
var gReturnType = gType.returnType as NamedType;
var tReference = gReturnType.name;
assertElement(tReference, tElement);
@ -4627,7 +4627,7 @@ void main() {
var yType = yDeclaration.variables.type as NamedType;
var yTypeType = yType.type as InterfaceType;
var yTypeTypeArgument = yTypeType.typeArguments[0] as TypeParameterType;
expect(yTypeTypeArgument.element, same(tElement));
expect(yTypeTypeArgument.element2, same(tElement));
var yElementType = yType.typeArguments!.arguments[0] as NamedType;
var tReference = yElementType.name;
assertElement(tReference, tElement);
@ -4677,7 +4677,7 @@ void main() {
var gTypeParameterType =
gTypeType.namedParameterTypes['u'] as TypeParameterType;
expect(gTypeParameterType.element, same(tElement));
expect(gTypeParameterType.element2, same(tElement));
var gArgumentType = gType.typeArguments!.arguments[0] as NamedType;
var tReference = gArgumentType.name;
@ -4704,7 +4704,7 @@ void main() {
var gTypeParameterType =
gTypeType.normalParameterTypes[0] as TypeParameterType;
expect(gTypeParameterType.element, same(tElement));
expect(gTypeParameterType.element2, same(tElement));
var gArgumentType = gType.typeArguments!.arguments[0] as NamedType;
var tReference = gArgumentType.name;
@ -4731,7 +4731,7 @@ void main() {
var gTypeParameterType =
gTypeType.optionalParameterTypes[0] as TypeParameterType;
expect(gTypeParameterType.element, same(tElement));
expect(gTypeParameterType.element2, same(tElement));
var gArgumentType = gType.typeArguments!.arguments[0] as NamedType;
var tReference = gArgumentType.name;
@ -4757,7 +4757,7 @@ void main() {
var gTypeType = gType.type as FunctionType;
var gTypeReturnType = gTypeType.returnType as TypeParameterType;
expect(gTypeReturnType.element, same(tElement));
expect(gTypeReturnType.element2, same(tElement));
var gArgumentType = gType.typeArguments!.arguments[0] as NamedType;
var tReference = gArgumentType.name;
@ -5569,7 +5569,7 @@ class C<T> {
assertInvokeType(fooInvocation, 'T Function(C<T>)');
assertType(fooInvocation.staticType, 'T');
final type = fooInvocation.typeOrThrow as TypeParameterType;
expect(type.element, same(tElement));
expect(type.element2, same(tElement));
}
test_methodInvocation_topLevelFunction() async {
@ -8685,7 +8685,7 @@ main() {
if (type is InterfaceType) {
expect(namedType.name.staticElement, same(type.element2));
} else if (type is TypeParameterType) {
expect(namedType.name.staticElement, same(type.element));
expect(namedType.name.staticElement, same(type.element2));
} else {
throw UnimplementedError();
}

View file

@ -1330,7 +1330,7 @@ class TypeParameterTypeImplTest extends AbstractTypeSystemTest {
void test_getElement() {
TypeParameterElementImpl element = TypeParameterElementImpl('E', -1);
TypeParameterTypeImpl type = typeParameterTypeStar(element);
expect(type.element, element);
expect(type.element2, element);
}
@deprecated

View file

@ -209,7 +209,7 @@ class GenericFunctionInferenceTest extends AbstractTypeSystemTest {
var inferredTypes = _inferCall(rawType, [S_and_int]);
var inferredType = inferredTypes[0] as TypeParameterTypeImpl;
expect(inferredType.element, S);
expect(inferredType.element2, S);
expect(inferredType.promotedBound, isNull);
}

View file

@ -536,8 +536,8 @@ class _TypeParameterCollector extends TypeVisitor<void> {
@override
void visitTypeParameterType(TypeParameterType type) {
if (!functionTypeParameters.contains(type.element)) {
var bound = type.element.bound;
if (!functionTypeParameters.contains(type.element2)) {
var bound = type.element2.bound;
var promotedBound = (type as TypeParameterTypeImpl).promotedBound;
if (bound == null && promotedBound == null) {
@ -548,7 +548,7 @@ class _TypeParameterCollector extends TypeVisitor<void> {
if (bound != null) {
var boundStr = bound.getDisplayString(withNullability: true);
str += '${type.element.name} extends $boundStr';
str += '${type.element2.name} extends $boundStr';
}
if (promotedBound != null) {
@ -558,7 +558,7 @@ class _TypeParameterCollector extends TypeVisitor<void> {
if (str.isNotEmpty) {
str += ', ';
}
str += '${type.element.name} & $promotedBoundStr';
str += '${type.element2.name} & $promotedBoundStr';
}
typeParameters.add(str);

View file

@ -787,7 +787,7 @@ class PromoteToNonNullTest extends AbstractTypeSystemTest {
required DartType? promotedBound,
}) {
var actual = typeSystem.promoteToNonNull(type) as TypeParameterTypeImpl;
expect(actual.element, same(element));
expect(actual.element2, same(element));
expect(actual.promotedBound, promotedBound);
expect(actual.nullabilitySuffix, NullabilitySuffix.none);
}

View file

@ -6051,8 +6051,8 @@ class _TypeParameterCollector extends TypeVisitor<void> {
@override
void visitTypeParameterType(TypeParameterType type) {
if (!functionTypeParameters.contains(type.element)) {
var bound = type.element.bound;
if (!functionTypeParameters.contains(type.element2)) {
var bound = type.element2.bound;
if (bound == null) {
return;
@ -6061,7 +6061,7 @@ class _TypeParameterCollector extends TypeVisitor<void> {
var str = '';
var boundStr = bound.getDisplayString(withNullability: true);
str += '${type.element.name} extends $boundStr';
str += '${type.element2.name} extends $boundStr';
typeParameters.add(str);
}

View file

@ -264,8 +264,8 @@ class SubstituteTest extends _Base {
var T2 = result.typeFormals[0];
var U2 = result.typeFormals[1];
var T2boundArgs = (T2.bound as InterfaceType).typeArguments;
expect((T2boundArgs[0] as TypeParameterType).element, same(T2));
expect((T2boundArgs[1] as TypeParameterType).element, same(U2));
expect((T2boundArgs[0] as TypeParameterType).element2, same(T2));
expect((T2boundArgs[1] as TypeParameterType).element2, same(U2));
}
test_interface_arguments() async {

View file

@ -3492,8 +3492,8 @@ class _TypeParameterCollector
@override
void visitTypeParameterType(TypeParameterType type) {
if (!functionTypeParameters.contains(type.element)) {
var bound = type.element.bound;
if (!functionTypeParameters.contains(type.element2)) {
var bound = type.element2.bound;
var promotedBound = (type as TypeParameterTypeImpl).promotedBound;
if (bound == null && promotedBound == null) {
@ -3504,7 +3504,7 @@ class _TypeParameterCollector
if (bound != null) {
var boundStr = bound.getDisplayString(withNullability: true);
str += '${type.element.name} extends $boundStr';
str += '${type.element2.name} extends $boundStr';
}
if (promotedBound != null) {
@ -3514,7 +3514,7 @@ class _TypeParameterCollector
if (str.isNotEmpty) {
str += ', ';
}
str += '${type.element.name} & $promotedBoundStr';
str += '${type.element2.name} & $promotedBoundStr';
}
typeParameters.add(str);

View file

@ -48,7 +48,7 @@ mixin ResolutionTest implements ResourceProviderMixin {
InterfaceType get doubleType => typeProvider.doubleType;
Element get dynamicElement =>
(typeProvider.dynamicType as DynamicTypeImpl).element;
(typeProvider.dynamicType as DynamicTypeImpl).element2;
FeatureSet get featureSet => result.libraryElement.featureSet;

View file

@ -1111,7 +1111,7 @@ class DartEditBuilderImpl extends EditBuilderImpl implements DartEditBuilder {
}
if (type is TypeParameterType) {
_initializeEnclosingElements();
var element = type.element;
var element = type.element2;
var enclosing = element.enclosingElement3;
while (enclosing is GenericFunctionTypeElement ||
enclosing is ParameterElement) {
@ -1269,7 +1269,7 @@ class DartEditBuilderImpl extends EditBuilderImpl implements DartEditBuilder {
}
if (type is TypeParameterType) {
write(type.element.name);
write(type.element2.name);
_writeTypeNullability(type);
return true;
}

View file

@ -114,8 +114,8 @@ class DecoratedClassHierarchy {
if (typeType is TypeParameterType) {
final innerType = _getInterfaceType(
_variables!.decoratedTypeParameterBound(typeType.element)!);
return type.substitute({typeType.element: innerType});
_variables!.decoratedTypeParameterBound(typeType.element2)!);
return type.substitute({typeType.element2: innerType});
}
throw ArgumentError('$type is an unexpected type');

View file

@ -458,7 +458,7 @@ class DecoratedType implements DecoratedTypeInfo {
return DecoratedType(undecoratedResult, node,
typeArguments: newTypeArguments);
} else if (type is TypeParameterType) {
var inner = substitution[type.element];
var inner = substitution[type.element2];
if (inner == null) {
return this;
} else {

View file

@ -93,7 +93,7 @@ class AssignmentCheckerForTesting extends Object with _AssignmentChecker {
@override
DecoratedType _getTypeParameterTypeBound(DecoratedType type) {
return bounds[(type.type as TypeParameterType).element] ??
return bounds[(type.type as TypeParameterType).element2] ??
(throw StateError('Unknown bound for $type'));
}
}
@ -2199,7 +2199,7 @@ class EdgeBuilder extends GeneralizingAstVisitor<DecoratedType>
var rightType = right.type;
if (leftType is TypeParameterType && leftType != type) {
// We are "unwrapping" a type parameter type to its bound.
final typeParam = leftType.element;
final typeParam = leftType.element2;
return _decorateUpperOrLowerBound(
astNode,
type,
@ -2211,7 +2211,7 @@ class EdgeBuilder extends GeneralizingAstVisitor<DecoratedType>
}
if (rightType is TypeParameterType && rightType != type) {
// We are "unwrapping" a type parameter type to its bound.
final typeParam = rightType.element;
final typeParam = rightType.element2;
return _decorateUpperOrLowerBound(
astNode,
type,
@ -2340,8 +2340,8 @@ class EdgeBuilder extends GeneralizingAstVisitor<DecoratedType>
return DecoratedType(type, node);
}
assert(leftType.sharedElement == type.element &&
rightType.sharedElement == type.element);
assert(leftType.element2 == type.element2 &&
rightType.element2 == type.element2);
return DecoratedType(type, node);
}
_unimplemented(astNode, '_decorateUpperOrLowerBound');
@ -2428,7 +2428,7 @@ class EdgeBuilder extends GeneralizingAstVisitor<DecoratedType>
// TODO(paulberry): once we've wired up flow analysis, return promoted
// bounds if applicable.
return _variables
.decoratedTypeParameterBound((type.type as TypeParameterType).element);
.decoratedTypeParameterBound((type.type as TypeParameterType).element2);
}
/// Creates the necessary constraint(s) for an assignment of the given
@ -3989,19 +3989,8 @@ extension on DartType? {
final self = this;
if (self is TypeParameterType &&
self.nullabilitySuffix == NullabilitySuffix.star) {
return self.element.bound.explicitBound;
return self.element2.bound.explicitBound;
}
return self;
}
Element? get sharedElement {
final self = this;
if (self is InterfaceType) {
return self.element2;
} else if (self is TypeParameterType) {
return self.element;
} else {
return null;
}
}
}

View file

@ -349,7 +349,7 @@ class Variables {
);
} else if (type is TypeParameterType) {
return TypeParameterTypeImpl(
element: type.element,
element: type.element2,
nullabilitySuffix: nullabilitySuffix,
);
} else {

View file

@ -597,7 +597,7 @@ class _ContextWithElement with EdgeTester {
String displayName,
) {
var type = decoratedType.type as TypeParameterTypeImpl;
expect(type.element, same(expectedElement));
expect(type.element2, same(expectedElement));
checkNullability(decoratedType.node, displayName);
}

View file

@ -384,12 +384,12 @@ class DecoratedTypeTest extends Object
expect(
((type.typeFormals[0].bound as InterfaceType).typeArguments[0]
as TypeParameterType)
.element,
.element2,
same(type.typeFormals[1]));
expect(
((type.typeFormals[2].bound as InterfaceType).typeArguments[0]
as TypeParameterType)
.element,
.element2,
same(type.typeFormals[1]));
}
@ -404,7 +404,7 @@ class DecoratedTypeTest extends Object
expect(
((type.parameters[0].type as InterfaceType).typeArguments[0]
as TypeParameterType)
.element,
.element2,
same(type.typeFormals[0]));
}
@ -419,7 +419,7 @@ class DecoratedTypeTest extends Object
expect(
((type.parameters[0].type as InterfaceType).typeArguments[0]
as TypeParameterType)
.element,
.element2,
same(type.typeFormals[0]));
}
@ -434,7 +434,7 @@ class DecoratedTypeTest extends Object
expect(
((type.parameters[0].type as InterfaceType).typeArguments[0]
as TypeParameterType)
.element,
.element2,
same(type.typeFormals[0]));
}
@ -449,7 +449,7 @@ class DecoratedTypeTest extends Object
expect(
((type.returnType as InterfaceType).typeArguments[0]
as TypeParameterType)
.element,
.element2,
same(type.typeFormals[0]));
}

View file

@ -1066,13 +1066,13 @@ typedef T F<T, U>(U u);
// type.
expect(decoratedType.typeFormals, isEmpty);
expect(decoratedType.returnType, same(decoratedTypeAnnotation('T F')));
expect(
(decoratedType.returnType!.type as TypeParameterType).element, same(t));
expect((decoratedType.returnType!.type as TypeParameterType).element2,
same(t));
expect(
decoratedType.returnType!.node, TypeMatcher<NullabilityNodeMutable>());
expect(
(decoratedType.positionalParameters![0]!.type as TypeParameterType)
.element,
.element2,
same(u));
expect(decoratedType.positionalParameters![0]!.node,
TypeMatcher<NullabilityNodeMutable>());
@ -1220,11 +1220,11 @@ void f(T Function<T, U>(U) x) {}
expect(decoratedType.typeFormals, hasLength(2));
var t = decoratedType.typeFormals![0];
var u = decoratedType.typeFormals![1];
expect(
(decoratedType.returnType!.type as TypeParameterType).element, same(t));
expect((decoratedType.returnType!.type as TypeParameterType).element2,
same(t));
expect(
(decoratedType.positionalParameters![0]!.type as TypeParameterType)
.element,
.element2,
same(u));
}
@ -1300,13 +1300,13 @@ typedef F = T Function<T, U>(U u);
var t = decoratedType.typeFormals![0];
var u = decoratedType.typeFormals![1];
expect(decoratedType.returnType, same(decoratedTypeAnnotation('T F')));
expect(
(decoratedType.returnType!.type as TypeParameterType).element, same(t));
expect((decoratedType.returnType!.type as TypeParameterType).element2,
same(t));
expect(
decoratedType.returnType!.node, TypeMatcher<NullabilityNodeMutable>());
expect(
(decoratedType.positionalParameters![0]!.type as TypeParameterType)
.element,
.element2,
same(u));
expect(decoratedType.positionalParameters![0]!.node,
TypeMatcher<NullabilityNodeMutable>());
@ -1327,13 +1327,13 @@ typedef F<T, U> = T Function(U u);
// type.
expect(decoratedType.typeFormals, isEmpty);
expect(decoratedType.returnType, same(decoratedTypeAnnotation('T F')));
expect(
(decoratedType.returnType!.type as TypeParameterType).element, same(t));
expect((decoratedType.returnType!.type as TypeParameterType).element2,
same(t));
expect(
decoratedType.returnType!.node, TypeMatcher<NullabilityNodeMutable>());
expect(
(decoratedType.positionalParameters![0]!.type as TypeParameterType)
.element,
.element2,
same(u));
expect(decoratedType.positionalParameters![0]!.node,
TypeMatcher<NullabilityNodeMutable>());