rust/tests/ui/union/union-borrow-move-parent-sibling.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

94 lines
2.3 KiB
Rust
Raw Normal View History

#![allow(unused)]
use std::ops::{Deref, DerefMut};
2022-06-30 02:33:18 +00:00
use std::mem::ManuallyDrop;
#[derive(Default)]
struct MockBox<T> {
value: [T; 1],
}
impl<T> MockBox<T> {
fn new(value: T) -> Self { MockBox { value: [value] } }
}
impl<T> Deref for MockBox<T> {
type Target = T;
fn deref(&self) -> &T { &self.value[0] }
}
impl<T> DerefMut for MockBox<T> {
fn deref_mut(&mut self) -> &mut T { &mut self.value[0] }
}
#[derive(Default)]
struct MockVec<T> {
value: [T; 0],
}
impl<T> MockVec<T> {
fn new() -> Self { MockVec { value: [] } }
}
impl<T> Deref for MockVec<T> {
type Target = [T];
fn deref(&self) -> &[T] { &self.value }
}
impl<T> DerefMut for MockVec<T> {
fn deref_mut(&mut self) -> &mut [T] { &mut self.value }
}
union U {
2022-06-30 02:33:18 +00:00
x: ManuallyDrop<((MockVec<u8>, MockVec<u8>), MockVec<u8>)>,
y: ManuallyDrop<MockBox<MockVec<u8>>>,
}
fn use_borrow<T>(_: &T) {}
unsafe fn parent_sibling_borrow() {
2022-06-30 02:33:18 +00:00
let mut u = U { x: ManuallyDrop::new(((MockVec::new(), MockVec::new()), MockVec::new())) };
let a = &mut (*u.x).0;
let b = &u.y; //~ ERROR cannot borrow `u` (via `u.y`)
use_borrow(a);
}
unsafe fn parent_sibling_move() {
2022-06-30 02:33:18 +00:00
let u = U { x: ManuallyDrop::new(((MockVec::new(), MockVec::new()), MockVec::new())) };
let a = u.x.0; //~ERROR cannot move out of dereference
let a = u.x;
let b = u.y; //~ ERROR use of moved value: `u`
}
unsafe fn grandparent_sibling_borrow() {
2022-06-30 02:33:18 +00:00
let mut u = U { x: ManuallyDrop::new(((MockVec::new(), MockVec::new()), MockVec::new())) };
let a = &mut ((*u.x).0).0;
let b = &u.y; //~ ERROR cannot borrow `u` (via `u.y`)
use_borrow(a);
}
unsafe fn grandparent_sibling_move() {
2022-06-30 02:33:18 +00:00
let u = U { x: ManuallyDrop::new(((MockVec::new(), MockVec::new()), MockVec::new())) };
let a = (u.x.0).0; //~ERROR cannot move out of dereference
let a = u.x;
let b = u.y; //~ ERROR use of moved value: `u`
}
unsafe fn deref_sibling_borrow() {
2022-06-30 02:33:18 +00:00
let mut u = U { y: ManuallyDrop::new(MockBox::default()) };
let a = &mut *u.y;
let b = &u.x; //~ ERROR cannot borrow `u` (via `u.x`)
use_borrow(a);
}
unsafe fn deref_sibling_move() {
2022-06-30 02:33:18 +00:00
let u = U { x: ManuallyDrop::new(((MockVec::new(), MockVec::new()), MockVec::new())) };
// No way to test deref-move without Box in union
// let a = *u.y;
// let b = u.x; ERROR use of moved value: `u`
}
fn main() {}