From 2b164e3e2d612de7b99560dea16d0d28f6457e34 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Mon, 18 May 2026 10:04:30 +0100 Subject: [PATCH] Hoist fwrite/fread args to avoid borrow errors --- rules/stdio/ir_refcount.json | 22 ++++++++------- rules/stdio/ir_unsafe.json | 22 ++++++++------- rules/stdio/tgt_refcount.rs | 12 ++++++-- rules/stdio/tgt_unsafe.rs | 12 ++++++-- .../out/refcount/fn_ptr_stdlib_compare.rs | 26 +++++++++-------- .../unit/out/unsafe/fn_ptr_stdlib_compare.rs | 28 +++++++++++-------- 6 files changed, 74 insertions(+), 48 deletions(-) diff --git a/rules/stdio/ir_refcount.json b/rules/stdio/ir_refcount.json index da8fd01a..39fdde38 100644 --- a/rules/stdio/ir_refcount.json +++ b/rules/stdio/ir_refcount.json @@ -431,7 +431,7 @@ "f5": { "body": [ { - "text": "libcc2rs::fread_refcount(" + "text": "let __a0 = " }, { "placeholder": { @@ -440,7 +440,7 @@ } }, { - "text": ", " + "text": ";\n let __a1 = " }, { "placeholder": { @@ -449,7 +449,7 @@ } }, { - "text": ", " + "text": ";\n let __a2 = " }, { "placeholder": { @@ -458,7 +458,7 @@ } }, { - "text": ", " + "text": ";\n let __a3 = " }, { "method_call": { @@ -478,9 +478,10 @@ } }, { - "text": ")" + "text": ";\n libcc2rs::fread_refcount(__a0, __a1, __a2, __a3)" } ], + "multi_statement": true, "params": { "a0": { "type": "AnyPtr" @@ -503,7 +504,7 @@ "f6": { "body": [ { - "text": "libcc2rs::fwrite_refcount(" + "text": "let __a0 = " }, { "placeholder": { @@ -512,7 +513,7 @@ } }, { - "text": ", " + "text": ";\n let __a1 = " }, { "placeholder": { @@ -521,7 +522,7 @@ } }, { - "text": ", " + "text": ";\n let __a2 = " }, { "placeholder": { @@ -530,7 +531,7 @@ } }, { - "text": ", " + "text": ";\n let __a3 = " }, { "method_call": { @@ -550,9 +551,10 @@ } }, { - "text": ")" + "text": ";\n libcc2rs::fwrite_refcount(__a0, __a1, __a2, __a3)" } ], + "multi_statement": true, "params": { "a0": { "type": "AnyPtr" diff --git a/rules/stdio/ir_unsafe.json b/rules/stdio/ir_unsafe.json index 1301ed25..ea38ee60 100644 --- a/rules/stdio/ir_unsafe.json +++ b/rules/stdio/ir_unsafe.json @@ -488,7 +488,7 @@ "f5": { "body": [ { - "text": "libcc2rs::fread_unsafe(" + "text": "let __a0 = " }, { "placeholder": { @@ -497,7 +497,7 @@ } }, { - "text": " as *mut ::std::ffi::c_void, " + "text": " as *mut ::std::ffi::c_void;\n let __a1 = " }, { "placeholder": { @@ -506,7 +506,7 @@ } }, { - "text": ", " + "text": ";\n let __a2 = " }, { "placeholder": { @@ -515,7 +515,7 @@ } }, { - "text": ", " + "text": ";\n let __a3 = " }, { "placeholder": { @@ -524,9 +524,10 @@ } }, { - "text": ")" + "text": ";\n libcc2rs::fread_unsafe(__a0, __a1, __a2, __a3)" } ], + "multi_statement": true, "params": { "a0": { "type": "*mut ::libc::c_void", @@ -550,7 +551,7 @@ "f6": { "body": [ { - "text": "libcc2rs::fwrite_unsafe(" + "text": "let __a0 = " }, { "placeholder": { @@ -559,7 +560,7 @@ } }, { - "text": " as *const ::std::ffi::c_void, " + "text": " as *const ::std::ffi::c_void;\n let __a1 = " }, { "placeholder": { @@ -568,7 +569,7 @@ } }, { - "text": ", " + "text": ";\n let __a2 = " }, { "placeholder": { @@ -577,7 +578,7 @@ } }, { - "text": ", " + "text": ";\n let __a3 = " }, { "placeholder": { @@ -586,9 +587,10 @@ } }, { - "text": ")" + "text": ";\n libcc2rs::fwrite_unsafe(__a0, __a1, __a2, __a3)" } ], + "multi_statement": true, "params": { "a0": { "type": "*const ::libc::c_void", diff --git a/rules/stdio/tgt_refcount.rs b/rules/stdio/tgt_refcount.rs index 1814ac55..1acc3e53 100644 --- a/rules/stdio/tgt_refcount.rs +++ b/rules/stdio/tgt_refcount.rs @@ -56,11 +56,19 @@ fn f4(a0: &mut ::std::fs::File, a1: i64, a2: i32) -> i32 { } fn f5(a0: AnyPtr, a1: u64, a2: u64, a3: Ptr<::std::fs::File>) -> u64 { - libcc2rs::fread_refcount(a0, a1, a2, a3.clone()) + let __a0 = a0; + let __a1 = a1; + let __a2 = a2; + let __a3 = a3.clone(); + libcc2rs::fread_refcount(__a0, __a1, __a2, __a3) } fn f6(a0: AnyPtr, a1: u64, a2: u64, a3: Ptr<::std::fs::File>) -> u64 { - libcc2rs::fwrite_refcount(a0, a1, a2, a3.clone()) + let __a0 = a0; + let __a1 = a1; + let __a2 = a2; + let __a3 = a3.clone(); + libcc2rs::fwrite_refcount(__a0, __a1, __a2, __a3) } fn f7(a0: Ptr<::std::fs::File>) -> i32 { diff --git a/rules/stdio/tgt_unsafe.rs b/rules/stdio/tgt_unsafe.rs index 771a8515..0b76382b 100644 --- a/rules/stdio/tgt_unsafe.rs +++ b/rules/stdio/tgt_unsafe.rs @@ -66,11 +66,19 @@ unsafe fn f4(a0: *mut ::std::fs::File, a1: i64, a2: i32) -> i32 { } unsafe fn f5(a0: *mut ::libc::c_void, a1: u64, a2: u64, a3: *mut ::std::fs::File) -> u64 { - libcc2rs::fread_unsafe(a0 as *mut ::std::ffi::c_void, a1, a2, a3) + let __a0 = a0 as *mut ::std::ffi::c_void; + let __a1 = a1; + let __a2 = a2; + let __a3 = a3; + libcc2rs::fread_unsafe(__a0, __a1, __a2, __a3) } unsafe fn f6(a0: *const ::libc::c_void, a1: u64, a2: u64, a3: *mut ::std::fs::File) -> u64 { - libcc2rs::fwrite_unsafe(a0 as *const ::std::ffi::c_void, a1, a2, a3) + let __a0 = a0 as *const ::std::ffi::c_void; + let __a1 = a1; + let __a2 = a2; + let __a3 = a3; + libcc2rs::fwrite_unsafe(__a0, __a1, __a2, __a3) } unsafe fn f7(a0: *mut ::std::fs::File) -> i32 { diff --git a/tests/unit/out/refcount/fn_ptr_stdlib_compare.rs b/tests/unit/out/refcount/fn_ptr_stdlib_compare.rs index cd56a61f..e095c177 100644 --- a/tests/unit/out/refcount/fn_ptr_stdlib_compare.rs +++ b/tests/unit/out/refcount/fn_ptr_stdlib_compare.rs @@ -102,12 +102,13 @@ fn main_0() -> i32 { ); ((buf.as_pointer() as Ptr) as Ptr).to_any().clone() }; - let n: Value = Rc::new(RefCell::new(libcc2rs::fread_refcount( - ((buf.as_pointer() as Ptr) as Ptr).to_any(), - 1_u64, - 10_u64, - (*stream.borrow()).clone(), - ))); + let n: Value = Rc::new(RefCell::new({ + let __a0 = ((buf.as_pointer() as Ptr) as Ptr).to_any(); + let __a1 = 1_u64; + let __a2 = 10_u64; + let __a3 = (*stream.borrow()).clone(); + libcc2rs::fread_refcount(__a0, __a1, __a2, __a3) + })); assert!(((*n.borrow()) == 10_u64)); let i: Value = Rc::new(RefCell::new(0)); 'loop_: while ((*i.borrow()) < 10) { @@ -267,12 +268,13 @@ fn main_0() -> i32 { ); ((buf.as_pointer() as Ptr) as Ptr).to_any().clone() }; - let n: Value = Rc::new(RefCell::new(libcc2rs::fwrite_refcount( - ((buf.as_pointer() as Ptr) as Ptr).to_any(), - 1_u64, - 10_u64, - (*stream.borrow()).clone(), - ))); + let n: Value = Rc::new(RefCell::new({ + let __a0 = ((buf.as_pointer() as Ptr) as Ptr).to_any(); + let __a1 = 1_u64; + let __a2 = 10_u64; + let __a3 = (*stream.borrow()).clone(); + libcc2rs::fwrite_refcount(__a0, __a1, __a2, __a3) + })); assert!(((*n.borrow()) == 10_u64)); { (*stream.borrow()).delete(); diff --git a/tests/unit/out/unsafe/fn_ptr_stdlib_compare.rs b/tests/unit/out/unsafe/fn_ptr_stdlib_compare.rs index aa223c99..8ae59743 100644 --- a/tests/unit/out/unsafe/fn_ptr_stdlib_compare.rs +++ b/tests/unit/out/unsafe/fn_ptr_stdlib_compare.rs @@ -95,12 +95,14 @@ unsafe fn main_0() -> i32 { } (buf.as_mut_ptr() as *mut u8 as *mut ::libc::c_void) }; - let mut n: u64 = libcc2rs::fread_unsafe( - (buf.as_mut_ptr() as *mut u8 as *mut ::libc::c_void) as *mut ::std::ffi::c_void, - 1_u64, - 10_u64, - stream, - ); + let mut n: u64 = { + let __a0 = + (buf.as_mut_ptr() as *mut u8 as *mut ::libc::c_void) as *mut ::std::ffi::c_void; + let __a1 = 1_u64; + let __a2 = 10_u64; + let __a3 = stream; + libcc2rs::fread_unsafe(__a0, __a1, __a2, __a3) + }; assert!(((n) == (10_u64))); let mut i: i32 = 0; 'loop_: while ((i) < (10)) { @@ -250,12 +252,14 @@ unsafe fn main_0() -> i32 { } (buf.as_mut_ptr() as *mut u8 as *mut ::libc::c_void) }; - let mut n: u64 = libcc2rs::fwrite_unsafe( - (buf.as_mut_ptr() as *const u8 as *const ::libc::c_void) as *const ::std::ffi::c_void, - 1_u64, - 10_u64, - stream, - ); + let mut n: u64 = { + let __a0 = (buf.as_mut_ptr() as *const u8 as *const ::libc::c_void) + as *const ::std::ffi::c_void; + let __a1 = 1_u64; + let __a2 = 10_u64; + let __a3 = stream; + libcc2rs::fwrite_unsafe(__a0, __a1, __a2, __a3) + }; assert!(((n) == (10_u64))); { Box::from_raw(stream);