Rust: Add SatisfiesConstraintInput
module in shared type inference
#19829
+121
−173
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 refactors the shared type inference library, it untangles the logic for figuring out whether a type tree implements a trait, such that we can expose that logic from the shared library. This is done by a new module called
SatisfiesConstraintInput
. This module can be used to figure out if and how the type of an AST nodes (or more generally anyHasTypeTree
) implements a given trait.I believe we can use this module for quite a few things in Rust:
for
loops likefor i of e { ... }
where the type ofi
depends on howe
implements theIntoIterator
trait.receiver
implementsTrait1
.Deref
trait we need to figure out whether the potentially deref'ed value implementsDeref
.Fn
,FnOnce
, etc.foo.await
we must know iffoo
implementsFuture
.To show that this works, the last item is carried out in this PR (results in a simplification compared to the current implementation that uses the
Matching
module).for
loops should also be pretty easy, but I didn't want to create conflicts with #19754.Below is an explanation of how this module can be used in the case of the iterator in a
for
loop:First we create a class that satisfies the
HasTypeTree
signature and which includes nodes that occur as the iterator in afor
loop:Then we create a modules that satisfies the
SatisfiesConstraint
signature. The signature only contains a predicate that for every type tree gives the traits that we're interested in that type tree satisfying:Now we can instantiate the module and get the type of
e
as an implementation of theIntoIterator
trait:This should make it possible to type
for
loops over any iterator type, which we could do following #19754.