Skip to content

Commit a1955aa

Browse files
scheglovCommit Queue
authored andcommitted
Elements. Switch FunctionTypeImpl to typeParameters, remove typeFormals.
Change-Id: I6f190d53c6b13cab7d921b2bb895f1038c3f1cc8 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/438401 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]> Reviewed-by: Paul Berry <[email protected]>
1 parent 945cb32 commit a1955aa

File tree

19 files changed

+101
-98
lines changed

19 files changed

+101
-98
lines changed

pkg/analysis_server/lib/src/services/correction/dart/create_method_or_function.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ class CreateMethodOrFunction extends ResolvedCorrectionProducer {
161161
}
162162
if (parameterType is InterfaceType && parameterType.isDartCoreFunction) {
163163
return FunctionTypeImpl(
164-
typeFormals: const [],
164+
typeParameters: const [],
165165
parameters: const [],
166166
returnType: DynamicTypeImpl.instance,
167167
nullabilitySuffix: NullabilitySuffix.none,

pkg/analysis_server_plugin/lib/edit/dart/correction_producer.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ abstract class ResolvedCorrectionProducer
517517
var type = conditionalExpression.correspondingParameter?.type;
518518
if (type is InterfaceType && type.isDartCoreFunction) {
519519
return FunctionTypeImpl(
520-
typeFormals: const [],
520+
typeParameters: const [],
521521
parameters: const [],
522522
returnType: DynamicTypeImpl.instance,
523523
nullabilitySuffix: NullabilitySuffix.none,

pkg/analyzer/lib/src/dart/analysis/driver.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ testFineAfterLibraryAnalyzerHook;
110110
// TODO(scheglov): Clean up the list of implicitly analyzed files.
111111
class AnalysisDriver {
112112
/// The version of data format, should be incremented on every format change.
113-
static const int DATA_VERSION = 484;
113+
static const int DATA_VERSION = 487;
114114

115115
/// The number of exception contexts allowed to write. Once this field is
116116
/// zero, we stop writing any new exception contexts in this process.

pkg/analyzer/lib/src/dart/constant/evaluation.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -858,7 +858,7 @@ class ConstantVisitor extends UnifyingAstVisitor<Constant> {
858858

859859
TypeAliasElement? viaTypeAlias;
860860
if (typeElement is TypeAliasElementImpl) {
861-
if (constructorFunctionType.typeFormals.isNotEmpty &&
861+
if (constructorFunctionType.typeParameters.isNotEmpty &&
862862
!typeElement.isProperRename) {
863863
// The type alias is not a proper rename of the aliased class, so
864864
// the constructor tear-off is distinct from the associated
@@ -2169,7 +2169,7 @@ class ConstantVisitor extends UnifyingAstVisitor<Constant> {
21692169
return value;
21702170
}
21712171
var valueType = functionElement.type;
2172-
if (valueType.typeFormals.isNotEmpty) {
2172+
if (valueType.typeParameters.isNotEmpty) {
21732173
var typeArgumentTypes = node.typeArgumentTypes;
21742174
if (typeArgumentTypes != null && typeArgumentTypes.isNotEmpty) {
21752175
var instantiatedType = functionElement.type.instantiate(
@@ -2204,7 +2204,7 @@ class ConstantVisitor extends UnifyingAstVisitor<Constant> {
22042204
return value;
22052205
}
22062206
var valueType = functionElement.type;
2207-
if (valueType.typeFormals.isNotEmpty) {
2207+
if (valueType.typeParameters.isNotEmpty) {
22082208
var tearOffTypeArgumentTypes = node.tearOffTypeArgumentTypes;
22092209
if (tearOffTypeArgumentTypes != null &&
22102210
tearOffTypeArgumentTypes.isNotEmpty) {
@@ -2710,15 +2710,15 @@ class DartObjectComputer {
27102710
) {
27112711
var rawType = function.type;
27122712
if (rawType is FunctionTypeImpl) {
2713-
if (typeArguments.length != rawType.typeFormals.length) {
2713+
if (typeArguments.length != rawType.typeParameters.length) {
27142714
if (node is SimpleIdentifier) {
27152715
return InvalidConstant.forEntity(
27162716
entity: typeArgumentsErrorNode,
27172717
diagnosticCode:
27182718
CompileTimeErrorCode.WRONG_NUMBER_OF_TYPE_ARGUMENTS_FUNCTION,
27192719
arguments: [
27202720
node.name,
2721-
rawType.typeFormals.length,
2721+
rawType.typeParameters.length,
27222722
typeArguments.length,
27232723
],
27242724
);
@@ -2728,7 +2728,7 @@ class DartObjectComputer {
27282728
diagnosticCode:
27292729
CompileTimeErrorCode
27302730
.WRONG_NUMBER_OF_TYPE_ARGUMENTS_ANONYMOUS_FUNCTION,
2731-
arguments: [rawType.typeFormals.length, typeArguments.length],
2731+
arguments: [rawType.typeParameters.length, typeArguments.length],
27322732
);
27332733
}
27342734
var type = rawType.instantiate(typeArguments);

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,7 +1100,7 @@ class ConstructorFragmentImpl extends ExecutableFragmentImpl
11001100
FunctionTypeImpl get type {
11011101
// TODO(scheglov): Remove "element" in the breaking changes branch.
11021102
return _type ??= FunctionTypeImpl(
1103-
typeFormals: typeParameters,
1103+
typeParameters: typeParameters.map((f) => f.asElement2).toList(),
11041104
parameters: parameters.map((f) => f.asElement2).toList(),
11051105
returnType: returnType,
11061106
nullabilitySuffix: NullabilitySuffix.none,
@@ -2360,7 +2360,7 @@ abstract class ExecutableFragmentImpl extends _ExistingFragmentImpl
23602360
if (_type != null) return _type!;
23612361

23622362
return _type = FunctionTypeImpl(
2363-
typeFormals: typeParameters,
2363+
typeParameters: typeParameters.map((f) => f.asElement2).toList(),
23642364
parameters: parameters.map((f) => f.asElement2).toList(),
23652365
returnType: returnType,
23662366
nullabilitySuffix: NullabilitySuffix.none,
@@ -4076,7 +4076,7 @@ class GenericFunctionTypeFragmentImpl extends _ExistingFragmentImpl
40764076
if (_type != null) return _type!;
40774077

40784078
return _type = FunctionTypeImpl(
4079-
typeFormals: typeParameters,
4079+
typeParameters: typeParameters.map((f) => f.asElement2).toList(),
40804080
parameters: parameters.map((f) => f.asElement2).toList(),
40814081
returnType: returnType,
40824082
nullabilitySuffix:
@@ -9461,9 +9461,9 @@ class TypeAliasElementImpl extends TypeDefiningElementImpl
94619461
: nullabilitySuffix;
94629462

94639463
if (type is FunctionTypeImpl) {
9464-
return FunctionTypeImpl(
9465-
typeFormals: type.typeFormals,
9466-
parameters: type.parameters,
9464+
return FunctionTypeImpl.v2(
9465+
typeParameters: type.typeParameters,
9466+
formalParameters: type.parameters,
94679467
returnType: type.returnType,
94689468
nullabilitySuffix: resultNullability,
94699469
alias: InstantiatedTypeAliasElementImpl(
@@ -9507,7 +9507,7 @@ class TypeAliasElementImpl extends TypeDefiningElementImpl
95079507

95089508
FunctionTypeImpl _errorFunctionType(NullabilitySuffix nullabilitySuffix) {
95099509
return FunctionTypeImpl(
9510-
typeFormals: const [],
9510+
typeParameters: const [],
95119511
parameters: const [],
95129512
returnType: DynamicTypeImpl.instance,
95139513
nullabilitySuffix: nullabilitySuffix,
@@ -9659,6 +9659,11 @@ class TypeParameterElementImpl extends TypeDefiningElementImpl
96599659
}
96609660
}
96619661

9662+
factory TypeParameterElementImpl.synthetic({required String name}) {
9663+
var fragment = TypeParameterFragmentImpl.synthetic(name2: name);
9664+
return TypeParameterElementImpl(firstFragment: fragment, name3: name);
9665+
}
9666+
96629667
@override
96639668
TypeParameterElement get baseElement => this;
96649669

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ class GenericInferrer {
232232
// Since we're trying to infer the instantiation, we want to ignore type
233233
// formals as we check the parameters and return type.
234234
var inferFnType = FunctionTypeImpl(
235-
typeFormals: const [],
235+
typeParameters: const [],
236236
parameters: fnType.parameters,
237237
returnType: fnType.returnType,
238238
nullabilitySuffix: fnType.nullabilitySuffix,
@@ -343,7 +343,7 @@ class GenericInferrer {
343343
}
344344

345345
if (inferred is FunctionTypeImpl &&
346-
inferred.typeFormals.isNotEmpty &&
346+
inferred.typeParameters.isNotEmpty &&
347347
!genericMetadataIsEnabled &&
348348
_diagnosticReporter != null) {
349349
if (failAtError) {
@@ -357,15 +357,15 @@ class GenericInferrer {
357357
return null;
358358
}
359359

360-
var typeFormals = inferred.typeFormals;
361-
var typeFormalsStr = typeFormals.map(_elementStr).join(', ');
360+
var typeParameters = inferred.typeParameters;
361+
var typeParametersStr = typeParameters.map(_elementStr).join(', ');
362362
_diagnosticReporter.atEntity(
363363
errorEntity!,
364364
CompileTimeErrorCode.COULD_NOT_INFER,
365365
arguments: [
366366
name,
367367
' Inferred candidate type ${_typeStr(inferred)} has type parameters'
368-
' [$typeFormalsStr], but a function with'
368+
' [$typeParametersStr], but a function with'
369369
' type parameters cannot be used as a type argument.',
370370
],
371371
);
@@ -621,8 +621,8 @@ class GenericInferrer {
621621
}
622622
}
623623

624-
String _elementStr(FragmentImpl element) {
625-
return element.getDisplayString();
624+
String _elementStr(ElementImpl element) {
625+
return element.displayString2();
626626
}
627627

628628
String _formatError(

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,8 @@ class GreatestLowerBoundHelper {
253253
/// https://github.com/dart-lang/language
254254
/// See `resources/type-system/upper-lower-bounds.md`
255255
TypeImpl _functionType(FunctionTypeImpl f, FunctionTypeImpl g) {
256-
var fTypeFormals = f.typeFormals;
257-
var gTypeFormals = g.typeFormals;
256+
var fTypeFormals = f.typeParameters;
257+
var gTypeFormals = g.typeParameters;
258258

259259
// The number of type parameters must be the same.
260260
// Otherwise the result is `Never`.

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1083,7 +1083,8 @@ class InheritanceManager3 {
10831083
nameOffset: -1,
10841084
);
10851085
resultFragment.enclosingElement3 = targetClass.firstFragment;
1086-
resultFragment.typeParameters = resultType.typeFormals;
1086+
resultFragment.typeParameters =
1087+
resultType.typeParameters.map((e) => e.firstFragment).toList();
10871088
resultFragment.returnType = resultType.returnType;
10881089
// TODO(scheglov): check if can type cast instead
10891090
resultFragment.parameters =

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

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import 'package:analyzer/src/dart/element/member.dart';
1515
import 'package:analyzer/src/dart/element/type_algebra.dart';
1616
import 'package:analyzer/src/dart/element/type_system.dart';
1717
import 'package:analyzer/src/utilities/extensions/collection.dart';
18-
import 'package:analyzer/src/utilities/extensions/element.dart';
1918
import 'package:collection/collection.dart';
2019

2120
/// Returns a [List] of fixed length with given types.
@@ -92,9 +91,8 @@ class FunctionTypeImpl extends TypeImpl
9291
@override
9392
final TypeImpl returnType;
9493

95-
/// The formal type parameters of this generic function; for example,
96-
/// `<T> T -> T`.
97-
final List<TypeParameterFragmentImpl> typeFormals;
94+
@override
95+
final List<TypeParameterElementImpl> typeParameters;
9896

9997
/// A list containing the parameters elements of this type of function.
10098
///
@@ -126,7 +124,7 @@ class FunctionTypeImpl extends TypeImpl
126124
final List<FormalParameterElementMixin> sortedNamedParameters;
127125

128126
factory FunctionTypeImpl({
129-
required List<TypeParameterFragmentImpl> typeFormals,
127+
required List<TypeParameterElementImpl> typeParameters,
130128
required List<FormalParameterElementMixin> parameters,
131129
required TypeImpl returnType,
132130
required NullabilitySuffix nullabilitySuffix,
@@ -176,7 +174,7 @@ class FunctionTypeImpl extends TypeImpl
176174
);
177175
}
178176
return FunctionTypeImpl._(
179-
typeFormals: typeFormals,
177+
typeParameters: typeParameters,
180178
parameters: parameters,
181179
returnType: returnType,
182180
nullabilitySuffix: nullabilitySuffix,
@@ -195,7 +193,7 @@ class FunctionTypeImpl extends TypeImpl
195193
InstantiatedTypeAliasElementImpl? alias,
196194
}) {
197195
return FunctionTypeImpl(
198-
typeFormals: typeParameters.map((e) => e.asElement).toList(),
196+
typeParameters: typeParameters,
199197
parameters: formalParameters,
200198
returnType: returnType,
201199
nullabilitySuffix: nullabilitySuffix,
@@ -204,7 +202,7 @@ class FunctionTypeImpl extends TypeImpl
204202
}
205203

206204
FunctionTypeImpl._({
207-
required this.typeFormals,
205+
required this.typeParameters,
208206
required this.parameters,
209207
required this.returnType,
210208
required this.nullabilitySuffix,
@@ -254,10 +252,6 @@ class FunctionTypeImpl extends TypeImpl
254252
List<FormalParameterElementMixin> get sortedNamedParametersShared =>
255253
sortedNamedParameters;
256254

257-
@override
258-
List<TypeParameterElementImpl> get typeParameters =>
259-
typeFormals.map((fragment) => fragment.element).toList();
260-
261255
@override
262256
List<TypeParameterElementImpl> get typeParametersShared => typeParameters;
263257

@@ -272,13 +266,13 @@ class FunctionTypeImpl extends TypeImpl
272266
return false;
273267
}
274268

275-
if (other.typeFormals.length != typeFormals.length) {
269+
if (other.typeParameters.length != typeParameters.length) {
276270
return false;
277271
}
278272
// `<T>T -> T` should be equal to `<U>U -> U`
279273
// To test this, we instantiate both types with the same (unique) type
280274
// variables, and see if the result is equal.
281-
if (typeFormals.isNotEmpty) {
275+
if (typeParameters.isNotEmpty) {
282276
var freshVariables = FunctionTypeImpl.relateTypeFormals(
283277
this,
284278
other,
@@ -330,7 +324,7 @@ class FunctionTypeImpl extends TypeImpl
330324

331325
return FunctionTypeImpl(
332326
returnType: substitution.substituteType(returnType),
333-
typeFormals: const [],
327+
typeParameters: const [],
334328
parameters:
335329
parameters
336330
.map((p) => ParameterMember.from2(p, substitution))
@@ -341,8 +335,8 @@ class FunctionTypeImpl extends TypeImpl
341335

342336
@override
343337
bool referencesAny(Set<TypeParameterElementImpl> parameters) {
344-
if (typeFormals.any((element) {
345-
assert(!parameters.contains(element.asElement2));
338+
if (typeParameters.any((element) {
339+
assert(!parameters.contains(element));
346340

347341
var bound = element.bound;
348342
if (bound != null && bound.referencesAny(parameters)) {
@@ -369,7 +363,7 @@ class FunctionTypeImpl extends TypeImpl
369363
TypeImpl withNullability(NullabilitySuffix nullabilitySuffix) {
370364
if (this.nullabilitySuffix == nullabilitySuffix) return this;
371365
return FunctionTypeImpl._(
372-
typeFormals: typeFormals,
366+
typeParameters: typeParameters,
373367
parameters: parameters,
374368
returnType: returnType,
375369
nullabilitySuffix: nullabilitySuffix,
@@ -381,7 +375,7 @@ class FunctionTypeImpl extends TypeImpl
381375
}
382376

383377
int _computeHashCode() {
384-
if (typeFormals.isNotEmpty) {
378+
if (typeParameters.isNotEmpty) {
385379
// Two generic function types are considered equivalent even if their type
386380
// formals have different names, so we need to normalize to a standard set
387381
// of type formals before taking the hash code.
@@ -391,7 +385,7 @@ class FunctionTypeImpl extends TypeImpl
391385
// parameter bounds will receive the same hash code; this should be rare
392386
// enough that it won't be a problem.
393387
return instantiate([
394-
for (var i = 0; i < typeFormals.length; i++)
388+
for (var i = 0; i < typeParameters.length; i++)
395389
TypeParameterTypeImpl(
396390
element: TypeParameterFragmentImpl.synthetic(name2: 'T$i').element,
397391
nullabilitySuffix: NullabilitySuffix.none,

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ FunctionTypeImpl replaceTypeParameters(
6666
FunctionTypeImpl type,
6767
List<TypeParameterElementImpl> newTypeParameters,
6868
) {
69-
assert(newTypeParameters.length == type.typeFormals.length);
69+
assert(newTypeParameters.length == type.typeParameters.length);
7070
if (newTypeParameters.isEmpty) {
7171
return type;
7272
}
@@ -75,7 +75,10 @@ FunctionTypeImpl replaceTypeParameters(
7575
newTypeParameters
7676
.map((e) => e.instantiate(nullabilitySuffix: NullabilitySuffix.none))
7777
.toList();
78-
var substitution = Substitution.fromPairs(type.typeFormals, typeArguments);
78+
var substitution = Substitution.fromPairs2(
79+
type.typeParameters,
80+
typeArguments,
81+
);
7982

8083
FormalParameterElementMixin transformParameter(
8184
FormalParameterElementMixin p,

0 commit comments

Comments
 (0)