Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Returning a `FutureOr` in an asynchronous function fails #32233

eernstg opened this issue Feb 19, 2018 · 1 comment


Copy link

commented Feb 19, 2018

Consider the following program:

import 'dart:async';
FutureOr<int> foo() => 42;
Future<num> bar() async => foo();
main() => bar();

The analyzer in strong mode responds as follows (when the program is stored in n020.dart):

> dartanalyzer --version
dartanalyzer version 2.0.0-dev.26.0
> dartanalyzer --strong n020.dart
Analyzing n020.dart...
  error • The return type 'Future<FutureOr<int>>' isn't a 'Future<num>', as defined by the method 'bar' at n020.dart:5:28 • return_of_invalid_type
1 error found.

Presumably, this means that the returned expression in bar is checked in a manner which does not precisely correspond to the flatten function (from the language specification, sections Function Expressions and Return):

If \code{$T =$ FutureOr<$S$>} then $flatten(T) = S$.
It is a static type warning if the body of $f$ is marked \ASYNC{} and
the type \code{Future<$flatten(T)$>} (\ref{functionExpressions}) may not
be assigned to the declared return type of $f$.

But Future<int> may be assigned to Future<num> so the program should be accepted. Note that analysis of the same program succeeds with dartanalyzer --preview-dart-2.


This comment has been minimized.

Copy link

commented Feb 20, 2018

Probably related: #30638

dart-bot pushed a commit that referenced this issue Jun 15, 2018
Fix various bugs and regularize behavior of returns statements in
various kinds of functions.

Closes-bug: #31887
Closes-bug: #30638
Closes-bug: #32233
Closes-bug: #32881
Closes-bug: #31278
Change-Id: I4ebd7e71096d611e189b571ba5de2998dd11c98b
Reviewed-by: Brian Wilkerson <>
Commit-Queue: Leaf Petersen <>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
2 participants
You can’t perform that action at this time.