|
22 | 22 |
|
23 | 23 | // run-pass
|
24 | 24 |
|
25 |
| -type Boxed<'a, 'b> = Box<(&'a mut u32, &'b mut u32)>; |
| 25 | +// This function shows quite directly what is going on: We have a |
| 26 | +// reborrow of contents within the box. |
| 27 | +fn return_borrow_from_dropped_box_1(x: Box<&mut u32>) -> &mut u32 { &mut **x } |
26 | 28 |
|
27 |
| -fn return_borrow_from_dropped_box<'a>(x: Boxed<'a, '_>) -> &'a mut u32 { |
| 29 | +// This function is the way you'll probably see this in practice (the |
| 30 | +// reborrow is now implicit). |
| 31 | +fn return_borrow_from_dropped_box_2(x: Box<&mut u32>) -> &mut u32 { *x } |
| 32 | + |
| 33 | +// For the remaining tests we just add some fields or other |
| 34 | +// indirection to ensure that the compiler isn't just special-casing |
| 35 | +// the above `Box<&mut T>` as the only type that would work. |
| 36 | + |
| 37 | +// Here we add a tuple of indirection between the box and the |
| 38 | +// reference. |
| 39 | +type BoxedTup<'a, 'b> = Box<(&'a mut u32, &'b mut u32)>; |
| 40 | + |
| 41 | +fn return_borrow_of_field_from_dropped_box_1<'a>(x: BoxedTup<'a, '_>) -> &'a mut u32 { |
28 | 42 | &mut *x.0
|
29 | 43 | }
|
30 | 44 |
|
31 |
| -fn return_borrow_from_dropped_tupled_box<'a>(x: (Boxed<'a, '_>, &mut u32)) -> &'a mut u32 { |
| 45 | +fn return_borrow_of_field_from_dropped_box_2<'a>(x: BoxedTup<'a, '_>) -> &'a mut u32 { |
| 46 | + x.0 |
| 47 | +} |
| 48 | + |
| 49 | +fn return_borrow_from_dropped_tupled_box_1<'a>(x: (BoxedTup<'a, '_>, &mut u32)) -> &'a mut u32 { |
32 | 50 | &mut *(x.0).0
|
33 | 51 | }
|
34 | 52 |
|
| 53 | +fn return_borrow_from_dropped_tupled_box_2<'a>(x: (BoxedTup<'a, '_>, &mut u32)) -> &'a mut u32 { |
| 54 | + (x.0).0 |
| 55 | +} |
| 56 | + |
35 | 57 | fn basic_tests() {
|
36 | 58 | let mut x = 2;
|
37 | 59 | let mut y = 3;
|
38 | 60 | let mut z = 4;
|
39 |
| - *return_borrow_from_dropped_box(Box::new((&mut x, &mut y))) += 10; |
| 61 | + *return_borrow_from_dropped_box_1(Box::new(&mut x)) += 10; |
40 | 62 | assert_eq!((x, y, z), (12, 3, 4));
|
41 |
| - *return_borrow_from_dropped_tupled_box((Box::new((&mut x, &mut y)), &mut z)) += 10; |
| 63 | + *return_borrow_from_dropped_box_2(Box::new(&mut x)) += 10; |
42 | 64 | assert_eq!((x, y, z), (22, 3, 4));
|
| 65 | + *return_borrow_of_field_from_dropped_box_1(Box::new((&mut x, &mut y))) += 10; |
| 66 | + assert_eq!((x, y, z), (32, 3, 4)); |
| 67 | + *return_borrow_of_field_from_dropped_box_2(Box::new((&mut x, &mut y))) += 10; |
| 68 | + assert_eq!((x, y, z), (42, 3, 4)); |
| 69 | + *return_borrow_from_dropped_tupled_box_1((Box::new((&mut x, &mut y)), &mut z)) += 10; |
| 70 | + assert_eq!((x, y, z), (52, 3, 4)); |
| 71 | + *return_borrow_from_dropped_tupled_box_2((Box::new((&mut x, &mut y)), &mut z)) += 10; |
| 72 | + assert_eq!((x, y, z), (62, 3, 4)); |
43 | 73 | }
|
44 | 74 |
|
45 | 75 | // These scribbling tests have been transcribed from
|
|
0 commit comments