Rust: Implement type inference for closures and calls to closures #20130
+479
−78
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.
This PR implements type inference for closure expressions, calls to closures, and correctly handles
FnOnce
trait bounds.A few notes on the implementation and the PR:
FnOnce
trait has a type parameterArgs
which is a tuple of all the arguments. That is reflected in our implementation as well, i.e., we re-use our tuple types for parameters.FnOnce
,FnMut
, andFn
traits. They form a trait hierarchy withFnOnce
at the top.FnOnce
uses an associated type for the return type and since we don't support associated types inherited in subtraits (tests for this was added in Rust: Fix type inference for trait objects for traits with associated types #20122) we can right now only really understand theFnOnce
trait and not the others.dyn FnOnce
type. That is, instead of creating some new type for closures we just reusedyn
. The Rust language itself says that closures have some internal type that can't be written by users, and all that known about it is that it implements some of theFnOnce
/Fn
/FnMut
traits.dyn
gives us this effect. rust-analyzer shows closures as having inimpl FnOnce
type. I think that makes a bit more sense thandyn
, but usingdyn
was simpler.Fn
trait for closures, as it is a subtrait of the others, but, again, we can't understandFn
yet.DCA seems fine, though we only get a 0.04% point increase in resolved calls, which is less than I had hoped.