Skip to content

Commit 766e542

Browse files
danrubelcommit-bot@chromium.org
authored andcommitted
handle return in async function
This updates EdgeBuilder to handle return statements in async functions when the return value is not a Future... Future<int> f() async { return 1; } Change-Id: Icc2a375546d01592c99ababb9652ce8108ae7dd6 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/108181 Commit-Queue: Dan Rubel <[email protected]> Reviewed-by: Paul Berry <[email protected]>
1 parent 51cf8f2 commit 766e542

2 files changed

Lines changed: 37 additions & 5 deletions

File tree

‎pkg/nnbd_migration/lib/src/edge_builder.dart‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -722,13 +722,24 @@ $stackTrace''');
722722

723723
@override
724724
DecoratedType visitReturnStatement(ReturnStatement node) {
725-
if (node.expression == null) {
725+
DecoratedType returnType = _currentFunctionType.returnType;
726+
Expression returnValue = node.expression;
727+
// TODO(danrubel): This does not handle situations where the returnType
728+
// or the returnValue's type extends or implements dart:async Future.
729+
if ((returnType.type.isDartAsyncFuture ||
730+
returnType.type.isDartAsyncFutureOr) &&
731+
node.thisOrAncestorOfType<FunctionBody>().isAsynchronous &&
732+
!returnValue.staticType.isDartAsyncFuture) {
733+
returnType = returnType.typeArguments?.first;
734+
if (returnType == null) {
735+
return null;
736+
}
737+
}
738+
if (returnValue == null) {
726739
_checkAssignment(null,
727-
source: _nullType,
728-
destination: _currentFunctionType.returnType,
729-
hard: false);
740+
source: _nullType, destination: returnType, hard: false);
730741
} else {
731-
_handleAssignment(node.expression, _currentFunctionType.returnType);
742+
_handleAssignment(returnValue, returnType);
732743
}
733744
return null;
734745
}

‎pkg/nnbd_migration/test/edge_builder_test.dart‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2317,6 +2317,27 @@ class C {
23172317
hard: true);
23182318
}
23192319

2320+
test_return_from_async_future() async {
2321+
await analyze('''
2322+
Future<int> f() async {
2323+
return g();
2324+
}
2325+
int g() => 1;
2326+
''');
2327+
// No assertions; just checking that it doesn't crash.
2328+
}
2329+
2330+
test_return_from_async_futureOr() async {
2331+
await analyze('''
2332+
import 'dart:async';
2333+
FutureOr<int> f() async {
2334+
return g();
2335+
}
2336+
int g() => 1;
2337+
''');
2338+
// No assertions; just checking that it doesn't crash.
2339+
}
2340+
23202341
test_return_function_type_simple() async {
23212342
await analyze('''
23222343
int/*1*/ Function() f(int/*2*/ Function() x) => x;

0 commit comments

Comments
 (0)