This code:
trait Foo: PartialOrd + PartialOrd<<Self as Foo>::A> {
type A;
}
trait Bar {
type B: Foo;
fn bar(&self) -> Self::B;
}
fn baz<T: Bar>(x: T, y: T) -> bool {
x.bar() < y.bar()
}
fails to compile with this error:
error[E0308]: mismatched types
--> src/lib.rs:12:15
|
12 | x.bar() < y.bar()
| ^^^^^^^ expected Foo::A, found Bar::B
|
= note: expected associated type `<<T as Bar>::B as Foo>::A`
found associated type `<T as Bar>::B`
which seems to imply that type inference is trying to call the B: PartialOrd<B::A>
impl instead of the B: PartialOrd<B>
impl which is also available.
Is this a bug or just an (intentional/unintentional) limitation of Rust's type inference?
Is there a better workaround than this:
<T::B as PartialOrd>::partial_cmp(&x.bar(), &y.bar()) == Some(Ordering::Less)
This was a bug/limitation of Rust that was fixed by https://github.com/rust-lang/rust/pull/73905. The first Rust release with the fix was 1.49.0.