Bryan Chen
Bryan Chen

Reputation: 46598

Unable to implement trait for associated type

Code: https://github.com/xlc/trait-bug

I want to implement a trait for an associated type

impl From<<baz::Baz as ::baz::BazTrait>::BazType> for Foo {
    fn from(_: <baz::Baz as ::baz::BazTrait>::BazType) -> Self {
        todo!()
    }
}

But getting error

error[E0119]: conflicting implementations of trait `From<Foo>` for type `Foo`
  --> foo/src/lib.rs:11:1
   |
11 | impl From<<baz::Baz as ::baz::BazTrait>::BazType> for Foo {
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: conflicting implementation in crate `core`:
           - impl<T> From<T> for T;

For more information about this error, try `rustc --explain E0119`.
error: could not compile `foo` (lib) due to previous error

Which doesn't make sense to me, as <baz::Baz as ::baz::BazTrait>::BazType is a concrete type (u32 in my example)

Is this a compiler bug?

Is there a workaround to achieve my goal?

Note that this is a simplified version of my original code, that is a macro taking a type as input and generate some implementations for this type and associated types.


Full code

foo/src/lib.rs

pub struct Foo;

impl From<<baz::Baz as ::baz::BazTrait>::BazType> for Foo {
    fn from(_: <baz::Baz as ::baz::BazTrait>::BazType) -> Self {
        todo!()
    }
}

baz/src/lib.rs

pub trait BazTrait {
    type BazType;
}

pub struct Baz;

impl BazTrait for Baz {
    type BazType = u32;
}

Upvotes: 3

Views: 71

Answers (1)

Chayim Friedman
Chayim Friedman

Reputation: 71025

This is issue #51445, so yes, a compiler bug.

Upvotes: 1

Related Questions