|
| 1 | +//@ run-pass |
| 2 | +//@ ignore-backends: gcc |
| 3 | +#![feature(c_variadic, const_c_variadic, const_destruct, const_raw_ptr_comparison)] |
| 4 | + |
| 5 | +use std::ffi::*; |
| 6 | + |
| 7 | +// In rustc we implement `va_arg` for the callee reading from a VaList, but still rely on LLVM |
| 8 | +// for exactly how to pass c-variadic arguments and for constructing the VaList. Here we test |
| 9 | +// that the rustc implementation works with what LLVM gives us. |
| 10 | + |
| 11 | +#[allow(improper_ctypes_definitions)] |
| 12 | +const unsafe extern "C" fn variadic<T: VaArgSafe>(mut ap: ...) -> (T, T) { |
| 13 | + let x = ap.arg::<T>(); |
| 14 | + // Intersperse a small type to test alignment logic. A `u32` (i.e. `c_uint`) is the smallest |
| 15 | + // type that implements `VaArgSafe`: smaller types would automatically be promoted. |
| 16 | + assert!(ap.arg::<u32>() == 0xAAAA_AAAA); |
| 17 | + let y = ap.arg::<T>(); |
| 18 | + |
| 19 | + (x, y) |
| 20 | +} |
| 21 | + |
| 22 | +macro_rules! roundtrip { |
| 23 | + ($ty:ty, $a:expr, $b:expr) => { |
| 24 | + const { |
| 25 | + let a: $ty = $a; |
| 26 | + let b: $ty = $b; |
| 27 | + let (x, y) = variadic::<$ty>(a, 0xAAAA_AAAAu32, b); |
| 28 | + assert!(a == x); |
| 29 | + assert!(b == y); |
| 30 | + } |
| 31 | + |
| 32 | + let a: $ty = $a; |
| 33 | + let b: $ty = $b; |
| 34 | + assert_eq!(variadic::<$ty>(a, 0xAAAA_AAAAu32, b), (a, b)) |
| 35 | + }; |
| 36 | +} |
| 37 | + |
| 38 | +macro_rules! roundtrip_ptr { |
| 39 | + ($ty:ty, $a:expr, $b:expr) => { |
| 40 | + const { |
| 41 | + let a: $ty = $a; |
| 42 | + let b: $ty = $b; |
| 43 | + let (x, y) = variadic::<$ty>(a, 0xAAAA_AAAAu32, b); |
| 44 | + assert!(a.guaranteed_eq(x).unwrap()); |
| 45 | + assert!(b.guaranteed_eq(y).unwrap()); |
| 46 | + } |
| 47 | + |
| 48 | + let a: $ty = $a; |
| 49 | + let b: $ty = $b; |
| 50 | + assert_eq!(variadic::<$ty>(a, 0xAAAA_AAAAu32, b), (a, b)) |
| 51 | + }; |
| 52 | +} |
| 53 | + |
| 54 | +fn main() { |
| 55 | + unsafe { |
| 56 | + roundtrip!(i32, -1, -2); |
| 57 | + roundtrip!(i64, -1, -2); |
| 58 | + roundtrip!(isize, -1, -2); |
| 59 | + roundtrip!(c_int, -1, -2); |
| 60 | + roundtrip!(c_long, -1, -2); |
| 61 | + roundtrip!(c_longlong, -1, -2); |
| 62 | + |
| 63 | + roundtrip!(u32, 1, 2); |
| 64 | + roundtrip!(u64, 1, 2); |
| 65 | + roundtrip!(usize, 1, 2); |
| 66 | + roundtrip!(c_uint, 1, 2); |
| 67 | + roundtrip!(c_ulong, 1, 2); |
| 68 | + roundtrip!(c_ulonglong, 1, 2); |
| 69 | + |
| 70 | + roundtrip!(f64, 3.14, 6.28); |
| 71 | + roundtrip!(c_double, 3.14, 6.28); |
| 72 | + |
| 73 | + static mut A: u32 = 1u32; |
| 74 | + static mut B: u32 = 2u32; |
| 75 | + roundtrip_ptr!(*const u32, &raw const A, &raw const B); |
| 76 | + roundtrip_ptr!(*mut u32, &raw mut A, &raw mut B); |
| 77 | + } |
| 78 | +} |
0 commit comments