]> git.proxmox.com Git - rustc.git/blob - src/test/compile-fail/borrowck-borrow-from-owned-ptr.rs
Imported Upstream version 1.0.0~0alpha
[rustc.git] / src / test / compile-fail / borrowck-borrow-from-owned-ptr.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
12 struct Foo {
13 bar1: Bar,
14 bar2: Bar
15 }
16
17 impl Copy for Foo {}
18
19 struct Bar {
20 int1: isize,
21 int2: isize,
22 }
23
24 impl Copy for Bar {}
25
26 fn make_foo() -> Box<Foo> { panic!() }
27
28 fn borrow_same_field_twice_mut_mut() {
29 let mut foo = make_foo();
30 let bar1 = &mut foo.bar1;
31 let _bar2 = &mut foo.bar1; //~ ERROR cannot borrow
32 *bar1;
33 }
34
35 fn borrow_same_field_twice_mut_imm() {
36 let mut foo = make_foo();
37 let bar1 = &mut foo.bar1;
38 let _bar2 = &foo.bar1; //~ ERROR cannot borrow
39 *bar1;
40 }
41
42 fn borrow_same_field_twice_imm_mut() {
43 let mut foo = make_foo();
44 let bar1 = &foo.bar1;
45 let _bar2 = &mut foo.bar1; //~ ERROR cannot borrow
46 *bar1;
47 }
48
49 fn borrow_same_field_twice_imm_imm() {
50 let mut foo = make_foo();
51 let bar1 = &foo.bar1;
52 let _bar2 = &foo.bar1;
53 *bar1;
54 }
55
56 fn borrow_both_fields_mut() {
57 let mut foo = make_foo();
58 let bar1 = &mut foo.bar1;
59 let _bar2 = &mut foo.bar2; //~ ERROR cannot borrow
60 *bar1;
61 }
62
63 fn borrow_both_mut_pattern() {
64 let mut foo = make_foo();
65 match *foo {
66 Foo { bar1: ref mut _bar1, bar2: ref mut _bar2 } => {}
67 //~^ ERROR cannot borrow
68 }
69 }
70
71 fn borrow_var_and_pattern() {
72 let mut foo = make_foo();
73 let bar1 = &mut foo.bar1;
74 match *foo {
75 Foo { bar1: ref mut _bar1, bar2: _ } => {}
76 //~^ ERROR cannot borrow
77 }
78 *bar1;
79 }
80
81 fn borrow_mut_and_base_imm() {
82 let mut foo = make_foo();
83 let bar1 = &mut foo.bar1.int1;
84 let _foo1 = &foo.bar1; //~ ERROR cannot borrow
85 let _foo2 = &*foo; //~ ERROR cannot borrow
86 *bar1;
87 }
88
89 fn borrow_mut_and_base_mut() {
90 let mut foo = make_foo();
91 let bar1 = &mut foo.bar1.int1;
92 let _foo1 = &mut foo.bar1; //~ ERROR cannot borrow
93 *bar1;
94 }
95
96 fn borrow_mut_and_base_mut2() {
97 let mut foo = make_foo();
98 let bar1 = &mut foo.bar1.int1;
99 let _foo2 = &mut *foo; //~ ERROR cannot borrow
100 *bar1;
101 }
102
103 fn borrow_imm_and_base_mut() {
104 let mut foo = make_foo();
105 let bar1 = &foo.bar1.int1;
106 let _foo1 = &mut foo.bar1; //~ ERROR cannot borrow
107 *bar1;
108 }
109
110 fn borrow_imm_and_base_mut2() {
111 let mut foo = make_foo();
112 let bar1 = &foo.bar1.int1;
113 let _foo2 = &mut *foo; //~ ERROR cannot borrow
114 *bar1;
115 }
116
117 fn borrow_imm_and_base_imm() {
118 let mut foo = make_foo();
119 let bar1 = &foo.bar1.int1;
120 let _foo1 = &foo.bar1;
121 let _foo2 = &*foo;
122 *bar1;
123 }
124
125 fn borrow_mut_and_imm() {
126 let mut foo = make_foo();
127 let bar1 = &mut foo.bar1;
128 let _foo1 = &foo.bar2; //~ ERROR cannot borrow
129 *bar1;
130 }
131
132 fn borrow_mut_from_imm() {
133 let foo = make_foo();
134 let bar1 = &mut foo.bar1; //~ ERROR cannot borrow
135 *bar1;
136 }
137
138 fn borrow_long_path_both_mut() {
139 let mut foo = make_foo();
140 let bar1 = &mut foo.bar1.int1;
141 let foo1 = &mut foo.bar2.int2; //~ ERROR cannot borrow
142 *bar1;
143 *foo1;
144 }
145
146 fn main() {}