]> git.proxmox.com Git - rustc.git/blob - src/test/ui/pattern/bindings-after-at/borrowck-pat-ref-mut-and-ref.rs
New upstream version 1.44.1+dfsg1
[rustc.git] / src / test / ui / pattern / bindings-after-at / borrowck-pat-ref-mut-and-ref.rs
1 #![feature(bindings_after_at)]
2 #![feature(move_ref_pattern)]
3
4 enum Option<T> {
5 None,
6 Some(T),
7 }
8
9 fn main() {
10 match &mut Some(1) {
11 ref mut z @ &mut Some(ref a) => {
12 //~^ ERROR cannot borrow value as immutable because it is also borrowed as mutable
13 //~| ERROR cannot borrow value as immutable because it is also borrowed as mutable
14 **z = None;
15 println!("{}", *a);
16 }
17 _ => ()
18 }
19
20 struct U;
21
22 // Prevent promotion:
23 fn u() -> U { U }
24
25 fn f1(ref a @ ref mut b: U) {}
26 //~^ ERROR cannot borrow value as mutable because it is also borrowed as immutable
27 fn f2(ref mut a @ ref b: U) {}
28 //~^ ERROR cannot borrow value as immutable because it is also borrowed as mutable
29 fn f3(ref a @ [ref b, ref mut mid @ .., ref c]: [U; 4]) {}
30 //~^ ERROR cannot borrow value as mutable because it is also borrowed as immutable
31 fn f4_also_moved(ref a @ ref mut b @ c: U) {}
32 //~^ ERROR cannot borrow value as mutable because it is also borrowed as immutable
33 //~| ERROR cannot move out of value because it is borrowed
34
35 let ref mut a @ (ref b @ ref mut c) = u(); // sub-in-sub
36 //~^ ERROR cannot borrow value as mutable more than once at a time
37 //~| ERROR cannot borrow value as mutable because it is also borrowed as immutable
38
39 let ref a @ ref mut b = U;
40 //~^ ERROR cannot borrow value as mutable because it is also borrowed as immutable
41 let ref mut a @ ref b = U;
42 //~^ ERROR cannot borrow value as immutable because it is also borrowed as mutable
43 let ref a @ (ref mut b, ref mut c) = (U, U);
44 //~^ ERROR cannot borrow value as mutable because it is also borrowed as immutable
45 let ref mut a @ (ref b, ref c) = (U, U);
46 //~^ ERROR cannot borrow value as immutable because it is also borrowed as mutable
47
48 let ref mut a @ ref b = u();
49 //~^ ERROR cannot borrow value as immutable because it is also borrowed as mutable
50 //~| ERROR cannot borrow value as immutable because it is also borrowed as mutable
51 *a = u();
52 drop(b);
53 let ref a @ ref mut b = u();
54 //~^ ERROR cannot borrow value as mutable because it is also borrowed as immutable
55 //~| ERROR cannot borrow value as mutable because it is also borrowed as immutable
56 *b = u();
57 drop(a);
58
59 let ref mut a @ ref b = U;
60 //~^ ERROR cannot borrow value as immutable because it is also borrowed as mutable
61 *a = U;
62 drop(b);
63 let ref a @ ref mut b = U;
64 //~^ ERROR cannot borrow value as mutable because it is also borrowed as immutable
65 *b = U;
66 drop(a);
67
68 match Ok(U) {
69 ref mut a @ Ok(ref b) | ref mut a @ Err(ref b) => {
70 //~^ ERROR cannot borrow value as immutable because it is also borrowed as mutable
71 //~| ERROR cannot borrow value as immutable because it is also borrowed as mutable
72 *a = Err(U);
73 drop(b);
74 }
75 }
76
77 match Ok(U) {
78 ref a @ Ok(ref mut b) | ref a @ Err(ref mut b) => {
79 //~^ ERROR cannot borrow value as mutable because it is also borrowed as immutable
80 //~| ERROR cannot borrow value as mutable because it is also borrowed as immutable
81 //~| ERROR cannot borrow value as mutable because it is also borrowed as immutable
82 //~| ERROR cannot borrow value as mutable because it is also borrowed as immutable
83 *b = U;
84 drop(a);
85 }
86 }
87
88 match Ok(U) {
89 ref a @ Ok(ref mut b) | ref a @ Err(ref mut b) if { *b = U; false } => {}
90 //~^ ERROR cannot borrow value as mutable because it is also borrowed as immutable
91 //~| ERROR cannot borrow value as mutable because it is also borrowed as immutable
92 //~| ERROR cannot assign to `*b`, as it is immutable for the pattern guard
93 _ => {}
94 }
95 match Ok(U) {
96 ref mut a @ Ok(ref b) | ref mut a @ Err(ref b) if { *a = Err(U); false } => {}
97 //~^ ERROR cannot borrow value as immutable because it is also borrowed as mutable
98 //~| ERROR cannot borrow value as immutable because it is also borrowed as mutable
99 //~| ERROR cannot assign to `*a`, as it is immutable for the pattern guard
100 _ => {}
101 }
102 match Ok(U) {
103 ref a @ Ok(ref mut b) | ref a @ Err(ref mut b) if { drop(b); false } => {}
104 //~^ ERROR cannot borrow value as mutable because it is also borrowed as immutable
105 //~| ERROR cannot borrow value as mutable because it is also borrowed as immutable
106 //~| ERROR cannot move out of `b` in pattern guard
107 //~| ERROR cannot move out of `b` in pattern guard
108 _ => {}
109 }
110 match Ok(U) {
111 ref mut a @ Ok(ref b) | ref mut a @ Err(ref b) if { drop(a); false } => {}
112 //~^ ERROR cannot borrow value as immutable because it is also borrowed as mutable
113 //~| ERROR cannot borrow value as immutable because it is also borrowed as mutable
114 //~| ERROR cannot move out of `a` in pattern guard
115 //~| ERROR cannot move out of `a` in pattern guard
116 _ => {}
117 }
118
119 let ref a @ (ref mut b, ref mut c) = (U, U);
120 //~^ ERROR cannot borrow value as mutable because it is also borrowed as immutable
121 *b = U;
122 *c = U;
123
124 let ref a @ (ref mut b, ref mut c) = (U, U);
125 //~^ ERROR cannot borrow value as mutable because it is also borrowed as immutable
126 //~| ERROR cannot borrow value as mutable because it is also borrowed as immutable
127 //~| ERROR cannot borrow value as mutable because it is also borrowed as immutable
128 *b = U;
129 drop(a);
130
131 let ref a @ (ref mut b, ref mut c) = (U, U);
132 //~^ ERROR cannot borrow value as mutable because it is also borrowed as immutable
133 *b = U; //~| ERROR cannot borrow value as mutable because it is also borrowed as immutable
134 *c = U; //~| ERROR cannot borrow value as mutable because it is also borrowed as immutable
135 drop(a);
136 let ref mut a @ (ref b, ref c) = (U, U);
137 //~^ ERROR cannot borrow value as immutable because it is also borrowed as mutable
138 }