[rust-dev] Lifetime required to outlive itself

Huon Wilson dbau.pp at gmail.com
Tue Jan 21 17:46:11 PST 2014


On 22/01/14 12:41, Scott Lawrence wrote:
> This code compiles successfully: http://ix.io/a34 . I believe this 
> behavior is correct. Just so it's clear what this code does: f() takes 
> a `&mut int` and adds it to an array - the idea is that all of the 
> `&mut int` can be changed at some later time. Naturally, there's some 
> fancy lifetime juggling involved in this (which I may have gotten wrong).
>
> Uncommenting the commented parts (the method f() in the impl A, in 
> particular) yields the error message shown at the bottom, which 
> appears to say that the lifetime created in the second parameter of 
> f() does not necessarily outlive itself.
>
> Is there some especially complicated aspect of lifetimes as they 
> interact with &self, or is this indeed a bug?
>

I think it's the compiler tricking you: the 'a in `fn f<'a>(&mut self, 
...)` is shadowing the `impl<'a> A<'a>` i.e. they are different 
lifetimes that happen to have the same identifier. Changing it to `fn 
f(&mut self, &'a mut int)` should work.


There's a bug open about warning on shadowed generics, because, as this 
demonstrates, you end up with hard-to-diagnose error messages: 
https://github.com/mozilla/rust/issues/11658


Huon


More information about the Rust-dev mailing list