1 // ignore-tidy-linelength
3 #![feature(slice_patterns)]
21 fn x(&mut self) -> &mut u32 { &mut self.x }
25 fn x(&mut self) -> &mut u32 { &mut self.0 }
29 fn x(&mut self) -> &mut u32 {
31 Baz::X(ref mut value) => value
37 // Local and field from struct
39 let mut f = Foo { x: 22 };
41 f.x; //~ ERROR cannot use `f.x` because it was mutably borrowed
44 // Local and field from tuple-struct
48 g.0; //~ ERROR cannot use `g.0` because it was mutably borrowed
51 // Local and field from tuple
55 h.0; //~ ERROR cannot use `h.0` because it was mutably borrowed
58 // Local and field from enum
60 let mut e = Baz::X(2);
63 Baz::X(value) => value //~ ERROR cannot use `e.0` because it was mutably borrowed
67 // Local and field from union
69 let mut u = U { b: 0 };
71 u.a; //~ ERROR cannot use `u.a` because it was mutably borrowed
74 // Deref and field from struct
76 let mut f = Box::new(Foo { x: 22 });
78 f.x; //~ ERROR cannot use `f.x` because it was mutably borrowed
81 // Deref and field from tuple-struct
83 let mut g = Box::new(Bar(22));
85 g.0; //~ ERROR cannot use `g.0` because it was mutably borrowed
88 // Deref and field from tuple
90 let mut h = Box::new((22, 23));
92 h.0; //~ ERROR cannot use `h.0` because it was mutably borrowed
95 // Deref and field from enum
97 let mut e = Box::new(Baz::X(3));
100 Baz::X(value) => value
101 //~^ ERROR cannot use `e.0` because it was mutably borrowed
105 // Deref and field from union
107 let mut u = Box::new(U { b: 0 });
109 u.a; //~ ERROR cannot use `u.a` because it was mutably borrowed
114 let mut v = &[1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
117 &[x, _, .., _, _] => println!("{}", x),
118 //~^ ERROR cannot use `v[..]` because it was mutably borrowed
119 _ => panic!("other case"),
122 &[_, x, .., _, _] => println!("{}", x),
123 //~^ ERROR cannot use `v[..]` because it was mutably borrowed
124 _ => panic!("other case"),
127 &[_, _, .., x, _] => println!("{}", x),
128 //~^ ERROR cannot use `v[..]` because it was mutably borrowed
129 _ => panic!("other case"),
132 &[_, _, .., _, x] => println!("{}", x),
133 //~^ ERROR cannot use `v[..]` because it was mutably borrowed
134 _ => panic!("other case"),
140 let mut v = &[1, 2, 3, 4, 5];
143 &[x @ ..] => println!("{:?}", x),
144 //~^ ERROR cannot use `v[..]` because it was mutably borrowed
145 _ => panic!("other case"),
148 &[_, x @ ..] => println!("{:?}", x),
149 //~^ ERROR cannot use `v[..]` because it was mutably borrowed
150 _ => panic!("other case"),
153 &[x @ .., _] => println!("{:?}", x),
154 //~^ ERROR cannot use `v[..]` because it was mutably borrowed
155 _ => panic!("other case"),
158 &[_, x @ .., _] => println!("{:?}", x),
159 //~^ ERROR cannot use `v[..]` because it was mutably borrowed
160 _ => panic!("other case"),
166 enum E<X> { A(X), B { x: X } }
172 //~^ ERROR cannot borrow `e.0` as immutable because it is also borrowed as mutable
173 //~| ERROR cannot use `e` because it was mutably borrowed
174 println!("e.ax: {:?}", ax),
175 E::B { x: ref bx } =>
176 //~^ ERROR cannot borrow `e.x` as immutable because it is also borrowed as mutable
177 println!("e.bx: {:?}", bx),
183 struct F { x: u32, y: u32 };
184 struct S { x: F, y: (u32, u32), };
185 let mut s = S { x: F { x: 1, y: 2}, y: (999, 998) };
188 S { y: (ref y0, _), .. } =>
189 //~^ ERROR cannot borrow `s.y.0` as immutable because it is also borrowed as mutable
190 println!("y0: {:?}", y0),
191 _ => panic!("other case"),
194 S { x: F { y: ref x0, .. }, .. } =>
195 //~^ ERROR cannot borrow `s.x.y` as immutable because it is also borrowed as mutable
196 println!("x0: {:?}", x0),
197 _ => panic!("other case"),
208 fn bump<'a>(mut block: &mut Block<'a>) {
210 let p: &'a u8 = &*block.current;
211 //~^ WARNING cannot borrow `*block.current` as immutable because it is also borrowed as mutable
212 //~| this error has been downgraded
213 //~| this warning will become a hard error in the future
214 // Warning because of issue rust#38899
222 unrelated: *const u8,
225 unsafe fn bump2(mut block: *mut Block2) {
227 let p : *const u8 = &*(*block).current;
228 //~^ WARNING cannot borrow `*block.current` as immutable because it is also borrowed as mutable
229 //~| this error has been downgraded
230 //~| this warning will become a hard error in the future
231 // Warning because of issue rust#38899
237 struct F {x: u32, y: u32};
238 let mut v = &[F{x: 1, y: 2}, F{x: 3, y: 4}];
241 //~^ ERROR cannot use `v[_].y` because it was mutably borrowed
242 //~| ERROR cannot use `*v` because it was mutably borrowed
245 // Field of constant index
247 struct F {x: u32, y: u32};
248 let mut v = &[F{x: 1, y: 2}, F{x: 3, y: 4}];
251 &[_, F {x: ref xf, ..}] => println!("{}", xf),
252 //~^ ERROR cannot borrow `v[..].x` as immutable because it is also borrowed as mutable
253 _ => panic!("other case")
262 &mut x; //~ ERROR cannot borrow `x` as mutable more than once at a time
266 // Field from upvar nested
270 || { //~ ERROR captured variable cannot escape `FnMut` closure body
272 &mut x; //~ ERROR cannot borrow `x` as mutable more than once at a time
279 fn foo(x: Vec<i32>) {
282 drop(x); //~ ERROR use of moved value: `x`