Skip to content

Errors when using an associated type defined in a supertrait #87

Open
@lukenels

Description

@lukenels

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:

 use auto_impl::auto_impl;

pub trait Foo {
    type MyType;
}

#[auto_impl(Arc)]
pub trait Bar: Foo {
    fn bar(&self, value: Self::MyType);
}

When I try to compile this, I get the following error:

error[E0308]: mismatched types
  --> src/main.rs:11:19
   |
9  | #[auto_impl(Arc)]
   | -----------------
   | |
   | this type parameter
   | arguments to this function are incorrect
10 | pub trait Bar: Foo {
11 |     fn bar(&self, value: Self::MyType);
   |                   ^^^^^ expected type parameter `T`, found struct `Arc`
   |
   = note: expected associated type `<T as Foo>::MyType`
              found associated type `<Arc<T> as Foo>::MyType`
note: associated function defined here
  --> src/main.rs:11:8
   |
11 |     fn bar(&self, value: Self::MyType);
   |        ^^^

For more information about this error, try `rustc --explain E0308`.
error: could not compile `autoimpl-demo` due to previous error

I've tried a few different things (e.g., using <Self as Foo>::MyType instead, putting auto_impl on both traits, etc.) but can't seem to get it to work. Any tips / workarounds? Thanks!


> rustc --version
rustc 1.64.0 (a55dd71d5 2022-09-19)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions