Skip to content

Commit 57693c0

Browse files
committed
Default initialize new
1 parent f83f67b commit 57693c0

5 files changed

Lines changed: 16 additions & 41 deletions

File tree

‎cpp2rust/converter/converter.cpp‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3440,12 +3440,16 @@ bool Converter::VisitCXXNewExpr(clang::CXXNewExpr *expr) {
34403440
if (!curr_init_type_.empty() && curr_init_type_.back()->isPointerType()) {
34413441
StrCat(".as_mut_ptr()");
34423442
}
3443+
SetFreshType(expr->getType());
34433444
} else {
3444-
auto initializer_as_string = ToString(expr->getInitializer());
3445+
auto initializer_as_string =
3446+
expr->getInitializer() ? ToString(expr->getInitializer())
3447+
: GetDefaultAsString(expr->getAllocatedType());
34453448
auto new_as_string =
34463449
std::format("(Box::leak(Box::new({})) as {})", initializer_as_string,
34473450
ToString(expr->getType()));
34483451
StrCat(new_as_string);
3452+
SetFreshType(expr->getType());
34493453
}
34503454
return false;
34513455
}

‎tests/unit/new_array.cpp‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,5 @@ int main() {
1818
return 1;
1919
}
2020
delete[] filled;
21-
22-
if (sum(new int[3], 0) != 0) {
23-
return 1;
24-
}
2521
return 0;
2622
}

‎tests/unit/out/refcount/new_array.rs‎

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,6 @@ fn main_0() -> i32 {
5050
return 1;
5151
}
5252
(*filled.borrow()).delete_array();
53-
if (({
54-
sum_0(
55-
Ptr::alloc_array(
56-
(0..3_usize)
57-
.map(|_| <i32>::default())
58-
.collect::<Box<[i32]>>(),
59-
),
60-
0,
61-
)
62-
}) != 0)
63-
{
64-
return 1;
65-
}
6653
return 0;
6754
}
6855
pub fn __cpp2rust_init_globals() {}

‎tests/unit/out/unsafe/new_array.rs‎

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,6 @@ unsafe fn main_0() -> i32 {
4747
filled,
4848
libcc2rs::malloc_usable_size(filled as *mut ::libc::c_void) / ::std::mem::size_of::<i32>(),
4949
)));
50-
if ((unsafe {
51-
sum_0(
52-
Box::leak((0..3_usize).map(|_| 0_i32).collect::<Box<[i32]>>()).as_mut_ptr(),
53-
0,
54-
)
55-
}) != (0))
56-
{
57-
return 1;
58-
}
5950
return 0;
6051
}
6152
pub unsafe fn __cpp2rust_init_globals() {}

‎tests/unit/out/unsafe/unique_ptr.rs‎

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -82,17 +82,17 @@ pub unsafe fn Consume_1(mut safe_ptr: Option<Box<SafePointer>>) -> i32 {
8282
}
8383
pub unsafe fn RndStuff_2() {
8484
let mut x1: Option<Box<[i32]>> = None;
85-
let mut x2: Option<Box<[i32]>> = Some(Box::from_raw(
86-
Box::leak((0..100_usize).map(|_| 0_i32).collect::<Box<[i32]>>()).as_mut_ptr(),
87-
));
85+
let mut x2: Option<Box<[i32]>> = Some(Box::from_raw(Box::leak(
86+
(0..100_usize).map(|_| 0_i32).collect::<Box<[i32]>>(),
87+
)));
8888
let mut i: i32 = 0;
8989
'loop_: while ((i) < (100)) {
9090
x2.as_mut().unwrap()[(i as usize)] = 1;
9191
i.prefix_inc();
9292
}
93-
x2 = Some(Box::from_raw(
94-
Box::leak((0..200_usize).map(|_| 0_i32).collect::<Box<[i32]>>()).as_mut_ptr(),
95-
));
93+
x2 = Some(Box::from_raw(Box::leak(
94+
(0..200_usize).map(|_| 0_i32).collect::<Box<[i32]>>(),
95+
)));
9696
let mut i: i32 = 0;
9797
'loop_: while ((i) < (200)) {
9898
x2.as_mut().unwrap()[(i as usize)] = 2;
@@ -128,14 +128,11 @@ pub unsafe fn RndStuff_2() {
128128
assert!((((*p3_0.offset((i) as isize)).y) == (12)));
129129
i.prefix_inc();
130130
}
131-
x3 = Some(Box::from_raw(
132-
Box::leak(
133-
(0..50_usize)
134-
.map(|_| <Pair>::default())
135-
.collect::<Box<[Pair]>>(),
136-
)
137-
.as_mut_ptr(),
138-
));
131+
x3 = Some(Box::from_raw(Box::leak(
132+
(0..50_usize)
133+
.map(|_| <Pair>::default())
134+
.collect::<Box<[Pair]>>(),
135+
)));
139136
let mut i: i32 = 0;
140137
'loop_: while ((i) < (50)) {
141138
x3.as_mut().unwrap()[(i as usize)] = Pair {

0 commit comments

Comments
 (0)