From abe4828fb1c5ec74f8fd3ae6b8ac892277cb6f85 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Tue, 15 Sep 2026 09:30:15 +0100 Subject: [PATCH 01/15] Add ComputedExprType::Unknown --- cpp2rust/converter/converter.cpp | 5 +++++ cpp2rust/converter/converter.h | 4 +++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/cpp2rust/converter/converter.cpp b/cpp2rust/converter/converter.cpp index 81a94fda..66c83554 100644 --- a/cpp2rust/converter/converter.cpp +++ b/cpp2rust/converter/converter.cpp @@ -1491,7 +1491,12 @@ bool Converter::Convert(clang::Expr *expr, NeedsImplicitScalarCast(expr->IgnoreImplicit()->getType(), *implicit_convert_to); PushParen paren(*this, needs_conversion); + computed_expr_type_ = ComputedExprType::Unknown; bool result = TraverseStmt(expr); + if (expr && computed_expr_type_ == ComputedExprType::Unknown) { + expr->dump(); + assert(false && "computed_expr_type_ not set"); + } if (needs_conversion) { ConvertCast(*implicit_convert_to); computed_expr_type_ = ComputedExprType::FreshValue; diff --git a/cpp2rust/converter/converter.h b/cpp2rust/converter/converter.h index a5117fc7..32897988 100644 --- a/cpp2rust/converter/converter.h +++ b/cpp2rust/converter/converter.h @@ -947,14 +947,16 @@ class Converter : public clang::RecursiveASTVisitor { }; enum class ComputedExprType : uint8_t { + Unknown, Value, FreshValue, Pointer, FreshPointer, }; - ComputedExprType computed_expr_type_ = ComputedExprType::FreshValue; + ComputedExprType computed_expr_type_ = ComputedExprType::Unknown; bool isFresh() const { + assert(computed_expr_type_ != ComputedExprType::Unknown); return computed_expr_type_ == ComputedExprType::FreshValue || computed_expr_type_ == ComputedExprType::FreshPointer; } From 65c1449bc85f37a3d25077bff1f1b9fbd9f567cb Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Tue, 15 Sep 2026 09:34:30 +0100 Subject: [PATCH 02/15] Add assert in SetFresh --- cpp2rust/converter/converter.cpp | 3 +++ cpp2rust/converter/converter.h | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/cpp2rust/converter/converter.cpp b/cpp2rust/converter/converter.cpp index 66c83554..df8a53ee 100644 --- a/cpp2rust/converter/converter.cpp +++ b/cpp2rust/converter/converter.cpp @@ -4779,6 +4779,9 @@ void Converter::SetFresh() { case ComputedExprType::FreshValue: case ComputedExprType::FreshPointer: break; + case ComputedExprType::Unknown: + assert(0 && "Unreachable ComputedExprType::Unknown"); + break; } } diff --git a/cpp2rust/converter/converter.h b/cpp2rust/converter/converter.h index 32897988..1116240c 100644 --- a/cpp2rust/converter/converter.h +++ b/cpp2rust/converter/converter.h @@ -947,11 +947,11 @@ class Converter : public clang::RecursiveASTVisitor { }; enum class ComputedExprType : uint8_t { - Unknown, Value, FreshValue, Pointer, FreshPointer, + Unknown, }; ComputedExprType computed_expr_type_ = ComputedExprType::Unknown; From bc4f7432f98e5bec037de7c2d3fe759770996f26 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Tue, 15 Sep 2026 10:02:42 +0100 Subject: [PATCH 03/15] Fix freshness in visitdeclrefexpr --- cpp2rust/converter/converter.cpp | 11 +++++++++++ cpp2rust/converter/models/converter_refcount.cpp | 5 +++++ tests/unit/out/unsafe/array_reference.rs | 2 +- 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/cpp2rust/converter/converter.cpp b/cpp2rust/converter/converter.cpp index df8a53ee..dd92f1e7 100644 --- a/cpp2rust/converter/converter.cpp +++ b/cpp2rust/converter/converter.cpp @@ -1814,6 +1814,7 @@ void Converter::EmitFnPtrCall(clang::Expr *callee) { void Converter::ConvertFunctionToFunctionPointer( const clang::FunctionDecl *fn_decl) { StrCat(std::format("Some({})", Mapper::MapFunctionName(fn_decl))); + computed_expr_type_ = ComputedExprType::FreshPointer; } Converter::CallInfo Converter::CollectCallInfo(clang::CallExpr *expr) { @@ -2887,6 +2888,9 @@ bool Converter::VisitDeclRefExpr(clang::DeclRefExpr *expr) { ConvertFunctionToFunctionPointer(fn_decl); return false; } + StrCat(str); + SetFreshType(expr->getType()); + return false; } if (auto var_decl = clang::dyn_cast(decl)) { @@ -2896,6 +2900,7 @@ bool Converter::VisitDeclRefExpr(clang::DeclRefExpr *expr) { init->IgnoreUnlessSpelledInSource())) { PushParen paren(*this); VisitLambdaExpr(lambda); + computed_expr_type_ = ComputedExprType::FreshValue; return false; } } @@ -2905,10 +2910,16 @@ bool Converter::VisitDeclRefExpr(clang::DeclRefExpr *expr) { if (!decl->getType()->getAs() && isAddrOf()) { StrCat(token::kRef, decl->getType().isConstQualified() ? "" : keyword_mut_, str); + computed_expr_type_ = ComputedExprType::FreshPointer; return false; } StrCat(str); + if (clang::isa(decl)) { + computed_expr_type_ = ComputedExprType::FreshValue; + return false; + } + SetValueFreshness(expr->getType()); return false; } diff --git a/cpp2rust/converter/models/converter_refcount.cpp b/cpp2rust/converter/models/converter_refcount.cpp index 4c10e375..ca853ff4 100644 --- a/cpp2rust/converter/models/converter_refcount.cpp +++ b/cpp2rust/converter/models/converter_refcount.cpp @@ -808,6 +808,7 @@ bool ConverterRefCount::VisitDeclRefExpr(clang::DeclRefExpr *expr) { clang::Expr *addrof_op = ToAddrOf(ctx_, expr); if (auto str = GetMappedAsString(addrof_op); !str.empty()) { StrCat(str); + SetFreshType(expr->getType()); return false; } } @@ -815,6 +816,7 @@ bool ConverterRefCount::VisitDeclRefExpr(clang::DeclRefExpr *expr) { if (ShouldReplaceWithMappedBody(expr)) { if (auto str = GetMappedAsString(expr); !str.empty()) { StrCat(str); + SetFreshType(expr->getType()); return false; } } @@ -827,12 +829,14 @@ bool ConverterRefCount::VisitDeclRefExpr(clang::DeclRefExpr *expr) { ConvertFunctionToFunctionPointer(fn_decl); } else { StrCat(str); + SetFreshType(expr->getType()); } return false; } if (clang::isa(decl)) { StrCat(str); + computed_expr_type_ = ComputedExprType::FreshValue; return false; } @@ -845,6 +849,7 @@ bool ConverterRefCount::VisitDeclRefExpr(clang::DeclRefExpr *expr) { if (auto *ref = decl_t->getAs()) { if (map_iter_decls_.contains(clang::dyn_cast(decl))) { StrCat(str); + SetValueFreshness(expr->getType()); return false; } 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; From 1d80951324c1f3c12ed831baba0961e818fdb192 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Tue, 15 Sep 2026 10:09:40 +0100 Subject: [PATCH 04/15] Set computed expr type for calls --- cpp2rust/converter/converter.cpp | 16 +++++++++++++--- tests/unit/out/unsafe/array_reference.rs | 2 +- tests/unit/out/unsafe/unistd.rs | 2 +- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/cpp2rust/converter/converter.cpp b/cpp2rust/converter/converter.cpp index dd92f1e7..c5aa07e9 100644 --- a/cpp2rust/converter/converter.cpp +++ b/cpp2rust/converter/converter.cpp @@ -1726,17 +1726,20 @@ void Converter::ConvertVAArgCall(clang::CallExpr *expr) { bool Converter::VisitCallExpr(clang::CallExpr *expr) { if (IsBuiltinVaStart(expr) || IsBuiltinVaEnd(expr) || IsBuiltinVaCopy(expr)) { ConvertVAArgCall(expr); + SetFreshType(expr->getType()); return false; } // p->~T() on a scalar is a no-op if (clang::isa( expr->getCallee()->IgnoreParenImpCasts())) { + SetFreshType(expr->getType()); return false; } if (auto plugin_str = TryPluginConvert(expr)) { StrCat(*plugin_str); + SetFreshType(expr->getType()); return false; } @@ -1755,9 +1758,10 @@ bool Converter::VisitCallExpr(clang::CallExpr *expr) { str = GetMappedAsString(expr, args, num_args, &ctx); }; - if ((IsReferenceType(expr) || - GetReturnTypeOfFunction(expr)->isReferenceType()) && - !isAddrOf() && !isVoid()) { + bool deref_ref = (IsReferenceType(expr) || + GetReturnTypeOfFunction(expr)->isReferenceType()) && + !isAddrOf() && !isVoid(); + if (deref_ref) { str = "( * " + std::move(str) + " )"; } @@ -1766,6 +1770,11 @@ bool Converter::VisitCallExpr(clang::CallExpr *expr) { } StrCat(str); + if (deref_ref) { + SetValueFreshness(expr->getType()); + } else { + SetFreshType(expr->getType()); + } return false; } @@ -1800,6 +1809,7 @@ bool Converter::VisitCallExpr(clang::CallExpr *expr) { } StrCat(str); + SetFreshType(expr->getType()); return false; } 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/unistd.rs b/tests/unit/out/unsafe/unistd.rs index 4d15e652..ef426d0c 100644 --- a/tests/unit/out/unsafe/unistd.rs +++ b/tests/unit/out/unsafe/unistd.rs @@ -220,7 +220,7 @@ pub unsafe fn test_ftruncate_5() { assert!(((((libc::fclose(fp)) == (0)) as i32) != 0)); fp = libc::fopen(path, (c"rb".as_ptr().cast_mut()).cast_const()); assert!((((!((fp).is_null())) as i32) != 0)); - fd = (libc::fileno(fp)).clone(); + fd = libc::fileno(fp); assert!(((((libc::lseek(fd, 0_i64, ::libc::SEEK_END)) == (5_i64)) as i32) != 0)); assert!(((((libc::fclose(fp)) == (0)) as i32) != 0)); libc::unlink(path); From 2e8bdd9a962a414306be48241962cafdb9aa3f06 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Tue, 15 Sep 2026 10:15:28 +0100 Subject: [PATCH 05/15] Add rest of computed expr type --- cpp2rust/converter/converter.cpp | 27 +++++++++++++++++++ .../converter/models/converter_refcount.cpp | 4 +++ tests/unit/out/unsafe/huffman.rs | 10 +++---- 3 files changed, 35 insertions(+), 6 deletions(-) diff --git a/cpp2rust/converter/converter.cpp b/cpp2rust/converter/converter.cpp index c5aa07e9..61b59f8f 100644 --- a/cpp2rust/converter/converter.cpp +++ b/cpp2rust/converter/converter.cpp @@ -2272,6 +2272,7 @@ void Converter::ConvertIntegerToEnumeralCast(clang::Expr *to, auto dst_enum = to->getType()->getAs(); if (src_enum && dst_enum && dst_enum->getDecl() == src_enum) { StrCat(EnumeratorName(ec)); + computed_expr_type_ = ComputedExprType::FreshValue; return; } } @@ -2282,6 +2283,7 @@ void Converter::ConvertIntegerToEnumeralCast(clang::Expr *to, Convert(from); } StrCat(keyword::kAs, GetUnsafeTypeAsString(to->getType())); + computed_expr_type_ = ComputedExprType::FreshValue; } void Converter::ConvertIntegralToBooleanCast(clang::ImplicitCastExpr *expr) { @@ -2301,6 +2303,7 @@ void Converter::ConvertIntegralToBooleanCast(clang::ImplicitCastExpr *expr) { Convert(sub_expr); StrCat(token::kDiff); StrCat(token::kZero); + computed_expr_type_ = ComputedExprType::FreshValue; } bool Converter::IsCastRedundantInRust(clang::Expr *expr, @@ -2339,6 +2342,7 @@ bool Converter::VisitImplicitCastExpr(clang::ImplicitCastExpr *expr) { } else { StrCat(dest_pointee_const ? ".as_ptr()" : ".as_mut_ptr()"); } + computed_expr_type_ = ComputedExprType::FreshPointer; break; } case clang::CastKind::CK_BitCast: { @@ -2350,6 +2354,7 @@ bool Converter::VisitImplicitCastExpr(clang::ImplicitCastExpr *expr) { StrCat(ConvertPointeeType(sub_expr->getType())); } ConvertCast(type); + SetFreshType(type); break; } case clang::CastKind::CK_NoOp: { @@ -2374,6 +2379,7 @@ bool Converter::VisitImplicitCastExpr(clang::ImplicitCastExpr *expr) { PushParen paren(*this); Convert(sub_expr); ConvertCast(type); + SetFreshType(type); } else { { PushParen paren(*this, suffix); @@ -2381,6 +2387,7 @@ bool Converter::VisitImplicitCastExpr(clang::ImplicitCastExpr *expr) { } if (suffix) { StrCat(suffix); + SetFreshType(type); } } break; @@ -2440,6 +2447,7 @@ bool Converter::VisitImplicitCastExpr(clang::ImplicitCastExpr *expr) { ConvertCast(type); } } + SetFreshType(type); } return false; } @@ -2730,6 +2738,7 @@ bool Converter::ConvertIncAndDec(clang::UnaryOperator *expr) { bool Converter::VisitUnaryOperator(clang::UnaryOperator *expr) { if (auto str = GetMappedAsString(expr); !str.empty()) { StrCat(str); + SetFreshType(expr->getType()); return false; } @@ -2786,6 +2795,7 @@ bool Converter::VisitUnaryOperator(clang::UnaryOperator *expr) { default: StrCat(expr->getOpcodeStr(opcode)); Convert(sub_expr); + SetFreshType(expr->getType()); } return false; } @@ -3022,6 +3032,7 @@ bool Converter::VisitMemberExpr(clang::MemberExpr *expr) { SetUFCSReceiver(expr->getBase(), expr->isArrow(), method); StrCat(GetRecordName(method->getParent()), token::kDoubleColon, GetMethodName(method)); + SetFreshType(expr->getType()); return false; } std::string str; @@ -3055,10 +3066,16 @@ bool Converter::VisitMemberExpr(clang::MemberExpr *expr) { if (!isAddrOf() && member->getType()->isFunctionPointerType()) { PushParen paren(*this); StrCat(str); + SetValueFreshness(expr->getType()); return false; } StrCat(str); + if (clang::isa(member)) { + SetFreshType(expr->getType()); + } else { + SetValueFreshness(expr->getType()); + } return false; } @@ -3170,6 +3187,7 @@ bool Converter::VisitCXXThisExpr(clang::CXXThisExpr *expr) { PushParen paren(*this); StrCat(keyword::kSelfValue, keyword::kAs, ToString(expr->getType())); } + computed_expr_type_ = ComputedExprType::FreshPointer; return false; } @@ -3195,6 +3213,7 @@ bool Converter::VisitInitListExpr(clang::InitListExpr *expr) { } else { StrCat(GetArrayDefaultAsString(qual_type)); } + SetFreshType(qual_type); return false; } @@ -3230,6 +3249,7 @@ bool Converter::VisitInitListExpr(clang::InitListExpr *expr) { } } } + SetFreshType(qual_type); return false; } @@ -3433,6 +3453,7 @@ bool Converter::VisitCXXConstructExpr(clang::CXXConstructExpr *expr) { if (auto str = GetMappedAsString(expr, expr->getArgs(), expr->getNumArgs()); !str.empty()) { StrCat(str); + SetFreshType(expr->getType()); return false; } @@ -3454,6 +3475,7 @@ bool Converter::VisitCXXConstructExpr(clang::CXXConstructExpr *expr) { if ((ctor->isCopyConstructor() || IsDefaultedMoveConstructor(ctor)) && !suppress && !TypeIsCopyable(expr->getType())) { StrCat(".clone()"); + SetFreshType(expr->getType()); } return false; } @@ -3461,6 +3483,7 @@ bool Converter::VisitCXXConstructExpr(clang::CXXConstructExpr *expr) { if (ctor->isDefaultConstructor() && !ctor->isUserProvided()) { auto ty = expr->getType(); StrCat(GetDefaultAsString(ty)); + SetFreshType(expr->getType()); return false; } @@ -3470,6 +3493,7 @@ bool Converter::VisitCXXConstructExpr(clang::CXXConstructExpr *expr) { } else { ConvertCXXConstructExprArgs(expr); } + SetFreshType(expr->getType()); return false; } @@ -4503,6 +4527,7 @@ void Converter::ConvertAddrOf(clang::Expr *expr, clang::QualType pointer_type) { : keyword_mut_); Convert(expr); ConvertCast(pointer_type); + computed_expr_type_ = ComputedExprType::FreshPointer; } else { StrCat(token::kRef); if (!pointer_type->getPointeeType().isConstQualified()) { @@ -4510,6 +4535,7 @@ void Converter::ConvertAddrOf(clang::Expr *expr, clang::QualType pointer_type) { } Convert(expr); ConvertCast(pointer_type); + computed_expr_type_ = ComputedExprType::FreshPointer; } } @@ -4521,6 +4547,7 @@ void Converter::EmitDeref(std::string inner, clang::QualType pointee_type) { } PushParen paren(*this); StrCat(GetPointerDerefPrefix(pointee_type), std::move(inner)); + SetValueFreshness(pointee_type); } void Converter::ConvertDeref(clang::Expr *expr) { diff --git a/cpp2rust/converter/models/converter_refcount.cpp b/cpp2rust/converter/models/converter_refcount.cpp index ca853ff4..ff8eba1c 100644 --- a/cpp2rust/converter/models/converter_refcount.cpp +++ b/cpp2rust/converter/models/converter_refcount.cpp @@ -1226,6 +1226,7 @@ bool ConverterRefCount::VisitImplicitCastExpr(clang::ImplicitCastExpr *expr) { if (IsStringLiteralExpr(sub_expr)) { StrCat(std::format("Ptr::from_string_literal({})", ToString(sub_expr->IgnoreParens()))); + computed_expr_type_ = ComputedExprType::FreshPointer; return false; } else { // we need to write (var.as_pointer as Ptr) because Rust isn't @@ -1235,6 +1236,7 @@ bool ConverterRefCount::VisitImplicitCastExpr(clang::ImplicitCastExpr *expr) { StrCat(IsReferenceType(sub_expr) ? ConvertObject(sub_expr) : ConvertPointer(sub_expr), keyword::kAs, ToString(expr->getType())); + computed_expr_type_ = ComputedExprType::FreshPointer; return false; } } @@ -1652,6 +1654,7 @@ bool ConverterRefCount::VisitMemberExpr(clang::MemberExpr *expr) { SetUFCSReceiver(expr->getBase(), expr->isArrow(), method); StrCat(TraitName(method->getParent()), token::kDoubleColon, GetMethodName(method)); + SetFreshType(expr->getType()); return false; } // User-defined types have Value fields; the struct itself is read-only @@ -1664,6 +1667,7 @@ bool ConverterRefCount::VisitMemberExpr(clang::MemberExpr *expr) { bool needs_mut = NeedsMutAccess(method, base_type); PushExprKind push(*this, needs_mut ? ExprKind::LValue : ExprKind::RValue); Converter::ConvertMemberExpr(expr); + SetFreshType(expr->getType()); return false; } diff --git a/tests/unit/out/unsafe/huffman.rs b/tests/unit/out/unsafe/huffman.rs index 75a246d4..ce0a4293 100644 --- a/tests/unit/out/unsafe/huffman.rs +++ b/tests/unit/out/unsafe/huffman.rs @@ -26,20 +26,18 @@ pub unsafe fn Swap_0(a: *mut MinHeapNode, b: *mut MinHeapNode) { left: (*a).left, right: (*a).right, }; - (*a) = (MinHeapNode { + (*a) = MinHeapNode { data: (*b).data, freq: (*b).freq, left: (*b).left, right: (*b).right, - }) - .clone(); - (*b) = (MinHeapNode { + }; + (*b) = MinHeapNode { data: t.data, freq: t.freq, left: t.left, right: t.right, - }) - .clone(); + }; } #[repr(C)] #[derive(Default)] From ee9614b920186ee62a5607f22b785dc70fb4d119 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Tue, 15 Sep 2026 10:18:35 +0100 Subject: [PATCH 06/15] Add VisitConstantExpr --- cpp2rust/converter/converter.cpp | 6 ++++++ cpp2rust/converter/converter.h | 1 + 2 files changed, 7 insertions(+) diff --git a/cpp2rust/converter/converter.cpp b/cpp2rust/converter/converter.cpp index 61b59f8f..a4c9fea7 100644 --- a/cpp2rust/converter/converter.cpp +++ b/cpp2rust/converter/converter.cpp @@ -3591,6 +3591,12 @@ bool Converter::VisitCXXDefaultArgExpr(clang::CXXDefaultArgExpr *expr) { return false; } +bool Converter::VisitConstantExpr(clang::ConstantExpr *expr) { + Convert(expr->getSubExpr()); + SetFreshType(expr->getType()); + return false; +} + bool Converter::VisitLambdaExpr(clang::LambdaExpr *expr) { if (isAddrOf() && expr->capture_size() == 0) { StrCat("Some"); diff --git a/cpp2rust/converter/converter.h b/cpp2rust/converter/converter.h index 1116240c..c15e565a 100644 --- a/cpp2rust/converter/converter.h +++ b/cpp2rust/converter/converter.h @@ -416,6 +416,7 @@ class Converter : public clang::RecursiveASTVisitor { virtual std::string EnumeratorName(const clang::EnumConstantDecl *decl) const; virtual bool VisitCXXDefaultArgExpr(clang::CXXDefaultArgExpr *expr); + virtual bool VisitConstantExpr(clang::ConstantExpr *expr); virtual bool VisitLambdaExpr(clang::LambdaExpr *expr); From 1235881cb45b6f43bb367d4a54dc3e7f70668f3c Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Tue, 15 Sep 2026 10:25:00 +0100 Subject: [PATCH 07/15] AddComputedExprType::Pending --- cpp2rust/converter/converter.cpp | 3 +++ cpp2rust/converter/converter.h | 2 ++ cpp2rust/converter/models/converter_refcount.cpp | 4 ++++ cpp2rust/converter/models/converter_refcount.h | 10 ++++++++-- 4 files changed, 17 insertions(+), 2 deletions(-) diff --git a/cpp2rust/converter/converter.cpp b/cpp2rust/converter/converter.cpp index a4c9fea7..f4e37a09 100644 --- a/cpp2rust/converter/converter.cpp +++ b/cpp2rust/converter/converter.cpp @@ -4836,6 +4836,9 @@ void Converter::SetFresh() { case ComputedExprType::Unknown: assert(0 && "Unreachable ComputedExprType::Unknown"); break; + case ComputedExprType::Pending: + assert(0 && "Unreachable ComputedExprType::Pending"); + break; } } diff --git a/cpp2rust/converter/converter.h b/cpp2rust/converter/converter.h index c15e565a..f5f6e6a8 100644 --- a/cpp2rust/converter/converter.h +++ b/cpp2rust/converter/converter.h @@ -953,11 +953,13 @@ class Converter : public clang::RecursiveASTVisitor { Pointer, FreshPointer, Unknown, + Pending, }; ComputedExprType computed_expr_type_ = ComputedExprType::Unknown; bool isFresh() const { assert(computed_expr_type_ != ComputedExprType::Unknown); + assert(computed_expr_type_ != ComputedExprType::Pending); return computed_expr_type_ == ComputedExprType::FreshValue || computed_expr_type_ == ComputedExprType::FreshPointer; } diff --git a/cpp2rust/converter/models/converter_refcount.cpp b/cpp2rust/converter/models/converter_refcount.cpp index ff8eba1c..353b2010 100644 --- a/cpp2rust/converter/models/converter_refcount.cpp +++ b/cpp2rust/converter/models/converter_refcount.cpp @@ -81,6 +81,7 @@ void ConverterRefCount::PendingDeref::set_unchecked(std::string str, clang::Expr *expr) { value = std::move(str); pointee_is_boxed = compute_inner_boxed(expr); + type = ComputedExprType::Pending; } std::string ConverterRefCount::GetInnerType(clang::QualType type) { @@ -2119,6 +2120,7 @@ void ConverterRefCount::EmitSetOrAssign(clang::Expr *lhs, } else { StrCat(lhs_str, token::kAssign, rhs); } + computed_expr_type_ = ComputedExprType::FreshValue; } void ConverterRefCount::ConvertAssignment(clang::Expr *lhs, clang::Expr *rhs, @@ -2149,6 +2151,7 @@ void ConverterRefCount::ConvertAssignment(clang::Expr *lhs, clang::Expr *rhs, } else { StrCat(lhs_str, assign_operator, rhs_as_string); } + computed_expr_type_ = ComputedExprType::FreshValue; } if (isRValue()) { @@ -2604,6 +2607,7 @@ std::string ConverterRefCount::ConvertMappedMethodCall( bool is_boxed = pending_deref_.is_boxed(); auto ptr = pending_deref_.take(); auto body = ConvertIRFragment(mc.body, expr, args, num_args, ctx); + SetFreshType(expr->getType()); if (is_boxed) { return std::format( diff --git a/cpp2rust/converter/models/converter_refcount.h b/cpp2rust/converter/models/converter_refcount.h index 818e06c9..8da14883 100644 --- a/cpp2rust/converter/models/converter_refcount.h +++ b/cpp2rust/converter/models/converter_refcount.h @@ -177,7 +177,11 @@ class ConverterRefCount final : public Converter { bool Convert(clang::Expr *expr, std::optional implicit_convert_to = {}) override { - return Converter::Convert(expr, implicit_convert_to); + auto result = Converter::Convert(expr, implicit_convert_to); + if (computed_expr_type_ == ComputedExprType::Pending) { + assert(!pending_deref_.empty() && "pending_deref_ taken without type"); + } + return result; } bool Convert(clang::Stmt *stmt) override { auto result = Converter::Convert(stmt); @@ -353,6 +357,7 @@ class ConverterRefCount final : public Converter { // emit ptr.write(rhs), or by ConvertMappedMethodCall to emit // ptr.with_mut(...). struct PendingDeref { + explicit PendingDeref(ComputedExprType &type) : type(type) {} void set(std::string str, clang::Expr *expr = nullptr); void set_unchecked(std::string str, clang::Expr *expr = nullptr); std::string take() { @@ -369,8 +374,9 @@ class ConverterRefCount final : public Converter { private: static bool compute_inner_boxed(clang::Expr *expr); + ComputedExprType &type; std::string value; bool pointee_is_boxed = false; - } pending_deref_; + } pending_deref_{computed_expr_type_}; }; } // namespace cpp2rust From f2bf6e1ecbcd6ce84f1ee1fd78655eb025483ed9 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Tue, 15 Sep 2026 10:41:58 +0100 Subject: [PATCH 08/15] Add fresh pointer attribute on PendingDeref --- .../converter/models/converter_refcount.cpp | 31 +++++++++++-------- .../converter/models/converter_refcount.h | 8 +++-- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/cpp2rust/converter/models/converter_refcount.cpp b/cpp2rust/converter/models/converter_refcount.cpp index 353b2010..941d77cb 100644 --- a/cpp2rust/converter/models/converter_refcount.cpp +++ b/cpp2rust/converter/models/converter_refcount.cpp @@ -72,15 +72,17 @@ bool ConverterRefCount::PendingDeref::compute_inner_boxed(clang::Expr *expr) { return false; } -void ConverterRefCount::PendingDeref::set(std::string str, clang::Expr *expr) { +void ConverterRefCount::PendingDeref::set(std::string str, bool fresh, + clang::Expr *expr) { assert_consumed(); - set_unchecked(std::move(str), expr); + set_unchecked(std::move(str), fresh, expr); } -void ConverterRefCount::PendingDeref::set_unchecked(std::string str, +void ConverterRefCount::PendingDeref::set_unchecked(std::string str, bool fresh, clang::Expr *expr) { value = std::move(str); pointee_is_boxed = compute_inner_boxed(expr); + ptr_is_fresh = fresh; type = ComputedExprType::Pending; } @@ -410,7 +412,7 @@ bool ConverterRefCount::VisitArraySubscriptExpr( pending_deref_.assert_consumed(); Buffer buf(*this); ConvertArraySubscript(base, expr->getIdx(), expr->getType()); - pending_deref_.set_unchecked(std::move(buf).str(), expr); + pending_deref_.set_unchecked(std::move(buf).str(), isFresh(), expr); return false; } PushParen paren(*this); @@ -874,7 +876,7 @@ bool ConverterRefCount::VisitDeclRefExpr(clang::DeclRefExpr *expr) { StrCat(str); } else { if (isLValue()) { - pending_deref_.set(str); + pending_deref_.set(str, /*fresh=*/false); return false; } StrCat(DerefPtrExpr(str, ref->getPointeeType())); @@ -1095,7 +1097,7 @@ bool ConverterRefCount::VisitCallExpr(clang::CallExpr *expr) { if (ctx && !ctx->temporary_bindings.empty()) { str = std::format("{{ {} {} }}", ctx->temporary_bindings, str); } - pending_deref_.set(str); + pending_deref_.set(str, /*fresh=*/true); return false; } // Apply deref before block wrapping so temporaries are still alive. @@ -1638,7 +1640,7 @@ void ConverterRefCount::ConvertUnionMemberAccessor(clang::MemberExpr *expr) { } if (isLValue()) { - pending_deref_.set(str); + pending_deref_.set(str, /*fresh=*/true); return; } StrCat(DerefPtrExpr(str, member->getType())); @@ -1702,7 +1704,7 @@ bool ConverterRefCount::VisitMemberExpr(clang::MemberExpr *expr) { if (member->getType()->isReferenceType()) { if (isLValue()) { - pending_deref_.set(str); + pending_deref_.set(str, /*fresh=*/false); return false; } StrCat(DerefPtrExpr(str, member->getType().getNonReferenceType())); @@ -2140,12 +2142,13 @@ void ConverterRefCount::ConvertAssignment(clang::Expr *lhs, clang::Expr *rhs, } else { auto lhs_str = ConvertLValue(lhs); if (!pending_deref_.empty()) { + bool fresh = pending_deref_.is_fresh(); auto ptr = pending_deref_.take(); auto op = assign_operator; op.remove_suffix(1); // remove '=' { PushBrace brace(*this); - StrCat(std::format("let _ptr = {}.clone();", ptr)); + StrCat(std::format("let _ptr = {}{};", ptr, fresh ? "" : ".clone()")); StrCat(std::format("_ptr.write(_ptr.read() {} {})", op, rhs_as_string)); } } else { @@ -2232,7 +2235,8 @@ bool ConverterRefCount::ConvertCXXOperatorCallExpr( } if (isLValue()) { - pending_deref_.set(ToString(expr->getArg(0))); + auto ptr = ToString(expr->getArg(0)); + pending_deref_.set(std::move(ptr), isFresh()); break; } @@ -2285,7 +2289,7 @@ bool ConverterRefCount::ConvertCXXOperatorCallExpr( ConvertObject(expr->getArg(0)), ConvertPtrType(expr->getArg(0)->getType()), ConvertSubscriptIndex(expr->getArg(1))), - expr); + /*fresh=*/true, expr); break; } @@ -2406,7 +2410,7 @@ void ConverterRefCount::ConvertPointerSubscript( pending_deref_.assert_consumed(); Buffer buf(*this); ConvertPointerOffset(base, idx); - pending_deref_.set_unchecked(std::move(buf).str(), expr); + pending_deref_.set_unchecked(std::move(buf).str(), isFresh(), expr); return; } @@ -2454,7 +2458,8 @@ void ConverterRefCount::ConvertDeref(clang::Expr *expr) { auto pointee_type = expr->getType()->getPointeeType(); if (isLValue()) { - pending_deref_.set(ToString(expr)); + auto ptr = ToString(expr); + pending_deref_.set(std::move(ptr), isFresh()); return; } diff --git a/cpp2rust/converter/models/converter_refcount.h b/cpp2rust/converter/models/converter_refcount.h index 8da14883..f588f6d1 100644 --- a/cpp2rust/converter/models/converter_refcount.h +++ b/cpp2rust/converter/models/converter_refcount.h @@ -358,16 +358,19 @@ class ConverterRefCount final : public Converter { // ptr.with_mut(...). struct PendingDeref { explicit PendingDeref(ComputedExprType &type) : type(type) {} - void set(std::string str, clang::Expr *expr = nullptr); - void set_unchecked(std::string str, clang::Expr *expr = nullptr); + void set(std::string str, bool fresh, clang::Expr *expr = nullptr); + void set_unchecked(std::string str, bool fresh, + clang::Expr *expr = nullptr); std::string take() { auto result = std::move(value); value.clear(); pointee_is_boxed = false; + ptr_is_fresh = false; return result; } bool empty() const { return value.empty(); } bool is_boxed() const { return pointee_is_boxed; } + bool is_fresh() const { return ptr_is_fresh; } void assert_consumed() const { assert(value.empty() && "pending_deref_ not consumed"); } @@ -377,6 +380,7 @@ class ConverterRefCount final : public Converter { ComputedExprType &type; std::string value; bool pointee_is_boxed = false; + bool ptr_is_fresh = false; } pending_deref_{computed_expr_type_}; }; } // namespace cpp2rust From b085242b4fb8541c3da154ad27eccb55a70f1b00 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Tue, 15 Sep 2026 10:42:16 +0100 Subject: [PATCH 09/15] Don't clone trivially copyable type in range statement --- cpp2rust/converter/models/converter_refcount.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/cpp2rust/converter/models/converter_refcount.cpp b/cpp2rust/converter/models/converter_refcount.cpp index 941d77cb..e3029d2c 100644 --- a/cpp2rust/converter/models/converter_refcount.cpp +++ b/cpp2rust/converter/models/converter_refcount.cpp @@ -1832,10 +1832,11 @@ bool ConverterRefCount::VisitCXXForRangeStmtVector( } StrCat(token::kSemiColon); } else { - EmitByValueShadow(loop_var_name, loop_var->getType(), - loop_var_name + - GetPointerDerefSuffix(loop_var->getType()) + - ".clone()"); + auto type = loop_var->getType(); + bool copy = type.isPODType(ctx_) && !type->isRecordType(); + EmitByValueShadow(loop_var_name, type, + loop_var_name + GetPointerDerefSuffix(type) + + (copy ? "" : ".clone()")); } ConvertForRangeBody(stmt); From 7b5abef745465eb4b9f649550a46505788b876f1 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Tue, 15 Sep 2026 10:42:21 +0100 Subject: [PATCH 10/15] Update tests --- tests/unit/out/refcount/auto.rs | 2 +- tests/unit/out/refcount/complex_function.rs | 13 +++++-------- tests/unit/out/refcount/compound_assign_ref.rs | 2 +- tests/unit/out/refcount/foreach.rs | 2 +- .../out/refcount/foreach_disjoint_field_borrow.rs | 2 +- tests/unit/out/refcount/foreach_double.rs | 4 ++-- tests/unit/out/refcount/foreach_mut.rs | 12 ++++++------ tests/unit/out/refcount/iterators.rs | 2 +- tests/unit/out/refcount/pointers.rs | 2 +- tests/unit/out/refcount/vector3.rs | 4 ++-- 10 files changed, 21 insertions(+), 24 deletions(-) diff --git a/tests/unit/out/refcount/auto.rs b/tests/unit/out/refcount/auto.rs index 45d2ce42..f1628708 100644 --- a/tests/unit/out/refcount/auto.rs +++ b/tests/unit/out/refcount/auto.rs @@ -18,7 +18,7 @@ fn main_0() -> i32 { (*v.borrow_mut()).push(2); let sum: Value = Rc::new(RefCell::new(0)); 'loop_: for mut elem in v.as_pointer() as Ptr { - let elem: Value = Rc::new(RefCell::new(elem.read().clone())); + let elem: Value = Rc::new(RefCell::new(elem.read())); (*sum.borrow_mut()) += (*elem.borrow()); } assert!(((*sum.borrow()) == 3)); diff --git a/tests/unit/out/refcount/complex_function.rs b/tests/unit/out/refcount/complex_function.rs index 245903fc..c2c67a7c 100644 --- a/tests/unit/out/refcount/complex_function.rs +++ b/tests/unit/out/refcount/complex_function.rs @@ -190,7 +190,7 @@ fn main_0() -> i32 { .borrow()), )); { - let _ptr = ({ bar_2(x1.as_pointer()) }).clone(); + let _ptr = ({ bar_2(x1.as_pointer()) }); _ptr.write(_ptr.read() + 10) }; ({ bar_2(x1.as_pointer()) }).with_mut(|__v| __v.postfix_inc()); @@ -221,8 +221,7 @@ fn main_0() -> i32 { .v .as_pointer(), ) - }) - .clone(); + }); _ptr.write(_ptr.read() + 10) }; ({ @@ -259,7 +258,7 @@ fn main_0() -> i32 { .with_mut(|__v| __v.postfix_inc()); ({ ptr_1((x1.as_pointer())) }).with_mut(|__v| __v.prefix_inc()); { - let _ptr = ({ ptr_1((x1.as_pointer())) }).clone(); + let _ptr = ({ ptr_1((x1.as_pointer())) }); _ptr.write(_ptr.read() + 1) }; ({ @@ -281,8 +280,7 @@ fn main_0() -> i32 { .v .as_pointer()), ) - }) - .clone(); + }); _ptr.write(_ptr.read() + 1) }; { @@ -294,8 +292,7 @@ fn main_0() -> i32 { .v .as_pointer()), ) - }) - .clone(); + }); _ptr.write(_ptr.read() + 1) }; let ptr1: Value = Rc::new(RefCell::new( diff --git a/tests/unit/out/refcount/compound_assign_ref.rs b/tests/unit/out/refcount/compound_assign_ref.rs index 6b94fc47..df941a46 100644 --- a/tests/unit/out/refcount/compound_assign_ref.rs +++ b/tests/unit/out/refcount/compound_assign_ref.rs @@ -13,7 +13,7 @@ fn main_0() -> i32 { let v: Value> = Rc::new(RefCell::new(Vec::new())); (*v.borrow_mut()).push(10); { - let _ptr = (v.as_pointer() as Ptr).clone(); + let _ptr = (v.as_pointer() as Ptr); _ptr.write(_ptr.read() + 5) }; assert!((((v.as_pointer() as Ptr).read()) == 15)); diff --git a/tests/unit/out/refcount/foreach.rs b/tests/unit/out/refcount/foreach.rs index 74d3697c..0a497228 100644 --- a/tests/unit/out/refcount/foreach.rs +++ b/tests/unit/out/refcount/foreach.rs @@ -21,7 +21,7 @@ fn main_0() -> i32 { } let sum: Value = Rc::new(RefCell::new(0)); 'loop_: for mut x in v.as_pointer() as Ptr { - let x: Value = Rc::new(RefCell::new(x.read().clone())); + let x: Value = Rc::new(RefCell::new(x.read())); (*sum.borrow_mut()) += (*x.borrow()); } assert!(((*sum.borrow()) == 45)); diff --git a/tests/unit/out/refcount/foreach_disjoint_field_borrow.rs b/tests/unit/out/refcount/foreach_disjoint_field_borrow.rs index ce6419ef..7649198e 100644 --- a/tests/unit/out/refcount/foreach_disjoint_field_borrow.rs +++ b/tests/unit/out/refcount/foreach_disjoint_field_borrow.rs @@ -43,7 +43,7 @@ fn main_0() -> i32 { let s: Value = Rc::new(RefCell::new(::default())); (*(*s.borrow()).v.borrow_mut()).push(1); 'loop_: for mut e in (*s.borrow()).v.as_pointer() as Ptr { - let e: Value = Rc::new(RefCell::new(e.read().clone())); + let e: Value = Rc::new(RefCell::new(e.read())); (*(*s.borrow()).a.borrow_mut()).postfix_inc(); } return 0; diff --git a/tests/unit/out/refcount/foreach_double.rs b/tests/unit/out/refcount/foreach_double.rs index 7766ac69..7140155b 100644 --- a/tests/unit/out/refcount/foreach_double.rs +++ b/tests/unit/out/refcount/foreach_double.rs @@ -16,9 +16,9 @@ fn main_0() -> i32 { (*v.borrow_mut()).push(3); let square: Value = Rc::new(RefCell::new(0)); 'loop_: for mut e1 in v.as_pointer() as Ptr { - let e1: Value = Rc::new(RefCell::new(e1.read().clone())); + let e1: Value = Rc::new(RefCell::new(e1.read())); 'loop_: for mut e2 in v.as_pointer() as Ptr { - let e2: Value = Rc::new(RefCell::new(e2.read().clone())); + let e2: Value = Rc::new(RefCell::new(e2.read())); (*square.borrow_mut()) += ((*e1.borrow()) * (*e2.borrow())); } } diff --git a/tests/unit/out/refcount/foreach_mut.rs b/tests/unit/out/refcount/foreach_mut.rs index 09ffe44e..6418ab6b 100644 --- a/tests/unit/out/refcount/foreach_mut.rs +++ b/tests/unit/out/refcount/foreach_mut.rs @@ -16,11 +16,11 @@ fn main_0() -> i32 { (*v1.borrow_mut()).push(3); let sum: Value = Rc::new(RefCell::new(0)); 'loop_: for mut x in v1.as_pointer() as Ptr { - let x: Value = Rc::new(RefCell::new(x.read().clone())); + let x: Value = Rc::new(RefCell::new(x.read())); (*sum.borrow_mut()) += (*x.borrow_mut()).prefix_inc(); } 'loop_: for x in v1.as_pointer() as Ptr { - let x: Value = Rc::new(RefCell::new(x.read().clone())); + let x: Value = Rc::new(RefCell::new(x.read())); (*sum.borrow_mut()) += (*x.borrow()); } 'loop_: for mut x in v1.as_pointer() as Ptr { @@ -38,26 +38,26 @@ fn main_0() -> i32 { (*v2.borrow_mut()).push(((v1.as_pointer() as Ptr).offset(1_usize))); (*v2.borrow_mut()).push(((v1.as_pointer() as Ptr).offset(2_usize))); 'loop_: for mut p in v2.as_pointer() as Ptr> { - let p: Value> = Rc::new(RefCell::new(p.read().clone())); + let p: Value> = Rc::new(RefCell::new(p.read())); { let _ptr = (*p.borrow()).clone(); _ptr.write(_ptr.read() + 5) }; } 'loop_: for p in v2.as_pointer() as Ptr> { - let p: Value> = Rc::new(RefCell::new(p.read().clone())); + let p: Value> = Rc::new(RefCell::new(p.read())); let __rhs = ((*p.borrow()).read()); (*sum.borrow_mut()) += __rhs; } 'loop_: for mut p in v2.as_pointer() as Ptr> { - let p: Value> = Rc::new(RefCell::new(p.read().clone())); + let p: Value> = Rc::new(RefCell::new(p.read())); { let _ptr = (*p.borrow()).clone(); _ptr.write(_ptr.read() + 5) }; } 'loop_: for mut p in v2.as_pointer() as Ptr> { - let p: Value> = Rc::new(RefCell::new(p.read().clone())); + let p: Value> = Rc::new(RefCell::new(p.read())); let __rhs = ((*p.borrow()).read()); (*sum.borrow_mut()) += __rhs; } diff --git a/tests/unit/out/refcount/iterators.rs b/tests/unit/out/refcount/iterators.rs index 54228911..6ea8c0ce 100644 --- a/tests/unit/out/refcount/iterators.rs +++ b/tests/unit/out/refcount/iterators.rs @@ -30,7 +30,7 @@ fn main_0() -> i32 { (*v.borrow_mut()).push(Ptr::alloc(2)); (*v.borrow_mut()).push(Ptr::alloc(3)); 'loop_: for mut p in v.as_pointer() as Ptr> { - let p: Value> = Rc::new(RefCell::new(p.read().clone())); + let p: Value> = Rc::new(RefCell::new(p.read())); println!("{}", ((*p.borrow()).read())); } return 0; diff --git a/tests/unit/out/refcount/pointers.rs b/tests/unit/out/refcount/pointers.rs index a16dfc19..2fb7aa29 100644 --- a/tests/unit/out/refcount/pointers.rs +++ b/tests/unit/out/refcount/pointers.rs @@ -59,7 +59,7 @@ fn main_0() -> i32 { (*t3.borrow_mut()) = (*t2.borrow()).clone(); (*(*(*t3.borrow()).upgrade().deref()).x.borrow_mut()) = 15; { - let _ptr = ({ TestImpl::as_ptr(&(*t3.borrow())) }).clone(); + let _ptr = ({ TestImpl::as_ptr(&(*t3.borrow())) }); _ptr.write(_ptr.read() + 10) }; assert!( diff --git a/tests/unit/out/refcount/vector3.rs b/tests/unit/out/refcount/vector3.rs index dccd70df..66c71b4b 100644 --- a/tests/unit/out/refcount/vector3.rs +++ b/tests/unit/out/refcount/vector3.rs @@ -63,14 +63,14 @@ fn main_0() -> i32 { 'loop_: for mut v2 in v.as_pointer() as Ptr>> { let v2: Ptr> = v2.upgrade().deref().as_pointer(); 'loop_: for mut i in v2.to_strong().as_pointer() as Ptr { - let i: Value = Rc::new(RefCell::new(i.read().clone())); + let i: Value = Rc::new(RefCell::new(i.read())); println!("{}", ((*i.borrow()) + 3)); } } 'loop_: for mut v2 in v.as_pointer() as Ptr>> { let v2: Value> = Rc::new(RefCell::new(v2.upgrade().deref().borrow().clone())); 'loop_: for mut i in v2.as_pointer() as Ptr { - let i: Value = Rc::new(RefCell::new(i.read().clone())); + let i: Value = Rc::new(RefCell::new(i.read())); println!("{}", (*i.borrow())); } } From 69dcba391e910e8cddf9d0a4d060d5ab9a47c5c2 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Tue, 15 Sep 2026 11:11:39 +0100 Subject: [PATCH 11/15] Delete unnecessary clones from rules --- rules/algorithm/tgt_refcount.rs | 6 +-- rules/arpa_inet/tgt_refcount.rs | 2 +- rules/array/tgt_refcount.rs | 2 +- rules/builtin/tgt_refcount.rs | 2 +- rules/cstdlib/tgt_refcount.rs | 2 +- rules/cstring/tgt_refcount.rs | 46 ++++++++-------- rules/deque/tgt_refcount.rs | 2 +- rules/functional/tgt_refcount.rs | 4 +- rules/ifaddrs/tgt_refcount.rs | 4 +- rules/map/tgt_refcount.rs | 6 +-- rules/netdb/tgt_refcount.rs | 10 ++-- rules/poll/tgt_refcount.rs | 2 +- rules/pwd/tgt_refcount.rs | 6 +-- rules/select/tgt_refcount.rs | 8 +-- rules/socket/tgt_refcount.rs | 2 +- rules/stdio/tgt_refcount.rs | 8 +-- rules/string/tgt_refcount.rs | 8 +-- rules/time/tgt_refcount.rs | 4 +- rules/unistd/tgt_refcount.rs | 2 +- rules/vector/tgt_refcount.rs | 16 +++--- tests/unit/out/refcount/12_test.rs | 3 +- tests/unit/out/refcount/clone_vs_move.rs | 13 +++-- tests/unit/out/refcount/cstring.rs | 42 +++++++-------- tests/unit/out/refcount/errno.rs | 2 +- tests/unit/out/refcount/fcntl.rs | 2 +- tests/unit/out/refcount/fd_io.rs | 2 +- tests/unit/out/refcount/find.rs | 6 +-- .../out/refcount/fn_ptr_stdlib_compare.rs | 8 +-- tests/unit/out/refcount/foreach_map.rs | 2 +- tests/unit/out/refcount/ifaddrs.rs | 2 +- tests/unit/out/refcount/inet_pton_ntop.rs | 6 +-- tests/unit/out/refcount/lseek_ftruncate.rs | 2 +- tests/unit/out/refcount/map-reallocation.rs | 8 +-- tests/unit/out/refcount/map.rs | 50 +++++++++-------- tests/unit/out/refcount/memcpy_overlap.rs | 4 +- .../unit/out/refcount/memcpy_struct_bytes.rs | 4 +- .../unit/out/refcount/memcpy_struct_struct.rs | 4 +- tests/unit/out/refcount/memset.rs | 2 +- tests/unit/out/refcount/netdb.rs | 24 ++++----- tests/unit/out/refcount/new_alloc_array.rs | 2 +- tests/unit/out/refcount/operator_traits.rs | 27 ++++------ tests/unit/out/refcount/pipe_io.rs | 4 +- tests/unit/out/refcount/poll.rs | 4 +- tests/unit/out/refcount/push_emplace_back.rs | 26 ++++----- tests/unit/out/refcount/pwd.rs | 12 ++--- .../refcount/redundant_copy_in_conversion.rs | 2 +- tests/unit/out/refcount/select.rs | 22 ++++---- tests/unit/out/refcount/simple_index.rs | 3 +- .../refcount/split_binop_aliased_borrows.rs | 2 +- tests/unit/out/refcount/stdcopy.rs | 2 +- tests/unit/out/refcount/stdio_nofd.rs | 8 +-- tests/unit/out/refcount/strdup.rs | 4 +- tests/unit/out/refcount/string_h.rs | 54 +++++++++---------- tests/unit/out/refcount/sys_time.rs | 6 +-- .../out/refcount/union_addrof_external.rs | 2 +- .../unit/out/refcount/union_cross_arm_cast.rs | 2 +- .../unit/out/refcount/union_memset_memcpy.rs | 5 +- tests/unit/out/refcount/union_nested.rs | 2 +- .../out/refcount/union_struct_dual_use.rs | 2 +- tests/unit/out/refcount/vector.rs | 6 +-- .../out/refcount/vector_with_allocator.rs | 6 +-- tests/unit/out/unsafe/foreach_double.rs | 6 +-- tests/unit/out/unsafe/map.rs | 6 +-- 63 files changed, 258 insertions(+), 285 deletions(-) diff --git a/rules/algorithm/tgt_refcount.rs b/rules/algorithm/tgt_refcount.rs index 92150fa6..74866b39 100644 --- a/rules/algorithm/tgt_refcount.rs +++ b/rules/algorithm/tgt_refcount.rs @@ -16,7 +16,7 @@ fn f2 + ByteRepr>( a2: Ptr, ) -> Ptr { let count = a1.get_offset() - a0.get_offset(); - let mut outptr = a2.clone(); + let mut outptr = a2; for value in PtrValueIter::new(&a0, count) { outptr.write(value.into()); outptr += 1; @@ -75,8 +75,8 @@ fn f10(a0: Ptr, a1: Ptr) -> Ptr { if count <= 1 { a1 } else { - let mut write_ptr = a0.clone(); let mut iter = PtrValueIter::new(&a0, count); + let mut write_ptr = a0; let mut last_unique = iter.next().unwrap(); // the first unique value is already in place @@ -94,7 +94,7 @@ fn f10(a0: Ptr, a1: Ptr) -> Ptr { } fn f12(a0: Ptr, a1: Ptr, a2: T1) { - let mut __a0 = a0.clone(); + let mut __a0 = a0; while __a0 != a1 { let v = a2.clone(); __a0.write(v); diff --git a/rules/arpa_inet/tgt_refcount.rs b/rules/arpa_inet/tgt_refcount.rs index 90a0e52e..a024f349 100644 --- a/rules/arpa_inet/tgt_refcount.rs +++ b/rules/arpa_inet/tgt_refcount.rs @@ -49,7 +49,7 @@ fn f6(a0: i32, a1: AnyPtr, a2: Ptr, a3: u32) -> Ptr { __sl[..__n].copy_from_slice(__s.as_bytes()); __sl[__n] = 0; }); - a2.clone() + a2 } Some(_) => { libcc2rs::cpp2rust_errno().write(::libc::ENOSPC); diff --git a/rules/array/tgt_refcount.rs b/rules/array/tgt_refcount.rs index 7b1fd496..90b46c20 100644 --- a/rules/array/tgt_refcount.rs +++ b/rules/array/tgt_refcount.rs @@ -18,5 +18,5 @@ fn f5(a0: Ptr>, a1: &mut Vec) { } fn f7(a0: Ptr>, a1: Vec) { - a0.write(a1.clone()) + a0.write(a1) } diff --git a/rules/builtin/tgt_refcount.rs b/rules/builtin/tgt_refcount.rs index d1dd3570..d455c11e 100644 --- a/rules/builtin/tgt_refcount.rs +++ b/rules/builtin/tgt_refcount.rs @@ -26,5 +26,5 @@ fn f13(a0: i64, a1: i64, a2: Ptr) -> bool { fn f14(a0: AnyPtr, a1: AnyPtr, a2: usize) -> AnyPtr { a0.memcpy(&a1, a2 as usize); - a0.clone() + a0 } diff --git a/rules/cstdlib/tgt_refcount.rs b/rules/cstdlib/tgt_refcount.rs index a965f5b0..534d4472 100644 --- a/rules/cstdlib/tgt_refcount.rs +++ b/rules/cstdlib/tgt_refcount.rs @@ -31,7 +31,7 @@ fn f6(a0: Ptr) -> Ptr { } fn f10(a0: Ptr, a1: Ptr) -> Ptr { - let __resolved = a1.clone(); + let __resolved = a1; match ::std::fs::canonicalize(a0.to_rust_string()) { Ok(__p) => { let mut __bytes = __p.into_os_string().into_encoded_bytes(); diff --git a/rules/cstring/tgt_refcount.rs b/rules/cstring/tgt_refcount.rs index f8974181..46e2b60e 100644 --- a/rules/cstring/tgt_refcount.rs +++ b/rules/cstring/tgt_refcount.rs @@ -5,12 +5,12 @@ use libcc2rs::*; fn f1(a0: AnyPtr, a1: AnyPtr, a2: usize) -> AnyPtr { a0.memcpy(&a1, a2 as usize); - a0.clone() + a0 } fn f2(a0: AnyPtr, a1: u8, a2: usize) -> AnyPtr { a0.memset((a1) as u8, a2 as usize); - a0.clone() + a0 } fn f3(a0: AnyPtr, a1: AnyPtr, a2: usize) -> i32 { @@ -19,11 +19,11 @@ fn f3(a0: AnyPtr, a1: AnyPtr, a2: usize) -> i32 { fn f4(a0: AnyPtr, a1: AnyPtr, a2: usize) -> AnyPtr { a0.memcpy(&a1, a2 as usize); - a0.clone() + a0 } fn f5(a0: Ptr, a1: i32) -> Ptr { - let __s = a0.clone(); + let __s = a0; let __t = a1 as u8; match __s.to_c_string_iterator().position(|__c| __c == __t) { Some(__i) => __s.offset(__i), @@ -88,7 +88,7 @@ fn f10(a0: AnyPtr, a1: i32, a2: usize) -> AnyPtr { } fn f11(a0: Ptr, a1: i32) -> Ptr { - let __s = a0.clone(); + let __s = a0; let __t = a1 as u8; match __s .to_c_string_iterator() @@ -108,26 +108,26 @@ fn f11(a0: Ptr, a1: i32) -> Ptr { } fn f15(a0: Ptr) -> Ptr { - libcc2rs::strdup_refcount(a0.clone()) + libcc2rs::strdup_refcount(a0) } fn f16(a0: Ptr, a1: Ptr) -> usize { - let __set = a1.clone(); + let __set = a1; a0.to_c_string_iterator() .take_while(|__c| !__set.to_c_string_iterator().any(|__r| __r == *__c)) .count() } fn f17(a0: Ptr, a1: Ptr) -> usize { - let __set = a1.clone(); + let __set = a1; a0.to_c_string_iterator() .take_while(|__c| __set.to_c_string_iterator().any(|__r| __r == *__c)) .count() } fn f18(a0: Ptr, a1: Ptr) -> Ptr { - let __needle = a1.clone(); - let mut __p = a0.clone(); + let __needle = a1; + let mut __p = a0; loop { let mut __h = __p.to_c_string_iterator(); if __needle @@ -144,8 +144,8 @@ fn f18(a0: Ptr, a1: Ptr) -> Ptr { } fn f21(a0: Ptr, a1: Ptr) -> Ptr { - let __s = a0.clone(); - let __set = a1.clone(); + let __s = a0; + let __set = a1; match __s .to_c_string_iterator() .position(|__c| __set.to_c_string_iterator().any(|__r| __r == __c)) @@ -221,7 +221,7 @@ fn f28(a0: i32, a1: Ptr, a2: usize) -> i32 { } fn f6(a0: Ptr, a1: i32) -> Ptr { - let __s = a0.clone(); + let __s = a0; let __t = a1 as u8; match __s.to_c_string_iterator().position(|__c| __c == __t) { Some(__i) => __s.offset(__i), @@ -251,7 +251,7 @@ fn f12(a0: AnyPtr, a1: i32, a2: usize) -> AnyPtr { } fn f13(a0: Ptr, a1: i32) -> Ptr { - let __s = a0.clone(); + let __s = a0; let __t = a1 as u8; match __s .to_c_string_iterator() @@ -271,7 +271,7 @@ fn f13(a0: Ptr, a1: i32) -> Ptr { } fn f14(a0: Ptr, a1: i32) -> Ptr { - let __s = a0.clone(); + let __s = a0; let __t = a1 as u8; match __s .to_c_string_iterator() @@ -291,8 +291,8 @@ fn f14(a0: Ptr, a1: i32) -> Ptr { } fn f19(a0: Ptr, a1: Ptr) -> Ptr { - let __needle = a1.clone(); - let mut __p = a0.clone(); + let __needle = a1; + let mut __p = a0; loop { let mut __h = __p.to_c_string_iterator(); if __needle @@ -309,8 +309,8 @@ fn f19(a0: Ptr, a1: Ptr) -> Ptr { } fn f20(a0: Ptr, a1: Ptr) -> Ptr { - let __needle = a1.clone(); - let mut __p = a0.clone(); + let __needle = a1; + let mut __p = a0; loop { let mut __h = __p.to_c_string_iterator(); if __needle @@ -327,8 +327,8 @@ fn f20(a0: Ptr, a1: Ptr) -> Ptr { } fn f22(a0: Ptr, a1: Ptr) -> Ptr { - let __s = a0.clone(); - let __set = a1.clone(); + let __s = a0; + let __set = a1; match __s .to_c_string_iterator() .position(|__c| __set.to_c_string_iterator().any(|__r| __r == __c)) @@ -339,8 +339,8 @@ fn f22(a0: Ptr, a1: Ptr) -> Ptr { } fn f23(a0: Ptr, a1: Ptr) -> Ptr { - let __s = a0.clone(); - let __set = a1.clone(); + let __s = a0; + let __set = a1; match __s .to_c_string_iterator() .position(|__c| __set.to_c_string_iterator().any(|__r| __r == __c)) diff --git a/rules/deque/tgt_refcount.rs b/rules/deque/tgt_refcount.rs index d3915854..8e698e53 100644 --- a/rules/deque/tgt_refcount.rs +++ b/rules/deque/tgt_refcount.rs @@ -18,7 +18,7 @@ fn f7(a0: Ptr>>>, a1: Value>) { } fn f10(a0: Ptr>, a1: Vec) { - a0.write(a1.clone()) + a0.write(a1) } fn f11(a0: Ptr>, a1: &mut Vec) { diff --git a/rules/functional/tgt_refcount.rs b/rules/functional/tgt_refcount.rs index 49193bcf..9e809583 100644 --- a/rules/functional/tgt_refcount.rs +++ b/rules/functional/tgt_refcount.rs @@ -12,9 +12,9 @@ fn f1(a0: Ptr) -> Ptr { } fn f2(a0: Ptr) -> Ptr { - a0.clone() + a0 } fn f3(a0: Ptr) -> Ptr { - a0.clone() + a0 } diff --git a/rules/ifaddrs/tgt_refcount.rs b/rules/ifaddrs/tgt_refcount.rs index 90ddb02a..4d47c627 100644 --- a/rules/ifaddrs/tgt_refcount.rs +++ b/rules/ifaddrs/tgt_refcount.rs @@ -8,7 +8,7 @@ fn t1() -> libcc2rs::Ifaddrs { } fn f1(a0: Ptr>) -> i32 { - let __out = a0.clone(); + let __out = a0; match nix::ifaddrs::getifaddrs() { Ok(__ifas) => { let __list: Vec = __ifas.collect(); @@ -29,7 +29,7 @@ fn f1(a0: Ptr>) -> i32 { } fn f2(a0: Ptr) { - let mut __cur = a0.clone(); + let mut __cur = a0; while !__cur.is_null() { let __next = __cur.with(|__i| { let __name = __i.ifa_name.borrow(); diff --git a/rules/map/tgt_refcount.rs b/rules/map/tgt_refcount.rs index c0fba36a..eae9d3d7 100644 --- a/rules/map/tgt_refcount.rs +++ b/rules/map/tgt_refcount.rs @@ -23,7 +23,7 @@ fn f1( a1: T1, ) -> Ptr { a0.with_mut(|__v: &mut BTreeMap>| { - __v.entry(a1.clone()) + __v.entry(a1) .or_insert_with(|| Rc::new(RefCell::new(::default()))) .as_pointer() }) @@ -59,7 +59,7 @@ fn f8( a1: T1, ) -> Ptr { a0.with_mut(|__v: &mut BTreeMap>| { - __v.entry(a1.clone()) + __v.entry(a1) .or_insert_with(|| Rc::new(RefCell::new(::default()))) .as_pointer() }) @@ -123,7 +123,7 @@ fn f17( } fn f19(a0: RefcountMapIter) -> RefcountMapIter { - a0.clone() + a0 } fn f20(a0: RefcountMapIter) -> Value { diff --git a/rules/netdb/tgt_refcount.rs b/rules/netdb/tgt_refcount.rs index 23e3d541..488137fb 100644 --- a/rules/netdb/tgt_refcount.rs +++ b/rules/netdb/tgt_refcount.rs @@ -8,10 +8,10 @@ fn t1() -> libcc2rs::Addrinfo { } fn f1(a0: Ptr, a1: Ptr, a2: Ptr, a3: Ptr>) -> i32 { - let __node = a0.clone(); - let __service = a1.clone(); - let __hints = a2.clone(); - let __out = a3.clone(); + let __node = a0; + let __service = a1; + let __hints = a2; + let __out = a3; let __family = if __hints.is_null() { ::libc::AF_UNSPEC } else { @@ -98,7 +98,7 @@ fn f1(a0: Ptr, a1: Ptr, a2: Ptr, a3: Ptr>) -> i3 } fn f2(a0: Ptr) { - let mut __cur = a0.clone(); + let mut __cur = a0; while !__cur.is_null() { let __next = __cur.with(|__ai| { let __addr = __ai.ai_addr.borrow(); diff --git a/rules/poll/tgt_refcount.rs b/rules/poll/tgt_refcount.rs index f8b37af6..9c47e373 100644 --- a/rules/poll/tgt_refcount.rs +++ b/rules/poll/tgt_refcount.rs @@ -8,7 +8,7 @@ fn t1() -> libcc2rs::Pollfd { } fn f1(a0: Ptr, a1: u64, a2: i32) -> i32 { - let __p = a0.clone(); + let __p = a0; let __timeout = match nix::poll::PollTimeout::try_from(a2) { Ok(__t) => __t, Err(_) => panic!("poll: unsupported timeout {}", a2), diff --git a/rules/pwd/tgt_refcount.rs b/rules/pwd/tgt_refcount.rs index 6f7dde52..ab0700be 100644 --- a/rules/pwd/tgt_refcount.rs +++ b/rules/pwd/tgt_refcount.rs @@ -8,10 +8,10 @@ fn t1() -> libcc2rs::Passwd { } fn f2(a0: u32, a1: Ptr, a2: Ptr, a3: usize, a4: Ptr>) -> i32 { - let __pwbuf = a1.clone(); - let __buf = a2.clone(); + let __pwbuf = a1; + let __buf = a2; let __buflen = a3; - let __out = a4.clone(); + let __out = a4; match nix::unistd::User::from_uid(nix::unistd::Uid::from_raw(a0)) { Ok(Some(__u)) => { let __strs: [Vec; 5] = [ diff --git a/rules/select/tgt_refcount.rs b/rules/select/tgt_refcount.rs index 00317292..2e5c273f 100644 --- a/rules/select/tgt_refcount.rs +++ b/rules/select/tgt_refcount.rs @@ -24,10 +24,10 @@ fn f5(a0: Ptr) { } fn f1(a0: i32, a1: Ptr, a2: Ptr, a3: Ptr, a4: Ptr) -> i32 { - let __rp = a1.clone(); - let __wp = a2.clone(); - let __ep = a3.clone(); - let __tp = a4.clone(); + let __rp = a1; + let __wp = a2; + let __ep = a3; + let __tp = a4; let __r_fds: Vec = match __rp.is_null() { true => Vec::new(), false => __rp.with(|__s| (0..a0).filter(|&__fd| __s.isset(__fd)).collect()), diff --git a/rules/socket/tgt_refcount.rs b/rules/socket/tgt_refcount.rs index b1d1722c..29580032 100644 --- a/rules/socket/tgt_refcount.rs +++ b/rules/socket/tgt_refcount.rs @@ -191,7 +191,7 @@ fn f11(a0: i32, a1: i32, a2: i32, a3: Ptr) -> i32 { }; match nix::sys::socket::socketpair(__family, __ty, __proto, __flags) { Ok((__a, __b)) => { - let __sv = a3.clone(); + let __sv = a3; __sv.write(FdRegistry::register(__a)); __sv.offset(1).write(FdRegistry::register(__b)); 0 diff --git a/rules/stdio/tgt_refcount.rs b/rules/stdio/tgt_refcount.rs index e275508d..57c8a5a8 100644 --- a/rules/stdio/tgt_refcount.rs +++ b/rules/stdio/tgt_refcount.rs @@ -35,7 +35,7 @@ fn f5(a0: AnyPtr, a1: usize, a2: usize, a3: Ptr) -> usize { let __a0 = a0; let __a1 = a1; let __a2 = a2; - let __a3 = a3.clone(); + let __a3 = a3; libcc2rs::fread_refcount(__a0, __a1, __a2, __a3) } @@ -43,7 +43,7 @@ fn f6(a0: AnyPtr, a1: usize, a2: usize, a3: Ptr) -> usize { let __a0 = a0; let __a1 = a1; let __a2 = a2; - let __a3 = a3.clone(); + let __a3 = a3; libcc2rs::fwrite_refcount(__a0, __a1, __a2, __a3) } @@ -101,7 +101,7 @@ fn f16(a0: Ptr) -> i32 { } fn f17(a0: Ptr, a1: i32, a2: Ptr) -> Ptr { - let __buf = a0.clone(); + let __buf = a0; let __n = a1; if __n <= 0 { Ptr::null() @@ -134,7 +134,7 @@ fn f17(a0: Ptr, a1: i32, a2: Ptr) -> Ptr { } fn f18(a0: Ptr, a1: Ptr, a2: Ptr) -> Ptr { - let __stream = a2.clone(); + let __stream = a2; let __old = __stream.with(|__f| __f.fd); match __old { 0..=2 => {} diff --git a/rules/string/tgt_refcount.rs b/rules/string/tgt_refcount.rs index 3550c267..a1bdd611 100644 --- a/rules/string/tgt_refcount.rs +++ b/rules/string/tgt_refcount.rs @@ -21,7 +21,7 @@ fn f1(a0: Vec, a1: usize, a2: usize) -> Vec { } fn f3(a0: Vec, a1: Ptr) -> Vec { - let mut r = a0.clone(); + let mut r = a0; r.pop(); r.extend(a1.to_c_string_iterator()); r.push(0); @@ -92,7 +92,7 @@ fn f16(a0: Vec, a1: Ptr) -> usize { // TODO: This should modify a0 in place fn f17(a0: Vec, a1: Ptr) -> Vec { - let mut __tmp2 = a0.clone(); + let mut __tmp2 = a0; __tmp2.pop(); __tmp2.extend(a1.to_c_string_iterator()); __tmp2.push(0); @@ -176,7 +176,7 @@ fn f25(a0: &mut Vec) { } fn f27(a0: Vec) -> Vec { - a0.clone() + a0 } fn f28(a0: &mut Vec) -> Vec { @@ -184,7 +184,7 @@ fn f28(a0: &mut Vec) -> Vec { } fn f29(a0: Ptr>, a1: Vec) { - a0.write(a1.clone()) + a0.write(a1) } fn f30(a0: Ptr>, a1: &mut Vec) { diff --git a/rules/time/tgt_refcount.rs b/rules/time/tgt_refcount.rs index 8100f0d3..6d6cfae5 100644 --- a/rules/time/tgt_refcount.rs +++ b/rules/time/tgt_refcount.rs @@ -49,7 +49,7 @@ fn f2(a0: nix::time::ClockId, a1: Ptr) -> i32 { } fn f4(a0: Ptr<::libc::time_t>, a1: Ptr) -> Ptr { - let __res = a1.clone(); + let __res = a1; match jiff::Timestamp::from_second(a0.read()) { Ok(__ts) => { let __dt = __ts.to_zoned(jiff::tz::TimeZone::UTC); @@ -64,7 +64,7 @@ fn f4(a0: Ptr<::libc::time_t>, a1: Ptr) -> Ptr { } fn f5(a0: Ptr<::libc::time_t>, a1: Ptr) -> Ptr { - let __res = a1.clone(); + let __res = a1; match jiff::Timestamp::from_second(a0.read()) { Ok(__ts) => { let __dt = __ts.to_zoned(jiff::tz::TimeZone::system()); diff --git a/rules/unistd/tgt_refcount.rs b/rules/unistd/tgt_refcount.rs index c4d5c7ec..faf9608c 100644 --- a/rules/unistd/tgt_refcount.rs +++ b/rules/unistd/tgt_refcount.rs @@ -49,7 +49,7 @@ fn f4(a0: Ptr) -> i32 { fn f5(a0: Ptr) -> i32 { match nix::unistd::pipe() { Ok((__r, __w)) => { - let __fds = a0.clone(); + let __fds = a0; __fds.write(FdRegistry::register(__r)); __fds.offset(1).write(FdRegistry::register(__w)); 0 diff --git a/rules/vector/tgt_refcount.rs b/rules/vector/tgt_refcount.rs index f2c9a615..33d0f3a5 100644 --- a/rules/vector/tgt_refcount.rs +++ b/rules/vector/tgt_refcount.rs @@ -74,11 +74,11 @@ fn f22(a0: Ptr) -> Ptr { } fn f23(a0: Ptr) -> Ptr { - a0.clone() + a0 } fn f24(a0: Ptr) -> Ptr { - a0.clone() + a0 } fn f25(a0: Ptr, a1: usize) -> Ptr { @@ -110,7 +110,7 @@ fn f30(a0: usize) -> Vec>> { } fn f31(a0: Ptr>>>, a1: Vec) { - a0.with_mut(|__v: &mut Vec>>| __v.push(Rc::new(RefCell::new(a1.clone())))) + a0.with_mut(|__v: &mut Vec>>| __v.push(Rc::new(RefCell::new(a1)))) } fn f32(a0: Ptr>>>, a1: usize) { @@ -178,7 +178,7 @@ fn f51(a0: Ptr) -> Ptr { } fn f52(a0: Ptr>>>, a1: Vec) { - a0.with_mut(|__v: &mut Vec>>| __v.push(Rc::new(RefCell::new(a1.clone())))) + a0.with_mut(|__v: &mut Vec>>| __v.push(Rc::new(RefCell::new(a1)))) } fn f53( @@ -209,7 +209,7 @@ fn f57(a0: Ptr) -> Ptr { } fn f58(a0: Ptr>, a1: Vec) { - a0.write(a1.clone()) + a0.write(a1) } fn f60(a0: Ptr>, a1: Ptr) -> Ptr { @@ -259,11 +259,11 @@ fn f81(a0: Ptr) -> Ptr { } fn f82(a0: Ptr) -> Ptr { - a0.clone() + a0 } fn f83(a0: Ptr) -> Ptr { - a0.clone() + a0 } fn f84(a0: Ptr, a1: usize) -> Ptr { @@ -357,5 +357,5 @@ fn f104(a0: Ptr) -> Ptr { } fn f105(a0: Ptr>, a1: Vec) { - a0.write(a1.clone()) + a0.write(a1) } diff --git a/tests/unit/out/refcount/12_test.rs b/tests/unit/out/refcount/12_test.rs index de352b0c..ba1b9e40 100644 --- a/tests/unit/out/refcount/12_test.rs +++ b/tests/unit/out/refcount/12_test.rs @@ -15,8 +15,7 @@ fn main_0() -> i32 { __v.push(Rc::new(RefCell::new( (0..(10_usize) as usize) .map(|_| ::default()) - .collect::>() - .clone(), + .collect::>(), ))) }); return 0; diff --git a/tests/unit/out/refcount/clone_vs_move.rs b/tests/unit/out/refcount/clone_vs_move.rs index 2b8c7c40..c3819e4e 100644 --- a/tests/unit/out/refcount/clone_vs_move.rs +++ b/tests/unit/out/refcount/clone_vs_move.rs @@ -201,8 +201,7 @@ fn main_0() -> i32 { __v.push(Rc::new(RefCell::new( (0..(10_usize) as usize) .map(|_| ::default()) - .collect::>() - .clone(), + .collect::>(), ))) }, ); @@ -335,7 +334,7 @@ fn main_0() -> i32 { let __rhs = (*i.borrow()); (map1.as_pointer() as Ptr>>) .with_mut(|__v: &mut BTreeMap>| { - __v.entry((*i.borrow()).clone()) + __v.entry((*i.borrow())) .or_insert_with(|| Rc::new(RefCell::new(::default()))) .as_pointer() }) @@ -353,7 +352,7 @@ fn main_0() -> i32 { assert!( (((map2.as_pointer() as Ptr>>) .with_mut(|__v: &mut BTreeMap>| { - __v.entry((*i.borrow()).clone()) + __v.entry((*i.borrow())) .or_insert_with(|| Rc::new(RefCell::new(::default()))) .as_pointer() }) @@ -362,7 +361,7 @@ fn main_0() -> i32 { ); (map2.as_pointer() as Ptr>>) .with_mut(|__v: &mut BTreeMap>| { - __v.entry((*i.borrow()).clone()) + __v.entry((*i.borrow())) .or_insert_with(|| Rc::new(RefCell::new(::default()))) .as_pointer() }) @@ -374,7 +373,7 @@ fn main_0() -> i32 { assert!( (((map1.as_pointer() as Ptr>>) .with_mut(|__v: &mut BTreeMap>| { - __v.entry((*i.borrow()).clone()) + __v.entry((*i.borrow())) .or_insert_with(|| Rc::new(RefCell::new(::default()))) .as_pointer() }) @@ -384,7 +383,7 @@ fn main_0() -> i32 { assert!( (((map2.as_pointer() as Ptr>>) .with_mut(|__v: &mut BTreeMap>| { - __v.entry((*i.borrow()).clone()) + __v.entry((*i.borrow())) .or_insert_with(|| Rc::new(RefCell::new(::default()))) .as_pointer() }) diff --git a/tests/unit/out/refcount/cstring.rs b/tests/unit/out/refcount/cstring.rs index 82bb4195..a7612381 100644 --- a/tests/unit/out/refcount/cstring.rs +++ b/tests/unit/out/refcount/cstring.rs @@ -21,7 +21,7 @@ pub fn test_memcpy_0() { &((src.as_pointer() as Ptr) as Ptr).to_any(), 6_usize as usize, ); - ((dst.as_pointer() as Ptr) as Ptr).to_any().clone() + ((dst.as_pointer() as Ptr) as Ptr).to_any() })); assert!({ let _lhs = (*r.borrow()).clone(); @@ -46,7 +46,7 @@ pub fn test_memset_1() { ((buf.as_pointer() as Ptr) as Ptr) .to_any() .memset((('x' as u8) as i32) as u8, 4_usize as usize); - ((buf.as_pointer() as Ptr) as Ptr).to_any().clone() + ((buf.as_pointer() as Ptr) as Ptr).to_any() })); assert!({ let _lhs = (*r.borrow()).clone(); @@ -107,9 +107,7 @@ pub fn test_memmove_3() { &((buf.as_pointer() as Ptr) as Ptr).to_any(), 4_usize as usize, ); - ((buf.as_pointer() as Ptr).offset((1) as isize) as Ptr) - .to_any() - .clone() + ((buf.as_pointer() as Ptr).offset((1) as isize) as Ptr).to_any() })); assert!({ let _lhs = (*r.borrow()).clone(); @@ -533,7 +531,7 @@ pub fn test_strrchr_9() { ]))); assert!( ({ - let __s = (buf.as_pointer() as Ptr).clone(); + let __s = (buf.as_pointer() as Ptr); let __t = (('a' as u8) as i32) as u8; match __s .to_c_string_iterator() @@ -555,7 +553,7 @@ pub fn test_strrchr_9() { } pub fn test_strdup_10() { let d: Value> = Rc::new(RefCell::new(libcc2rs::strdup_refcount( - Ptr::from_string_literal(b"hello").clone(), + Ptr::from_string_literal(b"hello"), ))); assert!(!((*d.borrow()).is_null())); assert!( @@ -604,7 +602,7 @@ pub fn test_strdup_10() { ); libcc2rs::free_refcount(((*d2.borrow()).clone() as Ptr).to_any()); let d3: Value> = Rc::new(RefCell::new(libcc2rs::strdup_refcount( - (buf.as_pointer() as Ptr).clone(), + (buf.as_pointer() as Ptr), ))); assert!(!((*d3.borrow()).is_null())); assert!( @@ -628,7 +626,7 @@ pub fn test_strdup_10() { pub fn test_strcspn_11() { assert!( ({ - let __set = Ptr::from_string_literal(b"el").clone(); + let __set = Ptr::from_string_literal(b"el"); Ptr::from_string_literal(b"hello") .to_c_string_iterator() .take_while(|__c| !__set.to_c_string_iterator().any(|__r| __r == *__c)) @@ -637,7 +635,7 @@ pub fn test_strcspn_11() { ); assert!( ({ - let __set = Ptr::from_string_literal(b"xyz").clone(); + let __set = Ptr::from_string_literal(b"xyz"); Ptr::from_string_literal(b"abc") .to_c_string_iterator() .take_while(|__c| !__set.to_c_string_iterator().any(|__r| __r == *__c)) @@ -646,7 +644,7 @@ pub fn test_strcspn_11() { ); assert!( ({ - let __set = Ptr::from_string_literal(b"abc").clone(); + let __set = Ptr::from_string_literal(b"abc"); Ptr::from_string_literal(b"") .to_c_string_iterator() .take_while(|__c| !__set.to_c_string_iterator().any(|__r| __r == *__c)) @@ -668,7 +666,7 @@ pub fn test_strcspn_11() { pub fn test_strspn_12() { assert!( ({ - let __set = Ptr::from_string_literal(b"hel").clone(); + let __set = Ptr::from_string_literal(b"hel"); Ptr::from_string_literal(b"hello") .to_c_string_iterator() .take_while(|__c| __set.to_c_string_iterator().any(|__r| __r == *__c)) @@ -677,7 +675,7 @@ pub fn test_strspn_12() { ); assert!( ({ - let __set = Ptr::from_string_literal(b"xyz").clone(); + let __set = Ptr::from_string_literal(b"xyz"); Ptr::from_string_literal(b"abc") .to_c_string_iterator() .take_while(|__c| __set.to_c_string_iterator().any(|__r| __r == *__c)) @@ -686,7 +684,7 @@ pub fn test_strspn_12() { ); assert!( ({ - let __set = Ptr::from_string_literal(b"a").clone(); + let __set = Ptr::from_string_literal(b"a"); Ptr::from_string_literal(b"aaa") .to_c_string_iterator() .take_while(|__c| __set.to_c_string_iterator().any(|__r| __r == *__c)) @@ -708,7 +706,7 @@ pub fn test_strspn_12() { pub fn test_strstr_13() { let h: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"hello world"))); let r: Value> = Rc::new(RefCell::new({ - let __needle = Ptr::from_string_literal(b"world").clone(); + let __needle = Ptr::from_string_literal(b"world"); let mut __p = (*h.borrow()).clone(); loop { let mut __h = __p.to_c_string_iterator(); @@ -731,7 +729,7 @@ pub fn test_strstr_13() { }); assert!( ({ - let __needle = Ptr::from_string_literal(b"xyz").clone(); + let __needle = Ptr::from_string_literal(b"xyz"); let mut __p = (*h.borrow()).clone(); loop { let mut __h = __p.to_c_string_iterator(); @@ -759,8 +757,8 @@ pub fn test_strstr_13() { ]))); assert!( ({ - let __needle = Ptr::from_string_literal(b"ll").clone(); - let mut __p = (buf.as_pointer() as Ptr).clone(); + let __needle = Ptr::from_string_literal(b"ll"); + let mut __p = (buf.as_pointer() as Ptr); loop { let mut __h = __p.to_c_string_iterator(); if __needle @@ -781,7 +779,7 @@ pub fn test_strpbrk_14() { let s: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"hello world"))); let r: Value> = Rc::new(RefCell::new({ let __s = (*s.borrow()).clone(); - let __set = Ptr::from_string_literal(b"wo").clone(); + let __set = Ptr::from_string_literal(b"wo"); match __s .to_c_string_iterator() .position(|__c| __set.to_c_string_iterator().any(|__r| __r == __c)) @@ -798,7 +796,7 @@ pub fn test_strpbrk_14() { assert!( ({ let __s = (*s.borrow()).clone(); - let __set = Ptr::from_string_literal(b"xyz").clone(); + let __set = Ptr::from_string_literal(b"xyz"); match __s .to_c_string_iterator() .position(|__c| __set.to_c_string_iterator().any(|__r| __r == __c)) @@ -817,8 +815,8 @@ pub fn test_strpbrk_14() { ]))); assert!( ({ - let __s = (buf.as_pointer() as Ptr).clone(); - let __set = Ptr::from_string_literal(b"b").clone(); + let __s = (buf.as_pointer() as Ptr); + let __set = Ptr::from_string_literal(b"b"); match __s .to_c_string_iterator() .position(|__c| __set.to_c_string_iterator().any(|__r| __r == __c)) diff --git a/tests/unit/out/refcount/errno.rs b/tests/unit/out/refcount/errno.rs index 2cf9c8bc..065a2f4d 100644 --- a/tests/unit/out/refcount/errno.rs +++ b/tests/unit/out/refcount/errno.rs @@ -18,7 +18,7 @@ pub fn test_errno_0() { pub fn test_errno_preserved_across_strdup_1() { libcc2rs::cpp2rust_errno().write(99); let d: Value> = Rc::new(RefCell::new(libcc2rs::strdup_refcount( - Ptr::from_string_literal(b"hello").clone(), + Ptr::from_string_literal(b"hello"), ))); assert!((((!((*d.borrow()).is_null())) as i32) != 0)); assert!(((((libcc2rs::cpp2rust_errno().read()) == 99) as i32) != 0)); diff --git a/tests/unit/out/refcount/fcntl.rs b/tests/unit/out/refcount/fcntl.rs index 7b8bf8ad..b88f7f24 100644 --- a/tests/unit/out/refcount/fcntl.rs +++ b/tests/unit/out/refcount/fcntl.rs @@ -16,7 +16,7 @@ fn main_0() -> i32 { assert!( (((match nix::unistd::pipe() { Ok((__r, __w)) => { - let __fds = (fds.as_pointer() as Ptr).clone(); + let __fds = (fds.as_pointer() as Ptr); __fds.write(FdRegistry::register(__r)); __fds.offset(1).write(FdRegistry::register(__w)); 0 diff --git a/tests/unit/out/refcount/fd_io.rs b/tests/unit/out/refcount/fd_io.rs index f1272772..2cc68180 100644 --- a/tests/unit/out/refcount/fd_io.rs +++ b/tests/unit/out/refcount/fd_io.rs @@ -74,7 +74,7 @@ fn main_0() -> i32 { ((buf.as_pointer() as Ptr) as Ptr) .to_any() .memset((0) as u8, ::std::mem::size_of::<[u8; 16]>() as usize); - ((buf.as_pointer() as Ptr) as Ptr).to_any().clone() + ((buf.as_pointer() as Ptr) as Ptr).to_any() }; assert!( (((match FdRegistry::with_fd((*fd.borrow()), |__fd| { diff --git a/tests/unit/out/refcount/find.rs b/tests/unit/out/refcount/find.rs index b2c8507c..c102d736 100644 --- a/tests/unit/out/refcount/find.rs +++ b/tests/unit/out/refcount/find.rs @@ -35,21 +35,21 @@ fn main_0() -> i32 { let m: Value>> = Rc::new(RefCell::new(BTreeMap::new())); (m.as_pointer() as Ptr>>) .with_mut(|__v: &mut BTreeMap>| { - __v.entry(1.clone()) + __v.entry(1) .or_insert_with(|| Rc::new(RefCell::new(::default()))) .as_pointer() }) .write(1_f64); (m.as_pointer() as Ptr>>) .with_mut(|__v: &mut BTreeMap>| { - __v.entry(2.clone()) + __v.entry(2) .or_insert_with(|| Rc::new(RefCell::new(::default()))) .as_pointer() }) .write(2_f64); (m.as_pointer() as Ptr>>) .with_mut(|__v: &mut BTreeMap>| { - __v.entry(3.clone()) + __v.entry(3) .or_insert_with(|| Rc::new(RefCell::new(::default()))) .as_pointer() }) diff --git a/tests/unit/out/refcount/fn_ptr_stdlib_compare.rs b/tests/unit/out/refcount/fn_ptr_stdlib_compare.rs index b809e187..dc405232 100644 --- a/tests/unit/out/refcount/fn_ptr_stdlib_compare.rs +++ b/tests/unit/out/refcount/fn_ptr_stdlib_compare.rs @@ -81,7 +81,7 @@ fn main_0() -> i32 { (('X' as u8) as i32) as u8, ::std::mem::size_of::<[u8; 16]>() as usize, ); - ((buf.as_pointer() as Ptr) as Ptr).to_any().clone() + ((buf.as_pointer() as Ptr) as Ptr).to_any() }; let n: Value = Rc::new(RefCell::new({ let __a0 = ((buf.as_pointer() as Ptr) as Ptr).to_any(); @@ -128,7 +128,7 @@ fn main_0() -> i32 { (('X' as u8) as i32) as u8, ::std::mem::size_of::<[u8; 16]>() as usize, ); - ((buf.as_pointer() as Ptr) as Ptr).to_any().clone() + ((buf.as_pointer() as Ptr) as Ptr).to_any() }; let n: Value = Rc::new(RefCell::new( ({ @@ -213,7 +213,7 @@ fn main_0() -> i32 { (('Y' as u8) as i32) as u8, ::std::mem::size_of::<[u8; 10]>() as usize, ); - ((buf.as_pointer() as Ptr) as Ptr).to_any().clone() + ((buf.as_pointer() as Ptr) as Ptr).to_any() }; let n: Value = Rc::new(RefCell::new({ let __a0 = ((buf.as_pointer() as Ptr) as Ptr).to_any(); @@ -250,7 +250,7 @@ fn main_0() -> i32 { (('Y' as u8) as i32) as u8, ::std::mem::size_of::<[u8; 10]>() as usize, ); - ((buf.as_pointer() as Ptr) as Ptr).to_any().clone() + ((buf.as_pointer() as Ptr) as Ptr).to_any() }; let n: Value = Rc::new(RefCell::new( ({ diff --git a/tests/unit/out/refcount/foreach_map.rs b/tests/unit/out/refcount/foreach_map.rs index 2fd93260..042cce5c 100644 --- a/tests/unit/out/refcount/foreach_map.rs +++ b/tests/unit/out/refcount/foreach_map.rs @@ -16,7 +16,7 @@ fn main_0() -> i32 { 'loop_: while ((*i.borrow()) < 100) { (m.as_pointer() as Ptr>>) .with_mut(|__v: &mut BTreeMap>| { - __v.entry((*i.borrow()).clone()) + __v.entry((*i.borrow())) .or_insert_with(|| Rc::new(RefCell::new(::default()))) .as_pointer() }) diff --git a/tests/unit/out/refcount/ifaddrs.rs b/tests/unit/out/refcount/ifaddrs.rs index 20dc87a9..bc431247 100644 --- a/tests/unit/out/refcount/ifaddrs.rs +++ b/tests/unit/out/refcount/ifaddrs.rs @@ -14,7 +14,7 @@ fn main_0() -> i32 { Rc::new(RefCell::new(Ptr::::null())); assert!( ((({ - let __out = (list.as_pointer()).clone(); + let __out = (list.as_pointer()); match nix::ifaddrs::getifaddrs() { Ok(__ifas) => { let __list: Vec = __ifas.collect(); diff --git a/tests/unit/out/refcount/inet_pton_ntop.rs b/tests/unit/out/refcount/inet_pton_ntop.rs index 2394ba92..cdb55376 100644 --- a/tests/unit/out/refcount/inet_pton_ntop.rs +++ b/tests/unit/out/refcount/inet_pton_ntop.rs @@ -241,7 +241,7 @@ fn main_0() -> i32 { __sl[..__n].copy_from_slice(__s.as_bytes()); __sl[__n] = 0; }); - (text.as_pointer() as Ptr).clone() + (text.as_pointer() as Ptr) } Some(_) => { libcc2rs::cpp2rust_errno().write(::libc::ENOSPC); @@ -314,7 +314,7 @@ fn main_0() -> i32 { __sl[..__n].copy_from_slice(__s.as_bytes()); __sl[__n] = 0; }); - (text.as_pointer() as Ptr).clone() + (text.as_pointer() as Ptr) } Some(_) => { libcc2rs::cpp2rust_errno().write(::libc::ENOSPC); @@ -365,7 +365,7 @@ fn main_0() -> i32 { __sl[..__n].copy_from_slice(__s.as_bytes()); __sl[__n] = 0; }); - (text.as_pointer() as Ptr).clone() + (text.as_pointer() as Ptr) } Some(_) => { libcc2rs::cpp2rust_errno().write(::libc::ENOSPC); diff --git a/tests/unit/out/refcount/lseek_ftruncate.rs b/tests/unit/out/refcount/lseek_ftruncate.rs index 5da75c3c..2547c1c1 100644 --- a/tests/unit/out/refcount/lseek_ftruncate.rs +++ b/tests/unit/out/refcount/lseek_ftruncate.rs @@ -95,7 +95,7 @@ fn main_0() -> i32 { ((buf.as_pointer() as Ptr) as Ptr) .to_any() .memset((0) as u8, ::std::mem::size_of::<[u8; 16]>() as usize); - ((buf.as_pointer() as Ptr) as Ptr).to_any().clone() + ((buf.as_pointer() as Ptr) as Ptr).to_any() }; assert!( (((match FdRegistry::with_fd((*fd.borrow()), |__fd| { diff --git a/tests/unit/out/refcount/map-reallocation.rs b/tests/unit/out/refcount/map-reallocation.rs index 72572fd3..b2649021 100644 --- a/tests/unit/out/refcount/map-reallocation.rs +++ b/tests/unit/out/refcount/map-reallocation.rs @@ -16,7 +16,7 @@ fn main_0() -> i32 { let __rhs = (*sentinel.borrow()); (m.as_pointer() as Ptr>>) .with_mut(|__v: &mut BTreeMap>| { - __v.entry((*sentinel.borrow()).clone()) + __v.entry((*sentinel.borrow())) .or_insert_with(|| Rc::new(RefCell::new(::default()))) .as_pointer() }) @@ -43,7 +43,7 @@ fn main_0() -> i32 { let __rhs = (*i.borrow()); (m.as_pointer() as Ptr>>) .with_mut(|__v: &mut BTreeMap>| { - __v.entry((*i.borrow()).clone()) + __v.entry((*i.borrow())) .or_insert_with(|| Rc::new(RefCell::new(::default()))) .as_pointer() }) @@ -55,7 +55,7 @@ fn main_0() -> i32 { let __rhs = (*i.borrow()); (m.as_pointer() as Ptr>>) .with_mut(|__v: &mut BTreeMap>| { - __v.entry((*i.borrow()).clone()) + __v.entry((*i.borrow())) .or_insert_with(|| Rc::new(RefCell::new(::default()))) .as_pointer() }) @@ -85,7 +85,7 @@ fn main_0() -> i32 { assert!( (((m.as_pointer() as Ptr>>) .with_mut(|__v: &mut BTreeMap>| { - __v.entry((*sentinel.borrow()).clone()) + __v.entry((*sentinel.borrow())) .or_insert_with(|| Rc::new(RefCell::new(::default()))) .as_pointer() }) diff --git a/tests/unit/out/refcount/map.rs b/tests/unit/out/refcount/map.rs index 77707d78..e942240e 100644 --- a/tests/unit/out/refcount/map.rs +++ b/tests/unit/out/refcount/map.rs @@ -22,21 +22,21 @@ fn main_0() -> i32 { let m: Value>> = Rc::new(RefCell::new(BTreeMap::new())); (m.as_pointer() as Ptr>>) .with_mut(|__v: &mut BTreeMap>| { - __v.entry(0_i16.clone()) + __v.entry(0_i16) .or_insert_with(|| Rc::new(RefCell::new(::default()))) .as_pointer() }) .write(1_u32); (m.as_pointer() as Ptr>>) .with_mut(|__v: &mut BTreeMap>| { - __v.entry(1_i16.clone()) + __v.entry(1_i16) .or_insert_with(|| Rc::new(RefCell::new(::default()))) .as_pointer() }) .write(2_u32); (m.as_pointer() as Ptr>>) .with_mut(|__v: &mut BTreeMap>| { - __v.entry(2_i16.clone()) + __v.entry(2_i16) .or_insert_with(|| Rc::new(RefCell::new(::default()))) .as_pointer() }) @@ -45,7 +45,7 @@ fn main_0() -> i32 { assert!( (((m.as_pointer() as Ptr>>) .with_mut(|__v: &mut BTreeMap>| { - __v.entry(0_i16.clone()) + __v.entry(0_i16) .or_insert_with(|| Rc::new(RefCell::new(::default()))) .as_pointer() }) @@ -55,7 +55,7 @@ fn main_0() -> i32 { assert!( (((m.as_pointer() as Ptr>>) .with_mut(|__v: &mut BTreeMap>| { - __v.entry(1_i16.clone()) + __v.entry(1_i16) .or_insert_with(|| Rc::new(RefCell::new(::default()))) .as_pointer() }) @@ -65,7 +65,7 @@ fn main_0() -> i32 { assert!( (((m.as_pointer() as Ptr>>) .with_mut(|__v: &mut BTreeMap>| { - __v.entry(2_i16.clone()) + __v.entry(2_i16) .or_insert_with(|| Rc::new(RefCell::new(::default()))) .as_pointer() }) @@ -75,7 +75,7 @@ fn main_0() -> i32 { let x: Value = Rc::new(RefCell::new(4)); (m.as_pointer() as Ptr>>) .with_mut(|__v: &mut BTreeMap>| { - __v.entry(1_i16.clone()) + __v.entry(1_i16) .or_insert_with(|| Rc::new(RefCell::new(::default()))) .as_pointer() }) @@ -84,7 +84,7 @@ fn main_0() -> i32 { assert!( (((m.as_pointer() as Ptr>>) .with_mut(|__v: &mut BTreeMap>| { - __v.entry(0_i16.clone()) + __v.entry(0_i16) .or_insert_with(|| Rc::new(RefCell::new(::default()))) .as_pointer() }) @@ -94,7 +94,7 @@ fn main_0() -> i32 { assert!( (((m.as_pointer() as Ptr>>) .with_mut(|__v: &mut BTreeMap>| { - __v.entry(1_i16.clone()) + __v.entry(1_i16) .or_insert_with(|| Rc::new(RefCell::new(::default()))) .as_pointer() }) @@ -104,7 +104,7 @@ fn main_0() -> i32 { assert!( (((m.as_pointer() as Ptr>>) .with_mut(|__v: &mut BTreeMap>| { - __v.entry(2_i16.clone()) + __v.entry(2_i16) .or_insert_with(|| Rc::new(RefCell::new(::default()))) .as_pointer() }) @@ -115,7 +115,7 @@ fn main_0() -> i32 { foo_0( ((m.as_pointer() as Ptr>>) .with_mut(|__v: &mut BTreeMap>| { - __v.entry(0_i16.clone()) + __v.entry(0_i16) .or_insert_with(|| Rc::new(RefCell::new(::default()))) .as_pointer() }) @@ -125,7 +125,7 @@ fn main_0() -> i32 { assert!( (((m.as_pointer() as Ptr>>) .with_mut(|__v: &mut BTreeMap>| { - __v.entry(0_i16.clone()) + __v.entry(0_i16) .or_insert_with(|| Rc::new(RefCell::new(::default()))) .as_pointer() }) @@ -135,7 +135,7 @@ fn main_0() -> i32 { ({ bar_1((m.as_pointer() as Ptr>>).with_mut( |__v: &mut BTreeMap>| { - __v.entry(2_i16.clone()) + __v.entry(2_i16) .or_insert_with(|| Rc::new(RefCell::new(::default()))) .as_pointer() }, @@ -144,7 +144,7 @@ fn main_0() -> i32 { assert!( (((m.as_pointer() as Ptr>>) .with_mut(|__v: &mut BTreeMap>| { - __v.entry(2_i16.clone()) + __v.entry(2_i16) .or_insert_with(|| Rc::new(RefCell::new(::default()))) .as_pointer() }) @@ -153,7 +153,7 @@ fn main_0() -> i32 { ); let __rhs = ((m.as_pointer() as Ptr>>) .with_mut(|__v: &mut BTreeMap>| { - __v.entry(0_i16.clone()) + __v.entry(0_i16) .or_insert_with(|| Rc::new(RefCell::new(::default()))) .as_pointer() }) @@ -161,7 +161,7 @@ fn main_0() -> i32 { .wrapping_add( ((m.as_pointer() as Ptr>>) .with_mut(|__v: &mut BTreeMap>| { - __v.entry(2_i16.clone()) + __v.entry(2_i16) .or_insert_with(|| Rc::new(RefCell::new(::default()))) .as_pointer() }) @@ -169,7 +169,7 @@ fn main_0() -> i32 { ); (m.as_pointer() as Ptr>>) .with_mut(|__v: &mut BTreeMap>| { - __v.entry(0_i16.clone()) + __v.entry(0_i16) .or_insert_with(|| Rc::new(RefCell::new(::default()))) .as_pointer() }) @@ -177,7 +177,7 @@ fn main_0() -> i32 { assert!( (((m.as_pointer() as Ptr>>) .with_mut(|__v: &mut BTreeMap>| { - __v.entry(0_i16.clone()) + __v.entry(0_i16) .or_insert_with(|| Rc::new(RefCell::new(::default()))) .as_pointer() }) @@ -192,8 +192,7 @@ fn main_0() -> i32 { &1_i16, ))); let const_it: Value> = Rc::new(RefCell::new( - RefcountMapIter::find_key((m.as_pointer() as Ptr>>), &10_i16) - .clone(), + RefcountMapIter::find_key((m.as_pointer() as Ptr>>), &10_i16), )); let x1: Value = Rc::new(RefCell::new(if (*it.borrow()) == (*end.borrow()) { 0_u32 @@ -221,7 +220,7 @@ fn main_0() -> i32 { assert!(((*x3.borrow()) == 4_u32)); let x4: Value = Rc::new(RefCell::new( if (*const_it.borrow()) - == RefcountMapIter::end((m.as_pointer() as Ptr>>)).clone() + == RefcountMapIter::end((m.as_pointer() as Ptr>>)) { 0_u32 } else { @@ -231,7 +230,7 @@ fn main_0() -> i32 { assert!(((*x4.borrow()) == 0_u32)); (m.as_pointer() as Ptr>>) .with_mut(|__v: &mut BTreeMap>| { - __v.entry(4_i16.clone()) + __v.entry(4_i16) .or_insert_with(|| Rc::new(RefCell::new(::default()))) .as_pointer() }) @@ -245,7 +244,7 @@ fn main_0() -> i32 { assert!( (((m.as_pointer() as Ptr>>) .with_mut(|__v: &mut BTreeMap>| { - __v.entry(4_i16.clone()) + __v.entry(4_i16) .or_insert_with(|| Rc::new(RefCell::new(::default()))) .as_pointer() }) @@ -259,7 +258,7 @@ fn main_0() -> i32 { assert!( (((m.as_pointer() as Ptr>>) .with_mut(|__v: &mut BTreeMap>| { - __v.entry(4_i16.clone()) + __v.entry(4_i16) .or_insert_with(|| Rc::new(RefCell::new(::default()))) .as_pointer() }) @@ -372,8 +371,7 @@ fn main_0() -> i32 { __v.entry( ((indexes.as_pointer() as Ptr) .offset(((*i.borrow()) as usize)) - .read()) - .clone(), + .read()), ) .or_insert_with(|| Rc::new(RefCell::new(::default()))) .as_pointer() diff --git a/tests/unit/out/refcount/memcpy_overlap.rs b/tests/unit/out/refcount/memcpy_overlap.rs index 821853c5..e10a5708 100644 --- a/tests/unit/out/refcount/memcpy_overlap.rs +++ b/tests/unit/out/refcount/memcpy_overlap.rs @@ -19,9 +19,7 @@ fn main_0() -> i32 { &((buf.as_pointer() as Ptr) as Ptr).to_any(), 4_usize as usize, ); - ((buf.as_pointer() as Ptr).offset((2) as isize) as Ptr) - .to_any() - .clone() + ((buf.as_pointer() as Ptr).offset((2) as isize) as Ptr).to_any() }; assert!((((*buf.borrow())[(0) as usize] as i32) == 1)); assert!((((*buf.borrow())[(1) as usize] as i32) == 2)); diff --git a/tests/unit/out/refcount/memcpy_struct_bytes.rs b/tests/unit/out/refcount/memcpy_struct_bytes.rs index e141580f..43030c7b 100644 --- a/tests/unit/out/refcount/memcpy_struct_bytes.rs +++ b/tests/unit/out/refcount/memcpy_struct_bytes.rs @@ -50,7 +50,7 @@ fn main_0() -> i32 { &((src.as_pointer()) as Ptr).to_any(), ::std::mem::size_of::<[u8; 8]>() as usize, ); - ((buf.as_pointer() as Ptr) as Ptr).to_any().clone() + ((buf.as_pointer() as Ptr) as Ptr).to_any() }; let dst: Value = >::default(); { @@ -58,7 +58,7 @@ fn main_0() -> i32 { &((buf.as_pointer() as Ptr) as Ptr).to_any(), 8usize as usize, ); - ((dst.as_pointer()) as Ptr).to_any().clone() + ((dst.as_pointer()) as Ptr).to_any() }; assert!(((((*(*dst.borrow()).x.borrow()) == 3) as i32) != 0)); assert!(((((*(*dst.borrow()).y.borrow()) == 7) as i32) != 0)); diff --git a/tests/unit/out/refcount/memcpy_struct_struct.rs b/tests/unit/out/refcount/memcpy_struct_struct.rs index 84575f4d..f5785cf2 100644 --- a/tests/unit/out/refcount/memcpy_struct_struct.rs +++ b/tests/unit/out/refcount/memcpy_struct_struct.rs @@ -82,9 +82,7 @@ fn main_0() -> i32 { &(((table.as_pointer() as Ptr).offset(0)) as Ptr).to_any(), (((*table_size.borrow()) as u64).wrapping_mul((4usize as u64)) as usize) as usize, ); - (((table.as_pointer() as Ptr).offset((*table_size.borrow()))) as Ptr) - .to_any() - .clone() + (((table.as_pointer() as Ptr).offset((*table_size.borrow()))) as Ptr).to_any() }; assert!( (((*(*table.borrow())[(4) as usize].bits.borrow()) as i32) == 1) diff --git a/tests/unit/out/refcount/memset.rs b/tests/unit/out/refcount/memset.rs index b36cf8b3..f402c774 100644 --- a/tests/unit/out/refcount/memset.rs +++ b/tests/unit/out/refcount/memset.rs @@ -21,7 +21,7 @@ fn main_0() -> i32 { (1) as u8, (::std::mem::size_of::() as usize).wrapping_mul(((*N.borrow()) as usize)) as usize, ); - ((*arr.borrow()).clone() as Ptr).to_any().clone() + ((*arr.borrow()).clone() as Ptr).to_any() }; let sum: Value = Rc::new(RefCell::new(0)); let i: Value = Rc::new(RefCell::new(0)); diff --git a/tests/unit/out/refcount/netdb.rs b/tests/unit/out/refcount/netdb.rs index 8aea22f4..c69604b9 100644 --- a/tests/unit/out/refcount/netdb.rs +++ b/tests/unit/out/refcount/netdb.rs @@ -20,10 +20,10 @@ pub fn test_ipv4_literal_0() { Rc::new(RefCell::new(Ptr::::null())); assert!( ((({ - let __node = Ptr::from_string_literal(b"127.0.0.1").clone(); - let __service = Ptr::from_string_literal(b"8080").clone(); - let __hints = (hints.as_pointer()).clone(); - let __out = (res.as_pointer()).clone(); + let __node = Ptr::from_string_literal(b"127.0.0.1"); + let __service = Ptr::from_string_literal(b"8080"); + let __hints = (hints.as_pointer()); + let __out = (res.as_pointer()); let __family = if __hints.is_null() { ::libc::AF_UNSPEC } else { @@ -191,10 +191,10 @@ pub fn test_ipv6_literal_1() { Rc::new(RefCell::new(Ptr::::null())); assert!( ((({ - let __node = Ptr::from_string_literal(b"::1").clone(); - let __service = Ptr::from_string_literal(b"443").clone(); - let __hints = (hints.as_pointer()).clone(); - let __out = (res.as_pointer()).clone(); + let __node = Ptr::from_string_literal(b"::1"); + let __service = Ptr::from_string_literal(b"443"); + let __hints = (hints.as_pointer()); + let __out = (res.as_pointer()); let __family = if __hints.is_null() { ::libc::AF_UNSPEC } else { @@ -352,10 +352,10 @@ pub fn test_null_hints_2() { Rc::new(RefCell::new(Ptr::::null())); assert!( ((({ - let __node = Ptr::from_string_literal(b"127.0.0.1").clone(); - let __service = Ptr::from_string_literal(b"80").clone(); - let __hints = Ptr::::null().clone(); - let __out = (res.as_pointer()).clone(); + let __node = Ptr::from_string_literal(b"127.0.0.1"); + let __service = Ptr::from_string_literal(b"80"); + let __hints = Ptr::::null(); + let __out = (res.as_pointer()); let __family = if __hints.is_null() { ::libc::AF_UNSPEC } else { diff --git a/tests/unit/out/refcount/new_alloc_array.rs b/tests/unit/out/refcount/new_alloc_array.rs index 9c3a8549..c4e12b23 100644 --- a/tests/unit/out/refcount/new_alloc_array.rs +++ b/tests/unit/out/refcount/new_alloc_array.rs @@ -20,7 +20,7 @@ fn main_0() -> i32 { (0) as u8, (::std::mem::size_of::() as usize).wrapping_mul(100_usize) as usize, ); - ((*array.borrow()).clone() as Ptr).to_any().clone() + ((*array.borrow()).clone() as Ptr).to_any() }; (*array.borrow()).offset((99) as isize).write(-1_i32); let p1: Value> = Rc::new(RefCell::new((*array.borrow()).clone())); diff --git a/tests/unit/out/refcount/operator_traits.rs b/tests/unit/out/refcount/operator_traits.rs index 4ac3a136..e4c54beb 100644 --- a/tests/unit/out/refcount/operator_traits.rs +++ b/tests/unit/out/refcount/operator_traits.rs @@ -444,24 +444,18 @@ fn main_0() -> i32 { let m: Value>> = Rc::new(RefCell::new(BTreeMap::new())); (m.as_pointer() as Ptr>>) .with_mut(|__v: &mut BTreeMap>| { - __v.entry( - Lt { - v: Rc::new(RefCell::new(2)), - } - .clone(), - ) + __v.entry(Lt { + v: Rc::new(RefCell::new(2)), + }) .or_insert_with(|| Rc::new(RefCell::new(::default()))) .as_pointer() }) .write(20); (m.as_pointer() as Ptr>>) .with_mut(|__v: &mut BTreeMap>| { - __v.entry( - Lt { - v: Rc::new(RefCell::new(1)), - } - .clone(), - ) + __v.entry(Lt { + v: Rc::new(RefCell::new(1)), + }) .or_insert_with(|| Rc::new(RefCell::new(::default()))) .as_pointer() }) @@ -475,12 +469,9 @@ fn main_0() -> i32 { assert!( (((m.as_pointer() as Ptr>>) .with_mut(|__v: &mut BTreeMap>| { - __v.entry( - Lt { - v: Rc::new(RefCell::new(2)), - } - .clone(), - ) + __v.entry(Lt { + v: Rc::new(RefCell::new(2)), + }) .or_insert_with(|| Rc::new(RefCell::new(::default()))) .as_pointer() }) diff --git a/tests/unit/out/refcount/pipe_io.rs b/tests/unit/out/refcount/pipe_io.rs index 5f51cf01..b46063fb 100644 --- a/tests/unit/out/refcount/pipe_io.rs +++ b/tests/unit/out/refcount/pipe_io.rs @@ -16,7 +16,7 @@ fn main_0() -> i32 { assert!( (((match nix::unistd::pipe() { Ok((__r, __w)) => { - let __fds = (fds.as_pointer() as Ptr).clone(); + let __fds = (fds.as_pointer() as Ptr); __fds.write(FdRegistry::register(__r)); __fds.offset(1).write(FdRegistry::register(__w)); 0 @@ -50,7 +50,7 @@ fn main_0() -> i32 { ((buf.as_pointer() as Ptr) as Ptr) .to_any() .memset((0) as u8, ::std::mem::size_of::<[u8; 4]>() as usize); - ((buf.as_pointer() as Ptr) as Ptr).to_any().clone() + ((buf.as_pointer() as Ptr) as Ptr).to_any() }; assert!( (((match FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { diff --git a/tests/unit/out/refcount/poll.rs b/tests/unit/out/refcount/poll.rs index f996dc13..acf0a506 100644 --- a/tests/unit/out/refcount/poll.rs +++ b/tests/unit/out/refcount/poll.rs @@ -16,7 +16,7 @@ fn main_0() -> i32 { assert!( (((match nix::unistd::pipe() { Ok((__r, __w)) => { - let __fds = (fds.as_pointer() as Ptr).clone(); + let __fds = (fds.as_pointer() as Ptr); __fds.write(FdRegistry::register(__r)); __fds.offset(1).write(FdRegistry::register(__w)); 0 @@ -56,7 +56,7 @@ fn main_0() -> i32 { (*(*pfd.borrow())[(1) as usize].revents.borrow_mut()) = 42_i16; assert!( ((({ - let __p = (pfd.as_pointer() as Ptr).clone(); + let __p = (pfd.as_pointer() as Ptr); let __timeout = match nix::poll::PollTimeout::try_from(0) { Ok(__t) => __t, Err(_) => panic!("poll: unsupported timeout {}", 0), diff --git a/tests/unit/out/refcount/push_emplace_back.rs b/tests/unit/out/refcount/push_emplace_back.rs index e547adac..2fc46195 100644 --- a/tests/unit/out/refcount/push_emplace_back.rs +++ b/tests/unit/out/refcount/push_emplace_back.rs @@ -106,9 +106,8 @@ impl ByteRepr for JPEGData { } pub fn push_param_0(dest: Ptr>>>) { let dest: Value>>>> = Rc::new(RefCell::new(dest)); - ((*dest.borrow()).to_strong().as_pointer() as Ptr>>>).with_mut( - |__v: &mut Vec>>| __v.push(Rc::new(RefCell::new(Vec::new().clone()))), - ); + ((*dest.borrow()).to_strong().as_pointer() as Ptr>>>) + .with_mut(|__v: &mut Vec>>| __v.push(Rc::new(RefCell::new(Vec::new())))); } pub fn push_local_from_field_1(jpg: Ptr, cond: bool) { let jpg: Value> = Rc::new(RefCell::new(jpg)); @@ -123,18 +122,15 @@ pub fn push_local_from_field_1(jpg: Ptr, cond: bool) { } ((*dest.borrow()).to_strong().as_pointer() as Ptr>>>).with_mut( |__v: &mut Vec>>| { - __v.push(Rc::new(RefCell::new( - { - let __count = (head.as_pointer() as Ptr) - .offset((3) as isize) - .get_offset() - - (head.as_pointer() as Ptr).get_offset(); - PtrValueIter::new(&(head.as_pointer() as Ptr), __count) - .map(|item| u8::try_from(item).ok().unwrap()) - .collect::>() - } - .clone(), - ))) + __v.push(Rc::new(RefCell::new({ + let __count = (head.as_pointer() as Ptr) + .offset((3) as isize) + .get_offset() + - (head.as_pointer() as Ptr).get_offset(); + PtrValueIter::new(&(head.as_pointer() as Ptr), __count) + .map(|item| u8::try_from(item).ok().unwrap()) + .collect::>() + }))) }, ); } diff --git a/tests/unit/out/refcount/pwd.rs b/tests/unit/out/refcount/pwd.rs index 7cd84a26..38af3b89 100644 --- a/tests/unit/out/refcount/pwd.rs +++ b/tests/unit/out/refcount/pwd.rs @@ -64,10 +64,10 @@ pub fn test_getpwuid_r_2() { Rc::new(RefCell::new(Ptr::::null())); assert!( ((({ - let __pwbuf = (pw.as_pointer()).clone(); - let __buf = (buf.as_pointer() as Ptr).clone(); + let __pwbuf = (pw.as_pointer()); + let __buf = (buf.as_pointer() as Ptr); let __buflen = ::std::mem::size_of::<[u8; 4096]>(); - let __out = (result.as_pointer()).clone(); + let __out = (result.as_pointer()); match nix::unistd::User::from_uid(nix::unistd::Uid::from_raw( nix::unistd::geteuid().as_raw(), )) { @@ -169,10 +169,10 @@ pub fn test_getpwuid_r_erange_3() { Rc::new(RefCell::new(Ptr::::null())); assert!( ((({ - let __pwbuf = (pw.as_pointer()).clone(); - let __buf = (tiny.as_pointer() as Ptr).clone(); + let __pwbuf = (pw.as_pointer()); + let __buf = (tiny.as_pointer() as Ptr); let __buflen = ::std::mem::size_of::<[u8; 1]>(); - let __out = (result.as_pointer()).clone(); + let __out = (result.as_pointer()); match nix::unistd::User::from_uid(nix::unistd::Uid::from_raw( nix::unistd::geteuid().as_raw(), )) { diff --git a/tests/unit/out/refcount/redundant_copy_in_conversion.rs b/tests/unit/out/refcount/redundant_copy_in_conversion.rs index 28c591b4..2e6a6c9d 100644 --- a/tests/unit/out/refcount/redundant_copy_in_conversion.rs +++ b/tests/unit/out/refcount/redundant_copy_in_conversion.rs @@ -22,7 +22,7 @@ fn main_0() -> i32 { let m: Value>> = Rc::new(RefCell::new(BTreeMap::new())); (m.as_pointer() as Ptr>>) .with_mut(|__v: &mut BTreeMap>| { - __v.entry(0.clone()) + __v.entry(0) .or_insert_with(|| Rc::new(RefCell::new(::default()))) .as_pointer() }) diff --git a/tests/unit/out/refcount/select.rs b/tests/unit/out/refcount/select.rs index 161fbfcb..732d9517 100644 --- a/tests/unit/out/refcount/select.rs +++ b/tests/unit/out/refcount/select.rs @@ -16,7 +16,7 @@ fn main_0() -> i32 { assert!( (((match nix::unistd::pipe() { Ok((__r, __w)) => { - let __fds = (fds.as_pointer() as Ptr).clone(); + let __fds = (fds.as_pointer() as Ptr); __fds.write(FdRegistry::register(__r)); __fds.offset(1).write(FdRegistry::register(__w)); 0 @@ -36,17 +36,15 @@ fn main_0() -> i32 { ((tv.as_pointer()) as Ptr) .to_any() .memset((0) as u8, 16usize as usize); - ((tv.as_pointer()) as Ptr) - .to_any() - .clone() + ((tv.as_pointer()) as Ptr).to_any() }; (*(*tv.borrow()).tv_sec.borrow_mut()) = 0_i64; assert!( ((({ - let __rp = (rset.as_pointer()).clone(); - let __wp = Ptr::::null().clone(); - let __ep = Ptr::::null().clone(); - let __tp = (tv.as_pointer()).clone(); + let __rp = (rset.as_pointer()); + let __wp = Ptr::::null(); + let __ep = Ptr::::null(); + let __tp = (tv.as_pointer()); let __r_fds: Vec = match __rp.is_null() { true => Vec::new(), false => __rp.with(|__s| { @@ -192,10 +190,10 @@ fn main_0() -> i32 { (*(*tv.borrow()).tv_sec.borrow_mut()) = 1_i64; assert!( ((({ - let __rp = (rset.as_pointer()).clone(); - let __wp = Ptr::::null().clone(); - let __ep = Ptr::::null().clone(); - let __tp = (tv.as_pointer()).clone(); + let __rp = (rset.as_pointer()); + let __wp = Ptr::::null(); + let __ep = Ptr::::null(); + let __tp = (tv.as_pointer()); let __r_fds: Vec = match __rp.is_null() { true => Vec::new(), false => __rp.with(|__s| { diff --git a/tests/unit/out/refcount/simple_index.rs b/tests/unit/out/refcount/simple_index.rs index c8a2e507..664149ea 100644 --- a/tests/unit/out/refcount/simple_index.rs +++ b/tests/unit/out/refcount/simple_index.rs @@ -15,7 +15,8 @@ fn main_0() -> i32 { ((*(v.as_pointer() as Ptr) .offset(0_usize) .upgrade() - .deref()) as bool) + .deref()) + .clone() as bool) ); return 0; } diff --git a/tests/unit/out/refcount/split_binop_aliased_borrows.rs b/tests/unit/out/refcount/split_binop_aliased_borrows.rs index 517116b9..897ac869 100644 --- a/tests/unit/out/refcount/split_binop_aliased_borrows.rs +++ b/tests/unit/out/refcount/split_binop_aliased_borrows.rs @@ -14,7 +14,7 @@ fn main_0() -> i32 { let p: Value> = Rc::new(RefCell::new((v.as_pointer() as Ptr))); let r: Ptr = (v.as_pointer() as Ptr).offset(1_usize); let __rhs = (r.read()); - (*p.borrow()).write(__rhs); + (*p.borrow()).clone().write(__rhs); assert!((((v.as_pointer() as Ptr).offset(0_usize).read()) == 2)); return 0; } diff --git a/tests/unit/out/refcount/stdcopy.rs b/tests/unit/out/refcount/stdcopy.rs index 9f4fabe2..63e9fea6 100644 --- a/tests/unit/out/refcount/stdcopy.rs +++ b/tests/unit/out/refcount/stdcopy.rs @@ -19,7 +19,7 @@ fn main_0() -> i32 { .offset((3) as isize) .get_offset() - (input.as_pointer() as Ptr).get_offset(); - let mut outptr = (output.as_pointer() as Ptr).clone(); + let mut outptr = (output.as_pointer() as Ptr); for value in PtrValueIter::new(&(input.as_pointer() as Ptr), count) { outptr.write(value.into()); outptr += 1; diff --git a/tests/unit/out/refcount/stdio_nofd.rs b/tests/unit/out/refcount/stdio_nofd.rs index e459d55b..57753ab7 100644 --- a/tests/unit/out/refcount/stdio_nofd.rs +++ b/tests/unit/out/refcount/stdio_nofd.rs @@ -174,7 +174,7 @@ pub fn test_fgets_getc_2() { )); assert!( (((!(({ - let __buf = (buf.as_pointer() as Ptr).clone(); + let __buf = (buf.as_pointer() as Ptr); let __n = 8; if __n <= 0 { Ptr::null() @@ -218,7 +218,7 @@ pub fn test_fgets_getc_2() { assert!(((((*fp.borrow()).with_mut(|__f| __f.getc()) == ('l' as i32)) as i32) != 0)); assert!( (((!(({ - let __buf = (buf.as_pointer() as Ptr).clone(); + let __buf = (buf.as_pointer() as Ptr); let __n = 4; if __n <= 0 { Ptr::null() @@ -261,7 +261,7 @@ pub fn test_fgets_getc_2() { ); assert!( (((!(({ - let __buf = (buf.as_pointer() as Ptr).clone(); + let __buf = (buf.as_pointer() as Ptr); let __n = 8; if __n <= 0 { Ptr::null() @@ -304,7 +304,7 @@ pub fn test_fgets_getc_2() { ); assert!( (((({ - let __buf = (buf.as_pointer() as Ptr).clone(); + let __buf = (buf.as_pointer() as Ptr); let __n = 8; if __n <= 0 { Ptr::null() diff --git a/tests/unit/out/refcount/strdup.rs b/tests/unit/out/refcount/strdup.rs index 33c1f344..e20d268b 100644 --- a/tests/unit/out/refcount/strdup.rs +++ b/tests/unit/out/refcount/strdup.rs @@ -35,7 +35,7 @@ pub fn main() { } fn main_0() -> i32 { let d: Value> = Rc::new(RefCell::new(libcc2rs::strdup_refcount( - Ptr::from_string_literal(b"hello").clone(), + Ptr::from_string_literal(b"hello"), ))); assert!((((!((*d.borrow()).is_null())) as i32) != 0)); assert!( @@ -86,7 +86,7 @@ fn main_0() -> i32 { ); libcc2rs::free_refcount(((*d2.borrow()).clone() as Ptr).to_any()); let d3: Value> = Rc::new(RefCell::new(libcc2rs::strdup_refcount( - (buf.as_pointer() as Ptr).clone(), + (buf.as_pointer() as Ptr), ))); assert!((((!((*d3.borrow()).is_null())) as i32) != 0)); assert!( diff --git a/tests/unit/out/refcount/string_h.rs b/tests/unit/out/refcount/string_h.rs index 232e6f23..ca2653f8 100644 --- a/tests/unit/out/refcount/string_h.rs +++ b/tests/unit/out/refcount/string_h.rs @@ -21,7 +21,7 @@ pub fn test_memcpy_0() { &((src.as_pointer() as Ptr) as Ptr).to_any(), 6_usize as usize, ); - ((dst.as_pointer() as Ptr) as Ptr).to_any().clone() + ((dst.as_pointer() as Ptr) as Ptr).to_any() })); assert!( ((({ @@ -57,7 +57,7 @@ pub fn test_memset_1() { ((buf.as_pointer() as Ptr) as Ptr) .to_any() .memset(('x' as i32) as u8, 4_usize as usize); - ((buf.as_pointer() as Ptr) as Ptr).to_any().clone() + ((buf.as_pointer() as Ptr) as Ptr).to_any() })); assert!( ((({ @@ -130,9 +130,7 @@ pub fn test_memmove_3() { &((buf.as_pointer() as Ptr) as Ptr).to_any(), 4_usize as usize, ); - ((buf.as_pointer() as Ptr).offset((1) as isize) as Ptr) - .to_any() - .clone() + ((buf.as_pointer() as Ptr).offset((1) as isize) as Ptr).to_any() })); assert!( ((({ @@ -163,7 +161,7 @@ pub fn test_memmove_3() { pub fn test_strchr_4() { let s: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"hello world"))); let r: Value> = Rc::new(RefCell::new({ - let __s = (*s.borrow()).reinterpret_cast::().clone(); + let __s = (*s.borrow()).reinterpret_cast::(); let __t = ('w' as i32) as u8; match __s.to_c_string_iterator().position(|__c| __c == __t) { Some(__i) => __s.offset(__i), @@ -180,7 +178,7 @@ pub fn test_strchr_4() { assert!(((((((*r.borrow()).read()) as i32) == ('w' as i32)) as i32) != 0)); assert!( (((({ - let __s = (*s.borrow()).reinterpret_cast::().clone(); + let __s = (*s.borrow()).reinterpret_cast::(); let __t = ('z' as i32) as u8; match __s.to_c_string_iterator().position(|__c| __c == __t) { Some(__i) => __s.offset(__i), @@ -577,7 +575,7 @@ pub fn test_memchr_8() { pub fn test_strrchr_9() { let s: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"hello world"))); let r: Value> = Rc::new(RefCell::new({ - let __s = (*s.borrow()).reinterpret_cast::().clone(); + let __s = (*s.borrow()).reinterpret_cast::(); let __t = ('l' as i32) as u8; match __s .to_c_string_iterator() @@ -606,7 +604,7 @@ pub fn test_strrchr_9() { ); assert!( (((({ - let __s = (*s.borrow()).reinterpret_cast::().clone(); + let __s = (*s.borrow()).reinterpret_cast::(); let __t = ('z' as i32) as u8; match __s .to_c_string_iterator() @@ -635,7 +633,7 @@ pub fn test_strrchr_9() { ]))); assert!( ((({ - let __s = (buf.as_pointer() as Ptr).clone(); + let __s = (buf.as_pointer() as Ptr); let __t = ('a' as i32) as u8; match __s .to_c_string_iterator() @@ -659,7 +657,7 @@ pub fn test_strrchr_9() { pub fn test_strcspn_10() { assert!( ((({ - let __set = Ptr::from_string_literal(b"el").clone(); + let __set = Ptr::from_string_literal(b"el"); Ptr::from_string_literal(b"hello") .to_c_string_iterator() .take_while(|__c| !__set.to_c_string_iterator().any(|__r| __r == *__c)) @@ -669,7 +667,7 @@ pub fn test_strcspn_10() { ); assert!( ((({ - let __set = Ptr::from_string_literal(b"xyz").clone(); + let __set = Ptr::from_string_literal(b"xyz"); Ptr::from_string_literal(b"abc") .to_c_string_iterator() .take_while(|__c| !__set.to_c_string_iterator().any(|__r| __r == *__c)) @@ -679,7 +677,7 @@ pub fn test_strcspn_10() { ); assert!( ((({ - let __set = Ptr::from_string_literal(b"abc").clone(); + let __set = Ptr::from_string_literal(b"abc"); Ptr::from_string_literal(b"") .to_c_string_iterator() .take_while(|__c| !__set.to_c_string_iterator().any(|__r| __r == *__c)) @@ -703,7 +701,7 @@ pub fn test_strcspn_10() { pub fn test_strspn_11() { assert!( ((({ - let __set = Ptr::from_string_literal(b"hel").clone(); + let __set = Ptr::from_string_literal(b"hel"); Ptr::from_string_literal(b"hello") .to_c_string_iterator() .take_while(|__c| __set.to_c_string_iterator().any(|__r| __r == *__c)) @@ -713,7 +711,7 @@ pub fn test_strspn_11() { ); assert!( ((({ - let __set = Ptr::from_string_literal(b"xyz").clone(); + let __set = Ptr::from_string_literal(b"xyz"); Ptr::from_string_literal(b"abc") .to_c_string_iterator() .take_while(|__c| __set.to_c_string_iterator().any(|__r| __r == *__c)) @@ -723,7 +721,7 @@ pub fn test_strspn_11() { ); assert!( ((({ - let __set = Ptr::from_string_literal(b"a").clone(); + let __set = Ptr::from_string_literal(b"a"); Ptr::from_string_literal(b"aaa") .to_c_string_iterator() .take_while(|__c| __set.to_c_string_iterator().any(|__r| __r == *__c)) @@ -747,8 +745,8 @@ pub fn test_strspn_11() { pub fn test_strstr_12() { let h: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"hello world"))); let r: Value> = Rc::new(RefCell::new({ - let __needle = Ptr::from_string_literal(b"world").clone(); - let mut __p = (*h.borrow()).reinterpret_cast::().clone(); + let __needle = Ptr::from_string_literal(b"world"); + let mut __p = (*h.borrow()).reinterpret_cast::(); loop { let mut __h = __p.to_c_string_iterator(); if __needle @@ -773,8 +771,8 @@ pub fn test_strstr_12() { ); assert!( (((({ - let __needle = Ptr::from_string_literal(b"xyz").clone(); - let mut __p = (*h.borrow()).reinterpret_cast::().clone(); + let __needle = Ptr::from_string_literal(b"xyz"); + let mut __p = (*h.borrow()).reinterpret_cast::(); loop { let mut __h = __p.to_c_string_iterator(); if __needle @@ -802,8 +800,8 @@ pub fn test_strstr_12() { ]))); assert!( ((({ - let __needle = Ptr::from_string_literal(b"ll").clone(); - let mut __p = (buf.as_pointer() as Ptr).clone(); + let __needle = Ptr::from_string_literal(b"ll"); + let mut __p = (buf.as_pointer() as Ptr); loop { let mut __h = __p.to_c_string_iterator(); if __needle @@ -824,8 +822,8 @@ pub fn test_strstr_12() { pub fn test_strpbrk_13() { let s: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"hello world"))); let r: Value> = Rc::new(RefCell::new({ - let __s = (*s.borrow()).reinterpret_cast::().clone(); - let __set = Ptr::from_string_literal(b"wo").clone(); + let __s = (*s.borrow()).reinterpret_cast::(); + let __set = Ptr::from_string_literal(b"wo"); match __s .to_c_string_iterator() .position(|__c| __set.to_c_string_iterator().any(|__r| __r == __c)) @@ -844,8 +842,8 @@ pub fn test_strpbrk_13() { ); assert!( (((({ - let __s = (*s.borrow()).reinterpret_cast::().clone(); - let __set = Ptr::from_string_literal(b"xyz").clone(); + let __s = (*s.borrow()).reinterpret_cast::(); + let __set = Ptr::from_string_literal(b"xyz"); match __s .to_c_string_iterator() .position(|__c| __set.to_c_string_iterator().any(|__r| __r == __c)) @@ -865,8 +863,8 @@ pub fn test_strpbrk_13() { ]))); assert!( ((({ - let __s = (buf.as_pointer() as Ptr).clone(); - let __set = Ptr::from_string_literal(b"b").clone(); + let __s = (buf.as_pointer() as Ptr); + let __set = Ptr::from_string_literal(b"b"); match __s .to_c_string_iterator() .position(|__c| __set.to_c_string_iterator().any(|__r| __r == __c)) diff --git a/tests/unit/out/refcount/sys_time.rs b/tests/unit/out/refcount/sys_time.rs index a26480fb..46a0c95a 100644 --- a/tests/unit/out/refcount/sys_time.rs +++ b/tests/unit/out/refcount/sys_time.rs @@ -49,7 +49,7 @@ pub fn print_tm_1(t: i64) { let tm: Value = Rc::new(RefCell::new(Default::default())); assert!( (((!(({ - let __res = (tm.as_pointer()).clone(); + let __res = (tm.as_pointer()); match jiff::Timestamp::from_second((t.as_pointer()).read()) { Ok(__ts) => { let __dt = __ts.to_zoned(jiff::tz::TimeZone::UTC); @@ -97,7 +97,7 @@ pub fn print_local_tm_3(t: i64) { let tm: Value = Rc::new(RefCell::new(Default::default())); assert!( (((!(({ - let __res = (tm.as_pointer()).clone(); + let __res = (tm.as_pointer()); match jiff::Timestamp::from_second((t.as_pointer()).read()) { Ok(__ts) => { let __dt = __ts.to_zoned(jiff::tz::TimeZone::system()); @@ -147,7 +147,7 @@ pub fn test_strftime_5() { let tm: Value = Rc::new(RefCell::new(Default::default())); assert!( (((!(({ - let __res = (tm.as_pointer()).clone(); + let __res = (tm.as_pointer()); match jiff::Timestamp::from_second((t.as_pointer()).read()) { Ok(__ts) => { let __dt = __ts.to_zoned(jiff::tz::TimeZone::UTC); diff --git a/tests/unit/out/refcount/union_addrof_external.rs b/tests/unit/out/refcount/union_addrof_external.rs index 15928f01..6da73132 100644 --- a/tests/unit/out/refcount/union_addrof_external.rs +++ b/tests/unit/out/refcount/union_addrof_external.rs @@ -169,7 +169,7 @@ fn main_0() -> i32 { ((c.as_pointer()) as Ptr) .to_any() .memset((0) as u8, 128usize as usize); - ((c.as_pointer()) as Ptr).to_any().clone() + ((c.as_pointer()) as Ptr).to_any() }; ({ let _out: AnyPtr = ((*c.borrow()).view.as_pointer()).to_any(); diff --git a/tests/unit/out/refcount/union_cross_arm_cast.rs b/tests/unit/out/refcount/union_cross_arm_cast.rs index 70fa8176..6732aafd 100644 --- a/tests/unit/out/refcount/union_cross_arm_cast.rs +++ b/tests/unit/out/refcount/union_cross_arm_cast.rs @@ -175,7 +175,7 @@ fn main_0() -> i32 { ((c.as_pointer()) as Ptr) .to_any() .memset((0) as u8, 68usize as usize); - ((c.as_pointer()) as Ptr).to_any().clone() + ((c.as_pointer()) as Ptr).to_any() }; (*(*(*(*c.borrow()).u.borrow()).a().upgrade().deref()) .code diff --git a/tests/unit/out/refcount/union_memset_memcpy.rs b/tests/unit/out/refcount/union_memset_memcpy.rs index fa78907e..d1c75877 100644 --- a/tests/unit/out/refcount/union_memset_memcpy.rs +++ b/tests/unit/out/refcount/union_memset_memcpy.rs @@ -166,7 +166,7 @@ fn main_0() -> i32 { ((c.as_pointer()) as Ptr) .to_any() .memset((0) as u8, 256usize as usize); - ((c.as_pointer()) as Ptr).to_any().clone() + ((c.as_pointer()) as Ptr).to_any() }; assert!( (((((*(*(*(*c.borrow()).view.borrow()).a().upgrade().deref()) @@ -240,7 +240,6 @@ fn main_0() -> i32 { .raw_() .reinterpret_cast::()) as Ptr) .to_any() - .clone() }; assert!( (((((*(*(*(*c.borrow()).view.borrow()).b().upgrade().deref()) @@ -263,7 +262,7 @@ fn main_0() -> i32 { ((c.as_pointer()) as Ptr) .to_any() .memset((0) as u8, 256usize as usize); - ((c.as_pointer()) as Ptr).to_any().clone() + ((c.as_pointer()) as Ptr).to_any() }; assert!( (((((*(*(*(*c.borrow()).view.borrow()).b().upgrade().deref()) diff --git a/tests/unit/out/refcount/union_nested.rs b/tests/unit/out/refcount/union_nested.rs index 094fc897..124238a8 100644 --- a/tests/unit/out/refcount/union_nested.rs +++ b/tests/unit/out/refcount/union_nested.rs @@ -193,7 +193,7 @@ fn main_0() -> i32 { ((ex.as_pointer()) as Ptr) .to_any() .memset((0) as u8, 144usize as usize); - ((ex.as_pointer()) as Ptr).to_any().clone() + ((ex.as_pointer()) as Ptr).to_any() }; (*(*ex.borrow()).kind.borrow_mut()) = 2; (*(*ex.borrow()).level.borrow_mut()) = 1; diff --git a/tests/unit/out/refcount/union_struct_dual_use.rs b/tests/unit/out/refcount/union_struct_dual_use.rs index e80895fa..27a23ef1 100644 --- a/tests/unit/out/refcount/union_struct_dual_use.rs +++ b/tests/unit/out/refcount/union_struct_dual_use.rs @@ -116,7 +116,7 @@ fn main_0() -> i32 { ((outer.as_pointer()) as Ptr) .to_any() .memset((0) as u8, 16usize as usize); - ((outer.as_pointer()) as Ptr).to_any().clone() + ((outer.as_pointer()) as Ptr).to_any() }; (*(*(*(*outer.borrow()).u.borrow()).inner().upgrade().deref()) .a diff --git a/tests/unit/out/refcount/vector.rs b/tests/unit/out/refcount/vector.rs index cc388c6c..1227c601 100644 --- a/tests/unit/out/refcount/vector.rs +++ b/tests/unit/out/refcount/vector.rs @@ -38,7 +38,7 @@ fn main_0() -> i32 { (*v2.borrow_mut()).push(3); assert!(((*v2.borrow()).len() == 3_usize)); { - let idx = (v2.as_pointer() as Ptr).clone().get_offset(); + let idx = (v2.as_pointer() as Ptr).get_offset(); (v2.as_pointer() as Ptr>).with_mut(|__v: &mut Vec| __v.remove(idx)); (v2.as_pointer() as Ptr>).to_strong().as_pointer() as Ptr }; @@ -46,9 +46,9 @@ fn main_0() -> i32 { assert!((((v2.as_pointer() as Ptr).offset(0_usize).read()) == 2)); assert!((((v2.as_pointer() as Ptr).offset(1_usize).read()) == 3)); { - let __off = (v2.as_pointer() as Ptr).clone().get_offset(); + let __off = (v2.as_pointer() as Ptr).get_offset(); (*v2.borrow_mut()).insert(__off, 100); - (v2.as_pointer() as Ptr).clone() + (v2.as_pointer() as Ptr) }; ({ copy_0((*v2.borrow()).clone()) }); assert!(((*v2.borrow()).len() == 3_usize)); diff --git a/tests/unit/out/refcount/vector_with_allocator.rs b/tests/unit/out/refcount/vector_with_allocator.rs index 288910bd..f217e8ba 100644 --- a/tests/unit/out/refcount/vector_with_allocator.rs +++ b/tests/unit/out/refcount/vector_with_allocator.rs @@ -116,7 +116,7 @@ fn main_0() -> i32 { (*v2.borrow_mut()).push(3); assert!(((*v2.borrow()).len() == 3_usize)); { - let idx = (v2.as_pointer() as Ptr).clone().get_offset(); + let idx = (v2.as_pointer() as Ptr).get_offset(); (v2.as_pointer() as Ptr>).with_mut(|__v: &mut Vec| __v.remove(idx)); (v2.as_pointer() as Ptr>).to_strong().as_pointer() as Ptr }; @@ -124,9 +124,9 @@ fn main_0() -> i32 { assert!((((v2.as_pointer() as Ptr).offset(0_usize).read()) == 2)); assert!((((v2.as_pointer() as Ptr).offset(1_usize).read()) == 3)); { - let __off = (v2.as_pointer() as Ptr).clone().get_offset(); + let __off = (v2.as_pointer() as Ptr).get_offset(); (*v2.borrow_mut()).insert(__off, 100); - (v2.as_pointer() as Ptr).clone() + (v2.as_pointer() as Ptr) }; ({ copy_0((*v2.borrow()).clone()) }); assert!(((*v2.borrow()).len() == 3_usize)); diff --git a/tests/unit/out/unsafe/foreach_double.rs b/tests/unit/out/unsafe/foreach_double.rs index 708d9bd8..1aba8f6e 100644 --- a/tests/unit/out/unsafe/foreach_double.rs +++ b/tests/unit/out/unsafe/foreach_double.rs @@ -47,11 +47,11 @@ unsafe fn main_0() -> i32 { } let mut m: Vec> = Vec::new(); let mut v1: Vec = Vec::new(); - m.push(v1); + m.push((v1).clone()); let mut v2: Vec = Vec::new(); - m.push(v2); + m.push((v2).clone()); let mut v3: Vec = Vec::new(); - m.push(v3); + m.push((v3).clone()); 'loop_: for row in 0..(m.len()) { let mut row = m.as_mut_ptr().add(row); 'loop_: for col in 0..((*row).len()) { diff --git a/tests/unit/out/unsafe/map.rs b/tests/unit/out/unsafe/map.rs index 36fdf0f0..25c49b6c 100644 --- a/tests/unit/out/unsafe/map.rs +++ b/tests/unit/out/unsafe/map.rs @@ -96,10 +96,10 @@ unsafe fn main_0() -> i32 { assert!(((other_map.len()) == (0_usize))); let mut key0: (i32, i64) = (1.into(), 1.into()); let mut value: f64 = 2_f64; - (*other_map.entry(key0).or_default().as_mut()) = value; - value = (*other_map.entry(key0).or_default().as_mut()); + (*other_map.entry((key0).clone()).or_default().as_mut()) = value; + value = (*other_map.entry((key0).clone()).or_default().as_mut()); assert!(((other_map.len()) == (1_usize))); - assert!(((*other_map.entry(key0).or_default().as_mut()) == (value))); + assert!(((*other_map.entry((key0).clone()).or_default().as_mut()) == (value))); assert!(((m.len()) == (3_usize))); let mut k: i32 = 0; assert!(((*(m.get(&(k as i16)).expect("out of range!").as_ref() as *const u32)) == (5_u32))); From 3c8a146d2246d91b3aebe03bb116823e35b57b5f Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Tue, 15 Sep 2026 11:12:02 +0100 Subject: [PATCH 12/15] Add move access type --- cpp2rust/converter/converter.cpp | 18 +-- cpp2rust/converter/converter.h | 2 +- .../converter/models/converter_refcount.cpp | 3 +- cpp2rust/converter/translation_rule.cpp | 27 +++-- cpp2rust/converter/translation_rule.h | 2 +- rule-preprocessor/src/ir.rs | 7 +- rule-preprocessor/src/semantic.rs | 104 ++++++++++++++---- rule-preprocessor/src/syntactic.rs | 14 +-- 8 files changed, 124 insertions(+), 53 deletions(-) diff --git a/cpp2rust/converter/converter.cpp b/cpp2rust/converter/converter.cpp index f4e37a09..9df9cc43 100644 --- a/cpp2rust/converter/converter.cpp +++ b/cpp2rust/converter/converter.cpp @@ -4614,8 +4614,8 @@ void Converter::PlaceholderCtx::dump() const { << ", is_cpp_ptr: " << is_cpp_ptr << ", maps_to_rust_ptr: " << maps_to_rust_ptr << ", declared_in_rule_as_rust_ptr: " - << declared_in_rule_as_rust_ptr << ", access: " - << (access == TranslationRule::Access::kRead ? "read" : "write") + << declared_in_rule_as_rust_ptr + << ", access: " << static_cast(access) << ", param_type: " << param_type << ", materialize_idx: " << materialize_idx << '\n'; } @@ -4653,10 +4653,10 @@ std::string Converter::ConvertPlaceholder(clang::Expr *expr, clang::Expr *arg, if (ph_ctx.needs_object_receiver()) { Buffer buf(*this); PushExplicitAutoref autoref( - *this, - ph_ctx.is_index_base - ? std::optional(ph_ctx.access == TranslationRule::Access::kWrite) - : std::nullopt); + *this, ph_ctx.is_index_base + ? std::optional(ph_ctx.access == + TranslationRule::Access::kBorrowMut) + : std::nullopt); PushExprKind push(*this, ExprKind::RValue); ConvertDeref(arg); return std::move(buf).str(); @@ -4670,13 +4670,17 @@ std::string Converter::ConvertPlaceholder(clang::Expr *expr, clang::Expr *arg, return ConvertLValue(arg); } - if (ph_ctx.access == TranslationRule::Access::kMove) { + if (ph_ctx.access == TranslationRule::Access::kTake) { if (clang::isa(arg)) { return ConvertRValue(arg); } return std::format("std::mem::take(&mut {})", ConvertLValue(arg)); } + if (ph_ctx.access == TranslationRule::Access::kRead) { + return ConvertFreshRValue(arg, ph_ctx.implicit_convert_to); + } + return ConvertRValue(arg, ph_ctx.implicit_convert_to); } diff --git a/cpp2rust/converter/converter.h b/cpp2rust/converter/converter.h index f5f6e6a8..713031cb 100644 --- a/cpp2rust/converter/converter.h +++ b/cpp2rust/converter/converter.h @@ -251,7 +251,7 @@ class Converter : public clang::RecursiveASTVisitor { } bool needs_lvalue() const { - return access == TranslationRule::Access::kWrite; + return access == TranslationRule::Access::kBorrowMut; } void dump() const; diff --git a/cpp2rust/converter/models/converter_refcount.cpp b/cpp2rust/converter/models/converter_refcount.cpp index e3029d2c..35d6732f 100644 --- a/cpp2rust/converter/models/converter_refcount.cpp +++ b/cpp2rust/converter/models/converter_refcount.cpp @@ -2584,7 +2584,8 @@ std::string ConverterRefCount::ConvertMappedMethodCall( clang::Expr *expr, const TranslationRule::MethodCallFragment &mc, clang::Expr **args, unsigned num_args, TempMaterializationCtx *ctx) { auto receiver_ph = mc.getReceiverPlaceholder(); - if (!receiver_ph || receiver_ph->access == TranslationRule::Access::kRead) { + if (!receiver_ph || receiver_ph->access == TranslationRule::Access::kBorrow || + receiver_ph->access == TranslationRule::Access::kRead) { return Converter::ConvertMappedMethodCall(expr, mc, args, num_args, ctx); } diff --git a/cpp2rust/converter/translation_rule.cpp b/cpp2rust/converter/translation_rule.cpp index 4536dee2..63c9ef67 100644 --- a/cpp2rust/converter/translation_rule.cpp +++ b/cpp2rust/converter/translation_rule.cpp @@ -35,16 +35,18 @@ TypeInfo ParseTypeInfoJSON(const llvm::json::Object &obj) { } Access ParseAccessJSON(llvm::StringRef value) { - if (value == "read") { + if (value == "borrow") { + return Access::kBorrow; + } else if (value == "borrow_mut") { + return Access::kBorrowMut; + } else if (value == "read") { return Access::kRead; - } else if (value == "write") { - return Access::kWrite; - } else if (value == "move") { - return Access::kMove; + } else if (value == "take") { + return Access::kTake; } else { llvm::errs() << "Invalid access value: " << value << '\n'; assert(0); - return Access::kRead; + return Access::kBorrow; } } @@ -247,14 +249,17 @@ void VaArgsFragment::dump() const { log() << " va_args\n"; } void PlaceholderFragment::dump() const { log() << " placeholder: " << n; switch (access) { + case Access::kBorrow: + log() << " (borrow)\n"; + break; + case Access::kBorrowMut: + log() << " (borrow_mut)\n"; + break; case Access::kRead: log() << " (read)\n"; break; - case Access::kWrite: - log() << " (write)\n"; - break; - case Access::kMove: - log() << " (move)\n"; + case Access::kTake: + log() << " (take)\n"; break; } } diff --git a/cpp2rust/converter/translation_rule.h b/cpp2rust/converter/translation_rule.h index dc07c4ef..e1adcfe2 100644 --- a/cpp2rust/converter/translation_rule.h +++ b/cpp2rust/converter/translation_rule.h @@ -23,7 +23,7 @@ struct TextFragment { void dump() const; }; -enum class Access : int8_t { kRead, kWrite, kMove }; +enum class Access : int8_t { kBorrow, kBorrowMut, kRead, kTake }; struct PlaceholderFragment { unsigned n; // "a0", "a1", ... diff --git a/rule-preprocessor/src/ir.rs b/rule-preprocessor/src/ir.rs index e9b353c3..91a1300c 100644 --- a/rule-preprocessor/src/ir.rs +++ b/rule-preprocessor/src/ir.rs @@ -143,11 +143,12 @@ pub enum BodyFragment { } #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] -#[serde(rename_all = "lowercase")] +#[serde(rename_all = "snake_case")] pub enum Access { + Borrow, + BorrowMut, Read, - Write, - Move, + Take, Unknown, } diff --git a/rule-preprocessor/src/semantic.rs b/rule-preprocessor/src/semantic.rs index 4e8b2dd8..1157771c 100644 --- a/rule-preprocessor/src/semantic.rs +++ b/rule-preprocessor/src/semantic.rs @@ -193,7 +193,7 @@ impl<'tcx> FnDecl<'tcx> { fn_ir, visited: HashMap::new(), }; - visitor.visit_expr(self.body.value, Access::Read); + visitor.visit_expr(self.body.value, Access::Borrow); } } @@ -290,6 +290,21 @@ fn decl_source_file( ) } +fn is_copy<'tcx>(tcx: rustc_middle::ty::TyCtxt<'tcx>, ty: rustc_middle::ty::Ty<'tcx>) -> bool { + use rustc_infer::infer::TyCtxtInferExt; + use rustc_trait_selection::infer::InferCtxtExt; + + let Some(copy_trait) = tcx.lang_items().copy_trait() else { + return false; + }; + let infcx = tcx + .infer_ctxt() + .build(rustc_middle::ty::TypingMode::non_body_analysis()); + infcx + .type_implements_trait(copy_trait, [ty], rustc_middle::ty::ParamEnv::empty()) + .must_apply_modulo_regions() +} + fn type_derives<'tcx>( tcx: rustc_middle::ty::TyCtxt<'tcx>, ty: rustc_middle::ty::Ty<'tcx>, @@ -353,10 +368,15 @@ impl<'a, 'tcx> AstVisitor<'a, 'tcx> { fn visit_expr(&mut self, expr: &'tcx rustc_hir::Expr<'tcx>, context: Access) { // Reached an argument used inside the rule body if let Some(param) = self.expr_as_decl_ref(expr) { + let access = if context == Access::Borrow && self.is_moved(expr) { + Access::Read + } else { + context + }; self.fn_ir .resolve_next_param(¶m, &mut self.visited, |p| { if p.access == Access::Unknown { - p.access = context; + p.access = access; } }); return; @@ -370,18 +390,18 @@ impl<'a, 'tcx> AstVisitor<'a, 'tcx> { param_access.first().copied().unwrap_or(Access::Unknown), ); for (i, arg) in args.iter().enumerate() { - let access = param_access.get(i + 1).copied().unwrap_or(Access::Read); + let access = param_access.get(i + 1).copied().unwrap_or(Access::Borrow); self.visit_expr(arg, access); } } rustc_hir::ExprKind::Call(callee, args) => { if self.is_std_mem_take(expr) && args.len() == 1 { - self.visit_expr(&args[0], Access::Move); + self.visit_expr(&args[0], Access::Take); } else { self.visit_expr(callee, context); let param_access = self.resolve_callee_param_access(expr); for (i, arg) in args.iter().enumerate() { - let access = param_access.get(i).copied().unwrap_or(Access::Read); + let access = param_access.get(i).copied().unwrap_or(Access::Borrow); self.visit_expr(arg, access); } } @@ -389,22 +409,31 @@ impl<'a, 'tcx> AstVisitor<'a, 'tcx> { rustc_hir::ExprKind::Assign(lhs, rhs, _) | rustc_hir::ExprKind::AssignOp(_, lhs, rhs) => { - self.visit_expr(lhs, Access::Write); - self.visit_expr(rhs, Access::Read); + self.visit_expr(lhs, Access::BorrowMut); + self.visit_expr(rhs, Access::Borrow); } rustc_hir::ExprKind::AddrOf(_, rustc_hir::Mutability::Mut, inner) => { self.visit_expr( inner, - if context == Access::Move { - Access::Move + if context == Access::Take { + Access::Take } else { - Access::Write + Access::BorrowMut }, ); } rustc_hir::ExprKind::AddrOf(_, rustc_hir::Mutability::Not, inner) => { - self.visit_expr(inner, Access::Read); + if let Some(param) = self.expr_as_decl_ref(inner) { + self.fn_ir + .resolve_next_param(¶m, &mut self.visited, |p| { + if p.access == Access::Unknown { + p.access = Access::Borrow; + } + }); + return; + } + self.visit_expr(inner, Access::Borrow); } rustc_hir::ExprKind::Block(block, _) | rustc_hir::ExprKind::Loop(block, _, _, _) => { @@ -412,11 +441,11 @@ impl<'a, 'tcx> AstVisitor<'a, 'tcx> { match &stmt.kind { rustc_hir::StmtKind::Let(local) => { if let Some(init) = local.init { - self.visit_expr(init, Access::Read); + self.visit_expr(init, Access::Borrow); } } rustc_hir::StmtKind::Expr(e) | rustc_hir::StmtKind::Semi(e) => { - self.visit_expr(e, Access::Read); + self.visit_expr(e, Access::Borrow); } _ => {} } @@ -427,22 +456,36 @@ impl<'a, 'tcx> AstVisitor<'a, 'tcx> { } rustc_hir::ExprKind::If(cond, then_branch, else_branch) => { - self.visit_expr(cond, Access::Read); + self.visit_expr(cond, Access::Borrow); self.visit_expr(then_branch, context); if let Some(e) = else_branch { self.visit_expr(e, context); } } rustc_hir::ExprKind::Match(scrutinee, arms, _) => { - self.visit_expr(scrutinee, Access::Read); + self.visit_expr(scrutinee, Access::Borrow); for arm in arms.iter() { + if let Some(guard) = arm.guard { + self.visit_expr(guard, Access::Borrow); + } self.visit_expr(arm.body, context); } } + rustc_hir::ExprKind::Field(base, _) => { + if let Some(param) = self.expr_as_decl_ref(base) { + self.fn_ir + .resolve_next_param(¶m, &mut self.visited, |p| { + if p.access == Access::Unknown { + p.access = context; + } + }); + return; + } + self.visit_expr(base, context); + } rustc_hir::ExprKind::Unary(_, e) | rustc_hir::ExprKind::Cast(e, _) - | rustc_hir::ExprKind::Field(e, _) | rustc_hir::ExprKind::DropTemps(e) | rustc_hir::ExprKind::Repeat(e, _) => { self.visit_expr(e, context); @@ -480,7 +523,7 @@ impl<'a, 'tcx> AstVisitor<'a, 'tcx> { | rustc_hir::ExprKind::Continue(_) => {} rustc_hir::ExprKind::Ret(Some(e)) | rustc_hir::ExprKind::Break(_, Some(e)) => { - self.visit_expr(e, Access::Read); + self.visit_expr(e, Access::Borrow); } other => { @@ -494,6 +537,15 @@ impl<'a, 'tcx> AstVisitor<'a, 'tcx> { } } + fn is_moved(&self, expr: &rustc_hir::Expr<'tcx>) -> bool { + let results = self.tcx.typeck(expr.hir_id.owner); + let borrowed = results + .expr_adjustments(expr) + .iter() + .any(|adj| matches!(adj.kind, rustc_middle::ty::adjustment::Adjust::Borrow(_))); + !borrowed && !is_copy(self.tcx, results.expr_ty(expr)) + } + fn expr_as_decl_ref(&self, expr: &rustc_hir::Expr<'_>) -> Option { if let rustc_hir::ExprKind::Path(rustc_hir::QPath::Resolved(_, path)) = &expr.kind && let Some(seg) = path.segments.last() @@ -548,11 +600,19 @@ impl<'a, 'tcx> AstVisitor<'a, 'tcx> { fn access_for_type(ty: &rustc_middle::ty::Ty<'_>) -> Access { match ty.kind() { - rustc_middle::ty::TyKind::Ref(_, _, rustc_middle::ty::Mutability::Mut) => Access::Write, - rustc_middle::ty::TyKind::Ref(_, _, rustc_middle::ty::Mutability::Not) => Access::Read, - rustc_middle::ty::TyKind::RawPtr(_, rustc_middle::ty::Mutability::Mut) => Access::Write, - rustc_middle::ty::TyKind::RawPtr(_, rustc_middle::ty::Mutability::Not) => Access::Read, - _ => Access::Read, + rustc_middle::ty::TyKind::Ref(_, _, rustc_middle::ty::Mutability::Mut) => { + Access::BorrowMut + } + rustc_middle::ty::TyKind::Ref(_, _, rustc_middle::ty::Mutability::Not) => { + Access::Borrow + } + rustc_middle::ty::TyKind::RawPtr(_, rustc_middle::ty::Mutability::Mut) => { + Access::BorrowMut + } + rustc_middle::ty::TyKind::RawPtr(_, rustc_middle::ty::Mutability::Not) => { + Access::Borrow + } + _ => Access::Borrow, } } } diff --git a/rule-preprocessor/src/syntactic.rs b/rule-preprocessor/src/syntactic.rs index 266613a0..79b3796e 100644 --- a/rule-preprocessor/src/syntactic.rs +++ b/rule-preprocessor/src/syntactic.rs @@ -405,15 +405,15 @@ impl<'a> FnIrBuilder<'a> { && lhs.syntax().text_range().end() == name_ref.syntax().text_range().end() { - return Some(Access::Write); + return Some(Access::BorrowMut); } - Some(Access::Read) + Some(Access::Unknown) }, ast::RefExpr(ref_expr) => { if ref_expr.mut_token().is_some() { - Some(Access::Write) + Some(Access::BorrowMut) } else { - Some(Access::Read) + Some(Access::Borrow) } }, ast::MethodCallExpr(call) => { @@ -435,16 +435,16 @@ impl<'a> FnIrBuilder<'a> { && sl.tail_expr().is_some_and(|tail| tail.syntax().text_range() == name_ref.syntax().text_range()) { - Some(Access::Write) + Some(Access::BorrowMut) } else { - Some(Access::Read) + Some(Access::Unknown) } }, _ => None, } } }) - .unwrap_or(Access::Read) + .unwrap_or(Access::Unknown) } fn is_extern(&self) -> bool { From 3b78ca808e382936da02c6c8e1b3ddb8b506f79b Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Tue, 15 Sep 2026 11:13:56 +0100 Subject: [PATCH 13/15] Fix rule for move vector --- rules/vector/tgt_refcount.rs | 6 ++++-- rules/vector/tgt_unsafe.rs | 4 ++-- tests/unit/out/refcount/foreach_double.rs | 12 +++++++++--- tests/unit/out/unsafe/foreach_double.rs | 6 +++--- 4 files changed, 18 insertions(+), 10 deletions(-) diff --git a/rules/vector/tgt_refcount.rs b/rules/vector/tgt_refcount.rs index 33d0f3a5..59165d22 100644 --- a/rules/vector/tgt_refcount.rs +++ b/rules/vector/tgt_refcount.rs @@ -109,8 +109,10 @@ fn f30(a0: usize) -> Vec>> { .collect::>() } -fn f31(a0: Ptr>>>, a1: Vec) { - a0.with_mut(|__v: &mut Vec>>| __v.push(Rc::new(RefCell::new(a1)))) +fn f31(a0: Ptr>>>, a1: &mut Vec) { + a0.with_mut(|__v: &mut Vec>>| { + __v.push(Rc::new(RefCell::new(std::mem::take(&mut *a1)))) + }) } fn f32(a0: Ptr>>>, a1: usize) { diff --git a/rules/vector/tgt_unsafe.rs b/rules/vector/tgt_unsafe.rs index 54228339..c4277a7d 100644 --- a/rules/vector/tgt_unsafe.rs +++ b/rules/vector/tgt_unsafe.rs @@ -138,8 +138,8 @@ unsafe fn f30(a0: usize) -> Vec> { .map(|_| >::default()) .collect::>() } -unsafe fn f31(a0: &mut Vec>, a1: Vec) { - a0.push(a1) +unsafe fn f31(a0: &mut Vec>, a1: &mut Vec) { + a0.push(std::mem::take(&mut *a1)) } unsafe fn f32(a0: &mut Vec>, a1: usize) { a0.resize_with(a1 as usize, || >::default()) diff --git a/tests/unit/out/refcount/foreach_double.rs b/tests/unit/out/refcount/foreach_double.rs index 7140155b..66860666 100644 --- a/tests/unit/out/refcount/foreach_double.rs +++ b/tests/unit/out/refcount/foreach_double.rs @@ -52,15 +52,21 @@ fn main_0() -> i32 { let m: Value>>> = Rc::new(RefCell::new(Vec::new())); let v1: Value> = Rc::new(RefCell::new(Vec::new())); (m.as_pointer() as Ptr>>>).with_mut(|__v: &mut Vec>>| { - __v.push(Rc::new(RefCell::new((*v1.borrow()).clone()))) + __v.push(Rc::new(RefCell::new(std::mem::take( + &mut (*v1.borrow_mut()), + )))) }); let v2: Value> = Rc::new(RefCell::new(Vec::new())); (m.as_pointer() as Ptr>>>).with_mut(|__v: &mut Vec>>| { - __v.push(Rc::new(RefCell::new((*v2.borrow()).clone()))) + __v.push(Rc::new(RefCell::new(std::mem::take( + &mut (*v2.borrow_mut()), + )))) }); let v3: Value> = Rc::new(RefCell::new(Vec::new())); (m.as_pointer() as Ptr>>>).with_mut(|__v: &mut Vec>>| { - __v.push(Rc::new(RefCell::new((*v3.borrow()).clone()))) + __v.push(Rc::new(RefCell::new(std::mem::take( + &mut (*v3.borrow_mut()), + )))) }); 'loop_: for mut row in m.as_pointer() as Ptr>> { let row: Ptr> = row.upgrade().deref().as_pointer(); diff --git a/tests/unit/out/unsafe/foreach_double.rs b/tests/unit/out/unsafe/foreach_double.rs index 1aba8f6e..ed68831b 100644 --- a/tests/unit/out/unsafe/foreach_double.rs +++ b/tests/unit/out/unsafe/foreach_double.rs @@ -47,11 +47,11 @@ unsafe fn main_0() -> i32 { } let mut m: Vec> = Vec::new(); let mut v1: Vec = Vec::new(); - m.push((v1).clone()); + m.push(std::mem::take(&mut v1)); let mut v2: Vec = Vec::new(); - m.push((v2).clone()); + m.push(std::mem::take(&mut v2)); let mut v3: Vec = Vec::new(); - m.push((v3).clone()); + m.push(std::mem::take(&mut v3)); 'loop_: for row in 0..(m.len()) { let mut row = m.as_mut_ptr().add(row); 'loop_: for col in 0..((*row).len()) { From 64298b6d017ac237cd609b21d2c4e520b19bf638 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Tue, 15 Sep 2026 11:33:11 +0100 Subject: [PATCH 14/15] Passthrough rules inherit the context of the caller --- cpp2rust/converter/converter.cpp | 13 +++++++++++-- cpp2rust/converter/converter.h | 2 ++ cpp2rust/converter/models/converter_refcount.cpp | 7 ++++++- rule-preprocessor/src/semantic.rs | 14 ++++++++++++++ tests/unit/out/refcount/find.rs | 7 +++---- tests/unit/out/refcount/map.rs | 12 +++++------- .../out/refcount/redundant_copy_in_conversion.rs | 14 ++++++-------- tests/unit/out/refcount/reference_wrapper.rs | 16 ++++++---------- tests/unit/out/refcount/simple_index.rs | 3 +-- .../out/refcount/split_binop_aliased_borrows.rs | 2 +- tests/unit/out/refcount/void_return.rs | 4 +--- 11 files changed, 56 insertions(+), 38 deletions(-) diff --git a/cpp2rust/converter/converter.cpp b/cpp2rust/converter/converter.cpp index 9df9cc43..1041ba22 100644 --- a/cpp2rust/converter/converter.cpp +++ b/cpp2rust/converter/converter.cpp @@ -681,6 +681,13 @@ bool Converter::RecordDerivesDefault(const clang::RecordDecl *decl) { return true; } +bool Converter::IsPassThroughRule(clang::Expr *expr) const { + const auto *rule = Mapper::GetExprRule(GetCalleeOrExpr(expr)); + return rule && rule->body.size() == 1 && + std::holds_alternative( + rule->body[0]); +} + bool Converter::RecordDerivesCopy(const clang::RecordDecl *decl) const { auto *derives = Mapper::MappedDerives(ctx_.getCanonicalTagType(decl)); return derives && @@ -1772,7 +1779,7 @@ bool Converter::VisitCallExpr(clang::CallExpr *expr) { StrCat(str); if (deref_ref) { SetValueFreshness(expr->getType()); - } else { + } else if (!IsPassThroughRule(expr)) { SetFreshType(expr->getType()); } return false; @@ -3453,7 +3460,9 @@ bool Converter::VisitCXXConstructExpr(clang::CXXConstructExpr *expr) { if (auto str = GetMappedAsString(expr, expr->getArgs(), expr->getNumArgs()); !str.empty()) { StrCat(str); - SetFreshType(expr->getType()); + if (!IsPassThroughRule(expr)) { + SetFreshType(expr->getType()); + } return false; } diff --git a/cpp2rust/converter/converter.h b/cpp2rust/converter/converter.h index 713031cb..c4bab0e4 100644 --- a/cpp2rust/converter/converter.h +++ b/cpp2rust/converter/converter.h @@ -653,6 +653,8 @@ class Converter : public clang::RecursiveASTVisitor { bool RecordDerivesCopy(const clang::RecordDecl *decl) const; + bool IsPassThroughRule(clang::Expr *expr) const; + bool RecordHasCopyableFields(const clang::RecordDecl *decl); bool ShouldReplaceWithMappedBody(clang::DeclRefExpr *expr) const; diff --git a/cpp2rust/converter/models/converter_refcount.cpp b/cpp2rust/converter/models/converter_refcount.cpp index 35d6732f..6cbc611a 100644 --- a/cpp2rust/converter/models/converter_refcount.cpp +++ b/cpp2rust/converter/models/converter_refcount.cpp @@ -1125,6 +1125,9 @@ bool ConverterRefCount::VisitCallExpr(clang::CallExpr *expr) { str = std::format("({{ {} {} }})", ctx->temporary_bindings, str); } StrCat(str); + if (IsPassThroughRule(expr)) { + return false; + } if (IsPointerType(ty) || ty->isReferenceType()) { computed_expr_type_ = ComputedExprType::FreshPointer; } else { @@ -1893,7 +1896,9 @@ bool ConverterRefCount::VisitCXXConstructExpr(clang::CXXConstructExpr *expr) { computed_expr_type_ = ComputedExprType::FreshPointer; } else { StrCat(str); - computed_expr_type_ = ComputedExprType::FreshValue; + if (!IsPassThroughRule(expr)) { + computed_expr_type_ = ComputedExprType::FreshValue; + } } return false; } diff --git a/rule-preprocessor/src/semantic.rs b/rule-preprocessor/src/semantic.rs index 1157771c..98880347 100644 --- a/rule-preprocessor/src/semantic.rs +++ b/rule-preprocessor/src/semantic.rs @@ -193,6 +193,20 @@ impl<'tcx> FnDecl<'tcx> { fn_ir, visited: HashMap::new(), }; + if let rustc_hir::ExprKind::Block(block, _) = &self.body.value.kind + && block.stmts.is_empty() + && let Some(e) = block.expr + && let Some(param) = visitor.expr_as_decl_ref(e) + { + visitor + .fn_ir + .resolve_next_param(¶m, &mut visitor.visited, |p| { + if p.access == Access::Unknown { + p.access = Access::Borrow; + } + }); + return; + } visitor.visit_expr(self.body.value, Access::Borrow); } } diff --git a/tests/unit/out/refcount/find.rs b/tests/unit/out/refcount/find.rs index c102d736..4ad31231 100644 --- a/tests/unit/out/refcount/find.rs +++ b/tests/unit/out/refcount/find.rs @@ -17,16 +17,15 @@ fn main_0() -> i32 { let v_begin: Value> = Rc::new(RefCell::new((v.as_pointer() as Ptr))); let v_end: Value> = Rc::new(RefCell::new((v.as_pointer() as Ptr).to_end())); let it: Value> = Rc::new(RefCell::new( - (*v_begin.borrow()).clone().offset( + (*v_begin.borrow()).offset( (*v_begin.borrow()) - .clone() .clone() .into_iter() .enumerate() .position(|(index_0, value_0)| { - index_0 < (*v_end.borrow()).clone().get_offset() as usize && value_0.read() == 2 + index_0 < (*v_end.borrow()).get_offset() as usize && value_0.read() == 2 }) - .unwrap_or((*v_end.borrow()).clone().get_offset() as usize) as isize, + .unwrap_or((*v_end.borrow()).get_offset() as usize) as isize, ), )); let v_result_true: Value = Rc::new(RefCell::new( diff --git a/tests/unit/out/refcount/map.rs b/tests/unit/out/refcount/map.rs index e942240e..5bf2841c 100644 --- a/tests/unit/out/refcount/map.rs +++ b/tests/unit/out/refcount/map.rs @@ -200,13 +200,11 @@ fn main_0() -> i32 { (*(*it.borrow()).second().borrow()) })); assert!(((*x1.borrow()) == 4_u32)); - let x2: Value = Rc::new(RefCell::new( - if (*const_it.borrow()) == (*end.borrow()).clone() { - 0_u32 - } else { - (*(*const_it.borrow()).second().borrow()) - }, - )); + let x2: Value = Rc::new(RefCell::new(if (*const_it.borrow()) == (*end.borrow()) { + 0_u32 + } else { + (*(*const_it.borrow()).second().borrow()) + })); assert!(((*x2.borrow()) == 0_u32)); let x3: Value = Rc::new(RefCell::new( if (*it.borrow()) diff --git a/tests/unit/out/refcount/redundant_copy_in_conversion.rs b/tests/unit/out/refcount/redundant_copy_in_conversion.rs index 2e6a6c9d..ea7213a9 100644 --- a/tests/unit/out/refcount/redundant_copy_in_conversion.rs +++ b/tests/unit/out/refcount/redundant_copy_in_conversion.rs @@ -9,7 +9,7 @@ use std::rc::{Rc, Weak}; pub fn sink_0(it: RefcountMapIter) -> i32 { let it: Value> = Rc::new(RefCell::new(it)); let cit: Value> = Rc::new(RefCell::new((*it.borrow()).clone())); - return if (*cit.borrow()) == (*it.borrow()).clone() { + return if (*cit.borrow()) == (*it.borrow()) { (*(*it.borrow()).second().borrow()) } else { 0 @@ -35,13 +35,11 @@ fn main_0() -> i32 { &0, ))); let const_it: Value> = Rc::new(RefCell::new((*it0.borrow()).clone())); - let r: Value = Rc::new(RefCell::new( - if (*const_it.borrow()) == (*end.borrow()).clone() { - 0 - } else { - 1 - }, - )); + let r: Value = Rc::new(RefCell::new(if (*const_it.borrow()) == (*end.borrow()) { + 0 + } else { + 1 + })); (*r.borrow_mut()) += ({ sink_0((*it0.borrow()).clone()) }); (*r.borrow_mut()) += if (*end.borrow()) == (*end.borrow()) { 0 diff --git a/tests/unit/out/refcount/reference_wrapper.rs b/tests/unit/out/refcount/reference_wrapper.rs index b04c0592..b6850a53 100644 --- a/tests/unit/out/refcount/reference_wrapper.rs +++ b/tests/unit/out/refcount/reference_wrapper.rs @@ -39,7 +39,7 @@ impl ByteRepr for Point { pub fn set_0(ref_: Ptr, val: i32) { let ref_: Value> = Rc::new(RefCell::new(ref_)); let val: Value = Rc::new(RefCell::new(val)); - (*ref_.borrow()).clone().write((*val.borrow())); + (*ref_.borrow()).write((*val.borrow())); } pub fn read_1(ref_: Ptr) -> i32 { let ref_: Value> = Rc::new(RefCell::new(ref_)); @@ -52,7 +52,7 @@ pub fn main() { fn main_0() -> i32 { let i1: Value = Rc::new(RefCell::new(10)); let ref_1: Value> = Rc::new(RefCell::new(i1.as_pointer())); - (*ref_1.borrow()).clone().write(20); + (*ref_1.borrow()).write(20); let i2: Ptr = (*ref_1.borrow()).clone(); { let _ptr = i2.clone(); @@ -63,8 +63,8 @@ fn main_0() -> i32 { let i4: Value = Rc::new(RefCell::new(2)); let ref_3: Value> = Rc::new(RefCell::new(i3.as_pointer())); let ref_4: Value> = Rc::new(RefCell::new(i4.as_pointer())); - let __rhs = ((*ref_4.borrow()).clone().read()); - (*ref_3.borrow()).clone().write(__rhs); + let __rhs = ((*ref_4.borrow()).read()); + (*ref_3.borrow()).write(__rhs); write!( libcc2rs::cout(), "{:} {:}\n", @@ -83,12 +83,8 @@ fn main_0() -> i32 { y: Rc::new(RefCell::new(4)), })); let point_ref: Value> = Rc::new(RefCell::new(point.as_pointer())); - (*(*(*point_ref.borrow()).clone().upgrade().deref()) - .x - .borrow_mut()) = 30; - (*(*(*point_ref.borrow()).clone().upgrade().deref()) - .y - .borrow_mut()) = 40; + (*(*(*point_ref.borrow()).upgrade().deref()).x.borrow_mut()) = 30; + (*(*(*point_ref.borrow()).upgrade().deref()).y.borrow_mut()) = 40; write!( libcc2rs::cout(), "{:} {:}\n", diff --git a/tests/unit/out/refcount/simple_index.rs b/tests/unit/out/refcount/simple_index.rs index 664149ea..c8a2e507 100644 --- a/tests/unit/out/refcount/simple_index.rs +++ b/tests/unit/out/refcount/simple_index.rs @@ -15,8 +15,7 @@ fn main_0() -> i32 { ((*(v.as_pointer() as Ptr) .offset(0_usize) .upgrade() - .deref()) - .clone() as bool) + .deref()) as bool) ); return 0; } diff --git a/tests/unit/out/refcount/split_binop_aliased_borrows.rs b/tests/unit/out/refcount/split_binop_aliased_borrows.rs index 897ac869..517116b9 100644 --- a/tests/unit/out/refcount/split_binop_aliased_borrows.rs +++ b/tests/unit/out/refcount/split_binop_aliased_borrows.rs @@ -14,7 +14,7 @@ fn main_0() -> i32 { let p: Value> = Rc::new(RefCell::new((v.as_pointer() as Ptr))); let r: Ptr = (v.as_pointer() as Ptr).offset(1_usize); let __rhs = (r.read()); - (*p.borrow()).clone().write(__rhs); + (*p.borrow()).write(__rhs); assert!((((v.as_pointer() as Ptr).offset(0_usize).read()) == 2)); return 0; } diff --git a/tests/unit/out/refcount/void_return.rs b/tests/unit/out/refcount/void_return.rs index 0cf54c6a..f1173bb2 100644 --- a/tests/unit/out/refcount/void_return.rs +++ b/tests/unit/out/refcount/void_return.rs @@ -9,9 +9,7 @@ use std::rc::{Rc, Weak}; pub fn f1_0(first: Ptr, last: Ptr) { let first: Value> = Rc::new(RefCell::new(first)); let last: Value> = Rc::new(RefCell::new(last)); - (*first.borrow()) - .clone() - .sort((*last.borrow()).clone().get_offset()); + (*first.borrow()).sort((*last.borrow()).get_offset()); return; } pub fn main() { From 089949a72edb66514a92f2ca6fcee7cba0a14ca5 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Tue, 15 Sep 2026 12:44:40 +0100 Subject: [PATCH 15/15] Rename read to move --- cpp2rust/converter/converter.cpp | 2 +- cpp2rust/converter/models/converter_refcount.cpp | 2 +- cpp2rust/converter/translation_rule.cpp | 8 ++++---- cpp2rust/converter/translation_rule.h | 2 +- rule-preprocessor/src/ir.rs | 2 +- rule-preprocessor/src/semantic.rs | 2 +- 6 files changed, 9 insertions(+), 9 deletions(-) diff --git a/cpp2rust/converter/converter.cpp b/cpp2rust/converter/converter.cpp index 1041ba22..031f6e83 100644 --- a/cpp2rust/converter/converter.cpp +++ b/cpp2rust/converter/converter.cpp @@ -4686,7 +4686,7 @@ std::string Converter::ConvertPlaceholder(clang::Expr *expr, clang::Expr *arg, return std::format("std::mem::take(&mut {})", ConvertLValue(arg)); } - if (ph_ctx.access == TranslationRule::Access::kRead) { + if (ph_ctx.access == TranslationRule::Access::kMove) { return ConvertFreshRValue(arg, ph_ctx.implicit_convert_to); } diff --git a/cpp2rust/converter/models/converter_refcount.cpp b/cpp2rust/converter/models/converter_refcount.cpp index 6cbc611a..8b6b7a19 100644 --- a/cpp2rust/converter/models/converter_refcount.cpp +++ b/cpp2rust/converter/models/converter_refcount.cpp @@ -2590,7 +2590,7 @@ std::string ConverterRefCount::ConvertMappedMethodCall( clang::Expr **args, unsigned num_args, TempMaterializationCtx *ctx) { auto receiver_ph = mc.getReceiverPlaceholder(); if (!receiver_ph || receiver_ph->access == TranslationRule::Access::kBorrow || - receiver_ph->access == TranslationRule::Access::kRead) { + receiver_ph->access == TranslationRule::Access::kMove) { return Converter::ConvertMappedMethodCall(expr, mc, args, num_args, ctx); } diff --git a/cpp2rust/converter/translation_rule.cpp b/cpp2rust/converter/translation_rule.cpp index 63c9ef67..8746c60c 100644 --- a/cpp2rust/converter/translation_rule.cpp +++ b/cpp2rust/converter/translation_rule.cpp @@ -39,8 +39,8 @@ Access ParseAccessJSON(llvm::StringRef value) { return Access::kBorrow; } else if (value == "borrow_mut") { return Access::kBorrowMut; - } else if (value == "read") { - return Access::kRead; + } else if (value == "move") { + return Access::kMove; } else if (value == "take") { return Access::kTake; } else { @@ -255,8 +255,8 @@ void PlaceholderFragment::dump() const { case Access::kBorrowMut: log() << " (borrow_mut)\n"; break; - case Access::kRead: - log() << " (read)\n"; + case Access::kMove: + log() << " (move)\n"; break; case Access::kTake: log() << " (take)\n"; diff --git a/cpp2rust/converter/translation_rule.h b/cpp2rust/converter/translation_rule.h index e1adcfe2..ad9d7450 100644 --- a/cpp2rust/converter/translation_rule.h +++ b/cpp2rust/converter/translation_rule.h @@ -23,7 +23,7 @@ struct TextFragment { void dump() const; }; -enum class Access : int8_t { kBorrow, kBorrowMut, kRead, kTake }; +enum class Access : int8_t { kBorrow, kBorrowMut, kMove, kTake }; struct PlaceholderFragment { unsigned n; // "a0", "a1", ... diff --git a/rule-preprocessor/src/ir.rs b/rule-preprocessor/src/ir.rs index 91a1300c..279a22aa 100644 --- a/rule-preprocessor/src/ir.rs +++ b/rule-preprocessor/src/ir.rs @@ -147,7 +147,7 @@ pub enum BodyFragment { pub enum Access { Borrow, BorrowMut, - Read, + Move, Take, Unknown, } diff --git a/rule-preprocessor/src/semantic.rs b/rule-preprocessor/src/semantic.rs index 98880347..3ab2d4d5 100644 --- a/rule-preprocessor/src/semantic.rs +++ b/rule-preprocessor/src/semantic.rs @@ -383,7 +383,7 @@ impl<'a, 'tcx> AstVisitor<'a, 'tcx> { // Reached an argument used inside the rule body if let Some(param) = self.expr_as_decl_ref(expr) { let access = if context == Access::Borrow && self.is_moved(expr) { - Access::Read + Access::Move } else { context };