]> git.proxmox.com Git - rustc.git/blame - src/test/run-pass/enum-clike-ffi-as-int.rs
Imported Upstream version 1.0.0~beta.3
[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 {
28 A = 0,
29 B = 23
30}
31
32#[inline(never)]
c34b1796 33extern "C" fn foo(_x: usize) -> Foo { Foo::B }
1a4d82fc
JJ
34
35pub fn main() {
36 unsafe {
c34b1796 37 let f: extern "C" fn(usize) -> u32 = ::std::mem::transmute(foo);
1a4d82fc
JJ
38 assert_eq!(f(0xDEADBEEF), Foo::B as u32);
39 }
40}