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

Incorrect behavior of return in async function. #39204

Closed
lrhn opened this issue Nov 1, 2019 · 5 comments
Closed

Incorrect behavior of return in async function. #39204

lrhn opened this issue Nov 1, 2019 · 5 comments
Labels
area-front-end Use area-front-end for front end / CFE / kernel format related issues.

Comments

@lrhn
Copy link
Member

lrhn commented Nov 1, 2019

The Dart 2 behavior for returns in async functions is to await a future if it's a future of the flattened return type.

That is:

Future<int> foo() { 
   return someExpression;
}

will evaluate someExpression with context type FutureOr<int>, and if it is a Future<int>, it is awaited before returning its value.

That it's awaited and not passed directly to a completer (an implementation detail) is important in the case where the return is inside a try/catch an you try to return a future containing an error.

Example:

Future<int> foo() {
   try {
     return compute();
   } catch (e) {
     return -1;
   }
}
Future<int> compute() async { 
  throw "No";
}
main() {
 foo().then(print);  // should print -1
}

See #22730 for history of the spec change.

@lrhn
Copy link
Member Author

lrhn commented Apr 29, 2020

Another reason awaiting is important is code like:

Future<int> foo() async {
  try {
    return Future<int>.error("gotcha");
  } finally {
    return 0;
  }
}

The issue here is that if the returned future is not awaited at the return statement, it will be silently discarded and the error becomes uncaught instead of thrown.

@natebosch
Copy link
Member

It's not clear to me what the concrete actions are to resolve this. Can we describe the specific change we are hoping for in the implementations?

Should we consider pushing on dart-lang/language#870 to fix the ambiguity?

@stereotype441
Copy link
Member

@lrhn @eernstg Is this a duplicate of #44395?

@eernstg
Copy link
Member

eernstg commented Feb 3, 2021

Yes, they are very similar, so #44395 is essentially a duplicate of this one. I didn't find this one when I created #44395.

@stereotype441
Copy link
Member

Duplicate of #44395

@stereotype441 stereotype441 marked this as a duplicate of #44395 Feb 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-front-end Use area-front-end for front end / CFE / kernel format related issues.
Projects
None yet
Development

No branches or pull requests

4 participants