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