Skip to content

Null safe async returns are currently unsound #944

@leafpetersen

Description

@leafpetersen

The following program results in a variable of type int containing null (as implemented on the VM, and I believe as currently specified):

Future<int>? foo() async {
  return Future<int?>.value(null) as dynamic;
}
void main() async {
  Future<int>? f = foo();
  if (f != null) {
    await f.then((x) => print(x.isEven));
  }
}

The issue is that flatten(Future<int>?) = int?, and so the dynamic semantics is specified to check if the returned value is Future<int?> (which here it is) and if so use the contents of the future to complete the return future, which has an incompatible type.

I believe we need to either use something like the future value type that @eernstg has proposed, or else possibly just disallow these odd return types for asynchronous functions.

cc @eernstg @lrhn @munificent

Metadata

Metadata

Assignees

No one assigned

    Labels

    nnbdNNBD related issues

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions