summaryrefslogtreecommitdiffstats
path: root/src/test/ui/structs-enums/align-struct.rs
blob: f5418e754b239a3d5485e32d41cd7488ebbc8218 (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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// run-pass
#![allow(dead_code)]

use std::mem;

// Raising alignment
#[repr(align(16))]
#[derive(Clone, Copy, Debug)]
struct Align16(i32);

// Lowering has no effect
#[repr(align(1))]
struct Align1(i32);

// Multiple attributes take the max
#[repr(align(4))]
#[repr(align(16))]
#[repr(align(8))]
struct AlignMany(i32);

// Raising alignment may not alter size.
#[repr(align(8))]
#[allow(dead_code)]
struct Align8Many {
    a: i32,
    b: i32,
    c: i32,
    d: u8,
}

enum Enum {
    #[allow(dead_code)]
    A(i32),
    B(Align16)
}

// Nested alignment - use `#[repr(C)]` to suppress field reordering for sizeof test
#[repr(C)]
struct Nested {
    a: i32,
    b: i32,
    c: Align16,
    d: i8,
}

#[repr(packed)]
struct Packed(i32);

#[repr(align(16))]
struct AlignContainsPacked {
    a: Packed,
    b: Packed,
}

#[repr(C, packed(4))]
struct Packed4C {
    a: u32,
    b: u64,
}

#[repr(align(16))]
struct AlignContainsPacked4C {
    a: Packed4C,
    b: u64,
}

// The align limit was originally smaller (2^15).
// Check that it works with big numbers.
#[repr(align(0x10000))]
struct AlignLarge {
    stuff: [u8; 0x10000],
}

union UnionContainsAlign {
    a: Align16,
    b: f32
}

impl Align16 {
    // return aligned type
    pub fn new(i: i32) -> Align16 {
        Align16(i)
    }
    // pass aligned type
    pub fn consume(a: Align16) -> i32 {
        a.0
    }
}

const CONST_ALIGN16: Align16 = Align16(7);
static STATIC_ALIGN16: Align16 = Align16(8);

// Check the actual address is aligned
fn is_aligned_to<T>(p: &T, align: usize) -> bool {
    let addr = p as *const T as usize;
    (addr & (align - 1)) == 0
}

pub fn main() {
    // check alignment and size by type and value
    assert_eq!(mem::align_of::<Align16>(), 16);
    assert_eq!(mem::size_of::<Align16>(), 16);

    let a = Align16(7);
    assert_eq!(a.0, 7);
    assert_eq!(mem::align_of_val(&a), 16);
    assert_eq!(mem::size_of_val(&a), 16);

    assert!(is_aligned_to(&a, 16));

    // lowering should have no effect
    assert_eq!(mem::align_of::<Align1>(), 4);
    assert_eq!(mem::size_of::<Align1>(), 4);
    let a = Align1(7);
    assert_eq!(a.0, 7);
    assert_eq!(mem::align_of_val(&a), 4);
    assert_eq!(mem::size_of_val(&a), 4);
    assert!(is_aligned_to(&a, 4));

    // when multiple attributes are specified the max should be used
    assert_eq!(mem::align_of::<AlignMany>(), 16);
    assert_eq!(mem::size_of::<AlignMany>(), 16);
    let a = AlignMany(7);
    assert_eq!(a.0, 7);
    assert_eq!(mem::align_of_val(&a), 16);
    assert_eq!(mem::size_of_val(&a), 16);
    assert!(is_aligned_to(&a, 16));

    // raising alignment should not reduce size
    assert_eq!(mem::align_of::<Align8Many>(), 8);
    assert_eq!(mem::size_of::<Align8Many>(), 16);
    let a = Align8Many { a: 1, b: 2, c: 3, d: 4 };
    assert_eq!(a.a, 1);
    assert_eq!(mem::align_of_val(&a), 8);
    assert_eq!(mem::size_of_val(&a), 16);
    assert!(is_aligned_to(&a, 8));

    // return type
    let a = Align16::new(1);
    assert_eq!(mem::align_of_val(&a), 16);
    assert_eq!(mem::size_of_val(&a), 16);
    assert_eq!(a.0, 1);
    assert!(is_aligned_to(&a, 16));
    assert_eq!(Align16::consume(a), 1);

    // check const alignment, size and value
    assert_eq!(mem::align_of_val(&CONST_ALIGN16), 16);
    assert_eq!(mem::size_of_val(&CONST_ALIGN16), 16);
    assert_eq!(CONST_ALIGN16.0, 7);
    assert!(is_aligned_to(&CONST_ALIGN16, 16));

    // check global static alignment, size and value
    assert_eq!(mem::align_of_val(&STATIC_ALIGN16), 16);
    assert_eq!(mem::size_of_val(&STATIC_ALIGN16), 16);
    assert_eq!(STATIC_ALIGN16.0, 8);
    assert!(is_aligned_to(&STATIC_ALIGN16, 16));

    // Note that the size of Nested may change if struct field re-ordering is enabled
    assert_eq!(mem::align_of::<Nested>(), 16);
    assert_eq!(mem::size_of::<Nested>(), 48);
    let a = Nested{ a: 1, b: 2, c: Align16(3), d: 4};
    assert_eq!(mem::align_of_val(&a), 16);
    assert_eq!(mem::align_of_val(&a.b), 4);
    assert_eq!(mem::align_of_val(&a.c), 16);
    assert_eq!(mem::size_of_val(&a), 48);
    assert!(is_aligned_to(&a, 16));
    // check the correct fields are indexed
    assert_eq!(a.a, 1);
    assert_eq!(a.b, 2);
    assert_eq!(a.c.0, 3);
    assert_eq!(a.d, 4);

    // enum should be aligned to max alignment
    assert_eq!(mem::align_of::<Enum>(), 16);
    assert_eq!(mem::align_of_val(&Enum::B(Align16(0))), 16);
    let e = Enum::B(Align16(15));
    match e {
        Enum::B(ref a) => {
            assert_eq!(a.0, 15);
            assert_eq!(mem::align_of_val(a), 16);
            assert_eq!(mem::size_of_val(a), 16);
        },
        _ => ()
    }
    assert!(is_aligned_to(&e, 16));

    // check union alignment
    assert_eq!(mem::align_of::<UnionContainsAlign>(), 16);
    assert_eq!(mem::size_of::<UnionContainsAlign>(), 16);
    let u = UnionContainsAlign { a: Align16(10) };
    unsafe {
        assert_eq!(mem::align_of_val(&u.a), 16);
        assert_eq!(mem::size_of_val(&u.a), 16);
        assert_eq!(u.a.0, 10);
        let UnionContainsAlign { a } = u;
        assert_eq!(a.0, 10);
    }

    // arrays of aligned elements should also be aligned
    assert_eq!(mem::align_of::<[Align16;2]>(), 16);
    assert_eq!(mem::size_of::<[Align16;2]>(), 32);

    let a = [Align16(0), Align16(1)];
    assert_eq!(mem::align_of_val(&a[0]), 16);
    assert_eq!(mem::align_of_val(&a[1]), 16);
    assert!(is_aligned_to(&a, 16));

    // check heap value is aligned
    assert_eq!(mem::align_of_val(Box::new(Align16(0)).as_ref()), 16);

    // check heap array is aligned
    let a = vec!(Align16(0), Align16(1));
    assert_eq!(mem::align_of_val(&a[0]), 16);
    assert_eq!(mem::align_of_val(&a[1]), 16);

    assert_eq!(mem::align_of::<AlignContainsPacked>(), 16);
    assert_eq!(mem::size_of::<AlignContainsPacked>(), 16);
    let a = AlignContainsPacked { a: Packed(1), b: Packed(2) };
    assert_eq!(mem::align_of_val(&a), 16);
    assert_eq!(mem::align_of_val(&a.a), 1);
    assert_eq!(mem::align_of_val(&a.b), 1);
    assert_eq!(mem::size_of_val(&a), 16);
    assert!(is_aligned_to(&a, 16));

    assert_eq!(mem::align_of::<AlignContainsPacked4C>(), 16);
    assert_eq!(mem::size_of::<AlignContainsPacked4C>(), 32);
    let a = AlignContainsPacked4C { a: Packed4C{ a: 1, b: 2 }, b: 3 };
    assert_eq!(mem::align_of_val(&a), 16);
    assert_eq!(mem::align_of_val(&a.a), 4);
    assert_eq!(mem::align_of_val(&a.b), mem::align_of::<u64>());
    assert_eq!(mem::size_of_val(&a), 32);
    assert!(is_aligned_to(&a, 16));

    let mut large = Box::new(AlignLarge {
        stuff: [0; 0x10000],
    });
    large.stuff[0] = 132;
    *large.stuff.last_mut().unwrap() = 102;
    assert_eq!(large.stuff[0], 132);
    assert_eq!(large.stuff.last(), Some(&102));
    assert_eq!(mem::align_of::<AlignLarge>(), 0x10000);
    assert_eq!(mem::align_of_val(&*large), 0x10000);
    assert!(is_aligned_to(&*large, 0x10000));
}