]> git.lizzy.rs Git - rust.git/blob - src/test/compile-fail/borrowck/borrowck-reborrow-from-shorter-lived-andmut.rs
eee407472bf14c12342ce07bf4d20200f88dcfc4
[rust.git] / src / test / compile-fail / borrowck / borrowck-reborrow-from-shorter-lived-andmut.rs
1 // Copyright 2014 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 // Test that assignments to an `&mut` pointer which is found in a
12 // borrowed (but otherwise non-aliasable) location is illegal.
13
14 struct S<'a> {
15     pointer: &'a mut isize
16 }
17
18 fn copy_borrowed_ptr<'a,'b>(p: &'a mut S<'b>) -> S<'b> {
19     S { pointer: &mut *p.pointer }
20     //~^ ERROR cannot infer
21 }
22
23 fn main() {
24     let mut x = 1;
25
26     {
27         let mut y = S { pointer: &mut x };
28         let z = copy_borrowed_ptr(&mut y);
29         *y.pointer += 1;
30         *z.pointer += 1;
31     }
32 }