]> git.lizzy.rs Git - rust.git/blob - src/test/ui/dropck/dropck-union.rs
Auto merge of #54624 - arielb1:evaluate-outlives, r=nikomatsakis
[rust.git] / src / test / ui / dropck / dropck-union.rs
1 // Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 #![feature(untagged_unions)]
12
13 use std::cell::Cell;
14 use std::ops::Deref;
15 use std::mem::ManuallyDrop;
16
17 union Wrap<T> { x: ManuallyDrop<T> }
18
19 impl<T> Drop for Wrap<T>  {
20     fn drop(&mut self) {
21         unsafe { std::ptr::drop_in_place(&mut *self.x as *mut T); }
22     }
23 }
24
25 impl<T> Wrap<T> {
26     fn new(x: T) -> Self {
27         Wrap { x: ManuallyDrop::new(x) }
28     }
29 }
30
31 impl<T> Deref for Wrap<T> {
32     type Target = T;
33     #[inline]
34     fn deref(&self) -> &Self::Target {
35         unsafe {
36             &self.x
37         }
38     }
39 }
40
41 struct C<'a>(Cell<Option<&'a C<'a>>>);
42
43 impl<'a> Drop for C<'a> {
44     fn drop(&mut self) {}
45 }
46
47 fn main() {
48     let v : Wrap<C> = Wrap::new(C(Cell::new(None)));
49     v.0.set(Some(&v)); //~ ERROR: `v` does not live long enough
50 }