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
3 changes: 2 additions & 1 deletion cpp2rust/converter/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()");
}
Expand Down
6 changes: 4 additions & 2 deletions cpp2rust/converter/models/boxed_vars.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,12 @@ template <typename E> 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));
Expand Down
39 changes: 39 additions & 0 deletions tests/std/class/class.mem/class.ctor/class.copy.ctor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// ADDITIONAL_COMPILE_FLAGS: -std=c++23
#include <cassert>
#include <initializer_list>
#include <utility>

// 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<int> source = {1, 2, 3};
std::initializer_list<int> 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;
}
Original file line number Diff line number Diff line change
@@ -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<p11_defaulted_move_constructor_is_trivial_MoveOnly>) -> 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<p11_defaulted_move_constructor_is_trivial_MoveOnly> =
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<Vec<i32>> = Rc::new(RefCell::new(Vec::from([1, 2, 3])));
let moved: Value<Vec<i32>> = 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() {}
57 changes: 57 additions & 0 deletions tests/std/class/class.mem/class.ctor/out/unsafe/class.copy.ctor.rs
Original file line number Diff line number Diff line change
@@ -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<i32> = Vec::from([1, 2, 3]);
let mut moved: Vec<i32> = 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() {}
Loading