The code
pub trait Q<S> {
fn f();
}
pub trait A {
type I;
type F: Q<Self::I>;
}
// this works (1)
//
// pub struct S<T>
// where
// T: A
// {
// unsatisfied trait bound (2)
pub struct S<T>
where
T: A<I = bool>,
{
t: T,
}
fails to compile:
error[E0277]: the trait bound `<T as A>::F: Q<bool>` is not satisfied
--> src/main.rs:18:1
|
18 | / pub struct S<T>
19 | | where
20 | | T: A<I = bool>,
21 | | {
22 | | t: T,
23 | | }
| |_^ the trait `Q<bool>` is not implemented for `<T as A>::F`
|
= help: consider adding a `where <T as A>::F: Q<bool>` bound
= note: required by `A`
Interestingly, it works if you use the commented out line (1) instead of (2). It also works if you turn the associated type I
into a generic type (write trait A<I>
and A<bool>
).
impl<T> S<T>
where T: A
{
fn g() {
T::F::f()
}
}
succeeds with line (1) or generic type I
, so T::F : Q<bool>
is indeed assumed in those cases.
Why is the trait bound automatically assumed with line (1) or generic types, but not with line (2)?
Can we fix the above code without appending where T::F: Q<bool>
every time we use T: A<I=bool>
?