From b32ac65af33d043a37f1d93d87df9113be9e4a2e Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Mon, 22 Jun 2026 17:43:42 +0100 Subject: [PATCH 1/2] Handle ops between i64 and isize --- cpp2rust/converter/converter.cpp | 2 +- tests/unit/out/refcount/size_t_ssize_t.rs | 9 ++++ tests/unit/out/unsafe/size_t_ssize_t.rs | 9 ++++ tests/unit/out/unsafe/unistd.rs | 54 ++++++++++++++++++++--- tests/unit/size_t_ssize_t.cpp | 11 +++++ 5 files changed, 78 insertions(+), 7 deletions(-) diff --git a/cpp2rust/converter/converter.cpp b/cpp2rust/converter/converter.cpp index 5f4ce08e..9ba109d2 100644 --- a/cpp2rust/converter/converter.cpp +++ b/cpp2rust/converter/converter.cpp @@ -2235,7 +2235,7 @@ void Converter::ConvertBinaryOperator(clang::BinaryOperator *expr) { auto op = opcode_as_string; op.remove_suffix(1); // remove '=' from operator StrCat(op); - Convert(rhs); + Convert(rhs, computation_result_type); } if (lhs_type->isBooleanType()) { StrCat(token::kDiff, token::kZero); diff --git a/tests/unit/out/refcount/size_t_ssize_t.rs b/tests/unit/out/refcount/size_t_ssize_t.rs index 24e72ffc..e45737c4 100644 --- a/tests/unit/out/refcount/size_t_ssize_t.rs +++ b/tests/unit/out/refcount/size_t_ssize_t.rs @@ -211,5 +211,14 @@ fn main_0() -> i32 { (((*sz.borrow()) as isize) - ((*ul.borrow()) as isize)), )); assert!(((*delta.borrow()) == 11_isize)); + let a64: Value = Rc::new(RefCell::new(100_i64)); + let b: Value = Rc::new(RefCell::new(30_isize)); + (*a64.borrow_mut()) -= ((*b.borrow()) as i64); + assert!(((*a64.borrow()) == 70_i64)); + (*a64.borrow_mut()) += ((*b.borrow()) as i64); + assert!(((*a64.borrow()) == 100_i64)); + let c: Value = Rc::new(RefCell::new((-20_i32 as isize))); + (*a64.borrow_mut()) -= ((*c.borrow()) as i64); + assert!(((*a64.borrow()) == 120_i64)); return (((*n.borrow()).wrapping_rem(7_usize)) as i32); } diff --git a/tests/unit/out/unsafe/size_t_ssize_t.rs b/tests/unit/out/unsafe/size_t_ssize_t.rs index dab5949d..1c5755f8 100644 --- a/tests/unit/out/unsafe/size_t_ssize_t.rs +++ b/tests/unit/out/unsafe/size_t_ssize_t.rs @@ -156,5 +156,14 @@ unsafe fn main_0() -> i32 { assert!(((smax) == (15_isize))); let mut delta: isize = ((sz as isize) - (ul as isize)); assert!(((delta) == (11_isize))); + let mut a64: i64 = 100_i64; + let mut b: isize = 30_isize; + a64 -= (b as i64); + assert!(((a64) == (70_i64))); + a64 += (b as i64); + assert!(((a64) == (100_i64))); + let mut c: isize = (-20_i32 as isize); + a64 -= (c as i64); + assert!(((a64) == (120_i64))); return (((n).wrapping_rem(7_usize)) as i32); } diff --git a/tests/unit/out/unsafe/unistd.rs b/tests/unit/out/unsafe/unistd.rs index 90fac9bc..0511676a 100644 --- a/tests/unit/out/unsafe/unistd.rs +++ b/tests/unit/out/unsafe/unistd.rs @@ -219,19 +219,58 @@ pub unsafe fn test_ftruncate_5() { assert!(((((libc::fclose(fp)) == (0)) as i32) != 0)); libc::unlink(path as *const i8); } -pub unsafe fn test_isatty_6() { +pub unsafe fn test_open_6() { + let mut fd: i32 = (unsafe { + libc::open( + (b"/dev/null\0".as_ptr().cast_mut()).cast_const() as *const i8, + 0 as i32, + (420), + ) + }); + assert!(((((fd) >= (-1_i32)) as i32) != 0)); + if ((((fd) >= (0)) as i32) != 0) { + libc::close(fd); + } + fd = (unsafe { + libc::open( + (b"/dev/null\0".as_ptr().cast_mut()).cast_const() as *const i8, + 0 as i32, + ) + }); + assert!(((((fd) >= (-1_i32)) as i32) != 0)); + if ((((fd) >= (0)) as i32) != 0) { + libc::close(fd); + } +} +pub unsafe fn test_fcntl_7() { + assert!(((((unsafe { libc::fcntl(0 as i32, 1 as i32,) }) >= (-1_i32)) as i32) != 0)); + let mut duped: i32 = (unsafe { libc::fcntl(0 as i32, 0 as i32, (100)) }); + assert!(((((duped) >= (-1_i32)) as i32) != 0)); + if ((((duped) >= (0)) as i32) != 0) { + libc::close(duped); + } +} +pub unsafe fn test_ioctl_8() { + let mut arg: i32 = 0; + assert!( + ((((unsafe { libc::ioctl(0 as i32, 0_u64 as u64, (&mut arg as *mut i32),) }) >= (-1_i32)) + as i32) + != 0) + ); +} +pub unsafe fn test_isatty_9() { printf( (b"%d\n\0".as_ptr().cast_mut()).cast_const() as *const i8, libc::isatty(0), ); } -pub unsafe fn test_geteuid_7() { +pub unsafe fn test_geteuid_10() { printf( (b"%u\n\0".as_ptr().cast_mut()).cast_const() as *const i8, libc::geteuid(), ); } -pub unsafe fn test_gethostname_8() { +pub unsafe fn test_gethostname_11() { let mut name: [u8; 256] = [0_u8; 256]; assert!( ((((libc::gethostname( @@ -257,8 +296,11 @@ unsafe fn main_0() -> i32 { (unsafe { test_unlink_3() }); (unsafe { test_pipe_4() }); (unsafe { test_ftruncate_5() }); - (unsafe { test_isatty_6() }); - (unsafe { test_geteuid_7() }); - (unsafe { test_gethostname_8() }); + (unsafe { test_open_6() }); + (unsafe { test_fcntl_7() }); + (unsafe { test_ioctl_8() }); + (unsafe { test_isatty_9() }); + (unsafe { test_geteuid_10() }); + (unsafe { test_gethostname_11() }); return 0; } diff --git a/tests/unit/size_t_ssize_t.cpp b/tests/unit/size_t_ssize_t.cpp index dbcbcc32..e07f8f52 100644 --- a/tests/unit/size_t_ssize_t.cpp +++ b/tests/unit/size_t_ssize_t.cpp @@ -85,5 +85,16 @@ int main() { ssize_t delta = (ssize_t)sz - (ssize_t)ul; assert(delta == 11); + typedef long long ll_t; + ll_t a64 = 100; + ssize_t b = 30; + a64 -= b; + assert(a64 == 70); + a64 += b; + assert(a64 == 100); + ssize_t c = -20; + a64 -= c; + assert(a64 == 120); + return (int)(n % 7); } From e796a358a86317db942c164cba199dbea2f96121 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Mon, 22 Jun 2026 17:48:11 +0100 Subject: [PATCH 2/2] Revert changed test --- tests/unit/out/unsafe/unistd.rs | 54 ++++----------------------------- 1 file changed, 6 insertions(+), 48 deletions(-) diff --git a/tests/unit/out/unsafe/unistd.rs b/tests/unit/out/unsafe/unistd.rs index 0511676a..90fac9bc 100644 --- a/tests/unit/out/unsafe/unistd.rs +++ b/tests/unit/out/unsafe/unistd.rs @@ -219,58 +219,19 @@ pub unsafe fn test_ftruncate_5() { assert!(((((libc::fclose(fp)) == (0)) as i32) != 0)); libc::unlink(path as *const i8); } -pub unsafe fn test_open_6() { - let mut fd: i32 = (unsafe { - libc::open( - (b"/dev/null\0".as_ptr().cast_mut()).cast_const() as *const i8, - 0 as i32, - (420), - ) - }); - assert!(((((fd) >= (-1_i32)) as i32) != 0)); - if ((((fd) >= (0)) as i32) != 0) { - libc::close(fd); - } - fd = (unsafe { - libc::open( - (b"/dev/null\0".as_ptr().cast_mut()).cast_const() as *const i8, - 0 as i32, - ) - }); - assert!(((((fd) >= (-1_i32)) as i32) != 0)); - if ((((fd) >= (0)) as i32) != 0) { - libc::close(fd); - } -} -pub unsafe fn test_fcntl_7() { - assert!(((((unsafe { libc::fcntl(0 as i32, 1 as i32,) }) >= (-1_i32)) as i32) != 0)); - let mut duped: i32 = (unsafe { libc::fcntl(0 as i32, 0 as i32, (100)) }); - assert!(((((duped) >= (-1_i32)) as i32) != 0)); - if ((((duped) >= (0)) as i32) != 0) { - libc::close(duped); - } -} -pub unsafe fn test_ioctl_8() { - let mut arg: i32 = 0; - assert!( - ((((unsafe { libc::ioctl(0 as i32, 0_u64 as u64, (&mut arg as *mut i32),) }) >= (-1_i32)) - as i32) - != 0) - ); -} -pub unsafe fn test_isatty_9() { +pub unsafe fn test_isatty_6() { printf( (b"%d\n\0".as_ptr().cast_mut()).cast_const() as *const i8, libc::isatty(0), ); } -pub unsafe fn test_geteuid_10() { +pub unsafe fn test_geteuid_7() { printf( (b"%u\n\0".as_ptr().cast_mut()).cast_const() as *const i8, libc::geteuid(), ); } -pub unsafe fn test_gethostname_11() { +pub unsafe fn test_gethostname_8() { let mut name: [u8; 256] = [0_u8; 256]; assert!( ((((libc::gethostname( @@ -296,11 +257,8 @@ unsafe fn main_0() -> i32 { (unsafe { test_unlink_3() }); (unsafe { test_pipe_4() }); (unsafe { test_ftruncate_5() }); - (unsafe { test_open_6() }); - (unsafe { test_fcntl_7() }); - (unsafe { test_ioctl_8() }); - (unsafe { test_isatty_9() }); - (unsafe { test_geteuid_10() }); - (unsafe { test_gethostname_11() }); + (unsafe { test_isatty_6() }); + (unsafe { test_geteuid_7() }); + (unsafe { test_gethostname_8() }); return 0; }