Skip to content

Commit bd1ffcb

Browse files
scheglovcommit-bot@chromium.org
authored andcommitted
Support for type formals in function LUB.
R=brianwilkerson@google.com, paulberry@google.com Change-Id: I5576db2279810bf1e4fc4a73870f60ca50166ee7 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/106924 Reviewed-by: Brian Wilkerson <brianwilkerson@google.com> Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
1 parent 8a94b6f commit bd1ffcb

File tree

6 files changed

+387
-132
lines changed

6 files changed

+387
-132
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1212,14 +1212,18 @@ abstract class FunctionTypeImpl extends TypeImpl implements FunctionType {
12121212
variables1.add(variable1);
12131213
variables2.add(variable2);
12141214
variablesFresh.add(variableFresh);
1215+
12151216
DartType bound1 = p1.bound ?? DynamicTypeImpl.instance;
12161217
DartType bound2 = p2.bound ?? DynamicTypeImpl.instance;
12171218
bound1 = bound1.substitute2(variablesFresh, variables1);
12181219
bound2 = bound2.substitute2(variablesFresh, variables2);
1219-
pFresh.bound = bound2;
12201220
if (!relation(bound2, bound1, p2, p1)) {
12211221
return null;
12221222
}
1223+
1224+
if (!bound2.isDynamic) {
1225+
pFresh.bound = bound2;
1226+
}
12231227
}
12241228
return variablesFresh;
12251229
}

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

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2342,8 +2342,30 @@ abstract class TypeSystem implements public.TypeSystem {
23422342
* Return a function type with those types.
23432343
*/
23442344
DartType _functionLeastUpperBound(FunctionType f, FunctionType g) {
2345-
// TODO(rnystrom): Right now, this assumes f and g do not have any type
2346-
// parameters. Revisit that in the presence of generic methods.
2345+
var fTypeFormals = f.typeFormals;
2346+
var gTypeFormals = g.typeFormals;
2347+
2348+
// If F and G differ in their number of type parameters, then the
2349+
// least upper bound of F and G is Function.
2350+
if (fTypeFormals.length != gTypeFormals.length) {
2351+
return typeProvider.functionType;
2352+
}
2353+
2354+
// If F and G differ in bounds of their of type parameters, then the
2355+
// least upper bound of F and G is Function.
2356+
var freshTypeFormalTypes =
2357+
FunctionTypeImpl.relateTypeFormals(f, g, (t, s, _, __) => t == s);
2358+
if (freshTypeFormalTypes == null) {
2359+
return typeProvider.functionType;
2360+
}
2361+
2362+
var typeFormals = freshTypeFormalTypes
2363+
.map<TypeParameterElement>((t) => t.element)
2364+
.toList();
2365+
2366+
f = f.instantiate(freshTypeFormalTypes);
2367+
g = g.instantiate(freshTypeFormalTypes);
2368+
23472369
List<DartType> fRequired = f.normalParameterTypes;
23482370
List<DartType> gRequired = g.normalParameterTypes;
23492371

@@ -2394,7 +2416,14 @@ abstract class TypeSystem implements public.TypeSystem {
23942416

23952417
// Calculate the LUB of the return type.
23962418
DartType returnType = getLeastUpperBound(f.returnType, g.returnType);
2397-
return new FunctionElementImpl.synthetic(parameters, returnType).type;
2419+
2420+
if (AnalysisDriver.useSummary2) {
2421+
return FunctionTypeImpl.synthetic(returnType, typeFormals, parameters);
2422+
}
2423+
2424+
var element = FunctionElementImpl.synthetic(parameters, returnType);
2425+
element.typeParameters = typeFormals;
2426+
return element.type;
23982427
}
23992428

24002429
/**

0 commit comments

Comments
 (0)