Skip to content

Commit 351ccc5

Browse files
author
John Messerly
committed
fix generic function expressions, part of #25175
[email protected] Review URL: https://codereview.chromium.org/1579303002 .
1 parent 666964b commit 351ccc5

File tree

3 files changed

+23
-21
lines changed

3 files changed

+23
-21
lines changed

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

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -216,22 +216,8 @@ class FunctionTypeImpl extends TypeImpl implements FunctionType {
216216
* Private constructor.
217217
*/
218218
FunctionTypeImpl._(TypeParameterizedElement element, String name,
219-
this.prunedTypedefs, List<DartType> typeArguments, this._isInstantiated)
220-
: super(element, name) {
221-
if (typeArguments == null) {
222-
// TODO(jmesserly): reuse TypeParameterTypeImpl.getTypes once we can
223-
// make it generic, which will allow it to return List<DartType> instead
224-
// of List<TypeParameterType>.
225-
if (typeParameters.isEmpty) {
226-
typeArguments = DartType.EMPTY_LIST;
227-
} else {
228-
typeArguments = new List<DartType>.from(
229-
typeParameters.map((t) => t.type),
230-
growable: false);
231-
}
232-
}
233-
_typeArguments = typeArguments;
234-
}
219+
this.prunedTypedefs, this._typeArguments, this._isInstantiated)
220+
: super(element, name);
235221

236222
/**
237223
* Return the base parameter elements of this function element.
@@ -493,7 +479,21 @@ class FunctionTypeImpl extends TypeImpl implements FunctionType {
493479
/**
494480
* A list containing the actual types of the type arguments.
495481
*/
496-
List<DartType> get typeArguments => _typeArguments;
482+
List<DartType> get typeArguments {
483+
if (_typeArguments == null) {
484+
// TODO(jmesserly): reuse TypeParameterTypeImpl.getTypes once we can
485+
// make it generic, which will allow it to return List<DartType> instead
486+
// of List<TypeParameterType>.
487+
if (typeParameters.isEmpty) {
488+
_typeArguments = DartType.EMPTY_LIST;
489+
} else {
490+
_typeArguments = new List<DartType>.from(
491+
typeParameters.map((t) => t.type),
492+
growable: false);
493+
}
494+
}
495+
return _typeArguments;
496+
}
497497

498498
@override
499499
List<TypeParameterElement> get typeParameters {

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3100,10 +3100,6 @@ class ElementBuilder extends RecursiveAstVisitor<Object> {
31003100
if (_functionTypesToFix != null) {
31013101
_functionTypesToFix.add(element);
31023102
} else {
3103-
// TODO(jmesserly): for local functions inside of top-level generic
3104-
// functions, this is probably not right. The function type should be set
3105-
// after the enclosingElement is set, otherwise we won't be able to
3106-
// substitute those type parameters later.
31073103
element.type = new FunctionTypeImpl(element);
31083104
}
31093105
element.hasImplicitReturnType = true;

pkg/analyzer/test/generated/resolver_test.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13043,6 +13043,8 @@ var topG = topF;
1304313043
void test/*<S>*/(/*=T*/ pf/*<T>*/(/*=T*/ e)) {
1304413044
var c = new C<int>();
1304513045
/*=T*/ lf/*<T>*/(/*=T*/ e) => null;
13046+
13047+
var lambdaCall = (/*<E>*/(/*=E*/ e) => e)/*<int>*/(3);
1304613048
var methodCall = (c.f)/*<int>*/(3);
1304713049
var staticCall = (C.g)/*<int>*/(3);
1304813050
var staticFieldCall = (C.h)/*<int>*/(3);
@@ -13059,6 +13061,7 @@ void test/*<S>*/(/*=T*/ pf/*<T>*/(/*=T*/ e)) {
1305913061
expect(_findIdentifier('topFieldCall').staticType.toString(), "int");
1306013062
expect(_findIdentifier('localCall').staticType.toString(), "int");
1306113063
expect(_findIdentifier('paramCall').staticType.toString(), "int");
13064+
expect(_findIdentifier('lambdaCall').staticType.toString(), "int");
1306213065
}
1306313066

1306413067
void fail_genericMethod_functionExpressionInvocation_inferred() {
@@ -13074,6 +13077,8 @@ var topG = topF;
1307413077
void test/*<S>*/(/*=T*/ pf/*<T>*/(/*=T*/ e)) {
1307513078
var c = new C<int>();
1307613079
/*=T*/ lf/*<T>*/(/*=T*/ e) => null;
13080+
13081+
var lambdaCall = (/*<E>*/(/*=E*/ e) => e)(3);
1307713082
var methodCall = (c.f)(3);
1307813083
var staticCall = (C.g)(3);
1307913084
var staticFieldCall = (C.h)(3);
@@ -13090,6 +13095,7 @@ void test/*<S>*/(/*=T*/ pf/*<T>*/(/*=T*/ e)) {
1309013095
expect(_findIdentifier('topFieldCall').staticType.toString(), "int");
1309113096
expect(_findIdentifier('localCall').staticType.toString(), "int");
1309213097
expect(_findIdentifier('paramCall').staticType.toString(), "int");
13098+
expect(_findIdentifier('lambdaCall').staticType.toString(), "int");
1309313099
}
1309413100

1309513101
void fail_genericMethod_functionInvocation_inferred() {

0 commit comments

Comments
 (0)