summary refs log tree commit diff
path: root/src/test/ui/consts/const-eval/const_transmute.rs
blob: f0e1d8263022b98bf104561a97278ce8d50a405e (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
51
52
53
54
55
// run-pass

#![feature(const_fn_union)]
#![allow(dead_code)]

#[repr(C)]
union Transmute<T: Copy, U: Copy> {
    t: T,
    u: U,
}

trait Bar {
    fn bar(&self) -> u32;
}

struct Foo {
    foo: u32,
    bar: bool,
}

impl Bar for Foo {
    fn bar(&self) -> u32 {
        self.foo
    }
}

impl Drop for Foo {
    fn drop(&mut self) {
        assert!(!self.bar);
        self.bar = true;
        println!("dropping Foo");
    }
}

#[derive(Copy, Clone)]
struct Fat<'a>(&'a Foo, &'static VTable);

struct VTable {
    drop: Option<for<'a> fn(&'a mut Foo)>,
    size: usize,
    align: usize,
    bar: for<'a> fn(&'a Foo) -> u32,
}

const FOO: &dyn Bar = &Foo { foo: 128, bar: false };
const G: Fat = unsafe { Transmute { t: FOO }.u };
const F: Option<for<'a> fn(&'a mut Foo)> = G.1.drop;
const H: for<'a> fn(&'a Foo) -> u32 = G.1.bar;

fn main() {
    let mut foo = Foo { foo: 99, bar: false };
    (F.unwrap())(&mut foo);
    std::mem::forget(foo); // already ran the drop impl
    assert_eq!(H(&Foo { foo: 42, bar: false }), 42);
}