diff --git a/cpp2rust/converter/converter.cpp b/cpp2rust/converter/converter.cpp index 32b385f073..c2323d72c8 100644 --- a/cpp2rust/converter/converter.cpp +++ b/cpp2rust/converter/converter.cpp @@ -4046,7 +4046,8 @@ bool Converter::VisitCXXConstructExpr(clang::CXXConstructExpr *expr) { } if (IsPassThroughConstructor(ctor)) { Convert(expr->getArg(0)); - if ((ctor->isCopyConstructor() || IsDefaultedMoveConstructor(ctor)) && + if ((ctor->isCopyConstructor() || IsDefaultedMoveConstructor(ctor) || + (ctor->isMoveConstructor() && ctor->isTrivial())) && !TypeIsCopyable(expr->getType())) { EmitFresh(".clone()"); } diff --git a/cpp2rust/converter/models/boxed_vars.cpp b/cpp2rust/converter/models/boxed_vars.cpp index 3934c5a434..bc198b47fe 100644 --- a/cpp2rust/converter/models/boxed_vars.cpp +++ b/cpp2rust/converter/models/boxed_vars.cpp @@ -71,10 +71,12 @@ template E *IgnoreNoOpCasts(E *expr) { } // The object copied by a trivial copy or move constructor, which copies its -// value, like an lvalue-to-rvalue conversion in C. +// value, like an lvalue-to-rvalue conversion in C, unless it is translated to +// a call to the constructor. clang::Expr *GetTrivialCopySource(clang::CXXConstructExpr *expr) { auto *ctor = expr->getConstructor(); - if (!ctor->isCopyOrMoveConstructor() || !ctor->isTrivial()) { + if (!ctor->isCopyOrMoveConstructor() || !ctor->isTrivial() || + IsConvertibleCopyOrMoveConstructor(ctor)) { return nullptr; } return IgnoreNoOpCasts(expr->getArg(0)); diff --git a/tests/std/class/class.mem/class.ctor/class.copy.ctor.cpp b/tests/std/class/class.mem/class.ctor/class.copy.ctor.cpp new file mode 100644 index 0000000000..2335434f74 --- /dev/null +++ b/tests/std/class/class.mem/class.ctor/class.copy.ctor.cpp @@ -0,0 +1,39 @@ +// ADDITIONAL_COMPILE_FLAGS: -std=c++23 +#include +#include +#include + +// N4950 [class.copy.ctor]/11: a copy/move constructor that is not +// user-provided is trivial. +namespace p11_defaulted_move_constructor_is_trivial { +struct MoveOnly { + int value; + MoveOnly(int value) : value(value) {} + MoveOnly(const MoveOnly &) = delete; + MoveOnly(MoveOnly &&) = default; +}; + +MoveOnly pass(MoveOnly object) { return object; } + +void test() { + MoveOnly result = pass(MoveOnly(7)); + assert(result.value == 7); +} +} // namespace p11_defaulted_move_constructor_is_trivial + +// N4950 [class.copy.ctor]/14: the implicitly-defined move constructor +// performs a memberwise move, so a trivial move leaves the source unchanged. +namespace p14_trivial_move_keeps_source { +void test() { + std::initializer_list source = {1, 2, 3}; + std::initializer_list moved = std::move(source); + assert(moved.size() == 3); + assert(source.size() == 3); +} +} // namespace p14_trivial_move_keeps_source + +int main() { + p11_defaulted_move_constructor_is_trivial::test(); + p14_trivial_move_keeps_source::test(); + return 0; +} diff --git a/tests/std/class/class.mem/class.ctor/out/refcount/class.copy.ctor.rs b/tests/std/class/class.mem/class.ctor/out/refcount/class.copy.ctor.rs new file mode 100644 index 0000000000..e75bb4d943 --- /dev/null +++ b/tests/std/class/class.mem/class.ctor/out/refcount/class.copy.ctor.rs @@ -0,0 +1,56 @@ +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(Record, ByteRepr, FnPtrArg, MoveCtor, CppFrom, Default)] +#[cpp_from(i32, new, value)] +#[byte_size(4)] +pub struct p11_defaulted_move_constructor_is_trivial_MoveOnly { + #[offset(0)] + pub value: i32, +} +impl p11_defaulted_move_constructor_is_trivial_MoveOnly { + pub fn new(mut value: i32) -> Self { + Self { value: value } + } + pub fn move_from(_a0: Ptr) -> Self { + Self { + value: { (*_a0.upgrade().deref()).value }, + } + } +} +pub fn pass_0( + object: p11_defaulted_move_constructor_is_trivial_MoveOnly, +) -> p11_defaulted_move_constructor_is_trivial_MoveOnly { + let object: Value = + Rc::new(RefCell::new(object)); + return p11_defaulted_move_constructor_is_trivial_MoveOnly::move_from({ object.as_pointer() }); +} +pub fn test_1() { + let mut result: p11_defaulted_move_constructor_is_trivial_MoveOnly = ({ + pass_0(p11_defaulted_move_constructor_is_trivial_MoveOnly::new({ + 7 + })) + }); + assert!((result.value == 7)); +} +pub fn test_2() { + let source: Value> = Rc::new(RefCell::new(Vec::from([1, 2, 3]))); + let moved: Value> = Rc::new(RefCell::new((*source.borrow_mut()).clone())); + assert!(({ ((*moved.borrow()).len() == 3_usize) })); + assert!(({ ((*source.borrow()).len() == 3_usize) })); +} +pub fn main() { + __cpp2rust_init_globals(); + std::process::exit(main_0()); +} +fn main_0() -> i32 { + ({ test_1() }); + ({ test_2() }); + return 0; +} +pub fn __cpp2rust_init_globals() {} diff --git a/tests/std/class/class.mem/class.ctor/out/unsafe/class.copy.ctor.rs b/tests/std/class/class.mem/class.ctor/out/unsafe/class.copy.ctor.rs new file mode 100644 index 0000000000..c704263540 --- /dev/null +++ b/tests/std/class/class.mem/class.ctor/out/unsafe/class.copy.ctor.rs @@ -0,0 +1,57 @@ +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(FnPtrArg, MoveCtorUnsafe, CppFromUnsafe, Default)] +#[cpp_from(i32, new, value)] +pub struct p11_defaulted_move_constructor_is_trivial_MoveOnly { + pub value: i32, +} +impl p11_defaulted_move_constructor_is_trivial_MoveOnly { + pub unsafe fn new(mut value: i32) -> Self { + let mut this = Self { value: value }; + this + } + pub unsafe fn move_from(_a0: *mut p11_defaulted_move_constructor_is_trivial_MoveOnly) -> Self { + let mut this = Self { + value: (*_a0).value, + }; + this + } +} +pub unsafe fn pass_0( + mut object: p11_defaulted_move_constructor_is_trivial_MoveOnly, +) -> p11_defaulted_move_constructor_is_trivial_MoveOnly { + return p11_defaulted_move_constructor_is_trivial_MoveOnly::move_from({ &mut object }); +} +pub unsafe fn test_1() { + let mut result: p11_defaulted_move_constructor_is_trivial_MoveOnly = (unsafe { + pass_0(p11_defaulted_move_constructor_is_trivial_MoveOnly::new({ + 7 + })) + }); + assert!(((result.value) == (7))); +} +pub unsafe fn test_2() { + let mut source: Vec = Vec::from([1, 2, 3]); + let mut moved: Vec = source.clone(); + assert!(((moved.len()) == (3_usize))); + assert!(((source.len()) == (3_usize))); +} +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + (unsafe { test_1() }); + (unsafe { test_2() }); + return 0; +} +pub unsafe fn __cpp2rust_init_globals() {}