]> git.proxmox.com Git - rustc.git/blob - src/test/run-pass/union/union-drop.rs
New upstream version 1.13.0+dfsg1
[rustc.git] / src / test / run-pass / union / union-drop.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 // Drop works for union itself.
12
13 #![feature(untagged_unions)]
14
15 struct S;
16
17 union U {
18 a: u8
19 }
20
21 union W {
22 a: S,
23 }
24
25 union Y {
26 a: S,
27 }
28
29 impl Drop for S {
30 fn drop(&mut self) {
31 unsafe { CHECK += 10; }
32 }
33 }
34
35 impl Drop for U {
36 fn drop(&mut self) {
37 unsafe { CHECK += 1; }
38 }
39 }
40
41 impl Drop for W {
42 fn drop(&mut self) {
43 unsafe { CHECK += 1; }
44 }
45 }
46
47 static mut CHECK: u8 = 0;
48
49 fn main() {
50 unsafe {
51 assert_eq!(CHECK, 0);
52 {
53 let u = U { a: 1 };
54 }
55 assert_eq!(CHECK, 1); // 1, dtor of U is called
56 {
57 let w = W { a: S };
58 }
59 assert_eq!(CHECK, 2); // 2, not 11, dtor of S is not called
60 {
61 let y = Y { a: S };
62 }
63 assert_eq!(CHECK, 2); // 2, not 12, dtor of S is not called
64 }
65 }