From b66fca3777ff7defdeeb6986efb89e23a8630160 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Mon, 14 Sep 2026 20:10:58 +0100 Subject: [PATCH 01/16] Convert move constructor When the move constructor is user written (1) or the move constructor is defaulted/implicit and copy constructor is user written (2). The rationale for 2 is: if user-written move constructor is defaulted then the first choice is to call .clone(). But if the copy constructor is user written, then that's wrong. So force the synthetization of the move constructor in that case. --- cpp2rust/converter/converter.cpp | 87 ++++++++++++++----- cpp2rust/converter/converter.h | 5 ++ cpp2rust/converter/converter_lib.cpp | 51 ++++++++++- cpp2rust/converter/converter_lib.h | 8 ++ .../converter/models/converter_refcount.cpp | 7 ++ .../converter/models/converter_refcount.h | 1 + tests/unit/out/refcount/clone_vs_move.rs | 4 +- tests/unit/out/refcount/copy_assign.rs | 8 +- tests/unit/out/refcount/copy_ctor.rs | 8 +- tests/unit/out/refcount/default.rs | 8 +- tests/unit/out/refcount/default_in_statics.rs | 4 +- tests/unit/out/refcount/destructor.rs | 4 +- tests/unit/out/refcount/huffman.rs | 32 +++++++ tests/unit/out/refcount/kruskal.rs | 52 +++++++++++ tests/unit/out/refcount/offsetof.rs | 4 +- .../operator_comparison_noncopyable.rs | 7 ++ .../refcount/operator_member_pointer_free.rs | 4 +- .../operator_member_pointer_member.rs | 4 +- tests/unit/out/refcount/pointer_array.rs | 4 +- tests/unit/out/refcount/random.rs | 8 +- tests/unit/out/refcount/unique_ptr.rs | 17 ++++ .../out/refcount/unique_ptr_const_deref.rs | 21 +++++ tests/unit/out/refcount/unique_ptr_nested.rs | 21 +++++ tests/unit/out/refcount/void_cast.rs | 21 +++++ tests/unit/out/unsafe/huffman.rs | 18 ++++ tests/unit/out/unsafe/kruskal.rs | 33 +++++++ .../unsafe/operator_comparison_noncopyable.rs | 6 ++ tests/unit/out/unsafe/unique_ptr.rs | 13 +++ .../unit/out/unsafe/unique_ptr_const_deref.rs | 12 +++ tests/unit/out/unsafe/unique_ptr_nested.rs | 12 +++ tests/unit/out/unsafe/void_cast.rs | 15 ++++ 31 files changed, 464 insertions(+), 35 deletions(-) diff --git a/cpp2rust/converter/converter.cpp b/cpp2rust/converter/converter.cpp index aae2337f..3f63da57 100644 --- a/cpp2rust/converter/converter.cpp +++ b/cpp2rust/converter/converter.cpp @@ -949,7 +949,7 @@ bool Converter::VisitCXXRecordDecl(clang::CXXRecordDecl *decl) { if (!record_decls_.MarkDefined(GetRecordName(decl))) { // Other translation units may instantiate members this one did not. - if (clang::isa(decl)) { + if (!decl->isAbstract()) { ConvertLateInstantiatedMethods(decl); } return false; @@ -960,25 +960,7 @@ bool Converter::VisitCXXRecordDecl(clang::CXXRecordDecl *decl) { return false; } - sema_->ForceDeclarationOfImplicitMembers(decl); - for (auto ctor : decl->ctors()) { - if (ctor->isCopyConstructor() && ctor->isImplicit() && - !ctor->doesThisDeclarationHaveABody() && !ctor->isDeleted()) { - sema_->DefineImplicitCopyConstructor(decl->getLocation(), ctor); - } - } - for (auto *method : decl->methods()) { - if (IsComparisonOperator(method) && method->isDefaulted() && - !method->doesThisDeclarationHaveABody()) { -#if CLANG_VERSION_MAJOR >= 24 - auto kind = method->getDefaultedComparisonKind(); -#else - auto kind = sema_->getDefaultedComparisonKind(method); -#endif - sema_->DefineDefaultedComparison(decl->getLocation(), method, kind); - } - } - + DefineImplicitMembers(decl); EmitRustStructOrUnion(decl); } else if (decl->isUnion()) { if (!record_decls_.MarkDefined(GetRecordName(decl))) { @@ -993,6 +975,44 @@ bool Converter::VisitCXXRecordDecl(clang::CXXRecordDecl *decl) { return false; } +void Converter::DefineImplicitMembers(clang::CXXRecordDecl *decl) { + clang::Scope tu_scope(nullptr, clang::Scope::DeclScope, + sema_->getDiagnostics()); + tu_scope.setEntity(ctx_.getTranslationUnitDecl()); + auto *saved_tu_scope = std::exchange(sema_->TUScope, &tu_scope); + sema_->ForceDeclarationOfImplicitMembers(decl); + for (auto ctor : decl->ctors()) { + if (ctor->isCopyConstructor() && ctor->isImplicit() && + !ctor->doesThisDeclarationHaveABody() && !ctor->isDeleted()) { + sema_->DefineImplicitCopyConstructor(decl->getLocation(), ctor); + } + if (ctor->isMoveConstructor() && !ctor->isUserProvided() && + !ctor->doesThisDeclarationHaveABody() && !ctor->isDeleted() && + !HasDefaultedCopyConstructor(decl)) { + sema_->DefineImplicitMoveConstructor(decl->getLocation(), ctor); + } + } + for (auto *method : decl->methods()) { + if (method->isMoveAssignmentOperator() && !method->isUserProvided() && + !method->doesThisDeclarationHaveABody() && !method->isDeleted() && + !HasDefaultedCopyAssignment(decl)) { + sema_->DefineImplicitMoveAssignment(decl->getLocation(), method); + } + } + for (auto *method : decl->methods()) { + if (IsComparisonOperator(method) && method->isDefaulted() && + !method->doesThisDeclarationHaveABody()) { +#if CLANG_VERSION_MAJOR >= 24 + auto kind = method->getDefaultedComparisonKind(); +#else + auto kind = sema_->getDefaultedComparisonKind(method); +#endif + sema_->DefineDefaultedComparison(decl->getLocation(), method, kind); + } + } + sema_->TUScope = saved_tu_scope; +} + bool Converter::VisitCXXMethodDecl(clang::CXXMethodDecl *decl) { decl->dump(log()); if (!ShouldConvertMethod(decl)) { @@ -1080,7 +1100,8 @@ std::string Converter::GetCtorName(clang::CXXConstructorDecl *decl) { } bool Converter::VisitCXXConstructorDecl(clang::CXXConstructorDecl *decl) { - if (decl->isOutOfLine() || decl->isImplicit()) { + if (decl->isOutOfLine() || + (decl->isImplicit() && !IsConvertibleImplicitMember(decl))) { return false; } PushCurrFunction push_fn(*this, decl); @@ -3147,6 +3168,26 @@ bool Converter::VisitCXXThisExpr(clang::CXXThisExpr *expr) { return false; } +bool Converter::VisitOpaqueValueExpr(clang::OpaqueValueExpr *expr) { + Convert(expr->getSourceExpr()); + return false; +} + +bool Converter::VisitArrayInitIndexExpr(clang::ArrayInitIndexExpr *expr) { + StrCat("__i"); + return false; +} + +bool Converter::VisitArrayInitLoopExpr(clang::ArrayInitLoopExpr *expr) { + StrCat(std::format("std::array::from_fn::<_, {}, _>", + GetArraySize(expr->getType()))); + PushParen paren(*this); + StrCat("|__i: usize|"); + ConvertVarInit(expr->getSubExpr()->getType(), expr->getSubExpr()); + computed_expr_type_ = ComputedExprType::FreshValue; + return false; +} + bool Converter::VisitInitListExpr(clang::InitListExpr *expr) { if (auto form = expr->getSemanticForm()) expr = form; @@ -3843,6 +3884,10 @@ std::string Converter::ConvertVarDefaultInit(clang::QualType qual_type) { std::string Converter::GetOverloadedFunctionName(const clang::FunctionDecl *decl) { auto name = GetFunctionBaseName(decl); + if (auto *ctor = clang::dyn_cast(decl); + ctor && !ctor->getParent()->getIdentifier()) { + name = GetRecordName(ctor->getParent()); + } if (decl->getNumParams() != 0U) { name += '_'; diff --git a/cpp2rust/converter/converter.h b/cpp2rust/converter/converter.h index a5117fc7..cc4241f5 100644 --- a/cpp2rust/converter/converter.h +++ b/cpp2rust/converter/converter.h @@ -331,6 +331,8 @@ class Converter : public clang::RecursiveASTVisitor { virtual void ConvertVariadicArg(clang::Expr *arg); + void DefineImplicitMembers(clang::CXXRecordDecl *decl); + virtual bool VisitCallExpr(clang::CallExpr *expr); virtual bool VisitIntegerLiteral(clang::IntegerLiteral *expr); @@ -384,6 +386,9 @@ class Converter : public clang::RecursiveASTVisitor { virtual bool VisitCXXThisExpr(clang::CXXThisExpr *expr); virtual bool VisitInitListExpr(clang::InitListExpr *expr); + bool VisitOpaqueValueExpr(clang::OpaqueValueExpr *expr); + bool VisitArrayInitIndexExpr(clang::ArrayInitIndexExpr *expr); + virtual bool VisitArrayInitLoopExpr(clang::ArrayInitLoopExpr *expr); virtual bool VisitCompoundLiteralExpr(clang::CompoundLiteralExpr *expr); diff --git a/cpp2rust/converter/converter_lib.cpp b/cpp2rust/converter/converter_lib.cpp index 7a031734..44709984 100644 --- a/cpp2rust/converter/converter_lib.cpp +++ b/cpp2rust/converter/converter_lib.cpp @@ -296,6 +296,35 @@ bool IsUserDefinedCopyOrMoveConstructor(const clang::CXXConstructorDecl *ctor) { IsUserDefinedMoveConstructor(ctor); } +static bool +IsConvertibleImplicitMemberParent(const clang::CXXRecordDecl *decl) { + return IsUserDefinedDecl(decl) && !decl->isAbstract() && + decl->getNumBases() == 0; +} + +bool IsConvertibleMoveConstructor(const clang::CXXConstructorDecl *ctor) { + return ctor->isMoveConstructor() && !ctor->isDeleted() && + IsConvertibleImplicitMemberParent(ctor->getParent()) && + (ctor->isUserProvided() || + !HasDefaultedCopyConstructor(ctor->getParent())) && + ctor->hasBody(); +} + +bool IsConvertibleMoveAssignment(const clang::CXXMethodDecl *method) { + return method->isMoveAssignmentOperator() && !method->isDeleted() && + IsConvertibleImplicitMemberParent(method->getParent()) && + (method->isUserProvided() || + !HasDefaultedCopyAssignment(method->getParent())) && + method->hasBody(); +} + +bool IsConvertibleImplicitMember(const clang::CXXMethodDecl *method) { + if (auto *ctor = clang::dyn_cast(method)) { + return IsConvertibleMoveConstructor(ctor); + } + return IsConvertibleMoveAssignment(method); +} + bool IsDefaultedMoveConstructor(const clang::CXXConstructorDecl *ctor) { return ctor->isMoveConstructor() && !ctor->isUserProvided() && IsUserDefinedDecl(ctor->getParent()); @@ -328,6 +357,19 @@ bool HasDefaultedCopyConstructor(const clang::RecordDecl *decl) { return !cxx->defaultedCopyConstructorIsDeleted(); } +bool HasDefaultedCopyAssignment(const clang::RecordDecl *decl) { + auto *cxx = clang::dyn_cast(decl); + if (!cxx) { + return true; + } + for (const auto *method : cxx->methods()) { + if (method->isCopyAssignmentOperator()) { + return !method->isUserProvided() && !method->isDeleted(); + } + } + return true; +} + bool HasCallableCopyConstructor(const clang::RecordDecl *decl) { auto *cxx = clang::dyn_cast(decl); if (!cxx) { @@ -375,7 +417,8 @@ bool IsConvertibleCXXMethodDecl(const clang::CXXMethodDecl *decl) { if (llvm::isa(decl)) { return GetUserDefinedDestructor(decl->getParent()) != nullptr; } - return !decl->isImplicit() || IsComparisonOperator(decl); + return !decl->isImplicit() || IsComparisonOperator(decl) || + IsConvertibleImplicitMember(decl); } bool IsConvertibleFunctionDecl(const clang::FunctionDecl *decl) { @@ -904,6 +947,9 @@ bool IsEmittableMethod(clang::CXXMethodDecl *method) { if (IsComparisonOperator(method)) { return method->hasBody(); } + if (IsConvertibleImplicitMember(method)) { + return method->hasBody(); + } // Compiler-generated members are covered by derived traits if (method->isImplicit()) { return false; @@ -921,6 +967,9 @@ bool IsMethodOnPtr(const clang::CXXMethodDecl *method) { clang::isa(method)) { return false; } + if (IsConvertibleImplicitMember(method)) { + return method->hasBody(); + } if (method->isImplicit() && !IsComparisonOperator(method)) { return false; } diff --git a/cpp2rust/converter/converter_lib.h b/cpp2rust/converter/converter_lib.h index df20998c..4246a471 100644 --- a/cpp2rust/converter/converter_lib.h +++ b/cpp2rust/converter/converter_lib.h @@ -76,6 +76,12 @@ bool IsUserDefinedCopyOrMoveConstructor(const clang::CXXConstructorDecl *ctor); bool IsDefaultedMoveConstructor(const clang::CXXConstructorDecl *ctor); +bool IsConvertibleMoveConstructor(const clang::CXXConstructorDecl *ctor); + +bool IsConvertibleMoveAssignment(const clang::CXXMethodDecl *method); + +bool IsConvertibleImplicitMember(const clang::CXXMethodDecl *method); + clang::CXXConstructorDecl * GetUserDefinedCopyConstructor(const clang::RecordDecl *decl); @@ -83,6 +89,8 @@ bool HasCallableCopyConstructor(const clang::RecordDecl *decl); bool HasDefaultedCopyConstructor(const clang::RecordDecl *decl); +bool HasDefaultedCopyAssignment(const clang::RecordDecl *decl); + bool IsRValueConvertingConstructor(const clang::CXXConstructorDecl *ctor); bool IsPassThroughConstructor(const clang::CXXConstructorDecl *ctor); diff --git a/cpp2rust/converter/models/converter_refcount.cpp b/cpp2rust/converter/models/converter_refcount.cpp index 4c10e375..39828e87 100644 --- a/cpp2rust/converter/models/converter_refcount.cpp +++ b/cpp2rust/converter/models/converter_refcount.cpp @@ -1853,6 +1853,13 @@ bool ConverterRefCount::VisitCXXForRangeStmtString( return false; } +bool ConverterRefCount::VisitArrayInitLoopExpr(clang::ArrayInitLoopExpr *expr) { + StrCat("Box::new"); + PushParen outer(*this); + PushConversionKind push(*this, ConversionKind::Unboxed); + return Converter::VisitArrayInitLoopExpr(expr); +} + void ConverterRefCount::ConvertArrayCXXConstructExpr( clang::CXXConstructExpr *expr) { StrCat("Box::new"); diff --git a/cpp2rust/converter/models/converter_refcount.h b/cpp2rust/converter/models/converter_refcount.h index 818e06c9..35748f58 100644 --- a/cpp2rust/converter/models/converter_refcount.h +++ b/cpp2rust/converter/models/converter_refcount.h @@ -123,6 +123,7 @@ class ConverterRefCount final : public Converter { void EmitStmtExprTail(clang::Expr *tail) override; bool VisitInitListExpr(clang::InitListExpr *expr) override; + bool VisitArrayInitLoopExpr(clang::ArrayInitLoopExpr *expr) override; bool VisitArraySubscriptExpr(clang::ArraySubscriptExpr *expr) override; diff --git a/tests/unit/out/refcount/clone_vs_move.rs b/tests/unit/out/refcount/clone_vs_move.rs index 2b8c7c40..616904e3 100644 --- a/tests/unit/out/refcount/clone_vs_move.rs +++ b/tests/unit/out/refcount/clone_vs_move.rs @@ -46,7 +46,9 @@ impl Clone for Foo { x: Rc::new(RefCell::new((*self.x.borrow()))), y: (self.y).clone(), z: Rc::new(RefCell::new((*self.z.borrow()).clone())), - a: Rc::new(RefCell::new((*self.a.borrow()).clone())), + a: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 3, _>( + |__i: usize| (*self.a.borrow())[(__i) as usize], + )))), bar: Rc::new(RefCell::new((*self.bar.borrow()).clone())), })); let this: Ptr = __this.as_pointer(); diff --git a/tests/unit/out/refcount/copy_assign.rs b/tests/unit/out/refcount/copy_assign.rs index 1eec39b5..5708fcb5 100644 --- a/tests/unit/out/refcount/copy_assign.rs +++ b/tests/unit/out/refcount/copy_assign.rs @@ -149,7 +149,13 @@ impl Clone for Holder { p: Rc::new(RefCell::new(Partial::Partial_pconstPartial({ self.p.as_pointer() }))), - arr: Rc::new(RefCell::new((*self.arr.borrow()).clone())), + arr: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 2, _>( + |__i: usize| { + Partial::Partial_pconstPartial({ + (self.arr.as_pointer() as Ptr).offset(__i) + }) + }, + )))), })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() diff --git a/tests/unit/out/refcount/copy_ctor.rs b/tests/unit/out/refcount/copy_ctor.rs index bab142e2..afd3641b 100644 --- a/tests/unit/out/refcount/copy_ctor.rs +++ b/tests/unit/out/refcount/copy_ctor.rs @@ -114,7 +114,13 @@ impl Clone for Holder { c: Rc::new(RefCell::new(Counted::Counted_pconstCounted({ self.c.as_pointer() }))), - arr: Rc::new(RefCell::new((*self.arr.borrow()).clone())), + arr: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 2, _>( + |__i: usize| { + Counted::Counted_pconstCounted({ + (self.arr.as_pointer() as Ptr).offset(__i) + }) + }, + )))), })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() diff --git a/tests/unit/out/refcount/default.rs b/tests/unit/out/refcount/default.rs index 0ebec44f..7e265c68 100644 --- a/tests/unit/out/refcount/default.rs +++ b/tests/unit/out/refcount/default.rs @@ -19,8 +19,12 @@ impl Clone for Pointers { let __this: Value = Rc::new(RefCell::new(Self { x1: Rc::new(RefCell::new((*self.x1.borrow()).clone())), x2: Rc::new(RefCell::new((*self.x2.borrow()).clone())), - x3: Rc::new(RefCell::new((*self.x3.borrow()).clone())), - x4: Rc::new(RefCell::new((*self.x4.borrow()).clone())), + x3: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 5, _>( + |__i: usize| ((*self.x3.borrow())[(__i) as usize]).clone(), + )))), + x4: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 10, _>( + |__i: usize| ((*self.x4.borrow())[(__i) as usize]).clone(), + )))), x5: Rc::new(RefCell::new((*self.x5.borrow()))), })); let this: Ptr = __this.as_pointer(); diff --git a/tests/unit/out/refcount/default_in_statics.rs b/tests/unit/out/refcount/default_in_statics.rs index 0302d002..0bf04450 100644 --- a/tests/unit/out/refcount/default_in_statics.rs +++ b/tests/unit/out/refcount/default_in_statics.rs @@ -52,7 +52,9 @@ impl Clone for Outer { let __this: Value = Rc::new(RefCell::new(Self { p1: Rc::new(RefCell::new((*self.p1.borrow()).clone())), p2: Rc::new(RefCell::new((*self.p2.borrow()).clone())), - arr: Rc::new(RefCell::new((*self.arr.borrow()).clone())), + arr: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 3, _>( + |__i: usize| ((*self.arr.borrow())[(__i) as usize]).clone(), + )))), cp: Rc::new(RefCell::new((*self.cp.borrow()).clone())), pp: Rc::new(RefCell::new((*self.pp.borrow()).clone())), inner: Rc::new(RefCell::new((*self.inner.borrow()).clone())), diff --git a/tests/unit/out/refcount/destructor.rs b/tests/unit/out/refcount/destructor.rs index 4e10438d..71dff440 100644 --- a/tests/unit/out/refcount/destructor.rs +++ b/tests/unit/out/refcount/destructor.rs @@ -112,7 +112,9 @@ pub struct ArrayMember { impl Clone for ArrayMember { fn clone(&self) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - items: Rc::new(RefCell::new((*self.items.borrow()).clone())), + items: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 3, _>( + |__i: usize| ((*self.items.borrow())[(__i) as usize]).clone(), + )))), })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() diff --git a/tests/unit/out/refcount/huffman.rs b/tests/unit/out/refcount/huffman.rs index 0b622cb5..eef8af57 100644 --- a/tests/unit/out/refcount/huffman.rs +++ b/tests/unit/out/refcount/huffman.rs @@ -82,6 +82,23 @@ pub struct MinHeap { pub next: Value, pub alloc: Value>>>, } +impl MinHeap { + pub fn MinHeap_pmutMinHeap(_a0: Ptr) -> Self { + let __this: Value = Rc::new(RefCell::new(Self { + size: Rc::new(RefCell::new((*(*_a0.upgrade().deref()).size.borrow()))), + capacity: Rc::new(RefCell::new((*(*_a0.upgrade().deref()).capacity.borrow()))), + arr: Rc::new(RefCell::new( + (*(*_a0.upgrade().deref()).arr.borrow_mut()).take(), + )), + next: Rc::new(RefCell::new((*(*_a0.upgrade().deref()).next.borrow()))), + alloc: Rc::new(RefCell::new( + (*(*_a0.upgrade().deref()).alloc.borrow_mut()).take(), + )), + })); + let this: Ptr = __this.as_pointer(); + Rc::try_unwrap(__this).ok().unwrap().into_inner() + } +} impl ByteRepr for MinHeap { fn byte_size() -> usize { 32 @@ -335,6 +352,7 @@ pub trait MinHeapImpl { freq: Ptr>>>, n: i32, ); + fn operator_assign_pmutMinHeap(&self, _a0: Ptr) -> Ptr; } impl MinHeapImpl for Ptr { fn Alloc(&self, data: u8, freq: i32) -> Ptr { @@ -506,6 +524,20 @@ impl MinHeapImpl for Ptr { (*i.borrow_mut()).prefix_dec(); } } + fn operator_assign_pmutMinHeap(&self, _a0: Ptr) -> Ptr { + let __rhs = (*(*_a0.upgrade().deref()).size.borrow()); + (*(*(*self).upgrade().deref()).size.borrow_mut()) = __rhs; + let __rhs = (*(*_a0.upgrade().deref()).capacity.borrow()); + (*(*(*self).upgrade().deref()).capacity.borrow_mut()) = __rhs; + ((*(*self).upgrade().deref()).arr.as_pointer() + as Ptr]>>>>) + .write((*(*_a0.upgrade().deref()).arr.borrow_mut()).take()); + let __rhs = (*(*_a0.upgrade().deref()).next.borrow()); + (*(*(*self).upgrade().deref()).next.borrow_mut()) = __rhs; + ((*(*self).upgrade().deref()).alloc.as_pointer() as Ptr>>>) + .write((*(*_a0.upgrade().deref()).alloc.borrow_mut()).take()); + return (*self).clone(); + } } pub trait MinHeapNodeImpl { fn IsLeaf(&self) -> bool; diff --git a/tests/unit/out/refcount/kruskal.rs b/tests/unit/out/refcount/kruskal.rs index de5b50f3..036e0fea 100644 --- a/tests/unit/out/refcount/kruskal.rs +++ b/tests/unit/out/refcount/kruskal.rs @@ -221,6 +221,21 @@ pub struct DisjointSet { pub parent: Value>>>, pub n: Value, } +impl DisjointSet { + pub fn DisjointSet_pmutDisjointSet(_a0: Ptr) -> Self { + let __this: Value = Rc::new(RefCell::new(Self { + rank: Rc::new(RefCell::new( + (*(*_a0.upgrade().deref()).rank.borrow_mut()).take(), + )), + parent: Rc::new(RefCell::new( + (*(*_a0.upgrade().deref()).parent.borrow_mut()).take(), + )), + n: Rc::new(RefCell::new((*(*_a0.upgrade().deref()).n.borrow()))), + })); + let this: Ptr = __this.as_pointer(); + Rc::try_unwrap(__this).ok().unwrap().into_inner() + } +} impl ByteRepr for DisjointSet { fn byte_size() -> usize { 24 @@ -248,6 +263,19 @@ pub struct Graph { pub V: Value, pub E: Value, } +impl Graph { + pub fn Graph_pmutGraph(_a0: Ptr) -> Self { + let __this: Value = Rc::new(RefCell::new(Self { + edges: Rc::new(RefCell::new( + (*(*_a0.upgrade().deref()).edges.borrow_mut()).take(), + )), + V: Rc::new(RefCell::new((*(*_a0.upgrade().deref()).V.borrow()))), + E: Rc::new(RefCell::new((*(*_a0.upgrade().deref()).E.borrow()))), + })); + let this: Ptr = __this.as_pointer(); + Rc::try_unwrap(__this).ok().unwrap().into_inner() + } +} impl ByteRepr for Graph { fn byte_size() -> usize { 16 @@ -390,6 +418,7 @@ pub trait DisjointSetImpl { fn makeSet(&self); fn find(&self, x: i32) -> i32; fn merge(&self, x: i32, y: i32); + fn operator_assign_pmutDisjointSet(&self, _a0: Ptr) -> Ptr; } impl DisjointSetImpl for Ptr { fn makeSet(&self) { @@ -486,4 +515,27 @@ impl DisjointSetImpl for Ptr { .borrow_mut()[((*xset.borrow()) as usize) as usize] = __rhs; } } + fn operator_assign_pmutDisjointSet(&self, _a0: Ptr) -> Ptr { + ((*(*self).upgrade().deref()).rank.as_pointer() as Ptr>>>) + .write((*(*_a0.upgrade().deref()).rank.borrow_mut()).take()); + ((*(*self).upgrade().deref()).parent.as_pointer() as Ptr>>>) + .write((*(*_a0.upgrade().deref()).parent.borrow_mut()).take()); + let __rhs = (*(*_a0.upgrade().deref()).n.borrow()); + (*(*(*self).upgrade().deref()).n.borrow_mut()) = __rhs; + return (*self).clone(); + } +} +pub trait GraphImpl { + fn operator_assign_pmutGraph(&self, _a0: Ptr) -> Ptr; +} +impl GraphImpl for Ptr { + fn operator_assign_pmutGraph(&self, _a0: Ptr) -> Ptr { + ((*(*self).upgrade().deref()).edges.as_pointer() as Ptr>>>) + .write((*(*_a0.upgrade().deref()).edges.borrow_mut()).take()); + let __rhs = (*(*_a0.upgrade().deref()).V.borrow()); + (*(*(*self).upgrade().deref()).V.borrow_mut()) = __rhs; + let __rhs = (*(*_a0.upgrade().deref()).E.borrow()); + (*(*(*self).upgrade().deref()).E.borrow_mut()) = __rhs; + return (*self).clone(); + } } diff --git a/tests/unit/out/refcount/offsetof.rs b/tests/unit/out/refcount/offsetof.rs index 7d9e0f58..da61178f 100644 --- a/tests/unit/out/refcount/offsetof.rs +++ b/tests/unit/out/refcount/offsetof.rs @@ -49,7 +49,9 @@ impl Clone for Frame { fn clone(&self) -> Self { let __this: Value = Rc::new(RefCell::new(Self { tag: Rc::new(RefCell::new((*self.tag.borrow()))), - body: Rc::new(RefCell::new((*self.body.borrow()).clone())), + body: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 64, _>( + |__i: usize| (*self.body.borrow())[(__i) as usize], + )))), })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() diff --git a/tests/unit/out/refcount/operator_comparison_noncopyable.rs b/tests/unit/out/refcount/operator_comparison_noncopyable.rs index fd5f23ca..71d91067 100644 --- a/tests/unit/out/refcount/operator_comparison_noncopyable.rs +++ b/tests/unit/out/refcount/operator_comparison_noncopyable.rs @@ -31,6 +31,13 @@ impl S { let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } + pub fn S_pmutS(_a0: Ptr) -> Self { + let __this: Value = Rc::new(RefCell::new(Self { + data_: Rc::new(RefCell::new((*(*_a0.upgrade().deref()).data_.borrow()))), + })); + let this: Ptr = __this.as_pointer(); + Rc::try_unwrap(__this).ok().unwrap().into_inner() + } } impl std::cmp::Ord for S { fn cmp(&self, other: &Self) -> std::cmp::Ordering { diff --git a/tests/unit/out/refcount/operator_member_pointer_free.rs b/tests/unit/out/refcount/operator_member_pointer_free.rs index 0fb3dad5..2fd79df3 100644 --- a/tests/unit/out/refcount/operator_member_pointer_free.rs +++ b/tests/unit/out/refcount/operator_member_pointer_free.rs @@ -40,7 +40,9 @@ pub struct S { impl Clone for S { fn clone(&self) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - data: Rc::new(RefCell::new((*self.data.borrow()).clone())), + data: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 3, _>( + |__i: usize| (*self.data.borrow())[(__i) as usize], + )))), inner: Rc::new(RefCell::new((*self.inner.borrow()).clone())), })); let this: Ptr = __this.as_pointer(); diff --git a/tests/unit/out/refcount/operator_member_pointer_member.rs b/tests/unit/out/refcount/operator_member_pointer_member.rs index 8d852312..c736517e 100644 --- a/tests/unit/out/refcount/operator_member_pointer_member.rs +++ b/tests/unit/out/refcount/operator_member_pointer_member.rs @@ -68,7 +68,9 @@ pub struct S { impl Clone for S { fn clone(&self) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - data: Rc::new(RefCell::new((*self.data.borrow()).clone())), + data: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 3, _>( + |__i: usize| (*self.data.borrow())[(__i) as usize], + )))), inner: Rc::new(RefCell::new((*self.inner.borrow()).clone())), })); let this: Ptr = __this.as_pointer(); diff --git a/tests/unit/out/refcount/pointer_array.rs b/tests/unit/out/refcount/pointer_array.rs index 2fd7520f..e3832661 100644 --- a/tests/unit/out/refcount/pointer_array.rs +++ b/tests/unit/out/refcount/pointer_array.rs @@ -13,7 +13,9 @@ pub struct StackArray { impl Clone for StackArray { fn clone(&self) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - arr: Rc::new(RefCell::new((*self.arr.borrow()).clone())), + arr: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 3, _>( + |__i: usize| ((*self.arr.borrow())[(__i) as usize]).clone(), + )))), })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() diff --git a/tests/unit/out/refcount/random.rs b/tests/unit/out/refcount/random.rs index 3e857cbc..ceb93973 100644 --- a/tests/unit/out/refcount/random.rs +++ b/tests/unit/out/refcount/random.rs @@ -21,11 +21,15 @@ impl Clone for Pair { let __this: Value = Rc::new(RefCell::new(Self { x: Rc::new(RefCell::new((*self.x.borrow()))), y: Rc::new(RefCell::new((*self.y.borrow()))), - a: Rc::new(RefCell::new((*self.a.borrow()).clone())), + a: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 5, _>( + |__i: usize| (*self.a.borrow())[(__i) as usize], + )))), r: (self.r).clone(), p: Rc::new(RefCell::new((*self.p.borrow()).clone())), pair: Rc::new(RefCell::new((*self.pair.borrow()).clone())), - ap: Rc::new(RefCell::new((*self.ap.borrow()).clone())), + ap: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 2, _>( + |__i: usize| ((*self.ap.borrow())[(__i) as usize]).clone(), + )))), })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() diff --git a/tests/unit/out/refcount/unique_ptr.rs b/tests/unit/out/refcount/unique_ptr.rs index 057d2497..b491bf44 100644 --- a/tests/unit/out/refcount/unique_ptr.rs +++ b/tests/unit/out/refcount/unique_ptr.rs @@ -10,6 +10,17 @@ use std::rc::{Rc, Weak}; pub struct SafePointer { pub ptr: Value>>, } +impl SafePointer { + pub fn SafePointer_pmutSafePointer(_a0: Ptr) -> Self { + let __this: Value = Rc::new(RefCell::new(Self { + ptr: Rc::new(RefCell::new( + (*(*_a0.upgrade().deref()).ptr.borrow_mut()).take(), + )), + })); + let this: Ptr = __this.as_pointer(); + Rc::try_unwrap(__this).ok().unwrap().into_inner() + } +} impl ByteRepr for SafePointer { fn byte_size() -> usize { 8 @@ -305,6 +316,7 @@ impl PairImpl for Ptr { } pub trait SafePointerImpl { fn inc(&self); + fn operator_assign_pmutSafePointer(&self, _a0: Ptr) -> Ptr; } impl SafePointerImpl for Ptr { fn inc(&self) { @@ -314,4 +326,9 @@ impl SafePointerImpl for Ptr { .borrow_mut()) .prefix_inc(); } + fn operator_assign_pmutSafePointer(&self, _a0: Ptr) -> Ptr { + ((*(*self).upgrade().deref()).ptr.as_pointer() as Ptr>>) + .write((*(*_a0.upgrade().deref()).ptr.borrow_mut()).take()); + return (*self).clone(); + } } diff --git a/tests/unit/out/refcount/unique_ptr_const_deref.rs b/tests/unit/out/refcount/unique_ptr_const_deref.rs index 299f6780..7aa75e9e 100644 --- a/tests/unit/out/refcount/unique_ptr_const_deref.rs +++ b/tests/unit/out/refcount/unique_ptr_const_deref.rs @@ -10,6 +10,17 @@ use std::rc::{Rc, Weak}; pub struct Holder { pub val: Value>>, } +impl Holder { + pub fn Holder_pmutHolder(_a0: Ptr) -> Self { + let __this: Value = Rc::new(RefCell::new(Self { + val: Rc::new(RefCell::new( + (*(*_a0.upgrade().deref()).val.borrow_mut()).take(), + )), + })); + let this: Ptr = __this.as_pointer(); + Rc::try_unwrap(__this).ok().unwrap().into_inner() + } +} impl ByteRepr for Holder { fn byte_size() -> usize { 8 @@ -49,3 +60,13 @@ fn main_0() -> i32 { assert!((({ read_val_0((h.as_pointer()),) }) == 42)); return 0; } +pub trait HolderImpl { + fn operator_assign_pmutHolder(&self, _a0: Ptr) -> Ptr; +} +impl HolderImpl for Ptr { + fn operator_assign_pmutHolder(&self, _a0: Ptr) -> Ptr { + ((*(*self).upgrade().deref()).val.as_pointer() as Ptr>>) + .write((*(*_a0.upgrade().deref()).val.borrow_mut()).take()); + return (*self).clone(); + } +} diff --git a/tests/unit/out/refcount/unique_ptr_nested.rs b/tests/unit/out/refcount/unique_ptr_nested.rs index 7f5be219..729267fa 100644 --- a/tests/unit/out/refcount/unique_ptr_nested.rs +++ b/tests/unit/out/refcount/unique_ptr_nested.rs @@ -40,6 +40,17 @@ impl ByteRepr for Inner { pub struct Outer { pub inner: Value>>, } +impl Outer { + pub fn Outer_pmutOuter(_a0: Ptr) -> Self { + let __this: Value = Rc::new(RefCell::new(Self { + inner: Rc::new(RefCell::new( + (*(*_a0.upgrade().deref()).inner.borrow_mut()).take(), + )), + })); + let this: Ptr = __this.as_pointer(); + Rc::try_unwrap(__this).ok().unwrap().into_inner() + } +} impl ByteRepr for Outer { fn byte_size() -> usize { 8 @@ -91,3 +102,13 @@ fn main_0() -> i32 { assert!((((*sum.borrow()) + (*(*b.borrow()).as_ref().unwrap().borrow())) == 135)); return 0; } +pub trait OuterImpl { + fn operator_assign_pmutOuter(&self, _a0: Ptr) -> Ptr; +} +impl OuterImpl for Ptr { + fn operator_assign_pmutOuter(&self, _a0: Ptr) -> Ptr { + ((*(*self).upgrade().deref()).inner.as_pointer() as Ptr>>) + .write((*(*_a0.upgrade().deref()).inner.borrow_mut()).take()); + return (*self).clone(); + } +} diff --git a/tests/unit/out/refcount/void_cast.rs b/tests/unit/out/refcount/void_cast.rs index 2c50822b..6778ff76 100644 --- a/tests/unit/out/refcount/void_cast.rs +++ b/tests/unit/out/refcount/void_cast.rs @@ -80,6 +80,17 @@ impl ByteRepr for Holder { pub struct NonCopyable { pub value: Value>>, } +impl NonCopyable { + pub fn NonCopyable_pmutNonCopyable(_a0: Ptr) -> Self { + let __this: Value = Rc::new(RefCell::new(Self { + value: Rc::new(RefCell::new( + (*(*_a0.upgrade().deref()).value.borrow_mut()).take(), + )), + })); + let this: Ptr = __this.as_pointer(); + Rc::try_unwrap(__this).ok().unwrap().into_inner() + } +} impl ByteRepr for NonCopyable { fn byte_size() -> usize { 8 @@ -168,3 +179,13 @@ fn main_0() -> i32 { assert!(((*(*(*g.borrow()).value.borrow()).as_ref().unwrap().borrow()) == 9)); return 0; } +pub trait NonCopyableImpl { + fn operator_assign_pmutNonCopyable(&self, _a0: Ptr) -> Ptr; +} +impl NonCopyableImpl for Ptr { + fn operator_assign_pmutNonCopyable(&self, _a0: Ptr) -> Ptr { + ((*(*self).upgrade().deref()).value.as_pointer() as Ptr>>) + .write((*(*_a0.upgrade().deref()).value.borrow_mut()).take()); + return (*self).clone(); + } +} diff --git a/tests/unit/out/unsafe/huffman.rs b/tests/unit/out/unsafe/huffman.rs index ea15aba8..25cbbe1e 100644 --- a/tests/unit/out/unsafe/huffman.rs +++ b/tests/unit/out/unsafe/huffman.rs @@ -129,6 +129,24 @@ impl MinHeap { i.prefix_dec(); } } + pub unsafe fn MinHeap_pmutMinHeap(_a0: *mut MinHeap) -> Self { + let mut this = Self { + size: (*_a0).size, + capacity: (*_a0).capacity, + arr: (*_a0).arr.take(), + next: (*_a0).next, + alloc: (*_a0).alloc.take(), + }; + this + } + pub unsafe fn operator_assign_pmutMinHeap(&mut self, _a0: *mut MinHeap) -> *mut MinHeap { + self.size = (*_a0).size; + self.capacity = (*_a0).capacity; + self.arr = (*_a0).arr.take(); + self.next = (*_a0).next; + self.alloc = (*_a0).alloc.take(); + return &mut (*(self as *mut MinHeap)) as *mut MinHeap; + } } pub unsafe fn AllocMinHeap_1(mut capacity: i32) -> Option> { let mut minHeap: Option> = Some(Box::new(MinHeap { diff --git a/tests/unit/out/unsafe/kruskal.rs b/tests/unit/out/unsafe/kruskal.rs index cb364530..b78c5b30 100644 --- a/tests/unit/out/unsafe/kruskal.rs +++ b/tests/unit/out/unsafe/kruskal.rs @@ -138,6 +138,23 @@ impl DisjointSet { ((self.rank.as_mut().unwrap()[(xset as usize)]) + (1)); } } + pub unsafe fn DisjointSet_pmutDisjointSet(_a0: *mut DisjointSet) -> Self { + let mut this = Self { + rank: (*_a0).rank.take(), + parent: (*_a0).parent.take(), + n: (*_a0).n, + }; + this + } + pub unsafe fn operator_assign_pmutDisjointSet( + &mut self, + _a0: *mut DisjointSet, + ) -> *mut DisjointSet { + self.rank = (*_a0).rank.take(); + self.parent = (*_a0).parent.take(); + self.n = (*_a0).n; + return &mut (*(self as *mut DisjointSet)) as *mut DisjointSet; + } } #[repr(C)] #[derive(Default)] @@ -146,6 +163,22 @@ pub struct Graph { pub V: i32, pub E: i32, } +impl Graph { + pub unsafe fn Graph_pmutGraph(_a0: *mut Graph) -> Self { + let mut this = Self { + edges: (*_a0).edges.take(), + V: (*_a0).V, + E: (*_a0).E, + }; + this + } + pub unsafe fn operator_assign_pmutGraph(&mut self, _a0: *mut Graph) -> *mut Graph { + self.edges = (*_a0).edges.take(); + self.V = (*_a0).V; + self.E = (*_a0).E; + return &mut (*(self as *mut Graph)) as *mut Graph; + } +} pub unsafe fn MSTKruskal_2(graph: *mut Graph) -> f64 { (unsafe { let _arr: *mut Option> = &mut (*graph).edges as *mut Option>; diff --git a/tests/unit/out/unsafe/operator_comparison_noncopyable.rs b/tests/unit/out/unsafe/operator_comparison_noncopyable.rs index 6fcf71c0..04618061 100644 --- a/tests/unit/out/unsafe/operator_comparison_noncopyable.rs +++ b/tests/unit/out/unsafe/operator_comparison_noncopyable.rs @@ -22,6 +22,12 @@ impl S { let mut this = Self { data_: data }; this } + pub unsafe fn S_pmutS(_a0: *mut S) -> Self { + let mut this = Self { + data_: (*_a0).data_, + }; + this + } } impl std::cmp::Ord for S { fn cmp(&self, other: &Self) -> std::cmp::Ordering { diff --git a/tests/unit/out/unsafe/unique_ptr.rs b/tests/unit/out/unsafe/unique_ptr.rs index 3c79812b..49990ba4 100644 --- a/tests/unit/out/unsafe/unique_ptr.rs +++ b/tests/unit/out/unsafe/unique_ptr.rs @@ -15,6 +15,19 @@ impl SafePointer { pub unsafe fn inc(&mut self) { (*self.ptr.as_deref_mut().unwrap()).prefix_inc(); } + pub unsafe fn SafePointer_pmutSafePointer(_a0: *mut SafePointer) -> Self { + let mut this = Self { + ptr: (*_a0).ptr.take(), + }; + this + } + pub unsafe fn operator_assign_pmutSafePointer( + &mut self, + _a0: *mut SafePointer, + ) -> *mut SafePointer { + self.ptr = (*_a0).ptr.take(); + return &mut (*(self as *mut SafePointer)) as *mut SafePointer; + } } #[repr(C)] #[derive(Copy, Clone, Default)] diff --git a/tests/unit/out/unsafe/unique_ptr_const_deref.rs b/tests/unit/out/unsafe/unique_ptr_const_deref.rs index 2de99893..22855a7f 100644 --- a/tests/unit/out/unsafe/unique_ptr_const_deref.rs +++ b/tests/unit/out/unsafe/unique_ptr_const_deref.rs @@ -11,6 +11,18 @@ use std::rc::Rc; pub struct Holder { pub val: Option>, } +impl Holder { + pub unsafe fn Holder_pmutHolder(_a0: *mut Holder) -> Self { + let mut this = Self { + val: (*_a0).val.take(), + }; + this + } + pub unsafe fn operator_assign_pmutHolder(&mut self, _a0: *mut Holder) -> *mut Holder { + self.val = (*_a0).val.take(); + return &mut (*(self as *mut Holder)) as *mut Holder; + } +} pub unsafe fn read_val_0(mut h: *const Holder) -> i32 { return (*(*(std::ptr::addr_of!((*h).val).cast_mut())) .as_deref_mut() diff --git a/tests/unit/out/unsafe/unique_ptr_nested.rs b/tests/unit/out/unsafe/unique_ptr_nested.rs index 02c01c14..26f0b820 100644 --- a/tests/unit/out/unsafe/unique_ptr_nested.rs +++ b/tests/unit/out/unsafe/unique_ptr_nested.rs @@ -17,6 +17,18 @@ pub struct Inner { pub struct Outer { pub inner: Option>, } +impl Outer { + pub unsafe fn Outer_pmutOuter(_a0: *mut Outer) -> Self { + let mut this = Self { + inner: (*_a0).inner.take(), + }; + this + } + pub unsafe fn operator_assign_pmutOuter(&mut self, _a0: *mut Outer) -> *mut Outer { + self.inner = (*_a0).inner.take(); + return &mut (*(self as *mut Outer)) as *mut Outer; + } +} pub fn main() { unsafe { std::process::exit(main_0() as i32); diff --git a/tests/unit/out/unsafe/void_cast.rs b/tests/unit/out/unsafe/void_cast.rs index 0ce11d69..19f23cc1 100644 --- a/tests/unit/out/unsafe/void_cast.rs +++ b/tests/unit/out/unsafe/void_cast.rs @@ -35,6 +35,21 @@ pub struct Holder { pub struct NonCopyable { pub value: Option>, } +impl NonCopyable { + pub unsafe fn NonCopyable_pmutNonCopyable(_a0: *mut NonCopyable) -> Self { + let mut this = Self { + value: (*_a0).value.take(), + }; + this + } + pub unsafe fn operator_assign_pmutNonCopyable( + &mut self, + _a0: *mut NonCopyable, + ) -> *mut NonCopyable { + self.value = (*_a0).value.take(); + return &mut (*(self as *mut NonCopyable)) as *mut NonCopyable; + } +} pub unsafe fn unused_noncopyable_param_5(x: *const NonCopyable) { &(*x); } From e7079a635e1ac7cb3b4fd63153aa9d7d50a75235 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Mon, 14 Sep 2026 20:16:57 +0100 Subject: [PATCH 02/16] Call the synthesized move constructor --- cpp2rust/converter/converter.cpp | 11 ----------- cpp2rust/converter/converter_lib.cpp | 15 +++++++-------- cpp2rust/converter/converter_lib.h | 4 +--- cpp2rust/converter/models/converter_refcount.cpp | 12 +----------- 4 files changed, 9 insertions(+), 33 deletions(-) diff --git a/cpp2rust/converter/converter.cpp b/cpp2rust/converter/converter.cpp index 3f63da57..304e1cf0 100644 --- a/cpp2rust/converter/converter.cpp +++ b/cpp2rust/converter/converter.cpp @@ -3452,16 +3452,6 @@ bool Converter::VisitCXXConstructExpr(clang::CXXConstructExpr *expr) { } auto *ctor = expr->getConstructor(); - // Default move is translated using a bitwise .clone() implementation. - // Bitwise clone is only satisfied by default copy constructor. If the copy - // constructor is user defined, then default move calls copy constructor, - // which is wrong. - if (IsDefaultedMoveConstructor(ctor) && - !HasDefaultedCopyConstructor(ctor->getParent())) { - llvm::report_fatal_error("defaulted move constructor without a fieldwise " - "copy constructor is not supported"); - } - if (IsPassThroughConstructor(ctor)) { // Take suppress before recursing into the child. bool suppress = PushSuppressIteratorClone::take(*this); @@ -3479,7 +3469,6 @@ bool Converter::VisitCXXConstructExpr(clang::CXXConstructExpr *expr) { return false; } - assert(ctor->isUserProvided()); if (expr->getType()->isArrayType()) { ConvertArrayCXXConstructExpr(expr); } else { diff --git a/cpp2rust/converter/converter_lib.cpp b/cpp2rust/converter/converter_lib.cpp index 44709984..baa3ef2b 100644 --- a/cpp2rust/converter/converter_lib.cpp +++ b/cpp2rust/converter/converter_lib.cpp @@ -286,14 +286,9 @@ bool IsUserDefinedCopyConstructor(const clang::CXXConstructorDecl *ctor) { IsUserDefinedDecl(ctor); } -bool IsUserDefinedMoveConstructor(const clang::CXXConstructorDecl *ctor) { - return ctor->isMoveConstructor() && ctor->isUserProvided() && - IsUserDefinedDecl(ctor); -} - -bool IsUserDefinedCopyOrMoveConstructor(const clang::CXXConstructorDecl *ctor) { +bool IsConvertibleCopyOrMoveConstructor(const clang::CXXConstructorDecl *ctor) { return IsUserDefinedCopyConstructor(ctor) || - IsUserDefinedMoveConstructor(ctor); + IsConvertibleMoveConstructor(ctor); } static bool @@ -394,7 +389,7 @@ bool IsRValueConvertingConstructor(const clang::CXXConstructorDecl *ctor) { } bool IsPassThroughConstructor(const clang::CXXConstructorDecl *ctor) { - return !IsUserDefinedCopyOrMoveConstructor(ctor) && + return !IsConvertibleCopyOrMoveConstructor(ctor) && (ctor->isCopyOrMoveConstructor() || IsRValueConvertingConstructor(ctor)); } @@ -857,6 +852,10 @@ bool IsUserOperatorCall(const clang::CXXOperatorCallExpr *expr) { method && method->isDefaulted() && IsComparisonOperator(method)) { return IsUserDefinedDecl(method->getParent()); } + if (const auto *method = clang::dyn_cast(callee); + method && IsConvertibleMoveAssignment(method)) { + return true; + } if (!callee->isUserProvided() || !IsUserDefinedDecl(callee)) { return false; } diff --git a/cpp2rust/converter/converter_lib.h b/cpp2rust/converter/converter_lib.h index 4246a471..52aae4d0 100644 --- a/cpp2rust/converter/converter_lib.h +++ b/cpp2rust/converter/converter_lib.h @@ -70,9 +70,7 @@ bool IsOverloadedMethod(const clang::CXXMethodDecl *decl); bool IsUserDefinedCopyConstructor(const clang::CXXConstructorDecl *ctor); -bool IsUserDefinedMoveConstructor(const clang::CXXConstructorDecl *ctor); - -bool IsUserDefinedCopyOrMoveConstructor(const clang::CXXConstructorDecl *ctor); +bool IsConvertibleCopyOrMoveConstructor(const clang::CXXConstructorDecl *ctor); bool IsDefaultedMoveConstructor(const clang::CXXConstructorDecl *ctor); diff --git a/cpp2rust/converter/models/converter_refcount.cpp b/cpp2rust/converter/models/converter_refcount.cpp index 39828e87..5ea24ff8 100644 --- a/cpp2rust/converter/models/converter_refcount.cpp +++ b/cpp2rust/converter/models/converter_refcount.cpp @@ -1899,17 +1899,8 @@ bool ConverterRefCount::VisitCXXConstructExpr(clang::CXXConstructExpr *expr) { return false; } - // Default move is translated using a bitwise .clone() implementation. - // Bitwise clone is only satisfied by default copy constructor. If the copy - // constructor is user defined, then default move calls copy constructor, - // which is wrong. - if (IsDefaultedMoveConstructor(ctor) && - !HasDefaultedCopyConstructor(ctor->getParent())) { - llvm::report_fatal_error("defaulted move constructor without a fieldwise " - "copy constructor is not supported"); - } if (ctor->isCopyOrMoveConstructor() && - !IsUserDefinedCopyOrMoveConstructor(ctor)) { + !IsConvertibleCopyOrMoveConstructor(ctor)) { StrCat(PushSuppressIteratorClone::take(*this) ? ConvertRValue(expr->getArg(0)) : ConvertFreshRValue(expr->getArg(0))); @@ -1923,7 +1914,6 @@ bool ConverterRefCount::VisitCXXConstructExpr(clang::CXXConstructExpr *expr) { return false; } - assert(ctor->isUserProvided()); if (expr->getType()->isArrayType()) { ConvertArrayCXXConstructExpr(expr); } else { From e87421b7326062a8d2ee610d8139ba0741bdbcf7 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Mon, 14 Sep 2026 20:18:22 +0100 Subject: [PATCH 03/16] Update tests --- .../defaulted_move_cross_tu/CMakeLists.txt | 3 + .../multi-file/defaulted_move_cross_tu/a.cpp | 12 + .../multi-file/defaulted_move_cross_tu/b.cpp | 14 + .../out/refcount/defaulted_move_cross_tu.rs | 114 ++++ .../out/unsafe/defaulted_move_cross_tu.rs | 75 +++ tests/multi-file/defaulted_move_cross_tu/s.h | 17 + tests/unit/copy_move_defaulted.cpp | 23 +- tests/unit/copy_move_deleted.cpp | 1 - .../unit/out/refcount/copy_move_defaulted.rs | 589 ++++++++++++++++++ tests/unit/out/refcount/copy_move_deleted.rs | 229 +++++++ tests/unit/out/unsafe/copy_move_defaulted.rs | 296 +++++++++ tests/unit/out/unsafe/copy_move_deleted.rs | 135 ++++ 12 files changed, 1506 insertions(+), 2 deletions(-) create mode 100644 tests/multi-file/defaulted_move_cross_tu/CMakeLists.txt create mode 100644 tests/multi-file/defaulted_move_cross_tu/a.cpp create mode 100644 tests/multi-file/defaulted_move_cross_tu/b.cpp create mode 100644 tests/multi-file/defaulted_move_cross_tu/out/refcount/defaulted_move_cross_tu.rs create mode 100644 tests/multi-file/defaulted_move_cross_tu/out/unsafe/defaulted_move_cross_tu.rs create mode 100644 tests/multi-file/defaulted_move_cross_tu/s.h create mode 100644 tests/unit/out/refcount/copy_move_defaulted.rs create mode 100644 tests/unit/out/refcount/copy_move_deleted.rs create mode 100644 tests/unit/out/unsafe/copy_move_defaulted.rs create mode 100644 tests/unit/out/unsafe/copy_move_deleted.rs diff --git a/tests/multi-file/defaulted_move_cross_tu/CMakeLists.txt b/tests/multi-file/defaulted_move_cross_tu/CMakeLists.txt new file mode 100644 index 00000000..504c2af0 --- /dev/null +++ b/tests/multi-file/defaulted_move_cross_tu/CMakeLists.txt @@ -0,0 +1,3 @@ +cmake_minimum_required(VERSION 3.16) +project(defaulted_move_cross_tu LANGUAGES CXX) +add_executable(app a.cpp b.cpp) diff --git a/tests/multi-file/defaulted_move_cross_tu/a.cpp b/tests/multi-file/defaulted_move_cross_tu/a.cpp new file mode 100644 index 00000000..3ef5d1a8 --- /dev/null +++ b/tests/multi-file/defaulted_move_cross_tu/a.cpp @@ -0,0 +1,12 @@ +#include + +#include "s.h" + +int sum(const S &s) { return static_cast(s.v.size()) + s.n[0] + s.n[1]; } + +int main() { + S s(2); + assert(sum(s) == 7); + assert(shuffle(3) == 10); + return 0; +} diff --git a/tests/multi-file/defaulted_move_cross_tu/b.cpp b/tests/multi-file/defaulted_move_cross_tu/b.cpp new file mode 100644 index 00000000..193cd0f0 --- /dev/null +++ b/tests/multi-file/defaulted_move_cross_tu/b.cpp @@ -0,0 +1,14 @@ +#include +#include + +#include "s.h" + +int shuffle(int x) { + S a(x); + S b(std::move(a)); + assert(a.v.empty()); + S c(1); + c = std::move(b); + assert(b.v.empty()); + return sum(c); +} diff --git a/tests/multi-file/defaulted_move_cross_tu/out/refcount/defaulted_move_cross_tu.rs b/tests/multi-file/defaulted_move_cross_tu/out/refcount/defaulted_move_cross_tu.rs new file mode 100644 index 00000000..5c9a8aab --- /dev/null +++ b/tests/multi-file/defaulted_move_cross_tu/out/refcount/defaulted_move_cross_tu.rs @@ -0,0 +1,114 @@ +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()] +pub struct S { + pub v: Value>, + pub n: Value>, +} +impl S { + pub fn S(x: i32) -> Self { + let x: Value = Rc::new(RefCell::new(x)); + let __this: Value = Rc::new(RefCell::new(Self { + v: Rc::new(RefCell::new(vec![ + (*x.borrow()); + ((*x.borrow()) as usize) as usize + ])), + n: Rc::new(RefCell::new(Box::new([(*x.borrow()), ((*x.borrow()) + 1)]))), + })); + let this: Ptr = __this.as_pointer(); + Rc::try_unwrap(__this).ok().unwrap().into_inner() + } + pub fn S_pmutS(_a0: Ptr) -> Self { + let __this: Value = Rc::new(RefCell::new(Self { + v: Rc::new(RefCell::new(std::mem::take( + &mut (*(*_a0.upgrade().deref()).v.borrow_mut()), + ))), + n: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 2, _>( + |__i: usize| (*(*_a0.upgrade().deref()).n.borrow())[(__i) as usize], + )))), + })); + let this: Ptr = __this.as_pointer(); + Rc::try_unwrap(__this).ok().unwrap().into_inner() + } +} +impl Default for S { + fn default() -> Self { + S { + v: Rc::new(RefCell::new(Default::default())), + n: Rc::new(RefCell::new( + (0..2).map(|_| ::default()).collect::>(), + )), + } + } +} +impl ByteRepr for S { + fn byte_size() -> usize { + 32 + } + fn to_bytes(&self, buf: &mut [u8]) { + (*self.v.borrow()).to_bytes(&mut buf[0..24]); + (*self.n.borrow()).to_bytes(&mut buf[24..32]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + v: Rc::new(RefCell::new(>::from_bytes(&buf[0..24]))), + n: Rc::new(RefCell::new(>::from_bytes(&buf[24..32]))), + } + } +} +pub fn sum_0(s: Ptr) -> i32 { + return { + let _lhs = { + let _lhs = ((*(*s.upgrade().deref()).v.borrow()).len() as i32); + _lhs + (*(*s.upgrade().deref()).n.borrow())[(0) as usize] + }; + _lhs + (*(*s.upgrade().deref()).n.borrow())[(1) as usize] + }; +} +pub fn main() { + std::process::exit(main_0()); +} +fn main_0() -> i32 { + let s: Value = Rc::new(RefCell::new(S::S({ 2 }))); + assert!((({ sum_0(s.as_pointer(),) }) == 7)); + assert!((({ shuffle_1(3,) }) == 10)); + return 0; +} +pub fn shuffle_1(x: i32) -> i32 { + let x: Value = Rc::new(RefCell::new(x)); + let a: Value = Rc::new(RefCell::new(S::S({ (*x.borrow()) }))); + let b: Value = Rc::new(RefCell::new(S::S_pmutS({ a.as_pointer() }))); + assert!((*(*a.borrow()).v.borrow()).is_empty()); + let c: Value = Rc::new(RefCell::new(S::S({ 1 }))); + ({ SImpl::operator_assign_pmutS(&c.as_pointer(), b.as_pointer()) }); + assert!((*(*b.borrow()).v.borrow()).is_empty()); + return ({ sum_0(c.as_pointer()) }); +} +pub trait SImpl { + fn operator_assign_pmutS(&self, _a0: Ptr) -> Ptr; +} +impl SImpl for Ptr { + fn operator_assign_pmutS(&self, _a0: Ptr) -> Ptr { + ((*(*self).upgrade().deref()).v.as_pointer() as Ptr>).write(std::mem::take( + &mut (*(*_a0.upgrade().deref()).v.borrow_mut()), + )); + { + (((*(*self).upgrade().deref()).n.as_pointer()) as Ptr) + .to_any() + .memcpy( + &(((*_a0.upgrade().deref()).n.as_pointer()) as Ptr).to_any(), + 8_usize as usize, + ); + (((*(*self).upgrade().deref()).n.as_pointer()) as Ptr) + .to_any() + .clone() + }; + return (*self).clone(); + } +} diff --git a/tests/multi-file/defaulted_move_cross_tu/out/unsafe/defaulted_move_cross_tu.rs b/tests/multi-file/defaulted_move_cross_tu/out/unsafe/defaulted_move_cross_tu.rs new file mode 100644 index 00000000..1cf9c82c --- /dev/null +++ b/tests/multi-file/defaulted_move_cross_tu/out/unsafe/defaulted_move_cross_tu.rs @@ -0,0 +1,75 @@ +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()] +pub struct S { + pub v: Vec, + pub n: [i32; 2], +} +impl S { + pub unsafe fn S(mut x: i32) -> Self { + let mut this = Self { + v: vec![x; (x as usize) as usize], + n: [x, ((x) + (1))], + }; + this + } + pub unsafe fn S_pmutS(_a0: *mut S) -> Self { + let mut this = Self { + v: std::mem::take(&mut (*_a0).v), + n: std::array::from_fn::<_, 2, _>(|__i: usize| (*_a0).n[(__i)]), + }; + this + } + pub unsafe fn operator_assign_pmutS(&mut self, _a0: *mut S) -> *mut S { + self.v = std::mem::take(&mut (*_a0).v); + { + if 8_usize != 0 { + ::std::ptr::copy_nonoverlapping( + ((&mut (*_a0).n as *mut [i32; 2]) as *const [i32; 2] as *const ::libc::c_void), + ((&mut self.n as *mut [i32; 2]) as *mut [i32; 2] as *mut ::libc::c_void), + 8_usize as usize, + ) + } + ((&mut self.n as *mut [i32; 2]) as *mut [i32; 2] as *mut ::libc::c_void) + }; + return &mut (*(self as *mut S)) as *mut S; + } +} +impl Default for S { + fn default() -> Self { + S { + v: Default::default(), + n: [0_i32; 2], + } + } +} +pub unsafe fn sum_0(s: *const S) -> i32 { + return ((((*s).v.len() as i32) + ((*s).n[(0) as usize])) + ((*s).n[(1) as usize])); +} +pub fn main() { + unsafe { + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + let mut s: S = S::S({ 2 }); + assert!(((unsafe { sum_0(&s as *const S,) }) == (7))); + assert!(((unsafe { shuffle_1(3,) }) == (10))); + return 0; +} +pub unsafe fn shuffle_1(mut x: i32) -> i32 { + let mut a: S = S::S({ x }); + let mut b: S = S::S_pmutS({ &mut a }); + assert!(a.v.is_empty()); + let mut c: S = S::S({ 1 }); + (unsafe { S::operator_assign_pmutS(&mut c, &mut b) }); + assert!(b.v.is_empty()); + return (unsafe { sum_0(&c as *const S) }); +} diff --git a/tests/multi-file/defaulted_move_cross_tu/s.h b/tests/multi-file/defaulted_move_cross_tu/s.h new file mode 100644 index 00000000..99c1e417 --- /dev/null +++ b/tests/multi-file/defaulted_move_cross_tu/s.h @@ -0,0 +1,17 @@ +#pragma once + +#include + +struct S { + std::vector v; + int n[2]; + + S(int x) : v(x, x), n{x, x + 1} {} + S(const S &) = delete; + S(S &&) = default; + S &operator=(const S &) = delete; + S &operator=(S &&) = default; +}; + +int sum(const S &s); +int shuffle(int x); diff --git a/tests/unit/copy_move_defaulted.cpp b/tests/unit/copy_move_defaulted.cpp index 8452357c..f95e9a26 100644 --- a/tests/unit/copy_move_defaulted.cpp +++ b/tests/unit/copy_move_defaulted.cpp @@ -1,4 +1,3 @@ -// translation-fail #include #include #include @@ -50,6 +49,17 @@ struct UserCopyDefaultMove { UserCopyDefaultMove &operator=(UserCopyDefaultMove &&) = default; }; +struct Buffer { + std::vector data; + int n; + int arr[2]; + Buffer(int n) : data(n, n), n(n), arr{n, n + 1} {} + Buffer(const Buffer &) = delete; + Buffer(Buffer &&) = default; + Buffer &operator=(const Buffer &) = delete; + Buffer &operator=(Buffer &&) = default; +}; + static bool same(const Explicit &a, const Explicit &b) { return a.v == b.v && a.inner.x == b.inner.x && a.arr[0] == b.arr[0] && a.arr[1] == b.arr[1]; @@ -101,5 +111,16 @@ int main() { u3 = u2; u4 = std::move(u2); assert(u3.v == 108 && u4.v == 8); + + Buffer p(3); + Buffer q = std::move(p); + assert(q.n == 3 && q.data.size() == 3 && q.data[2] == 3 && p.data.empty()); + Buffer r(1); + r = std::move(q); + assert(r.n == 3 && r.data.size() == 3 && r.arr[1] == 4 && q.data.empty()); + std::vector bufs; + bufs.push_back(std::move(r)); + bufs.emplace_back(std::move(bufs[0])); + assert(bufs[1].n == 3 && bufs[1].data.size() == 3 && bufs[0].data.empty()); return 0; } diff --git a/tests/unit/copy_move_deleted.cpp b/tests/unit/copy_move_deleted.cpp index c635eb03..4f7eabed 100644 --- a/tests/unit/copy_move_deleted.cpp +++ b/tests/unit/copy_move_deleted.cpp @@ -1,4 +1,3 @@ -// translation-fail #include #include diff --git a/tests/unit/out/refcount/copy_move_defaulted.rs b/tests/unit/out/refcount/copy_move_defaulted.rs new file mode 100644 index 00000000..5c39532a --- /dev/null +++ b/tests/unit/out/refcount/copy_move_defaulted.rs @@ -0,0 +1,589 @@ +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(Default)] +pub struct Inner { + pub x: Value, +} +impl Clone for Inner { + fn clone(&self) -> Self { + let __this: Value = Rc::new(RefCell::new(Self { + x: Rc::new(RefCell::new((*self.x.borrow()))), + })); + let this: Ptr = __this.as_pointer(); + Rc::try_unwrap(__this).ok().unwrap().into_inner() + } +} +impl ByteRepr for Inner { + fn byte_size() -> usize { + 4 + } + fn to_bytes(&self, buf: &mut [u8]) { + (*self.x.borrow()).to_bytes(&mut buf[0..4]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + x: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + } + } +} +#[derive()] +pub struct Explicit { + pub v: Value, + pub inner: Value, + pub arr: Value>, +} +impl Explicit { + pub fn Explicit(v: i32) -> Self { + let v: Value = Rc::new(RefCell::new(v)); + let __this: Value = Rc::new(RefCell::new(Self { + v: Rc::new(RefCell::new((*v.borrow()))), + inner: Rc::new(RefCell::new(Inner { + x: Rc::new(RefCell::new(((*v.borrow()) * 10))), + })), + arr: Rc::new(RefCell::new(Box::new([(*v.borrow()), ((*v.borrow()) + 1)]))), + })); + let this: Ptr = __this.as_pointer(); + Rc::try_unwrap(__this).ok().unwrap().into_inner() + } +} +impl Clone for Explicit { + fn clone(&self) -> Self { + let __this: Value = Rc::new(RefCell::new(Self { + v: Rc::new(RefCell::new((*self.v.borrow()))), + inner: Rc::new(RefCell::new((*self.inner.borrow()).clone())), + arr: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 2, _>( + |__i: usize| (*self.arr.borrow())[(__i) as usize], + )))), + })); + let this: Ptr = __this.as_pointer(); + Rc::try_unwrap(__this).ok().unwrap().into_inner() + } +} +impl Default for Explicit { + fn default() -> Self { + Explicit { + v: >::default(), + inner: >::default(), + arr: Rc::new(RefCell::new( + (0..2).map(|_| ::default()).collect::>(), + )), + } + } +} +impl ByteRepr for Explicit { + fn byte_size() -> usize { + 16 + } + fn to_bytes(&self, buf: &mut [u8]) { + (*self.v.borrow()).to_bytes(&mut buf[0..4]); + (*self.inner.borrow()).to_bytes(&mut buf[4..8]); + (*self.arr.borrow()).to_bytes(&mut buf[8..16]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + v: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + inner: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + arr: Rc::new(RefCell::new(>::from_bytes(&buf[8..16]))), + } + } +} +#[derive()] +pub struct Implicit { + pub v: Value, + pub inner: Value, + pub arr: Value>, +} +impl Clone for Implicit { + fn clone(&self) -> Self { + let __this: Value = Rc::new(RefCell::new(Self { + v: Rc::new(RefCell::new((*self.v.borrow()))), + inner: Rc::new(RefCell::new((*self.inner.borrow()).clone())), + arr: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 2, _>( + |__i: usize| (*self.arr.borrow())[(__i) as usize], + )))), + })); + let this: Ptr = __this.as_pointer(); + Rc::try_unwrap(__this).ok().unwrap().into_inner() + } +} +impl Default for Implicit { + fn default() -> Self { + Implicit { + v: >::default(), + inner: >::default(), + arr: Rc::new(RefCell::new( + (0..2).map(|_| ::default()).collect::>(), + )), + } + } +} +impl ByteRepr for Implicit { + fn byte_size() -> usize { + 16 + } + fn to_bytes(&self, buf: &mut [u8]) { + (*self.v.borrow()).to_bytes(&mut buf[0..4]); + (*self.inner.borrow()).to_bytes(&mut buf[4..8]); + (*self.arr.borrow()).to_bytes(&mut buf[8..16]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + v: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + inner: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + arr: Rc::new(RefCell::new(>::from_bytes(&buf[8..16]))), + } + } +} +#[derive(Default)] +pub struct DefaultCopyUserMove { + pub v: Value, +} +impl DefaultCopyUserMove { + pub fn DefaultCopyUserMove(v: i32) -> Self { + let v: Value = Rc::new(RefCell::new(v)); + let __this: Value = Rc::new(RefCell::new(Self { + v: Rc::new(RefCell::new((*v.borrow()))), + })); + let this: Ptr = __this.as_pointer(); + Rc::try_unwrap(__this).ok().unwrap().into_inner() + } + pub fn DefaultCopyUserMove_pmutDefaultCopyUserMove(o: Ptr) -> Self { + let __this: Value = Rc::new(RefCell::new(Self { + v: Rc::new(RefCell::new((*(*o.upgrade().deref()).v.borrow()))), + })); + let this: Ptr = __this.as_pointer(); + (*(*o.upgrade().deref()).v.borrow_mut()) = 0; + Rc::try_unwrap(__this).ok().unwrap().into_inner() + } +} +impl Clone for DefaultCopyUserMove { + fn clone(&self) -> Self { + let __this: Value = Rc::new(RefCell::new(Self { + v: Rc::new(RefCell::new((*self.v.borrow()))), + })); + let this: Ptr = __this.as_pointer(); + Rc::try_unwrap(__this).ok().unwrap().into_inner() + } +} +impl ByteRepr for DefaultCopyUserMove { + fn byte_size() -> usize { + 4 + } + fn to_bytes(&self, buf: &mut [u8]) { + (*self.v.borrow()).to_bytes(&mut buf[0..4]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + v: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + } + } +} +#[derive(Default)] +pub struct UserCopyDefaultMove { + pub v: Value, +} +impl UserCopyDefaultMove { + pub fn UserCopyDefaultMove(v: i32) -> Self { + let v: Value = Rc::new(RefCell::new(v)); + let __this: Value = Rc::new(RefCell::new(Self { + v: Rc::new(RefCell::new((*v.borrow()))), + })); + let this: Ptr = __this.as_pointer(); + Rc::try_unwrap(__this).ok().unwrap().into_inner() + } + pub fn UserCopyDefaultMove_pconstUserCopyDefaultMove(o: Ptr) -> Self { + let __this: Value = Rc::new(RefCell::new(Self { + v: Rc::new(RefCell::new(((*(*o.upgrade().deref()).v.borrow()) + 100))), + })); + let this: Ptr = __this.as_pointer(); + Rc::try_unwrap(__this).ok().unwrap().into_inner() + } + pub fn UserCopyDefaultMove_pmutUserCopyDefaultMove(_a0: Ptr) -> Self { + let __this: Value = Rc::new(RefCell::new(Self { + v: Rc::new(RefCell::new((*(*_a0.upgrade().deref()).v.borrow()))), + })); + let this: Ptr = __this.as_pointer(); + Rc::try_unwrap(__this).ok().unwrap().into_inner() + } +} +impl Clone for UserCopyDefaultMove { + fn clone(&self) -> Self { + let __src: Value = + Rc::new(RefCell::new(UserCopyDefaultMove { v: self.v.clone() })); + UserCopyDefaultMove::UserCopyDefaultMove_pconstUserCopyDefaultMove(__src.as_pointer()) + } +} +impl ByteRepr for UserCopyDefaultMove { + fn byte_size() -> usize { + 4 + } + fn to_bytes(&self, buf: &mut [u8]) { + (*self.v.borrow()).to_bytes(&mut buf[0..4]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + v: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + } + } +} +#[derive()] +pub struct Buffer { + pub data: Value>, + pub n: Value, + pub arr: Value>, +} +impl Buffer { + pub fn Buffer(n: i32) -> Self { + let n: Value = Rc::new(RefCell::new(n)); + let __this: Value = Rc::new(RefCell::new(Self { + data: Rc::new(RefCell::new(vec![ + (*n.borrow()); + ((*n.borrow()) as usize) as usize + ])), + n: Rc::new(RefCell::new((*n.borrow()))), + arr: Rc::new(RefCell::new(Box::new([(*n.borrow()), ((*n.borrow()) + 1)]))), + })); + let this: Ptr = __this.as_pointer(); + Rc::try_unwrap(__this).ok().unwrap().into_inner() + } + pub fn Buffer_pmutBuffer(_a0: Ptr) -> Self { + let __this: Value = Rc::new(RefCell::new(Self { + data: Rc::new(RefCell::new(std::mem::take( + &mut (*(*_a0.upgrade().deref()).data.borrow_mut()), + ))), + n: Rc::new(RefCell::new((*(*_a0.upgrade().deref()).n.borrow()))), + arr: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 2, _>( + |__i: usize| (*(*_a0.upgrade().deref()).arr.borrow())[(__i) as usize], + )))), + })); + let this: Ptr = __this.as_pointer(); + Rc::try_unwrap(__this).ok().unwrap().into_inner() + } +} +impl Default for Buffer { + fn default() -> Self { + Buffer { + data: Rc::new(RefCell::new(Default::default())), + n: >::default(), + arr: Rc::new(RefCell::new( + (0..2).map(|_| ::default()).collect::>(), + )), + } + } +} +impl ByteRepr for Buffer { + fn byte_size() -> usize { + 40 + } + fn to_bytes(&self, buf: &mut [u8]) { + (*self.data.borrow()).to_bytes(&mut buf[0..24]); + (*self.n.borrow()).to_bytes(&mut buf[24..28]); + (*self.arr.borrow()).to_bytes(&mut buf[28..36]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + data: Rc::new(RefCell::new(>::from_bytes(&buf[0..24]))), + n: Rc::new(RefCell::new(::from_bytes(&buf[24..28]))), + arr: Rc::new(RefCell::new(>::from_bytes(&buf[28..36]))), + } + } +} +pub fn same_0(a: Ptr, b: Ptr) -> bool { + return ((({ + let _lhs = (*(*a.upgrade().deref()).v.borrow()); + _lhs == (*(*b.upgrade().deref()).v.borrow()) + }) && ({ + let _lhs = (*(*(*a.upgrade().deref()).inner.borrow()).x.borrow()); + _lhs == (*(*(*b.upgrade().deref()).inner.borrow()).x.borrow()) + })) && ({ + let _lhs = (*(*a.upgrade().deref()).arr.borrow())[(0) as usize]; + _lhs == (*(*b.upgrade().deref()).arr.borrow())[(0) as usize] + })) && ({ + let _lhs = (*(*a.upgrade().deref()).arr.borrow())[(1) as usize]; + _lhs == (*(*b.upgrade().deref()).arr.borrow())[(1) as usize] + }); +} +pub fn main() { + std::process::exit(main_0()); +} +fn main_0() -> i32 { + let a: Value = Rc::new(RefCell::new(Explicit::Explicit({ 1 }))); + let _dtor_a = ScopedDestructor::new(&a, |__p| __p.destructor()); + let b: Value = Rc::new(RefCell::new((*a.borrow()).clone())); + let _dtor_b = ScopedDestructor::new(&b, |__p| __p.destructor()); + let c: Value = Rc::new(RefCell::new((*a.borrow()).clone())); + let _dtor_c = ScopedDestructor::new(&c, |__p| __p.destructor()); + let d: Value = Rc::new(RefCell::new((*a.borrow()).clone())); + let _dtor_d = ScopedDestructor::new(&d, |__p| __p.destructor()); + assert!( + (({ same_0(b.as_pointer(), a.as_pointer(),) }) + && ({ same_0(c.as_pointer(), a.as_pointer(),) })) + && ({ same_0(d.as_pointer(), a.as_pointer(),) }) + ); + let e: Value = Rc::new(RefCell::new(Explicit::Explicit({ 2 }))); + let _dtor_e = ScopedDestructor::new(&e, |__p| __p.destructor()); + let f: Value = Rc::new(RefCell::new(Explicit::Explicit({ 3 }))); + let _dtor_f = ScopedDestructor::new(&f, |__p| __p.destructor()); + (*e.borrow_mut()) = (*b.borrow()).clone(); + (*f.borrow_mut()) = (*c.borrow()).clone(); + assert!( + ({ same_0(e.as_pointer(), b.as_pointer(),) }) + && ({ same_0(f.as_pointer(), c.as_pointer(),) }) + ); + let g: Value = Rc::new(RefCell::new(Explicit::Explicit({ 4 }))); + let _dtor_g = ScopedDestructor::new(&g, |__p| __p.destructor()); + (*g.borrow_mut()) = { + (*e.borrow_mut()) = (*f.borrow()).clone(); + (*e.borrow()).clone() + }; + assert!( + ({ same_0(g.as_pointer(), f.as_pointer(),) }) + && ({ same_0(e.as_pointer(), f.as_pointer(),) }) + ); + let i: Value = Rc::new(RefCell::new(Implicit { + v: Rc::new(RefCell::new(5)), + inner: Rc::new(RefCell::new(Inner { + x: Rc::new(RefCell::new(50)), + })), + arr: Rc::new(RefCell::new(Box::new([5, 6]))), + })); + let j: Value = Rc::new(RefCell::new((*i.borrow()).clone())); + let k: Value = Rc::new(RefCell::new((*i.borrow()).clone())); + assert!( + (((*(*j.borrow()).v.borrow()) == 5) + && ((*(*(*j.borrow()).inner.borrow()).x.borrow()) == 50)) + && ((*(*j.borrow()).arr.borrow())[(1) as usize] == 6) + ); + assert!(((*(*i.borrow()).v.borrow()) == 5) && ((*(*k.borrow()).v.borrow()) == 5)); + let l: Value = Rc::new(RefCell::new(Implicit { + v: Rc::new(RefCell::new(0)), + inner: Rc::new(RefCell::new(Inner { + x: Rc::new(RefCell::new(0)), + })), + arr: Rc::new(RefCell::new(Box::new([0, 0]))), + })); + (*l.borrow_mut()) = (*j.borrow()).clone(); + assert!( + (((*(*l.borrow()).v.borrow()) == 5) + && ((*(*(*l.borrow()).inner.borrow()).x.borrow()) == 50)) + && ((*(*l.borrow()).arr.borrow())[(0) as usize] == 5) + ); + let vec_: Value> = Rc::new(RefCell::new(Vec::new())); + { + let a0_clone = (*b.borrow()).clone(); + (*vec_.borrow_mut()).push(a0_clone) + }; + (*vec_.borrow_mut()).push(Explicit::Explicit({ 9 })); + assert!( + ((*(*(vec_.as_pointer() as Ptr) + .offset(0_usize) + .upgrade() + .deref()) + .v + .borrow()) + == 1) + && ((*(*(vec_.as_pointer() as Ptr) + .offset(1_usize) + .upgrade() + .deref()) + .v + .borrow()) + == 9) + ); + let m: Value = + Rc::new(RefCell::new(DefaultCopyUserMove::DefaultCopyUserMove({ + 7 + }))); + let m1: Value = Rc::new(RefCell::new((*m.borrow()).clone())); + let m2: Value = Rc::new(RefCell::new( + DefaultCopyUserMove::DefaultCopyUserMove_pmutDefaultCopyUserMove({ m.as_pointer() }), + )); + assert!( + (((*(*m1.borrow()).v.borrow()) == 7) && ((*(*m2.borrow()).v.borrow()) == 7)) + && ((*(*m.borrow()).v.borrow()) == 0) + ); + let m3: Value = + Rc::new(RefCell::new(DefaultCopyUserMove::DefaultCopyUserMove({ + 1 + }))); + let m4: Value = + Rc::new(RefCell::new(DefaultCopyUserMove::DefaultCopyUserMove({ + 1 + }))); + (*m3.borrow_mut()) = (*m1.borrow()).clone(); + ({ + DefaultCopyUserMoveImpl::operator_assign_pmutDefaultCopyUserMove( + &m4.as_pointer(), + m1.as_pointer(), + ) + }); + assert!( + (((*(*m3.borrow()).v.borrow()) == 7) && ((*(*m4.borrow()).v.borrow()) == 7)) + && ((*(*m1.borrow()).v.borrow()) == 0) + ); + let u: Value = + Rc::new(RefCell::new(UserCopyDefaultMove::UserCopyDefaultMove({ + 8 + }))); + let u1: Value = Rc::new(RefCell::new( + UserCopyDefaultMove::UserCopyDefaultMove_pconstUserCopyDefaultMove({ u.as_pointer() }), + )); + let u2: Value = Rc::new(RefCell::new( + UserCopyDefaultMove::UserCopyDefaultMove_pmutUserCopyDefaultMove({ u.as_pointer() }), + )); + assert!( + (((*(*u1.borrow()).v.borrow()) == 108) && ((*(*u2.borrow()).v.borrow()) == 8)) + && ((*(*u.borrow()).v.borrow()) == 8) + ); + let u3: Value = + Rc::new(RefCell::new(UserCopyDefaultMove::UserCopyDefaultMove({ + 1 + }))); + let u4: Value = + Rc::new(RefCell::new(UserCopyDefaultMove::UserCopyDefaultMove({ + 1 + }))); + ({ + UserCopyDefaultMoveImpl::operator_assign_pconstUserCopyDefaultMove( + &u3.as_pointer(), + u2.as_pointer(), + ) + }); + ({ + UserCopyDefaultMoveImpl::operator_assign_pmutUserCopyDefaultMove( + &u4.as_pointer(), + u2.as_pointer(), + ) + }); + assert!(((*(*u3.borrow()).v.borrow()) == 108) && ((*(*u4.borrow()).v.borrow()) == 8)); + let p: Value = Rc::new(RefCell::new(Buffer::Buffer({ 3 }))); + let q: Value = Rc::new(RefCell::new(Buffer::Buffer_pmutBuffer({ p.as_pointer() }))); + assert!( + ((((*(*q.borrow()).n.borrow()) == 3) && ((*(*q.borrow()).data.borrow()).len() == 3_usize)) + && ((((*q.borrow()).data.as_pointer() as Ptr) + .offset(2_usize) + .read()) + == 3)) + && ((*(*p.borrow()).data.borrow()).is_empty()) + ); + let r: Value = Rc::new(RefCell::new(Buffer::Buffer({ 1 }))); + ({ BufferImpl::operator_assign_pmutBuffer(&r.as_pointer(), q.as_pointer()) }); + assert!( + ((((*(*r.borrow()).n.borrow()) == 3) && ((*(*r.borrow()).data.borrow()).len() == 3_usize)) + && ((*(*r.borrow()).arr.borrow())[(1) as usize] == 4)) + && ((*(*q.borrow()).data.borrow()).is_empty()) + ); + let bufs: Value> = Rc::new(RefCell::new(Vec::new())); + (*bufs.borrow_mut()).push(std::mem::take(&mut (*r.borrow_mut()))); + bufs.as_pointer().with_mut(|__v: &mut Vec| { + __v.push(std::mem::take(&mut Buffer::Buffer_pmutBuffer({ + (bufs.as_pointer() as Ptr).offset(0_usize) + }))) + }); + assert!( + (((*(*(bufs.as_pointer() as Ptr) + .offset(1_usize) + .upgrade() + .deref()) + .n + .borrow()) + == 3) + && ((*(*(bufs.as_pointer() as Ptr) + .offset(1_usize) + .upgrade() + .deref()) + .data + .borrow()) + .len() + == 3_usize)) + && ((*(*(bufs.as_pointer() as Ptr) + .offset(0_usize) + .upgrade() + .deref()) + .data + .borrow()) + .is_empty()) + ); + return 0; +} +pub trait BufferImpl { + fn operator_assign_pmutBuffer(&self, _a0: Ptr) -> Ptr; +} +impl BufferImpl for Ptr { + fn operator_assign_pmutBuffer(&self, _a0: Ptr) -> Ptr { + ((*(*self).upgrade().deref()).data.as_pointer() as Ptr>).write(std::mem::take( + &mut (*(*_a0.upgrade().deref()).data.borrow_mut()), + )); + let __rhs = (*(*_a0.upgrade().deref()).n.borrow()); + (*(*(*self).upgrade().deref()).n.borrow_mut()) = __rhs; + { + (((*(*self).upgrade().deref()).arr.as_pointer()) as Ptr) + .to_any() + .memcpy( + &(((*_a0.upgrade().deref()).arr.as_pointer()) as Ptr).to_any(), + 8_usize as usize, + ); + (((*(*self).upgrade().deref()).arr.as_pointer()) as Ptr) + .to_any() + .clone() + }; + return (*self).clone(); + } +} +pub trait DefaultCopyUserMoveImpl { + fn operator_assign_pmutDefaultCopyUserMove( + &self, + o: Ptr, + ) -> Ptr; +} +impl DefaultCopyUserMoveImpl for Ptr { + fn operator_assign_pmutDefaultCopyUserMove( + &self, + o: Ptr, + ) -> Ptr { + let __rhs = (*(*o.upgrade().deref()).v.borrow()); + (*(*(*self).upgrade().deref()).v.borrow_mut()) = __rhs; + (*(*o.upgrade().deref()).v.borrow_mut()) = 0; + return (*self).clone(); + } +} +pub trait ExplicitImpl { + fn destructor(&self); +} +impl ExplicitImpl for Ptr { + fn destructor(&self) {} +} +pub trait UserCopyDefaultMoveImpl { + fn operator_assign_pconstUserCopyDefaultMove( + &self, + o: Ptr, + ) -> Ptr; + fn operator_assign_pmutUserCopyDefaultMove( + &self, + _a0: Ptr, + ) -> Ptr; +} +impl UserCopyDefaultMoveImpl for Ptr { + fn operator_assign_pconstUserCopyDefaultMove( + &self, + o: Ptr, + ) -> Ptr { + let __rhs = ((*(*o.upgrade().deref()).v.borrow()) + 100); + (*(*(*self).upgrade().deref()).v.borrow_mut()) = __rhs; + return (*self).clone(); + } + fn operator_assign_pmutUserCopyDefaultMove( + &self, + _a0: Ptr, + ) -> Ptr { + let __rhs = (*(*_a0.upgrade().deref()).v.borrow()); + (*(*(*self).upgrade().deref()).v.borrow_mut()) = __rhs; + return (*self).clone(); + } +} diff --git a/tests/unit/out/refcount/copy_move_deleted.rs b/tests/unit/out/refcount/copy_move_deleted.rs new file mode 100644 index 00000000..0cf4c031 --- /dev/null +++ b/tests/unit/out/refcount/copy_move_deleted.rs @@ -0,0 +1,229 @@ +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(Default)] +pub struct NoCopy { + pub v: Value, +} +impl NoCopy { + pub fn NoCopy(v: i32) -> Self { + let v: Value = Rc::new(RefCell::new(v)); + let __this: Value = Rc::new(RefCell::new(Self { + v: Rc::new(RefCell::new((*v.borrow()))), + })); + let this: Ptr = __this.as_pointer(); + Rc::try_unwrap(__this).ok().unwrap().into_inner() + } + pub fn NoCopy_pmutNoCopy(o: Ptr) -> Self { + let __this: Value = Rc::new(RefCell::new(Self { + v: Rc::new(RefCell::new((*(*o.upgrade().deref()).v.borrow()))), + })); + let this: Ptr = __this.as_pointer(); + (*(*o.upgrade().deref()).v.borrow_mut()) = 0; + Rc::try_unwrap(__this).ok().unwrap().into_inner() + } +} +impl ByteRepr for NoCopy { + fn byte_size() -> usize { + 4 + } + fn to_bytes(&self, buf: &mut [u8]) { + (*self.v.borrow()).to_bytes(&mut buf[0..4]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + v: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + } + } +} +#[derive()] +pub struct PrivateCopy { + pub v: Value, +} +impl PrivateCopy { + pub fn PrivateCopy() -> Self { + let __this: Value = Rc::new(RefCell::new(Self { + v: Rc::new(RefCell::new(0)), + })); + let this: Ptr = __this.as_pointer(); + Rc::try_unwrap(__this).ok().unwrap().into_inner() + } + pub fn PrivateCopy_pmutPrivateCopy(o: Ptr) -> Self { + let __this: Value = Rc::new(RefCell::new(Self { + v: Rc::new(RefCell::new((*(*o.upgrade().deref()).v.borrow()))), + })); + let this: Ptr = __this.as_pointer(); + (*(*o.upgrade().deref()).v.borrow_mut()) = 0; + Rc::try_unwrap(__this).ok().unwrap().into_inner() + } +} +impl Default for PrivateCopy { + fn default() -> Self { + { PrivateCopy::PrivateCopy() } + } +} +impl ByteRepr for PrivateCopy { + fn byte_size() -> usize { + 4 + } + fn to_bytes(&self, buf: &mut [u8]) { + (*self.v.borrow()).to_bytes(&mut buf[0..4]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + v: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + } + } +} +#[derive()] +pub struct Immovable { + pub v: Value, +} +impl Immovable { + pub fn Immovable() -> Self { + let __this: Value = Rc::new(RefCell::new(Self { + v: Rc::new(RefCell::new(0)), + })); + let this: Ptr = __this.as_pointer(); + Rc::try_unwrap(__this).ok().unwrap().into_inner() + } +} +impl Default for Immovable { + fn default() -> Self { + { Immovable::Immovable() } + } +} +impl ByteRepr for Immovable { + fn byte_size() -> usize { + 4 + } + fn to_bytes(&self, buf: &mut [u8]) { + (*self.v.borrow()).to_bytes(&mut buf[0..4]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + v: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + } + } +} +#[derive(Default)] +pub struct Container { + pub inner: Value, + pub tag: Value, +} +impl Container { + pub fn Container_pmutContainer(_a0: Ptr) -> Self { + let __this: Value = Rc::new(RefCell::new(Self { + inner: Rc::new(RefCell::new(NoCopy::NoCopy_pmutNoCopy({ + (*_a0.upgrade().deref()).inner.as_pointer() + }))), + tag: Rc::new(RefCell::new((*(*_a0.upgrade().deref()).tag.borrow()))), + })); + let this: Ptr = __this.as_pointer(); + Rc::try_unwrap(__this).ok().unwrap().into_inner() + } +} +impl ByteRepr for Container { + fn byte_size() -> usize { + 8 + } + fn to_bytes(&self, buf: &mut [u8]) { + (*self.inner.borrow()).to_bytes(&mut buf[0..4]); + (*self.tag.borrow()).to_bytes(&mut buf[4..8]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + inner: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + tag: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + } + } +} +pub fn bump_0(p: Ptr) { + let p: Value> = Rc::new(RefCell::new(p)); + (*(*(*p.borrow()).upgrade().deref()).v.borrow_mut()).postfix_inc(); +} +pub fn bump_ref_1(r: Ptr) { + (*(*r.upgrade().deref()).v.borrow_mut()).postfix_inc(); +} +pub fn main() { + std::process::exit(main_0()); +} +fn main_0() -> i32 { + let a: Value = Rc::new(RefCell::new(NoCopy::NoCopy({ 1 }))); + let b: Value = Rc::new(RefCell::new(NoCopy::NoCopy_pmutNoCopy({ a.as_pointer() }))); + assert!(((*(*b.borrow()).v.borrow()) == 1) && ((*(*a.borrow()).v.borrow()) == 0)); + ({ NoCopyImpl::operator_assign_pmutNoCopy(&a.as_pointer(), b.as_pointer()) }); + assert!(((*(*a.borrow()).v.borrow()) == 1) && ((*(*b.borrow()).v.borrow()) == 0)); + ({ bump_0((a.as_pointer())) }); + assert!(((*(*a.borrow()).v.borrow()) == 2)); + let p: Value = Rc::new(RefCell::new(PrivateCopy::PrivateCopy())); + (*(*p.borrow()).v.borrow_mut()) = 3; + let q: Value = Rc::new(RefCell::new(PrivateCopy::PrivateCopy_pmutPrivateCopy({ + p.as_pointer() + }))); + assert!(((*(*q.borrow()).v.borrow()) == 3) && ((*(*p.borrow()).v.borrow()) == 0)); + ({ PrivateCopyImpl::operator_assign_pmutPrivateCopy(&p.as_pointer(), q.as_pointer()) }); + assert!(((*(*p.borrow()).v.borrow()) == 3) && ((*(*q.borrow()).v.borrow()) == 0)); + let im: Value = Rc::new(RefCell::new(Immovable::Immovable())); + (*(*im.borrow()).v.borrow_mut()) = 4; + ({ bump_ref_1(im.as_pointer()) }); + let pim: Value> = Rc::new(RefCell::new((im.as_pointer()))); + assert!(((*(*(*pim.borrow()).upgrade().deref()).v.borrow()) == 5)); + let c: Value = Rc::new(RefCell::new(Container { + inner: Rc::new(RefCell::new(NoCopy::NoCopy({ 6 }))), + tag: Rc::new(RefCell::new(7)), + })); + let d: Value = Rc::new(RefCell::new(Container::Container_pmutContainer({ + c.as_pointer() + }))); + assert!( + (((*(*(*d.borrow()).inner.borrow()).v.borrow()) == 6) + && ((*(*d.borrow()).tag.borrow()) == 7)) + && ((*(*(*c.borrow()).inner.borrow()).v.borrow()) == 0) + ); + return 0; +} +pub trait ContainerImpl { + fn operator_assign_pmutContainer(&self, _a0: Ptr) -> Ptr; +} +impl ContainerImpl for Ptr { + fn operator_assign_pmutContainer(&self, _a0: Ptr) -> Ptr { + ({ + let _o: Ptr = (*_a0.upgrade().deref()).inner.as_pointer(); + NoCopyImpl::operator_assign_pmutNoCopy( + &(*(*self).upgrade().deref()).inner.as_pointer(), + _o, + ) + }); + let __rhs = (*(*_a0.upgrade().deref()).tag.borrow()); + (*(*(*self).upgrade().deref()).tag.borrow_mut()) = __rhs; + return (*self).clone(); + } +} +pub trait NoCopyImpl { + fn operator_assign_pmutNoCopy(&self, o: Ptr) -> Ptr; +} +impl NoCopyImpl for Ptr { + fn operator_assign_pmutNoCopy(&self, o: Ptr) -> Ptr { + let __rhs = (*(*o.upgrade().deref()).v.borrow()); + (*(*(*self).upgrade().deref()).v.borrow_mut()) = __rhs; + (*(*o.upgrade().deref()).v.borrow_mut()) = 0; + return (*self).clone(); + } +} +pub trait PrivateCopyImpl { + fn operator_assign_pmutPrivateCopy(&self, o: Ptr) -> Ptr; +} +impl PrivateCopyImpl for Ptr { + fn operator_assign_pmutPrivateCopy(&self, o: Ptr) -> Ptr { + let __rhs = (*(*o.upgrade().deref()).v.borrow()); + (*(*(*self).upgrade().deref()).v.borrow_mut()) = __rhs; + (*(*o.upgrade().deref()).v.borrow_mut()) = 0; + return (*self).clone(); + } +} diff --git a/tests/unit/out/unsafe/copy_move_defaulted.rs b/tests/unit/out/unsafe/copy_move_defaulted.rs new file mode 100644 index 00000000..5dd64320 --- /dev/null +++ b/tests/unit/out/unsafe/copy_move_defaulted.rs @@ -0,0 +1,296 @@ +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(Copy, Clone, Default)] +pub struct Inner { + pub x: i32, +} +#[repr(C)] +#[derive(Clone)] +pub struct Explicit { + pub v: i32, + pub inner: Inner, + pub arr: [i32; 2], +} +impl Explicit { + pub unsafe fn Explicit(mut v: i32) -> Self { + let mut this = Self { + v: v, + inner: Inner { x: ((v) * (10)) }, + arr: [v, ((v) + (1))], + }; + this + } + pub unsafe fn destructor(&mut self) {} +} +impl Default for Explicit { + fn default() -> Self { + Explicit { + v: 0_i32, + inner: ::default(), + arr: [0_i32; 2], + } + } +} +#[repr(C)] +#[derive(Copy, Clone)] +pub struct Implicit { + pub v: i32, + pub inner: Inner, + pub arr: [i32; 2], +} +impl Default for Implicit { + fn default() -> Self { + Implicit { + v: 0_i32, + inner: ::default(), + arr: [0_i32; 2], + } + } +} +#[repr(C)] +#[derive(Copy, Clone, Default)] +pub struct DefaultCopyUserMove { + pub v: i32, +} +impl DefaultCopyUserMove { + pub unsafe fn DefaultCopyUserMove(mut v: i32) -> Self { + let mut this = Self { v: v }; + this + } + pub unsafe fn DefaultCopyUserMove_pmutDefaultCopyUserMove(o: *mut DefaultCopyUserMove) -> Self { + let mut this = Self { v: (*o).v }; + (*o).v = 0; + this + } + pub unsafe fn operator_assign_pmutDefaultCopyUserMove( + &mut self, + o: *mut DefaultCopyUserMove, + ) -> *mut DefaultCopyUserMove { + self.v = (*o).v; + (*o).v = 0; + return &mut (*(self as *mut DefaultCopyUserMove)) as *mut DefaultCopyUserMove; + } +} +#[repr(C)] +#[derive(Default)] +pub struct UserCopyDefaultMove { + pub v: i32, +} +impl UserCopyDefaultMove { + pub unsafe fn UserCopyDefaultMove(mut v: i32) -> Self { + let mut this = Self { v: v }; + this + } + pub unsafe fn UserCopyDefaultMove_pconstUserCopyDefaultMove( + o: *const UserCopyDefaultMove, + ) -> Self { + let mut this = Self { + v: (((*o).v) + (100)), + }; + this + } + pub unsafe fn UserCopyDefaultMove_pmutUserCopyDefaultMove( + _a0: *mut UserCopyDefaultMove, + ) -> Self { + let mut this = Self { v: (*_a0).v }; + this + } + pub unsafe fn operator_assign_pconstUserCopyDefaultMove( + &mut self, + o: *const UserCopyDefaultMove, + ) -> *mut UserCopyDefaultMove { + self.v = (((*o).v) + (100)); + return &mut (*(self as *mut UserCopyDefaultMove)) as *mut UserCopyDefaultMove; + } + pub unsafe fn operator_assign_pmutUserCopyDefaultMove( + &mut self, + _a0: *mut UserCopyDefaultMove, + ) -> *mut UserCopyDefaultMove { + self.v = (*_a0).v; + return &mut (*(self as *mut UserCopyDefaultMove)) as *mut UserCopyDefaultMove; + } +} +impl Clone for UserCopyDefaultMove { + fn clone(&self) -> Self { + unsafe { + UserCopyDefaultMove::UserCopyDefaultMove_pconstUserCopyDefaultMove( + self as *const UserCopyDefaultMove, + ) + } + } +} +#[repr(C)] +#[derive()] +pub struct Buffer { + pub data: Vec, + pub n: i32, + pub arr: [i32; 2], +} +impl Buffer { + pub unsafe fn Buffer(mut n: i32) -> Self { + let mut this = Self { + data: vec![n; (n as usize) as usize], + n: n, + arr: [n, ((n) + (1))], + }; + this + } + pub unsafe fn Buffer_pmutBuffer(_a0: *mut Buffer) -> Self { + let mut this = Self { + data: std::mem::take(&mut (*_a0).data), + n: (*_a0).n, + arr: std::array::from_fn::<_, 2, _>(|__i: usize| (*_a0).arr[(__i)]), + }; + this + } + pub unsafe fn operator_assign_pmutBuffer(&mut self, _a0: *mut Buffer) -> *mut Buffer { + self.data = std::mem::take(&mut (*_a0).data); + self.n = (*_a0).n; + { + if 8_usize != 0 { + ::std::ptr::copy_nonoverlapping( + ((&mut (*_a0).arr as *mut [i32; 2]) as *const [i32; 2] + as *const ::libc::c_void), + ((&mut self.arr as *mut [i32; 2]) as *mut [i32; 2] as *mut ::libc::c_void), + 8_usize as usize, + ) + } + ((&mut self.arr as *mut [i32; 2]) as *mut [i32; 2] as *mut ::libc::c_void) + }; + return &mut (*(self as *mut Buffer)) as *mut Buffer; + } +} +impl Default for Buffer { + fn default() -> Self { + Buffer { + data: Default::default(), + n: 0_i32, + arr: [0_i32; 2], + } + } +} +pub unsafe fn same_0(a: *const Explicit, b: *const Explicit) -> bool { + return (((((*a).v) == ((*b).v)) && (((*a).inner.x) == ((*b).inner.x))) + && (((*a).arr[(0) as usize]) == ((*b).arr[(0) as usize]))) + && (((*a).arr[(1) as usize]) == ((*b).arr[(1) as usize])); +} +pub fn main() { + unsafe { + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + let mut a: Explicit = Explicit::Explicit({ 1 }); + let _dtor_a = ScopedDestructorUnsafe::new(&raw mut a, Explicit::destructor); + let mut b: Explicit = a.clone(); + let _dtor_b = ScopedDestructorUnsafe::new(&raw mut b, Explicit::destructor); + let mut c: Explicit = a.clone(); + let _dtor_c = ScopedDestructorUnsafe::new(&raw mut c, Explicit::destructor); + let mut d: Explicit = a.clone(); + let _dtor_d = ScopedDestructorUnsafe::new(&raw mut d, Explicit::destructor); + assert!( + ((unsafe { same_0(&b as *const Explicit, &a as *const Explicit,) }) + && (unsafe { same_0(&c as *const Explicit, &a as *const Explicit,) })) + && (unsafe { same_0(&d as *const Explicit, &a as *const Explicit,) }) + ); + let mut e: Explicit = Explicit::Explicit({ 2 }); + let _dtor_e = ScopedDestructorUnsafe::new(&raw mut e, Explicit::destructor); + let mut f: Explicit = Explicit::Explicit({ 3 }); + let _dtor_f = ScopedDestructorUnsafe::new(&raw mut f, Explicit::destructor); + e = b; + f = c; + assert!( + (unsafe { same_0(&e as *const Explicit, &b as *const Explicit,) }) + && (unsafe { same_0(&f as *const Explicit, &c as *const Explicit,) }) + ); + let mut g: Explicit = Explicit::Explicit({ 4 }); + let _dtor_g = ScopedDestructorUnsafe::new(&raw mut g, Explicit::destructor); + g = { + e = f; + e + }; + assert!( + (unsafe { same_0(&g as *const Explicit, &f as *const Explicit,) }) + && (unsafe { same_0(&e as *const Explicit, &f as *const Explicit,) }) + ); + let mut i: Implicit = Implicit { + v: 5, + inner: Inner { x: 50 }, + arr: [5, 6], + }; + let mut j: Implicit = i; + let mut k: Implicit = i; + assert!((((j.v) == (5)) && ((j.inner.x) == (50))) && ((j.arr[(1) as usize]) == (6))); + assert!(((i.v) == (5)) && ((k.v) == (5))); + let mut l: Implicit = Implicit { + v: 0, + inner: Inner { x: 0 }, + arr: [0, 0], + }; + l = j; + assert!((((l.v) == (5)) && ((l.inner.x) == (50))) && ((l.arr[(0) as usize]) == (5))); + let mut vec_: Vec = Vec::new(); + { + let a0_clone = b.clone(); + vec_.push(a0_clone) + }; + vec_.push(Explicit::Explicit({ 9 })); + assert!(((vec_[(0_usize)].v) == (1)) && ((vec_[(1_usize)].v) == (9))); + let mut m: DefaultCopyUserMove = DefaultCopyUserMove::DefaultCopyUserMove({ 7 }); + let mut m1: DefaultCopyUserMove = m; + let mut m2: DefaultCopyUserMove = + DefaultCopyUserMove::DefaultCopyUserMove_pmutDefaultCopyUserMove({ &mut m }); + assert!((((m1.v) == (7)) && ((m2.v) == (7))) && ((m.v) == (0))); + let mut m3: DefaultCopyUserMove = DefaultCopyUserMove::DefaultCopyUserMove({ 1 }); + let mut m4: DefaultCopyUserMove = DefaultCopyUserMove::DefaultCopyUserMove({ 1 }); + m3 = m1; + (unsafe { DefaultCopyUserMove::operator_assign_pmutDefaultCopyUserMove(&mut m4, &mut m1) }); + assert!((((m3.v) == (7)) && ((m4.v) == (7))) && ((m1.v) == (0))); + let mut u: UserCopyDefaultMove = UserCopyDefaultMove::UserCopyDefaultMove({ 8 }); + let mut u1: UserCopyDefaultMove = + UserCopyDefaultMove::UserCopyDefaultMove_pconstUserCopyDefaultMove({ + &u as *const UserCopyDefaultMove + }); + let mut u2: UserCopyDefaultMove = + UserCopyDefaultMove::UserCopyDefaultMove_pmutUserCopyDefaultMove({ &mut u }); + assert!((((u1.v) == (108)) && ((u2.v) == (8))) && ((u.v) == (8))); + let mut u3: UserCopyDefaultMove = UserCopyDefaultMove::UserCopyDefaultMove({ 1 }); + let mut u4: UserCopyDefaultMove = UserCopyDefaultMove::UserCopyDefaultMove({ 1 }); + (unsafe { + UserCopyDefaultMove::operator_assign_pconstUserCopyDefaultMove( + &mut u3, + &u2 as *const UserCopyDefaultMove, + ) + }); + (unsafe { UserCopyDefaultMove::operator_assign_pmutUserCopyDefaultMove(&mut u4, &mut u2) }); + assert!(((u3.v) == (108)) && ((u4.v) == (8))); + let mut p: Buffer = Buffer::Buffer({ 3 }); + let mut q: Buffer = Buffer::Buffer_pmutBuffer({ &mut p }); + assert!( + ((((q.n) == (3)) && ((q.data.len()) == (3_usize))) && ((q.data[(2_usize)]) == (3))) + && (p.data.is_empty()) + ); + let mut r: Buffer = Buffer::Buffer({ 1 }); + (unsafe { Buffer::operator_assign_pmutBuffer(&mut r, &mut q) }); + assert!( + ((((r.n) == (3)) && ((r.data.len()) == (3_usize))) && ((r.arr[(1) as usize]) == (4))) + && (q.data.is_empty()) + ); + let mut bufs: Vec = Vec::new(); + bufs.push(std::mem::take(&mut r)); + bufs.push(std::mem::take(&mut Buffer::Buffer_pmutBuffer({ + &mut bufs[(0_usize)] + }))); + assert!( + (((bufs[(1_usize)].n) == (3)) && ((bufs[(1_usize)].data.len()) == (3_usize))) + && (bufs[(0_usize)].data.is_empty()) + ); + return 0; +} diff --git a/tests/unit/out/unsafe/copy_move_deleted.rs b/tests/unit/out/unsafe/copy_move_deleted.rs new file mode 100644 index 00000000..468e58c6 --- /dev/null +++ b/tests/unit/out/unsafe/copy_move_deleted.rs @@ -0,0 +1,135 @@ +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(Default)] +pub struct NoCopy { + pub v: i32, +} +impl NoCopy { + pub unsafe fn NoCopy(mut v: i32) -> Self { + let mut this = Self { v: v }; + this + } + pub unsafe fn NoCopy_pmutNoCopy(o: *mut NoCopy) -> Self { + let mut this = Self { v: (*o).v }; + (*o).v = 0; + this + } + pub unsafe fn operator_assign_pmutNoCopy(&mut self, o: *mut NoCopy) -> *mut NoCopy { + self.v = (*o).v; + (*o).v = 0; + return &mut (*(self as *mut NoCopy)) as *mut NoCopy; + } +} +#[repr(C)] +#[derive()] +pub struct PrivateCopy { + pub v: i32, +} +impl PrivateCopy { + pub unsafe fn PrivateCopy() -> Self { + let mut this = Self { v: 0 }; + this + } + pub unsafe fn PrivateCopy_pmutPrivateCopy(o: *mut PrivateCopy) -> Self { + let mut this = Self { v: (*o).v }; + (*o).v = 0; + this + } + pub unsafe fn operator_assign_pmutPrivateCopy( + &mut self, + o: *mut PrivateCopy, + ) -> *mut PrivateCopy { + self.v = (*o).v; + (*o).v = 0; + return &mut (*(self as *mut PrivateCopy)) as *mut PrivateCopy; + } +} +impl Default for PrivateCopy { + fn default() -> Self { + unsafe { PrivateCopy::PrivateCopy() } + } +} +#[repr(C)] +#[derive()] +pub struct Immovable { + pub v: i32, +} +impl Immovable { + pub unsafe fn Immovable() -> Self { + let mut this = Self { v: 0 }; + this + } +} +impl Default for Immovable { + fn default() -> Self { + unsafe { Immovable::Immovable() } + } +} +#[repr(C)] +#[derive(Default)] +pub struct Container { + pub inner: NoCopy, + pub tag: i32, +} +impl Container { + pub unsafe fn Container_pmutContainer(_a0: *mut Container) -> Self { + let mut this = Self { + inner: NoCopy::NoCopy_pmutNoCopy({ &mut (*_a0).inner as *mut NoCopy }), + tag: (*_a0).tag, + }; + this + } + pub unsafe fn operator_assign_pmutContainer(&mut self, _a0: *mut Container) -> *mut Container { + (unsafe { + let _o: *mut NoCopy = &mut (*_a0).inner as *mut NoCopy; + NoCopy::operator_assign_pmutNoCopy(&mut self.inner, _o) + }); + self.tag = (*_a0).tag; + return &mut (*(self as *mut Container)) as *mut Container; + } +} +pub unsafe fn bump_0(mut p: *mut NoCopy) { + (*p).v.postfix_inc(); +} +pub unsafe fn bump_ref_1(r: *mut Immovable) { + (*r).v.postfix_inc(); +} +pub fn main() { + unsafe { + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + let mut a: NoCopy = NoCopy::NoCopy({ 1 }); + let mut b: NoCopy = NoCopy::NoCopy_pmutNoCopy({ &mut a }); + assert!(((b.v) == (1)) && ((a.v) == (0))); + (unsafe { NoCopy::operator_assign_pmutNoCopy(&mut a, &mut b) }); + assert!(((a.v) == (1)) && ((b.v) == (0))); + (unsafe { bump_0((&mut a as *mut NoCopy)) }); + assert!(((a.v) == (2))); + let mut p: PrivateCopy = PrivateCopy::PrivateCopy(); + p.v = 3; + let mut q: PrivateCopy = PrivateCopy::PrivateCopy_pmutPrivateCopy({ &mut p }); + assert!(((q.v) == (3)) && ((p.v) == (0))); + (unsafe { PrivateCopy::operator_assign_pmutPrivateCopy(&mut p, &mut q) }); + assert!(((p.v) == (3)) && ((q.v) == (0))); + let mut im: Immovable = Immovable::Immovable(); + im.v = 4; + (unsafe { bump_ref_1(&mut im as *mut Immovable) }); + let mut pim: *mut Immovable = (&mut im as *mut Immovable); + assert!((((*pim).v) == (5))); + let mut c: Container = Container { + inner: NoCopy::NoCopy({ 6 }), + tag: 7, + }; + let mut d: Container = Container::Container_pmutContainer({ &mut c }); + assert!((((d.inner.v) == (6)) && ((d.tag) == (7))) && ((c.inner.v) == (0))); + return 0; +} From 253fa5a5969fbf201c6b914824ffd2f9a31d8338 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Mon, 14 Sep 2026 20:25:18 +0100 Subject: [PATCH 04/16] Fix freshness Related to https://github.com/Cpp2Rust/cpp2rust/issues/363 --- cpp2rust/converter/converter.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cpp2rust/converter/converter.cpp b/cpp2rust/converter/converter.cpp index 304e1cf0..e84c2a5f 100644 --- a/cpp2rust/converter/converter.cpp +++ b/cpp2rust/converter/converter.cpp @@ -2925,6 +2925,7 @@ bool Converter::VisitDeclRefExpr(clang::DeclRefExpr *expr) { } StrCat(str); + SetValueFreshness(expr->getType()); return false; } @@ -3054,6 +3055,7 @@ bool Converter::VisitMemberExpr(clang::MemberExpr *expr) { } StrCat(str); + SetValueFreshness(expr->getType()); return false; } From b3f806e0b47861cf4c1024248483f1a931d33865 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Mon, 14 Sep 2026 20:26:04 +0100 Subject: [PATCH 05/16] Fix borrow error in plugin --- cpp2rust/converter/converter.h | 4 +- .../converter/models/converter_refcount.cpp | 16 ++-- .../converter/models/converter_refcount.h | 4 +- cpp2rust/converter/plugins/emplace_back.cpp | 81 ++++++++++--------- 4 files changed, 56 insertions(+), 49 deletions(-) diff --git a/cpp2rust/converter/converter.h b/cpp2rust/converter/converter.h index cc4241f5..b14300b8 100644 --- a/cpp2rust/converter/converter.h +++ b/cpp2rust/converter/converter.h @@ -1001,8 +1001,8 @@ class Converter : public clang::RecursiveASTVisitor { virtual bool emplace_back_plugin_convert(clang::CallExpr *call); virtual void emplace_back_plugin_construct_arg(clang::QualType elem_type, clang::CXXConstructExpr *ctor); - virtual void emplace_back_emit_push_open(clang::CXXMemberCallExpr *call); - virtual void emplace_back_emit_push_close(clang::CXXMemberCallExpr *call); + virtual void emplace_back_emit_push(clang::CXXMemberCallExpr *call, + std::string_view arg); virtual const char *GetPointerDerefPrefix(clang::QualType pointee_type); diff --git a/cpp2rust/converter/models/converter_refcount.cpp b/cpp2rust/converter/models/converter_refcount.cpp index 5ea24ff8..dbe02193 100644 --- a/cpp2rust/converter/models/converter_refcount.cpp +++ b/cpp2rust/converter/models/converter_refcount.cpp @@ -2510,20 +2510,18 @@ void ConverterRefCount::emplace_back_plugin_construct_arg( ConvertVarInit(elem_type, ctor); } -void ConverterRefCount::emplace_back_emit_push_open( - clang::CXXMemberCallExpr *call) { +void ConverterRefCount::emplace_back_emit_push(clang::CXXMemberCallExpr *call, + std::string_view arg) { auto *obj = GetCallObject(call); auto obj_type = obj->getType().getNonReferenceType(); if (obj_type->isPointerType()) { obj_type = obj_type->getPointeeType(); } - StrCat(ConvertObject(obj), ".with_mut(|__v: &mut ", - ToString(obj_type.getNonReferenceType()), "| __v.push("); -} - -void ConverterRefCount::emplace_back_emit_push_close( - clang::CXXMemberCallExpr *call) { - StrCat("))"); + StrCat(ConvertObject(obj), ".with_mut"); + PushParen outer(*this); + StrCat("|__v: &mut ", ToString(obj_type.getNonReferenceType()), "| __v.push"); + PushParen inner(*this); + StrCat(arg); } const char * diff --git a/cpp2rust/converter/models/converter_refcount.h b/cpp2rust/converter/models/converter_refcount.h index 35748f58..9d26f462 100644 --- a/cpp2rust/converter/models/converter_refcount.h +++ b/cpp2rust/converter/models/converter_refcount.h @@ -240,8 +240,8 @@ class ConverterRefCount final : public Converter { void emplace_back_plugin_construct_arg(clang::QualType elem_type, clang::CXXConstructExpr *ctor) override; - void emplace_back_emit_push_open(clang::CXXMemberCallExpr *call) override; - void emplace_back_emit_push_close(clang::CXXMemberCallExpr *call) override; + void emplace_back_emit_push(clang::CXXMemberCallExpr *call, + std::string_view arg) override; const char *GetPointerDerefSuffix(clang::QualType pointee_type); const char *GetPointerDerefPrefix(clang::QualType pointee_type) override; diff --git a/cpp2rust/converter/plugins/emplace_back.cpp b/cpp2rust/converter/plugins/emplace_back.cpp index c7e6e1e8..16b294e3 100644 --- a/cpp2rust/converter/plugins/emplace_back.cpp +++ b/cpp2rust/converter/plugins/emplace_back.cpp @@ -3,6 +3,8 @@ #include +#include + #include "converter/converter_lib.h" #include "converter/mapper.h" #include "converter/models/converter_refcount.h" @@ -134,18 +136,16 @@ clang::CXXConstructExpr *buildConstructExpr(clang::CXXMemberCallExpr *call, } // namespace -void Converter::emplace_back_emit_push_open(clang::CXXMemberCallExpr *call) { +void Converter::emplace_back_emit_push(clang::CXXMemberCallExpr *call, + std::string_view arg) { { PushExprKind push(*this, ExprKind::LValue); auto callee = ToString(call->getCallee()); ReplaceAll(callee, "emplace_back", "push"); StrCat(callee); } - StrCat('('); -} - -void Converter::emplace_back_emit_push_close(clang::CXXMemberCallExpr *call) { - StrCat(')'); + PushParen paren(*this); + StrCat(arg); } bool Converter::emplace_back_plugin_convert(clang::CallExpr *call) { @@ -155,41 +155,50 @@ bool Converter::emplace_back_plugin_convert(clang::CallExpr *call) { auto [elem_ty, ctor] = analyzeEmplaceCall(member_call, GetSema()); assert(!elem_ty.isNull() && "Could not analyze emplace_back type"); - emplace_back_emit_push_open(member_call); - - if (ctor) { - auto is_argument_moved = false; - if (call->getNumArgs() > 0) { - if (auto arg_call = clang::dyn_cast(call->getArg(0))) { - is_argument_moved = arg_call->isCallToStdMove(); + std::string arg; + { + Buffer buf(*this); + if (ctor) { + auto *construct = buildConstructExpr(member_call, GetSema()); + auto is_argument_moved = + construct && construct->getConstructor()->isMoveConstructor() && + !IsConvertibleMoveConstructor(construct->getConstructor()); + + if (is_argument_moved) { + StrCat("std::mem::take(&mut"); + } + emplace_back_plugin_construct_arg(elem_ty, construct); + if (is_argument_moved) { + StrCat(')'); + } + } else if (elem_ty.isPODType(ctx_)) { + if (call->getNumArgs() == 0) { + StrCat(GetDefaultAsString(elem_ty)); + } else { + assert(call->getNumArgs() == 1 && + "multiple arguments passed for building POD type"); + Convert(call->getArg(0)); + StrCat("as"); + StrCat(GetUnsafeTypeAsString(elem_ty)); } - } - - if (is_argument_moved) { - StrCat("std::mem::take(&mut"); - } - emplace_back_plugin_construct_arg( - elem_ty, buildConstructExpr(member_call, GetSema())); - if (is_argument_moved) { - StrCat(')'); - } - } else if (elem_ty.isPODType(ctx_)) { - if (call->getNumArgs() == 0) { - StrCat(GetDefaultAsString(elem_ty)); } else { - assert(call->getNumArgs() == 1 && - "multiple arguments passed for building POD type"); - Convert(call->getArg(0)); - StrCat("as"); - StrCat(GetUnsafeTypeAsString(elem_ty)); + call->dump(); + assert(0 && "no ctor and no pod type"); + return false; } - } else { - call->dump(); - assert(0 && "no ctor and no pod type"); - return false; + arg = std::move(buf).str(); } - emplace_back_emit_push_close(member_call); + auto *obj = GetCallObject(member_call); + bool hoist = std::any_of( + call->arg_begin(), call->arg_end(), + [obj](auto *call_arg) { return MayCauseBorrowMutError(obj, call_arg); }); + PushBrace brace(*this, hoist); + if (hoist) { + StrCat("let __arg = ", arg, ";"); + arg = "__arg"; + } + emplace_back_emit_push(member_call, arg); return true; } From 01be90cc4dd63c022fca88cb64b0f4c798709126 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Mon, 14 Sep 2026 20:26:31 +0100 Subject: [PATCH 06/16] Update tests --- cpp2rust/converter/plugins/emplace_back.cpp | 7 ++++--- tests/unit/out/refcount/copy_move_defaulted.rs | 11 ++++++----- tests/unit/out/refcount/push_emplace_back.rs | 16 ++++++++-------- tests/unit/out/unsafe/array_reference.rs | 2 +- tests/unit/out/unsafe/copy_move_defaulted.rs | 15 ++++++++------- tests/unit/out/unsafe/push_emplace_back.rs | 5 ++++- .../out/unsafe/redundant_copy_in_conversion.rs | 2 +- 7 files changed, 32 insertions(+), 26 deletions(-) diff --git a/cpp2rust/converter/plugins/emplace_back.cpp b/cpp2rust/converter/plugins/emplace_back.cpp index 16b294e3..2c194672 100644 --- a/cpp2rust/converter/plugins/emplace_back.cpp +++ b/cpp2rust/converter/plugins/emplace_back.cpp @@ -190,9 +190,10 @@ bool Converter::emplace_back_plugin_convert(clang::CallExpr *call) { } auto *obj = GetCallObject(member_call); - bool hoist = std::any_of( - call->arg_begin(), call->arg_end(), - [obj](auto *call_arg) { return MayCauseBorrowMutError(obj, call_arg); }); + bool hoist = + std::any_of(call->arg_begin(), call->arg_end(), [obj](auto *call_arg) { + return MayCauseBorrowMutError(obj, call_arg); + }); PushBrace brace(*this, hoist); if (hoist) { StrCat("let __arg = ", arg, ";"); diff --git a/tests/unit/out/refcount/copy_move_defaulted.rs b/tests/unit/out/refcount/copy_move_defaulted.rs index 5c39532a..9341c27b 100644 --- a/tests/unit/out/refcount/copy_move_defaulted.rs +++ b/tests/unit/out/refcount/copy_move_defaulted.rs @@ -481,11 +481,12 @@ fn main_0() -> i32 { ); let bufs: Value> = Rc::new(RefCell::new(Vec::new())); (*bufs.borrow_mut()).push(std::mem::take(&mut (*r.borrow_mut()))); - bufs.as_pointer().with_mut(|__v: &mut Vec| { - __v.push(std::mem::take(&mut Buffer::Buffer_pmutBuffer({ - (bufs.as_pointer() as Ptr).offset(0_usize) - }))) - }); + { + let __arg = + Buffer::Buffer_pmutBuffer({ (bufs.as_pointer() as Ptr).offset(0_usize) }); + bufs.as_pointer() + .with_mut(|__v: &mut Vec| __v.push(__arg)) + }; assert!( (((*(*(bufs.as_pointer() as Ptr) .offset(1_usize) diff --git a/tests/unit/out/refcount/push_emplace_back.rs b/tests/unit/out/refcount/push_emplace_back.rs index e547adac..dbac1e7a 100644 --- a/tests/unit/out/refcount/push_emplace_back.rs +++ b/tests/unit/out/refcount/push_emplace_back.rs @@ -178,14 +178,14 @@ pub fn emplace_local_from_field_4(jpg: Ptr, cond: bool) { } pub fn nested_emplace_move_5(bw: Ptr) { let bw: Value> = Rc::new(RefCell::new(bw)); - (*(*(*bw.borrow()).upgrade().deref()).output.borrow()) - .to_strong() - .as_pointer() - .with_mut(|__v: &mut Vec| { - __v.push(std::mem::take( - &mut (*(*(*bw.borrow()).upgrade().deref()).chunk.borrow()).clone(), - )) - }); + { + let __arg = + std::mem::take(&mut (*(*(*bw.borrow()).upgrade().deref()).chunk.borrow()).clone()); + (*(*(*bw.borrow()).upgrade().deref()).output.borrow()) + .to_strong() + .as_pointer() + .with_mut(|__v: &mut Vec| __v.push(__arg)) + }; } pub fn self_ref_push_6(comps: Ptr>) { let comps: Value>> = Rc::new(RefCell::new(comps)); diff --git a/tests/unit/out/unsafe/array_reference.rs b/tests/unit/out/unsafe/array_reference.rs index bdf9acd1..a1f6c56e 100644 --- a/tests/unit/out/unsafe/array_reference.rs +++ b/tests/unit/out/unsafe/array_reference.rs @@ -39,7 +39,7 @@ pub unsafe fn fill_and_sum_5(a: *mut [i32; 3], mut v: i32, out: *mut i32) { let _v: i32 = v; fill_3(_a, _v) }); - (*out) = (unsafe { sum_twice_4(a) }); + (*out) = (unsafe { sum_twice_4(a) }).clone(); } pub unsafe fn pick_6(s: *const [libc::c_char; 5]) -> *const [libc::c_char; 5] { return s; diff --git a/tests/unit/out/unsafe/copy_move_defaulted.rs b/tests/unit/out/unsafe/copy_move_defaulted.rs index 5dd64320..2a69196a 100644 --- a/tests/unit/out/unsafe/copy_move_defaulted.rs +++ b/tests/unit/out/unsafe/copy_move_defaulted.rs @@ -204,8 +204,8 @@ unsafe fn main_0() -> i32 { let _dtor_e = ScopedDestructorUnsafe::new(&raw mut e, Explicit::destructor); let mut f: Explicit = Explicit::Explicit({ 3 }); let _dtor_f = ScopedDestructorUnsafe::new(&raw mut f, Explicit::destructor); - e = b; - f = c; + e = (b).clone(); + f = (c).clone(); assert!( (unsafe { same_0(&e as *const Explicit, &b as *const Explicit,) }) && (unsafe { same_0(&f as *const Explicit, &c as *const Explicit,) }) @@ -213,8 +213,8 @@ unsafe fn main_0() -> i32 { let mut g: Explicit = Explicit::Explicit({ 4 }); let _dtor_g = ScopedDestructorUnsafe::new(&raw mut g, Explicit::destructor); g = { - e = f; - e + e = (f).clone(); + (e).clone() }; assert!( (unsafe { same_0(&g as *const Explicit, &f as *const Explicit,) }) @@ -285,9 +285,10 @@ unsafe fn main_0() -> i32 { ); let mut bufs: Vec = Vec::new(); bufs.push(std::mem::take(&mut r)); - bufs.push(std::mem::take(&mut Buffer::Buffer_pmutBuffer({ - &mut bufs[(0_usize)] - }))); + { + let __arg = Buffer::Buffer_pmutBuffer({ &mut bufs[(0_usize)] }); + bufs.push(__arg) + }; assert!( (((bufs[(1_usize)].n) == (3)) && ((bufs[(1_usize)].data.len()) == (3_usize))) && (bufs[(0_usize)].data.is_empty()) diff --git a/tests/unit/out/unsafe/push_emplace_back.rs b/tests/unit/out/unsafe/push_emplace_back.rs index 9c295cd6..ef3c134b 100644 --- a/tests/unit/out/unsafe/push_emplace_back.rs +++ b/tests/unit/out/unsafe/push_emplace_back.rs @@ -69,7 +69,10 @@ pub unsafe fn emplace_local_from_field_4(mut jpg: *mut JPEGData, mut cond: bool) ); } pub unsafe fn nested_emplace_move_5(mut bw: *mut Writer) { - (*(*bw).output).push(std::mem::take(&mut (*bw).chunk)); + { + let __arg = std::mem::take(&mut (*bw).chunk); + (*(*bw).output).push(__arg) + }; } pub unsafe fn self_ref_push_6(mut comps: *mut Vec) { { diff --git a/tests/unit/out/unsafe/redundant_copy_in_conversion.rs b/tests/unit/out/unsafe/redundant_copy_in_conversion.rs index f2809b67..46caad85 100644 --- a/tests/unit/out/unsafe/redundant_copy_in_conversion.rs +++ b/tests/unit/out/unsafe/redundant_copy_in_conversion.rs @@ -24,7 +24,7 @@ unsafe fn main_0() -> i32 { UnsafeMapIterator::find_key(&m as *const BTreeMap>, &0); let mut const_it: UnsafeMapIterator = it0.clone(); let mut r: i32 = if const_it == end.clone() { 0 } else { 1 }; - r += (unsafe { sink_0(it0.clone()) }); + r += (unsafe { sink_0(it0.clone()) }).clone(); r += if end == end { 0 } else { 1 }; assert!(((r) == (2))); return 0; From 52196ae3f3fb235c984d9d61a7dd68288914fd02 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Mon, 14 Sep 2026 20:30:36 +0100 Subject: [PATCH 07/16] Make std::move transparent --- cpp2rust/converter/converter.cpp | 2 +- cpp2rust/converter/converter_lib.cpp | 17 +++++++++++ cpp2rust/converter/converter_lib.h | 4 +++ .../converter/models/converter_refcount.cpp | 2 +- cpp2rust/converter/plugins/emplace_back.cpp | 14 ++-------- .../out/unsafe/defaulted_move_cross_tu.rs | 4 +-- tests/unit/out/refcount/push_emplace_back.rs | 3 +- tests/unit/out/unsafe/copy_ctor.rs | 2 +- tests/unit/out/unsafe/copy_move_defaulted.rs | 28 ++++++++++++++----- tests/unit/out/unsafe/copy_move_deleted.rs | 11 ++++---- tests/unit/out/unsafe/move_assign.rs | 28 ++++++++++++++----- tests/unit/out/unsafe/move_ctor.rs | 17 ++++++----- tests/unit/out/unsafe/move_this.rs | 6 ++-- tests/unit/out/unsafe/push_emplace_back.rs | 2 +- tests/unit/out/unsafe/rule_of_five.rs | 8 +++--- tests/unit/out/unsafe/rule_of_three.rs | 4 +-- tests/unit/out/unsafe/rvalue_ref_general.rs | 2 +- tests/unit/out/unsafe/rvalue_struct.rs | 2 +- 18 files changed, 99 insertions(+), 57 deletions(-) diff --git a/cpp2rust/converter/converter.cpp b/cpp2rust/converter/converter.cpp index e84c2a5f..4cd62534 100644 --- a/cpp2rust/converter/converter.cpp +++ b/cpp2rust/converter/converter.cpp @@ -2681,7 +2681,7 @@ void Converter::ConvertGenericBinaryOperator(clang::BinaryOperator *expr) { } bool Converter::IsReferenceType(const clang::Expr *expr) const { - const auto *e = expr->IgnoreCasts(); + const auto *e = IgnoreStdMove(expr->IgnoreCasts())->IgnoreCasts(); if (const auto *call = clang::dyn_cast(e)) { return !clang::isa(call) && GetReturnTypeOfFunction(call)->isReferenceType(); diff --git a/cpp2rust/converter/converter_lib.cpp b/cpp2rust/converter/converter_lib.cpp index baa3ef2b..d531eedf 100644 --- a/cpp2rust/converter/converter_lib.cpp +++ b/cpp2rust/converter/converter_lib.cpp @@ -1358,6 +1358,23 @@ bool IsBuiltinVaCopy(const clang::CallExpr *expr) { return false; } +const clang::Expr *IgnoreStdMove(const clang::Expr *expr) { + if (const auto *call = + clang::dyn_cast(expr->IgnoreParenImpCasts()); + call && call->isCallToStdMove()) { + return call->getArg(0); + } + return expr; +} + +bool IsTemporaryObject(const clang::Expr *expr) { + const auto *operand = IgnoreStdMove(expr); + if (operand != expr) { + return !operand->isGLValue(); + } + return !expr->isLValue(); +} + bool ContainsVAArgExpr(const clang::Stmt *stmt) { if (clang::isa(stmt)) { return true; diff --git a/cpp2rust/converter/converter_lib.h b/cpp2rust/converter/converter_lib.h index 52aae4d0..d503cd92 100644 --- a/cpp2rust/converter/converter_lib.h +++ b/cpp2rust/converter/converter_lib.h @@ -246,6 +246,10 @@ bool IsBuiltinVaEnd(const clang::CallExpr *expr); bool IsBuiltinVaCopy(const clang::CallExpr *expr); +const clang::Expr *IgnoreStdMove(const clang::Expr *expr); + +bool IsTemporaryObject(const clang::Expr *expr); + bool ContainsVAArgExpr(const clang::Stmt *stmt); clang::Expr *NormalizeToBool(clang::Expr *expr, clang::ASTContext &ctx); diff --git a/cpp2rust/converter/models/converter_refcount.cpp b/cpp2rust/converter/models/converter_refcount.cpp index dbe02193..98212436 100644 --- a/cpp2rust/converter/models/converter_refcount.cpp +++ b/cpp2rust/converter/models/converter_refcount.cpp @@ -2662,7 +2662,7 @@ void ConverterRefCount::SetUFCSReceiver(clang::Expr *base, bool is_arrow, } return; } - if (!base->isLValue() && base->getType()->isRecordType() && + if (IsTemporaryObject(base) && base->getType()->isRecordType() && !IsReferenceType(base->IgnoreImplicit())) { PushConversionKind push(*this, ConversionKind::FullRefCount); ufcs_receiver_ = diff --git a/cpp2rust/converter/plugins/emplace_back.cpp b/cpp2rust/converter/plugins/emplace_back.cpp index 2c194672..f766ea95 100644 --- a/cpp2rust/converter/plugins/emplace_back.cpp +++ b/cpp2rust/converter/plugins/emplace_back.cpp @@ -159,18 +159,8 @@ bool Converter::emplace_back_plugin_convert(clang::CallExpr *call) { { Buffer buf(*this); if (ctor) { - auto *construct = buildConstructExpr(member_call, GetSema()); - auto is_argument_moved = - construct && construct->getConstructor()->isMoveConstructor() && - !IsConvertibleMoveConstructor(construct->getConstructor()); - - if (is_argument_moved) { - StrCat("std::mem::take(&mut"); - } - emplace_back_plugin_construct_arg(elem_ty, construct); - if (is_argument_moved) { - StrCat(')'); - } + emplace_back_plugin_construct_arg( + elem_ty, buildConstructExpr(member_call, GetSema())); } else if (elem_ty.isPODType(ctx_)) { if (call->getNumArgs() == 0) { StrCat(GetDefaultAsString(elem_ty)); diff --git a/tests/multi-file/defaulted_move_cross_tu/out/unsafe/defaulted_move_cross_tu.rs b/tests/multi-file/defaulted_move_cross_tu/out/unsafe/defaulted_move_cross_tu.rs index 1cf9c82c..6210c2cc 100644 --- a/tests/multi-file/defaulted_move_cross_tu/out/unsafe/defaulted_move_cross_tu.rs +++ b/tests/multi-file/defaulted_move_cross_tu/out/unsafe/defaulted_move_cross_tu.rs @@ -66,10 +66,10 @@ unsafe fn main_0() -> i32 { } pub unsafe fn shuffle_1(mut x: i32) -> i32 { let mut a: S = S::S({ x }); - let mut b: S = S::S_pmutS({ &mut a }); + let mut b: S = S::S_pmutS({ &mut a as *mut S }); assert!(a.v.is_empty()); let mut c: S = S::S({ 1 }); - (unsafe { S::operator_assign_pmutS(&mut c, &mut b) }); + (unsafe { S::operator_assign_pmutS(&mut c, &mut b as *mut S) }); assert!(b.v.is_empty()); return (unsafe { sum_0(&c as *const S) }); } diff --git a/tests/unit/out/refcount/push_emplace_back.rs b/tests/unit/out/refcount/push_emplace_back.rs index dbac1e7a..024c032b 100644 --- a/tests/unit/out/refcount/push_emplace_back.rs +++ b/tests/unit/out/refcount/push_emplace_back.rs @@ -179,8 +179,7 @@ pub fn emplace_local_from_field_4(jpg: Ptr, cond: bool) { pub fn nested_emplace_move_5(bw: Ptr) { let bw: Value> = Rc::new(RefCell::new(bw)); { - let __arg = - std::mem::take(&mut (*(*(*bw.borrow()).upgrade().deref()).chunk.borrow()).clone()); + let __arg = (*(*(*bw.borrow()).upgrade().deref()).chunk.borrow()).clone(); (*(*(*bw.borrow()).upgrade().deref()).output.borrow()) .to_strong() .as_pointer() diff --git a/tests/unit/out/unsafe/copy_ctor.rs b/tests/unit/out/unsafe/copy_ctor.rs index 5bbe7906..8df79bb6 100644 --- a/tests/unit/out/unsafe/copy_ctor.rs +++ b/tests/unit/out/unsafe/copy_ctor.rs @@ -80,7 +80,7 @@ pub unsafe fn by_value_1(mut c: Counted) -> i32 { } pub unsafe fn make_2(mut v: i32) -> Counted { let mut c: Counted = Counted::Counted({ v }); - return Counted::Counted_pconstCounted({ &mut c }); + return Counted::Counted_pconstCounted({ &c as *const Counted }); } pub fn main() { unsafe { diff --git a/tests/unit/out/unsafe/copy_move_defaulted.rs b/tests/unit/out/unsafe/copy_move_defaulted.rs index 2a69196a..323f0336 100644 --- a/tests/unit/out/unsafe/copy_move_defaulted.rs +++ b/tests/unit/out/unsafe/copy_move_defaulted.rs @@ -246,12 +246,19 @@ unsafe fn main_0() -> i32 { let mut m: DefaultCopyUserMove = DefaultCopyUserMove::DefaultCopyUserMove({ 7 }); let mut m1: DefaultCopyUserMove = m; let mut m2: DefaultCopyUserMove = - DefaultCopyUserMove::DefaultCopyUserMove_pmutDefaultCopyUserMove({ &mut m }); + DefaultCopyUserMove::DefaultCopyUserMove_pmutDefaultCopyUserMove({ + &mut m as *mut DefaultCopyUserMove + }); assert!((((m1.v) == (7)) && ((m2.v) == (7))) && ((m.v) == (0))); let mut m3: DefaultCopyUserMove = DefaultCopyUserMove::DefaultCopyUserMove({ 1 }); let mut m4: DefaultCopyUserMove = DefaultCopyUserMove::DefaultCopyUserMove({ 1 }); m3 = m1; - (unsafe { DefaultCopyUserMove::operator_assign_pmutDefaultCopyUserMove(&mut m4, &mut m1) }); + (unsafe { + DefaultCopyUserMove::operator_assign_pmutDefaultCopyUserMove( + &mut m4, + &mut m1 as *mut DefaultCopyUserMove, + ) + }); assert!((((m3.v) == (7)) && ((m4.v) == (7))) && ((m1.v) == (0))); let mut u: UserCopyDefaultMove = UserCopyDefaultMove::UserCopyDefaultMove({ 8 }); let mut u1: UserCopyDefaultMove = @@ -259,7 +266,9 @@ unsafe fn main_0() -> i32 { &u as *const UserCopyDefaultMove }); let mut u2: UserCopyDefaultMove = - UserCopyDefaultMove::UserCopyDefaultMove_pmutUserCopyDefaultMove({ &mut u }); + UserCopyDefaultMove::UserCopyDefaultMove_pmutUserCopyDefaultMove({ + &mut u as *mut UserCopyDefaultMove + }); assert!((((u1.v) == (108)) && ((u2.v) == (8))) && ((u.v) == (8))); let mut u3: UserCopyDefaultMove = UserCopyDefaultMove::UserCopyDefaultMove({ 1 }); let mut u4: UserCopyDefaultMove = UserCopyDefaultMove::UserCopyDefaultMove({ 1 }); @@ -269,16 +278,21 @@ unsafe fn main_0() -> i32 { &u2 as *const UserCopyDefaultMove, ) }); - (unsafe { UserCopyDefaultMove::operator_assign_pmutUserCopyDefaultMove(&mut u4, &mut u2) }); + (unsafe { + UserCopyDefaultMove::operator_assign_pmutUserCopyDefaultMove( + &mut u4, + &mut u2 as *mut UserCopyDefaultMove, + ) + }); assert!(((u3.v) == (108)) && ((u4.v) == (8))); let mut p: Buffer = Buffer::Buffer({ 3 }); - let mut q: Buffer = Buffer::Buffer_pmutBuffer({ &mut p }); + let mut q: Buffer = Buffer::Buffer_pmutBuffer({ &mut p as *mut Buffer }); assert!( ((((q.n) == (3)) && ((q.data.len()) == (3_usize))) && ((q.data[(2_usize)]) == (3))) && (p.data.is_empty()) ); let mut r: Buffer = Buffer::Buffer({ 1 }); - (unsafe { Buffer::operator_assign_pmutBuffer(&mut r, &mut q) }); + (unsafe { Buffer::operator_assign_pmutBuffer(&mut r, &mut q as *mut Buffer) }); assert!( ((((r.n) == (3)) && ((r.data.len()) == (3_usize))) && ((r.arr[(1) as usize]) == (4))) && (q.data.is_empty()) @@ -286,7 +300,7 @@ unsafe fn main_0() -> i32 { let mut bufs: Vec = Vec::new(); bufs.push(std::mem::take(&mut r)); { - let __arg = Buffer::Buffer_pmutBuffer({ &mut bufs[(0_usize)] }); + let __arg = Buffer::Buffer_pmutBuffer({ &mut bufs[(0_usize)] as *mut Buffer }); bufs.push(__arg) }; assert!( diff --git a/tests/unit/out/unsafe/copy_move_deleted.rs b/tests/unit/out/unsafe/copy_move_deleted.rs index 468e58c6..3853f0bf 100644 --- a/tests/unit/out/unsafe/copy_move_deleted.rs +++ b/tests/unit/out/unsafe/copy_move_deleted.rs @@ -108,17 +108,18 @@ pub fn main() { } unsafe fn main_0() -> i32 { let mut a: NoCopy = NoCopy::NoCopy({ 1 }); - let mut b: NoCopy = NoCopy::NoCopy_pmutNoCopy({ &mut a }); + let mut b: NoCopy = NoCopy::NoCopy_pmutNoCopy({ &mut a as *mut NoCopy }); assert!(((b.v) == (1)) && ((a.v) == (0))); - (unsafe { NoCopy::operator_assign_pmutNoCopy(&mut a, &mut b) }); + (unsafe { NoCopy::operator_assign_pmutNoCopy(&mut a, &mut b as *mut NoCopy) }); assert!(((a.v) == (1)) && ((b.v) == (0))); (unsafe { bump_0((&mut a as *mut NoCopy)) }); assert!(((a.v) == (2))); let mut p: PrivateCopy = PrivateCopy::PrivateCopy(); p.v = 3; - let mut q: PrivateCopy = PrivateCopy::PrivateCopy_pmutPrivateCopy({ &mut p }); + let mut q: PrivateCopy = + PrivateCopy::PrivateCopy_pmutPrivateCopy({ &mut p as *mut PrivateCopy }); assert!(((q.v) == (3)) && ((p.v) == (0))); - (unsafe { PrivateCopy::operator_assign_pmutPrivateCopy(&mut p, &mut q) }); + (unsafe { PrivateCopy::operator_assign_pmutPrivateCopy(&mut p, &mut q as *mut PrivateCopy) }); assert!(((p.v) == (3)) && ((q.v) == (0))); let mut im: Immovable = Immovable::Immovable(); im.v = 4; @@ -129,7 +130,7 @@ unsafe fn main_0() -> i32 { inner: NoCopy::NoCopy({ 6 }), tag: 7, }; - let mut d: Container = Container::Container_pmutContainer({ &mut c }); + let mut d: Container = Container::Container_pmutContainer({ &mut c as *mut Container }); assert!((((d.inner.v) == (6)) && ((d.tag) == (7))) && ((c.inner.v) == (0))); return 0; } diff --git a/tests/unit/out/unsafe/move_assign.rs b/tests/unit/out/unsafe/move_assign.rs index 81caf114..37ac2d03 100644 --- a/tests/unit/out/unsafe/move_assign.rs +++ b/tests/unit/out/unsafe/move_assign.rs @@ -62,7 +62,7 @@ impl Default for ConstMoveAssign { } pub unsafe fn make_0(mut v: i32) -> MoveOnly { let mut m: MoveOnly = MoveOnly::MoveOnly({ v }); - return MoveOnly::MoveOnly_pmutMoveOnly({ &mut m }); + return MoveOnly::MoveOnly_pmutMoveOnly({ &mut m as *mut MoveOnly }); } pub fn main() { unsafe { @@ -73,7 +73,7 @@ unsafe fn main_0() -> i32 { let mut a: MoveOnly = MoveOnly::MoveOnly({ 1 }); let mut b: MoveOnly = MoveOnly::MoveOnly({ 2 }); let mut c: MoveOnly = MoveOnly::MoveOnly({ 3 }); - (unsafe { MoveOnly::operator_assign_pmutMoveOnly(&mut a, &mut b) }); + (unsafe { MoveOnly::operator_assign_pmutMoveOnly(&mut a, &mut b as *mut MoveOnly) }); assert!(((a.v) == (2))); assert!(((b.v) == (0))); (unsafe { @@ -83,7 +83,9 @@ unsafe fn main_0() -> i32 { (unsafe { MoveOnly::operator_assign_pmutMoveOnly( &mut c, - (unsafe { MoveOnly::operator_assign_pmutMoveOnly(&mut a, &mut b) }), + &mut (*(unsafe { + MoveOnly::operator_assign_pmutMoveOnly(&mut a, &mut b as *mut MoveOnly) + })) as *mut MoveOnly, ) }); assert!((((b.v) == (0)) && ((a.v) == (0))) && ((c.v) == (3))); @@ -98,22 +100,34 @@ unsafe fn main_0() -> i32 { }); assert!(((a.v) == (6))); (unsafe { - let _o: *mut MoveOnly = &mut a; + let _o: *mut MoveOnly = &mut a as *mut MoveOnly; MoveOnly::operator_assign_pmutMoveOnly(&mut a, _o) }); assert!(((a.v) == (6))); let mut vec_: Vec = Vec::new(); vec_.push(MoveOnly::MoveOnly({ 7 })); let mut d: MoveOnly = MoveOnly::MoveOnly({ 8 }); - (unsafe { MoveOnly::operator_assign_pmutMoveOnly(&mut vec_[(0_usize)], &mut d) }); + (unsafe { + MoveOnly::operator_assign_pmutMoveOnly(&mut vec_[(0_usize)], &mut d as *mut MoveOnly) + }); assert!(((vec_[(0_usize)].v) == (8))); assert!(((d.v) == (0))); let mut m: ConstMoveAssign = ConstMoveAssign::ConstMoveAssign(); let mut m1: ConstMoveAssign = ConstMoveAssign::ConstMoveAssign(); let mut m2: ConstMoveAssign = ConstMoveAssign::ConstMoveAssign(); let cm: ConstMoveAssign = ConstMoveAssign::ConstMoveAssign(); - (unsafe { ConstMoveAssign::operator_assign_pmutConstMoveAssign(&mut m1, &mut m) }); - (unsafe { ConstMoveAssign::operator_assign_pconstConstMoveAssign(&mut m2, &cm) }); + (unsafe { + ConstMoveAssign::operator_assign_pmutConstMoveAssign( + &mut m1, + &mut m as *mut ConstMoveAssign, + ) + }); + (unsafe { + ConstMoveAssign::operator_assign_pconstConstMoveAssign( + &mut m2, + &cm as *const ConstMoveAssign, + ) + }); assert!(((m1.mark) == (1))); assert!(((m2.mark) == (10))); return 0; diff --git a/tests/unit/out/unsafe/move_ctor.rs b/tests/unit/out/unsafe/move_ctor.rs index a7614be6..68b29cb0 100644 --- a/tests/unit/out/unsafe/move_ctor.rs +++ b/tests/unit/out/unsafe/move_ctor.rs @@ -55,7 +55,7 @@ pub unsafe fn by_value_0(mut m: MoveOnly) -> i32 { } pub unsafe fn make_1(mut v: i32) -> MoveOnly { let mut m: MoveOnly = MoveOnly::MoveOnly({ v }); - return MoveOnly::MoveOnly_pmutMoveOnly({ &mut m }); + return MoveOnly::MoveOnly_pmutMoveOnly({ &mut m as *mut MoveOnly }); } pub fn main() { unsafe { @@ -64,19 +64,22 @@ pub fn main() { } unsafe fn main_0() -> i32 { let mut a: MoveOnly = MoveOnly::MoveOnly({ 1 }); - let mut b: MoveOnly = MoveOnly::MoveOnly_pmutMoveOnly({ &mut a }); + let mut b: MoveOnly = MoveOnly::MoveOnly_pmutMoveOnly({ &mut a as *mut MoveOnly }); assert!(((b.v) == (1))); assert!(((a.v) == (0))); - let mut c: MoveOnly = MoveOnly::MoveOnly_pmutMoveOnly({ &mut b }); + let mut c: MoveOnly = MoveOnly::MoveOnly_pmutMoveOnly({ &mut b as *mut MoveOnly }); assert!(((c.v) == (1))); assert!(((b.v) == (0))); - let mut d: MoveOnly = MoveOnly::MoveOnly_pmutMoveOnly({ &mut c }); + let mut d: MoveOnly = MoveOnly::MoveOnly_pmutMoveOnly({ &mut c as *mut MoveOnly }); assert!(((d.v) == (1))); assert!(((c.v) == (0))); let mut e: MoveOnly = (unsafe { make_1(5) }); assert!(((e.v) == (5))); assert!(((unsafe { by_value_0(MoveOnly::MoveOnly({ 6 },),) }) == (6))); - assert!(((unsafe { by_value_0(MoveOnly::MoveOnly_pmutMoveOnly({ &mut e },),) }) == (5))); + assert!( + ((unsafe { by_value_0(MoveOnly::MoveOnly_pmutMoveOnly({ &mut e as *mut MoveOnly },),) }) + == (5)) + ); assert!(((e.v) == (0))); let mut vec_: Vec = Vec::new(); vec_.push(MoveOnly::MoveOnly({ 7 })); @@ -85,9 +88,9 @@ unsafe fn main_0() -> i32 { assert!(((vec_[(0_usize)].v) == (7)) && ((vec_[(1_usize)].v) == (8))); assert!(((f.v) == (0))); let mut m: ConstMove = ConstMove::ConstMove(); - let mut m1: ConstMove = ConstMove::ConstMove_pmutConstMove({ &mut m }); + let mut m1: ConstMove = ConstMove::ConstMove_pmutConstMove({ &mut m as *mut ConstMove }); let cm: ConstMove = ConstMove::ConstMove(); - let mut m2: ConstMove = ConstMove::ConstMove_pconstConstMove({ &cm }); + let mut m2: ConstMove = ConstMove::ConstMove_pconstConstMove({ &cm as *const ConstMove }); assert!(((m1.mark) == (1))); assert!(((m2.mark) == (10))); return 0; diff --git a/tests/unit/out/unsafe/move_this.rs b/tests/unit/out/unsafe/move_this.rs index a06da153..b4034587 100644 --- a/tests/unit/out/unsafe/move_this.rs +++ b/tests/unit/out/unsafe/move_this.rs @@ -35,16 +35,16 @@ impl Chain { } pub unsafe fn add_i32_rref(&mut self, mut n: i32) -> *mut Chain { self.v += n; - return (self as *mut Chain); + return &mut (*(self as *mut Chain)) as *mut Chain; } pub unsafe fn take(&mut self) -> Chain { - return Chain::Chain_pmutChain({ (self as *mut Chain) }); + return Chain::Chain_pmutChain({ &mut (*(self as *mut Chain)) as *mut Chain }); } pub unsafe fn copy(&self) -> Chain { return Chain::Chain_pconstChain({ &(*(self as *const Chain)) as *const Chain }); } pub unsafe fn self_(&mut self) -> *mut Chain { - return (self as *mut Chain); + return &mut (*(self as *mut Chain)) as *mut Chain; } } impl Clone for Chain { diff --git a/tests/unit/out/unsafe/push_emplace_back.rs b/tests/unit/out/unsafe/push_emplace_back.rs index ef3c134b..4659d462 100644 --- a/tests/unit/out/unsafe/push_emplace_back.rs +++ b/tests/unit/out/unsafe/push_emplace_back.rs @@ -70,7 +70,7 @@ pub unsafe fn emplace_local_from_field_4(mut jpg: *mut JPEGData, mut cond: bool) } pub unsafe fn nested_emplace_move_5(mut bw: *mut Writer) { { - let __arg = std::mem::take(&mut (*bw).chunk); + let __arg = (*bw).chunk; (*(*bw).output).push(__arg) }; } diff --git a/tests/unit/out/unsafe/rule_of_five.rs b/tests/unit/out/unsafe/rule_of_five.rs index eaf5d228..28bb9596 100644 --- a/tests/unit/out/unsafe/rule_of_five.rs +++ b/tests/unit/out/unsafe/rule_of_five.rs @@ -107,7 +107,7 @@ impl Default for Buffer { pub unsafe fn make_3(mut size: i32) -> Buffer { let mut b: Buffer = Buffer::Buffer({ size }); let _dtor_b = ScopedDestructorUnsafe::new(&raw mut b, Buffer::destructor); - return Buffer::Buffer_pmutBuffer({ &mut b }); + return Buffer::Buffer_pmutBuffer({ &mut b as *mut Buffer }); } pub fn main() { unsafe { @@ -123,7 +123,7 @@ unsafe fn main_0() -> i32 { assert!((((alive_0) == (2)) && ((copies_1) == (1))) && ((moves_2) == (0))); b.data[(0) as usize] = 100; assert!(((a.data[(0) as usize]) == (0))); - let mut c: Buffer = Buffer::Buffer_pmutBuffer({ &mut a }); + let mut c: Buffer = Buffer::Buffer_pmutBuffer({ &mut a as *mut Buffer }); let _dtor_c = ScopedDestructorUnsafe::new(&raw mut c, Buffer::destructor); assert!(((alive_0) == (3)) && ((moves_2) == (1))); assert!(((a.size) == (0)) && ((a.data[(0) as usize]) == (-1_i32))); @@ -133,10 +133,10 @@ unsafe fn main_0() -> i32 { assert!(((d.size) == (2)) && ((moves_2) == (2))); (unsafe { Buffer::operator_assign_pconstBuffer(&mut d, &b as *const Buffer) }); assert!((((d.size) == (4)) && ((d.data[(0) as usize]) == (100))) && ((copies_1) == (2))); - (unsafe { Buffer::operator_assign_pmutBuffer(&mut d, &mut c) }); + (unsafe { Buffer::operator_assign_pmutBuffer(&mut d, &mut c as *mut Buffer) }); assert!((((d.data[(0) as usize]) == (0)) && ((c.size) == (0))) && ((moves_2) == (3))); (unsafe { - let _o: *mut Buffer = &mut d; + let _o: *mut Buffer = &mut d as *mut Buffer; Buffer::operator_assign_pmutBuffer(&mut d, _o) }); assert!(((d.size) == (4)) && ((moves_2) == (3))); diff --git a/tests/unit/out/unsafe/rule_of_three.rs b/tests/unit/out/unsafe/rule_of_three.rs index fe1b8cb5..c615f6a1 100644 --- a/tests/unit/out/unsafe/rule_of_three.rs +++ b/tests/unit/out/unsafe/rule_of_three.rs @@ -107,11 +107,11 @@ unsafe fn main_0() -> i32 { assert!(((copies_1) == (2))); assert!(((unsafe { sum_2(&a as *const Buffer,) }) == (6))); assert!(((unsafe { sum_2(&b as *const Buffer,) }) == (106))); - let mut d: Buffer = Buffer::Buffer_pconstBuffer({ &mut a }); + let mut d: Buffer = Buffer::Buffer_pconstBuffer({ &a as *const Buffer }); let _dtor_d = ScopedDestructorUnsafe::new(&raw mut d, Buffer::destructor); assert!(((alive_0) == (4)) && ((copies_1) == (3))); assert!(((a.size) == (4)) && ((a.data[(3) as usize]) == (3))); - (unsafe { Buffer::operator_assign(&mut d, &mut b) }); + (unsafe { Buffer::operator_assign(&mut d, &b as *const Buffer) }); assert!(((copies_1) == (4))); assert!(((b.data[(0) as usize]) == (100)) && ((d.data[(0) as usize]) == (100))); } diff --git a/tests/unit/out/unsafe/rvalue_ref_general.rs b/tests/unit/out/unsafe/rvalue_ref_general.rs index c5b6ce67..12c523d4 100644 --- a/tests/unit/out/unsafe/rvalue_ref_general.rs +++ b/tests/unit/out/unsafe/rvalue_ref_general.rs @@ -30,7 +30,7 @@ unsafe fn main_0() -> i32 { assert!(((*i6) == (*i3))); assert!(((*i7) == (*i4))); let mut i8: i32 = 3; - let i9: *mut i32 = &mut i8; + let i9: *mut i32 = &mut i8 as *mut i32; assert!(((*i9) == (3))); let mut p1: *mut i32 = (&mut i1 as *mut i32); let mut p2: *mut i32 = (i3); diff --git a/tests/unit/out/unsafe/rvalue_struct.rs b/tests/unit/out/unsafe/rvalue_struct.rs index 8d31bde0..af9a0225 100644 --- a/tests/unit/out/unsafe/rvalue_struct.rs +++ b/tests/unit/out/unsafe/rvalue_struct.rs @@ -25,7 +25,7 @@ pub fn main() { } unsafe fn main_0() -> i32 { let mut s1: S = S::S({ 1 }, { 2 }); - let s2: *mut S = &mut s1; + let s2: *mut S = &mut s1 as *mut S; assert!((((*s2).a) == (1))); assert!((((*s2).b) == (2))); return 0; From 1f2897f69bdbb68f4ee93814b9d550173b71370e Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Mon, 14 Sep 2026 20:33:45 +0100 Subject: [PATCH 08/16] Reference coerces to pointer --- cpp2rust/converter/converter.cpp | 4 -- .../out/unsafe/defaulted_move_cross_tu.rs | 10 +-- .../out/unsafe/dangling-prvalue-as-lvalue.rs | 2 +- tests/ub/out/unsafe/ub1.rs | 2 +- tests/ub/out/unsafe/ub4.rs | 2 +- tests/ub/out/unsafe/ub5.rs | 2 +- tests/ub/out/unsafe/ub6.rs | 7 +-- .../unit/out/unsafe/04_address_taken_array.rs | 2 +- tests/unit/out/unsafe/09_references.rs | 4 +- tests/unit/out/unsafe/11_move.rs | 2 +- tests/unit/out/unsafe/13_testing.rs | 2 +- tests/unit/out/unsafe/alloc_array.rs | 2 +- tests/unit/out/unsafe/array_reference.rs | 32 +++++----- tests/unit/out/unsafe/borrow_mut_opt.rs | 4 +- tests/unit/out/unsafe/class.rs | 8 +-- tests/unit/out/unsafe/clone_vs_move.rs | 4 +- tests/unit/out/unsafe/complex_function.rs | 47 +++++++------- tests/unit/out/unsafe/copy_assign.rs | 37 +++++------ tests/unit/out/unsafe/copy_ctor.rs | 19 +++--- tests/unit/out/unsafe/copy_move_defaulted.rs | 62 +++++-------------- tests/unit/out/unsafe/copy_move_deleted.rs | 23 ++++--- tests/unit/out/unsafe/cout_alias.rs | 2 +- tests/unit/out/unsafe/doubly_linked_list.rs | 28 ++++----- tests/unit/out/unsafe/exprs.rs | 4 +- tests/unit/out/unsafe/fatorial.rs | 4 +- tests/unit/out/unsafe/fft.rs | 9 +-- tests/unit/out/unsafe/friend.rs | 20 +++--- tests/unit/out/unsafe/function_overloading.rs | 4 +- tests/unit/out/unsafe/huffman.rs | 26 ++------ tests/unit/out/unsafe/init.rs | 2 +- tests/unit/out/unsafe/kruskal.rs | 10 +-- tests/unit/out/unsafe/linked_list.rs | 28 ++++----- tests/unit/out/unsafe/map.rs | 4 +- tests/unit/out/unsafe/move_assign.rs | 36 ++++------- tests/unit/out/unsafe/move_ctor.rs | 17 +++-- tests/unit/out/unsafe/move_this.rs | 10 +-- tests/unit/out/unsafe/new_array_var_size.rs | 2 +- .../out/unsafe/operator_arithmetic_free.rs | 36 +++++------ .../out/unsafe/operator_arithmetic_member.rs | 14 ++--- .../unit/out/unsafe/operator_bitwise_free.rs | 18 +++--- .../out/unsafe/operator_bitwise_member.rs | 6 +- .../unsafe/operator_comparison_defaulted.rs | 47 ++++++-------- .../out/unsafe/operator_comparison_free.rs | 32 +++++----- .../out/unsafe/operator_comparison_member.rs | 14 ++--- .../out/unsafe/operator_comparison_mixed.rs | 18 +++--- .../unsafe/operator_comparison_noncopyable.rs | 12 ++-- .../operator_compound_assignment_free.rs | 44 ++++++------- .../operator_compound_assignment_member.rs | 45 +++++++------- tests/unit/out/unsafe/operator_less_than.rs | 2 +- .../unit/out/unsafe/operator_logical_free.rs | 20 +++--- .../out/unsafe/operator_logical_member.rs | 8 +-- .../unsafe/operator_member_pointer_free.rs | 8 +-- .../unsafe/operator_member_pointer_member.rs | 10 +-- tests/unit/out/unsafe/operator_other_free.rs | 10 +-- .../unit/out/unsafe/operator_other_member.rs | 2 +- tests/unit/out/unsafe/operator_overloads.rs | 12 ++-- tests/unit/out/unsafe/operator_three_way.rs | 12 ++-- tests/unit/out/unsafe/pod.rs | 2 +- tests/unit/out/unsafe/pointer_array.rs | 2 +- tests/unit/out/unsafe/printfs.rs | 2 +- tests/unit/out/unsafe/prvalue-as-lvalue.rs | 2 +- tests/unit/out/unsafe/random.rs | 24 +++---- tests/unit/out/unsafe/rebind.rs | 2 +- tests/unit/out/unsafe/ref_calls.rs | 7 +-- tests/unit/out/unsafe/references.rs | 2 +- tests/unit/out/unsafe/references2.rs | 2 +- tests/unit/out/unsafe/refs_as_args.rs | 10 +-- tests/unit/out/unsafe/rule_of_five.rs | 20 +++--- tests/unit/out/unsafe/rule_of_three.rs | 18 +++--- tests/unit/out/unsafe/rvalue_ref_general.rs | 2 +- tests/unit/out/unsafe/rvalue_struct.rs | 2 +- .../out/unsafe/split_binop_aliased_borrows.rs | 2 +- tests/unit/out/unsafe/struct_ctor.rs | 6 +- tests/unit/out/unsafe/swap.rs | 2 +- tests/unit/out/unsafe/swap_extended.rs | 10 +-- tests/unit/out/unsafe/this.rs | 6 +- tests/unit/out/unsafe/unique_ptr.rs | 4 +- .../unit/out/unsafe/unique_ptr_const_deref.rs | 2 +- tests/unit/out/unsafe/unique_ptr_nested.rs | 2 +- tests/unit/out/unsafe/unique_ptr_small.rs | 2 +- tests/unit/out/unsafe/vector2.rs | 2 +- .../unit/out/unsafe/vector_with_allocator.rs | 2 +- tests/unit/out/unsafe/void_cast.rs | 6 +- 83 files changed, 449 insertions(+), 547 deletions(-) diff --git a/cpp2rust/converter/converter.cpp b/cpp2rust/converter/converter.cpp index 4cd62534..4e1cccea 100644 --- a/cpp2rust/converter/converter.cpp +++ b/cpp2rust/converter/converter.cpp @@ -4039,10 +4039,6 @@ void Converter::ConvertVarInit(clang::QualType qual_type, clang::Expr *expr) { PushInitType init_type(*this, qual_type); Convert(expr, qual_type); } - if (qual_type->isReferenceType() && !IsReferenceType(expr)) { - StrCat(keyword::kAs); - Convert(qual_type); - } } void Converter::ConvertUnsignedArithOperand(clang::Expr *expr, diff --git a/tests/multi-file/defaulted_move_cross_tu/out/unsafe/defaulted_move_cross_tu.rs b/tests/multi-file/defaulted_move_cross_tu/out/unsafe/defaulted_move_cross_tu.rs index 6210c2cc..726421dc 100644 --- a/tests/multi-file/defaulted_move_cross_tu/out/unsafe/defaulted_move_cross_tu.rs +++ b/tests/multi-file/defaulted_move_cross_tu/out/unsafe/defaulted_move_cross_tu.rs @@ -39,7 +39,7 @@ impl S { } ((&mut self.n as *mut [i32; 2]) as *mut [i32; 2] as *mut ::libc::c_void) }; - return &mut (*(self as *mut S)) as *mut S; + return &mut (*(self as *mut S)); } } impl Default for S { @@ -60,16 +60,16 @@ pub fn main() { } unsafe fn main_0() -> i32 { let mut s: S = S::S({ 2 }); - assert!(((unsafe { sum_0(&s as *const S,) }) == (7))); + assert!(((unsafe { sum_0(&s,) }) == (7))); assert!(((unsafe { shuffle_1(3,) }) == (10))); return 0; } pub unsafe fn shuffle_1(mut x: i32) -> i32 { let mut a: S = S::S({ x }); - let mut b: S = S::S_pmutS({ &mut a as *mut S }); + let mut b: S = S::S_pmutS({ &mut a }); assert!(a.v.is_empty()); let mut c: S = S::S({ 1 }); - (unsafe { S::operator_assign_pmutS(&mut c, &mut b as *mut S) }); + (unsafe { S::operator_assign_pmutS(&mut c, &mut b) }); assert!(b.v.is_empty()); - return (unsafe { sum_0(&c as *const S) }); + return (unsafe { sum_0(&c) }); } diff --git a/tests/ub/out/unsafe/dangling-prvalue-as-lvalue.rs b/tests/ub/out/unsafe/dangling-prvalue-as-lvalue.rs index dbe39a02..32337b39 100644 --- a/tests/ub/out/unsafe/dangling-prvalue-as-lvalue.rs +++ b/tests/ub/out/unsafe/dangling-prvalue-as-lvalue.rs @@ -16,7 +16,7 @@ pub fn main() { } unsafe fn main_0() -> i32 { let mut v: Vec = vec![1, 2]; - let b: *const i32 = (unsafe { foo_0(&(*v.as_mut_ptr()) as *const i32) }); + let b: *const i32 = (unsafe { foo_0(&(*v.as_mut_ptr())) }); v.clear(); return (*b); } diff --git a/tests/ub/out/unsafe/ub1.rs b/tests/ub/out/unsafe/ub1.rs index 5ad662e9..71faa394 100644 --- a/tests/ub/out/unsafe/ub1.rs +++ b/tests/ub/out/unsafe/ub1.rs @@ -9,7 +9,7 @@ use std::rc::Rc; pub unsafe fn dangling_0() -> *mut i32 { let mut x: i32 = 1; let mut p: *mut i32 = (&mut x as *mut i32); - return &mut (*p) as *mut i32; + return &mut (*p); } pub fn main() { unsafe { diff --git a/tests/ub/out/unsafe/ub4.rs b/tests/ub/out/unsafe/ub4.rs index df61ee19..22d7d6cb 100644 --- a/tests/ub/out/unsafe/ub4.rs +++ b/tests/ub/out/unsafe/ub4.rs @@ -19,7 +19,7 @@ unsafe fn main_0() -> i32 { let mut x1: i32 = 1; if (x1 != 0) { let mut x2: i32 = -1_i32; - out = (unsafe { smaller_0(&mut x1 as *mut i32, &mut x2 as *mut i32) }); + out = (unsafe { smaller_0(&mut x1, &mut x2) }); } return (*out); } diff --git a/tests/ub/out/unsafe/ub5.rs b/tests/ub/out/unsafe/ub5.rs index c148459a..b3c322cf 100644 --- a/tests/ub/out/unsafe/ub5.rs +++ b/tests/ub/out/unsafe/ub5.rs @@ -18,6 +18,6 @@ unsafe fn main_0() -> i32 { let mut x: i32 = 1; let mut p: *mut i32 = (&mut x as *mut i32); (unsafe { null_0((&mut p as *mut *mut i32)) }); - let r: *mut i32 = &mut (*p) as *mut i32; + let r: *mut i32 = &mut (*p); return (*r); } diff --git a/tests/ub/out/unsafe/ub6.rs b/tests/ub/out/unsafe/ub6.rs index e1c32351..9bb09de2 100644 --- a/tests/ub/out/unsafe/ub6.rs +++ b/tests/ub/out/unsafe/ub6.rs @@ -19,7 +19,7 @@ pub unsafe fn fill_1(arr: *mut Option>, n1: *mut i32) { let mut n2: i32 = (*n1); let mut pair: Pair = (unsafe { let _x1: *mut i32 = n1; - let _x2: *mut i32 = &mut n2 as *mut i32; + let _x2: *mut i32 = &mut n2; mkPair_0(_x1, _x2) }); (*arr).as_mut().unwrap()[(0_usize)] = (pair.x1); @@ -46,7 +46,6 @@ unsafe fn main_0() -> i32 { .map(|_| <*mut i32>::default()) .collect::>(), ); - (unsafe { fill_1(&mut arr as *mut Option>, &mut n as *mut i32) }); - return ((unsafe { any_2(&mut arr as *mut Option>, &mut n as *mut i32) }) - as i32); + (unsafe { fill_1(&mut arr, &mut n) }); + return ((unsafe { any_2(&mut arr, &mut n) }) as i32); } diff --git a/tests/unit/out/unsafe/04_address_taken_array.rs b/tests/unit/out/unsafe/04_address_taken_array.rs index 73dd1ef6..04105f2d 100644 --- a/tests/unit/out/unsafe/04_address_taken_array.rs +++ b/tests/unit/out/unsafe/04_address_taken_array.rs @@ -18,7 +18,7 @@ unsafe fn main_0() -> i32 { let mut arr2_ptr: *mut i32 = arr2.as_mut_ptr(); (*arr2_ptr.offset((0) as isize)) = 5; (*arr2_ptr.offset((1) as isize)) = 6; - let arr2_ref1: *mut i32 = &mut arr2[(1) as usize] as *mut i32; + let arr2_ref1: *mut i32 = &mut arr2[(1) as usize]; (*arr2_ref1) = 7; assert!((((arr2[(0) as usize]) + (arr2[(1) as usize])) == (12))); return 0; diff --git a/tests/unit/out/unsafe/09_references.rs b/tests/unit/out/unsafe/09_references.rs index 73b50b7e..7abb3895 100644 --- a/tests/unit/out/unsafe/09_references.rs +++ b/tests/unit/out/unsafe/09_references.rs @@ -13,10 +13,10 @@ pub fn main() { } unsafe fn main_0() -> i32 { let mut h: i32 = 15; - let h_ref1: *mut i32 = &mut h as *mut i32; + let h_ref1: *mut i32 = &mut h; (*h_ref1) = 16; let mut h_ptr: *mut i32 = (h_ref1); - let h_ref2: *mut i32 = &mut (*h_ptr) as *mut i32; + let h_ref2: *mut i32 = &mut (*h_ptr); (*h_ref2) = 17; assert!((((*h_ref1) + (*h_ref2)) == (34))); return 0; diff --git a/tests/unit/out/unsafe/11_move.rs b/tests/unit/out/unsafe/11_move.rs index e6a6359e..22fa8c85 100644 --- a/tests/unit/out/unsafe/11_move.rs +++ b/tests/unit/out/unsafe/11_move.rs @@ -22,7 +22,7 @@ pub fn main() { } unsafe fn main_0() -> i32 { let mut n: Option> = Some(Box::new(10)); - (unsafe { change_0(&mut n as *mut Option>) }); + (unsafe { change_0(&mut n) }); assert!(((*n.as_deref_mut().unwrap()) == (20))); return 0; } diff --git a/tests/unit/out/unsafe/13_testing.rs b/tests/unit/out/unsafe/13_testing.rs index 4825b256..ed7d44c3 100644 --- a/tests/unit/out/unsafe/13_testing.rs +++ b/tests/unit/out/unsafe/13_testing.rs @@ -13,7 +13,7 @@ pub fn main() { } unsafe fn main_0() -> i32 { let mut a: i32 = 1; - let r: *mut i32 = &mut a as *mut i32; + let r: *mut i32 = &mut a; let mut p: *mut i32 = (&mut a as *mut i32); (*r) = 2; (*p) = 3; diff --git a/tests/unit/out/unsafe/alloc_array.rs b/tests/unit/out/unsafe/alloc_array.rs index d7b4467c..be1d513d 100644 --- a/tests/unit/out/unsafe/alloc_array.rs +++ b/tests/unit/out/unsafe/alloc_array.rs @@ -39,7 +39,7 @@ unsafe fn main_0() -> i32 { .map(|_| ::default()) .collect::>(), ); - (unsafe { All_0(&mut arr as *mut Option>, N, 1) }); + (unsafe { All_0(&mut arr, N, 1) }); assert!(((unsafe { Consume_1(arr.take(), N,) }) == (10))); return 0; } diff --git a/tests/unit/out/unsafe/array_reference.rs b/tests/unit/out/unsafe/array_reference.rs index a1f6c56e..4c0ccd12 100644 --- a/tests/unit/out/unsafe/array_reference.rs +++ b/tests/unit/out/unsafe/array_reference.rs @@ -68,36 +68,32 @@ pub fn main() { } } unsafe fn main_0() -> i32 { - assert!( - ((unsafe { len_0(&std::mem::transmute(*b"beta\0") as *const [libc::c_char; 5],) }) == (4)) - ); + assert!(((unsafe { len_0(&std::mem::transmute(*b"beta\0"),) }) == (4))); let mut buf: [libc::c_char; 5] = std::mem::transmute(*b"abcd\0"); - assert!(((unsafe { len_0(&buf as *const [libc::c_char; 5],) }) == (4))); + assert!(((unsafe { len_0(&buf,) }) == (4))); let mut arr: [i32; 3] = [1, 2, 3]; - assert!(((unsafe { sum_2(&arr as *const [i32; 3],) }) == (6))); - (unsafe { fill_3(&mut arr as *mut [i32; 3], 7) }); - assert!(((unsafe { sum_2(&arr as *const [i32; 3],) }) == (21))); - assert!(((unsafe { sum_twice_4(&arr as *const [i32; 3],) }) == (42))); + assert!(((unsafe { sum_2(&arr,) }) == (6))); + (unsafe { fill_3(&mut arr, 7) }); + assert!(((unsafe { sum_2(&arr,) }) == (21))); + assert!(((unsafe { sum_twice_4(&arr,) }) == (42))); let mut out: i32 = 0; - (unsafe { fill_and_sum_5(&mut arr as *mut [i32; 3], 2, &mut out as *mut i32) }); + (unsafe { fill_and_sum_5(&mut arr, 2, &mut out) }); assert!(((out) == (12))); assert!(((arr[(0) as usize]) == (2))); - let lit: *const [libc::c_char; 5] = - &std::mem::transmute(*b"beta\0") as *const [libc::c_char; 5]; + let lit: *const [libc::c_char; 5] = &std::mem::transmute(*b"beta\0"); assert!(((unsafe { len_0(lit,) }) == (4))); assert!( - (((*(unsafe { pick_6(&std::mem::transmute(*b"beta\0") as *const [libc::c_char; 5],) })) - [(0) as usize] as i32) + (((*(unsafe { pick_6(&std::mem::transmute(*b"beta\0"),) }))[(0) as usize] as i32) == (('b' as libc::c_char) as i32)) ); - assert!(((unsafe { len_0((unsafe { pick_6(&buf as *const [libc::c_char; 5],) }),) }) == (4))); + assert!(((unsafe { len_0((unsafe { pick_6(&buf,) }),) }) == (4))); let mut pts: [Point; 2] = [Point { x: 1, y: 2 }, Point { x: 3, y: 4 }]; - assert!(((unsafe { sum_points_7(&pts as *const [Point; 2],) }) == (10))); - (unsafe { shift_points_8(&mut pts as *mut [Point; 2], 10) }); + assert!(((unsafe { sum_points_7(&pts,) }) == (10))); + (unsafe { shift_points_8(&mut pts, 10) }); assert!(((pts[(0) as usize].x) == (11))); assert!(((pts[(1) as usize].y) == (14))); - assert!(((unsafe { sum_points_7(&pts as *const [Point; 2],) }) == (30))); + assert!(((unsafe { sum_points_7(&pts,) }) == (30))); let mut names: [*const libc::c_char; 2] = [c"ab".as_ptr(), c"cde".as_ptr()]; - assert!(((unsafe { total_len_9(&mut names as *mut [*const libc::c_char; 2],) }) == (5))); + assert!(((unsafe { total_len_9(&mut names,) }) == (5))); return 0; } diff --git a/tests/unit/out/unsafe/borrow_mut_opt.rs b/tests/unit/out/unsafe/borrow_mut_opt.rs index 1aa8e5ae..7ebeca1e 100644 --- a/tests/unit/out/unsafe/borrow_mut_opt.rs +++ b/tests/unit/out/unsafe/borrow_mut_opt.rs @@ -32,7 +32,7 @@ pub unsafe fn convert_without_rhs_0() { c = arr2[(*p1) as usize]; let mut p2: *mut i32 = (&mut x as *mut i32); (*p2) = 1; - let r: *mut i32 = &mut x as *mut i32; + let r: *mut i32 = &mut x; (*r) = 1; } pub unsafe fn convert_with_rhs_1() { @@ -44,7 +44,7 @@ pub unsafe fn convert_with_rhs_1() { arr[(y) as usize] = ((y) + (1)); arr[(x) as usize] = ((x) + (1)); arr[(x) as usize] = ((arr[(y) as usize]) + (1)); - let z: *mut i32 = &mut x as *mut i32; + let z: *mut i32 = &mut x; x += (*z); y += (*z); let mut p: *mut i32 = (&mut x as *mut i32); diff --git a/tests/unit/out/unsafe/class.rs b/tests/unit/out/unsafe/class.rs index a2517775..2ac686ff 100644 --- a/tests/unit/out/unsafe/class.rs +++ b/tests/unit/out/unsafe/class.rs @@ -29,14 +29,14 @@ impl Pair { pub unsafe fn SetFirst(&mut self, mut new_first: i32) -> i32 { return ((unsafe { Pair::GetFirst(self) }) + (unsafe { - let _field: *mut i32 = &mut self.first as *mut i32; + let _field: *mut i32 = &mut self.first; Pair::Set(self, _field, new_first) })); } pub unsafe fn SetSecond(&mut self, mut new_second: i32) -> i32 { return ((unsafe { Pair::GetSecond(self) }) + (unsafe { - let _field: *mut i32 = &mut self.second as *mut i32; + let _field: *mut i32 = &mut self.second; Pair::Set(self, _field, new_second) })); } @@ -95,8 +95,8 @@ unsafe fn main_0() -> i32 { ) }); assert!( - (((((unsafe { RandomRoute_0(&mut route1 as *mut Route,) }) - + (unsafe { RandomRoute_0(&mut route2 as *mut Route,) })) as f64) + (((((unsafe { RandomRoute_0(&mut route1,) }) + (unsafe { RandomRoute_0(&mut route2,) })) + as f64) + (old_cost)) == (9_f64)) ); diff --git a/tests/unit/out/unsafe/clone_vs_move.rs b/tests/unit/out/unsafe/clone_vs_move.rs index 61340742..4d2f0030 100644 --- a/tests/unit/out/unsafe/clone_vs_move.rs +++ b/tests/unit/out/unsafe/clone_vs_move.rs @@ -47,7 +47,7 @@ unsafe fn main_0() -> i32 { x4.prefix_inc(); assert!(((x3) == (3.0E+0))); assert!(((x4) == (4.0E+0))); - let reference: *mut i32 = &mut x1 as *mut i32; + let reference: *mut i32 = &mut x1; let mut x5: i32 = (*reference); x5.prefix_inc(); assert!(((*reference) == (1))); @@ -63,7 +63,7 @@ unsafe fn main_0() -> i32 { assert!(((*other_pointer) == (*pointer))); let mut f1: Foo = Foo { x: 1, - y: &mut x1 as *mut i32, + y: &mut x1, z: (&mut x1 as *mut i32), a: [0, 1, 2], bar: Bar { w: 10 }, diff --git a/tests/unit/out/unsafe/complex_function.rs b/tests/unit/out/unsafe/complex_function.rs index ad8fc5c3..20fd319d 100644 --- a/tests/unit/out/unsafe/complex_function.rs +++ b/tests/unit/out/unsafe/complex_function.rs @@ -47,7 +47,7 @@ pub struct X4 { } impl X4 { pub unsafe fn get(&mut self) -> *mut X3 { - return &mut self.v as *mut X3; + return &mut self.v; } } pub fn main() { @@ -66,17 +66,15 @@ unsafe fn main_0() -> i32 { let mut p2: *mut i32 = (unsafe { ptr_1(p1) }); p1 = p2; p2 = (unsafe { ptr_1(p1) }); - let r1: *mut i32 = &mut x1 as *mut i32; - let r2: *mut i32 = (unsafe { bar_2(&mut x1 as *mut i32) }); + let r1: *mut i32 = &mut x1; + let r2: *mut i32 = (unsafe { bar_2(&mut x1) }); let r3: *mut i32 = (unsafe { bar_2(r1) }); (*r2) += x1; (*r3) += (*r1); let mut x4: i32 = (((unsafe { foo_0(x3) }) + (*(unsafe { ptr_1((&mut x3 as *mut i32)) }))) - + (*(unsafe { bar_2(&mut x2 as *mut i32) }))); + + (*(unsafe { bar_2(&mut x2) }))); let mut a: X1 = X1 { v: 0 }; - let mut b: X2 = X2 { - v: &mut a as *mut X1, - }; + let mut b: X2 = X2 { v: &mut a }; let mut c: X3 = X3 { v: (&mut b as *mut X2), }; @@ -88,36 +86,35 @@ unsafe fn main_0() -> i32 { let r4: *const i32 = &(*(unsafe { X2::get(&mut (*(unsafe { X3::get(&mut (*(unsafe { X4::get(&mut d) }))) }))) })) - .v as *const i32; + .v; let r5: *mut X1 = (unsafe { X2::get(&mut (*(unsafe { X3::get(&mut (*(unsafe { X4::get(&mut d) }))) }))) }); let mut p: *mut X2 = (unsafe { X3::get(&mut (*(unsafe { X4::get(&mut d) }))) }); let r6: *mut X3 = (unsafe { X4::get(&mut d) }); - let r7: *mut X3 = &mut d.v as *mut X3; - let r8: *mut i32 = - &mut (*(unsafe { X2::get(&mut (*(unsafe { X3::get(&mut d.v) }))) })).v as *mut i32; + let r7: *mut X3 = &mut d.v; + let r8: *mut i32 = &mut (*(unsafe { X2::get(&mut (*(unsafe { X3::get(&mut d.v) }))) })).v; let mut x5: i32 = (*(unsafe { X2::get(&mut (*(unsafe { X3::get(&mut (*(unsafe { X4::get(&mut d) }))) }))) })) .v; - (*(unsafe { bar_2(&mut x1 as *mut i32) })) += 10; - (*(unsafe { bar_2(&mut x1 as *mut i32) })).postfix_inc(); + (*(unsafe { bar_2(&mut x1) })) += 10; + (*(unsafe { bar_2(&mut x1) })).postfix_inc(); let mut bar_out: i32 = (*(unsafe { bar_2( &mut (*(unsafe { X2::get(&mut (*(unsafe { X3::get(&mut (*(unsafe { X4::get(&mut d) }))) }))) })) - .v as *mut i32, + .v, ) })); - let mut bar_inc: i32 = (*(unsafe { bar_2(&mut x1 as *mut i32) })).prefix_inc(); - bar_inc = (*(unsafe { bar_2(&mut x1 as *mut i32) })).postfix_inc(); - bar_inc = (((*(unsafe { bar_2(&mut x1 as *mut i32) })) + (unsafe { foo_0(x4) })) + (1)); + let mut bar_inc: i32 = (*(unsafe { bar_2(&mut x1) })).prefix_inc(); + bar_inc = (*(unsafe { bar_2(&mut x1) })).postfix_inc(); + bar_inc = (((*(unsafe { bar_2(&mut x1) })) + (unsafe { foo_0(x4) })) + (1)); (*(unsafe { bar_2( &mut (*(unsafe { X2::get(&mut (*(unsafe { X3::get(&mut (*(unsafe { X4::get(&mut d) }))) }))) })) - .v as *mut i32, + .v, ) })) += 10; (*(unsafe { @@ -125,7 +122,7 @@ unsafe fn main_0() -> i32 { &mut (*(unsafe { X2::get(&mut (*(unsafe { X3::get(&mut (*(unsafe { X4::get(&mut d) }))) }))) })) - .v as *mut i32, + .v, ) })) .postfix_inc(); @@ -134,7 +131,7 @@ unsafe fn main_0() -> i32 { &mut (*(unsafe { X2::get(&mut (*(unsafe { X3::get(&mut (*(unsafe { X4::get(&mut d) }))) }))) })) - .v as *mut i32, + .v, ) })) .prefix_inc(); @@ -143,7 +140,7 @@ unsafe fn main_0() -> i32 { &mut (*(unsafe { X2::get(&mut (*(unsafe { X3::get(&mut (*(unsafe { X4::get(&mut d) }))) }))) })) - .v as *mut i32, + .v, ) })) .postfix_inc(); @@ -190,7 +187,7 @@ unsafe fn main_0() -> i32 { })) .v as *mut i32), ) - })) as *mut i32; + })); let mut ptr3: *mut i32 = (&mut (*(unsafe { ptr_1( (&mut (*(unsafe { @@ -212,7 +209,7 @@ unsafe fn main_0() -> i32 { &mut (*(unsafe { X2::get(&mut (*(unsafe { X3::get(&mut (*(unsafe { X4::get(&mut d) }))) }))) })) - .v as *mut i32, + .v, ) }); (*(unsafe { @@ -220,7 +217,7 @@ unsafe fn main_0() -> i32 { &mut (*(unsafe { X2::get(&mut (*(unsafe { X3::get(&mut (*(unsafe { X4::get(&mut d) }))) }))) })) - .v as *mut i32, + .v, ) })) .postfix_inc(); @@ -237,7 +234,7 @@ unsafe fn main_0() -> i32 { &mut (*(unsafe { X2::get(&mut (*(unsafe { X3::get(&mut (*(unsafe { X4::get(&mut d) }))) }))) })) - .v as *mut i32, + .v, ) }))) + (unsafe { foo_0( diff --git a/tests/unit/out/unsafe/copy_assign.rs b/tests/unit/out/unsafe/copy_assign.rs index c16d17a5..d1b7469f 100644 --- a/tests/unit/out/unsafe/copy_assign.rs +++ b/tests/unit/out/unsafe/copy_assign.rs @@ -27,11 +27,11 @@ impl Partial { } pub unsafe fn operator_assign(&mut self, o: *const Partial) -> *mut Partial { if (((self as *mut Partial).cast_const()) == (o)) { - return &mut (*(self as *mut Partial)) as *mut Partial; + return &mut (*(self as *mut Partial)); } self.v = (*o).v; assigns_0.prefix_inc(); - return &mut (*(self as *mut Partial)) as *mut Partial; + return &mut (*(self as *mut Partial)); } } impl Clone for Partial { @@ -54,14 +54,14 @@ impl NonConstAssign { o: *mut NonConstAssign, ) -> *mut NonConstAssign { self.mark = (((*o).mark) + (1)); - return &mut (*(self as *mut NonConstAssign)) as *mut NonConstAssign; + return &mut (*(self as *mut NonConstAssign)); } pub unsafe fn operator_assign_pconstNonConstAssign( &mut self, o: *const NonConstAssign, ) -> *mut NonConstAssign { self.mark = (((*o).mark) + (10)); - return &mut (*(self as *mut NonConstAssign)) as *mut NonConstAssign; + return &mut (*(self as *mut NonConstAssign)); } } impl Default for NonConstAssign { @@ -81,7 +81,7 @@ impl RefQualified { } pub unsafe fn operator_assign(&mut self, o: *const RefQualified) -> *mut RefQualified { self.mark = (((*o).mark) + (1)); - return &mut (*(self as *mut RefQualified)) as *mut RefQualified; + return &mut (*(self as *mut RefQualified)); } } impl Default for RefQualified { @@ -112,20 +112,19 @@ unsafe fn main_0() -> i32 { let mut a: Partial = Partial::Partial({ 1 }, { 100 }); let mut b: Partial = Partial::Partial({ 2 }, { 200 }); let mut c: Partial = Partial::Partial({ 3 }, { 300 }); - (unsafe { Partial::operator_assign(&mut a, &b as *const Partial) }); + (unsafe { Partial::operator_assign(&mut a, &b) }); assert!(((a.v) == (2)) && ((a.keep) == (100))); assert!(((assigns_0) == (1))); (unsafe { Partial::operator_assign( &mut c, - &(*(unsafe { Partial::operator_assign(&mut a, &b as *const Partial) })) - as *const Partial, + &(*(unsafe { Partial::operator_assign(&mut a, &b) })), ) }); assert!(((c.v) == (2)) && ((c.keep) == (300))); assert!(((assigns_0) == (3))); (unsafe { - let _o: *const Partial = &a as *const Partial; + let _o: *const Partial = &a; Partial::operator_assign(&mut a, _o) }); assert!(((assigns_0) == (3))); @@ -135,15 +134,15 @@ unsafe fn main_0() -> i32 { }); assert!(((a.v) == (9)) && ((a.keep) == (100))); assert!(((assigns_0) == (4))); - let ra: *mut Partial = &mut a as *mut Partial; + let ra: *mut Partial = &mut a; (unsafe { - let _o: *const Partial = &c as *const Partial; + let _o: *const Partial = &c; Partial::operator_assign(&mut (*ra), _o) }); assert!(((a.v) == (2))); let mut pa: *mut Partial = (&mut a as *mut Partial); (unsafe { - let _o: *const Partial = &b as *const Partial; + let _o: *const Partial = &b; Partial::operator_assign(&mut (*pa), _o) }); assert!(((a.v) == (2))); @@ -155,8 +154,8 @@ unsafe fn main_0() -> i32 { Partial::Partial({ 6 }, { 60 }), ], }; - (unsafe { Partial::operator_assign(&mut h.p, &b as *const Partial) }); - (unsafe { Partial::operator_assign(&mut h.arr[(1) as usize], &c as *const Partial) }); + (unsafe { Partial::operator_assign(&mut h.p, &b) }); + (unsafe { Partial::operator_assign(&mut h.arr[(1) as usize], &c) }); assert!(((h.p.v) == (2)) && ((h.p.keep) == (40))); assert!(((h.arr[(1) as usize].v) == (2)) && ((h.arr[(1) as usize].keep) == (60))); assert!(((assigns_0) == (8))); @@ -164,17 +163,13 @@ unsafe fn main_0() -> i32 { let mut n1: NonConstAssign = NonConstAssign::NonConstAssign(); let mut n2: NonConstAssign = NonConstAssign::NonConstAssign(); let cn: NonConstAssign = NonConstAssign::NonConstAssign(); - (unsafe { - NonConstAssign::operator_assign_pmutNonConstAssign(&mut n1, &mut n as *mut NonConstAssign) - }); - (unsafe { - NonConstAssign::operator_assign_pconstNonConstAssign(&mut n2, &cn as *const NonConstAssign) - }); + (unsafe { NonConstAssign::operator_assign_pmutNonConstAssign(&mut n1, &mut n) }); + (unsafe { NonConstAssign::operator_assign_pconstNonConstAssign(&mut n2, &cn) }); assert!(((n1.mark) == (1))); assert!(((n2.mark) == (10))); let mut r: RefQualified = RefQualified::RefQualified(); let mut r1: RefQualified = RefQualified::RefQualified(); - (unsafe { RefQualified::operator_assign(&mut r1, &r as *const RefQualified) }); + (unsafe { RefQualified::operator_assign(&mut r1, &r) }); assert!(((r1.mark) == (1))); return 0; } diff --git a/tests/unit/out/unsafe/copy_ctor.rs b/tests/unit/out/unsafe/copy_ctor.rs index 8df79bb6..e098c68f 100644 --- a/tests/unit/out/unsafe/copy_ctor.rs +++ b/tests/unit/out/unsafe/copy_ctor.rs @@ -80,7 +80,7 @@ pub unsafe fn by_value_1(mut c: Counted) -> i32 { } pub unsafe fn make_2(mut v: i32) -> Counted { let mut c: Counted = Counted::Counted({ v }); - return Counted::Counted_pconstCounted({ &c as *const Counted }); + return Counted::Counted_pconstCounted({ &c }); } pub fn main() { unsafe { @@ -89,15 +89,12 @@ pub fn main() { } unsafe fn main_0() -> i32 { let mut a: Counted = Counted::Counted({ 1 }); - let mut b: Counted = Counted::Counted_pconstCounted({ &a as *const Counted }); - let mut c: Counted = Counted::Counted_pconstCounted({ &a as *const Counted }); - let mut d: Counted = Counted::Counted_pconstCounted({ &a as *const Counted }); + let mut b: Counted = Counted::Counted_pconstCounted({ &a }); + let mut c: Counted = Counted::Counted_pconstCounted({ &a }); + let mut d: Counted = Counted::Counted_pconstCounted({ &a }); assert!(((copies_0) == (3))); assert!((((b.v) == (1)) && ((c.v) == (1))) && ((d.v) == (1))); - assert!( - ((unsafe { by_value_1(Counted::Counted_pconstCounted({ &a as *const Counted },),) }) - == (1)) - ); + assert!(((unsafe { by_value_1(Counted::Counted_pconstCounted({ &a },),) }) == (1))); assert!(((copies_0) == (4))); let mut e: Counted = (unsafe { make_2(5) }); assert!(((e.v) == (5))); @@ -106,7 +103,7 @@ unsafe fn main_0() -> i32 { assert!(((f.v) == (6))); assert!(((copies_0) == (5))); let g: Counted = Counted::Counted({ 7 }); - let mut h: Counted = Counted::Counted_pconstCounted({ &g as *const Counted }); + let mut h: Counted = Counted::Counted_pconstCounted({ &g }); assert!(((h.v) == (7))); assert!(((copies_0) == (6))); let mut hold: Holder = Holder { @@ -127,9 +124,9 @@ unsafe fn main_0() -> i32 { assert!(((vec_[(0_usize)].v) == (1))); assert!(((copies_0) == (10))); let mut n: NonConst = NonConst::NonConst(); - let mut n1: NonConst = NonConst::NonConst_pmutNonConst({ &mut n as *mut NonConst }); + let mut n1: NonConst = NonConst::NonConst_pmutNonConst({ &mut n }); let cn: NonConst = NonConst::NonConst(); - let mut n2: NonConst = NonConst::NonConst_pconstNonConst({ &cn as *const NonConst }); + let mut n2: NonConst = NonConst::NonConst_pconstNonConst({ &cn }); assert!(((n1.mark) == (1))); assert!(((n2.mark) == (10))); return 0; diff --git a/tests/unit/out/unsafe/copy_move_defaulted.rs b/tests/unit/out/unsafe/copy_move_defaulted.rs index 323f0336..bb6147c8 100644 --- a/tests/unit/out/unsafe/copy_move_defaulted.rs +++ b/tests/unit/out/unsafe/copy_move_defaulted.rs @@ -75,7 +75,7 @@ impl DefaultCopyUserMove { ) -> *mut DefaultCopyUserMove { self.v = (*o).v; (*o).v = 0; - return &mut (*(self as *mut DefaultCopyUserMove)) as *mut DefaultCopyUserMove; + return &mut (*(self as *mut DefaultCopyUserMove)); } } #[repr(C)] @@ -107,14 +107,14 @@ impl UserCopyDefaultMove { o: *const UserCopyDefaultMove, ) -> *mut UserCopyDefaultMove { self.v = (((*o).v) + (100)); - return &mut (*(self as *mut UserCopyDefaultMove)) as *mut UserCopyDefaultMove; + return &mut (*(self as *mut UserCopyDefaultMove)); } pub unsafe fn operator_assign_pmutUserCopyDefaultMove( &mut self, _a0: *mut UserCopyDefaultMove, ) -> *mut UserCopyDefaultMove { self.v = (*_a0).v; - return &mut (*(self as *mut UserCopyDefaultMove)) as *mut UserCopyDefaultMove; + return &mut (*(self as *mut UserCopyDefaultMove)); } } impl Clone for UserCopyDefaultMove { @@ -164,7 +164,7 @@ impl Buffer { } ((&mut self.arr as *mut [i32; 2]) as *mut [i32; 2] as *mut ::libc::c_void) }; - return &mut (*(self as *mut Buffer)) as *mut Buffer; + return &mut (*(self as *mut Buffer)); } } impl Default for Buffer { @@ -196,9 +196,8 @@ unsafe fn main_0() -> i32 { let mut d: Explicit = a.clone(); let _dtor_d = ScopedDestructorUnsafe::new(&raw mut d, Explicit::destructor); assert!( - ((unsafe { same_0(&b as *const Explicit, &a as *const Explicit,) }) - && (unsafe { same_0(&c as *const Explicit, &a as *const Explicit,) })) - && (unsafe { same_0(&d as *const Explicit, &a as *const Explicit,) }) + ((unsafe { same_0(&b, &a,) }) && (unsafe { same_0(&c, &a,) })) + && (unsafe { same_0(&d, &a,) }) ); let mut e: Explicit = Explicit::Explicit({ 2 }); let _dtor_e = ScopedDestructorUnsafe::new(&raw mut e, Explicit::destructor); @@ -206,20 +205,14 @@ unsafe fn main_0() -> i32 { let _dtor_f = ScopedDestructorUnsafe::new(&raw mut f, Explicit::destructor); e = (b).clone(); f = (c).clone(); - assert!( - (unsafe { same_0(&e as *const Explicit, &b as *const Explicit,) }) - && (unsafe { same_0(&f as *const Explicit, &c as *const Explicit,) }) - ); + assert!((unsafe { same_0(&e, &b,) }) && (unsafe { same_0(&f, &c,) })); let mut g: Explicit = Explicit::Explicit({ 4 }); let _dtor_g = ScopedDestructorUnsafe::new(&raw mut g, Explicit::destructor); g = { e = (f).clone(); (e).clone() }; - assert!( - (unsafe { same_0(&g as *const Explicit, &f as *const Explicit,) }) - && (unsafe { same_0(&e as *const Explicit, &f as *const Explicit,) }) - ); + assert!((unsafe { same_0(&g, &f,) }) && (unsafe { same_0(&e, &f,) })); let mut i: Implicit = Implicit { v: 5, inner: Inner { x: 50 }, @@ -246,53 +239,32 @@ unsafe fn main_0() -> i32 { let mut m: DefaultCopyUserMove = DefaultCopyUserMove::DefaultCopyUserMove({ 7 }); let mut m1: DefaultCopyUserMove = m; let mut m2: DefaultCopyUserMove = - DefaultCopyUserMove::DefaultCopyUserMove_pmutDefaultCopyUserMove({ - &mut m as *mut DefaultCopyUserMove - }); + DefaultCopyUserMove::DefaultCopyUserMove_pmutDefaultCopyUserMove({ &mut m }); assert!((((m1.v) == (7)) && ((m2.v) == (7))) && ((m.v) == (0))); let mut m3: DefaultCopyUserMove = DefaultCopyUserMove::DefaultCopyUserMove({ 1 }); let mut m4: DefaultCopyUserMove = DefaultCopyUserMove::DefaultCopyUserMove({ 1 }); m3 = m1; - (unsafe { - DefaultCopyUserMove::operator_assign_pmutDefaultCopyUserMove( - &mut m4, - &mut m1 as *mut DefaultCopyUserMove, - ) - }); + (unsafe { DefaultCopyUserMove::operator_assign_pmutDefaultCopyUserMove(&mut m4, &mut m1) }); assert!((((m3.v) == (7)) && ((m4.v) == (7))) && ((m1.v) == (0))); let mut u: UserCopyDefaultMove = UserCopyDefaultMove::UserCopyDefaultMove({ 8 }); let mut u1: UserCopyDefaultMove = - UserCopyDefaultMove::UserCopyDefaultMove_pconstUserCopyDefaultMove({ - &u as *const UserCopyDefaultMove - }); + UserCopyDefaultMove::UserCopyDefaultMove_pconstUserCopyDefaultMove({ &u }); let mut u2: UserCopyDefaultMove = - UserCopyDefaultMove::UserCopyDefaultMove_pmutUserCopyDefaultMove({ - &mut u as *mut UserCopyDefaultMove - }); + UserCopyDefaultMove::UserCopyDefaultMove_pmutUserCopyDefaultMove({ &mut u }); assert!((((u1.v) == (108)) && ((u2.v) == (8))) && ((u.v) == (8))); let mut u3: UserCopyDefaultMove = UserCopyDefaultMove::UserCopyDefaultMove({ 1 }); let mut u4: UserCopyDefaultMove = UserCopyDefaultMove::UserCopyDefaultMove({ 1 }); - (unsafe { - UserCopyDefaultMove::operator_assign_pconstUserCopyDefaultMove( - &mut u3, - &u2 as *const UserCopyDefaultMove, - ) - }); - (unsafe { - UserCopyDefaultMove::operator_assign_pmutUserCopyDefaultMove( - &mut u4, - &mut u2 as *mut UserCopyDefaultMove, - ) - }); + (unsafe { UserCopyDefaultMove::operator_assign_pconstUserCopyDefaultMove(&mut u3, &u2) }); + (unsafe { UserCopyDefaultMove::operator_assign_pmutUserCopyDefaultMove(&mut u4, &mut u2) }); assert!(((u3.v) == (108)) && ((u4.v) == (8))); let mut p: Buffer = Buffer::Buffer({ 3 }); - let mut q: Buffer = Buffer::Buffer_pmutBuffer({ &mut p as *mut Buffer }); + let mut q: Buffer = Buffer::Buffer_pmutBuffer({ &mut p }); assert!( ((((q.n) == (3)) && ((q.data.len()) == (3_usize))) && ((q.data[(2_usize)]) == (3))) && (p.data.is_empty()) ); let mut r: Buffer = Buffer::Buffer({ 1 }); - (unsafe { Buffer::operator_assign_pmutBuffer(&mut r, &mut q as *mut Buffer) }); + (unsafe { Buffer::operator_assign_pmutBuffer(&mut r, &mut q) }); assert!( ((((r.n) == (3)) && ((r.data.len()) == (3_usize))) && ((r.arr[(1) as usize]) == (4))) && (q.data.is_empty()) @@ -300,7 +272,7 @@ unsafe fn main_0() -> i32 { let mut bufs: Vec = Vec::new(); bufs.push(std::mem::take(&mut r)); { - let __arg = Buffer::Buffer_pmutBuffer({ &mut bufs[(0_usize)] as *mut Buffer }); + let __arg = Buffer::Buffer_pmutBuffer({ &mut bufs[(0_usize)] }); bufs.push(__arg) }; assert!( diff --git a/tests/unit/out/unsafe/copy_move_deleted.rs b/tests/unit/out/unsafe/copy_move_deleted.rs index 3853f0bf..ac5257ac 100644 --- a/tests/unit/out/unsafe/copy_move_deleted.rs +++ b/tests/unit/out/unsafe/copy_move_deleted.rs @@ -24,7 +24,7 @@ impl NoCopy { pub unsafe fn operator_assign_pmutNoCopy(&mut self, o: *mut NoCopy) -> *mut NoCopy { self.v = (*o).v; (*o).v = 0; - return &mut (*(self as *mut NoCopy)) as *mut NoCopy; + return &mut (*(self as *mut NoCopy)); } } #[repr(C)] @@ -48,7 +48,7 @@ impl PrivateCopy { ) -> *mut PrivateCopy { self.v = (*o).v; (*o).v = 0; - return &mut (*(self as *mut PrivateCopy)) as *mut PrivateCopy; + return &mut (*(self as *mut PrivateCopy)); } } impl Default for PrivateCopy { @@ -81,18 +81,18 @@ pub struct Container { impl Container { pub unsafe fn Container_pmutContainer(_a0: *mut Container) -> Self { let mut this = Self { - inner: NoCopy::NoCopy_pmutNoCopy({ &mut (*_a0).inner as *mut NoCopy }), + inner: NoCopy::NoCopy_pmutNoCopy({ &mut (*_a0).inner }), tag: (*_a0).tag, }; this } pub unsafe fn operator_assign_pmutContainer(&mut self, _a0: *mut Container) -> *mut Container { (unsafe { - let _o: *mut NoCopy = &mut (*_a0).inner as *mut NoCopy; + let _o: *mut NoCopy = &mut (*_a0).inner; NoCopy::operator_assign_pmutNoCopy(&mut self.inner, _o) }); self.tag = (*_a0).tag; - return &mut (*(self as *mut Container)) as *mut Container; + return &mut (*(self as *mut Container)); } } pub unsafe fn bump_0(mut p: *mut NoCopy) { @@ -108,29 +108,28 @@ pub fn main() { } unsafe fn main_0() -> i32 { let mut a: NoCopy = NoCopy::NoCopy({ 1 }); - let mut b: NoCopy = NoCopy::NoCopy_pmutNoCopy({ &mut a as *mut NoCopy }); + let mut b: NoCopy = NoCopy::NoCopy_pmutNoCopy({ &mut a }); assert!(((b.v) == (1)) && ((a.v) == (0))); - (unsafe { NoCopy::operator_assign_pmutNoCopy(&mut a, &mut b as *mut NoCopy) }); + (unsafe { NoCopy::operator_assign_pmutNoCopy(&mut a, &mut b) }); assert!(((a.v) == (1)) && ((b.v) == (0))); (unsafe { bump_0((&mut a as *mut NoCopy)) }); assert!(((a.v) == (2))); let mut p: PrivateCopy = PrivateCopy::PrivateCopy(); p.v = 3; - let mut q: PrivateCopy = - PrivateCopy::PrivateCopy_pmutPrivateCopy({ &mut p as *mut PrivateCopy }); + let mut q: PrivateCopy = PrivateCopy::PrivateCopy_pmutPrivateCopy({ &mut p }); assert!(((q.v) == (3)) && ((p.v) == (0))); - (unsafe { PrivateCopy::operator_assign_pmutPrivateCopy(&mut p, &mut q as *mut PrivateCopy) }); + (unsafe { PrivateCopy::operator_assign_pmutPrivateCopy(&mut p, &mut q) }); assert!(((p.v) == (3)) && ((q.v) == (0))); let mut im: Immovable = Immovable::Immovable(); im.v = 4; - (unsafe { bump_ref_1(&mut im as *mut Immovable) }); + (unsafe { bump_ref_1(&mut im) }); let mut pim: *mut Immovable = (&mut im as *mut Immovable); assert!((((*pim).v) == (5))); let mut c: Container = Container { inner: NoCopy::NoCopy({ 6 }), tag: 7, }; - let mut d: Container = Container::Container_pmutContainer({ &mut c as *mut Container }); + let mut d: Container = Container::Container_pmutContainer({ &mut c }); assert!((((d.inner.v) == (6)) && ((d.tag) == (7))) && ((c.inner.v) == (0))); return 0; } diff --git a/tests/unit/out/unsafe/cout_alias.rs b/tests/unit/out/unsafe/cout_alias.rs index 35d1841c..3b4f5011 100644 --- a/tests/unit/out/unsafe/cout_alias.rs +++ b/tests/unit/out/unsafe/cout_alias.rs @@ -35,7 +35,7 @@ unsafe fn main_0() -> i32 { .unwrap() .into_raw_fd(), ) - } as *mut std::fs::File; + }; write!((*os2), "hello\n",); return 0; } diff --git a/tests/unit/out/unsafe/doubly_linked_list.rs b/tests/unit/out/unsafe/doubly_linked_list.rs index f11bcec2..0d414e79 100644 --- a/tests/unit/out/unsafe/doubly_linked_list.rs +++ b/tests/unit/out/unsafe/doubly_linked_list.rs @@ -127,38 +127,38 @@ unsafe fn main_0() -> i32 { prev: std::ptr::null_mut(), }; (unsafe { - let _head: *mut Node = &mut (*head) as *mut Node; - let _new_node: *mut Node = &mut n1 as *mut Node; + let _head: *mut Node = &mut (*head); + let _new_node: *mut Node = &mut n1; Append_2(_head, _new_node) }); (unsafe { - let _head: *mut Node = &mut (*head) as *mut Node; - let _new_node: *mut Node = &mut n2 as *mut Node; + let _head: *mut Node = &mut (*head); + let _new_node: *mut Node = &mut n2; Append_2(_head, _new_node) }); (unsafe { - let _head: *mut Node = &mut (*head) as *mut Node; - let _new_node: *mut Node = &mut n3 as *mut Node; + let _head: *mut Node = &mut (*head); + let _new_node: *mut Node = &mut n3; Append_2(_head, _new_node) }); (unsafe { - let _head: *mut Node = &mut (*head) as *mut Node; - let _new_node: *mut Node = &mut n4 as *mut Node; + let _head: *mut Node = &mut (*head); + let _new_node: *mut Node = &mut n4; Append_2(_head, _new_node) }); (unsafe { - let _head: *mut Node = &mut (*head) as *mut Node; - let _new_node: *mut Node = &mut n5 as *mut Node; + let _head: *mut Node = &mut (*head); + let _new_node: *mut Node = &mut n5; Append_2(_head, _new_node) }); (unsafe { - let _head: *mut Node = &mut (*head) as *mut Node; - let _new_node: *mut Node = &mut n6 as *mut Node; + let _head: *mut Node = &mut (*head); + let _new_node: *mut Node = &mut n6; Append_2(_head, _new_node) }); (unsafe { - let _head: *mut Node = &mut (*head) as *mut Node; - let _new_node: *mut Node = &mut n7 as *mut Node; + let _head: *mut Node = &mut (*head); + let _new_node: *mut Node = &mut n7; Append_2(_head, _new_node) }); head = (unsafe { Delete_3(head, 5) }); diff --git a/tests/unit/out/unsafe/exprs.rs b/tests/unit/out/unsafe/exprs.rs index 69405697..150824d4 100644 --- a/tests/unit/out/unsafe/exprs.rs +++ b/tests/unit/out/unsafe/exprs.rs @@ -19,7 +19,7 @@ pub struct Y { } impl Y { pub unsafe fn foo(&mut self) -> *mut X { - return &mut self.x as *mut X; + return &mut self.x; } pub unsafe fn ptr(&mut self) -> *mut X { return (&mut self.x as *mut X); @@ -45,7 +45,7 @@ unsafe fn main_0() -> i32 { (*p1) = (((x1) + (x4)) + (1)); let mut x5: i32 = (*p1); let mut x6: i32 = (((*p1) + (x3)) + (5)); - let r: *mut i32 = &mut x1 as *mut i32; + let r: *mut i32 = &mut x1; (*r) = 5; (*r) = ((*p1) + (5)); let mut x7: i32 = (*r); diff --git a/tests/unit/out/unsafe/fatorial.rs b/tests/unit/out/unsafe/fatorial.rs index 84ce95d8..ad00e181 100644 --- a/tests/unit/out/unsafe/fatorial.rs +++ b/tests/unit/out/unsafe/fatorial.rs @@ -18,7 +18,7 @@ pub unsafe fn fatorial_by_ref_1(n: *mut i32) { return; } let mut n_1: i32 = ((*n) - (1)); - (unsafe { fatorial_by_ref_1(&mut n_1 as *mut i32) }); + (unsafe { fatorial_by_ref_1(&mut n_1) }); (*n) *= n_1; } pub unsafe fn fatorial_by_ptr_2(mut n: *mut i32) { @@ -39,7 +39,7 @@ unsafe fn main_0() -> i32 { let mut n1: i32 = 2; (unsafe { fatorial_by_ptr_2((&mut n1 as *mut i32)) }); let mut n: i32 = ((n1) + (1)); - (unsafe { fatorial_by_ref_1(&mut n as *mut i32) }); + (unsafe { fatorial_by_ref_1(&mut n) }); assert!(((unsafe { fatorial_0(n,) }) == (720))); return 0; } diff --git a/tests/unit/out/unsafe/fft.rs b/tests/unit/out/unsafe/fft.rs index 19ed836f..88a02fea 100644 --- a/tests/unit/out/unsafe/fft.rs +++ b/tests/unit/out/unsafe/fft.rs @@ -82,10 +82,8 @@ pub unsafe fn fft_3(a: *mut Option>, mut N: i32) -> Option> = - (unsafe { fft_3(&mut A0 as *mut Option>, ((N) / (2))) }); - let mut y1: Option> = - (unsafe { fft_3(&mut A1 as *mut Option>, ((N) / (2))) }); + let mut y0: Option> = (unsafe { fft_3(&mut A0, ((N) / (2))) }); + let mut y1: Option> = (unsafe { fft_3(&mut A1, ((N) / (2))) }); let mut k: i32 = 0; 'loop_: while ((k) < ((N) / (2))) { let mut yk: Complex = (unsafe { @@ -142,8 +140,7 @@ unsafe fn main_0() -> i32 { }; i.postfix_inc(); } - let mut b: Option> = - (unsafe { fft_3(&mut a as *mut Option>, N) }); + let mut b: Option> = (unsafe { fft_3(&mut a, N) }); let mut reals: Option> = Some( (0..(N as usize)) .map(|_| ::default()) diff --git a/tests/unit/out/unsafe/friend.rs b/tests/unit/out/unsafe/friend.rs index 0b9d58f7..0838e6ce 100644 --- a/tests/unit/out/unsafe/friend.rs +++ b/tests/unit/out/unsafe/friend.rs @@ -62,26 +62,26 @@ unsafe fn main_0() -> i32 { let mut a: V = V { x: 3 }; let mut b: V = V { x: 3 }; let mut c: V = V { x: 4 }; - assert!(((unsafe { get_0(&a as *const V,) }) == (3))); + assert!(((unsafe { get_0(&a,) }) == (3))); assert!( (unsafe { - let _a: *const V = &a as *const V; - operator_eq_1(_a, &b as *const V) + let _a: *const V = &a; + operator_eq_1(_a, &b) }) ); assert!( !(unsafe { - let _a: *const V = &a as *const V; - operator_eq_1(_a, &c as *const V) + let _a: *const V = &a; + operator_eq_1(_a, &c) }) ); - assert!(((unsafe { scaled_2(&c as *const V, 2,) }) == (8))); - assert!(((unsafe { scaled_3(&c as *const V, 1.5E+0,) }) == (6.0E+0))); + assert!(((unsafe { scaled_2(&c, 2,) }) == (8))); + assert!(((unsafe { scaled_3(&c, 1.5E+0,) }) == (6.0E+0))); let mut wi: W_int_ = W_int_ { x: 5 }; let mut wl: W_long_ = W_long_ { x: 6_i64 }; - assert!(((unsafe { get_4(&wi as *const W_int_,) }) == (5))); - assert!(((unsafe { get_5(&wl as *const W_long_,) }) == (6_i64))); + assert!(((unsafe { get_4(&wi,) }) == (5))); + assert!(((unsafe { get_5(&wl,) }) == (6_i64))); let mut d: D = D { x: 7 }; - assert!(((unsafe { declared_then_defined_6(&d as *const D,) }) == (8))); + assert!(((unsafe { declared_then_defined_6(&d,) }) == (8))); return 0; } diff --git a/tests/unit/out/unsafe/function_overloading.rs b/tests/unit/out/unsafe/function_overloading.rs index 19cfa331..a1ea21e6 100644 --- a/tests/unit/out/unsafe/function_overloading.rs +++ b/tests/unit/out/unsafe/function_overloading.rs @@ -48,11 +48,11 @@ unsafe fn main_0() -> i32 { let mut out: i32 = 0; out += (unsafe { foo_0(0) }); out += (unsafe { foo_1((&mut x as *mut i32)) }); - out += (unsafe { bar_4(&mut x as *mut i32) }); + out += (unsafe { bar_4(&mut x) }); out += (unsafe { let _x: *mut i32 = (&mut x as *mut i32); let _y: *mut i32 = (&mut x as *mut i32); - let _z: *mut i32 = &mut x as *mut i32; + let _z: *mut i32 = &mut x; foo_3(_x, _y, _z) }); out += (unsafe { diff --git a/tests/unit/out/unsafe/huffman.rs b/tests/unit/out/unsafe/huffman.rs index 25cbbe1e..229c9eb3 100644 --- a/tests/unit/out/unsafe/huffman.rs +++ b/tests/unit/out/unsafe/huffman.rs @@ -79,10 +79,8 @@ impl MinHeap { } if ((smallest) != (idx)) { (unsafe { - let _a: *mut MinHeapNode = - &mut (*self.arr.as_mut().unwrap()[(smallest as usize)]) as *mut MinHeapNode; - let _b: *mut MinHeapNode = - &mut (*self.arr.as_mut().unwrap()[(idx as usize)]) as *mut MinHeapNode; + let _a: *mut MinHeapNode = &mut (*self.arr.as_mut().unwrap()[(smallest as usize)]); + let _b: *mut MinHeapNode = &mut (*self.arr.as_mut().unwrap()[(idx as usize)]); Swap_0(_a, _b) }); (unsafe { MinHeap::Heapify(self, smallest) }); @@ -145,7 +143,7 @@ impl MinHeap { self.arr = (*_a0).arr.take(); self.next = (*_a0).next; self.alloc = (*_a0).alloc.take(); - return &mut (*(self as *mut MinHeap)) as *mut MinHeap; + return &mut (*(self as *mut MinHeap)); } } pub unsafe fn AllocMinHeap_1(mut capacity: i32) -> Option> { @@ -278,15 +276,7 @@ pub unsafe fn HuffmanCodes_5( ); let mut top: i32 = 0; let mut next: i32 = 0; - (unsafe { - CollectCodes_4( - root, - &mut arr as *mut Option>, - top, - &mut out as *mut Option>, - &mut next as *mut i32, - ) - }); + (unsafe { CollectCodes_4(root, &mut arr, top, &mut out, &mut next) }); return out.take(); } pub fn main() { @@ -321,13 +311,7 @@ unsafe fn main_0() -> i32 { freq.as_mut().unwrap()[(i as usize)] = arr2[(i) as usize]; i.prefix_inc(); } - let mut out: Option> = (unsafe { - HuffmanCodes_5( - &mut data as *mut Option>, - &mut freq as *mut Option>, - size, - ) - }); + let mut out: Option> = (unsafe { HuffmanCodes_5(&mut data, &mut freq, size) }); assert!( ((((((out.as_mut().unwrap()[(0_usize)]) == (0)) && ((out.as_mut().unwrap()[(1_usize)]) == (100))) diff --git a/tests/unit/out/unsafe/init.rs b/tests/unit/out/unsafe/init.rs index 5590090d..c4b49a67 100644 --- a/tests/unit/out/unsafe/init.rs +++ b/tests/unit/out/unsafe/init.rs @@ -22,7 +22,7 @@ pub fn main() { unsafe fn main_0() -> i32 { let mut x: i32 = 0_i32; let mut p: *mut i32 = std::ptr::null_mut(); - let g: *mut i32 = &mut x as *mut i32; + let g: *mut i32 = &mut x; let mut q: *mut i32 = (&mut x as *mut i32); let mut z: *mut i32 = p; let mut xx: X = ::default(); diff --git a/tests/unit/out/unsafe/kruskal.rs b/tests/unit/out/unsafe/kruskal.rs index b78c5b30..5fa31aae 100644 --- a/tests/unit/out/unsafe/kruskal.rs +++ b/tests/unit/out/unsafe/kruskal.rs @@ -14,7 +14,7 @@ pub struct Edge { pub weight: f64, } pub unsafe fn partition_0(arr: *mut Option>, mut start: i32, mut end: i32) -> i32 { - let pivot: *mut Edge = &mut (*arr).as_mut().unwrap()[(start as usize)] as *mut Edge; + let pivot: *mut Edge = &mut (*arr).as_mut().unwrap()[(start as usize)]; let mut count: i32 = 0; let mut i: i32 = ((start) + (1)); 'loop_: while ((i) <= (end)) { @@ -153,7 +153,7 @@ impl DisjointSet { self.rank = (*_a0).rank.take(); self.parent = (*_a0).parent.take(); self.n = (*_a0).n; - return &mut (*(self as *mut DisjointSet)) as *mut DisjointSet; + return &mut (*(self as *mut DisjointSet)); } } #[repr(C)] @@ -176,12 +176,12 @@ impl Graph { self.edges = (*_a0).edges.take(); self.V = (*_a0).V; self.E = (*_a0).E; - return &mut (*(self as *mut Graph)) as *mut Graph; + return &mut (*(self as *mut Graph)); } } pub unsafe fn MSTKruskal_2(graph: *mut Graph) -> f64 { (unsafe { - let _arr: *mut Option> = &mut (*graph).edges as *mut Option>; + let _arr: *mut Option> = &mut (*graph).edges; let _end: i32 = (((*graph).E) - (1)); quicksort_1(_arr, 0, _end) }); @@ -257,7 +257,7 @@ unsafe fn main_0() -> i32 { v: 3, weight: 5_f64, }; - let mut total_weight: f64 = (unsafe { MSTKruskal_2(&mut graph as *mut Graph) }); + let mut total_weight: f64 = (unsafe { MSTKruskal_2(&mut graph) }); assert!(((total_weight) == (19_f64))); return 0; } diff --git a/tests/unit/out/unsafe/linked_list.rs b/tests/unit/out/unsafe/linked_list.rs index aaf26744..8fadd385 100644 --- a/tests/unit/out/unsafe/linked_list.rs +++ b/tests/unit/out/unsafe/linked_list.rs @@ -90,38 +90,38 @@ unsafe fn main_0() -> i32 { next: std::ptr::null_mut(), }; (unsafe { - let _head: *mut Node = &mut (*head) as *mut Node; - let _new_node: *mut Node = &mut n1 as *mut Node; + let _head: *mut Node = &mut (*head); + let _new_node: *mut Node = &mut n1; Append_1(_head, _new_node) }); (unsafe { - let _head: *mut Node = &mut (*head) as *mut Node; - let _new_node: *mut Node = &mut n2 as *mut Node; + let _head: *mut Node = &mut (*head); + let _new_node: *mut Node = &mut n2; Append_1(_head, _new_node) }); (unsafe { - let _head: *mut Node = &mut (*head) as *mut Node; - let _new_node: *mut Node = &mut n3 as *mut Node; + let _head: *mut Node = &mut (*head); + let _new_node: *mut Node = &mut n3; Append_1(_head, _new_node) }); (unsafe { - let _head: *mut Node = &mut (*head) as *mut Node; - let _new_node: *mut Node = &mut n4 as *mut Node; + let _head: *mut Node = &mut (*head); + let _new_node: *mut Node = &mut n4; Append_1(_head, _new_node) }); (unsafe { - let _head: *mut Node = &mut (*head) as *mut Node; - let _new_node: *mut Node = &mut n5 as *mut Node; + let _head: *mut Node = &mut (*head); + let _new_node: *mut Node = &mut n5; Append_1(_head, _new_node) }); (unsafe { - let _head: *mut Node = &mut (*head) as *mut Node; - let _new_node: *mut Node = &mut n6 as *mut Node; + let _head: *mut Node = &mut (*head); + let _new_node: *mut Node = &mut n6; Append_1(_head, _new_node) }); (unsafe { - let _head: *mut Node = &mut (*head) as *mut Node; - let _new_node: *mut Node = &mut n7 as *mut Node; + let _head: *mut Node = &mut (*head); + let _new_node: *mut Node = &mut n7; Append_1(_head, _new_node) }); head = (unsafe { Delete_2(head, 5) }); diff --git a/tests/unit/out/unsafe/map.rs b/tests/unit/out/unsafe/map.rs index f2cdfb24..36fdf0f0 100644 --- a/tests/unit/out/unsafe/map.rs +++ b/tests/unit/out/unsafe/map.rs @@ -34,7 +34,7 @@ unsafe fn main_0() -> i32 { assert!(((*m.entry(2_i16).or_default().as_mut()) == (3_u32))); (unsafe { foo_0((*m.entry(0_i16).or_default().as_mut())) }); assert!(((*m.entry(0_i16).or_default().as_mut()) == (1_u32))); - (unsafe { bar_1(&mut (*m.entry(2_i16).or_default().as_mut()) as *mut u32) }); + (unsafe { bar_1(&mut (*m.entry(2_i16).or_default().as_mut())) }); assert!(((*m.entry(2_i16).or_default().as_mut()) == (4_u32))); (*m.entry(0_i16).or_default().as_mut()) = (*m.entry(0_i16).or_default().as_mut()) .wrapping_add((*m.entry(2_i16).or_default().as_mut())); @@ -80,7 +80,7 @@ unsafe fn main_0() -> i32 { assert!(((*it4.second()) == (6_u32))); assert!(((*p) == (6_u32))); assert!(((x5) == (5_u32))); - let r: *mut BTreeMap> = &mut m as *mut BTreeMap>; + let r: *mut BTreeMap> = &mut m; assert!((((*r).len()) == (4_usize))); assert!( UnsafeMapIterator::find_key(&m as *const BTreeMap>, &4_i16) diff --git a/tests/unit/out/unsafe/move_assign.rs b/tests/unit/out/unsafe/move_assign.rs index 37ac2d03..da0855c8 100644 --- a/tests/unit/out/unsafe/move_assign.rs +++ b/tests/unit/out/unsafe/move_assign.rs @@ -23,11 +23,11 @@ impl MoveOnly { } pub unsafe fn operator_assign_pmutMoveOnly(&mut self, o: *mut MoveOnly) -> *mut MoveOnly { if ((self as *mut MoveOnly) == (o)) { - return &mut (*(self as *mut MoveOnly)) as *mut MoveOnly; + return &mut (*(self as *mut MoveOnly)); } self.v = (*o).v; (*o).v = 0; - return &mut (*(self as *mut MoveOnly)) as *mut MoveOnly; + return &mut (*(self as *mut MoveOnly)); } } #[repr(C)] @@ -45,14 +45,14 @@ impl ConstMoveAssign { o: *mut ConstMoveAssign, ) -> *mut ConstMoveAssign { self.mark = (((*o).mark) + (1)); - return &mut (*(self as *mut ConstMoveAssign)) as *mut ConstMoveAssign; + return &mut (*(self as *mut ConstMoveAssign)); } pub unsafe fn operator_assign_pconstConstMoveAssign( &mut self, o: *const ConstMoveAssign, ) -> *mut ConstMoveAssign { self.mark = (((*o).mark) + (10)); - return &mut (*(self as *mut ConstMoveAssign)) as *mut ConstMoveAssign; + return &mut (*(self as *mut ConstMoveAssign)); } } impl Default for ConstMoveAssign { @@ -62,7 +62,7 @@ impl Default for ConstMoveAssign { } pub unsafe fn make_0(mut v: i32) -> MoveOnly { let mut m: MoveOnly = MoveOnly::MoveOnly({ v }); - return MoveOnly::MoveOnly_pmutMoveOnly({ &mut m as *mut MoveOnly }); + return MoveOnly::MoveOnly_pmutMoveOnly({ &mut m }); } pub fn main() { unsafe { @@ -73,7 +73,7 @@ unsafe fn main_0() -> i32 { let mut a: MoveOnly = MoveOnly::MoveOnly({ 1 }); let mut b: MoveOnly = MoveOnly::MoveOnly({ 2 }); let mut c: MoveOnly = MoveOnly::MoveOnly({ 3 }); - (unsafe { MoveOnly::operator_assign_pmutMoveOnly(&mut a, &mut b as *mut MoveOnly) }); + (unsafe { MoveOnly::operator_assign_pmutMoveOnly(&mut a, &mut b) }); assert!(((a.v) == (2))); assert!(((b.v) == (0))); (unsafe { @@ -83,9 +83,7 @@ unsafe fn main_0() -> i32 { (unsafe { MoveOnly::operator_assign_pmutMoveOnly( &mut c, - &mut (*(unsafe { - MoveOnly::operator_assign_pmutMoveOnly(&mut a, &mut b as *mut MoveOnly) - })) as *mut MoveOnly, + &mut (*(unsafe { MoveOnly::operator_assign_pmutMoveOnly(&mut a, &mut b) })), ) }); assert!((((b.v) == (0)) && ((a.v) == (0))) && ((c.v) == (3))); @@ -100,34 +98,22 @@ unsafe fn main_0() -> i32 { }); assert!(((a.v) == (6))); (unsafe { - let _o: *mut MoveOnly = &mut a as *mut MoveOnly; + let _o: *mut MoveOnly = &mut a; MoveOnly::operator_assign_pmutMoveOnly(&mut a, _o) }); assert!(((a.v) == (6))); let mut vec_: Vec = Vec::new(); vec_.push(MoveOnly::MoveOnly({ 7 })); let mut d: MoveOnly = MoveOnly::MoveOnly({ 8 }); - (unsafe { - MoveOnly::operator_assign_pmutMoveOnly(&mut vec_[(0_usize)], &mut d as *mut MoveOnly) - }); + (unsafe { MoveOnly::operator_assign_pmutMoveOnly(&mut vec_[(0_usize)], &mut d) }); assert!(((vec_[(0_usize)].v) == (8))); assert!(((d.v) == (0))); let mut m: ConstMoveAssign = ConstMoveAssign::ConstMoveAssign(); let mut m1: ConstMoveAssign = ConstMoveAssign::ConstMoveAssign(); let mut m2: ConstMoveAssign = ConstMoveAssign::ConstMoveAssign(); let cm: ConstMoveAssign = ConstMoveAssign::ConstMoveAssign(); - (unsafe { - ConstMoveAssign::operator_assign_pmutConstMoveAssign( - &mut m1, - &mut m as *mut ConstMoveAssign, - ) - }); - (unsafe { - ConstMoveAssign::operator_assign_pconstConstMoveAssign( - &mut m2, - &cm as *const ConstMoveAssign, - ) - }); + (unsafe { ConstMoveAssign::operator_assign_pmutConstMoveAssign(&mut m1, &mut m) }); + (unsafe { ConstMoveAssign::operator_assign_pconstConstMoveAssign(&mut m2, &cm) }); assert!(((m1.mark) == (1))); assert!(((m2.mark) == (10))); return 0; diff --git a/tests/unit/out/unsafe/move_ctor.rs b/tests/unit/out/unsafe/move_ctor.rs index 68b29cb0..a7614be6 100644 --- a/tests/unit/out/unsafe/move_ctor.rs +++ b/tests/unit/out/unsafe/move_ctor.rs @@ -55,7 +55,7 @@ pub unsafe fn by_value_0(mut m: MoveOnly) -> i32 { } pub unsafe fn make_1(mut v: i32) -> MoveOnly { let mut m: MoveOnly = MoveOnly::MoveOnly({ v }); - return MoveOnly::MoveOnly_pmutMoveOnly({ &mut m as *mut MoveOnly }); + return MoveOnly::MoveOnly_pmutMoveOnly({ &mut m }); } pub fn main() { unsafe { @@ -64,22 +64,19 @@ pub fn main() { } unsafe fn main_0() -> i32 { let mut a: MoveOnly = MoveOnly::MoveOnly({ 1 }); - let mut b: MoveOnly = MoveOnly::MoveOnly_pmutMoveOnly({ &mut a as *mut MoveOnly }); + let mut b: MoveOnly = MoveOnly::MoveOnly_pmutMoveOnly({ &mut a }); assert!(((b.v) == (1))); assert!(((a.v) == (0))); - let mut c: MoveOnly = MoveOnly::MoveOnly_pmutMoveOnly({ &mut b as *mut MoveOnly }); + let mut c: MoveOnly = MoveOnly::MoveOnly_pmutMoveOnly({ &mut b }); assert!(((c.v) == (1))); assert!(((b.v) == (0))); - let mut d: MoveOnly = MoveOnly::MoveOnly_pmutMoveOnly({ &mut c as *mut MoveOnly }); + let mut d: MoveOnly = MoveOnly::MoveOnly_pmutMoveOnly({ &mut c }); assert!(((d.v) == (1))); assert!(((c.v) == (0))); let mut e: MoveOnly = (unsafe { make_1(5) }); assert!(((e.v) == (5))); assert!(((unsafe { by_value_0(MoveOnly::MoveOnly({ 6 },),) }) == (6))); - assert!( - ((unsafe { by_value_0(MoveOnly::MoveOnly_pmutMoveOnly({ &mut e as *mut MoveOnly },),) }) - == (5)) - ); + assert!(((unsafe { by_value_0(MoveOnly::MoveOnly_pmutMoveOnly({ &mut e },),) }) == (5))); assert!(((e.v) == (0))); let mut vec_: Vec = Vec::new(); vec_.push(MoveOnly::MoveOnly({ 7 })); @@ -88,9 +85,9 @@ unsafe fn main_0() -> i32 { assert!(((vec_[(0_usize)].v) == (7)) && ((vec_[(1_usize)].v) == (8))); assert!(((f.v) == (0))); let mut m: ConstMove = ConstMove::ConstMove(); - let mut m1: ConstMove = ConstMove::ConstMove_pmutConstMove({ &mut m as *mut ConstMove }); + let mut m1: ConstMove = ConstMove::ConstMove_pmutConstMove({ &mut m }); let cm: ConstMove = ConstMove::ConstMove(); - let mut m2: ConstMove = ConstMove::ConstMove_pconstConstMove({ &cm as *const ConstMove }); + let mut m2: ConstMove = ConstMove::ConstMove_pconstConstMove({ &cm }); assert!(((m1.mark) == (1))); assert!(((m2.mark) == (10))); return 0; diff --git a/tests/unit/out/unsafe/move_this.rs b/tests/unit/out/unsafe/move_this.rs index b4034587..d4d939fd 100644 --- a/tests/unit/out/unsafe/move_this.rs +++ b/tests/unit/out/unsafe/move_this.rs @@ -31,20 +31,20 @@ impl Chain { } pub unsafe fn add_i32_lref(&mut self, mut n: i32) -> *mut Chain { self.v += n; - return &mut (*(self as *mut Chain)) as *mut Chain; + return &mut (*(self as *mut Chain)); } pub unsafe fn add_i32_rref(&mut self, mut n: i32) -> *mut Chain { self.v += n; - return &mut (*(self as *mut Chain)) as *mut Chain; + return &mut (*(self as *mut Chain)); } pub unsafe fn take(&mut self) -> Chain { - return Chain::Chain_pmutChain({ &mut (*(self as *mut Chain)) as *mut Chain }); + return Chain::Chain_pmutChain({ &mut (*(self as *mut Chain)) }); } pub unsafe fn copy(&self) -> Chain { - return Chain::Chain_pconstChain({ &(*(self as *const Chain)) as *const Chain }); + return Chain::Chain_pconstChain({ &(*(self as *const Chain)) }); } pub unsafe fn self_(&mut self) -> *mut Chain { - return &mut (*(self as *mut Chain)) as *mut Chain; + return &mut (*(self as *mut Chain)); } } impl Clone for Chain { diff --git a/tests/unit/out/unsafe/new_array_var_size.rs b/tests/unit/out/unsafe/new_array_var_size.rs index 323b005f..e9e34ea5 100644 --- a/tests/unit/out/unsafe/new_array_var_size.rs +++ b/tests/unit/out/unsafe/new_array_var_size.rs @@ -20,7 +20,7 @@ unsafe fn main_0() -> i32 { A, libcc2rs::malloc_usable_size(A as *mut ::libc::c_void) / ::std::mem::size_of::(), ))); - let N2: *mut i32 = &mut N as *mut i32; + let N2: *mut i32 = &mut N; let mut A2: *mut i32 = Box::leak((0..((*N2) as usize)).map(|_| 0_i32).collect::>()).as_mut_ptr(); diff --git a/tests/unit/out/unsafe/operator_arithmetic_free.rs b/tests/unit/out/unsafe/operator_arithmetic_free.rs index 28a79422..07abe9a0 100644 --- a/tests/unit/out/unsafe/operator_arithmetic_free.rs +++ b/tests/unit/out/unsafe/operator_arithmetic_free.rs @@ -80,63 +80,63 @@ unsafe fn main_0() -> i32 { let mut b: S = S { v: 2 }; assert!( (((unsafe { - let _a: *const S = &a as *const S; - operator_add_0(_a, &b as *const S) + let _a: *const S = &a; + operator_add_0(_a, &b) }) .v) == (9)) ); assert!( (((unsafe { - let _a: *const S = &a as *const S; - operator_sub_1(_a, &b as *const S) + let _a: *const S = &a; + operator_sub_1(_a, &b) }) .v) == (5)) ); assert!( (((unsafe { - let _a: *const S = &a as *const S; - operator_mul_2(_a, &b as *const S) + let _a: *const S = &a; + operator_mul_2(_a, &b) }) .v) == (14)) ); assert!( (((unsafe { - let _a: *const S = &a as *const S; - operator_div_3(_a, &b as *const S) + let _a: *const S = &a; + operator_div_3(_a, &b) }) .v) == (3)) ); assert!( (((unsafe { - let _a: *const S = &a as *const S; - operator_rem_4(_a, &b as *const S) + let _a: *const S = &a; + operator_rem_4(_a, &b) }) .v) == (1)) ); assert!( (((unsafe { - let _a: *const S = &a as *const S; + let _a: *const S = &a; operator_pos_5(_a) }) .v) == (7)) ); assert!( (((unsafe { - let _a: *const S = &a as *const S; + let _a: *const S = &a; operator_neg_6(_a) }) .v) == (-7_i32)) ); assert!( (((*(unsafe { - let _a: *mut S = &mut a as *mut S; + let _a: *mut S = &mut a; operator_inc_7(_a) })) .v) == (8)) ); assert!( (((unsafe { - let _a: *mut S = &mut a as *mut S; + let _a: *mut S = &mut a; operator_post_inc_8(_a, 0) }) .v) == (8)) @@ -144,14 +144,14 @@ unsafe fn main_0() -> i32 { assert!(((a.v) == (9))); assert!( (((*(unsafe { - let _a: *mut S = &mut a as *mut S; + let _a: *mut S = &mut a; operator_dec_9(_a) })) .v) == (8)) ); assert!( (((unsafe { - let _a: *mut S = &mut a as *mut S; + let _a: *mut S = &mut a; operator_post_dec_10(_a, 0) }) .v) == (8)) @@ -159,11 +159,11 @@ unsafe fn main_0() -> i32 { assert!(((a.v) == (7))); assert!( (((unsafe { - let _a: *const S = &a as *const S; + let _a: *const S = &a; operator_add_11(_a, 1) }) .v) == (8)) ); - assert!((((unsafe { operator_add_12(1, &a as *const S,) }).v) == (8))); + assert!((((unsafe { operator_add_12(1, &a,) }).v) == (8))); return 0; } diff --git a/tests/unit/out/unsafe/operator_arithmetic_member.rs b/tests/unit/out/unsafe/operator_arithmetic_member.rs index c3166ce2..268866bc 100644 --- a/tests/unit/out/unsafe/operator_arithmetic_member.rs +++ b/tests/unit/out/unsafe/operator_arithmetic_member.rs @@ -45,7 +45,7 @@ impl S { } pub unsafe fn operator_inc(&mut self) -> *mut S { self.v.prefix_inc(); - return &mut (*(self as *mut S)) as *mut S; + return &mut (*(self as *mut S)); } pub unsafe fn operator_post_inc_i32(&mut self, mut _a0: i32) -> S { let mut old: S = (*(self as *mut S)); @@ -54,7 +54,7 @@ impl S { } pub unsafe fn operator_dec(&mut self) -> *mut S { self.v.prefix_dec(); - return &mut (*(self as *mut S)) as *mut S; + return &mut (*(self as *mut S)); } pub unsafe fn operator_post_dec_i32(&mut self, mut _a0: i32) -> S { let mut old: S = (*(self as *mut S)); @@ -70,11 +70,11 @@ pub fn main() { unsafe fn main_0() -> i32 { let mut a: S = S { v: 7 }; let mut b: S = S { v: 2 }; - assert!((((unsafe { S::operator_add_pconstS_const(&a, &b as *const S,) }).v) == (9))); - assert!((((unsafe { S::operator_sub_pconstS_const(&a, &b as *const S,) }).v) == (5))); - assert!((((unsafe { S::operator_mul(&a, &b as *const S,) }).v) == (14))); - assert!((((unsafe { S::operator_div(&a, &b as *const S,) }).v) == (3))); - assert!((((unsafe { S::operator_rem(&a, &b as *const S,) }).v) == (1))); + assert!((((unsafe { S::operator_add_pconstS_const(&a, &b,) }).v) == (9))); + assert!((((unsafe { S::operator_sub_pconstS_const(&a, &b,) }).v) == (5))); + assert!((((unsafe { S::operator_mul(&a, &b,) }).v) == (14))); + assert!((((unsafe { S::operator_div(&a, &b,) }).v) == (3))); + assert!((((unsafe { S::operator_rem(&a, &b,) }).v) == (1))); assert!((((unsafe { S::operator_pos_const(&a,) }).v) == (7))); assert!((((unsafe { S::operator_neg_const(&a,) }).v) == (-7_i32))); assert!((((*(unsafe { S::operator_inc(&mut a,) })).v) == (8))); diff --git a/tests/unit/out/unsafe/operator_bitwise_free.rs b/tests/unit/out/unsafe/operator_bitwise_free.rs index f6c11f71..5bfdae44 100644 --- a/tests/unit/out/unsafe/operator_bitwise_free.rs +++ b/tests/unit/out/unsafe/operator_bitwise_free.rs @@ -49,42 +49,42 @@ unsafe fn main_0() -> i32 { let mut b: S = S { v: 10_u32 }; assert!( (((unsafe { - let _a: *const S = &a as *const S; + let _a: *const S = &a; operator_bitnot_0(_a) }) .v) == (!12_u32)) ); assert!( (((unsafe { - let _a: *const S = &a as *const S; - operator_bitand_1(_a, &b as *const S) + let _a: *const S = &a; + operator_bitand_1(_a, &b) }) .v) == (8_u32)) ); assert!( (((unsafe { - let _a: *const S = &a as *const S; - operator_bitor_2(_a, &b as *const S) + let _a: *const S = &a; + operator_bitor_2(_a, &b) }) .v) == (14_u32)) ); assert!( (((unsafe { - let _a: *const S = &a as *const S; - operator_bitxor_3(_a, &b as *const S) + let _a: *const S = &a; + operator_bitxor_3(_a, &b) }) .v) == (6_u32)) ); assert!( (((unsafe { - let _a: *const S = &a as *const S; + let _a: *const S = &a; operator_shl_4(_a, 2) }) .v) == (48_u32)) ); assert!( (((unsafe { - let _a: *const S = &a as *const S; + let _a: *const S = &a; operator_shr_5(_a, 2) }) .v) == (3_u32)) diff --git a/tests/unit/out/unsafe/operator_bitwise_member.rs b/tests/unit/out/unsafe/operator_bitwise_member.rs index f4abca28..161a57fa 100644 --- a/tests/unit/out/unsafe/operator_bitwise_member.rs +++ b/tests/unit/out/unsafe/operator_bitwise_member.rs @@ -50,9 +50,9 @@ unsafe fn main_0() -> i32 { let mut a: S = S { v: 12_u32 }; let mut b: S = S { v: 10_u32 }; assert!((((unsafe { S::operator_bitnot(&a,) }).v) == (!12_u32))); - assert!((((unsafe { S::operator_bitand(&a, &b as *const S,) }).v) == (8_u32))); - assert!((((unsafe { S::operator_bitor(&a, &b as *const S,) }).v) == (14_u32))); - assert!((((unsafe { S::operator_bitxor(&a, &b as *const S,) }).v) == (6_u32))); + assert!((((unsafe { S::operator_bitand(&a, &b,) }).v) == (8_u32))); + assert!((((unsafe { S::operator_bitor(&a, &b,) }).v) == (14_u32))); + assert!((((unsafe { S::operator_bitxor(&a, &b,) }).v) == (6_u32))); assert!((((unsafe { S::operator_shl(&a, 2,) }).v) == (48_u32))); assert!((((unsafe { S::operator_shr(&a, 2,) }).v) == (3_u32))); return 0; diff --git a/tests/unit/out/unsafe/operator_comparison_defaulted.rs b/tests/unit/out/unsafe/operator_comparison_defaulted.rs index 72c41b77..a4c454c5 100644 --- a/tests/unit/out/unsafe/operator_comparison_defaulted.rs +++ b/tests/unit/out/unsafe/operator_comparison_defaulted.rs @@ -179,7 +179,7 @@ impl Outer { pub unsafe fn operator_cmp(&self, _a0: *const Outer) -> std::cmp::Ordering { { let mut cmp: std::cmp::Ordering = (unsafe { - let _arg0: *const Inner = &(*_a0).i as *const Inner; + let _arg0: *const Inner = &(*_a0).i; Inner::operator_cmp(&(*(self as *const Outer)).i, _arg0) }); if !(cmp == std::cmp::Ordering::Equal) { @@ -196,7 +196,7 @@ impl Outer { } pub unsafe fn operator_eq(&self, _a0: *const Outer) -> bool { return (unsafe { - let _arg0: *const Inner = &(*_a0).i as *const Inner; + let _arg0: *const Inner = &(*_a0).i; Inner::operator_eq(&(*(self as *const Outer)).i, _arg0) }) && (((*(self as *const Outer)).y) == ((*_a0).y)); } @@ -279,39 +279,34 @@ unsafe fn main_0() -> i32 { let mut e1: Eq = Eq { a: 1, b: 2 }; let mut e2: Eq = Eq { a: 1, b: 2 }; let mut e3: Eq = Eq { a: 1, b: 3 }; - assert!((unsafe { Eq::operator_eq(&e1, &e2 as *const Eq,) })); - assert!(!(unsafe { Eq::operator_eq(&e1, &e3 as *const Eq,) })); + assert!((unsafe { Eq::operator_eq(&e1, &e2,) })); + assert!(!(unsafe { Eq::operator_eq(&e1, &e3,) })); let mut c1: Cmp = Cmp { a: 1, b: 2 }; let mut c2: Cmp = Cmp { a: 1, b: 3 }; let mut c3: Cmp = Cmp { a: 2, b: 0 }; let mut c4: Cmp = Cmp { a: 1, b: 9 }; - assert!((unsafe { Cmp::operator_cmp(&c1, &c2 as *const Cmp,) }) == std::cmp::Ordering::Less); - assert!((unsafe { Cmp::operator_cmp(&c3, &c4 as *const Cmp,) }) == std::cmp::Ordering::Greater); + assert!((unsafe { Cmp::operator_cmp(&c1, &c2,) }) == std::cmp::Ordering::Less); + assert!((unsafe { Cmp::operator_cmp(&c3, &c4,) }) == std::cmp::Ordering::Greater); assert!( (unsafe { - let _arg0: *const Cmp = &c1 as *const Cmp; + let _arg0: *const Cmp = &c1; Cmp::operator_eq(&c1, _arg0) }) ); - assert!((unsafe { Cmp::operator_cmp(&c1, &c2 as *const Cmp,) }) == std::cmp::Ordering::Less); + assert!((unsafe { Cmp::operator_cmp(&c1, &c2,) }) == std::cmp::Ordering::Less); let mut b1: Both = Both { a: 1 }; let mut b2: Both = Both { a: 2 }; - assert!((unsafe { Both::operator_cmp(&b1, &b2 as *const Both,) }) == std::cmp::Ordering::Less); + assert!((unsafe { Both::operator_cmp(&b1, &b2,) }) == std::cmp::Ordering::Less); assert!( (unsafe { - let _arg0: *const Both = &b2 as *const Both; + let _arg0: *const Both = &b2; Both::operator_eq(&b2, _arg0) }) ); let mut o1: OrdOnly = OrdOnly { a: 1 }; let mut o2: OrdOnly = OrdOnly { a: 2 }; - assert!( - (unsafe { OrdOnly::operator_cmp(&o1, &o2 as *const OrdOnly,) }) == std::cmp::Ordering::Less - ); - assert!( - (unsafe { OrdOnly::operator_cmp(&o2, &o1 as *const OrdOnly,) }) - == std::cmp::Ordering::Greater - ); + assert!((unsafe { OrdOnly::operator_cmp(&o1, &o2,) }) == std::cmp::Ordering::Less); + assert!((unsafe { OrdOnly::operator_cmp(&o2, &o1,) }) == std::cmp::Ordering::Greater); let mut x1: Outer = Outer { i: Inner { x: 1 }, y: 9, @@ -324,18 +319,14 @@ unsafe fn main_0() -> i32 { i: Inner { x: 1 }, y: 9, }; - assert!( - (unsafe { Outer::operator_cmp(&x1, &x2 as *const Outer,) }) == std::cmp::Ordering::Less - ); - assert!((unsafe { Outer::operator_eq(&x1, &x3 as *const Outer,) })); - assert!( - (unsafe { Outer::operator_cmp(&x2, &x1 as *const Outer,) }) == std::cmp::Ordering::Greater - ); + assert!((unsafe { Outer::operator_cmp(&x1, &x2,) }) == std::cmp::Ordering::Less); + assert!((unsafe { Outer::operator_eq(&x1, &x3,) })); + assert!((unsafe { Outer::operator_cmp(&x2, &x1,) }) == std::cmp::Ordering::Greater); let mut s1: Secondary = Secondary { a: 1 }; let mut s2: Secondary = Secondary { a: 2 }; - assert!((unsafe { Secondary::operator_ne(&s1, &s2 as *const Secondary,) })); - assert!((unsafe { Secondary::operator_lt(&s1, &s2 as *const Secondary,) })); - assert!((unsafe { Secondary::operator_ge(&s2, &s1 as *const Secondary,) })); - assert!(!(unsafe { Secondary::operator_lt(&s2, &s1 as *const Secondary,) })); + assert!((unsafe { Secondary::operator_ne(&s1, &s2,) })); + assert!((unsafe { Secondary::operator_lt(&s1, &s2,) })); + assert!((unsafe { Secondary::operator_ge(&s2, &s1,) })); + assert!(!(unsafe { Secondary::operator_lt(&s2, &s1,) })); return 0; } diff --git a/tests/unit/out/unsafe/operator_comparison_free.rs b/tests/unit/out/unsafe/operator_comparison_free.rs index dea30f04..ecf85bb0 100644 --- a/tests/unit/out/unsafe/operator_comparison_free.rs +++ b/tests/unit/out/unsafe/operator_comparison_free.rs @@ -70,52 +70,52 @@ unsafe fn main_0() -> i32 { let mut c: S = S { v: 1 }; assert!( (unsafe { - let _a: *const S = &a as *const S; - operator_eq_1(_a, &c as *const S) + let _a: *const S = &a; + operator_eq_1(_a, &c) }) ); assert!( (unsafe { - let _a: *const S = &a as *const S; - operator_ne_2(_a, &b as *const S) + let _a: *const S = &a; + operator_ne_2(_a, &b) }) ); assert!( (unsafe { - let _a: *const S = &a as *const S; - operator_lt_0(_a, &b as *const S) + let _a: *const S = &a; + operator_lt_0(_a, &b) }) ); assert!( (unsafe { - let _a: *const S = &b as *const S; - operator_gt_3(_a, &a as *const S) + let _a: *const S = &b; + operator_gt_3(_a, &a) }) ); assert!( (unsafe { - let _a: *const S = &a as *const S; - operator_le_4(_a, &c as *const S) + let _a: *const S = &a; + operator_le_4(_a, &c) }) ); assert!( (unsafe { - let _a: *const S = &a as *const S; - operator_ge_5(_a, &c as *const S) + let _a: *const S = &a; + operator_ge_5(_a, &c) }) ); assert!( !(unsafe { - let _a: *const S = &b as *const S; - operator_lt_0(_a, &a as *const S) + let _a: *const S = &b; + operator_lt_0(_a, &a) }) ); assert!( (unsafe { - let _a: *const S = &a as *const S; + let _a: *const S = &a; operator_lt_6(_a, 5) }) ); - assert!((unsafe { operator_lt_7(0, &a as *const S,) })); + assert!((unsafe { operator_lt_7(0, &a,) })); return 0; } diff --git a/tests/unit/out/unsafe/operator_comparison_member.rs b/tests/unit/out/unsafe/operator_comparison_member.rs index c10a280f..12b44efa 100644 --- a/tests/unit/out/unsafe/operator_comparison_member.rs +++ b/tests/unit/out/unsafe/operator_comparison_member.rs @@ -67,13 +67,13 @@ unsafe fn main_0() -> i32 { let mut a: S = S { v: 1 }; let mut b: S = S { v: 2 }; let mut c: S = S { v: 1 }; - assert!((unsafe { S::operator_eq(&a, &c as *const S,) })); - assert!((unsafe { S::operator_ne(&a, &b as *const S,) })); - assert!((unsafe { S::operator_lt_pconstS_const(&a, &b as *const S,) })); - assert!((unsafe { S::operator_gt(&b, &a as *const S,) })); - assert!((unsafe { S::operator_le(&a, &c as *const S,) })); - assert!((unsafe { S::operator_ge(&a, &c as *const S,) })); - assert!(!(unsafe { S::operator_lt_pconstS_const(&b, &a as *const S,) })); + assert!((unsafe { S::operator_eq(&a, &c,) })); + assert!((unsafe { S::operator_ne(&a, &b,) })); + assert!((unsafe { S::operator_lt_pconstS_const(&a, &b,) })); + assert!((unsafe { S::operator_gt(&b, &a,) })); + assert!((unsafe { S::operator_le(&a, &c,) })); + assert!((unsafe { S::operator_ge(&a, &c,) })); + assert!(!(unsafe { S::operator_lt_pconstS_const(&b, &a,) })); assert!((unsafe { S::operator_lt_i32_const(&a, 5,) })); return 0; } diff --git a/tests/unit/out/unsafe/operator_comparison_mixed.rs b/tests/unit/out/unsafe/operator_comparison_mixed.rs index 729ab83d..47bc31c8 100644 --- a/tests/unit/out/unsafe/operator_comparison_mixed.rs +++ b/tests/unit/out/unsafe/operator_comparison_mixed.rs @@ -59,28 +59,28 @@ pub fn main() { } unsafe fn main_0() -> i32 { let mut s: S = S { v: 5 }; - let cs: *const S = &s as *const S; + let cs: *const S = &s; assert!((unsafe { S::operator_eq(&(*cs), 5,) })); assert!((unsafe { S::operator_ne(&(*cs), 4,) })); assert!((unsafe { S::operator_lt(&(*cs), 6,) })); assert!((unsafe { S::operator_gt(&(*cs), 4.5E+0,) })); assert!((unsafe { S::operator_le(&(*cs), 5_i64,) })); assert!((unsafe { S::operator_ge(&(*cs), c"3".as_ptr(),) })); - assert!((unsafe { operator_eq_0(5, &s as *const S,) })); - assert!((unsafe { operator_ne_1(4, &s as *const S,) })); - assert!((unsafe { operator_lt_2(4, &s as *const S,) })); - assert!((unsafe { operator_gt_3(5.5E+0, &s as *const S,) })); - assert!((unsafe { operator_le_4(5_i64, &s as *const S,) })); - assert!((unsafe { operator_ge_5(c"7".as_ptr(), &s as *const S,) })); + assert!((unsafe { operator_eq_0(5, &s,) })); + assert!((unsafe { operator_ne_1(4, &s,) })); + assert!((unsafe { operator_lt_2(4, &s,) })); + assert!((unsafe { operator_gt_3(5.5E+0, &s,) })); + assert!((unsafe { operator_le_4(5_i64, &s,) })); + assert!((unsafe { operator_ge_5(c"7".as_ptr(), &s,) })); assert!( (unsafe { - let _a: *mut S = &mut s as *mut S; + let _a: *mut S = &mut s; operator_lt_6(_a, 7) }) ); assert!( !(unsafe { - let _a: *mut S = &mut s as *mut S; + let _a: *mut S = &mut s; operator_lt_6(_a, 6) }) ); diff --git a/tests/unit/out/unsafe/operator_comparison_noncopyable.rs b/tests/unit/out/unsafe/operator_comparison_noncopyable.rs index 04618061..1593443c 100644 --- a/tests/unit/out/unsafe/operator_comparison_noncopyable.rs +++ b/tests/unit/out/unsafe/operator_comparison_noncopyable.rs @@ -64,20 +64,20 @@ unsafe fn main_0() -> i32 { let mut c: S = S::S({ 1 }); assert!( (unsafe { - let _x: *const S = &a as *const S; - operator_eq_0(_x, &c as *const S) + let _x: *const S = &a; + operator_eq_0(_x, &c) }) ); assert!( (unsafe { - let _x: *const S = &a as *const S; - operator_lt_1(_x, &b as *const S) + let _x: *const S = &a; + operator_lt_1(_x, &b) }) ); assert!( !(unsafe { - let _x: *const S = &b as *const S; - operator_lt_1(_x, &a as *const S) + let _x: *const S = &b; + operator_lt_1(_x, &a) }) ); return 0; diff --git a/tests/unit/out/unsafe/operator_compound_assignment_free.rs b/tests/unit/out/unsafe/operator_compound_assignment_free.rs index 5011fa87..b432a037 100644 --- a/tests/unit/out/unsafe/operator_compound_assignment_free.rs +++ b/tests/unit/out/unsafe/operator_compound_assignment_free.rs @@ -60,62 +60,62 @@ unsafe fn main_0() -> i32 { let mut a: S = S { v: 6_u32 }; let mut b: S = S { v: 4_u32 }; (unsafe { - let _a: *mut S = &mut a as *mut S; - operator_add_assign_0(_a, &b as *const S) + let _a: *mut S = &mut a; + operator_add_assign_0(_a, &b) }); assert!(((a.v) == (10_u32))); (unsafe { - let _a: *mut S = &mut a as *mut S; - operator_sub_assign_1(_a, &b as *const S) + let _a: *mut S = &mut a; + operator_sub_assign_1(_a, &b) }); assert!(((a.v) == (6_u32))); (unsafe { - let _a: *mut S = &mut a as *mut S; - operator_mul_assign_2(_a, &b as *const S) + let _a: *mut S = &mut a; + operator_mul_assign_2(_a, &b) }); assert!(((a.v) == (24_u32))); (unsafe { - let _a: *mut S = &mut a as *mut S; - operator_div_assign_3(_a, &b as *const S) + let _a: *mut S = &mut a; + operator_div_assign_3(_a, &b) }); assert!(((a.v) == (6_u32))); (unsafe { - let _a: *mut S = &mut a as *mut S; - operator_rem_assign_4(_a, &b as *const S) + let _a: *mut S = &mut a; + operator_rem_assign_4(_a, &b) }); assert!(((a.v) == (2_u32))); (unsafe { - let _a: *mut S = &mut a as *mut S; - operator_bitor_assign_6(_a, &b as *const S) + let _a: *mut S = &mut a; + operator_bitor_assign_6(_a, &b) }); assert!(((a.v) == (6_u32))); (unsafe { - let _a: *mut S = &mut a as *mut S; - operator_bitand_assign_5(_a, &b as *const S) + let _a: *mut S = &mut a; + operator_bitand_assign_5(_a, &b) }); assert!(((a.v) == (4_u32))); (unsafe { - let _a: *mut S = &mut a as *mut S; - operator_bitxor_assign_7(_a, &b as *const S) + let _a: *mut S = &mut a; + operator_bitxor_assign_7(_a, &b) }); assert!(((a.v) == (0_u32))); a.v = 3_u32; (unsafe { - let _a: *mut S = &mut a as *mut S; + let _a: *mut S = &mut a; operator_shl_assign_8(_a, 2) }); assert!(((a.v) == (12_u32))); (unsafe { - let _a: *mut S = &mut a as *mut S; + let _a: *mut S = &mut a; operator_shr_assign_9(_a, 1) }); assert!(((a.v) == (6_u32))); (unsafe { let _a: *mut S = &mut (*(unsafe { - let _a: *mut S = &mut a as *mut S; - operator_add_assign_0(_a, &b as *const S) - })) as *mut S; - let _b: *const S = &b as *const S; + let _a: *mut S = &mut a; + operator_add_assign_0(_a, &b) + })); + let _b: *const S = &b; operator_add_assign_0(_a, _b) }); assert!(((a.v) == (14_u32))); diff --git a/tests/unit/out/unsafe/operator_compound_assignment_member.rs b/tests/unit/out/unsafe/operator_compound_assignment_member.rs index e1858378..8d169d4e 100644 --- a/tests/unit/out/unsafe/operator_compound_assignment_member.rs +++ b/tests/unit/out/unsafe/operator_compound_assignment_member.rs @@ -14,47 +14,47 @@ pub struct S { impl S { pub unsafe fn operator_assign_u32(&mut self, mut n: u32) -> *mut S { self.v = n; - return &mut (*(self as *mut S)) as *mut S; + return &mut (*(self as *mut S)); } pub unsafe fn operator_add_assign(&mut self, o: *const S) -> *mut S { self.v = (self.v).wrapping_add((*o).v); - return &mut (*(self as *mut S)) as *mut S; + return &mut (*(self as *mut S)); } pub unsafe fn operator_sub_assign(&mut self, o: *const S) -> *mut S { self.v = (self.v).wrapping_sub((*o).v); - return &mut (*(self as *mut S)) as *mut S; + return &mut (*(self as *mut S)); } pub unsafe fn operator_mul_assign(&mut self, o: *const S) -> *mut S { self.v = (self.v).wrapping_mul((*o).v); - return &mut (*(self as *mut S)) as *mut S; + return &mut (*(self as *mut S)); } pub unsafe fn operator_div_assign(&mut self, o: *const S) -> *mut S { self.v = (self.v).wrapping_div((*o).v); - return &mut (*(self as *mut S)) as *mut S; + return &mut (*(self as *mut S)); } pub unsafe fn operator_rem_assign(&mut self, o: *const S) -> *mut S { self.v = (self.v).wrapping_rem((*o).v); - return &mut (*(self as *mut S)) as *mut S; + return &mut (*(self as *mut S)); } pub unsafe fn operator_bitand_assign(&mut self, o: *const S) -> *mut S { self.v &= (*o).v; - return &mut (*(self as *mut S)) as *mut S; + return &mut (*(self as *mut S)); } pub unsafe fn operator_bitor_assign(&mut self, o: *const S) -> *mut S { self.v |= (*o).v; - return &mut (*(self as *mut S)) as *mut S; + return &mut (*(self as *mut S)); } pub unsafe fn operator_bitxor_assign(&mut self, o: *const S) -> *mut S { self.v ^= (*o).v; - return &mut (*(self as *mut S)) as *mut S; + return &mut (*(self as *mut S)); } pub unsafe fn operator_shl_assign(&mut self, mut n: i32) -> *mut S { self.v <<= n; - return &mut (*(self as *mut S)) as *mut S; + return &mut (*(self as *mut S)); } pub unsafe fn operator_shr_assign(&mut self, mut n: i32) -> *mut S { self.v >>= n; - return &mut (*(self as *mut S)) as *mut S; + return &mut (*(self as *mut S)); } } pub fn main() { @@ -65,21 +65,21 @@ pub fn main() { unsafe fn main_0() -> i32 { let mut a: S = S { v: 6_u32 }; let mut b: S = S { v: 4_u32 }; - (unsafe { S::operator_add_assign(&mut a, &b as *const S) }); + (unsafe { S::operator_add_assign(&mut a, &b) }); assert!(((a.v) == (10_u32))); - (unsafe { S::operator_sub_assign(&mut a, &b as *const S) }); + (unsafe { S::operator_sub_assign(&mut a, &b) }); assert!(((a.v) == (6_u32))); - (unsafe { S::operator_mul_assign(&mut a, &b as *const S) }); + (unsafe { S::operator_mul_assign(&mut a, &b) }); assert!(((a.v) == (24_u32))); - (unsafe { S::operator_div_assign(&mut a, &b as *const S) }); + (unsafe { S::operator_div_assign(&mut a, &b) }); assert!(((a.v) == (6_u32))); - (unsafe { S::operator_rem_assign(&mut a, &b as *const S) }); + (unsafe { S::operator_rem_assign(&mut a, &b) }); assert!(((a.v) == (2_u32))); - (unsafe { S::operator_bitor_assign(&mut a, &b as *const S) }); + (unsafe { S::operator_bitor_assign(&mut a, &b) }); assert!(((a.v) == (6_u32))); - (unsafe { S::operator_bitand_assign(&mut a, &b as *const S) }); + (unsafe { S::operator_bitand_assign(&mut a, &b) }); assert!(((a.v) == (4_u32))); - (unsafe { S::operator_bitxor_assign(&mut a, &b as *const S) }); + (unsafe { S::operator_bitxor_assign(&mut a, &b) }); assert!(((a.v) == (0_u32))); (unsafe { S::operator_assign_u32(&mut a, 3_u32) }); assert!(((a.v) == (3_u32))); @@ -88,11 +88,8 @@ unsafe fn main_0() -> i32 { (unsafe { S::operator_shr_assign(&mut a, 1) }); assert!(((a.v) == (6_u32))); (unsafe { - let _o: *const S = &b as *const S; - S::operator_add_assign( - &mut (*(unsafe { S::operator_add_assign(&mut a, &b as *const S) })), - _o, - ) + let _o: *const S = &b; + S::operator_add_assign(&mut (*(unsafe { S::operator_add_assign(&mut a, &b) })), _o) }); assert!(((a.v) == (14_u32))); let mut c: S = S { v: 0_u32 }; diff --git a/tests/unit/out/unsafe/operator_less_than.rs b/tests/unit/out/unsafe/operator_less_than.rs index 94937ae4..364a0df8 100644 --- a/tests/unit/out/unsafe/operator_less_than.rs +++ b/tests/unit/out/unsafe/operator_less_than.rs @@ -53,6 +53,6 @@ pub fn main() { unsafe fn main_0() -> i32 { let mut pair1: Pair = Pair { x: 1, y: 2 }; let mut pair2: Pair = Pair { x: 1, y: 3 }; - assert!((unsafe { Pair::operator_lt(&mut pair1, &pair2 as *const Pair,) })); + assert!((unsafe { Pair::operator_lt(&mut pair1, &pair2,) })); return 0; } diff --git a/tests/unit/out/unsafe/operator_logical_free.rs b/tests/unit/out/unsafe/operator_logical_free.rs index db3d5cd5..459c8329 100644 --- a/tests/unit/out/unsafe/operator_logical_free.rs +++ b/tests/unit/out/unsafe/operator_logical_free.rs @@ -30,39 +30,39 @@ unsafe fn main_0() -> i32 { let mut f: S = S { v: 0 }; assert!( (unsafe { - let _a: *const S = &f as *const S; + let _a: *const S = &f; operator_not_0(_a) }) ); assert!( !(unsafe { - let _a: *const S = &t as *const S; + let _a: *const S = &t; operator_not_0(_a) }) ); assert!( (unsafe { - let _a: *const S = &t as *const S; - let _b: *const S = &t as *const S; + let _a: *const S = &t; + let _b: *const S = &t; operator_and_1(_a, _b) }) ); assert!( !(unsafe { - let _a: *const S = &t as *const S; - operator_and_1(_a, &f as *const S) + let _a: *const S = &t; + operator_and_1(_a, &f) }) ); assert!( (unsafe { - let _a: *const S = &t as *const S; - operator_or_2(_a, &f as *const S) + let _a: *const S = &t; + operator_or_2(_a, &f) }) ); assert!( !(unsafe { - let _a: *const S = &f as *const S; - let _b: *const S = &f as *const S; + let _a: *const S = &f; + let _b: *const S = &f; operator_or_2(_a, _b) }) ); diff --git a/tests/unit/out/unsafe/operator_logical_member.rs b/tests/unit/out/unsafe/operator_logical_member.rs index d6854ea2..d0c47a73 100644 --- a/tests/unit/out/unsafe/operator_logical_member.rs +++ b/tests/unit/out/unsafe/operator_logical_member.rs @@ -34,15 +34,15 @@ unsafe fn main_0() -> i32 { assert!(!(unsafe { S::operator_not(&t,) })); assert!( (unsafe { - let _o: *const S = &t as *const S; + let _o: *const S = &t; S::operator_and(&t, _o) }) ); - assert!(!(unsafe { S::operator_and(&t, &f as *const S,) })); - assert!((unsafe { S::operator_or(&t, &f as *const S,) })); + assert!(!(unsafe { S::operator_and(&t, &f,) })); + assert!((unsafe { S::operator_or(&t, &f,) })); assert!( !(unsafe { - let _o: *const S = &f as *const S; + let _o: *const S = &f; S::operator_or(&f, _o) }) ); diff --git a/tests/unit/out/unsafe/operator_member_pointer_free.rs b/tests/unit/out/unsafe/operator_member_pointer_free.rs index fb4920e4..ba261236 100644 --- a/tests/unit/out/unsafe/operator_member_pointer_free.rs +++ b/tests/unit/out/unsafe/operator_member_pointer_free.rs @@ -26,7 +26,7 @@ impl Default for S { } } pub unsafe fn operator_deref_0(s: *mut S) -> *mut Inner { - return &mut (*s).inner as *mut Inner; + return &mut (*s).inner; } pub unsafe fn operator_addr_1(s: *mut S) -> *mut i32 { return (&mut (*s).data[(0) as usize] as *mut i32); @@ -43,19 +43,19 @@ unsafe fn main_0() -> i32 { }; assert!( (((*(unsafe { - let _s: *mut S = &mut s as *mut S; + let _s: *mut S = &mut s; operator_deref_0(_s) })) .x) == (9)) ); (*(unsafe { - let _s: *mut S = &mut s as *mut S; + let _s: *mut S = &mut s; operator_deref_0(_s) })) .x = 10; assert!(((s.inner.x) == (10))); let mut p: *mut i32 = (unsafe { - let _s: *mut S = &mut s as *mut S; + let _s: *mut S = &mut s; operator_addr_1(_s) }); assert!(((*p) == (1))); diff --git a/tests/unit/out/unsafe/operator_member_pointer_member.rs b/tests/unit/out/unsafe/operator_member_pointer_member.rs index bcc55d81..f1836980 100644 --- a/tests/unit/out/unsafe/operator_member_pointer_member.rs +++ b/tests/unit/out/unsafe/operator_member_pointer_member.rs @@ -16,7 +16,7 @@ pub struct Inner { pub struct Table {} impl Table { pub unsafe fn operator_index(mut i: i32) -> *mut i32 { - return &mut table_0[(i) as usize] as *mut i32; + return &mut table_0[(i) as usize]; } } pub static mut table_0: [i32; 3] = unsafe { [7, 8, 9] }; @@ -28,13 +28,13 @@ pub struct S { } impl S { pub unsafe fn operator_index_i32(&mut self, mut i: i32) -> *mut i32 { - return &mut self.data[(i) as usize] as *mut i32; + return &mut self.data[(i) as usize]; } pub unsafe fn operator_index_i32_const(&self, mut i: i32) -> *const i32 { - return &self.data[(i) as usize] as *const i32; + return &self.data[(i) as usize]; } pub unsafe fn operator_deref(&mut self) -> *mut Inner { - return &mut self.inner as *mut Inner; + return &mut self.inner; } pub unsafe fn operator_arrow(&mut self) -> *mut Inner { return (&mut self.inner as *mut Inner); @@ -64,7 +64,7 @@ unsafe fn main_0() -> i32 { assert!(((*(unsafe { S::operator_index_i32(&mut s, 1,) })) == (2))); (*(unsafe { S::operator_index_i32(&mut s, 1) })) = 20; assert!(((*(unsafe { S::operator_index_i32(&mut s, 1,) })) == (20))); - let cs: *const S = &s as *const S; + let cs: *const S = &s; assert!(((*(unsafe { S::operator_index_i32_const(&(*cs), 2,) })) == (3))); assert!((((*(unsafe { S::operator_deref(&mut s,) })).x) == (9))); (*(unsafe { S::operator_deref(&mut s) })).x = 10; diff --git a/tests/unit/out/unsafe/operator_other_free.rs b/tests/unit/out/unsafe/operator_other_free.rs index e927438f..5d6da5e5 100644 --- a/tests/unit/out/unsafe/operator_other_free.rs +++ b/tests/unit/out/unsafe/operator_other_free.rs @@ -26,18 +26,18 @@ unsafe fn main_0() -> i32 { let mut t: S = S { v: 4 }; assert!( (((unsafe { - let _a: *const S = &s as *const S; - operator_comma_0(_a, &t as *const S) + let _a: *const S = &s; + operator_comma_0(_a, &t) }) .v) == (34)) ); assert!( (((unsafe { let mut _a: S = (unsafe { - let _a: *const S = &s as *const S; - operator_comma_0(_a, &t as *const S) + let _a: *const S = &s; + operator_comma_0(_a, &t) }); - let _b: *const S = &s as *const S; + let _b: *const S = &s; operator_comma_0(&mut _a, _b) }) .v) == (343)) diff --git a/tests/unit/out/unsafe/operator_other_member.rs b/tests/unit/out/unsafe/operator_other_member.rs index de4d9ced..03c53a9a 100644 --- a/tests/unit/out/unsafe/operator_other_member.rs +++ b/tests/unit/out/unsafe/operator_other_member.rs @@ -52,7 +52,7 @@ unsafe fn main_0() -> i32 { assert!(((unsafe { S::operator_call_const(&s,) }) == (3))); assert!(((unsafe { S::operator_call_i32_const(&s, 1,) }) == (4))); assert!(((unsafe { S::operator_call_i32_i32_const(&s, 1, 2,) }) == (6))); - assert!((((unsafe { S::operator_comma(&s, &t as *const S,) }).v) == (34))); + assert!((((unsafe { S::operator_comma(&s, &t,) }).v) == (34))); let mut i: i32 = (unsafe { S::operator_int(&s) }); assert!(((i) == (3))); assert!((((unsafe { S::operator_int(&s,) }) + (1)) == (4))); diff --git a/tests/unit/out/unsafe/operator_overloads.rs b/tests/unit/out/unsafe/operator_overloads.rs index 4a304668..50de25ab 100644 --- a/tests/unit/out/unsafe/operator_overloads.rs +++ b/tests/unit/out/unsafe/operator_overloads.rs @@ -64,14 +64,14 @@ unsafe fn main_0() -> i32 { assert!(((unsafe { S::operator_eq_i64_const(&s, 6_i64,) }) == (2))); assert!(((unsafe { S::operator_eq_f64_const(&s, 6.0E+0,) }) == (3))); assert!(((unsafe { S::operator_eq_i32_const(&s, 7,) }) == (0))); - assert!(((unsafe { S::operator_add(&s, &t as *const S,) }) == (10))); + assert!(((unsafe { S::operator_add(&s, &t,) }) == (10))); assert!(((unsafe { S::operator_sub(&s, t,) }) == (2))); - assert!(((unsafe { S::operator_mul_pconstS_const(&s, &t as *const S,) }) == (24))); + assert!(((unsafe { S::operator_mul_pconstS_const(&s, &t,) }) == (24))); assert!(((unsafe { S::operator_mul_i32_const(&s, 2,) }) == (13))); assert!( ((unsafe { - let _a: *const S = &s as *const S; - operator_div_0(_a, &t as *const S) + let _a: *const S = &s; + operator_div_0(_a, &t) }) == (1)) ); assert!( @@ -88,11 +88,11 @@ unsafe fn main_0() -> i32 { ); assert!( ((unsafe { - let _a: *const S = &s as *const S; + let _a: *const S = &s; operator_rem_3(_a, 4) }) == (3)) ); assert!(((unsafe { operator_eq_4(6, s,) }) == (4))); - assert!(((unsafe { operator_eq_5(6_i64, &s as *const S,) }) == (5))); + assert!(((unsafe { operator_eq_5(6_i64, &s,) }) == (5))); return 0; } diff --git a/tests/unit/out/unsafe/operator_three_way.rs b/tests/unit/out/unsafe/operator_three_way.rs index f4ee1154..3145dbc7 100644 --- a/tests/unit/out/unsafe/operator_three_way.rs +++ b/tests/unit/out/unsafe/operator_three_way.rs @@ -49,11 +49,11 @@ pub fn main() { unsafe fn main_0() -> i32 { let mut a: S = S { v: 1 }; let mut b: S = S { v: 2 }; - assert!((unsafe { S::operator_cmp(&a, &b as *const S,) }) == std::cmp::Ordering::Less); - assert!((unsafe { S::operator_cmp(&b, &a as *const S,) }) == std::cmp::Ordering::Greater); - assert!((unsafe { S::operator_cmp(&a, &b as *const S,) }) != std::cmp::Ordering::Greater); - assert!((unsafe { S::operator_cmp(&b, &a as *const S,) }) != std::cmp::Ordering::Less); - assert!(!(unsafe { S::operator_eq(&a, &b as *const S,) })); - assert!((unsafe { S::operator_cmp(&a, &b as *const S,) }) == std::cmp::Ordering::Less); + assert!((unsafe { S::operator_cmp(&a, &b,) }) == std::cmp::Ordering::Less); + assert!((unsafe { S::operator_cmp(&b, &a,) }) == std::cmp::Ordering::Greater); + assert!((unsafe { S::operator_cmp(&a, &b,) }) != std::cmp::Ordering::Greater); + assert!((unsafe { S::operator_cmp(&b, &a,) }) != std::cmp::Ordering::Less); + assert!(!(unsafe { S::operator_eq(&a, &b,) })); + assert!((unsafe { S::operator_cmp(&a, &b,) }) == std::cmp::Ordering::Less); return 0; } diff --git a/tests/unit/out/unsafe/pod.rs b/tests/unit/out/unsafe/pod.rs index cf4af260..181a2b9a 100644 --- a/tests/unit/out/unsafe/pod.rs +++ b/tests/unit/out/unsafe/pod.rs @@ -34,7 +34,7 @@ unsafe fn main_0() -> i32 { x2: p1.x2, x3: p1.x3, }; - (unsafe { PODIncrement_0(&mut p2 as *mut POD) }); + (unsafe { PODIncrement_0(&mut p2) }); assert!(((((p2.x1) + (p2.x2)) + (p2.x3)) == (39))); return 0; } diff --git a/tests/unit/out/unsafe/pointer_array.rs b/tests/unit/out/unsafe/pointer_array.rs index 0daf128b..12111292 100644 --- a/tests/unit/out/unsafe/pointer_array.rs +++ b/tests/unit/out/unsafe/pointer_array.rs @@ -39,7 +39,7 @@ unsafe fn main_0() -> i32 { (&mut x as *mut i32), ], }; - (unsafe { IncrementAll_0(&mut s as *mut StackArray) }); + (unsafe { IncrementAll_0(&mut s) }); assert!(((x) == (3))); return 0; } diff --git a/tests/unit/out/unsafe/printfs.rs b/tests/unit/out/unsafe/printfs.rs index 636b5163..55fe7854 100644 --- a/tests/unit/out/unsafe/printfs.rs +++ b/tests/unit/out/unsafe/printfs.rs @@ -53,7 +53,7 @@ unsafe fn main_0() -> i32 { ); printf( c"%s\n".as_ptr() as *const i8, - (*(unsafe { fn2_1(&s as *const Vec) })).as_ptr(), + (*(unsafe { fn2_1(&s) })).as_ptr(), ); return 0; } diff --git a/tests/unit/out/unsafe/prvalue-as-lvalue.rs b/tests/unit/out/unsafe/prvalue-as-lvalue.rs index 935f5325..7dfaf49a 100644 --- a/tests/unit/out/unsafe/prvalue-as-lvalue.rs +++ b/tests/unit/out/unsafe/prvalue-as-lvalue.rs @@ -17,7 +17,7 @@ pub fn main() { unsafe fn main_0() -> i32 { let mut a: i32 = 1; let mut pa: *mut i32 = (&mut a as *mut i32); - let b: *const i32 = (unsafe { foo_0(&(*pa) as *const i32) }); + let b: *const i32 = (unsafe { foo_0(&(*pa)) }); assert!(((*b) == (1))); return 0; } diff --git a/tests/unit/out/unsafe/random.rs b/tests/unit/out/unsafe/random.rs index 22e4c969..106726b1 100644 --- a/tests/unit/out/unsafe/random.rs +++ b/tests/unit/out/unsafe/random.rs @@ -31,7 +31,7 @@ impl Pair { return self.x; } pub unsafe fn as_ref(&mut self) -> *mut i32 { - return &mut self.x as *mut i32; + return &mut self.x; } pub unsafe fn as_ptr(&mut self) -> *mut i32 { return (&mut self.x as *mut i32); @@ -66,13 +66,13 @@ pub fn main() { unsafe fn main_0() -> i32 { let mut x1: i32 = 1; let mut c1: i32 = x1; - let rx1: *mut i32 = &mut x1 as *mut i32; + let rx1: *mut i32 = &mut x1; let mut px1: *mut i32 = (&mut x1 as *mut i32); let mut x2: i32 = (*rx1); let rx2: *mut i32 = rx1; let mut px2: *mut i32 = (rx1); let mut x3: i32 = (*px1); - let rx3: *mut i32 = &mut (*px1) as *mut i32; + let rx3: *mut i32 = &mut (*px1); let mut px3: *mut i32 = px1; let mut res: i32 = ((x1) + (x2)); res = ((x1) + (x2)); @@ -80,7 +80,7 @@ unsafe fn main_0() -> i32 { x: 1, y: 2, a: [1, 2, 3, 4, 5], - r: &mut x1 as *mut i32, + r: &mut x1, p: std::ptr::null_mut(), pair: std::ptr::null_mut(), ap: [std::ptr::null_mut(), std::ptr::null_mut()], @@ -100,7 +100,7 @@ unsafe fn main_0() -> i32 { pair: y1.pair, ap: [y1.ap[(0) as usize], y1.ap[(1) as usize]], }; - let ry1: *mut Pair = &mut y1 as *mut Pair; + let ry1: *mut Pair = &mut y1; let mut py1: *mut Pair = (&mut y1 as *mut Pair); let mut y2: Pair = Pair { x: (*ry1).x, @@ -134,15 +134,15 @@ unsafe fn main_0() -> i32 { pair: (*py1).pair, ap: [(*py1).ap[(0) as usize], (*py1).ap[(1) as usize]], }; - let ry3: *mut Pair = &mut (*py1) as *mut Pair; + let ry3: *mut Pair = &mut (*py1); let mut py3: *mut Pair = py1; py3 = std::ptr::null_mut(); let mut ptr2pair: *mut Pair = py3; (unsafe { let _x1: i32 = x1; - let _x2: *mut i32 = &mut x1 as *mut i32; + let _x2: *mut i32 = &mut x1; let _x3: *mut i32 = (&mut x1 as *mut i32); - let _p2: *mut Pair = &mut y1 as *mut Pair; + let _p2: *mut Pair = &mut y1; let _p3: *mut Pair = (&mut y1 as *mut Pair); foo_1(_x1, _x2, _x3, _p2, _p3) }); @@ -156,13 +156,13 @@ unsafe fn main_0() -> i32 { }); (unsafe { let _x1: i32 = (*px1); - let _x2: *mut i32 = &mut (*px1) as *mut i32; + let _x2: *mut i32 = &mut (*px1); let _x3: *mut i32 = px1; - let _p2: *mut Pair = &mut (*py1) as *mut Pair; + let _p2: *mut Pair = &mut (*py1); let _p3: *mut Pair = py1; foo_1(_x1, _x2, _x3, _p2, _p3) }); - let cr1: *mut i32 = &mut c1 as *mut i32; + let cr1: *mut i32 = &mut c1; let mut cp1: *mut i32 = (&mut c1 as *mut i32); x1 = c1; x1 = 1; @@ -196,7 +196,7 @@ unsafe fn main_0() -> i32 { x: 1, y: 2, a: [1, 2, 3, 4, 5], - r: &mut j as *mut i32, + r: &mut j, p: std::ptr::null_mut(), pair: std::ptr::null_mut(), ap: [std::ptr::null_mut(), std::ptr::null_mut()], diff --git a/tests/unit/out/unsafe/rebind.rs b/tests/unit/out/unsafe/rebind.rs index 3f13e57b..bd96f5c8 100644 --- a/tests/unit/out/unsafe/rebind.rs +++ b/tests/unit/out/unsafe/rebind.rs @@ -13,7 +13,7 @@ pub fn main() { } unsafe fn main_0() -> i32 { let mut x: i32 = 1; - let r: *mut i32 = &mut x as *mut i32; + let r: *mut i32 = &mut x; let mut y: i32 = 10; (*r) = y; y += 1; diff --git a/tests/unit/out/unsafe/ref_calls.rs b/tests/unit/out/unsafe/ref_calls.rs index f04d2505..8a1f4e56 100644 --- a/tests/unit/out/unsafe/ref_calls.rs +++ b/tests/unit/out/unsafe/ref_calls.rs @@ -19,11 +19,10 @@ pub fn main() { } unsafe fn main_0() -> i32 { let mut x: i32 = 5; - let mut y: i32 = (*(unsafe { foo_1(&mut x as *mut i32) })); - let z: *mut i32 = (unsafe { foo_1(&mut x as *mut i32) }); + let mut y: i32 = (*(unsafe { foo_1(&mut x) })); + let z: *mut i32 = (unsafe { foo_1(&mut x) }); assert!( - (((((*(unsafe { foo_1(&mut x as *mut i32,) })) - + (*(unsafe { foo_1(&mut y as *mut i32,) }))) + (((((*(unsafe { foo_1(&mut x,) })) + (*(unsafe { foo_1(&mut y,) }))) + (*(unsafe { foo_1(z,) }))) + (unsafe { bar_0() })) == (16)) diff --git a/tests/unit/out/unsafe/references.rs b/tests/unit/out/unsafe/references.rs index 957787eb..13c26f77 100644 --- a/tests/unit/out/unsafe/references.rs +++ b/tests/unit/out/unsafe/references.rs @@ -13,7 +13,7 @@ pub fn main() { } unsafe fn main_0() -> i32 { let mut x: i32 = 1; - let r: *mut i32 = &mut x as *mut i32; + let r: *mut i32 = &mut x; (*r) = 5; assert!(((x) == (5))); return 0; diff --git a/tests/unit/out/unsafe/references2.rs b/tests/unit/out/unsafe/references2.rs index 8e3254e5..812b637d 100644 --- a/tests/unit/out/unsafe/references2.rs +++ b/tests/unit/out/unsafe/references2.rs @@ -17,7 +17,7 @@ pub fn main() { } unsafe fn main_0() -> i32 { let mut a: Option> = Some(Box::new(5)); - (unsafe { change_0(&mut a as *mut Option>) }); + (unsafe { change_0(&mut a) }); assert!(((*a.as_deref_mut().unwrap()) == (7))); return 0; } diff --git a/tests/unit/out/unsafe/refs_as_args.rs b/tests/unit/out/unsafe/refs_as_args.rs index 2c8923db..b71d10a6 100644 --- a/tests/unit/out/unsafe/refs_as_args.rs +++ b/tests/unit/out/unsafe/refs_as_args.rs @@ -7,12 +7,12 @@ use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; pub unsafe fn more_refs_0(mut x1: i32, mut x2: i32, r1: *mut i32, r2: *const i32) { - let rx1: *const i32 = &x1 as *const i32; - let rx2: *mut i32 = &mut x2 as *mut i32; + let rx1: *const i32 = &x1; + let rx2: *mut i32 = &mut x2; let mut pr1: *const i32 = (r1).cast_const(); let mut pr2: *const i32 = (r2); - let rpr1: *const i32 = &(*pr1) as *const i32; - let rpr2: *const i32 = &(*pr2) as *const i32; + let rpr1: *const i32 = &(*pr1); + let rpr2: *const i32 = &(*pr2); let r: *const i32 = r1; (*rx2) += ((((((((1) + (*rx1)) + (*rx2)) + (*pr1)) + (*pr2)) + (*rpr1)) + (*rpr2)) + (*r)); (*r1) = (*rx2); @@ -25,7 +25,7 @@ pub fn main() { unsafe fn main_0() -> i32 { let mut x1: i32 = 1; let x2: i32 = 2; - (unsafe { more_refs_0(3, 4, &mut x1 as *mut i32, &x2 as *const i32) }); + (unsafe { more_refs_0(3, 4, &mut x1, &x2) }); assert!((((x1) + (x2)) == (21))); return 0; } diff --git a/tests/unit/out/unsafe/rule_of_five.rs b/tests/unit/out/unsafe/rule_of_five.rs index 28bb9596..d3878224 100644 --- a/tests/unit/out/unsafe/rule_of_five.rs +++ b/tests/unit/out/unsafe/rule_of_five.rs @@ -64,7 +64,7 @@ impl Buffer { } pub unsafe fn operator_assign_pconstBuffer(&mut self, o: *const Buffer) -> *mut Buffer { if (((self as *mut Buffer).cast_const()) == (o)) { - return &mut (*(self as *mut Buffer)) as *mut Buffer; + return &mut (*(self as *mut Buffer)); } self.size = (*o).size; let mut i: i32 = 0; @@ -73,11 +73,11 @@ impl Buffer { i.prefix_inc(); } copies_1.prefix_inc(); - return &mut (*(self as *mut Buffer)) as *mut Buffer; + return &mut (*(self as *mut Buffer)); } pub unsafe fn operator_assign_pmutBuffer(&mut self, o: *mut Buffer) -> *mut Buffer { if ((self as *mut Buffer) == (o)) { - return &mut (*(self as *mut Buffer)) as *mut Buffer; + return &mut (*(self as *mut Buffer)); } self.size = (*o).size; let mut i: i32 = 0; @@ -88,7 +88,7 @@ impl Buffer { } (*o).size = 0; moves_2.prefix_inc(); - return &mut (*(self as *mut Buffer)) as *mut Buffer; + return &mut (*(self as *mut Buffer)); } } impl Clone for Buffer { @@ -107,7 +107,7 @@ impl Default for Buffer { pub unsafe fn make_3(mut size: i32) -> Buffer { let mut b: Buffer = Buffer::Buffer({ size }); let _dtor_b = ScopedDestructorUnsafe::new(&raw mut b, Buffer::destructor); - return Buffer::Buffer_pmutBuffer({ &mut b as *mut Buffer }); + return Buffer::Buffer_pmutBuffer({ &mut b }); } pub fn main() { unsafe { @@ -118,12 +118,12 @@ unsafe fn main_0() -> i32 { { let mut a: Buffer = Buffer::Buffer({ 4 }); let _dtor_a = ScopedDestructorUnsafe::new(&raw mut a, Buffer::destructor); - let mut b: Buffer = Buffer::Buffer_pconstBuffer({ &a as *const Buffer }); + let mut b: Buffer = Buffer::Buffer_pconstBuffer({ &a }); let _dtor_b = ScopedDestructorUnsafe::new(&raw mut b, Buffer::destructor); assert!((((alive_0) == (2)) && ((copies_1) == (1))) && ((moves_2) == (0))); b.data[(0) as usize] = 100; assert!(((a.data[(0) as usize]) == (0))); - let mut c: Buffer = Buffer::Buffer_pmutBuffer({ &mut a as *mut Buffer }); + let mut c: Buffer = Buffer::Buffer_pmutBuffer({ &mut a }); let _dtor_c = ScopedDestructorUnsafe::new(&raw mut c, Buffer::destructor); assert!(((alive_0) == (3)) && ((moves_2) == (1))); assert!(((a.size) == (0)) && ((a.data[(0) as usize]) == (-1_i32))); @@ -131,12 +131,12 @@ unsafe fn main_0() -> i32 { let mut d: Buffer = (unsafe { make_3(2) }); let _dtor_d = ScopedDestructorUnsafe::new(&raw mut d, Buffer::destructor); assert!(((d.size) == (2)) && ((moves_2) == (2))); - (unsafe { Buffer::operator_assign_pconstBuffer(&mut d, &b as *const Buffer) }); + (unsafe { Buffer::operator_assign_pconstBuffer(&mut d, &b) }); assert!((((d.size) == (4)) && ((d.data[(0) as usize]) == (100))) && ((copies_1) == (2))); - (unsafe { Buffer::operator_assign_pmutBuffer(&mut d, &mut c as *mut Buffer) }); + (unsafe { Buffer::operator_assign_pmutBuffer(&mut d, &mut c) }); assert!((((d.data[(0) as usize]) == (0)) && ((c.size) == (0))) && ((moves_2) == (3))); (unsafe { - let _o: *mut Buffer = &mut d as *mut Buffer; + let _o: *mut Buffer = &mut d; Buffer::operator_assign_pmutBuffer(&mut d, _o) }); assert!(((d.size) == (4)) && ((moves_2) == (3))); diff --git a/tests/unit/out/unsafe/rule_of_three.rs b/tests/unit/out/unsafe/rule_of_three.rs index c615f6a1..b08a1eed 100644 --- a/tests/unit/out/unsafe/rule_of_three.rs +++ b/tests/unit/out/unsafe/rule_of_three.rs @@ -47,7 +47,7 @@ impl Buffer { } pub unsafe fn operator_assign(&mut self, o: *const Buffer) -> *mut Buffer { if (((self as *mut Buffer).cast_const()) == (o)) { - return &mut (*(self as *mut Buffer)) as *mut Buffer; + return &mut (*(self as *mut Buffer)); } self.size = (*o).size; let mut i: i32 = 0; @@ -56,7 +56,7 @@ impl Buffer { i.prefix_inc(); } copies_1.prefix_inc(); - return &mut (*(self as *mut Buffer)) as *mut Buffer; + return &mut (*(self as *mut Buffer)); } } impl Clone for Buffer { @@ -90,28 +90,28 @@ unsafe fn main_0() -> i32 { { let mut a: Buffer = Buffer::Buffer({ 4 }); let _dtor_a = ScopedDestructorUnsafe::new(&raw mut a, Buffer::destructor); - let mut b: Buffer = Buffer::Buffer_pconstBuffer({ &a as *const Buffer }); + let mut b: Buffer = Buffer::Buffer_pconstBuffer({ &a }); let _dtor_b = ScopedDestructorUnsafe::new(&raw mut b, Buffer::destructor); assert!(((alive_0) == (2)) && ((copies_1) == (1))); b.data[(0) as usize] = 100; assert!(((a.data[(0) as usize]) == (0))); let mut c: Buffer = Buffer::Buffer({ 2 }); let _dtor_c = ScopedDestructorUnsafe::new(&raw mut c, Buffer::destructor); - (unsafe { Buffer::operator_assign(&mut c, &a as *const Buffer) }); + (unsafe { Buffer::operator_assign(&mut c, &a) }); assert!(((c.size) == (4)) && ((c.data[(3) as usize]) == (3))); assert!(((alive_0) == (3)) && ((copies_1) == (2))); (unsafe { - let _o: *const Buffer = &c as *const Buffer; + let _o: *const Buffer = &c; Buffer::operator_assign(&mut c, _o) }); assert!(((copies_1) == (2))); - assert!(((unsafe { sum_2(&a as *const Buffer,) }) == (6))); - assert!(((unsafe { sum_2(&b as *const Buffer,) }) == (106))); - let mut d: Buffer = Buffer::Buffer_pconstBuffer({ &a as *const Buffer }); + assert!(((unsafe { sum_2(&a,) }) == (6))); + assert!(((unsafe { sum_2(&b,) }) == (106))); + let mut d: Buffer = Buffer::Buffer_pconstBuffer({ &a }); let _dtor_d = ScopedDestructorUnsafe::new(&raw mut d, Buffer::destructor); assert!(((alive_0) == (4)) && ((copies_1) == (3))); assert!(((a.size) == (4)) && ((a.data[(3) as usize]) == (3))); - (unsafe { Buffer::operator_assign(&mut d, &b as *const Buffer) }); + (unsafe { Buffer::operator_assign(&mut d, &b) }); assert!(((copies_1) == (4))); assert!(((b.data[(0) as usize]) == (100)) && ((d.data[(0) as usize]) == (100))); } diff --git a/tests/unit/out/unsafe/rvalue_ref_general.rs b/tests/unit/out/unsafe/rvalue_ref_general.rs index 12c523d4..c5b6ce67 100644 --- a/tests/unit/out/unsafe/rvalue_ref_general.rs +++ b/tests/unit/out/unsafe/rvalue_ref_general.rs @@ -30,7 +30,7 @@ unsafe fn main_0() -> i32 { assert!(((*i6) == (*i3))); assert!(((*i7) == (*i4))); let mut i8: i32 = 3; - let i9: *mut i32 = &mut i8 as *mut i32; + let i9: *mut i32 = &mut i8; assert!(((*i9) == (3))); let mut p1: *mut i32 = (&mut i1 as *mut i32); let mut p2: *mut i32 = (i3); diff --git a/tests/unit/out/unsafe/rvalue_struct.rs b/tests/unit/out/unsafe/rvalue_struct.rs index af9a0225..8d31bde0 100644 --- a/tests/unit/out/unsafe/rvalue_struct.rs +++ b/tests/unit/out/unsafe/rvalue_struct.rs @@ -25,7 +25,7 @@ pub fn main() { } unsafe fn main_0() -> i32 { let mut s1: S = S::S({ 1 }, { 2 }); - let s2: *mut S = &mut s1 as *mut S; + let s2: *mut S = &mut s1; assert!((((*s2).a) == (1))); assert!((((*s2).b) == (2))); return 0; diff --git a/tests/unit/out/unsafe/split_binop_aliased_borrows.rs b/tests/unit/out/unsafe/split_binop_aliased_borrows.rs index c8383e39..561b4b24 100644 --- a/tests/unit/out/unsafe/split_binop_aliased_borrows.rs +++ b/tests/unit/out/unsafe/split_binop_aliased_borrows.rs @@ -14,7 +14,7 @@ pub fn main() { unsafe fn main_0() -> i32 { let mut v: Vec = vec![1, 2]; let mut p: *mut i32 = v.as_mut_ptr(); - let r: *const i32 = &v[(1_usize)] as *const i32; + let r: *const i32 = &v[(1_usize)]; (*p) = (*r); assert!(((v[(0_usize)]) == (2))); return 0; diff --git a/tests/unit/out/unsafe/struct_ctor.rs b/tests/unit/out/unsafe/struct_ctor.rs index 45ce263a..7e2d6c9c 100644 --- a/tests/unit/out/unsafe/struct_ctor.rs +++ b/tests/unit/out/unsafe/struct_ctor.rs @@ -20,10 +20,10 @@ impl StructWithCtor { this } pub unsafe fn x1(&self) -> *const i32 { - return &self.x1_ as *const i32; + return &self.x1_; } pub unsafe fn x2(&self) -> *const i32 { - return &self.x2_ as *const i32; + return &self.x2_; } } pub unsafe fn foo_0(x: *mut i32) -> *mut i32 { @@ -38,7 +38,7 @@ unsafe fn main_0() -> i32 { let mut struct_with_ctor: StructWithCtor = StructWithCtor::StructWithCtor({ 1 }, { 2 }); let mut x: i32 = 3; assert!( - (((*(unsafe { foo_0(&mut x as *mut i32,) })) == (3)) + (((*(unsafe { foo_0(&mut x,) })) == (3)) && ((*(unsafe { StructWithCtor::x1(&struct_with_ctor,) })) == (2))) && ((*(unsafe { StructWithCtor::x2(&struct_with_ctor,) })) == (1)) ); diff --git a/tests/unit/out/unsafe/swap.rs b/tests/unit/out/unsafe/swap.rs index 08bfb83c..6ca3f645 100644 --- a/tests/unit/out/unsafe/swap.rs +++ b/tests/unit/out/unsafe/swap.rs @@ -33,7 +33,7 @@ unsafe fn main_0() -> i32 { p = (&mut (b) as *mut i32); p = (&mut a as *mut i32); (unsafe { swap_by_ptr_1(p, (&mut b as *mut i32)) }); - (unsafe { swap_by_ref_2(&mut a as *mut i32, &mut c as *mut i32) }); + (unsafe { swap_by_ref_2(&mut a, &mut c) }); assert!(((c) == (2))); return 0; } diff --git a/tests/unit/out/unsafe/swap_extended.rs b/tests/unit/out/unsafe/swap_extended.rs index 8e7a8fd4..902a6898 100644 --- a/tests/unit/out/unsafe/swap_extended.rs +++ b/tests/unit/out/unsafe/swap_extended.rs @@ -68,7 +68,7 @@ unsafe fn main_0() -> i32 { (unsafe { swap_by_ptr_1((&mut d as *mut i32), (&mut e as *mut i32)) }); let mut f: i32 = 4; let mut g: i32 = 5; - (unsafe { swap_by_ref_2(&mut f as *mut i32, &mut g as *mut i32) }); + (unsafe { swap_by_ref_2(&mut f, &mut g) }); let mut h: *mut i32 = (Box::leak(Box::new(6)) as *mut i32); write!( std::fs::File::from_raw_fd( @@ -114,14 +114,14 @@ unsafe fn main_0() -> i32 { }); (unsafe { swap_by_ref_2( - &mut (*(Box::leak(Box::new(9)) as *mut i32)) as *mut i32, - &mut (*(Box::leak(Box::new(10)) as *mut i32)) as *mut i32, + &mut (*(Box::leak(Box::new(9)) as *mut i32)), + &mut (*(Box::leak(Box::new(10)) as *mut i32)), ) }); (unsafe { swap_by_ref_2( - &mut (*(Box::leak(Box::new(9)) as *mut i32).offset((0) as isize)) as *mut i32, - &mut (*(Box::leak(Box::new(10)) as *mut i32).offset((0) as isize)) as *mut i32, + &mut (*(Box::leak(Box::new(9)) as *mut i32).offset((0) as isize)), + &mut (*(Box::leak(Box::new(10)) as *mut i32).offset((0) as isize)), ) }); let mut j: Option> = Some(Box::from_raw((Box::leak(Box::new(11)) as *mut i32))); diff --git a/tests/unit/out/unsafe/this.rs b/tests/unit/out/unsafe/this.rs index 12438299..bd7ea5eb 100644 --- a/tests/unit/out/unsafe/this.rs +++ b/tests/unit/out/unsafe/this.rs @@ -43,14 +43,14 @@ impl S { this } pub unsafe fn returns_this_reference(&mut self) -> *mut S { - return &mut (*(self as *mut S)) as *mut S; + return &mut (*(self as *mut S)); } pub unsafe fn returns_this_pointer(&mut self) -> *mut S { return (self as *mut S); } pub unsafe fn inc(&mut self) -> *mut S { self.a_.postfix_inc(); - return &mut (*(self as *mut S)) as *mut S; + return &mut (*(self as *mut S)); } pub unsafe fn set_from_this(&mut self) { self.a_ = ((self.a_) + (1)); @@ -68,7 +68,7 @@ impl S { (unsafe { bump_0((self as *mut S)) }); } pub unsafe fn cref(&self) -> *const S { - return &(*(self as *const S)) as *const S; + return &(*(self as *const S)); } pub unsafe fn is(&self, mut o: *const S) -> bool { return ((o) == (self as *const S)); diff --git a/tests/unit/out/unsafe/unique_ptr.rs b/tests/unit/out/unsafe/unique_ptr.rs index 49990ba4..3dc2ecc4 100644 --- a/tests/unit/out/unsafe/unique_ptr.rs +++ b/tests/unit/out/unsafe/unique_ptr.rs @@ -26,7 +26,7 @@ impl SafePointer { _a0: *mut SafePointer, ) -> *mut SafePointer { self.ptr = (*_a0).ptr.take(); - return &mut (*(self as *mut SafePointer)) as *mut SafePointer; + return &mut (*(self as *mut SafePointer)); } } #[repr(C)] @@ -163,7 +163,7 @@ pub fn main() { unsafe fn main_0() -> i32 { let mut x: Option> = Some(Box::new(0)); let mut safe_ptr: Option> = Some(Box::new(SafePointer { ptr: x.take() })); - (unsafe { DoStuffWithSafePointer_0(&mut safe_ptr as *mut Option>) }); + (unsafe { DoStuffWithSafePointer_0(&mut safe_ptr) }); assert!(((unsafe { Consume_1(safe_ptr.take(),) }) == (60))); return 0; } diff --git a/tests/unit/out/unsafe/unique_ptr_const_deref.rs b/tests/unit/out/unsafe/unique_ptr_const_deref.rs index 22855a7f..0da6adde 100644 --- a/tests/unit/out/unsafe/unique_ptr_const_deref.rs +++ b/tests/unit/out/unsafe/unique_ptr_const_deref.rs @@ -20,7 +20,7 @@ impl Holder { } pub unsafe fn operator_assign_pmutHolder(&mut self, _a0: *mut Holder) -> *mut Holder { self.val = (*_a0).val.take(); - return &mut (*(self as *mut Holder)) as *mut Holder; + return &mut (*(self as *mut Holder)); } } pub unsafe fn read_val_0(mut h: *const Holder) -> i32 { diff --git a/tests/unit/out/unsafe/unique_ptr_nested.rs b/tests/unit/out/unsafe/unique_ptr_nested.rs index 26f0b820..f13ecaa3 100644 --- a/tests/unit/out/unsafe/unique_ptr_nested.rs +++ b/tests/unit/out/unsafe/unique_ptr_nested.rs @@ -26,7 +26,7 @@ impl Outer { } pub unsafe fn operator_assign_pmutOuter(&mut self, _a0: *mut Outer) -> *mut Outer { self.inner = (*_a0).inner.take(); - return &mut (*(self as *mut Outer)) as *mut Outer; + return &mut (*(self as *mut Outer)); } } pub fn main() { diff --git a/tests/unit/out/unsafe/unique_ptr_small.rs b/tests/unit/out/unsafe/unique_ptr_small.rs index 9abcb151..4645417f 100644 --- a/tests/unit/out/unsafe/unique_ptr_small.rs +++ b/tests/unit/out/unsafe/unique_ptr_small.rs @@ -17,7 +17,7 @@ pub fn main() { } unsafe fn main_0() -> i32 { let mut n: Option> = Some(Box::new(10)); - (unsafe { change_0(&mut n as *mut Option>) }); + (unsafe { change_0(&mut n) }); assert!(((*n.as_deref_mut().unwrap()) == (20))); return 0; } diff --git a/tests/unit/out/unsafe/vector2.rs b/tests/unit/out/unsafe/vector2.rs index b23aebcd..1fd2d1a7 100644 --- a/tests/unit/out/unsafe/vector2.rs +++ b/tests/unit/out/unsafe/vector2.rs @@ -44,6 +44,6 @@ unsafe fn main_0() -> i32 { v.push(6); v2.push(8); v2.push(9); - (unsafe { fn_0(&mut v as *mut Vec, v2.clone()) }); + (unsafe { fn_0(&mut v, v2.clone()) }); return 0; } diff --git a/tests/unit/out/unsafe/vector_with_allocator.rs b/tests/unit/out/unsafe/vector_with_allocator.rs index 44d0acdb..178b6609 100644 --- a/tests/unit/out/unsafe/vector_with_allocator.rs +++ b/tests/unit/out/unsafe/vector_with_allocator.rs @@ -185,7 +185,7 @@ unsafe fn main_0() -> i32 { v7.push(6); v8.push(8); v8.push(9); - (unsafe { fn_1(&mut v7 as *mut Vec, v8.clone()) }); + (unsafe { fn_1(&mut v7, v8.clone()) }); let mut src: [u32; 3] = [1_u32, 2_u32, 3_u32]; let mut v9: Vec = core::slice::from_raw_parts( src.as_mut_ptr(), diff --git a/tests/unit/out/unsafe/void_cast.rs b/tests/unit/out/unsafe/void_cast.rs index 19f23cc1..71f34c2f 100644 --- a/tests/unit/out/unsafe/void_cast.rs +++ b/tests/unit/out/unsafe/void_cast.rs @@ -47,7 +47,7 @@ impl NonCopyable { _a0: *mut NonCopyable, ) -> *mut NonCopyable { self.value = (*_a0).value.take(); - return &mut (*(self as *mut NonCopyable)) as *mut NonCopyable; + return &mut (*(self as *mut NonCopyable)); } } pub unsafe fn unused_noncopyable_param_5(x: *const NonCopyable) { @@ -116,14 +116,14 @@ unsafe fn main_0() -> i32 { let mut hp: *mut Holder = (&mut h as *mut Holder); &((*hp).field); let mut nt: NonTrivial = ::default(); - (unsafe { unused_ref_param_1(&nt as *const NonTrivial) }); + (unsafe { unused_ref_param_1(&nt) }); (unsafe { unused_ptr_param_2((&mut nt as *mut NonTrivial).cast_const()) }); let mut g: NonCopyable = NonCopyable { value: Some(Box::new(9)), }; (&(g)); &(g); - (unsafe { unused_noncopyable_param_5(&g as *const NonCopyable) }); + (unsafe { unused_noncopyable_param_5(&g) }); assert!(((*g.value.as_deref_mut().unwrap()) == (9))); return 0; } From b2c18d2731d4d2a7848b4ff55b1f0d69309c712b Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Mon, 14 Sep 2026 20:37:13 +0100 Subject: [PATCH 09/16] Call user defined move constructor instead of mem::take --- cpp2rust/converter/converter.cpp | 26 ++++++++++++------- cpp2rust/converter/converter_lib.cpp | 14 ++++++++++ cpp2rust/converter/converter_lib.h | 5 ++++ .../unit/out/refcount/copy_move_defaulted.rs | 2 +- tests/unit/out/refcount/move_ctor.rs | 2 +- tests/unit/out/refcount/push_emplace_back.rs | 4 +-- tests/unit/out/unsafe/copy_move_defaulted.rs | 2 +- tests/unit/out/unsafe/move_ctor.rs | 2 +- tests/unit/out/unsafe/push_emplace_back.rs | 2 +- 9 files changed, 42 insertions(+), 17 deletions(-) diff --git a/cpp2rust/converter/converter.cpp b/cpp2rust/converter/converter.cpp index 4e1cccea..5da20fd8 100644 --- a/cpp2rust/converter/converter.cpp +++ b/cpp2rust/converter/converter.cpp @@ -4411,15 +4411,8 @@ void Converter::AddDefaultTrait(const clang::RecordDecl *decl) { if (auto *default_ctor = GetUserDefinedDefaultConstructor(cxx)) { StrCat(keyword_unsafe_); PushBrace unsafe_brace(*this); - Convert(clang::CXXConstructExpr::Create( - ctx_, ctx_.getCanonicalTagType(decl), clang::SourceLocation(), - default_ctor, - /*Elidable=*/false, llvm::ArrayRef(), - /*HadMultipleCandidates=*/false, - /*ListInitialization=*/false, - /*StdInitListInitialization=*/false, - /*ZeroInitialization=*/false, clang::CXXConstructionKind::Complete, - clang::SourceRange())); + Convert(MakeConstructExpr(ctx_, ctx_.getCanonicalTagType(decl), + default_ctor, {})); return; } } @@ -4651,6 +4644,21 @@ std::string Converter::ConvertPlaceholder(clang::Expr *expr, clang::Expr *arg, if (clang::isa(arg)) { return ConvertRValue(arg); } + if (auto *record = arg->getType()->getAsCXXRecordDecl(); + record && IsUserDefinedDecl(record)) { + for (auto *ctor : record->ctors()) { + if (!IsConvertibleMoveConstructor(ctor)) { + continue; + } + Buffer buf(*this); + Convert(MakeConstructExpr(ctx_, arg->getType(), ctor, arg)); + return std::move(buf).str(); + } + if (TypeIsCopyable(arg->getType())) { + return ConvertRValue(arg); + } + return ConvertFreshRValue(arg); + } return std::format("std::mem::take(&mut {})", ConvertLValue(arg)); } diff --git a/cpp2rust/converter/converter_lib.cpp b/cpp2rust/converter/converter_lib.cpp index d531eedf..ab18f112 100644 --- a/cpp2rust/converter/converter_lib.cpp +++ b/cpp2rust/converter/converter_lib.cpp @@ -994,6 +994,20 @@ clang::Expr *ToAddrOf(clang::ASTContext &ctx, clang::Expr *expr) { {}); } +clang::CXXConstructExpr *MakeConstructExpr(clang::ASTContext &ctx, + clang::QualType type, + clang::CXXConstructorDecl *ctor, + llvm::ArrayRef args) { + return clang::CXXConstructExpr::Create( + ctx, type, clang::SourceLocation(), ctor, + /*Elidable=*/false, args, + /*HadMultipleCandidates=*/false, + /*ListInitialization=*/false, + /*StdInitListInitialization=*/false, + /*ZeroInitialization=*/false, clang::CXXConstructionKind::Complete, + clang::SourceRange()); +} + std::vector GetNestedStructs(const clang::CXXRecordDecl *decl) { std::vector nested_record_decls; diff --git a/cpp2rust/converter/converter_lib.h b/cpp2rust/converter/converter_lib.h index d503cd92..5c44ad1f 100644 --- a/cpp2rust/converter/converter_lib.h +++ b/cpp2rust/converter/converter_lib.h @@ -171,6 +171,11 @@ bool RecordNeedsDestruction(const clang::CXXRecordDecl *decl); clang::Expr *ToAddrOf(clang::ASTContext &ctx, clang::Expr *expr); +clang::CXXConstructExpr *MakeConstructExpr(clang::ASTContext &ctx, + clang::QualType type, + clang::CXXConstructorDecl *ctor, + llvm::ArrayRef args); + std::vector GetNestedStructs(const clang::CXXRecordDecl *decl); diff --git a/tests/unit/out/refcount/copy_move_defaulted.rs b/tests/unit/out/refcount/copy_move_defaulted.rs index 9341c27b..ad7f72b7 100644 --- a/tests/unit/out/refcount/copy_move_defaulted.rs +++ b/tests/unit/out/refcount/copy_move_defaulted.rs @@ -480,7 +480,7 @@ fn main_0() -> i32 { && ((*(*q.borrow()).data.borrow()).is_empty()) ); let bufs: Value> = Rc::new(RefCell::new(Vec::new())); - (*bufs.borrow_mut()).push(std::mem::take(&mut (*r.borrow_mut()))); + (*bufs.borrow_mut()).push(Buffer::Buffer_pmutBuffer({ r.as_pointer() })); { let __arg = Buffer::Buffer_pmutBuffer({ (bufs.as_pointer() as Ptr).offset(0_usize) }); diff --git a/tests/unit/out/refcount/move_ctor.rs b/tests/unit/out/refcount/move_ctor.rs index 7a8cbadb..564326b3 100644 --- a/tests/unit/out/refcount/move_ctor.rs +++ b/tests/unit/out/refcount/move_ctor.rs @@ -123,7 +123,7 @@ fn main_0() -> i32 { let vec_: Value> = Rc::new(RefCell::new(Vec::new())); (*vec_.borrow_mut()).push(MoveOnly::MoveOnly({ 7 })); let f: Value = Rc::new(RefCell::new(MoveOnly::MoveOnly({ 8 }))); - (*vec_.borrow_mut()).push(std::mem::take(&mut (*f.borrow_mut()))); + (*vec_.borrow_mut()).push(MoveOnly::MoveOnly_pmutMoveOnly({ f.as_pointer() })); assert!( ((*(*(vec_.as_pointer() as Ptr) .offset(0_usize) diff --git a/tests/unit/out/refcount/push_emplace_back.rs b/tests/unit/out/refcount/push_emplace_back.rs index 024c032b..fac947e2 100644 --- a/tests/unit/out/refcount/push_emplace_back.rs +++ b/tests/unit/out/refcount/push_emplace_back.rs @@ -145,9 +145,7 @@ pub fn shrink_through_ptr_2(comps: Ptr>) { pub fn nested_push_move_3(bw: Ptr) { let bw: Value> = Rc::new(RefCell::new(bw)); (*(*(*bw.borrow()).upgrade().deref()).output.borrow()).with_mut(|__v: &mut Vec| { - __v.push(std::mem::take( - &mut (*(*(*bw.borrow()).upgrade().deref()).chunk.borrow_mut()), - )) + __v.push((*(*(*bw.borrow()).upgrade().deref()).chunk.borrow()).clone()) }); } pub fn emplace_local_from_field_4(jpg: Ptr, cond: bool) { diff --git a/tests/unit/out/unsafe/copy_move_defaulted.rs b/tests/unit/out/unsafe/copy_move_defaulted.rs index bb6147c8..cf883c3e 100644 --- a/tests/unit/out/unsafe/copy_move_defaulted.rs +++ b/tests/unit/out/unsafe/copy_move_defaulted.rs @@ -270,7 +270,7 @@ unsafe fn main_0() -> i32 { && (q.data.is_empty()) ); let mut bufs: Vec = Vec::new(); - bufs.push(std::mem::take(&mut r)); + bufs.push(Buffer::Buffer_pmutBuffer({ &mut r })); { let __arg = Buffer::Buffer_pmutBuffer({ &mut bufs[(0_usize)] }); bufs.push(__arg) diff --git a/tests/unit/out/unsafe/move_ctor.rs b/tests/unit/out/unsafe/move_ctor.rs index a7614be6..e6e83f28 100644 --- a/tests/unit/out/unsafe/move_ctor.rs +++ b/tests/unit/out/unsafe/move_ctor.rs @@ -81,7 +81,7 @@ unsafe fn main_0() -> i32 { let mut vec_: Vec = Vec::new(); vec_.push(MoveOnly::MoveOnly({ 7 })); let mut f: MoveOnly = MoveOnly::MoveOnly({ 8 }); - vec_.push(std::mem::take(&mut f)); + vec_.push(MoveOnly::MoveOnly_pmutMoveOnly({ &mut f })); assert!(((vec_[(0_usize)].v) == (7)) && ((vec_[(1_usize)].v) == (8))); assert!(((f.v) == (0))); let mut m: ConstMove = ConstMove::ConstMove(); diff --git a/tests/unit/out/unsafe/push_emplace_back.rs b/tests/unit/out/unsafe/push_emplace_back.rs index 4659d462..14ef3b9c 100644 --- a/tests/unit/out/unsafe/push_emplace_back.rs +++ b/tests/unit/out/unsafe/push_emplace_back.rs @@ -48,7 +48,7 @@ pub unsafe fn shrink_through_ptr_2(mut comps: *mut Vec) { (*comps).shrink_to_fit(); } pub unsafe fn nested_push_move_3(mut bw: *mut Writer) { - (*(*bw).output).push(std::mem::take(&mut (*bw).chunk)); + (*(*bw).output).push((*bw).chunk); } pub unsafe fn emplace_local_from_field_4(mut jpg: *mut JPEGData, mut cond: bool) { let mut head: [u8; 3] = [1_u8, 2_u8, 3_u8]; From 128d553336dfe528613d8d438f8eb0d5f5e75dca Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Mon, 14 Sep 2026 20:43:52 +0100 Subject: [PATCH 10/16] Test move of vector --- rules/vector/src.cpp | 6 +++ rules/vector/tgt_refcount.rs | 4 ++ rules/vector/tgt_unsafe.rs | 4 ++ tests/unit/copy_move_defaulted.cpp | 4 +- .../unit/out/refcount/copy_move_defaulted.rs | 43 ++++++++++++++++--- tests/unit/out/unsafe/copy_move_defaulted.rs | 10 +++++ 6 files changed, 65 insertions(+), 6 deletions(-) diff --git a/rules/vector/src.cpp b/rules/vector/src.cpp index 73cb285f..43e3385c 100644 --- a/rules/vector/src.cpp +++ b/rules/vector/src.cpp @@ -545,3 +545,9 @@ template > std::vector f110(const std::vector &o) { return std::vector(o); } + +template +std::vector> &f111(std::vector> &dst, + std::vector> &&src) { + return dst.operator=(std::move(src)); +} diff --git a/rules/vector/tgt_refcount.rs b/rules/vector/tgt_refcount.rs index f2c9a615..a44c34f3 100644 --- a/rules/vector/tgt_refcount.rs +++ b/rules/vector/tgt_refcount.rs @@ -359,3 +359,7 @@ fn f104(a0: Ptr) -> Ptr { fn f105(a0: Ptr>, a1: Vec) { a0.write(a1.clone()) } + +fn f111(a0: Ptr>>>, a1: &mut Vec>>) { + a0.write(std::mem::take(&mut *a1)) +} diff --git a/rules/vector/tgt_unsafe.rs b/rules/vector/tgt_unsafe.rs index 54228339..94fdf346 100644 --- a/rules/vector/tgt_unsafe.rs +++ b/rules/vector/tgt_unsafe.rs @@ -489,3 +489,7 @@ unsafe fn f109(a0: Vec) -> Vec { unsafe fn f110(a0: Vec) -> Vec { a0.clone() } + +unsafe fn f111(a0: &mut Vec>, a1: &mut Vec>) { + *a0 = std::mem::take(&mut *a1) +} diff --git a/tests/unit/copy_move_defaulted.cpp b/tests/unit/copy_move_defaulted.cpp index f95e9a26..5f5009ba 100644 --- a/tests/unit/copy_move_defaulted.cpp +++ b/tests/unit/copy_move_defaulted.cpp @@ -51,9 +51,10 @@ struct UserCopyDefaultMove { struct Buffer { std::vector data; + std::vector> rows; int n; int arr[2]; - Buffer(int n) : data(n, n), n(n), arr{n, n + 1} {} + Buffer(int n) : data(n, n), n(n), arr{n, n + 1} { rows.push_back(data); } Buffer(const Buffer &) = delete; Buffer(Buffer &&) = default; Buffer &operator=(const Buffer &) = delete; @@ -118,6 +119,7 @@ int main() { Buffer r(1); r = std::move(q); assert(r.n == 3 && r.data.size() == 3 && r.arr[1] == 4 && q.data.empty()); + assert(r.rows.size() == 1 && r.rows[0].size() == 3 && q.rows.empty()); std::vector bufs; bufs.push_back(std::move(r)); bufs.emplace_back(std::move(bufs[0])); diff --git a/tests/unit/out/refcount/copy_move_defaulted.rs b/tests/unit/out/refcount/copy_move_defaulted.rs index ad7f72b7..bc93b118 100644 --- a/tests/unit/out/refcount/copy_move_defaulted.rs +++ b/tests/unit/out/refcount/copy_move_defaulted.rs @@ -235,6 +235,7 @@ impl ByteRepr for UserCopyDefaultMove { #[derive()] pub struct Buffer { pub data: Value>, + pub rows: Value>>>, pub n: Value, pub arr: Value>, } @@ -246,10 +247,18 @@ impl Buffer { (*n.borrow()); ((*n.borrow()) as usize) as usize ])), + rows: Rc::new(RefCell::new(Vec::new())), n: Rc::new(RefCell::new((*n.borrow()))), arr: Rc::new(RefCell::new(Box::new([(*n.borrow()), ((*n.borrow()) + 1)]))), })); let this: Ptr = __this.as_pointer(); + ((*this.upgrade().deref()).rows.as_pointer() as Ptr>>>).with_mut( + |__v: &mut Vec>>| { + __v.push(Rc::new(RefCell::new( + (*(*this.upgrade().deref()).data.borrow()).clone(), + ))) + }, + ); Rc::try_unwrap(__this).ok().unwrap().into_inner() } pub fn Buffer_pmutBuffer(_a0: Ptr) -> Self { @@ -257,6 +266,9 @@ impl Buffer { data: Rc::new(RefCell::new(std::mem::take( &mut (*(*_a0.upgrade().deref()).data.borrow_mut()), ))), + rows: Rc::new(RefCell::new(std::mem::take( + &mut (*(*_a0.upgrade().deref()).rows.borrow_mut()), + ))), n: Rc::new(RefCell::new((*(*_a0.upgrade().deref()).n.borrow()))), arr: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 2, _>( |__i: usize| (*(*_a0.upgrade().deref()).arr.borrow())[(__i) as usize], @@ -270,6 +282,7 @@ impl Default for Buffer { fn default() -> Self { Buffer { data: Rc::new(RefCell::new(Default::default())), + rows: Rc::new(RefCell::new(Vec::new())), n: >::default(), arr: Rc::new(RefCell::new( (0..2).map(|_| ::default()).collect::>(), @@ -279,18 +292,22 @@ impl Default for Buffer { } impl ByteRepr for Buffer { fn byte_size() -> usize { - 40 + 64 } fn to_bytes(&self, buf: &mut [u8]) { (*self.data.borrow()).to_bytes(&mut buf[0..24]); - (*self.n.borrow()).to_bytes(&mut buf[24..28]); - (*self.arr.borrow()).to_bytes(&mut buf[28..36]); + (*self.rows.borrow()).to_bytes(&mut buf[24..48]); + (*self.n.borrow()).to_bytes(&mut buf[48..52]); + (*self.arr.borrow()).to_bytes(&mut buf[52..60]); } fn from_bytes(buf: &[u8]) -> Self { Self { data: Rc::new(RefCell::new(>::from_bytes(&buf[0..24]))), - n: Rc::new(RefCell::new(::from_bytes(&buf[24..28]))), - arr: Rc::new(RefCell::new(>::from_bytes(&buf[28..36]))), + rows: Rc::new(RefCell::new(>>>::from_bytes( + &buf[24..48], + ))), + n: Rc::new(RefCell::new(::from_bytes(&buf[48..52]))), + arr: Rc::new(RefCell::new(>::from_bytes(&buf[52..60]))), } } } @@ -479,6 +496,19 @@ fn main_0() -> i32 { && ((*(*r.borrow()).arr.borrow())[(1) as usize] == 4)) && ((*(*q.borrow()).data.borrow()).is_empty()) ); + assert!( + (((*(*r.borrow()).rows.borrow()).len() == 1_usize) + && ((*(((*r.borrow()).rows.as_pointer() as Ptr>>) + .offset(0_usize) + .upgrade() + .deref() + .as_pointer() as Ptr>) + .upgrade() + .deref()) + .len() + == 3_usize)) + && ((*(*q.borrow()).rows.borrow()).is_empty()) + ); let bufs: Value> = Rc::new(RefCell::new(Vec::new())); (*bufs.borrow_mut()).push(Buffer::Buffer_pmutBuffer({ r.as_pointer() })); { @@ -521,6 +551,9 @@ impl BufferImpl for Ptr { ((*(*self).upgrade().deref()).data.as_pointer() as Ptr>).write(std::mem::take( &mut (*(*_a0.upgrade().deref()).data.borrow_mut()), )); + ((*(*self).upgrade().deref()).rows.as_pointer() as Ptr>>>).write( + std::mem::take(&mut (*(*_a0.upgrade().deref()).rows.borrow_mut())), + ); let __rhs = (*(*_a0.upgrade().deref()).n.borrow()); (*(*(*self).upgrade().deref()).n.borrow_mut()) = __rhs; { diff --git a/tests/unit/out/unsafe/copy_move_defaulted.rs b/tests/unit/out/unsafe/copy_move_defaulted.rs index cf883c3e..1a76890c 100644 --- a/tests/unit/out/unsafe/copy_move_defaulted.rs +++ b/tests/unit/out/unsafe/copy_move_defaulted.rs @@ -130,6 +130,7 @@ impl Clone for UserCopyDefaultMove { #[derive()] pub struct Buffer { pub data: Vec, + pub rows: Vec>, pub n: i32, pub arr: [i32; 2], } @@ -137,14 +138,17 @@ impl Buffer { pub unsafe fn Buffer(mut n: i32) -> Self { let mut this = Self { data: vec![n; (n as usize) as usize], + rows: Vec::new(), n: n, arr: [n, ((n) + (1))], }; + this.rows.push(this.data.clone()); this } pub unsafe fn Buffer_pmutBuffer(_a0: *mut Buffer) -> Self { let mut this = Self { data: std::mem::take(&mut (*_a0).data), + rows: std::mem::take(&mut (*_a0).rows), n: (*_a0).n, arr: std::array::from_fn::<_, 2, _>(|__i: usize| (*_a0).arr[(__i)]), }; @@ -152,6 +156,7 @@ impl Buffer { } pub unsafe fn operator_assign_pmutBuffer(&mut self, _a0: *mut Buffer) -> *mut Buffer { self.data = std::mem::take(&mut (*_a0).data); + self.rows = std::mem::take(&mut (*_a0).rows); self.n = (*_a0).n; { if 8_usize != 0 { @@ -171,6 +176,7 @@ impl Default for Buffer { fn default() -> Self { Buffer { data: Default::default(), + rows: Vec::new(), n: 0_i32, arr: [0_i32; 2], } @@ -269,6 +275,10 @@ unsafe fn main_0() -> i32 { ((((r.n) == (3)) && ((r.data.len()) == (3_usize))) && ((r.arr[(1) as usize]) == (4))) && (q.data.is_empty()) ); + assert!( + (((r.rows.len()) == (1_usize)) && ((r.rows[(0_usize)].len()) == (3_usize))) + && (q.rows.is_empty()) + ); let mut bufs: Vec = Vec::new(); bufs.push(Buffer::Buffer_pmutBuffer({ &mut r })); { From d700acea29cd9758061bdb2592467c4d2b9cdfef Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Mon, 14 Sep 2026 21:07:29 +0100 Subject: [PATCH 11/16] Update tests --- tests/unit/copy_move_defaulted.cpp | 38 ++++ .../unit/out/refcount/copy_move_defaulted.rs | 208 ++++++++++++++++++ tests/unit/out/unsafe/copy_move_defaulted.rs | 154 +++++++++++++ 3 files changed, 400 insertions(+) diff --git a/tests/unit/copy_move_defaulted.cpp b/tests/unit/copy_move_defaulted.cpp index 5f5009ba..45e0acac 100644 --- a/tests/unit/copy_move_defaulted.cpp +++ b/tests/unit/copy_move_defaulted.cpp @@ -1,4 +1,5 @@ #include +#include #include #include @@ -61,6 +62,20 @@ struct Buffer { Buffer &operator=(Buffer &&) = default; }; +struct Owner { + std::vector data; + int n; + int arr[2]; + std::unique_ptr p; +}; + +struct Holder { + Inner inner; + Explicit e; + std::unique_ptr p; + Holder(int v) : inner{v}, e(v) {} +}; + static bool same(const Explicit &a, const Explicit &b) { return a.v == b.v && a.inner.x == b.inner.x && a.arr[0] == b.arr[0] && a.arr[1] == b.arr[1]; @@ -124,5 +139,28 @@ int main() { bufs.push_back(std::move(r)); bufs.emplace_back(std::move(bufs[0])); assert(bufs[1].n == 3 && bufs[1].data.size() == 3 && bufs[0].data.empty()); + + Owner o1; + o1.data.push_back(5); + o1.n = 5; + o1.arr[0] = 5; + o1.arr[1] = 6; + o1.p.reset(new int(7)); + Owner o2 = std::move(o1); + assert(o2.n == 5 && o2.data.size() == 1 && o2.arr[1] == 6 && *o2.p == 7); + assert(o1.data.empty() && o1.p.get() == nullptr); + Owner o3; + o3 = std::move(o2); + assert(o3.n == 5 && o3.data[0] == 5 && o3.arr[0] == 5 && *o3.p == 7); + assert(o2.data.empty() && o2.p.get() == nullptr); + + Holder h1(4); + h1.p.reset(new int(9)); + Holder h2 = std::move(h1); + assert(h2.inner.x == 4 && h2.e.v == 4 && *h2.p == 9 && h1.p.get() == nullptr); + Holder h3(1); + h3 = std::move(h2); + assert(h3.inner.x == 4 && h3.e.arr[1] == 5 && *h3.p == 9 && + h2.p.get() == nullptr); return 0; } diff --git a/tests/unit/out/refcount/copy_move_defaulted.rs b/tests/unit/out/refcount/copy_move_defaulted.rs index bc93b118..c51b1362 100644 --- a/tests/unit/out/refcount/copy_move_defaulted.rs +++ b/tests/unit/out/refcount/copy_move_defaulted.rs @@ -311,6 +311,112 @@ impl ByteRepr for Buffer { } } } +#[derive()] +pub struct Owner { + pub data: Value>, + pub n: Value, + pub arr: Value>, + pub p: Value>>, +} +impl Owner { + pub fn Owner_pmutOwner(_a0: Ptr) -> Self { + let __this: Value = Rc::new(RefCell::new(Self { + data: Rc::new(RefCell::new(std::mem::take( + &mut (*(*_a0.upgrade().deref()).data.borrow_mut()), + ))), + n: Rc::new(RefCell::new((*(*_a0.upgrade().deref()).n.borrow()))), + arr: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 2, _>( + |__i: usize| (*(*_a0.upgrade().deref()).arr.borrow())[(__i) as usize], + )))), + p: Rc::new(RefCell::new( + (*(*_a0.upgrade().deref()).p.borrow_mut()).take(), + )), + })); + let this: Ptr = __this.as_pointer(); + Rc::try_unwrap(__this).ok().unwrap().into_inner() + } +} +impl Default for Owner { + fn default() -> Self { + Owner { + data: Rc::new(RefCell::new(Default::default())), + n: >::default(), + arr: Rc::new(RefCell::new( + (0..2).map(|_| ::default()).collect::>(), + )), + p: Rc::new(RefCell::new(None)), + } + } +} +impl ByteRepr for Owner { + fn byte_size() -> usize { + 48 + } + fn to_bytes(&self, buf: &mut [u8]) { + (*self.data.borrow()).to_bytes(&mut buf[0..24]); + (*self.n.borrow()).to_bytes(&mut buf[24..28]); + (*self.arr.borrow()).to_bytes(&mut buf[28..36]); + (*self.p.borrow()).to_bytes(&mut buf[40..48]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + data: Rc::new(RefCell::new(>::from_bytes(&buf[0..24]))), + n: Rc::new(RefCell::new(::from_bytes(&buf[24..28]))), + arr: Rc::new(RefCell::new(>::from_bytes(&buf[28..36]))), + p: Rc::new(RefCell::new(>>::from_bytes(&buf[40..48]))), + } + } +} +#[derive(Default)] +pub struct Holder { + pub inner: Value, + pub e: Value, + pub p: Value>>, +} +impl Holder { + pub fn Holder(v: i32) -> Self { + let v: Value = Rc::new(RefCell::new(v)); + let __this: Value = Rc::new(RefCell::new(Self { + inner: Rc::new(RefCell::new(Inner { + x: Rc::new(RefCell::new((*v.borrow()))), + })), + e: Rc::new(RefCell::new(Explicit::Explicit({ (*v.borrow()) }))), + p: Rc::new(RefCell::new(None)), + })); + let this: Ptr = __this.as_pointer(); + Rc::try_unwrap(__this).ok().unwrap().into_inner() + } + pub fn Holder_pmutHolder(_a0: Ptr) -> Self { + let __this: Value = Rc::new(RefCell::new(Self { + inner: Rc::new(RefCell::new( + (*(*_a0.upgrade().deref()).inner.borrow()).clone(), + )), + e: Rc::new(RefCell::new((*(*_a0.upgrade().deref()).e.borrow()).clone())), + p: Rc::new(RefCell::new( + (*(*_a0.upgrade().deref()).p.borrow_mut()).take(), + )), + })); + let this: Ptr = __this.as_pointer(); + Rc::try_unwrap(__this).ok().unwrap().into_inner() + } +} +impl ByteRepr for Holder { + fn byte_size() -> usize { + 32 + } + fn to_bytes(&self, buf: &mut [u8]) { + (*self.inner.borrow()).to_bytes(&mut buf[0..4]); + (*self.e.borrow()).to_bytes(&mut buf[4..20]); + (*self.p.borrow()).to_bytes(&mut buf[24..32]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + inner: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + e: Rc::new(RefCell::new(::from_bytes(&buf[4..20]))), + p: Rc::new(RefCell::new(>>::from_bytes(&buf[24..32]))), + } + } +} pub fn same_0(a: Ptr, b: Ptr) -> bool { return ((({ let _lhs = (*(*a.upgrade().deref()).v.borrow()); @@ -541,6 +647,64 @@ fn main_0() -> i32 { .borrow()) .is_empty()) ); + let o1: Value = Rc::new(RefCell::new(::default())); + (*(*o1.borrow()).data.borrow_mut()).push(5); + (*(*o1.borrow()).n.borrow_mut()) = 5; + (*(*o1.borrow()).arr.borrow_mut())[(0) as usize] = 5; + (*(*o1.borrow()).arr.borrow_mut())[(1) as usize] = 6; + { + let _p: Ptr<_> = Ptr::alloc(7); + (*(*o1.borrow()).p.borrow_mut()) = _p.to_owned_opt() + }; + let o2: Value = Rc::new(RefCell::new(Owner::Owner_pmutOwner({ o1.as_pointer() }))); + assert!( + ((((*(*o2.borrow()).n.borrow()) == 5) + && ((*(*o2.borrow()).data.borrow()).len() == 1_usize)) + && ((*(*o2.borrow()).arr.borrow())[(1) as usize] == 6)) + && ((*(*(*o2.borrow()).p.borrow()).as_ref().unwrap().borrow()) == 7) + ); + assert!( + ((*(*o1.borrow()).data.borrow()).is_empty()) + && (((*(*o1.borrow()).p.borrow()).as_pointer()).is_null()) + ); + let o3: Value = Rc::new(RefCell::new(::default())); + ({ OwnerImpl::operator_assign_pmutOwner(&o3.as_pointer(), o2.as_pointer()) }); + assert!( + ((((*(*o3.borrow()).n.borrow()) == 5) + && ((((*o3.borrow()).data.as_pointer() as Ptr) + .offset(0_usize) + .read()) + == 5)) + && ((*(*o3.borrow()).arr.borrow())[(0) as usize] == 5)) + && ((*(*(*o3.borrow()).p.borrow()).as_ref().unwrap().borrow()) == 7) + ); + assert!( + ((*(*o2.borrow()).data.borrow()).is_empty()) + && (((*(*o2.borrow()).p.borrow()).as_pointer()).is_null()) + ); + let h1: Value = Rc::new(RefCell::new(Holder::Holder({ 4 }))); + let _dtor_h1 = ScopedDestructor::new(&h1, |__p| __p.destructor()); + { + let _p: Ptr<_> = Ptr::alloc(9); + (*(*h1.borrow()).p.borrow_mut()) = _p.to_owned_opt() + }; + let h2: Value = Rc::new(RefCell::new(Holder::Holder_pmutHolder({ h1.as_pointer() }))); + let _dtor_h2 = ScopedDestructor::new(&h2, |__p| __p.destructor()); + assert!( + ((((*(*(*h2.borrow()).inner.borrow()).x.borrow()) == 4) + && ((*(*(*h2.borrow()).e.borrow()).v.borrow()) == 4)) + && ((*(*(*h2.borrow()).p.borrow()).as_ref().unwrap().borrow()) == 9)) + && (((*(*h1.borrow()).p.borrow()).as_pointer()).is_null()) + ); + let h3: Value = Rc::new(RefCell::new(Holder::Holder({ 1 }))); + let _dtor_h3 = ScopedDestructor::new(&h3, |__p| __p.destructor()); + ({ HolderImpl::operator_assign_pmutHolder(&h3.as_pointer(), h2.as_pointer()) }); + assert!( + ((((*(*(*h3.borrow()).inner.borrow()).x.borrow()) == 4) + && ((*(*(*h3.borrow()).e.borrow()).arr.borrow())[(1) as usize] == 5)) + && ((*(*(*h3.borrow()).p.borrow()).as_ref().unwrap().borrow()) == 9)) + && (((*(*h2.borrow()).p.borrow()).as_pointer()).is_null()) + ); return 0; } pub trait BufferImpl { @@ -593,6 +757,50 @@ pub trait ExplicitImpl { impl ExplicitImpl for Ptr { fn destructor(&self) {} } +pub trait HolderImpl { + fn operator_assign_pmutHolder(&self, _a0: Ptr) -> Ptr; + fn destructor(&self); +} +impl HolderImpl for Ptr { + fn operator_assign_pmutHolder(&self, _a0: Ptr) -> Ptr { + let __rhs = (*(*_a0.upgrade().deref()).inner.borrow()).clone(); + (*(*(*self).upgrade().deref()).inner.borrow_mut()) = __rhs; + let __rhs = (*(*_a0.upgrade().deref()).e.borrow()).clone(); + (*(*(*self).upgrade().deref()).e.borrow_mut()) = __rhs; + ((*(*self).upgrade().deref()).p.as_pointer() as Ptr>>) + .write((*(*_a0.upgrade().deref()).p.borrow_mut()).take()); + return (*self).clone(); + } + fn destructor(&self) { + (*self.upgrade().deref()).e.as_pointer().destructor(); + } +} +pub trait OwnerImpl { + fn operator_assign_pmutOwner(&self, _a0: Ptr) -> Ptr; +} +impl OwnerImpl for Ptr { + fn operator_assign_pmutOwner(&self, _a0: Ptr) -> Ptr { + ((*(*self).upgrade().deref()).data.as_pointer() as Ptr>).write(std::mem::take( + &mut (*(*_a0.upgrade().deref()).data.borrow_mut()), + )); + let __rhs = (*(*_a0.upgrade().deref()).n.borrow()); + (*(*(*self).upgrade().deref()).n.borrow_mut()) = __rhs; + { + (((*(*self).upgrade().deref()).arr.as_pointer()) as Ptr) + .to_any() + .memcpy( + &(((*_a0.upgrade().deref()).arr.as_pointer()) as Ptr).to_any(), + 8_usize as usize, + ); + (((*(*self).upgrade().deref()).arr.as_pointer()) as Ptr) + .to_any() + .clone() + }; + ((*(*self).upgrade().deref()).p.as_pointer() as Ptr>>) + .write((*(*_a0.upgrade().deref()).p.borrow_mut()).take()); + return (*self).clone(); + } +} pub trait UserCopyDefaultMoveImpl { fn operator_assign_pconstUserCopyDefaultMove( &self, diff --git a/tests/unit/out/unsafe/copy_move_defaulted.rs b/tests/unit/out/unsafe/copy_move_defaulted.rs index 1a76890c..8e738688 100644 --- a/tests/unit/out/unsafe/copy_move_defaulted.rs +++ b/tests/unit/out/unsafe/copy_move_defaulted.rs @@ -182,6 +182,88 @@ impl Default for Buffer { } } } +#[repr(C)] +#[derive()] +pub struct Owner { + pub data: Vec, + pub n: i32, + pub arr: [i32; 2], + pub p: Option>, +} +impl Owner { + pub unsafe fn Owner_pmutOwner(_a0: *mut Owner) -> Self { + let mut this = Self { + data: std::mem::take(&mut (*_a0).data), + n: (*_a0).n, + arr: std::array::from_fn::<_, 2, _>(|__i: usize| (*_a0).arr[(__i)]), + p: (*_a0).p.take(), + }; + this + } + pub unsafe fn operator_assign_pmutOwner(&mut self, _a0: *mut Owner) -> *mut Owner { + self.data = std::mem::take(&mut (*_a0).data); + self.n = (*_a0).n; + { + if 8_usize != 0 { + ::std::ptr::copy_nonoverlapping( + ((&mut (*_a0).arr as *mut [i32; 2]) as *const [i32; 2] + as *const ::libc::c_void), + ((&mut self.arr as *mut [i32; 2]) as *mut [i32; 2] as *mut ::libc::c_void), + 8_usize as usize, + ) + } + ((&mut self.arr as *mut [i32; 2]) as *mut [i32; 2] as *mut ::libc::c_void) + }; + self.p = (*_a0).p.take(); + return &mut (*(self as *mut Owner)); + } +} +impl Default for Owner { + fn default() -> Self { + Owner { + data: Default::default(), + n: 0_i32, + arr: [0_i32; 2], + p: None, + } + } +} +#[repr(C)] +#[derive(Default)] +pub struct Holder { + pub inner: Inner, + pub e: Explicit, + pub p: Option>, +} +impl Holder { + pub unsafe fn Holder(mut v: i32) -> Self { + let mut this = Self { + inner: Inner { x: v }, + e: Explicit::Explicit({ v }), + p: None, + }; + this + } + pub unsafe fn Holder_pmutHolder(_a0: *mut Holder) -> Self { + let mut this = Self { + inner: (*_a0).inner, + e: (*_a0).e.clone(), + p: (*_a0).p.take(), + }; + this + } + pub unsafe fn operator_assign_pmutHolder(&mut self, _a0: *mut Holder) -> *mut Holder { + self.inner = (*_a0).inner; + self.e = ((*_a0).e).clone(); + self.p = (*_a0).p.take(); + return &mut (*(self as *mut Holder)); + } +} +impl Holder { + pub unsafe fn destructor(&mut self) { + Explicit::destructor(&mut self.e); + } +} pub unsafe fn same_0(a: *const Explicit, b: *const Explicit) -> bool { return (((((*a).v) == ((*b).v)) && (((*a).inner.x) == ((*b).inner.x))) && (((*a).arr[(0) as usize]) == ((*b).arr[(0) as usize]))) @@ -289,5 +371,77 @@ unsafe fn main_0() -> i32 { (((bufs[(1_usize)].n) == (3)) && ((bufs[(1_usize)].data.len()) == (3_usize))) && (bufs[(0_usize)].data.is_empty()) ); + let mut o1: Owner = ::default(); + o1.data.push(5); + o1.n = 5; + o1.arr[(0) as usize] = 5; + o1.arr[(1) as usize] = 6; + { + let _a0: *mut i32 = (Box::leak(Box::new(7)) as *mut i32); + o1.p = if _a0.is_null() { + None + } else { + Some(Box::from_raw(_a0)) + } + }; + let mut o2: Owner = Owner::Owner_pmutOwner({ &mut o1 }); + assert!( + ((((o2.n) == (5)) && ((o2.data.len()) == (1_usize))) && ((o2.arr[(1) as usize]) == (6))) + && ((*o2.p.as_deref_mut().unwrap()) == (7)) + ); + assert!( + (o1.data.is_empty()) + && ((o1 + .p + .as_deref_mut() + .map_or(::std::ptr::null_mut(), |v| v as *mut i32)) + .is_null()) + ); + let mut o3: Owner = ::default(); + (unsafe { Owner::operator_assign_pmutOwner(&mut o3, &mut o2) }); + assert!( + ((((o3.n) == (5)) && ((o3.data[(0_usize)]) == (5))) && ((o3.arr[(0) as usize]) == (5))) + && ((*o3.p.as_deref_mut().unwrap()) == (7)) + ); + assert!( + (o2.data.is_empty()) + && ((o2 + .p + .as_deref_mut() + .map_or(::std::ptr::null_mut(), |v| v as *mut i32)) + .is_null()) + ); + let mut h1: Holder = Holder::Holder({ 4 }); + let _dtor_h1 = ScopedDestructorUnsafe::new(&raw mut h1, Holder::destructor); + { + let _a0: *mut i32 = (Box::leak(Box::new(9)) as *mut i32); + h1.p = if _a0.is_null() { + None + } else { + Some(Box::from_raw(_a0)) + } + }; + let mut h2: Holder = Holder::Holder_pmutHolder({ &mut h1 }); + let _dtor_h2 = ScopedDestructorUnsafe::new(&raw mut h2, Holder::destructor); + assert!( + ((((h2.inner.x) == (4)) && ((h2.e.v) == (4))) && ((*h2.p.as_deref_mut().unwrap()) == (9))) + && ((h1 + .p + .as_deref_mut() + .map_or(::std::ptr::null_mut(), |v| v as *mut i32)) + .is_null()) + ); + let mut h3: Holder = Holder::Holder({ 1 }); + let _dtor_h3 = ScopedDestructorUnsafe::new(&raw mut h3, Holder::destructor); + (unsafe { Holder::operator_assign_pmutHolder(&mut h3, &mut h2) }); + assert!( + ((((h3.inner.x) == (4)) && ((h3.e.arr[(1) as usize]) == (5))) + && ((*h3.p.as_deref_mut().unwrap()) == (9))) + && ((h2 + .p + .as_deref_mut() + .map_or(::std::ptr::null_mut(), |v| v as *mut i32)) + .is_null()) + ); return 0; } From fffe2b9ac8471d824da1f6736bac63f4d9c64931 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Mon, 14 Sep 2026 21:11:44 +0100 Subject: [PATCH 12/16] Translate implicit move/copy assignment as plain assignment --- cpp2rust/converter/converter.cpp | 6 ++++++ cpp2rust/converter/converter_lib.cpp | 16 ++++++++++++++++ cpp2rust/converter/converter_lib.h | 1 + cpp2rust/converter/models/converter_refcount.cpp | 6 ++++++ 4 files changed, 29 insertions(+) diff --git a/cpp2rust/converter/converter.cpp b/cpp2rust/converter/converter.cpp index 5da20fd8..efca7f9a 100644 --- a/cpp2rust/converter/converter.cpp +++ b/cpp2rust/converter/converter.cpp @@ -1751,6 +1751,12 @@ bool Converter::VisitCallExpr(clang::CallExpr *expr) { return false; } + if (IsImplicitAssignmentCall(expr) && !Mapper::Contains(expr->getCallee())) { + auto *call = clang::cast(expr); + ConvertAssignment(call->getImplicitObjectArgument(), call->getArg(0), "="); + return false; + } + if (auto plugin_str = TryPluginConvert(expr)) { StrCat(*plugin_str); return false; diff --git a/cpp2rust/converter/converter_lib.cpp b/cpp2rust/converter/converter_lib.cpp index ab18f112..431cbb76 100644 --- a/cpp2rust/converter/converter_lib.cpp +++ b/cpp2rust/converter/converter_lib.cpp @@ -843,6 +843,22 @@ bool IsSameTypeComparison(const clang::FunctionDecl *fn, is_record(fn->getParamDecl(1)->getType()); } +bool IsImplicitAssignmentCall(const clang::CallExpr *expr) { + const auto *call = clang::dyn_cast(expr); + if (!call) { + return false; + } + const auto *method = call->getMethodDecl(); + if (!method || !(method->isCopyAssignmentOperator() || + method->isMoveAssignmentOperator())) { + return false; + } + if (method->isUserProvided() && IsUserDefinedDecl(method)) { + return false; + } + return !IsConvertibleMoveAssignment(method); +} + bool IsUserOperatorCall(const clang::CXXOperatorCallExpr *expr) { const auto *callee = expr->getDirectCallee(); if (!callee) { diff --git a/cpp2rust/converter/converter_lib.h b/cpp2rust/converter/converter_lib.h index 5c44ad1f..709940a7 100644 --- a/cpp2rust/converter/converter_lib.h +++ b/cpp2rust/converter/converter_lib.h @@ -155,6 +155,7 @@ const char *GetOverloadedOperator(const clang::FunctionDecl *decl); std::string GetFunctionBaseName(const clang::FunctionDecl *decl); +bool IsImplicitAssignmentCall(const clang::CallExpr *expr); bool IsUserOperatorCall(const clang::CXXOperatorCallExpr *expr); bool IsSameTypeComparison(const clang::FunctionDecl *fn, diff --git a/cpp2rust/converter/models/converter_refcount.cpp b/cpp2rust/converter/models/converter_refcount.cpp index 98212436..15d8cebc 100644 --- a/cpp2rust/converter/models/converter_refcount.cpp +++ b/cpp2rust/converter/models/converter_refcount.cpp @@ -1059,6 +1059,12 @@ bool ConverterRefCount::VisitCallExpr(clang::CallExpr *expr) { return false; } + if (IsImplicitAssignmentCall(expr) && !Mapper::Contains(expr->getCallee())) { + auto *call = clang::cast(expr); + ConvertAssignment(call->getImplicitObjectArgument(), call->getArg(0), "="); + return false; + } + if (expr->isCallToStdMove()) { return Converter::VisitCallExpr(expr); } From d674ee00acefccce4c398dd8a6dee0ee2bb9de4a Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Tue, 15 Sep 2026 08:54:14 +0100 Subject: [PATCH 13/16] Update tests --- tests/unit/out/unsafe/array_reference.rs | 2 +- tests/unit/out/unsafe/copy_ctor.rs | 2 +- tests/unit/out/unsafe/huffman.rs | 18 ++++++++++ tests/unit/out/unsafe/kruskal.rs | 33 +++++++++++++++++++ tests/unit/out/unsafe/move_assign.rs | 2 +- tests/unit/out/unsafe/move_this.rs | 6 ++-- .../unsafe/operator_comparison_noncopyable.rs | 6 ++++ tests/unit/out/unsafe/rule_of_three.rs | 4 +-- tests/unit/out/unsafe/unique_ptr.rs | 13 ++++++++ tests/unit/out/unsafe/void_cast.rs | 15 +++++++++ 10 files changed, 93 insertions(+), 8 deletions(-) diff --git a/tests/unit/out/unsafe/array_reference.rs b/tests/unit/out/unsafe/array_reference.rs index 1a39c841..4c0ccd12 100644 --- a/tests/unit/out/unsafe/array_reference.rs +++ b/tests/unit/out/unsafe/array_reference.rs @@ -39,7 +39,7 @@ pub unsafe fn fill_and_sum_5(a: *mut [i32; 3], mut v: i32, out: *mut i32) { let _v: i32 = v; fill_3(_a, _v) }); - (*out) = (unsafe { sum_twice_4(a) }); + (*out) = (unsafe { sum_twice_4(a) }).clone(); } pub unsafe fn pick_6(s: *const [libc::c_char; 5]) -> *const [libc::c_char; 5] { return s; diff --git a/tests/unit/out/unsafe/copy_ctor.rs b/tests/unit/out/unsafe/copy_ctor.rs index 56bd4a35..e098c68f 100644 --- a/tests/unit/out/unsafe/copy_ctor.rs +++ b/tests/unit/out/unsafe/copy_ctor.rs @@ -80,7 +80,7 @@ pub unsafe fn by_value_1(mut c: Counted) -> i32 { } pub unsafe fn make_2(mut v: i32) -> Counted { let mut c: Counted = Counted::Counted({ v }); - return Counted::Counted_pconstCounted({ &mut c }); + return Counted::Counted_pconstCounted({ &c }); } pub fn main() { unsafe { diff --git a/tests/unit/out/unsafe/huffman.rs b/tests/unit/out/unsafe/huffman.rs index 75a246d4..229c9eb3 100644 --- a/tests/unit/out/unsafe/huffman.rs +++ b/tests/unit/out/unsafe/huffman.rs @@ -127,6 +127,24 @@ impl MinHeap { i.prefix_dec(); } } + pub unsafe fn MinHeap_pmutMinHeap(_a0: *mut MinHeap) -> Self { + let mut this = Self { + size: (*_a0).size, + capacity: (*_a0).capacity, + arr: (*_a0).arr.take(), + next: (*_a0).next, + alloc: (*_a0).alloc.take(), + }; + this + } + pub unsafe fn operator_assign_pmutMinHeap(&mut self, _a0: *mut MinHeap) -> *mut MinHeap { + self.size = (*_a0).size; + self.capacity = (*_a0).capacity; + self.arr = (*_a0).arr.take(); + self.next = (*_a0).next; + self.alloc = (*_a0).alloc.take(); + return &mut (*(self as *mut MinHeap)); + } } pub unsafe fn AllocMinHeap_1(mut capacity: i32) -> Option> { let mut minHeap: Option> = Some(Box::new(MinHeap { diff --git a/tests/unit/out/unsafe/kruskal.rs b/tests/unit/out/unsafe/kruskal.rs index cb48b4dc..5fa31aae 100644 --- a/tests/unit/out/unsafe/kruskal.rs +++ b/tests/unit/out/unsafe/kruskal.rs @@ -138,6 +138,23 @@ impl DisjointSet { ((self.rank.as_mut().unwrap()[(xset as usize)]) + (1)); } } + pub unsafe fn DisjointSet_pmutDisjointSet(_a0: *mut DisjointSet) -> Self { + let mut this = Self { + rank: (*_a0).rank.take(), + parent: (*_a0).parent.take(), + n: (*_a0).n, + }; + this + } + pub unsafe fn operator_assign_pmutDisjointSet( + &mut self, + _a0: *mut DisjointSet, + ) -> *mut DisjointSet { + self.rank = (*_a0).rank.take(); + self.parent = (*_a0).parent.take(); + self.n = (*_a0).n; + return &mut (*(self as *mut DisjointSet)); + } } #[repr(C)] #[derive(Default)] @@ -146,6 +163,22 @@ pub struct Graph { pub V: i32, pub E: i32, } +impl Graph { + pub unsafe fn Graph_pmutGraph(_a0: *mut Graph) -> Self { + let mut this = Self { + edges: (*_a0).edges.take(), + V: (*_a0).V, + E: (*_a0).E, + }; + this + } + pub unsafe fn operator_assign_pmutGraph(&mut self, _a0: *mut Graph) -> *mut Graph { + self.edges = (*_a0).edges.take(); + self.V = (*_a0).V; + self.E = (*_a0).E; + return &mut (*(self as *mut Graph)); + } +} pub unsafe fn MSTKruskal_2(graph: *mut Graph) -> f64 { (unsafe { let _arr: *mut Option> = &mut (*graph).edges; diff --git a/tests/unit/out/unsafe/move_assign.rs b/tests/unit/out/unsafe/move_assign.rs index 5c9f54c9..da0855c8 100644 --- a/tests/unit/out/unsafe/move_assign.rs +++ b/tests/unit/out/unsafe/move_assign.rs @@ -83,7 +83,7 @@ unsafe fn main_0() -> i32 { (unsafe { MoveOnly::operator_assign_pmutMoveOnly( &mut c, - (unsafe { MoveOnly::operator_assign_pmutMoveOnly(&mut a, &mut b) }), + &mut (*(unsafe { MoveOnly::operator_assign_pmutMoveOnly(&mut a, &mut b) })), ) }); assert!((((b.v) == (0)) && ((a.v) == (0))) && ((c.v) == (3))); diff --git a/tests/unit/out/unsafe/move_this.rs b/tests/unit/out/unsafe/move_this.rs index 75b912f1..d4d939fd 100644 --- a/tests/unit/out/unsafe/move_this.rs +++ b/tests/unit/out/unsafe/move_this.rs @@ -35,16 +35,16 @@ impl Chain { } pub unsafe fn add_i32_rref(&mut self, mut n: i32) -> *mut Chain { self.v += n; - return (self as *mut Chain); + return &mut (*(self as *mut Chain)); } pub unsafe fn take(&mut self) -> Chain { - return Chain::Chain_pmutChain({ (self as *mut Chain) }); + return Chain::Chain_pmutChain({ &mut (*(self as *mut Chain)) }); } pub unsafe fn copy(&self) -> Chain { return Chain::Chain_pconstChain({ &(*(self as *const Chain)) }); } pub unsafe fn self_(&mut self) -> *mut Chain { - return (self as *mut Chain); + return &mut (*(self as *mut Chain)); } } impl Clone for Chain { diff --git a/tests/unit/out/unsafe/operator_comparison_noncopyable.rs b/tests/unit/out/unsafe/operator_comparison_noncopyable.rs index a4f9676d..1593443c 100644 --- a/tests/unit/out/unsafe/operator_comparison_noncopyable.rs +++ b/tests/unit/out/unsafe/operator_comparison_noncopyable.rs @@ -22,6 +22,12 @@ impl S { let mut this = Self { data_: data }; this } + pub unsafe fn S_pmutS(_a0: *mut S) -> Self { + let mut this = Self { + data_: (*_a0).data_, + }; + this + } } impl std::cmp::Ord for S { fn cmp(&self, other: &Self) -> std::cmp::Ordering { diff --git a/tests/unit/out/unsafe/rule_of_three.rs b/tests/unit/out/unsafe/rule_of_three.rs index f358e026..b08a1eed 100644 --- a/tests/unit/out/unsafe/rule_of_three.rs +++ b/tests/unit/out/unsafe/rule_of_three.rs @@ -107,11 +107,11 @@ unsafe fn main_0() -> i32 { assert!(((copies_1) == (2))); assert!(((unsafe { sum_2(&a,) }) == (6))); assert!(((unsafe { sum_2(&b,) }) == (106))); - let mut d: Buffer = Buffer::Buffer_pconstBuffer({ &mut a }); + let mut d: Buffer = Buffer::Buffer_pconstBuffer({ &a }); let _dtor_d = ScopedDestructorUnsafe::new(&raw mut d, Buffer::destructor); assert!(((alive_0) == (4)) && ((copies_1) == (3))); assert!(((a.size) == (4)) && ((a.data[(3) as usize]) == (3))); - (unsafe { Buffer::operator_assign(&mut d, &mut b) }); + (unsafe { Buffer::operator_assign(&mut d, &b) }); assert!(((copies_1) == (4))); assert!(((b.data[(0) as usize]) == (100)) && ((d.data[(0) as usize]) == (100))); } diff --git a/tests/unit/out/unsafe/unique_ptr.rs b/tests/unit/out/unsafe/unique_ptr.rs index 5b700600..3dc2ecc4 100644 --- a/tests/unit/out/unsafe/unique_ptr.rs +++ b/tests/unit/out/unsafe/unique_ptr.rs @@ -15,6 +15,19 @@ impl SafePointer { pub unsafe fn inc(&mut self) { (*self.ptr.as_deref_mut().unwrap()).prefix_inc(); } + pub unsafe fn SafePointer_pmutSafePointer(_a0: *mut SafePointer) -> Self { + let mut this = Self { + ptr: (*_a0).ptr.take(), + }; + this + } + pub unsafe fn operator_assign_pmutSafePointer( + &mut self, + _a0: *mut SafePointer, + ) -> *mut SafePointer { + self.ptr = (*_a0).ptr.take(); + return &mut (*(self as *mut SafePointer)); + } } #[repr(C)] #[derive(Copy, Clone, Default)] diff --git a/tests/unit/out/unsafe/void_cast.rs b/tests/unit/out/unsafe/void_cast.rs index aea1827d..71f34c2f 100644 --- a/tests/unit/out/unsafe/void_cast.rs +++ b/tests/unit/out/unsafe/void_cast.rs @@ -35,6 +35,21 @@ pub struct Holder { pub struct NonCopyable { pub value: Option>, } +impl NonCopyable { + pub unsafe fn NonCopyable_pmutNonCopyable(_a0: *mut NonCopyable) -> Self { + let mut this = Self { + value: (*_a0).value.take(), + }; + this + } + pub unsafe fn operator_assign_pmutNonCopyable( + &mut self, + _a0: *mut NonCopyable, + ) -> *mut NonCopyable { + self.value = (*_a0).value.take(); + return &mut (*(self as *mut NonCopyable)); + } +} pub unsafe fn unused_noncopyable_param_5(x: *const NonCopyable) { &(*x); } From ac293cd9786d00e48825b8743d8bd26339cf53a3 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Tue, 15 Sep 2026 15:54:52 +0100 Subject: [PATCH 14/16] Update tests --- tests/unit/out/refcount/push_emplace_back.rs | 19 ++++++++----------- tests/unit/out/unsafe/array_reference.rs | 2 +- tests/unit/out/unsafe/huffman.rs | 18 ++++++++++++++++++ .../unsafe/redundant_copy_in_conversion.rs | 2 +- 4 files changed, 28 insertions(+), 13 deletions(-) diff --git a/tests/unit/out/refcount/push_emplace_back.rs b/tests/unit/out/refcount/push_emplace_back.rs index 2fc46195..983b3dc4 100644 --- a/tests/unit/out/refcount/push_emplace_back.rs +++ b/tests/unit/out/refcount/push_emplace_back.rs @@ -141,9 +141,7 @@ pub fn shrink_through_ptr_2(comps: Ptr>) { pub fn nested_push_move_3(bw: Ptr) { let bw: Value> = Rc::new(RefCell::new(bw)); (*(*(*bw.borrow()).upgrade().deref()).output.borrow()).with_mut(|__v: &mut Vec| { - __v.push(std::mem::take( - &mut (*(*(*bw.borrow()).upgrade().deref()).chunk.borrow_mut()), - )) + __v.push((*(*(*bw.borrow()).upgrade().deref()).chunk.borrow()).clone()) }); } pub fn emplace_local_from_field_4(jpg: Ptr, cond: bool) { @@ -174,14 +172,13 @@ pub fn emplace_local_from_field_4(jpg: Ptr, cond: bool) { } pub fn nested_emplace_move_5(bw: Ptr) { let bw: Value> = Rc::new(RefCell::new(bw)); - (*(*(*bw.borrow()).upgrade().deref()).output.borrow()) - .to_strong() - .as_pointer() - .with_mut(|__v: &mut Vec| { - __v.push(std::mem::take( - &mut (*(*(*bw.borrow()).upgrade().deref()).chunk.borrow()).clone(), - )) - }); + { + let __arg = (*(*(*bw.borrow()).upgrade().deref()).chunk.borrow()).clone(); + (*(*(*bw.borrow()).upgrade().deref()).output.borrow()) + .to_strong() + .as_pointer() + .with_mut(|__v: &mut Vec| __v.push(__arg)) + }; } pub fn self_ref_push_6(comps: Ptr>) { let comps: Value>> = Rc::new(RefCell::new(comps)); diff --git a/tests/unit/out/unsafe/array_reference.rs b/tests/unit/out/unsafe/array_reference.rs index 4c0ccd12..1a39c841 100644 --- a/tests/unit/out/unsafe/array_reference.rs +++ b/tests/unit/out/unsafe/array_reference.rs @@ -39,7 +39,7 @@ pub unsafe fn fill_and_sum_5(a: *mut [i32; 3], mut v: i32, out: *mut i32) { let _v: i32 = v; fill_3(_a, _v) }); - (*out) = (unsafe { sum_twice_4(a) }).clone(); + (*out) = (unsafe { sum_twice_4(a) }); } pub unsafe fn pick_6(s: *const [libc::c_char; 5]) -> *const [libc::c_char; 5] { return s; diff --git a/tests/unit/out/unsafe/huffman.rs b/tests/unit/out/unsafe/huffman.rs index ce0a4293..ba9e4a2e 100644 --- a/tests/unit/out/unsafe/huffman.rs +++ b/tests/unit/out/unsafe/huffman.rs @@ -125,6 +125,24 @@ impl MinHeap { i.prefix_dec(); } } + pub unsafe fn MinHeap_pmutMinHeap(_a0: *mut MinHeap) -> Self { + let mut this = Self { + size: (*_a0).size, + capacity: (*_a0).capacity, + arr: (*_a0).arr.take(), + next: (*_a0).next, + alloc: (*_a0).alloc.take(), + }; + this + } + pub unsafe fn operator_assign_pmutMinHeap(&mut self, _a0: *mut MinHeap) -> *mut MinHeap { + self.size = (*_a0).size; + self.capacity = (*_a0).capacity; + self.arr = (*_a0).arr.take(); + self.next = (*_a0).next; + self.alloc = (*_a0).alloc.take(); + return &mut (*(self as *mut MinHeap)); + } } pub unsafe fn AllocMinHeap_1(mut capacity: i32) -> Option> { let mut minHeap: Option> = Some(Box::new(MinHeap { diff --git a/tests/unit/out/unsafe/redundant_copy_in_conversion.rs b/tests/unit/out/unsafe/redundant_copy_in_conversion.rs index 46caad85..f2809b67 100644 --- a/tests/unit/out/unsafe/redundant_copy_in_conversion.rs +++ b/tests/unit/out/unsafe/redundant_copy_in_conversion.rs @@ -24,7 +24,7 @@ unsafe fn main_0() -> i32 { UnsafeMapIterator::find_key(&m as *const BTreeMap>, &0); let mut const_it: UnsafeMapIterator = it0.clone(); let mut r: i32 = if const_it == end.clone() { 0 } else { 1 }; - r += (unsafe { sink_0(it0.clone()) }).clone(); + r += (unsafe { sink_0(it0.clone()) }); r += if end == end { 0 } else { 1 }; assert!(((r) == (2))); return 0; From 526315ef407e00b0b263676558730bc4a3e025cc Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Tue, 15 Sep 2026 15:58:50 +0100 Subject: [PATCH 15/16] Set freshness in VisitArrayInitIndexExpr --- cpp2rust/converter/converter.cpp | 1 + .../out/refcount/defaulted_move_cross_tu.rs | 10 +++---- tests/unit/out/refcount/clone_vs_move.rs | 4 ++- .../unit/out/refcount/copy_move_defaulted.rs | 26 ++++++++----------- 4 files changed, 19 insertions(+), 22 deletions(-) diff --git a/cpp2rust/converter/converter.cpp b/cpp2rust/converter/converter.cpp index 3bee7f7f..de510044 100644 --- a/cpp2rust/converter/converter.cpp +++ b/cpp2rust/converter/converter.cpp @@ -3232,6 +3232,7 @@ bool Converter::VisitOpaqueValueExpr(clang::OpaqueValueExpr *expr) { bool Converter::VisitArrayInitIndexExpr(clang::ArrayInitIndexExpr *expr) { StrCat("__i"); + computed_expr_type_ = ComputedExprType::FreshValue; return false; } diff --git a/tests/multi-file/defaulted_move_cross_tu/out/refcount/defaulted_move_cross_tu.rs b/tests/multi-file/defaulted_move_cross_tu/out/refcount/defaulted_move_cross_tu.rs index 5c9a8aab..9305e384 100644 --- a/tests/multi-file/defaulted_move_cross_tu/out/refcount/defaulted_move_cross_tu.rs +++ b/tests/multi-file/defaulted_move_cross_tu/out/refcount/defaulted_move_cross_tu.rs @@ -26,9 +26,9 @@ impl S { } pub fn S_pmutS(_a0: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new(std::mem::take( - &mut (*(*_a0.upgrade().deref()).v.borrow_mut()), - ))), + v: Rc::new(RefCell::new( + (std::mem::take(&mut (*(*_a0.upgrade().deref()).v.borrow_mut()))).clone(), + )), n: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 2, _>( |__i: usize| (*(*_a0.upgrade().deref()).n.borrow())[(__i) as usize], )))), @@ -105,9 +105,7 @@ impl SImpl for Ptr { &(((*_a0.upgrade().deref()).n.as_pointer()) as Ptr).to_any(), 8_usize as usize, ); - (((*(*self).upgrade().deref()).n.as_pointer()) as Ptr) - .to_any() - .clone() + (((*(*self).upgrade().deref()).n.as_pointer()) as Ptr).to_any() }; return (*self).clone(); } diff --git a/tests/unit/out/refcount/clone_vs_move.rs b/tests/unit/out/refcount/clone_vs_move.rs index c3819e4e..7b51f17b 100644 --- a/tests/unit/out/refcount/clone_vs_move.rs +++ b/tests/unit/out/refcount/clone_vs_move.rs @@ -46,7 +46,9 @@ impl Clone for Foo { x: Rc::new(RefCell::new((*self.x.borrow()))), y: (self.y).clone(), z: Rc::new(RefCell::new((*self.z.borrow()).clone())), - a: Rc::new(RefCell::new((*self.a.borrow()).clone())), + a: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 3, _>( + |__i: usize| (*self.a.borrow())[(__i) as usize], + )))), bar: Rc::new(RefCell::new((*self.bar.borrow()).clone())), })); let this: Ptr = __this.as_pointer(); diff --git a/tests/unit/out/refcount/copy_move_defaulted.rs b/tests/unit/out/refcount/copy_move_defaulted.rs index c51b1362..12ff4978 100644 --- a/tests/unit/out/refcount/copy_move_defaulted.rs +++ b/tests/unit/out/refcount/copy_move_defaulted.rs @@ -263,12 +263,12 @@ impl Buffer { } pub fn Buffer_pmutBuffer(_a0: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - data: Rc::new(RefCell::new(std::mem::take( - &mut (*(*_a0.upgrade().deref()).data.borrow_mut()), - ))), - rows: Rc::new(RefCell::new(std::mem::take( - &mut (*(*_a0.upgrade().deref()).rows.borrow_mut()), - ))), + data: Rc::new(RefCell::new( + (std::mem::take(&mut (*(*_a0.upgrade().deref()).data.borrow_mut()))).clone(), + )), + rows: Rc::new(RefCell::new( + (std::mem::take(&mut (*(*_a0.upgrade().deref()).rows.borrow_mut()))).clone(), + )), n: Rc::new(RefCell::new((*(*_a0.upgrade().deref()).n.borrow()))), arr: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 2, _>( |__i: usize| (*(*_a0.upgrade().deref()).arr.borrow())[(__i) as usize], @@ -321,9 +321,9 @@ pub struct Owner { impl Owner { pub fn Owner_pmutOwner(_a0: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - data: Rc::new(RefCell::new(std::mem::take( - &mut (*(*_a0.upgrade().deref()).data.borrow_mut()), - ))), + data: Rc::new(RefCell::new( + (std::mem::take(&mut (*(*_a0.upgrade().deref()).data.borrow_mut()))).clone(), + )), n: Rc::new(RefCell::new((*(*_a0.upgrade().deref()).n.borrow()))), arr: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 2, _>( |__i: usize| (*(*_a0.upgrade().deref()).arr.borrow())[(__i) as usize], @@ -727,9 +727,7 @@ impl BufferImpl for Ptr { &(((*_a0.upgrade().deref()).arr.as_pointer()) as Ptr).to_any(), 8_usize as usize, ); - (((*(*self).upgrade().deref()).arr.as_pointer()) as Ptr) - .to_any() - .clone() + (((*(*self).upgrade().deref()).arr.as_pointer()) as Ptr).to_any() }; return (*self).clone(); } @@ -792,9 +790,7 @@ impl OwnerImpl for Ptr { &(((*_a0.upgrade().deref()).arr.as_pointer()) as Ptr).to_any(), 8_usize as usize, ); - (((*(*self).upgrade().deref()).arr.as_pointer()) as Ptr) - .to_any() - .clone() + (((*(*self).upgrade().deref()).arr.as_pointer()) as Ptr).to_any() }; ((*(*self).upgrade().deref()).p.as_pointer() as Ptr>>) .write((*(*_a0.upgrade().deref()).p.borrow_mut()).take()); From 85128984ca2fe88644b7750dcc767b6204d3f6a7 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Tue, 15 Sep 2026 16:01:00 +0100 Subject: [PATCH 16/16] Takes are always fresh --- cpp2rust/converter/converter.cpp | 4 +++- .../out/refcount/defaulted_move_cross_tu.rs | 6 +++--- tests/unit/out/refcount/copy_move_defaulted.rs | 18 +++++++++--------- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/cpp2rust/converter/converter.cpp b/cpp2rust/converter/converter.cpp index de510044..54a4274c 100644 --- a/cpp2rust/converter/converter.cpp +++ b/cpp2rust/converter/converter.cpp @@ -4732,7 +4732,9 @@ std::string Converter::ConvertPlaceholder(clang::Expr *expr, clang::Expr *arg, } return ConvertFreshRValue(arg); } - return std::format("std::mem::take(&mut {})", ConvertLValue(arg)); + auto lvalue = ConvertLValue(arg); + SetFresh(); + return std::format("std::mem::take(&mut {})", std::move(lvalue)); } if (ph_ctx.access == TranslationRule::Access::kMove) { diff --git a/tests/multi-file/defaulted_move_cross_tu/out/refcount/defaulted_move_cross_tu.rs b/tests/multi-file/defaulted_move_cross_tu/out/refcount/defaulted_move_cross_tu.rs index 9305e384..6c54b789 100644 --- a/tests/multi-file/defaulted_move_cross_tu/out/refcount/defaulted_move_cross_tu.rs +++ b/tests/multi-file/defaulted_move_cross_tu/out/refcount/defaulted_move_cross_tu.rs @@ -26,9 +26,9 @@ impl S { } pub fn S_pmutS(_a0: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new( - (std::mem::take(&mut (*(*_a0.upgrade().deref()).v.borrow_mut()))).clone(), - )), + v: Rc::new(RefCell::new(std::mem::take( + &mut (*(*_a0.upgrade().deref()).v.borrow_mut()), + ))), n: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 2, _>( |__i: usize| (*(*_a0.upgrade().deref()).n.borrow())[(__i) as usize], )))), diff --git a/tests/unit/out/refcount/copy_move_defaulted.rs b/tests/unit/out/refcount/copy_move_defaulted.rs index 12ff4978..8bdafca3 100644 --- a/tests/unit/out/refcount/copy_move_defaulted.rs +++ b/tests/unit/out/refcount/copy_move_defaulted.rs @@ -263,12 +263,12 @@ impl Buffer { } pub fn Buffer_pmutBuffer(_a0: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - data: Rc::new(RefCell::new( - (std::mem::take(&mut (*(*_a0.upgrade().deref()).data.borrow_mut()))).clone(), - )), - rows: Rc::new(RefCell::new( - (std::mem::take(&mut (*(*_a0.upgrade().deref()).rows.borrow_mut()))).clone(), - )), + data: Rc::new(RefCell::new(std::mem::take( + &mut (*(*_a0.upgrade().deref()).data.borrow_mut()), + ))), + rows: Rc::new(RefCell::new(std::mem::take( + &mut (*(*_a0.upgrade().deref()).rows.borrow_mut()), + ))), n: Rc::new(RefCell::new((*(*_a0.upgrade().deref()).n.borrow()))), arr: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 2, _>( |__i: usize| (*(*_a0.upgrade().deref()).arr.borrow())[(__i) as usize], @@ -321,9 +321,9 @@ pub struct Owner { impl Owner { pub fn Owner_pmutOwner(_a0: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - data: Rc::new(RefCell::new( - (std::mem::take(&mut (*(*_a0.upgrade().deref()).data.borrow_mut()))).clone(), - )), + data: Rc::new(RefCell::new(std::mem::take( + &mut (*(*_a0.upgrade().deref()).data.borrow_mut()), + ))), n: Rc::new(RefCell::new((*(*_a0.upgrade().deref()).n.borrow()))), arr: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 2, _>( |__i: usize| (*(*_a0.upgrade().deref()).arr.borrow())[(__i) as usize],