1 A type mismatched an associated type of a trait.
3 Erroneous code example:
6 trait Trait { type AssociatedType; }
8 fn foo<T>(t: T) where T: Trait<AssociatedType=u32> {
9 // ~~~~~~~~ ~~~~~~~~~~~~~~~~~~
11 // This says `foo` can |
12 // only be used with |
14 // implements `Trait`. |
16 // This says not only must
17 // `T` be an impl of `Trait`
18 // but also that the impl
19 // must assign the type `u32`
20 // to the associated type.
24 impl Trait for i8 { type AssociatedType = &'static str; }
25 //~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
30 // ... but it is an implementation
31 // that assigns `&'static str` to
32 // the associated type.
35 // Here, we invoke `foo` with an `i8`, which does not satisfy
36 // the constraint `<i8 as Trait>::AssociatedType=u32`, and
37 // therefore the type-checker complains with this error code.
40 The issue can be resolved by changing the associated type:
41 1) in the `foo` implementation:
43 trait Trait { type AssociatedType; }
45 fn foo<T>(t: T) where T: Trait<AssociatedType = &'static str> {
49 impl Trait for i8 { type AssociatedType = &'static str; }
54 2) in the `Trait` implementation for `i8`:
56 trait Trait { type AssociatedType; }
58 fn foo<T>(t: T) where T: Trait<AssociatedType = u32> {
62 impl Trait for i8 { type AssociatedType = u32; }