Skip to content

Commit 7d43107

Browse files
committed
Always convert calls to move constructor
1 parent e3c78ce commit 7d43107

31 files changed

Lines changed: 1042 additions & 357 deletions

‎cpp2rust/converter/converter.cpp‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3424,9 +3424,8 @@ bool Converter::VisitCXXConstructExpr(clang::CXXConstructExpr *expr) {
34243424
// Take suppress before recursing into the child.
34253425
bool suppress = PushSuppressIteratorClone::take(*this);
34263426
Convert(expr->getArg(0));
3427-
if ((ctor->isCopyConstructor() ||
3428-
(ctor->isMoveConstructor() && IsUserDefinedDecl(ctor->getParent()))) &&
3429-
!suppress && !TypeIsCopyable(expr->getType())) {
3427+
if (ctor->isCopyConstructor() && !suppress &&
3428+
!TypeIsCopyable(expr->getType())) {
34303429
StrCat(".clone()");
34313430
}
34323431
return false;

‎cpp2rust/converter/converter_lib.cpp‎

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -286,24 +286,9 @@ bool IsUserDefinedCopyConstructor(const clang::CXXConstructorDecl *ctor) {
286286
IsUserDefinedDecl(ctor);
287287
}
288288

289-
static bool HasUserProvidedCopyMember(const clang::CXXRecordDecl *decl) {
290-
return std::any_of(decl->method_begin(), decl->method_end(), [](auto *m) {
291-
auto *ctor = clang::dyn_cast<clang::CXXConstructorDecl>(m);
292-
return m->isUserProvided() &&
293-
(ctor ? ctor->isCopyConstructor() : m->isCopyAssignmentOperator());
294-
});
295-
}
296-
297289
static bool IsTranslatedMoveMember(const clang::CXXMethodDecl *method) {
298-
if (method->isDeleted() || !IsUserDefinedDecl(method->getParent())) {
299-
return false;
300-
}
301-
if (method->isUserProvided()) {
302-
return true;
303-
}
304-
return method->isDefaulted() && method->hasBody() &&
305-
(!method->isTrivial() ||
306-
HasUserProvidedCopyMember(method->getParent()));
290+
return !method->isDeleted() && IsUserDefinedDecl(method->getParent()) &&
291+
method->hasBody();
307292
}
308293

309294
bool IsUserDefinedMoveConstructor(const clang::CXXConstructorDecl *ctor) {
@@ -1348,14 +1333,12 @@ bool IsBuiltinVaCopy(const clang::CallExpr *expr) {
13481333
}
13491334

13501335
const clang::Expr *IgnoreStdMove(const clang::Expr *expr) {
1351-
while (true) {
1352-
const auto *call =
1353-
clang::dyn_cast<clang::CallExpr>(expr->IgnoreParenImpCasts());
1354-
if (!call || !call->isCallToStdMove()) {
1355-
return expr;
1356-
}
1357-
expr = call->getArg(0);
1336+
if (const auto *call =
1337+
clang::dyn_cast<clang::CallExpr>(expr->IgnoreParenImpCasts());
1338+
call && call->isCallToStdMove()) {
1339+
return call->getArg(0);
13581340
}
1341+
return expr;
13591342
}
13601343

13611344
bool IsTemporaryObject(const clang::Expr *expr) {

‎cpp2rust/converter/models/converter_refcount.cpp‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1892,8 +1892,7 @@ bool ConverterRefCount::VisitCXXConstructExpr(clang::CXXConstructExpr *expr) {
18921892
return false;
18931893
}
18941894

1895-
if (ctor->isCopyOrMoveConstructor() &&
1896-
!IsUserDefinedCopyOrMoveConstructor(ctor)) {
1895+
if (ctor->isCopyConstructor() && !IsUserDefinedCopyConstructor(ctor)) {
18971896
StrCat(PushSuppressIteratorClone::take(*this)
18981897
? ConvertRValue(expr->getArg(0))
18991898
: ConvertFreshRValue(expr->getArg(0)));

‎tests/unit/out/refcount/bst.rs‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,21 @@ pub struct node_t {
1212
pub right: Value<Ptr<node_t>>,
1313
pub value: Value<i32>,
1414
}
15+
impl node_t {
16+
pub fn node_t_pmutnode_t(_a0: Ptr<node_t>) -> Self {
17+
let __this: Value<node_t> = Rc::new(RefCell::new(Self {
18+
left: Rc::new(RefCell::new(
19+
(*(*_a0.upgrade().deref()).left.borrow()).clone(),
20+
)),
21+
right: Rc::new(RefCell::new(
22+
(*(*_a0.upgrade().deref()).right.borrow()).clone(),
23+
)),
24+
value: Rc::new(RefCell::new((*(*_a0.upgrade().deref()).value.borrow()))),
25+
}));
26+
let this: Ptr<node_t> = __this.as_pointer();
27+
Rc::try_unwrap(__this).ok().unwrap().into_inner()
28+
}
29+
}
1530
impl Clone for node_t {
1631
fn clone(&self) -> Self {
1732
let __this: Value<node_t> = Rc::new(RefCell::new(Self {

‎tests/unit/out/refcount/copy_move_defaulted.rs‎

Lines changed: 78 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ use std::rc::{Rc, Weak};
1010
pub struct Inner {
1111
pub x: Value<i32>,
1212
}
13+
impl Inner {
14+
pub fn Inner_pmutInner(_a0: Ptr<Inner>) -> Self {
15+
let __this: Value<Inner> = Rc::new(RefCell::new(Self {
16+
x: Rc::new(RefCell::new((*(*_a0.upgrade().deref()).x.borrow()))),
17+
}));
18+
let this: Ptr<Inner> = __this.as_pointer();
19+
Rc::try_unwrap(__this).ok().unwrap().into_inner()
20+
}
21+
}
1322
impl Clone for Inner {
1423
fn clone(&self) -> Self {
1524
let __this: Value<Inner> = Rc::new(RefCell::new(Self {
@@ -51,6 +60,19 @@ impl Explicit {
5160
let this: Ptr<Explicit> = __this.as_pointer();
5261
Rc::try_unwrap(__this).ok().unwrap().into_inner()
5362
}
63+
pub fn Explicit_pmutExplicit(_a0: Ptr<Explicit>) -> Self {
64+
let __this: Value<Explicit> = Rc::new(RefCell::new(Self {
65+
v: Rc::new(RefCell::new((*(*_a0.upgrade().deref()).v.borrow()))),
66+
inner: Rc::new(RefCell::new(Inner::Inner_pmutInner({
67+
(*_a0.upgrade().deref()).inner.as_pointer()
68+
}))),
69+
arr: Rc::new(RefCell::new(
70+
(*(*_a0.upgrade().deref()).arr.borrow()).clone(),
71+
)),
72+
}));
73+
let this: Ptr<Explicit> = __this.as_pointer();
74+
Rc::try_unwrap(__this).ok().unwrap().into_inner()
75+
}
5476
}
5577
impl Clone for Explicit {
5678
fn clone(&self) -> Self {
@@ -97,6 +119,21 @@ pub struct Implicit {
97119
pub inner: Value<Inner>,
98120
pub arr: Value<Box<[i32]>>,
99121
}
122+
impl Implicit {
123+
pub fn Implicit_pmutImplicit(_a0: Ptr<Implicit>) -> Self {
124+
let __this: Value<Implicit> = Rc::new(RefCell::new(Self {
125+
v: Rc::new(RefCell::new((*(*_a0.upgrade().deref()).v.borrow()))),
126+
inner: Rc::new(RefCell::new(Inner::Inner_pmutInner({
127+
(*_a0.upgrade().deref()).inner.as_pointer()
128+
}))),
129+
arr: Rc::new(RefCell::new(
130+
(*(*_a0.upgrade().deref()).arr.borrow()).clone(),
131+
)),
132+
}));
133+
let this: Ptr<Implicit> = __this.as_pointer();
134+
Rc::try_unwrap(__this).ok().unwrap().into_inner()
135+
}
136+
}
100137
impl Clone for Implicit {
101138
fn clone(&self) -> Self {
102139
let __this: Value<Implicit> = Rc::new(RefCell::new(Self {
@@ -315,7 +352,9 @@ fn main_0() -> i32 {
315352
let _dtor_b = ScopedDestructor::new(&b, |__p| __p.destructor());
316353
let c: Value<Explicit> = Rc::new(RefCell::new((*a.borrow()).clone()));
317354
let _dtor_c = ScopedDestructor::new(&c, |__p| __p.destructor());
318-
let d: Value<Explicit> = Rc::new(RefCell::new((*a.borrow()).clone()));
355+
let d: Value<Explicit> = Rc::new(RefCell::new(Explicit::Explicit_pmutExplicit({
356+
a.as_pointer()
357+
})));
319358
let _dtor_d = ScopedDestructor::new(&d, |__p| __p.destructor());
320359
assert!(
321360
(({ same_0(b.as_pointer(), a.as_pointer(),) })
@@ -327,7 +366,7 @@ fn main_0() -> i32 {
327366
let f: Value<Explicit> = Rc::new(RefCell::new(Explicit::Explicit({ 3 })));
328367
let _dtor_f = ScopedDestructor::new(&f, |__p| __p.destructor());
329368
(*e.borrow_mut()) = (*b.borrow()).clone();
330-
(*f.borrow_mut()) = (*c.borrow()).clone();
369+
({ ExplicitImpl::operator_assign_pmutExplicit(&f.as_pointer(), c.as_pointer()) });
331370
assert!(
332371
({ same_0(e.as_pointer(), b.as_pointer(),) })
333372
&& ({ same_0(f.as_pointer(), c.as_pointer(),) })
@@ -350,7 +389,9 @@ fn main_0() -> i32 {
350389
arr: Rc::new(RefCell::new(Box::new([5, 6]))),
351390
}));
352391
let j: Value<Implicit> = Rc::new(RefCell::new((*i.borrow()).clone()));
353-
let k: Value<Implicit> = Rc::new(RefCell::new((*i.borrow()).clone()));
392+
let k: Value<Implicit> = Rc::new(RefCell::new(Implicit::Implicit_pmutImplicit({
393+
i.as_pointer()
394+
})));
354395
assert!(
355396
(((*(*j.borrow()).v.borrow()) == 5)
356397
&& ((*(*(*j.borrow()).inner.borrow()).x.borrow()) == 50))
@@ -551,11 +592,45 @@ impl DefaultCopyUserMoveImpl for Ptr<DefaultCopyUserMove> {
551592
}
552593
}
553594
pub trait ExplicitImpl {
595+
fn operator_assign_pmutExplicit(&self, _a0: Ptr<Explicit>) -> Ptr<Explicit>;
554596
fn destructor(&self);
555597
}
556598
impl ExplicitImpl for Ptr<Explicit> {
599+
fn operator_assign_pmutExplicit(&self, _a0: Ptr<Explicit>) -> Ptr<Explicit> {
600+
let __rhs = (*(*_a0.upgrade().deref()).v.borrow());
601+
(*(*(*self).upgrade().deref()).v.borrow_mut()) = __rhs;
602+
({
603+
let _arg0: Ptr<Inner> = (*_a0.upgrade().deref()).inner.as_pointer();
604+
InnerImpl::operator_assign_pmutInner(
605+
&(*(*self).upgrade().deref()).inner.as_pointer(),
606+
_arg0,
607+
)
608+
});
609+
{
610+
(((*(*self).upgrade().deref()).arr.as_pointer()) as Ptr<i32>)
611+
.to_any()
612+
.memcpy(
613+
&(((*_a0.upgrade().deref()).arr.as_pointer()) as Ptr<i32>).to_any(),
614+
8_usize as usize,
615+
);
616+
(((*(*self).upgrade().deref()).arr.as_pointer()) as Ptr<i32>)
617+
.to_any()
618+
.clone()
619+
};
620+
return (*self).clone();
621+
}
557622
fn destructor(&self) {}
558623
}
624+
pub trait InnerImpl {
625+
fn operator_assign_pmutInner(&self, _a0: Ptr<Inner>) -> Ptr<Inner>;
626+
}
627+
impl InnerImpl for Ptr<Inner> {
628+
fn operator_assign_pmutInner(&self, _a0: Ptr<Inner>) -> Ptr<Inner> {
629+
let __rhs = (*(*_a0.upgrade().deref()).x.borrow());
630+
(*(*(*self).upgrade().deref()).x.borrow_mut()) = __rhs;
631+
return (*self).clone();
632+
}
633+
}
559634
pub trait UserCopyDefaultMoveImpl {
560635
fn operator_assign_pconstUserCopyDefaultMove(
561636
&self,

0 commit comments

Comments
 (0)