diff --git a/rules/array/tgt_refcount.rs b/rules/array/tgt_refcount.rs index 7b9b72cdd0..91f5d9c942 100644 --- a/rules/array/tgt_refcount.rs +++ b/rules/array/tgt_refcount.rs @@ -21,6 +21,10 @@ fn f1(a0: Ptr) -> Ptr { a0 } +fn f2(a0: Ptr) -> usize { + T2 +} + fn f3(a0: Ptr) -> Ptr { a0 } @@ -35,6 +39,10 @@ fn f5(a0: Ptr, a1: Ptr) { } } +fn f6(a0: Ptr) -> Box<[T1]> { + (0..T2).map(|__i| a0.read_at(__i)).collect() +} + fn f7(a0: Ptr, a1: Ptr) { for __i in 0..T2 { T1::copy_assign(&a0.offset(__i), a1.offset(__i)); diff --git a/rules/deque/tgt_refcount.rs b/rules/deque/tgt_refcount.rs index ef1d15801d..560bee9a7b 100644 --- a/rules/deque/tgt_refcount.rs +++ b/rules/deque/tgt_refcount.rs @@ -5,6 +5,14 @@ use libcc2rs::*; use std::cell::RefCell; use std::rc::Rc; +fn t4() -> Ptr { + Ptr::null() +} + +fn t5() -> Ptr { + Ptr::null() +} + fn f1(a0: Ptr) -> Ptr { a0.to_last() } diff --git a/rules/stdio/tgt_refcount.rs b/rules/stdio/tgt_refcount.rs index eddb40b74b..0eccbd1f9b 100644 --- a/rules/stdio/tgt_refcount.rs +++ b/rules/stdio/tgt_refcount.rs @@ -7,6 +7,10 @@ fn t1() -> Ptr { Ptr::null() } +fn t2() -> CFile { + unimplemented!() +} + fn f1(a0: Ptr, a1: Ptr) -> Ptr { match CFile::open(&a0.to_rust_string(), &a1.to_rust_string()) { Some(__f) => Ptr::alloc(__f), diff --git a/rules/vector/tgt_refcount.rs b/rules/vector/tgt_refcount.rs index 5f48646b0d..75fcd876a9 100644 --- a/rules/vector/tgt_refcount.rs +++ b/rules/vector/tgt_refcount.rs @@ -93,6 +93,10 @@ fn f96(a0: Ptr) -> RandomAccessIterator { a0.end() } +fn f97(a0: Ptr>, a1: Ptr>) { + a0.swap_with(&a1, |__v| __v.with_mut(std::mem::take)) +} + fn f98(a0: Ptr>, a1: usize) -> Ptr { if a1 as usize >= (*a0.upgrade().deref()).len() { panic!("out of bounds access") diff --git a/rules/vector/tgt_unsafe.rs b/rules/vector/tgt_unsafe.rs index b1b8cfd8d1..0739641e97 100644 --- a/rules/vector/tgt_unsafe.rs +++ b/rules/vector/tgt_unsafe.rs @@ -175,7 +175,7 @@ unsafe fn f96(a0: Vec) -> UnsafeRandomAccessIterator { a0.end() } -unsafe fn f97(a0: &mut Vec, a1: &mut Vec) { +unsafe fn f97(a0: &mut Vec, a1: &mut Vec) { std::mem::swap(&mut *a0, &mut *a1) } diff --git a/tests/unit/inline_container_fields.cpp b/tests/unit/inline_container_fields.cpp index cb3388b55f..bb7ddd42f3 100644 --- a/tests/unit/inline_container_fields.cpp +++ b/tests/unit/inline_container_fields.cpp @@ -131,6 +131,7 @@ int main() { assert(moves == 3 && copies == 0 && cb[1].v == 5 && ca[1].v == -1); std::array cc(cb); assert(copies == 3 && cc[1].v == 5 && cb[1].v == 5); + assert(cc.size() == 3); cc[2].v = 8; cb = cc; assert(copy_assigns == 3 && move_assigns == 0 && cb[2].v == 8); diff --git a/tests/unit/out/refcount/inline_container_fields.rs b/tests/unit/out/refcount/inline_container_fields.rs index bdb9c6ed7b..3b53433550 100644 --- a/tests/unit/out/refcount/inline_container_fields.rs +++ b/tests/unit/out/refcount/inline_container_fields.rs @@ -366,13 +366,16 @@ fn main_0() -> i32 { && ({ (*ca.borrow())[1_usize].v } == -1_i32) }) ); - let cc: Value> = Rc::new(RefCell::new((*cb.borrow()).clone())); + let cc: Value> = Rc::new(RefCell::new( + (0..3).map(|__i| cb.decay().read_at(__i)).collect(), + )); assert!( ({ ((copies_2.with(|rc| *rc.borrow()) == 3) && ({ (*cc.borrow())[1_usize].v } == 5)) && ({ (*cb.borrow())[1_usize].v } == 5) }) ); + assert!((3 == 3_usize)); (*cc.borrow_mut())[2_usize].v = 8; for __i in 0..3 { &cb.decay().offset(__i).copy_assign(cc.decay().offset(__i)); diff --git a/tests/unit/out/refcount/printfs.rs b/tests/unit/out/refcount/printfs.rs index d59fdc07a3..16e7e5d88d 100644 --- a/tests/unit/out/refcount/printfs.rs +++ b/tests/unit/out/refcount/printfs.rs @@ -42,6 +42,18 @@ pub fn log_fmt_4(mut fmt: Ptr, mut v: i32) { libcc2rs::c_stdout().with_mut(|__f| __f.write(__s.as_bytes())) as i32 }; } +pub fn put_5(out: Ptr, mut s: Ptr) { + match ({ (out).clone() },) { + (mut __tmp_0,) => { + match s + .with_c_bytes(|__bytes| __tmp_0.with_mut(|__f| __f.write(__bytes)) == __bytes.len()) + { + true => 0, + false => -1, + } + } + }; +} pub fn main() { __cpp2rust_init_globals(); std::process::exit(main_0()); @@ -91,6 +103,8 @@ fn main_0() -> i32 { }); ({ log_fmt_4(Ptr::::from_string_literal(b"%d\n"), 6) }); println!("{{}} {} {}", 5_u64, (-6_i32 as i64)); + let mut fp: Ptr = libcc2rs::c_stdout(); + ({ put_5((fp).clone(), Ptr::::from_string_literal(b"value\n")) }); return 0; } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/vector.rs b/tests/unit/out/refcount/vector.rs index 3b52b3b4f1..052fe696c8 100644 --- a/tests/unit/out/refcount/vector.rs +++ b/tests/unit/out/refcount/vector.rs @@ -13,6 +13,27 @@ pub struct S { pub x: i32, } pub fn copy_0(mut copy_vector: Vec) {} +#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, MoveCtor, MoveAssign, Default)] +#[byte_size(48)] +pub struct Foo { + #[offset(0)] + #[byte_size(24)] + pub a: Vec, + #[offset(24)] + #[byte_size(24)] + pub b: Vec, +} +impl Foo { + pub fn move_from(_a0: Ptr) -> Self { + Self { + a: field!(_a0, a).with_mut(std::mem::take), + b: field!(_a0, b).with_mut(std::mem::take), + } + } +} +pub fn exchange_1(x: Ptr>, y: Ptr>) { + x.swap_with(&(y).clone(), |__v| __v.with_mut(std::mem::take)); +} pub fn main() { __cpp2rust_init_globals(); std::process::exit(main_0()); @@ -332,6 +353,69 @@ fn main_0() -> i32 { }; field!(((v9[0_usize].read()).read()), x).write(2); assert!(({ ({ (*a.borrow()).x } == 2) })); + let v10: Value> = Rc::new(RefCell::new(Vec::from([1, 2, 3]))); + let v11: Value> = Rc::new(RefCell::new(Vec::from([4]))); + v10.as_pointer() + .swap_with(&v11.as_pointer(), |__v| __v.with_mut(std::mem::take)); + assert!( + ({ + ((((*v10.borrow()).len() == 1_usize) && ({ (*v10.borrow())[0_usize] } == 4)) + && ((*v11.borrow()).len() == 3_usize)) + && ({ (*v11.borrow())[2_usize] } == 3) + }) + ); + let foo: Value = Rc::new(RefCell::new(::default())); + { + let __a1 = 5; + (*foo.borrow_mut()).a.push(__a1) + }; + { + let __a1 = 6; + (*foo.borrow_mut()).b.push(__a1) + }; + { + let __a1 = 7; + (*foo.borrow_mut()).b.push(__a1) + }; + field_ref!(foo.as_pointer(), a) + .as_pointer() + .swap_with(&field_ref!(foo.as_pointer(), b).as_pointer(), |__v| { + __v.with_mut(std::mem::take) + }); + assert!( + ({ + (((*foo.borrow()).a.len() == 2_usize) + && ((vec_elem!(foo.as_pointer(), a, 1_usize).read()) == 7)) + && ((vec_elem!(foo.as_pointer(), b, 0_usize).read()) == 5) + }) + ); + ({ + exchange_1( + field_ref!(foo.as_pointer(), a).as_pointer(), + v10.as_pointer(), + ) + }); + assert!( + ({ + (((*foo.borrow()).a.len() == 1_usize) + && ((vec_elem!(foo.as_pointer(), a, 0_usize).read()) == 4)) + && ({ (*v10.borrow())[1_usize] } == 7) + }) + ); return 0; } +pub trait FooImpl { + fn move_assign(&self, _a0: Ptr) -> Ptr; +} +impl FooImpl for Ptr { + fn move_assign(&self, _a0: Ptr) -> Ptr { + field_ref!((*self), a) + .as_pointer() + .write(field!(_a0, a).with_mut(std::mem::take)); + field_ref!((*self), b) + .as_pointer() + .write(field!(_a0, b).with_mut(std::mem::take)); + return (*self).clone(); + } +} pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/vector_deque_vector_string.rs b/tests/unit/out/refcount/vector_deque_vector_string.rs index f05f6bd4e0..4353c0ce22 100644 --- a/tests/unit/out/refcount/vector_deque_vector_string.rs +++ b/tests/unit/out/refcount/vector_deque_vector_string.rs @@ -179,6 +179,15 @@ fn main_0() -> i32 { ); assert!(({ ((*copy.decay().offset(1_usize).decay().upgrade().deref()).len() == 3_usize) })); assert!(({ ((*row.borrow()).len() == 3_usize) })); + let d: Value> = Rc::new(RefCell::new(Vec::new())); + let mut first: i32 = 1; + let mut last: i32 = 2; + (*d.borrow_mut()).push(first); + (*d.borrow_mut()).push(last); + let r: Ptr = d.decay(); + r.write(5); + let c: Ptr = d.decay().to_last(); + assert!(((d.decay().read()) == 5) && ((c.read()) == 2)); return 0; } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/unsafe/inline_container_fields.rs b/tests/unit/out/unsafe/inline_container_fields.rs index d90a7bc543..8427fa60aa 100644 --- a/tests/unit/out/unsafe/inline_container_fields.rs +++ b/tests/unit/out/unsafe/inline_container_fields.rs @@ -284,6 +284,7 @@ unsafe fn main_0() -> i32 { && ((cc[(1_usize)].v) == (5))) && ((cb[(1_usize)].v) == (5)) ); + assert!(((3) == (3_usize))); cc[(2_usize)].v = 8; for __i in 0..3 { (*&mut cb[__i]).copy_assign(&cc[__i]); diff --git a/tests/unit/out/unsafe/printfs.rs b/tests/unit/out/unsafe/printfs.rs index 2436add1f3..36b8e14895 100644 --- a/tests/unit/out/unsafe/printfs.rs +++ b/tests/unit/out/unsafe/printfs.rs @@ -42,6 +42,9 @@ pub unsafe fn log_to_3(mut out: *mut ::libc::FILE, mut fmt: *const libc::c_char, pub unsafe fn log_fmt_4(mut fmt: *const libc::c_char, mut v: i32) { (unsafe { libc::printf(fmt as *const libc::c_char, (v)) }); } +pub unsafe fn put_5(out: *mut ::libc::FILE, mut s: *const libc::c_char) { + libc::fputs(s, (out)); +} pub fn main() { unsafe { __cpp2rust_init_globals(); @@ -127,6 +130,8 @@ unsafe fn main_0() -> i32 { (-6_i32 as i64), ) }); + let mut fp: *mut ::libc::FILE = libcc2rs::stdout_unsafe(); + (unsafe { put_5(&mut (*fp), c"value\n".as_ptr()) }); return 0; } pub unsafe fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/unsafe/vector.rs b/tests/unit/out/unsafe/vector.rs index dd82b25c2e..82ae8a6aa5 100644 --- a/tests/unit/out/unsafe/vector.rs +++ b/tests/unit/out/unsafe/vector.rs @@ -12,6 +12,29 @@ pub struct S { pub x: i32, } pub unsafe fn copy_0(mut copy_vector: Vec) {} +#[repr(C)] +#[derive(Clone, VaArg, FnPtrArg, MoveCtorUnsafe, MoveAssignUnsafe, Default)] +pub struct Foo { + pub a: Vec, + pub b: Vec, +} +impl Foo { + pub unsafe fn move_from(_a0: *mut Foo) -> Self { + let mut this = Self { + a: std::mem::take(&mut (*_a0).a), + b: std::mem::take(&mut (*_a0).b), + }; + this + } + pub unsafe fn move_assign(&mut self, _a0: *mut Foo) -> *mut Foo { + self.a = std::mem::take(&mut (*_a0).a); + self.b = std::mem::take(&mut (*_a0).b); + return &mut (*(self as *mut Foo)); + } +} +pub unsafe fn exchange_1(x: *mut Vec, y: *mut Vec) { + std::mem::swap(&mut (*x), &mut (*y)); +} pub fn main() { unsafe { __cpp2rust_init_globals(); @@ -298,6 +321,35 @@ unsafe fn main_0() -> i32 { }; (*(*(*v9[(0_usize)]))).x = 2; assert!(((a.x) == (2))); + let mut v10: Vec = Vec::from([1, 2, 3]); + let mut v11: Vec = Vec::from([4]); + std::mem::swap(&mut v10, &mut v11); + assert!( + ((((v10.len()) == (1_usize)) && ((v10[(0_usize)]) == (4))) && ((v11.len()) == (3_usize))) + && ((v11[(2_usize)]) == (3)) + ); + let mut foo: Foo = ::default(); + { + let __a1 = 5; + foo.a.push(__a1) + }; + { + let __a1 = 6; + foo.b.push(__a1) + }; + { + let __a1 = 7; + foo.b.push(__a1) + }; + std::mem::swap(&mut foo.a, &mut foo.b); + assert!( + (((foo.a.len()) == (2_usize)) && ((foo.a[(1_usize)]) == (7))) + && ((foo.b[(0_usize)]) == (5)) + ); + (unsafe { exchange_1(&mut foo.a, &mut v10) }); + assert!( + (((foo.a.len()) == (1_usize)) && ((foo.a[(0_usize)]) == (4))) && ((v10[(1_usize)]) == (7)) + ); return 0; } pub unsafe fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/unsafe/vector_deque_vector_string.rs b/tests/unit/out/unsafe/vector_deque_vector_string.rs index ffa65b835f..1f15e61509 100644 --- a/tests/unit/out/unsafe/vector_deque_vector_string.rs +++ b/tests/unit/out/unsafe/vector_deque_vector_string.rs @@ -153,6 +153,15 @@ unsafe fn main_0() -> i32 { ); assert!((((*((copy[(1_usize)]).first_mut().unwrap())).len()) == (3_usize))); assert!(((row.len()) == (3_usize))); + let mut d: Vec = Vec::new(); + let mut first: i32 = 1; + let mut last: i32 = 2; + d.push(first); + d.push(last); + let r: *mut i32 = ((d).first_mut().unwrap()); + (*r) = 5; + let c: *const i32 = (d.last_mut().unwrap()); + assert!(((*((d).first_mut().unwrap())) == (5)) && ((*c) == (2))); return 0; } pub unsafe fn __cpp2rust_init_globals() {} diff --git a/tests/unit/printfs.cpp b/tests/unit/printfs.cpp index 087f9daacc..ec6c1c2b4c 100644 --- a/tests/unit/printfs.cpp +++ b/tests/unit/printfs.cpp @@ -13,6 +13,8 @@ template void log_to(FILE *out, const char *fmt, Args... args) { void log_fmt(const char *fmt, int v) { printf(fmt, v); } +static void put(FILE &out, const char *s) { fputs(s, &out); } + int main() { fprintf(stdout, "%s\n", "fprintf stdout"); fprintf(stdout, "%d %u %ld\n", 1, 2U, 3L); @@ -29,5 +31,7 @@ int main() { log_to(stderr, "%d\n", 5); log_fmt("%d\n", 6); printf("{} %ju %jd\n", (uintmax_t)5, (intmax_t)-6); + FILE *fp = stdout; + put(*fp, "value\n"); return 0; } diff --git a/tests/unit/vector.cpp b/tests/unit/vector.cpp index 82935676c3..2f907cca43 100644 --- a/tests/unit/vector.cpp +++ b/tests/unit/vector.cpp @@ -7,6 +7,13 @@ struct S { void copy(std::vector copy_vector) {} +struct Foo { + std::vector a; + std::vector b; +}; + +static void exchange(std::vector &x, std::vector &y) { x.swap(y); } + int main() { std::vector v1; assert(v1.size() == 0); @@ -157,5 +164,18 @@ int main() { v9.push_back(&ppa); (**v9[0])->x = 2; assert(a.x == 2); + + std::vector v10 = {1, 2, 3}; + std::vector v11 = {4}; + v10.swap(v11); + assert(v10.size() == 1 && v10[0] == 4 && v11.size() == 3 && v11[2] == 3); + Foo foo; + foo.a.push_back(5); + foo.b.push_back(6); + foo.b.push_back(7); + foo.a.swap(foo.b); + assert(foo.a.size() == 2 && foo.a[1] == 7 && foo.b[0] == 5); + exchange(foo.a, v10); + assert(foo.a.size() == 1 && foo.a[0] == 4 && v10[1] == 7); return 0; } diff --git a/tests/unit/vector_deque_vector_string.cpp b/tests/unit/vector_deque_vector_string.cpp index 3dca5dd8a2..32d1420551 100644 --- a/tests/unit/vector_deque_vector_string.cpp +++ b/tests/unit/vector_deque_vector_string.cpp @@ -31,5 +31,15 @@ int main() { assert(v[1].front()[0] == "bcx"); assert(copy[1].front().size() == 3); assert(row.size() == 3); + + std::deque d; + int first = 1; + int last = 2; + d.push_back(first); + d.push_back(last); + std::deque::reference r = d.front(); + r = 5; + std::deque::const_reference c = d.back(); + assert(d.front() == 5 && c == 2); return 0; }