]> git.lizzy.rs Git - rust.git/blob - tests/ui/borrowck/borrowck-borrow-from-stack-variable.rs
Rollup merge of #106726 - cmorin6:fix-comment-typos, r=Nilstrieb
[rust.git] / tests / ui / borrowck / borrowck-borrow-from-stack-variable.rs
1 #[derive(Copy, Clone)]
2 struct Foo {
3   bar1: Bar,
4   bar2: Bar
5 }
6
7 #[derive(Copy, Clone)]
8 struct Bar {
9   int1: isize,
10   int2: isize,
11 }
12
13 fn make_foo() -> Foo { panic!() }
14
15 fn borrow_same_field_twice_mut_mut() {
16     let mut foo = make_foo();
17     let bar1 = &mut foo.bar1;
18     let _bar2 = &mut foo.bar1;  //~ ERROR cannot borrow
19     *bar1;
20 }
21
22 fn borrow_same_field_twice_mut_imm() {
23     let mut foo = make_foo();
24     let bar1 = &mut foo.bar1;
25     let _bar2 = &foo.bar1;  //~ ERROR cannot borrow
26     *bar1;
27 }
28
29 fn borrow_same_field_twice_imm_mut() {
30     let mut foo = make_foo();
31     let bar1 = &foo.bar1;
32     let _bar2 = &mut foo.bar1;  //~ ERROR cannot borrow
33     *bar1;
34 }
35
36 fn borrow_same_field_twice_imm_imm() {
37     let mut foo = make_foo();
38     let bar1 = &foo.bar1;
39     let _bar2 = &foo.bar1;
40     *bar1;
41 }
42
43 fn borrow_both_mut() {
44     let mut foo = make_foo();
45     let bar1 = &mut foo.bar1;
46     let _bar2 = &mut foo.bar2;
47     *bar1;
48 }
49
50 fn borrow_both_mut_pattern() {
51     let mut foo = make_foo();
52     match foo {
53         Foo { bar1: ref mut _bar1, bar2: ref mut _bar2 } => {}
54     }
55 }
56
57 fn borrow_var_and_pattern() {
58     let mut foo = make_foo();
59     let bar1 = &mut foo.bar1;
60     match foo {
61         Foo { bar1: ref mut _bar1, bar2: _ } => {} //
62         //~^ ERROR cannot borrow
63     }
64     *bar1;
65 }
66
67 fn borrow_mut_and_base_imm() {
68     let mut foo = make_foo();
69     let bar1 = &mut foo.bar1.int1;
70     let _foo1 = &foo.bar1; //~ ERROR cannot borrow
71     let _foo2 = &foo; //~ ERROR cannot borrow
72     *bar1;
73 }
74
75 fn borrow_mut_and_base_mut() {
76     let mut foo = make_foo();
77     let bar1 = &mut foo.bar1.int1;
78     let _foo1 = &mut foo.bar1; //~ ERROR cannot borrow
79     *bar1;
80 }
81
82 fn borrow_mut_and_base_mut2() {
83     let mut foo = make_foo();
84     let bar1 = &mut foo.bar1.int1;
85     let _foo2 = &mut foo; //~ ERROR cannot borrow
86     *bar1;
87 }
88
89 fn borrow_imm_and_base_mut() {
90     let mut foo = make_foo();
91     let bar1 = &foo.bar1.int1;
92     let _foo1 = &mut foo.bar1; //~ ERROR cannot borrow
93     *bar1;
94 }
95
96 fn borrow_imm_and_base_mut2() {
97     let mut foo = make_foo();
98     let bar1 = &foo.bar1.int1;
99     let _foo2 = &mut foo; //~ ERROR cannot borrow
100     *bar1;
101 }
102
103 fn borrow_imm_and_base_imm() {
104     let mut foo = make_foo();
105     let bar1 = &foo.bar1.int1;
106     let _foo1 = &foo.bar1;
107     let _foo2 = &foo;
108     *bar1;
109 }
110
111 fn borrow_mut_and_imm() {
112     let mut foo = make_foo();
113     let bar1 = &mut foo.bar1;
114     let _foo1 = &foo.bar2;
115     *bar1;
116 }
117
118 fn borrow_mut_from_imm() {
119     let foo = make_foo();
120     let bar1 = &mut foo.bar1; //~ ERROR cannot borrow
121     *bar1;
122 }
123
124 fn borrow_long_path_both_mut() {
125     let mut foo = make_foo();
126     let bar1 = &mut foo.bar1.int1;
127     let _foo1 = &mut foo.bar2.int2;
128     *bar1;
129 }
130
131 fn main() {}