E0119 rust type_error ai_generated true

error[E0119]: conflicting implementations of trait `Trait` for type `Type`

ID: rust/e0119-conflicting-implementations-of-trait

Also available as: JSON · Markdown · 中文
85%Fix Rate
84%Confidence
1Evidence
2024-03-22First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
rustc 1.75.0 active
rustc 1.78.0 active
rustc 1.80.0 active

Root Cause

Two or more impl blocks provide implementations of the same trait for the same type, or an impl conflicts with a blanket impl.

generic

中文

两个或多个 impl 块为同一类型提供了同一 trait 的实现,或者某个实现与 blanket impl 冲突。

Official Documentation

https://doc.rust-lang.org/error_codes/E0119.html

Workarounds

  1. 95% success Remove the duplicate impl block. Example: keep only one `impl Trait for MyType`.
    Remove the duplicate impl block. Example: keep only one `impl Trait for MyType`.
  2. 85% success Use a newtype pattern to create a distinct type. Example: `struct MyWrapper(MyType);` and implement the trait for MyWrapper.
    Use a newtype pattern to create a distinct type. Example: `struct MyWrapper(MyType);` and implement the trait for MyWrapper.
  3. 70% success If the conflict is with a blanket impl, add a type constraint or use specialization (if nightly) with `default impl`. Example: `impl<T: std::fmt::Debug> Trait for T { }`.
    If the conflict is with a blanket impl, add a type constraint or use specialization (if nightly) with `default impl`. Example: `impl<T: std::fmt::Debug> Trait for T { }`.

中文步骤

  1. Remove the duplicate impl block. Example: keep only one `impl Trait for MyType`.
  2. Use a newtype pattern to create a distinct type. Example: `struct MyWrapper(MyType);` and implement the trait for MyWrapper.
  3. If the conflict is with a blanket impl, add a type constraint or use specialization (if nightly) with `default impl`. Example: `impl<T: std::fmt::Debug> Trait for T { }`.

Dead Ends

Common approaches that don't work:

  1. 50% fail

    If both impls are needed for different purposes, removing one will cause missing trait implementation errors elsewhere.

  2. 100% fail

    Rust doesn't have a lint to suppress conflicting impls; it's a fundamental type system error.

  3. 60% fail

    If the blanket impl covers the wrapper (e.g., impl<T> Trait for T), the conflict persists.