]> git.lizzy.rs Git - rust.git/blob - src/test/compile-fail/borrowck-borrow-from-stack-variable.rs
cleanup: s/impl Copy/#[derive(Copy)]/g
[rust.git] / src / test / compile-fail / borrowck-borrow-from-stack-variable.rs
1 // Copyright 2012 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 #[derive(Copy)]
12 struct Foo {
13   bar1: Bar,
14   bar2: Bar
15 }
16
17 #[derive(Copy)]
18 struct Bar {
19   int1: isize,
20   int2: isize,
21 }
22
23 fn make_foo() -> Foo { panic!() }
24
25 fn borrow_same_field_twice_mut_mut() {
26     let mut foo = make_foo();
27     let bar1 = &mut foo.bar1;
28     let _bar2 = &mut foo.bar1;  //~ ERROR cannot borrow
29     *bar1;
30 }
31
32 fn borrow_same_field_twice_mut_imm() {
33     let mut foo = make_foo();
34     let bar1 = &mut foo.bar1;
35     let _bar2 = &foo.bar1;  //~ ERROR cannot borrow
36     *bar1;
37 }
38
39 fn borrow_same_field_twice_imm_mut() {
40     let mut foo = make_foo();
41     let bar1 = &foo.bar1;
42     let _bar2 = &mut foo.bar1;  //~ ERROR cannot borrow
43     *bar1;
44 }
45
46 fn borrow_same_field_twice_imm_imm() {
47     let mut foo = make_foo();
48     let bar1 = &foo.bar1;
49     let _bar2 = &foo.bar1;
50     *bar1;
51 }
52
53 fn borrow_both_mut() {
54     let mut foo = make_foo();
55     let bar1 = &mut foo.bar1;
56     let _bar2 = &mut foo.bar2;
57     *bar1;
58 }
59
60 fn borrow_both_mut_pattern() {
61     let mut foo = make_foo();
62     match foo {
63         Foo { bar1: ref mut _bar1, bar2: ref mut _bar2 } => {}
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;
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;
138     *bar1;
139 }
140
141 fn main() {}