rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
98.94k stars 12.78k forks source link

negative_impls does not disarm coherence check (IntoGenerator example) #133556

Closed tmandry closed 5 hours ago

tmandry commented 5 hours ago
trait Iterator {}
trait IntoIterator {}
impl<I: Iterator> IntoIterator for I {}

trait Generator {}
trait IntoGenerator {}
impl<G: Generator> IntoGenerator for G {}

// ERROR: Conflicts with the above impl
impl<I: IntoIterator> IntoGenerator for I {}

// Attempts to fix:
impl<G: Generator> !Iterator for G {}
impl<G: Generator> !IntoIterator for G {}

gives (playground)

error[E0751]: found both positive and negative implementation of trait `IntoIterator`:
  --> src/lib.rs:17:1
   |
6  | impl<I: Iterator> IntoIterator for I {}
   | ------------------------------------ positive implementation here
...
17 | impl<G: Generator> !IntoIterator for G {}
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ negative implementation here

error[E0119]: conflicting implementations of trait `IntoGenerator`
  --> src/lib.rs:13:1
   |
10 | impl<G: Generator> IntoGenerator for G {}
   | -------------------------------------- first implementation here
...
13 | impl<I: IntoIterator> IntoGenerator for I {}
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation

There doesn't seem to be a way to remove the "conflicting implementations" error.

Slightly more elaborated playground

compiler-errors commented 5 hours ago

Enable the with_negative_coherence feature gate

tmandry commented 5 hours ago

Thanks! :)

compiler-errors commented 5 hours ago

I'm gonna close this bc I believe this is operating as intended. Negative coherence is gated behind a different feature gate intentionally :)

compiler-errors commented 5 hours ago

Race condition lol