An implementation of Ord and PartialOrd. for a struct which compiles correctly shows a red underline on the Ord impl, with the mouse over error: "the trait bound Example<A, B>: PartialOrd<Example<A, B>> is not satisf [E277]"
use std::cmp::{Ord, Ordering};
use std::time::Instant;
#[derive(Eq, PartialEq)]
struct Example<A, B>
where A: Ord,
B: Eq + PartialEq,
{
a: A,
b: B,
c: u8,
d: Instant, // We can presume `Instant`s will never be == here
}
impl <A, B> PartialOrd for Example<A, B>
where A: Ord,
B: Eq + PartialEq,
{
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
// The `Ord` here has the red underline and the mouse over error described
impl <A, B> Ord for Example<A, B>
where A: Ord,
B: Eq + PartialEq,
{
fn cmp(&self, other: &Self) -> Ordering {
self.c.cmp(&other.c).then_with(||other.d.cmp(&self.d))
}
}
小小 @Kobzol
@vlad20012 I managed to minimize the test case to this:
trait Trait<Rhs: ?Sized = Self> {}
trait Trait2: Trait<Self> {}
struct X<T>(T) where T: Trait;
impl <T> Trait for X<T> where T: Trait {}
impl <T> Trait2 for X<T> where T: Trait {}
it seems that the problem is the <Rhs .. = Self> default parameter in combination with its usage in where bounds.
I suspect that the problem is that we substitute Self = X<T>, however in the where bound the substitution should be Self = T. I'm not sure how to substitute the Self differently in the where bound and in the trait itself though.
@Kobzol, exactly! Thank you for the investigation! I fixed this in #5812