|
1 |
| -fn borrow_mut<T>(x: &mut T) -> &mut T { x } |
2 |
| -fn borrow<T>(x: &T) -> &T { x } |
| 1 | +// run-pass |
| 2 | +#![allow(unused_braces)] |
| 3 | +#![allow(dead_code)] |
| 4 | +// pretty-expanded FIXME #23616 |
3 | 5 |
|
4 |
| -fn borrow_mut2<T>(_: &mut T, _: &mut T) {} |
5 |
| -fn borrow2<T>(_: &mut T, _: &T) {} |
| 6 | +use std::rc::Rc; |
6 | 7 |
|
7 |
| -fn double_mut_borrow<T>(x: &mut Box<T>) { |
8 |
| - let y = borrow_mut(x); |
9 |
| - let z = borrow_mut(x); |
10 |
| - //~^ ERROR cannot borrow `*x` as mutable more than once at a time |
11 |
| - drop((y, z)); |
| 8 | +// Examples from the "deref coercions" RFC, at rust-lang/rfcs#241. |
| 9 | + |
| 10 | +fn use_ref<T>(_: &T) {} |
| 11 | +fn use_mut<T>(_: &mut T) {} |
| 12 | + |
| 13 | +fn use_rc<T>(t: Rc<T>) { |
| 14 | + use_ref(&*t); // what you have to write today |
| 15 | + use_ref(&t); // what you'd be able to write |
| 16 | + use_ref(&&&&&&t); |
| 17 | + use_ref(&mut &&&&&t); |
| 18 | + use_ref(&&&mut &&&t); |
| 19 | +} |
| 20 | + |
| 21 | +fn use_mut_box<T>(mut t: &mut Box<T>) { |
| 22 | + use_mut(&mut *t); // what you have to write today |
| 23 | + use_mut(t); // what you'd be able to write |
| 24 | + use_mut(&mut &mut &mut t); |
| 25 | + |
| 26 | + use_ref(&*t); // what you have to write today |
| 27 | + use_ref(t); // what you'd be able to write |
| 28 | + use_ref(&&&&&&t); |
| 29 | + use_ref(&mut &&&&&t); |
| 30 | + use_ref(&&&mut &&&t); |
12 | 31 | }
|
13 | 32 |
|
14 |
| -fn double_imm_borrow(x: &mut Box<i32>) { |
15 |
| - let y = borrow(x); |
16 |
| - let z = borrow(x); |
17 |
| - **x += 1; |
18 |
| - //~^ ERROR cannot assign to `**x` because it is borrowed |
19 |
| - drop((y, z)); |
| 33 | +fn use_nested<T>(t: &Box<T>) { |
| 34 | + use_ref(&**t); // what you have to write today |
| 35 | + use_ref(t); // what you'd be able to write (note: recursive deref) |
| 36 | + use_ref(&&&&&&t); |
| 37 | + use_ref(&mut &&&&&t); |
| 38 | + use_ref(&&&mut &&&t); |
| 39 | +} |
| 40 | + |
| 41 | +fn use_slice(_: &[u8]) {} |
| 42 | +fn use_slice_mut(_: &mut [u8]) {} |
| 43 | + |
| 44 | +fn use_vec(mut v: Vec<u8>) { |
| 45 | + use_slice_mut(&mut v[..]); // what you have to write today |
| 46 | + use_slice_mut(&mut v); // what you'd be able to write |
| 47 | + use_slice_mut(&mut &mut &mut v); |
| 48 | + |
| 49 | + use_slice(&v[..]); // what you have to write today |
| 50 | + use_slice(&v); // what you'd be able to write |
| 51 | + use_slice(&&&&&&v); |
| 52 | + use_slice(&mut &&&&&v); |
| 53 | + use_slice(&&&mut &&&v); |
20 | 54 | }
|
21 | 55 |
|
22 |
| -fn double_mut_borrow2<T>(x: &mut Box<T>) { |
23 |
| - borrow_mut2(x, x); |
24 |
| - //~^ ERROR cannot borrow `*x` as mutable more than once at a time |
| 56 | +fn use_vec_ref(v: &Vec<u8>) { |
| 57 | + use_slice(&v[..]); // what you have to write today |
| 58 | + use_slice(v); // what you'd be able to write |
| 59 | + use_slice(&&&&&&v); |
| 60 | + use_slice(&mut &&&&&v); |
| 61 | + use_slice(&&&mut &&&v); |
25 | 62 | }
|
26 | 63 |
|
27 |
| -fn double_borrow2<T>(x: &mut Box<T>) { |
28 |
| - borrow2(x, x); |
29 |
| - //~^ ERROR cannot borrow `*x` as mutable because it is also borrowed as immutable |
| 64 | +fn use_op_rhs(s: &mut String) { |
| 65 | + *s += {&String::from(" ")}; |
30 | 66 | }
|
31 | 67 |
|
32 | 68 | pub fn main() {}
|
0 commit comments