I am chasing a compiler bug and found the following example
trait Lt<'a> {
type T;
}
impl<'a> Lt<'a> for () {
type T = &'a ();
}
fn test() {
let _: fn(<() as Lt<'_>>::T) = |_: &'static ()| {};
}
fn main() {
test();
}
I expect the above to compile as I gave a hint for Lt<'_>
to be Lt<'static>
and so everything should be fine, but I get the following error:
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements
--> src/main.rs:10:53
|
10 | let _: fn(<() as Lt<'_>>::T) = |_: &'static ()| {};
| ^^
|
note: first, the lifetime cannot outlive the anonymous lifetime #2 defined on the body at 10:36...
--> src/main.rs:10:36
|
10 | let _: fn(<() as Lt<'_>>::T) = |_: &'static ()| {};
| ^^^^^^^^^^^^^^^^^^^
= note: ...so that the types are compatible:
expected Lt<'_>
found Lt<'_>
= note: but, the lifetime must be valid for the static lifetime...
= note: ...so that the types are compatible:
expected &()
found &'static ()
What is the logic behind "first, the lifetime cannot outlive the anonymous lifetime #2"? As I am looking at a variation of a bug, if the reason is not solid we can attempt to fix it.
Working variation
fn test() {
let _: fn(<() as Lt<'static>>::T) = |_: &'_ ()| {};
}