Skip to content

Commit 3cdfd90

Browse files
scheglovcommit-bot@chromium.org
authored andcommitted
Revert "Deprecate TypeParameterTypeImpl.getTypes()"
This reverts commit aa0bd58. Reason for revert: https://dart-review.googlesource.com/c/sdk/+/119587 was reverted. So, this change now breaks bots because of deprecated getTypes() usage in DDC. Original change's description: > Deprecate TypeParameterTypeImpl.getTypes() > > Presubmit in google3 looks green. > https://test.corp.google.com/ui#id=OCL:272795904:BASE:272795986:1570167013170:de765f48 > > Change-Id: I9ef25658a8a8e806b7fcc23b8d9d4e1d297ebff8 > Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/120003 > Reviewed-by: Brian Wilkerson <brianwilkerson@google.com> > Commit-Queue: Konstantin Shcheglov <scheglov@google.com> TBR=scheglov@google.com,brianwilkerson@google.com Change-Id: I3dc1a700f496c25eea9898de658a31138bbedcce No-Presubmit: true No-Tree-Checks: true No-Try: true Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/120141 Reviewed-by: Konstantin Shcheglov <scheglov@google.com> Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
1 parent aa0bd58 commit 3cdfd90

File tree

12 files changed

+121
-143
lines changed

12 files changed

+121
-143
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1207,7 +1207,6 @@ abstract class FunctionTypeAliasElement
12071207
/// typedef F<T> = void Function<U>(T, U);
12081208
/// then a single type argument should be provided, and it will be substituted
12091209
/// for T.
1210-
@deprecated
12111210
FunctionType instantiate(List<DartType> argumentTypes);
12121211

12131212
/// Produces the function type resulting from instantiating this typedef with

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

Lines changed: 49 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1312,17 +1312,16 @@ class ClassElementImpl extends AbstractClassElementImpl
13121312
// to produce constructors for this class. We want to be robust in the
13131313
// face of errors, so drop any extra type arguments and fill in any missing
13141314
// ones with `dynamic`.
1315-
var superTypeParameters = supertype.typeParameters;
1315+
List<DartType> parameterTypes =
1316+
TypeParameterTypeImpl.getTypes(supertype.typeParameters);
13161317
List<DartType> argumentTypes = new List<DartType>.filled(
1317-
superTypeParameters.length, DynamicTypeImpl.instance);
1318+
parameterTypes.length, DynamicTypeImpl.instance);
13181319
for (int i = 0; i < supertype.typeArguments.length; i++) {
13191320
if (i >= argumentTypes.length) {
13201321
break;
13211322
}
13221323
argumentTypes[i] = supertype.typeArguments[i];
13231324
}
1324-
var substitution =
1325-
Substitution.fromPairs(superTypeParameters, argumentTypes);
13261325

13271326
// Now create an implicit constructor for every constructor found above,
13281327
// substituting type parameters as appropriate.
@@ -1354,7 +1353,7 @@ class ClassElementImpl extends AbstractClassElementImpl
13541353
implicitParameter.parameterKind = superParameter.parameterKind;
13551354
implicitParameter.isSynthetic = true;
13561355
implicitParameter.type =
1357-
substitution.substituteType(superParameter.type);
1356+
superParameter.type.substitute2(argumentTypes, parameterTypes);
13581357
implicitParameters[i] = implicitParameter;
13591358
}
13601359
implicitConstructor.parameters = implicitParameters;
@@ -6444,33 +6443,18 @@ class GenericTypeAliasElementImpl extends ElementImpl
64446443

64456444
@override
64466445
FunctionType instantiate(List<DartType> argumentTypes) {
6447-
return instantiate2(
6448-
typeArguments: argumentTypes,
6449-
nullabilitySuffix: NullabilitySuffix.star,
6450-
);
6446+
return doInstantiate(this, argumentTypes);
64516447
}
64526448

