]> git.proxmox.com Git - rustc.git/blame - src/test/ui/structs-enums/multiple-reprs.rs
New upstream version 1.67.1+dfsg1
[rustc.git] / src / test / ui / structs-enums / multiple-reprs.rs
CommitLineData
416331ca
XL
1// run-pass
2
0bf4aa26 3#![allow(dead_code)]
476ff2be 4
ff7c6d11
XL
5use std::mem::{size_of, align_of};
6use std::os::raw::c_int;
476ff2be
SL
7
8// The two enums that follow are designed so that bugs trigger layout optimization.
9// Specifically, if either of the following reprs used here is not detected by the compiler,
10// then the sizes will be wrong.
11
12#[repr(C, u8)]
13enum E1 {
14 A(u8, u16, u8),
15 B(u8, u16, u8)
16}
17
18#[repr(u8, C)]
19enum E2 {
20 A(u8, u16, u8),
21 B(u8, u16, u8)
22}
23
ff7c6d11
XL
24// Check that repr(int) and repr(C) are in fact different from the above
25
26#[repr(u8)]
27enum E3 {
28 A(u8, u16, u8),
29 B(u8, u16, u8)
30}
31
32#[repr(u16)]
33enum E4 {
34 A(u8, u16, u8),
35 B(u8, u16, u8)
36}
37
38#[repr(u32)]
39enum E5 {
40 A(u8, u16, u8),
41 B(u8, u16, u8)
42}
43
44#[repr(u64)]
45enum E6 {
46 A(u8, u16, u8),
47 B(u8, u16, u8)
48}
49
50#[repr(C)]
51enum E7 {
52 A(u8, u16, u8),
53 B(u8, u16, u8)
54}
55
476ff2be
SL
56// From pr 37429
57
58#[repr(C,packed)]
59pub struct p0f_api_query {
60 pub magic: u32,
61 pub addr_type: u8,
62 pub addr: [u8; 16],
63}
64
65pub fn main() {
ff7c6d11
XL
66 assert_eq!(size_of::<E1>(), 8);
67 assert_eq!(size_of::<E2>(), 8);
68 assert_eq!(size_of::<E3>(), 6);
69 assert_eq!(size_of::<E4>(), 8);
70 assert_eq!(size_of::<E5>(), align_size(10, align_of::<u32>()));
71 assert_eq!(size_of::<E6>(), align_size(14, align_of::<u64>()));
72 assert_eq!(size_of::<E7>(), align_size(6 + size_of::<c_int>(), align_of::<c_int>()));
476ff2be
SL
73 assert_eq!(size_of::<p0f_api_query>(), 21);
74}
ff7c6d11
XL
75
76fn align_size(size: usize, align: usize) -> usize {
77 if size % align != 0 {
78 size + (align - (size % align))
79 } else {
80 size
81 }
82}