Intellij-rust: Erroneous Red Underline In `Ord` Implementation

Created on 26 Jul 2020  路  3Comments  路  Source: intellij-rust/intellij-rust

Environment

  • IntelliJ Rust plugin version: 0.3.126.3220-202
  • Rust toolchain version: 1.45.0 (5c1f21c3b 2020-07-13) x86_64-apple-darwin
  • IDE name and version: CLion 2020.2 Beta (CL-202.6397.12)
  • Operating system: macOS 10.15.6

Problem description

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]"

Steps to reproduce

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))
    }
}

bug code insight

All 3 comments

小小 @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

Was this page helpful?
0 / 5 - 0 ratings