]> git.lizzy.rs Git - rust.git/blob - tests/ui/abi/variadic-ffi.rs
Rollup merge of #106113 - krasimirgg:llvm-16-ext-tyid, r=nikic
[rust.git] / tests / ui / abi / variadic-ffi.rs
1 // run-pass
2 // ignore-wasm32-bare no libc to test ffi with
3 #![feature(c_variadic)]
4
5 use std::ffi::VaList;
6
7 #[link(name = "rust_test_helpers", kind = "static")]
8 extern "C" {
9     fn rust_interesting_average(_: u64, ...) -> f64;
10
11     // FIXME: we need to disable this lint for `VaList`,
12     // since it contains a `MaybeUninit<i32>` on the asmjs target,
13     // and this type isn't FFI-safe. This is OK for now,
14     // since the type is layout-compatible with `i32`.
15     #[cfg_attr(target_arch = "asmjs", allow(improper_ctypes))]
16     fn rust_valist_interesting_average(_: u64, _: VaList) -> f64;
17 }
18
19 pub unsafe extern "C" fn test_valist_forward(n: u64, mut ap: ...) -> f64 {
20     rust_valist_interesting_average(n, ap.as_va_list())
21 }
22
23 pub unsafe extern "C" fn test_va_copy(_: u64, mut ap: ...) {
24     let mut ap2 = ap.clone();
25     assert_eq!(rust_valist_interesting_average(2, ap2.as_va_list()) as i64, 30);
26
27     // Advance one pair in the copy before checking
28     let mut ap2 = ap.clone();
29     let _ = ap2.arg::<u64>();
30     let _ = ap2.arg::<f64>();
31     assert_eq!(rust_valist_interesting_average(2, ap2.as_va_list()) as i64, 50);
32
33     // Advance one pair in the original
34     let _ = ap.arg::<u64>();
35     let _ = ap.arg::<f64>();
36
37     let mut ap2 = ap.clone();
38     assert_eq!(rust_valist_interesting_average(2, ap2.as_va_list()) as i64, 50);
39
40     let mut ap2 = ap.clone();
41     let _ = ap2.arg::<u64>();
42     let _ = ap2.arg::<f64>();
43     assert_eq!(rust_valist_interesting_average(2, ap2.as_va_list()) as i64, 70);
44 }
45
46 pub fn main() {
47     // Call without variadic arguments
48     unsafe {
49         assert!(rust_interesting_average(0).is_nan());
50     }
51
52     // Call with direct arguments
53     unsafe {
54         assert_eq!(rust_interesting_average(1, 10i64, 10.0f64) as i64, 20);
55     }
56
57     // Call with named arguments, variable number of them
58     let (x1, x2, x3, x4) = (10i64, 10.0f64, 20i64, 20.0f64);
59     unsafe {
60         assert_eq!(rust_interesting_average(2, x1, x2, x3, x4) as i64, 30);
61     }
62
63     // A function that takes a function pointer
64     unsafe fn call(fp: unsafe extern "C" fn(u64, ...) -> f64) {
65         let (x1, x2, x3, x4) = (10i64, 10.0f64, 20i64, 20.0f64);
66         assert_eq!(fp(2, x1, x2, x3, x4) as i64, 30);
67     }
68
69     unsafe {
70         call(rust_interesting_average);
71
72         // Make a function pointer, pass indirectly
73         let x: unsafe extern "C" fn(u64, ...) -> f64 = rust_interesting_average;
74         call(x);
75     }
76
77     unsafe {
78         assert_eq!(test_valist_forward(2, 10i64, 10f64, 20i64, 20f64) as i64, 30);
79     }
80
81     unsafe {
82         test_va_copy(4, 10i64, 10f64, 20i64, 20f64, 30i64, 30f64, 40i64, 40f64);
83     }
84 }