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