]> git.proxmox.com Git - rustc.git/blame - src/test/run-pass-valgrind/cast-enum-with-dtor.rs
Imported Upstream version 1.9.0+dfsg1
[rustc.git] / src / test / run-pass-valgrind / cast-enum-with-dtor.rs
CommitLineData
62682a34
SL
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
54a0048b
SL
11// ignore-pretty : (#23623) problems when ending with // comments
12
7453a54e
SL
13// no-prefer-dynamic
14
62682a34 15#![allow(dead_code)]
54a0048b 16#![feature(const_fn, rustc_attrs)]
62682a34
SL
17
18// check dtor calling order when casting enums.
19
20use std::sync::atomic;
21use std::sync::atomic::Ordering;
22use std::mem;
23
24enum E {
25 A = 0,
26 B = 1,
27 C = 2
28}
29
30static FLAG: atomic::AtomicUsize = atomic::AtomicUsize::new(0);
31
32impl Drop for E {
33 fn drop(&mut self) {
34 // avoid dtor loop
35 unsafe { mem::forget(mem::replace(self, E::B)) };
36
37 FLAG.store(FLAG.load(Ordering::SeqCst)+1, Ordering::SeqCst);
38 }
39}
40
54a0048b 41#[rustc_no_mir] // FIXME #27840 MIR miscompiles this.
62682a34
SL
42fn main() {
43 assert_eq!(FLAG.load(Ordering::SeqCst), 0);
44 {
45 let e = E::C;
46 assert_eq!(e as u32, 2);
47 assert_eq!(FLAG.load(Ordering::SeqCst), 0);
48 }
49 assert_eq!(FLAG.load(Ordering::SeqCst), 1);
50}