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