]> git.lizzy.rs Git - rust.git/blob - tests/ui/borrowck/borrowck-scope-of-deref-issue-4666.rs
implement Hash for proc_macro::LineColumn
[rust.git] / tests / ui / borrowck / borrowck-scope-of-deref-issue-4666.rs
1 // run-pass
2 // Tests that the scope of the pointer returned from `get()` is
3 // limited to the deref operation itself, and does not infect the
4 // block as a whole.
5
6
7 struct Box {
8     x: usize
9 }
10
11 impl Box {
12     fn get(&self) -> &usize {
13         &self.x
14     }
15     fn set(&mut self, x: usize) {
16         self.x = x;
17     }
18 }
19
20 fn fun1() {
21     // in the past, borrow checker behaved differently when
22     // init and decl of `v` were distinct
23     let v;
24     let mut a_box = Box {x: 0};
25     a_box.set(22);
26     v = *a_box.get();
27     a_box.set(v+1);
28     assert_eq!(23, *a_box.get());
29 }
30
31 fn fun2() {
32     let mut a_box = Box {x: 0};
33     a_box.set(22);
34     let v = *a_box.get();
35     a_box.set(v+1);
36     assert_eq!(23, *a_box.get());
37 }
38
39 pub fn main() {
40     fun1();
41     fun2();
42 }