Skip to content

Commit cec85ce

Browse files
Rollup merge of #153309 - folkertdev:c-variadic-link-test-cleanup, r=jieyouxu
Cleanup of c-variadic link test Some changes pulled out of #152980 that are just cosmetic, but will help make the code run on embedded targets. r? jieyouxu
2 parents 8845cc0 + c9e44b0 commit cec85ce

File tree

2 files changed

+14
-18
lines changed

2 files changed

+14
-18
lines changed

tests/run-make/c-link-to-rust-va-list-fn/checkrust.rs

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![crate_type = "staticlib"]
22
#![feature(c_variadic)]
33

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

66
macro_rules! continue_if {
77
($cond:expr) => {
@@ -11,12 +11,8 @@ macro_rules! continue_if {
1111
};
1212
}
1313

14-
unsafe fn compare_c_str(ptr: *const c_char, val: &str) -> bool {
15-
let cstr0 = CStr::from_ptr(ptr);
16-
match CString::new(val) {
17-
Ok(cstr1) => &*cstr1 == cstr0,
18-
Err(_) => false,
19-
}
14+
unsafe fn compare_c_str(ptr: *const c_char, val: &CStr) -> bool {
15+
val == CStr::from_ptr(ptr)
2016
}
2117

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

4238
#[unsafe(no_mangle)]
4339
pub unsafe extern "C" fn check_list_2(mut ap: VaList) -> usize {
44-
continue_if!(ap.arg::<c_double>().floor() == 3.14f64.floor());
40+
continue_if!(ap.arg::<c_double>() == 3.14f64);
4541
continue_if!(ap.arg::<c_long>() == 12);
4642
continue_if!(ap.arg::<c_int>() == 'a' as c_int);
47-
continue_if!(ap.arg::<c_double>().floor() == 6.18f64.floor());
48-
continue_if!(compare_c_str(ap.arg::<*const c_char>(), "Hello"));
43+
continue_if!(ap.arg::<c_double>() == 6.28f64);
44+
continue_if!(compare_c_str(ap.arg::<*const c_char>(), c"Hello"));
4945
continue_if!(ap.arg::<c_int>() == 42);
50-
continue_if!(compare_c_str(ap.arg::<*const c_char>(), "World"));
46+
continue_if!(compare_c_str(ap.arg::<*const c_char>(), c"World"));
5147
0
5248
}
5349

5450
#[unsafe(no_mangle)]
5551
pub unsafe extern "C" fn check_list_copy_0(mut ap: VaList) -> usize {
56-
continue_if!(ap.arg::<c_double>().floor() == 6.28f64.floor());
52+
continue_if!(ap.arg::<c_double>() == 6.28f64);
5753
continue_if!(ap.arg::<c_int>() == 16);
5854
continue_if!(ap.arg::<c_int>() == 'A' as c_int);
59-
continue_if!(compare_c_str(ap.arg::<*const c_char>(), "Skip Me!"));
55+
continue_if!(compare_c_str(ap.arg::<*const c_char>(), c"Skip Me!"));
6056
let mut ap = ap.clone();
61-
if compare_c_str(ap.arg::<*const c_char>(), "Correct") { 0 } else { 0xff }
57+
if compare_c_str(ap.arg::<*const c_char>(), c"Correct") { 0 } else { 0xff }
6258
}
6359

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

7167
#[unsafe(no_mangle)]
7268
pub unsafe extern "C" fn check_varargs_1(_: c_int, mut ap: ...) -> usize {
73-
continue_if!(ap.arg::<c_double>().floor() == 3.14f64.floor());
69+
continue_if!(ap.arg::<c_double>() == 3.14f64);
7470
continue_if!(ap.arg::<c_long>() == 12);
7571
continue_if!(ap.arg::<c_int>() == 'A' as c_int);
7672
continue_if!(ap.arg::<c_longlong>() == 1);

tests/run-make/c-link-to-rust-va-list-fn/rmake.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use run_make_support::{cc, extra_c_flags, run, rustc, static_lib_name};
1111

1212
fn main() {
13-
rustc().input("checkrust.rs").run();
13+
rustc().edition("2021").input("checkrust.rs").run();
1414
cc().input("test.c")
1515
.input(static_lib_name("checkrust"))
1616
.out_exe("test")

0 commit comments

Comments
 (0)