From 81aff5169e08d740b07849fe5f298fe5d8346638 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Fri, 30 Oct 2026 11:30:14 +0000 Subject: [PATCH 01/36] Update tests --- tests/unit/out/refcount/clone_vs_move.rs | 12 ++++-------- tests/unit/out/refcount/map.rs | 4 ++-- tests/unit/out/refcount/overload_mangling.rs | 18 ++++++------------ tests/unit/out/refcount/pair.rs | 19 ++++++++----------- tests/unit/out/refcount/pair_get.rs | 4 ++-- tests/unit/out/refcount/size_t_ssize_t.rs | 8 ++------ .../structured_binding_element_ctor.rs | 8 ++------ .../out/refcount/structured_binding_pair.rs | 9 +++------ tests/unit/out/unsafe/clone_vs_move.rs | 9 +++------ tests/unit/out/unsafe/map.rs | 2 +- tests/unit/out/unsafe/overload_mangling.rs | 8 ++++---- tests/unit/out/unsafe/pair.rs | 8 ++++---- tests/unit/out/unsafe/pair_get.rs | 2 +- tests/unit/out/unsafe/size_t_ssize_t.rs | 2 +- .../unsafe/structured_binding_element_ctor.rs | 2 +- .../out/unsafe/structured_binding_pair.rs | 4 ++-- 16 files changed, 46 insertions(+), 73 deletions(-) diff --git a/tests/unit/out/refcount/clone_vs_move.rs b/tests/unit/out/refcount/clone_vs_move.rs index ebd577ad16..294e7c90b5 100644 --- a/tests/unit/out/refcount/clone_vs_move.rs +++ b/tests/unit/out/refcount/clone_vs_move.rs @@ -213,8 +213,8 @@ fn main_0() -> i32 { (*i.borrow_mut()).prefix_inc(); } let pair1: Value<(Value, Value)> = Rc::new(RefCell::new(( - Rc::new(RefCell::new(1.try_into().expect("failed conversion"))), - Rc::new(RefCell::new(2.try_into().expect("failed conversion"))), + Rc::new(RefCell::new(1)), + Rc::new(RefCell::new(2)), ))); let pair2: Value<(Value, Value)> = Rc::new(RefCell::new(( Rc::new(RefCell::new((*pair1.borrow()).0.borrow().clone())), @@ -228,13 +228,9 @@ fn main_0() -> i32 { assert!(((*(*pair1.borrow()).1.borrow()) == 2)); let pair3: Value<(Value>, Value)> = Rc::new(RefCell::new(( Rc::new(RefCell::new( - (0..(0_usize) as usize) - .map(|_| 0_i32) - .collect::>() - .try_into() - .expect("failed conversion"), + (0..(0_usize) as usize).map(|_| 0_i32).collect::>(), )), - Rc::new(RefCell::new(0.try_into().expect("failed conversion"))), + Rc::new(RefCell::new(0)), ))); let pair4: Value<(Value>, Value)> = Rc::new(RefCell::new(( Rc::new(RefCell::new((*pair3.borrow()).0.borrow().clone())), diff --git a/tests/unit/out/refcount/map.rs b/tests/unit/out/refcount/map.rs index 51afb82e4b..7031aac499 100644 --- a/tests/unit/out/refcount/map.rs +++ b/tests/unit/out/refcount/map.rs @@ -388,8 +388,8 @@ fn main_0() -> i32 { > = Rc::new(RefCell::new(Map::new())); assert!(((*other_map.borrow()).len() == 0_usize)); let key0: Value<(Value, Value)> = Rc::new(RefCell::new(( - Rc::new(RefCell::new(1.try_into().expect("failed conversion"))), - Rc::new(RefCell::new(1.try_into().expect("failed conversion"))), + Rc::new(RefCell::new(1)), + Rc::new(RefCell::new((1 as i64))), ))); let mut value: f64 = 2_f64; Map::subscript(other_map.as_pointer(), key0.as_pointer()).write(value); diff --git a/tests/unit/out/refcount/overload_mangling.rs b/tests/unit/out/refcount/overload_mangling.rs index 3ad28effd8..797b83c793 100644 --- a/tests/unit/out/refcount/overload_mangling.rs +++ b/tests/unit/out/refcount/overload_mangling.rs @@ -61,10 +61,7 @@ fn main_0() -> i32 { (({ SImpl::pick_8( &s.as_pointer(), - ( - Rc::new(RefCell::new(1.try_into().expect("failed conversion"))), - Rc::new(RefCell::new(2.try_into().expect("failed conversion"))), - ), + (Rc::new(RefCell::new(1)), Rc::new(RefCell::new(2))), ) }) == 101) ); @@ -72,10 +69,7 @@ fn main_0() -> i32 { (({ SImpl::pick_9( &s.as_pointer(), - ( - Rc::new(RefCell::new(1.try_into().expect("failed conversion"))), - Rc::new(RefCell::new(2_i64.try_into().expect("failed conversion"))), - ), + (Rc::new(RefCell::new(1)), Rc::new(RefCell::new(2_i64))), ) }) == 102) ); @@ -90,8 +84,8 @@ fn main_0() -> i32 { assert!( (({ let _p: Value<(Value, Value)> = Rc::new(RefCell::new(( - Rc::new(RefCell::new(1.try_into().expect("failed conversion"))), - Rc::new(RefCell::new(2_i64.try_into().expect("failed conversion"))), + Rc::new(RefCell::new(1)), + Rc::new(RefCell::new(2_i64)), ))); SImpl::combine_13( &s.as_pointer(), @@ -106,8 +100,8 @@ fn main_0() -> i32 { assert!( (({ let _p: Value<(Value, Value)> = Rc::new(RefCell::new(( - Rc::new(RefCell::new(1.try_into().expect("failed conversion"))), - Rc::new(RefCell::new(2.try_into().expect("failed conversion"))), + Rc::new(RefCell::new(1)), + Rc::new(RefCell::new(2)), ))); SImpl::combine_14( &s.as_pointer(), diff --git a/tests/unit/out/refcount/pair.rs b/tests/unit/out/refcount/pair.rs index 4fd402bece..80696f2b43 100644 --- a/tests/unit/out/refcount/pair.rs +++ b/tests/unit/out/refcount/pair.rs @@ -12,20 +12,20 @@ pub fn main() { } fn main_0() -> i32 { let p1: Value<(Value, Value)> = Rc::new(RefCell::new(( - Rc::new(RefCell::new(1.try_into().expect("failed conversion"))), - Rc::new(RefCell::new(2.try_into().expect("failed conversion"))), + Rc::new(RefCell::new((1 as f64))), + Rc::new(RefCell::new(2)), ))); assert!(((*(*p1.borrow()).0.borrow()) == 1_f64)); assert!(((*(*p1.borrow()).1.borrow()) == 2)); let p2: Value<(Value, Value)> = Rc::new(RefCell::new(( - Rc::new(RefCell::new(3.try_into().expect("failed conversion"))), - Rc::new(RefCell::new(4.try_into().expect("failed conversion"))), + Rc::new(RefCell::new((3 as f64))), + Rc::new(RefCell::new(4)), ))); assert!(((*(*p2.borrow()).0.borrow()) == 3_f64)); assert!(((*(*p2.borrow()).1.borrow()) == 4)); let p3: Value<(Value, Value)> = Rc::new(RefCell::new(( - Rc::new(RefCell::new(5.try_into().expect("failed conversion"))), - Rc::new(RefCell::new(6.try_into().expect("failed conversion"))), + Rc::new(RefCell::new(5)), + Rc::new(RefCell::new(6)), ))); assert!(((*(*p3.borrow()).0.borrow()) == 5_f64)); assert!(((*(*p3.borrow()).1.borrow()) == 6)); @@ -43,12 +43,9 @@ fn main_0() -> i32 { )); (*const_ptr_pairs.borrow_mut())[0_usize] = ( Rc::new(RefCell::new( - (arr.as_pointer() as Ptr) - .offset((1) as isize) - .try_into() - .expect("failed conversion"), + (arr.as_pointer() as Ptr).offset((1) as isize), )), - Rc::new(RefCell::new(n.try_into().expect("failed conversion"))), + Rc::new(RefCell::new(n)), ); assert!( (((*(*const_ptr_pairs.borrow())[0_usize].0.borrow()).read()) == 2) diff --git a/tests/unit/out/refcount/pair_get.rs b/tests/unit/out/refcount/pair_get.rs index dc4d45cfda..42bb3bac90 100644 --- a/tests/unit/out/refcount/pair_get.rs +++ b/tests/unit/out/refcount/pair_get.rs @@ -18,8 +18,8 @@ pub fn main() { } fn main_0() -> i32 { let p: Value<(Value, Value)> = Rc::new(RefCell::new(( - Rc::new(RefCell::new(1.5_f64.try_into().expect("failed conversion"))), - Rc::new(RefCell::new(2.try_into().expect("failed conversion"))), + Rc::new(RefCell::new(1.5_f64)), + Rc::new(RefCell::new(2)), ))); assert!((((*p.borrow()).0.as_pointer().read()) == 1.5_f64)); assert!((((*p.borrow()).1.as_pointer().read()) == 2)); diff --git a/tests/unit/out/refcount/size_t_ssize_t.rs b/tests/unit/out/refcount/size_t_ssize_t.rs index fa468641db..6d8ce914ca 100644 --- a/tests/unit/out/refcount/size_t_ssize_t.rs +++ b/tests/unit/out/refcount/size_t_ssize_t.rs @@ -194,12 +194,8 @@ fn main_0() -> i32 { assert!((mins[(1) as usize] == 1_usize)); assert!((mins[(2) as usize] == 2_usize)); let pr: Value<(Value, Value)> = Rc::new(RefCell::new(( - Rc::new(RefCell::new( - ((*sz.borrow()) as u64) - .try_into() - .expect("failed conversion"), - )), - Rc::new(RefCell::new(1.try_into().expect("failed conversion"))), + Rc::new(RefCell::new(((*sz.borrow()) as u64))), + Rc::new(RefCell::new(1)), ))); assert!(((*(*pr.borrow()).0.borrow()) == 21_u64)); let sizes: Value> = diff --git a/tests/unit/out/refcount/structured_binding_element_ctor.rs b/tests/unit/out/refcount/structured_binding_element_ctor.rs index 85fef2b42b..274f3143ce 100644 --- a/tests/unit/out/refcount/structured_binding_element_ctor.rs +++ b/tests/unit/out/refcount/structured_binding_element_ctor.rs @@ -101,12 +101,8 @@ fn main_0() -> i32 { assert!((moves_1.with(|rc| *rc.borrow()) == before_moves)); assert!(({ (*__decomp_6.borrow()).a.v } == 1)); let p: Value<(Value, Value)> = Rc::new(RefCell::new(( - Rc::new(RefCell::new( - Elem::new({ 7 }).try_into().expect("failed conversion"), - )), - Rc::new(RefCell::new( - Elem::new({ 8 }).try_into().expect("failed conversion"), - )), + Rc::new(RefCell::new(Elem::new({ 7 }))), + Rc::new(RefCell::new(Elem::new({ 8 }))), ))); before_copies = copies_0.with(|rc| *rc.borrow()); let __decomp_7: Value<(Value, Value)> = Rc::new(RefCell::new(( diff --git a/tests/unit/out/refcount/structured_binding_pair.rs b/tests/unit/out/refcount/structured_binding_pair.rs index c00bd9f9a9..ceec5d1595 100644 --- a/tests/unit/out/refcount/structured_binding_pair.rs +++ b/tests/unit/out/refcount/structured_binding_pair.rs @@ -7,10 +7,7 @@ use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; pub fn make_std_pair_0(mut x: i32) -> (Value, Value) { - return ( - Rc::new(RefCell::new(x.try_into().expect("failed conversion"))), - Rc::new(RefCell::new((x > 0).try_into().expect("failed conversion"))), - ); + return (Rc::new(RefCell::new(x)), Rc::new(RefCell::new((x > 0)))); } pub fn main() { __cpp2rust_init_globals(); @@ -24,8 +21,8 @@ fn main_0() -> i32 { assert!(((value.read()) == 5)); assert!(((positive.read()) as bool)); let p: Value<(Value, Value)> = Rc::new(RefCell::new(( - Rc::new(RefCell::new(1.try_into().expect("failed conversion"))), - Rc::new(RefCell::new(2.try_into().expect("failed conversion"))), + Rc::new(RefCell::new(1)), + Rc::new(RefCell::new(2)), ))); let __decomp_2: Value<(Value, Value)> = Rc::new(RefCell::new(( Rc::new(RefCell::new((*p.borrow()).0.borrow().clone())), diff --git a/tests/unit/out/unsafe/clone_vs_move.rs b/tests/unit/out/unsafe/clone_vs_move.rs index 0d1cc1429a..a07cc906c9 100644 --- a/tests/unit/out/unsafe/clone_vs_move.rs +++ b/tests/unit/out/unsafe/clone_vs_move.rs @@ -183,7 +183,7 @@ unsafe fn main_0() -> i32 { assert!(((*map2.subscript(&i)) == ((i) + (1)))); i.prefix_inc(); } - let mut pair1: (i32, i32) = (1.into(), 2.into()); + let mut pair1: (i32, i32) = (1, 2); let mut pair2: (i32, i32) = pair1.clone(); pair2.0 = ((pair2.0) * (10)); pair2.1 = ((pair2.1) * (10)); @@ -192,11 +192,8 @@ unsafe fn main_0() -> i32 { assert!(((pair1.0) == (1))); assert!(((pair1.1) == (2))); let mut pair3: (Vec, i32) = ( - (0..(0_usize) as usize) - .map(|_| 0_i32) - .collect::>() - .into(), - 0.into(), + (0..(0_usize) as usize).map(|_| 0_i32).collect::>(), + 0, ); let mut pair4: (Vec, i32) = pair3.clone(); { diff --git a/tests/unit/out/unsafe/map.rs b/tests/unit/out/unsafe/map.rs index ef97ba55bd..a07f253dda 100644 --- a/tests/unit/out/unsafe/map.rs +++ b/tests/unit/out/unsafe/map.rs @@ -110,7 +110,7 @@ unsafe fn main_0() -> i32 { assert!(m.find(&8_i16) == m.end()); let mut other_map: UnsafeMap<(i32, i64), ((i32, i64), f64)> = UnsafeMap::new(); assert!(((other_map.len()) == (0_usize))); - let mut key0: (i32, i64) = (1.into(), 1.into()); + let mut key0: (i32, i64) = (1, (1 as i64)); let mut value: f64 = 2_f64; (*other_map.subscript(&key0)) = value; value = (*other_map.subscript(&key0)); diff --git a/tests/unit/out/unsafe/overload_mangling.rs b/tests/unit/out/unsafe/overload_mangling.rs index 4306e708c7..5a2b0d164d 100644 --- a/tests/unit/out/unsafe/overload_mangling.rs +++ b/tests/unit/out/unsafe/overload_mangling.rs @@ -138,22 +138,22 @@ unsafe fn main_0() -> i32 { S::take_7(&s, &mut _x) }) == (107)) ); - assert!(((unsafe { S::pick_8(&s, (1.into(), 2.into()),) }) == (101))); - assert!(((unsafe { S::pick_9(&s, (1.into(), 2_i64.into()),) }) == (102))); + assert!(((unsafe { S::pick_8(&s, (1, 2),) }) == (101))); + assert!(((unsafe { S::pick_9(&s, (1, 2_i64),) }) == (102))); assert!(((unsafe { S::apply_10(&s, Some(inc_0), 1,) }) == (102))); assert!(((unsafe { S::apply_11(&s, Some(add_1), 1,) }) == (111))); assert!(((unsafe { S::apply_12(&s, Some(twice_2), 3,) }) == (106))); let c: i32 = 3; assert!( ((unsafe { - let mut _p: (i32, i64) = (1.into(), 2_i64.into()); + let mut _p: (i32, i64) = (1, 2_i64); S::combine_13(&s, &mut _p, Some(twice_2), (&c as *const i32), 4_u64) }) == (112)) ); let mut z: i32 = 1; assert!( ((unsafe { - let mut _p: (i32, i32) = (1.into(), 2.into()); + let mut _p: (i32, i32) = (1, 2); S::combine_14(&s, &mut _p, Some(add_1), (&mut z as *mut i32), 5_u64) }) == (107)) ); diff --git a/tests/unit/out/unsafe/pair.rs b/tests/unit/out/unsafe/pair.rs index dafb21c11e..ffeb449e12 100644 --- a/tests/unit/out/unsafe/pair.rs +++ b/tests/unit/out/unsafe/pair.rs @@ -13,13 +13,13 @@ pub fn main() { } } unsafe fn main_0() -> i32 { - let mut p1: (f64, i32) = (1.into(), 2.into()); + let mut p1: (f64, i32) = ((1 as f64), 2); assert!(((p1.0) == (1_f64))); assert!(((p1.1) == (2))); - let mut p2: (f64, i32) = (3.into(), 4.into()); + let mut p2: (f64, i32) = ((3 as f64), 4); assert!(((p2.0) == (3_f64))); assert!(((p2.1) == (4))); - let mut p3: (f64, i32) = (5.into(), 6.into()); + let mut p3: (f64, i32) = (5, 6); assert!(((p3.0) == (5_f64))); assert!(((p3.1) == (6))); let arr: [i32; 2] = [1, 2]; @@ -27,7 +27,7 @@ unsafe fn main_0() -> i32 { let mut const_ptr_pairs: Vec<(*const i32, u64)> = (0..(1_usize) as usize) .map(|_| <(*const i32, u64)>::default()) .collect::>(); - const_ptr_pairs[(0_usize)] = (arr.as_ptr().offset((1) as isize).into(), n.into()); + const_ptr_pairs[(0_usize)] = (arr.as_ptr().offset((1) as isize), n); assert!( ((*const_ptr_pairs[(0_usize)].0) == (2)) && ((const_ptr_pairs[(0_usize)].1) == (2_u64)) ); diff --git a/tests/unit/out/unsafe/pair_get.rs b/tests/unit/out/unsafe/pair_get.rs index 2a5bf44c77..a2c391af7a 100644 --- a/tests/unit/out/unsafe/pair_get.rs +++ b/tests/unit/out/unsafe/pair_get.rs @@ -19,7 +19,7 @@ pub fn main() { } } unsafe fn main_0() -> i32 { - let mut p: (f64, i32) = (1.5_f64.into(), 2.into()); + let mut p: (f64, i32) = (1.5_f64, 2); assert!(((p.0) == (1.5_f64))); assert!(((p.1) == (2))); assert!(((unsafe { first_of_1(&p,) }) == (1.5_f64))); diff --git a/tests/unit/out/unsafe/size_t_ssize_t.rs b/tests/unit/out/unsafe/size_t_ssize_t.rs index a70846c76f..12499e9e65 100644 --- a/tests/unit/out/unsafe/size_t_ssize_t.rs +++ b/tests/unit/out/unsafe/size_t_ssize_t.rs @@ -186,7 +186,7 @@ unsafe fn main_0() -> i32 { ]; assert!(((mins[(1) as usize]) == (1_usize))); assert!(((mins[(2) as usize]) == (2_usize))); - let mut pr: (u64, i32) = ((sz as u64).into(), 1.into()); + let mut pr: (u64, i32) = ((sz as u64), 1); assert!(((pr.0) == (21_u64))); let sizes: [usize; 4] = [1_usize, 2_usize, 3_usize, 4_usize]; let mut sizes_end: *const usize = diff --git a/tests/unit/out/unsafe/structured_binding_element_ctor.rs b/tests/unit/out/unsafe/structured_binding_element_ctor.rs index db6370f5b9..f177437750 100644 --- a/tests/unit/out/unsafe/structured_binding_element_ctor.rs +++ b/tests/unit/out/unsafe/structured_binding_element_ctor.rs @@ -94,7 +94,7 @@ unsafe fn main_0() -> i32 { assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut copies_0)) == (before_copies))); assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut moves_1)) == (before_moves))); assert!(((__decomp_6.a.v) == (1))); - let mut p: (Elem, Elem) = (Elem::new({ 7 }).into(), Elem::new({ 8 }).into()); + let mut p: (Elem, Elem) = (Elem::new({ 7 }), Elem::new({ 8 })); before_copies = (*std::cell::LazyCell::force_mut(&mut *&raw mut copies_0)); let mut __decomp_7: (Elem, Elem) = p.clone(); let x: *mut Elem = &mut __decomp_7.0; diff --git a/tests/unit/out/unsafe/structured_binding_pair.rs b/tests/unit/out/unsafe/structured_binding_pair.rs index 0bf6e072c1..0d2f41474c 100644 --- a/tests/unit/out/unsafe/structured_binding_pair.rs +++ b/tests/unit/out/unsafe/structured_binding_pair.rs @@ -7,7 +7,7 @@ use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; pub unsafe fn make_std_pair_0(mut x: i32) -> (i32, bool) { - return (x.into(), ((x) > (0)).into()); + return (x, ((x) > (0))); } pub fn main() { unsafe { @@ -21,7 +21,7 @@ unsafe fn main_0() -> i32 { let positive: *mut bool = &mut __decomp_1.1; assert!(((*value) == (5))); assert!(((*positive) as bool)); - let mut p: (i32, i32) = (1.into(), 2.into()); + let mut p: (i32, i32) = (1, 2); let mut __decomp_2: (i32, i32) = p.clone(); let a: *mut i32 = &mut __decomp_2.0; let b: *mut i32 = &mut __decomp_2.1; From 53fc2c4df924d50318e3d8fa6b2eca61f1b839d4 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Fri, 30 Oct 2026 11:30:26 +0000 Subject: [PATCH 02/36] Drop into/try_into conversions form pair rules --- rules/pair/tgt_refcount.rs | 15 +++------------ rules/pair/tgt_unsafe.rs | 6 +++--- 2 files changed, 6 insertions(+), 15 deletions(-) diff --git a/rules/pair/tgt_refcount.rs b/rules/pair/tgt_refcount.rs index 07757ab396..1eb75951c1 100644 --- a/rules/pair/tgt_refcount.rs +++ b/rules/pair/tgt_refcount.rs @@ -20,24 +20,15 @@ fn f2(a0: (Value, Value)) -> (Value, Value } fn f7(a0: T1, a1: T2) -> (Value, Value) { - ( - Rc::new(RefCell::new(a0.try_into().expect("failed conversion"))), - Rc::new(RefCell::new(a1.try_into().expect("failed conversion"))), - ) + (Rc::new(RefCell::new(a0)), Rc::new(RefCell::new(a1))) } fn f9(a0: T1, a1: T2) -> (Value, Value) { - ( - Rc::new(RefCell::new(a0.try_into().expect("failed conversion"))), - Rc::new(RefCell::new(a1.try_into().expect("failed conversion"))), - ) + (Rc::new(RefCell::new(a0)), Rc::new(RefCell::new(a1))) } fn f10(a0: T1, a1: T2) -> (Value, Value) { - ( - Rc::new(RefCell::new(a0.try_into().expect("failed conversion"))), - Rc::new(RefCell::new(a1.try_into().expect("failed conversion"))), - ) + (Rc::new(RefCell::new(a0)), Rc::new(RefCell::new(a1))) } fn f12(a0: &mut (Value, Value)) -> (Value, Value) { diff --git a/rules/pair/tgt_unsafe.rs b/rules/pair/tgt_unsafe.rs index 0604e05526..91ff832def 100644 --- a/rules/pair/tgt_unsafe.rs +++ b/rules/pair/tgt_unsafe.rs @@ -17,13 +17,13 @@ unsafe fn f2(a0: (T1, T2)) -> (T1, T2) { a0.clone() } unsafe fn f7(a0: T1, a1: T2) -> (T1, T2) { - (a0.into(), a1.into()) + (a0, a1) } unsafe fn f9(a0: T1, a1: T2) -> (T1, T2) { - (a0.into(), a1.into()) + (a0, a1) } unsafe fn f10(a0: T1, a1: T2) -> (T1, T2) { - (a0.into(), a1.into()) + (a0, a1) } unsafe fn f12(a0: &mut (T1, T2)) -> (T1, T2) { From da3ed45f18bd37cf40a951356681641ad26e8245 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Fri, 30 Oct 2026 11:30:53 +0000 Subject: [PATCH 03/36] Construct pair argument as the target type --- cpp2rust/converter/converter.cpp | 11 +++++++++++ cpp2rust/converter/rules/registry.cpp | 16 ++++++++++++++++ cpp2rust/converter/rules/registry.h | 6 ++++++ 3 files changed, 33 insertions(+) diff --git a/cpp2rust/converter/converter.cpp b/cpp2rust/converter/converter.cpp index cc4a32dff0..c2b2c8384b 100644 --- a/cpp2rust/converter/converter.cpp +++ b/cpp2rust/converter/converter.cpp @@ -4946,6 +4946,17 @@ std::string Converter::ConvertPlaceholder(clang::Expr *expr, clang::Expr *arg, ToStringBase(ctx_.getPointerType(elem))); } + auto construct_as = + RuleRegistry::GetParamGenericBinding(ctx_, expr, ph_ctx.arg_idx); + if (construct_as && !arg->getType()->isArrayType() && + !ctx_.hasSameUnqualifiedType(arg->getType().getNonReferenceType(), + *construct_as)) { + Buffer buf(*this); + PushExprKind push(*this, ExprKind::RValue); + ConvertConstructFromArgs(*construct_as, arg, arg->getExprLoc()); + return std::move(buf).str(); + } + if (ph_ctx.needs_materialization()) { auto materialized = ph_ctx.materialize_ctx->GetOrMaterialize( static_cast(ph_ctx.materialize_idx), diff --git a/cpp2rust/converter/rules/registry.cpp b/cpp2rust/converter/rules/registry.cpp index 70790d78d3..fc33342db2 100644 --- a/cpp2rust/converter/rules/registry.cpp +++ b/cpp2rust/converter/rules/registry.cpp @@ -461,6 +461,22 @@ bool ParamIsPointer(clang::ASTContext &ctx, const clang::Expr *expr, return rule->params.at(index).is_pointer(); } +std::optional GetParamGenericBinding(clang::ASTContext &ctx, + const clang::Expr *expr, + unsigned index) { + auto [rule, bindings] = Search(ctx, expr); + assert(rule && "expression must have a translation rule"); + llvm::StringRef type = rule->params.at(index).type; + unsigned n = 0; + if (!type.consume_front("T") || type.empty() || type.getAsInteger(10, n)) { + return std::nullopt; + } + auto &binding = bindings.at(n - 1); + assert(binding.getKind() == clang::TemplateArgument::Type && + "parameter typed by a non-type generic"); + return binding.getAsType(); +} + bool IsLibcPassthrough(clang::ASTContext &ctx, const clang::Expr *expr) { const auto *tgt_ir = GetExprRule(ctx, expr); if (tgt_ir == nullptr || !tgt_ir->body.empty() || !tgt_ir->is_extern) { diff --git a/cpp2rust/converter/rules/registry.h b/cpp2rust/converter/rules/registry.h index 8de311400c..e2dac1eb5d 100644 --- a/cpp2rust/converter/rules/registry.h +++ b/cpp2rust/converter/rules/registry.h @@ -9,6 +9,7 @@ #include #include +#include #include #include #include @@ -44,6 +45,11 @@ bool ReturnsPointer(clang::ASTContext &ctx, const clang::Expr *expr); bool ReturnsReference(const clang::Expr *expr); bool ParamIsPointer(clang::ASTContext &ctx, const clang::Expr *expr, unsigned index); + +std::optional GetParamGenericBinding(clang::ASTContext &ctx, + const clang::Expr *expr, + unsigned index); + bool MapsToPointer(clang::ASTContext &ctx, clang::QualType qual_type); bool MapsToRefcountPointer(clang::ASTContext &ctx, clang::QualType qual_type); From 5b16533a493265f201be7876953c24df746bc71d Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Fri, 30 Oct 2026 12:46:34 +0000 Subject: [PATCH 04/36] Add rules for pair converting constructor --- rules/pair/src.cpp | 10 ++++++++++ rules/pair/tgt_refcount.rs | 18 ++++++++++++++++++ rules/pair/tgt_unsafe.rs | 8 ++++++++ 3 files changed, 36 insertions(+) diff --git a/rules/pair/src.cpp b/rules/pair/src.cpp index f085066f66..4acf0a478a 100644 --- a/rules/pair/src.cpp +++ b/rules/pair/src.cpp @@ -78,3 +78,13 @@ template T1 &&f21(std::pair &&a0) { template T2 &&f22(std::pair &&a0) { return std::get<1>(std::move(a0)); } + +template +std::pair f23(std::pair &&a0) { + return std::pair(std::move(a0)); +} + +template +std::pair f24(const std::pair &a0) { + return std::pair(a0); +} diff --git a/rules/pair/tgt_refcount.rs b/rules/pair/tgt_refcount.rs index 1eb75951c1..f600065ede 100644 --- a/rules/pair/tgt_refcount.rs +++ b/rules/pair/tgt_refcount.rs @@ -83,3 +83,21 @@ fn f21(a0: (Value, Value)) -> Ptr { fn f22(a0: (Value, Value)) -> Ptr { a0.1.as_pointer() } + +fn f23, T2: TryFrom, T3: Clone, T4: Clone>( + a0: &mut (Value, Value), +) -> (Value, Value) { + ( + Rc::new(RefCell::new(a0.0.borrow().clone().try_into().ok().expect("failed conversion"))), + Rc::new(RefCell::new(a0.1.borrow().clone().try_into().ok().expect("failed conversion"))), + ) +} + +fn f24, T2: TryFrom, T3: Clone, T4: Clone>( + a0: (Value, Value), +) -> (Value, Value) { + ( + Rc::new(RefCell::new(a0.0.borrow().clone().try_into().ok().expect("failed conversion"))), + Rc::new(RefCell::new(a0.1.borrow().clone().try_into().ok().expect("failed conversion"))), + ) +} diff --git a/rules/pair/tgt_unsafe.rs b/rules/pair/tgt_unsafe.rs index 91ff832def..9736c3e7da 100644 --- a/rules/pair/tgt_unsafe.rs +++ b/rules/pair/tgt_unsafe.rs @@ -69,3 +69,11 @@ unsafe fn f21(a0: &mut (T1, T2)) -> *mut T1 { unsafe fn f22(a0: &mut (T1, T2)) -> *mut T2 { &mut a0.1 } + +unsafe fn f23, T2: From, T3: Clone, T4: Clone>(a0: &mut (T3, T4)) -> (T1, T2) { + (a0.0.clone().into(), a0.1.clone().into()) +} + +unsafe fn f24, T2: From, T3: Clone, T4: Clone>(a0: (T3, T4)) -> (T1, T2) { + (a0.0.into(), a0.1.into()) +} From 89f30be17e0c4bac50ffa477a525af7855bae0a3 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Fri, 30 Oct 2026 12:55:00 +0000 Subject: [PATCH 05/36] Add pair converting constructor tests --- tests/unit/pair_converting_construction.cpp | 78 +++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 tests/unit/pair_converting_construction.cpp diff --git a/tests/unit/pair_converting_construction.cpp b/tests/unit/pair_converting_construction.cpp new file mode 100644 index 0000000000..94493d2c9a --- /dev/null +++ b/tests/unit/pair_converting_construction.cpp @@ -0,0 +1,78 @@ +#include +#include + +struct Celsius { + int degrees; + Celsius(int d) : degrees(d) {} +}; + +struct Meters { + int value; + operator int() const { return value; } +}; + +struct Label { + int id; + Label(const Celsius &c) : id(c.degrees) {} +}; + +struct Owner { + int taken; + int copied; + Owner(const Celsius &c) : taken(0), copied(c.degrees) {} + Owner(Celsius &&c) : taken(c.degrees), copied(0) {} +}; + +struct Holder { + int value; + Holder(Celsius c) : value(c.degrees) {} +}; + +int main() { + std::pair widened(-1, 2u); + assert(widened.first == -1L); + assert(widened.second == 2u); + + std::pair from_ints(1, 2); + assert(from_ints.first.degrees == 1); + assert(from_ints.second.degrees == 2); + + Meters m{7}; + std::pair from_operators(m, m); + assert(from_operators.first == 7); + assert(from_operators.second == 7L); + + std::pair from_moved_pair = std::make_pair(3, 4); + assert(from_moved_pair.first == 3L); + assert(from_moved_pair.second == 4.0); + + std::pair ints(5, 6); + std::pair from_copied_pair = ints; + assert(from_copied_pair.first.degrees == 5); + assert(from_copied_pair.second == 6L); + + std::pair meters(Meters{8}, Meters{9}); + std::pair from_operator_pair = meters; + assert(from_operator_pair.first == 8); + assert(from_operator_pair.second == 9); + + std::pair celsius(10, 11); + std::pair from_record_pair = celsius; + assert(from_record_pair.first.id == 10); + assert(from_record_pair.second.copied == 11); + assert(from_record_pair.second.taken == 0); + + std::pair from_moved_record_pair = std::make_pair(Celsius(12), Celsius(13)); + assert(from_moved_record_pair.first.id == 12); + assert(from_moved_record_pair.second.taken == 13); + assert(from_moved_record_pair.second.copied == 0); + + std::pair from_by_value_pair = celsius; + assert(from_by_value_pair.first.value == 10); + assert(from_by_value_pair.second.value == 11); + + std::pair from_moved_by_value_pair = std::make_pair(Celsius(14), Celsius(15)); + assert(from_moved_by_value_pair.first.value == 14); + assert(from_moved_by_value_pair.second.value == 15); + return 0; +} From 6ed8c6b2146d76ce49f77e021c4fcbda07c617ee Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Fri, 30 Oct 2026 13:00:30 +0000 Subject: [PATCH 06/36] Constraint rules --- rules/pair/src.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/rules/pair/src.cpp b/rules/pair/src.cpp index 4acf0a478a..176892b111 100644 --- a/rules/pair/src.cpp +++ b/rules/pair/src.cpp @@ -79,12 +79,16 @@ template T2 &&f22(std::pair &&a0) { return std::get<1>(std::move(a0)); } -template +template ::value && std::is_same::value)>::type> std::pair f23(std::pair &&a0) { return std::pair(std::move(a0)); } -template +template ::value && std::is_same::value)>::type> std::pair f24(const std::pair &a0) { return std::pair(a0); } From 8af8e363350aae9689bdc63c5fdcff4cfe379816 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Fri, 30 Oct 2026 13:00:42 +0000 Subject: [PATCH 07/36] Update tests --- tests/unit/out/refcount/pair.rs | 20 ++- tests/unit/out/unsafe/pair.rs | 2 +- .../unsafe/pair_converting_construction.rs | 140 ++++++++++++++++++ 3 files changed, 159 insertions(+), 3 deletions(-) create mode 100644 tests/unit/out/unsafe/pair_converting_construction.rs diff --git a/tests/unit/out/refcount/pair.rs b/tests/unit/out/refcount/pair.rs index 80696f2b43..76aed45ee1 100644 --- a/tests/unit/out/refcount/pair.rs +++ b/tests/unit/out/refcount/pair.rs @@ -24,8 +24,24 @@ fn main_0() -> i32 { assert!(((*(*p2.borrow()).0.borrow()) == 3_f64)); assert!(((*(*p2.borrow()).1.borrow()) == 4)); let p3: Value<(Value, Value)> = Rc::new(RefCell::new(( - Rc::new(RefCell::new(5)), - Rc::new(RefCell::new(6)), + Rc::new(RefCell::new( + (Rc::new(RefCell::new(5)), Rc::new(RefCell::new(6))) + .0 + .borrow() + .clone() + .try_into() + .ok() + .expect("failed conversion"), + )), + Rc::new(RefCell::new( + (Rc::new(RefCell::new(5)), Rc::new(RefCell::new(6))) + .1 + .borrow() + .clone() + .try_into() + .ok() + .expect("failed conversion"), + )), ))); assert!(((*(*p3.borrow()).0.borrow()) == 5_f64)); assert!(((*(*p3.borrow()).1.borrow()) == 6)); diff --git a/tests/unit/out/unsafe/pair.rs b/tests/unit/out/unsafe/pair.rs index ffeb449e12..9fd46e8674 100644 --- a/tests/unit/out/unsafe/pair.rs +++ b/tests/unit/out/unsafe/pair.rs @@ -19,7 +19,7 @@ unsafe fn main_0() -> i32 { let mut p2: (f64, i32) = ((3 as f64), 4); assert!(((p2.0) == (3_f64))); assert!(((p2.1) == (4))); - let mut p3: (f64, i32) = (5, 6); + let mut p3: (f64, i32) = ((5, 6).0.clone().into(), (5, 6).1.clone().into()); assert!(((p3.0) == (5_f64))); assert!(((p3.1) == (6))); let arr: [i32; 2] = [1, 2]; diff --git a/tests/unit/out/unsafe/pair_converting_construction.rs b/tests/unit/out/unsafe/pair_converting_construction.rs new file mode 100644 index 0000000000..68ced9c378 --- /dev/null +++ b/tests/unit/out/unsafe/pair_converting_construction.rs @@ -0,0 +1,140 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +#[repr(C)] +#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +pub struct Celsius { + pub degrees: i32, +} +impl Celsius { + pub unsafe fn new(mut d: i32) -> Self { + let mut this = Self { degrees: d }; + this + } +} +#[repr(C)] +#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +pub struct Meters { + pub value: i32, +} +impl Meters { + pub unsafe fn to_i32(&self) -> i32 { + return self.value; + } +} +#[repr(C)] +#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +pub struct Label { + pub id: i32, +} +impl Label { + pub unsafe fn new(c: *const Celsius) -> Self { + let mut this = Self { id: (*c).degrees }; + this + } +} +#[repr(C)] +#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +pub struct Owner { + pub taken: i32, + pub copied: i32, +} +impl Owner { + pub unsafe fn new_1(c: *const Celsius) -> Self { + let mut this = Self { + taken: 0, + copied: (*c).degrees, + }; + this + } + pub unsafe fn new_2(c: *mut Celsius) -> Self { + let mut this = Self { + taken: (*c).degrees, + copied: 0, + }; + this + } +} +#[repr(C)] +#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +pub struct Holder { + pub value: i32, +} +impl Holder { + pub unsafe fn new(mut c: Celsius) -> Self { + let mut this = Self { value: c.degrees }; + this + } +} +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + let mut widened: (i64, u32) = ((-1_i32 as i64), 2_u32); + assert!(((widened.0) == (-1_i64))); + assert!(((widened.1) == (2_u32))); + let mut from_ints: (Celsius, Celsius) = (Celsius::new({ 1 }), Celsius::new({ 2 })); + assert!(((from_ints.0.degrees) == (1))); + assert!(((from_ints.1.degrees) == (2))); + let mut m: Meters = Meters { value: 7 }; + let mut from_operators: (i32, i64) = ( + (unsafe { Meters::to_i32(&m) }), + ((unsafe { Meters::to_i32(&m) }) as i64), + ); + assert!(((from_operators.0) == (7))); + assert!(((from_operators.1) == (7_i64))); + let mut from_moved_pair: (i64, f64) = ((3, 4).0.clone().into(), (3, 4).1.clone().into()); + assert!(((from_moved_pair.0) == (3_i64))); + assert!(((from_moved_pair.1) == (4_f64))); + let mut ints: (i32, i32) = (5, 6); + let mut from_copied_pair: (Celsius, i64) = (ints.0.into(), ints.1.into()); + assert!(((from_copied_pair.0.degrees) == (5))); + assert!(((from_copied_pair.1) == (6_i64))); + let mut meters: (Meters, Meters) = (Meters { value: 8 }, Meters { value: 9 }); + let mut from_operator_pair: (i32, i32) = (meters.0.into(), meters.1.into()); + assert!(((from_operator_pair.0) == (8))); + assert!(((from_operator_pair.1) == (9))); + let mut celsius: (Celsius, Celsius) = (Celsius::new({ 10 }), Celsius::new({ 11 })); + let mut from_record_pair: (Label, Owner) = (celsius.0.into(), celsius.1.into()); + assert!(((from_record_pair.0.id) == (10))); + assert!(((from_record_pair.1.copied) == (11))); + assert!(((from_record_pair.1.taken) == (0))); + let mut from_moved_record_pair: (Label, Owner) = ( + (Celsius::new({ 12 }), Celsius::new({ 13 })) + .0 + .clone() + .into(), + (Celsius::new({ 12 }), Celsius::new({ 13 })) + .1 + .clone() + .into(), + ); + assert!(((from_moved_record_pair.0.id) == (12))); + assert!(((from_moved_record_pair.1.taken) == (13))); + assert!(((from_moved_record_pair.1.copied) == (0))); + let mut from_by_value_pair: (Holder, Holder) = (celsius.0.into(), celsius.1.into()); + assert!(((from_by_value_pair.0.value) == (10))); + assert!(((from_by_value_pair.1.value) == (11))); + let mut from_moved_by_value_pair: (Holder, Holder) = ( + (Celsius::new({ 14 }), Celsius::new({ 15 })) + .0 + .clone() + .into(), + (Celsius::new({ 14 }), Celsius::new({ 15 })) + .1 + .clone() + .into(), + ); + assert!(((from_moved_by_value_pair.0.value) == (14))); + assert!(((from_moved_by_value_pair.1.value) == (15))); + return 0; +} +pub unsafe fn __cpp2rust_init_globals() {} From fa46b03dce7717f481c5990b68f0eb1e8507a6ed Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Fri, 30 Oct 2026 13:06:14 +0000 Subject: [PATCH 08/36] Build Init expression for parameter conversion --- cpp2rust/converter/converter.cpp | 23 ++++++++++++----------- cpp2rust/converter/converter.h | 2 ++ cpp2rust/converter/models/boxed_vars.cpp | 4 ++++ cpp2rust/converter/rules/registry.cpp | 18 +++++++++++++----- cpp2rust/converter/rules/registry.h | 7 +++---- 5 files changed, 34 insertions(+), 20 deletions(-) diff --git a/cpp2rust/converter/converter.cpp b/cpp2rust/converter/converter.cpp index c2b2c8384b..2b6ee344b8 100644 --- a/cpp2rust/converter/converter.cpp +++ b/cpp2rust/converter/converter.cpp @@ -4946,14 +4946,11 @@ std::string Converter::ConvertPlaceholder(clang::Expr *expr, clang::Expr *arg, ToStringBase(ctx_.getPointerType(elem))); } - auto construct_as = - RuleRegistry::GetParamGenericBinding(ctx_, expr, ph_ctx.arg_idx); - if (construct_as && !arg->getType()->isArrayType() && - !ctx_.hasSameUnqualifiedType(arg->getType().getNonReferenceType(), - *construct_as)) { + if (auto *init = RuleRegistry::BuildParamConversion(ctx_, expr, + ph_ctx.arg_idx, arg)) { Buffer buf(*this); PushExprKind push(*this, ExprKind::RValue); - ConvertConstructFromArgs(*construct_as, arg, arg->getExprLoc()); + ConvertInit(init->getType(), init); return std::move(buf).str(); } @@ -5191,14 +5188,18 @@ void Converter::ConvertConstructFromArgs(clang::QualType type, clang::SourceLocation loc) { auto *init = BuildInitExpr(GetSema(), type, args, loc); assert(init && "type cannot be initialized from the arguments"); - if (auto *ctor = - clang::dyn_cast(init->IgnoreImplicit())) { - ConvertConstructedValue(type, ctor); + if (args.empty() && + !clang::isa(init->IgnoreImplicit())) { + StrCat(GetDefaultAsString(type)); return; } + ConvertInit(type, init); +} - if (args.empty()) { - StrCat(GetDefaultAsString(type)); +void Converter::ConvertInit(clang::QualType type, clang::Expr *init) { + if (auto *ctor = + clang::dyn_cast(init->IgnoreImplicit())) { + ConvertConstructedValue(type, ctor); return; } Convert(init); diff --git a/cpp2rust/converter/converter.h b/cpp2rust/converter/converter.h index f6ba0973cf..4a1d794c28 100644 --- a/cpp2rust/converter/converter.h +++ b/cpp2rust/converter/converter.h @@ -866,6 +866,8 @@ class Converter : public clang::RecursiveASTVisitor { llvm::ArrayRef args, clang::SourceLocation loc); + void ConvertInit(clang::QualType type, clang::Expr *init); + virtual void ConvertConstructedValue(clang::QualType type, clang::CXXConstructExpr *ctor); diff --git a/cpp2rust/converter/models/boxed_vars.cpp b/cpp2rust/converter/models/boxed_vars.cpp index f5f663405f..62de1e3477 100644 --- a/cpp2rust/converter/models/boxed_vars.cpp +++ b/cpp2rust/converter/models/boxed_vars.cpp @@ -237,6 +237,10 @@ class AddressTakenVisitor auto all_args = BuildUnifiedArgs(expr, args, num_args); for (unsigned i = 0; i < all_args.size() && i < rule->params.size(); ++i) { auto *arg = IgnoreNoOpCasts(all_args[i]); + if (auto *init = RuleRegistry::BuildParamConversion(ctx_, expr, i, arg)) { + TraverseStmt(init); + continue; + } if (!rule->params[i].is_pointer() || arg->getType()->isPointerType() || RuleRegistry::MapsToPointer(ctx_, arg->getType())) { AddValueUse(arg); diff --git a/cpp2rust/converter/rules/registry.cpp b/cpp2rust/converter/rules/registry.cpp index fc33342db2..e0567598f5 100644 --- a/cpp2rust/converter/rules/registry.cpp +++ b/cpp2rust/converter/rules/registry.cpp @@ -461,20 +461,28 @@ bool ParamIsPointer(clang::ASTContext &ctx, const clang::Expr *expr, return rule->params.at(index).is_pointer(); } -std::optional GetParamGenericBinding(clang::ASTContext &ctx, - const clang::Expr *expr, - unsigned index) { +clang::Expr *BuildParamConversion(clang::ASTContext &ctx, + const clang::Expr *expr, unsigned index, + clang::Expr *arg) { auto [rule, bindings] = Search(ctx, expr); assert(rule && "expression must have a translation rule"); llvm::StringRef type = rule->params.at(index).type; unsigned n = 0; if (!type.consume_front("T") || type.empty() || type.getAsInteger(10, n)) { - return std::nullopt; + return nullptr; } auto &binding = bindings.at(n - 1); assert(binding.getKind() == clang::TemplateArgument::Type && "parameter typed by a non-type generic"); - return binding.getAsType(); + auto target = binding.getAsType(); + if (arg->getType()->isArrayType() || + ctx.hasSameUnqualifiedType(arg->getType().getNonReferenceType(), + target)) { + return nullptr; + } + auto *init = BuildInitExpr(*sema_, target, arg, arg->getExprLoc()); + assert(init && "argument cannot be converted to the parameter type"); + return init; } bool IsLibcPassthrough(clang::ASTContext &ctx, const clang::Expr *expr) { diff --git a/cpp2rust/converter/rules/registry.h b/cpp2rust/converter/rules/registry.h index e2dac1eb5d..3f5e83b361 100644 --- a/cpp2rust/converter/rules/registry.h +++ b/cpp2rust/converter/rules/registry.h @@ -9,7 +9,6 @@ #include #include -#include #include #include #include @@ -46,9 +45,9 @@ bool ReturnsReference(const clang::Expr *expr); bool ParamIsPointer(clang::ASTContext &ctx, const clang::Expr *expr, unsigned index); -std::optional GetParamGenericBinding(clang::ASTContext &ctx, - const clang::Expr *expr, - unsigned index); +clang::Expr *BuildParamConversion(clang::ASTContext &ctx, + const clang::Expr *expr, unsigned index, + clang::Expr *arg); bool MapsToPointer(clang::ASTContext &ctx, clang::QualType qual_type); bool MapsToRefcountPointer(clang::ASTContext &ctx, clang::QualType qual_type); From 39127ce67a2faa00fc0be9c9b14fa583f3f2a6ed Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Fri, 30 Oct 2026 13:06:28 +0000 Subject: [PATCH 09/36] Update tests --- .../refcount/pair_converting_construction.rs | 288 ++++++++++++++++++ 1 file changed, 288 insertions(+) create mode 100644 tests/unit/out/refcount/pair_converting_construction.rs diff --git a/tests/unit/out/refcount/pair_converting_construction.rs b/tests/unit/out/refcount/pair_converting_construction.rs new file mode 100644 index 0000000000..ac9c5d97ee --- /dev/null +++ b/tests/unit/out/refcount/pair_converting_construction.rs @@ -0,0 +1,288 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[byte_size(4)] +pub struct Celsius { + #[offset(0)] + pub degrees: i32, +} +impl Celsius { + pub fn new(mut d: i32) -> Self { + Self { degrees: d } + } +} +#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[byte_size(4)] +pub struct Meters { + #[offset(0)] + pub value: i32, +} +#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[byte_size(4)] +pub struct Label { + #[offset(0)] + pub id: i32, +} +impl Label { + pub fn new(c: Ptr) -> Self { + Self { + id: c.with(|__s| __s.degrees), + } + } +} +#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[byte_size(8)] +pub struct Owner { + #[offset(0)] + pub taken: i32, + #[offset(4)] + pub copied: i32, +} +impl Owner { + pub fn new_1(c: Ptr) -> Self { + Self { + taken: 0, + copied: c.with(|__s| __s.degrees), + } + } + pub fn new_2(c: Ptr) -> Self { + Self { + taken: c.with(|__s| __s.degrees), + copied: 0, + } + } +} +#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[byte_size(4)] +pub struct Holder { + #[offset(0)] + pub value: i32, +} +impl Holder { + pub fn new(mut c: Celsius) -> Self { + Self { value: c.degrees } + } +} +pub fn main() { + __cpp2rust_init_globals(); + std::process::exit(main_0()); +} +fn main_0() -> i32 { + let widened: Value<(Value, Value)> = Rc::new(RefCell::new(( + Rc::new(RefCell::new((-1_i32 as i64))), + Rc::new(RefCell::new(2_u32)), + ))); + assert!(((*(*widened.borrow()).0.borrow()) == -1_i64)); + assert!(((*(*widened.borrow()).1.borrow()) == 2_u32)); + let from_ints: Value<(Value, Value)> = Rc::new(RefCell::new(( + Rc::new(RefCell::new(Celsius::new({ 1 }))), + Rc::new(RefCell::new(Celsius::new({ 2 }))), + ))); + assert!(({ (*(*from_ints.borrow()).0.borrow()).degrees } == 1)); + assert!(({ (*(*from_ints.borrow()).1.borrow()).degrees } == 2)); + let m: Value = Rc::new(RefCell::new(Meters { value: 7 })); + let from_operators: Value<(Value, Value)> = Rc::new(RefCell::new(( + Rc::new(RefCell::new(({ MetersImpl::to_i32(&m.as_pointer()) }))), + Rc::new(RefCell::new( + (({ MetersImpl::to_i32(&m.as_pointer()) }) as i64), + )), + ))); + assert!(((*(*from_operators.borrow()).0.borrow()) == 7)); + assert!(((*(*from_operators.borrow()).1.borrow()) == 7_i64)); + let from_moved_pair: Value<(Value, Value)> = Rc::new(RefCell::new(( + Rc::new(RefCell::new( + (Rc::new(RefCell::new(3)), Rc::new(RefCell::new(4))) + .0 + .borrow() + .clone() + .try_into() + .ok() + .expect("failed conversion"), + )), + Rc::new(RefCell::new( + (Rc::new(RefCell::new(3)), Rc::new(RefCell::new(4))) + .1 + .borrow() + .clone() + .try_into() + .ok() + .expect("failed conversion"), + )), + ))); + assert!(((*(*from_moved_pair.borrow()).0.borrow()) == 3_i64)); + assert!(((*(*from_moved_pair.borrow()).1.borrow()) == 4_f64)); + let ints: Value<(Value, Value)> = Rc::new(RefCell::new(( + Rc::new(RefCell::new(5)), + Rc::new(RefCell::new(6)), + ))); + let from_copied_pair: Value<(Value, Value)> = Rc::new(RefCell::new(( + Rc::new(RefCell::new( + (*ints.borrow()) + .0 + .borrow() + .clone() + .try_into() + .ok() + .expect("failed conversion"), + )), + Rc::new(RefCell::new( + (*ints.borrow()) + .1 + .borrow() + .clone() + .try_into() + .ok() + .expect("failed conversion"), + )), + ))); + assert!(({ (*(*from_copied_pair.borrow()).0.borrow()).degrees } == 5)); + assert!(((*(*from_copied_pair.borrow()).1.borrow()) == 6_i64)); + let meters: Value<(Value, Value)> = Rc::new(RefCell::new(( + Rc::new(RefCell::new(Meters { value: 8 })), + Rc::new(RefCell::new(Meters { value: 9 })), + ))); + let from_operator_pair: Value<(Value, Value)> = Rc::new(RefCell::new(( + Rc::new(RefCell::new( + (*meters.borrow()) + .0 + .borrow() + .clone() + .try_into() + .ok() + .expect("failed conversion"), + )), + Rc::new(RefCell::new( + (*meters.borrow()) + .1 + .borrow() + .clone() + .try_into() + .ok() + .expect("failed conversion"), + )), + ))); + assert!(((*(*from_operator_pair.borrow()).0.borrow()) == 8)); + assert!(((*(*from_operator_pair.borrow()).1.borrow()) == 9)); + let celsius: Value<(Value, Value)> = Rc::new(RefCell::new(( + Rc::new(RefCell::new(Celsius::new({ 10 }))), + Rc::new(RefCell::new(Celsius::new({ 11 }))), + ))); + let from_record_pair: Value<(Value