rust-lang / rust-lang/rust

Unclear why i have conflicting impl

Open
#128,646 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-coherence A-diagnostics D-terse T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code
pub trait ErrorTrait: std::error::Error + std::fmt::Debug {}

#[derive(Debug)]
pub enum Error {
    VariantA,
    VariantB,
    Dynamic(Box<dyn ErrorTrait>),
}

impl std::fmt::Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:?}", self)
    }
}

impl std::error::Error for Error {}

impl ErrorTrait for Error {}

impl<E: ErrorTrait + 'static> From<E> for Error {
    fn from(e: E) -> Error {
        Error::Dynamic(Box::new(e))
    }
}

fn main () {}
Current output
error[E0119]: conflicting implementations of trait `From<Error>` for type `Error`
  --> src/main.rs:20:1
   |
20 | impl<E: ErrorTrait + 'static> From<E> for Error {
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: conflicting implementation in crate `core`:
           - impl<T> From<T> for T;

For more information about this error, try `rustc --explain E0119`.
error: could not compile `compiler-bug` (bin "compiler-bug") due to 1 previous error
Desired output
error[E0119]: conflicting implementations of trait `From<Error>` for type `Error`
  --> src/main.rs:20:1
   |
20 | impl<E: ErrorTrait + 'static> From<E> for Error {
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: conflicting implementation in crate `core`:
           - impl<T> From<T> for T;
   = note: Error implements ErrorTrait in module `main` line 18

For more information about this error, try `rustc --explain E0119`.
error: could not compile `compiler-bug` (bin "compiler-bug") due to 1 previous error
Rationale and extra context

In simple example it's easy see why there is conflicting implementation. In other case i was naming/merging error types so i ended in a module with impl ErrorTrait for Error where Error was now crate::Error and i could not understand why i had conflict impl. If message said ErrorTrait was implemented for Error the problem will be obvious in my case.

Other cases

No response

Rust Version
rustc 1.82.0-nightly (f8060d282 2024-07-30)
binary: rustc
commit-hash: f8060d282d42770fadd73905e3eefb85660d3278
commit-date: 2024-07-30
host: x86_64-unknown-linux-gnu
release: 1.82.0-nightly
LLVM version: 18.1.7
Anything else?

Thanks for best compiler errors <3

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the conflict using the src/main.rs example with rustc 1.82.0-nightly, then read the E0119 explanation via rustc --explain E0119 and trace the compiler diagnostic entry point for conflicting implementations. Done means the diagnostic identifies that Error implements ErrorTrait and includes the implementation location shown in the desired output.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.