Skip to content

Commit 477ac2d

Browse files
stereotype441commit-bot@chromium.org
authored andcommitted
Migration: start adding support for assignment of function types.
So far we only handle zero-parameter non-generic functions. Support for positional parameters, named parameters, and type parameters will be added in future CLs. Change-Id: I5a5907ba595da40352193e0193f908bca4d1bf1c Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/106040 Commit-Queue: Paul Berry <paulberry@google.com> Reviewed-by: Brian Wilkerson <brianwilkerson@google.com> Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
1 parent 718ab66 commit 477ac2d

3 files changed

Lines changed: 59 additions & 1 deletion

File tree

pkg/nnbd_migration/lib/src/graph_builder.dart

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,8 @@ $stackTrace''');
686686
DecoratedType visitSimpleIdentifier(SimpleIdentifier node) {
687687
var staticElement = node.staticElement;
688688
if (staticElement is ParameterElement ||
689-
staticElement is LocalVariableElement) {
689+
staticElement is LocalVariableElement ||
690+
staticElement is FunctionElement) {
690691
return getOrComputeElementType(staticElement);
691692
} else if (staticElement is PropertyAccessorElement) {
692693
var elementType = getOrComputeElementType(staticElement);
@@ -804,6 +805,23 @@ $stackTrace''');
804805
sourceType.typeArguments[i], expressionChecks,
805806
hard: false);
806807
}
808+
} else if (sourceType.type is FunctionType &&
809+
destinationType.type is FunctionType) {
810+
_checkAssignment(
811+
destinationType.returnType, sourceType.returnType, expressionChecks,
812+
hard: hard);
813+
if (sourceType.typeArguments.isNotEmpty ||
814+
destinationType.typeArguments.isNotEmpty) {
815+
throw UnimplementedError('TODO(paulberry)');
816+
}
817+
if (sourceType.positionalParameters.isNotEmpty ||
818+
destinationType.positionalParameters.isNotEmpty) {
819+
throw UnimplementedError('TODO(paulberry)');
820+
}
821+
if (sourceType.namedParameters.isNotEmpty ||
822+
destinationType.namedParameters.isNotEmpty) {
823+
throw UnimplementedError('TODO(paulberry)');
824+
}
807825
} else if (destinationType.type.isDynamic || sourceType.type.isDynamic) {
808826
// ok; nothing further to do.
809827
} else {

pkg/nnbd_migration/test/api_test.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,24 @@ int? f(C c) => c.f;
332332
await _checkSingleFileChanges(content, expected);
333333
}
334334

335+
test_data_flow_function_return_type() async {
336+
var content = '''
337+
int Function() f(int Function() x) => x;
338+
int g() => null;
339+
main() {
340+
f(g);
341+
}
342+
''';
343+
var expected = '''
344+
int? Function() f(int? Function() x) => x;
345+
int? g() => null;
346+
main() {
347+
f(g);
348+
}
349+
''';
350+
await _checkSingleFileChanges(content, expected);
351+
}
352+
335353
test_data_flow_generic_contravariant_inward() async {
336354
var content = '''
337355
class C<T> {

pkg/nnbd_migration/test/graph_builder_test.dart

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1450,6 +1450,15 @@ void test(C c) {
14501450
assertEdge(decoratedTypeAnnotation('C c').node, never, hard: true));
14511451
}
14521452

1453+
test_return_function_type_simple() async {
1454+
await analyze('''
1455+
int/*1*/ Function() f(int/*2*/ Function() x) => x;
1456+
''');
1457+
var int1 = decoratedTypeAnnotation('int/*1*/');
1458+
var int2 = decoratedTypeAnnotation('int/*2*/');
1459+
assertEdge(int2.node, int1.node, hard: true);
1460+
}
1461+
14531462
test_return_implicit_null() async {
14541463
verifyNoTestUnitErrors = false;
14551464
await analyze('''
@@ -1646,6 +1655,19 @@ Set<String> f() {
16461655
assertEdge(always, decoratedTypeAnnotation('String>{').node, hard: false);
16471656
}
16481657

1658+
test_simpleIdentifier_function() async {
1659+
await analyze('''
1660+
int f() => null;
1661+
main() {
1662+
int Function() g = f;
1663+
}
1664+
''');
1665+
1666+
assertEdge(decoratedTypeAnnotation('int f').node,
1667+
decoratedTypeAnnotation('int Function').node,
1668+
hard: false);
1669+
}
1670+
16491671
test_simpleIdentifier_local() async {
16501672
await analyze('''
16511673
main() {

0 commit comments

Comments
 (0)