Skip to content

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

Open
@tisonkun

Description

@tisonkun
$ 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-coherenceArea: CoherenceC-enhancementCategory: An issue proposing an enhancement or a PR with one.D-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions