[ddc] Use `.futureValueType` from CFE

The CFE FunctionNode has a new getter for the Future value type that
can be used in null safe libraries.

Legacy libraries will still rely on flatten to get the type of the
Future.

Change-Id: I54ad3bc096fdb981f7f499f2ba3ddfcb01fd97d6
Issue: https://github.com/dart-lang/sdk/issues/44745
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/185240
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Commit-Queue: Nicholas Shahan <nshahan@google.com>
diff --git a/pkg/dev_compiler/lib/src/kernel/compiler.dart b/pkg/dev_compiler/lib/src/kernel/compiler.dart
index 1bbce68..db471a1 100644
--- a/pkg/dev_compiler/lib/src/kernel/compiler.dart
+++ b/pkg/dev_compiler/lib/src/kernel/compiler.dart
@@ -3316,16 +3316,13 @@
     //
     // In the body of an `async`, `await` is generated simply as `yield`.
     var gen = emitGeneratorFn((_) => []);
-    // Return type of an async body is `Future<flatten(T)>`, where T is the
-    // declared return type, unless T is Object. In that case the Object refers
-    // to a return type of `Future<Object?>`.
-    // TODO(nshahan) Use the Future type value when available on a FunctionNode.
-    var declaredReturnType = function
-        .computeThisFunctionType(_currentLibrary.nonNullable)
-        .returnType;
-    var returnType = _coreTypes.isObject(declaredReturnType)
-        ? _coreTypes.objectNullableRawType
-        : _types.flatten(declaredReturnType);
+    var returnType = _currentLibrary.isNonNullableByDefault
+        ? function.futureValueType
+        // Otherwise flatten the return type because futureValueType(T) is not
+        // defined for legacy libraries.
+        : _types.flatten(function
+            .computeThisFunctionType(_currentLibrary.nonNullable)
+            .returnType);
     return js.call('#.async(#, #)',
         [emitLibraryName(_coreTypes.asyncLibrary), _emitType(returnType), gen]);
   }