]> git.lizzy.rs Git - rust.git/blob - src/test/ui/borrowck/borrowck-reborrow-from-mut.rs
Auto merge of #54624 - arielb1:evaluate-outlives, r=nikomatsakis
[rust.git] / src / test / ui / borrowck / borrowck-reborrow-from-mut.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: isize,
18   int2: isize,
19 }
20
21 fn borrow_same_field_twice_mut_mut(foo: &mut Foo) {
22     let _bar1 = &mut foo.bar1;
23     let _bar2 = &mut foo.bar1;  //~ ERROR cannot borrow
24 }
25
26 fn borrow_same_field_twice_mut_imm(foo: &mut Foo) {
27     let _bar1 = &mut foo.bar1;
28     let _bar2 = &foo.bar1;  //~ ERROR cannot borrow
29 }
30
31 fn borrow_same_field_twice_imm_mut(foo: &mut Foo) {
32     let _bar1 = &foo.bar1;
33     let _bar2 = &mut foo.bar1;  //~ ERROR cannot borrow
34 }
35
36 fn borrow_same_field_twice_imm_imm(foo: &mut Foo) {
37     let _bar1 = &foo.bar1;
38     let _bar2 = &foo.bar1;
39 }
40
41 fn borrow_both_mut(foo: &mut Foo) {
42     let _bar1 = &mut foo.bar1;
43     let _bar2 = &mut foo.bar2;
44 }
45
46 fn borrow_both_mut_pattern(foo: &mut Foo) {
47     match *foo {
48         Foo { bar1: ref mut _bar1, bar2: ref mut _bar2 } => {}
49     }
50 }
51
52 fn borrow_var_and_pattern(foo: &mut Foo) {
53     let _bar1 = &mut foo.bar1;
54     match *foo {
55         Foo { bar1: ref mut _bar1, bar2: _ } => {}
56         //~^ ERROR cannot borrow
57     }
58 }
59
60 fn borrow_mut_and_base_imm(foo: &mut Foo) {
61     let _bar1 = &mut foo.bar1.int1;
62     let _foo1 = &foo.bar1; //~ ERROR cannot borrow
63     let _foo2 = &*foo; //~ ERROR cannot borrow
64 }
65
66 fn borrow_mut_and_base_mut(foo: &mut Foo) {
67     let _bar1 = &mut foo.bar1.int1;
68     let _foo1 = &mut foo.bar1; //~ ERROR cannot borrow
69 }
70
71 fn borrow_mut_and_base_mut2(foo: &mut Foo) {
72     let _bar1 = &mut foo.bar1.int1;
73     let _foo2 = &mut *foo; //~ ERROR cannot borrow
74 }
75
76 fn borrow_imm_and_base_mut(foo: &mut Foo) {
77     let _bar1 = &foo.bar1.int1;
78     let _foo1 = &mut foo.bar1; //~ ERROR cannot borrow
79 }
80
81 fn borrow_imm_and_base_mut2(foo: &mut Foo) {
82     let _bar1 = &foo.bar1.int1;
83     let _foo2 = &mut *foo; //~ ERROR cannot borrow
84 }
85
86 fn borrow_imm_and_base_imm(foo: &mut Foo) {
87     let _bar1 = &foo.bar1.int1;
88     let _foo1 = &foo.bar1;
89     let _foo2 = &*foo;
90 }
91
92 fn borrow_mut_and_imm(foo: &mut Foo) {
93     let _bar1 = &mut foo.bar1;
94     let _foo1 = &foo.bar2;
95 }
96
97 fn borrow_mut_from_imm(foo: &Foo) {
98     let _bar1 = &mut foo.bar1; //~ ERROR cannot borrow
99 }
100
101 fn borrow_long_path_both_mut(foo: &mut Foo) {
102     let _bar1 = &mut foo.bar1.int1;
103     let _foo1 = &mut foo.bar2.int2;
104 }
105
106 fn main() {}