]> git.proxmox.com Git - rustc.git/blame - src/test/run-pass/enum-clike-ffi-as-int.rs
Imported Upstream version 1.9.0+dfsg1
[rustc.git] / src / test / run-pass / enum-clike-ffi-as-int.rs
CommitLineData
1a4d82fc
JJ
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
c34b1796 11
1a4d82fc
JJ
12/*!
13 * C-like enums have to be represented as LLVM ints, not wrapped in a
14 * struct, because it's important for the FFI that they interoperate
15 * with C integers/enums, and the ABI can treat structs differently.
16 * For example, on i686-linux-gnu, a struct return value is passed by
17 * storing to a hidden out parameter, whereas an integer would be
18 * returned in a register.
19 *
20 * This test just checks that the ABIs for the enum and the plain
21 * integer are compatible, rather than actually calling C code.
22 * The unused parameter to `foo` is to increase the likelihood of
23 * crashing if something goes wrong here.
24 */
25
26#[repr(u32)]
27enum Foo {
54a0048b
SL
28 A = 0,
29 B = 23
1a4d82fc
JJ
30}
31
32#[inline(never)]
c34b1796 33extern "C" fn foo(_x: usize) -> Foo { Foo::B }
1a4d82fc
JJ
34
35pub fn main() {
54a0048b
SL
36 unsafe {
37 let f: extern "C" fn(usize) -> u32 =
38 ::std::mem::transmute(foo as extern "C" fn(usize) -> Foo);
39 assert_eq!(f(0xDEADBEEF), Foo::B as u32);
40 }
1a4d82fc 41}