Rust: Associated types are inherited as type parameters by traits and dyn traits #21165
+7,070
−6,666
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 adds support for specifying associated type from a supertrait in a trait bound that uses a subtrait.
The Problem
Suppose we have a supertrait and a subtrait as follows:
Now trait bounds that use
AnotherGetcan mention theOutputassociated type like thisAdditionally,
dyntypes forAnotherGethave to specifyOutputfrom the supertrait:The Solution
To support the above, this PR makes associated types in supertraits (which are already turned into type parameters of the supertrait) induce type parameters in any subtraits (transitively). This seemed like the simplest implementation, given how we currently treat associated types as type parameters.
So in the above example, the trait
AnotherGetgets a type parameter forOutput, written asOutput[AnotherGet]. For a boundAnotherGet<Output = ..>the equality will instantiateOutput[AnotherGet].The new type parameters are accounted for in the sub-trait "inheritance" relation such that type parameters that correspond to the same associated type are matched up to each other.
Dyn trait types also receive the new type parameters in the same way as traits, such that
dyn Foostill has a set of type parameters that correspond 1-to-1 to those ofFoo.About the change to the consistency check:
Previously, for something like
the path
T::Outputwould inresolvePathTypeAtresolve to the type alias forOutputbut would then be filtered away insidePathTypeMention::resolvePathTypeAtat the check that prevents type parameters from escaping their scope.Now
resolvePathTypeAtwon't resolve to anything forT::OutputsinceTdoesn't resolve to a trait. This means that the current exemption doesn't apply and thus I added an additional one.Future Work
This should enable us to improve type inference for closures as well as support the
FnandFnMuttraits (which now inherits theResultassociated type from theirFnOncesupertrait).