Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 13 additions & 17 deletions tests/run-make/c-link-to-rust-va-list-fn/checkrust.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![crate_type = "staticlib"]
#![feature(c_variadic)]

use std::ffi::{CStr, CString, VaList, c_char, c_double, c_int, c_long, c_longlong};
use core::ffi::{CStr, VaList, c_char, c_double, c_int, c_long, c_longlong};

macro_rules! continue_if {
($cond:expr) => {
Expand All @@ -11,12 +11,8 @@ macro_rules! continue_if {
};
}

unsafe fn compare_c_str(ptr: *const c_char, val: &str) -> bool {
let cstr0 = CStr::from_ptr(ptr);
match CString::new(val) {
Ok(cstr1) => &*cstr1 == cstr0,
Err(_) => false,
}
unsafe fn compare_c_str(ptr: *const c_char, val: &CStr) -> bool {
val == CStr::from_ptr(ptr)
}

#[unsafe(no_mangle)]
Expand All @@ -35,42 +31,42 @@ pub unsafe extern "C" fn check_list_1(mut ap: VaList) -> usize {
continue_if!(ap.arg::<c_int>() == ';' as c_int);
continue_if!(ap.arg::<c_int>() == 0x32);
continue_if!(ap.arg::<c_int>() == 0x10000001);
continue_if!(compare_c_str(ap.arg::<*const c_char>(), "Valid!"));
continue_if!(compare_c_str(ap.arg::<*const c_char>(), c"Valid!"));
0
}

#[unsafe(no_mangle)]
pub unsafe extern "C" fn check_list_2(mut ap: VaList) -> usize {
continue_if!(ap.arg::<c_double>().floor() == 3.14f64.floor());
continue_if!(ap.arg::<c_double>() == 3.14f64);
continue_if!(ap.arg::<c_long>() == 12);
continue_if!(ap.arg::<c_int>() == 'a' as c_int);
continue_if!(ap.arg::<c_double>().floor() == 6.18f64.floor());
continue_if!(compare_c_str(ap.arg::<*const c_char>(), "Hello"));
continue_if!(ap.arg::<c_double>() == 6.28f64);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect the floor was used due to this typo in the value of tau. Clearly c-variadic functions should not truncate floating point numbers.

continue_if!(compare_c_str(ap.arg::<*const c_char>(), c"Hello"));
continue_if!(ap.arg::<c_int>() == 42);
continue_if!(compare_c_str(ap.arg::<*const c_char>(), "World"));
continue_if!(compare_c_str(ap.arg::<*const c_char>(), c"World"));
0
}

#[unsafe(no_mangle)]
pub unsafe extern "C" fn check_list_copy_0(mut ap: VaList) -> usize {
continue_if!(ap.arg::<c_double>().floor() == 6.28f64.floor());
continue_if!(ap.arg::<c_double>() == 6.28f64);
continue_if!(ap.arg::<c_int>() == 16);
continue_if!(ap.arg::<c_int>() == 'A' as c_int);
continue_if!(compare_c_str(ap.arg::<*const c_char>(), "Skip Me!"));
continue_if!(compare_c_str(ap.arg::<*const c_char>(), c"Skip Me!"));
let mut ap = ap.clone();
if compare_c_str(ap.arg::<*const c_char>(), "Correct") { 0 } else { 0xff }
if compare_c_str(ap.arg::<*const c_char>(), c"Correct") { 0 } else { 0xff }
}

#[unsafe(no_mangle)]
pub unsafe extern "C" fn check_varargs_0(_: c_int, mut ap: ...) -> usize {
continue_if!(ap.arg::<c_int>() == 42);
continue_if!(compare_c_str(ap.arg::<*const c_char>(), "Hello, World!"));
continue_if!(compare_c_str(ap.arg::<*const c_char>(), c"Hello, World!"));
0
}

#[unsafe(no_mangle)]
pub unsafe extern "C" fn check_varargs_1(_: c_int, mut ap: ...) -> usize {
continue_if!(ap.arg::<c_double>().floor() == 3.14f64.floor());
continue_if!(ap.arg::<c_double>() == 3.14f64);
continue_if!(ap.arg::<c_long>() == 12);
continue_if!(ap.arg::<c_int>() == 'A' as c_int);
continue_if!(ap.arg::<c_longlong>() == 1);
Expand Down
2 changes: 1 addition & 1 deletion tests/run-make/c-link-to-rust-va-list-fn/rmake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use run_make_support::{cc, extra_c_flags, run, rustc, static_lib_name};

fn main() {
rustc().input("checkrust.rs").run();
rustc().edition("2021").input("checkrust.rs").run();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needed for c"string literals".

cc().input("test.c")
.input(static_lib_name("checkrust"))
.out_exe("test")
Expand Down
Loading