]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/nullable-pointer-ffi-compat.rs
Auto merge of #28816 - petrochenkov:unistruct, r=nrc
[rust.git] / src / test / run-pass / nullable-pointer-ffi-compat.rs
1 // Copyright 2014 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 // #11303, #11040:
12 // This would previously crash on i686 Linux due to abi differences
13 // between returning an Option<T> and T, where T is a non nullable
14 // pointer.
15 // If we have an enum with two variants such that one is zero sized
16 // and the other contains a nonnullable pointer, we don't use a
17 // separate discriminant. Instead we use that pointer field to differentiate
18 // between the 2 cases.
19 // Also, if the variant with the nonnullable pointer has no other fields
20 // then we simply express the enum as just a pointer and not wrap it
21 // in a struct.
22
23
24 use std::mem;
25
26 #[inline(never)]
27 extern "C" fn foo<'a>(x: &'a isize) -> Option<&'a isize> { Some(x) }
28
29 static FOO: isize = 0xDEADBEE;
30
31 pub fn main() {
32     unsafe {
33         let f: for<'a> extern "C" fn(&'a isize) -> &'a isize = mem::transmute(foo);
34         assert_eq!(*f(&FOO), FOO);
35     }
36 }