]> git.proxmox.com Git - rustc.git/blob - src/test/run-pass-valgrind/cast-enum-with-dtor.rs
Update upstream source from tag 'upstream/1.24.1+dfsg1'
[rustc.git] / src / test / run-pass-valgrind / cast-enum-with-dtor.rs
1 // Copyright 2015 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 // no-prefer-dynamic
12
13 #![allow(dead_code)]
14
15 // check dtor calling order when casting enums.
16
17 use std::sync::atomic;
18 use std::sync::atomic::Ordering;
19 use std::mem;
20
21 enum E {
22 A = 0,
23 B = 1,
24 C = 2
25 }
26
27 static FLAG: atomic::AtomicUsize = atomic::AtomicUsize::new(0);
28
29 impl Drop for E {
30 fn drop(&mut self) {
31 // avoid dtor loop
32 unsafe { mem::forget(mem::replace(self, E::B)) };
33
34 FLAG.store(FLAG.load(Ordering::SeqCst)+1, Ordering::SeqCst);
35 }
36 }
37
38 fn main() {
39 assert_eq!(FLAG.load(Ordering::SeqCst), 0);
40 {
41 let e = E::C;
42 assert_eq!(e as u32, 2);
43 assert_eq!(FLAG.load(Ordering::SeqCst), 0);
44 }
45 assert_eq!(FLAG.load(Ordering::SeqCst), 0);
46 }