rust/tests/run-pass/function_calls/exported_symbol.rs

63 lines
1.3 KiB
Rust
Raw Normal View History

2021-04-15 16:19:23 -05:00
#![feature(rustc_attrs)]
#[no_mangle]
extern "C" fn foo() -> i32 {
-1
}
#[export_name = "bar"]
fn bar() -> i32 {
-2
}
#[rustc_std_internal_symbol]
fn baz() -> i32 {
-3
}
fn main() {
// Repeat calls to make sure the `Instance` cache is not broken.
for _ in 0..3 {
extern "C" {
fn foo() -> i32;
}
2021-05-30 22:05:04 -05:00
2021-04-15 16:19:23 -05:00
assert_eq!(unsafe { foo() }, -1);
2021-05-30 22:05:04 -05:00
2021-04-15 16:19:23 -05:00
extern "Rust" {
fn bar() -> i32;
fn baz() -> i32;
}
2021-05-30 22:05:04 -05:00
2021-04-15 16:19:23 -05:00
assert_eq!(unsafe { bar() }, -2);
assert_eq!(unsafe { baz() }, -3);
2021-05-30 22:05:04 -05:00
#[allow(clashing_extern_declarations)]
{
extern "Rust" {
fn foo() -> i32;
}
assert_eq!(
unsafe {
std::mem::transmute::<unsafe fn() -> i32, unsafe extern "C" fn() -> i32>(foo)()
},
-1
);
extern "C" {
fn bar() -> i32;
fn baz() -> i32;
}
unsafe {
let transmute = |f| {
std::mem::transmute::<unsafe extern "C" fn() -> i32, unsafe fn() -> i32>(f)
};
assert_eq!(transmute(bar)(), -2);
assert_eq!(transmute(baz)(), -3);
}
}
2021-04-15 16:19:23 -05:00
}
}