diff --git a/cpp2rust/converter/converter.cpp b/cpp2rust/converter/converter.cpp index b63c7fcb3..5f15e37d6 100644 --- a/cpp2rust/converter/converter.cpp +++ b/cpp2rust/converter/converter.cpp @@ -239,7 +239,11 @@ std::pair 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; diff --git a/tests/unit/06_new_array.cpp b/tests/unit/06_new_array.cpp index 004dc6bee..ac9348abc 100644 --- a/tests/unit/06_new_array.cpp +++ b/tests/unit/06_new_array.cpp @@ -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; } diff --git a/tests/unit/out/refcount/06_new_array.rs b/tests/unit/out/refcount/06_new_array.rs index 08e5322b7..942ca5257 100644 --- a/tests/unit/out/refcount/06_new_array.rs +++ b/tests/unit/out/refcount/06_new_array.rs @@ -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>) -> 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()); @@ -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> = Rc::new(RefCell::new(Ptr::alloc_array( + (0..4_usize).map(|_| 0_i8).collect::>(), + ))); + take_0(_p.as_pointer()) + }); + &(c); return 0; } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/unsafe/06_new_array.rs b/tests/unit/out/unsafe/06_new_array.rs index 80fa1ff5c..4558ec946 100644 --- a/tests/unit/out/unsafe/06_new_array.rs +++ b/tests/unit/out/unsafe/06_new_array.rs @@ -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::(), + ))) + } + }; + return c; +} pub fn main() { unsafe { __cpp2rust_init_globals(); @@ -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::>(), + ) + .as_mut_ptr(); + take_0(&mut _p) + }); + &(c); return 0; } pub unsafe fn __cpp2rust_init_globals() {}