|
| 1 | +// Copyright 2015 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +// Regression test for #21400 which itself was extracted from |
| 12 | +// stackoverflow.com/questions/28031155/is-my-borrow-checker-drunk/28031580 |
| 13 | + |
| 14 | +fn main() { |
| 15 | + let mut t = Test; |
| 16 | + assert_eq!(t.method1("one"), Ok(1)); |
| 17 | + assert_eq!(t.method2("two"), Ok(2)); |
| 18 | + assert_eq!(t.test(), Ok(2)); |
| 19 | +} |
| 20 | + |
| 21 | +struct Test; |
| 22 | + |
| 23 | +impl Test { |
| 24 | + fn method1(&mut self, _arg: &str) -> Result<usize, &str> { |
| 25 | + Ok(1) |
| 26 | + } |
| 27 | + |
| 28 | + fn method2(self: &mut Test, _arg: &str) -> Result<usize, &str> { |
| 29 | + Ok(2) |
| 30 | + } |
| 31 | + |
| 32 | + fn test(self: &mut Test) -> Result<usize, &str> { |
| 33 | + let s = format!("abcde"); |
| 34 | + // (Originally, this invocation of method2 was saying that `s` |
| 35 | + // does not live long enough.) |
| 36 | + let data = match self.method2(&*s) { |
| 37 | + Ok(r) => r, |
| 38 | + Err(e) => return Err(e) |
| 39 | + }; |
| 40 | + Ok(data) |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +// Below is a closer match for the original test that was failing to compile |
| 45 | + |
| 46 | +pub struct GitConnect; |
| 47 | + |
| 48 | +impl GitConnect { |
| 49 | + fn command(self: &mut GitConnect, _s: &str) -> Result<Vec<Vec<u8>>, &str> { |
| 50 | + unimplemented!() |
| 51 | + } |
| 52 | + |
| 53 | + pub fn git_upload_pack(self: &mut GitConnect) -> Result<String, &str> { |
| 54 | + let c = format!("git-upload-pack"); |
| 55 | + |
| 56 | + let mut out = String::new(); |
| 57 | + let data = try!(self.command(&c)); |
| 58 | + |
| 59 | + for line in data.iter() { |
| 60 | + out.push_str(&format!("{:?}", line)); |
| 61 | + } |
| 62 | + |
| 63 | + Ok(out) |
| 64 | + } |
| 65 | +} |
| 66 | + |
0 commit comments