[dart2js] Use correct futureValueType.

Fixes: #54317
Change-Id: Id94b486df6a66ef6c77f2c578ab8d11a73189ff9
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/341328
Reviewed-by: Sigmund Cherem <sigmund@google.com>
Commit-Queue: Mayank Patke <fishythefish@google.com>
This commit is contained in:
Mayank Patke 2023-12-13 17:50:00 +00:00 committed by Commit Queue
parent 9a452fb98a
commit 0b0a972526
5 changed files with 32 additions and 20 deletions

View file

@ -1496,10 +1496,10 @@ abstract class JElementEnvironment extends ElementEnvironment {
/// argument is passed.
DartType getTypeVariableDefaultType(TypeVariableEntity typeVariable);
/// Returns the 'element' type of a function with the async, async* or sync*
/// marker [marker]. [returnType] is the return type marked function.
/// Returns the 'element' type of a [function] with the async, async* or sync*
/// marker. [returnType] is the return type of the marked function.
DartType getAsyncOrSyncStarElementType(
AsyncMarker marker, DartType returnType);
FunctionEntity function, DartType returnType);
/// Returns the 'element' type of a function with an async, async* or sync*
/// marker. The return type of the method is inspected to determine the type

View file

@ -235,12 +235,18 @@ ir.FunctionNode? getFunctionNode(
case MemberKind.constructorBody:
ir.Member node = definition.node as ir.Member;
return node.function;
case MemberKind.generatorBody:
final node = definition.node;
if (node is ir.LocalFunction) return node.function;
return (node as ir.Member).function;
case MemberKind.closureCall:
ir.LocalFunction node = definition.node as ir.LocalFunction;
return node.function;
default:
case MemberKind.closureField:
case MemberKind.signature:
case MemberKind.recordGetter:
return null;
}
return null;
}
/// Returns the initializer for [field].

View file

@ -2221,13 +2221,14 @@ class JsElementEnvironment extends ElementEnvironment
DartType getFunctionAsyncOrSyncStarElementType(FunctionEntity function) {
// TODO(sra): Should be getting the DartType from the node.
DartType returnType = getFunctionType(function).returnType;
return getAsyncOrSyncStarElementType(function.asyncMarker, returnType);
return getAsyncOrSyncStarElementType(function, returnType);
}
@override
DartType getAsyncOrSyncStarElementType(
AsyncMarker asyncMarker, DartType returnType) {
var returnTypeWithoutNullability = returnType.withoutNullability;
FunctionEntity function, DartType returnType) {
final asyncMarker = function.asyncMarker;
final returnTypeWithoutNullability = returnType.withoutNullability;
switch (asyncMarker) {
case AsyncMarker.SYNC:
return returnType;
@ -2240,16 +2241,8 @@ class JsElementEnvironment extends ElementEnvironment
}
return dynamicType;
case AsyncMarker.ASYNC:
if (returnTypeWithoutNullability is FutureOrType) {
return returnTypeWithoutNullability.typeArgument;
}
if (returnTypeWithoutNullability is InterfaceType) {
if (returnTypeWithoutNullability.element ==
elementMap.commonElements.futureClass) {
return returnTypeWithoutNullability.typeArguments.first;
}
}
return dynamicType;
return elementMap.getDartType(
getFunctionNode(elementMap, function)!.futureValueType!);
case AsyncMarker.ASYNC_STAR:
if (returnTypeWithoutNullability is InterfaceType) {
if (returnTypeWithoutNullability.element ==

View file

@ -1367,7 +1367,7 @@ class KernelSsaGraphBuilder extends ir.VisitorDefault<void>
// Add the type parameter for the generator's element type.
DartType elementType = _elementEnvironment.getAsyncOrSyncStarElementType(
function.asyncMarker, _returnType!);
function, _returnType!);
// TODO(sra): [elementType] can contain free type variables that are erased
// due to no rtiNeed. We will get getter code if these type variables are
@ -1454,7 +1454,7 @@ class KernelSsaGraphBuilder extends ir.VisitorDefault<void>
// Call `_makeSyncStarIterable<T>(body)`. This usually gets inlined.
final elementType = _elementEnvironment.getAsyncOrSyncStarElementType(
function.asyncMarker, _returnType!);
function, _returnType!);
FunctionEntity method = _commonElements.syncStarIterableFactory;
List<HInstruction> arguments = [pop()];
List<DartType> typeArguments = const [];

View file

@ -0,0 +1,13 @@
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'dart:async';
import 'package:expect/expect.dart';
void main() async {
final value = await fn();
Expect.equals(42, value);
}
FutureOr<Object> fn() async => 42;