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: 1 addition & 2 deletions cpp2rust/converter/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
13 changes: 13 additions & 0 deletions tests/unit/copy_ctor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ struct Owner {
Box<int> 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) {
Expand Down Expand Up @@ -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;
Expand Down
29 changes: 29 additions & 0 deletions tests/unit/out/refcount/copy_ctor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<OutOfLine>) -> 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<OutOfLine> = 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;
}
Expand Down Expand Up @@ -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<OutOfLine> = 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<NonConst> = Rc::new(RefCell::new(NonConst::new()));
let mut n1: NonConst = NonConst::new_1({ n.as_pointer() });
let cn: Value<NonConst> = Rc::new(RefCell::new(NonConst::new()));
Expand Down
28 changes: 28 additions & 0 deletions tests/unit/out/unsafe/copy_ctor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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();
Expand Down
Loading