Skip to content

NNBD: throw in async function literal causing incorrect return type #41156

@jakemac53

Description

@jakemac53

You should be able to pass a function which always throws which can masquerade as having essentially any return type, but currently that is not possible.

Example:

void main() async {
  Future<String> Function(int) x = (int v) async {
    throw v;
  }
}

Gives the following error:

 Error: A value of type 'Future<FutureOr<String>> Function(int)' can't be assigned to a variable of type 'Future<String> Function(int)'.
 - 'Future' is from 'dart:async'.
 - 'FutureOr' is from 'dart:async'.
  Future<String> Function(int) x = (int v) async {

It looks like it is inferring the type FutureOr<String> when it would need to be Future<Never> or possibly Future<String>.

If you extract the function literal out to a top level method it is fine:

void main() async {
  Future<String> Function(int) y = x;
}

Future<String> x(int v) async {
  throw v;
}

This appears to only be a problem in the CFE and not analyzer.

cc @natebosch

Metadata

Metadata

Assignees

Labels

NNBDIssues related to NNBD Releaselegacy-area-front-endLegacy: Use area-dart-model instead.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions