Skip to content
Merged
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
6 changes: 5 additions & 1 deletion cpp2rust/converter/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,11 @@ std::pair<std::string, std::string>
Converter::MaterializeTemp(const std::string &binding_name,
clang::QualType param_type, clang::Expr *expr) {
auto pointee = param_type.getNonReferenceType();
auto value = ConvertRValue(expr, pointee);
std::string value;
{
PushInitType init_type(*this, pointee);
value = ConvertRValue(expr, pointee);
}
auto type_str = ToStringBase(pointee);
const auto *decl = in_const_initializer_ ? keyword::kStatic : keyword::kLet;

Expand Down
9 changes: 9 additions & 0 deletions tests/unit/06_new_array.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
static char take(char *&&p) {
p[0] = 9;
char c = p[0];
delete[] p;
return c;
}

int main() {
int *e = new int[2];
e[0] = 6;
e[1] = 7;
delete[] e;
char c = take(new char[4]);
(void)c;
return 0;
}
13 changes: 13 additions & 0 deletions tests/unit/out/refcount/06_new_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ use std::io::prelude::*;
use std::io::{Read, Seek, Write};
use std::os::fd::AsFd;
use std::rc::{Rc, Weak};
pub fn take_0(p: Ptr<Ptr<i8>>) -> i8 {
elem!((p.read()), 0).write(9_i8);
let mut c: i8 = (elem!((p.read()), 0).read());
(p.read()).delete();
return c;
}
pub fn main() {
__cpp2rust_init_globals();
std::process::exit(main_0());
Expand All @@ -15,6 +21,13 @@ fn main_0() -> i32 {
elem!(e, 0).write(6);
elem!(e, 1).write(7);
e.delete();
let mut c: i8 = ({
let _p: Value<Ptr<i8>> = Rc::new(RefCell::new(Ptr::alloc_array(
(0..4_usize).map(|_| 0_i8).collect::<Box<[i8]>>(),
)));
take_0(_p.as_pointer())
});
&(c);
return 0;
}
pub fn __cpp2rust_init_globals() {}
25 changes: 25 additions & 0 deletions tests/unit/out/unsafe/06_new_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,21 @@ use std::collections::BTreeMap;
use std::io::{Read, Seek, Write};
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
use std::rc::Rc;
pub unsafe fn take_0(p: *mut *mut libc::c_char) -> libc::c_char {
(*(*p).offset((0) as isize)) = (9 as libc::c_char);
let mut c: libc::c_char = (*(*p).offset((0) as isize));
{
let __p = (*p);
if !__p.is_null() {
::std::mem::drop(Box::from_raw(::std::slice::from_raw_parts_mut(
__p,
libcc2rs::malloc_usable_size(__p as *mut ::libc::c_void)
/ ::std::mem::size_of::<libc::c_char>(),
)))
}
};
return c;
}
pub fn main() {
unsafe {
__cpp2rust_init_globals();
Expand All @@ -27,6 +42,16 @@ unsafe fn main_0() -> i32 {
)))
}
};
let mut c: libc::c_char = (unsafe {
let mut _p: *mut libc::c_char = Box::leak(
(0..4_usize)
.map(|_| (0 as libc::c_char))
.collect::<Box<[libc::c_char]>>(),
)
.as_mut_ptr();
take_0(&mut _p)
});
&(c);
return 0;
}
pub unsafe fn __cpp2rust_init_globals() {}
Loading