]> git.lizzy.rs Git - rust.git/blob - src/test/ui/structs-enums/enum-clike-ffi-as-int.rs
Rollup merge of #89468 - FabianWolff:issue-89358, r=jackh726
[rust.git] / src / test / ui / structs-enums / enum-clike-ffi-as-int.rs
1 // run-pass
2 #![allow(dead_code)]
3
4 /*!
5  * C-like enums have to be represented as LLVM ints, not wrapped in a
6  * struct, because it's important for the FFI that they interoperate
7  * with C integers/enums, and the ABI can treat structs differently.
8  * For example, on i686-linux-gnu, a struct return value is passed by
9  * storing to a hidden out parameter, whereas an integer would be
10  * returned in a register.
11  *
12  * This test just checks that the ABIs for the enum and the plain
13  * integer are compatible, rather than actually calling C code.
14  * The unused parameter to `foo` is to increase the likelihood of
15  * crashing if something goes wrong here.
16  */
17
18 #[repr(u32)]
19 enum Foo {
20     A = 0,
21     B = 23
22 }
23
24 #[inline(never)]
25 extern "C" fn foo(_x: usize) -> Foo { Foo::B }
26
27 pub fn main() {
28     unsafe {
29         let f: extern "C" fn(usize) -> u32 =
30             ::std::mem::transmute(foo as extern "C" fn(usize) -> Foo);
31         assert_eq!(f(0xDEADBEEF), Foo::B as u32);
32     }
33 }