Maybe Compiler Bug

use std::*;
trait Unique<T> {
    fn unique(self) -> collections::HashSet<T>;
}
impl<T> Unique<T> for T {
    fn unique(self) -> collections::HashSet<T> {
        iter::once(self).collect()
    }
}
impl<T, A: Unique<T>, B: Unique<T>> Unique<T> for (A, B) {
    fn unique(self) -> collections::HashSet<T> {
        self.0.unique().into_iter().chain(self.1.unique()).collect()
    }
}

(Playground)

Errors:

   Compiling playground v0.0.1 (/playground)
error[E0119]: conflicting implementations of trait `Unique<(_, _)>` for type `(_, _)`
  --> src/lib.rs:10:1
   |
5  | impl<T> Unique<T> for T {
   | ----------------------- first implementation here
...
10 | impl<T, A: Unique<T>, B: Unique<T>> Unique<T> for (A, B) {
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `(_, _)`

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

I don't see how conflicting implementations arise. Type (A, B) only implements Unique<(A, B)> once (line 5), because there's no way both A and B could implement Unique<(A, B)>

There is. I (or anyone using your crate/module) could do this:

use your_crate::Unique;

struct MyA;
struct MyB;

impl Unique<(MyA, MyB)> for MyA { /* ... */ }

impl Unique<(MyA, MyB)> for MyB { /* ... */ }

My two impls are not in conflict with your ones (nor with each other), but now both of yours apply to (MyA, MyB) putting them into the conflict reported by compiler.

2 Likes

We need to have a way to tell the compiler that there's no one else that's going to use our module down the line, to dissable all these rules or something

Or, impl Unique<(Self, Self)> for MyS.

I believe this is the official issue. Scroll to 2017+ for less Fn-trait specific comments/explanations.

In the generic case, we can validate that no impls exist that the crate can see, but there may be other impls added in downstream crates. This implies that downstream crates would have to do their own checking as well on our behalf, and this is what we wanted to avoid.


Some form of sealed traits that are recognized by coherence, perhaps.

1 Like