1 // ignore-tidy-linelength
19 fn x(&mut self) -> &mut u32 { &mut self.x }
23 fn x(&mut self) -> &mut u32 { &mut self.0 }
27 fn x(&mut self) -> &mut u32 {
29 Baz::X(ref mut value) => value
35 // Local and field from struct
37 let mut f = Foo { x: 22 };
39 f.x; //~ ERROR cannot use `f.x` because it was mutably borrowed
42 // Local and field from tuple-struct
46 g.0; //~ ERROR cannot use `g.0` because it was mutably borrowed
49 // Local and field from tuple
53 h.0; //~ ERROR cannot use `h.0` because it was mutably borrowed
56 // Local and field from enum
58 let mut e = Baz::X(2);
61 Baz::X(value) => value //~ ERROR cannot use `e.0` because it was mutably borrowed
65 // Local and field from union
67 let mut u = U { b: 0 };
69 u.a; //~ ERROR cannot use `u.a` because it was mutably borrowed
72 // Deref and field from struct
74 let mut f = Box::new(Foo { x: 22 });
76 f.x; //~ ERROR cannot use `f.x` because it was mutably borrowed
79 // Deref and field from tuple-struct
81 let mut g = Box::new(Bar(22));
83 g.0; //~ ERROR cannot use `g.0` because it was mutably borrowed
86 // Deref and field from tuple
88 let mut h = Box::new((22, 23));
90 h.0; //~ ERROR cannot use `h.0` because it was mutably borrowed
93 // Deref and field from enum
95 let mut e = Box::new(Baz::X(3));
98 Baz::X(value) => value
99 //~^ ERROR cannot use `e.0` because it was mutably borrowed
103 // Deref and field from union
105 let mut u = Box::new(U { b: 0 });
107 u.a; //~ ERROR cannot use `u.a` because it was mutably borrowed
112 let mut v = &[1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
115 &[x, _, .., _, _] => println!("{}", x),
116 //~^ ERROR cannot use `v[..]` because it was mutably borrowed
117 _ => panic!("other case"),
120 &[_, x, .., _, _] => println!("{}", x),
121 //~^ ERROR cannot use `v[..]` because it was mutably borrowed
122 _ => panic!("other case"),
125 &[_, _, .., x, _] => println!("{}", x),
126 //~^ ERROR cannot use `v[..]` because it was mutably borrowed
127 _ => panic!("other case"),
130 &[_, _, .., _, x] => println!("{}", x),
131 //~^ ERROR cannot use `v[..]` because it was mutably borrowed
132 _ => panic!("other case"),
138 let mut v = &[1, 2, 3, 4, 5];
141 &[x @ ..] => println!("{:?}", x),
142 //~^ ERROR cannot use `v[..]` because it was mutably borrowed
143 _ => panic!("other case"),
146 &[_, x @ ..] => println!("{:?}", x),
147 //~^ ERROR cannot use `v[..]` because it was mutably borrowed
148 _ => panic!("other case"),
151 &[x @ .., _] => println!("{:?}", x),
152 //~^ ERROR cannot use `v[..]` because it was mutably borrowed
153 _ => panic!("other case"),
156 &[_, x @ .., _] => println!("{:?}", x),
157 //~^ ERROR cannot use `v[..]` because it was mutably borrowed
158 _ => panic!("other case"),
164 enum E<X> { A(X), B { x: X } }
170 //~^ ERROR cannot borrow `e.0` as immutable because it is also borrowed as mutable
171 //~| ERROR cannot use `e` because it was mutably borrowed
172 println!("e.ax: {:?}", ax),
173 E::B { x: ref bx } =>
174 //~^ ERROR cannot borrow `e.x` as immutable because it is also borrowed as mutable
175 println!("e.bx: {:?}", bx),
181 struct F { x: u32, y: u32 };
182 struct S { x: F, y: (u32, u32), };
183 let mut s = S { x: F { x: 1, y: 2}, y: (999, 998) };
186 S { y: (ref y0, _), .. } =>
187 //~^ ERROR cannot borrow `s.y.0` as immutable because it is also borrowed as mutable
188 println!("y0: {:?}", y0),
189 _ => panic!("other case"),
192 S { x: F { y: ref x0, .. }, .. } =>
193 //~^ ERROR cannot borrow `s.x.y` as immutable because it is also borrowed as mutable
194 println!("x0: {:?}", x0),
195 _ => panic!("other case"),
206 fn bump<'a>(mut block: &mut Block<'a>) {
208 let p: &'a u8 = &*block.current;
209 //~^ ERROR cannot borrow `*block.current` as immutable because it is also borrowed as mutable
210 // See issue rust#38899
218 unrelated: *const u8,
221 unsafe fn bump2(mut block: *mut Block2) {
223 let p : *const u8 = &*(*block).current;
224 //~^ ERROR cannot borrow `*block.current` as immutable because it is also borrowed as mutable
225 // See issue rust#38899
231 struct F {x: u32, y: u32};
232 let mut v = &[F{x: 1, y: 2}, F{x: 3, y: 4}];
235 //~^ ERROR cannot use `v[_].y` because it was mutably borrowed
236 //~| ERROR cannot use `*v` because it was mutably borrowed
239 // Field of constant index
241 struct F {x: u32, y: u32};
242 let mut v = &[F{x: 1, y: 2}, F{x: 3, y: 4}];
245 &[_, F {x: ref xf, ..}] => println!("{}", xf),
246 //~^ ERROR cannot borrow `v[..].x` as immutable because it is also borrowed as mutable
247 _ => panic!("other case")
256 &mut x; //~ ERROR cannot borrow `x` as mutable more than once at a time
260 // Field from upvar nested
264 || { //~ ERROR captured variable cannot escape `FnMut` closure body
266 &mut x; //~ ERROR cannot borrow `x` as mutable more than once at a time
273 fn foo(x: Vec<i32>) {
276 drop(x); //~ ERROR use of moved value: `x`