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