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
9 changes: 5 additions & 4 deletions cpp2rust/converter/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4161,10 +4161,11 @@ bool Converter::VisitCXXDeleteExpr(clang::CXXDeleteExpr *expr) {
}
auto record_name = GetRecordName(destroyed_type->getAsCXXRecordDecl());
if (expr->isArrayForm()) {
StrCat(std::format("for __i in 0..libcc2rs::malloc_usable_size(__p as *mut "
"::libc::c_void) / ::std::mem::size_of::<{0}>() {{ "
"{0}::{1}(&mut *__p.add(__i)); }}",
record_name, kDestructorName));
StrCat(
std::format("for __i in (0..libcc2rs::malloc_usable_size(__p as *mut "
"::libc::c_void) / ::std::mem::size_of::<{0}>()).rev() {{ "
"{0}::{1}(&mut *__p.add(__i)); }}",
record_name, kDestructorName));
} else {
StrCat(std::format("{}::{}(&mut *__p)", record_name, kDestructorName),
token::kSemiColon);
Expand Down
2 changes: 1 addition & 1 deletion cpp2rust/converter/models/converter_refcount.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2025,7 +2025,7 @@ bool ConverterRefCount::VisitCXXDeleteExpr(clang::CXXDeleteExpr *expr) {
StrCat(keyword::kLet, "__p", token::kAssign, ToString(expr->getArgument()),
token::kSemiColon);
if (expr->isArrayForm()) {
StrCat(std::format("for __i in 0..__p.len() {{ __p.offset(__i as "
StrCat(std::format("for __i in (0..__p.len()).rev() {{ __p.offset(__i as "
"isize).{}(); }}",
kDestructorName));
} else {
Expand Down
15 changes: 15 additions & 0 deletions tests/unit/delete_array_order.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <cstdio>

struct Element {
long id;
long value;
~Element() { printf("%ld:%ld ", id, value); }
};

int main() {
Element *a = new Element[3]{{1, 10}, {2, 20}, {3, 30}};
delete[] a;
printf("\n");

return 0;
}
58 changes: 58 additions & 0 deletions tests/unit/out/refcount/delete_array_order.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
extern crate libcc2rs;
use libcc2rs::*;
use std::cell::RefCell;
use std::collections::BTreeMap;
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, Destructor, Default)]
#[byte_size(16)]
pub struct Element {
#[offset(0)]
pub id: i64,
#[offset(8)]
pub value: i64,
}
pub fn main() {
__cpp2rust_init_globals();
libcc2rs::cpp2rust_exit(main_0());
}
fn main_0() -> i32 {
let mut a: Ptr<Element> = Ptr::alloc_array(Box::new([
Element {
id: 1_i64,
value: 10_i64,
},
Element {
id: 2_i64,
value: 20_i64,
},
Element {
id: 3_i64,
value: 30_i64,
},
]));
{
let __p = a;
for __i in (0..__p.len()).rev() {
__p.offset(__i as isize).destructor();
}
__p.delete();
};
printf!(b"\n");
return 0;
}
pub trait ElementImpl {
fn destructor(&self);
}
impl ElementImpl for Ptr<Element> {
fn destructor(&self) {
printf!(
b"%ld:%ld ",
(*self).with(|__s| __s.id),
(*self).with(|__s| __s.value)
);
}
}
pub fn __cpp2rust_init_globals() {}
67 changes: 67 additions & 0 deletions tests/unit/out/unsafe/delete_array_order.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
extern crate libc;
use libc::*;
extern crate libcc2rs;
use libcc2rs::*;
use std::collections::BTreeMap;
use std::io::{Read, Seek, Write};
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
use std::rc::Rc;
#[repr(C)]
#[derive(Clone, VaArg, FnPtrArg, DestructorUnsafe, Default)]
pub struct Element {
pub id: i64,
pub value: i64,
}
impl Element {
pub unsafe fn destructor(&mut self) {
(unsafe {
libc::printf(
c"%ld:%ld ".as_ptr() as *const libc::c_char,
(self.id),
(self.value),
)
});
}
}
pub fn main() {
unsafe {
__cpp2rust_init_globals();
libcc2rs::cpp2rust_exit(main_0() as i32);
}
}
unsafe fn main_0() -> i32 {
let mut a: *mut Element = Box::leak(Box::new([
Element {
id: 1_i64,
value: 10_i64,
},
Element {
id: 2_i64,
value: 20_i64,
},
Element {
id: 3_i64,
value: 30_i64,
},
]))
.as_mut_ptr();
{
let __p = a;
if !__p.is_null() {
for __i in (0..libcc2rs::malloc_usable_size(__p as *mut ::libc::c_void)
/ ::std::mem::size_of::<Element>())
.rev()
{
Element::destructor(&mut *__p.add(__i));
}
::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::<Element>(),
)))
}
};
(unsafe { libc::printf(c"\n".as_ptr() as *const libc::c_char) });
return 0;
}
pub unsafe fn __cpp2rust_init_globals() {}
Loading