diff --git a/cpp2rust/converter/converter.cpp b/cpp2rust/converter/converter.cpp index 779cfdc0b..573b6e004 100644 --- a/cpp2rust/converter/converter.cpp +++ b/cpp2rust/converter/converter.cpp @@ -1134,8 +1134,7 @@ bool Converter::VisitCXXConstructorDecl(clang::CXXConstructorDecl *decl) { } PushCurrFunction push_fn(*this, decl); - if (decl->isCopyOrMoveConstructor() && - !decl->doesThisDeclarationHaveABody()) { + if (decl->isCopyOrMoveConstructor() && !decl->hasBody()) { return false; } diff --git a/tests/unit/copy_ctor.cpp b/tests/unit/copy_ctor.cpp index 94459793c..c16058745 100644 --- a/tests/unit/copy_ctor.cpp +++ b/tests/unit/copy_ctor.cpp @@ -39,6 +39,14 @@ struct Owner { Box box; }; +struct OutOfLine { + int v; + OutOfLine(int v) : v(v) {} + OutOfLine(const OutOfLine &o); +}; + +OutOfLine::OutOfLine(const OutOfLine &o) : v(o.v + 100) { ++copies; } + static int by_value(Counted c) { return c.v; } static Counted make(int v) { @@ -89,6 +97,11 @@ int main() { Owner o2 = std::move(o); assert(o2.box.val == 3); + OutOfLine ol1(5); + OutOfLine ol2(ol1); + assert(ol2.v == 105); + assert(copies == 12); + NonConst n; NonConst n1(n); const NonConst cn; diff --git a/tests/unit/out/refcount/copy_ctor.rs b/tests/unit/out/refcount/copy_ctor.rs index 91a00d653..e16cb97ea 100644 --- a/tests/unit/out/refcount/copy_ctor.rs +++ b/tests/unit/out/refcount/copy_ctor.rs @@ -142,6 +142,31 @@ impl Owner { } } } +#[derive(Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[byte_size(4)] +pub struct OutOfLine { + #[offset(0)] + pub v: i32, +} +impl OutOfLine { + pub fn new(mut v: i32) -> Self { + Self { v: v } + } + pub fn copy_from(o: Ptr) -> Self { + let __this: OutOfLine = Self { + v: (o.with(|__s| __s.v) + 100), + }; + (*copies_0.with(Value::clone).borrow_mut()).prefix_inc(); + __this + } +} +impl Clone for OutOfLine { + fn clone(&self) -> Self { + let __src: Value = Rc::new(RefCell::new(OutOfLine { v: self.v.clone() })); + OutOfLine::copy_from(__src.as_pointer()) + } +} +impl OutOfLine {} pub fn by_value_1(mut c: Counted) -> i32 { return c.v; } @@ -212,6 +237,10 @@ fn main_0() -> i32 { })); let mut o2: Owner = Owner::move_from({ o.as_pointer() }); assert!((o2.box_.val == 3)); + let ol1: Value = Rc::new(RefCell::new(OutOfLine::new({ 5 }))); + let mut ol2: OutOfLine = OutOfLine::copy_from({ ol1.as_pointer() }); + assert!((ol2.v == 105)); + assert!((copies_0.with(|rc| *rc.borrow()) == 12)); let n: Value = Rc::new(RefCell::new(NonConst::new())); let mut n1: NonConst = NonConst::new_1({ n.as_pointer() }); let cn: Value = Rc::new(RefCell::new(NonConst::new())); diff --git a/tests/unit/out/unsafe/copy_ctor.rs b/tests/unit/out/unsafe/copy_ctor.rs index fa219598d..9b1a7d290 100644 --- a/tests/unit/out/unsafe/copy_ctor.rs +++ b/tests/unit/out/unsafe/copy_ctor.rs @@ -116,6 +116,30 @@ impl Owner { this } } +#[repr(C)] +#[derive(VaArg, FnPtrArg, Default)] +pub struct OutOfLine { + pub v: i32, +} +impl OutOfLine { + pub unsafe fn new(mut v: i32) -> Self { + let mut this = Self { v: v }; + this + } + pub unsafe fn copy_from(o: *const OutOfLine) -> Self { + let mut this = Self { + v: (((*o).v) + (100)), + }; + (*std::cell::LazyCell::force_mut(&mut *&raw mut copies_0)).prefix_inc(); + this + } +} +impl Clone for OutOfLine { + fn clone(&self) -> Self { + unsafe { OutOfLine::copy_from(self as *const OutOfLine) } + } +} +impl OutOfLine {} pub unsafe fn by_value_1(mut c: Counted) -> i32 { return c.v; } @@ -174,6 +198,10 @@ unsafe fn main_0() -> i32 { }; let mut o2: Owner = Owner::move_from({ &mut o }); assert!(((o2.box_.val) == (3))); + let mut ol1: OutOfLine = OutOfLine::new({ 5 }); + let mut ol2: OutOfLine = OutOfLine::copy_from({ &ol1 }); + assert!(((ol2.v) == (105))); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut copies_0)) == (12))); let mut n: NonConst = NonConst::new(); let mut n1: NonConst = NonConst::new_1({ &mut n }); let cn: NonConst = NonConst::new();