"consider adding an explicit lifetime bound `R: 'static" suggestion is misleading
Consider an (incorrect) code:
trait Foo {} struct Bar<R>(R); impl<R> Foo for Bar<R> { } fn bb<R>(r: R) -> Box<Foo> { Box::new(Bar(r)) } fn main() { let a = 10; let _b = bb(&a); }
Rustc suggests:
consider adding an explicit lifetime bound `R: 'static`
This suggestion R: 'static is not good enough, code still won't compile.
The problem is that suggestion is misleading, because proper fix should be
fn bb<'r, R : 'r>(r: R) -> Box<Foo + 'r> { ... }
People who don't understand lifetimes well (like me), could stuck after adding 'static bound.
So better suggestion could be something like:
consider adding an explicit lifetime bound `R: 'static`,
or lifetime parameter `<'r, R: 'r> ... -> ... Trait + 'r`
or do not mention 'static at all:
consider adding an explicit lifetime bound to `R`
And also, I think, E0310 could have an example with non-static lifetime bound.