]> git.lizzy.rs Git - rust.git/blob - src/test/ui/dropck/dropck-union.rs
Merge commit '370c397ec9169809e5ad270079712e0043514240' into sync_cg_clif-2022-03-20
[rust.git] / src / test / ui / dropck / dropck-union.rs
1 use std::cell::Cell;
2 use std::ops::Deref;
3 use std::mem::ManuallyDrop;
4
5 union Wrap<T> { x: ManuallyDrop<T> }
6
7 impl<T> Drop for Wrap<T>  {
8     fn drop(&mut self) {
9         unsafe { std::ptr::drop_in_place(&mut *self.x as *mut T); }
10     }
11 }
12
13 impl<T> Wrap<T> {
14     fn new(x: T) -> Self {
15         Wrap { x: ManuallyDrop::new(x) }
16     }
17 }
18
19 impl<T> Deref for Wrap<T> {
20     type Target = T;
21     #[inline]
22     fn deref(&self) -> &Self::Target {
23         unsafe {
24             &self.x
25         }
26     }
27 }
28
29 struct C<'a>(Cell<Option<&'a C<'a>>>);
30
31 impl<'a> Drop for C<'a> {
32     fn drop(&mut self) {}
33 }
34
35 fn main() {
36     let v : Wrap<C> = Wrap::new(C(Cell::new(None)));
37     v.0.set(Some(&v)); //~ ERROR: `v` does not live long enough
38 }