]> git.lizzy.rs Git - rust.git/blob - src/test/ui/union/union-nodrop.rs
Merge commit '3e7c6dec244539970b593824334876f8b6ed0b18' into clippyup
[rust.git] / src / test / ui / union / union-nodrop.rs
1 // run-pass
2
3 #![allow(dead_code)]
4
5 use std::mem::needs_drop;
6 use std::mem::ManuallyDrop;
7
8 struct NeedDrop;
9
10 impl Drop for NeedDrop {
11     fn drop(&mut self) {}
12 }
13
14 // Constant expressios allow `NoDrop` to go out of scope,
15 // unlike a value of the interior type implementing `Drop`.
16 static X: () = (NoDrop { inner: ManuallyDrop::new(NeedDrop) }, ()).1;
17
18 const Y: () = (NoDrop { inner: ManuallyDrop::new(NeedDrop) }, ()).1;
19
20 const fn _f() { (NoDrop { inner: ManuallyDrop::new(NeedDrop) }, ()).1 }
21
22 // A union that scrubs the drop glue from its inner type
23 union NoDrop<T> { inner: ManuallyDrop<T> }
24
25 // Copy currently can't be implemented on drop-containing unions,
26 // this may change later
27 // https://github.com/rust-lang/rust/pull/38934#issuecomment-271219289
28
29 // // We should be able to implement Copy for NoDrop
30 // impl<T> Copy for NoDrop<T> {}
31 // impl<T> Clone for NoDrop<T> {fn clone(&self) -> Self { *self }}
32
33 // // We should be able to implement Copy for things using NoDrop
34 // #[derive(Copy, Clone)]
35 struct Foo {
36     x: NoDrop<Box<u8>>
37 }
38
39 struct Baz {
40     x: NoDrop<Box<u8>>,
41     y: Box<u8>,
42 }
43
44 union ActuallyDrop<T> { inner: ManuallyDrop<T> }
45
46 impl<T> Drop for ActuallyDrop<T> {
47     fn drop(&mut self) {}
48 }
49
50 fn main() {
51     // NoDrop should not make needs_drop true
52     assert!(!needs_drop::<Foo>());
53     assert!(!needs_drop::<NoDrop<u8>>());
54     assert!(!needs_drop::<NoDrop<Box<u8>>>());
55     // presence of other drop types should still work
56     assert!(needs_drop::<Baz>());
57     // drop impl on union itself should work
58     assert!(needs_drop::<ActuallyDrop<u8>>());
59     assert!(needs_drop::<ActuallyDrop<Box<u8>>>());
60 }