The following code gives a compiler error: error[E0310]: the parameter type F may not live long enough, but F is never used to declare a variable or a member or reference. It is only used to access its associated types.
trait A {
type B: 'static;
}
struct C<D: A> {
b: D::B
}
struct E<F: A> {
x: &'static C<F>,
}
This must be some limitation of the type checker, but I can't figure out what exactly the problem is. Or is this a bug?
Also, this code compiles when I replace C<F> with F::B.
I'm sorry, but I don't follow how using a &'static C<F> would be different from PhantomData<C<F>> when you want to access the associated type B of F. Could you please provide an example for me where the former works but the latter doesn't?
This is the one that baffles me. For my type C only has a field of type F::B. So I understand that for C<F>: 'static it would require F::B: 'static. Why would F need to be 'static when it is never instantiated?
Without that requirement, you can erase or transmute any lifetimes in F in various ways (coerce to dyn Trait + 'static, round-trip through Box<dyn Any>) which is unsound in the general case.
There's more motivations in the RFC that made the rule syntactical.