]> git.lizzy.rs Git - rust.git/blob - tests/ui/union/union-borrow-move-parent-sibling.rs
add (currently ICEing) test
[rust.git] / tests / ui / union / union-borrow-move-parent-sibling.rs
1 // revisions: mirunsafeck thirunsafeck
2 // [thirunsafeck]compile-flags: -Z thir-unsafeck
3
4 #![allow(unused)]
5
6 use std::ops::{Deref, DerefMut};
7 use std::mem::ManuallyDrop;
8
9 #[derive(Default)]
10 struct MockBox<T> {
11     value: [T; 1],
12 }
13
14 impl<T> MockBox<T> {
15     fn new(value: T) -> Self { MockBox { value: [value] } }
16 }
17
18 impl<T> Deref for MockBox<T> {
19     type Target = T;
20     fn deref(&self) -> &T { &self.value[0] }
21 }
22
23 impl<T> DerefMut for MockBox<T> {
24     fn deref_mut(&mut self) -> &mut T { &mut self.value[0] }
25 }
26
27 #[derive(Default)]
28 struct MockVec<T> {
29     value: [T; 0],
30 }
31
32 impl<T> MockVec<T> {
33     fn new() -> Self { MockVec { value: [] } }
34 }
35
36 impl<T> Deref for MockVec<T> {
37     type Target = [T];
38     fn deref(&self) -> &[T] { &self.value }
39 }
40
41 impl<T> DerefMut for MockVec<T> {
42     fn deref_mut(&mut self) -> &mut [T] { &mut self.value }
43 }
44
45
46 union U {
47     x: ManuallyDrop<((MockVec<u8>, MockVec<u8>), MockVec<u8>)>,
48     y: ManuallyDrop<MockBox<MockVec<u8>>>,
49 }
50
51 fn use_borrow<T>(_: &T) {}
52
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`)
57     use_borrow(a);
58 }
59
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
63     let a = u.x;
64     let b = u.y; //~ ERROR use of moved value: `u`
65 }
66
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`)
71     use_borrow(a);
72 }
73
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
77     let a = u.x;
78     let b = u.y; //~ ERROR use of moved value: `u`
79 }
80
81 unsafe fn deref_sibling_borrow() {
82     let mut u = U { y: ManuallyDrop::new(MockBox::default()) };
83     let a = &mut *u.y;
84     let b = &u.x; //~ ERROR cannot borrow `u` (via `u.x`)
85     use_borrow(a);
86 }
87
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
91     // let a = *u.y;
92     // let b = u.x; ERROR use of moved value: `u`
93 }
94
95
96 fn main() {}