File: issue-21400.rs

package info (click to toggle)
rustc-web 1.78.0%2Bdfsg1-2~deb11u3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,245,360 kB
  • sloc: xml: 147,985; javascript: 18,022; sh: 11,083; python: 10,265; ansic: 6,172; cpp: 5,023; asm: 4,390; makefile: 4,269
file content (56 lines) | stat: -rw-r--r-- 1,392 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//@ run-pass
// Regression test for #21400 which itself was extracted from
// stackoverflow.com/questions/28031155/is-my-borrow-checker-drunk/28031580

fn main() {
    let mut t = Test;
    assert_eq!(t.method1("one"), Ok(1));
    assert_eq!(t.method2("two"), Ok(2));
    assert_eq!(t.test(), Ok(2));
}

struct Test;

impl Test {
    fn method1(&mut self, _arg: &str) -> Result<usize, &str> {
        Ok(1)
    }

    fn method2(self: &mut Test, _arg: &str) -> Result<usize, &str> {
        Ok(2)
    }

    fn test(self: &mut Test) -> Result<usize, &str> {
        let s = format!("abcde");
        // (Originally, this invocation of method2 was saying that `s`
        // does not live long enough.)
        let data = match self.method2(&*s) {
            Ok(r) => r,
            Err(e) => return Err(e)
        };
        Ok(data)
    }
}

// Below is a closer match for the original test that was failing to compile

pub struct GitConnect;

impl GitConnect {
    fn command(self: &mut GitConnect, _s: &str) -> Result<Vec<Vec<u8>>, &str> {
        unimplemented!()
    }

    pub fn git_upload_pack(self: &mut GitConnect) -> Result<String, &str> {
        let c = format!("git-upload-pack");

        let mut out = String::new();
        let data = self.command(&c)?;

        for line in data.iter() {
            out.push_str(&format!("{:?}", line));
        }

        Ok(out)
    }
}