Skip to content

Commit aa0bd58

Browse files
scheglovcommit-bot@chromium.org
authored andcommitted
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>
1 parent 42a1ba7 commit aa0bd58

File tree

12 files changed

+143
-121
lines changed

12 files changed

+143
-121
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,6 +1207,7 @@ 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
12101211
FunctionType instantiate(List<DartType> argumentTypes);
12111212

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

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

Lines changed: 24 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1312,16 +1312,17 @@ 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-
List<DartType> parameterTypes =
1316-
TypeParameterTypeImpl.getTypes(supertype.typeParameters);
1315+
var superTypeParameters = supertype.typeParameters;
13171316
List<DartType> argumentTypes = new List<DartType>.filled(
1318-
parameterTypes.length, DynamicTypeImpl.instance);
1317+
superTypeParameters.length, DynamicTypeImpl.instance);
13191318
for (int i = 0; i < supertype.typeArguments.length; i++) {
13201319
if (i >= argumentTypes.length) {
13211320
break;
13221321
}
13231322
argumentTypes[i] = supertype.typeArguments[i];
13241323
}
1324+
var substitution =
1325+
Substitution.fromPairs(superTypeParameters, argumentTypes);
13251326

13261327
// Now create an implicit constructor for every constructor found above,
13271328
// substituting type parameters as appropriate.
@@ -1353,7 +1354,7 @@ class ClassElementImpl extends AbstractClassElementImpl
13531354
implicitParameter.parameterKind = superParameter.parameterKind;
13541355
implicitParameter.isSynthetic = true;
13551356
implicitParameter.type =
1356-
superParameter.type.substitute2(argumentTypes, parameterTypes);
1357+
substitution.substituteType(superParameter.type);
13571358
implicitParameters[i] = implicitParameter;
13581359
}
13591360
implicitConstructor.parameters = implicitParameters;
@@ -6443,18 +6444,33 @@ class GenericTypeAliasElementImpl extends ElementImpl
64436444

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

64496453
@override
64506454
FunctionType instantiate2({
64516455
@required List<DartType> typeArguments,
64526456
@required NullabilitySuffix nullabilitySuffix,
64536457
}) {
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;
64546470
return FunctionTypeImpl.synthetic(
6455-
returnType,
6456-
typeParameters,
6457-
parameters,
6471+
type.returnType,
6472+
type.typeFormals,
6473+
type.parameters,
64586474
element: this,
64596475
typeArguments: typeArguments,
64606476
nullabilitySuffix: nullabilitySuffix,
@@ -6467,47 +6483,6 @@ class GenericTypeAliasElementImpl extends ElementImpl
64676483
safelyVisitChildren(typeParameters, visitor);
64686484
function?.accept(visitor);
64696485
}
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-
}
65116486
}
65126487

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

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

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

782+
@deprecated
782783
@override
783784
FunctionType instantiate(List<DartType> argumentTypes) =>
784785
actualElement.instantiate(argumentTypes);
@@ -837,6 +838,7 @@ class GenericTypeAliasElementHandle extends ElementHandle
837838
@override
838839
FunctionTypeAlias computeNode() => actualElement.computeNode();
839840

841+
@deprecated
840842
@override
841843
FunctionType instantiate(List<DartType> argumentTypes) =>
842844
actualElement.instantiate(argumentTypes);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2856,6 +2856,7 @@ 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
28592860
static List<TypeParameterType> getTypes(
28602861
List<TypeParameterElement> typeParameters) {
28612862
int count = typeParameters.length;

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,43 @@ 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+
4582
/// Returns a type where all occurrences of the given type parameters have been
4683
/// replaced with the corresponding types.
4784
///

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

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

315314
// If the amount mismatches, clean up the lists to be substitutable. The
@@ -338,8 +337,13 @@ class TypeArgumentsVerifier {
338337
continue;
339338
}
340339

341-
DartType bound =
342-
fnTypeParams[i].bound.substitute2(typeArgs, fnTypeParams);
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);
343347
if (!_typeSystem.isSubtypeOf(argType, bound)) {
344348
_errorReporter.reportTypeErrorForNode(
345349
CompileTimeErrorCode.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS,

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6208,9 +6208,11 @@ class TypeNameResolver {
62086208
}
62096209
}
62106210
if (element is GenericTypeAliasElementImpl) {
6211-
type = GenericTypeAliasElementImpl.typeAfterSubstitution(
6212-
element, typeArguments) ??
6213-
dynamicType;
6211+
type = element.instantiate2(
6212+
typeArguments: typeArguments,
6213+
nullabilitySuffix: _getNullability(node.question != null),
6214+
);
6215+
type ??= dynamicType;
62146216
} else {
62156217
type = typeSystem.instantiateType(type, typeArguments);
62166218
}
@@ -6219,14 +6221,14 @@ class TypeNameResolver {
62196221
);
62206222
} else {
62216223
if (element is GenericTypeAliasElementImpl) {
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),
6224+
var typeArguments = typeSystem.instantiateTypeFormalsToBounds(
6225+
element.typeParameters,
6226+
);
6227+
type = element.instantiate2(
6228+
typeArguments: typeArguments,
6229+
nullabilitySuffix: _getNullability(node.question != null),
62296230
);
6231+
type ??= dynamicType;
62306232
} else {
62316233
type = typeSystem.instantiateToBounds(type);
62326234
}

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

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1249,8 +1249,11 @@ 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 =
1253-
fnType.instantiate(TypeParameterTypeImpl.getTypes(fnType.typeFormals));
1252+
var inferFnType = FunctionTypeImpl.synthetic(
1253+
fnType.returnType,
1254+
const [],
1255+
fnType.parameters,
1256+
);
12541257
tryMatchSubtypeOf(inferFnType, contextType, origin, covariant: true);
12551258
}
12561259

@@ -2687,16 +2690,6 @@ abstract class TypeSystem implements public.TypeSystem {
26872690
}
26882691
}
26892692

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-
27002693
/**
27012694
* Compute the least upper bound of function types [f] and [g].
27022695
*

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ 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';
2223
static const _returnTypeKey = 'lazyAst_returnType';
2324
static const _typeInferenceErrorKey = 'lazyAst_typeInferenceError';
2425
static const _typeKey = 'lazyAst_type';
@@ -39,6 +40,10 @@ class LazyAst {
3940
return node.getProperty(_inheritsCovariantKey) ?? false;
4041
}
4142

43+
static DartType getRawFunctionType(AstNode node) {
44+
return node.getProperty(_rawFunctionTypeKey);
45+
}
46+
4247
static DartType getReturnType(AstNode node) {
4348
return node.getProperty(_returnTypeKey);
4449
}
@@ -75,6 +80,10 @@ class LazyAst {
7580
node.setProperty(_hasOverrideInferenceKey, true);
7681
}
7782

83+
static void setRawFunctionType(AstNode node, DartType type) {
84+
node.setProperty(_rawFunctionTypeKey, type);
85+
}
86+
7887
static void setReturnType(AstNode node, DartType type) {
7988
node.setProperty(_returnTypeKey, type);
8089
}

0 commit comments

Comments
 (0)