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
5 changes: 3 additions & 2 deletions cpp2rust/converter/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3748,8 +3748,9 @@ void Converter::EmitDeallocation(clang::CXXDeleteExpr *expr,
argument_as_string, argument_as_string, destroyed_type_as_string));
}
} else {
StrCat(
std::format("::std::mem::drop(Box::from_raw({}))", argument_as_string));
bool cast_mut = !IsMut(expr->getDestroyedType());
StrCat(std::format("::std::mem::drop(Box::from_raw({}{}))",
argument_as_string, cast_mut ? ".cast_mut()" : ""));
}
}

Expand Down
9 changes: 9 additions & 0 deletions tests/unit/05_new.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
struct A {};

struct D {
void operator()(const A *ptr) const { delete ptr; }
};

int main() {
int *d = new int(0);
*d = 5;
delete d;
const int *c = new int(3);
delete c;
D{}(new A);
return 0;
}
22 changes: 22 additions & 0 deletions tests/unit/out/refcount/05_new.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};
#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)]
#[byte_size(1)]
pub struct A {}
#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)]
#[byte_size(1)]
pub struct D {}
pub fn main() {
__cpp2rust_init_globals();
std::process::exit(main_0());
Expand All @@ -14,6 +20,22 @@ fn main_0() -> i32 {
let mut d: Ptr<i32> = Ptr::alloc(0);
d.write(5);
d.delete();
let mut c: Ptr<i32> = Ptr::alloc(3);
c.delete();
({
DImpl::operator_call(
&Rc::new(RefCell::new(<D>::default())).as_pointer(),
Ptr::alloc(<A>::default()),
)
});
return 0;
}
pub trait DImpl {
fn operator_call(&self, ptr: Ptr<A>);
}
impl DImpl for Ptr<D> {
fn operator_call(&self, mut ptr: Ptr<A>) {
ptr.delete();
}
}
pub fn __cpp2rust_init_globals() {}
29 changes: 29 additions & 0 deletions tests/unit/out/unsafe/05_new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,22 @@ use std::collections::BTreeMap;
use std::io::{Read, Seek, Write};
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
use std::rc::Rc;
#[repr(C)]
#[derive(Copy, Clone, VaArg, FnPtrArg, Default)]
pub struct A {}
#[repr(C)]
#[derive(Copy, Clone, VaArg, FnPtrArg, Default)]
pub struct D {}
impl D {
pub unsafe fn operator_call(&self, mut ptr: *const A) {
{
let __p = ptr;
if !__p.is_null() {
::std::mem::drop(Box::from_raw(__p.cast_mut()))
}
};
}
}
pub fn main() {
unsafe {
__cpp2rust_init_globals();
Expand All @@ -21,6 +37,19 @@ unsafe fn main_0() -> i32 {
::std::mem::drop(Box::from_raw(__p))
}
};
let mut c: *const i32 = (Box::leak(Box::new(3)) as *mut i32).cast_const();
{
let __p = c;
if !__p.is_null() {
::std::mem::drop(Box::from_raw(__p.cast_mut()))
}
};
(unsafe {
D::operator_call(
&<D>::default(),
(Box::leak(Box::new(<A>::default())) as *mut A).cast_const(),
)
});
return 0;
}
pub unsafe fn __cpp2rust_init_globals() {}
Loading