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