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