summaryrefslogtreecommitdiffstats
path: root/tests/ui/union/union-packed.rs
blob: 9c6398bf5aa631039253d25a501cc344f9b6df10 (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// run-pass
// revisions: mirunsafeck thirunsafeck
// [thirunsafeck]compile-flags: -Z thir-unsafeck

#![allow(dead_code)]
#![allow(non_snake_case)]

use std::mem::{size_of, size_of_val, align_of, align_of_val};

struct S {
    a: u16,
    b: [u8; 3],
}

#[repr(packed)]
struct Sp1 {
    a: u16,
    b: [u8; 3],
}

#[repr(packed(2))]
struct Sp2 {
    a: u16,
    b: [u8; 3],
}

union U {
    a: u16,
    b: [u8; 3],
}

#[repr(packed)]
union Up1 {
    a: u16,
    b: [u8; 3],
}

#[repr(packed(2))]
union Up2 {
    a: u16,
    b: [u8; 3],
}

#[repr(C, packed(4))]
union Up4c {
    a: u16,
    b: [u8; 3],
}

const CS: S = S { a: 0, b: [0, 0, 0] };
const CSP1: Sp1 = Sp1 { a: 0, b: [0, 0, 0] };
const CSP2: Sp2 = Sp2 { a: 0, b: [0, 0, 0] };
const CU: U = U { b: [0, 0, 0] };
const CUP1: Up1 = Up1 { b: [0, 0, 0] };
const CUP2: Up2 = Up2 { b: [0, 0, 0] };
const CUP4C: Up4c = Up4c { b: [0, 0, 0] };

fn main() {
    let s = S { a: 0, b: [0, 0, 0] };
    assert_eq!(size_of::<S>(), 6);
    assert_eq!(size_of_val(&s), 6);
    assert_eq!(size_of_val(&CS), 6);
    assert_eq!(align_of::<S>(), 2);
    assert_eq!(align_of_val(&s), 2);
    assert_eq!(align_of_val(&CS), 2);

    let sp1 = Sp1 { a: 0, b: [0, 0, 0] };
    assert_eq!(size_of::<Sp1>(), 5);
    assert_eq!(size_of_val(&sp1), 5);
    assert_eq!(size_of_val(&CSP1), 5);
    assert_eq!(align_of::<Sp1>(), 1);
    assert_eq!(align_of_val(&sp1), 1);
    assert_eq!(align_of_val(&CSP1), 1);

    let sp2 = Sp2 { a: 0, b: [0, 0, 0] };
    assert_eq!(size_of::<Sp2>(), 6);
    assert_eq!(size_of_val(&sp2), 6);
    assert_eq!(size_of_val(&CSP2), 6);
    assert_eq!(align_of::<Sp2>(), 2);
    assert_eq!(align_of_val(&sp2), 2);
    assert_eq!(align_of_val(&CSP2), 2);

    let u = U { b: [0, 0, 0] };
    assert_eq!(size_of::<U>(), 4);
    assert_eq!(size_of_val(&u), 4);
    assert_eq!(size_of_val(&CU), 4);
    assert_eq!(align_of::<U>(), 2);
    assert_eq!(align_of_val(&u), 2);
    assert_eq!(align_of_val(&CU), 2);

    let Up1 = Up1 { b: [0, 0, 0] };
    assert_eq!(size_of::<Up1>(), 3);
    assert_eq!(size_of_val(&Up1), 3);
    assert_eq!(size_of_val(&CUP1), 3);
    assert_eq!(align_of::<Up1>(), 1);
    assert_eq!(align_of_val(&Up1), 1);
    assert_eq!(align_of_val(&CUP1), 1);

    let up2 = Up2 { b: [0, 0, 0] };
    assert_eq!(size_of::<Up2>(), 4);
    assert_eq!(size_of_val(&up2), 4);
    assert_eq!(size_of_val(&CUP2), 4);
    assert_eq!(align_of::<Up2>(), 2);
    assert_eq!(align_of_val(&up2), 2);
    assert_eq!(align_of_val(&CUP2), 2);

    let up4c = Up4c { b: [0, 0, 0] };
    assert_eq!(size_of::<Up4c>(), 4);
    assert_eq!(size_of_val(&up4c), 4);
    assert_eq!(size_of_val(&CUP4C), 4);
    assert_eq!(align_of::<Up4c>(), 2);
    assert_eq!(align_of_val(&up4c), 2);
    assert_eq!(align_of_val(&CUP4C), 2);

    hybrid::check_hybrid();
}

mod hybrid {
    use std::mem::{size_of, align_of};

    #[repr(packed)]
    #[derive(Copy, Clone)]
    struct S1 {
        a: u16,
        b: u8,
    }

    #[repr(packed)]
    union U {
        s: S1,
        c: u16,
    }

    #[repr(packed)]
    struct S2 {
        d: u8,
        u: U,
    }

    #[repr(C, packed(2))]
    struct S1C {
        a: u16,
        b: u8,
    }

    #[repr(C, packed(2))]
    union UC {
        s: S1,
        c: u16,
    }

    #[repr(C, packed(2))]
    struct S2C {
        d: u8,
        u: UC,
    }

    pub fn check_hybrid() {
        assert_eq!(align_of::<S1>(), 1);
        assert_eq!(size_of::<S1>(), 3);
        assert_eq!(align_of::<U>(), 1);
        assert_eq!(size_of::<U>(), 3);
        assert_eq!(align_of::<S2>(), 1);
        assert_eq!(size_of::<S2>(), 4);

        assert_eq!(align_of::<S1C>(), 2);
        assert_eq!(size_of::<S1C>(), 4);
        assert_eq!(align_of::<UC>(), 2);
        assert_eq!(size_of::<UC>(), 4);
        assert_eq!(align_of::<S2C>(), 2);
        assert_eq!(size_of::<S2C>(), 6);
    }
}