I have some code that implements a trait for all Fn(&str) closures:
trait Target {}
impl<T: Fn(&str)> Target for T {}
fn takes(_: impl Target) {}
fn test() {
takes(|_| {});
}
The function call fails to compile:
= note: closure with signature `fn(&'2 str)` must implement `FnOnce<(&'1 str,)>`, for any lifetime `'1`...
= note: ...but it actually implements `FnOnce<(&'2 str,)>`, for some specific lifetime `'2`
Rust can figure out the type, but it botches the lifetimes. But if I give the parameter a type myself, it works:
takes(|_: &str| {});
Why is that needed? Can I fix the definition somehow so Rust can infer the lifetime properly?
Type inference of closures with HRTBs involved has always been pretty junky. (The Fn(&str) is equivalent to for<'a> Fn(&'a str), and those for<'a> bounds are called HRTBs, Higher Ranked Trait Bounds)