-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Errors when using an associated type defined in a supertrait #87
Comments
Hi @lukenels 👋 I imagine you would have gotten to the bottom of this long ago by now 🙂 But I think the problem here is a missing bound on the generated code. If we expand your example, it looks something like this: pub trait Foo {
type MyType;
}
pub trait Bar: Foo {
fn bar(&self, value: Self::MyType);
}
const _: () = {
extern crate alloc;
impl<T: Bar + ?Sized> Bar for alloc::sync::Arc<T>
where
alloc::sync::Arc<T>: Foo,
{
fn bar(&self, value: Self::MyType) {
T::bar(self, value)
}
}
}; But we can't call pub trait Foo {
type MyType;
}
pub trait Bar: Foo {
fn bar(&self, value: Self::MyType);
}
const _: () = {
extern crate alloc;
impl<T: Bar + ?Sized> Bar for alloc::sync::Arc<T>
where
alloc::sync::Arc<T>: Foo<MyType = <T as Foo>::MyType>, // <- bound added here
{
fn bar(&self, value: Self::MyType) {
T::bar(self, value)
}
}
}; I don't think we'd be able to generate that bound automatically, because we don't have the definition of |
Hi, KodrAus, thanks for looking into this for me! I ended up just working around the issue by implementing Your explanation of why this doesn't work with the current implementation of One thing I noticed though while playing around with the generated code is that, if As a concrete example, if we add use auto_impl::auto_impl;
#[auto_impl(Arc)]
pub trait Foo {
type MyType;
}
pub trait Bar: Foo {
fn bar(&self, value: Self::MyType);
}
const _: () = {
extern crate alloc;
impl<T: Bar + ?Sized> Bar for alloc::sync::Arc<T>
where
T: Foo,
{
fn bar(&self, value: Self::MyType) {
T::bar(self, value)
}
}
}; There are some other advantages to this encoding as well. For example, if I don't implement But it's hard for me to tell if this encoding is trading something else off (such as the ability to use What do you think? Are there other reasons to prefer the current encoding of supertrait constraints that I'm not thinking of, and are there any examples of code where the current encoding works but this possible alternate one wouldn't? |
Ah interesting, it's been a while since I've been really immersed in this code so can't tell at a glance whether we'd be swapping one set of failure modes for another, but I think it would be worth trying and seeing what it does with our test suite 🤔 |
@KodrAus I've submitted a (draft) pull request implementing the change here: #91. The change (and the update to the unit tests) is pretty simple, but it is a functional change to how |
Thanks for taking the time to work on this!
Something like crater, but for ecosystem libraries is something I’ve wanted for a long time 🙂 Doing a quick search on GitHub shows up a bunch of use-cases: https://github.com/search?q=%23%5Bauto_impl&type=code I’ll have a dig through them and see if I can spot any using supertraits. Once we’ve got a clear idea of what might break and what is now possible we can decide whether we want to go ahead with this, and whether we should consider it a new feature in a new major version bump, or more like a bug fix. |
Awesome! Let me know what you end up finding out and then I can un-draft the PR if you still think it's the right approach. |
Ideally, an automatic mechanism would be used like implemented by the auto_impl crate. However, at this moment, gryf runs into auto-impl-rs/auto_impl#87
Ideally, an automatic mechanism would be used like implemented by the auto_impl crate. However, at this moment, gryf runs into auto-impl-rs/auto_impl#87
I'm having some trouble getting
auto_impl
to work when a trait defines a method that takes an argument of some associated type, where the associated type is defined in a supertrait:When I try to compile this, I get the following error:
I've tried a few different things (e.g., using
<Self as Foo>::MyType
instead, puttingauto_impl
on both traits, etc.) but can't seem to get it to work. Any tips / workarounds? Thanks!The text was updated successfully, but these errors were encountered: