]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/enum-clike-ffi-as-int.rs
1cbcaac379f09c0f90d71c8e6ab6c3ea2518eb84
[rust.git] / src / test / run-pass / enum-clike-ffi-as-int.rs
1 // Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 /*!
12  * C-like enums have to be represented as LLVM ints, not wrapped in a
13  * struct, because it's important for the FFI that they interoperate
14  * with C integers/enums, and the ABI can treat structs differently.
15  * For example, on i686-linux-gnu, a struct return value is passed by
16  * storing to a hidden out parameter, whereas an integer would be
17  * returned in a register.
18  *
19  * This test just checks that the ABIs for the enum and the plain
20  * integer are compatible, rather than actually calling C code.
21  * The unused parameter to `foo` is to increase the likelihood of
22  * crashing if something goes wrong here.
23  */
24
25 #[repr(u32)]
26 enum Foo {
27   A = 0,
28   B = 23
29 }
30
31 #[inline(never)]
32 extern "C" fn foo(_x: uint) -> Foo { Foo::B }
33
34 pub fn main() {
35   unsafe {
36     let f: extern "C" fn(uint) -> u32 = ::std::mem::transmute(foo);
37     assert_eq!(f(0xDEADBEEF), Foo::B as u32);
38   }
39 }