]> git.lizzy.rs Git - rust.git/blob - tests/run-pass-valgrind/cast-enum-with-dtor.rs
Rollup merge of #107777 - compiler-errors:derive_const-actually-derive-const, r=fee1...
[rust.git] / tests / run-pass-valgrind / cast-enum-with-dtor.rs
1 #![allow(dead_code, cenum_impl_drop_cast)]
2
3 // check dtor calling order when casting enums.
4
5 use std::sync::atomic;
6 use std::sync::atomic::Ordering;
7 use std::mem;
8
9 enum E {
10     A = 0,
11     B = 1,
12     C = 2
13 }
14
15 static FLAG: atomic::AtomicUsize = atomic::AtomicUsize::new(0);
16
17 impl Drop for E {
18     fn drop(&mut self) {
19         // avoid dtor loop
20         unsafe { mem::forget(mem::replace(self, E::B)) };
21
22         FLAG.store(FLAG.load(Ordering::SeqCst)+1, Ordering::SeqCst);
23     }
24 }
25
26 fn main() {
27     assert_eq!(FLAG.load(Ordering::SeqCst), 0);
28     {
29         let e = E::C;
30         assert_eq!(e as u32, 2);
31         assert_eq!(FLAG.load(Ordering::SeqCst), 1);
32     }
33     assert_eq!(FLAG.load(Ordering::SeqCst), 1);
34 }