Closed
Description
This code:
trait Foo: PartialOrd + PartialOrd<i32> {}
trait Bar {
type Baz: Foo;
fn bar(&self) -> Self::Baz;
}
fn baz<T: Bar>(x: T, y: T) -> bool {
x.bar() < y.bar()
}
gives this error:
Compiling playground v0.0.1 (/playground)
error[E0308]: mismatched types
--> src/lib.rs:10:15
|
10 | x.bar() < y.bar()
| ^^^^^^^ expected `i32`, found associated type
|
= note: expected type `i32`
found associated type `<T as Bar>::Baz`
= note: consider constraining the associated type `<T as Bar>::Baz` to `i32`
= note: for more information, visit https://doc.rust-lang.org/book/ch19-03-advanced-traits.html
which indicates that only the PartialOrd<i32>
impl is being considered. It seems like the only way to fix it at the usage site is something like
<T::Baz as PartialOrd>::partial_cmp(&x.bar(), &y.bar())
On the other hand, just swapping the order of the supertraits fixes it:
trait Foo: PartialOrd<i32> + PartialOrd {}