error: lifetime may not live long enough
--> src/lib.rs:9:9
|
5 | impl<'repo> Wrapper<'repo> {
| ----- lifetime `'repo` defined here
6 | fn email(&self) -> Option<&'repo str> {
| - let's call the lifetime of this reference `'1`
...
9 | email
| ^^^^^ associated function was supposed to return data with lifetime `'repo` but it is returning data with lifetime `'1`
error[E0515]: cannot return value referencing local variable `signature`
--> src/lib.rs:9:9
|
8 | let email = signature.email();
| ----------------- `signature` is borrowed here
9 | email
| ^^^^^ returns a value referencing data owned by the current function
For more information about this error, try `rustc --explain E0515`.
Underlying issue
On the Commit struct, there are several "getter" methods where the lifetime of the return object is tied to a borrow of Self:
pub fn author(&self) -> Signature<'_> {
pub fn body(&self) -> Option<&str> {
Etc.
In other words, written without elision, these signatures all look like
pub fn author<'a>(&'a self) -> Signature<'a> {
pub fn body<'a>(&'a self) -> Option<&'a str> {
Etc.
But the pointers that are returned by these methods all live as long as the 'repo lifetime of the Commit struct itself if you look at the C code. In other words, they should all have these (more flexible) signatures:
This issue is very similar to #299, which was resolved in this PR: https://github.com/rust-lang/git2-rs/pull/302
In addition to filing this issue, I'm also submitting a new PR to address it.
Motivating example
This code should compile
but fails with
Underlying issue
On the
Commit
struct, there are several "getter" methods where the lifetime of the return object is tied to a borrow ofSelf
:pub fn author(&self) -> Signature<'_> {
pub fn body(&self) -> Option<&str> {
In other words, written without elision, these signatures all look like
pub fn author<'a>(&'a self) -> Signature<'a> {
pub fn body<'a>(&'a self) -> Option<&'a str> {
But the pointers that are returned by these methods all live as long as the
'repo
lifetime of theCommit
struct itself if you look at the C code. In other words, they should all have these (more flexible) signatures:There is a very similar problem happening for the "getter" methods of the
Signature
struct.The C code
The equivalent "getter" methods are defined in the underlying C code is here:
The pointers they return live here:
I believe all these pointers live as long as this raw
*mut raw::git_commit
pointer here:which has the
'repo
lifetime.