Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions rules/array/tgt_refcount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ fn f1<T1>(a0: Ptr<T1>) -> Ptr<T1> {
a0
}

fn f2<T1, const T2: usize>(a0: Ptr<T1>) -> usize {
T2
}

fn f3<T1>(a0: Ptr<T1>) -> Ptr<T1> {
a0
}
Expand All @@ -35,6 +39,10 @@ fn f5<T1: MoveAssign, const T2: usize>(a0: Ptr<T1>, a1: Ptr<T1>) {
}
}

fn f6<T1: Clone + ByteRepr, const T2: usize>(a0: Ptr<T1>) -> Box<[T1]> {
(0..T2).map(|__i| a0.read_at(__i)).collect()
}

fn f7<T1: CopyAssign, const T2: usize>(a0: Ptr<T1>, a1: Ptr<T1>) {
for __i in 0..T2 {
T1::copy_assign(&a0.offset(__i), a1.offset(__i));
Expand Down
8 changes: 8 additions & 0 deletions rules/deque/tgt_refcount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ use libcc2rs::*;
use std::cell::RefCell;
use std::rc::Rc;

fn t4<T1>() -> Ptr<T1> {
Ptr::null()
}

fn t5<T1>() -> Ptr<T1> {
Ptr::null()
}

fn f1<T1>(a0: Ptr<T1>) -> Ptr<T1> {
a0.to_last()
}
Expand Down
4 changes: 4 additions & 0 deletions rules/stdio/tgt_refcount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ fn t1() -> Ptr<CFile> {
Ptr::null()
}

fn t2() -> CFile {
unimplemented!()
}

fn f1(a0: Ptr<i8>, a1: Ptr<i8>) -> Ptr<CFile> {
match CFile::open(&a0.to_rust_string(), &a1.to_rust_string()) {
Some(__f) => Ptr::alloc(__f),
Expand Down
4 changes: 4 additions & 0 deletions rules/vector/tgt_refcount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ fn f96<T1>(a0: Ptr<T1>) -> RandomAccessIterator<T1> {
a0.end()
}

fn f97<T1: ByteRepr>(a0: Ptr<Vec<T1>>, a1: Ptr<Vec<T1>>) {
a0.swap_with(&a1, |__v| __v.with_mut(std::mem::take))
}

fn f98<T1: ByteRepr + 'static>(a0: Ptr<Vec<T1>>, a1: usize) -> Ptr<T1> {
if a1 as usize >= (*a0.upgrade().deref()).len() {
panic!("out of bounds access")
Expand Down
2 changes: 1 addition & 1 deletion rules/vector/tgt_unsafe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ unsafe fn f96<T1>(a0: Vec<T1>) -> UnsafeRandomAccessIterator<T1> {
a0.end()
}

unsafe fn f97(a0: &mut Vec<bool>, a1: &mut Vec<bool>) {
unsafe fn f97<T1>(a0: &mut Vec<T1>, a1: &mut Vec<T1>) {
std::mem::swap(&mut *a0, &mut *a1)
}

Expand Down
1 change: 1 addition & 0 deletions tests/unit/inline_container_fields.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ int main() {
assert(moves == 3 && copies == 0 && cb[1].v == 5 && ca[1].v == -1);
std::array<Counted, 3> 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);
Expand Down
5 changes: 4 additions & 1 deletion tests/unit/out/refcount/inline_container_fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,13 +366,16 @@ fn main_0() -> i32 {
&& ({ (*ca.borrow())[1_usize].v } == -1_i32)
})
);
let cc: Value<Box<[Counted]>> = Rc::new(RefCell::new((*cb.borrow()).clone()));
let cc: Value<Box<[Counted]>> = 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));
Expand Down
14 changes: 14 additions & 0 deletions tests/unit/out/refcount/printfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ pub fn log_fmt_4(mut fmt: Ptr<i8>, mut v: i32) {
libcc2rs::c_stdout().with_mut(|__f| __f.write(__s.as_bytes())) as i32
};
}
pub fn put_5(out: Ptr<CFile>, mut s: Ptr<i8>) {
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());
Expand Down Expand Up @@ -91,6 +103,8 @@ fn main_0() -> i32 {
});
({ log_fmt_4(Ptr::<i8>::from_string_literal(b"%d\n"), 6) });
println!("{{}} {} {}", 5_u64, (-6_i32 as i64));
let mut fp: Ptr<CFile> = libcc2rs::c_stdout();
({ put_5((fp).clone(), Ptr::<i8>::from_string_literal(b"value\n")) });
return 0;
}
pub fn __cpp2rust_init_globals() {}
84 changes: 84 additions & 0 deletions tests/unit/out/refcount/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,27 @@ pub struct S {
pub x: i32,
}
pub fn copy_0(mut copy_vector: Vec<i32>) {}
#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, MoveCtor, MoveAssign, Default)]
#[byte_size(48)]
pub struct Foo {
#[offset(0)]
#[byte_size(24)]
pub a: Vec<i32>,
#[offset(24)]
#[byte_size(24)]
pub b: Vec<i32>,
}
impl Foo {
pub fn move_from(_a0: Ptr<Foo>) -> 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<Vec<i32>>, y: Ptr<Vec<i32>>) {
x.swap_with(&(y).clone(), |__v| __v.with_mut(std::mem::take));
}
pub fn main() {
__cpp2rust_init_globals();
std::process::exit(main_0());
Expand Down Expand Up @@ -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<Vec<i32>> = Rc::new(RefCell::new(Vec::from([1, 2, 3])));
let v11: Value<Vec<i32>> = 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<Foo> = Rc::new(RefCell::new(<Foo>::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<Foo>) -> Ptr<Foo>;
}
impl FooImpl for Ptr<Foo> {
fn move_assign(&self, _a0: Ptr<Foo>) -> Ptr<Foo> {
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() {}
9 changes: 9 additions & 0 deletions tests/unit/out/refcount/vector_deque_vector_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<i32>> = 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<i32> = d.decay();
r.write(5);
let c: Ptr<i32> = d.decay().to_last();
assert!(((d.decay().read()) == 5) && ((c.read()) == 2));
return 0;
}
pub fn __cpp2rust_init_globals() {}
1 change: 1 addition & 0 deletions tests/unit/out/unsafe/inline_container_fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
5 changes: 5 additions & 0 deletions tests/unit/out/unsafe/printfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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() {}
52 changes: 52 additions & 0 deletions tests/unit/out/unsafe/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,29 @@ pub struct S {
pub x: i32,
}
pub unsafe fn copy_0(mut copy_vector: Vec<i32>) {}
#[repr(C)]
#[derive(Clone, VaArg, FnPtrArg, MoveCtorUnsafe, MoveAssignUnsafe, Default)]
pub struct Foo {
pub a: Vec<i32>,
pub b: Vec<i32>,
}
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<i32>, y: *mut Vec<i32>) {
std::mem::swap(&mut (*x), &mut (*y));
}
pub fn main() {
unsafe {
__cpp2rust_init_globals();
Expand Down Expand Up @@ -298,6 +321,35 @@ unsafe fn main_0() -> i32 {
};
(*(*(*v9[(0_usize)]))).x = 2;
assert!(((a.x) == (2)));
let mut v10: Vec<i32> = Vec::from([1, 2, 3]);
let mut v11: Vec<i32> = 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 = <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() {}
9 changes: 9 additions & 0 deletions tests/unit/out/unsafe/vector_deque_vector_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<i32> = 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() {}
4 changes: 4 additions & 0 deletions tests/unit/printfs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ template <class... Args> 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);
Expand All @@ -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;
}
20 changes: 20 additions & 0 deletions tests/unit/vector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ struct S {

void copy(std::vector<int> copy_vector) {}

struct Foo {
std::vector<int> a;
std::vector<int> b;
};

static void exchange(std::vector<int> &x, std::vector<int> &y) { x.swap(y); }

int main() {
std::vector<int> v1;
assert(v1.size() == 0);
Expand Down Expand Up @@ -157,5 +164,18 @@ int main() {
v9.push_back(&ppa);
(**v9[0])->x = 2;
assert(a.x == 2);

std::vector<int> v10 = {1, 2, 3};
std::vector<int> 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;
}
Loading
Loading