Conflict trait impls

Suppose I have following code

trait TraitA {
     type Marker;
}
struct MarkerA;
struct MarkerB;

struct Wrapper<T>(T);

impl <T: TraitA<Marker = MarkerA>> SomeOtherTrait for Wrapper<T>{}
impl <T: TraitA<Marker = MarkerB>> SomeOtherTrait for Wrapper<T>{}

It fails to compile Rust Playground

Standard Error

   Compiling playground v0.0.1 (/playground)
error[E0119]: conflicting implementations of trait `SomeOtherTrait` for type `Wrapper<_>`:
  --> src/lib.rs:12:1
   |
11 | impl <T: TraitA<Marker = MarkerA>> SomeOtherTrait for Wrapper<T>{}
   | ---------------------------------------------------------------- first implementation here
12 | impl <T: TraitA<Marker = MarkerB>> SomeOtherTrait for Wrapper<T>{}
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `Wrapper<_>`

error: aborting due to previous error

But my understand is as long as the type T should have only one implementation for TraitA, so that any T: TraitA<Marker = MarkerA> should not have T: TraitA<Marker = MarkerB> satisified at the same time. So there shouldn't be any conflict impls.

So I am wondering if this is a current type cheker limitation or I was missing something? If this is some current type checker's limit, will chalk helps on this?


I know there's an work around by introducing generic parameter to SomeOtherTrait

impl <T: TraitA<Marker = MarkerA>> SomeOtherTrait<MarkerA> for Wrapper<T>{}
impl <T: TraitA<Marker = MarkerB>> SomeOtherTrait<MarkerB> for Wrapper<T>{}

But do we have any solution other than this, as the work around seems logically weird.

Thanks in advance!

This is issue 20400, in particular see this comment by @jschievink:

To give an update on this: In 2016, RFC 1672 was proposed to fix this, but was postponed until the Chalk integration is done. Additionally, according to rust-lang/rfcs#1672 (comment), allowing these kinds of impls would allow users to express mutually exclusive traits, which is a very powerful feature that needs more in-depth consideration by the language team (hence marking as blocked on an RFC as well).

4 Likes

I see. Seems Chalk will bring some exciting features, really happy to know that. :slight_smile:

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.