]> git.lizzy.rs Git - rust.git/blob - tests/run-pass/function_calls/exported_symbol.rs
Update tests for `#[no_mangle]` associated functions
[rust.git] / tests / run-pass / function_calls / exported_symbol.rs
1 #![feature(rustc_attrs)]
2
3 #[no_mangle]
4 extern "C" fn foo() -> i32 {
5     -1
6 }
7
8 #[export_name = "bar"]
9 fn bar() -> i32 {
10     -2
11 }
12
13 #[rustc_std_internal_symbol]
14 fn baz() -> i32 {
15     -3
16 }
17
18 struct AssocFn;
19
20 impl AssocFn {
21     #[no_mangle]
22     fn qux() -> i32 {
23         -4
24     }
25 }
26
27
28 fn main() {
29     // Repeat calls to make sure the `Instance` cache is not broken.
30     for _ in 0..3 {
31         extern "C" {
32             fn foo() -> i32;
33             fn free(_: *mut std::ffi::c_void);
34         }
35
36         assert_eq!(unsafe { foo() }, -1);
37
38         // `free()` is a built-in shim, so calling it will add ("free", None) to the cache.
39         // Test that the cache is not broken with ("free", None).
40         unsafe { free(std::ptr::null_mut()) }
41
42         extern "Rust" {
43             fn bar() -> i32;
44             fn baz() -> i32;
45             fn qux() -> i32;
46         }
47
48         assert_eq!(unsafe { bar() }, -2);
49         assert_eq!(unsafe { baz() }, -3);
50         assert_eq!(unsafe { qux() }, -4);
51
52         #[allow(clashing_extern_declarations)]
53         {
54             extern "Rust" {
55                 fn foo() -> i32;
56             }
57
58             assert_eq!(
59                 unsafe {
60                     std::mem::transmute::<unsafe fn() -> i32, unsafe extern "C" fn() -> i32>(foo)()
61                 },
62                 -1
63             );
64
65             extern "C" {
66                 fn bar() -> i32;
67                 fn baz() -> i32;
68                 fn qux() -> i32;
69             }
70
71             unsafe {
72                 let transmute = |f| {
73                     std::mem::transmute::<unsafe extern "C" fn() -> i32, unsafe fn() -> i32>(f)
74                 };
75                 assert_eq!(transmute(bar)(), -2);
76                 assert_eq!(transmute(baz)(), -3);
77                 assert_eq!(transmute(qux)(), -4);
78             }
79         }
80     }
81 }