1 // revisions: mirunsafeck thirunsafeck
2 // [thirunsafeck]compile-flags: -Z thir-unsafeck
6 use std::ops::{Deref, DerefMut};
7 use std::mem::ManuallyDrop;
15 fn new(value: T) -> Self { MockBox { value: [value] } }
18 impl<T> Deref for MockBox<T> {
20 fn deref(&self) -> &T { &self.value[0] }
23 impl<T> DerefMut for MockBox<T> {
24 fn deref_mut(&mut self) -> &mut T { &mut self.value[0] }
33 fn new() -> Self { MockVec { value: [] } }
36 impl<T> Deref for MockVec<T> {
38 fn deref(&self) -> &[T] { &self.value }
41 impl<T> DerefMut for MockVec<T> {
42 fn deref_mut(&mut self) -> &mut [T] { &mut self.value }
47 x: ManuallyDrop<((MockVec<u8>, MockVec<u8>), MockVec<u8>)>,
48 y: ManuallyDrop<MockBox<MockVec<u8>>>,
51 fn use_borrow<T>(_: &T) {}
53 unsafe fn parent_sibling_borrow() {
54 let mut u = U { x: ManuallyDrop::new(((MockVec::new(), MockVec::new()), MockVec::new())) };
55 let a = &mut (*u.x).0;
56 let b = &u.y; //~ ERROR cannot borrow `u` (via `u.y`)
60 unsafe fn parent_sibling_move() {
61 let u = U { x: ManuallyDrop::new(((MockVec::new(), MockVec::new()), MockVec::new())) };
62 let a = u.x.0; //~ERROR cannot move out of dereference
64 let b = u.y; //~ ERROR use of moved value: `u`
67 unsafe fn grandparent_sibling_borrow() {
68 let mut u = U { x: ManuallyDrop::new(((MockVec::new(), MockVec::new()), MockVec::new())) };
69 let a = &mut ((*u.x).0).0;
70 let b = &u.y; //~ ERROR cannot borrow `u` (via `u.y`)
74 unsafe fn grandparent_sibling_move() {
75 let u = U { x: ManuallyDrop::new(((MockVec::new(), MockVec::new()), MockVec::new())) };
76 let a = (u.x.0).0; //~ERROR cannot move out of dereference
78 let b = u.y; //~ ERROR use of moved value: `u`
81 unsafe fn deref_sibling_borrow() {
82 let mut u = U { y: ManuallyDrop::new(MockBox::default()) };
84 let b = &u.x; //~ ERROR cannot borrow `u` (via `u.x`)
88 unsafe fn deref_sibling_move() {
89 let u = U { x: ManuallyDrop::new(((MockVec::new(), MockVec::new()), MockVec::new())) };
90 // No way to test deref-move without Box in union
92 // let b = u.x; ERROR use of moved value: `u`