]> 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.49.0~beta.4+dfsg1
[rustc.git] / src / test / ui / pattern / bindings-after-at / borrowck-pat-ref-mut-and-ref.rs
1 #![feature(bindings_after_at)]
2
3 enum Option<T> {
4 None,
5 Some(T),
6 }
7
8 fn main() {
9 match &mut Some(1) {
10 ref mut z @ &mut Some(ref a) => {
11 //~^ ERROR cannot borrow value as immutable because it is also borrowed as mutable
12 //~| ERROR cannot borrow value as immutable because it is also borrowed as mutable
13 **z = None;
14 println!("{}", *a);
15 }
16 _ => ()
17 }
18
19 struct U;
20
21 // Prevent promotion:
22 fn u() -> U { U }
23
24 fn f1(ref a @ ref mut b: U) {}
25 //~^ ERROR cannot borrow value as mutable because it is also borrowed as immutable
26 fn f2(ref mut a @ ref b: U) {}
27 //~^ ERROR cannot borrow value as immutable because it is also borrowed as mutable
28 fn f3(ref a @ [ref b, ref mut mid @ .., ref c]: [U; 4]) {}
29 //~^ ERROR cannot borrow value as mutable because it is also borrowed as immutable
30 fn f4_also_moved(ref a @ ref mut b @ c: U) {}
31 //~^ ERROR cannot borrow value as mutable because it is also borrowed as immutable
32 //~| ERROR cannot move out of value because it is borrowed
33 //~| ERROR borrow of moved value
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 mutable because it is also borrowed as immutable
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 immutable because it is also borrowed as mutable
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 //~| ERROR cannot borrow value as immutable because it is also borrowed as mutable
122 *b = U;
123 *c = U;
124
125 let ref a @ (ref mut b, ref mut c) = (U, U);
126 //~^ ERROR cannot borrow value as mutable because it is also borrowed as immutable
127 //~| ERROR cannot borrow value as immutable because it is also borrowed as mutable
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 immutable because it is also borrowed as mutable
133 *b = U; //~| ERROR cannot borrow value as mutable because it is also borrowed as immutable
134 *c = U;
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 }