Adjust function literal return type inference to avoid spurious null #4210
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.
See dart-lang/sdk#59669 where this topic came up. Thanks to @chiholai-sanasofthk for spotting this issue!
This PR changes one item in the list of actions taken during function literal return type inference: A
return;
statement only addsNull
to the return type in cases where the given function literal is a non-generator.With the current version,
Null
is added to the return type also in cases like() sync* { yield 1; return; }
such that this function literal gets the inferred return typeIterable<int?>
. This is an unnecessary loss of typing precision because null is never actually added to the returned iterable. With this update, the inferred return type isIterable<int>
.