-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Represent C-like enums with a plain LLVM integer, not a struct.
This is needed so that the FFI works as expected on platforms that don't flatten aggregates the way the AMD64 ABI does, especially for `#[repr(C)]`. This moves more of `type_of` into `trans::adt`, because the type might or might not be an LLVM struct.
- Loading branch information
Showing
3 changed files
with
99 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
/*! | ||
* C-like enums have to be represented as LLVM ints, not wrapped in a | ||
* struct, because it's important for the FFI that they interoperate | ||
* with C integers/enums, and the ABI can treat structs differently. | ||
* For example, on i686-linux-gnu, a struct return value is passed by | ||
* storing to a hidden out parameter, whereas an integer would be | ||
* returned in a register. | ||
* | ||
* This test just checks that the ABIs for the enum and the plain | ||
* integer are compatible, rather than actually calling C code. | ||
* The unused parameter to `foo` is to increase the likelihood of | ||
* crashing if something goes wrong here. | ||
*/ | ||
|
||
#[repr(u32)] | ||
enum Foo { | ||
A = 0, | ||
B = 23 | ||
} | ||
|
||
#[inline(never)] | ||
extern "C" fn foo(_x: uint) -> Foo { B } | ||
|
||
pub fn main() { | ||
unsafe { | ||
let f: extern "C" fn(uint) -> u32 = std::cast::transmute(foo); | ||
assert_eq!(f(0xDEADBEEF), B as u32); | ||
} | ||
} |
8624d5b
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
saw approval from thestinger
at jld@8624d5b
8624d5b
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
merging jld/rust/enum-unstruct = 8624d5b into auto
8624d5b
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
jld/rust/enum-unstruct = 8624d5b merged ok, testing candidate = 4d5f8df0
8624d5b
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
some tests failed:
failure: http://buildbot.rust-lang.org/builders/auto-win-32-opt/builds/2626
exception: http://buildbot.rust-lang.org/builders/auto-mac-32-opt/builds/2621
exception: http://buildbot.rust-lang.org/builders/auto-mac-32-nopt-c/builds/467
exception: http://buildbot.rust-lang.org/builders/auto-mac-32-nopt-t/builds/467
exception: http://buildbot.rust-lang.org/builders/auto-mac-64-opt/builds/2625
exception: http://buildbot.rust-lang.org/builders/auto-mac-64-nopt-t/builds/1730
exception: http://buildbot.rust-lang.org/builders/auto-linux-32-opt/builds/2638
exception: http://buildbot.rust-lang.org/builders/auto-linux-32-nopt-c/builds/1730
exception: http://buildbot.rust-lang.org/builders/auto-linux-32-nopt-t/builds/1730
exception: http://buildbot.rust-lang.org/builders/auto-linux-64-opt/builds/2639
exception: http://buildbot.rust-lang.org/builders/auto-linux-64-nopt-t/builds/1730
exception: http://buildbot.rust-lang.org/builders/auto-win-32-nopt-c/builds/1730
exception: http://buildbot.rust-lang.org/builders/auto-win-32-nopt-t/builds/1732
exception: http://buildbot.rust-lang.org/builders/auto-bsd-64-opt/builds/2402