Rust E0119 "conflicting implementations of trait" in CI
rustc found two trait implementations that can both apply to the same type. Coherence forbids overlap, so it refuses to pick one -- commonly a hand-written impl colliding with a #[derive] or a blanket impl.
What this error means
The build stops with error[E0119]: conflicting implementations of trait Foo for type Bar`` and points at both impls. It reproduces deterministically.
error[E0119]: conflicting implementations of trait `Default` for type `Config`
--> src/config.rs:8:10
|
5 | #[derive(Default)]
| ------- first implementation here
...
8 | impl Default for Config { fn default() -> Self { Config { retries: 3 } } }
| ^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `Config`Common causes
Manual impl plus a derive
A #[derive(Default)] (or similar) generates an impl, and a hand-written impl Default for the same type overlaps it.
Overlap with a blanket impl
A blanket impl such as impl<T: Bar> Foo for T already covers your type, so a specific impl Foo for MyType conflicts.
How to fix it
Keep only one implementation
- Decide whether you want the derived behavior or the manual one.
- Remove the
#[derive(...)]if you keep the manual impl, or delete the manual impl if the derive is enough. - For blanket-impl overlap, drop the specific impl or narrow the blanket bound so it no longer covers your type.
// keep the manual impl, drop the derive:
struct Config { retries: u32 }
impl Default for Config {
fn default() -> Self { Config { retries: 3 } }
}How to prevent it
- Do not pair a manual trait impl with a derive of the same trait.
- Audit blanket impls before adding a specific impl for a covered type.
- Run
cargo checkafter adding or removing derives.