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
8 changes: 8 additions & 0 deletions cpp2rust/converter/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1775,6 +1775,14 @@ bool Converter::VisitCallExpr(clang::CallExpr *expr) {
return false;
}

// p->~T() is a no-op when T has nothing to destruct
if (auto *dtor = clang::dyn_cast_or_null<clang::CXXDestructorDecl>(
expr->getCalleeDecl());
dtor && !RecordNeedsDestruction(dtor->getParent())) {
SetFreshType(expr->getType());
return false;
}

if (IsImplicitAssignmentCall(expr) && !Mapper::Contains(expr->getCallee())) {
auto *call = clang::cast<clang::CXXMemberCallExpr>(expr);
ConvertAssignment(call->getImplicitObjectArgument(), call->getArg(0), "=");
Expand Down
7 changes: 7 additions & 0 deletions cpp2rust/converter/models/converter_refcount.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1103,6 +1103,13 @@ bool ConverterRefCount::VisitCallExpr(clang::CallExpr *expr) {
return false;
}

// p->~T() is a no-op when T has nothing to destruct
if (auto *dtor = clang::dyn_cast_or_null<clang::CXXDestructorDecl>(
expr->getCalleeDecl());
dtor && !RecordNeedsDestruction(dtor->getParent())) {
return false;
}

if (IsImplicitAssignmentCall(expr) && !Mapper::Contains(expr->getCallee())) {
auto *call = clang::cast<clang::CXXMemberCallExpr>(expr);
ConvertAssignment(call->getImplicitObjectArgument(), call->getArg(0), "=");
Expand Down
34 changes: 34 additions & 0 deletions tests/unit/out/refcount/scalar_builtin_construct_destruct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,32 @@ use std::io::prelude::*;
use std::io::{Read, Seek, Write};
use std::os::fd::AsFd;
use std::rc::{Rc, Weak};
#[derive(Default)]
pub struct Pod {
pub v: Value<i32>,
}
impl Clone for Pod {
fn clone(&self) -> Self {
let __this: Value<Pod> = Rc::new(RefCell::new(Self {
v: Rc::new(RefCell::new((*self.v.borrow()))),
}));
let this: Ptr<Pod> = __this.as_pointer();
Rc::try_unwrap(__this).ok().unwrap().into_inner()
}
}
impl ByteRepr for Pod {
fn byte_size() -> usize {
4
}
fn to_bytes(&self, buf: &mut [u8]) {
(*self.v.borrow()).to_bytes(&mut buf[0..4]);
}
fn from_bytes(buf: &[u8]) -> Self {
Self {
v: Rc::new(RefCell::new(<i32>::from_bytes(&buf[0..4]))),
}
}
}
pub fn zero_0() -> Ptr<i32> {
return Ptr::<i32>::null();
}
Expand All @@ -15,6 +41,9 @@ pub fn zero_1() -> i64 {
pub fn destroy_2(p: Ptr<i32>) {
let p: Value<Ptr<i32>> = Rc::new(RefCell::new(p));
}
pub fn destroy_3(p: Ptr<Pod>) {
let p: Value<Ptr<Pod>> = Rc::new(RefCell::new(p));
}
pub fn main() {
__cpp2rust_init_globals();
std::process::exit(main_0());
Expand All @@ -30,6 +59,11 @@ fn main_0() -> i32 {
let x: Value<i32> = Rc::new(RefCell::new(5));
({ destroy_2((x.as_pointer())) });
assert!(((*x.borrow()) == 5));
let pod: Value<Pod> = Rc::new(RefCell::new(Pod {
v: Rc::new(RefCell::new(7)),
}));
({ destroy_3((pod.as_pointer())) });
assert!(((*(*pod.borrow()).v.borrow()) == 7));
return 0;
}
pub fn __cpp2rust_init_globals() {}
9 changes: 9 additions & 0 deletions tests/unit/out/unsafe/scalar_builtin_construct_destruct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@ 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, Default)]
pub struct Pod {
pub v: i32,
}
pub unsafe fn zero_0() -> *mut i32 {
return std::ptr::null_mut();
}
pub unsafe fn zero_1() -> i64 {
return 0_i64;
}
pub unsafe fn destroy_2(mut p: *mut i32) {}
pub unsafe fn destroy_3(mut p: *mut Pod) {}
pub fn main() {
unsafe {
__cpp2rust_init_globals();
Expand All @@ -30,6 +36,9 @@ unsafe fn main_0() -> i32 {
let mut x: i32 = 5;
(unsafe { destroy_2((&mut x as *mut i32)) });
assert!(((x) == (5)));
let mut pod: Pod = Pod { v: 7 };
(unsafe { destroy_3((&mut pod as *mut Pod)) });
assert!(((pod.v) == (7)));
return 0;
}
pub unsafe fn __cpp2rust_init_globals() {}
9 changes: 9 additions & 0 deletions tests/unit/scalar_builtin_construct_destruct.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#include <cassert>

struct Pod {
int v;
};

template <typename T> T zero() { return T(); }

template <typename T> void destroy(T *p) { p->~T(); }
Expand All @@ -18,5 +22,10 @@ int main() {
using I = int;
x.~I();
assert(x == 5);

Pod pod{7};
destroy(&pod);
pod.~Pod();
assert(pod.v == 7);
return 0;
}
Loading