4

reference must be valid for the block at 50:74... ...but borrowed value is only valid for the block at 50:74

Right. So what's the problem?

The block in question:

pub fn git_upload_pack(self: &mut GitConnect) -> Result<String, &str> {
    let c = format!("git-upload-pack {}\0host={}\0", self.repo.path, self.repo.domain);

    let mut out = String::new();
    let data = try!(self.command(c.as_slice()));

    for line in data.iter() {
        out.push_str(from_utf8(line.as_slice()).unwrap());
    }

    Ok(out)
}

self.command:

fn command(self: &mut GitConnect, command: &str) -> Result<Vec<Vec<u8>>, &str> {

The full error:

src/protocol/git_connect.rs:54:38: 54:39 error: `c` does not live long enough
src/protocol/git_connect.rs:54         let data = try!(self.command(c.as_slice()));
                                                                    ^
<std macros>:1:1: 6:60 note: in expansion of try!
src/protocol/git_connect.rs:54:20: 54:53 note: expansion site
src/protocol/git_connect.rs:50:75: 61:6 note: reference must be valid for the anonymous lifetime #1 defined on the block at 50:74...
src/protocol/git_connect.rs:50     pub fn git_upload_pack(self: &mut GitConnect) -> Result<String, &str> {
src/protocol/git_connect.rs:51         let c = format!("git-upload-pack {}\0host={}\0", self.repo.path, self.repo.domain);
src/protocol/git_connect.rs:52 
src/protocol/git_connect.rs:53         let mut out = String::new();
src/protocol/git_connect.rs:54         let data = try!(self.command(c.as_slice()));
src/protocol/git_connect.rs:55 
                               ...
src/protocol/git_connect.rs:50:75: 61:6 note: ...but borrowed value is only valid for the block at 50:74
src/protocol/git_connect.rs:50     pub fn git_upload_pack(self: &mut GitConnect) -> Result<String, &str> {
src/protocol/git_connect.rs:51         let c = format!("git-upload-pack {}\0host={}\0", self.repo.path, self.repo.domain);
src/protocol/git_connect.rs:52 
src/protocol/git_connect.rs:53         let mut out = String::new();
src/protocol/git_connect.rs:54         let data = try!(self.command(c.as_slice()));
  • What's the signature of self.command? – Vladimir Matveev Jan 19 '15 at 18:28
  • Also could you please provide the full code of your function, including its signature? It may be important. – Vladimir Matveev Jan 19 '15 at 18:29
3

It looks like a bug to me.

This signature:

fn command(self: &mut GitConnect, command: &str) -> Result<Vec<Vec<u8>>, &str>

according to lifetime elision rules, should be equivalent to this one:

fn command<'a, 'b>(self: &'a mut GitConnect, command: &'b str) -> Result<Vec<Vec<u8>>, &'a str>

And in fact if you rewrite your command() to use this expanded variant it should compile. Also if you use shorthand self argument definition:

fn command(&mut self, command: &str) -> Result<Vec<Vec<u8>>, &str>

then it also compiles.

It seems that currently

fn command(self: &mut GitConnect, command: &str) -> Result<Vec<Vec<u8>>, &str>

is equivalent to

fn command<'a>(self: &'a mut GitConnect, command: &'a str) -> Result<Vec<Vec<u8>>, &'a str>

which gives exactly the same error for the correct reason because there is a lifetime error: command parameter's lifetime is asserted to be the same as self, so it won't work with local variables whose lifetime will be shorter than the one of self.

  • I figured it out just before you posted - you're almost right, except the lifetime of the Result &str is unrelated to the lifetime of the input. Specifically, it's &'static str. Looks like it was giving the Result &str the same lifetime as the command parameter – J V Jan 19 '15 at 18:56
  • Well, according to your signatures and elision rules it is related to the input. – Vladimir Matveev Jan 19 '15 at 18:57
  • FWIW, I've submitted an issue here. – Vladimir Matveev Jan 19 '15 at 19:08

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.