From 204f3bbb0597e6fbf6893f8518ef52900e6e0c3d Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Mon, 18 May 2026 15:07:58 +0100 Subject: [PATCH 1/2] Rewrite compound assignment in a single expression --- .../converter/models/converter_refcount.cpp | 7 +- tests/unit/out/refcount/borrow_mut_opt.rs | 36 +----- tests/unit/out/refcount/complex_function.rs | 114 ++++++++++++------ tests/unit/out/refcount/fatorial.rs | 24 +--- .../unit/out/refcount/fn_ptr_as_condition.rs | 6 +- tests/unit/out/refcount/foreach_mut.rs | 6 +- tests/unit/out/refcount/pointer_arithmetic.rs | 30 +---- tests/unit/out/refcount/pointer_array.rs | 7 +- tests/unit/out/refcount/pointers.rs | 7 +- tests/unit/out/refcount/refs_as_args.rs | 6 +- tests/unit/out/refcount/unique_ptr.rs | 6 +- tests/unit/out/refcount/vector.rs | 6 +- 12 files changed, 100 insertions(+), 155 deletions(-) diff --git a/cpp2rust/converter/models/converter_refcount.cpp b/cpp2rust/converter/models/converter_refcount.cpp index c2bd9e18..b9d41b91 100644 --- a/cpp2rust/converter/models/converter_refcount.cpp +++ b/cpp2rust/converter/models/converter_refcount.cpp @@ -1796,13 +1796,10 @@ void ConverterRefCount::ConvertAssignment(clang::Expr *lhs, clang::Expr *rhs, auto lhs_str = ConvertLValue(lhs); if (!pending_deref_.empty()) { auto ptr = pending_deref_.take(); - // *ptr += val => { let __ptr = ptr.clone(); let __tmp = __ptr.read() + - // val; - // __ptr.write(__tmp) } auto op = assign_operator; op.remove_suffix(1); // remove '=' - StrCat("{ let __ptr = ", ptr, ".clone(); let __tmp = __ptr.read() ", op, - " ", rhs_as_string, "; __ptr.write(__tmp) }"); + StrCat(std::format("{}.write({}.read() {} {})", ptr, ptr, op, + rhs_as_string)); } else { StrCat(lhs_str, assign_operator, rhs_as_string); } diff --git a/tests/unit/out/refcount/borrow_mut_opt.rs b/tests/unit/out/refcount/borrow_mut_opt.rs index 2cbc40c2..c2767444 100644 --- a/tests/unit/out/refcount/borrow_mut_opt.rs +++ b/tests/unit/out/refcount/borrow_mut_opt.rs @@ -67,47 +67,23 @@ pub fn convert_with_rhs_1() { let __rhs = ((*p.borrow()).read()); (*arr.borrow_mut())[(0) as usize] = __rhs; let __rhs = (*x.borrow()); - { - let __ptr = z.clone(); - let __tmp = __ptr.read() + __rhs; - __ptr.write(__tmp) - }; + z.write(z.read() + __rhs); let __rhs = (*y.borrow()); - { - let __ptr = z.clone(); - let __tmp = __ptr.read() + __rhs; - __ptr.write(__tmp) - }; + z.write(z.read() + __rhs); let __rhs = ((*p.borrow()).read()); - { - let __ptr = z.clone(); - let __tmp = __ptr.read() + __rhs; - __ptr.write(__tmp) - }; + z.write(z.read() + __rhs); let __rhs = ((*y.borrow()) + (*x.borrow())); - { - let __ptr = (*p.borrow()).clone(); - let __tmp = __ptr.read() + __rhs; - __ptr.write(__tmp) - }; + (*p.borrow()).write((*p.borrow()).read() + __rhs); let __rhs = { let _lhs = (*x.borrow()); _lhs + (z.read()) }; - { - let __ptr = (*p.borrow()).clone(); - let __tmp = __ptr.read() + __rhs; - __ptr.write(__tmp) - }; + (*p.borrow()).write((*p.borrow()).read() + __rhs); let __rhs = { let _lhs = (*y.borrow()); _lhs + (z.read()) }; - { - let __ptr = (*p.borrow()).clone(); - let __tmp = __ptr.read() + __rhs; - __ptr.write(__tmp) - }; + (*p.borrow()).write((*p.borrow()).read() + __rhs); } pub fn main() { std::process::exit(main_0()); diff --git a/tests/unit/out/refcount/complex_function.rs b/tests/unit/out/refcount/complex_function.rs index a10997f4..4b595d6f 100644 --- a/tests/unit/out/refcount/complex_function.rs +++ b/tests/unit/out/refcount/complex_function.rs @@ -139,17 +139,9 @@ fn main_0() -> i32 { bar_2(_x) }); let __rhs = (*x1.borrow()); - { - let __ptr = r2.clone(); - let __tmp = __ptr.read() + __rhs; - __ptr.write(__tmp) - }; + r2.write(r2.read() + __rhs); let __rhs = (r1.read()); - { - let __ptr = r3.clone(); - let __tmp = __ptr.read() + __rhs; - __ptr.write(__tmp) - }; + r3.write(r3.read() + __rhs); let x4: Value = Rc::new(RefCell::new( ((({ let _x: i32 = (*x3.borrow()); @@ -230,15 +222,18 @@ fn main_0() -> i32 { .v .borrow()), )); - { - let __ptr = ({ + ({ + let _x: Ptr = x1.as_pointer(); + bar_2(_x) + }) + .write( + ({ let _x: Ptr = x1.as_pointer(); bar_2(_x) }) - .clone(); - let __tmp = __ptr.read() + 10; - __ptr.write(__tmp) - }; + .read() + + 10, + ); ({ let _x: Ptr = x1.as_pointer(); bar_2(_x) @@ -282,8 +277,21 @@ fn main_0() -> i32 { foo_0(_x) })) + 1); - { - let __ptr = ({ + ({ + let _x: Ptr = (*({ + (*({ (*({ (*d.borrow()).get() }).upgrade().deref()).get() }) + .upgrade() + .deref()) + .get() + }) + .upgrade() + .deref()) + .v + .as_pointer(); + bar_2(_x) + }) + .write( + ({ let _x: Ptr = (*({ (*({ (*({ (*d.borrow()).get() }).upgrade().deref()).get() }) .upgrade() @@ -296,10 +304,9 @@ fn main_0() -> i32 { .as_pointer(); bar_2(_x) }) - .clone(); - let __tmp = __ptr.read() + 10; - __ptr.write(__tmp) - }; + .read() + + 10, + ); ({ let _x: Ptr = (*({ (*({ (*({ (*d.borrow()).get() }).upgrade().deref()).get() }) @@ -349,15 +356,18 @@ fn main_0() -> i32 { ptr_1(_x) }) .with_mut(|__v| __v.prefix_inc()); - { - let __ptr = ({ + ({ + let _x: Ptr = (x1.as_pointer()); + ptr_1(_x) + }) + .write( + ({ let _x: Ptr = (x1.as_pointer()); ptr_1(_x) }) - .clone(); - let __tmp = __ptr.read() + 1; - __ptr.write(__tmp) - }; + .read() + + 1, + ); ({ let _x: Ptr = ((*({ (*({ (*({ (*d.borrow()).get() }).upgrade().deref()).get() }) @@ -372,8 +382,21 @@ fn main_0() -> i32 { ptr_1(_x) }) .with_mut(|__v| __v.prefix_inc()); - { - let __ptr = ({ + ({ + let _x: Ptr = ((*({ + (*({ (*({ (*d.borrow()).get() }).upgrade().deref()).get() }) + .upgrade() + .deref()) + .get() + }) + .upgrade() + .deref()) + .v + .as_pointer()); + ptr_1(_x) + }) + .write( + ({ let _x: Ptr = ((*({ (*({ (*({ (*d.borrow()).get() }).upgrade().deref()).get() }) .upgrade() @@ -386,12 +409,24 @@ fn main_0() -> i32 { .as_pointer()); ptr_1(_x) }) - .clone(); - let __tmp = __ptr.read() + 1; - __ptr.write(__tmp) - }; - { - let __ptr = ({ + .read() + + 1, + ); + ({ + let _x: Ptr = ((*({ + (*({ (*({ (*d.borrow()).get() }).upgrade().deref()).get() }) + .upgrade() + .deref()) + .get() + }) + .upgrade() + .deref()) + .v + .as_pointer()); + ptr_1(_x) + }) + .write( + ({ let _x: Ptr = ((*({ (*({ (*({ (*d.borrow()).get() }).upgrade().deref()).get() }) .upgrade() @@ -404,10 +439,9 @@ fn main_0() -> i32 { .as_pointer()); ptr_1(_x) }) - .clone(); - let __tmp = __ptr.read() + 1; - __ptr.write(__tmp) - }; + .read() + + 1, + ); let ptr1: Value = Rc::new(RefCell::new( ({ let _x: Ptr = ((*({ diff --git a/tests/unit/out/refcount/fatorial.rs b/tests/unit/out/refcount/fatorial.rs index 9e7f4ca3..991f3084 100644 --- a/tests/unit/out/refcount/fatorial.rs +++ b/tests/unit/out/refcount/fatorial.rs @@ -19,11 +19,7 @@ pub fn fatorial_0(n: i32) -> i32 { } pub fn fatorial_by_ref_1(n: Ptr) { if ((n.read()) == 1) { - { - let __ptr = n.clone(); - let __tmp = __ptr.read() * 1; - __ptr.write(__tmp) - }; + n.write(n.read() * 1); return; } let n_1: Value = Rc::new(RefCell::new(((n.read()) - 1))); @@ -32,20 +28,12 @@ pub fn fatorial_by_ref_1(n: Ptr) { fatorial_by_ref_1(_n) }); let __rhs = (*n_1.borrow()); - { - let __ptr = n.clone(); - let __tmp = __ptr.read() * __rhs; - __ptr.write(__tmp) - }; + n.write(n.read() * __rhs); } pub fn fatorial_by_ptr_2(n: Ptr) { let n: Value> = Rc::new(RefCell::new(n)); if (((*n.borrow()).read()) == 1) { - { - let __ptr = (*n.borrow()).clone(); - let __tmp = __ptr.read() * 1; - __ptr.write(__tmp) - }; + (*n.borrow()).write((*n.borrow()).read() * 1); return; } let n_1: Value = Rc::new(RefCell::new((((*n.borrow()).read()) - 1))); @@ -54,11 +42,7 @@ pub fn fatorial_by_ptr_2(n: Ptr) { fatorial_by_ptr_2(_n) }); let __rhs = (*n_1.borrow()); - { - let __ptr = (*n.borrow()).clone(); - let __tmp = __ptr.read() * __rhs; - __ptr.write(__tmp) - }; + (*n.borrow()).write((*n.borrow()).read() * __rhs); } pub fn main() { std::process::exit(main_0()); diff --git a/tests/unit/out/refcount/fn_ptr_as_condition.rs b/tests/unit/out/refcount/fn_ptr_as_condition.rs index d629dfba..cc6790ef 100644 --- a/tests/unit/out/refcount/fn_ptr_as_condition.rs +++ b/tests/unit/out/refcount/fn_ptr_as_condition.rs @@ -8,11 +8,7 @@ use std::os::fd::AsFd; use std::rc::{Rc, Weak}; pub fn double_it_0(x: Ptr) { let x: Value> = Rc::new(RefCell::new(x)); - { - let __ptr = (*x.borrow()).clone(); - let __tmp = __ptr.read() * 2; - __ptr.write(__tmp) - }; + (*x.borrow()).write((*x.borrow()).read() * 2); } pub fn maybe_call_1(cb: FnPtr)>, x: Ptr) { let cb: Value)>> = Rc::new(RefCell::new(cb)); diff --git a/tests/unit/out/refcount/foreach_mut.rs b/tests/unit/out/refcount/foreach_mut.rs index 8af20da7..0459ab25 100644 --- a/tests/unit/out/refcount/foreach_mut.rs +++ b/tests/unit/out/refcount/foreach_mut.rs @@ -37,11 +37,7 @@ fn main_0() -> i32 { (*v2.borrow_mut()).push(((v1.as_pointer() as Ptr).offset(2_u64 as isize))); 'loop_: for mut p in v2.as_pointer() as Ptr> { let p: Value> = Rc::new(RefCell::new(p.read().clone())); - { - let __ptr = (*p.borrow()).clone(); - let __tmp = __ptr.read() + 5; - __ptr.write(__tmp) - }; + (*p.borrow()).write((*p.borrow()).read() + 5); } 'loop_: for p in v2.as_pointer() as Ptr> { let p: Value> = Rc::new(RefCell::new(p.read().clone())); diff --git a/tests/unit/out/refcount/pointer_arithmetic.rs b/tests/unit/out/refcount/pointer_arithmetic.rs index d0bf7823..1f198bd0 100644 --- a/tests/unit/out/refcount/pointer_arithmetic.rs +++ b/tests/unit/out/refcount/pointer_arithmetic.rs @@ -12,40 +12,20 @@ pub fn main() { fn main_0() -> i32 { let x: Value = Rc::new(RefCell::new(1)); let p: Value> = Rc::new(RefCell::new((x.as_pointer()))); - { - let __ptr = (*p.borrow()).clone(); - let __tmp = __ptr.read() + 1; - __ptr.write(__tmp) - }; + (*p.borrow()).write((*p.borrow()).read() + 1); if ((*x.borrow()) == 2) { let a: Value> = Rc::new(RefCell::new(Box::new([1, 2]))); (*p.borrow_mut()) = ((a.as_pointer() as Ptr).offset(1 as isize)); - { - let __ptr = (*p.borrow()).clone(); - let __tmp = __ptr.read() + 1; - __ptr.write(__tmp) - }; + (*p.borrow()).write((*p.borrow()).read() + 1); if ((*a.borrow())[(0) as usize] == 1) && ((*a.borrow())[(1) as usize] == 3) { (*p.borrow_mut()).prefix_dec(); - { - let __ptr = (*p.borrow()).clone(); - let __tmp = __ptr.read() + 1; - __ptr.write(__tmp) - }; + (*p.borrow()).write((*p.borrow()).read() + 1); if ((*a.borrow())[(0) as usize] == 2) && ((*a.borrow())[(1) as usize] == 3) { (*p.borrow_mut()) = (x.as_pointer()); - { - let __ptr = (*p.borrow()).clone(); - let __tmp = __ptr.read() + 1; - __ptr.write(__tmp) - }; + (*p.borrow()).write((*p.borrow()).read() + 1); if ((*x.borrow()) == 3) { let p2: Value> = Rc::new(RefCell::new((*p.borrow()).clone())); - { - let __ptr = (*p2.borrow()).clone(); - let __tmp = __ptr.read() + 1; - __ptr.write(__tmp) - }; + (*p2.borrow()).write((*p2.borrow()).read() + 1); return (((*x.borrow()) == 4) as i32); } } diff --git a/tests/unit/out/refcount/pointer_array.rs b/tests/unit/out/refcount/pointer_array.rs index fac12083..d96df8fc 100644 --- a/tests/unit/out/refcount/pointer_array.rs +++ b/tests/unit/out/refcount/pointer_array.rs @@ -33,11 +33,8 @@ impl ByteRepr for StackArray {} pub fn IncrementAll_0(s: Ptr) { let i: Value = Rc::new(RefCell::new(0)); 'loop_: while ((*i.borrow()) < 3) { - { - let __ptr = (*(*s.upgrade().deref()).arr.borrow())[(*i.borrow()) as usize].clone(); - let __tmp = __ptr.read() + 1; - __ptr.write(__tmp) - }; + (*(*s.upgrade().deref()).arr.borrow())[(*i.borrow()) as usize] + .write((*(*s.upgrade().deref()).arr.borrow())[(*i.borrow()) as usize].read() + 1); (*i.borrow_mut()).prefix_inc(); } } diff --git a/tests/unit/out/refcount/pointers.rs b/tests/unit/out/refcount/pointers.rs index 8a9f472d..528ccddf 100644 --- a/tests/unit/out/refcount/pointers.rs +++ b/tests/unit/out/refcount/pointers.rs @@ -70,11 +70,8 @@ fn main_0() -> i32 { let t3: Value> = Rc::new(RefCell::new(Ptr::::null())); (*t3.borrow_mut()) = (*t2.borrow()).clone(); (*(*(*t3.borrow()).upgrade().deref()).x.borrow_mut()) = 15; - { - let __ptr = ({ (*(*t3.borrow()).upgrade().deref()).as_ptr() }).clone(); - let __tmp = __ptr.read() + 10; - __ptr.write(__tmp) - }; + ({ (*(*t3.borrow()).upgrade().deref()).as_ptr() }) + .write(({ (*(*t3.borrow()).upgrade().deref()).as_ptr() }).read() + 10); return { let _lhs = { let _lhs = (*(*(*t3.borrow()).upgrade().deref()).x.borrow()); diff --git a/tests/unit/out/refcount/refs_as_args.rs b/tests/unit/out/refcount/refs_as_args.rs index 930f4006..d03bf065 100644 --- a/tests/unit/out/refcount/refs_as_args.rs +++ b/tests/unit/out/refcount/refs_as_args.rs @@ -35,11 +35,7 @@ pub fn more_refs_0(x1: i32, x2: i32, r1: Ptr, r2: Ptr) { }; _lhs + (r.read()) }; - { - let __ptr = rx2.clone(); - let __tmp = __ptr.read() + __rhs; - __ptr.write(__tmp) - }; + rx2.write(rx2.read() + __rhs); let __rhs = (rx2.read()); r1.write(__rhs); } diff --git a/tests/unit/out/refcount/unique_ptr.rs b/tests/unit/out/refcount/unique_ptr.rs index b9228cf3..5e7de46e 100644 --- a/tests/unit/out/refcount/unique_ptr.rs +++ b/tests/unit/out/refcount/unique_ptr.rs @@ -57,11 +57,7 @@ pub fn DoStuffWithSafePointer_0(safe_ptr: Ptr>>) { (*(*x3.borrow_mut()).as_ref().unwrap().borrow_mut()) = __rhs; (*x4.borrow_mut()) = (*x3.borrow_mut()).take(); let raw_ptr2: Value> = Rc::new(RefCell::new(((*x4.borrow()).as_pointer()))); - { - let __ptr = (*raw_ptr2.borrow()).clone(); - let __tmp = __ptr.read() + 1; - __ptr.write(__tmp) - }; + (*raw_ptr2.borrow()).write((*raw_ptr2.borrow()).read() + 1); let pair: Value>> = Rc::new(RefCell::new(Some(Rc::new(RefCell::new(Pair { x: Rc::new(RefCell::new(((*raw_ptr2.borrow()).read()))), diff --git a/tests/unit/out/refcount/vector.rs b/tests/unit/out/refcount/vector.rs index c501f26f..b6979565 100644 --- a/tests/unit/out/refcount/vector.rs +++ b/tests/unit/out/refcount/vector.rs @@ -210,11 +210,7 @@ fn main_0() -> i32 { ); let ref1: Ptr = (v6.as_pointer() as Ptr).offset((*s2.borrow()).wrapping_sub(1_u64) as isize); - { - let __ptr = ref1.clone(); - let __tmp = __ptr.read() + 1.5E+0; - __ptr.write(__tmp) - }; + ref1.write(ref1.read() + 1.5E+0); assert!( (((v6.as_pointer() as Ptr) .offset((*s2.borrow()).wrapping_sub(1_u64) as isize) From a6764346d29cc9efbe20ddffa7ccf6df89c87184 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Mon, 18 May 2026 16:22:46 +0100 Subject: [PATCH 2/2] Hoist _ptr to save one clone --- .../converter/models/converter_refcount.cpp | 7 +- tests/unit/out/refcount/borrow_mut_opt.rs | 30 ++++- tests/unit/out/refcount/complex_function.rs | 107 ++++++------------ tests/unit/out/refcount/fatorial.rs | 20 +++- .../unit/out/refcount/fn_ptr_as_condition.rs | 5 +- tests/unit/out/refcount/foreach_mut.rs | 5 +- tests/unit/out/refcount/pointer_arithmetic.rs | 25 +++- tests/unit/out/refcount/pointer_array.rs | 6 +- tests/unit/out/refcount/pointers.rs | 6 +- tests/unit/out/refcount/refs_as_args.rs | 5 +- tests/unit/out/refcount/unique_ptr.rs | 5 +- tests/unit/out/refcount/vector.rs | 5 +- 12 files changed, 126 insertions(+), 100 deletions(-) diff --git a/cpp2rust/converter/models/converter_refcount.cpp b/cpp2rust/converter/models/converter_refcount.cpp index b9d41b91..88839beb 100644 --- a/cpp2rust/converter/models/converter_refcount.cpp +++ b/cpp2rust/converter/models/converter_refcount.cpp @@ -1798,8 +1798,11 @@ void ConverterRefCount::ConvertAssignment(clang::Expr *lhs, clang::Expr *rhs, auto ptr = pending_deref_.take(); auto op = assign_operator; op.remove_suffix(1); // remove '=' - StrCat(std::format("{}.write({}.read() {} {})", ptr, ptr, op, - rhs_as_string)); + { + PushBrace brace(*this); + StrCat(std::format("let _ptr = {}.clone();", ptr)); + StrCat(std::format("_ptr.write(_ptr.read() {} {})", op, rhs_as_string)); + } } else { StrCat(lhs_str, assign_operator, rhs_as_string); } diff --git a/tests/unit/out/refcount/borrow_mut_opt.rs b/tests/unit/out/refcount/borrow_mut_opt.rs index c2767444..c5af8da9 100644 --- a/tests/unit/out/refcount/borrow_mut_opt.rs +++ b/tests/unit/out/refcount/borrow_mut_opt.rs @@ -67,23 +67,41 @@ pub fn convert_with_rhs_1() { let __rhs = ((*p.borrow()).read()); (*arr.borrow_mut())[(0) as usize] = __rhs; let __rhs = (*x.borrow()); - z.write(z.read() + __rhs); + { + let _ptr = z.clone(); + _ptr.write(_ptr.read() + __rhs) + }; let __rhs = (*y.borrow()); - z.write(z.read() + __rhs); + { + let _ptr = z.clone(); + _ptr.write(_ptr.read() + __rhs) + }; let __rhs = ((*p.borrow()).read()); - z.write(z.read() + __rhs); + { + let _ptr = z.clone(); + _ptr.write(_ptr.read() + __rhs) + }; let __rhs = ((*y.borrow()) + (*x.borrow())); - (*p.borrow()).write((*p.borrow()).read() + __rhs); + { + let _ptr = (*p.borrow()).clone(); + _ptr.write(_ptr.read() + __rhs) + }; let __rhs = { let _lhs = (*x.borrow()); _lhs + (z.read()) }; - (*p.borrow()).write((*p.borrow()).read() + __rhs); + { + let _ptr = (*p.borrow()).clone(); + _ptr.write(_ptr.read() + __rhs) + }; let __rhs = { let _lhs = (*y.borrow()); _lhs + (z.read()) }; - (*p.borrow()).write((*p.borrow()).read() + __rhs); + { + let _ptr = (*p.borrow()).clone(); + _ptr.write(_ptr.read() + __rhs) + }; } pub fn main() { std::process::exit(main_0()); diff --git a/tests/unit/out/refcount/complex_function.rs b/tests/unit/out/refcount/complex_function.rs index 4b595d6f..933772f7 100644 --- a/tests/unit/out/refcount/complex_function.rs +++ b/tests/unit/out/refcount/complex_function.rs @@ -139,9 +139,15 @@ fn main_0() -> i32 { bar_2(_x) }); let __rhs = (*x1.borrow()); - r2.write(r2.read() + __rhs); + { + let _ptr = r2.clone(); + _ptr.write(_ptr.read() + __rhs) + }; let __rhs = (r1.read()); - r3.write(r3.read() + __rhs); + { + let _ptr = r3.clone(); + _ptr.write(_ptr.read() + __rhs) + }; let x4: Value = Rc::new(RefCell::new( ((({ let _x: i32 = (*x3.borrow()); @@ -222,18 +228,14 @@ fn main_0() -> i32 { .v .borrow()), )); - ({ - let _x: Ptr = x1.as_pointer(); - bar_2(_x) - }) - .write( - ({ + { + let _ptr = ({ let _x: Ptr = x1.as_pointer(); bar_2(_x) }) - .read() - + 10, - ); + .clone(); + _ptr.write(_ptr.read() + 10) + }; ({ let _x: Ptr = x1.as_pointer(); bar_2(_x) @@ -277,21 +279,8 @@ fn main_0() -> i32 { foo_0(_x) })) + 1); - ({ - let _x: Ptr = (*({ - (*({ (*({ (*d.borrow()).get() }).upgrade().deref()).get() }) - .upgrade() - .deref()) - .get() - }) - .upgrade() - .deref()) - .v - .as_pointer(); - bar_2(_x) - }) - .write( - ({ + { + let _ptr = ({ let _x: Ptr = (*({ (*({ (*({ (*d.borrow()).get() }).upgrade().deref()).get() }) .upgrade() @@ -304,9 +293,9 @@ fn main_0() -> i32 { .as_pointer(); bar_2(_x) }) - .read() - + 10, - ); + .clone(); + _ptr.write(_ptr.read() + 10) + }; ({ let _x: Ptr = (*({ (*({ (*({ (*d.borrow()).get() }).upgrade().deref()).get() }) @@ -356,18 +345,14 @@ fn main_0() -> i32 { ptr_1(_x) }) .with_mut(|__v| __v.prefix_inc()); - ({ - let _x: Ptr = (x1.as_pointer()); - ptr_1(_x) - }) - .write( - ({ + { + let _ptr = ({ let _x: Ptr = (x1.as_pointer()); ptr_1(_x) }) - .read() - + 1, - ); + .clone(); + _ptr.write(_ptr.read() + 1) + }; ({ let _x: Ptr = ((*({ (*({ (*({ (*d.borrow()).get() }).upgrade().deref()).get() }) @@ -382,21 +367,8 @@ fn main_0() -> i32 { ptr_1(_x) }) .with_mut(|__v| __v.prefix_inc()); - ({ - let _x: Ptr = ((*({ - (*({ (*({ (*d.borrow()).get() }).upgrade().deref()).get() }) - .upgrade() - .deref()) - .get() - }) - .upgrade() - .deref()) - .v - .as_pointer()); - ptr_1(_x) - }) - .write( - ({ + { + let _ptr = ({ let _x: Ptr = ((*({ (*({ (*({ (*d.borrow()).get() }).upgrade().deref()).get() }) .upgrade() @@ -409,24 +381,11 @@ fn main_0() -> i32 { .as_pointer()); ptr_1(_x) }) - .read() - + 1, - ); - ({ - let _x: Ptr = ((*({ - (*({ (*({ (*d.borrow()).get() }).upgrade().deref()).get() }) - .upgrade() - .deref()) - .get() - }) - .upgrade() - .deref()) - .v - .as_pointer()); - ptr_1(_x) - }) - .write( - ({ + .clone(); + _ptr.write(_ptr.read() + 1) + }; + { + let _ptr = ({ let _x: Ptr = ((*({ (*({ (*({ (*d.borrow()).get() }).upgrade().deref()).get() }) .upgrade() @@ -439,9 +398,9 @@ fn main_0() -> i32 { .as_pointer()); ptr_1(_x) }) - .read() - + 1, - ); + .clone(); + _ptr.write(_ptr.read() + 1) + }; let ptr1: Value = Rc::new(RefCell::new( ({ let _x: Ptr = ((*({ diff --git a/tests/unit/out/refcount/fatorial.rs b/tests/unit/out/refcount/fatorial.rs index 991f3084..e5088305 100644 --- a/tests/unit/out/refcount/fatorial.rs +++ b/tests/unit/out/refcount/fatorial.rs @@ -19,7 +19,10 @@ pub fn fatorial_0(n: i32) -> i32 { } pub fn fatorial_by_ref_1(n: Ptr) { if ((n.read()) == 1) { - n.write(n.read() * 1); + { + let _ptr = n.clone(); + _ptr.write(_ptr.read() * 1) + }; return; } let n_1: Value = Rc::new(RefCell::new(((n.read()) - 1))); @@ -28,12 +31,18 @@ pub fn fatorial_by_ref_1(n: Ptr) { fatorial_by_ref_1(_n) }); let __rhs = (*n_1.borrow()); - n.write(n.read() * __rhs); + { + let _ptr = n.clone(); + _ptr.write(_ptr.read() * __rhs) + }; } pub fn fatorial_by_ptr_2(n: Ptr) { let n: Value> = Rc::new(RefCell::new(n)); if (((*n.borrow()).read()) == 1) { - (*n.borrow()).write((*n.borrow()).read() * 1); + { + let _ptr = (*n.borrow()).clone(); + _ptr.write(_ptr.read() * 1) + }; return; } let n_1: Value = Rc::new(RefCell::new((((*n.borrow()).read()) - 1))); @@ -42,7 +51,10 @@ pub fn fatorial_by_ptr_2(n: Ptr) { fatorial_by_ptr_2(_n) }); let __rhs = (*n_1.borrow()); - (*n.borrow()).write((*n.borrow()).read() * __rhs); + { + let _ptr = (*n.borrow()).clone(); + _ptr.write(_ptr.read() * __rhs) + }; } pub fn main() { std::process::exit(main_0()); diff --git a/tests/unit/out/refcount/fn_ptr_as_condition.rs b/tests/unit/out/refcount/fn_ptr_as_condition.rs index cc6790ef..eb161583 100644 --- a/tests/unit/out/refcount/fn_ptr_as_condition.rs +++ b/tests/unit/out/refcount/fn_ptr_as_condition.rs @@ -8,7 +8,10 @@ use std::os::fd::AsFd; use std::rc::{Rc, Weak}; pub fn double_it_0(x: Ptr) { let x: Value> = Rc::new(RefCell::new(x)); - (*x.borrow()).write((*x.borrow()).read() * 2); + { + let _ptr = (*x.borrow()).clone(); + _ptr.write(_ptr.read() * 2) + }; } pub fn maybe_call_1(cb: FnPtr)>, x: Ptr) { let cb: Value)>> = Rc::new(RefCell::new(cb)); diff --git a/tests/unit/out/refcount/foreach_mut.rs b/tests/unit/out/refcount/foreach_mut.rs index 0459ab25..c969ecbe 100644 --- a/tests/unit/out/refcount/foreach_mut.rs +++ b/tests/unit/out/refcount/foreach_mut.rs @@ -37,7 +37,10 @@ fn main_0() -> i32 { (*v2.borrow_mut()).push(((v1.as_pointer() as Ptr).offset(2_u64 as isize))); 'loop_: for mut p in v2.as_pointer() as Ptr> { let p: Value> = Rc::new(RefCell::new(p.read().clone())); - (*p.borrow()).write((*p.borrow()).read() + 5); + { + let _ptr = (*p.borrow()).clone(); + _ptr.write(_ptr.read() + 5) + }; } 'loop_: for p in v2.as_pointer() as Ptr> { let p: Value> = Rc::new(RefCell::new(p.read().clone())); diff --git a/tests/unit/out/refcount/pointer_arithmetic.rs b/tests/unit/out/refcount/pointer_arithmetic.rs index 1f198bd0..08dbd9f5 100644 --- a/tests/unit/out/refcount/pointer_arithmetic.rs +++ b/tests/unit/out/refcount/pointer_arithmetic.rs @@ -12,20 +12,35 @@ pub fn main() { fn main_0() -> i32 { let x: Value = Rc::new(RefCell::new(1)); let p: Value> = Rc::new(RefCell::new((x.as_pointer()))); - (*p.borrow()).write((*p.borrow()).read() + 1); + { + let _ptr = (*p.borrow()).clone(); + _ptr.write(_ptr.read() + 1) + }; if ((*x.borrow()) == 2) { let a: Value> = Rc::new(RefCell::new(Box::new([1, 2]))); (*p.borrow_mut()) = ((a.as_pointer() as Ptr).offset(1 as isize)); - (*p.borrow()).write((*p.borrow()).read() + 1); + { + let _ptr = (*p.borrow()).clone(); + _ptr.write(_ptr.read() + 1) + }; if ((*a.borrow())[(0) as usize] == 1) && ((*a.borrow())[(1) as usize] == 3) { (*p.borrow_mut()).prefix_dec(); - (*p.borrow()).write((*p.borrow()).read() + 1); + { + let _ptr = (*p.borrow()).clone(); + _ptr.write(_ptr.read() + 1) + }; if ((*a.borrow())[(0) as usize] == 2) && ((*a.borrow())[(1) as usize] == 3) { (*p.borrow_mut()) = (x.as_pointer()); - (*p.borrow()).write((*p.borrow()).read() + 1); + { + let _ptr = (*p.borrow()).clone(); + _ptr.write(_ptr.read() + 1) + }; if ((*x.borrow()) == 3) { let p2: Value> = Rc::new(RefCell::new((*p.borrow()).clone())); - (*p2.borrow()).write((*p2.borrow()).read() + 1); + { + let _ptr = (*p2.borrow()).clone(); + _ptr.write(_ptr.read() + 1) + }; return (((*x.borrow()) == 4) as i32); } } diff --git a/tests/unit/out/refcount/pointer_array.rs b/tests/unit/out/refcount/pointer_array.rs index d96df8fc..e420db79 100644 --- a/tests/unit/out/refcount/pointer_array.rs +++ b/tests/unit/out/refcount/pointer_array.rs @@ -33,8 +33,10 @@ impl ByteRepr for StackArray {} pub fn IncrementAll_0(s: Ptr) { let i: Value = Rc::new(RefCell::new(0)); 'loop_: while ((*i.borrow()) < 3) { - (*(*s.upgrade().deref()).arr.borrow())[(*i.borrow()) as usize] - .write((*(*s.upgrade().deref()).arr.borrow())[(*i.borrow()) as usize].read() + 1); + { + let _ptr = (*(*s.upgrade().deref()).arr.borrow())[(*i.borrow()) as usize].clone(); + _ptr.write(_ptr.read() + 1) + }; (*i.borrow_mut()).prefix_inc(); } } diff --git a/tests/unit/out/refcount/pointers.rs b/tests/unit/out/refcount/pointers.rs index 528ccddf..36b6a57b 100644 --- a/tests/unit/out/refcount/pointers.rs +++ b/tests/unit/out/refcount/pointers.rs @@ -70,8 +70,10 @@ fn main_0() -> i32 { let t3: Value> = Rc::new(RefCell::new(Ptr::::null())); (*t3.borrow_mut()) = (*t2.borrow()).clone(); (*(*(*t3.borrow()).upgrade().deref()).x.borrow_mut()) = 15; - ({ (*(*t3.borrow()).upgrade().deref()).as_ptr() }) - .write(({ (*(*t3.borrow()).upgrade().deref()).as_ptr() }).read() + 10); + { + let _ptr = ({ (*(*t3.borrow()).upgrade().deref()).as_ptr() }).clone(); + _ptr.write(_ptr.read() + 10) + }; return { let _lhs = { let _lhs = (*(*(*t3.borrow()).upgrade().deref()).x.borrow()); diff --git a/tests/unit/out/refcount/refs_as_args.rs b/tests/unit/out/refcount/refs_as_args.rs index d03bf065..0d85da47 100644 --- a/tests/unit/out/refcount/refs_as_args.rs +++ b/tests/unit/out/refcount/refs_as_args.rs @@ -35,7 +35,10 @@ pub fn more_refs_0(x1: i32, x2: i32, r1: Ptr, r2: Ptr) { }; _lhs + (r.read()) }; - rx2.write(rx2.read() + __rhs); + { + let _ptr = rx2.clone(); + _ptr.write(_ptr.read() + __rhs) + }; let __rhs = (rx2.read()); r1.write(__rhs); } diff --git a/tests/unit/out/refcount/unique_ptr.rs b/tests/unit/out/refcount/unique_ptr.rs index 5e7de46e..2df1e3a4 100644 --- a/tests/unit/out/refcount/unique_ptr.rs +++ b/tests/unit/out/refcount/unique_ptr.rs @@ -57,7 +57,10 @@ pub fn DoStuffWithSafePointer_0(safe_ptr: Ptr>>) { (*(*x3.borrow_mut()).as_ref().unwrap().borrow_mut()) = __rhs; (*x4.borrow_mut()) = (*x3.borrow_mut()).take(); let raw_ptr2: Value> = Rc::new(RefCell::new(((*x4.borrow()).as_pointer()))); - (*raw_ptr2.borrow()).write((*raw_ptr2.borrow()).read() + 1); + { + let _ptr = (*raw_ptr2.borrow()).clone(); + _ptr.write(_ptr.read() + 1) + }; let pair: Value>> = Rc::new(RefCell::new(Some(Rc::new(RefCell::new(Pair { x: Rc::new(RefCell::new(((*raw_ptr2.borrow()).read()))), diff --git a/tests/unit/out/refcount/vector.rs b/tests/unit/out/refcount/vector.rs index b6979565..3884389e 100644 --- a/tests/unit/out/refcount/vector.rs +++ b/tests/unit/out/refcount/vector.rs @@ -210,7 +210,10 @@ fn main_0() -> i32 { ); let ref1: Ptr = (v6.as_pointer() as Ptr).offset((*s2.borrow()).wrapping_sub(1_u64) as isize); - ref1.write(ref1.read() + 1.5E+0); + { + let _ptr = ref1.clone(); + _ptr.write(_ptr.read() + 1.5E+0) + }; assert!( (((v6.as_pointer() as Ptr) .offset((*s2.borrow()).wrapping_sub(1_u64) as isize)