Skip to content

Commit cfdd86a

Browse files
authored
Push init type in materialized temporaries (#512)
1 parent b4c051e commit cfdd86a

4 files changed

Lines changed: 52 additions & 1 deletion

File tree

‎cpp2rust/converter/converter.cpp‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,11 @@ std::pair<std::string, std::string>
239239
Converter::MaterializeTemp(const std::string &binding_name,
240240
clang::QualType param_type, clang::Expr *expr) {
241241
auto pointee = param_type.getNonReferenceType();
242-
auto value = ConvertRValue(expr, pointee);
242+
std::string value;
243+
{
244+
PushInitType init_type(*this, pointee);
245+
value = ConvertRValue(expr, pointee);
246+
}
243247
auto type_str = ToStringBase(pointee);
244248
const auto *decl = in_const_initializer_ ? keyword::kStatic : keyword::kLet;
245249

‎tests/unit/06_new_array.cpp‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
1+
static char take(char *&&p) {
2+
p[0] = 9;
3+
char c = p[0];
4+
delete[] p;
5+
return c;
6+
}
7+
18
int main() {
29
int *e = new int[2];
310
e[0] = 6;
411
e[1] = 7;
512
delete[] e;
13+
char c = take(new char[4]);
14+
(void)c;
615
return 0;
716
}

‎tests/unit/out/refcount/06_new_array.rs‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ use std::io::prelude::*;
66
use std::io::{Read, Seek, Write};
77
use std::os::fd::AsFd;
88
use std::rc::{Rc, Weak};
9+
pub fn take_0(p: Ptr<Ptr<i8>>) -> i8 {
10+
elem!((p.read()), 0).write(9_i8);
11+
let mut c: i8 = (elem!((p.read()), 0).read());
12+
(p.read()).delete();
13+
return c;
14+
}
915
pub fn main() {
1016
__cpp2rust_init_globals();
1117
std::process::exit(main_0());
@@ -15,6 +21,13 @@ fn main_0() -> i32 {
1521
elem!(e, 0).write(6);
1622
elem!(e, 1).write(7);
1723
e.delete();
24+
let mut c: i8 = ({
25+
let _p: Value<Ptr<i8>> = Rc::new(RefCell::new(Ptr::alloc_array(
26+
(0..4_usize).map(|_| 0_i8).collect::<Box<[i8]>>(),
27+
)));
28+
take_0(_p.as_pointer())
29+
});
30+
&(c);
1831
return 0;
1932
}
2033
pub fn __cpp2rust_init_globals() {}

‎tests/unit/out/unsafe/06_new_array.rs‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,21 @@ use std::collections::BTreeMap;
66
use std::io::{Read, Seek, Write};
77
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
88
use std::rc::Rc;
9+
pub unsafe fn take_0(p: *mut *mut libc::c_char) -> libc::c_char {
10+
(*(*p).offset((0) as isize)) = (9 as libc::c_char);
11+
let mut c: libc::c_char = (*(*p).offset((0) as isize));
12+
{
13+
let __p = (*p);
14+
if !__p.is_null() {
15+
::std::mem::drop(Box::from_raw(::std::slice::from_raw_parts_mut(
16+
__p,
17+
libcc2rs::malloc_usable_size(__p as *mut ::libc::c_void)
18+
/ ::std::mem::size_of::<libc::c_char>(),
19+
)))
20+
}
21+
};
22+
return c;
23+
}
924
pub fn main() {
1025
unsafe {
1126
__cpp2rust_init_globals();
@@ -27,6 +42,16 @@ unsafe fn main_0() -> i32 {
2742
)))
2843
}
2944
};
45+
let mut c: libc::c_char = (unsafe {
46+
let mut _p: *mut libc::c_char = Box::leak(
47+
(0..4_usize)
48+
.map(|_| (0 as libc::c_char))
49+
.collect::<Box<[libc::c_char]>>(),
50+
)
51+
.as_mut_ptr();
52+
take_0(&mut _p)
53+
});
54+
&(c);
3055
return 0;
3156
}
3257
pub unsafe fn __cpp2rust_init_globals() {}

0 commit comments

Comments
 (0)