From ff1a407dab4c789c93b067f1bdf3a6a747a79a74 Mon Sep 17 00:00:00 2001 From: fw Date: Fri, 11 Jul 2025 09:12:11 -0400 Subject: [PATCH 1/2] transpile: snapshots: add snapshot for #1024 this isn't how we want this code to be transpiled (it should use `wrapping_mul`), but it is how it transpiles now --- .../tests/snapshots/platform-specific/rnd.c | 16 ++++++++ .../tests/snapshots/platform-specific/rnd.rs | 30 +++++++++++++++ .../snapshots__transpile-linux@rnd.c.snap | 37 +++++++++++++++++++ .../snapshots__transpile-macos@rnd.c.snap | 35 ++++++++++++++++++ 4 files changed, 118 insertions(+) create mode 100644 c2rust-transpile/tests/snapshots/platform-specific/rnd.c create mode 100644 c2rust-transpile/tests/snapshots/platform-specific/rnd.rs create mode 100644 c2rust-transpile/tests/snapshots/snapshots__transpile-linux@rnd.c.snap create mode 100644 c2rust-transpile/tests/snapshots/snapshots__transpile-macos@rnd.c.snap diff --git a/c2rust-transpile/tests/snapshots/platform-specific/rnd.c b/c2rust-transpile/tests/snapshots/platform-specific/rnd.c new file mode 100644 index 0000000000..26e9224d42 --- /dev/null +++ b/c2rust-transpile/tests/snapshots/platform-specific/rnd.c @@ -0,0 +1,16 @@ +#include +#include + +uint32_t cur_rand_seed = 0; + +void set_rand_seed(uint32_t s) { + cur_rand_seed = s; +} + +uint32_t get_rand_seed() { + const uint32_t INCREMENT = 1; + const uint32_t MULTIPLIER = 0x015A4E35; + cur_rand_seed = MULTIPLIER * cur_rand_seed + INCREMENT; + uint32_t ret = abs((int32_t)cur_rand_seed); + return ret; +} \ No newline at end of file diff --git a/c2rust-transpile/tests/snapshots/platform-specific/rnd.rs b/c2rust-transpile/tests/snapshots/platform-specific/rnd.rs new file mode 100644 index 0000000000..5e77c6e0a9 --- /dev/null +++ b/c2rust-transpile/tests/snapshots/platform-specific/rnd.rs @@ -0,0 +1,30 @@ +#![allow( + dead_code, + mutable_transmutes, + non_camel_case_types, + non_snake_case, + non_upper_case_globals, + unused_assignments, + unused_mut +)] +extern "C" { + fn abs(_: std::ffi::c_int) -> std::ffi::c_int; +} +pub type __int32_t = std::ffi::c_int; +pub type __uint32_t = std::ffi::c_uint; +pub type int32_t = __int32_t; +pub type uint32_t = __uint32_t; +#[no_mangle] +pub static mut cur_rand_seed: uint32_t = 0 as std::ffi::c_int as uint32_t; +#[no_mangle] +pub unsafe extern "C" fn set_rand_seed(mut s: uint32_t) { + cur_rand_seed = s; +} +#[no_mangle] +pub unsafe extern "C" fn get_rand_seed() -> uint32_t { + let INCREMENT: uint32_t = 1 as std::ffi::c_int as uint32_t; + let MULTIPLIER: uint32_t = 0x15a4e35 as std::ffi::c_int as uint32_t; + cur_rand_seed = (MULTIPLIER * cur_rand_seed).wrapping_add(INCREMENT); + let mut ret: uint32_t = abs(cur_rand_seed as int32_t) as uint32_t; + return ret; +} diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile-linux@rnd.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile-linux@rnd.c.snap new file mode 100644 index 0000000000..a67f13ee18 --- /dev/null +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile-linux@rnd.c.snap @@ -0,0 +1,37 @@ +--- +source: c2rust-transpile/tests/snapshots.rs +assertion_line: 80 +expression: cat tests/snapshots/rnd.rs +input_file: c2rust-transpile/tests/snapshots/rnd.c +--- +#![allow( + dead_code, + mutable_transmutes, + non_camel_case_types, + non_snake_case, + non_upper_case_globals, + unused_assignments, + unused_mut +)] +extern "C" { + fn abs(_: std::ffi::c_int) -> std::ffi::c_int; +} +pub type __int32_t = std::ffi::c_int; +pub type __uint32_t = std::ffi::c_uint; +pub type int32_t = __int32_t; +pub type uint32_t = __uint32_t; +#[no_mangle] +pub static mut cur_rand_seed: uint32_t = 0 as std::ffi::c_int as uint32_t; +#[no_mangle] +pub unsafe extern "C" fn set_rand_seed(mut s: uint32_t) { + cur_rand_seed = s; +} +#[no_mangle] +pub unsafe extern "C" fn get_rand_seed() -> uint32_t { + let INCREMENT: uint32_t = 1 as std::ffi::c_int as uint32_t; + let MULTIPLIER: uint32_t = 0x15a4e35 as std::ffi::c_int as uint32_t; + cur_rand_seed = (MULTIPLIER * cur_rand_seed).wrapping_add(INCREMENT); + let mut ret: uint32_t = abs(cur_rand_seed as int32_t) as uint32_t; + return ret; +} + diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile-macos@rnd.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile-macos@rnd.c.snap new file mode 100644 index 0000000000..e49ffef23f --- /dev/null +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile-macos@rnd.c.snap @@ -0,0 +1,35 @@ +--- +source: c2rust-transpile/tests/snapshots.rs +assertion_line: 80 +expression: cat tests/snapshots/rnd.rs +input_file: c2rust-transpile/tests/snapshots/rnd.c +--- +#![allow( + dead_code, + mutable_transmutes, + non_camel_case_types, + non_snake_case, + non_upper_case_globals, + unused_assignments, + unused_mut +)] +extern "C" { + fn abs(_: std::ffi::c_int) -> std::ffi::c_int; +} +pub type int32_t = std::ffi::c_int; +pub type uint32_t = std::ffi::c_uint; +#[no_mangle] +pub static mut cur_rand_seed: uint32_t = 0 as std::ffi::c_int as uint32_t; +#[no_mangle] +pub unsafe extern "C" fn set_rand_seed(mut s: uint32_t) { + cur_rand_seed = s; +} +#[no_mangle] +pub unsafe extern "C" fn get_rand_seed() -> uint32_t { + let INCREMENT: uint32_t = 1 as std::ffi::c_int as uint32_t; + let MULTIPLIER: uint32_t = 0x15a4e35 as std::ffi::c_int as uint32_t; + cur_rand_seed = (MULTIPLIER * cur_rand_seed).wrapping_add(INCREMENT); + let mut ret: uint32_t = abs(cur_rand_seed as int32_t) as uint32_t; + return ret; +} + From d594f261ed901a4554f9900656b3fa148e33faea Mon Sep 17 00:00:00 2001 From: fw Date: Fri, 11 Jul 2025 00:33:38 -0400 Subject: [PATCH 2/2] transpile: operators: resolve type when checking for unsignedness other code in operators.rs remembers to do this, but this method forgot fixes #1024 --- c2rust-transpile/src/translator/operators.rs | 2 +- c2rust-transpile/tests/snapshots/platform-specific/rnd.rs | 2 +- .../tests/snapshots/snapshots__transpile-linux@rnd.c.snap | 2 +- .../tests/snapshots/snapshots__transpile-macos@rnd.c.snap | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/c2rust-transpile/src/translator/operators.rs b/c2rust-transpile/src/translator/operators.rs index d665a7b12a..717d2b7f16 100644 --- a/c2rust-transpile/src/translator/operators.rs +++ b/c2rust-transpile/src/translator/operators.rs @@ -506,7 +506,7 @@ impl<'c> Translation<'c> { ) -> TranslationResult> { let is_unsigned_integral_type = self .ast_context - .index(ctype) + .resolve_type(ctype) .kind .is_unsigned_integral_type(); diff --git a/c2rust-transpile/tests/snapshots/platform-specific/rnd.rs b/c2rust-transpile/tests/snapshots/platform-specific/rnd.rs index 5e77c6e0a9..8488a7c431 100644 --- a/c2rust-transpile/tests/snapshots/platform-specific/rnd.rs +++ b/c2rust-transpile/tests/snapshots/platform-specific/rnd.rs @@ -24,7 +24,7 @@ pub unsafe extern "C" fn set_rand_seed(mut s: uint32_t) { pub unsafe extern "C" fn get_rand_seed() -> uint32_t { let INCREMENT: uint32_t = 1 as std::ffi::c_int as uint32_t; let MULTIPLIER: uint32_t = 0x15a4e35 as std::ffi::c_int as uint32_t; - cur_rand_seed = (MULTIPLIER * cur_rand_seed).wrapping_add(INCREMENT); + cur_rand_seed = MULTIPLIER.wrapping_mul(cur_rand_seed).wrapping_add(INCREMENT); let mut ret: uint32_t = abs(cur_rand_seed as int32_t) as uint32_t; return ret; } diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile-linux@rnd.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile-linux@rnd.c.snap index a67f13ee18..fcc93e3df4 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile-linux@rnd.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile-linux@rnd.c.snap @@ -30,7 +30,7 @@ pub unsafe extern "C" fn set_rand_seed(mut s: uint32_t) { pub unsafe extern "C" fn get_rand_seed() -> uint32_t { let INCREMENT: uint32_t = 1 as std::ffi::c_int as uint32_t; let MULTIPLIER: uint32_t = 0x15a4e35 as std::ffi::c_int as uint32_t; - cur_rand_seed = (MULTIPLIER * cur_rand_seed).wrapping_add(INCREMENT); + cur_rand_seed = MULTIPLIER.wrapping_mul(cur_rand_seed).wrapping_add(INCREMENT); let mut ret: uint32_t = abs(cur_rand_seed as int32_t) as uint32_t; return ret; } diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile-macos@rnd.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile-macos@rnd.c.snap index e49ffef23f..0f9dedb0f0 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile-macos@rnd.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile-macos@rnd.c.snap @@ -28,7 +28,7 @@ pub unsafe extern "C" fn set_rand_seed(mut s: uint32_t) { pub unsafe extern "C" fn get_rand_seed() -> uint32_t { let INCREMENT: uint32_t = 1 as std::ffi::c_int as uint32_t; let MULTIPLIER: uint32_t = 0x15a4e35 as std::ffi::c_int as uint32_t; - cur_rand_seed = (MULTIPLIER * cur_rand_seed).wrapping_add(INCREMENT); + cur_rand_seed = MULTIPLIER.wrapping_mul(cur_rand_seed).wrapping_add(INCREMENT); let mut ret: uint32_t = abs(cur_rand_seed as int32_t) as uint32_t; return ret; }