From 495291be7b02ffd20a4dd100bbde26062b18f967 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Wed, 18 Nov 2026 17:25:21 +0000 Subject: [PATCH 1/2] Reorder elements without requiring Clone --- libcc2rs/src/rc.rs | 6 ++++-- tests/unit/sort_no_copy.cpp | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/libcc2rs/src/rc.rs b/libcc2rs/src/rc.rs index 787697b78b..f805f5f463 100644 --- a/libcc2rs/src/rc.rs +++ b/libcc2rs/src/rc.rs @@ -1328,12 +1328,14 @@ fn root_address(root: &SliceRoot) -> *const () { // Moves the element at(order[i]) to at(i), for every i, as std::sort does // once it knows the order. The elements are swapped, which moves them instead // of copying them. -pub fn reorder(mut order: Vec, at: impl Fn(isize) -> Ptr) { +pub fn reorder(mut order: Vec, at: impl Fn(isize) -> Ptr) { for i in 0..order.len() { let mut cur = i; while order[cur] as usize != i { let next = order[cur] as usize; - at(cur as isize).swap_with(&at(next as isize), Ptr::read); + at(cur as isize).swap_with(&at(next as isize), |_| { + unreachable!("the elements of a range are in the same array") + }); order[cur] = cur as isize; cur = next; } diff --git a/tests/unit/sort_no_copy.cpp b/tests/unit/sort_no_copy.cpp index 3c91f922dd..5f9710c39c 100644 --- a/tests/unit/sort_no_copy.cpp +++ b/tests/unit/sort_no_copy.cpp @@ -26,6 +26,16 @@ bool operator<(const Tracked &a, const Tracked &b) { return a.v < b.v; } bool greater(const Tracked &a, const Tracked &b) { return a.v > b.v; } +struct MoveOnly { + int v; + MoveOnly(int v) : v(v) {} + MoveOnly(MoveOnly &&) = default; + MoveOnly &operator=(MoveOnly &&) = default; + MoveOnly(const MoveOnly &) = delete; +}; + +bool operator<(const MoveOnly &a, const MoveOnly &b) { return a.v < b.v; } + struct Bag { std::vector items; }; @@ -101,5 +111,16 @@ int main() { assert(bag_map.find(bags[2])->second == 2); assert(bag_map.find(bags[1]) == bag_map.end()); assert(copies == 0); + + std::vector moves; + moves.emplace_back(3); + moves.emplace_back(1); + moves.emplace_back(2); + std::sort(moves.begin(), moves.end()); + assert(moves[0].v == 1 && moves[1].v == 2 && moves[2].v == 3); + std::stable_sort( + moves.begin(), moves.end(), + [](const MoveOnly &a, const MoveOnly &b) { return a.v > b.v; }); + assert(moves[0].v == 3 && moves[1].v == 2 && moves[2].v == 1); return copies; } From b01cb1079fc43ab51e6dfb489be885e7d5b724eb Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Wed, 18 Nov 2026 17:34:30 +0000 Subject: [PATCH 2/2] Update tests --- tests/unit/out/refcount/sort_no_copy.rs | 115 +++++++++++++++++++- tests/unit/out/unsafe/sort_no_copy.rs | 135 +++++++++++++++++++++++- 2 files changed, 241 insertions(+), 9 deletions(-) diff --git a/tests/unit/out/refcount/sort_no_copy.rs b/tests/unit/out/refcount/sort_no_copy.rs index 9c9e9f1d9d..8cfaf15979 100644 --- a/tests/unit/out/refcount/sort_no_copy.rs +++ b/tests/unit/out/refcount/sort_no_copy.rs @@ -64,6 +64,37 @@ pub fn operator_lt_1(a: Ptr, b: Ptr) -> bool { pub fn greater_2(a: Ptr, b: Ptr) -> bool { return ({ a.with(|__s| __s.v) } > { b.with(|__s| __s.v) }); } +#[derive(Record, ByteRepr, FnPtrArg, MoveCtor, CppFrom, MoveAssign, Default)] +#[cpp_from(i32, new, value)] +#[byte_size(4)] +pub struct MoveOnly { + #[offset(0)] + pub v: i32, +} +impl MoveOnly { + pub fn new(mut v: i32) -> Self { + Self { v: v } + } + pub fn move_from(_a0: Ptr) -> Self { + Self { + v: { (*_a0.upgrade().deref()).v }, + } + } +} +impl CppLess for MoveOnly { + fn less(__lhs: &Ptr, __rhs: &Ptr) -> bool { + operator_lt_3(__lhs.clone(), __rhs.clone()) + } +} +impl CppEq for MoveOnly { + fn equal(__lhs: &Ptr, __rhs: &Ptr) -> bool { + !(operator_lt_3(__lhs.clone(), __rhs.clone())) + && !(operator_lt_3(__rhs.clone(), __lhs.clone())) + } +} +pub fn operator_lt_3(a: Ptr, b: Ptr) -> bool { + return ({ a.with(|__s| __s.v) } < { b.with(|__s| __s.v) }); +} #[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, MoveCtor, MoveAssign, Default)] #[byte_size(24)] pub struct Bag { @@ -80,18 +111,18 @@ impl Bag { } impl CppLess for Bag { fn less(__lhs: &Ptr, __rhs: &Ptr) -> bool { - operator_lt_3(__lhs.clone(), __rhs.clone()) + operator_lt_4(__lhs.clone(), __rhs.clone()) } } impl CppEq for Bag { fn equal(__lhs: &Ptr, __rhs: &Ptr) -> bool { - operator_eq_4(__lhs.clone(), __rhs.clone()) + operator_eq_5(__lhs.clone(), __rhs.clone()) } } -pub fn operator_lt_3(a: Ptr, b: Ptr) -> bool { +pub fn operator_lt_4(a: Ptr, b: Ptr) -> bool { return ({ (*a.upgrade().deref()).items.len() } < { (*b.upgrade().deref()).items.len() }); } -pub fn operator_eq_4(a: Ptr, b: Ptr) -> bool { +pub fn operator_eq_5(a: Ptr, b: Ptr) -> bool { return ({ (*a.upgrade().deref()).items.len() } == { (*b.upgrade().deref()).items.len() }); } pub fn main() { @@ -390,6 +421,73 @@ fn main_0() -> i32 { .with(|__m: &Map, Value)>| __m.end()) ); assert!((copies_0.with(|rc| *rc.borrow()) == 0)); + let moves: Value> = Rc::new(RefCell::new(Vec::new())); + { + let __init = MoveOnly::new({ 3 }); + (*moves.borrow_mut()).push(__init) + }; + { + let __init = MoveOnly::new({ 1 }); + (*moves.borrow_mut()).push(__init) + }; + { + let __init = MoveOnly::new({ 2 }); + (*moves.borrow_mut()).push(__init) + }; + match ({ moves.decay().begin() },) { + (mut __tmp_11,) => { + let __n = moves.decay().end().iter_distance(&__tmp_11); + let mut __order: Vec = (0..__n).collect(); + __order.sort_by(|&__x, &__y| cpp_cmp(&__tmp_11.iter_at(__x), &__tmp_11.iter_at(__y))); + reorder(__order, |__i| __tmp_11.iter_at(__i)); + } + }; + assert!( + ({ + (({ (*moves.borrow())[0_usize].v } == 1) && ({ (*moves.borrow())[1_usize].v } == 2)) + && ({ (*moves.borrow())[2_usize].v } == 3) + }) + ); + match ({ moves.decay().begin() },) { + (mut __tmp_12,) => { + let __n = moves.decay().end().iter_distance(&__tmp_12); + let mut __order: Vec = (0..__n).collect(); + __order.sort_by(|&__x, &__y| { + let __a = __tmp_12.iter_at(__x); + let __b = __tmp_12.iter_at(__y); + if FnPtr::, Ptr) -> bool>::new( + |a: Ptr, b: Ptr| -> bool { + { + return ({ a.with(|__s| __s.v) } > { b.with(|__s| __s.v) }); + } + }, + ) + .call(__a.clone(), __b.clone()) + { + std::cmp::Ordering::Less + } else if FnPtr::, Ptr) -> bool>::new( + |a: Ptr, b: Ptr| -> bool { + { + return ({ a.with(|__s| __s.v) } > { b.with(|__s| __s.v) }); + } + }, + ) + .call(__b, __a) + { + std::cmp::Ordering::Greater + } else { + std::cmp::Ordering::Equal + } + }); + reorder(__order, |__i| __tmp_12.iter_at(__i)); + } + }; + assert!( + ({ + (({ (*moves.borrow())[0_usize].v } == 3) && ({ (*moves.borrow())[1_usize].v } == 2)) + && ({ (*moves.borrow())[2_usize].v } == 1) + }) + ); return copies_0.with(|rc| *rc.borrow()); } pub trait BagImpl { @@ -406,6 +504,15 @@ impl BagImpl for Ptr { return (*self).clone(); } } +pub trait MoveOnlyImpl { + fn move_assign(&self, _a0: Ptr) -> Ptr; +} +impl MoveOnlyImpl for Ptr { + fn move_assign(&self, _a0: Ptr) -> Ptr { + field!((*self), v).write({ { (*_a0.upgrade().deref()).v } }); + return (*self).clone(); + } +} pub trait TrackedImpl { fn copy_assign(&self, o: Ptr) -> Ptr; fn move_assign(&self, o: Ptr) -> Ptr; diff --git a/tests/unit/out/unsafe/sort_no_copy.rs b/tests/unit/out/unsafe/sort_no_copy.rs index a976e93845..2fc852d7ea 100644 --- a/tests/unit/out/unsafe/sort_no_copy.rs +++ b/tests/unit/out/unsafe/sort_no_copy.rs @@ -85,6 +85,56 @@ pub unsafe fn greater_2(a: *const Tracked, b: *const Tracked) -> bool { return (((*a).v) > ((*b).v)); } #[repr(C)] +#[derive(FnPtrArg, MoveCtorUnsafe, CppFromUnsafe, MoveAssignUnsafe, Default)] +#[cpp_from(i32, new, value)] +pub struct MoveOnly { + pub v: i32, +} +impl MoveOnly { + pub unsafe fn new(mut v: i32) -> Self { + let mut this = Self { v: v }; + this + } + pub unsafe fn move_from(_a0: *mut MoveOnly) -> Self { + let mut this = Self { v: (*_a0).v }; + this + } + pub unsafe fn move_assign(&mut self, _a0: *mut MoveOnly) -> *mut MoveOnly { + self.v = (*_a0).v; + return &mut (*(self as *mut MoveOnly)); + } +} +impl std::cmp::Ord for MoveOnly { + fn cmp(&self, other: &Self) -> std::cmp::Ordering { + unsafe { + if operator_lt_3(self as *const MoveOnly, other as *const MoveOnly) { + std::cmp::Ordering::Less + } else if operator_lt_3(other as *const MoveOnly, self as *const MoveOnly) { + std::cmp::Ordering::Greater + } else { + std::cmp::Ordering::Equal + } + } + } +} +impl std::cmp::PartialOrd for MoveOnly { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } +} +impl std::cmp::PartialEq for MoveOnly { + fn eq(&self, other: &Self) -> bool { + unsafe { + !(operator_lt_3(self as *const MoveOnly, other as *const MoveOnly)) + && !(operator_lt_3(other as *const MoveOnly, self as *const MoveOnly)) + } + } +} +impl std::cmp::Eq for MoveOnly {} +pub unsafe fn operator_lt_3(a: *const MoveOnly, b: *const MoveOnly) -> bool { + return (((*a).v) < ((*b).v)); +} +#[repr(C)] #[derive(Clone, VaArg, FnPtrArg, MoveCtorUnsafe, MoveAssignUnsafe, Default)] pub struct Bag { pub items: Vec, @@ -107,9 +157,9 @@ impl Bag { impl std::cmp::Ord for Bag { fn cmp(&self, other: &Self) -> std::cmp::Ordering { unsafe { - if operator_lt_3(self as *const Bag, other as *const Bag) { + if operator_lt_4(self as *const Bag, other as *const Bag) { std::cmp::Ordering::Less - } else if operator_lt_3(other as *const Bag, self as *const Bag) { + } else if operator_lt_4(other as *const Bag, self as *const Bag) { std::cmp::Ordering::Greater } else { std::cmp::Ordering::Equal @@ -124,14 +174,14 @@ impl std::cmp::PartialOrd for Bag { } impl std::cmp::PartialEq for Bag { fn eq(&self, other: &Self) -> bool { - unsafe { operator_eq_4(self as *const Bag, other as *const Bag) } + unsafe { operator_eq_5(self as *const Bag, other as *const Bag) } } } impl std::cmp::Eq for Bag {} -pub unsafe fn operator_lt_3(a: *const Bag, b: *const Bag) -> bool { +pub unsafe fn operator_lt_4(a: *const Bag, b: *const Bag) -> bool { return (((*a).items.len()) < ((*b).items.len())); } -pub unsafe fn operator_eq_4(a: *const Bag, b: *const Bag) -> bool { +pub unsafe fn operator_eq_5(a: *const Bag, b: *const Bag) -> bool { return (((*a).items.len()) == ((*b).items.len())); } pub fn main() { @@ -437,6 +487,81 @@ unsafe fn main_0() -> i32 { assert!((((*bag_map.find(&bags[(2) as usize]).iter_deref()).1) == (2))); assert!(bag_map.find(&bags[(1) as usize]) == bag_map.end()); assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut copies_0)) == (0))); + let mut moves: Vec = Vec::new(); + { + let __init = MoveOnly::new({ 3 }); + moves.push(__init) + }; + { + let __init = MoveOnly::new({ 1 }); + moves.push(__init) + }; + { + let __init = MoveOnly::new({ 2 }); + moves.push(__init) + }; + match ({ moves.begin() },) { + (mut __tmp_11,) => { + let __n = moves.end().iter_distance(&__tmp_11); + let mut __order: Vec = (0..__n).collect(); + __order.sort_by(|&__x, &__y| (*__tmp_11.iter_at(__x)).cmp(&*__tmp_11.iter_at(__y))); + let __values: Vec<_> = __order + .iter() + .map(|&__i| std::ptr::read(__tmp_11.iter_at(__i))) + .collect(); + for (__i, __v) in (0..__n).zip(__values) { + std::ptr::write(__tmp_11.iter_at(__i), __v); + } + } + }; + assert!( + (((moves[(0_usize)].v) == (1)) && ((moves[(1_usize)].v) == (2))) + && ((moves[(2_usize)].v) == (3)) + ); + match ({ moves.begin() },) { + (mut __tmp_12,) => { + let __n = moves.end().iter_distance(&__tmp_12); + let mut __order: Vec = (0..__n).collect(); + __order.sort_by(|&__x, &__y| { + let __a = __tmp_12.iter_at(__x) as *const _; + let __b = __tmp_12.iter_at(__y) as *const _; + if FnPtr:: bool>::new( + |a: *const MoveOnly, b: *const MoveOnly| -> bool { + unsafe { + return (((*a).v) > ((*b).v)); + } + }, + ) + .call(__a, __b) + { + std::cmp::Ordering::Less + } else if FnPtr:: bool>::new( + |a: *const MoveOnly, b: *const MoveOnly| -> bool { + unsafe { + return (((*a).v) > ((*b).v)); + } + }, + ) + .call(__b, __a) + { + std::cmp::Ordering::Greater + } else { + std::cmp::Ordering::Equal + } + }); + let __values: Vec<_> = __order + .iter() + .map(|&__i| std::ptr::read(__tmp_12.iter_at(__i))) + .collect(); + for (__i, __v) in (0..__n).zip(__values) { + std::ptr::write(__tmp_12.iter_at(__i), __v); + } + } + }; + assert!( + (((moves[(0_usize)].v) == (3)) && ((moves[(1_usize)].v) == (2))) + && ((moves[(2_usize)].v) == (1)) + ); return (*std::cell::LazyCell::force_mut(&mut *&raw mut copies_0)); } pub unsafe fn __cpp2rust_init_globals() {