-
Notifications
You must be signed in to change notification settings - Fork 13.8k
Description
Hi! Several times I've faced this interesting case of lifetime miselision:
struct Foo<'a> { bar: &'a Bar}
impl<'a> Foo<'a> {
fn get_bar(&self) -> & /* 'a */ Bar { self.bar }
}
http://play.integer32.com/?gist=3aba0526aff729bb9d62483a10732826&version=undefined
Here, we want to use 'a
lifetime for the returned Bar
reference, but lifetime elision gives use lifetime of the self reference. The problem here is that code with elided lifetimes compiles flawlessly by itself, but may fail at the call site, and this is tremendously confusing, because you get a misleading compiler error in the completely wrong place! I've hit this issues several times in practice (the latest one).
I wonder if we can warn about this? That is, we issue a warning if a lifetime in a returned position is elided, but the lifetime of the returned value from the function can actually be larger (alternatively, if the actual lifetime is among the input lifetimes, and is not the same as the one inferred by elision). The fix for warning would be to specify lifetimes explicitly (I suppose that "I did mean to use a shorter lifetime here" is a rather rare use-case).