64536449
@override
64546450
FunctionType instantiate2({
64556451
@required List<DartType> typeArguments,
64566452
@required NullabilitySuffix nullabilitySuffix,
64576453
}) {
6458-
if (function == null) {
6459-
return null;
6460-
}
6461-
6462-
if (typeArguments.length != typeParameters.length) {
6463-
throw new ArgumentError(
6464-
"typeArguments.length (${typeArguments.length}) != "
6465-
"typeParameters.length (${typeParameters.length})");
6466-
}
6467-
6468-
var substitution = Substitution.fromPairs(typeParameters, typeArguments);
6469-
var type = substitution.substituteType(function.type) as FunctionType;
64706454
return FunctionTypeImpl.synthetic(
6471-
type.returnType,
6472-
type.typeFormals,
6473-
type.parameters,
6455+
returnType,
6456+
typeParameters,
6457+
parameters,
64746458
element: this,
64756459
typeArguments: typeArguments,
64766460
nullabilitySuffix: nullabilitySuffix,
@@ -6483,6 +6467,47 @@ class GenericTypeAliasElementImpl extends ElementImpl
64836467
safelyVisitChildren(typeParameters, visitor);
64846468
function?.accept(visitor);
64856469
}
6470+
6471+
static FunctionType doInstantiate(
6472+
FunctionTypeAliasElement element, List<DartType> argumentTypes) {
6473+
if (argumentTypes.length != element.typeParameters.length) {
6474+
throw new ArgumentError('Wrong number of type arguments supplied');
6475+
}
6476+
if (element.typeParameters.isEmpty) return element.function.type;
6477+
return typeAfterSubstitution(element, argumentTypes);
6478+
}
6479+
6480+
/// Return the type of the function defined by this typedef after substituting
6481+
/// the given [typeArguments] for the type parameters defined for this typedef
6482+
/// (but not the type parameters defined by the function). If the number of
6483+
/// [typeArguments] does not match the number of type parameters, then
6484+
/// `dynamic` will be used in place of each of the type arguments.
6485+
static FunctionType typeAfterSubstitution(
6486+
FunctionTypeAliasElement element, List<DartType> typeArguments) {
6487+
GenericFunctionTypeElement function = element.function;
6488+
if (function == null) {
6489+
return null;
6490+
}
6491+
FunctionType functionType = function.type;
6492+
6493+
List<TypeParameterElement> parameterElements = element.typeParameters;
6494+
int parameterCount = parameterElements.length;
6495+
6496+
if (typeArguments == null ||
6497+
parameterElements.length != typeArguments.length) {
6498+
DartType dynamicType = element.context.typeProvider.dynamicType;
6499+
typeArguments = new List<DartType>.filled(parameterCount, dynamicType);
6500+
}
6501+
6502+
if (element is GenericTypeAliasElementImpl && element.linkedNode != null) {
6503+
return Substitution.fromPairs(parameterElements, typeArguments)
6504+
.substituteType(functionType);
6505+
}
6506+
6507+
List<DartType> parameterTypes =
6508+
TypeParameterTypeImpl.getTypes(parameterElements);
6509+
return functionType.substitute2(typeArguments, parameterTypes);
6510+
}
64866511
}
64876512

64886513
/// A concrete implementation of a [HideElementCombinator].

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -779,7 +779,6 @@ class FunctionTypeAliasElementHandle extends ElementHandle
779779
@override
780780
FunctionTypeAlias computeNode() => actualElement.computeNode();
781781

782-
@deprecated
783782
@override
784783
FunctionType instantiate(List<DartType> argumentTypes) =>
785784
actualElement.instantiate(argumentTypes);
@@ -838,7 +837,6 @@ class GenericTypeAliasElementHandle extends ElementHandle
838837
@override
839838
FunctionTypeAlias computeNode() => actualElement.computeNode();
840839

