Error code E0582
A lifetime is only present in an associated-type binding, and not in the input types to the trait.
Erroneous code example:
fn bar<F>(t: F) // No type can satisfy this requirement, since `'a` does not // appear in any of the input types (here, `i32`): where F: for<'a> Fn(i32) -> Option<&'a i32> { } fn main() { }
To fix this issue, either use the lifetime in the inputs, or use
'static
. Example:
fn bar<F, G>(t: F, u: G) where F: for<'a> Fn(&'a i32) -> Option<&'a i32>, G: Fn(i32) -> Option<&'static i32>, { } fn main() { }
This error also includes the use of associated types with lifetime parameters.
#![allow(unused)] fn main() { trait Foo { type Assoc<'a>; } struct Bar<X, F> where X: Foo, F: for<'a> Fn(X::Assoc<'a>) -> &'a i32 { x: X, f: F } }
The latter scenario encounters this error because Foo::Assoc<'a>
could be
implemented by a type that does not use the 'a
parameter, so there is no
guarentee that X::Assoc<'a>
actually uses 'a
.
To fix this we can pass a dummy parameter:
#![allow(unused)] fn main() { trait Foo { type Assoc<'a>; } struct Bar<X, F> where X: Foo, F: for<'a> Fn(X::Assoc<'a>, /* dummy */ &'a ()) -> &'a i32 { x: X, f: F } }
Note: The examples above used to be (erroneously) accepted by the compiler, but this was since corrected. See issue #33685 for more details.