#Conflicting implementations when targets are disjunct?

4 messages · Page 1 of 1 (latest)

rose smelt
#

Given:

trait Logic {}

impl Logic for bool {}

impl<I, T> Logic for I
where
    I: IntoIterator<Item = T>,
    T: Logic,
{}

yields:

error[E0119]: conflicting implementations of trait `Logic` for type `bool`
 --> src/lib.rs:5:1
  |
3 |   impl Logic for bool {}
  |   ------------------- first implementation here
4 |
5 | / impl<I, T> Logic for I
6 | | where
7 | |     I: IntoIterator<Item = T>,
8 | |     T: Logic,
  | |_____________^ conflicting implementation for `bool`
  |
  = note: upstream crates may add a new impl of trait `std::iter::Iterator` for type `bool` in future versions

For more information about this error, try `rustc --explain E0119`.

(playground)

I don't get why though. I get the argument that theoretically there could be an Iterator impl for bool in future versions of std.
However, then it'd need to yield elements implementing Logic, which it can't since Logic is from my crate (otherwise there would need to be a circular dependency between my crate <-> std)? So why the error?

tidal vigil
#

The rules the compiler follows do not perfectly reflect what is theoretically possible. In this case, you’re entirely right, but the compiler doesn't follow your logic.

#

The primary item on the wishlist for less strict trait coherence in this kind of case is “negative impls”; std can then declare impl !IntoIterator for bool {} to say that bool will never implement IntoIterator. But that’s not complete nor stable yet.

rose smelt
#

Fascinating. Thanks!!