841-
@deprecated
842840
@override
843841
FunctionType instantiate(List<DartType> argumentTypes) =>
844842
actualElement.instantiate(argumentTypes);

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2856,7 +2856,6 @@ class TypeParameterTypeImpl extends TypeImpl implements TypeParameterType {
28562856
* Return a list containing the type parameter types defined by the given
28572857
* array of type parameter elements ([typeParameters]).
28582858
*/
2859-
@deprecated
28602859
static List<TypeParameterType> getTypes(
28612860
List<TypeParameterElement> typeParameters) {
28622861
int count = typeParameters.length;

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

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -42,43 +42,6 @@ FreshTypeParameters getFreshTypeParameters(
4242
return new FreshTypeParameters(freshParameters, substitution);
4343
}
4444

45-
/// Given a generic function [type] of a class member (so that it does not
46-
/// carry its element and type arguments), substitute its type parameters with
47-
/// the [newTypeParameters] in the formal parameters and return type.
48-
FunctionType replaceTypeParameters(
49-
FunctionTypeImpl type,
50-
List<TypeParameterElement> newTypeParameters,
51-
) {
52-
assert(newTypeParameters.length == type.typeFormals.length);
53-
if (newTypeParameters.isEmpty) {
54-
return type;
55-
}
56-
57-
var typeArguments = newTypeParameters
58-
.map((e) => e.instantiate(nullabilitySuffix: type.nullabilitySuffix))
59-
.toList();
60-
var substitution = Substitution.fromPairs(type.typeFormals, typeArguments);
61-
62-
ParameterElement transformParameter(ParameterElement p) {
63-
var type = p.type;
64-
var newType = substitution.substituteType(type);
65-
if (identical(newType, type)) return p;
66-
return ParameterElementImpl.synthetic(
67-
p.name,
68-
newType,
69-
// ignore: deprecated_member_use_from_same_package
70-
p.parameterKind,
71-
)..isExplicitlyCovariant = p.isCovariant;
72-
}
73-
74-
return FunctionTypeImpl.synthetic(
75-
substitution.substituteType(type.returnType),
76-
newTypeParameters,
77-
type.parameters.map(transformParameter).toList(),
78-
nullabilitySuffix: type.nullabilitySuffix,
79-
);
80-
}
81-
8245
/// Returns a type where all occurrences of the given type parameters have been
8346
/// replaced with the corresponding types.
8447
///

pkg/analyzer/lib/src/error/type_arguments_verifier.dart

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,8 @@ class TypeArgumentsVerifier {
308308
var genericType = node.function.staticType;
309309
var instantiatedType = node.staticInvokeType;
310310
if (genericType is FunctionType && instantiatedType is FunctionType) {
311-
var fnTypeParams = genericType.typeFormals;
311+
var fnTypeParams =
312+
TypeParameterTypeImpl.getTypes(genericType.typeFormals);
312313
var typeArgs = typeArgumentList.map((t) => t.type).toList();
313314

314315
// If the amount mismatches, clean up the lists to be substitutable. The
@@ -337,13 +338,8 @@ class TypeArgumentsVerifier {
337338
continue;
338339
}
339340

340-
var rawBound = fnTypeParams[i].bound;
341-
if (rawBound == null) {
342-
continue;
343-
}
344-
345-
var substitution = Substitution.fromPairs(fnTypeParams, typeArgs);
346-
var bound = substitution.substituteType(rawBound);
341+
DartType bound =
342+
fnTypeParams[i].bound.substitute2(typeArgs, fnTypeParams);
347343
if (!_typeSystem.isSubtypeOf(argType, bound)) {
348344
_errorReporter.reportTypeErrorForNode(
349345
CompileTimeErrorCode.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS,

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

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6208,11 +6208,9 @@ class TypeNameResolver {
62086208
}
62096209
}
62106210
if (element is GenericTypeAliasElementImpl) {
6211-
type = element.instantiate2(
6212-
typeArguments: typeArguments,
6213-
nullabilitySuffix: _getNullability(node.question != null),
6214-
);
6215-
type ??= dynamicType;
6211+
type = GenericTypeAliasElementImpl.typeAfterSubstitution(
6212+
element, typeArguments) ??
6213+
dynamicType;
62166214
} else {
62176215
type = typeSystem.instantiateType(type, typeArguments);
62186216
}
@@ -6221,14 +6219,14 @@ class TypeNameResolver {
62216219
);
62226220
} else {
62236221
if (element is GenericTypeAliasElementImpl) {
6224-
var typeArguments = typeSystem.instantiateTypeFormalsToBounds(
6225-
element.typeParameters,
6226-
);
6227-
type = element.instantiate2(
6228-
typeArguments: typeArguments,
6229-
nullabilitySuffix: _getNullability(node.question != null),
6222+
List<DartType> typeArguments =
6223+
typeSystem.instantiateTypeFormalsToBounds2(element);
6224+
type = GenericTypeAliasElementImpl.typeAfterSubstitution(
6225+
element, typeArguments) ??
6226+
dynamicType;
6227+
type = (type as TypeImpl).withNullability(
6228+
_getNullability(node.question != null),
62306229
);
6231-
type ??= dynamicType;
62326230
} else {
62336231
type = typeSystem.instantiateToBounds(type);
62346232
}

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1249,11 +1249,8 @@ class GenericInferrer {
12491249

12501250
// Since we're trying to infer the instantiation, we want to ignore type
12511251
// formals as we check the parameters and return type.
1252-
var inferFnType = FunctionTypeImpl.synthetic(
1253-
fnType.returnType,
1254-
const [],
1255-
fnType.parameters,
1256-
);
1252+
var inferFnType =
1253+
fnType.instantiate(TypeParameterTypeImpl.getTypes(fnType.typeFormals));
12571254
tryMatchSubtypeOf(inferFnType, contextType, origin, covariant: true);
12581255
}
12591256

@@ -2690,6 +2687,16 @@ abstract class TypeSystem implements public.TypeSystem {
26902687
}
26912688
}
26922689

2690+
/**
2691+
* Given a [DartType] type, return the [DartType]s corresponding
2692+
* to its formal type parameters (if any).
2693+
*
2694+
* @param type the type whose type arguments are to be returned
2695+
* @return the type arguments associated with the given type
2696+
*/
2697+
List<DartType> typeFormalsAsTypes(DartType type) =>
2698+
TypeParameterTypeImpl.getTypes(typeFormalsAsElements(type));
2699+
26932700
/**
26942701
* Compute the least upper bound of function types [f] and [g].
26952702
*

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

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ class LazyAst {
1919
static const _hasOverrideInferenceKey = 'lazyAst_hasOverrideInference';
2020
static const _inheritsCovariantKey = 'lazyAst_isCovariant';
2121
static const _isSimplyBoundedKey = 'lazyAst_simplyBounded';
22-
static const _rawFunctionTypeKey = 'lazyAst_rawFunctionType';
2322
static const _returnTypeKey = 'lazyAst_returnType';
2423
static const _typeInferenceErrorKey = 'lazyAst_typeInferenceError';
2524
static const _typeKey = 'lazyAst_type';
@@ -40,10 +39,6 @@ class LazyAst {
4039
return node.getProperty(_inheritsCovariantKey) ?? false;
4140
}
4241

43-
static DartType getRawFunctionType(AstNode node) {
44-
return node.getProperty(_rawFunctionTypeKey);
45-
}
46-
4742
static DartType getReturnType(AstNode node) {
4843
return node.getProperty(_returnTypeKey);
4944
}
@@ -80,10 +75,6 @@ class LazyAst {
8075
node.setProperty(_hasOverrideInferenceKey, true);
8176
}
8277

83-
static void setRawFunctionType(AstNode node, DartType type) {
84-
node.setProperty(_rawFunctionTypeKey, type);
85-
}
86-
8778
static void setReturnType(AstNode node, DartType type) {
8879
node.setProperty(_returnTypeKey, type);
8980
}

0 commit comments

Comments
 (0)