summaryrefslogtreecommitdiff
path: root/src/test/run-pass-valgrind/cast-enum-with-dtor.rs
blob: 0de949471c68407cf66129e7aaa16d494d473d6d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// ignore-pretty : (#23623) problems when  ending with // comments

// no-prefer-dynamic

#![allow(dead_code)]
#![feature(const_fn, rustc_attrs)]

// check dtor calling order when casting enums.

use std::sync::atomic;
use std::sync::atomic::Ordering;
use std::mem;

enum E {
    A = 0,
    B = 1,
    C = 2
}

static FLAG: atomic::AtomicUsize = atomic::AtomicUsize::new(0);

impl Drop for E {
    fn drop(&mut self) {
        // avoid dtor loop
        unsafe { mem::forget(mem::replace(self, E::B)) };

        FLAG.store(FLAG.load(Ordering::SeqCst)+1, Ordering::SeqCst);
    }
}

#[rustc_no_mir] // FIXME #27840 MIR miscompiles this.
fn main() {
    assert_eq!(FLAG.load(Ordering::SeqCst), 0);
    {
        let e = E::C;
        assert_eq!(e as u32, 2);
        assert_eq!(FLAG.load(Ordering::SeqCst), 0);
    }
    assert_eq!(FLAG.load(Ordering::SeqCst), 1);
}