rust-lang / rust-lang/rust

"downstream crates may implement trait for type" for both trait and type private to this crate

Open
#130,839 6 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-coherence C-enhancement D-incorrect S-has-mcve T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

$ rustc --version
rustc 1.83.0-nightly (363ae4188 2024-09-24)
use std::cmp::Ordering;
use compare::Compare;
use num_cmp::NumCmp;

trait PartialCompare<L: ?Sized, R: ?Sized = L> {
    fn compare(&self, l: &L, r: &R) -> Option<Ordering>;
}

impl<L, R, C: Compare<L, R>> PartialCompare<L, R> for C {
    fn compare(&self, l: &L, r: &R) -> Option<Ordering> {
        Some(self.compare(l, r))
    }
}

#[derive(Default, Debug, Clone, Copy)]
struct NumCompare;

impl<L, R> PartialCompare<L, R> for NumCompare
where
    L: NumCmp<R> + Copy,
    R: Copy,
{
    fn compare(&self, l: &L, r: &R) -> Option<Ordering> {
        NumCmp::num_cmp(*l, *r)
    }
}


fn main() {}

With dependencies:

num-cmp = "0.1.0"
compare = "0.1.0"

Failed to compile:

error[E0119]: conflicting implementations of trait `PartialCompare<_, _>` for type `NumCompare`
  --> src/main.rs:18:1
   |
9  |   impl<L, R, C: Compare<L, R>> PartialCompare<L, R> for C {
   |   ------------------------------------------------------- first implementation here
...
18 | / impl<L, R> PartialCompare<L, R> for NumCompare
19 | | where
20 | |     L: NumCmp<R> + Copy,
21 | |     R: Copy,
   | |____________^ conflicting implementation for `NumCompare`
   |
   = note: downstream crates may implement trait `compare::Compare<_, _>` for type `NumCompare`

Since both PartialCompare and NumCompare are defined in this crate, the error message seems incorrect. Not sure it's a bug or an error messaging inaccurate.

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

Start by compiling the supplied Rust example with the reported rustc 1.83.0-nightly version and confirm the E0119 diagnostic. Trace the coherence-checking diagnostic path and its handling of private traits and types; done means the diagnostic accurately describes whether a downstream implementation can create the conflict, with regression coverage for this example.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
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.