Change the encoding of supertrait clauses to support associated types #91
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 pull request is related to the discussion in #87.
When
auto_impl
is used to implement trait that has a supertrait, it needs to enforce that the inner type already implements the supertrait. However, the current encoding does not work correctly when the supertrait has an associated type that the subtrait refers to in one of its methods.To make this concrete, take the following example.
auto_impl
will expand this code into the following, which fails to compile since the compiler doesn't know thatArc<T>::Foo
is the same type asT::Foo
.This commit changes how
auto_impl
encodes supertraitwhere
constraints likealloc::sync::Arc<T>: Foo
to refer to the inner type parameter instead, e.g.,T: Foo
. With this change, if we also annotate traitFoo
withauto_impl
, then the associated type example will compile, because the compiler has access to the implementation ofFoo
forArc<T>
whenT: Foo
:This change is not backwards compatible in certain edge scenarios. For example, the new encoding does not permit using
auto_impl
to implementBar
forArc<T>
but having a user-defined / custom implementation ofArc<T>
for its supertraitFoo
.It also causes
auto_impl
onBar
to fail to compile eagerly when there is no corresponding implementation ofFoo
, whereas the previous behavior would allow this to compile (but would produce a constraint that fails to be satisfied if the implementation ofBar
is ever attempted to be used).These seem like niche scenarios, but this does constitute a functional (though minor) change. However, this this trade-off may be worth since it's otherwise not possible to use associated types from supertraits with
auto_impl
.This commit also updates the unit tests accordingly; for example, adding
auto_impl
on supertraits where it was missing before in order to fix the new build errors.