Rust: Implement type inference for associated types for concrete types#21188
Merged
paldepind merged 8 commits intogithub:mainfrom Feb 2, 2026
Merged
Rust: Implement type inference for associated types for concrete types#21188paldepind merged 8 commits intogithub:mainfrom
paldepind merged 8 commits intogithub:mainfrom
Conversation
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.
The goal of this PR is to handle paths that access an associated type on a concrete type (concrete meaning not a type parameter) in
TypeMention.These paths fall into one of two cases:
<Foo as Trait>::AssocwhereFoocan be an arbitrarily complex type.Self::Associnside animplblock. For animplblock of the formimpl Trait for Foosuch a path is basically sugar for the above.A key point is that resolving the associated type relies on how the concrete type implements the trait that the associated type is from. For instance, the added tests have examples where
<Odd<i32> as GetSet>::Outputis equal toboolbut<Odd<bool> as GetSet>::Outputis equal tochar.Changes to path resolution
Today case 2 is somewhat handled inside path resolution. But path resolution doesn't understand how types implement traits: For a
Self::Assocpath we find all associated types with the nameAssocacross all traits thatSelfimplement. This leads to spurious path resolutions as seen in the tests.To address this, the PR tweaks path resolution such that a
Self::Assocpath resolves to the associated type in the trait whereAssocis from (except whenAssocis defined directly in theimplblock thatSelfbelongs to). This does not depend on type information, so it can be done correctly in path resolution.Changes to type inference
After that we use the
SatisfiesConstraintmodule inTypeMentionto find the correct trait implementations and read the associated types off of those. When implementing this I ran into non-monotonic recursion. The problem as well as the workaround is documented in the comment for theMkTypeMentionmodule.Non-unique certain type inconsistency
The last commit adds an example of a non-unique certain type inconsistency introduced by the PR caused by a type mention that resolves to two paths.
In this case the two types obviously can't both be "certain", but they still represent certain information in the sense that any other types would certainly be incorrect. Hence it's still beneficial to include those types as certain, even though it breaks the inconsistency rule.
I tried to come up with an exclusion to the rule, but it's not trivial as the certain types originate in a type mention and can propagate many places from there.
DCA
The DCA report shows more types and a 0.2% point increase in resolved calls.
There is a small slowdown. I'm not seeing any bad joins, so I think it's from now constructing two instances of
TypeMention. If that is indeed the cause it might be possible to limit the impact in follow up work by restrictingPreTypeMentionto the (small) subset of type mentions that are actually used to construct the type hierarchy (those used inconditionSatisfiesConstraint).