Why does the following not compile?
trait A<'a> {
type Typ;
}
trait B {}
struct C<T> {
_ph: std::marker::PhantomData<T>,
}
impl<'a, T, V> B for C<T> where
T: A<'a, Typ=V>
{}
The previous gives the error "the type parameter V
is not constrained by the impl trait, self type, or predicates". Changing the associated type to a generic type also does not compile, giving the same error.
However, the following compiles when the lifetimes are removed.
trait A {
type Typ;
}
trait B {}
struct C<T> {
_ph: std::marker::PhantomData<T>,
}
impl<T, V> B for C<T> where
T: A<Typ=V>
{}
Removing the associated type compiles as well.
trait A<'a> {}
trait B {}
struct C<T> {
_ph: std::marker::PhantomData<T>,
}
impl<'a, T> B for C<T> where
T: A<'a>
{}
Seeing how just removing the lifetime causes the code to compile (not changing V in any way), I'm guessing the error message is not the real underlying error. I have no clue what the real reason for not compiling would be.
The original situation this arose isn't really relevant - this question's more about the strange behaviour and error message, but you can look at it here if you want.
'a
andV
pairs that satisfyT: A<'a, Typ=V>
, which means that just given the typeC<T>
, Rust cannot decide what genericimpl
parameters to use. With no lifetime and only a constraint on the associated type, there can only be oneA
impl and thus only one validV
. – Aplet123