]> git.proxmox.com Git - rustc.git/blame - src/test/run-pass/borrowck-scope-of-deref-issue-4666.rs
Imported Upstream version 1.0.0~beta
[rustc.git] / src / test / run-pass / borrowck-scope-of-deref-issue-4666.rs
CommitLineData
970d7e83
LB
1// Copyright 2012-2013 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// Tests that the scope of the pointer returned from `get()` is
12// limited to the deref operation itself, and does not infect the
13// block as a whole.
14
c34b1796
AL
15// pretty-expanded FIXME #23616
16
970d7e83 17struct Box {
c34b1796 18 x: usize
970d7e83
LB
19}
20
21impl Box {
c34b1796 22 fn get(&self) -> &usize {
970d7e83
LB
23 &self.x
24 }
c34b1796 25 fn set(&mut self, x: usize) {
970d7e83
LB
26 self.x = x;
27 }
28}
29
30fn fun1() {
31 // in the past, borrow checker behaved differently when
32 // init and decl of `v` were distinct
33 let v;
1a4d82fc
JJ
34 let mut a_box = Box {x: 0};
35 a_box.set(22);
36 v = *a_box.get();
37 a_box.set(v+1);
38 assert_eq!(23, *a_box.get());
970d7e83
LB
39}
40
41fn fun2() {
1a4d82fc
JJ
42 let mut a_box = Box {x: 0};
43 a_box.set(22);
44 let v = *a_box.get();
45 a_box.set(v+1);
46 assert_eq!(23, *a_box.get());
970d7e83
LB
47}
48
49pub fn main() {
50 fun1();
51 fun2();
1a4d82fc 52}