Adjust function literal return type inference#3149
Closed
eernstg wants to merge 5 commits into
Closed
Conversation
eernstg
commented
Jun 15, 2023
| If the previous paragraph did not yield a value for `S`: When the function is | ||
| marked `async`: if `T <: futureValueType(R)` then let `S` be `T`; otherwise let | ||
| `S` be `futureValueType(R)`. When the function is not marked `async`: if `T <: | ||
| R` then let `S` be `T`; otherwise let `S` be `R`. |
Member
Author
There was a problem hiding this comment.
We need to use futureValueType(R), not flatten(R). If we were to use flatten then we'd carry ? over from the context. So we could have a context type that wants a Future<num>?, then we'd get int? as the value of R, and then we'd get an inferred return type of Future<int?> where the context wants a return type of Future<int>?, and that's a type error.
Future<int>? Function() f = () async => 1;In this example R is Future<int>? and flatten(R) is int?. But we don't want the function literal to have a return type of Future<int?>, it should be Future<int>.
Member
Author
|
Closing: #3151 contains a PR which simplifies a larger number of locations in this section because it avoids wrapping |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Issue #3148 showed that we need to adjust the rules about function literal return type inference. This PR changes the rules accordingly.
The basic idea is that the last step in the inference of an
asyncfunction used to use the inferred return type ofFuture<flatten(S)>whereSis taken from the context or from the returned expressions, but it should instead beFuture<S>. Also,flattenthen needs to be applied to the type obtained from the context, such that we still have the same return type as before when the context type gets to decide the outcome.