Skip to content

Runtime behavior of async return doesn't match static behavior. #22730

Closed
@lrhn

Description

@lrhn

The static behavior of "return e;" in an async expression is to check that Future<flatten(T)> (T is static type of e) is assignable to function return type.

The runtime behavior in checked mode is (where S is the runtime type of the value of e):
"If the body of f is marked async (9) it is a dynamic type error if o is
not null (16.2) and Future<S> is not a subtype of the actual return type
(19.8.1) of f."

There is no flatten here, which means that:

import "dart:async";
Future<int> foo() async {
  return new Future.value(42);  // no await.
}
main() {
  foo().then(print);
}

will not give a static warning (flatten(Future<int>) is int), but at runtime it should fail because Future<Future<int>> is not assignable to Future<int>.

I think the flatten should also be applied to the runtime behavior.
Both the Dart VM and dart2js runs the code above with no error and prints 42 in checked mode.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-specification (deprecated)Deprecated: use area-language and a language- label.type-bugIncorrect behavior (everything from a crash to more subtle misbehavior)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions