From 6738fb807960eecb76b96c782d682256956b3b63 Mon Sep 17 00:00:00 2001 From: Nuno Lopes Date: Wed, 30 Sep 2026 09:38:15 +0100 Subject: [PATCH 01/13] Remove Value<> boxing from struct fields --- cpp2rust/converter/converter.cpp | 46 +- cpp2rust/converter/converter.h | 14 +- .../converter/models/converter_refcount.cpp | 875 +++++++++++++++++- .../converter/models/converter_refcount.h | 104 +++ docs/src/codegen/types.md | 20 +- docs/src/codegen/types/boxing.md | 40 +- docs/src/codegen/types/pointers.md | 38 +- docs/src/codegen/types/traits.md | 47 +- docs/src/runtime/rc.md | 93 +- docs/src/runtime/reinterpret.md | 42 +- libcc2rs-macros/src/lib.rs | 17 + libcc2rs-macros/src/record.rs | 94 ++ libcc2rs/src/cstr.rs | 6 + libcc2rs/src/field.rs | 556 +++++++++++ libcc2rs/src/iterators.rs | 2 +- libcc2rs/src/lib.rs | 15 +- libcc2rs/src/rc.rs | 127 ++- libcc2rs/src/reinterpret.rs | 87 +- rules/algorithm/tgt_refcount.rs | 4 +- rules/deque/tgt_refcount.rs | 2 +- rules/vector/tgt_refcount.rs | 22 +- .../out/refcount/cross_tu_tag_collision.rs | 20 +- .../out/refcount/defaulted_move_cross_tu.rs | 65 +- .../out/refcount/friend_operator.rs | 61 +- .../out/refcount/opaque_forward_decl.rs | 34 +- .../refcount/out_of_line_methods_cross_tu.rs | 66 +- .../template_method_late_instantiation.rs | 26 +- tests/ub/out/refcount/ctor_ref_member.rs | 9 +- tests/ub/out/refcount/ub6.rs | 10 +- tests/unit/field_pointers.cpp | 91 ++ tests/unit/out/refcount/07_unique.rs | 4 +- tests/unit/out/refcount/10_struct.rs | 156 ++-- tests/unit/out/refcount/addr_of_global.rs | 85 +- tests/unit/out/refcount/anonymous-struct.rs | 320 +++---- tests/unit/out/refcount/anonymous-struct_c.rs | 317 +++---- tests/unit/out/refcount/anonymous_enum.rs | 52 +- tests/unit/out/refcount/anonymous_enum_c.rs | 54 +- tests/unit/out/refcount/array_const_init.rs | 54 +- .../out/refcount/array_of_noncopy_struct.rs | 43 +- tests/unit/out/refcount/array_reference.rs | 54 +- tests/unit/out/refcount/as_const.rs | 34 +- tests/unit/out/refcount/bounded_struct_ptr.rs | 40 +- tests/unit/out/refcount/bst.rs | 119 +-- tests/unit/out/refcount/c_struct.rs | 198 ++-- tests/unit/out/refcount/class.rs | 147 ++- .../refcount/class_template_lazy_member.rs | 69 +- tests/unit/out/refcount/class_templates.rs | 293 +++--- tests/unit/out/refcount/clone_vs_move.rs | 148 ++- tests/unit/out/refcount/complex_function.rs | 334 +++---- .../out/refcount/compound_assign_paren.rs | 48 +- tests/unit/out/refcount/concepts.rs | 2 +- tests/unit/out/refcount/constexpr_function.rs | 62 +- tests/unit/out/refcount/constructor.rs | 70 +- tests/unit/out/refcount/copy_assign.rs | 201 ++-- tests/unit/out/refcount/copy_ctor.rs | 168 ++-- .../unit/out/refcount/copy_move_defaulted.rs | 600 ++++++------ tests/unit/out/refcount/copy_move_deleted.rs | 150 ++- tests/unit/out/refcount/default.rs | 76 +- .../refcount/default_function_arguments.rs | 30 +- tests/unit/out/refcount/default_in_statics.rs | 286 +++--- tests/unit/out/refcount/destructor.rs | 366 +++++--- tests/unit/out/refcount/doubly_linked_list.rs | 358 ++----- .../out/refcount/enum_default_in_static.rs | 28 +- tests/unit/out/refcount/enum_int_interop.rs | 76 +- tests/unit/out/refcount/enum_int_interop_c.rs | 82 +- tests/unit/out/refcount/exprs.rs | 86 +- tests/unit/out/refcount/fft.rs | 168 ++-- tests/unit/out/refcount/field_pointers.rs | 278 ++++++ tests/unit/out/refcount/fn_ptr_cast.rs | 22 +- tests/unit/out/refcount/fn_ptr_stable_sort.rs | 66 +- tests/unit/out/refcount/fn_ptr_struct.rs | 62 +- .../unit/out/refcount/fn_ptr_typedef_cast.rs | 41 +- tests/unit/out/refcount/fn_ptr_vtable.rs | 66 +- .../refcount/foreach_disjoint_field_borrow.rs | 32 +- tests/unit/out/refcount/forward_basic.rs | 103 ++- tests/unit/out/refcount/forward_decltype.rs | 59 +- tests/unit/out/refcount/forward_explicit.rs | 73 +- tests/unit/out/refcount/forward_pack.rs | 107 ++- tests/unit/out/refcount/friend.rs | 112 +-- .../unit/out/refcount/function_overloading.rs | 2 +- .../out/refcount/global_init_side_effect.rs | 2 +- .../out/refcount/global_non_const_init.rs | 76 +- tests/unit/out/refcount/global_pointers.rs | 48 +- .../refcount/global_without_initializer.rs | 18 +- .../out/refcount/goto_aggregate_default.rs | 24 +- tests/unit/out/refcount/goto_cleanup.rs | 20 +- tests/unit/out/refcount/huffman.rs | 380 ++++---- .../refcount/immutable-deref-on-func-call.rs | 44 +- tests/unit/out/refcount/implicit_autoref.rs | 36 +- .../refcount/in_class_field_initializer.rs | 131 ++- tests/unit/out/refcount/init.rs | 26 +- tests/unit/out/refcount/kruskal.rs | 597 ++++++------ .../refcount/libc_struct_without_default.rs | 54 +- tests/unit/out/refcount/linked_list.rs | 101 +- .../refcount/local_anon_struct_collision.rs | 60 +- .../out/refcount/local_record_template.rs | 109 +-- tests/unit/out/refcount/map.rs | 13 +- .../unit/out/refcount/memcpy_struct_bytes.rs | 33 +- .../unit/out/refcount/memcpy_struct_struct.rs | 74 +- tests/unit/out/refcount/move_assign.rs | 93 +- tests/unit/out/refcount/move_ctor.rs | 219 +++-- tests/unit/out/refcount/move_this.rs | 47 +- tests/unit/out/refcount/nested_structs.rs | 130 +-- tests/unit/out/refcount/new.rs | 34 +- tests/unit/out/refcount/new_bst.rs | 105 +-- tests/unit/out/refcount/new_struct.rs | 98 +- tests/unit/out/refcount/offsetof.rs | 76 +- .../unit/out/refcount/opaque_forward_decl.rs | 32 +- tests/unit/out/refcount/opaque_record_copy.rs | 32 +- .../unit/out/refcount/opaque_then_defined.rs | 66 +- .../out/refcount/operator_arithmetic_free.rs | 254 +++-- .../refcount/operator_arithmetic_member.rs | 187 ++-- .../out/refcount/operator_bitwise_free.rs | 148 ++- .../out/refcount/operator_bitwise_member.rs | 87 +- .../refcount/operator_comparison_defaulted.rs | 345 +++---- .../out/refcount/operator_comparison_free.rs | 96 +- .../refcount/operator_comparison_member.rs | 56 +- .../out/refcount/operator_comparison_mixed.rs | 48 +- .../operator_comparison_noncopyable.rs | 21 +- .../operator_compound_assignment_free.rs | 91 +- .../operator_compound_assignment_member.rs | 101 +- tests/unit/out/refcount/operator_friend.rs | 269 ++---- tests/unit/out/refcount/operator_less_than.rs | 48 +- .../out/refcount/operator_logical_free.rs | 34 +- .../out/refcount/operator_logical_member.rs | 34 +- .../refcount/operator_member_pointer_free.rs | 78 +- .../operator_member_pointer_member.rs | 106 +-- .../unit/out/refcount/operator_other_free.rs | 74 +- .../out/refcount/operator_other_member.rs | 79 +- tests/unit/out/refcount/operator_overloads.rs | 58 +- .../unit/out/refcount/operator_qualifiers.rs | 68 +- tests/unit/out/refcount/operator_three_way.rs | 38 +- tests/unit/out/refcount/operator_traits.rs | 351 +++---- tests/unit/out/refcount/overload_mangling.rs | 91 +- tests/unit/out/refcount/pod.rs | 58 +- tests/unit/out/refcount/pointer_array.rs | 36 +- tests/unit/out/refcount/pointers.rs | 43 +- tests/unit/out/refcount/polymorphism.rs | 4 +- tests/unit/out/refcount/printfs.rs | 2 +- tests/unit/out/refcount/ptr_cast_init.rs | 47 +- .../out/refcount/ptr_to_incomplete_struct.rs | 2 +- tests/unit/out/refcount/push_emplace_back.rs | 262 +++--- tests/unit/out/refcount/random.rs | 308 +++--- tests/unit/out/refcount/reference_wrapper.rs | 44 +- tests/unit/out/refcount/refs_as_args.rs | 24 +- .../out/refcount/reinterpret_cast_struct.rs | 30 +- .../reinterpret_cast_struct_to_struct.rs | 64 +- tests/unit/out/refcount/reserved_keywords.rs | 390 ++++---- tests/unit/out/refcount/rule_of_five.rs | 102 +- tests/unit/out/refcount/rule_of_three.rs | 73 +- tests/unit/out/refcount/rvalue_struct.rs | 34 +- .../scalar_builtin_construct_destruct.rs | 24 +- .../unit/out/refcount/static_var_in_class.rs | 4 +- tests/unit/out/refcount/strdup.rs | 33 +- .../out/refcount/string_literal_ptr_init.rs | 122 ++- tests/unit/out/refcount/struct_ctor.rs | 94 +- .../unit/out/refcount/struct_default_ctor.rs | 53 +- tests/unit/out/refcount/struct_ptr.rs | 26 +- .../refcount/tag_vs_identifier_collision.rs | 127 +-- .../unit/out/refcount/template_default_arg.rs | 102 +- tests/unit/out/refcount/this.rs | 142 ++- .../unit/out/refcount/typedef-anon-struct.rs | 71 +- .../out/refcount/unary_expr_or_type_trait.rs | 26 +- .../out/refcount/union_addrof_external.rs | 100 +- .../unit/out/refcount/union_cross_arm_cast.rs | 165 ++-- .../out/refcount/union_field_alignment.rs | 31 +- .../out/refcount/union_flex_array_member.rs | 63 +- .../unit/out/refcount/union_memset_memcpy.rs | 150 +-- tests/unit/out/refcount/union_nested.rs | 139 ++- .../out/refcount/union_pointer_pun_address.rs | 44 +- .../out/refcount/union_struct_dual_use.rs | 75 +- .../out/refcount/union_tagged_many_arms.rs | 66 +- .../unit/out/refcount/union_tagged_simple.rs | 54 +- .../out/refcount/union_tagged_struct_arms.rs | 307 +++--- .../refcount/union_void_ptr_sized_deref.rs | 68 +- tests/unit/out/refcount/unique_ptr.rs | 169 ++-- .../out/refcount/unique_ptr_const_deref.rs | 37 +- .../unit/out/refcount/unique_ptr_deref_ops.rs | 6 +- tests/unit/out/refcount/unique_ptr_nested.rs | 89 +- tests/unit/out/refcount/unique_ptr_struct.rs | 41 +- tests/unit/out/refcount/va_arg_mixed_types.rs | 31 +- .../out/refcount/va_arg_non_primitive_ptrs.rs | 38 +- tests/unit/out/refcount/va_arg_struct_ctx.rs | 40 +- tests/unit/out/refcount/va_arg_void_ptr.rs | 42 +- .../out/refcount/vector_subscript_direct.rs | 210 +++++ .../out/refcount/vector_with_allocator.rs | 4 +- tests/unit/out/refcount/void_cast.rs | 80 +- tests/unit/out/unsafe/field_pointers.rs | 150 +++ .../out/unsafe/vector_subscript_direct.rs | 91 ++ .../reinterpret_cast_struct_to_struct.cpp | 2 - tests/unit/union_struct_dual_use.c | 1 - tests/unit/vector_subscript_direct.cpp | 84 ++ 192 files changed, 10009 insertions(+), 9289 deletions(-) create mode 100644 libcc2rs-macros/src/record.rs create mode 100644 libcc2rs/src/field.rs create mode 100644 tests/unit/field_pointers.cpp create mode 100644 tests/unit/out/refcount/field_pointers.rs create mode 100644 tests/unit/out/refcount/vector_subscript_direct.rs create mode 100644 tests/unit/out/unsafe/field_pointers.rs create mode 100644 tests/unit/out/unsafe/vector_subscript_direct.rs create mode 100644 tests/unit/vector_subscript_direct.cpp diff --git a/cpp2rust/converter/converter.cpp b/cpp2rust/converter/converter.cpp index e1317c41fc..f7622950d3 100644 --- a/cpp2rust/converter/converter.cpp +++ b/cpp2rust/converter/converter.cpp @@ -1171,7 +1171,6 @@ void Converter::EmitConstructorFieldInits(clang::CXXConstructorDecl *decl) { for (const auto *field : record_decl->fields()) { auto field_name = GetNamedDeclAsString(field); - auto field_type = field->getType(); const clang::CXXCtorInitializer *ctor_initializer = nullptr; for (const auto *init : definition->inits()) { if (init->isMemberInitializer() && init->getMember() == field) { @@ -1180,15 +1179,9 @@ void Converter::EmitConstructorFieldInits(clang::CXXConstructorDecl *decl) { } } - if (ctor_initializer) { - StrCat(field_name, token::kColon); - ConvertVarInit(field_type, ctor_initializer->getInit()); - } else if (auto *init = field->getInClassInitializer()) { - StrCat(field_name, token::kColon); - ConvertVarInit(field_type, init); - } else { - StrCat(field_name, token::kColon, GetDefaultAsString(field_type)); - } + StrCat(field_name, token::kColon); + ConvertFieldInit(field, ctor_initializer ? ctor_initializer->getInit() + : field->getInClassInitializer()); StrCat(token::kComma); } } @@ -2444,7 +2437,9 @@ bool Converter::VisitImplicitCastExpr(clang::ImplicitCastExpr *expr) { case clang::CastKind::CK_LValueToRValue: { PushExprKind push(*this, ExprKind::RValue); Convert(sub_expr); - SetValueFreshness(type); + if (!isFresh()) { + SetValueFreshness(type); + } break; } case clang::CastKind::CK_ArrayToPointerDecay: { @@ -3398,7 +3393,7 @@ bool Converter::VisitInitListExpr(clang::InitListExpr *expr) { int i = 0; for (const auto *field : record->fields()) { StrCat(GetNamedDeclAsString(field), token::kColon); - ConvertVarInit(field->getType(), expr->getInit(i++)); + ConvertFieldInit(field, expr->getInit(i++)); StrCat(token::kComma); } } else { @@ -4665,15 +4660,20 @@ void Converter::EmitDefaultStructLiteral(const clang::RecordDecl *decl) { PushBrace brace(*this); for (auto *field : decl->fields()) { StrCat(GetNamedDeclAsString(field), token::kColon); - if (auto *init = field->getInClassInitializer()) { - ConvertVarInit(field->getType(), init); - } else { - StrCat(GetDefaultAsString(field->getType())); - } + ConvertFieldInit(field, field->getInClassInitializer()); StrCat(token::kComma); } } +void Converter::ConvertFieldInit(const clang::FieldDecl *field, + clang::Expr *init) { + if (init) { + ConvertVarInit(field->getType(), init); + } else { + StrCat(GetDefaultAsString(field->getType())); + } +} + void Converter::AddByteReprTrait(const clang::RecordDecl *decl) {} void Converter::ConvertUnsignedArithBinaryOperator(clang::BinaryOperator *op, @@ -4838,8 +4838,16 @@ void Converter::PlaceholderCtx::dump() const { << ", materialize_idx: " << materialize_idx << '\n'; } +std::string Converter::ConvertTake(clang::Expr *arg) { + return std::format("std::mem::take(&mut {})", ConvertLValue(arg)); +} + std::string Converter::ConvertPlaceholder(clang::Expr *expr, clang::Expr *arg, const PlaceholderCtx &ph_ctx) { + if (auto it = hoisted_exprs_.find(arg); it != hoisted_exprs_.end()) { + return it->second; + } + if (arg->getType()->isFunctionPointerType()) { return ConvertFnPtrPlaceholder(arg); } @@ -4907,9 +4915,9 @@ std::string Converter::ConvertPlaceholder(clang::Expr *expr, clang::Expr *arg, } return ConvertFreshRValue(arg); } - auto lvalue = ConvertLValue(arg); + auto str = ConvertTake(arg); SetFresh(); - return std::format("std::mem::take(&mut {})", std::move(lvalue)); + return str; } if (ph_ctx.access == TranslationRule::Access::kMove) { diff --git a/cpp2rust/converter/converter.h b/cpp2rust/converter/converter.h index 6d71708bd0..64cfe40a5f 100644 --- a/cpp2rust/converter/converter.h +++ b/cpp2rust/converter/converter.h @@ -340,6 +340,9 @@ class Converter : public clang::RecursiveASTVisitor { std::string ConvertFnPtrCallee(clang::Expr *arg); virtual std::string ConvertFnPtrPlaceholder(clang::Expr *arg); + // Moves the value of the lvalue arg out, leaving a default value behind. + virtual std::string ConvertTake(clang::Expr *arg); + // Option implements Copy virtual bool FunctionPointerImplementsCopy() const { return true; } @@ -597,6 +600,11 @@ class Converter : public clang::RecursiveASTVisitor { virtual void ConvertVarInit(clang::QualType qual_type, clang::Expr *expr); + // The initial value of a field of a struct: `init`, or the default value + // if null. + virtual void ConvertFieldInit(const clang::FieldDecl *field, + clang::Expr *init); + virtual void ConvertUnsignedArithOperand(clang::Expr *expr, clang::QualType type); @@ -696,7 +704,7 @@ class Converter : public clang::RecursiveASTVisitor { unsigned num_args = 0, TempMaterializationCtx *ctx = nullptr); - std::string + virtual std::string ConvertIRFragment(const std::vector &fragments, clang::Expr *expr, clang::Expr **args, unsigned num_args, TempMaterializationCtx *ctx); @@ -760,6 +768,10 @@ class Converter : public clang::RecursiveASTVisitor { }; std::string ufcs_receiver_; + + // Expressions that were evaluated into local variables ahead of the + // expression that contains them, and the names of these variables. + std::unordered_map hoisted_exprs_; bool in_const_initializer_ = false; std::optional autoref_mut_; bool suppress_iterator_clone_ = false; diff --git a/cpp2rust/converter/models/converter_refcount.cpp b/cpp2rust/converter/models/converter_refcount.cpp index 48d75b3053..8f48dcb694 100644 --- a/cpp2rust/converter/models/converter_refcount.cpp +++ b/cpp2rust/converter/models/converter_refcount.cpp @@ -58,7 +58,8 @@ static bool IsBoxedType(clang::ASTContext &ctx, clang::QualType type) { static bool NeedsMutAccess(clang::ASTContext &ctx, const clang::CXXMethodDecl *method, clang::QualType base_type) { - return !method->isConst() && IsBoxedType(ctx, base_type); + return !method->isConst() && + (IsBoxedType(ctx, base_type) || method->isVirtual()); } static bool IsPointerType(clang::ASTContext &ctx, clang::QualType type) { @@ -66,6 +67,63 @@ static bool IsPointerType(clang::ASTContext &ctx, clang::QualType type) { IteratorCategory::Contiguous; } +static bool IsTranslatedStruct(const clang::RecordDecl *decl) { + return decl && !decl->isUnion() && IsUserDefinedDecl(decl); +} + +// Whether evaluating expr doesn't access memory other than local variables. +static bool IsTrivialValue(const clang::Expr *expr) { + expr = expr->IgnoreParenImpCasts(); + if (clang::isa(expr)) { + return true; + } + auto *ref = clang::dyn_cast(expr); + auto *var = ref ? clang::dyn_cast(ref->getDecl()) : nullptr; + return (ref && clang::isa(ref->getDecl())) || + (var && var->isLocalVarDeclOrParm() && + !var->getType()->isReferenceType()); +} + +// Whether expr accesses a field of a struct that is stored in the struct. +static bool IsFieldAccess(const clang::Expr *expr) { + auto *member = clang::dyn_cast(expr); + auto *field = + member ? clang::dyn_cast(member->getMemberDecl()) + : nullptr; + return field && IsTranslatedStruct(field->getParent()) && + !field->getType()->isReferenceType(); +} + +// Whether the field holds a Value of its own, instead of being stored in the +// struct. Vectors are, so that pointers to their elements, and to the fields +// of those, point into the vector and not the struct. +static bool IsValueField(clang::ASTContext &ctx, + const clang::ValueDecl *decl) { + auto *field = clang::dyn_cast(decl); + return field && IsTranslatedStruct(field->getParent()) && + !field->getType()->isReferenceType() && + Mapper::Map(ctx, field->getType().getUnqualifiedType()) + .starts_with("Vec<"); +} + +// Whether evaluating expr reads a field of a struct, and hence borrows the +// struct until the end of the statement. +static bool ReadsField(const clang::Stmt *stmt) { + if (!stmt) { + return false; + } + if (auto *member = clang::dyn_cast(stmt)) { + if (auto *field = + clang::dyn_cast(member->getMemberDecl()); + field && IsTranslatedStruct(field->getParent())) { + return true; + } + } + return std::ranges::any_of(stmt->children(), ReadsField); +} + bool ConverterRefCount::PendingDeref::compute_inner_boxed( clang::Expr *expr) const { if (!expr) { @@ -357,6 +415,27 @@ std::string ConverterRefCount::ConvertFresh( std::string ConverterRefCount::ConvertFreshRValue( clang::Expr *expr, std::optional implicit_convert_to) { + if (auto str = ConvertInStructPtr(expr, false, [&] { + return ConvertFreshRValue(expr, implicit_convert_to); + })) { + SetFreshType(expr->getType()); + return *str; + } + // A copy of a struct pointed to. + if (auto *object = expr->IgnoreParenImpCasts(); + IsTranslatedStruct(expr->getType()->getAsRecordDecl()) && + ((clang::isa(object) && + clang::cast(object) + ->getDecl() + ->getType() + ->isReferenceType()) || + (clang::isa(object) && + clang::cast(object)->getOpcode() == + clang::UO_Deref))) { + auto ptr = ConvertPointer(object); + SetFreshType(expr->getType()); + return std::format("{}.read()", ptr); + } auto str = ConvertRValue(expr, implicit_convert_to); if (!isFresh() && !expr->getType()->isVoidType()) { SetFresh(); @@ -403,9 +482,35 @@ std::string ConverterRefCount::ConvertPtrType(clang::QualType type) { return std::format("Ptr<{}>", std::move(str)); } +bool ConverterRefCount::ConvertWithHoistedIndex( + clang::Expr *expr, clang::Expr *base, clang::Expr *idx, + llvm::function_ref convert) { + // The value of the whole block must be fresh. + if (isLValue() || (!isAddrOf() && !TypeIsCopyable(expr->getType())) || + hoisted_exprs_.contains(idx) || !idx->HasSideEffects(ctx_) || + !ReadsField(base)) { + return false; + } + auto idx_str = ConvertRValue(idx); + hoisted_exprs_.emplace(idx, "__idx"); + std::string str; + { + Buffer buf(*this); + convert(); + str = std::move(buf).str(); + } + hoisted_exprs_.erase(idx); + StrCat(std::format("({{ let __idx = {}; {} }})", idx_str, str)); + return true; +} + bool ConverterRefCount::VisitArraySubscriptExpr( clang::ArraySubscriptExpr *expr) { auto *base = expr->getBase(); + if (ConvertWithHoistedIndex(expr, base, expr->getIdx(), + [&] { VisitArraySubscriptExpr(expr); })) { + return false; + } if (base->IgnoreCasts()->getType()->isPointerType() || IsUnionArrayMember(base) || (IsReferenceType(base) && @@ -463,16 +568,65 @@ ConverterRefCount::GetComparisonReceiver(const clang::CXXMethodDecl *, return std::format("&{}.as_pointer()", BoxValue(GetShallowCopy(decl, lhs))); } +std::string ConverterRefCount::DetachPtr(std::string ptr, bool fresh, + const clang::Expr *expr) { + if (auto trimmed = Trim(ptr); + !ReadsField(expr) || + (fresh && trimmed.starts_with('{') && trimmed.ends_with('}'))) { + return ptr; + } + // The temporaries of the tail expression of a block are dropped at the end + // of the block. + return std::format("{{ {}{} }}", ptr, fresh ? "" : ".clone()"); +} + +std::string +ConverterRefCount::TakePendingDerefToWrite(const clang::Expr *lvalue) { + bool fresh = pending_deref_.is_fresh(); + return DetachPtr(pending_deref_.take(), fresh, lvalue); +} + +// A copy of `src` that doesn't run the copy constructors of its fields, and +// hence has no side effects. It stands for `src` itself where a pointer to it +// is needed, but only a reference is available. std::string ConverterRefCount::GetShallowCopy(const clang::RecordDecl *decl, std::string_view src) { std::string fields; for (auto *field : decl->fields()) { auto name = GetNamedDeclAsString(field); - fields += std::format("{0}: {1}.{0}.clone(),", name, src); + auto type = field->getType(); + auto value = std::format("{}.{}", src, name); + if (auto *record = type->getAsRecordDecl(); IsTranslatedStruct(record)) { + value = GetShallowCopy(record, value); + } else if (auto *arr = ctx_.getAsConstantArrayType(type); + arr && IsTranslatedStruct( + arr->getElementType()->getAsRecordDecl())) { + auto elem = std::format("__e{}", src.size()); + value = std::format( + "{}.iter().map(|{}| {}).collect()", value, elem, + GetShallowCopy(arr->getElementType()->getAsRecordDecl(), elem)); + } else { + value += ".clone()"; + } + fields += std::format("{}: {},", name, value); } return std::format("{} {{ {} }}", GetRecordName(decl), fields); } +// A copy of `src`, of type `type`, that copies the arrays nested in Values +// instead of sharing them. +std::string ConverterRefCount::GetDeepCopy(clang::QualType type, + std::string_view src) { + auto *arr = ctx_.getAsConstantArrayType(type); + if (!arr || !arr->getElementType()->isArrayType()) { + return std::format("{}.clone()", src); + } + auto elem = std::format("__e{}", src.size()); + return std::format( + "{}.iter().map(|{}| Rc::new(RefCell::new({}))).collect()", src, elem, + GetDeepCopy(arr->getElementType(), std::format("(*{}.borrow())", elem))); +} + bool ConverterRefCount::RecordImplementsClone(const clang::RecordDecl *decl) { if (decl->isUnion() || (HasDefaultedCopyConstructor(decl) && RecordHasOnlyReferenceFields(decl))) { @@ -482,6 +636,27 @@ bool ConverterRefCount::RecordImplementsClone(const clang::RecordDecl *decl) { HasCallableCopyConstructor(decl); } +bool ConverterRefCount::RecordDerivesClone(const clang::RecordDecl *decl) { + if (decl->isUnion()) { + return false; + } + if (HasDefaultedCopyConstructor(decl) && RecordHasOnlyReferenceFields(decl)) { + return true; + } + // A C struct is copied field by field, as is a C++ one with an implicit or + // defaulted copy constructor. Nested Values must be copied deeply, though. + if (auto *cxx = clang::dyn_cast(decl); + cxx && (!HasCallableCopyConstructor(cxx) || + GetUserDefinedCopyConstructor(cxx) || cxx->getNumBases() > 0)) { + return false; + } + PushConversionKind push(*this, ConversionKind::Pointee); + return std::ranges::none_of(decl->fields(), [&](auto *field) { + return IsValueField(ctx_, field) || + ToString(field->getType()).contains("Value<"); + }); +} + void ConverterRefCount::AddCloneTrait(const clang::RecordDecl *decl) { auto record_name = GetRecordName(decl); @@ -495,11 +670,12 @@ void ConverterRefCount::AddCloneTrait(const clang::RecordDecl *decl) { return; } - if (HasDefaultedCopyConstructor(decl) && RecordHasOnlyReferenceFields(decl)) { + if (RecordDerivesClone(decl)) { return; } auto *cxx = clang::dyn_cast(decl); if (!cxx) { + // A C struct with nested Values, which are copied deeply. StrCat(keyword::kImpl, "Clone for", record_name); PushBrace impl_brace(*this); StrCat("fn clone(&self) -> Self"); @@ -508,8 +684,8 @@ void ConverterRefCount::AddCloneTrait(const clang::RecordDecl *decl) { PushBrace init_brace(*this); for (auto *field : decl->fields()) { auto name = GetNamedDeclAsString(field); - StrCat(std::format( - "{0}: Rc::new(RefCell::new((*self.{0}.borrow()).clone())),", name)); + StrCat(std::format("{}: {},", name, + GetDeepCopy(field->getType(), "self." + name))); } return; } @@ -542,7 +718,7 @@ void ConverterRefCount::AddCloneTrait(const clang::RecordDecl *decl) { } void ConverterRefCount::AddDefaultTrait(const clang::RecordDecl *decl) { - PushConversionKind push(*this, ConversionKind::FullRefCount); + PushConversionKind push(*this, ConversionKind::Pointee); Converter::AddDefaultTrait(decl); } @@ -597,8 +773,13 @@ void ConverterRefCount::AddByteReprTrait(const clang::RecordDecl *decl) { auto struct_name = GetRecordName(decl); if (!TypeImplementsByteRepr(ctx_.getCanonicalTagType(decl))) { + // The size locates the fields of arrays of structs. StrCat(std::format("impl ByteRepr for {}", struct_name)); PushBrace brace(*this); + if (!decl->isUnion()) { + StrCat(std::format("fn byte_size() -> usize {{ {} }}", + ctx_.getTypeSize(ctx_.getCanonicalTagType(decl)) / 8)); + } return; } @@ -628,7 +809,7 @@ void ConverterRefCount::AddByteReprTrait(const clang::RecordDecl *decl) { for (auto *field : decl->fields()) { auto byte_off = layout.getFieldOffset(idx) / 8; auto byte_size = ctx_.getTypeSize(field->getType()) / 8; - StrCat(std::format("(*self.{}.borrow()).to_bytes(&mut buf[{}..{}]);", + StrCat(std::format("self.{}.to_bytes(&mut buf[{}..{}]);", GetNamedDeclAsString(field), byte_off, byte_off + byte_size)); ++idx; @@ -644,13 +825,13 @@ void ConverterRefCount::AddByteReprTrait(const clang::RecordDecl *decl) { for (auto *field : decl->fields()) { auto byte_off = layout.getFieldOffset(idx) / 8; auto byte_size = ctx_.getTypeSize(field->getType()) / 8; - PushConversionKind push(*this, ConversionKind::FullRefCount); - std::string storage_ty = ToString(field->getType()); - Unwrap(storage_ty, "Value<", ">"); - StrCat(std::format( - "{}: Rc::new(RefCell::new(<{}>::from_bytes(&buf[{}..{}]))),", - GetNamedDeclAsString(field), storage_ty, byte_off, - byte_off + byte_size)); + PushConversionKind push(*this, IsValueField(ctx_, field) + ? ConversionKind::FullRefCount + : ConversionKind::Pointee); + StrCat(std::format("{}: <{}>::from_bytes(&buf[{}..{}]),", + GetNamedDeclAsString(field), + ToString(field->getType()), byte_off, + byte_off + byte_size)); ++idx; } } @@ -658,7 +839,11 @@ void ConverterRefCount::AddByteReprTrait(const clang::RecordDecl *decl) { std::string ConverterRefCount::GetSelfMaybeWithMut(const clang::CXXMethodDecl *decl) { - return "&self"; + // Methods that are not on Ptr, like virtual ones, write to the fields + // through self. + return NeedsMutAccess(ctx_, decl, decl->getThisType()->getPointeeType()) + ? "&mut self" + : "&self"; } bool ConverterRefCount::VisitCXXConstructorDecl( @@ -668,10 +853,38 @@ bool ConverterRefCount::VisitCXXConstructorDecl( } bool ConverterRefCount::VisitFieldDecl(clang::FieldDecl *decl) { - PushConversionKind push(*this, ConversionKind::FullRefCount); + if (!decl->getParent()->isUnion()) { + const auto &layout = ctx_.getASTRecordLayout(decl->getParent()); + StrCat(std::format("#[offset({})]", + layout.getFieldOffset(decl->getFieldIndex()) / 8)); + } + PushConversionKind push(*this, IsValueField(ctx_, decl) + ? ConversionKind::FullRefCount + : ConversionKind::Pointee); return Converter::VisitFieldDecl(decl); } +void ConverterRefCount::ConvertFieldInit(const clang::FieldDecl *field, + clang::Expr *init) { + if (!IsValueField(ctx_, field)) { + Converter::ConvertFieldInit(field, init); + return; + } + PushConversionKind push(*this, ConversionKind::FullRefCount); + if (!init || !ReadsField(init)) { + Converter::ConvertFieldInit(field, init); + return; + } + // Like in ConvertVarInit, the initializer must not keep a struct borrowed. + std::string str; + { + Buffer buf(*this); + Converter::ConvertFieldInit(field, init); + str = std::move(buf).str(); + } + StrCat(std::format("{{ {} }}", str)); +} + void ConverterRefCount::EmitFunctionPreamble(clang::FunctionDecl *decl) { // In the header, the function might be declared as `int foo(int name_1)', // while in the source file the function might be defined as `int foo(int @@ -812,9 +1025,17 @@ bool ConverterRefCount::ConvertIncAndDec(clang::UnaryOperator *expr) { return false; } + if (auto str = ConvertInStructPtr(sub_expr, true, [&] { + return std::format("{}.{}()", ConvertLValue(sub_expr), method); + })) { + StrCat(*str); + SetFreshType(expr->getType()); + return true; + } auto str = ConvertLValue(sub_expr); if (!pending_deref_.empty()) { - StrCat(pending_deref_.take(), ".with_mut(|__v| __v.", method, "())"); + StrCat(TakePendingDerefToWrite(sub_expr), ".with_mut(|__v| __v.", method, + "())"); } else { StrCat(str, '.', method, "()"); } @@ -1663,14 +1884,11 @@ bool ConverterRefCount::VisitInitListExpr(clang::InitListExpr *expr) { { PushBrace brace(*this); unsigned i = 0; - PushConversionKind push(*this, ConversionKind::FullRefCount); + PushConversionKind push(*this, ConversionKind::Pointee); for (const auto *field : record->fields()) { StrCat(GetNamedDeclAsString(field), token::kColon); - if (i < expr->getNumInits()) { - ConvertVarInit(field->getType(), expr->getInit(i++)); - } else { - StrCat(GetDefaultAsString(field->getType())); - } + ConvertFieldInit(field, i < expr->getNumInits() ? expr->getInit(i++) + : nullptr); StrCat(token::kComma); } } @@ -1763,9 +1981,7 @@ bool ConverterRefCount::VisitMemberExpr(clang::MemberExpr *expr) { SetFreshType(expr->getType()); return false; } - // User-defined types have Value fields; the struct itself is read-only - // and only needs an immutable borrow. Non-user-defined types (STL) - // need a mutable borrow for non-const methods + // Non-const methods need a mutable borrow auto base_type = expr->getBase()->getType().getNonReferenceType(); if (base_type->isPointerType()) { base_type = base_type->getPointeeType(); @@ -1784,6 +2000,12 @@ bool ConverterRefCount::VisitMemberExpr(clang::MemberExpr *expr) { return false; } + if (auto *field = clang::dyn_cast(member); + field && !known && IsUserDefinedDecl(field->getParent())) { + ConvertFieldAccess(expr); + return false; + } + std::string str; if (known) { str = GetMappedAsString(expr); @@ -1820,6 +2042,313 @@ bool ConverterRefCount::VisitMemberExpr(clang::MemberExpr *expr) { return false; } +std::string ConverterRefCount::ConvertRecordPlace(clang::MemberExpr *expr, + bool mut) { + auto *base = expr->getBase(); + if (clang::isa(base->IgnoreCasts()) && + !ThisIsRustPtr()) { + return std::string(keyword::kSelfValue); + } + if (expr->isArrow()) { + Buffer buf(*this); + PushExprKind push(*this, mut ? ExprKind::LValue : ExprKind::RValue); + ConvertArrow(base); + return std::move(buf).str(); + } + if (!mut) { + return ConvertRValue(base); + } + auto str = ConvertLValue(base); + if (!pending_deref_.empty()) { + return std::format("(*{}.upgrade().deref_mut())", + TakePendingDerefToWrite(base)); + } + return str; +} + +// Whether expr is (a subobject of) the parameter of a defaulted copy +// constructor, which is translated as the `self` of Clone::clone. +static bool IsCloneSource(const clang::Expr *expr) { + while (true) { + expr = expr->IgnoreParenImpCasts(); + if (auto *member = clang::dyn_cast(expr); + member && !member->isArrow()) { + expr = member->getBase(); + } else if (auto *subscript = + clang::dyn_cast(expr)) { + expr = subscript->getBase(); + } else if (auto *opaque = clang::dyn_cast(expr)) { + expr = opaque->getSourceExpr(); + } else { + break; + } + } + auto *ref = clang::dyn_cast(expr); + auto *param = ref ? clang::dyn_cast(ref->getDecl()) + : nullptr; + auto *ctor = param ? clang::dyn_cast( + param->getDeclContext()) + : nullptr; + return ctor && ctor->isCopyConstructor() && ctor->isDefaulted() && + param->getName().empty(); +} + +// Whether the object is accessed through a pointer: a reference, an element +// of an array pointed to, or a union member (whose accessor returns a +// pointer). +static bool IsPointee(clang::Expr *object) { + if (auto *subscript = clang::dyn_cast(object)) { + auto *base = subscript->getBase()->IgnoreImpCasts(); + return base->getType()->isPointerType() || + base->getType()->isReferenceType() || + (clang::isa(base) && + clang::cast(base) + ->getDecl() + ->getType() + ->isReferenceType()); + } + if (auto *member = clang::dyn_cast(object)) { + auto *record = clang::dyn_cast( + member->getMemberDecl()->getDeclContext()); + return record && record->isUnion(); + } + return false; +} + +std::optional +ConverterRefCount::FindStructPtr(clang::Expr *place, bool mut) { + // Indexes are evaluated in the closure, unless hoisted before it. + auto unsafe_index = [&](clang::Expr *idx) { + return !hoisted_exprs_.contains(idx) && + (idx->HasSideEffects(ctx_) || (mut && ReadsField(idx))); + }; + // An element of a container that is a field, which is indexed in place. + auto field_subscript = [&](clang::Expr *e) -> clang::CXXOperatorCallExpr * { + auto *op = clang::dyn_cast(e); + if (op && op->getOperator() == clang::OO_Subscript && + IsFieldAccess(op->getArg(0)->IgnoreImpCasts()) && + ((IsBoxedType(ctx_, op->getArg(0)->getType()) && + !IsBoxedType(ctx_, op->getType().getNonReferenceType())) || + IsUniquePtr(op->getArg(0)->getType()))) { + return op; + } + return nullptr; + }; + auto *expr = place->IgnoreParenLValueCasts(); + bool in_value_field = false; + while (true) { + // Elements of arrays and containers that are fields. + if (auto *subscript = clang::dyn_cast(expr)) { + if (!subscript->getBase()->IgnoreImpCasts()->getType()->isArrayType() || + unsafe_index(subscript->getIdx())) { + return std::nullopt; + } + expr = subscript->getBase()->IgnoreParenImpCasts(); + continue; + } + if (auto *op = field_subscript(expr)) { + if (unsafe_index(op->getArg(1))) { + return std::nullopt; + } + expr = op->getArg(0)->IgnoreImpCasts(); + continue; + } + + auto *member = clang::dyn_cast(expr); + if (!member || !IsFieldAccess(member)) { + return std::nullopt; + } + in_value_field |= IsValueField(ctx_, member->getMemberDecl()); + if (field_places_.contains(member)) { + return StructPtr{.member = member, .nested = true}; + } + auto *base = member->getBase(); + auto *object = base->IgnoreParenImpCasts(); + // In methods that are not on Ptr, and in Clone, the struct is a + // reference. Smart pointers own the struct, which is borrowed directly. + if ((clang::isa(object) && !ThisIsRustPtr()) || + IsCloneSource(object) || + (member->isArrow() && clang::isa(object))) { + return std::nullopt; + } + if (member->isArrow()) { + return StructPtr{ + .member = member, .ptr = base, .in_value_field = in_value_field}; + } + if (auto *deref = clang::dyn_cast(object); + deref && deref->getOpcode() == clang::UO_Deref) { + return StructPtr{.member = member, + .ptr = deref->getSubExpr(), + .in_value_field = in_value_field}; + } + if (auto *op = field_subscript(object); + op && !unsafe_index(op->getArg(1))) { + expr = object; + continue; + } + if (IsReferenceType(object) || IsPointee(object)) { + return StructPtr{.member = member, + .ptr = base, + .via_reference = true, + .in_value_field = in_value_field}; + } + expr = object; + } +} + +std::optional +ConverterRefCount::ConvertInStructPtr( + clang::Expr *place, bool mut, llvm::function_ref convert) { + auto sp = FindStructPtr(place, mut); + if (!sp || sp->nested) { + return std::nullopt; + } + auto ptr = + sp->via_reference ? ConvertPointer(sp->ptr) : ConvertRValue(sp->ptr); + auto *record = + clang::cast(sp->member->getMemberDecl())->getParent(); + field_places_.emplace( + sp->member, + std::format("__s.{}", GetNamedDeclAsString(sp->member->getMemberDecl()))); + auto body = convert(); + field_places_.erase(sp->member); + // A Value field is written through a Value of its own. + mut &= !sp->in_value_field; + return std::format("{}.{}(|__s: &{}{}| {})", ptr, mut ? "with_mut" : "with", + mut ? "mut " : "", GetRecordName(record), body); +} + +std::string ConverterRefCount::ConvertFieldPointer(clang::MemberExpr *expr) { + auto *base = expr->getBase(); + auto *object = base->IgnoreParenImpCasts(); + std::string base_ptr; + if (expr->isArrow() && !clang::isa(object)) { + base_ptr = ConvertRValue(base); + } else if (auto *ref = clang::dyn_cast(object); + ref && clang::isa(ref->getDecl()) && + !IsGlobalVar(ref) && !IsReferenceType(ref)) { + // The Value of a local variable. + base_ptr = ConvertDeclRef(ref, ref->getDecl()); + } else { + base_ptr = ConvertPointer(base); + } + auto str = std::format("field_ptr!({}, {})", base_ptr, + GetNamedDeclAsString(expr->getMemberDecl())); + + // Pointers to arrays and containers either point to the whole object or + // to its elements. The type is inferred from the context, like for + // `as_pointer()`, unless we know which one is wanted. + if (isObject()) { + auto field_type = expr->getMemberDecl()->getType(); + if (WantsElementPtr()) { + return std::format("({} as {})", str, ConvertPtrType(field_type)); + } + PushConversionKind push(*this, ConversionKind::Pointee); + return std::format("({} as Ptr<{}>)", str, ToString(field_type)); + } + return str; +} + +void ConverterRefCount::ConvertFieldAccess(clang::MemberExpr *expr) { + auto *member = expr->getMemberDecl(); + auto field_type = member->getType(); + auto name = GetNamedDeclAsString(member); + + if (field_type->isReferenceType()) { + // The field holds a pointer to the referenced object. + auto str = std::format("{}.{}", ConvertRecordPlace(expr, /*mut=*/false), + name); + if (isAddrOf()) { + StrCat(str); + computed_expr_type_ = ComputedExprType::Pointer; + } else if (isLValue()) { + pending_deref_.set(std::move(str), /*fresh=*/false); + } else { + StrCat(DerefPtrExpr(str, field_type.getNonReferenceType())); + SetValueFreshness(expr->getType()); + } + return; + } + + if (IsValueField(ctx_, member)) { + auto sp = FindStructPtr(expr); + if (!sp || sp->nested) { + auto *object = expr->getBase()->IgnoreParenImpCasts(); + bool borrows = !field_places_.contains(expr) && !IsCloneSource(object) && + !(clang::isa(object) && + !ThisIsRustPtr()); + PushBrace block(*this, borrows && isAddrOf()); + ConvertFieldPlace(expr, ConvertValueField(expr)); + return; + } + // The Value is taken out of the struct, which is borrowed only in the + // closure. + auto str = *ConvertInStructPtr(expr, false, [&] { + return ConvertValueField(expr) + + (isAddrOf() ? ".as_pointer()" : ".clone()"); + }); + if (isAddrOf()) { + StrCat(str); + computed_expr_type_ = ComputedExprType::FreshPointer; + } else { + ConvertFieldPlace(expr, std::move(str)); + } + return; + } + + if (isAddrOf()) { + StrCat(ConvertFieldPointer(expr)); + computed_expr_type_ = ComputedExprType::FreshPointer; + return; + } + + // Reads of values must not keep the struct borrowed after the read, as the + // rest of the statement may write to it. + bool copyable = TypeIsCopyable(field_type); + if (isRValue() && (copyable || field_type->isPointerType())) { + auto sp = FindStructPtr(expr); + if (sp && !sp->nested) { + StrCat(ConvertFreshRValue(expr)); + } else { + auto read = std::format("{}.{}{}", ConvertRecordPlace(expr, false), name, + copyable ? "" : ".clone()"); + // The temporaries of the tail expression of a block are dropped at + // the end of the block. + StrCat(sp ? read : std::format("{{ {} }}", read)); + } + SetFreshType(expr->getType()); + return; + } + StrCat(std::format("{}.{}", ConvertRecordPlace(expr, isLValue()), name)); + SetValueFreshness(expr->getType()); +} + +std::string ConverterRefCount::ConvertValueField(clang::MemberExpr *expr) { + if (auto it = field_places_.find(expr); it != field_places_.end()) { + return it->second; + } + return std::format("{}.{}", ConvertRecordPlace(expr, /*mut=*/false), + GetNamedDeclAsString(expr->getMemberDecl())); +} + +void ConverterRefCount::ConvertFieldPlace(clang::MemberExpr *expr, + std::string place) { + if (!IsValueField(ctx_, expr->getMemberDecl())) { + StrCat(place); + SetValueFreshness(expr->getType()); + return; + } + // Like a local variable. + if (isAddrOf()) { + StrCat(place, ".as_pointer()"); + computed_expr_type_ = ComputedExprType::FreshPointer; + return; + } + StrCat(std::format("(*{}.{}())", place, + isRValue() ? "borrow" : "borrow_mut")); + SetValueFreshness(expr->getType()); +} + bool ConverterRefCount::VisitCXXNewExpr(clang::CXXNewExpr *expr) { if (expr->isArray()) { if (auto *init = llvm::dyn_cast_or_null( @@ -2013,6 +2542,14 @@ bool ConverterRefCount::VisitCXXConstructExpr(clang::CXXConstructExpr *expr) { } auto *ctor = expr->getConstructor(); + if (ctor->isCopyConstructor() && IsCloneSource(expr->getArg(0))) { + // In Clone::clone, the source object is only available as a reference. + // Copy its fields with their own clone(). + StrCat(std::format("{}.clone()", ConvertRValue(expr->getArg(0)))); + computed_expr_type_ = ComputedExprType::FreshValue; + return false; + } + if (IsRValueConvertingConstructor(ctor) || (ctor->isMoveConstructor() && !IsUserDefinedDecl(ctor->getParent()))) { StrCat(ConvertLValue(expr->getArg(0))); @@ -2171,10 +2708,13 @@ ConverterRefCount::GetStructAttributes(const clang::RecordDecl *decl) { return attrs; } - if (HasDefaultedCopyConstructor(decl) && RecordHasOnlyReferenceFields(decl)) { + if (RecordDerivesClone(decl)) { attrs.emplace_back("Clone"); } + // Gives access to the fields of the struct through pointers to them. + attrs.emplace_back("Record"); + if (RecordDerivesByteRepr(decl)) { attrs.emplace_back("ByteRepr"); } @@ -2259,9 +2799,17 @@ std::string ConverterRefCount::ConvertFnPtrValue(clang::QualType qual_type, void ConverterRefCount::ConvertVarInit(clang::QualType qual_type, clang::Expr *expr) { + // The fields of a struct literal are initialized in a single statement. + // An initializer must not keep a struct borrowed, as another one may write + // to it. + bool is_field = getConversionKind() == ConversionKind::Pointee; bool is_ref = qual_type->isReferenceType(); PushConversionKind push(*this, ConversionKind::Unboxed, is_ref); - StrCat(BoxValue(ConvertVarInitValue(qual_type, expr))); + auto value = BoxValue(ConvertVarInitValue(qual_type, expr)); + if (is_field && ReadsField(expr) && !Trim(value).starts_with('{')) { + value = std::format("{{ {} }}", value); + } + StrCat(value); } bool ConverterRefCount::EmitGlobalValueAssign(clang::Expr *lhs, @@ -2286,32 +2834,87 @@ void ConverterRefCount::EmitSetOrAssign(clang::Expr *lhs, computed_expr_type_ = ComputedExprType::FreshValue; return; } + if (auto str = ConvertInStructPtr(lhs, true, [&] { + return std::format("{} = {}", ConvertLValue(lhs), rhs); + })) { + StrCat(*str); + computed_expr_type_ = ComputedExprType::FreshValue; + return; + } auto lhs_str = ConvertLValue(lhs); if (!pending_deref_.empty()) { - auto ptr = pending_deref_.take(); - StrCat(ptr, ".write(", rhs, ')'); + StrCat(TakePendingDerefToWrite(lhs), ".write(", rhs, ')'); } else { StrCat(lhs_str, token::kAssign, rhs); } computed_expr_type_ = ComputedExprType::FreshValue; } +// The index of the subscript expression that the place expr is in, if any. +static clang::Expr *GetSubscriptIndex(clang::Expr *expr) { + expr = expr->IgnoreParenImpCasts(); + while (auto *member = clang::dyn_cast(expr)) { + if (member->isArrow()) { + return nullptr; + } + expr = member->getBase()->IgnoreParenImpCasts(); + } + if (auto *subscript = clang::dyn_cast(expr)) { + return subscript->getIdx(); + } + if (auto *op = clang::dyn_cast(expr); + op && op->getOperator() == clang::OO_Subscript) { + return op->getArg(1); + } + return nullptr; +} + void ConverterRefCount::ConvertAssignment(clang::Expr *lhs, clang::Expr *rhs, std::string_view assign_operator) { auto rhs_as_string = ConvertFreshRValue(rhs, lhs->getType()); PushBrace brace(*this, isRValue()); - if (MayCauseBorrowMutError(lhs, rhs)) { + // An index that has side effects or reads a field may conflict with the + // borrow of the struct that holds the subscripted array. Evaluate it before + // borrowing the struct. + auto *idx = GetSubscriptIndex(lhs); + if (idx && !idx->HasSideEffects(ctx_) && !ReadsField(idx)) { + idx = nullptr; + } + + // A field is assigned by borrowing its struct mutably, which conflicts + // with any struct borrowed by the right-hand side. A field of a struct + // reached through a pointer is assigned in a closure, where the right-hand + // side must not access memory. + if (idx || MayCauseBorrowMutError(lhs, rhs) || + (ReadsField(lhs) && ReadsField(rhs)) || + (FindStructPtr(lhs, true) && !IsTrivialValue(rhs))) { StrCat(keyword::kLet, "__rhs", token::kAssign, rhs_as_string, token::kSemiColon); rhs_as_string = "__rhs"; } + if (idx) { + StrCat(keyword::kLet, "__idx", token::kAssign, ConvertRValue(idx), + token::kSemiColon); + hoisted_exprs_.emplace(idx, "__idx"); + } + struct EraseHoisted { + ConverterRefCount &c; + clang::Expr *idx; + ~EraseHoisted() { c.hoisted_exprs_.erase(idx); } + } erase_hoisted{*this, idx}; if (assign_operator == "=") { EmitSetOrAssign(lhs, rhs_as_string); } else if (EmitGlobalValueAssign(lhs, assign_operator, rhs_as_string)) { computed_expr_type_ = ComputedExprType::FreshValue; + } else if (auto str = ConvertInStructPtr(lhs, true, [&] { + return std::format("{} {} {}", ConvertLValue(lhs), + assign_operator, rhs_as_string); + })) { + StrCat(*str); + computed_expr_type_ = ComputedExprType::FreshValue; } else { auto lhs_str = ConvertLValue(lhs); if (!pending_deref_.empty()) { @@ -2388,7 +2991,8 @@ void ConverterRefCount::ConvertUniquePtrDeref( computed_expr_type_ = ComputedExprType::FreshPointer; } else { StrCat(std::format("(*{}.as_ref().unwrap().borrow{}())", - ToString(expr->getArg(0)), isRValue() ? "" : "_mut")); + ConvertRValue(expr->getArg(0)), + isRValue() ? "" : "_mut")); SetValueFreshness(expr->getType()); } } @@ -2434,6 +3038,12 @@ bool ConverterRefCount::ConvertCXXOperatorCallExpr( break; case clang::OverloadedOperatorKind::OO_Subscript: { + if (ConvertWithHoistedIndex(expr, expr->getArg(0), expr->getArg(1), [&] { + ConvertCXXOperatorCallExpr(expr); + })) { + break; + } + if (IsUniquePtr(expr->getArg(0)->getType())) { StrCat( std::format("{}.as_ref().unwrap()", ConvertRValue(expr->getArg(0)))); @@ -2456,6 +3066,29 @@ bool ConverterRefCount::ConvertCXXOperatorCallExpr( IsBoxedType(ctx_, expr->getType().getNonReferenceType()) && IsBoxedType(ctx_, expr->getArg(0)->getType().getNonReferenceType()); + // A container that is a field is indexed in place, instead of through a + // pointer to the field. The index must not borrow the struct mutably, nor + // at all if the container is borrowed mutably. + if (auto *container = expr->getArg(0)->IgnoreImpCasts(), + *idx = expr->getArg(1); + !isAddrOf() && !is_inner_boxed && IsFieldAccess(container) && + IsBoxedType(ctx_, container->getType()) && + (hoisted_exprs_.contains(idx) || + (!idx->HasSideEffects(ctx_) && (!isLValue() || !ReadsField(idx))))) { + auto place = + isLValue() ? ConvertLValue(container) : ConvertRValue(container); + auto str = + std::format("{}[({}) as usize]", place, ConvertSubscriptIndex(idx)); + if (isRValue() && TypeIsCopyable(expr->getType())) { + StrCat(std::format("{{ {} }}", str)); + SetFreshType(expr->getType()); + } else { + StrCat(str); + SetValueFreshness(expr->getType()); + } + break; + } + if (isLValue()) { PushConversionKind push_ck(*this, ConversionKind::Unboxed); pending_deref_.set( @@ -2711,8 +3344,13 @@ void ConverterRefCount::ConvertArrow(clang::Expr *expr) { op && op->getOperator() == clang::OverloadedOperatorKind::OO_Arrow; if (!is_overloaded_arrow || IsUserOperatorCall(op)) { - auto ptr = ToString(expr); - StrCat(DerefPtrExpr(ptr, expr->getType()->getPointeeType())); + if (isLValue()) { + auto ptr = ConvertRValue(expr); + StrCat(std::format("(*{}.upgrade().deref_mut())", + DetachPtr(std::move(ptr), isFresh(), expr))); + } else { + StrCat(DerefPtrExpr(ToString(expr), expr->getType()->getPointeeType())); + } SetValueFreshness(expr->getType()->getPointeeType()); return; } @@ -2784,10 +3422,162 @@ bool ConverterRefCount::IsReferenceType(const clang::Expr *expr) const { return false; } +// Collects the ways placeholder `n` is accessed in `fragments`. +static void +CollectPlaceholderAccesses( + const std::vector &fragments, unsigned n, + std::vector &accesses) { + for (const auto &frag : fragments) { + if (auto *ph = std::get_if(&frag)) { + if (ph->n == n) { + accesses.push_back(ph->access); + } + } else if (auto *mc = std::get_if< + std::unique_ptr>( + &frag)) { + CollectPlaceholderAccesses((*mc)->receiver, n, accesses); + CollectPlaceholderAccesses((*mc)->body, n, accesses); + } + } +} + +// Whether argument `n` of a mapped call can be evaluated into a local +// variable ahead of the call: the rule moves it, or only reads it and it can +// be copied. +static bool +CanHoistArg(clang::ASTContext &ctx, + const std::vector &fragments, + const clang::Expr *callee, unsigned n, const clang::Expr *arg, + bool copyable) { + using TranslationRule::Access; + std::vector accesses; + CollectPlaceholderAccesses(fragments, n, accesses); + if (accesses.empty()) { + return false; + } + // Taking a temporary moves it. + if (clang::isa(arg) && + std::ranges::all_of(accesses, [](auto a) { + return a == Access::kMove || a == Access::kTake; + })) { + return true; + } + if (RuleRegistry::ParamIsPointer(ctx, callee, n) || + Mapper::GetParamType(ctx, callee, n).starts_with('&')) { + return false; + } + if (std::ranges::all_of(accesses, + [](auto a) { return a == Access::kMove; })) { + return true; + } + return copyable && std::ranges::all_of(accesses, [](auto a) { + return a == Access::kMove || a == Access::kBorrow; + }); +} + +std::optional ConverterRefCount::ConvertOnFieldReceiver( + const std::vector &fragments, + const TranslationRule::PlaceholderFragment &receiver_ph, clang::Expr *expr, + clang::Expr **args, unsigned num_args, + llvm::function_ref convert) { + auto all_args = BuildUnifiedArgs(expr, args, num_args); + auto *receiver = all_args[receiver_ph.n]; + // A method called on a field of a struct reached through a pointer is + // called in a closure, unless the rule takes a pointer to the field. + auto sp = receiver_ph.access != TranslationRule::Access::kMove && + !receiver->getType()->isPointerType() && + !RuleRegistry::ParamIsPointer(ctx_, GetCalleeOrExpr(expr), + receiver_ph.n) + ? FindStructPtr(receiver, receiver_ph.access != + TranslationRule::Access::kBorrow) + : std::nullopt; + bool in_closure = sp && !sp->nested; + + // Fields share the borrow of their struct. Evaluate the arguments that + // read fields before borrowing the receiver, if it is a field as well. In + // a closure, evaluate those that access any memory. + if (ReadsField(receiver)) { + std::string lets; + for (unsigned i = 0; i < all_args.size(); ++i) { + auto *arg = all_args[i]; + if (arg == receiver || + (in_closure ? IsTrivialValue(arg) : !ReadsField(arg)) || + hoisted_exprs_.contains(arg) || + !CanHoistArg(ctx_, fragments, GetCalleeOrExpr(expr), i, arg, + TypeIsCopyable(arg->getType()))) { + continue; + } + auto name = std::format("__a{}", i); + lets += std::format("let {} = {};", name, ConvertFreshRValue(arg)); + hoisted_exprs_.emplace(arg, std::move(name)); + } + if (!lets.empty()) { + auto call = convert(); + for (auto *arg : all_args) { + hoisted_exprs_.erase(arg); + } + return std::format("{{ {} {} }}", lets, call); + } + } + + if (in_closure) { + return ConvertInStructPtr( + receiver, receiver_ph.access != TranslationRule::Access::kBorrow, + convert); + } + return std::nullopt; +} + +std::string ConverterRefCount::ConvertIRFragment( + const std::vector &fragments, + clang::Expr *expr, clang::Expr **args, unsigned num_args, + TempMaterializationCtx *ctx) { + // A rule that calls methods on the result of a method called on a field, + // e.g., `a0.get(a1).unwrap()`, is converted in the closure as a whole, as + // the result may borrow the field. + using TranslationRule::MethodCallFragment; + const TranslationRule::PlaceholderFragment *receiver_ph = nullptr; + bool chained = fragments.size() > 1; + for (const auto *frags = &fragments; frags->size() >= 1;) { + auto *mc = + std::get_if>(&frags->front()); + if (!mc) { + break; + } + if ((receiver_ph = (*mc)->getReceiverPlaceholder())) { + break; + } + frags = &(*mc)->receiver; + chained = true; + } + if (receiver_ph && chained) { + if (auto str = ConvertOnFieldReceiver( + fragments, *receiver_ph, expr, args, num_args, [&] { + auto str = + ConvertIRFragment(fragments, expr, args, num_args, ctx); + // The rule may have statements. + return Trim(str).contains(';') ? std::format("{{ {} }}", str) + : str; + })) { + return *str; + } + } + return Converter::ConvertIRFragment(fragments, expr, args, num_args, ctx); +} + 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) { + if (auto str = ConvertOnFieldReceiver( + mc.body, *receiver_ph, expr, args, num_args, [&] { + return ConvertMappedMethodCall(expr, mc, args, num_args, ctx); + })) { + return *str; + } + } + if (!receiver_ph || receiver_ph->access == TranslationRule::Access::kBorrow || receiver_ph->access == TranslationRule::Access::kMove) { return Converter::ConvertMappedMethodCall(expr, mc, args, num_args, ctx); @@ -2816,7 +3606,7 @@ std::string ConverterRefCount::ConvertMappedMethodCall( assert(!pending_deref_.empty()); bool is_boxed = pending_deref_.is_boxed(); - auto ptr = pending_deref_.take(); + auto ptr = TakePendingDerefToWrite(arg); auto body = ConvertIRFragment(mc.body, expr, args, num_args, ctx); SetFreshType(expr->getType()); @@ -2902,8 +3692,9 @@ void ConverterRefCount::SetUFCSReceiver(clang::Expr *base, bool is_arrow, token::kRef + BoxValue(ConvertRValue(base)) + ".as_pointer()"; return; } - ufcs_receiver_ = token::kRef + (base_is_pointer ? ConvertRValue(base) - : ConvertPointer(base)); + auto ptr = base_is_pointer ? ConvertRValue(base) : ConvertPointer(base); + // The method may write to the struct that holds the receiver. + ufcs_receiver_ = token::kRef + DetachPtr(std::move(ptr), isFresh(), base); } std::string @@ -3033,12 +3824,13 @@ ConverterRefCount::DestroyMembers(const clang::CXXRecordDecl *decl) { field->getType()->getBaseElementTypeUnsafe()->getAsCXXRecordDecl(); assert(elem); out += std::format( - "{{ let __p = (*self.upgrade().deref()).{0}.as_pointer(); for __i in " + "{{ let __p: Ptr<{3}> = field_ptr!(self, {0}); for __i in " "0..__p.len() {{ {2}::{1}(&__p.offset(__i as isize)); }} }}\n", - name, kDestructorName, TraitName(elem)); + name, kDestructorName, TraitName(elem), GetRecordName(elem)); } else { - out += std::format("(*self.upgrade().deref()).{0}.as_pointer().{1}();\n", - name, kDestructorName); + out += std::format( + "{2}::{1}(&field_ptr!(self, {0}));\n", name, kDestructorName, + TraitName(field->getType()->getAsCXXRecordDecl())); } } return out; @@ -3056,6 +3848,7 @@ void ConverterRefCount::ConvertCXXConstructorBody( } else { StrCat("Self"); PushBrace this_init(*this); + PushConversionKind push(*this, ConversionKind::Pointee); EmitConstructorFieldInits(decl); } StrCat("))", token::kSemiColon); diff --git a/cpp2rust/converter/models/converter_refcount.h b/cpp2rust/converter/models/converter_refcount.h index 80f2ef34ad..4f15ec3cee 100644 --- a/cpp2rust/converter/models/converter_refcount.h +++ b/cpp2rust/converter/models/converter_refcount.h @@ -51,6 +51,12 @@ class ConverterRefCount final : public Converter { bool RecordImplementsClone(const clang::RecordDecl *decl) override; + std::string GetDeepCopy(clang::QualType type, std::string_view src); + + // Whether the Clone of a struct copies each field with its own clone(), and + // hence can be derived. + bool RecordDerivesClone(const clang::RecordDecl *decl); + void AddByteReprTrait(const clang::RecordDecl *decl) override; bool @@ -143,10 +149,71 @@ class ConverterRefCount final : public Converter { bool VisitArraySubscriptExpr(clang::ArraySubscriptExpr *expr) override; + // Converts the subscript expr with convert, having evaluated its index + // first, if the index has side effects that may conflict with the borrow of + // the struct that holds base. Returns false if it doesn't do so. + bool ConvertWithHoistedIndex(clang::Expr *expr, clang::Expr *base, + clang::Expr *idx, + llvm::function_ref convert); + bool VisitMemberExpr(clang::MemberExpr *expr) override; void ConvertUnionMemberAccessor(clang::MemberExpr *expr); + // Converts an access to a field of a struct translated by us. Fields are + // stored inline in the struct. + void ConvertFieldAccess(clang::MemberExpr *expr); + + // The struct whose field `expr` accesses, as a place expression. The struct + // is borrowed mutably if `mut` is set. + std::string ConvertRecordPlace(clang::MemberExpr *expr, bool mut); + + // Wraps ptr, the result of converting expr, such that it doesn't keep any + // struct borrowed if it is written through. + std::string DetachPtr(std::string ptr, bool fresh, const clang::Expr *expr); + std::string TakePendingDerefToWrite(const clang::Expr *lvalue); + + // A Ptr to the field accessed by `expr`. + std::string ConvertFieldPointer(clang::MemberExpr *expr); + + // The Value held by a Value field (see IsValueField). + std::string ConvertValueField(clang::MemberExpr *expr); + + // Converts the field `expr` given the place where it is stored. + void ConvertFieldPlace(clang::MemberExpr *expr, std::string place); + + // A field of a struct reached through the pointer `ptr`, which a place + // expression accesses (e.g., p->x, (*p).x, or r.a[i].x for a reference r). + // `nested` if the place is converted inside ConvertInStructPtr already. + struct StructPtr { + clang::MemberExpr *member; + clang::Expr *ptr = nullptr; + bool via_reference = false; + bool nested = false; + // Whether the place is in a Value field, which is borrowed separately + // from the struct. + bool in_value_field = false; + }; + // If `mut`, the place is written, and must not borrow the struct. + std::optional FindStructPtr(clang::Expr *place, bool mut = false); + + // If `place` is a field of a struct reached through a pointer, converts + // the expression given by `convert`, which accesses `place`, in a closure + // that borrows the struct through the pointer, `p.with(|__s: &S| ...)`. + std::optional + ConvertInStructPtr(clang::Expr *place, bool mut, + llvm::function_ref convert); + + std::string ConvertTake(clang::Expr *arg) override { + auto take = [&] { return Converter::ConvertTake(arg); }; + auto str = ConvertInStructPtr(arg, true, take); + return str ? *str : take(); + } + + // The fields converted by ConvertInStructPtr, and their places in the + // closure. + std::unordered_map field_places_; + bool VisitCXXNewExpr(clang::CXXNewExpr *expr) override; bool VisitCXXDeleteExpr(clang::CXXDeleteExpr *expr) override; @@ -194,6 +261,25 @@ class ConverterRefCount final : public Converter { bool Convert(clang::Expr *expr, std::optional implicit_convert_to = {}) override { + if (auto it = hoisted_exprs_.find(expr); it != hoisted_exprs_.end()) { + StrCat(it->second); + computed_expr_type_ = ComputedExprType::FreshValue; + return false; + } + if (auto it = field_places_.find(expr); it != field_places_.end()) { + ConvertFieldPlace(clang::cast(expr), it->second); + return false; + } + // Values read from fields of structs reached through pointers. + if (isRValue() && TypeIsCopyable(expr->getType())) { + if (auto str = ConvertInStructPtr(expr, false, [&] { + return ToString(expr, implicit_convert_to); + })) { + StrCat(*str); + SetFreshType(expr->getType()); + return false; + } + } auto result = Converter::Convert(expr, implicit_convert_to); if (computed_expr_type_ == ComputedExprType::Pending) { assert(!pending_deref_.empty() && "pending_deref_ taken without type"); @@ -208,6 +294,9 @@ class ConverterRefCount final : public Converter { void ConvertVarInit(clang::QualType qual_type, clang::Expr *expr) override; + void ConvertFieldInit(const clang::FieldDecl *field, + clang::Expr *init) override; + std::string ConvertVarInitValue(clang::QualType qual_type, clang::Expr *expr); void ConvertAssignment(clang::Expr *lhs, clang::Expr *rhs, @@ -241,6 +330,21 @@ class ConverterRefCount final : public Converter { bool IsReferenceType(const clang::Expr *expr) const override; + // Converts a rule that calls a method on `receiver_ph`, by `convert`, in + // a closure if the receiver is a field of a struct reached through a + // pointer, or after evaluating the arguments that read fields if it is a + // field. + std::optional ConvertOnFieldReceiver( + const std::vector &fragments, + const TranslationRule::PlaceholderFragment &receiver_ph, + clang::Expr *expr, clang::Expr **args, unsigned num_args, + llvm::function_ref convert); + + std::string + ConvertIRFragment(const std::vector &fragments, + clang::Expr *expr, clang::Expr **args, unsigned num_args, + TempMaterializationCtx *ctx) override; + std::string ConvertMappedMethodCall(clang::Expr *expr, const TranslationRule::MethodCallFragment &mc, diff --git a/docs/src/codegen/types.md b/docs/src/codegen/types.md index c56b1711e9..58205b7526 100644 --- a/docs/src/codegen/types.md +++ b/docs/src/codegen/types.md @@ -35,18 +35,22 @@ and the refcount model produces ```rust pub struct Item { - pub id: Value, - pub name: Value>, + #[offset(0)] + pub id: i32, + #[offset(4)] + pub name: Box<[u8]>, + #[offset(16)] pub refs: Value>, } pub fn count_0(item: Item) -> i32 { let item: Value = Rc::new(RefCell::new(item)); - return *(*item.borrow()).id.borrow(); + return (*item.borrow()).id; } ``` -Every struct field is boxed in its own `Value` so that a pointer can be taken -to it. This is set to change: field writes and field addresses through a -reinterpreted struct pointer go to a temporary and are lost, so fields will -become plain `T` and `Ptr` will gain a kind that stores the parent struct plus -an offset ([#309](https://github.com/Cpp2Rust/cpp2rust/issues/309)). +Fields are stored inline in their struct, so a whole struct lives in a single +`Value`, like the elements of an array; only vectors are `Value`s of their own +(see [Boxing](types/boxing.md)). A pointer to a field records the allocation of +the struct plus the byte offset of the field in it, which the `#[offset(N)]` +attributes give (see +[Pointers to fields](../runtime/rc.md#pointers-to-fields)). diff --git a/docs/src/codegen/types/boxing.md b/docs/src/codegen/types/boxing.md index a94ece81e5..4dcdc4707b 100644 --- a/docs/src/codegen/types/boxing.md +++ b/docs/src/codegen/types/boxing.md @@ -10,8 +10,11 @@ Not every type position is boxed. `ConverterRefCount` keeps a stack of conversion kinds, [`conversion_kind_`](../internals/state.md), and the construct that owns the type pushes one before printing it: -- `FullRefCount`: pushed by variable and field declarations; `Convert(QualType)` - wraps the result in `Value<...>`. +- `FullRefCount`: pushed by variable declarations; `Convert(QualType)` wraps the + result in `Value<...>`. +- `Pointee`: pushed by field declarations; the bare type is printed, but arrays + are boxed slices, as in a `Value`. Fields whose type maps to a `Vec` + (`std::vector`, `std::string`) push `FullRefCount` instead (see below). - `Unboxed`: pushed by parameter lists, return types, and record names; the bare type is printed. - `Ptr`: pushed by a pointer type for its pointee; also printed bare. @@ -20,8 +23,10 @@ The result by position: | Position | `int` | `Item` | `int[3]` | | ------------------------------------------- | ------------ | ------------- | -------------------- | -| local variable, struct field, global | `Value` | `Value` | `Value>` | +| local variable, global | `Value` | `Value` | `Value>` | | function parameter, return type | `i32` | `Item` | decays to `Ptr` | +| struct field | `i32` | `Item` | `Box<[i32]>` | +| struct field of type `std::vector` | | | `Value>` | | pointee of `Ptr`, element of a container | `i32` | `Item` | `Box<[i32]>` | Parameters arrive unboxed and are re-boxed by the function preamble; return @@ -35,7 +40,7 @@ int add(int a, Item item) { return a + item.id; } pub fn add_0(a: i32, item: Item) -> i32 { let a: Value = Rc::new(RefCell::new(a)); let item: Value = Rc::new(RefCell::new(item)); - return *a.borrow() + *(*item.borrow()).id.borrow(); + return *a.borrow() + (*item.borrow()).id; } ``` @@ -50,3 +55,30 @@ and a pointer can be taken to it. The boxing is written into the type rules themselves: `std::vector>` maps to `Vec>>`, and the `carray` rules map `int a[2][2]` to `Box<[Value>]>`, both before the outer `Value<...>` of the declaration is added. + +Struct fields are stored inline, so that a whole struct is a single allocation, +and a pointer to a field records the struct's allocation and the field's byte +offset (see [Pointers](pointers.md)). Vectors are the exception: a vector field +is a `Value>` of its own. The elements of a vector live outside the +struct, and their number changes, so a pointer to an element, or to a field of +an element, has the vector as its allocation instead of the struct: + +```cpp +struct Holder { std::vector points; int n; }; +h.points[0].y = 5; +``` + +```rust +pub struct Holder { + #[offset(0)] + pub points: Value>, + #[offset(24)] + pub n: i32, +} +(*(*h.borrow()).points.borrow_mut())[(0_usize) as usize].y = 5; +``` + +A vector field is accessed like a local vector, through its own `borrow()` or +`borrow_mut()`, and the struct is only borrowed immutably to reach it. As +`Value` is shared on `clone()`, structs with vector fields implement `Clone` by +copying the vectors, instead of deriving it. diff --git a/docs/src/codegen/types/pointers.md b/docs/src/codegen/types/pointers.md index 448c5417e1..91cd4ae0f2 100644 --- a/docs/src/codegen/types/pointers.md +++ b/docs/src/codegen/types/pointers.md @@ -47,9 +47,11 @@ pointer type is to `const`. Globals use `&raw mut x` so no reference to the an element is `&mut arr[i] as *mut T`. Refcount model: `&x` becomes `x.as_pointer()`, which produces a `Ptr` holding a -weak reference to the variable's `Value`. Since every field is its own `Value`, -`&s.field` is `s.field.as_pointer()`. An array decays with -`arr.as_pointer() as Ptr`, a `Ptr` to element 0 of the whole array, and +weak reference to the variable's `Value`. `&s.field` is +`field_ptr!(s, field)`, and `&p->field` is `field_ptr!(p, field)`: a pointer to +the field of the struct, made from the `Value` or the `Ptr` of the struct (see +[Pointers to fields](../../runtime/rc.md#pointers-to-fields)). An array decays +with `arr.as_pointer() as Ptr`, a `Ptr` to element 0 of the whole array, and `&arr[i]` is that pointer offset by `i`. Both models push the address down to the innermost place expression: @@ -82,12 +84,21 @@ write copies it in. Which form is emitted follows the [expression kind](../expressions/kinds.md), what the enclosing construct expects of the dereference: -- An rvalue use copies the value out. A scalar or pointer pointee is `p.read()`. - A record pointee goes through `p.upgrade().deref()`, which briefly turns the - weak pointer into a [strong one](../../runtime/rc.md#strong-pointers) and - borrows the record; a field of it is a `Value` and is borrowed as usual, - `(*p.upgrade().deref()).x`. A read-only method on a boxed pointee borrows the - same way: `p->size()` is `(*p.upgrade().deref()).len()`. +- An rvalue use copies the value out. A scalar or pointer pointee is `p.read()`, + and so is a whole record, `*p`. A field of a record pointee is read in a + closure that borrows the record for its duration: `p->x` is + `p.with(|__s: &S| __s.x)`, and `p->a[i].y` is + `p.with(|__s: &S| __s.a[i].y)`. A method called on a field runs in the + closure too, together with the rest of its rule: `p->v.size()` is + `p.with(|__s: &S| (*__s.v.borrow()).len())`. +- A write to a field of a record pointee is also done in a closure, which + borrows the record mutably: `p->x = v` is + `p.with_mut(|__s: &mut S| __s.x = v)`. Operands that may access memory are + evaluated before the closure (`let __rhs = ...;`), so that they don't find the + record borrowed. Fields that are [`Value`s of their own](boxing.md) only need + the record borrowed immutably, `p.with(|__s: &S| (*__s.v.borrow_mut()).push(1))`. + The same closures are used for references to records, `r.x`, and for elements + of arrays of records, `a[i].x`. - An address-of use prints `p` itself. - An lvalue use prints nothing at once. The converter records the pointer expression as a [pending dereference](../expressions/pending-deref.md), and @@ -97,10 +108,11 @@ of the dereference: instead of a borrow followed by an assignment, and `*p += v` as `{ let _ptr = p.clone(); _ptr.write(_ptr.read() + v) }`. -The strong pointer only ever appears as a temporary inside the expression, which -is what breaks field writes and field addresses through reinterpreted pointers -([#309](https://github.com/Cpp2Rust/cpp2rust/issues/309)) and union accessors -([#311](https://github.com/Cpp2Rust/cpp2rust/issues/311)). +`with` and `with_mut` work on any pointer, including a +[reinterpreted](../../runtime/reinterpret.md) one, whose pointee is decoded from +the bytes of the original allocation and, for `with_mut`, encoded back into +them before the closure returns. A write through a reinterpreted pointer is +hence visible right away through every other pointer to the same bytes. ## Arithmetic and comparison diff --git a/docs/src/codegen/types/traits.md b/docs/src/codegen/types/traits.md index 9110897cb0..191b518bb1 100644 --- a/docs/src/codegen/types/traits.md +++ b/docs/src/codegen/types/traits.md @@ -8,12 +8,13 @@ for structs. | Trait | Unsafe model | Refcount model | | -------------------------------------- | --------------------------------------------------------------------- | --------------------------------------------------- | -| `Copy` | derived when every field is copyable | never (a `Value` is not `Copy`) | -| `Clone` | derived | hand-written, deep-copies each field into a new box | +| `Copy` | derived when every field is copyable | never | +| `Clone` | derived | derived when member-wise, else hand-written | | `Default` | derived when possible, else hand-written | same | | `Drop` | not emitted ([#310](https://github.com/Cpp2Rust/cpp2rust/issues/310)) | hand-written from a user destructor with a body | | `Ord`, `PartialOrd`, `PartialEq`, `Eq` | hand-written from `operator<` | same | | `ByteRepr` | not needed | hand-written for every record and enum | +| `Record` | not needed | derived for every struct | ## Copy and Clone @@ -21,20 +22,19 @@ In the unsafe model a record derives `Copy` unless a field is translated to a `Vec`, `BTreeMap`, `Option>` (`std::unique_ptr`), or a record that is not itself `Copy`; `Clone` is derived unless the C++ copy constructor is deleted. The refcount model skips `Clone` altogether when the copy constructor is -deleted. The refcount model cannot derive either, because a `Value` field is -an `Rc` and a derived `Clone` would only bump its count, leaving the copy -aliasing the original. The generated `Clone` therefore rebuilds every field: - -```rust -impl Clone for Counter { - fn clone(&self) -> Self { - let mut this = Self { - count_: Rc::new(RefCell::new(*self.count_.borrow())), - }; - this - } -} -``` +deleted, and never derives `Copy`. It derives `Clone` for C structs and for +classes with an implicit or defaulted copy constructor, which copy each field +with its own `clone`, i.e., its C++ copy constructor. The exceptions are fields +that are or nest a `Value`, such as `std::vector` (`Value>`, see +[Boxing](boxing.md)) or `std::vector>` +(`Value>>>`), whose derived `clone` would share the +`Value`s instead of copying them; the generated `Clone` then translates the +implicit copy constructor, which copies them deeply. + +A user-defined copy constructor takes a `Ptr` to the source, while `clone` only +has `&self`. `clone` passes it a pointer to a shallow copy of `self`, built field +by field without running any copy constructor, so that the source is copied +exactly once. This is what gives struct assignment and pass-by-value C++'s member-by-member copy. @@ -86,5 +86,16 @@ The refcount model emits [`ByteRepr`](../../runtime/reinterpret.md#byterepr) for every record and enum: `byte_size`, `to_bytes`, and `from_bytes` laid out with the C offsets of the fields (enums go through their `i32` value). It is what lets a `Ptr` to the type be reinterpreted as bytes, and bytes be read back as -the type. A record with a field that has no byte representation gets an empty -impl, and reinterpreting it panics at run time. +the type. A record with a field that has no byte representation gets an impl +with `byte_size` alone, which locates the elements of arrays of the record for +field pointers, and reinterpreting it panics at run time. + +## Record + +`#[derive(Record)]`, from `libcc2rs-macros`, gives +[pointers to fields](../../runtime/rc.md#pointers-to-fields) access to the +fields of a struct. The converter writes the C offset of each field, from +Clang's record layout, as an `#[offset(N)]` attribute on it, and the derive +generates the table of offsets that `field_ptr!` looks fields up in, and the +`locate` functions that find a field from its offset when a field pointer is +accessed. diff --git a/docs/src/runtime/rc.md b/docs/src/runtime/rc.md index d71161baa6..88cac9bc7a 100644 --- a/docs/src/runtime/rc.md +++ b/docs/src/runtime/rc.md @@ -60,6 +60,7 @@ A `Ptr` knows what it points into: - `StackArray` and `HeapArray`: a fixed-size array. - `StackVec` and `HeapVec`: a growable buffer; `std::vector` contents and string literals live in one. +- `Field`: a field of a struct (see [Pointers to fields](#pointers-to-fields)). - `Reinterpreted`: a byte-level view produced by a cast (see [Type Reinterpretation](./reinterpret.md)). @@ -72,6 +73,71 @@ Two pointers compare equal when they point into the same allocation at the same byte offset, and ordering compares allocation addresses, as C++ pointer comparison does. +## Pointers to fields + +Fields are stored inline in their struct, so a struct and all of its fields +share one allocation and one `RefCell`. A pointer to a field is to a struct +what a pointer to an element is to an array: it records a weak reference to the +allocation that holds the struct, its root, together with the byte offset of +the field in it. `field_ptr!` creates one: + +```c +struct point { int x; int y; }; +struct point p; +int *y = &p.y; +``` + +```rust +let p: Value = Rc::new(RefCell::new(::default())); +let y: Value> = Rc::new(RefCell::new(field_ptr!(p, y))); +``` + +`field_ptr!(p, y)` works on a `Value` or a `Ptr` to a struct. The offsets are +those of the C layout of the struct, which the code generator gets from Clang +and writes on the fields as `#[offset(N)]` attributes; `#[derive(Record)]` +turns them into the implementation of the `Record` trait: + +```rust +#[derive(Clone, Record, Default)] +pub struct point { + #[offset(0)] + pub x: i32, + #[offset(4)] + pub y: i32, +} +``` + +A field pointer is thus the same size as any other pointer, and creating one +allocates nothing. Pointers to fields of fields, and to fields of array +elements, add up the offsets, and keep the allocation of the outermost struct or +array as their root. A pointer to an array field can either point to the whole +field (`Ptr>`) or index into it (`Ptr`); the expected pointer type +picks which, as for `as_pointer`. + +An access through a field pointer borrows the root, and finds the field from +its offset and its type with `Record::locate`, which the derive generates as a +comparison of the offset against those of the fields, recursing into nested +structs and arrays of structs. A field is identified by its type too, as a +struct and its first field share an offset. An offset that doesn't find a field +of the pointer's type panics with `ub: invalid field pointer`. + +Vectors, whose elements are not part of the struct's layout, are not stored +inline: a `std::vector` field is a `Value>` of its own, and pointers to +its elements, and to the fields of those, have the vector as their root. + +Reading a field through a pointer borrows the struct only for the duration of a +closure, e.g., `p.with(|s: &point| s.y)`, so that the borrow ends before the +rest of the statement runs; `p.with_mut(|s: &mut point| s.y = 3)` writes it. + +A field of a reinterpreted struct is itself a reinterpreted pointer, to the +bytes of the field. Conversely, reinterpreting a field pointer views the bytes +of the field alone, as if it were its own allocation. + +Since all fields share the borrow of their struct, an expression must not write +to a field while another field of the same struct is borrowed. The code +generator scopes the borrows of field reads so that they end before any write +in the same statement. + ## The heap `new` and `new[]` are translated as `Ptr::alloc` and `Ptr::alloc_array`, and @@ -180,26 +246,25 @@ pub enum StrongPtr { StackSingle(Rc>), Vec { rc: Rc>>, offset: usize }, StackArray { rc: Rc>>, offset: usize }, - Reinterpreted { - alloc: Rc, - byte_offset: usize, - cell: RefCell>, - }, + Field { root: Rc, field: u32, offset: usize }, + Reinterpreted { alloc: OriginalAlloc, byte_offset: usize, cell: RefCell> }, } ``` -Its one operation is `deref`, which returns a `Ref<'_, T>` to the pointee. The -`Ref` borrows the `StrongPtr`, so the borrow of the `RefCell` lasts as long as -the strong pointer does: in `p.upgrade().deref().field`, the temporary +`deref` returns a `Ref<'_, T>` to the pointee, and `deref_mut` a `RefMut<'_, T>`. +The `Ref` borrows the `StrongPtr`, so the borrow of the `RefCell` lasts as long +as the strong pointer does: in `p.upgrade().deref().field`, the temporary `StrongPtr` lives until the end of the enclosing statement, and so does the -borrow. That is what makes a member access through a pointer expressible without -a closure. There is no `deref_mut`; writes go through `with_mut`. +borrow. The code generator prefers the `with` and `with_mut` closures, and +upgrades only where the result of an access must borrow the pointee beyond a +closure. For the `Reinterpreted` variant there is no value to reference, only bytes in -another allocation. `deref` reads those bytes into the local `cell` and hands -out a `Ref` to that copy, refreshing it on every call. The copy is what -[Type Reinterpretation](./reinterpret.md#known-limitations) is about: writes -into the copy never reach the original allocation. +another allocation. `deref` reads those bytes into a local cell and hands out a +`Ref` to that copy, refreshing it on every call. `deref_mut` panics: a write +must go through `with_mut`, which writes the bytes through to the original +allocation before it returns, so that the write is visible through every other +pointer at once. > [!WARNING] > diff --git a/docs/src/runtime/reinterpret.md b/docs/src/runtime/reinterpret.md index b0efa6a339..5c93016ff0 100644 --- a/docs/src/runtime/reinterpret.md +++ b/docs/src/runtime/reinterpret.md @@ -19,8 +19,8 @@ pub trait ByteRepr: 'static { } ``` -A C struct is translated as a Rust struct whose fields are `Value`s, and the -code generator emits the `ByteRepr` implementation next to it: +The code generator emits the `ByteRepr` implementation of a C struct next to +it: ```c struct header { @@ -31,8 +31,8 @@ struct header { ```rust pub struct header { - pub tag: Value, - pub size: Value, + pub tag: i32, + pub size: i32, } impl ByteRepr for header { @@ -40,13 +40,13 @@ impl ByteRepr for header { 8 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.tag.borrow()).to_bytes(&mut buf[0..4]); - (*self.size.borrow()).to_bytes(&mut buf[4..8]); + self.tag.to_bytes(&mut buf[0..4]); + self.size.to_bytes(&mut buf[4..8]); } fn from_bytes(buf: &[u8]) -> Self { Self { - tag: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - size: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + tag: ::from_bytes(&buf[0..4]), + size: ::from_bytes(&buf[4..8]), } } } @@ -110,7 +110,9 @@ reinterpreted to bytes and the original allocation is deleted. A cast allocates exactly one small object, the view, which holds a weak reference to the original allocation, the size of the target type, and a reference to a stateless table of the byte-level operations for the original's -storage type (a single value, a `Vec`, or a boxed slice). Copying or offsetting +storage type (a single value, a `Vec`, a boxed slice, or a field of a struct, +whose bytes are found through the struct's allocation as for a +[field pointer](./rc.md#pointers-to-fields)). Copying or offsetting the resulting `Ptr` only bumps a reference count, so a loop over a `malloc`ed array pays for the cast once, not per access. @@ -123,19 +125,15 @@ directly instead of serializing the elements one by one. ## Known limitations Reading a struct through a reinterpreted pointer builds a fresh struct with -`from_bytes`, so its fields are new `Value`s that exist only as long as that -temporary struct. Three C patterns break because of this; all are set to be -fixed in the near future. - -1. Taking the address of a field of a reinterpreted struct yields a pointer into - the temporary, which dangles as soon as the temporary is dropped. -2. Writing to a field of a reinterpreted struct, translated as - `p.upgrade().deref().field.borrow_mut()`, mutates the temporary returned by - `p.upgrade().deref()` and never writes the bytes back to the original - allocation, so the write is lost. -3. [Union accessors](../codegen/unions.md) return pointers to the union's - storage; on a reinterpreted union that storage is the temporary, so the - returned pointer dangles. +`from_bytes`, which exists only for the duration of the `with` closure, or as +long as the `StrongPtr` that holds it. Writes to its fields go through +`with_mut`, which encodes the struct back into the original allocation before +it returns, so they are visible right away through any other pointer; a pointer +to one of its fields is a reinterpreted pointer into the original allocation. +However, +[union accessors](../codegen/unions.md) return pointers to the union's storage; +on a reinterpreted union that storage is the temporary, so the returned pointer +dangles. This is set to be fixed in the near future. ## AnyPtr casts diff --git a/libcc2rs-macros/src/lib.rs b/libcc2rs-macros/src/lib.rs index 6d94595da3..8352466175 100644 --- a/libcc2rs-macros/src/lib.rs +++ b/libcc2rs-macros/src/lib.rs @@ -6,6 +6,7 @@ use proc_macro::TokenStream; mod byte_repr; mod fn_ptr_arg; mod goto; +mod record; mod state_machine; mod switch; mod va_arg; @@ -103,3 +104,19 @@ pub fn derive_va_arg(input: TokenStream) -> TokenStream { pub fn derive_fn_ptr_arg(input: TokenStream) -> TokenStream { fn_ptr_arg::expand(input) } + +// #[derive(Record)] +// pub struct S { +// #[offset(0)] +// pub x: i32, +// ... +// } +// +// Implements libcc2rs::Record for S, which lets pointers to the fields of S +// be created by field_ptr!. The argument of the offset attribute is the byte +// offset of the field in the C layout of S. + +#[proc_macro_derive(Record, attributes(offset))] +pub fn derive_record(input: TokenStream) -> TokenStream { + record::expand(input) +} diff --git a/libcc2rs-macros/src/record.rs b/libcc2rs-macros/src/record.rs new file mode 100644 index 0000000000..e1418db7f2 --- /dev/null +++ b/libcc2rs-macros/src/record.rs @@ -0,0 +1,94 @@ +// Copyright (c) 2022-present INESC-ID. +// Distributed under the MIT license that can be found in the LICENSE file. + +use proc_macro::TokenStream; +use quote::{format_ident, quote}; +use syn::{Data, DeriveInput, Fields, LitInt, parse_macro_input}; + +pub fn expand(input: TokenStream) -> TokenStream { + let input = parse_macro_input!(input as DeriveInput); + let name = &input.ident; + let vis = &input.vis; + let Data::Struct(data) = &input.data else { + panic!("derive(Record) is only supported on structs, `{name}` is not one"); + }; + let mut fields: Vec<_> = match &data.fields { + Fields::Named(fields) => fields + .named + .iter() + .map(|field| { + let ident = field.ident.clone().unwrap(); + let offset: LitInt = field + .attrs + .iter() + .find(|attr| attr.path().is_ident("offset")) + .unwrap_or_else(|| panic!("field `{name}::{ident}` has no #[offset(...)]")) + .parse_args() + .unwrap(); + let offset: usize = offset.base10_parse().unwrap(); + (ident, offset) + }) + .collect(), + _ => Vec::new(), + }; + let offsets = format_ident!("__{}_offsets", name); + let idents: Vec<_> = fields.iter().map(|(ident, _)| ident.clone()).collect(); + let values: Vec<_> = fields.iter().map(|(_, offset)| *offset).collect(); + + // The field that contains a byte is the last one that starts before it. + fields.sort_by_key(|(_, offset)| std::cmp::Reverse(*offset)); + let locate = fields.iter().map(|(ident, offset)| { + quote! { + if offset >= #offset { + return ::libcc2rs::__locate_field!(&self.#ident, offset - #offset, ty); + } + } + }); + let locate_mut = fields.iter().map(|(ident, offset)| { + quote! { + if offset >= #offset { + return ::libcc2rs::__locate_field_mut!(&mut self.#ident, offset - #offset, ty); + } + } + }); + + quote! { + #[doc(hidden)] + #[allow(non_camel_case_types)] + #vis struct #offsets { + #(pub #idents: usize,)* + } + + impl ::libcc2rs::Record for #name { + type Offsets = #offsets; + const OFFSETS: #offsets = #offsets { #(#idents: #values,)* }; + + #[allow(unused_comparisons)] + fn locate( + &self, + offset: usize, + ty: ::std::any::TypeId, + ) -> ::core::option::Option<&dyn ::std::any::Any> { + if offset == 0 && ty == ::std::any::TypeId::of::() { + return ::core::option::Option::Some(self); + } + #(#locate)* + ::core::option::Option::None + } + + #[allow(unused_comparisons)] + fn locate_mut( + &mut self, + offset: usize, + ty: ::std::any::TypeId, + ) -> ::core::option::Option<&mut dyn ::std::any::Any> { + if offset == 0 && ty == ::std::any::TypeId::of::() { + return ::core::option::Option::Some(self); + } + #(#locate_mut)* + ::core::option::Option::None + } + } + } + .into() +} diff --git a/libcc2rs/src/cstr.rs b/libcc2rs/src/cstr.rs index 0b323d2bdf..f527749fdb 100644 --- a/libcc2rs/src/cstr.rs +++ b/libcc2rs/src/cstr.rs @@ -144,6 +144,12 @@ impl Ptr { let b = rc.borrow(); f(until_nul(&b[self.offset..])) } + PtrKind::Field(root, field) => { + let root = crate::field::upgrade(root); + f(until_nul( + &Ptr::::borrow_field(&*root, *field)[self.offset..], + )) + } PtrKind::Reinterpreted(_) => f(&self.to_c_string_iterator().collect::>()), } } diff --git a/libcc2rs/src/field.rs b/libcc2rs/src/field.rs new file mode 100644 index 0000000000..8feeb96e5d --- /dev/null +++ b/libcc2rs/src/field.rs @@ -0,0 +1,556 @@ +// Copyright (c) 2022-present INESC-ID. +// Distributed under the MIT license that can be found in the LICENSE file. + +// Pointers to the fields of structs. +// +// Struct fields are stored inline in their struct, and hence a whole struct +// lives in a single Value. A pointer to a field records the allocation that +// holds the struct and the byte offset of the field in it (in the C layout), +// similarly to how a pointer to an array element records the array and the +// index of the element. The field is found from the offset when accessed. + +use std::any::{Any, TypeId}; +use std::cell::{Cell, Ref, RefCell, RefMut}; +use std::rc::{Rc, Weak}; + +use crate::rc::{AsPointer, Ptr, PtrKind, Value, null_deref}; +use crate::reinterpret::ByteRepr; + +// A struct whose fields can be pointed to. Implemented by #[derive(Record)], +// from the C offsets of the fields given by their #[offset(...)] attributes. +pub trait Record: ByteRepr { + // A struct with a usize member per field, holding the offset of the + // field. Used by field_ptr! to name fields. + type Offsets; + const OFFSETS: Self::Offsets; + + // The object of type `ty` at byte `offset` of the struct: the struct + // itself, a field, a field of a field, and so on. For a field that is an + // array of `ty`, the array. + fn locate(&self, offset: usize, ty: TypeId) -> Option<&dyn Any>; + fn locate_mut(&mut self, offset: usize, ty: TypeId) -> Option<&mut dyn Any>; +} + +// The elements that a pointer of type T sees in an object: the object itself, +// or the elements of an array. +pub trait Elems {} +impl Elems for T {} +impl Elems for Box<[T]> {} + +#[inline] +pub(crate) fn elems(obj: &dyn Any) -> &[T] { + if let Some(v) = obj.downcast_ref::() { + std::slice::from_ref(v) + } else if let Some(a) = obj.downcast_ref::>() { + a + } else { + invalid_field() + } +} + +#[inline] +pub(crate) fn elems_mut(obj: &mut dyn Any) -> &mut [T] { + if obj.is::() { + std::slice::from_mut(obj.downcast_mut::().unwrap()) + } else if let Some(a) = obj.downcast_mut::>() { + a + } else { + invalid_field() + } +} + +#[cold] +#[inline(never)] +#[track_caller] +fn invalid_field() -> ! { + panic!("ub: invalid field pointer") +} + +// The allocation of a Value, Value> or Value>, where R is +// a struct, that field pointers point into. +pub trait Root: Any { + fn borrow_at(&self, offset: usize, ty: TypeId) -> Ref<'_, dyn Any>; + fn borrow_at_mut(&self, offset: usize, ty: TypeId) -> RefMut<'_, dyn Any>; +} + +impl Root for RefCell { + #[inline] + fn borrow_at(&self, offset: usize, ty: TypeId) -> Ref<'_, dyn Any> { + Ref::map(self.borrow(), |r| { + r.locate(offset, ty).unwrap_or_else(|| invalid_field()) + }) + } + + #[inline] + fn borrow_at_mut(&self, offset: usize, ty: TypeId) -> RefMut<'_, dyn Any> { + RefMut::map(self.borrow_mut(), |r| { + r.locate_mut(offset, ty).unwrap_or_else(|| invalid_field()) + }) + } +} + +// The element of an array of structs, and the offset in it, that contains +// byte `offset` of the array. +#[inline] +fn split(offset: usize) -> (usize, usize) { + let size = R::byte_size(); + (offset / size, offset % size) +} + +impl Root for RefCell> { + #[inline] + fn borrow_at(&self, offset: usize, ty: TypeId) -> Ref<'_, dyn Any> { + let (idx, offset) = split::(offset); + Ref::map(self.borrow(), |a| { + a[idx].locate(offset, ty).unwrap_or_else(|| invalid_field()) + }) + } + + #[inline] + fn borrow_at_mut(&self, offset: usize, ty: TypeId) -> RefMut<'_, dyn Any> { + let (idx, offset) = split::(offset); + RefMut::map(self.borrow_mut(), |a| { + a[idx] + .locate_mut(offset, ty) + .unwrap_or_else(|| invalid_field()) + }) + } +} + +impl Root for RefCell> { + #[inline] + fn borrow_at(&self, offset: usize, ty: TypeId) -> Ref<'_, dyn Any> { + let (idx, offset) = split::(offset); + Ref::map(self.borrow(), |v| { + v[idx].locate(offset, ty).unwrap_or_else(|| invalid_field()) + }) + } + + #[inline] + fn borrow_at_mut(&self, offset: usize, ty: TypeId) -> RefMut<'_, dyn Any> { + let (idx, offset) = split::(offset); + RefMut::map(self.borrow_mut(), |v| { + v[idx] + .locate_mut(offset, ty) + .unwrap_or_else(|| invalid_field()) + }) + } +} + +// Creates pointers to the fields of the struct S pointed to by self. Use it +// through the field_ptr! macro. +pub trait FieldPtr { + // A pointer to the field at `offset(S::OFFSETS)`. The field has type U, + // as given by the (uncalled) `field`, and T is either U or, if U is an + // array, the type of its elements. + fn field_ptr>( + &self, + field: fn(&S) -> &U, + offset: fn(&S::Offsets) -> usize, + ) -> Ptr; +} + +impl FieldPtr for Ptr { + #[inline] + fn field_ptr>( + &self, + _field: fn(&S) -> &U, + offset: fn(&S::Offsets) -> usize, + ) -> Ptr { + let offset = offset(&S::OFFSETS); + // The byte offset of the struct in its allocation. + let start = || self.offset.wrapping_mul(S::byte_size()); + let (root, start): (Weak, usize) = match &self.kind { + PtrKind::Null => null_deref(), + PtrKind::StackSingle(w) | PtrKind::HeapSingle(w) => (w.clone(), start()), + PtrKind::StackArray(w) | PtrKind::HeapArray(w) => (w.clone(), start()), + PtrKind::StackVec(w) | PtrKind::HeapVec(w) => (w.clone(), start()), + PtrKind::Field(root, field) => (root.clone(), *field as usize + start()), + PtrKind::Reinterpreted(_) => { + let (alloc, byte_offset) = self.original_alloc().unwrap(); + return Ptr::from_original_alloc(alloc, byte_offset.wrapping_add(offset)); + } + }; + Ptr { + offset: 0, + kind: PtrKind::Field(root, (start + offset) as u32), + } + } +} + +impl FieldPtr for Value { + #[inline] + fn field_ptr>( + &self, + field: fn(&S) -> &U, + offset: fn(&S::Offsets) -> usize, + ) -> Ptr { + self.as_pointer().field_ptr(field, offset) + } +} + +// A pointer to field `field` of the struct that a Ptr or a Value holds, +// e.g., `field_ptr!(p, x)`. +#[macro_export] +macro_rules! field_ptr { + ($base:expr, $field:ident) => {{ + use $crate::FieldPtr as _; + ($base).field_ptr(|__s| &__s.$field, |__o| __o.$field) + }}; +} + +// Finds the object at a byte offset of a field for Record::locate. The +// implementation is chosen based on the type of the field, by autoref-based +// specialization: `(&&&&Locate(&field)).locate(...)` picks the first of the +// traits below that is implemented for the field's type. +#[doc(hidden)] +pub struct Locate<'a, T>(pub &'a T); + +#[doc(hidden)] +pub struct LocateMut<'a, T>(pub Cell>); + +impl<'a, T> LocateMut<'a, T> { + #[inline] + pub fn new(field: &'a mut T) -> Self { + Self(Cell::new(Some(field))) + } + #[inline] + fn take(&self) -> &'a mut T { + self.0.take().unwrap() + } +} + +#[doc(hidden)] +pub trait LocateRecord<'a> { + fn locate(self, offset: usize, ty: TypeId) -> Option<&'a dyn Any>; +} +#[doc(hidden)] +pub trait LocateRecords<'a> { + fn locate(self, offset: usize, ty: TypeId) -> Option<&'a dyn Any>; +} +#[doc(hidden)] +pub trait LocateArray<'a> { + fn locate(self, offset: usize, ty: TypeId) -> Option<&'a dyn Any>; +} +#[doc(hidden)] +pub trait LocateLeaf<'a> { + fn locate(self, offset: usize, ty: TypeId) -> Option<&'a dyn Any>; +} +#[doc(hidden)] +pub trait LocateRecordMut<'a> { + fn locate(self, offset: usize, ty: TypeId) -> Option<&'a mut dyn Any>; +} +#[doc(hidden)] +pub trait LocateRecordsMut<'a> { + fn locate(self, offset: usize, ty: TypeId) -> Option<&'a mut dyn Any>; +} +#[doc(hidden)] +pub trait LocateArrayMut<'a> { + fn locate(self, offset: usize, ty: TypeId) -> Option<&'a mut dyn Any>; +} +#[doc(hidden)] +pub trait LocateLeafMut<'a> { + fn locate(self, offset: usize, ty: TypeId) -> Option<&'a mut dyn Any>; +} + +// A struct. +impl<'a, R: Record> LocateRecord<'a> for &&&&Locate<'a, R> { + #[inline] + fn locate(self, offset: usize, ty: TypeId) -> Option<&'a dyn Any> { + self.0.locate(offset, ty) + } +} + +impl<'a, R: Record> LocateRecordMut<'a> for &&&&LocateMut<'a, R> { + #[inline] + fn locate(self, offset: usize, ty: TypeId) -> Option<&'a mut dyn Any> { + self.take().locate_mut(offset, ty) + } +} + +// An array of structs. +impl<'a, R: Record> LocateRecords<'a> for &&&Locate<'a, Box<[R]>> { + #[inline] + fn locate(self, offset: usize, ty: TypeId) -> Option<&'a dyn Any> { + // The Box itself is returned, as elems() looks for a Box<[R]>. + let array = self.0; + if offset == 0 && (ty == TypeId::of::() || ty == TypeId::of::>()) { + return Some(array as &dyn Any); + } + let (idx, offset) = split::(offset); + array.get(idx)?.locate(offset, ty) + } +} + +impl<'a, R: Record> LocateRecordsMut<'a> for &&&LocateMut<'a, Box<[R]>> { + #[inline] + fn locate(self, offset: usize, ty: TypeId) -> Option<&'a mut dyn Any> { + let array: &'a mut Box<[R]> = self.take(); + if offset == 0 && (ty == TypeId::of::() || ty == TypeId::of::>()) { + return Some(array); + } + let (idx, offset) = split::(offset); + array.get_mut(idx)?.locate_mut(offset, ty) + } +} + +// An array of anything else, which is where it ends. +impl<'a, T: 'static> LocateArray<'a> for &&Locate<'a, Box<[T]>> { + #[inline] + fn locate(self, offset: usize, ty: TypeId) -> Option<&'a dyn Any> { + (offset == 0 && (ty == TypeId::of::() || ty == TypeId::of::>())) + .then_some(self.0 as &dyn Any) + } +} + +impl<'a, T: 'static> LocateArrayMut<'a> for &&LocateMut<'a, Box<[T]>> { + #[inline] + fn locate(self, offset: usize, ty: TypeId) -> Option<&'a mut dyn Any> { + let array: &'a mut Box<[T]> = self.take(); + (offset == 0 && (ty == TypeId::of::() || ty == TypeId::of::>())) + .then_some(array as &mut dyn Any) + } +} + +// Anything else. +impl<'a, T: 'static> LocateLeaf<'a> for &Locate<'a, T> { + #[inline] + fn locate(self, offset: usize, ty: TypeId) -> Option<&'a dyn Any> { + (offset == 0 && ty == TypeId::of::()).then_some(self.0 as &dyn Any) + } +} + +impl<'a, T: 'static> LocateLeafMut<'a> for &LocateMut<'a, T> { + #[inline] + fn locate(self, offset: usize, ty: TypeId) -> Option<&'a mut dyn Any> { + (offset == 0 && ty == TypeId::of::()).then_some(self.take() as &mut dyn Any) + } +} + +// Used by #[derive(Record)]. +#[doc(hidden)] +#[macro_export] +macro_rules! __locate_field { + ($field:expr, $offset:expr, $ty:expr) => {{ + #[allow(unused_imports)] + use $crate::__field::{LocateArray, LocateLeaf, LocateRecord, LocateRecords}; + (&&&&$crate::__field::Locate($field)).locate($offset, $ty) + }}; +} + +#[doc(hidden)] +#[macro_export] +macro_rules! __locate_field_mut { + ($field:expr, $offset:expr, $ty:expr) => {{ + #[allow(unused_imports)] + use $crate::__field::{LocateArrayMut, LocateLeafMut, LocateRecordMut, LocateRecordsMut}; + (&&&&$crate::__field::LocateMut::new($field)).locate($offset, $ty) + }}; +} + +impl Ptr { + // Borrows the elements of the struct field pointed to by a Field pointer. + #[inline] + pub(crate) fn borrow_field<'a>(root: &'a dyn Root, field: u32) -> Ref<'a, [T]> { + Ref::map(root.borrow_at(field as usize, TypeId::of::()), |obj| { + elems::(obj) + }) + } + + #[inline] + pub(crate) fn borrow_field_mut<'a>(root: &'a dyn Root, field: u32) -> RefMut<'a, [T]> { + RefMut::map( + root.borrow_at_mut(field as usize, TypeId::of::()), + |obj| elems_mut::(obj), + ) + } +} + +impl Ptr { + pub(crate) fn field_len(root: &Weak, field: u32) -> usize { + let root = upgrade(root); + Self::borrow_field(&*root, field).len() + } +} + +#[inline] +pub(crate) fn upgrade(root: &Weak) -> Rc { + root.upgrade().unwrap_or_else(|| crate::rc::dangling()) +} + +#[cfg(test)] +mod tests { + use crate::{AsPointer, ByteRepr, Ptr, Record, Value}; + use std::cell::RefCell; + use std::rc::Rc; + + #[derive(Default, Record)] + struct Inner { + #[offset(0)] + a: i32, + #[offset(4)] + name: Box<[u8]>, + } + + impl ByteRepr for Inner { + fn byte_size() -> usize { + 8 + } + fn to_bytes(&self, buf: &mut [u8]) { + self.a.to_bytes(&mut buf[0..4]); + buf[4..8].copy_from_slice(&self.name); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + a: i32::from_bytes(&buf[0..4]), + name: Box::from(&buf[4..8]), + } + } + } + + #[derive(Default, Record)] + struct Outer { + #[offset(0)] + x: i32, + #[offset(4)] + inner: Inner, + #[offset(12)] + items: Box<[Inner]>, + #[offset(36)] + v: Value>, + } + + impl ByteRepr for Outer { + fn byte_size() -> usize { + 64 + } + } + + fn inner(a: i32) -> Inner { + Inner { + a, + name: Box::from(*b"abc\0"), + } + } + + fn outer() -> Value { + Rc::new(RefCell::new(Outer { + items: vec![inner(1), inner(2), inner(3)].into_boxed_slice(), + ..Default::default() + })) + } + + #[test] + fn read_write_field() { + let s = outer(); + let x = field_ptr!(s, x); + x.write(5); + assert_eq!(s.borrow().x, 5); + s.borrow_mut().x = 7; + assert_eq!(x.read(), 7); + x.with_mut(|v| *v += 1); + assert_eq!(s.borrow().x, 8); + assert_eq!(x.len(), 1); + assert_eq!(std::mem::size_of::>(), 32); + } + + #[test] + fn nested_field() { + let s = outer(); + let p = field_ptr!(s, inner); + let a = field_ptr!(p, a); + a.write(3); + assert_eq!(s.borrow().inner.a, 3); + assert_eq!(p.upgrade().deref().a, 3); + p.upgrade().deref_mut().a = 4; + assert_eq!(a.read(), 4); + p.with_mut(|p| p.a = 5); + assert_eq!(a.read(), 5); + } + + #[test] + fn array_field() { + let s: Value = Rc::new(RefCell::new(inner(1))); + let name: Ptr = field_ptr!(s, name); + assert_eq!(name.len(), 4); + assert_eq!(name.offset(1).read(), b'b'); + name.offset(2).write(b'x'); + assert_eq!(&*s.borrow().name, b"abx\0"); + assert_eq!(name.to_c_bytes(), b"abx"); + let whole: Ptr> = field_ptr!(s, name); + assert_eq!(whole.decay().offset(3), name.offset(3)); + assert_eq!(name.offset(3) - name.clone(), 3); + assert!(name.offset(1) < name.offset(2)); + name.with_slice_mut(2, |b| b.copy_from_slice(b"zy")); + assert_eq!(&*s.borrow().name, b"zyx\0"); + } + + #[test] + fn field_of_array_element() { + let s = outer(); + let items: Ptr = field_ptr!(s, items); + let a = field_ptr!(items.offset(1), a); + assert_eq!(a.read(), 2); + a.write(20); + assert_eq!(s.borrow().items[1].a, 20); + let name: Ptr = field_ptr!(items.offset(2), name); + name.offset(1).write(b'q'); + assert_eq!(&*s.borrow().items[2].name, b"aqc\0"); + let first = field_ptr!(items, a); + assert!(first < a); + assert_ne!(first, a); + assert_eq!(first, field_ptr!(items.offset(0), a)); + // A field of an element of an array. + let arr: Value> = + Rc::new(RefCell::new(vec![inner(1), inner(2)].into_boxed_slice())); + let a = field_ptr!((arr.as_pointer() as Ptr).offset(1), a); + assert_eq!(a.read(), 2); + } + + #[test] + fn vector_field() { + // Vectors are held in Values of their own, which are the roots of the + // field pointers into their elements. + let s = outer(); + *s.borrow().v.borrow_mut() = vec![inner(1), inner(2)]; + let v: Ptr = s.borrow().v.as_pointer(); + let a = field_ptr!(v.offset(1), a); + assert_eq!(a.read(), 2); + s.borrow().v.borrow_mut().push(inner(3)); + a.write(20); + assert_eq!(s.borrow().v.borrow()[1].a, 20); + let name: Ptr = field_ptr!(v.offset(2), name); + assert_eq!(name.offset(1).read(), b'b'); + } + + #[test] + fn reinterpret_field() { + let s: Value = Rc::new(RefCell::new(inner(0x01020304))); + let a = field_ptr!(s, a); + let bytes = a.reinterpret_cast::(); + assert_eq!(bytes.read(), 0x04); + bytes.write(0xff); + assert_eq!(s.borrow().a, 0x010203ff); + // The field is viewed as a separate allocation. + assert_eq!(bytes.len(), 4); + // A field of a reinterpreted struct is written through. + let raw: Ptr = Ptr::alloc_array(vec![0u8; 8].into_boxed_slice()); + let view: Ptr = raw.reinterpret_cast(); + let name: Ptr = field_ptr!(view, name); + name.offset(1).write(b'q'); + assert_eq!(raw.offset(5).read(), b'q'); + view.with_mut(|v| v.a = 0x11223344); + assert_eq!(raw.read(), 0x44); + raw.delete(); + } + + #[test] + #[should_panic(expected = "ub: dangling pointer")] + fn dangling_field() { + let p = { + let s = outer(); + field_ptr!(s, x) + }; + p.read(); + } +} diff --git a/libcc2rs/src/iterators.rs b/libcc2rs/src/iterators.rs index 1c64474ef6..3d875bee5d 100644 --- a/libcc2rs/src/iterators.rs +++ b/libcc2rs/src/iterators.rs @@ -259,7 +259,7 @@ pub struct StringIterator { ptr: Ptr, } -impl Iterator for StringIterator { +impl Iterator for StringIterator { type Item = Ptr; fn next(&mut self) -> Option { // stop before the null terminator at the last position diff --git a/libcc2rs/src/lib.rs b/libcc2rs/src/lib.rs index d573c4c615..59ac014790 100644 --- a/libcc2rs/src/lib.rs +++ b/libcc2rs/src/lib.rs @@ -1,12 +1,25 @@ // Copyright (c) 2022-present INESC-ID. // Distributed under the MIT license that can be found in the LICENSE file. +extern crate self as libcc2rs; + mod reinterpret; pub use reinterpret::ByteRepr; mod rc; pub use rc::*; +mod field; +pub use field::{Elems, FieldPtr, Record}; +// Used by #[derive(Record)]. +#[doc(hidden)] +pub mod __field { + pub use crate::field::{ + Locate, LocateArray, LocateArrayMut, LocateLeaf, LocateLeafMut, LocateMut, LocateRecord, + LocateRecordMut, LocateRecords, LocateRecordsMut, + }; +} + mod cstr; mod void; @@ -56,4 +69,4 @@ pub use fd::*; mod format; pub use format::*; -pub use libcc2rs_macros::{ByteRepr, FnPtrArg, VaArg, goto, goto_block, switch}; +pub use libcc2rs_macros::{ByteRepr, FnPtrArg, Record, VaArg, goto, goto_block, switch}; diff --git a/libcc2rs/src/rc.rs b/libcc2rs/src/rc.rs index 5fca8d88f0..b62dd19ad0 100644 --- a/libcc2rs/src/rc.rs +++ b/libcc2rs/src/rc.rs @@ -5,12 +5,13 @@ use crate::{PostfixDec, PostfixInc, PrefixDec, PrefixInc}; use std::any::{Any, TypeId}; use std::{ - cell::{Ref, RefCell}, + cell::{Ref, RefCell, RefMut}, fmt, ops::Sub, rc::{Rc, Weak}, }; +use crate::field::{Root, upgrade as upgrade_root}; use crate::reinterpret::{ByteRepr, OriginalAlloc, with_scratch}; pub type Value = Rc>; @@ -34,6 +35,10 @@ pub(crate) enum PtrKind { HeapArray(Weak>>), StackVec(Weak>>), HeapVec(Weak>>), + // A field of a struct: the allocation that holds the struct, and the + // byte offset of the field in it. The field is either a T, or an array of + // T that the pointer indexes into. + Field(Weak, u32), Reinterpreted(Rc), } @@ -47,6 +52,11 @@ pub enum StrongPtr { rc: Rc>>, offset: usize, }, + Field { + root: Rc, + field: u32, + offset: usize, + }, Reinterpreted { alloc: OriginalAlloc, byte_offset: usize, @@ -65,6 +75,11 @@ impl StrongPtr { StrongPtr::StackSingle(rc) => rc.borrow(), StrongPtr::Vec { rc, offset } => Ref::map(rc.borrow(), |v| &v[*offset]), StrongPtr::StackArray { rc, offset } => Ref::map(rc.borrow(), |a| &a[*offset]), + StrongPtr::Field { + root, + field, + offset, + } => Ref::map(Ptr::borrow_field(&**root, *field), |f| &f[*offset]), StrongPtr::Reinterpreted { alloc, byte_offset, @@ -73,6 +88,25 @@ impl StrongPtr { } } + // Writes through reinterpreted pointers must use with_mut instead, as + // they are written through to the original allocation. + #[inline(always)] + pub fn deref_mut(&self) -> RefMut<'_, T> { + match self { + StrongPtr::StackSingle(rc) => rc.borrow_mut(), + StrongPtr::Vec { rc, offset } => RefMut::map(rc.borrow_mut(), |v| &mut v[*offset]), + StrongPtr::StackArray { rc, offset } => { + RefMut::map(rc.borrow_mut(), |a| &mut a[*offset]) + } + StrongPtr::Field { + root, + field, + offset, + } => RefMut::map(Ptr::borrow_field_mut(&**root, *field), |f| &mut f[*offset]), + StrongPtr::Reinterpreted { .. } => panic!("mutable reference to reinterpreted memory"), + } + } + #[inline(never)] fn deref_reinterpreted<'a>( alloc: &OriginalAlloc, @@ -98,6 +132,7 @@ impl fmt::Debug for PtrKind { PtrKind::HeapSingle(w) => write!(f, "HeapSingle({:?})", w.as_ptr()), PtrKind::StackArray(w) => write!(f, "StackArray({:?})", w.as_ptr()), PtrKind::HeapArray(w) => write!(f, "HeapArray({:?})", w.as_ptr()), + PtrKind::Field(root, field) => write!(f, "Field({:?}, {field})", root.as_ptr()), PtrKind::Reinterpreted(data) => { write!(f, "Reinterpreted(0x{:x})", data.alloc.address()) } @@ -116,6 +151,7 @@ impl Clone for PtrKind { PtrKind::HeapSingle(weak) => PtrKind::HeapSingle(weak.clone()), PtrKind::StackArray(weak) => PtrKind::StackArray(weak.clone()), PtrKind::HeapArray(weak) => PtrKind::HeapArray(weak.clone()), + PtrKind::Field(root, field) => PtrKind::Field(root.clone(), *field), PtrKind::Reinterpreted(data) => PtrKind::Reinterpreted(Rc::clone(data)), } } @@ -128,6 +164,7 @@ impl PtrKind { PtrKind::StackSingle(w) | PtrKind::HeapSingle(w) => w.as_ptr() as usize, PtrKind::StackVec(w) | PtrKind::HeapVec(w) => w.as_ptr() as usize, PtrKind::StackArray(w) | PtrKind::HeapArray(w) => w.as_ptr() as usize, + PtrKind::Field(root, _) => root.as_ptr() as *const () as usize, PtrKind::Reinterpreted(data) => data.alloc.address(), } } @@ -274,6 +311,9 @@ impl Ptr { fn byte_offset(&self) -> usize { match &self.kind { PtrKind::Reinterpreted(_) => self.offset, + PtrKind::Field(_, field) => { + (*field as usize).wrapping_add(self.offset.wrapping_mul(std::mem::size_of::())) + } _ => self.offset.wrapping_mul(std::mem::size_of::()), } } @@ -290,7 +330,10 @@ impl Ptr { } #[inline] - pub fn len(&self) -> usize { + pub fn len(&self) -> usize + where + T: 'static, + { match &self.kind { PtrKind::Null => 0, PtrKind::StackSingle(_) | PtrKind::HeapSingle(_) => 1, @@ -300,12 +343,16 @@ impl Ptr { PtrKind::StackArray(weak) | PtrKind::HeapArray(weak) => { weak.upgrade().expect("ub: dangling pointer").borrow().len() } + PtrKind::Field(root, field) => Self::field_len(root, *field), PtrKind::Reinterpreted(data) => data.alloc.total_byte_len() / data.elem_byte_size, } } #[inline] - pub fn is_empty(&self) -> bool { + pub fn is_empty(&self) -> bool + where + T: 'static, + { match &self.kind { PtrKind::Null => true, PtrKind::StackSingle(_) | PtrKind::HeapSingle(_) => false, @@ -319,6 +366,7 @@ impl Ptr { .expect("ub: dangling pointer") .borrow() .is_empty(), + PtrKind::Field(root, field) => Self::field_len(root, *field) == 0, PtrKind::Reinterpreted(data) => self.offset >= data.alloc.total_byte_len(), } } @@ -344,7 +392,10 @@ impl Ptr { } #[inline] - pub fn to_last(&self) -> Self { + pub fn to_last(&self) -> Self + where + T: 'static, + { Self { kind: self.kind.clone(), offset: self.len().wrapping_sub(1).wrapping_mul(self.elem_step()), @@ -352,7 +403,10 @@ impl Ptr { } #[inline] - pub fn to_end(&self) -> Self { + pub fn to_end(&self) -> Self + where + T: 'static, + { Self { kind: self.kind.clone(), offset: self.len().wrapping_mul(self.elem_step()), @@ -374,6 +428,11 @@ impl Ptr { rc: weak.upgrade().unwrap_or_else(|| dangling()), offset: self.offset, }, + PtrKind::Field(root, field) => StrongPtr::Field { + root: upgrade_root(root), + field: *field, + offset: self.offset, + }, PtrKind::Reinterpreted(data) => StrongPtr::Reinterpreted { alloc: data.alloc.clone(), byte_offset: self.offset, @@ -425,6 +484,10 @@ impl Ptr { OriginalAlloc::slice(weak), self.offset.wrapping_mul(T::byte_size()), ), + PtrKind::Field(root, field) => ( + OriginalAlloc::field::(root, *field), + self.offset.wrapping_mul(T::byte_size()), + ), PtrKind::Reinterpreted(data) => (data.alloc.clone(), self.offset), }) } @@ -470,6 +533,10 @@ impl Ptr { let mut borrow = rc.borrow_mut(); f(&mut borrow[self.offset]) } + PtrKind::Field(root, field) => { + let root = upgrade_root(root); + f(&mut Self::borrow_field_mut(&*root, *field)[self.offset]) + } PtrKind::Reinterpreted(data) => Self::with_mut_reinterpreted(data, self.offset, f), PtrKind::Null => null_deref(), } @@ -515,6 +582,10 @@ impl Ptr { let borrow = rc.borrow(); f(&borrow[self.offset]) } + PtrKind::Field(root, field) => { + let root = upgrade_root(root); + f(&Self::borrow_field(&*root, *field)[self.offset]) + } PtrKind::Reinterpreted(data) => Self::with_reinterpreted(data, self.offset, f), PtrKind::Null => null_deref(), } @@ -535,14 +606,14 @@ impl Ptr { #[cold] #[inline(never)] #[track_caller] -fn null_deref() -> ! { +pub(crate) fn null_deref() -> ! { panic!("ub: null pointer") } #[cold] #[inline(never)] #[track_caller] -fn dangling() -> ! { +pub(crate) fn dangling() -> ! { panic!("ub: dangling pointer") } @@ -567,6 +638,10 @@ impl Ptr { let mut b = rc.borrow_mut(); f(&mut b[off..off + len]) } + PtrKind::Field(root, field) => { + let root = upgrade_root(root); + f(&mut Self::borrow_field_mut(&*root, *field)[off..off + len]) + } PtrKind::Reinterpreted(data) => with_scratch(len, |buf| { data.alloc.read_bytes(off, buf); let r = f(buf); @@ -596,6 +671,10 @@ impl Ptr { let b = rc.borrow(); f(&b[off..off + len]) } + PtrKind::Field(root, field) => { + let root = upgrade_root(root); + f(&Self::borrow_field(&*root, *field)[off..off + len]) + } PtrKind::Reinterpreted(data) => with_scratch(len, |buf| { data.alloc.read_bytes(off, buf); f(buf) @@ -628,7 +707,7 @@ impl Ptr { } } -impl Ptr { +impl Ptr { pub fn sort(&self, last: usize) { match self.kind { PtrKind::Null => panic!("ub: dereference of null pointer"), @@ -643,6 +722,10 @@ impl Ptr { let strong = weak.upgrade().expect("ub: dangling pointer"); (*strong.borrow_mut())[self.get_offset()..last].sort(); } + PtrKind::Field(ref root, field) => { + let root = upgrade_root(root); + Self::borrow_field_mut(&*root, field)[self.offset..last].sort(); + } PtrKind::Reinterpreted(_) => { panic!("sorting not supported for reinterpreted pointers") } @@ -650,7 +733,7 @@ impl Ptr { } } -impl Ptr { +impl Ptr { pub fn sort_with_cmp(&self, last: usize, mut cmp: F) where F: FnMut(Ptr, Ptr) -> bool, @@ -688,6 +771,15 @@ impl Ptr { let mut borrow = strong.borrow_mut(); sort(&mut borrow, self.get_offset(), last, &mut cmp); } + PtrKind::Field(ref root, field) => { + let root = upgrade_root(root); + sort( + &mut Self::borrow_field_mut(&*root, field), + self.offset, + last, + &mut cmp, + ); + } PtrKind::Reinterpreted(_) => { panic!("sorting not supported for reinterpreted pointers") } @@ -697,7 +789,7 @@ impl Ptr { impl IntoIterator for &Ptr where - T: Clone, + T: Clone + 'static, { type Item = Ptr; type IntoIter = Ptr; @@ -707,7 +799,7 @@ where } } -impl Iterator for Ptr { +impl Iterator for Ptr { type Item = Ptr; fn next(&mut self) -> Option { if self.get_offset() < self.len() { @@ -960,6 +1052,11 @@ impl Ptr> { offset: self.offset, kind: PtrKind::HeapVec(weak.clone()), }, + // A vector that is a field. + PtrKind::Field(root, field) if self.offset == 0 => Ptr { + offset: 0, + kind: PtrKind::Field(root.clone(), *field), + }, _ => panic!("ub: invalid decay"), } } @@ -978,6 +1075,11 @@ impl Ptr> { offset: self.offset, kind: PtrKind::HeapArray(weak.clone()), }, + // An array that is a field. + PtrKind::Field(root, field) if self.offset == 0 => Ptr { + offset: 0, + kind: PtrKind::Field(root.clone(), *field), + }, _ => panic!("ub: invalid decay"), } } @@ -1007,6 +1109,7 @@ impl ToOwnedOption for Ptr { panic!("Can't own a stack variable") } PtrKind::StackVec(_) | PtrKind::HeapVec(_) => panic!("Can't own a vector"), + PtrKind::Field(..) => panic!("Can't own a field"), PtrKind::HeapArray(_) => panic!("Can't own an array variable as single"), PtrKind::Reinterpreted(_) => panic!("Can't own a reinterpreted pointer"), } @@ -1033,6 +1136,7 @@ impl ToOwnedOption> for Ptr { panic!("Can't own a stack variable") } PtrKind::StackVec(_) | PtrKind::HeapVec(_) => panic!("Can't own a vector"), + PtrKind::Field(..) => panic!("Can't own a field"), PtrKind::HeapSingle(_) => panic!("Can't own a single variable as an array"), PtrKind::Reinterpreted(_) => panic!("Can't own a reinterpreted pointer"), } @@ -1052,6 +1156,7 @@ impl fmt::Debug for Ptr { PtrKind::StackVec(w) | PtrKind::HeapVec(w) => { (Weak::as_ptr(w) as usize).wrapping_add(self.byte_offset()) } + PtrKind::Field(..) => self.kind.address().wrapping_add(self.byte_offset()), PtrKind::Reinterpreted(data) => data.alloc.address().wrapping_add(self.byte_offset()), }; write!(f, "0x{:x}", addr) diff --git a/libcc2rs/src/reinterpret.rs b/libcc2rs/src/reinterpret.rs index ed25c6d70a..6ad2b73813 100644 --- a/libcc2rs/src/reinterpret.rs +++ b/libcc2rs/src/reinterpret.rs @@ -8,6 +8,9 @@ use std::{ rc::{Rc, Weak}, }; +use crate::field::{Root, upgrade as upgrade_root}; +use crate::rc::Ptr; + pub trait ByteRepr: 'static { fn byte_size() -> usize where @@ -140,61 +143,94 @@ pub(crate) trait AllocOps { // It is a plain weak reference plus a reference to the (stateless) operations // for the concrete storage type. Hence, it doesn't require any heap // allocation on its own. +// The storage of a field of a struct is the field alone, as if it was a +// separate allocation. #[derive(Clone)] pub struct OriginalAlloc { - weak: Weak, + storage: Storage, ops: &'static dyn AllocOps, } +#[derive(Clone)] +enum Storage { + Alloc(Weak), + Field(Weak, u32), +} + +// The storage passed to the AllocOps of a field. +struct FieldStorage { + root: Rc, + field: u32, +} + impl OriginalAlloc { pub(crate) fn single(weak: &Weak>) -> Self { Self { - weak: weak.clone(), + storage: Storage::Alloc(weak.clone()), ops: &SingleOps::(PhantomData), } } pub(crate) fn slice(weak: &Weak>) -> Self { Self { - weak: weak.clone(), + storage: Storage::Alloc(weak.clone()), ops: &SliceOps::(PhantomData), } } + // A field whose elements are of type T. + pub(crate) fn field(root: &Weak, field: u32) -> Self { + Self { + storage: Storage::Field(root.clone(), field), + ops: &FieldOps::(PhantomData), + } + } + #[inline] - fn upgrade(&self) -> Rc { - self.weak.upgrade().expect("ub: dangling pointer") + fn with_storage(&self, f: impl FnOnce(&dyn Any) -> R) -> R { + match &self.storage { + Storage::Alloc(weak) => f(&*weak.upgrade().expect("ub: dangling pointer")), + Storage::Field(root, field) => f(&FieldStorage { + root: upgrade_root(root), + field: *field, + }), + } } #[inline] pub(crate) fn read_bytes(&self, byte_offset: usize, buf: &mut [u8]) { - let rc = self.upgrade(); - self.ops.read_bytes(&*rc, byte_offset, buf); + self.with_storage(|storage| self.ops.read_bytes(storage, byte_offset, buf)); } #[inline] pub(crate) fn write_bytes(&self, byte_offset: usize, data: &[u8]) { - let rc = self.upgrade(); - self.ops.write_bytes(&*rc, byte_offset, data); + self.with_storage(|storage| self.ops.write_bytes(storage, byte_offset, data)); } pub(crate) fn total_byte_len(&self) -> usize { - let rc = self.upgrade(); - self.ops.total_byte_len(&*rc) + self.with_storage(|storage| self.ops.total_byte_len(storage)) } // Stable address used for pointer equality across PtrKind variants. pub(crate) fn address(&self) -> usize { - self.weak.as_ptr() as *const () as usize + match &self.storage { + Storage::Alloc(weak) => weak.as_ptr() as *const () as usize, + Storage::Field(root, field) => { + (root.as_ptr() as *const () as usize).wrapping_add(*field as usize) + } + } } pub(crate) fn delete(&self) { - assert_eq!(Weak::strong_count(&self.weak), 1, "ub: invalid delete"); + let Storage::Alloc(weak) = &self.storage else { + panic!("ub: invalid delete"); + }; + assert_eq!(Weak::strong_count(weak), 1, "ub: invalid delete"); unsafe { - let strong = self.upgrade(); + let strong = weak.upgrade().expect("ub: dangling pointer"); Rc::from_raw(Rc::as_ptr(&strong)); } - assert_eq!(Weak::strong_count(&self.weak), 0, "ub: double free"); + assert_eq!(Weak::strong_count(weak), 0, "ub: double free"); } } @@ -342,3 +378,24 @@ impl AllocOps for SliceOps { cell.borrow().as_slice().len() * ::byte_size() } } + +struct FieldOps(PhantomData T>); + +impl AllocOps for FieldOps { + fn read_bytes(&self, storage: &dyn Any, byte_offset: usize, buf: &mut [u8]) { + let storage = storage.downcast_ref::().unwrap(); + let elems = Ptr::::borrow_field(&*storage.root, storage.field); + slice_read_bytes(&elems, byte_offset, buf); + } + + fn write_bytes(&self, storage: &dyn Any, byte_offset: usize, data: &[u8]) { + let storage = storage.downcast_ref::().unwrap(); + let mut elems = Ptr::::borrow_field_mut(&*storage.root, storage.field); + slice_write_bytes(&mut elems, byte_offset, data); + } + + fn total_byte_len(&self, storage: &dyn Any) -> usize { + let storage = storage.downcast_ref::().unwrap(); + Ptr::::borrow_field(&*storage.root, storage.field).len() * T::byte_size() + } +} diff --git a/rules/algorithm/tgt_refcount.rs b/rules/algorithm/tgt_refcount.rs index 66b8a8018e..ef02721d15 100644 --- a/rules/algorithm/tgt_refcount.rs +++ b/rules/algorithm/tgt_refcount.rs @@ -6,7 +6,7 @@ use std::cell::RefCell; use std::io::Write; use std::rc::Rc; -fn f1(a0: Ptr, a1: Ptr) { +fn f1(a0: Ptr, a1: Ptr) { a0.sort(a1.get_offset()) } @@ -36,7 +36,7 @@ fn f3(a0: Ptr, a1: Ptr, a2: T1) -> Ptr ) } -fn f6(a0: Ptr, a1: Ptr, a2: T2) +fn f6(a0: Ptr, a1: Ptr, a2: T2) where T2: Callable2, Ptr, bool>, { diff --git a/rules/deque/tgt_refcount.rs b/rules/deque/tgt_refcount.rs index b70e9d05de..2cd9d36744 100644 --- a/rules/deque/tgt_refcount.rs +++ b/rules/deque/tgt_refcount.rs @@ -5,7 +5,7 @@ use libcc2rs::*; use std::cell::RefCell; use std::rc::Rc; -fn f1(a0: Ptr) -> Ptr { +fn f1(a0: Ptr) -> Ptr { a0.to_last() } diff --git a/rules/vector/tgt_refcount.rs b/rules/vector/tgt_refcount.rs index 6208b07155..c0371c65df 100644 --- a/rules/vector/tgt_refcount.rs +++ b/rules/vector/tgt_refcount.rs @@ -45,7 +45,7 @@ fn f9(a0: Ptr) -> Ptr { a0 } -fn f10(a0: Ptr) -> Ptr { +fn f10(a0: Ptr) -> Ptr { a0.to_last() } @@ -53,7 +53,7 @@ fn f13(a0: Ptr) -> Ptr { a0 } -fn f17(a0: Ptr) -> Ptr { +fn f17(a0: Ptr) -> Ptr { a0.to_end() } @@ -140,7 +140,7 @@ fn f37, T2: Clone + ByteRepr>(a0: Ptr, a1: Ptr) -> Vec>() } -fn f40(a0: Ptr) -> Ptr { +fn f40(a0: Ptr) -> Ptr { a0.to_end() } @@ -163,7 +163,7 @@ fn f43(a0: Ptr) -> Ptr { a0 } -fn f44(a0: Ptr) -> Ptr { +fn f44(a0: Ptr) -> Ptr { a0.to_end() } @@ -175,7 +175,7 @@ fn f50(a0: Ptr>, a1: usize) -> Ptr> { } } -fn f51(a0: Ptr) -> Ptr { +fn f51(a0: Ptr) -> Ptr { a0.to_last() } @@ -206,7 +206,7 @@ fn f55(a0: Ptr>, a1: &mut Vec) { a0.write(std::mem::take(&mut *a1)) } -fn f57(a0: Ptr) -> Ptr { +fn f57(a0: Ptr) -> Ptr { a0.to_end() } @@ -232,7 +232,7 @@ fn f68(a0: Ptr) -> Ptr { a0 } -fn f69(a0: Ptr) -> Ptr { +fn f69(a0: Ptr) -> Ptr { a0.to_last() } @@ -240,7 +240,7 @@ fn f72(a0: Ptr) -> Ptr { a0 } -fn f76(a0: Ptr) -> Ptr { +fn f76(a0: Ptr) -> Ptr { a0.to_end() } @@ -323,7 +323,7 @@ fn f95(a0: Ptr) -> Ptr { a0 } -fn f96(a0: Ptr) -> Ptr { +fn f96(a0: Ptr) -> Ptr { a0.to_end() } @@ -331,7 +331,7 @@ fn f98(a0: Ptr, a1: usize) -> Ptr { a0.offset(a1 as isize) } -fn f99(a0: Ptr) -> Ptr { +fn f99(a0: Ptr) -> Ptr { a0.to_last() } @@ -354,7 +354,7 @@ fn f103(a0: Ptr>, a1: &mut Vec) { a0.write(std::mem::take(&mut *a1)) } -fn f104(a0: Ptr) -> Ptr { +fn f104(a0: Ptr) -> Ptr { a0.to_end() } diff --git a/tests/multi-file/cross_tu_tag_collision/out/refcount/cross_tu_tag_collision.rs b/tests/multi-file/cross_tu_tag_collision/out/refcount/cross_tu_tag_collision.rs index 0794396fd5..5a7c7d210d 100644 --- a/tests/multi-file/cross_tu_tag_collision/out/refcount/cross_tu_tag_collision.rs +++ b/tests/multi-file/cross_tu_tag_collision/out/refcount/cross_tu_tag_collision.rs @@ -6,34 +6,28 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct widget { - pub id: Value, -} -impl Clone for widget { - fn clone(&self) -> Self { - Self { - id: Rc::new(RefCell::new((*self.id.borrow()).clone())), - } - } + #[offset(0)] + pub id: i32, } impl ByteRepr for widget { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.id.borrow()).to_bytes(&mut buf[0..4]); + self.id.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - id: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + id: ::from_bytes(&buf[0..4]), } } } pub fn a_value_0() -> i32 { let w: Value = >::default(); - (*(*w.borrow()).id.borrow_mut()) = 11; - return (*(*w.borrow()).id.borrow()); + (*w.borrow_mut()).id = 11; + return { (*w.borrow()).id }; } pub fn main() { __cpp2rust_init_globals(); diff --git a/tests/multi-file/defaulted_move_cross_tu/out/refcount/defaulted_move_cross_tu.rs b/tests/multi-file/defaulted_move_cross_tu/out/refcount/defaulted_move_cross_tu.rs index 63cbe7b220..d19df7f0df 100644 --- a/tests/multi-file/defaulted_move_cross_tu/out/refcount/defaulted_move_cross_tu.rs +++ b/tests/multi-file/defaulted_move_cross_tu/out/refcount/defaulted_move_cross_tu.rs @@ -6,10 +6,12 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive()] +#[derive(Record)] pub struct S { + #[offset(0)] pub v: Value>, - pub n: Value>, + #[offset(24)] + pub n: Box<[i32]>, } impl S { pub fn new(x: i32) -> Self { @@ -19,19 +21,21 @@ impl S { (*x.borrow()); ((*x.borrow()) as usize) as usize ])), - n: Rc::new(RefCell::new(Box::new([(*x.borrow()), ((*x.borrow()) + 1)]))), + n: Box::new([(*x.borrow()), ((*x.borrow()) + 1)]), })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } pub fn move_from(_a0: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new(std::mem::take( - &mut (*(*_a0.upgrade().deref()).v.borrow_mut()), - ))), - n: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 2, _>( - |__i: usize| (*(*_a0.upgrade().deref()).n.borrow())[(__i) as usize], - )))), + v: { + Rc::new(RefCell::new( + _a0.with(|__s: &S| std::mem::take(&mut (*__s.v.borrow_mut()))), + )) + }, + n: Box::new(std::array::from_fn::<_, 2, _>(|__i: usize| { + (*_a0.upgrade().deref()).n[(__i) as usize] + })), })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() @@ -41,18 +45,36 @@ impl Default for S { fn default() -> Self { S { v: Rc::new(RefCell::new(Default::default())), - n: Rc::new(RefCell::new((0..2).map(|_| 0_i32).collect::>())), + n: (0..2).map(|_| 0_i32).collect::>(), } } } +<<<<<<< HEAD impl ByteRepr for S {} +======= +impl ByteRepr for S { + fn byte_size() -> usize { + 32 + } + fn to_bytes(&self, buf: &mut [u8]) { + self.v.to_bytes(&mut buf[0..24]); + self.n.to_bytes(&mut buf[24..32]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + v: >>::from_bytes(&buf[0..24]), + n: >::from_bytes(&buf[24..32]), + } + } +} +>>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub fn sum_0(s: Ptr) -> i32 { return { let _lhs = { - let _lhs = ((*(*s.upgrade().deref()).v.borrow()).len() as i32); - _lhs + (*(*s.upgrade().deref()).n.borrow())[(0) as usize] + let _lhs = (s.with(|__s: &S| (*__s.v.borrow()).len()) as i32); + _lhs + s.with(|__s: &S| __s.n[(0) as usize]) }; - _lhs + (*(*s.upgrade().deref()).n.borrow())[(1) as usize] + _lhs + s.with(|__s: &S| __s.n[(1) as usize]) }; } pub fn main() { @@ -80,17 +102,14 @@ pub trait SImpl { } impl SImpl for Ptr { fn move_assign(&self, _a0: Ptr) -> Ptr { - ((*(*self).upgrade().deref()).v.as_pointer() as Ptr>).write(std::mem::take( - &mut (*(*_a0.upgrade().deref()).v.borrow_mut()), - )); + ((*self).with(|__s: &S| __s.v.as_pointer()) as Ptr>) + .write(_a0.with(|__s: &S| std::mem::take(&mut (*__s.v.borrow_mut())))); { - (((*(*self).upgrade().deref()).n.as_pointer()) as Ptr) - .to_any() - .memcpy( - &(((*_a0.upgrade().deref()).n.as_pointer()) as Ptr).to_any(), - 8_usize as usize, - ); - (((*(*self).upgrade().deref()).n.as_pointer()) as Ptr).to_any() + ((field_ptr!((*self), n)) as Ptr).to_any().memcpy( + &((field_ptr!(_a0, n)) as Ptr).to_any(), + 8_usize as usize, + ); + ((field_ptr!((*self), n)) as Ptr).to_any() }; return (*self).clone(); } diff --git a/tests/multi-file/friend_operator/out/refcount/friend_operator.rs b/tests/multi-file/friend_operator/out/refcount/friend_operator.rs index 3b30db7ad0..c8e5156df8 100644 --- a/tests/multi-file/friend_operator/out/refcount/friend_operator.rs +++ b/tests/multi-file/friend_operator/out/refcount/friend_operator.rs @@ -6,33 +6,25 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct S { - pub a: Value, - pub b: Value, -} -impl Clone for S { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - a: Rc::new(RefCell::new((*self.a.borrow()))), - b: Rc::new(RefCell::new((*self.b.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub a: i32, + #[offset(4)] + pub b: i32, } impl ByteRepr for S { fn byte_size() -> usize { 8 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.a.borrow()).to_bytes(&mut buf[0..4]); - (*self.b.borrow()).to_bytes(&mut buf[4..8]); + self.a.to_bytes(&mut buf[0..4]); + self.b.to_bytes(&mut buf[4..8]); } fn from_bytes(buf: &[u8]) -> Self { Self { - a: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - b: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + a: ::from_bytes(&buf[0..4]), + b: ::from_bytes(&buf[4..8]), } } } @@ -41,18 +33,9 @@ pub fn main() { std::process::exit(main_0()); } fn main_0() -> i32 { - let s1: Value = Rc::new(RefCell::new(S { - a: Rc::new(RefCell::new(1)), - b: Rc::new(RefCell::new(2)), - })); - let s2: Value = Rc::new(RefCell::new(S { - a: Rc::new(RefCell::new(1)), - b: Rc::new(RefCell::new(2)), - })); - let s3: Value = Rc::new(RefCell::new(S { - a: Rc::new(RefCell::new(1)), - b: Rc::new(RefCell::new(3)), - })); + let s1: Value = Rc::new(RefCell::new(S { a: 1, b: 2 })); + let s2: Value = Rc::new(RefCell::new(S { a: 1, b: 2 })); + let s3: Value = Rc::new(RefCell::new(S { a: 1, b: 3 })); assert!( ({ let _x: Ptr = s1.as_pointer(); @@ -90,11 +73,11 @@ fn main_0() -> i32 { } pub fn operator_eq_0(x: Ptr, y: Ptr) -> bool { return ({ - let _lhs = (*(*x.upgrade().deref()).a.borrow()); - _lhs == (*(*y.upgrade().deref()).a.borrow()) + let _lhs = x.with(|__s: &S| __s.a); + _lhs == y.with(|__s: &S| __s.a) }) && ({ - let _lhs = (*(*x.upgrade().deref()).b.borrow()); - _lhs == (*(*y.upgrade().deref()).b.borrow()) + let _lhs = x.with(|__s: &S| __s.b); + _lhs == y.with(|__s: &S| __s.b) }); } pub fn operator_ne_1(x: Ptr, y: Ptr) -> bool { @@ -106,14 +89,14 @@ pub fn operator_ne_1(x: Ptr, y: Ptr) -> bool { } pub fn operator_lt_2(x: Ptr, y: Ptr) -> bool { return ({ - let _lhs = (*(*x.upgrade().deref()).a.borrow()); - _lhs < (*(*y.upgrade().deref()).a.borrow()) + let _lhs = x.with(|__s: &S| __s.a); + _lhs < y.with(|__s: &S| __s.a) }) || (({ - let _lhs = (*(*x.upgrade().deref()).a.borrow()); - _lhs == (*(*y.upgrade().deref()).a.borrow()) + let _lhs = x.with(|__s: &S| __s.a); + _lhs == y.with(|__s: &S| __s.a) }) && ({ - let _lhs = (*(*x.upgrade().deref()).b.borrow()); - _lhs < (*(*y.upgrade().deref()).b.borrow()) + let _lhs = x.with(|__s: &S| __s.b); + _lhs < y.with(|__s: &S| __s.b) })); } pub fn compare_3(x: Ptr, y: Ptr) -> i32 { diff --git a/tests/multi-file/opaque_forward_decl/out/refcount/opaque_forward_decl.rs b/tests/multi-file/opaque_forward_decl/out/refcount/opaque_forward_decl.rs index 1d9a107f94..ecdb3b3920 100644 --- a/tests/multi-file/opaque_forward_decl/out/refcount/opaque_forward_decl.rs +++ b/tests/multi-file/opaque_forward_decl/out/refcount/opaque_forward_decl.rs @@ -6,31 +6,25 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct container { - pub p: Value>, - pub x: Value, -} -impl Clone for container { - fn clone(&self) -> Self { - Self { - p: Rc::new(RefCell::new((*self.p.borrow()).clone())), - x: Rc::new(RefCell::new((*self.x.borrow()).clone())), - } - } + #[offset(0)] + pub p: Ptr, + #[offset(8)] + pub x: i32, } impl ByteRepr for container { fn byte_size() -> usize { 16 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.p.borrow()).to_bytes(&mut buf[0..8]); - (*self.x.borrow()).to_bytes(&mut buf[8..12]); + self.p.to_bytes(&mut buf[0..8]); + self.x.to_bytes(&mut buf[8..12]); } fn from_bytes(buf: &[u8]) -> Self { Self { - p: Rc::new(RefCell::new(>::from_bytes(&buf[0..8]))), - x: Rc::new(RefCell::new(::from_bytes(&buf[8..12]))), + p: >::from_bytes(&buf[0..8]), + x: ::from_bytes(&buf[8..12]), } } } @@ -40,17 +34,17 @@ pub fn main() { } fn main_0() -> i32 { let c: Value = Rc::new(RefCell::new(container { - p: Rc::new(RefCell::new(Ptr::::null())), - x: Rc::new(RefCell::new(42)), + p: Ptr::::null(), + x: 42, })); ({ touch_0((c.as_pointer())) }); - assert!(((((*(*c.borrow()).x.borrow()) == 42) as i32) != 0)); - assert!(((((*(*c.borrow()).p.borrow()).is_null()) as i32) != 0)); + assert!(((({ (*c.borrow()).x } == 42) as i32) != 0)); + assert!((((({ (*c.borrow()).p.clone() }).is_null()) as i32) != 0)); return 0; } pub fn touch_0(c: Ptr) { let c: Value> = Rc::new(RefCell::new(c)); - &(*(*(*c.borrow()).upgrade().deref()).p.borrow()); + &((*c.borrow()).with(|__s: &container| (__s.p).clone())); } #[derive(Clone, Copy, Default, ByteRepr, VaArg, FnPtrArg)] pub struct opaque; diff --git a/tests/multi-file/out_of_line_methods_cross_tu/out/refcount/out_of_line_methods_cross_tu.rs b/tests/multi-file/out_of_line_methods_cross_tu/out/refcount/out_of_line_methods_cross_tu.rs index 8672fdd6de..f4ebd27970 100644 --- a/tests/multi-file/out_of_line_methods_cross_tu/out/refcount/out_of_line_methods_cross_tu.rs +++ b/tests/multi-file/out_of_line_methods_cross_tu/out/refcount/out_of_line_methods_cross_tu.rs @@ -6,11 +6,13 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct S { - pub v: Value, + #[offset(0)] + pub v: i32, } impl S {} +<<<<<<< HEAD impl Clone for S { fn clone(&self) -> Self { let __this: Value = Rc::new(RefCell::new(Self { @@ -21,24 +23,56 @@ impl Clone for S { } } impl ByteRepr for S {} +======= +impl ByteRepr for S { + fn byte_size() -> usize { + 4 + } + fn to_bytes(&self, buf: &mut [u8]) { + self.v.to_bytes(&mut buf[0..4]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + v: ::from_bytes(&buf[0..4]), + } + } +} +>>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub trait Base { - fn apply(&self, x: i32) -> i32; + fn apply(&mut self, x: i32) -> i32; } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Record, VaArg, FnPtrArg, Default)] pub struct Derived { - pub factor: Value, + #[offset(8)] + pub factor: i32, } impl Derived {} impl Clone for Derived { fn clone(&self) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - factor: Rc::new(RefCell::new((*self.factor.borrow()))), + factor: { self.factor }, })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } } +<<<<<<< HEAD impl ByteRepr for Derived {} +======= +impl ByteRepr for Derived { + fn byte_size() -> usize { + 16 + } + fn to_bytes(&self, buf: &mut [u8]) { + self.factor.to_bytes(&mut buf[8..12]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + factor: ::from_bytes(&buf[8..12]), + } + } +} +>>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub fn main() { __cpp2rust_init_globals(); std::process::exit(main_0()); @@ -53,15 +87,13 @@ fn main_0() -> i32 { let base: Value> = Rc::new(RefCell::new( (derived.as_pointer()).to_dyn::(|w| w), )); - assert!((({ (*(*base.borrow()).upgrade().deref()).apply(5,) }) == 15)); + assert!((({ (*(*base.borrow()).upgrade().deref_mut()).apply(5,) }) == 15)); return 0; } impl S { pub fn new(x: i32) -> Self { let x: Value = Rc::new(RefCell::new(x)); - let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*x.borrow()))), - })); + let __this: Value = Rc::new(RefCell::new(Self { v: (*x.borrow()) })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } @@ -70,7 +102,7 @@ impl Derived { pub fn new(factor: i32) -> Self { let factor: Value = Rc::new(RefCell::new(factor)); let __this: Value = Rc::new(RefCell::new(Self { - factor: Rc::new(RefCell::new((*factor.borrow()))), + factor: (*factor.borrow()), })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() @@ -79,9 +111,9 @@ impl Derived { impl S {} impl Derived {} impl Base for Derived { - fn apply(&self, x: i32) -> i32 { + fn apply(&mut self, x: i32) -> i32 { let x: Value = Rc::new(RefCell::new(x)); - return ((*self.factor.borrow()) * (*x.borrow())); + return ({ self.factor } * (*x.borrow())); } } pub trait SImpl { @@ -98,17 +130,17 @@ pub trait SImpl { } impl SImpl for Ptr { fn get(&self) -> i32 { - return (*(*(*self).upgrade().deref()).v.borrow()); + return (*self).with(|__s: &S| __s.v); } fn destructor(&self) {} fn set(&self, x: i32) { let x: Value = Rc::new(RefCell::new(x)); - (*(*(*self).upgrade().deref()).v.borrow_mut()) = (*x.borrow()); + (*self).with_mut(|__s: &mut S| __s.v = (*x.borrow())); } fn add(&self, x: i32) -> i32 { let x: Value = Rc::new(RefCell::new(x)); - (*(*(*self).upgrade().deref()).v.borrow_mut()) += (*x.borrow()); - return (*(*(*self).upgrade().deref()).v.borrow()); + (*self).with_mut(|__s: &mut S| __s.v += (*x.borrow())); + return (*self).with(|__s: &S| __s.v); } } pub fn __cpp2rust_init_globals() {} diff --git a/tests/multi-file/template_method_late_instantiation/out/refcount/template_method_late_instantiation.rs b/tests/multi-file/template_method_late_instantiation/out/refcount/template_method_late_instantiation.rs index 74a31b546d..fbf4a5391f 100644 --- a/tests/multi-file/template_method_late_instantiation/out/refcount/template_method_late_instantiation.rs +++ b/tests/multi-file/template_method_late_instantiation/out/refcount/template_method_late_instantiation.rs @@ -6,24 +6,14 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg)] +#[derive(Clone, Record, VaArg, FnPtrArg)] pub struct S_int_ { - pub x: Value, -} -impl Clone for S_int_ { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - x: Rc::new(RefCell::new((*self.x.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub x: i32, } impl Default for S_int_ { fn default() -> Self { - S_int_ { - x: Rc::new(RefCell::new(0)), - } + S_int_ { x: 0 } } } impl ByteRepr for S_int_ { @@ -31,11 +21,11 @@ impl ByteRepr for S_int_ { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.x.borrow()).to_bytes(&mut buf[0..4]); + self.x.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - x: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + x: ::from_bytes(&buf[0..4]), } } } @@ -62,10 +52,10 @@ pub trait S_int_Impl { impl S_int_Impl for Ptr { fn set(&self, v: i32) { let v: Value = Rc::new(RefCell::new(v)); - (*(*(*self).upgrade().deref()).x.borrow_mut()) = (*v.borrow()); + (*self).with_mut(|__s: &mut S_int_| __s.x = (*v.borrow())); } fn get(&self) -> i32 { - return (*(*(*self).upgrade().deref()).x.borrow()); + return (*self).with(|__s: &S_int_| __s.x); } } pub fn __cpp2rust_init_globals() {} diff --git a/tests/ub/out/refcount/ctor_ref_member.rs b/tests/ub/out/refcount/ctor_ref_member.rs index 62ffdb424f..9a83e9d133 100644 --- a/tests/ub/out/refcount/ctor_ref_member.rs +++ b/tests/ub/out/refcount/ctor_ref_member.rs @@ -6,8 +6,9 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Clone, VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct S { + #[offset(0)] pub r: Ptr, } impl S { @@ -17,7 +18,11 @@ impl S { Rc::try_unwrap(__this).ok().unwrap().into_inner() } } -impl ByteRepr for S {} +impl ByteRepr for S { + fn byte_size() -> usize { + 8 + } +} pub fn main() { __cpp2rust_init_globals(); std::process::exit(main_0()); diff --git a/tests/ub/out/refcount/ub6.rs b/tests/ub/out/refcount/ub6.rs index 7936c85f9b..5a7a8c623a 100644 --- a/tests/ub/out/refcount/ub6.rs +++ b/tests/ub/out/refcount/ub6.rs @@ -6,12 +6,18 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Clone, VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Pair { + #[offset(0)] pub x1: Ptr, + #[offset(8)] pub x2: Ptr, } -impl ByteRepr for Pair {} +impl ByteRepr for Pair { + fn byte_size() -> usize { + 16 + } +} pub fn mkPair_0(x1: Ptr, x2: Ptr) -> Pair { return Pair { x1: (x1).clone(), diff --git a/tests/unit/field_pointers.cpp b/tests/unit/field_pointers.cpp new file mode 100644 index 0000000000..09ed311db2 --- /dev/null +++ b/tests/unit/field_pointers.cpp @@ -0,0 +1,91 @@ +#include +#include +#include + +struct Inner { + int a; + char name[8]; +}; + +struct Header { + int tag; + short size; +}; + +struct Outer { + int x; + Inner inner; + Inner items[3]; + std::vector v; + int *cursor; + int buf[4]; + + int next() { return buf[x++]; } + int sum() const { return x + inner.a; } + void push(int k) { v.push_back(k + x); } +}; + +static void set(int *p, int value) { *p = value; } + +static int bump(Outer *o) { + o->x++; + return 1; +} + +int main() { + Outer o{}; + o.x = 1; + + // Pointers to fields, nested fields, and fields of array elements. + int *px = &o.x; + *px = 2; + assert(o.x == 2); + int *pa = &o.inner.a; + set(pa, 3); + assert(o.inner.a == 3); + int *pi = &o.items[1].a; + *pi = 4; + assert(o.items[1].a == 4); + assert(&o.items[0].a != pi); + + // Arrays in fields decay to pointers to their elements. + char *name = o.inner.name; + for (int i = 0; i < 3; ++i) { + name[i] = 'a' + i; + } + assert(strlen(o.inner.name) == 3); + assert(name + 3 == &o.inner.name[3]); + + // A pointer into the struct, stored in the struct itself. + o.cursor = &o.buf[1]; + *o.cursor = 5; + *o.cursor++ += 1; + assert(o.buf[1] == 6 && o.cursor == &o.buf[2]); + + // Reads and writes of fields of the same struct in one statement. + o.x = 0; + int first = o.next(); + assert(first == 0 && o.x == 1); + o.x = o.inner.a + bump(&o); + assert(o.x == 4); + o.push(o.inner.a); + assert(o.v.size() == 1 && o.v[0] == 7); + assert(o.sum() == 7); + + // Byte-level access to a field. + int y = 0; + memcpy(&o.items[2].a, &o.items[1].a, sizeof(int)); + memcpy(&y, &o.items[2].a, sizeof(int)); + assert(y == 4); + + // Writes through a reinterpreted struct pointer. + unsigned char bytes[sizeof(Header)] = {}; + Header *view = reinterpret_cast
(bytes); + view->tag = 0x01020304; + assert(bytes[0] == 0x04 || bytes[3] == 0x04); + set(&view->tag, 0); + assert(bytes[0] == 0 && bytes[3] == 0); + view->size = 1; + assert(bytes[4] + bytes[5] == 1); + return 0; +} diff --git a/tests/unit/out/refcount/07_unique.rs b/tests/unit/out/refcount/07_unique.rs index e270ab598e..14cec80ad9 100644 --- a/tests/unit/out/refcount/07_unique.rs +++ b/tests/unit/out/refcount/07_unique.rs @@ -8,7 +8,7 @@ use std::os::fd::AsFd; use std::rc::{Rc, Weak}; pub fn fn_0(u: Option>) -> Option> { let u: Value>> = Rc::new(RefCell::new(u)); - (*(*u.borrow_mut()).as_ref().unwrap().borrow_mut()) = 10; + (*(*u.borrow()).as_ref().unwrap().borrow_mut()) = 10; return (*u.borrow_mut()).take(); } pub fn main() { @@ -17,7 +17,7 @@ pub fn main() { } fn main_0() -> i32 { let f: Value>> = Rc::new(RefCell::new(Some(Rc::new(RefCell::new(8))))); - (*(*f.borrow_mut()).as_ref().unwrap().borrow_mut()) = 9; + (*(*f.borrow()).as_ref().unwrap().borrow_mut()) = 9; let f_ptr1: Value> = Rc::new(RefCell::new((*f.borrow()).as_pointer())); (*f_ptr1.borrow()).write(10); let f_ptr2: Value> = Rc::new(RefCell::new(((*f.borrow()).as_pointer()))); diff --git a/tests/unit/out/refcount/10_struct.rs b/tests/unit/out/refcount/10_struct.rs index 225e8257b9..73e1080b28 100644 --- a/tests/unit/out/refcount/10_struct.rs +++ b/tests/unit/out/refcount/10_struct.rs @@ -6,84 +6,60 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct GraphNode { - pub dst: Value, - pub next: Value>, -} -impl Clone for GraphNode { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - dst: Rc::new(RefCell::new((*self.dst.borrow()))), - next: Rc::new(RefCell::new((*self.next.borrow()).clone())), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub dst: u32, + #[offset(8)] + pub next: Ptr, } impl ByteRepr for GraphNode { fn byte_size() -> usize { 16 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.dst.borrow()).to_bytes(&mut buf[0..4]); - (*self.next.borrow()).to_bytes(&mut buf[8..16]); + self.dst.to_bytes(&mut buf[0..4]); + self.next.to_bytes(&mut buf[8..16]); } fn from_bytes(buf: &[u8]) -> Self { Self { - dst: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - next: Rc::new(RefCell::new(>::from_bytes(&buf[8..16]))), + dst: ::from_bytes(&buf[0..4]), + next: >::from_bytes(&buf[8..16]), } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Graph { - pub V: Value, - pub adj: Value>>, -} -impl Clone for Graph { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - V: Rc::new(RefCell::new((*self.V.borrow()))), - adj: Rc::new(RefCell::new((*self.adj.borrow()).clone())), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub V: u32, + #[offset(8)] + pub adj: Ptr>, } impl ByteRepr for Graph { fn byte_size() -> usize { 16 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.V.borrow()).to_bytes(&mut buf[0..4]); - (*self.adj.borrow()).to_bytes(&mut buf[8..16]); + self.V.to_bytes(&mut buf[0..4]); + self.adj.to_bytes(&mut buf[8..16]); } fn from_bytes(buf: &[u8]) -> Self { Self { - V: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - adj: Rc::new(RefCell::new(>>::from_bytes(&buf[8..16]))), + V: ::from_bytes(&buf[0..4]), + adj: >>::from_bytes(&buf[8..16]), } } } -#[derive(VaArg, FnPtrArg)] +#[derive(Clone, Record, VaArg, FnPtrArg)] pub struct Partial { - pub p: Value>, + #[offset(0)] + pub p: Ptr, } impl Partial { pub fn new_1(q: Ptr) -> Self { let q: Value> = Rc::new(RefCell::new(q)); let __this: Value = Rc::new(RefCell::new(Self { - p: Rc::new(RefCell::new((*q.borrow()).clone())), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } -} -impl Clone for Partial { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - p: Rc::new(RefCell::new((*self.p.borrow()).clone())), + p: (*q.borrow()).clone(), })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() @@ -92,7 +68,7 @@ impl Clone for Partial { impl Default for Partial { fn default() -> Self { Partial { - p: Rc::new(RefCell::new(Ptr::::null())), + p: Ptr::::null(), } } } @@ -101,44 +77,36 @@ impl ByteRepr for Partial { 8 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.p.borrow()).to_bytes(&mut buf[0..8]); + self.p.to_bytes(&mut buf[0..8]); } fn from_bytes(buf: &[u8]) -> Self { Self { - p: Rc::new(RefCell::new(>::from_bytes(&buf[0..8]))), + p: >::from_bytes(&buf[0..8]), } } } -#[derive(Clone, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] pub struct Declared {} impl Declared {} -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct S { - pub i: Value, - pub d: Value>, -} -impl Clone for S { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - i: Rc::new(RefCell::new((*self.i.borrow()))), - d: Rc::new(RefCell::new((*self.d.borrow()).clone())), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub i: i32, + #[offset(8)] + pub d: Ptr, } impl ByteRepr for S { fn byte_size() -> usize { 16 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.i.borrow()).to_bytes(&mut buf[0..4]); - (*self.d.borrow()).to_bytes(&mut buf[8..16]); + self.i.to_bytes(&mut buf[0..4]); + self.d.to_bytes(&mut buf[8..16]); } fn from_bytes(buf: &[u8]) -> Self { Self { - i: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - d: Rc::new(RefCell::new(>::from_bytes(&buf[8..16]))), + i: ::from_bytes(&buf[0..4]), + d: >::from_bytes(&buf[8..16]), } } } @@ -148,28 +116,28 @@ pub fn main() { } fn main_0() -> i32 { let g: Value = Rc::new(RefCell::new(Graph { - V: Rc::new(RefCell::new(5_u32)), - adj: Rc::new(RefCell::new(Ptr::>::null())), + V: 5_u32, + adj: Ptr::>::null(), })); let arr: Value> = Rc::new(RefCell::new(Box::new([3, 1, 4]))); let it: Value = Rc::new(RefCell::new(Partial::new_1({ (arr.as_pointer() as Ptr) }))); if { - let _lhs = (*(*it.borrow()).p.borrow()).clone(); + let _lhs = { (*it.borrow()).p.clone() }; _lhs != (arr.as_pointer() as Ptr) } { return 1; } let def: Value = Rc::new(RefCell::new(::default())); - if !((*(*def.borrow()).p.borrow()).is_null()) { + if !(({ (*def.borrow()).p.clone() }).is_null()) { return 1; } let s: Value = Rc::new(RefCell::new(S { - i: Rc::new(RefCell::new(7)), - d: Rc::new(RefCell::new(Ptr::::null())), + i: 7, + d: Ptr::::null(), })); - if ((*(*s.borrow()).i.borrow()) != 7) || (!((*(*s.borrow()).d.borrow()).is_null())) { + if ({ (*s.borrow()).i } != 7) || (!(({ (*s.borrow()).d.clone() }).is_null())) { return 1; } return 0; @@ -182,29 +150,37 @@ impl GraphImpl for Ptr { let src: Value = Rc::new(RefCell::new(src)); let dst: Value = Rc::new(RefCell::new(dst)); let __rhs = Ptr::alloc(GraphNode { - dst: Rc::new(RefCell::new((*dst.borrow()))), - next: Rc::new(RefCell::new( - ((*(*(*self).upgrade().deref()).adj.borrow()) + dst: (*dst.borrow()), + next: { + ((*self) + .with(|__s: &Graph| (__s.adj).clone()) .offset((*src.borrow()) as isize) .read()) - .clone(), - )), + .clone() + }, }); - (*(*(*self).upgrade().deref()).adj.borrow()) - .offset((*src.borrow()) as isize) - .write(__rhs); + { + (*self) + .with(|__s: &Graph| (__s.adj).clone()) + .offset((*src.borrow()) as isize) + } + .write(__rhs); let __rhs = Ptr::alloc(GraphNode { - dst: Rc::new(RefCell::new((*src.borrow()))), - next: Rc::new(RefCell::new( - ((*(*(*self).upgrade().deref()).adj.borrow()) + dst: (*src.borrow()), + next: { + ((*self) + .with(|__s: &Graph| (__s.adj).clone()) .offset((*dst.borrow()) as isize) .read()) - .clone(), - )), + .clone() + }, }); - (*(*(*self).upgrade().deref()).adj.borrow()) - .offset((*dst.borrow()) as isize) - .write(__rhs); + { + (*self) + .with(|__s: &Graph| (__s.adj).clone()) + .offset((*dst.borrow()) as isize) + } + .write(__rhs); } } pub trait PartialImpl { diff --git a/tests/unit/out/refcount/addr_of_global.rs b/tests/unit/out/refcount/addr_of_global.rs index dea1b71133..2c2412f028 100644 --- a/tests/unit/out/refcount/addr_of_global.rs +++ b/tests/unit/out/refcount/addr_of_global.rs @@ -6,72 +6,50 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Inner { - pub value: Value, -} -impl Clone for Inner { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - value: Rc::new(RefCell::new((*self.value.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub value: i32, } impl ByteRepr for Inner { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.value.borrow()).to_bytes(&mut buf[0..4]); + self.value.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - value: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + value: ::from_bytes(&buf[0..4]), } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Outer { - pub p: Value>, -} -impl Clone for Outer { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - p: Rc::new(RefCell::new((*self.p.borrow()).clone())), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub p: Ptr, } impl ByteRepr for Outer { fn byte_size() -> usize { 8 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.p.borrow()).to_bytes(&mut buf[0..8]); + self.p.to_bytes(&mut buf[0..8]); } fn from_bytes(buf: &[u8]) -> Self { Self { - p: Rc::new(RefCell::new(>::from_bytes(&buf[0..8]))), + p: >::from_bytes(&buf[0..8]), } } } thread_local!( - pub static alpha_0: Value = Rc::new(RefCell::new(Inner { - value: Rc::new(RefCell::new(1)), - })); + pub static alpha_0: Value = Rc::new(RefCell::new(Inner { value: 1 })); ); thread_local!( - pub static beta_1: Value = Rc::new(RefCell::new(Inner { - value: Rc::new(RefCell::new(2)), - })); + pub static beta_1: Value = Rc::new(RefCell::new(Inner { value: 2 })); ); thread_local!( - pub static shared_2: Value = Rc::new(RefCell::new(Inner { - value: Rc::new(RefCell::new(42)), - })); + pub static shared_2: Value = Rc::new(RefCell::new(Inner { value: 42 })); ); thread_local!( pub static items_3: Value]>> = Rc::new(RefCell::new(Box::new([ @@ -81,7 +59,7 @@ thread_local!( ); thread_local!( pub static obj_4: Value = Rc::new(RefCell::new(Outer { - p: Rc::new(RefCell::new((shared_2.with(|v| v.as_pointer())))), + p: (shared_2.with(|v| v.as_pointer())), })); ); pub fn main() { @@ -90,34 +68,23 @@ pub fn main() { } fn main_0() -> i32 { assert!( - ((*(*({ + (({ let __idx = (0) as usize; items_3.with(|rc| rc.borrow()[__idx].clone()) }) - .upgrade() - .deref()) - .value - .borrow()) + .with(|__s: &Inner| __s.value) == 1) ); assert!( - ((*(*({ + (({ let __idx = (1) as usize; items_3.with(|rc| rc.borrow()[__idx].clone()) }) - .upgrade() - .deref()) - .value - .borrow()) + .with(|__s: &Inner| __s.value) == 2) ); assert!( - ((*(*(*(*obj_4.with(Value::clone).borrow()).p.borrow()) - .upgrade() - .deref()) - .value - .borrow()) - == 42) + ({ (*obj_4.with(Value::clone).borrow()).p.clone() }.with(|__s: &Inner| __s.value) == 42) ); thread_local!( static cache_5: Value]>> = Rc::new(RefCell::new(Box::new([ @@ -126,25 +93,19 @@ fn main_0() -> i32 { ]))); ); assert!( - ((*(*({ + (({ let __idx = (0) as usize; cache_5.with(|rc| rc.borrow()[__idx].clone()) }) - .upgrade() - .deref()) - .value - .borrow()) + .with(|__s: &Inner| __s.value) == 1) ); assert!( - ((*(*({ + (({ let __idx = (1) as usize; cache_5.with(|rc| rc.borrow()[__idx].clone()) }) - .upgrade() - .deref()) - .value - .borrow()) + .with(|__s: &Inner| __s.value) == 2) ); return 0; diff --git a/tests/unit/out/refcount/anonymous-struct.rs b/tests/unit/out/refcount/anonymous-struct.rs index 7514ae12b2..23fd7a9f13 100644 --- a/tests/unit/out/refcount/anonymous-struct.rs +++ b/tests/unit/out/refcount/anonymous-struct.rs @@ -6,251 +6,187 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Outer_Named { - pub a: Value, - pub b: Value, -} -impl Clone for Outer_Named { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - a: Rc::new(RefCell::new((*self.a.borrow()))), - b: Rc::new(RefCell::new((*self.b.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub a: i32, + #[offset(4)] + pub b: i32, } impl ByteRepr for Outer_Named { fn byte_size() -> usize { 8 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.a.borrow()).to_bytes(&mut buf[0..4]); - (*self.b.borrow()).to_bytes(&mut buf[4..8]); + self.a.to_bytes(&mut buf[0..4]); + self.b.to_bytes(&mut buf[4..8]); } fn from_bytes(buf: &[u8]) -> Self { Self { - a: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - b: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + a: ::from_bytes(&buf[0..4]), + b: ::from_bytes(&buf[4..8]), } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct anon_0 { - pub c: Value, - pub d: Value, -} -impl Clone for anon_0 { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - c: Rc::new(RefCell::new((*self.c.borrow()))), - d: Rc::new(RefCell::new((*self.d.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub c: i32, + #[offset(4)] + pub d: i32, } impl ByteRepr for anon_0 { fn byte_size() -> usize { 8 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.c.borrow()).to_bytes(&mut buf[0..4]); - (*self.d.borrow()).to_bytes(&mut buf[4..8]); + self.c.to_bytes(&mut buf[0..4]); + self.d.to_bytes(&mut buf[4..8]); } fn from_bytes(buf: &[u8]) -> Self { Self { - c: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - d: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + c: ::from_bytes(&buf[0..4]), + d: ::from_bytes(&buf[4..8]), } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct anon_1 { - pub g: Value, - pub h: Value, -} -impl Clone for anon_1 { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - g: Rc::new(RefCell::new((*self.g.borrow()))), - h: Rc::new(RefCell::new((*self.h.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub g: i32, + #[offset(4)] + pub h: i32, } impl ByteRepr for anon_1 { fn byte_size() -> usize { 8 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.g.borrow()).to_bytes(&mut buf[0..4]); - (*self.h.borrow()).to_bytes(&mut buf[4..8]); + self.g.to_bytes(&mut buf[0..4]); + self.h.to_bytes(&mut buf[4..8]); } fn from_bytes(buf: &[u8]) -> Self { Self { - g: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - h: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + g: ::from_bytes(&buf[0..4]), + h: ::from_bytes(&buf[4..8]), } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct anon_2 { - pub e: Value, - pub f: Value, -} -impl Clone for anon_2 { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - e: Rc::new(RefCell::new((*self.e.borrow()))), - f: Rc::new(RefCell::new((*self.f.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub e: i32, + #[offset(4)] + pub f: i32, } impl ByteRepr for anon_2 { fn byte_size() -> usize { 8 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.e.borrow()).to_bytes(&mut buf[0..4]); - (*self.f.borrow()).to_bytes(&mut buf[4..8]); + self.e.to_bytes(&mut buf[0..4]); + self.f.to_bytes(&mut buf[4..8]); } fn from_bytes(buf: &[u8]) -> Self { Self { - e: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - f: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + e: ::from_bytes(&buf[0..4]), + f: ::from_bytes(&buf[4..8]), } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct anon_4 { - pub j: Value, -} -impl Clone for anon_4 { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - j: Rc::new(RefCell::new((*self.j.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub j: i32, } impl ByteRepr for anon_4 { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.j.borrow()).to_bytes(&mut buf[0..4]); + self.j.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - j: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + j: ::from_bytes(&buf[0..4]), } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct anon_5 { - pub k: Value, -} -impl Clone for anon_5 { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - k: Rc::new(RefCell::new((*self.k.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub k: i32, } impl ByteRepr for anon_5 { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.k.borrow()).to_bytes(&mut buf[0..4]); + self.k.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - k: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + k: ::from_bytes(&buf[0..4]), } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct anon_3 { - pub i: Value, - pub inner_named: Value, - pub anon_5: Value, -} -impl Clone for anon_3 { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - i: Rc::new(RefCell::new((*self.i.borrow()))), - inner_named: Rc::new(RefCell::new((*self.inner_named.borrow()).clone())), - anon_5: Rc::new(RefCell::new((*self.anon_5.borrow()).clone())), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub i: i32, + #[offset(4)] + pub inner_named: anon_4, + #[offset(8)] + pub anon_5: anon_5, } impl ByteRepr for anon_3 { fn byte_size() -> usize { 12 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.i.borrow()).to_bytes(&mut buf[0..4]); - (*self.inner_named.borrow()).to_bytes(&mut buf[4..8]); - (*self.anon_5.borrow()).to_bytes(&mut buf[8..12]); + self.i.to_bytes(&mut buf[0..4]); + self.inner_named.to_bytes(&mut buf[4..8]); + self.anon_5.to_bytes(&mut buf[8..12]); } fn from_bytes(buf: &[u8]) -> Self { Self { - i: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - inner_named: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), - anon_5: Rc::new(RefCell::new(::from_bytes(&buf[8..12]))), + i: ::from_bytes(&buf[0..4]), + inner_named: ::from_bytes(&buf[4..8]), + anon_5: ::from_bytes(&buf[8..12]), } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Outer { - pub named: Value, - pub anonymous_named_0: Value, - pub anonymous_named_1: Value, - pub anon_2: Value, - pub anon_3: Value, -} -impl Clone for Outer { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - named: Rc::new(RefCell::new((*self.named.borrow()).clone())), - anonymous_named_0: Rc::new(RefCell::new((*self.anonymous_named_0.borrow()).clone())), - anonymous_named_1: Rc::new(RefCell::new((*self.anonymous_named_1.borrow()).clone())), - anon_2: Rc::new(RefCell::new((*self.anon_2.borrow()).clone())), - anon_3: Rc::new(RefCell::new((*self.anon_3.borrow()).clone())), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub named: Outer_Named, + #[offset(8)] + pub anonymous_named_0: anon_0, + #[offset(16)] + pub anonymous_named_1: anon_1, + #[offset(24)] + pub anon_2: anon_2, + #[offset(32)] + pub anon_3: anon_3, } impl ByteRepr for Outer { fn byte_size() -> usize { 44 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.named.borrow()).to_bytes(&mut buf[0..8]); - (*self.anonymous_named_0.borrow()).to_bytes(&mut buf[8..16]); - (*self.anonymous_named_1.borrow()).to_bytes(&mut buf[16..24]); - (*self.anon_2.borrow()).to_bytes(&mut buf[24..32]); - (*self.anon_3.borrow()).to_bytes(&mut buf[32..44]); + self.named.to_bytes(&mut buf[0..8]); + self.anonymous_named_0.to_bytes(&mut buf[8..16]); + self.anonymous_named_1.to_bytes(&mut buf[16..24]); + self.anon_2.to_bytes(&mut buf[24..32]); + self.anon_3.to_bytes(&mut buf[32..44]); } fn from_bytes(buf: &[u8]) -> Self { Self { - named: Rc::new(RefCell::new(::from_bytes(&buf[0..8]))), - anonymous_named_0: Rc::new(RefCell::new(::from_bytes(&buf[8..16]))), - anonymous_named_1: Rc::new(RefCell::new(::from_bytes(&buf[16..24]))), - anon_2: Rc::new(RefCell::new(::from_bytes(&buf[24..32]))), - anon_3: Rc::new(RefCell::new(::from_bytes(&buf[32..44]))), + named: ::from_bytes(&buf[0..8]), + anonymous_named_0: ::from_bytes(&buf[8..16]), + anonymous_named_1: ::from_bytes(&buf[16..24]), + anon_2: ::from_bytes(&buf[24..32]), + anon_3: ::from_bytes(&buf[32..44]), } } } @@ -260,86 +196,64 @@ pub fn main() { } fn main_0() -> i32 { let o: Value = Rc::new(RefCell::new(::default())); - (*(*(*o.borrow()).named.borrow()).a.borrow_mut()) = 1; - (*(*(*o.borrow()).named.borrow()).b.borrow_mut()) = 2; - (*(*(*o.borrow()).anonymous_named_0.borrow()).c.borrow_mut()) = 3; - (*(*(*o.borrow()).anonymous_named_0.borrow()).d.borrow_mut()) = 4; - (*(*(*o.borrow()).anonymous_named_1.borrow()).g.borrow_mut()) = 5; - (*(*(*o.borrow()).anonymous_named_1.borrow()).h.borrow_mut()) = 6; - (*(*(*o.borrow()).anon_2.borrow()).e.borrow_mut()) = 7; - (*(*(*o.borrow()).anon_2.borrow()).f.borrow_mut()) = 8; - (*(*(*o.borrow()).anon_3.borrow()).i.borrow_mut()) = 9; - (*(*(*(*o.borrow()).anon_3.borrow()).inner_named.borrow()) - .j - .borrow_mut()) = 10; - (*(*(*(*o.borrow()).anon_3.borrow()).anon_5.borrow()) - .k - .borrow_mut()) = 11; - assert!(((*(*(*o.borrow()).named.borrow()).a.borrow()) == 1)); - assert!(((*(*(*o.borrow()).named.borrow()).b.borrow()) == 2)); - assert!(((*(*(*o.borrow()).anonymous_named_0.borrow()).c.borrow()) == 3)); - assert!(((*(*(*o.borrow()).anonymous_named_0.borrow()).d.borrow()) == 4)); - assert!(((*(*(*o.borrow()).anonymous_named_1.borrow()).g.borrow()) == 5)); - assert!(((*(*(*o.borrow()).anonymous_named_1.borrow()).h.borrow()) == 6)); - assert!(((*(*(*o.borrow()).anon_2.borrow()).e.borrow()) == 7)); - assert!(((*(*(*o.borrow()).anon_2.borrow()).f.borrow()) == 8)); - assert!(((*(*(*o.borrow()).anon_3.borrow()).i.borrow()) == 9)); - assert!( - ((*(*(*(*o.borrow()).anon_3.borrow()).inner_named.borrow()) - .j - .borrow()) - == 10) - ); - assert!( - ((*(*(*(*o.borrow()).anon_3.borrow()).anon_5.borrow()) - .k - .borrow()) - == 11) - ); + (*o.borrow_mut()).named.a = 1; + (*o.borrow_mut()).named.b = 2; + (*o.borrow_mut()).anonymous_named_0.c = 3; + (*o.borrow_mut()).anonymous_named_0.d = 4; + (*o.borrow_mut()).anonymous_named_1.g = 5; + (*o.borrow_mut()).anonymous_named_1.h = 6; + (*o.borrow_mut()).anon_2.e = 7; + (*o.borrow_mut()).anon_2.f = 8; + (*o.borrow_mut()).anon_3.i = 9; + (*o.borrow_mut()).anon_3.inner_named.j = 10; + (*o.borrow_mut()).anon_3.anon_5.k = 11; + assert!(({ (*o.borrow()).named.a } == 1)); + assert!(({ (*o.borrow()).named.b } == 2)); + assert!(({ (*o.borrow()).anonymous_named_0.c } == 3)); + assert!(({ (*o.borrow()).anonymous_named_0.d } == 4)); + assert!(({ (*o.borrow()).anonymous_named_1.g } == 5)); + assert!(({ (*o.borrow()).anonymous_named_1.h } == 6)); + assert!(({ (*o.borrow()).anon_2.e } == 7)); + assert!(({ (*o.borrow()).anon_2.f } == 8)); + assert!(({ (*o.borrow()).anon_3.i } == 9)); + assert!(({ (*o.borrow()).anon_3.inner_named.j } == 10)); + assert!(({ (*o.borrow()).anon_3.anon_5.k } == 11)); let s: Value = Rc::new(RefCell::new(::default())); - (*(*s.borrow()).x.borrow_mut()) = 1; - (*(*s.borrow()).z.borrow_mut()) = 2; + (*s.borrow_mut()).x = 1; + (*s.borrow_mut()).z = 2; assert!( ({ - (*(*s.borrow()).x.borrow_mut()) = 1; - (*(*s.borrow()).x.borrow()) + (*s.borrow_mut()).x = 1; + { (*s.borrow()).x } } != 0) ); assert!( ({ - (*(*s.borrow()).z.borrow_mut()) = 2; - (*(*s.borrow()).z.borrow()) + (*s.borrow_mut()).z = 2; + { (*s.borrow()).z } } != 0) ); return 0; } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct anon_6 { - pub x: Value, - pub z: Value, -} -impl Clone for anon_6 { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - x: Rc::new(RefCell::new((*self.x.borrow()))), - z: Rc::new(RefCell::new((*self.z.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub x: i32, + #[offset(4)] + pub z: i32, } impl ByteRepr for anon_6 { fn byte_size() -> usize { 8 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.x.borrow()).to_bytes(&mut buf[0..4]); - (*self.z.borrow()).to_bytes(&mut buf[4..8]); + self.x.to_bytes(&mut buf[0..4]); + self.z.to_bytes(&mut buf[4..8]); } fn from_bytes(buf: &[u8]) -> Self { Self { - x: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - z: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + x: ::from_bytes(&buf[0..4]), + z: ::from_bytes(&buf[4..8]), } } } diff --git a/tests/unit/out/refcount/anonymous-struct_c.rs b/tests/unit/out/refcount/anonymous-struct_c.rs index ca5f7c50af..dbd8780c1d 100644 --- a/tests/unit/out/refcount/anonymous-struct_c.rs +++ b/tests/unit/out/refcount/anonymous-struct_c.rs @@ -6,235 +6,187 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Named { - pub a: Value, - pub b: Value, -} -impl Clone for Named { - fn clone(&self) -> Self { - Self { - a: Rc::new(RefCell::new((*self.a.borrow()).clone())), - b: Rc::new(RefCell::new((*self.b.borrow()).clone())), - } - } + #[offset(0)] + pub a: i32, + #[offset(4)] + pub b: i32, } impl ByteRepr for Named { fn byte_size() -> usize { 8 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.a.borrow()).to_bytes(&mut buf[0..4]); - (*self.b.borrow()).to_bytes(&mut buf[4..8]); + self.a.to_bytes(&mut buf[0..4]); + self.b.to_bytes(&mut buf[4..8]); } fn from_bytes(buf: &[u8]) -> Self { Self { - a: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - b: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + a: ::from_bytes(&buf[0..4]), + b: ::from_bytes(&buf[4..8]), } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct anon_0 { - pub c: Value, - pub d: Value, -} -impl Clone for anon_0 { - fn clone(&self) -> Self { - Self { - c: Rc::new(RefCell::new((*self.c.borrow()).clone())), - d: Rc::new(RefCell::new((*self.d.borrow()).clone())), - } - } + #[offset(0)] + pub c: i32, + #[offset(4)] + pub d: i32, } impl ByteRepr for anon_0 { fn byte_size() -> usize { 8 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.c.borrow()).to_bytes(&mut buf[0..4]); - (*self.d.borrow()).to_bytes(&mut buf[4..8]); + self.c.to_bytes(&mut buf[0..4]); + self.d.to_bytes(&mut buf[4..8]); } fn from_bytes(buf: &[u8]) -> Self { Self { - c: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - d: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + c: ::from_bytes(&buf[0..4]), + d: ::from_bytes(&buf[4..8]), } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct anon_1 { - pub g: Value, - pub h: Value, -} -impl Clone for anon_1 { - fn clone(&self) -> Self { - Self { - g: Rc::new(RefCell::new((*self.g.borrow()).clone())), - h: Rc::new(RefCell::new((*self.h.borrow()).clone())), - } - } + #[offset(0)] + pub g: i32, + #[offset(4)] + pub h: i32, } impl ByteRepr for anon_1 { fn byte_size() -> usize { 8 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.g.borrow()).to_bytes(&mut buf[0..4]); - (*self.h.borrow()).to_bytes(&mut buf[4..8]); + self.g.to_bytes(&mut buf[0..4]); + self.h.to_bytes(&mut buf[4..8]); } fn from_bytes(buf: &[u8]) -> Self { Self { - g: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - h: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + g: ::from_bytes(&buf[0..4]), + h: ::from_bytes(&buf[4..8]), } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct anon_2 { - pub e: Value, - pub f: Value, -} -impl Clone for anon_2 { - fn clone(&self) -> Self { - Self { - e: Rc::new(RefCell::new((*self.e.borrow()).clone())), - f: Rc::new(RefCell::new((*self.f.borrow()).clone())), - } - } + #[offset(0)] + pub e: i32, + #[offset(4)] + pub f: i32, } impl ByteRepr for anon_2 { fn byte_size() -> usize { 8 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.e.borrow()).to_bytes(&mut buf[0..4]); - (*self.f.borrow()).to_bytes(&mut buf[4..8]); + self.e.to_bytes(&mut buf[0..4]); + self.f.to_bytes(&mut buf[4..8]); } fn from_bytes(buf: &[u8]) -> Self { Self { - e: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - f: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + e: ::from_bytes(&buf[0..4]), + f: ::from_bytes(&buf[4..8]), } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct anon_4 { - pub j: Value, -} -impl Clone for anon_4 { - fn clone(&self) -> Self { - Self { - j: Rc::new(RefCell::new((*self.j.borrow()).clone())), - } - } + #[offset(0)] + pub j: i32, } impl ByteRepr for anon_4 { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.j.borrow()).to_bytes(&mut buf[0..4]); + self.j.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - j: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + j: ::from_bytes(&buf[0..4]), } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct anon_5 { - pub k: Value, -} -impl Clone for anon_5 { - fn clone(&self) -> Self { - Self { - k: Rc::new(RefCell::new((*self.k.borrow()).clone())), - } - } + #[offset(0)] + pub k: i32, } impl ByteRepr for anon_5 { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.k.borrow()).to_bytes(&mut buf[0..4]); + self.k.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - k: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + k: ::from_bytes(&buf[0..4]), } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct anon_3 { - pub i: Value, - pub inner_named: Value, - pub anon_5: Value, -} -impl Clone for anon_3 { - fn clone(&self) -> Self { - Self { - i: Rc::new(RefCell::new((*self.i.borrow()).clone())), - inner_named: Rc::new(RefCell::new((*self.inner_named.borrow()).clone())), - anon_5: Rc::new(RefCell::new((*self.anon_5.borrow()).clone())), - } - } + #[offset(0)] + pub i: i32, + #[offset(4)] + pub inner_named: anon_4, + #[offset(8)] + pub anon_5: anon_5, } impl ByteRepr for anon_3 { fn byte_size() -> usize { 12 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.i.borrow()).to_bytes(&mut buf[0..4]); - (*self.inner_named.borrow()).to_bytes(&mut buf[4..8]); - (*self.anon_5.borrow()).to_bytes(&mut buf[8..12]); + self.i.to_bytes(&mut buf[0..4]); + self.inner_named.to_bytes(&mut buf[4..8]); + self.anon_5.to_bytes(&mut buf[8..12]); } fn from_bytes(buf: &[u8]) -> Self { Self { - i: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - inner_named: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), - anon_5: Rc::new(RefCell::new(::from_bytes(&buf[8..12]))), + i: ::from_bytes(&buf[0..4]), + inner_named: ::from_bytes(&buf[4..8]), + anon_5: ::from_bytes(&buf[8..12]), } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Outer { - pub named: Value, - pub anon0: Value, - pub anon1: Value, - pub anon_2: Value, - pub anon_3: Value, -} -impl Clone for Outer { - fn clone(&self) -> Self { - Self { - named: Rc::new(RefCell::new((*self.named.borrow()).clone())), - anon0: Rc::new(RefCell::new((*self.anon0.borrow()).clone())), - anon1: Rc::new(RefCell::new((*self.anon1.borrow()).clone())), - anon_2: Rc::new(RefCell::new((*self.anon_2.borrow()).clone())), - anon_3: Rc::new(RefCell::new((*self.anon_3.borrow()).clone())), - } - } + #[offset(0)] + pub named: Named, + #[offset(8)] + pub anon0: anon_0, + #[offset(16)] + pub anon1: anon_1, + #[offset(24)] + pub anon_2: anon_2, + #[offset(32)] + pub anon_3: anon_3, } impl ByteRepr for Outer { fn byte_size() -> usize { 44 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.named.borrow()).to_bytes(&mut buf[0..8]); - (*self.anon0.borrow()).to_bytes(&mut buf[8..16]); - (*self.anon1.borrow()).to_bytes(&mut buf[16..24]); - (*self.anon_2.borrow()).to_bytes(&mut buf[24..32]); - (*self.anon_3.borrow()).to_bytes(&mut buf[32..44]); + self.named.to_bytes(&mut buf[0..8]); + self.anon0.to_bytes(&mut buf[8..16]); + self.anon1.to_bytes(&mut buf[16..24]); + self.anon_2.to_bytes(&mut buf[24..32]); + self.anon_3.to_bytes(&mut buf[32..44]); } fn from_bytes(buf: &[u8]) -> Self { Self { - named: Rc::new(RefCell::new(::from_bytes(&buf[0..8]))), - anon0: Rc::new(RefCell::new(::from_bytes(&buf[8..16]))), - anon1: Rc::new(RefCell::new(::from_bytes(&buf[16..24]))), - anon_2: Rc::new(RefCell::new(::from_bytes(&buf[24..32]))), - anon_3: Rc::new(RefCell::new(::from_bytes(&buf[32..44]))), + named: ::from_bytes(&buf[0..8]), + anon0: ::from_bytes(&buf[8..16]), + anon1: ::from_bytes(&buf[16..24]), + anon_2: ::from_bytes(&buf[24..32]), + anon_3: ::from_bytes(&buf[32..44]), } } } @@ -244,95 +196,70 @@ pub fn main() { } fn main_0() -> i32 { let o: Value = Rc::new(RefCell::new(Outer { - named: Rc::new(RefCell::new(Named { - a: Rc::new(RefCell::new(0)), - b: Rc::new(RefCell::new(0_i32)), - })), - anon0: Rc::new(RefCell::new(::default())), - anon1: Rc::new(RefCell::new(::default())), - anon_2: Rc::new(RefCell::new(::default())), - anon_3: Rc::new(RefCell::new(::default())), + named: Named { a: 0, b: 0_i32 }, + anon0: ::default(), + anon1: ::default(), + anon_2: ::default(), + anon_3: ::default(), })); - (*(*(*o.borrow()).named.borrow()).a.borrow_mut()) = 1; - (*(*(*o.borrow()).named.borrow()).b.borrow_mut()) = 2; - (*(*(*o.borrow()).anon0.borrow()).c.borrow_mut()) = 3; - (*(*(*o.borrow()).anon0.borrow()).d.borrow_mut()) = 4; - (*(*(*o.borrow()).anon1.borrow()).g.borrow_mut()) = 5; - (*(*(*o.borrow()).anon1.borrow()).h.borrow_mut()) = 6; - (*(*(*o.borrow()).anon_2.borrow()).e.borrow_mut()) = 7; - (*(*(*o.borrow()).anon_2.borrow()).f.borrow_mut()) = 8; - (*(*(*o.borrow()).anon_3.borrow()).i.borrow_mut()) = 9; - (*(*(*(*o.borrow()).anon_3.borrow()).inner_named.borrow()) - .j - .borrow_mut()) = 10; - (*(*(*(*o.borrow()).anon_3.borrow()).anon_5.borrow()) - .k - .borrow_mut()) = 11; - assert!(((((*(*(*o.borrow()).named.borrow()).a.borrow()) == 1) as i32) != 0)); - assert!(((((*(*(*o.borrow()).named.borrow()).b.borrow()) == 2) as i32) != 0)); - assert!(((((*(*(*o.borrow()).anon0.borrow()).c.borrow()) == 3) as i32) != 0)); - assert!(((((*(*(*o.borrow()).anon0.borrow()).d.borrow()) == 4) as i32) != 0)); - assert!(((((*(*(*o.borrow()).anon1.borrow()).g.borrow()) == 5) as i32) != 0)); - assert!(((((*(*(*o.borrow()).anon1.borrow()).h.borrow()) == 6) as i32) != 0)); - assert!(((((*(*(*o.borrow()).anon_2.borrow()).e.borrow()) == 7) as i32) != 0)); - assert!(((((*(*(*o.borrow()).anon_2.borrow()).f.borrow()) == 8) as i32) != 0)); - assert!(((((*(*(*o.borrow()).anon_3.borrow()).i.borrow()) == 9) as i32) != 0)); - assert!( - ((((*(*(*(*o.borrow()).anon_3.borrow()).inner_named.borrow()) - .j - .borrow()) - == 10) as i32) - != 0) - ); - assert!( - ((((*(*(*(*o.borrow()).anon_3.borrow()).anon_5.borrow()) - .k - .borrow()) - == 11) as i32) - != 0) - ); + (*o.borrow_mut()).named.a = 1; + (*o.borrow_mut()).named.b = 2; + (*o.borrow_mut()).anon0.c = 3; + (*o.borrow_mut()).anon0.d = 4; + (*o.borrow_mut()).anon1.g = 5; + (*o.borrow_mut()).anon1.h = 6; + (*o.borrow_mut()).anon_2.e = 7; + (*o.borrow_mut()).anon_2.f = 8; + (*o.borrow_mut()).anon_3.i = 9; + (*o.borrow_mut()).anon_3.inner_named.j = 10; + (*o.borrow_mut()).anon_3.anon_5.k = 11; + assert!(((({ (*o.borrow()).named.a } == 1) as i32) != 0)); + assert!(((({ (*o.borrow()).named.b } == 2) as i32) != 0)); + assert!(((({ (*o.borrow()).anon0.c } == 3) as i32) != 0)); + assert!(((({ (*o.borrow()).anon0.d } == 4) as i32) != 0)); + assert!(((({ (*o.borrow()).anon1.g } == 5) as i32) != 0)); + assert!(((({ (*o.borrow()).anon1.h } == 6) as i32) != 0)); + assert!(((({ (*o.borrow()).anon_2.e } == 7) as i32) != 0)); + assert!(((({ (*o.borrow()).anon_2.f } == 8) as i32) != 0)); + assert!(((({ (*o.borrow()).anon_3.i } == 9) as i32) != 0)); + assert!(((({ (*o.borrow()).anon_3.inner_named.j } == 10) as i32) != 0)); + assert!(((({ (*o.borrow()).anon_3.anon_5.k } == 11) as i32) != 0)); let s: Value = >::default(); - (*(*s.borrow()).x.borrow_mut()) = 1; - (*(*s.borrow()).z.borrow_mut()) = 2; + (*s.borrow_mut()).x = 1; + (*s.borrow_mut()).z = 2; assert!( ({ - (*(*s.borrow()).x.borrow_mut()) = 1; - (*(*s.borrow()).x.borrow()) + (*s.borrow_mut()).x = 1; + { (*s.borrow()).x } } != 0) ); assert!( ({ - (*(*s.borrow()).z.borrow_mut()) = 2; - (*(*s.borrow()).z.borrow()) + (*s.borrow_mut()).z = 2; + { (*s.borrow()).z } } != 0) ); return 0; } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct anon_6 { - pub x: Value, - pub z: Value, -} -impl Clone for anon_6 { - fn clone(&self) -> Self { - Self { - x: Rc::new(RefCell::new((*self.x.borrow()).clone())), - z: Rc::new(RefCell::new((*self.z.borrow()).clone())), - } - } + #[offset(0)] + pub x: i32, + #[offset(4)] + pub z: i32, } impl ByteRepr for anon_6 { fn byte_size() -> usize { 8 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.x.borrow()).to_bytes(&mut buf[0..4]); - (*self.z.borrow()).to_bytes(&mut buf[4..8]); + self.x.to_bytes(&mut buf[0..4]); + self.z.to_bytes(&mut buf[4..8]); } fn from_bytes(buf: &[u8]) -> Self { Self { - x: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - z: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + x: ::from_bytes(&buf[0..4]), + z: ::from_bytes(&buf[4..8]), } } } diff --git a/tests/unit/out/refcount/anonymous_enum.rs b/tests/unit/out/refcount/anonymous_enum.rs index ef1577e39d..7d38217d02 100644 --- a/tests/unit/out/refcount/anonymous_enum.rs +++ b/tests/unit/out/refcount/anonymous_enum.rs @@ -12,29 +12,21 @@ pub const anon_0_FIRST_B: anon_0 = 1; pub type anon_1 = u32; pub const anon_1_SECOND_A: anon_1 = 0; pub const anon_1_SECOND_B: anon_1 = 1; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct S { - pub a: Value, -} -impl Clone for S { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - a: Rc::new(RefCell::new((*self.a.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub a: i32, } impl ByteRepr for S { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.a.borrow()).to_bytes(&mut buf[0..4]); + self.a.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - a: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + a: ::from_bytes(&buf[0..4]), } } } @@ -44,33 +36,25 @@ pub const TdEnum_TD_B: TdEnum = 1; pub type anon_2 = u32; pub const anon_2_FIELD_A: anon_2 = 0; pub const anon_2_FIELD_B: anon_2 = 1; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct WithAnonField { - pub a: Value, - pub field: Value, -} -impl Clone for WithAnonField { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - a: Rc::new(RefCell::new((*self.a.borrow()))), - field: Rc::new(RefCell::new((*self.field.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub a: i32, + #[offset(4)] + pub field: anon_2, } impl ByteRepr for WithAnonField { fn byte_size() -> usize { 8 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.a.borrow()).to_bytes(&mut buf[0..4]); - (*self.field.borrow()).to_bytes(&mut buf[4..8]); + self.a.to_bytes(&mut buf[0..4]); + self.field.to_bytes(&mut buf[4..8]); } fn from_bytes(buf: &[u8]) -> Self { Self { - a: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - field: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + a: ::from_bytes(&buf[0..4]), + field: ::from_bytes(&buf[4..8]), } } } @@ -87,10 +71,10 @@ fn main_0() -> i32 { (*td.borrow_mut()) = TdEnum_TD_B; assert!((((*td.borrow()) as i32) == (TdEnum_TD_B as i32))); let w: Value = Rc::new(RefCell::new(::default())); - (*(*w.borrow()).field.borrow_mut()) = anon_2_FIELD_A; - assert!((((*(*w.borrow()).field.borrow()) as i32) == (anon_2_FIELD_A as i32))); - (*(*w.borrow()).field.borrow_mut()) = anon_2_FIELD_B; - assert!((((*(*w.borrow()).field.borrow()) as i32) == (anon_2_FIELD_B as i32))); + (*w.borrow_mut()).field = anon_2_FIELD_A; + assert!((({ (*w.borrow()).field } as i32) == (anon_2_FIELD_A as i32))); + (*w.borrow_mut()).field = anon_2_FIELD_B; + assert!((({ (*w.borrow()).field } as i32) == (anon_2_FIELD_B as i32))); return 0; } pub type anon_3 = u32; diff --git a/tests/unit/out/refcount/anonymous_enum_c.rs b/tests/unit/out/refcount/anonymous_enum_c.rs index c0585ac7ba..77960d9000 100644 --- a/tests/unit/out/refcount/anonymous_enum_c.rs +++ b/tests/unit/out/refcount/anonymous_enum_c.rs @@ -12,27 +12,21 @@ pub const anon_0_FIRST_B: anon_0 = 1; pub type anon_1 = u32; pub const anon_1_SECOND_A: anon_1 = 0; pub const anon_1_SECOND_B: anon_1 = 1; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct S { - pub a: Value, -} -impl Clone for S { - fn clone(&self) -> Self { - Self { - a: Rc::new(RefCell::new((*self.a.borrow()).clone())), - } - } + #[offset(0)] + pub a: i32, } impl ByteRepr for S { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.a.borrow()).to_bytes(&mut buf[0..4]); + self.a.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - a: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + a: ::from_bytes(&buf[0..4]), } } } @@ -42,31 +36,25 @@ pub const TdEnum_enum_TD_B: TdEnum_enum = 1; pub type anon_2 = u32; pub const anon_2_FIELD_A: anon_2 = 0; pub const anon_2_FIELD_B: anon_2 = 1; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct WithAnonField { - pub a: Value, - pub field: Value, -} -impl Clone for WithAnonField { - fn clone(&self) -> Self { - Self { - a: Rc::new(RefCell::new((*self.a.borrow()).clone())), - field: Rc::new(RefCell::new((*self.field.borrow()).clone())), - } - } + #[offset(0)] + pub a: i32, + #[offset(4)] + pub field: anon_2, } impl ByteRepr for WithAnonField { fn byte_size() -> usize { 8 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.a.borrow()).to_bytes(&mut buf[0..4]); - (*self.field.borrow()).to_bytes(&mut buf[4..8]); + self.a.to_bytes(&mut buf[0..4]); + self.field.to_bytes(&mut buf[4..8]); } fn from_bytes(buf: &[u8]) -> Self { Self { - a: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - field: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + a: ::from_bytes(&buf[0..4]), + field: ::from_bytes(&buf[4..8]), } } } @@ -83,16 +71,10 @@ fn main_0() -> i32 { (*td.borrow_mut()) = TdEnum_enum_TD_B; assert!((((((*td.borrow()) as u32) == ((TdEnum_enum_TD_B as i32) as u32)) as i32) != 0)); let w: Value = >::default(); - (*(*w.borrow()).field.borrow_mut()) = anon_2_FIELD_A; - assert!( - (((((*(*w.borrow()).field.borrow()) as u32) == ((anon_2_FIELD_A as i32) as u32)) as i32) - != 0) - ); - (*(*w.borrow()).field.borrow_mut()) = anon_2_FIELD_B; - assert!( - (((((*(*w.borrow()).field.borrow()) as u32) == ((anon_2_FIELD_B as i32) as u32)) as i32) - != 0) - ); + (*w.borrow_mut()).field = anon_2_FIELD_A; + assert!((((({ (*w.borrow()).field } as u32) == ((anon_2_FIELD_A as i32) as u32)) as i32) != 0)); + (*w.borrow_mut()).field = anon_2_FIELD_B; + assert!((((({ (*w.borrow()).field } as u32) == ((anon_2_FIELD_B as i32) as u32)) as i32) != 0)); return 0; } pub type anon_3 = u32; diff --git a/tests/unit/out/refcount/array_const_init.rs b/tests/unit/out/refcount/array_const_init.rs index d933ef8d34..26131e5d41 100644 --- a/tests/unit/out/refcount/array_const_init.rs +++ b/tests/unit/out/refcount/array_const_init.rs @@ -6,27 +6,21 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg)] +#[derive(Clone, Record, VaArg, FnPtrArg)] pub struct S { - pub head: Value, - pub tail: Value>, - pub buf: Value>, -} -impl Clone for S { - fn clone(&self) -> Self { - Self { - head: Rc::new(RefCell::new((*self.head.borrow()).clone())), - tail: Rc::new(RefCell::new((*self.tail.borrow()).clone())), - buf: Rc::new(RefCell::new((*self.buf.borrow()).clone())), - } - } + #[offset(0)] + pub head: i32, + #[offset(4)] + pub tail: Box<[i32]>, + #[offset(16)] + pub buf: Box<[u8]>, } impl Default for S { fn default() -> Self { S { - head: Rc::new(RefCell::new(0_i32)), - tail: Rc::new(RefCell::new((0..3).map(|_| 0_i32).collect::>())), - buf: Rc::new(RefCell::new((0..4).map(|_| 0_u8).collect::>())), + head: 0_i32, + tail: (0..3).map(|_| 0_i32).collect::>(), + buf: (0..4).map(|_| 0_u8).collect::>(), } } } @@ -35,23 +29,23 @@ impl ByteRepr for S { 20 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.head.borrow()).to_bytes(&mut buf[0..4]); - (*self.tail.borrow()).to_bytes(&mut buf[4..16]); - (*self.buf.borrow()).to_bytes(&mut buf[16..20]); + self.head.to_bytes(&mut buf[0..4]); + self.tail.to_bytes(&mut buf[4..16]); + self.buf.to_bytes(&mut buf[16..20]); } fn from_bytes(buf: &[u8]) -> Self { Self { - head: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - tail: Rc::new(RefCell::new(>::from_bytes(&buf[4..16]))), - buf: Rc::new(RefCell::new(>::from_bytes(&buf[16..20]))), + head: ::from_bytes(&buf[0..4]), + tail: >::from_bytes(&buf[4..16]), + buf: >::from_bytes(&buf[16..20]), } } } thread_local!( pub static s_0: Value = Rc::new(RefCell::new(S { - head: Rc::new(RefCell::new(5)), - tail: Rc::new(RefCell::new(Box::new([0; 3]))), - buf: Rc::new(RefCell::new(Box::new([0; 4]))), + head: 5, + tail: Box::new([0; 3]), + buf: Box::new([0; 4]), })); ); pub fn main() { @@ -59,21 +53,19 @@ pub fn main() { std::process::exit(main_0()); } fn main_0() -> i32 { - assert!(((((*(*s_0.with(Value::clone).borrow()).head.borrow()) == 5) as i32) != 0)); + assert!(((({ (*s_0.with(Value::clone).borrow()).head } == 5) as i32) != 0)); let i: Value = Rc::new(RefCell::new(0)); 'loop_: while ((((*i.borrow()) < 3) as i32) != 0) { assert!( - ((((*(*s_0.with(Value::clone).borrow()).tail.borrow())[(*i.borrow()) as usize] == 0) - as i32) - != 0) + ((((*s_0.with(Value::clone).borrow()).tail[(*i.borrow()) as usize] == 0) as i32) != 0) ); (*i.borrow_mut()).postfix_inc(); } let i: Value = Rc::new(RefCell::new(0)); 'loop_: while ((((*i.borrow()) < 4) as i32) != 0) { assert!( - (((((*(*s_0.with(Value::clone).borrow()).buf.borrow())[(*i.borrow()) as usize] as i32) - == 0) as i32) + (((((*s_0.with(Value::clone).borrow()).buf[(*i.borrow()) as usize] as i32) == 0) + as i32) != 0) ); (*i.borrow_mut()).postfix_inc(); diff --git a/tests/unit/out/refcount/array_of_noncopy_struct.rs b/tests/unit/out/refcount/array_of_noncopy_struct.rs index e7c63faadb..2f833534ab 100644 --- a/tests/unit/out/refcount/array_of_noncopy_struct.rs +++ b/tests/unit/out/refcount/array_of_noncopy_struct.rs @@ -6,16 +6,18 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg)] +#[derive(Record, VaArg, FnPtrArg)] pub struct NonCopy { + #[offset(0)] pub data: Value>, - pub tag: Value, + #[offset(24)] + pub tag: i32, } impl Clone for NonCopy { fn clone(&self) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - data: Rc::new(RefCell::new((*self.data.borrow()).clone())), - tag: Rc::new(RefCell::new((*self.tag.borrow()))), + data: { Rc::new(RefCell::new((*self.data.borrow()).clone())) }, + tag: { self.tag }, })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() @@ -25,11 +27,29 @@ impl Default for NonCopy { fn default() -> Self { NonCopy { data: Rc::new(RefCell::new(Default::default())), - tag: Rc::new(RefCell::new(0)), + tag: 0, } } } +<<<<<<< HEAD impl ByteRepr for NonCopy {} +======= +impl ByteRepr for NonCopy { + fn byte_size() -> usize { + 32 + } + fn to_bytes(&self, buf: &mut [u8]) { + self.data.to_bytes(&mut buf[0..24]); + self.tag.to_bytes(&mut buf[24..28]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + data: >>::from_bytes(&buf[0..24]), + tag: ::from_bytes(&buf[24..28]), + } + } +} +>>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub fn main() { __cpp2rust_init_globals(); std::process::exit(main_0()); @@ -40,17 +60,12 @@ fn main_0() -> i32 { .map(|_| ::default()) .collect::>(), )); - (*(*arr.borrow())[(0) as usize].tag.borrow_mut()) = 7; + (*arr.borrow_mut())[(0) as usize].tag = 7; (*(*arr.borrow())[(1) as usize].data.borrow_mut()).push(42); - assert!(((*(*arr.borrow())[(0) as usize].tag.borrow()) == 7)); + assert!(({ (*arr.borrow())[(0) as usize].tag } == 7)); assert!(((*(*arr.borrow())[(1) as usize].data.borrow()).len() == 1_usize)); - assert!( - ((((*arr.borrow())[(1) as usize].data.as_pointer() as Ptr) - .offset(0_usize) - .read()) - == 42) - ); - assert!(((*(*arr.borrow())[(2) as usize].tag.borrow()) == 0)); + assert!(({ (*(*arr.borrow())[(1) as usize].data.borrow())[(0_usize) as usize] } == 42)); + assert!(({ (*arr.borrow())[(2) as usize].tag } == 0)); assert!(((*(*arr.borrow())[(2) as usize].data.borrow()).len() == 0_usize)); return 0; } diff --git a/tests/unit/out/refcount/array_reference.rs b/tests/unit/out/refcount/array_reference.rs index 26b9148ffd..b14ce1b961 100644 --- a/tests/unit/out/refcount/array_reference.rs +++ b/tests/unit/out/refcount/array_reference.rs @@ -70,33 +70,25 @@ pub fn fill_and_sum_9(a: Ptr, v: i32, out: Ptr) { pub fn pick_10(s: Ptr) -> Ptr { return ((s).clone() as Ptr); } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Point { - pub x: Value, - pub y: Value, -} -impl Clone for Point { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - x: Rc::new(RefCell::new((*self.x.borrow()))), - y: Rc::new(RefCell::new((*self.y.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub x: i32, + #[offset(4)] + pub y: i32, } impl ByteRepr for Point { fn byte_size() -> usize { 8 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.x.borrow()).to_bytes(&mut buf[0..4]); - (*self.y.borrow()).to_bytes(&mut buf[4..8]); + self.x.to_bytes(&mut buf[0..4]); + self.y.to_bytes(&mut buf[4..8]); } fn from_bytes(buf: &[u8]) -> Self { Self { - x: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - y: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + x: ::from_bytes(&buf[0..4]), + y: ::from_bytes(&buf[4..8]), } } } @@ -104,18 +96,20 @@ pub fn sum_points_11(p: Ptr) -> i32 { return { let _lhs = { let _lhs = { - let _lhs = (*(*(p).offset((0) as isize).upgrade().deref()).x.borrow()); - _lhs + (*(*(p).offset((0) as isize).upgrade().deref()).y.borrow()) + let _lhs = (p).offset((0) as isize).with(|__s: &Point| __s.x); + _lhs + (p).offset((0) as isize).with(|__s: &Point| __s.y) }; - _lhs + (*(*(p).offset((1) as isize).upgrade().deref()).x.borrow()) + _lhs + (p).offset((1) as isize).with(|__s: &Point| __s.x) }; - _lhs + (*(*(p).offset((1) as isize).upgrade().deref()).y.borrow()) + _lhs + (p).offset((1) as isize).with(|__s: &Point| __s.y) }; } pub fn shift_points_12(p: Ptr, d: i32) { let d: Value = Rc::new(RefCell::new(d)); - (*(*(p).offset((0) as isize).upgrade().deref()).x.borrow_mut()) += (*d.borrow()); - (*(*(p).offset((1) as isize).upgrade().deref()).y.borrow_mut()) += (*d.borrow()); + (p).offset((0) as isize) + .with_mut(|__s: &mut Point| __s.x += (*d.borrow())); + (p).offset((1) as isize) + .with_mut(|__s: &mut Point| __s.y += (*d.borrow())); } pub fn total_len_13(names: Ptr>) -> i32 { return (({ len5_1(((names).offset((0) as isize).read()).clone()) }) @@ -148,19 +142,13 @@ fn main_0() -> i32 { ); assert!((({ len_0((({ pick_10((buf.as_pointer() as Ptr),) }) as Ptr),) }) == 4)); let pts: Value> = Rc::new(RefCell::new(Box::new([ - Point { - x: Rc::new(RefCell::new(1)), - y: Rc::new(RefCell::new(2)), - }, - Point { - x: Rc::new(RefCell::new(3)), - y: Rc::new(RefCell::new(4)), - }, + Point { x: 1, y: 2 }, + Point { x: 3, y: 4 }, ]))); assert!((({ sum_points_11((pts.as_pointer() as Ptr),) }) == 10)); ({ shift_points_12((pts.as_pointer() as Ptr), 10) }); - assert!(((*(*pts.borrow())[(0) as usize].x.borrow()) == 11)); - assert!(((*(*pts.borrow())[(1) as usize].y.borrow()) == 14)); + assert!(({ (*pts.borrow())[(0) as usize].x } == 11)); + assert!(({ (*pts.borrow())[(1) as usize].y } == 14)); assert!((({ sum_points_11((pts.as_pointer() as Ptr),) }) == 30)); assert!( (({ sum_decayed_6((arr.as_pointer() as Ptr),) }) diff --git a/tests/unit/out/refcount/as_const.rs b/tests/unit/out/refcount/as_const.rs index 1f4b928069..ee10107516 100644 --- a/tests/unit/out/refcount/as_const.rs +++ b/tests/unit/out/refcount/as_const.rs @@ -9,29 +9,21 @@ use std::rc::{Rc, Weak}; pub type Overload = u32; pub const Overload_kMutableOverload: Overload = 1; pub const Overload_kConstOverload: Overload = 2; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct S { - pub v: Value, -} -impl Clone for S { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*self.v.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub v: i32, } impl ByteRepr for S { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.v.borrow()).to_bytes(&mut buf[0..4]); + self.v.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - v: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + v: ::from_bytes(&buf[0..4]), } } } @@ -46,22 +38,20 @@ pub fn main() { std::process::exit(main_0()); } fn main_0() -> i32 { - let s: Value = Rc::new(RefCell::new(S { - v: Rc::new(RefCell::new(7)), - })); + let s: Value = Rc::new(RefCell::new(S { v: 7 })); assert!(((({ SImpl::f_1(&s.as_pointer(),) }) as i32) == (Overload_kMutableOverload as i32))); assert!(((({ SImpl::f_2(&s.as_pointer(),) }) as i32) == (Overload_kConstOverload as i32))); assert!(((({ g_0(s.as_pointer(),) }) as i32) == (Overload_kMutableOverload as i32))); assert!(((({ g_1(s.as_pointer(),) }) as i32) == (Overload_kConstOverload as i32))); ({ SImpl::value_ref_3(&s.as_pointer()) }).write(9); - assert!(((*(*s.borrow()).v.borrow()) == 9)); + assert!(({ (*s.borrow()).v } == 9)); assert!(((({ SImpl::value_ref_4(&s.as_pointer(),) }).read()) == 9)); let cs: Ptr = s.as_pointer(); assert!(((({ SImpl::f_2(&cs,) }) as i32) == (Overload_kConstOverload as i32))); - assert!(((*(*cs.upgrade().deref()).v.borrow()) == 9)); + assert!((cs.with(|__s: &S| __s.v) == 9)); let p: Value> = Rc::new(RefCell::new((s.as_pointer()))); - (*(*(*p.borrow()).upgrade().deref()).v.borrow_mut()) = 11; - assert!(((*(*s.borrow()).v.borrow()) == 11)); + (*p.borrow()).with_mut(|__s: &mut S| __s.v = 11); + assert!(({ (*s.borrow()).v } == 11)); assert!(((({ SImpl::f_1(&(*p.borrow()),) }) as i32) == (Overload_kMutableOverload as i32))); return 0; } @@ -79,10 +69,10 @@ impl SImpl for Ptr { return Overload_kConstOverload; } fn value_ref_3(&self) -> Ptr { - return (*(*self).upgrade().deref()).v.as_pointer(); + return field_ptr!((*self), v); } fn value_ref_4(&self) -> Ptr { - return (*(*self).upgrade().deref()).v.as_pointer(); + return field_ptr!((*self), v); } } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/bounded_struct_ptr.rs b/tests/unit/out/refcount/bounded_struct_ptr.rs index eb7dceda31..96850a0fe2 100644 --- a/tests/unit/out/refcount/bounded_struct_ptr.rs +++ b/tests/unit/out/refcount/bounded_struct_ptr.rs @@ -6,33 +6,25 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Foo { - pub x1: Value, - pub x2: Value, -} -impl Clone for Foo { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - x1: Rc::new(RefCell::new((*self.x1.borrow()))), - x2: Rc::new(RefCell::new((*self.x2.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub x1: i32, + #[offset(4)] + pub x2: i32, } impl ByteRepr for Foo { fn byte_size() -> usize { 8 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.x1.borrow()).to_bytes(&mut buf[0..4]); - (*self.x2.borrow()).to_bytes(&mut buf[4..8]); + self.x1.to_bytes(&mut buf[0..4]); + self.x2.to_bytes(&mut buf[4..8]); } fn from_bytes(buf: &[u8]) -> Self { Self { - x1: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - x2: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + x1: ::from_bytes(&buf[0..4]), + x2: ::from_bytes(&buf[4..8]), } } } @@ -42,24 +34,18 @@ pub fn main() { } fn main_0() -> i32 { let arr: Value> = Rc::new(RefCell::new(Box::new([ - Foo { - x1: Rc::new(RefCell::new(1)), - x2: Rc::new(RefCell::new(2)), - }, - Foo { - x1: Rc::new(RefCell::new(3)), - x2: Rc::new(RefCell::new(4)), - }, + Foo { x1: 1, x2: 2 }, + Foo { x1: 3, x2: 4 }, ]))); let p1: Value> = Rc::new(RefCell::new( - ((*arr.borrow())[(1) as usize].x1.as_pointer()), + (field_ptr!((arr.as_pointer() as Ptr).offset(1), x1)), )); let a: Value = Rc::new(RefCell::new(((*p1.borrow()).read()))); let p2: Value> = Rc::new(RefCell::new(((arr.as_pointer() as Ptr).offset(0)))); assert!( ({ let _lhs = (*a.borrow()); - _lhs + (*(*(*p2.borrow()).upgrade().deref()).x2.borrow()) + _lhs + (*p2.borrow()).with(|__s: &Foo| __s.x2) } == 5) ); return 0; diff --git a/tests/unit/out/refcount/bst.rs b/tests/unit/out/refcount/bst.rs index f12b99ae59..9f95ddfe8a 100644 --- a/tests/unit/out/refcount/bst.rs +++ b/tests/unit/out/refcount/bst.rs @@ -6,37 +6,29 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct node_t { - pub left: Value>, - pub right: Value>, - pub value: Value, -} -impl Clone for node_t { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - left: Rc::new(RefCell::new((*self.left.borrow()).clone())), - right: Rc::new(RefCell::new((*self.right.borrow()).clone())), - value: Rc::new(RefCell::new((*self.value.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub left: Ptr, + #[offset(8)] + pub right: Ptr, + #[offset(16)] + pub value: i32, } impl ByteRepr for node_t { fn byte_size() -> usize { 24 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.left.borrow()).to_bytes(&mut buf[0..8]); - (*self.right.borrow()).to_bytes(&mut buf[8..16]); - (*self.value.borrow()).to_bytes(&mut buf[16..20]); + self.left.to_bytes(&mut buf[0..8]); + self.right.to_bytes(&mut buf[8..16]); + self.value.to_bytes(&mut buf[16..20]); } fn from_bytes(buf: &[u8]) -> Self { Self { - left: Rc::new(RefCell::new(>::from_bytes(&buf[0..8]))), - right: Rc::new(RefCell::new(>::from_bytes(&buf[8..16]))), - value: Rc::new(RefCell::new(::from_bytes(&buf[16..20]))), + left: >::from_bytes(&buf[0..8]), + right: >::from_bytes(&buf[8..16]), + value: ::from_bytes(&buf[16..20]), } } } @@ -45,29 +37,29 @@ pub fn find_0(node: Ptr, value: i32) -> Ptr { let value: Value = Rc::new(RefCell::new(value)); if ({ let _lhs = (*value.borrow()); - _lhs < (*(*(*node.borrow()).upgrade().deref()).value.borrow()) - }) && (!((*(*(*node.borrow()).upgrade().deref()).left.borrow()).is_null())) + _lhs < (*node.borrow()).with(|__s: &node_t| __s.value) + }) && (!(((*node.borrow()).with(|__s: &node_t| (__s.left).clone())).is_null())) { return ({ find_0( - (*(*(*node.borrow()).upgrade().deref()).left.borrow()).clone(), + (*node.borrow()).with(|__s: &node_t| (__s.left).clone()), (*value.borrow()), ) }); } else if ({ let _lhs = (*value.borrow()); - _lhs > (*(*(*node.borrow()).upgrade().deref()).value.borrow()) - }) && (!((*(*(*node.borrow()).upgrade().deref()).right.borrow()).is_null())) + _lhs > (*node.borrow()).with(|__s: &node_t| __s.value) + }) && (!(((*node.borrow()).with(|__s: &node_t| (__s.right).clone())).is_null())) { return ({ find_0( - (*(*(*node.borrow()).upgrade().deref()).right.borrow()).clone(), + (*node.borrow()).with(|__s: &node_t| (__s.right).clone()), (*value.borrow()), ) }); } else if { let _lhs = (*value.borrow()); - _lhs == (*(*(*node.borrow()).upgrade().deref()).value.borrow()) + _lhs == (*node.borrow()).with(|__s: &node_t| __s.value) } { return (*node.borrow()).clone(); } @@ -80,27 +72,27 @@ pub fn insert_1(node: Ptr, new_node: Ptr) -> Ptr { return (*new_node.borrow()).clone(); } if { - let _lhs = (*(*(*new_node.borrow()).upgrade().deref()).value.borrow()); - _lhs < (*(*(*node.borrow()).upgrade().deref()).value.borrow()) + let _lhs = (*new_node.borrow()).with(|__s: &node_t| __s.value); + _lhs < (*node.borrow()).with(|__s: &node_t| __s.value) } { let __rhs = ({ insert_1( - (*(*(*node.borrow()).upgrade().deref()).left.borrow()).clone(), + (*node.borrow()).with(|__s: &node_t| (__s.left).clone()), (*new_node.borrow()).clone(), ) }); - (*(*(*node.borrow()).upgrade().deref()).left.borrow_mut()) = __rhs; + (*node.borrow()).with_mut(|__s: &mut node_t| __s.left = __rhs); } else if { - let _lhs = (*(*(*new_node.borrow()).upgrade().deref()).value.borrow()); - _lhs > (*(*(*node.borrow()).upgrade().deref()).value.borrow()) + let _lhs = (*new_node.borrow()).with(|__s: &node_t| __s.value); + _lhs > (*node.borrow()).with(|__s: &node_t| __s.value) } { let __rhs = ({ insert_1( - (*(*(*node.borrow()).upgrade().deref()).right.borrow()).clone(), + (*node.borrow()).with(|__s: &node_t| (__s.right).clone()), (*new_node.borrow()).clone(), ) }); - (*(*(*node.borrow()).upgrade().deref()).right.borrow_mut()) = __rhs; + (*node.borrow()).with_mut(|__s: &mut node_t| __s.right = __rhs); } return (*node.borrow()).clone(); } @@ -111,33 +103,33 @@ pub fn main() { fn main_0() -> i32 { let tree: Value>> = Rc::new(RefCell::new(Some(Rc::new(RefCell::new(node_t { - left: Rc::new(RefCell::new(Ptr::::null())), - right: Rc::new(RefCell::new(Ptr::::null())), - value: Rc::new(RefCell::new(0)), + left: Ptr::::null(), + right: Ptr::::null(), + value: 0, }))))); let n1: Value>> = Rc::new(RefCell::new(Some(Rc::new(RefCell::new(node_t { - left: Rc::new(RefCell::new(Ptr::::null())), - right: Rc::new(RefCell::new(Ptr::::null())), - value: Rc::new(RefCell::new(1)), + left: Ptr::::null(), + right: Ptr::::null(), + value: 1, }))))); let n2: Value>> = Rc::new(RefCell::new(Some(Rc::new(RefCell::new(node_t { - left: Rc::new(RefCell::new(Ptr::::null())), - right: Rc::new(RefCell::new(Ptr::::null())), - value: Rc::new(RefCell::new(2)), + left: Ptr::::null(), + right: Ptr::::null(), + value: 2, }))))); let n3: Value>> = Rc::new(RefCell::new(Some(Rc::new(RefCell::new(node_t { - left: Rc::new(RefCell::new(Ptr::::null())), - right: Rc::new(RefCell::new(Ptr::::null())), - value: Rc::new(RefCell::new(3)), + left: Ptr::::null(), + right: Ptr::::null(), + value: 3, }))))); let n4: Value>> = Rc::new(RefCell::new(Some(Rc::new(RefCell::new(node_t { - left: Rc::new(RefCell::new(Ptr::::null())), - right: Rc::new(RefCell::new(Ptr::::null())), - value: Rc::new(RefCell::new(4)), + left: Ptr::::null(), + right: Ptr::::null(), + value: 4, }))))); let ptr1: Value> = Rc::new(RefCell::new(((*tree.borrow()).as_pointer()))); let __rhs = ({ insert_1((*ptr1.borrow()).clone(), ((*n1.borrow()).as_pointer())) }); @@ -149,26 +141,11 @@ fn main_0() -> i32 { let __rhs = ({ insert_1((*ptr1.borrow()).clone(), ((*n4.borrow()).as_pointer())) }); (*ptr1.borrow_mut()) = __rhs; assert!( - ((((((*(*({ find_0((*ptr1.borrow()).clone(), 0,) }).upgrade().deref()) - .value - .borrow()) - == 0) - && ((*(*({ find_0((*ptr1.borrow()).clone(), 1,) }).upgrade().deref()) - .value - .borrow()) - == 1)) - && ((*(*({ find_0((*ptr1.borrow()).clone(), 2,) }).upgrade().deref()) - .value - .borrow()) - == 2)) - && ((*(*({ find_0((*ptr1.borrow()).clone(), 3,) }).upgrade().deref()) - .value - .borrow()) - == 3)) - && ((*(*({ find_0((*ptr1.borrow()).clone(), 4,) }).upgrade().deref()) - .value - .borrow()) - == 4)) + (((((({ find_0((*ptr1.borrow()).clone(), 0,) }).with(|__s: &node_t| __s.value) == 0) + && (({ find_0((*ptr1.borrow()).clone(), 1,) }).with(|__s: &node_t| __s.value) == 1)) + && (({ find_0((*ptr1.borrow()).clone(), 2,) }).with(|__s: &node_t| __s.value) == 2)) + && (({ find_0((*ptr1.borrow()).clone(), 3,) }).with(|__s: &node_t| __s.value) == 3)) + && (({ find_0((*ptr1.borrow()).clone(), 4,) }).with(|__s: &node_t| __s.value) == 4)) && (({ find_0((*ptr1.borrow()).clone(), 5,) }).is_null()) ); return 0; diff --git a/tests/unit/out/refcount/c_struct.rs b/tests/unit/out/refcount/c_struct.rs index 9899c832c5..be8b7f3cf0 100644 --- a/tests/unit/out/refcount/c_struct.rs +++ b/tests/unit/out/refcount/c_struct.rs @@ -6,87 +6,69 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Point { - pub x: Value, - pub y: Value, -} -impl Clone for Point { - fn clone(&self) -> Self { - Self { - x: Rc::new(RefCell::new((*self.x.borrow()).clone())), - y: Rc::new(RefCell::new((*self.y.borrow()).clone())), - } - } + #[offset(0)] + pub x: i32, + #[offset(4)] + pub y: i32, } impl ByteRepr for Point { fn byte_size() -> usize { 8 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.x.borrow()).to_bytes(&mut buf[0..4]); - (*self.y.borrow()).to_bytes(&mut buf[4..8]); + self.x.to_bytes(&mut buf[0..4]); + self.y.to_bytes(&mut buf[4..8]); } fn from_bytes(buf: &[u8]) -> Self { Self { - x: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - y: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + x: ::from_bytes(&buf[0..4]), + y: ::from_bytes(&buf[4..8]), } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Line { - pub start: Value, - pub end: Value, -} -impl Clone for Line { - fn clone(&self) -> Self { - Self { - start: Rc::new(RefCell::new((*self.start.borrow()).clone())), - end: Rc::new(RefCell::new((*self.end.borrow()).clone())), - } - } + #[offset(0)] + pub start: Point, + #[offset(8)] + pub end: Point, } impl ByteRepr for Line { fn byte_size() -> usize { 16 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.start.borrow()).to_bytes(&mut buf[0..8]); - (*self.end.borrow()).to_bytes(&mut buf[8..16]); + self.start.to_bytes(&mut buf[0..8]); + self.end.to_bytes(&mut buf[8..16]); } fn from_bytes(buf: &[u8]) -> Self { Self { - start: Rc::new(RefCell::new(::from_bytes(&buf[0..8]))), - end: Rc::new(RefCell::new(::from_bytes(&buf[8..16]))), + start: ::from_bytes(&buf[0..8]), + end: ::from_bytes(&buf[8..16]), } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Node { - pub value: Value, - pub next: Value>, -} -impl Clone for Node { - fn clone(&self) -> Self { - Self { - value: Rc::new(RefCell::new((*self.value.borrow()).clone())), - next: Rc::new(RefCell::new((*self.next.borrow()).clone())), - } - } + #[offset(0)] + pub value: i32, + #[offset(8)] + pub next: Ptr, } impl ByteRepr for Node { fn byte_size() -> usize { 16 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.value.borrow()).to_bytes(&mut buf[0..4]); - (*self.next.borrow()).to_bytes(&mut buf[8..16]); + self.value.to_bytes(&mut buf[0..4]); + self.next.to_bytes(&mut buf[8..16]); } fn from_bytes(buf: &[u8]) -> Self { Self { - value: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - next: Rc::new(RefCell::new(>::from_bytes(&buf[8..16]))), + value: ::from_bytes(&buf[0..4]), + next: >::from_bytes(&buf[8..16]), } } } @@ -94,63 +76,51 @@ pub type Color = u32; pub const Color_RED: Color = 0; pub const Color_GREEN: Color = 1; pub const Color_BLUE: Color = 2; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Inner { - pub a: Value, - pub b: Value, -} -impl Clone for Inner { - fn clone(&self) -> Self { - Self { - a: Rc::new(RefCell::new((*self.a.borrow()).clone())), - b: Rc::new(RefCell::new((*self.b.borrow()).clone())), - } - } + #[offset(0)] + pub a: i32, + #[offset(4)] + pub b: i32, } impl ByteRepr for Inner { fn byte_size() -> usize { 8 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.a.borrow()).to_bytes(&mut buf[0..4]); - (*self.b.borrow()).to_bytes(&mut buf[4..8]); + self.a.to_bytes(&mut buf[0..4]); + self.b.to_bytes(&mut buf[4..8]); } fn from_bytes(buf: &[u8]) -> Self { Self { - a: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - b: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + a: ::from_bytes(&buf[0..4]), + b: ::from_bytes(&buf[4..8]), } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Container { - pub inner: Value, - pub color: Value, - pub count: Value, -} -impl Clone for Container { - fn clone(&self) -> Self { - Self { - inner: Rc::new(RefCell::new((*self.inner.borrow()).clone())), - color: Rc::new(RefCell::new((*self.color.borrow()).clone())), - count: Rc::new(RefCell::new((*self.count.borrow()).clone())), - } - } + #[offset(0)] + pub inner: Inner, + #[offset(8)] + pub color: Color, + #[offset(12)] + pub count: i32, } impl ByteRepr for Container { fn byte_size() -> usize { 16 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.inner.borrow()).to_bytes(&mut buf[0..8]); - (*self.color.borrow()).to_bytes(&mut buf[8..12]); - (*self.count.borrow()).to_bytes(&mut buf[12..16]); + self.inner.to_bytes(&mut buf[0..8]); + self.color.to_bytes(&mut buf[8..12]); + self.count.to_bytes(&mut buf[12..16]); } fn from_bytes(buf: &[u8]) -> Self { Self { - inner: Rc::new(RefCell::new(::from_bytes(&buf[0..8]))), - color: Rc::new(RefCell::new(::from_bytes(&buf[8..12]))), - count: Rc::new(RefCell::new(::from_bytes(&buf[12..16]))), + inner: ::from_bytes(&buf[0..8]), + color: ::from_bytes(&buf[8..12]), + count: ::from_bytes(&buf[12..16]), } } } @@ -159,61 +129,41 @@ pub fn main() { std::process::exit(main_0()); } fn main_0() -> i32 { - let p: Value = Rc::new(RefCell::new(Point { - x: Rc::new(RefCell::new(10)), - y: Rc::new(RefCell::new(20)), - })); - assert!(((((*(*p.borrow()).x.borrow()) == 10) as i32) != 0)); - assert!(((((*(*p.borrow()).y.borrow()) == 20) as i32) != 0)); + let p: Value = Rc::new(RefCell::new(Point { x: 10, y: 20 })); + assert!(((({ (*p.borrow()).x } == 10) as i32) != 0)); + assert!(((({ (*p.borrow()).y } == 20) as i32) != 0)); let q: Value = Rc::new(RefCell::new((*p.borrow()).clone())); - (*(*q.borrow()).x.borrow_mut()) = 99; - assert!(((((*(*p.borrow()).x.borrow()) == 10) as i32) != 0)); - assert!(((((*(*q.borrow()).x.borrow()) == 99) as i32) != 0)); - assert!(((((*(*q.borrow()).y.borrow()) == 20) as i32) != 0)); + (*q.borrow_mut()).x = 99; + assert!(((({ (*p.borrow()).x } == 10) as i32) != 0)); + assert!(((({ (*q.borrow()).x } == 99) as i32) != 0)); + assert!(((({ (*q.borrow()).y } == 20) as i32) != 0)); let l: Value = Rc::new(RefCell::new(Line { - start: Rc::new(RefCell::new(Point { - x: Rc::new(RefCell::new(1)), - y: Rc::new(RefCell::new(2)), - })), - end: Rc::new(RefCell::new(Point { - x: Rc::new(RefCell::new(3)), - y: Rc::new(RefCell::new(4)), - })), + start: Point { x: 1, y: 2 }, + end: Point { x: 3, y: 4 }, })); - assert!(((((*(*(*l.borrow()).start.borrow()).x.borrow()) == 1) as i32) != 0)); - assert!(((((*(*(*l.borrow()).end.borrow()).y.borrow()) == 4) as i32) != 0)); + assert!(((({ (*l.borrow()).start.x } == 1) as i32) != 0)); + assert!(((({ (*l.borrow()).end.y } == 4) as i32) != 0)); let a: Value = Rc::new(RefCell::new(Node { - value: Rc::new(RefCell::new(1)), - next: Rc::new(RefCell::new(Ptr::::null())), + value: 1, + next: Ptr::::null(), })); let b: Value = Rc::new(RefCell::new(Node { - value: Rc::new(RefCell::new(2)), - next: Rc::new(RefCell::new((a.as_pointer()))), + value: 2, + next: (a.as_pointer()), })); - assert!( - ((((*(*(*(*b.borrow()).next.borrow()).upgrade().deref()) - .value - .borrow()) - == 1) as i32) - != 0) - ); + assert!(((({ (*b.borrow()).next.clone() }.with(|__s: &Node| __s.value) == 1) as i32) != 0)); let c: Value = Rc::new(RefCell::new(Container { - inner: Rc::new(RefCell::new(Inner { - a: Rc::new(RefCell::new(5)), - b: Rc::new(RefCell::new(6)), - })), - color: Rc::new(RefCell::new(Color_GREEN)), - count: Rc::new(RefCell::new(42)), + inner: Inner { a: 5, b: 6 }, + color: Color_GREEN, + count: 42, })); - assert!(((((*(*(*c.borrow()).inner.borrow()).a.borrow()) == 5) as i32) != 0)); - assert!(((((*(*(*c.borrow()).inner.borrow()).b.borrow()) == 6) as i32) != 0)); - assert!( - (((((*(*c.borrow()).color.borrow()) as u32) == ((Color_GREEN as i32) as u32)) as i32) != 0) - ); - assert!(((((*(*c.borrow()).count.borrow()) == 42) as i32) != 0)); + assert!(((({ (*c.borrow()).inner.a } == 5) as i32) != 0)); + assert!(((({ (*c.borrow()).inner.b } == 6) as i32) != 0)); + assert!((((({ (*c.borrow()).color } as u32) == ((Color_GREEN as i32) as u32)) as i32) != 0)); + assert!(((({ (*c.borrow()).count } == 42) as i32) != 0)); let c2: Value = >::default(); - (*(*c2.borrow()).color.borrow_mut()) = Color_BLUE; - assert!((((((*(*c2.borrow()).color.borrow()) as u32) == 2_u32) as i32) != 0)); + (*c2.borrow_mut()).color = Color_BLUE; + assert!((((({ (*c2.borrow()).color } as u32) == 2_u32) as i32) != 0)); return 0; } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/class.rs b/tests/unit/out/refcount/class.rs index 631329eb81..0fb0573fd9 100644 --- a/tests/unit/out/refcount/class.rs +++ b/tests/unit/out/refcount/class.rs @@ -6,70 +6,56 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Pair { - pub first: Value, - pub second: Value, -} -impl Clone for Pair { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - first: Rc::new(RefCell::new((*self.first.borrow()))), - second: Rc::new(RefCell::new((*self.second.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub first: i32, + #[offset(4)] + pub second: i32, } impl ByteRepr for Pair { fn byte_size() -> usize { 8 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.first.borrow()).to_bytes(&mut buf[0..4]); - (*self.second.borrow()).to_bytes(&mut buf[4..8]); + self.first.to_bytes(&mut buf[0..4]); + self.second.to_bytes(&mut buf[4..8]); } fn from_bytes(buf: &[u8]) -> Self { Self { - first: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - second: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + first: ::from_bytes(&buf[0..4]), + second: ::from_bytes(&buf[4..8]), } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Route { - pub path: Value, - pub cost: Value, -} -impl Clone for Route { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - path: Rc::new(RefCell::new((*self.path.borrow()).clone())), - cost: Rc::new(RefCell::new((*self.cost.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub path: Pair, + #[offset(8)] + pub cost: f64, } impl ByteRepr for Route { fn byte_size() -> usize { 16 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.path.borrow()).to_bytes(&mut buf[0..8]); - (*self.cost.borrow()).to_bytes(&mut buf[8..16]); + self.path.to_bytes(&mut buf[0..8]); + self.cost.to_bytes(&mut buf[8..16]); } fn from_bytes(buf: &[u8]) -> Self { Self { - path: Rc::new(RefCell::new(::from_bytes(&buf[0..8]))), - cost: Rc::new(RefCell::new(::from_bytes(&buf[8..16]))), + path: ::from_bytes(&buf[0..8]), + cost: ::from_bytes(&buf[8..16]), } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Counter { - pub v: Value, - pub calls: Value, + #[offset(0)] + pub v: i32, + #[offset(4)] + pub calls: i32, } impl std::cmp::PartialEq for Counter { fn eq(&self, other: &Self) -> bool { @@ -90,43 +76,31 @@ impl std::cmp::PartialEq for Counter { } } impl std::cmp::Eq for Counter {} -impl Clone for Counter { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*self.v.borrow()))), - calls: Rc::new(RefCell::new((*self.calls.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } -} impl ByteRepr for Counter { fn byte_size() -> usize { 8 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.v.borrow()).to_bytes(&mut buf[0..4]); - (*self.calls.borrow()).to_bytes(&mut buf[4..8]); + self.v.to_bytes(&mut buf[0..4]); + self.calls.to_bytes(&mut buf[4..8]); } fn from_bytes(buf: &[u8]) -> Self { Self { - v: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - calls: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + v: ::from_bytes(&buf[0..4]), + calls: ::from_bytes(&buf[4..8]), } } } pub fn RandomRoute_0(route: Ptr) -> i32 { - if (((*(*(*route.upgrade().deref()).path.borrow()).first.borrow()) % 2) != 0) { + if ((route.with(|__s: &Route| __s.path.first) % 2) != 0) { return ({ - let _new_first: i32 = - ({ PairImpl::SetSecond(&(*route.upgrade().deref()).path.as_pointer(), 10) }); - PairImpl::SetFirst(&(*route.upgrade().deref()).path.as_pointer(), _new_first) + let _new_first: i32 = ({ PairImpl::SetSecond(&{ field_ptr!(route, path) }, 10) }); + PairImpl::SetFirst(&{ field_ptr!(route, path) }, _new_first) }); } else { return ({ - let _new_second: i32 = - ({ PairImpl::SetFirst(&(*route.upgrade().deref()).path.as_pointer(), -10_i32) }); - PairImpl::SetSecond(&(*route.upgrade().deref()).path.as_pointer(), _new_second) + let _new_second: i32 = ({ PairImpl::SetFirst(&{ field_ptr!(route, path) }, -10_i32) }); + PairImpl::SetSecond(&{ field_ptr!(route, path) }, _new_second) }); } panic!("ub: non-void function does not return a value") @@ -137,18 +111,18 @@ pub fn main() { } fn main_0() -> i32 { let route1: Value = Rc::new(RefCell::new(Route { - path: Rc::new(RefCell::new(Pair { - first: Rc::new(RefCell::new(0)), - second: Rc::new(RefCell::new(1)), - })), - cost: Rc::new(RefCell::new(5_f64)), + path: Pair { + first: 0, + second: 1, + }, + cost: 5_f64, })); let route2: Value = Rc::new(RefCell::new(Route { - path: Rc::new(RefCell::new(Pair { - first: Rc::new(RefCell::new(1)), - second: Rc::new(RefCell::new(0)), - })), - cost: Rc::new(RefCell::new(10_f64)), + path: Pair { + first: 1, + second: 0, + }, + cost: 10_f64, })); let old_cost: Value = Rc::new(RefCell::new( ({ @@ -164,22 +138,16 @@ fn main_0() -> i32 { + (*old_cost.borrow())) == 9_f64) ); - let c1: Value = Rc::new(RefCell::new(Counter { - v: Rc::new(RefCell::new(3)), - calls: Rc::new(RefCell::new(0)), - })); - let c2: Value = Rc::new(RefCell::new(Counter { - v: Rc::new(RefCell::new(3)), - calls: Rc::new(RefCell::new(0)), - })); + let c1: Value = Rc::new(RefCell::new(Counter { v: 3, calls: 0 })); + let c2: Value = Rc::new(RefCell::new(Counter { v: 3, calls: 0 })); let pc: Value> = Rc::new(RefCell::new((c1.as_pointer()))); assert!((({ CounterImpl::Get(&c1.as_pointer(),) }) == 3)); assert!((({ CounterImpl::Get(&c2.as_pointer(),) }) == 3)); assert!((({ CounterImpl::Get(&(*pc.borrow()),) }) == 3)); assert!(({ CounterImpl::operator_eq(&c1.as_pointer(), c2.as_pointer(),) })); assert!(({ CounterImpl::operator_eq(&c2.as_pointer(), c1.as_pointer(),) })); - assert!(((*(*c1.borrow()).calls.borrow()) == 3)); - assert!(((*(*c2.borrow()).calls.borrow()) == 2)); + assert!(({ (*c1.borrow()).calls } == 3)); + assert!(({ (*c2.borrow()).calls } == 2)); return 0; } pub trait CounterImpl { @@ -188,14 +156,14 @@ pub trait CounterImpl { } impl CounterImpl for Ptr { fn Get(&self) -> i32 { - (*(*(*self).upgrade().deref()).calls.borrow_mut()).prefix_inc(); - return (*(*(*self).upgrade().deref()).v.borrow()); + (*self).with_mut(|__s: &mut Counter| __s.calls.prefix_inc()); + return (*self).with(|__s: &Counter| __s.v); } fn operator_eq(&self, o: Ptr) -> bool { - (*(*(*self).upgrade().deref()).calls.borrow_mut()).prefix_inc(); + (*self).with_mut(|__s: &mut Counter| __s.calls.prefix_inc()); return { - let _lhs = (*(*(*self).upgrade().deref()).v.borrow()); - _lhs == (*(*o.upgrade().deref()).v.borrow()) + let _lhs = (*self).with(|__s: &Counter| __s.v); + _lhs == o.with(|__s: &Counter| __s.v) }; } } @@ -210,10 +178,10 @@ pub trait PairImpl { impl PairImpl for Ptr { fn NOP(&self) {} fn GetFirst(&self) -> i32 { - return (*(*(*self).upgrade().deref()).first.borrow()); + return (*self).with(|__s: &Pair| __s.first); } fn GetSecond(&self) -> i32 { - return (*(*(*self).upgrade().deref()).second.borrow()); + return (*self).with(|__s: &Pair| __s.second); } fn Set(&self, field: Ptr, new_val: i32) -> i32 { let new_val: Value = Rc::new(RefCell::new(new_val)); @@ -227,7 +195,7 @@ impl PairImpl for Ptr { let new_first: Value = Rc::new(RefCell::new(new_first)); return (({ PairImpl::GetFirst(self) }) + ({ - let _field: Ptr = (*(*self).upgrade().deref()).first.as_pointer(); + let _field: Ptr = field_ptr!((*self), first); PairImpl::Set(self, _field, (*new_first.borrow())) })); } @@ -235,7 +203,7 @@ impl PairImpl for Ptr { let new_second: Value = Rc::new(RefCell::new(new_second)); return (({ PairImpl::GetSecond(self) }) + ({ - let _field: Ptr = (*(*self).upgrade().deref()).second.as_pointer(); + let _field: Ptr = field_ptr!((*self), second); PairImpl::Set(self, _field, (*new_second.borrow())) })); } @@ -246,9 +214,8 @@ pub trait RouteImpl { impl RouteImpl for Ptr { fn SetCost(&self, new_cost: f64) -> f64 { let new_cost: Value = Rc::new(RefCell::new(new_cost)); - let old_cost: Value = - Rc::new(RefCell::new((*(*(*self).upgrade().deref()).cost.borrow()))); - (*(*(*self).upgrade().deref()).cost.borrow_mut()) = (*new_cost.borrow()); + let old_cost: Value = Rc::new(RefCell::new((*self).with(|__s: &Route| __s.cost))); + (*self).with_mut(|__s: &mut Route| __s.cost = (*new_cost.borrow())); return (*old_cost.borrow()); } } diff --git a/tests/unit/out/refcount/class_template_lazy_member.rs b/tests/unit/out/refcount/class_template_lazy_member.rs index bb71ef5cc9..ce1ab54c87 100644 --- a/tests/unit/out/refcount/class_template_lazy_member.rs +++ b/tests/unit/out/refcount/class_template_lazy_member.rs @@ -6,81 +6,57 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Point { - pub x: Value, -} -impl Clone for Point { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - x: Rc::new(RefCell::new((*self.x.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub x: i32, } impl ByteRepr for Point { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.x.borrow()).to_bytes(&mut buf[0..4]); + self.x.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - x: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + x: ::from_bytes(&buf[0..4]), } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Box_int_ { - pub val: Value, -} -impl Clone for Box_int_ { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - val: Rc::new(RefCell::new((*self.val.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub val: i32, } impl ByteRepr for Box_int_ { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.val.borrow()).to_bytes(&mut buf[0..4]); + self.val.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - val: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + val: ::from_bytes(&buf[0..4]), } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Box_Point_ { - pub val: Value, -} -impl Clone for Box_Point_ { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - val: Rc::new(RefCell::new((*self.val.borrow()).clone())), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub val: Point, } impl ByteRepr for Box_Point_ { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.val.borrow()).to_bytes(&mut buf[0..4]); + self.val.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - val: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + val: ::from_bytes(&buf[0..4]), } } } @@ -89,16 +65,12 @@ pub fn main() { std::process::exit(main_0()); } fn main_0() -> i32 { - let i: Value = Rc::new(RefCell::new(Box_int_ { - val: Rc::new(RefCell::new(3)), - })); + let i: Value = Rc::new(RefCell::new(Box_int_ { val: 3 })); assert!((({ Box_int_Impl::twice(&i.as_pointer(),) }) == 6)); let p: Value = Rc::new(RefCell::new(Box_Point_ { - val: Rc::new(RefCell::new(Point { - x: Rc::new(RefCell::new(4)), - })), + val: Point { x: 4 }, })); - assert!(((*({ Box_Point_Impl::get(&p.as_pointer(),) }).x.borrow()) == 4)); + assert!(({ ({ Box_Point_Impl::get(&p.as_pointer(),) }).x } == 4)); return 0; } pub trait Box_Point_Impl { @@ -109,7 +81,7 @@ pub trait Box_Point_Impl { } impl Box_Point_Impl for Ptr { fn get(&self) -> Point { - return (*(*(*self).upgrade().deref()).val.borrow()).clone(); + return ((*(*self).upgrade().deref()).val).clone(); } } pub trait Box_int_Impl { @@ -120,8 +92,7 @@ pub trait Box_int_Impl { } impl Box_int_Impl for Ptr { fn twice(&self) -> i32 { - return ((*(*(*self).upgrade().deref()).val.borrow()) - + (*(*(*self).upgrade().deref()).val.borrow())); + return ((*self).with(|__s: &Box_int_| __s.val) + (*self).with(|__s: &Box_int_| __s.val)); } } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/class_templates.rs b/tests/unit/out/refcount/class_templates.rs index 638c159d63..319acb1e66 100644 --- a/tests/unit/out/refcount/class_templates.rs +++ b/tests/unit/out/refcount/class_templates.rs @@ -6,51 +6,106 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Record, VaArg, FnPtrArg, Default)] pub struct MyContainer_int_ { + #[offset(0)] vec_: Value>, } impl Clone for MyContainer_int_ { fn clone(&self) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - vec_: Rc::new(RefCell::new((*self.vec_.borrow()).clone())), + vec_: { Rc::new(RefCell::new((*self.vec_.borrow()).clone())) }, })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } } +<<<<<<< HEAD impl ByteRepr for MyContainer_int_ {} #[derive(VaArg, FnPtrArg, Default)] +======= +impl ByteRepr for MyContainer_int_ { + fn byte_size() -> usize { + 24 + } + fn to_bytes(&self, buf: &mut [u8]) { + self.vec_.to_bytes(&mut buf[0..24]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + vec_: >>::from_bytes(&buf[0..24]), + } + } +} +#[derive(Record, VaArg, FnPtrArg, Default)] +>>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub struct MyContainer_char_ { + #[offset(0)] vec_: Value>, } impl Clone for MyContainer_char_ { fn clone(&self) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - vec_: Rc::new(RefCell::new((*self.vec_.borrow()).clone())), + vec_: { Rc::new(RefCell::new((*self.vec_.borrow()).clone())) }, })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } } +<<<<<<< HEAD impl ByteRepr for MyContainer_char_ {} #[derive(VaArg, FnPtrArg, Default)] +======= +impl ByteRepr for MyContainer_char_ { + fn byte_size() -> usize { + 24 + } + fn to_bytes(&self, buf: &mut [u8]) { + self.vec_.to_bytes(&mut buf[0..24]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + vec_: >>::from_bytes(&buf[0..24]), + } + } +} +#[derive(Record, VaArg, FnPtrArg, Default)] +>>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub struct MyContainer_float_ { + #[offset(0)] vec_: Value>, } impl Clone for MyContainer_float_ { fn clone(&self) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - vec_: Rc::new(RefCell::new((*self.vec_.borrow()).clone())), + vec_: { Rc::new(RefCell::new((*self.vec_.borrow()).clone())) }, })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } } +<<<<<<< HEAD impl ByteRepr for MyContainer_float_ {} #[derive(VaArg, FnPtrArg, Default)] +======= +impl ByteRepr for MyContainer_float_ { + fn byte_size() -> usize { + 24 + } + fn to_bytes(&self, buf: &mut [u8]) { + self.vec_.to_bytes(&mut buf[0..24]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + vec_: >>::from_bytes(&buf[0..24]), + } + } +} +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] +>>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub struct Boxed_int_ { - pub value: Value, + #[offset(0)] + pub value: i32, } impl Boxed_int_ { pub fn twice(v: i32) -> i32 { @@ -58,31 +113,23 @@ impl Boxed_int_ { return ((*v.borrow()) + (*v.borrow())); } } -impl Clone for Boxed_int_ { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - value: Rc::new(RefCell::new((*self.value.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } -} impl ByteRepr for Boxed_int_ { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.value.borrow()).to_bytes(&mut buf[0..4]); + self.value.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - value: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + value: ::from_bytes(&buf[0..4]), } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Boxed_long_ { - pub value: Value, + #[offset(0)] + pub value: i64, } impl Boxed_long_ { pub fn twice(v: i64) -> i64 { @@ -90,167 +137,118 @@ impl Boxed_long_ { return ((*v.borrow()) + (*v.borrow())); } } -impl Clone for Boxed_long_ { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - value: Rc::new(RefCell::new((*self.value.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } -} impl ByteRepr for Boxed_long_ { fn byte_size() -> usize { 8 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.value.borrow()).to_bytes(&mut buf[0..8]); + self.value.to_bytes(&mut buf[0..8]); } fn from_bytes(buf: &[u8]) -> Self { Self { - value: Rc::new(RefCell::new(::from_bytes(&buf[0..8]))), + value: ::from_bytes(&buf[0..8]), } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Outer_int__Inner_int_ { - pub t: Value, - pub u: Value, -} -impl Clone for Outer_int__Inner_int_ { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - t: Rc::new(RefCell::new((*self.t.borrow()))), - u: Rc::new(RefCell::new((*self.u.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub t: i32, + #[offset(4)] + pub u: i32, } impl ByteRepr for Outer_int__Inner_int_ { fn byte_size() -> usize { 8 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.t.borrow()).to_bytes(&mut buf[0..4]); - (*self.u.borrow()).to_bytes(&mut buf[4..8]); + self.t.to_bytes(&mut buf[0..4]); + self.u.to_bytes(&mut buf[4..8]); } fn from_bytes(buf: &[u8]) -> Self { Self { - t: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - u: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + t: ::from_bytes(&buf[0..4]), + u: ::from_bytes(&buf[4..8]), } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Outer_int_ { - pub v: Value, -} -impl Clone for Outer_int_ { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*self.v.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub v: i32, } impl ByteRepr for Outer_int_ { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.v.borrow()).to_bytes(&mut buf[0..4]); + self.v.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - v: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + v: ::from_bytes(&buf[0..4]), } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Outer_long__Inner_int_ { - pub t: Value, - pub u: Value, -} -impl Clone for Outer_long__Inner_int_ { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - t: Rc::new(RefCell::new((*self.t.borrow()))), - u: Rc::new(RefCell::new((*self.u.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub t: i64, + #[offset(8)] + pub u: i32, } impl ByteRepr for Outer_long__Inner_int_ { fn byte_size() -> usize { 16 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.t.borrow()).to_bytes(&mut buf[0..8]); - (*self.u.borrow()).to_bytes(&mut buf[8..12]); + self.t.to_bytes(&mut buf[0..8]); + self.u.to_bytes(&mut buf[8..12]); } fn from_bytes(buf: &[u8]) -> Self { Self { - t: Rc::new(RefCell::new(::from_bytes(&buf[0..8]))), - u: Rc::new(RefCell::new(::from_bytes(&buf[8..12]))), + t: ::from_bytes(&buf[0..8]), + u: ::from_bytes(&buf[8..12]), } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Outer_long__Inner_char_ { - pub t: Value, - pub u: Value, -} -impl Clone for Outer_long__Inner_char_ { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - t: Rc::new(RefCell::new((*self.t.borrow()))), - u: Rc::new(RefCell::new((*self.u.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub t: i64, + #[offset(8)] + pub u: u8, } impl ByteRepr for Outer_long__Inner_char_ { fn byte_size() -> usize { 16 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.t.borrow()).to_bytes(&mut buf[0..8]); - (*self.u.borrow()).to_bytes(&mut buf[8..9]); + self.t.to_bytes(&mut buf[0..8]); + self.u.to_bytes(&mut buf[8..9]); } fn from_bytes(buf: &[u8]) -> Self { Self { - t: Rc::new(RefCell::new(::from_bytes(&buf[0..8]))), - u: Rc::new(RefCell::new(::from_bytes(&buf[8..9]))), + t: ::from_bytes(&buf[0..8]), + u: ::from_bytes(&buf[8..9]), } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Outer_long_ { - pub v: Value, -} -impl Clone for Outer_long_ { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*self.v.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub v: i64, } impl ByteRepr for Outer_long_ { fn byte_size() -> usize { 8 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.v.borrow()).to_bytes(&mut buf[0..8]); + self.v.to_bytes(&mut buf[0..8]); } fn from_bytes(buf: &[u8]) -> Self { Self { - v: Rc::new(RefCell::new(::from_bytes(&buf[0..8]))), + v: ::from_bytes(&buf[0..8]), } } } @@ -259,9 +257,7 @@ pub fn main() { std::process::exit(main_0()); } fn main_0() -> i32 { - let oi: Value = Rc::new(RefCell::new(Outer_int_ { - v: Rc::new(RefCell::new(3)), - })); + let oi: Value = Rc::new(RefCell::new(Outer_int_ { v: 3 })); assert!( (({ Outer_int__Inner_int_Impl::sum( @@ -272,12 +268,10 @@ fn main_0() -> i32 { ) }) == 7) ); - let ol: Value = Rc::new(RefCell::new(Outer_long_ { - v: Rc::new(RefCell::new(5_i64)), - })); + let ol: Value = Rc::new(RefCell::new(Outer_long_ { v: 5_i64 })); let ic: Value = Rc::new(RefCell::new(Outer_long__Inner_char_ { - t: Rc::new(RefCell::new(6_i64)), - u: Rc::new(RefCell::new(('a' as u8))), + t: 6_i64, + u: ('a' as u8), })); assert!( (({ @@ -293,14 +287,10 @@ fn main_0() -> i32 { (({ Outer_long__Inner_char_Impl::sum(&ic.as_pointer(),) }) == (6 + (('a' as u8) as i32))) ); assert!((({ Boxed_int_::twice(3,) }) == 6)); - let bi: Value = Rc::new(RefCell::new(Boxed_int_ { - value: Rc::new(RefCell::new(4)), - })); + let bi: Value = Rc::new(RefCell::new(Boxed_int_ { value: 4 })); assert!((({ Boxed_int_Impl::plus(&bi.as_pointer(), 5,) }) == 9)); assert!((({ Boxed_long_::twice(10_i64,) }) == 20_i64)); - let bl: Value = Rc::new(RefCell::new(Boxed_long_ { - value: Rc::new(RefCell::new(7_i64)), - })); + let bl: Value = Rc::new(RefCell::new(Boxed_long_ { value: 7_i64 })); assert!((({ Boxed_long_Impl::plus(&bl.as_pointer(), 1_i64,) }) == 8_i64)); let imc: Value = Rc::new(RefCell::new(::default())); assert!(({ MyContainer_int_Impl::empty(&imc.as_pointer(),) })); @@ -348,7 +338,7 @@ pub trait Boxed_int_Impl { impl Boxed_int_Impl for Ptr { fn plus(&self, other: i32) -> i32 { let other: Value = Rc::new(RefCell::new(other)); - return ((*(*(*self).upgrade().deref()).value.borrow()) + (*other.borrow())); + return ((*self).with(|__s: &Boxed_int_| __s.value) + (*other.borrow())); } } pub trait Boxed_long_Impl { @@ -357,7 +347,7 @@ pub trait Boxed_long_Impl { impl Boxed_long_Impl for Ptr { fn plus(&self, other: i64) -> i64 { let other: Value = Rc::new(RefCell::new(other)); - return ((*(*(*self).upgrade().deref()).value.borrow()) + (*other.borrow())); + return ((*self).with(|__s: &Boxed_long_| __s.value) + (*other.borrow())); } } pub trait MyContainer_char_Impl { @@ -372,22 +362,25 @@ pub trait MyContainer_char_Impl { } impl MyContainer_char_Impl for Ptr { fn empty(&self) -> bool { - return (*(*(*self).upgrade().deref()).vec_.borrow()).is_empty(); + return (*self).with(|__s: &MyContainer_char_| (*__s.vec_.borrow()).is_empty()); } fn size(&self) -> usize { - return (*(*(*self).upgrade().deref()).vec_.borrow()).len(); + return (*self).with(|__s: &MyContainer_char_| (*__s.vec_.borrow()).len()); } fn back_4(&self) -> Ptr { - return ((*(*self).upgrade().deref()).vec_.as_pointer() as Ptr).to_last(); + return ((*self).with(|__s: &MyContainer_char_| __s.vec_.as_pointer()) as Ptr) + .to_last(); } fn pop_back(&self) { - (*(*(*self).upgrade().deref()).vec_.borrow_mut()).pop(); + (*self).with(|__s: &MyContainer_char_| { + (*__s.vec_.borrow_mut()).pop(); + }); return; } fn push_back(&self, item: Ptr) { { let a0_clone = (item.read()).clone(); - (*(*(*self).upgrade().deref()).vec_.borrow_mut()).push(a0_clone) + (*self).with(|__s: &MyContainer_char_| (*__s.vec_.borrow_mut()).push(a0_clone)) }; } } @@ -403,22 +396,25 @@ pub trait MyContainer_float_Impl { } impl MyContainer_float_Impl for Ptr { fn empty(&self) -> bool { - return (*(*(*self).upgrade().deref()).vec_.borrow()).is_empty(); + return (*self).with(|__s: &MyContainer_float_| (*__s.vec_.borrow()).is_empty()); } fn size(&self) -> usize { - return (*(*(*self).upgrade().deref()).vec_.borrow()).len(); + return (*self).with(|__s: &MyContainer_float_| (*__s.vec_.borrow()).len()); } fn back_4(&self) -> Ptr { - return ((*(*self).upgrade().deref()).vec_.as_pointer() as Ptr).to_last(); + return ((*self).with(|__s: &MyContainer_float_| __s.vec_.as_pointer()) as Ptr) + .to_last(); } fn pop_back(&self) { - (*(*(*self).upgrade().deref()).vec_.borrow_mut()).pop(); + (*self).with(|__s: &MyContainer_float_| { + (*__s.vec_.borrow_mut()).pop(); + }); return; } fn push_back(&self, item: Ptr) { { let a0_clone = (item.read()).clone(); - (*(*(*self).upgrade().deref()).vec_.borrow_mut()).push(a0_clone) + (*self).with(|__s: &MyContainer_float_| (*__s.vec_.borrow_mut()).push(a0_clone)) }; } } @@ -434,22 +430,25 @@ pub trait MyContainer_int_Impl { } impl MyContainer_int_Impl for Ptr { fn empty(&self) -> bool { - return (*(*(*self).upgrade().deref()).vec_.borrow()).is_empty(); + return (*self).with(|__s: &MyContainer_int_| (*__s.vec_.borrow()).is_empty()); } fn size(&self) -> usize { - return (*(*(*self).upgrade().deref()).vec_.borrow()).len(); + return (*self).with(|__s: &MyContainer_int_| (*__s.vec_.borrow()).len()); } fn back_4(&self) -> Ptr { - return ((*(*self).upgrade().deref()).vec_.as_pointer() as Ptr).to_last(); + return ((*self).with(|__s: &MyContainer_int_| __s.vec_.as_pointer()) as Ptr) + .to_last(); } fn pop_back(&self) { - (*(*(*self).upgrade().deref()).vec_.borrow_mut()).pop(); + (*self).with(|__s: &MyContainer_int_| { + (*__s.vec_.borrow_mut()).pop(); + }); return; } fn push_back(&self, item: Ptr) { { let a0_clone = (item.read()).clone(); - (*(*(*self).upgrade().deref()).vec_.borrow_mut()).push(a0_clone) + (*self).with(|__s: &MyContainer_int_| (*__s.vec_.borrow_mut()).push(a0_clone)) }; } } @@ -460,8 +459,8 @@ impl Outer_int_Impl for Ptr { fn with(&self, n: i32) -> Outer_int__Inner_int_ { let n: Value = Rc::new(RefCell::new(n)); return Outer_int__Inner_int_ { - t: Rc::new(RefCell::new((*(*(*self).upgrade().deref()).v.borrow()))), - u: Rc::new(RefCell::new((*n.borrow()))), + t: { (*self).with(|__s: &Outer_int_| __s.v) }, + u: (*n.borrow()), }; } } @@ -470,8 +469,8 @@ pub trait Outer_int__Inner_int_Impl { } impl Outer_int__Inner_int_Impl for Ptr { fn sum(&self) -> i32 { - return (((*(*(*self).upgrade().deref()).t.borrow()) as i32) - + ((*(*(*self).upgrade().deref()).u.borrow()) as i32)); + return (((*self).with(|__s: &Outer_int__Inner_int_| __s.t) as i32) + + ((*self).with(|__s: &Outer_int__Inner_int_| __s.u) as i32)); } } pub trait Outer_long_Impl { @@ -481,8 +480,8 @@ impl Outer_long_Impl for Ptr { fn with(&self, n: i32) -> Outer_long__Inner_int_ { let n: Value = Rc::new(RefCell::new(n)); return Outer_long__Inner_int_ { - t: Rc::new(RefCell::new((*(*(*self).upgrade().deref()).v.borrow()))), - u: Rc::new(RefCell::new((*n.borrow()))), + t: { (*self).with(|__s: &Outer_long_| __s.v) }, + u: (*n.borrow()), }; } } @@ -491,8 +490,8 @@ pub trait Outer_long__Inner_char_Impl { } impl Outer_long__Inner_char_Impl for Ptr { fn sum(&self) -> i32 { - return (((*(*(*self).upgrade().deref()).t.borrow()) as i32) - + ((*(*(*self).upgrade().deref()).u.borrow()) as i32)); + return (((*self).with(|__s: &Outer_long__Inner_char_| __s.t) as i32) + + ((*self).with(|__s: &Outer_long__Inner_char_| __s.u) as i32)); } } pub trait Outer_long__Inner_int_Impl { @@ -500,8 +499,8 @@ pub trait Outer_long__Inner_int_Impl { } impl Outer_long__Inner_int_Impl for Ptr { fn sum(&self) -> i32 { - return (((*(*(*self).upgrade().deref()).t.borrow()) as i32) - + ((*(*(*self).upgrade().deref()).u.borrow()) as i32)); + return (((*self).with(|__s: &Outer_long__Inner_int_| __s.t) as i32) + + ((*self).with(|__s: &Outer_long__Inner_int_| __s.u) as i32)); } } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/clone_vs_move.rs b/tests/unit/out/refcount/clone_vs_move.rs index f5377c3180..06d30a3045 100644 --- a/tests/unit/out/refcount/clone_vs_move.rs +++ b/tests/unit/out/refcount/clone_vs_move.rs @@ -6,73 +6,65 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Bar { - pub w: Value, -} -impl Clone for Bar { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - w: Rc::new(RefCell::new((*self.w.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub w: i32, } impl ByteRepr for Bar { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.w.borrow()).to_bytes(&mut buf[0..4]); + self.w.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - w: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + w: ::from_bytes(&buf[0..4]), } } } -#[derive(VaArg, FnPtrArg)] +#[derive(Clone, Record, VaArg, FnPtrArg)] pub struct Foo { - pub x: Value, + #[offset(0)] + pub x: i32, + #[offset(8)] pub y: Ptr, - pub z: Value>, - pub a: Value>, - pub bar: Value, -} -impl Clone for Foo { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - x: Rc::new(RefCell::new((*self.x.borrow()))), - y: (self.y).clone(), - z: Rc::new(RefCell::new((*self.z.borrow()).clone())), - a: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 3, _>( - |__i: usize| (*self.a.borrow())[(__i) as usize], - )))), - bar: Rc::new(RefCell::new((*self.bar.borrow()).clone())), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(16)] + pub z: Ptr, + #[offset(24)] + pub a: Box<[i32]>, + #[offset(36)] + pub bar: Bar, } impl Default for Foo { fn default() -> Self { Foo { - x: Rc::new(RefCell::new(0_i32)), + x: 0_i32, y: >::default(), - z: Rc::new(RefCell::new(Ptr::::null())), - a: Rc::new(RefCell::new((0..3).map(|_| 0_i32).collect::>())), - bar: >::default(), + z: Ptr::::null(), + a: (0..3).map(|_| 0_i32).collect::>(), + bar: ::default(), } } } -impl ByteRepr for Foo {} -#[derive(Clone, VaArg, FnPtrArg, Default)] +impl ByteRepr for Foo { + fn byte_size() -> usize { + 40 + } +} +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Refs { + #[offset(0)] pub a: Ptr, + #[offset(8)] pub b: Ptr, } -impl ByteRepr for Refs {} +impl ByteRepr for Refs { + fn byte_size() -> usize { + 16 + } +} pub fn main() { __cpp2rust_init_globals(); std::process::exit(main_0()); @@ -109,53 +101,51 @@ fn main_0() -> i32 { _lhs == ((*pointer.borrow()).read()) }); let f1: Value = Rc::new(RefCell::new(Foo { - x: Rc::new(RefCell::new(1)), + x: 1, y: x1.as_pointer(), - z: Rc::new(RefCell::new((x1.as_pointer()))), - a: Rc::new(RefCell::new(Box::new([0, 1, 2]))), - bar: Rc::new(RefCell::new(Bar { - w: Rc::new(RefCell::new(10)), - })), + z: (x1.as_pointer()), + a: Box::new([0, 1, 2]), + bar: Bar { w: 10 }, })); - assert!(((*(*f1.borrow()).x.borrow()) == 1)); + assert!(({ (*f1.borrow()).x } == 1)); assert!((((*f1.borrow()).y.read()) == 2)); assert!({ - let _lhs = (*(*f1.borrow()).z.borrow()).clone(); + let _lhs = { (*f1.borrow()).z.clone() }; _lhs == (x1.as_pointer()) }); - assert!((((*(*f1.borrow()).z.borrow()).read()) == 2)); + assert!((({ (*f1.borrow()).z.clone() }.read()) == 2)); let f2: Value = Rc::new(RefCell::new((*f1.borrow()).clone())); - (*(*f2.borrow()).x.borrow_mut()).prefix_inc(); - (*f2.borrow()).y.with_mut(|__v| __v.prefix_inc()); - assert!(((*(*f2.borrow()).x.borrow()) == 2)); + (*f2.borrow_mut()).x.prefix_inc(); + { (*f2.borrow()).y.clone() }.with_mut(|__v| __v.prefix_inc()); + assert!(({ (*f2.borrow()).x } == 2)); assert!((((*f2.borrow()).y.read()) == 3)); - assert!(((*(*f1.borrow()).x.borrow()) == 1)); + assert!(({ (*f1.borrow()).x } == 1)); assert!((((*f1.borrow()).y.read()) == 3)); - (*(*f2.borrow()).z.borrow()).with_mut(|__v| __v.prefix_inc()); + { (*f2.borrow()).z.clone() }.with_mut(|__v| __v.prefix_inc()); assert!((((*f2.borrow()).y.read()) == 4)); assert!({ - let _lhs = (*(*f2.borrow()).z.borrow()).clone(); + let _lhs = { (*f2.borrow()).z.clone() }; _lhs == (x1.as_pointer()) }); - assert!((((*(*f2.borrow()).z.borrow()).read()) == 4)); + assert!((({ (*f2.borrow()).z.clone() }.read()) == 4)); assert!((((*f1.borrow()).y.read()) == 4)); assert!({ - let _lhs = (*(*f1.borrow()).z.borrow()).clone(); + let _lhs = { (*f1.borrow()).z.clone() }; _lhs == (x1.as_pointer()) }); - assert!((((*(*f1.borrow()).z.borrow()).read()) == 4)); - (*(*f2.borrow()).a.borrow_mut())[(0) as usize].prefix_inc(); - (*(*f2.borrow()).a.borrow_mut())[(1) as usize].prefix_inc(); - (*(*f2.borrow()).a.borrow_mut())[(2) as usize].prefix_inc(); - assert!(((*(*f2.borrow()).a.borrow())[(0) as usize] == 1)); - assert!(((*(*f2.borrow()).a.borrow())[(1) as usize] == 2)); - assert!(((*(*f2.borrow()).a.borrow())[(2) as usize] == 3)); - assert!(((*(*f1.borrow()).a.borrow())[(0) as usize] == 0)); - assert!(((*(*f1.borrow()).a.borrow())[(1) as usize] == 1)); - assert!(((*(*f1.borrow()).a.borrow())[(2) as usize] == 2)); - (*(*(*f2.borrow()).bar.borrow()).w.borrow_mut()) = 20; - assert!(((*(*(*f2.borrow()).bar.borrow()).w.borrow()) == 20)); - assert!(((*(*(*f1.borrow()).bar.borrow()).w.borrow()) == 10)); + assert!((({ (*f1.borrow()).z.clone() }.read()) == 4)); + (*f2.borrow_mut()).a[(0) as usize].prefix_inc(); + (*f2.borrow_mut()).a[(1) as usize].prefix_inc(); + (*f2.borrow_mut()).a[(2) as usize].prefix_inc(); + assert!(((*f2.borrow()).a[(0) as usize] == 1)); + assert!(((*f2.borrow()).a[(1) as usize] == 2)); + assert!(((*f2.borrow()).a[(2) as usize] == 3)); + assert!(((*f1.borrow()).a[(0) as usize] == 0)); + assert!(((*f1.borrow()).a[(1) as usize] == 1)); + assert!(((*f1.borrow()).a[(2) as usize] == 2)); + (*f2.borrow_mut()).bar.w = 20; + assert!(({ (*f2.borrow()).bar.w } == 20)); + assert!(({ (*f1.borrow()).bar.w } == 10)); let N: Value = Rc::new(RefCell::new(5)); let v1: Value> = Rc::new(RefCell::new(Vec::new())); let i: Value = Rc::new(RefCell::new(0)); @@ -470,16 +460,12 @@ fn main_0() -> i32 { assert!( ((((s1.as_pointer() as Ptr).offset(2_usize).read()) as i32) == (('a' as u8) as i32)) ); - let b1: Value = Rc::new(RefCell::new(Bar { - w: Rc::new(RefCell::new(1)), - })); - let b2: Value = Rc::new(RefCell::new(Bar { - w: Rc::new(RefCell::new(2)), - })); + let b1: Value = Rc::new(RefCell::new(Bar { w: 1 })); + let b2: Value = Rc::new(RefCell::new(Bar { w: 2 })); (*b2.borrow_mut()) = (*b1.borrow()).clone(); - (*(*b2.borrow()).w.borrow_mut()).postfix_inc(); - assert!(((*(*b1.borrow()).w.borrow()) == 1)); - assert!(((*(*b2.borrow()).w.borrow()) == 2)); + (*b2.borrow_mut()).w.postfix_inc(); + assert!(({ (*b1.borrow()).w } == 1)); + assert!(({ (*b2.borrow()).w } == 2)); let v4: Value> = Rc::new(RefCell::new(Vec::new())); (v4.as_pointer() as Ptr>).write((*v2.borrow()).clone()); let i: Value = Rc::new(RefCell::new(0)); @@ -518,8 +504,8 @@ fn main_0() -> i32 { b: rb.as_pointer(), })); let r2: Value = Rc::new(RefCell::new((*r1.borrow()).clone())); - (*r2.borrow()).a.write(10); - (*r2.borrow()).b.with_mut(|__v| __v.prefix_inc()); + { (*r2.borrow()).a.clone() }.write(10); + { (*r2.borrow()).b.clone() }.with_mut(|__v| __v.prefix_inc()); assert!(((*ra.borrow()) == 10)); assert!(((*rb.borrow()) == 3)); assert!((((*r1.borrow()).a.read()) == 10)); diff --git a/tests/unit/out/refcount/complex_function.rs b/tests/unit/out/refcount/complex_function.rs index 77393c257a..fd765c077d 100644 --- a/tests/unit/out/refcount/complex_function.rs +++ b/tests/unit/out/refcount/complex_function.rs @@ -17,86 +17,67 @@ pub fn ptr_1(x: Ptr) -> Ptr { pub fn bar_2(x: Ptr) -> Ptr { return (x).clone(); } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct X1 { - pub v: Value, -} -impl Clone for X1 { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*self.v.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub v: i32, } impl ByteRepr for X1 { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.v.borrow()).to_bytes(&mut buf[0..4]); + self.v.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - v: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + v: ::from_bytes(&buf[0..4]), } } } -#[derive(Clone, VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct X2 { + #[offset(0)] pub v: Ptr, } -impl ByteRepr for X2 {} -#[derive(VaArg, FnPtrArg, Default)] -pub struct X3 { - pub v: Value>, -} -impl Clone for X3 { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*self.v.borrow()).clone())), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() +impl ByteRepr for X2 { + fn byte_size() -> usize { + 8 } } +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] +pub struct X3 { + #[offset(0)] + pub v: Ptr, +} impl ByteRepr for X3 { fn byte_size() -> usize { 8 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.v.borrow()).to_bytes(&mut buf[0..8]); + self.v.to_bytes(&mut buf[0..8]); } fn from_bytes(buf: &[u8]) -> Self { Self { - v: Rc::new(RefCell::new(>::from_bytes(&buf[0..8]))), + v: >::from_bytes(&buf[0..8]), } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct X4 { - pub v: Value, -} -impl Clone for X4 { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*self.v.borrow()).clone())), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub v: X3, } impl ByteRepr for X4 { fn byte_size() -> usize { 8 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.v.borrow()).to_bytes(&mut buf[0..8]); + self.v.to_bytes(&mut buf[0..8]); } fn from_bytes(buf: &[u8]) -> Self { Self { - v: Rc::new(RefCell::new(::from_bytes(&buf[0..8]))), + v: ::from_bytes(&buf[0..8]), } } } @@ -135,51 +116,37 @@ fn main_0() -> i32 { ((({ foo_0((*x3.borrow())) }) + (({ ptr_1((x3.as_pointer())) }).read())) + (({ bar_2(x2.as_pointer()) }).read())), )); - let a: Value = Rc::new(RefCell::new(X1 { - v: Rc::new(RefCell::new(0)), - })); + let a: Value = Rc::new(RefCell::new(X1 { v: 0 })); let b: Value = Rc::new(RefCell::new(X2 { v: a.as_pointer() })); let c: Value = Rc::new(RefCell::new(X3 { - v: Rc::new(RefCell::new((b.as_pointer()))), + v: (b.as_pointer()), })); let d: Value = Rc::new(RefCell::new(X4 { - v: Rc::new(RefCell::new((*c.borrow()).clone())), + v: (*c.borrow()).clone(), })); - (*(*(*(*(*(*d.borrow()).v.borrow()).v.borrow()).upgrade().deref()) + (*{ (*d.borrow()).v.v.clone() }.upgrade().deref()) .v - .upgrade() - .deref()) - .v - .borrow_mut()) = 0; - (*(*({ X2Impl::get(&({ X3Impl::get(&({ X4Impl::get(&d.as_pointer()) })) })) }) - .upgrade() - .deref()) - .v - .borrow_mut()) = 0; - (*(*(*d.borrow()).v.borrow()).v.borrow_mut()) = (b.as_pointer()); - let r4: Ptr = - (*({ X2Impl::get(&({ X3Impl::get(&({ X4Impl::get(&d.as_pointer()) })) })) }) - .upgrade() - .deref()) - .v - .as_pointer(); + .with_mut(|__s: &mut X1| __s.v = 0); + ({ X2Impl::get(&({ X3Impl::get(&({ X4Impl::get(&d.as_pointer()) })) })) }) + .with_mut(|__s: &mut X1| __s.v = 0); + (*d.borrow_mut()).v.v = (b.as_pointer()); + let r4: Ptr = field_ptr!( + ({ X2Impl::get(&({ X3Impl::get(&({ X4Impl::get(&d.as_pointer(),) }),) }),) }), + v + ); let r5: Ptr = ({ X2Impl::get(&({ X3Impl::get(&({ X4Impl::get(&d.as_pointer()) })) })) }); let p: Value> = Rc::new(RefCell::new( ({ X3Impl::get(&({ X4Impl::get(&d.as_pointer()) })) }), )); let r6: Ptr = ({ X4Impl::get(&d.as_pointer()) }); - let r7: Ptr = (*d.borrow()).v.as_pointer(); - let r8: Ptr = (*({ X2Impl::get(&({ X3Impl::get(&(*d.borrow()).v.as_pointer()) })) }) - .upgrade() - .deref()) - .v - .as_pointer(); + let r7: Ptr = field_ptr!(d, v); + let r8: Ptr = field_ptr!( + ({ X2Impl::get(&{ ({ X3Impl::get(&{ field_ptr!(d, v) },) }) },) }), + v + ); let x5: Value = Rc::new(RefCell::new( - (*(*({ X2Impl::get(&({ X3Impl::get(&({ X4Impl::get(&d.as_pointer()) })) })) }) - .upgrade() - .deref()) - .v - .borrow()), + ({ X2Impl::get(&({ X3Impl::get(&({ X4Impl::get(&d.as_pointer()) })) })) }) + .with(|__s: &X1| __s.v), )); { let _ptr = ({ bar_2(x1.as_pointer()) }); @@ -188,13 +155,10 @@ fn main_0() -> i32 { ({ bar_2(x1.as_pointer()) }).with_mut(|__v| __v.postfix_inc()); let bar_out: Value = Rc::new(RefCell::new( (({ - bar_2( - (*({ X2Impl::get(&({ X3Impl::get(&({ X4Impl::get(&d.as_pointer()) })) })) }) - .upgrade() - .deref()) - .v - .as_pointer(), - ) + bar_2(field_ptr!( + ({ X2Impl::get(&({ X3Impl::get(&({ X4Impl::get(&d.as_pointer(),) }),) }),) }), + v + )) }) .read()), )); @@ -206,71 +170,65 @@ fn main_0() -> i32 { (((({ bar_2(x1.as_pointer()) }).read()) + ({ foo_0((*x4.borrow())) })) + 1); { let _ptr = ({ - bar_2( - (*({ X2Impl::get(&({ X3Impl::get(&({ X4Impl::get(&d.as_pointer()) })) })) }) - .upgrade() - .deref()) - .v - .as_pointer(), - ) + bar_2(field_ptr!( + ({ X2Impl::get(&({ X3Impl::get(&({ X4Impl::get(&d.as_pointer(),) }),) }),) }), + v + )) }); _ptr.write(_ptr.read() + 10) }; - ({ - bar_2( - (*({ X2Impl::get(&({ X3Impl::get(&({ X4Impl::get(&d.as_pointer()) })) })) }) - .upgrade() - .deref()) - .v - .as_pointer(), - ) - }) - .with_mut(|__v| __v.postfix_inc()); - let bar_inc2: Value = Rc::new(RefCell::new( + { ({ - bar_2( - (*({ X2Impl::get(&({ X3Impl::get(&({ X4Impl::get(&d.as_pointer()) })) })) }) - .upgrade() - .deref()) - .v - .as_pointer(), - ) + bar_2(field_ptr!( + ({ X2Impl::get(&({ X3Impl::get(&({ X4Impl::get(&d.as_pointer(),) }),) }),) }), + v + )) }) + } + .with_mut(|__v| __v.postfix_inc()); + let bar_inc2: Value = Rc::new(RefCell::new( + { + ({ + bar_2(field_ptr!( + ({ X2Impl::get(&({ X3Impl::get(&({ X4Impl::get(&d.as_pointer(),) }),) }),) }), + v + )) + }) + } .with_mut(|__v| __v.prefix_inc()), )); - (*bar_inc2.borrow_mut()) = ({ - bar_2( - (*({ X2Impl::get(&({ X3Impl::get(&({ X4Impl::get(&d.as_pointer()) })) })) }) - .upgrade() - .deref()) - .v - .as_pointer(), - ) - }) + (*bar_inc2.borrow_mut()) = { + ({ + bar_2(field_ptr!( + ({ X2Impl::get(&({ X3Impl::get(&({ X4Impl::get(&d.as_pointer(),) }),) }),) }), + v + )) + }) + } .with_mut(|__v| __v.postfix_inc()); ({ ptr_1((x1.as_pointer())) }).with_mut(|__v| __v.prefix_inc()); { let _ptr = ({ ptr_1((x1.as_pointer())) }); _ptr.write(_ptr.read() + 1) }; - ({ - ptr_1( - ((*({ X2Impl::get(&({ X3Impl::get(&({ X4Impl::get(&d.as_pointer()) })) })) }) - .upgrade() - .deref()) - .v - .as_pointer()), - ) - }) + { + ({ + ptr_1( + (field_ptr!( + ({ X2Impl::get(&({ X3Impl::get(&({ X4Impl::get(&d.as_pointer(),) }),) }),) }), + v + )), + ) + }) + } .with_mut(|__v| __v.prefix_inc()); { let _ptr = ({ ptr_1( - ((*({ X2Impl::get(&({ X3Impl::get(&({ X4Impl::get(&d.as_pointer()) })) })) }) - .upgrade() - .deref()) - .v - .as_pointer()), + (field_ptr!( + ({ X2Impl::get(&({ X3Impl::get(&({ X4Impl::get(&d.as_pointer(),) }),) }),) }), + v + )), ) }); _ptr.write(_ptr.read() + 1) @@ -278,110 +236,96 @@ fn main_0() -> i32 { { let _ptr = ({ ptr_1( - ((*({ X2Impl::get(&({ X3Impl::get(&({ X4Impl::get(&d.as_pointer()) })) })) }) - .upgrade() - .deref()) - .v - .as_pointer()), + (field_ptr!( + ({ X2Impl::get(&({ X3Impl::get(&({ X4Impl::get(&d.as_pointer(),) }),) }),) }), + v + )), ) }); _ptr.write(_ptr.read() + 1) }; let ptr1: Value = Rc::new(RefCell::new( - ({ - ptr_1( - ((*({ X2Impl::get(&({ X3Impl::get(&({ X4Impl::get(&d.as_pointer()) })) })) }) - .upgrade() - .deref()) - .v - .as_pointer()), - ) - }) + { + ({ + ptr_1( + (field_ptr!( + ({ + X2Impl::get(&({ X3Impl::get(&({ X4Impl::get(&d.as_pointer()) })) })) + }), + v + )), + ) + }) + } .with_mut(|__v| __v.postfix_inc()), )); let ptr2: Ptr = ({ ptr_1( - ((*({ X2Impl::get(&({ X3Impl::get(&({ X4Impl::get(&d.as_pointer()) })) })) }) - .upgrade() - .deref()) - .v - .as_pointer()), + (field_ptr!( + ({ X2Impl::get(&({ X3Impl::get(&({ X4Impl::get(&d.as_pointer(),) }),) }),) }), + v + )), ) }); let ptr3: Value> = Rc::new(RefCell::new( ({ ptr_1( - ((*({ X2Impl::get(&({ X3Impl::get(&({ X4Impl::get(&d.as_pointer()) })) })) }) - .upgrade() - .deref()) - .v - .as_pointer()), + (field_ptr!( + ({ X2Impl::get(&({ X3Impl::get(&({ X4Impl::get(&d.as_pointer(),) }),) }),) }), + v + )), ) }), )); let vptr: Value = Rc::new(RefCell::new( (({ ptr_1( - ((*({ X2Impl::get(&({ X3Impl::get(&({ X4Impl::get(&d.as_pointer()) })) })) }) - .upgrade() - .deref()) - .v - .as_pointer()), + (field_ptr!( + ({ X2Impl::get(&({ X3Impl::get(&({ X4Impl::get(&d.as_pointer(),) }),) }),) }), + v + )), ) }) .read()), )); let pref: Value> = Rc::new(RefCell::new( ({ - bar_2( - (*({ X2Impl::get(&({ X3Impl::get(&({ X4Impl::get(&d.as_pointer()) })) })) }) - .upgrade() - .deref()) - .v - .as_pointer(), - ) + bar_2(field_ptr!( + ({ X2Impl::get(&({ X3Impl::get(&({ X4Impl::get(&d.as_pointer(),) }),) }),) }), + v + )) }), )); - ({ - bar_2( - (*({ X2Impl::get(&({ X3Impl::get(&({ X4Impl::get(&d.as_pointer()) })) })) }) - .upgrade() - .deref()) - .v - .as_pointer(), - ) - }) + { + ({ + bar_2(field_ptr!( + ({ X2Impl::get(&({ X3Impl::get(&({ X4Impl::get(&d.as_pointer(),) }),) }),) }), + v + )) + }) + } .with_mut(|__v| __v.postfix_inc()); assert!( ((((({ ptr_1( - ((*({ X2Impl::get(&({ X3Impl::get(&({ X4Impl::get(&d.as_pointer()) })) })) }) - .upgrade() - .deref()) - .v - .as_pointer()), + (field_ptr!( + ({ X2Impl::get(&({ X3Impl::get(&({ X4Impl::get(&d.as_pointer(),) }),) }),) }), + v + )), ) }) .read()) + (({ - bar_2( - (*({ X2Impl::get(&({ X3Impl::get(&({ X4Impl::get(&d.as_pointer()) })) })) }) - .upgrade() - .deref()) - .v - .as_pointer(), - ) + bar_2(field_ptr!( + ({ X2Impl::get(&({ X3Impl::get(&({ X4Impl::get(&d.as_pointer(),) }),) }),) }), + v + )) }) .read())) + ({ foo_0( - (*(*({ - X2Impl::get(&({ X3Impl::get(&({ X4Impl::get(&d.as_pointer()) })) })) - }) - .upgrade() - .deref()) - .v - .borrow()), + ({ X2Impl::get(&({ X3Impl::get(&({ X4Impl::get(&d.as_pointer()) })) })) }) + .with(|__s: &X1| __s.v), ) })) == 54) @@ -401,7 +345,7 @@ pub trait X3Impl { } impl X3Impl for Ptr { fn get(&self) -> Ptr { - return (*(*(*self).upgrade().deref()).v.borrow()).clone(); + return (*self).with(|__s: &X3| (__s.v).clone()); } } pub trait X4Impl { @@ -409,7 +353,7 @@ pub trait X4Impl { } impl X4Impl for Ptr { fn get(&self) -> Ptr { - return (*(*self).upgrade().deref()).v.as_pointer(); + return field_ptr!((*self), v); } } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/compound_assign_paren.rs b/tests/unit/out/refcount/compound_assign_paren.rs index 7dbe5c4dd0..b4c4b70120 100644 --- a/tests/unit/out/refcount/compound_assign_paren.rs +++ b/tests/unit/out/refcount/compound_assign_paren.rs @@ -6,29 +6,21 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Item { - pub flags: Value, -} -impl Clone for Item { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - flags: Rc::new(RefCell::new((*self.flags.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub flags: u8, } impl ByteRepr for Item { fn byte_size() -> usize { 1 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.flags.borrow()).to_bytes(&mut buf[0..1]); + self.flags.to_bytes(&mut buf[0..1]); } fn from_bytes(buf: &[u8]) -> Self { Self { - flags: Rc::new(RefCell::new(::from_bytes(&buf[0..1]))), + flags: ::from_bytes(&buf[0..1]), } } } @@ -37,27 +29,23 @@ pub fn main() { std::process::exit(main_0()); } fn main_0() -> i32 { - let item: Value = Rc::new(RefCell::new(Item { - flags: Rc::new(RefCell::new(0_u8)), - })); + let item: Value = Rc::new(RefCell::new(Item { flags: 0_u8 })); let ptr: Value> = Rc::new(RefCell::new((item.as_pointer()))); { - let rhs_0 = - (((*(*(*ptr.borrow()).upgrade().deref()).flags.borrow()) as i32) | (1 << 0)) as u8; - (*(*(*ptr.borrow()).upgrade().deref()).flags.borrow_mut()) = rhs_0 + let rhs_0 = (((*ptr.borrow()).with(|__s: &Item| __s.flags) as i32) | (1 << 0)) as u8; + (*ptr.borrow()).with_mut(|__s: &mut Item| __s.flags = rhs_0) }; { - let rhs_0 = - (((*(*(*ptr.borrow()).upgrade().deref()).flags.borrow()) as i32) | (1 << 1)) as u8; - (*(*(*ptr.borrow()).upgrade().deref()).flags.borrow_mut()) = rhs_0 + let rhs_0 = (((*ptr.borrow()).with(|__s: &Item| __s.flags) as i32) | (1 << 1)) as u8; + (*ptr.borrow()).with_mut(|__s: &mut Item| __s.flags = rhs_0) }; - assert!((((*(*(*ptr.borrow()).upgrade().deref()).flags.borrow()) as i32) == 3)); + assert!((((*ptr.borrow()).with(|__s: &Item| __s.flags) as i32) == 3)); { - let rhs_0 = (((*(*(*ptr.borrow()).upgrade().deref()).flags.borrow()) as i32) + let rhs_0 = (((*ptr.borrow()).with(|__s: &Item| __s.flags) as i32) & ((!(1 << 0) as u8) as i32)) as u8; - (*(*(*ptr.borrow()).upgrade().deref()).flags.borrow_mut()) = rhs_0 + (*ptr.borrow()).with_mut(|__s: &mut Item| __s.flags = rhs_0) }; - assert!((((*(*(*ptr.borrow()).upgrade().deref()).flags.borrow()) as i32) == 2)); + assert!((((*ptr.borrow()).with(|__s: &Item| __s.flags) as i32) == 2)); let bits: Value> = Rc::new(RefCell::new(Box::new([0_u8, 0_u8, 0_u8, 0_u8]))); { let rhs_0 = (((*bits.borrow())[((5) / 8) as usize] as i32) @@ -72,14 +60,14 @@ fn main_0() -> i32 { assert!((((*bits.borrow())[(0) as usize] as i32) == 32)); assert!((((*bits.borrow())[(1) as usize] as i32) == 32)); assert!((((*bits.borrow())[(2) as usize] as i32) == 0)); - if (((*(*(*ptr.borrow()).upgrade().deref()).flags.borrow()) as i32) != 0) { + if (((*ptr.borrow()).with(|__s: &Item| __s.flags) as i32) != 0) { { - let rhs_0 = (((*(*(*ptr.borrow()).upgrade().deref()).flags.borrow()) as i32) + let rhs_0 = (((*ptr.borrow()).with(|__s: &Item| __s.flags) as i32) & ((!(1 << 1) as u8) as i32)) as u8; - (*(*(*ptr.borrow()).upgrade().deref()).flags.borrow_mut()) = rhs_0 + (*ptr.borrow()).with_mut(|__s: &mut Item| __s.flags = rhs_0) }; } - assert!((((*(*(*ptr.borrow()).upgrade().deref()).flags.borrow()) as i32) == 0)); + assert!((((*ptr.borrow()).with(|__s: &Item| __s.flags) as i32) == 0)); return 0; } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/concepts.rs b/tests/unit/out/refcount/concepts.rs index efe5a35a52..4b15483386 100644 --- a/tests/unit/out/refcount/concepts.rs +++ b/tests/unit/out/refcount/concepts.rs @@ -11,7 +11,7 @@ const _: () = assert!( (::std::mem::size_of::() == 4_usize), "sizeof(int) == 4" ); -#[derive(Clone, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] pub struct Sized {} pub fn is_small_0() -> bool { return true; diff --git a/tests/unit/out/refcount/constexpr_function.rs b/tests/unit/out/refcount/constexpr_function.rs index f42be24c16..8f104e2488 100644 --- a/tests/unit/out/refcount/constexpr_function.rs +++ b/tests/unit/out/refcount/constexpr_function.rs @@ -25,65 +25,49 @@ pub fn half_3(x: f64) -> f64 { let x: Value = Rc::new(RefCell::new(x)); return ((*x.borrow()) / 2.0E+0); } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Flag { - pub v: Value, -} -impl Clone for Flag { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*self.v.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub v: i32, } impl ByteRepr for Flag { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.v.borrow()).to_bytes(&mut buf[0..4]); + self.v.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - v: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + v: ::from_bytes(&buf[0..4]), } } } pub fn use_4(f: Flag) -> i32 { let f: Value = Rc::new(RefCell::new(f)); assert!(({ FlagImpl::to_bool(&f.as_pointer(),) })); - return (*(*f.borrow()).v.borrow()); + return { (*f.borrow()).v }; } pub fn checked_5(x: i32) -> i32 { let x: Value = Rc::new(RefCell::new(x)); assert!(((*x.borrow()) > 0)); return ((*x.borrow()) + 1); } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct P { - pub v: Value, -} -impl Clone for P { - fn clone(&self) -> Self { - let __this: Value

= Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*self.v.borrow()))), - })); - let this: Ptr

= __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub v: i32, } impl ByteRepr for P { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.v.borrow()).to_bytes(&mut buf[0..4]); + self.v.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - v: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + v: ::from_bytes(&buf[0..4]), } } } @@ -101,26 +85,12 @@ fn main_0() -> i32 { assert!((({ checked_5(1,) }) == 2)); let c: Value = Rc::new(RefCell::new(({ checked_5(4) }))); assert!(((*c.borrow()) == 5)); - assert!( - (({ - use_4(Flag { - v: Rc::new(RefCell::new(2)), - }) - }) == 2) - ); - let u: Value = Rc::new(RefCell::new( - ({ - use_4(Flag { - v: Rc::new(RefCell::new(3)), - }) - }), - )); + assert!((({ use_4(Flag { v: 2 },) }) == 2)); + let u: Value = Rc::new(RefCell::new(({ use_4(Flag { v: 3 }) }))); assert!(((*u.borrow()) == 3)); let ptr: Value> = Rc::new(RefCell::new((arr.as_pointer() as Ptr))); assert!(!(*ptr.borrow()).is_null()); - let p: Value

= Rc::new(RefCell::new(P { - v: Rc::new(RefCell::new(9)), - })); + let p: Value

= Rc::new(RefCell::new(P { v: 9 })); assert!((({ PImpl::get(&p.as_pointer(),) }) == 9)); let k: Value = Rc::new(RefCell::new(({ scaled_2(4) }))); assert!(((*k.borrow()) == 4)); @@ -131,7 +101,7 @@ pub trait FlagImpl { } impl FlagImpl for Ptr { fn to_bool(&self) -> bool { - return ((*(*(*self).upgrade().deref()).v.borrow()) != 0); + return ((*self).with(|__s: &Flag| __s.v) != 0); } } pub trait PImpl { @@ -139,7 +109,7 @@ pub trait PImpl { } impl PImpl for Ptr

{ fn get(&self) -> i32 { - return (*(*(*self).upgrade().deref()).v.borrow()); + return (*self).with(|__s: &P| __s.v); } } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/constructor.rs b/tests/unit/out/refcount/constructor.rs index 048831344a..753f9c659e 100644 --- a/tests/unit/out/refcount/constructor.rs +++ b/tests/unit/out/refcount/constructor.rs @@ -9,15 +9,16 @@ use std::rc::{Rc, Weak}; thread_local!( pub static total_0: Value = Rc::new(RefCell::new(0)); ); -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct S { - pub v: Value, + #[offset(0)] + pub v: i32, } impl S { pub fn new(init: i32) -> Self { let init: Value = Rc::new(RefCell::new(init)); let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*init.borrow()))), + v: (*init.borrow()), })); let this: Ptr = __this.as_pointer(); ({ SImpl::mut_method(&this) }); @@ -25,6 +26,7 @@ impl S { Rc::try_unwrap(__this).ok().unwrap().into_inner() } } +<<<<<<< HEAD impl Clone for S { fn clone(&self) -> Self { let __this: Value = Rc::new(RefCell::new(Self { @@ -36,17 +38,35 @@ impl Clone for S { } impl ByteRepr for S {} #[derive(VaArg, FnPtrArg)] +======= +impl ByteRepr for S { + fn byte_size() -> usize { + 4 + } + fn to_bytes(&self, buf: &mut [u8]) { + self.v.to_bytes(&mut buf[0..4]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + v: ::from_bytes(&buf[0..4]), + } + } +} +#[derive(Clone, Record, VaArg, FnPtrArg)] +>>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub struct Point { - pub x: Value, - pub y: Value, + #[offset(0)] + pub x: i32, + #[offset(4)] + pub y: i32, } impl Point { pub fn new_1(x: i32, y: i32) -> Self { let x: Value = Rc::new(RefCell::new(x)); let y: Value = Rc::new(RefCell::new(y)); let __this: Value = Rc::new(RefCell::new(Self { - x: Rc::new(RefCell::new((*x.borrow()))), - y: Rc::new(RefCell::new((*y.borrow()))), + x: (*x.borrow()), + y: (*y.borrow()), })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() @@ -57,23 +77,13 @@ impl Point { ((*v.borrow()) + 1) }))); let this: Ptr = __this.as_pointer(); - (*(*this.upgrade().deref()).y.borrow_mut()) *= 10; + this.with_mut(|__s: &mut Point| __s.y *= 10); Rc::try_unwrap(__this).ok().unwrap().into_inner() } pub fn new_3() -> Self { let __this: Value = Rc::new(RefCell::new(Point::new_2({ 4 }))); let this: Ptr = __this.as_pointer(); - (*(*this.upgrade().deref()).x.borrow_mut()) += 100; - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } -} -impl Clone for Point { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - x: Rc::new(RefCell::new((*self.x.borrow()))), - y: Rc::new(RefCell::new((*self.y.borrow()))), - })); - let this: Ptr = __this.as_pointer(); + this.with_mut(|__s: &mut Point| __s.x += 100); Rc::try_unwrap(__this).ok().unwrap().into_inner() } } @@ -87,13 +97,13 @@ impl ByteRepr for Point { 8 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.x.borrow()).to_bytes(&mut buf[0..4]); - (*self.y.borrow()).to_bytes(&mut buf[4..8]); + self.x.to_bytes(&mut buf[0..4]); + self.y.to_bytes(&mut buf[4..8]); } fn from_bytes(buf: &[u8]) -> Self { Self { - x: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - y: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + x: ::from_bytes(&buf[0..4]), + y: ::from_bytes(&buf[4..8]), } } } @@ -105,16 +115,16 @@ fn main_0() -> i32 { { let s: Value = Rc::new(RefCell::new(S::new({ 3 }))); let _dtor_s = ScopedDestructor::new(&s, |__p| __p.destructor()); - assert!(((*(*s.borrow()).v.borrow()) == 4)); + assert!(({ (*s.borrow()).v } == 4)); assert!((total_0.with(|rc| *rc.borrow()) == 8)); } assert!((total_0.with(|rc| *rc.borrow()) == 18)); let p: Value = Rc::new(RefCell::new(Point::new_3())); - assert!(((*(*p.borrow()).x.borrow()) == 104)); - assert!(((*(*p.borrow()).y.borrow()) == 50)); + assert!(({ (*p.borrow()).x } == 104)); + assert!(({ (*p.borrow()).y } == 50)); let q: Value = Rc::new(RefCell::new(Point::new_2({ 7 }))); - assert!(((*(*q.borrow()).x.borrow()) == 7)); - assert!(((*(*q.borrow()).y.borrow()) == 80)); + assert!(({ (*q.borrow()).x } == 7)); + assert!(({ (*q.borrow()).y } == 80)); return 0; } pub trait SImpl { @@ -124,10 +134,10 @@ pub trait SImpl { } impl SImpl for Ptr { fn const_method(&self) -> i32 { - return ((*(*(*self).upgrade().deref()).v.borrow()) * 2); + return ((*self).with(|__s: &S| __s.v) * 2); } fn mut_method(&self) { - (*(*(*self).upgrade().deref()).v.borrow_mut()) += 1; + (*self).with_mut(|__s: &mut S| __s.v += 1); } fn destructor(&self) { ({ SImpl::mut_method(self) }); diff --git a/tests/unit/out/refcount/copy_assign.rs b/tests/unit/out/refcount/copy_assign.rs index 61d707eee6..a1f45f2453 100644 --- a/tests/unit/out/refcount/copy_assign.rs +++ b/tests/unit/out/refcount/copy_assign.rs @@ -9,26 +9,28 @@ use std::rc::{Rc, Weak}; thread_local!( pub static assigns_0: Value = Rc::new(RefCell::new(0)); ); -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Record, VaArg, FnPtrArg, Default)] pub struct Partial { - pub v: Value, - pub keep: Value, + #[offset(0)] + pub v: i32, + #[offset(4)] + pub keep: i32, } impl Partial { pub fn new(v: i32, keep: i32) -> Self { let v: Value = Rc::new(RefCell::new(v)); let keep: Value = Rc::new(RefCell::new(keep)); let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*v.borrow()))), - keep: Rc::new(RefCell::new((*keep.borrow()))), + v: (*v.borrow()), + keep: (*keep.borrow()), })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } pub fn copy_from(o: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*(*o.upgrade().deref()).v.borrow()))), - keep: Rc::new(RefCell::new((*(*o.upgrade().deref()).keep.borrow()))), + v: { o.with(|__s: &Partial| __s.v) }, + keep: { o.with(|__s: &Partial| __s.keep) }, })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() @@ -43,25 +45,34 @@ impl Clone for Partial { Partial::copy_from(__src.as_pointer()) } } +<<<<<<< HEAD impl ByteRepr for Partial {} #[derive(VaArg, FnPtrArg)] +======= +impl ByteRepr for Partial { + fn byte_size() -> usize { + 8 + } + fn to_bytes(&self, buf: &mut [u8]) { + self.v.to_bytes(&mut buf[0..4]); + self.keep.to_bytes(&mut buf[4..8]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + v: ::from_bytes(&buf[0..4]), + keep: ::from_bytes(&buf[4..8]), + } + } +} +#[derive(Clone, Record, VaArg, FnPtrArg)] +>>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub struct NonConstAssign { - pub mark: Value, + #[offset(0)] + pub mark: i32, } impl NonConstAssign { pub fn new() -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - mark: Rc::new(RefCell::new(0)), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } -} -impl Clone for NonConstAssign { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - mark: Rc::new(RefCell::new((*self.mark.borrow()))), - })); + let __this: Value = Rc::new(RefCell::new(Self { mark: 0 })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } @@ -71,25 +82,32 @@ impl Default for NonConstAssign { { NonConstAssign::new() } } } +<<<<<<< HEAD impl ByteRepr for NonConstAssign {} #[derive(VaArg, FnPtrArg)] +======= +impl ByteRepr for NonConstAssign { + fn byte_size() -> usize { + 4 + } + fn to_bytes(&self, buf: &mut [u8]) { + self.mark.to_bytes(&mut buf[0..4]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + mark: ::from_bytes(&buf[0..4]), + } + } +} +#[derive(Clone, Record, VaArg, FnPtrArg)] +>>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub struct RefQualified { - pub mark: Value, + #[offset(0)] + pub mark: i32, } impl RefQualified { pub fn new() -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - mark: Rc::new(RefCell::new(0)), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } -} -impl Clone for RefQualified { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - mark: Rc::new(RefCell::new((*self.mark.borrow()))), - })); + let __this: Value = Rc::new(RefCell::new(Self { mark: 0 })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } @@ -99,39 +117,60 @@ impl Default for RefQualified { { RefQualified::new() } } } +<<<<<<< HEAD impl ByteRepr for RefQualified {} #[derive(VaArg, FnPtrArg)] -pub struct Holder { - pub p: Value, - pub arr: Value>, -} -impl Clone for Holder { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - p: Rc::new(RefCell::new(Partial::copy_from({ self.p.as_pointer() }))), - arr: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 2, _>( - |__i: usize| { - Partial::copy_from({ (self.arr.as_pointer() as Ptr).offset(__i) }) - }, - )))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() +======= +impl ByteRepr for RefQualified { + fn byte_size() -> usize { + 4 + } + fn to_bytes(&self, buf: &mut [u8]) { + self.mark.to_bytes(&mut buf[0..4]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + mark: ::from_bytes(&buf[0..4]), + } } } +#[derive(Clone, Record, VaArg, FnPtrArg)] +>>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) +pub struct Holder { + #[offset(0)] + pub p: Partial, + #[offset(8)] + pub arr: Box<[Partial]>, +} impl Default for Holder { fn default() -> Self { Holder { - p: >::default(), - arr: Rc::new(RefCell::new( - (0..2) - .map(|_| ::default()) - .collect::>(), - )), + p: ::default(), + arr: (0..2) + .map(|_| ::default()) + .collect::>(), } } } +<<<<<<< HEAD impl ByteRepr for Holder {} +======= +impl ByteRepr for Holder { + fn byte_size() -> usize { + 24 + } + fn to_bytes(&self, buf: &mut [u8]) { + self.p.to_bytes(&mut buf[0..8]); + self.arr.to_bytes(&mut buf[8..24]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + p: ::from_bytes(&buf[0..8]), + arr: >::from_bytes(&buf[8..24]), + } + } +} +>>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub fn main() { __cpp2rust_init_globals(); std::process::exit(main_0()); @@ -141,7 +180,7 @@ fn main_0() -> i32 { let b: Value = Rc::new(RefCell::new(Partial::new({ 2 }, { 200 }))); let c: Value = Rc::new(RefCell::new(Partial::new({ 3 }, { 300 }))); ({ PartialImpl::copy_assign(&a.as_pointer(), b.as_pointer()) }); - assert!(((*(*a.borrow()).v.borrow()) == 2) && ((*(*a.borrow()).keep.borrow()) == 100)); + assert!(({ (*a.borrow()).v } == 2) && ({ (*a.borrow()).keep } == 100)); assert!((assigns_0.with(|rc| *rc.borrow()) == 1)); ({ PartialImpl::copy_assign( @@ -149,7 +188,7 @@ fn main_0() -> i32 { ({ PartialImpl::copy_assign(&a.as_pointer(), b.as_pointer()) }), ) }); - assert!(((*(*c.borrow()).v.borrow()) == 2) && ((*(*c.borrow()).keep.borrow()) == 300)); + assert!(({ (*c.borrow()).v } == 2) && ({ (*c.borrow()).keep } == 300)); assert!((assigns_0.with(|rc| *rc.borrow()) == 3)); ({ let _o: Ptr = a.as_pointer(); @@ -160,42 +199,36 @@ fn main_0() -> i32 { let _o: Value = Rc::new(RefCell::new(Partial::new({ 9 }, { 900 }))); PartialImpl::copy_assign(&a.as_pointer(), _o.as_pointer()) }); - assert!(((*(*a.borrow()).v.borrow()) == 9) && ((*(*a.borrow()).keep.borrow()) == 100)); + assert!(({ (*a.borrow()).v } == 9) && ({ (*a.borrow()).keep } == 100)); assert!((assigns_0.with(|rc| *rc.borrow()) == 4)); let ra: Ptr = a.as_pointer(); ({ let _o: Ptr = c.as_pointer(); PartialImpl::copy_assign(&ra, _o) }); - assert!(((*(*a.borrow()).v.borrow()) == 2)); + assert!(({ (*a.borrow()).v } == 2)); let pa: Value> = Rc::new(RefCell::new((a.as_pointer()))); ({ let _o: Ptr = b.as_pointer(); PartialImpl::copy_assign(&(*pa.borrow()), _o) }); - assert!(((*(*a.borrow()).v.borrow()) == 2)); + assert!(({ (*a.borrow()).v } == 2)); assert!((assigns_0.with(|rc| *rc.borrow()) == 6)); let h: Value = Rc::new(RefCell::new(Holder { - p: Rc::new(RefCell::new(Partial::new({ 4 }, { 40 }))), - arr: Rc::new(RefCell::new(Box::new([ - Partial::new({ 5 }, { 50 }), - Partial::new({ 6 }, { 60 }), - ]))), + p: Partial::new({ 4 }, { 40 }), + arr: Box::new([Partial::new({ 5 }, { 50 }), Partial::new({ 6 }, { 60 })]), })); - ({ PartialImpl::copy_assign(&(*h.borrow()).p.as_pointer(), b.as_pointer()) }); + ({ PartialImpl::copy_assign(&{ field_ptr!(h, p) }, b.as_pointer()) }); ({ PartialImpl::copy_assign( - &((*h.borrow()).arr.as_pointer() as Ptr).offset(1), + &{ (field_ptr!(h, arr) as Ptr).offset(1) }, c.as_pointer(), ) }); + assert!(({ (*h.borrow()).p.v } == 2) && ({ (*h.borrow()).p.keep } == 40)); assert!( - ((*(*(*h.borrow()).p.borrow()).v.borrow()) == 2) - && ((*(*(*h.borrow()).p.borrow()).keep.borrow()) == 40) - ); - assert!( - ((*(*(*h.borrow()).arr.borrow())[(1) as usize].v.borrow()) == 2) - && ((*(*(*h.borrow()).arr.borrow())[(1) as usize].keep.borrow()) == 60) + ({ (*h.borrow()).arr[(1) as usize].v } == 2) + && ({ (*h.borrow()).arr[(1) as usize].keep } == 60) ); assert!((assigns_0.with(|rc| *rc.borrow()) == 8)); let n: Value = Rc::new(RefCell::new(NonConstAssign::new())); @@ -204,12 +237,12 @@ fn main_0() -> i32 { let cn: Value = Rc::new(RefCell::new(NonConstAssign::new())); ({ NonConstAssignImpl::operator_assign_2(&n1.as_pointer(), n.as_pointer()) }); ({ NonConstAssignImpl::operator_assign_3(&n2.as_pointer(), cn.as_pointer()) }); - assert!(((*(*n1.borrow()).mark.borrow()) == 1)); - assert!(((*(*n2.borrow()).mark.borrow()) == 10)); + assert!(({ (*n1.borrow()).mark } == 1)); + assert!(({ (*n2.borrow()).mark } == 10)); let r: Value = Rc::new(RefCell::new(RefQualified::new())); let r1: Value = Rc::new(RefCell::new(RefQualified::new())); ({ RefQualifiedImpl::copy_assign(&r1.as_pointer(), r.as_pointer()) }); - assert!(((*(*r1.borrow()).mark.borrow()) == 1)); + assert!(({ (*r1.borrow()).mark } == 1)); return 0; } pub trait NonConstAssignImpl { @@ -218,13 +251,13 @@ pub trait NonConstAssignImpl { } impl NonConstAssignImpl for Ptr { fn operator_assign_2(&self, o: Ptr) -> Ptr { - let __rhs = ((*(*o.upgrade().deref()).mark.borrow()) + 1); - (*(*(*self).upgrade().deref()).mark.borrow_mut()) = __rhs; + let __rhs = (o.with(|__s: &NonConstAssign| __s.mark) + 1); + (*self).with_mut(|__s: &mut NonConstAssign| __s.mark = __rhs); return (*self).clone(); } fn operator_assign_3(&self, o: Ptr) -> Ptr { - let __rhs = ((*(*o.upgrade().deref()).mark.borrow()) + 10); - (*(*(*self).upgrade().deref()).mark.borrow_mut()) = __rhs; + let __rhs = (o.with(|__s: &NonConstAssign| __s.mark) + 10); + (*self).with_mut(|__s: &mut NonConstAssign| __s.mark = __rhs); return (*self).clone(); } } @@ -236,8 +269,8 @@ impl PartialImpl for Ptr { if ((*self) == (o)) { return (*self).clone(); } - let __rhs = (*(*o.upgrade().deref()).v.borrow()); - (*(*(*self).upgrade().deref()).v.borrow_mut()) = __rhs; + let __rhs = o.with(|__s: &Partial| __s.v); + (*self).with_mut(|__s: &mut Partial| __s.v = __rhs); (*assigns_0.with(Value::clone).borrow_mut()).prefix_inc(); return (*self).clone(); } @@ -247,8 +280,8 @@ pub trait RefQualifiedImpl { } impl RefQualifiedImpl for Ptr { fn copy_assign(&self, o: Ptr) -> Ptr { - let __rhs = ((*(*o.upgrade().deref()).mark.borrow()) + 1); - (*(*(*self).upgrade().deref()).mark.borrow_mut()) = __rhs; + let __rhs = (o.with(|__s: &RefQualified| __s.mark) + 1); + (*self).with_mut(|__s: &mut RefQualified| __s.mark = __rhs); return (*self).clone(); } } diff --git a/tests/unit/out/refcount/copy_ctor.rs b/tests/unit/out/refcount/copy_ctor.rs index e9a3b8fddc..440b207902 100644 --- a/tests/unit/out/refcount/copy_ctor.rs +++ b/tests/unit/out/refcount/copy_ctor.rs @@ -9,22 +9,21 @@ use std::rc::{Rc, Weak}; thread_local!( pub static copies_0: Value = Rc::new(RefCell::new(0)); ); -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Record, VaArg, FnPtrArg, Default)] pub struct Counted { - pub v: Value, + #[offset(0)] + pub v: i32, } impl Counted { pub fn new(v: i32) -> Self { let v: Value = Rc::new(RefCell::new(v)); - let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*v.borrow()))), - })); + let __this: Value = Rc::new(RefCell::new(Self { v: (*v.borrow()) })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } pub fn copy_from(o: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*(*o.upgrade().deref()).v.borrow()))), + v: { o.with(|__s: &Counted| __s.v) }, })); let this: Ptr = __this.as_pointer(); (*copies_0.with(Value::clone).borrow_mut()).prefix_inc(); @@ -37,29 +36,45 @@ impl Clone for Counted { Counted::copy_from(__src.as_pointer()) } } +<<<<<<< HEAD impl ByteRepr for Counted {} #[derive(VaArg, FnPtrArg)] +======= +impl ByteRepr for Counted { + fn byte_size() -> usize { + 4 + } + fn to_bytes(&self, buf: &mut [u8]) { + self.v.to_bytes(&mut buf[0..4]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + v: ::from_bytes(&buf[0..4]), + } + } +} +#[derive(Record, VaArg, FnPtrArg)] +>>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub struct NonConst { - pub mark: Value, + #[offset(0)] + pub mark: i32, } impl NonConst { pub fn new() -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - mark: Rc::new(RefCell::new(0)), - })); + let __this: Value = Rc::new(RefCell::new(Self { mark: 0 })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } pub fn new_1(o: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - mark: Rc::new(RefCell::new(((*(*o.upgrade().deref()).mark.borrow()) + 1))), + mark: { (o.with(|__s: &NonConst| __s.mark) + 1) }, })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } pub fn new_2(o: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - mark: Rc::new(RefCell::new(((*(*o.upgrade().deref()).mark.borrow()) + 10))), + mark: { (o.with(|__s: &NonConst| __s.mark) + 10) }, })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() @@ -78,24 +93,38 @@ impl Default for NonConst { { NonConst::new() } } } +<<<<<<< HEAD impl ByteRepr for NonConst {} #[derive(VaArg, FnPtrArg, Default)] +======= +impl ByteRepr for NonConst { + fn byte_size() -> usize { + 4 + } + fn to_bytes(&self, buf: &mut [u8]) { + self.mark.to_bytes(&mut buf[0..4]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + mark: ::from_bytes(&buf[0..4]), + } + } +} +#[derive(Record, VaArg, FnPtrArg, Default)] +>>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub struct Ignored { - pub v: Value, + #[offset(0)] + pub v: i32, } impl Ignored { pub fn new(v: i32) -> Self { let v: Value = Rc::new(RefCell::new(v)); - let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*v.borrow()))), - })); + let __this: Value = Rc::new(RefCell::new(Self { v: (*v.borrow()) })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } pub fn copy_from(_a0: Ptr) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new(-1_i32)), - })); + let __this: Value = Rc::new(RefCell::new(Self { v: -1_i32 })); let this: Ptr = __this.as_pointer(); (*copies_0.with(Value::clone).borrow_mut()).prefix_inc(); Rc::try_unwrap(__this).ok().unwrap().into_inner() @@ -107,42 +136,63 @@ impl Clone for Ignored { Ignored::copy_from(__src.as_pointer()) } } +<<<<<<< HEAD impl ByteRepr for Ignored {} #[derive(VaArg, FnPtrArg)] -pub struct Holder { - pub c: Value, - pub arr: Value>, -} -impl Clone for Holder { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - c: Rc::new(RefCell::new(Counted::copy_from({ self.c.as_pointer() }))), - arr: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 2, _>( - |__i: usize| { - Counted::copy_from({ (self.arr.as_pointer() as Ptr).offset(__i) }) - }, - )))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() +======= +impl ByteRepr for Ignored { + fn byte_size() -> usize { + 4 + } + fn to_bytes(&self, buf: &mut [u8]) { + self.v.to_bytes(&mut buf[0..4]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + v: ::from_bytes(&buf[0..4]), + } } } +#[derive(Clone, Record, VaArg, FnPtrArg)] +>>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) +pub struct Holder { + #[offset(0)] + pub c: Counted, + #[offset(4)] + pub arr: Box<[Counted]>, +} impl Default for Holder { fn default() -> Self { Holder { - c: >::default(), - arr: Rc::new(RefCell::new( - (0..2) - .map(|_| ::default()) - .collect::>(), - )), + c: ::default(), + arr: (0..2) + .map(|_| ::default()) + .collect::>(), } } } +<<<<<<< HEAD impl ByteRepr for Holder {} +======= +impl ByteRepr for Holder { + fn byte_size() -> usize { + 12 + } + fn to_bytes(&self, buf: &mut [u8]) { + self.c.to_bytes(&mut buf[0..4]); + self.arr.to_bytes(&mut buf[4..12]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + c: ::from_bytes(&buf[0..4]), + arr: >::from_bytes(&buf[4..12]), + } + } +} +>>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub fn by_value_1(c: Counted) -> i32 { let c: Value = Rc::new(RefCell::new(c)); - return (*(*c.borrow()).v.borrow()); + return { (*c.borrow()).v }; } pub fn make_2(v: i32) -> Counted { let v: Value = Rc::new(RefCell::new(v)); @@ -160,33 +210,28 @@ fn main_0() -> i32 { let d: Value = Rc::new(RefCell::new(Counted::copy_from({ a.as_pointer() }))); assert!((copies_0.with(|rc| *rc.borrow()) == 3)); assert!( - (((*(*b.borrow()).v.borrow()) == 1) && ((*(*c.borrow()).v.borrow()) == 1)) - && ((*(*d.borrow()).v.borrow()) == 1) + (({ (*b.borrow()).v } == 1) && ({ (*c.borrow()).v } == 1)) && ({ (*d.borrow()).v } == 1) ); assert!((({ by_value_1(Counted::copy_from({ a.as_pointer() },),) }) == 1)); assert!((copies_0.with(|rc| *rc.borrow()) == 4)); let e: Value = Rc::new(RefCell::new(({ make_2(5) }))); - assert!(((*(*e.borrow()).v.borrow()) == 5)); + assert!(({ (*e.borrow()).v } == 5)); assert!((copies_0.with(|rc| *rc.borrow()) == 5)); let f: Value = Rc::new(RefCell::new(Counted::new({ 6 }))); - assert!(((*(*f.borrow()).v.borrow()) == 6)); + assert!(({ (*f.borrow()).v } == 6)); assert!((copies_0.with(|rc| *rc.borrow()) == 5)); let g: Value = Rc::new(RefCell::new(Counted::new({ 7 }))); let h: Value = Rc::new(RefCell::new(Counted::copy_from({ g.as_pointer() }))); - assert!(((*(*h.borrow()).v.borrow()) == 7)); + assert!(({ (*h.borrow()).v } == 7)); assert!((copies_0.with(|rc| *rc.borrow()) == 6)); let hold: Value = Rc::new(RefCell::new(Holder { - c: Rc::new(RefCell::new(Counted::new({ 8 }))), - arr: Rc::new(RefCell::new(Box::new([ - Counted::new({ 9 }), - Counted::new({ 10 }), - ]))), + c: Counted::new({ 8 }), + arr: Box::new([Counted::new({ 9 }), Counted::new({ 10 })]), })); let hold2: Value = Rc::new(RefCell::new((*hold.borrow()).clone())); assert!( - (((*(*(*hold2.borrow()).c.borrow()).v.borrow()) == 8) - && ((*(*(*hold2.borrow()).arr.borrow())[(0) as usize].v.borrow()) == 9)) - && ((*(*(*hold2.borrow()).arr.borrow())[(1) as usize].v.borrow()) == 10) + (({ (*hold2.borrow()).c.v } == 8) && ({ (*hold2.borrow()).arr[(0) as usize].v } == 9)) + && ({ (*hold2.borrow()).arr[(1) as usize].v } == 10) ); assert!((copies_0.with(|rc| *rc.borrow()) == 9)); let vec_: Value> = Rc::new(RefCell::new(Vec::new())); @@ -195,25 +240,22 @@ fn main_0() -> i32 { (*vec_.borrow_mut()).push(a0_clone) }; assert!( - ((*(*(vec_.as_pointer() as Ptr) + ((vec_.as_pointer() as Ptr) .offset(0_usize) - .upgrade() - .deref()) - .v - .borrow()) + .with(|__s: &Counted| __s.v) == 1) ); assert!((copies_0.with(|rc| *rc.borrow()) == 10)); let i1: Value = Rc::new(RefCell::new(Ignored::new({ 1 }))); let i2: Value = Rc::new(RefCell::new(Ignored::copy_from({ i1.as_pointer() }))); - assert!(((*(*i1.borrow()).v.borrow()) == 1) && ((*(*i2.borrow()).v.borrow()) == -1_i32)); + assert!(({ (*i1.borrow()).v } == 1) && ({ (*i2.borrow()).v } == -1_i32)); assert!((copies_0.with(|rc| *rc.borrow()) == 11)); let n: Value = Rc::new(RefCell::new(NonConst::new())); let n1: Value = Rc::new(RefCell::new(NonConst::new_1({ n.as_pointer() }))); let cn: Value = Rc::new(RefCell::new(NonConst::new())); let n2: Value = Rc::new(RefCell::new(NonConst::new_2({ cn.as_pointer() }))); - assert!(((*(*n1.borrow()).mark.borrow()) == 1)); - assert!(((*(*n2.borrow()).mark.borrow()) == 10)); + assert!(({ (*n1.borrow()).mark } == 1)); + assert!(({ (*n2.borrow()).mark } == 10)); return 0; } pub fn __cpp2rust_init_globals() { diff --git a/tests/unit/out/refcount/copy_move_defaulted.rs b/tests/unit/out/refcount/copy_move_defaulted.rs index 35401d4f4e..adb4f42399 100644 --- a/tests/unit/out/refcount/copy_move_defaulted.rs +++ b/tests/unit/out/refcount/copy_move_defaulted.rs @@ -6,60 +6,42 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Inner { - pub x: Value, -} -impl Clone for Inner { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - x: Rc::new(RefCell::new((*self.x.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub x: i32, } impl ByteRepr for Inner { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.x.borrow()).to_bytes(&mut buf[0..4]); + self.x.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - x: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + x: ::from_bytes(&buf[0..4]), } } } -#[derive(VaArg, FnPtrArg)] +#[derive(Clone, Record, VaArg, FnPtrArg)] pub struct Explicit { - pub v: Value, - pub inner: Value, - pub arr: Value>, + #[offset(0)] + pub v: i32, + #[offset(4)] + pub inner: Inner, + #[offset(8)] + pub arr: Box<[i32]>, } impl Explicit { pub fn new(v: i32) -> Self { let v: Value = Rc::new(RefCell::new(v)); let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*v.borrow()))), - inner: Rc::new(RefCell::new(Inner { - x: Rc::new(RefCell::new(((*v.borrow()) * 10))), - })), - arr: Rc::new(RefCell::new(Box::new([(*v.borrow()), ((*v.borrow()) + 1)]))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } -} -impl Clone for Explicit { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*self.v.borrow()))), - inner: Rc::new(RefCell::new((*self.inner.borrow()).clone())), - arr: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 2, _>( - |__i: usize| (*self.arr.borrow())[(__i) as usize], - )))), + v: (*v.borrow()), + inner: Inner { + x: ((*v.borrow()) * 10), + }, + arr: Box::new([(*v.borrow()), ((*v.borrow()) + 1)]), })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() @@ -68,38 +50,49 @@ impl Clone for Explicit { impl Default for Explicit { fn default() -> Self { Explicit { - v: Rc::new(RefCell::new(0_i32)), - inner: >::default(), - arr: Rc::new(RefCell::new((0..2).map(|_| 0_i32).collect::>())), + v: 0_i32, + inner: ::default(), + arr: (0..2).map(|_| 0_i32).collect::>(), } } } +<<<<<<< HEAD impl ByteRepr for Explicit {} #[derive(VaArg, FnPtrArg)] -pub struct Implicit { - pub v: Value, - pub inner: Value, - pub arr: Value>, -} -impl Clone for Implicit { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*self.v.borrow()))), - inner: Rc::new(RefCell::new((*self.inner.borrow()).clone())), - arr: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 2, _>( - |__i: usize| (*self.arr.borrow())[(__i) as usize], - )))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() +======= +impl ByteRepr for Explicit { + fn byte_size() -> usize { + 16 + } + fn to_bytes(&self, buf: &mut [u8]) { + self.v.to_bytes(&mut buf[0..4]); + self.inner.to_bytes(&mut buf[4..8]); + self.arr.to_bytes(&mut buf[8..16]); } + fn from_bytes(buf: &[u8]) -> Self { + Self { + v: ::from_bytes(&buf[0..4]), + inner: ::from_bytes(&buf[4..8]), + arr: >::from_bytes(&buf[8..16]), + } + } +} +#[derive(Clone, Record, VaArg, FnPtrArg)] +>>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) +pub struct Implicit { + #[offset(0)] + pub v: i32, + #[offset(4)] + pub inner: Inner, + #[offset(8)] + pub arr: Box<[i32]>, } impl Default for Implicit { fn default() -> Self { Implicit { - v: Rc::new(RefCell::new(0_i32)), - inner: >::default(), - arr: Rc::new(RefCell::new((0..2).map(|_| 0_i32).collect::>())), + v: 0_i32, + inner: ::default(), + arr: (0..2).map(|_| 0_i32).collect::>(), } } } @@ -108,73 +101,79 @@ impl ByteRepr for Implicit { 16 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.v.borrow()).to_bytes(&mut buf[0..4]); - (*self.inner.borrow()).to_bytes(&mut buf[4..8]); - (*self.arr.borrow()).to_bytes(&mut buf[8..16]); + self.v.to_bytes(&mut buf[0..4]); + self.inner.to_bytes(&mut buf[4..8]); + self.arr.to_bytes(&mut buf[8..16]); } fn from_bytes(buf: &[u8]) -> Self { Self { - v: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - inner: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), - arr: Rc::new(RefCell::new(>::from_bytes(&buf[8..16]))), + v: ::from_bytes(&buf[0..4]), + inner: ::from_bytes(&buf[4..8]), + arr: >::from_bytes(&buf[8..16]), } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct DefaultCopyUserMove { - pub v: Value, + #[offset(0)] + pub v: i32, } impl DefaultCopyUserMove { pub fn new(v: i32) -> Self { let v: Value = Rc::new(RefCell::new(v)); - let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*v.borrow()))), - })); + let __this: Value = Rc::new(RefCell::new(Self { v: (*v.borrow()) })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } pub fn move_from(o: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*(*o.upgrade().deref()).v.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - (*(*o.upgrade().deref()).v.borrow_mut()) = 0; - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } -} -impl Clone for DefaultCopyUserMove { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*self.v.borrow()))), + v: { o.with(|__s: &DefaultCopyUserMove| __s.v) }, })); let this: Ptr = __this.as_pointer(); + o.with_mut(|__s: &mut DefaultCopyUserMove| __s.v = 0); Rc::try_unwrap(__this).ok().unwrap().into_inner() } } +<<<<<<< HEAD impl ByteRepr for DefaultCopyUserMove {} #[derive(VaArg, FnPtrArg, Default)] +======= +impl ByteRepr for DefaultCopyUserMove { + fn byte_size() -> usize { + 4 + } + fn to_bytes(&self, buf: &mut [u8]) { + self.v.to_bytes(&mut buf[0..4]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + v: ::from_bytes(&buf[0..4]), + } + } +} +#[derive(Record, VaArg, FnPtrArg, Default)] +>>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub struct UserCopyDefaultMove { - pub v: Value, + #[offset(0)] + pub v: i32, } impl UserCopyDefaultMove { pub fn new(v: i32) -> Self { let v: Value = Rc::new(RefCell::new(v)); - let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*v.borrow()))), - })); + let __this: Value = Rc::new(RefCell::new(Self { v: (*v.borrow()) })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } pub fn copy_from(o: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new(((*(*o.upgrade().deref()).v.borrow()) + 100))), + v: { (o.with(|__s: &UserCopyDefaultMove| __s.v) + 100) }, })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } pub fn move_from(_a0: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*(*_a0.upgrade().deref()).v.borrow()))), + v: { _a0.with(|__s: &UserCopyDefaultMove| __s.v) }, })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() @@ -187,13 +186,34 @@ impl Clone for UserCopyDefaultMove { UserCopyDefaultMove::copy_from(__src.as_pointer()) } } +<<<<<<< HEAD impl ByteRepr for UserCopyDefaultMove {} #[derive()] +======= +impl ByteRepr for UserCopyDefaultMove { + fn byte_size() -> usize { + 4 + } + fn to_bytes(&self, buf: &mut [u8]) { + self.v.to_bytes(&mut buf[0..4]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + v: ::from_bytes(&buf[0..4]), + } + } +} +#[derive(Record)] +>>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub struct Buffer { + #[offset(0)] pub data: Value>, + #[offset(24)] pub rows: Value>>>, - pub n: Value, - pub arr: Value>, + #[offset(48)] + pub n: i32, + #[offset(52)] + pub arr: Box<[i32]>, } impl Buffer { pub fn new(n: i32) -> Self { @@ -204,31 +224,33 @@ impl Buffer { ((*n.borrow()) as usize) as usize ])), rows: Rc::new(RefCell::new(Vec::new())), - n: Rc::new(RefCell::new((*n.borrow()))), - arr: Rc::new(RefCell::new(Box::new([(*n.borrow()), ((*n.borrow()) + 1)]))), + n: (*n.borrow()), + arr: Box::new([(*n.borrow()), ((*n.borrow()) + 1)]), })); let this: Ptr = __this.as_pointer(); - ((*this.upgrade().deref()).rows.as_pointer() as Ptr>>>).with_mut( - |__v: &mut Vec>>| { - __v.push(Rc::new(RefCell::new( - (*(*this.upgrade().deref()).data.borrow()).clone(), - ))) - }, - ); + { + let __a1 = (*this.with(|__s: &Buffer| __s.data.clone()).borrow()).clone(); + (this.with(|__s: &Buffer| __s.rows.as_pointer()) as Ptr>>>) + .with_mut(|__v: &mut Vec>>| __v.push(Rc::new(RefCell::new(__a1)))) + }; Rc::try_unwrap(__this).ok().unwrap().into_inner() } pub fn move_from(_a0: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - data: Rc::new(RefCell::new(std::mem::take( - &mut (*(*_a0.upgrade().deref()).data.borrow_mut()), - ))), - rows: Rc::new(RefCell::new(std::mem::take( - &mut (*(*_a0.upgrade().deref()).rows.borrow_mut()), - ))), - n: Rc::new(RefCell::new((*(*_a0.upgrade().deref()).n.borrow()))), - arr: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 2, _>( - |__i: usize| (*(*_a0.upgrade().deref()).arr.borrow())[(__i) as usize], - )))), + data: { + Rc::new(RefCell::new(_a0.with(|__s: &Buffer| { + std::mem::take(&mut (*__s.data.borrow_mut())) + }))) + }, + rows: { + Rc::new(RefCell::new(_a0.with(|__s: &Buffer| { + std::mem::take(&mut (*__s.rows.borrow_mut())) + }))) + }, + n: { _a0.with(|__s: &Buffer| __s.n) }, + arr: Box::new(std::array::from_fn::<_, 2, _>(|__i: usize| { + (*_a0.upgrade().deref()).arr[(__i) as usize] + })), })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() @@ -239,32 +261,59 @@ impl Default for Buffer { Buffer { data: Rc::new(RefCell::new(Default::default())), rows: Rc::new(RefCell::new(Vec::new())), - n: Rc::new(RefCell::new(0_i32)), - arr: Rc::new(RefCell::new((0..2).map(|_| 0_i32).collect::>())), + n: 0_i32, + arr: (0..2).map(|_| 0_i32).collect::>(), } } } +<<<<<<< HEAD impl ByteRepr for Buffer {} #[derive()] +======= +impl ByteRepr for Buffer { + fn byte_size() -> usize { + 64 + } + fn to_bytes(&self, buf: &mut [u8]) { + self.data.to_bytes(&mut buf[0..24]); + self.rows.to_bytes(&mut buf[24..48]); + self.n.to_bytes(&mut buf[48..52]); + self.arr.to_bytes(&mut buf[52..60]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + data: >>::from_bytes(&buf[0..24]), + rows: >>>>::from_bytes(&buf[24..48]), + n: ::from_bytes(&buf[48..52]), + arr: >::from_bytes(&buf[52..60]), + } + } +} +#[derive(Record)] +>>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub struct Owner { + #[offset(0)] pub data: Value>, - pub n: Value, - pub arr: Value>, - pub p: Value>>, + #[offset(24)] + pub n: i32, + #[offset(28)] + pub arr: Box<[i32]>, + #[offset(40)] + pub p: Option>, } impl Owner { pub fn move_from(_a0: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - data: Rc::new(RefCell::new(std::mem::take( - &mut (*(*_a0.upgrade().deref()).data.borrow_mut()), - ))), - n: Rc::new(RefCell::new((*(*_a0.upgrade().deref()).n.borrow()))), - arr: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 2, _>( - |__i: usize| (*(*_a0.upgrade().deref()).arr.borrow())[(__i) as usize], - )))), - p: Rc::new(RefCell::new( - (*(*_a0.upgrade().deref()).p.borrow_mut()).take(), - )), + data: { + Rc::new(RefCell::new(_a0.with(|__s: &Owner| { + std::mem::take(&mut (*__s.data.borrow_mut())) + }))) + }, + n: { _a0.with(|__s: &Owner| __s.n) }, + arr: Box::new(std::array::from_fn::<_, 2, _>(|__i: usize| { + (*_a0.upgrade().deref()).arr[(__i) as usize] + })), + p: { _a0.with_mut(|__s: &mut Owner| __s.p.take()) }, })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() @@ -274,60 +323,100 @@ impl Default for Owner { fn default() -> Self { Owner { data: Rc::new(RefCell::new(Default::default())), - n: Rc::new(RefCell::new(0_i32)), - arr: Rc::new(RefCell::new((0..2).map(|_| 0_i32).collect::>())), - p: Rc::new(RefCell::new(None)), + n: 0_i32, + arr: (0..2).map(|_| 0_i32).collect::>(), + p: None, } } } +<<<<<<< HEAD impl ByteRepr for Owner {} #[derive(Default)] +======= +impl ByteRepr for Owner { + fn byte_size() -> usize { + 48 + } + fn to_bytes(&self, buf: &mut [u8]) { + self.data.to_bytes(&mut buf[0..24]); + self.n.to_bytes(&mut buf[24..28]); + self.arr.to_bytes(&mut buf[28..36]); + self.p.to_bytes(&mut buf[40..48]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + data: >>::from_bytes(&buf[0..24]), + n: ::from_bytes(&buf[24..28]), + arr: >::from_bytes(&buf[28..36]), + p: >>::from_bytes(&buf[40..48]), + } + } +} +#[derive(Record, Default)] +>>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub struct Holder { - pub inner: Value, - pub e: Value, - pub p: Value>>, + #[offset(0)] + pub inner: Inner, + #[offset(4)] + pub e: Explicit, + #[offset(24)] + pub p: Option>, } impl Holder { pub fn new(v: i32) -> Self { let v: Value = Rc::new(RefCell::new(v)); let __this: Value = Rc::new(RefCell::new(Self { - inner: Rc::new(RefCell::new(Inner { - x: Rc::new(RefCell::new((*v.borrow()))), - })), - e: Rc::new(RefCell::new(Explicit::new({ (*v.borrow()) }))), - p: Rc::new(RefCell::new(None)), + inner: Inner { x: (*v.borrow()) }, + e: Explicit::new({ (*v.borrow()) }), + p: None, })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } pub fn move_from(_a0: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - inner: Rc::new(RefCell::new( - (*(*_a0.upgrade().deref()).inner.borrow()).clone(), - )), - e: Rc::new(RefCell::new((*(*_a0.upgrade().deref()).e.borrow()).clone())), - p: Rc::new(RefCell::new( - (*(*_a0.upgrade().deref()).p.borrow_mut()).take(), - )), + inner: { _a0.with(|__s: &Holder| (__s.inner).clone()) }, + e: { _a0.with(|__s: &Holder| (__s.e).clone()) }, + p: { _a0.with_mut(|__s: &mut Holder| __s.p.take()) }, })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } } +<<<<<<< HEAD impl ByteRepr for Holder {} +======= +impl ByteRepr for Holder { + fn byte_size() -> usize { + 32 + } + fn to_bytes(&self, buf: &mut [u8]) { + self.inner.to_bytes(&mut buf[0..4]); + self.e.to_bytes(&mut buf[4..20]); + self.p.to_bytes(&mut buf[24..32]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + inner: ::from_bytes(&buf[0..4]), + e: ::from_bytes(&buf[4..20]), + p: >>::from_bytes(&buf[24..32]), + } + } +} +>>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub fn same_0(a: Ptr, b: Ptr) -> bool { return ((({ - let _lhs = (*(*a.upgrade().deref()).v.borrow()); - _lhs == (*(*b.upgrade().deref()).v.borrow()) + let _lhs = a.with(|__s: &Explicit| __s.v); + _lhs == b.with(|__s: &Explicit| __s.v) }) && ({ - let _lhs = (*(*(*a.upgrade().deref()).inner.borrow()).x.borrow()); - _lhs == (*(*(*b.upgrade().deref()).inner.borrow()).x.borrow()) + let _lhs = a.with(|__s: &Explicit| __s.inner.x); + _lhs == b.with(|__s: &Explicit| __s.inner.x) })) && ({ - let _lhs = (*(*a.upgrade().deref()).arr.borrow())[(0) as usize]; - _lhs == (*(*b.upgrade().deref()).arr.borrow())[(0) as usize] + let _lhs = a.with(|__s: &Explicit| __s.arr[(0) as usize]); + _lhs == b.with(|__s: &Explicit| __s.arr[(0) as usize]) })) && ({ - let _lhs = (*(*a.upgrade().deref()).arr.borrow())[(1) as usize]; - _lhs == (*(*b.upgrade().deref()).arr.borrow())[(1) as usize] + let _lhs = a.with(|__s: &Explicit| __s.arr[(1) as usize]); + _lhs == b.with(|__s: &Explicit| __s.arr[(1) as usize]) }); } pub fn main() { @@ -369,32 +458,26 @@ fn main_0() -> i32 { && ({ same_0(e.as_pointer(), f.as_pointer(),) }) ); let i: Value = Rc::new(RefCell::new(Implicit { - v: Rc::new(RefCell::new(5)), - inner: Rc::new(RefCell::new(Inner { - x: Rc::new(RefCell::new(50)), - })), - arr: Rc::new(RefCell::new(Box::new([5, 6]))), + v: 5, + inner: Inner { x: 50 }, + arr: Box::new([5, 6]), })); let j: Value = Rc::new(RefCell::new((*i.borrow()).clone())); let k: Value = Rc::new(RefCell::new((*i.borrow()).clone())); assert!( - (((*(*j.borrow()).v.borrow()) == 5) - && ((*(*(*j.borrow()).inner.borrow()).x.borrow()) == 50)) - && ((*(*j.borrow()).arr.borrow())[(1) as usize] == 6) + (({ (*j.borrow()).v } == 5) && ({ (*j.borrow()).inner.x } == 50)) + && ((*j.borrow()).arr[(1) as usize] == 6) ); - assert!(((*(*i.borrow()).v.borrow()) == 5) && ((*(*k.borrow()).v.borrow()) == 5)); + assert!(({ (*i.borrow()).v } == 5) && ({ (*k.borrow()).v } == 5)); let l: Value = Rc::new(RefCell::new(Implicit { - v: Rc::new(RefCell::new(0)), - inner: Rc::new(RefCell::new(Inner { - x: Rc::new(RefCell::new(0)), - })), - arr: Rc::new(RefCell::new(Box::new([0, 0]))), + v: 0, + inner: Inner { x: 0 }, + arr: Box::new([0, 0]), })); (*l.borrow_mut()) = (*j.borrow()).clone(); assert!( - (((*(*l.borrow()).v.borrow()) == 5) - && ((*(*(*l.borrow()).inner.borrow()).x.borrow()) == 50)) - && ((*(*l.borrow()).arr.borrow())[(0) as usize] == 5) + (({ (*l.borrow()).v } == 5) && ({ (*l.borrow()).inner.x } == 50)) + && ((*l.borrow()).arr[(0) as usize] == 5) ); let vec_: Value> = Rc::new(RefCell::new(Vec::new())); { @@ -403,19 +486,13 @@ fn main_0() -> i32 { }; (*vec_.borrow_mut()).push(Explicit::new({ 9 })); assert!( - ((*(*(vec_.as_pointer() as Ptr) + ((vec_.as_pointer() as Ptr) .offset(0_usize) - .upgrade() - .deref()) - .v - .borrow()) + .with(|__s: &Explicit| __s.v) == 1) - && ((*(*(vec_.as_pointer() as Ptr) + && ((vec_.as_pointer() as Ptr) .offset(1_usize) - .upgrade() - .deref()) - .v - .borrow()) + .with(|__s: &Explicit| __s.v) == 9) ); let m: Value = Rc::new(RefCell::new(DefaultCopyUserMove::new({ 7 }))); @@ -424,16 +501,14 @@ fn main_0() -> i32 { m.as_pointer() }))); assert!( - (((*(*m1.borrow()).v.borrow()) == 7) && ((*(*m2.borrow()).v.borrow()) == 7)) - && ((*(*m.borrow()).v.borrow()) == 0) + (({ (*m1.borrow()).v } == 7) && ({ (*m2.borrow()).v } == 7)) && ({ (*m.borrow()).v } == 0) ); let m3: Value = Rc::new(RefCell::new(DefaultCopyUserMove::new({ 1 }))); let m4: Value = Rc::new(RefCell::new(DefaultCopyUserMove::new({ 1 }))); (*m3.borrow_mut()) = (*m1.borrow()).clone(); ({ DefaultCopyUserMoveImpl::move_assign(&m4.as_pointer(), m1.as_pointer()) }); assert!( - (((*(*m3.borrow()).v.borrow()) == 7) && ((*(*m4.borrow()).v.borrow()) == 7)) - && ((*(*m1.borrow()).v.borrow()) == 0) + (({ (*m3.borrow()).v } == 7) && ({ (*m4.borrow()).v } == 7)) && ({ (*m1.borrow()).v } == 0) ); let u: Value = Rc::new(RefCell::new(UserCopyDefaultMove::new({ 8 }))); let u1: Value = Rc::new(RefCell::new(UserCopyDefaultMove::copy_from({ @@ -443,34 +518,31 @@ fn main_0() -> i32 { u.as_pointer() }))); assert!( - (((*(*u1.borrow()).v.borrow()) == 108) && ((*(*u2.borrow()).v.borrow()) == 8)) - && ((*(*u.borrow()).v.borrow()) == 8) + (({ (*u1.borrow()).v } == 108) && ({ (*u2.borrow()).v } == 8)) + && ({ (*u.borrow()).v } == 8) ); let u3: Value = Rc::new(RefCell::new(UserCopyDefaultMove::new({ 1 }))); let u4: Value = Rc::new(RefCell::new(UserCopyDefaultMove::new({ 1 }))); ({ UserCopyDefaultMoveImpl::copy_assign(&u3.as_pointer(), u2.as_pointer()) }); ({ UserCopyDefaultMoveImpl::move_assign(&u4.as_pointer(), u2.as_pointer()) }); - assert!(((*(*u3.borrow()).v.borrow()) == 108) && ((*(*u4.borrow()).v.borrow()) == 8)); + assert!(({ (*u3.borrow()).v } == 108) && ({ (*u4.borrow()).v } == 8)); let p: Value = Rc::new(RefCell::new(Buffer::new({ 3 }))); let q: Value = Rc::new(RefCell::new(Buffer::move_from({ p.as_pointer() }))); assert!( - ((((*(*q.borrow()).n.borrow()) == 3) && ((*(*q.borrow()).data.borrow()).len() == 3_usize)) - && ((((*q.borrow()).data.as_pointer() as Ptr) - .offset(2_usize) - .read()) - == 3)) + ((({ (*q.borrow()).n } == 3) && ((*(*q.borrow()).data.borrow()).len() == 3_usize)) + && ({ (*(*q.borrow()).data.borrow())[(2_usize) as usize] } == 3)) && ((*(*p.borrow()).data.borrow()).is_empty()) ); let r: Value = Rc::new(RefCell::new(Buffer::new({ 1 }))); ({ BufferImpl::move_assign(&r.as_pointer(), q.as_pointer()) }); assert!( - ((((*(*r.borrow()).n.borrow()) == 3) && ((*(*r.borrow()).data.borrow()).len() == 3_usize)) - && ((*(*r.borrow()).arr.borrow())[(1) as usize] == 4)) + ((({ (*r.borrow()).n } == 3) && ((*(*r.borrow()).data.borrow()).len() == 3_usize)) + && ((*r.borrow()).arr[(1) as usize] == 4)) && ((*(*q.borrow()).data.borrow()).is_empty()) ); assert!( (((*(*r.borrow()).rows.borrow()).len() == 1_usize) - && ((*(((*r.borrow()).rows.as_pointer() as Ptr>>) + && ((*(({ (*r.borrow()).rows.as_pointer() } as Ptr>>) .offset(0_usize) .upgrade() .deref() @@ -488,86 +560,71 @@ fn main_0() -> i32 { (*bufs.borrow_mut()).push(__init) }; assert!( - (((*(*(bufs.as_pointer() as Ptr) + (((bufs.as_pointer() as Ptr) .offset(1_usize) - .upgrade() - .deref()) - .n - .borrow()) + .with(|__s: &Buffer| __s.n) == 3) - && ((*(*(bufs.as_pointer() as Ptr) + && ((*(bufs.as_pointer() as Ptr) .offset(1_usize) - .upgrade() - .deref()) - .data - .borrow()) + .with(|__s: &Buffer| __s.data.clone()) + .borrow()) .len() == 3_usize)) - && ((*(*(bufs.as_pointer() as Ptr) + && ((*(bufs.as_pointer() as Ptr) .offset(0_usize) - .upgrade() - .deref()) - .data - .borrow()) + .with(|__s: &Buffer| __s.data.clone()) + .borrow()) .is_empty()) ); let o1: Value = Rc::new(RefCell::new(::default())); (*(*o1.borrow()).data.borrow_mut()).push(5); - (*(*o1.borrow()).n.borrow_mut()) = 5; - (*(*o1.borrow()).arr.borrow_mut())[(0) as usize] = 5; - (*(*o1.borrow()).arr.borrow_mut())[(1) as usize] = 6; + (*o1.borrow_mut()).n = 5; + (*o1.borrow_mut()).arr[(0) as usize] = 5; + (*o1.borrow_mut()).arr[(1) as usize] = 6; { let _p: Ptr<_> = Ptr::alloc(7); - (*(*o1.borrow()).p.borrow_mut()) = _p.to_owned_opt() + (*o1.borrow_mut()).p = _p.to_owned_opt() }; let o2: Value = Rc::new(RefCell::new(Owner::move_from({ o1.as_pointer() }))); assert!( - ((((*(*o2.borrow()).n.borrow()) == 5) - && ((*(*o2.borrow()).data.borrow()).len() == 1_usize)) - && ((*(*o2.borrow()).arr.borrow())[(1) as usize] == 6)) - && ((*(*(*o2.borrow()).p.borrow()).as_ref().unwrap().borrow()) == 7) + ((({ (*o2.borrow()).n } == 5) && ((*(*o2.borrow()).data.borrow()).len() == 1_usize)) + && ((*o2.borrow()).arr[(1) as usize] == 6)) + && ((*(*o2.borrow()).p.as_ref().unwrap().borrow()) == 7) ); assert!( - ((*(*o1.borrow()).data.borrow()).is_empty()) - && (((*(*o1.borrow()).p.borrow()).as_pointer()).is_null()) + ((*(*o1.borrow()).data.borrow()).is_empty()) && (((*o1.borrow()).p.as_pointer()).is_null()) ); let o3: Value = Rc::new(RefCell::new(::default())); ({ OwnerImpl::move_assign(&o3.as_pointer(), o2.as_pointer()) }); assert!( - ((((*(*o3.borrow()).n.borrow()) == 5) - && ((((*o3.borrow()).data.as_pointer() as Ptr) - .offset(0_usize) - .read()) - == 5)) - && ((*(*o3.borrow()).arr.borrow())[(0) as usize] == 5)) - && ((*(*(*o3.borrow()).p.borrow()).as_ref().unwrap().borrow()) == 7) + ((({ (*o3.borrow()).n } == 5) + && ({ (*(*o3.borrow()).data.borrow())[(0_usize) as usize] } == 5)) + && ((*o3.borrow()).arr[(0) as usize] == 5)) + && ((*(*o3.borrow()).p.as_ref().unwrap().borrow()) == 7) ); assert!( - ((*(*o2.borrow()).data.borrow()).is_empty()) - && (((*(*o2.borrow()).p.borrow()).as_pointer()).is_null()) + ((*(*o2.borrow()).data.borrow()).is_empty()) && (((*o2.borrow()).p.as_pointer()).is_null()) ); let h1: Value = Rc::new(RefCell::new(Holder::new({ 4 }))); let _dtor_h1 = ScopedDestructor::new(&h1, |__p| __p.destructor()); { let _p: Ptr<_> = Ptr::alloc(9); - (*(*h1.borrow()).p.borrow_mut()) = _p.to_owned_opt() + (*h1.borrow_mut()).p = _p.to_owned_opt() }; let h2: Value = Rc::new(RefCell::new(Holder::move_from({ h1.as_pointer() }))); let _dtor_h2 = ScopedDestructor::new(&h2, |__p| __p.destructor()); assert!( - ((((*(*(*h2.borrow()).inner.borrow()).x.borrow()) == 4) - && ((*(*(*h2.borrow()).e.borrow()).v.borrow()) == 4)) - && ((*(*(*h2.borrow()).p.borrow()).as_ref().unwrap().borrow()) == 9)) - && (((*(*h1.borrow()).p.borrow()).as_pointer()).is_null()) + ((({ (*h2.borrow()).inner.x } == 4) && ({ (*h2.borrow()).e.v } == 4)) + && ((*(*h2.borrow()).p.as_ref().unwrap().borrow()) == 9)) + && (((*h1.borrow()).p.as_pointer()).is_null()) ); let h3: Value = Rc::new(RefCell::new(Holder::new({ 1 }))); let _dtor_h3 = ScopedDestructor::new(&h3, |__p| __p.destructor()); ({ HolderImpl::move_assign(&h3.as_pointer(), h2.as_pointer()) }); assert!( - ((((*(*(*h3.borrow()).inner.borrow()).x.borrow()) == 4) - && ((*(*(*h3.borrow()).e.borrow()).arr.borrow())[(1) as usize] == 5)) - && ((*(*(*h3.borrow()).p.borrow()).as_ref().unwrap().borrow()) == 9)) - && (((*(*h2.borrow()).p.borrow()).as_pointer()).is_null()) + ((({ (*h3.borrow()).inner.x } == 4) && ((*h3.borrow()).e.arr[(1) as usize] == 5)) + && ((*(*h3.borrow()).p.as_ref().unwrap().borrow()) == 9)) + && (((*h2.borrow()).p.as_pointer()).is_null()) ); return 0; } @@ -576,22 +633,18 @@ pub trait BufferImpl { } impl BufferImpl for Ptr { fn move_assign(&self, _a0: Ptr) -> Ptr { - ((*(*self).upgrade().deref()).data.as_pointer() as Ptr>).write(std::mem::take( - &mut (*(*_a0.upgrade().deref()).data.borrow_mut()), - )); - ((*(*self).upgrade().deref()).rows.as_pointer() as Ptr>>>).write( - std::mem::take(&mut (*(*_a0.upgrade().deref()).rows.borrow_mut())), - ); - let __rhs = (*(*_a0.upgrade().deref()).n.borrow()); - (*(*(*self).upgrade().deref()).n.borrow_mut()) = __rhs; + ((*self).with(|__s: &Buffer| __s.data.as_pointer()) as Ptr>) + .write(_a0.with(|__s: &Buffer| std::mem::take(&mut (*__s.data.borrow_mut())))); + ((*self).with(|__s: &Buffer| __s.rows.as_pointer()) as Ptr>>>) + .write(_a0.with(|__s: &Buffer| std::mem::take(&mut (*__s.rows.borrow_mut())))); + let __rhs = _a0.with(|__s: &Buffer| __s.n); + (*self).with_mut(|__s: &mut Buffer| __s.n = __rhs); { - (((*(*self).upgrade().deref()).arr.as_pointer()) as Ptr) - .to_any() - .memcpy( - &(((*_a0.upgrade().deref()).arr.as_pointer()) as Ptr).to_any(), - 8_usize as usize, - ); - (((*(*self).upgrade().deref()).arr.as_pointer()) as Ptr).to_any() + ((field_ptr!((*self), arr)) as Ptr).to_any().memcpy( + &((field_ptr!(_a0, arr)) as Ptr).to_any(), + 8_usize as usize, + ); + ((field_ptr!((*self), arr)) as Ptr).to_any() }; return (*self).clone(); } @@ -601,9 +654,9 @@ pub trait DefaultCopyUserMoveImpl { } impl DefaultCopyUserMoveImpl for Ptr { fn move_assign(&self, o: Ptr) -> Ptr { - let __rhs = (*(*o.upgrade().deref()).v.borrow()); - (*(*(*self).upgrade().deref()).v.borrow_mut()) = __rhs; - (*(*o.upgrade().deref()).v.borrow_mut()) = 0; + let __rhs = o.with(|__s: &DefaultCopyUserMove| __s.v); + (*self).with_mut(|__s: &mut DefaultCopyUserMove| __s.v = __rhs); + o.with_mut(|__s: &mut DefaultCopyUserMove| __s.v = 0); return (*self).clone(); } } @@ -619,16 +672,16 @@ pub trait HolderImpl { } impl HolderImpl for Ptr { fn move_assign(&self, _a0: Ptr) -> Ptr { - let __rhs = (*(*_a0.upgrade().deref()).inner.borrow()).clone(); - (*(*(*self).upgrade().deref()).inner.borrow_mut()) = __rhs; - let __rhs = (*(*_a0.upgrade().deref()).e.borrow()).clone(); - (*(*(*self).upgrade().deref()).e.borrow_mut()) = __rhs; - ((*(*self).upgrade().deref()).p.as_pointer() as Ptr>>) - .write((*(*_a0.upgrade().deref()).p.borrow_mut()).take()); + let __rhs = _a0.with(|__s: &Holder| (__s.inner).clone()); + (*self).with_mut(|__s: &mut Holder| __s.inner = __rhs); + let __rhs = _a0.with(|__s: &Holder| (__s.e).clone()); + (*self).with_mut(|__s: &mut Holder| __s.e = __rhs); + ((field_ptr!((*self), p) as Ptr>>) as Ptr>>) + .write(_a0.with_mut(|__s: &mut Holder| __s.p.take())); return (*self).clone(); } fn destructor(&self) { - (*self.upgrade().deref()).e.as_pointer().destructor(); + ExplicitImpl::destructor(&field_ptr!(self, e)); } } pub trait OwnerImpl { @@ -636,22 +689,19 @@ pub trait OwnerImpl { } impl OwnerImpl for Ptr { fn move_assign(&self, _a0: Ptr) -> Ptr { - ((*(*self).upgrade().deref()).data.as_pointer() as Ptr>).write(std::mem::take( - &mut (*(*_a0.upgrade().deref()).data.borrow_mut()), - )); - let __rhs = (*(*_a0.upgrade().deref()).n.borrow()); - (*(*(*self).upgrade().deref()).n.borrow_mut()) = __rhs; + ((*self).with(|__s: &Owner| __s.data.as_pointer()) as Ptr>) + .write(_a0.with(|__s: &Owner| std::mem::take(&mut (*__s.data.borrow_mut())))); + let __rhs = _a0.with(|__s: &Owner| __s.n); + (*self).with_mut(|__s: &mut Owner| __s.n = __rhs); { - (((*(*self).upgrade().deref()).arr.as_pointer()) as Ptr) - .to_any() - .memcpy( - &(((*_a0.upgrade().deref()).arr.as_pointer()) as Ptr).to_any(), - 8_usize as usize, - ); - (((*(*self).upgrade().deref()).arr.as_pointer()) as Ptr).to_any() + ((field_ptr!((*self), arr)) as Ptr).to_any().memcpy( + &((field_ptr!(_a0, arr)) as Ptr).to_any(), + 8_usize as usize, + ); + ((field_ptr!((*self), arr)) as Ptr).to_any() }; - ((*(*self).upgrade().deref()).p.as_pointer() as Ptr>>) - .write((*(*_a0.upgrade().deref()).p.borrow_mut()).take()); + ((field_ptr!((*self), p) as Ptr>>) as Ptr>>) + .write(_a0.with_mut(|__s: &mut Owner| __s.p.take())); return (*self).clone(); } } @@ -661,13 +711,13 @@ pub trait UserCopyDefaultMoveImpl { } impl UserCopyDefaultMoveImpl for Ptr { fn copy_assign(&self, o: Ptr) -> Ptr { - let __rhs = ((*(*o.upgrade().deref()).v.borrow()) + 100); - (*(*(*self).upgrade().deref()).v.borrow_mut()) = __rhs; + let __rhs = (o.with(|__s: &UserCopyDefaultMove| __s.v) + 100); + (*self).with_mut(|__s: &mut UserCopyDefaultMove| __s.v = __rhs); return (*self).clone(); } fn move_assign(&self, _a0: Ptr) -> Ptr { - let __rhs = (*(*_a0.upgrade().deref()).v.borrow()); - (*(*(*self).upgrade().deref()).v.borrow_mut()) = __rhs; + let __rhs = _a0.with(|__s: &UserCopyDefaultMove| __s.v); + (*self).with_mut(|__s: &mut UserCopyDefaultMove| __s.v = __rhs); return (*self).clone(); } } diff --git a/tests/unit/out/refcount/copy_move_deleted.rs b/tests/unit/out/refcount/copy_move_deleted.rs index b6b74a2241..1c76cc9e5f 100644 --- a/tests/unit/out/refcount/copy_move_deleted.rs +++ b/tests/unit/out/refcount/copy_move_deleted.rs @@ -6,47 +6,62 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Default)] +#[derive(Record, Default)] pub struct NoCopy { - pub v: Value, + #[offset(0)] + pub v: i32, } impl NoCopy { pub fn new(v: i32) -> Self { let v: Value = Rc::new(RefCell::new(v)); - let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*v.borrow()))), - })); + let __this: Value = Rc::new(RefCell::new(Self { v: (*v.borrow()) })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } pub fn move_from(o: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*(*o.upgrade().deref()).v.borrow()))), + v: { o.with(|__s: &NoCopy| __s.v) }, })); let this: Ptr = __this.as_pointer(); - (*(*o.upgrade().deref()).v.borrow_mut()) = 0; + o.with_mut(|__s: &mut NoCopy| __s.v = 0); Rc::try_unwrap(__this).ok().unwrap().into_inner() } } +<<<<<<< HEAD impl ByteRepr for NoCopy {} #[derive()] +======= +impl ByteRepr for NoCopy { + fn byte_size() -> usize { + 4 + } + fn to_bytes(&self, buf: &mut [u8]) { + self.v.to_bytes(&mut buf[0..4]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + v: ::from_bytes(&buf[0..4]), + } + } +} +#[derive(Record)] +>>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub struct PrivateCopy { - pub v: Value, + #[offset(0)] + pub v: i32, } impl PrivateCopy { pub fn new() -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new(0)), - })); + let __this: Value = Rc::new(RefCell::new(Self { v: 0 })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } pub fn move_from(o: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*(*o.upgrade().deref()).v.borrow()))), + v: { o.with(|__s: &PrivateCopy| __s.v) }, })); let this: Ptr = __this.as_pointer(); - (*(*o.upgrade().deref()).v.borrow_mut()) = 0; + o.with_mut(|__s: &mut PrivateCopy| __s.v = 0); Rc::try_unwrap(__this).ok().unwrap().into_inner() } } @@ -55,16 +70,32 @@ impl Default for PrivateCopy { { PrivateCopy::new() } } } +<<<<<<< HEAD impl ByteRepr for PrivateCopy {} #[derive()] +======= +impl ByteRepr for PrivateCopy { + fn byte_size() -> usize { + 4 + } + fn to_bytes(&self, buf: &mut [u8]) { + self.v.to_bytes(&mut buf[0..4]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + v: ::from_bytes(&buf[0..4]), + } + } +} +#[derive(Record)] +>>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub struct Immovable { - pub v: Value, + #[offset(0)] + pub v: i32, } impl Immovable { pub fn new() -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new(0)), - })); + let __this: Value = Rc::new(RefCell::new(Self { v: 0 })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } @@ -79,38 +110,56 @@ impl ByteRepr for Immovable { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.v.borrow()).to_bytes(&mut buf[0..4]); + self.v.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - v: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + v: ::from_bytes(&buf[0..4]), } } } -#[derive(Default)] +#[derive(Record, Default)] pub struct Container { - pub inner: Value, - pub tag: Value, + #[offset(0)] + pub inner: NoCopy, + #[offset(4)] + pub tag: i32, } impl Container { pub fn move_from(_a0: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - inner: Rc::new(RefCell::new(NoCopy::move_from({ - (*_a0.upgrade().deref()).inner.as_pointer() - }))), - tag: Rc::new(RefCell::new((*(*_a0.upgrade().deref()).tag.borrow()))), + inner: { NoCopy::move_from({ field_ptr!(_a0, inner) }) }, + tag: { _a0.with(|__s: &Container| __s.tag) }, })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } } +<<<<<<< HEAD impl ByteRepr for Container {} +======= +impl ByteRepr for Container { + fn byte_size() -> usize { + 8 + } + fn to_bytes(&self, buf: &mut [u8]) { + self.inner.to_bytes(&mut buf[0..4]); + self.tag.to_bytes(&mut buf[4..8]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + inner: ::from_bytes(&buf[0..4]), + tag: ::from_bytes(&buf[4..8]), + } + } +} +>>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub fn bump_0(p: Ptr) { let p: Value> = Rc::new(RefCell::new(p)); - (*(*(*p.borrow()).upgrade().deref()).v.borrow_mut()).postfix_inc(); + (*p.borrow()).with_mut(|__s: &mut NoCopy| __s.v.postfix_inc()); } pub fn bump_ref_1(r: Ptr) { - (*(*r.upgrade().deref()).v.borrow_mut()).postfix_inc(); + r.with_mut(|__s: &mut Immovable| __s.v.postfix_inc()); } pub fn main() { __cpp2rust_init_globals(); @@ -119,31 +168,30 @@ pub fn main() { fn main_0() -> i32 { let a: Value = Rc::new(RefCell::new(NoCopy::new({ 1 }))); let b: Value = Rc::new(RefCell::new(NoCopy::move_from({ a.as_pointer() }))); - assert!(((*(*b.borrow()).v.borrow()) == 1) && ((*(*a.borrow()).v.borrow()) == 0)); + assert!(({ (*b.borrow()).v } == 1) && ({ (*a.borrow()).v } == 0)); ({ NoCopyImpl::move_assign(&a.as_pointer(), b.as_pointer()) }); - assert!(((*(*a.borrow()).v.borrow()) == 1) && ((*(*b.borrow()).v.borrow()) == 0)); + assert!(({ (*a.borrow()).v } == 1) && ({ (*b.borrow()).v } == 0)); ({ bump_0((a.as_pointer())) }); - assert!(((*(*a.borrow()).v.borrow()) == 2)); + assert!(({ (*a.borrow()).v } == 2)); let p: Value = Rc::new(RefCell::new(PrivateCopy::new())); - (*(*p.borrow()).v.borrow_mut()) = 3; + (*p.borrow_mut()).v = 3; let q: Value = Rc::new(RefCell::new(PrivateCopy::move_from({ p.as_pointer() }))); - assert!(((*(*q.borrow()).v.borrow()) == 3) && ((*(*p.borrow()).v.borrow()) == 0)); + assert!(({ (*q.borrow()).v } == 3) && ({ (*p.borrow()).v } == 0)); ({ PrivateCopyImpl::move_assign(&p.as_pointer(), q.as_pointer()) }); - assert!(((*(*p.borrow()).v.borrow()) == 3) && ((*(*q.borrow()).v.borrow()) == 0)); + assert!(({ (*p.borrow()).v } == 3) && ({ (*q.borrow()).v } == 0)); let im: Value = Rc::new(RefCell::new(Immovable::new())); - (*(*im.borrow()).v.borrow_mut()) = 4; + (*im.borrow_mut()).v = 4; ({ bump_ref_1(im.as_pointer()) }); let pim: Value> = Rc::new(RefCell::new((im.as_pointer()))); - assert!(((*(*(*pim.borrow()).upgrade().deref()).v.borrow()) == 5)); + assert!(((*pim.borrow()).with(|__s: &Immovable| __s.v) == 5)); let c: Value = Rc::new(RefCell::new(Container { - inner: Rc::new(RefCell::new(NoCopy::new({ 6 }))), - tag: Rc::new(RefCell::new(7)), + inner: NoCopy::new({ 6 }), + tag: 7, })); let d: Value = Rc::new(RefCell::new(Container::move_from({ c.as_pointer() }))); assert!( - (((*(*(*d.borrow()).inner.borrow()).v.borrow()) == 6) - && ((*(*d.borrow()).tag.borrow()) == 7)) - && ((*(*(*c.borrow()).inner.borrow()).v.borrow()) == 0) + (({ (*d.borrow()).inner.v } == 6) && ({ (*d.borrow()).tag } == 7)) + && ({ (*c.borrow()).inner.v } == 0) ); return 0; } @@ -153,11 +201,11 @@ pub trait ContainerImpl { impl ContainerImpl for Ptr { fn move_assign(&self, _a0: Ptr) -> Ptr { ({ - let _o: Ptr = (*_a0.upgrade().deref()).inner.as_pointer(); - NoCopyImpl::move_assign(&(*(*self).upgrade().deref()).inner.as_pointer(), _o) + let _o: Ptr = field_ptr!(_a0, inner); + NoCopyImpl::move_assign(&{ field_ptr!((*self), inner) }, _o) }); - let __rhs = (*(*_a0.upgrade().deref()).tag.borrow()); - (*(*(*self).upgrade().deref()).tag.borrow_mut()) = __rhs; + let __rhs = _a0.with(|__s: &Container| __s.tag); + (*self).with_mut(|__s: &mut Container| __s.tag = __rhs); return (*self).clone(); } } @@ -166,9 +214,9 @@ pub trait NoCopyImpl { } impl NoCopyImpl for Ptr { fn move_assign(&self, o: Ptr) -> Ptr { - let __rhs = (*(*o.upgrade().deref()).v.borrow()); - (*(*(*self).upgrade().deref()).v.borrow_mut()) = __rhs; - (*(*o.upgrade().deref()).v.borrow_mut()) = 0; + let __rhs = o.with(|__s: &NoCopy| __s.v); + (*self).with_mut(|__s: &mut NoCopy| __s.v = __rhs); + o.with_mut(|__s: &mut NoCopy| __s.v = 0); return (*self).clone(); } } @@ -180,9 +228,9 @@ pub trait PrivateCopyImpl { } impl PrivateCopyImpl for Ptr { fn move_assign(&self, o: Ptr) -> Ptr { - let __rhs = (*(*o.upgrade().deref()).v.borrow()); - (*(*(*self).upgrade().deref()).v.borrow_mut()) = __rhs; - (*(*o.upgrade().deref()).v.borrow_mut()) = 0; + let __rhs = o.with(|__s: &PrivateCopy| __s.v); + (*self).with_mut(|__s: &mut PrivateCopy| __s.v = __rhs); + o.with_mut(|__s: &mut PrivateCopy| __s.v = 0); return (*self).clone(); } } diff --git a/tests/unit/out/refcount/default.rs b/tests/unit/out/refcount/default.rs index 8a5ccc5ff9..32a73a0fac 100644 --- a/tests/unit/out/refcount/default.rs +++ b/tests/unit/out/refcount/default.rs @@ -6,47 +6,31 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg)] +#[derive(Clone, Record, VaArg, FnPtrArg)] pub struct Pointers { - pub x1: Value>, - pub x2: Value>, - pub x3: Value]>>, - pub x4: Value]>>, - pub x5: Value, -} -impl Clone for Pointers { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - x1: Rc::new(RefCell::new((*self.x1.borrow()).clone())), - x2: Rc::new(RefCell::new((*self.x2.borrow()).clone())), - x3: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 5, _>( - |__i: usize| ((*self.x3.borrow())[(__i) as usize]).clone(), - )))), - x4: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 10, _>( - |__i: usize| ((*self.x4.borrow())[(__i) as usize]).clone(), - )))), - x5: Rc::new(RefCell::new((*self.x5.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub x1: Ptr, + #[offset(8)] + pub x2: Ptr, + #[offset(16)] + pub x3: Box<[Ptr]>, + #[offset(56)] + pub x4: Box<[Ptr]>, + #[offset(136)] + pub x5: i32, } impl Default for Pointers { fn default() -> Self { Pointers { - x1: Rc::new(RefCell::new(Ptr::::null())), - x2: Rc::new(RefCell::new(Ptr::::null())), - x3: Rc::new(RefCell::new( - (0..5) - .map(|_| Ptr::::null()) - .collect::]>>(), - )), - x4: Rc::new(RefCell::new( - (0..10) - .map(|_| Ptr::::null()) - .collect::]>>(), - )), - x5: Rc::new(RefCell::new(0_i32)), + x1: Ptr::::null(), + x2: Ptr::::null(), + x3: (0..5) + .map(|_| Ptr::::null()) + .collect::]>>(), + x4: (0..10) + .map(|_| Ptr::::null()) + .collect::]>>(), + x5: 0_i32, } } } @@ -55,19 +39,19 @@ impl ByteRepr for Pointers { 144 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.x1.borrow()).to_bytes(&mut buf[0..8]); - (*self.x2.borrow()).to_bytes(&mut buf[8..16]); - (*self.x3.borrow()).to_bytes(&mut buf[16..56]); - (*self.x4.borrow()).to_bytes(&mut buf[56..136]); - (*self.x5.borrow()).to_bytes(&mut buf[136..140]); + self.x1.to_bytes(&mut buf[0..8]); + self.x2.to_bytes(&mut buf[8..16]); + self.x3.to_bytes(&mut buf[16..56]); + self.x4.to_bytes(&mut buf[56..136]); + self.x5.to_bytes(&mut buf[136..140]); } fn from_bytes(buf: &[u8]) -> Self { Self { - x1: Rc::new(RefCell::new(>::from_bytes(&buf[0..8]))), - x2: Rc::new(RefCell::new(>::from_bytes(&buf[8..16]))), - x3: Rc::new(RefCell::new(]>>::from_bytes(&buf[16..56]))), - x4: Rc::new(RefCell::new(]>>::from_bytes(&buf[56..136]))), - x5: Rc::new(RefCell::new(::from_bytes(&buf[136..140]))), + x1: >::from_bytes(&buf[0..8]), + x2: >::from_bytes(&buf[8..16]), + x3: ]>>::from_bytes(&buf[16..56]), + x4: ]>>::from_bytes(&buf[56..136]), + x5: ::from_bytes(&buf[136..140]), } } } diff --git a/tests/unit/out/refcount/default_function_arguments.rs b/tests/unit/out/refcount/default_function_arguments.rs index f4ef733073..bb7771a87e 100644 --- a/tests/unit/out/refcount/default_function_arguments.rs +++ b/tests/unit/out/refcount/default_function_arguments.rs @@ -19,25 +19,15 @@ pub fn baz_1(a: Ptr, b: Option>) -> bool { _lhs == (*b.borrow()).clone() }; } -#[derive(VaArg, FnPtrArg)] +#[derive(Clone, Record, VaArg, FnPtrArg)] pub struct Bar { - pub v: Value, + #[offset(0)] + pub v: i32, } impl Bar { pub fn new(v: Option) -> Self { let v: Value = Rc::new(RefCell::new(v.unwrap_or(1))); - let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*v.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } -} -impl Clone for Bar { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*self.v.borrow()))), - })); + let __this: Value = Rc::new(RefCell::new(Self { v: (*v.borrow()) })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } @@ -52,11 +42,11 @@ impl ByteRepr for Bar { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.v.borrow()).to_bytes(&mut buf[0..4]); + self.v.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - v: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + v: ::from_bytes(&buf[0..4]), } } } @@ -78,15 +68,15 @@ fn main_0() -> i32 { == (true as i32)) ); let b: Value = Rc::new(RefCell::new(Bar::new(None))); - assert!(((*(*b.borrow()).v.borrow()) == 1)); - assert!(((*Bar::new({ Some(2) },).v.borrow()) == 2)); + assert!(({ (*b.borrow()).v } == 1)); + assert!(({ Bar::new({ Some(2) },).v } == 2)); let arr: Value> = Rc::new(RefCell::new(Box::new([ Bar::new(None), Bar::new(None), Bar::new(None), ]))); - assert!(((*(*arr.borrow())[(0) as usize].v.borrow()) == 1)); - assert!(((*(*arr.borrow())[(2) as usize].v.borrow()) == 1)); + assert!(({ (*arr.borrow())[(0) as usize].v } == 1)); + assert!(({ (*arr.borrow())[(2) as usize].v } == 1)); return 0; } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/default_in_statics.rs b/tests/unit/out/refcount/default_in_statics.rs index 0e775c0bd3..29899c9991 100644 --- a/tests/unit/out/refcount/default_in_statics.rs +++ b/tests/unit/out/refcount/default_in_statics.rs @@ -6,80 +6,60 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Inner { - pub v: Value, - pub name: Value>, -} -impl Clone for Inner { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*self.v.borrow()))), - name: Rc::new(RefCell::new((*self.name.borrow()).clone())), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub v: i32, + #[offset(8)] + pub name: Ptr, } impl ByteRepr for Inner { fn byte_size() -> usize { 16 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.v.borrow()).to_bytes(&mut buf[0..4]); - (*self.name.borrow()).to_bytes(&mut buf[8..16]); + self.v.to_bytes(&mut buf[0..4]); + self.name.to_bytes(&mut buf[8..16]); } fn from_bytes(buf: &[u8]) -> Self { Self { - v: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - name: Rc::new(RefCell::new(>::from_bytes(&buf[8..16]))), + v: ::from_bytes(&buf[0..4]), + name: >::from_bytes(&buf[8..16]), } } } -#[derive(VaArg, FnPtrArg)] +#[derive(Clone, Record, VaArg, FnPtrArg)] pub struct Outer { - pub p1: Value>, - pub p2: Value>, - pub arr: Value]>>, - pub cp: Value>, - pub pp: Value>>, - pub inner: Value, - pub x: Value, - pub fn_: Value i32>>, -} -impl Clone for Outer { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - p1: Rc::new(RefCell::new((*self.p1.borrow()).clone())), - p2: Rc::new(RefCell::new((*self.p2.borrow()).clone())), - arr: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 3, _>( - |__i: usize| ((*self.arr.borrow())[(__i) as usize]).clone(), - )))), - cp: Rc::new(RefCell::new((*self.cp.borrow()).clone())), - pp: Rc::new(RefCell::new((*self.pp.borrow()).clone())), - inner: Rc::new(RefCell::new((*self.inner.borrow()).clone())), - x: Rc::new(RefCell::new((*self.x.borrow()))), - fn_: Rc::new(RefCell::new((*self.fn_.borrow()).clone())), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub p1: Ptr, + #[offset(8)] + pub p2: Ptr, + #[offset(16)] + pub arr: Box<[Ptr]>, + #[offset(40)] + pub cp: Ptr, + #[offset(48)] + pub pp: Ptr>, + #[offset(56)] + pub inner: Inner, + #[offset(72)] + pub x: i32, + #[offset(80)] + pub fn_: FnPtr i32>, } impl Default for Outer { fn default() -> Self { Outer { - p1: Rc::new(RefCell::new(Ptr::::null())), - p2: Rc::new(RefCell::new(Ptr::::null())), - arr: Rc::new(RefCell::new( - (0..3) - .map(|_| Ptr::::null()) - .collect::]>>(), - )), - cp: Rc::new(RefCell::new(Ptr::::null())), - pp: Rc::new(RefCell::new(Ptr::>::null())), - inner: >::default(), - x: Rc::new(RefCell::new(0_i32)), - fn_: Rc::new(RefCell::new(FnPtr:: i32>::null())), + p1: Ptr::::null(), + p2: Ptr::::null(), + arr: (0..3) + .map(|_| Ptr::::null()) + .collect::]>>(), + cp: Ptr::::null(), + pp: Ptr::>::null(), + inner: ::default(), + x: 0_i32, + fn_: FnPtr:: i32>::null(), } } } @@ -88,59 +68,49 @@ impl ByteRepr for Outer { 88 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.p1.borrow()).to_bytes(&mut buf[0..8]); - (*self.p2.borrow()).to_bytes(&mut buf[8..16]); - (*self.arr.borrow()).to_bytes(&mut buf[16..40]); - (*self.cp.borrow()).to_bytes(&mut buf[40..48]); - (*self.pp.borrow()).to_bytes(&mut buf[48..56]); - (*self.inner.borrow()).to_bytes(&mut buf[56..72]); - (*self.x.borrow()).to_bytes(&mut buf[72..76]); - (*self.fn_.borrow()).to_bytes(&mut buf[80..88]); + self.p1.to_bytes(&mut buf[0..8]); + self.p2.to_bytes(&mut buf[8..16]); + self.arr.to_bytes(&mut buf[16..40]); + self.cp.to_bytes(&mut buf[40..48]); + self.pp.to_bytes(&mut buf[48..56]); + self.inner.to_bytes(&mut buf[56..72]); + self.x.to_bytes(&mut buf[72..76]); + self.fn_.to_bytes(&mut buf[80..88]); } fn from_bytes(buf: &[u8]) -> Self { Self { - p1: Rc::new(RefCell::new(>::from_bytes(&buf[0..8]))), - p2: Rc::new(RefCell::new(>::from_bytes(&buf[8..16]))), - arr: Rc::new(RefCell::new(]>>::from_bytes(&buf[16..40]))), - cp: Rc::new(RefCell::new(>::from_bytes(&buf[40..48]))), - pp: Rc::new(RefCell::new(>>::from_bytes(&buf[48..56]))), - inner: Rc::new(RefCell::new(::from_bytes(&buf[56..72]))), - x: Rc::new(RefCell::new(::from_bytes(&buf[72..76]))), - fn_: Rc::new(RefCell::new( i32>>::from_bytes( - &buf[80..88], - ))), + p1: >::from_bytes(&buf[0..8]), + p2: >::from_bytes(&buf[8..16]), + arr: ]>>::from_bytes(&buf[16..40]), + cp: >::from_bytes(&buf[40..48]), + pp: >>::from_bytes(&buf[48..56]), + inner: ::from_bytes(&buf[56..72]), + x: ::from_bytes(&buf[72..76]), + fn_: i32>>::from_bytes(&buf[80..88]), } } } -#[derive(VaArg, FnPtrArg)] +#[derive(Clone, Record, VaArg, FnPtrArg)] pub struct Foo { - pub s1: Value>, - pub s2: Value>, - pub fn1: Value i32>>, - pub fn2: Value i32>>, - pub n: Value, -} -impl Clone for Foo { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - s1: Rc::new(RefCell::new((*self.s1.borrow()).clone())), - s2: Rc::new(RefCell::new((*self.s2.borrow()).clone())), - fn1: Rc::new(RefCell::new((*self.fn1.borrow()).clone())), - fn2: Rc::new(RefCell::new((*self.fn2.borrow()).clone())), - n: Rc::new(RefCell::new((*self.n.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub s1: Ptr, + #[offset(8)] + pub s2: Ptr, + #[offset(16)] + pub fn1: FnPtr i32>, + #[offset(24)] + pub fn2: FnPtr i32>, + #[offset(32)] + pub n: i32, } impl Default for Foo { fn default() -> Self { Foo { - s1: Rc::new(RefCell::new(Ptr::::null())), - s2: Rc::new(RefCell::new(Ptr::::null())), - fn1: Rc::new(RefCell::new(FnPtr:: i32>::null())), - fn2: Rc::new(RefCell::new(FnPtr:: i32>::null())), - n: Rc::new(RefCell::new(0_i32)), + s1: Ptr::::null(), + s2: Ptr::::null(), + fn1: FnPtr:: i32>::null(), + fn2: FnPtr:: i32>::null(), + n: 0_i32, } } } @@ -149,23 +119,19 @@ impl ByteRepr for Foo { 40 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.s1.borrow()).to_bytes(&mut buf[0..8]); - (*self.s2.borrow()).to_bytes(&mut buf[8..16]); - (*self.fn1.borrow()).to_bytes(&mut buf[16..24]); - (*self.fn2.borrow()).to_bytes(&mut buf[24..32]); - (*self.n.borrow()).to_bytes(&mut buf[32..36]); + self.s1.to_bytes(&mut buf[0..8]); + self.s2.to_bytes(&mut buf[8..16]); + self.fn1.to_bytes(&mut buf[16..24]); + self.fn2.to_bytes(&mut buf[24..32]); + self.n.to_bytes(&mut buf[32..36]); } fn from_bytes(buf: &[u8]) -> Self { Self { - s1: Rc::new(RefCell::new(>::from_bytes(&buf[0..8]))), - s2: Rc::new(RefCell::new(>::from_bytes(&buf[8..16]))), - fn1: Rc::new(RefCell::new( i32>>::from_bytes( - &buf[16..24], - ))), - fn2: Rc::new(RefCell::new( i32>>::from_bytes( - &buf[24..32], - ))), - n: Rc::new(RefCell::new(::from_bytes(&buf[32..36]))), + s1: >::from_bytes(&buf[0..8]), + s2: >::from_bytes(&buf[8..16]), + fn1: i32>>::from_bytes(&buf[16..24]), + fn2: i32>>::from_bytes(&buf[24..32]), + n: ::from_bytes(&buf[32..36]), } } } @@ -183,28 +149,28 @@ thread_local!( ); thread_local!( pub static static_foo_3: Value = Rc::new(RefCell::new(Foo { - s1: Rc::new(RefCell::new(Ptr::::from_string_literal(b"hello"))), - s2: Rc::new(RefCell::new(Ptr::::null())), - fn1: Rc::new(RefCell::new(FnPtr:: i32>::null())), - fn2: Rc::new(RefCell::new(FnPtr:: i32>::null())), - n: Rc::new(RefCell::new(42)), + s1: Ptr::::from_string_literal(b"hello"), + s2: Ptr::::null(), + fn1: FnPtr:: i32>::null(), + fn2: FnPtr:: i32>::null(), + n: 42, })); ); thread_local!( pub static static_foo_array_4: Value> = Rc::new(RefCell::new(Box::new([ Foo { - s1: Rc::new(RefCell::new(Ptr::::from_string_literal(b"first"))), - s2: Rc::new(RefCell::new(Ptr::::null())), - fn1: Rc::new(RefCell::new(FnPtr:: i32>::null())), - fn2: Rc::new(RefCell::new(FnPtr:: i32>::null())), - n: Rc::new(RefCell::new(1)), + s1: Ptr::::from_string_literal(b"first"), + s2: Ptr::::null(), + fn1: FnPtr:: i32>::null(), + fn2: FnPtr:: i32>::null(), + n: 1, }, Foo { - s1: Rc::new(RefCell::new(Ptr::::from_string_literal(b"second"))), - s2: Rc::new(RefCell::new(Ptr::::null())), - fn1: Rc::new(RefCell::new(FnPtr:: i32>::null())), - fn2: Rc::new(RefCell::new(FnPtr:: i32>::null())), - n: Rc::new(RefCell::new(2)), + s1: Ptr::::from_string_literal(b"second"), + s2: Ptr::::null(), + fn1: FnPtr:: i32>::null(), + fn2: FnPtr:: i32>::null(), + n: 2, }, ]))); ); @@ -219,8 +185,8 @@ pub fn check_local_static_5() { thread_local!( static local_p_8: Value> = Rc::new(RefCell::new(Ptr::::null())); ); - assert!((*(*local_outer_6.with(Value::clone).borrow()).p1.borrow()).is_null()); - assert!((*(*local_outer_6.with(Value::clone).borrow()).fn_.borrow()).is_null()); + assert!(({ (*local_outer_6.with(Value::clone).borrow()).p1.clone() }).is_null()); + assert!(({ (*local_outer_6.with(Value::clone).borrow()).fn_.clone() }).is_null()); assert!((*local_fn_7.with(Value::clone).borrow()).is_null()); assert!((*local_p_8.with(Value::clone).borrow()).is_null()); } @@ -230,57 +196,67 @@ pub fn main() { } fn main_0() -> i32 { assert!((*static_fn_0.with(Value::clone).borrow()).is_null()); - assert!((*(*static_outer_1.with(Value::clone).borrow()).p1.borrow()).is_null()); - assert!((*(*static_outer_1.with(Value::clone).borrow()).p2.borrow()).is_null()); - assert!((*(*static_outer_1.with(Value::clone).borrow()).cp.borrow()).is_null()); - assert!((*(*static_outer_1.with(Value::clone).borrow()).pp.borrow()).is_null()); - assert!((*(*static_outer_1.with(Value::clone).borrow()).fn_.borrow()).is_null()); + assert!(({ (*static_outer_1.with(Value::clone).borrow()).p1.clone() }).is_null()); + assert!(({ (*static_outer_1.with(Value::clone).borrow()).p2.clone() }).is_null()); + assert!(({ (*static_outer_1.with(Value::clone).borrow()).cp.clone() }).is_null()); + assert!(({ (*static_outer_1.with(Value::clone).borrow()).pp.clone() }).is_null()); + assert!(({ (*static_outer_1.with(Value::clone).borrow()).fn_.clone() }).is_null()); let i: Value = Rc::new(RefCell::new(0)); 'loop_: while ((*i.borrow()) < 3) { assert!( - ((*(*static_outer_1.with(Value::clone).borrow()).arr.borrow())[(*i.borrow()) as usize]) - .is_null() + ((*static_outer_1.with(Value::clone).borrow()).arr[(*i.borrow()) as usize]).is_null() ); (*i.borrow_mut()).prefix_inc(); } assert!( - (*(*(*static_outer_1.with(Value::clone).borrow()).inner.borrow()) - .name - .borrow()) + ({ + (*static_outer_1.with(Value::clone).borrow()) + .inner + .name + .clone() + }) .is_null() ); let i: Value = Rc::new(RefCell::new(0)); 'loop_: while ((*i.borrow()) < 2) { assert!( - (*(*static_inner_array_2.with(Value::clone).borrow())[(*i.borrow()) as usize] - .name - .borrow()) + ({ + (*static_inner_array_2.with(Value::clone).borrow())[(*i.borrow()) as usize] + .name + .clone() + }) .is_null() ); (*i.borrow_mut()).prefix_inc(); } - assert!((*(*static_foo_3.with(Value::clone).borrow()).s2.borrow()).is_null()); - assert!((*(*static_foo_3.with(Value::clone).borrow()).fn1.borrow()).is_null()); - assert!((*(*static_foo_3.with(Value::clone).borrow()).fn2.borrow()).is_null()); - assert!(((*(*static_foo_3.with(Value::clone).borrow()).n.borrow()) == 42)); + assert!(({ (*static_foo_3.with(Value::clone).borrow()).s2.clone() }).is_null()); + assert!(({ (*static_foo_3.with(Value::clone).borrow()).fn1.clone() }).is_null()); + assert!(({ (*static_foo_3.with(Value::clone).borrow()).fn2.clone() }).is_null()); + assert!(({ (*static_foo_3.with(Value::clone).borrow()).n } == 42)); let i: Value = Rc::new(RefCell::new(0)); 'loop_: while ((*i.borrow()) < 2) { assert!( - (*(*static_foo_array_4.with(Value::clone).borrow())[(*i.borrow()) as usize] - .s2 - .borrow()) + ({ + (*static_foo_array_4.with(Value::clone).borrow())[(*i.borrow()) as usize] + .s2 + .clone() + }) .is_null() ); assert!( - (*(*static_foo_array_4.with(Value::clone).borrow())[(*i.borrow()) as usize] - .fn1 - .borrow()) + ({ + (*static_foo_array_4.with(Value::clone).borrow())[(*i.borrow()) as usize] + .fn1 + .clone() + }) .is_null() ); assert!( - (*(*static_foo_array_4.with(Value::clone).borrow())[(*i.borrow()) as usize] - .fn2 - .borrow()) + ({ + (*static_foo_array_4.with(Value::clone).borrow())[(*i.borrow()) as usize] + .fn2 + .clone() + }) .is_null() ); (*i.borrow_mut()).prefix_inc(); diff --git a/tests/unit/out/refcount/destructor.rs b/tests/unit/out/refcount/destructor.rs index 8933ec066f..7fd47d7a8f 100644 --- a/tests/unit/out/refcount/destructor.rs +++ b/tests/unit/out/refcount/destructor.rs @@ -9,131 +9,198 @@ use std::rc::{Rc, Weak}; thread_local!( pub static global_0: Value = Rc::new(RefCell::new(0)); ); -#[derive(Clone, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] pub struct S {} -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Defaulted { - pub s: Value, -} -impl Clone for Defaulted { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - s: Rc::new(RefCell::new((*self.s.borrow()).clone())), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub s: S, } +<<<<<<< HEAD impl ByteRepr for Defaulted {} #[derive(VaArg, FnPtrArg, Default)] -pub struct Middle { - pub s: Value, -} -impl Clone for Middle { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - s: Rc::new(RefCell::new((*self.s.borrow()).clone())), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() +======= +impl ByteRepr for Defaulted { + fn byte_size() -> usize { + 1 + } + fn to_bytes(&self, buf: &mut [u8]) { + self.s.to_bytes(&mut buf[0..1]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + s: ::from_bytes(&buf[0..1]), + } } } +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] +>>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) +pub struct Middle { + #[offset(0)] + pub s: S, +} +<<<<<<< HEAD impl ByteRepr for Middle {} #[derive(VaArg, FnPtrArg, Default)] -pub struct Outer { - pub m: Value, -} -impl Clone for Outer { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - m: Rc::new(RefCell::new((*self.m.borrow()).clone())), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() +======= +impl ByteRepr for Middle { + fn byte_size() -> usize { + 1 } + fn to_bytes(&self, buf: &mut [u8]) { + self.s.to_bytes(&mut buf[0..1]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + s: ::from_bytes(&buf[0..1]), + } + } +} +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] +>>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) +pub struct Outer { + #[offset(0)] + pub m: Middle, } +<<<<<<< HEAD impl ByteRepr for Outer {} #[derive(VaArg, FnPtrArg)] -pub struct ArrayMember { - pub items: Value>, -} -impl Clone for ArrayMember { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - items: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 3, _>( - |__i: usize| ((*self.items.borrow())[(__i) as usize]).clone(), - )))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() +======= +impl ByteRepr for Outer { + fn byte_size() -> usize { + 1 + } + fn to_bytes(&self, buf: &mut [u8]) { + self.m.to_bytes(&mut buf[0..1]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + m: ::from_bytes(&buf[0..1]), + } } } +#[derive(Clone, Record, VaArg, FnPtrArg)] +>>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) +pub struct ArrayMember { + #[offset(0)] + pub items: Box<[S]>, +} impl Default for ArrayMember { fn default() -> Self { ArrayMember { - items: Rc::new(RefCell::new( - (0..3).map(|_| ::default()).collect::>(), - )), + items: (0..3).map(|_| ::default()).collect::>(), } } } +<<<<<<< HEAD impl ByteRepr for ArrayMember {} #[derive(VaArg, FnPtrArg, Default)] -pub struct EmptyBody { - pub s: Value, -} -impl Clone for EmptyBody { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - s: Rc::new(RefCell::new((*self.s.borrow()).clone())), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() +======= +impl ByteRepr for ArrayMember { + fn byte_size() -> usize { + 3 } + fn to_bytes(&self, buf: &mut [u8]) { + self.items.to_bytes(&mut buf[0..3]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + items: >::from_bytes(&buf[0..3]), + } + } +} +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] +>>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) +pub struct EmptyBody { + #[offset(0)] + pub s: S, } +<<<<<<< HEAD impl ByteRepr for EmptyBody {} #[derive(VaArg, FnPtrArg, Default)] -pub struct Templated_char_ { - pub v: Value, -} -impl Clone for Templated_char_ { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*self.v.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() +======= +impl ByteRepr for EmptyBody { + fn byte_size() -> usize { + 1 + } + fn to_bytes(&self, buf: &mut [u8]) { + self.s.to_bytes(&mut buf[0..1]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + s: ::from_bytes(&buf[0..1]), + } } } +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] +>>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) +pub struct Templated_char_ { + #[offset(0)] + pub v: u8, +} +<<<<<<< HEAD impl ByteRepr for Templated_char_ {} #[derive(VaArg, FnPtrArg, Default)] -pub struct Templated_int_ { - pub v: Value, -} -impl Clone for Templated_int_ { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*self.v.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() +======= +impl ByteRepr for Templated_char_ { + fn byte_size() -> usize { + 1 + } + fn to_bytes(&self, buf: &mut [u8]) { + self.v.to_bytes(&mut buf[0..1]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + v: ::from_bytes(&buf[0..1]), + } } } +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] +>>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) +pub struct Templated_int_ { + #[offset(0)] + pub v: i32, +} +<<<<<<< HEAD impl ByteRepr for Templated_int_ {} #[derive(VaArg, FnPtrArg, Default)] +======= +impl ByteRepr for Templated_int_ { + fn byte_size() -> usize { + 4 + } + fn to_bytes(&self, buf: &mut [u8]) { + self.v.to_bytes(&mut buf[0..4]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + v: ::from_bytes(&buf[0..4]), + } + } +} +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] +>>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub struct Copied { - pub v: Value, + #[offset(0)] + pub v: i32, } -impl Clone for Copied { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*self.v.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() +<<<<<<< HEAD +impl ByteRepr for Copied {} +======= +impl ByteRepr for Copied { + fn byte_size() -> usize { + 4 + } + fn to_bytes(&self, buf: &mut [u8]) { + self.v.to_bytes(&mut buf[0..4]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + v: ::from_bytes(&buf[0..4]), + } } } -impl ByteRepr for Copied {} +>>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) thread_local!( pub static order_1: Value> = Rc::new(RefCell::new((0..3).map(|_| 0_i32).collect::>())); @@ -141,42 +208,67 @@ thread_local!( thread_local!( pub static order_count_2: Value = Rc::new(RefCell::new(0)); ); -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Tagged { - pub tag: Value, -} -impl Clone for Tagged { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - tag: Rc::new(RefCell::new((*self.tag.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub tag: i32, } +<<<<<<< HEAD impl ByteRepr for Tagged {} #[derive(VaArg, FnPtrArg, Default)] -pub struct Ordered { - pub first: Value, - pub dummy1: Value, - pub second: Value, - pub dummy2: Value, - pub third: Value, -} -impl Clone for Ordered { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - first: Rc::new(RefCell::new((*self.first.borrow()).clone())), - dummy1: Rc::new(RefCell::new((*self.dummy1.borrow()))), - second: Rc::new(RefCell::new((*self.second.borrow()).clone())), - dummy2: Rc::new(RefCell::new((*self.dummy2.borrow()))), - third: Rc::new(RefCell::new((*self.third.borrow()).clone())), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() +======= +impl ByteRepr for Tagged { + fn byte_size() -> usize { + 4 + } + fn to_bytes(&self, buf: &mut [u8]) { + self.tag.to_bytes(&mut buf[0..4]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + tag: ::from_bytes(&buf[0..4]), + } } } +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] +>>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) +pub struct Ordered { + #[offset(0)] + pub first: Tagged, + #[offset(4)] + pub dummy1: i32, + #[offset(8)] + pub second: Tagged, + #[offset(12)] + pub dummy2: i32, + #[offset(16)] + pub third: Tagged, +} +<<<<<<< HEAD impl ByteRepr for Ordered {} +======= +impl ByteRepr for Ordered { + fn byte_size() -> usize { + 20 + } + fn to_bytes(&self, buf: &mut [u8]) { + self.first.to_bytes(&mut buf[0..4]); + self.dummy1.to_bytes(&mut buf[4..8]); + self.second.to_bytes(&mut buf[8..12]); + self.dummy2.to_bytes(&mut buf[12..16]); + self.third.to_bytes(&mut buf[16..20]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + first: ::from_bytes(&buf[0..4]), + dummy1: ::from_bytes(&buf[4..8]), + second: ::from_bytes(&buf[8..12]), + dummy2: ::from_bytes(&buf[12..16]), + third: ::from_bytes(&buf[16..20]), + } + } +} +>>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub fn main() { __cpp2rust_init_globals(); std::process::exit(main_0()); @@ -220,28 +312,20 @@ fn main_0() -> i32 { } assert!((global_0.with(|rc| *rc.borrow()) == 13)); { - let a: Value = Rc::new(RefCell::new(Copied { - v: Rc::new(RefCell::new(5)), - })); + let a: Value = Rc::new(RefCell::new(Copied { v: 5 })); let _dtor_a = ScopedDestructor::new(&a, |__p| __p.destructor()); let b: Value = Rc::new(RefCell::new((*a.borrow()).clone())); let _dtor_b = ScopedDestructor::new(&b, |__p| __p.destructor()); - assert!(((*(*b.borrow()).v.borrow()) == 5)); + assert!(({ (*b.borrow()).v } == 5)); } assert!((global_0.with(|rc| *rc.borrow()) == 15)); { let o: Value = Rc::new(RefCell::new(Ordered { - first: Rc::new(RefCell::new(Tagged { - tag: Rc::new(RefCell::new(1)), - })), - dummy1: Rc::new(RefCell::new(0)), - second: Rc::new(RefCell::new(Tagged { - tag: Rc::new(RefCell::new(2)), - })), - dummy2: Rc::new(RefCell::new(0)), - third: Rc::new(RefCell::new(Tagged { - tag: Rc::new(RefCell::new(3)), - })), + first: Tagged { tag: 1 }, + dummy1: 0, + second: Tagged { tag: 2 }, + dummy2: 0, + third: Tagged { tag: 3 }, })); let _dtor_o = ScopedDestructor::new(&o, |__p| __p.destructor()); } @@ -272,7 +356,7 @@ pub trait ArrayMemberImpl { impl ArrayMemberImpl for Ptr { fn destructor(&self) { { - let __p = (*self.upgrade().deref()).items.as_pointer(); + let __p: Ptr = field_ptr!(self, items); for __i in 0..__p.len() { SImpl::destructor(&__p.offset(__i as isize)); } @@ -292,7 +376,7 @@ pub trait DefaultedImpl { } impl DefaultedImpl for Ptr { fn destructor(&self) { - (*self.upgrade().deref()).s.as_pointer().destructor(); + SImpl::destructor(&field_ptr!(self, s)); } } pub trait EmptyBodyImpl { @@ -300,7 +384,7 @@ pub trait EmptyBodyImpl { } impl EmptyBodyImpl for Ptr { fn destructor(&self) { - (*self.upgrade().deref()).s.as_pointer().destructor(); + SImpl::destructor(&field_ptr!(self, s)); } } pub trait MiddleImpl { @@ -308,7 +392,7 @@ pub trait MiddleImpl { } impl MiddleImpl for Ptr { fn destructor(&self) { - (*self.upgrade().deref()).s.as_pointer().destructor(); + SImpl::destructor(&field_ptr!(self, s)); } } pub trait OrderedImpl { @@ -316,9 +400,9 @@ pub trait OrderedImpl { } impl OrderedImpl for Ptr { fn destructor(&self) { - (*self.upgrade().deref()).third.as_pointer().destructor(); - (*self.upgrade().deref()).second.as_pointer().destructor(); - (*self.upgrade().deref()).first.as_pointer().destructor(); + TaggedImpl::destructor(&field_ptr!(self, third)); + TaggedImpl::destructor(&field_ptr!(self, second)); + TaggedImpl::destructor(&field_ptr!(self, first)); } } pub trait OuterImpl { @@ -326,7 +410,7 @@ pub trait OuterImpl { } impl OuterImpl for Ptr { fn destructor(&self) { - (*self.upgrade().deref()).m.as_pointer().destructor(); + MiddleImpl::destructor(&field_ptr!(self, m)); } } pub trait SImpl { @@ -342,9 +426,9 @@ pub trait TaggedImpl { } impl TaggedImpl for Ptr { fn destructor(&self) { - (*order_1.with(Value::clone).borrow_mut()) - [((*order_count_2.with(Value::clone).borrow_mut()).postfix_inc()) as usize] = - (*(*(*self).upgrade().deref()).tag.borrow()); + let __rhs = (*self).with(|__s: &Tagged| __s.tag); + let __idx = (*order_count_2.with(Value::clone).borrow_mut()).postfix_inc(); + (*order_1.with(Value::clone).borrow_mut())[(__idx) as usize] = __rhs; } } pub trait Templated_char_Impl { diff --git a/tests/unit/out/refcount/doubly_linked_list.rs b/tests/unit/out/refcount/doubly_linked_list.rs index 4e61a525e6..3f886084ee 100644 --- a/tests/unit/out/refcount/doubly_linked_list.rs +++ b/tests/unit/out/refcount/doubly_linked_list.rs @@ -6,37 +6,29 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Node { - pub val: Value, - pub next: Value>, - pub prev: Value>, -} -impl Clone for Node { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - val: Rc::new(RefCell::new((*self.val.borrow()))), - next: Rc::new(RefCell::new((*self.next.borrow()).clone())), - prev: Rc::new(RefCell::new((*self.prev.borrow()).clone())), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub val: i32, + #[offset(8)] + pub next: Ptr, + #[offset(16)] + pub prev: Ptr, } impl ByteRepr for Node { fn byte_size() -> usize { 24 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.val.borrow()).to_bytes(&mut buf[0..4]); - (*self.next.borrow()).to_bytes(&mut buf[8..16]); - (*self.prev.borrow()).to_bytes(&mut buf[16..24]); + self.val.to_bytes(&mut buf[0..4]); + self.next.to_bytes(&mut buf[8..16]); + self.prev.to_bytes(&mut buf[16..24]); } fn from_bytes(buf: &[u8]) -> Self { Self { - val: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - next: Rc::new(RefCell::new(>::from_bytes(&buf[8..16]))), - prev: Rc::new(RefCell::new(>::from_bytes(&buf[16..24]))), + val: ::from_bytes(&buf[0..4]), + next: >::from_bytes(&buf[8..16]), + prev: >::from_bytes(&buf[16..24]), } } } @@ -46,7 +38,7 @@ pub fn Find_0(head: Ptr, idx: i32) -> Ptr { let curr: Value> = Rc::new(RefCell::new((*head.borrow()).clone())); let i: Value = Rc::new(RefCell::new(0)); 'loop_: while ((*i.borrow()) < (*idx.borrow())) { - let __rhs = (*(*(*curr.borrow()).upgrade().deref()).next.borrow()).clone(); + let __rhs = (*curr.borrow()).with(|__s: &Node| (__s.next).clone()); (*curr.borrow_mut()) = __rhs; (*i.borrow_mut()).postfix_inc(); } @@ -58,7 +50,7 @@ pub fn FindBack_1(tail: Ptr, idx: i32) -> Ptr { let curr: Value> = Rc::new(RefCell::new((*tail.borrow()).clone())); let i: Value = Rc::new(RefCell::new(0)); 'loop_: while ((*i.borrow()) < (*idx.borrow())) { - let __rhs = (*(*(*curr.borrow()).upgrade().deref()).prev.borrow()).clone(); + let __rhs = (*curr.borrow()).with(|__s: &Node| (__s.prev).clone()); (*curr.borrow_mut()) = __rhs; (*i.borrow_mut()).postfix_inc(); } @@ -66,8 +58,8 @@ pub fn FindBack_1(tail: Ptr, idx: i32) -> Ptr { } pub fn Append_2(head: Ptr, new_node: Ptr) { let curr: Value> = Rc::new(RefCell::new((head).clone())); - 'loop_: while !((*(*(*curr.borrow()).upgrade().deref()).next.borrow()).is_null()) { - let __rhs = (*(*(*curr.borrow()).upgrade().deref()).next.borrow()).clone(); + 'loop_: while !(((*curr.borrow()).with(|__s: &Node| (__s.next).clone())).is_null()) { + let __rhs = (*curr.borrow()).with(|__s: &Node| (__s.next).clone()); (*curr.borrow_mut()) = __rhs; } ({ NodeImpl::SetNext(&(*curr.borrow()), (new_node).clone()) }); @@ -82,22 +74,20 @@ pub fn Delete_3(head: Ptr, val: i32) -> Ptr { let curr: Value> = Rc::new(RefCell::new((*head.borrow()).clone())); 'loop_: while !((*curr.borrow()).is_null()) { if { - let _lhs = (*(*(*curr.borrow()).upgrade().deref()).val.borrow()); + let _lhs = (*curr.borrow()).with(|__s: &Node| __s.val); _lhs == (*val.borrow()) } { let prev: Value> = Rc::new(RefCell::new( - (*(*(*curr.borrow()).upgrade().deref()).prev.borrow()).clone(), + (*curr.borrow()).with(|__s: &Node| (__s.prev).clone()), )); let next: Value> = Rc::new(RefCell::new( - (*(*(*curr.borrow()).upgrade().deref()).next.borrow()).clone(), + (*curr.borrow()).with(|__s: &Node| (__s.next).clone()), )); if !((*prev.borrow()).is_null()) { - (*(*(*prev.borrow()).upgrade().deref()).next.borrow_mut()) = - (*next.borrow()).clone(); + (*prev.borrow()).with_mut(|__s: &mut Node| __s.next = (*next.borrow()).clone()); } if !((*next.borrow()).is_null()) { - (*(*(*next.borrow()).upgrade().deref()).prev.borrow_mut()) = - (*prev.borrow()).clone(); + (*next.borrow()).with_mut(|__s: &mut Node| __s.prev = (*prev.borrow()).clone()); } if !((*prev.borrow()).is_null()) { return (*head.borrow()).clone(); @@ -105,7 +95,7 @@ pub fn Delete_3(head: Ptr, val: i32) -> Ptr { return (*next.borrow()).clone(); } } - let __rhs = (*(*(*curr.borrow()).upgrade().deref()).next.borrow()).clone(); + let __rhs = (*curr.borrow()).with(|__s: &Node| (__s.next).clone()); (*curr.borrow_mut()) = __rhs; } return (*head.borrow()).clone(); @@ -113,8 +103,8 @@ pub fn Delete_3(head: Ptr, val: i32) -> Ptr { pub fn Tail_4(head: Ptr) -> Ptr { let head: Value> = Rc::new(RefCell::new(head)); let curr: Value> = Rc::new(RefCell::new((*head.borrow()).clone())); - 'loop_: while !((*(*(*curr.borrow()).upgrade().deref()).next.borrow()).is_null()) { - let __rhs = (*(*(*curr.borrow()).upgrade().deref()).next.borrow()).clone(); + 'loop_: while !(((*curr.borrow()).with(|__s: &Node| (__s.next).clone())).is_null()) { + let __rhs = (*curr.borrow()).with(|__s: &Node| (__s.next).clone()); (*curr.borrow_mut()) = __rhs; } return (*curr.borrow()).clone(); @@ -125,45 +115,45 @@ pub fn main() { } fn main_0() -> i32 { let n0: Value = Rc::new(RefCell::new(Node { - val: Rc::new(RefCell::new(5)), - next: Rc::new(RefCell::new(Ptr::::null())), - prev: Rc::new(RefCell::new(Ptr::::null())), + val: 5, + next: Ptr::::null(), + prev: Ptr::::null(), })); let head: Value> = Rc::new(RefCell::new((n0.as_pointer()))); let n1: Value = Rc::new(RefCell::new(Node { - val: Rc::new(RefCell::new(4)), - next: Rc::new(RefCell::new(Ptr::::null())), - prev: Rc::new(RefCell::new(Ptr::::null())), + val: 4, + next: Ptr::::null(), + prev: Ptr::::null(), })); let n2: Value = Rc::new(RefCell::new(Node { - val: Rc::new(RefCell::new(3)), - next: Rc::new(RefCell::new(Ptr::::null())), - prev: Rc::new(RefCell::new(Ptr::::null())), + val: 3, + next: Ptr::::null(), + prev: Ptr::::null(), })); let n3: Value = Rc::new(RefCell::new(Node { - val: Rc::new(RefCell::new(2)), - next: Rc::new(RefCell::new(Ptr::::null())), - prev: Rc::new(RefCell::new(Ptr::::null())), + val: 2, + next: Ptr::::null(), + prev: Ptr::::null(), })); let n4: Value = Rc::new(RefCell::new(Node { - val: Rc::new(RefCell::new(1)), - next: Rc::new(RefCell::new(Ptr::::null())), - prev: Rc::new(RefCell::new(Ptr::::null())), + val: 1, + next: Ptr::::null(), + prev: Ptr::::null(), })); let n5: Value = Rc::new(RefCell::new(Node { - val: Rc::new(RefCell::new(0)), - next: Rc::new(RefCell::new(Ptr::::null())), - prev: Rc::new(RefCell::new(Ptr::::null())), + val: 0, + next: Ptr::::null(), + prev: Ptr::::null(), })); let n6: Value = Rc::new(RefCell::new(Node { - val: Rc::new(RefCell::new(-1_i32)), - next: Rc::new(RefCell::new(Ptr::::null())), - prev: Rc::new(RefCell::new(Ptr::::null())), + val: -1_i32, + next: Ptr::::null(), + prev: Ptr::::null(), })); let n7: Value = Rc::new(RefCell::new(Node { - val: Rc::new(RefCell::new(-2_i32)), - next: Rc::new(RefCell::new(Ptr::::null())), - prev: Rc::new(RefCell::new(Ptr::::null())), + val: -2_i32, + next: Ptr::::null(), + prev: Ptr::::null(), })); ({ let _head: Ptr = (*head.borrow()).clone(); @@ -207,228 +197,84 @@ fn main_0() -> i32 { let __rhs = ({ Delete_3((*head.borrow()).clone(), -2_i32) }); (*head.borrow_mut()) = __rhs; let tail: Value> = Rc::new(RefCell::new(({ Tail_4((*head.borrow()).clone()) }))); - assert!( - ((*(*({ Find_0((*head.borrow()).clone(), 0,) }).upgrade().deref()) - .val - .borrow()) - == 4) - ); - assert!( - ((*(*({ Find_0((*head.borrow()).clone(), 1,) }).upgrade().deref()) - .val - .borrow()) - == 3) - ); - assert!( - ((*(*({ Find_0((*head.borrow()).clone(), 2,) }).upgrade().deref()) - .val - .borrow()) - == 2) - ); - assert!( - ((*(*({ Find_0((*head.borrow()).clone(), 3,) }).upgrade().deref()) - .val - .borrow()) - == 1) - ); - assert!( - ((*(*({ Find_0((*head.borrow()).clone(), 4,) }).upgrade().deref()) - .val - .borrow()) - == -1_i32) - ); + assert!((({ Find_0((*head.borrow()).clone(), 0,) }).with(|__s: &Node| __s.val) == 4)); + assert!((({ Find_0((*head.borrow()).clone(), 1,) }).with(|__s: &Node| __s.val) == 3)); + assert!((({ Find_0((*head.borrow()).clone(), 2,) }).with(|__s: &Node| __s.val) == 2)); + assert!((({ Find_0((*head.borrow()).clone(), 3,) }).with(|__s: &Node| __s.val) == 1)); + assert!((({ Find_0((*head.borrow()).clone(), 4,) }).with(|__s: &Node| __s.val) == -1_i32)); assert!(({ Find_0((*head.borrow()).clone(), 5,) }).is_null()); + assert!((({ FindBack_1((*tail.borrow()).clone(), 0,) }).with(|__s: &Node| __s.val) == -1_i32)); + assert!((({ FindBack_1((*tail.borrow()).clone(), 1,) }).with(|__s: &Node| __s.val) == 1)); + assert!((({ FindBack_1((*tail.borrow()).clone(), 2,) }).with(|__s: &Node| __s.val) == 2)); + assert!((({ FindBack_1((*tail.borrow()).clone(), 3,) }).with(|__s: &Node| __s.val) == 3)); + assert!((({ FindBack_1((*tail.borrow()).clone(), 4,) }).with(|__s: &Node| __s.val) == 4)); assert!( - ((*(*({ FindBack_1((*tail.borrow()).clone(), 0,) }) - .upgrade() - .deref()) - .val - .borrow()) - == -1_i32) - ); - assert!( - ((*(*({ FindBack_1((*tail.borrow()).clone(), 1,) }) - .upgrade() - .deref()) - .val - .borrow()) - == 1) - ); - assert!( - ((*(*({ FindBack_1((*tail.borrow()).clone(), 2,) }) - .upgrade() - .deref()) - .val - .borrow()) - == 2) + (({ FindBack_1((*tail.borrow()).clone(), 4,) }).with(|__s: &Node| (__s.prev).clone())) + .is_null() ); assert!( - ((*(*({ FindBack_1((*tail.borrow()).clone(), 3,) }) - .upgrade() - .deref()) - .val - .borrow()) + (({ Find_0((*head.borrow()).clone(), 0,) }) + .with(|__s: &Node| (__s.next).clone()) + .with(|__s: &Node| __s.val) == 3) ); assert!( - ((*(*({ FindBack_1((*tail.borrow()).clone(), 4,) }) - .upgrade() - .deref()) - .val - .borrow()) - == 4) - ); - assert!( - (*(*({ FindBack_1((*tail.borrow()).clone(), 4,) }) - .upgrade() - .deref()) - .prev - .borrow()) - .is_null() - ); - assert!( - ((*(*(*(*({ Find_0((*head.borrow()).clone(), 0,) }).upgrade().deref()) - .next - .borrow()) - .upgrade() - .deref()) - .val - .borrow()) - == 3) - ); - assert!( - ((*(*(*(*(*(*({ Find_0((*head.borrow()).clone(), 1,) }).upgrade().deref()) - .next - .borrow()) - .upgrade() - .deref()) - .next - .borrow()) - .upgrade() - .deref()) - .val - .borrow()) + (({ Find_0((*head.borrow()).clone(), 1,) }) + .with(|__s: &Node| (__s.next).clone()) + .with(|__s: &Node| (__s.next).clone()) + .with(|__s: &Node| __s.val) == 1) ); assert!( - ((*(*(*(*({ Find_0((*head.borrow()).clone(), 2,) }).upgrade().deref()) - .prev - .borrow()) - .upgrade() - .deref()) - .val - .borrow()) + (({ Find_0((*head.borrow()).clone(), 2,) }) + .with(|__s: &Node| (__s.prev).clone()) + .with(|__s: &Node| __s.val) == 3) ); assert!( - (*(*({ Find_0((*head.borrow()).clone(), 4,) }).upgrade().deref()) - .next - .borrow()) - .is_null() + (({ Find_0((*head.borrow()).clone(), 4,) }).with(|__s: &Node| (__s.next).clone())) + .is_null() ); assert!( - ((*(*(*(*(*(*({ FindBack_1((*tail.borrow()).clone(), 1,) }) - .upgrade() - .deref()) - .prev - .borrow()) - .upgrade() - .deref()) - .prev - .borrow()) - .upgrade() - .deref()) - .val - .borrow()) + (({ FindBack_1((*tail.borrow()).clone(), 1,) }) + .with(|__s: &Node| (__s.prev).clone()) + .with(|__s: &Node| (__s.prev).clone()) + .with(|__s: &Node| __s.val) == 3) ); - (*(*(*(*({ Find_0((*head.borrow()).clone(), 0) }).upgrade().deref()) - .next - .borrow()) - .upgrade() - .deref()) - .val - .borrow_mut()) = 30; - assert!( - ((*(*({ Find_0((*head.borrow()).clone(), 1,) }).upgrade().deref()) - .val - .borrow()) - == 30) - ); - let __rhs = ((*(*({ Find_0((*head.borrow()).clone(), 0) }).upgrade().deref()) - .val - .borrow()) - + (*(*({ Find_0((*head.borrow()).clone(), 3) }).upgrade().deref()) - .val - .borrow())); - (*(*(*(*({ Find_0((*head.borrow()).clone(), 1) }).upgrade().deref()) - .next - .borrow()) - .upgrade() - .deref()) - .val - .borrow_mut()) = __rhs; - assert!( - ((*(*({ Find_0((*head.borrow()).clone(), 2,) }).upgrade().deref()) - .val - .borrow()) - == (4 + 1)) - ); + ({ Find_0((*head.borrow()).clone(), 0) }) + .with(|__s: &Node| (__s.next).clone()) + .with_mut(|__s: &mut Node| __s.val = 30); + assert!((({ Find_0((*head.borrow()).clone(), 1,) }).with(|__s: &Node| __s.val) == 30)); + let __rhs = (({ Find_0((*head.borrow()).clone(), 0) }).with(|__s: &Node| __s.val) + + ({ Find_0((*head.borrow()).clone(), 3) }).with(|__s: &Node| __s.val)); + ({ Find_0((*head.borrow()).clone(), 1) }) + .with(|__s: &Node| (__s.next).clone()) + .with_mut(|__s: &mut Node| __s.val = __rhs); + assert!((({ Find_0((*head.borrow()).clone(), 2,) }).with(|__s: &Node| __s.val) == (4 + 1))); let sum: Value = Rc::new(RefCell::new( - (((((*(*({ Find_0((*head.borrow()).clone(), 0) }).upgrade().deref()) - .val - .borrow()) - + (*(*({ Find_0((*head.borrow()).clone(), 1) }).upgrade().deref()) - .val - .borrow())) - + (*(*({ Find_0((*head.borrow()).clone(), 2) }).upgrade().deref()) - .val - .borrow())) - + (*(*({ Find_0((*head.borrow()).clone(), 3) }).upgrade().deref()) - .val - .borrow())) - + (*(*({ Find_0((*head.borrow()).clone(), 4) }).upgrade().deref()) - .val - .borrow())), + ((((({ Find_0((*head.borrow()).clone(), 0) }).with(|__s: &Node| __s.val) + + ({ Find_0((*head.borrow()).clone(), 1) }).with(|__s: &Node| __s.val)) + + ({ Find_0((*head.borrow()).clone(), 2) }).with(|__s: &Node| __s.val)) + + ({ Find_0((*head.borrow()).clone(), 3) }).with(|__s: &Node| __s.val)) + + ({ Find_0((*head.borrow()).clone(), 4) }).with(|__s: &Node| __s.val)), )); assert!(((*sum.borrow()) == ((((4 + 30) + 5) + 1) + -1_i32))); assert!( ({ - let _lhs = (*(*({ Find_0((*head.borrow()).clone(), 0) }).upgrade().deref()) - .val - .borrow()); - _lhs + (*(*({ FindBack_1((*tail.borrow()).clone(), 0) }) - .upgrade() - .deref()) - .val - .borrow()) + let _lhs = ({ Find_0((*head.borrow()).clone(), 0) }).with(|__s: &Node| __s.val); + _lhs + ({ FindBack_1((*tail.borrow()).clone(), 0) }).with(|__s: &Node| __s.val) } == (4 + -1_i32)) ); assert!({ - let _lhs = (*(*(*(*({ Find_0((*head.borrow()).clone(), 2) }).upgrade().deref()) - .next - .borrow()) - .upgrade() - .deref()) - .val - .borrow()); - _lhs == (*(*({ FindBack_1((*tail.borrow()).clone(), 1) }) - .upgrade() - .deref()) - .val - .borrow()) + let _lhs = ({ Find_0((*head.borrow()).clone(), 2) }) + .with(|__s: &Node| (__s.next).clone()) + .with(|__s: &Node| __s.val); + _lhs == ({ FindBack_1((*tail.borrow()).clone(), 1) }).with(|__s: &Node| __s.val) }); assert!({ - let _lhs = (*(*({ Find_0((*head.borrow()).clone(), 0) }).upgrade().deref()) - .prev - .borrow()) - .clone(); - _lhs == (*(*({ FindBack_1((*tail.borrow()).clone(), 4) }) - .upgrade() - .deref()) - .prev - .borrow()) - .clone() + let _lhs = ({ Find_0((*head.borrow()).clone(), 0) }).with(|__s: &Node| (__s.prev).clone()); + _lhs == ({ FindBack_1((*tail.borrow()).clone(), 4) }).with(|__s: &Node| (__s.prev).clone()) }); return 0; } @@ -439,11 +285,11 @@ pub trait NodeImpl { impl NodeImpl for Ptr { fn SetNext(&self, n: Ptr) { let n: Value> = Rc::new(RefCell::new(n)); - (*(*(*self).upgrade().deref()).next.borrow_mut()) = (*n.borrow()).clone(); + (*self).with_mut(|__s: &mut Node| __s.next = (*n.borrow()).clone()); } fn SetPrev(&self, p: Ptr) { let p: Value> = Rc::new(RefCell::new(p)); - (*(*(*self).upgrade().deref()).prev.borrow_mut()) = (*p.borrow()).clone(); + (*self).with_mut(|__s: &mut Node| __s.prev = (*p.borrow()).clone()); } } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/enum_default_in_static.rs b/tests/unit/out/refcount/enum_default_in_static.rs index abb2af13fc..d49af4772d 100644 --- a/tests/unit/out/refcount/enum_default_in_static.rs +++ b/tests/unit/out/refcount/enum_default_in_static.rs @@ -10,31 +10,25 @@ pub type Mode = u32; pub const Mode_MODE_NONE: Mode = 0; pub const Mode_MODE_ONE: Mode = 1; pub const Mode_MODE_TWO: Mode = 2; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Config { - pub count: Value, - pub mode: Value, -} -impl Clone for Config { - fn clone(&self) -> Self { - Self { - count: Rc::new(RefCell::new((*self.count.borrow()).clone())), - mode: Rc::new(RefCell::new((*self.mode.borrow()).clone())), - } - } + #[offset(0)] + pub count: i32, + #[offset(4)] + pub mode: Mode, } impl ByteRepr for Config { fn byte_size() -> usize { 8 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.count.borrow()).to_bytes(&mut buf[0..4]); - (*self.mode.borrow()).to_bytes(&mut buf[4..8]); + self.count.to_bytes(&mut buf[0..4]); + self.mode.to_bytes(&mut buf[4..8]); } fn from_bytes(buf: &[u8]) -> Self { Self { - count: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - mode: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + count: ::from_bytes(&buf[0..4]), + mode: ::from_bytes(&buf[4..8]), } } } @@ -46,9 +40,9 @@ pub fn main() { std::process::exit(main_0()); } fn main_0() -> i32 { - assert!(((((*(*config_0.with(Value::clone).borrow()).count.borrow()) == 0) as i32) != 0)); + assert!(((({ (*config_0.with(Value::clone).borrow()).count } == 0) as i32) != 0)); assert!( - (((((*(*config_0.with(Value::clone).borrow()).mode.borrow()) as u32) + (((({ (*config_0.with(Value::clone).borrow()).mode } as u32) == ((Mode_MODE_NONE as i32) as u32)) as i32) != 0) ); diff --git a/tests/unit/out/refcount/enum_int_interop.rs b/tests/unit/out/refcount/enum_int_interop.rs index 6c00eff4d9..5f1e1e7450 100644 --- a/tests/unit/out/refcount/enum_int_interop.rs +++ b/tests/unit/out/refcount/enum_int_interop.rs @@ -19,37 +19,29 @@ pub type Tag = u32; pub const Tag_TAG_ZERO: Tag = 0; pub const Tag_TAG_ONE: Tag = 1; pub const Tag_TAG_TWO: Tag = 2; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Entry { - pub name: Value>, - pub color: Value, - pub opt: Value

(), + )); + (*view.borrow()).with_mut(|__s: &mut Header| __s.tag = 16909060); + assert!( + (((*bytes.borrow())[(0) as usize] as i32) == 4) + || (((*bytes.borrow())[(3) as usize] as i32) == 4) + ); + ({ set_0((field_ptr!((*view.borrow()), tag)), 0) }); + assert!( + (((*bytes.borrow())[(0) as usize] as i32) == 0) + && (((*bytes.borrow())[(3) as usize] as i32) == 0) + ); + (*view.borrow()).with_mut(|__s: &mut Header| __s.size = 1_i16); + assert!( + ((((*bytes.borrow())[(4) as usize] as i32) + ((*bytes.borrow())[(5) as usize] as i32)) + == 1) + ); + return 0; +} +pub trait OuterImpl { + fn next(&self) -> i32; + fn sum(&self) -> i32; + fn push(&self, k: i32); +} +impl OuterImpl for Ptr { + fn next(&self) -> i32 { + return ({ + let __idx = (*self).with_mut(|__s: &mut Outer| __s.x.postfix_inc()); + (*(*self).upgrade().deref()).buf[(__idx) as usize] + }); + } + fn sum(&self) -> i32 { + return ((*self).with(|__s: &Outer| __s.x) + (*self).with(|__s: &Outer| __s.inner.a)); + } + fn push(&self, k: i32) { + let k: Value = Rc::new(RefCell::new(k)); + { + let __a1 = ((*k.borrow()) + (*self).with(|__s: &Outer| __s.x)); + (*self).with(|__s: &Outer| (*__s.v.borrow_mut()).push(__a1)) + }; + } +} +pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/fn_ptr_cast.rs b/tests/unit/out/refcount/fn_ptr_cast.rs index 0451f4b740..9f48278ebb 100644 --- a/tests/unit/out/refcount/fn_ptr_cast.rs +++ b/tests/unit/out/refcount/fn_ptr_cast.rs @@ -36,37 +36,29 @@ pub fn test_double_cast_2() { _lhs == (*fn_.borrow()).clone() }); } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Command { - pub data: Value, -} -impl Clone for Command { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - data: Rc::new(RefCell::new((*self.data.borrow()).clone())), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub data: AnyPtr, } impl ByteRepr for Command { fn byte_size() -> usize { 8 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.data.borrow()).to_bytes(&mut buf[0..8]); + self.data.to_bytes(&mut buf[0..8]); } fn from_bytes(buf: &[u8]) -> Self { Self { - data: Rc::new(RefCell::new(::from_bytes(&buf[0..8]))), + data: ::from_bytes(&buf[0..8]), } } } pub fn test_void_ptr_to_fn_3() { let cmd: Value = Rc::new(RefCell::new(::default())); - (*(*cmd.borrow()).data.borrow_mut()) = FnPtr:: i32>::new(double_it_0).to_any(); + (*cmd.borrow_mut()).data = FnPtr:: i32>::new(double_it_0).to_any(); let fn_: Value i32>> = Rc::new(RefCell::new( - (*(*cmd.borrow()).data.borrow()) + { (*cmd.borrow()).data.clone() } .cast_fn:: i32>() .expect("ub:wrong fn type"), )); diff --git a/tests/unit/out/refcount/fn_ptr_stable_sort.rs b/tests/unit/out/refcount/fn_ptr_stable_sort.rs index fe8d3ce96a..0dbf4f954d 100644 --- a/tests/unit/out/refcount/fn_ptr_stable_sort.rs +++ b/tests/unit/out/refcount/fn_ptr_stable_sort.rs @@ -6,40 +6,32 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Item { - pub key: Value, - pub value: Value, -} -impl Clone for Item { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - key: Rc::new(RefCell::new((*self.key.borrow()))), - value: Rc::new(RefCell::new((*self.value.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub key: i32, + #[offset(4)] + pub value: i32, } impl ByteRepr for Item { fn byte_size() -> usize { 8 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.key.borrow()).to_bytes(&mut buf[0..4]); - (*self.value.borrow()).to_bytes(&mut buf[4..8]); + self.key.to_bytes(&mut buf[0..4]); + self.value.to_bytes(&mut buf[4..8]); } fn from_bytes(buf: &[u8]) -> Self { Self { - key: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - value: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + key: ::from_bytes(&buf[0..4]), + value: ::from_bytes(&buf[4..8]), } } } pub fn Compare_0(a: Ptr, b: Ptr) -> bool { return { - let _lhs = (*(*a.upgrade().deref()).key.borrow()); - _lhs < (*(*b.upgrade().deref()).key.borrow()) + let _lhs = a.with(|__s: &Item| __s.key); + _lhs < b.with(|__s: &Item| __s.key) }; } pub fn main() { @@ -48,47 +40,29 @@ pub fn main() { } fn main_0() -> i32 { let v: Value> = Rc::new(RefCell::new(Vec::new())); - (*v.borrow_mut()).push(Item { - key: Rc::new(RefCell::new(3)), - value: Rc::new(RefCell::new(30)), - }); - (*v.borrow_mut()).push(Item { - key: Rc::new(RefCell::new(1)), - value: Rc::new(RefCell::new(10)), - }); - (*v.borrow_mut()).push(Item { - key: Rc::new(RefCell::new(2)), - value: Rc::new(RefCell::new(20)), - }); + (*v.borrow_mut()).push(Item { key: 3, value: 30 }); + (*v.borrow_mut()).push(Item { key: 1, value: 10 }); + (*v.borrow_mut()).push(Item { key: 2, value: 20 }); (v.as_pointer() as Ptr).sort_with_cmp( (v.as_pointer() as Ptr).to_end().get_offset(), |x, y| Compare_0.call(x, y), ); assert!( - ((*(*(v.as_pointer() as Ptr) + ((v.as_pointer() as Ptr) .offset(0_usize) - .upgrade() - .deref()) - .key - .borrow()) + .with(|__s: &Item| __s.key) == 1) ); assert!( - ((*(*(v.as_pointer() as Ptr) + ((v.as_pointer() as Ptr) .offset(1_usize) - .upgrade() - .deref()) - .key - .borrow()) + .with(|__s: &Item| __s.key) == 2) ); assert!( - ((*(*(v.as_pointer() as Ptr) + ((v.as_pointer() as Ptr) .offset(2_usize) - .upgrade() - .deref()) - .key - .borrow()) + .with(|__s: &Item| __s.key) == 3) ); return 0; diff --git a/tests/unit/out/refcount/fn_ptr_struct.rs b/tests/unit/out/refcount/fn_ptr_struct.rs index e7b03a5606..d4cd90492a 100644 --- a/tests/unit/out/refcount/fn_ptr_struct.rs +++ b/tests/unit/out/refcount/fn_ptr_struct.rs @@ -6,26 +6,18 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg)] +#[derive(Clone, Record, VaArg, FnPtrArg)] pub struct Handler { - pub tag: Value, - pub cb: Value i32>>, -} -impl Clone for Handler { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - tag: Rc::new(RefCell::new((*self.tag.borrow()))), - cb: Rc::new(RefCell::new((*self.cb.borrow()).clone())), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub tag: i32, + #[offset(8)] + pub cb: FnPtr i32>, } impl Default for Handler { fn default() -> Self { Handler { - tag: Rc::new(RefCell::new(0_i32)), - cb: Rc::new(RefCell::new(FnPtr:: i32>::null())), + tag: 0_i32, + cb: FnPtr:: i32>::null(), } } } @@ -34,15 +26,13 @@ impl ByteRepr for Handler { 16 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.tag.borrow()).to_bytes(&mut buf[0..4]); - (*self.cb.borrow()).to_bytes(&mut buf[8..16]); + self.tag.to_bytes(&mut buf[0..4]); + self.cb.to_bytes(&mut buf[8..16]); } fn from_bytes(buf: &[u8]) -> Self { Self { - tag: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - cb: Rc::new(RefCell::new( i32>>::from_bytes( - &buf[8..16], - ))), + tag: ::from_bytes(&buf[0..4]), + cb: i32>>::from_bytes(&buf[8..16]), } } } @@ -54,7 +44,7 @@ pub fn negate_1(x: i32) -> i32 { let x: Value = Rc::new(RefCell::new(x)); return -(*x.borrow()); } -#[derive(Clone, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] pub struct S {} impl S { pub fn pick_1(x: i32) -> i32 { @@ -83,26 +73,26 @@ fn main_0() -> i32 { assert!((({ (*p2.borrow()).call(5,) }) == 8)); assert!((({ S::pick_2(5_i64,) }) == 7)); let h3: Value = Rc::new(RefCell::new(Handler { - tag: Rc::new(RefCell::new(3)), - cb: Rc::new(RefCell::new((FnPtr:: i32>::new(S::pick_1)))), + tag: 3, + cb: (FnPtr:: i32>::new(S::pick_1)), })); - assert!((({ (*(*h3.borrow()).cb.borrow()).call(1,) }) == 2)); + assert!((({ { (*h3.borrow()).cb.clone() }.call(1,) }) == 2)); let h1: Value = Rc::new(RefCell::new(Handler { - tag: Rc::new(RefCell::new(1)), - cb: Rc::new(RefCell::new(FnPtr:: i32>::new(double_it_0))), + tag: 1, + cb: FnPtr:: i32>::new(double_it_0), })); let h2: Value = Rc::new(RefCell::new(Handler { - tag: Rc::new(RefCell::new(2)), - cb: Rc::new(RefCell::new(FnPtr:: i32>::new(negate_1))), + tag: 2, + cb: FnPtr:: i32>::new(negate_1), })); - assert!(!((*(*h1.borrow()).cb.borrow()).is_null())); - assert!((({ (*(*h1.borrow()).cb.borrow()).call(5,) }) == 10)); - assert!((({ (*(*h2.borrow()).cb.borrow()).call(7,) }) == -7_i32)); - (*(*h1.borrow()).cb.borrow_mut()) = FnPtr:: i32>::new(negate_1); - assert!((({ (*(*h1.borrow()).cb.borrow()).call(3,) }) == -3_i32)); + assert!(!(({ (*h1.borrow()).cb.clone() }).is_null())); + assert!((({ { (*h1.borrow()).cb.clone() }.call(5,) }) == 10)); + assert!((({ { (*h2.borrow()).cb.clone() }.call(7,) }) == -7_i32)); + (*h1.borrow_mut()).cb = FnPtr:: i32>::new(negate_1); + assert!((({ { (*h1.borrow()).cb.clone() }.call(3,) }) == -3_i32)); assert!({ - let _lhs = (*(*h1.borrow()).cb.borrow()).clone(); - _lhs == (*(*h2.borrow()).cb.borrow()).clone() + let _lhs = { (*h1.borrow()).cb.clone() }; + _lhs == { (*h2.borrow()).cb.clone() } }); return 0; } diff --git a/tests/unit/out/refcount/fn_ptr_typedef_cast.rs b/tests/unit/out/refcount/fn_ptr_typedef_cast.rs index 87a4de20d3..4174aa1585 100644 --- a/tests/unit/out/refcount/fn_ptr_typedef_cast.rs +++ b/tests/unit/out/refcount/fn_ptr_typedef_cast.rs @@ -22,45 +22,39 @@ pub fn via_size_t_return_3(b: u64) -> usize { let b: Value = Rc::new(RefCell::new(b)); return ((*b.borrow()) as usize); } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct pair { - pub a: Value, - pub b: Value, -} -impl Clone for pair { - fn clone(&self) -> Self { - Self { - a: Rc::new(RefCell::new((*self.a.borrow()).clone())), - b: Rc::new(RefCell::new((*self.b.borrow()).clone())), - } - } + #[offset(0)] + pub a: i32, + #[offset(4)] + pub b: i32, } impl ByteRepr for pair { fn byte_size() -> usize { 8 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.a.borrow()).to_bytes(&mut buf[0..4]); - (*self.b.borrow()).to_bytes(&mut buf[4..8]); + self.a.to_bytes(&mut buf[0..4]); + self.b.to_bytes(&mut buf[4..8]); } fn from_bytes(buf: &[u8]) -> Self { Self { - a: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - b: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + a: ::from_bytes(&buf[0..4]), + b: ::from_bytes(&buf[4..8]), } } } pub fn pair_scaled_4(p: pair, n: usize) -> u64 { let p: Value = Rc::new(RefCell::new(p)); let n: Value = Rc::new(RefCell::new(n)); - return ((((*(*p.borrow()).a.borrow()) as usize).wrapping_mul((*n.borrow()))) - .wrapping_add(((*(*p.borrow()).b.borrow()) as usize)) as u64); + return ((({ (*p.borrow()).a } as usize).wrapping_mul((*n.borrow()))) + .wrapping_add(({ (*p.borrow()).b } as usize)) as u64); } pub fn make_pair_5(n: usize) -> pair { let n: Value = Rc::new(RefCell::new(n)); let p: Value = Rc::new(RefCell::new(pair { - a: Rc::new(RefCell::new(((*n.borrow()) as i32))), - b: Rc::new(RefCell::new((((*n.borrow()) as i32) * 2))), + a: ((*n.borrow()) as i32), + b: (((*n.borrow()) as i32) * 2), })); return (*p.borrow()).clone(); } @@ -107,10 +101,7 @@ fn main_0() -> i32 { let scaled: Value u64>> = Rc::new(RefCell::new( FnPtr:: u64>::new(pair_scaled_4).cast:: u64>(), )); - let p: Value = Rc::new(RefCell::new(pair { - a: Rc::new(RefCell::new(3)), - b: Rc::new(RefCell::new(4)), - })); + let p: Value = Rc::new(RefCell::new(pair { a: 3, b: 4 })); assert!( (((({ (*scaled.borrow()).call((*p.borrow()).clone(), 10_u64,) }) == 34_u64) as i32) != 0) ); @@ -118,8 +109,8 @@ fn main_0() -> i32 { FnPtr:: pair>::new(make_pair_5).cast:: pair>(), )); let q: Value = Rc::new(RefCell::new(({ (*make.borrow()).call(5_u64) }))); - assert!(((((*(*q.borrow()).a.borrow()) == 5) as i32) != 0)); - assert!(((((*(*q.borrow()).b.borrow()) == 10) as i32) != 0)); + assert!(((({ (*q.borrow()).a } == 5) as i32) != 0)); + assert!(((({ (*q.borrow()).b } == 10) as i32) != 0)); return 0; } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/fn_ptr_vtable.rs b/tests/unit/out/refcount/fn_ptr_vtable.rs index b59a2ffe91..cc15a0b851 100644 --- a/tests/unit/out/refcount/fn_ptr_vtable.rs +++ b/tests/unit/out/refcount/fn_ptr_vtable.rs @@ -6,29 +6,21 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg)] +#[derive(Clone, Record, VaArg, FnPtrArg)] pub struct Vtable { - pub create: Value AnyPtr>>, - pub get: Value i32>>, - pub destroy: Value>, -} -impl Clone for Vtable { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - create: Rc::new(RefCell::new((*self.create.borrow()).clone())), - get: Rc::new(RefCell::new((*self.get.borrow()).clone())), - destroy: Rc::new(RefCell::new((*self.destroy.borrow()).clone())), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub create: FnPtr AnyPtr>, + #[offset(8)] + pub get: FnPtr i32>, + #[offset(16)] + pub destroy: FnPtr, } impl Default for Vtable { fn default() -> Self { Vtable { - create: Rc::new(RefCell::new(FnPtr:: AnyPtr>::null())), - get: Rc::new(RefCell::new(FnPtr:: i32>::null())), - destroy: Rc::new(RefCell::new(FnPtr::::null())), + create: FnPtr:: AnyPtr>::null(), + get: FnPtr:: i32>::null(), + destroy: FnPtr::::null(), } } } @@ -37,19 +29,15 @@ impl ByteRepr for Vtable { 24 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.create.borrow()).to_bytes(&mut buf[0..8]); - (*self.get.borrow()).to_bytes(&mut buf[8..16]); - (*self.destroy.borrow()).to_bytes(&mut buf[16..24]); + self.create.to_bytes(&mut buf[0..8]); + self.get.to_bytes(&mut buf[8..16]); + self.destroy.to_bytes(&mut buf[16..24]); } fn from_bytes(buf: &[u8]) -> Self { Self { - create: Rc::new(RefCell::new( AnyPtr>>::from_bytes( - &buf[0..8], - ))), - get: Rc::new(RefCell::new( i32>>::from_bytes( - &buf[8..16], - ))), - destroy: Rc::new(RefCell::new(>::from_bytes(&buf[16..24]))), + create: AnyPtr>>::from_bytes(&buf[0..8]), + get: i32>>::from_bytes(&buf[8..16]), + destroy: >::from_bytes(&buf[16..24]), } } } @@ -75,21 +63,21 @@ pub fn main() { } fn main_0() -> i32 { let vt: Value = Rc::new(RefCell::new(Vtable { - create: Rc::new(RefCell::new(FnPtr:: AnyPtr>::new(int_create_1))), - get: Rc::new(RefCell::new(FnPtr:: i32>::new(int_get_2))), - destroy: Rc::new(RefCell::new(FnPtr::::new(int_destroy_3))), + create: FnPtr:: AnyPtr>::new(int_create_1), + get: FnPtr:: i32>::new(int_get_2), + destroy: FnPtr::::new(int_destroy_3), })); - assert!(!((*(*vt.borrow()).create.borrow()).is_null())); - assert!(!((*(*vt.borrow()).get.borrow()).is_null())); - assert!(!((*(*vt.borrow()).destroy.borrow()).is_null())); + assert!(!(({ (*vt.borrow()).create.clone() }).is_null())); + assert!(!(({ (*vt.borrow()).get.clone() }).is_null())); + assert!(!(({ (*vt.borrow()).destroy.clone() }).is_null())); let obj: Value = Rc::new(RefCell::new( - ({ (*(*vt.borrow()).create.borrow()).call(42) }), + ({ { (*vt.borrow()).create.clone() }.call(42) }), )); - assert!((({ (*(*vt.borrow()).get.borrow()).call((*obj.borrow()).clone(),) }) == 42)); - ({ (*(*vt.borrow()).destroy.borrow()).call((*obj.borrow()).clone()) }); + assert!((({ { (*vt.borrow()).get.clone() }.call((*obj.borrow()).clone(),) }) == 42)); + ({ { (*vt.borrow()).destroy.clone() }.call((*obj.borrow()).clone()) }); assert!((storage_0.with(|rc| *rc.borrow()) == 0)); - (*(*vt.borrow()).get.borrow_mut()) = FnPtr:: i32>::null(); - assert!((*(*vt.borrow()).get.borrow()).is_null()); + (*vt.borrow_mut()).get = FnPtr:: i32>::null(); + assert!(({ (*vt.borrow()).get.clone() }).is_null()); return 0; } pub fn __cpp2rust_init_globals() { diff --git a/tests/unit/out/refcount/foreach_disjoint_field_borrow.rs b/tests/unit/out/refcount/foreach_disjoint_field_borrow.rs index 2d28a70244..80d298191e 100644 --- a/tests/unit/out/refcount/foreach_disjoint_field_borrow.rs +++ b/tests/unit/out/refcount/foreach_disjoint_field_borrow.rs @@ -6,22 +6,42 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Record, VaArg, FnPtrArg, Default)] pub struct S { + #[offset(0)] pub v: Value>, - pub a: Value, + #[offset(24)] + pub a: i32, } impl Clone for S { fn clone(&self) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*self.v.borrow()).clone())), - a: Rc::new(RefCell::new((*self.a.borrow()))), + v: { Rc::new(RefCell::new((*self.v.borrow()).clone())) }, + a: { self.a }, })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } } +<<<<<<< HEAD impl ByteRepr for S {} +======= +impl ByteRepr for S { + fn byte_size() -> usize { + 32 + } + fn to_bytes(&self, buf: &mut [u8]) { + self.v.to_bytes(&mut buf[0..24]); + self.a.to_bytes(&mut buf[24..28]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + v: >>::from_bytes(&buf[0..24]), + a: ::from_bytes(&buf[24..28]), + } + } +} +>>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub fn main() { __cpp2rust_init_globals(); std::process::exit(main_0()); @@ -29,9 +49,9 @@ pub fn main() { 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 { + 'loop_: for mut e in { (*s.borrow()).v.as_pointer() } as Ptr { let e: Value = Rc::new(RefCell::new(e.read())); - (*(*s.borrow()).a.borrow_mut()).postfix_inc(); + (*s.borrow_mut()).a.postfix_inc(); } return 0; } diff --git a/tests/unit/out/refcount/forward_basic.rs b/tests/unit/out/refcount/forward_basic.rs index a469279268..a92eb3764b 100644 --- a/tests/unit/out/refcount/forward_basic.rs +++ b/tests/unit/out/refcount/forward_basic.rs @@ -9,42 +9,43 @@ use std::rc::{Rc, Weak}; pub type Overload = u32; pub const Overload_kLvalueOverload: Overload = 1; pub const Overload_kRvalueOverload: Overload = 2; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Record, VaArg, FnPtrArg, Default)] pub struct Tracked { - pub v: Value, - pub copies: Value, - pub moves: Value, + #[offset(0)] + pub v: i32, + #[offset(4)] + pub copies: i32, + #[offset(8)] + pub moves: i32, } impl Tracked { pub fn new(v: i32) -> Self { let v: Value = Rc::new(RefCell::new(v)); let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*v.borrow()))), - copies: Rc::new(RefCell::new(0)), - moves: Rc::new(RefCell::new(0)), + v: (*v.borrow()), + copies: 0, + moves: 0, })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } pub fn copy_from(o: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*(*o.upgrade().deref()).v.borrow()))), - copies: Rc::new(RefCell::new( - ((*(*o.upgrade().deref()).copies.borrow()) + 1), - )), - moves: Rc::new(RefCell::new((*(*o.upgrade().deref()).moves.borrow()))), + v: { o.with(|__s: &Tracked| __s.v) }, + copies: { (o.with(|__s: &Tracked| __s.copies) + 1) }, + moves: { o.with(|__s: &Tracked| __s.moves) }, })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } pub fn move_from(o: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*(*o.upgrade().deref()).v.borrow()))), - copies: Rc::new(RefCell::new((*(*o.upgrade().deref()).copies.borrow()))), - moves: Rc::new(RefCell::new(((*(*o.upgrade().deref()).moves.borrow()) + 1))), + v: { o.with(|__s: &Tracked| __s.v) }, + copies: { o.with(|__s: &Tracked| __s.copies) }, + moves: { (o.with(|__s: &Tracked| __s.moves) + 1) }, })); let this: Ptr = __this.as_pointer(); - (*(*o.upgrade().deref()).v.borrow_mut()) = 0; + o.with_mut(|__s: &mut Tracked| __s.v = 0); Rc::try_unwrap(__this).ok().unwrap().into_inner() } } @@ -58,7 +59,27 @@ impl Clone for Tracked { Tracked::copy_from(__src.as_pointer()) } } +<<<<<<< HEAD impl ByteRepr for Tracked {} +======= +impl ByteRepr for Tracked { + fn byte_size() -> usize { + 12 + } + fn to_bytes(&self, buf: &mut [u8]) { + self.v.to_bytes(&mut buf[0..4]); + self.copies.to_bytes(&mut buf[4..8]); + self.moves.to_bytes(&mut buf[8..12]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + v: ::from_bytes(&buf[0..4]), + copies: ::from_bytes(&buf[4..8]), + moves: ::from_bytes(&buf[8..12]), + } + } +} +>>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub fn chosen_overload_0(_a0: Ptr) -> Overload { return Overload_kLvalueOverload; } @@ -68,7 +89,7 @@ pub fn chosen_overload_1(_a0: Ptr) -> Overload { impl Holder { pub fn new_1(x: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - t: Rc::new(RefCell::new(Tracked::copy_from({ (x).clone() }))), + t: Tracked::copy_from({ (x).clone() }), })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() @@ -77,26 +98,34 @@ impl Holder { impl Holder { pub fn new_2(x: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - t: Rc::new(RefCell::new(Tracked::move_from({ (x).clone() }))), + t: Tracked::move_from({ (x).clone() }), })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Holder { - pub t: Value, + #[offset(0)] + pub t: Tracked, } -impl Clone for Holder { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - t: Rc::new(RefCell::new(Tracked::copy_from({ self.t.as_pointer() }))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() +<<<<<<< HEAD +impl ByteRepr for Holder {} +======= +impl ByteRepr for Holder { + fn byte_size() -> usize { + 12 + } + fn to_bytes(&self, buf: &mut [u8]) { + self.t.to_bytes(&mut buf[0..12]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + t: ::from_bytes(&buf[0..12]), + } } } -impl ByteRepr for Holder {} +>>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub fn forward_once_2(x: Ptr) -> Overload { return ({ chosen_overload_0((x).clone()) }); } @@ -124,7 +153,7 @@ fn main_0() -> i32 { assert!( ((({ forward_once_2(lvalue.as_pointer(),) }) as i32) == (Overload_kLvalueOverload as i32)) ); - assert!(((*(*lvalue.borrow()).v.borrow()) == 7)); + assert!(({ (*lvalue.borrow()).v } == 7)); assert!( ((({ let _x: Value = Rc::new(RefCell::new(Tracked::new({ 8 }))); @@ -137,7 +166,7 @@ fn main_0() -> i32 { ((({ forward_twice_4(relayed.as_pointer(),) }) as i32) == (Overload_kLvalueOverload as i32)) ); - assert!(((*(*relayed.borrow()).v.borrow()) == 9)); + assert!(({ (*relayed.borrow()).v } == 9)); assert!( ((({ let _x: Value = Rc::new(RefCell::new(Tracked::new({ 10 }))); @@ -148,19 +177,19 @@ fn main_0() -> i32 { let kept: Value = Rc::new(RefCell::new(Tracked::new({ 11 }))); let from_lvalue: Value = Rc::new(RefCell::new(({ forward_into_ctor_6(kept.as_pointer()) }))); - assert!(((*(*(*from_lvalue.borrow()).t.borrow()).v.borrow()) == 11)); - assert!(((*(*(*from_lvalue.borrow()).t.borrow()).copies.borrow()) == 1)); - assert!(((*(*(*from_lvalue.borrow()).t.borrow()).moves.borrow()) == 0)); - assert!(((*(*kept.borrow()).v.borrow()) == 11)); + assert!(({ (*from_lvalue.borrow()).t.v } == 11)); + assert!(({ (*from_lvalue.borrow()).t.copies } == 1)); + assert!(({ (*from_lvalue.borrow()).t.moves } == 0)); + assert!(({ (*kept.borrow()).v } == 11)); let from_rvalue: Value = Rc::new(RefCell::new( ({ let _x: Value = Rc::new(RefCell::new(Tracked::new({ 12 }))); forward_into_ctor_7(_x.as_pointer()) }), )); - assert!(((*(*(*from_rvalue.borrow()).t.borrow()).v.borrow()) == 12)); - assert!(((*(*(*from_rvalue.borrow()).t.borrow()).copies.borrow()) == 0)); - assert!(((*(*(*from_rvalue.borrow()).t.borrow()).moves.borrow()) == 1)); + assert!(({ (*from_rvalue.borrow()).t.v } == 12)); + assert!(({ (*from_rvalue.borrow()).t.copies } == 0)); + assert!(({ (*from_rvalue.borrow()).t.moves } == 1)); return 0; } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/forward_decltype.rs b/tests/unit/out/refcount/forward_decltype.rs index 91fd805b8b..c24eda5ce4 100644 --- a/tests/unit/out/refcount/forward_decltype.rs +++ b/tests/unit/out/refcount/forward_decltype.rs @@ -9,42 +9,43 @@ use std::rc::{Rc, Weak}; pub type Overload = u32; pub const Overload_kLvalueOverload: Overload = 1; pub const Overload_kRvalueOverload: Overload = 2; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Record, VaArg, FnPtrArg, Default)] pub struct Tracked { - pub v: Value, - pub copies: Value, - pub moves: Value, + #[offset(0)] + pub v: i32, + #[offset(4)] + pub copies: i32, + #[offset(8)] + pub moves: i32, } impl Tracked { pub fn new(v: i32) -> Self { let v: Value = Rc::new(RefCell::new(v)); let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*v.borrow()))), - copies: Rc::new(RefCell::new(0)), - moves: Rc::new(RefCell::new(0)), + v: (*v.borrow()), + copies: 0, + moves: 0, })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } pub fn copy_from(o: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*(*o.upgrade().deref()).v.borrow()))), - copies: Rc::new(RefCell::new( - ((*(*o.upgrade().deref()).copies.borrow()) + 1), - )), - moves: Rc::new(RefCell::new((*(*o.upgrade().deref()).moves.borrow()))), + v: { o.with(|__s: &Tracked| __s.v) }, + copies: { (o.with(|__s: &Tracked| __s.copies) + 1) }, + moves: { o.with(|__s: &Tracked| __s.moves) }, })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } pub fn move_from(o: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*(*o.upgrade().deref()).v.borrow()))), - copies: Rc::new(RefCell::new((*(*o.upgrade().deref()).copies.borrow()))), - moves: Rc::new(RefCell::new(((*(*o.upgrade().deref()).moves.borrow()) + 1))), + v: { o.with(|__s: &Tracked| __s.v) }, + copies: { o.with(|__s: &Tracked| __s.copies) }, + moves: { (o.with(|__s: &Tracked| __s.moves) + 1) }, })); let this: Ptr = __this.as_pointer(); - (*(*o.upgrade().deref()).v.borrow_mut()) = 0; + o.with_mut(|__s: &mut Tracked| __s.v = 0); Rc::try_unwrap(__this).ok().unwrap().into_inner() } } @@ -58,7 +59,27 @@ impl Clone for Tracked { Tracked::copy_from(__src.as_pointer()) } } +<<<<<<< HEAD impl ByteRepr for Tracked {} +======= +impl ByteRepr for Tracked { + fn byte_size() -> usize { + 12 + } + fn to_bytes(&self, buf: &mut [u8]) { + self.v.to_bytes(&mut buf[0..4]); + self.copies.to_bytes(&mut buf[4..8]); + self.moves.to_bytes(&mut buf[8..12]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + v: ::from_bytes(&buf[0..4]), + copies: ::from_bytes(&buf[4..8]), + moves: ::from_bytes(&buf[8..12]), + } + } +} +>>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub fn chosen_overload_0(_a0: Ptr) -> Overload { return Overload_kLvalueOverload; } @@ -93,7 +114,7 @@ fn main_0() -> i32 { ((({ forward_by_decltype_2(a.as_pointer(),) }) as i32) == (Overload_kLvalueOverload as i32)) ); - assert!(((*(*a.borrow()).v.borrow()) == 3)); + assert!(({ (*a.borrow()).v } == 3)); assert!( ((({ let _x: Value = Rc::new(RefCell::new(Tracked::new({ 4 }))); @@ -106,7 +127,7 @@ fn main_0() -> i32 { ((({ forward_abbreviated_4(b.as_pointer(),) }) as i32) == (Overload_kLvalueOverload as i32)) ); - assert!(((*(*b.borrow()).v.borrow()) == 5)); + assert!(({ (*b.borrow()).v } == 5)); assert!( ((({ let _x: Value = Rc::new(RefCell::new(Tracked::new({ 6 }))); @@ -121,7 +142,7 @@ fn main_0() -> i32 { forward_abbreviated_pack_6(c.as_pointer(), _args_1.as_pointer()) }) == ((Overload_kLvalueOverload as i32) + (Overload_kRvalueOverload as i32))) ); - assert!(((*(*c.borrow()).v.borrow()) == 7)); + assert!(({ (*c.borrow()).v } == 7)); return 0; } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/forward_explicit.rs b/tests/unit/out/refcount/forward_explicit.rs index 047b4c1555..cef9362d09 100644 --- a/tests/unit/out/refcount/forward_explicit.rs +++ b/tests/unit/out/refcount/forward_explicit.rs @@ -11,42 +11,43 @@ pub const Overload_kLvalueOverload: Overload = 1; pub const Overload_kRvalueOverload: Overload = 2; pub const Overload_kIntLvalueOverload: Overload = 3; pub const Overload_kIntRvalueOverload: Overload = 4; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Record, VaArg, FnPtrArg, Default)] pub struct Tracked { - pub v: Value, - pub copies: Value, - pub moves: Value, + #[offset(0)] + pub v: i32, + #[offset(4)] + pub copies: i32, + #[offset(8)] + pub moves: i32, } impl Tracked { pub fn new(v: i32) -> Self { let v: Value = Rc::new(RefCell::new(v)); let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*v.borrow()))), - copies: Rc::new(RefCell::new(0)), - moves: Rc::new(RefCell::new(0)), + v: (*v.borrow()), + copies: 0, + moves: 0, })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } pub fn copy_from(o: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*(*o.upgrade().deref()).v.borrow()))), - copies: Rc::new(RefCell::new( - ((*(*o.upgrade().deref()).copies.borrow()) + 1), - )), - moves: Rc::new(RefCell::new((*(*o.upgrade().deref()).moves.borrow()))), + v: { o.with(|__s: &Tracked| __s.v) }, + copies: { (o.with(|__s: &Tracked| __s.copies) + 1) }, + moves: { o.with(|__s: &Tracked| __s.moves) }, })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } pub fn move_from(o: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*(*o.upgrade().deref()).v.borrow()))), - copies: Rc::new(RefCell::new((*(*o.upgrade().deref()).copies.borrow()))), - moves: Rc::new(RefCell::new(((*(*o.upgrade().deref()).moves.borrow()) + 1))), + v: { o.with(|__s: &Tracked| __s.v) }, + copies: { o.with(|__s: &Tracked| __s.copies) }, + moves: { (o.with(|__s: &Tracked| __s.moves) + 1) }, })); let this: Ptr = __this.as_pointer(); - (*(*o.upgrade().deref()).v.borrow_mut()) = 0; + o.with_mut(|__s: &mut Tracked| __s.v = 0); Rc::try_unwrap(__this).ok().unwrap().into_inner() } } @@ -60,7 +61,27 @@ impl Clone for Tracked { Tracked::copy_from(__src.as_pointer()) } } +<<<<<<< HEAD impl ByteRepr for Tracked {} +======= +impl ByteRepr for Tracked { + fn byte_size() -> usize { + 12 + } + fn to_bytes(&self, buf: &mut [u8]) { + self.v.to_bytes(&mut buf[0..4]); + self.copies.to_bytes(&mut buf[4..8]); + self.moves.to_bytes(&mut buf[8..12]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + v: ::from_bytes(&buf[0..4]), + copies: ::from_bytes(&buf[4..8]), + moves: ::from_bytes(&buf[8..12]), + } + } +} +>>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub fn chosen_overload_0(_a0: Ptr) -> Overload { return Overload_kLvalueOverload; } @@ -86,27 +107,27 @@ fn main_0() -> i32 { assert!( ((({ chosen_overload_0(a.as_pointer(),) }) as i32) == (Overload_kLvalueOverload as i32)) ); - assert!(((*(*a.borrow()).v.borrow()) == 5)); + assert!(({ (*a.borrow()).v } == 5)); assert!( ((({ chosen_overload_1(a.as_pointer(),) }) as i32) == (Overload_kRvalueOverload as i32)) ); - assert!(((*(*a.borrow()).v.borrow()) == 5)); + assert!(({ (*a.borrow()).v } == 5)); let b: Value = Rc::new(RefCell::new(Tracked::new({ 6 }))); let moved: Value = Rc::new(RefCell::new( ({ copy_or_move_into_param_4(Tracked::move_from({ b.as_pointer() })) }), )); - assert!(((*(*moved.borrow()).v.borrow()) == 6)); - assert!(((*(*moved.borrow()).copies.borrow()) == 0)); - assert!(((*(*moved.borrow()).moves.borrow()) == 2)); - assert!(((*(*b.borrow()).v.borrow()) == 0)); + assert!(({ (*moved.borrow()).v } == 6)); + assert!(({ (*moved.borrow()).copies } == 0)); + assert!(({ (*moved.borrow()).moves } == 2)); + assert!(({ (*b.borrow()).v } == 0)); let c: Value = Rc::new(RefCell::new(Tracked::new({ 7 }))); let copied: Value = Rc::new(RefCell::new( ({ copy_or_move_into_param_4(Tracked::copy_from({ c.as_pointer() })) }), )); - assert!(((*(*copied.borrow()).v.borrow()) == 7)); - assert!(((*(*copied.borrow()).copies.borrow()) == 1)); - assert!(((*(*copied.borrow()).moves.borrow()) == 1)); - assert!(((*(*c.borrow()).v.borrow()) == 7)); + assert!(({ (*copied.borrow()).v } == 7)); + assert!(({ (*copied.borrow()).copies } == 1)); + assert!(({ (*copied.borrow()).moves } == 1)); + assert!(({ (*c.borrow()).v } == 7)); let i: Value = Rc::new(RefCell::new(8)); assert!( ((({ chosen_overload_2(i.as_pointer(),) }) as i32) == (Overload_kIntLvalueOverload as i32)) diff --git a/tests/unit/out/refcount/forward_pack.rs b/tests/unit/out/refcount/forward_pack.rs index 6f7ba65ddb..7d7d5feab0 100644 --- a/tests/unit/out/refcount/forward_pack.rs +++ b/tests/unit/out/refcount/forward_pack.rs @@ -11,42 +11,43 @@ pub const Overload_kLvalueOverload: Overload = 1; pub const Overload_kRvalueOverload: Overload = 2; pub const Overload_kIntLvalueOverload: Overload = 3; pub const Overload_kIntRvalueOverload: Overload = 4; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Record, VaArg, FnPtrArg, Default)] pub struct Tracked { - pub v: Value, - pub copies: Value, - pub moves: Value, + #[offset(0)] + pub v: i32, + #[offset(4)] + pub copies: i32, + #[offset(8)] + pub moves: i32, } impl Tracked { pub fn new(v: i32) -> Self { let v: Value = Rc::new(RefCell::new(v)); let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*v.borrow()))), - copies: Rc::new(RefCell::new(0)), - moves: Rc::new(RefCell::new(0)), + v: (*v.borrow()), + copies: 0, + moves: 0, })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } pub fn copy_from(o: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*(*o.upgrade().deref()).v.borrow()))), - copies: Rc::new(RefCell::new( - ((*(*o.upgrade().deref()).copies.borrow()) + 1), - )), - moves: Rc::new(RefCell::new((*(*o.upgrade().deref()).moves.borrow()))), + v: { o.with(|__s: &Tracked| __s.v) }, + copies: { (o.with(|__s: &Tracked| __s.copies) + 1) }, + moves: { o.with(|__s: &Tracked| __s.moves) }, })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } pub fn move_from(o: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*(*o.upgrade().deref()).v.borrow()))), - copies: Rc::new(RefCell::new((*(*o.upgrade().deref()).copies.borrow()))), - moves: Rc::new(RefCell::new(((*(*o.upgrade().deref()).moves.borrow()) + 1))), + v: { o.with(|__s: &Tracked| __s.v) }, + copies: { o.with(|__s: &Tracked| __s.copies) }, + moves: { (o.with(|__s: &Tracked| __s.moves) + 1) }, })); let this: Ptr = __this.as_pointer(); - (*(*o.upgrade().deref()).v.borrow_mut()) = 0; + o.with_mut(|__s: &mut Tracked| __s.v = 0); Rc::try_unwrap(__this).ok().unwrap().into_inner() } } @@ -60,7 +61,27 @@ impl Clone for Tracked { Tracked::copy_from(__src.as_pointer()) } } +<<<<<<< HEAD impl ByteRepr for Tracked {} +======= +impl ByteRepr for Tracked { + fn byte_size() -> usize { + 12 + } + fn to_bytes(&self, buf: &mut [u8]) { + self.v.to_bytes(&mut buf[0..4]); + self.copies.to_bytes(&mut buf[4..8]); + self.moves.to_bytes(&mut buf[8..12]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + v: ::from_bytes(&buf[0..4]), + copies: ::from_bytes(&buf[4..8]), + moves: ::from_bytes(&buf[8..12]), + } + } +} +>>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub fn chosen_overload_0(_a0: Ptr) -> Overload { return Overload_kLvalueOverload; } @@ -134,29 +155,39 @@ pub fn forward_pack_7( impl Pair { pub fn new(x: Ptr, y: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - a: Rc::new(RefCell::new(Tracked::copy_from({ (x).clone() }))), - b: Rc::new(RefCell::new(Tracked::move_from({ (y).clone() }))), + a: Tracked::copy_from({ (x).clone() }), + b: Tracked::move_from({ (y).clone() }), })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Pair { - pub a: Value, - pub b: Value, + #[offset(0)] + pub a: Tracked, + #[offset(12)] + pub b: Tracked, } -impl Clone for Pair { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - a: Rc::new(RefCell::new(Tracked::copy_from({ self.a.as_pointer() }))), - b: Rc::new(RefCell::new(Tracked::copy_from({ self.b.as_pointer() }))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() +<<<<<<< HEAD +impl ByteRepr for Pair {} +======= +impl ByteRepr for Pair { + fn byte_size() -> usize { + 24 + } + fn to_bytes(&self, buf: &mut [u8]) { + self.a.to_bytes(&mut buf[0..12]); + self.b.to_bytes(&mut buf[12..24]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + a: ::from_bytes(&buf[0..12]), + b: ::from_bytes(&buf[12..24]), + } } } -impl ByteRepr for Pair {} +>>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub fn forward_pack_into_ctor_8(args_0: Ptr, args_1: Ptr) -> Pair { return Pair::new({ (args_0).clone() }, { (args_1).clone() }); } @@ -168,7 +199,7 @@ fn main_0() -> i32 { assert!((({ forward_pack_4() }) == 0)); let a: Value = Rc::new(RefCell::new(Tracked::new({ 1 }))); assert!((({ forward_pack_5(a.as_pointer(),) }) == (Overload_kLvalueOverload as i32))); - assert!(((*(*a.borrow()).v.borrow()) == 1)); + assert!(({ (*a.borrow()).v } == 1)); assert!( (({ let _args: Value = Rc::new(RefCell::new(Tracked::new({ 2 }))); @@ -188,7 +219,7 @@ fn main_0() -> i32 { ) }) == 1234) ); - assert!(((*(*a.borrow()).v.borrow()) == 1)); + assert!(({ (*a.borrow()).v } == 1)); assert!(((*i.borrow()) == 3)); let lhs: Value = Rc::new(RefCell::new(Tracked::new({ 6 }))); let p: Value = Rc::new(RefCell::new( @@ -197,12 +228,12 @@ fn main_0() -> i32 { forward_pack_into_ctor_8(lhs.as_pointer(), _args_1.as_pointer()) }), )); - assert!(((*(*(*p.borrow()).a.borrow()).v.borrow()) == 6)); - assert!(((*(*(*p.borrow()).a.borrow()).copies.borrow()) == 1)); - assert!(((*(*(*p.borrow()).b.borrow()).v.borrow()) == 7)); - assert!(((*(*(*p.borrow()).b.borrow()).copies.borrow()) == 0)); - assert!(((*(*(*p.borrow()).b.borrow()).moves.borrow()) == 1)); - assert!(((*(*lhs.borrow()).v.borrow()) == 6)); + assert!(({ (*p.borrow()).a.v } == 6)); + assert!(({ (*p.borrow()).a.copies } == 1)); + assert!(({ (*p.borrow()).b.v } == 7)); + assert!(({ (*p.borrow()).b.copies } == 0)); + assert!(({ (*p.borrow()).b.moves } == 1)); + assert!(({ (*lhs.borrow()).v } == 6)); return 0; } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/friend.rs b/tests/unit/out/refcount/friend.rs index 35399d0f7f..f800dd6c1c 100644 --- a/tests/unit/out/refcount/friend.rs +++ b/tests/unit/out/refcount/friend.rs @@ -7,31 +7,32 @@ use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; pub fn get_0(v: Ptr) -> i32 { - return (*(*v.upgrade().deref()).x.borrow()); + return v.with(|__s: &V| __s.x); } pub fn operator_eq_1(a: Ptr, b: Ptr) -> bool { return { - let _lhs = (*(*a.upgrade().deref()).x.borrow()); - _lhs == (*(*b.upgrade().deref()).x.borrow()) + let _lhs = a.with(|__s: &V| __s.x); + _lhs == b.with(|__s: &V| __s.x) }; } pub fn scaled_2(v: Ptr, k: i32) -> i32 { let k: Value = Rc::new(RefCell::new(k)); return { - let _lhs = (*(*v.upgrade().deref()).x.borrow()); + let _lhs = v.with(|__s: &V| __s.x); _lhs * (*k.borrow()) }; } pub fn scaled_3(v: Ptr, k: f64) -> f64 { let k: Value = Rc::new(RefCell::new(k)); return { - let _lhs = ((*(*v.upgrade().deref()).x.borrow()) as f64); + let _lhs = (v.with(|__s: &V| __s.x) as f64); _lhs * (*k.borrow()) }; } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct V { - pub x: Value, + #[offset(0)] + pub x: i32, } impl std::cmp::PartialEq for V { fn eq(&self, other: &Self) -> bool { @@ -44,129 +45,90 @@ impl std::cmp::PartialEq for V { } } impl std::cmp::Eq for V {} -impl Clone for V { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - x: Rc::new(RefCell::new((*self.x.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } -} impl ByteRepr for V { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.x.borrow()).to_bytes(&mut buf[0..4]); + self.x.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - x: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + x: ::from_bytes(&buf[0..4]), } } } pub fn get_4(w: Ptr) -> i32 { - return (*(*w.upgrade().deref()).x.borrow()); + return w.with(|__s: &W_int_| __s.x); } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct W_int_ { - pub x: Value, -} -impl Clone for W_int_ { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - x: Rc::new(RefCell::new((*self.x.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub x: i32, } impl ByteRepr for W_int_ { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.x.borrow()).to_bytes(&mut buf[0..4]); + self.x.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - x: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + x: ::from_bytes(&buf[0..4]), } } } pub fn get_5(w: Ptr) -> i64 { - return (*(*w.upgrade().deref()).x.borrow()); + return w.with(|__s: &W_long_| __s.x); } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct W_long_ { - pub x: Value, -} -impl Clone for W_long_ { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - x: Rc::new(RefCell::new((*self.x.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub x: i64, } impl ByteRepr for W_long_ { fn byte_size() -> usize { 8 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.x.borrow()).to_bytes(&mut buf[0..8]); + self.x.to_bytes(&mut buf[0..8]); } fn from_bytes(buf: &[u8]) -> Self { Self { - x: Rc::new(RefCell::new(::from_bytes(&buf[0..8]))), + x: ::from_bytes(&buf[0..8]), } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct D { - pub x: Value, -} -impl Clone for D { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - x: Rc::new(RefCell::new((*self.x.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub x: i32, } impl ByteRepr for D { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.x.borrow()).to_bytes(&mut buf[0..4]); + self.x.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - x: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + x: ::from_bytes(&buf[0..4]), } } } pub fn declared_then_defined_6(d: Ptr) -> i32 { - return ((*(*d.upgrade().deref()).x.borrow()) + 1); + return (d.with(|__s: &D| __s.x) + 1); } pub fn main() { __cpp2rust_init_globals(); std::process::exit(main_0()); } fn main_0() -> i32 { - let a: Value = Rc::new(RefCell::new(V { - x: Rc::new(RefCell::new(3)), - })); - let b: Value = Rc::new(RefCell::new(V { - x: Rc::new(RefCell::new(3)), - })); - let c: Value = Rc::new(RefCell::new(V { - x: Rc::new(RefCell::new(4)), - })); + let a: Value = Rc::new(RefCell::new(V { x: 3 })); + let b: Value = Rc::new(RefCell::new(V { x: 3 })); + let c: Value = Rc::new(RefCell::new(V { x: 4 })); assert!((({ get_0(a.as_pointer(),) }) == 3)); assert!( ({ @@ -182,17 +144,11 @@ fn main_0() -> i32 { ); assert!((({ scaled_2(c.as_pointer(), 2,) }) == 8)); assert!((({ scaled_3(c.as_pointer(), 1.5E+0,) }) == 6.0E+0)); - let wi: Value = Rc::new(RefCell::new(W_int_ { - x: Rc::new(RefCell::new(5)), - })); - let wl: Value = Rc::new(RefCell::new(W_long_ { - x: Rc::new(RefCell::new(6_i64)), - })); + let wi: Value = Rc::new(RefCell::new(W_int_ { x: 5 })); + let wl: Value = Rc::new(RefCell::new(W_long_ { x: 6_i64 })); assert!((({ get_4(wi.as_pointer(),) }) == 5)); assert!((({ get_5(wl.as_pointer(),) }) == 6_i64)); - let d: Value = Rc::new(RefCell::new(D { - x: Rc::new(RefCell::new(7)), - })); + let d: Value = Rc::new(RefCell::new(D { x: 7 })); assert!((({ declared_then_defined_6(d.as_pointer(),) }) == 8)); return 0; } diff --git a/tests/unit/out/refcount/function_overloading.rs b/tests/unit/out/refcount/function_overloading.rs index 14fd623290..c0bc238e45 100644 --- a/tests/unit/out/refcount/function_overloading.rs +++ b/tests/unit/out/refcount/function_overloading.rs @@ -36,7 +36,7 @@ pub fn foo_3(x: Ptr, y: Ptr, z: Ptr) -> i32 { pub fn bar_4(x: Ptr) -> i32 { return (x.read()); } -#[derive(Clone, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] pub struct Foo {} pub fn func_5(x: i32) -> i32 { let x: Value = Rc::new(RefCell::new(x)); diff --git a/tests/unit/out/refcount/global_init_side_effect.rs b/tests/unit/out/refcount/global_init_side_effect.rs index 645c771d82..a49dac3aef 100644 --- a/tests/unit/out/refcount/global_init_side_effect.rs +++ b/tests/unit/out/refcount/global_init_side_effect.rs @@ -9,7 +9,7 @@ use std::rc::{Rc, Weak}; thread_local!( pub static total_0: Value = Rc::new(RefCell::new(0)); ); -#[derive(Clone, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] pub struct S {} impl S { pub fn new(x: i32) -> Self { diff --git a/tests/unit/out/refcount/global_non_const_init.rs b/tests/unit/out/refcount/global_non_const_init.rs index 277f5aff16..266b616c01 100644 --- a/tests/unit/out/refcount/global_non_const_init.rs +++ b/tests/unit/out/refcount/global_non_const_init.rs @@ -33,32 +33,20 @@ thread_local!( pub static depends_on_call_6: Value = Rc::new(RefCell::new((from_call_5.with(|rc| *rc.borrow()) + 1))); ); -#[derive(VaArg, FnPtrArg)] +#[derive(Clone, Record, VaArg, FnPtrArg)] pub struct Ctor { - pub v: Value, + #[offset(0)] + pub v: i32, } impl Ctor { pub fn new_1() -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new(({ next_0() }))), - })); + let __this: Value = Rc::new(RefCell::new(Self { v: ({ next_0() }) })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } pub fn new_2(x: i32) -> Self { let x: Value = Rc::new(RefCell::new(x)); - let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*x.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } -} -impl Clone for Ctor { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*self.v.borrow()))), - })); + let __this: Value = Rc::new(RefCell::new(Self { v: (*x.borrow()) })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } @@ -73,11 +61,11 @@ impl ByteRepr for Ctor { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.v.borrow()).to_bytes(&mut buf[0..4]); + self.v.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - v: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + v: ::from_bytes(&buf[0..4]), } } } @@ -97,7 +85,7 @@ thread_local!( thread_local!( pub static inline_member_11: Value = Rc::new(RefCell::new(Ctor::new_2({ 5 }))); ); -#[derive(Clone, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] pub struct Holder {} thread_local!( pub static member_10: Value = Rc::new(RefCell::new(({ next_0() }))); @@ -109,18 +97,16 @@ pub fn local_static_12() -> i32 { thread_local!( static local_ctor_14: Value = Rc::new(RefCell::new(Ctor::new_2({ 3 }))); ); - return (once_13.with(|rc| *rc.borrow()) - + (*(*local_ctor_14.with(Value::clone).borrow()).v.borrow())); + return (once_13.with(|rc| *rc.borrow()) + { (*local_ctor_14.with(Value::clone).borrow()).v }); } -#[derive(VaArg, FnPtrArg)] +#[derive(Clone, Record, VaArg, FnPtrArg)] pub struct Singleton { - pub hits: Value, + #[offset(0)] + pub hits: i32, } impl Singleton { pub fn new() -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - hits: Rc::new(RefCell::new(0)), - })); + let __this: Value = Rc::new(RefCell::new(Self { hits: 0 })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } @@ -131,15 +117,6 @@ impl Singleton { return s_15.with(|v| v.as_pointer()); } } -impl Clone for Singleton { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - hits: Rc::new(RefCell::new((*self.hits.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } -} impl Default for Singleton { fn default() -> Self { { Singleton::new() } @@ -150,11 +127,11 @@ impl ByteRepr for Singleton { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.hits.borrow()).to_bytes(&mut buf[0..4]); + self.hits.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - hits: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + hits: ::from_bytes(&buf[0..4]), } } } @@ -180,8 +157,8 @@ fn main_0() -> i32 { assert!(((single_4.with(|rc| *rc.borrow()) as i32) == 18)); assert!((from_call_5.with(|rc| *rc.borrow()) == 1)); assert!((depends_on_call_6.with(|rc| *rc.borrow()) == 2)); - assert!(((*(*default_ctor_7.with(Value::clone).borrow()).v.borrow()) == 2)); - assert!(((*(*arg_ctor_8.with(Value::clone).borrow()).v.borrow()) == 7)); + assert!(({ (*default_ctor_7.with(Value::clone).borrow()).v } == 2)); + assert!(({ (*arg_ctor_8.with(Value::clone).borrow()).v } == 7)); assert!( (*str_9.with(Value::clone).borrow()) .iter() @@ -190,23 +167,12 @@ fn main_0() -> i32 { .eq(Ptr::::from_string_literal(b"abc").to_c_string_iterator()) ); assert!((member_10.with(|rc| *rc.borrow()) == 3)); - assert!(((*(*inline_member_11.with(Value::clone).borrow()).v.borrow()) == 5)); + assert!(({ (*inline_member_11.with(Value::clone).borrow()).v } == 5)); assert!((({ local_static_12() }) == 7)); assert!((({ local_static_12() }) == 7)); - (*(*({ Singleton::instance() }).upgrade().deref()) - .hits - .borrow_mut()) - .postfix_inc(); - (*(*({ Singleton::instance() }).upgrade().deref()) - .hits - .borrow_mut()) - .postfix_inc(); - assert!( - ((*(*({ Singleton::instance() }).upgrade().deref()) - .hits - .borrow()) - == 2) - ); + ({ Singleton::instance() }).with_mut(|__s: &mut Singleton| __s.hits.postfix_inc()); + ({ Singleton::instance() }).with_mut(|__s: &mut Singleton| __s.hits.postfix_inc()); + assert!((({ Singleton::instance() }).with(|__s: &Singleton| __s.hits) == 2)); assert!((({ Singleton::instance() }) == ({ Singleton::instance() }))); return 0; } diff --git a/tests/unit/out/refcount/global_pointers.rs b/tests/unit/out/refcount/global_pointers.rs index d5dcc4454f..512b6a7204 100644 --- a/tests/unit/out/refcount/global_pointers.rs +++ b/tests/unit/out/refcount/global_pointers.rs @@ -6,51 +6,43 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Entry { - pub name: Value>, - pub p: Value>, -} -impl Clone for Entry { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - name: Rc::new(RefCell::new((*self.name.borrow()).clone())), - p: Rc::new(RefCell::new((*self.p.borrow()).clone())), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub name: Ptr, + #[offset(8)] + pub p: Ptr, } impl ByteRepr for Entry { fn byte_size() -> usize { 16 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.name.borrow()).to_bytes(&mut buf[0..8]); - (*self.p.borrow()).to_bytes(&mut buf[8..16]); + self.name.to_bytes(&mut buf[0..8]); + self.p.to_bytes(&mut buf[8..16]); } fn from_bytes(buf: &[u8]) -> Self { Self { - name: Rc::new(RefCell::new(>::from_bytes(&buf[0..8]))), - p: Rc::new(RefCell::new(>::from_bytes(&buf[8..16]))), + name: >::from_bytes(&buf[0..8]), + p: >::from_bytes(&buf[8..16]), } } } thread_local!( pub static single_entry_0: Value = Rc::new(RefCell::new(Entry { - name: Rc::new(RefCell::new(Ptr::::from_string_literal(b"alone"))), - p: Rc::new(RefCell::new(Ptr::::null())), + name: Ptr::::from_string_literal(b"alone"), + p: Ptr::::null(), })); ); thread_local!( pub static entries_1: Value> = Rc::new(RefCell::new(Box::new([ Entry { - name: Rc::new(RefCell::new(Ptr::::from_string_literal(b"first"))), - p: Rc::new(RefCell::new(Ptr::::null())), + name: Ptr::::from_string_literal(b"first"), + p: Ptr::::null(), }, Entry { - name: Rc::new(RefCell::new(Ptr::::from_string_literal(b"second"))), - p: Rc::new(RefCell::new(Ptr::::null())), + name: Ptr::::from_string_literal(b"second"), + p: Ptr::::null(), }, ]))); ); @@ -66,13 +58,15 @@ pub fn main() { std::process::exit(main_0()); } fn main_0() -> i32 { - assert!((*(*single_entry_0.with(Value::clone).borrow()).p.borrow()).is_null()); + assert!(({ (*single_entry_0.with(Value::clone).borrow()).p.clone() }).is_null()); let i: Value = Rc::new(RefCell::new(0)); 'loop_: while ((*i.borrow()) < 2) { assert!( - (*(*entries_1.with(Value::clone).borrow())[(*i.borrow()) as usize] - .p - .borrow()) + ({ + (*entries_1.with(Value::clone).borrow())[(*i.borrow()) as usize] + .p + .clone() + }) .is_null() ); assert!( diff --git a/tests/unit/out/refcount/global_without_initializer.rs b/tests/unit/out/refcount/global_without_initializer.rs index 38d562eb14..0ed01cec83 100644 --- a/tests/unit/out/refcount/global_without_initializer.rs +++ b/tests/unit/out/refcount/global_without_initializer.rs @@ -6,29 +6,21 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct S { - pub a: Value, -} -impl Clone for S { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - a: Rc::new(RefCell::new((*self.a.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub a: i32, } impl ByteRepr for S { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.a.borrow()).to_bytes(&mut buf[0..4]); + self.a.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - a: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + a: ::from_bytes(&buf[0..4]), } } } diff --git a/tests/unit/out/refcount/goto_aggregate_default.rs b/tests/unit/out/refcount/goto_aggregate_default.rs index 531693714e..a1a36912f6 100644 --- a/tests/unit/out/refcount/goto_aggregate_default.rs +++ b/tests/unit/out/refcount/goto_aggregate_default.rs @@ -6,31 +6,25 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Point { - pub x: Value, - pub y: Value, -} -impl Clone for Point { - fn clone(&self) -> Self { - Self { - x: Rc::new(RefCell::new((*self.x.borrow()).clone())), - y: Rc::new(RefCell::new((*self.y.borrow()).clone())), - } - } + #[offset(0)] + pub x: i32, + #[offset(4)] + pub y: i32, } impl ByteRepr for Point { fn byte_size() -> usize { 8 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.x.borrow()).to_bytes(&mut buf[0..4]); - (*self.y.borrow()).to_bytes(&mut buf[4..8]); + self.x.to_bytes(&mut buf[0..4]); + self.y.to_bytes(&mut buf[4..8]); } fn from_bytes(buf: &[u8]) -> Self { Self { - x: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - y: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + x: ::from_bytes(&buf[0..4]), + y: ::from_bytes(&buf[4..8]), } } } diff --git a/tests/unit/out/refcount/goto_cleanup.rs b/tests/unit/out/refcount/goto_cleanup.rs index 08b5a78968..765bb764f2 100644 --- a/tests/unit/out/refcount/goto_cleanup.rs +++ b/tests/unit/out/refcount/goto_cleanup.rs @@ -77,27 +77,21 @@ pub fn from_switch_2(n: i32) -> i32 { }); panic!("ub: non-void function does not return a value") } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct wrapper { - pub item: Value>, -} -impl Clone for wrapper { - fn clone(&self) -> Self { - Self { - item: Rc::new(RefCell::new((*self.item.borrow()).clone())), - } - } + #[offset(0)] + pub item: Ptr, } impl ByteRepr for wrapper { fn byte_size() -> usize { 8 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.item.borrow()).to_bytes(&mut buf[0..8]); + self.item.to_bytes(&mut buf[0..8]); } fn from_bytes(buf: &[u8]) -> Self { Self { - item: Rc::new(RefCell::new(>::from_bytes(&buf[0..8]))), + item: >::from_bytes(&buf[0..8]), } } } @@ -109,7 +103,7 @@ pub fn via_pointer_3(w: Ptr, fail: i32) -> i32 { goto_block!({ '__entry: { *ret.borrow_mut() = 0; - *item.borrow_mut() = (*(*(*w.borrow()).upgrade().deref()).item.borrow()).clone(); + *item.borrow_mut() = (*w.borrow()).with(|__s: &wrapper| (__s.item).clone()); if ((*fail.borrow()) != 0) { (*ret.borrow_mut()) = -1_i32; goto!('out); @@ -163,7 +157,7 @@ fn main_0() -> i32 { assert!((((({ from_switch_2(2,) }) == 999) as i32) != 0)); let value: Value = Rc::new(RefCell::new(42)); let w: Value = Rc::new(RefCell::new(wrapper { - item: Rc::new(RefCell::new((value.as_pointer()))), + item: (value.as_pointer()), })); assert!((((({ via_pointer_3((w.as_pointer()), 0,) }) == 42) as i32) != 0)); assert!((((({ via_pointer_3((w.as_pointer()), 1,) }) == -1_i32) as i32) != 0)); diff --git a/tests/unit/out/refcount/huffman.rs b/tests/unit/out/refcount/huffman.rs index 3d534bb9ef..81a244b584 100644 --- a/tests/unit/out/refcount/huffman.rs +++ b/tests/unit/out/refcount/huffman.rs @@ -6,118 +6,127 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct MinHeapNode { - pub data: Value, - pub freq: Value, - pub left: Value>, - pub right: Value>, -} -impl Clone for MinHeapNode { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - data: Rc::new(RefCell::new((*self.data.borrow()))), - freq: Rc::new(RefCell::new((*self.freq.borrow()))), - left: Rc::new(RefCell::new((*self.left.borrow()).clone())), - right: Rc::new(RefCell::new((*self.right.borrow()).clone())), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub data: u8, + #[offset(4)] + pub freq: i32, + #[offset(8)] + pub left: Ptr, + #[offset(16)] + pub right: Ptr, } impl ByteRepr for MinHeapNode { fn byte_size() -> usize { 24 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.data.borrow()).to_bytes(&mut buf[0..1]); - (*self.freq.borrow()).to_bytes(&mut buf[4..8]); - (*self.left.borrow()).to_bytes(&mut buf[8..16]); - (*self.right.borrow()).to_bytes(&mut buf[16..24]); + self.data.to_bytes(&mut buf[0..1]); + self.freq.to_bytes(&mut buf[4..8]); + self.left.to_bytes(&mut buf[8..16]); + self.right.to_bytes(&mut buf[16..24]); } fn from_bytes(buf: &[u8]) -> Self { Self { - data: Rc::new(RefCell::new(::from_bytes(&buf[0..1]))), - freq: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), - left: Rc::new(RefCell::new(>::from_bytes(&buf[8..16]))), - right: Rc::new(RefCell::new(>::from_bytes(&buf[16..24]))), + data: ::from_bytes(&buf[0..1]), + freq: ::from_bytes(&buf[4..8]), + left: >::from_bytes(&buf[8..16]), + right: >::from_bytes(&buf[16..24]), } } } pub fn Swap_0(a: Ptr, b: Ptr) { let t: Value = Rc::new(RefCell::new(MinHeapNode { - data: Rc::new(RefCell::new((*(*a.upgrade().deref()).data.borrow()))), - freq: Rc::new(RefCell::new((*(*a.upgrade().deref()).freq.borrow()))), - left: Rc::new(RefCell::new( - (*(*a.upgrade().deref()).left.borrow()).clone(), - )), - right: Rc::new(RefCell::new( - (*(*a.upgrade().deref()).right.borrow()).clone(), - )), + data: { a.with(|__s: &MinHeapNode| __s.data) }, + freq: { a.with(|__s: &MinHeapNode| __s.freq) }, + left: { a.with(|__s: &MinHeapNode| (__s.left).clone()) }, + right: { a.with(|__s: &MinHeapNode| (__s.right).clone()) }, })); let __rhs = MinHeapNode { - data: Rc::new(RefCell::new((*(*b.upgrade().deref()).data.borrow()))), - freq: Rc::new(RefCell::new((*(*b.upgrade().deref()).freq.borrow()))), - left: Rc::new(RefCell::new( - (*(*b.upgrade().deref()).left.borrow()).clone(), - )), - right: Rc::new(RefCell::new( - (*(*b.upgrade().deref()).right.borrow()).clone(), - )), + data: { b.with(|__s: &MinHeapNode| __s.data) }, + freq: { b.with(|__s: &MinHeapNode| __s.freq) }, + left: { b.with(|__s: &MinHeapNode| (__s.left).clone()) }, + right: { b.with(|__s: &MinHeapNode| (__s.right).clone()) }, }; a.write(__rhs); let __rhs = MinHeapNode { - data: Rc::new(RefCell::new((*(*t.borrow()).data.borrow()))), - freq: Rc::new(RefCell::new((*(*t.borrow()).freq.borrow()))), - left: Rc::new(RefCell::new((*(*t.borrow()).left.borrow()).clone())), - right: Rc::new(RefCell::new((*(*t.borrow()).right.borrow()).clone())), + data: { (*t.borrow()).data }, + freq: { (*t.borrow()).freq }, + left: { (*t.borrow()).left.clone() }, + right: { (*t.borrow()).right.clone() }, }; b.write(__rhs); } -#[derive(Default)] +#[derive(Record, Default)] pub struct MinHeap { - pub size: Value, - pub capacity: Value, - pub arr: Value]>>>>, - pub next: Value, - pub alloc: Value>>>, + #[offset(0)] + pub size: i32, + #[offset(4)] + pub capacity: i32, + #[offset(8)] + pub arr: Option]>>>, + #[offset(16)] + pub next: i32, + #[offset(24)] + pub alloc: Option>>, } impl MinHeap { pub fn move_from(_a0: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - size: Rc::new(RefCell::new((*(*_a0.upgrade().deref()).size.borrow()))), - capacity: Rc::new(RefCell::new((*(*_a0.upgrade().deref()).capacity.borrow()))), - arr: Rc::new(RefCell::new( - (*(*_a0.upgrade().deref()).arr.borrow_mut()).take(), - )), - next: Rc::new(RefCell::new((*(*_a0.upgrade().deref()).next.borrow()))), - alloc: Rc::new(RefCell::new( - (*(*_a0.upgrade().deref()).alloc.borrow_mut()).take(), - )), + size: { _a0.with(|__s: &MinHeap| __s.size) }, + capacity: { _a0.with(|__s: &MinHeap| __s.capacity) }, + arr: { _a0.with_mut(|__s: &mut MinHeap| __s.arr.take()) }, + next: { _a0.with(|__s: &MinHeap| __s.next) }, + alloc: { _a0.with_mut(|__s: &mut MinHeap| __s.alloc.take()) }, })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } } +<<<<<<< HEAD impl ByteRepr for MinHeap {} +======= +impl ByteRepr for MinHeap { + fn byte_size() -> usize { + 32 + } + fn to_bytes(&self, buf: &mut [u8]) { + self.size.to_bytes(&mut buf[0..4]); + self.capacity.to_bytes(&mut buf[4..8]); + self.arr.to_bytes(&mut buf[8..16]); + self.next.to_bytes(&mut buf[16..20]); + self.alloc.to_bytes(&mut buf[24..32]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + size: ::from_bytes(&buf[0..4]), + capacity: ::from_bytes(&buf[4..8]), + arr: ]>>>>::from_bytes(&buf[8..16]), + next: ::from_bytes(&buf[16..20]), + alloc: >>>::from_bytes(&buf[24..32]), + } + } +} +>>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub fn AllocMinHeap_1(capacity: i32) -> Option> { let capacity: Value = Rc::new(RefCell::new(capacity)); let minHeap: Value>> = Rc::new(RefCell::new(Some(Rc::new(RefCell::new({ let __tmp_0: Value = Rc::new(RefCell::new(MinHeap { - size: Rc::new(RefCell::new(0)), - capacity: Rc::new(RefCell::new((*capacity.borrow()))), - arr: Rc::new(RefCell::new(Some(Rc::new(RefCell::new( + size: 0, + capacity: (*capacity.borrow()), + arr: Some(Rc::new(RefCell::new( (0..((*capacity.borrow()) as usize)) .map(|_| >::default()) .collect::>(), - ))))), - next: Rc::new(RefCell::new(0)), - alloc: Rc::new(RefCell::new(Some(Rc::new(RefCell::new( + ))), + next: 0, + alloc: Some(Rc::new(RefCell::new( (0..10000_usize) .map(|_| ::default()) .collect::>(), - ))))), + ))), })); MinHeap::move_from({ __tmp_0.as_pointer() }) }))))); @@ -137,11 +146,7 @@ pub fn Huffman_2( let _n: i32 = (*size.borrow()); MinHeapImpl::Build(&((*minHeap.borrow()).as_pointer()), _data, _freq, _n) }); - 'loop_: while ((*(*(*minHeap.borrow()).as_ref().unwrap().borrow()) - .size - .borrow()) - != 1) - { + 'loop_: while (((*minHeap.borrow()).as_pointer()).with(|__s: &MinHeap| __s.size) != 1) { let left: Value> = Rc::new(RefCell::new( ({ MinHeapImpl::ExtractMin(&((*minHeap.borrow()).as_pointer())) }), )); @@ -151,13 +156,13 @@ pub fn Huffman_2( let top: Value> = Rc::new(RefCell::new( ({ MinHeapImpl::Alloc(&((*minHeap.borrow()).as_pointer()), ('$' as u8), { - let _lhs = (*(*(*left.borrow()).upgrade().deref()).freq.borrow()); - _lhs + (*(*(*right.borrow()).upgrade().deref()).freq.borrow()) + let _lhs = (*left.borrow()).with(|__s: &MinHeapNode| __s.freq); + _lhs + (*right.borrow()).with(|__s: &MinHeapNode| __s.freq) }) }), )); - (*(*(*top.borrow()).upgrade().deref()).left.borrow_mut()) = (*left.borrow()).clone(); - (*(*(*top.borrow()).upgrade().deref()).right.borrow_mut()) = (*right.borrow()).clone(); + (*top.borrow()).with_mut(|__s: &mut MinHeapNode| __s.left = (*left.borrow()).clone()); + (*top.borrow()).with_mut(|__s: &mut MinHeapNode| __s.right = (*right.borrow()).clone()); ({ MinHeapImpl::Insert(&((*minHeap.borrow()).as_pointer()), (*top.borrow()).clone()) }); } return (*minHeap.borrow_mut()).take(); @@ -198,12 +203,12 @@ pub fn CollectCodes_4( ) { let root: Value> = Rc::new(RefCell::new(root)); let top: Value = Rc::new(RefCell::new(top)); - if !((*(*(*root.borrow()).upgrade().deref()).left.borrow()).is_null()) { + if !(((*root.borrow()).with(|__s: &MinHeapNode| (__s.left).clone())).is_null()) { (*arr.upgrade().deref()).as_ref().unwrap().borrow_mut() [((*top.borrow()) as usize) as usize] = 0; ({ let _root: Ptr = - (*(*(*root.borrow()).upgrade().deref()).left.borrow()).clone(); + (*root.borrow()).with(|__s: &MinHeapNode| (__s.left).clone()); let _arr: Ptr>>> = (arr).clone(); let _top: i32 = ((*top.borrow()) + 1); let _out: Ptr>>> = (out).clone(); @@ -211,12 +216,12 @@ pub fn CollectCodes_4( CollectCodes_4(_root, _arr, _top, _out, _next) }); } - if !((*(*(*root.borrow()).upgrade().deref()).right.borrow()).is_null()) { + if !(((*root.borrow()).with(|__s: &MinHeapNode| (__s.right).clone())).is_null()) { (*arr.upgrade().deref()).as_ref().unwrap().borrow_mut() [((*top.borrow()) as usize) as usize] = 1; ({ let _root: Ptr = - (*(*(*root.borrow()).upgrade().deref()).right.borrow()).clone(); + (*root.borrow()).with(|__s: &MinHeapNode| (__s.right).clone()); let _arr: Ptr>>> = (arr).clone(); let _top: i32 = ((*top.borrow()) + 1); let _out: Ptr>>> = (out).clone(); @@ -338,21 +343,25 @@ impl MinHeapImpl for Ptr { fn Alloc(&self, data: u8, freq: i32) -> Ptr { let data: Value = Rc::new(RefCell::new(data)); let freq: Value = Rc::new(RefCell::new(freq)); - (*(*(*self).upgrade().deref()).alloc.borrow()) - .as_ref() - .unwrap() - .borrow_mut()[((*(*(*self).upgrade().deref()).next.borrow()) as usize) as usize] = - MinHeapNode { - data: Rc::new(RefCell::new((*data.borrow()))), - freq: Rc::new(RefCell::new((*freq.borrow()))), - left: Rc::new(RefCell::new(Ptr::::null())), - right: Rc::new(RefCell::new(Ptr::::null())), - }; - return ((*(*(*self).upgrade().deref()).alloc.borrow()) - .as_ref() - .unwrap() - .as_pointer() - .offset(((*(*(*self).upgrade().deref()).next.borrow_mut()).postfix_inc() as usize))) + let __rhs = MinHeapNode { + data: (*data.borrow()), + freq: (*freq.borrow()), + left: Ptr::::null(), + right: Ptr::::null(), + }; + let __idx = ((*self).with(|__s: &MinHeap| __s.next) as usize); + (*self).with_mut(|__s: &mut MinHeap| { + __s.alloc.as_ref().unwrap().borrow_mut()[(__idx) as usize] = __rhs + }); + return ({ + let __idx = ((*self).with_mut(|__s: &mut MinHeap| __s.next.postfix_inc()) as usize); + (*(*self).upgrade().deref()) + .alloc + .as_ref() + .unwrap() + .as_pointer() + .offset((__idx)) + }) .clone(); } fn Heapify(&self, idx: i32) { @@ -360,119 +369,101 @@ impl MinHeapImpl for Ptr { let smallest: Value = Rc::new(RefCell::new((*idx.borrow()))); let left: Value = Rc::new(RefCell::new(((2 * (*idx.borrow())) + 1))); let right: Value = Rc::new(RefCell::new(((2 * (*idx.borrow())) + 2))); - if ((*left.borrow()) < (*(*(*self).upgrade().deref()).size.borrow())) - && ((*(*(*(*(*self).upgrade().deref()).arr.borrow()) - .as_ref() - .unwrap() - .borrow()[((*left.borrow()) as usize) as usize] - .upgrade() - .deref()) - .freq - .borrow()) - < (*(*(*(*(*self).upgrade().deref()).arr.borrow()) - .as_ref() - .unwrap() - .borrow()[((*smallest.borrow()) as usize) as usize] + if ((*left.borrow()) < (*self).with(|__s: &MinHeap| __s.size)) + && ({ + (*(*(*self).upgrade().deref()).arr.as_ref().unwrap().borrow() + [((*left.borrow()) as usize) as usize] .upgrade() .deref()) .freq - .borrow())) + } < { + (*(*(*self).upgrade().deref()).arr.as_ref().unwrap().borrow() + [((*smallest.borrow()) as usize) as usize] + .upgrade() + .deref()) + .freq + }) { (*smallest.borrow_mut()) = (*left.borrow()); } - if ((*right.borrow()) < (*(*(*self).upgrade().deref()).size.borrow())) - && ((*(*(*(*(*self).upgrade().deref()).arr.borrow()) - .as_ref() - .unwrap() - .borrow()[((*right.borrow()) as usize) as usize] - .upgrade() - .deref()) - .freq - .borrow()) - < (*(*(*(*(*self).upgrade().deref()).arr.borrow()) - .as_ref() - .unwrap() - .borrow()[((*smallest.borrow()) as usize) as usize] + if ((*right.borrow()) < (*self).with(|__s: &MinHeap| __s.size)) + && ({ + (*(*(*self).upgrade().deref()).arr.as_ref().unwrap().borrow() + [((*right.borrow()) as usize) as usize] + .upgrade() + .deref()) + .freq + } < { + (*(*(*self).upgrade().deref()).arr.as_ref().unwrap().borrow() + [((*smallest.borrow()) as usize) as usize] .upgrade() .deref()) .freq - .borrow())) + }) { (*smallest.borrow_mut()) = (*right.borrow()); } if ((*smallest.borrow()) != (*idx.borrow())) { ({ - let _a: Ptr = ((*(*(*self).upgrade().deref()).arr.borrow()) - .as_ref() - .unwrap() - .borrow()[((*smallest.borrow()) as usize) as usize]) - .clone(); - let _b: Ptr = ((*(*(*self).upgrade().deref()).arr.borrow()) - .as_ref() - .unwrap() - .borrow()[((*idx.borrow()) as usize) as usize]) - .clone(); + let _a: Ptr = + ((*(*self).upgrade().deref()).arr.as_ref().unwrap().borrow() + [((*smallest.borrow()) as usize) as usize]) + .clone(); + let _b: Ptr = + ((*(*self).upgrade().deref()).arr.as_ref().unwrap().borrow() + [((*idx.borrow()) as usize) as usize]) + .clone(); Swap_0(_a, _b) }); ({ MinHeapImpl::Heapify(self, (*smallest.borrow())) }); } } fn ExtractMin(&self) -> Ptr { - let out: Value> = Rc::new(RefCell::new( - ((*(*(*self).upgrade().deref()).arr.borrow()) - .as_ref() - .unwrap() - .borrow()[(0_usize) as usize]) - .clone(), - )); - (*(*(*self).upgrade().deref()).size.borrow_mut()).prefix_dec(); - let __rhs = ((*(*(*self).upgrade().deref()).arr.borrow()) - .as_ref() - .unwrap() - .borrow()[((*(*(*self).upgrade().deref()).size.borrow()) as usize) as usize]) - .clone(); - (*(*(*self).upgrade().deref()).arr.borrow()) - .as_ref() - .unwrap() - .borrow_mut()[(0_usize) as usize] = __rhs; + let out: Value> = Rc::new(RefCell::new((*self).with(|__s: &MinHeap| { + (__s.arr.as_ref().unwrap().borrow()[(0_usize) as usize]).clone() + }))); + (*self).with_mut(|__s: &mut MinHeap| __s.size.prefix_dec()); + let __rhs = (*self).with(|__s: &MinHeap| { + (__s.arr.as_ref().unwrap().borrow() + [((*self).with(|__s: &MinHeap| __s.size) as usize) as usize]) + .clone() + }); + (*self).with_mut(|__s: &mut MinHeap| { + __s.arr.as_ref().unwrap().borrow_mut()[(0_usize) as usize] = __rhs + }); ({ MinHeapImpl::Heapify(self, 0) }); return (*out.borrow()).clone(); } fn Insert(&self, node: Ptr) { let node: Value> = Rc::new(RefCell::new(node)); - (*(*(*self).upgrade().deref()).size.borrow_mut()).prefix_inc(); - let i: Value = Rc::new(RefCell::new( - ((*(*(*self).upgrade().deref()).size.borrow()) - 1), - )); + (*self).with_mut(|__s: &mut MinHeap| __s.size.prefix_inc()); + let i: Value = Rc::new(RefCell::new(((*self).with(|__s: &MinHeap| __s.size) - 1))); 'loop_: while ((*i.borrow()) != 0) && ({ - let _lhs = (*(*(*node.borrow()).upgrade().deref()).freq.borrow()); - _lhs < (*(*(*(*(*self).upgrade().deref()).arr.borrow()) - .as_ref() - .unwrap() - .borrow()[((((*i.borrow()) - 1) / 2) as usize) as usize] - .upgrade() - .deref()) - .freq - .borrow()) + let _lhs = (*node.borrow()).with(|__s: &MinHeapNode| __s.freq); + _lhs < { + (*(*(*self).upgrade().deref()).arr.as_ref().unwrap().borrow() + [((((*i.borrow()) - 1) / 2) as usize) as usize] + .upgrade() + .deref()) + .freq + } }) { - let __rhs = ((*(*(*self).upgrade().deref()).arr.borrow()) - .as_ref() - .unwrap() - .borrow()[((((*i.borrow()) - 1) / 2) as usize) as usize]) - .clone(); - (*(*(*self).upgrade().deref()).arr.borrow()) - .as_ref() - .unwrap() - .borrow_mut()[((*i.borrow()) as usize) as usize] = __rhs; + let __rhs = (*self).with(|__s: &MinHeap| { + (__s.arr.as_ref().unwrap().borrow()[((((*i.borrow()) - 1) / 2) as usize) as usize]) + .clone() + }); + (*self).with_mut(|__s: &mut MinHeap| { + __s.arr.as_ref().unwrap().borrow_mut()[((*i.borrow()) as usize) as usize] = __rhs + }); let __rhs = (((*i.borrow()) - 1) / 2); (*i.borrow_mut()) = __rhs; } - (*(*(*self).upgrade().deref()).arr.borrow()) - .as_ref() - .unwrap() - .borrow_mut()[((*i.borrow()) as usize) as usize] = (*node.borrow()).clone(); + (*self).with_mut(|__s: &mut MinHeap| { + __s.arr.as_ref().unwrap().borrow_mut()[((*i.borrow()) as usize) as usize] = + (*node.borrow()).clone() + }); } fn Build( &self, @@ -483,21 +474,21 @@ impl MinHeapImpl for Ptr { let n: Value = Rc::new(RefCell::new(n)); let i: Value = Rc::new(RefCell::new(0)); 'loop_: while ((*i.borrow()) < (*n.borrow())) { - (*(*(*self).upgrade().deref()).arr.borrow()) - .as_ref() - .unwrap() - .borrow_mut()[((*(*(*self).upgrade().deref()).size.borrow_mut()) - .postfix_inc() as usize) as usize] = ({ + let __rhs = ({ let _data: u8 = (*data.upgrade().deref()).as_ref().unwrap().borrow() [((*i.borrow()) as usize) as usize]; let _freq: i32 = (*freq.upgrade().deref()).as_ref().unwrap().borrow() [((*i.borrow()) as usize) as usize]; MinHeapImpl::Alloc(self, _data, _freq) }); + let __idx = ((*self).with_mut(|__s: &mut MinHeap| __s.size.postfix_inc()) as usize); + (*self).with_mut(|__s: &mut MinHeap| { + __s.arr.as_ref().unwrap().borrow_mut()[(__idx) as usize] = __rhs + }); (*i.borrow_mut()).prefix_inc(); } let i: Value = Rc::new(RefCell::new( - (((*(*(*self).upgrade().deref()).size.borrow()) - 2) / 2), + (((*self).with(|__s: &MinHeap| __s.size) - 2) / 2), )); 'loop_: while ((*i.borrow()) >= 0) { ({ MinHeapImpl::Heapify(self, (*i.borrow())) }); @@ -505,17 +496,18 @@ impl MinHeapImpl for Ptr { } } fn move_assign(&self, _a0: Ptr) -> Ptr { - let __rhs = (*(*_a0.upgrade().deref()).size.borrow()); - (*(*(*self).upgrade().deref()).size.borrow_mut()) = __rhs; - let __rhs = (*(*_a0.upgrade().deref()).capacity.borrow()); - (*(*(*self).upgrade().deref()).capacity.borrow_mut()) = __rhs; - ((*(*self).upgrade().deref()).arr.as_pointer() + let __rhs = _a0.with(|__s: &MinHeap| __s.size); + (*self).with_mut(|__s: &mut MinHeap| __s.size = __rhs); + let __rhs = _a0.with(|__s: &MinHeap| __s.capacity); + (*self).with_mut(|__s: &mut MinHeap| __s.capacity = __rhs); + ((field_ptr!((*self), arr) as Ptr]>>>>) as Ptr]>>>>) - .write((*(*_a0.upgrade().deref()).arr.borrow_mut()).take()); - let __rhs = (*(*_a0.upgrade().deref()).next.borrow()); - (*(*(*self).upgrade().deref()).next.borrow_mut()) = __rhs; - ((*(*self).upgrade().deref()).alloc.as_pointer() as Ptr>>>) - .write((*(*_a0.upgrade().deref()).alloc.borrow_mut()).take()); + .write(_a0.with_mut(|__s: &mut MinHeap| __s.arr.take())); + let __rhs = _a0.with(|__s: &MinHeap| __s.next); + (*self).with_mut(|__s: &mut MinHeap| __s.next = __rhs); + ((field_ptr!((*self), alloc) as Ptr>>>) + as Ptr>>>) + .write(_a0.with_mut(|__s: &mut MinHeap| __s.alloc.take())); return (*self).clone(); } } @@ -524,8 +516,8 @@ pub trait MinHeapNodeImpl { } impl MinHeapNodeImpl for Ptr { fn IsLeaf(&self) -> bool { - return ((*(*(*self).upgrade().deref()).left.borrow()).is_null()) - && ((*(*(*self).upgrade().deref()).right.borrow()).is_null()); + return (((*self).with(|__s: &MinHeapNode| (__s.left).clone())).is_null()) + && (((*self).with(|__s: &MinHeapNode| (__s.right).clone())).is_null()); } } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/immutable-deref-on-func-call.rs b/tests/unit/out/refcount/immutable-deref-on-func-call.rs index fc28fecf22..02dc722c2e 100644 --- a/tests/unit/out/refcount/immutable-deref-on-func-call.rs +++ b/tests/unit/out/refcount/immutable-deref-on-func-call.rs @@ -6,29 +6,21 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Item { - pub value: Value, -} -impl Clone for Item { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - value: Rc::new(RefCell::new((*self.value.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub value: i32, } impl ByteRepr for Item { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.value.borrow()).to_bytes(&mut buf[0..4]); + self.value.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - value: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + value: ::from_bytes(&buf[0..4]), } } } @@ -42,23 +34,23 @@ fn main_0() -> i32 { .map(|_| ::default()) .collect::>(), ))); - (*(*(*arr.borrow()).offset((0) as isize).upgrade().deref()) - .value - .borrow_mut()) = 1; - (*(*(*arr.borrow()).offset((1) as isize).upgrade().deref()) - .value - .borrow_mut()) = 2; + (*arr.borrow()) + .offset((0) as isize) + .with_mut(|__s: &mut Item| __s.value = 1); + (*arr.borrow()) + .offset((1) as isize) + .with_mut(|__s: &mut Item| __s.value = 2); ({ let _other: Ptr = ((*arr.borrow()).offset((1) as isize)); ItemImpl::foo(&(*arr.borrow()).offset((0) as isize), _other) }); let result: Value = Rc::new(RefCell::new( - ((*(*(*arr.borrow()).offset((0) as isize).upgrade().deref()) - .value - .borrow()) - + (*(*(*arr.borrow()).offset((1) as isize).upgrade().deref()) - .value - .borrow())), + ((*arr.borrow()) + .offset((0) as isize) + .with(|__s: &Item| __s.value) + + (*arr.borrow()) + .offset((1) as isize) + .with(|__s: &Item| __s.value)), )); (*arr.borrow()).delete(); assert!(((*result.borrow()) == 11)); @@ -70,7 +62,7 @@ pub trait ItemImpl { impl ItemImpl for Ptr { fn foo(&self, other: Ptr) { let other: Value> = Rc::new(RefCell::new(other)); - (*(*(*other.borrow()).upgrade().deref()).value.borrow_mut()) = 10; + (*other.borrow()).with_mut(|__s: &mut Item| __s.value = 10); } } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/implicit_autoref.rs b/tests/unit/out/refcount/implicit_autoref.rs index fedfc640ff..f6369fb362 100644 --- a/tests/unit/out/refcount/implicit_autoref.rs +++ b/tests/unit/out/refcount/implicit_autoref.rs @@ -6,20 +6,37 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Record, VaArg, FnPtrArg, Default)] pub struct Holder { + #[offset(0)] pub v: Value>, } impl Clone for Holder { fn clone(&self) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*self.v.borrow()).clone())), + v: { Rc::new(RefCell::new((*self.v.borrow()).clone())) }, })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } } +<<<<<<< HEAD impl ByteRepr for Holder {} +======= +impl ByteRepr for Holder { + fn byte_size() -> usize { + 24 + } + fn to_bytes(&self, buf: &mut [u8]) { + self.v.to_bytes(&mut buf[0..24]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + v: >>::from_bytes(&buf[0..24]), + } + } +} +>>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub fn write_through_0(p: Ptr) { let p: Value> = Rc::new(RefCell::new(p)); (*p.borrow()).write(42); @@ -46,13 +63,9 @@ fn main_0() -> i32 { (*(*h.borrow()).v.borrow_mut()).push(50); let hp: Value> = Rc::new(RefCell::new((h.as_pointer()))); let b: Value = Rc::new(RefCell::new( - (((*(*hp.borrow()).upgrade().deref()).v.as_pointer() as Ptr) - .offset(0_usize) - .read()), + (*hp.borrow()).with(|__s: &Holder| (*__s.v.borrow())[(0_usize) as usize]), )); - ((*(*hp.borrow()).upgrade().deref()).v.as_pointer() as Ptr) - .offset(1_usize) - .write(60); + (*hp.borrow()).with(|__s: &Holder| (*__s.v.borrow_mut())[(1_usize) as usize] = 60); assert!(((*a.borrow()) == 10)); assert!( ((((Ptr::>::decay(&(*p.borrow()))) as Ptr) @@ -61,12 +74,7 @@ fn main_0() -> i32 { == 30) ); assert!(((*b.borrow()) == 40)); - assert!( - ((((*(*hp.borrow()).upgrade().deref()).v.as_pointer() as Ptr) - .offset(1_usize) - .read()) - == 60) - ); + assert!(((*hp.borrow()).with(|__s: &Holder| { (*__s.v.borrow())[(1_usize) as usize] }) == 60)); ({ write_through_0( ((Ptr::>::decay(&(*p.borrow())) as Ptr).offset(0_usize as isize)), diff --git a/tests/unit/out/refcount/in_class_field_initializer.rs b/tests/unit/out/refcount/in_class_field_initializer.rs index 628f36563e..87cc449683 100644 --- a/tests/unit/out/refcount/in_class_field_initializer.rs +++ b/tests/unit/out/refcount/in_class_field_initializer.rs @@ -6,27 +6,16 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg)] +#[derive(Clone, Record, VaArg, FnPtrArg)] pub struct Inner { - pub x: Value, - pub y: Value, -} -impl Clone for Inner { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - x: Rc::new(RefCell::new((*self.x.borrow()))), - y: Rc::new(RefCell::new((*self.y.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub x: i32, + #[offset(4)] + pub y: i32, } impl Default for Inner { fn default() -> Self { - Inner { - x: Rc::new(RefCell::new(3)), - y: Rc::new(RefCell::new(4)), - } + Inner { x: 3, y: 4 } } } impl ByteRepr for Inner { @@ -34,42 +23,34 @@ impl ByteRepr for Inner { 8 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.x.borrow()).to_bytes(&mut buf[0..4]); - (*self.y.borrow()).to_bytes(&mut buf[4..8]); + self.x.to_bytes(&mut buf[0..4]); + self.y.to_bytes(&mut buf[4..8]); } fn from_bytes(buf: &[u8]) -> Self { Self { - x: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - y: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + x: ::from_bytes(&buf[0..4]), + y: ::from_bytes(&buf[4..8]), } } } -#[derive(VaArg, FnPtrArg)] +#[derive(Clone, Record, VaArg, FnPtrArg)] pub struct S { - pub a: Value, - pub b: Value, - pub c: Value, - pub d: Value, -} -impl Clone for S { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - a: Rc::new(RefCell::new((*self.a.borrow()))), - b: Rc::new(RefCell::new((*self.b.borrow()))), - c: Rc::new(RefCell::new((*self.c.borrow()).clone())), - d: Rc::new(RefCell::new((*self.d.borrow()).clone())), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub a: i32, + #[offset(4)] + pub b: u8, + #[offset(8)] + pub c: Inner, + #[offset(16)] + pub d: Inner, } impl Default for S { fn default() -> Self { S { - a: Rc::new(RefCell::new(1)), - b: Rc::new(RefCell::new(2_u8)), - c: Rc::new(RefCell::new(::default())), - d: >::default(), + a: 1, + b: 2_u8, + c: ::default(), + d: ::default(), } } } @@ -78,42 +59,34 @@ impl ByteRepr for S { 24 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.a.borrow()).to_bytes(&mut buf[0..4]); - (*self.b.borrow()).to_bytes(&mut buf[4..5]); - (*self.c.borrow()).to_bytes(&mut buf[8..16]); - (*self.d.borrow()).to_bytes(&mut buf[16..24]); + self.a.to_bytes(&mut buf[0..4]); + self.b.to_bytes(&mut buf[4..5]); + self.c.to_bytes(&mut buf[8..16]); + self.d.to_bytes(&mut buf[16..24]); } fn from_bytes(buf: &[u8]) -> Self { Self { - a: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - b: Rc::new(RefCell::new(::from_bytes(&buf[4..5]))), - c: Rc::new(RefCell::new(::from_bytes(&buf[8..16]))), - d: Rc::new(RefCell::new(::from_bytes(&buf[16..24]))), + a: ::from_bytes(&buf[0..4]), + b: ::from_bytes(&buf[4..5]), + c: ::from_bytes(&buf[8..16]), + d: ::from_bytes(&buf[16..24]), } } } -#[derive(VaArg, FnPtrArg)] +#[derive(Clone, Record, VaArg, FnPtrArg)] pub struct Boxed_int_ { - pub v: Value, - pub tag: Value, + #[offset(0)] + pub v: i32, + #[offset(4)] + pub tag: i32, } impl Boxed_int_ { pub fn new(x: i32, t: i32) -> Self { let x: Value = Rc::new(RefCell::new(x)); let t: Value = Rc::new(RefCell::new(t)); let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*x.borrow()))), - tag: Rc::new(RefCell::new((*t.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } -} -impl Clone for Boxed_int_ { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*self.v.borrow()))), - tag: Rc::new(RefCell::new((*self.tag.borrow()))), + v: (*x.borrow()), + tag: (*t.borrow()), })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() @@ -122,8 +95,8 @@ impl Clone for Boxed_int_ { impl Default for Boxed_int_ { fn default() -> Self { Boxed_int_ { - v: Rc::new(RefCell::new(0_i32)), - tag: Rc::new(RefCell::new(0_i32)), + v: 0_i32, + tag: 0_i32, } } } @@ -132,13 +105,13 @@ impl ByteRepr for Boxed_int_ { 8 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.v.borrow()).to_bytes(&mut buf[0..4]); - (*self.tag.borrow()).to_bytes(&mut buf[4..8]); + self.v.to_bytes(&mut buf[0..4]); + self.tag.to_bytes(&mut buf[4..8]); } fn from_bytes(buf: &[u8]) -> Self { Self { - v: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - tag: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + v: ::from_bytes(&buf[0..4]), + tag: ::from_bytes(&buf[4..8]), } } } @@ -148,15 +121,15 @@ pub fn main() { } fn main_0() -> i32 { let s: Value = Rc::new(RefCell::new(::default())); - assert!(((*(*s.borrow()).a.borrow()) == 1)); - assert!((((*(*s.borrow()).b.borrow()) as i32) == 2)); - assert!(((*(*(*s.borrow()).c.borrow()).x.borrow()) == 3)); - assert!(((*(*(*s.borrow()).c.borrow()).y.borrow()) == 4)); - assert!(((*(*(*s.borrow()).d.borrow()).x.borrow()) == 3)); - assert!(((*(*(*s.borrow()).d.borrow()).y.borrow()) == 4)); + assert!(({ (*s.borrow()).a } == 1)); + assert!((({ (*s.borrow()).b } as i32) == 2)); + assert!(({ (*s.borrow()).c.x } == 3)); + assert!(({ (*s.borrow()).c.y } == 4)); + assert!(({ (*s.borrow()).d.x } == 3)); + assert!(({ (*s.borrow()).d.y } == 4)); let boxed: Value = Rc::new(RefCell::new(Boxed_int_::new({ 5 }, { 9 }))); - assert!(((*(*boxed.borrow()).v.borrow()) == 5)); - assert!(((*(*boxed.borrow()).tag.borrow()) == 9)); + assert!(({ (*boxed.borrow()).v } == 5)); + assert!(({ (*boxed.borrow()).tag } == 9)); return 0; } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/init.rs b/tests/unit/out/refcount/init.rs index 8eba663076..f424e07479 100644 --- a/tests/unit/out/refcount/init.rs +++ b/tests/unit/out/refcount/init.rs @@ -6,29 +6,21 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct X { - pub x: Value, -} -impl Clone for X { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - x: Rc::new(RefCell::new((*self.x.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub x: i32, } impl ByteRepr for X { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.x.borrow()).to_bytes(&mut buf[0..4]); + self.x.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - x: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + x: ::from_bytes(&buf[0..4]), } } } @@ -47,10 +39,10 @@ fn main_0() -> i32 { let z: Value> = Rc::new(RefCell::new((*p.borrow()).clone())); let xx: Value = Rc::new(RefCell::new(::default())); let zz: Value> = Rc::new(RefCell::new((xx.as_pointer()))); - (*(*xx.borrow()).x.borrow_mut()) = 1; - (*q.borrow_mut()) = ((*xx.borrow()).x.as_pointer()); - (*q.borrow_mut()) = ((*(*zz.borrow()).upgrade().deref()).x.as_pointer()); - (*(*(*zz.borrow()).upgrade().deref()).x.borrow_mut()) = 2; + (*xx.borrow_mut()).x = 1; + (*q.borrow_mut()) = (field_ptr!(xx, x)); + (*q.borrow_mut()) = (field_ptr!((*zz.borrow()), x)); + (*zz.borrow()).with_mut(|__s: &mut X| __s.x = 2); let ww: Value = Rc::new(RefCell::new((*xx.borrow()).clone())); (*ww.borrow_mut()) = (*xx.borrow()).clone(); let aa: Value = Rc::new(RefCell::new(({ func_0() }))); diff --git a/tests/unit/out/refcount/kruskal.rs b/tests/unit/out/refcount/kruskal.rs index 10a25232cd..c1cc99b5d0 100644 --- a/tests/unit/out/refcount/kruskal.rs +++ b/tests/unit/out/refcount/kruskal.rs @@ -6,37 +6,29 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Edge { - pub u: Value, - pub v: Value, - pub weight: Value, -} -impl Clone for Edge { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - u: Rc::new(RefCell::new((*self.u.borrow()))), - v: Rc::new(RefCell::new((*self.v.borrow()))), - weight: Rc::new(RefCell::new((*self.weight.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub u: i32, + #[offset(4)] + pub v: i32, + #[offset(8)] + pub weight: f64, } impl ByteRepr for Edge { fn byte_size() -> usize { 16 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.u.borrow()).to_bytes(&mut buf[0..4]); - (*self.v.borrow()).to_bytes(&mut buf[4..8]); - (*self.weight.borrow()).to_bytes(&mut buf[8..16]); + self.u.to_bytes(&mut buf[0..4]); + self.v.to_bytes(&mut buf[4..8]); + self.weight.to_bytes(&mut buf[8..16]); } fn from_bytes(buf: &[u8]) -> Self { Self { - u: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - v: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), - weight: Rc::new(RefCell::new(::from_bytes(&buf[8..16]))), + u: ::from_bytes(&buf[0..4]), + v: ::from_bytes(&buf[4..8]), + weight: ::from_bytes(&buf[8..16]), } } } @@ -53,11 +45,13 @@ pub fn partition_0(arr: Ptr>>>, start: i32, end: i32) - let i: Value = Rc::new(RefCell::new(((*start.borrow()) + 1))); 'loop_: while ((*i.borrow()) <= (*end.borrow())) { if { - let _lhs = (*(*arr.upgrade().deref()).as_ref().unwrap().borrow() - [((*i.borrow()) as usize) as usize] - .weight - .borrow()); - _lhs <= (*(*pivot.upgrade().deref()).weight.borrow()) + let _lhs = (*arr.upgrade().deref()) + .as_ref() + .unwrap() + .as_pointer() + .offset(((*i.borrow()) as usize)) + .with(|__s: &Edge| __s.weight); + _lhs <= pivot.with(|__s: &Edge| __s.weight) } { (*count.borrow_mut()).postfix_inc(); } @@ -65,122 +59,150 @@ pub fn partition_0(arr: Ptr>>>, start: i32, end: i32) - } let pidx: Value = Rc::new(RefCell::new(((*start.borrow()) + (*count.borrow())))); let tmp: Value = Rc::new(RefCell::new(Edge { - u: Rc::new(RefCell::new( - (*(*arr.upgrade().deref()).as_ref().unwrap().borrow() - [((*pidx.borrow()) as usize) as usize] - .u - .borrow()), - )), - v: Rc::new(RefCell::new( - (*(*arr.upgrade().deref()).as_ref().unwrap().borrow() - [((*pidx.borrow()) as usize) as usize] - .v - .borrow()), - )), - weight: Rc::new(RefCell::new( - (*(*arr.upgrade().deref()).as_ref().unwrap().borrow() - [((*pidx.borrow()) as usize) as usize] - .weight - .borrow()), - )), + u: { + (*arr.upgrade().deref()) + .as_ref() + .unwrap() + .as_pointer() + .offset(((*pidx.borrow()) as usize)) + .with(|__s: &Edge| __s.u) + }, + v: { + (*arr.upgrade().deref()) + .as_ref() + .unwrap() + .as_pointer() + .offset(((*pidx.borrow()) as usize)) + .with(|__s: &Edge| __s.v) + }, + weight: { + (*arr.upgrade().deref()) + .as_ref() + .unwrap() + .as_pointer() + .offset(((*pidx.borrow()) as usize)) + .with(|__s: &Edge| __s.weight) + }, })); let __rhs = Edge { - u: Rc::new(RefCell::new( - (*(*arr.upgrade().deref()).as_ref().unwrap().borrow() - [((*start.borrow()) as usize) as usize] - .u - .borrow()), - )), - v: Rc::new(RefCell::new( - (*(*arr.upgrade().deref()).as_ref().unwrap().borrow() - [((*start.borrow()) as usize) as usize] - .v - .borrow()), - )), - weight: Rc::new(RefCell::new( - (*(*arr.upgrade().deref()).as_ref().unwrap().borrow() - [((*start.borrow()) as usize) as usize] - .weight - .borrow()), - )), + u: { + (*arr.upgrade().deref()) + .as_ref() + .unwrap() + .as_pointer() + .offset(((*start.borrow()) as usize)) + .with(|__s: &Edge| __s.u) + }, + v: { + (*arr.upgrade().deref()) + .as_ref() + .unwrap() + .as_pointer() + .offset(((*start.borrow()) as usize)) + .with(|__s: &Edge| __s.v) + }, + weight: { + (*arr.upgrade().deref()) + .as_ref() + .unwrap() + .as_pointer() + .offset(((*start.borrow()) as usize)) + .with(|__s: &Edge| __s.weight) + }, }; (*arr.upgrade().deref()).as_ref().unwrap().borrow_mut()[((*pidx.borrow()) as usize) as usize] = __rhs; (*arr.upgrade().deref()).as_ref().unwrap().borrow_mut() [((*start.borrow()) as usize) as usize] = Edge { - u: Rc::new(RefCell::new((*(*tmp.borrow()).u.borrow()))), - v: Rc::new(RefCell::new((*(*tmp.borrow()).v.borrow()))), - weight: Rc::new(RefCell::new((*(*tmp.borrow()).weight.borrow()))), + u: { (*tmp.borrow()).u }, + v: { (*tmp.borrow()).v }, + weight: { (*tmp.borrow()).weight }, }; let i: Value = Rc::new(RefCell::new((*start.borrow()))); let j: Value = Rc::new(RefCell::new((*end.borrow()))); 'loop_: while ((*i.borrow()) < (*pidx.borrow())) && ((*j.borrow()) > (*pidx.borrow())) { 'loop_: while { - let _lhs = (*(*arr.upgrade().deref()).as_ref().unwrap().borrow() - [((*i.borrow()) as usize) as usize] - .weight - .borrow()); - _lhs <= (*(*pivot.upgrade().deref()).weight.borrow()) + let _lhs = (*arr.upgrade().deref()) + .as_ref() + .unwrap() + .as_pointer() + .offset(((*i.borrow()) as usize)) + .with(|__s: &Edge| __s.weight); + _lhs <= pivot.with(|__s: &Edge| __s.weight) } { (*i.borrow_mut()).prefix_inc(); } 'loop_: while { - let _lhs = (*(*arr.upgrade().deref()).as_ref().unwrap().borrow() - [((*j.borrow()) as usize) as usize] - .weight - .borrow()); - _lhs > (*(*pivot.upgrade().deref()).weight.borrow()) + let _lhs = (*arr.upgrade().deref()) + .as_ref() + .unwrap() + .as_pointer() + .offset(((*j.borrow()) as usize)) + .with(|__s: &Edge| __s.weight); + _lhs > pivot.with(|__s: &Edge| __s.weight) } { (*j.borrow_mut()).prefix_dec(); } if ((*i.borrow()) < (*pidx.borrow())) && ((*j.borrow()) > (*pidx.borrow())) { (*tmp.borrow_mut()) = Edge { - u: Rc::new(RefCell::new( - (*(*arr.upgrade().deref()).as_ref().unwrap().borrow() - [((*i.borrow()) as usize) as usize] - .u - .borrow()), - )), - v: Rc::new(RefCell::new( - (*(*arr.upgrade().deref()).as_ref().unwrap().borrow() - [((*i.borrow()) as usize) as usize] - .v - .borrow()), - )), - weight: Rc::new(RefCell::new( - (*(*arr.upgrade().deref()).as_ref().unwrap().borrow() - [((*i.borrow()) as usize) as usize] - .weight - .borrow()), - )), + u: { + (*arr.upgrade().deref()) + .as_ref() + .unwrap() + .as_pointer() + .offset(((*i.borrow()) as usize)) + .with(|__s: &Edge| __s.u) + }, + v: { + (*arr.upgrade().deref()) + .as_ref() + .unwrap() + .as_pointer() + .offset(((*i.borrow()) as usize)) + .with(|__s: &Edge| __s.v) + }, + weight: { + (*arr.upgrade().deref()) + .as_ref() + .unwrap() + .as_pointer() + .offset(((*i.borrow()) as usize)) + .with(|__s: &Edge| __s.weight) + }, }; let __rhs = Edge { - u: Rc::new(RefCell::new( - (*(*arr.upgrade().deref()).as_ref().unwrap().borrow() - [((*j.borrow()) as usize) as usize] - .u - .borrow()), - )), - v: Rc::new(RefCell::new( - (*(*arr.upgrade().deref()).as_ref().unwrap().borrow() - [((*j.borrow()) as usize) as usize] - .v - .borrow()), - )), - weight: Rc::new(RefCell::new( - (*(*arr.upgrade().deref()).as_ref().unwrap().borrow() - [((*j.borrow()) as usize) as usize] - .weight - .borrow()), - )), + u: { + (*arr.upgrade().deref()) + .as_ref() + .unwrap() + .as_pointer() + .offset(((*j.borrow()) as usize)) + .with(|__s: &Edge| __s.u) + }, + v: { + (*arr.upgrade().deref()) + .as_ref() + .unwrap() + .as_pointer() + .offset(((*j.borrow()) as usize)) + .with(|__s: &Edge| __s.v) + }, + weight: { + (*arr.upgrade().deref()) + .as_ref() + .unwrap() + .as_pointer() + .offset(((*j.borrow()) as usize)) + .with(|__s: &Edge| __s.weight) + }, }; (*arr.upgrade().deref()).as_ref().unwrap().borrow_mut() [((*i.borrow()) as usize) as usize] = __rhs; (*arr.upgrade().deref()).as_ref().unwrap().borrow_mut() [((*j.borrow()) as usize) as usize] = Edge { - u: Rc::new(RefCell::new((*(*tmp.borrow()).u.borrow()))), - v: Rc::new(RefCell::new((*(*tmp.borrow()).v.borrow()))), - weight: Rc::new(RefCell::new((*(*tmp.borrow()).weight.borrow()))), + u: { (*tmp.borrow()).u }, + v: { (*tmp.borrow()).v }, + weight: { (*tmp.borrow()).weight }, }; (*i.borrow_mut()).postfix_inc(); (*j.borrow_mut()).postfix_dec(); @@ -215,98 +237,128 @@ pub fn quicksort_1(arr: Ptr>>>, start: i32, end: i32) { quicksort_1(_arr, _start, _end) }); } -#[derive(Default)] +#[derive(Record, Default)] pub struct DisjointSet { - pub rank: Value>>>, - pub parent: Value>>>, - pub n: Value, + #[offset(0)] + pub rank: Option>>, + #[offset(8)] + pub parent: Option>>, + #[offset(16)] + pub n: i32, } impl DisjointSet { pub fn move_from(_a0: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - rank: Rc::new(RefCell::new( - (*(*_a0.upgrade().deref()).rank.borrow_mut()).take(), - )), - parent: Rc::new(RefCell::new( - (*(*_a0.upgrade().deref()).parent.borrow_mut()).take(), - )), - n: Rc::new(RefCell::new((*(*_a0.upgrade().deref()).n.borrow()))), + rank: { _a0.with_mut(|__s: &mut DisjointSet| __s.rank.take()) }, + parent: { _a0.with_mut(|__s: &mut DisjointSet| __s.parent.take()) }, + n: { _a0.with(|__s: &DisjointSet| __s.n) }, })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } } +<<<<<<< HEAD impl ByteRepr for DisjointSet {} #[derive(Default)] +======= +impl ByteRepr for DisjointSet { + fn byte_size() -> usize { + 24 + } + fn to_bytes(&self, buf: &mut [u8]) { + self.rank.to_bytes(&mut buf[0..8]); + self.parent.to_bytes(&mut buf[8..16]); + self.n.to_bytes(&mut buf[16..20]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + rank: >>>::from_bytes(&buf[0..8]), + parent: >>>::from_bytes(&buf[8..16]), + n: ::from_bytes(&buf[16..20]), + } + } +} +#[derive(Record, Default)] +>>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub struct Graph { - pub edges: Value>>>, - pub V: Value, - pub E: Value, + #[offset(0)] + pub edges: Option>>, + #[offset(8)] + pub V: i32, + #[offset(12)] + pub E: i32, } impl Graph { pub fn move_from(_a0: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - edges: Rc::new(RefCell::new( - (*(*_a0.upgrade().deref()).edges.borrow_mut()).take(), - )), - V: Rc::new(RefCell::new((*(*_a0.upgrade().deref()).V.borrow()))), - E: Rc::new(RefCell::new((*(*_a0.upgrade().deref()).E.borrow()))), + edges: { _a0.with_mut(|__s: &mut Graph| __s.edges.take()) }, + V: { _a0.with(|__s: &Graph| __s.V) }, + E: { _a0.with(|__s: &Graph| __s.E) }, })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } } +<<<<<<< HEAD impl ByteRepr for Graph {} +======= +impl ByteRepr for Graph { + fn byte_size() -> usize { + 16 + } + fn to_bytes(&self, buf: &mut [u8]) { + self.edges.to_bytes(&mut buf[0..8]); + self.V.to_bytes(&mut buf[8..12]); + self.E.to_bytes(&mut buf[12..16]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + edges: >>>::from_bytes(&buf[0..8]), + V: ::from_bytes(&buf[8..12]), + E: ::from_bytes(&buf[12..16]), + } + } +} +>>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub fn MSTKruskal_2(graph: Ptr) -> f64 { ({ - let _arr: Ptr>>> = (*graph.upgrade().deref()).edges.as_pointer(); - let _end: i32 = ((*(*graph.upgrade().deref()).E.borrow()) - 1); + let _arr: Ptr>>> = field_ptr!(graph, edges); + let _end: i32 = (graph.with(|__s: &Graph| __s.E) - 1); quicksort_1(_arr, 0, _end) }); let set: Value = Rc::new(RefCell::new(DisjointSet { - rank: Rc::new(RefCell::new(Some(Rc::new(RefCell::new( - (0..((*(*graph.upgrade().deref()).V.borrow()) as usize)) - .map(|_| ::default()) - .collect::>(), - ))))), - parent: Rc::new(RefCell::new(Some(Rc::new(RefCell::new( - (0..((*(*graph.upgrade().deref()).V.borrow()) as usize)) - .map(|_| ::default()) - .collect::>(), - ))))), - n: Rc::new(RefCell::new((*(*graph.upgrade().deref()).V.borrow()))), + rank: { + Some(Rc::new(RefCell::new( + (0..(graph.with(|__s: &Graph| __s.V) as usize)) + .map(|_| ::default()) + .collect::>(), + ))) + }, + parent: { + Some(Rc::new(RefCell::new( + (0..(graph.with(|__s: &Graph| __s.V) as usize)) + .map(|_| ::default()) + .collect::>(), + ))) + }, + n: { graph.with(|__s: &Graph| __s.V) }, })); ({ DisjointSetImpl::makeSet(&set.as_pointer()) }); let total_weight: Value = Rc::new(RefCell::new(0_f64)); let i: Value = Rc::new(RefCell::new(0)); 'loop_: while { let _lhs = (*i.borrow()); - _lhs < (*(*graph.upgrade().deref()).E.borrow()) + _lhs < graph.with(|__s: &Graph| __s.E) } { - let x: Value = Rc::new(RefCell::new( - (*(*(*graph.upgrade().deref()).edges.borrow()) - .as_ref() - .unwrap() - .borrow()[((*i.borrow()) as usize) as usize] - .u - .borrow()), - )); - let y: Value = Rc::new(RefCell::new( - (*(*(*graph.upgrade().deref()).edges.borrow()) - .as_ref() - .unwrap() - .borrow()[((*i.borrow()) as usize) as usize] - .v - .borrow()), - )); - let w: Value = Rc::new(RefCell::new( - (*(*(*graph.upgrade().deref()).edges.borrow()) - .as_ref() - .unwrap() - .borrow()[((*i.borrow()) as usize) as usize] - .weight - .borrow()), - )); + let x: Value = Rc::new(RefCell::new(graph.with(|__s: &Graph| { + __s.edges.as_ref().unwrap().borrow()[((*i.borrow()) as usize) as usize].u + }))); + let y: Value = Rc::new(RefCell::new(graph.with(|__s: &Graph| { + __s.edges.as_ref().unwrap().borrow()[((*i.borrow()) as usize) as usize].v + }))); + let w: Value = Rc::new(RefCell::new(graph.with(|__s: &Graph| { + __s.edges.as_ref().unwrap().borrow()[((*i.borrow()) as usize) as usize].weight + }))); if (({ DisjointSetImpl::find(&set.as_pointer(), (*x.borrow())) }) != ({ DisjointSetImpl::find(&set.as_pointer(), (*y.borrow())) })) { @@ -325,53 +377,38 @@ fn main_0() -> i32 { let V: Value = Rc::new(RefCell::new(4)); let E: Value = Rc::new(RefCell::new(5)); let graph: Value = Rc::new(RefCell::new(Graph { - edges: Rc::new(RefCell::new(Some(Rc::new(RefCell::new( + edges: Some(Rc::new(RefCell::new( (0..((*E.borrow()) as usize)) .map(|_| ::default()) .collect::>(), - ))))), - V: Rc::new(RefCell::new((*V.borrow()))), - E: Rc::new(RefCell::new((*E.borrow()))), + ))), + V: (*V.borrow()), + E: (*E.borrow()), })); - (*(*graph.borrow()).edges.borrow()) - .as_ref() - .unwrap() - .borrow_mut()[(0_usize) as usize] = Edge { - u: Rc::new(RefCell::new(0)), - v: Rc::new(RefCell::new(1)), - weight: Rc::new(RefCell::new(10_f64)), + (*graph.borrow()).edges.as_ref().unwrap().borrow_mut()[(0_usize) as usize] = Edge { + u: 0, + v: 1, + weight: 10_f64, }; - (*(*graph.borrow()).edges.borrow()) - .as_ref() - .unwrap() - .borrow_mut()[(1_usize) as usize] = Edge { - u: Rc::new(RefCell::new(1)), - v: Rc::new(RefCell::new(3)), - weight: Rc::new(RefCell::new(15_f64)), + (*graph.borrow()).edges.as_ref().unwrap().borrow_mut()[(1_usize) as usize] = Edge { + u: 1, + v: 3, + weight: 15_f64, }; - (*(*graph.borrow()).edges.borrow()) - .as_ref() - .unwrap() - .borrow_mut()[(2_usize) as usize] = Edge { - u: Rc::new(RefCell::new(2)), - v: Rc::new(RefCell::new(3)), - weight: Rc::new(RefCell::new(4_f64)), + (*graph.borrow()).edges.as_ref().unwrap().borrow_mut()[(2_usize) as usize] = Edge { + u: 2, + v: 3, + weight: 4_f64, }; - (*(*graph.borrow()).edges.borrow()) - .as_ref() - .unwrap() - .borrow_mut()[(3_usize) as usize] = Edge { - u: Rc::new(RefCell::new(2)), - v: Rc::new(RefCell::new(0)), - weight: Rc::new(RefCell::new(6_f64)), + (*graph.borrow()).edges.as_ref().unwrap().borrow_mut()[(3_usize) as usize] = Edge { + u: 2, + v: 0, + weight: 6_f64, }; - (*(*graph.borrow()).edges.borrow()) - .as_ref() - .unwrap() - .borrow_mut()[(4_usize) as usize] = Edge { - u: Rc::new(RefCell::new(0)), - v: Rc::new(RefCell::new(3)), - weight: Rc::new(RefCell::new(5_f64)), + (*graph.borrow()).edges.as_ref().unwrap().borrow_mut()[(4_usize) as usize] = Edge { + u: 0, + v: 3, + weight: 5_f64, }; let total_weight: Value = Rc::new(RefCell::new(({ MSTKruskal_2(graph.as_pointer()) }))); assert!(((*total_weight.borrow()) == 19_f64)); @@ -386,43 +423,36 @@ pub trait DisjointSetImpl { impl DisjointSetImpl for Ptr { fn makeSet(&self) { let i: Value = Rc::new(RefCell::new(0)); - 'loop_: while ((*i.borrow()) < (*(*(*self).upgrade().deref()).n.borrow())) { + 'loop_: while ((*i.borrow()) < (*self).with(|__s: &DisjointSet| __s.n)) { let __rhs = (*i.borrow()); - (*(*(*self).upgrade().deref()).parent.borrow()) - .as_ref() - .unwrap() - .borrow_mut()[((*i.borrow()) as usize) as usize] = __rhs; - (*(*(*self).upgrade().deref()).rank.borrow()) - .as_ref() - .unwrap() - .borrow_mut()[((*i.borrow()) as usize) as usize] = 1; + (*self).with_mut(|__s: &mut DisjointSet| { + __s.parent.as_ref().unwrap().borrow_mut()[((*i.borrow()) as usize) as usize] = __rhs + }); + (*self).with_mut(|__s: &mut DisjointSet| { + __s.rank.as_ref().unwrap().borrow_mut()[((*i.borrow()) as usize) as usize] = 1 + }); (*i.borrow_mut()).postfix_inc(); } } fn find(&self, x: i32) -> i32 { let x: Value = Rc::new(RefCell::new(x)); - if ((*(*(*self).upgrade().deref()).parent.borrow()) - .as_ref() - .unwrap() - .borrow()[((*x.borrow()) as usize) as usize] - != (*x.borrow())) + if ((*self).with(|__s: &DisjointSet| { + __s.parent.as_ref().unwrap().borrow()[((*x.borrow()) as usize) as usize] + }) != (*x.borrow())) { let __rhs = ({ - let _x: i32 = (*(*(*self).upgrade().deref()).parent.borrow()) - .as_ref() - .unwrap() - .borrow()[((*x.borrow()) as usize) as usize]; + let _x: i32 = (*self).with(|__s: &DisjointSet| { + __s.parent.as_ref().unwrap().borrow()[((*x.borrow()) as usize) as usize] + }); DisjointSetImpl::find(self, _x) }); - (*(*(*self).upgrade().deref()).parent.borrow()) - .as_ref() - .unwrap() - .borrow_mut()[((*x.borrow()) as usize) as usize] = __rhs; + (*self).with_mut(|__s: &mut DisjointSet| { + __s.parent.as_ref().unwrap().borrow_mut()[((*x.borrow()) as usize) as usize] = __rhs + }); } - return (*(*(*self).upgrade().deref()).parent.borrow()) - .as_ref() - .unwrap() - .borrow()[((*x.borrow()) as usize) as usize]; + return (*self).with(|__s: &DisjointSet| { + __s.parent.as_ref().unwrap().borrow()[((*x.borrow()) as usize) as usize] + }); } fn merge(&self, x: i32, y: i32) { let x: Value = Rc::new(RefCell::new(x)); @@ -436,55 +466,47 @@ impl DisjointSetImpl for Ptr { if ((*xset.borrow()) == (*yset.borrow())) { return; } - if ((*(*(*self).upgrade().deref()).rank.borrow()) - .as_ref() - .unwrap() - .borrow()[((*xset.borrow()) as usize) as usize] - < (*(*(*self).upgrade().deref()).rank.borrow()) - .as_ref() - .unwrap() - .borrow()[((*yset.borrow()) as usize) as usize]) - { - (*(*(*self).upgrade().deref()).parent.borrow()) - .as_ref() - .unwrap() - .borrow_mut()[((*xset.borrow()) as usize) as usize] = (*yset.borrow()); - } else if ((*(*(*self).upgrade().deref()).rank.borrow()) - .as_ref() - .unwrap() - .borrow()[((*xset.borrow()) as usize) as usize] - > (*(*(*self).upgrade().deref()).rank.borrow()) - .as_ref() - .unwrap() - .borrow()[((*yset.borrow()) as usize) as usize]) - { - (*(*(*self).upgrade().deref()).parent.borrow()) - .as_ref() - .unwrap() - .borrow_mut()[((*yset.borrow()) as usize) as usize] = (*xset.borrow()); + if ((*self).with(|__s: &DisjointSet| { + __s.rank.as_ref().unwrap().borrow()[((*xset.borrow()) as usize) as usize] + }) < (*self).with(|__s: &DisjointSet| { + __s.rank.as_ref().unwrap().borrow()[((*yset.borrow()) as usize) as usize] + })) { + (*self).with_mut(|__s: &mut DisjointSet| { + __s.parent.as_ref().unwrap().borrow_mut()[((*xset.borrow()) as usize) as usize] = + (*yset.borrow()) + }); + } else if ((*self).with(|__s: &DisjointSet| { + __s.rank.as_ref().unwrap().borrow()[((*xset.borrow()) as usize) as usize] + }) > (*self).with(|__s: &DisjointSet| { + __s.rank.as_ref().unwrap().borrow()[((*yset.borrow()) as usize) as usize] + })) { + (*self).with_mut(|__s: &mut DisjointSet| { + __s.parent.as_ref().unwrap().borrow_mut()[((*yset.borrow()) as usize) as usize] = + (*xset.borrow()) + }); } else { - (*(*(*self).upgrade().deref()).parent.borrow()) - .as_ref() - .unwrap() - .borrow_mut()[((*yset.borrow()) as usize) as usize] = (*xset.borrow()); - let __rhs = ((*(*(*self).upgrade().deref()).rank.borrow()) - .as_ref() - .unwrap() - .borrow()[((*xset.borrow()) as usize) as usize] - + 1); - (*(*(*self).upgrade().deref()).rank.borrow()) - .as_ref() - .unwrap() - .borrow_mut()[((*xset.borrow()) as usize) as usize] = __rhs; + (*self).with_mut(|__s: &mut DisjointSet| { + __s.parent.as_ref().unwrap().borrow_mut()[((*yset.borrow()) as usize) as usize] = + (*xset.borrow()) + }); + let __rhs = ((*self).with(|__s: &DisjointSet| { + __s.rank.as_ref().unwrap().borrow()[((*xset.borrow()) as usize) as usize] + }) + 1); + (*self).with_mut(|__s: &mut DisjointSet| { + __s.rank.as_ref().unwrap().borrow_mut()[((*xset.borrow()) as usize) as usize] = + __rhs + }); } } fn move_assign(&self, _a0: Ptr) -> Ptr { - ((*(*self).upgrade().deref()).rank.as_pointer() as Ptr>>>) - .write((*(*_a0.upgrade().deref()).rank.borrow_mut()).take()); - ((*(*self).upgrade().deref()).parent.as_pointer() as Ptr>>>) - .write((*(*_a0.upgrade().deref()).parent.borrow_mut()).take()); - let __rhs = (*(*_a0.upgrade().deref()).n.borrow()); - (*(*(*self).upgrade().deref()).n.borrow_mut()) = __rhs; + ((field_ptr!((*self), rank) as Ptr>>>) + as Ptr>>>) + .write(_a0.with_mut(|__s: &mut DisjointSet| __s.rank.take())); + ((field_ptr!((*self), parent) as Ptr>>>) + as Ptr>>>) + .write(_a0.with_mut(|__s: &mut DisjointSet| __s.parent.take())); + let __rhs = _a0.with(|__s: &DisjointSet| __s.n); + (*self).with_mut(|__s: &mut DisjointSet| __s.n = __rhs); return (*self).clone(); } } @@ -493,12 +515,13 @@ pub trait GraphImpl { } impl GraphImpl for Ptr { fn move_assign(&self, _a0: Ptr) -> Ptr { - ((*(*self).upgrade().deref()).edges.as_pointer() as Ptr>>>) - .write((*(*_a0.upgrade().deref()).edges.borrow_mut()).take()); - let __rhs = (*(*_a0.upgrade().deref()).V.borrow()); - (*(*(*self).upgrade().deref()).V.borrow_mut()) = __rhs; - let __rhs = (*(*_a0.upgrade().deref()).E.borrow()); - (*(*(*self).upgrade().deref()).E.borrow_mut()) = __rhs; + ((field_ptr!((*self), edges) as Ptr>>>) + as Ptr>>>) + .write(_a0.with_mut(|__s: &mut Graph| __s.edges.take())); + let __rhs = _a0.with(|__s: &Graph| __s.V); + (*self).with_mut(|__s: &mut Graph| __s.V = __rhs); + let __rhs = _a0.with(|__s: &Graph| __s.E); + (*self).with_mut(|__s: &mut Graph| __s.E = __rhs); return (*self).clone(); } } diff --git a/tests/unit/out/refcount/libc_struct_without_default.rs b/tests/unit/out/refcount/libc_struct_without_default.rs index 402758c24b..86f43b2b9e 100644 --- a/tests/unit/out/refcount/libc_struct_without_default.rs +++ b/tests/unit/out/refcount/libc_struct_without_default.rs @@ -6,16 +6,18 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg)] +#[derive(Record, VaArg, FnPtrArg)] pub struct UserDefined { + #[offset(0)] pub a: Value>, + #[offset(8)] pub v: Value>, } impl Clone for UserDefined { fn clone(&self) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - a: Rc::new(RefCell::new((*self.a.borrow()).clone())), - v: Rc::new(RefCell::new((*self.v.borrow()).clone())), + a: { Rc::new(RefCell::new((*self.a.borrow()).clone())) }, + v: { Rc::new(RefCell::new((*self.v.borrow()).clone())) }, })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() @@ -31,24 +33,35 @@ impl Default for UserDefined { } } } +<<<<<<< HEAD impl ByteRepr for UserDefined {} #[derive(VaArg, FnPtrArg)] -pub struct FieldIsLibcType { - pub addr: Value, -} -impl Clone for FieldIsLibcType { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - addr: Rc::new(RefCell::new((*self.addr.borrow()).clone())), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() +======= +impl ByteRepr for UserDefined { + fn byte_size() -> usize { + 32 } + fn to_bytes(&self, buf: &mut [u8]) { + self.a.to_bytes(&mut buf[0..4]); + self.v.to_bytes(&mut buf[8..32]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + a: >>::from_bytes(&buf[0..4]), + v: >>::from_bytes(&buf[8..32]), + } + } +} +#[derive(Clone, Record, VaArg, FnPtrArg)] +>>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) +pub struct FieldIsLibcType { + #[offset(0)] + pub addr: libcc2rs::Sockaddr, } impl Default for FieldIsLibcType { fn default() -> Self { FieldIsLibcType { - addr: Rc::new(RefCell::new(Default::default())), + addr: Default::default(), } } } @@ -57,11 +70,11 @@ impl ByteRepr for FieldIsLibcType { 16 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.addr.borrow()).to_bytes(&mut buf[0..16]); + self.addr.to_bytes(&mut buf[0..16]); } fn from_bytes(buf: &[u8]) -> Self { Self { - addr: Rc::new(RefCell::new(::from_bytes(&buf[0..16]))), + addr: ::from_bytes(&buf[0..16]), } } } @@ -91,15 +104,10 @@ fn main_0() -> i32 { (*(*st.borrow()).st_size.borrow_mut()) = 1024_i64; assert!(((*(*st.borrow()).st_size.borrow()) == 1024_i64)); let ud: Value = Rc::new(RefCell::new(::default())); - assert!( - ((((*ud.borrow()).a.as_pointer() as Ptr) - .offset(0_usize) - .read()) - == 0) - ); + assert!(({ (*(*ud.borrow()).a.borrow())[(0_usize) as usize] } == 0)); assert!(((*(*ud.borrow()).v.borrow()).len() == 0_usize)); let filt: Value = Rc::new(RefCell::new(::default())); - assert!((((*(*(*filt.borrow()).addr.borrow()).sa_family.borrow()) as i32) == 0)); + assert!((((*(*filt.borrow()).addr.sa_family.borrow()) as i32) == 0)); return 0; } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/linked_list.rs b/tests/unit/out/refcount/linked_list.rs index 4aeb98a6f9..4924ff1a9e 100644 --- a/tests/unit/out/refcount/linked_list.rs +++ b/tests/unit/out/refcount/linked_list.rs @@ -6,33 +6,25 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Node { - pub val: Value, - pub next: Value>, -} -impl Clone for Node { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - val: Rc::new(RefCell::new((*self.val.borrow()))), - next: Rc::new(RefCell::new((*self.next.borrow()).clone())), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub val: i32, + #[offset(8)] + pub next: Ptr, } impl ByteRepr for Node { fn byte_size() -> usize { 16 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.val.borrow()).to_bytes(&mut buf[0..4]); - (*self.next.borrow()).to_bytes(&mut buf[8..16]); + self.val.to_bytes(&mut buf[0..4]); + self.next.to_bytes(&mut buf[8..16]); } fn from_bytes(buf: &[u8]) -> Self { Self { - val: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - next: Rc::new(RefCell::new(>::from_bytes(&buf[8..16]))), + val: ::from_bytes(&buf[0..4]), + next: >::from_bytes(&buf[8..16]), } } } @@ -42,7 +34,7 @@ pub fn Find_0(head: Ptr, idx: i32) -> Ptr { let curr: Value> = Rc::new(RefCell::new((*head.borrow()).clone())); let i: Value = Rc::new(RefCell::new(0)); 'loop_: while ((*i.borrow()) < (*idx.borrow())) { - let __rhs = (*(*(*curr.borrow()).upgrade().deref()).next.borrow()).clone(); + let __rhs = (*curr.borrow()).with(|__s: &Node| (__s.next).clone()); (*curr.borrow_mut()) = __rhs; (*i.borrow_mut()).postfix_inc(); } @@ -50,8 +42,8 @@ pub fn Find_0(head: Ptr, idx: i32) -> Ptr { } pub fn Append_1(head: Ptr, new_node: Ptr) { let curr: Value> = Rc::new(RefCell::new((head).clone())); - 'loop_: while !((*(*(*curr.borrow()).upgrade().deref()).next.borrow()).is_null()) { - let __rhs = (*(*(*curr.borrow()).upgrade().deref()).next.borrow()).clone(); + 'loop_: while !(((*curr.borrow()).with(|__s: &Node| (__s.next).clone())).is_null()) { + let __rhs = (*curr.borrow()).with(|__s: &Node| (__s.next).clone()); (*curr.borrow_mut()) = __rhs; } ({ NodeImpl::SetNext(&(*curr.borrow()), (new_node).clone()) }); @@ -63,19 +55,19 @@ pub fn Delete_2(head: Ptr, val: i32) -> Ptr { let prev: Value> = Rc::new(RefCell::new(Ptr::::null())); 'loop_: while !((*curr.borrow()).is_null()) { if { - let _lhs = (*(*(*curr.borrow()).upgrade().deref()).val.borrow()); + let _lhs = (*curr.borrow()).with(|__s: &Node| __s.val); _lhs == (*val.borrow()) } { if !((*prev.borrow()).is_null()) { - let __rhs = (*(*(*curr.borrow()).upgrade().deref()).next.borrow()).clone(); - (*(*(*prev.borrow()).upgrade().deref()).next.borrow_mut()) = __rhs; + let __rhs = (*curr.borrow()).with(|__s: &Node| (__s.next).clone()); + (*prev.borrow()).with_mut(|__s: &mut Node| __s.next = __rhs); return (*head.borrow()).clone(); } else { - return (*(*(*curr.borrow()).upgrade().deref()).next.borrow()).clone(); + return (*curr.borrow()).with(|__s: &Node| (__s.next).clone()); } } (*prev.borrow_mut()) = (*curr.borrow()).clone(); - let __rhs = (*(*(*curr.borrow()).upgrade().deref()).next.borrow()).clone(); + let __rhs = (*curr.borrow()).with(|__s: &Node| (__s.next).clone()); (*curr.borrow_mut()) = __rhs; } return (*head.borrow()).clone(); @@ -86,37 +78,37 @@ pub fn main() { } fn main_0() -> i32 { let n0: Value = Rc::new(RefCell::new(Node { - val: Rc::new(RefCell::new(5)), - next: Rc::new(RefCell::new(Ptr::::null())), + val: 5, + next: Ptr::::null(), })); let head: Value> = Rc::new(RefCell::new((n0.as_pointer()))); let n1: Value = Rc::new(RefCell::new(Node { - val: Rc::new(RefCell::new(4)), - next: Rc::new(RefCell::new(Ptr::::null())), + val: 4, + next: Ptr::::null(), })); let n2: Value = Rc::new(RefCell::new(Node { - val: Rc::new(RefCell::new(3)), - next: Rc::new(RefCell::new(Ptr::::null())), + val: 3, + next: Ptr::::null(), })); let n3: Value = Rc::new(RefCell::new(Node { - val: Rc::new(RefCell::new(2)), - next: Rc::new(RefCell::new(Ptr::::null())), + val: 2, + next: Ptr::::null(), })); let n4: Value = Rc::new(RefCell::new(Node { - val: Rc::new(RefCell::new(1)), - next: Rc::new(RefCell::new(Ptr::::null())), + val: 1, + next: Ptr::::null(), })); let n5: Value = Rc::new(RefCell::new(Node { - val: Rc::new(RefCell::new(0)), - next: Rc::new(RefCell::new(Ptr::::null())), + val: 0, + next: Ptr::::null(), })); let n6: Value = Rc::new(RefCell::new(Node { - val: Rc::new(RefCell::new(-1_i32)), - next: Rc::new(RefCell::new(Ptr::::null())), + val: -1_i32, + next: Ptr::::null(), })); let n7: Value = Rc::new(RefCell::new(Node { - val: Rc::new(RefCell::new(-2_i32)), - next: Rc::new(RefCell::new(Ptr::::null())), + val: -2_i32, + next: Ptr::::null(), })); ({ let _head: Ptr = (*head.borrow()).clone(); @@ -160,26 +152,11 @@ fn main_0() -> i32 { let __rhs = ({ Delete_2((*head.borrow()).clone(), -2_i32) }); (*head.borrow_mut()) = __rhs; assert!( - ((((((*(*({ Find_0((*head.borrow()).clone(), 0,) }).upgrade().deref()) - .val - .borrow()) - == 4) - && ((*(*({ Find_0((*head.borrow()).clone(), 1,) }).upgrade().deref()) - .val - .borrow()) - == 3)) - && ((*(*({ Find_0((*head.borrow()).clone(), 2,) }).upgrade().deref()) - .val - .borrow()) - == 2)) - && ((*(*({ Find_0((*head.borrow()).clone(), 3,) }).upgrade().deref()) - .val - .borrow()) - == 1)) - && ((*(*({ Find_0((*head.borrow()).clone(), 4,) }).upgrade().deref()) - .val - .borrow()) - == -1_i32)) + (((((({ Find_0((*head.borrow()).clone(), 0,) }).with(|__s: &Node| __s.val) == 4) + && (({ Find_0((*head.borrow()).clone(), 1,) }).with(|__s: &Node| __s.val) == 3)) + && (({ Find_0((*head.borrow()).clone(), 2,) }).with(|__s: &Node| __s.val) == 2)) + && (({ Find_0((*head.borrow()).clone(), 3,) }).with(|__s: &Node| __s.val) == 1)) + && (({ Find_0((*head.borrow()).clone(), 4,) }).with(|__s: &Node| __s.val) == -1_i32)) && (({ Find_0((*head.borrow()).clone(), 5,) }).is_null()) ); return 0; @@ -190,7 +167,7 @@ pub trait NodeImpl { impl NodeImpl for Ptr { fn SetNext(&self, next: Ptr) { let next: Value> = Rc::new(RefCell::new(next)); - (*(*(*self).upgrade().deref()).next.borrow_mut()) = (*next.borrow()).clone(); + (*self).with_mut(|__s: &mut Node| __s.next = (*next.borrow()).clone()); } } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/local_anon_struct_collision.rs b/tests/unit/out/refcount/local_anon_struct_collision.rs index 5bc55b6dae..7d34039ec1 100644 --- a/tests/unit/out/refcount/local_anon_struct_collision.rs +++ b/tests/unit/out/refcount/local_anon_struct_collision.rs @@ -8,69 +8,57 @@ use std::os::fd::AsFd; use std::rc::{Rc, Weak}; pub fn first_0() -> i32 { let p: Value = >::default(); - (*(*p.borrow()).x.borrow_mut()) = 1; - (*(*p.borrow()).y.borrow_mut()) = 2; - return ((*(*p.borrow()).x.borrow()) + (*(*p.borrow()).y.borrow())); + (*p.borrow_mut()).x = 1; + (*p.borrow_mut()).y = 2; + return ({ (*p.borrow()).x } + { (*p.borrow()).y }); } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct anon_1 { - pub x: Value, - pub y: Value, -} -impl Clone for anon_1 { - fn clone(&self) -> Self { - Self { - x: Rc::new(RefCell::new((*self.x.borrow()).clone())), - y: Rc::new(RefCell::new((*self.y.borrow()).clone())), - } - } + #[offset(0)] + pub x: i32, + #[offset(4)] + pub y: i32, } impl ByteRepr for anon_1 { fn byte_size() -> usize { 8 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.x.borrow()).to_bytes(&mut buf[0..4]); - (*self.y.borrow()).to_bytes(&mut buf[4..8]); + self.x.to_bytes(&mut buf[0..4]); + self.y.to_bytes(&mut buf[4..8]); } fn from_bytes(buf: &[u8]) -> Self { Self { - x: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - y: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + x: ::from_bytes(&buf[0..4]), + y: ::from_bytes(&buf[4..8]), } } } pub fn second_2() -> i32 { let q: Value = >::default(); - (*(*q.borrow()).a.borrow_mut()) = 10_i64; - (*(*q.borrow()).b.borrow_mut()) = 20_i64; - return (((*(*q.borrow()).a.borrow()) + (*(*q.borrow()).b.borrow())) as i32); + (*q.borrow_mut()).a = 10_i64; + (*q.borrow_mut()).b = 20_i64; + return (({ (*q.borrow()).a } + { (*q.borrow()).b }) as i32); } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct anon_3 { - pub a: Value, - pub b: Value, -} -impl Clone for anon_3 { - fn clone(&self) -> Self { - Self { - a: Rc::new(RefCell::new((*self.a.borrow()).clone())), - b: Rc::new(RefCell::new((*self.b.borrow()).clone())), - } - } + #[offset(0)] + pub a: i64, + #[offset(8)] + pub b: i64, } impl ByteRepr for anon_3 { fn byte_size() -> usize { 16 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.a.borrow()).to_bytes(&mut buf[0..8]); - (*self.b.borrow()).to_bytes(&mut buf[8..16]); + self.a.to_bytes(&mut buf[0..8]); + self.b.to_bytes(&mut buf[8..16]); } fn from_bytes(buf: &[u8]) -> Self { Self { - a: Rc::new(RefCell::new(::from_bytes(&buf[0..8]))), - b: Rc::new(RefCell::new(::from_bytes(&buf[8..16]))), + a: ::from_bytes(&buf[0..8]), + b: ::from_bytes(&buf[8..16]), } } } diff --git a/tests/unit/out/refcount/local_record_template.rs b/tests/unit/out/refcount/local_record_template.rs index c8ce490fbb..22b65d501b 100644 --- a/tests/unit/out/refcount/local_record_template.rs +++ b/tests/unit/out/refcount/local_record_template.rs @@ -8,124 +8,93 @@ use std::os::fd::AsFd; use std::rc::{Rc, Weak}; pub fn get_0(t: Local_1) -> i32 { let t: Value = Rc::new(RefCell::new(t)); - return ((*(*t.borrow()).x.borrow()) as i32); + return ({ (*t.borrow()).x } as i32); } pub fn get_2(t: Local_3) -> i32 { let t: Value = Rc::new(RefCell::new(t)); - return (*(*t.borrow()).x.borrow()); + return { (*t.borrow()).x }; } pub fn get_4(t: Local_5) -> i32 { let t: Value = Rc::new(RefCell::new(t)); - return (*(*t.borrow()).x.borrow()); + return { (*t.borrow()).x }; } pub fn get_6(t: Local_7) -> i32 { let t: Value = Rc::new(RefCell::new(t)); - return ((*(*t.borrow()).x.borrow()) as i32); + return ({ (*t.borrow()).x } as i32); } pub fn twice_8(t: Local_3) -> i32 { let t: Value = Rc::new(RefCell::new(t)); - return ((*(*t.borrow()).x.borrow()) * 2); + return ({ (*t.borrow()).x } * 2); } pub fn wrap_9(v: i32) -> i32 { let v: Value = Rc::new(RefCell::new(v)); - let l: Value = Rc::new(RefCell::new(Local_5 { - x: Rc::new(RefCell::new((*v.borrow()))), - })); + let l: Value = Rc::new(RefCell::new(Local_5 { x: (*v.borrow()) })); return ({ get_4((*l.borrow()).clone()) }); } pub fn wrap_10(v: i64) -> i32 { let v: Value = Rc::new(RefCell::new(v)); - let l: Value = Rc::new(RefCell::new(Local_7 { - x: Rc::new(RefCell::new((*v.borrow()))), - })); + let l: Value = Rc::new(RefCell::new(Local_7 { x: (*v.borrow()) })); return ({ get_6((*l.borrow()).clone()) }); } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Local_5 { - pub x: Value, -} -impl Clone for Local_5 { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - x: Rc::new(RefCell::new((*self.x.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub x: i32, } impl ByteRepr for Local_5 { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.x.borrow()).to_bytes(&mut buf[0..4]); + self.x.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - x: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + x: ::from_bytes(&buf[0..4]), } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Local_7 { - pub x: Value, -} -impl Clone for Local_7 { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - x: Rc::new(RefCell::new((*self.x.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub x: i64, } impl ByteRepr for Local_7 { fn byte_size() -> usize { 8 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.x.borrow()).to_bytes(&mut buf[0..8]); + self.x.to_bytes(&mut buf[0..8]); } fn from_bytes(buf: &[u8]) -> Self { Self { - x: Rc::new(RefCell::new(::from_bytes(&buf[0..8]))), + x: ::from_bytes(&buf[0..8]), } } } pub fn other_11() -> i32 { - let l: Value = Rc::new(RefCell::new(Local_1 { - x: Rc::new(RefCell::new(3_i64)), - y: Rc::new(RefCell::new(4_i64)), - })); - return (({ get_0((*l.borrow()).clone()) }) + ((*(*l.borrow()).y.borrow()) as i32)); + let l: Value = Rc::new(RefCell::new(Local_1 { x: 3_i64, y: 4_i64 })); + return (({ get_0((*l.borrow()).clone()) }) + ({ (*l.borrow()).y } as i32)); } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Local_1 { - pub x: Value, - pub y: Value, -} -impl Clone for Local_1 { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - x: Rc::new(RefCell::new((*self.x.borrow()))), - y: Rc::new(RefCell::new((*self.y.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub x: i64, + #[offset(8)] + pub y: i64, } impl ByteRepr for Local_1 { fn byte_size() -> usize { 16 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.x.borrow()).to_bytes(&mut buf[0..8]); - (*self.y.borrow()).to_bytes(&mut buf[8..16]); + self.x.to_bytes(&mut buf[0..8]); + self.y.to_bytes(&mut buf[8..16]); } fn from_bytes(buf: &[u8]) -> Self { Self { - x: Rc::new(RefCell::new(::from_bytes(&buf[0..8]))), - y: Rc::new(RefCell::new(::from_bytes(&buf[8..16]))), + x: ::from_bytes(&buf[0..8]), + y: ::from_bytes(&buf[8..16]), } } } @@ -134,9 +103,7 @@ pub fn main() { std::process::exit(main_0()); } fn main_0() -> i32 { - let l: Value = Rc::new(RefCell::new(Local_3 { - x: Rc::new(RefCell::new(7)), - })); + let l: Value = Rc::new(RefCell::new(Local_3 { x: 7 })); assert!((({ get_2((*l.borrow()).clone(),) }) == 7)); assert!((({ twice_8((*l.borrow()).clone(),) }) == 14)); assert!((({ other_11() }) == 7)); @@ -144,29 +111,21 @@ fn main_0() -> i32 { assert!((({ wrap_10(6_i64,) }) == 6)); return 0; } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Local_3 { - pub x: Value, -} -impl Clone for Local_3 { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - x: Rc::new(RefCell::new((*self.x.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub x: i32, } impl ByteRepr for Local_3 { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.x.borrow()).to_bytes(&mut buf[0..4]); + self.x.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - x: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + x: ::from_bytes(&buf[0..4]), } } } diff --git a/tests/unit/out/refcount/map.rs b/tests/unit/out/refcount/map.rs index d9424f5739..26e5b068a5 100644 --- a/tests/unit/out/refcount/map.rs +++ b/tests/unit/out/refcount/map.rs @@ -365,15 +365,14 @@ fn main_0() -> i32 { let i: Value = Rc::new(RefCell::new(0_u32)); 'loop_: while (((*i.borrow()) as usize) < (*indexes.borrow()).len()) { let __rhs = ((*i.borrow()).wrapping_rem(2_u32) != 0); + let __idx = ((indexes.as_pointer() as Ptr) + .offset(((*i.borrow()) as usize)) + .read()); (m2.as_pointer() as Ptr>>) .with_mut(|__v: &mut BTreeMap>| { - __v.entry( - ((indexes.as_pointer() as Ptr) - .offset(((*i.borrow()) as usize)) - .read()), - ) - .or_insert_with(|| Rc::new(RefCell::new(::default()))) - .as_pointer() + __v.entry(__idx) + .or_insert_with(|| Rc::new(RefCell::new(::default()))) + .as_pointer() }) .write(__rhs); (*i.borrow_mut()).prefix_inc(); diff --git a/tests/unit/out/refcount/memcpy_struct_bytes.rs b/tests/unit/out/refcount/memcpy_struct_bytes.rs index 85cb492fce..dcb3ab2372 100644 --- a/tests/unit/out/refcount/memcpy_struct_bytes.rs +++ b/tests/unit/out/refcount/memcpy_struct_bytes.rs @@ -6,31 +6,25 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct point { - pub x: Value, - pub y: Value, -} -impl Clone for point { - fn clone(&self) -> Self { - Self { - x: Rc::new(RefCell::new((*self.x.borrow()).clone())), - y: Rc::new(RefCell::new((*self.y.borrow()).clone())), - } - } + #[offset(0)] + pub x: i32, + #[offset(4)] + pub y: i32, } impl ByteRepr for point { fn byte_size() -> usize { 8 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.x.borrow()).to_bytes(&mut buf[0..4]); - (*self.y.borrow()).to_bytes(&mut buf[4..8]); + self.x.to_bytes(&mut buf[0..4]); + self.y.to_bytes(&mut buf[4..8]); } fn from_bytes(buf: &[u8]) -> Self { Self { - x: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - y: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + x: ::from_bytes(&buf[0..4]), + y: ::from_bytes(&buf[4..8]), } } } @@ -39,10 +33,7 @@ pub fn main() { std::process::exit(main_0()); } fn main_0() -> i32 { - let src: Value = Rc::new(RefCell::new(point { - x: Rc::new(RefCell::new(3)), - y: Rc::new(RefCell::new(7)), - })); + let src: Value = Rc::new(RefCell::new(point { x: 3, y: 7 })); let buf: Value> = Rc::new(RefCell::new((0..8).map(|_| 0_u8).collect::>())); { ((buf.as_pointer() as Ptr) as Ptr).to_any().memcpy( @@ -59,8 +50,8 @@ fn main_0() -> i32 { ); ((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)); + assert!(((({ (*dst.borrow()).x } == 3) as i32) != 0)); + assert!(((({ (*dst.borrow()).y } == 7) as i32) != 0)); return 0; } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/memcpy_struct_struct.rs b/tests/unit/out/refcount/memcpy_struct_struct.rs index d557c9519f..5eda2b901e 100644 --- a/tests/unit/out/refcount/memcpy_struct_struct.rs +++ b/tests/unit/out/refcount/memcpy_struct_struct.rs @@ -6,33 +6,25 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Entry { - pub bits: Value, - pub value: Value, -} -impl Clone for Entry { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - bits: Rc::new(RefCell::new((*self.bits.borrow()))), - value: Rc::new(RefCell::new((*self.value.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub bits: u8, + #[offset(2)] + pub value: u16, } impl ByteRepr for Entry { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.bits.borrow()).to_bytes(&mut buf[0..1]); - (*self.value.borrow()).to_bytes(&mut buf[2..4]); + self.bits.to_bytes(&mut buf[0..1]); + self.value.to_bytes(&mut buf[2..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - bits: Rc::new(RefCell::new(::from_bytes(&buf[0..1]))), - value: Rc::new(RefCell::new(::from_bytes(&buf[2..4]))), + bits: ::from_bytes(&buf[0..1]), + value: ::from_bytes(&buf[2..4]), } } } @@ -43,36 +35,36 @@ pub fn main() { fn main_0() -> i32 { let table: Value> = Rc::new(RefCell::new(Box::new([ Entry { - bits: Rc::new(RefCell::new(1_u8)), - value: Rc::new(RefCell::new(4369_u16)), + bits: 1_u8, + value: 4369_u16, }, Entry { - bits: Rc::new(RefCell::new(2_u8)), - value: Rc::new(RefCell::new(8738_u16)), + bits: 2_u8, + value: 8738_u16, }, Entry { - bits: Rc::new(RefCell::new(3_u8)), - value: Rc::new(RefCell::new(13107_u16)), + bits: 3_u8, + value: 13107_u16, }, Entry { - bits: Rc::new(RefCell::new(4_u8)), - value: Rc::new(RefCell::new(17476_u16)), + bits: 4_u8, + value: 17476_u16, }, Entry { - bits: Rc::new(RefCell::new(0_u8)), - value: Rc::new(RefCell::new(0_u16)), + bits: 0_u8, + value: 0_u16, }, Entry { - bits: Rc::new(RefCell::new(0_u8)), - value: Rc::new(RefCell::new(0_u16)), + bits: 0_u8, + value: 0_u16, }, Entry { - bits: Rc::new(RefCell::new(0_u8)), - value: Rc::new(RefCell::new(0_u16)), + bits: 0_u8, + value: 0_u16, }, Entry { - bits: Rc::new(RefCell::new(0_u8)), - value: Rc::new(RefCell::new(0_u16)), + bits: 0_u8, + value: 0_u16, }, ]))); let table_size: Value = Rc::new(RefCell::new(4_usize)); @@ -86,20 +78,20 @@ fn main_0() -> i32 { (((table.as_pointer() as Ptr).offset((*table_size.borrow()))) as Ptr).to_any() }; assert!( - (((*(*table.borrow())[(4) as usize].bits.borrow()) as i32) == 1) - && (((*(*table.borrow())[(4) as usize].value.borrow()) as i32) == 4369) + (({ (*table.borrow())[(4) as usize].bits } as i32) == 1) + && (({ (*table.borrow())[(4) as usize].value } as i32) == 4369) ); assert!( - (((*(*table.borrow())[(5) as usize].bits.borrow()) as i32) == 2) - && (((*(*table.borrow())[(5) as usize].value.borrow()) as i32) == 8738) + (({ (*table.borrow())[(5) as usize].bits } as i32) == 2) + && (({ (*table.borrow())[(5) as usize].value } as i32) == 8738) ); assert!( - (((*(*table.borrow())[(6) as usize].bits.borrow()) as i32) == 3) - && (((*(*table.borrow())[(6) as usize].value.borrow()) as i32) == 13107) + (({ (*table.borrow())[(6) as usize].bits } as i32) == 3) + && (({ (*table.borrow())[(6) as usize].value } as i32) == 13107) ); assert!( - (((*(*table.borrow())[(7) as usize].bits.borrow()) as i32) == 4) - && (((*(*table.borrow())[(7) as usize].value.borrow()) as i32) == 17476) + (({ (*table.borrow())[(7) as usize].bits } as i32) == 4) + && (({ (*table.borrow())[(7) as usize].value } as i32) == 17476) ); return 0; } diff --git a/tests/unit/out/refcount/move_assign.rs b/tests/unit/out/refcount/move_assign.rs index aec0b65c24..5d30212622 100644 --- a/tests/unit/out/refcount/move_assign.rs +++ b/tests/unit/out/refcount/move_assign.rs @@ -6,38 +6,53 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Default)] +#[derive(Record, Default)] pub struct MoveOnly { - pub v: Value, + #[offset(0)] + pub v: i32, } impl MoveOnly { pub fn new(v: i32) -> Self { let v: Value = Rc::new(RefCell::new(v)); - let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*v.borrow()))), - })); + let __this: Value = Rc::new(RefCell::new(Self { v: (*v.borrow()) })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } pub fn move_from(o: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*(*o.upgrade().deref()).v.borrow()))), + v: { o.with(|__s: &MoveOnly| __s.v) }, })); let this: Ptr = __this.as_pointer(); - (*(*o.upgrade().deref()).v.borrow_mut()) = 0; + o.with_mut(|__s: &mut MoveOnly| __s.v = 0); Rc::try_unwrap(__this).ok().unwrap().into_inner() } } +<<<<<<< HEAD impl ByteRepr for MoveOnly {} #[derive()] +======= +impl ByteRepr for MoveOnly { + fn byte_size() -> usize { + 4 + } + fn to_bytes(&self, buf: &mut [u8]) { + self.v.to_bytes(&mut buf[0..4]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + v: ::from_bytes(&buf[0..4]), + } + } +} +#[derive(Record)] +>>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub struct ConstMoveAssign { - pub mark: Value, + #[offset(0)] + pub mark: i32, } impl ConstMoveAssign { pub fn new() -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - mark: Rc::new(RefCell::new(0)), - })); + let __this: Value = Rc::new(RefCell::new(Self { mark: 0 })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } @@ -47,7 +62,23 @@ impl Default for ConstMoveAssign { { ConstMoveAssign::new() } } } +<<<<<<< HEAD impl ByteRepr for ConstMoveAssign {} +======= +impl ByteRepr for ConstMoveAssign { + fn byte_size() -> usize { + 4 + } + fn to_bytes(&self, buf: &mut [u8]) { + self.mark.to_bytes(&mut buf[0..4]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + mark: ::from_bytes(&buf[0..4]), + } + } +} +>>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub fn make_0(v: i32) -> MoveOnly { let v: Value = Rc::new(RefCell::new(v)); let m: Value = Rc::new(RefCell::new(MoveOnly::new({ (*v.borrow()) }))); @@ -62,8 +93,8 @@ fn main_0() -> i32 { let b: Value = Rc::new(RefCell::new(MoveOnly::new({ 2 }))); let c: Value = Rc::new(RefCell::new(MoveOnly::new({ 3 }))); ({ MoveOnlyImpl::move_assign(&a.as_pointer(), b.as_pointer()) }); - assert!(((*(*a.borrow()).v.borrow()) == 2)); - assert!(((*(*b.borrow()).v.borrow()) == 0)); + assert!(({ (*a.borrow()).v } == 2)); + assert!(({ (*b.borrow()).v } == 0)); ({ let _o: Value = Rc::new(RefCell::new(MoveOnly::new({ 3 }))); MoveOnlyImpl::move_assign(&b.as_pointer(), _o.as_pointer()) @@ -75,24 +106,23 @@ fn main_0() -> i32 { ) }); assert!( - (((*(*b.borrow()).v.borrow()) == 0) && ((*(*a.borrow()).v.borrow()) == 0)) - && ((*(*c.borrow()).v.borrow()) == 3) + (({ (*b.borrow()).v } == 0) && ({ (*a.borrow()).v } == 0)) && ({ (*c.borrow()).v } == 3) ); ({ let _o: Value = Rc::new(RefCell::new(MoveOnly::new({ 5 }))); MoveOnlyImpl::move_assign(&a.as_pointer(), _o.as_pointer()) }); - assert!(((*(*a.borrow()).v.borrow()) == 5)); + assert!(({ (*a.borrow()).v } == 5)); ({ let _o: Value = Rc::new(RefCell::new(({ make_0(6) }))); MoveOnlyImpl::move_assign(&a.as_pointer(), _o.as_pointer()) }); - assert!(((*(*a.borrow()).v.borrow()) == 6)); + assert!(({ (*a.borrow()).v } == 6)); ({ let _o: Ptr = a.as_pointer(); MoveOnlyImpl::move_assign(&a.as_pointer(), _o) }); - assert!(((*(*a.borrow()).v.borrow()) == 6)); + assert!(({ (*a.borrow()).v } == 6)); let vec_: Value> = Rc::new(RefCell::new(Vec::new())); (*vec_.borrow_mut()).push(MoveOnly::new({ 7 })); let d: Value = Rc::new(RefCell::new(MoveOnly::new({ 8 }))); @@ -103,23 +133,20 @@ fn main_0() -> i32 { ) }); assert!( - ((*(*(vec_.as_pointer() as Ptr) + ((vec_.as_pointer() as Ptr) .offset(0_usize) - .upgrade() - .deref()) - .v - .borrow()) + .with(|__s: &MoveOnly| __s.v) == 8) ); - assert!(((*(*d.borrow()).v.borrow()) == 0)); + assert!(({ (*d.borrow()).v } == 0)); let m: Value = Rc::new(RefCell::new(ConstMoveAssign::new())); let m1: Value = Rc::new(RefCell::new(ConstMoveAssign::new())); let m2: Value = Rc::new(RefCell::new(ConstMoveAssign::new())); let cm: Value = Rc::new(RefCell::new(ConstMoveAssign::new())); ({ ConstMoveAssignImpl::operator_assign_2(&m1.as_pointer(), m.as_pointer()) }); ({ ConstMoveAssignImpl::operator_assign_3(&m2.as_pointer(), cm.as_pointer()) }); - assert!(((*(*m1.borrow()).mark.borrow()) == 1)); - assert!(((*(*m2.borrow()).mark.borrow()) == 10)); + assert!(({ (*m1.borrow()).mark } == 1)); + assert!(({ (*m2.borrow()).mark } == 10)); return 0; } pub trait ConstMoveAssignImpl { @@ -128,13 +155,13 @@ pub trait ConstMoveAssignImpl { } impl ConstMoveAssignImpl for Ptr { fn operator_assign_2(&self, o: Ptr) -> Ptr { - let __rhs = ((*(*o.upgrade().deref()).mark.borrow()) + 1); - (*(*(*self).upgrade().deref()).mark.borrow_mut()) = __rhs; + let __rhs = (o.with(|__s: &ConstMoveAssign| __s.mark) + 1); + (*self).with_mut(|__s: &mut ConstMoveAssign| __s.mark = __rhs); return (*self).clone(); } fn operator_assign_3(&self, o: Ptr) -> Ptr { - let __rhs = ((*(*o.upgrade().deref()).mark.borrow()) + 10); - (*(*(*self).upgrade().deref()).mark.borrow_mut()) = __rhs; + let __rhs = (o.with(|__s: &ConstMoveAssign| __s.mark) + 10); + (*self).with_mut(|__s: &mut ConstMoveAssign| __s.mark = __rhs); return (*self).clone(); } } @@ -146,9 +173,9 @@ impl MoveOnlyImpl for Ptr { if ((*self) == (o)) { return (*self).clone(); } - let __rhs = (*(*o.upgrade().deref()).v.borrow()); - (*(*(*self).upgrade().deref()).v.borrow_mut()) = __rhs; - (*(*o.upgrade().deref()).v.borrow_mut()) = 0; + let __rhs = o.with(|__s: &MoveOnly| __s.v); + (*self).with_mut(|__s: &mut MoveOnly| __s.v = __rhs); + o.with_mut(|__s: &mut MoveOnly| __s.v = 0); return (*self).clone(); } } diff --git a/tests/unit/out/refcount/move_ctor.rs b/tests/unit/out/refcount/move_ctor.rs index 4f54df5786..97aaa5f1eb 100644 --- a/tests/unit/out/refcount/move_ctor.rs +++ b/tests/unit/out/refcount/move_ctor.rs @@ -6,51 +6,66 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Default)] +#[derive(Record, Default)] pub struct MoveOnly { - pub v: Value, + #[offset(0)] + pub v: i32, } impl MoveOnly { pub fn new(v: i32) -> Self { let v: Value = Rc::new(RefCell::new(v)); - let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*v.borrow()))), - })); + let __this: Value = Rc::new(RefCell::new(Self { v: (*v.borrow()) })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } pub fn move_from(o: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*(*o.upgrade().deref()).v.borrow()))), + v: { o.with(|__s: &MoveOnly| __s.v) }, })); let this: Ptr = __this.as_pointer(); - (*(*o.upgrade().deref()).v.borrow_mut()) = 0; + o.with_mut(|__s: &mut MoveOnly| __s.v = 0); Rc::try_unwrap(__this).ok().unwrap().into_inner() } } +<<<<<<< HEAD impl ByteRepr for MoveOnly {} #[derive()] +======= +impl ByteRepr for MoveOnly { + fn byte_size() -> usize { + 4 + } + fn to_bytes(&self, buf: &mut [u8]) { + self.v.to_bytes(&mut buf[0..4]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + v: ::from_bytes(&buf[0..4]), + } + } +} +#[derive(Record)] +>>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub struct ConstMove { - pub mark: Value, + #[offset(0)] + pub mark: i32, } impl ConstMove { pub fn new() -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - mark: Rc::new(RefCell::new(0)), - })); + let __this: Value = Rc::new(RefCell::new(Self { mark: 0 })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } pub fn new_1(o: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - mark: Rc::new(RefCell::new(((*(*o.upgrade().deref()).mark.borrow()) + 1))), + mark: { (o.with(|__s: &ConstMove| __s.mark) + 1) }, })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } pub fn new_2(o: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - mark: Rc::new(RefCell::new(((*(*o.upgrade().deref()).mark.borrow()) + 10))), + mark: { (o.with(|__s: &ConstMove| __s.mark) + 10) }, })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() @@ -61,43 +76,61 @@ impl Default for ConstMove { { ConstMove::new() } } } +<<<<<<< HEAD impl ByteRepr for ConstMove {} #[derive(VaArg, FnPtrArg, Default)] +======= +impl ByteRepr for ConstMove { + fn byte_size() -> usize { + 4 + } + fn to_bytes(&self, buf: &mut [u8]) { + self.mark.to_bytes(&mut buf[0..4]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + mark: ::from_bytes(&buf[0..4]), + } + } +} +#[derive(Record, VaArg, FnPtrArg, Default)] +>>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub struct ThrowingMove { - pub v: Value, - pub copies: Value, - pub moves: Value, + #[offset(0)] + pub v: i32, + #[offset(4)] + pub copies: i32, + #[offset(8)] + pub moves: i32, } impl ThrowingMove { pub fn new(v: i32) -> Self { let v: Value = Rc::new(RefCell::new(v)); let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*v.borrow()))), - copies: Rc::new(RefCell::new(0)), - moves: Rc::new(RefCell::new(0)), + v: (*v.borrow()), + copies: 0, + moves: 0, })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } pub fn copy_from(o: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*(*o.upgrade().deref()).v.borrow()))), - copies: Rc::new(RefCell::new( - ((*(*o.upgrade().deref()).copies.borrow()) + 1), - )), - moves: Rc::new(RefCell::new((*(*o.upgrade().deref()).moves.borrow()))), + v: { o.with(|__s: &ThrowingMove| __s.v) }, + copies: { (o.with(|__s: &ThrowingMove| __s.copies) + 1) }, + moves: { o.with(|__s: &ThrowingMove| __s.moves) }, })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } pub fn move_from(o: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*(*o.upgrade().deref()).v.borrow()))), - copies: Rc::new(RefCell::new((*(*o.upgrade().deref()).copies.borrow()))), - moves: Rc::new(RefCell::new(((*(*o.upgrade().deref()).moves.borrow()) + 1))), + v: { o.with(|__s: &ThrowingMove| __s.v) }, + copies: { o.with(|__s: &ThrowingMove| __s.copies) }, + moves: { (o.with(|__s: &ThrowingMove| __s.moves) + 1) }, })); let this: Ptr = __this.as_pointer(); - (*(*o.upgrade().deref()).v.borrow_mut()) = 0; + o.with_mut(|__s: &mut ThrowingMove| __s.v = 0); Rc::try_unwrap(__this).ok().unwrap().into_inner() } } @@ -111,43 +144,65 @@ impl Clone for ThrowingMove { ThrowingMove::copy_from(__src.as_pointer()) } } +<<<<<<< HEAD impl ByteRepr for ThrowingMove {} #[derive(VaArg, FnPtrArg, Default)] +======= +impl ByteRepr for ThrowingMove { + fn byte_size() -> usize { + 12 + } + fn to_bytes(&self, buf: &mut [u8]) { + self.v.to_bytes(&mut buf[0..4]); + self.copies.to_bytes(&mut buf[4..8]); + self.moves.to_bytes(&mut buf[8..12]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + v: ::from_bytes(&buf[0..4]), + copies: ::from_bytes(&buf[4..8]), + moves: ::from_bytes(&buf[8..12]), + } + } +} +#[derive(Record, VaArg, FnPtrArg, Default)] +>>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub struct NoexceptMove { - pub v: Value, - pub copies: Value, - pub moves: Value, + #[offset(0)] + pub v: i32, + #[offset(4)] + pub copies: i32, + #[offset(8)] + pub moves: i32, } impl NoexceptMove { pub fn new(v: i32) -> Self { let v: Value = Rc::new(RefCell::new(v)); let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*v.borrow()))), - copies: Rc::new(RefCell::new(0)), - moves: Rc::new(RefCell::new(0)), + v: (*v.borrow()), + copies: 0, + moves: 0, })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } pub fn copy_from(o: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*(*o.upgrade().deref()).v.borrow()))), - copies: Rc::new(RefCell::new( - ((*(*o.upgrade().deref()).copies.borrow()) + 1), - )), - moves: Rc::new(RefCell::new((*(*o.upgrade().deref()).moves.borrow()))), + v: { o.with(|__s: &NoexceptMove| __s.v) }, + copies: { (o.with(|__s: &NoexceptMove| __s.copies) + 1) }, + moves: { o.with(|__s: &NoexceptMove| __s.moves) }, })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } pub fn move_from(o: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*(*o.upgrade().deref()).v.borrow()))), - copies: Rc::new(RefCell::new((*(*o.upgrade().deref()).copies.borrow()))), - moves: Rc::new(RefCell::new(((*(*o.upgrade().deref()).moves.borrow()) + 1))), + v: { o.with(|__s: &NoexceptMove| __s.v) }, + copies: { o.with(|__s: &NoexceptMove| __s.copies) }, + moves: { (o.with(|__s: &NoexceptMove| __s.moves) + 1) }, })); let this: Ptr = __this.as_pointer(); - (*(*o.upgrade().deref()).v.borrow_mut()) = 0; + o.with_mut(|__s: &mut NoexceptMove| __s.v = 0); Rc::try_unwrap(__this).ok().unwrap().into_inner() } } @@ -161,10 +216,30 @@ impl Clone for NoexceptMove { NoexceptMove::copy_from(__src.as_pointer()) } } +<<<<<<< HEAD impl ByteRepr for NoexceptMove {} +======= +impl ByteRepr for NoexceptMove { + fn byte_size() -> usize { + 12 + } + fn to_bytes(&self, buf: &mut [u8]) { + self.v.to_bytes(&mut buf[0..4]); + self.copies.to_bytes(&mut buf[4..8]); + self.moves.to_bytes(&mut buf[8..12]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + v: ::from_bytes(&buf[0..4]), + copies: ::from_bytes(&buf[4..8]), + moves: ::from_bytes(&buf[8..12]), + } + } +} +>>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub fn by_value_0(m: MoveOnly) -> i32 { let m: Value = Rc::new(RefCell::new(m)); - return (*(*m.borrow()).v.borrow()); + return { (*m.borrow()).v }; } pub fn make_1(v: i32) -> MoveOnly { let v: Value = Rc::new(RefCell::new(v)); @@ -178,64 +253,58 @@ pub fn main() { fn main_0() -> i32 { let a: Value = Rc::new(RefCell::new(MoveOnly::new({ 1 }))); let b: Value = Rc::new(RefCell::new(MoveOnly::move_from({ a.as_pointer() }))); - assert!(((*(*b.borrow()).v.borrow()) == 1)); - assert!(((*(*a.borrow()).v.borrow()) == 0)); + assert!(({ (*b.borrow()).v } == 1)); + assert!(({ (*a.borrow()).v } == 0)); let c: Value = Rc::new(RefCell::new(MoveOnly::move_from({ b.as_pointer() }))); - assert!(((*(*c.borrow()).v.borrow()) == 1)); - assert!(((*(*b.borrow()).v.borrow()) == 0)); + assert!(({ (*c.borrow()).v } == 1)); + assert!(({ (*b.borrow()).v } == 0)); let d: Value = Rc::new(RefCell::new(MoveOnly::move_from({ c.as_pointer() }))); - assert!(((*(*d.borrow()).v.borrow()) == 1)); - assert!(((*(*c.borrow()).v.borrow()) == 0)); + assert!(({ (*d.borrow()).v } == 1)); + assert!(({ (*c.borrow()).v } == 0)); let e: Value = Rc::new(RefCell::new(({ make_1(5) }))); - assert!(((*(*e.borrow()).v.borrow()) == 5)); + assert!(({ (*e.borrow()).v } == 5)); assert!((({ by_value_0(MoveOnly::new({ 6 },),) }) == 6)); assert!((({ by_value_0(MoveOnly::move_from({ e.as_pointer() },),) }) == 5)); - assert!(((*(*e.borrow()).v.borrow()) == 0)); + assert!(({ (*e.borrow()).v } == 0)); let vec_: Value> = Rc::new(RefCell::new(Vec::new())); (*vec_.borrow_mut()).push(MoveOnly::new({ 7 })); let f: Value = Rc::new(RefCell::new(MoveOnly::new({ 8 }))); (*vec_.borrow_mut()).push(MoveOnly::move_from({ f.as_pointer() })); assert!( - ((*(*(vec_.as_pointer() as Ptr) + ((vec_.as_pointer() as Ptr) .offset(0_usize) - .upgrade() - .deref()) - .v - .borrow()) + .with(|__s: &MoveOnly| __s.v) == 7) - && ((*(*(vec_.as_pointer() as Ptr) + && ((vec_.as_pointer() as Ptr) .offset(1_usize) - .upgrade() - .deref()) - .v - .borrow()) + .with(|__s: &MoveOnly| __s.v) == 8) ); - assert!(((*(*f.borrow()).v.borrow()) == 0)); + assert!(({ (*f.borrow()).v } == 0)); let m: Value = Rc::new(RefCell::new(ConstMove::new())); let m1: Value = Rc::new(RefCell::new(ConstMove::new_1({ m.as_pointer() }))); let cm: Value = Rc::new(RefCell::new(ConstMove::new())); let m2: Value = Rc::new(RefCell::new(ConstMove::new_2({ cm.as_pointer() }))); - assert!(((*(*m1.borrow()).mark.borrow()) == 1)); - assert!(((*(*m2.borrow()).mark.borrow()) == 10)); + assert!(({ (*m1.borrow()).mark } == 1)); + assert!(({ (*m2.borrow()).mark } == 10)); let t: Value = Rc::new(RefCell::new(ThrowingMove::new({ 1 }))); let t1: Value = Rc::new(RefCell::new(ThrowingMove::copy_from({ t.as_pointer() }))); - assert!(((*(*t1.borrow()).v.borrow()) == 1)); - assert!(((*(*t1.borrow()).copies.borrow()) == 1)); - assert!(((*(*t1.borrow()).moves.borrow()) == 0)); - assert!(((*(*t.borrow()).v.borrow()) == 1)); + assert!(({ (*t1.borrow()).v } == 1)); + assert!(({ (*t1.borrow()).copies } == 1)); + assert!(({ (*t1.borrow()).moves } == 0)); + assert!(({ (*t.borrow()).v } == 1)); let n: Value = Rc::new(RefCell::new(NoexceptMove::new({ 2 }))); let n1: Value = Rc::new(RefCell::new(NoexceptMove::move_from({ n.as_pointer() }))); - assert!(((*(*n1.borrow()).v.borrow()) == 2)); - assert!(((*(*n1.borrow()).copies.borrow()) == 0)); - assert!(((*(*n1.borrow()).moves.borrow()) == 1)); - assert!(((*(*n.borrow()).v.borrow()) == 0)); + assert!(({ (*n1.borrow()).v } == 2)); + assert!(({ (*n1.borrow()).copies } == 0)); + assert!(({ (*n1.borrow()).moves } == 1)); + assert!(({ (*n.borrow()).v } == 0)); let g: Value = Rc::new(RefCell::new(MoveOnly::new({ 3 }))); let g1: Value = Rc::new(RefCell::new(MoveOnly::move_from({ g.as_pointer() }))); - assert!(((*(*g1.borrow()).v.borrow()) == 3)); - assert!(((*(*g.borrow()).v.borrow()) == 0)); + assert!(({ (*g1.borrow()).v } == 3)); + assert!(({ (*g.borrow()).v } == 0)); return 0; } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/move_this.rs b/tests/unit/out/refcount/move_this.rs index 997c96ac19..2f8e9c08bf 100644 --- a/tests/unit/out/refcount/move_this.rs +++ b/tests/unit/out/refcount/move_this.rs @@ -6,32 +6,31 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Record, VaArg, FnPtrArg, Default)] pub struct Chain { - pub v: Value, + #[offset(0)] + pub v: i32, } impl Chain { pub fn new(v: i32) -> Self { let v: Value = Rc::new(RefCell::new(v)); - let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*v.borrow()))), - })); + let __this: Value = Rc::new(RefCell::new(Self { v: (*v.borrow()) })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } pub fn copy_from(o: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new(((*(*o.upgrade().deref()).v.borrow()) + 100))), + v: { (o.with(|__s: &Chain| __s.v) + 100) }, })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } pub fn move_from(o: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new(((*(*o.upgrade().deref()).v.borrow()) + 1))), + v: { (o.with(|__s: &Chain| __s.v) + 1) }, })); let this: Ptr = __this.as_pointer(); - (*(*o.upgrade().deref()).v.borrow_mut()) = 0; + o.with_mut(|__s: &mut Chain| __s.v = 0); Rc::try_unwrap(__this).ok().unwrap().into_inner() } } @@ -41,10 +40,26 @@ impl Clone for Chain { Chain::copy_from(__src.as_pointer()) } } +<<<<<<< HEAD impl ByteRepr for Chain {} +======= +impl ByteRepr for Chain { + fn byte_size() -> usize { + 4 + } + fn to_bytes(&self, buf: &mut [u8]) { + self.v.to_bytes(&mut buf[0..4]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + v: ::from_bytes(&buf[0..4]), + } + } +} +>>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub fn consume_0(c: Chain) -> i32 { let c: Value = Rc::new(RefCell::new(c)); - return (*(*c.borrow()).v.borrow()); + return { (*c.borrow()).v }; } pub fn main() { __cpp2rust_init_globals(); @@ -53,18 +68,18 @@ pub fn main() { fn main_0() -> i32 { let a: Value = Rc::new(RefCell::new(Chain::new({ 1 }))); ({ ChainImpl::add_4(&({ ChainImpl::add_4(&a.as_pointer(), 1) }), 1) }); - assert!(((*(*a.borrow()).v.borrow()) == 3)); + assert!(({ (*a.borrow()).v } == 3)); let b0: Value = Rc::new(RefCell::new(Chain::new({ 5 }))); let b: Value = Rc::new(RefCell::new(Chain::move_from({ ({ ChainImpl::add_5(&({ ChainImpl::add_5(&b0.as_pointer(), 1) }), 1) }) }))); - assert!(((*(*b.borrow()).v.borrow()) == 8) && ((*(*b0.borrow()).v.borrow()) == 0)); + assert!(({ (*b.borrow()).v } == 8) && ({ (*b0.borrow()).v } == 0)); let c: Value = Rc::new(RefCell::new( ({ ChainImpl::take(&Rc::new(RefCell::new(Chain::new({ 10 }))).as_pointer()) }), )); - assert!(((*(*c.borrow()).v.borrow()) == 11)); + assert!(({ (*c.borrow()).v } == 11)); let d: Value = Rc::new(RefCell::new(({ ChainImpl::copy(&c.as_pointer()) }))); - assert!(((*(*d.borrow()).v.borrow()) == 111) && ((*(*c.borrow()).v.borrow()) == 11)); + assert!(({ (*d.borrow()).v } == 111) && ({ (*c.borrow()).v } == 11)); let g: Value = Rc::new(RefCell::new(Chain::new({ 20 }))); assert!( (({ @@ -75,7 +90,7 @@ fn main_0() -> i32 { ); let e: Value = Rc::new(RefCell::new(Chain::new({ 30 }))); let f: Value = Rc::new(RefCell::new(({ ChainImpl::take(&e.as_pointer()) }))); - assert!(((*(*f.borrow()).v.borrow()) == 31) && ((*(*e.borrow()).v.borrow()) == 0)); + assert!(({ (*f.borrow()).v } == 31) && ({ (*e.borrow()).v } == 0)); return 0; } pub trait ChainImpl { @@ -88,12 +103,12 @@ pub trait ChainImpl { impl ChainImpl for Ptr { fn add_4(&self, n: i32) -> Ptr { let n: Value = Rc::new(RefCell::new(n)); - (*(*(*self).upgrade().deref()).v.borrow_mut()) += (*n.borrow()); + (*self).with_mut(|__s: &mut Chain| __s.v += (*n.borrow())); return (*self).clone(); } fn add_5(&self, n: i32) -> Ptr { let n: Value = Rc::new(RefCell::new(n)); - (*(*(*self).upgrade().deref()).v.borrow_mut()) += (*n.borrow()); + (*self).with_mut(|__s: &mut Chain| __s.v += (*n.borrow())); return (*self).clone(); } fn take(&self) -> Chain { diff --git a/tests/unit/out/refcount/nested_structs.rs b/tests/unit/out/refcount/nested_structs.rs index 55c110545a..4dcb0c931f 100644 --- a/tests/unit/out/refcount/nested_structs.rs +++ b/tests/unit/out/refcount/nested_structs.rs @@ -6,169 +6,121 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Level0_Level1_1_Level2_1_Level3_1 { - pub x1: Value, -} -impl Clone for Level0_Level1_1_Level2_1_Level3_1 { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - x1: Rc::new(RefCell::new((*self.x1.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub x1: i32, } impl ByteRepr for Level0_Level1_1_Level2_1_Level3_1 { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.x1.borrow()).to_bytes(&mut buf[0..4]); + self.x1.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - x1: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + x1: ::from_bytes(&buf[0..4]), } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Level0_Level1_1_Level2_1_Level3_2 { - pub x1: Value, - pub x2: Value, -} -impl Clone for Level0_Level1_1_Level2_1_Level3_2 { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - x1: Rc::new(RefCell::new((*self.x1.borrow()))), - x2: Rc::new(RefCell::new((*self.x2.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub x1: i32, + #[offset(4)] + pub x2: i32, } impl ByteRepr for Level0_Level1_1_Level2_1_Level3_2 { fn byte_size() -> usize { 8 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.x1.borrow()).to_bytes(&mut buf[0..4]); - (*self.x2.borrow()).to_bytes(&mut buf[4..8]); + self.x1.to_bytes(&mut buf[0..4]); + self.x2.to_bytes(&mut buf[4..8]); } fn from_bytes(buf: &[u8]) -> Self { Self { - x1: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - x2: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + x1: ::from_bytes(&buf[0..4]), + x2: ::from_bytes(&buf[4..8]), } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Level0_Level1_1_Level2_1 { - pub x1: Value, -} -impl Clone for Level0_Level1_1_Level2_1 { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - x1: Rc::new(RefCell::new((*self.x1.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub x1: i32, } impl ByteRepr for Level0_Level1_1_Level2_1 { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.x1.borrow()).to_bytes(&mut buf[0..4]); + self.x1.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - x1: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + x1: ::from_bytes(&buf[0..4]), } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Level0_Level1_1 { - pub x1: Value, -} -impl Clone for Level0_Level1_1 { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - x1: Rc::new(RefCell::new((*self.x1.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub x1: i32, } impl ByteRepr for Level0_Level1_1 { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.x1.borrow()).to_bytes(&mut buf[0..4]); + self.x1.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - x1: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + x1: ::from_bytes(&buf[0..4]), } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Level0_Level1_2 { - pub x1: Value, - pub x2: Value, -} -impl Clone for Level0_Level1_2 { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - x1: Rc::new(RefCell::new((*self.x1.borrow()))), - x2: Rc::new(RefCell::new((*self.x2.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub x1: i32, + #[offset(4)] + pub x2: i32, } impl ByteRepr for Level0_Level1_2 { fn byte_size() -> usize { 8 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.x1.borrow()).to_bytes(&mut buf[0..4]); - (*self.x2.borrow()).to_bytes(&mut buf[4..8]); + self.x1.to_bytes(&mut buf[0..4]); + self.x2.to_bytes(&mut buf[4..8]); } fn from_bytes(buf: &[u8]) -> Self { Self { - x1: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - x2: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + x1: ::from_bytes(&buf[0..4]), + x2: ::from_bytes(&buf[4..8]), } } } -#[derive(Clone, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] pub struct Level0 {} pub fn main() { __cpp2rust_init_globals(); std::process::exit(main_0()); } fn main_0() -> i32 { - let x1: Value = Rc::new(RefCell::new(Level0_Level1_1 { - x1: Rc::new(RefCell::new(0)), - })); - let x2: Value = Rc::new(RefCell::new(Level0_Level1_2 { - x1: Rc::new(RefCell::new(1)), - x2: Rc::new(RefCell::new(2)), - })); - let x3: Value = Rc::new(RefCell::new(Level0_Level1_1_Level2_1 { - x1: Rc::new(RefCell::new(3)), - })); + let x1: Value = Rc::new(RefCell::new(Level0_Level1_1 { x1: 0 })); + let x2: Value = Rc::new(RefCell::new(Level0_Level1_2 { x1: 1, x2: 2 })); + let x3: Value = + Rc::new(RefCell::new(Level0_Level1_1_Level2_1 { x1: 3 })); let x4: Value = - Rc::new(RefCell::new(Level0_Level1_1_Level2_1_Level3_1 { - x1: Rc::new(RefCell::new(4)), - })); + Rc::new(RefCell::new(Level0_Level1_1_Level2_1_Level3_1 { x1: 4 })); let x5: Value = Rc::new(RefCell::new(Level0_Level1_1_Level2_1_Level3_2 { - x1: Rc::new(RefCell::new(5)), - x2: Rc::new(RefCell::new(6)), + x1: 5, + x2: 6, })); return 0; } diff --git a/tests/unit/out/refcount/new.rs b/tests/unit/out/refcount/new.rs index dbd75e9800..851c326f9d 100644 --- a/tests/unit/out/refcount/new.rs +++ b/tests/unit/out/refcount/new.rs @@ -6,33 +6,25 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Pair { - pub x: Value, - pub y: Value, -} -impl Clone for Pair { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - x: Rc::new(RefCell::new((*self.x.borrow()))), - y: Rc::new(RefCell::new((*self.y.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub x: i32, + #[offset(4)] + pub y: i32, } impl ByteRepr for Pair { fn byte_size() -> usize { 8 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.x.borrow()).to_bytes(&mut buf[0..4]); - (*self.y.borrow()).to_bytes(&mut buf[4..8]); + self.x.to_bytes(&mut buf[0..4]); + self.y.to_bytes(&mut buf[4..8]); } fn from_bytes(buf: &[u8]) -> Self { Self { - x: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - y: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + x: ::from_bytes(&buf[0..4]), + y: ::from_bytes(&buf[4..8]), } } } @@ -50,12 +42,12 @@ fn main_0() -> i32 { assert!((((*y.borrow()).read()) == 9)); (*y.borrow()).delete(); let p: Value> = Rc::new(RefCell::new(Ptr::alloc(::default()))); - (*(*(*p.borrow()).upgrade().deref()).x.borrow_mut()) = 1; - (*(*(*p.borrow()).upgrade().deref()).y.borrow_mut()) = 2; + (*p.borrow()).with_mut(|__s: &mut Pair| __s.x = 1); + (*p.borrow()).with_mut(|__s: &mut Pair| __s.y = 2); assert!( ({ - let _lhs = (*(*(*p.borrow()).upgrade().deref()).x.borrow()); - _lhs + (*(*(*p.borrow()).upgrade().deref()).y.borrow()) + let _lhs = (*p.borrow()).with(|__s: &Pair| __s.x); + _lhs + (*p.borrow()).with(|__s: &Pair| __s.y) } == 3) ); (*p.borrow()).delete(); diff --git a/tests/unit/out/refcount/new_bst.rs b/tests/unit/out/refcount/new_bst.rs index 70b14cd1f7..684769681e 100644 --- a/tests/unit/out/refcount/new_bst.rs +++ b/tests/unit/out/refcount/new_bst.rs @@ -6,37 +6,29 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct node_t { - pub left: Value>, - pub right: Value>, - pub value: Value, -} -impl Clone for node_t { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - left: Rc::new(RefCell::new((*self.left.borrow()).clone())), - right: Rc::new(RefCell::new((*self.right.borrow()).clone())), - value: Rc::new(RefCell::new((*self.value.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub left: Ptr, + #[offset(8)] + pub right: Ptr, + #[offset(16)] + pub value: i32, } impl ByteRepr for node_t { fn byte_size() -> usize { 24 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.left.borrow()).to_bytes(&mut buf[0..8]); - (*self.right.borrow()).to_bytes(&mut buf[8..16]); - (*self.value.borrow()).to_bytes(&mut buf[16..20]); + self.left.to_bytes(&mut buf[0..8]); + self.right.to_bytes(&mut buf[8..16]); + self.value.to_bytes(&mut buf[16..20]); } fn from_bytes(buf: &[u8]) -> Self { Self { - left: Rc::new(RefCell::new(>::from_bytes(&buf[0..8]))), - right: Rc::new(RefCell::new(>::from_bytes(&buf[8..16]))), - value: Rc::new(RefCell::new(::from_bytes(&buf[16..20]))), + left: >::from_bytes(&buf[0..8]), + right: >::from_bytes(&buf[8..16]), + value: ::from_bytes(&buf[16..20]), } } } @@ -45,29 +37,29 @@ pub fn find_0(node: Ptr, value: i32) -> Ptr { let value: Value = Rc::new(RefCell::new(value)); if ({ let _lhs = (*value.borrow()); - _lhs < (*(*(*node.borrow()).upgrade().deref()).value.borrow()) - }) && (!((*(*(*node.borrow()).upgrade().deref()).left.borrow()).is_null())) + _lhs < (*node.borrow()).with(|__s: &node_t| __s.value) + }) && (!(((*node.borrow()).with(|__s: &node_t| (__s.left).clone())).is_null())) { return ({ find_0( - (*(*(*node.borrow()).upgrade().deref()).left.borrow()).clone(), + (*node.borrow()).with(|__s: &node_t| (__s.left).clone()), (*value.borrow()), ) }); } else if ({ let _lhs = (*value.borrow()); - _lhs > (*(*(*node.borrow()).upgrade().deref()).value.borrow()) - }) && (!((*(*(*node.borrow()).upgrade().deref()).right.borrow()).is_null())) + _lhs > (*node.borrow()).with(|__s: &node_t| __s.value) + }) && (!(((*node.borrow()).with(|__s: &node_t| (__s.right).clone())).is_null())) { return ({ find_0( - (*(*(*node.borrow()).upgrade().deref()).right.borrow()).clone(), + (*node.borrow()).with(|__s: &node_t| (__s.right).clone()), (*value.borrow()), ) }); } else if { let _lhs = (*value.borrow()); - _lhs == (*(*(*node.borrow()).upgrade().deref()).value.borrow()) + _lhs == (*node.borrow()).with(|__s: &node_t| __s.value) } { return (*node.borrow()).clone(); } @@ -78,43 +70,43 @@ pub fn insert_1(node: Ptr, value: i32) -> Ptr { let value: Value = Rc::new(RefCell::new(value)); if (*node.borrow()).is_null() { return Ptr::alloc(node_t { - left: Rc::new(RefCell::new(Ptr::::null())), - right: Rc::new(RefCell::new(Ptr::::null())), - value: Rc::new(RefCell::new((*value.borrow()))), + left: Ptr::::null(), + right: Ptr::::null(), + value: (*value.borrow()), }); } if { let _lhs = (*value.borrow()); - _lhs < (*(*(*node.borrow()).upgrade().deref()).value.borrow()) + _lhs < (*node.borrow()).with(|__s: &node_t| __s.value) } { let __rhs = ({ insert_1( - (*(*(*node.borrow()).upgrade().deref()).left.borrow()).clone(), + (*node.borrow()).with(|__s: &node_t| (__s.left).clone()), (*value.borrow()), ) }); - (*(*(*node.borrow()).upgrade().deref()).left.borrow_mut()) = __rhs; + (*node.borrow()).with_mut(|__s: &mut node_t| __s.left = __rhs); } else if { let _lhs = (*value.borrow()); - _lhs > (*(*(*node.borrow()).upgrade().deref()).value.borrow()) + _lhs > (*node.borrow()).with(|__s: &node_t| __s.value) } { let __rhs = ({ insert_1( - (*(*(*node.borrow()).upgrade().deref()).right.borrow()).clone(), + (*node.borrow()).with(|__s: &node_t| (__s.right).clone()), (*value.borrow()), ) }); - (*(*(*node.borrow()).upgrade().deref()).right.borrow_mut()) = __rhs; + (*node.borrow()).with_mut(|__s: &mut node_t| __s.right = __rhs); } return (*node.borrow()).clone(); } pub fn del_2(node: Ptr) { let node: Value> = Rc::new(RefCell::new(node)); - if !((*(*(*node.borrow()).upgrade().deref()).left.borrow()).is_null()) { - ({ del_2((*(*(*node.borrow()).upgrade().deref()).left.borrow()).clone()) }); + if !(((*node.borrow()).with(|__s: &node_t| (__s.left).clone())).is_null()) { + ({ del_2((*node.borrow()).with(|__s: &node_t| (__s.left).clone())) }); } - if !((*(*(*node.borrow()).upgrade().deref()).right.borrow()).is_null()) { - ({ del_2((*(*(*node.borrow()).upgrade().deref()).right.borrow()).clone()) }); + if !(((*node.borrow()).with(|__s: &node_t| (__s.right).clone())).is_null()) { + ({ del_2((*node.borrow()).with(|__s: &node_t| (__s.right).clone())) }); } (*node.borrow()).delete(); } @@ -124,9 +116,9 @@ pub fn main() { } fn main_0() -> i32 { let root: Value> = Rc::new(RefCell::new(Ptr::alloc(node_t { - left: Rc::new(RefCell::new(Ptr::::null())), - right: Rc::new(RefCell::new(Ptr::::null())), - value: Rc::new(RefCell::new(0)), + left: Ptr::::null(), + right: Ptr::::null(), + value: 0, }))); let __rhs = ({ insert_1((*root.borrow()).clone(), 1) }); (*root.borrow_mut()) = __rhs; @@ -137,26 +129,11 @@ fn main_0() -> i32 { let __rhs = ({ insert_1((*root.borrow()).clone(), 4) }); (*root.borrow_mut()) = __rhs; let out: Value = Rc::new(RefCell::new( - ((((((*(*({ find_0((*root.borrow()).clone(), 0) }).upgrade().deref()) - .value - .borrow()) - == 0) - && ((*(*({ find_0((*root.borrow()).clone(), 1) }).upgrade().deref()) - .value - .borrow()) - == 1)) - && ((*(*({ find_0((*root.borrow()).clone(), 2) }).upgrade().deref()) - .value - .borrow()) - == 2)) - && ((*(*({ find_0((*root.borrow()).clone(), 3) }).upgrade().deref()) - .value - .borrow()) - == 3)) - && ((*(*({ find_0((*root.borrow()).clone(), 4) }).upgrade().deref()) - .value - .borrow()) - == 4)) + (((((({ find_0((*root.borrow()).clone(), 0) }).with(|__s: &node_t| __s.value) == 0) + && (({ find_0((*root.borrow()).clone(), 1) }).with(|__s: &node_t| __s.value) == 1)) + && (({ find_0((*root.borrow()).clone(), 2) }).with(|__s: &node_t| __s.value) == 2)) + && (({ find_0((*root.borrow()).clone(), 3) }).with(|__s: &node_t| __s.value) == 3)) + && (({ find_0((*root.borrow()).clone(), 4) }).with(|__s: &node_t| __s.value) == 4)) && (({ find_0((*root.borrow()).clone(), 5) }).is_null()), )); ({ del_2((*root.borrow()).clone()) }); diff --git a/tests/unit/out/refcount/new_struct.rs b/tests/unit/out/refcount/new_struct.rs index 0ee5cc018d..c54cb6065c 100644 --- a/tests/unit/out/refcount/new_struct.rs +++ b/tests/unit/out/refcount/new_struct.rs @@ -6,67 +6,51 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Pair { - pub x: Value, - pub y: Value, -} -impl Clone for Pair { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - x: Rc::new(RefCell::new((*self.x.borrow()))), - y: Rc::new(RefCell::new((*self.y.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub x: i32, + #[offset(4)] + pub y: i32, } impl ByteRepr for Pair { fn byte_size() -> usize { 8 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.x.borrow()).to_bytes(&mut buf[0..4]); - (*self.y.borrow()).to_bytes(&mut buf[4..8]); + self.x.to_bytes(&mut buf[0..4]); + self.y.to_bytes(&mut buf[4..8]); } fn from_bytes(buf: &[u8]) -> Self { Self { - x: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - y: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + x: ::from_bytes(&buf[0..4]), + y: ::from_bytes(&buf[4..8]), } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Triple { - pub a: Value, - pub b: Value, - pub p: Value, -} -impl Clone for Triple { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - a: Rc::new(RefCell::new((*self.a.borrow()))), - b: Rc::new(RefCell::new((*self.b.borrow()))), - p: Rc::new(RefCell::new((*self.p.borrow()).clone())), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub a: i32, + #[offset(4)] + pub b: i32, + #[offset(8)] + pub p: Pair, } impl ByteRepr for Triple { fn byte_size() -> usize { 16 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.a.borrow()).to_bytes(&mut buf[0..4]); - (*self.b.borrow()).to_bytes(&mut buf[4..8]); - (*self.p.borrow()).to_bytes(&mut buf[8..16]); + self.a.to_bytes(&mut buf[0..4]); + self.b.to_bytes(&mut buf[4..8]); + self.p.to_bytes(&mut buf[8..16]); } fn from_bytes(buf: &[u8]) -> Self { Self { - a: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - b: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), - p: Rc::new(RefCell::new(::from_bytes(&buf[8..16]))), + a: ::from_bytes(&buf[0..4]), + b: ::from_bytes(&buf[4..8]), + p: ::from_bytes(&buf[8..16]), } } } @@ -75,37 +59,31 @@ pub fn main() { std::process::exit(main_0()); } fn main_0() -> i32 { - let p: Value> = Rc::new(RefCell::new(Ptr::alloc(Pair { - x: Rc::new(RefCell::new(1)), - y: Rc::new(RefCell::new(2)), - }))); + let p: Value> = Rc::new(RefCell::new(Ptr::alloc(Pair { x: 1, y: 2 }))); let out: Value = Rc::new(RefCell::new({ - let _lhs = (*(*(*p.borrow()).upgrade().deref()).x.borrow()); - _lhs + (*(*(*p.borrow()).upgrade().deref()).y.borrow()) + let _lhs = (*p.borrow()).with(|__s: &Pair| __s.x); + _lhs + (*p.borrow()).with(|__s: &Pair| __s.y) })); (*p.borrow()).delete(); assert!(((*out.borrow()) == 3)); let t: Value = Rc::new(RefCell::new(Triple { - a: Rc::new(RefCell::new(1)), - b: Rc::new(RefCell::new(0_i32)), - p: Rc::new(RefCell::new(::default())), + a: 1, + b: 0_i32, + p: ::default(), })); - assert!(((*(*t.borrow()).a.borrow()) == 1)); - assert!(((*(*t.borrow()).b.borrow()) == 0)); - assert!( - ((*(*(*t.borrow()).p.borrow()).x.borrow()) == 0) - && ((*(*(*t.borrow()).p.borrow()).y.borrow()) == 0) - ); + assert!(({ (*t.borrow()).a } == 1)); + assert!(({ (*t.borrow()).b } == 0)); + assert!(({ (*t.borrow()).p.x } == 0) && ({ (*t.borrow()).p.y } == 0)); let q: Value> = Rc::new(RefCell::new(Ptr::alloc(Triple { - a: Rc::new(RefCell::new(2)), - b: Rc::new(RefCell::new(3)), - p: Rc::new(RefCell::new(::default())), + a: 2, + b: 3, + p: ::default(), }))); - assert!(((*(*(*q.borrow()).upgrade().deref()).a.borrow()) == 2)); - assert!(((*(*(*q.borrow()).upgrade().deref()).b.borrow()) == 3)); + assert!(((*q.borrow()).with(|__s: &Triple| __s.a) == 2)); + assert!(((*q.borrow()).with(|__s: &Triple| __s.b) == 3)); assert!( - ((*(*(*(*q.borrow()).upgrade().deref()).p.borrow()).x.borrow()) == 0) - && ((*(*(*(*q.borrow()).upgrade().deref()).p.borrow()).y.borrow()) == 0) + ((*q.borrow()).with(|__s: &Triple| __s.p.x) == 0) + && ((*q.borrow()).with(|__s: &Triple| __s.p.y) == 0) ); (*q.borrow()).delete(); return 0; diff --git a/tests/unit/out/refcount/offsetof.rs b/tests/unit/out/refcount/offsetof.rs index e143c05a31..99f21611a4 100644 --- a/tests/unit/out/refcount/offsetof.rs +++ b/tests/unit/out/refcount/offsetof.rs @@ -6,62 +6,44 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Layout { - pub a: Value, - pub b: Value, - pub c: Value, -} -impl Clone for Layout { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - a: Rc::new(RefCell::new((*self.a.borrow()))), - b: Rc::new(RefCell::new((*self.b.borrow()))), - c: Rc::new(RefCell::new((*self.c.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub a: u8, + #[offset(4)] + pub b: u32, + #[offset(8)] + pub c: u16, } impl ByteRepr for Layout { fn byte_size() -> usize { 12 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.a.borrow()).to_bytes(&mut buf[0..1]); - (*self.b.borrow()).to_bytes(&mut buf[4..8]); - (*self.c.borrow()).to_bytes(&mut buf[8..10]); + self.a.to_bytes(&mut buf[0..1]); + self.b.to_bytes(&mut buf[4..8]); + self.c.to_bytes(&mut buf[8..10]); } fn from_bytes(buf: &[u8]) -> Self { Self { - a: Rc::new(RefCell::new(::from_bytes(&buf[0..1]))), - b: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), - c: Rc::new(RefCell::new(::from_bytes(&buf[8..10]))), + a: ::from_bytes(&buf[0..1]), + b: ::from_bytes(&buf[4..8]), + c: ::from_bytes(&buf[8..10]), } } } -#[derive(VaArg, FnPtrArg)] +#[derive(Clone, Record, VaArg, FnPtrArg)] pub struct Frame { - pub tag: Value, - pub body: Value>, -} -impl Clone for Frame { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - tag: Rc::new(RefCell::new((*self.tag.borrow()))), - body: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 64, _>( - |__i: usize| (*self.body.borrow())[(__i) as usize], - )))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub tag: u16, + #[offset(2)] + pub body: Box<[u8]>, } impl Default for Frame { fn default() -> Self { Frame { - tag: Rc::new(RefCell::new(0_u16)), - body: Rc::new(RefCell::new((0..64).map(|_| 0_u8).collect::>())), + tag: 0_u16, + body: (0..64).map(|_| 0_u8).collect::>(), } } } @@ -70,13 +52,13 @@ impl ByteRepr for Frame { 66 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.tag.borrow()).to_bytes(&mut buf[0..2]); - (*self.body.borrow()).to_bytes(&mut buf[2..66]); + self.tag.to_bytes(&mut buf[0..2]); + self.body.to_bytes(&mut buf[2..66]); } fn from_bytes(buf: &[u8]) -> Self { Self { - tag: Rc::new(RefCell::new(::from_bytes(&buf[0..2]))), - body: Rc::new(RefCell::new(>::from_bytes(&buf[2..66]))), + tag: ::from_bytes(&buf[0..2]), + body: >::from_bytes(&buf[2..66]), } } } @@ -89,11 +71,11 @@ fn main_0() -> i32 { assert!((4_usize == 4_usize)); assert!((8_usize == 8_usize)); let v: Value = Rc::new(RefCell::new(Layout { - a: Rc::new(RefCell::new(0_u8)), - b: Rc::new(RefCell::new(0_u32)), - c: Rc::new(RefCell::new(0_u16)), + a: 0_u8, + b: 0_u32, + c: 0_u16, })); - (*(*v.borrow()).b.borrow_mut()) = 3735928559_u32; + (*v.borrow_mut()).b = 3735928559_u32; let base: Value> = Rc::new(RefCell::new((v.as_pointer()).reinterpret_cast::())); let bp: Value> = Rc::new(RefCell::new( ((*base.borrow()).offset((4_usize) as isize)).reinterpret_cast::(), @@ -102,7 +84,7 @@ fn main_0() -> i32 { ((*base.borrow()).offset((4_usize) as isize)) .reinterpret_cast::() .write(305419896_u32); - assert!(((*(*v.borrow()).b.borrow()) == 305419896_u32)); + assert!(({ (*v.borrow()).b } == 305419896_u32)); let text: Value> = Rc::new(RefCell::new(Ptr::::from_string_literal( b"example-body", ))); diff --git a/tests/unit/out/refcount/opaque_forward_decl.rs b/tests/unit/out/refcount/opaque_forward_decl.rs index 907c99a135..3797dc543d 100644 --- a/tests/unit/out/refcount/opaque_forward_decl.rs +++ b/tests/unit/out/refcount/opaque_forward_decl.rs @@ -6,31 +6,25 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct container { - pub p: Value>, - pub x: Value, -} -impl Clone for container { - fn clone(&self) -> Self { - Self { - p: Rc::new(RefCell::new((*self.p.borrow()).clone())), - x: Rc::new(RefCell::new((*self.x.borrow()).clone())), - } - } + #[offset(0)] + pub p: Ptr, + #[offset(8)] + pub x: i32, } impl ByteRepr for container { fn byte_size() -> usize { 16 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.p.borrow()).to_bytes(&mut buf[0..8]); - (*self.x.borrow()).to_bytes(&mut buf[8..12]); + self.p.to_bytes(&mut buf[0..8]); + self.x.to_bytes(&mut buf[8..12]); } fn from_bytes(buf: &[u8]) -> Self { Self { - p: Rc::new(RefCell::new(>::from_bytes(&buf[0..8]))), - x: Rc::new(RefCell::new(::from_bytes(&buf[8..12]))), + p: >::from_bytes(&buf[0..8]), + x: ::from_bytes(&buf[8..12]), } } } @@ -40,11 +34,11 @@ pub fn main() { } fn main_0() -> i32 { let c: Value = Rc::new(RefCell::new(container { - p: Rc::new(RefCell::new(Ptr::::null())), - x: Rc::new(RefCell::new(42)), + p: Ptr::::null(), + x: 42, })); - &(*(*c.borrow()).p.borrow()); - return ((*(*c.borrow()).x.borrow()) - 42); + &({ (*c.borrow()).p.clone() }); + return ({ (*c.borrow()).x } - 42); } #[derive(Clone, Copy, Default, ByteRepr, VaArg, FnPtrArg)] pub struct opaque; diff --git a/tests/unit/out/refcount/opaque_record_copy.rs b/tests/unit/out/refcount/opaque_record_copy.rs index 6eb297eb3a..6670f57c73 100644 --- a/tests/unit/out/refcount/opaque_record_copy.rs +++ b/tests/unit/out/refcount/opaque_record_copy.rs @@ -6,35 +6,27 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Clone, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] pub struct Probe {} -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Wrapper_Probe_ { - pub base_: Value, - pub tag: Value, -} -impl Clone for Wrapper_Probe_ { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - base_: Rc::new(RefCell::new((*self.base_.borrow()).clone())), - tag: Rc::new(RefCell::new((*self.tag.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub base_: Probe, + #[offset(4)] + pub tag: i32, } impl ByteRepr for Wrapper_Probe_ { fn byte_size() -> usize { 8 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.base_.borrow()).to_bytes(&mut buf[0..1]); - (*self.tag.borrow()).to_bytes(&mut buf[4..8]); + self.base_.to_bytes(&mut buf[0..1]); + self.tag.to_bytes(&mut buf[4..8]); } fn from_bytes(buf: &[u8]) -> Self { Self { - base_: Rc::new(RefCell::new(::from_bytes(&buf[0..1]))), - tag: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + base_: ::from_bytes(&buf[0..1]), + tag: ::from_bytes(&buf[4..8]), } } } @@ -44,9 +36,9 @@ pub fn main() { } fn main_0() -> i32 { let a: Value = Rc::new(RefCell::new(::default())); - (*(*a.borrow()).tag.borrow_mut()) = 3; + (*a.borrow_mut()).tag = 3; let b: Value = Rc::new(RefCell::new((*a.borrow()).clone())); - assert!(((*(*b.borrow()).tag.borrow()) == 3)); + assert!(({ (*b.borrow()).tag } == 3)); return 0; } pub trait ProbeImpl { diff --git a/tests/unit/out/refcount/opaque_then_defined.rs b/tests/unit/out/refcount/opaque_then_defined.rs index d731204bd4..dfa73ea3a8 100644 --- a/tests/unit/out/refcount/opaque_then_defined.rs +++ b/tests/unit/out/refcount/opaque_then_defined.rs @@ -6,59 +6,47 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct list { - pub head: Value>, - pub size: Value, -} -impl Clone for list { - fn clone(&self) -> Self { - Self { - head: Rc::new(RefCell::new((*self.head.borrow()).clone())), - size: Rc::new(RefCell::new((*self.size.borrow()).clone())), - } - } + #[offset(0)] + pub head: Ptr, + #[offset(8)] + pub size: i32, } impl ByteRepr for list { fn byte_size() -> usize { 16 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.head.borrow()).to_bytes(&mut buf[0..8]); - (*self.size.borrow()).to_bytes(&mut buf[8..12]); + self.head.to_bytes(&mut buf[0..8]); + self.size.to_bytes(&mut buf[8..12]); } fn from_bytes(buf: &[u8]) -> Self { Self { - head: Rc::new(RefCell::new(>::from_bytes(&buf[0..8]))), - size: Rc::new(RefCell::new(::from_bytes(&buf[8..12]))), + head: >::from_bytes(&buf[0..8]), + size: ::from_bytes(&buf[8..12]), } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct node { - pub value: Value, - pub next: Value>, -} -impl Clone for node { - fn clone(&self) -> Self { - Self { - value: Rc::new(RefCell::new((*self.value.borrow()).clone())), - next: Rc::new(RefCell::new((*self.next.borrow()).clone())), - } - } + #[offset(0)] + pub value: i32, + #[offset(8)] + pub next: Ptr, } impl ByteRepr for node { fn byte_size() -> usize { 16 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.value.borrow()).to_bytes(&mut buf[0..4]); - (*self.next.borrow()).to_bytes(&mut buf[8..16]); + self.value.to_bytes(&mut buf[0..4]); + self.next.to_bytes(&mut buf[8..16]); } fn from_bytes(buf: &[u8]) -> Self { Self { - value: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - next: Rc::new(RefCell::new(>::from_bytes(&buf[8..16]))), + value: ::from_bytes(&buf[0..4]), + next: >::from_bytes(&buf[8..16]), } } } @@ -68,21 +56,15 @@ pub fn main() { } fn main_0() -> i32 { let n: Value = Rc::new(RefCell::new(node { - value: Rc::new(RefCell::new(42)), - next: Rc::new(RefCell::new(Ptr::::null())), + value: 42, + next: Ptr::::null(), })); let l: Value = Rc::new(RefCell::new(list { - head: Rc::new(RefCell::new((n.as_pointer()))), - size: Rc::new(RefCell::new(1)), + head: (n.as_pointer()), + size: 1, })); - assert!( - ((((*(*(*(*l.borrow()).head.borrow()).upgrade().deref()) - .value - .borrow()) - == 42) as i32) - != 0) - ); - assert!(((((*(*l.borrow()).size.borrow()) == 1) as i32) != 0)); + assert!(((({ (*l.borrow()).head.clone() }.with(|__s: &node| __s.value) == 42) as i32) != 0)); + assert!(((({ (*l.borrow()).size } == 1) as i32) != 0)); return 0; } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/operator_arithmetic_free.rs b/tests/unit/out/refcount/operator_arithmetic_free.rs index ce4141a69a..81dc207c4a 100644 --- a/tests/unit/out/refcount/operator_arithmetic_free.rs +++ b/tests/unit/out/refcount/operator_arithmetic_free.rs @@ -6,118 +6,110 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct S { - pub v: Value, -} -impl Clone for S { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*self.v.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub v: i32, } impl ByteRepr for S { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.v.borrow()).to_bytes(&mut buf[0..4]); + self.v.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - v: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + v: ::from_bytes(&buf[0..4]), } } } pub fn operator_add_0(a: Ptr, b: Ptr) -> S { return S { - v: Rc::new(RefCell::new({ - let _lhs = (*(*a.upgrade().deref()).v.borrow()); - _lhs + (*(*b.upgrade().deref()).v.borrow()) - })), + v: { + let _lhs = a.with(|__s: &S| __s.v); + _lhs + b.with(|__s: &S| __s.v) + }, }; } pub fn operator_sub_1(a: Ptr, b: Ptr) -> S { return S { - v: Rc::new(RefCell::new({ - let _lhs = (*(*a.upgrade().deref()).v.borrow()); - _lhs - (*(*b.upgrade().deref()).v.borrow()) - })), + v: { + let _lhs = a.with(|__s: &S| __s.v); + _lhs - b.with(|__s: &S| __s.v) + }, }; } pub fn operator_mul_2(a: Ptr, b: Ptr) -> S { return S { - v: Rc::new(RefCell::new({ - let _lhs = (*(*a.upgrade().deref()).v.borrow()); - _lhs * (*(*b.upgrade().deref()).v.borrow()) - })), + v: { + let _lhs = a.with(|__s: &S| __s.v); + _lhs * b.with(|__s: &S| __s.v) + }, }; } pub fn operator_div_3(a: Ptr, b: Ptr) -> S { return S { - v: Rc::new(RefCell::new({ - let _lhs = (*(*a.upgrade().deref()).v.borrow()); - _lhs / (*(*b.upgrade().deref()).v.borrow()) - })), + v: { + let _lhs = a.with(|__s: &S| __s.v); + _lhs / b.with(|__s: &S| __s.v) + }, }; } pub fn operator_rem_4(a: Ptr, b: Ptr) -> S { return S { - v: Rc::new(RefCell::new({ - let _lhs = (*(*a.upgrade().deref()).v.borrow()); - _lhs % (*(*b.upgrade().deref()).v.borrow()) - })), + v: { + let _lhs = a.with(|__s: &S| __s.v); + _lhs % b.with(|__s: &S| __s.v) + }, }; } pub fn operator_pos_5(a: Ptr) -> S { return S { - v: Rc::new(RefCell::new((*(*a.upgrade().deref()).v.borrow()))), + v: { a.with(|__s: &S| __s.v) }, }; } pub fn operator_neg_6(a: Ptr) -> S { return S { - v: Rc::new(RefCell::new(-(*(*a.upgrade().deref()).v.borrow()))), + v: { -a.with(|__s: &S| __s.v) }, }; } pub fn operator_inc_7(a: Ptr) -> Ptr { - (*(*a.upgrade().deref()).v.borrow_mut()).prefix_inc(); + a.with_mut(|__s: &mut S| __s.v.prefix_inc()); return (a).clone(); } pub fn operator_post_inc_8(a: Ptr, _a1: i32) -> S { let _a1: Value = Rc::new(RefCell::new(_a1)); - let old: Value = Rc::new(RefCell::new((*a.upgrade().deref()).clone())); - (*(*a.upgrade().deref()).v.borrow_mut()).prefix_inc(); + let old: Value = Rc::new(RefCell::new(a.read())); + a.with_mut(|__s: &mut S| __s.v.prefix_inc()); return (*old.borrow()).clone(); } pub fn operator_dec_9(a: Ptr) -> Ptr { - (*(*a.upgrade().deref()).v.borrow_mut()).prefix_dec(); + a.with_mut(|__s: &mut S| __s.v.prefix_dec()); return (a).clone(); } pub fn operator_post_dec_10(a: Ptr, _a1: i32) -> S { let _a1: Value = Rc::new(RefCell::new(_a1)); - let old: Value = Rc::new(RefCell::new((*a.upgrade().deref()).clone())); - (*(*a.upgrade().deref()).v.borrow_mut()).prefix_dec(); + let old: Value = Rc::new(RefCell::new(a.read())); + a.with_mut(|__s: &mut S| __s.v.prefix_dec()); return (*old.borrow()).clone(); } pub fn operator_add_11(a: Ptr, b: i32) -> S { let b: Value = Rc::new(RefCell::new(b)); return S { - v: Rc::new(RefCell::new({ - let _lhs = (*(*a.upgrade().deref()).v.borrow()); + v: { + let _lhs = a.with(|__s: &S| __s.v); _lhs + (*b.borrow()) - })), + }, }; } pub fn operator_add_12(a: i32, b: Ptr) -> S { let a: Value = Rc::new(RefCell::new(a)); return S { - v: Rc::new(RefCell::new({ + v: { let _lhs = (*a.borrow()); - _lhs + (*(*b.upgrade().deref()).v.borrow()) - })), + _lhs + b.with(|__s: &S| __s.v) + }, }; } pub fn main() { @@ -125,127 +117,117 @@ pub fn main() { std::process::exit(main_0()); } fn main_0() -> i32 { - let a: Value = Rc::new(RefCell::new(S { - v: Rc::new(RefCell::new(7)), - })); - let b: Value = Rc::new(RefCell::new(S { - v: Rc::new(RefCell::new(2)), - })); + let a: Value = Rc::new(RefCell::new(S { v: 7 })); + let b: Value = Rc::new(RefCell::new(S { v: 2 })); assert!( - ((*({ - let _a: Ptr = a.as_pointer(); - operator_add_0(_a, b.as_pointer()) - }) - .v - .borrow()) - == 9) + ({ + ({ + let _a: Ptr = a.as_pointer(); + operator_add_0(_a, b.as_pointer()) + }) + .v + } == 9) ); assert!( - ((*({ - let _a: Ptr = a.as_pointer(); - operator_sub_1(_a, b.as_pointer()) - }) - .v - .borrow()) - == 5) + ({ + ({ + let _a: Ptr = a.as_pointer(); + operator_sub_1(_a, b.as_pointer()) + }) + .v + } == 5) ); assert!( - ((*({ - let _a: Ptr = a.as_pointer(); - operator_mul_2(_a, b.as_pointer()) - }) - .v - .borrow()) - == 14) + ({ + ({ + let _a: Ptr = a.as_pointer(); + operator_mul_2(_a, b.as_pointer()) + }) + .v + } == 14) ); assert!( - ((*({ - let _a: Ptr = a.as_pointer(); - operator_div_3(_a, b.as_pointer()) - }) - .v - .borrow()) - == 3) + ({ + ({ + let _a: Ptr = a.as_pointer(); + operator_div_3(_a, b.as_pointer()) + }) + .v + } == 3) ); assert!( - ((*({ - let _a: Ptr = a.as_pointer(); - operator_rem_4(_a, b.as_pointer()) - }) - .v - .borrow()) - == 1) + ({ + ({ + let _a: Ptr = a.as_pointer(); + operator_rem_4(_a, b.as_pointer()) + }) + .v + } == 1) ); assert!( - ((*({ - let _a: Ptr = a.as_pointer(); - operator_pos_5(_a) - }) - .v - .borrow()) - == 7) + ({ + ({ + let _a: Ptr = a.as_pointer(); + operator_pos_5(_a) + }) + .v + } == 7) ); assert!( - ((*({ - let _a: Ptr = a.as_pointer(); - operator_neg_6(_a) - }) - .v - .borrow()) - == -7_i32) + ({ + ({ + let _a: Ptr = a.as_pointer(); + operator_neg_6(_a) + }) + .v + } == -7_i32) ); assert!( - ((*(*({ + (({ let _a: Ptr = a.as_pointer(); operator_inc_7(_a) }) - .upgrade() - .deref()) - .v - .borrow()) + .with(|__s: &S| __s.v) == 8) ); assert!( - ((*({ - let _a: Ptr = a.as_pointer(); - operator_post_inc_8(_a, 0) - }) - .v - .borrow()) - == 8) + ({ + ({ + let _a: Ptr = a.as_pointer(); + operator_post_inc_8(_a, 0) + }) + .v + } == 8) ); - assert!(((*(*a.borrow()).v.borrow()) == 9)); + assert!(({ (*a.borrow()).v } == 9)); assert!( - ((*(*({ + (({ let _a: Ptr = a.as_pointer(); operator_dec_9(_a) }) - .upgrade() - .deref()) - .v - .borrow()) + .with(|__s: &S| __s.v) == 8) ); assert!( - ((*({ - let _a: Ptr = a.as_pointer(); - operator_post_dec_10(_a, 0) - }) - .v - .borrow()) - == 8) + ({ + ({ + let _a: Ptr = a.as_pointer(); + operator_post_dec_10(_a, 0) + }) + .v + } == 8) ); - assert!(((*(*a.borrow()).v.borrow()) == 7)); + assert!(({ (*a.borrow()).v } == 7)); assert!( - ((*({ - let _a: Ptr = a.as_pointer(); - operator_add_11(_a, 1) - }) - .v - .borrow()) - == 8) + ({ + ({ + let _a: Ptr = a.as_pointer(); + operator_add_11(_a, 1) + }) + .v + } == 8) ); - assert!(((*({ operator_add_12(1, a.as_pointer(),) }).v.borrow()) == 8)); + assert!(({ ({ operator_add_12(1, a.as_pointer(),) }).v } == 8)); return 0; } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/operator_arithmetic_member.rs b/tests/unit/out/refcount/operator_arithmetic_member.rs index 79d1540864..2e3a6e6005 100644 --- a/tests/unit/out/refcount/operator_arithmetic_member.rs +++ b/tests/unit/out/refcount/operator_arithmetic_member.rs @@ -6,29 +6,21 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct S { - pub v: Value, -} -impl Clone for S { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*self.v.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub v: i32, } impl ByteRepr for S { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.v.borrow()).to_bytes(&mut buf[0..4]); + self.v.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - v: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + v: ::from_bytes(&buf[0..4]), } } } @@ -37,98 +29,37 @@ pub fn main() { std::process::exit(main_0()); } fn main_0() -> i32 { - let a: Value = Rc::new(RefCell::new(S { - v: Rc::new(RefCell::new(7)), - })); - let b: Value = Rc::new(RefCell::new(S { - v: Rc::new(RefCell::new(2)), - })); + let a: Value = Rc::new(RefCell::new(S { v: 7 })); + let b: Value = Rc::new(RefCell::new(S { v: 2 })); + assert!(({ ({ SImpl::operator_add_1(&a.as_pointer(), b.as_pointer(),) }).v } == 9)); + assert!(({ ({ SImpl::operator_sub_2(&a.as_pointer(), b.as_pointer(),) }).v } == 5)); + assert!(({ ({ SImpl::operator_mul(&a.as_pointer(), b.as_pointer(),) }).v } == 14)); + assert!(({ ({ SImpl::operator_div(&a.as_pointer(), b.as_pointer(),) }).v } == 3)); + assert!(({ ({ SImpl::operator_rem(&a.as_pointer(), b.as_pointer(),) }).v } == 1)); + assert!(({ ({ SImpl::operator_pos_6(&a.as_pointer(),) }).v } == 7)); + assert!(({ ({ SImpl::operator_neg_7(&a.as_pointer(),) }).v } == -7_i32)); + assert!((({ SImpl::operator_inc_8(&a.as_pointer(),) }).with(|__s: &S| __s.v) == 8)); + assert!(({ ({ SImpl::operator_post_inc_9(&a.as_pointer(), 0,) }).v } == 8)); + assert!(({ (*a.borrow()).v } == 9)); + assert!((({ SImpl::operator_dec_10(&a.as_pointer(),) }).with(|__s: &S| __s.v) == 8)); + assert!(({ ({ SImpl::operator_post_dec_11(&a.as_pointer(), 0,) }).v } == 8)); + assert!(({ (*a.borrow()).v } == 7)); assert!( - ((*({ SImpl::operator_add_1(&a.as_pointer(), b.as_pointer(),) }) - .v - .borrow()) + (({ SImpl::operator_inc_8(&({ SImpl::operator_inc_8(&a.as_pointer(),) }),) }) + .with(|__s: &S| __s.v) == 9) ); assert!( - ((*({ SImpl::operator_sub_2(&a.as_pointer(), b.as_pointer(),) }) - .v - .borrow()) - == 5) - ); - assert!( - ((*({ SImpl::operator_mul(&a.as_pointer(), b.as_pointer(),) }) - .v - .borrow()) - == 14) - ); - assert!( - ((*({ SImpl::operator_div(&a.as_pointer(), b.as_pointer(),) }) + ({ + ({ + let _o: Value = Rc::new(RefCell::new(S { v: 4 })); + SImpl::operator_add_1( + &Rc::new(RefCell::new(S { v: 3 })).as_pointer(), + _o.as_pointer(), + ) + }) .v - .borrow()) - == 3) - ); - assert!( - ((*({ SImpl::operator_rem(&a.as_pointer(), b.as_pointer(),) }) - .v - .borrow()) - == 1) - ); - assert!(((*({ SImpl::operator_pos_6(&a.as_pointer(),) }).v.borrow()) == 7)); - assert!(((*({ SImpl::operator_neg_7(&a.as_pointer(),) }).v.borrow()) == -7_i32)); - assert!( - ((*(*({ SImpl::operator_inc_8(&a.as_pointer(),) }) - .upgrade() - .deref()) - .v - .borrow()) - == 8) - ); - assert!( - ((*({ SImpl::operator_post_inc_9(&a.as_pointer(), 0,) }) - .v - .borrow()) - == 8) - ); - assert!(((*(*a.borrow()).v.borrow()) == 9)); - assert!( - ((*(*({ SImpl::operator_dec_10(&a.as_pointer(),) }) - .upgrade() - .deref()) - .v - .borrow()) - == 8) - ); - assert!( - ((*({ SImpl::operator_post_dec_11(&a.as_pointer(), 0,) }) - .v - .borrow()) - == 8) - ); - assert!(((*(*a.borrow()).v.borrow()) == 7)); - assert!( - ((*(*({ SImpl::operator_inc_8(&({ SImpl::operator_inc_8(&a.as_pointer(),) }),) }) - .upgrade() - .deref()) - .v - .borrow()) - == 9) - ); - assert!( - ((*({ - let _o: Value = Rc::new(RefCell::new(S { - v: Rc::new(RefCell::new(4)), - })); - SImpl::operator_add_1( - &Rc::new(RefCell::new(S { - v: Rc::new(RefCell::new(3)), - })) - .as_pointer(), - _o.as_pointer(), - ) - }) - .v - .borrow()) - == 7) + } == 7) ); return 0; } @@ -148,72 +79,72 @@ pub trait SImpl { impl SImpl for Ptr { fn operator_add_1(&self, o: Ptr) -> S { return S { - v: Rc::new(RefCell::new({ - let _lhs = (*(*(*self).upgrade().deref()).v.borrow()); - _lhs + (*(*o.upgrade().deref()).v.borrow()) - })), + v: { + let _lhs = (*self).with(|__s: &S| __s.v); + _lhs + o.with(|__s: &S| __s.v) + }, }; } fn operator_sub_2(&self, o: Ptr) -> S { return S { - v: Rc::new(RefCell::new({ - let _lhs = (*(*(*self).upgrade().deref()).v.borrow()); - _lhs - (*(*o.upgrade().deref()).v.borrow()) - })), + v: { + let _lhs = (*self).with(|__s: &S| __s.v); + _lhs - o.with(|__s: &S| __s.v) + }, }; } fn operator_mul(&self, o: Ptr) -> S { return S { - v: Rc::new(RefCell::new({ - let _lhs = (*(*(*self).upgrade().deref()).v.borrow()); - _lhs * (*(*o.upgrade().deref()).v.borrow()) - })), + v: { + let _lhs = (*self).with(|__s: &S| __s.v); + _lhs * o.with(|__s: &S| __s.v) + }, }; } fn operator_div(&self, o: Ptr) -> S { return S { - v: Rc::new(RefCell::new({ - let _lhs = (*(*(*self).upgrade().deref()).v.borrow()); - _lhs / (*(*o.upgrade().deref()).v.borrow()) - })), + v: { + let _lhs = (*self).with(|__s: &S| __s.v); + _lhs / o.with(|__s: &S| __s.v) + }, }; } fn operator_rem(&self, o: Ptr) -> S { return S { - v: Rc::new(RefCell::new({ - let _lhs = (*(*(*self).upgrade().deref()).v.borrow()); - _lhs % (*(*o.upgrade().deref()).v.borrow()) - })), + v: { + let _lhs = (*self).with(|__s: &S| __s.v); + _lhs % o.with(|__s: &S| __s.v) + }, }; } fn operator_pos_6(&self) -> S { return S { - v: Rc::new(RefCell::new((*(*(*self).upgrade().deref()).v.borrow()))), + v: { (*self).with(|__s: &S| __s.v) }, }; } fn operator_neg_7(&self) -> S { return S { - v: Rc::new(RefCell::new(-(*(*(*self).upgrade().deref()).v.borrow()))), + v: { -(*self).with(|__s: &S| __s.v) }, }; } fn operator_inc_8(&self) -> Ptr { - (*(*(*self).upgrade().deref()).v.borrow_mut()).prefix_inc(); + (*self).with_mut(|__s: &mut S| __s.v.prefix_inc()); return (*self).clone(); } fn operator_post_inc_9(&self, _a0: i32) -> S { let _a0: Value = Rc::new(RefCell::new(_a0)); - let old: Value = Rc::new(RefCell::new((*(*self).upgrade().deref()).clone())); - (*(*(*self).upgrade().deref()).v.borrow_mut()).prefix_inc(); + let old: Value = Rc::new(RefCell::new((*self).read())); + (*self).with_mut(|__s: &mut S| __s.v.prefix_inc()); return (*old.borrow()).clone(); } fn operator_dec_10(&self) -> Ptr { - (*(*(*self).upgrade().deref()).v.borrow_mut()).prefix_dec(); + (*self).with_mut(|__s: &mut S| __s.v.prefix_dec()); return (*self).clone(); } fn operator_post_dec_11(&self, _a0: i32) -> S { let _a0: Value = Rc::new(RefCell::new(_a0)); - let old: Value = Rc::new(RefCell::new((*(*self).upgrade().deref()).clone())); - (*(*(*self).upgrade().deref()).v.borrow_mut()).prefix_dec(); + let old: Value = Rc::new(RefCell::new((*self).read())); + (*self).with_mut(|__s: &mut S| __s.v.prefix_dec()); return (*old.borrow()).clone(); } } diff --git a/tests/unit/out/refcount/operator_bitwise_free.rs b/tests/unit/out/refcount/operator_bitwise_free.rs index d480b6dd68..2cb4b31ea4 100644 --- a/tests/unit/out/refcount/operator_bitwise_free.rs +++ b/tests/unit/out/refcount/operator_bitwise_free.rs @@ -6,77 +6,69 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct S { - pub v: Value, -} -impl Clone for S { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*self.v.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub v: u32, } impl ByteRepr for S { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.v.borrow()).to_bytes(&mut buf[0..4]); + self.v.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - v: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + v: ::from_bytes(&buf[0..4]), } } } pub fn operator_bitnot_0(a: Ptr) -> S { return S { - v: Rc::new(RefCell::new(!(*(*a.upgrade().deref()).v.borrow()))), + v: { !a.with(|__s: &S| __s.v) }, }; } pub fn operator_bitand_1(a: Ptr, b: Ptr) -> S { return S { - v: Rc::new(RefCell::new({ - let _lhs = (*(*a.upgrade().deref()).v.borrow()); - _lhs & (*(*b.upgrade().deref()).v.borrow()) - })), + v: { + let _lhs = a.with(|__s: &S| __s.v); + _lhs & b.with(|__s: &S| __s.v) + }, }; } pub fn operator_bitor_2(a: Ptr, b: Ptr) -> S { return S { - v: Rc::new(RefCell::new({ - let _lhs = (*(*a.upgrade().deref()).v.borrow()); - _lhs | (*(*b.upgrade().deref()).v.borrow()) - })), + v: { + let _lhs = a.with(|__s: &S| __s.v); + _lhs | b.with(|__s: &S| __s.v) + }, }; } pub fn operator_bitxor_3(a: Ptr, b: Ptr) -> S { return S { - v: Rc::new(RefCell::new({ - let _lhs = (*(*a.upgrade().deref()).v.borrow()); - _lhs ^ (*(*b.upgrade().deref()).v.borrow()) - })), + v: { + let _lhs = a.with(|__s: &S| __s.v); + _lhs ^ b.with(|__s: &S| __s.v) + }, }; } pub fn operator_shl_4(a: Ptr, n: i32) -> S { let n: Value = Rc::new(RefCell::new(n)); return S { - v: Rc::new(RefCell::new({ - let _lhs = (*(*a.upgrade().deref()).v.borrow()); + v: { + let _lhs = a.with(|__s: &S| __s.v); _lhs << (*n.borrow()) - })), + }, }; } pub fn operator_shr_5(a: Ptr, n: i32) -> S { let n: Value = Rc::new(RefCell::new(n)); return S { - v: Rc::new(RefCell::new({ - let _lhs = (*(*a.upgrade().deref()).v.borrow()); + v: { + let _lhs = a.with(|__s: &S| __s.v); _lhs >> (*n.borrow()) - })), + }, }; } pub fn main() { @@ -84,65 +76,61 @@ pub fn main() { std::process::exit(main_0()); } fn main_0() -> i32 { - let a: Value = Rc::new(RefCell::new(S { - v: Rc::new(RefCell::new(12_u32)), - })); - let b: Value = Rc::new(RefCell::new(S { - v: Rc::new(RefCell::new(10_u32)), - })); + let a: Value = Rc::new(RefCell::new(S { v: 12_u32 })); + let b: Value = Rc::new(RefCell::new(S { v: 10_u32 })); assert!( - ((*({ - let _a: Ptr = a.as_pointer(); - operator_bitnot_0(_a) - }) - .v - .borrow()) - == !12_u32) + ({ + ({ + let _a: Ptr = a.as_pointer(); + operator_bitnot_0(_a) + }) + .v + } == !12_u32) ); assert!( - ((*({ - let _a: Ptr = a.as_pointer(); - operator_bitand_1(_a, b.as_pointer()) - }) - .v - .borrow()) - == 8_u32) + ({ + ({ + let _a: Ptr = a.as_pointer(); + operator_bitand_1(_a, b.as_pointer()) + }) + .v + } == 8_u32) ); assert!( - ((*({ - let _a: Ptr = a.as_pointer(); - operator_bitor_2(_a, b.as_pointer()) - }) - .v - .borrow()) - == 14_u32) + ({ + ({ + let _a: Ptr = a.as_pointer(); + operator_bitor_2(_a, b.as_pointer()) + }) + .v + } == 14_u32) ); assert!( - ((*({ - let _a: Ptr = a.as_pointer(); - operator_bitxor_3(_a, b.as_pointer()) - }) - .v - .borrow()) - == 6_u32) + ({ + ({ + let _a: Ptr = a.as_pointer(); + operator_bitxor_3(_a, b.as_pointer()) + }) + .v + } == 6_u32) ); assert!( - ((*({ - let _a: Ptr = a.as_pointer(); - operator_shl_4(_a, 2) - }) - .v - .borrow()) - == 48_u32) + ({ + ({ + let _a: Ptr = a.as_pointer(); + operator_shl_4(_a, 2) + }) + .v + } == 48_u32) ); assert!( - ((*({ - let _a: Ptr = a.as_pointer(); - operator_shr_5(_a, 2) - }) - .v - .borrow()) - == 3_u32) + ({ + ({ + let _a: Ptr = a.as_pointer(); + operator_shr_5(_a, 2) + }) + .v + } == 3_u32) ); return 0; } diff --git a/tests/unit/out/refcount/operator_bitwise_member.rs b/tests/unit/out/refcount/operator_bitwise_member.rs index dd440b11fb..3568e230db 100644 --- a/tests/unit/out/refcount/operator_bitwise_member.rs +++ b/tests/unit/out/refcount/operator_bitwise_member.rs @@ -6,29 +6,21 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct S { - pub v: Value, -} -impl Clone for S { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*self.v.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub v: u32, } impl ByteRepr for S { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.v.borrow()).to_bytes(&mut buf[0..4]); + self.v.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - v: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + v: ::from_bytes(&buf[0..4]), } } } @@ -37,33 +29,14 @@ pub fn main() { std::process::exit(main_0()); } fn main_0() -> i32 { - let a: Value = Rc::new(RefCell::new(S { - v: Rc::new(RefCell::new(12_u32)), - })); - let b: Value = Rc::new(RefCell::new(S { - v: Rc::new(RefCell::new(10_u32)), - })); - assert!(((*({ SImpl::operator_bitnot(&a.as_pointer(),) }).v.borrow()) == !12_u32)); - assert!( - ((*({ SImpl::operator_bitand(&a.as_pointer(), b.as_pointer(),) }) - .v - .borrow()) - == 8_u32) - ); - assert!( - ((*({ SImpl::operator_bitor(&a.as_pointer(), b.as_pointer(),) }) - .v - .borrow()) - == 14_u32) - ); - assert!( - ((*({ SImpl::operator_bitxor(&a.as_pointer(), b.as_pointer(),) }) - .v - .borrow()) - == 6_u32) - ); - assert!(((*({ SImpl::operator_shl(&a.as_pointer(), 2,) }).v.borrow()) == 48_u32)); - assert!(((*({ SImpl::operator_shr(&a.as_pointer(), 2,) }).v.borrow()) == 3_u32)); + let a: Value = Rc::new(RefCell::new(S { v: 12_u32 })); + let b: Value = Rc::new(RefCell::new(S { v: 10_u32 })); + assert!(({ ({ SImpl::operator_bitnot(&a.as_pointer(),) }).v } == !12_u32)); + assert!(({ ({ SImpl::operator_bitand(&a.as_pointer(), b.as_pointer(),) }).v } == 8_u32)); + assert!(({ ({ SImpl::operator_bitor(&a.as_pointer(), b.as_pointer(),) }).v } == 14_u32)); + assert!(({ ({ SImpl::operator_bitxor(&a.as_pointer(), b.as_pointer(),) }).v } == 6_u32)); + assert!(({ ({ SImpl::operator_shl(&a.as_pointer(), 2,) }).v } == 48_u32)); + assert!(({ ({ SImpl::operator_shr(&a.as_pointer(), 2,) }).v } == 3_u32)); return 0; } pub trait SImpl { @@ -77,47 +50,43 @@ pub trait SImpl { impl SImpl for Ptr { fn operator_bitnot(&self) -> S { return S { - v: Rc::new(RefCell::new(!(*(*(*self).upgrade().deref()).v.borrow()))), + v: { !(*self).with(|__s: &S| __s.v) }, }; } fn operator_bitand(&self, o: Ptr) -> S { return S { - v: Rc::new(RefCell::new({ - let _lhs = (*(*(*self).upgrade().deref()).v.borrow()); - _lhs & (*(*o.upgrade().deref()).v.borrow()) - })), + v: { + let _lhs = (*self).with(|__s: &S| __s.v); + _lhs & o.with(|__s: &S| __s.v) + }, }; } fn operator_bitor(&self, o: Ptr) -> S { return S { - v: Rc::new(RefCell::new({ - let _lhs = (*(*(*self).upgrade().deref()).v.borrow()); - _lhs | (*(*o.upgrade().deref()).v.borrow()) - })), + v: { + let _lhs = (*self).with(|__s: &S| __s.v); + _lhs | o.with(|__s: &S| __s.v) + }, }; } fn operator_bitxor(&self, o: Ptr) -> S { return S { - v: Rc::new(RefCell::new({ - let _lhs = (*(*(*self).upgrade().deref()).v.borrow()); - _lhs ^ (*(*o.upgrade().deref()).v.borrow()) - })), + v: { + let _lhs = (*self).with(|__s: &S| __s.v); + _lhs ^ o.with(|__s: &S| __s.v) + }, }; } fn operator_shl(&self, n: i32) -> S { let n: Value = Rc::new(RefCell::new(n)); return S { - v: Rc::new(RefCell::new( - ((*(*(*self).upgrade().deref()).v.borrow()) << (*n.borrow())), - )), + v: { ((*self).with(|__s: &S| __s.v) << (*n.borrow())) }, }; } fn operator_shr(&self, n: i32) -> S { let n: Value = Rc::new(RefCell::new(n)); return S { - v: Rc::new(RefCell::new( - ((*(*(*self).upgrade().deref()).v.borrow()) >> (*n.borrow())), - )), + v: { ((*self).with(|__s: &S| __s.v) >> (*n.borrow())) }, }; } } diff --git a/tests/unit/out/refcount/operator_comparison_defaulted.rs b/tests/unit/out/refcount/operator_comparison_defaulted.rs index f0feb1992c..2f529d3002 100644 --- a/tests/unit/out/refcount/operator_comparison_defaulted.rs +++ b/tests/unit/out/refcount/operator_comparison_defaulted.rs @@ -6,10 +6,12 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Eq { - pub a: Value, - pub b: Value, + #[offset(0)] + pub a: i32, + #[offset(4)] + pub b: i32, } impl std::cmp::PartialEq for Eq { fn eq(&self, other: &Self) -> bool { @@ -30,35 +32,27 @@ impl std::cmp::PartialEq for Eq { } } impl std::cmp::Eq for Eq {} -impl Clone for Eq { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - a: Rc::new(RefCell::new((*self.a.borrow()))), - b: Rc::new(RefCell::new((*self.b.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } -} impl ByteRepr for Eq { fn byte_size() -> usize { 8 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.a.borrow()).to_bytes(&mut buf[0..4]); - (*self.b.borrow()).to_bytes(&mut buf[4..8]); + self.a.to_bytes(&mut buf[0..4]); + self.b.to_bytes(&mut buf[4..8]); } fn from_bytes(buf: &[u8]) -> Self { Self { - a: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - b: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + a: ::from_bytes(&buf[0..4]), + b: ::from_bytes(&buf[4..8]), } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Cmp { - pub a: Value, - pub b: Value, + #[offset(0)] + pub a: i32, + #[offset(4)] + pub b: i32, } impl std::cmp::Ord for Cmp { fn cmp(&self, other: &Self) -> std::cmp::Ordering { @@ -102,34 +96,25 @@ impl std::cmp::PartialEq for Cmp { } } impl std::cmp::Eq for Cmp {} -impl Clone for Cmp { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - a: Rc::new(RefCell::new((*self.a.borrow()))), - b: Rc::new(RefCell::new((*self.b.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } -} impl ByteRepr for Cmp { fn byte_size() -> usize { 8 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.a.borrow()).to_bytes(&mut buf[0..4]); - (*self.b.borrow()).to_bytes(&mut buf[4..8]); + self.a.to_bytes(&mut buf[0..4]); + self.b.to_bytes(&mut buf[4..8]); } fn from_bytes(buf: &[u8]) -> Self { Self { - a: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - b: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + a: ::from_bytes(&buf[0..4]), + b: ::from_bytes(&buf[4..8]), } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Both { - pub a: Value, + #[offset(0)] + pub a: i32, } impl std::cmp::Ord for Both { fn cmp(&self, other: &Self) -> std::cmp::Ordering { @@ -157,31 +142,23 @@ impl std::cmp::PartialEq for Both { } } impl std::cmp::Eq for Both {} -impl Clone for Both { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - a: Rc::new(RefCell::new((*self.a.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } -} impl ByteRepr for Both { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.a.borrow()).to_bytes(&mut buf[0..4]); + self.a.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - a: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + a: ::from_bytes(&buf[0..4]), } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct OrdOnly { - pub a: Value, + #[offset(0)] + pub a: i32, } impl std::cmp::Ord for OrdOnly { fn cmp(&self, other: &Self) -> std::cmp::Ordering { @@ -209,31 +186,23 @@ impl std::cmp::PartialEq for OrdOnly { } } impl std::cmp::Eq for OrdOnly {} -impl Clone for OrdOnly { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - a: Rc::new(RefCell::new((*self.a.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } -} impl ByteRepr for OrdOnly { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.a.borrow()).to_bytes(&mut buf[0..4]); + self.a.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - a: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + a: ::from_bytes(&buf[0..4]), } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Inner { - pub x: Value, + #[offset(0)] + pub x: i32, } impl std::cmp::Ord for Inner { fn cmp(&self, other: &Self) -> std::cmp::Ordering { @@ -261,44 +230,41 @@ impl std::cmp::PartialEq for Inner { } } impl std::cmp::Eq for Inner {} -impl Clone for Inner { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - x: Rc::new(RefCell::new((*self.x.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } -} impl ByteRepr for Inner { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.x.borrow()).to_bytes(&mut buf[0..4]); + self.x.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - x: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + x: ::from_bytes(&buf[0..4]), } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Outer { - pub i: Value, - pub y: Value, + #[offset(0)] + pub i: Inner, + #[offset(4)] + pub y: i32, } impl std::cmp::Ord for Outer { fn cmp(&self, other: &Self) -> std::cmp::Ordering { { OuterImpl::operator_cmp( &Rc::new(RefCell::new(Outer { - i: self.i.clone(), + i: Inner { + x: self.i.x.clone(), + }, y: self.y.clone(), })) .as_pointer(), Rc::new(RefCell::new(Outer { - i: other.i.clone(), + i: Inner { + x: other.i.x.clone(), + }, y: other.y.clone(), })) .as_pointer(), @@ -316,12 +282,16 @@ impl std::cmp::PartialEq for Outer { { OuterImpl::operator_cmp( &Rc::new(RefCell::new(Outer { - i: self.i.clone(), + i: Inner { + x: self.i.x.clone(), + }, y: self.y.clone(), })) .as_pointer(), Rc::new(RefCell::new(Outer { - i: other.i.clone(), + i: Inner { + x: other.i.x.clone(), + }, y: other.y.clone(), })) .as_pointer(), @@ -330,34 +300,25 @@ impl std::cmp::PartialEq for Outer { } } impl std::cmp::Eq for Outer {} -impl Clone for Outer { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - i: Rc::new(RefCell::new((*self.i.borrow()).clone())), - y: Rc::new(RefCell::new((*self.y.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } -} impl ByteRepr for Outer { fn byte_size() -> usize { 8 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.i.borrow()).to_bytes(&mut buf[0..4]); - (*self.y.borrow()).to_bytes(&mut buf[4..8]); + self.i.to_bytes(&mut buf[0..4]); + self.y.to_bytes(&mut buf[4..8]); } fn from_bytes(buf: &[u8]) -> Self { Self { - i: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - y: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + i: ::from_bytes(&buf[0..4]), + y: ::from_bytes(&buf[4..8]), } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Secondary { - pub a: Value, + #[offset(0)] + pub a: i32, } impl std::cmp::Ord for Secondary { fn cmp(&self, other: &Self) -> std::cmp::Ordering { @@ -385,31 +346,23 @@ impl std::cmp::PartialEq for Secondary { } } impl std::cmp::Eq for Secondary {} -impl Clone for Secondary { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - a: Rc::new(RefCell::new((*self.a.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } -} impl ByteRepr for Secondary { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.a.borrow()).to_bytes(&mut buf[0..4]); + self.a.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - a: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + a: ::from_bytes(&buf[0..4]), } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct PtrMember { - pub p: Value>, + #[offset(0)] + pub p: Ptr, } impl std::cmp::Ord for PtrMember { fn cmp(&self, other: &Self) -> std::cmp::Ordering { @@ -437,25 +390,16 @@ impl std::cmp::PartialEq for PtrMember { } } impl std::cmp::Eq for PtrMember {} -impl Clone for PtrMember { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - p: Rc::new(RefCell::new((*self.p.borrow()).clone())), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } -} impl ByteRepr for PtrMember { fn byte_size() -> usize { 8 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.p.borrow()).to_bytes(&mut buf[0..8]); + self.p.to_bytes(&mut buf[0..8]); } fn from_bytes(buf: &[u8]) -> Self { Self { - p: Rc::new(RefCell::new(>::from_bytes(&buf[0..8]))), + p: >::from_bytes(&buf[0..8]), } } } @@ -464,36 +408,15 @@ pub fn main() { std::process::exit(main_0()); } fn main_0() -> i32 { - let e1: Value = Rc::new(RefCell::new(Eq { - a: Rc::new(RefCell::new(1)), - b: Rc::new(RefCell::new(2)), - })); - let e2: Value = Rc::new(RefCell::new(Eq { - a: Rc::new(RefCell::new(1)), - b: Rc::new(RefCell::new(2)), - })); - let e3: Value = Rc::new(RefCell::new(Eq { - a: Rc::new(RefCell::new(1)), - b: Rc::new(RefCell::new(3)), - })); + let e1: Value = Rc::new(RefCell::new(Eq { a: 1, b: 2 })); + let e2: Value = Rc::new(RefCell::new(Eq { a: 1, b: 2 })); + let e3: Value = Rc::new(RefCell::new(Eq { a: 1, b: 3 })); assert!(({ EqImpl::operator_eq(&e1.as_pointer(), e2.as_pointer(),) })); assert!(!({ EqImpl::operator_eq(&e1.as_pointer(), e3.as_pointer(),) })); - let c1: Value = Rc::new(RefCell::new(Cmp { - a: Rc::new(RefCell::new(1)), - b: Rc::new(RefCell::new(2)), - })); - let c2: Value = Rc::new(RefCell::new(Cmp { - a: Rc::new(RefCell::new(1)), - b: Rc::new(RefCell::new(3)), - })); - let c3: Value = Rc::new(RefCell::new(Cmp { - a: Rc::new(RefCell::new(2)), - b: Rc::new(RefCell::new(0)), - })); - let c4: Value = Rc::new(RefCell::new(Cmp { - a: Rc::new(RefCell::new(1)), - b: Rc::new(RefCell::new(9)), - })); + let c1: Value = Rc::new(RefCell::new(Cmp { a: 1, b: 2 })); + let c2: Value = Rc::new(RefCell::new(Cmp { a: 1, b: 3 })); + let c3: Value = Rc::new(RefCell::new(Cmp { a: 2, b: 0 })); + let c4: Value = Rc::new(RefCell::new(Cmp { a: 1, b: 9 })); assert!( ({ CmpImpl::operator_cmp(&c1.as_pointer(), c2.as_pointer(),) }) == std::cmp::Ordering::Less ); @@ -510,12 +433,8 @@ fn main_0() -> i32 { assert!( ({ CmpImpl::operator_cmp(&c1.as_pointer(), c2.as_pointer(),) }) == std::cmp::Ordering::Less ); - let b1: Value = Rc::new(RefCell::new(Both { - a: Rc::new(RefCell::new(1)), - })); - let b2: Value = Rc::new(RefCell::new(Both { - a: Rc::new(RefCell::new(2)), - })); + let b1: Value = Rc::new(RefCell::new(Both { a: 1 })); + let b2: Value = Rc::new(RefCell::new(Both { a: 2 })); assert!( ({ BothImpl::operator_cmp(&b1.as_pointer(), b2.as_pointer(),) }) == std::cmp::Ordering::Less @@ -526,12 +445,8 @@ fn main_0() -> i32 { BothImpl::operator_eq(&b2.as_pointer(), _arg0) }) ); - let o1: Value = Rc::new(RefCell::new(OrdOnly { - a: Rc::new(RefCell::new(1)), - })); - let o2: Value = Rc::new(RefCell::new(OrdOnly { - a: Rc::new(RefCell::new(2)), - })); + let o1: Value = Rc::new(RefCell::new(OrdOnly { a: 1 })); + let o2: Value = Rc::new(RefCell::new(OrdOnly { a: 2 })); assert!( ({ OrdOnlyImpl::operator_cmp(&o1.as_pointer(), o2.as_pointer(),) }) == std::cmp::Ordering::Less @@ -541,22 +456,16 @@ fn main_0() -> i32 { == std::cmp::Ordering::Greater ); let x1: Value = Rc::new(RefCell::new(Outer { - i: Rc::new(RefCell::new(Inner { - x: Rc::new(RefCell::new(1)), - })), - y: Rc::new(RefCell::new(9)), + i: Inner { x: 1 }, + y: 9, })); let x2: Value = Rc::new(RefCell::new(Outer { - i: Rc::new(RefCell::new(Inner { - x: Rc::new(RefCell::new(2)), - })), - y: Rc::new(RefCell::new(0)), + i: Inner { x: 2 }, + y: 0, })); let x3: Value = Rc::new(RefCell::new(Outer { - i: Rc::new(RefCell::new(Inner { - x: Rc::new(RefCell::new(1)), - })), - y: Rc::new(RefCell::new(9)), + i: Inner { x: 1 }, + y: 9, })); assert!( ({ OuterImpl::operator_cmp(&x1.as_pointer(), x2.as_pointer(),) }) @@ -567,27 +476,21 @@ fn main_0() -> i32 { ({ OuterImpl::operator_cmp(&x2.as_pointer(), x1.as_pointer(),) }) == std::cmp::Ordering::Greater ); - let s1: Value = Rc::new(RefCell::new(Secondary { - a: Rc::new(RefCell::new(1)), - })); - let s2: Value = Rc::new(RefCell::new(Secondary { - a: Rc::new(RefCell::new(2)), - })); + let s1: Value = Rc::new(RefCell::new(Secondary { a: 1 })); + let s2: Value = Rc::new(RefCell::new(Secondary { a: 2 })); assert!(({ SecondaryImpl::operator_ne(&s1.as_pointer(), s2.as_pointer(),) })); assert!(({ SecondaryImpl::operator_lt(&s1.as_pointer(), s2.as_pointer(),) })); assert!(({ SecondaryImpl::operator_ge(&s2.as_pointer(), s1.as_pointer(),) })); assert!(!({ SecondaryImpl::operator_lt(&s2.as_pointer(), s1.as_pointer(),) })); let arr: Value> = Rc::new(RefCell::new(Box::new([0, 0]))); let p1: Value = Rc::new(RefCell::new(PtrMember { - p: Rc::new(RefCell::new((arr.as_pointer() as Ptr))), + p: (arr.as_pointer() as Ptr), })); let p2: Value = Rc::new(RefCell::new(PtrMember { - p: Rc::new(RefCell::new( - (arr.as_pointer() as Ptr).offset((1) as isize), - )), + p: (arr.as_pointer() as Ptr).offset((1) as isize), })); let p3: Value = Rc::new(RefCell::new(PtrMember { - p: Rc::new(RefCell::new((arr.as_pointer() as Ptr))), + p: (arr.as_pointer() as Ptr), })); assert!( ({ PtrMemberImpl::operator_cmp(&p1.as_pointer(), p2.as_pointer(),) }) @@ -607,15 +510,15 @@ pub trait BothImpl { impl BothImpl for Ptr { fn operator_eq(&self, _a0: Ptr) -> bool { return { - let _lhs = (*(*(*self).upgrade().deref()).a.borrow()); - _lhs == (*(*_a0.upgrade().deref()).a.borrow()) + let _lhs = (*self).with(|__s: &Both| __s.a); + _lhs == _a0.with(|__s: &Both| __s.a) }; } fn operator_cmp(&self, _a0: Ptr) -> std::cmp::Ordering { { let cmp: Value = Rc::new(RefCell::new(std::cmp::Ord::cmp( - &(*(*(*self).upgrade().deref()).a.borrow()), - &(*(*_a0.upgrade().deref()).a.borrow()), + &((*self).with(|__s: &Both| __s.a)), + &(_a0.with(|__s: &Both| __s.a)), ))); if !((*cmp.borrow()) == std::cmp::Ordering::Equal) { return (*cmp.borrow_mut()).clone(); @@ -632,8 +535,8 @@ impl CmpImpl for Ptr { fn operator_cmp(&self, _a0: Ptr) -> std::cmp::Ordering { { let cmp: Value = Rc::new(RefCell::new(std::cmp::Ord::cmp( - &(*(*(*self).upgrade().deref()).a.borrow()), - &(*(*_a0.upgrade().deref()).a.borrow()), + &((*self).with(|__s: &Cmp| __s.a)), + &(_a0.with(|__s: &Cmp| __s.a)), ))); if !((*cmp.borrow()) == std::cmp::Ordering::Equal) { return (*cmp.borrow_mut()).clone(); @@ -641,8 +544,8 @@ impl CmpImpl for Ptr { } { let cmp: Value = Rc::new(RefCell::new(std::cmp::Ord::cmp( - &(*(*(*self).upgrade().deref()).b.borrow()), - &(*(*_a0.upgrade().deref()).b.borrow()), + &((*self).with(|__s: &Cmp| __s.b)), + &(_a0.with(|__s: &Cmp| __s.b)), ))); if !((*cmp.borrow()) == std::cmp::Ordering::Equal) { return (*cmp.borrow_mut()).clone(); @@ -652,11 +555,11 @@ impl CmpImpl for Ptr { } fn operator_eq(&self, _a0: Ptr) -> bool { return ({ - let _lhs = (*(*(*self).upgrade().deref()).a.borrow()); - _lhs == (*(*_a0.upgrade().deref()).a.borrow()) + let _lhs = (*self).with(|__s: &Cmp| __s.a); + _lhs == _a0.with(|__s: &Cmp| __s.a) }) && ({ - let _lhs = (*(*(*self).upgrade().deref()).b.borrow()); - _lhs == (*(*_a0.upgrade().deref()).b.borrow()) + let _lhs = (*self).with(|__s: &Cmp| __s.b); + _lhs == _a0.with(|__s: &Cmp| __s.b) }); } } @@ -666,11 +569,11 @@ pub trait EqImpl { impl EqImpl for Ptr { fn operator_eq(&self, _a0: Ptr) -> bool { return ({ - let _lhs = (*(*(*self).upgrade().deref()).a.borrow()); - _lhs == (*(*_a0.upgrade().deref()).a.borrow()) + let _lhs = (*self).with(|__s: &Eq| __s.a); + _lhs == _a0.with(|__s: &Eq| __s.a) }) && ({ - let _lhs = (*(*(*self).upgrade().deref()).b.borrow()); - _lhs == (*(*_a0.upgrade().deref()).b.borrow()) + let _lhs = (*self).with(|__s: &Eq| __s.b); + _lhs == _a0.with(|__s: &Eq| __s.b) }); } } @@ -682,8 +585,8 @@ impl InnerImpl for Ptr { fn operator_cmp(&self, _a0: Ptr) -> std::cmp::Ordering { { let cmp: Value = Rc::new(RefCell::new(std::cmp::Ord::cmp( - &(*(*(*self).upgrade().deref()).x.borrow()), - &(*(*_a0.upgrade().deref()).x.borrow()), + &((*self).with(|__s: &Inner| __s.x)), + &(_a0.with(|__s: &Inner| __s.x)), ))); if !((*cmp.borrow()) == std::cmp::Ordering::Equal) { return (*cmp.borrow_mut()).clone(); @@ -693,8 +596,8 @@ impl InnerImpl for Ptr { } fn operator_eq(&self, _a0: Ptr) -> bool { return { - let _lhs = (*(*(*self).upgrade().deref()).x.borrow()); - _lhs == (*(*_a0.upgrade().deref()).x.borrow()) + let _lhs = (*self).with(|__s: &Inner| __s.x); + _lhs == _a0.with(|__s: &Inner| __s.x) }; } } @@ -705,8 +608,8 @@ impl OrdOnlyImpl for Ptr { fn operator_cmp(&self, _a0: Ptr) -> std::cmp::Ordering { { let cmp: Value = Rc::new(RefCell::new(std::cmp::Ord::cmp( - &(*(*(*self).upgrade().deref()).a.borrow()), - &(*(*_a0.upgrade().deref()).a.borrow()), + &((*self).with(|__s: &OrdOnly| __s.a)), + &(_a0.with(|__s: &OrdOnly| __s.a)), ))); if !((*cmp.borrow()) == std::cmp::Ordering::Equal) { return (*cmp.borrow_mut()).clone(); @@ -724,8 +627,8 @@ impl OuterImpl for Ptr { { let cmp: Value = Rc::new(RefCell::new( ({ - let _arg0: Ptr = (*_a0.upgrade().deref()).i.as_pointer(); - InnerImpl::operator_cmp(&(*(*self).upgrade().deref()).i.as_pointer(), _arg0) + let _arg0: Ptr = field_ptr!(_a0, i); + InnerImpl::operator_cmp(&{ field_ptr!((*self), i) }, _arg0) }), )); if !((*cmp.borrow()) == std::cmp::Ordering::Equal) { @@ -734,8 +637,8 @@ impl OuterImpl for Ptr { } { let cmp: Value = Rc::new(RefCell::new(std::cmp::Ord::cmp( - &(*(*(*self).upgrade().deref()).y.borrow()), - &(*(*_a0.upgrade().deref()).y.borrow()), + &((*self).with(|__s: &Outer| __s.y)), + &(_a0.with(|__s: &Outer| __s.y)), ))); if !((*cmp.borrow()) == std::cmp::Ordering::Equal) { return (*cmp.borrow_mut()).clone(); @@ -745,11 +648,11 @@ impl OuterImpl for Ptr { } fn operator_eq(&self, _a0: Ptr) -> bool { return ({ - let _arg0: Ptr = (*_a0.upgrade().deref()).i.as_pointer(); - InnerImpl::operator_eq(&(*(*self).upgrade().deref()).i.as_pointer(), _arg0) + let _arg0: Ptr = field_ptr!(_a0, i); + InnerImpl::operator_eq(&{ field_ptr!((*self), i) }, _arg0) }) && ({ - let _lhs = (*(*(*self).upgrade().deref()).y.borrow()); - _lhs == (*(*_a0.upgrade().deref()).y.borrow()) + let _lhs = (*self).with(|__s: &Outer| __s.y); + _lhs == _a0.with(|__s: &Outer| __s.y) }); } } @@ -761,8 +664,8 @@ impl PtrMemberImpl for Ptr { fn operator_cmp(&self, _a0: Ptr) -> std::cmp::Ordering { { let cmp: Value = Rc::new(RefCell::new(std::cmp::Ord::cmp( - &(*(*(*self).upgrade().deref()).p.borrow()), - &(*(*_a0.upgrade().deref()).p.borrow()), + &((*self).with(|__s: &PtrMember| (__s.p).clone())), + &(_a0.with(|__s: &PtrMember| (__s.p).clone())), ))); if !((*cmp.borrow()) == std::cmp::Ordering::Equal) { return (*cmp.borrow_mut()).clone(); @@ -772,8 +675,8 @@ impl PtrMemberImpl for Ptr { } fn operator_eq(&self, _a0: Ptr) -> bool { return { - let _lhs = (*(*(*self).upgrade().deref()).p.borrow()).clone(); - _lhs == (*(*_a0.upgrade().deref()).p.borrow()).clone() + let _lhs = (*self).with(|__s: &PtrMember| (__s.p).clone()); + _lhs == _a0.with(|__s: &PtrMember| (__s.p).clone()) }; } } @@ -787,8 +690,8 @@ pub trait SecondaryImpl { impl SecondaryImpl for Ptr { fn operator_eq(&self, _a0: Ptr) -> bool { return { - let _lhs = (*(*(*self).upgrade().deref()).a.borrow()); - _lhs == (*(*_a0.upgrade().deref()).a.borrow()) + let _lhs = (*self).with(|__s: &Secondary| __s.a); + _lhs == _a0.with(|__s: &Secondary| __s.a) }; } fn operator_ne(&self, _a0: Ptr) -> bool { @@ -800,8 +703,8 @@ impl SecondaryImpl for Ptr { fn operator_cmp(&self, _a0: Ptr) -> std::cmp::Ordering { { let cmp: Value = Rc::new(RefCell::new(std::cmp::Ord::cmp( - &(*(*(*self).upgrade().deref()).a.borrow()), - &(*(*_a0.upgrade().deref()).a.borrow()), + &((*self).with(|__s: &Secondary| __s.a)), + &(_a0.with(|__s: &Secondary| __s.a)), ))); if !((*cmp.borrow()) == std::cmp::Ordering::Equal) { return (*cmp.borrow_mut()).clone(); diff --git a/tests/unit/out/refcount/operator_comparison_free.rs b/tests/unit/out/refcount/operator_comparison_free.rs index 38c9b8084e..2e1e1a2be3 100644 --- a/tests/unit/out/refcount/operator_comparison_free.rs +++ b/tests/unit/out/refcount/operator_comparison_free.rs @@ -6,9 +6,10 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct S { - pub v: Value, + #[offset(0)] + pub v: i32, } impl std::cmp::Ord for S { fn cmp(&self, other: &Self) -> std::cmp::Ordering { @@ -45,68 +46,59 @@ impl std::cmp::PartialEq for S { } } impl std::cmp::Eq for S {} -impl Clone for S { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*self.v.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } -} impl ByteRepr for S { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.v.borrow()).to_bytes(&mut buf[0..4]); + self.v.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - v: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + v: ::from_bytes(&buf[0..4]), } } } pub fn operator_eq_1(a: Ptr, b: Ptr) -> bool { return { - let _lhs = (*(*a.upgrade().deref()).v.borrow()); - _lhs == (*(*b.upgrade().deref()).v.borrow()) + let _lhs = a.with(|__s: &S| __s.v); + _lhs == b.with(|__s: &S| __s.v) }; } pub fn operator_ne_2(a: Ptr, b: Ptr) -> bool { return { - let _lhs = (*(*a.upgrade().deref()).v.borrow()); - _lhs != (*(*b.upgrade().deref()).v.borrow()) + let _lhs = a.with(|__s: &S| __s.v); + _lhs != b.with(|__s: &S| __s.v) }; } pub fn operator_lt_0(a: Ptr, b: Ptr) -> bool { return { - let _lhs = (*(*a.upgrade().deref()).v.borrow()); - _lhs < (*(*b.upgrade().deref()).v.borrow()) + let _lhs = a.with(|__s: &S| __s.v); + _lhs < b.with(|__s: &S| __s.v) }; } pub fn operator_gt_3(a: Ptr, b: Ptr) -> bool { return { - let _lhs = (*(*a.upgrade().deref()).v.borrow()); - _lhs > (*(*b.upgrade().deref()).v.borrow()) + let _lhs = a.with(|__s: &S| __s.v); + _lhs > b.with(|__s: &S| __s.v) }; } pub fn operator_le_4(a: Ptr, b: Ptr) -> bool { return { - let _lhs = (*(*a.upgrade().deref()).v.borrow()); - _lhs <= (*(*b.upgrade().deref()).v.borrow()) + let _lhs = a.with(|__s: &S| __s.v); + _lhs <= b.with(|__s: &S| __s.v) }; } pub fn operator_ge_5(a: Ptr, b: Ptr) -> bool { return { - let _lhs = (*(*a.upgrade().deref()).v.borrow()); - _lhs >= (*(*b.upgrade().deref()).v.borrow()) + let _lhs = a.with(|__s: &S| __s.v); + _lhs >= b.with(|__s: &S| __s.v) }; } pub fn operator_lt_6(a: Ptr, b: i32) -> bool { let b: Value = Rc::new(RefCell::new(b)); return { - let _lhs = (*(*a.upgrade().deref()).v.borrow()); + let _lhs = a.with(|__s: &S| __s.v); _lhs < (*b.borrow()) }; } @@ -114,12 +106,13 @@ pub fn operator_lt_7(a: i32, b: Ptr) -> bool { let a: Value = Rc::new(RefCell::new(a)); return { let _lhs = (*a.borrow()); - _lhs < (*(*b.upgrade().deref()).v.borrow()) + _lhs < b.with(|__s: &S| __s.v) }; } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct V { - pub v: Value, + #[offset(0)] + pub v: i32, } impl std::cmp::Ord for V { fn cmp(&self, other: &Self) -> std::cmp::Ordering { @@ -145,62 +138,47 @@ impl std::cmp::PartialEq for V { } } impl std::cmp::Eq for V {} -impl Clone for V { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*self.v.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } -} impl ByteRepr for V { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.v.borrow()).to_bytes(&mut buf[0..4]); + self.v.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - v: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + v: ::from_bytes(&buf[0..4]), } } } pub fn operator_eq_9(a: V, b: V) -> bool { let a: Value = Rc::new(RefCell::new(a)); let b: Value = Rc::new(RefCell::new(b)); - return ((*(*a.borrow()).v.borrow()) == (*(*b.borrow()).v.borrow())); + return ({ (*a.borrow()).v } == { (*b.borrow()).v }); } pub fn operator_ne_10(a: V, b: V) -> bool { let a: Value = Rc::new(RefCell::new(a)); let b: Value = Rc::new(RefCell::new(b)); - return ((*(*a.borrow()).v.borrow()) != (*(*b.borrow()).v.borrow())); + return ({ (*a.borrow()).v } != { (*b.borrow()).v }); } pub fn operator_lt_8(a: V, b: V) -> bool { let a: Value = Rc::new(RefCell::new(a)); let b: Value = Rc::new(RefCell::new(b)); - return ((*(*a.borrow()).v.borrow()) < (*(*b.borrow()).v.borrow())); + return ({ (*a.borrow()).v } < { (*b.borrow()).v }); } pub fn operator_gt_11(a: V, b: V) -> bool { let a: Value = Rc::new(RefCell::new(a)); let b: Value = Rc::new(RefCell::new(b)); - return ((*(*a.borrow()).v.borrow()) > (*(*b.borrow()).v.borrow())); + return ({ (*a.borrow()).v } > { (*b.borrow()).v }); } pub fn main() { __cpp2rust_init_globals(); std::process::exit(main_0()); } fn main_0() -> i32 { - let x: Value = Rc::new(RefCell::new(V { - v: Rc::new(RefCell::new(1)), - })); - let y: Value = Rc::new(RefCell::new(V { - v: Rc::new(RefCell::new(2)), - })); - let z: Value = Rc::new(RefCell::new(V { - v: Rc::new(RefCell::new(1)), - })); + let x: Value = Rc::new(RefCell::new(V { v: 1 })); + let y: Value = Rc::new(RefCell::new(V { v: 2 })); + let z: Value = Rc::new(RefCell::new(V { v: 1 })); assert!( ({ let _a: V = (*x.borrow()).clone(); @@ -231,15 +209,9 @@ fn main_0() -> i32 { operator_lt_8(_a, (*x.borrow()).clone()) }) ); - let a: Value = Rc::new(RefCell::new(S { - v: Rc::new(RefCell::new(1)), - })); - let b: Value = Rc::new(RefCell::new(S { - v: Rc::new(RefCell::new(2)), - })); - let c: Value = Rc::new(RefCell::new(S { - v: Rc::new(RefCell::new(1)), - })); + let a: Value = Rc::new(RefCell::new(S { v: 1 })); + let b: Value = Rc::new(RefCell::new(S { v: 2 })); + let c: Value = Rc::new(RefCell::new(S { v: 1 })); assert!( ({ let _a: Ptr = a.as_pointer(); diff --git a/tests/unit/out/refcount/operator_comparison_member.rs b/tests/unit/out/refcount/operator_comparison_member.rs index 2ac3533c91..0e2ba872c0 100644 --- a/tests/unit/out/refcount/operator_comparison_member.rs +++ b/tests/unit/out/refcount/operator_comparison_member.rs @@ -6,9 +6,10 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct S { - pub v: Value, + #[offset(0)] + pub v: i32, } impl std::cmp::Ord for S { fn cmp(&self, other: &Self) -> std::cmp::Ordering { @@ -45,25 +46,16 @@ impl std::cmp::PartialEq for S { } } impl std::cmp::Eq for S {} -impl Clone for S { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*self.v.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } -} impl ByteRepr for S { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.v.borrow()).to_bytes(&mut buf[0..4]); + self.v.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - v: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + v: ::from_bytes(&buf[0..4]), } } } @@ -72,15 +64,9 @@ pub fn main() { std::process::exit(main_0()); } fn main_0() -> i32 { - let a: Value = Rc::new(RefCell::new(S { - v: Rc::new(RefCell::new(1)), - })); - let b: Value = Rc::new(RefCell::new(S { - v: Rc::new(RefCell::new(2)), - })); - let c: Value = Rc::new(RefCell::new(S { - v: Rc::new(RefCell::new(1)), - })); + let a: Value = Rc::new(RefCell::new(S { v: 1 })); + let b: Value = Rc::new(RefCell::new(S { v: 2 })); + let c: Value = Rc::new(RefCell::new(S { v: 1 })); assert!(({ SImpl::operator_eq(&a.as_pointer(), c.as_pointer(),) })); assert!(({ SImpl::operator_ne(&a.as_pointer(), b.as_pointer(),) })); assert!(({ SImpl::operator_lt_3(&a.as_pointer(), b.as_pointer(),) })); @@ -103,43 +89,43 @@ pub trait SImpl { impl SImpl for Ptr { fn operator_eq(&self, o: Ptr) -> bool { return { - let _lhs = (*(*(*self).upgrade().deref()).v.borrow()); - _lhs == (*(*o.upgrade().deref()).v.borrow()) + let _lhs = (*self).with(|__s: &S| __s.v); + _lhs == o.with(|__s: &S| __s.v) }; } fn operator_ne(&self, o: Ptr) -> bool { return { - let _lhs = (*(*(*self).upgrade().deref()).v.borrow()); - _lhs != (*(*o.upgrade().deref()).v.borrow()) + let _lhs = (*self).with(|__s: &S| __s.v); + _lhs != o.with(|__s: &S| __s.v) }; } fn operator_lt_3(&self, o: Ptr) -> bool { return { - let _lhs = (*(*(*self).upgrade().deref()).v.borrow()); - _lhs < (*(*o.upgrade().deref()).v.borrow()) + let _lhs = (*self).with(|__s: &S| __s.v); + _lhs < o.with(|__s: &S| __s.v) }; } fn operator_gt(&self, o: Ptr) -> bool { return { - let _lhs = (*(*(*self).upgrade().deref()).v.borrow()); - _lhs > (*(*o.upgrade().deref()).v.borrow()) + let _lhs = (*self).with(|__s: &S| __s.v); + _lhs > o.with(|__s: &S| __s.v) }; } fn operator_le(&self, o: Ptr) -> bool { return { - let _lhs = (*(*(*self).upgrade().deref()).v.borrow()); - _lhs <= (*(*o.upgrade().deref()).v.borrow()) + let _lhs = (*self).with(|__s: &S| __s.v); + _lhs <= o.with(|__s: &S| __s.v) }; } fn operator_ge(&self, o: Ptr) -> bool { return { - let _lhs = (*(*(*self).upgrade().deref()).v.borrow()); - _lhs >= (*(*o.upgrade().deref()).v.borrow()) + let _lhs = (*self).with(|__s: &S| __s.v); + _lhs >= o.with(|__s: &S| __s.v) }; } fn operator_lt_7(&self, o: i32) -> bool { let o: Value = Rc::new(RefCell::new(o)); - return ((*(*(*self).upgrade().deref()).v.borrow()) < (*o.borrow())); + return ((*self).with(|__s: &S| __s.v) < (*o.borrow())); } } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/operator_comparison_mixed.rs b/tests/unit/out/refcount/operator_comparison_mixed.rs index 8e025beab2..e9d0e8e87d 100644 --- a/tests/unit/out/refcount/operator_comparison_mixed.rs +++ b/tests/unit/out/refcount/operator_comparison_mixed.rs @@ -6,29 +6,21 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct S { - pub v: Value, -} -impl Clone for S { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*self.v.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub v: i32, } impl ByteRepr for S { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.v.borrow()).to_bytes(&mut buf[0..4]); + self.v.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - v: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + v: ::from_bytes(&buf[0..4]), } } } @@ -36,48 +28,48 @@ pub fn operator_eq_0(a: i32, b: Ptr) -> bool { let a: Value = Rc::new(RefCell::new(a)); return { let _lhs = (*a.borrow()); - _lhs == (*(*b.upgrade().deref()).v.borrow()) + _lhs == b.with(|__s: &S| __s.v) }; } pub fn operator_ne_1(a: i32, b: Ptr) -> bool { let a: Value = Rc::new(RefCell::new(a)); return { let _lhs = (*a.borrow()); - _lhs != (*(*b.upgrade().deref()).v.borrow()) + _lhs != b.with(|__s: &S| __s.v) }; } pub fn operator_lt_2(a: i32, b: Ptr) -> bool { let a: Value = Rc::new(RefCell::new(a)); return { let _lhs = (*a.borrow()); - _lhs < (*(*b.upgrade().deref()).v.borrow()) + _lhs < b.with(|__s: &S| __s.v) }; } pub fn operator_gt_3(a: f64, b: Ptr) -> bool { let a: Value = Rc::new(RefCell::new(a)); return { let _lhs = (*a.borrow()); - _lhs > ((*(*b.upgrade().deref()).v.borrow()) as f64) + _lhs > (b.with(|__s: &S| __s.v) as f64) }; } pub fn operator_le_4(a: i64, b: Ptr) -> bool { let a: Value = Rc::new(RefCell::new(a)); return { let _lhs = (*a.borrow()); - _lhs <= ((*(*b.upgrade().deref()).v.borrow()) as i64) + _lhs <= (b.with(|__s: &S| __s.v) as i64) }; } pub fn operator_ge_5(a: Ptr, b: Ptr) -> bool { let a: Value> = Rc::new(RefCell::new(a)); return { let _lhs = ((((*a.borrow()).read()) as i32) - (('0' as u8) as i32)); - _lhs >= (*(*b.upgrade().deref()).v.borrow()) + _lhs >= b.with(|__s: &S| __s.v) }; } pub fn operator_lt_6(a: Ptr, b: i32) -> bool { let b: Value = Rc::new(RefCell::new(b)); return { - let _lhs = ((*(*a.upgrade().deref()).v.borrow()) + 1); + let _lhs = (a.with(|__s: &S| __s.v) + 1); _lhs < (*b.borrow()) }; } @@ -86,9 +78,7 @@ pub fn main() { std::process::exit(main_0()); } fn main_0() -> i32 { - let s: Value = Rc::new(RefCell::new(S { - v: Rc::new(RefCell::new(5)), - })); + let s: Value = Rc::new(RefCell::new(S { v: 5 })); let cs: Ptr = s.as_pointer(); assert!(({ SImpl::operator_eq(&cs, 5,) })); assert!(({ SImpl::operator_ne(&cs, 4,) })); @@ -127,28 +117,28 @@ pub trait SImpl { impl SImpl for Ptr { fn operator_eq(&self, o: i32) -> bool { let o: Value = Rc::new(RefCell::new(o)); - return ((*(*(*self).upgrade().deref()).v.borrow()) == (*o.borrow())); + return ((*self).with(|__s: &S| __s.v) == (*o.borrow())); } fn operator_ne(&self, o: i32) -> bool { let o: Value = Rc::new(RefCell::new(o)); - return ((*(*(*self).upgrade().deref()).v.borrow()) != (*o.borrow())); + return ((*self).with(|__s: &S| __s.v) != (*o.borrow())); } fn operator_lt(&self, o: i32) -> bool { let o: Value = Rc::new(RefCell::new(o)); - return ((*(*(*self).upgrade().deref()).v.borrow()) < (*o.borrow())); + return ((*self).with(|__s: &S| __s.v) < (*o.borrow())); } fn operator_gt(&self, o: f64) -> bool { let o: Value = Rc::new(RefCell::new(o)); - return (((*(*(*self).upgrade().deref()).v.borrow()) as f64) > (*o.borrow())); + return (((*self).with(|__s: &S| __s.v) as f64) > (*o.borrow())); } fn operator_le(&self, o: i64) -> bool { let o: Value = Rc::new(RefCell::new(o)); - return (((*(*(*self).upgrade().deref()).v.borrow()) as i64) <= (*o.borrow())); + return (((*self).with(|__s: &S| __s.v) as i64) <= (*o.borrow())); } fn operator_ge(&self, o: Ptr) -> bool { let o: Value> = Rc::new(RefCell::new(o)); return { - let _lhs = (*(*(*self).upgrade().deref()).v.borrow()); + let _lhs = (*self).with(|__s: &S| __s.v); _lhs >= ((((*o.borrow()).read()) as i32) - (('0' as u8) as i32)) }; } diff --git a/tests/unit/out/refcount/operator_comparison_noncopyable.rs b/tests/unit/out/refcount/operator_comparison_noncopyable.rs index 3be6d15245..1bff0a0715 100644 --- a/tests/unit/out/refcount/operator_comparison_noncopyable.rs +++ b/tests/unit/out/refcount/operator_comparison_noncopyable.rs @@ -8,32 +8,33 @@ use std::os::fd::AsFd; use std::rc::{Rc, Weak}; pub fn operator_eq_0(x: Ptr, y: Ptr) -> bool { return { - let _lhs = (*(*x.upgrade().deref()).data_.borrow()); - _lhs == (*(*y.upgrade().deref()).data_.borrow()) + let _lhs = x.with(|__s: &S| __s.data_); + _lhs == y.with(|__s: &S| __s.data_) }; } pub fn operator_lt_1(x: Ptr, y: Ptr) -> bool { return { - let _lhs = (*(*x.upgrade().deref()).data_.borrow()); - _lhs < (*(*y.upgrade().deref()).data_.borrow()) + let _lhs = x.with(|__s: &S| __s.data_); + _lhs < y.with(|__s: &S| __s.data_) }; } -#[derive(Default)] +#[derive(Record, Default)] pub struct S { - data_: Value, + #[offset(0)] + data_: i32, } impl S { pub fn new(data: i32) -> Self { let data: Value = Rc::new(RefCell::new(data)); let __this: Value = Rc::new(RefCell::new(Self { - data_: Rc::new(RefCell::new((*data.borrow()))), + data_: (*data.borrow()), })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } pub fn move_from(_a0: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - data_: Rc::new(RefCell::new((*(*_a0.upgrade().deref()).data_.borrow()))), + data_: { _a0.with(|__s: &S| __s.data_) }, })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() @@ -97,11 +98,11 @@ impl ByteRepr for S { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.data_.borrow()).to_bytes(&mut buf[0..4]); + self.data_.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - data_: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + data_: ::from_bytes(&buf[0..4]), } } } diff --git a/tests/unit/out/refcount/operator_compound_assignment_free.rs b/tests/unit/out/refcount/operator_compound_assignment_free.rs index 0cdadb5544..00df835bec 100644 --- a/tests/unit/out/refcount/operator_compound_assignment_free.rs +++ b/tests/unit/out/refcount/operator_compound_assignment_free.rs @@ -6,95 +6,82 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct S { - pub v: Value, -} -impl Clone for S { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*self.v.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub v: u32, } impl ByteRepr for S { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.v.borrow()).to_bytes(&mut buf[0..4]); + self.v.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - v: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + v: ::from_bytes(&buf[0..4]), } } } pub fn operator_add_assign_0(a: Ptr, b: Ptr) -> Ptr { { - let rhs_0 = - (*(*a.upgrade().deref()).v.borrow()).wrapping_add((*(*b.upgrade().deref()).v.borrow())); - (*(*a.upgrade().deref()).v.borrow_mut()) = rhs_0 + let rhs_0 = (a.with(|__s: &S| __s.v)).wrapping_add(b.with(|__s: &S| __s.v)); + a.with_mut(|__s: &mut S| __s.v = rhs_0) }; return (a).clone(); } pub fn operator_sub_assign_1(a: Ptr, b: Ptr) -> Ptr { { - let rhs_0 = - (*(*a.upgrade().deref()).v.borrow()).wrapping_sub((*(*b.upgrade().deref()).v.borrow())); - (*(*a.upgrade().deref()).v.borrow_mut()) = rhs_0 + let rhs_0 = (a.with(|__s: &S| __s.v)).wrapping_sub(b.with(|__s: &S| __s.v)); + a.with_mut(|__s: &mut S| __s.v = rhs_0) }; return (a).clone(); } pub fn operator_mul_assign_2(a: Ptr, b: Ptr) -> Ptr { { - let rhs_0 = - (*(*a.upgrade().deref()).v.borrow()).wrapping_mul((*(*b.upgrade().deref()).v.borrow())); - (*(*a.upgrade().deref()).v.borrow_mut()) = rhs_0 + let rhs_0 = (a.with(|__s: &S| __s.v)).wrapping_mul(b.with(|__s: &S| __s.v)); + a.with_mut(|__s: &mut S| __s.v = rhs_0) }; return (a).clone(); } pub fn operator_div_assign_3(a: Ptr, b: Ptr) -> Ptr { { - let rhs_0 = - (*(*a.upgrade().deref()).v.borrow()).wrapping_div((*(*b.upgrade().deref()).v.borrow())); - (*(*a.upgrade().deref()).v.borrow_mut()) = rhs_0 + let rhs_0 = (a.with(|__s: &S| __s.v)).wrapping_div(b.with(|__s: &S| __s.v)); + a.with_mut(|__s: &mut S| __s.v = rhs_0) }; return (a).clone(); } pub fn operator_rem_assign_4(a: Ptr, b: Ptr) -> Ptr { { - let rhs_0 = - (*(*a.upgrade().deref()).v.borrow()).wrapping_rem((*(*b.upgrade().deref()).v.borrow())); - (*(*a.upgrade().deref()).v.borrow_mut()) = rhs_0 + let rhs_0 = (a.with(|__s: &S| __s.v)).wrapping_rem(b.with(|__s: &S| __s.v)); + a.with_mut(|__s: &mut S| __s.v = rhs_0) }; return (a).clone(); } pub fn operator_bitand_assign_5(a: Ptr, b: Ptr) -> Ptr { - let __rhs = (*(*b.upgrade().deref()).v.borrow()); - (*(*a.upgrade().deref()).v.borrow_mut()) &= __rhs; + let __rhs = b.with(|__s: &S| __s.v); + a.with_mut(|__s: &mut S| __s.v &= __rhs); return (a).clone(); } pub fn operator_bitor_assign_6(a: Ptr, b: Ptr) -> Ptr { - let __rhs = (*(*b.upgrade().deref()).v.borrow()); - (*(*a.upgrade().deref()).v.borrow_mut()) |= __rhs; + let __rhs = b.with(|__s: &S| __s.v); + a.with_mut(|__s: &mut S| __s.v |= __rhs); return (a).clone(); } pub fn operator_bitxor_assign_7(a: Ptr, b: Ptr) -> Ptr { - let __rhs = (*(*b.upgrade().deref()).v.borrow()); - (*(*a.upgrade().deref()).v.borrow_mut()) ^= __rhs; + let __rhs = b.with(|__s: &S| __s.v); + a.with_mut(|__s: &mut S| __s.v ^= __rhs); return (a).clone(); } pub fn operator_shl_assign_8(a: Ptr, n: i32) -> Ptr { let n: Value = Rc::new(RefCell::new(n)); - (*(*a.upgrade().deref()).v.borrow_mut()) <<= (*n.borrow()); + a.with_mut(|__s: &mut S| __s.v <<= (*n.borrow())); return (a).clone(); } pub fn operator_shr_assign_9(a: Ptr, n: i32) -> Ptr { let n: Value = Rc::new(RefCell::new(n)); - (*(*a.upgrade().deref()).v.borrow_mut()) >>= (*n.borrow()); + a.with_mut(|__s: &mut S| __s.v >>= (*n.borrow())); return (a).clone(); } pub fn main() { @@ -102,63 +89,59 @@ pub fn main() { std::process::exit(main_0()); } fn main_0() -> i32 { - let a: Value = Rc::new(RefCell::new(S { - v: Rc::new(RefCell::new(6_u32)), - })); - let b: Value = Rc::new(RefCell::new(S { - v: Rc::new(RefCell::new(4_u32)), - })); + let a: Value = Rc::new(RefCell::new(S { v: 6_u32 })); + let b: Value = Rc::new(RefCell::new(S { v: 4_u32 })); ({ let _a: Ptr = a.as_pointer(); operator_add_assign_0(_a, b.as_pointer()) }); - assert!(((*(*a.borrow()).v.borrow()) == 10_u32)); + assert!(({ (*a.borrow()).v } == 10_u32)); ({ let _a: Ptr = a.as_pointer(); operator_sub_assign_1(_a, b.as_pointer()) }); - assert!(((*(*a.borrow()).v.borrow()) == 6_u32)); + assert!(({ (*a.borrow()).v } == 6_u32)); ({ let _a: Ptr = a.as_pointer(); operator_mul_assign_2(_a, b.as_pointer()) }); - assert!(((*(*a.borrow()).v.borrow()) == 24_u32)); + assert!(({ (*a.borrow()).v } == 24_u32)); ({ let _a: Ptr = a.as_pointer(); operator_div_assign_3(_a, b.as_pointer()) }); - assert!(((*(*a.borrow()).v.borrow()) == 6_u32)); + assert!(({ (*a.borrow()).v } == 6_u32)); ({ let _a: Ptr = a.as_pointer(); operator_rem_assign_4(_a, b.as_pointer()) }); - assert!(((*(*a.borrow()).v.borrow()) == 2_u32)); + assert!(({ (*a.borrow()).v } == 2_u32)); ({ let _a: Ptr = a.as_pointer(); operator_bitor_assign_6(_a, b.as_pointer()) }); - assert!(((*(*a.borrow()).v.borrow()) == 6_u32)); + assert!(({ (*a.borrow()).v } == 6_u32)); ({ let _a: Ptr = a.as_pointer(); operator_bitand_assign_5(_a, b.as_pointer()) }); - assert!(((*(*a.borrow()).v.borrow()) == 4_u32)); + assert!(({ (*a.borrow()).v } == 4_u32)); ({ let _a: Ptr = a.as_pointer(); operator_bitxor_assign_7(_a, b.as_pointer()) }); - assert!(((*(*a.borrow()).v.borrow()) == 0_u32)); - (*(*a.borrow()).v.borrow_mut()) = 3_u32; + assert!(({ (*a.borrow()).v } == 0_u32)); + (*a.borrow_mut()).v = 3_u32; ({ let _a: Ptr = a.as_pointer(); operator_shl_assign_8(_a, 2) }); - assert!(((*(*a.borrow()).v.borrow()) == 12_u32)); + assert!(({ (*a.borrow()).v } == 12_u32)); ({ let _a: Ptr = a.as_pointer(); operator_shr_assign_9(_a, 1) }); - assert!(((*(*a.borrow()).v.borrow()) == 6_u32)); + assert!(({ (*a.borrow()).v } == 6_u32)); ({ let _a: Ptr = ({ let _a: Ptr = a.as_pointer(); @@ -167,7 +150,7 @@ fn main_0() -> i32 { let _b: Ptr = b.as_pointer(); operator_add_assign_0(_a, _b) }); - assert!(((*(*a.borrow()).v.borrow()) == 14_u32)); + assert!(({ (*a.borrow()).v } == 14_u32)); return 0; } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/operator_compound_assignment_member.rs b/tests/unit/out/refcount/operator_compound_assignment_member.rs index ca6f770ed7..719e47d389 100644 --- a/tests/unit/out/refcount/operator_compound_assignment_member.rs +++ b/tests/unit/out/refcount/operator_compound_assignment_member.rs @@ -6,29 +6,21 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct S { - pub v: Value, -} -impl Clone for S { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*self.v.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub v: u32, } impl ByteRepr for S { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.v.borrow()).to_bytes(&mut buf[0..4]); + self.v.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - v: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + v: ::from_bytes(&buf[0..4]), } } } @@ -37,34 +29,30 @@ pub fn main() { std::process::exit(main_0()); } fn main_0() -> i32 { - let a: Value = Rc::new(RefCell::new(S { - v: Rc::new(RefCell::new(6_u32)), - })); - let b: Value = Rc::new(RefCell::new(S { - v: Rc::new(RefCell::new(4_u32)), - })); + let a: Value = Rc::new(RefCell::new(S { v: 6_u32 })); + let b: Value = Rc::new(RefCell::new(S { v: 4_u32 })); ({ SImpl::operator_add_assign(&a.as_pointer(), b.as_pointer()) }); - assert!(((*(*a.borrow()).v.borrow()) == 10_u32)); + assert!(({ (*a.borrow()).v } == 10_u32)); ({ SImpl::operator_sub_assign(&a.as_pointer(), b.as_pointer()) }); - assert!(((*(*a.borrow()).v.borrow()) == 6_u32)); + assert!(({ (*a.borrow()).v } == 6_u32)); ({ SImpl::operator_mul_assign(&a.as_pointer(), b.as_pointer()) }); - assert!(((*(*a.borrow()).v.borrow()) == 24_u32)); + assert!(({ (*a.borrow()).v } == 24_u32)); ({ SImpl::operator_div_assign(&a.as_pointer(), b.as_pointer()) }); - assert!(((*(*a.borrow()).v.borrow()) == 6_u32)); + assert!(({ (*a.borrow()).v } == 6_u32)); ({ SImpl::operator_rem_assign(&a.as_pointer(), b.as_pointer()) }); - assert!(((*(*a.borrow()).v.borrow()) == 2_u32)); + assert!(({ (*a.borrow()).v } == 2_u32)); ({ SImpl::operator_bitor_assign(&a.as_pointer(), b.as_pointer()) }); - assert!(((*(*a.borrow()).v.borrow()) == 6_u32)); + assert!(({ (*a.borrow()).v } == 6_u32)); ({ SImpl::operator_bitand_assign(&a.as_pointer(), b.as_pointer()) }); - assert!(((*(*a.borrow()).v.borrow()) == 4_u32)); + assert!(({ (*a.borrow()).v } == 4_u32)); ({ SImpl::operator_bitxor_assign(&a.as_pointer(), b.as_pointer()) }); - assert!(((*(*a.borrow()).v.borrow()) == 0_u32)); + assert!(({ (*a.borrow()).v } == 0_u32)); ({ SImpl::operator_assign_1(&a.as_pointer(), 3_u32) }); - assert!(((*(*a.borrow()).v.borrow()) == 3_u32)); + assert!(({ (*a.borrow()).v } == 3_u32)); ({ SImpl::operator_shl_assign(&a.as_pointer(), 2) }); - assert!(((*(*a.borrow()).v.borrow()) == 12_u32)); + assert!(({ (*a.borrow()).v } == 12_u32)); ({ SImpl::operator_shr_assign(&a.as_pointer(), 1) }); - assert!(((*(*a.borrow()).v.borrow()) == 6_u32)); + assert!(({ (*a.borrow()).v } == 6_u32)); ({ let _o: Ptr = b.as_pointer(); SImpl::operator_add_assign( @@ -72,16 +60,14 @@ fn main_0() -> i32 { _o, ) }); - assert!(((*(*a.borrow()).v.borrow()) == 14_u32)); - let c: Value = Rc::new(RefCell::new(S { - v: Rc::new(RefCell::new(0_u32)), - })); + assert!(({ (*a.borrow()).v } == 14_u32)); + let c: Value = Rc::new(RefCell::new(S { v: 0_u32 })); (*c.borrow_mut()) = (*({ SImpl::operator_assign_1(&a.as_pointer(), 1_u32) }) .upgrade() .deref()) .clone(); - assert!(((*(*a.borrow()).v.borrow()) == 1_u32)); - assert!(((*(*c.borrow()).v.borrow()) == 1_u32)); + assert!(({ (*a.borrow()).v } == 1_u32)); + assert!(({ (*c.borrow()).v } == 1_u32)); return 0; } pub trait SImpl { @@ -100,72 +86,67 @@ pub trait SImpl { impl SImpl for Ptr { fn operator_assign_1(&self, n: u32) -> Ptr { let n: Value = Rc::new(RefCell::new(n)); - (*(*(*self).upgrade().deref()).v.borrow_mut()) = (*n.borrow()); + (*self).with_mut(|__s: &mut S| __s.v = (*n.borrow())); return (*self).clone(); } fn operator_add_assign(&self, o: Ptr) -> Ptr { { - let rhs_0 = (*(*(*self).upgrade().deref()).v.borrow()) - .wrapping_add((*(*o.upgrade().deref()).v.borrow())); - (*(*(*self).upgrade().deref()).v.borrow_mut()) = rhs_0 + let rhs_0 = ((*self).with(|__s: &S| __s.v)).wrapping_add(o.with(|__s: &S| __s.v)); + (*self).with_mut(|__s: &mut S| __s.v = rhs_0) }; return (*self).clone(); } fn operator_sub_assign(&self, o: Ptr) -> Ptr { { - let rhs_0 = (*(*(*self).upgrade().deref()).v.borrow()) - .wrapping_sub((*(*o.upgrade().deref()).v.borrow())); - (*(*(*self).upgrade().deref()).v.borrow_mut()) = rhs_0 + let rhs_0 = ((*self).with(|__s: &S| __s.v)).wrapping_sub(o.with(|__s: &S| __s.v)); + (*self).with_mut(|__s: &mut S| __s.v = rhs_0) }; return (*self).clone(); } fn operator_mul_assign(&self, o: Ptr) -> Ptr { { - let rhs_0 = (*(*(*self).upgrade().deref()).v.borrow()) - .wrapping_mul((*(*o.upgrade().deref()).v.borrow())); - (*(*(*self).upgrade().deref()).v.borrow_mut()) = rhs_0 + let rhs_0 = ((*self).with(|__s: &S| __s.v)).wrapping_mul(o.with(|__s: &S| __s.v)); + (*self).with_mut(|__s: &mut S| __s.v = rhs_0) }; return (*self).clone(); } fn operator_div_assign(&self, o: Ptr) -> Ptr { { - let rhs_0 = (*(*(*self).upgrade().deref()).v.borrow()) - .wrapping_div((*(*o.upgrade().deref()).v.borrow())); - (*(*(*self).upgrade().deref()).v.borrow_mut()) = rhs_0 + let rhs_0 = ((*self).with(|__s: &S| __s.v)).wrapping_div(o.with(|__s: &S| __s.v)); + (*self).with_mut(|__s: &mut S| __s.v = rhs_0) }; return (*self).clone(); } fn operator_rem_assign(&self, o: Ptr) -> Ptr { { - let rhs_0 = (*(*(*self).upgrade().deref()).v.borrow()) - .wrapping_rem((*(*o.upgrade().deref()).v.borrow())); - (*(*(*self).upgrade().deref()).v.borrow_mut()) = rhs_0 + let rhs_0 = ((*self).with(|__s: &S| __s.v)).wrapping_rem(o.with(|__s: &S| __s.v)); + (*self).with_mut(|__s: &mut S| __s.v = rhs_0) }; return (*self).clone(); } fn operator_bitand_assign(&self, o: Ptr) -> Ptr { - let __rhs = (*(*o.upgrade().deref()).v.borrow()); - (*(*(*self).upgrade().deref()).v.borrow_mut()) &= __rhs; + let __rhs = o.with(|__s: &S| __s.v); + (*self).with_mut(|__s: &mut S| __s.v &= __rhs); return (*self).clone(); } fn operator_bitor_assign(&self, o: Ptr) -> Ptr { - let __rhs = (*(*o.upgrade().deref()).v.borrow()); - (*(*(*self).upgrade().deref()).v.borrow_mut()) |= __rhs; + let __rhs = o.with(|__s: &S| __s.v); + (*self).with_mut(|__s: &mut S| __s.v |= __rhs); return (*self).clone(); } fn operator_bitxor_assign(&self, o: Ptr) -> Ptr { - let __rhs = (*(*o.upgrade().deref()).v.borrow()); - (*(*(*self).upgrade().deref()).v.borrow_mut()) ^= __rhs; + let __rhs = o.with(|__s: &S| __s.v); + (*self).with_mut(|__s: &mut S| __s.v ^= __rhs); return (*self).clone(); } fn operator_shl_assign(&self, n: i32) -> Ptr { let n: Value = Rc::new(RefCell::new(n)); - (*(*(*self).upgrade().deref()).v.borrow_mut()) <<= (*n.borrow()); + (*self).with_mut(|__s: &mut S| __s.v <<= (*n.borrow())); return (*self).clone(); } fn operator_shr_assign(&self, n: i32) -> Ptr { let n: Value = Rc::new(RefCell::new(n)); - (*(*(*self).upgrade().deref()).v.borrow_mut()) >>= (*n.borrow()); + (*self).with_mut(|__s: &mut S| __s.v >>= (*n.borrow())); return (*self).clone(); } } diff --git a/tests/unit/out/refcount/operator_friend.rs b/tests/unit/out/refcount/operator_friend.rs index e281fd5ba3..ab5c6ccacb 100644 --- a/tests/unit/out/refcount/operator_friend.rs +++ b/tests/unit/out/refcount/operator_friend.rs @@ -8,17 +8,19 @@ use std::os::fd::AsFd; use std::rc::{Rc, Weak}; pub fn operator_eq_0(_a0: Ptr, _a1: Ptr) -> bool { return ({ - let _lhs = (*(*_a0.upgrade().deref()).a.borrow()); - _lhs == (*(*_a1.upgrade().deref()).a.borrow()) + let _lhs = _a0.with(|__s: &Defaulted| __s.a); + _lhs == _a1.with(|__s: &Defaulted| __s.a) }) && ({ - let _lhs = (*(*_a0.upgrade().deref()).b.borrow()); - _lhs == (*(*_a1.upgrade().deref()).b.borrow()) + let _lhs = _a0.with(|__s: &Defaulted| __s.b); + _lhs == _a1.with(|__s: &Defaulted| __s.b) }); } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Defaulted { - pub a: Value, - pub b: Value, + #[offset(0)] + pub a: i32, + #[offset(4)] + pub b: i32, } impl std::cmp::PartialEq for Defaulted { fn eq(&self, other: &Self) -> bool { @@ -39,36 +41,26 @@ impl std::cmp::PartialEq for Defaulted { } } impl std::cmp::Eq for Defaulted {} -impl Clone for Defaulted { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - a: Rc::new(RefCell::new((*self.a.borrow()))), - b: Rc::new(RefCell::new((*self.b.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } -} impl ByteRepr for Defaulted { fn byte_size() -> usize { 8 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.a.borrow()).to_bytes(&mut buf[0..4]); - (*self.b.borrow()).to_bytes(&mut buf[4..8]); + self.a.to_bytes(&mut buf[0..4]); + self.b.to_bytes(&mut buf[4..8]); } fn from_bytes(buf: &[u8]) -> Self { Self { - a: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - b: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + a: ::from_bytes(&buf[0..4]), + b: ::from_bytes(&buf[4..8]), } } } pub fn operator_cmp_1(_a0: Ptr, _a1: Ptr) -> std::cmp::Ordering { { let cmp: Value = Rc::new(RefCell::new(std::cmp::Ord::cmp( - &(*(*_a0.upgrade().deref()).a.borrow()), - &(*(*_a1.upgrade().deref()).a.borrow()), + &(_a0.with(|__s: &DefaultedOrd| __s.a)), + &(_a1.with(|__s: &DefaultedOrd| __s.a)), ))); if !((*cmp.borrow()) == std::cmp::Ordering::Equal) { return (*cmp.borrow_mut()).clone(); @@ -78,13 +70,14 @@ pub fn operator_cmp_1(_a0: Ptr, _a1: Ptr) -> std::cm } pub fn operator_eq_2(_a0: Ptr, _a1: Ptr) -> bool { return { - let _lhs = (*(*_a0.upgrade().deref()).a.borrow()); - _lhs == (*(*_a1.upgrade().deref()).a.borrow()) + let _lhs = _a0.with(|__s: &DefaultedOrd| __s.a); + _lhs == _a1.with(|__s: &DefaultedOrd| __s.a) }; } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct DefaultedOrd { - pub a: Value, + #[offset(0)] + pub a: i32, } impl std::cmp::Ord for DefaultedOrd { fn cmp(&self, other: &Self) -> std::cmp::Ordering { @@ -112,51 +105,43 @@ impl std::cmp::PartialEq for DefaultedOrd { } } impl std::cmp::Eq for DefaultedOrd {} -impl Clone for DefaultedOrd { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - a: Rc::new(RefCell::new((*self.a.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } -} impl ByteRepr for DefaultedOrd { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.a.borrow()).to_bytes(&mut buf[0..4]); + self.a.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - a: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + a: ::from_bytes(&buf[0..4]), } } } pub fn operator_eq_3(x: Ptr, y: Ptr) -> bool { return { - let _lhs = (*(*x.upgrade().deref()).a.borrow()); - _lhs == (*(*y.upgrade().deref()).a.borrow()) + let _lhs = x.with(|__s: &Inline| __s.a); + _lhs == y.with(|__s: &Inline| __s.a) }; } pub fn operator_lt_4(x: Ptr, y: Ptr) -> bool { return { - let _lhs = (*(*x.upgrade().deref()).a.borrow()); - _lhs < (*(*y.upgrade().deref()).a.borrow()) + let _lhs = x.with(|__s: &Inline| __s.a); + _lhs < y.with(|__s: &Inline| __s.a) }; } pub fn operator_add_5(x: Ptr, y: Ptr) -> Inline { return Inline { - a: Rc::new(RefCell::new({ - let _lhs = (*(*x.upgrade().deref()).a.borrow()); - _lhs + (*(*y.upgrade().deref()).a.borrow()) - })), + a: { + let _lhs = x.with(|__s: &Inline| __s.a); + _lhs + y.with(|__s: &Inline| __s.a) + }, }; } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Inline { - pub a: Value, + #[offset(0)] + pub a: i32, } impl std::cmp::Ord for Inline { fn cmp(&self, other: &Self) -> std::cmp::Ordering { @@ -193,31 +178,23 @@ impl std::cmp::PartialEq for Inline { } } impl std::cmp::Eq for Inline {} -impl Clone for Inline { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - a: Rc::new(RefCell::new((*self.a.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } -} impl ByteRepr for Inline { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.a.borrow()).to_bytes(&mut buf[0..4]); + self.a.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - a: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + a: ::from_bytes(&buf[0..4]), } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct OutOfLine { - pub a: Value, + #[offset(0)] + pub a: i32, } impl std::cmp::PartialEq for OutOfLine { fn eq(&self, other: &Self) -> bool { @@ -230,32 +207,23 @@ impl std::cmp::PartialEq for OutOfLine { } } impl std::cmp::Eq for OutOfLine {} -impl Clone for OutOfLine { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - a: Rc::new(RefCell::new((*self.a.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } -} impl ByteRepr for OutOfLine { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.a.borrow()).to_bytes(&mut buf[0..4]); + self.a.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - a: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + a: ::from_bytes(&buf[0..4]), } } } pub fn operator_eq_6(x: Ptr, y: Ptr) -> bool { return { - let _lhs = (*(*x.upgrade().deref()).a.borrow()); - _lhs == (*(*y.upgrade().deref()).a.borrow()) + let _lhs = x.with(|__s: &OutOfLine| __s.a); + _lhs == y.with(|__s: &OutOfLine| __s.a) }; } pub fn operator_ne_7(x: Ptr, y: Ptr) -> bool { @@ -267,25 +235,26 @@ pub fn operator_ne_7(x: Ptr, y: Ptr) -> bool { } pub fn operator_eq_8(x: Ptr, y: Ptr) -> bool { return { - let _lhs = (*(*x.upgrade().deref()).v.borrow()); - _lhs == (*(*y.upgrade().deref()).v.borrow()) + let _lhs = x.with(|__s: &Tmpl_int_| __s.v); + _lhs == y.with(|__s: &Tmpl_int_| __s.v) }; } pub fn operator_lt_9(x: Ptr, y: Ptr) -> bool { return { - let _lhs = (*(*x.upgrade().deref()).v.borrow()); - _lhs < (*(*y.upgrade().deref()).v.borrow()) + let _lhs = x.with(|__s: &Tmpl_int_| __s.v); + _lhs < y.with(|__s: &Tmpl_int_| __s.v) }; } pub fn operator_eq_10(x: Ptr, y: Ptr) -> bool { return { - let _lhs = ((*(*x.upgrade().deref()).v.borrow()) as i64); - _lhs == (*(*y.upgrade().deref()).v.borrow()) + let _lhs = (x.with(|__s: &Tmpl_int_| __s.v) as i64); + _lhs == y.with(|__s: &Tmpl_long_| __s.v) }; } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Tmpl_int_ { - pub v: Value, + #[offset(0)] + pub v: i32, } impl std::cmp::Ord for Tmpl_int_ { fn cmp(&self, other: &Self) -> std::cmp::Ordering { @@ -322,63 +291,47 @@ impl std::cmp::PartialEq for Tmpl_int_ { } } impl std::cmp::Eq for Tmpl_int_ {} -impl Clone for Tmpl_int_ { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*self.v.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } -} impl ByteRepr for Tmpl_int_ { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.v.borrow()).to_bytes(&mut buf[0..4]); + self.v.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - v: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + v: ::from_bytes(&buf[0..4]), } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Tmpl_long_ { - pub v: Value, -} -impl Clone for Tmpl_long_ { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*self.v.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub v: i64, } impl ByteRepr for Tmpl_long_ { fn byte_size() -> usize { 8 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.v.borrow()).to_bytes(&mut buf[0..8]); + self.v.to_bytes(&mut buf[0..8]); } fn from_bytes(buf: &[u8]) -> Self { Self { - v: Rc::new(RefCell::new(::from_bytes(&buf[0..8]))), + v: ::from_bytes(&buf[0..8]), } } } pub fn operator_eq_11(_a0: Ptr, _a1: Ptr) -> bool { return { - let _lhs = (*(*_a0.upgrade().deref()).v.borrow()); - _lhs == (*(*_a1.upgrade().deref()).v.borrow()) + let _lhs = _a0.with(|__s: &TmplDefaulted_int_| __s.v); + _lhs == _a1.with(|__s: &TmplDefaulted_int_| __s.v) }; } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct TmplDefaulted_int_ { - pub v: Value, + #[offset(0)] + pub v: i32, } impl std::cmp::PartialEq for TmplDefaulted_int_ { fn eq(&self, other: &Self) -> bool { @@ -391,25 +344,16 @@ impl std::cmp::PartialEq for TmplDefaulted_int_ { } } impl std::cmp::Eq for TmplDefaulted_int_ {} -impl Clone for TmplDefaulted_int_ { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*self.v.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } -} impl ByteRepr for TmplDefaulted_int_ { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.v.borrow()).to_bytes(&mut buf[0..4]); + self.v.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - v: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + v: ::from_bytes(&buf[0..4]), } } } @@ -418,18 +362,9 @@ pub fn main() { std::process::exit(main_0()); } fn main_0() -> i32 { - let d1: Value = Rc::new(RefCell::new(Defaulted { - a: Rc::new(RefCell::new(1)), - b: Rc::new(RefCell::new(2)), - })); - let d2: Value = Rc::new(RefCell::new(Defaulted { - a: Rc::new(RefCell::new(1)), - b: Rc::new(RefCell::new(2)), - })); - let d3: Value = Rc::new(RefCell::new(Defaulted { - a: Rc::new(RefCell::new(1)), - b: Rc::new(RefCell::new(3)), - })); + let d1: Value = Rc::new(RefCell::new(Defaulted { a: 1, b: 2 })); + let d2: Value = Rc::new(RefCell::new(Defaulted { a: 1, b: 2 })); + let d3: Value = Rc::new(RefCell::new(Defaulted { a: 1, b: 3 })); assert!( ({ let _arg0: Ptr = d1.as_pointer(); @@ -448,12 +383,8 @@ fn main_0() -> i32 { operator_eq_0(_arg0, d3.as_pointer()) }) ); - let o1: Value = Rc::new(RefCell::new(DefaultedOrd { - a: Rc::new(RefCell::new(1)), - })); - let o2: Value = Rc::new(RefCell::new(DefaultedOrd { - a: Rc::new(RefCell::new(2)), - })); + let o1: Value = Rc::new(RefCell::new(DefaultedOrd { a: 1 })); + let o2: Value = Rc::new(RefCell::new(DefaultedOrd { a: 2 })); assert!( ({ let _arg0: Ptr = o1.as_pointer(); @@ -479,15 +410,9 @@ fn main_0() -> i32 { operator_cmp_1(_arg0, o2.as_pointer()) }) == std::cmp::Ordering::Less ); - let i1: Value = Rc::new(RefCell::new(Inline { - a: Rc::new(RefCell::new(1)), - })); - let i2: Value = Rc::new(RefCell::new(Inline { - a: Rc::new(RefCell::new(2)), - })); - let i3: Value = Rc::new(RefCell::new(Inline { - a: Rc::new(RefCell::new(1)), - })); + let i1: Value = Rc::new(RefCell::new(Inline { a: 1 })); + let i2: Value = Rc::new(RefCell::new(Inline { a: 2 })); + let i3: Value = Rc::new(RefCell::new(Inline { a: 1 })); assert!( ({ let _x: Ptr = i1.as_pointer(); @@ -520,21 +445,13 @@ fn main_0() -> i32 { operator_add_5(_x, i2.as_pointer()) }), )); - let _y: Value = Rc::new(RefCell::new(Inline { - a: Rc::new(RefCell::new(3)), - })); + let _y: Value = Rc::new(RefCell::new(Inline { a: 3 })); operator_eq_3(_x.as_pointer(), _y.as_pointer()) }) ); - let f1: Value = Rc::new(RefCell::new(OutOfLine { - a: Rc::new(RefCell::new(4)), - })); - let f2: Value = Rc::new(RefCell::new(OutOfLine { - a: Rc::new(RefCell::new(4)), - })); - let f3: Value = Rc::new(RefCell::new(OutOfLine { - a: Rc::new(RefCell::new(5)), - })); + let f1: Value = Rc::new(RefCell::new(OutOfLine { a: 4 })); + let f2: Value = Rc::new(RefCell::new(OutOfLine { a: 4 })); + let f3: Value = Rc::new(RefCell::new(OutOfLine { a: 5 })); assert!( ({ let _x: Ptr = f1.as_pointer(); @@ -553,15 +470,9 @@ fn main_0() -> i32 { operator_eq_6(_x, f3.as_pointer()) }) ); - let t1: Value = Rc::new(RefCell::new(Tmpl_int_ { - v: Rc::new(RefCell::new(1)), - })); - let t2: Value = Rc::new(RefCell::new(Tmpl_int_ { - v: Rc::new(RefCell::new(2)), - })); - let t3: Value = Rc::new(RefCell::new(Tmpl_int_ { - v: Rc::new(RefCell::new(1)), - })); + let t1: Value = Rc::new(RefCell::new(Tmpl_int_ { v: 1 })); + let t2: Value = Rc::new(RefCell::new(Tmpl_int_ { v: 2 })); + let t3: Value = Rc::new(RefCell::new(Tmpl_int_ { v: 1 })); assert!( ({ let _x: Ptr = t1.as_pointer(); @@ -580,12 +491,8 @@ fn main_0() -> i32 { operator_lt_9(_x, t2.as_pointer()) }) ); - let u1: Value = Rc::new(RefCell::new(Tmpl_long_ { - v: Rc::new(RefCell::new(1_i64)), - })); - let u2: Value = Rc::new(RefCell::new(Tmpl_long_ { - v: Rc::new(RefCell::new(2_i64)), - })); + let u1: Value = Rc::new(RefCell::new(Tmpl_long_ { v: 1_i64 })); + let u2: Value = Rc::new(RefCell::new(Tmpl_long_ { v: 2_i64 })); assert!( ({ let _x: Ptr = t1.as_pointer(); @@ -598,15 +505,9 @@ fn main_0() -> i32 { operator_eq_10(_x, u2.as_pointer()) }) ); - let v1: Value = Rc::new(RefCell::new(TmplDefaulted_int_ { - v: Rc::new(RefCell::new(7)), - })); - let v2: Value = Rc::new(RefCell::new(TmplDefaulted_int_ { - v: Rc::new(RefCell::new(7)), - })); - let v3: Value = Rc::new(RefCell::new(TmplDefaulted_int_ { - v: Rc::new(RefCell::new(8)), - })); + let v1: Value = Rc::new(RefCell::new(TmplDefaulted_int_ { v: 7 })); + let v2: Value = Rc::new(RefCell::new(TmplDefaulted_int_ { v: 7 })); + let v3: Value = Rc::new(RefCell::new(TmplDefaulted_int_ { v: 8 })); assert!( ({ let _arg0: Ptr = v1.as_pointer(); diff --git a/tests/unit/out/refcount/operator_less_than.rs b/tests/unit/out/refcount/operator_less_than.rs index b8d964738f..3c82c1bbcc 100644 --- a/tests/unit/out/refcount/operator_less_than.rs +++ b/tests/unit/out/refcount/operator_less_than.rs @@ -6,10 +6,12 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Pair { - pub x: Value, - pub y: Value, + #[offset(0)] + pub x: i32, + #[offset(4)] + pub y: i32, } impl std::cmp::Ord for Pair { fn cmp(&self, other: &Self) -> std::cmp::Ordering { @@ -81,28 +83,18 @@ impl std::cmp::PartialEq for Pair { } } impl std::cmp::Eq for Pair {} -impl Clone for Pair { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - x: Rc::new(RefCell::new((*self.x.borrow()))), - y: Rc::new(RefCell::new((*self.y.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } -} impl ByteRepr for Pair { fn byte_size() -> usize { 8 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.x.borrow()).to_bytes(&mut buf[0..4]); - (*self.y.borrow()).to_bytes(&mut buf[4..8]); + self.x.to_bytes(&mut buf[0..4]); + self.y.to_bytes(&mut buf[4..8]); } fn from_bytes(buf: &[u8]) -> Self { Self { - x: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - y: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + x: ::from_bytes(&buf[0..4]), + y: ::from_bytes(&buf[4..8]), } } } @@ -111,14 +103,8 @@ pub fn main() { std::process::exit(main_0()); } fn main_0() -> i32 { - let pair1: Value = Rc::new(RefCell::new(Pair { - x: Rc::new(RefCell::new(1)), - y: Rc::new(RefCell::new(2)), - })); - let pair2: Value = Rc::new(RefCell::new(Pair { - x: Rc::new(RefCell::new(1)), - y: Rc::new(RefCell::new(3)), - })); + let pair1: Value = Rc::new(RefCell::new(Pair { x: 1, y: 2 })); + let pair2: Value = Rc::new(RefCell::new(Pair { x: 1, y: 3 })); assert!(({ PairImpl::operator_lt(&pair1.as_pointer(), pair2.as_pointer(),) })); return 0; } @@ -128,14 +114,14 @@ pub trait PairImpl { impl PairImpl for Ptr { fn operator_lt(&self, other: Ptr) -> bool { return ({ - let _lhs = (*(*(*self).upgrade().deref()).x.borrow()); - _lhs < (*(*other.upgrade().deref()).x.borrow()) + let _lhs = (*self).with(|__s: &Pair| __s.x); + _lhs < other.with(|__s: &Pair| __s.x) }) || (({ - let _lhs = (*(*(*self).upgrade().deref()).x.borrow()); - _lhs == (*(*other.upgrade().deref()).x.borrow()) + let _lhs = (*self).with(|__s: &Pair| __s.x); + _lhs == other.with(|__s: &Pair| __s.x) }) && ({ - let _lhs = (*(*(*self).upgrade().deref()).y.borrow()); - _lhs < (*(*other.upgrade().deref()).y.borrow()) + let _lhs = (*self).with(|__s: &Pair| __s.y); + _lhs < other.with(|__s: &Pair| __s.y) })); } } diff --git a/tests/unit/out/refcount/operator_logical_free.rs b/tests/unit/out/refcount/operator_logical_free.rs index f07ae0d8c9..0ff3f1024f 100644 --- a/tests/unit/out/refcount/operator_logical_free.rs +++ b/tests/unit/out/refcount/operator_logical_free.rs @@ -6,54 +6,40 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct S { - pub v: Value, -} -impl Clone for S { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*self.v.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub v: i32, } impl ByteRepr for S { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.v.borrow()).to_bytes(&mut buf[0..4]); + self.v.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - v: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + v: ::from_bytes(&buf[0..4]), } } } pub fn operator_not_0(a: Ptr) -> bool { - return ((*(*a.upgrade().deref()).v.borrow()) == 0); + return (a.with(|__s: &S| __s.v) == 0); } pub fn operator_and_1(a: Ptr, b: Ptr) -> bool { - return ((*(*a.upgrade().deref()).v.borrow()) != 0) - && ((*(*b.upgrade().deref()).v.borrow()) != 0); + return (a.with(|__s: &S| __s.v) != 0) && (b.with(|__s: &S| __s.v) != 0); } pub fn operator_or_2(a: Ptr, b: Ptr) -> bool { - return ((*(*a.upgrade().deref()).v.borrow()) != 0) - || ((*(*b.upgrade().deref()).v.borrow()) != 0); + return (a.with(|__s: &S| __s.v) != 0) || (b.with(|__s: &S| __s.v) != 0); } pub fn main() { __cpp2rust_init_globals(); std::process::exit(main_0()); } fn main_0() -> i32 { - let t: Value = Rc::new(RefCell::new(S { - v: Rc::new(RefCell::new(1)), - })); - let f: Value = Rc::new(RefCell::new(S { - v: Rc::new(RefCell::new(0)), - })); + let t: Value = Rc::new(RefCell::new(S { v: 1 })); + let f: Value = Rc::new(RefCell::new(S { v: 0 })); assert!( ({ let _a: Ptr = f.as_pointer(); diff --git a/tests/unit/out/refcount/operator_logical_member.rs b/tests/unit/out/refcount/operator_logical_member.rs index a4dba0a4f5..e01a7c50ea 100644 --- a/tests/unit/out/refcount/operator_logical_member.rs +++ b/tests/unit/out/refcount/operator_logical_member.rs @@ -6,29 +6,21 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct S { - pub v: Value, -} -impl Clone for S { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*self.v.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub v: i32, } impl ByteRepr for S { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.v.borrow()).to_bytes(&mut buf[0..4]); + self.v.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - v: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + v: ::from_bytes(&buf[0..4]), } } } @@ -37,12 +29,8 @@ pub fn main() { std::process::exit(main_0()); } fn main_0() -> i32 { - let t: Value = Rc::new(RefCell::new(S { - v: Rc::new(RefCell::new(1)), - })); - let f: Value = Rc::new(RefCell::new(S { - v: Rc::new(RefCell::new(0)), - })); + let t: Value = Rc::new(RefCell::new(S { v: 1 })); + let f: Value = Rc::new(RefCell::new(S { v: 0 })); assert!(({ SImpl::operator_not(&f.as_pointer(),) })); assert!(!({ SImpl::operator_not(&t.as_pointer(),) })); assert!( @@ -68,15 +56,13 @@ pub trait SImpl { } impl SImpl for Ptr { fn operator_not(&self) -> bool { - return ((*(*(*self).upgrade().deref()).v.borrow()) == 0); + return ((*self).with(|__s: &S| __s.v) == 0); } fn operator_and(&self, o: Ptr) -> bool { - return ((*(*(*self).upgrade().deref()).v.borrow()) != 0) - && ((*(*o.upgrade().deref()).v.borrow()) != 0); + return ((*self).with(|__s: &S| __s.v) != 0) && (o.with(|__s: &S| __s.v) != 0); } fn operator_or(&self, o: Ptr) -> bool { - return ((*(*(*self).upgrade().deref()).v.borrow()) != 0) - || ((*(*o.upgrade().deref()).v.borrow()) != 0); + return ((*self).with(|__s: &S| __s.v) != 0) || (o.with(|__s: &S| __s.v) != 0); } } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/operator_member_pointer_free.rs b/tests/unit/out/refcount/operator_member_pointer_free.rs index 37b51dd8c0..fa542fff29 100644 --- a/tests/unit/out/refcount/operator_member_pointer_free.rs +++ b/tests/unit/out/refcount/operator_member_pointer_free.rs @@ -6,54 +6,36 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Inner { - pub x: Value, -} -impl Clone for Inner { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - x: Rc::new(RefCell::new((*self.x.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub x: i32, } impl ByteRepr for Inner { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.x.borrow()).to_bytes(&mut buf[0..4]); + self.x.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - x: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + x: ::from_bytes(&buf[0..4]), } } } -#[derive(VaArg, FnPtrArg)] +#[derive(Clone, Record, VaArg, FnPtrArg)] pub struct S { - pub data: Value>, - pub inner: Value, -} -impl Clone for S { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - data: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 3, _>( - |__i: usize| (*self.data.borrow())[(__i) as usize], - )))), - inner: Rc::new(RefCell::new((*self.inner.borrow()).clone())), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub data: Box<[i32]>, + #[offset(12)] + pub inner: Inner, } impl Default for S { fn default() -> Self { S { - data: Rc::new(RefCell::new((0..3).map(|_| 0_i32).collect::>())), - inner: >::default(), + data: (0..3).map(|_| 0_i32).collect::>(), + inner: ::default(), } } } @@ -62,21 +44,21 @@ impl ByteRepr for S { 16 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.data.borrow()).to_bytes(&mut buf[0..12]); - (*self.inner.borrow()).to_bytes(&mut buf[12..16]); + self.data.to_bytes(&mut buf[0..12]); + self.inner.to_bytes(&mut buf[12..16]); } fn from_bytes(buf: &[u8]) -> Self { Self { - data: Rc::new(RefCell::new(>::from_bytes(&buf[0..12]))), - inner: Rc::new(RefCell::new(::from_bytes(&buf[12..16]))), + data: >::from_bytes(&buf[0..12]), + inner: ::from_bytes(&buf[12..16]), } } } pub fn operator_deref_0(s: Ptr) -> Ptr { - return (*s.upgrade().deref()).inner.as_pointer(); + return field_ptr!(s, inner); } pub fn operator_addr_1(s: Ptr) -> Ptr { - return (((*s.upgrade().deref()).data.as_pointer() as Ptr).offset(0)); + return ((field_ptr!(s, data) as Ptr).offset(0)); } pub fn main() { __cpp2rust_init_globals(); @@ -84,31 +66,23 @@ pub fn main() { } fn main_0() -> i32 { let s: Value = Rc::new(RefCell::new(S { - data: Rc::new(RefCell::new(Box::new([1, 2, 3]))), - inner: Rc::new(RefCell::new(Inner { - x: Rc::new(RefCell::new(9)), - })), + data: Box::new([1, 2, 3]), + inner: Inner { x: 9 }, })); assert!( - ((*(*({ + (({ let _s: Ptr = s.as_pointer(); operator_deref_0(_s) }) - .upgrade() - .deref()) - .x - .borrow()) + .with(|__s: &Inner| __s.x) == 9) ); - (*(*({ + ({ let _s: Ptr = s.as_pointer(); operator_deref_0(_s) }) - .upgrade() - .deref()) - .x - .borrow_mut()) = 10; - assert!(((*(*(*s.borrow()).inner.borrow()).x.borrow()) == 10)); + .with_mut(|__s: &mut Inner| __s.x = 10); + assert!(({ (*s.borrow()).inner.x } == 10)); let p: Value> = Rc::new(RefCell::new( ({ let _s: Ptr = s.as_pointer(); @@ -117,7 +91,7 @@ fn main_0() -> i32 { )); assert!((((*p.borrow()).read()) == 1)); (*p.borrow()).write(5); - assert!(((*(*s.borrow()).data.borrow())[(0) as usize] == 5)); + assert!(((*s.borrow()).data[(0) as usize] == 5)); return 0; } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/operator_member_pointer_member.rs b/tests/unit/out/refcount/operator_member_pointer_member.rs index f2ac5ea037..15349cd985 100644 --- a/tests/unit/out/refcount/operator_member_pointer_member.rs +++ b/tests/unit/out/refcount/operator_member_pointer_member.rs @@ -6,33 +6,25 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Inner { - pub x: Value, -} -impl Clone for Inner { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - x: Rc::new(RefCell::new((*self.x.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub x: i32, } impl ByteRepr for Inner { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.x.borrow()).to_bytes(&mut buf[0..4]); + self.x.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - x: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + x: ::from_bytes(&buf[0..4]), } } } -#[derive(Clone, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] pub struct Table {} impl Table { pub fn operator_index(i: i32) -> Ptr { @@ -43,28 +35,18 @@ impl Table { thread_local!( pub static table_0: Value> = Rc::new(RefCell::new(Box::new([7, 8, 9]))); ); -#[derive(VaArg, FnPtrArg)] +#[derive(Clone, Record, VaArg, FnPtrArg)] pub struct S { - pub data: Value>, - pub inner: Value, -} -impl Clone for S { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - data: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 3, _>( - |__i: usize| (*self.data.borrow())[(__i) as usize], - )))), - inner: Rc::new(RefCell::new((*self.inner.borrow()).clone())), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub data: Box<[i32]>, + #[offset(12)] + pub inner: Inner, } impl Default for S { fn default() -> Self { S { - data: Rc::new(RefCell::new((0..3).map(|_| 0_i32).collect::>())), - inner: >::default(), + data: (0..3).map(|_| 0_i32).collect::>(), + inner: ::default(), } } } @@ -73,13 +55,13 @@ impl ByteRepr for S { 16 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.data.borrow()).to_bytes(&mut buf[0..12]); - (*self.inner.borrow()).to_bytes(&mut buf[12..16]); + self.data.to_bytes(&mut buf[0..12]); + self.inner.to_bytes(&mut buf[12..16]); } fn from_bytes(buf: &[u8]) -> Self { Self { - data: Rc::new(RefCell::new(>::from_bytes(&buf[0..12]))), - inner: Rc::new(RefCell::new(::from_bytes(&buf[12..16]))), + data: >::from_bytes(&buf[0..12]), + inner: ::from_bytes(&buf[12..16]), } } } @@ -89,47 +71,33 @@ pub fn main() { } fn main_0() -> i32 { let s: Value = Rc::new(RefCell::new(S { - data: Rc::new(RefCell::new(Box::new([1, 2, 3]))), - inner: Rc::new(RefCell::new(Inner { - x: Rc::new(RefCell::new(9)), - })), + data: Box::new([1, 2, 3]), + inner: Inner { x: 9 }, })); assert!(((({ SImpl::operator_index_1(&s.as_pointer(), 1,) }).read()) == 2)); ({ SImpl::operator_index_1(&s.as_pointer(), 1) }).write(20); assert!(((({ SImpl::operator_index_1(&s.as_pointer(), 1,) }).read()) == 20)); let cs: Ptr = s.as_pointer(); assert!(((({ SImpl::operator_index_2(&cs, 2,) }).read()) == 3)); + assert!((({ SImpl::operator_deref(&s.as_pointer(),) }).with(|__s: &Inner| __s.x) == 9)); + ({ SImpl::operator_deref(&s.as_pointer()) }).with_mut(|__s: &mut Inner| __s.x = 10); assert!( - ((*(*({ SImpl::operator_deref(&s.as_pointer(),) }) - .upgrade() - .deref()) - .x - .borrow()) - == 9) - ); - (*(*({ SImpl::operator_deref(&s.as_pointer()) }) - .upgrade() - .deref()) - .x - .borrow_mut()) = 10; - assert!( - ((*(*({ SImpl::operator_arrow(&s.as_pointer(),) }) - .upgrade() - .deref()) - .x - .borrow()) - == 10) + ({ + (*({ SImpl::operator_arrow(&s.as_pointer()) }) + .upgrade() + .deref()) + .x + } == 10) ); - (*(*({ SImpl::operator_arrow(&s.as_pointer()) }) + (*({ SImpl::operator_arrow(&s.as_pointer()) }) .upgrade() - .deref()) - .x - .borrow_mut()) = 11; - assert!(((*(*(*s.borrow()).inner.borrow()).x.borrow()) == 11)); + .deref_mut()) + .x = 11; + assert!(({ (*s.borrow()).inner.x } == 11)); let p: Value> = Rc::new(RefCell::new(({ SImpl::operator_addr(&s.as_pointer()) }))); assert!((((*p.borrow()).read()) == 1)); (*p.borrow()).write(5); - assert!(((*(*s.borrow()).data.borrow())[(0) as usize] == 5)); + assert!(((*s.borrow()).data[(0) as usize] == 5)); let t: Value = Rc::new(RefCell::new(
::default())); assert!(((({ Table::operator_index(1,) }).read()) == 8)); ({ Table::operator_index(1) }).write(80); @@ -151,20 +119,20 @@ pub trait SImpl { impl SImpl for Ptr { fn operator_index_1(&self, i: i32) -> Ptr { let i: Value = Rc::new(RefCell::new(i)); - return ((*(*self).upgrade().deref()).data.as_pointer() as Ptr).offset((*i.borrow())); + return (field_ptr!((*self), data) as Ptr).offset((*i.borrow())); } fn operator_index_2(&self, i: i32) -> Ptr { let i: Value = Rc::new(RefCell::new(i)); - return ((*(*self).upgrade().deref()).data.as_pointer() as Ptr).offset((*i.borrow())); + return (field_ptr!((*self), data) as Ptr).offset((*i.borrow())); } fn operator_deref(&self) -> Ptr { - return (*(*self).upgrade().deref()).inner.as_pointer(); + return field_ptr!((*self), inner); } fn operator_arrow(&self) -> Ptr { - return ((*(*self).upgrade().deref()).inner.as_pointer()); + return (field_ptr!((*self), inner)); } fn operator_addr(&self) -> Ptr { - return (((*(*self).upgrade().deref()).data.as_pointer() as Ptr).offset(0)); + return ((field_ptr!((*self), data) as Ptr).offset(0)); } } pub fn __cpp2rust_init_globals() { diff --git a/tests/unit/out/refcount/operator_other_free.rs b/tests/unit/out/refcount/operator_other_free.rs index bf07389628..a2c693d79c 100644 --- a/tests/unit/out/refcount/operator_other_free.rs +++ b/tests/unit/out/refcount/operator_other_free.rs @@ -6,38 +6,30 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct S { - pub v: Value, -} -impl Clone for S { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*self.v.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub v: i32, } impl ByteRepr for S { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.v.borrow()).to_bytes(&mut buf[0..4]); + self.v.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - v: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + v: ::from_bytes(&buf[0..4]), } } } pub fn operator_comma_0(a: Ptr, b: Ptr) -> S { return S { - v: Rc::new(RefCell::new({ - let _lhs = ((*(*a.upgrade().deref()).v.borrow()) * 10); - _lhs + (*(*b.upgrade().deref()).v.borrow()) - })), + v: { + let _lhs = (a.with(|__s: &S| __s.v) * 10); + _lhs + b.with(|__s: &S| __s.v) + }, }; } pub fn main() { @@ -45,35 +37,31 @@ pub fn main() { std::process::exit(main_0()); } fn main_0() -> i32 { - let s: Value = Rc::new(RefCell::new(S { - v: Rc::new(RefCell::new(3)), - })); - let t: Value = Rc::new(RefCell::new(S { - v: Rc::new(RefCell::new(4)), - })); + let s: Value = Rc::new(RefCell::new(S { v: 3 })); + let t: Value = Rc::new(RefCell::new(S { v: 4 })); assert!( - ((*({ - let _a: Ptr = s.as_pointer(); - operator_comma_0(_a, t.as_pointer()) - }) - .v - .borrow()) - == 34) + ({ + ({ + let _a: Ptr = s.as_pointer(); + operator_comma_0(_a, t.as_pointer()) + }) + .v + } == 34) ); assert!( - ((*({ - let _a: Value = Rc::new(RefCell::new( - ({ - let _a: Ptr = s.as_pointer(); - operator_comma_0(_a, t.as_pointer()) - }), - )); - let _b: Ptr = s.as_pointer(); - operator_comma_0(_a.as_pointer(), _b) - }) - .v - .borrow()) - == 343) + ({ + ({ + let _a: Value = Rc::new(RefCell::new( + ({ + let _a: Ptr = s.as_pointer(); + operator_comma_0(_a, t.as_pointer()) + }), + )); + let _b: Ptr = s.as_pointer(); + operator_comma_0(_a.as_pointer(), _b) + }) + .v + } == 343) ); return 0; } diff --git a/tests/unit/out/refcount/operator_other_member.rs b/tests/unit/out/refcount/operator_other_member.rs index fbc00504f6..c3b6e1a50d 100644 --- a/tests/unit/out/refcount/operator_other_member.rs +++ b/tests/unit/out/refcount/operator_other_member.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Clone, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] pub struct Static {} impl Static { pub fn operator_call(a: i32, b: i32) -> i32 { @@ -15,29 +15,21 @@ impl Static { return ((*a.borrow()) * (*b.borrow())); } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct S { - pub v: Value, -} -impl Clone for S { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*self.v.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub v: i32, } impl ByteRepr for S { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.v.borrow()).to_bytes(&mut buf[0..4]); + self.v.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - v: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + v: ::from_bytes(&buf[0..4]), } } } @@ -46,21 +38,12 @@ pub fn main() { std::process::exit(main_0()); } fn main_0() -> i32 { - let s: Value = Rc::new(RefCell::new(S { - v: Rc::new(RefCell::new(3)), - })); - let t: Value = Rc::new(RefCell::new(S { - v: Rc::new(RefCell::new(4)), - })); + let s: Value = Rc::new(RefCell::new(S { v: 3 })); + let t: Value = Rc::new(RefCell::new(S { v: 4 })); assert!((({ SImpl::operator_call_1(&s.as_pointer(),) }) == 3)); assert!((({ SImpl::operator_call_2(&s.as_pointer(), 1,) }) == 4)); assert!((({ SImpl::operator_call_3(&s.as_pointer(), 1, 2,) }) == 6)); - assert!( - ((*({ SImpl::operator_comma(&s.as_pointer(), t.as_pointer(),) }) - .v - .borrow()) - == 34) - ); + assert!(({ ({ SImpl::operator_comma(&s.as_pointer(), t.as_pointer(),) }).v } == 34)); let i: Value = Rc::new(RefCell::new(({ SImpl::to_i32(&s.as_pointer()) }))); assert!(((*i.borrow()) == 3)); assert!(((({ SImpl::to_i32(&s.as_pointer(),) }) + 1) == 4)); @@ -69,35 +52,15 @@ fn main_0() -> i32 { } else { assert!(false); } - let z: Value = Rc::new(RefCell::new(S { - v: Rc::new(RefCell::new(0)), - })); + let z: Value = Rc::new(RefCell::new(S { v: 0 })); assert!(({ SImpl::to_bool(&s.as_pointer(),) })); assert!(!({ SImpl::to_bool(&z.as_pointer(),) })); assert!(({ SImpl::to_bool(&s.as_pointer(),) }) && (!({ SImpl::to_bool(&z.as_pointer(),) }))); let st: Value = Rc::new(RefCell::new(::default())); assert!((({ Static::operator_call(6, 7,) }) == 42)); + assert!((({ SImpl::operator_call_1(&Rc::new(RefCell::new(S { v: 5 })).as_pointer(),) }) == 5)); assert!( - (({ - SImpl::operator_call_1( - &Rc::new(RefCell::new(S { - v: Rc::new(RefCell::new(5)), - })) - .as_pointer(), - ) - }) == 5) - ); - assert!( - (({ - SImpl::operator_call_3( - &Rc::new(RefCell::new(S { - v: Rc::new(RefCell::new(5)), - })) - .as_pointer(), - 1, - 1, - ) - }) == 7) + (({ SImpl::operator_call_3(&Rc::new(RefCell::new(S { v: 5 })).as_pointer(), 1, 1,) }) == 7) ); return 0; } @@ -111,30 +74,30 @@ pub trait SImpl { } impl SImpl for Ptr { fn operator_call_1(&self) -> i32 { - return (*(*(*self).upgrade().deref()).v.borrow()); + return (*self).with(|__s: &S| __s.v); } fn operator_call_2(&self, a: i32) -> i32 { let a: Value = Rc::new(RefCell::new(a)); - return ((*(*(*self).upgrade().deref()).v.borrow()) + (*a.borrow())); + return ((*self).with(|__s: &S| __s.v) + (*a.borrow())); } fn operator_call_3(&self, a: i32, b: i32) -> i32 { let a: Value = Rc::new(RefCell::new(a)); let b: Value = Rc::new(RefCell::new(b)); - return (((*(*(*self).upgrade().deref()).v.borrow()) + (*a.borrow())) + (*b.borrow())); + return (((*self).with(|__s: &S| __s.v) + (*a.borrow())) + (*b.borrow())); } fn operator_comma(&self, o: Ptr) -> S { return S { - v: Rc::new(RefCell::new({ - let _lhs = ((*(*(*self).upgrade().deref()).v.borrow()) * 10); - _lhs + (*(*o.upgrade().deref()).v.borrow()) - })), + v: { + let _lhs = ((*self).with(|__s: &S| __s.v) * 10); + _lhs + o.with(|__s: &S| __s.v) + }, }; } fn to_i32(&self) -> i32 { - return (*(*(*self).upgrade().deref()).v.borrow()); + return (*self).with(|__s: &S| __s.v); } fn to_bool(&self) -> bool { - return ((*(*(*self).upgrade().deref()).v.borrow()) != 0); + return ((*self).with(|__s: &S| __s.v) != 0); } } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/operator_overloads.rs b/tests/unit/out/refcount/operator_overloads.rs index 8e1d1ce40e..b36aad1667 100644 --- a/tests/unit/out/refcount/operator_overloads.rs +++ b/tests/unit/out/refcount/operator_overloads.rs @@ -6,59 +6,51 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct S { - pub v: Value, -} -impl Clone for S { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*self.v.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub v: i32, } impl ByteRepr for S { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.v.borrow()).to_bytes(&mut buf[0..4]); + self.v.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - v: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + v: ::from_bytes(&buf[0..4]), } } } pub fn operator_div_0(a: Ptr, b: Ptr) -> i32 { return { - let _lhs = (*(*a.upgrade().deref()).v.borrow()); - _lhs / (*(*b.upgrade().deref()).v.borrow()) + let _lhs = a.with(|__s: &S| __s.v); + _lhs / b.with(|__s: &S| __s.v) }; } pub fn operator_div_1(a: S, b: i32) -> i32 { let a: Value = Rc::new(RefCell::new(a)); let b: Value = Rc::new(RefCell::new(b)); - return (((*(*a.borrow()).v.borrow()) / (*b.borrow())) + 1); + return (({ (*a.borrow()).v } / (*b.borrow())) + 1); } pub fn operator_rem_2(a: S, b: S) -> i32 { let a: Value = Rc::new(RefCell::new(a)); let b: Value = Rc::new(RefCell::new(b)); - return ((*(*a.borrow()).v.borrow()) % (*(*b.borrow()).v.borrow())); + return ({ (*a.borrow()).v } % { (*b.borrow()).v }); } pub fn operator_rem_3(a: Ptr, b: i32) -> i32 { let b: Value = Rc::new(RefCell::new(b)); return ({ - let _lhs = (*(*a.upgrade().deref()).v.borrow()); + let _lhs = a.with(|__s: &S| __s.v); _lhs % (*b.borrow()) } + 1); } pub fn operator_eq_4(a: i32, b: S) -> i32 { let a: Value = Rc::new(RefCell::new(a)); let b: Value = Rc::new(RefCell::new(b)); - return if ((*a.borrow()) == (*(*b.borrow()).v.borrow())) { + return if ((*a.borrow()) == { (*b.borrow()).v }) { 4 } else { 0 @@ -68,7 +60,7 @@ pub fn operator_eq_5(a: i64, b: Ptr) -> i32 { let a: Value = Rc::new(RefCell::new(a)); return if { let _lhs = (*a.borrow()); - _lhs == ((*(*b.upgrade().deref()).v.borrow()) as i64) + _lhs == (b.with(|__s: &S| __s.v) as i64) } { 5 } else { @@ -80,12 +72,8 @@ pub fn main() { std::process::exit(main_0()); } fn main_0() -> i32 { - let s: Value = Rc::new(RefCell::new(S { - v: Rc::new(RefCell::new(6)), - })); - let t: Value = Rc::new(RefCell::new(S { - v: Rc::new(RefCell::new(4)), - })); + let s: Value = Rc::new(RefCell::new(S { v: 6 })); + let t: Value = Rc::new(RefCell::new(S { v: 4 })); assert!((({ SImpl::operator_eq_1(&s.as_pointer(), 6,) }) == 1)); assert!((({ SImpl::operator_eq_2(&s.as_pointer(), 6_i64,) }) == 2)); assert!((({ SImpl::operator_eq_3(&s.as_pointer(), 6.0E+0,) }) == 3)); @@ -134,7 +122,7 @@ pub trait SImpl { impl SImpl for Ptr { fn operator_eq_1(&self, o: i32) -> i32 { let o: Value = Rc::new(RefCell::new(o)); - return if ((*(*(*self).upgrade().deref()).v.borrow()) == (*o.borrow())) { + return if ((*self).with(|__s: &S| __s.v) == (*o.borrow())) { 1 } else { 0 @@ -142,7 +130,7 @@ impl SImpl for Ptr { } fn operator_eq_2(&self, o: i64) -> i32 { let o: Value = Rc::new(RefCell::new(o)); - return if (((*(*(*self).upgrade().deref()).v.borrow()) as i64) == (*o.borrow())) { + return if (((*self).with(|__s: &S| __s.v) as i64) == (*o.borrow())) { 2 } else { 0 @@ -150,7 +138,7 @@ impl SImpl for Ptr { } fn operator_eq_3(&self, o: f64) -> i32 { let o: Value = Rc::new(RefCell::new(o)); - return if (((*(*(*self).upgrade().deref()).v.borrow()) as f64) == (*o.borrow())) { + return if (((*self).with(|__s: &S| __s.v) as f64) == (*o.borrow())) { 3 } else { 0 @@ -158,23 +146,23 @@ impl SImpl for Ptr { } fn operator_add(&self, o: Ptr) -> i32 { return { - let _lhs = (*(*(*self).upgrade().deref()).v.borrow()); - _lhs + (*(*o.upgrade().deref()).v.borrow()) + let _lhs = (*self).with(|__s: &S| __s.v); + _lhs + o.with(|__s: &S| __s.v) }; } fn operator_sub(&self, o: S) -> i32 { let o: Value = Rc::new(RefCell::new(o)); - return ((*(*(*self).upgrade().deref()).v.borrow()) - (*(*o.borrow()).v.borrow())); + return ((*self).with(|__s: &S| __s.v) - { (*o.borrow()).v }); } fn operator_mul_6(&self, o: Ptr) -> i32 { return { - let _lhs = (*(*(*self).upgrade().deref()).v.borrow()); - _lhs * (*(*o.upgrade().deref()).v.borrow()) + let _lhs = (*self).with(|__s: &S| __s.v); + _lhs * o.with(|__s: &S| __s.v) }; } fn operator_mul_7(&self, o: i32) -> i32 { let o: Value = Rc::new(RefCell::new(o)); - return (((*(*(*self).upgrade().deref()).v.borrow()) * (*o.borrow())) + 1); + return (((*self).with(|__s: &S| __s.v) * (*o.borrow())) + 1); } } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/operator_qualifiers.rs b/tests/unit/out/refcount/operator_qualifiers.rs index e77493d488..f50ed3db78 100644 --- a/tests/unit/out/refcount/operator_qualifiers.rs +++ b/tests/unit/out/refcount/operator_qualifiers.rs @@ -6,29 +6,21 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct S { - pub v: Value, -} -impl Clone for S { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*self.v.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub v: i32, } impl ByteRepr for S { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.v.borrow()).to_bytes(&mut buf[0..4]); + self.v.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - v: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + v: ::from_bytes(&buf[0..4]), } } } @@ -37,42 +29,20 @@ pub fn main() { std::process::exit(main_0()); } fn main_0() -> i32 { - let s: Value = Rc::new(RefCell::new(S { - v: Rc::new(RefCell::new(10)), - })); - let cs: Value = Rc::new(RefCell::new(S { - v: Rc::new(RefCell::new(10)), - })); - let vs: Value = Rc::new(RefCell::new(S { - v: Rc::new(RefCell::new(10)), - })); + let s: Value = Rc::new(RefCell::new(S { v: 10 })); + let cs: Value = Rc::new(RefCell::new(S { v: 10 })); + let vs: Value = Rc::new(RefCell::new(S { v: 10 })); assert!((({ SImpl::operator_add_1(&s.as_pointer(), 1,) }) == 11)); assert!((({ SImpl::operator_add_2(&cs.as_pointer(), 1,) }) == 12)); assert!((({ SImpl::operator_add_3(&vs.as_pointer(), 1,) }) == 13)); assert!((({ SImpl::operator_sub_4(&s.as_pointer(), 1,) }) == 9)); assert!( - (({ - SImpl::operator_sub_5( - &Rc::new(RefCell::new(S { - v: Rc::new(RefCell::new(10)), - })) - .as_pointer(), - 1, - ) - }) == 8) + (({ SImpl::operator_sub_5(&Rc::new(RefCell::new(S { v: 10 })).as_pointer(), 1,) }) == 8) ); assert!((({ SImpl::operator_mul_6(&s.as_pointer(), 3,) }) == 30)); assert!((({ SImpl::operator_mul_6(&cs.as_pointer(), 3,) }) == 30)); assert!( - (({ - SImpl::operator_mul_7( - &Rc::new(RefCell::new(S { - v: Rc::new(RefCell::new(10)), - })) - .as_pointer(), - 3, - ) - }) == 60) + (({ SImpl::operator_mul_7(&Rc::new(RefCell::new(S { v: 10 })).as_pointer(), 3,) }) == 60) ); assert!((({ SImpl::operator_index_8(&s.as_pointer(), 2,) }) == 12)); assert!((({ SImpl::operator_index_9(&cs.as_pointer(), 2,) }) == 112)); @@ -92,39 +62,39 @@ pub trait SImpl { impl SImpl for Ptr { fn operator_add_1(&self, a: i32) -> i32 { let a: Value = Rc::new(RefCell::new(a)); - return ((*(*(*self).upgrade().deref()).v.borrow()) + (*a.borrow())); + return ((*self).with(|__s: &S| __s.v) + (*a.borrow())); } fn operator_add_2(&self, a: i32) -> i32 { let a: Value = Rc::new(RefCell::new(a)); - return (((*(*(*self).upgrade().deref()).v.borrow()) + (*a.borrow())) + 1); + return (((*self).with(|__s: &S| __s.v) + (*a.borrow())) + 1); } fn operator_add_3(&self, a: i32) -> i32 { let a: Value = Rc::new(RefCell::new(a)); - return (((*(*(*self).upgrade().deref()).v.borrow()) + (*a.borrow())) + 2); + return (((*self).with(|__s: &S| __s.v) + (*a.borrow())) + 2); } fn operator_sub_4(&self, a: i32) -> i32 { let a: Value = Rc::new(RefCell::new(a)); - return ((*(*(*self).upgrade().deref()).v.borrow()) - (*a.borrow())); + return ((*self).with(|__s: &S| __s.v) - (*a.borrow())); } fn operator_sub_5(&self, a: i32) -> i32 { let a: Value = Rc::new(RefCell::new(a)); - return (((*(*(*self).upgrade().deref()).v.borrow()) - (*a.borrow())) - 1); + return (((*self).with(|__s: &S| __s.v) - (*a.borrow())) - 1); } fn operator_mul_6(&self, a: i32) -> i32 { let a: Value = Rc::new(RefCell::new(a)); - return ((*(*(*self).upgrade().deref()).v.borrow()) * (*a.borrow())); + return ((*self).with(|__s: &S| __s.v) * (*a.borrow())); } fn operator_mul_7(&self, a: i32) -> i32 { let a: Value = Rc::new(RefCell::new(a)); - return (((*(*(*self).upgrade().deref()).v.borrow()) * (*a.borrow())) * 2); + return (((*self).with(|__s: &S| __s.v) * (*a.borrow())) * 2); } fn operator_index_8(&self, i: i32) -> i32 { let i: Value = Rc::new(RefCell::new(i)); - return ((*(*(*self).upgrade().deref()).v.borrow()) + (*i.borrow())); + return ((*self).with(|__s: &S| __s.v) + (*i.borrow())); } fn operator_index_9(&self, i: i32) -> i32 { let i: Value = Rc::new(RefCell::new(i)); - return (((*(*(*self).upgrade().deref()).v.borrow()) + (*i.borrow())) + 100); + return (((*self).with(|__s: &S| __s.v) + (*i.borrow())) + 100); } } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/operator_three_way.rs b/tests/unit/out/refcount/operator_three_way.rs index 4c15a90a4b..6647d907a1 100644 --- a/tests/unit/out/refcount/operator_three_way.rs +++ b/tests/unit/out/refcount/operator_three_way.rs @@ -6,9 +6,10 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct S { - pub v: Value, + #[offset(0)] + pub v: i32, } impl std::cmp::Ord for S { fn cmp(&self, other: &Self) -> std::cmp::Ordering { @@ -36,25 +37,16 @@ impl std::cmp::PartialEq for S { } } impl std::cmp::Eq for S {} -impl Clone for S { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*self.v.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } -} impl ByteRepr for S { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.v.borrow()).to_bytes(&mut buf[0..4]); + self.v.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - v: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + v: ::from_bytes(&buf[0..4]), } } } @@ -63,12 +55,8 @@ pub fn main() { std::process::exit(main_0()); } fn main_0() -> i32 { - let a: Value = Rc::new(RefCell::new(S { - v: Rc::new(RefCell::new(1)), - })); - let b: Value = Rc::new(RefCell::new(S { - v: Rc::new(RefCell::new(2)), - })); + let a: Value = Rc::new(RefCell::new(S { v: 1 })); + let b: Value = Rc::new(RefCell::new(S { v: 2 })); assert!( ({ SImpl::operator_cmp(&a.as_pointer(), b.as_pointer(),) }) == std::cmp::Ordering::Less ); @@ -94,14 +82,14 @@ pub trait SImpl { impl SImpl for Ptr { fn operator_cmp(&self, o: Ptr) -> std::cmp::Ordering { if { - let _lhs = (*(*(*self).upgrade().deref()).v.borrow()); - _lhs < (*(*o.upgrade().deref()).v.borrow()) + let _lhs = (*self).with(|__s: &S| __s.v); + _lhs < o.with(|__s: &S| __s.v) } { return std::cmp::Ordering::Less; } if { - let _lhs = (*(*(*self).upgrade().deref()).v.borrow()); - _lhs > (*(*o.upgrade().deref()).v.borrow()) + let _lhs = (*self).with(|__s: &S| __s.v); + _lhs > o.with(|__s: &S| __s.v) } { return std::cmp::Ordering::Greater; } @@ -109,8 +97,8 @@ impl SImpl for Ptr { } fn operator_eq(&self, o: Ptr) -> bool { return { - let _lhs = (*(*(*self).upgrade().deref()).v.borrow()); - _lhs == (*(*o.upgrade().deref()).v.borrow()) + let _lhs = (*self).with(|__s: &S| __s.v); + _lhs == o.with(|__s: &S| __s.v) }; } } diff --git a/tests/unit/out/refcount/operator_traits.rs b/tests/unit/out/refcount/operator_traits.rs index 9c6c05803b..56b2a0a685 100644 --- a/tests/unit/out/refcount/operator_traits.rs +++ b/tests/unit/out/refcount/operator_traits.rs @@ -6,9 +6,10 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Lt { - pub v: Value, + #[offset(0)] + pub v: i32, } impl std::cmp::Ord for Lt { fn cmp(&self, other: &Self) -> std::cmp::Ordering { @@ -48,31 +49,23 @@ impl std::cmp::PartialEq for Lt { } } impl std::cmp::Eq for Lt {} -impl Clone for Lt { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*self.v.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } -} impl ByteRepr for Lt { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.v.borrow()).to_bytes(&mut buf[0..4]); + self.v.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - v: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + v: ::from_bytes(&buf[0..4]), } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Eq { - pub v: Value, + #[offset(0)] + pub v: i32, } impl std::cmp::PartialEq for Eq { fn eq(&self, other: &Self) -> bool { @@ -85,31 +78,23 @@ impl std::cmp::PartialEq for Eq { } } impl std::cmp::Eq for Eq {} -impl Clone for Eq { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*self.v.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } -} impl ByteRepr for Eq { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.v.borrow()).to_bytes(&mut buf[0..4]); + self.v.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - v: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + v: ::from_bytes(&buf[0..4]), } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Cmp { - pub v: Value, + #[offset(0)] + pub v: i32, } impl std::cmp::Ord for Cmp { fn cmp(&self, other: &Self) -> std::cmp::Ordering { @@ -137,31 +122,23 @@ impl std::cmp::PartialEq for Cmp { } } impl std::cmp::Eq for Cmp {} -impl Clone for Cmp { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*self.v.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } -} impl ByteRepr for Cmp { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.v.borrow()).to_bytes(&mut buf[0..4]); + self.v.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - v: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + v: ::from_bytes(&buf[0..4]), } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Free { - pub v: Value, + #[offset(0)] + pub v: i32, } impl std::cmp::Ord for Free { fn cmp(&self, other: &Self) -> std::cmp::Ordering { @@ -198,63 +175,46 @@ impl std::cmp::PartialEq for Free { } } impl std::cmp::Eq for Free {} -impl Clone for Free { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*self.v.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } -} impl ByteRepr for Free { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.v.borrow()).to_bytes(&mut buf[0..4]); + self.v.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - v: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + v: ::from_bytes(&buf[0..4]), } } } pub fn operator_lt_0(a: Ptr, b: Ptr) -> bool { return { - let _lhs = (*(*a.upgrade().deref()).v.borrow()); - _lhs < (*(*b.upgrade().deref()).v.borrow()) + let _lhs = a.with(|__s: &Free| __s.v); + _lhs < b.with(|__s: &Free| __s.v) }; } pub fn operator_eq_1(a: Ptr, b: Ptr) -> bool { return { - let _lhs = (*(*a.upgrade().deref()).v.borrow()); - _lhs == (*(*b.upgrade().deref()).v.borrow()) + let _lhs = a.with(|__s: &Free| __s.v); + _lhs == b.with(|__s: &Free| __s.v) }; } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Wrapped_int_ { - pub v: Value, -} -impl Clone for Wrapped_int_ { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*self.v.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub v: i32, } impl ByteRepr for Wrapped_int_ { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.v.borrow()).to_bytes(&mut buf[0..4]); + self.v.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - v: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + v: ::from_bytes(&buf[0..4]), } } } @@ -263,80 +223,45 @@ pub fn main() { std::process::exit(main_0()); } fn main_0() -> i32 { - let w: Value = Rc::new(RefCell::new(Wrapped_int_ { - v: Rc::new(RefCell::new(2)), - })); - assert!(((*(*w.borrow()).v.borrow()) == 2)); - let lts: Value> = Rc::new(RefCell::new(vec![ - Lt { - v: Rc::new(RefCell::new(3)), - }, - Lt { - v: Rc::new(RefCell::new(1)), - }, - Lt { - v: Rc::new(RefCell::new(2)), - }, - ])); + let w: Value = Rc::new(RefCell::new(Wrapped_int_ { v: 2 })); + assert!(({ (*w.borrow()).v } == 2)); + let lts: Value> = Rc::new(RefCell::new(vec![Lt { v: 3 }, Lt { v: 1 }, Lt { v: 2 }])); (lts.as_pointer() as Ptr).sort((lts.as_pointer() as Ptr).to_end().get_offset()); assert!( - (((*(*(lts.as_pointer() as Ptr) + (((lts.as_pointer() as Ptr) .offset(0_usize) - .upgrade() - .deref()) - .v - .borrow()) + .with(|__s: &Lt| __s.v) == 1) - && ((*(*(lts.as_pointer() as Ptr) + && ((lts.as_pointer() as Ptr) .offset(1_usize) - .upgrade() - .deref()) - .v - .borrow()) + .with(|__s: &Lt| __s.v) == 2)) - && ((*(*(lts.as_pointer() as Ptr) + && ((lts.as_pointer() as Ptr) .offset(2_usize) - .upgrade() - .deref()) - .v - .borrow()) + .with(|__s: &Lt| __s.v) == 3) ); - let eqs: Value> = Rc::new(RefCell::new(vec![ - Eq { - v: Rc::new(RefCell::new(1)), - }, - Eq { - v: Rc::new(RefCell::new(2)), - }, - Eq { - v: Rc::new(RefCell::new(3)), - }, - ])); - let two: Value = Rc::new(RefCell::new(Eq { - v: Rc::new(RefCell::new(2)), - })); - let nine: Value = Rc::new(RefCell::new(Eq { - v: Rc::new(RefCell::new(9)), - })); + let eqs: Value> = Rc::new(RefCell::new(vec![Eq { v: 1 }, Eq { v: 2 }, Eq { v: 3 }])); + let two: Value = Rc::new(RefCell::new(Eq { v: 2 })); + let nine: Value = Rc::new(RefCell::new(Eq { v: 9 })); assert!( - ((*((eqs.as_pointer() as Ptr) - .offset( - (eqs.as_pointer() as Ptr) - .clone() - .into_iter() - .enumerate() - .position(|(index_0, value_0)| { - index_0 < (eqs.as_pointer() as Ptr).to_end().get_offset() as usize - && value_0.read() == (*two.borrow()) - }) - .unwrap_or((eqs.as_pointer() as Ptr).to_end().get_offset() as usize) - as isize, - ) - .read()) - .v - .borrow()) - == 2) + ({ + ((eqs.as_pointer() as Ptr) + .offset( + (eqs.as_pointer() as Ptr) + .clone() + .into_iter() + .enumerate() + .position(|(index_0, value_0)| { + index_0 < (eqs.as_pointer() as Ptr).to_end().get_offset() as usize + && value_0.read() == (*two.borrow()) + }) + .unwrap_or((eqs.as_pointer() as Ptr).to_end().get_offset() as usize) + as isize, + ) + .read()) + .v + } == 2) ); assert!( (eqs.as_pointer() as Ptr).offset( @@ -352,113 +277,81 @@ fn main_0() -> i32 { as isize, ) == (eqs.as_pointer() as Ptr).to_end() ); - let cmps: Value> = Rc::new(RefCell::new(vec![ - Cmp { - v: Rc::new(RefCell::new(3)), - }, - Cmp { - v: Rc::new(RefCell::new(1)), - }, - Cmp { - v: Rc::new(RefCell::new(2)), - }, - ])); + let cmps: Value> = + Rc::new(RefCell::new(vec![Cmp { v: 3 }, Cmp { v: 1 }, Cmp { v: 2 }])); (cmps.as_pointer() as Ptr).sort((cmps.as_pointer() as Ptr).to_end().get_offset()); assert!( - ((*(*(cmps.as_pointer() as Ptr) + ((cmps.as_pointer() as Ptr) .offset(0_usize) - .upgrade() - .deref()) - .v - .borrow()) + .with(|__s: &Cmp| __s.v) == 1) - && ((*(*(cmps.as_pointer() as Ptr) + && ((cmps.as_pointer() as Ptr) .offset(2_usize) - .upgrade() - .deref()) - .v - .borrow()) + .with(|__s: &Cmp| __s.v) == 3) ); - let three: Value = Rc::new(RefCell::new(Cmp { - v: Rc::new(RefCell::new(3)), - })); + let three: Value = Rc::new(RefCell::new(Cmp { v: 3 })); assert!( - ((*((cmps.as_pointer() as Ptr) - .offset( - (cmps.as_pointer() as Ptr) - .clone() - .into_iter() - .enumerate() - .position(|(index_0, value_0)| { - index_0 < (cmps.as_pointer() as Ptr).to_end().get_offset() as usize - && value_0.read() == (*three.borrow()) - }) - .unwrap_or((cmps.as_pointer() as Ptr).to_end().get_offset() as usize) - as isize, - ) - .read()) - .v - .borrow()) - == 3) + ({ + ((cmps.as_pointer() as Ptr) + .offset( + (cmps.as_pointer() as Ptr) + .clone() + .into_iter() + .enumerate() + .position(|(index_0, value_0)| { + index_0 < (cmps.as_pointer() as Ptr).to_end().get_offset() as usize + && value_0.read() == (*three.borrow()) + }) + .unwrap_or((cmps.as_pointer() as Ptr).to_end().get_offset() as usize) + as isize, + ) + .read()) + .v + } == 3) ); - let frees: Value> = Rc::new(RefCell::new(vec![ - Free { - v: Rc::new(RefCell::new(2)), - }, - Free { - v: Rc::new(RefCell::new(1)), - }, - ])); + let frees: Value> = Rc::new(RefCell::new(vec![Free { v: 2 }, Free { v: 1 }])); (frees.as_pointer() as Ptr).sort((frees.as_pointer() as Ptr).to_end().get_offset()); assert!( - ((*(*(frees.as_pointer() as Ptr) + ((frees.as_pointer() as Ptr) .offset(0_usize) - .upgrade() - .deref()) - .v - .borrow()) + .with(|__s: &Free| __s.v) == 1) ); - let ftwo: Value = Rc::new(RefCell::new(Free { - v: Rc::new(RefCell::new(2)), - })); + let ftwo: Value = Rc::new(RefCell::new(Free { v: 2 })); assert!( - ((*((frees.as_pointer() as Ptr) - .offset( - (frees.as_pointer() as Ptr) - .clone() - .into_iter() - .enumerate() - .position(|(index_0, value_0)| { - index_0 < (frees.as_pointer() as Ptr).to_end().get_offset() as usize - && value_0.read() == (*ftwo.borrow()) - }) - .unwrap_or((frees.as_pointer() as Ptr).to_end().get_offset() as usize) - as isize, - ) - .read()) - .v - .borrow()) - == 2) + ({ + ((frees.as_pointer() as Ptr) + .offset( + (frees.as_pointer() as Ptr) + .clone() + .into_iter() + .enumerate() + .position(|(index_0, value_0)| { + index_0 + < (frees.as_pointer() as Ptr).to_end().get_offset() as usize + && value_0.read() == (*ftwo.borrow()) + }) + .unwrap_or((frees.as_pointer() as Ptr).to_end().get_offset() as usize) + as isize, + ) + .read()) + .v + } == 2) ); 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)), - }) - .or_insert_with(|| Rc::new(RefCell::new(::default()))) - .as_pointer() + __v.entry(Lt { v: 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)), - }) - .or_insert_with(|| Rc::new(RefCell::new(::default()))) - .as_pointer() + __v.entry(Lt { v: 1 }) + .or_insert_with(|| Rc::new(RefCell::new(::default()))) + .as_pointer() }) .write(10); assert!( @@ -470,11 +363,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)), - }) - .or_insert_with(|| Rc::new(RefCell::new(::default()))) - .as_pointer() + __v.entry(Lt { v: 2 }) + .or_insert_with(|| Rc::new(RefCell::new(::default()))) + .as_pointer() }) .read()) == 20) @@ -488,14 +379,14 @@ pub trait CmpImpl { impl CmpImpl for Ptr { fn operator_cmp(&self, o: Ptr) -> std::cmp::Ordering { return std::cmp::Ord::cmp( - &(*(*(*self).upgrade().deref()).v.borrow()), - &(*(*o.upgrade().deref()).v.borrow()), + &((*self).with(|__s: &Cmp| __s.v)), + &(o.with(|__s: &Cmp| __s.v)), ); } fn operator_eq(&self, o: Ptr) -> bool { return { - let _lhs = (*(*(*self).upgrade().deref()).v.borrow()); - _lhs == (*(*o.upgrade().deref()).v.borrow()) + let _lhs = (*self).with(|__s: &Cmp| __s.v); + _lhs == o.with(|__s: &Cmp| __s.v) }; } } @@ -505,8 +396,8 @@ pub trait EqImpl { impl EqImpl for Ptr { fn operator_eq(&self, o: Ptr) -> bool { return { - let _lhs = (*(*(*self).upgrade().deref()).v.borrow()); - _lhs == (*(*o.upgrade().deref()).v.borrow()) + let _lhs = (*self).with(|__s: &Eq| __s.v); + _lhs == o.with(|__s: &Eq| __s.v) }; } } @@ -516,8 +407,8 @@ pub trait LtImpl { impl LtImpl for Ptr { fn operator_lt(&self, o: Ptr) -> bool { return { - let _lhs = (*(*(*self).upgrade().deref()).v.borrow()); - _lhs < (*(*o.upgrade().deref()).v.borrow()) + let _lhs = (*self).with(|__s: &Lt| __s.v); + _lhs < o.with(|__s: &Lt| __s.v) }; } } diff --git a/tests/unit/out/refcount/overload_mangling.rs b/tests/unit/out/refcount/overload_mangling.rs index c20d0d132d..6c95505e8b 100644 --- a/tests/unit/out/refcount/overload_mangling.rs +++ b/tests/unit/out/refcount/overload_mangling.rs @@ -26,57 +26,41 @@ pub fn twice_2(n: i32) -> i32 { let n: Value = Rc::new(RefCell::new(n)); return ((*n.borrow()) * 2); } -#[derive(Clone, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] pub struct Access_S_ {} -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct S { - pub base: Value, -} -impl Clone for S { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - base: Rc::new(RefCell::new((*self.base.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub base: i32, } impl ByteRepr for S { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.base.borrow()).to_bytes(&mut buf[0..4]); + self.base.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - base: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + base: ::from_bytes(&buf[0..4]), } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Box { - pub v: Value, -} -impl Clone for Box { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*self.v.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub v: i32, } impl ByteRepr for Box { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.v.borrow()).to_bytes(&mut buf[0..4]); + self.v.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - v: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + v: ::from_bytes(&buf[0..4]), } } } @@ -85,9 +69,7 @@ pub fn main() { std::process::exit(main_0()); } fn main_0() -> i32 { - let s: Value = Rc::new(RefCell::new(S { - base: Rc::new(RefCell::new(100)), - })); + let s: Value = Rc::new(RefCell::new(S { base: 100 })); assert!((({ SImpl::width_1_char(&s.as_pointer(), 3,) }) == 103)); assert!((({ SImpl::width_1_int(&s.as_pointer(), 3,) }) == 112)); assert!((({ SImpl::scale_2_2(&s.as_pointer(), 5,) }) == 110)); @@ -181,10 +163,8 @@ fn main_0() -> i32 { Access_S_Impl::ref_4(&a.as_pointer(), _r) }) == 103) ); - let b: Value = Rc::new(RefCell::new(Box { - v: Rc::new(RefCell::new(4)), - })); - assert!(((*(*b.borrow()).v.borrow()) == 4)); + let b: Value = Rc::new(RefCell::new(Box { v: 4 })); + assert!(({ (*b.borrow()).v } == 4)); return 0; } pub trait Access_S_Impl { @@ -196,17 +176,17 @@ pub trait Access_S_Impl { impl Access_S_Impl for Ptr { fn get_1(&self, p: Ptr) -> i32 { let p: Value> = Rc::new(RefCell::new(p)); - return (*(*(*p.borrow()).upgrade().deref()).base.borrow()); + return (*p.borrow()).with(|__s: &S| __s.base); } fn get_2(&self, p: Ptr) -> i32 { let p: Value> = Rc::new(RefCell::new(p)); - return ((*(*(*p.borrow()).upgrade().deref()).base.borrow()) + 1); + return ((*p.borrow()).with(|__s: &S| __s.base) + 1); } fn ref_3(&self, r: Ptr) -> i32 { - return ((*(*r.upgrade().deref()).base.borrow()) + 2); + return (r.with(|__s: &S| __s.base) + 2); } fn ref_4(&self, r: Ptr) -> i32 { - return ((*(*r.upgrade().deref()).base.borrow()) + 3); + return (r.with(|__s: &S| __s.base) + 3); } } pub trait SImpl { @@ -243,50 +223,49 @@ pub trait SImpl { impl SImpl for Ptr { fn plain_4(&self, x: i32) -> i32 { let x: Value = Rc::new(RefCell::new(x)); - return ((*(*(*self).upgrade().deref()).base.borrow()) + (*x.borrow())); + return ((*self).with(|__s: &S| __s.base) + (*x.borrow())); } fn plain_5(&self, x: i64) -> i32 { let x: Value = Rc::new(RefCell::new(x)); - return (((*(*(*self).upgrade().deref()).base.borrow()) + ((*x.borrow()) as i32)) + 1); + return (((*self).with(|__s: &S| __s.base) + ((*x.borrow()) as i32)) + 1); } fn take_6(&self, x: Ptr) -> i32 { return ({ - let _lhs = (*(*(*self).upgrade().deref()).base.borrow()); + let _lhs = (*self).with(|__s: &S| __s.base); _lhs + (x.read()) } + 1); } fn take_7(&self, x: Ptr) -> i32 { return ({ - let _lhs = (*(*(*self).upgrade().deref()).base.borrow()); + let _lhs = (*self).with(|__s: &S| __s.base); _lhs + (x.read()) } + 2); } fn pick_8(&self, p: (Value, Value)) -> i32 { let p: Value<(Value, Value)> = Rc::new(RefCell::new(p)); - return ((*(*(*self).upgrade().deref()).base.borrow()) + (*(*p.borrow()).0.borrow())); + return ((*self).with(|__s: &S| __s.base) + (*(*p.borrow()).0.borrow())); } fn pick_9(&self, p: (Value, Value)) -> i32 { let p: Value<(Value, Value)> = Rc::new(RefCell::new(p)); - return ((*(*(*self).upgrade().deref()).base.borrow()) - + ((*(*p.borrow()).1.borrow()) as i32)); + return ((*self).with(|__s: &S| __s.base) + ((*(*p.borrow()).1.borrow()) as i32)); } fn apply_10(&self, f: FnPtr)>, x: i32) -> i32 { let f: Value)>> = Rc::new(RefCell::new(f)); let x: Value = Rc::new(RefCell::new(x)); ({ (*f.borrow()).call((x.as_pointer())) }); - return ((*(*(*self).upgrade().deref()).base.borrow()) + (*x.borrow())); + return ((*self).with(|__s: &S| __s.base) + (*x.borrow())); } fn apply_11(&self, f: FnPtr, i32)>, x: i32) -> i32 { let f: Value, i32)>> = Rc::new(RefCell::new(f)); let x: Value = Rc::new(RefCell::new(x)); ({ (*f.borrow()).call((x.as_pointer()), 10) }); - return ((*(*(*self).upgrade().deref()).base.borrow()) + (*x.borrow())); + return ((*self).with(|__s: &S| __s.base) + (*x.borrow())); } fn apply_12(&self, f: FnPtr i32>, x: i32) -> i32 { let f: Value i32>> = Rc::new(RefCell::new(f)); let x: Value = Rc::new(RefCell::new(x)); return { - let _lhs = (*(*(*self).upgrade().deref()).base.borrow()); + let _lhs = (*self).with(|__s: &S| __s.base); _lhs + ({ (*f.borrow()).call((*x.borrow())) }) }; } @@ -303,7 +282,7 @@ impl SImpl for Ptr { return { let _lhs = { let _lhs = { - let _lhs = (*(*(*self).upgrade().deref()).base.borrow()); + let _lhs = (*self).with(|__s: &S| __s.base); _lhs + ((*(*p.upgrade().deref()).1.borrow()) as i32) }; _lhs + ({ (*f.borrow()).call(((*q.borrow()).read())) }) @@ -328,7 +307,7 @@ impl SImpl for Ptr { }); return { let _lhs = { - let _lhs = (*(*(*self).upgrade().deref()).base.borrow()); + let _lhs = (*self).with(|__s: &S| __s.base); _lhs + (*(*p.upgrade().deref()).0.borrow()) }; _lhs + ((*q.borrow()).read()) @@ -336,29 +315,29 @@ impl SImpl for Ptr { } fn width_1_char(&self, x: i32) -> i32 { let x: Value = Rc::new(RefCell::new(x)); - return ((*(*(*self).upgrade().deref()).base.borrow()) + return ((*self).with(|__s: &S| __s.base) + ((*x.borrow()) * (::std::mem::size_of::() as i32))); } fn width_1_int(&self, x: i32) -> i32 { let x: Value = Rc::new(RefCell::new(x)); - return ((*(*(*self).upgrade().deref()).base.borrow()) + return ((*self).with(|__s: &S| __s.base) + ((*x.borrow()) * (::std::mem::size_of::() as i32))); } fn scale_2_2(&self, x: i32) -> i32 { let x: Value = Rc::new(RefCell::new(x)); - return ((*(*(*self).upgrade().deref()).base.borrow()) + ((*x.borrow()) * 2)); + return ((*self).with(|__s: &S| __s.base) + ((*x.borrow()) * 2)); } fn scale_2_3(&self, x: i32) -> i32 { let x: Value = Rc::new(RefCell::new(x)); - return ((*(*(*self).upgrade().deref()).base.borrow()) + ((*x.borrow()) * 3)); + return ((*self).with(|__s: &S| __s.base) + ((*x.borrow()) * 3)); } fn count_3(&self, x: i32) -> i32 { let x: Value = Rc::new(RefCell::new(x)); - return (((*(*(*self).upgrade().deref()).base.borrow()) + (*x.borrow())) + (0 as i32)); + return (((*self).with(|__s: &S| __s.base) + (*x.borrow())) + (0 as i32)); } fn count_3_int_long(&self, x: i32) -> i32 { let x: Value = Rc::new(RefCell::new(x)); - return (((*(*(*self).upgrade().deref()).base.borrow()) + (*x.borrow())) + (2 as i32)); + return (((*self).with(|__s: &S| __s.base) + (*x.borrow())) + (2 as i32)); } } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/pod.rs b/tests/unit/out/refcount/pod.rs index 238585b974..89e7a78bd9 100644 --- a/tests/unit/out/refcount/pod.rs +++ b/tests/unit/out/refcount/pod.rs @@ -6,44 +6,36 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct POD { - pub x1: Value, - pub x2: Value, - pub x3: Value, -} -impl Clone for POD { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - x1: Rc::new(RefCell::new((*self.x1.borrow()))), - x2: Rc::new(RefCell::new((*self.x2.borrow()))), - x3: Rc::new(RefCell::new((*self.x3.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub x1: i32, + #[offset(4)] + pub x2: i32, + #[offset(8)] + pub x3: i32, } impl ByteRepr for POD { fn byte_size() -> usize { 12 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.x1.borrow()).to_bytes(&mut buf[0..4]); - (*self.x2.borrow()).to_bytes(&mut buf[4..8]); - (*self.x3.borrow()).to_bytes(&mut buf[8..12]); + self.x1.to_bytes(&mut buf[0..4]); + self.x2.to_bytes(&mut buf[4..8]); + self.x3.to_bytes(&mut buf[8..12]); } fn from_bytes(buf: &[u8]) -> Self { Self { - x1: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - x2: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), - x3: Rc::new(RefCell::new(::from_bytes(&buf[8..12]))), + x1: ::from_bytes(&buf[0..4]), + x2: ::from_bytes(&buf[4..8]), + x3: ::from_bytes(&buf[8..12]), } } } pub fn PODIncrement_0(pod: Ptr) { - (*(*pod.upgrade().deref()).x1.borrow_mut()) += 1; - (*(*pod.upgrade().deref()).x2.borrow_mut()) += 2; - (*(*pod.upgrade().deref()).x3.borrow_mut()) += 3; + pod.with_mut(|__s: &mut POD| __s.x1 += 1); + pod.with_mut(|__s: &mut POD| __s.x2 += 2); + pod.with_mut(|__s: &mut POD| __s.x3 += 3); } pub fn main() { __cpp2rust_init_globals(); @@ -51,21 +43,17 @@ pub fn main() { } fn main_0() -> i32 { let p1: Value = Rc::new(RefCell::new(POD { - x1: Rc::new(RefCell::new(10)), - x2: Rc::new(RefCell::new(11)), - x3: Rc::new(RefCell::new(12)), + x1: 10, + x2: 11, + x3: 12, })); let p2: Value = Rc::new(RefCell::new(POD { - x1: Rc::new(RefCell::new((*(*p1.borrow()).x1.borrow()))), - x2: Rc::new(RefCell::new((*(*p1.borrow()).x2.borrow()))), - x3: Rc::new(RefCell::new((*(*p1.borrow()).x3.borrow()))), + x1: { (*p1.borrow()).x1 }, + x2: { (*p1.borrow()).x2 }, + x3: { (*p1.borrow()).x3 }, })); ({ PODIncrement_0(p2.as_pointer()) }); - assert!( - ((((*(*p2.borrow()).x1.borrow()) + (*(*p2.borrow()).x2.borrow())) - + (*(*p2.borrow()).x3.borrow())) - == 39) - ); + assert!(((({ (*p2.borrow()).x1 } + { (*p2.borrow()).x2 }) + { (*p2.borrow()).x3 }) == 39)); return 0; } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/pointer_array.rs b/tests/unit/out/refcount/pointer_array.rs index 56033a1cf2..58fcf3c35d 100644 --- a/tests/unit/out/refcount/pointer_array.rs +++ b/tests/unit/out/refcount/pointer_array.rs @@ -6,29 +6,17 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg)] +#[derive(Clone, Record, VaArg, FnPtrArg)] pub struct StackArray { - pub arr: Value]>>, -} -impl Clone for StackArray { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - arr: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 3, _>( - |__i: usize| ((*self.arr.borrow())[(__i) as usize]).clone(), - )))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub arr: Box<[Ptr]>, } impl Default for StackArray { fn default() -> Self { StackArray { - arr: Rc::new(RefCell::new( - (0..3) - .map(|_| Ptr::::null()) - .collect::]>>(), - )), + arr: (0..3) + .map(|_| Ptr::::null()) + .collect::]>>(), } } } @@ -37,11 +25,11 @@ impl ByteRepr for StackArray { 24 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.arr.borrow()).to_bytes(&mut buf[0..24]); + self.arr.to_bytes(&mut buf[0..24]); } fn from_bytes(buf: &[u8]) -> Self { Self { - arr: Rc::new(RefCell::new(]>>::from_bytes(&buf[0..24]))), + arr: ]>>::from_bytes(&buf[0..24]), } } } @@ -49,7 +37,7 @@ pub fn IncrementAll_0(s: Ptr) { let i: Value = Rc::new(RefCell::new(0)); 'loop_: while ((*i.borrow()) < 3) { { - let _ptr = (*(*s.upgrade().deref()).arr.borrow())[(*i.borrow()) as usize].clone(); + let _ptr = (*s.upgrade().deref()).arr[(*i.borrow()) as usize].clone(); _ptr.write(_ptr.read() + 1) }; (*i.borrow_mut()).prefix_inc(); @@ -62,11 +50,7 @@ pub fn main() { fn main_0() -> i32 { let x: Value = Rc::new(RefCell::new(0)); let s: Value = Rc::new(RefCell::new(StackArray { - arr: Rc::new(RefCell::new(Box::new([ - (x.as_pointer()), - (x.as_pointer()), - (x.as_pointer()), - ]))), + arr: Box::new([(x.as_pointer()), (x.as_pointer()), (x.as_pointer())]), })); ({ IncrementAll_0(s.as_pointer()) }); assert!(((*x.borrow()) == 3)); diff --git a/tests/unit/out/refcount/pointers.rs b/tests/unit/out/refcount/pointers.rs index ef7754079e..31a1306d53 100644 --- a/tests/unit/out/refcount/pointers.rs +++ b/tests/unit/out/refcount/pointers.rs @@ -6,29 +6,21 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Test { - pub x: Value, -} -impl Clone for Test { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - x: Rc::new(RefCell::new((*self.x.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub x: i32, } impl ByteRepr for Test { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.x.borrow()).to_bytes(&mut buf[0..4]); + self.x.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - x: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + x: ::from_bytes(&buf[0..4]), } } } @@ -38,8 +30,8 @@ pub fn Update_0(t: Ptr) -> Ptr { let y: Value = Rc::new(RefCell::new(2)); (*x.borrow_mut()).prefix_inc(); ({ TestImpl::update(&(*t.borrow()), (*x.borrow()), (*y.borrow())) }); - (*x.borrow_mut()) = (*(*(*t.borrow()).upgrade().deref()).x.borrow()); - (*y.borrow_mut()) = (*(*(*t.borrow()).upgrade().deref()).x.borrow()); + (*x.borrow_mut()) = (*t.borrow()).with(|__s: &Test| __s.x); + (*y.borrow_mut()) = (*t.borrow()).with(|__s: &Test| __s.x); ({ let _x: i32 = (*x.borrow()); let _y: i32 = (*y.borrow()); @@ -52,13 +44,11 @@ pub fn main() { std::process::exit(main_0()); } fn main_0() -> i32 { - let t1: Value = Rc::new(RefCell::new(Test { - x: Rc::new(RefCell::new(100)), - })); + let t1: Value = Rc::new(RefCell::new(Test { x: 100 })); let t2: Value> = Rc::new(RefCell::new(({ Update_0((t1.as_pointer())) }))); let t3: Value> = Rc::new(RefCell::new(Ptr::::null())); (*t3.borrow_mut()) = (*t2.borrow()).clone(); - (*(*(*t3.borrow()).upgrade().deref()).x.borrow_mut()) = 15; + (*t3.borrow()).with_mut(|__s: &mut Test| __s.x = 15); { let _ptr = ({ TestImpl::as_ptr(&(*t3.borrow())) }); _ptr.write(_ptr.read() + 10) @@ -66,10 +56,10 @@ fn main_0() -> i32 { assert!( ({ let _lhs = { - let _lhs = (*(*(*t3.borrow()).upgrade().deref()).x.borrow()); - _lhs + (*(*(*t2.borrow()).upgrade().deref()).x.borrow()) + let _lhs = (*t3.borrow()).with(|__s: &Test| __s.x); + _lhs + (*t2.borrow()).with(|__s: &Test| __s.x) }; - _lhs + (*(*t1.borrow()).x.borrow()) + _lhs + { (*t1.borrow()).x } } == 75) ); return 0; @@ -82,18 +72,19 @@ pub trait TestImpl { } impl TestImpl for Ptr { fn inc(&self) { - (*(*(*self).upgrade().deref()).x.borrow_mut()).postfix_inc(); + (*self).with_mut(|__s: &mut Test| __s.x.postfix_inc()); } fn dec(&self) { - (*(*(*self).upgrade().deref()).x.borrow_mut()).postfix_dec(); + (*self).with_mut(|__s: &mut Test| __s.x.postfix_dec()); } fn as_ptr(&self) -> Ptr { - return ((*(*self).upgrade().deref()).x.as_pointer()); + return (field_ptr!((*self), x)); } fn update(&self, x: i32, y: i32) { let x: Value = Rc::new(RefCell::new(x)); let y: Value = Rc::new(RefCell::new(y)); - (*(*(*self).upgrade().deref()).x.borrow_mut()) = ((*x.borrow()) + (*y.borrow())); + let __rhs = ((*x.borrow()) + (*y.borrow())); + (*self).with_mut(|__s: &mut Test| __s.x = __rhs); } } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/polymorphism.rs b/tests/unit/out/refcount/polymorphism.rs index e7b9237e65..34e9b6051c 100644 --- a/tests/unit/out/refcount/polymorphism.rs +++ b/tests/unit/out/refcount/polymorphism.rs @@ -9,9 +9,9 @@ use std::rc::{Rc, Weak}; pub trait Animal { fn bark(&self) -> bool; } -#[derive(Clone, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] pub struct Dog {} -#[derive(Clone, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] pub struct Cat {} pub fn main() { __cpp2rust_init_globals(); diff --git a/tests/unit/out/refcount/printfs.rs b/tests/unit/out/refcount/printfs.rs index d5d8dadf71..975b7a2a70 100644 --- a/tests/unit/out/refcount/printfs.rs +++ b/tests/unit/out/refcount/printfs.rs @@ -27,7 +27,7 @@ fn main_0() -> i32 { println!("{}", Ptr::::from_string_literal(b"fprintf stdout")); println!("{} {} {}", 1, 2_u32, 3_i64); print!("hello world"); - let in_: Value> = Rc::new(RefCell::new((libcc2rs::c_stdin()).clone())); + let in_: Value> = Rc::new(RefCell::new(libcc2rs::c_stdin())); assert!(!((*in_.borrow()).is_null())); println!("{}", Ptr::::from_string_literal(b"printf")); print!("hello world"); diff --git a/tests/unit/out/refcount/ptr_cast_init.rs b/tests/unit/out/refcount/ptr_cast_init.rs index d0c94849ce..4307f673b2 100644 --- a/tests/unit/out/refcount/ptr_cast_init.rs +++ b/tests/unit/out/refcount/ptr_cast_init.rs @@ -6,55 +6,43 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct header { - pub tag: Value, - pub size: Value, -} -impl Clone for header { - fn clone(&self) -> Self { - Self { - tag: Rc::new(RefCell::new((*self.tag.borrow()).clone())), - size: Rc::new(RefCell::new((*self.size.borrow()).clone())), - } - } + #[offset(0)] + pub tag: i32, + #[offset(4)] + pub size: i32, } impl ByteRepr for header { fn byte_size() -> usize { 8 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.tag.borrow()).to_bytes(&mut buf[0..4]); - (*self.size.borrow()).to_bytes(&mut buf[4..8]); + self.tag.to_bytes(&mut buf[0..4]); + self.size.to_bytes(&mut buf[4..8]); } fn from_bytes(buf: &[u8]) -> Self { Self { - tag: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - size: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + tag: ::from_bytes(&buf[0..4]), + size: ::from_bytes(&buf[4..8]), } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct view { - pub tag: Value, -} -impl Clone for view { - fn clone(&self) -> Self { - Self { - tag: Rc::new(RefCell::new((*self.tag.borrow()).clone())), - } - } + #[offset(0)] + pub tag: i32, } impl ByteRepr for view { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.tag.borrow()).to_bytes(&mut buf[0..4]); + self.tag.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - tag: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + tag: ::from_bytes(&buf[0..4]), } } } @@ -68,13 +56,10 @@ fn main_0() -> i32 { let u: Value> = Rc::new(RefCell::new((*cp.borrow()).reinterpret_cast::())); assert!(((((((*u.borrow()).offset((0) as isize).read()) as i32) == ('h' as i32)) as i32) != 0)); assert!(((((((*u.borrow()).offset((1) as isize).read()) as i32) == ('i' as i32)) as i32) != 0)); - let h: Value
= Rc::new(RefCell::new(header { - tag: Rc::new(RefCell::new(7)), - size: Rc::new(RefCell::new(32)), - })); + let h: Value
= Rc::new(RefCell::new(header { tag: 7, size: 32 })); let hp: Value> = Rc::new(RefCell::new((h.as_pointer()))); let v: Value> = Rc::new(RefCell::new((*hp.borrow()).reinterpret_cast::())); - assert!(((((*(*(*v.borrow()).upgrade().deref()).tag.borrow()) == 7) as i32) != 0)); + assert!(((((*v.borrow()).with(|__s: &view| __s.tag) == 7) as i32) != 0)); let data: Value> = Rc::new(RefCell::new(Box::from(*b"hi\0"))); let vp: Value = Rc::new(RefCell::new( ((data.as_pointer() as Ptr) as Ptr).to_any(), diff --git a/tests/unit/out/refcount/ptr_to_incomplete_struct.rs b/tests/unit/out/refcount/ptr_to_incomplete_struct.rs index 7254ed0a1d..42e49255b1 100644 --- a/tests/unit/out/refcount/ptr_to_incomplete_struct.rs +++ b/tests/unit/out/refcount/ptr_to_incomplete_struct.rs @@ -11,7 +11,7 @@ pub fn main() { std::process::exit(main_0()); } fn main_0() -> i32 { - let fp: Value> = Rc::new(RefCell::new((libcc2rs::c_stdout()).clone())); + let fp: Value> = Rc::new(RefCell::new(libcc2rs::c_stdout())); let p: Value = Rc::new(RefCell::new((*fp.borrow()).to_any())); let fp2: Value> = Rc::new(RefCell::new((*p.borrow()).reinterpret_cast::())); assert!( diff --git a/tests/unit/out/refcount/push_emplace_back.rs b/tests/unit/out/refcount/push_emplace_back.rs index 6c09b80512..e0c622281c 100644 --- a/tests/unit/out/refcount/push_emplace_back.rs +++ b/tests/unit/out/refcount/push_emplace_back.rs @@ -6,88 +6,96 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Chunk { - pub data: Value, -} -impl Clone for Chunk { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - data: Rc::new(RefCell::new((*self.data.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub data: i32, } impl ByteRepr for Chunk { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.data.borrow()).to_bytes(&mut buf[0..4]); + self.data.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - data: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + data: ::from_bytes(&buf[0..4]), } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Writer { - pub output: Value>>, - pub chunk: Value, -} -impl Clone for Writer { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - output: Rc::new(RefCell::new((*self.output.borrow()).clone())), - chunk: Rc::new(RefCell::new((*self.chunk.borrow()).clone())), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub output: Ptr>, + #[offset(8)] + pub chunk: Chunk, } impl ByteRepr for Writer { fn byte_size() -> usize { 16 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.output.borrow()).to_bytes(&mut buf[0..8]); - (*self.chunk.borrow()).to_bytes(&mut buf[8..12]); + self.output.to_bytes(&mut buf[0..8]); + self.chunk.to_bytes(&mut buf[8..12]); } fn from_bytes(buf: &[u8]) -> Self { Self { - output: Rc::new(RefCell::new(>>::from_bytes(&buf[0..8]))), - chunk: Rc::new(RefCell::new(::from_bytes(&buf[8..12]))), + output: >>::from_bytes(&buf[0..8]), + chunk: ::from_bytes(&buf[8..12]), } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Record, VaArg, FnPtrArg, Default)] pub struct JPEGData { + #[offset(0)] pub com_data: Value>>>, + #[offset(24)] pub app_data: Value>>>, } impl Clone for JPEGData { fn clone(&self) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - com_data: Rc::new(RefCell::new( - (*self.com_data.borrow()) - .iter() - .map(|inner_vec| Rc::new(RefCell::new(inner_vec.borrow().clone()))) - .collect(), - )), - app_data: Rc::new(RefCell::new( - (*self.app_data.borrow()) - .iter() - .map(|inner_vec| Rc::new(RefCell::new(inner_vec.borrow().clone()))) - .collect(), - )), + com_data: { + Rc::new(RefCell::new( + (*self.com_data.borrow()) + .iter() + .map(|inner_vec| Rc::new(RefCell::new(inner_vec.borrow().clone()))) + .collect(), + )) + }, + app_data: { + Rc::new(RefCell::new( + (*self.app_data.borrow()) + .iter() + .map(|inner_vec| Rc::new(RefCell::new(inner_vec.borrow().clone()))) + .collect(), + )) + }, })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } } +<<<<<<< HEAD impl ByteRepr for JPEGData {} +======= +impl ByteRepr for JPEGData { + fn byte_size() -> usize { + 48 + } + fn to_bytes(&self, buf: &mut [u8]) { + self.com_data.to_bytes(&mut buf[0..24]); + self.app_data.to_bytes(&mut buf[24..48]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + com_data: >>>>::from_bytes(&buf[0..24]), + app_data: >>>>::from_bytes(&buf[24..48]), + } + } +} +>>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub fn push_param_0(dest: Ptr>>>) { let dest: Value>>>> = Rc::new(RefCell::new(dest)); ((*dest.borrow()).clone() as Ptr>>>) @@ -100,9 +108,9 @@ pub fn push_local_from_field_1(jpg: Ptr, cond: bool) { let dest: Value>>>> = Rc::new(RefCell::new(Ptr::>>>::null())); if (*cond.borrow()) { - (*dest.borrow_mut()) = ((*(*jpg.borrow()).upgrade().deref()).com_data.as_pointer()); + (*dest.borrow_mut()) = ((*jpg.borrow()).with(|__s: &JPEGData| __s.com_data.as_pointer())); } else { - (*dest.borrow_mut()) = ((*(*jpg.borrow()).upgrade().deref()).app_data.as_pointer()); + (*dest.borrow_mut()) = ((*jpg.borrow()).with(|__s: &JPEGData| __s.app_data.as_pointer())); } ((*dest.borrow()).clone() as Ptr>>>).with_mut( |__v: &mut Vec>>| { @@ -124,9 +132,11 @@ pub fn shrink_through_ptr_2(comps: Ptr>) { } pub fn nested_push_move_3(bw: Ptr) { let bw: Value> = Rc::new(RefCell::new(bw)); - (*(*(*bw.borrow()).upgrade().deref()).output.borrow()).with_mut(|__v: &mut Vec| { - __v.push((*(*(*bw.borrow()).upgrade().deref()).chunk.borrow()).clone()) - }); + (*bw.borrow()) + .with(|__s: &Writer| (__s.output).clone()) + .with_mut(|__v: &mut Vec| { + __v.push(((*(*bw.borrow()).upgrade().deref()).chunk).clone()) + }); } pub fn emplace_local_from_field_4(jpg: Ptr, cond: bool) { let jpg: Value> = Rc::new(RefCell::new(jpg)); @@ -135,9 +145,9 @@ pub fn emplace_local_from_field_4(jpg: Ptr, cond: bool) { let dest: Value>>>> = Rc::new(RefCell::new(Ptr::>>>::null())); if (*cond.borrow()) { - (*dest.borrow_mut()) = ((*(*jpg.borrow()).upgrade().deref()).com_data.as_pointer()); + (*dest.borrow_mut()) = ((*jpg.borrow()).with(|__s: &JPEGData| __s.com_data.as_pointer())); } else { - (*dest.borrow_mut()) = ((*(*jpg.borrow()).upgrade().deref()).app_data.as_pointer()); + (*dest.borrow_mut()) = ((*jpg.borrow()).with(|__s: &JPEGData| __s.app_data.as_pointer())); } { let __init = { @@ -156,8 +166,9 @@ pub fn emplace_local_from_field_4(jpg: Ptr, cond: bool) { pub fn nested_emplace_move_5(bw: Ptr) { let bw: Value> = Rc::new(RefCell::new(bw)); { - let __init = (*(*(*bw.borrow()).upgrade().deref()).chunk.borrow()).clone(); - (*(*(*bw.borrow()).upgrade().deref()).output.borrow()) + let __init = ((*(*bw.borrow()).upgrade().deref()).chunk).clone(); + (*bw.borrow()) + .with(|__s: &Writer| (__s.output).clone()) .with_mut(|__v: &mut Vec| __v.push(__init)) }; } @@ -171,16 +182,18 @@ pub fn self_ref_push_6(comps: Ptr>) { (*comps.borrow()).with_mut(|__v: &mut Vec| __v.push(a0_clone)) }; } -#[derive(VaArg, FnPtrArg)] +#[derive(Clone, Record, VaArg, FnPtrArg)] pub struct Pair { - pub first: Value, - pub second: Value, + #[offset(0)] + pub first: i32, + #[offset(4)] + pub second: i32, } impl Pair { pub fn new_1() -> Self { let __this: Value = Rc::new(RefCell::new(Self { - first: Rc::new(RefCell::new(-1_i32)), - second: Rc::new(RefCell::new(-1_i32)), + first: -1_i32, + second: -1_i32, })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() @@ -188,8 +201,8 @@ impl Pair { pub fn new_2(a: i32) -> Self { let a: Value = Rc::new(RefCell::new(a)); let __this: Value = Rc::new(RefCell::new(Self { - first: Rc::new(RefCell::new((*a.borrow()))), - second: Rc::new(RefCell::new(0)), + first: (*a.borrow()), + second: 0, })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() @@ -198,18 +211,8 @@ impl Pair { let a: Value = Rc::new(RefCell::new(a)); let b: Value = Rc::new(RefCell::new(b)); let __this: Value = Rc::new(RefCell::new(Self { - first: Rc::new(RefCell::new((*a.borrow()))), - second: Rc::new(RefCell::new(((*b.borrow()) * 2))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } -} -impl Clone for Pair { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - first: Rc::new(RefCell::new((*self.first.borrow()))), - second: Rc::new(RefCell::new((*self.second.borrow()))), + first: (*a.borrow()), + second: ((*b.borrow()) * 2), })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() @@ -225,13 +228,13 @@ impl ByteRepr for Pair { 8 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.first.borrow()).to_bytes(&mut buf[0..4]); - (*self.second.borrow()).to_bytes(&mut buf[4..8]); + self.first.to_bytes(&mut buf[0..4]); + self.second.to_bytes(&mut buf[4..8]); } fn from_bytes(buf: &[u8]) -> Self { Self { - first: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - second: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + first: ::from_bytes(&buf[0..4]), + second: ::from_bytes(&buf[4..8]), } } } @@ -295,7 +298,7 @@ fn main_0() -> i32 { ({ push_local_from_field_1((jpg.as_pointer()), true) }); assert!(((*(*jpg.borrow()).com_data.borrow()).len() == 1_usize)); assert!( - ((*(((*jpg.borrow()).com_data.as_pointer() as Ptr>>) + ((*(({ (*jpg.borrow()).com_data.as_pointer() } as Ptr>>) .offset(0_usize) .upgrade() .deref() @@ -306,7 +309,7 @@ fn main_0() -> i32 { == 3_usize) ); assert!( - ((((((*jpg.borrow()).com_data.as_pointer() as Ptr>>) + ((((({ (*jpg.borrow()).com_data.as_pointer() } as Ptr>>) .offset(0_usize) .upgrade() .deref() @@ -316,7 +319,7 @@ fn main_0() -> i32 { == 1) ); assert!( - ((((((*jpg.borrow()).com_data.as_pointer() as Ptr>>) + ((((({ (*jpg.borrow()).com_data.as_pointer() } as Ptr>>) .offset(0_usize) .upgrade() .deref() @@ -326,7 +329,7 @@ fn main_0() -> i32 { == 2) ); assert!( - ((((((*jpg.borrow()).com_data.as_pointer() as Ptr>>) + ((((({ (*jpg.borrow()).com_data.as_pointer() } as Ptr>>) .offset(0_usize) .upgrade() .deref() @@ -340,23 +343,20 @@ fn main_0() -> i32 { ({ shrink_through_ptr_2((chunks.as_pointer())) }); assert!((*chunks.borrow()).is_empty()); let w: Value = Rc::new(RefCell::new(::default())); - (*(*(*w.borrow()).chunk.borrow()).data.borrow_mut()) = 42; - (*(*w.borrow()).output.borrow_mut()) = (chunks.as_pointer()); + (*w.borrow_mut()).chunk.data = 42; + (*w.borrow_mut()).output = (chunks.as_pointer()); ({ nested_push_move_3((w.as_pointer())) }); assert!(((*chunks.borrow()).len() == 1_usize)); assert!( - ((*(*(chunks.as_pointer() as Ptr) + ((chunks.as_pointer() as Ptr) .offset(0_usize) - .upgrade() - .deref()) - .data - .borrow()) + .with(|__s: &Chunk| __s.data) == 42) ); ({ emplace_local_from_field_4((jpg.as_pointer()), false) }); assert!(((*(*jpg.borrow()).app_data.borrow()).len() == 1_usize)); assert!( - ((*(((*jpg.borrow()).app_data.as_pointer() as Ptr>>) + ((*(({ (*jpg.borrow()).app_data.as_pointer() } as Ptr>>) .offset(0_usize) .upgrade() .deref() @@ -367,7 +367,7 @@ fn main_0() -> i32 { == 3_usize) ); assert!( - ((((((*jpg.borrow()).app_data.as_pointer() as Ptr>>) + ((((({ (*jpg.borrow()).app_data.as_pointer() } as Ptr>>) .offset(0_usize) .upgrade() .deref() @@ -377,7 +377,7 @@ fn main_0() -> i32 { == 1) ); assert!( - ((((((*jpg.borrow()).app_data.as_pointer() as Ptr>>) + ((((({ (*jpg.borrow()).app_data.as_pointer() } as Ptr>>) .offset(0_usize) .upgrade() .deref() @@ -387,107 +387,71 @@ fn main_0() -> i32 { == 3) ); assert!(((*(*jpg.borrow()).com_data.borrow()).len() == 1_usize)); - (*(*(*w.borrow()).chunk.borrow()).data.borrow_mut()) = 99; - (*(*w.borrow()).output.borrow_mut()) = (chunks.as_pointer()); + (*w.borrow_mut()).chunk.data = 99; + (*w.borrow_mut()).output = (chunks.as_pointer()); ({ nested_emplace_move_5((w.as_pointer())) }); assert!(((*chunks.borrow()).len() == 2_usize)); assert!( - ((*(*(chunks.as_pointer() as Ptr) + ((chunks.as_pointer() as Ptr) .offset(1_usize) - .upgrade() - .deref()) - .data - .borrow()) + .with(|__s: &Chunk| __s.data) == 99) ); ({ self_ref_push_6((chunks.as_pointer())) }); assert!(((*chunks.borrow()).len() == 3_usize)); assert!( - ((*(*(chunks.as_pointer() as Ptr) + ((chunks.as_pointer() as Ptr) .offset(2_usize) - .upgrade() - .deref()) - .data - .borrow()) + .with(|__s: &Chunk| __s.data) == 42) ); let pairs: Value> = Rc::new(RefCell::new(Vec::new())); ({ emplace_ctor_args_7((pairs.as_pointer())) }); assert!(((*pairs.borrow()).len() == 3_usize)); assert!( - ((*(*(pairs.as_pointer() as Ptr) + ((pairs.as_pointer() as Ptr) .offset(0_usize) - .upgrade() - .deref()) - .first - .borrow()) + .with(|__s: &Pair| __s.first) == -1_i32) - && ((*(*(pairs.as_pointer() as Ptr) + && ((pairs.as_pointer() as Ptr) .offset(0_usize) - .upgrade() - .deref()) - .second - .borrow()) + .with(|__s: &Pair| __s.second) == -1_i32) ); assert!( - ((*(*(pairs.as_pointer() as Ptr) + ((pairs.as_pointer() as Ptr) .offset(1_usize) - .upgrade() - .deref()) - .first - .borrow()) + .with(|__s: &Pair| __s.first) == 3) - && ((*(*(pairs.as_pointer() as Ptr) + && ((pairs.as_pointer() as Ptr) .offset(1_usize) - .upgrade() - .deref()) - .second - .borrow()) + .with(|__s: &Pair| __s.second) == 0) ); assert!( - ((*(*(pairs.as_pointer() as Ptr) + ((pairs.as_pointer() as Ptr) .offset(2_usize) - .upgrade() - .deref()) - .first - .borrow()) + .with(|__s: &Pair| __s.first) == 4) - && ((*(*(pairs.as_pointer() as Ptr) + && ((pairs.as_pointer() as Ptr) .offset(2_usize) - .upgrade() - .deref()) - .second - .borrow()) + .with(|__s: &Pair| __s.second) == 10) ); let queue: Value> = Rc::new(RefCell::new(Vec::new())); ({ emplace_deque_8((queue.as_pointer())) }); assert!( - ((*(*(queue.as_pointer() as Ptr).upgrade().deref()) - .first - .borrow()) - == 6) - && ((*(*(queue.as_pointer() as Ptr).upgrade().deref()) - .second - .borrow()) - == 14) + ((queue.as_pointer() as Ptr).with(|__s: &Pair| __s.first) == 6) + && ((queue.as_pointer() as Ptr).with(|__s: &Pair| __s.second) == 14) ); assert!( - ((*(*(queue.as_pointer() as Ptr) + ((queue.as_pointer() as Ptr) .to_last() - .upgrade() - .deref()) - .first - .borrow()) + .with(|__s: &Pair| __s.first) == -1_i32) - && ((*(*(queue.as_pointer() as Ptr) + && ((queue.as_pointer() as Ptr) .to_last() - .upgrade() - .deref()) - .second - .borrow()) + .with(|__s: &Pair| __s.second) == -1_i32) ); let values: Value> = Rc::new(RefCell::new(Vec::new())); diff --git a/tests/unit/out/refcount/random.rs b/tests/unit/out/refcount/random.rs index 42b4e5b2fd..1276be38d0 100644 --- a/tests/unit/out/refcount/random.rs +++ b/tests/unit/out/refcount/random.rs @@ -6,57 +6,47 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg)] +#[derive(Clone, Record, VaArg, FnPtrArg)] pub struct Pair { - pub x: Value, - pub y: Value, - pub a: Value>, + #[offset(0)] + pub x: i32, + #[offset(4)] + pub y: i32, + #[offset(8)] + pub a: Box<[i32]>, + #[offset(32)] pub r: Ptr, - pub p: Value>, - pub pair: Value>, - pub ap: Value]>>, -} -impl Clone for Pair { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - x: Rc::new(RefCell::new((*self.x.borrow()))), - y: Rc::new(RefCell::new((*self.y.borrow()))), - a: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 5, _>( - |__i: usize| (*self.a.borrow())[(__i) as usize], - )))), - r: (self.r).clone(), - p: Rc::new(RefCell::new((*self.p.borrow()).clone())), - pair: Rc::new(RefCell::new((*self.pair.borrow()).clone())), - ap: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 2, _>( - |__i: usize| ((*self.ap.borrow())[(__i) as usize]).clone(), - )))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(40)] + pub p: Ptr, + #[offset(48)] + pub pair: Ptr, + #[offset(56)] + pub ap: Box<[Ptr]>, } impl Default for Pair { fn default() -> Self { Pair { - x: Rc::new(RefCell::new(0_i32)), - y: Rc::new(RefCell::new(0_i32)), - a: Rc::new(RefCell::new((0..5).map(|_| 0_i32).collect::>())), + x: 0_i32, + y: 0_i32, + a: (0..5).map(|_| 0_i32).collect::>(), r: >::default(), - p: Rc::new(RefCell::new(Ptr::::null())), - pair: Rc::new(RefCell::new(Ptr::::null())), - ap: Rc::new(RefCell::new( - (0..2) - .map(|_| Ptr::::null()) - .collect::]>>(), - )), + p: Ptr::::null(), + pair: Ptr::::null(), + ap: (0..2) + .map(|_| Ptr::::null()) + .collect::]>>(), } } } -impl ByteRepr for Pair {} +impl ByteRepr for Pair { + fn byte_size() -> usize { + 72 + } +} pub fn zero_0() -> i32 { return 0; } -#[derive(Clone, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] pub struct X1 {} pub fn foo_1(x1: i32, x2: Ptr, x3: Ptr, p2: Ptr, p3: Ptr) { let x1: Value = Rc::new(RefCell::new(x1)); @@ -81,84 +71,83 @@ fn main_0() -> i32 { let res: Value = Rc::new(RefCell::new(((*x1.borrow()) + (*x2.borrow())))); (*res.borrow_mut()) = ((*x1.borrow()) + (*x2.borrow())); let y1: Value = Rc::new(RefCell::new(Pair { - x: Rc::new(RefCell::new(1)), - y: Rc::new(RefCell::new(2)), - a: Rc::new(RefCell::new(Box::new([1, 2, 3, 4, 5]))), + x: 1, + y: 2, + a: Box::new([1, 2, 3, 4, 5]), r: x1.as_pointer(), - p: Rc::new(RefCell::new(Ptr::::null())), - pair: Rc::new(RefCell::new(Ptr::::null())), - ap: Rc::new(RefCell::new(Box::new([ - Ptr::::null(), - Ptr::::null(), - ]))), + p: Ptr::::null(), + pair: Ptr::::null(), + ap: Box::new([Ptr::::null(), Ptr::::null()]), })); let y4: Value = Rc::new(RefCell::new(Pair { - x: Rc::new(RefCell::new((*(*y1.borrow()).x.borrow()))), - y: Rc::new(RefCell::new((*(*y1.borrow()).y.borrow()))), - a: Rc::new(RefCell::new(Box::new([ - (*(*y1.borrow()).a.borrow())[(0) as usize], - (*(*y1.borrow()).a.borrow())[(1) as usize], - (*(*y1.borrow()).a.borrow())[(2) as usize], - (*(*y1.borrow()).a.borrow())[(3) as usize], - (*(*y1.borrow()).a.borrow())[(4) as usize], - ]))), - r: ((*y1.borrow()).r).clone(), - p: Rc::new(RefCell::new((*(*y1.borrow()).p.borrow()).clone())), - pair: Rc::new(RefCell::new((*(*y1.borrow()).pair.borrow()).clone())), - ap: Rc::new(RefCell::new(Box::new([ - ((*(*y1.borrow()).ap.borrow())[(0) as usize]).clone(), - ((*(*y1.borrow()).ap.borrow())[(1) as usize]).clone(), - ]))), + x: { (*y1.borrow()).x }, + y: { (*y1.borrow()).y }, + a: { + Box::new([ + (*y1.borrow()).a[(0) as usize], + (*y1.borrow()).a[(1) as usize], + (*y1.borrow()).a[(2) as usize], + (*y1.borrow()).a[(3) as usize], + (*y1.borrow()).a[(4) as usize], + ]) + }, + r: { ((*y1.borrow()).r).clone() }, + p: { (*y1.borrow()).p.clone() }, + pair: { (*y1.borrow()).pair.clone() }, + ap: { + Box::new([ + ((*y1.borrow()).ap[(0) as usize]).clone(), + ((*y1.borrow()).ap[(1) as usize]).clone(), + ]) + }, })); let ry1: Ptr = y1.as_pointer(); let py1: Value> = Rc::new(RefCell::new((y1.as_pointer()))); let y2: Value = Rc::new(RefCell::new(Pair { - x: Rc::new(RefCell::new((*(*ry1.upgrade().deref()).x.borrow()))), - y: Rc::new(RefCell::new((*(*ry1.upgrade().deref()).y.borrow()))), - a: Rc::new(RefCell::new(Box::new([ - (*(*ry1.upgrade().deref()).a.borrow())[(0) as usize], - (*(*ry1.upgrade().deref()).a.borrow())[(1) as usize], - (*(*ry1.upgrade().deref()).a.borrow())[(2) as usize], - (*(*ry1.upgrade().deref()).a.borrow())[(3) as usize], - (*(*ry1.upgrade().deref()).a.borrow())[(4) as usize], - ]))), - r: ((*ry1.upgrade().deref()).r).clone(), - p: Rc::new(RefCell::new((*(*ry1.upgrade().deref()).p.borrow()).clone())), - pair: Rc::new(RefCell::new( - (*(*ry1.upgrade().deref()).pair.borrow()).clone(), - )), - ap: Rc::new(RefCell::new(Box::new([ - ((*(*ry1.upgrade().deref()).ap.borrow())[(0) as usize]).clone(), - ((*(*ry1.upgrade().deref()).ap.borrow())[(1) as usize]).clone(), - ]))), + x: { ry1.with(|__s: &Pair| __s.x) }, + y: { ry1.with(|__s: &Pair| __s.y) }, + a: { + Box::new([ + ry1.with(|__s: &Pair| __s.a[(0) as usize]), + ry1.with(|__s: &Pair| __s.a[(1) as usize]), + ry1.with(|__s: &Pair| __s.a[(2) as usize]), + ry1.with(|__s: &Pair| __s.a[(3) as usize]), + ry1.with(|__s: &Pair| __s.a[(4) as usize]), + ]) + }, + r: { ((*ry1.upgrade().deref()).r).clone() }, + p: { ry1.with(|__s: &Pair| (__s.p).clone()) }, + pair: { ry1.with(|__s: &Pair| (__s.pair).clone()) }, + ap: { + Box::new([ + ry1.with(|__s: &Pair| (__s.ap[(0) as usize]).clone()), + ry1.with(|__s: &Pair| (__s.ap[(1) as usize]).clone()), + ]) + }, })); let ry2: Ptr = (ry1).clone(); let py2: Value> = Rc::new(RefCell::new((ry1).clone())); let y3: Value = Rc::new(RefCell::new(Pair { - x: Rc::new(RefCell::new( - (*(*(*py1.borrow()).upgrade().deref()).x.borrow()), - )), - y: Rc::new(RefCell::new( - (*(*(*py1.borrow()).upgrade().deref()).y.borrow()), - )), - a: Rc::new(RefCell::new(Box::new([ - (*(*(*py1.borrow()).upgrade().deref()).a.borrow())[(0) as usize], - (*(*(*py1.borrow()).upgrade().deref()).a.borrow())[(1) as usize], - (*(*(*py1.borrow()).upgrade().deref()).a.borrow())[(2) as usize], - (*(*(*py1.borrow()).upgrade().deref()).a.borrow())[(3) as usize], - (*(*(*py1.borrow()).upgrade().deref()).a.borrow())[(4) as usize], - ]))), - r: ((*(*py1.borrow()).upgrade().deref()).r).clone(), - p: Rc::new(RefCell::new( - (*(*(*py1.borrow()).upgrade().deref()).p.borrow()).clone(), - )), - pair: Rc::new(RefCell::new( - (*(*(*py1.borrow()).upgrade().deref()).pair.borrow()).clone(), - )), - ap: Rc::new(RefCell::new(Box::new([ - ((*(*(*py1.borrow()).upgrade().deref()).ap.borrow())[(0) as usize]).clone(), - ((*(*(*py1.borrow()).upgrade().deref()).ap.borrow())[(1) as usize]).clone(), - ]))), + x: { (*py1.borrow()).with(|__s: &Pair| __s.x) }, + y: { (*py1.borrow()).with(|__s: &Pair| __s.y) }, + a: { + Box::new([ + (*py1.borrow()).with(|__s: &Pair| __s.a[(0) as usize]), + (*py1.borrow()).with(|__s: &Pair| __s.a[(1) as usize]), + (*py1.borrow()).with(|__s: &Pair| __s.a[(2) as usize]), + (*py1.borrow()).with(|__s: &Pair| __s.a[(3) as usize]), + (*py1.borrow()).with(|__s: &Pair| __s.a[(4) as usize]), + ]) + }, + r: { ((*(*py1.borrow()).upgrade().deref()).r).clone() }, + p: { (*py1.borrow()).with(|__s: &Pair| (__s.p).clone()) }, + pair: { (*py1.borrow()).with(|__s: &Pair| (__s.pair).clone()) }, + ap: { + Box::new([ + (*py1.borrow()).with(|__s: &Pair| (__s.ap[(0) as usize]).clone()), + (*py1.borrow()).with(|__s: &Pair| (__s.ap[(1) as usize]).clone()), + ]) + }, })); let ry3: Ptr = (*py1.borrow()).clone(); let py3: Value> = Rc::new(RefCell::new((*py1.borrow()).clone())); @@ -211,74 +200,55 @@ fn main_0() -> i32 { (*px1.borrow_mut()) = (c1.as_pointer()); (*px2.borrow_mut()) = (cr1).clone(); (*px3.borrow_mut()) = (*cp1.borrow()).clone(); - (*(*y1.borrow()).x.borrow_mut()) = 2; - (*(*y1.borrow()).y.borrow_mut()) = 3; - (*(*y1.borrow()).a.borrow_mut())[(0) as usize] = 100; - (*y1.borrow()).r.write(10); - (*(*y1.borrow()).p.borrow_mut()) = (*px3.borrow()).clone(); + (*y1.borrow_mut()).x = 2; + (*y1.borrow_mut()).y = 3; + (*y1.borrow_mut()).a[(0) as usize] = 100; + { (*y1.borrow()).r.clone() }.write(10); + (*y1.borrow_mut()).p = (*px3.borrow()).clone(); (*px3.borrow_mut()) = (*px2.borrow()).clone(); - (*(*y1.borrow()).pair.borrow_mut()) = (y3.as_pointer()); - (*(*(*(*y1.borrow()).pair.borrow()).upgrade().deref()) - .x - .borrow_mut()) = 100; - (*(*(*(*y1.borrow()).pair.borrow()).upgrade().deref()) - .pair - .borrow_mut()) = (y2.as_pointer()); - (*(*(*(*(*(*y1.borrow()).pair.borrow()).upgrade().deref()) - .pair - .borrow()) - .upgrade() - .deref()) - .x - .borrow_mut()) = 100; - (*(*y1.borrow()).ap.borrow_mut())[(0) as usize] = (x1.as_pointer()); - (*(*y1.borrow()).ap.borrow_mut())[(1) as usize] = (x2.as_pointer()); - (*(*y1.borrow()).ap.borrow())[(0) as usize].write(0); + (*y1.borrow_mut()).pair = (y3.as_pointer()); + { (*y1.borrow()).pair.clone() }.with_mut(|__s: &mut Pair| __s.x = 100); + let __rhs = (y2.as_pointer()); + { (*y1.borrow()).pair.clone() }.with_mut(|__s: &mut Pair| __s.pair = __rhs); + { (*y1.borrow()).pair.clone() } + .with(|__s: &Pair| (__s.pair).clone()) + .with_mut(|__s: &mut Pair| __s.x = 100); + (*y1.borrow_mut()).ap[(0) as usize] = (x1.as_pointer()); + (*y1.borrow_mut()).ap[(1) as usize] = (x2.as_pointer()); + { (*y1.borrow()).ap[(0) as usize].clone() }.write(0); (*c1.borrow_mut()) = ((*x1.borrow()) + 1); let j: Value = Rc::new(RefCell::new(0)); let new_y: Value = Rc::new(RefCell::new(Pair { - x: Rc::new(RefCell::new(1)), - y: Rc::new(RefCell::new(2)), - a: Rc::new(RefCell::new(Box::new([1, 2, 3, 4, 5]))), + x: 1, + y: 2, + a: Box::new([1, 2, 3, 4, 5]), r: j.as_pointer(), - p: Rc::new(RefCell::new(Ptr::::null())), - pair: Rc::new(RefCell::new(Ptr::::null())), - ap: Rc::new(RefCell::new(Box::new([ - Ptr::::null(), - Ptr::::null(), - ]))), + p: Ptr::::null(), + pair: Ptr::::null(), + ap: Box::new([Ptr::::null(), Ptr::::null()]), })); - let __rhs = (*(*new_y.borrow()).x.borrow()); - (*(*y1.borrow()).x.borrow_mut()) = __rhs; + let __rhs = { (*new_y.borrow()).x }; + (*y1.borrow_mut()).x = __rhs; let i: Value = Rc::new(RefCell::new(1_u32)); - (*(*y1.borrow()).a.borrow_mut())[(*i.borrow()) as usize] = -1_i32; + (*y1.borrow_mut()).a[(*i.borrow()) as usize] = -1_i32; (*x1.borrow_mut()).postfix_inc(); (*x1.borrow_mut()).prefix_inc(); - (*(*y1.borrow()).x.borrow_mut()).postfix_inc(); - (*(*(*(*y1.borrow()).pair.borrow()).upgrade().deref()) - .pair - .borrow_mut()) = (y2.as_pointer()); - (*(*(*(*(*(*y1.borrow()).pair.borrow()).upgrade().deref()) - .pair - .borrow()) - .upgrade() - .deref()) - .x - .borrow_mut()) = 10; + (*y1.borrow_mut()).x.postfix_inc(); + let __rhs = (y2.as_pointer()); + { (*y1.borrow()).pair.clone() }.with_mut(|__s: &mut Pair| __s.pair = __rhs); + { (*y1.borrow()).pair.clone() } + .with(|__s: &Pair| (__s.pair).clone()) + .with_mut(|__s: &mut Pair| __s.x = 10); ({ PairImpl::method(&y1.as_pointer()) }); - (*(*y1.borrow()).pair.borrow_mut()) = (y2.as_pointer()); - (*(*y2.borrow()).pair.borrow_mut()) = (y3.as_pointer()); + (*y1.borrow_mut()).pair = (y2.as_pointer()); + (*y2.borrow_mut()).pair = (y3.as_pointer()); ({ - PairImpl::method( - &(*(*(*(*y1.borrow()).pair.borrow()).upgrade().deref()) - .pair - .borrow()), - ) + PairImpl::method(&{ { (*y1.borrow()).pair.clone() }.with(|__s: &Pair| (__s.pair).clone()) }) }); let x: Value = Rc::new(RefCell::new(::default())); let y: Value = Rc::new(RefCell::new(::default())); - (*x1.borrow_mut()) = (({ zero_0() }) + (*(*y1.borrow()).x.borrow())); - (*(*y1.borrow()).x.borrow_mut()) = (({ zero_0() }) + 5); + (*x1.borrow_mut()) = (({ zero_0() }) + { (*y1.borrow()).x }); + (*y1.borrow_mut()).x = (({ zero_0() }) + 5); let ptr2ptr_1: Value>> = Rc::new(RefCell::new((px1.as_pointer()))); let ptr2ptr_2: Value>> = Rc::new(RefCell::new((py1.as_pointer()))); return 0; @@ -291,22 +261,22 @@ pub trait PairImpl { } impl PairImpl for Ptr { fn method(&self) { - (*(*(*self).upgrade().deref()).x.borrow_mut()).postfix_inc(); - (*(*(*self).upgrade().deref()).y.borrow_mut()).prefix_inc(); - (*(*(*self).upgrade().deref()).a.borrow_mut())[(4) as usize] = 1; - (*(*self).upgrade().deref()).r.write(1); - (*(*(*self).upgrade().deref()).p.borrow_mut()) = Ptr::::null(); - (*(*(*self).upgrade().deref()).pair.borrow_mut()) = Ptr::::null(); - (*(*(*self).upgrade().deref()).ap.borrow_mut())[(0) as usize] = Ptr::::null(); + (*self).with_mut(|__s: &mut Pair| __s.x.postfix_inc()); + (*self).with_mut(|__s: &mut Pair| __s.y.prefix_inc()); + (*self).with_mut(|__s: &mut Pair| __s.a[(4) as usize] = 1); + { (*(*self).upgrade().deref()).r.clone() }.write(1); + (*self).with_mut(|__s: &mut Pair| __s.p = Ptr::::null()); + (*self).with_mut(|__s: &mut Pair| __s.pair = Ptr::::null()); + (*self).with_mut(|__s: &mut Pair| __s.ap[(0) as usize] = Ptr::::null()); } fn as_val(&self) -> i32 { - return (*(*(*self).upgrade().deref()).x.borrow()); + return (*self).with(|__s: &Pair| __s.x); } fn as_ref(&self) -> Ptr { - return (*(*self).upgrade().deref()).x.as_pointer(); + return field_ptr!((*self), x); } fn as_ptr(&self) -> Ptr { - return ((*(*self).upgrade().deref()).x.as_pointer()); + return (field_ptr!((*self), x)); } } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/reference_wrapper.rs b/tests/unit/out/refcount/reference_wrapper.rs index d3d64d1e1a..772d3e1cc3 100644 --- a/tests/unit/out/refcount/reference_wrapper.rs +++ b/tests/unit/out/refcount/reference_wrapper.rs @@ -6,33 +6,25 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Point { - pub x: Value, - pub y: Value, -} -impl Clone for Point { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - x: Rc::new(RefCell::new((*self.x.borrow()))), - y: Rc::new(RefCell::new((*self.y.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub x: i32, + #[offset(4)] + pub y: i32, } impl ByteRepr for Point { fn byte_size() -> usize { 8 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.x.borrow()).to_bytes(&mut buf[0..4]); - (*self.y.borrow()).to_bytes(&mut buf[4..8]); + self.x.to_bytes(&mut buf[0..4]); + self.y.to_bytes(&mut buf[4..8]); } fn from_bytes(buf: &[u8]) -> Self { Self { - x: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - y: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + x: ::from_bytes(&buf[0..4]), + y: ::from_bytes(&buf[4..8]), } } } @@ -79,19 +71,13 @@ fn main_0() -> i32 { (*i1.borrow()), ({ read_1((*ref_1.borrow()).clone(),) }), ); - let point: Value = Rc::new(RefCell::new(Point { - x: Rc::new(RefCell::new(3)), - y: Rc::new(RefCell::new(4)), - })); + let point: Value = Rc::new(RefCell::new(Point { x: 3, y: 4 })); let point_ref: Value> = Rc::new(RefCell::new(point.as_pointer())); - (*(*(*point_ref.borrow()).upgrade().deref()).x.borrow_mut()) = 30; - (*(*(*point_ref.borrow()).upgrade().deref()).y.borrow_mut()) = 40; - write!( - libcc2rs::cout(), - "{:} {:}\n", - (*(*point.borrow()).x.borrow()), - (*(*point.borrow()).y.borrow()), - ); + (*point_ref.borrow()).with_mut(|__s: &mut Point| __s.x = 30); + (*point_ref.borrow()).with_mut(|__s: &mut Point| __s.y = 40); + write!(libcc2rs::cout(), "{:} {:}\n", { (*point.borrow()).x }, { + (*point.borrow()).y + },); return 0; } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/refs_as_args.rs b/tests/unit/out/refcount/refs_as_args.rs index 644cd1e18b..7c71ab44a0 100644 --- a/tests/unit/out/refcount/refs_as_args.rs +++ b/tests/unit/out/refcount/refs_as_args.rs @@ -42,36 +42,28 @@ pub fn more_refs_0(x1: i32, x2: i32, r1: Ptr, r2: Ptr) { let __rhs = (rx2.read()); r1.write(__rhs); } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Val { - pub x: Value, -} -impl Clone for Val { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - x: Rc::new(RefCell::new((*self.x.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub x: i32, } impl ByteRepr for Val { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.x.borrow()).to_bytes(&mut buf[0..4]); + self.x.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - x: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + x: ::from_bytes(&buf[0..4]), } } } pub fn sum_1(a: Val, b: Val) -> i32 { let a: Value = Rc::new(RefCell::new(a)); let b: Value = Rc::new(RefCell::new(b)); - return ((*(*a.borrow()).x.borrow()) + (*(*b.borrow()).x.borrow())); + return ({ (*a.borrow()).x } + { (*b.borrow()).x }); } pub fn main() { __cpp2rust_init_globals(); @@ -82,9 +74,7 @@ fn main_0() -> i32 { let x2: Value = Rc::new(RefCell::new(2)); ({ more_refs_0(3, 4, x1.as_pointer(), x2.as_pointer()) }); assert!((((*x1.borrow()) + (*x2.borrow())) == 21)); - let v: Value = Rc::new(RefCell::new(Val { - x: Rc::new(RefCell::new(5)), - })); + let v: Value = Rc::new(RefCell::new(Val { x: 5 })); let acc: Value = Rc::new(RefCell::new( ({ let _a: Val = (*v.borrow()).clone(); diff --git a/tests/unit/out/refcount/reinterpret_cast_struct.rs b/tests/unit/out/refcount/reinterpret_cast_struct.rs index 3434b3b893..c0942a19c1 100644 --- a/tests/unit/out/refcount/reinterpret_cast_struct.rs +++ b/tests/unit/out/refcount/reinterpret_cast_struct.rs @@ -6,33 +6,25 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Point { - pub x: Value, - pub y: Value, -} -impl Clone for Point { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - x: Rc::new(RefCell::new((*self.x.borrow()))), - y: Rc::new(RefCell::new((*self.y.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub x: i32, + #[offset(4)] + pub y: i32, } impl ByteRepr for Point { fn byte_size() -> usize { 8 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.x.borrow()).to_bytes(&mut buf[0..4]); - (*self.y.borrow()).to_bytes(&mut buf[4..8]); + self.x.to_bytes(&mut buf[0..4]); + self.y.to_bytes(&mut buf[4..8]); } fn from_bytes(buf: &[u8]) -> Self { Self { - x: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - y: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + x: ::from_bytes(&buf[0..4]), + y: ::from_bytes(&buf[4..8]), } } } @@ -42,8 +34,8 @@ pub fn main() { } fn main_0() -> i32 { let p: Value = Rc::new(RefCell::new(::default())); - (*(*p.borrow()).x.borrow_mut()) = 67305985; - (*(*p.borrow()).y.borrow_mut()) = 134678021; + (*p.borrow_mut()).x = 67305985; + (*p.borrow_mut()).y = 134678021; let bytes: Value> = Rc::new(RefCell::new((p.as_pointer()).reinterpret_cast::())); assert!(((((*bytes.borrow()).offset((0) as isize).read()) as i32) == 1)); assert!(((((*bytes.borrow()).offset((3) as isize).read()) as i32) == 4)); diff --git a/tests/unit/out/refcount/reinterpret_cast_struct_to_struct.rs b/tests/unit/out/refcount/reinterpret_cast_struct_to_struct.rs index 8f3133966e..641d9c868a 100644 --- a/tests/unit/out/refcount/reinterpret_cast_struct_to_struct.rs +++ b/tests/unit/out/refcount/reinterpret_cast_struct_to_struct.rs @@ -6,63 +6,47 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Point { - pub x: Value, - pub y: Value, -} -impl Clone for Point { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - x: Rc::new(RefCell::new((*self.x.borrow()))), - y: Rc::new(RefCell::new((*self.y.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub x: u32, + #[offset(4)] + pub y: u32, } impl ByteRepr for Point { fn byte_size() -> usize { 8 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.x.borrow()).to_bytes(&mut buf[0..4]); - (*self.y.borrow()).to_bytes(&mut buf[4..8]); + self.x.to_bytes(&mut buf[0..4]); + self.y.to_bytes(&mut buf[4..8]); } fn from_bytes(buf: &[u8]) -> Self { Self { - x: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - y: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + x: ::from_bytes(&buf[0..4]), + y: ::from_bytes(&buf[4..8]), } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Pair { - pub first: Value, - pub second: Value, -} -impl Clone for Pair { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - first: Rc::new(RefCell::new((*self.first.borrow()))), - second: Rc::new(RefCell::new((*self.second.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub first: u32, + #[offset(4)] + pub second: u32, } impl ByteRepr for Pair { fn byte_size() -> usize { 8 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.first.borrow()).to_bytes(&mut buf[0..4]); - (*self.second.borrow()).to_bytes(&mut buf[4..8]); + self.first.to_bytes(&mut buf[0..4]); + self.second.to_bytes(&mut buf[4..8]); } fn from_bytes(buf: &[u8]) -> Self { Self { - first: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - second: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + first: ::from_bytes(&buf[0..4]), + second: ::from_bytes(&buf[4..8]), } } } @@ -72,15 +56,15 @@ pub fn main() { } fn main_0() -> i32 { let pt: Value = Rc::new(RefCell::new(Point { - x: Rc::new(RefCell::new(10_u32)), - y: Rc::new(RefCell::new(20_u32)), + x: 10_u32, + y: 20_u32, })); let pair: Value> = Rc::new(RefCell::new((pt.as_pointer()).reinterpret_cast::())); - assert!(((*(*(*pair.borrow()).upgrade().deref()).first.borrow()) == 10_u32)); - assert!(((*(*(*pair.borrow()).upgrade().deref()).second.borrow()) == 20_u32)); - (*(*(*pair.borrow()).upgrade().deref()).first.borrow_mut()) = 42_u32; - assert!(((*(*pt.borrow()).x.borrow()) == 42_u32)); + assert!(((*pair.borrow()).with(|__s: &Pair| __s.first) == 10_u32)); + assert!(((*pair.borrow()).with(|__s: &Pair| __s.second) == 20_u32)); + (*pair.borrow()).with_mut(|__s: &mut Pair| __s.first = 42_u32); + assert!(({ (*pt.borrow()).x } == 42_u32)); return 0; } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/reserved_keywords.rs b/tests/unit/out/refcount/reserved_keywords.rs index b258af429d..20d22ebff4 100644 --- a/tests/unit/out/refcount/reserved_keywords.rs +++ b/tests/unit/out/refcount/reserved_keywords.rs @@ -6,177 +6,169 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct S { - pub as_: Value, - pub async_: Value, - pub await_: Value, - pub crate_: Value, - pub dyn_: Value, - pub fn_: Value, - pub impl_: Value, - pub in_: Value, - pub let_: Value, - pub loop_: Value, - pub match_: Value, - pub mod_: Value, - pub move_: Value, - pub mut_: Value, - pub pub_: Value, - pub ref_: Value, - pub self_: Value, - pub Self_: Value, - pub super_: Value, - pub trait_: Value, - pub type_: Value, - pub unsafe_: Value, - pub use_: Value, - pub where_: Value, - pub abstract_: Value, - pub become_: Value, - pub box_: Value, - pub final_: Value, - pub gen_: Value, - pub macro_: Value, - pub override_: Value, - pub priv_: Value, - pub unsized_: Value, - pub yield_: Value, - pub macro_rules_: Value, - pub raw_: Value, - pub safe_: Value, - pub vec_: Value, -} -impl Clone for S { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - as_: Rc::new(RefCell::new((*self.as_.borrow()))), - async_: Rc::new(RefCell::new((*self.async_.borrow()))), - await_: Rc::new(RefCell::new((*self.await_.borrow()))), - crate_: Rc::new(RefCell::new((*self.crate_.borrow()))), - dyn_: Rc::new(RefCell::new((*self.dyn_.borrow()))), - fn_: Rc::new(RefCell::new((*self.fn_.borrow()))), - impl_: Rc::new(RefCell::new((*self.impl_.borrow()))), - in_: Rc::new(RefCell::new((*self.in_.borrow()))), - let_: Rc::new(RefCell::new((*self.let_.borrow()))), - loop_: Rc::new(RefCell::new((*self.loop_.borrow()))), - match_: Rc::new(RefCell::new((*self.match_.borrow()))), - mod_: Rc::new(RefCell::new((*self.mod_.borrow()))), - move_: Rc::new(RefCell::new((*self.move_.borrow()))), - mut_: Rc::new(RefCell::new((*self.mut_.borrow()))), - pub_: Rc::new(RefCell::new((*self.pub_.borrow()))), - ref_: Rc::new(RefCell::new((*self.ref_.borrow()))), - self_: Rc::new(RefCell::new((*self.self_.borrow()))), - Self_: Rc::new(RefCell::new((*self.Self_.borrow()))), - super_: Rc::new(RefCell::new((*self.super_.borrow()))), - trait_: Rc::new(RefCell::new((*self.trait_.borrow()))), - type_: Rc::new(RefCell::new((*self.type_.borrow()))), - unsafe_: Rc::new(RefCell::new((*self.unsafe_.borrow()))), - use_: Rc::new(RefCell::new((*self.use_.borrow()))), - where_: Rc::new(RefCell::new((*self.where_.borrow()))), - abstract_: Rc::new(RefCell::new((*self.abstract_.borrow()))), - become_: Rc::new(RefCell::new((*self.become_.borrow()))), - box_: Rc::new(RefCell::new((*self.box_.borrow()))), - final_: Rc::new(RefCell::new((*self.final_.borrow()))), - gen_: Rc::new(RefCell::new((*self.gen_.borrow()))), - macro_: Rc::new(RefCell::new((*self.macro_.borrow()))), - override_: Rc::new(RefCell::new((*self.override_.borrow()))), - priv_: Rc::new(RefCell::new((*self.priv_.borrow()))), - unsized_: Rc::new(RefCell::new((*self.unsized_.borrow()))), - yield_: Rc::new(RefCell::new((*self.yield_.borrow()))), - macro_rules_: Rc::new(RefCell::new((*self.macro_rules_.borrow()))), - raw_: Rc::new(RefCell::new((*self.raw_.borrow()))), - safe_: Rc::new(RefCell::new((*self.safe_.borrow()))), - vec_: Rc::new(RefCell::new((*self.vec_.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub as_: i32, + #[offset(4)] + pub async_: i32, + #[offset(8)] + pub await_: i32, + #[offset(12)] + pub crate_: i32, + #[offset(16)] + pub dyn_: i32, + #[offset(20)] + pub fn_: i32, + #[offset(24)] + pub impl_: i32, + #[offset(28)] + pub in_: i32, + #[offset(32)] + pub let_: i32, + #[offset(36)] + pub loop_: i32, + #[offset(40)] + pub match_: i32, + #[offset(44)] + pub mod_: i32, + #[offset(48)] + pub move_: i32, + #[offset(52)] + pub mut_: i32, + #[offset(56)] + pub pub_: i32, + #[offset(60)] + pub ref_: i32, + #[offset(64)] + pub self_: i32, + #[offset(68)] + pub Self_: i32, + #[offset(72)] + pub super_: i32, + #[offset(76)] + pub trait_: i32, + #[offset(80)] + pub type_: i32, + #[offset(84)] + pub unsafe_: i32, + #[offset(88)] + pub use_: i32, + #[offset(92)] + pub where_: i32, + #[offset(96)] + pub abstract_: i32, + #[offset(100)] + pub become_: i32, + #[offset(104)] + pub box_: i32, + #[offset(108)] + pub final_: i32, + #[offset(112)] + pub gen_: i32, + #[offset(116)] + pub macro_: i32, + #[offset(120)] + pub override_: i32, + #[offset(124)] + pub priv_: i32, + #[offset(128)] + pub unsized_: i32, + #[offset(132)] + pub yield_: i32, + #[offset(136)] + pub macro_rules_: i32, + #[offset(140)] + pub raw_: i32, + #[offset(144)] + pub safe_: i32, + #[offset(148)] + pub vec_: i32, } impl ByteRepr for S { fn byte_size() -> usize { 152 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.as_.borrow()).to_bytes(&mut buf[0..4]); - (*self.async_.borrow()).to_bytes(&mut buf[4..8]); - (*self.await_.borrow()).to_bytes(&mut buf[8..12]); - (*self.crate_.borrow()).to_bytes(&mut buf[12..16]); - (*self.dyn_.borrow()).to_bytes(&mut buf[16..20]); - (*self.fn_.borrow()).to_bytes(&mut buf[20..24]); - (*self.impl_.borrow()).to_bytes(&mut buf[24..28]); - (*self.in_.borrow()).to_bytes(&mut buf[28..32]); - (*self.let_.borrow()).to_bytes(&mut buf[32..36]); - (*self.loop_.borrow()).to_bytes(&mut buf[36..40]); - (*self.match_.borrow()).to_bytes(&mut buf[40..44]); - (*self.mod_.borrow()).to_bytes(&mut buf[44..48]); - (*self.move_.borrow()).to_bytes(&mut buf[48..52]); - (*self.mut_.borrow()).to_bytes(&mut buf[52..56]); - (*self.pub_.borrow()).to_bytes(&mut buf[56..60]); - (*self.ref_.borrow()).to_bytes(&mut buf[60..64]); - (*self.self_.borrow()).to_bytes(&mut buf[64..68]); - (*self.Self_.borrow()).to_bytes(&mut buf[68..72]); - (*self.super_.borrow()).to_bytes(&mut buf[72..76]); - (*self.trait_.borrow()).to_bytes(&mut buf[76..80]); - (*self.type_.borrow()).to_bytes(&mut buf[80..84]); - (*self.unsafe_.borrow()).to_bytes(&mut buf[84..88]); - (*self.use_.borrow()).to_bytes(&mut buf[88..92]); - (*self.where_.borrow()).to_bytes(&mut buf[92..96]); - (*self.abstract_.borrow()).to_bytes(&mut buf[96..100]); - (*self.become_.borrow()).to_bytes(&mut buf[100..104]); - (*self.box_.borrow()).to_bytes(&mut buf[104..108]); - (*self.final_.borrow()).to_bytes(&mut buf[108..112]); - (*self.gen_.borrow()).to_bytes(&mut buf[112..116]); - (*self.macro_.borrow()).to_bytes(&mut buf[116..120]); - (*self.override_.borrow()).to_bytes(&mut buf[120..124]); - (*self.priv_.borrow()).to_bytes(&mut buf[124..128]); - (*self.unsized_.borrow()).to_bytes(&mut buf[128..132]); - (*self.yield_.borrow()).to_bytes(&mut buf[132..136]); - (*self.macro_rules_.borrow()).to_bytes(&mut buf[136..140]); - (*self.raw_.borrow()).to_bytes(&mut buf[140..144]); - (*self.safe_.borrow()).to_bytes(&mut buf[144..148]); - (*self.vec_.borrow()).to_bytes(&mut buf[148..152]); + self.as_.to_bytes(&mut buf[0..4]); + self.async_.to_bytes(&mut buf[4..8]); + self.await_.to_bytes(&mut buf[8..12]); + self.crate_.to_bytes(&mut buf[12..16]); + self.dyn_.to_bytes(&mut buf[16..20]); + self.fn_.to_bytes(&mut buf[20..24]); + self.impl_.to_bytes(&mut buf[24..28]); + self.in_.to_bytes(&mut buf[28..32]); + self.let_.to_bytes(&mut buf[32..36]); + self.loop_.to_bytes(&mut buf[36..40]); + self.match_.to_bytes(&mut buf[40..44]); + self.mod_.to_bytes(&mut buf[44..48]); + self.move_.to_bytes(&mut buf[48..52]); + self.mut_.to_bytes(&mut buf[52..56]); + self.pub_.to_bytes(&mut buf[56..60]); + self.ref_.to_bytes(&mut buf[60..64]); + self.self_.to_bytes(&mut buf[64..68]); + self.Self_.to_bytes(&mut buf[68..72]); + self.super_.to_bytes(&mut buf[72..76]); + self.trait_.to_bytes(&mut buf[76..80]); + self.type_.to_bytes(&mut buf[80..84]); + self.unsafe_.to_bytes(&mut buf[84..88]); + self.use_.to_bytes(&mut buf[88..92]); + self.where_.to_bytes(&mut buf[92..96]); + self.abstract_.to_bytes(&mut buf[96..100]); + self.become_.to_bytes(&mut buf[100..104]); + self.box_.to_bytes(&mut buf[104..108]); + self.final_.to_bytes(&mut buf[108..112]); + self.gen_.to_bytes(&mut buf[112..116]); + self.macro_.to_bytes(&mut buf[116..120]); + self.override_.to_bytes(&mut buf[120..124]); + self.priv_.to_bytes(&mut buf[124..128]); + self.unsized_.to_bytes(&mut buf[128..132]); + self.yield_.to_bytes(&mut buf[132..136]); + self.macro_rules_.to_bytes(&mut buf[136..140]); + self.raw_.to_bytes(&mut buf[140..144]); + self.safe_.to_bytes(&mut buf[144..148]); + self.vec_.to_bytes(&mut buf[148..152]); } fn from_bytes(buf: &[u8]) -> Self { Self { - as_: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - async_: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), - await_: Rc::new(RefCell::new(::from_bytes(&buf[8..12]))), - crate_: Rc::new(RefCell::new(::from_bytes(&buf[12..16]))), - dyn_: Rc::new(RefCell::new(::from_bytes(&buf[16..20]))), - fn_: Rc::new(RefCell::new(::from_bytes(&buf[20..24]))), - impl_: Rc::new(RefCell::new(::from_bytes(&buf[24..28]))), - in_: Rc::new(RefCell::new(::from_bytes(&buf[28..32]))), - let_: Rc::new(RefCell::new(::from_bytes(&buf[32..36]))), - loop_: Rc::new(RefCell::new(::from_bytes(&buf[36..40]))), - match_: Rc::new(RefCell::new(::from_bytes(&buf[40..44]))), - mod_: Rc::new(RefCell::new(::from_bytes(&buf[44..48]))), - move_: Rc::new(RefCell::new(::from_bytes(&buf[48..52]))), - mut_: Rc::new(RefCell::new(::from_bytes(&buf[52..56]))), - pub_: Rc::new(RefCell::new(::from_bytes(&buf[56..60]))), - ref_: Rc::new(RefCell::new(::from_bytes(&buf[60..64]))), - self_: Rc::new(RefCell::new(::from_bytes(&buf[64..68]))), - Self_: Rc::new(RefCell::new(::from_bytes(&buf[68..72]))), - super_: Rc::new(RefCell::new(::from_bytes(&buf[72..76]))), - trait_: Rc::new(RefCell::new(::from_bytes(&buf[76..80]))), - type_: Rc::new(RefCell::new(::from_bytes(&buf[80..84]))), - unsafe_: Rc::new(RefCell::new(::from_bytes(&buf[84..88]))), - use_: Rc::new(RefCell::new(::from_bytes(&buf[88..92]))), - where_: Rc::new(RefCell::new(::from_bytes(&buf[92..96]))), - abstract_: Rc::new(RefCell::new(::from_bytes(&buf[96..100]))), - become_: Rc::new(RefCell::new(::from_bytes(&buf[100..104]))), - box_: Rc::new(RefCell::new(::from_bytes(&buf[104..108]))), - final_: Rc::new(RefCell::new(::from_bytes(&buf[108..112]))), - gen_: Rc::new(RefCell::new(::from_bytes(&buf[112..116]))), - macro_: Rc::new(RefCell::new(::from_bytes(&buf[116..120]))), - override_: Rc::new(RefCell::new(::from_bytes(&buf[120..124]))), - priv_: Rc::new(RefCell::new(::from_bytes(&buf[124..128]))), - unsized_: Rc::new(RefCell::new(::from_bytes(&buf[128..132]))), - yield_: Rc::new(RefCell::new(::from_bytes(&buf[132..136]))), - macro_rules_: Rc::new(RefCell::new(::from_bytes(&buf[136..140]))), - raw_: Rc::new(RefCell::new(::from_bytes(&buf[140..144]))), - safe_: Rc::new(RefCell::new(::from_bytes(&buf[144..148]))), - vec_: Rc::new(RefCell::new(::from_bytes(&buf[148..152]))), + as_: ::from_bytes(&buf[0..4]), + async_: ::from_bytes(&buf[4..8]), + await_: ::from_bytes(&buf[8..12]), + crate_: ::from_bytes(&buf[12..16]), + dyn_: ::from_bytes(&buf[16..20]), + fn_: ::from_bytes(&buf[20..24]), + impl_: ::from_bytes(&buf[24..28]), + in_: ::from_bytes(&buf[28..32]), + let_: ::from_bytes(&buf[32..36]), + loop_: ::from_bytes(&buf[36..40]), + match_: ::from_bytes(&buf[40..44]), + mod_: ::from_bytes(&buf[44..48]), + move_: ::from_bytes(&buf[48..52]), + mut_: ::from_bytes(&buf[52..56]), + pub_: ::from_bytes(&buf[56..60]), + ref_: ::from_bytes(&buf[60..64]), + self_: ::from_bytes(&buf[64..68]), + Self_: ::from_bytes(&buf[68..72]), + super_: ::from_bytes(&buf[72..76]), + trait_: ::from_bytes(&buf[76..80]), + type_: ::from_bytes(&buf[80..84]), + unsafe_: ::from_bytes(&buf[84..88]), + use_: ::from_bytes(&buf[88..92]), + where_: ::from_bytes(&buf[92..96]), + abstract_: ::from_bytes(&buf[96..100]), + become_: ::from_bytes(&buf[100..104]), + box_: ::from_bytes(&buf[104..108]), + final_: ::from_bytes(&buf[108..112]), + gen_: ::from_bytes(&buf[112..116]), + macro_: ::from_bytes(&buf[116..120]), + override_: ::from_bytes(&buf[120..124]), + priv_: ::from_bytes(&buf[124..128]), + unsized_: ::from_bytes(&buf[128..132]), + yield_: ::from_bytes(&buf[132..136]), + macro_rules_: ::from_bytes(&buf[136..140]), + raw_: ::from_bytes(&buf[140..144]), + safe_: ::from_bytes(&buf[144..148]), + vec_: ::from_bytes(&buf[148..152]), } } } @@ -268,44 +260,44 @@ pub fn main() { } fn main_0() -> i32 { let s: Value = Rc::new(RefCell::new(S { - as_: Rc::new(RefCell::new(0)), - async_: Rc::new(RefCell::new(0)), - await_: Rc::new(RefCell::new(0)), - crate_: Rc::new(RefCell::new(0)), - dyn_: Rc::new(RefCell::new(0)), - fn_: Rc::new(RefCell::new(0)), - impl_: Rc::new(RefCell::new(0)), - in_: Rc::new(RefCell::new(0)), - let_: Rc::new(RefCell::new(0)), - loop_: Rc::new(RefCell::new(0)), - match_: Rc::new(RefCell::new(0)), - mod_: Rc::new(RefCell::new(0)), - move_: Rc::new(RefCell::new(0)), - mut_: Rc::new(RefCell::new(0)), - pub_: Rc::new(RefCell::new(0)), - ref_: Rc::new(RefCell::new(0)), - self_: Rc::new(RefCell::new(0)), - Self_: Rc::new(RefCell::new(0)), - super_: Rc::new(RefCell::new(0)), - trait_: Rc::new(RefCell::new(0)), - type_: Rc::new(RefCell::new(0)), - unsafe_: Rc::new(RefCell::new(0)), - use_: Rc::new(RefCell::new(0)), - where_: Rc::new(RefCell::new(0)), - abstract_: Rc::new(RefCell::new(0)), - become_: Rc::new(RefCell::new(0)), - box_: Rc::new(RefCell::new(0)), - final_: Rc::new(RefCell::new(0)), - gen_: Rc::new(RefCell::new(0)), - macro_: Rc::new(RefCell::new(0)), - override_: Rc::new(RefCell::new(0)), - priv_: Rc::new(RefCell::new(0)), - unsized_: Rc::new(RefCell::new(0)), - yield_: Rc::new(RefCell::new(0)), - macro_rules_: Rc::new(RefCell::new(0)), - raw_: Rc::new(RefCell::new(0)), - safe_: Rc::new(RefCell::new(0)), - vec_: Rc::new(RefCell::new(0)), + as_: 0, + async_: 0, + await_: 0, + crate_: 0, + dyn_: 0, + fn_: 0, + impl_: 0, + in_: 0, + let_: 0, + loop_: 0, + match_: 0, + mod_: 0, + move_: 0, + mut_: 0, + pub_: 0, + ref_: 0, + self_: 0, + Self_: 0, + super_: 0, + trait_: 0, + type_: 0, + unsafe_: 0, + use_: 0, + where_: 0, + abstract_: 0, + become_: 0, + box_: 0, + final_: 0, + gen_: 0, + macro_: 0, + override_: 0, + priv_: 0, + unsized_: 0, + yield_: 0, + macro_rules_: 0, + raw_: 0, + safe_: 0, + vec_: 0, })); let as_: Value = Rc::new(RefCell::new(0)); let async_: Value = Rc::new(RefCell::new(0)); diff --git a/tests/unit/out/refcount/rule_of_five.rs b/tests/unit/out/refcount/rule_of_five.rs index 2bddf88f99..5a9e3d6a9a 100644 --- a/tests/unit/out/refcount/rule_of_five.rs +++ b/tests/unit/out/refcount/rule_of_five.rs @@ -15,17 +15,19 @@ thread_local!( thread_local!( pub static moves_2: Value = Rc::new(RefCell::new(0)); ); -#[derive(VaArg, FnPtrArg)] +#[derive(Record, VaArg, FnPtrArg)] pub struct Buffer { - pub data: Value>, - pub size: Value, + #[offset(0)] + pub data: Box<[i32]>, + #[offset(16)] + pub size: i32, } impl Buffer { pub fn new(size: i32) -> Self { let size: Value = Rc::new(RefCell::new(size)); let __this: Value = Rc::new(RefCell::new(Self { - data: Rc::new(RefCell::new((0..4).map(|_| 0_i32).collect::>())), - size: Rc::new(RefCell::new((*size.borrow()))), + data: (0..4).map(|_| 0_i32).collect::>(), + size: (*size.borrow()), })); let this: Ptr = __this.as_pointer(); let i: Value = Rc::new(RefCell::new(0)); @@ -35,7 +37,7 @@ impl Buffer { } else { -1_i32 }; - (*(*this.upgrade().deref()).data.borrow_mut())[(*i.borrow()) as usize] = __rhs; + this.with_mut(|__s: &mut Buffer| __s.data[(*i.borrow()) as usize] = __rhs); (*i.borrow_mut()).prefix_inc(); } (*alive_0.with(Value::clone).borrow_mut()).prefix_inc(); @@ -43,14 +45,14 @@ impl Buffer { } pub fn copy_from(o: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - data: Rc::new(RefCell::new((0..4).map(|_| 0_i32).collect::>())), - size: Rc::new(RefCell::new((*(*o.upgrade().deref()).size.borrow()))), + data: (0..4).map(|_| 0_i32).collect::>(), + size: { o.with(|__s: &Buffer| __s.size) }, })); let this: Ptr = __this.as_pointer(); let i: Value = Rc::new(RefCell::new(0)); 'loop_: while ((*i.borrow()) < 4) { - let __rhs = (*(*o.upgrade().deref()).data.borrow())[(*i.borrow()) as usize]; - (*(*this.upgrade().deref()).data.borrow_mut())[(*i.borrow()) as usize] = __rhs; + let __rhs = o.with(|__s: &Buffer| __s.data[(*i.borrow()) as usize]); + this.with_mut(|__s: &mut Buffer| __s.data[(*i.borrow()) as usize] = __rhs); (*i.borrow_mut()).prefix_inc(); } (*alive_0.with(Value::clone).borrow_mut()).prefix_inc(); @@ -59,18 +61,19 @@ impl Buffer { } pub fn move_from(o: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - data: Rc::new(RefCell::new((0..4).map(|_| 0_i32).collect::>())), - size: Rc::new(RefCell::new((*(*o.upgrade().deref()).size.borrow()))), + data: (0..4).map(|_| 0_i32).collect::>(), + size: { o.with(|__s: &Buffer| __s.size) }, })); let this: Ptr = __this.as_pointer(); let i: Value = Rc::new(RefCell::new(0)); 'loop_: while ((*i.borrow()) < 4) { - let __rhs = (*(*o.upgrade().deref()).data.borrow())[(*i.borrow()) as usize]; - (*(*this.upgrade().deref()).data.borrow_mut())[(*i.borrow()) as usize] = __rhs; - (*(*o.upgrade().deref()).data.borrow_mut())[(*i.borrow()) as usize] = -1_i32; + let __rhs = o.with(|__s: &Buffer| __s.data[(*i.borrow()) as usize]); + this.with_mut(|__s: &mut Buffer| __s.data[(*i.borrow()) as usize] = __rhs); + let __rhs = -1_i32; + o.with_mut(|__s: &mut Buffer| __s.data[(*i.borrow()) as usize] = __rhs); (*i.borrow_mut()).prefix_inc(); } - (*(*o.upgrade().deref()).size.borrow_mut()) = 0; + o.with_mut(|__s: &mut Buffer| __s.size = 0); (*alive_0.with(Value::clone).borrow_mut()).prefix_inc(); (*moves_2.with(Value::clone).borrow_mut()).prefix_inc(); Rc::try_unwrap(__this).ok().unwrap().into_inner() @@ -88,12 +91,30 @@ impl Clone for Buffer { impl Default for Buffer { fn default() -> Self { Buffer { - data: Rc::new(RefCell::new((0..4).map(|_| 0_i32).collect::>())), - size: Rc::new(RefCell::new(0_i32)), + data: (0..4).map(|_| 0_i32).collect::>(), + size: 0_i32, } } } +<<<<<<< HEAD impl ByteRepr for Buffer {} +======= +impl ByteRepr for Buffer { + fn byte_size() -> usize { + 20 + } + fn to_bytes(&self, buf: &mut [u8]) { + self.data.to_bytes(&mut buf[0..16]); + self.size.to_bytes(&mut buf[16..20]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + data: >::from_bytes(&buf[0..16]), + size: ::from_bytes(&buf[16..20]), + } + } +} +>>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub fn make_3(size: i32) -> Buffer { let size: Value = Rc::new(RefCell::new(size)); let b: Value = Rc::new(RefCell::new(Buffer::new({ (*size.borrow()) }))); @@ -114,39 +135,31 @@ fn main_0() -> i32 { ((alive_0.with(|rc| *rc.borrow()) == 2) && (copies_1.with(|rc| *rc.borrow()) == 1)) && (moves_2.with(|rc| *rc.borrow()) == 0) ); - (*(*b.borrow()).data.borrow_mut())[(0) as usize] = 100; - assert!(((*(*a.borrow()).data.borrow())[(0) as usize] == 0)); + (*b.borrow_mut()).data[(0) as usize] = 100; + assert!(((*a.borrow()).data[(0) as usize] == 0)); let c: Value = Rc::new(RefCell::new(Buffer::move_from({ a.as_pointer() }))); let _dtor_c = ScopedDestructor::new(&c, |__p| __p.destructor()); assert!((alive_0.with(|rc| *rc.borrow()) == 3) && (moves_2.with(|rc| *rc.borrow()) == 1)); - assert!( - ((*(*a.borrow()).size.borrow()) == 0) - && ((*(*a.borrow()).data.borrow())[(0) as usize] == -1_i32) - ); - assert!( - ((*(*c.borrow()).size.borrow()) == 4) - && ((*(*c.borrow()).data.borrow())[(3) as usize] == 3) - ); + assert!(({ (*a.borrow()).size } == 0) && ((*a.borrow()).data[(0) as usize] == -1_i32)); + assert!(({ (*c.borrow()).size } == 4) && ((*c.borrow()).data[(3) as usize] == 3)); let d: Value = Rc::new(RefCell::new(({ make_3(2) }))); let _dtor_d = ScopedDestructor::new(&d, |__p| __p.destructor()); - assert!(((*(*d.borrow()).size.borrow()) == 2) && (moves_2.with(|rc| *rc.borrow()) == 2)); + assert!(({ (*d.borrow()).size } == 2) && (moves_2.with(|rc| *rc.borrow()) == 2)); ({ BufferImpl::copy_assign(&d.as_pointer(), b.as_pointer()) }); assert!( - (((*(*d.borrow()).size.borrow()) == 4) - && ((*(*d.borrow()).data.borrow())[(0) as usize] == 100)) + (({ (*d.borrow()).size } == 4) && ((*d.borrow()).data[(0) as usize] == 100)) && (copies_1.with(|rc| *rc.borrow()) == 2) ); ({ BufferImpl::move_assign(&d.as_pointer(), c.as_pointer()) }); assert!( - (((*(*d.borrow()).data.borrow())[(0) as usize] == 0) - && ((*(*c.borrow()).size.borrow()) == 0)) + (((*d.borrow()).data[(0) as usize] == 0) && ({ (*c.borrow()).size } == 0)) && (moves_2.with(|rc| *rc.borrow()) == 3) ); ({ let _o: Ptr = d.as_pointer(); BufferImpl::move_assign(&d.as_pointer(), _o) }); - assert!(((*(*d.borrow()).size.borrow()) == 4) && (moves_2.with(|rc| *rc.borrow()) == 3)); + assert!(({ (*d.borrow()).size } == 4) && (moves_2.with(|rc| *rc.borrow()) == 3)); } assert!((alive_0.with(|rc| *rc.borrow()) == 0)); return 0; @@ -164,12 +177,12 @@ impl BufferImpl for Ptr { if ((*self) == (o)) { return (*self).clone(); } - let __rhs = (*(*o.upgrade().deref()).size.borrow()); - (*(*(*self).upgrade().deref()).size.borrow_mut()) = __rhs; + let __rhs = o.with(|__s: &Buffer| __s.size); + (*self).with_mut(|__s: &mut Buffer| __s.size = __rhs); let i: Value = Rc::new(RefCell::new(0)); 'loop_: while ((*i.borrow()) < 4) { - let __rhs = (*(*o.upgrade().deref()).data.borrow())[(*i.borrow()) as usize]; - (*(*(*self).upgrade().deref()).data.borrow_mut())[(*i.borrow()) as usize] = __rhs; + let __rhs = o.with(|__s: &Buffer| __s.data[(*i.borrow()) as usize]); + (*self).with_mut(|__s: &mut Buffer| __s.data[(*i.borrow()) as usize] = __rhs); (*i.borrow_mut()).prefix_inc(); } (*copies_1.with(Value::clone).borrow_mut()).prefix_inc(); @@ -179,16 +192,17 @@ impl BufferImpl for Ptr { if ((*self) == (o)) { return (*self).clone(); } - let __rhs = (*(*o.upgrade().deref()).size.borrow()); - (*(*(*self).upgrade().deref()).size.borrow_mut()) = __rhs; + let __rhs = o.with(|__s: &Buffer| __s.size); + (*self).with_mut(|__s: &mut Buffer| __s.size = __rhs); let i: Value = Rc::new(RefCell::new(0)); 'loop_: while ((*i.borrow()) < 4) { - let __rhs = (*(*o.upgrade().deref()).data.borrow())[(*i.borrow()) as usize]; - (*(*(*self).upgrade().deref()).data.borrow_mut())[(*i.borrow()) as usize] = __rhs; - (*(*o.upgrade().deref()).data.borrow_mut())[(*i.borrow()) as usize] = -1_i32; + let __rhs = o.with(|__s: &Buffer| __s.data[(*i.borrow()) as usize]); + (*self).with_mut(|__s: &mut Buffer| __s.data[(*i.borrow()) as usize] = __rhs); + let __rhs = -1_i32; + o.with_mut(|__s: &mut Buffer| __s.data[(*i.borrow()) as usize] = __rhs); (*i.borrow_mut()).prefix_inc(); } - (*(*o.upgrade().deref()).size.borrow_mut()) = 0; + o.with_mut(|__s: &mut Buffer| __s.size = 0); (*moves_2.with(Value::clone).borrow_mut()).prefix_inc(); return (*self).clone(); } diff --git a/tests/unit/out/refcount/rule_of_three.rs b/tests/unit/out/refcount/rule_of_three.rs index f65582ef03..e462558a2e 100644 --- a/tests/unit/out/refcount/rule_of_three.rs +++ b/tests/unit/out/refcount/rule_of_three.rs @@ -12,17 +12,19 @@ thread_local!( thread_local!( pub static copies_1: Value = Rc::new(RefCell::new(0)); ); -#[derive(VaArg, FnPtrArg)] +#[derive(Record, VaArg, FnPtrArg)] pub struct Buffer { - pub data: Value>, - pub size: Value, + #[offset(0)] + pub data: Box<[i32]>, + #[offset(16)] + pub size: i32, } impl Buffer { pub fn new(size: i32) -> Self { let size: Value = Rc::new(RefCell::new(size)); let __this: Value = Rc::new(RefCell::new(Self { - data: Rc::new(RefCell::new((0..4).map(|_| 0_i32).collect::>())), - size: Rc::new(RefCell::new((*size.borrow()))), + data: (0..4).map(|_| 0_i32).collect::>(), + size: (*size.borrow()), })); let this: Ptr = __this.as_pointer(); let i: Value = Rc::new(RefCell::new(0)); @@ -32,7 +34,7 @@ impl Buffer { } else { -1_i32 }; - (*(*this.upgrade().deref()).data.borrow_mut())[(*i.borrow()) as usize] = __rhs; + this.with_mut(|__s: &mut Buffer| __s.data[(*i.borrow()) as usize] = __rhs); (*i.borrow_mut()).prefix_inc(); } (*alive_0.with(Value::clone).borrow_mut()).prefix_inc(); @@ -40,14 +42,14 @@ impl Buffer { } pub fn copy_from(o: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - data: Rc::new(RefCell::new((0..4).map(|_| 0_i32).collect::>())), - size: Rc::new(RefCell::new((*(*o.upgrade().deref()).size.borrow()))), + data: (0..4).map(|_| 0_i32).collect::>(), + size: { o.with(|__s: &Buffer| __s.size) }, })); let this: Ptr = __this.as_pointer(); let i: Value = Rc::new(RefCell::new(0)); 'loop_: while ((*i.borrow()) < 4) { - let __rhs = (*(*o.upgrade().deref()).data.borrow())[(*i.borrow()) as usize]; - (*(*this.upgrade().deref()).data.borrow_mut())[(*i.borrow()) as usize] = __rhs; + let __rhs = o.with(|__s: &Buffer| __s.data[(*i.borrow()) as usize]); + this.with_mut(|__s: &mut Buffer| __s.data[(*i.borrow()) as usize] = __rhs); (*i.borrow_mut()).prefix_inc(); } (*alive_0.with(Value::clone).borrow_mut()).prefix_inc(); @@ -67,20 +69,38 @@ impl Clone for Buffer { impl Default for Buffer { fn default() -> Self { Buffer { - data: Rc::new(RefCell::new((0..4).map(|_| 0_i32).collect::>())), - size: Rc::new(RefCell::new(0_i32)), + data: (0..4).map(|_| 0_i32).collect::>(), + size: 0_i32, } } } +<<<<<<< HEAD impl ByteRepr for Buffer {} +======= +impl ByteRepr for Buffer { + fn byte_size() -> usize { + 20 + } + fn to_bytes(&self, buf: &mut [u8]) { + self.data.to_bytes(&mut buf[0..16]); + self.size.to_bytes(&mut buf[16..20]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + data: >::from_bytes(&buf[0..16]), + size: ::from_bytes(&buf[16..20]), + } + } +} +>>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub fn sum_2(b: Ptr) -> i32 { let s: Value = Rc::new(RefCell::new(0)); let i: Value = Rc::new(RefCell::new(0)); 'loop_: while { let _lhs = (*i.borrow()); - _lhs < (*(*b.upgrade().deref()).size.borrow()) + _lhs < b.with(|__s: &Buffer| __s.size) } { - (*s.borrow_mut()) += (*(*b.upgrade().deref()).data.borrow())[(*i.borrow()) as usize]; + (*s.borrow_mut()) += b.with(|__s: &Buffer| __s.data[(*i.borrow()) as usize]); (*i.borrow_mut()).prefix_inc(); } return (*s.borrow()); @@ -96,15 +116,12 @@ fn main_0() -> i32 { let b: Value = Rc::new(RefCell::new(Buffer::copy_from({ a.as_pointer() }))); let _dtor_b = ScopedDestructor::new(&b, |__p| __p.destructor()); assert!((alive_0.with(|rc| *rc.borrow()) == 2) && (copies_1.with(|rc| *rc.borrow()) == 1)); - (*(*b.borrow()).data.borrow_mut())[(0) as usize] = 100; - assert!(((*(*a.borrow()).data.borrow())[(0) as usize] == 0)); + (*b.borrow_mut()).data[(0) as usize] = 100; + assert!(((*a.borrow()).data[(0) as usize] == 0)); let c: Value = Rc::new(RefCell::new(Buffer::new({ 2 }))); let _dtor_c = ScopedDestructor::new(&c, |__p| __p.destructor()); ({ BufferImpl::copy_assign(&c.as_pointer(), a.as_pointer()) }); - assert!( - ((*(*c.borrow()).size.borrow()) == 4) - && ((*(*c.borrow()).data.borrow())[(3) as usize] == 3) - ); + assert!(({ (*c.borrow()).size } == 4) && ((*c.borrow()).data[(3) as usize] == 3)); assert!((alive_0.with(|rc| *rc.borrow()) == 3) && (copies_1.with(|rc| *rc.borrow()) == 2)); ({ let _o: Ptr = c.as_pointer(); @@ -116,15 +133,11 @@ fn main_0() -> i32 { let d: Value = Rc::new(RefCell::new(Buffer::copy_from({ a.as_pointer() }))); let _dtor_d = ScopedDestructor::new(&d, |__p| __p.destructor()); assert!((alive_0.with(|rc| *rc.borrow()) == 4) && (copies_1.with(|rc| *rc.borrow()) == 3)); - assert!( - ((*(*a.borrow()).size.borrow()) == 4) - && ((*(*a.borrow()).data.borrow())[(3) as usize] == 3) - ); + assert!(({ (*a.borrow()).size } == 4) && ((*a.borrow()).data[(3) as usize] == 3)); ({ BufferImpl::copy_assign(&d.as_pointer(), b.as_pointer()) }); assert!((copies_1.with(|rc| *rc.borrow()) == 4)); assert!( - ((*(*b.borrow()).data.borrow())[(0) as usize] == 100) - && ((*(*d.borrow()).data.borrow())[(0) as usize] == 100) + ((*b.borrow()).data[(0) as usize] == 100) && ((*d.borrow()).data[(0) as usize] == 100) ); } assert!((alive_0.with(|rc| *rc.borrow()) == 0)); @@ -142,12 +155,12 @@ impl BufferImpl for Ptr { if ((*self) == (o)) { return (*self).clone(); } - let __rhs = (*(*o.upgrade().deref()).size.borrow()); - (*(*(*self).upgrade().deref()).size.borrow_mut()) = __rhs; + let __rhs = o.with(|__s: &Buffer| __s.size); + (*self).with_mut(|__s: &mut Buffer| __s.size = __rhs); let i: Value = Rc::new(RefCell::new(0)); 'loop_: while ((*i.borrow()) < 4) { - let __rhs = (*(*o.upgrade().deref()).data.borrow())[(*i.borrow()) as usize]; - (*(*(*self).upgrade().deref()).data.borrow_mut())[(*i.borrow()) as usize] = __rhs; + let __rhs = o.with(|__s: &Buffer| __s.data[(*i.borrow()) as usize]); + (*self).with_mut(|__s: &mut Buffer| __s.data[(*i.borrow()) as usize] = __rhs); (*i.borrow_mut()).prefix_inc(); } (*copies_1.with(Value::clone).borrow_mut()).prefix_inc(); diff --git a/tests/unit/out/refcount/rvalue_struct.rs b/tests/unit/out/refcount/rvalue_struct.rs index 2939fd48a0..1528244920 100644 --- a/tests/unit/out/refcount/rvalue_struct.rs +++ b/tests/unit/out/refcount/rvalue_struct.rs @@ -6,28 +6,20 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct S { - pub a: Value, - pub b: Value, + #[offset(0)] + pub a: i32, + #[offset(4)] + pub b: i32, } impl S { pub fn new(a: i32, b: i32) -> Self { let a: Value = Rc::new(RefCell::new(a)); let b: Value = Rc::new(RefCell::new(b)); let __this: Value = Rc::new(RefCell::new(Self { - a: Rc::new(RefCell::new((*a.borrow()))), - b: Rc::new(RefCell::new((*b.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } -} -impl Clone for S { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - a: Rc::new(RefCell::new((*self.a.borrow()))), - b: Rc::new(RefCell::new((*self.b.borrow()))), + a: (*a.borrow()), + b: (*b.borrow()), })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() @@ -38,13 +30,13 @@ impl ByteRepr for S { 8 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.a.borrow()).to_bytes(&mut buf[0..4]); - (*self.b.borrow()).to_bytes(&mut buf[4..8]); + self.a.to_bytes(&mut buf[0..4]); + self.b.to_bytes(&mut buf[4..8]); } fn from_bytes(buf: &[u8]) -> Self { Self { - a: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - b: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + a: ::from_bytes(&buf[0..4]), + b: ::from_bytes(&buf[4..8]), } } } @@ -55,8 +47,8 @@ pub fn main() { fn main_0() -> i32 { let s1: Value = Rc::new(RefCell::new(S::new({ 1 }, { 2 }))); let s2: Ptr = s1.as_pointer(); - assert!(((*(*s2.upgrade().deref()).a.borrow()) == 1)); - assert!(((*(*s2.upgrade().deref()).b.borrow()) == 2)); + assert!((s2.with(|__s: &S| __s.a) == 1)); + assert!((s2.with(|__s: &S| __s.b) == 2)); return 0; } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/scalar_builtin_construct_destruct.rs b/tests/unit/out/refcount/scalar_builtin_construct_destruct.rs index 41b34e53f1..47a65fa826 100644 --- a/tests/unit/out/refcount/scalar_builtin_construct_destruct.rs +++ b/tests/unit/out/refcount/scalar_builtin_construct_destruct.rs @@ -6,29 +6,21 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Pod { - pub v: Value, -} -impl Clone for Pod { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*self.v.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub v: i32, } impl ByteRepr for Pod { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.v.borrow()).to_bytes(&mut buf[0..4]); + self.v.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - v: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + v: ::from_bytes(&buf[0..4]), } } } @@ -59,11 +51,9 @@ fn main_0() -> i32 { let x: Value = Rc::new(RefCell::new(5)); ({ destroy_2((x.as_pointer())) }); assert!(((*x.borrow()) == 5)); - let pod: Value = Rc::new(RefCell::new(Pod { - v: Rc::new(RefCell::new(7)), - })); + let pod: Value = Rc::new(RefCell::new(Pod { v: 7 })); ({ destroy_3((pod.as_pointer())) }); - assert!(((*(*pod.borrow()).v.borrow()) == 7)); + assert!(({ (*pod.borrow()).v } == 7)); return 0; } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/static_var_in_class.rs b/tests/unit/out/refcount/static_var_in_class.rs index 3e3871e1df..c34cad50da 100644 --- a/tests/unit/out/refcount/static_var_in_class.rs +++ b/tests/unit/out/refcount/static_var_in_class.rs @@ -9,14 +9,14 @@ use std::rc::{Rc, Weak}; thread_local!( static inner_const_0: Value = Rc::new(RefCell::new(1)); ); -#[derive(Clone, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] pub struct C {} thread_local!( pub static inner_const_1: Value = Rc::new(RefCell::new(2)); ); pub type anon_3 = u32; pub const anon_3_kValue: anon_3 = 3; -#[derive(Clone, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] pub struct S {} impl S { pub fn f() -> i32 { diff --git a/tests/unit/out/refcount/strdup.rs b/tests/unit/out/refcount/strdup.rs index 767036ab46..35cb9adc3d 100644 --- a/tests/unit/out/refcount/strdup.rs +++ b/tests/unit/out/refcount/strdup.rs @@ -6,27 +6,21 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct record { - pub name: Value>, -} -impl Clone for record { - fn clone(&self) -> Self { - Self { - name: Rc::new(RefCell::new((*self.name.borrow()).clone())), - } - } + #[offset(0)] + pub name: Ptr, } impl ByteRepr for record { fn byte_size() -> usize { 8 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.name.borrow()).to_bytes(&mut buf[0..8]); + self.name.to_bytes(&mut buf[0..8]); } fn from_bytes(buf: &[u8]) -> Self { Self { - name: Rc::new(RefCell::new(>::from_bytes(&buf[0..8]))), + name: >::from_bytes(&buf[0..8]), } } } @@ -130,16 +124,17 @@ fn main_0() -> i32 { ); libcc2rs::free_refcount((*d4.borrow()).to_any()); let rec: Value = Rc::new(RefCell::new(record { - name: Rc::new(RefCell::new(Ptr::::null())), + name: Ptr::::null(), })); let r: Value> = Rc::new(RefCell::new((rec.as_pointer()))); - (*(*(*r.borrow()).upgrade().deref()).name.borrow_mut()) = - libcc2rs::strdup_refcount((*p.borrow()).clone()); - assert!((((!((*(*(*r.borrow()).upgrade().deref()).name.borrow()).is_null())) as i32) != 0)); + let __rhs = libcc2rs::strdup_refcount((*p.borrow()).clone()); + (*r.borrow()).with_mut(|__s: &mut record| __s.name = __rhs); + assert!((((!(((*r.borrow()).with(|__s: &record| (__s.name).clone())).is_null())) as i32) != 0)); assert!( ((({ - let mut __it1 = - (*(*(*r.borrow()).upgrade().deref()).name.borrow()).to_c_string_iterator(); + let mut __it1 = (*r.borrow()) + .with(|__s: &record| (__s.name).clone()) + .to_c_string_iterator(); let mut __it2 = (*p.borrow()).to_c_string_iterator(); loop { let __c1 = __it1.next(); @@ -154,7 +149,9 @@ fn main_0() -> i32 { } == 0) as i32) != 0) ); - libcc2rs::free_refcount((*(*(*r.borrow()).upgrade().deref()).name.borrow()).to_any()); + libcc2rs::free_refcount( + ((*r.borrow()).with(|__s: &record| (__s.name).clone()) as Ptr).to_any(), + ); return 0; } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/string_literal_ptr_init.rs b/tests/unit/out/refcount/string_literal_ptr_init.rs index 1cfd1c6eb7..4f0b8a377c 100644 --- a/tests/unit/out/refcount/string_literal_ptr_init.rs +++ b/tests/unit/out/refcount/string_literal_ptr_init.rs @@ -6,27 +6,21 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg)] +#[derive(Clone, Record, VaArg, FnPtrArg)] pub struct label { - pub name: Value>, - pub probe: Value i32>>, - pub mask: Value, -} -impl Clone for label { - fn clone(&self) -> Self { - Self { - name: Rc::new(RefCell::new((*self.name.borrow()).clone())), - probe: Rc::new(RefCell::new((*self.probe.borrow()).clone())), - mask: Rc::new(RefCell::new((*self.mask.borrow()).clone())), - } - } + #[offset(0)] + pub name: Ptr, + #[offset(8)] + pub probe: FnPtr i32>, + #[offset(16)] + pub mask: i32, } impl Default for label { fn default() -> Self { label { - name: Rc::new(RefCell::new(Ptr::::null())), - probe: Rc::new(RefCell::new(FnPtr:: i32>::null())), - mask: Rc::new(RefCell::new(0_i32)), + name: Ptr::::null(), + probe: FnPtr:: i32>::null(), + mask: 0_i32, } } } @@ -35,15 +29,15 @@ impl ByteRepr for label { 24 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.name.borrow()).to_bytes(&mut buf[0..8]); - (*self.probe.borrow()).to_bytes(&mut buf[8..16]); - (*self.mask.borrow()).to_bytes(&mut buf[16..20]); + self.name.to_bytes(&mut buf[0..8]); + self.probe.to_bytes(&mut buf[8..16]); + self.mask.to_bytes(&mut buf[16..20]); } fn from_bytes(buf: &[u8]) -> Self { Self { - name: Rc::new(RefCell::new(>::from_bytes(&buf[0..8]))), - probe: Rc::new(RefCell::new( i32>>::from_bytes(&buf[8..16]))), - mask: Rc::new(RefCell::new(::from_bytes(&buf[16..20]))), + name: >::from_bytes(&buf[0..8]), + probe: i32>>::from_bytes(&buf[8..16]), + mask: ::from_bytes(&buf[16..20]), } } } @@ -53,14 +47,14 @@ pub fn probe_two_0() -> i32 { thread_local!( pub static table_1: Value> = Rc::new(RefCell::new(Box::new([ label { - name: Rc::new(RefCell::new(Ptr::::from_string_literal(b"first"))), - probe: Rc::new(RefCell::new(FnPtr:: i32>::null())), - mask: Rc::new(RefCell::new((1 << 4))), + name: Ptr::::from_string_literal(b"first"), + probe: FnPtr:: i32>::null(), + mask: (1 << 4), }, label { - name: Rc::new(RefCell::new(Ptr::::from_string_literal(b"second"))), - probe: Rc::new(RefCell::new((FnPtr:: i32>::new(probe_two_0)))), - mask: Rc::new(RefCell::new((1 << 5))), + name: Ptr::::from_string_literal(b"second"), + probe: (FnPtr:: i32>::new(probe_two_0)), + mask: (1 << 5), }, ]))); ); @@ -70,41 +64,43 @@ pub fn main() { } fn main_0() -> i32 { assert!( - ((((((*(*table_1.with(Value::clone).borrow())[(0) as usize] - .name - .borrow()) + ((((({ + (*table_1.with(Value::clone).borrow())[(0) as usize] + .name + .clone() + } .offset((0) as isize) .read()) as i32) == ('f' as i32)) as i32) != 0) ); assert!( - ((((((*(*table_1.with(Value::clone).borrow())[(0) as usize] - .name - .borrow()) + ((((({ + (*table_1.with(Value::clone).borrow())[(0) as usize] + .name + .clone() + } .offset((4) as isize) .read()) as i32) == ('t' as i32)) as i32) != 0) ); assert!( - ((((*(*table_1.with(Value::clone).borrow())[(0) as usize] - .probe - .borrow()) + (((({ + (*table_1.with(Value::clone).borrow())[(0) as usize] + .probe + .clone() + }) .is_null()) as i32) != 0) ); + assert!(((({ (*table_1.with(Value::clone).borrow())[(0) as usize].mask } == 16) as i32) != 0)); assert!( - ((((*(*table_1.with(Value::clone).borrow())[(0) as usize] - .mask - .borrow()) - == 16) as i32) - != 0) - ); - assert!( - ((((((*(*table_1.with(Value::clone).borrow())[(1) as usize] - .name - .borrow()) + ((((({ + (*table_1.with(Value::clone).borrow())[(1) as usize] + .name + .clone() + } .offset((0) as isize) .read()) as i32) == ('s' as i32)) as i32) @@ -112,20 +108,16 @@ fn main_0() -> i32 { ); assert!( (((({ - (*(*table_1.with(Value::clone).borrow())[(1) as usize] - .probe - .borrow()) + { + (*table_1.with(Value::clone).borrow())[(1) as usize] + .probe + .clone() + } .call() }) == 1) as i32) != 0) ); - assert!( - ((((*(*table_1.with(Value::clone).borrow())[(1) as usize] - .mask - .borrow()) - == 32) as i32) - != 0) - ); + assert!(((({ (*table_1.with(Value::clone).borrow())[(1) as usize].mask } == 32) as i32) != 0)); let tail: Value> = Rc::new(RefCell::new( (Ptr::::from_string_literal(b"ab.cd").offset(2)), )); @@ -140,9 +132,11 @@ fn main_0() -> i32 { ); let have: Value = Rc::new(RefCell::new(0)); let p: Value = Rc::new(RefCell::new(if ((*have.borrow()) != 0) { - (*(*table_1.with(Value::clone).borrow())[(0) as usize] - .name - .borrow()) + ({ + (*table_1.with(Value::clone).borrow())[(0) as usize] + .name + .clone() + }) .to_any() } else { Ptr::::from_string_literal(b"").to_any() @@ -156,9 +150,11 @@ fn main_0() -> i32 { ); (*have.borrow_mut()) = 1; (*p.borrow_mut()) = if ((*have.borrow()) != 0) { - (*(*table_1.with(Value::clone).borrow())[(0) as usize] - .name - .borrow()) + ({ + (*table_1.with(Value::clone).borrow())[(0) as usize] + .name + .clone() + }) .to_any() } else { Ptr::::from_string_literal(b"").to_any() diff --git a/tests/unit/out/refcount/struct_ctor.rs b/tests/unit/out/refcount/struct_ctor.rs index cf521d7285..ba0e30707c 100644 --- a/tests/unit/out/refcount/struct_ctor.rs +++ b/tests/unit/out/refcount/struct_ctor.rs @@ -6,32 +6,24 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct StructWithCtor { - x1_: Value, - x2_: Value, + #[offset(0)] + x1_: i32, + #[offset(4)] + x2_: i32, } impl StructWithCtor { pub fn new(x1: i32, x2: i32) -> Self { let x1: Value = Rc::new(RefCell::new(x1)); let x2: Value = Rc::new(RefCell::new(x2)); let __this: Value = Rc::new(RefCell::new(Self { - x1_: Rc::new(RefCell::new((*x1.borrow()))), - x2_: Rc::new(RefCell::new((*x2.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - (*(*this.upgrade().deref()).x1_.borrow_mut()).prefix_inc(); - (*(*this.upgrade().deref()).x2_.borrow_mut()).prefix_dec(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } -} -impl Clone for StructWithCtor { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - x1_: Rc::new(RefCell::new((*self.x1_.borrow()))), - x2_: Rc::new(RefCell::new((*self.x2_.borrow()))), + x1_: (*x1.borrow()), + x2_: (*x2.borrow()), })); let this: Ptr = __this.as_pointer(); + this.with_mut(|__s: &mut StructWithCtor| __s.x1_.prefix_inc()); + this.with_mut(|__s: &mut StructWithCtor| __s.x2_.prefix_dec()); Rc::try_unwrap(__this).ok().unwrap().into_inner() } } @@ -40,38 +32,28 @@ impl ByteRepr for StructWithCtor { 8 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.x1_.borrow()).to_bytes(&mut buf[0..4]); - (*self.x2_.borrow()).to_bytes(&mut buf[4..8]); + self.x1_.to_bytes(&mut buf[0..4]); + self.x2_.to_bytes(&mut buf[4..8]); } fn from_bytes(buf: &[u8]) -> Self { Self { - x1_: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - x2_: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + x1_: ::from_bytes(&buf[0..4]), + x2_: ::from_bytes(&buf[4..8]), } } } pub fn foo_0(x: Ptr) -> Ptr { return (x).clone(); } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Value_ { - pub v: Value, + #[offset(0)] + pub v: i32, } impl Value_ { pub fn new(u: i32) -> Self { let u: Value = Rc::new(RefCell::new(u)); - let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*u.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } -} -impl Clone for Value_ { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*self.v.borrow()))), - })); + let __this: Value = Rc::new(RefCell::new(Self { v: (*u.borrow()) })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } @@ -81,34 +63,26 @@ impl ByteRepr for Value_ { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.v.borrow()).to_bytes(&mut buf[0..4]); + self.v.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - v: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + v: ::from_bytes(&buf[0..4]), } } } -#[derive(VaArg, FnPtrArg)] +#[derive(Clone, Record, VaArg, FnPtrArg)] pub struct Ptr_ { - pub v1: Value, - pub v2: Value, + #[offset(0)] + pub v1: Value_, + #[offset(4)] + pub v2: Value_, } impl Ptr_ { pub fn new() -> Self { let __this: Value = Rc::new(RefCell::new(Self { - v1: Rc::new(RefCell::new(Value_::new({ 11 }))), - v2: Rc::new(RefCell::new(Value_::new({ 22 }))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } -} -impl Clone for Ptr_ { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - v1: Rc::new(RefCell::new((*self.v1.borrow()).clone())), - v2: Rc::new(RefCell::new((*self.v2.borrow()).clone())), + v1: Value_::new({ 11 }), + v2: Value_::new({ 22 }), })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() @@ -124,13 +98,13 @@ impl ByteRepr for Ptr_ { 8 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.v1.borrow()).to_bytes(&mut buf[0..4]); - (*self.v2.borrow()).to_bytes(&mut buf[4..8]); + self.v1.to_bytes(&mut buf[0..4]); + self.v2.to_bytes(&mut buf[4..8]); } fn from_bytes(buf: &[u8]) -> Self { Self { - v1: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - v2: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + v1: ::from_bytes(&buf[0..4]), + v2: ::from_bytes(&buf[4..8]), } } } @@ -148,8 +122,8 @@ fn main_0() -> i32 { && ((({ StructWithCtorImpl::x2(&struct_with_ctor.as_pointer(),) }).read()) == 1) ); let p: Value = Rc::new(RefCell::new(Ptr_::new())); - assert!(((*(*(*p.borrow()).v1.borrow()).v.borrow()) == 11)); - assert!(((*(*(*p.borrow()).v2.borrow()).v.borrow()) == 22)); + assert!(({ (*p.borrow()).v1.v } == 11)); + assert!(({ (*p.borrow()).v2.v } == 22)); return 0; } pub trait StructWithCtorImpl { @@ -158,10 +132,10 @@ pub trait StructWithCtorImpl { } impl StructWithCtorImpl for Ptr { fn x1(&self) -> Ptr { - return (*(*self).upgrade().deref()).x1_.as_pointer(); + return field_ptr!((*self), x1_); } fn x2(&self) -> Ptr { - return (*(*self).upgrade().deref()).x2_.as_pointer(); + return field_ptr!((*self), x2_); } } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/struct_default_ctor.rs b/tests/unit/out/refcount/struct_default_ctor.rs index 8fa090ba96..e67c879120 100644 --- a/tests/unit/out/refcount/struct_default_ctor.rs +++ b/tests/unit/out/refcount/struct_default_ctor.rs @@ -6,27 +6,16 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg)] +#[derive(Clone, Record, VaArg, FnPtrArg)] pub struct S { - pub a: Value, - pub b: Value, + #[offset(0)] + pub a: i32, + #[offset(4)] + pub b: bool, } impl S { pub fn new() -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - a: Rc::new(RefCell::new(11)), - b: Rc::new(RefCell::new(true)), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } -} -impl Clone for S { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - a: Rc::new(RefCell::new((*self.a.borrow()))), - b: Rc::new(RefCell::new((*self.b.borrow()))), - })); + let __this: Value = Rc::new(RefCell::new(Self { a: 11, b: true })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } @@ -41,40 +30,32 @@ impl ByteRepr for S { 8 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.a.borrow()).to_bytes(&mut buf[0..4]); - (*self.b.borrow()).to_bytes(&mut buf[4..5]); + self.a.to_bytes(&mut buf[0..4]); + self.b.to_bytes(&mut buf[4..5]); } fn from_bytes(buf: &[u8]) -> Self { Self { - a: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - b: Rc::new(RefCell::new(::from_bytes(&buf[4..5]))), + a: ::from_bytes(&buf[0..4]), + b: ::from_bytes(&buf[4..5]), } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Declared { - pub v: Value, + #[offset(0)] + pub v: i32, } impl Declared {} -impl Clone for Declared { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*self.v.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } -} impl ByteRepr for Declared { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.v.borrow()).to_bytes(&mut buf[0..4]); + self.v.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - v: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + v: ::from_bytes(&buf[0..4]), } } } @@ -86,8 +67,8 @@ fn main_0() -> i32 { let d: Value> = Rc::new(RefCell::new(Ptr::::null())); assert!((*d.borrow()).is_null()); let s: Value = Rc::new(RefCell::new(S::new())); - assert!(((*(*s.borrow()).a.borrow()) == 11)); - assert!((((*(*s.borrow()).b.borrow()) as i32) == (true as i32))); + assert!(({ (*s.borrow()).a } == 11)); + assert!((({ (*s.borrow()).b } as i32) == (true as i32))); return 0; } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/struct_ptr.rs b/tests/unit/out/refcount/struct_ptr.rs index 37bce055e4..c38de8745c 100644 --- a/tests/unit/out/refcount/struct_ptr.rs +++ b/tests/unit/out/refcount/struct_ptr.rs @@ -6,29 +6,21 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct XX { - pub x: Value, -} -impl Clone for XX { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - x: Rc::new(RefCell::new((*self.x.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub x: i32, } impl ByteRepr for XX { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.x.borrow()).to_bytes(&mut buf[0..4]); + self.x.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - x: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + x: ::from_bytes(&buf[0..4]), } } } @@ -39,14 +31,14 @@ pub fn main() { fn main_0() -> i32 { let obj: Value = Rc::new(RefCell::new(::default())); let ptr: Value> = Rc::new(RefCell::new((obj.as_pointer()))); - (*(*(*ptr.borrow()).upgrade().deref()).x.borrow_mut()) = 2; + (*ptr.borrow()).with_mut(|__s: &mut XX| __s.x = 2); let c: Value = Rc::new(RefCell::new(false)); let r: Value = Rc::new(RefCell::new(if (*c.borrow()) { - (*(*obj.borrow()).x.borrow()) + { (*obj.borrow()).x } } else { - (*(*(*ptr.borrow()).upgrade().deref()).x.borrow()) + (*ptr.borrow()).with(|__s: &XX| __s.x) })); - let p: Value> = Rc::new(RefCell::new(((*obj.borrow()).x.as_pointer()))); + let p: Value> = Rc::new(RefCell::new((field_ptr!(obj, x)))); assert!( ({ let _lhs = ((*p.borrow()).read()); diff --git a/tests/unit/out/refcount/tag_vs_identifier_collision.rs b/tests/unit/out/refcount/tag_vs_identifier_collision.rs index 53a717fe08..1cebff6da8 100644 --- a/tests/unit/out/refcount/tag_vs_identifier_collision.rs +++ b/tests/unit/out/refcount/tag_vs_identifier_collision.rs @@ -10,59 +10,47 @@ pub type widget_enum = u32; pub const widget_enum_MODE_IDLE: widget_enum = 0; pub const widget_enum_MODE_ACTIVE: widget_enum = 1; pub const widget_enum_MODE_DONE: widget_enum = 2; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct widget { - pub id: Value, - pub mode: Value, -} -impl Clone for widget { - fn clone(&self) -> Self { - Self { - id: Rc::new(RefCell::new((*self.id.borrow()).clone())), - mode: Rc::new(RefCell::new((*self.mode.borrow()).clone())), - } - } + #[offset(0)] + pub id: i32, + #[offset(4)] + pub mode: widget_enum, } impl ByteRepr for widget { fn byte_size() -> usize { 8 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.id.borrow()).to_bytes(&mut buf[0..4]); - (*self.mode.borrow()).to_bytes(&mut buf[4..8]); + self.id.to_bytes(&mut buf[0..4]); + self.mode.to_bytes(&mut buf[4..8]); } fn from_bytes(buf: &[u8]) -> Self { Self { - id: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - mode: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + id: ::from_bytes(&buf[0..4]), + mode: ::from_bytes(&buf[4..8]), } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct point_struct { - pub x: Value, - pub y: Value, -} -impl Clone for point_struct { - fn clone(&self) -> Self { - Self { - x: Rc::new(RefCell::new((*self.x.borrow()).clone())), - y: Rc::new(RefCell::new((*self.y.borrow()).clone())), - } - } + #[offset(0)] + pub x: i32, + #[offset(4)] + pub y: i32, } impl ByteRepr for point_struct { fn byte_size() -> usize { 8 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.x.borrow()).to_bytes(&mut buf[0..4]); - (*self.y.borrow()).to_bytes(&mut buf[4..8]); + self.x.to_bytes(&mut buf[0..4]); + self.y.to_bytes(&mut buf[4..8]); } fn from_bytes(buf: &[u8]) -> Self { Self { - x: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - y: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + x: ::from_bytes(&buf[0..4]), + y: ::from_bytes(&buf[4..8]), } } } @@ -145,81 +133,63 @@ impl ByteRepr for slot_union { pub type slot = u32; pub const slot_SLOT_A: slot = 0; pub const slot_SLOT_B: slot = 1; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Inner { - pub tag_field: Value, -} -impl Clone for Inner { - fn clone(&self) -> Self { - Self { - tag_field: Rc::new(RefCell::new((*self.tag_field.borrow()).clone())), - } - } + #[offset(0)] + pub tag_field: i32, } impl ByteRepr for Inner { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.tag_field.borrow()).to_bytes(&mut buf[0..4]); + self.tag_field.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - tag_field: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + tag_field: ::from_bytes(&buf[0..4]), } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Outer { - pub field: Value, -} -impl Clone for Outer { - fn clone(&self) -> Self { - Self { - field: Rc::new(RefCell::new((*self.field.borrow()).clone())), - } - } + #[offset(0)] + pub field: Inner, } impl ByteRepr for Outer { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.field.borrow()).to_bytes(&mut buf[0..4]); + self.field.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - field: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + field: ::from_bytes(&buf[0..4]), } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Inner_struct { - pub typedef_field: Value, -} -impl Clone for Inner_struct { - fn clone(&self) -> Self { - Self { - typedef_field: Rc::new(RefCell::new((*self.typedef_field.borrow()).clone())), - } - } + #[offset(0)] + pub typedef_field: i32, } impl ByteRepr for Inner_struct { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.typedef_field.borrow()).to_bytes(&mut buf[0..4]); + self.typedef_field.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - typedef_field: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + typedef_field: ::from_bytes(&buf[0..4]), } } } pub fn is_active_0(w: Ptr) -> i32 { let w: Value> = Rc::new(RefCell::new(w)); - return ((((*(*(*w.borrow()).upgrade().deref()).mode.borrow()) as u32) + return ((((*w.borrow()).with(|__s: &widget| __s.mode) as u32) == ((widget_enum_MODE_ACTIVE as i32) as u32)) as i32); } pub fn main() { @@ -228,19 +198,18 @@ pub fn main() { } fn main_0() -> i32 { let w: Value = >::default(); - (*(*w.borrow()).id.borrow_mut()) = 7; - (*(*w.borrow()).mode.borrow_mut()) = widget_enum_MODE_ACTIVE; + (*w.borrow_mut()).id = 7; + (*w.borrow_mut()).mode = widget_enum_MODE_ACTIVE; assert!((({ is_active_0((w.as_pointer()),) }) != 0)); - (*(*w.borrow()).mode.borrow_mut()) = widget_enum_MODE_DONE; + (*w.borrow_mut()).mode = widget_enum_MODE_DONE; assert!( - (((((*(*w.borrow()).mode.borrow()) as u32) == ((widget_enum_MODE_DONE as i32) as u32)) - as i32) + (((({ (*w.borrow()).mode } as u32) == ((widget_enum_MODE_DONE as i32) as u32)) as i32) != 0) ); let p: Value = >::default(); - (*(*p.borrow()).x.borrow_mut()) = 3; - (*(*p.borrow()).y.borrow_mut()) = 4; - assert!((((((*(*p.borrow()).x.borrow()) + (*(*p.borrow()).y.borrow())) == 7) as i32) != 0)); + (*p.borrow_mut()).x = 3; + (*p.borrow_mut()).y = 4; + assert!((((({ (*p.borrow()).x } + { (*p.borrow()).y }) == 7) as i32) != 0)); let up: Value = >::default(); (*up.borrow_mut()).whole().write(5); assert!((((((*up.borrow()).whole().read()) == 5) as i32) != 0)); @@ -250,15 +219,15 @@ fn main_0() -> i32 { let e: Value = Rc::new(RefCell::new(slot_SLOT_B)); assert!((((((*e.borrow()) as u32) == ((slot_SLOT_B as i32) as u32)) as i32) != 0)); let inner_tag: Value = >::default(); - (*(*inner_tag.borrow()).tag_field.borrow_mut()) = 11; - assert!(((((*(*inner_tag.borrow()).tag_field.borrow()) == 11) as i32) != 0)); + (*inner_tag.borrow_mut()).tag_field = 11; + assert!(((({ (*inner_tag.borrow()).tag_field } == 11) as i32) != 0)); let inner_typedef: Value = >::default(); - (*(*inner_typedef.borrow()).typedef_field.borrow_mut()) = 22; - assert!(((((*(*inner_typedef.borrow()).typedef_field.borrow()) == 22) as i32) != 0)); + (*inner_typedef.borrow_mut()).typedef_field = 22; + assert!(((({ (*inner_typedef.borrow()).typedef_field } == 22) as i32) != 0)); let o: Value = >::default(); - (*(*(*o.borrow()).field.borrow()).tag_field.borrow_mut()) = 33; - assert!(((((*(*(*o.borrow()).field.borrow()).tag_field.borrow()) == 33) as i32) != 0)); - assert!(((((*(*w.borrow()).id.borrow()) == 7) as i32) != 0)); + (*o.borrow_mut()).field.tag_field = 33; + assert!(((({ (*o.borrow()).field.tag_field } == 33) as i32) != 0)); + assert!(((({ (*w.borrow()).id } == 7) as i32) != 0)); return 0; } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/template_default_arg.rs b/tests/unit/out/refcount/template_default_arg.rs index 9f1d86173f..c5ccd5f7f3 100644 --- a/tests/unit/out/refcount/template_default_arg.rs +++ b/tests/unit/out/refcount/template_default_arg.rs @@ -6,32 +6,20 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg)] +#[derive(Clone, Record, VaArg, FnPtrArg)] pub struct A { - pub v: Value, + #[offset(0)] + pub v: i32, } impl A { pub fn new_1() -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new(1)), - })); + let __this: Value = Rc::new(RefCell::new(Self { v: 1 })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } pub fn new_2(v: i32) -> Self { let v: Value = Rc::new(RefCell::new(v)); - let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*v.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } -} -impl Clone for A { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*self.v.borrow()))), - })); + let __this: Value = Rc::new(RefCell::new(Self { v: (*v.borrow()) })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } @@ -46,32 +34,22 @@ impl ByteRepr for A { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.v.borrow()).to_bytes(&mut buf[0..4]); + self.v.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - v: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + v: ::from_bytes(&buf[0..4]), } } } -#[derive(VaArg, FnPtrArg)] +#[derive(Clone, Record, VaArg, FnPtrArg)] pub struct B { - pub v: Value, + #[offset(0)] + pub v: i32, } impl B { pub fn new() -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new(2)), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } -} -impl Clone for B { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*self.v.borrow()))), - })); + let __this: Value = Rc::new(RefCell::new(Self { v: 2 })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } @@ -86,33 +64,23 @@ impl ByteRepr for B { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.v.borrow()).to_bytes(&mut buf[0..4]); + self.v.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - v: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + v: ::from_bytes(&buf[0..4]), } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct NoDefault { - pub v: Value, + #[offset(0)] + pub v: i32, } impl NoDefault { pub fn new(v: i32) -> Self { let v: Value = Rc::new(RefCell::new(v)); - let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*v.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } -} -impl Clone for NoDefault { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*self.v.borrow()))), - })); + let __this: Value = Rc::new(RefCell::new(Self { v: (*v.borrow()) })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } @@ -122,50 +90,40 @@ impl ByteRepr for NoDefault { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.v.borrow()).to_bytes(&mut buf[0..4]); + self.v.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - v: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + v: ::from_bytes(&buf[0..4]), } } } pub fn used_0(x: Option) -> i32 { let x: Value = Rc::new(RefCell::new(x.unwrap_or(A::new_1()))); - return (*(*x.borrow()).v.borrow()); + return { (*x.borrow()).v }; } pub fn used_1(x: Option) -> i32 { let x: Value = Rc::new(RefCell::new(x.unwrap_or(B::new()))); - return (*(*x.borrow()).v.borrow()); + return { (*x.borrow()).v }; } pub fn scaled_2(x: A, n: Option) -> i32 { let x: Value = Rc::new(RefCell::new(x)); let n: Value = Rc::new(RefCell::new(n.unwrap_or((4usize as i32)))); - return ((*(*x.borrow()).v.borrow()) * (*n.borrow())); + return ({ (*x.borrow()).v } * (*n.borrow())); } pub fn always_given_3(x: NoDefault) -> i32 { let x: Value = Rc::new(RefCell::new(x)); - return (*(*x.borrow()).v.borrow()); + return { (*x.borrow()).v }; } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct S_NoDefault_ { - pub v: Value, + #[offset(0)] + pub v: i32, } impl S_NoDefault_ { pub fn new(v: i32) -> Self { let v: Value = Rc::new(RefCell::new(v)); - let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*v.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } -} -impl Clone for S_NoDefault_ { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*self.v.borrow()))), - })); + let __this: Value = Rc::new(RefCell::new(Self { v: (*v.borrow()) })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } @@ -175,11 +133,11 @@ impl ByteRepr for S_NoDefault_ { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.v.borrow()).to_bytes(&mut buf[0..4]); + self.v.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - v: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + v: ::from_bytes(&buf[0..4]), } } } @@ -204,7 +162,7 @@ pub trait S_NoDefault_Impl { impl S_NoDefault_Impl for Ptr { fn get(&self, t: NoDefault) -> i32 { let t: Value = Rc::new(RefCell::new(t)); - return ((*(*(*self).upgrade().deref()).v.borrow()) + (*(*t.borrow()).v.borrow())); + return ((*self).with(|__s: &S_NoDefault_| __s.v) + { (*t.borrow()).v }); } } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/this.rs b/tests/unit/out/refcount/this.rs index bd26de3f9a..9e1e4d2114 100644 --- a/tests/unit/out/refcount/this.rs +++ b/tests/unit/out/refcount/this.rs @@ -6,17 +6,19 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct S { - pub a_: Value, - pub self__: Value>, + #[offset(0)] + pub a_: i32, + #[offset(8)] + pub self__: Ptr, } impl S { pub fn new_1(a: i32) -> Self { let a: Value = Rc::new(RefCell::new(a)); let __this: Value = Rc::new(RefCell::new(Self { - a_: Rc::new(RefCell::new((*a.borrow()))), - self__: Rc::new(RefCell::new(Ptr::::null())), + a_: (*a.borrow()), + self__: Ptr::::null(), })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() @@ -25,14 +27,14 @@ impl S { let a: Value = Rc::new(RefCell::new(a)); let other: Value> = Rc::new(RefCell::new(other)); let __this: Value = Rc::new(RefCell::new(Self { - a_: Rc::new(RefCell::new((*a.borrow()))), - self__: Rc::new(RefCell::new(Ptr::::null())), + a_: (*a.borrow()), + self__: Ptr::::null(), })); let this: Ptr = __this.as_pointer(); if (this == (*other.borrow())) { - (*(*this.upgrade().deref()).self__.borrow_mut()) = Ptr::::null(); + this.with_mut(|__s: &mut S| __s.self__ = Ptr::::null()); } else { - (*(*this.upgrade().deref()).self__.borrow_mut()) = (*other.borrow()).clone(); + this.with_mut(|__s: &mut S| __s.self__ = (*other.borrow()).clone()); } Rc::try_unwrap(__this).ok().unwrap().into_inner() } @@ -40,66 +42,46 @@ impl S { let a: Value = Rc::new(RefCell::new(a)); let other: Value> = Rc::new(RefCell::new(other)); let __this: Value = Rc::new(RefCell::new(Self { - a_: Rc::new(RefCell::new((*a.borrow()))), - self__: Rc::new(RefCell::new(Ptr::::null())), + a_: (*a.borrow()), + self__: Ptr::::null(), })); let this: Ptr = __this.as_pointer(); if (this == (*other.borrow())) { - (*(*this.upgrade().deref()).self__.borrow_mut()) = Ptr::::null(); + this.with_mut(|__s: &mut S| __s.self__ = Ptr::::null()); } Rc::try_unwrap(__this).ok().unwrap().into_inner() } } -impl Clone for S { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - a_: Rc::new(RefCell::new((*self.a_.borrow()))), - self__: Rc::new(RefCell::new((*self.self__.borrow()).clone())), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } -} impl ByteRepr for S { fn byte_size() -> usize { 16 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.a_.borrow()).to_bytes(&mut buf[0..4]); - (*self.self__.borrow()).to_bytes(&mut buf[8..16]); + self.a_.to_bytes(&mut buf[0..4]); + self.self__.to_bytes(&mut buf[8..16]); } fn from_bytes(buf: &[u8]) -> Self { Self { - a_: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - self__: Rc::new(RefCell::new(>::from_bytes(&buf[8..16]))), + a_: ::from_bytes(&buf[0..4]), + self__: >::from_bytes(&buf[8..16]), } } } pub fn bump_0(p: Ptr) { let p: Value> = Rc::new(RefCell::new(p)); - (*(*(*p.borrow()).upgrade().deref()).a_.borrow_mut()).postfix_inc(); + (*p.borrow()).with_mut(|__s: &mut S| __s.a_.postfix_inc()); } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct D { - pub a_: Value, + #[offset(0)] + pub a_: i32, } impl D { pub fn new(a: i32) -> Self { let a: Value = Rc::new(RefCell::new(a)); - let __this: Value = Rc::new(RefCell::new(Self { - a_: Rc::new(RefCell::new((*a.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - (*(*this.upgrade().deref()).a_.borrow_mut()) *= 2; - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } -} -impl Clone for D { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - a_: Rc::new(RefCell::new((*self.a_.borrow()))), - })); + let __this: Value = Rc::new(RefCell::new(Self { a_: (*a.borrow()) })); let this: Ptr = __this.as_pointer(); + this.with_mut(|__s: &mut D| __s.a_ *= 2); Rc::try_unwrap(__this).ok().unwrap().into_inner() } } @@ -108,11 +90,11 @@ impl ByteRepr for D { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.a_.borrow()).to_bytes(&mut buf[0..4]); + self.a_.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - a_: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + a_: ::from_bytes(&buf[0..4]), } } } @@ -123,34 +105,31 @@ pub fn main() { fn main_0() -> i32 { let s: Value = Rc::new(RefCell::new(S::new_1({ 1 }))); let ref_: Ptr = ({ SImpl::returns_this_reference(&s.as_pointer()) }); - (*(*ref_.upgrade().deref()).a_.borrow_mut()).postfix_inc(); - assert!(((*(*s.borrow()).a_.borrow()) == 2)); + ref_.with_mut(|__s: &mut S| __s.a_.postfix_inc()); + assert!(({ (*s.borrow()).a_ } == 2)); let ptr: Value> = Rc::new(RefCell::new( ({ SImpl::returns_this_pointer(&s.as_pointer()) }), )); - (*(*(*ptr.borrow()).upgrade().deref()).a_.borrow_mut()).postfix_inc(); - assert!(((*(*s.borrow()).a_.borrow()) == 3)); + (*ptr.borrow()).with_mut(|__s: &mut S| __s.a_.postfix_inc()); + assert!(({ (*s.borrow()).a_ } == 3)); ({ SImpl::inc(&({ SImpl::inc(&({ SImpl::inc(&s.as_pointer()) })) })) }); - assert!(((*(*s.borrow()).a_.borrow()) == 6)); + assert!(({ (*s.borrow()).a_ } == 6)); ({ SImpl::set_from_this(&s.as_pointer()) }); - assert!(((*(*s.borrow()).a_.borrow()) == 7)); + assert!(({ (*s.borrow()).a_ } == 7)); assert!((({ SImpl::twice(&s.as_pointer(),) }) == 14)); ({ SImpl::link(&s.as_pointer()) }); assert!({ - let _lhs = (*(*s.borrow()).self__.borrow()).clone(); + let _lhs = { (*s.borrow()).self__.clone() }; _lhs == (s.as_pointer()) }); - (*(*(*(*s.borrow()).self__.borrow()).upgrade().deref()) - .a_ - .borrow_mut()) - .postfix_inc(); - assert!(((*(*s.borrow()).a_.borrow()) == 8)); + { (*s.borrow()).self__.clone() }.with_mut(|__s: &mut S| __s.a_.postfix_inc()); + assert!(({ (*s.borrow()).a_ } == 8)); ({ SImpl::bump_me(&s.as_pointer()) }); - assert!(((*(*s.borrow()).a_.borrow()) == 9)); + assert!(({ (*s.borrow()).a_ } == 9)); let d: Value = Rc::new(RefCell::new(D::new({ 3 }))); - assert!(((*(*d.borrow()).a_.borrow()) == 6)); + assert!(({ (*d.borrow()).a_ } == 6)); let cr: Ptr = ({ SImpl::cref(&s.as_pointer()) }); - assert!(((*(*cr.upgrade().deref()).a_.borrow()) == 9)); + assert!((cr.with(|__s: &S| __s.a_) == 9)); let t: Value = Rc::new(RefCell::new(S::new_1({ 0 }))); assert!( ({ @@ -163,14 +142,14 @@ fn main_0() -> i32 { let q: Value> = Rc::new(RefCell::new( ({ SImpl::returns_this_pointer(&(*p.borrow())) }), )); - (*(*(*q.borrow()).upgrade().deref()).a_.borrow_mut()).postfix_inc(); - assert!(((*(*(*p.borrow()).upgrade().deref()).a_.borrow()) == 2)); + (*q.borrow()).with_mut(|__s: &mut S| __s.a_.postfix_inc()); + assert!(((*p.borrow()).with(|__s: &S| __s.a_) == 2)); (*p.borrow()).delete(); let h: Value> = Rc::new(RefCell::new(Ptr::alloc(S::new_1({ 5 })))); ({ SImpl::destroy(&(*h.borrow())) }); ({ SImpl::reset(&s.as_pointer()) }); - assert!(((*(*s.borrow()).a_.borrow()) == 0)); - assert!((*(*s.borrow()).self__.borrow()).is_null()); + assert!(({ (*s.borrow()).a_ } == 0)); + assert!(({ (*s.borrow()).self__.clone() }).is_null()); assert!( ((({ let _other: Ptr = (s.as_pointer()); @@ -190,19 +169,19 @@ fn main_0() -> i32 { ((({ SImpl::copy_if_different(&s.as_pointer(), (other.as_pointer()),) }) as i32) == (true as i32)) ); - assert!(((*(*s.borrow()).a_.borrow()) == (*(*other.borrow()).a_.borrow()))); + assert!(({ (*s.borrow()).a_ } == { (*other.borrow()).a_ })); assert!({ - let _lhs = (*(*s.borrow()).self__.borrow()).clone(); - _lhs == (*(*other.borrow()).self__.borrow()).clone() + let _lhs = { (*s.borrow()).self__.clone() }; + _lhs == { (*other.borrow()).self__.clone() } }); let u: Value = Rc::new(RefCell::new(S::new_2({ 1 }, { (s.as_pointer()) }))); assert!({ - let _lhs = (*(*u.borrow()).self__.borrow()).clone(); + let _lhs = { (*u.borrow()).self__.clone() }; _lhs == (s.as_pointer()) }); let s_const: Value = Rc::new(RefCell::new(S::new_1({ 100 }))); let u1: Value = Rc::new(RefCell::new(S::new_3({ 1 }, { (s_const.as_pointer()) }))); - assert!((*(*u1.borrow()).self__.borrow()).is_null()); + assert!(({ (*u1.borrow()).self__.clone() }).is_null()); return 0; } pub trait SImpl { @@ -229,21 +208,22 @@ impl SImpl for Ptr { return (*self).clone(); } fn inc(&self) -> Ptr { - (*(*(*self).upgrade().deref()).a_.borrow_mut()).postfix_inc(); + (*self).with_mut(|__s: &mut S| __s.a_.postfix_inc()); return (*self).clone(); } fn set_from_this(&self) { - let __rhs = ((*(*(*self).upgrade().deref()).a_.borrow()) + 1); - (*(*(*self).upgrade().deref()).a_.borrow_mut()) = __rhs; + let __rhs = ((*self).with(|__s: &S| __s.a_) + 1); + (*self).with_mut(|__s: &mut S| __s.a_ = __rhs); } fn get(&self) -> i32 { - return (*(*(*self).upgrade().deref()).a_.borrow()); + return (*self).with(|__s: &S| __s.a_); } fn twice(&self) -> i32 { return (({ SImpl::get(self) }) * 2); } fn link(&self) { - (*(*(*self).upgrade().deref()).self__.borrow_mut()) = (*self).clone(); + let __rhs = (*self).clone(); + (*self).with_mut(|__s: &mut S| __s.self__ = __rhs); } fn bump_me(&self) { ({ bump_0((*self).clone()) }); @@ -266,10 +246,10 @@ impl SImpl for Ptr { if ((*self) == (*other.borrow())) { return false; } - let __rhs = (*(*(*other.borrow()).upgrade().deref()).a_.borrow()); - (*(*(*self).upgrade().deref()).a_.borrow_mut()) = __rhs; - let __rhs = (*(*(*other.borrow()).upgrade().deref()).self__.borrow()).clone(); - (*(*(*self).upgrade().deref()).self__.borrow_mut()) = __rhs; + let __rhs = (*other.borrow()).with(|__s: &S| __s.a_); + (*self).with_mut(|__s: &mut S| __s.a_ = __rhs); + let __rhs = (*other.borrow()).with(|__s: &S| (__s.self__).clone()); + (*self).with_mut(|__s: &mut S| __s.self__ = __rhs); return true; } fn copy_if_different(&self, other: Ptr) -> bool { @@ -277,10 +257,10 @@ impl SImpl for Ptr { if ((*self) == (*other.borrow())) { return false; } - let __rhs = (*(*(*other.borrow()).upgrade().deref()).a_.borrow()); - (*(*(*self).upgrade().deref()).a_.borrow_mut()) = __rhs; - let __rhs = (*(*(*other.borrow()).upgrade().deref()).self__.borrow()).clone(); - (*(*(*self).upgrade().deref()).self__.borrow_mut()) = __rhs; + let __rhs = (*other.borrow()).with(|__s: &S| __s.a_); + (*self).with_mut(|__s: &mut S| __s.a_ = __rhs); + let __rhs = (*other.borrow()).with(|__s: &S| (__s.self__).clone()); + (*self).with_mut(|__s: &mut S| __s.self__ = __rhs); return true; } } diff --git a/tests/unit/out/refcount/typedef-anon-struct.rs b/tests/unit/out/refcount/typedef-anon-struct.rs index 8b579910b1..640b353ed3 100644 --- a/tests/unit/out/refcount/typedef-anon-struct.rs +++ b/tests/unit/out/refcount/typedef-anon-struct.rs @@ -6,50 +6,59 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Outer_RunInfo { - pub block_idx: Value, - pub num_extra_zero_runs: Value, -} -impl Clone for Outer_RunInfo { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - block_idx: Rc::new(RefCell::new((*self.block_idx.borrow()))), - num_extra_zero_runs: Rc::new(RefCell::new((*self.num_extra_zero_runs.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub block_idx: i32, + #[offset(4)] + pub num_extra_zero_runs: i32, } impl ByteRepr for Outer_RunInfo { fn byte_size() -> usize { 8 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.block_idx.borrow()).to_bytes(&mut buf[0..4]); - (*self.num_extra_zero_runs.borrow()).to_bytes(&mut buf[4..8]); + self.block_idx.to_bytes(&mut buf[0..4]); + self.num_extra_zero_runs.to_bytes(&mut buf[4..8]); } fn from_bytes(buf: &[u8]) -> Self { Self { - block_idx: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - num_extra_zero_runs: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + block_idx: ::from_bytes(&buf[0..4]), + num_extra_zero_runs: ::from_bytes(&buf[4..8]), } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Record, VaArg, FnPtrArg, Default)] pub struct Outer { + #[offset(0)] pub runs: Value>, } impl Clone for Outer { fn clone(&self) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - runs: Rc::new(RefCell::new((*self.runs.borrow()).clone())), + runs: { Rc::new(RefCell::new((*self.runs.borrow()).clone())) }, })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } } +<<<<<<< HEAD impl ByteRepr for Outer {} +======= +impl ByteRepr for Outer { + fn byte_size() -> usize { + 24 + } + fn to_bytes(&self, buf: &mut [u8]) { + self.runs.to_bytes(&mut buf[0..24]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + runs: >>::from_bytes(&buf[0..24]), + } + } +} +>>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub fn main() { __cpp2rust_init_globals(); std::process::exit(main_0()); @@ -57,31 +66,15 @@ pub fn main() { fn main_0() -> i32 { let o: Value = Rc::new(RefCell::new(::default())); let info: Value = Rc::new(RefCell::new(::default())); - (*(*info.borrow()).block_idx.borrow_mut()) = 1; - (*(*info.borrow()).num_extra_zero_runs.borrow_mut()) = 2; + (*info.borrow_mut()).block_idx = 1; + (*info.borrow_mut()).num_extra_zero_runs = 2; { let a0_clone = (*info.borrow()).clone(); (*(*o.borrow()).runs.borrow_mut()).push(a0_clone) }; assert!(((*(*o.borrow()).runs.borrow()).len() == 1_usize)); - assert!( - ((*(*((*o.borrow()).runs.as_pointer() as Ptr) - .offset(0_usize) - .upgrade() - .deref()) - .block_idx - .borrow()) - == 1) - ); - assert!( - ((*(*((*o.borrow()).runs.as_pointer() as Ptr) - .offset(0_usize) - .upgrade() - .deref()) - .num_extra_zero_runs - .borrow()) - == 2) - ); + assert!(({ (*(*o.borrow()).runs.borrow())[(0_usize) as usize].block_idx } == 1)); + assert!(({ (*(*o.borrow()).runs.borrow())[(0_usize) as usize].num_extra_zero_runs } == 2)); return 0; } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/unary_expr_or_type_trait.rs b/tests/unit/out/refcount/unary_expr_or_type_trait.rs index b15e7e241b..e0cf61262f 100644 --- a/tests/unit/out/refcount/unary_expr_or_type_trait.rs +++ b/tests/unit/out/refcount/unary_expr_or_type_trait.rs @@ -6,33 +6,25 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct S { - pub c: Value, - pub x: Value, -} -impl Clone for S { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - c: Rc::new(RefCell::new((*self.c.borrow()))), - x: Rc::new(RefCell::new((*self.x.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub c: u8, + #[offset(8)] + pub x: i64, } impl ByteRepr for S { fn byte_size() -> usize { 16 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.c.borrow()).to_bytes(&mut buf[0..1]); - (*self.x.borrow()).to_bytes(&mut buf[8..16]); + self.c.to_bytes(&mut buf[0..1]); + self.x.to_bytes(&mut buf[8..16]); } fn from_bytes(buf: &[u8]) -> Self { Self { - c: Rc::new(RefCell::new(::from_bytes(&buf[0..1]))), - x: Rc::new(RefCell::new(::from_bytes(&buf[8..16]))), + c: ::from_bytes(&buf[0..1]), + x: ::from_bytes(&buf[8..16]), } } } diff --git a/tests/unit/out/refcount/union_addrof_external.rs b/tests/unit/out/refcount/union_addrof_external.rs index 5995c26d31..3d7031007d 100644 --- a/tests/unit/out/refcount/union_addrof_external.rs +++ b/tests/unit/out/refcount/union_addrof_external.rs @@ -6,30 +6,24 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg)] +#[derive(Clone, Record, VaArg, FnPtrArg)] pub struct record { - pub code: Value, - pub lo: Value, - pub hi: Value, - pub pad: Value>, -} -impl Clone for record { - fn clone(&self) -> Self { - Self { - code: Rc::new(RefCell::new((*self.code.borrow()).clone())), - lo: Rc::new(RefCell::new((*self.lo.borrow()).clone())), - hi: Rc::new(RefCell::new((*self.hi.borrow()).clone())), - pad: Rc::new(RefCell::new((*self.pad.borrow()).clone())), - } - } + #[offset(0)] + pub code: u16, + #[offset(2)] + pub lo: u16, + #[offset(4)] + pub hi: u32, + #[offset(8)] + pub pad: Box<[u8]>, } impl Default for record { fn default() -> Self { record { - code: Rc::new(RefCell::new(0_u16)), - lo: Rc::new(RefCell::new(0_u16)), - hi: Rc::new(RefCell::new(0_u32)), - pad: Rc::new(RefCell::new((0..8).map(|_| 0_u8).collect::>())), + code: 0_u16, + lo: 0_u16, + hi: 0_u32, + pad: (0..8).map(|_| 0_u8).collect::>(), } } } @@ -38,17 +32,17 @@ impl ByteRepr for record { 16 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.code.borrow()).to_bytes(&mut buf[0..2]); - (*self.lo.borrow()).to_bytes(&mut buf[2..4]); - (*self.hi.borrow()).to_bytes(&mut buf[4..8]); - (*self.pad.borrow()).to_bytes(&mut buf[8..16]); + self.code.to_bytes(&mut buf[0..2]); + self.lo.to_bytes(&mut buf[2..4]); + self.hi.to_bytes(&mut buf[4..8]); + self.pad.to_bytes(&mut buf[8..16]); } fn from_bytes(buf: &[u8]) -> Self { Self { - code: Rc::new(RefCell::new(::from_bytes(&buf[0..2]))), - lo: Rc::new(RefCell::new(::from_bytes(&buf[2..4]))), - hi: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), - pad: Rc::new(RefCell::new(>::from_bytes(&buf[8..16]))), + code: ::from_bytes(&buf[0..2]), + lo: ::from_bytes(&buf[2..4]), + hi: ::from_bytes(&buf[4..8]), + pad: >::from_bytes(&buf[8..16]), } } } @@ -90,27 +84,21 @@ impl ByteRepr for anon_0 { } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Container { - pub view: Value, -} -impl Clone for Container { - fn clone(&self) -> Self { - Self { - view: Rc::new(RefCell::new((*self.view.borrow()).clone())), - } - } + #[offset(0)] + pub view: anon_0, } impl ByteRepr for Container { fn byte_size() -> usize { 128 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.view.borrow()).to_bytes(&mut buf[0..128]); + self.view.to_bytes(&mut buf[0..128]); } fn from_bytes(buf: &[u8]) -> Self { Self { - view: Rc::new(RefCell::new(::from_bytes(&buf[0..128]))), + view: ::from_bytes(&buf[0..128]), } } } @@ -157,50 +145,34 @@ fn main_0() -> i32 { ((c.as_pointer()) as Ptr).to_any() }; ({ - let _out: AnyPtr = ((*c.borrow()).view.as_pointer()).to_any(); + let _out: AnyPtr = (field_ptr!(c, view)).to_any(); let _cap: usize = 128usize; fill_1(_out, _cap) }); + assert!((((((*c.borrow()).view.h().with(|__s: &record| __s.code) as i32) == 2) as i32) != 0)); assert!( - (((((*(*(*(*c.borrow()).view.borrow()).h().upgrade().deref()) - .code - .borrow()) as i32) - == 2) as i32) - != 0) - ); - assert!( - ((((((((*(*(*c.borrow()).view.borrow()).h().upgrade().deref()) - .lo - .as_pointer()) - .reinterpret_cast::()) - .offset((0) as isize) - .read()) as i32) + (((((((field_ptr!((*c.borrow()).view.h(), lo)).reinterpret_cast::()) + .offset((0) as isize) + .read()) as i32) == 0) as i32) != 0) ); assert!( - ((((((((*(*(*c.borrow()).view.borrow()).h().upgrade().deref()) - .lo - .as_pointer()) - .reinterpret_cast::()) - .offset((1) as isize) - .read()) as i32) + (((((((field_ptr!((*c.borrow()).view.h(), lo)).reinterpret_cast::()) + .offset((1) as isize) + .read()) as i32) == 80) as i32) != 0) ); assert!( - (((((((*(*c.borrow()).view.borrow()) - .raw_() - .reinterpret_cast::() as Ptr::) + (((((((*c.borrow()).view.raw_().reinterpret_cast::() as Ptr::) .offset((0) as isize) .read()) as i32) == 2) as i32) != 0) ); assert!( - ((((((((*(*c.borrow()).view.borrow()) - .raw_() - .reinterpret_cast::() as Ptr::) + ((((((((*c.borrow()).view.raw_().reinterpret_cast::() as Ptr::) .offset((3) as isize) .read()) as u8) as i32) == 80) as i32) diff --git a/tests/unit/out/refcount/union_cross_arm_cast.rs b/tests/unit/out/refcount/union_cross_arm_cast.rs index fb95449621..3a29d2cd4a 100644 --- a/tests/unit/out/refcount/union_cross_arm_cast.rs +++ b/tests/unit/out/refcount/union_cross_arm_cast.rs @@ -6,24 +6,18 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg)] +#[derive(Clone, Record, VaArg, FnPtrArg)] pub struct shape_a { - pub code: Value, - pub pad: Value>, -} -impl Clone for shape_a { - fn clone(&self) -> Self { - Self { - code: Rc::new(RefCell::new((*self.code.borrow()).clone())), - pad: Rc::new(RefCell::new((*self.pad.borrow()).clone())), - } - } + #[offset(0)] + pub code: u16, + #[offset(2)] + pub pad: Box<[u8]>, } impl Default for shape_a { fn default() -> Self { shape_a { - code: Rc::new(RefCell::new(0_u16)), - pad: Rc::new(RefCell::new((0..14).map(|_| 0_u8).collect::>())), + code: 0_u16, + pad: (0..14).map(|_| 0_u8).collect::>(), } } } @@ -32,43 +26,37 @@ impl ByteRepr for shape_a { 16 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.code.borrow()).to_bytes(&mut buf[0..2]); - (*self.pad.borrow()).to_bytes(&mut buf[2..16]); + self.code.to_bytes(&mut buf[0..2]); + self.pad.to_bytes(&mut buf[2..16]); } fn from_bytes(buf: &[u8]) -> Self { Self { - code: Rc::new(RefCell::new(::from_bytes(&buf[0..2]))), - pad: Rc::new(RefCell::new(>::from_bytes(&buf[2..16]))), + code: ::from_bytes(&buf[0..2]), + pad: >::from_bytes(&buf[2..16]), } } } -#[derive(VaArg, FnPtrArg)] +#[derive(Clone, Record, VaArg, FnPtrArg)] pub struct shape_b { - pub code: Value, - pub lo: Value, - pub mid: Value, - pub fill: Value>, - pub tail: Value, -} -impl Clone for shape_b { - fn clone(&self) -> Self { - Self { - code: Rc::new(RefCell::new((*self.code.borrow()).clone())), - lo: Rc::new(RefCell::new((*self.lo.borrow()).clone())), - mid: Rc::new(RefCell::new((*self.mid.borrow()).clone())), - fill: Rc::new(RefCell::new((*self.fill.borrow()).clone())), - tail: Rc::new(RefCell::new((*self.tail.borrow()).clone())), - } - } + #[offset(0)] + pub code: u16, + #[offset(2)] + pub lo: u16, + #[offset(4)] + pub mid: u32, + #[offset(8)] + pub fill: Box<[u8]>, + #[offset(24)] + pub tail: u32, } impl Default for shape_b { fn default() -> Self { shape_b { - code: Rc::new(RefCell::new(0_u16)), - lo: Rc::new(RefCell::new(0_u16)), - mid: Rc::new(RefCell::new(0_u32)), - fill: Rc::new(RefCell::new((0..16).map(|_| 0_u8).collect::>())), - tail: Rc::new(RefCell::new(0_u32)), + code: 0_u16, + lo: 0_u16, + mid: 0_u32, + fill: (0..16).map(|_| 0_u8).collect::>(), + tail: 0_u32, } } } @@ -77,19 +65,19 @@ impl ByteRepr for shape_b { 28 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.code.borrow()).to_bytes(&mut buf[0..2]); - (*self.lo.borrow()).to_bytes(&mut buf[2..4]); - (*self.mid.borrow()).to_bytes(&mut buf[4..8]); - (*self.fill.borrow()).to_bytes(&mut buf[8..24]); - (*self.tail.borrow()).to_bytes(&mut buf[24..28]); + self.code.to_bytes(&mut buf[0..2]); + self.lo.to_bytes(&mut buf[2..4]); + self.mid.to_bytes(&mut buf[4..8]); + self.fill.to_bytes(&mut buf[8..24]); + self.tail.to_bytes(&mut buf[24..28]); } fn from_bytes(buf: &[u8]) -> Self { Self { - code: Rc::new(RefCell::new(::from_bytes(&buf[0..2]))), - lo: Rc::new(RefCell::new(::from_bytes(&buf[2..4]))), - mid: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), - fill: Rc::new(RefCell::new(>::from_bytes(&buf[8..24]))), - tail: Rc::new(RefCell::new(::from_bytes(&buf[24..28]))), + code: ::from_bytes(&buf[0..2]), + lo: ::from_bytes(&buf[2..4]), + mid: ::from_bytes(&buf[4..8]), + fill: >::from_bytes(&buf[8..24]), + tail: ::from_bytes(&buf[24..28]), } } } @@ -134,31 +122,25 @@ impl ByteRepr for anon_0 { } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Container { - pub len: Value, - pub u: Value, -} -impl Clone for Container { - fn clone(&self) -> Self { - Self { - len: Rc::new(RefCell::new((*self.len.borrow()).clone())), - u: Rc::new(RefCell::new((*self.u.borrow()).clone())), - } - } + #[offset(0)] + pub len: u32, + #[offset(4)] + pub u: anon_0, } impl ByteRepr for Container { fn byte_size() -> usize { 68 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.len.borrow()).to_bytes(&mut buf[0..4]); - (*self.u.borrow()).to_bytes(&mut buf[4..68]); + self.len.to_bytes(&mut buf[0..4]); + self.u.to_bytes(&mut buf[4..68]); } fn from_bytes(buf: &[u8]) -> Self { Self { - len: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - u: Rc::new(RefCell::new(::from_bytes(&buf[4..68]))), + len: ::from_bytes(&buf[0..4]), + u: ::from_bytes(&buf[4..68]), } } } @@ -174,47 +156,30 @@ fn main_0() -> i32 { .memset((0) as u8, 68usize as usize); ((c.as_pointer()) as Ptr).to_any() }; - (*(*(*(*c.borrow()).u.borrow()).a().upgrade().deref()) - .code - .borrow_mut()) = 10_u16; - (*(*c.borrow()).len.borrow_mut()) = (28usize as u32); - (*(*(((*(*c.borrow()).u.borrow()).a()) - .to_any() - .reinterpret_cast::()) - .upgrade() - .deref()) - .tail - .borrow_mut()) = 3735928559_u32; - assert!( - ((((*(*(*(*c.borrow()).u.borrow()).b().upgrade().deref()) - .tail - .borrow()) - == 3735928559_u32) as i32) - != 0) - ); - assert!( - (((((*(*(*(*c.borrow()).u.borrow()).b().upgrade().deref()) - .code - .borrow()) as i32) - == 10) as i32) - != 0) - ); - (*(*(*(*c.borrow()).u.borrow()).b().upgrade().deref()) - .lo - .borrow_mut()) = 8080_u16; + (*c.borrow()) + .u + .a() + .with_mut(|__s: &mut shape_a| __s.code = 10_u16); + (*c.borrow_mut()).len = (28usize as u32); + (((*c.borrow()).u.a()).to_any().reinterpret_cast::()) + .with_mut(|__s: &mut shape_b| __s.tail = 3735928559_u32); + assert!(((((*c.borrow()).u.b().with(|__s: &shape_b| __s.tail) == 3735928559_u32) as i32) != 0)); + assert!((((((*c.borrow()).u.b().with(|__s: &shape_b| __s.code) as i32) == 10) as i32) != 0)); + (*c.borrow()) + .u + .b() + .with_mut(|__s: &mut shape_b| __s.lo = 8080_u16); assert!( - ((((((((*(*c.borrow()).u.borrow()).raw_().reinterpret_cast::()) - .reinterpret_cast::()) - .offset((2) as isize) - .read()) as i32) + ((((((((*c.borrow()).u.raw_().reinterpret_cast::()).reinterpret_cast::()) + .offset((2) as isize) + .read()) as i32) == 144) as i32) != 0) ); assert!( - ((((((((*(*c.borrow()).u.borrow()).raw_().reinterpret_cast::()) - .reinterpret_cast::()) - .offset((3) as isize) - .read()) as i32) + ((((((((*c.borrow()).u.raw_().reinterpret_cast::()).reinterpret_cast::()) + .offset((3) as isize) + .read()) as i32) == 31) as i32) != 0) ); diff --git a/tests/unit/out/refcount/union_field_alignment.rs b/tests/unit/out/refcount/union_field_alignment.rs index a88b6f1d4a..1e44418f16 100644 --- a/tests/unit/out/refcount/union_field_alignment.rs +++ b/tests/unit/out/refcount/union_field_alignment.rs @@ -44,31 +44,25 @@ impl ByteRepr for anon_0 { } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct node { - pub next: Value>, - pub x: Value, -} -impl Clone for node { - fn clone(&self) -> Self { - Self { - next: Rc::new(RefCell::new((*self.next.borrow()).clone())), - x: Rc::new(RefCell::new((*self.x.borrow()).clone())), - } - } + #[offset(0)] + pub next: Ptr, + #[offset(8)] + pub x: anon_0, } impl ByteRepr for node { fn byte_size() -> usize { 16 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.next.borrow()).to_bytes(&mut buf[0..8]); - (*self.x.borrow()).to_bytes(&mut buf[8..16]); + self.next.to_bytes(&mut buf[0..8]); + self.x.to_bytes(&mut buf[8..16]); } fn from_bytes(buf: &[u8]) -> Self { Self { - next: Rc::new(RefCell::new(>::from_bytes(&buf[0..8]))), - x: Rc::new(RefCell::new(::from_bytes(&buf[8..16]))), + next: >::from_bytes(&buf[0..8]), + x: ::from_bytes(&buf[8..16]), } } } @@ -78,12 +72,11 @@ pub fn main() { } fn main_0() -> i32 { let n: Value = >::default(); - (*(*n.borrow()).next.borrow_mut()) = Ptr::::null(); - ((*(*n.borrow()).x.borrow()).bytes().reinterpret_cast::() as Ptr) - .offset((0) as isize) + (*n.borrow_mut()).next = Ptr::::null(); + { ((*n.borrow()).x.bytes().reinterpret_cast::() as Ptr).offset((0) as isize) } .write(171_u8); assert!( - (((((((*(*n.borrow()).x.borrow()).bytes().reinterpret_cast::() as Ptr::) + (((((((*n.borrow()).x.bytes().reinterpret_cast::() as Ptr::) .offset((0) as isize) .read()) as i32) == 171) as i32) diff --git a/tests/unit/out/refcount/union_flex_array_member.rs b/tests/unit/out/refcount/union_flex_array_member.rs index 6ff6c49c65..263e7bddf0 100644 --- a/tests/unit/out/refcount/union_flex_array_member.rs +++ b/tests/unit/out/refcount/union_flex_array_member.rs @@ -44,35 +44,29 @@ impl ByteRepr for anon_0 { } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct node { - pub len: Value, - pub pos: Value, - pub x: Value, -} -impl Clone for node { - fn clone(&self) -> Self { - Self { - len: Rc::new(RefCell::new((*self.len.borrow()).clone())), - pos: Rc::new(RefCell::new((*self.pos.borrow()).clone())), - x: Rc::new(RefCell::new((*self.x.borrow()).clone())), - } - } + #[offset(0)] + pub len: usize, + #[offset(8)] + pub pos: usize, + #[offset(16)] + pub x: anon_0, } impl ByteRepr for node { fn byte_size() -> usize { 24 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.len.borrow()).to_bytes(&mut buf[0..8]); - (*self.pos.borrow()).to_bytes(&mut buf[8..16]); - (*self.x.borrow()).to_bytes(&mut buf[16..24]); + self.len.to_bytes(&mut buf[0..8]); + self.pos.to_bytes(&mut buf[8..16]); + self.x.to_bytes(&mut buf[16..24]); } fn from_bytes(buf: &[u8]) -> Self { Self { - len: Rc::new(RefCell::new(::from_bytes(&buf[0..8]))), - pos: Rc::new(RefCell::new(::from_bytes(&buf[8..16]))), - x: Rc::new(RefCell::new(::from_bytes(&buf[16..24]))), + len: ::from_bytes(&buf[0..8]), + pos: ::from_bytes(&buf[8..16]), + x: ::from_bytes(&buf[16..24]), } } } @@ -88,22 +82,26 @@ fn main_0() -> i32 { ) .reinterpret_cast::(), )); - (*(*(*n.borrow()).upgrade().deref()).len.borrow_mut()) = (*tail_size.borrow()); + (*n.borrow()).with_mut(|__s: &mut node| __s.len = (*tail_size.borrow())); let i: Value = Rc::new(RefCell::new(0_usize)); 'loop_: while ((((*i.borrow()) < (*tail_size.borrow())) as i32) != 0) { let __rhs = (((*i.borrow()) & 255_usize) as u8); - ((*(*(*n.borrow()).upgrade().deref()).x.borrow()) - .bytes() - .reinterpret_cast::() as Ptr) - .offset((*i.borrow()) as isize) - .write(__rhs); + { + ((*(*n.borrow()).upgrade().deref()) + .x + .bytes() + .reinterpret_cast::() as Ptr) + .offset((*i.borrow()) as isize) + } + .write(__rhs); (*i.borrow_mut()).postfix_inc(); } let i: Value = Rc::new(RefCell::new(0_usize)); 'loop_: while ((((*i.borrow()) < (*tail_size.borrow())) as i32) != 0) { assert!( ((({ - let _lhs = ((((*(*(*n.borrow()).upgrade().deref()).x.borrow()) + let _lhs = ((((*(*n.borrow()).upgrade().deref()) + .x .bytes() .reinterpret_cast::() as Ptr) .offset((*i.borrow()) as isize) @@ -115,7 +113,8 @@ fn main_0() -> i32 { (*i.borrow_mut()).postfix_inc(); } let p: Value> = Rc::new(RefCell::new( - (((*(*(*n.borrow()).upgrade().deref()).x.borrow()) + (((*(*n.borrow()).upgrade().deref()) + .x .bytes() .reinterpret_cast::() as Ptr) .offset((10) as isize)), @@ -123,7 +122,8 @@ fn main_0() -> i32 { assert!(((((((*p.borrow()).read()) as i32) == 10) as i32) != 0)); (*p.borrow()).write(170_u8); assert!( - (((((((*(*(*n.borrow()).upgrade().deref()).x.borrow()) + (((((((*(*n.borrow()).upgrade().deref()) + .x .bytes() .reinterpret_cast::() as Ptr::) .offset((10) as isize) @@ -131,12 +131,13 @@ fn main_0() -> i32 { == 170) as i32) != 0) ); - (*(*(*n.borrow()).upgrade().deref()).pos.borrow_mut()) = 20_usize; + (*n.borrow()).with_mut(|__s: &mut node| __s.pos = 20_usize); let q: Value> = Rc::new(RefCell::new( - (((*(*(*n.borrow()).upgrade().deref()).x.borrow()) + (((*(*n.borrow()).upgrade().deref()) + .x .bytes() .reinterpret_cast::() as Ptr) - .offset((*(*(*n.borrow()).upgrade().deref()).pos.borrow()) as isize)), + .offset(((*n.borrow()).with(|__s: &node| __s.pos)) as isize)), )); assert!(((((((*q.borrow()).read()) as i32) == 20) as i32) != 0)); (*q.borrow()).write(187_u8); diff --git a/tests/unit/out/refcount/union_memset_memcpy.rs b/tests/unit/out/refcount/union_memset_memcpy.rs index 03dc4fb401..b239b1dd72 100644 --- a/tests/unit/out/refcount/union_memset_memcpy.rs +++ b/tests/unit/out/refcount/union_memset_memcpy.rs @@ -6,24 +6,18 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg)] +#[derive(Clone, Record, VaArg, FnPtrArg)] pub struct shape_a { - pub code: Value, - pub pad: Value>, -} -impl Clone for shape_a { - fn clone(&self) -> Self { - Self { - code: Rc::new(RefCell::new((*self.code.borrow()).clone())), - pad: Rc::new(RefCell::new((*self.pad.borrow()).clone())), - } - } + #[offset(0)] + pub code: u16, + #[offset(2)] + pub pad: Box<[u8]>, } impl Default for shape_a { fn default() -> Self { shape_a { - code: Rc::new(RefCell::new(0_u16)), - pad: Rc::new(RefCell::new((0..14).map(|_| 0_u8).collect::>())), + code: 0_u16, + pad: (0..14).map(|_| 0_u8).collect::>(), } } } @@ -32,40 +26,34 @@ impl ByteRepr for shape_a { 16 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.code.borrow()).to_bytes(&mut buf[0..2]); - (*self.pad.borrow()).to_bytes(&mut buf[2..16]); + self.code.to_bytes(&mut buf[0..2]); + self.pad.to_bytes(&mut buf[2..16]); } fn from_bytes(buf: &[u8]) -> Self { Self { - code: Rc::new(RefCell::new(::from_bytes(&buf[0..2]))), - pad: Rc::new(RefCell::new(>::from_bytes(&buf[2..16]))), + code: ::from_bytes(&buf[0..2]), + pad: >::from_bytes(&buf[2..16]), } } } -#[derive(VaArg, FnPtrArg)] +#[derive(Clone, Record, VaArg, FnPtrArg)] pub struct shape_b { - pub code: Value, - pub lo: Value, - pub hi: Value, - pub fill: Value>, -} -impl Clone for shape_b { - fn clone(&self) -> Self { - Self { - code: Rc::new(RefCell::new((*self.code.borrow()).clone())), - lo: Rc::new(RefCell::new((*self.lo.borrow()).clone())), - hi: Rc::new(RefCell::new((*self.hi.borrow()).clone())), - fill: Rc::new(RefCell::new((*self.fill.borrow()).clone())), - } - } + #[offset(0)] + pub code: u16, + #[offset(2)] + pub lo: u16, + #[offset(4)] + pub hi: u32, + #[offset(8)] + pub fill: Box<[u8]>, } impl Default for shape_b { fn default() -> Self { shape_b { - code: Rc::new(RefCell::new(0_u16)), - lo: Rc::new(RefCell::new(0_u16)), - hi: Rc::new(RefCell::new(0_u32)), - fill: Rc::new(RefCell::new((0..8).map(|_| 0_u8).collect::>())), + code: 0_u16, + lo: 0_u16, + hi: 0_u32, + fill: (0..8).map(|_| 0_u8).collect::>(), } } } @@ -74,17 +62,17 @@ impl ByteRepr for shape_b { 16 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.code.borrow()).to_bytes(&mut buf[0..2]); - (*self.lo.borrow()).to_bytes(&mut buf[2..4]); - (*self.hi.borrow()).to_bytes(&mut buf[4..8]); - (*self.fill.borrow()).to_bytes(&mut buf[8..16]); + self.code.to_bytes(&mut buf[0..2]); + self.lo.to_bytes(&mut buf[2..4]); + self.hi.to_bytes(&mut buf[4..8]); + self.fill.to_bytes(&mut buf[8..16]); } fn from_bytes(buf: &[u8]) -> Self { Self { - code: Rc::new(RefCell::new(::from_bytes(&buf[0..2]))), - lo: Rc::new(RefCell::new(::from_bytes(&buf[2..4]))), - hi: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), - fill: Rc::new(RefCell::new(>::from_bytes(&buf[8..16]))), + code: ::from_bytes(&buf[0..2]), + lo: ::from_bytes(&buf[2..4]), + hi: ::from_bytes(&buf[4..8]), + fill: >::from_bytes(&buf[8..16]), } } } @@ -129,27 +117,21 @@ impl ByteRepr for anon_0 { } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Container { - pub view: Value, -} -impl Clone for Container { - fn clone(&self) -> Self { - Self { - view: Rc::new(RefCell::new((*self.view.borrow()).clone())), - } - } + #[offset(0)] + pub view: anon_0, } impl ByteRepr for Container { fn byte_size() -> usize { 256 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.view.borrow()).to_bytes(&mut buf[0..256]); + self.view.to_bytes(&mut buf[0..256]); } fn from_bytes(buf: &[u8]) -> Self { Self { - view: Rc::new(RefCell::new(::from_bytes(&buf[0..256]))), + view: ::from_bytes(&buf[0..256]), } } } @@ -165,33 +147,17 @@ fn main_0() -> i32 { .memset((0) as u8, 256usize as usize); ((c.as_pointer()) as Ptr).to_any() }; + assert!((((((*c.borrow()).view.a().with(|__s: &shape_a| __s.code) as i32) == 0) as i32) != 0)); + assert!((((((*c.borrow()).view.b().with(|__s: &shape_b| __s.lo) as i32) == 0) as i32) != 0)); assert!( - (((((*(*(*(*c.borrow()).view.borrow()).a().upgrade().deref()) - .code - .borrow()) as i32) - == 0) as i32) - != 0) - ); - assert!( - (((((*(*(*(*c.borrow()).view.borrow()).b().upgrade().deref()) - .lo - .borrow()) as i32) - == 0) as i32) - != 0) - ); - assert!( - (((((((*(*c.borrow()).view.borrow()) - .raw_() - .reinterpret_cast::() as Ptr::) + (((((((*c.borrow()).view.raw_().reinterpret_cast::() as Ptr::) .offset((0) as isize) .read()) as i32) == 0) as i32) != 0) ); assert!( - (((((((*(*c.borrow()).view.borrow()) - .raw_() - .reinterpret_cast::() as Ptr::) + (((((((*c.borrow()).view.raw_().reinterpret_cast::() as Ptr::) .offset((255) as isize) .read()) as i32) == 0) as i32) @@ -211,33 +177,19 @@ fn main_0() -> i32 { let len: Value = Rc::new(RefCell::new(16_usize)); assert!(((((*len.borrow()) <= ::std::mem::size_of::<[u8; 256]>()) as i32) != 0)); { - (((*(*c.borrow()).view.borrow()) - .raw_() - .reinterpret_cast::()) as Ptr) + (((*c.borrow()).view.raw_().reinterpret_cast::()) as Ptr) .to_any() .memcpy( &((src.as_pointer() as Ptr) as Ptr).to_any(), (*len.borrow()) as usize, ); - (((*(*c.borrow()).view.borrow()) - .raw_() - .reinterpret_cast::()) as Ptr) - .to_any() + (((*c.borrow()).view.raw_().reinterpret_cast::()) as Ptr).to_any() }; + assert!((((((*c.borrow()).view.b().with(|__s: &shape_b| __s.code) as i32) == 2) as i32) != 0)); assert!( - (((((*(*(*(*c.borrow()).view.borrow()).b().upgrade().deref()) - .code - .borrow()) as i32) - == 2) as i32) - != 0) - ); - assert!( - ((((((((*(*(*c.borrow()).view.borrow()).b().upgrade().deref()) - .lo - .as_pointer()) - .reinterpret_cast::()) - .offset((0) as isize) - .read()) as i32) + (((((((field_ptr!((*c.borrow()).view.b(), lo)).reinterpret_cast::()) + .offset((0) as isize) + .read()) as i32) == 80) as i32) != 0) ); @@ -247,13 +199,7 @@ fn main_0() -> i32 { .memset((0) as u8, 256usize as usize); ((c.as_pointer()) as Ptr).to_any() }; - assert!( - (((((*(*(*(*c.borrow()).view.borrow()).b().upgrade().deref()) - .code - .borrow()) as i32) - == 0) as i32) - != 0) - ); + assert!((((((*c.borrow()).view.b().with(|__s: &shape_b| __s.code) as i32) == 0) as i32) != 0)); return 0; } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/union_nested.rs b/tests/unit/out/refcount/union_nested.rs index 163a7a2bc6..5ccd0552bc 100644 --- a/tests/unit/out/refcount/union_nested.rs +++ b/tests/unit/out/refcount/union_nested.rs @@ -6,24 +6,18 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg)] +#[derive(Clone, Record, VaArg, FnPtrArg)] pub struct record { - pub code: Value, - pub pad: Value>, -} -impl Clone for record { - fn clone(&self) -> Self { - Self { - code: Rc::new(RefCell::new((*self.code.borrow()).clone())), - pad: Rc::new(RefCell::new((*self.pad.borrow()).clone())), - } - } + #[offset(0)] + pub code: u16, + #[offset(2)] + pub pad: Box<[u8]>, } impl Default for record { fn default() -> Self { record { - code: Rc::new(RefCell::new(0_u16)), - pad: Rc::new(RefCell::new((0..14).map(|_| 0_u8).collect::>())), + code: 0_u16, + pad: (0..14).map(|_| 0_u8).collect::>(), } } } @@ -32,13 +26,13 @@ impl ByteRepr for record { 16 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.code.borrow()).to_bytes(&mut buf[0..2]); - (*self.pad.borrow()).to_bytes(&mut buf[2..16]); + self.code.to_bytes(&mut buf[0..2]); + self.pad.to_bytes(&mut buf[2..16]); } fn from_bytes(buf: &[u8]) -> Self { Self { - code: Rc::new(RefCell::new(::from_bytes(&buf[0..2]))), - pad: Rc::new(RefCell::new(>::from_bytes(&buf[2..16]))), + code: ::from_bytes(&buf[0..2]), + pad: >::from_bytes(&buf[2..16]), } } } @@ -80,27 +74,21 @@ impl ByteRepr for anon_0 { } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct inner { - pub view: Value, -} -impl Clone for inner { - fn clone(&self) -> Self { - Self { - view: Rc::new(RefCell::new((*self.view.borrow()).clone())), - } - } + #[offset(0)] + pub view: anon_0, } impl ByteRepr for inner { fn byte_size() -> usize { 128 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.view.borrow()).to_bytes(&mut buf[0..128]); + self.view.to_bytes(&mut buf[0..128]); } fn from_bytes(buf: &[u8]) -> Self { Self { - view: Rc::new(RefCell::new(::from_bytes(&buf[0..128]))), + view: ::from_bytes(&buf[0..128]), } } } @@ -142,43 +130,37 @@ impl ByteRepr for anon_1 { } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Outer { - pub kind: Value, - pub level: Value, - pub variant: Value, - pub len: Value, - pub body: Value, -} -impl Clone for Outer { - fn clone(&self) -> Self { - Self { - kind: Rc::new(RefCell::new((*self.kind.borrow()).clone())), - level: Rc::new(RefCell::new((*self.level.borrow()).clone())), - variant: Rc::new(RefCell::new((*self.variant.borrow()).clone())), - len: Rc::new(RefCell::new((*self.len.borrow()).clone())), - body: Rc::new(RefCell::new((*self.body.borrow()).clone())), - } - } + #[offset(0)] + pub kind: i32, + #[offset(4)] + pub level: i32, + #[offset(8)] + pub variant: i32, + #[offset(12)] + pub len: u32, + #[offset(16)] + pub body: anon_1, } impl ByteRepr for Outer { fn byte_size() -> usize { 144 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.kind.borrow()).to_bytes(&mut buf[0..4]); - (*self.level.borrow()).to_bytes(&mut buf[4..8]); - (*self.variant.borrow()).to_bytes(&mut buf[8..12]); - (*self.len.borrow()).to_bytes(&mut buf[12..16]); - (*self.body.borrow()).to_bytes(&mut buf[16..144]); + self.kind.to_bytes(&mut buf[0..4]); + self.level.to_bytes(&mut buf[4..8]); + self.variant.to_bytes(&mut buf[8..12]); + self.len.to_bytes(&mut buf[12..16]); + self.body.to_bytes(&mut buf[16..144]); } fn from_bytes(buf: &[u8]) -> Self { Self { - kind: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - level: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), - variant: Rc::new(RefCell::new(::from_bytes(&buf[8..12]))), - len: Rc::new(RefCell::new(::from_bytes(&buf[12..16]))), - body: Rc::new(RefCell::new(::from_bytes(&buf[16..144]))), + kind: ::from_bytes(&buf[0..4]), + level: ::from_bytes(&buf[4..8]), + variant: ::from_bytes(&buf[8..12]), + len: ::from_bytes(&buf[12..16]), + body: ::from_bytes(&buf[16..144]), } } } @@ -194,39 +176,32 @@ fn main_0() -> i32 { .memset((0) as u8, 144usize as usize); ((ex.as_pointer()) as Ptr).to_any() }; - (*(*ex.borrow()).kind.borrow_mut()) = 2; - (*(*ex.borrow()).level.borrow_mut()) = 1; - (*(*ex.borrow()).variant.borrow_mut()) = 6; - (*(*ex.borrow()).len.borrow_mut()) = (16usize as u32); - (*(*(*(*ex.borrow()).body.borrow()).h().upgrade().deref()) - .code - .borrow_mut()) = 2_u16; - (*(*(*(*ex.borrow()).body.borrow()).h().upgrade().deref()) - .pad - .borrow_mut())[(0) as usize] = (('X' as i32) as u8); - assert!( - (((((*(*(*(*ex.borrow()).body.borrow()).h().upgrade().deref()) - .code - .borrow()) as i32) - == 2) as i32) - != 0) - ); + (*ex.borrow_mut()).kind = 2; + (*ex.borrow_mut()).level = 1; + (*ex.borrow_mut()).variant = 6; + (*ex.borrow_mut()).len = (16usize as u32); + (*ex.borrow()) + .body + .h() + .with_mut(|__s: &mut record| __s.code = 2_u16); + (*ex.borrow()) + .body + .h() + .with_mut(|__s: &mut record| __s.pad[(0) as usize] = (('X' as i32) as u8)); + assert!((((((*ex.borrow()).body.h().with(|__s: &record| __s.code) as i32) == 2) as i32) != 0)); assert!( - (((((*(*(*(*ex.borrow()).body.borrow()).h().upgrade().deref()) - .pad - .borrow())[(0) as usize] as i32) + (((((*ex.borrow()) + .body + .h() + .with(|__s: &record| __s.pad[(0) as usize]) as i32) == ('X' as i32)) as i32) != 0) ); assert!( - (((((*(*(*(*(*(*ex.borrow()).body.borrow()).nested().upgrade().deref()) + (((((*(*ex.borrow()).body.nested().upgrade().deref()) .view - .borrow()) - .h() - .upgrade() - .deref()) - .code - .borrow()) as i32) + .h() + .with(|__s: &record| __s.code) as i32) == 2) as i32) != 0) ); diff --git a/tests/unit/out/refcount/union_pointer_pun_address.rs b/tests/unit/out/refcount/union_pointer_pun_address.rs index 9bf72f73c6..1cc26aef18 100644 --- a/tests/unit/out/refcount/union_pointer_pun_address.rs +++ b/tests/unit/out/refcount/union_pointer_pun_address.rs @@ -6,55 +6,43 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct node_a { - pub n: Value, -} -impl Clone for node_a { - fn clone(&self) -> Self { - Self { - n: Rc::new(RefCell::new((*self.n.borrow()).clone())), - } - } + #[offset(0)] + pub n: i32, } impl ByteRepr for node_a { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.n.borrow()).to_bytes(&mut buf[0..4]); + self.n.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - n: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + n: ::from_bytes(&buf[0..4]), } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct node_b { - pub data: Value, - pub next: Value>, -} -impl Clone for node_b { - fn clone(&self) -> Self { - Self { - data: Rc::new(RefCell::new((*self.data.borrow()).clone())), - next: Rc::new(RefCell::new((*self.next.borrow()).clone())), - } - } + #[offset(0)] + pub data: AnyPtr, + #[offset(8)] + pub next: Ptr, } impl ByteRepr for node_b { fn byte_size() -> usize { 16 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.data.borrow()).to_bytes(&mut buf[0..8]); - (*self.next.borrow()).to_bytes(&mut buf[8..16]); + self.data.to_bytes(&mut buf[0..8]); + self.next.to_bytes(&mut buf[8..16]); } fn from_bytes(buf: &[u8]) -> Self { Self { - data: Rc::new(RefCell::new(::from_bytes(&buf[0..8]))), - next: Rc::new(RefCell::new(>::from_bytes(&buf[8..16]))), + data: ::from_bytes(&buf[0..8]), + next: >::from_bytes(&buf[8..16]), } } } @@ -63,9 +51,7 @@ pub fn main() { std::process::exit(main_0()); } fn main_0() -> i32 { - let a: Value = Rc::new(RefCell::new(node_a { - n: Rc::new(RefCell::new(123)), - })); + let a: Value = Rc::new(RefCell::new(node_a { n: 123 })); let ptr: Value = >::default(); (*ptr.borrow_mut()).to_a().write((a.as_pointer())); let out: Value> = Rc::new(RefCell::new(((*ptr.borrow()).to_b().read()).clone())); diff --git a/tests/unit/out/refcount/union_struct_dual_use.rs b/tests/unit/out/refcount/union_struct_dual_use.rs index c90bf3ad88..3df37e012f 100644 --- a/tests/unit/out/refcount/union_struct_dual_use.rs +++ b/tests/unit/out/refcount/union_struct_dual_use.rs @@ -6,39 +6,33 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Inner { - pub a: Value, - pub b: Value, -} -impl Clone for Inner { - fn clone(&self) -> Self { - Self { - a: Rc::new(RefCell::new((*self.a.borrow()).clone())), - b: Rc::new(RefCell::new((*self.b.borrow()).clone())), - } - } + #[offset(0)] + pub a: i32, + #[offset(4)] + pub b: i32, } impl ByteRepr for Inner { fn byte_size() -> usize { 8 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.a.borrow()).to_bytes(&mut buf[0..4]); - (*self.b.borrow()).to_bytes(&mut buf[4..8]); + self.a.to_bytes(&mut buf[0..4]); + self.b.to_bytes(&mut buf[4..8]); } fn from_bytes(buf: &[u8]) -> Self { Self { - a: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - b: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + a: ::from_bytes(&buf[0..4]), + b: ::from_bytes(&buf[4..8]), } } } pub fn sum_inner_0(i: Ptr) -> i32 { let i: Value> = Rc::new(RefCell::new(i)); return { - let _lhs = (*(*(*i.borrow()).upgrade().deref()).a.borrow()); - _lhs + (*(*(*i.borrow()).upgrade().deref()).b.borrow()) + let _lhs = (*i.borrow()).with(|__s: &Inner| __s.a); + _lhs + (*i.borrow()).with(|__s: &Inner| __s.b) }; } pub struct anon_1 { @@ -79,27 +73,21 @@ impl ByteRepr for anon_1 { } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Outer { - pub u: Value, -} -impl Clone for Outer { - fn clone(&self) -> Self { - Self { - u: Rc::new(RefCell::new((*self.u.borrow()).clone())), - } - } + #[offset(0)] + pub u: anon_1, } impl ByteRepr for Outer { fn byte_size() -> usize { 16 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.u.borrow()).to_bytes(&mut buf[0..16]); + self.u.to_bytes(&mut buf[0..16]); } fn from_bytes(buf: &[u8]) -> Self { Self { - u: Rc::new(RefCell::new(::from_bytes(&buf[0..16]))), + u: ::from_bytes(&buf[0..16]), } } } @@ -109,8 +97,8 @@ pub fn main() { } fn main_0() -> i32 { let standalone: Value = >::default(); - (*(*standalone.borrow()).a.borrow_mut()) = 3; - (*(*standalone.borrow()).b.borrow_mut()) = 4; + (*standalone.borrow_mut()).a = 3; + (*standalone.borrow_mut()).b = 4; assert!((((({ sum_inner_0((standalone.as_pointer()),) }) == 7) as i32) != 0)); let outer: Value = >::default(); { @@ -119,29 +107,24 @@ fn main_0() -> i32 { .memset((0) as u8, 16usize as usize); ((outer.as_pointer()) as Ptr).to_any() }; - (*(*(*(*outer.borrow()).u.borrow()).inner().upgrade().deref()) - .a - .borrow_mut()) = 3; - (*(*(*(*outer.borrow()).u.borrow()).inner().upgrade().deref()) - .b - .borrow_mut()) = 4; - assert!( - (((({ sum_inner_0(((*(*outer.borrow()).u.borrow()).inner()).clone(),) }) == 7) as i32) - != 0) - ); + (*outer.borrow()) + .u + .inner() + .with_mut(|__s: &mut Inner| __s.a = 3); + (*outer.borrow()) + .u + .inner() + .with_mut(|__s: &mut Inner| __s.b = 4); + assert!((((({ sum_inner_0(((*outer.borrow()).u.inner()).clone(),) }) == 7) as i32) != 0)); assert!( - ((((((((*(*outer.borrow()).u.borrow()) - .raw_() - .reinterpret_cast::() as Ptr::) + ((((((((*outer.borrow()).u.raw_().reinterpret_cast::() as Ptr::) .offset((0) as isize) .read()) as u8) as i32) == 3) as i32) != 0) ); assert!( - ((((((((*(*outer.borrow()).u.borrow()) - .raw_() - .reinterpret_cast::() as Ptr::) + ((((((((*outer.borrow()).u.raw_().reinterpret_cast::() as Ptr::) .offset((4) as isize) .read()) as u8) as i32) == 4) as i32) diff --git a/tests/unit/out/refcount/union_tagged_many_arms.rs b/tests/unit/out/refcount/union_tagged_many_arms.rs index 5f33d4956c..4af70390d4 100644 --- a/tests/unit/out/refcount/union_tagged_many_arms.rs +++ b/tests/unit/out/refcount/union_tagged_many_arms.rs @@ -59,31 +59,25 @@ impl ByteRepr for anon_0 { } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Slot { - pub tag: Value, - pub payload: Value, -} -impl Clone for Slot { - fn clone(&self) -> Self { - Self { - tag: Rc::new(RefCell::new((*self.tag.borrow()).clone())), - payload: Rc::new(RefCell::new((*self.payload.borrow()).clone())), - } - } + #[offset(0)] + pub tag: Tag_enum, + #[offset(8)] + pub payload: anon_0, } impl ByteRepr for Slot { fn byte_size() -> usize { 16 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.tag.borrow()).to_bytes(&mut buf[0..4]); - (*self.payload.borrow()).to_bytes(&mut buf[8..16]); + self.tag.to_bytes(&mut buf[0..4]); + self.payload.to_bytes(&mut buf[8..16]); } fn from_bytes(buf: &[u8]) -> Self { Self { - tag: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - payload: Rc::new(RefCell::new(::from_bytes(&buf[8..16]))), + tag: ::from_bytes(&buf[0..4]), + payload: ::from_bytes(&buf[8..16]), } } } @@ -93,46 +87,34 @@ pub fn main() { } fn main_0() -> i32 { let a: Value = >::default(); - (*(*a.borrow()).tag.borrow_mut()) = Tag_enum_T_NUM_S; - (*(*a.borrow()).payload.borrow_mut()) - .signed_n() - .write((-7_i32 as i64)); - assert!( - (((((*(*a.borrow()).payload.borrow()).signed_n().read()) == (-7_i32 as i64)) as i32) != 0) - ); + (*a.borrow_mut()).tag = Tag_enum_T_NUM_S; + { (*a.borrow_mut()).payload.signed_n() }.write((-7_i32 as i64)); + assert!((((((*a.borrow()).payload.signed_n().read()) == (-7_i32 as i64)) as i32) != 0)); let b: Value = >::default(); - (*(*b.borrow()).tag.borrow_mut()) = Tag_enum_T_NUM_U; - (*(*b.borrow()).payload.borrow_mut()) - .unsigned_n() - .write(3735928559_u64); - assert!( - (((((*(*b.borrow()).payload.borrow()).unsigned_n().read()) == 3735928559_u64) as i32) != 0) - ); + (*b.borrow_mut()).tag = Tag_enum_T_NUM_U; + { (*b.borrow_mut()).payload.unsigned_n() }.write(3735928559_u64); + assert!((((((*b.borrow()).payload.unsigned_n().read()) == 3735928559_u64) as i32) != 0)); let c: Value = >::default(); - (*(*c.borrow()).tag.borrow_mut()) = Tag_enum_T_TEXT; - (*(*c.borrow()).payload.borrow_mut()) - .text() - .write(Ptr::::from_string_literal(b"hello")); + (*c.borrow_mut()).tag = Tag_enum_T_TEXT; + { (*c.borrow_mut()).payload.text() }.write(Ptr::::from_string_literal(b"hello")); assert!( - (((((((*(*c.borrow()).payload.borrow()).text().read()) + (((((((*c.borrow()).payload.text().read()) .offset((0) as isize) .read()) as i32) == ('h' as i32)) as i32) != 0) ); let d: Value = >::default(); - (*(*d.borrow()).tag.borrow_mut()) = Tag_enum_T_FLOAT; - (*(*d.borrow()).payload.borrow_mut()).f().write(1.5E+0); - assert!((((((*(*d.borrow()).payload.borrow()).f().read()) == 1.5E+0) as i32) != 0)); + (*d.borrow_mut()).tag = Tag_enum_T_FLOAT; + { (*d.borrow_mut()).payload.f() }.write(1.5E+0); + assert!((((((*d.borrow()).payload.f().read()) == 1.5E+0) as i32) != 0)); let x: Value = Rc::new(RefCell::new(0)); let e: Value = >::default(); - (*(*e.borrow()).tag.borrow_mut()) = Tag_enum_T_REF; - (*(*e.borrow()).payload.borrow_mut()) - .handle() - .write(((x.as_pointer()) as Ptr).to_any()); + (*e.borrow_mut()).tag = Tag_enum_T_REF; + { (*e.borrow_mut()).payload.handle() }.write(((x.as_pointer()) as Ptr).to_any()); assert!( ((({ - let _lhs = ((*(*e.borrow()).payload.borrow()).handle().read()).clone(); + let _lhs = ((*e.borrow()).payload.handle().read()).clone(); _lhs == ((x.as_pointer()) as Ptr).to_any() }) as i32) != 0) diff --git a/tests/unit/out/refcount/union_tagged_simple.rs b/tests/unit/out/refcount/union_tagged_simple.rs index e24df792e6..f3e0f091de 100644 --- a/tests/unit/out/refcount/union_tagged_simple.rs +++ b/tests/unit/out/refcount/union_tagged_simple.rs @@ -47,35 +47,29 @@ impl ByteRepr for anon_0 { } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Event { - pub kind: Value, - pub handle: Value, - pub payload: Value, -} -impl Clone for Event { - fn clone(&self) -> Self { - Self { - kind: Rc::new(RefCell::new((*self.kind.borrow()).clone())), - handle: Rc::new(RefCell::new((*self.handle.borrow()).clone())), - payload: Rc::new(RefCell::new((*self.payload.borrow()).clone())), - } - } + #[offset(0)] + pub kind: Kind_enum, + #[offset(8)] + pub handle: AnyPtr, + #[offset(16)] + pub payload: anon_0, } impl ByteRepr for Event { fn byte_size() -> usize { 24 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.kind.borrow()).to_bytes(&mut buf[0..4]); - (*self.handle.borrow()).to_bytes(&mut buf[8..16]); - (*self.payload.borrow()).to_bytes(&mut buf[16..24]); + self.kind.to_bytes(&mut buf[0..4]); + self.handle.to_bytes(&mut buf[8..16]); + self.payload.to_bytes(&mut buf[16..24]); } fn from_bytes(buf: &[u8]) -> Self { Self { - kind: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - handle: Rc::new(RefCell::new(::from_bytes(&buf[8..16]))), - payload: Rc::new(RefCell::new(::from_bytes(&buf[16..24]))), + kind: ::from_bytes(&buf[0..4]), + handle: ::from_bytes(&buf[8..16]), + payload: ::from_bytes(&buf[16..24]), } } } @@ -86,24 +80,20 @@ pub fn main() { fn main_0() -> i32 { let dummy: Value = Rc::new(RefCell::new(0)); let m1: Value = >::default(); - (*(*m1.borrow()).kind.borrow_mut()) = Kind_enum_KIND_DONE; - (*(*m1.borrow()).handle.borrow_mut()) = ((dummy.as_pointer()) as Ptr).to_any(); - (*(*m1.borrow()).payload.borrow_mut()).code().write(42); + (*m1.borrow_mut()).kind = Kind_enum_KIND_DONE; + (*m1.borrow_mut()).handle = ((dummy.as_pointer()) as Ptr).to_any(); + { (*m1.borrow_mut()).payload.code() }.write(42); assert!( - (((((*(*m1.borrow()).kind.borrow()) as u32) == ((Kind_enum_KIND_DONE as i32) as u32)) - as i32) - != 0) + (((({ (*m1.borrow()).kind } as u32) == ((Kind_enum_KIND_DONE as i32) as u32)) as i32) != 0) ); - assert!((((((*(*m1.borrow()).payload.borrow()).code().read()) == 42) as i32) != 0)); + assert!((((((*m1.borrow()).payload.code().read()) == 42) as i32) != 0)); let m2: Value = >::default(); - (*(*m2.borrow()).kind.borrow_mut()) = Kind_enum_KIND_NONE; - (*(*m2.borrow()).handle.borrow_mut()) = ((dummy.as_pointer()) as Ptr).to_any(); - (*(*m2.borrow()).payload.borrow_mut()) - .obj() - .write(((dummy.as_pointer()) as Ptr).to_any()); + (*m2.borrow_mut()).kind = Kind_enum_KIND_NONE; + (*m2.borrow_mut()).handle = ((dummy.as_pointer()) as Ptr).to_any(); + { (*m2.borrow_mut()).payload.obj() }.write(((dummy.as_pointer()) as Ptr).to_any()); assert!( ((({ - let _lhs = ((*(*m2.borrow()).payload.borrow()).obj().read()).clone(); + let _lhs = ((*m2.borrow()).payload.obj().read()).clone(); _lhs == ((dummy.as_pointer()) as Ptr).to_any() }) as i32) != 0) diff --git a/tests/unit/out/refcount/union_tagged_struct_arms.rs b/tests/unit/out/refcount/union_tagged_struct_arms.rs index 9514b8922c..85761043c8 100644 --- a/tests/unit/out/refcount/union_tagged_struct_arms.rs +++ b/tests/unit/out/refcount/union_tagged_struct_arms.rs @@ -10,111 +10,93 @@ pub type Choice_enum = u32; pub const Choice_enum_C_LIST: Choice_enum = 1; pub const Choice_enum_C_LETTERS: Choice_enum = 2; pub const Choice_enum_C_INTEGERS: Choice_enum = 3; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct anon_1 { - pub items: Value>>, - pub count: Value, - pub cursor: Value, -} -impl Clone for anon_1 { - fn clone(&self) -> Self { - Self { - items: Rc::new(RefCell::new((*self.items.borrow()).clone())), - count: Rc::new(RefCell::new((*self.count.borrow()).clone())), - cursor: Rc::new(RefCell::new((*self.cursor.borrow()).clone())), - } - } + #[offset(0)] + pub items: Ptr>, + #[offset(8)] + pub count: i64, + #[offset(16)] + pub cursor: i64, } impl ByteRepr for anon_1 { fn byte_size() -> usize { 24 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.items.borrow()).to_bytes(&mut buf[0..8]); - (*self.count.borrow()).to_bytes(&mut buf[8..16]); - (*self.cursor.borrow()).to_bytes(&mut buf[16..24]); + self.items.to_bytes(&mut buf[0..8]); + self.count.to_bytes(&mut buf[8..16]); + self.cursor.to_bytes(&mut buf[16..24]); } fn from_bytes(buf: &[u8]) -> Self { Self { - items: Rc::new(RefCell::new(>>::from_bytes(&buf[0..8]))), - count: Rc::new(RefCell::new(::from_bytes(&buf[8..16]))), - cursor: Rc::new(RefCell::new(::from_bytes(&buf[16..24]))), + items: >>::from_bytes(&buf[0..8]), + count: ::from_bytes(&buf[8..16]), + cursor: ::from_bytes(&buf[16..24]), } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct anon_2 { - pub lo: Value, - pub hi: Value, - pub curr: Value, - pub step: Value, -} -impl Clone for anon_2 { - fn clone(&self) -> Self { - Self { - lo: Rc::new(RefCell::new((*self.lo.borrow()).clone())), - hi: Rc::new(RefCell::new((*self.hi.borrow()).clone())), - curr: Rc::new(RefCell::new((*self.curr.borrow()).clone())), - step: Rc::new(RefCell::new((*self.step.borrow()).clone())), - } - } + #[offset(0)] + pub lo: i32, + #[offset(4)] + pub hi: i32, + #[offset(8)] + pub curr: i32, + #[offset(12)] + pub step: u8, } impl ByteRepr for anon_2 { fn byte_size() -> usize { 16 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.lo.borrow()).to_bytes(&mut buf[0..4]); - (*self.hi.borrow()).to_bytes(&mut buf[4..8]); - (*self.curr.borrow()).to_bytes(&mut buf[8..12]); - (*self.step.borrow()).to_bytes(&mut buf[12..13]); + self.lo.to_bytes(&mut buf[0..4]); + self.hi.to_bytes(&mut buf[4..8]); + self.curr.to_bytes(&mut buf[8..12]); + self.step.to_bytes(&mut buf[12..13]); } fn from_bytes(buf: &[u8]) -> Self { Self { - lo: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - hi: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), - curr: Rc::new(RefCell::new(::from_bytes(&buf[8..12]))), - step: Rc::new(RefCell::new(::from_bytes(&buf[12..13]))), + lo: ::from_bytes(&buf[0..4]), + hi: ::from_bytes(&buf[4..8]), + curr: ::from_bytes(&buf[8..12]), + step: ::from_bytes(&buf[12..13]), } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct anon_3 { - pub lo: Value, - pub hi: Value, - pub curr: Value, - pub step: Value, - pub width: Value, -} -impl Clone for anon_3 { - fn clone(&self) -> Self { - Self { - lo: Rc::new(RefCell::new((*self.lo.borrow()).clone())), - hi: Rc::new(RefCell::new((*self.hi.borrow()).clone())), - curr: Rc::new(RefCell::new((*self.curr.borrow()).clone())), - step: Rc::new(RefCell::new((*self.step.borrow()).clone())), - width: Rc::new(RefCell::new((*self.width.borrow()).clone())), - } - } + #[offset(0)] + pub lo: i64, + #[offset(8)] + pub hi: i64, + #[offset(16)] + pub curr: i64, + #[offset(24)] + pub step: i64, + #[offset(32)] + pub width: i32, } impl ByteRepr for anon_3 { fn byte_size() -> usize { 40 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.lo.borrow()).to_bytes(&mut buf[0..8]); - (*self.hi.borrow()).to_bytes(&mut buf[8..16]); - (*self.curr.borrow()).to_bytes(&mut buf[16..24]); - (*self.step.borrow()).to_bytes(&mut buf[24..32]); - (*self.width.borrow()).to_bytes(&mut buf[32..36]); + self.lo.to_bytes(&mut buf[0..8]); + self.hi.to_bytes(&mut buf[8..16]); + self.curr.to_bytes(&mut buf[16..24]); + self.step.to_bytes(&mut buf[24..32]); + self.width.to_bytes(&mut buf[32..36]); } fn from_bytes(buf: &[u8]) -> Self { Self { - lo: Rc::new(RefCell::new(::from_bytes(&buf[0..8]))), - hi: Rc::new(RefCell::new(::from_bytes(&buf[8..16]))), - curr: Rc::new(RefCell::new(::from_bytes(&buf[16..24]))), - step: Rc::new(RefCell::new(::from_bytes(&buf[24..32]))), - width: Rc::new(RefCell::new(::from_bytes(&buf[32..36]))), + lo: ::from_bytes(&buf[0..8]), + hi: ::from_bytes(&buf[8..16]), + curr: ::from_bytes(&buf[16..24]), + step: ::from_bytes(&buf[24..32]), + width: ::from_bytes(&buf[32..36]), } } } @@ -159,35 +141,29 @@ impl ByteRepr for anon_0 { } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Branch { - pub choice: Value, - pub index: Value, - pub v: Value, -} -impl Clone for Branch { - fn clone(&self) -> Self { - Self { - choice: Rc::new(RefCell::new((*self.choice.borrow()).clone())), - index: Rc::new(RefCell::new((*self.index.borrow()).clone())), - v: Rc::new(RefCell::new((*self.v.borrow()).clone())), - } - } + #[offset(0)] + pub choice: Choice_enum, + #[offset(4)] + pub index: i32, + #[offset(8)] + pub v: anon_0, } impl ByteRepr for Branch { fn byte_size() -> usize { 48 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.choice.borrow()).to_bytes(&mut buf[0..4]); - (*self.index.borrow()).to_bytes(&mut buf[4..8]); - (*self.v.borrow()).to_bytes(&mut buf[8..48]); + self.choice.to_bytes(&mut buf[0..4]); + self.index.to_bytes(&mut buf[4..8]); + self.v.to_bytes(&mut buf[8..48]); } fn from_bytes(buf: &[u8]) -> Self { Self { - choice: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - index: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), - v: Rc::new(RefCell::new(::from_bytes(&buf[8..48]))), + choice: ::from_bytes(&buf[0..4]), + index: ::from_bytes(&buf[4..8]), + v: ::from_bytes(&buf[8..48]), } } } @@ -204,128 +180,99 @@ fn main_0() -> i32 { ]))); ); let p_list: Value = >::default(); - (*(*p_list.borrow()).choice.borrow_mut()) = Choice_enum_C_LIST; - (*(*p_list.borrow()).index.borrow_mut()) = 0; - (*(*(*(*p_list.borrow()).v.borrow()).list().upgrade().deref()) - .items - .borrow_mut()) = (items_4.with(|v| v.as_pointer()) as Ptr>); - (*(*(*(*p_list.borrow()).v.borrow()).list().upgrade().deref()) - .count - .borrow_mut()) = 3_i64; - (*(*(*(*p_list.borrow()).v.borrow()).list().upgrade().deref()) - .cursor - .borrow_mut()) = 1_i64; - assert!( - ((((*(*(*(*p_list.borrow()).v.borrow()).list().upgrade().deref()) - .count - .borrow()) - == 3_i64) as i32) - != 0) - ); + (*p_list.borrow_mut()).choice = Choice_enum_C_LIST; + (*p_list.borrow_mut()).index = 0; + (*p_list.borrow()).v.list().with_mut(|__s: &mut anon_1| { + __s.items = (items_4.with(|v| v.as_pointer()) as Ptr>) + }); + (*p_list.borrow()) + .v + .list() + .with_mut(|__s: &mut anon_1| __s.count = 3_i64); + (*p_list.borrow()) + .v + .list() + .with_mut(|__s: &mut anon_1| __s.cursor = 1_i64); + assert!(((((*p_list.borrow()).v.list().with(|__s: &anon_1| __s.count) == 3_i64) as i32) != 0)); assert!( - (((((((*(*(*(*p_list.borrow()).v.borrow()).list().upgrade().deref()) - .items - .borrow()) - .offset((1) as isize) - .read()) + (((((((*p_list.borrow()) + .v + .list() + .with(|__s: &anon_1| (__s.items).clone()) + .offset((1) as isize) + .read()) .offset((0) as isize) .read()) as i32) == ('b' as i32)) as i32) != 0) ); let p_letters: Value = >::default(); - (*(*p_letters.borrow()).choice.borrow_mut()) = Choice_enum_C_LETTERS; - (*(*p_letters.borrow()).index.borrow_mut()) = 1; - (*(*(*(*p_letters.borrow()).v.borrow()) + (*p_letters.borrow_mut()).choice = Choice_enum_C_LETTERS; + (*p_letters.borrow_mut()).index = 1; + (*p_letters.borrow()) + .v .letters() - .upgrade() - .deref()) - .lo - .borrow_mut()) = ('a' as i32); - (*(*(*(*p_letters.borrow()).v.borrow()) + .with_mut(|__s: &mut anon_2| __s.lo = ('a' as i32)); + (*p_letters.borrow()) + .v .letters() - .upgrade() - .deref()) - .hi - .borrow_mut()) = ('z' as i32); - (*(*(*(*p_letters.borrow()).v.borrow()) + .with_mut(|__s: &mut anon_2| __s.hi = ('z' as i32)); + (*p_letters.borrow()) + .v .letters() - .upgrade() - .deref()) - .curr - .borrow_mut()) = ('m' as i32); - (*(*(*(*p_letters.borrow()).v.borrow()) + .with_mut(|__s: &mut anon_2| __s.curr = ('m' as i32)); + (*p_letters.borrow()) + .v .letters() - .upgrade() - .deref()) - .step - .borrow_mut()) = 1_u8; + .with_mut(|__s: &mut anon_2| __s.step = 1_u8); assert!( - (((((*(*(*(*p_letters.borrow()).v.borrow()) + (((((*p_letters.borrow()) + .v .letters() - .upgrade() - .deref()) - .hi - .borrow()) - - (*(*(*(*p_letters.borrow()).v.borrow()) + .with(|__s: &anon_2| __s.hi) + - (*p_letters.borrow()) + .v .letters() - .upgrade() - .deref()) - .lo - .borrow())) + .with(|__s: &anon_2| __s.lo)) == 25) as i32) != 0) ); let p_integers: Value = >::default(); - (*(*p_integers.borrow()).choice.borrow_mut()) = Choice_enum_C_INTEGERS; - (*(*p_integers.borrow()).index.borrow_mut()) = 2; - (*(*(*(*p_integers.borrow()).v.borrow()) + (*p_integers.borrow_mut()).choice = Choice_enum_C_INTEGERS; + (*p_integers.borrow_mut()).index = 2; + (*p_integers.borrow()) + .v .integers() - .upgrade() - .deref()) - .lo - .borrow_mut()) = 1_i64; - (*(*(*(*p_integers.borrow()).v.borrow()) + .with_mut(|__s: &mut anon_3| __s.lo = 1_i64); + (*p_integers.borrow()) + .v .integers() - .upgrade() - .deref()) - .hi - .borrow_mut()) = 100_i64; - (*(*(*(*p_integers.borrow()).v.borrow()) + .with_mut(|__s: &mut anon_3| __s.hi = 100_i64); + (*p_integers.borrow()) + .v .integers() - .upgrade() - .deref()) - .curr - .borrow_mut()) = 1_i64; - (*(*(*(*p_integers.borrow()).v.borrow()) + .with_mut(|__s: &mut anon_3| __s.curr = 1_i64); + (*p_integers.borrow()) + .v .integers() - .upgrade() - .deref()) - .step - .borrow_mut()) = 1_i64; - (*(*(*(*p_integers.borrow()).v.borrow()) + .with_mut(|__s: &mut anon_3| __s.step = 1_i64); + (*p_integers.borrow()) + .v .integers() - .upgrade() - .deref()) - .width - .borrow_mut()) = 3; + .with_mut(|__s: &mut anon_3| __s.width = 3); assert!( - ((((*(*(*(*p_integers.borrow()).v.borrow()) + ((((*p_integers.borrow()) + .v .integers() - .upgrade() - .deref()) - .hi - .borrow()) + .with(|__s: &anon_3| __s.hi) == 100_i64) as i32) != 0) ); assert!( - ((((*(*(*(*p_integers.borrow()).v.borrow()) + ((((*p_integers.borrow()) + .v .integers() - .upgrade() - .deref()) - .width - .borrow()) + .with(|__s: &anon_3| __s.width) == 3) as i32) != 0) ); diff --git a/tests/unit/out/refcount/union_void_ptr_sized_deref.rs b/tests/unit/out/refcount/union_void_ptr_sized_deref.rs index b67290394a..c102286cd0 100644 --- a/tests/unit/out/refcount/union_void_ptr_sized_deref.rs +++ b/tests/unit/out/refcount/union_void_ptr_sized_deref.rs @@ -54,31 +54,25 @@ impl ByteRepr for anon_0 { } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Sink { - pub width: Value, - pub out: Value, -} -impl Clone for Sink { - fn clone(&self) -> Self { - Self { - width: Rc::new(RefCell::new((*self.width.borrow()).clone())), - out: Rc::new(RefCell::new((*self.out.borrow()).clone())), - } - } + #[offset(0)] + pub width: Width_enum, + #[offset(8)] + pub out: anon_0, } impl ByteRepr for Sink { fn byte_size() -> usize { 16 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.width.borrow()).to_bytes(&mut buf[0..4]); - (*self.out.borrow()).to_bytes(&mut buf[8..16]); + self.width.to_bytes(&mut buf[0..4]); + self.out.to_bytes(&mut buf[8..16]); } fn from_bytes(buf: &[u8]) -> Self { Self { - width: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - out: Rc::new(RefCell::new(::from_bytes(&buf[8..16]))), + width: ::from_bytes(&buf[0..4]), + out: ::from_bytes(&buf[8..16]), } } } @@ -86,29 +80,29 @@ pub fn write_count_1(s: Ptr, count: i64) { let s: Value> = Rc::new(RefCell::new(s)); let count: Value = Rc::new(RefCell::new(count)); 'switch: { - let __match_cond = ((*(*(*s.borrow()).upgrade().deref()).width.borrow()) as u32); + let __match_cond = ((*s.borrow()).with(|__s: &Sink| __s.width) as u32); match __match_cond { __v if __v == ((Width_enum_W_64 as i32) as u32) => { - ((*(*(*s.borrow()).upgrade().deref()).out.borrow()) - .handle() - .read()) - .reinterpret_cast::() + { + ((*(*s.borrow()).upgrade().deref()).out.handle().read()) + .reinterpret_cast::() + } .write((*count.borrow())); break 'switch; } __v if __v == ((Width_enum_W_32 as i32) as u32) => { - ((*(*(*s.borrow()).upgrade().deref()).out.borrow()) - .handle() - .read()) - .reinterpret_cast::() + { + ((*(*s.borrow()).upgrade().deref()).out.handle().read()) + .reinterpret_cast::() + } .write(((*count.borrow()) as i32)); break 'switch; } __v if __v == ((Width_enum_W_16 as i32) as u32) => { - ((*(*(*s.borrow()).upgrade().deref()).out.borrow()) - .handle() - .read()) - .reinterpret_cast::() + { + ((*(*s.borrow()).upgrade().deref()).out.handle().read()) + .reinterpret_cast::() + } .write(((*count.borrow()) as i16)); break 'switch; } @@ -125,22 +119,16 @@ fn main_0() -> i32 { let buf32: Value = Rc::new(RefCell::new(0)); let buf16: Value = Rc::new(RefCell::new(0_i16)); let s: Value = >::default(); - (*(*s.borrow()).width.borrow_mut()) = Width_enum_W_64; - (*(*s.borrow()).out.borrow_mut()) - .handle() - .write(((buf64.as_pointer()) as Ptr).to_any()); + (*s.borrow_mut()).width = Width_enum_W_64; + { (*s.borrow_mut()).out.handle() }.write(((buf64.as_pointer()) as Ptr).to_any()); ({ write_count_1((s.as_pointer()), 1234605616436508552_i64) }); assert!(((((*buf64.borrow()) == 1234605616436508552_i64) as i32) != 0)); - (*(*s.borrow()).width.borrow_mut()) = Width_enum_W_32; - (*(*s.borrow()).out.borrow_mut()) - .handle() - .write(((buf32.as_pointer()) as Ptr).to_any()); + (*s.borrow_mut()).width = Width_enum_W_32; + { (*s.borrow_mut()).out.handle() }.write(((buf32.as_pointer()) as Ptr).to_any()); ({ write_count_1((s.as_pointer()), 305419896_i64) }); assert!(((((*buf32.borrow()) == 305419896) as i32) != 0)); - (*(*s.borrow()).width.borrow_mut()) = Width_enum_W_16; - (*(*s.borrow()).out.borrow_mut()) - .handle() - .write(((buf16.as_pointer()) as Ptr).to_any()); + (*s.borrow_mut()).width = Width_enum_W_16; + { (*s.borrow_mut()).out.handle() }.write(((buf16.as_pointer()) as Ptr).to_any()); ({ write_count_1((s.as_pointer()), 4660_i64) }); assert!((((((*buf16.borrow()) as i32) == 4660) as i32) != 0)); return 0; diff --git a/tests/unit/out/refcount/unique_ptr.rs b/tests/unit/out/refcount/unique_ptr.rs index 34e348324e..45b4561bc7 100644 --- a/tests/unit/out/refcount/unique_ptr.rs +++ b/tests/unit/out/refcount/unique_ptr.rs @@ -6,62 +6,69 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Default)] +#[derive(Record, Default)] pub struct SafePointer { - pub ptr: Value>>, + #[offset(0)] + pub ptr: Option>, } impl SafePointer { pub fn move_from(_a0: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - ptr: Rc::new(RefCell::new( - (*(*_a0.upgrade().deref()).ptr.borrow_mut()).take(), - )), + ptr: { _a0.with_mut(|__s: &mut SafePointer| __s.ptr.take()) }, })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } } +<<<<<<< HEAD impl ByteRepr for SafePointer {} #[derive(VaArg, FnPtrArg, Default)] -pub struct Pair { - pub x: Value, - pub y: Value, -} -impl Clone for Pair { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - x: Rc::new(RefCell::new((*self.x.borrow()))), - y: Rc::new(RefCell::new((*self.y.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() +======= +impl ByteRepr for SafePointer { + fn byte_size() -> usize { + 8 + } + fn to_bytes(&self, buf: &mut [u8]) { + self.ptr.to_bytes(&mut buf[0..8]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + ptr: >>::from_bytes(&buf[0..8]), + } } } +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] +>>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) +pub struct Pair { + #[offset(0)] + pub x: i32, + #[offset(4)] + pub y: i32, +} impl ByteRepr for Pair { fn byte_size() -> usize { 8 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.x.borrow()).to_bytes(&mut buf[0..4]); - (*self.y.borrow()).to_bytes(&mut buf[4..8]); + self.x.to_bytes(&mut buf[0..4]); + self.y.to_bytes(&mut buf[4..8]); } fn from_bytes(buf: &[u8]) -> Self { Self { - x: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - y: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + x: ::from_bytes(&buf[0..4]), + y: ::from_bytes(&buf[4..8]), } } } pub fn DoStuffWithSafePointer_0(safe_ptr: Ptr>>) { let x1: Value>> = Rc::new(RefCell::new(Some(Rc::new(RefCell::new(0))))); let x2: Value>> = Rc::new(RefCell::new(Some(Rc::new(RefCell::new(0))))); - (*(*x2.borrow_mut()).as_ref().unwrap().borrow_mut()) = 1; + (*(*x2.borrow()).as_ref().unwrap().borrow_mut()) = 1; (x1.as_pointer() as Ptr>>).write((*x2.borrow_mut()).take()); let raw_ptr1: Value> = Rc::new(RefCell::new(((*x1.borrow()).as_pointer()))); (*raw_ptr1.borrow()).with_mut(|__v| __v.prefix_inc()); - ((*(*safe_ptr.upgrade().deref()).as_ref().unwrap().borrow()) - .ptr - .as_pointer() as Ptr>>) + ((field_ptr!(((*safe_ptr.upgrade().deref()).as_pointer()), ptr) as Ptr>>) + as Ptr>>) .write((*x1.borrow_mut()).take()); ({ SafePointerImpl::inc(&((*safe_ptr.upgrade().deref()).as_pointer())) }); ({ SafePointerImpl::inc(&((*safe_ptr.upgrade().deref()).as_pointer())) }); @@ -69,7 +76,7 @@ pub fn DoStuffWithSafePointer_0(safe_ptr: Ptr>>) { let x4: Value>> = Rc::new(RefCell::new(Some(Rc::new(RefCell::new(20))))); let __rhs = ((*(*x3.borrow()).as_ref().unwrap().borrow()) + (*(*x4.borrow()).as_ref().unwrap().borrow())); - (*(*x3.borrow_mut()).as_ref().unwrap().borrow_mut()) = __rhs; + (*(*x3.borrow()).as_ref().unwrap().borrow_mut()) = __rhs; (x4.as_pointer() as Ptr>>).write((*x3.borrow_mut()).take()); let raw_ptr2: Value> = Rc::new(RefCell::new(((*x4.borrow()).as_pointer()))); { @@ -78,28 +85,26 @@ pub fn DoStuffWithSafePointer_0(safe_ptr: Ptr>>) { }; let pair: Value>> = Rc::new(RefCell::new(Some(Rc::new(RefCell::new(Pair { - x: Rc::new(RefCell::new(((*raw_ptr2.borrow()).read()))), - y: Rc::new(RefCell::new(5)), + x: ((*raw_ptr2.borrow()).read()), + y: 5, }))))); ({ PairImpl::inc(&((*pair.borrow()).as_pointer()), 10) }); let __rhs = { let _lhs = { - let _lhs = (*(*(*(*safe_ptr.upgrade().deref()).as_ref().unwrap().borrow()) + let _lhs = (*(*(*safe_ptr.upgrade().deref()).as_ref().unwrap().borrow()) .ptr - .borrow()) - .as_ref() - .unwrap() - .borrow()); - _lhs + (*(*(*pair.borrow()).as_ref().unwrap().borrow()).x.borrow()) + .as_ref() + .unwrap() + .borrow()); + _lhs + ((*pair.borrow()).as_pointer()).with(|__s: &Pair| __s.x) }; - _lhs + (*(*(*pair.borrow()).as_ref().unwrap().borrow()).y.borrow()) + _lhs + ((*pair.borrow()).as_pointer()).with(|__s: &Pair| __s.y) }; - (*(*(*(*safe_ptr.upgrade().deref()).as_ref().unwrap().borrow()) + (*(*(*safe_ptr.upgrade().deref()).as_ref().unwrap().borrow()) .ptr - .borrow_mut()) - .as_ref() - .unwrap() - .borrow_mut()) = __rhs; + .as_ref() + .unwrap() + .borrow_mut()) = __rhs; } pub fn Consume_1(safe_ptr: Option>) -> i32 { let safe_ptr: Value>> = Rc::new(RefCell::new(safe_ptr)); @@ -107,11 +112,12 @@ pub fn Consume_1(safe_ptr: Option>) -> i32 { Rc::new(RefCell::new((*safe_ptr.borrow_mut()).take())); let p: Value>> = Rc::new(RefCell::new(Ptr::alloc(::default()).to_owned_opt())); - return ((*(*(*(*x.borrow()).as_ref().unwrap().borrow()).ptr.borrow()) + return ((*(*(*x.borrow()).as_ref().unwrap().borrow()) + .ptr .as_ref() .unwrap() .borrow()) - + (*(*(*p.borrow()).as_ref().unwrap().borrow()).x.borrow())); + + { (*(*p.borrow()).as_ref().unwrap().borrow()).x }); } pub fn RndStuff_2() { let x1: Value>>> = Rc::new(RefCell::new(None)); @@ -143,31 +149,23 @@ pub fn RndStuff_2() { ))))); let i: Value = Rc::new(RefCell::new(0)); 'loop_: while ((*i.borrow()) < 10) { - (*x3.borrow()).as_ref().unwrap().borrow_mut()[((*i.borrow()) as usize) as usize] = Pair { - x: Rc::new(RefCell::new(1)), - y: Rc::new(RefCell::new(2)), - }; + (*x3.borrow()).as_ref().unwrap().borrow_mut()[((*i.borrow()) as usize) as usize] = + Pair { x: 1, y: 2 }; (*i.borrow_mut()).prefix_inc(); } let p3_0: Value> = Rc::new(RefCell::new((*x3.borrow()).as_pointer())); let i: Value = Rc::new(RefCell::new(0)); 'loop_: while ((*i.borrow()) < 10) { assert!( - ((*(*(*p3_0.borrow()) + ((*p3_0.borrow()) .offset((*i.borrow()) as isize) - .upgrade() - .deref()) - .x - .borrow()) + .with(|__s: &Pair| __s.x) == 1) ); assert!( - ((*(*(*p3_0.borrow()) + ((*p3_0.borrow()) .offset((*i.borrow()) as isize) - .upgrade() - .deref()) - .y - .borrow()) + .with(|__s: &Pair| __s.y) == 2) ); ({ @@ -181,21 +179,15 @@ pub fn RndStuff_2() { ) }); assert!( - ((*(*(*p3_0.borrow()) + ((*p3_0.borrow()) .offset((*i.borrow()) as isize) - .upgrade() - .deref()) - .x - .borrow()) + .with(|__s: &Pair| __s.x) == 11) ); assert!( - ((*(*(*p3_0.borrow()) + ((*p3_0.borrow()) .offset((*i.borrow()) as isize) - .upgrade() - .deref()) - .y - .borrow()) + .with(|__s: &Pair| __s.y) == 12) ); (*i.borrow_mut()).prefix_inc(); @@ -209,8 +201,8 @@ pub fn RndStuff_2() { let i: Value = Rc::new(RefCell::new(0)); 'loop_: while ((*i.borrow()) < 50) { (*x3.borrow()).as_ref().unwrap().borrow_mut()[((*i.borrow()) as usize) as usize] = Pair { - x: Rc::new(RefCell::new(-1_i32)), - y: Rc::new(RefCell::new(-2_i32)), + x: -1_i32, + y: -2_i32, }; (*i.borrow_mut()).prefix_inc(); } @@ -222,21 +214,15 @@ pub fn RndStuff_2() { let i: Value = Rc::new(RefCell::new(0)); 'loop_: while ((*i.borrow()) < 50) { assert!( - ((*(*(*p3_1.borrow()) + ((*p3_1.borrow()) .offset((*i.borrow()) as isize) - .upgrade() - .deref()) - .x - .borrow()) + .with(|__s: &Pair| __s.x) == -1_i32) ); assert!( - ((*(*(*p3_1.borrow()) + ((*p3_1.borrow()) .offset((*i.borrow()) as isize) - .upgrade() - .deref()) - .y - .borrow()) + .with(|__s: &Pair| __s.y) == -2_i32) ); ({ @@ -250,21 +236,15 @@ pub fn RndStuff_2() { ) }); assert!( - ((*(*(*p3_1.borrow()) + ((*p3_1.borrow()) .offset((*i.borrow()) as isize) - .upgrade() - .deref()) - .x - .borrow()) + .with(|__s: &Pair| __s.x) == -11_i32) ); assert!( - ((*(*(*p3_1.borrow()) + ((*p3_1.borrow()) .offset((*i.borrow()) as isize) - .upgrade() - .deref()) - .y - .borrow()) + .with(|__s: &Pair| __s.y) == -12_i32) ); (*i.borrow_mut()).prefix_inc(); @@ -279,7 +259,7 @@ fn main_0() -> i32 { let safe_ptr: Value>> = Rc::new(RefCell::new(Some(Rc::new(RefCell::new({ let __tmp_0: Value = Rc::new(RefCell::new(SafePointer { - ptr: Rc::new(RefCell::new((*x.borrow_mut()).take())), + ptr: (*x.borrow_mut()).take(), })); SafePointer::move_from({ __tmp_0.as_pointer() }) }))))); @@ -293,8 +273,8 @@ pub trait PairImpl { impl PairImpl for Ptr { fn inc(&self, k: i32) { let k: Value = Rc::new(RefCell::new(k)); - (*(*(*self).upgrade().deref()).x.borrow_mut()) += (*k.borrow()); - (*(*(*self).upgrade().deref()).y.borrow_mut()) += (*k.borrow()); + (*self).with_mut(|__s: &mut Pair| __s.x += (*k.borrow())); + (*self).with_mut(|__s: &mut Pair| __s.y += (*k.borrow())); } } pub trait SafePointerImpl { @@ -303,15 +283,16 @@ pub trait SafePointerImpl { } impl SafePointerImpl for Ptr { fn inc(&self) { - (*(*(*(*self).upgrade().deref()).ptr.borrow_mut()) + (*(*(*self).upgrade().deref()) + .ptr .as_ref() .unwrap() .borrow_mut()) .prefix_inc(); } fn move_assign(&self, _a0: Ptr) -> Ptr { - ((*(*self).upgrade().deref()).ptr.as_pointer() as Ptr>>) - .write((*(*_a0.upgrade().deref()).ptr.borrow_mut()).take()); + ((field_ptr!((*self), ptr) as Ptr>>) as Ptr>>) + .write(_a0.with_mut(|__s: &mut SafePointer| __s.ptr.take())); return (*self).clone(); } } diff --git a/tests/unit/out/refcount/unique_ptr_const_deref.rs b/tests/unit/out/refcount/unique_ptr_const_deref.rs index e4b70edfd5..e6ce3ca654 100644 --- a/tests/unit/out/refcount/unique_ptr_const_deref.rs +++ b/tests/unit/out/refcount/unique_ptr_const_deref.rs @@ -6,25 +6,41 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Default)] +#[derive(Record, Default)] pub struct Holder { - pub val: Value>>, + #[offset(0)] + pub val: Option>, } impl Holder { pub fn move_from(_a0: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - val: Rc::new(RefCell::new( - (*(*_a0.upgrade().deref()).val.borrow_mut()).take(), - )), + val: { _a0.with_mut(|__s: &mut Holder| __s.val.take()) }, })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } } +<<<<<<< HEAD impl ByteRepr for Holder {} +======= +impl ByteRepr for Holder { + fn byte_size() -> usize { + 8 + } + fn to_bytes(&self, buf: &mut [u8]) { + self.val.to_bytes(&mut buf[0..8]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + val: >>::from_bytes(&buf[0..8]), + } + } +} +>>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub fn read_val_0(h: Ptr) -> i32 { let h: Value> = Rc::new(RefCell::new(h)); - return (*(*(*(*h.borrow()).upgrade().deref()).val.borrow()) + return (*(*(*h.borrow()).upgrade().deref()) + .val .as_ref() .unwrap() .borrow()); @@ -32,7 +48,8 @@ pub fn read_val_0(h: Ptr) -> i32 { pub fn write_val_1(h: Ptr, v: i32) { let h: Value> = Rc::new(RefCell::new(h)); let v: Value = Rc::new(RefCell::new(v)); - (*(*(*(*h.borrow()).upgrade().deref()).val.borrow_mut()) + (*(*(*h.borrow()).upgrade().deref()) + .val .as_ref() .unwrap() .borrow_mut()) = (*v.borrow()); @@ -43,7 +60,7 @@ pub fn main() { } fn main_0() -> i32 { let h: Value = Rc::new(RefCell::new(::default())); - ((*h.borrow()).val.as_pointer() as Ptr>>) + ((field_ptr!(h, val) as Ptr>>) as Ptr>>) .write(Some(Rc::new(RefCell::new(10))).take()); ({ write_val_1((h.as_pointer()), 42) }); assert!((({ read_val_0((h.as_pointer()),) }) == 42)); @@ -54,8 +71,8 @@ pub trait HolderImpl { } impl HolderImpl for Ptr { fn move_assign(&self, _a0: Ptr) -> Ptr { - ((*(*self).upgrade().deref()).val.as_pointer() as Ptr>>) - .write((*(*_a0.upgrade().deref()).val.borrow_mut()).take()); + ((field_ptr!((*self), val) as Ptr>>) as Ptr>>) + .write(_a0.with_mut(|__s: &mut Holder| __s.val.take())); return (*self).clone(); } } diff --git a/tests/unit/out/refcount/unique_ptr_deref_ops.rs b/tests/unit/out/refcount/unique_ptr_deref_ops.rs index 446fb5f723..42fc27408d 100644 --- a/tests/unit/out/refcount/unique_ptr_deref_ops.rs +++ b/tests/unit/out/refcount/unique_ptr_deref_ops.rs @@ -12,9 +12,9 @@ pub fn main() { } fn main_0() -> i32 { let p: Value>> = Rc::new(RefCell::new(Some(Rc::new(RefCell::new(10))))); - (*(*p.borrow_mut()).as_ref().unwrap().borrow_mut()) += 5; - (*(*p.borrow_mut()).as_ref().unwrap().borrow_mut()) -= 3; - (*(*p.borrow_mut()).as_ref().unwrap().borrow_mut()) *= 2; + (*(*p.borrow()).as_ref().unwrap().borrow_mut()) += 5; + (*(*p.borrow()).as_ref().unwrap().borrow_mut()) -= 3; + (*(*p.borrow()).as_ref().unwrap().borrow_mut()) *= 2; let q: Value>> = Rc::new(RefCell::new(Some(Rc::new(RefCell::new(1))))); let sum: Value = Rc::new(RefCell::new( ((*(*p.borrow()).as_ref().unwrap().borrow()) + (*(*q.borrow()).as_ref().unwrap().borrow())), diff --git a/tests/unit/out/refcount/unique_ptr_nested.rs b/tests/unit/out/refcount/unique_ptr_nested.rs index e90b0333d0..c7576c5be0 100644 --- a/tests/unit/out/refcount/unique_ptr_nested.rs +++ b/tests/unit/out/refcount/unique_ptr_nested.rs @@ -6,52 +6,59 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Inner { - pub x: Value, - pub y: Value, -} -impl Clone for Inner { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - x: Rc::new(RefCell::new((*self.x.borrow()))), - y: Rc::new(RefCell::new((*self.y.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub x: i32, + #[offset(4)] + pub y: i32, } impl ByteRepr for Inner { fn byte_size() -> usize { 8 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.x.borrow()).to_bytes(&mut buf[0..4]); - (*self.y.borrow()).to_bytes(&mut buf[4..8]); + self.x.to_bytes(&mut buf[0..4]); + self.y.to_bytes(&mut buf[4..8]); } fn from_bytes(buf: &[u8]) -> Self { Self { - x: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - y: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + x: ::from_bytes(&buf[0..4]), + y: ::from_bytes(&buf[4..8]), } } } -#[derive(Default)] +#[derive(Record, Default)] pub struct Outer { - pub inner: Value>>, + #[offset(0)] + pub inner: Option>, } impl Outer { pub fn move_from(_a0: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - inner: Rc::new(RefCell::new( - (*(*_a0.upgrade().deref()).inner.borrow_mut()).take(), - )), + inner: { _a0.with_mut(|__s: &mut Outer| __s.inner.take()) }, })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } } +<<<<<<< HEAD impl ByteRepr for Outer {} +======= +impl ByteRepr for Outer { + fn byte_size() -> usize { + 8 + } + fn to_bytes(&self, buf: &mut [u8]) { + self.inner.to_bytes(&mut buf[0..8]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + inner: >>::from_bytes(&buf[0..8]), + } + } +} +>>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub fn main() { __cpp2rust_init_globals(); std::process::exit(main_0()); @@ -59,37 +66,37 @@ pub fn main() { fn main_0() -> i32 { let o: Value>> = Rc::new(RefCell::new(Some(Rc::new(RefCell::new({ let __tmp_0: Value = Rc::new(RefCell::new(Outer { - inner: Rc::new(RefCell::new(Some(Rc::new(RefCell::new(Inner { - x: Rc::new(RefCell::new(10)), - y: Rc::new(RefCell::new(20)), - }))))), + inner: Some(Rc::new(RefCell::new(Inner { x: 10, y: 20 }))), })); Outer::move_from({ __tmp_0.as_pointer() }) }))))); - (*(*(*(*(*o.borrow()).as_ref().unwrap().borrow()).inner.borrow()) + (*(*(*o.borrow()).as_ref().unwrap().borrow()) + .inner .as_ref() .unwrap() - .borrow()) - .x - .borrow_mut()) += 5; + .borrow_mut()) + .x += 5; let sum: Value = Rc::new(RefCell::new( - ((*(*(*(*(*o.borrow()).as_ref().unwrap().borrow()).inner.borrow()) - .as_ref() - .unwrap() - .borrow()) - .x - .borrow()) - + (*(*(*(*(*o.borrow()).as_ref().unwrap().borrow()).inner.borrow()) + ({ + (*(*(*o.borrow()).as_ref().unwrap().borrow()) + .inner + .as_ref() + .unwrap() + .borrow()) + .x + } + { + (*(*(*o.borrow()).as_ref().unwrap().borrow()) + .inner .as_ref() .unwrap() .borrow()) .y - .borrow())), + }), )); let a: Value>> = Rc::new(RefCell::new(Some(Rc::new(RefCell::new(100))))); let b: Value>> = Rc::new(RefCell::new(Some(Rc::new(RefCell::new(0))))); let __rhs = (*(*a.borrow()).as_ref().unwrap().borrow()); - (*(*b.borrow_mut()).as_ref().unwrap().borrow_mut()) = __rhs; + (*(*b.borrow()).as_ref().unwrap().borrow_mut()) = __rhs; assert!((((*sum.borrow()) + (*(*b.borrow()).as_ref().unwrap().borrow())) == 135)); return 0; } @@ -98,8 +105,8 @@ pub trait OuterImpl { } impl OuterImpl for Ptr { fn move_assign(&self, _a0: Ptr) -> Ptr { - ((*(*self).upgrade().deref()).inner.as_pointer() as Ptr>>) - .write((*(*_a0.upgrade().deref()).inner.borrow_mut()).take()); + ((field_ptr!((*self), inner) as Ptr>>) as Ptr>>) + .write(_a0.with_mut(|__s: &mut Outer| __s.inner.take())); return (*self).clone(); } } diff --git a/tests/unit/out/refcount/unique_ptr_struct.rs b/tests/unit/out/refcount/unique_ptr_struct.rs index 7f80b770f2..a92f120722 100644 --- a/tests/unit/out/refcount/unique_ptr_struct.rs +++ b/tests/unit/out/refcount/unique_ptr_struct.rs @@ -6,39 +6,31 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Point { - pub x: Value, - pub y: Value, -} -impl Clone for Point { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - x: Rc::new(RefCell::new((*self.x.borrow()))), - y: Rc::new(RefCell::new((*self.y.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub x: i32, + #[offset(4)] + pub y: i32, } impl ByteRepr for Point { fn byte_size() -> usize { 8 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.x.borrow()).to_bytes(&mut buf[0..4]); - (*self.y.borrow()).to_bytes(&mut buf[4..8]); + self.x.to_bytes(&mut buf[0..4]); + self.y.to_bytes(&mut buf[4..8]); } fn from_bytes(buf: &[u8]) -> Self { Self { - x: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - y: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + x: ::from_bytes(&buf[0..4]), + y: ::from_bytes(&buf[4..8]), } } } pub fn sum_0(p: Point) -> i32 { let p: Value = Rc::new(RefCell::new(p)); - return ((*(*p.borrow()).x.borrow()) + (*(*p.borrow()).y.borrow())); + return ({ (*p.borrow()).x } + { (*p.borrow()).y }); } pub fn main() { __cpp2rust_init_globals(); @@ -47,13 +39,14 @@ pub fn main() { fn main_0() -> i32 { let p: Value>> = Rc::new(RefCell::new(Some(Rc::new(RefCell::new(Point { - x: Rc::new(RefCell::new(3)), - y: Rc::new(RefCell::new(4)), + x: 3, + y: 4, }))))); - (*(*(*p.borrow()).as_ref().unwrap().borrow()).x.borrow_mut()) += 10; - let __rhs = ((*(*(*p.borrow()).as_ref().unwrap().borrow()).x.borrow()) - + (*(*(*p.borrow()).as_ref().unwrap().borrow()).y.borrow())); - (*(*(*p.borrow()).as_ref().unwrap().borrow()).y.borrow_mut()) = __rhs; + (*(*p.borrow()).as_ref().unwrap().borrow_mut()).x += 10; + let __rhs = ({ (*(*p.borrow()).as_ref().unwrap().borrow()).x } + { + (*(*p.borrow()).as_ref().unwrap().borrow()).y + }); + (*(*p.borrow()).as_ref().unwrap().borrow_mut()).y = __rhs; let s: Value = Rc::new(RefCell::new( ({ sum_0((*(*p.borrow()).as_ref().unwrap().borrow()).clone()) }), )); diff --git a/tests/unit/out/refcount/va_arg_mixed_types.rs b/tests/unit/out/refcount/va_arg_mixed_types.rs index 58adbd549c..62557d6718 100644 --- a/tests/unit/out/refcount/va_arg_mixed_types.rs +++ b/tests/unit/out/refcount/va_arg_mixed_types.rs @@ -6,31 +6,25 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct pair { - pub a: Value, - pub b: Value, -} -impl Clone for pair { - fn clone(&self) -> Self { - Self { - a: Rc::new(RefCell::new((*self.a.borrow()).clone())), - b: Rc::new(RefCell::new((*self.b.borrow()).clone())), - } - } + #[offset(0)] + pub a: i32, + #[offset(4)] + pub b: i32, } impl ByteRepr for pair { fn byte_size() -> usize { 8 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.a.borrow()).to_bytes(&mut buf[0..4]); - (*self.b.borrow()).to_bytes(&mut buf[4..8]); + self.a.to_bytes(&mut buf[0..4]); + self.b.to_bytes(&mut buf[4..8]); } fn from_bytes(buf: &[u8]) -> Self { Self { - a: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - b: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + a: ::from_bytes(&buf[0..4]), + b: ::from_bytes(&buf[4..8]), } } } @@ -48,7 +42,7 @@ pub fn sum_mixed_0(count: i32, __args: &[VaArg]) -> i32 { (*total.borrow_mut()) += ((*ap.borrow_mut()).arg::() as i32); } else if ((((*tag.borrow()) == 3) as i32) != 0) { let p: Value = Rc::new(RefCell::new((*ap.borrow_mut()).arg::())); - (*total.borrow_mut()) += ((*(*p.borrow()).a.borrow()) * (*(*p.borrow()).b.borrow())); + (*total.borrow_mut()) += ({ (*p.borrow()).a } * { (*p.borrow()).b }); } else { let val: Value = Rc::new(RefCell::new((*ap.borrow_mut()).arg::())); (*total.borrow_mut()) += ((*val.borrow()) as i32); @@ -88,10 +82,7 @@ fn main_0() -> i32 { }) == 103) as i32) != 0) ); - let p: Value = Rc::new(RefCell::new(pair { - a: Rc::new(RefCell::new(7)), - b: Rc::new(RefCell::new(8)), - })); + let p: Value = Rc::new(RefCell::new(pair { a: 7, b: 8 })); assert!( (((({ sum_mixed_0( diff --git a/tests/unit/out/refcount/va_arg_non_primitive_ptrs.rs b/tests/unit/out/refcount/va_arg_non_primitive_ptrs.rs index b4a2a3f30b..a1f9235fe2 100644 --- a/tests/unit/out/refcount/va_arg_non_primitive_ptrs.rs +++ b/tests/unit/out/refcount/va_arg_non_primitive_ptrs.rs @@ -6,31 +6,25 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct node { - pub data: Value, - pub next: Value>, -} -impl Clone for node { - fn clone(&self) -> Self { - Self { - data: Rc::new(RefCell::new((*self.data.borrow()).clone())), - next: Rc::new(RefCell::new((*self.next.borrow()).clone())), - } - } + #[offset(0)] + pub data: i32, + #[offset(8)] + pub next: Ptr, } impl ByteRepr for node { fn byte_size() -> usize { 16 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.data.borrow()).to_bytes(&mut buf[0..4]); - (*self.next.borrow()).to_bytes(&mut buf[8..16]); + self.data.to_bytes(&mut buf[0..4]); + self.next.to_bytes(&mut buf[8..16]); } fn from_bytes(buf: &[u8]) -> Self { Self { - data: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - next: Rc::new(RefCell::new(>::from_bytes(&buf[8..16]))), + data: ::from_bytes(&buf[0..4]), + next: >::from_bytes(&buf[8..16]), } } } @@ -63,7 +57,7 @@ pub fn dispatch_0(option: i32, __args: &[VaArg]) -> i32 { __v if __v == (opt_OPT_NODE as i32) => { let n: Value> = Rc::new(RefCell::new((*ap.borrow_mut()).arg::>())); - (*result.borrow_mut()) = (*(*(*n.borrow()).upgrade().deref()).data.borrow()); + (*result.borrow_mut()) = (*n.borrow()).with(|__s: &node| __s.data); break 'switch; } __v if __v == (opt_OPT_NODE_OUT as i32) => { @@ -91,12 +85,8 @@ fn main_0() -> i32 { ); assert!((((!((*s.borrow()).is_null())) as i32) != 0)); assert!( - (((({ - dispatch_0( - (opt_OPT_FILE as i32), - &[((libcc2rs::c_stdout()).clone()).into()], - ) - }) == 1) as i32) + (((({ dispatch_0((opt_OPT_FILE as i32), &[(libcc2rs::c_stdout()).into(),]) }) == 1) + as i32) != 0) ); assert!( @@ -109,8 +99,8 @@ fn main_0() -> i32 { != 0) ); let head: Value = Rc::new(RefCell::new(node { - data: Rc::new(RefCell::new(42)), - next: Rc::new(RefCell::new(Ptr::::null())), + data: 42, + next: Ptr::::null(), })); assert!( (((({ dispatch_0((opt_OPT_NODE as i32), &[(head.as_pointer()).into(),]) }) == 42) as i32) diff --git a/tests/unit/out/refcount/va_arg_struct_ctx.rs b/tests/unit/out/refcount/va_arg_struct_ctx.rs index 6e398207c2..bc3a6c03e3 100644 --- a/tests/unit/out/refcount/va_arg_struct_ctx.rs +++ b/tests/unit/out/refcount/va_arg_struct_ctx.rs @@ -6,42 +6,36 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct context { - pub verbose: Value, - pub last_error: Value, -} -impl Clone for context { - fn clone(&self) -> Self { - Self { - verbose: Rc::new(RefCell::new((*self.verbose.borrow()).clone())), - last_error: Rc::new(RefCell::new((*self.last_error.borrow()).clone())), - } - } + #[offset(0)] + pub verbose: i32, + #[offset(4)] + pub last_error: i32, } impl ByteRepr for context { fn byte_size() -> usize { 8 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.verbose.borrow()).to_bytes(&mut buf[0..4]); - (*self.last_error.borrow()).to_bytes(&mut buf[4..8]); + self.verbose.to_bytes(&mut buf[0..4]); + self.last_error.to_bytes(&mut buf[4..8]); } fn from_bytes(buf: &[u8]) -> Self { Self { - verbose: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - last_error: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + verbose: ::from_bytes(&buf[0..4]), + last_error: ::from_bytes(&buf[4..8]), } } } pub fn set_error_0(ctx: Ptr, fmt: Ptr, __args: &[VaArg]) { let ctx: Value> = Rc::new(RefCell::new(ctx)); let fmt: Value> = Rc::new(RefCell::new(fmt)); - if ((*(*(*ctx.borrow()).upgrade().deref()).verbose.borrow()) != 0) { + if ((*ctx.borrow()).with(|__s: &context| __s.verbose) != 0) { let ap: Value = Rc::new(RefCell::new(VaList::default())); (*ap.borrow_mut()) = VaList::new(__args); - (*(*(*ctx.borrow()).upgrade().deref()).last_error.borrow_mut()) = - (*ap.borrow_mut()).arg::(); + let __rhs = (*ap.borrow_mut()).arg::(); + (*ctx.borrow()).with_mut(|__s: &mut context| __s.last_error = __rhs); } } pub fn main() { @@ -50,8 +44,8 @@ pub fn main() { } fn main_0() -> i32 { let ctx: Value = >::default(); - (*(*ctx.borrow()).verbose.borrow_mut()) = 1; - (*(*ctx.borrow()).last_error.borrow_mut()) = 0; + (*ctx.borrow_mut()).verbose = 1; + (*ctx.borrow_mut()).last_error = 0; ({ set_error_0( (ctx.as_pointer()), @@ -59,8 +53,8 @@ fn main_0() -> i32 { &[(42).into()], ) }); - assert!(((((*(*ctx.borrow()).last_error.borrow()) == 42) as i32) != 0)); - (*(*ctx.borrow()).verbose.borrow_mut()) = 0; + assert!(((({ (*ctx.borrow()).last_error } == 42) as i32) != 0)); + (*ctx.borrow_mut()).verbose = 0; ({ set_error_0( (ctx.as_pointer()), @@ -68,7 +62,7 @@ fn main_0() -> i32 { &[(99).into()], ) }); - assert!(((((*(*ctx.borrow()).last_error.borrow()) == 42) as i32) != 0)); + assert!(((({ (*ctx.borrow()).last_error } == 42) as i32) != 0)); return 0; } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/va_arg_void_ptr.rs b/tests/unit/out/refcount/va_arg_void_ptr.rs index 54360faab4..46c301ca1d 100644 --- a/tests/unit/out/refcount/va_arg_void_ptr.rs +++ b/tests/unit/out/refcount/va_arg_void_ptr.rs @@ -6,31 +6,25 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct registry { - pub slot: Value, - pub level: Value, -} -impl Clone for registry { - fn clone(&self) -> Self { - Self { - slot: Rc::new(RefCell::new((*self.slot.borrow()).clone())), - level: Rc::new(RefCell::new((*self.level.borrow()).clone())), - } - } + #[offset(0)] + pub slot: AnyPtr, + #[offset(8)] + pub level: i64, } impl ByteRepr for registry { fn byte_size() -> usize { 16 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.slot.borrow()).to_bytes(&mut buf[0..8]); - (*self.level.borrow()).to_bytes(&mut buf[8..16]); + self.slot.to_bytes(&mut buf[0..8]); + self.level.to_bytes(&mut buf[8..16]); } fn from_bytes(buf: &[u8]) -> Self { Self { - slot: Rc::new(RefCell::new(::from_bytes(&buf[0..8]))), - level: Rc::new(RefCell::new(::from_bytes(&buf[8..16]))), + slot: ::from_bytes(&buf[0..8]), + level: ::from_bytes(&buf[8..16]), } } } @@ -47,13 +41,13 @@ pub fn registry_update_0(r: Ptr, field: field, __args: &[VaArg]) -> i3 let __match_cond = ((*field.borrow()) as u32); match __match_cond { __v if __v == ((field_FIELD_SLOT as i32) as u32) => { - (*(*(*r.borrow()).upgrade().deref()).slot.borrow_mut()) = - (*ap.borrow_mut()).arg::(); + let __rhs = (*ap.borrow_mut()).arg::(); + (*r.borrow()).with_mut(|__s: &mut registry| __s.slot = __rhs); break 'switch; } __v if __v == ((field_FIELD_LEVEL as i32) as u32) => { - (*(*(*r.borrow()).upgrade().deref()).level.borrow_mut()) = - (*ap.borrow_mut()).arg::(); + let __rhs = (*ap.borrow_mut()).arg::(); + (*r.borrow()).with_mut(|__s: &mut registry| __s.level = __rhs); break 'switch; } _ => { @@ -70,8 +64,8 @@ pub fn main() { } fn main_0() -> i32 { let r: Value = Rc::new(RefCell::new(registry { - slot: Rc::new(RefCell::new(AnyPtr::default())), - level: Rc::new(RefCell::new(0_i64)), + slot: AnyPtr::default(), + level: 0_i64, })); let payload: Value = Rc::new(RefCell::new(7)); assert!( @@ -91,19 +85,19 @@ fn main_0() -> i32 { ); assert!( ((({ - let _lhs = (*(*r.borrow()).slot.borrow()).clone(); + let _lhs = { (*r.borrow()).slot.clone() }; _lhs == (payload.as_pointer()).to_any() }) as i32) != 0) ); assert!( - (((((*(*r.borrow()).slot.borrow()) + (((({ (*r.borrow()).slot.clone() } .reinterpret_cast::() .read()) == 7) as i32) != 0) ); - assert!(((((*(*r.borrow()).level.borrow()) == 5_i64) as i32) != 0)); + assert!(((({ (*r.borrow()).level } == 5_i64) as i32) != 0)); return 0; } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/vector_subscript_direct.rs b/tests/unit/out/refcount/vector_subscript_direct.rs new file mode 100644 index 0000000000..85dd48e97c --- /dev/null +++ b/tests/unit/out/refcount/vector_subscript_direct.rs @@ -0,0 +1,210 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] +pub struct Point { + #[offset(0)] + pub x: i32, + #[offset(4)] + pub y: i32, +} +impl ByteRepr for Point { + fn byte_size() -> usize { + 8 + } + fn to_bytes(&self, buf: &mut [u8]) { + self.x.to_bytes(&mut buf[0..4]); + self.y.to_bytes(&mut buf[4..8]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + x: ::from_bytes(&buf[0..4]), + y: ::from_bytes(&buf[4..8]), + } + } +} +#[derive(Record, VaArg, FnPtrArg, Default)] +pub struct Holder { + #[offset(0)] + pub values: Value>, + #[offset(24)] + pub points: Value>, +} +impl Clone for Holder { + fn clone(&self) -> Self { + let __this: Value = Rc::new(RefCell::new(Self { + values: { Rc::new(RefCell::new((*self.values.borrow()).clone())) }, + points: { Rc::new(RefCell::new((*self.points.borrow()).clone())) }, + })); + let this: Ptr = __this.as_pointer(); + Rc::try_unwrap(__this).ok().unwrap().into_inner() + } +} +impl ByteRepr for Holder { + fn byte_size() -> usize { + 48 + } + fn to_bytes(&self, buf: &mut [u8]) { + self.values.to_bytes(&mut buf[0..24]); + self.points.to_bytes(&mut buf[24..48]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + values: >>::from_bytes(&buf[0..24]), + points: >>::from_bytes(&buf[24..48]), + } + } +} +pub fn push_and_index_0(v: Ptr>) -> i32 { + v.with_mut(|__v: &mut Vec| __v.push(42)); + return (((*v.upgrade().deref()).len() as i32) - 1); +} +pub fn sum_ref_1(v: Ptr>) -> i32 { + let s: Value = Rc::new(RefCell::new(0)); + let i: Value = Rc::new(RefCell::new(0_usize)); + 'loop_: while { + let _lhs = (*i.borrow()); + _lhs < (*v.upgrade().deref()).len() + } { + (*s.borrow_mut()) += ((Ptr::>::decay(&(v)) as Ptr) + .offset((*i.borrow())) + .read()); + (*i.borrow_mut()).prefix_inc(); + } + return (*s.borrow()); +} +pub fn main() { + __cpp2rust_init_globals(); + std::process::exit(main_0()); +} +fn main_0() -> i32 { + let v: Value> = Rc::new(RefCell::new(vec![1, 2, 3])); + (v.as_pointer() as Ptr).offset(0_usize).write(10); + assert!((((v.as_pointer() as Ptr).offset(0_usize).read()) == 10)); + { + let _ptr = (v.as_pointer() as Ptr).offset(1_usize); + _ptr.write(_ptr.read() + 5) + }; + (v.as_pointer() as Ptr) + .offset(2_usize) + .with_mut(|__v| __v.postfix_inc()); + assert!( + (((v.as_pointer() as Ptr).offset(1_usize).read()) == 7) + && (((v.as_pointer() as Ptr).offset(2_usize).read()) == 4) + ); + let __rhs = ((v.as_pointer() as Ptr).offset(1_usize).read()); + (v.as_pointer() as Ptr).offset(0_usize).write(__rhs); + assert!((((v.as_pointer() as Ptr).offset(0_usize).read()) == 7)); + (v.as_pointer() as Ptr).offset(1_usize).write(0); + assert!( + (((v.as_pointer() as Ptr) + .offset((((v.as_pointer() as Ptr).offset(1_usize).read()) as usize)) + .read()) + == 7) + ); + let i: Value = Rc::new(RefCell::new(0)); + let __rhs = 3; + let __idx = ((*i.borrow_mut()).postfix_inc() as usize); + (v.as_pointer() as Ptr).offset(__idx).write(__rhs); + assert!(((*i.borrow()) == 1) && (((v.as_pointer() as Ptr).offset(0_usize).read()) == 3)); + assert!( + (((v.as_pointer() as Ptr) + .offset((({ push_and_index_0(v.as_pointer(),) }) as usize)) + .read()) + == 42) + ); + let __rhs = 5; + let __idx = (({ push_and_index_0(v.as_pointer()) }) as usize); + (v.as_pointer() as Ptr).offset(__idx).write(__rhs); + assert!( + ((*v.borrow()).len() == 5_usize) + && (((v.as_pointer() as Ptr).offset(4_usize).read()) == 5) + ); + let p: Value> = Rc::new(RefCell::new(((v.as_pointer() as Ptr).offset(2_usize)))); + (*p.borrow()).write(9); + assert!((((v.as_pointer() as Ptr).offset(2_usize).read()) == 9)); + assert!((({ sum_ref_1(v.as_pointer(),) }) == ((((3 + 0) + 9) + 42) + 5))); + let h: Value = Rc::new(RefCell::new(::default())); + { + let __a0 = 2_usize as usize; + (*(*h.borrow()).values.borrow_mut()).resize_with(__a0, || ::default()) + }; + (*(*h.borrow()).values.borrow_mut())[(1_usize) as usize] = 6; + (*(*h.borrow()).points.borrow_mut()).push(Point { x: 1, y: 2 }); + (*(*h.borrow()).points.borrow_mut())[(0_usize) as usize].y = 5; + assert!(({ (*(*h.borrow()).values.borrow())[(1_usize) as usize] } == 6)); + assert!( + (({ + PointImpl::sum(&{ + ({ (*h.borrow()).points.as_pointer() } as Ptr).offset(0_usize) + }) + }) == 6) + ); + let hp: Value> = Rc::new(RefCell::new((h.as_pointer()))); + let __rhs = + ((*hp.borrow()).with(|__s: &Holder| (*__s.values.borrow())[(1_usize) as usize]) + 1); + (*hp.borrow()).with(|__s: &Holder| (*__s.values.borrow_mut())[(0_usize) as usize] = __rhs); + let __rhs = (*hp.borrow()).with(|__s: &Holder| (*__s.values.borrow())[(0_usize) as usize]); + (*hp.borrow()).with(|__s: &Holder| (*__s.points.borrow_mut())[(0_usize) as usize].x = __rhs); + assert!( + ({ (*(*h.borrow()).values.borrow())[(0_usize) as usize] } == 7) + && ({ (*(*h.borrow()).points.borrow())[(0_usize) as usize].x } == 7) + ); + let q: Value = Rc::new(RefCell::new( + ((*(*h.borrow()).points.borrow())[(0_usize) as usize]).clone(), + )); + (*q.borrow_mut()).x = 0; + assert!(({ (*(*h.borrow()).points.borrow())[(0_usize) as usize].x } == 7)); + let grid: Value>>> = Rc::new(RefCell::new(Vec::new())); + (grid.as_pointer() as Ptr>>>).with_mut(|__v: &mut Vec>>| { + __v.push(Rc::new(RefCell::new(vec![0; 3_usize as usize]))) + }); + (grid.as_pointer() as Ptr>>>).with_mut(|__v: &mut Vec>>| { + __v.push(Rc::new(RefCell::new(vec![0; 3_usize as usize]))) + }); + ((grid.as_pointer() as Ptr>>) + .offset(1_usize) + .upgrade() + .deref() + .as_pointer() as Ptr) + .offset(2_usize) + .write(8); + assert!( + ((((grid.as_pointer() as Ptr>>) + .offset(1_usize) + .upgrade() + .deref() + .as_pointer() as Ptr) + .offset(2_usize) + .read()) + == 8) + && ((((grid.as_pointer() as Ptr>>) + .offset(0_usize) + .upgrade() + .deref() + .as_pointer() as Ptr) + .offset(2_usize) + .read()) + == 0) + ); + let a: Value> = Rc::new(RefCell::new(vec![4, 5, 6])); + let __rhs = (((a.as_pointer() as Ptr).offset(0_usize).read()) + + ((a.as_pointer() as Ptr).offset(2_usize).read())); + (a.as_pointer() as Ptr).offset(1_usize).write(__rhs); + assert!((((a.as_pointer() as Ptr).offset(1_usize).read()) == 10)); + return 0; +} +pub trait PointImpl { + fn sum(&self) -> i32; +} +impl PointImpl for Ptr { + fn sum(&self) -> i32 { + return ((*self).with(|__s: &Point| __s.x) + (*self).with(|__s: &Point| __s.y)); + } +} +pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/vector_with_allocator.rs b/tests/unit/out/refcount/vector_with_allocator.rs index 1e5d1a91b7..2ca30f33dc 100644 --- a/tests/unit/out/refcount/vector_with_allocator.rs +++ b/tests/unit/out/refcount/vector_with_allocator.rs @@ -6,9 +6,9 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Clone, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] pub struct TestAllocator_int_ {} -#[derive(Clone, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] pub struct TestAllocator_double_ {} pub fn copy_0(copy_vector: Vec) { let copy_vector: Value> = Rc::new(RefCell::new(copy_vector)); diff --git a/tests/unit/out/refcount/void_cast.rs b/tests/unit/out/refcount/void_cast.rs index a7fff2c360..e95a9ba416 100644 --- a/tests/unit/out/refcount/void_cast.rs +++ b/tests/unit/out/refcount/void_cast.rs @@ -10,20 +10,37 @@ pub fn unused_param_0(x: i32) { let x: Value = Rc::new(RefCell::new(x)); &(*x.borrow_mut()); } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Record, VaArg, FnPtrArg, Default)] pub struct NonTrivial { + #[offset(0)] pub data: Value>, } impl Clone for NonTrivial { fn clone(&self) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - data: Rc::new(RefCell::new((*self.data.borrow()).clone())), + data: { Rc::new(RefCell::new((*self.data.borrow()).clone())) }, })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } } +<<<<<<< HEAD impl ByteRepr for NonTrivial {} +======= +impl ByteRepr for NonTrivial { + fn byte_size() -> usize { + 24 + } + fn to_bytes(&self, buf: &mut [u8]) { + self.data.to_bytes(&mut buf[0..24]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + data: >>::from_bytes(&buf[0..24]), + } + } +} +>>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub fn unused_ref_param_1(x: Ptr) { &(*x.upgrade().deref()); } @@ -38,48 +55,55 @@ pub fn bump_and_return_4() -> i32 { (*side_effect_counter_3.with(Value::clone).borrow_mut()).prefix_inc(); return side_effect_counter_3.with(|rc| *rc.borrow()); } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Holder { - pub field: Value, -} -impl Clone for Holder { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - field: Rc::new(RefCell::new((*self.field.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub field: i32, } impl ByteRepr for Holder { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.field.borrow()).to_bytes(&mut buf[0..4]); + self.field.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - field: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + field: ::from_bytes(&buf[0..4]), } } } -#[derive(Default)] +#[derive(Record, Default)] pub struct NonCopyable { - pub value: Value>>, + #[offset(0)] + pub value: Option>, } impl NonCopyable { pub fn move_from(_a0: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - value: Rc::new(RefCell::new( - (*(*_a0.upgrade().deref()).value.borrow_mut()).take(), - )), + value: { _a0.with_mut(|__s: &mut NonCopyable| __s.value.take()) }, })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } } +<<<<<<< HEAD impl ByteRepr for NonCopyable {} +======= +impl ByteRepr for NonCopyable { + fn byte_size() -> usize { + 8 + } + fn to_bytes(&self, buf: &mut [u8]) { + self.value.to_bytes(&mut buf[0..8]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + value: >>::from_bytes(&buf[0..8]), + } + } +} +>>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub fn unused_noncopyable_param_5(x: Ptr) { &(*x.upgrade().deref()); } @@ -140,22 +164,20 @@ fn main_0() -> i32 { &(*p.borrow_mut()); let arr: Value> = Rc::new(RefCell::new(Box::new([1, 2, 3]))); &((*arr.borrow_mut())[(1) as usize]); - let h: Value = Rc::new(RefCell::new(Holder { - field: Rc::new(RefCell::new(17)), - })); - &(*(*h.borrow()).field.borrow_mut()); + let h: Value = Rc::new(RefCell::new(Holder { field: 17 })); + &((*h.borrow()).field); let hp: Value> = Rc::new(RefCell::new((h.as_pointer()))); - &(*(*(*hp.borrow()).upgrade().deref()).field.borrow_mut()); + &((*(*hp.borrow()).upgrade().deref()).field); let nt: Value = Rc::new(RefCell::new(::default())); ({ unused_ref_param_1(nt.as_pointer()) }); ({ unused_ptr_param_2((nt.as_pointer())) }); let g: Value = Rc::new(RefCell::new(NonCopyable { - value: Rc::new(RefCell::new(Some(Rc::new(RefCell::new(9))))), + value: Some(Rc::new(RefCell::new(9))), })); (&(*g.borrow_mut())); &(*g.borrow_mut()); ({ unused_noncopyable_param_5(g.as_pointer()) }); - assert!(((*(*(*g.borrow()).value.borrow()).as_ref().unwrap().borrow()) == 9)); + assert!(((*(*g.borrow()).value.as_ref().unwrap().borrow()) == 9)); return 0; } pub trait NonCopyableImpl { @@ -163,8 +185,8 @@ pub trait NonCopyableImpl { } impl NonCopyableImpl for Ptr { fn move_assign(&self, _a0: Ptr) -> Ptr { - ((*(*self).upgrade().deref()).value.as_pointer() as Ptr>>) - .write((*(*_a0.upgrade().deref()).value.borrow_mut()).take()); + ((field_ptr!((*self), value) as Ptr>>) as Ptr>>) + .write(_a0.with_mut(|__s: &mut NonCopyable| __s.value.take())); return (*self).clone(); } } diff --git a/tests/unit/out/unsafe/field_pointers.rs b/tests/unit/out/unsafe/field_pointers.rs new file mode 100644 index 0000000000..3dd8b7ccf6 --- /dev/null +++ b/tests/unit/out/unsafe/field_pointers.rs @@ -0,0 +1,150 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +#[repr(C)] +#[derive(Copy, Clone, VaArg)] +pub struct Inner { + pub a: i32, + pub name: [libc::c_char; 8], +} +impl Default for Inner { + fn default() -> Self { + Inner { + a: 0_i32, + name: [(0 as libc::c_char); 8], + } + } +} +#[repr(C)] +#[derive(Copy, Clone, VaArg, Default)] +pub struct Header { + pub tag: i32, + pub size: i16, +} +#[repr(C)] +#[derive(Clone, VaArg)] +pub struct Outer { + pub x: i32, + pub inner: Inner, + pub items: [Inner; 3], + pub v: Vec, + pub cursor: *mut i32, + pub buf: [i32; 4], +} +impl Outer { + pub unsafe fn next(&mut self) -> i32 { + return self.buf[(self.x.postfix_inc()) as usize]; + } + pub unsafe fn sum(&self) -> i32 { + return ((self.x) + (self.inner.a)); + } + pub unsafe fn push(&mut self, mut k: i32) { + self.v.push(((k) + (self.x))); + } +} +impl Default for Outer { + fn default() -> Self { + Outer { + x: 0_i32, + inner: ::default(), + items: [::default(); 3], + v: Default::default(), + cursor: std::ptr::null_mut(), + buf: [0_i32; 4], + } + } +} +pub unsafe fn set_0(mut p: *mut i32, mut value: i32) { + (*p) = value; +} +pub unsafe fn bump_1(mut o: *mut Outer) -> i32 { + (*o).x.postfix_inc(); + return 1; +} +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + let mut o: Outer = ::default(); + o.x = 1; + let mut px: *mut i32 = (&mut o.x as *mut i32); + (*px) = 2; + assert!(((o.x) == (2))); + let mut pa: *mut i32 = (&mut o.inner.a as *mut i32); + (unsafe { + let _p: *mut i32 = pa; + set_0(_p, 3) + }); + assert!(((o.inner.a) == (3))); + let mut pi: *mut i32 = (&mut o.items[(1) as usize].a as *mut i32); + (*pi) = 4; + assert!(((o.items[(1) as usize].a) == (4))); + assert!(((&mut o.items[(0) as usize].a as *mut i32) != (pi))); + let mut name: *mut libc::c_char = o.inner.name.as_mut_ptr(); + let mut i: i32 = 0; + 'loop_: while ((i) < (3)) { + (*name.offset((i) as isize)) = (((('a' as libc::c_char) as i32) + (i)) as libc::c_char); + i.prefix_inc(); + } + assert!(((libc::strlen((o.inner.name.as_mut_ptr()).cast_const())) == (3_usize))); + assert!( + ((name.offset((3) as isize)) == (&mut o.inner.name[(3) as usize] as *mut libc::c_char)) + ); + o.cursor = (&mut o.buf[(1) as usize] as *mut i32); + (*o.cursor) = 5; + (*o.cursor.postfix_inc()) += 1; + assert!( + ((o.buf[(1) as usize]) == (6)) && ((o.cursor) == (&mut o.buf[(2) as usize] as *mut i32)) + ); + o.x = 0; + let mut first: i32 = (unsafe { Outer::next(&mut o) }); + assert!(((first) == (0)) && ((o.x) == (1))); + o.x = ((o.inner.a) + (unsafe { bump_1((&mut o as *mut Outer)) })); + assert!(((o.x) == (4))); + (unsafe { + let _k: i32 = o.inner.a; + Outer::push(&mut o, _k) + }); + assert!(((o.v.len()) == (1_usize)) && ((o.v[(0_usize)]) == (7))); + assert!(((unsafe { Outer::sum(&o,) }) == (7))); + let mut y: i32 = 0; + { + if ::std::mem::size_of::() != 0 { + ::std::ptr::copy_nonoverlapping( + ((&mut o.items[(1) as usize].a as *mut i32) as *const ::libc::c_void), + ((&mut o.items[(2) as usize].a as *mut i32) as *mut ::libc::c_void), + ::std::mem::size_of::() as usize, + ) + } + ((&mut o.items[(2) as usize].a as *mut i32) as *mut ::libc::c_void) + }; + { + if ::std::mem::size_of::() != 0 { + ::std::ptr::copy_nonoverlapping( + ((&mut o.items[(2) as usize].a as *mut i32) as *const ::libc::c_void), + ((&mut y as *mut i32) as *mut ::libc::c_void), + ::std::mem::size_of::() as usize, + ) + } + ((&mut y as *mut i32) as *mut ::libc::c_void) + }; + assert!(((y) == (4))); + let mut bytes: [u8; 8] = [0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8]; + let mut view: *mut Header = (bytes.as_mut_ptr() as *mut Header); + (*view).tag = 16909060; + assert!(((bytes[(0) as usize] as i32) == (4)) || ((bytes[(3) as usize] as i32) == (4))); + (unsafe { set_0((&mut (*view).tag as *mut i32), 0) }); + assert!(((bytes[(0) as usize] as i32) == (0)) && ((bytes[(3) as usize] as i32) == (0))); + (*view).size = 1_i16; + assert!((((bytes[(4) as usize] as i32) + (bytes[(5) as usize] as i32)) == (1))); + return 0; +} +pub unsafe fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/unsafe/vector_subscript_direct.rs b/tests/unit/out/unsafe/vector_subscript_direct.rs new file mode 100644 index 0000000000..bf23597547 --- /dev/null +++ b/tests/unit/out/unsafe/vector_subscript_direct.rs @@ -0,0 +1,91 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +#[repr(C)] +#[derive(Copy, Clone, Default)] +pub struct Point { + pub x: i32, + pub y: i32, +} +impl Point { + pub unsafe fn sum(&self) -> i32 { + return ((self.x) + (self.y)); + } +} +#[repr(C)] +#[derive(Clone, Default)] +pub struct Holder { + pub values: Vec, + pub points: Vec, +} +pub unsafe fn push_and_index_0(v: *mut Vec) -> i32 { + (*v).push(42); + return (((*v).len() as i32) - (1)); +} +pub unsafe fn sum_ref_1(v: *const Vec) -> i32 { + let mut s: i32 = 0; + let mut i: usize = 0_usize; + 'loop_: while ((i) < ((*v).len())) { + s += (&(*v))[(i)]; + i.prefix_inc(); + } + return s; +} +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + let mut v: Vec = vec![1, 2, 3]; + v[(0_usize)] = 10; + assert!(((v[(0_usize)]) == (10))); + v[(1_usize)] += 5; + v[(2_usize)].postfix_inc(); + assert!(((v[(1_usize)]) == (7)) && ((v[(2_usize)]) == (4))); + v[(0_usize)] = v[(1_usize)]; + assert!(((v[(0_usize)]) == (7))); + v[(1_usize)] = 0; + assert!(((v[(v[(1_usize)] as usize)]) == (7))); + let mut i: i32 = 0; + v[(i.postfix_inc() as usize)] = 3; + assert!(((i) == (1)) && ((v[(0_usize)]) == (3))); + assert!(((v[((unsafe { push_and_index_0(&mut v,) }) as usize)]) == (42))); + v[((unsafe { push_and_index_0(&mut v) }) as usize)] = 5; + assert!(((v.len()) == (5_usize)) && ((v[(4_usize)]) == (5))); + let mut p: *mut i32 = (&mut v[(2_usize)] as *mut i32); + (*p) = 9; + assert!(((v[(2_usize)]) == (9))); + assert!(((unsafe { sum_ref_1(&v,) }) == (((((3) + (0)) + (9)) + (42)) + (5)))); + let mut h: Holder = ::default(); + { + let __a0 = 2_usize as usize; + h.values.resize_with(__a0, || ::default()) + }; + h.values[(1_usize)] = 6; + h.points.push(Point { x: 1, y: 2 }); + h.points[(0_usize)].y = 5; + assert!(((h.values[(1_usize)]) == (6))); + assert!(((unsafe { Point::sum(&h.points[(0_usize)],) }) == (6))); + let mut hp: *mut Holder = (&mut h as *mut Holder); + (&mut (*hp)).values[(0_usize)] = (((&mut (*hp)).values[(1_usize)]) + (1)); + (&mut (*hp)).points[(0_usize)].x = (&mut (*hp)).values[(0_usize)]; + assert!(((h.values[(0_usize)]) == (7)) && ((h.points[(0_usize)].x) == (7))); + let mut q: Point = h.points[(0_usize)]; + q.x = 0; + assert!(((h.points[(0_usize)].x) == (7))); + let mut grid: Vec> = vec![vec![0; 3_usize as usize]; 2_usize as usize]; + grid[(1_usize)][(2_usize)] = 8; + assert!(((grid[(1_usize)][(2_usize)]) == (8)) && ((grid[(0_usize)][(2_usize)]) == (0))); + let mut a: Vec = vec![4, 5, 6]; + a[(1_usize)] = ((a[(0_usize)]) + (a[(2_usize)])); + assert!(((a[(1_usize)]) == (10))); + return 0; +} +pub unsafe fn __cpp2rust_init_globals() {} diff --git a/tests/unit/reinterpret_cast_struct_to_struct.cpp b/tests/unit/reinterpret_cast_struct_to_struct.cpp index e01adc19b6..0940df5244 100644 --- a/tests/unit/reinterpret_cast_struct_to_struct.cpp +++ b/tests/unit/reinterpret_cast_struct_to_struct.cpp @@ -1,4 +1,3 @@ -// panic: refcount #include #include @@ -13,7 +12,6 @@ struct Pair { }; // Reinterpret between two structs with identical layout. -// Fails because custom structs don't implement ByteRepr (yet!). int main() { Point pt = {10, 20}; Pair *pair = reinterpret_cast(&pt); diff --git a/tests/unit/union_struct_dual_use.c b/tests/unit/union_struct_dual_use.c index 400f5c4ec2..a3ee8ba0c2 100644 --- a/tests/unit/union_struct_dual_use.c +++ b/tests/unit/union_struct_dual_use.c @@ -1,4 +1,3 @@ -// panic: refcount #include #include diff --git a/tests/unit/vector_subscript_direct.cpp b/tests/unit/vector_subscript_direct.cpp new file mode 100644 index 0000000000..20601121af --- /dev/null +++ b/tests/unit/vector_subscript_direct.cpp @@ -0,0 +1,84 @@ +// The unsafe model cannot express v[push_and_index(v)] without two borrows. +// no-compile: unsafe +#include +#include +#include + +struct Point { + int x; + int y; + int sum() const { return x + y; } +}; + +struct Holder { + std::vector values; + std::vector points; +}; + +// Grows the vector while an index into it is being computed. +int push_and_index(std::vector &v) { + v.push_back(42); + return static_cast(v.size()) - 1; +} + +int sum_ref(const std::vector &v) { + int s = 0; + for (std::size_t i = 0; i < v.size(); ++i) { + s += v[i]; + } + return s; +} + +int main() { + std::vector v = {1, 2, 3}; + v[0] = 10; + assert(v[0] == 10); + v[1] += 5; + v[2]++; + assert(v[1] == 7 && v[2] == 4); + v[0] = v[1]; + assert(v[0] == 7); + v[1] = 0; + assert(v[v[1]] == 7); + + int i = 0; + v[i++] = 3; + assert(i == 1 && v[0] == 3); + + assert(v[push_and_index(v)] == 42); + v[push_and_index(v)] = 5; + assert(v.size() == 5 && v[4] == 5); + + int *p = &v[2]; + *p = 9; + assert(v[2] == 9); + assert(sum_ref(v) == 3 + 0 + 9 + 42 + 5); + + Holder h; + h.values.resize(2); + h.values[1] = 6; + h.points.push_back({1, 2}); + h.points[0].y = 5; + assert(h.values[1] == 6); + assert(h.points[0].sum() == 6); + + Holder *hp = &h; + hp->values[0] = hp->values[1] + 1; + hp->points[0].x = hp->values[0]; + assert(h.values[0] == 7 && h.points[0].x == 7); + + Point q = h.points[0]; + q.x = 0; + assert(h.points[0].x == 7); + + std::vector> grid; + grid.push_back(std::vector(3, 0)); + grid.push_back(std::vector(3, 0)); + grid[1][2] = 8; + assert(grid[1][2] == 8 && grid[0][2] == 0); + + std::array a = {4, 5, 6}; + a[1] = a[0] + a[2]; + assert(a[1] == 10); + return 0; +} From e85e3d044562d7fdd677f25862a74dab4d37a13c Mon Sep 17 00:00:00 2001 From: Nuno Lopes Date: Wed, 30 Sep 2026 10:07:44 +0100 Subject: [PATCH 02/13] Keep Box fields boxed --- .../converter/models/converter_refcount.cpp | 134 +++++----- docs/src/codegen/types.md | 10 +- docs/src/codegen/types/boxing.md | 26 +- docs/src/codegen/types/pointers.md | 26 +- docs/src/codegen/types/traits.md | 30 +-- docs/src/runtime/rc.md | 59 ++--- docs/src/runtime/reinterpret.md | 22 +- libcc2rs/src/cstr.rs | 7 +- libcc2rs/src/field.rs | 235 +++++------------- libcc2rs/src/lib.rs | 5 +- libcc2rs/src/rc.rs | 104 +++----- libcc2rs/src/reinterpret.rs | 23 +- .../out/refcount/defaulted_move_cross_tu.rs | 36 +-- tests/unit/field_array_elem_ptr.cpp | 70 ++++++ tests/unit/out/refcount/array_const_init.rs | 39 +-- .../out/refcount/array_of_noncopy_struct.rs | 4 +- tests/unit/out/refcount/class_templates.rs | 12 +- tests/unit/out/refcount/clone_vs_move.rs | 41 ++- tests/unit/out/refcount/copy_assign.rs | 41 ++- tests/unit/out/refcount/copy_ctor.rs | 40 ++- .../unit/out/refcount/copy_move_defaulted.rs | 151 ++++++----- tests/unit/out/refcount/default.rs | 47 +++- tests/unit/out/refcount/default_in_statics.rs | 37 ++- tests/unit/out/refcount/destructor.rs | 27 +- .../unit/out/refcount/field_array_elem_ptr.rs | 200 +++++++++++++++ tests/unit/out/refcount/field_pointers.rs | 104 +++++--- .../refcount/foreach_disjoint_field_borrow.rs | 4 +- tests/unit/out/refcount/huffman.rs | 6 +- tests/unit/out/refcount/implicit_autoref.rs | 4 +- tests/unit/out/refcount/kruskal.rs | 9 +- .../refcount/libc_struct_without_default.rs | 8 +- tests/unit/out/refcount/offsetof.rs | 22 +- .../refcount/operator_member_pointer_free.rs | 28 ++- .../operator_member_pointer_member.rs | 32 ++- tests/unit/out/refcount/pointer_array.rs | 37 ++- tests/unit/out/refcount/push_emplace_back.rs | 10 +- tests/unit/out/refcount/random.rs | 129 ++++++---- tests/unit/out/refcount/rule_of_five.rs | 54 ++-- tests/unit/out/refcount/rule_of_three.rs | 39 +-- .../unit/out/refcount/typedef-anon-struct.rs | 4 +- .../out/refcount/union_addrof_external.rs | 20 +- .../unit/out/refcount/union_cross_arm_cast.rs | 39 ++- .../unit/out/refcount/union_memset_memcpy.rs | 38 ++- tests/unit/out/refcount/union_nested.rs | 22 +- tests/unit/out/refcount/unique_ptr.rs | 5 +- .../out/refcount/unique_ptr_const_deref.rs | 5 +- tests/unit/out/refcount/unique_ptr_nested.rs | 2 +- .../out/refcount/vector_subscript_direct.rs | 8 +- tests/unit/out/refcount/void_cast.rs | 6 +- tests/unit/out/unsafe/field_array_elem_ptr.rs | 107 ++++++++ 50 files changed, 1367 insertions(+), 801 deletions(-) create mode 100644 tests/unit/field_array_elem_ptr.cpp create mode 100644 tests/unit/out/refcount/field_array_elem_ptr.rs create mode 100644 tests/unit/out/unsafe/field_array_elem_ptr.rs diff --git a/cpp2rust/converter/models/converter_refcount.cpp b/cpp2rust/converter/models/converter_refcount.cpp index 8f48dcb694..b969608ded 100644 --- a/cpp2rust/converter/models/converter_refcount.cpp +++ b/cpp2rust/converter/models/converter_refcount.cpp @@ -89,23 +89,24 @@ static bool IsTrivialValue(const clang::Expr *expr) { // Whether expr accesses a field of a struct that is stored in the struct. static bool IsFieldAccess(const clang::Expr *expr) { auto *member = clang::dyn_cast(expr); - auto *field = - member ? clang::dyn_cast(member->getMemberDecl()) - : nullptr; + auto *field = member + ? clang::dyn_cast(member->getMemberDecl()) + : nullptr; return field && IsTranslatedStruct(field->getParent()) && !field->getType()->isReferenceType(); } // Whether the field holds a Value of its own, instead of being stored in the -// struct. Vectors are, so that pointers to their elements, and to the fields -// of those, point into the vector and not the struct. -static bool IsValueField(clang::ASTContext &ctx, - const clang::ValueDecl *decl) { +// struct. Arrays and vectors are, so that pointers to their elements, and to +// the fields of those, point into the array and not the struct. +static bool IsValueField(clang::ASTContext &ctx, const clang::ValueDecl *decl) { auto *field = clang::dyn_cast(decl); - return field && IsTranslatedStruct(field->getParent()) && - !field->getType()->isReferenceType() && - Mapper::Map(ctx, field->getType().getUnqualifiedType()) - .starts_with("Vec<"); + if (!field || !IsTranslatedStruct(field->getParent())) { + return false; + } + auto type = field->getType(); + return !type->isReferenceType() && + (type->isConstantArrayType() || IsBoxedType(ctx, type)); } // Whether evaluating expr reads a field of a struct, and hence borrows the @@ -596,15 +597,9 @@ std::string ConverterRefCount::GetShallowCopy(const clang::RecordDecl *decl, auto name = GetNamedDeclAsString(field); auto type = field->getType(); auto value = std::format("{}.{}", src, name); + // Value fields are shared. if (auto *record = type->getAsRecordDecl(); IsTranslatedStruct(record)) { value = GetShallowCopy(record, value); - } else if (auto *arr = ctx_.getAsConstantArrayType(type); - arr && IsTranslatedStruct( - arr->getElementType()->getAsRecordDecl())) { - auto elem = std::format("__e{}", src.size()); - value = std::format( - "{}.iter().map(|{}| {}).collect()", value, elem, - GetShallowCopy(arr->getElementType()->getAsRecordDecl(), elem)); } else { value += ".clone()"; } @@ -684,8 +679,14 @@ void ConverterRefCount::AddCloneTrait(const clang::RecordDecl *decl) { PushBrace init_brace(*this); for (auto *field : decl->fields()) { auto name = GetNamedDeclAsString(field); - StrCat(std::format("{}: {},", name, - GetDeepCopy(field->getType(), "self." + name))); + StrCat(std::format( + "{}: {},", name, + IsValueField(ctx_, field) + ? std::format( + "Rc::new(RefCell::new({}))", + GetDeepCopy(field->getType(), + std::format("(*self.{}.borrow())", name))) + : GetDeepCopy(field->getType(), "self." + name))); } return; } @@ -809,9 +810,12 @@ void ConverterRefCount::AddByteReprTrait(const clang::RecordDecl *decl) { for (auto *field : decl->fields()) { auto byte_off = layout.getFieldOffset(idx) / 8; auto byte_size = ctx_.getTypeSize(field->getType()) / 8; - StrCat(std::format("self.{}.to_bytes(&mut buf[{}..{}]);", - GetNamedDeclAsString(field), byte_off, - byte_off + byte_size)); + auto name = GetNamedDeclAsString(field); + StrCat(std::format("{}.to_bytes(&mut buf[{}..{}]);", + IsValueField(ctx_, field) + ? std::format("(*self.{}.borrow())", name) + : "self." + name, + byte_off, byte_off + byte_size)); ++idx; } } @@ -825,13 +829,14 @@ void ConverterRefCount::AddByteReprTrait(const clang::RecordDecl *decl) { for (auto *field : decl->fields()) { auto byte_off = layout.getFieldOffset(idx) / 8; auto byte_size = ctx_.getTypeSize(field->getType()) / 8; - PushConversionKind push(*this, IsValueField(ctx_, field) - ? ConversionKind::FullRefCount - : ConversionKind::Pointee); - StrCat(std::format("{}: <{}>::from_bytes(&buf[{}..{}]),", - GetNamedDeclAsString(field), - ToString(field->getType()), byte_off, - byte_off + byte_size)); + PushConversionKind push(*this, ConversionKind::Pointee); + auto value = std::format("<{}>::from_bytes(&buf[{}..{}])", + ToString(field->getType()), byte_off, + byte_off + byte_size); + if (IsValueField(ctx_, field)) { + value = std::format("Rc::new(RefCell::new({}))", value); + } + StrCat(std::format("{}: {},", GetNamedDeclAsString(field), value)); ++idx; } } @@ -2045,8 +2050,7 @@ bool ConverterRefCount::VisitMemberExpr(clang::MemberExpr *expr) { std::string ConverterRefCount::ConvertRecordPlace(clang::MemberExpr *expr, bool mut) { auto *base = expr->getBase(); - if (clang::isa(base->IgnoreCasts()) && - !ThisIsRustPtr()) { + if (clang::isa(base->IgnoreCasts()) && !ThisIsRustPtr()) { return std::string(keyword::kSelfValue); } if (expr->isArrow()) { @@ -2084,11 +2088,12 @@ static bool IsCloneSource(const clang::Expr *expr) { } } auto *ref = clang::dyn_cast(expr); - auto *param = ref ? clang::dyn_cast(ref->getDecl()) - : nullptr; - auto *ctor = param ? clang::dyn_cast( - param->getDeclContext()) - : nullptr; + auto *param = + ref ? clang::dyn_cast(ref->getDecl()) : nullptr; + auto *ctor = + param + ? clang::dyn_cast(param->getDeclContext()) + : nullptr; return ctor && ctor->isCopyConstructor() && ctor->isDefaulted() && param->getName().empty(); } @@ -2196,8 +2201,7 @@ ConverterRefCount::FindStructPtr(clang::Expr *place, bool mut) { } } -std::optional -ConverterRefCount::ConvertInStructPtr( +std::optional ConverterRefCount::ConvertInStructPtr( clang::Expr *place, bool mut, llvm::function_ref convert) { auto sp = FindStructPtr(place, mut); if (!sp || sp->nested) { @@ -2232,21 +2236,8 @@ std::string ConverterRefCount::ConvertFieldPointer(clang::MemberExpr *expr) { } else { base_ptr = ConvertPointer(base); } - auto str = std::format("field_ptr!({}, {})", base_ptr, - GetNamedDeclAsString(expr->getMemberDecl())); - - // Pointers to arrays and containers either point to the whole object or - // to its elements. The type is inferred from the context, like for - // `as_pointer()`, unless we know which one is wanted. - if (isObject()) { - auto field_type = expr->getMemberDecl()->getType(); - if (WantsElementPtr()) { - return std::format("({} as {})", str, ConvertPtrType(field_type)); - } - PushConversionKind push(*this, ConversionKind::Pointee); - return std::format("({} as Ptr<{}>)", str, ToString(field_type)); - } - return str; + return std::format("field_ptr!({}, {})", base_ptr, + GetNamedDeclAsString(expr->getMemberDecl())); } void ConverterRefCount::ConvertFieldAccess(clang::MemberExpr *expr) { @@ -2256,8 +2247,8 @@ void ConverterRefCount::ConvertFieldAccess(clang::MemberExpr *expr) { if (field_type->isReferenceType()) { // The field holds a pointer to the referenced object. - auto str = std::format("{}.{}", ConvertRecordPlace(expr, /*mut=*/false), - name); + auto str = + std::format("{}.{}", ConvertRecordPlace(expr, /*mut=*/false), name); if (isAddrOf()) { StrCat(str); computed_expr_type_ = ComputedExprType::Pointer; @@ -2274,9 +2265,9 @@ void ConverterRefCount::ConvertFieldAccess(clang::MemberExpr *expr) { auto sp = FindStructPtr(expr); if (!sp || sp->nested) { auto *object = expr->getBase()->IgnoreParenImpCasts(); - bool borrows = !field_places_.contains(expr) && !IsCloneSource(object) && - !(clang::isa(object) && - !ThisIsRustPtr()); + bool borrows = + !field_places_.contains(expr) && !IsCloneSource(object) && + !(clang::isa(object) && !ThisIsRustPtr()); PushBrace block(*this, borrows && isAddrOf()); ConvertFieldPlace(expr, ConvertValueField(expr)); return; @@ -2344,8 +2335,8 @@ void ConverterRefCount::ConvertFieldPlace(clang::MemberExpr *expr, computed_expr_type_ = ComputedExprType::FreshPointer; return; } - StrCat(std::format("(*{}.{}())", place, - isRValue() ? "borrow" : "borrow_mut")); + StrCat( + std::format("(*{}.{}())", place, isRValue() ? "borrow" : "borrow_mut")); SetValueFreshness(expr->getType()); } @@ -3038,9 +3029,8 @@ bool ConverterRefCount::ConvertCXXOperatorCallExpr( break; case clang::OverloadedOperatorKind::OO_Subscript: { - if (ConvertWithHoistedIndex(expr, expr->getArg(0), expr->getArg(1), [&] { - ConvertCXXOperatorCallExpr(expr); - })) { + if (ConvertWithHoistedIndex(expr, expr->getArg(0), expr->getArg(1), + [&] { ConvertCXXOperatorCallExpr(expr); })) { break; } @@ -3423,8 +3413,7 @@ bool ConverterRefCount::IsReferenceType(const clang::Expr *expr) const { } // Collects the ways placeholder `n` is accessed in `fragments`. -static void -CollectPlaceholderAccesses( +static void CollectPlaceholderAccesses( const std::vector &fragments, unsigned n, std::vector &accesses) { for (const auto &frag : fragments) { @@ -3487,7 +3476,7 @@ std::optional ConverterRefCount::ConvertOnFieldReceiver( auto sp = receiver_ph.access != TranslationRule::Access::kMove && !receiver->getType()->isPointerType() && !RuleRegistry::ParamIsPointer(ctx_, GetCalleeOrExpr(expr), - receiver_ph.n) + receiver_ph.n) ? FindStructPtr(receiver, receiver_ph.access != TranslationRule::Access::kBorrow) : std::nullopt; @@ -3823,14 +3812,15 @@ ConverterRefCount::DestroyMembers(const clang::CXXRecordDecl *decl) { auto *elem = field->getType()->getBaseElementTypeUnsafe()->getAsCXXRecordDecl(); assert(elem); + // The array is a Value of its own. out += std::format( - "{{ let __p: Ptr<{3}> = field_ptr!(self, {0}); for __i in " - "0..__p.len() {{ {2}::{1}(&__p.offset(__i as isize)); }} }}\n", + "{{ let __p: Ptr<{3}> = self.with(|__s| __s.{0}.as_pointer()); for " + "__i in 0..__p.len() {{ {2}::{1}(&__p.offset(__i as isize)); }} }}\n", name, kDestructorName, TraitName(elem), GetRecordName(elem)); } else { - out += std::format( - "{2}::{1}(&field_ptr!(self, {0}));\n", name, kDestructorName, - TraitName(field->getType()->getAsCXXRecordDecl())); + out += std::format("{2}::{1}(&field_ptr!(self, {0}));\n", name, + kDestructorName, + TraitName(field->getType()->getAsCXXRecordDecl())); } } return out; diff --git a/docs/src/codegen/types.md b/docs/src/codegen/types.md index 58205b7526..9611499020 100644 --- a/docs/src/codegen/types.md +++ b/docs/src/codegen/types.md @@ -38,7 +38,7 @@ pub struct Item { #[offset(0)] pub id: i32, #[offset(4)] - pub name: Box<[u8]>, + pub name: Value>, #[offset(16)] pub refs: Value>, } @@ -49,8 +49,8 @@ pub fn count_0(item: Item) -> i32 { ``` Fields are stored inline in their struct, so a whole struct lives in a single -`Value`, like the elements of an array; only vectors are `Value`s of their own -(see [Boxing](types/boxing.md)). A pointer to a field records the allocation of -the struct plus the byte offset of the field in it, which the `#[offset(N)]` -attributes give (see +`Value`, like the elements of an array; only arrays and vectors are `Value`s of +their own (see [Boxing](types/boxing.md)). A pointer to a field records the +allocation of the struct plus the byte offset of the field in it, which the +`#[offset(N)]` attributes give (see [Pointers to fields](../runtime/rc.md#pointers-to-fields)). diff --git a/docs/src/codegen/types/boxing.md b/docs/src/codegen/types/boxing.md index 4dcdc4707b..38e1629e27 100644 --- a/docs/src/codegen/types/boxing.md +++ b/docs/src/codegen/types/boxing.md @@ -12,9 +12,9 @@ that owns the type pushes one before printing it: - `FullRefCount`: pushed by variable declarations; `Convert(QualType)` wraps the result in `Value<...>`. -- `Pointee`: pushed by field declarations; the bare type is printed, but arrays - are boxed slices, as in a `Value`. Fields whose type maps to a `Vec` - (`std::vector`, `std::string`) push `FullRefCount` instead (see below). +- `Pointee`: pushed by field declarations; the bare type is printed. Fields that + are arrays, or whose type maps to a `Vec` or a `Box` (`std::vector`, + `std::string`), push `FullRefCount` instead (see below). - `Unboxed`: pushed by parameter lists, return types, and record names; the bare type is printed. - `Ptr`: pushed by a pointer type for its pointee; also printed bare. @@ -25,8 +25,7 @@ The result by position: | ------------------------------------------- | ------------ | ------------- | -------------------- | | local variable, global | `Value` | `Value` | `Value>` | | function parameter, return type | `i32` | `Item` | decays to `Ptr` | -| struct field | `i32` | `Item` | `Box<[i32]>` | -| struct field of type `std::vector` | | | `Value>` | +| struct field | `i32` | `Item` | `Value>` | | pointee of `Ptr`, element of a container | `i32` | `Item` | `Box<[i32]>` | Parameters arrive unboxed and are re-boxed by the function preamble; return @@ -58,10 +57,11 @@ the outer `Value<...>` of the declaration is added. Struct fields are stored inline, so that a whole struct is a single allocation, and a pointer to a field records the struct's allocation and the field's byte -offset (see [Pointers](pointers.md)). Vectors are the exception: a vector field -is a `Value>` of its own. The elements of a vector live outside the -struct, and their number changes, so a pointer to an element, or to a field of -an element, has the vector as its allocation instead of the struct: +offset (see [Pointers](pointers.md)). Arrays and vectors are the exception: an +array field is a `Value>` of its own, and a vector field a +`Value>`. A pointer to an element, or to a field of an element, then has +the array or the vector as its allocation instead of the struct, and pointer +arithmetic moves between elements as for any other array: ```cpp struct Holder { std::vector points; int n; }; @@ -78,7 +78,7 @@ pub struct Holder { (*(*h.borrow()).points.borrow_mut())[(0_usize) as usize].y = 5; ``` -A vector field is accessed like a local vector, through its own `borrow()` or -`borrow_mut()`, and the struct is only borrowed immutably to reach it. As -`Value` is shared on `clone()`, structs with vector fields implement `Clone` by -copying the vectors, instead of deriving it. +An array or vector field is accessed like a local one, through its own +`borrow()` or `borrow_mut()`, and the struct is only borrowed immutably to reach +it. As `Value` is shared on `clone()`, structs with such fields implement +`Clone` by copying the arrays and vectors, instead of deriving it. diff --git a/docs/src/codegen/types/pointers.md b/docs/src/codegen/types/pointers.md index 91cd4ae0f2..9bb12fb518 100644 --- a/docs/src/codegen/types/pointers.md +++ b/docs/src/codegen/types/pointers.md @@ -47,9 +47,9 @@ pointer type is to `const`. Globals use `&raw mut x` so no reference to the an element is `&mut arr[i] as *mut T`. Refcount model: `&x` becomes `x.as_pointer()`, which produces a `Ptr` holding a -weak reference to the variable's `Value`. `&s.field` is -`field_ptr!(s, field)`, and `&p->field` is `field_ptr!(p, field)`: a pointer to -the field of the struct, made from the `Value` or the `Ptr` of the struct (see +weak reference to the variable's `Value`. `&s.field` is `field_ptr!(s, field)`, +and `&p->field` is `field_ptr!(p, field)`: a pointer to the field of the struct, +made from the `Value` or the `Ptr` of the struct (see [Pointers to fields](../../runtime/rc.md#pointers-to-fields)). An array decays with `arr.as_pointer() as Ptr`, a `Ptr` to element 0 of the whole array, and `&arr[i]` is that pointer offset by `i`. @@ -87,18 +87,18 @@ of the dereference: - An rvalue use copies the value out. A scalar or pointer pointee is `p.read()`, and so is a whole record, `*p`. A field of a record pointee is read in a closure that borrows the record for its duration: `p->x` is - `p.with(|__s: &S| __s.x)`, and `p->a[i].y` is - `p.with(|__s: &S| __s.a[i].y)`. A method called on a field runs in the - closure too, together with the rest of its rule: `p->v.size()` is - `p.with(|__s: &S| (*__s.v.borrow()).len())`. + `p.with(|__s: &S| __s.x)`, and `p->a[i].y` is `p.with(|__s: &S| __s.a[i].y)`. + A method called on a field runs in the closure too, together with the rest of + its rule: `p->v.size()` is `p.with(|__s: &S| (*__s.v.borrow()).len())`. - A write to a field of a record pointee is also done in a closure, which borrows the record mutably: `p->x = v` is `p.with_mut(|__s: &mut S| __s.x = v)`. Operands that may access memory are evaluated before the closure (`let __rhs = ...;`), so that they don't find the record borrowed. Fields that are [`Value`s of their own](boxing.md) only need - the record borrowed immutably, `p.with(|__s: &S| (*__s.v.borrow_mut()).push(1))`. - The same closures are used for references to records, `r.x`, and for elements - of arrays of records, `a[i].x`. + the record borrowed immutably, + `p.with(|__s: &S| (*__s.v.borrow_mut()).push(1))`. The same closures are used + for references to records, `r.x`, and for elements of arrays of records, + `a[i].x`. - An address-of use prints `p` itself. - An lvalue use prints nothing at once. The converter records the pointer expression as a [pending dereference](../expressions/pending-deref.md), and @@ -110,9 +110,9 @@ of the dereference: `with` and `with_mut` work on any pointer, including a [reinterpreted](../../runtime/reinterpret.md) one, whose pointee is decoded from -the bytes of the original allocation and, for `with_mut`, encoded back into -them before the closure returns. A write through a reinterpreted pointer is -hence visible right away through every other pointer to the same bytes. +the bytes of the original allocation and, for `with_mut`, encoded back into them +before the closure returns. A write through a reinterpreted pointer is hence +visible right away through every other pointer to the same bytes. ## Arithmetic and comparison diff --git a/docs/src/codegen/types/traits.md b/docs/src/codegen/types/traits.md index 191b518bb1..734cf67d73 100644 --- a/docs/src/codegen/types/traits.md +++ b/docs/src/codegen/types/traits.md @@ -6,15 +6,15 @@ derived, some are written out; which is which depends on the model. Enums derive `Copy, Clone` in the unsafe model regardless of their fields; the table below is for structs. -| Trait | Unsafe model | Refcount model | -| -------------------------------------- | --------------------------------------------------------------------- | --------------------------------------------------- | -| `Copy` | derived when every field is copyable | never | -| `Clone` | derived | derived when member-wise, else hand-written | -| `Default` | derived when possible, else hand-written | same | -| `Drop` | not emitted ([#310](https://github.com/Cpp2Rust/cpp2rust/issues/310)) | hand-written from a user destructor with a body | -| `Ord`, `PartialOrd`, `PartialEq`, `Eq` | hand-written from `operator<` | same | -| `ByteRepr` | not needed | hand-written for every record and enum | -| `Record` | not needed | derived for every struct | +| Trait | Unsafe model | Refcount model | +| -------------------------------------- | --------------------------------------------------------------------- | ----------------------------------------------- | +| `Copy` | derived when every field is copyable | never | +| `Clone` | derived | derived when member-wise, else hand-written | +| `Default` | derived when possible, else hand-written | same | +| `Drop` | not emitted ([#310](https://github.com/Cpp2Rust/cpp2rust/issues/310)) | hand-written from a user destructor with a body | +| `Ord`, `PartialOrd`, `PartialEq`, `Eq` | hand-written from `operator<` | same | +| `ByteRepr` | not needed | hand-written for every record and enum | +| `Record` | not needed | derived for every struct | ## Copy and Clone @@ -27,14 +27,14 @@ classes with an implicit or defaulted copy constructor, which copy each field with its own `clone`, i.e., its C++ copy constructor. The exceptions are fields that are or nest a `Value`, such as `std::vector` (`Value>`, see [Boxing](boxing.md)) or `std::vector>` -(`Value>>>`), whose derived `clone` would share the -`Value`s instead of copying them; the generated `Clone` then translates the -implicit copy constructor, which copies them deeply. +(`Value>>>`), whose derived `clone` would share the `Value`s +instead of copying them; the generated `Clone` then translates the implicit copy +constructor, which copies them deeply. A user-defined copy constructor takes a `Ptr` to the source, while `clone` only -has `&self`. `clone` passes it a pointer to a shallow copy of `self`, built field -by field without running any copy constructor, so that the source is copied -exactly once. +has `&self`. `clone` passes it a pointer to a shallow copy of `self`, built +field by field without running any copy constructor, so that the source is +copied exactly once. This is what gives struct assignment and pass-by-value C++'s member-by-member copy. diff --git a/docs/src/runtime/rc.md b/docs/src/runtime/rc.md index 88cac9bc7a..4a9b3f32ec 100644 --- a/docs/src/runtime/rc.md +++ b/docs/src/runtime/rc.md @@ -76,10 +76,10 @@ comparison does. ## Pointers to fields Fields are stored inline in their struct, so a struct and all of its fields -share one allocation and one `RefCell`. A pointer to a field is to a struct -what a pointer to an element is to an array: it records a weak reference to the -allocation that holds the struct, its root, together with the byte offset of -the field in it. `field_ptr!` creates one: +share one allocation and one `RefCell`. A pointer to a field is to a struct what +a pointer to an element is to an array: it records a weak reference to the +allocation that holds the struct, its root, together with the byte offset of the +field in it. `field_ptr!` creates one: ```c struct point { int x; int y; }; @@ -94,8 +94,8 @@ let y: Value> = Rc::new(RefCell::new(field_ptr!(p, y))); `field_ptr!(p, y)` works on a `Value` or a `Ptr` to a struct. The offsets are those of the C layout of the struct, which the code generator gets from Clang -and writes on the fields as `#[offset(N)]` attributes; `#[derive(Record)]` -turns them into the implementation of the `Record` trait: +and writes on the fields as `#[offset(N)]` attributes; `#[derive(Record)]` turns +them into the implementation of the `Record` trait: ```rust #[derive(Clone, Record, Default)] @@ -107,23 +107,24 @@ pub struct point { } ``` -A field pointer is thus the same size as any other pointer, and creating one -allocates nothing. Pointers to fields of fields, and to fields of array +Creating a field pointer allocates nothing. The byte offset of the field is kept +in the pointer's offset, which for a field pointer counts bytes, as for a +reinterpreted pointer. Pointers to fields of fields, and to fields of array elements, add up the offsets, and keep the allocation of the outermost struct or -array as their root. A pointer to an array field can either point to the whole -field (`Ptr>`) or index into it (`Ptr`); the expected pointer type -picks which, as for `as_pointer`. +array as their root. -An access through a field pointer borrows the root, and finds the field from -its offset and its type with `Record::locate`, which the derive generates as a +An access through a field pointer borrows the root, and finds the field from its +offset and its type with `Record::locate`, which the derive generates as a comparison of the offset against those of the fields, recursing into nested -structs and arrays of structs. A field is identified by its type too, as a -struct and its first field share an offset. An offset that doesn't find a field -of the pointer's type panics with `ub: invalid field pointer`. +structs. A field is identified by its type too, as a struct and its first field +share an offset. An offset that doesn't find a field of the pointer's type +panics with `ub: invalid field pointer`. -Vectors, whose elements are not part of the struct's layout, are not stored -inline: a `std::vector` field is a `Value>` of its own, and pointers to -its elements, and to the fields of those, have the vector as their root. +Arrays and vectors are not stored inline: an array field is a `Value>` +of its own, and a `std::vector` field a `Value>`. Pointers to their +elements are ordinary array pointers, and pointers to the fields of those have +the array or vector as their root. A field pointer hence always points to a +single object, which is why it needs no element index. Reading a field through a pointer borrows the struct only for the duration of a closure, e.g., `p.with(|s: &point| s.y)`, so that the borrow ends before the @@ -135,8 +136,8 @@ of the field alone, as if it were its own allocation. Since all fields share the borrow of their struct, an expression must not write to a field while another field of the same struct is borrowed. The code -generator scopes the borrows of field reads so that they end before any write -in the same statement. +generator scopes the borrows of field reads so that they end before any write in +the same statement. ## The heap @@ -246,18 +247,18 @@ pub enum StrongPtr { StackSingle(Rc>), Vec { rc: Rc>>, offset: usize }, StackArray { rc: Rc>>, offset: usize }, - Field { root: Rc, field: u32, offset: usize }, + Field { root: Rc, offset: usize }, Reinterpreted { alloc: OriginalAlloc, byte_offset: usize, cell: RefCell> }, } ``` -`deref` returns a `Ref<'_, T>` to the pointee, and `deref_mut` a `RefMut<'_, T>`. -The `Ref` borrows the `StrongPtr`, so the borrow of the `RefCell` lasts as long -as the strong pointer does: in `p.upgrade().deref().field`, the temporary -`StrongPtr` lives until the end of the enclosing statement, and so does the -borrow. The code generator prefers the `with` and `with_mut` closures, and -upgrades only where the result of an access must borrow the pointee beyond a -closure. +`deref` returns a `Ref<'_, T>` to the pointee, and `deref_mut` a +`RefMut<'_, T>`. The `Ref` borrows the `StrongPtr`, so the borrow of the +`RefCell` lasts as long as the strong pointer does: in +`p.upgrade().deref().field`, the temporary `StrongPtr` lives until the end of +the enclosing statement, and so does the borrow. The code generator prefers the +`with` and `with_mut` closures, and upgrades only where the result of an access +must borrow the pointee beyond a closure. For the `Reinterpreted` variant there is no value to reference, only bytes in another allocation. `deref` reads those bytes into a local cell and hands out a diff --git a/docs/src/runtime/reinterpret.md b/docs/src/runtime/reinterpret.md index 5c93016ff0..efd176b24f 100644 --- a/docs/src/runtime/reinterpret.md +++ b/docs/src/runtime/reinterpret.md @@ -19,8 +19,7 @@ pub trait ByteRepr: 'static { } ``` -The code generator emits the `ByteRepr` implementation of a C struct next to -it: +The code generator emits the `ByteRepr` implementation of a C struct next to it: ```c struct header { @@ -112,9 +111,9 @@ reference to the original allocation, the size of the target type, and a reference to a stateless table of the byte-level operations for the original's storage type (a single value, a `Vec`, a boxed slice, or a field of a struct, whose bytes are found through the struct's allocation as for a -[field pointer](./rc.md#pointers-to-fields)). Copying or offsetting -the resulting `Ptr` only bumps a reference count, so a loop over a `malloc`ed -array pays for the cast once, not per access. +[field pointer](./rc.md#pointers-to-fields)). Copying or offsetting the +resulting `Ptr` only bumps a reference count, so a loop over a `malloc`ed array +pays for the cast once, not per access. Accessing memory through a view does not allocate: the bytes of the accessed element are staged in a stack buffer (a heap buffer is used only for accesses @@ -127,13 +126,12 @@ directly instead of serializing the elements one by one. Reading a struct through a reinterpreted pointer builds a fresh struct with `from_bytes`, which exists only for the duration of the `with` closure, or as long as the `StrongPtr` that holds it. Writes to its fields go through -`with_mut`, which encodes the struct back into the original allocation before -it returns, so they are visible right away through any other pointer; a pointer -to one of its fields is a reinterpreted pointer into the original allocation. -However, -[union accessors](../codegen/unions.md) return pointers to the union's storage; -on a reinterpreted union that storage is the temporary, so the returned pointer -dangles. This is set to be fixed in the near future. +`with_mut`, which encodes the struct back into the original allocation before it +returns, so they are visible right away through any other pointer; a pointer to +one of its fields is a reinterpreted pointer into the original allocation. +However, [union accessors](../codegen/unions.md) return pointers to the union's +storage; on a reinterpreted union that storage is the temporary, so the returned +pointer dangles. This is set to be fixed in the near future. ## AnyPtr casts diff --git a/libcc2rs/src/cstr.rs b/libcc2rs/src/cstr.rs index f527749fdb..f4fa3b5c2d 100644 --- a/libcc2rs/src/cstr.rs +++ b/libcc2rs/src/cstr.rs @@ -144,11 +144,10 @@ impl Ptr { let b = rc.borrow(); f(until_nul(&b[self.offset..])) } - PtrKind::Field(root, field) => { + PtrKind::Field(root) => { let root = crate::field::upgrade(root); - f(until_nul( - &Ptr::::borrow_field(&*root, *field)[self.offset..], - )) + let b = Ptr::::borrow_field(&*root, self.offset); + f(until_nul(std::slice::from_ref(&*b))) } PtrKind::Reinterpreted(_) => f(&self.to_c_string_iterator().collect::>()), } diff --git a/libcc2rs/src/field.rs b/libcc2rs/src/field.rs index 8feeb96e5d..b9aae8152e 100644 --- a/libcc2rs/src/field.rs +++ b/libcc2rs/src/field.rs @@ -8,6 +8,10 @@ // holds the struct and the byte offset of the field in it (in the C layout), // similarly to how a pointer to an array element records the array and the // index of the element. The field is found from the offset when accessed. +// +// Arrays and vectors are not stored inline, but in Values of their own, so +// that pointers to their elements point into those Values. A field pointer +// hence always points to a single object. use std::any::{Any, TypeId}; use std::cell::{Cell, Ref, RefCell, RefMut}; @@ -25,40 +29,11 @@ pub trait Record: ByteRepr { const OFFSETS: Self::Offsets; // The object of type `ty` at byte `offset` of the struct: the struct - // itself, a field, a field of a field, and so on. For a field that is an - // array of `ty`, the array. + // itself, a field, a field of a field, and so on. fn locate(&self, offset: usize, ty: TypeId) -> Option<&dyn Any>; fn locate_mut(&mut self, offset: usize, ty: TypeId) -> Option<&mut dyn Any>; } -// The elements that a pointer of type T sees in an object: the object itself, -// or the elements of an array. -pub trait Elems {} -impl Elems for T {} -impl Elems for Box<[T]> {} - -#[inline] -pub(crate) fn elems(obj: &dyn Any) -> &[T] { - if let Some(v) = obj.downcast_ref::() { - std::slice::from_ref(v) - } else if let Some(a) = obj.downcast_ref::>() { - a - } else { - invalid_field() - } -} - -#[inline] -pub(crate) fn elems_mut(obj: &mut dyn Any) -> &mut [T] { - if obj.is::() { - std::slice::from_mut(obj.downcast_mut::().unwrap()) - } else if let Some(a) = obj.downcast_mut::>() { - a - } else { - invalid_field() - } -} - #[cold] #[inline(never)] #[track_caller] @@ -140,21 +115,20 @@ impl Root for RefCell> { // Creates pointers to the fields of the struct S pointed to by self. Use it // through the field_ptr! macro. pub trait FieldPtr { - // A pointer to the field at `offset(S::OFFSETS)`. The field has type U, - // as given by the (uncalled) `field`, and T is either U or, if U is an - // array, the type of its elements. - fn field_ptr>( + // A pointer to the field at `offset(S::OFFSETS)`, whose type is given by + // the (uncalled) `field`. + fn field_ptr( &self, - field: fn(&S) -> &U, + field: fn(&S) -> &T, offset: fn(&S::Offsets) -> usize, ) -> Ptr; } impl FieldPtr for Ptr { #[inline] - fn field_ptr>( + fn field_ptr( &self, - _field: fn(&S) -> &U, + _field: fn(&S) -> &T, offset: fn(&S::Offsets) -> usize, ) -> Ptr { let offset = offset(&S::OFFSETS); @@ -165,24 +139,24 @@ impl FieldPtr for Ptr { PtrKind::StackSingle(w) | PtrKind::HeapSingle(w) => (w.clone(), start()), PtrKind::StackArray(w) | PtrKind::HeapArray(w) => (w.clone(), start()), PtrKind::StackVec(w) | PtrKind::HeapVec(w) => (w.clone(), start()), - PtrKind::Field(root, field) => (root.clone(), *field as usize + start()), + PtrKind::Field(root) => (root.clone(), self.offset), PtrKind::Reinterpreted(_) => { let (alloc, byte_offset) = self.original_alloc().unwrap(); return Ptr::from_original_alloc(alloc, byte_offset.wrapping_add(offset)); } }; Ptr { - offset: 0, - kind: PtrKind::Field(root, (start + offset) as u32), + offset: start.wrapping_add(offset), + kind: PtrKind::Field(root), } } } impl FieldPtr for Value { #[inline] - fn field_ptr>( + fn field_ptr( &self, - field: fn(&S) -> &U, + field: fn(&S) -> &T, offset: fn(&S::Offsets) -> usize, ) -> Ptr { self.as_pointer().field_ptr(field, offset) @@ -201,8 +175,8 @@ macro_rules! field_ptr { // Finds the object at a byte offset of a field for Record::locate. The // implementation is chosen based on the type of the field, by autoref-based -// specialization: `(&&&&Locate(&field)).locate(...)` picks the first of the -// traits below that is implemented for the field's type. +// specialization: `(&&Locate(&field)).locate(...)` picks LocateRecord if the +// field is a struct, and LocateLeaf otherwise. #[doc(hidden)] pub struct Locate<'a, T>(pub &'a T); @@ -225,14 +199,6 @@ pub trait LocateRecord<'a> { fn locate(self, offset: usize, ty: TypeId) -> Option<&'a dyn Any>; } #[doc(hidden)] -pub trait LocateRecords<'a> { - fn locate(self, offset: usize, ty: TypeId) -> Option<&'a dyn Any>; -} -#[doc(hidden)] -pub trait LocateArray<'a> { - fn locate(self, offset: usize, ty: TypeId) -> Option<&'a dyn Any>; -} -#[doc(hidden)] pub trait LocateLeaf<'a> { fn locate(self, offset: usize, ty: TypeId) -> Option<&'a dyn Any>; } @@ -241,77 +207,25 @@ pub trait LocateRecordMut<'a> { fn locate(self, offset: usize, ty: TypeId) -> Option<&'a mut dyn Any>; } #[doc(hidden)] -pub trait LocateRecordsMut<'a> { - fn locate(self, offset: usize, ty: TypeId) -> Option<&'a mut dyn Any>; -} -#[doc(hidden)] -pub trait LocateArrayMut<'a> { - fn locate(self, offset: usize, ty: TypeId) -> Option<&'a mut dyn Any>; -} -#[doc(hidden)] pub trait LocateLeafMut<'a> { fn locate(self, offset: usize, ty: TypeId) -> Option<&'a mut dyn Any>; } // A struct. -impl<'a, R: Record> LocateRecord<'a> for &&&&Locate<'a, R> { +impl<'a, R: Record> LocateRecord<'a> for &&Locate<'a, R> { #[inline] fn locate(self, offset: usize, ty: TypeId) -> Option<&'a dyn Any> { self.0.locate(offset, ty) } } -impl<'a, R: Record> LocateRecordMut<'a> for &&&&LocateMut<'a, R> { +impl<'a, R: Record> LocateRecordMut<'a> for &&LocateMut<'a, R> { #[inline] fn locate(self, offset: usize, ty: TypeId) -> Option<&'a mut dyn Any> { self.take().locate_mut(offset, ty) } } -// An array of structs. -impl<'a, R: Record> LocateRecords<'a> for &&&Locate<'a, Box<[R]>> { - #[inline] - fn locate(self, offset: usize, ty: TypeId) -> Option<&'a dyn Any> { - // The Box itself is returned, as elems() looks for a Box<[R]>. - let array = self.0; - if offset == 0 && (ty == TypeId::of::() || ty == TypeId::of::>()) { - return Some(array as &dyn Any); - } - let (idx, offset) = split::(offset); - array.get(idx)?.locate(offset, ty) - } -} - -impl<'a, R: Record> LocateRecordsMut<'a> for &&&LocateMut<'a, Box<[R]>> { - #[inline] - fn locate(self, offset: usize, ty: TypeId) -> Option<&'a mut dyn Any> { - let array: &'a mut Box<[R]> = self.take(); - if offset == 0 && (ty == TypeId::of::() || ty == TypeId::of::>()) { - return Some(array); - } - let (idx, offset) = split::(offset); - array.get_mut(idx)?.locate_mut(offset, ty) - } -} - -// An array of anything else, which is where it ends. -impl<'a, T: 'static> LocateArray<'a> for &&Locate<'a, Box<[T]>> { - #[inline] - fn locate(self, offset: usize, ty: TypeId) -> Option<&'a dyn Any> { - (offset == 0 && (ty == TypeId::of::() || ty == TypeId::of::>())) - .then_some(self.0 as &dyn Any) - } -} - -impl<'a, T: 'static> LocateArrayMut<'a> for &&LocateMut<'a, Box<[T]>> { - #[inline] - fn locate(self, offset: usize, ty: TypeId) -> Option<&'a mut dyn Any> { - let array: &'a mut Box<[T]> = self.take(); - (offset == 0 && (ty == TypeId::of::() || ty == TypeId::of::>())) - .then_some(array as &mut dyn Any) - } -} - // Anything else. impl<'a, T: 'static> LocateLeaf<'a> for &Locate<'a, T> { #[inline] @@ -333,8 +247,8 @@ impl<'a, T: 'static> LocateLeafMut<'a> for &LocateMut<'a, T> { macro_rules! __locate_field { ($field:expr, $offset:expr, $ty:expr) => {{ #[allow(unused_imports)] - use $crate::__field::{LocateArray, LocateLeaf, LocateRecord, LocateRecords}; - (&&&&$crate::__field::Locate($field)).locate($offset, $ty) + use $crate::__field::{LocateLeaf, LocateRecord}; + (&&$crate::__field::Locate($field)).locate($offset, $ty) }}; } @@ -343,33 +257,25 @@ macro_rules! __locate_field { macro_rules! __locate_field_mut { ($field:expr, $offset:expr, $ty:expr) => {{ #[allow(unused_imports)] - use $crate::__field::{LocateArrayMut, LocateLeafMut, LocateRecordMut, LocateRecordsMut}; - (&&&&$crate::__field::LocateMut::new($field)).locate($offset, $ty) + use $crate::__field::{LocateLeafMut, LocateRecordMut}; + (&&$crate::__field::LocateMut::new($field)).locate($offset, $ty) }}; } impl Ptr { - // Borrows the elements of the struct field pointed to by a Field pointer. + // Borrows the field at byte `offset` of `root`. #[inline] - pub(crate) fn borrow_field<'a>(root: &'a dyn Root, field: u32) -> Ref<'a, [T]> { - Ref::map(root.borrow_at(field as usize, TypeId::of::()), |obj| { - elems::(obj) + pub(crate) fn borrow_field(root: &dyn Root, offset: usize) -> Ref<'_, T> { + Ref::map(root.borrow_at(offset, TypeId::of::()), |obj| { + obj.downcast_ref::().unwrap_or_else(|| invalid_field()) }) } #[inline] - pub(crate) fn borrow_field_mut<'a>(root: &'a dyn Root, field: u32) -> RefMut<'a, [T]> { - RefMut::map( - root.borrow_at_mut(field as usize, TypeId::of::()), - |obj| elems_mut::(obj), - ) - } -} - -impl Ptr { - pub(crate) fn field_len(root: &Weak, field: u32) -> usize { - let root = upgrade(root); - Self::borrow_field(&*root, field).len() + pub(crate) fn borrow_field_mut(root: &dyn Root, offset: usize) -> RefMut<'_, T> { + RefMut::map(root.borrow_at_mut(offset, TypeId::of::()), |obj| { + obj.downcast_mut::().unwrap_or_else(|| invalid_field()) + }) } } @@ -389,7 +295,7 @@ mod tests { #[offset(0)] a: i32, #[offset(4)] - name: Box<[u8]>, + b: i32, } impl ByteRepr for Inner { @@ -398,12 +304,12 @@ mod tests { } fn to_bytes(&self, buf: &mut [u8]) { self.a.to_bytes(&mut buf[0..4]); - buf[4..8].copy_from_slice(&self.name); + self.b.to_bytes(&mut buf[4..8]); } fn from_bytes(buf: &[u8]) -> Self { Self { a: i32::from_bytes(&buf[0..4]), - name: Box::from(&buf[4..8]), + b: i32::from_bytes(&buf[4..8]), } } } @@ -415,8 +321,8 @@ mod tests { #[offset(4)] inner: Inner, #[offset(12)] - items: Box<[Inner]>, - #[offset(36)] + items: Value>, + #[offset(40)] v: Value>, } @@ -427,15 +333,14 @@ mod tests { } fn inner(a: i32) -> Inner { - Inner { - a, - name: Box::from(*b"abc\0"), - } + Inner { a, b: -a } } fn outer() -> Value { Rc::new(RefCell::new(Outer { - items: vec![inner(1), inner(2), inner(3)].into_boxed_slice(), + items: Rc::new(RefCell::new( + vec![inner(1), inner(2), inner(3)].into_boxed_slice(), + )), ..Default::default() })) } @@ -451,7 +356,7 @@ mod tests { x.with_mut(|v| *v += 1); assert_eq!(s.borrow().x, 8); assert_eq!(x.len(), 1); - assert_eq!(std::mem::size_of::>(), 32); + assert_eq!(x.offset(1) - x.clone(), 1); } #[test] @@ -466,51 +371,31 @@ mod tests { assert_eq!(a.read(), 4); p.with_mut(|p| p.a = 5); assert_eq!(a.read(), 5); - } - - #[test] - fn array_field() { - let s: Value = Rc::new(RefCell::new(inner(1))); - let name: Ptr = field_ptr!(s, name); - assert_eq!(name.len(), 4); - assert_eq!(name.offset(1).read(), b'b'); - name.offset(2).write(b'x'); - assert_eq!(&*s.borrow().name, b"abx\0"); - assert_eq!(name.to_c_bytes(), b"abx"); - let whole: Ptr> = field_ptr!(s, name); - assert_eq!(whole.decay().offset(3), name.offset(3)); - assert_eq!(name.offset(3) - name.clone(), 3); - assert!(name.offset(1) < name.offset(2)); - name.with_slice_mut(2, |b| b.copy_from_slice(b"zy")); - assert_eq!(&*s.borrow().name, b"zyx\0"); + let b = field_ptr!(p, b); + assert!(a < b); + assert_ne!(a, b); + assert_eq!(b, field_ptr!(field_ptr!(s, inner), b)); } #[test] fn field_of_array_element() { + // Arrays are held in Values of their own, which are the roots of the + // field pointers into their elements. let s = outer(); - let items: Ptr = field_ptr!(s, items); + let items: Ptr = s.borrow().items.as_pointer(); let a = field_ptr!(items.offset(1), a); assert_eq!(a.read(), 2); a.write(20); - assert_eq!(s.borrow().items[1].a, 20); - let name: Ptr = field_ptr!(items.offset(2), name); - name.offset(1).write(b'q'); - assert_eq!(&*s.borrow().items[2].name, b"aqc\0"); + assert_eq!(s.borrow().items.borrow()[1].a, 20); + let b = field_ptr!(items.offset(2), b); + assert_eq!(b.read(), -3); let first = field_ptr!(items, a); assert!(first < a); - assert_ne!(first, a); assert_eq!(first, field_ptr!(items.offset(0), a)); - // A field of an element of an array. - let arr: Value> = - Rc::new(RefCell::new(vec![inner(1), inner(2)].into_boxed_slice())); - let a = field_ptr!((arr.as_pointer() as Ptr).offset(1), a); - assert_eq!(a.read(), 2); } #[test] fn vector_field() { - // Vectors are held in Values of their own, which are the roots of the - // field pointers into their elements. let s = outer(); *s.borrow().v.borrow_mut() = vec![inner(1), inner(2)]; let v: Ptr = s.borrow().v.as_pointer(); @@ -519,8 +404,7 @@ mod tests { s.borrow().v.borrow_mut().push(inner(3)); a.write(20); assert_eq!(s.borrow().v.borrow()[1].a, 20); - let name: Ptr = field_ptr!(v.offset(2), name); - assert_eq!(name.offset(1).read(), b'b'); + assert_eq!(field_ptr!(v.offset(2), b).read(), -3); } #[test] @@ -536,14 +420,21 @@ mod tests { // A field of a reinterpreted struct is written through. let raw: Ptr = Ptr::alloc_array(vec![0u8; 8].into_boxed_slice()); let view: Ptr = raw.reinterpret_cast(); - let name: Ptr = field_ptr!(view, name); - name.offset(1).write(b'q'); - assert_eq!(raw.offset(5).read(), b'q'); + let b: Ptr = field_ptr!(view, b); + b.write(0x0a0b0c0d); + assert_eq!(raw.offset(4).read(), 0x0d); view.with_mut(|v| v.a = 0x11223344); assert_eq!(raw.read(), 0x44); raw.delete(); } + #[test] + #[should_panic(expected = "ub: invalid field pointer")] + fn out_of_bounds_field() { + let s = outer(); + field_ptr!(s, x).offset(-1).read(); + } + #[test] #[should_panic(expected = "ub: dangling pointer")] fn dangling_field() { diff --git a/libcc2rs/src/lib.rs b/libcc2rs/src/lib.rs index 59ac014790..2c504f35e6 100644 --- a/libcc2rs/src/lib.rs +++ b/libcc2rs/src/lib.rs @@ -10,13 +10,12 @@ mod rc; pub use rc::*; mod field; -pub use field::{Elems, FieldPtr, Record}; +pub use field::{FieldPtr, Record}; // Used by #[derive(Record)]. #[doc(hidden)] pub mod __field { pub use crate::field::{ - Locate, LocateArray, LocateArrayMut, LocateLeaf, LocateLeafMut, LocateMut, LocateRecord, - LocateRecordMut, LocateRecords, LocateRecordsMut, + Locate, LocateLeaf, LocateLeafMut, LocateMut, LocateRecord, LocateRecordMut, }; } diff --git a/libcc2rs/src/rc.rs b/libcc2rs/src/rc.rs index b62dd19ad0..24b015b469 100644 --- a/libcc2rs/src/rc.rs +++ b/libcc2rs/src/rc.rs @@ -35,10 +35,7 @@ pub(crate) enum PtrKind { HeapArray(Weak>>), StackVec(Weak>>), HeapVec(Weak>>), - // A field of a struct: the allocation that holds the struct, and the - // byte offset of the field in it. The field is either a T, or an array of - // T that the pointer indexes into. - Field(Weak, u32), + Field(Weak), Reinterpreted(Rc), } @@ -54,7 +51,6 @@ pub enum StrongPtr { }, Field { root: Rc, - field: u32, offset: usize, }, Reinterpreted { @@ -75,11 +71,7 @@ impl StrongPtr { StrongPtr::StackSingle(rc) => rc.borrow(), StrongPtr::Vec { rc, offset } => Ref::map(rc.borrow(), |v| &v[*offset]), StrongPtr::StackArray { rc, offset } => Ref::map(rc.borrow(), |a| &a[*offset]), - StrongPtr::Field { - root, - field, - offset, - } => Ref::map(Ptr::borrow_field(&**root, *field), |f| &f[*offset]), + StrongPtr::Field { root, offset } => Ptr::borrow_field(&**root, *offset), StrongPtr::Reinterpreted { alloc, byte_offset, @@ -98,11 +90,7 @@ impl StrongPtr { StrongPtr::StackArray { rc, offset } => { RefMut::map(rc.borrow_mut(), |a| &mut a[*offset]) } - StrongPtr::Field { - root, - field, - offset, - } => RefMut::map(Ptr::borrow_field_mut(&**root, *field), |f| &mut f[*offset]), + StrongPtr::Field { root, offset } => Ptr::borrow_field_mut(&**root, *offset), StrongPtr::Reinterpreted { .. } => panic!("mutable reference to reinterpreted memory"), } } @@ -132,7 +120,7 @@ impl fmt::Debug for PtrKind { PtrKind::HeapSingle(w) => write!(f, "HeapSingle({:?})", w.as_ptr()), PtrKind::StackArray(w) => write!(f, "StackArray({:?})", w.as_ptr()), PtrKind::HeapArray(w) => write!(f, "HeapArray({:?})", w.as_ptr()), - PtrKind::Field(root, field) => write!(f, "Field({:?}, {field})", root.as_ptr()), + PtrKind::Field(root) => write!(f, "Field({:?})", root.as_ptr()), PtrKind::Reinterpreted(data) => { write!(f, "Reinterpreted(0x{:x})", data.alloc.address()) } @@ -151,7 +139,7 @@ impl Clone for PtrKind { PtrKind::HeapSingle(weak) => PtrKind::HeapSingle(weak.clone()), PtrKind::StackArray(weak) => PtrKind::StackArray(weak.clone()), PtrKind::HeapArray(weak) => PtrKind::HeapArray(weak.clone()), - PtrKind::Field(root, field) => PtrKind::Field(root.clone(), *field), + PtrKind::Field(root) => PtrKind::Field(root.clone()), PtrKind::Reinterpreted(data) => PtrKind::Reinterpreted(Rc::clone(data)), } } @@ -164,7 +152,7 @@ impl PtrKind { PtrKind::StackSingle(w) | PtrKind::HeapSingle(w) => w.as_ptr() as usize, PtrKind::StackVec(w) | PtrKind::HeapVec(w) => w.as_ptr() as usize, PtrKind::StackArray(w) | PtrKind::HeapArray(w) => w.as_ptr() as usize, - PtrKind::Field(root, _) => root.as_ptr() as *const () as usize, + PtrKind::Field(root) => root.as_ptr() as *const () as usize, PtrKind::Reinterpreted(data) => data.alloc.address(), } } @@ -310,21 +298,19 @@ impl Ptr { #[inline] fn byte_offset(&self) -> usize { match &self.kind { - PtrKind::Reinterpreted(_) => self.offset, - PtrKind::Field(_, field) => { - (*field as usize).wrapping_add(self.offset.wrapping_mul(std::mem::size_of::())) - } + PtrKind::Reinterpreted(_) | PtrKind::Field(_) => self.offset, _ => self.offset.wrapping_mul(std::mem::size_of::()), } } - // For Reinterpreted, Ptr::offset is in bytes. For all other variants, - // Ptr::offset is in elements (step = 1). This helper converts between - // user-facing element counts and the internal offset units. + // For Reinterpreted and Field, Ptr::offset is in bytes. For all other + // variants, Ptr::offset is in elements (step = 1). This helper converts + // between user-facing element counts and the internal offset units. #[inline] fn elem_step(&self) -> usize { match &self.kind { PtrKind::Reinterpreted(data) => data.elem_byte_size, + PtrKind::Field(_) => std::mem::size_of::().max(1), _ => 1, } } @@ -336,14 +322,13 @@ impl Ptr { { match &self.kind { PtrKind::Null => 0, - PtrKind::StackSingle(_) | PtrKind::HeapSingle(_) => 1, + PtrKind::StackSingle(_) | PtrKind::HeapSingle(_) | PtrKind::Field(_) => 1, PtrKind::StackVec(weak) | PtrKind::HeapVec(weak) => { weak.upgrade().expect("ub: dangling pointer").borrow().len() } PtrKind::StackArray(weak) | PtrKind::HeapArray(weak) => { weak.upgrade().expect("ub: dangling pointer").borrow().len() } - PtrKind::Field(root, field) => Self::field_len(root, *field), PtrKind::Reinterpreted(data) => data.alloc.total_byte_len() / data.elem_byte_size, } } @@ -355,7 +340,7 @@ impl Ptr { { match &self.kind { PtrKind::Null => true, - PtrKind::StackSingle(_) | PtrKind::HeapSingle(_) => false, + PtrKind::StackSingle(_) | PtrKind::HeapSingle(_) | PtrKind::Field(_) => false, PtrKind::StackVec(weak) | PtrKind::HeapVec(weak) => weak .upgrade() .expect("ub: dangling pointer") @@ -366,7 +351,6 @@ impl Ptr { .expect("ub: dangling pointer") .borrow() .is_empty(), - PtrKind::Field(root, field) => Self::field_len(root, *field) == 0, PtrKind::Reinterpreted(data) => self.offset >= data.alloc.total_byte_len(), } } @@ -396,6 +380,10 @@ impl Ptr { where T: 'static, { + // A field is a single object, whose offset is that of the pointer. + if let PtrKind::Field(_) = self.kind { + return self.clone(); + } Self { kind: self.kind.clone(), offset: self.len().wrapping_sub(1).wrapping_mul(self.elem_step()), @@ -407,6 +395,9 @@ impl Ptr { where T: 'static, { + if let PtrKind::Field(_) = self.kind { + return self.offset(1); + } Self { kind: self.kind.clone(), offset: self.len().wrapping_mul(self.elem_step()), @@ -428,9 +419,8 @@ impl Ptr { rc: weak.upgrade().unwrap_or_else(|| dangling()), offset: self.offset, }, - PtrKind::Field(root, field) => StrongPtr::Field { + PtrKind::Field(root) => StrongPtr::Field { root: upgrade_root(root), - field: *field, offset: self.offset, }, PtrKind::Reinterpreted(data) => StrongPtr::Reinterpreted { @@ -484,10 +474,7 @@ impl Ptr { OriginalAlloc::slice(weak), self.offset.wrapping_mul(T::byte_size()), ), - PtrKind::Field(root, field) => ( - OriginalAlloc::field::(root, *field), - self.offset.wrapping_mul(T::byte_size()), - ), + PtrKind::Field(root) => (OriginalAlloc::field::(root, self.offset), 0), PtrKind::Reinterpreted(data) => (data.alloc.clone(), self.offset), }) } @@ -533,9 +520,9 @@ impl Ptr { let mut borrow = rc.borrow_mut(); f(&mut borrow[self.offset]) } - PtrKind::Field(root, field) => { + PtrKind::Field(root) => { let root = upgrade_root(root); - f(&mut Self::borrow_field_mut(&*root, *field)[self.offset]) + f(&mut Self::borrow_field_mut(&*root, self.offset)) } PtrKind::Reinterpreted(data) => Self::with_mut_reinterpreted(data, self.offset, f), PtrKind::Null => null_deref(), @@ -582,9 +569,9 @@ impl Ptr { let borrow = rc.borrow(); f(&borrow[self.offset]) } - PtrKind::Field(root, field) => { + PtrKind::Field(root) => { let root = upgrade_root(root); - f(&Self::borrow_field(&*root, *field)[self.offset]) + f(&Self::borrow_field(&*root, self.offset)) } PtrKind::Reinterpreted(data) => Self::with_reinterpreted(data, self.offset, f), PtrKind::Null => null_deref(), @@ -638,9 +625,11 @@ impl Ptr { let mut b = rc.borrow_mut(); f(&mut b[off..off + len]) } - PtrKind::Field(root, field) => { + PtrKind::Field(root) => { + assert!(len <= 1, "ub: with_slice_mut out of bounds"); let root = upgrade_root(root); - f(&mut Self::borrow_field_mut(&*root, *field)[off..off + len]) + let mut b = Self::borrow_field_mut(&*root, off); + f(&mut std::slice::from_mut(&mut *b)[..len]) } PtrKind::Reinterpreted(data) => with_scratch(len, |buf| { data.alloc.read_bytes(off, buf); @@ -671,9 +660,11 @@ impl Ptr { let b = rc.borrow(); f(&b[off..off + len]) } - PtrKind::Field(root, field) => { + PtrKind::Field(root) => { + assert!(len <= 1, "ub: with_slice out of bounds"); let root = upgrade_root(root); - f(&Self::borrow_field(&*root, *field)[off..off + len]) + let b = Self::borrow_field(&*root, off); + f(&std::slice::from_ref(&*b)[..len]) } PtrKind::Reinterpreted(data) => with_scratch(len, |buf| { data.alloc.read_bytes(off, buf); @@ -711,7 +702,7 @@ impl Ptr { pub fn sort(&self, last: usize) { match self.kind { PtrKind::Null => panic!("ub: dereference of null pointer"), - PtrKind::StackSingle(_) | PtrKind::HeapSingle(_) => { + PtrKind::StackSingle(_) | PtrKind::HeapSingle(_) | PtrKind::Field(_) => { panic!("only vecs and arrays can be sorted") } PtrKind::StackVec(ref weak) | PtrKind::HeapVec(ref weak) => { @@ -722,10 +713,6 @@ impl Ptr { let strong = weak.upgrade().expect("ub: dangling pointer"); (*strong.borrow_mut())[self.get_offset()..last].sort(); } - PtrKind::Field(ref root, field) => { - let root = upgrade_root(root); - Self::borrow_field_mut(&*root, field)[self.offset..last].sort(); - } PtrKind::Reinterpreted(_) => { panic!("sorting not supported for reinterpreted pointers") } @@ -758,7 +745,7 @@ impl Ptr { } match self.kind { PtrKind::Null => panic!("ub: dereference of null pointer"), - PtrKind::StackSingle(_) | PtrKind::HeapSingle(_) => { + PtrKind::StackSingle(_) | PtrKind::HeapSingle(_) | PtrKind::Field(_) => { panic!("only vecs and arrays can be sorted") } PtrKind::StackVec(ref weak) | PtrKind::HeapVec(ref weak) => { @@ -771,15 +758,6 @@ impl Ptr { let mut borrow = strong.borrow_mut(); sort(&mut borrow, self.get_offset(), last, &mut cmp); } - PtrKind::Field(ref root, field) => { - let root = upgrade_root(root); - sort( - &mut Self::borrow_field_mut(&*root, field), - self.offset, - last, - &mut cmp, - ); - } PtrKind::Reinterpreted(_) => { panic!("sorting not supported for reinterpreted pointers") } @@ -1052,11 +1030,6 @@ impl Ptr> { offset: self.offset, kind: PtrKind::HeapVec(weak.clone()), }, - // A vector that is a field. - PtrKind::Field(root, field) if self.offset == 0 => Ptr { - offset: 0, - kind: PtrKind::Field(root.clone(), *field), - }, _ => panic!("ub: invalid decay"), } } @@ -1075,11 +1048,6 @@ impl Ptr> { offset: self.offset, kind: PtrKind::HeapArray(weak.clone()), }, - // An array that is a field. - PtrKind::Field(root, field) if self.offset == 0 => Ptr { - offset: 0, - kind: PtrKind::Field(root.clone(), *field), - }, _ => panic!("ub: invalid decay"), } } diff --git a/libcc2rs/src/reinterpret.rs b/libcc2rs/src/reinterpret.rs index 6ad2b73813..c6e97f3045 100644 --- a/libcc2rs/src/reinterpret.rs +++ b/libcc2rs/src/reinterpret.rs @@ -154,13 +154,13 @@ pub struct OriginalAlloc { #[derive(Clone)] enum Storage { Alloc(Weak), - Field(Weak, u32), + Field(Weak, usize), } // The storage passed to the AllocOps of a field. struct FieldStorage { root: Rc, - field: u32, + field: usize, } impl OriginalAlloc { @@ -178,8 +178,8 @@ impl OriginalAlloc { } } - // A field whose elements are of type T. - pub(crate) fn field(root: &Weak, field: u32) -> Self { + // The field of type T at byte offset `field` of `root`. + pub(crate) fn field(root: &Weak, field: usize) -> Self { Self { storage: Storage::Field(root.clone(), field), ops: &FieldOps::(PhantomData), @@ -216,7 +216,7 @@ impl OriginalAlloc { match &self.storage { Storage::Alloc(weak) => weak.as_ptr() as *const () as usize, Storage::Field(root, field) => { - (root.as_ptr() as *const () as usize).wrapping_add(*field as usize) + (root.as_ptr() as *const () as usize).wrapping_add(*field) } } } @@ -384,18 +384,17 @@ struct FieldOps(PhantomData T>); impl AllocOps for FieldOps { fn read_bytes(&self, storage: &dyn Any, byte_offset: usize, buf: &mut [u8]) { let storage = storage.downcast_ref::().unwrap(); - let elems = Ptr::::borrow_field(&*storage.root, storage.field); - slice_read_bytes(&elems, byte_offset, buf); + let field = Ptr::::borrow_field(&*storage.root, storage.field); + slice_read_bytes(std::slice::from_ref(&*field), byte_offset, buf); } fn write_bytes(&self, storage: &dyn Any, byte_offset: usize, data: &[u8]) { let storage = storage.downcast_ref::().unwrap(); - let mut elems = Ptr::::borrow_field_mut(&*storage.root, storage.field); - slice_write_bytes(&mut elems, byte_offset, data); + let mut field = Ptr::::borrow_field_mut(&*storage.root, storage.field); + slice_write_bytes(std::slice::from_mut(&mut *field), byte_offset, data); } - fn total_byte_len(&self, storage: &dyn Any) -> usize { - let storage = storage.downcast_ref::().unwrap(); - Ptr::::borrow_field(&*storage.root, storage.field).len() * T::byte_size() + fn total_byte_len(&self, _storage: &dyn Any) -> usize { + T::byte_size() } } diff --git a/tests/multi-file/defaulted_move_cross_tu/out/refcount/defaulted_move_cross_tu.rs b/tests/multi-file/defaulted_move_cross_tu/out/refcount/defaulted_move_cross_tu.rs index d19df7f0df..c05f4b7080 100644 --- a/tests/multi-file/defaulted_move_cross_tu/out/refcount/defaulted_move_cross_tu.rs +++ b/tests/multi-file/defaulted_move_cross_tu/out/refcount/defaulted_move_cross_tu.rs @@ -11,7 +11,7 @@ pub struct S { #[offset(0)] pub v: Value>, #[offset(24)] - pub n: Box<[i32]>, + pub n: Value>, } impl S { pub fn new(x: i32) -> Self { @@ -21,7 +21,7 @@ impl S { (*x.borrow()); ((*x.borrow()) as usize) as usize ])), - n: Box::new([(*x.borrow()), ((*x.borrow()) + 1)]), + n: Rc::new(RefCell::new(Box::new([(*x.borrow()), ((*x.borrow()) + 1)]))), })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() @@ -33,9 +33,9 @@ impl S { _a0.with(|__s: &S| std::mem::take(&mut (*__s.v.borrow_mut()))), )) }, - n: Box::new(std::array::from_fn::<_, 2, _>(|__i: usize| { - (*_a0.upgrade().deref()).n[(__i) as usize] - })), + n: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 2, _>( + |__i: usize| (*_a0.with(|__s: &S| __s.n.clone()).borrow())[(__i) as usize], + )))), })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() @@ -45,7 +45,7 @@ impl Default for S { fn default() -> Self { S { v: Rc::new(RefCell::new(Default::default())), - n: (0..2).map(|_| 0_i32).collect::>(), + n: Rc::new(RefCell::new((0..2).map(|_| 0_i32).collect::>())), } } } @@ -57,13 +57,13 @@ impl ByteRepr for S { 32 } fn to_bytes(&self, buf: &mut [u8]) { - self.v.to_bytes(&mut buf[0..24]); - self.n.to_bytes(&mut buf[24..32]); + (*self.v.borrow()).to_bytes(&mut buf[0..24]); + (*self.n.borrow()).to_bytes(&mut buf[24..32]); } fn from_bytes(buf: &[u8]) -> Self { Self { - v: >>::from_bytes(&buf[0..24]), - n: >::from_bytes(&buf[24..32]), + v: Rc::new(RefCell::new(>::from_bytes(&buf[0..24]))), + n: Rc::new(RefCell::new(>::from_bytes(&buf[24..32]))), } } } @@ -72,9 +72,9 @@ pub fn sum_0(s: Ptr) -> i32 { return { let _lhs = { let _lhs = (s.with(|__s: &S| (*__s.v.borrow()).len()) as i32); - _lhs + s.with(|__s: &S| __s.n[(0) as usize]) + _lhs + s.with(|__s: &S| (*__s.n.borrow())[(0) as usize]) }; - _lhs + s.with(|__s: &S| __s.n[(1) as usize]) + _lhs + s.with(|__s: &S| (*__s.n.borrow())[(1) as usize]) }; } pub fn main() { @@ -105,11 +105,13 @@ impl SImpl for Ptr { ((*self).with(|__s: &S| __s.v.as_pointer()) as Ptr>) .write(_a0.with(|__s: &S| std::mem::take(&mut (*__s.v.borrow_mut())))); { - ((field_ptr!((*self), n)) as Ptr).to_any().memcpy( - &((field_ptr!(_a0, n)) as Ptr).to_any(), - 8_usize as usize, - ); - ((field_ptr!((*self), n)) as Ptr).to_any() + (((*self).with(|__s: &S| __s.n.as_pointer())) as Ptr) + .to_any() + .memcpy( + &((_a0.with(|__s: &S| __s.n.as_pointer())) as Ptr).to_any(), + 8_usize as usize, + ); + (((*self).with(|__s: &S| __s.n.as_pointer())) as Ptr).to_any() }; return (*self).clone(); } diff --git a/tests/unit/field_array_elem_ptr.cpp b/tests/unit/field_array_elem_ptr.cpp new file mode 100644 index 0000000000..5ca37af577 --- /dev/null +++ b/tests/unit/field_array_elem_ptr.cpp @@ -0,0 +1,70 @@ +#include + +struct Point { + int x; + int y; +}; + +struct Shape { + int id; + int coords[4]; + Point points[3]; + int tail; +}; + +static int sum(const int *p, int n) { + int s = 0; + for (int i = 0; i < n; ++i) { + s += p[i]; + } + return s; +} + +static void set_y(Point *p, int y) { p->y = y; } + +int main() { + Shape s = {1, {10, 20, 30, 40}, {{1, 2}, {3, 4}, {5, 6}}, 99}; + + // Pointers to elements of an array field, and arithmetic on them. + int *c = &s.coords[1]; + assert(*c == 20); + *c = 21; + assert(s.coords[1] == 21); + c += 2; + assert(*c == 40); + assert(c - &s.coords[0] == 3); + assert(c[-1] == 30); + assert(sum(s.coords, 4) == 101); + assert(sum(&s.coords[2], 2) == 70); + + // Pointers to elements of an array of structs, and to their fields. + Point *p = &s.points[1]; + assert(p->x == 3); + set_y(p + 1, 60); + assert(s.points[2].y == 60); + int *py = &s.points[0].y; + *py = 7; + assert(s.points[0].y == 7); + int *px = &(p + 1)->x; + *px += 50; + assert(s.points[2].x == 55); + + // The same, through a pointer to the struct. + Shape *sp = &s; + int *d = sp->coords + 3; + *d = 41; + assert(s.coords[3] == 41); + sp->points[1].y = sp->coords[0] + sp->points[0].x; + assert(s.points[1].y == 11); + Point *q = sp->points; + q[2].x = 8; + assert(sp->points[2].x == 8); + + // Copies of the struct copy the arrays. + Shape t = s; + t.coords[0] = 0; + t.points[0].x = 0; + assert(s.coords[0] == 10 && s.points[0].x == 1); + assert(t.coords[1] == 21 && t.points[2].y == 60 && t.tail == 99); + return 0; +} diff --git a/tests/unit/out/refcount/array_const_init.rs b/tests/unit/out/refcount/array_const_init.rs index 26131e5d41..5dad1ee329 100644 --- a/tests/unit/out/refcount/array_const_init.rs +++ b/tests/unit/out/refcount/array_const_init.rs @@ -6,21 +6,30 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Clone, Record, VaArg, FnPtrArg)] +#[derive(Record, VaArg, FnPtrArg)] pub struct S { #[offset(0)] pub head: i32, #[offset(4)] - pub tail: Box<[i32]>, + pub tail: Value>, #[offset(16)] - pub buf: Box<[u8]>, + pub buf: Value>, +} +impl Clone for S { + fn clone(&self) -> Self { + Self { + head: self.head.clone(), + tail: Rc::new(RefCell::new((*self.tail.borrow()).clone())), + buf: Rc::new(RefCell::new((*self.buf.borrow()).clone())), + } + } } impl Default for S { fn default() -> Self { S { head: 0_i32, - tail: (0..3).map(|_| 0_i32).collect::>(), - buf: (0..4).map(|_| 0_u8).collect::>(), + tail: Rc::new(RefCell::new((0..3).map(|_| 0_i32).collect::>())), + buf: Rc::new(RefCell::new((0..4).map(|_| 0_u8).collect::>())), } } } @@ -30,22 +39,22 @@ impl ByteRepr for S { } fn to_bytes(&self, buf: &mut [u8]) { self.head.to_bytes(&mut buf[0..4]); - self.tail.to_bytes(&mut buf[4..16]); - self.buf.to_bytes(&mut buf[16..20]); + (*self.tail.borrow()).to_bytes(&mut buf[4..16]); + (*self.buf.borrow()).to_bytes(&mut buf[16..20]); } fn from_bytes(buf: &[u8]) -> Self { Self { head: ::from_bytes(&buf[0..4]), - tail: >::from_bytes(&buf[4..16]), - buf: >::from_bytes(&buf[16..20]), + tail: Rc::new(RefCell::new(>::from_bytes(&buf[4..16]))), + buf: Rc::new(RefCell::new(>::from_bytes(&buf[16..20]))), } } } thread_local!( pub static s_0: Value = Rc::new(RefCell::new(S { head: 5, - tail: Box::new([0; 3]), - buf: Box::new([0; 4]), + tail: Rc::new(RefCell::new(Box::new([0; 3]))), + buf: Rc::new(RefCell::new(Box::new([0; 4]))), })); ); pub fn main() { @@ -57,15 +66,17 @@ fn main_0() -> i32 { let i: Value = Rc::new(RefCell::new(0)); 'loop_: while ((((*i.borrow()) < 3) as i32) != 0) { assert!( - ((((*s_0.with(Value::clone).borrow()).tail[(*i.borrow()) as usize] == 0) as i32) != 0) + ((((*(*s_0.with(Value::clone).borrow()).tail.borrow())[(*i.borrow()) as usize] == 0) + as i32) + != 0) ); (*i.borrow_mut()).postfix_inc(); } let i: Value = Rc::new(RefCell::new(0)); 'loop_: while ((((*i.borrow()) < 4) as i32) != 0) { assert!( - (((((*s_0.with(Value::clone).borrow()).buf[(*i.borrow()) as usize] as i32) == 0) - as i32) + (((((*(*s_0.with(Value::clone).borrow()).buf.borrow())[(*i.borrow()) as usize] as i32) + == 0) as i32) != 0) ); (*i.borrow_mut()).postfix_inc(); diff --git a/tests/unit/out/refcount/array_of_noncopy_struct.rs b/tests/unit/out/refcount/array_of_noncopy_struct.rs index 2f833534ab..aee01a2853 100644 --- a/tests/unit/out/refcount/array_of_noncopy_struct.rs +++ b/tests/unit/out/refcount/array_of_noncopy_struct.rs @@ -39,12 +39,12 @@ impl ByteRepr for NonCopy { 32 } fn to_bytes(&self, buf: &mut [u8]) { - self.data.to_bytes(&mut buf[0..24]); + (*self.data.borrow()).to_bytes(&mut buf[0..24]); self.tag.to_bytes(&mut buf[24..28]); } fn from_bytes(buf: &[u8]) -> Self { Self { - data: >>::from_bytes(&buf[0..24]), + data: Rc::new(RefCell::new(>::from_bytes(&buf[0..24]))), tag: ::from_bytes(&buf[24..28]), } } diff --git a/tests/unit/out/refcount/class_templates.rs b/tests/unit/out/refcount/class_templates.rs index 319acb1e66..038986386f 100644 --- a/tests/unit/out/refcount/class_templates.rs +++ b/tests/unit/out/refcount/class_templates.rs @@ -29,11 +29,11 @@ impl ByteRepr for MyContainer_int_ { 24 } fn to_bytes(&self, buf: &mut [u8]) { - self.vec_.to_bytes(&mut buf[0..24]); + (*self.vec_.borrow()).to_bytes(&mut buf[0..24]); } fn from_bytes(buf: &[u8]) -> Self { Self { - vec_: >>::from_bytes(&buf[0..24]), + vec_: Rc::new(RefCell::new(>::from_bytes(&buf[0..24]))), } } } @@ -61,11 +61,11 @@ impl ByteRepr for MyContainer_char_ { 24 } fn to_bytes(&self, buf: &mut [u8]) { - self.vec_.to_bytes(&mut buf[0..24]); + (*self.vec_.borrow()).to_bytes(&mut buf[0..24]); } fn from_bytes(buf: &[u8]) -> Self { Self { - vec_: >>::from_bytes(&buf[0..24]), + vec_: Rc::new(RefCell::new(>::from_bytes(&buf[0..24]))), } } } @@ -93,11 +93,11 @@ impl ByteRepr for MyContainer_float_ { 24 } fn to_bytes(&self, buf: &mut [u8]) { - self.vec_.to_bytes(&mut buf[0..24]); + (*self.vec_.borrow()).to_bytes(&mut buf[0..24]); } fn from_bytes(buf: &[u8]) -> Self { Self { - vec_: >>::from_bytes(&buf[0..24]), + vec_: Rc::new(RefCell::new(>::from_bytes(&buf[0..24]))), } } } diff --git a/tests/unit/out/refcount/clone_vs_move.rs b/tests/unit/out/refcount/clone_vs_move.rs index 06d30a3045..cbb8ef8bc9 100644 --- a/tests/unit/out/refcount/clone_vs_move.rs +++ b/tests/unit/out/refcount/clone_vs_move.rs @@ -24,7 +24,7 @@ impl ByteRepr for Bar { } } } -#[derive(Clone, Record, VaArg, FnPtrArg)] +#[derive(Record, VaArg, FnPtrArg)] pub struct Foo { #[offset(0)] pub x: i32, @@ -33,17 +33,32 @@ pub struct Foo { #[offset(16)] pub z: Ptr, #[offset(24)] - pub a: Box<[i32]>, + pub a: Value>, #[offset(36)] pub bar: Bar, } +impl Clone for Foo { + fn clone(&self) -> Self { + let __this: Value = Rc::new(RefCell::new(Self { + x: { self.x }, + y: { (self.y).clone() }, + z: { self.z.clone() }, + a: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 3, _>( + |__i: usize| (*self.a.borrow())[(__i) as usize], + )))), + bar: { self.bar.clone() }, + })); + let this: Ptr = __this.as_pointer(); + Rc::try_unwrap(__this).ok().unwrap().into_inner() + } +} impl Default for Foo { fn default() -> Self { Foo { x: 0_i32, y: >::default(), z: Ptr::::null(), - a: (0..3).map(|_| 0_i32).collect::>(), + a: Rc::new(RefCell::new((0..3).map(|_| 0_i32).collect::>())), bar: ::default(), } } @@ -104,7 +119,7 @@ fn main_0() -> i32 { x: 1, y: x1.as_pointer(), z: (x1.as_pointer()), - a: Box::new([0, 1, 2]), + a: Rc::new(RefCell::new(Box::new([0, 1, 2]))), bar: Bar { w: 10 }, })); assert!(({ (*f1.borrow()).x } == 1)); @@ -134,15 +149,15 @@ fn main_0() -> i32 { _lhs == (x1.as_pointer()) }); assert!((({ (*f1.borrow()).z.clone() }.read()) == 4)); - (*f2.borrow_mut()).a[(0) as usize].prefix_inc(); - (*f2.borrow_mut()).a[(1) as usize].prefix_inc(); - (*f2.borrow_mut()).a[(2) as usize].prefix_inc(); - assert!(((*f2.borrow()).a[(0) as usize] == 1)); - assert!(((*f2.borrow()).a[(1) as usize] == 2)); - assert!(((*f2.borrow()).a[(2) as usize] == 3)); - assert!(((*f1.borrow()).a[(0) as usize] == 0)); - assert!(((*f1.borrow()).a[(1) as usize] == 1)); - assert!(((*f1.borrow()).a[(2) as usize] == 2)); + (*(*f2.borrow()).a.borrow_mut())[(0) as usize].prefix_inc(); + (*(*f2.borrow()).a.borrow_mut())[(1) as usize].prefix_inc(); + (*(*f2.borrow()).a.borrow_mut())[(2) as usize].prefix_inc(); + assert!(((*(*f2.borrow()).a.borrow())[(0) as usize] == 1)); + assert!(((*(*f2.borrow()).a.borrow())[(1) as usize] == 2)); + assert!(((*(*f2.borrow()).a.borrow())[(2) as usize] == 3)); + assert!(((*(*f1.borrow()).a.borrow())[(0) as usize] == 0)); + assert!(((*(*f1.borrow()).a.borrow())[(1) as usize] == 1)); + assert!(((*(*f1.borrow()).a.borrow())[(2) as usize] == 2)); (*f2.borrow_mut()).bar.w = 20; assert!(({ (*f2.borrow()).bar.w } == 20)); assert!(({ (*f1.borrow()).bar.w } == 10)); diff --git a/tests/unit/out/refcount/copy_assign.rs b/tests/unit/out/refcount/copy_assign.rs index a1f45f2453..8afdd4ed85 100644 --- a/tests/unit/out/refcount/copy_assign.rs +++ b/tests/unit/out/refcount/copy_assign.rs @@ -134,21 +134,39 @@ impl ByteRepr for RefQualified { } } } +<<<<<<< HEAD #[derive(Clone, Record, VaArg, FnPtrArg)] >>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) +======= +#[derive(Record, VaArg, FnPtrArg)] +>>>>>>> 349ab5a6 (Keep Box fields boxed) pub struct Holder { #[offset(0)] pub p: Partial, #[offset(8)] - pub arr: Box<[Partial]>, + pub arr: Value>, +} +impl Clone for Holder { + fn clone(&self) -> Self { + let __this: Value = Rc::new(RefCell::new(Self { + p: { self.p.clone() }, + arr: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 2, _>( + |__i: usize| (*self.arr.borrow())[(__i) as usize].clone(), + )))), + })); + let this: Ptr = __this.as_pointer(); + Rc::try_unwrap(__this).ok().unwrap().into_inner() + } } impl Default for Holder { fn default() -> Self { Holder { p: ::default(), - arr: (0..2) - .map(|_| ::default()) - .collect::>(), + arr: Rc::new(RefCell::new( + (0..2) + .map(|_| ::default()) + .collect::>(), + )), } } } @@ -161,12 +179,12 @@ impl ByteRepr for Holder { } fn to_bytes(&self, buf: &mut [u8]) { self.p.to_bytes(&mut buf[0..8]); - self.arr.to_bytes(&mut buf[8..24]); + (*self.arr.borrow()).to_bytes(&mut buf[8..24]); } fn from_bytes(buf: &[u8]) -> Self { Self { p: ::from_bytes(&buf[0..8]), - arr: >::from_bytes(&buf[8..24]), + arr: Rc::new(RefCell::new(>::from_bytes(&buf[8..24]))), } } } @@ -216,19 +234,22 @@ fn main_0() -> i32 { assert!((assigns_0.with(|rc| *rc.borrow()) == 6)); let h: Value = Rc::new(RefCell::new(Holder { p: Partial::new({ 4 }, { 40 }), - arr: Box::new([Partial::new({ 5 }, { 50 }), Partial::new({ 6 }, { 60 })]), + arr: Rc::new(RefCell::new(Box::new([ + Partial::new({ 5 }, { 50 }), + Partial::new({ 6 }, { 60 }), + ]))), })); ({ PartialImpl::copy_assign(&{ field_ptr!(h, p) }, b.as_pointer()) }); ({ PartialImpl::copy_assign( - &{ (field_ptr!(h, arr) as Ptr).offset(1) }, + &{ ({ (*h.borrow()).arr.as_pointer() } as Ptr).offset(1) }, c.as_pointer(), ) }); assert!(({ (*h.borrow()).p.v } == 2) && ({ (*h.borrow()).p.keep } == 40)); assert!( - ({ (*h.borrow()).arr[(1) as usize].v } == 2) - && ({ (*h.borrow()).arr[(1) as usize].keep } == 60) + ({ (*(*h.borrow()).arr.borrow())[(1) as usize].v } == 2) + && ({ (*(*h.borrow()).arr.borrow())[(1) as usize].keep } == 60) ); assert!((assigns_0.with(|rc| *rc.borrow()) == 8)); let n: Value = Rc::new(RefCell::new(NonConstAssign::new())); diff --git a/tests/unit/out/refcount/copy_ctor.rs b/tests/unit/out/refcount/copy_ctor.rs index 440b207902..4382eb1f59 100644 --- a/tests/unit/out/refcount/copy_ctor.rs +++ b/tests/unit/out/refcount/copy_ctor.rs @@ -153,21 +153,39 @@ impl ByteRepr for Ignored { } } } +<<<<<<< HEAD #[derive(Clone, Record, VaArg, FnPtrArg)] >>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) +======= +#[derive(Record, VaArg, FnPtrArg)] +>>>>>>> 349ab5a6 (Keep Box fields boxed) pub struct Holder { #[offset(0)] pub c: Counted, #[offset(4)] - pub arr: Box<[Counted]>, + pub arr: Value>, +} +impl Clone for Holder { + fn clone(&self) -> Self { + let __this: Value = Rc::new(RefCell::new(Self { + c: { self.c.clone() }, + arr: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 2, _>( + |__i: usize| (*self.arr.borrow())[(__i) as usize].clone(), + )))), + })); + let this: Ptr = __this.as_pointer(); + Rc::try_unwrap(__this).ok().unwrap().into_inner() + } } impl Default for Holder { fn default() -> Self { Holder { c: ::default(), - arr: (0..2) - .map(|_| ::default()) - .collect::>(), + arr: Rc::new(RefCell::new( + (0..2) + .map(|_| ::default()) + .collect::>(), + )), } } } @@ -180,12 +198,12 @@ impl ByteRepr for Holder { } fn to_bytes(&self, buf: &mut [u8]) { self.c.to_bytes(&mut buf[0..4]); - self.arr.to_bytes(&mut buf[4..12]); + (*self.arr.borrow()).to_bytes(&mut buf[4..12]); } fn from_bytes(buf: &[u8]) -> Self { Self { c: ::from_bytes(&buf[0..4]), - arr: >::from_bytes(&buf[4..12]), + arr: Rc::new(RefCell::new(>::from_bytes(&buf[4..12]))), } } } @@ -226,12 +244,16 @@ fn main_0() -> i32 { assert!((copies_0.with(|rc| *rc.borrow()) == 6)); let hold: Value = Rc::new(RefCell::new(Holder { c: Counted::new({ 8 }), - arr: Box::new([Counted::new({ 9 }), Counted::new({ 10 })]), + arr: Rc::new(RefCell::new(Box::new([ + Counted::new({ 9 }), + Counted::new({ 10 }), + ]))), })); let hold2: Value = Rc::new(RefCell::new((*hold.borrow()).clone())); assert!( - (({ (*hold2.borrow()).c.v } == 8) && ({ (*hold2.borrow()).arr[(0) as usize].v } == 9)) - && ({ (*hold2.borrow()).arr[(1) as usize].v } == 10) + (({ (*hold2.borrow()).c.v } == 8) + && ({ (*(*hold2.borrow()).arr.borrow())[(0) as usize].v } == 9)) + && ({ (*(*hold2.borrow()).arr.borrow())[(1) as usize].v } == 10) ); assert!((copies_0.with(|rc| *rc.borrow()) == 9)); let vec_: Value> = Rc::new(RefCell::new(Vec::new())); diff --git a/tests/unit/out/refcount/copy_move_defaulted.rs b/tests/unit/out/refcount/copy_move_defaulted.rs index adb4f42399..60adb0bc87 100644 --- a/tests/unit/out/refcount/copy_move_defaulted.rs +++ b/tests/unit/out/refcount/copy_move_defaulted.rs @@ -24,14 +24,14 @@ impl ByteRepr for Inner { } } } -#[derive(Clone, Record, VaArg, FnPtrArg)] +#[derive(Record, VaArg, FnPtrArg)] pub struct Explicit { #[offset(0)] pub v: i32, #[offset(4)] pub inner: Inner, #[offset(8)] - pub arr: Box<[i32]>, + pub arr: Value>, } impl Explicit { pub fn new(v: i32) -> Self { @@ -41,7 +41,20 @@ impl Explicit { inner: Inner { x: ((*v.borrow()) * 10), }, - arr: Box::new([(*v.borrow()), ((*v.borrow()) + 1)]), + arr: Rc::new(RefCell::new(Box::new([(*v.borrow()), ((*v.borrow()) + 1)]))), + })); + let this: Ptr = __this.as_pointer(); + Rc::try_unwrap(__this).ok().unwrap().into_inner() + } +} +impl Clone for Explicit { + fn clone(&self) -> Self { + let __this: Value = Rc::new(RefCell::new(Self { + v: { self.v }, + inner: { self.inner.clone() }, + arr: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 2, _>( + |__i: usize| (*self.arr.borrow())[(__i) as usize], + )))), })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() @@ -52,7 +65,7 @@ impl Default for Explicit { Explicit { v: 0_i32, inner: ::default(), - arr: (0..2).map(|_| 0_i32).collect::>(), + arr: Rc::new(RefCell::new((0..2).map(|_| 0_i32).collect::>())), } } } @@ -67,32 +80,49 @@ impl ByteRepr for Explicit { fn to_bytes(&self, buf: &mut [u8]) { self.v.to_bytes(&mut buf[0..4]); self.inner.to_bytes(&mut buf[4..8]); - self.arr.to_bytes(&mut buf[8..16]); + (*self.arr.borrow()).to_bytes(&mut buf[8..16]); } fn from_bytes(buf: &[u8]) -> Self { Self { v: ::from_bytes(&buf[0..4]), inner: ::from_bytes(&buf[4..8]), - arr: >::from_bytes(&buf[8..16]), + arr: Rc::new(RefCell::new(>::from_bytes(&buf[8..16]))), } } } +<<<<<<< HEAD #[derive(Clone, Record, VaArg, FnPtrArg)] >>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) +======= +#[derive(Record, VaArg, FnPtrArg)] +>>>>>>> 349ab5a6 (Keep Box fields boxed) pub struct Implicit { #[offset(0)] pub v: i32, #[offset(4)] pub inner: Inner, #[offset(8)] - pub arr: Box<[i32]>, + pub arr: Value>, +} +impl Clone for Implicit { + fn clone(&self) -> Self { + let __this: Value = Rc::new(RefCell::new(Self { + v: { self.v }, + inner: { self.inner.clone() }, + arr: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 2, _>( + |__i: usize| (*self.arr.borrow())[(__i) as usize], + )))), + })); + let this: Ptr = __this.as_pointer(); + Rc::try_unwrap(__this).ok().unwrap().into_inner() + } } impl Default for Implicit { fn default() -> Self { Implicit { v: 0_i32, inner: ::default(), - arr: (0..2).map(|_| 0_i32).collect::>(), + arr: Rc::new(RefCell::new((0..2).map(|_| 0_i32).collect::>())), } } } @@ -103,13 +133,13 @@ impl ByteRepr for Implicit { fn to_bytes(&self, buf: &mut [u8]) { self.v.to_bytes(&mut buf[0..4]); self.inner.to_bytes(&mut buf[4..8]); - self.arr.to_bytes(&mut buf[8..16]); + (*self.arr.borrow()).to_bytes(&mut buf[8..16]); } fn from_bytes(buf: &[u8]) -> Self { Self { v: ::from_bytes(&buf[0..4]), inner: ::from_bytes(&buf[4..8]), - arr: >::from_bytes(&buf[8..16]), + arr: Rc::new(RefCell::new(>::from_bytes(&buf[8..16]))), } } } @@ -213,7 +243,7 @@ pub struct Buffer { #[offset(48)] pub n: i32, #[offset(52)] - pub arr: Box<[i32]>, + pub arr: Value>, } impl Buffer { pub fn new(n: i32) -> Self { @@ -225,7 +255,7 @@ impl Buffer { ])), rows: Rc::new(RefCell::new(Vec::new())), n: (*n.borrow()), - arr: Box::new([(*n.borrow()), ((*n.borrow()) + 1)]), + arr: Rc::new(RefCell::new(Box::new([(*n.borrow()), ((*n.borrow()) + 1)]))), })); let this: Ptr = __this.as_pointer(); { @@ -248,9 +278,9 @@ impl Buffer { }))) }, n: { _a0.with(|__s: &Buffer| __s.n) }, - arr: Box::new(std::array::from_fn::<_, 2, _>(|__i: usize| { - (*_a0.upgrade().deref()).arr[(__i) as usize] - })), + arr: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 2, _>( + |__i: usize| (*_a0.with(|__s: &Buffer| __s.arr.clone()).borrow())[(__i) as usize], + )))), })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() @@ -262,7 +292,7 @@ impl Default for Buffer { data: Rc::new(RefCell::new(Default::default())), rows: Rc::new(RefCell::new(Vec::new())), n: 0_i32, - arr: (0..2).map(|_| 0_i32).collect::>(), + arr: Rc::new(RefCell::new((0..2).map(|_| 0_i32).collect::>())), } } } @@ -275,17 +305,19 @@ impl ByteRepr for Buffer { 64 } fn to_bytes(&self, buf: &mut [u8]) { - self.data.to_bytes(&mut buf[0..24]); - self.rows.to_bytes(&mut buf[24..48]); + (*self.data.borrow()).to_bytes(&mut buf[0..24]); + (*self.rows.borrow()).to_bytes(&mut buf[24..48]); self.n.to_bytes(&mut buf[48..52]); - self.arr.to_bytes(&mut buf[52..60]); + (*self.arr.borrow()).to_bytes(&mut buf[52..60]); } fn from_bytes(buf: &[u8]) -> Self { Self { - data: >>::from_bytes(&buf[0..24]), - rows: >>>>::from_bytes(&buf[24..48]), + data: Rc::new(RefCell::new(>::from_bytes(&buf[0..24]))), + rows: Rc::new(RefCell::new(>>>::from_bytes( + &buf[24..48], + ))), n: ::from_bytes(&buf[48..52]), - arr: >::from_bytes(&buf[52..60]), + arr: Rc::new(RefCell::new(>::from_bytes(&buf[52..60]))), } } } @@ -297,7 +329,7 @@ pub struct Owner { #[offset(24)] pub n: i32, #[offset(28)] - pub arr: Box<[i32]>, + pub arr: Value>, #[offset(40)] pub p: Option>, } @@ -310,9 +342,9 @@ impl Owner { }))) }, n: { _a0.with(|__s: &Owner| __s.n) }, - arr: Box::new(std::array::from_fn::<_, 2, _>(|__i: usize| { - (*_a0.upgrade().deref()).arr[(__i) as usize] - })), + arr: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 2, _>( + |__i: usize| (*_a0.with(|__s: &Owner| __s.arr.clone()).borrow())[(__i) as usize], + )))), p: { _a0.with_mut(|__s: &mut Owner| __s.p.take()) }, })); let this: Ptr = __this.as_pointer(); @@ -324,7 +356,7 @@ impl Default for Owner { Owner { data: Rc::new(RefCell::new(Default::default())), n: 0_i32, - arr: (0..2).map(|_| 0_i32).collect::>(), + arr: Rc::new(RefCell::new((0..2).map(|_| 0_i32).collect::>())), p: None, } } @@ -338,16 +370,16 @@ impl ByteRepr for Owner { 48 } fn to_bytes(&self, buf: &mut [u8]) { - self.data.to_bytes(&mut buf[0..24]); + (*self.data.borrow()).to_bytes(&mut buf[0..24]); self.n.to_bytes(&mut buf[24..28]); - self.arr.to_bytes(&mut buf[28..36]); + (*self.arr.borrow()).to_bytes(&mut buf[28..36]); self.p.to_bytes(&mut buf[40..48]); } fn from_bytes(buf: &[u8]) -> Self { Self { - data: >>::from_bytes(&buf[0..24]), + data: Rc::new(RefCell::new(>::from_bytes(&buf[0..24]))), n: ::from_bytes(&buf[24..28]), - arr: >::from_bytes(&buf[28..36]), + arr: Rc::new(RefCell::new(>::from_bytes(&buf[28..36]))), p: >>::from_bytes(&buf[40..48]), } } @@ -412,11 +444,11 @@ pub fn same_0(a: Ptr, b: Ptr) -> bool { let _lhs = a.with(|__s: &Explicit| __s.inner.x); _lhs == b.with(|__s: &Explicit| __s.inner.x) })) && ({ - let _lhs = a.with(|__s: &Explicit| __s.arr[(0) as usize]); - _lhs == b.with(|__s: &Explicit| __s.arr[(0) as usize]) + let _lhs = a.with(|__s: &Explicit| (*__s.arr.borrow())[(0) as usize]); + _lhs == b.with(|__s: &Explicit| (*__s.arr.borrow())[(0) as usize]) })) && ({ - let _lhs = a.with(|__s: &Explicit| __s.arr[(1) as usize]); - _lhs == b.with(|__s: &Explicit| __s.arr[(1) as usize]) + let _lhs = a.with(|__s: &Explicit| (*__s.arr.borrow())[(1) as usize]); + _lhs == b.with(|__s: &Explicit| (*__s.arr.borrow())[(1) as usize]) }); } pub fn main() { @@ -460,24 +492,24 @@ fn main_0() -> i32 { let i: Value = Rc::new(RefCell::new(Implicit { v: 5, inner: Inner { x: 50 }, - arr: Box::new([5, 6]), + arr: Rc::new(RefCell::new(Box::new([5, 6]))), })); let j: Value = Rc::new(RefCell::new((*i.borrow()).clone())); let k: Value = Rc::new(RefCell::new((*i.borrow()).clone())); assert!( (({ (*j.borrow()).v } == 5) && ({ (*j.borrow()).inner.x } == 50)) - && ((*j.borrow()).arr[(1) as usize] == 6) + && ((*(*j.borrow()).arr.borrow())[(1) as usize] == 6) ); assert!(({ (*i.borrow()).v } == 5) && ({ (*k.borrow()).v } == 5)); let l: Value = Rc::new(RefCell::new(Implicit { v: 0, inner: Inner { x: 0 }, - arr: Box::new([0, 0]), + arr: Rc::new(RefCell::new(Box::new([0, 0]))), })); (*l.borrow_mut()) = (*j.borrow()).clone(); assert!( (({ (*l.borrow()).v } == 5) && ({ (*l.borrow()).inner.x } == 50)) - && ((*l.borrow()).arr[(0) as usize] == 5) + && ((*(*l.borrow()).arr.borrow())[(0) as usize] == 5) ); let vec_: Value> = Rc::new(RefCell::new(Vec::new())); { @@ -537,7 +569,7 @@ fn main_0() -> i32 { ({ BufferImpl::move_assign(&r.as_pointer(), q.as_pointer()) }); assert!( ((({ (*r.borrow()).n } == 3) && ((*(*r.borrow()).data.borrow()).len() == 3_usize)) - && ((*r.borrow()).arr[(1) as usize] == 4)) + && ((*(*r.borrow()).arr.borrow())[(1) as usize] == 4)) && ((*(*q.borrow()).data.borrow()).is_empty()) ); assert!( @@ -579,8 +611,8 @@ fn main_0() -> i32 { let o1: Value = Rc::new(RefCell::new(::default())); (*(*o1.borrow()).data.borrow_mut()).push(5); (*o1.borrow_mut()).n = 5; - (*o1.borrow_mut()).arr[(0) as usize] = 5; - (*o1.borrow_mut()).arr[(1) as usize] = 6; + (*(*o1.borrow()).arr.borrow_mut())[(0) as usize] = 5; + (*(*o1.borrow()).arr.borrow_mut())[(1) as usize] = 6; { let _p: Ptr<_> = Ptr::alloc(7); (*o1.borrow_mut()).p = _p.to_owned_opt() @@ -588,7 +620,7 @@ fn main_0() -> i32 { let o2: Value = Rc::new(RefCell::new(Owner::move_from({ o1.as_pointer() }))); assert!( ((({ (*o2.borrow()).n } == 5) && ((*(*o2.borrow()).data.borrow()).len() == 1_usize)) - && ((*o2.borrow()).arr[(1) as usize] == 6)) + && ((*(*o2.borrow()).arr.borrow())[(1) as usize] == 6)) && ((*(*o2.borrow()).p.as_ref().unwrap().borrow()) == 7) ); assert!( @@ -599,7 +631,7 @@ fn main_0() -> i32 { assert!( ((({ (*o3.borrow()).n } == 5) && ({ (*(*o3.borrow()).data.borrow())[(0_usize) as usize] } == 5)) - && ((*o3.borrow()).arr[(0) as usize] == 5)) + && ((*(*o3.borrow()).arr.borrow())[(0) as usize] == 5)) && ((*(*o3.borrow()).p.as_ref().unwrap().borrow()) == 7) ); assert!( @@ -622,7 +654,8 @@ fn main_0() -> i32 { let _dtor_h3 = ScopedDestructor::new(&h3, |__p| __p.destructor()); ({ HolderImpl::move_assign(&h3.as_pointer(), h2.as_pointer()) }); assert!( - ((({ (*h3.borrow()).inner.x } == 4) && ((*h3.borrow()).e.arr[(1) as usize] == 5)) + ((({ (*h3.borrow()).inner.x } == 4) + && ((*(*h3.borrow()).e.arr.borrow())[(1) as usize] == 5)) && ((*(*h3.borrow()).p.as_ref().unwrap().borrow()) == 9)) && (((*h2.borrow()).p.as_pointer()).is_null()) ); @@ -640,11 +673,13 @@ impl BufferImpl for Ptr { let __rhs = _a0.with(|__s: &Buffer| __s.n); (*self).with_mut(|__s: &mut Buffer| __s.n = __rhs); { - ((field_ptr!((*self), arr)) as Ptr).to_any().memcpy( - &((field_ptr!(_a0, arr)) as Ptr).to_any(), - 8_usize as usize, - ); - ((field_ptr!((*self), arr)) as Ptr).to_any() + (((*self).with(|__s: &Buffer| __s.arr.as_pointer())) as Ptr) + .to_any() + .memcpy( + &((_a0.with(|__s: &Buffer| __s.arr.as_pointer())) as Ptr).to_any(), + 8_usize as usize, + ); + (((*self).with(|__s: &Buffer| __s.arr.as_pointer())) as Ptr).to_any() }; return (*self).clone(); } @@ -676,7 +711,7 @@ impl HolderImpl for Ptr { (*self).with_mut(|__s: &mut Holder| __s.inner = __rhs); let __rhs = _a0.with(|__s: &Holder| (__s.e).clone()); (*self).with_mut(|__s: &mut Holder| __s.e = __rhs); - ((field_ptr!((*self), p) as Ptr>>) as Ptr>>) + (field_ptr!((*self), p) as Ptr>>) .write(_a0.with_mut(|__s: &mut Holder| __s.p.take())); return (*self).clone(); } @@ -694,13 +729,15 @@ impl OwnerImpl for Ptr { let __rhs = _a0.with(|__s: &Owner| __s.n); (*self).with_mut(|__s: &mut Owner| __s.n = __rhs); { - ((field_ptr!((*self), arr)) as Ptr).to_any().memcpy( - &((field_ptr!(_a0, arr)) as Ptr).to_any(), - 8_usize as usize, - ); - ((field_ptr!((*self), arr)) as Ptr).to_any() + (((*self).with(|__s: &Owner| __s.arr.as_pointer())) as Ptr) + .to_any() + .memcpy( + &((_a0.with(|__s: &Owner| __s.arr.as_pointer())) as Ptr).to_any(), + 8_usize as usize, + ); + (((*self).with(|__s: &Owner| __s.arr.as_pointer())) as Ptr).to_any() }; - ((field_ptr!((*self), p) as Ptr>>) as Ptr>>) + (field_ptr!((*self), p) as Ptr>>) .write(_a0.with_mut(|__s: &mut Owner| __s.p.take())); return (*self).clone(); } diff --git a/tests/unit/out/refcount/default.rs b/tests/unit/out/refcount/default.rs index 32a73a0fac..3e4443a3b7 100644 --- a/tests/unit/out/refcount/default.rs +++ b/tests/unit/out/refcount/default.rs @@ -6,30 +6,51 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Clone, Record, VaArg, FnPtrArg)] +#[derive(Record, VaArg, FnPtrArg)] pub struct Pointers { #[offset(0)] pub x1: Ptr, #[offset(8)] pub x2: Ptr, #[offset(16)] - pub x3: Box<[Ptr]>, + pub x3: Value]>>, #[offset(56)] - pub x4: Box<[Ptr]>, + pub x4: Value]>>, #[offset(136)] pub x5: i32, } +impl Clone for Pointers { + fn clone(&self) -> Self { + let __this: Value = Rc::new(RefCell::new(Self { + x1: { self.x1.clone() }, + x2: { self.x2.clone() }, + x3: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 5, _>( + |__i: usize| ((*self.x3.borrow())[(__i) as usize]).clone(), + )))), + x4: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 10, _>( + |__i: usize| ((*self.x4.borrow())[(__i) as usize]).clone(), + )))), + x5: { self.x5 }, + })); + let this: Ptr = __this.as_pointer(); + Rc::try_unwrap(__this).ok().unwrap().into_inner() + } +} impl Default for Pointers { fn default() -> Self { Pointers { x1: Ptr::::null(), x2: Ptr::::null(), - x3: (0..5) - .map(|_| Ptr::::null()) - .collect::]>>(), - x4: (0..10) - .map(|_| Ptr::::null()) - .collect::]>>(), + x3: Rc::new(RefCell::new( + (0..5) + .map(|_| Ptr::::null()) + .collect::]>>(), + )), + x4: Rc::new(RefCell::new( + (0..10) + .map(|_| Ptr::::null()) + .collect::]>>(), + )), x5: 0_i32, } } @@ -41,16 +62,16 @@ impl ByteRepr for Pointers { fn to_bytes(&self, buf: &mut [u8]) { self.x1.to_bytes(&mut buf[0..8]); self.x2.to_bytes(&mut buf[8..16]); - self.x3.to_bytes(&mut buf[16..56]); - self.x4.to_bytes(&mut buf[56..136]); + (*self.x3.borrow()).to_bytes(&mut buf[16..56]); + (*self.x4.borrow()).to_bytes(&mut buf[56..136]); self.x5.to_bytes(&mut buf[136..140]); } fn from_bytes(buf: &[u8]) -> Self { Self { x1: >::from_bytes(&buf[0..8]), x2: >::from_bytes(&buf[8..16]), - x3: ]>>::from_bytes(&buf[16..56]), - x4: ]>>::from_bytes(&buf[56..136]), + x3: Rc::new(RefCell::new(]>>::from_bytes(&buf[16..56]))), + x4: Rc::new(RefCell::new(]>>::from_bytes(&buf[56..136]))), x5: ::from_bytes(&buf[136..140]), } } diff --git a/tests/unit/out/refcount/default_in_statics.rs b/tests/unit/out/refcount/default_in_statics.rs index 29899c9991..622105a847 100644 --- a/tests/unit/out/refcount/default_in_statics.rs +++ b/tests/unit/out/refcount/default_in_statics.rs @@ -28,14 +28,14 @@ impl ByteRepr for Inner { } } } -#[derive(Clone, Record, VaArg, FnPtrArg)] +#[derive(Record, VaArg, FnPtrArg)] pub struct Outer { #[offset(0)] pub p1: Ptr, #[offset(8)] pub p2: Ptr, #[offset(16)] - pub arr: Box<[Ptr]>, + pub arr: Value]>>, #[offset(40)] pub cp: Ptr, #[offset(48)] @@ -47,14 +47,34 @@ pub struct Outer { #[offset(80)] pub fn_: FnPtr i32>, } +impl Clone for Outer { + fn clone(&self) -> Self { + let __this: Value = Rc::new(RefCell::new(Self { + p1: { self.p1.clone() }, + p2: { self.p2.clone() }, + arr: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 3, _>( + |__i: usize| ((*self.arr.borrow())[(__i) as usize]).clone(), + )))), + cp: { self.cp.clone() }, + pp: { self.pp.clone() }, + inner: { self.inner.clone() }, + x: { self.x }, + fn_: { self.fn_.clone() }, + })); + let this: Ptr = __this.as_pointer(); + Rc::try_unwrap(__this).ok().unwrap().into_inner() + } +} impl Default for Outer { fn default() -> Self { Outer { p1: Ptr::::null(), p2: Ptr::::null(), - arr: (0..3) - .map(|_| Ptr::::null()) - .collect::]>>(), + arr: Rc::new(RefCell::new( + (0..3) + .map(|_| Ptr::::null()) + .collect::]>>(), + )), cp: Ptr::::null(), pp: Ptr::>::null(), inner: ::default(), @@ -70,7 +90,7 @@ impl ByteRepr for Outer { fn to_bytes(&self, buf: &mut [u8]) { self.p1.to_bytes(&mut buf[0..8]); self.p2.to_bytes(&mut buf[8..16]); - self.arr.to_bytes(&mut buf[16..40]); + (*self.arr.borrow()).to_bytes(&mut buf[16..40]); self.cp.to_bytes(&mut buf[40..48]); self.pp.to_bytes(&mut buf[48..56]); self.inner.to_bytes(&mut buf[56..72]); @@ -81,7 +101,7 @@ impl ByteRepr for Outer { Self { p1: >::from_bytes(&buf[0..8]), p2: >::from_bytes(&buf[8..16]), - arr: ]>>::from_bytes(&buf[16..40]), + arr: Rc::new(RefCell::new(]>>::from_bytes(&buf[16..40]))), cp: >::from_bytes(&buf[40..48]), pp: >>::from_bytes(&buf[48..56]), inner: ::from_bytes(&buf[56..72]), @@ -204,7 +224,8 @@ fn main_0() -> i32 { let i: Value = Rc::new(RefCell::new(0)); 'loop_: while ((*i.borrow()) < 3) { assert!( - ((*static_outer_1.with(Value::clone).borrow()).arr[(*i.borrow()) as usize]).is_null() + ((*(*static_outer_1.with(Value::clone).borrow()).arr.borrow())[(*i.borrow()) as usize]) + .is_null() ); (*i.borrow_mut()).prefix_inc(); } diff --git a/tests/unit/out/refcount/destructor.rs b/tests/unit/out/refcount/destructor.rs index 7fd47d7a8f..cc249ada1b 100644 --- a/tests/unit/out/refcount/destructor.rs +++ b/tests/unit/out/refcount/destructor.rs @@ -79,16 +79,33 @@ impl ByteRepr for Outer { } } } +<<<<<<< HEAD #[derive(Clone, Record, VaArg, FnPtrArg)] >>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) +======= +#[derive(Record, VaArg, FnPtrArg)] +>>>>>>> 349ab5a6 (Keep Box fields boxed) pub struct ArrayMember { #[offset(0)] - pub items: Box<[S]>, + pub items: Value>, +} +impl Clone for ArrayMember { + fn clone(&self) -> Self { + let __this: Value = Rc::new(RefCell::new(Self { + items: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 3, _>( + |__i: usize| (*self.items.borrow())[(__i) as usize].clone(), + )))), + })); + let this: Ptr = __this.as_pointer(); + Rc::try_unwrap(__this).ok().unwrap().into_inner() + } } impl Default for ArrayMember { fn default() -> Self { ArrayMember { - items: (0..3).map(|_| ::default()).collect::>(), + items: Rc::new(RefCell::new( + (0..3).map(|_| ::default()).collect::>(), + )), } } } @@ -101,11 +118,11 @@ impl ByteRepr for ArrayMember { 3 } fn to_bytes(&self, buf: &mut [u8]) { - self.items.to_bytes(&mut buf[0..3]); + (*self.items.borrow()).to_bytes(&mut buf[0..3]); } fn from_bytes(buf: &[u8]) -> Self { Self { - items: >::from_bytes(&buf[0..3]), + items: Rc::new(RefCell::new(>::from_bytes(&buf[0..3]))), } } } @@ -356,7 +373,7 @@ pub trait ArrayMemberImpl { impl ArrayMemberImpl for Ptr { fn destructor(&self) { { - let __p: Ptr = field_ptr!(self, items); + let __p: Ptr = self.with(|__s| __s.items.as_pointer()); for __i in 0..__p.len() { SImpl::destructor(&__p.offset(__i as isize)); } diff --git a/tests/unit/out/refcount/field_array_elem_ptr.rs b/tests/unit/out/refcount/field_array_elem_ptr.rs new file mode 100644 index 0000000000..9c096cc4ba --- /dev/null +++ b/tests/unit/out/refcount/field_array_elem_ptr.rs @@ -0,0 +1,200 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] +pub struct Point { + #[offset(0)] + pub x: i32, + #[offset(4)] + pub y: i32, +} +impl ByteRepr for Point { + fn byte_size() -> usize { + 8 + } + fn to_bytes(&self, buf: &mut [u8]) { + self.x.to_bytes(&mut buf[0..4]); + self.y.to_bytes(&mut buf[4..8]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + x: ::from_bytes(&buf[0..4]), + y: ::from_bytes(&buf[4..8]), + } + } +} +#[derive(Record, VaArg, FnPtrArg)] +pub struct Shape { + #[offset(0)] + pub id: i32, + #[offset(4)] + pub coords: Value>, + #[offset(20)] + pub points: Value>, + #[offset(44)] + pub tail: i32, +} +impl Clone for Shape { + fn clone(&self) -> Self { + let __this: Value = Rc::new(RefCell::new(Self { + id: { self.id }, + coords: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 4, _>( + |__i: usize| (*self.coords.borrow())[(__i) as usize], + )))), + points: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 3, _>( + |__i: usize| (*self.points.borrow())[(__i) as usize].clone(), + )))), + tail: { self.tail }, + })); + let this: Ptr = __this.as_pointer(); + Rc::try_unwrap(__this).ok().unwrap().into_inner() + } +} +impl Default for Shape { + fn default() -> Self { + Shape { + id: 0_i32, + coords: Rc::new(RefCell::new((0..4).map(|_| 0_i32).collect::>())), + points: Rc::new(RefCell::new( + (0..3).map(|_| ::default()).collect::>(), + )), + tail: 0_i32, + } + } +} +impl ByteRepr for Shape { + fn byte_size() -> usize { + 48 + } + fn to_bytes(&self, buf: &mut [u8]) { + self.id.to_bytes(&mut buf[0..4]); + (*self.coords.borrow()).to_bytes(&mut buf[4..20]); + (*self.points.borrow()).to_bytes(&mut buf[20..44]); + self.tail.to_bytes(&mut buf[44..48]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + id: ::from_bytes(&buf[0..4]), + coords: Rc::new(RefCell::new(>::from_bytes(&buf[4..20]))), + points: Rc::new(RefCell::new(>::from_bytes(&buf[20..44]))), + tail: ::from_bytes(&buf[44..48]), + } + } +} +pub fn sum_0(p: Ptr, n: i32) -> i32 { + let p: Value> = Rc::new(RefCell::new(p)); + let n: Value = Rc::new(RefCell::new(n)); + let s: Value = Rc::new(RefCell::new(0)); + let i: Value = Rc::new(RefCell::new(0)); + 'loop_: while ((*i.borrow()) < (*n.borrow())) { + let __rhs = ((*p.borrow()).offset((*i.borrow()) as isize).read()); + (*s.borrow_mut()) += __rhs; + (*i.borrow_mut()).prefix_inc(); + } + return (*s.borrow()); +} +pub fn set_y_1(p: Ptr, y: i32) { + let p: Value> = Rc::new(RefCell::new(p)); + let y: Value = Rc::new(RefCell::new(y)); + (*p.borrow()).with_mut(|__s: &mut Point| __s.y = (*y.borrow())); +} +pub fn main() { + __cpp2rust_init_globals(); + std::process::exit(main_0()); +} +fn main_0() -> i32 { + let s: Value = Rc::new(RefCell::new(Shape { + id: 1, + coords: Rc::new(RefCell::new(Box::new([10, 20, 30, 40]))), + points: Rc::new(RefCell::new(Box::new([ + Point { x: 1, y: 2 }, + Point { x: 3, y: 4 }, + Point { x: 5, y: 6 }, + ]))), + tail: 99, + })); + let c: Value> = Rc::new(RefCell::new( + (({ (*s.borrow()).coords.as_pointer() } as Ptr).offset(1)), + )); + assert!((((*c.borrow()).read()) == 20)); + (*c.borrow()).write(21); + assert!(((*(*s.borrow()).coords.borrow())[(1) as usize] == 21)); + (*c.borrow_mut()) += 2; + assert!((((*c.borrow()).read()) == 40)); + assert!( + (((*c.borrow()).clone() - (({ (*s.borrow()).coords.as_pointer() } as Ptr).offset(0))) + as i64 + == 3_i64) + ); + assert!((((*c.borrow()).offset((-1_i32) as isize).read()) == 30)); + assert!((({ sum_0(({ (*s.borrow()).coords.as_pointer() } as Ptr::), 4,) }) == 101)); + assert!( + (({ + sum_0( + (({ (*s.borrow()).coords.as_pointer() } as Ptr).offset(2)), + 2, + ) + }) == 70) + ); + let p: Value> = Rc::new(RefCell::new( + (({ (*s.borrow()).points.as_pointer() } as Ptr).offset(1)), + )); + assert!(((*p.borrow()).with(|__s: &Point| __s.x) == 3)); + ({ set_y_1((*p.borrow()).offset((1) as isize), 60) }); + assert!(({ (*(*s.borrow()).points.borrow())[(2) as usize].y } == 60)); + let py: Value> = Rc::new(RefCell::new( + (field_ptr!( + ({ (*s.borrow()).points.as_pointer() } as Ptr).offset(0), + y + )), + )); + (*py.borrow()).write(7); + assert!(({ (*(*s.borrow()).points.borrow())[(0) as usize].y } == 7)); + let px: Value> = Rc::new(RefCell::new( + (field_ptr!(((*p.borrow()).offset((1) as isize)), x)), + )); + { + let _ptr = (*px.borrow()).clone(); + _ptr.write(_ptr.read() + 50) + }; + assert!(({ (*(*s.borrow()).points.borrow())[(2) as usize].x } == 55)); + let sp: Value> = Rc::new(RefCell::new((s.as_pointer()))); + let d: Value> = Rc::new(RefCell::new( + ((*sp.borrow()).with(|__s: &Shape| __s.coords.as_pointer()) as Ptr) + .offset((3) as isize), + )); + (*d.borrow()).write(41); + assert!(((*(*s.borrow()).coords.borrow())[(3) as usize] == 41)); + let __rhs = { + let _lhs = (*sp.borrow()).with(|__s: &Shape| (*__s.coords.borrow())[(0) as usize]); + _lhs + (*sp.borrow()).with(|__s: &Shape| (*__s.points.borrow())[(0) as usize].x) + }; + (*sp.borrow()).with(|__s: &Shape| (*__s.points.borrow_mut())[(1) as usize].y = __rhs); + assert!(({ (*(*s.borrow()).points.borrow())[(1) as usize].y } == 11)); + let q: Value> = Rc::new(RefCell::new( + ((*sp.borrow()).with(|__s: &Shape| __s.points.as_pointer()) as Ptr), + )); + (*q.borrow()) + .offset((2) as isize) + .with_mut(|__s: &mut Point| __s.x = 8); + assert!(((*sp.borrow()).with(|__s: &Shape| (*__s.points.borrow())[(2) as usize].x) == 8)); + let t: Value = Rc::new(RefCell::new((*s.borrow()).clone())); + (*(*t.borrow()).coords.borrow_mut())[(0) as usize] = 0; + (*(*t.borrow()).points.borrow_mut())[(0) as usize].x = 0; + assert!( + ((*(*s.borrow()).coords.borrow())[(0) as usize] == 10) + && ({ (*(*s.borrow()).points.borrow())[(0) as usize].x } == 1) + ); + assert!( + (((*(*t.borrow()).coords.borrow())[(1) as usize] == 21) + && ({ (*(*t.borrow()).points.borrow())[(2) as usize].y } == 60)) + && ({ (*t.borrow()).tail } == 99) + ); + return 0; +} +pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/field_pointers.rs b/tests/unit/out/refcount/field_pointers.rs index 5bdb4adaf1..30086bf9f3 100644 --- a/tests/unit/out/refcount/field_pointers.rs +++ b/tests/unit/out/refcount/field_pointers.rs @@ -6,18 +6,30 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Clone, Record, VaArg, FnPtrArg)] +#[derive(Record, VaArg, FnPtrArg)] pub struct Inner { #[offset(0)] pub a: i32, #[offset(4)] - pub name: Box<[u8]>, + pub name: Value>, +} +impl Clone for Inner { + fn clone(&self) -> Self { + let __this: Value = Rc::new(RefCell::new(Self { + a: { self.a }, + name: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 8, _>( + |__i: usize| (*self.name.borrow())[(__i) as usize], + )))), + })); + let this: Ptr = __this.as_pointer(); + Rc::try_unwrap(__this).ok().unwrap().into_inner() + } } impl Default for Inner { fn default() -> Self { Inner { a: 0_i32, - name: (0..8).map(|_| 0_u8).collect::>(), + name: Rc::new(RefCell::new((0..8).map(|_| 0_u8).collect::>())), } } } @@ -27,12 +39,12 @@ impl ByteRepr for Inner { } fn to_bytes(&self, buf: &mut [u8]) { self.a.to_bytes(&mut buf[0..4]); - self.name.to_bytes(&mut buf[4..12]); + (*self.name.borrow()).to_bytes(&mut buf[4..12]); } fn from_bytes(buf: &[u8]) -> Self { Self { a: ::from_bytes(&buf[0..4]), - name: >::from_bytes(&buf[4..12]), + name: Rc::new(RefCell::new(>::from_bytes(&buf[4..12]))), } } } @@ -65,27 +77,27 @@ pub struct Outer { #[offset(4)] pub inner: Inner, #[offset(16)] - pub items: Box<[Inner]>, + pub items: Value>, #[offset(56)] pub v: Value>, #[offset(80)] pub cursor: Ptr, #[offset(88)] - pub buf: Box<[i32]>, + pub buf: Value>, } impl Clone for Outer { fn clone(&self) -> Self { let __this: Value = Rc::new(RefCell::new(Self { x: { self.x }, inner: { self.inner.clone() }, - items: Box::new(std::array::from_fn::<_, 3, _>(|__i: usize| { - self.items[(__i) as usize].clone() - })), + items: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 3, _>( + |__i: usize| (*self.items.borrow())[(__i) as usize].clone(), + )))), v: { Rc::new(RefCell::new((*self.v.borrow()).clone())) }, cursor: { self.cursor.clone() }, - buf: Box::new(std::array::from_fn::<_, 4, _>(|__i: usize| { - self.buf[(__i) as usize] - })), + buf: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 4, _>( + |__i: usize| (*self.buf.borrow())[(__i) as usize], + )))), })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() @@ -96,10 +108,12 @@ impl Default for Outer { Outer { x: 0_i32, inner: ::default(), - items: (0..3).map(|_| ::default()).collect::>(), + items: Rc::new(RefCell::new( + (0..3).map(|_| ::default()).collect::>(), + )), v: Rc::new(RefCell::new(Default::default())), cursor: Ptr::::null(), - buf: (0..4).map(|_| 0_i32).collect::>(), + buf: Rc::new(RefCell::new((0..4).map(|_| 0_i32).collect::>())), } } } @@ -110,19 +124,19 @@ impl ByteRepr for Outer { fn to_bytes(&self, buf: &mut [u8]) { self.x.to_bytes(&mut buf[0..4]); self.inner.to_bytes(&mut buf[4..16]); - self.items.to_bytes(&mut buf[16..52]); - self.v.to_bytes(&mut buf[56..80]); + (*self.items.borrow()).to_bytes(&mut buf[16..52]); + (*self.v.borrow()).to_bytes(&mut buf[56..80]); self.cursor.to_bytes(&mut buf[80..88]); - self.buf.to_bytes(&mut buf[88..104]); + (*self.buf.borrow()).to_bytes(&mut buf[88..104]); } fn from_bytes(buf: &[u8]) -> Self { Self { x: ::from_bytes(&buf[0..4]), inner: ::from_bytes(&buf[4..16]), - items: >::from_bytes(&buf[16..52]), - v: >>::from_bytes(&buf[56..80]), + items: Rc::new(RefCell::new(>::from_bytes(&buf[16..52]))), + v: Rc::new(RefCell::new(>::from_bytes(&buf[56..80]))), cursor: >::from_bytes(&buf[80..88]), - buf: >::from_bytes(&buf[88..104]), + buf: Rc::new(RefCell::new(>::from_bytes(&buf[88..104]))), } } } @@ -154,16 +168,22 @@ fn main_0() -> i32 { }); assert!(({ (*o.borrow()).inner.a } == 3)); let pi: Value> = Rc::new(RefCell::new( - (field_ptr!((field_ptr!(o, items) as Ptr).offset(1), a)), + (field_ptr!( + ({ (*o.borrow()).items.as_pointer() } as Ptr).offset(1), + a + )), )); (*pi.borrow()).write(4); - assert!(({ (*o.borrow()).items[(1) as usize].a } == 4)); + assert!(({ (*(*o.borrow()).items.borrow())[(1) as usize].a } == 4)); assert!({ - let _lhs = (field_ptr!((field_ptr!(o, items) as Ptr).offset(0), a)); + let _lhs = (field_ptr!( + ({ (*o.borrow()).items.as_pointer() } as Ptr).offset(0), + a + )); _lhs != (*pi.borrow()).clone() }); let name: Value> = Rc::new(RefCell::new( - (field_ptr!(field_ptr!(o, inner), name) as Ptr), + ({ (*o.borrow()).inner.name.as_pointer() } as Ptr), )); let i: Value = Rc::new(RefCell::new(0)); 'loop_: while ((*i.borrow()) < 3) { @@ -172,16 +192,16 @@ fn main_0() -> i32 { (*i.borrow_mut()).prefix_inc(); } assert!( - ((field_ptr!(field_ptr!(o, inner), name) as Ptr::) + (({ (*o.borrow()).inner.name.as_pointer() } as Ptr::) .to_c_string_iterator() .count() == 3_usize) ); assert!({ let _lhs = (*name.borrow()).offset((3) as isize); - _lhs == ((field_ptr!(field_ptr!(o, inner), name) as Ptr).offset(3)) + _lhs == (({ (*o.borrow()).inner.name.as_pointer() } as Ptr).offset(3)) }); - let __rhs = ((field_ptr!(o, buf) as Ptr).offset(1)); + let __rhs = (({ (*o.borrow()).buf.as_pointer() } as Ptr).offset(1)); (*o.borrow_mut()).cursor = __rhs; { (*o.borrow()).cursor.clone() }.write(5); { @@ -189,10 +209,10 @@ fn main_0() -> i32 { _ptr.write(_ptr.read() + 1) }; assert!( - ((*o.borrow()).buf[(1) as usize] == 6) + ((*(*o.borrow()).buf.borrow())[(1) as usize] == 6) && ({ let _lhs = { (*o.borrow()).cursor.clone() }; - _lhs == ((field_ptr!(o, buf) as Ptr).offset(2)) + _lhs == (({ (*o.borrow()).buf.as_pointer() } as Ptr).offset(2)) }) ); (*o.borrow_mut()).x = 0; @@ -212,18 +232,32 @@ fn main_0() -> i32 { assert!((({ OuterImpl::sum(&o.as_pointer(),) }) == 7)); let y: Value = Rc::new(RefCell::new(0)); { - ((field_ptr!((field_ptr!(o, items) as Ptr).offset(2), a)) as Ptr) + ((field_ptr!( + ({ (*o.borrow()).items.as_pointer() } as Ptr).offset(2), + a + )) as Ptr) .to_any() .memcpy( - &((field_ptr!((field_ptr!(o, items) as Ptr).offset(1), a)) as Ptr) + &((field_ptr!( + ({ (*o.borrow()).items.as_pointer() } as Ptr).offset(1), + a + )) as Ptr) .to_any(), ::std::mem::size_of::() as usize, ); - ((field_ptr!((field_ptr!(o, items) as Ptr).offset(2), a)) as Ptr).to_any() + ((field_ptr!( + ({ (*o.borrow()).items.as_pointer() } as Ptr).offset(2), + a + )) as Ptr) + .to_any() }; { ((y.as_pointer()) as Ptr).to_any().memcpy( - &((field_ptr!((field_ptr!(o, items) as Ptr).offset(2), a)) as Ptr).to_any(), + &((field_ptr!( + ({ (*o.borrow()).items.as_pointer() } as Ptr).offset(2), + a + )) as Ptr) + .to_any(), ::std::mem::size_of::() as usize, ); ((y.as_pointer()) as Ptr).to_any() @@ -261,7 +295,7 @@ impl OuterImpl for Ptr { fn next(&self) -> i32 { return ({ let __idx = (*self).with_mut(|__s: &mut Outer| __s.x.postfix_inc()); - (*(*self).upgrade().deref()).buf[(__idx) as usize] + (*(*self).with(|__s: &Outer| __s.buf.clone()).borrow())[(__idx) as usize] }); } fn sum(&self) -> i32 { diff --git a/tests/unit/out/refcount/foreach_disjoint_field_borrow.rs b/tests/unit/out/refcount/foreach_disjoint_field_borrow.rs index 80d298191e..6b89520e59 100644 --- a/tests/unit/out/refcount/foreach_disjoint_field_borrow.rs +++ b/tests/unit/out/refcount/foreach_disjoint_field_borrow.rs @@ -31,12 +31,12 @@ impl ByteRepr for S { 32 } fn to_bytes(&self, buf: &mut [u8]) { - self.v.to_bytes(&mut buf[0..24]); + (*self.v.borrow()).to_bytes(&mut buf[0..24]); self.a.to_bytes(&mut buf[24..28]); } fn from_bytes(buf: &[u8]) -> Self { Self { - v: >>::from_bytes(&buf[0..24]), + v: Rc::new(RefCell::new(>::from_bytes(&buf[0..24]))), a: ::from_bytes(&buf[24..28]), } } diff --git a/tests/unit/out/refcount/huffman.rs b/tests/unit/out/refcount/huffman.rs index 81a244b584..c2ee25162b 100644 --- a/tests/unit/out/refcount/huffman.rs +++ b/tests/unit/out/refcount/huffman.rs @@ -500,13 +500,11 @@ impl MinHeapImpl for Ptr { (*self).with_mut(|__s: &mut MinHeap| __s.size = __rhs); let __rhs = _a0.with(|__s: &MinHeap| __s.capacity); (*self).with_mut(|__s: &mut MinHeap| __s.capacity = __rhs); - ((field_ptr!((*self), arr) as Ptr]>>>>) - as Ptr]>>>>) + (field_ptr!((*self), arr) as Ptr]>>>>) .write(_a0.with_mut(|__s: &mut MinHeap| __s.arr.take())); let __rhs = _a0.with(|__s: &MinHeap| __s.next); (*self).with_mut(|__s: &mut MinHeap| __s.next = __rhs); - ((field_ptr!((*self), alloc) as Ptr>>>) - as Ptr>>>) + (field_ptr!((*self), alloc) as Ptr>>>) .write(_a0.with_mut(|__s: &mut MinHeap| __s.alloc.take())); return (*self).clone(); } diff --git a/tests/unit/out/refcount/implicit_autoref.rs b/tests/unit/out/refcount/implicit_autoref.rs index f6369fb362..ed9cc9f18c 100644 --- a/tests/unit/out/refcount/implicit_autoref.rs +++ b/tests/unit/out/refcount/implicit_autoref.rs @@ -28,11 +28,11 @@ impl ByteRepr for Holder { 24 } fn to_bytes(&self, buf: &mut [u8]) { - self.v.to_bytes(&mut buf[0..24]); + (*self.v.borrow()).to_bytes(&mut buf[0..24]); } fn from_bytes(buf: &[u8]) -> Self { Self { - v: >>::from_bytes(&buf[0..24]), + v: Rc::new(RefCell::new(>::from_bytes(&buf[0..24]))), } } } diff --git a/tests/unit/out/refcount/kruskal.rs b/tests/unit/out/refcount/kruskal.rs index c1cc99b5d0..508cc3ac68 100644 --- a/tests/unit/out/refcount/kruskal.rs +++ b/tests/unit/out/refcount/kruskal.rs @@ -499,11 +499,9 @@ impl DisjointSetImpl for Ptr { } } fn move_assign(&self, _a0: Ptr) -> Ptr { - ((field_ptr!((*self), rank) as Ptr>>>) - as Ptr>>>) + (field_ptr!((*self), rank) as Ptr>>>) .write(_a0.with_mut(|__s: &mut DisjointSet| __s.rank.take())); - ((field_ptr!((*self), parent) as Ptr>>>) - as Ptr>>>) + (field_ptr!((*self), parent) as Ptr>>>) .write(_a0.with_mut(|__s: &mut DisjointSet| __s.parent.take())); let __rhs = _a0.with(|__s: &DisjointSet| __s.n); (*self).with_mut(|__s: &mut DisjointSet| __s.n = __rhs); @@ -515,8 +513,7 @@ pub trait GraphImpl { } impl GraphImpl for Ptr { fn move_assign(&self, _a0: Ptr) -> Ptr { - ((field_ptr!((*self), edges) as Ptr>>>) - as Ptr>>>) + (field_ptr!((*self), edges) as Ptr>>>) .write(_a0.with_mut(|__s: &mut Graph| __s.edges.take())); let __rhs = _a0.with(|__s: &Graph| __s.V); (*self).with_mut(|__s: &mut Graph| __s.V = __rhs); diff --git a/tests/unit/out/refcount/libc_struct_without_default.rs b/tests/unit/out/refcount/libc_struct_without_default.rs index 86f43b2b9e..474d6683bd 100644 --- a/tests/unit/out/refcount/libc_struct_without_default.rs +++ b/tests/unit/out/refcount/libc_struct_without_default.rs @@ -42,13 +42,13 @@ impl ByteRepr for UserDefined { 32 } fn to_bytes(&self, buf: &mut [u8]) { - self.a.to_bytes(&mut buf[0..4]); - self.v.to_bytes(&mut buf[8..32]); + (*self.a.borrow()).to_bytes(&mut buf[0..4]); + (*self.v.borrow()).to_bytes(&mut buf[8..32]); } fn from_bytes(buf: &[u8]) -> Self { Self { - a: >>::from_bytes(&buf[0..4]), - v: >>::from_bytes(&buf[8..32]), + a: Rc::new(RefCell::new(>::from_bytes(&buf[0..4]))), + v: Rc::new(RefCell::new(>::from_bytes(&buf[8..32]))), } } } diff --git a/tests/unit/out/refcount/offsetof.rs b/tests/unit/out/refcount/offsetof.rs index 99f21611a4..4b267ef75e 100644 --- a/tests/unit/out/refcount/offsetof.rs +++ b/tests/unit/out/refcount/offsetof.rs @@ -32,18 +32,30 @@ impl ByteRepr for Layout { } } } -#[derive(Clone, Record, VaArg, FnPtrArg)] +#[derive(Record, VaArg, FnPtrArg)] pub struct Frame { #[offset(0)] pub tag: u16, #[offset(2)] - pub body: Box<[u8]>, + pub body: Value>, +} +impl Clone for Frame { + fn clone(&self) -> Self { + let __this: Value = Rc::new(RefCell::new(Self { + tag: { self.tag }, + body: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 64, _>( + |__i: usize| (*self.body.borrow())[(__i) as usize], + )))), + })); + let this: Ptr = __this.as_pointer(); + Rc::try_unwrap(__this).ok().unwrap().into_inner() + } } impl Default for Frame { fn default() -> Self { Frame { tag: 0_u16, - body: (0..64).map(|_| 0_u8).collect::>(), + body: Rc::new(RefCell::new((0..64).map(|_| 0_u8).collect::>())), } } } @@ -53,12 +65,12 @@ impl ByteRepr for Frame { } fn to_bytes(&self, buf: &mut [u8]) { self.tag.to_bytes(&mut buf[0..2]); - self.body.to_bytes(&mut buf[2..66]); + (*self.body.borrow()).to_bytes(&mut buf[2..66]); } fn from_bytes(buf: &[u8]) -> Self { Self { tag: ::from_bytes(&buf[0..2]), - body: >::from_bytes(&buf[2..66]), + body: Rc::new(RefCell::new(>::from_bytes(&buf[2..66]))), } } } diff --git a/tests/unit/out/refcount/operator_member_pointer_free.rs b/tests/unit/out/refcount/operator_member_pointer_free.rs index fa542fff29..e11fadec20 100644 --- a/tests/unit/out/refcount/operator_member_pointer_free.rs +++ b/tests/unit/out/refcount/operator_member_pointer_free.rs @@ -24,17 +24,29 @@ impl ByteRepr for Inner { } } } -#[derive(Clone, Record, VaArg, FnPtrArg)] +#[derive(Record, VaArg, FnPtrArg)] pub struct S { #[offset(0)] - pub data: Box<[i32]>, + pub data: Value>, #[offset(12)] pub inner: Inner, } +impl Clone for S { + fn clone(&self) -> Self { + let __this: Value = Rc::new(RefCell::new(Self { + data: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 3, _>( + |__i: usize| (*self.data.borrow())[(__i) as usize], + )))), + inner: { self.inner.clone() }, + })); + let this: Ptr = __this.as_pointer(); + Rc::try_unwrap(__this).ok().unwrap().into_inner() + } +} impl Default for S { fn default() -> Self { S { - data: (0..3).map(|_| 0_i32).collect::>(), + data: Rc::new(RefCell::new((0..3).map(|_| 0_i32).collect::>())), inner: ::default(), } } @@ -44,12 +56,12 @@ impl ByteRepr for S { 16 } fn to_bytes(&self, buf: &mut [u8]) { - self.data.to_bytes(&mut buf[0..12]); + (*self.data.borrow()).to_bytes(&mut buf[0..12]); self.inner.to_bytes(&mut buf[12..16]); } fn from_bytes(buf: &[u8]) -> Self { Self { - data: >::from_bytes(&buf[0..12]), + data: Rc::new(RefCell::new(>::from_bytes(&buf[0..12]))), inner: ::from_bytes(&buf[12..16]), } } @@ -58,7 +70,7 @@ pub fn operator_deref_0(s: Ptr) -> Ptr { return field_ptr!(s, inner); } pub fn operator_addr_1(s: Ptr) -> Ptr { - return ((field_ptr!(s, data) as Ptr).offset(0)); + return ((s.with(|__s: &S| __s.data.as_pointer()) as Ptr).offset(0)); } pub fn main() { __cpp2rust_init_globals(); @@ -66,7 +78,7 @@ pub fn main() { } fn main_0() -> i32 { let s: Value = Rc::new(RefCell::new(S { - data: Box::new([1, 2, 3]), + data: Rc::new(RefCell::new(Box::new([1, 2, 3]))), inner: Inner { x: 9 }, })); assert!( @@ -91,7 +103,7 @@ fn main_0() -> i32 { )); assert!((((*p.borrow()).read()) == 1)); (*p.borrow()).write(5); - assert!(((*s.borrow()).data[(0) as usize] == 5)); + assert!(((*(*s.borrow()).data.borrow())[(0) as usize] == 5)); return 0; } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/operator_member_pointer_member.rs b/tests/unit/out/refcount/operator_member_pointer_member.rs index 15349cd985..fcd583a431 100644 --- a/tests/unit/out/refcount/operator_member_pointer_member.rs +++ b/tests/unit/out/refcount/operator_member_pointer_member.rs @@ -35,17 +35,29 @@ impl Table { thread_local!( pub static table_0: Value> = Rc::new(RefCell::new(Box::new([7, 8, 9]))); ); -#[derive(Clone, Record, VaArg, FnPtrArg)] +#[derive(Record, VaArg, FnPtrArg)] pub struct S { #[offset(0)] - pub data: Box<[i32]>, + pub data: Value>, #[offset(12)] pub inner: Inner, } +impl Clone for S { + fn clone(&self) -> Self { + let __this: Value = Rc::new(RefCell::new(Self { + data: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 3, _>( + |__i: usize| (*self.data.borrow())[(__i) as usize], + )))), + inner: { self.inner.clone() }, + })); + let this: Ptr = __this.as_pointer(); + Rc::try_unwrap(__this).ok().unwrap().into_inner() + } +} impl Default for S { fn default() -> Self { S { - data: (0..3).map(|_| 0_i32).collect::>(), + data: Rc::new(RefCell::new((0..3).map(|_| 0_i32).collect::>())), inner: ::default(), } } @@ -55,12 +67,12 @@ impl ByteRepr for S { 16 } fn to_bytes(&self, buf: &mut [u8]) { - self.data.to_bytes(&mut buf[0..12]); + (*self.data.borrow()).to_bytes(&mut buf[0..12]); self.inner.to_bytes(&mut buf[12..16]); } fn from_bytes(buf: &[u8]) -> Self { Self { - data: >::from_bytes(&buf[0..12]), + data: Rc::new(RefCell::new(>::from_bytes(&buf[0..12]))), inner: ::from_bytes(&buf[12..16]), } } @@ -71,7 +83,7 @@ pub fn main() { } fn main_0() -> i32 { let s: Value = Rc::new(RefCell::new(S { - data: Box::new([1, 2, 3]), + data: Rc::new(RefCell::new(Box::new([1, 2, 3]))), inner: Inner { x: 9 }, })); assert!(((({ SImpl::operator_index_1(&s.as_pointer(), 1,) }).read()) == 2)); @@ -97,7 +109,7 @@ fn main_0() -> i32 { let p: Value> = Rc::new(RefCell::new(({ SImpl::operator_addr(&s.as_pointer()) }))); assert!((((*p.borrow()).read()) == 1)); (*p.borrow()).write(5); - assert!(((*s.borrow()).data[(0) as usize] == 5)); + assert!(((*(*s.borrow()).data.borrow())[(0) as usize] == 5)); let t: Value
= Rc::new(RefCell::new(
::default())); assert!(((({ Table::operator_index(1,) }).read()) == 8)); ({ Table::operator_index(1) }).write(80); @@ -119,11 +131,11 @@ pub trait SImpl { impl SImpl for Ptr { fn operator_index_1(&self, i: i32) -> Ptr { let i: Value = Rc::new(RefCell::new(i)); - return (field_ptr!((*self), data) as Ptr).offset((*i.borrow())); + return ((*self).with(|__s: &S| __s.data.as_pointer()) as Ptr).offset((*i.borrow())); } fn operator_index_2(&self, i: i32) -> Ptr { let i: Value = Rc::new(RefCell::new(i)); - return (field_ptr!((*self), data) as Ptr).offset((*i.borrow())); + return ((*self).with(|__s: &S| __s.data.as_pointer()) as Ptr).offset((*i.borrow())); } fn operator_deref(&self) -> Ptr { return field_ptr!((*self), inner); @@ -132,7 +144,7 @@ impl SImpl for Ptr { return (field_ptr!((*self), inner)); } fn operator_addr(&self) -> Ptr { - return ((field_ptr!((*self), data) as Ptr).offset(0)); + return (((*self).with(|__s: &S| __s.data.as_pointer()) as Ptr).offset(0)); } } pub fn __cpp2rust_init_globals() { diff --git a/tests/unit/out/refcount/pointer_array.rs b/tests/unit/out/refcount/pointer_array.rs index 58fcf3c35d..ed4e0954b6 100644 --- a/tests/unit/out/refcount/pointer_array.rs +++ b/tests/unit/out/refcount/pointer_array.rs @@ -6,17 +6,30 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Clone, Record, VaArg, FnPtrArg)] +#[derive(Record, VaArg, FnPtrArg)] pub struct StackArray { #[offset(0)] - pub arr: Box<[Ptr]>, + pub arr: Value]>>, +} +impl Clone for StackArray { + fn clone(&self) -> Self { + let __this: Value = Rc::new(RefCell::new(Self { + arr: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 3, _>( + |__i: usize| ((*self.arr.borrow())[(__i) as usize]).clone(), + )))), + })); + let this: Ptr = __this.as_pointer(); + Rc::try_unwrap(__this).ok().unwrap().into_inner() + } } impl Default for StackArray { fn default() -> Self { StackArray { - arr: (0..3) - .map(|_| Ptr::::null()) - .collect::]>>(), + arr: Rc::new(RefCell::new( + (0..3) + .map(|_| Ptr::::null()) + .collect::]>>(), + )), } } } @@ -25,11 +38,11 @@ impl ByteRepr for StackArray { 24 } fn to_bytes(&self, buf: &mut [u8]) { - self.arr.to_bytes(&mut buf[0..24]); + (*self.arr.borrow()).to_bytes(&mut buf[0..24]); } fn from_bytes(buf: &[u8]) -> Self { Self { - arr: ]>>::from_bytes(&buf[0..24]), + arr: Rc::new(RefCell::new(]>>::from_bytes(&buf[0..24]))), } } } @@ -37,7 +50,9 @@ pub fn IncrementAll_0(s: Ptr) { let i: Value = Rc::new(RefCell::new(0)); 'loop_: while ((*i.borrow()) < 3) { { - let _ptr = (*s.upgrade().deref()).arr[(*i.borrow()) as usize].clone(); + let _ptr = (*s.with(|__s: &StackArray| __s.arr.clone()).borrow()) + [(*i.borrow()) as usize] + .clone(); _ptr.write(_ptr.read() + 1) }; (*i.borrow_mut()).prefix_inc(); @@ -50,7 +65,11 @@ pub fn main() { fn main_0() -> i32 { let x: Value = Rc::new(RefCell::new(0)); let s: Value = Rc::new(RefCell::new(StackArray { - arr: Box::new([(x.as_pointer()), (x.as_pointer()), (x.as_pointer())]), + arr: Rc::new(RefCell::new(Box::new([ + (x.as_pointer()), + (x.as_pointer()), + (x.as_pointer()), + ]))), })); ({ IncrementAll_0(s.as_pointer()) }); assert!(((*x.borrow()) == 3)); diff --git a/tests/unit/out/refcount/push_emplace_back.rs b/tests/unit/out/refcount/push_emplace_back.rs index e0c622281c..2b8384e552 100644 --- a/tests/unit/out/refcount/push_emplace_back.rs +++ b/tests/unit/out/refcount/push_emplace_back.rs @@ -85,13 +85,15 @@ impl ByteRepr for JPEGData { 48 } fn to_bytes(&self, buf: &mut [u8]) { - self.com_data.to_bytes(&mut buf[0..24]); - self.app_data.to_bytes(&mut buf[24..48]); + (*self.com_data.borrow()).to_bytes(&mut buf[0..24]); + (*self.app_data.borrow()).to_bytes(&mut buf[24..48]); } fn from_bytes(buf: &[u8]) -> Self { Self { - com_data: >>>>::from_bytes(&buf[0..24]), - app_data: >>>>::from_bytes(&buf[24..48]), + com_data: Rc::new(RefCell::new(>>>::from_bytes(&buf[0..24]))), + app_data: Rc::new(RefCell::new(>>>::from_bytes( + &buf[24..48], + ))), } } } diff --git a/tests/unit/out/refcount/random.rs b/tests/unit/out/refcount/random.rs index 1276be38d0..0138ee42e6 100644 --- a/tests/unit/out/refcount/random.rs +++ b/tests/unit/out/refcount/random.rs @@ -6,14 +6,14 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Clone, Record, VaArg, FnPtrArg)] +#[derive(Record, VaArg, FnPtrArg)] pub struct Pair { #[offset(0)] pub x: i32, #[offset(4)] pub y: i32, #[offset(8)] - pub a: Box<[i32]>, + pub a: Value>, #[offset(32)] pub r: Ptr, #[offset(40)] @@ -21,20 +21,41 @@ pub struct Pair { #[offset(48)] pub pair: Ptr, #[offset(56)] - pub ap: Box<[Ptr]>, + pub ap: Value]>>, +} +impl Clone for Pair { + fn clone(&self) -> Self { + let __this: Value = Rc::new(RefCell::new(Self { + x: { self.x }, + y: { self.y }, + a: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 5, _>( + |__i: usize| (*self.a.borrow())[(__i) as usize], + )))), + r: { (self.r).clone() }, + p: { self.p.clone() }, + pair: { self.pair.clone() }, + ap: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 2, _>( + |__i: usize| ((*self.ap.borrow())[(__i) as usize]).clone(), + )))), + })); + let this: Ptr = __this.as_pointer(); + Rc::try_unwrap(__this).ok().unwrap().into_inner() + } } impl Default for Pair { fn default() -> Self { Pair { x: 0_i32, y: 0_i32, - a: (0..5).map(|_| 0_i32).collect::>(), + a: Rc::new(RefCell::new((0..5).map(|_| 0_i32).collect::>())), r: >::default(), p: Ptr::::null(), pair: Ptr::::null(), - ap: (0..2) - .map(|_| Ptr::::null()) - .collect::]>>(), + ap: Rc::new(RefCell::new( + (0..2) + .map(|_| Ptr::::null()) + .collect::]>>(), + )), } } } @@ -73,32 +94,35 @@ fn main_0() -> i32 { let y1: Value = Rc::new(RefCell::new(Pair { x: 1, y: 2, - a: Box::new([1, 2, 3, 4, 5]), + a: Rc::new(RefCell::new(Box::new([1, 2, 3, 4, 5]))), r: x1.as_pointer(), p: Ptr::::null(), pair: Ptr::::null(), - ap: Box::new([Ptr::::null(), Ptr::::null()]), + ap: Rc::new(RefCell::new(Box::new([ + Ptr::::null(), + Ptr::::null(), + ]))), })); let y4: Value = Rc::new(RefCell::new(Pair { x: { (*y1.borrow()).x }, y: { (*y1.borrow()).y }, a: { - Box::new([ - (*y1.borrow()).a[(0) as usize], - (*y1.borrow()).a[(1) as usize], - (*y1.borrow()).a[(2) as usize], - (*y1.borrow()).a[(3) as usize], - (*y1.borrow()).a[(4) as usize], - ]) + Rc::new(RefCell::new(Box::new([ + (*(*y1.borrow()).a.borrow())[(0) as usize], + (*(*y1.borrow()).a.borrow())[(1) as usize], + (*(*y1.borrow()).a.borrow())[(2) as usize], + (*(*y1.borrow()).a.borrow())[(3) as usize], + (*(*y1.borrow()).a.borrow())[(4) as usize], + ]))) }, r: { ((*y1.borrow()).r).clone() }, p: { (*y1.borrow()).p.clone() }, pair: { (*y1.borrow()).pair.clone() }, ap: { - Box::new([ - ((*y1.borrow()).ap[(0) as usize]).clone(), - ((*y1.borrow()).ap[(1) as usize]).clone(), - ]) + Rc::new(RefCell::new(Box::new([ + ((*(*y1.borrow()).ap.borrow())[(0) as usize]).clone(), + ((*(*y1.borrow()).ap.borrow())[(1) as usize]).clone(), + ]))) }, })); let ry1: Ptr = y1.as_pointer(); @@ -107,22 +131,22 @@ fn main_0() -> i32 { x: { ry1.with(|__s: &Pair| __s.x) }, y: { ry1.with(|__s: &Pair| __s.y) }, a: { - Box::new([ - ry1.with(|__s: &Pair| __s.a[(0) as usize]), - ry1.with(|__s: &Pair| __s.a[(1) as usize]), - ry1.with(|__s: &Pair| __s.a[(2) as usize]), - ry1.with(|__s: &Pair| __s.a[(3) as usize]), - ry1.with(|__s: &Pair| __s.a[(4) as usize]), - ]) + Rc::new(RefCell::new(Box::new([ + ry1.with(|__s: &Pair| (*__s.a.borrow())[(0) as usize]), + ry1.with(|__s: &Pair| (*__s.a.borrow())[(1) as usize]), + ry1.with(|__s: &Pair| (*__s.a.borrow())[(2) as usize]), + ry1.with(|__s: &Pair| (*__s.a.borrow())[(3) as usize]), + ry1.with(|__s: &Pair| (*__s.a.borrow())[(4) as usize]), + ]))) }, r: { ((*ry1.upgrade().deref()).r).clone() }, p: { ry1.with(|__s: &Pair| (__s.p).clone()) }, pair: { ry1.with(|__s: &Pair| (__s.pair).clone()) }, ap: { - Box::new([ - ry1.with(|__s: &Pair| (__s.ap[(0) as usize]).clone()), - ry1.with(|__s: &Pair| (__s.ap[(1) as usize]).clone()), - ]) + Rc::new(RefCell::new(Box::new([ + ry1.with(|__s: &Pair| ((*__s.ap.borrow())[(0) as usize]).clone()), + ry1.with(|__s: &Pair| ((*__s.ap.borrow())[(1) as usize]).clone()), + ]))) }, })); let ry2: Ptr = (ry1).clone(); @@ -131,22 +155,22 @@ fn main_0() -> i32 { x: { (*py1.borrow()).with(|__s: &Pair| __s.x) }, y: { (*py1.borrow()).with(|__s: &Pair| __s.y) }, a: { - Box::new([ - (*py1.borrow()).with(|__s: &Pair| __s.a[(0) as usize]), - (*py1.borrow()).with(|__s: &Pair| __s.a[(1) as usize]), - (*py1.borrow()).with(|__s: &Pair| __s.a[(2) as usize]), - (*py1.borrow()).with(|__s: &Pair| __s.a[(3) as usize]), - (*py1.borrow()).with(|__s: &Pair| __s.a[(4) as usize]), - ]) + Rc::new(RefCell::new(Box::new([ + (*py1.borrow()).with(|__s: &Pair| (*__s.a.borrow())[(0) as usize]), + (*py1.borrow()).with(|__s: &Pair| (*__s.a.borrow())[(1) as usize]), + (*py1.borrow()).with(|__s: &Pair| (*__s.a.borrow())[(2) as usize]), + (*py1.borrow()).with(|__s: &Pair| (*__s.a.borrow())[(3) as usize]), + (*py1.borrow()).with(|__s: &Pair| (*__s.a.borrow())[(4) as usize]), + ]))) }, r: { ((*(*py1.borrow()).upgrade().deref()).r).clone() }, p: { (*py1.borrow()).with(|__s: &Pair| (__s.p).clone()) }, pair: { (*py1.borrow()).with(|__s: &Pair| (__s.pair).clone()) }, ap: { - Box::new([ - (*py1.borrow()).with(|__s: &Pair| (__s.ap[(0) as usize]).clone()), - (*py1.borrow()).with(|__s: &Pair| (__s.ap[(1) as usize]).clone()), - ]) + Rc::new(RefCell::new(Box::new([ + (*py1.borrow()).with(|__s: &Pair| ((*__s.ap.borrow())[(0) as usize]).clone()), + (*py1.borrow()).with(|__s: &Pair| ((*__s.ap.borrow())[(1) as usize]).clone()), + ]))) }, })); let ry3: Ptr = (*py1.borrow()).clone(); @@ -202,7 +226,7 @@ fn main_0() -> i32 { (*px3.borrow_mut()) = (*cp1.borrow()).clone(); (*y1.borrow_mut()).x = 2; (*y1.borrow_mut()).y = 3; - (*y1.borrow_mut()).a[(0) as usize] = 100; + (*(*y1.borrow()).a.borrow_mut())[(0) as usize] = 100; { (*y1.borrow()).r.clone() }.write(10); (*y1.borrow_mut()).p = (*px3.borrow()).clone(); (*px3.borrow_mut()) = (*px2.borrow()).clone(); @@ -213,24 +237,27 @@ fn main_0() -> i32 { { (*y1.borrow()).pair.clone() } .with(|__s: &Pair| (__s.pair).clone()) .with_mut(|__s: &mut Pair| __s.x = 100); - (*y1.borrow_mut()).ap[(0) as usize] = (x1.as_pointer()); - (*y1.borrow_mut()).ap[(1) as usize] = (x2.as_pointer()); - { (*y1.borrow()).ap[(0) as usize].clone() }.write(0); + (*(*y1.borrow()).ap.borrow_mut())[(0) as usize] = (x1.as_pointer()); + (*(*y1.borrow()).ap.borrow_mut())[(1) as usize] = (x2.as_pointer()); + { (*(*y1.borrow()).ap.borrow())[(0) as usize].clone() }.write(0); (*c1.borrow_mut()) = ((*x1.borrow()) + 1); let j: Value = Rc::new(RefCell::new(0)); let new_y: Value = Rc::new(RefCell::new(Pair { x: 1, y: 2, - a: Box::new([1, 2, 3, 4, 5]), + a: Rc::new(RefCell::new(Box::new([1, 2, 3, 4, 5]))), r: j.as_pointer(), p: Ptr::::null(), pair: Ptr::::null(), - ap: Box::new([Ptr::::null(), Ptr::::null()]), + ap: Rc::new(RefCell::new(Box::new([ + Ptr::::null(), + Ptr::::null(), + ]))), })); let __rhs = { (*new_y.borrow()).x }; (*y1.borrow_mut()).x = __rhs; let i: Value = Rc::new(RefCell::new(1_u32)); - (*y1.borrow_mut()).a[(*i.borrow()) as usize] = -1_i32; + (*(*y1.borrow()).a.borrow_mut())[(*i.borrow()) as usize] = -1_i32; (*x1.borrow_mut()).postfix_inc(); (*x1.borrow_mut()).prefix_inc(); (*y1.borrow_mut()).x.postfix_inc(); @@ -263,11 +290,11 @@ impl PairImpl for Ptr { fn method(&self) { (*self).with_mut(|__s: &mut Pair| __s.x.postfix_inc()); (*self).with_mut(|__s: &mut Pair| __s.y.prefix_inc()); - (*self).with_mut(|__s: &mut Pair| __s.a[(4) as usize] = 1); + (*self).with(|__s: &Pair| (*__s.a.borrow_mut())[(4) as usize] = 1); { (*(*self).upgrade().deref()).r.clone() }.write(1); (*self).with_mut(|__s: &mut Pair| __s.p = Ptr::::null()); (*self).with_mut(|__s: &mut Pair| __s.pair = Ptr::::null()); - (*self).with_mut(|__s: &mut Pair| __s.ap[(0) as usize] = Ptr::::null()); + (*self).with(|__s: &Pair| (*__s.ap.borrow_mut())[(0) as usize] = Ptr::::null()); } fn as_val(&self) -> i32 { return (*self).with(|__s: &Pair| __s.x); diff --git a/tests/unit/out/refcount/rule_of_five.rs b/tests/unit/out/refcount/rule_of_five.rs index 5a9e3d6a9a..62a533a9c4 100644 --- a/tests/unit/out/refcount/rule_of_five.rs +++ b/tests/unit/out/refcount/rule_of_five.rs @@ -18,7 +18,7 @@ thread_local!( #[derive(Record, VaArg, FnPtrArg)] pub struct Buffer { #[offset(0)] - pub data: Box<[i32]>, + pub data: Value>, #[offset(16)] pub size: i32, } @@ -26,7 +26,7 @@ impl Buffer { pub fn new(size: i32) -> Self { let size: Value = Rc::new(RefCell::new(size)); let __this: Value = Rc::new(RefCell::new(Self { - data: (0..4).map(|_| 0_i32).collect::>(), + data: Rc::new(RefCell::new((0..4).map(|_| 0_i32).collect::>())), size: (*size.borrow()), })); let this: Ptr = __this.as_pointer(); @@ -37,7 +37,7 @@ impl Buffer { } else { -1_i32 }; - this.with_mut(|__s: &mut Buffer| __s.data[(*i.borrow()) as usize] = __rhs); + this.with(|__s: &Buffer| (*__s.data.borrow_mut())[(*i.borrow()) as usize] = __rhs); (*i.borrow_mut()).prefix_inc(); } (*alive_0.with(Value::clone).borrow_mut()).prefix_inc(); @@ -45,14 +45,14 @@ impl Buffer { } pub fn copy_from(o: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - data: (0..4).map(|_| 0_i32).collect::>(), + data: Rc::new(RefCell::new((0..4).map(|_| 0_i32).collect::>())), size: { o.with(|__s: &Buffer| __s.size) }, })); let this: Ptr = __this.as_pointer(); let i: Value = Rc::new(RefCell::new(0)); 'loop_: while ((*i.borrow()) < 4) { - let __rhs = o.with(|__s: &Buffer| __s.data[(*i.borrow()) as usize]); - this.with_mut(|__s: &mut Buffer| __s.data[(*i.borrow()) as usize] = __rhs); + let __rhs = o.with(|__s: &Buffer| (*__s.data.borrow())[(*i.borrow()) as usize]); + this.with(|__s: &Buffer| (*__s.data.borrow_mut())[(*i.borrow()) as usize] = __rhs); (*i.borrow_mut()).prefix_inc(); } (*alive_0.with(Value::clone).borrow_mut()).prefix_inc(); @@ -61,16 +61,16 @@ impl Buffer { } pub fn move_from(o: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - data: (0..4).map(|_| 0_i32).collect::>(), + data: Rc::new(RefCell::new((0..4).map(|_| 0_i32).collect::>())), size: { o.with(|__s: &Buffer| __s.size) }, })); let this: Ptr = __this.as_pointer(); let i: Value = Rc::new(RefCell::new(0)); 'loop_: while ((*i.borrow()) < 4) { - let __rhs = o.with(|__s: &Buffer| __s.data[(*i.borrow()) as usize]); - this.with_mut(|__s: &mut Buffer| __s.data[(*i.borrow()) as usize] = __rhs); + let __rhs = o.with(|__s: &Buffer| (*__s.data.borrow())[(*i.borrow()) as usize]); + this.with(|__s: &Buffer| (*__s.data.borrow_mut())[(*i.borrow()) as usize] = __rhs); let __rhs = -1_i32; - o.with_mut(|__s: &mut Buffer| __s.data[(*i.borrow()) as usize] = __rhs); + o.with(|__s: &Buffer| (*__s.data.borrow_mut())[(*i.borrow()) as usize] = __rhs); (*i.borrow_mut()).prefix_inc(); } o.with_mut(|__s: &mut Buffer| __s.size = 0); @@ -91,7 +91,7 @@ impl Clone for Buffer { impl Default for Buffer { fn default() -> Self { Buffer { - data: (0..4).map(|_| 0_i32).collect::>(), + data: Rc::new(RefCell::new((0..4).map(|_| 0_i32).collect::>())), size: 0_i32, } } @@ -104,12 +104,12 @@ impl ByteRepr for Buffer { 20 } fn to_bytes(&self, buf: &mut [u8]) { - self.data.to_bytes(&mut buf[0..16]); + (*self.data.borrow()).to_bytes(&mut buf[0..16]); self.size.to_bytes(&mut buf[16..20]); } fn from_bytes(buf: &[u8]) -> Self { Self { - data: >::from_bytes(&buf[0..16]), + data: Rc::new(RefCell::new(>::from_bytes(&buf[0..16]))), size: ::from_bytes(&buf[16..20]), } } @@ -135,24 +135,30 @@ fn main_0() -> i32 { ((alive_0.with(|rc| *rc.borrow()) == 2) && (copies_1.with(|rc| *rc.borrow()) == 1)) && (moves_2.with(|rc| *rc.borrow()) == 0) ); - (*b.borrow_mut()).data[(0) as usize] = 100; - assert!(((*a.borrow()).data[(0) as usize] == 0)); + (*(*b.borrow()).data.borrow_mut())[(0) as usize] = 100; + assert!(((*(*a.borrow()).data.borrow())[(0) as usize] == 0)); let c: Value = Rc::new(RefCell::new(Buffer::move_from({ a.as_pointer() }))); let _dtor_c = ScopedDestructor::new(&c, |__p| __p.destructor()); assert!((alive_0.with(|rc| *rc.borrow()) == 3) && (moves_2.with(|rc| *rc.borrow()) == 1)); - assert!(({ (*a.borrow()).size } == 0) && ((*a.borrow()).data[(0) as usize] == -1_i32)); - assert!(({ (*c.borrow()).size } == 4) && ((*c.borrow()).data[(3) as usize] == 3)); + assert!( + ({ (*a.borrow()).size } == 0) + && ((*(*a.borrow()).data.borrow())[(0) as usize] == -1_i32) + ); + assert!( + ({ (*c.borrow()).size } == 4) && ((*(*c.borrow()).data.borrow())[(3) as usize] == 3) + ); let d: Value = Rc::new(RefCell::new(({ make_3(2) }))); let _dtor_d = ScopedDestructor::new(&d, |__p| __p.destructor()); assert!(({ (*d.borrow()).size } == 2) && (moves_2.with(|rc| *rc.borrow()) == 2)); ({ BufferImpl::copy_assign(&d.as_pointer(), b.as_pointer()) }); assert!( - (({ (*d.borrow()).size } == 4) && ((*d.borrow()).data[(0) as usize] == 100)) + (({ (*d.borrow()).size } == 4) + && ((*(*d.borrow()).data.borrow())[(0) as usize] == 100)) && (copies_1.with(|rc| *rc.borrow()) == 2) ); ({ BufferImpl::move_assign(&d.as_pointer(), c.as_pointer()) }); assert!( - (((*d.borrow()).data[(0) as usize] == 0) && ({ (*c.borrow()).size } == 0)) + (((*(*d.borrow()).data.borrow())[(0) as usize] == 0) && ({ (*c.borrow()).size } == 0)) && (moves_2.with(|rc| *rc.borrow()) == 3) ); ({ @@ -181,8 +187,8 @@ impl BufferImpl for Ptr { (*self).with_mut(|__s: &mut Buffer| __s.size = __rhs); let i: Value = Rc::new(RefCell::new(0)); 'loop_: while ((*i.borrow()) < 4) { - let __rhs = o.with(|__s: &Buffer| __s.data[(*i.borrow()) as usize]); - (*self).with_mut(|__s: &mut Buffer| __s.data[(*i.borrow()) as usize] = __rhs); + let __rhs = o.with(|__s: &Buffer| (*__s.data.borrow())[(*i.borrow()) as usize]); + (*self).with(|__s: &Buffer| (*__s.data.borrow_mut())[(*i.borrow()) as usize] = __rhs); (*i.borrow_mut()).prefix_inc(); } (*copies_1.with(Value::clone).borrow_mut()).prefix_inc(); @@ -196,10 +202,10 @@ impl BufferImpl for Ptr { (*self).with_mut(|__s: &mut Buffer| __s.size = __rhs); let i: Value = Rc::new(RefCell::new(0)); 'loop_: while ((*i.borrow()) < 4) { - let __rhs = o.with(|__s: &Buffer| __s.data[(*i.borrow()) as usize]); - (*self).with_mut(|__s: &mut Buffer| __s.data[(*i.borrow()) as usize] = __rhs); + let __rhs = o.with(|__s: &Buffer| (*__s.data.borrow())[(*i.borrow()) as usize]); + (*self).with(|__s: &Buffer| (*__s.data.borrow_mut())[(*i.borrow()) as usize] = __rhs); let __rhs = -1_i32; - o.with_mut(|__s: &mut Buffer| __s.data[(*i.borrow()) as usize] = __rhs); + o.with(|__s: &Buffer| (*__s.data.borrow_mut())[(*i.borrow()) as usize] = __rhs); (*i.borrow_mut()).prefix_inc(); } o.with_mut(|__s: &mut Buffer| __s.size = 0); diff --git a/tests/unit/out/refcount/rule_of_three.rs b/tests/unit/out/refcount/rule_of_three.rs index e462558a2e..b4268fdcc8 100644 --- a/tests/unit/out/refcount/rule_of_three.rs +++ b/tests/unit/out/refcount/rule_of_three.rs @@ -15,7 +15,7 @@ thread_local!( #[derive(Record, VaArg, FnPtrArg)] pub struct Buffer { #[offset(0)] - pub data: Box<[i32]>, + pub data: Value>, #[offset(16)] pub size: i32, } @@ -23,7 +23,7 @@ impl Buffer { pub fn new(size: i32) -> Self { let size: Value = Rc::new(RefCell::new(size)); let __this: Value = Rc::new(RefCell::new(Self { - data: (0..4).map(|_| 0_i32).collect::>(), + data: Rc::new(RefCell::new((0..4).map(|_| 0_i32).collect::>())), size: (*size.borrow()), })); let this: Ptr = __this.as_pointer(); @@ -34,7 +34,7 @@ impl Buffer { } else { -1_i32 }; - this.with_mut(|__s: &mut Buffer| __s.data[(*i.borrow()) as usize] = __rhs); + this.with(|__s: &Buffer| (*__s.data.borrow_mut())[(*i.borrow()) as usize] = __rhs); (*i.borrow_mut()).prefix_inc(); } (*alive_0.with(Value::clone).borrow_mut()).prefix_inc(); @@ -42,14 +42,14 @@ impl Buffer { } pub fn copy_from(o: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - data: (0..4).map(|_| 0_i32).collect::>(), + data: Rc::new(RefCell::new((0..4).map(|_| 0_i32).collect::>())), size: { o.with(|__s: &Buffer| __s.size) }, })); let this: Ptr = __this.as_pointer(); let i: Value = Rc::new(RefCell::new(0)); 'loop_: while ((*i.borrow()) < 4) { - let __rhs = o.with(|__s: &Buffer| __s.data[(*i.borrow()) as usize]); - this.with_mut(|__s: &mut Buffer| __s.data[(*i.borrow()) as usize] = __rhs); + let __rhs = o.with(|__s: &Buffer| (*__s.data.borrow())[(*i.borrow()) as usize]); + this.with(|__s: &Buffer| (*__s.data.borrow_mut())[(*i.borrow()) as usize] = __rhs); (*i.borrow_mut()).prefix_inc(); } (*alive_0.with(Value::clone).borrow_mut()).prefix_inc(); @@ -69,7 +69,7 @@ impl Clone for Buffer { impl Default for Buffer { fn default() -> Self { Buffer { - data: (0..4).map(|_| 0_i32).collect::>(), + data: Rc::new(RefCell::new((0..4).map(|_| 0_i32).collect::>())), size: 0_i32, } } @@ -82,12 +82,12 @@ impl ByteRepr for Buffer { 20 } fn to_bytes(&self, buf: &mut [u8]) { - self.data.to_bytes(&mut buf[0..16]); + (*self.data.borrow()).to_bytes(&mut buf[0..16]); self.size.to_bytes(&mut buf[16..20]); } fn from_bytes(buf: &[u8]) -> Self { Self { - data: >::from_bytes(&buf[0..16]), + data: Rc::new(RefCell::new(>::from_bytes(&buf[0..16]))), size: ::from_bytes(&buf[16..20]), } } @@ -100,7 +100,7 @@ pub fn sum_2(b: Ptr) -> i32 { let _lhs = (*i.borrow()); _lhs < b.with(|__s: &Buffer| __s.size) } { - (*s.borrow_mut()) += b.with(|__s: &Buffer| __s.data[(*i.borrow()) as usize]); + (*s.borrow_mut()) += b.with(|__s: &Buffer| (*__s.data.borrow())[(*i.borrow()) as usize]); (*i.borrow_mut()).prefix_inc(); } return (*s.borrow()); @@ -116,12 +116,14 @@ fn main_0() -> i32 { let b: Value = Rc::new(RefCell::new(Buffer::copy_from({ a.as_pointer() }))); let _dtor_b = ScopedDestructor::new(&b, |__p| __p.destructor()); assert!((alive_0.with(|rc| *rc.borrow()) == 2) && (copies_1.with(|rc| *rc.borrow()) == 1)); - (*b.borrow_mut()).data[(0) as usize] = 100; - assert!(((*a.borrow()).data[(0) as usize] == 0)); + (*(*b.borrow()).data.borrow_mut())[(0) as usize] = 100; + assert!(((*(*a.borrow()).data.borrow())[(0) as usize] == 0)); let c: Value = Rc::new(RefCell::new(Buffer::new({ 2 }))); let _dtor_c = ScopedDestructor::new(&c, |__p| __p.destructor()); ({ BufferImpl::copy_assign(&c.as_pointer(), a.as_pointer()) }); - assert!(({ (*c.borrow()).size } == 4) && ((*c.borrow()).data[(3) as usize] == 3)); + assert!( + ({ (*c.borrow()).size } == 4) && ((*(*c.borrow()).data.borrow())[(3) as usize] == 3) + ); assert!((alive_0.with(|rc| *rc.borrow()) == 3) && (copies_1.with(|rc| *rc.borrow()) == 2)); ({ let _o: Ptr = c.as_pointer(); @@ -133,11 +135,14 @@ fn main_0() -> i32 { let d: Value = Rc::new(RefCell::new(Buffer::copy_from({ a.as_pointer() }))); let _dtor_d = ScopedDestructor::new(&d, |__p| __p.destructor()); assert!((alive_0.with(|rc| *rc.borrow()) == 4) && (copies_1.with(|rc| *rc.borrow()) == 3)); - assert!(({ (*a.borrow()).size } == 4) && ((*a.borrow()).data[(3) as usize] == 3)); + assert!( + ({ (*a.borrow()).size } == 4) && ((*(*a.borrow()).data.borrow())[(3) as usize] == 3) + ); ({ BufferImpl::copy_assign(&d.as_pointer(), b.as_pointer()) }); assert!((copies_1.with(|rc| *rc.borrow()) == 4)); assert!( - ((*b.borrow()).data[(0) as usize] == 100) && ((*d.borrow()).data[(0) as usize] == 100) + ((*(*b.borrow()).data.borrow())[(0) as usize] == 100) + && ((*(*d.borrow()).data.borrow())[(0) as usize] == 100) ); } assert!((alive_0.with(|rc| *rc.borrow()) == 0)); @@ -159,8 +164,8 @@ impl BufferImpl for Ptr { (*self).with_mut(|__s: &mut Buffer| __s.size = __rhs); let i: Value = Rc::new(RefCell::new(0)); 'loop_: while ((*i.borrow()) < 4) { - let __rhs = o.with(|__s: &Buffer| __s.data[(*i.borrow()) as usize]); - (*self).with_mut(|__s: &mut Buffer| __s.data[(*i.borrow()) as usize] = __rhs); + let __rhs = o.with(|__s: &Buffer| (*__s.data.borrow())[(*i.borrow()) as usize]); + (*self).with(|__s: &Buffer| (*__s.data.borrow_mut())[(*i.borrow()) as usize] = __rhs); (*i.borrow_mut()).prefix_inc(); } (*copies_1.with(Value::clone).borrow_mut()).prefix_inc(); diff --git a/tests/unit/out/refcount/typedef-anon-struct.rs b/tests/unit/out/refcount/typedef-anon-struct.rs index 640b353ed3..def16265f8 100644 --- a/tests/unit/out/refcount/typedef-anon-struct.rs +++ b/tests/unit/out/refcount/typedef-anon-struct.rs @@ -50,11 +50,11 @@ impl ByteRepr for Outer { 24 } fn to_bytes(&self, buf: &mut [u8]) { - self.runs.to_bytes(&mut buf[0..24]); + (*self.runs.borrow()).to_bytes(&mut buf[0..24]); } fn from_bytes(buf: &[u8]) -> Self { Self { - runs: >>::from_bytes(&buf[0..24]), + runs: Rc::new(RefCell::new(>::from_bytes(&buf[0..24]))), } } } diff --git a/tests/unit/out/refcount/union_addrof_external.rs b/tests/unit/out/refcount/union_addrof_external.rs index 3d7031007d..47ec96a0eb 100644 --- a/tests/unit/out/refcount/union_addrof_external.rs +++ b/tests/unit/out/refcount/union_addrof_external.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Clone, Record, VaArg, FnPtrArg)] +#[derive(Record, VaArg, FnPtrArg)] pub struct record { #[offset(0)] pub code: u16, @@ -15,7 +15,17 @@ pub struct record { #[offset(4)] pub hi: u32, #[offset(8)] - pub pad: Box<[u8]>, + pub pad: Value>, +} +impl Clone for record { + fn clone(&self) -> Self { + Self { + code: self.code.clone(), + lo: self.lo.clone(), + hi: self.hi.clone(), + pad: Rc::new(RefCell::new((*self.pad.borrow()).clone())), + } + } } impl Default for record { fn default() -> Self { @@ -23,7 +33,7 @@ impl Default for record { code: 0_u16, lo: 0_u16, hi: 0_u32, - pad: (0..8).map(|_| 0_u8).collect::>(), + pad: Rc::new(RefCell::new((0..8).map(|_| 0_u8).collect::>())), } } } @@ -35,14 +45,14 @@ impl ByteRepr for record { self.code.to_bytes(&mut buf[0..2]); self.lo.to_bytes(&mut buf[2..4]); self.hi.to_bytes(&mut buf[4..8]); - self.pad.to_bytes(&mut buf[8..16]); + (*self.pad.borrow()).to_bytes(&mut buf[8..16]); } fn from_bytes(buf: &[u8]) -> Self { Self { code: ::from_bytes(&buf[0..2]), lo: ::from_bytes(&buf[2..4]), hi: ::from_bytes(&buf[4..8]), - pad: >::from_bytes(&buf[8..16]), + pad: Rc::new(RefCell::new(>::from_bytes(&buf[8..16]))), } } } diff --git a/tests/unit/out/refcount/union_cross_arm_cast.rs b/tests/unit/out/refcount/union_cross_arm_cast.rs index 3a29d2cd4a..e6971d592a 100644 --- a/tests/unit/out/refcount/union_cross_arm_cast.rs +++ b/tests/unit/out/refcount/union_cross_arm_cast.rs @@ -6,18 +6,26 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Clone, Record, VaArg, FnPtrArg)] +#[derive(Record, VaArg, FnPtrArg)] pub struct shape_a { #[offset(0)] pub code: u16, #[offset(2)] - pub pad: Box<[u8]>, + pub pad: Value>, +} +impl Clone for shape_a { + fn clone(&self) -> Self { + Self { + code: self.code.clone(), + pad: Rc::new(RefCell::new((*self.pad.borrow()).clone())), + } + } } impl Default for shape_a { fn default() -> Self { shape_a { code: 0_u16, - pad: (0..14).map(|_| 0_u8).collect::>(), + pad: Rc::new(RefCell::new((0..14).map(|_| 0_u8).collect::>())), } } } @@ -27,16 +35,16 @@ impl ByteRepr for shape_a { } fn to_bytes(&self, buf: &mut [u8]) { self.code.to_bytes(&mut buf[0..2]); - self.pad.to_bytes(&mut buf[2..16]); + (*self.pad.borrow()).to_bytes(&mut buf[2..16]); } fn from_bytes(buf: &[u8]) -> Self { Self { code: ::from_bytes(&buf[0..2]), - pad: >::from_bytes(&buf[2..16]), + pad: Rc::new(RefCell::new(>::from_bytes(&buf[2..16]))), } } } -#[derive(Clone, Record, VaArg, FnPtrArg)] +#[derive(Record, VaArg, FnPtrArg)] pub struct shape_b { #[offset(0)] pub code: u16, @@ -45,17 +53,28 @@ pub struct shape_b { #[offset(4)] pub mid: u32, #[offset(8)] - pub fill: Box<[u8]>, + pub fill: Value>, #[offset(24)] pub tail: u32, } +impl Clone for shape_b { + fn clone(&self) -> Self { + Self { + code: self.code.clone(), + lo: self.lo.clone(), + mid: self.mid.clone(), + fill: Rc::new(RefCell::new((*self.fill.borrow()).clone())), + tail: self.tail.clone(), + } + } +} impl Default for shape_b { fn default() -> Self { shape_b { code: 0_u16, lo: 0_u16, mid: 0_u32, - fill: (0..16).map(|_| 0_u8).collect::>(), + fill: Rc::new(RefCell::new((0..16).map(|_| 0_u8).collect::>())), tail: 0_u32, } } @@ -68,7 +87,7 @@ impl ByteRepr for shape_b { self.code.to_bytes(&mut buf[0..2]); self.lo.to_bytes(&mut buf[2..4]); self.mid.to_bytes(&mut buf[4..8]); - self.fill.to_bytes(&mut buf[8..24]); + (*self.fill.borrow()).to_bytes(&mut buf[8..24]); self.tail.to_bytes(&mut buf[24..28]); } fn from_bytes(buf: &[u8]) -> Self { @@ -76,7 +95,7 @@ impl ByteRepr for shape_b { code: ::from_bytes(&buf[0..2]), lo: ::from_bytes(&buf[2..4]), mid: ::from_bytes(&buf[4..8]), - fill: >::from_bytes(&buf[8..24]), + fill: Rc::new(RefCell::new(>::from_bytes(&buf[8..24]))), tail: ::from_bytes(&buf[24..28]), } } diff --git a/tests/unit/out/refcount/union_memset_memcpy.rs b/tests/unit/out/refcount/union_memset_memcpy.rs index b239b1dd72..2add3504fe 100644 --- a/tests/unit/out/refcount/union_memset_memcpy.rs +++ b/tests/unit/out/refcount/union_memset_memcpy.rs @@ -6,18 +6,26 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Clone, Record, VaArg, FnPtrArg)] +#[derive(Record, VaArg, FnPtrArg)] pub struct shape_a { #[offset(0)] pub code: u16, #[offset(2)] - pub pad: Box<[u8]>, + pub pad: Value>, +} +impl Clone for shape_a { + fn clone(&self) -> Self { + Self { + code: self.code.clone(), + pad: Rc::new(RefCell::new((*self.pad.borrow()).clone())), + } + } } impl Default for shape_a { fn default() -> Self { shape_a { code: 0_u16, - pad: (0..14).map(|_| 0_u8).collect::>(), + pad: Rc::new(RefCell::new((0..14).map(|_| 0_u8).collect::>())), } } } @@ -27,16 +35,16 @@ impl ByteRepr for shape_a { } fn to_bytes(&self, buf: &mut [u8]) { self.code.to_bytes(&mut buf[0..2]); - self.pad.to_bytes(&mut buf[2..16]); + (*self.pad.borrow()).to_bytes(&mut buf[2..16]); } fn from_bytes(buf: &[u8]) -> Self { Self { code: ::from_bytes(&buf[0..2]), - pad: >::from_bytes(&buf[2..16]), + pad: Rc::new(RefCell::new(>::from_bytes(&buf[2..16]))), } } } -#[derive(Clone, Record, VaArg, FnPtrArg)] +#[derive(Record, VaArg, FnPtrArg)] pub struct shape_b { #[offset(0)] pub code: u16, @@ -45,7 +53,17 @@ pub struct shape_b { #[offset(4)] pub hi: u32, #[offset(8)] - pub fill: Box<[u8]>, + pub fill: Value>, +} +impl Clone for shape_b { + fn clone(&self) -> Self { + Self { + code: self.code.clone(), + lo: self.lo.clone(), + hi: self.hi.clone(), + fill: Rc::new(RefCell::new((*self.fill.borrow()).clone())), + } + } } impl Default for shape_b { fn default() -> Self { @@ -53,7 +71,7 @@ impl Default for shape_b { code: 0_u16, lo: 0_u16, hi: 0_u32, - fill: (0..8).map(|_| 0_u8).collect::>(), + fill: Rc::new(RefCell::new((0..8).map(|_| 0_u8).collect::>())), } } } @@ -65,14 +83,14 @@ impl ByteRepr for shape_b { self.code.to_bytes(&mut buf[0..2]); self.lo.to_bytes(&mut buf[2..4]); self.hi.to_bytes(&mut buf[4..8]); - self.fill.to_bytes(&mut buf[8..16]); + (*self.fill.borrow()).to_bytes(&mut buf[8..16]); } fn from_bytes(buf: &[u8]) -> Self { Self { code: ::from_bytes(&buf[0..2]), lo: ::from_bytes(&buf[2..4]), hi: ::from_bytes(&buf[4..8]), - fill: >::from_bytes(&buf[8..16]), + fill: Rc::new(RefCell::new(>::from_bytes(&buf[8..16]))), } } } diff --git a/tests/unit/out/refcount/union_nested.rs b/tests/unit/out/refcount/union_nested.rs index 5ccd0552bc..18577e4979 100644 --- a/tests/unit/out/refcount/union_nested.rs +++ b/tests/unit/out/refcount/union_nested.rs @@ -6,18 +6,26 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Clone, Record, VaArg, FnPtrArg)] +#[derive(Record, VaArg, FnPtrArg)] pub struct record { #[offset(0)] pub code: u16, #[offset(2)] - pub pad: Box<[u8]>, + pub pad: Value>, +} +impl Clone for record { + fn clone(&self) -> Self { + Self { + code: self.code.clone(), + pad: Rc::new(RefCell::new((*self.pad.borrow()).clone())), + } + } } impl Default for record { fn default() -> Self { record { code: 0_u16, - pad: (0..14).map(|_| 0_u8).collect::>(), + pad: Rc::new(RefCell::new((0..14).map(|_| 0_u8).collect::>())), } } } @@ -27,12 +35,12 @@ impl ByteRepr for record { } fn to_bytes(&self, buf: &mut [u8]) { self.code.to_bytes(&mut buf[0..2]); - self.pad.to_bytes(&mut buf[2..16]); + (*self.pad.borrow()).to_bytes(&mut buf[2..16]); } fn from_bytes(buf: &[u8]) -> Self { Self { code: ::from_bytes(&buf[0..2]), - pad: >::from_bytes(&buf[2..16]), + pad: Rc::new(RefCell::new(>::from_bytes(&buf[2..16]))), } } } @@ -187,13 +195,13 @@ fn main_0() -> i32 { (*ex.borrow()) .body .h() - .with_mut(|__s: &mut record| __s.pad[(0) as usize] = (('X' as i32) as u8)); + .with(|__s: &record| (*__s.pad.borrow_mut())[(0) as usize] = (('X' as i32) as u8)); assert!((((((*ex.borrow()).body.h().with(|__s: &record| __s.code) as i32) == 2) as i32) != 0)); assert!( (((((*ex.borrow()) .body .h() - .with(|__s: &record| __s.pad[(0) as usize]) as i32) + .with(|__s: &record| (*__s.pad.borrow())[(0) as usize]) as i32) == ('X' as i32)) as i32) != 0) ); diff --git a/tests/unit/out/refcount/unique_ptr.rs b/tests/unit/out/refcount/unique_ptr.rs index 45b4561bc7..b16ddf13d9 100644 --- a/tests/unit/out/refcount/unique_ptr.rs +++ b/tests/unit/out/refcount/unique_ptr.rs @@ -67,8 +67,7 @@ pub fn DoStuffWithSafePointer_0(safe_ptr: Ptr>>) { (x1.as_pointer() as Ptr>>).write((*x2.borrow_mut()).take()); let raw_ptr1: Value> = Rc::new(RefCell::new(((*x1.borrow()).as_pointer()))); (*raw_ptr1.borrow()).with_mut(|__v| __v.prefix_inc()); - ((field_ptr!(((*safe_ptr.upgrade().deref()).as_pointer()), ptr) as Ptr>>) - as Ptr>>) + (field_ptr!(((*safe_ptr.upgrade().deref()).as_pointer()), ptr) as Ptr>>) .write((*x1.borrow_mut()).take()); ({ SafePointerImpl::inc(&((*safe_ptr.upgrade().deref()).as_pointer())) }); ({ SafePointerImpl::inc(&((*safe_ptr.upgrade().deref()).as_pointer())) }); @@ -291,7 +290,7 @@ impl SafePointerImpl for Ptr { .prefix_inc(); } fn move_assign(&self, _a0: Ptr) -> Ptr { - ((field_ptr!((*self), ptr) as Ptr>>) as Ptr>>) + (field_ptr!((*self), ptr) as Ptr>>) .write(_a0.with_mut(|__s: &mut SafePointer| __s.ptr.take())); return (*self).clone(); } diff --git a/tests/unit/out/refcount/unique_ptr_const_deref.rs b/tests/unit/out/refcount/unique_ptr_const_deref.rs index e6ce3ca654..6c7bd3d39f 100644 --- a/tests/unit/out/refcount/unique_ptr_const_deref.rs +++ b/tests/unit/out/refcount/unique_ptr_const_deref.rs @@ -60,8 +60,7 @@ pub fn main() { } fn main_0() -> i32 { let h: Value = Rc::new(RefCell::new(::default())); - ((field_ptr!(h, val) as Ptr>>) as Ptr>>) - .write(Some(Rc::new(RefCell::new(10))).take()); + (field_ptr!(h, val) as Ptr>>).write(Some(Rc::new(RefCell::new(10))).take()); ({ write_val_1((h.as_pointer()), 42) }); assert!((({ read_val_0((h.as_pointer()),) }) == 42)); return 0; @@ -71,7 +70,7 @@ pub trait HolderImpl { } impl HolderImpl for Ptr { fn move_assign(&self, _a0: Ptr) -> Ptr { - ((field_ptr!((*self), val) as Ptr>>) as Ptr>>) + (field_ptr!((*self), val) as Ptr>>) .write(_a0.with_mut(|__s: &mut Holder| __s.val.take())); return (*self).clone(); } diff --git a/tests/unit/out/refcount/unique_ptr_nested.rs b/tests/unit/out/refcount/unique_ptr_nested.rs index c7576c5be0..0ffe61810b 100644 --- a/tests/unit/out/refcount/unique_ptr_nested.rs +++ b/tests/unit/out/refcount/unique_ptr_nested.rs @@ -105,7 +105,7 @@ pub trait OuterImpl { } impl OuterImpl for Ptr { fn move_assign(&self, _a0: Ptr) -> Ptr { - ((field_ptr!((*self), inner) as Ptr>>) as Ptr>>) + (field_ptr!((*self), inner) as Ptr>>) .write(_a0.with_mut(|__s: &mut Outer| __s.inner.take())); return (*self).clone(); } diff --git a/tests/unit/out/refcount/vector_subscript_direct.rs b/tests/unit/out/refcount/vector_subscript_direct.rs index 85dd48e97c..7f4a0297a6 100644 --- a/tests/unit/out/refcount/vector_subscript_direct.rs +++ b/tests/unit/out/refcount/vector_subscript_direct.rs @@ -50,13 +50,13 @@ impl ByteRepr for Holder { 48 } fn to_bytes(&self, buf: &mut [u8]) { - self.values.to_bytes(&mut buf[0..24]); - self.points.to_bytes(&mut buf[24..48]); + (*self.values.borrow()).to_bytes(&mut buf[0..24]); + (*self.points.borrow()).to_bytes(&mut buf[24..48]); } fn from_bytes(buf: &[u8]) -> Self { Self { - values: >>::from_bytes(&buf[0..24]), - points: >>::from_bytes(&buf[24..48]), + values: Rc::new(RefCell::new(>::from_bytes(&buf[0..24]))), + points: Rc::new(RefCell::new(>::from_bytes(&buf[24..48]))), } } } diff --git a/tests/unit/out/refcount/void_cast.rs b/tests/unit/out/refcount/void_cast.rs index e95a9ba416..c36593e0bd 100644 --- a/tests/unit/out/refcount/void_cast.rs +++ b/tests/unit/out/refcount/void_cast.rs @@ -32,11 +32,11 @@ impl ByteRepr for NonTrivial { 24 } fn to_bytes(&self, buf: &mut [u8]) { - self.data.to_bytes(&mut buf[0..24]); + (*self.data.borrow()).to_bytes(&mut buf[0..24]); } fn from_bytes(buf: &[u8]) -> Self { Self { - data: >>::from_bytes(&buf[0..24]), + data: Rc::new(RefCell::new(>::from_bytes(&buf[0..24]))), } } } @@ -185,7 +185,7 @@ pub trait NonCopyableImpl { } impl NonCopyableImpl for Ptr { fn move_assign(&self, _a0: Ptr) -> Ptr { - ((field_ptr!((*self), value) as Ptr>>) as Ptr>>) + (field_ptr!((*self), value) as Ptr>>) .write(_a0.with_mut(|__s: &mut NonCopyable| __s.value.take())); return (*self).clone(); } diff --git a/tests/unit/out/unsafe/field_array_elem_ptr.rs b/tests/unit/out/unsafe/field_array_elem_ptr.rs new file mode 100644 index 0000000000..cb05733b1d --- /dev/null +++ b/tests/unit/out/unsafe/field_array_elem_ptr.rs @@ -0,0 +1,107 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +#[repr(C)] +#[derive(Copy, Clone, VaArg, Default)] +pub struct Point { + pub x: i32, + pub y: i32, +} +#[repr(C)] +#[derive(Copy, Clone, VaArg)] +pub struct Shape { + pub id: i32, + pub coords: [i32; 4], + pub points: [Point; 3], + pub tail: i32, +} +impl Default for Shape { + fn default() -> Self { + Shape { + id: 0_i32, + coords: [0_i32; 4], + points: [::default(); 3], + tail: 0_i32, + } + } +} +pub unsafe fn sum_0(mut p: *const i32, mut n: i32) -> i32 { + let mut s: i32 = 0; + let mut i: i32 = 0; + 'loop_: while ((i) < (n)) { + s += (*p.offset((i) as isize)); + i.prefix_inc(); + } + return s; +} +pub unsafe fn set_y_1(mut p: *mut Point, mut y: i32) { + (*p).y = y; +} +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + let mut s: Shape = Shape { + id: 1, + coords: [10, 20, 30, 40], + points: [ + Point { x: 1, y: 2 }, + Point { x: 3, y: 4 }, + Point { x: 5, y: 6 }, + ], + tail: 99, + }; + let mut c: *mut i32 = (&mut s.coords[(1) as usize] as *mut i32); + assert!(((*c) == (20))); + (*c) = 21; + assert!(((s.coords[(1) as usize]) == (21))); + c = (c).wrapping_add((2 as i32) as usize); + assert!(((*c) == (40))); + assert!( + ((((c as usize - (&mut s.coords[(0) as usize] as *mut i32) as usize) + / ::std::mem::size_of::()) as i64) + == (3_i64)) + ); + assert!(((*c.offset((-1_i32) as isize)) == (30))); + assert!(((unsafe { sum_0((s.coords.as_mut_ptr()).cast_const(), 4,) }) == (101))); + assert!( + ((unsafe { sum_0((&mut s.coords[(2) as usize] as *mut i32).cast_const(), 2,) }) == (70)) + ); + let mut p: *mut Point = (&mut s.points[(1) as usize] as *mut Point); + assert!((((*p).x) == (3))); + (unsafe { set_y_1(p.offset((1) as isize), 60) }); + assert!(((s.points[(2) as usize].y) == (60))); + let mut py: *mut i32 = (&mut s.points[(0) as usize].y as *mut i32); + (*py) = 7; + assert!(((s.points[(0) as usize].y) == (7))); + let mut px: *mut i32 = (&mut (*(p.offset((1) as isize))).x as *mut i32); + (*px) += 50; + assert!(((s.points[(2) as usize].x) == (55))); + let mut sp: *mut Shape = (&mut s as *mut Shape); + let mut d: *mut i32 = (*sp).coords.as_mut_ptr().offset((3) as isize); + (*d) = 41; + assert!(((s.coords[(3) as usize]) == (41))); + (*sp).points[(1) as usize].y = (((*sp).coords[(0) as usize]) + ((*sp).points[(0) as usize].x)); + assert!(((s.points[(1) as usize].y) == (11))); + let mut q: *mut Point = (*sp).points.as_mut_ptr(); + (*q.offset((2) as isize)).x = 8; + assert!((((*sp).points[(2) as usize].x) == (8))); + let mut t: Shape = s; + t.coords[(0) as usize] = 0; + t.points[(0) as usize].x = 0; + assert!(((s.coords[(0) as usize]) == (10)) && ((s.points[(0) as usize].x) == (1))); + assert!( + (((t.coords[(1) as usize]) == (21)) && ((t.points[(2) as usize].y) == (60))) + && ((t.tail) == (99)) + ); + return 0; +} +pub unsafe fn __cpp2rust_init_globals() {} From 14528b12cf3440c04a952e892650930820bde757 Mon Sep 17 00:00:00 2001 From: Nuno Lopes Date: Wed, 30 Sep 2026 10:54:52 +0100 Subject: [PATCH 03/13] revert unneeded changes --- libcc2rs/src/iterators.rs | 2 +- libcc2rs/src/rc.rs | 28 ++++++++-------------------- rules/algorithm/tgt_refcount.rs | 4 ++-- rules/deque/tgt_refcount.rs | 2 +- rules/vector/tgt_refcount.rs | 22 +++++++++++----------- 5 files changed, 23 insertions(+), 35 deletions(-) diff --git a/libcc2rs/src/iterators.rs b/libcc2rs/src/iterators.rs index 3d875bee5d..1c64474ef6 100644 --- a/libcc2rs/src/iterators.rs +++ b/libcc2rs/src/iterators.rs @@ -259,7 +259,7 @@ pub struct StringIterator { ptr: Ptr, } -impl Iterator for StringIterator { +impl Iterator for StringIterator { type Item = Ptr; fn next(&mut self) -> Option { // stop before the null terminator at the last position diff --git a/libcc2rs/src/rc.rs b/libcc2rs/src/rc.rs index 24b015b469..326711fdc4 100644 --- a/libcc2rs/src/rc.rs +++ b/libcc2rs/src/rc.rs @@ -316,10 +316,7 @@ impl Ptr { } #[inline] - pub fn len(&self) -> usize - where - T: 'static, - { + pub fn len(&self) -> usize { match &self.kind { PtrKind::Null => 0, PtrKind::StackSingle(_) | PtrKind::HeapSingle(_) | PtrKind::Field(_) => 1, @@ -334,10 +331,7 @@ impl Ptr { } #[inline] - pub fn is_empty(&self) -> bool - where - T: 'static, - { + pub fn is_empty(&self) -> bool { match &self.kind { PtrKind::Null => true, PtrKind::StackSingle(_) | PtrKind::HeapSingle(_) | PtrKind::Field(_) => false, @@ -376,10 +370,7 @@ impl Ptr { } #[inline] - pub fn to_last(&self) -> Self - where - T: 'static, - { + pub fn to_last(&self) -> Self { // A field is a single object, whose offset is that of the pointer. if let PtrKind::Field(_) = self.kind { return self.clone(); @@ -391,10 +382,7 @@ impl Ptr { } #[inline] - pub fn to_end(&self) -> Self - where - T: 'static, - { + pub fn to_end(&self) -> Self { if let PtrKind::Field(_) = self.kind { return self.offset(1); } @@ -698,7 +686,7 @@ impl Ptr { } } -impl Ptr { +impl Ptr { pub fn sort(&self, last: usize) { match self.kind { PtrKind::Null => panic!("ub: dereference of null pointer"), @@ -720,7 +708,7 @@ impl Ptr { } } -impl Ptr { +impl Ptr { pub fn sort_with_cmp(&self, last: usize, mut cmp: F) where F: FnMut(Ptr, Ptr) -> bool, @@ -767,7 +755,7 @@ impl Ptr { impl IntoIterator for &Ptr where - T: Clone + 'static, + T: Clone, { type Item = Ptr; type IntoIter = Ptr; @@ -777,7 +765,7 @@ where } } -impl Iterator for Ptr { +impl Iterator for Ptr { type Item = Ptr; fn next(&mut self) -> Option { if self.get_offset() < self.len() { diff --git a/rules/algorithm/tgt_refcount.rs b/rules/algorithm/tgt_refcount.rs index ef02721d15..66b8a8018e 100644 --- a/rules/algorithm/tgt_refcount.rs +++ b/rules/algorithm/tgt_refcount.rs @@ -6,7 +6,7 @@ use std::cell::RefCell; use std::io::Write; use std::rc::Rc; -fn f1(a0: Ptr, a1: Ptr) { +fn f1(a0: Ptr, a1: Ptr) { a0.sort(a1.get_offset()) } @@ -36,7 +36,7 @@ fn f3(a0: Ptr, a1: Ptr, a2: T1) -> Ptr ) } -fn f6(a0: Ptr, a1: Ptr, a2: T2) +fn f6(a0: Ptr, a1: Ptr, a2: T2) where T2: Callable2, Ptr, bool>, { diff --git a/rules/deque/tgt_refcount.rs b/rules/deque/tgt_refcount.rs index 2cd9d36744..b70e9d05de 100644 --- a/rules/deque/tgt_refcount.rs +++ b/rules/deque/tgt_refcount.rs @@ -5,7 +5,7 @@ use libcc2rs::*; use std::cell::RefCell; use std::rc::Rc; -fn f1(a0: Ptr) -> Ptr { +fn f1(a0: Ptr) -> Ptr { a0.to_last() } diff --git a/rules/vector/tgt_refcount.rs b/rules/vector/tgt_refcount.rs index c0371c65df..6208b07155 100644 --- a/rules/vector/tgt_refcount.rs +++ b/rules/vector/tgt_refcount.rs @@ -45,7 +45,7 @@ fn f9(a0: Ptr) -> Ptr { a0 } -fn f10(a0: Ptr) -> Ptr { +fn f10(a0: Ptr) -> Ptr { a0.to_last() } @@ -53,7 +53,7 @@ fn f13(a0: Ptr) -> Ptr { a0 } -fn f17(a0: Ptr) -> Ptr { +fn f17(a0: Ptr) -> Ptr { a0.to_end() } @@ -140,7 +140,7 @@ fn f37, T2: Clone + ByteRepr>(a0: Ptr, a1: Ptr) -> Vec>() } -fn f40(a0: Ptr) -> Ptr { +fn f40(a0: Ptr) -> Ptr { a0.to_end() } @@ -163,7 +163,7 @@ fn f43(a0: Ptr) -> Ptr { a0 } -fn f44(a0: Ptr) -> Ptr { +fn f44(a0: Ptr) -> Ptr { a0.to_end() } @@ -175,7 +175,7 @@ fn f50(a0: Ptr>, a1: usize) -> Ptr> { } } -fn f51(a0: Ptr) -> Ptr { +fn f51(a0: Ptr) -> Ptr { a0.to_last() } @@ -206,7 +206,7 @@ fn f55(a0: Ptr>, a1: &mut Vec) { a0.write(std::mem::take(&mut *a1)) } -fn f57(a0: Ptr) -> Ptr { +fn f57(a0: Ptr) -> Ptr { a0.to_end() } @@ -232,7 +232,7 @@ fn f68(a0: Ptr) -> Ptr { a0 } -fn f69(a0: Ptr) -> Ptr { +fn f69(a0: Ptr) -> Ptr { a0.to_last() } @@ -240,7 +240,7 @@ fn f72(a0: Ptr) -> Ptr { a0 } -fn f76(a0: Ptr) -> Ptr { +fn f76(a0: Ptr) -> Ptr { a0.to_end() } @@ -323,7 +323,7 @@ fn f95(a0: Ptr) -> Ptr { a0 } -fn f96(a0: Ptr) -> Ptr { +fn f96(a0: Ptr) -> Ptr { a0.to_end() } @@ -331,7 +331,7 @@ fn f98(a0: Ptr, a1: usize) -> Ptr { a0.offset(a1 as isize) } -fn f99(a0: Ptr) -> Ptr { +fn f99(a0: Ptr) -> Ptr { a0.to_last() } @@ -354,7 +354,7 @@ fn f103(a0: Ptr>, a1: &mut Vec) { a0.write(std::mem::take(&mut *a1)) } -fn f104(a0: Ptr) -> Ptr { +fn f104(a0: Ptr) -> Ptr { a0.to_end() } From 9a2f9a28df24f9ba5c7a638a889b2bc2063df754 Mon Sep 17 00:00:00 2001 From: Nuno Lopes Date: Wed, 30 Sep 2026 11:38:36 +0100 Subject: [PATCH 04/13] remove StrongPtr::deref_mut --- .../converter/models/converter_refcount.cpp | 65 +++++++--- .../converter/models/converter_refcount.h | 3 +- docs/src/runtime/rc.md | 23 ++-- libcc2rs/src/field.rs | 2 - libcc2rs/src/rc.rs | 17 +-- .../operator_member_pointer_member.rs | 5 +- .../out/refcount/unique_ptr_field_reset.rs | 122 ++++++++++++++++++ .../unit/out/unsafe/unique_ptr_field_reset.rs | 69 ++++++++++ tests/unit/unique_ptr_field_reset.cpp | 25 ++++ 9 files changed, 279 insertions(+), 52 deletions(-) create mode 100644 tests/unit/out/refcount/unique_ptr_field_reset.rs create mode 100644 tests/unit/out/unsafe/unique_ptr_field_reset.rs create mode 100644 tests/unit/unique_ptr_field_reset.cpp diff --git a/cpp2rust/converter/models/converter_refcount.cpp b/cpp2rust/converter/models/converter_refcount.cpp index b969608ded..9271af483d 100644 --- a/cpp2rust/converter/models/converter_refcount.cpp +++ b/cpp2rust/converter/models/converter_refcount.cpp @@ -2054,20 +2054,17 @@ std::string ConverterRefCount::ConvertRecordPlace(clang::MemberExpr *expr, return std::string(keyword::kSelfValue); } if (expr->isArrow()) { + auto *op = clang::dyn_cast(base); + if (mut && (!op || IsUserOperatorCall(op))) { + pending_deref_.set(ConvertRValue(base), isFresh()); + return {}; + } Buffer buf(*this); PushExprKind push(*this, mut ? ExprKind::LValue : ExprKind::RValue); ConvertArrow(base); return std::move(buf).str(); } - if (!mut) { - return ConvertRValue(base); - } - auto str = ConvertLValue(base); - if (!pending_deref_.empty()) { - return std::format("(*{}.upgrade().deref_mut())", - TakePendingDerefToWrite(base)); - } - return str; + return mut ? ConvertLValue(base) : ConvertRValue(base); } // Whether expr is (a subobject of) the parameter of a defaulted copy @@ -2310,7 +2307,16 @@ void ConverterRefCount::ConvertFieldAccess(clang::MemberExpr *expr) { SetFreshType(expr->getType()); return; } - StrCat(std::format("{}.{}", ConvertRecordPlace(expr, isLValue()), name)); + auto place = ConvertRecordPlace(expr, isLValue()); + if (!pending_deref_.empty()) { + // A struct reached through a pointer is written through a pointer to the + // field. + auto ptr = pending_deref_.take(); + pending_deref_.set(std::format("field_ptr!({}, {})", ptr, name), + /*fresh=*/true, expr); + return; + } + StrCat(std::format("{}.{}", place, name)); SetValueFreshness(expr->getType()); } @@ -3334,14 +3340,16 @@ void ConverterRefCount::ConvertArrow(clang::Expr *expr) { op && op->getOperator() == clang::OverloadedOperatorKind::OO_Arrow; if (!is_overloaded_arrow || IsUserOperatorCall(op)) { - if (isLValue()) { - auto ptr = ConvertRValue(expr); - StrCat(std::format("(*{}.upgrade().deref_mut())", - DetachPtr(std::move(ptr), isFresh(), expr))); + auto ptr = ToString(expr); + auto pointee_type = expr->getType()->getPointeeType(); + // Virtual methods, which take &mut self, are called through PtrDyn. + if (auto *record = pointee_type->getAsRecordDecl(); + isLValue() && record && abstract_structs_.contains(GetID(record))) { + StrCat(std::format("(*{}.upgrade().deref_mut())", ptr)); } else { - StrCat(DerefPtrExpr(ToString(expr), expr->getType()->getPointeeType())); + StrCat(DerefPtrExpr(ptr, pointee_type)); } - SetValueFreshness(expr->getType()->getPointeeType()); + SetValueFreshness(pointee_type); return; } @@ -3489,11 +3497,16 @@ std::optional ConverterRefCount::ConvertOnFieldReceiver( std::string lets; for (unsigned i = 0; i < all_args.size(); ++i) { auto *arg = all_args[i]; + // A pointer that is computed, not read from a variable, is a fresh + // value, which the closure can take. + bool fresh_ptr = + in_closure && arg->isPRValue() && arg->getType()->isPointerType(); if (arg == receiver || (in_closure ? IsTrivialValue(arg) : !ReadsField(arg)) || hoisted_exprs_.contains(arg) || - !CanHoistArg(ctx_, fragments, GetCalleeOrExpr(expr), i, arg, - TypeIsCopyable(arg->getType()))) { + (!fresh_ptr && + !CanHoistArg(ctx_, fragments, GetCalleeOrExpr(expr), i, arg, + TypeIsCopyable(arg->getType())))) { continue; } auto name = std::format("__a{}", i); @@ -3539,6 +3552,22 @@ std::string ConverterRefCount::ConvertIRFragment( frags = &(*mc)->receiver; chained = true; } + // So is a rule that writes a field reached through a pointer, e.g., + // `a0 = a1.to_owned_opt()`. + if (!receiver_ph) { + auto all_args = BuildUnifiedArgs(expr, args, num_args); + for (const auto &frag : fragments) { + auto *ph = std::get_if(&frag); + if (ph && ph->access == TranslationRule::Access::kBorrowMut) { + if (auto sp = FindStructPtr(all_args[ph->n], /*mut=*/true); + sp && !sp->nested) { + receiver_ph = ph; + chained = true; + } + break; + } + } + } if (receiver_ph && chained) { if (auto str = ConvertOnFieldReceiver( fragments, *receiver_ph, expr, args, num_args, [&] { diff --git a/cpp2rust/converter/models/converter_refcount.h b/cpp2rust/converter/models/converter_refcount.h index 4f15ec3cee..526c903fea 100644 --- a/cpp2rust/converter/models/converter_refcount.h +++ b/cpp2rust/converter/models/converter_refcount.h @@ -165,7 +165,8 @@ class ConverterRefCount final : public Converter { void ConvertFieldAccess(clang::MemberExpr *expr); // The struct whose field `expr` accesses, as a place expression. The struct - // is borrowed mutably if `mut` is set. + // is borrowed mutably if `mut` is set, unless it is reached through a + // pointer, which is then left in pending_deref_. std::string ConvertRecordPlace(clang::MemberExpr *expr, bool mut); // Wraps ptr, the result of converting expr, such that it doesn't keep any diff --git a/docs/src/runtime/rc.md b/docs/src/runtime/rc.md index 4a9b3f32ec..00394b8321 100644 --- a/docs/src/runtime/rc.md +++ b/docs/src/runtime/rc.md @@ -252,20 +252,21 @@ pub enum StrongPtr { } ``` -`deref` returns a `Ref<'_, T>` to the pointee, and `deref_mut` a -`RefMut<'_, T>`. The `Ref` borrows the `StrongPtr`, so the borrow of the -`RefCell` lasts as long as the strong pointer does: in -`p.upgrade().deref().field`, the temporary `StrongPtr` lives until the end of -the enclosing statement, and so does the borrow. The code generator prefers the -`with` and `with_mut` closures, and upgrades only where the result of an access -must borrow the pointee beyond a closure. +`deref` returns a `Ref<'_, T>` to the pointee. The `Ref` borrows the +`StrongPtr`, so the borrow of the `RefCell` lasts as long as the strong pointer +does: in `p.upgrade().deref().field`, the temporary `StrongPtr` lives until the +end of the enclosing statement, and so does the borrow. The code generator +prefers the `with` and `with_mut` closures, and upgrades only where the result +of an access must borrow the pointee beyond a closure. There is no `deref_mut`; +writes go through `write` and `with_mut`. A field of a struct reached through a +pointer, when not written in a `with_mut` closure, is written through a pointer +to the field: `field_ptr!(p, x).write(v)`. For the `Reinterpreted` variant there is no value to reference, only bytes in another allocation. `deref` reads those bytes into a local cell and hands out a -`Ref` to that copy, refreshing it on every call. `deref_mut` panics: a write -must go through `with_mut`, which writes the bytes through to the original -allocation before it returns, so that the write is visible through every other -pointer at once. +`Ref` to that copy, refreshing it on every call. `with_mut` writes the bytes +through to the original allocation before it returns, so that a write is +visible through every other pointer at once. > [!WARNING] > diff --git a/libcc2rs/src/field.rs b/libcc2rs/src/field.rs index b9aae8152e..c1768ab2be 100644 --- a/libcc2rs/src/field.rs +++ b/libcc2rs/src/field.rs @@ -367,8 +367,6 @@ mod tests { a.write(3); assert_eq!(s.borrow().inner.a, 3); assert_eq!(p.upgrade().deref().a, 3); - p.upgrade().deref_mut().a = 4; - assert_eq!(a.read(), 4); p.with_mut(|p| p.a = 5); assert_eq!(a.read(), 5); let b = field_ptr!(p, b); diff --git a/libcc2rs/src/rc.rs b/libcc2rs/src/rc.rs index 326711fdc4..47d505db11 100644 --- a/libcc2rs/src/rc.rs +++ b/libcc2rs/src/rc.rs @@ -5,7 +5,7 @@ use crate::{PostfixDec, PostfixInc, PrefixDec, PrefixInc}; use std::any::{Any, TypeId}; use std::{ - cell::{Ref, RefCell, RefMut}, + cell::{Ref, RefCell}, fmt, ops::Sub, rc::{Rc, Weak}, @@ -80,21 +80,6 @@ impl StrongPtr { } } - // Writes through reinterpreted pointers must use with_mut instead, as - // they are written through to the original allocation. - #[inline(always)] - pub fn deref_mut(&self) -> RefMut<'_, T> { - match self { - StrongPtr::StackSingle(rc) => rc.borrow_mut(), - StrongPtr::Vec { rc, offset } => RefMut::map(rc.borrow_mut(), |v| &mut v[*offset]), - StrongPtr::StackArray { rc, offset } => { - RefMut::map(rc.borrow_mut(), |a| &mut a[*offset]) - } - StrongPtr::Field { root, offset } => Ptr::borrow_field_mut(&**root, *offset), - StrongPtr::Reinterpreted { .. } => panic!("mutable reference to reinterpreted memory"), - } - } - #[inline(never)] fn deref_reinterpreted<'a>( alloc: &OriginalAlloc, diff --git a/tests/unit/out/refcount/operator_member_pointer_member.rs b/tests/unit/out/refcount/operator_member_pointer_member.rs index fcd583a431..26afebeeb9 100644 --- a/tests/unit/out/refcount/operator_member_pointer_member.rs +++ b/tests/unit/out/refcount/operator_member_pointer_member.rs @@ -101,10 +101,7 @@ fn main_0() -> i32 { .x } == 10) ); - (*({ SImpl::operator_arrow(&s.as_pointer()) }) - .upgrade() - .deref_mut()) - .x = 11; + { field_ptr!(({ SImpl::operator_arrow(&s.as_pointer(),) }), x) }.write(11); assert!(({ (*s.borrow()).inner.x } == 11)); let p: Value> = Rc::new(RefCell::new(({ SImpl::operator_addr(&s.as_pointer()) }))); assert!((((*p.borrow()).read()) == 1)); diff --git a/tests/unit/out/refcount/unique_ptr_field_reset.rs b/tests/unit/out/refcount/unique_ptr_field_reset.rs new file mode 100644 index 0000000000..087b435914 --- /dev/null +++ b/tests/unit/out/refcount/unique_ptr_field_reset.rs @@ -0,0 +1,122 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] +pub struct Data { + #[offset(0)] + pub v: i32, +} +impl ByteRepr for Data { + fn byte_size() -> usize { + 4 + } + fn to_bytes(&self, buf: &mut [u8]) { + self.v.to_bytes(&mut buf[0..4]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + v: ::from_bytes(&buf[0..4]), + } + } +} +#[derive(Record, Default)] +pub struct Holder { + #[offset(0)] + pub data: Option>, + #[offset(8)] + pub n: i32, +} +impl Holder { + pub fn move_from(_a0: Ptr) -> Self { + let __this: Value = Rc::new(RefCell::new(Self { + data: { _a0.with_mut(|__s: &mut Holder| __s.data.take()) }, + n: { _a0.with(|__s: &Holder| __s.n) }, + })); + let this: Ptr = __this.as_pointer(); + Rc::try_unwrap(__this).ok().unwrap().into_inner() + } +} +impl ByteRepr for Holder { + fn byte_size() -> usize { + 16 + } + fn to_bytes(&self, buf: &mut [u8]) { + self.data.to_bytes(&mut buf[0..8]); + self.n.to_bytes(&mut buf[8..12]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + data: >>::from_bytes(&buf[0..8]), + n: ::from_bytes(&buf[8..12]), + } + } +} +pub fn main() { + __cpp2rust_init_globals(); + std::process::exit(main_0()); +} +fn main_0() -> i32 { + let h: Value = Rc::new(RefCell::new(::default())); + (*h.borrow_mut()).n = 1; + let hp: Value> = Rc::new(RefCell::new((h.as_pointer()))); + { + { + let __a1 = Ptr::alloc(Data { v: 3 }); + { + (*hp.borrow()).with_mut(|__s: &mut Holder| { + let _p: Ptr<_> = __a1; + __s.data = _p.to_owned_opt() + }) + } + } + }; + assert!(({ (*(*h.borrow()).data.as_ref().unwrap().borrow()).v } == 3)); + ({ HolderImpl::set(&h.as_pointer(), Ptr::alloc(Data { v: 4 })) }); + assert!( + ({ + (*(*(*hp.borrow()).upgrade().deref()) + .data + .as_ref() + .unwrap() + .borrow()) + .v + } == 4) + ); + let __rhs = (*hp.borrow()).with(|__s: &Holder| __s.n); + (*(*(*hp.borrow()).upgrade().deref()) + .data + .as_ref() + .unwrap() + .borrow_mut()) + .v += __rhs; + assert!(({ (*(*h.borrow()).data.as_ref().unwrap().borrow()).v } == 5)); + return 0; +} +pub trait HolderImpl { + fn set(&self, p: Ptr); + fn move_assign(&self, _a0: Ptr) -> Ptr; +} +impl HolderImpl for Ptr { + fn set(&self, p: Ptr) { + let p: Value> = Rc::new(RefCell::new(p)); + { + (*self).with_mut(|__s: &mut Holder| { + let _p: Ptr<_> = (*p.borrow()).clone(); + __s.data = _p.to_owned_opt() + }) + }; + } + fn move_assign(&self, _a0: Ptr) -> Ptr { + (field_ptr!((*self), data) as Ptr>>) + .write(_a0.with_mut(|__s: &mut Holder| __s.data.take())); + let __rhs = _a0.with(|__s: &Holder| __s.n); + (*self).with_mut(|__s: &mut Holder| __s.n = __rhs); + return (*self).clone(); + } +} +pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/unsafe/unique_ptr_field_reset.rs b/tests/unit/out/unsafe/unique_ptr_field_reset.rs new file mode 100644 index 0000000000..daf15e3b5b --- /dev/null +++ b/tests/unit/out/unsafe/unique_ptr_field_reset.rs @@ -0,0 +1,69 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +#[repr(C)] +#[derive(Copy, Clone, VaArg, Default)] +pub struct Data { + pub v: i32, +} +#[repr(C)] +#[derive(Default)] +pub struct Holder { + pub data: Option>, + pub n: i32, +} +impl Holder { + pub unsafe fn set(&mut self, mut p: *mut Data) { + { + let _a0: *mut Data = p; + self.data = if _a0.is_null() { + None + } else { + Some(Box::from_raw(_a0)) + } + }; + } + pub unsafe fn move_from(_a0: *mut Holder) -> Self { + let mut this = Self { + data: (*_a0).data.take(), + n: (*_a0).n, + }; + this + } + pub unsafe fn move_assign(&mut self, _a0: *mut Holder) -> *mut Holder { + self.data = (*_a0).data.take(); + self.n = (*_a0).n; + return &mut (*(self as *mut Holder)); + } +} +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + let mut h: Holder = ::default(); + h.n = 1; + let mut hp: *mut Holder = (&mut h as *mut Holder); + { + let _a0: *mut Data = (Box::leak(Box::new(Data { v: 3 })) as *mut Data); + (*hp).data = if _a0.is_null() { + None + } else { + Some(Box::from_raw(_a0)) + } + }; + assert!((((*h.data.as_deref_mut().unwrap()).v) == (3))); + (unsafe { Holder::set(&mut h, (Box::leak(Box::new(Data { v: 4 })) as *mut Data)) }); + assert!((((*(*hp).data.as_deref_mut().unwrap()).v) == (4))); + (*(*hp).data.as_deref_mut().unwrap()).v += (*hp).n; + assert!((((*h.data.as_deref_mut().unwrap()).v) == (5))); + return 0; +} +pub unsafe fn __cpp2rust_init_globals() {} diff --git a/tests/unit/unique_ptr_field_reset.cpp b/tests/unit/unique_ptr_field_reset.cpp new file mode 100644 index 0000000000..7cc757f40f --- /dev/null +++ b/tests/unit/unique_ptr_field_reset.cpp @@ -0,0 +1,25 @@ +#include +#include + +struct Data { + int v; +}; + +struct Holder { + std::unique_ptr data; + int n; + void set(Data *p) { data.reset(p); } +}; + +int main() { + Holder h; + h.n = 1; + Holder *hp = &h; + hp->data.reset(new Data{3}); + assert(h.data->v == 3); + h.set(new Data{4}); + assert(hp->data->v == 4); + hp->data->v += hp->n; + assert(h.data->v == 5); + return 0; +} From 27b549c9313e1d30500ed4774c592fe9caaed9f8 Mon Sep 17 00:00:00 2001 From: Nuno Lopes Date: Wed, 30 Sep 2026 11:41:17 +0100 Subject: [PATCH 05/13] fix --- cpp2rust/converter/models/converter_refcount.cpp | 5 ++--- docs/src/runtime/rc.md | 4 ++-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/cpp2rust/converter/models/converter_refcount.cpp b/cpp2rust/converter/models/converter_refcount.cpp index 9271af483d..9f205440fe 100644 --- a/cpp2rust/converter/models/converter_refcount.cpp +++ b/cpp2rust/converter/models/converter_refcount.cpp @@ -3504,9 +3504,8 @@ std::optional ConverterRefCount::ConvertOnFieldReceiver( if (arg == receiver || (in_closure ? IsTrivialValue(arg) : !ReadsField(arg)) || hoisted_exprs_.contains(arg) || - (!fresh_ptr && - !CanHoistArg(ctx_, fragments, GetCalleeOrExpr(expr), i, arg, - TypeIsCopyable(arg->getType())))) { + (!fresh_ptr && !CanHoistArg(ctx_, fragments, GetCalleeOrExpr(expr), i, + arg, TypeIsCopyable(arg->getType())))) { continue; } auto name = std::format("__a{}", i); diff --git a/docs/src/runtime/rc.md b/docs/src/runtime/rc.md index 00394b8321..b89afcb05d 100644 --- a/docs/src/runtime/rc.md +++ b/docs/src/runtime/rc.md @@ -265,8 +265,8 @@ to the field: `field_ptr!(p, x).write(v)`. For the `Reinterpreted` variant there is no value to reference, only bytes in another allocation. `deref` reads those bytes into a local cell and hands out a `Ref` to that copy, refreshing it on every call. `with_mut` writes the bytes -through to the original allocation before it returns, so that a write is -visible through every other pointer at once. +through to the original allocation before it returns, so that a write is visible +through every other pointer at once. > [!WARNING] > From 615896171dbb0608082c3b6f244f2bb6ad967440 Mon Sep 17 00:00:00 2001 From: Nuno Lopes Date: Wed, 30 Sep 2026 11:43:01 +0100 Subject: [PATCH 06/13] delete garbage --- .../refcount/reinterpret_cast_u16_to_u8.rs | 48 ------------ .../refcount/reinterpret_cast_u64_write.rs | 31 -------- .../out/unsafe/reinterpret_cast_u16_to_u8.rs | 77 ------------------- .../out/unsafe/reinterpret_cast_u64_write.rs | 36 --------- 4 files changed, 192 deletions(-) delete mode 100644 tests/unit/out/refcount/reinterpret_cast_u16_to_u8.rs delete mode 100644 tests/unit/out/refcount/reinterpret_cast_u64_write.rs delete mode 100644 tests/unit/out/unsafe/reinterpret_cast_u16_to_u8.rs delete mode 100644 tests/unit/out/unsafe/reinterpret_cast_u64_write.rs diff --git a/tests/unit/out/refcount/reinterpret_cast_u16_to_u8.rs b/tests/unit/out/refcount/reinterpret_cast_u16_to_u8.rs deleted file mode 100644 index daf47c606b..0000000000 --- a/tests/unit/out/refcount/reinterpret_cast_u16_to_u8.rs +++ /dev/null @@ -1,48 +0,0 @@ -extern crate libcc2rs; -use libcc2rs::*; -use std::cell::RefCell; -use std::io::Seek; -use std::io::{Read, Write}; -use std::os::fd::AsFd; -use std::os::fd::AsRawFd; -use std::os::fd::FromRawFd; -use std::rc::{Rc, Weak}; -pub fn main() { - std::process::exit(main_0()); -} -fn main_0() -> i32 { - let arr: Value> = Rc::new(RefCell::new(Box::new([513_u16, 1027_u16, 1541_u16]))); - let bytes: Value> = Rc::new(RefCell::new( - (arr.as_pointer() as Ptr).reinterpret_cast::(), - )); - (if ((((*bytes.borrow()).offset((0) as isize).deref()) as i32) == 1) { - } else { - std::panic::panic_any(Ptr::from_string_literal("bytes[0] == 0x01").to_rust_string()) - }); - (if ((((*bytes.borrow()).offset((1) as isize).deref()) as i32) == 2) { - } else { - std::panic::panic_any(Ptr::from_string_literal("bytes[1] == 0x02").to_rust_string()) - }); - (if ((((*bytes.borrow()).offset((2) as isize).deref()) as i32) == 3) { - } else { - std::panic::panic_any(Ptr::from_string_literal("bytes[2] == 0x03").to_rust_string()) - }); - (if ((((*bytes.borrow()).offset((3) as isize).deref()) as i32) == 4) { - } else { - std::panic::panic_any(Ptr::from_string_literal("bytes[3] == 0x04").to_rust_string()) - }); - (if ((((*bytes.borrow()).offset((4) as isize).deref()) as i32) == 5) { - } else { - std::panic::panic_any(Ptr::from_string_literal("bytes[4] == 0x05").to_rust_string()) - }); - (if ((((*bytes.borrow()).offset((5) as isize).deref()) as i32) == 6) { - } else { - std::panic::panic_any(Ptr::from_string_literal("bytes[5] == 0x06").to_rust_string()) - }); - (*(*bytes.borrow()).offset((1) as isize).upgrade().deref_mut()) = 255_u8; - (if (((*arr.borrow())[(0) as usize] as i32) == 65281) { - } else { - std::panic::panic_any(Ptr::from_string_literal("arr[0] == 0xFF01").to_rust_string()) - }); - return 0; -} diff --git a/tests/unit/out/refcount/reinterpret_cast_u64_write.rs b/tests/unit/out/refcount/reinterpret_cast_u64_write.rs deleted file mode 100644 index 5afa571a32..0000000000 --- a/tests/unit/out/refcount/reinterpret_cast_u64_write.rs +++ /dev/null @@ -1,31 +0,0 @@ -extern crate libcc2rs; -use libcc2rs::*; -use std::cell::RefCell; -use std::io::Seek; -use std::io::{Read, Write}; -use std::os::fd::AsFd; -use std::os::fd::AsRawFd; -use std::os::fd::FromRawFd; -use std::rc::{Rc, Weak}; -pub fn main() { - std::process::exit(main_0()); -} -fn main_0() -> i32 { - let val: Value = Rc::new(RefCell::new(0_u64)); - let bytes: Value> = Rc::new(RefCell::new((val.as_pointer()).reinterpret_cast::())); - (*(*bytes.borrow()).offset((0) as isize).upgrade().deref_mut()) = 1_u8; - (*(*bytes.borrow()).offset((1) as isize).upgrade().deref_mut()) = 2_u8; - (*(*bytes.borrow()).offset((2) as isize).upgrade().deref_mut()) = 3_u8; - (*(*bytes.borrow()).offset((3) as isize).upgrade().deref_mut()) = 4_u8; - (*(*bytes.borrow()).offset((4) as isize).upgrade().deref_mut()) = 5_u8; - (*(*bytes.borrow()).offset((5) as isize).upgrade().deref_mut()) = 6_u8; - (*(*bytes.borrow()).offset((6) as isize).upgrade().deref_mut()) = 7_u8; - (*(*bytes.borrow()).offset((7) as isize).upgrade().deref_mut()) = 8_u8; - (if (((*val.borrow()) as u64) == 578437695752307201_u64) { - } else { - std::panic::panic_any( - Ptr::from_string_literal("val == 0x0807060504030201ULL").to_rust_string(), - ) - }); - return 0; -} diff --git a/tests/unit/out/unsafe/reinterpret_cast_u16_to_u8.rs b/tests/unit/out/unsafe/reinterpret_cast_u16_to_u8.rs deleted file mode 100644 index 2b6ce409b1..0000000000 --- a/tests/unit/out/unsafe/reinterpret_cast_u16_to_u8.rs +++ /dev/null @@ -1,77 +0,0 @@ -extern crate libc; -use libc::*; -extern crate libcc2rs; -use libcc2rs::*; -use std::io::Seek; -use std::io::{Read, Write}; -use std::os::fd::AsFd; -use std::os::fd::AsRawFd; -use std::os::fd::FromRawFd; -use std::rc::Rc; -pub fn main() { - unsafe { - std::process::exit(main_0() as i32); - } -} -unsafe fn main_0() -> i32 { - let mut arr: [u16; 3] = [513_u16, 1027_u16, 1541_u16]; - let mut bytes: *mut u8 = (arr.as_mut_ptr() as *mut u8); - (if (((*bytes.offset((0) as isize)) as i32) == (1)) { - } else { - std::panic::panic_any( - ::std::ffi::CStr::from_ptr(b"bytes[0] == 0x01\0".as_ptr() as *const i8) - .to_str() - .unwrap(), - ) - }); - (if (((*bytes.offset((1) as isize)) as i32) == (2)) { - } else { - std::panic::panic_any( - ::std::ffi::CStr::from_ptr(b"bytes[1] == 0x02\0".as_ptr() as *const i8) - .to_str() - .unwrap(), - ) - }); - (if (((*bytes.offset((2) as isize)) as i32) == (3)) { - } else { - std::panic::panic_any( - ::std::ffi::CStr::from_ptr(b"bytes[2] == 0x03\0".as_ptr() as *const i8) - .to_str() - .unwrap(), - ) - }); - (if (((*bytes.offset((3) as isize)) as i32) == (4)) { - } else { - std::panic::panic_any( - ::std::ffi::CStr::from_ptr(b"bytes[3] == 0x04\0".as_ptr() as *const i8) - .to_str() - .unwrap(), - ) - }); - (if (((*bytes.offset((4) as isize)) as i32) == (5)) { - } else { - std::panic::panic_any( - ::std::ffi::CStr::from_ptr(b"bytes[4] == 0x05\0".as_ptr() as *const i8) - .to_str() - .unwrap(), - ) - }); - (if (((*bytes.offset((5) as isize)) as i32) == (6)) { - } else { - std::panic::panic_any( - ::std::ffi::CStr::from_ptr(b"bytes[5] == 0x06\0".as_ptr() as *const i8) - .to_str() - .unwrap(), - ) - }); - (*bytes.offset((1) as isize)) = 255_u8; - (if ((arr[(0) as usize] as i32) == (65281)) { - } else { - std::panic::panic_any( - ::std::ffi::CStr::from_ptr(b"arr[0] == 0xFF01\0".as_ptr() as *const i8) - .to_str() - .unwrap(), - ) - }); - return 0; -} diff --git a/tests/unit/out/unsafe/reinterpret_cast_u64_write.rs b/tests/unit/out/unsafe/reinterpret_cast_u64_write.rs deleted file mode 100644 index a796aaf797..0000000000 --- a/tests/unit/out/unsafe/reinterpret_cast_u64_write.rs +++ /dev/null @@ -1,36 +0,0 @@ -extern crate libc; -use libc::*; -extern crate libcc2rs; -use libcc2rs::*; -use std::io::Seek; -use std::io::{Read, Write}; -use std::os::fd::AsFd; -use std::os::fd::AsRawFd; -use std::os::fd::FromRawFd; -use std::rc::Rc; -pub fn main() { - unsafe { - std::process::exit(main_0() as i32); - } -} -unsafe fn main_0() -> i32 { - let mut val: u64 = 0_u64; - let mut bytes: *mut u8 = ((&mut val as *mut u64) as *mut u8); - (*bytes.offset((0) as isize)) = 1_u8; - (*bytes.offset((1) as isize)) = 2_u8; - (*bytes.offset((2) as isize)) = 3_u8; - (*bytes.offset((3) as isize)) = 4_u8; - (*bytes.offset((4) as isize)) = 5_u8; - (*bytes.offset((5) as isize)) = 6_u8; - (*bytes.offset((6) as isize)) = 7_u8; - (*bytes.offset((7) as isize)) = 8_u8; - (if ((val as u64) == (578437695752307201_u64)) { - } else { - std::panic::panic_any( - ::std::ffi::CStr::from_ptr(b"val == 0x0807060504030201ULL\0".as_ptr() as *const i8) - .to_str() - .unwrap(), - ) - }); - return 0; -} From 5c38e53b155927bda05a538e40877358b9c5ee78 Mon Sep 17 00:00:00 2001 From: Nuno Lopes Date: Wed, 30 Sep 2026 14:39:04 +0100 Subject: [PATCH 07/13] fix --- .gitignore | 2 + cpp2rust/converter/converter.cpp | 13 +- cpp2rust/converter/converter.h | 9 +- .../converter/models/converter_refcount.cpp | 957 +++--------------- .../converter/models/converter_refcount.h | 132 +-- docs/src/codegen/types/boxing.md | 2 +- docs/src/codegen/types/pointers.md | 28 +- docs/src/runtime/libc-shims.md | 33 +- docs/src/runtime/rc.md | 18 +- libcc2rs-macros/src/lib.rs | 3 +- libcc2rs-macros/src/record.rs | 32 +- libcc2rs/src/field.rs | 102 ++ libcc2rs/src/lib.rs | 2 +- rules/dirent/shim.rs | 45 +- rules/ifaddrs/shim.rs | 46 +- rules/ifaddrs/tgt_refcount.rs | 12 +- rules/ip/shim.rs | 21 +- rules/netdb/shim.rs | 48 +- rules/netdb/tgt_refcount.rs | 33 +- rules/poll/shim.rs | 28 +- rules/poll/tgt_refcount.rs | 10 +- rules/pwd/shim.rs | 66 +- rules/select/tgt_refcount.rs | 10 +- rules/socket/shim.rs | 200 ++-- rules/stat/shim.rs | 94 +- rules/termios/shim.rs | 130 +-- rules/time/shim.rs | 142 ++- rules/time/tgt_refcount.rs | 24 +- rules/unique_ptr/tgt_refcount.rs | 12 +- .../out/refcount/defaulted_move_cross_tu.rs | 31 +- .../out/refcount/friend_operator.rs | 20 +- .../out/refcount/opaque_forward_decl.rs | 2 +- .../refcount/out_of_line_methods_cross_tu.rs | 11 +- .../template_method_late_instantiation.rs | 4 +- tests/ub/out/refcount/ctor_ref_member.rs | 2 +- tests/ub/out/refcount/ub6.rs | 4 +- tests/unit/out/refcount/10_struct.rs | 44 +- tests/unit/out/refcount/addr_of_global.rs | 12 +- tests/unit/out/refcount/array_const_init.rs | 8 +- .../out/refcount/array_of_noncopy_struct.rs | 23 +- tests/unit/out/refcount/array_reference.rs | 20 +- tests/unit/out/refcount/as_const.rs | 4 +- tests/unit/out/refcount/bounded_struct_ptr.rs | 2 +- tests/unit/out/refcount/bst.rs | 40 +- tests/unit/out/refcount/c_struct.rs | 2 +- tests/unit/out/refcount/class.rs | 28 +- .../refcount/class_template_lazy_member.rs | 4 +- tests/unit/out/refcount/class_templates.rs | 80 +- tests/unit/out/refcount/clone_vs_move.rs | 50 +- tests/unit/out/refcount/complex_function.rs | 109 +- .../out/refcount/compound_assign_paren.rs | 28 +- tests/unit/out/refcount/constexpr_function.rs | 4 +- tests/unit/out/refcount/constructor.rs | 17 +- tests/unit/out/refcount/copy_assign.rs | 40 +- tests/unit/out/refcount/copy_ctor.rs | 33 +- .../unit/out/refcount/copy_move_defaulted.rs | 293 +++--- tests/unit/out/refcount/copy_move_deleted.rs | 36 +- tests/unit/out/refcount/default.rs | 20 +- tests/unit/out/refcount/default_in_statics.rs | 27 +- tests/unit/out/refcount/destructor.rs | 32 +- tests/unit/out/refcount/doubly_linked_list.rs | 130 ++- tests/unit/out/refcount/enum_int_interop_c.rs | 3 +- tests/unit/out/refcount/exprs.rs | 12 +- tests/unit/out/refcount/fft.rs | 76 +- .../unit/out/refcount/field_array_elem_ptr.rs | 82 +- tests/unit/out/refcount/field_pointers.rs | 93 +- tests/unit/out/refcount/fn_ptr_stable_sort.rs | 37 +- .../refcount/foreach_disjoint_field_borrow.rs | 14 +- tests/unit/out/refcount/forward_basic.rs | 14 +- tests/unit/out/refcount/forward_decltype.rs | 14 +- tests/unit/out/refcount/forward_explicit.rs | 14 +- tests/unit/out/refcount/forward_pack.rs | 14 +- tests/unit/out/refcount/friend.rs | 16 +- tests/unit/out/refcount/fstat.rs | 2 +- .../out/refcount/global_non_const_init.rs | 6 +- tests/unit/out/refcount/goto_cleanup.rs | 2 +- tests/unit/out/refcount/huffman.rs | 267 ++--- tests/unit/out/refcount/ifaddrs.rs | 56 +- .../refcount/immutable-deref-on-func-call.rs | 19 +- tests/unit/out/refcount/implicit_autoref.rs | 27 +- tests/unit/out/refcount/init.rs | 4 +- tests/unit/out/refcount/kruskal.rs | 393 +++---- .../unit/out/refcount/libc_char_ptr_field.rs | 6 +- .../refcount/libc_struct_without_default.rs | 53 +- tests/unit/out/refcount/linked_list.rs | 28 +- tests/unit/out/refcount/map.rs | 13 +- tests/unit/out/refcount/move_assign.rs | 29 +- tests/unit/out/refcount/move_ctor.rs | 58 +- tests/unit/out/refcount/move_this.rs | 16 +- tests/unit/out/refcount/netdb.rs | 202 ++-- tests/unit/out/refcount/new.rs | 8 +- tests/unit/out/refcount/new_bst.rs | 44 +- tests/unit/out/refcount/new_struct.rs | 13 +- tests/unit/out/refcount/offsetof.rs | 12 +- .../unit/out/refcount/opaque_then_defined.rs | 2 +- .../out/refcount/operator_arithmetic_free.rs | 44 +- .../refcount/operator_arithmetic_member.rs | 42 +- .../out/refcount/operator_bitwise_free.rs | 18 +- .../out/refcount/operator_bitwise_member.rs | 18 +- .../refcount/operator_comparison_defaulted.rs | 72 +- .../out/refcount/operator_comparison_free.rs | 28 +- .../refcount/operator_comparison_member.rs | 26 +- .../out/refcount/operator_comparison_mixed.rs | 26 +- .../operator_comparison_noncopyable.rs | 10 +- .../operator_compound_assignment_free.rs | 51 +- .../operator_compound_assignment_member.rs | 53 +- tests/unit/out/refcount/operator_friend.rs | 48 +- tests/unit/out/refcount/operator_less_than.rs | 12 +- .../out/refcount/operator_logical_free.rs | 6 +- .../out/refcount/operator_logical_member.rs | 6 +- .../refcount/operator_member_pointer_free.rs | 31 +- .../operator_member_pointer_member.rs | 37 +- .../unit/out/refcount/operator_other_free.rs | 4 +- .../out/refcount/operator_other_member.rs | 14 +- tests/unit/out/refcount/operator_overloads.rs | 26 +- .../unit/out/refcount/operator_qualifiers.rs | 18 +- tests/unit/out/refcount/operator_three_way.rs | 12 +- tests/unit/out/refcount/operator_traits.rs | 91 +- tests/unit/out/refcount/overload_mangling.rs | 42 +- tests/unit/out/refcount/pod.rs | 15 +- tests/unit/out/refcount/pointer_array.rs | 14 +- tests/unit/out/refcount/pointers.rs | 17 +- tests/unit/out/refcount/poll.rs | 28 +- tests/unit/out/refcount/ptr_cast_init.rs | 2 +- tests/unit/out/refcount/push_emplace_back.rs | 167 +-- tests/unit/out/refcount/pwd.rs | 25 +- tests/unit/out/refcount/random.rs | 178 ++-- tests/unit/out/refcount/reference_wrapper.rs | 4 +- .../reinterpret_cast_struct_to_struct.rs | 6 +- tests/unit/out/refcount/rule_of_five.rs | 54 +- tests/unit/out/refcount/rule_of_three.rs | 34 +- tests/unit/out/refcount/rvalue_struct.rs | 4 +- tests/unit/out/refcount/select.rs | 22 +- tests/unit/out/refcount/strdup.rs | 11 +- tests/unit/out/refcount/struct_ctor.rs | 4 +- tests/unit/out/refcount/struct_ptr.rs | 6 +- tests/unit/out/refcount/sys_stat.rs | 8 +- tests/unit/out/refcount/sys_time.rs | 48 +- .../refcount/tag_vs_identifier_collision.rs | 2 +- .../unit/out/refcount/template_default_arg.rs | 2 +- tests/unit/out/refcount/this.rs | 52 +- .../unit/out/refcount/typedef-anon-struct.rs | 32 +- .../out/refcount/union_addrof_external.rs | 4 +- .../unit/out/refcount/union_cross_arm_cast.rs | 21 +- .../out/refcount/union_field_alignment.rs | 3 +- .../out/refcount/union_flex_array_member.rs | 20 +- .../unit/out/refcount/union_memset_memcpy.rs | 8 +- tests/unit/out/refcount/union_nested.rs | 19 +- .../out/refcount/union_struct_dual_use.rs | 14 +- .../out/refcount/union_tagged_many_arms.rs | 16 +- .../unit/out/refcount/union_tagged_simple.rs | 7 +- .../out/refcount/union_tagged_struct_arms.rs | 92 +- .../refcount/union_void_ptr_sized_deref.rs | 41 +- tests/unit/out/refcount/unique_ptr.rs | 158 +-- .../out/refcount/unique_ptr_const_deref.rs | 15 +- .../out/refcount/unique_ptr_field_reset.rs | 41 +- tests/unit/out/refcount/unique_ptr_nested.rs | 13 +- .../out/refcount/va_arg_non_primitive_ptrs.rs | 2 +- tests/unit/out/refcount/va_arg_struct_ctx.rs | 5 +- tests/unit/out/refcount/va_arg_void_ptr.rs | 6 +- .../out/refcount/vector_subscript_direct.rs | 100 +- tests/unit/out/refcount/void_cast.rs | 14 +- 162 files changed, 3259 insertions(+), 3873 deletions(-) diff --git a/.gitignore b/.gitignore index 36280d4c8d..93d8ab724e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,8 @@ # CMake build/ _buildconfig.py +Cargo.lock +target/ rules/rustls/rustls.h rules/brotli/brotli/ diff --git a/cpp2rust/converter/converter.cpp b/cpp2rust/converter/converter.cpp index f7622950d3..2813ecc44d 100644 --- a/cpp2rust/converter/converter.cpp +++ b/cpp2rust/converter/converter.cpp @@ -2437,6 +2437,7 @@ bool Converter::VisitImplicitCastExpr(clang::ImplicitCastExpr *expr) { case clang::CastKind::CK_LValueToRValue: { PushExprKind push(*this, ExprKind::RValue); Convert(sub_expr); + // The value may have been copied out already. if (!isFresh()) { SetValueFreshness(type); } @@ -4838,16 +4839,8 @@ void Converter::PlaceholderCtx::dump() const { << ", materialize_idx: " << materialize_idx << '\n'; } -std::string Converter::ConvertTake(clang::Expr *arg) { - return std::format("std::mem::take(&mut {})", ConvertLValue(arg)); -} - std::string Converter::ConvertPlaceholder(clang::Expr *expr, clang::Expr *arg, const PlaceholderCtx &ph_ctx) { - if (auto it = hoisted_exprs_.find(arg); it != hoisted_exprs_.end()) { - return it->second; - } - if (arg->getType()->isFunctionPointerType()) { return ConvertFnPtrPlaceholder(arg); } @@ -4915,9 +4908,9 @@ std::string Converter::ConvertPlaceholder(clang::Expr *expr, clang::Expr *arg, } return ConvertFreshRValue(arg); } - auto str = ConvertTake(arg); + auto lvalue = ConvertLValue(arg); SetFresh(); - return str; + return std::format("std::mem::take(&mut {})", std::move(lvalue)); } if (ph_ctx.access == TranslationRule::Access::kMove) { diff --git a/cpp2rust/converter/converter.h b/cpp2rust/converter/converter.h index 64cfe40a5f..87a85f0f0e 100644 --- a/cpp2rust/converter/converter.h +++ b/cpp2rust/converter/converter.h @@ -340,9 +340,6 @@ class Converter : public clang::RecursiveASTVisitor { std::string ConvertFnPtrCallee(clang::Expr *arg); virtual std::string ConvertFnPtrPlaceholder(clang::Expr *arg); - // Moves the value of the lvalue arg out, leaving a default value behind. - virtual std::string ConvertTake(clang::Expr *arg); - // Option implements Copy virtual bool FunctionPointerImplementsCopy() const { return true; } @@ -704,7 +701,7 @@ class Converter : public clang::RecursiveASTVisitor { unsigned num_args = 0, TempMaterializationCtx *ctx = nullptr); - virtual std::string + std::string ConvertIRFragment(const std::vector &fragments, clang::Expr *expr, clang::Expr **args, unsigned num_args, TempMaterializationCtx *ctx); @@ -768,10 +765,6 @@ class Converter : public clang::RecursiveASTVisitor { }; std::string ufcs_receiver_; - - // Expressions that were evaluated into local variables ahead of the - // expression that contains them, and the names of these variables. - std::unordered_map hoisted_exprs_; bool in_const_initializer_ = false; std::optional autoref_mut_; bool suppress_iterator_clone_ = false; diff --git a/cpp2rust/converter/models/converter_refcount.cpp b/cpp2rust/converter/models/converter_refcount.cpp index 9f205440fe..afdd8fc773 100644 --- a/cpp2rust/converter/models/converter_refcount.cpp +++ b/cpp2rust/converter/models/converter_refcount.cpp @@ -55,6 +55,8 @@ static bool IsBoxedType(clang::ASTContext &ctx, clang::QualType type) { return IsBoxedType(Mapper::Map(ctx, type.getUnqualifiedType())); } +// Virtual methods take &mut self, as the fields of the struct are stored in +// it. static bool NeedsMutAccess(clang::ASTContext &ctx, const clang::CXXMethodDecl *method, clang::QualType base_type) { @@ -62,67 +64,18 @@ static bool NeedsMutAccess(clang::ASTContext &ctx, (IsBoxedType(ctx, base_type) || method->isVirtual()); } -static bool IsPointerType(clang::ASTContext &ctx, clang::QualType type) { - return type->isPointerType() || GetStrongestIteratorCategory(ctx, type) == - IteratorCategory::Contiguous; -} - -static bool IsTranslatedStruct(const clang::RecordDecl *decl) { - return decl && !decl->isUnion() && IsUserDefinedDecl(decl); -} - -// Whether evaluating expr doesn't access memory other than local variables. -static bool IsTrivialValue(const clang::Expr *expr) { - expr = expr->IgnoreParenImpCasts(); - if (clang::isa(expr)) { - return true; - } - auto *ref = clang::dyn_cast(expr); - auto *var = ref ? clang::dyn_cast(ref->getDecl()) : nullptr; - return (ref && clang::isa(ref->getDecl())) || - (var && var->isLocalVarDeclOrParm() && - !var->getType()->isReferenceType()); -} - -// Whether expr accesses a field of a struct that is stored in the struct. -static bool IsFieldAccess(const clang::Expr *expr) { - auto *member = clang::dyn_cast(expr); - auto *field = member - ? clang::dyn_cast(member->getMemberDecl()) - : nullptr; - return field && IsTranslatedStruct(field->getParent()) && - !field->getType()->isReferenceType(); -} - -// Whether the field holds a Value of its own, instead of being stored in the -// struct. Arrays and vectors are, so that pointers to their elements, and to -// the fields of those, point into the array and not the struct. -static bool IsValueField(clang::ASTContext &ctx, const clang::ValueDecl *decl) { - auto *field = clang::dyn_cast(decl); - if (!field || !IsTranslatedStruct(field->getParent())) { - return false; - } +// Whether a field is stored in a Value of its own, like a variable, instead of +// inline in its struct. Arrays and vectors are, so that pointers to their +// elements point into them. +static bool IsValueField(clang::ASTContext &ctx, + const clang::FieldDecl *field) { auto type = field->getType(); - return !type->isReferenceType() && - (type->isConstantArrayType() || IsBoxedType(ctx, type)); + return type->isConstantArrayType() || IsBoxedType(ctx, type); } -// Whether evaluating expr reads a field of a struct, and hence borrows the -// struct until the end of the statement. -static bool ReadsField(const clang::Stmt *stmt) { - if (!stmt) { - return false; - } - if (auto *member = clang::dyn_cast(stmt)) { - if (auto *field = - clang::dyn_cast(member->getMemberDecl()); - field && IsTranslatedStruct(field->getParent())) { - return true; - } - } - return std::ranges::any_of(stmt->children(), ReadsField); +static bool IsPointerType(clang::ASTContext &ctx, clang::QualType type) { + return type->isPointerType() || GetStrongestIteratorCategory(ctx, type) == + IteratorCategory::Contiguous; } bool ConverterRefCount::PendingDeref::compute_inner_boxed( @@ -416,28 +369,9 @@ std::string ConverterRefCount::ConvertFresh( std::string ConverterRefCount::ConvertFreshRValue( clang::Expr *expr, std::optional implicit_convert_to) { - if (auto str = ConvertInStructPtr(expr, false, [&] { - return ConvertFreshRValue(expr, implicit_convert_to); - })) { - SetFreshType(expr->getType()); - return *str; - } - // A copy of a struct pointed to. - if (auto *object = expr->IgnoreParenImpCasts(); - IsTranslatedStruct(expr->getType()->getAsRecordDecl()) && - ((clang::isa(object) && - clang::cast(object) - ->getDecl() - ->getType() - ->isReferenceType()) || - (clang::isa(object) && - clang::cast(object)->getOpcode() == - clang::UO_Deref))) { - auto ptr = ConvertPointer(object); - SetFreshType(expr->getType()); - return std::format("{}.read()", ptr); - } + auto *copied = std::exchange(copied_expr_, expr->IgnoreParenImpCasts()); auto str = ConvertRValue(expr, implicit_convert_to); + copied_expr_ = copied; if (!isFresh() && !expr->getType()->isVoidType()) { SetFresh(); return std::format("({}).clone()", std::move(str)); @@ -483,35 +417,9 @@ std::string ConverterRefCount::ConvertPtrType(clang::QualType type) { return std::format("Ptr<{}>", std::move(str)); } -bool ConverterRefCount::ConvertWithHoistedIndex( - clang::Expr *expr, clang::Expr *base, clang::Expr *idx, - llvm::function_ref convert) { - // The value of the whole block must be fresh. - if (isLValue() || (!isAddrOf() && !TypeIsCopyable(expr->getType())) || - hoisted_exprs_.contains(idx) || !idx->HasSideEffects(ctx_) || - !ReadsField(base)) { - return false; - } - auto idx_str = ConvertRValue(idx); - hoisted_exprs_.emplace(idx, "__idx"); - std::string str; - { - Buffer buf(*this); - convert(); - str = std::move(buf).str(); - } - hoisted_exprs_.erase(idx); - StrCat(std::format("({{ let __idx = {}; {} }})", idx_str, str)); - return true; -} - bool ConverterRefCount::VisitArraySubscriptExpr( clang::ArraySubscriptExpr *expr) { auto *base = expr->getBase(); - if (ConvertWithHoistedIndex(expr, base, expr->getIdx(), - [&] { VisitArraySubscriptExpr(expr); })) { - return false; - } if (base->IgnoreCasts()->getType()->isPointerType() || IsUnionArrayMember(base) || (IsReferenceType(base) && @@ -569,36 +477,15 @@ ConverterRefCount::GetComparisonReceiver(const clang::CXXMethodDecl *, return std::format("&{}.as_pointer()", BoxValue(GetShallowCopy(decl, lhs))); } -std::string ConverterRefCount::DetachPtr(std::string ptr, bool fresh, - const clang::Expr *expr) { - if (auto trimmed = Trim(ptr); - !ReadsField(expr) || - (fresh && trimmed.starts_with('{') && trimmed.ends_with('}'))) { - return ptr; - } - // The temporaries of the tail expression of a block are dropped at the end - // of the block. - return std::format("{{ {}{} }}", ptr, fresh ? "" : ".clone()"); -} - -std::string -ConverterRefCount::TakePendingDerefToWrite(const clang::Expr *lvalue) { - bool fresh = pending_deref_.is_fresh(); - return DetachPtr(pending_deref_.take(), fresh, lvalue); -} - -// A copy of `src` that doesn't run the copy constructors of its fields, and -// hence has no side effects. It stands for `src` itself where a pointer to it -// is needed, but only a reference is available. std::string ConverterRefCount::GetShallowCopy(const clang::RecordDecl *decl, std::string_view src) { std::string fields; for (auto *field : decl->fields()) { auto name = GetNamedDeclAsString(field); - auto type = field->getType(); auto value = std::format("{}.{}", src, name); - // Value fields are shared. - if (auto *record = type->getAsRecordDecl(); IsTranslatedStruct(record)) { + // Fields are copied without running their copy constructors. + if (auto *record = field->getType()->getAsRecordDecl(); + record && IsUserDefinedDecl(record) && !IsValueField(ctx_, field)) { value = GetShallowCopy(record, value); } else { value += ".clone()"; @@ -608,20 +495,6 @@ std::string ConverterRefCount::GetShallowCopy(const clang::RecordDecl *decl, return std::format("{} {{ {} }}", GetRecordName(decl), fields); } -// A copy of `src`, of type `type`, that copies the arrays nested in Values -// instead of sharing them. -std::string ConverterRefCount::GetDeepCopy(clang::QualType type, - std::string_view src) { - auto *arr = ctx_.getAsConstantArrayType(type); - if (!arr || !arr->getElementType()->isArrayType()) { - return std::format("{}.clone()", src); - } - auto elem = std::format("__e{}", src.size()); - return std::format( - "{}.iter().map(|{}| Rc::new(RefCell::new({}))).collect()", src, elem, - GetDeepCopy(arr->getElementType(), std::format("(*{}.borrow())", elem))); -} - bool ConverterRefCount::RecordImplementsClone(const clang::RecordDecl *decl) { if (decl->isUnion() || (HasDefaultedCopyConstructor(decl) && RecordHasOnlyReferenceFields(decl))) { @@ -632,23 +505,20 @@ bool ConverterRefCount::RecordImplementsClone(const clang::RecordDecl *decl) { } bool ConverterRefCount::RecordDerivesClone(const clang::RecordDecl *decl) { - if (decl->isUnion()) { - return false; - } if (HasDefaultedCopyConstructor(decl) && RecordHasOnlyReferenceFields(decl)) { return true; } - // A C struct is copied field by field, as is a C++ one with an implicit or - // defaulted copy constructor. Nested Values must be copied deeply, though. - if (auto *cxx = clang::dyn_cast(decl); - cxx && (!HasCallableCopyConstructor(cxx) || - GetUserDefinedCopyConstructor(cxx) || cxx->getNumBases() > 0)) { + // The copy of a struct copies each field, which a derived Clone does too, + // except for Values, which it shares. + auto *cxx = clang::dyn_cast(decl); + if (decl->isUnion() || + (cxx && (!HasCallableCopyConstructor(cxx) || + GetUserDefinedCopyConstructor(cxx) || cxx->getNumBases() > 0))) { return false; } PushConversionKind push(*this, ConversionKind::Pointee); return std::ranges::none_of(decl->fields(), [&](auto *field) { - return IsValueField(ctx_, field) || - ToString(field->getType()).contains("Value<"); + return IsValueField(ctx_, field) || ToString(field->getType()).contains("Value<"); }); } @@ -665,12 +535,14 @@ void ConverterRefCount::AddCloneTrait(const clang::RecordDecl *decl) { return; } - if (RecordDerivesClone(decl)) { + if (RecordDerivesClone(decl) || !RecordImplementsClone(decl)) { return; } + // Without a user-defined copy constructor, each field is copied with its + // own clone(), and Values are copied deeply. auto *cxx = clang::dyn_cast(decl); - if (!cxx) { - // A C struct with nested Values, which are copied deeply. + if (!cxx || + (!GetUserDefinedCopyConstructor(cxx) && cxx->getNumBases() == 0)) { StrCat(keyword::kImpl, "Clone for", record_name); PushBrace impl_brace(*this); StrCat("fn clone(&self) -> Self"); @@ -679,22 +551,16 @@ void ConverterRefCount::AddCloneTrait(const clang::RecordDecl *decl) { PushBrace init_brace(*this); for (auto *field : decl->fields()) { auto name = GetNamedDeclAsString(field); - StrCat(std::format( - "{}: {},", name, - IsValueField(ctx_, field) - ? std::format( - "Rc::new(RefCell::new({}))", - GetDeepCopy(field->getType(), - std::format("(*self.{}.borrow())", name))) - : GetDeepCopy(field->getType(), "self." + name))); + auto value = std::format("self.{}.clone()", name); + if (IsValueField(ctx_, field)) { + value = std::format("Rc::new(RefCell::new((*self.{}.borrow()).clone()))", + name); + } + StrCat(std::format("{}: {},", name, value)); } return; } - if (!RecordImplementsClone(decl)) { - return; - } - StrCat(keyword::kImpl, "Clone for", record_name, '{'); StrCat("fn clone(&self) -> Self {"); @@ -719,7 +585,7 @@ void ConverterRefCount::AddCloneTrait(const clang::RecordDecl *decl) { } void ConverterRefCount::AddDefaultTrait(const clang::RecordDecl *decl) { - PushConversionKind push(*this, ConversionKind::Pointee); + PushConversionKind push(*this, ConversionKind::FullRefCount); Converter::AddDefaultTrait(decl); } @@ -774,7 +640,7 @@ void ConverterRefCount::AddByteReprTrait(const clang::RecordDecl *decl) { auto struct_name = GetRecordName(decl); if (!TypeImplementsByteRepr(ctx_.getCanonicalTagType(decl))) { - // The size locates the fields of arrays of structs. + // The size locates the fields of the elements of arrays of structs. StrCat(std::format("impl ByteRepr for {}", struct_name)); PushBrace brace(*this); if (!decl->isUnion()) { @@ -810,12 +676,12 @@ void ConverterRefCount::AddByteReprTrait(const clang::RecordDecl *decl) { for (auto *field : decl->fields()) { auto byte_off = layout.getFieldOffset(idx) / 8; auto byte_size = ctx_.getTypeSize(field->getType()) / 8; - auto name = GetNamedDeclAsString(field); - StrCat(std::format("{}.to_bytes(&mut buf[{}..{}]);", - IsValueField(ctx_, field) - ? std::format("(*self.{}.borrow())", name) - : "self." + name, - byte_off, byte_off + byte_size)); + auto value = "self." + GetNamedDeclAsString(field); + if (IsValueField(ctx_, field)) { + value = std::format("(*{}.borrow())", value); + } + StrCat(std::format("{}.to_bytes(&mut buf[{}..{}]);", value, byte_off, + byte_off + byte_size)); ++idx; } } @@ -829,10 +695,11 @@ void ConverterRefCount::AddByteReprTrait(const clang::RecordDecl *decl) { for (auto *field : decl->fields()) { auto byte_off = layout.getFieldOffset(idx) / 8; auto byte_size = ctx_.getTypeSize(field->getType()) / 8; - PushConversionKind push(*this, ConversionKind::Pointee); - auto value = std::format("<{}>::from_bytes(&buf[{}..{}])", - ToString(field->getType()), byte_off, - byte_off + byte_size); + PushConversionKind push(*this, ConversionKind::FullRefCount); + std::string storage_ty = ToString(field->getType()); + Unwrap(storage_ty, "Value<", ">"); + auto value = std::format("<{}>::from_bytes(&buf[{}..{}])", storage_ty, + byte_off, byte_off + byte_size); if (IsValueField(ctx_, field)) { value = std::format("Rc::new(RefCell::new({}))", value); } @@ -844,8 +711,6 @@ void ConverterRefCount::AddByteReprTrait(const clang::RecordDecl *decl) { std::string ConverterRefCount::GetSelfMaybeWithMut(const clang::CXXMethodDecl *decl) { - // Methods that are not on Ptr, like virtual ones, write to the fields - // through self. return NeedsMutAccess(ctx_, decl, decl->getThisType()->getPointeeType()) ? "&mut self" : "&self"; @@ -858,38 +723,16 @@ bool ConverterRefCount::VisitCXXConstructorDecl( } bool ConverterRefCount::VisitFieldDecl(clang::FieldDecl *decl) { - if (!decl->getParent()->isUnion()) { - const auto &layout = ctx_.getASTRecordLayout(decl->getParent()); - StrCat(std::format("#[offset({})]", - layout.getFieldOffset(decl->getFieldIndex()) / 8)); - } + // The C offset of the field, which locates it for pointers to it. + const auto &layout = ctx_.getASTRecordLayout(decl->getParent()); + StrCat(std::format("#[offset({})]", + layout.getFieldOffset(decl->getFieldIndex()) / 8)); PushConversionKind push(*this, IsValueField(ctx_, decl) ? ConversionKind::FullRefCount : ConversionKind::Pointee); return Converter::VisitFieldDecl(decl); } -void ConverterRefCount::ConvertFieldInit(const clang::FieldDecl *field, - clang::Expr *init) { - if (!IsValueField(ctx_, field)) { - Converter::ConvertFieldInit(field, init); - return; - } - PushConversionKind push(*this, ConversionKind::FullRefCount); - if (!init || !ReadsField(init)) { - Converter::ConvertFieldInit(field, init); - return; - } - // Like in ConvertVarInit, the initializer must not keep a struct borrowed. - std::string str; - { - Buffer buf(*this); - Converter::ConvertFieldInit(field, init); - str = std::move(buf).str(); - } - StrCat(std::format("{{ {} }}", str)); -} - void ConverterRefCount::EmitFunctionPreamble(clang::FunctionDecl *decl) { // In the header, the function might be declared as `int foo(int name_1)', // while in the source file the function might be defined as `int foo(int @@ -1030,17 +873,9 @@ bool ConverterRefCount::ConvertIncAndDec(clang::UnaryOperator *expr) { return false; } - if (auto str = ConvertInStructPtr(sub_expr, true, [&] { - return std::format("{}.{}()", ConvertLValue(sub_expr), method); - })) { - StrCat(*str); - SetFreshType(expr->getType()); - return true; - } auto str = ConvertLValue(sub_expr); if (!pending_deref_.empty()) { - StrCat(TakePendingDerefToWrite(sub_expr), ".with_mut(|__v| __v.", method, - "())"); + StrCat(pending_deref_.take(), ".with_mut(|__v| __v.", method, "())"); } else { StrCat(str, '.', method, "()"); } @@ -1889,7 +1724,6 @@ bool ConverterRefCount::VisitInitListExpr(clang::InitListExpr *expr) { { PushBrace brace(*this); unsigned i = 0; - PushConversionKind push(*this, ConversionKind::Pointee); for (const auto *field : record->fields()) { StrCat(GetNamedDeclAsString(field), token::kColon); ConvertFieldInit(field, i < expr->getNumInits() ? expr->getInit(i++) @@ -1986,7 +1820,8 @@ bool ConverterRefCount::VisitMemberExpr(clang::MemberExpr *expr) { SetFreshType(expr->getType()); return false; } - // Non-const methods need a mutable borrow + // Non-user-defined types (STL) need a mutable borrow for non-const + // methods, as do virtual methods. auto base_type = expr->getBase()->getType().getNonReferenceType(); if (base_type->isPointerType()) { base_type = base_type->getPointeeType(); @@ -2006,14 +1841,18 @@ bool ConverterRefCount::VisitMemberExpr(clang::MemberExpr *expr) { } if (auto *field = clang::dyn_cast(member); - field && !known && IsUserDefinedDecl(field->getParent())) { - ConvertFieldAccess(expr); + field && !known && !IsValueField(ctx_, field) && + !field->getType()->isReferenceType()) { + ConvertInlineField(expr); return false; } std::string str; if (known) { str = GetMappedAsString(expr); + } else if (clang::isa(member)) { + // The Value or the pointer that the field holds. + str = ReadField(expr); } else { Buffer buf(*this); PushExprKind push(*this, ExprKind::RValue); @@ -2047,303 +1886,77 @@ bool ConverterRefCount::VisitMemberExpr(clang::MemberExpr *expr) { return false; } -std::string ConverterRefCount::ConvertRecordPlace(clang::MemberExpr *expr, - bool mut) { +void ConverterRefCount::ConvertInlineField(clang::MemberExpr *expr) { + auto name = GetNamedDeclAsString(expr->getMemberDecl()); + auto type = expr->getType(); auto *base = expr->getBase(); - if (clang::isa(base->IgnoreCasts()) && !ThisIsRustPtr()) { - return std::string(keyword::kSelfValue); - } - if (expr->isArrow()) { - auto *op = clang::dyn_cast(base); - if (mut && (!op || IsUserOperatorCall(op))) { - pending_deref_.set(ConvertRValue(base), isFresh()); - return {}; - } - Buffer buf(*this); - PushExprKind push(*this, mut ? ExprKind::LValue : ExprKind::RValue); - ConvertArrow(base); - return std::move(buf).str(); - } - return mut ? ConvertLValue(base) : ConvertRValue(base); -} - -// Whether expr is (a subobject of) the parameter of a defaulted copy -// constructor, which is translated as the `self` of Clone::clone. -static bool IsCloneSource(const clang::Expr *expr) { - while (true) { - expr = expr->IgnoreParenImpCasts(); - if (auto *member = clang::dyn_cast(expr); - member && !member->isArrow()) { - expr = member->getBase(); - } else if (auto *subscript = - clang::dyn_cast(expr)) { - expr = subscript->getBase(); - } else if (auto *opaque = clang::dyn_cast(expr)) { - expr = opaque->getSourceExpr(); - } else { - break; - } - } - auto *ref = clang::dyn_cast(expr); - auto *param = - ref ? clang::dyn_cast(ref->getDecl()) : nullptr; - auto *ctor = - param - ? clang::dyn_cast(param->getDeclContext()) - : nullptr; - return ctor && ctor->isCopyConstructor() && ctor->isDefaulted() && - param->getName().empty(); -} - -// Whether the object is accessed through a pointer: a reference, an element -// of an array pointed to, or a union member (whose accessor returns a -// pointer). -static bool IsPointee(clang::Expr *object) { - if (auto *subscript = clang::dyn_cast(object)) { - auto *base = subscript->getBase()->IgnoreImpCasts(); - return base->getType()->isPointerType() || - base->getType()->isReferenceType() || - (clang::isa(base) && - clang::cast(base) - ->getDecl() - ->getType() - ->isReferenceType()); - } - if (auto *member = clang::dyn_cast(object)) { - auto *record = clang::dyn_cast( - member->getMemberDecl()->getDeclContext()); - return record && record->isUnion(); - } - return false; -} - -std::optional -ConverterRefCount::FindStructPtr(clang::Expr *place, bool mut) { - // Indexes are evaluated in the closure, unless hoisted before it. - auto unsafe_index = [&](clang::Expr *idx) { - return !hoisted_exprs_.contains(idx) && - (idx->HasSideEffects(ctx_) || (mut && ReadsField(idx))); - }; - // An element of a container that is a field, which is indexed in place. - auto field_subscript = [&](clang::Expr *e) -> clang::CXXOperatorCallExpr * { - auto *op = clang::dyn_cast(e); - if (op && op->getOperator() == clang::OO_Subscript && - IsFieldAccess(op->getArg(0)->IgnoreImpCasts()) && - ((IsBoxedType(ctx_, op->getArg(0)->getType()) && - !IsBoxedType(ctx_, op->getType().getNonReferenceType())) || - IsUniquePtr(op->getArg(0)->getType()))) { - return op; - } - return nullptr; - }; - auto *expr = place->IgnoreParenLValueCasts(); - bool in_value_field = false; - while (true) { - // Elements of arrays and containers that are fields. - if (auto *subscript = clang::dyn_cast(expr)) { - if (!subscript->getBase()->IgnoreImpCasts()->getType()->isArrayType() || - unsafe_index(subscript->getIdx())) { - return std::nullopt; - } - expr = subscript->getBase()->IgnoreParenImpCasts(); - continue; - } - if (auto *op = field_subscript(expr)) { - if (unsafe_index(op->getArg(1))) { - return std::nullopt; + if (isAddrOf()) { + // A pointer to the struct, offset to the field. + std::string ptr; + { + Buffer buf(*this); + PushExprKind push(*this, ExprKind::AddrOf); + if (expr->isArrow()) { + ConvertArrow(base); + } else { + Convert(base); } - expr = op->getArg(0)->IgnoreImpCasts(); - continue; - } - - auto *member = clang::dyn_cast(expr); - if (!member || !IsFieldAccess(member)) { - return std::nullopt; - } - in_value_field |= IsValueField(ctx_, member->getMemberDecl()); - if (field_places_.contains(member)) { - return StructPtr{.member = member, .nested = true}; - } - auto *base = member->getBase(); - auto *object = base->IgnoreParenImpCasts(); - // In methods that are not on Ptr, and in Clone, the struct is a - // reference. Smart pointers own the struct, which is borrowed directly. - if ((clang::isa(object) && !ThisIsRustPtr()) || - IsCloneSource(object) || - (member->isArrow() && clang::isa(object))) { - return std::nullopt; - } - if (member->isArrow()) { - return StructPtr{ - .member = member, .ptr = base, .in_value_field = in_value_field}; - } - if (auto *deref = clang::dyn_cast(object); - deref && deref->getOpcode() == clang::UO_Deref) { - return StructPtr{.member = member, - .ptr = deref->getSubExpr(), - .in_value_field = in_value_field}; - } - if (auto *op = field_subscript(object); - op && !unsafe_index(op->getArg(1))) { - expr = object; - continue; - } - if (IsReferenceType(object) || IsPointee(object)) { - return StructPtr{.member = member, - .ptr = base, - .via_reference = true, - .in_value_field = in_value_field}; - } - expr = object; - } -} - -std::optional ConverterRefCount::ConvertInStructPtr( - clang::Expr *place, bool mut, llvm::function_ref convert) { - auto sp = FindStructPtr(place, mut); - if (!sp || sp->nested) { - return std::nullopt; - } - auto ptr = - sp->via_reference ? ConvertPointer(sp->ptr) : ConvertRValue(sp->ptr); - auto *record = - clang::cast(sp->member->getMemberDecl())->getParent(); - field_places_.emplace( - sp->member, - std::format("__s.{}", GetNamedDeclAsString(sp->member->getMemberDecl()))); - auto body = convert(); - field_places_.erase(sp->member); - // A Value field is written through a Value of its own. - mut &= !sp->in_value_field; - return std::format("{}.{}(|__s: &{}{}| {})", ptr, mut ? "with_mut" : "with", - mut ? "mut " : "", GetRecordName(record), body); -} - -std::string ConverterRefCount::ConvertFieldPointer(clang::MemberExpr *expr) { - auto *base = expr->getBase(); - auto *object = base->IgnoreParenImpCasts(); - std::string base_ptr; - if (expr->isArrow() && !clang::isa(object)) { - base_ptr = ConvertRValue(base); - } else if (auto *ref = clang::dyn_cast(object); - ref && clang::isa(ref->getDecl()) && - !IsGlobalVar(ref) && !IsReferenceType(ref)) { - // The Value of a local variable. - base_ptr = ConvertDeclRef(ref, ref->getDecl()); - } else { - base_ptr = ConvertPointer(base); - } - return std::format("field_ptr!({}, {})", base_ptr, - GetNamedDeclAsString(expr->getMemberDecl())); -} - -void ConverterRefCount::ConvertFieldAccess(clang::MemberExpr *expr) { - auto *member = expr->getMemberDecl(); - auto field_type = member->getType(); - auto name = GetNamedDeclAsString(member); - - if (field_type->isReferenceType()) { - // The field holds a pointer to the referenced object. - auto str = - std::format("{}.{}", ConvertRecordPlace(expr, /*mut=*/false), name); - if (isAddrOf()) { - StrCat(str); - computed_expr_type_ = ComputedExprType::Pointer; - } else if (isLValue()) { - pending_deref_.set(std::move(str), /*fresh=*/false); - } else { - StrCat(DerefPtrExpr(str, field_type.getNonReferenceType())); - SetValueFreshness(expr->getType()); - } - return; - } - - if (IsValueField(ctx_, member)) { - auto sp = FindStructPtr(expr); - if (!sp || sp->nested) { - auto *object = expr->getBase()->IgnoreParenImpCasts(); - bool borrows = - !field_places_.contains(expr) && !IsCloneSource(object) && - !(clang::isa(object) && !ThisIsRustPtr()); - PushBrace block(*this, borrows && isAddrOf()); - ConvertFieldPlace(expr, ConvertValueField(expr)); - return; - } - // The Value is taken out of the struct, which is borrowed only in the - // closure. - auto str = *ConvertInStructPtr(expr, false, [&] { - return ConvertValueField(expr) + - (isAddrOf() ? ".as_pointer()" : ".clone()"); - }); - if (isAddrOf()) { - StrCat(str); - computed_expr_type_ = ComputedExprType::FreshPointer; - } else { - ConvertFieldPlace(expr, std::move(str)); + ptr = std::move(buf).str(); } + StrCat(std::format("field_ptr!({}, {})", ptr, name)); + computed_expr_type_ = ComputedExprType::FreshPointer; return; } - if (isAddrOf()) { - StrCat(ConvertFieldPointer(expr)); - computed_expr_type_ = ComputedExprType::FreshPointer; + // Pointers, and unique_ptr, which holds a Value, are copied out too, as is + // any field that is copied. + if (isRValue() && !record_ptr_ && + (TypeIsCopyable(type) || type->isPointerType() || IsUniquePtr(type) || + expr == copied_expr_)) { + StrCat(ReadField(expr)); + SetFreshType(type); return; } - // Reads of values must not keep the struct borrowed after the read, as the - // rest of the statement may write to it. - bool copyable = TypeIsCopyable(field_type); - if (isRValue() && (copyable || field_type->isPointerType())) { - auto sp = FindStructPtr(expr); - if (sp && !sp->nested) { - StrCat(ConvertFreshRValue(expr)); - } else { - auto read = std::format("{}.{}{}", ConvertRecordPlace(expr, false), name, - copyable ? "" : ".clone()"); - // The temporaries of the tail expression of a block are dropped at - // the end of the block. - StrCat(sp ? read : std::format("{{ {} }}", read)); - } - SetFreshType(expr->getType()); - return; + // The struct, or a struct that holds it, in the closure of a read. + std::string str; + { + Buffer buf(*this); + PushRecordPtr push(*this, record_ptr_, expr->isArrow() ? nullptr : base); + Converter::ConvertMemberExpr(expr); + str = std::move(buf).str(); } - auto place = ConvertRecordPlace(expr, isLValue()); + // A field of a struct reached through a pointer is written through the + // pointer, projected to the field. if (!pending_deref_.empty()) { - // A struct reached through a pointer is written through a pointer to the - // field. - auto ptr = pending_deref_.take(); - pending_deref_.set(std::format("field_ptr!({}, {})", ptr, name), - /*fresh=*/true, expr); + pending_deref_.set( + std::format("field!({}, {})", pending_deref_.take(), name), + /*fresh=*/true); return; } - StrCat(std::format("{}.{}", place, name)); - SetValueFreshness(expr->getType()); -} - -std::string ConverterRefCount::ConvertValueField(clang::MemberExpr *expr) { - if (auto it = field_places_.find(expr); it != field_places_.end()) { - return it->second; - } - return std::format("{}.{}", ConvertRecordPlace(expr, /*mut=*/false), - GetNamedDeclAsString(expr->getMemberDecl())); + StrCat(str); + SetValueFreshness(type); } -void ConverterRefCount::ConvertFieldPlace(clang::MemberExpr *expr, - std::string place) { - if (!IsValueField(ctx_, expr->getMemberDecl())) { - StrCat(place); - SetValueFreshness(expr->getType()); - return; +std::string ConverterRefCount::ReadField(clang::MemberExpr *expr) { + // Unless the struct is reached through a pointer, in which case the field + // is read in a closure, the borrow of the struct ends at the end of the + // block. + std::string ptr; + std::string str; + { + Buffer buf(*this); + PushRecordPtr push(*this, &ptr, + expr->isArrow() ? nullptr : expr->getBase()); + PushExprKind push_kind(*this, ExprKind::RValue); + Converter::ConvertMemberExpr(expr); + str = std::move(buf).str(); } - // Like a local variable. - if (isAddrOf()) { - StrCat(place, ".as_pointer()"); - computed_expr_type_ = ComputedExprType::FreshPointer; - return; + if (!TypeIsCopyable(expr->getMemberDecl()->getType())) { + str += ".clone()"; } - StrCat( - std::format("(*{}.{}())", place, isRValue() ? "borrow" : "borrow_mut")); - SetValueFreshness(expr->getType()); + return ptr.empty() ? std::format("{{ {} }}", str) + : std::format("{}.with(|__s| {})", ptr, str); } bool ConverterRefCount::VisitCXXNewExpr(clang::CXXNewExpr *expr) { @@ -2539,14 +2152,6 @@ bool ConverterRefCount::VisitCXXConstructExpr(clang::CXXConstructExpr *expr) { } auto *ctor = expr->getConstructor(); - if (ctor->isCopyConstructor() && IsCloneSource(expr->getArg(0))) { - // In Clone::clone, the source object is only available as a reference. - // Copy its fields with their own clone(). - StrCat(std::format("{}.clone()", ConvertRValue(expr->getArg(0)))); - computed_expr_type_ = ComputedExprType::FreshValue; - return false; - } - if (IsRValueConvertingConstructor(ctor) || (ctor->isMoveConstructor() && !IsUserDefinedDecl(ctor->getParent()))) { StrCat(ConvertLValue(expr->getArg(0))); @@ -2709,7 +2314,7 @@ ConverterRefCount::GetStructAttributes(const clang::RecordDecl *decl) { attrs.emplace_back("Clone"); } - // Gives access to the fields of the struct through pointers to them. + // Gives access to the fields through pointers to them. attrs.emplace_back("Record"); if (RecordDerivesByteRepr(decl)) { @@ -2794,19 +2399,19 @@ std::string ConverterRefCount::ConvertFnPtrValue(clang::QualType qual_type, return std::format("{}.cast::<{}>()", base, fn_type); } +void ConverterRefCount::ConvertFieldInit(const clang::FieldDecl *field, + clang::Expr *init) { + PushConversionKind push(*this, IsValueField(ctx_, field) + ? ConversionKind::FullRefCount + : ConversionKind::Pointee); + Converter::ConvertFieldInit(field, init); +} + void ConverterRefCount::ConvertVarInit(clang::QualType qual_type, clang::Expr *expr) { - // The fields of a struct literal are initialized in a single statement. - // An initializer must not keep a struct borrowed, as another one may write - // to it. - bool is_field = getConversionKind() == ConversionKind::Pointee; bool is_ref = qual_type->isReferenceType(); PushConversionKind push(*this, ConversionKind::Unboxed, is_ref); - auto value = BoxValue(ConvertVarInitValue(qual_type, expr)); - if (is_field && ReadsField(expr) && !Trim(value).starts_with('{')) { - value = std::format("{{ {} }}", value); - } - StrCat(value); + StrCat(BoxValue(ConvertVarInitValue(qual_type, expr))); } bool ConverterRefCount::EmitGlobalValueAssign(clang::Expr *lhs, @@ -2831,87 +2436,32 @@ void ConverterRefCount::EmitSetOrAssign(clang::Expr *lhs, computed_expr_type_ = ComputedExprType::FreshValue; return; } - if (auto str = ConvertInStructPtr(lhs, true, [&] { - return std::format("{} = {}", ConvertLValue(lhs), rhs); - })) { - StrCat(*str); - computed_expr_type_ = ComputedExprType::FreshValue; - return; - } auto lhs_str = ConvertLValue(lhs); if (!pending_deref_.empty()) { - StrCat(TakePendingDerefToWrite(lhs), ".write(", rhs, ')'); + auto ptr = pending_deref_.take(); + StrCat(ptr, ".write(", rhs, ')'); } else { StrCat(lhs_str, token::kAssign, rhs); } computed_expr_type_ = ComputedExprType::FreshValue; } -// The index of the subscript expression that the place expr is in, if any. -static clang::Expr *GetSubscriptIndex(clang::Expr *expr) { - expr = expr->IgnoreParenImpCasts(); - while (auto *member = clang::dyn_cast(expr)) { - if (member->isArrow()) { - return nullptr; - } - expr = member->getBase()->IgnoreParenImpCasts(); - } - if (auto *subscript = clang::dyn_cast(expr)) { - return subscript->getIdx(); - } - if (auto *op = clang::dyn_cast(expr); - op && op->getOperator() == clang::OO_Subscript) { - return op->getArg(1); - } - return nullptr; -} - void ConverterRefCount::ConvertAssignment(clang::Expr *lhs, clang::Expr *rhs, std::string_view assign_operator) { auto rhs_as_string = ConvertFreshRValue(rhs, lhs->getType()); PushBrace brace(*this, isRValue()); - // An index that has side effects or reads a field may conflict with the - // borrow of the struct that holds the subscripted array. Evaluate it before - // borrowing the struct. - auto *idx = GetSubscriptIndex(lhs); - if (idx && !idx->HasSideEffects(ctx_) && !ReadsField(idx)) { - idx = nullptr; - } - - // A field is assigned by borrowing its struct mutably, which conflicts - // with any struct borrowed by the right-hand side. A field of a struct - // reached through a pointer is assigned in a closure, where the right-hand - // side must not access memory. - if (idx || MayCauseBorrowMutError(lhs, rhs) || - (ReadsField(lhs) && ReadsField(rhs)) || - (FindStructPtr(lhs, true) && !IsTrivialValue(rhs))) { + if (MayCauseBorrowMutError(lhs, rhs)) { StrCat(keyword::kLet, "__rhs", token::kAssign, rhs_as_string, token::kSemiColon); rhs_as_string = "__rhs"; } - if (idx) { - StrCat(keyword::kLet, "__idx", token::kAssign, ConvertRValue(idx), - token::kSemiColon); - hoisted_exprs_.emplace(idx, "__idx"); - } - struct EraseHoisted { - ConverterRefCount &c; - clang::Expr *idx; - ~EraseHoisted() { c.hoisted_exprs_.erase(idx); } - } erase_hoisted{*this, idx}; if (assign_operator == "=") { EmitSetOrAssign(lhs, rhs_as_string); } else if (EmitGlobalValueAssign(lhs, assign_operator, rhs_as_string)) { computed_expr_type_ = ComputedExprType::FreshValue; - } else if (auto str = ConvertInStructPtr(lhs, true, [&] { - return std::format("{} {} {}", ConvertLValue(lhs), - assign_operator, rhs_as_string); - })) { - StrCat(*str); - computed_expr_type_ = ComputedExprType::FreshValue; } else { auto lhs_str = ConvertLValue(lhs); if (!pending_deref_.empty()) { @@ -2987,6 +2537,7 @@ void ConverterRefCount::ConvertUniquePtrDeref( StrCat(ConvertRValue(expr->getArg(0)), ".as_pointer()"); computed_expr_type_ = ComputedExprType::FreshPointer; } else { + // The object is modified through its own Value. StrCat(std::format("(*{}.as_ref().unwrap().borrow{}())", ConvertRValue(expr->getArg(0)), isRValue() ? "" : "_mut")); @@ -3035,11 +2586,6 @@ bool ConverterRefCount::ConvertCXXOperatorCallExpr( break; case clang::OverloadedOperatorKind::OO_Subscript: { - if (ConvertWithHoistedIndex(expr, expr->getArg(0), expr->getArg(1), - [&] { ConvertCXXOperatorCallExpr(expr); })) { - break; - } - if (IsUniquePtr(expr->getArg(0)->getType())) { StrCat( std::format("{}.as_ref().unwrap()", ConvertRValue(expr->getArg(0)))); @@ -3062,29 +2608,6 @@ bool ConverterRefCount::ConvertCXXOperatorCallExpr( IsBoxedType(ctx_, expr->getType().getNonReferenceType()) && IsBoxedType(ctx_, expr->getArg(0)->getType().getNonReferenceType()); - // A container that is a field is indexed in place, instead of through a - // pointer to the field. The index must not borrow the struct mutably, nor - // at all if the container is borrowed mutably. - if (auto *container = expr->getArg(0)->IgnoreImpCasts(), - *idx = expr->getArg(1); - !isAddrOf() && !is_inner_boxed && IsFieldAccess(container) && - IsBoxedType(ctx_, container->getType()) && - (hoisted_exprs_.contains(idx) || - (!idx->HasSideEffects(ctx_) && (!isLValue() || !ReadsField(idx))))) { - auto place = - isLValue() ? ConvertLValue(container) : ConvertRValue(container); - auto str = - std::format("{}[({}) as usize]", place, ConvertSubscriptIndex(idx)); - if (isRValue() && TypeIsCopyable(expr->getType())) { - StrCat(std::format("{{ {} }}", str)); - SetFreshType(expr->getType()); - } else { - StrCat(str); - SetValueFreshness(expr->getType()); - } - break; - } - if (isLValue()) { PushConversionKind push_ck(*this, ConversionKind::Unboxed); pending_deref_.set( @@ -3306,6 +2829,11 @@ void ConverterRefCount::ConvertDeref(clang::Expr *expr) { return; } + if (record_ptr_ && pointee_type->isRecordType() && isRValue()) { + StrCat(DerefPtrExpr(ToString(expr), pointee_type)); + return; + } + std::string str; { Buffer buf(*this); @@ -3340,15 +2868,20 @@ void ConverterRefCount::ConvertArrow(clang::Expr *expr) { op && op->getOperator() == clang::OverloadedOperatorKind::OO_Arrow; if (!is_overloaded_arrow || IsUserOperatorCall(op)) { - auto ptr = ToString(expr); auto pointee_type = expr->getType()->getPointeeType(); // Virtual methods, which take &mut self, are called through PtrDyn. if (auto *record = pointee_type->getAsRecordDecl(); isLValue() && record && abstract_structs_.contains(GetID(record))) { - StrCat(std::format("(*{}.upgrade().deref_mut())", ptr)); - } else { - StrCat(DerefPtrExpr(ptr, pointee_type)); + StrCat(std::format("(*{}.upgrade().deref_mut())", ToString(expr))); + SetValueFreshness(pointee_type); + return; + } + if (isLValue() || isAddrOf()) { + ConvertDeref(expr); + return; } + auto ptr = ToString(expr); + StrCat(DerefPtrExpr(ptr, pointee_type)); SetValueFreshness(pointee_type); return; } @@ -3405,6 +2938,10 @@ ConverterRefCount::GetPointerDerefPrefix(clang::QualType pointee_type) { std::string ConverterRefCount::DerefPtrExpr(std::string_view ptr_expr, clang::QualType pointee_type) { + if (record_ptr_ && pointee_type->isRecordType()) { + *std::exchange(record_ptr_, nullptr) = ptr_expr; + return "__s"; + } return std::format("({}{}{})", GetPointerDerefPrefix(pointee_type), ptr_expr, GetPointerDerefSuffix(pointee_type)); } @@ -3420,181 +2957,10 @@ bool ConverterRefCount::IsReferenceType(const clang::Expr *expr) const { return false; } -// Collects the ways placeholder `n` is accessed in `fragments`. -static void CollectPlaceholderAccesses( - const std::vector &fragments, unsigned n, - std::vector &accesses) { - for (const auto &frag : fragments) { - if (auto *ph = std::get_if(&frag)) { - if (ph->n == n) { - accesses.push_back(ph->access); - } - } else if (auto *mc = std::get_if< - std::unique_ptr>( - &frag)) { - CollectPlaceholderAccesses((*mc)->receiver, n, accesses); - CollectPlaceholderAccesses((*mc)->body, n, accesses); - } - } -} - -// Whether argument `n` of a mapped call can be evaluated into a local -// variable ahead of the call: the rule moves it, or only reads it and it can -// be copied. -static bool -CanHoistArg(clang::ASTContext &ctx, - const std::vector &fragments, - const clang::Expr *callee, unsigned n, const clang::Expr *arg, - bool copyable) { - using TranslationRule::Access; - std::vector accesses; - CollectPlaceholderAccesses(fragments, n, accesses); - if (accesses.empty()) { - return false; - } - // Taking a temporary moves it. - if (clang::isa(arg) && - std::ranges::all_of(accesses, [](auto a) { - return a == Access::kMove || a == Access::kTake; - })) { - return true; - } - if (RuleRegistry::ParamIsPointer(ctx, callee, n) || - Mapper::GetParamType(ctx, callee, n).starts_with('&')) { - return false; - } - if (std::ranges::all_of(accesses, - [](auto a) { return a == Access::kMove; })) { - return true; - } - return copyable && std::ranges::all_of(accesses, [](auto a) { - return a == Access::kMove || a == Access::kBorrow; - }); -} - -std::optional ConverterRefCount::ConvertOnFieldReceiver( - const std::vector &fragments, - const TranslationRule::PlaceholderFragment &receiver_ph, clang::Expr *expr, - clang::Expr **args, unsigned num_args, - llvm::function_ref convert) { - auto all_args = BuildUnifiedArgs(expr, args, num_args); - auto *receiver = all_args[receiver_ph.n]; - // A method called on a field of a struct reached through a pointer is - // called in a closure, unless the rule takes a pointer to the field. - auto sp = receiver_ph.access != TranslationRule::Access::kMove && - !receiver->getType()->isPointerType() && - !RuleRegistry::ParamIsPointer(ctx_, GetCalleeOrExpr(expr), - receiver_ph.n) - ? FindStructPtr(receiver, receiver_ph.access != - TranslationRule::Access::kBorrow) - : std::nullopt; - bool in_closure = sp && !sp->nested; - - // Fields share the borrow of their struct. Evaluate the arguments that - // read fields before borrowing the receiver, if it is a field as well. In - // a closure, evaluate those that access any memory. - if (ReadsField(receiver)) { - std::string lets; - for (unsigned i = 0; i < all_args.size(); ++i) { - auto *arg = all_args[i]; - // A pointer that is computed, not read from a variable, is a fresh - // value, which the closure can take. - bool fresh_ptr = - in_closure && arg->isPRValue() && arg->getType()->isPointerType(); - if (arg == receiver || - (in_closure ? IsTrivialValue(arg) : !ReadsField(arg)) || - hoisted_exprs_.contains(arg) || - (!fresh_ptr && !CanHoistArg(ctx_, fragments, GetCalleeOrExpr(expr), i, - arg, TypeIsCopyable(arg->getType())))) { - continue; - } - auto name = std::format("__a{}", i); - lets += std::format("let {} = {};", name, ConvertFreshRValue(arg)); - hoisted_exprs_.emplace(arg, std::move(name)); - } - if (!lets.empty()) { - auto call = convert(); - for (auto *arg : all_args) { - hoisted_exprs_.erase(arg); - } - return std::format("{{ {} {} }}", lets, call); - } - } - - if (in_closure) { - return ConvertInStructPtr( - receiver, receiver_ph.access != TranslationRule::Access::kBorrow, - convert); - } - return std::nullopt; -} - -std::string ConverterRefCount::ConvertIRFragment( - const std::vector &fragments, - clang::Expr *expr, clang::Expr **args, unsigned num_args, - TempMaterializationCtx *ctx) { - // A rule that calls methods on the result of a method called on a field, - // e.g., `a0.get(a1).unwrap()`, is converted in the closure as a whole, as - // the result may borrow the field. - using TranslationRule::MethodCallFragment; - const TranslationRule::PlaceholderFragment *receiver_ph = nullptr; - bool chained = fragments.size() > 1; - for (const auto *frags = &fragments; frags->size() >= 1;) { - auto *mc = - std::get_if>(&frags->front()); - if (!mc) { - break; - } - if ((receiver_ph = (*mc)->getReceiverPlaceholder())) { - break; - } - frags = &(*mc)->receiver; - chained = true; - } - // So is a rule that writes a field reached through a pointer, e.g., - // `a0 = a1.to_owned_opt()`. - if (!receiver_ph) { - auto all_args = BuildUnifiedArgs(expr, args, num_args); - for (const auto &frag : fragments) { - auto *ph = std::get_if(&frag); - if (ph && ph->access == TranslationRule::Access::kBorrowMut) { - if (auto sp = FindStructPtr(all_args[ph->n], /*mut=*/true); - sp && !sp->nested) { - receiver_ph = ph; - chained = true; - } - break; - } - } - } - if (receiver_ph && chained) { - if (auto str = ConvertOnFieldReceiver( - fragments, *receiver_ph, expr, args, num_args, [&] { - auto str = - ConvertIRFragment(fragments, expr, args, num_args, ctx); - // The rule may have statements. - return Trim(str).contains(';') ? std::format("{{ {} }}", str) - : str; - })) { - return *str; - } - } - return Converter::ConvertIRFragment(fragments, expr, args, num_args, ctx); -} - 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) { - if (auto str = ConvertOnFieldReceiver( - mc.body, *receiver_ph, expr, args, num_args, [&] { - return ConvertMappedMethodCall(expr, mc, args, num_args, ctx); - })) { - return *str; - } - } - if (!receiver_ph || receiver_ph->access == TranslationRule::Access::kBorrow || receiver_ph->access == TranslationRule::Access::kMove) { return Converter::ConvertMappedMethodCall(expr, mc, args, num_args, ctx); @@ -3607,10 +2973,6 @@ std::string ConverterRefCount::ConvertMappedMethodCall( arg = call->getArg(0); } - if (!arg->getType()->isPointerType() && !IsReferenceType(arg)) { - return Converter::ConvertMappedMethodCall(expr, mc, args, num_args, ctx); - } - auto param_type = Mapper::GetParamType(ctx_, GetCalleeOrExpr(expr), arg_idx); if (arg->getType()->isPointerType()) { @@ -3619,11 +2981,14 @@ std::string ConverterRefCount::ConvertMappedMethodCall( ConvertIRFragment(mc.body, expr, args, num_args, ctx)); } - ConvertIRFragment(mc.receiver, expr, args, num_args, ctx); - assert(!pending_deref_.empty()); + // A receiver reached through a pointer is modified through it. + auto receiver = ConvertIRFragment(mc.receiver, expr, args, num_args, ctx); + if (pending_deref_.empty()) { + return receiver + ConvertIRFragment(mc.body, expr, args, num_args, ctx); + } bool is_boxed = pending_deref_.is_boxed(); - auto ptr = TakePendingDerefToWrite(arg); + auto ptr = pending_deref_.take(); auto body = ConvertIRFragment(mc.body, expr, args, num_args, ctx); SetFreshType(expr->getType()); @@ -3709,9 +3074,8 @@ void ConverterRefCount::SetUFCSReceiver(clang::Expr *base, bool is_arrow, token::kRef + BoxValue(ConvertRValue(base)) + ".as_pointer()"; return; } - auto ptr = base_is_pointer ? ConvertRValue(base) : ConvertPointer(base); - // The method may write to the struct that holds the receiver. - ufcs_receiver_ = token::kRef + DetachPtr(std::move(ptr), isFresh(), base); + ufcs_receiver_ = token::kRef + (base_is_pointer ? ConvertRValue(base) + : ConvertPointer(base)); } std::string @@ -3840,15 +3204,13 @@ ConverterRefCount::DestroyMembers(const clang::CXXRecordDecl *decl) { auto *elem = field->getType()->getBaseElementTypeUnsafe()->getAsCXXRecordDecl(); assert(elem); - // The array is a Value of its own. out += std::format( - "{{ let __p: Ptr<{3}> = self.with(|__s| __s.{0}.as_pointer()); for " - "__i in 0..__p.len() {{ {2}::{1}(&__p.offset(__i as isize)); }} }}\n", - name, kDestructorName, TraitName(elem), GetRecordName(elem)); + "{{ let __p = (*self.upgrade().deref()).{0}.as_pointer(); for __i in " + "0..__p.len() {{ {2}::{1}(&__p.offset(__i as isize)); }} }}\n", + name, kDestructorName, TraitName(elem)); } else { - out += std::format("{2}::{1}(&field_ptr!(self, {0}));\n", name, - kDestructorName, - TraitName(field->getType()->getAsCXXRecordDecl())); + out += std::format("field_ptr!(self, {0}).{1}();\n", name, + kDestructorName); } } return out; @@ -3866,7 +3228,6 @@ void ConverterRefCount::ConvertCXXConstructorBody( } else { StrCat("Self"); PushBrace this_init(*this); - PushConversionKind push(*this, ConversionKind::Pointee); EmitConstructorFieldInits(decl); } StrCat("))", token::kSemiColon); diff --git a/cpp2rust/converter/models/converter_refcount.h b/cpp2rust/converter/models/converter_refcount.h index 526c903fea..9013d21a42 100644 --- a/cpp2rust/converter/models/converter_refcount.h +++ b/cpp2rust/converter/models/converter_refcount.h @@ -51,10 +51,6 @@ class ConverterRefCount final : public Converter { bool RecordImplementsClone(const clang::RecordDecl *decl) override; - std::string GetDeepCopy(clang::QualType type, std::string_view src); - - // Whether the Clone of a struct copies each field with its own clone(), and - // hence can be derived. bool RecordDerivesClone(const clang::RecordDecl *decl); void AddByteReprTrait(const clang::RecordDecl *decl) override; @@ -149,71 +145,42 @@ class ConverterRefCount final : public Converter { bool VisitArraySubscriptExpr(clang::ArraySubscriptExpr *expr) override; - // Converts the subscript expr with convert, having evaluated its index - // first, if the index has side effects that may conflict with the borrow of - // the struct that holds base. Returns false if it doesn't do so. - bool ConvertWithHoistedIndex(clang::Expr *expr, clang::Expr *base, - clang::Expr *idx, - llvm::function_ref convert); - bool VisitMemberExpr(clang::MemberExpr *expr) override; void ConvertUnionMemberAccessor(clang::MemberExpr *expr); - // Converts an access to a field of a struct translated by us. Fields are - // stored inline in the struct. - void ConvertFieldAccess(clang::MemberExpr *expr); - - // The struct whose field `expr` accesses, as a place expression. The struct - // is borrowed mutably if `mut` is set, unless it is reached through a - // pointer, which is then left in pending_deref_. - std::string ConvertRecordPlace(clang::MemberExpr *expr, bool mut); - - // Wraps ptr, the result of converting expr, such that it doesn't keep any - // struct borrowed if it is written through. - std::string DetachPtr(std::string ptr, bool fresh, const clang::Expr *expr); - std::string TakePendingDerefToWrite(const clang::Expr *lvalue); - - // A Ptr to the field accessed by `expr`. - std::string ConvertFieldPointer(clang::MemberExpr *expr); - - // The Value held by a Value field (see IsValueField). - std::string ConvertValueField(clang::MemberExpr *expr); - - // Converts the field `expr` given the place where it is stored. - void ConvertFieldPlace(clang::MemberExpr *expr, std::string place); - - // A field of a struct reached through the pointer `ptr`, which a place - // expression accesses (e.g., p->x, (*p).x, or r.a[i].x for a reference r). - // `nested` if the place is converted inside ConvertInStructPtr already. - struct StructPtr { - clang::MemberExpr *member; - clang::Expr *ptr = nullptr; - bool via_reference = false; - bool nested = false; - // Whether the place is in a Value field, which is borrowed separately - // from the struct. - bool in_value_field = false; - }; - // If `mut`, the place is written, and must not borrow the struct. - std::optional FindStructPtr(clang::Expr *place, bool mut = false); - - // If `place` is a field of a struct reached through a pointer, converts - // the expression given by `convert`, which accesses `place`, in a closure - // that borrows the struct through the pointer, `p.with(|__s: &S| ...)`. - std::optional - ConvertInStructPtr(clang::Expr *place, bool mut, - llvm::function_ref convert); - - std::string ConvertTake(clang::Expr *arg) override { - auto take = [&] { return Converter::ConvertTake(arg); }; - auto str = ConvertInStructPtr(arg, true, take); - return str ? *str : take(); - } + // Converts an access to a field that is stored inline in its struct. + void ConvertInlineField(clang::MemberExpr *expr); - // The fields converted by ConvertInStructPtr, and their places in the - // closure. - std::unordered_map field_places_; + // Copies the value of the field `expr` out of its struct, such that the + // struct doesn't stay borrowed. + std::string ReadField(clang::MemberExpr *expr); + + // A field that is read through a pointer to its struct is read in a + // closure, `p.with(|__s| __s.x)`. While converting the struct whose field + // is read, record_base_, or `*p` for `p->x`, the dereference of a pointer + // to it is emitted as `__s`, and the pointer is stored in *record_ptr_. + std::string *record_ptr_ = nullptr; + const clang::Expr *record_base_ = nullptr; + + // The expression that ConvertFreshRValue copies. + const clang::Expr *copied_expr_ = nullptr; + + struct PushRecordPtr { + ConverterRefCount &c; + std::string *record_ptr; + const clang::Expr *record_base; + + PushRecordPtr(ConverterRefCount &c, std::string *ptr, + const clang::Expr *base) + : c(c), record_ptr(std::exchange(c.record_ptr_, ptr)), + record_base(std::exchange( + c.record_base_, base ? base->IgnoreParenImpCasts() : nullptr)) {} + ~PushRecordPtr() { + c.record_ptr_ = record_ptr; + c.record_base_ = record_base; + } + }; bool VisitCXXNewExpr(clang::CXXNewExpr *expr) override; @@ -262,26 +229,12 @@ class ConverterRefCount final : public Converter { bool Convert(clang::Expr *expr, std::optional implicit_convert_to = {}) override { - if (auto it = hoisted_exprs_.find(expr); it != hoisted_exprs_.end()) { - StrCat(it->second); - computed_expr_type_ = ComputedExprType::FreshValue; - return false; - } - if (auto it = field_places_.find(expr); it != field_places_.end()) { - ConvertFieldPlace(clang::cast(expr), it->second); - return false; - } - // Values read from fields of structs reached through pointers. - if (isRValue() && TypeIsCopyable(expr->getType())) { - if (auto str = ConvertInStructPtr(expr, false, [&] { - return ToString(expr, implicit_convert_to); - })) { - StrCat(*str); - SetFreshType(expr->getType()); - return false; - } + auto *record_ptr = record_ptr_; + if (!expr || expr->IgnoreParenImpCasts() != record_base_) { + record_ptr_ = nullptr; } auto result = Converter::Convert(expr, implicit_convert_to); + record_ptr_ = record_ptr; if (computed_expr_type_ == ComputedExprType::Pending) { assert(!pending_deref_.empty() && "pending_deref_ taken without type"); } @@ -331,21 +284,6 @@ class ConverterRefCount final : public Converter { bool IsReferenceType(const clang::Expr *expr) const override; - // Converts a rule that calls a method on `receiver_ph`, by `convert`, in - // a closure if the receiver is a field of a struct reached through a - // pointer, or after evaluating the arguments that read fields if it is a - // field. - std::optional ConvertOnFieldReceiver( - const std::vector &fragments, - const TranslationRule::PlaceholderFragment &receiver_ph, - clang::Expr *expr, clang::Expr **args, unsigned num_args, - llvm::function_ref convert); - - std::string - ConvertIRFragment(const std::vector &fragments, - clang::Expr *expr, clang::Expr **args, unsigned num_args, - TempMaterializationCtx *ctx) override; - std::string ConvertMappedMethodCall(clang::Expr *expr, const TranslationRule::MethodCallFragment &mc, diff --git a/docs/src/codegen/types/boxing.md b/docs/src/codegen/types/boxing.md index 38e1629e27..9f055eaef1 100644 --- a/docs/src/codegen/types/boxing.md +++ b/docs/src/codegen/types/boxing.md @@ -14,7 +14,7 @@ that owns the type pushes one before printing it: result in `Value<...>`. - `Pointee`: pushed by field declarations; the bare type is printed. Fields that are arrays, or whose type maps to a `Vec` or a `Box` (`std::vector`, - `std::string`), push `FullRefCount` instead (see below). + `std::string`, `std::array`), push `FullRefCount` instead (see below). - `Unboxed`: pushed by parameter lists, return types, and record names; the bare type is printed. - `Ptr`: pushed by a pointer type for its pointee; also printed bare. diff --git a/docs/src/codegen/types/pointers.md b/docs/src/codegen/types/pointers.md index 9bb12fb518..7f0fdcba3e 100644 --- a/docs/src/codegen/types/pointers.md +++ b/docs/src/codegen/types/pointers.md @@ -85,20 +85,16 @@ write copies it in. Which form is emitted follows the of the dereference: - An rvalue use copies the value out. A scalar or pointer pointee is `p.read()`, - and so is a whole record, `*p`. A field of a record pointee is read in a + and so is a whole record, `*p`. A field of a record pointee is copied out in a closure that borrows the record for its duration: `p->x` is - `p.with(|__s: &S| __s.x)`, and `p->a[i].y` is `p.with(|__s: &S| __s.a[i].y)`. - A method called on a field runs in the closure too, together with the rest of - its rule: `p->v.size()` is `p.with(|__s: &S| (*__s.v.borrow()).len())`. -- A write to a field of a record pointee is also done in a closure, which - borrows the record mutably: `p->x = v` is - `p.with_mut(|__s: &mut S| __s.x = v)`. Operands that may access memory are - evaluated before the closure (`let __rhs = ...;`), so that they don't find the - record borrowed. Fields that are [`Value`s of their own](boxing.md) only need - the record borrowed immutably, - `p.with(|__s: &S| (*__s.v.borrow_mut()).push(1))`. The same closures are used - for references to records, `r.x`, and for elements of arrays of records, - `a[i].x`. + `p.with(|__s| __s.x)`, and `p->a.b` is `p.with(|__s| __s.a.b)`; `ReadField` + converts the record with `record_ptr_` set, so that its dereference is + emitted as `__s`. A field that is a [`Value` of its own](boxing.md), or a + `std::unique_ptr`, is copied out as well, i.e., its `Rc`: `p->v.size()` is + `(*p.with(|__s| __s.v.clone()).borrow()).len()`. When the record is not + reached through a pointer, the copy is in a block, `{ (*s.borrow()).x }`. In + all cases the record doesn't stay borrowed for the rest of the statement, + which may write to it. - An address-of use prints `p` itself. - An lvalue use prints nothing at once. The converter records the pointer expression as a [pending dereference](../expressions/pending-deref.md), and @@ -106,7 +102,11 @@ of the dereference: `p.write(v)`, or [`with_mut`](../../rules/rewriting.md) for a mutating method on a boxed pointee. This is what lets `*p = v` come out as a single `write` instead of a borrow followed by an assignment, and `*p += v` as - `{ let _ptr = p.clone(); _ptr.write(_ptr.read() + v) }`. + `{ let _ptr = p.clone(); _ptr.write(_ptr.read() + v) }`. A field of a record + pointee is a pending dereference too, of `field!(p, x)`, which projects the + pointer to the field (see + [Pointers to fields](../../runtime/rc.md#pointers-to-fields)): `p->x = v` is + `field!(p, x).write(v)`, and `p->a.b = v` is `field!(field!(p, a), b).write(v)`. `with` and `with_mut` work on any pointer, including a [reinterpreted](../../runtime/reinterpret.md) one, whose pointee is decoded from diff --git a/docs/src/runtime/libc-shims.md b/docs/src/runtime/libc-shims.md index 885c802bbb..9d3e15d0c1 100644 --- a/docs/src/runtime/libc-shims.md +++ b/docs/src/runtime/libc-shims.md @@ -1,22 +1,25 @@ # libc Shims -In the refcount model every struct member is wrapped in a `Value`, so a libc -struct cannot be used directly. Even without that wrapping the layouts would not -meet: libc structs hold raw pointers, which are incompatible with the refcounted -pointers the model uses. The shim modules therefore define Rust counterparts for -the libc types translated programs use. A shim struct mirrors its C struct -member by member, with each field a `Value`, and converts to or from the -underlying libc or nix type at the call boundary. +libc structs hold raw pointers, which are incompatible with the refcounted +pointers the refcount model uses, so a libc struct cannot be used directly. The +shim modules therefore define Rust counterparts for the libc types translated +programs use. A shim struct mirrors its C struct member by member, like a +translated struct: fields are stored inline, except for arrays, which are +`Value>`s of their own (see [Boxing](../codegen/types/boxing.md)). A +shim converts to or from the underlying libc or nix type at the call boundary. `Stat` is a typical shim: ```rust -#[derive(Default)] +#[derive(Clone, Default, Record)] pub struct Stat { - pub st_dev: Value, - pub st_ino: Value, + #[offset(offset_of!(::libc::stat, st_dev))] + pub st_dev: u64, + #[offset(offset_of!(::libc::stat, st_ino))] + pub st_ino: u64, // ... - pub st_size: Value, + #[offset(offset_of!(::libc::stat, st_size))] + pub st_size: i64, } impl Stat { @@ -28,6 +31,14 @@ A `stat` call in the source program becomes a `nix::sys::stat::stat` call. On success nix returns a raw `libc::stat`, so the result goes through `Stat::from_libc` before it is written into the translated struct. +Like translated structs, shims derive `Record`, so that pointers to their fields +can be taken (see [Pointers to fields](./rc.md#pointers-to-fields)). The offsets +of the fields are those of the libc struct, given by `offset_of!`, and their +`ByteRepr` gives the size of the libc struct, which locates the fields of the +elements of an array of shims, like an array of `pollfd`. The `sockaddr` family, +whose byte representation has a layout of its own, uses the offsets of that +layout. + ## The modules Each shim lives next to the rules that use it, as `rules//shim.rs`. The diff --git a/docs/src/runtime/rc.md b/docs/src/runtime/rc.md index b89afcb05d..1949c2765b 100644 --- a/docs/src/runtime/rc.md +++ b/docs/src/runtime/rc.md @@ -94,8 +94,9 @@ let y: Value> = Rc::new(RefCell::new(field_ptr!(p, y))); `field_ptr!(p, y)` works on a `Value` or a `Ptr` to a struct. The offsets are those of the C layout of the struct, which the code generator gets from Clang -and writes on the fields as `#[offset(N)]` attributes; `#[derive(Record)]` turns -them into the implementation of the `Record` trait: +and writes on the fields as `#[offset(N)]` attributes (any constant expression +works, e.g., `offset_of!` in the [libc shims](./libc-shims.md)); +`#[derive(Record)]` turns them into the implementation of the `Record` trait: ```rust #[derive(Clone, Record, Default)] @@ -127,8 +128,12 @@ the array or vector as their root. A field pointer hence always points to a single object, which is why it needs no element index. Reading a field through a pointer borrows the struct only for the duration of a -closure, e.g., `p.with(|s: &point| s.y)`, so that the borrow ends before the -rest of the statement runs; `p.with_mut(|s: &mut point| s.y = 3)` writes it. +closure, e.g., `p.with(|s| s.y)`, so that the borrow ends before the rest of the +statement runs. `field!(p, y)` is the place of the field, which is read and +written through `p`, projected to the field by a closure, without looking the +field up: `field!(p, y).write(3)`. It holds a reference to `p`, and takes no +more room than a pointer. `field_ptr!` is only used where an actual pointer to +the field is needed. A field of a reinterpreted struct is itself a reinterpreted pointer, to the bytes of the field. Conversely, reinterpreting a field pointer views the bytes @@ -258,9 +263,8 @@ does: in `p.upgrade().deref().field`, the temporary `StrongPtr` lives until the end of the enclosing statement, and so does the borrow. The code generator prefers the `with` and `with_mut` closures, and upgrades only where the result of an access must borrow the pointee beyond a closure. There is no `deref_mut`; -writes go through `write` and `with_mut`. A field of a struct reached through a -pointer, when not written in a `with_mut` closure, is written through a pointer -to the field: `field_ptr!(p, x).write(v)`. +writes go through `write` and `with_mut`, including those to a field of a struct +reached through a pointer: `field!(p, x).write(v)`. For the `Reinterpreted` variant there is no value to reference, only bytes in another allocation. `deref` reads those bytes into a local cell and hands out a diff --git a/libcc2rs-macros/src/lib.rs b/libcc2rs-macros/src/lib.rs index 8352466175..3f5be5b99c 100644 --- a/libcc2rs-macros/src/lib.rs +++ b/libcc2rs-macros/src/lib.rs @@ -114,7 +114,8 @@ pub fn derive_fn_ptr_arg(input: TokenStream) -> TokenStream { // // Implements libcc2rs::Record for S, which lets pointers to the fields of S // be created by field_ptr!. The argument of the offset attribute is the byte -// offset of the field in the C layout of S. +// offset of the field in the C layout of S, as a constant expression (e.g., +// `offset_of!(libc::stat, st_size)`). #[proc_macro_derive(Record, attributes(offset))] pub fn derive_record(input: TokenStream) -> TokenStream { diff --git a/libcc2rs-macros/src/record.rs b/libcc2rs-macros/src/record.rs index e1418db7f2..6ad808969e 100644 --- a/libcc2rs-macros/src/record.rs +++ b/libcc2rs-macros/src/record.rs @@ -3,7 +3,7 @@ use proc_macro::TokenStream; use quote::{format_ident, quote}; -use syn::{Data, DeriveInput, Fields, LitInt, parse_macro_input}; +use syn::{Data, DeriveInput, Expr, Fields, parse_macro_input}; pub fn expand(input: TokenStream) -> TokenStream { let input = parse_macro_input!(input as DeriveInput); @@ -12,20 +12,19 @@ pub fn expand(input: TokenStream) -> TokenStream { let Data::Struct(data) = &input.data else { panic!("derive(Record) is only supported on structs, `{name}` is not one"); }; - let mut fields: Vec<_> = match &data.fields { + let fields: Vec<_> = match &data.fields { Fields::Named(fields) => fields .named .iter() .map(|field| { let ident = field.ident.clone().unwrap(); - let offset: LitInt = field + let offset: Expr = field .attrs .iter() .find(|attr| attr.path().is_ident("offset")) .unwrap_or_else(|| panic!("field `{name}::{ident}` has no #[offset(...)]")) .parse_args() .unwrap(); - let offset: usize = offset.base10_parse().unwrap(); (ident, offset) }) .collect(), @@ -33,21 +32,28 @@ pub fn expand(input: TokenStream) -> TokenStream { }; let offsets = format_ident!("__{}_offsets", name); let idents: Vec<_> = fields.iter().map(|(ident, _)| ident.clone()).collect(); - let values: Vec<_> = fields.iter().map(|(_, offset)| *offset).collect(); + let values: Vec<_> = fields.iter().map(|(_, offset)| offset.clone()).collect(); - // The field that contains a byte is the last one that starts before it. - fields.sort_by_key(|(_, offset)| std::cmp::Reverse(*offset)); - let locate = fields.iter().map(|(ident, offset)| { + // The field that contains a byte is one that starts before it, usually + // the last one, as fields are usually declared in the order of their + // offsets. + let locate = fields.iter().rev().map(|(ident, _)| { quote! { - if offset >= #offset { - return ::libcc2rs::__locate_field!(&self.#ident, offset - #offset, ty); + if offset >= Self::OFFSETS.#ident { + if let ::core::option::Option::Some(field) = ::libcc2rs::__locate_field!( + &self.#ident, offset - Self::OFFSETS.#ident, ty) { + return ::core::option::Option::Some(field); + } } } }); - let locate_mut = fields.iter().map(|(ident, offset)| { + let locate_mut = fields.iter().rev().map(|(ident, _)| { quote! { - if offset >= #offset { - return ::libcc2rs::__locate_field_mut!(&mut self.#ident, offset - #offset, ty); + if offset >= Self::OFFSETS.#ident { + if let ::core::option::Option::Some(field) = ::libcc2rs::__locate_field_mut!( + &mut self.#ident, offset - Self::OFFSETS.#ident, ty) { + return ::core::option::Option::Some(field); + } } } }); diff --git a/libcc2rs/src/field.rs b/libcc2rs/src/field.rs index c1768ab2be..742750f200 100644 --- a/libcc2rs/src/field.rs +++ b/libcc2rs/src/field.rs @@ -173,6 +173,95 @@ macro_rules! field_ptr { }}; } +// Something that is read and written in place, like the object a pointer +// points to. +pub trait Place: Clone { + type Target; + fn with(&self, f: impl FnOnce(&Self::Target) -> R) -> R; + fn with_mut(&self, f: impl FnOnce(&mut Self::Target) -> R) -> R; +} + +impl Place for Ptr { + type Target = T; + #[inline] + fn with(&self, f: impl FnOnce(&T) -> R) -> R { + Ptr::with(self, f) + } + #[inline] + fn with_mut(&self, f: impl FnOnce(&mut T) -> R) -> R { + Ptr::with_mut(self, f) + } +} + +// A field of the struct in place `base`, which is accessed through `base` and +// projected to the field, without looking the field up as a pointer to it +// does. Written by `field!(p, x)`. +pub struct FieldPlace<'a, B: Place, T> { + pub base: &'a B, + pub get: fn(&B::Target) -> &T, + pub get_mut: fn(&mut B::Target) -> &mut T, +} + +impl Clone for FieldPlace<'_, B, T> { + #[inline] + fn clone(&self) -> Self { + *self + } +} + +impl Copy for FieldPlace<'_, B, T> {} + +impl FieldPlace<'_, B, T> { + #[inline] + pub fn with(&self, f: impl FnOnce(&T) -> R) -> R { + self.base.with(|s| f((self.get)(s))) + } + + #[inline] + pub fn with_mut(&self, f: impl FnOnce(&mut T) -> R) -> R { + self.base.with_mut(|s| f((self.get_mut)(s))) + } + + #[inline] + pub fn read(&self) -> T + where + T: Clone, + { + self.with(T::clone) + } + + #[inline] + pub fn write(&self, value: T) { + self.with_mut(|v| *v = value) + } +} + +impl Place for FieldPlace<'_, B, T> { + type Target = T; + #[inline] + fn with(&self, f: impl FnOnce(&T) -> R) -> R { + FieldPlace::with(self, f) + } + #[inline] + fn with_mut(&self, f: impl FnOnce(&mut T) -> R) -> R { + FieldPlace::with_mut(self, f) + } +} + +// Field `field` of the struct in the place `base` (a Ptr, or another field), +// e.g., `field!(p, x).write(1)`. It is a struct expression, so that a +// temporary base lives as long as the field when bound to a variable. +#[macro_export] +macro_rules! field { + ($base:expr, $field:ident) => { + $crate::FieldPlace { + base: &$base, + get: |__s| &__s.$field, + get_mut: |__s| &mut __s.$field, + } + }; +} + // Finds the object at a byte offset of a field for Record::locate. The // implementation is chosen based on the type of the field, by autoref-based // specialization: `(&&Locate(&field)).locate(...)` picks LocateRecord if the @@ -426,6 +515,19 @@ mod tests { raw.delete(); } + #[test] + fn field_place() { + let s = outer(); + let p = s.as_pointer(); + field!(p, x).write(3); + assert_eq!(s.borrow().x, 3); + field!(field!(p, inner), b).with_mut(|b| *b += 2); + assert_eq!(field!(field!(p, inner), b).read(), 2); + let items: Ptr = s.borrow().items.as_pointer(); + field!(items.offset(1), a).write(7); + assert_eq!(s.borrow().items.borrow()[1].a, 7); + } + #[test] #[should_panic(expected = "ub: invalid field pointer")] fn out_of_bounds_field() { diff --git a/libcc2rs/src/lib.rs b/libcc2rs/src/lib.rs index 2c504f35e6..f187da9685 100644 --- a/libcc2rs/src/lib.rs +++ b/libcc2rs/src/lib.rs @@ -10,7 +10,7 @@ mod rc; pub use rc::*; mod field; -pub use field::{FieldPtr, Record}; +pub use field::{FieldPlace, FieldPtr, Place, Record}; // Used by #[derive(Record)]. #[doc(hidden)] pub mod __field { diff --git a/rules/dirent/shim.rs b/rules/dirent/shim.rs index eeb6cec55f..45acd7f4f5 100644 --- a/rules/dirent/shim.rs +++ b/rules/dirent/shim.rs @@ -1,25 +1,33 @@ // Copyright (c) 2022-present INESC-ID. // Distributed under the MIT license that can be found in the LICENSE file. -use crate::{ByteRepr, Value}; +use crate::{ByteRepr, Record, Value}; use std::cell::{Cell, RefCell}; +use std::mem::{offset_of, size_of}; use std::rc::Rc; +#[derive(Record)] pub struct Dirent { - pub d_ino: Value, - pub d_off: Value, - pub d_reclen: Value, - pub d_type: Value, + #[offset(offset_of!(::libc::dirent, d_ino))] + pub d_ino: u64, + #[cfg_attr(target_os = "linux", offset(offset_of!(::libc::dirent, d_off)))] + #[cfg_attr(target_os = "macos", offset(offset_of!(::libc::dirent, d_seekoff)))] + pub d_off: i64, + #[offset(offset_of!(::libc::dirent, d_reclen))] + pub d_reclen: u16, + #[offset(offset_of!(::libc::dirent, d_type))] + pub d_type: u8, + #[offset(offset_of!(::libc::dirent, d_name))] pub d_name: Value>, } impl Default for Dirent { fn default() -> Self { Self { - d_ino: Rc::new(RefCell::new(0)), - d_off: Rc::new(RefCell::new(0)), - d_reclen: Rc::new(RefCell::new(0)), - d_type: Rc::new(RefCell::new(0)), + d_ino: 0, + d_off: 0, + d_reclen: 0, + d_type: 0, d_name: Rc::new(RefCell::new(vec![0u8; 256].into_boxed_slice())), } } @@ -27,9 +35,11 @@ impl Default for Dirent { impl Dirent { pub fn from_entry(ino: u64, name: &[u8], d_type: u8) -> Self { - let de = Dirent::default(); - *de.d_ino.borrow_mut() = ino; - *de.d_type.borrow_mut() = d_type; + let de = Dirent { + d_ino: ino, + d_type, + ..Dirent::default() + }; { let mut nm = de.d_name.borrow_mut(); let n = name.len().min(nm.len() - 1); @@ -43,16 +53,17 @@ impl Dirent { impl Clone for Dirent { fn clone(&self) -> Self { Self { - d_ino: Rc::new(RefCell::new(*self.d_ino.borrow())), - d_off: Rc::new(RefCell::new(*self.d_off.borrow())), - d_reclen: Rc::new(RefCell::new(*self.d_reclen.borrow())), - d_type: Rc::new(RefCell::new(*self.d_type.borrow())), d_name: Rc::new(RefCell::new(self.d_name.borrow().clone())), + ..*self } } } -impl ByteRepr for Dirent {} +impl ByteRepr for Dirent { + fn byte_size() -> usize { + size_of::<::libc::dirent>() + } +} pub struct CDir { pub entries: Vec<(u64, Vec, u8)>, diff --git a/rules/ifaddrs/shim.rs b/rules/ifaddrs/shim.rs index 9e55df74cb..ad4b673994 100644 --- a/rules/ifaddrs/shim.rs +++ b/rules/ifaddrs/shim.rs @@ -1,17 +1,21 @@ // Copyright (c) 2022-present INESC-ID. // Distributed under the MIT license that can be found in the LICENSE file. -use crate::{ByteRepr, Ptr, Sockaddr, SockaddrIn, SockaddrIn6, SockaddrStorage, Value}; -use std::cell::RefCell; -use std::rc::Rc; +use crate::{ByteRepr, Ptr, Record, Sockaddr, SockaddrIn, SockaddrIn6, SockaddrStorage}; +use std::mem::{offset_of, size_of}; -#[derive(Default)] +#[derive(Clone, Default, Record)] pub struct Ifaddrs { - pub ifa_next: Value>, - pub ifa_name: Value>, - pub ifa_flags: Value, - pub ifa_addr: Value>, - pub ifa_netmask: Value>, + #[offset(offset_of!(::libc::ifaddrs, ifa_next))] + pub ifa_next: Ptr, + #[offset(offset_of!(::libc::ifaddrs, ifa_name))] + pub ifa_name: Ptr, + #[offset(offset_of!(::libc::ifaddrs, ifa_flags))] + pub ifa_flags: u32, + #[offset(offset_of!(::libc::ifaddrs, ifa_addr))] + pub ifa_addr: Ptr, + #[offset(offset_of!(::libc::ifaddrs, ifa_netmask))] + pub ifa_netmask: Ptr, } impl Ifaddrs { @@ -38,29 +42,21 @@ impl Ifaddrs { }, } } - let node = Ifaddrs::default(); + let mut node = Ifaddrs::default(); let mut name = ifa.interface_name.clone().into_bytes(); name.push(0); - *node.ifa_name.borrow_mut() = Ptr::alloc_array(name.into_boxed_slice()); - *node.ifa_flags.borrow_mut() = ifa.flags.bits() as u32; - *node.ifa_addr.borrow_mut() = mk_addr(ifa.address.as_ref()); - *node.ifa_netmask.borrow_mut() = mk_addr(ifa.netmask.as_ref()); + node.ifa_name = Ptr::alloc_array(name.into_boxed_slice()); + node.ifa_flags = ifa.flags.bits() as u32; + node.ifa_addr = mk_addr(ifa.address.as_ref()); + node.ifa_netmask = mk_addr(ifa.netmask.as_ref()); node } } -impl Clone for Ifaddrs { - fn clone(&self) -> Self { - Self { - ifa_next: Rc::new(RefCell::new(self.ifa_next.borrow().clone())), - ifa_name: Rc::new(RefCell::new(self.ifa_name.borrow().clone())), - ifa_flags: Rc::new(RefCell::new(*self.ifa_flags.borrow())), - ifa_addr: Rc::new(RefCell::new(self.ifa_addr.borrow().clone())), - ifa_netmask: Rc::new(RefCell::new(self.ifa_netmask.borrow().clone())), - } +impl ByteRepr for Ifaddrs { + fn byte_size() -> usize { + size_of::<::libc::ifaddrs>() } } -impl ByteRepr for Ifaddrs {} - impl ByteRepr for ::libc::ifaddrs {} diff --git a/rules/ifaddrs/tgt_refcount.rs b/rules/ifaddrs/tgt_refcount.rs index 3d953f7a98..e87fbe986e 100644 --- a/rules/ifaddrs/tgt_refcount.rs +++ b/rules/ifaddrs/tgt_refcount.rs @@ -14,8 +14,8 @@ fn f1(a0: Ptr>) -> i32 { let __list: Vec = __ifas.collect(); let mut __next = Ptr::::null(); for __ifa in __list.iter().rev() { - let __node = Ifaddrs::from_interface_address(__ifa); - *__node.ifa_next.borrow_mut() = __next.clone(); + let mut __node = Ifaddrs::from_interface_address(__ifa); + __node.ifa_next = __next.clone(); __next = Ptr::alloc(__node); } __out.write(__next); @@ -32,10 +32,10 @@ fn f2(a0: Ptr) { let mut __cur = a0; while !__cur.is_null() { let __next = __cur.with(|__i| { - __i.ifa_name.borrow().delete(); - __i.ifa_addr.borrow().delete(); - __i.ifa_netmask.borrow().delete(); - (*__i.ifa_next.borrow()).clone() + __i.ifa_name.delete(); + __i.ifa_addr.delete(); + __i.ifa_netmask.delete(); + __i.ifa_next.clone() }); __cur.delete(); __cur = __next; diff --git a/rules/ip/shim.rs b/rules/ip/shim.rs index e767de547c..b9f839a732 100644 --- a/rules/ip/shim.rs +++ b/rules/ip/shim.rs @@ -1,16 +1,19 @@ // Copyright (c) 2022-present INESC-ID. // Distributed under the MIT license that can be found in the LICENSE file. -use crate::{AsPointer, ByteRepr, Ptr, Value}; +use crate::{AsPointer, ByteRepr, Ptr, Record, Value}; use std::cell::RefCell; use std::rc::Rc; -#[derive(Default)] +#[derive(Clone, Default, Record)] pub struct InAddr { - pub s_addr: Value, + #[offset(0)] + pub s_addr: u32, } +#[derive(Record)] pub struct In6Addr { + #[offset(0)] pub s6_addr: Value>, } @@ -28,14 +31,6 @@ impl Default for In6Addr { } } -impl Clone for InAddr { - fn clone(&self) -> Self { - Self { - s_addr: Rc::new(RefCell::new(*self.s_addr.borrow())), - } - } -} - impl Clone for In6Addr { fn clone(&self) -> Self { Self { @@ -49,11 +44,11 @@ impl ByteRepr for InAddr { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.s_addr.borrow()).to_bytes(&mut buf[0..4]); + self.s_addr.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - s_addr: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + s_addr: ::from_bytes(&buf[0..4]), } } } diff --git a/rules/netdb/shim.rs b/rules/netdb/shim.rs index a4ef6b4677..f5aa89f10a 100644 --- a/rules/netdb/shim.rs +++ b/rules/netdb/shim.rs @@ -1,37 +1,33 @@ // Copyright (c) 2022-present INESC-ID. // Distributed under the MIT license that can be found in the LICENSE file. -use crate::{ByteRepr, Ptr, Sockaddr, Value}; -use std::cell::RefCell; -use std::rc::Rc; +use crate::{ByteRepr, Ptr, Record, Sockaddr}; +use std::mem::{offset_of, size_of}; -#[derive(Default)] +#[derive(Clone, Default, Record)] pub struct Addrinfo { - pub ai_flags: Value, - pub ai_family: Value, - pub ai_socktype: Value, - pub ai_protocol: Value, - pub ai_addrlen: Value, - pub ai_addr: Value>, - pub ai_canonname: Value>, - pub ai_next: Value>, + #[offset(offset_of!(::libc::addrinfo, ai_flags))] + pub ai_flags: i32, + #[offset(offset_of!(::libc::addrinfo, ai_family))] + pub ai_family: i32, + #[offset(offset_of!(::libc::addrinfo, ai_socktype))] + pub ai_socktype: i32, + #[offset(offset_of!(::libc::addrinfo, ai_protocol))] + pub ai_protocol: i32, + #[offset(offset_of!(::libc::addrinfo, ai_addrlen))] + pub ai_addrlen: u32, + #[offset(offset_of!(::libc::addrinfo, ai_addr))] + pub ai_addr: Ptr, + #[offset(offset_of!(::libc::addrinfo, ai_canonname))] + pub ai_canonname: Ptr, + #[offset(offset_of!(::libc::addrinfo, ai_next))] + pub ai_next: Ptr, } -impl Clone for Addrinfo { - fn clone(&self) -> Self { - Self { - ai_flags: Rc::new(RefCell::new(*self.ai_flags.borrow())), - ai_family: Rc::new(RefCell::new(*self.ai_family.borrow())), - ai_socktype: Rc::new(RefCell::new(*self.ai_socktype.borrow())), - ai_protocol: Rc::new(RefCell::new(*self.ai_protocol.borrow())), - ai_addrlen: Rc::new(RefCell::new(*self.ai_addrlen.borrow())), - ai_addr: Rc::new(RefCell::new(self.ai_addr.borrow().clone())), - ai_canonname: Rc::new(RefCell::new(self.ai_canonname.borrow().clone())), - ai_next: Rc::new(RefCell::new(self.ai_next.borrow().clone())), - } +impl ByteRepr for Addrinfo { + fn byte_size() -> usize { + size_of::<::libc::addrinfo>() } } -impl ByteRepr for Addrinfo {} - impl ByteRepr for ::libc::addrinfo {} diff --git a/rules/netdb/tgt_refcount.rs b/rules/netdb/tgt_refcount.rs index 488137fbff..b33ae2a24d 100644 --- a/rules/netdb/tgt_refcount.rs +++ b/rules/netdb/tgt_refcount.rs @@ -15,17 +15,17 @@ fn f1(a0: Ptr, a1: Ptr, a2: Ptr, a3: Ptr>) -> i3 let __family = if __hints.is_null() { ::libc::AF_UNSPEC } else { - __hints.with(|__h| *__h.ai_family.borrow()) + __hints.with(|__h| __h.ai_family) }; let __socktype = if __hints.is_null() { 0 } else { - __hints.with(|__h| *__h.ai_socktype.borrow()) + __hints.with(|__h| __h.ai_socktype) }; let __protocol = if __hints.is_null() { 0 } else { - __hints.with(|__h| *__h.ai_protocol.borrow()) + __hints.with(|__h| __h.ai_protocol) }; let __port: u16 = if __service.is_null() { 0 @@ -66,30 +66,28 @@ fn f1(a0: Ptr, a1: Ptr, a2: Ptr, a3: Ptr>) -> i3 } else { let mut __next = Ptr::::null(); for __ip in __addrs.iter().rev() { - let __ai = Addrinfo::default(); - *__ai.ai_socktype.borrow_mut() = __socktype; - *__ai.ai_protocol.borrow_mut() = __protocol; + let mut __ai = Addrinfo::default(); + __ai.ai_socktype = __socktype; + __ai.ai_protocol = __protocol; let __storage = Ptr::alloc(SockaddrStorage::default()); match __ip { ::std::net::IpAddr::V4(__v4) => { - *__ai.ai_family.borrow_mut() = ::libc::AF_INET; - *__ai.ai_addrlen.borrow_mut() = - ::std::mem::size_of::<::libc::sockaddr_in>() as u32; + __ai.ai_family = ::libc::AF_INET; + __ai.ai_addrlen = ::std::mem::size_of::<::libc::sockaddr_in>() as u32; __storage .reinterpret_cast::() .write(SockaddrIn::from_ipv4(__v4, __port)); } ::std::net::IpAddr::V6(__v6) => { - *__ai.ai_family.borrow_mut() = ::libc::AF_INET6; - *__ai.ai_addrlen.borrow_mut() = - ::std::mem::size_of::<::libc::sockaddr_in6>() as u32; + __ai.ai_family = ::libc::AF_INET6; + __ai.ai_addrlen = ::std::mem::size_of::<::libc::sockaddr_in6>() as u32; __storage .reinterpret_cast::() .write(SockaddrIn6::from_ipv6(__v6, __port)); } } - *__ai.ai_addr.borrow_mut() = __storage.reinterpret_cast::(); - *__ai.ai_next.borrow_mut() = __next.clone(); + __ai.ai_addr = __storage.reinterpret_cast::(); + __ai.ai_next = __next.clone(); __next = Ptr::alloc(__ai); } __out.write(__next); @@ -101,11 +99,10 @@ fn f2(a0: Ptr) { let mut __cur = a0; while !__cur.is_null() { let __next = __cur.with(|__ai| { - let __addr = __ai.ai_addr.borrow(); - if !__addr.is_null() { - __addr.delete(); + if !__ai.ai_addr.is_null() { + __ai.ai_addr.delete(); } - (*__ai.ai_next.borrow()).clone() + __ai.ai_next.clone() }); __cur.delete(); __cur = __next; diff --git a/rules/poll/shim.rs b/rules/poll/shim.rs index 51b1be845a..7307b13455 100644 --- a/rules/poll/shim.rs +++ b/rules/poll/shim.rs @@ -1,27 +1,23 @@ // Copyright (c) 2022-present INESC-ID. // Distributed under the MIT license that can be found in the LICENSE file. -use crate::{ByteRepr, Value}; -use std::cell::RefCell; -use std::rc::Rc; +use crate::{ByteRepr, Record}; +use std::mem::{offset_of, size_of}; -#[derive(Default)] +#[derive(Clone, Default, Record)] pub struct Pollfd { - pub fd: Value, - pub events: Value, - pub revents: Value, + #[offset(offset_of!(::libc::pollfd, fd))] + pub fd: i32, + #[offset(offset_of!(::libc::pollfd, events))] + pub events: i16, + #[offset(offset_of!(::libc::pollfd, revents))] + pub revents: i16, } -impl Clone for Pollfd { - fn clone(&self) -> Self { - Self { - fd: Rc::new(RefCell::new(*self.fd.borrow())), - events: Rc::new(RefCell::new(*self.events.borrow())), - revents: Rc::new(RefCell::new(*self.revents.borrow())), - } +impl ByteRepr for Pollfd { + fn byte_size() -> usize { + size_of::<::libc::pollfd>() } } -impl ByteRepr for Pollfd {} - impl ByteRepr for ::libc::pollfd {} diff --git a/rules/poll/tgt_refcount.rs b/rules/poll/tgt_refcount.rs index 9c47e37397..c6d658a61d 100644 --- a/rules/poll/tgt_refcount.rs +++ b/rules/poll/tgt_refcount.rs @@ -17,11 +17,8 @@ fn f1(a0: Ptr, a1: u64, a2: i32) -> i32 { let mut __wanted = Vec::new(); let mut __events = Vec::new(); for __i in 0..(a1 as usize) { - let (__fd, __ev) = __p - .offset(__i) - .with(|__e| (*__e.fd.borrow(), *__e.events.borrow())); - __p.offset(__i) - .with_mut(|__e| *__e.revents.borrow_mut() = 0); + let (__fd, __ev) = __p.offset(__i).with(|__e| (__e.fd, __e.events)); + __p.offset(__i).with_mut(|__e| __e.revents = 0); if __fd >= 0 { __idx.push(__i); __wanted.push(__fd); @@ -43,8 +40,7 @@ fn f1(a0: Ptr, a1: u64, a2: i32) -> i32 { Some(__r) => __r.bits(), None => 0, }; - __p.offset(__i) - .with_mut(|__e| *__e.revents.borrow_mut() = __rev); + __p.offset(__i).with_mut(|__e| __e.revents = __rev); } __count } diff --git a/rules/pwd/shim.rs b/rules/pwd/shim.rs index 9798c02274..efc4315b2a 100644 --- a/rules/pwd/shim.rs +++ b/rules/pwd/shim.rs @@ -1,33 +1,39 @@ // Copyright (c) 2022-present INESC-ID. // Distributed under the MIT license that can be found in the LICENSE file. -use crate::{ByteRepr, Ptr, Value}; -use std::cell::RefCell; -use std::rc::Rc; +use crate::{ByteRepr, Ptr, Record}; +use std::mem::{offset_of, size_of}; -#[derive(Default)] +#[derive(Clone, Default, Record)] pub struct Passwd { - pub pw_name: Value>, - pub pw_passwd: Value>, - pub pw_uid: Value, - pub pw_gid: Value, - pub pw_gecos: Value>, - pub pw_dir: Value>, - pub pw_shell: Value>, + #[offset(offset_of!(::libc::passwd, pw_name))] + pub pw_name: Ptr, + #[offset(offset_of!(::libc::passwd, pw_passwd))] + pub pw_passwd: Ptr, + #[offset(offset_of!(::libc::passwd, pw_uid))] + pub pw_uid: u32, + #[offset(offset_of!(::libc::passwd, pw_gid))] + pub pw_gid: u32, + #[offset(offset_of!(::libc::passwd, pw_gecos))] + pub pw_gecos: Ptr, + #[offset(offset_of!(::libc::passwd, pw_dir))] + pub pw_dir: Ptr, + #[offset(offset_of!(::libc::passwd, pw_shell))] + pub pw_shell: Ptr, } impl Passwd { pub fn from_user(u: &nix::unistd::User) -> Self { - let mk = |s: &[u8]| -> Value> { + let mk = |s: &[u8]| -> Ptr { let mut v = s.to_vec(); v.push(0); - Rc::new(RefCell::new(Ptr::alloc_array(v.into_boxed_slice()))) + Ptr::alloc_array(v.into_boxed_slice()) }; Self { pw_name: mk(u.name.as_bytes()), pw_passwd: mk(u.passwd.as_bytes()), - pw_uid: Rc::new(RefCell::new(u.uid.as_raw())), - pw_gid: Rc::new(RefCell::new(u.gid.as_raw())), + pw_uid: u.uid.as_raw(), + pw_gid: u.gid.as_raw(), pw_gecos: mk(u.gecos.as_bytes()), pw_dir: mk(u.dir.as_os_str().as_encoded_bytes()), pw_shell: mk(u.shell.as_os_str().as_encoded_bytes()), @@ -36,31 +42,21 @@ impl Passwd { pub fn from_user_in(u: &nix::unistd::User, strings: &[Ptr]) -> Self { Self { - pw_name: Rc::new(RefCell::new(strings[0].clone())), - pw_passwd: Rc::new(RefCell::new(strings[1].clone())), - pw_uid: Rc::new(RefCell::new(u.uid.as_raw())), - pw_gid: Rc::new(RefCell::new(u.gid.as_raw())), - pw_gecos: Rc::new(RefCell::new(strings[2].clone())), - pw_dir: Rc::new(RefCell::new(strings[3].clone())), - pw_shell: Rc::new(RefCell::new(strings[4].clone())), + pw_name: strings[0].clone(), + pw_passwd: strings[1].clone(), + pw_uid: u.uid.as_raw(), + pw_gid: u.gid.as_raw(), + pw_gecos: strings[2].clone(), + pw_dir: strings[3].clone(), + pw_shell: strings[4].clone(), } } } -impl Clone for Passwd { - fn clone(&self) -> Self { - Self { - pw_name: Rc::new(RefCell::new(self.pw_name.borrow().clone())), - pw_passwd: Rc::new(RefCell::new(self.pw_passwd.borrow().clone())), - pw_uid: Rc::new(RefCell::new(*self.pw_uid.borrow())), - pw_gid: Rc::new(RefCell::new(*self.pw_gid.borrow())), - pw_gecos: Rc::new(RefCell::new(self.pw_gecos.borrow().clone())), - pw_dir: Rc::new(RefCell::new(self.pw_dir.borrow().clone())), - pw_shell: Rc::new(RefCell::new(self.pw_shell.borrow().clone())), - } +impl ByteRepr for Passwd { + fn byte_size() -> usize { + size_of::<::libc::passwd>() } } -impl ByteRepr for Passwd {} - impl ByteRepr for ::libc::passwd {} diff --git a/rules/select/tgt_refcount.rs b/rules/select/tgt_refcount.rs index 2e5c273f5d..0f1319ffc5 100644 --- a/rules/select/tgt_refcount.rs +++ b/rules/select/tgt_refcount.rs @@ -61,9 +61,9 @@ fn f1(a0: i32, a1: Ptr, a2: Ptr, a3: Ptr, a4: Ptr None, - false => Some(__tp.with(|__t| { - nix::sys::time::TimeVal::new(*__t.tv_sec.borrow() as _, *__t.tv_usec.borrow() as _) - })), + false => Some( + __tp.with(|__t| nix::sys::time::TimeVal::new(__t.tv_sec as _, __t.tv_usec as _)), + ), }; match nix::sys::select::select( a0, @@ -114,8 +114,8 @@ fn f1(a0: i32, a1: Ptr, a2: Ptr, a3: Ptr, a4: Ptr __tp.with_mut(|__dst| { - *__dst.tv_sec.borrow_mut() = __t.tv_sec() as i64; - *__dst.tv_usec.borrow_mut() = __t.tv_usec() as i64; + __dst.tv_sec = __t.tv_sec() as i64; + __dst.tv_usec = __t.tv_usec() as i64; }), _ => {} } diff --git a/rules/socket/shim.rs b/rules/socket/shim.rs index 62476f4e8c..5618f4219c 100644 --- a/rules/socket/shim.rs +++ b/rules/socket/shim.rs @@ -1,38 +1,58 @@ // Copyright (c) 2022-present INESC-ID. // Distributed under the MIT license that can be found in the LICENSE file. -use crate::{ByteRepr, In6Addr, InAddr, Ptr, Value}; +use crate::{ByteRepr, In6Addr, InAddr, Ptr, Record, Value}; use std::cell::RefCell; use std::rc::Rc; +// The fields are at the offsets of the byte representation below. +#[derive(Record)] pub struct Sockaddr { - pub sa_family: Value, + #[offset(0)] + pub sa_family: u16, + #[offset(2)] pub sa_data: Value>, } +#[derive(Record)] pub struct SockaddrIn { - pub sin_family: Value, - pub sin_port: Value, - pub sin_addr: Value, + #[offset(0)] + pub sin_family: u16, + #[offset(2)] + pub sin_port: u16, + #[offset(4)] + pub sin_addr: InAddr, + #[offset(8)] pub sin_zero: Value>, } -#[derive(Default)] +#[derive(Default, Record)] pub struct SockaddrIn6 { - pub sin6_family: Value, - pub sin6_port: Value, - pub sin6_flowinfo: Value, - pub sin6_addr: Value, - pub sin6_scope_id: Value, + #[offset(0)] + pub sin6_family: u16, + #[offset(2)] + pub sin6_port: u16, + #[offset(4)] + pub sin6_flowinfo: u32, + #[offset(8)] + pub sin6_addr: In6Addr, + #[offset(24)] + pub sin6_scope_id: u32, } +#[derive(Record)] pub struct SockaddrUn { - pub sun_family: Value, + #[offset(0)] + pub sun_family: u16, + #[offset(2)] pub sun_path: Value>, } +#[derive(Record)] pub struct SockaddrStorage { - pub ss_family: Value, + #[offset(0)] + pub ss_family: u16, + #[offset(2)] pub __pad: Value>, } @@ -40,11 +60,11 @@ impl SockaddrIn { #[allow(clippy::unnecessary_cast)] pub fn from_libc(l: &::libc::sockaddr_in) -> Self { Self { - sin_family: Rc::new(RefCell::new(l.sin_family as u16)), - sin_port: Rc::new(RefCell::new(l.sin_port)), - sin_addr: Rc::new(RefCell::new(InAddr { - s_addr: Rc::new(RefCell::new(l.sin_addr.s_addr)), - })), + sin_family: l.sin_family as u16, + sin_port: l.sin_port, + sin_addr: InAddr { + s_addr: l.sin_addr.s_addr, + }, sin_zero: Rc::new(RefCell::new( l.sin_zero .iter() @@ -56,11 +76,14 @@ impl SockaddrIn { } pub fn from_ipv4(addr: &::std::net::Ipv4Addr, port: u16) -> Self { - let s = Self::default(); - *s.sin_family.borrow_mut() = ::libc::AF_INET as u16; - *s.sin_port.borrow_mut() = port.to_be(); - *s.sin_addr.borrow().s_addr.borrow_mut() = u32::from(*addr).to_be(); - s + Self { + sin_family: ::libc::AF_INET as u16, + sin_port: port.to_be(), + sin_addr: InAddr { + s_addr: u32::from(*addr).to_be(), + }, + ..Self::default() + } } #[cfg(target_os = "linux")] @@ -68,10 +91,10 @@ impl SockaddrIn { let mut sin_zero = [0u8; 8]; sin_zero.copy_from_slice(&self.sin_zero.borrow()); ::libc::sockaddr_in { - sin_family: *self.sin_family.borrow(), - sin_port: *self.sin_port.borrow(), + sin_family: self.sin_family, + sin_port: self.sin_port, sin_addr: ::libc::in_addr { - s_addr: *self.sin_addr.borrow().s_addr.borrow(), + s_addr: self.sin_addr.s_addr, }, sin_zero, } @@ -85,10 +108,10 @@ impl SockaddrIn { } ::libc::sockaddr_in { sin_len: ::std::mem::size_of::<::libc::sockaddr_in>() as u8, - sin_family: *self.sin_family.borrow() as u8, - sin_port: *self.sin_port.borrow(), + sin_family: self.sin_family as u8, + sin_port: self.sin_port, sin_addr: ::libc::in_addr { - s_addr: *self.sin_addr.borrow().s_addr.borrow(), + s_addr: self.sin_addr.s_addr, }, sin_zero, } @@ -99,24 +122,25 @@ impl SockaddrIn6 { #[allow(clippy::unnecessary_cast)] pub fn from_libc(l: &::libc::sockaddr_in6) -> Self { Self { - sin6_family: Rc::new(RefCell::new(l.sin6_family as u16)), - sin6_port: Rc::new(RefCell::new(l.sin6_port)), - sin6_flowinfo: Rc::new(RefCell::new(l.sin6_flowinfo)), - sin6_addr: Rc::new(RefCell::new(In6Addr { + sin6_family: l.sin6_family as u16, + sin6_port: l.sin6_port, + sin6_flowinfo: l.sin6_flowinfo, + sin6_addr: In6Addr { s6_addr: Rc::new(RefCell::new( l.sin6_addr.s6_addr.to_vec().into_boxed_slice(), )), - })), - sin6_scope_id: Rc::new(RefCell::new(l.sin6_scope_id)), + }, + sin6_scope_id: l.sin6_scope_id, } } pub fn from_ipv6(addr: &::std::net::Ipv6Addr, port: u16) -> Self { - let s = Self::default(); - *s.sin6_family.borrow_mut() = ::libc::AF_INET6 as u16; - *s.sin6_port.borrow_mut() = port.to_be(); + let s = Self { + sin6_family: ::libc::AF_INET6 as u16, + sin6_port: port.to_be(), + ..Self::default() + }; s.sin6_addr - .borrow() .s6_addr .borrow_mut() .copy_from_slice(&addr.octets()); @@ -126,27 +150,27 @@ impl SockaddrIn6 { #[cfg(target_os = "linux")] pub fn to_libc(&self) -> ::libc::sockaddr_in6 { let mut s6_addr = [0u8; 16]; - s6_addr.copy_from_slice(&self.sin6_addr.borrow().s6_addr.borrow()); + s6_addr.copy_from_slice(&self.sin6_addr.s6_addr.borrow()); ::libc::sockaddr_in6 { - sin6_family: *self.sin6_family.borrow(), - sin6_port: *self.sin6_port.borrow(), - sin6_flowinfo: *self.sin6_flowinfo.borrow(), + sin6_family: self.sin6_family, + sin6_port: self.sin6_port, + sin6_flowinfo: self.sin6_flowinfo, sin6_addr: ::libc::in6_addr { s6_addr }, - sin6_scope_id: *self.sin6_scope_id.borrow(), + sin6_scope_id: self.sin6_scope_id, } } #[cfg(target_os = "macos")] pub fn to_libc(&self) -> ::libc::sockaddr_in6 { let mut s6_addr = [0u8; 16]; - s6_addr.copy_from_slice(&self.sin6_addr.borrow().s6_addr.borrow()); + s6_addr.copy_from_slice(&self.sin6_addr.s6_addr.borrow()); ::libc::sockaddr_in6 { sin6_len: ::std::mem::size_of::<::libc::sockaddr_in6>() as u8, - sin6_family: *self.sin6_family.borrow() as u8, - sin6_port: *self.sin6_port.borrow(), - sin6_flowinfo: *self.sin6_flowinfo.borrow(), + sin6_family: self.sin6_family as u8, + sin6_port: self.sin6_port, + sin6_flowinfo: self.sin6_flowinfo, sin6_addr: ::libc::in6_addr { s6_addr }, - sin6_scope_id: *self.sin6_scope_id.borrow(), + sin6_scope_id: self.sin6_scope_id, } } } @@ -154,7 +178,7 @@ impl SockaddrIn6 { impl Default for Sockaddr { fn default() -> Self { Self { - sa_family: Rc::new(RefCell::new(0)), + sa_family: 0, sa_data: Rc::new(RefCell::new(vec![0u8; 14].into_boxed_slice())), } } @@ -163,9 +187,9 @@ impl Default for Sockaddr { impl Default for SockaddrIn { fn default() -> Self { Self { - sin_family: Rc::new(RefCell::new(0)), - sin_port: Rc::new(RefCell::new(0)), - sin_addr: Rc::new(RefCell::new(InAddr::default())), + sin_family: 0, + sin_port: 0, + sin_addr: InAddr::default(), sin_zero: Rc::new(RefCell::new(vec![0u8; 8].into_boxed_slice())), } } @@ -174,7 +198,7 @@ impl Default for SockaddrIn { impl Default for SockaddrUn { fn default() -> Self { Self { - sun_family: Rc::new(RefCell::new(0)), + sun_family: 0, sun_path: Rc::new(RefCell::new(vec![0u8; 108].into_boxed_slice())), } } @@ -183,7 +207,7 @@ impl Default for SockaddrUn { impl Default for SockaddrStorage { fn default() -> Self { Self { - ss_family: Rc::new(RefCell::new(0)), + ss_family: 0, __pad: Rc::new(RefCell::new(vec![0u8; 126].into_boxed_slice())), } } @@ -192,7 +216,7 @@ impl Default for SockaddrStorage { impl Clone for Sockaddr { fn clone(&self) -> Self { Self { - sa_family: Rc::new(RefCell::new(*self.sa_family.borrow())), + sa_family: self.sa_family, sa_data: Rc::new(RefCell::new(self.sa_data.borrow().clone())), } } @@ -201,9 +225,9 @@ impl Clone for Sockaddr { impl Clone for SockaddrIn { fn clone(&self) -> Self { Self { - sin_family: Rc::new(RefCell::new(*self.sin_family.borrow())), - sin_port: Rc::new(RefCell::new(*self.sin_port.borrow())), - sin_addr: Rc::new(RefCell::new(self.sin_addr.borrow().clone())), + sin_family: self.sin_family, + sin_port: self.sin_port, + sin_addr: self.sin_addr.clone(), sin_zero: Rc::new(RefCell::new(self.sin_zero.borrow().clone())), } } @@ -212,11 +236,11 @@ impl Clone for SockaddrIn { impl Clone for SockaddrIn6 { fn clone(&self) -> Self { Self { - sin6_family: Rc::new(RefCell::new(*self.sin6_family.borrow())), - sin6_port: Rc::new(RefCell::new(*self.sin6_port.borrow())), - sin6_flowinfo: Rc::new(RefCell::new(*self.sin6_flowinfo.borrow())), - sin6_addr: Rc::new(RefCell::new(self.sin6_addr.borrow().clone())), - sin6_scope_id: Rc::new(RefCell::new(*self.sin6_scope_id.borrow())), + sin6_family: self.sin6_family, + sin6_port: self.sin6_port, + sin6_flowinfo: self.sin6_flowinfo, + sin6_addr: self.sin6_addr.clone(), + sin6_scope_id: self.sin6_scope_id, } } } @@ -224,7 +248,7 @@ impl Clone for SockaddrIn6 { impl Clone for SockaddrUn { fn clone(&self) -> Self { Self { - sun_family: Rc::new(RefCell::new(*self.sun_family.borrow())), + sun_family: self.sun_family, sun_path: Rc::new(RefCell::new(self.sun_path.borrow().clone())), } } @@ -233,7 +257,7 @@ impl Clone for SockaddrUn { impl Clone for SockaddrStorage { fn clone(&self) -> Self { Self { - ss_family: Rc::new(RefCell::new(*self.ss_family.borrow())), + ss_family: self.ss_family, __pad: Rc::new(RefCell::new(self.__pad.borrow().clone())), } } @@ -244,12 +268,12 @@ impl ByteRepr for Sockaddr { 16 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.sa_family.borrow()).to_bytes(&mut buf[0..2]); + self.sa_family.to_bytes(&mut buf[0..2]); buf[2..16].copy_from_slice(&self.sa_data.borrow()); } fn from_bytes(buf: &[u8]) -> Self { Self { - sa_family: Rc::new(RefCell::new(::from_bytes(&buf[0..2]))), + sa_family: ::from_bytes(&buf[0..2]), sa_data: Rc::new(RefCell::new(buf[2..16].to_vec().into_boxed_slice())), } } @@ -260,16 +284,16 @@ impl ByteRepr for SockaddrIn { 16 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.sin_family.borrow()).to_bytes(&mut buf[0..2]); - (*self.sin_port.borrow()).to_bytes(&mut buf[2..4]); - (*self.sin_addr.borrow()).to_bytes(&mut buf[4..8]); + self.sin_family.to_bytes(&mut buf[0..2]); + self.sin_port.to_bytes(&mut buf[2..4]); + self.sin_addr.to_bytes(&mut buf[4..8]); buf[8..16].copy_from_slice(&self.sin_zero.borrow()); } fn from_bytes(buf: &[u8]) -> Self { Self { - sin_family: Rc::new(RefCell::new(::from_bytes(&buf[0..2]))), - sin_port: Rc::new(RefCell::new(::from_bytes(&buf[2..4]))), - sin_addr: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + sin_family: ::from_bytes(&buf[0..2]), + sin_port: ::from_bytes(&buf[2..4]), + sin_addr: ::from_bytes(&buf[4..8]), sin_zero: Rc::new(RefCell::new(buf[8..16].to_vec().into_boxed_slice())), } } @@ -280,19 +304,19 @@ impl ByteRepr for SockaddrIn6 { 28 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.sin6_family.borrow()).to_bytes(&mut buf[0..2]); - (*self.sin6_port.borrow()).to_bytes(&mut buf[2..4]); - (*self.sin6_flowinfo.borrow()).to_bytes(&mut buf[4..8]); - (*self.sin6_addr.borrow()).to_bytes(&mut buf[8..24]); - (*self.sin6_scope_id.borrow()).to_bytes(&mut buf[24..28]); + self.sin6_family.to_bytes(&mut buf[0..2]); + self.sin6_port.to_bytes(&mut buf[2..4]); + self.sin6_flowinfo.to_bytes(&mut buf[4..8]); + self.sin6_addr.to_bytes(&mut buf[8..24]); + self.sin6_scope_id.to_bytes(&mut buf[24..28]); } fn from_bytes(buf: &[u8]) -> Self { Self { - sin6_family: Rc::new(RefCell::new(::from_bytes(&buf[0..2]))), - sin6_port: Rc::new(RefCell::new(::from_bytes(&buf[2..4]))), - sin6_flowinfo: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), - sin6_addr: Rc::new(RefCell::new(::from_bytes(&buf[8..24]))), - sin6_scope_id: Rc::new(RefCell::new(::from_bytes(&buf[24..28]))), + sin6_family: ::from_bytes(&buf[0..2]), + sin6_port: ::from_bytes(&buf[2..4]), + sin6_flowinfo: ::from_bytes(&buf[4..8]), + sin6_addr: ::from_bytes(&buf[8..24]), + sin6_scope_id: ::from_bytes(&buf[24..28]), } } } @@ -302,12 +326,12 @@ impl ByteRepr for SockaddrUn { 110 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.sun_family.borrow()).to_bytes(&mut buf[0..2]); + self.sun_family.to_bytes(&mut buf[0..2]); buf[2..110].copy_from_slice(&self.sun_path.borrow()); } fn from_bytes(buf: &[u8]) -> Self { Self { - sun_family: Rc::new(RefCell::new(::from_bytes(&buf[0..2]))), + sun_family: ::from_bytes(&buf[0..2]), sun_path: Rc::new(RefCell::new(buf[2..110].to_vec().into_boxed_slice())), } } @@ -318,12 +342,12 @@ impl ByteRepr for SockaddrStorage { 128 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.ss_family.borrow()).to_bytes(&mut buf[0..2]); + self.ss_family.to_bytes(&mut buf[0..2]); buf[2..128].copy_from_slice(&self.__pad.borrow()); } fn from_bytes(buf: &[u8]) -> Self { Self { - ss_family: Rc::new(RefCell::new(::from_bytes(&buf[0..2]))), + ss_family: ::from_bytes(&buf[0..2]), __pad: Rc::new(RefCell::new(buf[2..128].to_vec().into_boxed_slice())), } } diff --git a/rules/stat/shim.rs b/rules/stat/shim.rs index 792bff4900..60261f8b10 100644 --- a/rules/stat/shim.rs +++ b/rules/stat/shim.rs @@ -1,68 +1,64 @@ // Copyright (c) 2022-present INESC-ID. // Distributed under the MIT license that can be found in the LICENSE file. -use crate::{ByteRepr, Value}; -use std::cell::RefCell; -use std::rc::Rc; +use crate::{ByteRepr, Record}; +use std::mem::{offset_of, size_of}; -#[derive(Default)] +#[derive(Clone, Default, Record)] pub struct Stat { - pub st_dev: Value, - pub st_ino: Value, - pub st_nlink: Value, - pub st_mode: Value, - pub st_uid: Value, - pub st_gid: Value, - pub st_rdev: Value, - pub st_size: Value, - pub st_blksize: Value, - pub st_blocks: Value, - pub st_atime: Value, - pub st_mtime: Value, - pub st_ctime: Value, + #[offset(offset_of!(::libc::stat, st_dev))] + pub st_dev: u64, + #[offset(offset_of!(::libc::stat, st_ino))] + pub st_ino: u64, + #[offset(offset_of!(::libc::stat, st_nlink))] + pub st_nlink: u64, + #[offset(offset_of!(::libc::stat, st_mode))] + pub st_mode: u32, + #[offset(offset_of!(::libc::stat, st_uid))] + pub st_uid: u32, + #[offset(offset_of!(::libc::stat, st_gid))] + pub st_gid: u32, + #[offset(offset_of!(::libc::stat, st_rdev))] + pub st_rdev: u64, + #[offset(offset_of!(::libc::stat, st_size))] + pub st_size: i64, + #[offset(offset_of!(::libc::stat, st_blksize))] + pub st_blksize: i64, + #[offset(offset_of!(::libc::stat, st_blocks))] + pub st_blocks: i64, + #[offset(offset_of!(::libc::stat, st_atime))] + pub st_atime: i64, + #[offset(offset_of!(::libc::stat, st_mtime))] + pub st_mtime: i64, + #[offset(offset_of!(::libc::stat, st_ctime))] + pub st_ctime: i64, } impl Stat { #[allow(clippy::unnecessary_cast)] pub fn from_libc(s: &::libc::stat) -> Self { Self { - st_dev: Rc::new(RefCell::new(s.st_dev as u64)), - st_ino: Rc::new(RefCell::new(s.st_ino as u64)), - st_nlink: Rc::new(RefCell::new(s.st_nlink as u64)), - st_mode: Rc::new(RefCell::new(s.st_mode as u32)), - st_uid: Rc::new(RefCell::new(s.st_uid)), - st_gid: Rc::new(RefCell::new(s.st_gid)), - st_rdev: Rc::new(RefCell::new(s.st_rdev as u64)), - st_size: Rc::new(RefCell::new(s.st_size as i64)), - st_blksize: Rc::new(RefCell::new(s.st_blksize as i64)), - st_blocks: Rc::new(RefCell::new(s.st_blocks as i64)), - st_atime: Rc::new(RefCell::new(s.st_atime as i64)), - st_mtime: Rc::new(RefCell::new(s.st_mtime as i64)), - st_ctime: Rc::new(RefCell::new(s.st_ctime as i64)), + st_dev: s.st_dev as u64, + st_ino: s.st_ino as u64, + st_nlink: s.st_nlink as u64, + st_mode: s.st_mode as u32, + st_uid: s.st_uid, + st_gid: s.st_gid, + st_rdev: s.st_rdev as u64, + st_size: s.st_size as i64, + st_blksize: s.st_blksize as i64, + st_blocks: s.st_blocks as i64, + st_atime: s.st_atime as i64, + st_mtime: s.st_mtime as i64, + st_ctime: s.st_ctime as i64, } } } -impl Clone for Stat { - fn clone(&self) -> Self { - Self { - st_dev: Rc::new(RefCell::new(*self.st_dev.borrow())), - st_ino: Rc::new(RefCell::new(*self.st_ino.borrow())), - st_nlink: Rc::new(RefCell::new(*self.st_nlink.borrow())), - st_mode: Rc::new(RefCell::new(*self.st_mode.borrow())), - st_uid: Rc::new(RefCell::new(*self.st_uid.borrow())), - st_gid: Rc::new(RefCell::new(*self.st_gid.borrow())), - st_rdev: Rc::new(RefCell::new(*self.st_rdev.borrow())), - st_size: Rc::new(RefCell::new(*self.st_size.borrow())), - st_blksize: Rc::new(RefCell::new(*self.st_blksize.borrow())), - st_blocks: Rc::new(RefCell::new(*self.st_blocks.borrow())), - st_atime: Rc::new(RefCell::new(*self.st_atime.borrow())), - st_mtime: Rc::new(RefCell::new(*self.st_mtime.borrow())), - st_ctime: Rc::new(RefCell::new(*self.st_ctime.borrow())), - } +impl ByteRepr for Stat { + fn byte_size() -> usize { + size_of::<::libc::stat>() } } -impl ByteRepr for Stat {} - impl ByteRepr for ::libc::stat {} diff --git a/rules/termios/shim.rs b/rules/termios/shim.rs index 5d96f47435..bc85889585 100644 --- a/rules/termios/shim.rs +++ b/rules/termios/shim.rs @@ -1,32 +1,43 @@ // Copyright (c) 2022-present INESC-ID. // Distributed under the MIT license that can be found in the LICENSE file. -use crate::{ByteRepr, Value}; +use crate::{ByteRepr, Record, Value}; use std::cell::RefCell; +use std::mem::{offset_of, size_of}; use std::rc::Rc; +#[derive(Record)] pub struct Termios { - pub c_iflag: Value, - pub c_oflag: Value, - pub c_cflag: Value, - pub c_lflag: Value, - pub c_line: Value, + #[offset(offset_of!(::libc::termios, c_iflag))] + pub c_iflag: u32, + #[offset(offset_of!(::libc::termios, c_oflag))] + pub c_oflag: u32, + #[offset(offset_of!(::libc::termios, c_cflag))] + pub c_cflag: u32, + #[offset(offset_of!(::libc::termios, c_lflag))] + pub c_lflag: u32, + #[cfg_attr(target_os = "linux", offset(offset_of!(::libc::termios, c_line)))] + #[cfg_attr(target_os = "macos", offset(offset_of!(::libc::termios, c_cc)))] + pub c_line: u8, + #[offset(offset_of!(::libc::termios, c_cc))] pub c_cc: Value>, - pub c_ispeed: Value, - pub c_ospeed: Value, + #[offset(offset_of!(::libc::termios, c_ispeed))] + pub c_ispeed: u32, + #[offset(offset_of!(::libc::termios, c_ospeed))] + pub c_ospeed: u32, } impl Default for Termios { fn default() -> Self { Self { - c_iflag: Rc::new(RefCell::new(0)), - c_oflag: Rc::new(RefCell::new(0)), - c_cflag: Rc::new(RefCell::new(0)), - c_lflag: Rc::new(RefCell::new(0)), - c_line: Rc::new(RefCell::new(0)), + c_iflag: 0, + c_oflag: 0, + c_cflag: 0, + c_lflag: 0, + c_line: 0, c_cc: Rc::new(RefCell::new(vec![0u8; 32].into_boxed_slice())), - c_ispeed: Rc::new(RefCell::new(0)), - c_ospeed: Rc::new(RefCell::new(0)), + c_ispeed: 0, + c_ospeed: 0, } } } @@ -34,50 +45,48 @@ impl Default for Termios { impl Clone for Termios { fn clone(&self) -> Self { Self { - c_iflag: Rc::new(RefCell::new(*self.c_iflag.borrow())), - c_oflag: Rc::new(RefCell::new(*self.c_oflag.borrow())), - c_cflag: Rc::new(RefCell::new(*self.c_cflag.borrow())), - c_lflag: Rc::new(RefCell::new(*self.c_lflag.borrow())), - c_line: Rc::new(RefCell::new(*self.c_line.borrow())), c_cc: Rc::new(RefCell::new(self.c_cc.borrow().clone())), - c_ispeed: Rc::new(RefCell::new(*self.c_ispeed.borrow())), - c_ospeed: Rc::new(RefCell::new(*self.c_ospeed.borrow())), + ..*self } } } -impl ByteRepr for Termios {} +impl ByteRepr for Termios { + fn byte_size() -> usize { + size_of::<::libc::termios>() + } +} impl Termios { #[allow(clippy::unnecessary_cast)] pub fn from_libc(t: &::libc::termios) -> Self { - let s = Self::default(); - *s.c_iflag.borrow_mut() = t.c_iflag as u32; - *s.c_oflag.borrow_mut() = t.c_oflag as u32; - *s.c_cflag.borrow_mut() = t.c_cflag as u32; - *s.c_lflag.borrow_mut() = t.c_lflag as u32; - #[cfg(target_os = "linux")] - { - *s.c_line.borrow_mut() = t.c_line; - } + let s = Self { + c_iflag: t.c_iflag as u32, + c_oflag: t.c_oflag as u32, + c_cflag: t.c_cflag as u32, + c_lflag: t.c_lflag as u32, + #[cfg(target_os = "linux")] + c_line: t.c_line, + c_ispeed: t.c_ispeed as u32, + c_ospeed: t.c_ospeed as u32, + ..Self::default() + }; { let mut cc = s.c_cc.borrow_mut(); let n = t.c_cc.len().min(cc.len()); cc[..n].copy_from_slice(&t.c_cc[..n]); } - *s.c_ispeed.borrow_mut() = t.c_ispeed as u32; - *s.c_ospeed.borrow_mut() = t.c_ospeed as u32; s } #[cfg(target_os = "linux")] pub fn to_libc(&self) -> ::libc::termios { ::libc::termios { - c_iflag: *self.c_iflag.borrow(), - c_oflag: *self.c_oflag.borrow(), - c_cflag: *self.c_cflag.borrow(), - c_lflag: *self.c_lflag.borrow(), - c_line: *self.c_line.borrow(), + c_iflag: self.c_iflag, + c_oflag: self.c_oflag, + c_cflag: self.c_cflag, + c_lflag: self.c_lflag, + c_line: self.c_line, c_cc: { let mut cc = [0u8; 32]; let src = self.c_cc.borrow(); @@ -85,18 +94,18 @@ impl Termios { cc[..n].copy_from_slice(&src[..n]); cc }, - c_ispeed: *self.c_ispeed.borrow(), - c_ospeed: *self.c_ospeed.borrow(), + c_ispeed: self.c_ispeed, + c_ospeed: self.c_ospeed, } } #[cfg(target_os = "macos")] pub fn to_libc(&self) -> ::libc::termios { ::libc::termios { - c_iflag: *self.c_iflag.borrow() as u64, - c_oflag: *self.c_oflag.borrow() as u64, - c_cflag: *self.c_cflag.borrow() as u64, - c_lflag: *self.c_lflag.borrow() as u64, + c_iflag: self.c_iflag as u64, + c_oflag: self.c_oflag as u64, + c_cflag: self.c_cflag as u64, + c_lflag: self.c_lflag as u64, c_cc: { let mut cc = [0u8; 20]; let src = self.c_cc.borrow(); @@ -104,29 +113,26 @@ impl Termios { cc[..n].copy_from_slice(&src[..n]); cc }, - c_ispeed: *self.c_ispeed.borrow() as u64, - c_ospeed: *self.c_ospeed.borrow() as u64, + c_ispeed: self.c_ispeed as u64, + c_ospeed: self.c_ospeed as u64, } } } -#[derive(Default)] +#[derive(Clone, Default, Record)] pub struct Winsize { - pub ws_row: Value, - pub ws_col: Value, - pub ws_xpixel: Value, - pub ws_ypixel: Value, + #[offset(offset_of!(::libc::winsize, ws_row))] + pub ws_row: u16, + #[offset(offset_of!(::libc::winsize, ws_col))] + pub ws_col: u16, + #[offset(offset_of!(::libc::winsize, ws_xpixel))] + pub ws_xpixel: u16, + #[offset(offset_of!(::libc::winsize, ws_ypixel))] + pub ws_ypixel: u16, } -impl Clone for Winsize { - fn clone(&self) -> Self { - Self { - ws_row: Rc::new(RefCell::new(*self.ws_row.borrow())), - ws_col: Rc::new(RefCell::new(*self.ws_col.borrow())), - ws_xpixel: Rc::new(RefCell::new(*self.ws_xpixel.borrow())), - ws_ypixel: Rc::new(RefCell::new(*self.ws_ypixel.borrow())), - } +impl ByteRepr for Winsize { + fn byte_size() -> usize { + size_of::<::libc::winsize>() } } - -impl ByteRepr for Winsize {} diff --git a/rules/time/shim.rs b/rules/time/shim.rs index e53a896dee..6de9eeeabd 100644 --- a/rules/time/shim.rs +++ b/rules/time/shim.rs @@ -1,92 +1,81 @@ // Copyright (c) 2022-present INESC-ID. // Distributed under the MIT license that can be found in the LICENSE file. -use crate::{ByteRepr, Ptr, Value}; -use std::cell::RefCell; -use std::rc::Rc; +use crate::{ByteRepr, Ptr, Record}; +use std::mem::{offset_of, size_of}; -#[derive(Default)] +#[derive(Clone, Default, Record)] pub struct Tm { - pub tm_sec: Value, - pub tm_min: Value, - pub tm_hour: Value, - pub tm_mday: Value, - pub tm_mon: Value, - pub tm_year: Value, - pub tm_wday: Value, - pub tm_yday: Value, - pub tm_isdst: Value, - pub tm_gmtoff: Value, - pub tm_zone: Value>, + #[offset(offset_of!(::libc::tm, tm_sec))] + pub tm_sec: i32, + #[offset(offset_of!(::libc::tm, tm_min))] + pub tm_min: i32, + #[offset(offset_of!(::libc::tm, tm_hour))] + pub tm_hour: i32, + #[offset(offset_of!(::libc::tm, tm_mday))] + pub tm_mday: i32, + #[offset(offset_of!(::libc::tm, tm_mon))] + pub tm_mon: i32, + #[offset(offset_of!(::libc::tm, tm_year))] + pub tm_year: i32, + #[offset(offset_of!(::libc::tm, tm_wday))] + pub tm_wday: i32, + #[offset(offset_of!(::libc::tm, tm_yday))] + pub tm_yday: i32, + #[offset(offset_of!(::libc::tm, tm_isdst))] + pub tm_isdst: i32, + #[offset(offset_of!(::libc::tm, tm_gmtoff))] + pub tm_gmtoff: i64, + #[offset(offset_of!(::libc::tm, tm_zone))] + pub tm_zone: Ptr, } impl Tm { pub fn from_zoned(dt: &jiff::Zoned) -> Self { - let tm = Tm::default(); - *tm.tm_sec.borrow_mut() = dt.second() as i32; - *tm.tm_min.borrow_mut() = dt.minute() as i32; - *tm.tm_hour.borrow_mut() = dt.hour() as i32; - *tm.tm_mday.borrow_mut() = dt.day() as i32; - *tm.tm_mon.borrow_mut() = dt.month() as i32 - 1; - *tm.tm_year.borrow_mut() = dt.year() as i32 - 1900; - *tm.tm_wday.borrow_mut() = dt.weekday().to_sunday_zero_offset() as i32; - *tm.tm_yday.borrow_mut() = dt.day_of_year() as i32 - 1; - *tm.tm_isdst.borrow_mut() = 0; - *tm.tm_gmtoff.borrow_mut() = dt.offset().seconds() as i64; #[cfg(target_os = "linux")] let zone: &'static [u8] = b"GMT"; #[cfg(target_os = "macos")] let zone: &'static [u8] = b"UTC"; - *tm.tm_zone.borrow_mut() = Ptr::::from_string_literal(zone); - tm + Tm { + tm_sec: dt.second() as i32, + tm_min: dt.minute() as i32, + tm_hour: dt.hour() as i32, + tm_mday: dt.day() as i32, + tm_mon: dt.month() as i32 - 1, + tm_year: dt.year() as i32 - 1900, + tm_wday: dt.weekday().to_sunday_zero_offset() as i32, + tm_yday: dt.day_of_year() as i32 - 1, + tm_isdst: 0, + tm_gmtoff: dt.offset().seconds() as i64, + tm_zone: Ptr::::from_string_literal(zone), + } } pub fn to_civil(&self) -> Result { jiff::civil::DateTime::new( - (*self.tm_year.borrow() + 1900) as i16, - (*self.tm_mon.borrow() + 1) as i8, - *self.tm_mday.borrow() as i8, - *self.tm_hour.borrow() as i8, - *self.tm_min.borrow() as i8, - *self.tm_sec.borrow() as i8, + (self.tm_year + 1900) as i16, + (self.tm_mon + 1) as i8, + self.tm_mday as i8, + self.tm_hour as i8, + self.tm_min as i8, + self.tm_sec as i8, 0, ) } } -impl Clone for Tm { - fn clone(&self) -> Self { - Self { - tm_sec: Rc::new(RefCell::new(*self.tm_sec.borrow())), - tm_min: Rc::new(RefCell::new(*self.tm_min.borrow())), - tm_hour: Rc::new(RefCell::new(*self.tm_hour.borrow())), - tm_mday: Rc::new(RefCell::new(*self.tm_mday.borrow())), - tm_mon: Rc::new(RefCell::new(*self.tm_mon.borrow())), - tm_year: Rc::new(RefCell::new(*self.tm_year.borrow())), - tm_wday: Rc::new(RefCell::new(*self.tm_wday.borrow())), - tm_yday: Rc::new(RefCell::new(*self.tm_yday.borrow())), - tm_isdst: Rc::new(RefCell::new(*self.tm_isdst.borrow())), - tm_gmtoff: Rc::new(RefCell::new(*self.tm_gmtoff.borrow())), - tm_zone: Rc::new(RefCell::new(self.tm_zone.borrow().clone())), - } +impl ByteRepr for Tm { + fn byte_size() -> usize { + size_of::<::libc::tm>() } } -impl ByteRepr for Tm {} - -#[derive(Default)] +#[derive(Clone, Default, Record)] pub struct Timeval { - pub tv_sec: Value, - pub tv_usec: Value, -} - -impl Clone for Timeval { - fn clone(&self) -> Self { - Self { - tv_sec: Rc::new(RefCell::new(*self.tv_sec.borrow())), - tv_usec: Rc::new(RefCell::new(*self.tv_usec.borrow())), - } - } + #[offset(offset_of!(::libc::timeval, tv_sec))] + pub tv_sec: i64, + #[offset(offset_of!(::libc::timeval, tv_usec))] + pub tv_usec: i64, } impl ByteRepr for Timeval { @@ -95,35 +84,32 @@ impl ByteRepr for Timeval { } fn to_bytes(&self, buf: &mut [u8]) { - (*self.tv_sec.borrow()).to_bytes(&mut buf[0..8]); - (*self.tv_usec.borrow()).to_bytes(&mut buf[8..16]); + self.tv_sec.to_bytes(&mut buf[0..8]); + self.tv_usec.to_bytes(&mut buf[8..16]); } fn from_bytes(buf: &[u8]) -> Self { Self { - tv_sec: Rc::new(RefCell::new(i64::from_bytes(&buf[0..8]))), - tv_usec: Rc::new(RefCell::new(i64::from_bytes(&buf[8..16]))), + tv_sec: i64::from_bytes(&buf[0..8]), + tv_usec: i64::from_bytes(&buf[8..16]), } } } -#[derive(Default)] +#[derive(Clone, Default, Record)] pub struct Timespec { - pub tv_sec: Value, - pub tv_nsec: Value, + #[offset(offset_of!(::libc::timespec, tv_sec))] + pub tv_sec: i64, + #[offset(offset_of!(::libc::timespec, tv_nsec))] + pub tv_nsec: i64, } -impl Clone for Timespec { - fn clone(&self) -> Self { - Self { - tv_sec: Rc::new(RefCell::new(*self.tv_sec.borrow())), - tv_nsec: Rc::new(RefCell::new(*self.tv_nsec.borrow())), - } +impl ByteRepr for Timespec { + fn byte_size() -> usize { + size_of::<::libc::timespec>() } } -impl ByteRepr for Timespec {} - impl ByteRepr for ::libc::tm {} impl ByteRepr for ::libc::timeval {} impl ByteRepr for ::libc::timespec {} diff --git a/rules/time/tgt_refcount.rs b/rules/time/tgt_refcount.rs index 6d6cfae5f1..4a9e15f295 100644 --- a/rules/time/tgt_refcount.rs +++ b/rules/time/tgt_refcount.rs @@ -36,8 +36,8 @@ fn f2(a0: nix::time::ClockId, a1: Ptr) -> i32 { match nix::time::clock_gettime(a0) { Ok(__ts) => { a1.with_mut(|__t| { - *__t.tv_sec.borrow_mut() = __ts.tv_sec() as i64; - *__t.tv_nsec.borrow_mut() = __ts.tv_nsec() as i64; + __t.tv_sec = __ts.tv_sec() as i64; + __t.tv_nsec = __ts.tv_nsec() as i64; }); 0 } @@ -73,8 +73,8 @@ fn f5(a0: Ptr<::libc::time_t>, a1: Ptr) -> Ptr { let __isdst = if __info.dst().is_dst() { 1 } else { 0 }; __res.with_mut(|__tm| { *__tm = Tm::from_zoned(&__dt); - *__tm.tm_isdst.borrow_mut() = __isdst; - *__tm.tm_zone.borrow_mut() = Ptr::alloc_array(__zone.into_boxed_slice()); + __tm.tm_isdst = __isdst; + __tm.tm_zone = Ptr::alloc_array(__zone.into_boxed_slice()); }); __res } @@ -108,14 +108,14 @@ fn f7(a0: Ptr, a1: Ptr) -> i32 { let __times = a1; let __at = __times.with(|__tv| { nix::sys::time::TimeVal::new( - *__tv.tv_sec.borrow() as ::libc::time_t, - *__tv.tv_usec.borrow() as ::libc::suseconds_t, + __tv.tv_sec as ::libc::time_t, + __tv.tv_usec as ::libc::suseconds_t, ) }); let __mt = __times.offset(1).with(|__tv| { nix::sys::time::TimeVal::new( - *__tv.tv_sec.borrow() as ::libc::time_t, - *__tv.tv_usec.borrow() as ::libc::suseconds_t, + __tv.tv_sec as ::libc::time_t, + __tv.tv_usec as ::libc::suseconds_t, ) }); match nix::sys::stat::utimes(a0.to_rust_string().as_str(), &__at, &__mt) { @@ -135,8 +135,8 @@ fn f8(a0: Ptr, a1: Ptr<::libc::timezone>) -> i32 { match nix::time::clock_gettime(nix::time::ClockId::CLOCK_REALTIME) { Ok(__ts) => { a0.with_mut(|__tv| { - *__tv.tv_sec.borrow_mut() = __ts.tv_sec() as i64; - *__tv.tv_usec.borrow_mut() = (__ts.tv_nsec() / 1000) as i64; + __tv.tv_sec = __ts.tv_sec() as i64; + __tv.tv_usec = (__ts.tv_nsec() / 1000) as i64; }); 0 } @@ -155,8 +155,8 @@ fn f8(a0: Ptr, a1: AnyPtr) -> i32 { match nix::time::clock_gettime(nix::time::ClockId::CLOCK_REALTIME) { Ok(__ts) => { a0.with_mut(|__tv| { - *__tv.tv_sec.borrow_mut() = __ts.tv_sec() as i64; - *__tv.tv_usec.borrow_mut() = (__ts.tv_nsec() / 1000) as i64; + __tv.tv_sec = __ts.tv_sec() as i64; + __tv.tv_usec = (__ts.tv_nsec() / 1000) as i64; }); 0 } diff --git a/rules/unique_ptr/tgt_refcount.rs b/rules/unique_ptr/tgt_refcount.rs index 05c5a1d604..46600c01d3 100644 --- a/rules/unique_ptr/tgt_refcount.rs +++ b/rules/unique_ptr/tgt_refcount.rs @@ -31,13 +31,13 @@ fn f4(a0: Ptr) -> Option> { a0.to_owned_opt() } -fn f5(a0: &mut Option>, a1: Ptr) { +fn f5(a0: Ptr>>, a1: Ptr) { let _p: Ptr<_> = a1; - *a0 = _p.to_owned_opt() + a0.write(_p.to_owned_opt()) } -fn f6(a0: &mut Option>>, a1: Ptr) { - *a0 = a1.to_owned_opt() +fn f6(a0: Ptr>>>, a1: Ptr) { + a0.write(a1.to_owned_opt()) } fn f7(a0: Value) -> Ptr { @@ -48,8 +48,8 @@ fn f8(init: T1) -> Option> { Some(Rc::new(RefCell::new(init))) } -fn f9(a0: &mut Option>>) { - *a0 = None +fn f9(a0: Ptr>>>) { + a0.write(None) } fn f10() -> Option> { diff --git a/tests/multi-file/defaulted_move_cross_tu/out/refcount/defaulted_move_cross_tu.rs b/tests/multi-file/defaulted_move_cross_tu/out/refcount/defaulted_move_cross_tu.rs index c05f4b7080..afbb21b457 100644 --- a/tests/multi-file/defaulted_move_cross_tu/out/refcount/defaulted_move_cross_tu.rs +++ b/tests/multi-file/defaulted_move_cross_tu/out/refcount/defaulted_move_cross_tu.rs @@ -28,13 +28,11 @@ impl S { } pub fn move_from(_a0: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - v: { - Rc::new(RefCell::new( - _a0.with(|__s: &S| std::mem::take(&mut (*__s.v.borrow_mut()))), - )) - }, + v: Rc::new(RefCell::new(std::mem::take( + &mut (*{ (*_a0.upgrade().deref()).v.clone() }.borrow_mut()), + ))), n: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 2, _>( - |__i: usize| (*_a0.with(|__s: &S| __s.n.clone()).borrow())[(__i) as usize], + |__i: usize| (*{ (*_a0.upgrade().deref()).n.clone() }.borrow())[(__i) as usize], )))), })); let this: Ptr = __this.as_pointer(); @@ -71,10 +69,10 @@ impl ByteRepr for S { pub fn sum_0(s: Ptr) -> i32 { return { let _lhs = { - let _lhs = (s.with(|__s: &S| (*__s.v.borrow()).len()) as i32); - _lhs + s.with(|__s: &S| (*__s.n.borrow())[(0) as usize]) + let _lhs = ((*s.with(|__s| __s.v.clone()).borrow()).len() as i32); + _lhs + (*s.with(|__s| __s.n.clone()).borrow())[(0) as usize] }; - _lhs + s.with(|__s: &S| (*__s.n.borrow())[(1) as usize]) + _lhs + (*s.with(|__s| __s.n.clone()).borrow())[(1) as usize] }; } pub fn main() { @@ -91,10 +89,10 @@ pub fn shuffle_1(x: i32) -> i32 { let x: Value = Rc::new(RefCell::new(x)); let a: Value = Rc::new(RefCell::new(S::new({ (*x.borrow()) }))); let b: Value = Rc::new(RefCell::new(S::move_from({ a.as_pointer() }))); - assert!((*(*a.borrow()).v.borrow()).is_empty()); + assert!((*{ (*a.borrow()).v.clone() }.borrow()).is_empty()); let c: Value = Rc::new(RefCell::new(S::new({ 1 }))); ({ SImpl::move_assign(&c.as_pointer(), b.as_pointer()) }); - assert!((*(*b.borrow()).v.borrow()).is_empty()); + assert!((*{ (*b.borrow()).v.clone() }.borrow()).is_empty()); return ({ sum_0(c.as_pointer()) }); } pub trait SImpl { @@ -102,16 +100,17 @@ pub trait SImpl { } impl SImpl for Ptr { fn move_assign(&self, _a0: Ptr) -> Ptr { - ((*self).with(|__s: &S| __s.v.as_pointer()) as Ptr>) - .write(_a0.with(|__s: &S| std::mem::take(&mut (*__s.v.borrow_mut())))); + ((*self).with(|__s| __s.v.clone()).as_pointer() as Ptr>).write(std::mem::take( + &mut (*{ (*_a0.upgrade().deref()).v.clone() }.borrow_mut()), + )); { - (((*self).with(|__s: &S| __s.n.as_pointer())) as Ptr) + (((*self).with(|__s| __s.n.clone()).as_pointer()) as Ptr) .to_any() .memcpy( - &((_a0.with(|__s: &S| __s.n.as_pointer())) as Ptr).to_any(), + &(({ (*_a0.upgrade().deref()).n.clone() }.as_pointer()) as Ptr).to_any(), 8_usize as usize, ); - (((*self).with(|__s: &S| __s.n.as_pointer())) as Ptr).to_any() + (((*self).with(|__s| __s.n.clone()).as_pointer()) as Ptr).to_any() }; return (*self).clone(); } diff --git a/tests/multi-file/friend_operator/out/refcount/friend_operator.rs b/tests/multi-file/friend_operator/out/refcount/friend_operator.rs index c8e5156df8..ce0d422d3d 100644 --- a/tests/multi-file/friend_operator/out/refcount/friend_operator.rs +++ b/tests/multi-file/friend_operator/out/refcount/friend_operator.rs @@ -73,11 +73,11 @@ fn main_0() -> i32 { } pub fn operator_eq_0(x: Ptr, y: Ptr) -> bool { return ({ - let _lhs = x.with(|__s: &S| __s.a); - _lhs == y.with(|__s: &S| __s.a) + let _lhs = x.with(|__s| __s.a); + _lhs == y.with(|__s| __s.a) }) && ({ - let _lhs = x.with(|__s: &S| __s.b); - _lhs == y.with(|__s: &S| __s.b) + let _lhs = x.with(|__s| __s.b); + _lhs == y.with(|__s| __s.b) }); } pub fn operator_ne_1(x: Ptr, y: Ptr) -> bool { @@ -89,14 +89,14 @@ pub fn operator_ne_1(x: Ptr, y: Ptr) -> bool { } pub fn operator_lt_2(x: Ptr, y: Ptr) -> bool { return ({ - let _lhs = x.with(|__s: &S| __s.a); - _lhs < y.with(|__s: &S| __s.a) + let _lhs = x.with(|__s| __s.a); + _lhs < y.with(|__s| __s.a) }) || (({ - let _lhs = x.with(|__s: &S| __s.a); - _lhs == y.with(|__s: &S| __s.a) + let _lhs = x.with(|__s| __s.a); + _lhs == y.with(|__s| __s.a) }) && ({ - let _lhs = x.with(|__s: &S| __s.b); - _lhs < y.with(|__s: &S| __s.b) + let _lhs = x.with(|__s| __s.b); + _lhs < y.with(|__s| __s.b) })); } pub fn compare_3(x: Ptr, y: Ptr) -> i32 { diff --git a/tests/multi-file/opaque_forward_decl/out/refcount/opaque_forward_decl.rs b/tests/multi-file/opaque_forward_decl/out/refcount/opaque_forward_decl.rs index ecdb3b3920..eef549990e 100644 --- a/tests/multi-file/opaque_forward_decl/out/refcount/opaque_forward_decl.rs +++ b/tests/multi-file/opaque_forward_decl/out/refcount/opaque_forward_decl.rs @@ -44,7 +44,7 @@ fn main_0() -> i32 { } pub fn touch_0(c: Ptr) { let c: Value> = Rc::new(RefCell::new(c)); - &((*c.borrow()).with(|__s: &container| (__s.p).clone())); + &((*c.borrow()).with(|__s| __s.p.clone())); } #[derive(Clone, Copy, Default, ByteRepr, VaArg, FnPtrArg)] pub struct opaque; diff --git a/tests/multi-file/out_of_line_methods_cross_tu/out/refcount/out_of_line_methods_cross_tu.rs b/tests/multi-file/out_of_line_methods_cross_tu/out/refcount/out_of_line_methods_cross_tu.rs index f4ebd27970..364506b716 100644 --- a/tests/multi-file/out_of_line_methods_cross_tu/out/refcount/out_of_line_methods_cross_tu.rs +++ b/tests/multi-file/out_of_line_methods_cross_tu/out/refcount/out_of_line_methods_cross_tu.rs @@ -130,17 +130,20 @@ pub trait SImpl { } impl SImpl for Ptr { fn get(&self) -> i32 { - return (*self).with(|__s: &S| __s.v); + return (*self).with(|__s| __s.v); } fn destructor(&self) {} fn set(&self, x: i32) { let x: Value = Rc::new(RefCell::new(x)); - (*self).with_mut(|__s: &mut S| __s.v = (*x.borrow())); + field!((*self), v).write((*x.borrow())); } fn add(&self, x: i32) -> i32 { let x: Value = Rc::new(RefCell::new(x)); - (*self).with_mut(|__s: &mut S| __s.v += (*x.borrow())); - return (*self).with(|__s: &S| __s.v); + { + let _ptr = field!((*self), v); + _ptr.write(_ptr.read() + (*x.borrow())) + }; + return (*self).with(|__s| __s.v); } } pub fn __cpp2rust_init_globals() {} diff --git a/tests/multi-file/template_method_late_instantiation/out/refcount/template_method_late_instantiation.rs b/tests/multi-file/template_method_late_instantiation/out/refcount/template_method_late_instantiation.rs index fbf4a5391f..8b0d0e0bfe 100644 --- a/tests/multi-file/template_method_late_instantiation/out/refcount/template_method_late_instantiation.rs +++ b/tests/multi-file/template_method_late_instantiation/out/refcount/template_method_late_instantiation.rs @@ -52,10 +52,10 @@ pub trait S_int_Impl { impl S_int_Impl for Ptr { fn set(&self, v: i32) { let v: Value = Rc::new(RefCell::new(v)); - (*self).with_mut(|__s: &mut S_int_| __s.x = (*v.borrow())); + field!((*self), x).write((*v.borrow())); } fn get(&self) -> i32 { - return (*self).with(|__s: &S_int_| __s.x); + return (*self).with(|__s| __s.x); } } pub fn __cpp2rust_init_globals() {} diff --git a/tests/ub/out/refcount/ctor_ref_member.rs b/tests/ub/out/refcount/ctor_ref_member.rs index 9a83e9d133..2d1111e3f8 100644 --- a/tests/ub/out/refcount/ctor_ref_member.rs +++ b/tests/ub/out/refcount/ctor_ref_member.rs @@ -32,7 +32,7 @@ fn main_0() -> i32 { let __tmp_0: Value = Rc::new(RefCell::new(5)); S::new({ __tmp_0.as_pointer() }) })); - assert!((((*s.borrow()).r.read()) == 5)); + assert!((({ (*s.borrow()).r.clone() }.read()) == 5)); return 0; } pub fn __cpp2rust_init_globals() {} diff --git a/tests/ub/out/refcount/ub6.rs b/tests/ub/out/refcount/ub6.rs index 5a7a8c623a..7c7b4cec36 100644 --- a/tests/ub/out/refcount/ub6.rs +++ b/tests/ub/out/refcount/ub6.rs @@ -34,9 +34,9 @@ pub fn fill_1(arr: Ptr]>>>>, n1: Ptr) { }), )); (*arr.upgrade().deref()).as_ref().unwrap().borrow_mut()[(0_usize) as usize] = - ((*pair.borrow()).x1).clone(); + ({ (*pair.borrow()).x1.clone() }).clone(); (*arr.upgrade().deref()).as_ref().unwrap().borrow_mut()[(1_usize) as usize] = - ((*pair.borrow()).x2).clone(); + ({ (*pair.borrow()).x2.clone() }).clone(); } pub fn any_2(arr: Ptr]>>>>, n1: Ptr) -> bool { let out: Value = Rc::new(RefCell::new(false)); diff --git a/tests/unit/out/refcount/10_struct.rs b/tests/unit/out/refcount/10_struct.rs index 73e1080b28..12ab5cb701 100644 --- a/tests/unit/out/refcount/10_struct.rs +++ b/tests/unit/out/refcount/10_struct.rs @@ -151,36 +151,28 @@ impl GraphImpl for Ptr { let dst: Value = Rc::new(RefCell::new(dst)); let __rhs = Ptr::alloc(GraphNode { dst: (*dst.borrow()), - next: { - ((*self) - .with(|__s: &Graph| (__s.adj).clone()) - .offset((*src.borrow()) as isize) - .read()) - .clone() - }, - }); - { - (*self) - .with(|__s: &Graph| (__s.adj).clone()) + next: ((*self) + .with(|__s| __s.adj.clone()) .offset((*src.borrow()) as isize) - } - .write(__rhs); + .read()) + .clone(), + }); + (*self) + .with(|__s| __s.adj.clone()) + .offset((*src.borrow()) as isize) + .write(__rhs); let __rhs = Ptr::alloc(GraphNode { dst: (*src.borrow()), - next: { - ((*self) - .with(|__s: &Graph| (__s.adj).clone()) - .offset((*dst.borrow()) as isize) - .read()) - .clone() - }, - }); - { - (*self) - .with(|__s: &Graph| (__s.adj).clone()) + next: ((*self) + .with(|__s| __s.adj.clone()) .offset((*dst.borrow()) as isize) - } - .write(__rhs); + .read()) + .clone(), + }); + (*self) + .with(|__s| __s.adj.clone()) + .offset((*dst.borrow()) as isize) + .write(__rhs); } } pub trait PartialImpl { diff --git a/tests/unit/out/refcount/addr_of_global.rs b/tests/unit/out/refcount/addr_of_global.rs index 2c2412f028..301792e74e 100644 --- a/tests/unit/out/refcount/addr_of_global.rs +++ b/tests/unit/out/refcount/addr_of_global.rs @@ -72,7 +72,7 @@ fn main_0() -> i32 { let __idx = (0) as usize; items_3.with(|rc| rc.borrow()[__idx].clone()) }) - .with(|__s: &Inner| __s.value) + .with(|__s| __s.value) == 1) ); assert!( @@ -80,12 +80,10 @@ fn main_0() -> i32 { let __idx = (1) as usize; items_3.with(|rc| rc.borrow()[__idx].clone()) }) - .with(|__s: &Inner| __s.value) + .with(|__s| __s.value) == 2) ); - assert!( - ({ (*obj_4.with(Value::clone).borrow()).p.clone() }.with(|__s: &Inner| __s.value) == 42) - ); + assert!(({ (*obj_4.with(Value::clone).borrow()).p.clone() }.with(|__s| __s.value) == 42)); thread_local!( static cache_5: Value]>> = Rc::new(RefCell::new(Box::new([ (alpha_0.with(|v| v.as_pointer())), @@ -97,7 +95,7 @@ fn main_0() -> i32 { let __idx = (0) as usize; cache_5.with(|rc| rc.borrow()[__idx].clone()) }) - .with(|__s: &Inner| __s.value) + .with(|__s| __s.value) == 1) ); assert!( @@ -105,7 +103,7 @@ fn main_0() -> i32 { let __idx = (1) as usize; cache_5.with(|rc| rc.borrow()[__idx].clone()) }) - .with(|__s: &Inner| __s.value) + .with(|__s| __s.value) == 2) ); return 0; diff --git a/tests/unit/out/refcount/array_const_init.rs b/tests/unit/out/refcount/array_const_init.rs index 5dad1ee329..754e6c98a6 100644 --- a/tests/unit/out/refcount/array_const_init.rs +++ b/tests/unit/out/refcount/array_const_init.rs @@ -66,8 +66,9 @@ fn main_0() -> i32 { let i: Value = Rc::new(RefCell::new(0)); 'loop_: while ((((*i.borrow()) < 3) as i32) != 0) { assert!( - ((((*(*s_0.with(Value::clone).borrow()).tail.borrow())[(*i.borrow()) as usize] == 0) - as i32) + ((((*{ (*s_0.with(Value::clone).borrow()).tail.clone() }.borrow()) + [(*i.borrow()) as usize] + == 0) as i32) != 0) ); (*i.borrow_mut()).postfix_inc(); @@ -75,7 +76,8 @@ fn main_0() -> i32 { let i: Value = Rc::new(RefCell::new(0)); 'loop_: while ((((*i.borrow()) < 4) as i32) != 0) { assert!( - (((((*(*s_0.with(Value::clone).borrow()).buf.borrow())[(*i.borrow()) as usize] as i32) + (((((*{ (*s_0.with(Value::clone).borrow()).buf.clone() }.borrow()) + [(*i.borrow()) as usize] as i32) == 0) as i32) != 0) ); diff --git a/tests/unit/out/refcount/array_of_noncopy_struct.rs b/tests/unit/out/refcount/array_of_noncopy_struct.rs index aee01a2853..6496aba4cd 100644 --- a/tests/unit/out/refcount/array_of_noncopy_struct.rs +++ b/tests/unit/out/refcount/array_of_noncopy_struct.rs @@ -15,12 +15,10 @@ pub struct NonCopy { } impl Clone for NonCopy { fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - data: { Rc::new(RefCell::new((*self.data.borrow()).clone())) }, - tag: { self.tag }, - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() + Self { + data: Rc::new(RefCell::new((*self.data.borrow()).clone())), + tag: self.tag.clone(), + } } } impl Default for NonCopy { @@ -61,12 +59,17 @@ fn main_0() -> i32 { .collect::>(), )); (*arr.borrow_mut())[(0) as usize].tag = 7; - (*(*arr.borrow())[(1) as usize].data.borrow_mut()).push(42); + (*{ (*arr.borrow())[(1) as usize].data.clone() }.borrow_mut()).push(42); assert!(({ (*arr.borrow())[(0) as usize].tag } == 7)); - assert!(((*(*arr.borrow())[(1) as usize].data.borrow()).len() == 1_usize)); - assert!(({ (*(*arr.borrow())[(1) as usize].data.borrow())[(0_usize) as usize] } == 42)); + assert!(((*{ (*arr.borrow())[(1) as usize].data.clone() }.borrow()).len() == 1_usize)); + assert!( + ((({ (*arr.borrow())[(1) as usize].data.clone() }.as_pointer() as Ptr) + .offset(0_usize) + .read()) + == 42) + ); assert!(({ (*arr.borrow())[(2) as usize].tag } == 0)); - assert!(((*(*arr.borrow())[(2) as usize].data.borrow()).len() == 0_usize)); + assert!(((*{ (*arr.borrow())[(2) as usize].data.clone() }.borrow()).len() == 0_usize)); return 0; } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/array_reference.rs b/tests/unit/out/refcount/array_reference.rs index b14ce1b961..58a5dc0e15 100644 --- a/tests/unit/out/refcount/array_reference.rs +++ b/tests/unit/out/refcount/array_reference.rs @@ -96,20 +96,24 @@ pub fn sum_points_11(p: Ptr) -> i32 { return { let _lhs = { let _lhs = { - let _lhs = (p).offset((0) as isize).with(|__s: &Point| __s.x); - _lhs + (p).offset((0) as isize).with(|__s: &Point| __s.y) + let _lhs = { (*(p).offset((0) as isize).upgrade().deref()).x }; + _lhs + { (*(p).offset((0) as isize).upgrade().deref()).y } }; - _lhs + (p).offset((1) as isize).with(|__s: &Point| __s.x) + _lhs + { (*(p).offset((1) as isize).upgrade().deref()).x } }; - _lhs + (p).offset((1) as isize).with(|__s: &Point| __s.y) + _lhs + { (*(p).offset((1) as isize).upgrade().deref()).y } }; } pub fn shift_points_12(p: Ptr, d: i32) { let d: Value = Rc::new(RefCell::new(d)); - (p).offset((0) as isize) - .with_mut(|__s: &mut Point| __s.x += (*d.borrow())); - (p).offset((1) as isize) - .with_mut(|__s: &mut Point| __s.y += (*d.borrow())); + { + let _ptr = field!((p).offset((0) as isize), x); + _ptr.write(_ptr.read() + (*d.borrow())) + }; + { + let _ptr = field!((p).offset((1) as isize), y); + _ptr.write(_ptr.read() + (*d.borrow())) + }; } pub fn total_len_13(names: Ptr>) -> i32 { return (({ len5_1(((names).offset((0) as isize).read()).clone()) }) diff --git a/tests/unit/out/refcount/as_const.rs b/tests/unit/out/refcount/as_const.rs index ee10107516..f06629bc69 100644 --- a/tests/unit/out/refcount/as_const.rs +++ b/tests/unit/out/refcount/as_const.rs @@ -48,9 +48,9 @@ fn main_0() -> i32 { assert!(((({ SImpl::value_ref_4(&s.as_pointer(),) }).read()) == 9)); let cs: Ptr = s.as_pointer(); assert!(((({ SImpl::f_2(&cs,) }) as i32) == (Overload_kConstOverload as i32))); - assert!((cs.with(|__s: &S| __s.v) == 9)); + assert!((cs.with(|__s| __s.v) == 9)); let p: Value> = Rc::new(RefCell::new((s.as_pointer()))); - (*p.borrow()).with_mut(|__s: &mut S| __s.v = 11); + field!((*p.borrow()), v).write(11); assert!(({ (*s.borrow()).v } == 11)); assert!(((({ SImpl::f_1(&(*p.borrow()),) }) as i32) == (Overload_kMutableOverload as i32))); return 0; diff --git a/tests/unit/out/refcount/bounded_struct_ptr.rs b/tests/unit/out/refcount/bounded_struct_ptr.rs index 96850a0fe2..7af434f482 100644 --- a/tests/unit/out/refcount/bounded_struct_ptr.rs +++ b/tests/unit/out/refcount/bounded_struct_ptr.rs @@ -45,7 +45,7 @@ fn main_0() -> i32 { assert!( ({ let _lhs = (*a.borrow()); - _lhs + (*p2.borrow()).with(|__s: &Foo| __s.x2) + _lhs + (*p2.borrow()).with(|__s| __s.x2) } == 5) ); return 0; diff --git a/tests/unit/out/refcount/bst.rs b/tests/unit/out/refcount/bst.rs index 9f95ddfe8a..facb4cdced 100644 --- a/tests/unit/out/refcount/bst.rs +++ b/tests/unit/out/refcount/bst.rs @@ -37,29 +37,29 @@ pub fn find_0(node: Ptr, value: i32) -> Ptr { let value: Value = Rc::new(RefCell::new(value)); if ({ let _lhs = (*value.borrow()); - _lhs < (*node.borrow()).with(|__s: &node_t| __s.value) - }) && (!(((*node.borrow()).with(|__s: &node_t| (__s.left).clone())).is_null())) + _lhs < (*node.borrow()).with(|__s| __s.value) + }) && (!(((*node.borrow()).with(|__s| __s.left.clone())).is_null())) { return ({ find_0( - (*node.borrow()).with(|__s: &node_t| (__s.left).clone()), + (*node.borrow()).with(|__s| __s.left.clone()), (*value.borrow()), ) }); } else if ({ let _lhs = (*value.borrow()); - _lhs > (*node.borrow()).with(|__s: &node_t| __s.value) - }) && (!(((*node.borrow()).with(|__s: &node_t| (__s.right).clone())).is_null())) + _lhs > (*node.borrow()).with(|__s| __s.value) + }) && (!(((*node.borrow()).with(|__s| __s.right.clone())).is_null())) { return ({ find_0( - (*node.borrow()).with(|__s: &node_t| (__s.right).clone()), + (*node.borrow()).with(|__s| __s.right.clone()), (*value.borrow()), ) }); } else if { let _lhs = (*value.borrow()); - _lhs == (*node.borrow()).with(|__s: &node_t| __s.value) + _lhs == (*node.borrow()).with(|__s| __s.value) } { return (*node.borrow()).clone(); } @@ -72,27 +72,27 @@ pub fn insert_1(node: Ptr, new_node: Ptr) -> Ptr { return (*new_node.borrow()).clone(); } if { - let _lhs = (*new_node.borrow()).with(|__s: &node_t| __s.value); - _lhs < (*node.borrow()).with(|__s: &node_t| __s.value) + let _lhs = (*new_node.borrow()).with(|__s| __s.value); + _lhs < (*node.borrow()).with(|__s| __s.value) } { let __rhs = ({ insert_1( - (*node.borrow()).with(|__s: &node_t| (__s.left).clone()), + (*node.borrow()).with(|__s| __s.left.clone()), (*new_node.borrow()).clone(), ) }); - (*node.borrow()).with_mut(|__s: &mut node_t| __s.left = __rhs); + field!((*node.borrow()), left).write(__rhs); } else if { - let _lhs = (*new_node.borrow()).with(|__s: &node_t| __s.value); - _lhs > (*node.borrow()).with(|__s: &node_t| __s.value) + let _lhs = (*new_node.borrow()).with(|__s| __s.value); + _lhs > (*node.borrow()).with(|__s| __s.value) } { let __rhs = ({ insert_1( - (*node.borrow()).with(|__s: &node_t| (__s.right).clone()), + (*node.borrow()).with(|__s| __s.right.clone()), (*new_node.borrow()).clone(), ) }); - (*node.borrow()).with_mut(|__s: &mut node_t| __s.right = __rhs); + field!((*node.borrow()), right).write(__rhs); } return (*node.borrow()).clone(); } @@ -141,11 +141,11 @@ fn main_0() -> i32 { let __rhs = ({ insert_1((*ptr1.borrow()).clone(), ((*n4.borrow()).as_pointer())) }); (*ptr1.borrow_mut()) = __rhs; assert!( - (((((({ find_0((*ptr1.borrow()).clone(), 0,) }).with(|__s: &node_t| __s.value) == 0) - && (({ find_0((*ptr1.borrow()).clone(), 1,) }).with(|__s: &node_t| __s.value) == 1)) - && (({ find_0((*ptr1.borrow()).clone(), 2,) }).with(|__s: &node_t| __s.value) == 2)) - && (({ find_0((*ptr1.borrow()).clone(), 3,) }).with(|__s: &node_t| __s.value) == 3)) - && (({ find_0((*ptr1.borrow()).clone(), 4,) }).with(|__s: &node_t| __s.value) == 4)) + (((((({ find_0((*ptr1.borrow()).clone(), 0,) }).with(|__s| __s.value) == 0) + && (({ find_0((*ptr1.borrow()).clone(), 1,) }).with(|__s| __s.value) == 1)) + && (({ find_0((*ptr1.borrow()).clone(), 2,) }).with(|__s| __s.value) == 2)) + && (({ find_0((*ptr1.borrow()).clone(), 3,) }).with(|__s| __s.value) == 3)) + && (({ find_0((*ptr1.borrow()).clone(), 4,) }).with(|__s| __s.value) == 4)) && (({ find_0((*ptr1.borrow()).clone(), 5,) }).is_null()) ); return 0; diff --git a/tests/unit/out/refcount/c_struct.rs b/tests/unit/out/refcount/c_struct.rs index be8b7f3cf0..c6191026f0 100644 --- a/tests/unit/out/refcount/c_struct.rs +++ b/tests/unit/out/refcount/c_struct.rs @@ -151,7 +151,7 @@ fn main_0() -> i32 { value: 2, next: (a.as_pointer()), })); - assert!(((({ (*b.borrow()).next.clone() }.with(|__s: &Node| __s.value) == 1) as i32) != 0)); + assert!(((({ (*b.borrow()).next.clone() }.with(|__s| __s.value) == 1) as i32) != 0)); let c: Value = Rc::new(RefCell::new(Container { inner: Inner { a: 5, b: 6 }, color: Color_GREEN, diff --git a/tests/unit/out/refcount/class.rs b/tests/unit/out/refcount/class.rs index 0fb0573fd9..e8b4a0b9ec 100644 --- a/tests/unit/out/refcount/class.rs +++ b/tests/unit/out/refcount/class.rs @@ -92,15 +92,15 @@ impl ByteRepr for Counter { } } pub fn RandomRoute_0(route: Ptr) -> i32 { - if ((route.with(|__s: &Route| __s.path.first) % 2) != 0) { + if ((route.with(|__s| __s.path.first) % 2) != 0) { return ({ - let _new_first: i32 = ({ PairImpl::SetSecond(&{ field_ptr!(route, path) }, 10) }); - PairImpl::SetFirst(&{ field_ptr!(route, path) }, _new_first) + let _new_first: i32 = ({ PairImpl::SetSecond(&field_ptr!(route, path), 10) }); + PairImpl::SetFirst(&field_ptr!(route, path), _new_first) }); } else { return ({ - let _new_second: i32 = ({ PairImpl::SetFirst(&{ field_ptr!(route, path) }, -10_i32) }); - PairImpl::SetSecond(&{ field_ptr!(route, path) }, _new_second) + let _new_second: i32 = ({ PairImpl::SetFirst(&field_ptr!(route, path), -10_i32) }); + PairImpl::SetSecond(&field_ptr!(route, path), _new_second) }); } panic!("ub: non-void function does not return a value") @@ -156,14 +156,14 @@ pub trait CounterImpl { } impl CounterImpl for Ptr { fn Get(&self) -> i32 { - (*self).with_mut(|__s: &mut Counter| __s.calls.prefix_inc()); - return (*self).with(|__s: &Counter| __s.v); + field!((*self), calls).with_mut(|__v| __v.prefix_inc()); + return (*self).with(|__s| __s.v); } fn operator_eq(&self, o: Ptr) -> bool { - (*self).with_mut(|__s: &mut Counter| __s.calls.prefix_inc()); + field!((*self), calls).with_mut(|__v| __v.prefix_inc()); return { - let _lhs = (*self).with(|__s: &Counter| __s.v); - _lhs == o.with(|__s: &Counter| __s.v) + let _lhs = (*self).with(|__s| __s.v); + _lhs == o.with(|__s| __s.v) }; } } @@ -178,10 +178,10 @@ pub trait PairImpl { impl PairImpl for Ptr { fn NOP(&self) {} fn GetFirst(&self) -> i32 { - return (*self).with(|__s: &Pair| __s.first); + return (*self).with(|__s| __s.first); } fn GetSecond(&self) -> i32 { - return (*self).with(|__s: &Pair| __s.second); + return (*self).with(|__s| __s.second); } fn Set(&self, field: Ptr, new_val: i32) -> i32 { let new_val: Value = Rc::new(RefCell::new(new_val)); @@ -214,8 +214,8 @@ pub trait RouteImpl { impl RouteImpl for Ptr { fn SetCost(&self, new_cost: f64) -> f64 { let new_cost: Value = Rc::new(RefCell::new(new_cost)); - let old_cost: Value = Rc::new(RefCell::new((*self).with(|__s: &Route| __s.cost))); - (*self).with_mut(|__s: &mut Route| __s.cost = (*new_cost.borrow())); + let old_cost: Value = Rc::new(RefCell::new((*self).with(|__s| __s.cost))); + field!((*self), cost).write((*new_cost.borrow())); return (*old_cost.borrow()); } } diff --git a/tests/unit/out/refcount/class_template_lazy_member.rs b/tests/unit/out/refcount/class_template_lazy_member.rs index ce1ab54c87..bddf231747 100644 --- a/tests/unit/out/refcount/class_template_lazy_member.rs +++ b/tests/unit/out/refcount/class_template_lazy_member.rs @@ -81,7 +81,7 @@ pub trait Box_Point_Impl { } impl Box_Point_Impl for Ptr { fn get(&self) -> Point { - return ((*(*self).upgrade().deref()).val).clone(); + return (*self).with(|__s| __s.val.clone()); } } pub trait Box_int_Impl { @@ -92,7 +92,7 @@ pub trait Box_int_Impl { } impl Box_int_Impl for Ptr { fn twice(&self) -> i32 { - return ((*self).with(|__s: &Box_int_| __s.val) + (*self).with(|__s: &Box_int_| __s.val)); + return ((*self).with(|__s| __s.val) + (*self).with(|__s| __s.val)); } } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/class_templates.rs b/tests/unit/out/refcount/class_templates.rs index 038986386f..205da877e5 100644 --- a/tests/unit/out/refcount/class_templates.rs +++ b/tests/unit/out/refcount/class_templates.rs @@ -13,11 +13,9 @@ pub struct MyContainer_int_ { } impl Clone for MyContainer_int_ { fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - vec_: { Rc::new(RefCell::new((*self.vec_.borrow()).clone())) }, - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() + Self { + vec_: Rc::new(RefCell::new((*self.vec_.borrow()).clone())), + } } } <<<<<<< HEAD @@ -45,11 +43,9 @@ pub struct MyContainer_char_ { } impl Clone for MyContainer_char_ { fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - vec_: { Rc::new(RefCell::new((*self.vec_.borrow()).clone())) }, - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() + Self { + vec_: Rc::new(RefCell::new((*self.vec_.borrow()).clone())), + } } } <<<<<<< HEAD @@ -77,11 +73,9 @@ pub struct MyContainer_float_ { } impl Clone for MyContainer_float_ { fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - vec_: { Rc::new(RefCell::new((*self.vec_.borrow()).clone())) }, - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() + Self { + vec_: Rc::new(RefCell::new((*self.vec_.borrow()).clone())), + } } } <<<<<<< HEAD @@ -338,7 +332,7 @@ pub trait Boxed_int_Impl { impl Boxed_int_Impl for Ptr { fn plus(&self, other: i32) -> i32 { let other: Value = Rc::new(RefCell::new(other)); - return ((*self).with(|__s: &Boxed_int_| __s.value) + (*other.borrow())); + return ((*self).with(|__s| __s.value) + (*other.borrow())); } } pub trait Boxed_long_Impl { @@ -347,7 +341,7 @@ pub trait Boxed_long_Impl { impl Boxed_long_Impl for Ptr { fn plus(&self, other: i64) -> i64 { let other: Value = Rc::new(RefCell::new(other)); - return ((*self).with(|__s: &Boxed_long_| __s.value) + (*other.borrow())); + return ((*self).with(|__s| __s.value) + (*other.borrow())); } } pub trait MyContainer_char_Impl { @@ -362,25 +356,22 @@ pub trait MyContainer_char_Impl { } impl MyContainer_char_Impl for Ptr { fn empty(&self) -> bool { - return (*self).with(|__s: &MyContainer_char_| (*__s.vec_.borrow()).is_empty()); + return (*(*self).with(|__s| __s.vec_.clone()).borrow()).is_empty(); } fn size(&self) -> usize { - return (*self).with(|__s: &MyContainer_char_| (*__s.vec_.borrow()).len()); + return (*(*self).with(|__s| __s.vec_.clone()).borrow()).len(); } fn back_4(&self) -> Ptr { - return ((*self).with(|__s: &MyContainer_char_| __s.vec_.as_pointer()) as Ptr) - .to_last(); + return ((*self).with(|__s| __s.vec_.clone()).as_pointer() as Ptr).to_last(); } fn pop_back(&self) { - (*self).with(|__s: &MyContainer_char_| { - (*__s.vec_.borrow_mut()).pop(); - }); + (*(*self).with(|__s| __s.vec_.clone()).borrow_mut()).pop(); return; } fn push_back(&self, item: Ptr) { { let a0_clone = (item.read()).clone(); - (*self).with(|__s: &MyContainer_char_| (*__s.vec_.borrow_mut()).push(a0_clone)) + (*(*self).with(|__s| __s.vec_.clone()).borrow_mut()).push(a0_clone) }; } } @@ -396,25 +387,22 @@ pub trait MyContainer_float_Impl { } impl MyContainer_float_Impl for Ptr { fn empty(&self) -> bool { - return (*self).with(|__s: &MyContainer_float_| (*__s.vec_.borrow()).is_empty()); + return (*(*self).with(|__s| __s.vec_.clone()).borrow()).is_empty(); } fn size(&self) -> usize { - return (*self).with(|__s: &MyContainer_float_| (*__s.vec_.borrow()).len()); + return (*(*self).with(|__s| __s.vec_.clone()).borrow()).len(); } fn back_4(&self) -> Ptr { - return ((*self).with(|__s: &MyContainer_float_| __s.vec_.as_pointer()) as Ptr) - .to_last(); + return ((*self).with(|__s| __s.vec_.clone()).as_pointer() as Ptr).to_last(); } fn pop_back(&self) { - (*self).with(|__s: &MyContainer_float_| { - (*__s.vec_.borrow_mut()).pop(); - }); + (*(*self).with(|__s| __s.vec_.clone()).borrow_mut()).pop(); return; } fn push_back(&self, item: Ptr) { { let a0_clone = (item.read()).clone(); - (*self).with(|__s: &MyContainer_float_| (*__s.vec_.borrow_mut()).push(a0_clone)) + (*(*self).with(|__s| __s.vec_.clone()).borrow_mut()).push(a0_clone) }; } } @@ -430,25 +418,22 @@ pub trait MyContainer_int_Impl { } impl MyContainer_int_Impl for Ptr { fn empty(&self) -> bool { - return (*self).with(|__s: &MyContainer_int_| (*__s.vec_.borrow()).is_empty()); + return (*(*self).with(|__s| __s.vec_.clone()).borrow()).is_empty(); } fn size(&self) -> usize { - return (*self).with(|__s: &MyContainer_int_| (*__s.vec_.borrow()).len()); + return (*(*self).with(|__s| __s.vec_.clone()).borrow()).len(); } fn back_4(&self) -> Ptr { - return ((*self).with(|__s: &MyContainer_int_| __s.vec_.as_pointer()) as Ptr) - .to_last(); + return ((*self).with(|__s| __s.vec_.clone()).as_pointer() as Ptr).to_last(); } fn pop_back(&self) { - (*self).with(|__s: &MyContainer_int_| { - (*__s.vec_.borrow_mut()).pop(); - }); + (*(*self).with(|__s| __s.vec_.clone()).borrow_mut()).pop(); return; } fn push_back(&self, item: Ptr) { { let a0_clone = (item.read()).clone(); - (*self).with(|__s: &MyContainer_int_| (*__s.vec_.borrow_mut()).push(a0_clone)) + (*(*self).with(|__s| __s.vec_.clone()).borrow_mut()).push(a0_clone) }; } } @@ -459,7 +444,7 @@ impl Outer_int_Impl for Ptr { fn with(&self, n: i32) -> Outer_int__Inner_int_ { let n: Value = Rc::new(RefCell::new(n)); return Outer_int__Inner_int_ { - t: { (*self).with(|__s: &Outer_int_| __s.v) }, + t: (*self).with(|__s| __s.v), u: (*n.borrow()), }; } @@ -469,8 +454,7 @@ pub trait Outer_int__Inner_int_Impl { } impl Outer_int__Inner_int_Impl for Ptr { fn sum(&self) -> i32 { - return (((*self).with(|__s: &Outer_int__Inner_int_| __s.t) as i32) - + ((*self).with(|__s: &Outer_int__Inner_int_| __s.u) as i32)); + return (((*self).with(|__s| __s.t) as i32) + ((*self).with(|__s| __s.u) as i32)); } } pub trait Outer_long_Impl { @@ -480,7 +464,7 @@ impl Outer_long_Impl for Ptr { fn with(&self, n: i32) -> Outer_long__Inner_int_ { let n: Value = Rc::new(RefCell::new(n)); return Outer_long__Inner_int_ { - t: { (*self).with(|__s: &Outer_long_| __s.v) }, + t: (*self).with(|__s| __s.v), u: (*n.borrow()), }; } @@ -490,8 +474,7 @@ pub trait Outer_long__Inner_char_Impl { } impl Outer_long__Inner_char_Impl for Ptr { fn sum(&self) -> i32 { - return (((*self).with(|__s: &Outer_long__Inner_char_| __s.t) as i32) - + ((*self).with(|__s: &Outer_long__Inner_char_| __s.u) as i32)); + return (((*self).with(|__s| __s.t) as i32) + ((*self).with(|__s| __s.u) as i32)); } } pub trait Outer_long__Inner_int_Impl { @@ -499,8 +482,7 @@ pub trait Outer_long__Inner_int_Impl { } impl Outer_long__Inner_int_Impl for Ptr { fn sum(&self) -> i32 { - return (((*self).with(|__s: &Outer_long__Inner_int_| __s.t) as i32) - + ((*self).with(|__s: &Outer_long__Inner_int_| __s.u) as i32)); + return (((*self).with(|__s| __s.t) as i32) + ((*self).with(|__s| __s.u) as i32)); } } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/clone_vs_move.rs b/tests/unit/out/refcount/clone_vs_move.rs index cbb8ef8bc9..cb50b8e171 100644 --- a/tests/unit/out/refcount/clone_vs_move.rs +++ b/tests/unit/out/refcount/clone_vs_move.rs @@ -39,17 +39,13 @@ pub struct Foo { } impl Clone for Foo { fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - x: { self.x }, - y: { (self.y).clone() }, - z: { self.z.clone() }, - a: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 3, _>( - |__i: usize| (*self.a.borrow())[(__i) as usize], - )))), - bar: { self.bar.clone() }, - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() + Self { + x: self.x.clone(), + y: self.y.clone(), + z: self.z.clone(), + a: Rc::new(RefCell::new((*self.a.borrow()).clone())), + bar: self.bar.clone(), + } } } impl Default for Foo { @@ -123,7 +119,7 @@ fn main_0() -> i32 { bar: Bar { w: 10 }, })); assert!(({ (*f1.borrow()).x } == 1)); - assert!((((*f1.borrow()).y.read()) == 2)); + assert!((({ (*f1.borrow()).y.clone() }.read()) == 2)); assert!({ let _lhs = { (*f1.borrow()).z.clone() }; _lhs == (x1.as_pointer()) @@ -133,31 +129,31 @@ fn main_0() -> i32 { (*f2.borrow_mut()).x.prefix_inc(); { (*f2.borrow()).y.clone() }.with_mut(|__v| __v.prefix_inc()); assert!(({ (*f2.borrow()).x } == 2)); - assert!((((*f2.borrow()).y.read()) == 3)); + assert!((({ (*f2.borrow()).y.clone() }.read()) == 3)); assert!(({ (*f1.borrow()).x } == 1)); - assert!((((*f1.borrow()).y.read()) == 3)); + assert!((({ (*f1.borrow()).y.clone() }.read()) == 3)); { (*f2.borrow()).z.clone() }.with_mut(|__v| __v.prefix_inc()); - assert!((((*f2.borrow()).y.read()) == 4)); + assert!((({ (*f2.borrow()).y.clone() }.read()) == 4)); assert!({ let _lhs = { (*f2.borrow()).z.clone() }; _lhs == (x1.as_pointer()) }); assert!((({ (*f2.borrow()).z.clone() }.read()) == 4)); - assert!((((*f1.borrow()).y.read()) == 4)); + assert!((({ (*f1.borrow()).y.clone() }.read()) == 4)); assert!({ let _lhs = { (*f1.borrow()).z.clone() }; _lhs == (x1.as_pointer()) }); assert!((({ (*f1.borrow()).z.clone() }.read()) == 4)); - (*(*f2.borrow()).a.borrow_mut())[(0) as usize].prefix_inc(); - (*(*f2.borrow()).a.borrow_mut())[(1) as usize].prefix_inc(); - (*(*f2.borrow()).a.borrow_mut())[(2) as usize].prefix_inc(); - assert!(((*(*f2.borrow()).a.borrow())[(0) as usize] == 1)); - assert!(((*(*f2.borrow()).a.borrow())[(1) as usize] == 2)); - assert!(((*(*f2.borrow()).a.borrow())[(2) as usize] == 3)); - assert!(((*(*f1.borrow()).a.borrow())[(0) as usize] == 0)); - assert!(((*(*f1.borrow()).a.borrow())[(1) as usize] == 1)); - assert!(((*(*f1.borrow()).a.borrow())[(2) as usize] == 2)); + (*{ (*f2.borrow()).a.clone() }.borrow_mut())[(0) as usize].prefix_inc(); + (*{ (*f2.borrow()).a.clone() }.borrow_mut())[(1) as usize].prefix_inc(); + (*{ (*f2.borrow()).a.clone() }.borrow_mut())[(2) as usize].prefix_inc(); + assert!(((*{ (*f2.borrow()).a.clone() }.borrow())[(0) as usize] == 1)); + assert!(((*{ (*f2.borrow()).a.clone() }.borrow())[(1) as usize] == 2)); + assert!(((*{ (*f2.borrow()).a.clone() }.borrow())[(2) as usize] == 3)); + assert!(((*{ (*f1.borrow()).a.clone() }.borrow())[(0) as usize] == 0)); + assert!(((*{ (*f1.borrow()).a.clone() }.borrow())[(1) as usize] == 1)); + assert!(((*{ (*f1.borrow()).a.clone() }.borrow())[(2) as usize] == 2)); (*f2.borrow_mut()).bar.w = 20; assert!(({ (*f2.borrow()).bar.w } == 20)); assert!(({ (*f1.borrow()).bar.w } == 10)); @@ -523,8 +519,8 @@ fn main_0() -> i32 { { (*r2.borrow()).b.clone() }.with_mut(|__v| __v.prefix_inc()); assert!(((*ra.borrow()) == 10)); assert!(((*rb.borrow()) == 3)); - assert!((((*r1.borrow()).a.read()) == 10)); - assert!((((*r1.borrow()).b.read()) == 3)); + assert!((({ (*r1.borrow()).a.clone() }.read()) == 10)); + assert!((({ (*r1.borrow()).b.clone() }.read()) == 3)); return 0; } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/complex_function.rs b/tests/unit/out/refcount/complex_function.rs index fd765c077d..ae1aee07f6 100644 --- a/tests/unit/out/refcount/complex_function.rs +++ b/tests/unit/out/refcount/complex_function.rs @@ -124,11 +124,12 @@ fn main_0() -> i32 { let d: Value = Rc::new(RefCell::new(X4 { v: (*c.borrow()).clone(), })); - (*{ (*d.borrow()).v.v.clone() }.upgrade().deref()) - .v - .with_mut(|__s: &mut X1| __s.v = 0); - ({ X2Impl::get(&({ X3Impl::get(&({ X4Impl::get(&d.as_pointer()) })) })) }) - .with_mut(|__s: &mut X1| __s.v = 0); + field!({ (*d.borrow()).v.v.clone() }.with(|__s| __s.v.clone()), v).write(0); + field!( + ({ X2Impl::get(&({ X3Impl::get(&({ X4Impl::get(&d.as_pointer(),) }),) }),) }), + v + ) + .write(0); (*d.borrow_mut()).v.v = (b.as_pointer()); let r4: Ptr = field_ptr!( ({ X2Impl::get(&({ X3Impl::get(&({ X4Impl::get(&d.as_pointer(),) }),) }),) }), @@ -139,14 +140,14 @@ fn main_0() -> i32 { ({ X3Impl::get(&({ X4Impl::get(&d.as_pointer()) })) }), )); let r6: Ptr = ({ X4Impl::get(&d.as_pointer()) }); - let r7: Ptr = field_ptr!(d, v); + let r7: Ptr = field_ptr!(d.as_pointer(), v); let r8: Ptr = field_ptr!( - ({ X2Impl::get(&{ ({ X3Impl::get(&{ field_ptr!(d, v) },) }) },) }), + ({ X2Impl::get(&({ X3Impl::get(&field_ptr!(d.as_pointer(), v),) }),) }), v ); let x5: Value = Rc::new(RefCell::new( ({ X2Impl::get(&({ X3Impl::get(&({ X4Impl::get(&d.as_pointer()) })) })) }) - .with(|__s: &X1| __s.v), + .with(|__s| __s.v), )); { let _ptr = ({ bar_2(x1.as_pointer()) }); @@ -177,50 +178,42 @@ fn main_0() -> i32 { }); _ptr.write(_ptr.read() + 10) }; - { - ({ - bar_2(field_ptr!( - ({ X2Impl::get(&({ X3Impl::get(&({ X4Impl::get(&d.as_pointer(),) }),) }),) }), - v - )) - }) - } + ({ + bar_2(field_ptr!( + ({ X2Impl::get(&({ X3Impl::get(&({ X4Impl::get(&d.as_pointer(),) }),) }),) }), + v + )) + }) .with_mut(|__v| __v.postfix_inc()); let bar_inc2: Value = Rc::new(RefCell::new( - { - ({ - bar_2(field_ptr!( - ({ X2Impl::get(&({ X3Impl::get(&({ X4Impl::get(&d.as_pointer(),) }),) }),) }), - v - )) - }) - } - .with_mut(|__v| __v.prefix_inc()), - )); - (*bar_inc2.borrow_mut()) = { ({ bar_2(field_ptr!( ({ X2Impl::get(&({ X3Impl::get(&({ X4Impl::get(&d.as_pointer(),) }),) }),) }), v )) }) - } + .with_mut(|__v| __v.prefix_inc()), + )); + (*bar_inc2.borrow_mut()) = ({ + bar_2(field_ptr!( + ({ X2Impl::get(&({ X3Impl::get(&({ X4Impl::get(&d.as_pointer(),) }),) }),) }), + v + )) + }) .with_mut(|__v| __v.postfix_inc()); ({ ptr_1((x1.as_pointer())) }).with_mut(|__v| __v.prefix_inc()); { let _ptr = ({ ptr_1((x1.as_pointer())) }); _ptr.write(_ptr.read() + 1) }; - { - ({ - ptr_1( - (field_ptr!( - ({ X2Impl::get(&({ X3Impl::get(&({ X4Impl::get(&d.as_pointer(),) }),) }),) }), - v - )), - ) - }) - } + ({ + ptr_1( + (field_ptr!( + ({ X2Impl::get(&({ X3Impl::get(&({ X4Impl::get(&d.as_pointer(),) }),) }),) }), + v + )), + ) + }) .with_mut(|__v| __v.prefix_inc()); { let _ptr = ({ @@ -245,18 +238,14 @@ fn main_0() -> i32 { _ptr.write(_ptr.read() + 1) }; let ptr1: Value = Rc::new(RefCell::new( - { - ({ - ptr_1( - (field_ptr!( - ({ - X2Impl::get(&({ X3Impl::get(&({ X4Impl::get(&d.as_pointer()) })) })) - }), - v - )), - ) - }) - } + ({ + ptr_1( + (field_ptr!( + ({ X2Impl::get(&({ X3Impl::get(&({ X4Impl::get(&d.as_pointer(),) }),) }),) }), + v + )), + ) + }) .with_mut(|__v| __v.postfix_inc()), )); let ptr2: Ptr = ({ @@ -296,14 +285,12 @@ fn main_0() -> i32 { )) }), )); - { - ({ - bar_2(field_ptr!( - ({ X2Impl::get(&({ X3Impl::get(&({ X4Impl::get(&d.as_pointer(),) }),) }),) }), - v - )) - }) - } + ({ + bar_2(field_ptr!( + ({ X2Impl::get(&({ X3Impl::get(&({ X4Impl::get(&d.as_pointer(),) }),) }),) }), + v + )) + }) .with_mut(|__v| __v.postfix_inc()); assert!( ((((({ @@ -325,7 +312,7 @@ fn main_0() -> i32 { + ({ foo_0( ({ X2Impl::get(&({ X3Impl::get(&({ X4Impl::get(&d.as_pointer()) })) })) }) - .with(|__s: &X1| __s.v), + .with(|__s| __s.v), ) })) == 54) @@ -337,7 +324,7 @@ pub trait X2Impl { } impl X2Impl for Ptr { fn get(&self) -> Ptr { - return ((*(*self).upgrade().deref()).v).clone(); + return ((*self).with(|__s| __s.v.clone())).clone(); } } pub trait X3Impl { @@ -345,7 +332,7 @@ pub trait X3Impl { } impl X3Impl for Ptr { fn get(&self) -> Ptr { - return (*self).with(|__s: &X3| (__s.v).clone()); + return (*self).with(|__s| __s.v.clone()); } } pub trait X4Impl { diff --git a/tests/unit/out/refcount/compound_assign_paren.rs b/tests/unit/out/refcount/compound_assign_paren.rs index b4c4b70120..3842e1ee89 100644 --- a/tests/unit/out/refcount/compound_assign_paren.rs +++ b/tests/unit/out/refcount/compound_assign_paren.rs @@ -32,20 +32,20 @@ fn main_0() -> i32 { let item: Value = Rc::new(RefCell::new(Item { flags: 0_u8 })); let ptr: Value> = Rc::new(RefCell::new((item.as_pointer()))); { - let rhs_0 = (((*ptr.borrow()).with(|__s: &Item| __s.flags) as i32) | (1 << 0)) as u8; - (*ptr.borrow()).with_mut(|__s: &mut Item| __s.flags = rhs_0) + let rhs_0 = (((*ptr.borrow()).with(|__s| __s.flags) as i32) | (1 << 0)) as u8; + field!((*ptr.borrow()), flags).write(rhs_0) }; { - let rhs_0 = (((*ptr.borrow()).with(|__s: &Item| __s.flags) as i32) | (1 << 1)) as u8; - (*ptr.borrow()).with_mut(|__s: &mut Item| __s.flags = rhs_0) + let rhs_0 = (((*ptr.borrow()).with(|__s| __s.flags) as i32) | (1 << 1)) as u8; + field!((*ptr.borrow()), flags).write(rhs_0) }; - assert!((((*ptr.borrow()).with(|__s: &Item| __s.flags) as i32) == 3)); + assert!((((*ptr.borrow()).with(|__s| __s.flags) as i32) == 3)); { - let rhs_0 = (((*ptr.borrow()).with(|__s: &Item| __s.flags) as i32) - & ((!(1 << 0) as u8) as i32)) as u8; - (*ptr.borrow()).with_mut(|__s: &mut Item| __s.flags = rhs_0) + let rhs_0 = + (((*ptr.borrow()).with(|__s| __s.flags) as i32) & ((!(1 << 0) as u8) as i32)) as u8; + field!((*ptr.borrow()), flags).write(rhs_0) }; - assert!((((*ptr.borrow()).with(|__s: &Item| __s.flags) as i32) == 2)); + assert!((((*ptr.borrow()).with(|__s| __s.flags) as i32) == 2)); let bits: Value> = Rc::new(RefCell::new(Box::new([0_u8, 0_u8, 0_u8, 0_u8]))); { let rhs_0 = (((*bits.borrow())[((5) / 8) as usize] as i32) @@ -60,14 +60,14 @@ fn main_0() -> i32 { assert!((((*bits.borrow())[(0) as usize] as i32) == 32)); assert!((((*bits.borrow())[(1) as usize] as i32) == 32)); assert!((((*bits.borrow())[(2) as usize] as i32) == 0)); - if (((*ptr.borrow()).with(|__s: &Item| __s.flags) as i32) != 0) { + if (((*ptr.borrow()).with(|__s| __s.flags) as i32) != 0) { { - let rhs_0 = (((*ptr.borrow()).with(|__s: &Item| __s.flags) as i32) - & ((!(1 << 1) as u8) as i32)) as u8; - (*ptr.borrow()).with_mut(|__s: &mut Item| __s.flags = rhs_0) + let rhs_0 = + (((*ptr.borrow()).with(|__s| __s.flags) as i32) & ((!(1 << 1) as u8) as i32)) as u8; + field!((*ptr.borrow()), flags).write(rhs_0) }; } - assert!((((*ptr.borrow()).with(|__s: &Item| __s.flags) as i32) == 0)); + assert!((((*ptr.borrow()).with(|__s| __s.flags) as i32) == 0)); return 0; } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/constexpr_function.rs b/tests/unit/out/refcount/constexpr_function.rs index 8f104e2488..12a9c56db9 100644 --- a/tests/unit/out/refcount/constexpr_function.rs +++ b/tests/unit/out/refcount/constexpr_function.rs @@ -101,7 +101,7 @@ pub trait FlagImpl { } impl FlagImpl for Ptr { fn to_bool(&self) -> bool { - return ((*self).with(|__s: &Flag| __s.v) != 0); + return ((*self).with(|__s| __s.v) != 0); } } pub trait PImpl { @@ -109,7 +109,7 @@ pub trait PImpl { } impl PImpl for Ptr

{ fn get(&self) -> i32 { - return (*self).with(|__s: &P| __s.v); + return (*self).with(|__s| __s.v); } } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/constructor.rs b/tests/unit/out/refcount/constructor.rs index 753f9c659e..84f4e57f51 100644 --- a/tests/unit/out/refcount/constructor.rs +++ b/tests/unit/out/refcount/constructor.rs @@ -77,13 +77,19 @@ impl Point { ((*v.borrow()) + 1) }))); let this: Ptr = __this.as_pointer(); - this.with_mut(|__s: &mut Point| __s.y *= 10); + { + let _ptr = field!(this, y); + _ptr.write(_ptr.read() * 10) + }; Rc::try_unwrap(__this).ok().unwrap().into_inner() } pub fn new_3() -> Self { let __this: Value = Rc::new(RefCell::new(Point::new_2({ 4 }))); let this: Ptr = __this.as_pointer(); - this.with_mut(|__s: &mut Point| __s.x += 100); + { + let _ptr = field!(this, x); + _ptr.write(_ptr.read() + 100) + }; Rc::try_unwrap(__this).ok().unwrap().into_inner() } } @@ -134,10 +140,13 @@ pub trait SImpl { } impl SImpl for Ptr { fn const_method(&self) -> i32 { - return ((*self).with(|__s: &S| __s.v) * 2); + return ((*self).with(|__s| __s.v) * 2); } fn mut_method(&self) { - (*self).with_mut(|__s: &mut S| __s.v += 1); + { + let _ptr = field!((*self), v); + _ptr.write(_ptr.read() + 1) + }; } fn destructor(&self) { ({ SImpl::mut_method(self) }); diff --git a/tests/unit/out/refcount/copy_assign.rs b/tests/unit/out/refcount/copy_assign.rs index 8afdd4ed85..73d9da377d 100644 --- a/tests/unit/out/refcount/copy_assign.rs +++ b/tests/unit/out/refcount/copy_assign.rs @@ -29,8 +29,8 @@ impl Partial { } pub fn copy_from(o: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - v: { o.with(|__s: &Partial| __s.v) }, - keep: { o.with(|__s: &Partial| __s.keep) }, + v: o.with(|__s| __s.v), + keep: o.with(|__s| __s.keep), })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() @@ -148,14 +148,10 @@ pub struct Holder { } impl Clone for Holder { fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - p: { self.p.clone() }, - arr: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 2, _>( - |__i: usize| (*self.arr.borrow())[(__i) as usize].clone(), - )))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() + Self { + p: self.p.clone(), + arr: Rc::new(RefCell::new((*self.arr.borrow()).clone())), + } } } impl Default for Holder { @@ -239,17 +235,17 @@ fn main_0() -> i32 { Partial::new({ 6 }, { 60 }), ]))), })); - ({ PartialImpl::copy_assign(&{ field_ptr!(h, p) }, b.as_pointer()) }); + ({ PartialImpl::copy_assign(&field_ptr!(h.as_pointer(), p), b.as_pointer()) }); ({ PartialImpl::copy_assign( - &{ ({ (*h.borrow()).arr.as_pointer() } as Ptr).offset(1) }, + &({ (*h.borrow()).arr.clone() }.as_pointer() as Ptr).offset(1), c.as_pointer(), ) }); assert!(({ (*h.borrow()).p.v } == 2) && ({ (*h.borrow()).p.keep } == 40)); assert!( - ({ (*(*h.borrow()).arr.borrow())[(1) as usize].v } == 2) - && ({ (*(*h.borrow()).arr.borrow())[(1) as usize].keep } == 60) + ({ (*{ (*h.borrow()).arr.clone() }.borrow())[(1) as usize].v } == 2) + && ({ (*{ (*h.borrow()).arr.clone() }.borrow())[(1) as usize].keep } == 60) ); assert!((assigns_0.with(|rc| *rc.borrow()) == 8)); let n: Value = Rc::new(RefCell::new(NonConstAssign::new())); @@ -272,13 +268,13 @@ pub trait NonConstAssignImpl { } impl NonConstAssignImpl for Ptr { fn operator_assign_2(&self, o: Ptr) -> Ptr { - let __rhs = (o.with(|__s: &NonConstAssign| __s.mark) + 1); - (*self).with_mut(|__s: &mut NonConstAssign| __s.mark = __rhs); + let __rhs = (o.with(|__s| __s.mark) + 1); + field!((*self), mark).write(__rhs); return (*self).clone(); } fn operator_assign_3(&self, o: Ptr) -> Ptr { - let __rhs = (o.with(|__s: &NonConstAssign| __s.mark) + 10); - (*self).with_mut(|__s: &mut NonConstAssign| __s.mark = __rhs); + let __rhs = (o.with(|__s| __s.mark) + 10); + field!((*self), mark).write(__rhs); return (*self).clone(); } } @@ -290,8 +286,8 @@ impl PartialImpl for Ptr { if ((*self) == (o)) { return (*self).clone(); } - let __rhs = o.with(|__s: &Partial| __s.v); - (*self).with_mut(|__s: &mut Partial| __s.v = __rhs); + let __rhs = o.with(|__s| __s.v); + field!((*self), v).write(__rhs); (*assigns_0.with(Value::clone).borrow_mut()).prefix_inc(); return (*self).clone(); } @@ -301,8 +297,8 @@ pub trait RefQualifiedImpl { } impl RefQualifiedImpl for Ptr { fn copy_assign(&self, o: Ptr) -> Ptr { - let __rhs = (o.with(|__s: &RefQualified| __s.mark) + 1); - (*self).with_mut(|__s: &mut RefQualified| __s.mark = __rhs); + let __rhs = (o.with(|__s| __s.mark) + 1); + field!((*self), mark).write(__rhs); return (*self).clone(); } } diff --git a/tests/unit/out/refcount/copy_ctor.rs b/tests/unit/out/refcount/copy_ctor.rs index 4382eb1f59..f3b748e0cf 100644 --- a/tests/unit/out/refcount/copy_ctor.rs +++ b/tests/unit/out/refcount/copy_ctor.rs @@ -23,7 +23,7 @@ impl Counted { } pub fn copy_from(o: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - v: { o.with(|__s: &Counted| __s.v) }, + v: o.with(|__s| __s.v), })); let this: Ptr = __this.as_pointer(); (*copies_0.with(Value::clone).borrow_mut()).prefix_inc(); @@ -67,14 +67,14 @@ impl NonConst { } pub fn new_1(o: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - mark: { (o.with(|__s: &NonConst| __s.mark) + 1) }, + mark: (o.with(|__s| __s.mark) + 1), })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } pub fn new_2(o: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - mark: { (o.with(|__s: &NonConst| __s.mark) + 10) }, + mark: (o.with(|__s| __s.mark) + 10), })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() @@ -167,14 +167,10 @@ pub struct Holder { } impl Clone for Holder { fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - c: { self.c.clone() }, - arr: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 2, _>( - |__i: usize| (*self.arr.borrow())[(__i) as usize].clone(), - )))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() + Self { + c: self.c.clone(), + arr: Rc::new(RefCell::new((*self.arr.borrow()).clone())), + } } } impl Default for Holder { @@ -252,8 +248,8 @@ fn main_0() -> i32 { let hold2: Value = Rc::new(RefCell::new((*hold.borrow()).clone())); assert!( (({ (*hold2.borrow()).c.v } == 8) - && ({ (*(*hold2.borrow()).arr.borrow())[(0) as usize].v } == 9)) - && ({ (*(*hold2.borrow()).arr.borrow())[(1) as usize].v } == 10) + && ({ (*{ (*hold2.borrow()).arr.clone() }.borrow())[(0) as usize].v } == 9)) + && ({ (*{ (*hold2.borrow()).arr.clone() }.borrow())[(1) as usize].v } == 10) ); assert!((copies_0.with(|rc| *rc.borrow()) == 9)); let vec_: Value> = Rc::new(RefCell::new(Vec::new())); @@ -262,10 +258,13 @@ fn main_0() -> i32 { (*vec_.borrow_mut()).push(a0_clone) }; assert!( - ((vec_.as_pointer() as Ptr) - .offset(0_usize) - .with(|__s: &Counted| __s.v) - == 1) + ({ + (*(vec_.as_pointer() as Ptr) + .offset(0_usize) + .upgrade() + .deref()) + .v + } == 1) ); assert!((copies_0.with(|rc| *rc.borrow()) == 10)); let i1: Value = Rc::new(RefCell::new(Ignored::new({ 1 }))); diff --git a/tests/unit/out/refcount/copy_move_defaulted.rs b/tests/unit/out/refcount/copy_move_defaulted.rs index 60adb0bc87..0c3a3145bc 100644 --- a/tests/unit/out/refcount/copy_move_defaulted.rs +++ b/tests/unit/out/refcount/copy_move_defaulted.rs @@ -49,15 +49,11 @@ impl Explicit { } impl Clone for Explicit { fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - v: { self.v }, - inner: { self.inner.clone() }, - arr: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 2, _>( - |__i: usize| (*self.arr.borrow())[(__i) as usize], - )))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() + Self { + v: self.v.clone(), + inner: self.inner.clone(), + arr: Rc::new(RefCell::new((*self.arr.borrow()).clone())), + } } } impl Default for Explicit { @@ -106,15 +102,11 @@ pub struct Implicit { } impl Clone for Implicit { fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - v: { self.v }, - inner: { self.inner.clone() }, - arr: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 2, _>( - |__i: usize| (*self.arr.borrow())[(__i) as usize], - )))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() + Self { + v: self.v.clone(), + inner: self.inner.clone(), + arr: Rc::new(RefCell::new((*self.arr.borrow()).clone())), + } } } impl Default for Implicit { @@ -157,10 +149,10 @@ impl DefaultCopyUserMove { } pub fn move_from(o: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - v: { o.with(|__s: &DefaultCopyUserMove| __s.v) }, + v: o.with(|__s| __s.v), })); let this: Ptr = __this.as_pointer(); - o.with_mut(|__s: &mut DefaultCopyUserMove| __s.v = 0); + field!(o, v).write(0); Rc::try_unwrap(__this).ok().unwrap().into_inner() } } @@ -196,14 +188,14 @@ impl UserCopyDefaultMove { } pub fn copy_from(o: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - v: { (o.with(|__s: &UserCopyDefaultMove| __s.v) + 100) }, + v: (o.with(|__s| __s.v) + 100), })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } pub fn move_from(_a0: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - v: { _a0.with(|__s: &UserCopyDefaultMove| __s.v) }, + v: { (*_a0.upgrade().deref()).v }, })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() @@ -258,28 +250,26 @@ impl Buffer { arr: Rc::new(RefCell::new(Box::new([(*n.borrow()), ((*n.borrow()) + 1)]))), })); let this: Ptr = __this.as_pointer(); - { - let __a1 = (*this.with(|__s: &Buffer| __s.data.clone()).borrow()).clone(); - (this.with(|__s: &Buffer| __s.rows.as_pointer()) as Ptr>>>) - .with_mut(|__v: &mut Vec>>| __v.push(Rc::new(RefCell::new(__a1)))) - }; + (this.with(|__s| __s.rows.clone()).as_pointer() as Ptr>>>).with_mut( + |__v: &mut Vec>>| { + __v.push(Rc::new(RefCell::new( + (*this.with(|__s| __s.data.clone()).borrow()).clone(), + ))) + }, + ); Rc::try_unwrap(__this).ok().unwrap().into_inner() } pub fn move_from(_a0: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - data: { - Rc::new(RefCell::new(_a0.with(|__s: &Buffer| { - std::mem::take(&mut (*__s.data.borrow_mut())) - }))) - }, - rows: { - Rc::new(RefCell::new(_a0.with(|__s: &Buffer| { - std::mem::take(&mut (*__s.rows.borrow_mut())) - }))) - }, - n: { _a0.with(|__s: &Buffer| __s.n) }, + data: Rc::new(RefCell::new(std::mem::take( + &mut (*{ (*_a0.upgrade().deref()).data.clone() }.borrow_mut()), + ))), + rows: Rc::new(RefCell::new(std::mem::take( + &mut (*{ (*_a0.upgrade().deref()).rows.clone() }.borrow_mut()), + ))), + n: { (*_a0.upgrade().deref()).n }, arr: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 2, _>( - |__i: usize| (*_a0.with(|__s: &Buffer| __s.arr.clone()).borrow())[(__i) as usize], + |__i: usize| (*{ (*_a0.upgrade().deref()).arr.clone() }.borrow())[(__i) as usize], )))), })); let this: Ptr = __this.as_pointer(); @@ -336,16 +326,14 @@ pub struct Owner { impl Owner { pub fn move_from(_a0: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - data: { - Rc::new(RefCell::new(_a0.with(|__s: &Owner| { - std::mem::take(&mut (*__s.data.borrow_mut())) - }))) - }, - n: { _a0.with(|__s: &Owner| __s.n) }, + data: Rc::new(RefCell::new(std::mem::take( + &mut (*{ (*_a0.upgrade().deref()).data.clone() }.borrow_mut()), + ))), + n: { (*_a0.upgrade().deref()).n }, arr: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 2, _>( - |__i: usize| (*_a0.with(|__s: &Owner| __s.arr.clone()).borrow())[(__i) as usize], + |__i: usize| (*{ (*_a0.upgrade().deref()).arr.clone() }.borrow())[(__i) as usize], )))), - p: { _a0.with_mut(|__s: &mut Owner| __s.p.take()) }, + p: field!(_a0, p).with_mut(|__v: &mut Option>| __v.take()), })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() @@ -407,9 +395,9 @@ impl Holder { } pub fn move_from(_a0: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - inner: { _a0.with(|__s: &Holder| (__s.inner).clone()) }, - e: { _a0.with(|__s: &Holder| (__s.e).clone()) }, - p: { _a0.with_mut(|__s: &mut Holder| __s.p.take()) }, + inner: { (*_a0.upgrade().deref()).inner.clone() }, + e: { (*_a0.upgrade().deref()).e.clone() }, + p: field!(_a0, p).with_mut(|__v: &mut Option>| __v.take()), })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() @@ -438,17 +426,17 @@ impl ByteRepr for Holder { >>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub fn same_0(a: Ptr, b: Ptr) -> bool { return ((({ - let _lhs = a.with(|__s: &Explicit| __s.v); - _lhs == b.with(|__s: &Explicit| __s.v) + let _lhs = a.with(|__s| __s.v); + _lhs == b.with(|__s| __s.v) }) && ({ - let _lhs = a.with(|__s: &Explicit| __s.inner.x); - _lhs == b.with(|__s: &Explicit| __s.inner.x) + let _lhs = a.with(|__s| __s.inner.x); + _lhs == b.with(|__s| __s.inner.x) })) && ({ - let _lhs = a.with(|__s: &Explicit| (*__s.arr.borrow())[(0) as usize]); - _lhs == b.with(|__s: &Explicit| (*__s.arr.borrow())[(0) as usize]) + let _lhs = (*a.with(|__s| __s.arr.clone()).borrow())[(0) as usize]; + _lhs == (*b.with(|__s| __s.arr.clone()).borrow())[(0) as usize] })) && ({ - let _lhs = a.with(|__s: &Explicit| (*__s.arr.borrow())[(1) as usize]); - _lhs == b.with(|__s: &Explicit| (*__s.arr.borrow())[(1) as usize]) + let _lhs = (*a.with(|__s| __s.arr.clone()).borrow())[(1) as usize]; + _lhs == (*b.with(|__s| __s.arr.clone()).borrow())[(1) as usize] }); } pub fn main() { @@ -498,7 +486,7 @@ fn main_0() -> i32 { let k: Value = Rc::new(RefCell::new((*i.borrow()).clone())); assert!( (({ (*j.borrow()).v } == 5) && ({ (*j.borrow()).inner.x } == 50)) - && ((*(*j.borrow()).arr.borrow())[(1) as usize] == 6) + && ((*{ (*j.borrow()).arr.clone() }.borrow())[(1) as usize] == 6) ); assert!(({ (*i.borrow()).v } == 5) && ({ (*k.borrow()).v } == 5)); let l: Value = Rc::new(RefCell::new(Implicit { @@ -509,7 +497,7 @@ fn main_0() -> i32 { (*l.borrow_mut()) = (*j.borrow()).clone(); assert!( (({ (*l.borrow()).v } == 5) && ({ (*l.borrow()).inner.x } == 50)) - && ((*(*l.borrow()).arr.borrow())[(0) as usize] == 5) + && ((*{ (*l.borrow()).arr.clone() }.borrow())[(0) as usize] == 5) ); let vec_: Value> = Rc::new(RefCell::new(Vec::new())); { @@ -518,14 +506,20 @@ fn main_0() -> i32 { }; (*vec_.borrow_mut()).push(Explicit::new({ 9 })); assert!( - ((vec_.as_pointer() as Ptr) - .offset(0_usize) - .with(|__s: &Explicit| __s.v) - == 1) - && ((vec_.as_pointer() as Ptr) - .offset(1_usize) - .with(|__s: &Explicit| __s.v) - == 9) + ({ + (*(vec_.as_pointer() as Ptr) + .offset(0_usize) + .upgrade() + .deref()) + .v + } == 1) + && ({ + (*(vec_.as_pointer() as Ptr) + .offset(1_usize) + .upgrade() + .deref()) + .v + } == 9) ); let m: Value = Rc::new(RefCell::new(DefaultCopyUserMove::new({ 7 }))); let m1: Value = Rc::new(RefCell::new((*m.borrow()).clone())); @@ -561,20 +555,25 @@ fn main_0() -> i32 { let p: Value = Rc::new(RefCell::new(Buffer::new({ 3 }))); let q: Value = Rc::new(RefCell::new(Buffer::move_from({ p.as_pointer() }))); assert!( - ((({ (*q.borrow()).n } == 3) && ((*(*q.borrow()).data.borrow()).len() == 3_usize)) - && ({ (*(*q.borrow()).data.borrow())[(2_usize) as usize] } == 3)) - && ((*(*p.borrow()).data.borrow()).is_empty()) + ((({ (*q.borrow()).n } == 3) + && ((*{ (*q.borrow()).data.clone() }.borrow()).len() == 3_usize)) + && ((({ (*q.borrow()).data.clone() }.as_pointer() as Ptr) + .offset(2_usize) + .read()) + == 3)) + && ((*{ (*p.borrow()).data.clone() }.borrow()).is_empty()) ); let r: Value = Rc::new(RefCell::new(Buffer::new({ 1 }))); ({ BufferImpl::move_assign(&r.as_pointer(), q.as_pointer()) }); assert!( - ((({ (*r.borrow()).n } == 3) && ((*(*r.borrow()).data.borrow()).len() == 3_usize)) - && ((*(*r.borrow()).arr.borrow())[(1) as usize] == 4)) - && ((*(*q.borrow()).data.borrow()).is_empty()) + ((({ (*r.borrow()).n } == 3) + && ((*{ (*r.borrow()).data.clone() }.borrow()).len() == 3_usize)) + && ((*{ (*r.borrow()).arr.clone() }.borrow())[(1) as usize] == 4)) + && ((*{ (*q.borrow()).data.clone() }.borrow()).is_empty()) ); assert!( - (((*(*r.borrow()).rows.borrow()).len() == 1_usize) - && ((*(({ (*r.borrow()).rows.as_pointer() } as Ptr>>) + (((*{ (*r.borrow()).rows.clone() }.borrow()).len() == 1_usize) + && ((*(({ (*r.borrow()).rows.clone() }.as_pointer() as Ptr>>) .offset(0_usize) .upgrade() .deref() @@ -583,7 +582,7 @@ fn main_0() -> i32 { .deref()) .len() == 3_usize)) - && ((*(*q.borrow()).rows.borrow()).is_empty()) + && ((*{ (*q.borrow()).rows.clone() }.borrow()).is_empty()) ); let bufs: Value> = Rc::new(RefCell::new(Vec::new())); (*bufs.borrow_mut()).push(Buffer::move_from({ r.as_pointer() })); @@ -592,72 +591,91 @@ fn main_0() -> i32 { (*bufs.borrow_mut()).push(__init) }; assert!( - (((bufs.as_pointer() as Ptr) - .offset(1_usize) - .with(|__s: &Buffer| __s.n) - == 3) - && ((*(bufs.as_pointer() as Ptr) + (({ + (*(bufs.as_pointer() as Ptr) .offset(1_usize) - .with(|__s: &Buffer| __s.data.clone()) - .borrow()) + .upgrade() + .deref()) + .n + } == 3) + && ((*{ + (*(bufs.as_pointer() as Ptr) + .offset(1_usize) + .upgrade() + .deref()) + .data + .clone() + } + .borrow()) .len() == 3_usize)) - && ((*(bufs.as_pointer() as Ptr) - .offset(0_usize) - .with(|__s: &Buffer| __s.data.clone()) - .borrow()) + && ((*{ + (*(bufs.as_pointer() as Ptr) + .offset(0_usize) + .upgrade() + .deref()) + .data + .clone() + } + .borrow()) .is_empty()) ); let o1: Value = Rc::new(RefCell::new(::default())); - (*(*o1.borrow()).data.borrow_mut()).push(5); + (*{ (*o1.borrow()).data.clone() }.borrow_mut()).push(5); (*o1.borrow_mut()).n = 5; - (*(*o1.borrow()).arr.borrow_mut())[(0) as usize] = 5; - (*(*o1.borrow()).arr.borrow_mut())[(1) as usize] = 6; + (*{ (*o1.borrow()).arr.clone() }.borrow_mut())[(0) as usize] = 5; + (*{ (*o1.borrow()).arr.clone() }.borrow_mut())[(1) as usize] = 6; { let _p: Ptr<_> = Ptr::alloc(7); - (*o1.borrow_mut()).p = _p.to_owned_opt() + (field_ptr!(o1.as_pointer(), p) as Ptr>>).write(_p.to_owned_opt()) }; let o2: Value = Rc::new(RefCell::new(Owner::move_from({ o1.as_pointer() }))); assert!( - ((({ (*o2.borrow()).n } == 5) && ((*(*o2.borrow()).data.borrow()).len() == 1_usize)) - && ((*(*o2.borrow()).arr.borrow())[(1) as usize] == 6)) - && ((*(*o2.borrow()).p.as_ref().unwrap().borrow()) == 7) + ((({ (*o2.borrow()).n } == 5) + && ((*{ (*o2.borrow()).data.clone() }.borrow()).len() == 1_usize)) + && ((*{ (*o2.borrow()).arr.clone() }.borrow())[(1) as usize] == 6)) + && ((*{ (*o2.borrow()).p.clone() }.as_ref().unwrap().borrow()) == 7) ); assert!( - ((*(*o1.borrow()).data.borrow()).is_empty()) && (((*o1.borrow()).p.as_pointer()).is_null()) + ((*{ (*o1.borrow()).data.clone() }.borrow()).is_empty()) + && (({ (*o1.borrow()).p.clone() }.as_pointer()).is_null()) ); let o3: Value = Rc::new(RefCell::new(::default())); ({ OwnerImpl::move_assign(&o3.as_pointer(), o2.as_pointer()) }); assert!( ((({ (*o3.borrow()).n } == 5) - && ({ (*(*o3.borrow()).data.borrow())[(0_usize) as usize] } == 5)) - && ((*(*o3.borrow()).arr.borrow())[(0) as usize] == 5)) - && ((*(*o3.borrow()).p.as_ref().unwrap().borrow()) == 7) + && ((({ (*o3.borrow()).data.clone() }.as_pointer() as Ptr) + .offset(0_usize) + .read()) + == 5)) + && ((*{ (*o3.borrow()).arr.clone() }.borrow())[(0) as usize] == 5)) + && ((*{ (*o3.borrow()).p.clone() }.as_ref().unwrap().borrow()) == 7) ); assert!( - ((*(*o2.borrow()).data.borrow()).is_empty()) && (((*o2.borrow()).p.as_pointer()).is_null()) + ((*{ (*o2.borrow()).data.clone() }.borrow()).is_empty()) + && (({ (*o2.borrow()).p.clone() }.as_pointer()).is_null()) ); let h1: Value = Rc::new(RefCell::new(Holder::new({ 4 }))); let _dtor_h1 = ScopedDestructor::new(&h1, |__p| __p.destructor()); { let _p: Ptr<_> = Ptr::alloc(9); - (*h1.borrow_mut()).p = _p.to_owned_opt() + (field_ptr!(h1.as_pointer(), p) as Ptr>>).write(_p.to_owned_opt()) }; let h2: Value = Rc::new(RefCell::new(Holder::move_from({ h1.as_pointer() }))); let _dtor_h2 = ScopedDestructor::new(&h2, |__p| __p.destructor()); assert!( ((({ (*h2.borrow()).inner.x } == 4) && ({ (*h2.borrow()).e.v } == 4)) - && ((*(*h2.borrow()).p.as_ref().unwrap().borrow()) == 9)) - && (((*h1.borrow()).p.as_pointer()).is_null()) + && ((*{ (*h2.borrow()).p.clone() }.as_ref().unwrap().borrow()) == 9)) + && (({ (*h1.borrow()).p.clone() }.as_pointer()).is_null()) ); let h3: Value = Rc::new(RefCell::new(Holder::new({ 1 }))); let _dtor_h3 = ScopedDestructor::new(&h3, |__p| __p.destructor()); ({ HolderImpl::move_assign(&h3.as_pointer(), h2.as_pointer()) }); assert!( ((({ (*h3.borrow()).inner.x } == 4) - && ((*(*h3.borrow()).e.arr.borrow())[(1) as usize] == 5)) - && ((*(*h3.borrow()).p.as_ref().unwrap().borrow()) == 9)) - && (((*h2.borrow()).p.as_pointer()).is_null()) + && ((*{ (*h3.borrow()).e.arr.clone() }.borrow())[(1) as usize] == 5)) + && ((*{ (*h3.borrow()).p.clone() }.as_ref().unwrap().borrow()) == 9)) + && (({ (*h2.borrow()).p.clone() }.as_pointer()).is_null()) ); return 0; } @@ -666,20 +684,22 @@ pub trait BufferImpl { } impl BufferImpl for Ptr { fn move_assign(&self, _a0: Ptr) -> Ptr { - ((*self).with(|__s: &Buffer| __s.data.as_pointer()) as Ptr>) - .write(_a0.with(|__s: &Buffer| std::mem::take(&mut (*__s.data.borrow_mut())))); - ((*self).with(|__s: &Buffer| __s.rows.as_pointer()) as Ptr>>>) - .write(_a0.with(|__s: &Buffer| std::mem::take(&mut (*__s.rows.borrow_mut())))); - let __rhs = _a0.with(|__s: &Buffer| __s.n); - (*self).with_mut(|__s: &mut Buffer| __s.n = __rhs); + ((*self).with(|__s| __s.data.clone()).as_pointer() as Ptr>).write(std::mem::take( + &mut (*{ (*_a0.upgrade().deref()).data.clone() }.borrow_mut()), + )); + ((*self).with(|__s| __s.rows.clone()).as_pointer() as Ptr>>>).write( + std::mem::take(&mut (*{ (*_a0.upgrade().deref()).rows.clone() }.borrow_mut())), + ); + let __rhs = { (*_a0.upgrade().deref()).n }; + field!((*self), n).write(__rhs); { - (((*self).with(|__s: &Buffer| __s.arr.as_pointer())) as Ptr) + (((*self).with(|__s| __s.arr.clone()).as_pointer()) as Ptr) .to_any() .memcpy( - &((_a0.with(|__s: &Buffer| __s.arr.as_pointer())) as Ptr).to_any(), + &(({ (*_a0.upgrade().deref()).arr.clone() }.as_pointer()) as Ptr).to_any(), 8_usize as usize, ); - (((*self).with(|__s: &Buffer| __s.arr.as_pointer())) as Ptr).to_any() + (((*self).with(|__s| __s.arr.clone()).as_pointer()) as Ptr).to_any() }; return (*self).clone(); } @@ -689,9 +709,9 @@ pub trait DefaultCopyUserMoveImpl { } impl DefaultCopyUserMoveImpl for Ptr { fn move_assign(&self, o: Ptr) -> Ptr { - let __rhs = o.with(|__s: &DefaultCopyUserMove| __s.v); - (*self).with_mut(|__s: &mut DefaultCopyUserMove| __s.v = __rhs); - o.with_mut(|__s: &mut DefaultCopyUserMove| __s.v = 0); + let __rhs = o.with(|__s| __s.v); + field!((*self), v).write(__rhs); + field!(o, v).write(0); return (*self).clone(); } } @@ -707,16 +727,16 @@ pub trait HolderImpl { } impl HolderImpl for Ptr { fn move_assign(&self, _a0: Ptr) -> Ptr { - let __rhs = _a0.with(|__s: &Holder| (__s.inner).clone()); - (*self).with_mut(|__s: &mut Holder| __s.inner = __rhs); - let __rhs = _a0.with(|__s: &Holder| (__s.e).clone()); - (*self).with_mut(|__s: &mut Holder| __s.e = __rhs); + let __rhs = { (*_a0.upgrade().deref()).inner.clone() }; + field!((*self), inner).write(__rhs); + let __rhs = { (*_a0.upgrade().deref()).e.clone() }; + field!((*self), e).write(__rhs); (field_ptr!((*self), p) as Ptr>>) - .write(_a0.with_mut(|__s: &mut Holder| __s.p.take())); + .write(field!(_a0, p).with_mut(|__v: &mut Option>| __v.take())); return (*self).clone(); } fn destructor(&self) { - ExplicitImpl::destructor(&field_ptr!(self, e)); + field_ptr!(self, e).destructor(); } } pub trait OwnerImpl { @@ -724,21 +744,22 @@ pub trait OwnerImpl { } impl OwnerImpl for Ptr { fn move_assign(&self, _a0: Ptr) -> Ptr { - ((*self).with(|__s: &Owner| __s.data.as_pointer()) as Ptr>) - .write(_a0.with(|__s: &Owner| std::mem::take(&mut (*__s.data.borrow_mut())))); - let __rhs = _a0.with(|__s: &Owner| __s.n); - (*self).with_mut(|__s: &mut Owner| __s.n = __rhs); + ((*self).with(|__s| __s.data.clone()).as_pointer() as Ptr>).write(std::mem::take( + &mut (*{ (*_a0.upgrade().deref()).data.clone() }.borrow_mut()), + )); + let __rhs = { (*_a0.upgrade().deref()).n }; + field!((*self), n).write(__rhs); { - (((*self).with(|__s: &Owner| __s.arr.as_pointer())) as Ptr) + (((*self).with(|__s| __s.arr.clone()).as_pointer()) as Ptr) .to_any() .memcpy( - &((_a0.with(|__s: &Owner| __s.arr.as_pointer())) as Ptr).to_any(), + &(({ (*_a0.upgrade().deref()).arr.clone() }.as_pointer()) as Ptr).to_any(), 8_usize as usize, ); - (((*self).with(|__s: &Owner| __s.arr.as_pointer())) as Ptr).to_any() + (((*self).with(|__s| __s.arr.clone()).as_pointer()) as Ptr).to_any() }; (field_ptr!((*self), p) as Ptr>>) - .write(_a0.with_mut(|__s: &mut Owner| __s.p.take())); + .write(field!(_a0, p).with_mut(|__v: &mut Option>| __v.take())); return (*self).clone(); } } @@ -748,13 +769,13 @@ pub trait UserCopyDefaultMoveImpl { } impl UserCopyDefaultMoveImpl for Ptr { fn copy_assign(&self, o: Ptr) -> Ptr { - let __rhs = (o.with(|__s: &UserCopyDefaultMove| __s.v) + 100); - (*self).with_mut(|__s: &mut UserCopyDefaultMove| __s.v = __rhs); + let __rhs = (o.with(|__s| __s.v) + 100); + field!((*self), v).write(__rhs); return (*self).clone(); } fn move_assign(&self, _a0: Ptr) -> Ptr { - let __rhs = _a0.with(|__s: &UserCopyDefaultMove| __s.v); - (*self).with_mut(|__s: &mut UserCopyDefaultMove| __s.v = __rhs); + let __rhs = { (*_a0.upgrade().deref()).v }; + field!((*self), v).write(__rhs); return (*self).clone(); } } diff --git a/tests/unit/out/refcount/copy_move_deleted.rs b/tests/unit/out/refcount/copy_move_deleted.rs index 1c76cc9e5f..6aa0529bdf 100644 --- a/tests/unit/out/refcount/copy_move_deleted.rs +++ b/tests/unit/out/refcount/copy_move_deleted.rs @@ -20,10 +20,10 @@ impl NoCopy { } pub fn move_from(o: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - v: { o.with(|__s: &NoCopy| __s.v) }, + v: o.with(|__s| __s.v), })); let this: Ptr = __this.as_pointer(); - o.with_mut(|__s: &mut NoCopy| __s.v = 0); + field!(o, v).write(0); Rc::try_unwrap(__this).ok().unwrap().into_inner() } } @@ -58,10 +58,10 @@ impl PrivateCopy { } pub fn move_from(o: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - v: { o.with(|__s: &PrivateCopy| __s.v) }, + v: o.with(|__s| __s.v), })); let this: Ptr = __this.as_pointer(); - o.with_mut(|__s: &mut PrivateCopy| __s.v = 0); + field!(o, v).write(0); Rc::try_unwrap(__this).ok().unwrap().into_inner() } } @@ -128,8 +128,8 @@ pub struct Container { impl Container { pub fn move_from(_a0: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - inner: { NoCopy::move_from({ field_ptr!(_a0, inner) }) }, - tag: { _a0.with(|__s: &Container| __s.tag) }, + inner: NoCopy::move_from({ field_ptr!(_a0, inner) }), + tag: { (*_a0.upgrade().deref()).tag }, })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() @@ -156,10 +156,10 @@ impl ByteRepr for Container { >>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub fn bump_0(p: Ptr) { let p: Value> = Rc::new(RefCell::new(p)); - (*p.borrow()).with_mut(|__s: &mut NoCopy| __s.v.postfix_inc()); + field!((*p.borrow()), v).with_mut(|__v| __v.postfix_inc()); } pub fn bump_ref_1(r: Ptr) { - r.with_mut(|__s: &mut Immovable| __s.v.postfix_inc()); + field!(r, v).with_mut(|__v| __v.postfix_inc()); } pub fn main() { __cpp2rust_init_globals(); @@ -183,7 +183,7 @@ fn main_0() -> i32 { (*im.borrow_mut()).v = 4; ({ bump_ref_1(im.as_pointer()) }); let pim: Value> = Rc::new(RefCell::new((im.as_pointer()))); - assert!(((*pim.borrow()).with(|__s: &Immovable| __s.v) == 5)); + assert!(((*pim.borrow()).with(|__s| __s.v) == 5)); let c: Value = Rc::new(RefCell::new(Container { inner: NoCopy::new({ 6 }), tag: 7, @@ -202,10 +202,10 @@ impl ContainerImpl for Ptr { fn move_assign(&self, _a0: Ptr) -> Ptr { ({ let _o: Ptr = field_ptr!(_a0, inner); - NoCopyImpl::move_assign(&{ field_ptr!((*self), inner) }, _o) + NoCopyImpl::move_assign(&field_ptr!((*self), inner), _o) }); - let __rhs = _a0.with(|__s: &Container| __s.tag); - (*self).with_mut(|__s: &mut Container| __s.tag = __rhs); + let __rhs = { (*_a0.upgrade().deref()).tag }; + field!((*self), tag).write(__rhs); return (*self).clone(); } } @@ -214,9 +214,9 @@ pub trait NoCopyImpl { } impl NoCopyImpl for Ptr { fn move_assign(&self, o: Ptr) -> Ptr { - let __rhs = o.with(|__s: &NoCopy| __s.v); - (*self).with_mut(|__s: &mut NoCopy| __s.v = __rhs); - o.with_mut(|__s: &mut NoCopy| __s.v = 0); + let __rhs = o.with(|__s| __s.v); + field!((*self), v).write(__rhs); + field!(o, v).write(0); return (*self).clone(); } } @@ -228,9 +228,9 @@ pub trait PrivateCopyImpl { } impl PrivateCopyImpl for Ptr { fn move_assign(&self, o: Ptr) -> Ptr { - let __rhs = o.with(|__s: &PrivateCopy| __s.v); - (*self).with_mut(|__s: &mut PrivateCopy| __s.v = __rhs); - o.with_mut(|__s: &mut PrivateCopy| __s.v = 0); + let __rhs = o.with(|__s| __s.v); + field!((*self), v).write(__rhs); + field!(o, v).write(0); return (*self).clone(); } } diff --git a/tests/unit/out/refcount/default.rs b/tests/unit/out/refcount/default.rs index 3e4443a3b7..d7ca262c59 100644 --- a/tests/unit/out/refcount/default.rs +++ b/tests/unit/out/refcount/default.rs @@ -21,19 +21,13 @@ pub struct Pointers { } impl Clone for Pointers { fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - x1: { self.x1.clone() }, - x2: { self.x2.clone() }, - x3: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 5, _>( - |__i: usize| ((*self.x3.borrow())[(__i) as usize]).clone(), - )))), - x4: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 10, _>( - |__i: usize| ((*self.x4.borrow())[(__i) as usize]).clone(), - )))), - x5: { self.x5 }, - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() + Self { + x1: self.x1.clone(), + x2: self.x2.clone(), + x3: Rc::new(RefCell::new((*self.x3.borrow()).clone())), + x4: Rc::new(RefCell::new((*self.x4.borrow()).clone())), + x5: self.x5.clone(), + } } } impl Default for Pointers { diff --git a/tests/unit/out/refcount/default_in_statics.rs b/tests/unit/out/refcount/default_in_statics.rs index 622105a847..9ba87d6316 100644 --- a/tests/unit/out/refcount/default_in_statics.rs +++ b/tests/unit/out/refcount/default_in_statics.rs @@ -49,20 +49,16 @@ pub struct Outer { } impl Clone for Outer { fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - p1: { self.p1.clone() }, - p2: { self.p2.clone() }, - arr: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 3, _>( - |__i: usize| ((*self.arr.borrow())[(__i) as usize]).clone(), - )))), - cp: { self.cp.clone() }, - pp: { self.pp.clone() }, - inner: { self.inner.clone() }, - x: { self.x }, - fn_: { self.fn_.clone() }, - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() + Self { + p1: self.p1.clone(), + p2: self.p2.clone(), + arr: Rc::new(RefCell::new((*self.arr.borrow()).clone())), + cp: self.cp.clone(), + pp: self.pp.clone(), + inner: self.inner.clone(), + x: self.x.clone(), + fn_: self.fn_.clone(), + } } } impl Default for Outer { @@ -224,7 +220,8 @@ fn main_0() -> i32 { let i: Value = Rc::new(RefCell::new(0)); 'loop_: while ((*i.borrow()) < 3) { assert!( - ((*(*static_outer_1.with(Value::clone).borrow()).arr.borrow())[(*i.borrow()) as usize]) + ((*{ (*static_outer_1.with(Value::clone).borrow()).arr.clone() }.borrow()) + [(*i.borrow()) as usize]) .is_null() ); (*i.borrow_mut()).prefix_inc(); diff --git a/tests/unit/out/refcount/destructor.rs b/tests/unit/out/refcount/destructor.rs index cc249ada1b..bbee36edea 100644 --- a/tests/unit/out/refcount/destructor.rs +++ b/tests/unit/out/refcount/destructor.rs @@ -91,13 +91,9 @@ pub struct ArrayMember { } impl Clone for ArrayMember { fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - items: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 3, _>( - |__i: usize| (*self.items.borrow())[(__i) as usize].clone(), - )))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() + Self { + items: Rc::new(RefCell::new((*self.items.borrow()).clone())), + } } } impl Default for ArrayMember { @@ -373,7 +369,7 @@ pub trait ArrayMemberImpl { impl ArrayMemberImpl for Ptr { fn destructor(&self) { { - let __p: Ptr = self.with(|__s| __s.items.as_pointer()); + let __p = (*self.upgrade().deref()).items.as_pointer(); for __i in 0..__p.len() { SImpl::destructor(&__p.offset(__i as isize)); } @@ -393,7 +389,7 @@ pub trait DefaultedImpl { } impl DefaultedImpl for Ptr { fn destructor(&self) { - SImpl::destructor(&field_ptr!(self, s)); + field_ptr!(self, s).destructor(); } } pub trait EmptyBodyImpl { @@ -401,7 +397,7 @@ pub trait EmptyBodyImpl { } impl EmptyBodyImpl for Ptr { fn destructor(&self) { - SImpl::destructor(&field_ptr!(self, s)); + field_ptr!(self, s).destructor(); } } pub trait MiddleImpl { @@ -409,7 +405,7 @@ pub trait MiddleImpl { } impl MiddleImpl for Ptr { fn destructor(&self) { - SImpl::destructor(&field_ptr!(self, s)); + field_ptr!(self, s).destructor(); } } pub trait OrderedImpl { @@ -417,9 +413,9 @@ pub trait OrderedImpl { } impl OrderedImpl for Ptr { fn destructor(&self) { - TaggedImpl::destructor(&field_ptr!(self, third)); - TaggedImpl::destructor(&field_ptr!(self, second)); - TaggedImpl::destructor(&field_ptr!(self, first)); + field_ptr!(self, third).destructor(); + field_ptr!(self, second).destructor(); + field_ptr!(self, first).destructor(); } } pub trait OuterImpl { @@ -427,7 +423,7 @@ pub trait OuterImpl { } impl OuterImpl for Ptr { fn destructor(&self) { - MiddleImpl::destructor(&field_ptr!(self, m)); + field_ptr!(self, m).destructor(); } } pub trait SImpl { @@ -443,9 +439,9 @@ pub trait TaggedImpl { } impl TaggedImpl for Ptr { fn destructor(&self) { - let __rhs = (*self).with(|__s: &Tagged| __s.tag); - let __idx = (*order_count_2.with(Value::clone).borrow_mut()).postfix_inc(); - (*order_1.with(Value::clone).borrow_mut())[(__idx) as usize] = __rhs; + (*order_1.with(Value::clone).borrow_mut()) + [((*order_count_2.with(Value::clone).borrow_mut()).postfix_inc()) as usize] = + (*self).with(|__s| __s.tag); } } pub trait Templated_char_Impl { diff --git a/tests/unit/out/refcount/doubly_linked_list.rs b/tests/unit/out/refcount/doubly_linked_list.rs index 3f886084ee..00b85e8509 100644 --- a/tests/unit/out/refcount/doubly_linked_list.rs +++ b/tests/unit/out/refcount/doubly_linked_list.rs @@ -38,7 +38,7 @@ pub fn Find_0(head: Ptr, idx: i32) -> Ptr { let curr: Value> = Rc::new(RefCell::new((*head.borrow()).clone())); let i: Value = Rc::new(RefCell::new(0)); 'loop_: while ((*i.borrow()) < (*idx.borrow())) { - let __rhs = (*curr.borrow()).with(|__s: &Node| (__s.next).clone()); + let __rhs = (*curr.borrow()).with(|__s| __s.next.clone()); (*curr.borrow_mut()) = __rhs; (*i.borrow_mut()).postfix_inc(); } @@ -50,7 +50,7 @@ pub fn FindBack_1(tail: Ptr, idx: i32) -> Ptr { let curr: Value> = Rc::new(RefCell::new((*tail.borrow()).clone())); let i: Value = Rc::new(RefCell::new(0)); 'loop_: while ((*i.borrow()) < (*idx.borrow())) { - let __rhs = (*curr.borrow()).with(|__s: &Node| (__s.prev).clone()); + let __rhs = (*curr.borrow()).with(|__s| __s.prev.clone()); (*curr.borrow_mut()) = __rhs; (*i.borrow_mut()).postfix_inc(); } @@ -58,8 +58,8 @@ pub fn FindBack_1(tail: Ptr, idx: i32) -> Ptr { } pub fn Append_2(head: Ptr, new_node: Ptr) { let curr: Value> = Rc::new(RefCell::new((head).clone())); - 'loop_: while !(((*curr.borrow()).with(|__s: &Node| (__s.next).clone())).is_null()) { - let __rhs = (*curr.borrow()).with(|__s: &Node| (__s.next).clone()); + 'loop_: while !(((*curr.borrow()).with(|__s| __s.next.clone())).is_null()) { + let __rhs = (*curr.borrow()).with(|__s| __s.next.clone()); (*curr.borrow_mut()) = __rhs; } ({ NodeImpl::SetNext(&(*curr.borrow()), (new_node).clone()) }); @@ -74,20 +74,18 @@ pub fn Delete_3(head: Ptr, val: i32) -> Ptr { let curr: Value> = Rc::new(RefCell::new((*head.borrow()).clone())); 'loop_: while !((*curr.borrow()).is_null()) { if { - let _lhs = (*curr.borrow()).with(|__s: &Node| __s.val); + let _lhs = (*curr.borrow()).with(|__s| __s.val); _lhs == (*val.borrow()) } { - let prev: Value> = Rc::new(RefCell::new( - (*curr.borrow()).with(|__s: &Node| (__s.prev).clone()), - )); - let next: Value> = Rc::new(RefCell::new( - (*curr.borrow()).with(|__s: &Node| (__s.next).clone()), - )); + let prev: Value> = + Rc::new(RefCell::new((*curr.borrow()).with(|__s| __s.prev.clone()))); + let next: Value> = + Rc::new(RefCell::new((*curr.borrow()).with(|__s| __s.next.clone()))); if !((*prev.borrow()).is_null()) { - (*prev.borrow()).with_mut(|__s: &mut Node| __s.next = (*next.borrow()).clone()); + field!((*prev.borrow()), next).write((*next.borrow()).clone()); } if !((*next.borrow()).is_null()) { - (*next.borrow()).with_mut(|__s: &mut Node| __s.prev = (*prev.borrow()).clone()); + field!((*next.borrow()), prev).write((*prev.borrow()).clone()); } if !((*prev.borrow()).is_null()) { return (*head.borrow()).clone(); @@ -95,7 +93,7 @@ pub fn Delete_3(head: Ptr, val: i32) -> Ptr { return (*next.borrow()).clone(); } } - let __rhs = (*curr.borrow()).with(|__s: &Node| (__s.next).clone()); + let __rhs = (*curr.borrow()).with(|__s| __s.next.clone()); (*curr.borrow_mut()) = __rhs; } return (*head.borrow()).clone(); @@ -103,8 +101,8 @@ pub fn Delete_3(head: Ptr, val: i32) -> Ptr { pub fn Tail_4(head: Ptr) -> Ptr { let head: Value> = Rc::new(RefCell::new(head)); let curr: Value> = Rc::new(RefCell::new((*head.borrow()).clone())); - 'loop_: while !(((*curr.borrow()).with(|__s: &Node| (__s.next).clone())).is_null()) { - let __rhs = (*curr.borrow()).with(|__s: &Node| (__s.next).clone()); + 'loop_: while !(((*curr.borrow()).with(|__s| __s.next.clone())).is_null()) { + let __rhs = (*curr.borrow()).with(|__s| __s.next.clone()); (*curr.borrow_mut()) = __rhs; } return (*curr.borrow()).clone(); @@ -197,84 +195,84 @@ fn main_0() -> i32 { let __rhs = ({ Delete_3((*head.borrow()).clone(), -2_i32) }); (*head.borrow_mut()) = __rhs; let tail: Value> = Rc::new(RefCell::new(({ Tail_4((*head.borrow()).clone()) }))); - assert!((({ Find_0((*head.borrow()).clone(), 0,) }).with(|__s: &Node| __s.val) == 4)); - assert!((({ Find_0((*head.borrow()).clone(), 1,) }).with(|__s: &Node| __s.val) == 3)); - assert!((({ Find_0((*head.borrow()).clone(), 2,) }).with(|__s: &Node| __s.val) == 2)); - assert!((({ Find_0((*head.borrow()).clone(), 3,) }).with(|__s: &Node| __s.val) == 1)); - assert!((({ Find_0((*head.borrow()).clone(), 4,) }).with(|__s: &Node| __s.val) == -1_i32)); + assert!((({ Find_0((*head.borrow()).clone(), 0,) }).with(|__s| __s.val) == 4)); + assert!((({ Find_0((*head.borrow()).clone(), 1,) }).with(|__s| __s.val) == 3)); + assert!((({ Find_0((*head.borrow()).clone(), 2,) }).with(|__s| __s.val) == 2)); + assert!((({ Find_0((*head.borrow()).clone(), 3,) }).with(|__s| __s.val) == 1)); + assert!((({ Find_0((*head.borrow()).clone(), 4,) }).with(|__s| __s.val) == -1_i32)); assert!(({ Find_0((*head.borrow()).clone(), 5,) }).is_null()); - assert!((({ FindBack_1((*tail.borrow()).clone(), 0,) }).with(|__s: &Node| __s.val) == -1_i32)); - assert!((({ FindBack_1((*tail.borrow()).clone(), 1,) }).with(|__s: &Node| __s.val) == 1)); - assert!((({ FindBack_1((*tail.borrow()).clone(), 2,) }).with(|__s: &Node| __s.val) == 2)); - assert!((({ FindBack_1((*tail.borrow()).clone(), 3,) }).with(|__s: &Node| __s.val) == 3)); - assert!((({ FindBack_1((*tail.borrow()).clone(), 4,) }).with(|__s: &Node| __s.val) == 4)); + assert!((({ FindBack_1((*tail.borrow()).clone(), 0,) }).with(|__s| __s.val) == -1_i32)); + assert!((({ FindBack_1((*tail.borrow()).clone(), 1,) }).with(|__s| __s.val) == 1)); + assert!((({ FindBack_1((*tail.borrow()).clone(), 2,) }).with(|__s| __s.val) == 2)); + assert!((({ FindBack_1((*tail.borrow()).clone(), 3,) }).with(|__s| __s.val) == 3)); + assert!((({ FindBack_1((*tail.borrow()).clone(), 4,) }).with(|__s| __s.val) == 4)); assert!( - (({ FindBack_1((*tail.borrow()).clone(), 4,) }).with(|__s: &Node| (__s.prev).clone())) - .is_null() + (({ FindBack_1((*tail.borrow()).clone(), 4,) }).with(|__s| __s.prev.clone())).is_null() ); assert!( (({ Find_0((*head.borrow()).clone(), 0,) }) - .with(|__s: &Node| (__s.next).clone()) - .with(|__s: &Node| __s.val) + .with(|__s| __s.next.clone()) + .with(|__s| __s.val) == 3) ); assert!( (({ Find_0((*head.borrow()).clone(), 1,) }) - .with(|__s: &Node| (__s.next).clone()) - .with(|__s: &Node| (__s.next).clone()) - .with(|__s: &Node| __s.val) + .with(|__s| __s.next.clone()) + .with(|__s| __s.next.clone()) + .with(|__s| __s.val) == 1) ); assert!( (({ Find_0((*head.borrow()).clone(), 2,) }) - .with(|__s: &Node| (__s.prev).clone()) - .with(|__s: &Node| __s.val) + .with(|__s| __s.prev.clone()) + .with(|__s| __s.val) == 3) ); - assert!( - (({ Find_0((*head.borrow()).clone(), 4,) }).with(|__s: &Node| (__s.next).clone())) - .is_null() - ); + assert!((({ Find_0((*head.borrow()).clone(), 4,) }).with(|__s| __s.next.clone())).is_null()); assert!( (({ FindBack_1((*tail.borrow()).clone(), 1,) }) - .with(|__s: &Node| (__s.prev).clone()) - .with(|__s: &Node| (__s.prev).clone()) - .with(|__s: &Node| __s.val) + .with(|__s| __s.prev.clone()) + .with(|__s| __s.prev.clone()) + .with(|__s| __s.val) == 3) ); - ({ Find_0((*head.borrow()).clone(), 0) }) - .with(|__s: &Node| (__s.next).clone()) - .with_mut(|__s: &mut Node| __s.val = 30); - assert!((({ Find_0((*head.borrow()).clone(), 1,) }).with(|__s: &Node| __s.val) == 30)); - let __rhs = (({ Find_0((*head.borrow()).clone(), 0) }).with(|__s: &Node| __s.val) - + ({ Find_0((*head.borrow()).clone(), 3) }).with(|__s: &Node| __s.val)); - ({ Find_0((*head.borrow()).clone(), 1) }) - .with(|__s: &Node| (__s.next).clone()) - .with_mut(|__s: &mut Node| __s.val = __rhs); - assert!((({ Find_0((*head.borrow()).clone(), 2,) }).with(|__s: &Node| __s.val) == (4 + 1))); + field!( + ({ Find_0((*head.borrow()).clone(), 0,) }).with(|__s| __s.next.clone()), + val + ) + .write(30); + assert!((({ Find_0((*head.borrow()).clone(), 1,) }).with(|__s| __s.val) == 30)); + let __rhs = (({ Find_0((*head.borrow()).clone(), 0) }).with(|__s| __s.val) + + ({ Find_0((*head.borrow()).clone(), 3) }).with(|__s| __s.val)); + field!( + ({ Find_0((*head.borrow()).clone(), 1,) }).with(|__s| __s.next.clone()), + val + ) + .write(__rhs); + assert!((({ Find_0((*head.borrow()).clone(), 2,) }).with(|__s| __s.val) == (4 + 1))); let sum: Value = Rc::new(RefCell::new( - ((((({ Find_0((*head.borrow()).clone(), 0) }).with(|__s: &Node| __s.val) - + ({ Find_0((*head.borrow()).clone(), 1) }).with(|__s: &Node| __s.val)) - + ({ Find_0((*head.borrow()).clone(), 2) }).with(|__s: &Node| __s.val)) - + ({ Find_0((*head.borrow()).clone(), 3) }).with(|__s: &Node| __s.val)) - + ({ Find_0((*head.borrow()).clone(), 4) }).with(|__s: &Node| __s.val)), + ((((({ Find_0((*head.borrow()).clone(), 0) }).with(|__s| __s.val) + + ({ Find_0((*head.borrow()).clone(), 1) }).with(|__s| __s.val)) + + ({ Find_0((*head.borrow()).clone(), 2) }).with(|__s| __s.val)) + + ({ Find_0((*head.borrow()).clone(), 3) }).with(|__s| __s.val)) + + ({ Find_0((*head.borrow()).clone(), 4) }).with(|__s| __s.val)), )); assert!(((*sum.borrow()) == ((((4 + 30) + 5) + 1) + -1_i32))); assert!( ({ - let _lhs = ({ Find_0((*head.borrow()).clone(), 0) }).with(|__s: &Node| __s.val); - _lhs + ({ FindBack_1((*tail.borrow()).clone(), 0) }).with(|__s: &Node| __s.val) + let _lhs = ({ Find_0((*head.borrow()).clone(), 0) }).with(|__s| __s.val); + _lhs + ({ FindBack_1((*tail.borrow()).clone(), 0) }).with(|__s| __s.val) } == (4 + -1_i32)) ); assert!({ let _lhs = ({ Find_0((*head.borrow()).clone(), 2) }) - .with(|__s: &Node| (__s.next).clone()) - .with(|__s: &Node| __s.val); - _lhs == ({ FindBack_1((*tail.borrow()).clone(), 1) }).with(|__s: &Node| __s.val) + .with(|__s| __s.next.clone()) + .with(|__s| __s.val); + _lhs == ({ FindBack_1((*tail.borrow()).clone(), 1) }).with(|__s| __s.val) }); assert!({ - let _lhs = ({ Find_0((*head.borrow()).clone(), 0) }).with(|__s: &Node| (__s.prev).clone()); - _lhs == ({ FindBack_1((*tail.borrow()).clone(), 4) }).with(|__s: &Node| (__s.prev).clone()) + let _lhs = ({ Find_0((*head.borrow()).clone(), 0) }).with(|__s| __s.prev.clone()); + _lhs == ({ FindBack_1((*tail.borrow()).clone(), 4) }).with(|__s| __s.prev.clone()) }); return 0; } @@ -285,11 +283,11 @@ pub trait NodeImpl { impl NodeImpl for Ptr { fn SetNext(&self, n: Ptr) { let n: Value> = Rc::new(RefCell::new(n)); - (*self).with_mut(|__s: &mut Node| __s.next = (*n.borrow()).clone()); + field!((*self), next).write((*n.borrow()).clone()); } fn SetPrev(&self, p: Ptr) { let p: Value> = Rc::new(RefCell::new(p)); - (*self).with_mut(|__s: &mut Node| __s.prev = (*p.borrow()).clone()); + field!((*self), prev).write((*p.borrow()).clone()); } } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/enum_int_interop_c.rs b/tests/unit/out/refcount/enum_int_interop_c.rs index 69214383b2..9373b114e3 100644 --- a/tests/unit/out/refcount/enum_int_interop_c.rs +++ b/tests/unit/out/refcount/enum_int_interop_c.rs @@ -271,8 +271,7 @@ fn main_0() -> i32 { ((entries_3.with(|v| v.as_pointer()) as Ptr).offset((*idx.borrow()) as isize)), )); assert!( - (((((*pe.borrow()).with(|__s: &Entry| __s.opt) as u32) == ((Option_OPT_A as i32) as u32)) - as i32) + (((((*pe.borrow()).with(|__s| __s.opt) as u32) == ((Option_OPT_A as i32) as u32)) as i32) != 0) ); return 0; diff --git a/tests/unit/out/refcount/exprs.rs b/tests/unit/out/refcount/exprs.rs index 036d41a90d..c8de851a9e 100644 --- a/tests/unit/out/refcount/exprs.rs +++ b/tests/unit/out/refcount/exprs.rs @@ -90,14 +90,12 @@ fn main_0() -> i32 { p: (x.as_pointer()), })); (*y.borrow_mut()).x.x = 5; - ({ YImpl::foo(&y.as_pointer()) }).with_mut(|__s: &mut X| __s.x = 1); - { (*y.borrow()).p.clone() }.with_mut(|__s: &mut X| __s.x = 10); + field!(({ YImpl::foo(&y.as_pointer(),) }), x).write(1); + field!({ (*y.borrow()).p.clone() }, x).write(10); let p3: Value> = Rc::new(RefCell::new((y.as_pointer()))); - (*p3.borrow()) - .with(|__s: &Y| (__s.p).clone()) - .with_mut(|__s: &mut X| __s.x = 100); - ({ YImpl::ptr(&y.as_pointer()) }).with_mut(|__s: &mut X| __s.x = 1); - ({ YImpl::ptr(&y.as_pointer()) }).with_mut(|__s: &mut X| __s.x = 50); + field!((*p3.borrow()).with(|__s| __s.p.clone()), x).write(100); + field!(({ YImpl::ptr(&y.as_pointer(),) }), x).write(1); + field!(({ YImpl::ptr(&y.as_pointer(),) }), x).write(50); assert!(({ (*x.borrow()).x } == 100)); return 0; } diff --git a/tests/unit/out/refcount/fft.rs b/tests/unit/out/refcount/fft.rs index 7e20aa61b1..bfee5b5955 100644 --- a/tests/unit/out/refcount/fft.rs +++ b/tests/unit/out/refcount/fft.rs @@ -65,8 +65,8 @@ pub fn Sum_1(z1: Complex, z2: Complex) -> Complex { pub fn Neg_2(z1: Complex) -> Complex { let z1: Value = Rc::new(RefCell::new(z1)); return Complex { - re: { -{ (*z1.borrow()).re } }, - img: { -{ (*z1.borrow()).img } }, + re: -{ (*z1.borrow()).re }, + img: -{ (*z1.borrow()).img }, }; } pub fn fft_3(a: Ptr>>>, N: i32) -> Option>> { @@ -79,22 +79,8 @@ pub fn fft_3(a: Ptr>>>, N: i32) -> Option>>>, N: i32) -> Option i32 { ))))); let i: Value = Rc::new(RefCell::new(0)); 'loop_: while ((*i.borrow()) < (*N.borrow())) { - let __rhs = ((*b.borrow()) - .as_ref() - .unwrap() - .as_pointer() - .offset(((*i.borrow()) as usize)) - .with(|__s: &Complex| __s.re.round()) as i32); + let __rhs = + ({ (*b.borrow()).as_ref().unwrap().borrow()[((*i.borrow()) as usize) as usize].re } + .round() as i32); (*reals.borrow()).as_ref().unwrap().borrow_mut()[((*i.borrow()) as usize) as usize] = __rhs; - let __rhs = ((*b.borrow()) - .as_ref() - .unwrap() - .as_pointer() - .offset(((*i.borrow()) as usize)) - .with(|__s: &Complex| __s.img.round()) as i32); + let __rhs = + ({ (*b.borrow()).as_ref().unwrap().borrow()[((*i.borrow()) as usize) as usize].img } + .round() as i32); (*imgs.borrow()).as_ref().unwrap().borrow_mut()[((*i.borrow()) as usize) as usize] = __rhs; (*i.borrow_mut()).prefix_inc(); } diff --git a/tests/unit/out/refcount/field_array_elem_ptr.rs b/tests/unit/out/refcount/field_array_elem_ptr.rs index 9c096cc4ba..7adcf1aacb 100644 --- a/tests/unit/out/refcount/field_array_elem_ptr.rs +++ b/tests/unit/out/refcount/field_array_elem_ptr.rs @@ -41,18 +41,12 @@ pub struct Shape { } impl Clone for Shape { fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - id: { self.id }, - coords: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 4, _>( - |__i: usize| (*self.coords.borrow())[(__i) as usize], - )))), - points: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 3, _>( - |__i: usize| (*self.points.borrow())[(__i) as usize].clone(), - )))), - tail: { self.tail }, - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() + Self { + id: self.id.clone(), + coords: Rc::new(RefCell::new((*self.coords.borrow()).clone())), + points: Rc::new(RefCell::new((*self.points.borrow()).clone())), + tail: self.tail.clone(), + } } } impl Default for Shape { @@ -101,7 +95,7 @@ pub fn sum_0(p: Ptr, n: i32) -> i32 { pub fn set_y_1(p: Ptr, y: i32) { let p: Value> = Rc::new(RefCell::new(p)); let y: Value = Rc::new(RefCell::new(y)); - (*p.borrow()).with_mut(|__s: &mut Point| __s.y = (*y.borrow())); + field!((*p.borrow()), y).write((*y.borrow())); } pub fn main() { __cpp2rust_init_globals(); @@ -119,42 +113,50 @@ fn main_0() -> i32 { tail: 99, })); let c: Value> = Rc::new(RefCell::new( - (({ (*s.borrow()).coords.as_pointer() } as Ptr).offset(1)), + (({ (*s.borrow()).coords.clone() }.as_pointer() as Ptr).offset(1)), )); assert!((((*c.borrow()).read()) == 20)); (*c.borrow()).write(21); - assert!(((*(*s.borrow()).coords.borrow())[(1) as usize] == 21)); + assert!(((*{ (*s.borrow()).coords.clone() }.borrow())[(1) as usize] == 21)); (*c.borrow_mut()) += 2; assert!((((*c.borrow()).read()) == 40)); assert!( - (((*c.borrow()).clone() - (({ (*s.borrow()).coords.as_pointer() } as Ptr).offset(0))) + (((*c.borrow()).clone() + - (({ (*s.borrow()).coords.clone() }.as_pointer() as Ptr).offset(0))) as i64 == 3_i64) ); assert!((((*c.borrow()).offset((-1_i32) as isize).read()) == 30)); - assert!((({ sum_0(({ (*s.borrow()).coords.as_pointer() } as Ptr::), 4,) }) == 101)); assert!( (({ sum_0( - (({ (*s.borrow()).coords.as_pointer() } as Ptr).offset(2)), + ({ (*s.borrow()).coords.clone() }.as_pointer() as Ptr), + 4, + ) + }) == 101) + ); + assert!( + (({ + sum_0( + (({ (*s.borrow()).coords.clone() }.as_pointer() as Ptr).offset(2)), 2, ) }) == 70) ); let p: Value> = Rc::new(RefCell::new( - (({ (*s.borrow()).points.as_pointer() } as Ptr).offset(1)), + (({ (*s.borrow()).points.clone() }.as_pointer() as Ptr).offset(1)), )); - assert!(((*p.borrow()).with(|__s: &Point| __s.x) == 3)); + assert!(((*p.borrow()).with(|__s| __s.x) == 3)); ({ set_y_1((*p.borrow()).offset((1) as isize), 60) }); - assert!(({ (*(*s.borrow()).points.borrow())[(2) as usize].y } == 60)); + assert!(({ (*{ (*s.borrow()).points.clone() }.borrow())[(2) as usize].y } == 60)); let py: Value> = Rc::new(RefCell::new( (field_ptr!( - ({ (*s.borrow()).points.as_pointer() } as Ptr).offset(0), + ({ (*s.borrow()).points.clone() }.as_pointer() as Ptr).offset(0), y )), )); (*py.borrow()).write(7); - assert!(({ (*(*s.borrow()).points.borrow())[(0) as usize].y } == 7)); + assert!(({ (*{ (*s.borrow()).points.clone() }.borrow())[(0) as usize].y } == 7)); let px: Value> = Rc::new(RefCell::new( (field_ptr!(((*p.borrow()).offset((1) as isize)), x)), )); @@ -162,37 +164,35 @@ fn main_0() -> i32 { let _ptr = (*px.borrow()).clone(); _ptr.write(_ptr.read() + 50) }; - assert!(({ (*(*s.borrow()).points.borrow())[(2) as usize].x } == 55)); + assert!(({ (*{ (*s.borrow()).points.clone() }.borrow())[(2) as usize].x } == 55)); let sp: Value> = Rc::new(RefCell::new((s.as_pointer()))); let d: Value> = Rc::new(RefCell::new( - ((*sp.borrow()).with(|__s: &Shape| __s.coords.as_pointer()) as Ptr) + ((*sp.borrow()).with(|__s| __s.coords.clone()).as_pointer() as Ptr) .offset((3) as isize), )); (*d.borrow()).write(41); - assert!(((*(*s.borrow()).coords.borrow())[(3) as usize] == 41)); + assert!(((*{ (*s.borrow()).coords.clone() }.borrow())[(3) as usize] == 41)); let __rhs = { - let _lhs = (*sp.borrow()).with(|__s: &Shape| (*__s.coords.borrow())[(0) as usize]); - _lhs + (*sp.borrow()).with(|__s: &Shape| (*__s.points.borrow())[(0) as usize].x) + let _lhs = (*(*sp.borrow()).with(|__s| __s.coords.clone()).borrow())[(0) as usize]; + _lhs + { (*(*sp.borrow()).with(|__s| __s.points.clone()).borrow())[(0) as usize].x } }; - (*sp.borrow()).with(|__s: &Shape| (*__s.points.borrow_mut())[(1) as usize].y = __rhs); - assert!(({ (*(*s.borrow()).points.borrow())[(1) as usize].y } == 11)); + (*(*sp.borrow()).with(|__s| __s.points.clone()).borrow_mut())[(1) as usize].y = __rhs; + assert!(({ (*{ (*s.borrow()).points.clone() }.borrow())[(1) as usize].y } == 11)); let q: Value> = Rc::new(RefCell::new( - ((*sp.borrow()).with(|__s: &Shape| __s.points.as_pointer()) as Ptr), + ((*sp.borrow()).with(|__s| __s.points.clone()).as_pointer() as Ptr), )); - (*q.borrow()) - .offset((2) as isize) - .with_mut(|__s: &mut Point| __s.x = 8); - assert!(((*sp.borrow()).with(|__s: &Shape| (*__s.points.borrow())[(2) as usize].x) == 8)); + field!((*q.borrow()).offset((2) as isize), x).write(8); + assert!(({ (*(*sp.borrow()).with(|__s| __s.points.clone()).borrow())[(2) as usize].x } == 8)); let t: Value = Rc::new(RefCell::new((*s.borrow()).clone())); - (*(*t.borrow()).coords.borrow_mut())[(0) as usize] = 0; - (*(*t.borrow()).points.borrow_mut())[(0) as usize].x = 0; + (*{ (*t.borrow()).coords.clone() }.borrow_mut())[(0) as usize] = 0; + (*{ (*t.borrow()).points.clone() }.borrow_mut())[(0) as usize].x = 0; assert!( - ((*(*s.borrow()).coords.borrow())[(0) as usize] == 10) - && ({ (*(*s.borrow()).points.borrow())[(0) as usize].x } == 1) + ((*{ (*s.borrow()).coords.clone() }.borrow())[(0) as usize] == 10) + && ({ (*{ (*s.borrow()).points.clone() }.borrow())[(0) as usize].x } == 1) ); assert!( - (((*(*t.borrow()).coords.borrow())[(1) as usize] == 21) - && ({ (*(*t.borrow()).points.borrow())[(2) as usize].y } == 60)) + (((*{ (*t.borrow()).coords.clone() }.borrow())[(1) as usize] == 21) + && ({ (*{ (*t.borrow()).points.clone() }.borrow())[(2) as usize].y } == 60)) && ({ (*t.borrow()).tail } == 99) ); return 0; diff --git a/tests/unit/out/refcount/field_pointers.rs b/tests/unit/out/refcount/field_pointers.rs index 30086bf9f3..b96e60810b 100644 --- a/tests/unit/out/refcount/field_pointers.rs +++ b/tests/unit/out/refcount/field_pointers.rs @@ -15,14 +15,10 @@ pub struct Inner { } impl Clone for Inner { fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - a: { self.a }, - name: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 8, _>( - |__i: usize| (*self.name.borrow())[(__i) as usize], - )))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() + Self { + a: self.a.clone(), + name: Rc::new(RefCell::new((*self.name.borrow()).clone())), + } } } impl Default for Inner { @@ -87,20 +83,14 @@ pub struct Outer { } impl Clone for Outer { fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - x: { self.x }, - inner: { self.inner.clone() }, - items: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 3, _>( - |__i: usize| (*self.items.borrow())[(__i) as usize].clone(), - )))), - v: { Rc::new(RefCell::new((*self.v.borrow()).clone())) }, - cursor: { self.cursor.clone() }, - buf: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 4, _>( - |__i: usize| (*self.buf.borrow())[(__i) as usize], - )))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() + Self { + x: self.x.clone(), + inner: self.inner.clone(), + items: Rc::new(RefCell::new((*self.items.borrow()).clone())), + v: Rc::new(RefCell::new((*self.v.borrow()).clone())), + cursor: self.cursor.clone(), + buf: Rc::new(RefCell::new((*self.buf.borrow()).clone())), + } } } impl Default for Outer { @@ -148,7 +138,7 @@ pub fn set_0(p: Ptr, value: i32) { } pub fn bump_1(o: Ptr) -> i32 { let o: Value> = Rc::new(RefCell::new(o)); - (*o.borrow()).with_mut(|__s: &mut Outer| __s.x.postfix_inc()); + field!((*o.borrow()), x).with_mut(|__v| __v.postfix_inc()); return 1; } pub fn main() { @@ -158,10 +148,12 @@ pub fn main() { fn main_0() -> i32 { let o: Value = Rc::new(RefCell::new(::default())); (*o.borrow_mut()).x = 1; - let px: Value> = Rc::new(RefCell::new((field_ptr!(o, x)))); + let px: Value> = Rc::new(RefCell::new((field_ptr!(o.as_pointer(), x)))); (*px.borrow()).write(2); assert!(({ (*o.borrow()).x } == 2)); - let pa: Value> = Rc::new(RefCell::new((field_ptr!(field_ptr!(o, inner), a)))); + let pa: Value> = Rc::new(RefCell::new( + (field_ptr!(field_ptr!(o.as_pointer(), inner), a)), + )); ({ let _p: Ptr = (*pa.borrow()).clone(); set_0(_p, 3) @@ -169,21 +161,21 @@ fn main_0() -> i32 { assert!(({ (*o.borrow()).inner.a } == 3)); let pi: Value> = Rc::new(RefCell::new( (field_ptr!( - ({ (*o.borrow()).items.as_pointer() } as Ptr).offset(1), + ({ (*o.borrow()).items.clone() }.as_pointer() as Ptr).offset(1), a )), )); (*pi.borrow()).write(4); - assert!(({ (*(*o.borrow()).items.borrow())[(1) as usize].a } == 4)); + assert!(({ (*{ (*o.borrow()).items.clone() }.borrow())[(1) as usize].a } == 4)); assert!({ let _lhs = (field_ptr!( - ({ (*o.borrow()).items.as_pointer() } as Ptr).offset(0), + ({ (*o.borrow()).items.clone() }.as_pointer() as Ptr).offset(0), a )); _lhs != (*pi.borrow()).clone() }); let name: Value> = Rc::new(RefCell::new( - ({ (*o.borrow()).inner.name.as_pointer() } as Ptr), + ({ (*o.borrow()).inner.name.clone() }.as_pointer() as Ptr), )); let i: Value = Rc::new(RefCell::new(0)); 'loop_: while ((*i.borrow()) < 3) { @@ -192,16 +184,16 @@ fn main_0() -> i32 { (*i.borrow_mut()).prefix_inc(); } assert!( - (({ (*o.borrow()).inner.name.as_pointer() } as Ptr::) + (({ (*o.borrow()).inner.name.clone() }.as_pointer() as Ptr::) .to_c_string_iterator() .count() == 3_usize) ); assert!({ let _lhs = (*name.borrow()).offset((3) as isize); - _lhs == (({ (*o.borrow()).inner.name.as_pointer() } as Ptr).offset(3)) + _lhs == (({ (*o.borrow()).inner.name.clone() }.as_pointer() as Ptr).offset(3)) }); - let __rhs = (({ (*o.borrow()).buf.as_pointer() } as Ptr).offset(1)); + let __rhs = (({ (*o.borrow()).buf.clone() }.as_pointer() as Ptr).offset(1)); (*o.borrow_mut()).cursor = __rhs; { (*o.borrow()).cursor.clone() }.write(5); { @@ -209,10 +201,10 @@ fn main_0() -> i32 { _ptr.write(_ptr.read() + 1) }; assert!( - ((*(*o.borrow()).buf.borrow())[(1) as usize] == 6) + ((*{ (*o.borrow()).buf.clone() }.borrow())[(1) as usize] == 6) && ({ let _lhs = { (*o.borrow()).cursor.clone() }; - _lhs == (({ (*o.borrow()).buf.as_pointer() } as Ptr).offset(2)) + _lhs == (({ (*o.borrow()).buf.clone() }.as_pointer() as Ptr).offset(2)) }) ); (*o.borrow_mut()).x = 0; @@ -226,27 +218,30 @@ fn main_0() -> i32 { OuterImpl::push(&o.as_pointer(), _k) }); assert!( - ((*(*o.borrow()).v.borrow()).len() == 1_usize) - && ({ (*(*o.borrow()).v.borrow())[(0_usize) as usize] } == 7) + ((*{ (*o.borrow()).v.clone() }.borrow()).len() == 1_usize) + && ((({ (*o.borrow()).v.clone() }.as_pointer() as Ptr) + .offset(0_usize) + .read()) + == 7) ); assert!((({ OuterImpl::sum(&o.as_pointer(),) }) == 7)); let y: Value = Rc::new(RefCell::new(0)); { ((field_ptr!( - ({ (*o.borrow()).items.as_pointer() } as Ptr).offset(2), + ({ (*o.borrow()).items.clone() }.as_pointer() as Ptr).offset(2), a )) as Ptr) .to_any() .memcpy( &((field_ptr!( - ({ (*o.borrow()).items.as_pointer() } as Ptr).offset(1), + ({ (*o.borrow()).items.clone() }.as_pointer() as Ptr).offset(1), a )) as Ptr) .to_any(), ::std::mem::size_of::() as usize, ); ((field_ptr!( - ({ (*o.borrow()).items.as_pointer() } as Ptr).offset(2), + ({ (*o.borrow()).items.clone() }.as_pointer() as Ptr).offset(2), a )) as Ptr) .to_any() @@ -254,7 +249,7 @@ fn main_0() -> i32 { { ((y.as_pointer()) as Ptr).to_any().memcpy( &((field_ptr!( - ({ (*o.borrow()).items.as_pointer() } as Ptr).offset(2), + ({ (*o.borrow()).items.clone() }.as_pointer() as Ptr).offset(2), a )) as Ptr) .to_any(), @@ -269,7 +264,7 @@ fn main_0() -> i32 { let view: Value> = Rc::new(RefCell::new( (bytes.as_pointer() as Ptr).reinterpret_cast::

(), )); - (*view.borrow()).with_mut(|__s: &mut Header| __s.tag = 16909060); + field!((*view.borrow()), tag).write(16909060); assert!( (((*bytes.borrow())[(0) as usize] as i32) == 4) || (((*bytes.borrow())[(3) as usize] as i32) == 4) @@ -279,7 +274,7 @@ fn main_0() -> i32 { (((*bytes.borrow())[(0) as usize] as i32) == 0) && (((*bytes.borrow())[(3) as usize] as i32) == 0) ); - (*view.borrow()).with_mut(|__s: &mut Header| __s.size = 1_i16); + field!((*view.borrow()), size).write(1_i16); assert!( ((((*bytes.borrow())[(4) as usize] as i32) + ((*bytes.borrow())[(5) as usize] as i32)) == 1) @@ -293,20 +288,16 @@ pub trait OuterImpl { } impl OuterImpl for Ptr { fn next(&self) -> i32 { - return ({ - let __idx = (*self).with_mut(|__s: &mut Outer| __s.x.postfix_inc()); - (*(*self).with(|__s: &Outer| __s.buf.clone()).borrow())[(__idx) as usize] - }); + return (*(*self).with(|__s| __s.buf.clone()).borrow()) + [(field!((*self), x).with_mut(|__v| __v.postfix_inc())) as usize]; } fn sum(&self) -> i32 { - return ((*self).with(|__s: &Outer| __s.x) + (*self).with(|__s: &Outer| __s.inner.a)); + return ((*self).with(|__s| __s.x) + (*self).with(|__s| __s.inner.a)); } fn push(&self, k: i32) { let k: Value = Rc::new(RefCell::new(k)); - { - let __a1 = ((*k.borrow()) + (*self).with(|__s: &Outer| __s.x)); - (*self).with(|__s: &Outer| (*__s.v.borrow_mut()).push(__a1)) - }; + (*(*self).with(|__s| __s.v.clone()).borrow_mut()) + .push(((*k.borrow()) + (*self).with(|__s| __s.x))); } } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/fn_ptr_stable_sort.rs b/tests/unit/out/refcount/fn_ptr_stable_sort.rs index 0dbf4f954d..cefc527b5a 100644 --- a/tests/unit/out/refcount/fn_ptr_stable_sort.rs +++ b/tests/unit/out/refcount/fn_ptr_stable_sort.rs @@ -30,8 +30,8 @@ impl ByteRepr for Item { } pub fn Compare_0(a: Ptr, b: Ptr) -> bool { return { - let _lhs = a.with(|__s: &Item| __s.key); - _lhs < b.with(|__s: &Item| __s.key) + let _lhs = a.with(|__s| __s.key); + _lhs < b.with(|__s| __s.key) }; } pub fn main() { @@ -48,22 +48,31 @@ fn main_0() -> i32 { |x, y| Compare_0.call(x, y), ); assert!( - ((v.as_pointer() as Ptr) - .offset(0_usize) - .with(|__s: &Item| __s.key) - == 1) + ({ + (*(v.as_pointer() as Ptr) + .offset(0_usize) + .upgrade() + .deref()) + .key + } == 1) ); assert!( - ((v.as_pointer() as Ptr) - .offset(1_usize) - .with(|__s: &Item| __s.key) - == 2) + ({ + (*(v.as_pointer() as Ptr) + .offset(1_usize) + .upgrade() + .deref()) + .key + } == 2) ); assert!( - ((v.as_pointer() as Ptr) - .offset(2_usize) - .with(|__s: &Item| __s.key) - == 3) + ({ + (*(v.as_pointer() as Ptr) + .offset(2_usize) + .upgrade() + .deref()) + .key + } == 3) ); return 0; } diff --git a/tests/unit/out/refcount/foreach_disjoint_field_borrow.rs b/tests/unit/out/refcount/foreach_disjoint_field_borrow.rs index 6b89520e59..06ab38ee80 100644 --- a/tests/unit/out/refcount/foreach_disjoint_field_borrow.rs +++ b/tests/unit/out/refcount/foreach_disjoint_field_borrow.rs @@ -15,12 +15,10 @@ pub struct S { } impl Clone for S { fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - v: { Rc::new(RefCell::new((*self.v.borrow()).clone())) }, - a: { self.a }, - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() + Self { + v: Rc::new(RefCell::new((*self.v.borrow()).clone())), + a: self.a.clone(), + } } } <<<<<<< HEAD @@ -48,8 +46,8 @@ pub fn main() { } 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 { + (*{ (*s.borrow()).v.clone() }.borrow_mut()).push(1); + 'loop_: for mut e in { (*s.borrow()).v.clone() }.as_pointer() as Ptr { let e: Value = Rc::new(RefCell::new(e.read())); (*s.borrow_mut()).a.postfix_inc(); } diff --git a/tests/unit/out/refcount/forward_basic.rs b/tests/unit/out/refcount/forward_basic.rs index a92eb3764b..a7adc3a592 100644 --- a/tests/unit/out/refcount/forward_basic.rs +++ b/tests/unit/out/refcount/forward_basic.rs @@ -31,21 +31,21 @@ impl Tracked { } pub fn copy_from(o: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - v: { o.with(|__s: &Tracked| __s.v) }, - copies: { (o.with(|__s: &Tracked| __s.copies) + 1) }, - moves: { o.with(|__s: &Tracked| __s.moves) }, + v: o.with(|__s| __s.v), + copies: (o.with(|__s| __s.copies) + 1), + moves: o.with(|__s| __s.moves), })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } pub fn move_from(o: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - v: { o.with(|__s: &Tracked| __s.v) }, - copies: { o.with(|__s: &Tracked| __s.copies) }, - moves: { (o.with(|__s: &Tracked| __s.moves) + 1) }, + v: o.with(|__s| __s.v), + copies: o.with(|__s| __s.copies), + moves: (o.with(|__s| __s.moves) + 1), })); let this: Ptr = __this.as_pointer(); - o.with_mut(|__s: &mut Tracked| __s.v = 0); + field!(o, v).write(0); Rc::try_unwrap(__this).ok().unwrap().into_inner() } } diff --git a/tests/unit/out/refcount/forward_decltype.rs b/tests/unit/out/refcount/forward_decltype.rs index c24eda5ce4..d81242b7e0 100644 --- a/tests/unit/out/refcount/forward_decltype.rs +++ b/tests/unit/out/refcount/forward_decltype.rs @@ -31,21 +31,21 @@ impl Tracked { } pub fn copy_from(o: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - v: { o.with(|__s: &Tracked| __s.v) }, - copies: { (o.with(|__s: &Tracked| __s.copies) + 1) }, - moves: { o.with(|__s: &Tracked| __s.moves) }, + v: o.with(|__s| __s.v), + copies: (o.with(|__s| __s.copies) + 1), + moves: o.with(|__s| __s.moves), })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } pub fn move_from(o: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - v: { o.with(|__s: &Tracked| __s.v) }, - copies: { o.with(|__s: &Tracked| __s.copies) }, - moves: { (o.with(|__s: &Tracked| __s.moves) + 1) }, + v: o.with(|__s| __s.v), + copies: o.with(|__s| __s.copies), + moves: (o.with(|__s| __s.moves) + 1), })); let this: Ptr = __this.as_pointer(); - o.with_mut(|__s: &mut Tracked| __s.v = 0); + field!(o, v).write(0); Rc::try_unwrap(__this).ok().unwrap().into_inner() } } diff --git a/tests/unit/out/refcount/forward_explicit.rs b/tests/unit/out/refcount/forward_explicit.rs index cef9362d09..00cc54ae15 100644 --- a/tests/unit/out/refcount/forward_explicit.rs +++ b/tests/unit/out/refcount/forward_explicit.rs @@ -33,21 +33,21 @@ impl Tracked { } pub fn copy_from(o: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - v: { o.with(|__s: &Tracked| __s.v) }, - copies: { (o.with(|__s: &Tracked| __s.copies) + 1) }, - moves: { o.with(|__s: &Tracked| __s.moves) }, + v: o.with(|__s| __s.v), + copies: (o.with(|__s| __s.copies) + 1), + moves: o.with(|__s| __s.moves), })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } pub fn move_from(o: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - v: { o.with(|__s: &Tracked| __s.v) }, - copies: { o.with(|__s: &Tracked| __s.copies) }, - moves: { (o.with(|__s: &Tracked| __s.moves) + 1) }, + v: o.with(|__s| __s.v), + copies: o.with(|__s| __s.copies), + moves: (o.with(|__s| __s.moves) + 1), })); let this: Ptr = __this.as_pointer(); - o.with_mut(|__s: &mut Tracked| __s.v = 0); + field!(o, v).write(0); Rc::try_unwrap(__this).ok().unwrap().into_inner() } } diff --git a/tests/unit/out/refcount/forward_pack.rs b/tests/unit/out/refcount/forward_pack.rs index 7d7d5feab0..be7d5c9724 100644 --- a/tests/unit/out/refcount/forward_pack.rs +++ b/tests/unit/out/refcount/forward_pack.rs @@ -33,21 +33,21 @@ impl Tracked { } pub fn copy_from(o: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - v: { o.with(|__s: &Tracked| __s.v) }, - copies: { (o.with(|__s: &Tracked| __s.copies) + 1) }, - moves: { o.with(|__s: &Tracked| __s.moves) }, + v: o.with(|__s| __s.v), + copies: (o.with(|__s| __s.copies) + 1), + moves: o.with(|__s| __s.moves), })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } pub fn move_from(o: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - v: { o.with(|__s: &Tracked| __s.v) }, - copies: { o.with(|__s: &Tracked| __s.copies) }, - moves: { (o.with(|__s: &Tracked| __s.moves) + 1) }, + v: o.with(|__s| __s.v), + copies: o.with(|__s| __s.copies), + moves: (o.with(|__s| __s.moves) + 1), })); let this: Ptr = __this.as_pointer(); - o.with_mut(|__s: &mut Tracked| __s.v = 0); + field!(o, v).write(0); Rc::try_unwrap(__this).ok().unwrap().into_inner() } } diff --git a/tests/unit/out/refcount/friend.rs b/tests/unit/out/refcount/friend.rs index f800dd6c1c..650bc1145e 100644 --- a/tests/unit/out/refcount/friend.rs +++ b/tests/unit/out/refcount/friend.rs @@ -7,25 +7,25 @@ use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; pub fn get_0(v: Ptr) -> i32 { - return v.with(|__s: &V| __s.x); + return v.with(|__s| __s.x); } pub fn operator_eq_1(a: Ptr, b: Ptr) -> bool { return { - let _lhs = a.with(|__s: &V| __s.x); - _lhs == b.with(|__s: &V| __s.x) + let _lhs = a.with(|__s| __s.x); + _lhs == b.with(|__s| __s.x) }; } pub fn scaled_2(v: Ptr, k: i32) -> i32 { let k: Value = Rc::new(RefCell::new(k)); return { - let _lhs = v.with(|__s: &V| __s.x); + let _lhs = v.with(|__s| __s.x); _lhs * (*k.borrow()) }; } pub fn scaled_3(v: Ptr, k: f64) -> f64 { let k: Value = Rc::new(RefCell::new(k)); return { - let _lhs = (v.with(|__s: &V| __s.x) as f64); + let _lhs = (v.with(|__s| __s.x) as f64); _lhs * (*k.borrow()) }; } @@ -59,7 +59,7 @@ impl ByteRepr for V { } } pub fn get_4(w: Ptr) -> i32 { - return w.with(|__s: &W_int_| __s.x); + return w.with(|__s| __s.x); } #[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct W_int_ { @@ -80,7 +80,7 @@ impl ByteRepr for W_int_ { } } pub fn get_5(w: Ptr) -> i64 { - return w.with(|__s: &W_long_| __s.x); + return w.with(|__s| __s.x); } #[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct W_long_ { @@ -119,7 +119,7 @@ impl ByteRepr for D { } } pub fn declared_then_defined_6(d: Ptr) -> i32 { - return (d.with(|__s: &D| __s.x) + 1); + return (d.with(|__s| __s.x) + 1); } pub fn main() { __cpp2rust_init_globals(); diff --git a/tests/unit/out/refcount/fstat.rs b/tests/unit/out/refcount/fstat.rs index ac3ebe744e..cfe2340cb6 100644 --- a/tests/unit/out/refcount/fstat.rs +++ b/tests/unit/out/refcount/fstat.rs @@ -63,7 +63,7 @@ fn main_0() -> i32 { } == 0) as i32) != 0) ); - assert!(((((*(*st.borrow()).st_size.borrow()) == 5_i64) as i32) != 0)); + assert!(((({ (*st.borrow()).st_size } == 5_i64) as i32) != 0)); assert!((((FdRegistry::close((*fd.borrow())) == 0) as i32) != 0)); assert!( (((match nix::unistd::unlink((*path.borrow()).to_rust_string().as_str()) { diff --git a/tests/unit/out/refcount/global_non_const_init.rs b/tests/unit/out/refcount/global_non_const_init.rs index 266b616c01..b30eefa7cc 100644 --- a/tests/unit/out/refcount/global_non_const_init.rs +++ b/tests/unit/out/refcount/global_non_const_init.rs @@ -170,9 +170,9 @@ fn main_0() -> i32 { assert!(({ (*inline_member_11.with(Value::clone).borrow()).v } == 5)); assert!((({ local_static_12() }) == 7)); assert!((({ local_static_12() }) == 7)); - ({ Singleton::instance() }).with_mut(|__s: &mut Singleton| __s.hits.postfix_inc()); - ({ Singleton::instance() }).with_mut(|__s: &mut Singleton| __s.hits.postfix_inc()); - assert!((({ Singleton::instance() }).with(|__s: &Singleton| __s.hits) == 2)); + field!(({ Singleton::instance() }), hits).with_mut(|__v| __v.postfix_inc()); + field!(({ Singleton::instance() }), hits).with_mut(|__v| __v.postfix_inc()); + assert!((({ Singleton::instance() }).with(|__s| __s.hits) == 2)); assert!((({ Singleton::instance() }) == ({ Singleton::instance() }))); return 0; } diff --git a/tests/unit/out/refcount/goto_cleanup.rs b/tests/unit/out/refcount/goto_cleanup.rs index 765bb764f2..23ae12be6f 100644 --- a/tests/unit/out/refcount/goto_cleanup.rs +++ b/tests/unit/out/refcount/goto_cleanup.rs @@ -103,7 +103,7 @@ pub fn via_pointer_3(w: Ptr, fail: i32) -> i32 { goto_block!({ '__entry: { *ret.borrow_mut() = 0; - *item.borrow_mut() = (*w.borrow()).with(|__s: &wrapper| (__s.item).clone()); + *item.borrow_mut() = (*w.borrow()).with(|__s| __s.item.clone()); if ((*fail.borrow()) != 0) { (*ret.borrow_mut()) = -1_i32; goto!('out); diff --git a/tests/unit/out/refcount/huffman.rs b/tests/unit/out/refcount/huffman.rs index c2ee25162b..0a39dc248e 100644 --- a/tests/unit/out/refcount/huffman.rs +++ b/tests/unit/out/refcount/huffman.rs @@ -38,16 +38,16 @@ impl ByteRepr for MinHeapNode { } pub fn Swap_0(a: Ptr, b: Ptr) { let t: Value = Rc::new(RefCell::new(MinHeapNode { - data: { a.with(|__s: &MinHeapNode| __s.data) }, - freq: { a.with(|__s: &MinHeapNode| __s.freq) }, - left: { a.with(|__s: &MinHeapNode| (__s.left).clone()) }, - right: { a.with(|__s: &MinHeapNode| (__s.right).clone()) }, + data: a.with(|__s| __s.data), + freq: a.with(|__s| __s.freq), + left: a.with(|__s| __s.left.clone()), + right: a.with(|__s| __s.right.clone()), })); let __rhs = MinHeapNode { - data: { b.with(|__s: &MinHeapNode| __s.data) }, - freq: { b.with(|__s: &MinHeapNode| __s.freq) }, - left: { b.with(|__s: &MinHeapNode| (__s.left).clone()) }, - right: { b.with(|__s: &MinHeapNode| (__s.right).clone()) }, + data: b.with(|__s| __s.data), + freq: b.with(|__s| __s.freq), + left: b.with(|__s| __s.left.clone()), + right: b.with(|__s| __s.right.clone()), }; a.write(__rhs); let __rhs = MinHeapNode { @@ -74,11 +74,13 @@ pub struct MinHeap { impl MinHeap { pub fn move_from(_a0: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - size: { _a0.with(|__s: &MinHeap| __s.size) }, - capacity: { _a0.with(|__s: &MinHeap| __s.capacity) }, - arr: { _a0.with_mut(|__s: &mut MinHeap| __s.arr.take()) }, - next: { _a0.with(|__s: &MinHeap| __s.next) }, - alloc: { _a0.with_mut(|__s: &mut MinHeap| __s.alloc.take()) }, + size: { (*_a0.upgrade().deref()).size }, + capacity: { (*_a0.upgrade().deref()).capacity }, + arr: field!(_a0, arr) + .with_mut(|__v: &mut Option]>>>| __v.take()), + next: { (*_a0.upgrade().deref()).next }, + alloc: field!(_a0, alloc) + .with_mut(|__v: &mut Option>>| __v.take()), })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() @@ -146,7 +148,7 @@ pub fn Huffman_2( let _n: i32 = (*size.borrow()); MinHeapImpl::Build(&((*minHeap.borrow()).as_pointer()), _data, _freq, _n) }); - 'loop_: while (((*minHeap.borrow()).as_pointer()).with(|__s: &MinHeap| __s.size) != 1) { + 'loop_: while ({ (*(*minHeap.borrow()).as_ref().unwrap().borrow()).size } != 1) { let left: Value> = Rc::new(RefCell::new( ({ MinHeapImpl::ExtractMin(&((*minHeap.borrow()).as_pointer())) }), )); @@ -156,13 +158,13 @@ pub fn Huffman_2( let top: Value> = Rc::new(RefCell::new( ({ MinHeapImpl::Alloc(&((*minHeap.borrow()).as_pointer()), ('$' as u8), { - let _lhs = (*left.borrow()).with(|__s: &MinHeapNode| __s.freq); - _lhs + (*right.borrow()).with(|__s: &MinHeapNode| __s.freq) + let _lhs = (*left.borrow()).with(|__s| __s.freq); + _lhs + (*right.borrow()).with(|__s| __s.freq) }) }), )); - (*top.borrow()).with_mut(|__s: &mut MinHeapNode| __s.left = (*left.borrow()).clone()); - (*top.borrow()).with_mut(|__s: &mut MinHeapNode| __s.right = (*right.borrow()).clone()); + field!((*top.borrow()), left).write((*left.borrow()).clone()); + field!((*top.borrow()), right).write((*right.borrow()).clone()); ({ MinHeapImpl::Insert(&((*minHeap.borrow()).as_pointer()), (*top.borrow()).clone()) }); } return (*minHeap.borrow_mut()).take(); @@ -203,12 +205,11 @@ pub fn CollectCodes_4( ) { let root: Value> = Rc::new(RefCell::new(root)); let top: Value = Rc::new(RefCell::new(top)); - if !(((*root.borrow()).with(|__s: &MinHeapNode| (__s.left).clone())).is_null()) { + if !(((*root.borrow()).with(|__s| __s.left.clone())).is_null()) { (*arr.upgrade().deref()).as_ref().unwrap().borrow_mut() [((*top.borrow()) as usize) as usize] = 0; ({ - let _root: Ptr = - (*root.borrow()).with(|__s: &MinHeapNode| (__s.left).clone()); + let _root: Ptr = (*root.borrow()).with(|__s| __s.left.clone()); let _arr: Ptr>>> = (arr).clone(); let _top: i32 = ((*top.borrow()) + 1); let _out: Ptr>>> = (out).clone(); @@ -216,12 +217,11 @@ pub fn CollectCodes_4( CollectCodes_4(_root, _arr, _top, _out, _next) }); } - if !(((*root.borrow()).with(|__s: &MinHeapNode| (__s.right).clone())).is_null()) { + if !(((*root.borrow()).with(|__s| __s.right.clone())).is_null()) { (*arr.upgrade().deref()).as_ref().unwrap().borrow_mut() [((*top.borrow()) as usize) as usize] = 1; ({ - let _root: Ptr = - (*root.borrow()).with(|__s: &MinHeapNode| (__s.right).clone()); + let _root: Ptr = (*root.borrow()).with(|__s| __s.right.clone()); let _arr: Ptr>>> = (arr).clone(); let _top: i32 = ((*top.borrow()) + 1); let _out: Ptr>>> = (out).clone(); @@ -343,25 +343,22 @@ impl MinHeapImpl for Ptr { fn Alloc(&self, data: u8, freq: i32) -> Ptr { let data: Value = Rc::new(RefCell::new(data)); let freq: Value = Rc::new(RefCell::new(freq)); - let __rhs = MinHeapNode { + (*self) + .with(|__s| __s.alloc.clone()) + .as_ref() + .unwrap() + .borrow_mut()[((*self).with(|__s| __s.next) as usize) as usize] = MinHeapNode { data: (*data.borrow()), freq: (*freq.borrow()), left: Ptr::::null(), right: Ptr::::null(), }; - let __idx = ((*self).with(|__s: &MinHeap| __s.next) as usize); - (*self).with_mut(|__s: &mut MinHeap| { - __s.alloc.as_ref().unwrap().borrow_mut()[(__idx) as usize] = __rhs - }); - return ({ - let __idx = ((*self).with_mut(|__s: &mut MinHeap| __s.next.postfix_inc()) as usize); - (*(*self).upgrade().deref()) - .alloc - .as_ref() - .unwrap() - .as_pointer() - .offset((__idx)) - }) + return ((*self) + .with(|__s| __s.alloc.clone()) + .as_ref() + .unwrap() + .as_pointer() + .offset((field!((*self), next).with_mut(|__v| __v.postfix_inc()) as usize))) .clone(); } fn Heapify(&self, idx: i32) { @@ -369,101 +366,115 @@ impl MinHeapImpl for Ptr { let smallest: Value = Rc::new(RefCell::new((*idx.borrow()))); let left: Value = Rc::new(RefCell::new(((2 * (*idx.borrow())) + 1))); let right: Value = Rc::new(RefCell::new(((2 * (*idx.borrow())) + 2))); - if ((*left.borrow()) < (*self).with(|__s: &MinHeap| __s.size)) - && ({ - (*(*(*self).upgrade().deref()).arr.as_ref().unwrap().borrow() - [((*left.borrow()) as usize) as usize] - .upgrade() - .deref()) - .freq - } < { - (*(*(*self).upgrade().deref()).arr.as_ref().unwrap().borrow() - [((*smallest.borrow()) as usize) as usize] - .upgrade() - .deref()) - .freq - }) + if ((*left.borrow()) < (*self).with(|__s| __s.size)) + && ((*self) + .with(|__s| __s.arr.clone()) + .as_ref() + .unwrap() + .borrow()[((*left.borrow()) as usize) as usize] + .with(|__s| __s.freq) + < (*self) + .with(|__s| __s.arr.clone()) + .as_ref() + .unwrap() + .borrow()[((*smallest.borrow()) as usize) as usize] + .with(|__s| __s.freq)) { (*smallest.borrow_mut()) = (*left.borrow()); } - if ((*right.borrow()) < (*self).with(|__s: &MinHeap| __s.size)) - && ({ - (*(*(*self).upgrade().deref()).arr.as_ref().unwrap().borrow() - [((*right.borrow()) as usize) as usize] - .upgrade() - .deref()) - .freq - } < { - (*(*(*self).upgrade().deref()).arr.as_ref().unwrap().borrow() - [((*smallest.borrow()) as usize) as usize] - .upgrade() - .deref()) - .freq - }) + if ((*right.borrow()) < (*self).with(|__s| __s.size)) + && ((*self) + .with(|__s| __s.arr.clone()) + .as_ref() + .unwrap() + .borrow()[((*right.borrow()) as usize) as usize] + .with(|__s| __s.freq) + < (*self) + .with(|__s| __s.arr.clone()) + .as_ref() + .unwrap() + .borrow()[((*smallest.borrow()) as usize) as usize] + .with(|__s| __s.freq)) { (*smallest.borrow_mut()) = (*right.borrow()); } if ((*smallest.borrow()) != (*idx.borrow())) { ({ - let _a: Ptr = - ((*(*self).upgrade().deref()).arr.as_ref().unwrap().borrow() - [((*smallest.borrow()) as usize) as usize]) - .clone(); - let _b: Ptr = - ((*(*self).upgrade().deref()).arr.as_ref().unwrap().borrow() - [((*idx.borrow()) as usize) as usize]) - .clone(); + let _a: Ptr = ((*self) + .with(|__s| __s.arr.clone()) + .as_ref() + .unwrap() + .borrow()[((*smallest.borrow()) as usize) as usize]) + .clone(); + let _b: Ptr = ((*self) + .with(|__s| __s.arr.clone()) + .as_ref() + .unwrap() + .borrow()[((*idx.borrow()) as usize) as usize]) + .clone(); Swap_0(_a, _b) }); ({ MinHeapImpl::Heapify(self, (*smallest.borrow())) }); } } fn ExtractMin(&self) -> Ptr { - let out: Value> = Rc::new(RefCell::new((*self).with(|__s: &MinHeap| { - (__s.arr.as_ref().unwrap().borrow()[(0_usize) as usize]).clone() - }))); - (*self).with_mut(|__s: &mut MinHeap| __s.size.prefix_dec()); - let __rhs = (*self).with(|__s: &MinHeap| { - (__s.arr.as_ref().unwrap().borrow() - [((*self).with(|__s: &MinHeap| __s.size) as usize) as usize]) - .clone() - }); - (*self).with_mut(|__s: &mut MinHeap| { - __s.arr.as_ref().unwrap().borrow_mut()[(0_usize) as usize] = __rhs - }); + let out: Value> = Rc::new(RefCell::new( + ((*self) + .with(|__s| __s.arr.clone()) + .as_ref() + .unwrap() + .borrow()[(0_usize) as usize]) + .clone(), + )); + field!((*self), size).with_mut(|__v| __v.prefix_dec()); + let __rhs = ((*self) + .with(|__s| __s.arr.clone()) + .as_ref() + .unwrap() + .borrow()[((*self).with(|__s| __s.size) as usize) as usize]) + .clone(); + (*self) + .with(|__s| __s.arr.clone()) + .as_ref() + .unwrap() + .borrow_mut()[(0_usize) as usize] = __rhs; ({ MinHeapImpl::Heapify(self, 0) }); return (*out.borrow()).clone(); } fn Insert(&self, node: Ptr) { let node: Value> = Rc::new(RefCell::new(node)); - (*self).with_mut(|__s: &mut MinHeap| __s.size.prefix_inc()); - let i: Value = Rc::new(RefCell::new(((*self).with(|__s: &MinHeap| __s.size) - 1))); + field!((*self), size).with_mut(|__v| __v.prefix_inc()); + let i: Value = Rc::new(RefCell::new(((*self).with(|__s| __s.size) - 1))); 'loop_: while ((*i.borrow()) != 0) && ({ - let _lhs = (*node.borrow()).with(|__s: &MinHeapNode| __s.freq); - _lhs < { - (*(*(*self).upgrade().deref()).arr.as_ref().unwrap().borrow() - [((((*i.borrow()) - 1) / 2) as usize) as usize] - .upgrade() - .deref()) - .freq - } + let _lhs = (*node.borrow()).with(|__s| __s.freq); + _lhs < (*self) + .with(|__s| __s.arr.clone()) + .as_ref() + .unwrap() + .borrow()[((((*i.borrow()) - 1) / 2) as usize) as usize] + .with(|__s| __s.freq) }) { - let __rhs = (*self).with(|__s: &MinHeap| { - (__s.arr.as_ref().unwrap().borrow()[((((*i.borrow()) - 1) / 2) as usize) as usize]) - .clone() - }); - (*self).with_mut(|__s: &mut MinHeap| { - __s.arr.as_ref().unwrap().borrow_mut()[((*i.borrow()) as usize) as usize] = __rhs - }); + let __rhs = ((*self) + .with(|__s| __s.arr.clone()) + .as_ref() + .unwrap() + .borrow()[((((*i.borrow()) - 1) / 2) as usize) as usize]) + .clone(); + (*self) + .with(|__s| __s.arr.clone()) + .as_ref() + .unwrap() + .borrow_mut()[((*i.borrow()) as usize) as usize] = __rhs; let __rhs = (((*i.borrow()) - 1) / 2); (*i.borrow_mut()) = __rhs; } - (*self).with_mut(|__s: &mut MinHeap| { - __s.arr.as_ref().unwrap().borrow_mut()[((*i.borrow()) as usize) as usize] = - (*node.borrow()).clone() - }); + (*self) + .with(|__s| __s.arr.clone()) + .as_ref() + .unwrap() + .borrow_mut()[((*i.borrow()) as usize) as usize] = (*node.borrow()).clone(); } fn Build( &self, @@ -474,38 +485,40 @@ impl MinHeapImpl for Ptr { let n: Value = Rc::new(RefCell::new(n)); let i: Value = Rc::new(RefCell::new(0)); 'loop_: while ((*i.borrow()) < (*n.borrow())) { - let __rhs = ({ + (*self) + .with(|__s| __s.arr.clone()) + .as_ref() + .unwrap() + .borrow_mut() + [(field!((*self), size).with_mut(|__v| __v.postfix_inc()) as usize) as usize] = ({ let _data: u8 = (*data.upgrade().deref()).as_ref().unwrap().borrow() [((*i.borrow()) as usize) as usize]; let _freq: i32 = (*freq.upgrade().deref()).as_ref().unwrap().borrow() [((*i.borrow()) as usize) as usize]; MinHeapImpl::Alloc(self, _data, _freq) }); - let __idx = ((*self).with_mut(|__s: &mut MinHeap| __s.size.postfix_inc()) as usize); - (*self).with_mut(|__s: &mut MinHeap| { - __s.arr.as_ref().unwrap().borrow_mut()[(__idx) as usize] = __rhs - }); (*i.borrow_mut()).prefix_inc(); } - let i: Value = Rc::new(RefCell::new( - (((*self).with(|__s: &MinHeap| __s.size) - 2) / 2), - )); + let i: Value = Rc::new(RefCell::new((((*self).with(|__s| __s.size) - 2) / 2))); 'loop_: while ((*i.borrow()) >= 0) { ({ MinHeapImpl::Heapify(self, (*i.borrow())) }); (*i.borrow_mut()).prefix_dec(); } } fn move_assign(&self, _a0: Ptr) -> Ptr { - let __rhs = _a0.with(|__s: &MinHeap| __s.size); - (*self).with_mut(|__s: &mut MinHeap| __s.size = __rhs); - let __rhs = _a0.with(|__s: &MinHeap| __s.capacity); - (*self).with_mut(|__s: &mut MinHeap| __s.capacity = __rhs); - (field_ptr!((*self), arr) as Ptr]>>>>) - .write(_a0.with_mut(|__s: &mut MinHeap| __s.arr.take())); - let __rhs = _a0.with(|__s: &MinHeap| __s.next); - (*self).with_mut(|__s: &mut MinHeap| __s.next = __rhs); - (field_ptr!((*self), alloc) as Ptr>>>) - .write(_a0.with_mut(|__s: &mut MinHeap| __s.alloc.take())); + let __rhs = { (*_a0.upgrade().deref()).size }; + field!((*self), size).write(__rhs); + let __rhs = { (*_a0.upgrade().deref()).capacity }; + field!((*self), capacity).write(__rhs); + (field_ptr!((*self), arr) as Ptr]>>>>).write( + field!(_a0, arr) + .with_mut(|__v: &mut Option]>>>| __v.take()), + ); + let __rhs = { (*_a0.upgrade().deref()).next }; + field!((*self), next).write(__rhs); + (field_ptr!((*self), alloc) as Ptr>>>).write( + field!(_a0, alloc).with_mut(|__v: &mut Option>>| __v.take()), + ); return (*self).clone(); } } @@ -514,8 +527,8 @@ pub trait MinHeapNodeImpl { } impl MinHeapNodeImpl for Ptr { fn IsLeaf(&self) -> bool { - return (((*self).with(|__s: &MinHeapNode| (__s.left).clone())).is_null()) - && (((*self).with(|__s: &MinHeapNode| (__s.right).clone())).is_null()); + return (((*self).with(|__s| __s.left.clone())).is_null()) + && (((*self).with(|__s| __s.right.clone())).is_null()); } } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/ifaddrs.rs b/tests/unit/out/refcount/ifaddrs.rs index a2154a2c86..d4518f9b08 100644 --- a/tests/unit/out/refcount/ifaddrs.rs +++ b/tests/unit/out/refcount/ifaddrs.rs @@ -21,8 +21,8 @@ fn main_0() -> i32 { let __list: Vec = __ifas.collect(); let mut __next = Ptr::::null(); for __ifa in __list.iter().rev() { - let __node = Ifaddrs::from_interface_address(__ifa); - *__node.ifa_next.borrow_mut() = __next.clone(); + let mut __node = Ifaddrs::from_interface_address(__ifa); + __node.ifa_next = __next.clone(); __next = Ptr::alloc(__node); } __out.write(__next); @@ -42,34 +42,29 @@ fn main_0() -> i32 { Rc::new(RefCell::new(Ptr::::null())); (*ifa.borrow_mut()) = (*list.borrow()).clone(); 'loop_: while (((!((*ifa.borrow()).is_null())) as i32) != 0) { - assert!( - (((!((*(*(*ifa.borrow()).upgrade().deref()).ifa_name.borrow()).is_null())) as i32) - != 0) - ); - if ((((*(*(*ifa.borrow()).upgrade().deref()).ifa_addr.borrow()).is_null()) as i32) != 0) { - let __rhs = (*(*(*ifa.borrow()).upgrade().deref()).ifa_next.borrow()).clone(); + assert!((((!(((*ifa.borrow()).with(|__s| __s.ifa_name.clone())).is_null())) as i32) != 0)); + if (((((*ifa.borrow()).with(|__s| __s.ifa_addr.clone())).is_null()) as i32) != 0) { + let __rhs = (*ifa.borrow()).with(|__s| __s.ifa_next.clone()); (*ifa.borrow_mut()) = __rhs; continue 'loop_; } - if (((((*(*(*(*(*ifa.borrow()).upgrade().deref()).ifa_addr.borrow()) - .upgrade() - .deref()) - .sa_family - .borrow()) as i32) + if (((((*ifa.borrow()) + .with(|__s| __s.ifa_addr.clone()) + .with(|__s| __s.sa_family) as i32) != libc::AF_INET) as i32) != 0) { - let __rhs = (*(*(*ifa.borrow()).upgrade().deref()).ifa_next.borrow()).clone(); + let __rhs = (*ifa.borrow()).with(|__s| __s.ifa_next.clone()); (*ifa.borrow_mut()) = __rhs; continue 'loop_; } let sin: Value> = Rc::new(RefCell::new( - (*(*(*ifa.borrow()).upgrade().deref()).ifa_addr.borrow()) + (*ifa.borrow()) + .with(|__s| __s.ifa_addr.clone()) .reinterpret_cast::(), )); let lo_be: Value> = Rc::new(RefCell::new(Box::new([127_u8, 0_u8, 0_u8, 1_u8]))); - if ((((((*(*sin.borrow()).upgrade().deref()).sin_addr.as_pointer()) - as Ptr) + if (((((field_ptr!((*sin.borrow()), sin_addr)) as Ptr) .to_any() .memcmp( &((lo_be.as_pointer() as Ptr) as Ptr).to_any(), @@ -79,24 +74,20 @@ fn main_0() -> i32 { != 0) { (*found_loopback.borrow_mut()) = 1; + assert!(((((*ifa.borrow()).with(|__s| __s.ifa_flags) != 0_u32) as i32) != 0)); assert!( - ((((*(*(*ifa.borrow()).upgrade().deref()).ifa_flags.borrow()) != 0_u32) as i32) - != 0) - ); - assert!( - (((!((*(*(*ifa.borrow()).upgrade().deref()).ifa_netmask.borrow()).is_null())) - as i32) + (((!(((*ifa.borrow()).with(|__s| __s.ifa_netmask.clone())).is_null())) as i32) != 0) ); let mask: Value> = Rc::new(RefCell::new( - (*(*(*ifa.borrow()).upgrade().deref()).ifa_netmask.borrow()) + (*ifa.borrow()) + .with(|__s| __s.ifa_netmask.clone()) .reinterpret_cast::(), )); let mask_be: Value> = Rc::new(RefCell::new(Box::new([255_u8, 0_u8, 0_u8, 0_u8]))); assert!( - ((((((*(*mask.borrow()).upgrade().deref()).sin_addr.as_pointer()) - as Ptr) + (((((field_ptr!((*mask.borrow()), sin_addr)) as Ptr) .to_any() .memcmp( &((mask_be.as_pointer() as Ptr::) as Ptr::).to_any(), @@ -107,7 +98,8 @@ fn main_0() -> i32 { ); assert!( (((match nix::net::if_::if_nametoindex( - (*(*(*ifa.borrow()).upgrade().deref()).ifa_name.borrow()) + (*ifa.borrow()) + .with(|__s| __s.ifa_name.clone()) .to_rust_string() .as_str() ) { @@ -120,7 +112,7 @@ fn main_0() -> i32 { != 0) ); } - let __rhs = (*(*(*ifa.borrow()).upgrade().deref()).ifa_next.borrow()).clone(); + let __rhs = (*ifa.borrow()).with(|__s| __s.ifa_next.clone()); (*ifa.borrow_mut()) = __rhs; } assert!(((*found_loopback.borrow()) != 0)); @@ -128,10 +120,10 @@ fn main_0() -> i32 { let mut __cur = (*list.borrow()).clone(); while !__cur.is_null() { let __next = __cur.with(|__i| { - __i.ifa_name.borrow().delete(); - __i.ifa_addr.borrow().delete(); - __i.ifa_netmask.borrow().delete(); - (*__i.ifa_next.borrow()).clone() + __i.ifa_name.delete(); + __i.ifa_addr.delete(); + __i.ifa_netmask.delete(); + __i.ifa_next.clone() }); __cur.delete(); __cur = __next; diff --git a/tests/unit/out/refcount/immutable-deref-on-func-call.rs b/tests/unit/out/refcount/immutable-deref-on-func-call.rs index 02dc722c2e..e1d9d28100 100644 --- a/tests/unit/out/refcount/immutable-deref-on-func-call.rs +++ b/tests/unit/out/refcount/immutable-deref-on-func-call.rs @@ -34,23 +34,16 @@ fn main_0() -> i32 { .map(|_| ::default()) .collect::>(), ))); - (*arr.borrow()) - .offset((0) as isize) - .with_mut(|__s: &mut Item| __s.value = 1); - (*arr.borrow()) - .offset((1) as isize) - .with_mut(|__s: &mut Item| __s.value = 2); + field!((*arr.borrow()).offset((0) as isize), value).write(1); + field!((*arr.borrow()).offset((1) as isize), value).write(2); ({ let _other: Ptr = ((*arr.borrow()).offset((1) as isize)); ItemImpl::foo(&(*arr.borrow()).offset((0) as isize), _other) }); let result: Value = Rc::new(RefCell::new( - ((*arr.borrow()) - .offset((0) as isize) - .with(|__s: &Item| __s.value) - + (*arr.borrow()) - .offset((1) as isize) - .with(|__s: &Item| __s.value)), + ({ (*(*arr.borrow()).offset((0) as isize).upgrade().deref()).value } + { + (*(*arr.borrow()).offset((1) as isize).upgrade().deref()).value + }), )); (*arr.borrow()).delete(); assert!(((*result.borrow()) == 11)); @@ -62,7 +55,7 @@ pub trait ItemImpl { impl ItemImpl for Ptr { fn foo(&self, other: Ptr) { let other: Value> = Rc::new(RefCell::new(other)); - (*other.borrow()).with_mut(|__s: &mut Item| __s.value = 10); + field!((*other.borrow()), value).write(10); } } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/implicit_autoref.rs b/tests/unit/out/refcount/implicit_autoref.rs index ed9cc9f18c..0c65ead544 100644 --- a/tests/unit/out/refcount/implicit_autoref.rs +++ b/tests/unit/out/refcount/implicit_autoref.rs @@ -13,11 +13,9 @@ pub struct Holder { } impl Clone for Holder { fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - v: { Rc::new(RefCell::new((*self.v.borrow()).clone())) }, - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() + Self { + v: Rc::new(RefCell::new((*self.v.borrow()).clone())), + } } } <<<<<<< HEAD @@ -59,13 +57,17 @@ fn main_0() -> i32 { .offset(1_usize) .write(30); let h: Value = Rc::new(RefCell::new(::default())); - (*(*h.borrow()).v.borrow_mut()).push(40); - (*(*h.borrow()).v.borrow_mut()).push(50); + (*{ (*h.borrow()).v.clone() }.borrow_mut()).push(40); + (*{ (*h.borrow()).v.clone() }.borrow_mut()).push(50); let hp: Value> = Rc::new(RefCell::new((h.as_pointer()))); let b: Value = Rc::new(RefCell::new( - (*hp.borrow()).with(|__s: &Holder| (*__s.v.borrow())[(0_usize) as usize]), + (((*hp.borrow()).with(|__s| (__s).v.clone()).as_pointer() as Ptr) + .offset(0_usize) + .read()), )); - (*hp.borrow()).with(|__s: &Holder| (*__s.v.borrow_mut())[(1_usize) as usize] = 60); + ((*hp.borrow()).with(|__s| (__s).v.clone()).as_pointer() as Ptr) + .offset(1_usize) + .write(60); assert!(((*a.borrow()) == 10)); assert!( ((((Ptr::>::decay(&(*p.borrow()))) as Ptr) @@ -74,7 +76,12 @@ fn main_0() -> i32 { == 30) ); assert!(((*b.borrow()) == 40)); - assert!(((*hp.borrow()).with(|__s: &Holder| { (*__s.v.borrow())[(1_usize) as usize] }) == 60)); + assert!( + ((((*hp.borrow()).with(|__s| (__s).v.clone()).as_pointer() as Ptr) + .offset(1_usize) + .read()) + == 60) + ); ({ write_through_0( ((Ptr::>::decay(&(*p.borrow())) as Ptr).offset(0_usize as isize)), diff --git a/tests/unit/out/refcount/init.rs b/tests/unit/out/refcount/init.rs index f424e07479..4e815eb166 100644 --- a/tests/unit/out/refcount/init.rs +++ b/tests/unit/out/refcount/init.rs @@ -40,9 +40,9 @@ fn main_0() -> i32 { let xx: Value = Rc::new(RefCell::new(::default())); let zz: Value> = Rc::new(RefCell::new((xx.as_pointer()))); (*xx.borrow_mut()).x = 1; - (*q.borrow_mut()) = (field_ptr!(xx, x)); + (*q.borrow_mut()) = (field_ptr!(xx.as_pointer(), x)); (*q.borrow_mut()) = (field_ptr!((*zz.borrow()), x)); - (*zz.borrow()).with_mut(|__s: &mut X| __s.x = 2); + field!((*zz.borrow()), x).write(2); let ww: Value = Rc::new(RefCell::new((*xx.borrow()).clone())); (*ww.borrow_mut()) = (*xx.borrow()).clone(); let aa: Value = Rc::new(RefCell::new(({ func_0() }))); diff --git a/tests/unit/out/refcount/kruskal.rs b/tests/unit/out/refcount/kruskal.rs index 508cc3ac68..890bbae59c 100644 --- a/tests/unit/out/refcount/kruskal.rs +++ b/tests/unit/out/refcount/kruskal.rs @@ -45,13 +45,12 @@ pub fn partition_0(arr: Ptr>>>, start: i32, end: i32) - let i: Value = Rc::new(RefCell::new(((*start.borrow()) + 1))); 'loop_: while ((*i.borrow()) <= (*end.borrow())) { if { - let _lhs = (*arr.upgrade().deref()) - .as_ref() - .unwrap() - .as_pointer() - .offset(((*i.borrow()) as usize)) - .with(|__s: &Edge| __s.weight); - _lhs <= pivot.with(|__s: &Edge| __s.weight) + let _lhs = { + (*arr.upgrade().deref()).as_ref().unwrap().borrow() + [((*i.borrow()) as usize) as usize] + .weight + }; + _lhs <= pivot.with(|__s| __s.weight) } { (*count.borrow_mut()).postfix_inc(); } @@ -60,54 +59,36 @@ pub fn partition_0(arr: Ptr>>>, start: i32, end: i32) - let pidx: Value = Rc::new(RefCell::new(((*start.borrow()) + (*count.borrow())))); let tmp: Value = Rc::new(RefCell::new(Edge { u: { - (*arr.upgrade().deref()) - .as_ref() - .unwrap() - .as_pointer() - .offset(((*pidx.borrow()) as usize)) - .with(|__s: &Edge| __s.u) + (*arr.upgrade().deref()).as_ref().unwrap().borrow() + [((*pidx.borrow()) as usize) as usize] + .u }, v: { - (*arr.upgrade().deref()) - .as_ref() - .unwrap() - .as_pointer() - .offset(((*pidx.borrow()) as usize)) - .with(|__s: &Edge| __s.v) + (*arr.upgrade().deref()).as_ref().unwrap().borrow() + [((*pidx.borrow()) as usize) as usize] + .v }, weight: { - (*arr.upgrade().deref()) - .as_ref() - .unwrap() - .as_pointer() - .offset(((*pidx.borrow()) as usize)) - .with(|__s: &Edge| __s.weight) + (*arr.upgrade().deref()).as_ref().unwrap().borrow() + [((*pidx.borrow()) as usize) as usize] + .weight }, })); let __rhs = Edge { u: { - (*arr.upgrade().deref()) - .as_ref() - .unwrap() - .as_pointer() - .offset(((*start.borrow()) as usize)) - .with(|__s: &Edge| __s.u) + (*arr.upgrade().deref()).as_ref().unwrap().borrow() + [((*start.borrow()) as usize) as usize] + .u }, v: { - (*arr.upgrade().deref()) - .as_ref() - .unwrap() - .as_pointer() - .offset(((*start.borrow()) as usize)) - .with(|__s: &Edge| __s.v) + (*arr.upgrade().deref()).as_ref().unwrap().borrow() + [((*start.borrow()) as usize) as usize] + .v }, weight: { - (*arr.upgrade().deref()) - .as_ref() - .unwrap() - .as_pointer() - .offset(((*start.borrow()) as usize)) - .with(|__s: &Edge| __s.weight) + (*arr.upgrade().deref()).as_ref().unwrap().borrow() + [((*start.borrow()) as usize) as usize] + .weight }, }; (*arr.upgrade().deref()).as_ref().unwrap().borrow_mut()[((*pidx.borrow()) as usize) as usize] = @@ -122,78 +103,58 @@ pub fn partition_0(arr: Ptr>>>, start: i32, end: i32) - let j: Value = Rc::new(RefCell::new((*end.borrow()))); 'loop_: while ((*i.borrow()) < (*pidx.borrow())) && ((*j.borrow()) > (*pidx.borrow())) { 'loop_: while { - let _lhs = (*arr.upgrade().deref()) - .as_ref() - .unwrap() - .as_pointer() - .offset(((*i.borrow()) as usize)) - .with(|__s: &Edge| __s.weight); - _lhs <= pivot.with(|__s: &Edge| __s.weight) + let _lhs = { + (*arr.upgrade().deref()).as_ref().unwrap().borrow() + [((*i.borrow()) as usize) as usize] + .weight + }; + _lhs <= pivot.with(|__s| __s.weight) } { (*i.borrow_mut()).prefix_inc(); } 'loop_: while { - let _lhs = (*arr.upgrade().deref()) - .as_ref() - .unwrap() - .as_pointer() - .offset(((*j.borrow()) as usize)) - .with(|__s: &Edge| __s.weight); - _lhs > pivot.with(|__s: &Edge| __s.weight) + let _lhs = { + (*arr.upgrade().deref()).as_ref().unwrap().borrow() + [((*j.borrow()) as usize) as usize] + .weight + }; + _lhs > pivot.with(|__s| __s.weight) } { (*j.borrow_mut()).prefix_dec(); } if ((*i.borrow()) < (*pidx.borrow())) && ((*j.borrow()) > (*pidx.borrow())) { (*tmp.borrow_mut()) = Edge { u: { - (*arr.upgrade().deref()) - .as_ref() - .unwrap() - .as_pointer() - .offset(((*i.borrow()) as usize)) - .with(|__s: &Edge| __s.u) + (*arr.upgrade().deref()).as_ref().unwrap().borrow() + [((*i.borrow()) as usize) as usize] + .u }, v: { - (*arr.upgrade().deref()) - .as_ref() - .unwrap() - .as_pointer() - .offset(((*i.borrow()) as usize)) - .with(|__s: &Edge| __s.v) + (*arr.upgrade().deref()).as_ref().unwrap().borrow() + [((*i.borrow()) as usize) as usize] + .v }, weight: { - (*arr.upgrade().deref()) - .as_ref() - .unwrap() - .as_pointer() - .offset(((*i.borrow()) as usize)) - .with(|__s: &Edge| __s.weight) + (*arr.upgrade().deref()).as_ref().unwrap().borrow() + [((*i.borrow()) as usize) as usize] + .weight }, }; let __rhs = Edge { u: { - (*arr.upgrade().deref()) - .as_ref() - .unwrap() - .as_pointer() - .offset(((*j.borrow()) as usize)) - .with(|__s: &Edge| __s.u) + (*arr.upgrade().deref()).as_ref().unwrap().borrow() + [((*j.borrow()) as usize) as usize] + .u }, v: { - (*arr.upgrade().deref()) - .as_ref() - .unwrap() - .as_pointer() - .offset(((*j.borrow()) as usize)) - .with(|__s: &Edge| __s.v) + (*arr.upgrade().deref()).as_ref().unwrap().borrow() + [((*j.borrow()) as usize) as usize] + .v }, weight: { - (*arr.upgrade().deref()) - .as_ref() - .unwrap() - .as_pointer() - .offset(((*j.borrow()) as usize)) - .with(|__s: &Edge| __s.weight) + (*arr.upgrade().deref()).as_ref().unwrap().borrow() + [((*j.borrow()) as usize) as usize] + .weight }, }; (*arr.upgrade().deref()).as_ref().unwrap().borrow_mut() @@ -249,9 +210,9 @@ pub struct DisjointSet { impl DisjointSet { pub fn move_from(_a0: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - rank: { _a0.with_mut(|__s: &mut DisjointSet| __s.rank.take()) }, - parent: { _a0.with_mut(|__s: &mut DisjointSet| __s.parent.take()) }, - n: { _a0.with(|__s: &DisjointSet| __s.n) }, + rank: field!(_a0, rank).with_mut(|__v: &mut Option>>| __v.take()), + parent: field!(_a0, parent).with_mut(|__v: &mut Option>>| __v.take()), + n: { (*_a0.upgrade().deref()).n }, })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() @@ -291,9 +252,9 @@ pub struct Graph { impl Graph { pub fn move_from(_a0: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - edges: { _a0.with_mut(|__s: &mut Graph| __s.edges.take()) }, - V: { _a0.with(|__s: &Graph| __s.V) }, - E: { _a0.with(|__s: &Graph| __s.E) }, + edges: field!(_a0, edges).with_mut(|__v: &mut Option>>| __v.take()), + V: { (*_a0.upgrade().deref()).V }, + E: { (*_a0.upgrade().deref()).E }, })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() @@ -323,42 +284,53 @@ impl ByteRepr for Graph { pub fn MSTKruskal_2(graph: Ptr) -> f64 { ({ let _arr: Ptr>>> = field_ptr!(graph, edges); - let _end: i32 = (graph.with(|__s: &Graph| __s.E) - 1); + let _end: i32 = (graph.with(|__s| __s.E) - 1); quicksort_1(_arr, 0, _end) }); let set: Value = Rc::new(RefCell::new(DisjointSet { - rank: { - Some(Rc::new(RefCell::new( - (0..(graph.with(|__s: &Graph| __s.V) as usize)) - .map(|_| ::default()) - .collect::>(), - ))) - }, - parent: { - Some(Rc::new(RefCell::new( - (0..(graph.with(|__s: &Graph| __s.V) as usize)) - .map(|_| ::default()) - .collect::>(), - ))) - }, - n: { graph.with(|__s: &Graph| __s.V) }, + rank: Some(Rc::new(RefCell::new( + (0..(graph.with(|__s| __s.V) as usize)) + .map(|_| ::default()) + .collect::>(), + ))), + parent: Some(Rc::new(RefCell::new( + (0..(graph.with(|__s| __s.V) as usize)) + .map(|_| ::default()) + .collect::>(), + ))), + n: graph.with(|__s| __s.V), })); ({ DisjointSetImpl::makeSet(&set.as_pointer()) }); let total_weight: Value = Rc::new(RefCell::new(0_f64)); let i: Value = Rc::new(RefCell::new(0)); 'loop_: while { let _lhs = (*i.borrow()); - _lhs < graph.with(|__s: &Graph| __s.E) + _lhs < graph.with(|__s| __s.E) } { - let x: Value = Rc::new(RefCell::new(graph.with(|__s: &Graph| { - __s.edges.as_ref().unwrap().borrow()[((*i.borrow()) as usize) as usize].u - }))); - let y: Value = Rc::new(RefCell::new(graph.with(|__s: &Graph| { - __s.edges.as_ref().unwrap().borrow()[((*i.borrow()) as usize) as usize].v - }))); - let w: Value = Rc::new(RefCell::new(graph.with(|__s: &Graph| { - __s.edges.as_ref().unwrap().borrow()[((*i.borrow()) as usize) as usize].weight - }))); + let x: Value = Rc::new(RefCell::new({ + graph + .with(|__s| __s.edges.clone()) + .as_ref() + .unwrap() + .borrow()[((*i.borrow()) as usize) as usize] + .u + })); + let y: Value = Rc::new(RefCell::new({ + graph + .with(|__s| __s.edges.clone()) + .as_ref() + .unwrap() + .borrow()[((*i.borrow()) as usize) as usize] + .v + })); + let w: Value = Rc::new(RefCell::new({ + graph + .with(|__s| __s.edges.clone()) + .as_ref() + .unwrap() + .borrow()[((*i.borrow()) as usize) as usize] + .weight + })); if (({ DisjointSetImpl::find(&set.as_pointer(), (*x.borrow())) }) != ({ DisjointSetImpl::find(&set.as_pointer(), (*y.borrow())) })) { @@ -385,27 +357,42 @@ fn main_0() -> i32 { V: (*V.borrow()), E: (*E.borrow()), })); - (*graph.borrow()).edges.as_ref().unwrap().borrow_mut()[(0_usize) as usize] = Edge { + { (*graph.borrow()).edges.clone() } + .as_ref() + .unwrap() + .borrow_mut()[(0_usize) as usize] = Edge { u: 0, v: 1, weight: 10_f64, }; - (*graph.borrow()).edges.as_ref().unwrap().borrow_mut()[(1_usize) as usize] = Edge { + { (*graph.borrow()).edges.clone() } + .as_ref() + .unwrap() + .borrow_mut()[(1_usize) as usize] = Edge { u: 1, v: 3, weight: 15_f64, }; - (*graph.borrow()).edges.as_ref().unwrap().borrow_mut()[(2_usize) as usize] = Edge { + { (*graph.borrow()).edges.clone() } + .as_ref() + .unwrap() + .borrow_mut()[(2_usize) as usize] = Edge { u: 2, v: 3, weight: 4_f64, }; - (*graph.borrow()).edges.as_ref().unwrap().borrow_mut()[(3_usize) as usize] = Edge { + { (*graph.borrow()).edges.clone() } + .as_ref() + .unwrap() + .borrow_mut()[(3_usize) as usize] = Edge { u: 2, v: 0, weight: 6_f64, }; - (*graph.borrow()).edges.as_ref().unwrap().borrow_mut()[(4_usize) as usize] = Edge { + { (*graph.borrow()).edges.clone() } + .as_ref() + .unwrap() + .borrow_mut()[(4_usize) as usize] = Edge { u: 0, v: 3, weight: 5_f64, @@ -423,36 +410,49 @@ pub trait DisjointSetImpl { impl DisjointSetImpl for Ptr { fn makeSet(&self) { let i: Value = Rc::new(RefCell::new(0)); - 'loop_: while ((*i.borrow()) < (*self).with(|__s: &DisjointSet| __s.n)) { + 'loop_: while ((*i.borrow()) < (*self).with(|__s| __s.n)) { let __rhs = (*i.borrow()); - (*self).with_mut(|__s: &mut DisjointSet| { - __s.parent.as_ref().unwrap().borrow_mut()[((*i.borrow()) as usize) as usize] = __rhs - }); - (*self).with_mut(|__s: &mut DisjointSet| { - __s.rank.as_ref().unwrap().borrow_mut()[((*i.borrow()) as usize) as usize] = 1 - }); + (*self) + .with(|__s| __s.parent.clone()) + .as_ref() + .unwrap() + .borrow_mut()[((*i.borrow()) as usize) as usize] = __rhs; + (*self) + .with(|__s| __s.rank.clone()) + .as_ref() + .unwrap() + .borrow_mut()[((*i.borrow()) as usize) as usize] = 1; (*i.borrow_mut()).postfix_inc(); } } fn find(&self, x: i32) -> i32 { let x: Value = Rc::new(RefCell::new(x)); - if ((*self).with(|__s: &DisjointSet| { - __s.parent.as_ref().unwrap().borrow()[((*x.borrow()) as usize) as usize] - }) != (*x.borrow())) + if ((*self) + .with(|__s| __s.parent.clone()) + .as_ref() + .unwrap() + .borrow()[((*x.borrow()) as usize) as usize] + != (*x.borrow())) { let __rhs = ({ - let _x: i32 = (*self).with(|__s: &DisjointSet| { - __s.parent.as_ref().unwrap().borrow()[((*x.borrow()) as usize) as usize] - }); + let _x: i32 = (*self) + .with(|__s| __s.parent.clone()) + .as_ref() + .unwrap() + .borrow()[((*x.borrow()) as usize) as usize]; DisjointSetImpl::find(self, _x) }); - (*self).with_mut(|__s: &mut DisjointSet| { - __s.parent.as_ref().unwrap().borrow_mut()[((*x.borrow()) as usize) as usize] = __rhs - }); + (*self) + .with(|__s| __s.parent.clone()) + .as_ref() + .unwrap() + .borrow_mut()[((*x.borrow()) as usize) as usize] = __rhs; } - return (*self).with(|__s: &DisjointSet| { - __s.parent.as_ref().unwrap().borrow()[((*x.borrow()) as usize) as usize] - }); + return (*self) + .with(|__s| __s.parent.clone()) + .as_ref() + .unwrap() + .borrow()[((*x.borrow()) as usize) as usize]; } fn merge(&self, x: i32, y: i32) { let x: Value = Rc::new(RefCell::new(x)); @@ -466,45 +466,64 @@ impl DisjointSetImpl for Ptr { if ((*xset.borrow()) == (*yset.borrow())) { return; } - if ((*self).with(|__s: &DisjointSet| { - __s.rank.as_ref().unwrap().borrow()[((*xset.borrow()) as usize) as usize] - }) < (*self).with(|__s: &DisjointSet| { - __s.rank.as_ref().unwrap().borrow()[((*yset.borrow()) as usize) as usize] - })) { - (*self).with_mut(|__s: &mut DisjointSet| { - __s.parent.as_ref().unwrap().borrow_mut()[((*xset.borrow()) as usize) as usize] = - (*yset.borrow()) - }); - } else if ((*self).with(|__s: &DisjointSet| { - __s.rank.as_ref().unwrap().borrow()[((*xset.borrow()) as usize) as usize] - }) > (*self).with(|__s: &DisjointSet| { - __s.rank.as_ref().unwrap().borrow()[((*yset.borrow()) as usize) as usize] - })) { - (*self).with_mut(|__s: &mut DisjointSet| { - __s.parent.as_ref().unwrap().borrow_mut()[((*yset.borrow()) as usize) as usize] = - (*xset.borrow()) - }); + if ((*self) + .with(|__s| __s.rank.clone()) + .as_ref() + .unwrap() + .borrow()[((*xset.borrow()) as usize) as usize] + < (*self) + .with(|__s| __s.rank.clone()) + .as_ref() + .unwrap() + .borrow()[((*yset.borrow()) as usize) as usize]) + { + (*self) + .with(|__s| __s.parent.clone()) + .as_ref() + .unwrap() + .borrow_mut()[((*xset.borrow()) as usize) as usize] = (*yset.borrow()); + } else if ((*self) + .with(|__s| __s.rank.clone()) + .as_ref() + .unwrap() + .borrow()[((*xset.borrow()) as usize) as usize] + > (*self) + .with(|__s| __s.rank.clone()) + .as_ref() + .unwrap() + .borrow()[((*yset.borrow()) as usize) as usize]) + { + (*self) + .with(|__s| __s.parent.clone()) + .as_ref() + .unwrap() + .borrow_mut()[((*yset.borrow()) as usize) as usize] = (*xset.borrow()); } else { - (*self).with_mut(|__s: &mut DisjointSet| { - __s.parent.as_ref().unwrap().borrow_mut()[((*yset.borrow()) as usize) as usize] = - (*xset.borrow()) - }); - let __rhs = ((*self).with(|__s: &DisjointSet| { - __s.rank.as_ref().unwrap().borrow()[((*xset.borrow()) as usize) as usize] - }) + 1); - (*self).with_mut(|__s: &mut DisjointSet| { - __s.rank.as_ref().unwrap().borrow_mut()[((*xset.borrow()) as usize) as usize] = - __rhs - }); + (*self) + .with(|__s| __s.parent.clone()) + .as_ref() + .unwrap() + .borrow_mut()[((*yset.borrow()) as usize) as usize] = (*xset.borrow()); + let __rhs = ((*self) + .with(|__s| __s.rank.clone()) + .as_ref() + .unwrap() + .borrow()[((*xset.borrow()) as usize) as usize] + + 1); + (*self) + .with(|__s| __s.rank.clone()) + .as_ref() + .unwrap() + .borrow_mut()[((*xset.borrow()) as usize) as usize] = __rhs; } } fn move_assign(&self, _a0: Ptr) -> Ptr { (field_ptr!((*self), rank) as Ptr>>>) - .write(_a0.with_mut(|__s: &mut DisjointSet| __s.rank.take())); + .write(field!(_a0, rank).with_mut(|__v: &mut Option>>| __v.take())); (field_ptr!((*self), parent) as Ptr>>>) - .write(_a0.with_mut(|__s: &mut DisjointSet| __s.parent.take())); - let __rhs = _a0.with(|__s: &DisjointSet| __s.n); - (*self).with_mut(|__s: &mut DisjointSet| __s.n = __rhs); + .write(field!(_a0, parent).with_mut(|__v: &mut Option>>| __v.take())); + let __rhs = { (*_a0.upgrade().deref()).n }; + field!((*self), n).write(__rhs); return (*self).clone(); } } @@ -514,11 +533,11 @@ pub trait GraphImpl { impl GraphImpl for Ptr { fn move_assign(&self, _a0: Ptr) -> Ptr { (field_ptr!((*self), edges) as Ptr>>>) - .write(_a0.with_mut(|__s: &mut Graph| __s.edges.take())); - let __rhs = _a0.with(|__s: &Graph| __s.V); - (*self).with_mut(|__s: &mut Graph| __s.V = __rhs); - let __rhs = _a0.with(|__s: &Graph| __s.E); - (*self).with_mut(|__s: &mut Graph| __s.E = __rhs); + .write(field!(_a0, edges).with_mut(|__v: &mut Option>>| __v.take())); + let __rhs = { (*_a0.upgrade().deref()).V }; + field!((*self), V).write(__rhs); + let __rhs = { (*_a0.upgrade().deref()).E }; + field!((*self), E).write(__rhs); return (*self).clone(); } } diff --git a/tests/unit/out/refcount/libc_char_ptr_field.rs b/tests/unit/out/refcount/libc_char_ptr_field.rs index e1a29dcb99..691022c8fb 100644 --- a/tests/unit/out/refcount/libc_char_ptr_field.rs +++ b/tests/unit/out/refcount/libc_char_ptr_field.rs @@ -26,9 +26,7 @@ fn main_0() -> i32 { if !(!(*pw.borrow()).is_null()) { return 0; } - let home: Value> = Rc::new(RefCell::new( - (*(*(*pw.borrow()).upgrade().deref()).pw_dir.borrow()).clone(), - )); + let home: Value> = Rc::new(RefCell::new((*pw.borrow()).with(|__s| __s.pw_dir.clone()))); let d: Value> = Rc::new(RefCell::new( match nix::dir::Dir::open( Ptr::::from_string_literal(b"/tmp") @@ -55,7 +53,7 @@ fn main_0() -> i32 { }), )); let dname: Value> = Rc::new(RefCell::new( - ((*(*d.borrow()).upgrade().deref()).d_name.as_pointer() as Ptr), + ((*d.borrow()).with(|__s| __s.d_name.clone()).as_pointer() as Ptr), )); return 0; } diff --git a/tests/unit/out/refcount/libc_struct_without_default.rs b/tests/unit/out/refcount/libc_struct_without_default.rs index 474d6683bd..9f7b35ecb0 100644 --- a/tests/unit/out/refcount/libc_struct_without_default.rs +++ b/tests/unit/out/refcount/libc_struct_without_default.rs @@ -15,12 +15,10 @@ pub struct UserDefined { } impl Clone for UserDefined { fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - a: { Rc::new(RefCell::new((*self.a.borrow()).clone())) }, - v: { Rc::new(RefCell::new((*self.v.borrow()).clone())) }, - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() + Self { + a: Rc::new(RefCell::new((*self.a.borrow()).clone())), + v: Rc::new(RefCell::new((*self.v.borrow()).clone())), + } } } impl Default for UserDefined { @@ -84,30 +82,35 @@ pub fn main() { } fn main_0() -> i32 { let p: Value = Rc::new(RefCell::new(Default::default())); - (*(*p.borrow()).fd.borrow_mut()) = -1_i32; - (*(*p.borrow()).events.borrow_mut()) = 0_i16; - (*(*p.borrow()).revents.borrow_mut()) = 2_i16; - assert!(((*(*p.borrow()).fd.borrow()) == -1_i32)); - assert!((((*(*p.borrow()).events.borrow()) as i32) == 0)); - assert!((((*(*p.borrow()).revents.borrow()) as i32) == 2)); + (*p.borrow_mut()).fd = -1_i32; + (*p.borrow_mut()).events = 0_i16; + (*p.borrow_mut()).revents = 2_i16; + assert!(({ (*p.borrow()).fd } == -1_i32)); + assert!((({ (*p.borrow()).events } as i32) == 0)); + assert!((({ (*p.borrow()).revents } as i32) == 2)); let ia: Value = Rc::new(RefCell::new(Default::default())); - (*(*ia.borrow()).s_addr.borrow_mut()) = 1_u32; - assert!(((*(*ia.borrow()).s_addr.borrow()) == 1_u32)); + (*ia.borrow_mut()).s_addr = 1_u32; + assert!(({ (*ia.borrow()).s_addr } == 1_u32)); let t: Value = Rc::new(RefCell::new(Default::default())); - (*(*t.borrow()).tm_year.borrow_mut()) = 124; - (*(*t.borrow()).tm_mon.borrow_mut()) = 5; - (*(*t.borrow()).tm_mday.borrow_mut()) = 15; - assert!(((*(*t.borrow()).tm_year.borrow()) == 124)); - assert!(((*(*t.borrow()).tm_mon.borrow()) == 5)); - assert!(((*(*t.borrow()).tm_mday.borrow()) == 15)); + (*t.borrow_mut()).tm_year = 124; + (*t.borrow_mut()).tm_mon = 5; + (*t.borrow_mut()).tm_mday = 15; + assert!(({ (*t.borrow()).tm_year } == 124)); + assert!(({ (*t.borrow()).tm_mon } == 5)); + assert!(({ (*t.borrow()).tm_mday } == 15)); let st: Value = Rc::new(RefCell::new(Default::default())); - (*(*st.borrow()).st_size.borrow_mut()) = 1024_i64; - assert!(((*(*st.borrow()).st_size.borrow()) == 1024_i64)); + (*st.borrow_mut()).st_size = 1024_i64; + assert!(({ (*st.borrow()).st_size } == 1024_i64)); let ud: Value = Rc::new(RefCell::new(::default())); - assert!(({ (*(*ud.borrow()).a.borrow())[(0_usize) as usize] } == 0)); - assert!(((*(*ud.borrow()).v.borrow()).len() == 0_usize)); + assert!( + ((({ (*ud.borrow()).a.clone() }.as_pointer() as Ptr) + .offset(0_usize) + .read()) + == 0) + ); + assert!(((*{ (*ud.borrow()).v.clone() }.borrow()).len() == 0_usize)); let filt: Value = Rc::new(RefCell::new(::default())); - assert!((((*(*filt.borrow()).addr.sa_family.borrow()) as i32) == 0)); + assert!((({ (*filt.borrow()).addr.sa_family } as i32) == 0)); return 0; } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/linked_list.rs b/tests/unit/out/refcount/linked_list.rs index 4924ff1a9e..6ec880119f 100644 --- a/tests/unit/out/refcount/linked_list.rs +++ b/tests/unit/out/refcount/linked_list.rs @@ -34,7 +34,7 @@ pub fn Find_0(head: Ptr, idx: i32) -> Ptr { let curr: Value> = Rc::new(RefCell::new((*head.borrow()).clone())); let i: Value = Rc::new(RefCell::new(0)); 'loop_: while ((*i.borrow()) < (*idx.borrow())) { - let __rhs = (*curr.borrow()).with(|__s: &Node| (__s.next).clone()); + let __rhs = (*curr.borrow()).with(|__s| __s.next.clone()); (*curr.borrow_mut()) = __rhs; (*i.borrow_mut()).postfix_inc(); } @@ -42,8 +42,8 @@ pub fn Find_0(head: Ptr, idx: i32) -> Ptr { } pub fn Append_1(head: Ptr, new_node: Ptr) { let curr: Value> = Rc::new(RefCell::new((head).clone())); - 'loop_: while !(((*curr.borrow()).with(|__s: &Node| (__s.next).clone())).is_null()) { - let __rhs = (*curr.borrow()).with(|__s: &Node| (__s.next).clone()); + 'loop_: while !(((*curr.borrow()).with(|__s| __s.next.clone())).is_null()) { + let __rhs = (*curr.borrow()).with(|__s| __s.next.clone()); (*curr.borrow_mut()) = __rhs; } ({ NodeImpl::SetNext(&(*curr.borrow()), (new_node).clone()) }); @@ -55,19 +55,19 @@ pub fn Delete_2(head: Ptr, val: i32) -> Ptr { let prev: Value> = Rc::new(RefCell::new(Ptr::::null())); 'loop_: while !((*curr.borrow()).is_null()) { if { - let _lhs = (*curr.borrow()).with(|__s: &Node| __s.val); + let _lhs = (*curr.borrow()).with(|__s| __s.val); _lhs == (*val.borrow()) } { if !((*prev.borrow()).is_null()) { - let __rhs = (*curr.borrow()).with(|__s: &Node| (__s.next).clone()); - (*prev.borrow()).with_mut(|__s: &mut Node| __s.next = __rhs); + let __rhs = (*curr.borrow()).with(|__s| __s.next.clone()); + field!((*prev.borrow()), next).write(__rhs); return (*head.borrow()).clone(); } else { - return (*curr.borrow()).with(|__s: &Node| (__s.next).clone()); + return (*curr.borrow()).with(|__s| __s.next.clone()); } } (*prev.borrow_mut()) = (*curr.borrow()).clone(); - let __rhs = (*curr.borrow()).with(|__s: &Node| (__s.next).clone()); + let __rhs = (*curr.borrow()).with(|__s| __s.next.clone()); (*curr.borrow_mut()) = __rhs; } return (*head.borrow()).clone(); @@ -152,11 +152,11 @@ fn main_0() -> i32 { let __rhs = ({ Delete_2((*head.borrow()).clone(), -2_i32) }); (*head.borrow_mut()) = __rhs; assert!( - (((((({ Find_0((*head.borrow()).clone(), 0,) }).with(|__s: &Node| __s.val) == 4) - && (({ Find_0((*head.borrow()).clone(), 1,) }).with(|__s: &Node| __s.val) == 3)) - && (({ Find_0((*head.borrow()).clone(), 2,) }).with(|__s: &Node| __s.val) == 2)) - && (({ Find_0((*head.borrow()).clone(), 3,) }).with(|__s: &Node| __s.val) == 1)) - && (({ Find_0((*head.borrow()).clone(), 4,) }).with(|__s: &Node| __s.val) == -1_i32)) + (((((({ Find_0((*head.borrow()).clone(), 0,) }).with(|__s| __s.val) == 4) + && (({ Find_0((*head.borrow()).clone(), 1,) }).with(|__s| __s.val) == 3)) + && (({ Find_0((*head.borrow()).clone(), 2,) }).with(|__s| __s.val) == 2)) + && (({ Find_0((*head.borrow()).clone(), 3,) }).with(|__s| __s.val) == 1)) + && (({ Find_0((*head.borrow()).clone(), 4,) }).with(|__s| __s.val) == -1_i32)) && (({ Find_0((*head.borrow()).clone(), 5,) }).is_null()) ); return 0; @@ -167,7 +167,7 @@ pub trait NodeImpl { impl NodeImpl for Ptr { fn SetNext(&self, next: Ptr) { let next: Value> = Rc::new(RefCell::new(next)); - (*self).with_mut(|__s: &mut Node| __s.next = (*next.borrow()).clone()); + field!((*self), next).write((*next.borrow()).clone()); } } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/map.rs b/tests/unit/out/refcount/map.rs index 26e5b068a5..d9424f5739 100644 --- a/tests/unit/out/refcount/map.rs +++ b/tests/unit/out/refcount/map.rs @@ -365,14 +365,15 @@ fn main_0() -> i32 { let i: Value = Rc::new(RefCell::new(0_u32)); 'loop_: while (((*i.borrow()) as usize) < (*indexes.borrow()).len()) { let __rhs = ((*i.borrow()).wrapping_rem(2_u32) != 0); - let __idx = ((indexes.as_pointer() as Ptr) - .offset(((*i.borrow()) as usize)) - .read()); (m2.as_pointer() as Ptr>>) .with_mut(|__v: &mut BTreeMap>| { - __v.entry(__idx) - .or_insert_with(|| Rc::new(RefCell::new(::default()))) - .as_pointer() + __v.entry( + ((indexes.as_pointer() as Ptr) + .offset(((*i.borrow()) as usize)) + .read()), + ) + .or_insert_with(|| Rc::new(RefCell::new(::default()))) + .as_pointer() }) .write(__rhs); (*i.borrow_mut()).prefix_inc(); diff --git a/tests/unit/out/refcount/move_assign.rs b/tests/unit/out/refcount/move_assign.rs index 5d30212622..f5cb8b3b7b 100644 --- a/tests/unit/out/refcount/move_assign.rs +++ b/tests/unit/out/refcount/move_assign.rs @@ -20,10 +20,10 @@ impl MoveOnly { } pub fn move_from(o: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - v: { o.with(|__s: &MoveOnly| __s.v) }, + v: o.with(|__s| __s.v), })); let this: Ptr = __this.as_pointer(); - o.with_mut(|__s: &mut MoveOnly| __s.v = 0); + field!(o, v).write(0); Rc::try_unwrap(__this).ok().unwrap().into_inner() } } @@ -133,10 +133,13 @@ fn main_0() -> i32 { ) }); assert!( - ((vec_.as_pointer() as Ptr) - .offset(0_usize) - .with(|__s: &MoveOnly| __s.v) - == 8) + ({ + (*(vec_.as_pointer() as Ptr) + .offset(0_usize) + .upgrade() + .deref()) + .v + } == 8) ); assert!(({ (*d.borrow()).v } == 0)); let m: Value = Rc::new(RefCell::new(ConstMoveAssign::new())); @@ -155,13 +158,13 @@ pub trait ConstMoveAssignImpl { } impl ConstMoveAssignImpl for Ptr { fn operator_assign_2(&self, o: Ptr) -> Ptr { - let __rhs = (o.with(|__s: &ConstMoveAssign| __s.mark) + 1); - (*self).with_mut(|__s: &mut ConstMoveAssign| __s.mark = __rhs); + let __rhs = (o.with(|__s| __s.mark) + 1); + field!((*self), mark).write(__rhs); return (*self).clone(); } fn operator_assign_3(&self, o: Ptr) -> Ptr { - let __rhs = (o.with(|__s: &ConstMoveAssign| __s.mark) + 10); - (*self).with_mut(|__s: &mut ConstMoveAssign| __s.mark = __rhs); + let __rhs = (o.with(|__s| __s.mark) + 10); + field!((*self), mark).write(__rhs); return (*self).clone(); } } @@ -173,9 +176,9 @@ impl MoveOnlyImpl for Ptr { if ((*self) == (o)) { return (*self).clone(); } - let __rhs = o.with(|__s: &MoveOnly| __s.v); - (*self).with_mut(|__s: &mut MoveOnly| __s.v = __rhs); - o.with_mut(|__s: &mut MoveOnly| __s.v = 0); + let __rhs = o.with(|__s| __s.v); + field!((*self), v).write(__rhs); + field!(o, v).write(0); return (*self).clone(); } } diff --git a/tests/unit/out/refcount/move_ctor.rs b/tests/unit/out/refcount/move_ctor.rs index 97aaa5f1eb..e28da0b1ae 100644 --- a/tests/unit/out/refcount/move_ctor.rs +++ b/tests/unit/out/refcount/move_ctor.rs @@ -20,10 +20,10 @@ impl MoveOnly { } pub fn move_from(o: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - v: { o.with(|__s: &MoveOnly| __s.v) }, + v: o.with(|__s| __s.v), })); let this: Ptr = __this.as_pointer(); - o.with_mut(|__s: &mut MoveOnly| __s.v = 0); + field!(o, v).write(0); Rc::try_unwrap(__this).ok().unwrap().into_inner() } } @@ -58,14 +58,14 @@ impl ConstMove { } pub fn new_1(o: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - mark: { (o.with(|__s: &ConstMove| __s.mark) + 1) }, + mark: (o.with(|__s| __s.mark) + 1), })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } pub fn new_2(o: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - mark: { (o.with(|__s: &ConstMove| __s.mark) + 10) }, + mark: (o.with(|__s| __s.mark) + 10), })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() @@ -116,21 +116,21 @@ impl ThrowingMove { } pub fn copy_from(o: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - v: { o.with(|__s: &ThrowingMove| __s.v) }, - copies: { (o.with(|__s: &ThrowingMove| __s.copies) + 1) }, - moves: { o.with(|__s: &ThrowingMove| __s.moves) }, + v: o.with(|__s| __s.v), + copies: (o.with(|__s| __s.copies) + 1), + moves: o.with(|__s| __s.moves), })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } pub fn move_from(o: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - v: { o.with(|__s: &ThrowingMove| __s.v) }, - copies: { o.with(|__s: &ThrowingMove| __s.copies) }, - moves: { (o.with(|__s: &ThrowingMove| __s.moves) + 1) }, + v: o.with(|__s| __s.v), + copies: o.with(|__s| __s.copies), + moves: (o.with(|__s| __s.moves) + 1), })); let this: Ptr = __this.as_pointer(); - o.with_mut(|__s: &mut ThrowingMove| __s.v = 0); + field!(o, v).write(0); Rc::try_unwrap(__this).ok().unwrap().into_inner() } } @@ -188,21 +188,21 @@ impl NoexceptMove { } pub fn copy_from(o: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - v: { o.with(|__s: &NoexceptMove| __s.v) }, - copies: { (o.with(|__s: &NoexceptMove| __s.copies) + 1) }, - moves: { o.with(|__s: &NoexceptMove| __s.moves) }, + v: o.with(|__s| __s.v), + copies: (o.with(|__s| __s.copies) + 1), + moves: o.with(|__s| __s.moves), })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } pub fn move_from(o: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - v: { o.with(|__s: &NoexceptMove| __s.v) }, - copies: { o.with(|__s: &NoexceptMove| __s.copies) }, - moves: { (o.with(|__s: &NoexceptMove| __s.moves) + 1) }, + v: o.with(|__s| __s.v), + copies: o.with(|__s| __s.copies), + moves: (o.with(|__s| __s.moves) + 1), })); let this: Ptr = __this.as_pointer(); - o.with_mut(|__s: &mut NoexceptMove| __s.v = 0); + field!(o, v).write(0); Rc::try_unwrap(__this).ok().unwrap().into_inner() } } @@ -271,14 +271,20 @@ fn main_0() -> i32 { let f: Value = Rc::new(RefCell::new(MoveOnly::new({ 8 }))); (*vec_.borrow_mut()).push(MoveOnly::move_from({ f.as_pointer() })); assert!( - ((vec_.as_pointer() as Ptr) - .offset(0_usize) - .with(|__s: &MoveOnly| __s.v) - == 7) - && ((vec_.as_pointer() as Ptr) - .offset(1_usize) - .with(|__s: &MoveOnly| __s.v) - == 8) + ({ + (*(vec_.as_pointer() as Ptr) + .offset(0_usize) + .upgrade() + .deref()) + .v + } == 7) + && ({ + (*(vec_.as_pointer() as Ptr) + .offset(1_usize) + .upgrade() + .deref()) + .v + } == 8) ); assert!(({ (*f.borrow()).v } == 0)); let m: Value = Rc::new(RefCell::new(ConstMove::new())); diff --git a/tests/unit/out/refcount/move_this.rs b/tests/unit/out/refcount/move_this.rs index 2f8e9c08bf..a6fb8569c0 100644 --- a/tests/unit/out/refcount/move_this.rs +++ b/tests/unit/out/refcount/move_this.rs @@ -20,17 +20,17 @@ impl Chain { } pub fn copy_from(o: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - v: { (o.with(|__s: &Chain| __s.v) + 100) }, + v: (o.with(|__s| __s.v) + 100), })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() } pub fn move_from(o: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - v: { (o.with(|__s: &Chain| __s.v) + 1) }, + v: (o.with(|__s| __s.v) + 1), })); let this: Ptr = __this.as_pointer(); - o.with_mut(|__s: &mut Chain| __s.v = 0); + field!(o, v).write(0); Rc::try_unwrap(__this).ok().unwrap().into_inner() } } @@ -103,12 +103,18 @@ pub trait ChainImpl { impl ChainImpl for Ptr { fn add_4(&self, n: i32) -> Ptr { let n: Value = Rc::new(RefCell::new(n)); - (*self).with_mut(|__s: &mut Chain| __s.v += (*n.borrow())); + { + let _ptr = field!((*self), v); + _ptr.write(_ptr.read() + (*n.borrow())) + }; return (*self).clone(); } fn add_5(&self, n: i32) -> Ptr { let n: Value = Rc::new(RefCell::new(n)); - (*self).with_mut(|__s: &mut Chain| __s.v += (*n.borrow())); + { + let _ptr = field!((*self), v); + _ptr.write(_ptr.read() + (*n.borrow())) + }; return (*self).clone(); } fn take(&self) -> Chain { diff --git a/tests/unit/out/refcount/netdb.rs b/tests/unit/out/refcount/netdb.rs index 1688bd0468..0035f4c4cb 100644 --- a/tests/unit/out/refcount/netdb.rs +++ b/tests/unit/out/refcount/netdb.rs @@ -8,14 +8,14 @@ use std::os::fd::AsFd; use std::rc::{Rc, Weak}; pub fn test_ipv4_literal_0() { let hints: Value = Rc::new(RefCell::new(Default::default())); - (*(*hints.borrow()).ai_flags.borrow_mut()) = 0; - (*(*hints.borrow()).ai_protocol.borrow_mut()) = 0; - (*(*hints.borrow()).ai_addrlen.borrow_mut()) = 0_u32; - (*(*hints.borrow()).ai_addr.borrow_mut()) = Ptr::::null(); - (*(*hints.borrow()).ai_canonname.borrow_mut()) = Ptr::::null(); - (*(*hints.borrow()).ai_next.borrow_mut()) = Ptr::::null(); - (*(*hints.borrow()).ai_family.borrow_mut()) = libc::AF_INET; - (*(*hints.borrow()).ai_socktype.borrow_mut()) = libc::SOCK_STREAM; + (*hints.borrow_mut()).ai_flags = 0; + (*hints.borrow_mut()).ai_protocol = 0; + (*hints.borrow_mut()).ai_addrlen = 0_u32; + (*hints.borrow_mut()).ai_addr = Ptr::::null(); + (*hints.borrow_mut()).ai_canonname = Ptr::::null(); + (*hints.borrow_mut()).ai_next = Ptr::::null(); + (*hints.borrow_mut()).ai_family = libc::AF_INET; + (*hints.borrow_mut()).ai_socktype = libc::SOCK_STREAM; let res: Value> = Rc::new(RefCell::new(Ptr::::null())); assert!( @@ -27,17 +27,17 @@ pub fn test_ipv4_literal_0() { let __family = if __hints.is_null() { ::libc::AF_UNSPEC } else { - __hints.with(|__h| *__h.ai_family.borrow()) + __hints.with(|__h| __h.ai_family) }; let __socktype = if __hints.is_null() { 0 } else { - __hints.with(|__h| *__h.ai_socktype.borrow()) + __hints.with(|__h| __h.ai_socktype) }; let __protocol = if __hints.is_null() { 0 } else { - __hints.with(|__h| *__h.ai_protocol.borrow()) + __hints.with(|__h| __h.ai_protocol) }; let __port: u16 = if __service.is_null() { 0 @@ -78,30 +78,28 @@ pub fn test_ipv4_literal_0() { } else { let mut __next = Ptr::::null(); for __ip in __addrs.iter().rev() { - let __ai = Addrinfo::default(); - *__ai.ai_socktype.borrow_mut() = __socktype; - *__ai.ai_protocol.borrow_mut() = __protocol; + let mut __ai = Addrinfo::default(); + __ai.ai_socktype = __socktype; + __ai.ai_protocol = __protocol; let __storage = Ptr::alloc(SockaddrStorage::default()); match __ip { ::std::net::IpAddr::V4(__v4) => { - *__ai.ai_family.borrow_mut() = ::libc::AF_INET; - *__ai.ai_addrlen.borrow_mut() = - ::std::mem::size_of::<::libc::sockaddr_in>() as u32; + __ai.ai_family = ::libc::AF_INET; + __ai.ai_addrlen = ::std::mem::size_of::<::libc::sockaddr_in>() as u32; __storage .reinterpret_cast::() .write(SockaddrIn::from_ipv4(__v4, __port)); } ::std::net::IpAddr::V6(__v6) => { - *__ai.ai_family.borrow_mut() = ::libc::AF_INET6; - *__ai.ai_addrlen.borrow_mut() = - ::std::mem::size_of::<::libc::sockaddr_in6>() as u32; + __ai.ai_family = ::libc::AF_INET6; + __ai.ai_addrlen = ::std::mem::size_of::<::libc::sockaddr_in6>() as u32; __storage .reinterpret_cast::() .write(SockaddrIn6::from_ipv6(__v6, __port)); } } - *__ai.ai_addr.borrow_mut() = __storage.reinterpret_cast::(); - *__ai.ai_next.borrow_mut() = __next.clone(); + __ai.ai_addr = __storage.reinterpret_cast::(); + __ai.ai_next = __next.clone(); __next = Ptr::alloc(__ai); } __out.write(__next); @@ -111,38 +109,22 @@ pub fn test_ipv4_literal_0() { != 0) ); assert!((((!((*res.borrow()).is_null())) as i32) != 0)); - assert!( - ((((*(*(*res.borrow()).upgrade().deref()).ai_family.borrow()) == libc::AF_INET) as i32) - != 0) - ); - assert!( - ((((*(*(*res.borrow()).upgrade().deref()).ai_socktype.borrow()) == libc::SOCK_STREAM) - as i32) - != 0) - ); - assert!( - (((((*(*(*res.borrow()).upgrade().deref()).ai_addrlen.borrow()) as usize) == 16usize) - as i32) - != 0) - ); - assert!( - (((!((*(*(*res.borrow()).upgrade().deref()).ai_addr.borrow()).is_null())) as i32) != 0) - ); + assert!(((((*res.borrow()).with(|__s| __s.ai_family) == libc::AF_INET) as i32) != 0)); + assert!(((((*res.borrow()).with(|__s| __s.ai_socktype) == libc::SOCK_STREAM) as i32) != 0)); + assert!((((((*res.borrow()).with(|__s| __s.ai_addrlen) as usize) == 16usize) as i32) != 0)); + assert!((((!(((*res.borrow()).with(|__s| __s.ai_addr.clone())).is_null())) as i32) != 0)); let sin: Value> = Rc::new(RefCell::new( - (*(*(*res.borrow()).upgrade().deref()).ai_addr.borrow()) + (*res.borrow()) + .with(|__s| __s.ai_addr.clone()) .reinterpret_cast::(), )); - assert!( - (((((*(*(*sin.borrow()).upgrade().deref()).sin_family.borrow()) as i32) == libc::AF_INET) - as i32) - != 0) - ); + assert!((((((*sin.borrow()).with(|__s| __s.sin_family) as i32) == libc::AF_INET) as i32) != 0)); let port_be: Value> = Rc::new(RefCell::new(Box::new([ ((8080 / 256) as u8), ((8080 % 256) as u8), ]))); assert!( - ((((((*(*sin.borrow()).upgrade().deref()).sin_port.as_pointer()) as Ptr::) + (((((field_ptr!((*sin.borrow()), sin_port)) as Ptr::) .to_any() .memcmp( &((port_be.as_pointer() as Ptr::) as Ptr::).to_any(), @@ -153,7 +135,7 @@ pub fn test_ipv4_literal_0() { ); let addr_be: Value> = Rc::new(RefCell::new(Box::new([127_u8, 0_u8, 0_u8, 1_u8]))); assert!( - ((((((*(*sin.borrow()).upgrade().deref()).sin_addr.as_pointer()) as Ptr) + (((((field_ptr!((*sin.borrow()), sin_addr)) as Ptr) .to_any() .memcmp( &((addr_be.as_pointer() as Ptr::) as Ptr::).to_any(), @@ -166,11 +148,10 @@ pub fn test_ipv4_literal_0() { let mut __cur = (*res.borrow()).clone(); while !__cur.is_null() { let __next = __cur.with(|__ai| { - let __addr = __ai.ai_addr.borrow(); - if !__addr.is_null() { - __addr.delete(); + if !__ai.ai_addr.is_null() { + __ai.ai_addr.delete(); } - (*__ai.ai_next.borrow()).clone() + __ai.ai_next.clone() }); __cur.delete(); __cur = __next; @@ -179,14 +160,14 @@ pub fn test_ipv4_literal_0() { } pub fn test_ipv6_literal_1() { let hints: Value = Rc::new(RefCell::new(Default::default())); - (*(*hints.borrow()).ai_flags.borrow_mut()) = 0; - (*(*hints.borrow()).ai_protocol.borrow_mut()) = 0; - (*(*hints.borrow()).ai_addrlen.borrow_mut()) = 0_u32; - (*(*hints.borrow()).ai_addr.borrow_mut()) = Ptr::::null(); - (*(*hints.borrow()).ai_canonname.borrow_mut()) = Ptr::::null(); - (*(*hints.borrow()).ai_next.borrow_mut()) = Ptr::::null(); - (*(*hints.borrow()).ai_family.borrow_mut()) = libc::AF_INET6; - (*(*hints.borrow()).ai_socktype.borrow_mut()) = libc::SOCK_STREAM; + (*hints.borrow_mut()).ai_flags = 0; + (*hints.borrow_mut()).ai_protocol = 0; + (*hints.borrow_mut()).ai_addrlen = 0_u32; + (*hints.borrow_mut()).ai_addr = Ptr::::null(); + (*hints.borrow_mut()).ai_canonname = Ptr::::null(); + (*hints.borrow_mut()).ai_next = Ptr::::null(); + (*hints.borrow_mut()).ai_family = libc::AF_INET6; + (*hints.borrow_mut()).ai_socktype = libc::SOCK_STREAM; let res: Value> = Rc::new(RefCell::new(Ptr::::null())); assert!( @@ -198,17 +179,17 @@ pub fn test_ipv6_literal_1() { let __family = if __hints.is_null() { ::libc::AF_UNSPEC } else { - __hints.with(|__h| *__h.ai_family.borrow()) + __hints.with(|__h| __h.ai_family) }; let __socktype = if __hints.is_null() { 0 } else { - __hints.with(|__h| *__h.ai_socktype.borrow()) + __hints.with(|__h| __h.ai_socktype) }; let __protocol = if __hints.is_null() { 0 } else { - __hints.with(|__h| *__h.ai_protocol.borrow()) + __hints.with(|__h| __h.ai_protocol) }; let __port: u16 = if __service.is_null() { 0 @@ -249,30 +230,28 @@ pub fn test_ipv6_literal_1() { } else { let mut __next = Ptr::::null(); for __ip in __addrs.iter().rev() { - let __ai = Addrinfo::default(); - *__ai.ai_socktype.borrow_mut() = __socktype; - *__ai.ai_protocol.borrow_mut() = __protocol; + let mut __ai = Addrinfo::default(); + __ai.ai_socktype = __socktype; + __ai.ai_protocol = __protocol; let __storage = Ptr::alloc(SockaddrStorage::default()); match __ip { ::std::net::IpAddr::V4(__v4) => { - *__ai.ai_family.borrow_mut() = ::libc::AF_INET; - *__ai.ai_addrlen.borrow_mut() = - ::std::mem::size_of::<::libc::sockaddr_in>() as u32; + __ai.ai_family = ::libc::AF_INET; + __ai.ai_addrlen = ::std::mem::size_of::<::libc::sockaddr_in>() as u32; __storage .reinterpret_cast::() .write(SockaddrIn::from_ipv4(__v4, __port)); } ::std::net::IpAddr::V6(__v6) => { - *__ai.ai_family.borrow_mut() = ::libc::AF_INET6; - *__ai.ai_addrlen.borrow_mut() = - ::std::mem::size_of::<::libc::sockaddr_in6>() as u32; + __ai.ai_family = ::libc::AF_INET6; + __ai.ai_addrlen = ::std::mem::size_of::<::libc::sockaddr_in6>() as u32; __storage .reinterpret_cast::() .write(SockaddrIn6::from_ipv6(__v6, __port)); } } - *__ai.ai_addr.borrow_mut() = __storage.reinterpret_cast::(); - *__ai.ai_next.borrow_mut() = __next.clone(); + __ai.ai_addr = __storage.reinterpret_cast::(); + __ai.ai_next = __next.clone(); __next = Ptr::alloc(__ai); } __out.write(__next); @@ -282,33 +261,23 @@ pub fn test_ipv6_literal_1() { != 0) ); assert!((((!((*res.borrow()).is_null())) as i32) != 0)); - assert!( - ((((*(*(*res.borrow()).upgrade().deref()).ai_family.borrow()) == libc::AF_INET6) as i32) - != 0) - ); - assert!( - (((((*(*(*res.borrow()).upgrade().deref()).ai_addrlen.borrow()) as usize) == 28usize) - as i32) - != 0) - ); - assert!( - (((!((*(*(*res.borrow()).upgrade().deref()).ai_addr.borrow()).is_null())) as i32) != 0) - ); + assert!(((((*res.borrow()).with(|__s| __s.ai_family) == libc::AF_INET6) as i32) != 0)); + assert!((((((*res.borrow()).with(|__s| __s.ai_addrlen) as usize) == 28usize) as i32) != 0)); + assert!((((!(((*res.borrow()).with(|__s| __s.ai_addr.clone())).is_null())) as i32) != 0)); let sin6: Value> = Rc::new(RefCell::new( - (*(*(*res.borrow()).upgrade().deref()).ai_addr.borrow()) + (*res.borrow()) + .with(|__s| __s.ai_addr.clone()) .reinterpret_cast::(), )); assert!( - (((((*(*(*sin6.borrow()).upgrade().deref()).sin6_family.borrow()) as i32) == libc::AF_INET6) - as i32) - != 0) + (((((*sin6.borrow()).with(|__s| __s.sin6_family) as i32) == libc::AF_INET6) as i32) != 0) ); let port_be: Value> = Rc::new(RefCell::new(Box::new([ ((443 / 256) as u8), ((443 % 256) as u8), ]))); assert!( - ((((((*(*sin6.borrow()).upgrade().deref()).sin6_port.as_pointer()) as Ptr::) + (((((field_ptr!((*sin6.borrow()), sin6_port)) as Ptr::) .to_any() .memcmp( &((port_be.as_pointer() as Ptr::) as Ptr::).to_any(), @@ -322,8 +291,7 @@ pub fn test_ipv6_literal_1() { 1_u8, ]))); assert!( - ((((((*(*sin6.borrow()).upgrade().deref()).sin6_addr.as_pointer()) - as Ptr) + (((((field_ptr!((*sin6.borrow()), sin6_addr)) as Ptr) .to_any() .memcmp( &((addr_be.as_pointer() as Ptr::) as Ptr::).to_any(), @@ -336,11 +304,10 @@ pub fn test_ipv6_literal_1() { let mut __cur = (*res.borrow()).clone(); while !__cur.is_null() { let __next = __cur.with(|__ai| { - let __addr = __ai.ai_addr.borrow(); - if !__addr.is_null() { - __addr.delete(); + if !__ai.ai_addr.is_null() { + __ai.ai_addr.delete(); } - (*__ai.ai_next.borrow()).clone() + __ai.ai_next.clone() }); __cur.delete(); __cur = __next; @@ -359,17 +326,17 @@ pub fn test_null_hints_2() { let __family = if __hints.is_null() { ::libc::AF_UNSPEC } else { - __hints.with(|__h| *__h.ai_family.borrow()) + __hints.with(|__h| __h.ai_family) }; let __socktype = if __hints.is_null() { 0 } else { - __hints.with(|__h| *__h.ai_socktype.borrow()) + __hints.with(|__h| __h.ai_socktype) }; let __protocol = if __hints.is_null() { 0 } else { - __hints.with(|__h| *__h.ai_protocol.borrow()) + __hints.with(|__h| __h.ai_protocol) }; let __port: u16 = if __service.is_null() { 0 @@ -410,30 +377,28 @@ pub fn test_null_hints_2() { } else { let mut __next = Ptr::::null(); for __ip in __addrs.iter().rev() { - let __ai = Addrinfo::default(); - *__ai.ai_socktype.borrow_mut() = __socktype; - *__ai.ai_protocol.borrow_mut() = __protocol; + let mut __ai = Addrinfo::default(); + __ai.ai_socktype = __socktype; + __ai.ai_protocol = __protocol; let __storage = Ptr::alloc(SockaddrStorage::default()); match __ip { ::std::net::IpAddr::V4(__v4) => { - *__ai.ai_family.borrow_mut() = ::libc::AF_INET; - *__ai.ai_addrlen.borrow_mut() = - ::std::mem::size_of::<::libc::sockaddr_in>() as u32; + __ai.ai_family = ::libc::AF_INET; + __ai.ai_addrlen = ::std::mem::size_of::<::libc::sockaddr_in>() as u32; __storage .reinterpret_cast::() .write(SockaddrIn::from_ipv4(__v4, __port)); } ::std::net::IpAddr::V6(__v6) => { - *__ai.ai_family.borrow_mut() = ::libc::AF_INET6; - *__ai.ai_addrlen.borrow_mut() = - ::std::mem::size_of::<::libc::sockaddr_in6>() as u32; + __ai.ai_family = ::libc::AF_INET6; + __ai.ai_addrlen = ::std::mem::size_of::<::libc::sockaddr_in6>() as u32; __storage .reinterpret_cast::() .write(SockaddrIn6::from_ipv6(__v6, __port)); } } - *__ai.ai_addr.borrow_mut() = __storage.reinterpret_cast::(); - *__ai.ai_next.borrow_mut() = __next.clone(); + __ai.ai_addr = __storage.reinterpret_cast::(); + __ai.ai_next = __next.clone(); __next = Ptr::alloc(__ai); } __out.write(__next); @@ -443,17 +408,15 @@ pub fn test_null_hints_2() { != 0) ); assert!((((!((*res.borrow()).is_null())) as i32) != 0)); - assert!( - ((((*(*(*res.borrow()).upgrade().deref()).ai_family.borrow()) == libc::AF_INET) as i32) - != 0) - ); + assert!(((((*res.borrow()).with(|__s| __s.ai_family) == libc::AF_INET) as i32) != 0)); let sin: Value> = Rc::new(RefCell::new( - (*(*(*res.borrow()).upgrade().deref()).ai_addr.borrow()) + (*res.borrow()) + .with(|__s| __s.ai_addr.clone()) .reinterpret_cast::(), )); let addr_be: Value> = Rc::new(RefCell::new(Box::new([127_u8, 0_u8, 0_u8, 1_u8]))); assert!( - ((((((*(*sin.borrow()).upgrade().deref()).sin_addr.as_pointer()) as Ptr) + (((((field_ptr!((*sin.borrow()), sin_addr)) as Ptr) .to_any() .memcmp( &((addr_be.as_pointer() as Ptr::) as Ptr::).to_any(), @@ -466,11 +429,10 @@ pub fn test_null_hints_2() { let mut __cur = (*res.borrow()).clone(); while !__cur.is_null() { let __next = __cur.with(|__ai| { - let __addr = __ai.ai_addr.borrow(); - if !__addr.is_null() { - __addr.delete(); + if !__ai.ai_addr.is_null() { + __ai.ai_addr.delete(); } - (*__ai.ai_next.borrow()).clone() + __ai.ai_next.clone() }); __cur.delete(); __cur = __next; diff --git a/tests/unit/out/refcount/new.rs b/tests/unit/out/refcount/new.rs index 851c326f9d..ed8e3c1fbe 100644 --- a/tests/unit/out/refcount/new.rs +++ b/tests/unit/out/refcount/new.rs @@ -42,12 +42,12 @@ fn main_0() -> i32 { assert!((((*y.borrow()).read()) == 9)); (*y.borrow()).delete(); let p: Value> = Rc::new(RefCell::new(Ptr::alloc(::default()))); - (*p.borrow()).with_mut(|__s: &mut Pair| __s.x = 1); - (*p.borrow()).with_mut(|__s: &mut Pair| __s.y = 2); + field!((*p.borrow()), x).write(1); + field!((*p.borrow()), y).write(2); assert!( ({ - let _lhs = (*p.borrow()).with(|__s: &Pair| __s.x); - _lhs + (*p.borrow()).with(|__s: &Pair| __s.y) + let _lhs = (*p.borrow()).with(|__s| __s.x); + _lhs + (*p.borrow()).with(|__s| __s.y) } == 3) ); (*p.borrow()).delete(); diff --git a/tests/unit/out/refcount/new_bst.rs b/tests/unit/out/refcount/new_bst.rs index 684769681e..7b60b44fa1 100644 --- a/tests/unit/out/refcount/new_bst.rs +++ b/tests/unit/out/refcount/new_bst.rs @@ -37,29 +37,29 @@ pub fn find_0(node: Ptr, value: i32) -> Ptr { let value: Value = Rc::new(RefCell::new(value)); if ({ let _lhs = (*value.borrow()); - _lhs < (*node.borrow()).with(|__s: &node_t| __s.value) - }) && (!(((*node.borrow()).with(|__s: &node_t| (__s.left).clone())).is_null())) + _lhs < (*node.borrow()).with(|__s| __s.value) + }) && (!(((*node.borrow()).with(|__s| __s.left.clone())).is_null())) { return ({ find_0( - (*node.borrow()).with(|__s: &node_t| (__s.left).clone()), + (*node.borrow()).with(|__s| __s.left.clone()), (*value.borrow()), ) }); } else if ({ let _lhs = (*value.borrow()); - _lhs > (*node.borrow()).with(|__s: &node_t| __s.value) - }) && (!(((*node.borrow()).with(|__s: &node_t| (__s.right).clone())).is_null())) + _lhs > (*node.borrow()).with(|__s| __s.value) + }) && (!(((*node.borrow()).with(|__s| __s.right.clone())).is_null())) { return ({ find_0( - (*node.borrow()).with(|__s: &node_t| (__s.right).clone()), + (*node.borrow()).with(|__s| __s.right.clone()), (*value.borrow()), ) }); } else if { let _lhs = (*value.borrow()); - _lhs == (*node.borrow()).with(|__s: &node_t| __s.value) + _lhs == (*node.borrow()).with(|__s| __s.value) } { return (*node.borrow()).clone(); } @@ -77,36 +77,36 @@ pub fn insert_1(node: Ptr, value: i32) -> Ptr { } if { let _lhs = (*value.borrow()); - _lhs < (*node.borrow()).with(|__s: &node_t| __s.value) + _lhs < (*node.borrow()).with(|__s| __s.value) } { let __rhs = ({ insert_1( - (*node.borrow()).with(|__s: &node_t| (__s.left).clone()), + (*node.borrow()).with(|__s| __s.left.clone()), (*value.borrow()), ) }); - (*node.borrow()).with_mut(|__s: &mut node_t| __s.left = __rhs); + field!((*node.borrow()), left).write(__rhs); } else if { let _lhs = (*value.borrow()); - _lhs > (*node.borrow()).with(|__s: &node_t| __s.value) + _lhs > (*node.borrow()).with(|__s| __s.value) } { let __rhs = ({ insert_1( - (*node.borrow()).with(|__s: &node_t| (__s.right).clone()), + (*node.borrow()).with(|__s| __s.right.clone()), (*value.borrow()), ) }); - (*node.borrow()).with_mut(|__s: &mut node_t| __s.right = __rhs); + field!((*node.borrow()), right).write(__rhs); } return (*node.borrow()).clone(); } pub fn del_2(node: Ptr) { let node: Value> = Rc::new(RefCell::new(node)); - if !(((*node.borrow()).with(|__s: &node_t| (__s.left).clone())).is_null()) { - ({ del_2((*node.borrow()).with(|__s: &node_t| (__s.left).clone())) }); + if !(((*node.borrow()).with(|__s| __s.left.clone())).is_null()) { + ({ del_2((*node.borrow()).with(|__s| __s.left.clone())) }); } - if !(((*node.borrow()).with(|__s: &node_t| (__s.right).clone())).is_null()) { - ({ del_2((*node.borrow()).with(|__s: &node_t| (__s.right).clone())) }); + if !(((*node.borrow()).with(|__s| __s.right.clone())).is_null()) { + ({ del_2((*node.borrow()).with(|__s| __s.right.clone())) }); } (*node.borrow()).delete(); } @@ -129,11 +129,11 @@ fn main_0() -> i32 { let __rhs = ({ insert_1((*root.borrow()).clone(), 4) }); (*root.borrow_mut()) = __rhs; let out: Value = Rc::new(RefCell::new( - (((((({ find_0((*root.borrow()).clone(), 0) }).with(|__s: &node_t| __s.value) == 0) - && (({ find_0((*root.borrow()).clone(), 1) }).with(|__s: &node_t| __s.value) == 1)) - && (({ find_0((*root.borrow()).clone(), 2) }).with(|__s: &node_t| __s.value) == 2)) - && (({ find_0((*root.borrow()).clone(), 3) }).with(|__s: &node_t| __s.value) == 3)) - && (({ find_0((*root.borrow()).clone(), 4) }).with(|__s: &node_t| __s.value) == 4)) + (((((({ find_0((*root.borrow()).clone(), 0) }).with(|__s| __s.value) == 0) + && (({ find_0((*root.borrow()).clone(), 1) }).with(|__s| __s.value) == 1)) + && (({ find_0((*root.borrow()).clone(), 2) }).with(|__s| __s.value) == 2)) + && (({ find_0((*root.borrow()).clone(), 3) }).with(|__s| __s.value) == 3)) + && (({ find_0((*root.borrow()).clone(), 4) }).with(|__s| __s.value) == 4)) && (({ find_0((*root.borrow()).clone(), 5) }).is_null()), )); ({ del_2((*root.borrow()).clone()) }); diff --git a/tests/unit/out/refcount/new_struct.rs b/tests/unit/out/refcount/new_struct.rs index c54cb6065c..70e5501bbc 100644 --- a/tests/unit/out/refcount/new_struct.rs +++ b/tests/unit/out/refcount/new_struct.rs @@ -61,8 +61,8 @@ pub fn main() { fn main_0() -> i32 { let p: Value> = Rc::new(RefCell::new(Ptr::alloc(Pair { x: 1, y: 2 }))); let out: Value = Rc::new(RefCell::new({ - let _lhs = (*p.borrow()).with(|__s: &Pair| __s.x); - _lhs + (*p.borrow()).with(|__s: &Pair| __s.y) + let _lhs = (*p.borrow()).with(|__s| __s.x); + _lhs + (*p.borrow()).with(|__s| __s.y) })); (*p.borrow()).delete(); assert!(((*out.borrow()) == 3)); @@ -79,12 +79,9 @@ fn main_0() -> i32 { b: 3, p: ::default(), }))); - assert!(((*q.borrow()).with(|__s: &Triple| __s.a) == 2)); - assert!(((*q.borrow()).with(|__s: &Triple| __s.b) == 3)); - assert!( - ((*q.borrow()).with(|__s: &Triple| __s.p.x) == 0) - && ((*q.borrow()).with(|__s: &Triple| __s.p.y) == 0) - ); + assert!(((*q.borrow()).with(|__s| __s.a) == 2)); + assert!(((*q.borrow()).with(|__s| __s.b) == 3)); + assert!(((*q.borrow()).with(|__s| __s.p.x) == 0) && ((*q.borrow()).with(|__s| __s.p.y) == 0)); (*q.borrow()).delete(); return 0; } diff --git a/tests/unit/out/refcount/offsetof.rs b/tests/unit/out/refcount/offsetof.rs index 4b267ef75e..428f7f0715 100644 --- a/tests/unit/out/refcount/offsetof.rs +++ b/tests/unit/out/refcount/offsetof.rs @@ -41,14 +41,10 @@ pub struct Frame { } impl Clone for Frame { fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - tag: { self.tag }, - body: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 64, _>( - |__i: usize| (*self.body.borrow())[(__i) as usize], - )))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() + Self { + tag: self.tag.clone(), + body: Rc::new(RefCell::new((*self.body.borrow()).clone())), + } } } impl Default for Frame { diff --git a/tests/unit/out/refcount/opaque_then_defined.rs b/tests/unit/out/refcount/opaque_then_defined.rs index dfa73ea3a8..23430100ac 100644 --- a/tests/unit/out/refcount/opaque_then_defined.rs +++ b/tests/unit/out/refcount/opaque_then_defined.rs @@ -63,7 +63,7 @@ fn main_0() -> i32 { head: (n.as_pointer()), size: 1, })); - assert!(((({ (*l.borrow()).head.clone() }.with(|__s: &node| __s.value) == 42) as i32) != 0)); + assert!(((({ (*l.borrow()).head.clone() }.with(|__s| __s.value) == 42) as i32) != 0)); assert!(((({ (*l.borrow()).size } == 1) as i32) != 0)); return 0; } diff --git a/tests/unit/out/refcount/operator_arithmetic_free.rs b/tests/unit/out/refcount/operator_arithmetic_free.rs index 81dc207c4a..9af178850f 100644 --- a/tests/unit/out/refcount/operator_arithmetic_free.rs +++ b/tests/unit/out/refcount/operator_arithmetic_free.rs @@ -27,78 +27,78 @@ impl ByteRepr for S { pub fn operator_add_0(a: Ptr, b: Ptr) -> S { return S { v: { - let _lhs = a.with(|__s: &S| __s.v); - _lhs + b.with(|__s: &S| __s.v) + let _lhs = a.with(|__s| __s.v); + _lhs + b.with(|__s| __s.v) }, }; } pub fn operator_sub_1(a: Ptr, b: Ptr) -> S { return S { v: { - let _lhs = a.with(|__s: &S| __s.v); - _lhs - b.with(|__s: &S| __s.v) + let _lhs = a.with(|__s| __s.v); + _lhs - b.with(|__s| __s.v) }, }; } pub fn operator_mul_2(a: Ptr, b: Ptr) -> S { return S { v: { - let _lhs = a.with(|__s: &S| __s.v); - _lhs * b.with(|__s: &S| __s.v) + let _lhs = a.with(|__s| __s.v); + _lhs * b.with(|__s| __s.v) }, }; } pub fn operator_div_3(a: Ptr, b: Ptr) -> S { return S { v: { - let _lhs = a.with(|__s: &S| __s.v); - _lhs / b.with(|__s: &S| __s.v) + let _lhs = a.with(|__s| __s.v); + _lhs / b.with(|__s| __s.v) }, }; } pub fn operator_rem_4(a: Ptr, b: Ptr) -> S { return S { v: { - let _lhs = a.with(|__s: &S| __s.v); - _lhs % b.with(|__s: &S| __s.v) + let _lhs = a.with(|__s| __s.v); + _lhs % b.with(|__s| __s.v) }, }; } pub fn operator_pos_5(a: Ptr) -> S { return S { - v: { a.with(|__s: &S| __s.v) }, + v: a.with(|__s| __s.v), }; } pub fn operator_neg_6(a: Ptr) -> S { return S { - v: { -a.with(|__s: &S| __s.v) }, + v: -a.with(|__s| __s.v), }; } pub fn operator_inc_7(a: Ptr) -> Ptr { - a.with_mut(|__s: &mut S| __s.v.prefix_inc()); + field!(a, v).with_mut(|__v| __v.prefix_inc()); return (a).clone(); } pub fn operator_post_inc_8(a: Ptr, _a1: i32) -> S { let _a1: Value = Rc::new(RefCell::new(_a1)); - let old: Value = Rc::new(RefCell::new(a.read())); - a.with_mut(|__s: &mut S| __s.v.prefix_inc()); + let old: Value = Rc::new(RefCell::new((*a.upgrade().deref()).clone())); + field!(a, v).with_mut(|__v| __v.prefix_inc()); return (*old.borrow()).clone(); } pub fn operator_dec_9(a: Ptr) -> Ptr { - a.with_mut(|__s: &mut S| __s.v.prefix_dec()); + field!(a, v).with_mut(|__v| __v.prefix_dec()); return (a).clone(); } pub fn operator_post_dec_10(a: Ptr, _a1: i32) -> S { let _a1: Value = Rc::new(RefCell::new(_a1)); - let old: Value = Rc::new(RefCell::new(a.read())); - a.with_mut(|__s: &mut S| __s.v.prefix_dec()); + let old: Value = Rc::new(RefCell::new((*a.upgrade().deref()).clone())); + field!(a, v).with_mut(|__v| __v.prefix_dec()); return (*old.borrow()).clone(); } pub fn operator_add_11(a: Ptr, b: i32) -> S { let b: Value = Rc::new(RefCell::new(b)); return S { v: { - let _lhs = a.with(|__s: &S| __s.v); + let _lhs = a.with(|__s| __s.v); _lhs + (*b.borrow()) }, }; @@ -108,7 +108,7 @@ pub fn operator_add_12(a: i32, b: Ptr) -> S { return S { v: { let _lhs = (*a.borrow()); - _lhs + b.with(|__s: &S| __s.v) + _lhs + b.with(|__s| __s.v) }, }; } @@ -187,7 +187,7 @@ fn main_0() -> i32 { let _a: Ptr = a.as_pointer(); operator_inc_7(_a) }) - .with(|__s: &S| __s.v) + .with(|__s| (__s).v) == 8) ); assert!( @@ -205,7 +205,7 @@ fn main_0() -> i32 { let _a: Ptr = a.as_pointer(); operator_dec_9(_a) }) - .with(|__s: &S| __s.v) + .with(|__s| (__s).v) == 8) ); assert!( diff --git a/tests/unit/out/refcount/operator_arithmetic_member.rs b/tests/unit/out/refcount/operator_arithmetic_member.rs index 2e3a6e6005..e1dd502506 100644 --- a/tests/unit/out/refcount/operator_arithmetic_member.rs +++ b/tests/unit/out/refcount/operator_arithmetic_member.rs @@ -38,15 +38,15 @@ fn main_0() -> i32 { assert!(({ ({ SImpl::operator_rem(&a.as_pointer(), b.as_pointer(),) }).v } == 1)); assert!(({ ({ SImpl::operator_pos_6(&a.as_pointer(),) }).v } == 7)); assert!(({ ({ SImpl::operator_neg_7(&a.as_pointer(),) }).v } == -7_i32)); - assert!((({ SImpl::operator_inc_8(&a.as_pointer(),) }).with(|__s: &S| __s.v) == 8)); + assert!((({ SImpl::operator_inc_8(&a.as_pointer(),) }).with(|__s| (__s).v) == 8)); assert!(({ ({ SImpl::operator_post_inc_9(&a.as_pointer(), 0,) }).v } == 8)); assert!(({ (*a.borrow()).v } == 9)); - assert!((({ SImpl::operator_dec_10(&a.as_pointer(),) }).with(|__s: &S| __s.v) == 8)); + assert!((({ SImpl::operator_dec_10(&a.as_pointer(),) }).with(|__s| (__s).v) == 8)); assert!(({ ({ SImpl::operator_post_dec_11(&a.as_pointer(), 0,) }).v } == 8)); assert!(({ (*a.borrow()).v } == 7)); assert!( (({ SImpl::operator_inc_8(&({ SImpl::operator_inc_8(&a.as_pointer(),) }),) }) - .with(|__s: &S| __s.v) + .with(|__s| (__s).v) == 9) ); assert!( @@ -80,71 +80,71 @@ impl SImpl for Ptr { fn operator_add_1(&self, o: Ptr) -> S { return S { v: { - let _lhs = (*self).with(|__s: &S| __s.v); - _lhs + o.with(|__s: &S| __s.v) + let _lhs = (*self).with(|__s| __s.v); + _lhs + o.with(|__s| __s.v) }, }; } fn operator_sub_2(&self, o: Ptr) -> S { return S { v: { - let _lhs = (*self).with(|__s: &S| __s.v); - _lhs - o.with(|__s: &S| __s.v) + let _lhs = (*self).with(|__s| __s.v); + _lhs - o.with(|__s| __s.v) }, }; } fn operator_mul(&self, o: Ptr) -> S { return S { v: { - let _lhs = (*self).with(|__s: &S| __s.v); - _lhs * o.with(|__s: &S| __s.v) + let _lhs = (*self).with(|__s| __s.v); + _lhs * o.with(|__s| __s.v) }, }; } fn operator_div(&self, o: Ptr) -> S { return S { v: { - let _lhs = (*self).with(|__s: &S| __s.v); - _lhs / o.with(|__s: &S| __s.v) + let _lhs = (*self).with(|__s| __s.v); + _lhs / o.with(|__s| __s.v) }, }; } fn operator_rem(&self, o: Ptr) -> S { return S { v: { - let _lhs = (*self).with(|__s: &S| __s.v); - _lhs % o.with(|__s: &S| __s.v) + let _lhs = (*self).with(|__s| __s.v); + _lhs % o.with(|__s| __s.v) }, }; } fn operator_pos_6(&self) -> S { return S { - v: { (*self).with(|__s: &S| __s.v) }, + v: (*self).with(|__s| __s.v), }; } fn operator_neg_7(&self) -> S { return S { - v: { -(*self).with(|__s: &S| __s.v) }, + v: -(*self).with(|__s| __s.v), }; } fn operator_inc_8(&self) -> Ptr { - (*self).with_mut(|__s: &mut S| __s.v.prefix_inc()); + field!((*self), v).with_mut(|__v| __v.prefix_inc()); return (*self).clone(); } fn operator_post_inc_9(&self, _a0: i32) -> S { let _a0: Value = Rc::new(RefCell::new(_a0)); - let old: Value = Rc::new(RefCell::new((*self).read())); - (*self).with_mut(|__s: &mut S| __s.v.prefix_inc()); + let old: Value = Rc::new(RefCell::new((*(*self).upgrade().deref()).clone())); + field!((*self), v).with_mut(|__v| __v.prefix_inc()); return (*old.borrow()).clone(); } fn operator_dec_10(&self) -> Ptr { - (*self).with_mut(|__s: &mut S| __s.v.prefix_dec()); + field!((*self), v).with_mut(|__v| __v.prefix_dec()); return (*self).clone(); } fn operator_post_dec_11(&self, _a0: i32) -> S { let _a0: Value = Rc::new(RefCell::new(_a0)); - let old: Value = Rc::new(RefCell::new((*self).read())); - (*self).with_mut(|__s: &mut S| __s.v.prefix_dec()); + let old: Value = Rc::new(RefCell::new((*(*self).upgrade().deref()).clone())); + field!((*self), v).with_mut(|__v| __v.prefix_dec()); return (*old.borrow()).clone(); } } diff --git a/tests/unit/out/refcount/operator_bitwise_free.rs b/tests/unit/out/refcount/operator_bitwise_free.rs index 2cb4b31ea4..90d72d14ce 100644 --- a/tests/unit/out/refcount/operator_bitwise_free.rs +++ b/tests/unit/out/refcount/operator_bitwise_free.rs @@ -26,30 +26,30 @@ impl ByteRepr for S { } pub fn operator_bitnot_0(a: Ptr) -> S { return S { - v: { !a.with(|__s: &S| __s.v) }, + v: !a.with(|__s| __s.v), }; } pub fn operator_bitand_1(a: Ptr, b: Ptr) -> S { return S { v: { - let _lhs = a.with(|__s: &S| __s.v); - _lhs & b.with(|__s: &S| __s.v) + let _lhs = a.with(|__s| __s.v); + _lhs & b.with(|__s| __s.v) }, }; } pub fn operator_bitor_2(a: Ptr, b: Ptr) -> S { return S { v: { - let _lhs = a.with(|__s: &S| __s.v); - _lhs | b.with(|__s: &S| __s.v) + let _lhs = a.with(|__s| __s.v); + _lhs | b.with(|__s| __s.v) }, }; } pub fn operator_bitxor_3(a: Ptr, b: Ptr) -> S { return S { v: { - let _lhs = a.with(|__s: &S| __s.v); - _lhs ^ b.with(|__s: &S| __s.v) + let _lhs = a.with(|__s| __s.v); + _lhs ^ b.with(|__s| __s.v) }, }; } @@ -57,7 +57,7 @@ pub fn operator_shl_4(a: Ptr, n: i32) -> S { let n: Value = Rc::new(RefCell::new(n)); return S { v: { - let _lhs = a.with(|__s: &S| __s.v); + let _lhs = a.with(|__s| __s.v); _lhs << (*n.borrow()) }, }; @@ -66,7 +66,7 @@ pub fn operator_shr_5(a: Ptr, n: i32) -> S { let n: Value = Rc::new(RefCell::new(n)); return S { v: { - let _lhs = a.with(|__s: &S| __s.v); + let _lhs = a.with(|__s| __s.v); _lhs >> (*n.borrow()) }, }; diff --git a/tests/unit/out/refcount/operator_bitwise_member.rs b/tests/unit/out/refcount/operator_bitwise_member.rs index 3568e230db..ae9ec72be7 100644 --- a/tests/unit/out/refcount/operator_bitwise_member.rs +++ b/tests/unit/out/refcount/operator_bitwise_member.rs @@ -50,43 +50,43 @@ pub trait SImpl { impl SImpl for Ptr { fn operator_bitnot(&self) -> S { return S { - v: { !(*self).with(|__s: &S| __s.v) }, + v: !(*self).with(|__s| __s.v), }; } fn operator_bitand(&self, o: Ptr) -> S { return S { v: { - let _lhs = (*self).with(|__s: &S| __s.v); - _lhs & o.with(|__s: &S| __s.v) + let _lhs = (*self).with(|__s| __s.v); + _lhs & o.with(|__s| __s.v) }, }; } fn operator_bitor(&self, o: Ptr) -> S { return S { v: { - let _lhs = (*self).with(|__s: &S| __s.v); - _lhs | o.with(|__s: &S| __s.v) + let _lhs = (*self).with(|__s| __s.v); + _lhs | o.with(|__s| __s.v) }, }; } fn operator_bitxor(&self, o: Ptr) -> S { return S { v: { - let _lhs = (*self).with(|__s: &S| __s.v); - _lhs ^ o.with(|__s: &S| __s.v) + let _lhs = (*self).with(|__s| __s.v); + _lhs ^ o.with(|__s| __s.v) }, }; } fn operator_shl(&self, n: i32) -> S { let n: Value = Rc::new(RefCell::new(n)); return S { - v: { ((*self).with(|__s: &S| __s.v) << (*n.borrow())) }, + v: ((*self).with(|__s| __s.v) << (*n.borrow())), }; } fn operator_shr(&self, n: i32) -> S { let n: Value = Rc::new(RefCell::new(n)); return S { - v: { ((*self).with(|__s: &S| __s.v) >> (*n.borrow())) }, + v: ((*self).with(|__s| __s.v) >> (*n.borrow())), }; } } diff --git a/tests/unit/out/refcount/operator_comparison_defaulted.rs b/tests/unit/out/refcount/operator_comparison_defaulted.rs index 2f529d3002..d39dc216d5 100644 --- a/tests/unit/out/refcount/operator_comparison_defaulted.rs +++ b/tests/unit/out/refcount/operator_comparison_defaulted.rs @@ -510,15 +510,15 @@ pub trait BothImpl { impl BothImpl for Ptr { fn operator_eq(&self, _a0: Ptr) -> bool { return { - let _lhs = (*self).with(|__s: &Both| __s.a); - _lhs == _a0.with(|__s: &Both| __s.a) + let _lhs = (*self).with(|__s| __s.a); + _lhs == _a0.with(|__s| __s.a) }; } fn operator_cmp(&self, _a0: Ptr) -> std::cmp::Ordering { { let cmp: Value = Rc::new(RefCell::new(std::cmp::Ord::cmp( - &((*self).with(|__s: &Both| __s.a)), - &(_a0.with(|__s: &Both| __s.a)), + &((*self).with(|__s| __s.a)), + &(_a0.with(|__s| __s.a)), ))); if !((*cmp.borrow()) == std::cmp::Ordering::Equal) { return (*cmp.borrow_mut()).clone(); @@ -535,8 +535,8 @@ impl CmpImpl for Ptr { fn operator_cmp(&self, _a0: Ptr) -> std::cmp::Ordering { { let cmp: Value = Rc::new(RefCell::new(std::cmp::Ord::cmp( - &((*self).with(|__s: &Cmp| __s.a)), - &(_a0.with(|__s: &Cmp| __s.a)), + &((*self).with(|__s| __s.a)), + &(_a0.with(|__s| __s.a)), ))); if !((*cmp.borrow()) == std::cmp::Ordering::Equal) { return (*cmp.borrow_mut()).clone(); @@ -544,8 +544,8 @@ impl CmpImpl for Ptr { } { let cmp: Value = Rc::new(RefCell::new(std::cmp::Ord::cmp( - &((*self).with(|__s: &Cmp| __s.b)), - &(_a0.with(|__s: &Cmp| __s.b)), + &((*self).with(|__s| __s.b)), + &(_a0.with(|__s| __s.b)), ))); if !((*cmp.borrow()) == std::cmp::Ordering::Equal) { return (*cmp.borrow_mut()).clone(); @@ -555,11 +555,11 @@ impl CmpImpl for Ptr { } fn operator_eq(&self, _a0: Ptr) -> bool { return ({ - let _lhs = (*self).with(|__s: &Cmp| __s.a); - _lhs == _a0.with(|__s: &Cmp| __s.a) + let _lhs = (*self).with(|__s| __s.a); + _lhs == _a0.with(|__s| __s.a) }) && ({ - let _lhs = (*self).with(|__s: &Cmp| __s.b); - _lhs == _a0.with(|__s: &Cmp| __s.b) + let _lhs = (*self).with(|__s| __s.b); + _lhs == _a0.with(|__s| __s.b) }); } } @@ -569,11 +569,11 @@ pub trait EqImpl { impl EqImpl for Ptr { fn operator_eq(&self, _a0: Ptr) -> bool { return ({ - let _lhs = (*self).with(|__s: &Eq| __s.a); - _lhs == _a0.with(|__s: &Eq| __s.a) + let _lhs = (*self).with(|__s| __s.a); + _lhs == _a0.with(|__s| __s.a) }) && ({ - let _lhs = (*self).with(|__s: &Eq| __s.b); - _lhs == _a0.with(|__s: &Eq| __s.b) + let _lhs = (*self).with(|__s| __s.b); + _lhs == _a0.with(|__s| __s.b) }); } } @@ -585,8 +585,8 @@ impl InnerImpl for Ptr { fn operator_cmp(&self, _a0: Ptr) -> std::cmp::Ordering { { let cmp: Value = Rc::new(RefCell::new(std::cmp::Ord::cmp( - &((*self).with(|__s: &Inner| __s.x)), - &(_a0.with(|__s: &Inner| __s.x)), + &((*self).with(|__s| __s.x)), + &(_a0.with(|__s| __s.x)), ))); if !((*cmp.borrow()) == std::cmp::Ordering::Equal) { return (*cmp.borrow_mut()).clone(); @@ -596,8 +596,8 @@ impl InnerImpl for Ptr { } fn operator_eq(&self, _a0: Ptr) -> bool { return { - let _lhs = (*self).with(|__s: &Inner| __s.x); - _lhs == _a0.with(|__s: &Inner| __s.x) + let _lhs = (*self).with(|__s| __s.x); + _lhs == _a0.with(|__s| __s.x) }; } } @@ -608,8 +608,8 @@ impl OrdOnlyImpl for Ptr { fn operator_cmp(&self, _a0: Ptr) -> std::cmp::Ordering { { let cmp: Value = Rc::new(RefCell::new(std::cmp::Ord::cmp( - &((*self).with(|__s: &OrdOnly| __s.a)), - &(_a0.with(|__s: &OrdOnly| __s.a)), + &((*self).with(|__s| __s.a)), + &(_a0.with(|__s| __s.a)), ))); if !((*cmp.borrow()) == std::cmp::Ordering::Equal) { return (*cmp.borrow_mut()).clone(); @@ -628,7 +628,7 @@ impl OuterImpl for Ptr { let cmp: Value = Rc::new(RefCell::new( ({ let _arg0: Ptr = field_ptr!(_a0, i); - InnerImpl::operator_cmp(&{ field_ptr!((*self), i) }, _arg0) + InnerImpl::operator_cmp(&field_ptr!((*self), i), _arg0) }), )); if !((*cmp.borrow()) == std::cmp::Ordering::Equal) { @@ -637,8 +637,8 @@ impl OuterImpl for Ptr { } { let cmp: Value = Rc::new(RefCell::new(std::cmp::Ord::cmp( - &((*self).with(|__s: &Outer| __s.y)), - &(_a0.with(|__s: &Outer| __s.y)), + &((*self).with(|__s| __s.y)), + &(_a0.with(|__s| __s.y)), ))); if !((*cmp.borrow()) == std::cmp::Ordering::Equal) { return (*cmp.borrow_mut()).clone(); @@ -649,10 +649,10 @@ impl OuterImpl for Ptr { fn operator_eq(&self, _a0: Ptr) -> bool { return ({ let _arg0: Ptr = field_ptr!(_a0, i); - InnerImpl::operator_eq(&{ field_ptr!((*self), i) }, _arg0) + InnerImpl::operator_eq(&field_ptr!((*self), i), _arg0) }) && ({ - let _lhs = (*self).with(|__s: &Outer| __s.y); - _lhs == _a0.with(|__s: &Outer| __s.y) + let _lhs = (*self).with(|__s| __s.y); + _lhs == _a0.with(|__s| __s.y) }); } } @@ -664,8 +664,8 @@ impl PtrMemberImpl for Ptr { fn operator_cmp(&self, _a0: Ptr) -> std::cmp::Ordering { { let cmp: Value = Rc::new(RefCell::new(std::cmp::Ord::cmp( - &((*self).with(|__s: &PtrMember| (__s.p).clone())), - &(_a0.with(|__s: &PtrMember| (__s.p).clone())), + &((*self).with(|__s| __s.p.clone())), + &(_a0.with(|__s| __s.p.clone())), ))); if !((*cmp.borrow()) == std::cmp::Ordering::Equal) { return (*cmp.borrow_mut()).clone(); @@ -675,8 +675,8 @@ impl PtrMemberImpl for Ptr { } fn operator_eq(&self, _a0: Ptr) -> bool { return { - let _lhs = (*self).with(|__s: &PtrMember| (__s.p).clone()); - _lhs == _a0.with(|__s: &PtrMember| (__s.p).clone()) + let _lhs = (*self).with(|__s| __s.p.clone()); + _lhs == _a0.with(|__s| __s.p.clone()) }; } } @@ -690,8 +690,8 @@ pub trait SecondaryImpl { impl SecondaryImpl for Ptr { fn operator_eq(&self, _a0: Ptr) -> bool { return { - let _lhs = (*self).with(|__s: &Secondary| __s.a); - _lhs == _a0.with(|__s: &Secondary| __s.a) + let _lhs = (*self).with(|__s| __s.a); + _lhs == _a0.with(|__s| __s.a) }; } fn operator_ne(&self, _a0: Ptr) -> bool { @@ -703,8 +703,8 @@ impl SecondaryImpl for Ptr { fn operator_cmp(&self, _a0: Ptr) -> std::cmp::Ordering { { let cmp: Value = Rc::new(RefCell::new(std::cmp::Ord::cmp( - &((*self).with(|__s: &Secondary| __s.a)), - &(_a0.with(|__s: &Secondary| __s.a)), + &((*self).with(|__s| __s.a)), + &(_a0.with(|__s| __s.a)), ))); if !((*cmp.borrow()) == std::cmp::Ordering::Equal) { return (*cmp.borrow_mut()).clone(); diff --git a/tests/unit/out/refcount/operator_comparison_free.rs b/tests/unit/out/refcount/operator_comparison_free.rs index 2e1e1a2be3..0ec3ae6d9e 100644 --- a/tests/unit/out/refcount/operator_comparison_free.rs +++ b/tests/unit/out/refcount/operator_comparison_free.rs @@ -61,44 +61,44 @@ impl ByteRepr for S { } pub fn operator_eq_1(a: Ptr, b: Ptr) -> bool { return { - let _lhs = a.with(|__s: &S| __s.v); - _lhs == b.with(|__s: &S| __s.v) + let _lhs = a.with(|__s| __s.v); + _lhs == b.with(|__s| __s.v) }; } pub fn operator_ne_2(a: Ptr, b: Ptr) -> bool { return { - let _lhs = a.with(|__s: &S| __s.v); - _lhs != b.with(|__s: &S| __s.v) + let _lhs = a.with(|__s| __s.v); + _lhs != b.with(|__s| __s.v) }; } pub fn operator_lt_0(a: Ptr, b: Ptr) -> bool { return { - let _lhs = a.with(|__s: &S| __s.v); - _lhs < b.with(|__s: &S| __s.v) + let _lhs = a.with(|__s| __s.v); + _lhs < b.with(|__s| __s.v) }; } pub fn operator_gt_3(a: Ptr, b: Ptr) -> bool { return { - let _lhs = a.with(|__s: &S| __s.v); - _lhs > b.with(|__s: &S| __s.v) + let _lhs = a.with(|__s| __s.v); + _lhs > b.with(|__s| __s.v) }; } pub fn operator_le_4(a: Ptr, b: Ptr) -> bool { return { - let _lhs = a.with(|__s: &S| __s.v); - _lhs <= b.with(|__s: &S| __s.v) + let _lhs = a.with(|__s| __s.v); + _lhs <= b.with(|__s| __s.v) }; } pub fn operator_ge_5(a: Ptr, b: Ptr) -> bool { return { - let _lhs = a.with(|__s: &S| __s.v); - _lhs >= b.with(|__s: &S| __s.v) + let _lhs = a.with(|__s| __s.v); + _lhs >= b.with(|__s| __s.v) }; } pub fn operator_lt_6(a: Ptr, b: i32) -> bool { let b: Value = Rc::new(RefCell::new(b)); return { - let _lhs = a.with(|__s: &S| __s.v); + let _lhs = a.with(|__s| __s.v); _lhs < (*b.borrow()) }; } @@ -106,7 +106,7 @@ pub fn operator_lt_7(a: i32, b: Ptr) -> bool { let a: Value = Rc::new(RefCell::new(a)); return { let _lhs = (*a.borrow()); - _lhs < b.with(|__s: &S| __s.v) + _lhs < b.with(|__s| __s.v) }; } #[derive(Clone, Record, VaArg, FnPtrArg, Default)] diff --git a/tests/unit/out/refcount/operator_comparison_member.rs b/tests/unit/out/refcount/operator_comparison_member.rs index 0e2ba872c0..deebb8971a 100644 --- a/tests/unit/out/refcount/operator_comparison_member.rs +++ b/tests/unit/out/refcount/operator_comparison_member.rs @@ -89,43 +89,43 @@ pub trait SImpl { impl SImpl for Ptr { fn operator_eq(&self, o: Ptr) -> bool { return { - let _lhs = (*self).with(|__s: &S| __s.v); - _lhs == o.with(|__s: &S| __s.v) + let _lhs = (*self).with(|__s| __s.v); + _lhs == o.with(|__s| __s.v) }; } fn operator_ne(&self, o: Ptr) -> bool { return { - let _lhs = (*self).with(|__s: &S| __s.v); - _lhs != o.with(|__s: &S| __s.v) + let _lhs = (*self).with(|__s| __s.v); + _lhs != o.with(|__s| __s.v) }; } fn operator_lt_3(&self, o: Ptr) -> bool { return { - let _lhs = (*self).with(|__s: &S| __s.v); - _lhs < o.with(|__s: &S| __s.v) + let _lhs = (*self).with(|__s| __s.v); + _lhs < o.with(|__s| __s.v) }; } fn operator_gt(&self, o: Ptr) -> bool { return { - let _lhs = (*self).with(|__s: &S| __s.v); - _lhs > o.with(|__s: &S| __s.v) + let _lhs = (*self).with(|__s| __s.v); + _lhs > o.with(|__s| __s.v) }; } fn operator_le(&self, o: Ptr) -> bool { return { - let _lhs = (*self).with(|__s: &S| __s.v); - _lhs <= o.with(|__s: &S| __s.v) + let _lhs = (*self).with(|__s| __s.v); + _lhs <= o.with(|__s| __s.v) }; } fn operator_ge(&self, o: Ptr) -> bool { return { - let _lhs = (*self).with(|__s: &S| __s.v); - _lhs >= o.with(|__s: &S| __s.v) + let _lhs = (*self).with(|__s| __s.v); + _lhs >= o.with(|__s| __s.v) }; } fn operator_lt_7(&self, o: i32) -> bool { let o: Value = Rc::new(RefCell::new(o)); - return ((*self).with(|__s: &S| __s.v) < (*o.borrow())); + return ((*self).with(|__s| __s.v) < (*o.borrow())); } } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/operator_comparison_mixed.rs b/tests/unit/out/refcount/operator_comparison_mixed.rs index e9d0e8e87d..7955b3b54f 100644 --- a/tests/unit/out/refcount/operator_comparison_mixed.rs +++ b/tests/unit/out/refcount/operator_comparison_mixed.rs @@ -28,48 +28,48 @@ pub fn operator_eq_0(a: i32, b: Ptr) -> bool { let a: Value = Rc::new(RefCell::new(a)); return { let _lhs = (*a.borrow()); - _lhs == b.with(|__s: &S| __s.v) + _lhs == b.with(|__s| __s.v) }; } pub fn operator_ne_1(a: i32, b: Ptr) -> bool { let a: Value = Rc::new(RefCell::new(a)); return { let _lhs = (*a.borrow()); - _lhs != b.with(|__s: &S| __s.v) + _lhs != b.with(|__s| __s.v) }; } pub fn operator_lt_2(a: i32, b: Ptr) -> bool { let a: Value = Rc::new(RefCell::new(a)); return { let _lhs = (*a.borrow()); - _lhs < b.with(|__s: &S| __s.v) + _lhs < b.with(|__s| __s.v) }; } pub fn operator_gt_3(a: f64, b: Ptr) -> bool { let a: Value = Rc::new(RefCell::new(a)); return { let _lhs = (*a.borrow()); - _lhs > (b.with(|__s: &S| __s.v) as f64) + _lhs > (b.with(|__s| __s.v) as f64) }; } pub fn operator_le_4(a: i64, b: Ptr) -> bool { let a: Value = Rc::new(RefCell::new(a)); return { let _lhs = (*a.borrow()); - _lhs <= (b.with(|__s: &S| __s.v) as i64) + _lhs <= (b.with(|__s| __s.v) as i64) }; } pub fn operator_ge_5(a: Ptr, b: Ptr) -> bool { let a: Value> = Rc::new(RefCell::new(a)); return { let _lhs = ((((*a.borrow()).read()) as i32) - (('0' as u8) as i32)); - _lhs >= b.with(|__s: &S| __s.v) + _lhs >= b.with(|__s| __s.v) }; } pub fn operator_lt_6(a: Ptr, b: i32) -> bool { let b: Value = Rc::new(RefCell::new(b)); return { - let _lhs = (a.with(|__s: &S| __s.v) + 1); + let _lhs = (a.with(|__s| __s.v) + 1); _lhs < (*b.borrow()) }; } @@ -117,28 +117,28 @@ pub trait SImpl { impl SImpl for Ptr { fn operator_eq(&self, o: i32) -> bool { let o: Value = Rc::new(RefCell::new(o)); - return ((*self).with(|__s: &S| __s.v) == (*o.borrow())); + return ((*self).with(|__s| __s.v) == (*o.borrow())); } fn operator_ne(&self, o: i32) -> bool { let o: Value = Rc::new(RefCell::new(o)); - return ((*self).with(|__s: &S| __s.v) != (*o.borrow())); + return ((*self).with(|__s| __s.v) != (*o.borrow())); } fn operator_lt(&self, o: i32) -> bool { let o: Value = Rc::new(RefCell::new(o)); - return ((*self).with(|__s: &S| __s.v) < (*o.borrow())); + return ((*self).with(|__s| __s.v) < (*o.borrow())); } fn operator_gt(&self, o: f64) -> bool { let o: Value = Rc::new(RefCell::new(o)); - return (((*self).with(|__s: &S| __s.v) as f64) > (*o.borrow())); + return (((*self).with(|__s| __s.v) as f64) > (*o.borrow())); } fn operator_le(&self, o: i64) -> bool { let o: Value = Rc::new(RefCell::new(o)); - return (((*self).with(|__s: &S| __s.v) as i64) <= (*o.borrow())); + return (((*self).with(|__s| __s.v) as i64) <= (*o.borrow())); } fn operator_ge(&self, o: Ptr) -> bool { let o: Value> = Rc::new(RefCell::new(o)); return { - let _lhs = (*self).with(|__s: &S| __s.v); + let _lhs = (*self).with(|__s| __s.v); _lhs >= ((((*o.borrow()).read()) as i32) - (('0' as u8) as i32)) }; } diff --git a/tests/unit/out/refcount/operator_comparison_noncopyable.rs b/tests/unit/out/refcount/operator_comparison_noncopyable.rs index 1bff0a0715..9654c62b85 100644 --- a/tests/unit/out/refcount/operator_comparison_noncopyable.rs +++ b/tests/unit/out/refcount/operator_comparison_noncopyable.rs @@ -8,14 +8,14 @@ use std::os::fd::AsFd; use std::rc::{Rc, Weak}; pub fn operator_eq_0(x: Ptr, y: Ptr) -> bool { return { - let _lhs = x.with(|__s: &S| __s.data_); - _lhs == y.with(|__s: &S| __s.data_) + let _lhs = x.with(|__s| __s.data_); + _lhs == y.with(|__s| __s.data_) }; } pub fn operator_lt_1(x: Ptr, y: Ptr) -> bool { return { - let _lhs = x.with(|__s: &S| __s.data_); - _lhs < y.with(|__s: &S| __s.data_) + let _lhs = x.with(|__s| __s.data_); + _lhs < y.with(|__s| __s.data_) }; } #[derive(Record, Default)] @@ -34,7 +34,7 @@ impl S { } pub fn move_from(_a0: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - data_: { _a0.with(|__s: &S| __s.data_) }, + data_: { (*_a0.upgrade().deref()).data_ }, })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() diff --git a/tests/unit/out/refcount/operator_compound_assignment_free.rs b/tests/unit/out/refcount/operator_compound_assignment_free.rs index 00df835bec..6941687933 100644 --- a/tests/unit/out/refcount/operator_compound_assignment_free.rs +++ b/tests/unit/out/refcount/operator_compound_assignment_free.rs @@ -26,62 +26,77 @@ impl ByteRepr for S { } pub fn operator_add_assign_0(a: Ptr, b: Ptr) -> Ptr { { - let rhs_0 = (a.with(|__s: &S| __s.v)).wrapping_add(b.with(|__s: &S| __s.v)); - a.with_mut(|__s: &mut S| __s.v = rhs_0) + let rhs_0 = (a.with(|__s| __s.v)).wrapping_add(b.with(|__s| __s.v)); + field!(a, v).write(rhs_0) }; return (a).clone(); } pub fn operator_sub_assign_1(a: Ptr, b: Ptr) -> Ptr { { - let rhs_0 = (a.with(|__s: &S| __s.v)).wrapping_sub(b.with(|__s: &S| __s.v)); - a.with_mut(|__s: &mut S| __s.v = rhs_0) + let rhs_0 = (a.with(|__s| __s.v)).wrapping_sub(b.with(|__s| __s.v)); + field!(a, v).write(rhs_0) }; return (a).clone(); } pub fn operator_mul_assign_2(a: Ptr, b: Ptr) -> Ptr { { - let rhs_0 = (a.with(|__s: &S| __s.v)).wrapping_mul(b.with(|__s: &S| __s.v)); - a.with_mut(|__s: &mut S| __s.v = rhs_0) + let rhs_0 = (a.with(|__s| __s.v)).wrapping_mul(b.with(|__s| __s.v)); + field!(a, v).write(rhs_0) }; return (a).clone(); } pub fn operator_div_assign_3(a: Ptr, b: Ptr) -> Ptr { { - let rhs_0 = (a.with(|__s: &S| __s.v)).wrapping_div(b.with(|__s: &S| __s.v)); - a.with_mut(|__s: &mut S| __s.v = rhs_0) + let rhs_0 = (a.with(|__s| __s.v)).wrapping_div(b.with(|__s| __s.v)); + field!(a, v).write(rhs_0) }; return (a).clone(); } pub fn operator_rem_assign_4(a: Ptr, b: Ptr) -> Ptr { { - let rhs_0 = (a.with(|__s: &S| __s.v)).wrapping_rem(b.with(|__s: &S| __s.v)); - a.with_mut(|__s: &mut S| __s.v = rhs_0) + let rhs_0 = (a.with(|__s| __s.v)).wrapping_rem(b.with(|__s| __s.v)); + field!(a, v).write(rhs_0) }; return (a).clone(); } pub fn operator_bitand_assign_5(a: Ptr, b: Ptr) -> Ptr { - let __rhs = b.with(|__s: &S| __s.v); - a.with_mut(|__s: &mut S| __s.v &= __rhs); + let __rhs = b.with(|__s| __s.v); + { + let _ptr = field!(a, v); + _ptr.write(_ptr.read() & __rhs) + }; return (a).clone(); } pub fn operator_bitor_assign_6(a: Ptr, b: Ptr) -> Ptr { - let __rhs = b.with(|__s: &S| __s.v); - a.with_mut(|__s: &mut S| __s.v |= __rhs); + let __rhs = b.with(|__s| __s.v); + { + let _ptr = field!(a, v); + _ptr.write(_ptr.read() | __rhs) + }; return (a).clone(); } pub fn operator_bitxor_assign_7(a: Ptr, b: Ptr) -> Ptr { - let __rhs = b.with(|__s: &S| __s.v); - a.with_mut(|__s: &mut S| __s.v ^= __rhs); + let __rhs = b.with(|__s| __s.v); + { + let _ptr = field!(a, v); + _ptr.write(_ptr.read() ^ __rhs) + }; return (a).clone(); } pub fn operator_shl_assign_8(a: Ptr, n: i32) -> Ptr { let n: Value = Rc::new(RefCell::new(n)); - a.with_mut(|__s: &mut S| __s.v <<= (*n.borrow())); + { + let _ptr = field!(a, v); + _ptr.write(_ptr.read() << (*n.borrow())) + }; return (a).clone(); } pub fn operator_shr_assign_9(a: Ptr, n: i32) -> Ptr { let n: Value = Rc::new(RefCell::new(n)); - a.with_mut(|__s: &mut S| __s.v >>= (*n.borrow())); + { + let _ptr = field!(a, v); + _ptr.write(_ptr.read() >> (*n.borrow())) + }; return (a).clone(); } pub fn main() { diff --git a/tests/unit/out/refcount/operator_compound_assignment_member.rs b/tests/unit/out/refcount/operator_compound_assignment_member.rs index 719e47d389..1bbbeb2f53 100644 --- a/tests/unit/out/refcount/operator_compound_assignment_member.rs +++ b/tests/unit/out/refcount/operator_compound_assignment_member.rs @@ -86,67 +86,82 @@ pub trait SImpl { impl SImpl for Ptr { fn operator_assign_1(&self, n: u32) -> Ptr { let n: Value = Rc::new(RefCell::new(n)); - (*self).with_mut(|__s: &mut S| __s.v = (*n.borrow())); + field!((*self), v).write((*n.borrow())); return (*self).clone(); } fn operator_add_assign(&self, o: Ptr) -> Ptr { { - let rhs_0 = ((*self).with(|__s: &S| __s.v)).wrapping_add(o.with(|__s: &S| __s.v)); - (*self).with_mut(|__s: &mut S| __s.v = rhs_0) + let rhs_0 = ((*self).with(|__s| __s.v)).wrapping_add(o.with(|__s| __s.v)); + field!((*self), v).write(rhs_0) }; return (*self).clone(); } fn operator_sub_assign(&self, o: Ptr) -> Ptr { { - let rhs_0 = ((*self).with(|__s: &S| __s.v)).wrapping_sub(o.with(|__s: &S| __s.v)); - (*self).with_mut(|__s: &mut S| __s.v = rhs_0) + let rhs_0 = ((*self).with(|__s| __s.v)).wrapping_sub(o.with(|__s| __s.v)); + field!((*self), v).write(rhs_0) }; return (*self).clone(); } fn operator_mul_assign(&self, o: Ptr) -> Ptr { { - let rhs_0 = ((*self).with(|__s: &S| __s.v)).wrapping_mul(o.with(|__s: &S| __s.v)); - (*self).with_mut(|__s: &mut S| __s.v = rhs_0) + let rhs_0 = ((*self).with(|__s| __s.v)).wrapping_mul(o.with(|__s| __s.v)); + field!((*self), v).write(rhs_0) }; return (*self).clone(); } fn operator_div_assign(&self, o: Ptr) -> Ptr { { - let rhs_0 = ((*self).with(|__s: &S| __s.v)).wrapping_div(o.with(|__s: &S| __s.v)); - (*self).with_mut(|__s: &mut S| __s.v = rhs_0) + let rhs_0 = ((*self).with(|__s| __s.v)).wrapping_div(o.with(|__s| __s.v)); + field!((*self), v).write(rhs_0) }; return (*self).clone(); } fn operator_rem_assign(&self, o: Ptr) -> Ptr { { - let rhs_0 = ((*self).with(|__s: &S| __s.v)).wrapping_rem(o.with(|__s: &S| __s.v)); - (*self).with_mut(|__s: &mut S| __s.v = rhs_0) + let rhs_0 = ((*self).with(|__s| __s.v)).wrapping_rem(o.with(|__s| __s.v)); + field!((*self), v).write(rhs_0) }; return (*self).clone(); } fn operator_bitand_assign(&self, o: Ptr) -> Ptr { - let __rhs = o.with(|__s: &S| __s.v); - (*self).with_mut(|__s: &mut S| __s.v &= __rhs); + let __rhs = o.with(|__s| __s.v); + { + let _ptr = field!((*self), v); + _ptr.write(_ptr.read() & __rhs) + }; return (*self).clone(); } fn operator_bitor_assign(&self, o: Ptr) -> Ptr { - let __rhs = o.with(|__s: &S| __s.v); - (*self).with_mut(|__s: &mut S| __s.v |= __rhs); + let __rhs = o.with(|__s| __s.v); + { + let _ptr = field!((*self), v); + _ptr.write(_ptr.read() | __rhs) + }; return (*self).clone(); } fn operator_bitxor_assign(&self, o: Ptr) -> Ptr { - let __rhs = o.with(|__s: &S| __s.v); - (*self).with_mut(|__s: &mut S| __s.v ^= __rhs); + let __rhs = o.with(|__s| __s.v); + { + let _ptr = field!((*self), v); + _ptr.write(_ptr.read() ^ __rhs) + }; return (*self).clone(); } fn operator_shl_assign(&self, n: i32) -> Ptr { let n: Value = Rc::new(RefCell::new(n)); - (*self).with_mut(|__s: &mut S| __s.v <<= (*n.borrow())); + { + let _ptr = field!((*self), v); + _ptr.write(_ptr.read() << (*n.borrow())) + }; return (*self).clone(); } fn operator_shr_assign(&self, n: i32) -> Ptr { let n: Value = Rc::new(RefCell::new(n)); - (*self).with_mut(|__s: &mut S| __s.v >>= (*n.borrow())); + { + let _ptr = field!((*self), v); + _ptr.write(_ptr.read() >> (*n.borrow())) + }; return (*self).clone(); } } diff --git a/tests/unit/out/refcount/operator_friend.rs b/tests/unit/out/refcount/operator_friend.rs index ab5c6ccacb..bb91ed00c0 100644 --- a/tests/unit/out/refcount/operator_friend.rs +++ b/tests/unit/out/refcount/operator_friend.rs @@ -8,11 +8,11 @@ use std::os::fd::AsFd; use std::rc::{Rc, Weak}; pub fn operator_eq_0(_a0: Ptr, _a1: Ptr) -> bool { return ({ - let _lhs = _a0.with(|__s: &Defaulted| __s.a); - _lhs == _a1.with(|__s: &Defaulted| __s.a) + let _lhs = _a0.with(|__s| __s.a); + _lhs == _a1.with(|__s| __s.a) }) && ({ - let _lhs = _a0.with(|__s: &Defaulted| __s.b); - _lhs == _a1.with(|__s: &Defaulted| __s.b) + let _lhs = _a0.with(|__s| __s.b); + _lhs == _a1.with(|__s| __s.b) }); } #[derive(Clone, Record, VaArg, FnPtrArg, Default)] @@ -59,8 +59,8 @@ impl ByteRepr for Defaulted { pub fn operator_cmp_1(_a0: Ptr, _a1: Ptr) -> std::cmp::Ordering { { let cmp: Value = Rc::new(RefCell::new(std::cmp::Ord::cmp( - &(_a0.with(|__s: &DefaultedOrd| __s.a)), - &(_a1.with(|__s: &DefaultedOrd| __s.a)), + &(_a0.with(|__s| __s.a)), + &(_a1.with(|__s| __s.a)), ))); if !((*cmp.borrow()) == std::cmp::Ordering::Equal) { return (*cmp.borrow_mut()).clone(); @@ -70,8 +70,8 @@ pub fn operator_cmp_1(_a0: Ptr, _a1: Ptr) -> std::cm } pub fn operator_eq_2(_a0: Ptr, _a1: Ptr) -> bool { return { - let _lhs = _a0.with(|__s: &DefaultedOrd| __s.a); - _lhs == _a1.with(|__s: &DefaultedOrd| __s.a) + let _lhs = _a0.with(|__s| __s.a); + _lhs == _a1.with(|__s| __s.a) }; } #[derive(Clone, Record, VaArg, FnPtrArg, Default)] @@ -120,21 +120,21 @@ impl ByteRepr for DefaultedOrd { } pub fn operator_eq_3(x: Ptr, y: Ptr) -> bool { return { - let _lhs = x.with(|__s: &Inline| __s.a); - _lhs == y.with(|__s: &Inline| __s.a) + let _lhs = x.with(|__s| __s.a); + _lhs == y.with(|__s| __s.a) }; } pub fn operator_lt_4(x: Ptr, y: Ptr) -> bool { return { - let _lhs = x.with(|__s: &Inline| __s.a); - _lhs < y.with(|__s: &Inline| __s.a) + let _lhs = x.with(|__s| __s.a); + _lhs < y.with(|__s| __s.a) }; } pub fn operator_add_5(x: Ptr, y: Ptr) -> Inline { return Inline { a: { - let _lhs = x.with(|__s: &Inline| __s.a); - _lhs + y.with(|__s: &Inline| __s.a) + let _lhs = x.with(|__s| __s.a); + _lhs + y.with(|__s| __s.a) }, }; } @@ -222,8 +222,8 @@ impl ByteRepr for OutOfLine { } pub fn operator_eq_6(x: Ptr, y: Ptr) -> bool { return { - let _lhs = x.with(|__s: &OutOfLine| __s.a); - _lhs == y.with(|__s: &OutOfLine| __s.a) + let _lhs = x.with(|__s| __s.a); + _lhs == y.with(|__s| __s.a) }; } pub fn operator_ne_7(x: Ptr, y: Ptr) -> bool { @@ -235,20 +235,20 @@ pub fn operator_ne_7(x: Ptr, y: Ptr) -> bool { } pub fn operator_eq_8(x: Ptr, y: Ptr) -> bool { return { - let _lhs = x.with(|__s: &Tmpl_int_| __s.v); - _lhs == y.with(|__s: &Tmpl_int_| __s.v) + let _lhs = x.with(|__s| __s.v); + _lhs == y.with(|__s| __s.v) }; } pub fn operator_lt_9(x: Ptr, y: Ptr) -> bool { return { - let _lhs = x.with(|__s: &Tmpl_int_| __s.v); - _lhs < y.with(|__s: &Tmpl_int_| __s.v) + let _lhs = x.with(|__s| __s.v); + _lhs < y.with(|__s| __s.v) }; } pub fn operator_eq_10(x: Ptr, y: Ptr) -> bool { return { - let _lhs = (x.with(|__s: &Tmpl_int_| __s.v) as i64); - _lhs == y.with(|__s: &Tmpl_long_| __s.v) + let _lhs = (x.with(|__s| __s.v) as i64); + _lhs == y.with(|__s| __s.v) }; } #[derive(Clone, Record, VaArg, FnPtrArg, Default)] @@ -324,8 +324,8 @@ impl ByteRepr for Tmpl_long_ { } pub fn operator_eq_11(_a0: Ptr, _a1: Ptr) -> bool { return { - let _lhs = _a0.with(|__s: &TmplDefaulted_int_| __s.v); - _lhs == _a1.with(|__s: &TmplDefaulted_int_| __s.v) + let _lhs = _a0.with(|__s| __s.v); + _lhs == _a1.with(|__s| __s.v) }; } #[derive(Clone, Record, VaArg, FnPtrArg, Default)] diff --git a/tests/unit/out/refcount/operator_less_than.rs b/tests/unit/out/refcount/operator_less_than.rs index 3c82c1bbcc..cc31246e64 100644 --- a/tests/unit/out/refcount/operator_less_than.rs +++ b/tests/unit/out/refcount/operator_less_than.rs @@ -114,14 +114,14 @@ pub trait PairImpl { impl PairImpl for Ptr { fn operator_lt(&self, other: Ptr) -> bool { return ({ - let _lhs = (*self).with(|__s: &Pair| __s.x); - _lhs < other.with(|__s: &Pair| __s.x) + let _lhs = (*self).with(|__s| __s.x); + _lhs < other.with(|__s| __s.x) }) || (({ - let _lhs = (*self).with(|__s: &Pair| __s.x); - _lhs == other.with(|__s: &Pair| __s.x) + let _lhs = (*self).with(|__s| __s.x); + _lhs == other.with(|__s| __s.x) }) && ({ - let _lhs = (*self).with(|__s: &Pair| __s.y); - _lhs < other.with(|__s: &Pair| __s.y) + let _lhs = (*self).with(|__s| __s.y); + _lhs < other.with(|__s| __s.y) })); } } diff --git a/tests/unit/out/refcount/operator_logical_free.rs b/tests/unit/out/refcount/operator_logical_free.rs index 0ff3f1024f..2e9ffdf11b 100644 --- a/tests/unit/out/refcount/operator_logical_free.rs +++ b/tests/unit/out/refcount/operator_logical_free.rs @@ -25,13 +25,13 @@ impl ByteRepr for S { } } pub fn operator_not_0(a: Ptr) -> bool { - return (a.with(|__s: &S| __s.v) == 0); + return (a.with(|__s| __s.v) == 0); } pub fn operator_and_1(a: Ptr, b: Ptr) -> bool { - return (a.with(|__s: &S| __s.v) != 0) && (b.with(|__s: &S| __s.v) != 0); + return (a.with(|__s| __s.v) != 0) && (b.with(|__s| __s.v) != 0); } pub fn operator_or_2(a: Ptr, b: Ptr) -> bool { - return (a.with(|__s: &S| __s.v) != 0) || (b.with(|__s: &S| __s.v) != 0); + return (a.with(|__s| __s.v) != 0) || (b.with(|__s| __s.v) != 0); } pub fn main() { __cpp2rust_init_globals(); diff --git a/tests/unit/out/refcount/operator_logical_member.rs b/tests/unit/out/refcount/operator_logical_member.rs index e01a7c50ea..8bcef7475a 100644 --- a/tests/unit/out/refcount/operator_logical_member.rs +++ b/tests/unit/out/refcount/operator_logical_member.rs @@ -56,13 +56,13 @@ pub trait SImpl { } impl SImpl for Ptr { fn operator_not(&self) -> bool { - return ((*self).with(|__s: &S| __s.v) == 0); + return ((*self).with(|__s| __s.v) == 0); } fn operator_and(&self, o: Ptr) -> bool { - return ((*self).with(|__s: &S| __s.v) != 0) && (o.with(|__s: &S| __s.v) != 0); + return ((*self).with(|__s| __s.v) != 0) && (o.with(|__s| __s.v) != 0); } fn operator_or(&self, o: Ptr) -> bool { - return ((*self).with(|__s: &S| __s.v) != 0) || (o.with(|__s: &S| __s.v) != 0); + return ((*self).with(|__s| __s.v) != 0) || (o.with(|__s| __s.v) != 0); } } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/operator_member_pointer_free.rs b/tests/unit/out/refcount/operator_member_pointer_free.rs index e11fadec20..8d82758df1 100644 --- a/tests/unit/out/refcount/operator_member_pointer_free.rs +++ b/tests/unit/out/refcount/operator_member_pointer_free.rs @@ -33,14 +33,10 @@ pub struct S { } impl Clone for S { fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - data: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 3, _>( - |__i: usize| (*self.data.borrow())[(__i) as usize], - )))), - inner: { self.inner.clone() }, - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() + Self { + data: Rc::new(RefCell::new((*self.data.borrow()).clone())), + inner: self.inner.clone(), + } } } impl Default for S { @@ -70,7 +66,7 @@ pub fn operator_deref_0(s: Ptr) -> Ptr { return field_ptr!(s, inner); } pub fn operator_addr_1(s: Ptr) -> Ptr { - return ((s.with(|__s: &S| __s.data.as_pointer()) as Ptr).offset(0)); + return ((s.with(|__s| __s.data.clone()).as_pointer() as Ptr).offset(0)); } pub fn main() { __cpp2rust_init_globals(); @@ -86,14 +82,17 @@ fn main_0() -> i32 { let _s: Ptr = s.as_pointer(); operator_deref_0(_s) }) - .with(|__s: &Inner| __s.x) + .with(|__s| (__s).x) == 9) ); - ({ - let _s: Ptr = s.as_pointer(); - operator_deref_0(_s) - }) - .with_mut(|__s: &mut Inner| __s.x = 10); + field!( + ({ + let _s: Ptr = s.as_pointer(); + operator_deref_0(_s) + }), + x + ) + .write(10); assert!(({ (*s.borrow()).inner.x } == 10)); let p: Value> = Rc::new(RefCell::new( ({ @@ -103,7 +102,7 @@ fn main_0() -> i32 { )); assert!((((*p.borrow()).read()) == 1)); (*p.borrow()).write(5); - assert!(((*(*s.borrow()).data.borrow())[(0) as usize] == 5)); + assert!(((*{ (*s.borrow()).data.clone() }.borrow())[(0) as usize] == 5)); return 0; } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/operator_member_pointer_member.rs b/tests/unit/out/refcount/operator_member_pointer_member.rs index 26afebeeb9..c1fecbff74 100644 --- a/tests/unit/out/refcount/operator_member_pointer_member.rs +++ b/tests/unit/out/refcount/operator_member_pointer_member.rs @@ -44,14 +44,10 @@ pub struct S { } impl Clone for S { fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - data: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 3, _>( - |__i: usize| (*self.data.borrow())[(__i) as usize], - )))), - inner: { self.inner.clone() }, - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() + Self { + data: Rc::new(RefCell::new((*self.data.borrow()).clone())), + inner: self.inner.clone(), + } } } impl Default for S { @@ -91,22 +87,15 @@ fn main_0() -> i32 { assert!(((({ SImpl::operator_index_1(&s.as_pointer(), 1,) }).read()) == 20)); let cs: Ptr = s.as_pointer(); assert!(((({ SImpl::operator_index_2(&cs, 2,) }).read()) == 3)); - assert!((({ SImpl::operator_deref(&s.as_pointer(),) }).with(|__s: &Inner| __s.x) == 9)); - ({ SImpl::operator_deref(&s.as_pointer()) }).with_mut(|__s: &mut Inner| __s.x = 10); - assert!( - ({ - (*({ SImpl::operator_arrow(&s.as_pointer()) }) - .upgrade() - .deref()) - .x - } == 10) - ); - { field_ptr!(({ SImpl::operator_arrow(&s.as_pointer(),) }), x) }.write(11); + assert!((({ SImpl::operator_deref(&s.as_pointer(),) }).with(|__s| (__s).x) == 9)); + field!(({ SImpl::operator_deref(&s.as_pointer(),) }), x).write(10); + assert!((({ SImpl::operator_arrow(&s.as_pointer(),) }).with(|__s| __s.x) == 10)); + field!(({ SImpl::operator_arrow(&s.as_pointer(),) }), x).write(11); assert!(({ (*s.borrow()).inner.x } == 11)); let p: Value> = Rc::new(RefCell::new(({ SImpl::operator_addr(&s.as_pointer()) }))); assert!((((*p.borrow()).read()) == 1)); (*p.borrow()).write(5); - assert!(((*(*s.borrow()).data.borrow())[(0) as usize] == 5)); + assert!(((*{ (*s.borrow()).data.clone() }.borrow())[(0) as usize] == 5)); let t: Value
= Rc::new(RefCell::new(
::default())); assert!(((({ Table::operator_index(1,) }).read()) == 8)); ({ Table::operator_index(1) }).write(80); @@ -128,11 +117,13 @@ pub trait SImpl { impl SImpl for Ptr { fn operator_index_1(&self, i: i32) -> Ptr { let i: Value = Rc::new(RefCell::new(i)); - return ((*self).with(|__s: &S| __s.data.as_pointer()) as Ptr).offset((*i.borrow())); + return ((*self).with(|__s| __s.data.clone()).as_pointer() as Ptr) + .offset((*i.borrow())); } fn operator_index_2(&self, i: i32) -> Ptr { let i: Value = Rc::new(RefCell::new(i)); - return ((*self).with(|__s: &S| __s.data.as_pointer()) as Ptr).offset((*i.borrow())); + return ((*self).with(|__s| __s.data.clone()).as_pointer() as Ptr) + .offset((*i.borrow())); } fn operator_deref(&self) -> Ptr { return field_ptr!((*self), inner); @@ -141,7 +132,7 @@ impl SImpl for Ptr { return (field_ptr!((*self), inner)); } fn operator_addr(&self) -> Ptr { - return (((*self).with(|__s: &S| __s.data.as_pointer()) as Ptr).offset(0)); + return (((*self).with(|__s| __s.data.clone()).as_pointer() as Ptr).offset(0)); } } pub fn __cpp2rust_init_globals() { diff --git a/tests/unit/out/refcount/operator_other_free.rs b/tests/unit/out/refcount/operator_other_free.rs index a2c693d79c..7dee7942f3 100644 --- a/tests/unit/out/refcount/operator_other_free.rs +++ b/tests/unit/out/refcount/operator_other_free.rs @@ -27,8 +27,8 @@ impl ByteRepr for S { pub fn operator_comma_0(a: Ptr, b: Ptr) -> S { return S { v: { - let _lhs = (a.with(|__s: &S| __s.v) * 10); - _lhs + b.with(|__s: &S| __s.v) + let _lhs = (a.with(|__s| __s.v) * 10); + _lhs + b.with(|__s| __s.v) }, }; } diff --git a/tests/unit/out/refcount/operator_other_member.rs b/tests/unit/out/refcount/operator_other_member.rs index c3b6e1a50d..ce545e59d1 100644 --- a/tests/unit/out/refcount/operator_other_member.rs +++ b/tests/unit/out/refcount/operator_other_member.rs @@ -74,30 +74,30 @@ pub trait SImpl { } impl SImpl for Ptr { fn operator_call_1(&self) -> i32 { - return (*self).with(|__s: &S| __s.v); + return (*self).with(|__s| __s.v); } fn operator_call_2(&self, a: i32) -> i32 { let a: Value = Rc::new(RefCell::new(a)); - return ((*self).with(|__s: &S| __s.v) + (*a.borrow())); + return ((*self).with(|__s| __s.v) + (*a.borrow())); } fn operator_call_3(&self, a: i32, b: i32) -> i32 { let a: Value = Rc::new(RefCell::new(a)); let b: Value = Rc::new(RefCell::new(b)); - return (((*self).with(|__s: &S| __s.v) + (*a.borrow())) + (*b.borrow())); + return (((*self).with(|__s| __s.v) + (*a.borrow())) + (*b.borrow())); } fn operator_comma(&self, o: Ptr) -> S { return S { v: { - let _lhs = ((*self).with(|__s: &S| __s.v) * 10); - _lhs + o.with(|__s: &S| __s.v) + let _lhs = ((*self).with(|__s| __s.v) * 10); + _lhs + o.with(|__s| __s.v) }, }; } fn to_i32(&self) -> i32 { - return (*self).with(|__s: &S| __s.v); + return (*self).with(|__s| __s.v); } fn to_bool(&self) -> bool { - return ((*self).with(|__s: &S| __s.v) != 0); + return ((*self).with(|__s| __s.v) != 0); } } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/operator_overloads.rs b/tests/unit/out/refcount/operator_overloads.rs index b36aad1667..f5efeaea51 100644 --- a/tests/unit/out/refcount/operator_overloads.rs +++ b/tests/unit/out/refcount/operator_overloads.rs @@ -26,8 +26,8 @@ impl ByteRepr for S { } pub fn operator_div_0(a: Ptr, b: Ptr) -> i32 { return { - let _lhs = a.with(|__s: &S| __s.v); - _lhs / b.with(|__s: &S| __s.v) + let _lhs = a.with(|__s| __s.v); + _lhs / b.with(|__s| __s.v) }; } pub fn operator_div_1(a: S, b: i32) -> i32 { @@ -43,7 +43,7 @@ pub fn operator_rem_2(a: S, b: S) -> i32 { pub fn operator_rem_3(a: Ptr, b: i32) -> i32 { let b: Value = Rc::new(RefCell::new(b)); return ({ - let _lhs = a.with(|__s: &S| __s.v); + let _lhs = a.with(|__s| __s.v); _lhs % (*b.borrow()) } + 1); } @@ -60,7 +60,7 @@ pub fn operator_eq_5(a: i64, b: Ptr) -> i32 { let a: Value = Rc::new(RefCell::new(a)); return if { let _lhs = (*a.borrow()); - _lhs == (b.with(|__s: &S| __s.v) as i64) + _lhs == (b.with(|__s| __s.v) as i64) } { 5 } else { @@ -122,7 +122,7 @@ pub trait SImpl { impl SImpl for Ptr { fn operator_eq_1(&self, o: i32) -> i32 { let o: Value = Rc::new(RefCell::new(o)); - return if ((*self).with(|__s: &S| __s.v) == (*o.borrow())) { + return if ((*self).with(|__s| __s.v) == (*o.borrow())) { 1 } else { 0 @@ -130,7 +130,7 @@ impl SImpl for Ptr { } fn operator_eq_2(&self, o: i64) -> i32 { let o: Value = Rc::new(RefCell::new(o)); - return if (((*self).with(|__s: &S| __s.v) as i64) == (*o.borrow())) { + return if (((*self).with(|__s| __s.v) as i64) == (*o.borrow())) { 2 } else { 0 @@ -138,7 +138,7 @@ impl SImpl for Ptr { } fn operator_eq_3(&self, o: f64) -> i32 { let o: Value = Rc::new(RefCell::new(o)); - return if (((*self).with(|__s: &S| __s.v) as f64) == (*o.borrow())) { + return if (((*self).with(|__s| __s.v) as f64) == (*o.borrow())) { 3 } else { 0 @@ -146,23 +146,23 @@ impl SImpl for Ptr { } fn operator_add(&self, o: Ptr) -> i32 { return { - let _lhs = (*self).with(|__s: &S| __s.v); - _lhs + o.with(|__s: &S| __s.v) + let _lhs = (*self).with(|__s| __s.v); + _lhs + o.with(|__s| __s.v) }; } fn operator_sub(&self, o: S) -> i32 { let o: Value = Rc::new(RefCell::new(o)); - return ((*self).with(|__s: &S| __s.v) - { (*o.borrow()).v }); + return ((*self).with(|__s| __s.v) - { (*o.borrow()).v }); } fn operator_mul_6(&self, o: Ptr) -> i32 { return { - let _lhs = (*self).with(|__s: &S| __s.v); - _lhs * o.with(|__s: &S| __s.v) + let _lhs = (*self).with(|__s| __s.v); + _lhs * o.with(|__s| __s.v) }; } fn operator_mul_7(&self, o: i32) -> i32 { let o: Value = Rc::new(RefCell::new(o)); - return (((*self).with(|__s: &S| __s.v) * (*o.borrow())) + 1); + return (((*self).with(|__s| __s.v) * (*o.borrow())) + 1); } } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/operator_qualifiers.rs b/tests/unit/out/refcount/operator_qualifiers.rs index f50ed3db78..d87ebdb658 100644 --- a/tests/unit/out/refcount/operator_qualifiers.rs +++ b/tests/unit/out/refcount/operator_qualifiers.rs @@ -62,39 +62,39 @@ pub trait SImpl { impl SImpl for Ptr { fn operator_add_1(&self, a: i32) -> i32 { let a: Value = Rc::new(RefCell::new(a)); - return ((*self).with(|__s: &S| __s.v) + (*a.borrow())); + return ((*self).with(|__s| __s.v) + (*a.borrow())); } fn operator_add_2(&self, a: i32) -> i32 { let a: Value = Rc::new(RefCell::new(a)); - return (((*self).with(|__s: &S| __s.v) + (*a.borrow())) + 1); + return (((*self).with(|__s| __s.v) + (*a.borrow())) + 1); } fn operator_add_3(&self, a: i32) -> i32 { let a: Value = Rc::new(RefCell::new(a)); - return (((*self).with(|__s: &S| __s.v) + (*a.borrow())) + 2); + return (((*self).with(|__s| __s.v) + (*a.borrow())) + 2); } fn operator_sub_4(&self, a: i32) -> i32 { let a: Value = Rc::new(RefCell::new(a)); - return ((*self).with(|__s: &S| __s.v) - (*a.borrow())); + return ((*self).with(|__s| __s.v) - (*a.borrow())); } fn operator_sub_5(&self, a: i32) -> i32 { let a: Value = Rc::new(RefCell::new(a)); - return (((*self).with(|__s: &S| __s.v) - (*a.borrow())) - 1); + return (((*self).with(|__s| __s.v) - (*a.borrow())) - 1); } fn operator_mul_6(&self, a: i32) -> i32 { let a: Value = Rc::new(RefCell::new(a)); - return ((*self).with(|__s: &S| __s.v) * (*a.borrow())); + return ((*self).with(|__s| __s.v) * (*a.borrow())); } fn operator_mul_7(&self, a: i32) -> i32 { let a: Value = Rc::new(RefCell::new(a)); - return (((*self).with(|__s: &S| __s.v) * (*a.borrow())) * 2); + return (((*self).with(|__s| __s.v) * (*a.borrow())) * 2); } fn operator_index_8(&self, i: i32) -> i32 { let i: Value = Rc::new(RefCell::new(i)); - return ((*self).with(|__s: &S| __s.v) + (*i.borrow())); + return ((*self).with(|__s| __s.v) + (*i.borrow())); } fn operator_index_9(&self, i: i32) -> i32 { let i: Value = Rc::new(RefCell::new(i)); - return (((*self).with(|__s: &S| __s.v) + (*i.borrow())) + 100); + return (((*self).with(|__s| __s.v) + (*i.borrow())) + 100); } } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/operator_three_way.rs b/tests/unit/out/refcount/operator_three_way.rs index 6647d907a1..32529d6165 100644 --- a/tests/unit/out/refcount/operator_three_way.rs +++ b/tests/unit/out/refcount/operator_three_way.rs @@ -82,14 +82,14 @@ pub trait SImpl { impl SImpl for Ptr { fn operator_cmp(&self, o: Ptr) -> std::cmp::Ordering { if { - let _lhs = (*self).with(|__s: &S| __s.v); - _lhs < o.with(|__s: &S| __s.v) + let _lhs = (*self).with(|__s| __s.v); + _lhs < o.with(|__s| __s.v) } { return std::cmp::Ordering::Less; } if { - let _lhs = (*self).with(|__s: &S| __s.v); - _lhs > o.with(|__s: &S| __s.v) + let _lhs = (*self).with(|__s| __s.v); + _lhs > o.with(|__s| __s.v) } { return std::cmp::Ordering::Greater; } @@ -97,8 +97,8 @@ impl SImpl for Ptr { } fn operator_eq(&self, o: Ptr) -> bool { return { - let _lhs = (*self).with(|__s: &S| __s.v); - _lhs == o.with(|__s: &S| __s.v) + let _lhs = (*self).with(|__s| __s.v); + _lhs == o.with(|__s| __s.v) }; } } diff --git a/tests/unit/out/refcount/operator_traits.rs b/tests/unit/out/refcount/operator_traits.rs index 56b2a0a685..f24368b6ed 100644 --- a/tests/unit/out/refcount/operator_traits.rs +++ b/tests/unit/out/refcount/operator_traits.rs @@ -190,14 +190,14 @@ impl ByteRepr for Free { } pub fn operator_lt_0(a: Ptr, b: Ptr) -> bool { return { - let _lhs = a.with(|__s: &Free| __s.v); - _lhs < b.with(|__s: &Free| __s.v) + let _lhs = a.with(|__s| __s.v); + _lhs < b.with(|__s| __s.v) }; } pub fn operator_eq_1(a: Ptr, b: Ptr) -> bool { return { - let _lhs = a.with(|__s: &Free| __s.v); - _lhs == b.with(|__s: &Free| __s.v) + let _lhs = a.with(|__s| __s.v); + _lhs == b.with(|__s| __s.v) }; } #[derive(Clone, Record, VaArg, FnPtrArg, Default)] @@ -228,18 +228,27 @@ fn main_0() -> i32 { let lts: Value> = Rc::new(RefCell::new(vec![Lt { v: 3 }, Lt { v: 1 }, Lt { v: 2 }])); (lts.as_pointer() as Ptr).sort((lts.as_pointer() as Ptr).to_end().get_offset()); assert!( - (((lts.as_pointer() as Ptr) - .offset(0_usize) - .with(|__s: &Lt| __s.v) - == 1) - && ((lts.as_pointer() as Ptr) - .offset(1_usize) - .with(|__s: &Lt| __s.v) - == 2)) - && ((lts.as_pointer() as Ptr) - .offset(2_usize) - .with(|__s: &Lt| __s.v) - == 3) + (({ + (*(lts.as_pointer() as Ptr) + .offset(0_usize) + .upgrade() + .deref()) + .v + } == 1) + && ({ + (*(lts.as_pointer() as Ptr) + .offset(1_usize) + .upgrade() + .deref()) + .v + } == 2)) + && ({ + (*(lts.as_pointer() as Ptr) + .offset(2_usize) + .upgrade() + .deref()) + .v + } == 3) ); let eqs: Value> = Rc::new(RefCell::new(vec![Eq { v: 1 }, Eq { v: 2 }, Eq { v: 3 }])); let two: Value = Rc::new(RefCell::new(Eq { v: 2 })); @@ -281,14 +290,20 @@ fn main_0() -> i32 { Rc::new(RefCell::new(vec![Cmp { v: 3 }, Cmp { v: 1 }, Cmp { v: 2 }])); (cmps.as_pointer() as Ptr).sort((cmps.as_pointer() as Ptr).to_end().get_offset()); assert!( - ((cmps.as_pointer() as Ptr) - .offset(0_usize) - .with(|__s: &Cmp| __s.v) - == 1) - && ((cmps.as_pointer() as Ptr) - .offset(2_usize) - .with(|__s: &Cmp| __s.v) - == 3) + ({ + (*(cmps.as_pointer() as Ptr) + .offset(0_usize) + .upgrade() + .deref()) + .v + } == 1) + && ({ + (*(cmps.as_pointer() as Ptr) + .offset(2_usize) + .upgrade() + .deref()) + .v + } == 3) ); let three: Value = Rc::new(RefCell::new(Cmp { v: 3 })); assert!( @@ -313,10 +328,13 @@ fn main_0() -> i32 { let frees: Value> = Rc::new(RefCell::new(vec![Free { v: 2 }, Free { v: 1 }])); (frees.as_pointer() as Ptr).sort((frees.as_pointer() as Ptr).to_end().get_offset()); assert!( - ((frees.as_pointer() as Ptr) - .offset(0_usize) - .with(|__s: &Free| __s.v) - == 1) + ({ + (*(frees.as_pointer() as Ptr) + .offset(0_usize) + .upgrade() + .deref()) + .v + } == 1) ); let ftwo: Value = Rc::new(RefCell::new(Free { v: 2 })); assert!( @@ -378,15 +396,12 @@ pub trait CmpImpl { } impl CmpImpl for Ptr { fn operator_cmp(&self, o: Ptr) -> std::cmp::Ordering { - return std::cmp::Ord::cmp( - &((*self).with(|__s: &Cmp| __s.v)), - &(o.with(|__s: &Cmp| __s.v)), - ); + return std::cmp::Ord::cmp(&((*self).with(|__s| __s.v)), &(o.with(|__s| __s.v))); } fn operator_eq(&self, o: Ptr) -> bool { return { - let _lhs = (*self).with(|__s: &Cmp| __s.v); - _lhs == o.with(|__s: &Cmp| __s.v) + let _lhs = (*self).with(|__s| __s.v); + _lhs == o.with(|__s| __s.v) }; } } @@ -396,8 +411,8 @@ pub trait EqImpl { impl EqImpl for Ptr { fn operator_eq(&self, o: Ptr) -> bool { return { - let _lhs = (*self).with(|__s: &Eq| __s.v); - _lhs == o.with(|__s: &Eq| __s.v) + let _lhs = (*self).with(|__s| __s.v); + _lhs == o.with(|__s| __s.v) }; } } @@ -407,8 +422,8 @@ pub trait LtImpl { impl LtImpl for Ptr { fn operator_lt(&self, o: Ptr) -> bool { return { - let _lhs = (*self).with(|__s: &Lt| __s.v); - _lhs < o.with(|__s: &Lt| __s.v) + let _lhs = (*self).with(|__s| __s.v); + _lhs < o.with(|__s| __s.v) }; } } diff --git a/tests/unit/out/refcount/overload_mangling.rs b/tests/unit/out/refcount/overload_mangling.rs index 6c95505e8b..3603a6f4cb 100644 --- a/tests/unit/out/refcount/overload_mangling.rs +++ b/tests/unit/out/refcount/overload_mangling.rs @@ -176,17 +176,17 @@ pub trait Access_S_Impl { impl Access_S_Impl for Ptr { fn get_1(&self, p: Ptr) -> i32 { let p: Value> = Rc::new(RefCell::new(p)); - return (*p.borrow()).with(|__s: &S| __s.base); + return (*p.borrow()).with(|__s| __s.base); } fn get_2(&self, p: Ptr) -> i32 { let p: Value> = Rc::new(RefCell::new(p)); - return ((*p.borrow()).with(|__s: &S| __s.base) + 1); + return ((*p.borrow()).with(|__s| __s.base) + 1); } fn ref_3(&self, r: Ptr) -> i32 { - return (r.with(|__s: &S| __s.base) + 2); + return (r.with(|__s| __s.base) + 2); } fn ref_4(&self, r: Ptr) -> i32 { - return (r.with(|__s: &S| __s.base) + 3); + return (r.with(|__s| __s.base) + 3); } } pub trait SImpl { @@ -223,49 +223,49 @@ pub trait SImpl { impl SImpl for Ptr { fn plain_4(&self, x: i32) -> i32 { let x: Value = Rc::new(RefCell::new(x)); - return ((*self).with(|__s: &S| __s.base) + (*x.borrow())); + return ((*self).with(|__s| __s.base) + (*x.borrow())); } fn plain_5(&self, x: i64) -> i32 { let x: Value = Rc::new(RefCell::new(x)); - return (((*self).with(|__s: &S| __s.base) + ((*x.borrow()) as i32)) + 1); + return (((*self).with(|__s| __s.base) + ((*x.borrow()) as i32)) + 1); } fn take_6(&self, x: Ptr) -> i32 { return ({ - let _lhs = (*self).with(|__s: &S| __s.base); + let _lhs = (*self).with(|__s| __s.base); _lhs + (x.read()) } + 1); } fn take_7(&self, x: Ptr) -> i32 { return ({ - let _lhs = (*self).with(|__s: &S| __s.base); + let _lhs = (*self).with(|__s| __s.base); _lhs + (x.read()) } + 2); } fn pick_8(&self, p: (Value, Value)) -> i32 { let p: Value<(Value, Value)> = Rc::new(RefCell::new(p)); - return ((*self).with(|__s: &S| __s.base) + (*(*p.borrow()).0.borrow())); + return ((*self).with(|__s| __s.base) + (*(*p.borrow()).0.borrow())); } fn pick_9(&self, p: (Value, Value)) -> i32 { let p: Value<(Value, Value)> = Rc::new(RefCell::new(p)); - return ((*self).with(|__s: &S| __s.base) + ((*(*p.borrow()).1.borrow()) as i32)); + return ((*self).with(|__s| __s.base) + ((*(*p.borrow()).1.borrow()) as i32)); } fn apply_10(&self, f: FnPtr)>, x: i32) -> i32 { let f: Value)>> = Rc::new(RefCell::new(f)); let x: Value = Rc::new(RefCell::new(x)); ({ (*f.borrow()).call((x.as_pointer())) }); - return ((*self).with(|__s: &S| __s.base) + (*x.borrow())); + return ((*self).with(|__s| __s.base) + (*x.borrow())); } fn apply_11(&self, f: FnPtr, i32)>, x: i32) -> i32 { let f: Value, i32)>> = Rc::new(RefCell::new(f)); let x: Value = Rc::new(RefCell::new(x)); ({ (*f.borrow()).call((x.as_pointer()), 10) }); - return ((*self).with(|__s: &S| __s.base) + (*x.borrow())); + return ((*self).with(|__s| __s.base) + (*x.borrow())); } fn apply_12(&self, f: FnPtr i32>, x: i32) -> i32 { let f: Value i32>> = Rc::new(RefCell::new(f)); let x: Value = Rc::new(RefCell::new(x)); return { - let _lhs = (*self).with(|__s: &S| __s.base); + let _lhs = (*self).with(|__s| __s.base); _lhs + ({ (*f.borrow()).call((*x.borrow())) }) }; } @@ -282,7 +282,7 @@ impl SImpl for Ptr { return { let _lhs = { let _lhs = { - let _lhs = (*self).with(|__s: &S| __s.base); + let _lhs = (*self).with(|__s| __s.base); _lhs + ((*(*p.upgrade().deref()).1.borrow()) as i32) }; _lhs + ({ (*f.borrow()).call(((*q.borrow()).read())) }) @@ -307,7 +307,7 @@ impl SImpl for Ptr { }); return { let _lhs = { - let _lhs = (*self).with(|__s: &S| __s.base); + let _lhs = (*self).with(|__s| __s.base); _lhs + (*(*p.upgrade().deref()).0.borrow()) }; _lhs + ((*q.borrow()).read()) @@ -315,29 +315,29 @@ impl SImpl for Ptr { } fn width_1_char(&self, x: i32) -> i32 { let x: Value = Rc::new(RefCell::new(x)); - return ((*self).with(|__s: &S| __s.base) + return ((*self).with(|__s| __s.base) + ((*x.borrow()) * (::std::mem::size_of::() as i32))); } fn width_1_int(&self, x: i32) -> i32 { let x: Value = Rc::new(RefCell::new(x)); - return ((*self).with(|__s: &S| __s.base) + return ((*self).with(|__s| __s.base) + ((*x.borrow()) * (::std::mem::size_of::() as i32))); } fn scale_2_2(&self, x: i32) -> i32 { let x: Value = Rc::new(RefCell::new(x)); - return ((*self).with(|__s: &S| __s.base) + ((*x.borrow()) * 2)); + return ((*self).with(|__s| __s.base) + ((*x.borrow()) * 2)); } fn scale_2_3(&self, x: i32) -> i32 { let x: Value = Rc::new(RefCell::new(x)); - return ((*self).with(|__s: &S| __s.base) + ((*x.borrow()) * 3)); + return ((*self).with(|__s| __s.base) + ((*x.borrow()) * 3)); } fn count_3(&self, x: i32) -> i32 { let x: Value = Rc::new(RefCell::new(x)); - return (((*self).with(|__s: &S| __s.base) + (*x.borrow())) + (0 as i32)); + return (((*self).with(|__s| __s.base) + (*x.borrow())) + (0 as i32)); } fn count_3_int_long(&self, x: i32) -> i32 { let x: Value = Rc::new(RefCell::new(x)); - return (((*self).with(|__s: &S| __s.base) + (*x.borrow())) + (2 as i32)); + return (((*self).with(|__s| __s.base) + (*x.borrow())) + (2 as i32)); } } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/pod.rs b/tests/unit/out/refcount/pod.rs index 89e7a78bd9..2c6fcc7c08 100644 --- a/tests/unit/out/refcount/pod.rs +++ b/tests/unit/out/refcount/pod.rs @@ -33,9 +33,18 @@ impl ByteRepr for POD { } } pub fn PODIncrement_0(pod: Ptr) { - pod.with_mut(|__s: &mut POD| __s.x1 += 1); - pod.with_mut(|__s: &mut POD| __s.x2 += 2); - pod.with_mut(|__s: &mut POD| __s.x3 += 3); + { + let _ptr = field!(pod, x1); + _ptr.write(_ptr.read() + 1) + }; + { + let _ptr = field!(pod, x2); + _ptr.write(_ptr.read() + 2) + }; + { + let _ptr = field!(pod, x3); + _ptr.write(_ptr.read() + 3) + }; } pub fn main() { __cpp2rust_init_globals(); diff --git a/tests/unit/out/refcount/pointer_array.rs b/tests/unit/out/refcount/pointer_array.rs index ed4e0954b6..47ef01d81e 100644 --- a/tests/unit/out/refcount/pointer_array.rs +++ b/tests/unit/out/refcount/pointer_array.rs @@ -13,13 +13,9 @@ pub struct StackArray { } impl Clone for StackArray { fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - arr: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 3, _>( - |__i: usize| ((*self.arr.borrow())[(__i) as usize]).clone(), - )))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() + Self { + arr: Rc::new(RefCell::new((*self.arr.borrow()).clone())), + } } } impl Default for StackArray { @@ -50,9 +46,7 @@ pub fn IncrementAll_0(s: Ptr) { let i: Value = Rc::new(RefCell::new(0)); 'loop_: while ((*i.borrow()) < 3) { { - let _ptr = (*s.with(|__s: &StackArray| __s.arr.clone()).borrow()) - [(*i.borrow()) as usize] - .clone(); + let _ptr = (*s.with(|__s| __s.arr.clone()).borrow())[(*i.borrow()) as usize].clone(); _ptr.write(_ptr.read() + 1) }; (*i.borrow_mut()).prefix_inc(); diff --git a/tests/unit/out/refcount/pointers.rs b/tests/unit/out/refcount/pointers.rs index 31a1306d53..8ffd59253a 100644 --- a/tests/unit/out/refcount/pointers.rs +++ b/tests/unit/out/refcount/pointers.rs @@ -30,8 +30,8 @@ pub fn Update_0(t: Ptr) -> Ptr { let y: Value = Rc::new(RefCell::new(2)); (*x.borrow_mut()).prefix_inc(); ({ TestImpl::update(&(*t.borrow()), (*x.borrow()), (*y.borrow())) }); - (*x.borrow_mut()) = (*t.borrow()).with(|__s: &Test| __s.x); - (*y.borrow_mut()) = (*t.borrow()).with(|__s: &Test| __s.x); + (*x.borrow_mut()) = (*t.borrow()).with(|__s| (__s).x); + (*y.borrow_mut()) = (*t.borrow()).with(|__s| __s.x); ({ let _x: i32 = (*x.borrow()); let _y: i32 = (*y.borrow()); @@ -48,7 +48,7 @@ fn main_0() -> i32 { let t2: Value> = Rc::new(RefCell::new(({ Update_0((t1.as_pointer())) }))); let t3: Value> = Rc::new(RefCell::new(Ptr::::null())); (*t3.borrow_mut()) = (*t2.borrow()).clone(); - (*t3.borrow()).with_mut(|__s: &mut Test| __s.x = 15); + field!((*t3.borrow()), x).write(15); { let _ptr = ({ TestImpl::as_ptr(&(*t3.borrow())) }); _ptr.write(_ptr.read() + 10) @@ -56,8 +56,8 @@ fn main_0() -> i32 { assert!( ({ let _lhs = { - let _lhs = (*t3.borrow()).with(|__s: &Test| __s.x); - _lhs + (*t2.borrow()).with(|__s: &Test| __s.x) + let _lhs = (*t3.borrow()).with(|__s| __s.x); + _lhs + (*t2.borrow()).with(|__s| __s.x) }; _lhs + { (*t1.borrow()).x } } == 75) @@ -72,10 +72,10 @@ pub trait TestImpl { } impl TestImpl for Ptr { fn inc(&self) { - (*self).with_mut(|__s: &mut Test| __s.x.postfix_inc()); + field!((*self), x).with_mut(|__v| __v.postfix_inc()); } fn dec(&self) { - (*self).with_mut(|__s: &mut Test| __s.x.postfix_dec()); + field!((*self), x).with_mut(|__v| __v.postfix_dec()); } fn as_ptr(&self) -> Ptr { return (field_ptr!((*self), x)); @@ -83,8 +83,7 @@ impl TestImpl for Ptr { fn update(&self, x: i32, y: i32) { let x: Value = Rc::new(RefCell::new(x)); let y: Value = Rc::new(RefCell::new(y)); - let __rhs = ((*x.borrow()) + (*y.borrow())); - (*self).with_mut(|__s: &mut Test| __s.x = __rhs); + field!((*self), x).write(((*x.borrow()) + (*y.borrow()))); } } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/poll.rs b/tests/unit/out/refcount/poll.rs index 16c7c88abc..366297c714 100644 --- a/tests/unit/out/refcount/poll.rs +++ b/tests/unit/out/refcount/poll.rs @@ -48,12 +48,12 @@ fn main_0() -> i32 { .map(|_| Default::default()) .collect::>(), )); - (*(*pfd.borrow())[(0) as usize].fd.borrow_mut()) = (*fds.borrow())[(0) as usize]; - (*(*pfd.borrow())[(0) as usize].events.borrow_mut()) = 1_i16; - (*(*pfd.borrow())[(0) as usize].revents.borrow_mut()) = 0_i16; - (*(*pfd.borrow())[(1) as usize].fd.borrow_mut()) = -1_i32; - (*(*pfd.borrow())[(1) as usize].events.borrow_mut()) = 1_i16; - (*(*pfd.borrow())[(1) as usize].revents.borrow_mut()) = 42_i16; + (*pfd.borrow_mut())[(0) as usize].fd = (*fds.borrow())[(0) as usize]; + (*pfd.borrow_mut())[(0) as usize].events = 1_i16; + (*pfd.borrow_mut())[(0) as usize].revents = 0_i16; + (*pfd.borrow_mut())[(1) as usize].fd = -1_i32; + (*pfd.borrow_mut())[(1) as usize].events = 1_i16; + (*pfd.borrow_mut())[(1) as usize].revents = 42_i16; assert!( ((({ let __p = (pfd.as_pointer() as Ptr); @@ -65,11 +65,8 @@ fn main_0() -> i32 { let mut __wanted = Vec::new(); let mut __events = Vec::new(); for __i in 0..((2 as ::libc::nfds_t) as usize) { - let (__fd, __ev) = __p - .offset(__i) - .with(|__e| (*__e.fd.borrow(), *__e.events.borrow())); - __p.offset(__i) - .with_mut(|__e| *__e.revents.borrow_mut() = 0); + let (__fd, __ev) = __p.offset(__i).with(|__e| (__e.fd, __e.events)); + __p.offset(__i).with_mut(|__e| __e.revents = 0); if __fd >= 0 { __idx.push(__i); __wanted.push(__fd); @@ -91,8 +88,7 @@ fn main_0() -> i32 { Some(__r) => __r.bits(), None => 0, }; - __p.offset(__i) - .with_mut(|__e| *__e.revents.borrow_mut() = __rev); + __p.offset(__i).with_mut(|__e| __e.revents = __rev); } __count } @@ -105,10 +101,8 @@ fn main_0() -> i32 { } == 1) as i32) != 0) ); - assert!( - ((((((*(*pfd.borrow())[(0) as usize].revents.borrow()) as i32) & 1) != 0) as i32) != 0) - ); - assert!((((((*(*pfd.borrow())[(1) as usize].revents.borrow()) as i32) == 0) as i32) != 0)); + assert!(((((({ (*pfd.borrow())[(0) as usize].revents } as i32) & 1) != 0) as i32) != 0)); + assert!((((({ (*pfd.borrow())[(1) as usize].revents } as i32) == 0) as i32) != 0)); let ch: Value = Rc::new(RefCell::new(0_u8)); assert!( (((match FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { diff --git a/tests/unit/out/refcount/ptr_cast_init.rs b/tests/unit/out/refcount/ptr_cast_init.rs index 4307f673b2..fa02c949a0 100644 --- a/tests/unit/out/refcount/ptr_cast_init.rs +++ b/tests/unit/out/refcount/ptr_cast_init.rs @@ -59,7 +59,7 @@ fn main_0() -> i32 { let h: Value
= Rc::new(RefCell::new(header { tag: 7, size: 32 })); let hp: Value> = Rc::new(RefCell::new((h.as_pointer()))); let v: Value> = Rc::new(RefCell::new((*hp.borrow()).reinterpret_cast::())); - assert!(((((*v.borrow()).with(|__s: &view| __s.tag) == 7) as i32) != 0)); + assert!(((((*v.borrow()).with(|__s| __s.tag) == 7) as i32) != 0)); let data: Value> = Rc::new(RefCell::new(Box::from(*b"hi\0"))); let vp: Value = Rc::new(RefCell::new( ((data.as_pointer() as Ptr) as Ptr).to_any(), diff --git a/tests/unit/out/refcount/push_emplace_back.rs b/tests/unit/out/refcount/push_emplace_back.rs index 2b8384e552..cb2f3fd52d 100644 --- a/tests/unit/out/refcount/push_emplace_back.rs +++ b/tests/unit/out/refcount/push_emplace_back.rs @@ -55,26 +55,10 @@ pub struct JPEGData { } impl Clone for JPEGData { fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - com_data: { - Rc::new(RefCell::new( - (*self.com_data.borrow()) - .iter() - .map(|inner_vec| Rc::new(RefCell::new(inner_vec.borrow().clone()))) - .collect(), - )) - }, - app_data: { - Rc::new(RefCell::new( - (*self.app_data.borrow()) - .iter() - .map(|inner_vec| Rc::new(RefCell::new(inner_vec.borrow().clone()))) - .collect(), - )) - }, - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() + Self { + com_data: Rc::new(RefCell::new((*self.com_data.borrow()).clone())), + app_data: Rc::new(RefCell::new((*self.app_data.borrow()).clone())), + } } } <<<<<<< HEAD @@ -110,9 +94,13 @@ pub fn push_local_from_field_1(jpg: Ptr, cond: bool) { let dest: Value>>>> = Rc::new(RefCell::new(Ptr::>>>::null())); if (*cond.borrow()) { - (*dest.borrow_mut()) = ((*jpg.borrow()).with(|__s: &JPEGData| __s.com_data.as_pointer())); + (*dest.borrow_mut()) = ((*jpg.borrow()) + .with(|__s| __s.com_data.clone()) + .as_pointer()); } else { - (*dest.borrow_mut()) = ((*jpg.borrow()).with(|__s: &JPEGData| __s.app_data.as_pointer())); + (*dest.borrow_mut()) = ((*jpg.borrow()) + .with(|__s| __s.app_data.clone()) + .as_pointer()); } ((*dest.borrow()).clone() as Ptr>>>).with_mut( |__v: &mut Vec>>| { @@ -135,7 +123,7 @@ pub fn shrink_through_ptr_2(comps: Ptr>) { pub fn nested_push_move_3(bw: Ptr) { let bw: Value> = Rc::new(RefCell::new(bw)); (*bw.borrow()) - .with(|__s: &Writer| (__s.output).clone()) + .with(|__s| __s.output.clone()) .with_mut(|__v: &mut Vec| { __v.push(((*(*bw.borrow()).upgrade().deref()).chunk).clone()) }); @@ -147,9 +135,13 @@ pub fn emplace_local_from_field_4(jpg: Ptr, cond: bool) { let dest: Value>>>> = Rc::new(RefCell::new(Ptr::>>>::null())); if (*cond.borrow()) { - (*dest.borrow_mut()) = ((*jpg.borrow()).with(|__s: &JPEGData| __s.com_data.as_pointer())); + (*dest.borrow_mut()) = ((*jpg.borrow()) + .with(|__s| __s.com_data.clone()) + .as_pointer()); } else { - (*dest.borrow_mut()) = ((*jpg.borrow()).with(|__s: &JPEGData| __s.app_data.as_pointer())); + (*dest.borrow_mut()) = ((*jpg.borrow()) + .with(|__s| __s.app_data.clone()) + .as_pointer()); } { let __init = { @@ -170,7 +162,7 @@ pub fn nested_emplace_move_5(bw: Ptr) { { let __init = ((*(*bw.borrow()).upgrade().deref()).chunk).clone(); (*bw.borrow()) - .with(|__s: &Writer| (__s.output).clone()) + .with(|__s| __s.output.clone()) .with_mut(|__v: &mut Vec| __v.push(__init)) }; } @@ -298,9 +290,9 @@ fn main_0() -> i32 { ); let jpg: Value = Rc::new(RefCell::new(::default())); ({ push_local_from_field_1((jpg.as_pointer()), true) }); - assert!(((*(*jpg.borrow()).com_data.borrow()).len() == 1_usize)); + assert!(((*{ (*jpg.borrow()).com_data.clone() }.borrow()).len() == 1_usize)); assert!( - ((*(({ (*jpg.borrow()).com_data.as_pointer() } as Ptr>>) + ((*(({ (*jpg.borrow()).com_data.clone() }.as_pointer() as Ptr>>) .offset(0_usize) .upgrade() .deref() @@ -311,7 +303,7 @@ fn main_0() -> i32 { == 3_usize) ); assert!( - ((((({ (*jpg.borrow()).com_data.as_pointer() } as Ptr>>) + ((((({ (*jpg.borrow()).com_data.clone() }.as_pointer() as Ptr>>) .offset(0_usize) .upgrade() .deref() @@ -321,7 +313,7 @@ fn main_0() -> i32 { == 1) ); assert!( - ((((({ (*jpg.borrow()).com_data.as_pointer() } as Ptr>>) + ((((({ (*jpg.borrow()).com_data.clone() }.as_pointer() as Ptr>>) .offset(0_usize) .upgrade() .deref() @@ -331,7 +323,7 @@ fn main_0() -> i32 { == 2) ); assert!( - ((((({ (*jpg.borrow()).com_data.as_pointer() } as Ptr>>) + ((((({ (*jpg.borrow()).com_data.clone() }.as_pointer() as Ptr>>) .offset(0_usize) .upgrade() .deref() @@ -340,7 +332,7 @@ fn main_0() -> i32 { .read()) as i32) == 3) ); - assert!((*(*jpg.borrow()).app_data.borrow()).is_empty()); + assert!((*{ (*jpg.borrow()).app_data.clone() }.borrow()).is_empty()); let chunks: Value> = Rc::new(RefCell::new(Vec::new())); ({ shrink_through_ptr_2((chunks.as_pointer())) }); assert!((*chunks.borrow()).is_empty()); @@ -350,15 +342,18 @@ fn main_0() -> i32 { ({ nested_push_move_3((w.as_pointer())) }); assert!(((*chunks.borrow()).len() == 1_usize)); assert!( - ((chunks.as_pointer() as Ptr) - .offset(0_usize) - .with(|__s: &Chunk| __s.data) - == 42) + ({ + (*(chunks.as_pointer() as Ptr) + .offset(0_usize) + .upgrade() + .deref()) + .data + } == 42) ); ({ emplace_local_from_field_4((jpg.as_pointer()), false) }); - assert!(((*(*jpg.borrow()).app_data.borrow()).len() == 1_usize)); + assert!(((*{ (*jpg.borrow()).app_data.clone() }.borrow()).len() == 1_usize)); assert!( - ((*(({ (*jpg.borrow()).app_data.as_pointer() } as Ptr>>) + ((*(({ (*jpg.borrow()).app_data.clone() }.as_pointer() as Ptr>>) .offset(0_usize) .upgrade() .deref() @@ -369,7 +364,7 @@ fn main_0() -> i32 { == 3_usize) ); assert!( - ((((({ (*jpg.borrow()).app_data.as_pointer() } as Ptr>>) + ((((({ (*jpg.borrow()).app_data.clone() }.as_pointer() as Ptr>>) .offset(0_usize) .upgrade() .deref() @@ -379,7 +374,7 @@ fn main_0() -> i32 { == 1) ); assert!( - ((((({ (*jpg.borrow()).app_data.as_pointer() } as Ptr>>) + ((((({ (*jpg.borrow()).app_data.clone() }.as_pointer() as Ptr>>) .offset(0_usize) .upgrade() .deref() @@ -388,72 +383,96 @@ fn main_0() -> i32 { .read()) as i32) == 3) ); - assert!(((*(*jpg.borrow()).com_data.borrow()).len() == 1_usize)); + assert!(((*{ (*jpg.borrow()).com_data.clone() }.borrow()).len() == 1_usize)); (*w.borrow_mut()).chunk.data = 99; (*w.borrow_mut()).output = (chunks.as_pointer()); ({ nested_emplace_move_5((w.as_pointer())) }); assert!(((*chunks.borrow()).len() == 2_usize)); assert!( - ((chunks.as_pointer() as Ptr) - .offset(1_usize) - .with(|__s: &Chunk| __s.data) - == 99) + ({ + (*(chunks.as_pointer() as Ptr) + .offset(1_usize) + .upgrade() + .deref()) + .data + } == 99) ); ({ self_ref_push_6((chunks.as_pointer())) }); assert!(((*chunks.borrow()).len() == 3_usize)); assert!( - ((chunks.as_pointer() as Ptr) - .offset(2_usize) - .with(|__s: &Chunk| __s.data) - == 42) + ({ + (*(chunks.as_pointer() as Ptr) + .offset(2_usize) + .upgrade() + .deref()) + .data + } == 42) ); let pairs: Value> = Rc::new(RefCell::new(Vec::new())); ({ emplace_ctor_args_7((pairs.as_pointer())) }); assert!(((*pairs.borrow()).len() == 3_usize)); assert!( - ((pairs.as_pointer() as Ptr) - .offset(0_usize) - .with(|__s: &Pair| __s.first) - == -1_i32) - && ((pairs.as_pointer() as Ptr) + ({ + (*(pairs.as_pointer() as Ptr) .offset(0_usize) - .with(|__s: &Pair| __s.second) - == -1_i32) + .upgrade() + .deref()) + .first + } == -1_i32) + && ({ + (*(pairs.as_pointer() as Ptr) + .offset(0_usize) + .upgrade() + .deref()) + .second + } == -1_i32) ); assert!( - ((pairs.as_pointer() as Ptr) - .offset(1_usize) - .with(|__s: &Pair| __s.first) - == 3) - && ((pairs.as_pointer() as Ptr) + ({ + (*(pairs.as_pointer() as Ptr) .offset(1_usize) - .with(|__s: &Pair| __s.second) - == 0) + .upgrade() + .deref()) + .first + } == 3) + && ({ + (*(pairs.as_pointer() as Ptr) + .offset(1_usize) + .upgrade() + .deref()) + .second + } == 0) ); assert!( - ((pairs.as_pointer() as Ptr) - .offset(2_usize) - .with(|__s: &Pair| __s.first) - == 4) - && ((pairs.as_pointer() as Ptr) + ({ + (*(pairs.as_pointer() as Ptr) .offset(2_usize) - .with(|__s: &Pair| __s.second) - == 10) + .upgrade() + .deref()) + .first + } == 4) + && ({ + (*(pairs.as_pointer() as Ptr) + .offset(2_usize) + .upgrade() + .deref()) + .second + } == 10) ); let queue: Value> = Rc::new(RefCell::new(Vec::new())); ({ emplace_deque_8((queue.as_pointer())) }); assert!( - ((queue.as_pointer() as Ptr).with(|__s: &Pair| __s.first) == 6) - && ((queue.as_pointer() as Ptr).with(|__s: &Pair| __s.second) == 14) + ((queue.as_pointer() as Ptr).with(|__s| __s.first) == 6) + && ((queue.as_pointer() as Ptr).with(|__s| __s.second) == 14) ); assert!( ((queue.as_pointer() as Ptr) .to_last() - .with(|__s: &Pair| __s.first) + .with(|__s| __s.first) == -1_i32) && ((queue.as_pointer() as Ptr) .to_last() - .with(|__s: &Pair| __s.second) + .with(|__s| __s.second) == -1_i32) ); let values: Value> = Rc::new(RefCell::new(Vec::new())); diff --git a/tests/unit/out/refcount/pwd.rs b/tests/unit/out/refcount/pwd.rs index 3a03db1542..8d4bf8b659 100644 --- a/tests/unit/out/refcount/pwd.rs +++ b/tests/unit/out/refcount/pwd.rs @@ -22,23 +22,21 @@ pub fn test_getpwuid_0() { assert!((((!((*pw.borrow()).is_null())) as i32) != 0)); assert!( ((({ - let _lhs = (*(*(*pw.borrow()).upgrade().deref()).pw_uid.borrow()); + let _lhs = (*pw.borrow()).with(|__s| __s.pw_uid); _lhs == nix::unistd::geteuid().as_raw() }) as i32) != 0) ); assert!( - ((((*(*(*pw.borrow()).upgrade().deref()).pw_name.borrow()) + ((((*pw.borrow()) + .with(|__s| __s.pw_name.clone()) .to_c_string_iterator() .count() > 0_usize) as i32) != 0) ); - assert!((((!((*(*(*pw.borrow()).upgrade().deref()).pw_dir.borrow()).is_null())) as i32) != 0)); - println!( - "{}", - (*(*(*pw.borrow()).upgrade().deref()).pw_name.borrow()) - ); + assert!((((!(((*pw.borrow()).with(|__s| __s.pw_dir.clone())).is_null())) as i32) != 0)); + println!("{}", (*pw.borrow()).with(|__s| __s.pw_name.clone())); } pub fn test_getpwuid_missing_1() { libcc2rs::cpp2rust_errno().write(0); @@ -118,9 +116,9 @@ pub fn test_getpwuid_r_2() { }) as i32) != 0) ); - assert!(((((*(*pw.borrow()).pw_uid.borrow()) == nix::unistd::geteuid().as_raw()) as i32) != 0)); + assert!(((({ (*pw.borrow()).pw_uid } == nix::unistd::geteuid().as_raw()) as i32) != 0)); assert!( - ((((*(*pw.borrow()).pw_name.borrow()) + ((({ (*pw.borrow()).pw_name.clone() } .to_c_string_iterator() .count() > 0_usize) as i32) @@ -141,9 +139,10 @@ pub fn test_getpwuid_r_2() { assert!((((!((*pw2.borrow()).is_null())) as i32) != 0)); assert!( ((({ - let mut __it1 = (*(*pw.borrow()).pw_name.borrow()).to_c_string_iterator(); - let mut __it2 = - (*(*(*pw2.borrow()).upgrade().deref()).pw_name.borrow()).to_c_string_iterator(); + let mut __it1 = { (*pw.borrow()).pw_name.clone() }.to_c_string_iterator(); + let mut __it2 = (*pw2.borrow()) + .with(|__s| __s.pw_name.clone()) + .to_c_string_iterator(); loop { let __c1 = __it1.next(); let __c2 = __it2.next(); @@ -157,7 +156,7 @@ pub fn test_getpwuid_r_2() { } == 0) as i32) != 0) ); - println!("{}", (*(*pw.borrow()).pw_name.borrow())); + println!("{}", { (*pw.borrow()).pw_name.clone() }); } pub fn test_getpwuid_r_erange_3() { let pw: Value = Rc::new(RefCell::new(Default::default())); diff --git a/tests/unit/out/refcount/random.rs b/tests/unit/out/refcount/random.rs index 0138ee42e6..c61cd6f894 100644 --- a/tests/unit/out/refcount/random.rs +++ b/tests/unit/out/refcount/random.rs @@ -25,21 +25,15 @@ pub struct Pair { } impl Clone for Pair { fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - x: { self.x }, - y: { self.y }, - a: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 5, _>( - |__i: usize| (*self.a.borrow())[(__i) as usize], - )))), - r: { (self.r).clone() }, - p: { self.p.clone() }, - pair: { self.pair.clone() }, - ap: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 2, _>( - |__i: usize| ((*self.ap.borrow())[(__i) as usize]).clone(), - )))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() + Self { + x: self.x.clone(), + y: self.y.clone(), + a: Rc::new(RefCell::new((*self.a.borrow()).clone())), + r: self.r.clone(), + p: self.p.clone(), + pair: self.pair.clone(), + ap: Rc::new(RefCell::new((*self.ap.borrow()).clone())), + } } } impl Default for Pair { @@ -106,72 +100,60 @@ fn main_0() -> i32 { let y4: Value = Rc::new(RefCell::new(Pair { x: { (*y1.borrow()).x }, y: { (*y1.borrow()).y }, - a: { - Rc::new(RefCell::new(Box::new([ - (*(*y1.borrow()).a.borrow())[(0) as usize], - (*(*y1.borrow()).a.borrow())[(1) as usize], - (*(*y1.borrow()).a.borrow())[(2) as usize], - (*(*y1.borrow()).a.borrow())[(3) as usize], - (*(*y1.borrow()).a.borrow())[(4) as usize], - ]))) - }, - r: { ((*y1.borrow()).r).clone() }, + a: Rc::new(RefCell::new(Box::new([ + (*{ (*y1.borrow()).a.clone() }.borrow())[(0) as usize], + (*{ (*y1.borrow()).a.clone() }.borrow())[(1) as usize], + (*{ (*y1.borrow()).a.clone() }.borrow())[(2) as usize], + (*{ (*y1.borrow()).a.clone() }.borrow())[(3) as usize], + (*{ (*y1.borrow()).a.clone() }.borrow())[(4) as usize], + ]))), + r: ({ (*y1.borrow()).r.clone() }).clone(), p: { (*y1.borrow()).p.clone() }, pair: { (*y1.borrow()).pair.clone() }, - ap: { - Rc::new(RefCell::new(Box::new([ - ((*(*y1.borrow()).ap.borrow())[(0) as usize]).clone(), - ((*(*y1.borrow()).ap.borrow())[(1) as usize]).clone(), - ]))) - }, + ap: Rc::new(RefCell::new(Box::new([ + ((*{ (*y1.borrow()).ap.clone() }.borrow())[(0) as usize]).clone(), + ((*{ (*y1.borrow()).ap.clone() }.borrow())[(1) as usize]).clone(), + ]))), })); let ry1: Ptr = y1.as_pointer(); let py1: Value> = Rc::new(RefCell::new((y1.as_pointer()))); let y2: Value = Rc::new(RefCell::new(Pair { - x: { ry1.with(|__s: &Pair| __s.x) }, - y: { ry1.with(|__s: &Pair| __s.y) }, - a: { - Rc::new(RefCell::new(Box::new([ - ry1.with(|__s: &Pair| (*__s.a.borrow())[(0) as usize]), - ry1.with(|__s: &Pair| (*__s.a.borrow())[(1) as usize]), - ry1.with(|__s: &Pair| (*__s.a.borrow())[(2) as usize]), - ry1.with(|__s: &Pair| (*__s.a.borrow())[(3) as usize]), - ry1.with(|__s: &Pair| (*__s.a.borrow())[(4) as usize]), - ]))) - }, - r: { ((*ry1.upgrade().deref()).r).clone() }, - p: { ry1.with(|__s: &Pair| (__s.p).clone()) }, - pair: { ry1.with(|__s: &Pair| (__s.pair).clone()) }, - ap: { - Rc::new(RefCell::new(Box::new([ - ry1.with(|__s: &Pair| ((*__s.ap.borrow())[(0) as usize]).clone()), - ry1.with(|__s: &Pair| ((*__s.ap.borrow())[(1) as usize]).clone()), - ]))) - }, + x: ry1.with(|__s| __s.x), + y: ry1.with(|__s| __s.y), + a: Rc::new(RefCell::new(Box::new([ + (*ry1.with(|__s| __s.a.clone()).borrow())[(0) as usize], + (*ry1.with(|__s| __s.a.clone()).borrow())[(1) as usize], + (*ry1.with(|__s| __s.a.clone()).borrow())[(2) as usize], + (*ry1.with(|__s| __s.a.clone()).borrow())[(3) as usize], + (*ry1.with(|__s| __s.a.clone()).borrow())[(4) as usize], + ]))), + r: (ry1.with(|__s| __s.r.clone())).clone(), + p: ry1.with(|__s| __s.p.clone()), + pair: ry1.with(|__s| __s.pair.clone()), + ap: Rc::new(RefCell::new(Box::new([ + ((*ry1.with(|__s| __s.ap.clone()).borrow())[(0) as usize]).clone(), + ((*ry1.with(|__s| __s.ap.clone()).borrow())[(1) as usize]).clone(), + ]))), })); let ry2: Ptr = (ry1).clone(); let py2: Value> = Rc::new(RefCell::new((ry1).clone())); let y3: Value = Rc::new(RefCell::new(Pair { - x: { (*py1.borrow()).with(|__s: &Pair| __s.x) }, - y: { (*py1.borrow()).with(|__s: &Pair| __s.y) }, - a: { - Rc::new(RefCell::new(Box::new([ - (*py1.borrow()).with(|__s: &Pair| (*__s.a.borrow())[(0) as usize]), - (*py1.borrow()).with(|__s: &Pair| (*__s.a.borrow())[(1) as usize]), - (*py1.borrow()).with(|__s: &Pair| (*__s.a.borrow())[(2) as usize]), - (*py1.borrow()).with(|__s: &Pair| (*__s.a.borrow())[(3) as usize]), - (*py1.borrow()).with(|__s: &Pair| (*__s.a.borrow())[(4) as usize]), - ]))) - }, - r: { ((*(*py1.borrow()).upgrade().deref()).r).clone() }, - p: { (*py1.borrow()).with(|__s: &Pair| (__s.p).clone()) }, - pair: { (*py1.borrow()).with(|__s: &Pair| (__s.pair).clone()) }, - ap: { - Rc::new(RefCell::new(Box::new([ - (*py1.borrow()).with(|__s: &Pair| ((*__s.ap.borrow())[(0) as usize]).clone()), - (*py1.borrow()).with(|__s: &Pair| ((*__s.ap.borrow())[(1) as usize]).clone()), - ]))) - }, + x: (*py1.borrow()).with(|__s| __s.x), + y: (*py1.borrow()).with(|__s| __s.y), + a: Rc::new(RefCell::new(Box::new([ + (*(*py1.borrow()).with(|__s| __s.a.clone()).borrow())[(0) as usize], + (*(*py1.borrow()).with(|__s| __s.a.clone()).borrow())[(1) as usize], + (*(*py1.borrow()).with(|__s| __s.a.clone()).borrow())[(2) as usize], + (*(*py1.borrow()).with(|__s| __s.a.clone()).borrow())[(3) as usize], + (*(*py1.borrow()).with(|__s| __s.a.clone()).borrow())[(4) as usize], + ]))), + r: ((*py1.borrow()).with(|__s| __s.r.clone())).clone(), + p: (*py1.borrow()).with(|__s| __s.p.clone()), + pair: (*py1.borrow()).with(|__s| __s.pair.clone()), + ap: Rc::new(RefCell::new(Box::new([ + ((*(*py1.borrow()).with(|__s| __s.ap.clone()).borrow())[(0) as usize]).clone(), + ((*(*py1.borrow()).with(|__s| __s.ap.clone()).borrow())[(1) as usize]).clone(), + ]))), })); let ry3: Ptr = (*py1.borrow()).clone(); let py3: Value> = Rc::new(RefCell::new((*py1.borrow()).clone())); @@ -226,20 +208,21 @@ fn main_0() -> i32 { (*px3.borrow_mut()) = (*cp1.borrow()).clone(); (*y1.borrow_mut()).x = 2; (*y1.borrow_mut()).y = 3; - (*(*y1.borrow()).a.borrow_mut())[(0) as usize] = 100; + (*{ (*y1.borrow()).a.clone() }.borrow_mut())[(0) as usize] = 100; { (*y1.borrow()).r.clone() }.write(10); (*y1.borrow_mut()).p = (*px3.borrow()).clone(); (*px3.borrow_mut()) = (*px2.borrow()).clone(); (*y1.borrow_mut()).pair = (y3.as_pointer()); - { (*y1.borrow()).pair.clone() }.with_mut(|__s: &mut Pair| __s.x = 100); - let __rhs = (y2.as_pointer()); - { (*y1.borrow()).pair.clone() }.with_mut(|__s: &mut Pair| __s.pair = __rhs); - { (*y1.borrow()).pair.clone() } - .with(|__s: &Pair| (__s.pair).clone()) - .with_mut(|__s: &mut Pair| __s.x = 100); - (*(*y1.borrow()).ap.borrow_mut())[(0) as usize] = (x1.as_pointer()); - (*(*y1.borrow()).ap.borrow_mut())[(1) as usize] = (x2.as_pointer()); - { (*(*y1.borrow()).ap.borrow())[(0) as usize].clone() }.write(0); + field!({ (*y1.borrow()).pair.clone() }, x).write(100); + field!({ (*y1.borrow()).pair.clone() }, pair).write((y2.as_pointer())); + field!( + { (*y1.borrow()).pair.clone() }.with(|__s| (__s).pair.clone()), + x + ) + .write(100); + (*{ (*y1.borrow()).ap.clone() }.borrow_mut())[(0) as usize] = (x1.as_pointer()); + (*{ (*y1.borrow()).ap.clone() }.borrow_mut())[(1) as usize] = (x2.as_pointer()); + (*{ (*y1.borrow()).ap.clone() }.borrow())[(0) as usize].write(0); (*c1.borrow_mut()) = ((*x1.borrow()) + 1); let j: Value = Rc::new(RefCell::new(0)); let new_y: Value = Rc::new(RefCell::new(Pair { @@ -257,21 +240,20 @@ fn main_0() -> i32 { let __rhs = { (*new_y.borrow()).x }; (*y1.borrow_mut()).x = __rhs; let i: Value = Rc::new(RefCell::new(1_u32)); - (*(*y1.borrow()).a.borrow_mut())[(*i.borrow()) as usize] = -1_i32; + (*{ (*y1.borrow()).a.clone() }.borrow_mut())[(*i.borrow()) as usize] = -1_i32; (*x1.borrow_mut()).postfix_inc(); (*x1.borrow_mut()).prefix_inc(); (*y1.borrow_mut()).x.postfix_inc(); - let __rhs = (y2.as_pointer()); - { (*y1.borrow()).pair.clone() }.with_mut(|__s: &mut Pair| __s.pair = __rhs); - { (*y1.borrow()).pair.clone() } - .with(|__s: &Pair| (__s.pair).clone()) - .with_mut(|__s: &mut Pair| __s.x = 10); + field!({ (*y1.borrow()).pair.clone() }, pair).write((y2.as_pointer())); + field!( + { (*y1.borrow()).pair.clone() }.with(|__s| __s.pair.clone()), + x + ) + .write(10); ({ PairImpl::method(&y1.as_pointer()) }); (*y1.borrow_mut()).pair = (y2.as_pointer()); (*y2.borrow_mut()).pair = (y3.as_pointer()); - ({ - PairImpl::method(&{ { (*y1.borrow()).pair.clone() }.with(|__s: &Pair| (__s.pair).clone()) }) - }); + ({ PairImpl::method(&{ (*y1.borrow()).pair.clone() }.with(|__s| __s.pair.clone())) }); let x: Value = Rc::new(RefCell::new(::default())); let y: Value = Rc::new(RefCell::new(::default())); (*x1.borrow_mut()) = (({ zero_0() }) + { (*y1.borrow()).x }); @@ -288,16 +270,16 @@ pub trait PairImpl { } impl PairImpl for Ptr { fn method(&self) { - (*self).with_mut(|__s: &mut Pair| __s.x.postfix_inc()); - (*self).with_mut(|__s: &mut Pair| __s.y.prefix_inc()); - (*self).with(|__s: &Pair| (*__s.a.borrow_mut())[(4) as usize] = 1); - { (*(*self).upgrade().deref()).r.clone() }.write(1); - (*self).with_mut(|__s: &mut Pair| __s.p = Ptr::::null()); - (*self).with_mut(|__s: &mut Pair| __s.pair = Ptr::::null()); - (*self).with(|__s: &Pair| (*__s.ap.borrow_mut())[(0) as usize] = Ptr::::null()); + field!((*self), x).with_mut(|__v| __v.postfix_inc()); + field!((*self), y).with_mut(|__v| __v.prefix_inc()); + (*(*self).with(|__s| __s.a.clone()).borrow_mut())[(4) as usize] = 1; + (*self).with(|__s| __s.r.clone()).write(1); + field!((*self), p).write(Ptr::::null()); + field!((*self), pair).write(Ptr::::null()); + (*(*self).with(|__s| __s.ap.clone()).borrow_mut())[(0) as usize] = Ptr::::null(); } fn as_val(&self) -> i32 { - return (*self).with(|__s: &Pair| __s.x); + return (*self).with(|__s| __s.x); } fn as_ref(&self) -> Ptr { return field_ptr!((*self), x); diff --git a/tests/unit/out/refcount/reference_wrapper.rs b/tests/unit/out/refcount/reference_wrapper.rs index 772d3e1cc3..4c3222cf7e 100644 --- a/tests/unit/out/refcount/reference_wrapper.rs +++ b/tests/unit/out/refcount/reference_wrapper.rs @@ -73,8 +73,8 @@ fn main_0() -> i32 { ); let point: Value = Rc::new(RefCell::new(Point { x: 3, y: 4 })); let point_ref: Value> = Rc::new(RefCell::new(point.as_pointer())); - (*point_ref.borrow()).with_mut(|__s: &mut Point| __s.x = 30); - (*point_ref.borrow()).with_mut(|__s: &mut Point| __s.y = 40); + field!((*point_ref.borrow()), x).write(30); + field!((*point_ref.borrow()), y).write(40); write!(libcc2rs::cout(), "{:} {:}\n", { (*point.borrow()).x }, { (*point.borrow()).y },); diff --git a/tests/unit/out/refcount/reinterpret_cast_struct_to_struct.rs b/tests/unit/out/refcount/reinterpret_cast_struct_to_struct.rs index 641d9c868a..85f79ab2b5 100644 --- a/tests/unit/out/refcount/reinterpret_cast_struct_to_struct.rs +++ b/tests/unit/out/refcount/reinterpret_cast_struct_to_struct.rs @@ -61,9 +61,9 @@ fn main_0() -> i32 { })); let pair: Value> = Rc::new(RefCell::new((pt.as_pointer()).reinterpret_cast::())); - assert!(((*pair.borrow()).with(|__s: &Pair| __s.first) == 10_u32)); - assert!(((*pair.borrow()).with(|__s: &Pair| __s.second) == 20_u32)); - (*pair.borrow()).with_mut(|__s: &mut Pair| __s.first = 42_u32); + assert!(((*pair.borrow()).with(|__s| __s.first) == 10_u32)); + assert!(((*pair.borrow()).with(|__s| __s.second) == 20_u32)); + field!((*pair.borrow()), first).write(42_u32); assert!(({ (*pt.borrow()).x } == 42_u32)); return 0; } diff --git a/tests/unit/out/refcount/rule_of_five.rs b/tests/unit/out/refcount/rule_of_five.rs index 62a533a9c4..8ea4e4edc6 100644 --- a/tests/unit/out/refcount/rule_of_five.rs +++ b/tests/unit/out/refcount/rule_of_five.rs @@ -37,7 +37,7 @@ impl Buffer { } else { -1_i32 }; - this.with(|__s: &Buffer| (*__s.data.borrow_mut())[(*i.borrow()) as usize] = __rhs); + (*this.with(|__s| __s.data.clone()).borrow_mut())[(*i.borrow()) as usize] = __rhs; (*i.borrow_mut()).prefix_inc(); } (*alive_0.with(Value::clone).borrow_mut()).prefix_inc(); @@ -46,13 +46,13 @@ impl Buffer { pub fn copy_from(o: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { data: Rc::new(RefCell::new((0..4).map(|_| 0_i32).collect::>())), - size: { o.with(|__s: &Buffer| __s.size) }, + size: o.with(|__s| __s.size), })); let this: Ptr = __this.as_pointer(); let i: Value = Rc::new(RefCell::new(0)); 'loop_: while ((*i.borrow()) < 4) { - let __rhs = o.with(|__s: &Buffer| (*__s.data.borrow())[(*i.borrow()) as usize]); - this.with(|__s: &Buffer| (*__s.data.borrow_mut())[(*i.borrow()) as usize] = __rhs); + let __rhs = (*o.with(|__s| __s.data.clone()).borrow())[(*i.borrow()) as usize]; + (*this.with(|__s| __s.data.clone()).borrow_mut())[(*i.borrow()) as usize] = __rhs; (*i.borrow_mut()).prefix_inc(); } (*alive_0.with(Value::clone).borrow_mut()).prefix_inc(); @@ -62,18 +62,17 @@ impl Buffer { pub fn move_from(o: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { data: Rc::new(RefCell::new((0..4).map(|_| 0_i32).collect::>())), - size: { o.with(|__s: &Buffer| __s.size) }, + size: o.with(|__s| __s.size), })); let this: Ptr = __this.as_pointer(); let i: Value = Rc::new(RefCell::new(0)); 'loop_: while ((*i.borrow()) < 4) { - let __rhs = o.with(|__s: &Buffer| (*__s.data.borrow())[(*i.borrow()) as usize]); - this.with(|__s: &Buffer| (*__s.data.borrow_mut())[(*i.borrow()) as usize] = __rhs); - let __rhs = -1_i32; - o.with(|__s: &Buffer| (*__s.data.borrow_mut())[(*i.borrow()) as usize] = __rhs); + let __rhs = (*o.with(|__s| __s.data.clone()).borrow())[(*i.borrow()) as usize]; + (*this.with(|__s| __s.data.clone()).borrow_mut())[(*i.borrow()) as usize] = __rhs; + (*o.with(|__s| __s.data.clone()).borrow_mut())[(*i.borrow()) as usize] = -1_i32; (*i.borrow_mut()).prefix_inc(); } - o.with_mut(|__s: &mut Buffer| __s.size = 0); + field!(o, size).write(0); (*alive_0.with(Value::clone).borrow_mut()).prefix_inc(); (*moves_2.with(Value::clone).borrow_mut()).prefix_inc(); Rc::try_unwrap(__this).ok().unwrap().into_inner() @@ -135,17 +134,18 @@ fn main_0() -> i32 { ((alive_0.with(|rc| *rc.borrow()) == 2) && (copies_1.with(|rc| *rc.borrow()) == 1)) && (moves_2.with(|rc| *rc.borrow()) == 0) ); - (*(*b.borrow()).data.borrow_mut())[(0) as usize] = 100; - assert!(((*(*a.borrow()).data.borrow())[(0) as usize] == 0)); + (*{ (*b.borrow()).data.clone() }.borrow_mut())[(0) as usize] = 100; + assert!(((*{ (*a.borrow()).data.clone() }.borrow())[(0) as usize] == 0)); let c: Value = Rc::new(RefCell::new(Buffer::move_from({ a.as_pointer() }))); let _dtor_c = ScopedDestructor::new(&c, |__p| __p.destructor()); assert!((alive_0.with(|rc| *rc.borrow()) == 3) && (moves_2.with(|rc| *rc.borrow()) == 1)); assert!( ({ (*a.borrow()).size } == 0) - && ((*(*a.borrow()).data.borrow())[(0) as usize] == -1_i32) + && ((*{ (*a.borrow()).data.clone() }.borrow())[(0) as usize] == -1_i32) ); assert!( - ({ (*c.borrow()).size } == 4) && ((*(*c.borrow()).data.borrow())[(3) as usize] == 3) + ({ (*c.borrow()).size } == 4) + && ((*{ (*c.borrow()).data.clone() }.borrow())[(3) as usize] == 3) ); let d: Value = Rc::new(RefCell::new(({ make_3(2) }))); let _dtor_d = ScopedDestructor::new(&d, |__p| __p.destructor()); @@ -153,12 +153,13 @@ fn main_0() -> i32 { ({ BufferImpl::copy_assign(&d.as_pointer(), b.as_pointer()) }); assert!( (({ (*d.borrow()).size } == 4) - && ((*(*d.borrow()).data.borrow())[(0) as usize] == 100)) + && ((*{ (*d.borrow()).data.clone() }.borrow())[(0) as usize] == 100)) && (copies_1.with(|rc| *rc.borrow()) == 2) ); ({ BufferImpl::move_assign(&d.as_pointer(), c.as_pointer()) }); assert!( - (((*(*d.borrow()).data.borrow())[(0) as usize] == 0) && ({ (*c.borrow()).size } == 0)) + (((*{ (*d.borrow()).data.clone() }.borrow())[(0) as usize] == 0) + && ({ (*c.borrow()).size } == 0)) && (moves_2.with(|rc| *rc.borrow()) == 3) ); ({ @@ -183,12 +184,12 @@ impl BufferImpl for Ptr { if ((*self) == (o)) { return (*self).clone(); } - let __rhs = o.with(|__s: &Buffer| __s.size); - (*self).with_mut(|__s: &mut Buffer| __s.size = __rhs); + let __rhs = o.with(|__s| __s.size); + field!((*self), size).write(__rhs); let i: Value = Rc::new(RefCell::new(0)); 'loop_: while ((*i.borrow()) < 4) { - let __rhs = o.with(|__s: &Buffer| (*__s.data.borrow())[(*i.borrow()) as usize]); - (*self).with(|__s: &Buffer| (*__s.data.borrow_mut())[(*i.borrow()) as usize] = __rhs); + let __rhs = (*o.with(|__s| __s.data.clone()).borrow())[(*i.borrow()) as usize]; + (*(*self).with(|__s| __s.data.clone()).borrow_mut())[(*i.borrow()) as usize] = __rhs; (*i.borrow_mut()).prefix_inc(); } (*copies_1.with(Value::clone).borrow_mut()).prefix_inc(); @@ -198,17 +199,16 @@ impl BufferImpl for Ptr { if ((*self) == (o)) { return (*self).clone(); } - let __rhs = o.with(|__s: &Buffer| __s.size); - (*self).with_mut(|__s: &mut Buffer| __s.size = __rhs); + let __rhs = o.with(|__s| __s.size); + field!((*self), size).write(__rhs); let i: Value = Rc::new(RefCell::new(0)); 'loop_: while ((*i.borrow()) < 4) { - let __rhs = o.with(|__s: &Buffer| (*__s.data.borrow())[(*i.borrow()) as usize]); - (*self).with(|__s: &Buffer| (*__s.data.borrow_mut())[(*i.borrow()) as usize] = __rhs); - let __rhs = -1_i32; - o.with(|__s: &Buffer| (*__s.data.borrow_mut())[(*i.borrow()) as usize] = __rhs); + let __rhs = (*o.with(|__s| __s.data.clone()).borrow())[(*i.borrow()) as usize]; + (*(*self).with(|__s| __s.data.clone()).borrow_mut())[(*i.borrow()) as usize] = __rhs; + (*o.with(|__s| __s.data.clone()).borrow_mut())[(*i.borrow()) as usize] = -1_i32; (*i.borrow_mut()).prefix_inc(); } - o.with_mut(|__s: &mut Buffer| __s.size = 0); + field!(o, size).write(0); (*moves_2.with(Value::clone).borrow_mut()).prefix_inc(); return (*self).clone(); } diff --git a/tests/unit/out/refcount/rule_of_three.rs b/tests/unit/out/refcount/rule_of_three.rs index b4268fdcc8..2509659540 100644 --- a/tests/unit/out/refcount/rule_of_three.rs +++ b/tests/unit/out/refcount/rule_of_three.rs @@ -34,7 +34,7 @@ impl Buffer { } else { -1_i32 }; - this.with(|__s: &Buffer| (*__s.data.borrow_mut())[(*i.borrow()) as usize] = __rhs); + (*this.with(|__s| __s.data.clone()).borrow_mut())[(*i.borrow()) as usize] = __rhs; (*i.borrow_mut()).prefix_inc(); } (*alive_0.with(Value::clone).borrow_mut()).prefix_inc(); @@ -43,13 +43,13 @@ impl Buffer { pub fn copy_from(o: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { data: Rc::new(RefCell::new((0..4).map(|_| 0_i32).collect::>())), - size: { o.with(|__s: &Buffer| __s.size) }, + size: o.with(|__s| __s.size), })); let this: Ptr = __this.as_pointer(); let i: Value = Rc::new(RefCell::new(0)); 'loop_: while ((*i.borrow()) < 4) { - let __rhs = o.with(|__s: &Buffer| (*__s.data.borrow())[(*i.borrow()) as usize]); - this.with(|__s: &Buffer| (*__s.data.borrow_mut())[(*i.borrow()) as usize] = __rhs); + let __rhs = (*o.with(|__s| __s.data.clone()).borrow())[(*i.borrow()) as usize]; + (*this.with(|__s| __s.data.clone()).borrow_mut())[(*i.borrow()) as usize] = __rhs; (*i.borrow_mut()).prefix_inc(); } (*alive_0.with(Value::clone).borrow_mut()).prefix_inc(); @@ -98,9 +98,9 @@ pub fn sum_2(b: Ptr) -> i32 { let i: Value = Rc::new(RefCell::new(0)); 'loop_: while { let _lhs = (*i.borrow()); - _lhs < b.with(|__s: &Buffer| __s.size) + _lhs < b.with(|__s| __s.size) } { - (*s.borrow_mut()) += b.with(|__s: &Buffer| (*__s.data.borrow())[(*i.borrow()) as usize]); + (*s.borrow_mut()) += (*b.with(|__s| __s.data.clone()).borrow())[(*i.borrow()) as usize]; (*i.borrow_mut()).prefix_inc(); } return (*s.borrow()); @@ -116,13 +116,14 @@ fn main_0() -> i32 { let b: Value = Rc::new(RefCell::new(Buffer::copy_from({ a.as_pointer() }))); let _dtor_b = ScopedDestructor::new(&b, |__p| __p.destructor()); assert!((alive_0.with(|rc| *rc.borrow()) == 2) && (copies_1.with(|rc| *rc.borrow()) == 1)); - (*(*b.borrow()).data.borrow_mut())[(0) as usize] = 100; - assert!(((*(*a.borrow()).data.borrow())[(0) as usize] == 0)); + (*{ (*b.borrow()).data.clone() }.borrow_mut())[(0) as usize] = 100; + assert!(((*{ (*a.borrow()).data.clone() }.borrow())[(0) as usize] == 0)); let c: Value = Rc::new(RefCell::new(Buffer::new({ 2 }))); let _dtor_c = ScopedDestructor::new(&c, |__p| __p.destructor()); ({ BufferImpl::copy_assign(&c.as_pointer(), a.as_pointer()) }); assert!( - ({ (*c.borrow()).size } == 4) && ((*(*c.borrow()).data.borrow())[(3) as usize] == 3) + ({ (*c.borrow()).size } == 4) + && ((*{ (*c.borrow()).data.clone() }.borrow())[(3) as usize] == 3) ); assert!((alive_0.with(|rc| *rc.borrow()) == 3) && (copies_1.with(|rc| *rc.borrow()) == 2)); ({ @@ -136,13 +137,14 @@ fn main_0() -> i32 { let _dtor_d = ScopedDestructor::new(&d, |__p| __p.destructor()); assert!((alive_0.with(|rc| *rc.borrow()) == 4) && (copies_1.with(|rc| *rc.borrow()) == 3)); assert!( - ({ (*a.borrow()).size } == 4) && ((*(*a.borrow()).data.borrow())[(3) as usize] == 3) + ({ (*a.borrow()).size } == 4) + && ((*{ (*a.borrow()).data.clone() }.borrow())[(3) as usize] == 3) ); ({ BufferImpl::copy_assign(&d.as_pointer(), b.as_pointer()) }); assert!((copies_1.with(|rc| *rc.borrow()) == 4)); assert!( - ((*(*b.borrow()).data.borrow())[(0) as usize] == 100) - && ((*(*d.borrow()).data.borrow())[(0) as usize] == 100) + ((*{ (*b.borrow()).data.clone() }.borrow())[(0) as usize] == 100) + && ((*{ (*d.borrow()).data.clone() }.borrow())[(0) as usize] == 100) ); } assert!((alive_0.with(|rc| *rc.borrow()) == 0)); @@ -160,12 +162,12 @@ impl BufferImpl for Ptr { if ((*self) == (o)) { return (*self).clone(); } - let __rhs = o.with(|__s: &Buffer| __s.size); - (*self).with_mut(|__s: &mut Buffer| __s.size = __rhs); + let __rhs = o.with(|__s| __s.size); + field!((*self), size).write(__rhs); let i: Value = Rc::new(RefCell::new(0)); 'loop_: while ((*i.borrow()) < 4) { - let __rhs = o.with(|__s: &Buffer| (*__s.data.borrow())[(*i.borrow()) as usize]); - (*self).with(|__s: &Buffer| (*__s.data.borrow_mut())[(*i.borrow()) as usize] = __rhs); + let __rhs = (*o.with(|__s| __s.data.clone()).borrow())[(*i.borrow()) as usize]; + (*(*self).with(|__s| __s.data.clone()).borrow_mut())[(*i.borrow()) as usize] = __rhs; (*i.borrow_mut()).prefix_inc(); } (*copies_1.with(Value::clone).borrow_mut()).prefix_inc(); diff --git a/tests/unit/out/refcount/rvalue_struct.rs b/tests/unit/out/refcount/rvalue_struct.rs index 1528244920..57411fb946 100644 --- a/tests/unit/out/refcount/rvalue_struct.rs +++ b/tests/unit/out/refcount/rvalue_struct.rs @@ -47,8 +47,8 @@ pub fn main() { fn main_0() -> i32 { let s1: Value = Rc::new(RefCell::new(S::new({ 1 }, { 2 }))); let s2: Ptr = s1.as_pointer(); - assert!((s2.with(|__s: &S| __s.a) == 1)); - assert!((s2.with(|__s: &S| __s.b) == 2)); + assert!((s2.with(|__s| __s.a) == 1)); + assert!((s2.with(|__s| __s.b) == 2)); return 0; } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/select.rs b/tests/unit/out/refcount/select.rs index 0660112fb3..f5e75d55c3 100644 --- a/tests/unit/out/refcount/select.rs +++ b/tests/unit/out/refcount/select.rs @@ -38,7 +38,7 @@ fn main_0() -> i32 { .memset((0) as u8, 16usize as usize); ((tv.as_pointer()) as Ptr).to_any() }; - (*(*tv.borrow()).tv_sec.borrow_mut()) = 0_i64; + (*tv.borrow_mut()).tv_sec = 0_i64; assert!( ((({ let __rp = (rset.as_pointer()); @@ -91,10 +91,7 @@ fn main_0() -> i32 { let mut __tv = match __tp.is_null() { true => None, false => Some(__tp.with(|__t| { - nix::sys::time::TimeVal::new( - *__t.tv_sec.borrow() as _, - *__t.tv_usec.borrow() as _, - ) + nix::sys::time::TimeVal::new(__t.tv_sec as _, __t.tv_usec as _) })), }; match nix::sys::select::select( @@ -146,8 +143,8 @@ fn main_0() -> i32 { } match (__tp.is_null(), __tv.as_ref()) { (false, Some(__t)) => __tp.with_mut(|__dst| { - *__dst.tv_sec.borrow_mut() = __t.tv_sec() as i64; - *__dst.tv_usec.borrow_mut() = __t.tv_usec() as i64; + __dst.tv_sec = __t.tv_sec() as i64; + __dst.tv_usec = __t.tv_usec() as i64; }), _ => {} } @@ -187,7 +184,7 @@ fn main_0() -> i32 { ); (rset.as_pointer()).with_mut(|__s| __s.zero()); (rset.as_pointer()).with_mut(|__s| __s.set((*fds.borrow())[(0) as usize])); - (*(*tv.borrow()).tv_sec.borrow_mut()) = 1_i64; + (*tv.borrow_mut()).tv_sec = 1_i64; assert!( ((({ let __rp = (rset.as_pointer()); @@ -240,10 +237,7 @@ fn main_0() -> i32 { let mut __tv = match __tp.is_null() { true => None, false => Some(__tp.with(|__t| { - nix::sys::time::TimeVal::new( - *__t.tv_sec.borrow() as _, - *__t.tv_usec.borrow() as _, - ) + nix::sys::time::TimeVal::new(__t.tv_sec as _, __t.tv_usec as _) })), }; match nix::sys::select::select( @@ -295,8 +289,8 @@ fn main_0() -> i32 { } match (__tp.is_null(), __tv.as_ref()) { (false, Some(__t)) => __tp.with_mut(|__dst| { - *__dst.tv_sec.borrow_mut() = __t.tv_sec() as i64; - *__dst.tv_usec.borrow_mut() = __t.tv_usec() as i64; + __dst.tv_sec = __t.tv_sec() as i64; + __dst.tv_usec = __t.tv_usec() as i64; }), _ => {} } diff --git a/tests/unit/out/refcount/strdup.rs b/tests/unit/out/refcount/strdup.rs index 35cb9adc3d..d4422005f0 100644 --- a/tests/unit/out/refcount/strdup.rs +++ b/tests/unit/out/refcount/strdup.rs @@ -127,13 +127,12 @@ fn main_0() -> i32 { name: Ptr::::null(), })); let r: Value> = Rc::new(RefCell::new((rec.as_pointer()))); - let __rhs = libcc2rs::strdup_refcount((*p.borrow()).clone()); - (*r.borrow()).with_mut(|__s: &mut record| __s.name = __rhs); - assert!((((!(((*r.borrow()).with(|__s: &record| (__s.name).clone())).is_null())) as i32) != 0)); + field!((*r.borrow()), name).write(libcc2rs::strdup_refcount((*p.borrow()).clone())); + assert!((((!(((*r.borrow()).with(|__s| __s.name.clone())).is_null())) as i32) != 0)); assert!( ((({ let mut __it1 = (*r.borrow()) - .with(|__s: &record| (__s.name).clone()) + .with(|__s| __s.name.clone()) .to_c_string_iterator(); let mut __it2 = (*p.borrow()).to_c_string_iterator(); loop { @@ -149,9 +148,7 @@ fn main_0() -> i32 { } == 0) as i32) != 0) ); - libcc2rs::free_refcount( - ((*r.borrow()).with(|__s: &record| (__s.name).clone()) as Ptr).to_any(), - ); + libcc2rs::free_refcount(((*r.borrow()).with(|__s| __s.name.clone()) as Ptr).to_any()); return 0; } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/struct_ctor.rs b/tests/unit/out/refcount/struct_ctor.rs index ba0e30707c..d206ee4d56 100644 --- a/tests/unit/out/refcount/struct_ctor.rs +++ b/tests/unit/out/refcount/struct_ctor.rs @@ -22,8 +22,8 @@ impl StructWithCtor { x2_: (*x2.borrow()), })); let this: Ptr = __this.as_pointer(); - this.with_mut(|__s: &mut StructWithCtor| __s.x1_.prefix_inc()); - this.with_mut(|__s: &mut StructWithCtor| __s.x2_.prefix_dec()); + field!(this, x1_).with_mut(|__v| __v.prefix_inc()); + field!(this, x2_).with_mut(|__v| __v.prefix_dec()); Rc::try_unwrap(__this).ok().unwrap().into_inner() } } diff --git a/tests/unit/out/refcount/struct_ptr.rs b/tests/unit/out/refcount/struct_ptr.rs index c38de8745c..72057da08a 100644 --- a/tests/unit/out/refcount/struct_ptr.rs +++ b/tests/unit/out/refcount/struct_ptr.rs @@ -31,14 +31,14 @@ pub fn main() { fn main_0() -> i32 { let obj: Value = Rc::new(RefCell::new(::default())); let ptr: Value> = Rc::new(RefCell::new((obj.as_pointer()))); - (*ptr.borrow()).with_mut(|__s: &mut XX| __s.x = 2); + field!((*ptr.borrow()), x).write(2); let c: Value = Rc::new(RefCell::new(false)); let r: Value = Rc::new(RefCell::new(if (*c.borrow()) { { (*obj.borrow()).x } } else { - (*ptr.borrow()).with(|__s: &XX| __s.x) + (*ptr.borrow()).with(|__s| __s.x) })); - let p: Value> = Rc::new(RefCell::new((field_ptr!(obj, x)))); + let p: Value> = Rc::new(RefCell::new((field_ptr!(obj.as_pointer(), x)))); assert!( ({ let _lhs = ((*p.borrow()).read()); diff --git a/tests/unit/out/refcount/sys_stat.rs b/tests/unit/out/refcount/sys_stat.rs index 92a66251d7..4fc026cd33 100644 --- a/tests/unit/out/refcount/sys_stat.rs +++ b/tests/unit/out/refcount/sys_stat.rs @@ -49,8 +49,8 @@ pub fn test_stat_0() { } == 0) as i32) != 0) ); - assert!(((((*(*st.borrow()).st_size.borrow()) == 5_i64) as i32) != 0)); - assert!(((((*(*st.borrow()).st_mtime.borrow()) > 0_i64) as i32) != 0)); + assert!(((({ (*st.borrow()).st_size } == 5_i64) as i32) != 0)); + assert!(((({ (*st.borrow()).st_mtime } > 0_i64) as i32) != 0)); match nix::unistd::unlink((*path.borrow()).to_rust_string().as_str()) { Ok(()) => 0, Err(__e) => { @@ -96,8 +96,8 @@ pub fn test_fstat_1() { } == 0) as i32) != 0) ); - assert!(((((*(*st.borrow()).st_size.borrow()) == 11_i64) as i32) != 0)); - assert!(((((*(*st.borrow()).st_mtime.borrow()) > 0_i64) as i32) != 0)); + assert!(((({ (*st.borrow()).st_size } == 11_i64) as i32) != 0)); + assert!(((({ (*st.borrow()).st_mtime } > 0_i64) as i32) != 0)); assert!( ((({ let __r = (*fp.borrow()).with(|__f| __f.close()); diff --git a/tests/unit/out/refcount/sys_time.rs b/tests/unit/out/refcount/sys_time.rs index c604842e48..4f8f1e5949 100644 --- a/tests/unit/out/refcount/sys_time.rs +++ b/tests/unit/out/refcount/sys_time.rs @@ -67,17 +67,17 @@ pub fn print_tm_1(t: i64) { ); println!( "{}-{}-{} {}:{}:{} wday={} yday={} {} gmtoff={} isdst={}", - (*(*tm.borrow()).tm_year.borrow()), - (*(*tm.borrow()).tm_mon.borrow()), - (*(*tm.borrow()).tm_mday.borrow()), - (*(*tm.borrow()).tm_hour.borrow()), - (*(*tm.borrow()).tm_min.borrow()), - (*(*tm.borrow()).tm_sec.borrow()), - (*(*tm.borrow()).tm_wday.borrow()), - (*(*tm.borrow()).tm_yday.borrow()), - (*(*tm.borrow()).tm_zone.borrow()), - (*(*tm.borrow()).tm_gmtoff.borrow()), - (*(*tm.borrow()).tm_isdst.borrow()) + { (*tm.borrow()).tm_year }, + { (*tm.borrow()).tm_mon }, + { (*tm.borrow()).tm_mday }, + { (*tm.borrow()).tm_hour }, + { (*tm.borrow()).tm_min }, + { (*tm.borrow()).tm_sec }, + { (*tm.borrow()).tm_wday }, + { (*tm.borrow()).tm_yday }, + { (*tm.borrow()).tm_zone.clone() }, + { (*tm.borrow()).tm_gmtoff }, + { (*tm.borrow()).tm_isdst } ); } pub fn test_gmtime_r_2() { @@ -106,8 +106,8 @@ pub fn print_local_tm_3(t: i64) { let __isdst = if __info.dst().is_dst() { 1 } else { 0 }; __res.with_mut(|__tm| { *__tm = Tm::from_zoned(&__dt); - *__tm.tm_isdst.borrow_mut() = __isdst; - *__tm.tm_zone.borrow_mut() = Ptr::alloc_array(__zone.into_boxed_slice()); + __tm.tm_isdst = __isdst; + __tm.tm_zone = Ptr::alloc_array(__zone.into_boxed_slice()); }); __res } @@ -122,17 +122,17 @@ pub fn print_local_tm_3(t: i64) { ); println!( "{}-{}-{} {}:{}:{} wday={} yday={} {} gmtoff={} isdst={}", - (*(*tm.borrow()).tm_year.borrow()), - (*(*tm.borrow()).tm_mon.borrow()), - (*(*tm.borrow()).tm_mday.borrow()), - (*(*tm.borrow()).tm_hour.borrow()), - (*(*tm.borrow()).tm_min.borrow()), - (*(*tm.borrow()).tm_sec.borrow()), - (*(*tm.borrow()).tm_wday.borrow()), - (*(*tm.borrow()).tm_yday.borrow()), - (*(*tm.borrow()).tm_zone.borrow()), - (*(*tm.borrow()).tm_gmtoff.borrow()), - (*(*tm.borrow()).tm_isdst.borrow()) + { (*tm.borrow()).tm_year }, + { (*tm.borrow()).tm_mon }, + { (*tm.borrow()).tm_mday }, + { (*tm.borrow()).tm_hour }, + { (*tm.borrow()).tm_min }, + { (*tm.borrow()).tm_sec }, + { (*tm.borrow()).tm_wday }, + { (*tm.borrow()).tm_yday }, + { (*tm.borrow()).tm_zone.clone() }, + { (*tm.borrow()).tm_gmtoff }, + { (*tm.borrow()).tm_isdst } ); } pub fn test_localtime_r_4() { diff --git a/tests/unit/out/refcount/tag_vs_identifier_collision.rs b/tests/unit/out/refcount/tag_vs_identifier_collision.rs index 1cebff6da8..6ed7532744 100644 --- a/tests/unit/out/refcount/tag_vs_identifier_collision.rs +++ b/tests/unit/out/refcount/tag_vs_identifier_collision.rs @@ -189,7 +189,7 @@ impl ByteRepr for Inner_struct { } pub fn is_active_0(w: Ptr) -> i32 { let w: Value> = Rc::new(RefCell::new(w)); - return ((((*w.borrow()).with(|__s: &widget| __s.mode) as u32) + return ((((*w.borrow()).with(|__s| __s.mode) as u32) == ((widget_enum_MODE_ACTIVE as i32) as u32)) as i32); } pub fn main() { diff --git a/tests/unit/out/refcount/template_default_arg.rs b/tests/unit/out/refcount/template_default_arg.rs index c5ccd5f7f3..21f4d3b1d4 100644 --- a/tests/unit/out/refcount/template_default_arg.rs +++ b/tests/unit/out/refcount/template_default_arg.rs @@ -162,7 +162,7 @@ pub trait S_NoDefault_Impl { impl S_NoDefault_Impl for Ptr { fn get(&self, t: NoDefault) -> i32 { let t: Value = Rc::new(RefCell::new(t)); - return ((*self).with(|__s: &S_NoDefault_| __s.v) + { (*t.borrow()).v }); + return ((*self).with(|__s| __s.v) + { (*t.borrow()).v }); } } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/this.rs b/tests/unit/out/refcount/this.rs index 9e1e4d2114..d7ef399e14 100644 --- a/tests/unit/out/refcount/this.rs +++ b/tests/unit/out/refcount/this.rs @@ -32,9 +32,9 @@ impl S { })); let this: Ptr = __this.as_pointer(); if (this == (*other.borrow())) { - this.with_mut(|__s: &mut S| __s.self__ = Ptr::::null()); + field!(this, self__).write(Ptr::::null()); } else { - this.with_mut(|__s: &mut S| __s.self__ = (*other.borrow()).clone()); + field!(this, self__).write((*other.borrow()).clone()); } Rc::try_unwrap(__this).ok().unwrap().into_inner() } @@ -47,7 +47,7 @@ impl S { })); let this: Ptr = __this.as_pointer(); if (this == (*other.borrow())) { - this.with_mut(|__s: &mut S| __s.self__ = Ptr::::null()); + field!(this, self__).write(Ptr::::null()); } Rc::try_unwrap(__this).ok().unwrap().into_inner() } @@ -69,7 +69,7 @@ impl ByteRepr for S { } pub fn bump_0(p: Ptr) { let p: Value> = Rc::new(RefCell::new(p)); - (*p.borrow()).with_mut(|__s: &mut S| __s.a_.postfix_inc()); + field!((*p.borrow()), a_).with_mut(|__v| __v.postfix_inc()); } #[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct D { @@ -81,7 +81,10 @@ impl D { let a: Value = Rc::new(RefCell::new(a)); let __this: Value = Rc::new(RefCell::new(Self { a_: (*a.borrow()) })); let this: Ptr = __this.as_pointer(); - this.with_mut(|__s: &mut D| __s.a_ *= 2); + { + let _ptr = field!(this, a_); + _ptr.write(_ptr.read() * 2) + }; Rc::try_unwrap(__this).ok().unwrap().into_inner() } } @@ -105,12 +108,12 @@ pub fn main() { fn main_0() -> i32 { let s: Value = Rc::new(RefCell::new(S::new_1({ 1 }))); let ref_: Ptr = ({ SImpl::returns_this_reference(&s.as_pointer()) }); - ref_.with_mut(|__s: &mut S| __s.a_.postfix_inc()); + field!(ref_, a_).with_mut(|__v| __v.postfix_inc()); assert!(({ (*s.borrow()).a_ } == 2)); let ptr: Value> = Rc::new(RefCell::new( ({ SImpl::returns_this_pointer(&s.as_pointer()) }), )); - (*ptr.borrow()).with_mut(|__s: &mut S| __s.a_.postfix_inc()); + field!((*ptr.borrow()), a_).with_mut(|__v| __v.postfix_inc()); assert!(({ (*s.borrow()).a_ } == 3)); ({ SImpl::inc(&({ SImpl::inc(&({ SImpl::inc(&s.as_pointer()) })) })) }); assert!(({ (*s.borrow()).a_ } == 6)); @@ -122,14 +125,14 @@ fn main_0() -> i32 { let _lhs = { (*s.borrow()).self__.clone() }; _lhs == (s.as_pointer()) }); - { (*s.borrow()).self__.clone() }.with_mut(|__s: &mut S| __s.a_.postfix_inc()); + field!({ (*s.borrow()).self__.clone() }, a_).with_mut(|__v| __v.postfix_inc()); assert!(({ (*s.borrow()).a_ } == 8)); ({ SImpl::bump_me(&s.as_pointer()) }); assert!(({ (*s.borrow()).a_ } == 9)); let d: Value = Rc::new(RefCell::new(D::new({ 3 }))); assert!(({ (*d.borrow()).a_ } == 6)); let cr: Ptr = ({ SImpl::cref(&s.as_pointer()) }); - assert!((cr.with(|__s: &S| __s.a_) == 9)); + assert!((cr.with(|__s| __s.a_) == 9)); let t: Value = Rc::new(RefCell::new(S::new_1({ 0 }))); assert!( ({ @@ -142,8 +145,8 @@ fn main_0() -> i32 { let q: Value> = Rc::new(RefCell::new( ({ SImpl::returns_this_pointer(&(*p.borrow())) }), )); - (*q.borrow()).with_mut(|__s: &mut S| __s.a_.postfix_inc()); - assert!(((*p.borrow()).with(|__s: &S| __s.a_) == 2)); + field!((*q.borrow()), a_).with_mut(|__v| __v.postfix_inc()); + assert!(((*p.borrow()).with(|__s| __s.a_) == 2)); (*p.borrow()).delete(); let h: Value> = Rc::new(RefCell::new(Ptr::alloc(S::new_1({ 5 })))); ({ SImpl::destroy(&(*h.borrow())) }); @@ -208,22 +211,21 @@ impl SImpl for Ptr { return (*self).clone(); } fn inc(&self) -> Ptr { - (*self).with_mut(|__s: &mut S| __s.a_.postfix_inc()); + field!((*self), a_).with_mut(|__v| __v.postfix_inc()); return (*self).clone(); } fn set_from_this(&self) { - let __rhs = ((*self).with(|__s: &S| __s.a_) + 1); - (*self).with_mut(|__s: &mut S| __s.a_ = __rhs); + let __rhs = ((*self).with(|__s| __s.a_) + 1); + field!((*self), a_).write(__rhs); } fn get(&self) -> i32 { - return (*self).with(|__s: &S| __s.a_); + return (*self).with(|__s| __s.a_); } fn twice(&self) -> i32 { return (({ SImpl::get(self) }) * 2); } fn link(&self) { - let __rhs = (*self).clone(); - (*self).with_mut(|__s: &mut S| __s.self__ = __rhs); + field!((*self), self__).write((*self).clone()); } fn bump_me(&self) { ({ bump_0((*self).clone()) }); @@ -246,10 +248,10 @@ impl SImpl for Ptr { if ((*self) == (*other.borrow())) { return false; } - let __rhs = (*other.borrow()).with(|__s: &S| __s.a_); - (*self).with_mut(|__s: &mut S| __s.a_ = __rhs); - let __rhs = (*other.borrow()).with(|__s: &S| (__s.self__).clone()); - (*self).with_mut(|__s: &mut S| __s.self__ = __rhs); + let __rhs = (*other.borrow()).with(|__s| __s.a_); + field!((*self), a_).write(__rhs); + let __rhs = (*other.borrow()).with(|__s| __s.self__.clone()); + field!((*self), self__).write(__rhs); return true; } fn copy_if_different(&self, other: Ptr) -> bool { @@ -257,10 +259,10 @@ impl SImpl for Ptr { if ((*self) == (*other.borrow())) { return false; } - let __rhs = (*other.borrow()).with(|__s: &S| __s.a_); - (*self).with_mut(|__s: &mut S| __s.a_ = __rhs); - let __rhs = (*other.borrow()).with(|__s: &S| (__s.self__).clone()); - (*self).with_mut(|__s: &mut S| __s.self__ = __rhs); + let __rhs = (*other.borrow()).with(|__s| __s.a_); + field!((*self), a_).write(__rhs); + let __rhs = (*other.borrow()).with(|__s| __s.self__.clone()); + field!((*self), self__).write(__rhs); return true; } } diff --git a/tests/unit/out/refcount/typedef-anon-struct.rs b/tests/unit/out/refcount/typedef-anon-struct.rs index def16265f8..e36d2ffecd 100644 --- a/tests/unit/out/refcount/typedef-anon-struct.rs +++ b/tests/unit/out/refcount/typedef-anon-struct.rs @@ -35,11 +35,9 @@ pub struct Outer { } impl Clone for Outer { fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - runs: { Rc::new(RefCell::new((*self.runs.borrow()).clone())) }, - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() + Self { + runs: Rc::new(RefCell::new((*self.runs.borrow()).clone())), + } } } <<<<<<< HEAD @@ -70,11 +68,27 @@ fn main_0() -> i32 { (*info.borrow_mut()).num_extra_zero_runs = 2; { let a0_clone = (*info.borrow()).clone(); - (*(*o.borrow()).runs.borrow_mut()).push(a0_clone) + (*{ (*o.borrow()).runs.clone() }.borrow_mut()).push(a0_clone) }; - assert!(((*(*o.borrow()).runs.borrow()).len() == 1_usize)); - assert!(({ (*(*o.borrow()).runs.borrow())[(0_usize) as usize].block_idx } == 1)); - assert!(({ (*(*o.borrow()).runs.borrow())[(0_usize) as usize].num_extra_zero_runs } == 2)); + assert!(((*{ (*o.borrow()).runs.clone() }.borrow()).len() == 1_usize)); + assert!( + ({ + (*({ (*o.borrow()).runs.clone() }.as_pointer() as Ptr) + .offset(0_usize) + .upgrade() + .deref()) + .block_idx + } == 1) + ); + assert!( + ({ + (*({ (*o.borrow()).runs.clone() }.as_pointer() as Ptr) + .offset(0_usize) + .upgrade() + .deref()) + .num_extra_zero_runs + } == 2) + ); return 0; } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/union_addrof_external.rs b/tests/unit/out/refcount/union_addrof_external.rs index 47ec96a0eb..c0f808d02d 100644 --- a/tests/unit/out/refcount/union_addrof_external.rs +++ b/tests/unit/out/refcount/union_addrof_external.rs @@ -155,11 +155,11 @@ fn main_0() -> i32 { ((c.as_pointer()) as Ptr).to_any() }; ({ - let _out: AnyPtr = (field_ptr!(c, view)).to_any(); + let _out: AnyPtr = (field_ptr!(c.as_pointer(), view)).to_any(); let _cap: usize = 128usize; fill_1(_out, _cap) }); - assert!((((((*c.borrow()).view.h().with(|__s: &record| __s.code) as i32) == 2) as i32) != 0)); + assert!((((((*c.borrow()).view.h().with(|__s| __s.code) as i32) == 2) as i32) != 0)); assert!( (((((((field_ptr!((*c.borrow()).view.h(), lo)).reinterpret_cast::()) .offset((0) as isize) diff --git a/tests/unit/out/refcount/union_cross_arm_cast.rs b/tests/unit/out/refcount/union_cross_arm_cast.rs index e6971d592a..6e8bf3badd 100644 --- a/tests/unit/out/refcount/union_cross_arm_cast.rs +++ b/tests/unit/out/refcount/union_cross_arm_cast.rs @@ -175,19 +175,16 @@ fn main_0() -> i32 { .memset((0) as u8, 68usize as usize); ((c.as_pointer()) as Ptr).to_any() }; - (*c.borrow()) - .u - .a() - .with_mut(|__s: &mut shape_a| __s.code = 10_u16); + field!((*c.borrow_mut()).u.a(), code).write(10_u16); (*c.borrow_mut()).len = (28usize as u32); - (((*c.borrow()).u.a()).to_any().reinterpret_cast::()) - .with_mut(|__s: &mut shape_b| __s.tail = 3735928559_u32); - assert!(((((*c.borrow()).u.b().with(|__s: &shape_b| __s.tail) == 3735928559_u32) as i32) != 0)); - assert!((((((*c.borrow()).u.b().with(|__s: &shape_b| __s.code) as i32) == 10) as i32) != 0)); - (*c.borrow()) - .u - .b() - .with_mut(|__s: &mut shape_b| __s.lo = 8080_u16); + field!( + (((*c.borrow()).u.a()).to_any().reinterpret_cast::()), + tail + ) + .write(3735928559_u32); + assert!(((((*c.borrow()).u.b().with(|__s| __s.tail) == 3735928559_u32) as i32) != 0)); + assert!((((((*c.borrow()).u.b().with(|__s| __s.code) as i32) == 10) as i32) != 0)); + field!((*c.borrow_mut()).u.b(), lo).write(8080_u16); assert!( ((((((((*c.borrow()).u.raw_().reinterpret_cast::()).reinterpret_cast::()) .offset((2) as isize) diff --git a/tests/unit/out/refcount/union_field_alignment.rs b/tests/unit/out/refcount/union_field_alignment.rs index 1e44418f16..2d9ce3f2ae 100644 --- a/tests/unit/out/refcount/union_field_alignment.rs +++ b/tests/unit/out/refcount/union_field_alignment.rs @@ -73,7 +73,8 @@ pub fn main() { fn main_0() -> i32 { let n: Value = >::default(); (*n.borrow_mut()).next = Ptr::::null(); - { ((*n.borrow()).x.bytes().reinterpret_cast::() as Ptr).offset((0) as isize) } + ((*n.borrow()).x.bytes().reinterpret_cast::() as Ptr) + .offset((0) as isize) .write(171_u8); assert!( (((((((*n.borrow()).x.bytes().reinterpret_cast::() as Ptr::) diff --git a/tests/unit/out/refcount/union_flex_array_member.rs b/tests/unit/out/refcount/union_flex_array_member.rs index 263e7bddf0..837c0ab4a3 100644 --- a/tests/unit/out/refcount/union_flex_array_member.rs +++ b/tests/unit/out/refcount/union_flex_array_member.rs @@ -82,18 +82,16 @@ fn main_0() -> i32 { ) .reinterpret_cast::(), )); - (*n.borrow()).with_mut(|__s: &mut node| __s.len = (*tail_size.borrow())); + field!((*n.borrow()), len).write((*tail_size.borrow())); let i: Value = Rc::new(RefCell::new(0_usize)); 'loop_: while ((((*i.borrow()) < (*tail_size.borrow())) as i32) != 0) { let __rhs = (((*i.borrow()) & 255_usize) as u8); - { - ((*(*n.borrow()).upgrade().deref()) - .x - .bytes() - .reinterpret_cast::() as Ptr) - .offset((*i.borrow()) as isize) - } - .write(__rhs); + ((*(*n.borrow()).upgrade().deref()) + .x + .bytes() + .reinterpret_cast::() as Ptr) + .offset((*i.borrow()) as isize) + .write(__rhs); (*i.borrow_mut()).postfix_inc(); } let i: Value = Rc::new(RefCell::new(0_usize)); @@ -131,13 +129,13 @@ fn main_0() -> i32 { == 170) as i32) != 0) ); - (*n.borrow()).with_mut(|__s: &mut node| __s.pos = 20_usize); + field!((*n.borrow()), pos).write(20_usize); let q: Value> = Rc::new(RefCell::new( (((*(*n.borrow()).upgrade().deref()) .x .bytes() .reinterpret_cast::() as Ptr) - .offset(((*n.borrow()).with(|__s: &node| __s.pos)) as isize)), + .offset(((*n.borrow()).with(|__s| __s.pos)) as isize)), )); assert!(((((((*q.borrow()).read()) as i32) == 20) as i32) != 0)); (*q.borrow()).write(187_u8); diff --git a/tests/unit/out/refcount/union_memset_memcpy.rs b/tests/unit/out/refcount/union_memset_memcpy.rs index 2add3504fe..0e047322d4 100644 --- a/tests/unit/out/refcount/union_memset_memcpy.rs +++ b/tests/unit/out/refcount/union_memset_memcpy.rs @@ -165,8 +165,8 @@ fn main_0() -> i32 { .memset((0) as u8, 256usize as usize); ((c.as_pointer()) as Ptr).to_any() }; - assert!((((((*c.borrow()).view.a().with(|__s: &shape_a| __s.code) as i32) == 0) as i32) != 0)); - assert!((((((*c.borrow()).view.b().with(|__s: &shape_b| __s.lo) as i32) == 0) as i32) != 0)); + assert!((((((*c.borrow()).view.a().with(|__s| __s.code) as i32) == 0) as i32) != 0)); + assert!((((((*c.borrow()).view.b().with(|__s| __s.lo) as i32) == 0) as i32) != 0)); assert!( (((((((*c.borrow()).view.raw_().reinterpret_cast::() as Ptr::) .offset((0) as isize) @@ -203,7 +203,7 @@ fn main_0() -> i32 { ); (((*c.borrow()).view.raw_().reinterpret_cast::()) as Ptr).to_any() }; - assert!((((((*c.borrow()).view.b().with(|__s: &shape_b| __s.code) as i32) == 2) as i32) != 0)); + assert!((((((*c.borrow()).view.b().with(|__s| __s.code) as i32) == 2) as i32) != 0)); assert!( (((((((field_ptr!((*c.borrow()).view.b(), lo)).reinterpret_cast::()) .offset((0) as isize) @@ -217,7 +217,7 @@ fn main_0() -> i32 { .memset((0) as u8, 256usize as usize); ((c.as_pointer()) as Ptr).to_any() }; - assert!((((((*c.borrow()).view.b().with(|__s: &shape_b| __s.code) as i32) == 0) as i32) != 0)); + assert!((((((*c.borrow()).view.b().with(|__s| __s.code) as i32) == 0) as i32) != 0)); return 0; } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/union_nested.rs b/tests/unit/out/refcount/union_nested.rs index 18577e4979..7a6ab636b0 100644 --- a/tests/unit/out/refcount/union_nested.rs +++ b/tests/unit/out/refcount/union_nested.rs @@ -188,20 +188,15 @@ fn main_0() -> i32 { (*ex.borrow_mut()).level = 1; (*ex.borrow_mut()).variant = 6; (*ex.borrow_mut()).len = (16usize as u32); - (*ex.borrow()) + field!((*ex.borrow_mut()).body.h(), code).write(2_u16); + (*(*ex.borrow()) .body .h() - .with_mut(|__s: &mut record| __s.code = 2_u16); - (*ex.borrow()) - .body - .h() - .with(|__s: &record| (*__s.pad.borrow_mut())[(0) as usize] = (('X' as i32) as u8)); - assert!((((((*ex.borrow()).body.h().with(|__s: &record| __s.code) as i32) == 2) as i32) != 0)); + .with(|__s| __s.pad.clone()) + .borrow_mut())[(0) as usize] = (('X' as i32) as u8); + assert!((((((*ex.borrow()).body.h().with(|__s| __s.code) as i32) == 2) as i32) != 0)); assert!( - (((((*ex.borrow()) - .body - .h() - .with(|__s: &record| (*__s.pad.borrow())[(0) as usize]) as i32) + (((((*(*ex.borrow()).body.h().with(|__s| __s.pad.clone()).borrow())[(0) as usize] as i32) == ('X' as i32)) as i32) != 0) ); @@ -209,7 +204,7 @@ fn main_0() -> i32 { (((((*(*ex.borrow()).body.nested().upgrade().deref()) .view .h() - .with(|__s: &record| __s.code) as i32) + .with(|__s| __s.code) as i32) == 2) as i32) != 0) ); diff --git a/tests/unit/out/refcount/union_struct_dual_use.rs b/tests/unit/out/refcount/union_struct_dual_use.rs index 3df37e012f..11cd17c47d 100644 --- a/tests/unit/out/refcount/union_struct_dual_use.rs +++ b/tests/unit/out/refcount/union_struct_dual_use.rs @@ -31,8 +31,8 @@ impl ByteRepr for Inner { pub fn sum_inner_0(i: Ptr) -> i32 { let i: Value> = Rc::new(RefCell::new(i)); return { - let _lhs = (*i.borrow()).with(|__s: &Inner| __s.a); - _lhs + (*i.borrow()).with(|__s: &Inner| __s.b) + let _lhs = (*i.borrow()).with(|__s| __s.a); + _lhs + (*i.borrow()).with(|__s| __s.b) }; } pub struct anon_1 { @@ -107,14 +107,8 @@ fn main_0() -> i32 { .memset((0) as u8, 16usize as usize); ((outer.as_pointer()) as Ptr).to_any() }; - (*outer.borrow()) - .u - .inner() - .with_mut(|__s: &mut Inner| __s.a = 3); - (*outer.borrow()) - .u - .inner() - .with_mut(|__s: &mut Inner| __s.b = 4); + field!((*outer.borrow_mut()).u.inner(), a).write(3); + field!((*outer.borrow_mut()).u.inner(), b).write(4); assert!((((({ sum_inner_0(((*outer.borrow()).u.inner()).clone(),) }) == 7) as i32) != 0)); assert!( ((((((((*outer.borrow()).u.raw_().reinterpret_cast::() as Ptr::) diff --git a/tests/unit/out/refcount/union_tagged_many_arms.rs b/tests/unit/out/refcount/union_tagged_many_arms.rs index 4af70390d4..0ab537301a 100644 --- a/tests/unit/out/refcount/union_tagged_many_arms.rs +++ b/tests/unit/out/refcount/union_tagged_many_arms.rs @@ -88,15 +88,18 @@ pub fn main() { fn main_0() -> i32 { let a: Value = >::default(); (*a.borrow_mut()).tag = Tag_enum_T_NUM_S; - { (*a.borrow_mut()).payload.signed_n() }.write((-7_i32 as i64)); + (*a.borrow_mut()).payload.signed_n().write((-7_i32 as i64)); assert!((((((*a.borrow()).payload.signed_n().read()) == (-7_i32 as i64)) as i32) != 0)); let b: Value = >::default(); (*b.borrow_mut()).tag = Tag_enum_T_NUM_U; - { (*b.borrow_mut()).payload.unsigned_n() }.write(3735928559_u64); + (*b.borrow_mut()).payload.unsigned_n().write(3735928559_u64); assert!((((((*b.borrow()).payload.unsigned_n().read()) == 3735928559_u64) as i32) != 0)); let c: Value = >::default(); (*c.borrow_mut()).tag = Tag_enum_T_TEXT; - { (*c.borrow_mut()).payload.text() }.write(Ptr::::from_string_literal(b"hello")); + (*c.borrow_mut()) + .payload + .text() + .write(Ptr::::from_string_literal(b"hello")); assert!( (((((((*c.borrow()).payload.text().read()) .offset((0) as isize) @@ -106,12 +109,15 @@ fn main_0() -> i32 { ); let d: Value = >::default(); (*d.borrow_mut()).tag = Tag_enum_T_FLOAT; - { (*d.borrow_mut()).payload.f() }.write(1.5E+0); + (*d.borrow_mut()).payload.f().write(1.5E+0); assert!((((((*d.borrow()).payload.f().read()) == 1.5E+0) as i32) != 0)); let x: Value = Rc::new(RefCell::new(0)); let e: Value = >::default(); (*e.borrow_mut()).tag = Tag_enum_T_REF; - { (*e.borrow_mut()).payload.handle() }.write(((x.as_pointer()) as Ptr).to_any()); + (*e.borrow_mut()) + .payload + .handle() + .write(((x.as_pointer()) as Ptr).to_any()); assert!( ((({ let _lhs = ((*e.borrow()).payload.handle().read()).clone(); diff --git a/tests/unit/out/refcount/union_tagged_simple.rs b/tests/unit/out/refcount/union_tagged_simple.rs index f3e0f091de..e48df1b758 100644 --- a/tests/unit/out/refcount/union_tagged_simple.rs +++ b/tests/unit/out/refcount/union_tagged_simple.rs @@ -82,7 +82,7 @@ fn main_0() -> i32 { let m1: Value = >::default(); (*m1.borrow_mut()).kind = Kind_enum_KIND_DONE; (*m1.borrow_mut()).handle = ((dummy.as_pointer()) as Ptr).to_any(); - { (*m1.borrow_mut()).payload.code() }.write(42); + (*m1.borrow_mut()).payload.code().write(42); assert!( (((({ (*m1.borrow()).kind } as u32) == ((Kind_enum_KIND_DONE as i32) as u32)) as i32) != 0) ); @@ -90,7 +90,10 @@ fn main_0() -> i32 { let m2: Value = >::default(); (*m2.borrow_mut()).kind = Kind_enum_KIND_NONE; (*m2.borrow_mut()).handle = ((dummy.as_pointer()) as Ptr).to_any(); - { (*m2.borrow_mut()).payload.obj() }.write(((dummy.as_pointer()) as Ptr).to_any()); + (*m2.borrow_mut()) + .payload + .obj() + .write(((dummy.as_pointer()) as Ptr).to_any()); assert!( ((({ let _lhs = ((*m2.borrow()).payload.obj().read()).clone(); diff --git a/tests/unit/out/refcount/union_tagged_struct_arms.rs b/tests/unit/out/refcount/union_tagged_struct_arms.rs index 85761043c8..be222915e7 100644 --- a/tests/unit/out/refcount/union_tagged_struct_arms.rs +++ b/tests/unit/out/refcount/union_tagged_struct_arms.rs @@ -182,23 +182,16 @@ fn main_0() -> i32 { let p_list: Value = >::default(); (*p_list.borrow_mut()).choice = Choice_enum_C_LIST; (*p_list.borrow_mut()).index = 0; - (*p_list.borrow()).v.list().with_mut(|__s: &mut anon_1| { - __s.items = (items_4.with(|v| v.as_pointer()) as Ptr>) - }); - (*p_list.borrow()) - .v - .list() - .with_mut(|__s: &mut anon_1| __s.count = 3_i64); - (*p_list.borrow()) - .v - .list() - .with_mut(|__s: &mut anon_1| __s.cursor = 1_i64); - assert!(((((*p_list.borrow()).v.list().with(|__s: &anon_1| __s.count) == 3_i64) as i32) != 0)); + field!((*p_list.borrow_mut()).v.list(), items) + .write((items_4.with(|v| v.as_pointer()) as Ptr>)); + field!((*p_list.borrow_mut()).v.list(), count).write(3_i64); + field!((*p_list.borrow_mut()).v.list(), cursor).write(1_i64); + assert!(((((*p_list.borrow()).v.list().with(|__s| __s.count) == 3_i64) as i32) != 0)); assert!( (((((((*p_list.borrow()) .v .list() - .with(|__s: &anon_1| (__s.items).clone()) + .with(|__s| __s.items.clone()) .offset((1) as isize) .read()) .offset((0) as isize) @@ -209,73 +202,26 @@ fn main_0() -> i32 { let p_letters: Value = >::default(); (*p_letters.borrow_mut()).choice = Choice_enum_C_LETTERS; (*p_letters.borrow_mut()).index = 1; - (*p_letters.borrow()) - .v - .letters() - .with_mut(|__s: &mut anon_2| __s.lo = ('a' as i32)); - (*p_letters.borrow()) - .v - .letters() - .with_mut(|__s: &mut anon_2| __s.hi = ('z' as i32)); - (*p_letters.borrow()) - .v - .letters() - .with_mut(|__s: &mut anon_2| __s.curr = ('m' as i32)); - (*p_letters.borrow()) - .v - .letters() - .with_mut(|__s: &mut anon_2| __s.step = 1_u8); + field!((*p_letters.borrow_mut()).v.letters(), lo).write(('a' as i32)); + field!((*p_letters.borrow_mut()).v.letters(), hi).write(('z' as i32)); + field!((*p_letters.borrow_mut()).v.letters(), curr).write(('m' as i32)); + field!((*p_letters.borrow_mut()).v.letters(), step).write(1_u8); assert!( - (((((*p_letters.borrow()) - .v - .letters() - .with(|__s: &anon_2| __s.hi) - - (*p_letters.borrow()) - .v - .letters() - .with(|__s: &anon_2| __s.lo)) + (((((*p_letters.borrow()).v.letters().with(|__s| __s.hi) + - (*p_letters.borrow()).v.letters().with(|__s| __s.lo)) == 25) as i32) != 0) ); let p_integers: Value = >::default(); (*p_integers.borrow_mut()).choice = Choice_enum_C_INTEGERS; (*p_integers.borrow_mut()).index = 2; - (*p_integers.borrow()) - .v - .integers() - .with_mut(|__s: &mut anon_3| __s.lo = 1_i64); - (*p_integers.borrow()) - .v - .integers() - .with_mut(|__s: &mut anon_3| __s.hi = 100_i64); - (*p_integers.borrow()) - .v - .integers() - .with_mut(|__s: &mut anon_3| __s.curr = 1_i64); - (*p_integers.borrow()) - .v - .integers() - .with_mut(|__s: &mut anon_3| __s.step = 1_i64); - (*p_integers.borrow()) - .v - .integers() - .with_mut(|__s: &mut anon_3| __s.width = 3); - assert!( - ((((*p_integers.borrow()) - .v - .integers() - .with(|__s: &anon_3| __s.hi) - == 100_i64) as i32) - != 0) - ); - assert!( - ((((*p_integers.borrow()) - .v - .integers() - .with(|__s: &anon_3| __s.width) - == 3) as i32) - != 0) - ); + field!((*p_integers.borrow_mut()).v.integers(), lo).write(1_i64); + field!((*p_integers.borrow_mut()).v.integers(), hi).write(100_i64); + field!((*p_integers.borrow_mut()).v.integers(), curr).write(1_i64); + field!((*p_integers.borrow_mut()).v.integers(), step).write(1_i64); + field!((*p_integers.borrow_mut()).v.integers(), width).write(3); + assert!(((((*p_integers.borrow()).v.integers().with(|__s| __s.hi) == 100_i64) as i32) != 0)); + assert!(((((*p_integers.borrow()).v.integers().with(|__s| __s.width) == 3) as i32) != 0)); return 0; } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/union_void_ptr_sized_deref.rs b/tests/unit/out/refcount/union_void_ptr_sized_deref.rs index c102286cd0..2f5b3de160 100644 --- a/tests/unit/out/refcount/union_void_ptr_sized_deref.rs +++ b/tests/unit/out/refcount/union_void_ptr_sized_deref.rs @@ -80,30 +80,24 @@ pub fn write_count_1(s: Ptr, count: i64) { let s: Value> = Rc::new(RefCell::new(s)); let count: Value = Rc::new(RefCell::new(count)); 'switch: { - let __match_cond = ((*s.borrow()).with(|__s: &Sink| __s.width) as u32); + let __match_cond = ((*s.borrow()).with(|__s| __s.width) as u32); match __match_cond { __v if __v == ((Width_enum_W_64 as i32) as u32) => { - { - ((*(*s.borrow()).upgrade().deref()).out.handle().read()) - .reinterpret_cast::() - } - .write((*count.borrow())); + ((*(*s.borrow()).upgrade().deref()).out.handle().read()) + .reinterpret_cast::() + .write((*count.borrow())); break 'switch; } __v if __v == ((Width_enum_W_32 as i32) as u32) => { - { - ((*(*s.borrow()).upgrade().deref()).out.handle().read()) - .reinterpret_cast::() - } - .write(((*count.borrow()) as i32)); + ((*(*s.borrow()).upgrade().deref()).out.handle().read()) + .reinterpret_cast::() + .write(((*count.borrow()) as i32)); break 'switch; } __v if __v == ((Width_enum_W_16 as i32) as u32) => { - { - ((*(*s.borrow()).upgrade().deref()).out.handle().read()) - .reinterpret_cast::() - } - .write(((*count.borrow()) as i16)); + ((*(*s.borrow()).upgrade().deref()).out.handle().read()) + .reinterpret_cast::() + .write(((*count.borrow()) as i16)); break 'switch; } _ => {} @@ -120,15 +114,24 @@ fn main_0() -> i32 { let buf16: Value = Rc::new(RefCell::new(0_i16)); let s: Value = >::default(); (*s.borrow_mut()).width = Width_enum_W_64; - { (*s.borrow_mut()).out.handle() }.write(((buf64.as_pointer()) as Ptr).to_any()); + (*s.borrow_mut()) + .out + .handle() + .write(((buf64.as_pointer()) as Ptr).to_any()); ({ write_count_1((s.as_pointer()), 1234605616436508552_i64) }); assert!(((((*buf64.borrow()) == 1234605616436508552_i64) as i32) != 0)); (*s.borrow_mut()).width = Width_enum_W_32; - { (*s.borrow_mut()).out.handle() }.write(((buf32.as_pointer()) as Ptr).to_any()); + (*s.borrow_mut()) + .out + .handle() + .write(((buf32.as_pointer()) as Ptr).to_any()); ({ write_count_1((s.as_pointer()), 305419896_i64) }); assert!(((((*buf32.borrow()) == 305419896) as i32) != 0)); (*s.borrow_mut()).width = Width_enum_W_16; - { (*s.borrow_mut()).out.handle() }.write(((buf16.as_pointer()) as Ptr).to_any()); + (*s.borrow_mut()) + .out + .handle() + .write(((buf16.as_pointer()) as Ptr).to_any()); ({ write_count_1((s.as_pointer()), 4660_i64) }); assert!((((((*buf16.borrow()) as i32) == 4660) as i32) != 0)); return 0; diff --git a/tests/unit/out/refcount/unique_ptr.rs b/tests/unit/out/refcount/unique_ptr.rs index b16ddf13d9..e84d7ff097 100644 --- a/tests/unit/out/refcount/unique_ptr.rs +++ b/tests/unit/out/refcount/unique_ptr.rs @@ -14,7 +14,7 @@ pub struct SafePointer { impl SafePointer { pub fn move_from(_a0: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - ptr: { _a0.with_mut(|__s: &mut SafePointer| __s.ptr.take()) }, + ptr: field!(_a0, ptr).with_mut(|__v: &mut Option>| __v.take()), })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() @@ -90,20 +90,26 @@ pub fn DoStuffWithSafePointer_0(safe_ptr: Ptr>>) { ({ PairImpl::inc(&((*pair.borrow()).as_pointer()), 10) }); let __rhs = { let _lhs = { - let _lhs = (*(*(*safe_ptr.upgrade().deref()).as_ref().unwrap().borrow()) - .ptr - .as_ref() - .unwrap() - .borrow()); - _lhs + ((*pair.borrow()).as_pointer()).with(|__s: &Pair| __s.x) + let _lhs = (*{ + (*(*safe_ptr.upgrade().deref()).as_ref().unwrap().borrow()) + .ptr + .clone() + } + .as_ref() + .unwrap() + .borrow()); + _lhs + { (*(*pair.borrow()).as_ref().unwrap().borrow()).x } }; - _lhs + ((*pair.borrow()).as_pointer()).with(|__s: &Pair| __s.y) + _lhs + { (*(*pair.borrow()).as_ref().unwrap().borrow()).y } }; - (*(*(*safe_ptr.upgrade().deref()).as_ref().unwrap().borrow()) - .ptr - .as_ref() - .unwrap() - .borrow_mut()) = __rhs; + (*{ + (*(*safe_ptr.upgrade().deref()).as_ref().unwrap().borrow()) + .ptr + .clone() + } + .as_ref() + .unwrap() + .borrow_mut()) = __rhs; } pub fn Consume_1(safe_ptr: Option>) -> i32 { let safe_ptr: Value>> = Rc::new(RefCell::new(safe_ptr)); @@ -111,8 +117,7 @@ pub fn Consume_1(safe_ptr: Option>) -> i32 { Rc::new(RefCell::new((*safe_ptr.borrow_mut()).take())); let p: Value>> = Rc::new(RefCell::new(Ptr::alloc(::default()).to_owned_opt())); - return ((*(*(*x.borrow()).as_ref().unwrap().borrow()) - .ptr + return ((*{ (*(*x.borrow()).as_ref().unwrap().borrow()).ptr.clone() } .as_ref() .unwrap() .borrow()) @@ -128,8 +133,9 @@ pub fn RndStuff_2() { (*x2.borrow()).as_ref().unwrap().borrow_mut()[((*i.borrow()) as usize) as usize] = 1; (*i.borrow_mut()).prefix_inc(); } - (*x2.borrow_mut()) = - Ptr::alloc_array((0..200_usize).map(|_| 0_i32).collect::>()).to_owned_opt(); + (x2.as_pointer() as Ptr>>>).write( + Ptr::alloc_array((0..200_usize).map(|_| 0_i32).collect::>()).to_owned_opt(), + ); let i: Value = Rc::new(RefCell::new(0)); 'loop_: while ((*i.borrow()) < 200) { (*x2.borrow()).as_ref().unwrap().borrow_mut()[((*i.borrow()) as usize) as usize] = 2; @@ -156,16 +162,22 @@ pub fn RndStuff_2() { let i: Value = Rc::new(RefCell::new(0)); 'loop_: while ((*i.borrow()) < 10) { assert!( - ((*p3_0.borrow()) - .offset((*i.borrow()) as isize) - .with(|__s: &Pair| __s.x) - == 1) + ({ + (*(*p3_0.borrow()) + .offset((*i.borrow()) as isize) + .upgrade() + .deref()) + .x + } == 1) ); assert!( - ((*p3_0.borrow()) - .offset((*i.borrow()) as isize) - .with(|__s: &Pair| __s.y) - == 2) + ({ + (*(*p3_0.borrow()) + .offset((*i.borrow()) as isize) + .upgrade() + .deref()) + .y + } == 2) ); ({ PairImpl::inc( @@ -178,25 +190,33 @@ pub fn RndStuff_2() { ) }); assert!( - ((*p3_0.borrow()) - .offset((*i.borrow()) as isize) - .with(|__s: &Pair| __s.x) - == 11) + ({ + (*(*p3_0.borrow()) + .offset((*i.borrow()) as isize) + .upgrade() + .deref()) + .x + } == 11) ); assert!( - ((*p3_0.borrow()) - .offset((*i.borrow()) as isize) - .with(|__s: &Pair| __s.y) - == 12) + ({ + (*(*p3_0.borrow()) + .offset((*i.borrow()) as isize) + .upgrade() + .deref()) + .y + } == 12) ); (*i.borrow_mut()).prefix_inc(); } - (*x3.borrow_mut()) = Ptr::alloc_array( - (0..50_usize) - .map(|_| ::default()) - .collect::>(), - ) - .to_owned_opt(); + (x3.as_pointer() as Ptr>>>).write( + Ptr::alloc_array( + (0..50_usize) + .map(|_| ::default()) + .collect::>(), + ) + .to_owned_opt(), + ); let i: Value = Rc::new(RefCell::new(0)); 'loop_: while ((*i.borrow()) < 50) { (*x3.borrow()).as_ref().unwrap().borrow_mut()[((*i.borrow()) as usize) as usize] = Pair { @@ -213,16 +233,22 @@ pub fn RndStuff_2() { let i: Value = Rc::new(RefCell::new(0)); 'loop_: while ((*i.borrow()) < 50) { assert!( - ((*p3_1.borrow()) - .offset((*i.borrow()) as isize) - .with(|__s: &Pair| __s.x) - == -1_i32) + ({ + (*(*p3_1.borrow()) + .offset((*i.borrow()) as isize) + .upgrade() + .deref()) + .x + } == -1_i32) ); assert!( - ((*p3_1.borrow()) - .offset((*i.borrow()) as isize) - .with(|__s: &Pair| __s.y) - == -2_i32) + ({ + (*(*p3_1.borrow()) + .offset((*i.borrow()) as isize) + .upgrade() + .deref()) + .y + } == -2_i32) ); ({ PairImpl::inc( @@ -235,16 +261,22 @@ pub fn RndStuff_2() { ) }); assert!( - ((*p3_1.borrow()) - .offset((*i.borrow()) as isize) - .with(|__s: &Pair| __s.x) - == -11_i32) + ({ + (*(*p3_1.borrow()) + .offset((*i.borrow()) as isize) + .upgrade() + .deref()) + .x + } == -11_i32) ); assert!( - ((*p3_1.borrow()) - .offset((*i.borrow()) as isize) - .with(|__s: &Pair| __s.y) - == -12_i32) + ({ + (*(*p3_1.borrow()) + .offset((*i.borrow()) as isize) + .upgrade() + .deref()) + .y + } == -12_i32) ); (*i.borrow_mut()).prefix_inc(); } @@ -272,8 +304,14 @@ pub trait PairImpl { impl PairImpl for Ptr { fn inc(&self, k: i32) { let k: Value = Rc::new(RefCell::new(k)); - (*self).with_mut(|__s: &mut Pair| __s.x += (*k.borrow())); - (*self).with_mut(|__s: &mut Pair| __s.y += (*k.borrow())); + { + let _ptr = field!((*self), x); + _ptr.write(_ptr.read() + (*k.borrow())) + }; + { + let _ptr = field!((*self), y); + _ptr.write(_ptr.read() + (*k.borrow())) + }; } } pub trait SafePointerImpl { @@ -282,8 +320,8 @@ pub trait SafePointerImpl { } impl SafePointerImpl for Ptr { fn inc(&self) { - (*(*(*self).upgrade().deref()) - .ptr + (*(*self) + .with(|__s| __s.ptr.clone()) .as_ref() .unwrap() .borrow_mut()) @@ -291,7 +329,7 @@ impl SafePointerImpl for Ptr { } fn move_assign(&self, _a0: Ptr) -> Ptr { (field_ptr!((*self), ptr) as Ptr>>) - .write(_a0.with_mut(|__s: &mut SafePointer| __s.ptr.take())); + .write(field!(_a0, ptr).with_mut(|__v: &mut Option>| __v.take())); return (*self).clone(); } } diff --git a/tests/unit/out/refcount/unique_ptr_const_deref.rs b/tests/unit/out/refcount/unique_ptr_const_deref.rs index 6c7bd3d39f..5fa5e3b831 100644 --- a/tests/unit/out/refcount/unique_ptr_const_deref.rs +++ b/tests/unit/out/refcount/unique_ptr_const_deref.rs @@ -14,7 +14,7 @@ pub struct Holder { impl Holder { pub fn move_from(_a0: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - val: { _a0.with_mut(|__s: &mut Holder| __s.val.take()) }, + val: field!(_a0, val).with_mut(|__v: &mut Option>| __v.take()), })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() @@ -39,8 +39,8 @@ impl ByteRepr for Holder { >>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub fn read_val_0(h: Ptr) -> i32 { let h: Value> = Rc::new(RefCell::new(h)); - return (*(*(*h.borrow()).upgrade().deref()) - .val + return (*(*h.borrow()) + .with(|__s| __s.val.clone()) .as_ref() .unwrap() .borrow()); @@ -48,8 +48,8 @@ pub fn read_val_0(h: Ptr) -> i32 { pub fn write_val_1(h: Ptr, v: i32) { let h: Value> = Rc::new(RefCell::new(h)); let v: Value = Rc::new(RefCell::new(v)); - (*(*(*h.borrow()).upgrade().deref()) - .val + (*(*h.borrow()) + .with(|__s| __s.val.clone()) .as_ref() .unwrap() .borrow_mut()) = (*v.borrow()); @@ -60,7 +60,8 @@ pub fn main() { } fn main_0() -> i32 { let h: Value = Rc::new(RefCell::new(::default())); - (field_ptr!(h, val) as Ptr>>).write(Some(Rc::new(RefCell::new(10))).take()); + (field_ptr!(h.as_pointer(), val) as Ptr>>) + .write(Some(Rc::new(RefCell::new(10))).take()); ({ write_val_1((h.as_pointer()), 42) }); assert!((({ read_val_0((h.as_pointer()),) }) == 42)); return 0; @@ -71,7 +72,7 @@ pub trait HolderImpl { impl HolderImpl for Ptr { fn move_assign(&self, _a0: Ptr) -> Ptr { (field_ptr!((*self), val) as Ptr>>) - .write(_a0.with_mut(|__s: &mut Holder| __s.val.take())); + .write(field!(_a0, val).with_mut(|__v: &mut Option>| __v.take())); return (*self).clone(); } } diff --git a/tests/unit/out/refcount/unique_ptr_field_reset.rs b/tests/unit/out/refcount/unique_ptr_field_reset.rs index 087b435914..2354c8bcd6 100644 --- a/tests/unit/out/refcount/unique_ptr_field_reset.rs +++ b/tests/unit/out/refcount/unique_ptr_field_reset.rs @@ -34,8 +34,8 @@ pub struct Holder { impl Holder { pub fn move_from(_a0: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - data: { _a0.with_mut(|__s: &mut Holder| __s.data.take()) }, - n: { _a0.with(|__s: &Holder| __s.n) }, + data: field!(_a0, data).with_mut(|__v: &mut Option>| __v.take()), + n: { (*_a0.upgrade().deref()).n }, })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() @@ -65,36 +65,29 @@ fn main_0() -> i32 { (*h.borrow_mut()).n = 1; let hp: Value> = Rc::new(RefCell::new((h.as_pointer()))); { - { - let __a1 = Ptr::alloc(Data { v: 3 }); - { - (*hp.borrow()).with_mut(|__s: &mut Holder| { - let _p: Ptr<_> = __a1; - __s.data = _p.to_owned_opt() - }) - } - } + let _p: Ptr<_> = Ptr::alloc(Data { v: 3 }); + (field_ptr!((*hp.borrow()), data) as Ptr>>).write(_p.to_owned_opt()) }; - assert!(({ (*(*h.borrow()).data.as_ref().unwrap().borrow()).v } == 3)); + assert!(({ (*{ (*h.borrow()).data.clone() }.as_ref().unwrap().borrow()).v } == 3)); ({ HolderImpl::set(&h.as_pointer(), Ptr::alloc(Data { v: 4 })) }); assert!( ({ - (*(*(*hp.borrow()).upgrade().deref()) - .data + (*(*hp.borrow()) + .with(|__s| __s.data.clone()) .as_ref() .unwrap() .borrow()) .v } == 4) ); - let __rhs = (*hp.borrow()).with(|__s: &Holder| __s.n); - (*(*(*hp.borrow()).upgrade().deref()) - .data + let __rhs = (*hp.borrow()).with(|__s| __s.n); + (*(*hp.borrow()) + .with(|__s| __s.data.clone()) .as_ref() .unwrap() .borrow_mut()) .v += __rhs; - assert!(({ (*(*h.borrow()).data.as_ref().unwrap().borrow()).v } == 5)); + assert!(({ (*{ (*h.borrow()).data.clone() }.as_ref().unwrap().borrow()).v } == 5)); return 0; } pub trait HolderImpl { @@ -105,17 +98,15 @@ impl HolderImpl for Ptr { fn set(&self, p: Ptr) { let p: Value> = Rc::new(RefCell::new(p)); { - (*self).with_mut(|__s: &mut Holder| { - let _p: Ptr<_> = (*p.borrow()).clone(); - __s.data = _p.to_owned_opt() - }) + let _p: Ptr<_> = (*p.borrow()).clone(); + (field_ptr!((*self), data) as Ptr>>).write(_p.to_owned_opt()) }; } fn move_assign(&self, _a0: Ptr) -> Ptr { (field_ptr!((*self), data) as Ptr>>) - .write(_a0.with_mut(|__s: &mut Holder| __s.data.take())); - let __rhs = _a0.with(|__s: &Holder| __s.n); - (*self).with_mut(|__s: &mut Holder| __s.n = __rhs); + .write(field!(_a0, data).with_mut(|__v: &mut Option>| __v.take())); + let __rhs = { (*_a0.upgrade().deref()).n }; + field!((*self), n).write(__rhs); return (*self).clone(); } } diff --git a/tests/unit/out/refcount/unique_ptr_nested.rs b/tests/unit/out/refcount/unique_ptr_nested.rs index 0ffe61810b..02b4196842 100644 --- a/tests/unit/out/refcount/unique_ptr_nested.rs +++ b/tests/unit/out/refcount/unique_ptr_nested.rs @@ -36,7 +36,7 @@ pub struct Outer { impl Outer { pub fn move_from(_a0: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - inner: { _a0.with_mut(|__s: &mut Outer| __s.inner.take()) }, + inner: field!(_a0, inner).with_mut(|__v: &mut Option>| __v.take()), })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() @@ -70,23 +70,20 @@ fn main_0() -> i32 { })); Outer::move_from({ __tmp_0.as_pointer() }) }))))); - (*(*(*o.borrow()).as_ref().unwrap().borrow()) - .inner + (*{ (*(*o.borrow()).as_ref().unwrap().borrow()).inner.clone() } .as_ref() .unwrap() .borrow_mut()) .x += 5; let sum: Value = Rc::new(RefCell::new( ({ - (*(*(*o.borrow()).as_ref().unwrap().borrow()) - .inner + (*{ (*(*o.borrow()).as_ref().unwrap().borrow()).inner.clone() } .as_ref() .unwrap() .borrow()) .x } + { - (*(*(*o.borrow()).as_ref().unwrap().borrow()) - .inner + (*{ (*(*o.borrow()).as_ref().unwrap().borrow()).inner.clone() } .as_ref() .unwrap() .borrow()) @@ -106,7 +103,7 @@ pub trait OuterImpl { impl OuterImpl for Ptr { fn move_assign(&self, _a0: Ptr) -> Ptr { (field_ptr!((*self), inner) as Ptr>>) - .write(_a0.with_mut(|__s: &mut Outer| __s.inner.take())); + .write(field!(_a0, inner).with_mut(|__v: &mut Option>| __v.take())); return (*self).clone(); } } diff --git a/tests/unit/out/refcount/va_arg_non_primitive_ptrs.rs b/tests/unit/out/refcount/va_arg_non_primitive_ptrs.rs index a1f9235fe2..5d3bfbc50d 100644 --- a/tests/unit/out/refcount/va_arg_non_primitive_ptrs.rs +++ b/tests/unit/out/refcount/va_arg_non_primitive_ptrs.rs @@ -57,7 +57,7 @@ pub fn dispatch_0(option: i32, __args: &[VaArg]) -> i32 { __v if __v == (opt_OPT_NODE as i32) => { let n: Value> = Rc::new(RefCell::new((*ap.borrow_mut()).arg::>())); - (*result.borrow_mut()) = (*n.borrow()).with(|__s: &node| __s.data); + (*result.borrow_mut()) = (*n.borrow()).with(|__s| __s.data); break 'switch; } __v if __v == (opt_OPT_NODE_OUT as i32) => { diff --git a/tests/unit/out/refcount/va_arg_struct_ctx.rs b/tests/unit/out/refcount/va_arg_struct_ctx.rs index bc3a6c03e3..1c0371787c 100644 --- a/tests/unit/out/refcount/va_arg_struct_ctx.rs +++ b/tests/unit/out/refcount/va_arg_struct_ctx.rs @@ -31,11 +31,10 @@ impl ByteRepr for context { pub fn set_error_0(ctx: Ptr, fmt: Ptr, __args: &[VaArg]) { let ctx: Value> = Rc::new(RefCell::new(ctx)); let fmt: Value> = Rc::new(RefCell::new(fmt)); - if ((*ctx.borrow()).with(|__s: &context| __s.verbose) != 0) { + if ((*ctx.borrow()).with(|__s| __s.verbose) != 0) { let ap: Value = Rc::new(RefCell::new(VaList::default())); (*ap.borrow_mut()) = VaList::new(__args); - let __rhs = (*ap.borrow_mut()).arg::(); - (*ctx.borrow()).with_mut(|__s: &mut context| __s.last_error = __rhs); + field!((*ctx.borrow()), last_error).write((*ap.borrow_mut()).arg::()); } } pub fn main() { diff --git a/tests/unit/out/refcount/va_arg_void_ptr.rs b/tests/unit/out/refcount/va_arg_void_ptr.rs index 46c301ca1d..0c7cb433a5 100644 --- a/tests/unit/out/refcount/va_arg_void_ptr.rs +++ b/tests/unit/out/refcount/va_arg_void_ptr.rs @@ -41,13 +41,11 @@ pub fn registry_update_0(r: Ptr, field: field, __args: &[VaArg]) -> i3 let __match_cond = ((*field.borrow()) as u32); match __match_cond { __v if __v == ((field_FIELD_SLOT as i32) as u32) => { - let __rhs = (*ap.borrow_mut()).arg::(); - (*r.borrow()).with_mut(|__s: &mut registry| __s.slot = __rhs); + field!((*r.borrow()), slot).write((*ap.borrow_mut()).arg::()); break 'switch; } __v if __v == ((field_FIELD_LEVEL as i32) as u32) => { - let __rhs = (*ap.borrow_mut()).arg::(); - (*r.borrow()).with_mut(|__s: &mut registry| __s.level = __rhs); + field!((*r.borrow()), level).write((*ap.borrow_mut()).arg::()); break 'switch; } _ => { diff --git a/tests/unit/out/refcount/vector_subscript_direct.rs b/tests/unit/out/refcount/vector_subscript_direct.rs index 7f4a0297a6..954c5479cb 100644 --- a/tests/unit/out/refcount/vector_subscript_direct.rs +++ b/tests/unit/out/refcount/vector_subscript_direct.rs @@ -37,12 +37,10 @@ pub struct Holder { } impl Clone for Holder { fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - values: { Rc::new(RefCell::new((*self.values.borrow()).clone())) }, - points: { Rc::new(RefCell::new((*self.points.borrow()).clone())) }, - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() + Self { + values: Rc::new(RefCell::new((*self.values.borrow()).clone())), + points: Rc::new(RefCell::new((*self.points.borrow()).clone())), + } } } impl ByteRepr for Holder { @@ -108,9 +106,9 @@ fn main_0() -> i32 { == 7) ); let i: Value = Rc::new(RefCell::new(0)); - let __rhs = 3; - let __idx = ((*i.borrow_mut()).postfix_inc() as usize); - (v.as_pointer() as Ptr).offset(__idx).write(__rhs); + (v.as_pointer() as Ptr) + .offset(((*i.borrow_mut()).postfix_inc() as usize)) + .write(3); assert!(((*i.borrow()) == 1) && (((v.as_pointer() as Ptr).offset(0_usize).read()) == 3)); assert!( (((v.as_pointer() as Ptr) @@ -118,9 +116,9 @@ fn main_0() -> i32 { .read()) == 42) ); - let __rhs = 5; - let __idx = (({ push_and_index_0(v.as_pointer()) }) as usize); - (v.as_pointer() as Ptr).offset(__idx).write(__rhs); + (v.as_pointer() as Ptr) + .offset((({ push_and_index_0(v.as_pointer()) }) as usize)) + .write(5); assert!( ((*v.borrow()).len() == 5_usize) && (((v.as_pointer() as Ptr).offset(4_usize).read()) == 5) @@ -132,34 +130,76 @@ fn main_0() -> i32 { let h: Value = Rc::new(RefCell::new(::default())); { let __a0 = 2_usize as usize; - (*(*h.borrow()).values.borrow_mut()).resize_with(__a0, || ::default()) + (*{ (*h.borrow()).values.clone() }.borrow_mut()).resize_with(__a0, || ::default()) }; - (*(*h.borrow()).values.borrow_mut())[(1_usize) as usize] = 6; - (*(*h.borrow()).points.borrow_mut()).push(Point { x: 1, y: 2 }); - (*(*h.borrow()).points.borrow_mut())[(0_usize) as usize].y = 5; - assert!(({ (*(*h.borrow()).values.borrow())[(1_usize) as usize] } == 6)); + ({ (*h.borrow()).values.clone() }.as_pointer() as Ptr) + .offset(1_usize) + .write(6); + (*{ (*h.borrow()).points.clone() }.borrow_mut()).push(Point { x: 1, y: 2 }); + field!( + ({ (*h.borrow()).points.clone() }.as_pointer() as Ptr).offset(0_usize), + y + ) + .write(5); + assert!( + ((({ (*h.borrow()).values.clone() }.as_pointer() as Ptr) + .offset(1_usize) + .read()) + == 6) + ); assert!( (({ - PointImpl::sum(&{ - ({ (*h.borrow()).points.as_pointer() } as Ptr).offset(0_usize) - }) + PointImpl::sum( + &({ (*h.borrow()).points.clone() }.as_pointer() as Ptr).offset(0_usize), + ) }) == 6) ); let hp: Value> = Rc::new(RefCell::new((h.as_pointer()))); - let __rhs = - ((*hp.borrow()).with(|__s: &Holder| (*__s.values.borrow())[(1_usize) as usize]) + 1); - (*hp.borrow()).with(|__s: &Holder| (*__s.values.borrow_mut())[(0_usize) as usize] = __rhs); - let __rhs = (*hp.borrow()).with(|__s: &Holder| (*__s.values.borrow())[(0_usize) as usize]); - (*hp.borrow()).with(|__s: &Holder| (*__s.points.borrow_mut())[(0_usize) as usize].x = __rhs); + let __rhs = ((((*hp.borrow()).with(|__s| __s.values.clone()).as_pointer() as Ptr) + .offset(1_usize) + .read()) + + 1); + ((*hp.borrow()).with(|__s| __s.values.clone()).as_pointer() as Ptr) + .offset(0_usize) + .write(__rhs); + let __rhs = (((*hp.borrow()).with(|__s| __s.values.clone()).as_pointer() as Ptr) + .offset(0_usize) + .read()); + field!( + ((*hp.borrow()).with(|__s| __s.points.clone()).as_pointer() as Ptr).offset(0_usize), + x + ) + .write(__rhs); assert!( - ({ (*(*h.borrow()).values.borrow())[(0_usize) as usize] } == 7) - && ({ (*(*h.borrow()).points.borrow())[(0_usize) as usize].x } == 7) + ((({ (*h.borrow()).values.clone() }.as_pointer() as Ptr) + .offset(0_usize) + .read()) + == 7) + && ({ + (*({ (*h.borrow()).points.clone() }.as_pointer() as Ptr) + .offset(0_usize) + .upgrade() + .deref()) + .x + } == 7) ); let q: Value = Rc::new(RefCell::new( - ((*(*h.borrow()).points.borrow())[(0_usize) as usize]).clone(), + (*({ (*h.borrow()).points.clone() }.as_pointer() as Ptr) + .offset(0_usize) + .upgrade() + .deref()) + .clone(), )); (*q.borrow_mut()).x = 0; - assert!(({ (*(*h.borrow()).points.borrow())[(0_usize) as usize].x } == 7)); + assert!( + ({ + (*({ (*h.borrow()).points.clone() }.as_pointer() as Ptr) + .offset(0_usize) + .upgrade() + .deref()) + .x + } == 7) + ); let grid: Value>>> = Rc::new(RefCell::new(Vec::new())); (grid.as_pointer() as Ptr>>>).with_mut(|__v: &mut Vec>>| { __v.push(Rc::new(RefCell::new(vec![0; 3_usize as usize]))) @@ -204,7 +244,7 @@ pub trait PointImpl { } impl PointImpl for Ptr { fn sum(&self) -> i32 { - return ((*self).with(|__s: &Point| __s.x) + (*self).with(|__s: &Point| __s.y)); + return ((*self).with(|__s| __s.x) + (*self).with(|__s| __s.y)); } } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/void_cast.rs b/tests/unit/out/refcount/void_cast.rs index c36593e0bd..223dc14108 100644 --- a/tests/unit/out/refcount/void_cast.rs +++ b/tests/unit/out/refcount/void_cast.rs @@ -17,11 +17,9 @@ pub struct NonTrivial { } impl Clone for NonTrivial { fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - data: { Rc::new(RefCell::new((*self.data.borrow()).clone())) }, - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() + Self { + data: Rc::new(RefCell::new((*self.data.borrow()).clone())), + } } } <<<<<<< HEAD @@ -81,7 +79,7 @@ pub struct NonCopyable { impl NonCopyable { pub fn move_from(_a0: Ptr) -> Self { let __this: Value = Rc::new(RefCell::new(Self { - value: { _a0.with_mut(|__s: &mut NonCopyable| __s.value.take()) }, + value: field!(_a0, value).with_mut(|__v: &mut Option>| __v.take()), })); let this: Ptr = __this.as_pointer(); Rc::try_unwrap(__this).ok().unwrap().into_inner() @@ -177,7 +175,7 @@ fn main_0() -> i32 { (&(*g.borrow_mut())); &(*g.borrow_mut()); ({ unused_noncopyable_param_5(g.as_pointer()) }); - assert!(((*(*g.borrow()).value.as_ref().unwrap().borrow()) == 9)); + assert!(((*{ (*g.borrow()).value.clone() }.as_ref().unwrap().borrow()) == 9)); return 0; } pub trait NonCopyableImpl { @@ -186,7 +184,7 @@ pub trait NonCopyableImpl { impl NonCopyableImpl for Ptr { fn move_assign(&self, _a0: Ptr) -> Ptr { (field_ptr!((*self), value) as Ptr>>) - .write(_a0.with_mut(|__s: &mut NonCopyable| __s.value.take())); + .write(field!(_a0, value).with_mut(|__v: &mut Option>| __v.take())); return (*self).clone(); } } From ad4d0bea44dff74519c52f366bd45e0e0f821b0e Mon Sep 17 00:00:00 2001 From: Nuno Lopes Date: Wed, 30 Sep 2026 14:40:04 +0100 Subject: [PATCH 08/13] fix --- cpp2rust/converter/models/converter_refcount.cpp | 11 ++++++----- docs/src/codegen/types/pointers.md | 7 ++++--- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/cpp2rust/converter/models/converter_refcount.cpp b/cpp2rust/converter/models/converter_refcount.cpp index afdd8fc773..206b446e0d 100644 --- a/cpp2rust/converter/models/converter_refcount.cpp +++ b/cpp2rust/converter/models/converter_refcount.cpp @@ -518,7 +518,8 @@ bool ConverterRefCount::RecordDerivesClone(const clang::RecordDecl *decl) { } PushConversionKind push(*this, ConversionKind::Pointee); return std::ranges::none_of(decl->fields(), [&](auto *field) { - return IsValueField(ctx_, field) || ToString(field->getType()).contains("Value<"); + return IsValueField(ctx_, field) || + ToString(field->getType()).contains("Value<"); }); } @@ -553,8 +554,8 @@ void ConverterRefCount::AddCloneTrait(const clang::RecordDecl *decl) { auto name = GetNamedDeclAsString(field); auto value = std::format("self.{}.clone()", name); if (IsValueField(ctx_, field)) { - value = std::format("Rc::new(RefCell::new((*self.{}.borrow()).clone()))", - name); + value = std::format( + "Rc::new(RefCell::new((*self.{}.borrow()).clone()))", name); } StrCat(std::format("{}: {},", name, value)); } @@ -3209,8 +3210,8 @@ ConverterRefCount::DestroyMembers(const clang::CXXRecordDecl *decl) { "0..__p.len() {{ {2}::{1}(&__p.offset(__i as isize)); }} }}\n", name, kDestructorName, TraitName(elem)); } else { - out += std::format("field_ptr!(self, {0}).{1}();\n", name, - kDestructorName); + out += + std::format("field_ptr!(self, {0}).{1}();\n", name, kDestructorName); } } return out; diff --git a/docs/src/codegen/types/pointers.md b/docs/src/codegen/types/pointers.md index 7f0fdcba3e..13c1494089 100644 --- a/docs/src/codegen/types/pointers.md +++ b/docs/src/codegen/types/pointers.md @@ -88,8 +88,8 @@ of the dereference: and so is a whole record, `*p`. A field of a record pointee is copied out in a closure that borrows the record for its duration: `p->x` is `p.with(|__s| __s.x)`, and `p->a.b` is `p.with(|__s| __s.a.b)`; `ReadField` - converts the record with `record_ptr_` set, so that its dereference is - emitted as `__s`. A field that is a [`Value` of its own](boxing.md), or a + converts the record with `record_ptr_` set, so that its dereference is emitted + as `__s`. A field that is a [`Value` of its own](boxing.md), or a `std::unique_ptr`, is copied out as well, i.e., its `Rc`: `p->v.size()` is `(*p.with(|__s| __s.v.clone()).borrow()).len()`. When the record is not reached through a pointer, the copy is in a block, `{ (*s.borrow()).x }`. In @@ -106,7 +106,8 @@ of the dereference: pointee is a pending dereference too, of `field!(p, x)`, which projects the pointer to the field (see [Pointers to fields](../../runtime/rc.md#pointers-to-fields)): `p->x = v` is - `field!(p, x).write(v)`, and `p->a.b = v` is `field!(field!(p, a), b).write(v)`. + `field!(p, x).write(v)`, and `p->a.b = v` is + `field!(field!(p, a), b).write(v)`. `with` and `with_mut` work on any pointer, including a [reinterpreted](../../runtime/reinterpret.md) one, whose pointee is decoded from From 9c3ffc70281389463ad2a18d0093f87149dedd24 Mon Sep 17 00:00:00 2001 From: Nuno Lopes Date: Wed, 30 Sep 2026 15:12:04 +0100 Subject: [PATCH 09/13] fix --- rules/vector/tgt_unsafe.rs | 3 +- .../out/refcount/defaulted_move_cross_tu.rs | 14 -- .../refcount/out_of_line_methods_cross_tu.rs | 33 ---- tests/unit/container_field_addr.cpp | 1 - tests/unit/field_pointers_malloced_struct.cpp | 1 - tests/unit/first_field_ptr_covers_struct.c | 1 - tests/unit/first_member_downcast.c | 1 - tests/unit/memset_struct_tail.c | 1 - .../out/refcount/array_of_noncopy_struct.rs | 19 +- tests/unit/out/refcount/auto.rs | 10 +- tests/unit/out/refcount/class_templates.rs | 39 ----- tests/unit/out/refcount/clone_vs_move.rs | 5 +- .../unit/out/refcount/compound_assign_ref.rs | 5 +- tests/unit/out/refcount/constructor.rs | 22 --- .../unit/out/refcount/container_field_addr.rs | 69 ++++---- tests/unit/out/refcount/copy_assign.rs | 59 ------- tests/unit/out/refcount/copy_ctor.rs | 57 ------ .../unit/out/refcount/copy_move_defaulted.rs | 118 ++----------- tests/unit/out/refcount/copy_move_deleted.rs | 40 ----- tests/unit/out/refcount/destructor.rs | 140 --------------- tests/unit/out/refcount/field_aliasing.rs | 163 +++++++----------- .../out/refcount/field_method_receiver.rs | 125 +++++++------- tests/unit/out/refcount/field_pointers.rs | 24 +-- .../field_pointers_malloced_struct.rs | 111 ++++++------ tests/unit/out/refcount/find.rs | 15 +- .../refcount/first_field_ptr_covers_struct.rs | 127 +++++--------- .../out/refcount/first_member_downcast.rs | 74 +++----- .../flex_array_member_sibling_write.rs | 124 ++++++------- tests/unit/out/refcount/fn_ptr_stable_sort.rs | 15 +- .../refcount/foreach_disjoint_field_borrow.rs | 19 +- tests/unit/out/refcount/foreach_double.rs | 15 +- tests/unit/out/refcount/foreach_mut.rs | 30 +++- tests/unit/out/refcount/forward_basic.rs | 28 --- tests/unit/out/refcount/forward_decltype.rs | 16 -- tests/unit/out/refcount/forward_explicit.rs | 16 -- tests/unit/out/refcount/forward_pack.rs | 30 ---- tests/unit/out/refcount/huffman.rs | 20 --- tests/unit/out/refcount/implicit_autoref.rs | 32 ++-- tests/unit/out/refcount/iterators.rs | 10 +- tests/unit/out/refcount/kruskal.rs | 33 ---- .../refcount/libc_struct_without_default.rs | 15 -- tests/unit/out/refcount/map.rs | 15 +- tests/unit/out/refcount/memset_struct_tail.rs | 88 +++++----- tests/unit/out/refcount/move_assign.rs | 30 +--- tests/unit/out/refcount/move_ctor.rs | 69 +------- tests/unit/out/refcount/move_this.rs | 12 -- tests/unit/out/refcount/push_emplace_back.rs | 27 +-- .../refcount/reinterpret_cast_vec_write.rs | 10 +- tests/unit/out/refcount/rule_of_five.rs | 14 -- tests/unit/out/refcount/rule_of_three.rs | 14 -- tests/unit/out/refcount/sort.rs | 50 ++++-- .../out/refcount/struct_2d_array_field.rs | 49 +++--- .../refcount/struct_member_array_adjacency.rs | 8 +- .../unit/out/refcount/typedef-anon-struct.rs | 12 -- tests/unit/out/refcount/unique_ptr.rs | 13 -- .../out/refcount/unique_ptr_const_deref.rs | 12 -- .../out/refcount/unique_ptr_field_reset.rs | 10 -- tests/unit/out/refcount/unique_ptr_nested.rs | 12 -- tests/unit/out/refcount/vector.rs | 20 ++- tests/unit/out/refcount/vector2.rs | 50 ++++-- .../out/refcount/vector_subscript_direct.rs | 20 +-- tests/unit/out/refcount/void_cast.rs | 24 --- .../out/unsafe/array_of_noncopy_struct.rs | 5 +- tests/unit/out/unsafe/auto.rs | 10 +- tests/unit/out/unsafe/clone_vs_move.rs | 5 +- tests/unit/out/unsafe/compound_assign_ref.rs | 5 +- tests/unit/out/unsafe/container_field_addr.rs | 5 +- tests/unit/out/unsafe/copy_move_defaulted.rs | 15 +- .../unit/out/unsafe/field_method_receiver.rs | 5 +- tests/unit/out/unsafe/field_pointers.rs | 5 +- tests/unit/out/unsafe/find.rs | 15 +- tests/unit/out/unsafe/fn_ptr_stable_sort.rs | 15 +- .../unsafe/foreach_disjoint_field_borrow.rs | 5 +- tests/unit/out/unsafe/foreach_double.rs | 15 +- tests/unit/out/unsafe/foreach_mut.rs | 30 +++- tests/unit/out/unsafe/implicit_autoref.rs | 20 ++- tests/unit/out/unsafe/iterators.rs | 10 +- tests/unit/out/unsafe/map.rs | 15 +- tests/unit/out/unsafe/move_assign.rs | 5 +- tests/unit/out/unsafe/move_ctor.rs | 10 +- tests/unit/out/unsafe/push_emplace_back.rs | 5 +- .../out/unsafe/reinterpret_cast_vec_write.rs | 10 +- tests/unit/out/unsafe/sort.rs | 50 ++++-- tests/unit/out/unsafe/vector.rs | 20 ++- tests/unit/out/unsafe/vector2.rs | 50 ++++-- tests/unit/union_cross_arm_cast.c | 1 - 86 files changed, 948 insertions(+), 1622 deletions(-) diff --git a/rules/vector/tgt_unsafe.rs b/rules/vector/tgt_unsafe.rs index c63add8533..efa218c54f 100644 --- a/rules/vector/tgt_unsafe.rs +++ b/rules/vector/tgt_unsafe.rs @@ -80,7 +80,8 @@ unsafe fn f13(a0: &mut Vec) -> *mut T1 { a0.as_mut_ptr() } unsafe fn f14(a0: &mut Vec, a1: &mut T1) { - a0.push(std::mem::take(&mut *a1)) + let __a1 = std::mem::take(&mut *a1); + a0.push(__a1) } unsafe fn f15(a0: &mut Vec, a1: usize) { let __a0 = a1 as usize; diff --git a/tests/multi-file/defaulted_move_cross_tu/out/refcount/defaulted_move_cross_tu.rs b/tests/multi-file/defaulted_move_cross_tu/out/refcount/defaulted_move_cross_tu.rs index afbb21b457..17d66178da 100644 --- a/tests/multi-file/defaulted_move_cross_tu/out/refcount/defaulted_move_cross_tu.rs +++ b/tests/multi-file/defaulted_move_cross_tu/out/refcount/defaulted_move_cross_tu.rs @@ -47,25 +47,11 @@ impl Default for S { } } } -<<<<<<< HEAD -impl ByteRepr for S {} -======= impl ByteRepr for S { fn byte_size() -> usize { 32 } - fn to_bytes(&self, buf: &mut [u8]) { - (*self.v.borrow()).to_bytes(&mut buf[0..24]); - (*self.n.borrow()).to_bytes(&mut buf[24..32]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - v: Rc::new(RefCell::new(>::from_bytes(&buf[0..24]))), - n: Rc::new(RefCell::new(>::from_bytes(&buf[24..32]))), - } - } } ->>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub fn sum_0(s: Ptr) -> i32 { return { let _lhs = { diff --git a/tests/multi-file/out_of_line_methods_cross_tu/out/refcount/out_of_line_methods_cross_tu.rs b/tests/multi-file/out_of_line_methods_cross_tu/out/refcount/out_of_line_methods_cross_tu.rs index 364506b716..a5a5b80c79 100644 --- a/tests/multi-file/out_of_line_methods_cross_tu/out/refcount/out_of_line_methods_cross_tu.rs +++ b/tests/multi-file/out_of_line_methods_cross_tu/out/refcount/out_of_line_methods_cross_tu.rs @@ -12,32 +12,11 @@ pub struct S { pub v: i32, } impl S {} -<<<<<<< HEAD -impl Clone for S { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*self.v.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } -} -impl ByteRepr for S {} -======= impl ByteRepr for S { fn byte_size() -> usize { 4 } - fn to_bytes(&self, buf: &mut [u8]) { - self.v.to_bytes(&mut buf[0..4]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - v: ::from_bytes(&buf[0..4]), - } - } } ->>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub trait Base { fn apply(&mut self, x: i32) -> i32; } @@ -56,23 +35,11 @@ impl Clone for Derived { Rc::try_unwrap(__this).ok().unwrap().into_inner() } } -<<<<<<< HEAD -impl ByteRepr for Derived {} -======= impl ByteRepr for Derived { fn byte_size() -> usize { 16 } - fn to_bytes(&self, buf: &mut [u8]) { - self.factor.to_bytes(&mut buf[8..12]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - factor: ::from_bytes(&buf[8..12]), - } - } } ->>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub fn main() { __cpp2rust_init_globals(); std::process::exit(main_0()); diff --git a/tests/unit/container_field_addr.cpp b/tests/unit/container_field_addr.cpp index 1de76fb75a..a5d26fc13d 100644 --- a/tests/unit/container_field_addr.cpp +++ b/tests/unit/container_field_addr.cpp @@ -1,4 +1,3 @@ -// panic: refcount #include #include #include diff --git a/tests/unit/field_pointers_malloced_struct.cpp b/tests/unit/field_pointers_malloced_struct.cpp index 76af7825db..f042aa41aa 100644 --- a/tests/unit/field_pointers_malloced_struct.cpp +++ b/tests/unit/field_pointers_malloced_struct.cpp @@ -1,4 +1,3 @@ -// panic: refcount #include #include diff --git a/tests/unit/first_field_ptr_covers_struct.c b/tests/unit/first_field_ptr_covers_struct.c index 0eaff4dbb8..f06cd24198 100644 --- a/tests/unit/first_field_ptr_covers_struct.c +++ b/tests/unit/first_field_ptr_covers_struct.c @@ -1,4 +1,3 @@ -// panic: refcount #include #include #include diff --git a/tests/unit/first_member_downcast.c b/tests/unit/first_member_downcast.c index 04a9432afa..4e10ce4753 100644 --- a/tests/unit/first_member_downcast.c +++ b/tests/unit/first_member_downcast.c @@ -1,4 +1,3 @@ -// panic: refcount #include #include diff --git a/tests/unit/memset_struct_tail.c b/tests/unit/memset_struct_tail.c index 326d24f78f..cb3a64914b 100644 --- a/tests/unit/memset_struct_tail.c +++ b/tests/unit/memset_struct_tail.c @@ -1,4 +1,3 @@ -// panic: refcount #include #include #include diff --git a/tests/unit/out/refcount/array_of_noncopy_struct.rs b/tests/unit/out/refcount/array_of_noncopy_struct.rs index 6496aba4cd..8f9459d222 100644 --- a/tests/unit/out/refcount/array_of_noncopy_struct.rs +++ b/tests/unit/out/refcount/array_of_noncopy_struct.rs @@ -29,25 +29,11 @@ impl Default for NonCopy { } } } -<<<<<<< HEAD -impl ByteRepr for NonCopy {} -======= impl ByteRepr for NonCopy { fn byte_size() -> usize { 32 } - fn to_bytes(&self, buf: &mut [u8]) { - (*self.data.borrow()).to_bytes(&mut buf[0..24]); - self.tag.to_bytes(&mut buf[24..28]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - data: Rc::new(RefCell::new(>::from_bytes(&buf[0..24]))), - tag: ::from_bytes(&buf[24..28]), - } - } } ->>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub fn main() { __cpp2rust_init_globals(); std::process::exit(main_0()); @@ -59,7 +45,10 @@ fn main_0() -> i32 { .collect::>(), )); (*arr.borrow_mut())[(0) as usize].tag = 7; - (*{ (*arr.borrow())[(1) as usize].data.clone() }.borrow_mut()).push(42); + { + let __a1 = 42; + (*{ (*arr.borrow())[(1) as usize].data.clone() }.borrow_mut()).push(__a1) + }; assert!(({ (*arr.borrow())[(0) as usize].tag } == 7)); assert!(((*{ (*arr.borrow())[(1) as usize].data.clone() }.borrow()).len() == 1_usize)); assert!( diff --git a/tests/unit/out/refcount/auto.rs b/tests/unit/out/refcount/auto.rs index 01ad94e170..143d6defac 100644 --- a/tests/unit/out/refcount/auto.rs +++ b/tests/unit/out/refcount/auto.rs @@ -15,8 +15,14 @@ fn main_0() -> i32 { let x2: Value = Rc::new(RefCell::new(2_i16)); let x3: Value = Rc::new(RefCell::new(4_u32)); let v: Value> = Rc::new(RefCell::new(Vec::new())); - (*v.borrow_mut()).push(1); - (*v.borrow_mut()).push(2); + { + let __a1 = 1; + (*v.borrow_mut()).push(__a1) + }; + { + let __a1 = 2; + (*v.borrow_mut()).push(__a1) + }; 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())); diff --git a/tests/unit/out/refcount/class_templates.rs b/tests/unit/out/refcount/class_templates.rs index 205da877e5..7d90d32785 100644 --- a/tests/unit/out/refcount/class_templates.rs +++ b/tests/unit/out/refcount/class_templates.rs @@ -18,25 +18,12 @@ impl Clone for MyContainer_int_ { } } } -<<<<<<< HEAD -impl ByteRepr for MyContainer_int_ {} -#[derive(VaArg, FnPtrArg, Default)] -======= impl ByteRepr for MyContainer_int_ { fn byte_size() -> usize { 24 } - fn to_bytes(&self, buf: &mut [u8]) { - (*self.vec_.borrow()).to_bytes(&mut buf[0..24]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - vec_: Rc::new(RefCell::new(>::from_bytes(&buf[0..24]))), - } - } } #[derive(Record, VaArg, FnPtrArg, Default)] ->>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub struct MyContainer_char_ { #[offset(0)] vec_: Value>, @@ -48,25 +35,12 @@ impl Clone for MyContainer_char_ { } } } -<<<<<<< HEAD -impl ByteRepr for MyContainer_char_ {} -#[derive(VaArg, FnPtrArg, Default)] -======= impl ByteRepr for MyContainer_char_ { fn byte_size() -> usize { 24 } - fn to_bytes(&self, buf: &mut [u8]) { - (*self.vec_.borrow()).to_bytes(&mut buf[0..24]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - vec_: Rc::new(RefCell::new(>::from_bytes(&buf[0..24]))), - } - } } #[derive(Record, VaArg, FnPtrArg, Default)] ->>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub struct MyContainer_float_ { #[offset(0)] vec_: Value>, @@ -78,25 +52,12 @@ impl Clone for MyContainer_float_ { } } } -<<<<<<< HEAD -impl ByteRepr for MyContainer_float_ {} -#[derive(VaArg, FnPtrArg, Default)] -======= impl ByteRepr for MyContainer_float_ { fn byte_size() -> usize { 24 } - fn to_bytes(&self, buf: &mut [u8]) { - (*self.vec_.borrow()).to_bytes(&mut buf[0..24]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - vec_: Rc::new(RefCell::new(>::from_bytes(&buf[0..24]))), - } - } } #[derive(Clone, Record, VaArg, FnPtrArg, Default)] ->>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub struct Boxed_int_ { #[offset(0)] pub value: i32, diff --git a/tests/unit/out/refcount/clone_vs_move.rs b/tests/unit/out/refcount/clone_vs_move.rs index cb50b8e171..79a2f1fc20 100644 --- a/tests/unit/out/refcount/clone_vs_move.rs +++ b/tests/unit/out/refcount/clone_vs_move.rs @@ -430,7 +430,10 @@ fn main_0() -> i32 { Rc::new(RefCell::new((*pair3.borrow()).0.borrow().clone())), Rc::new(RefCell::new((*pair3.borrow()).1.borrow().clone())), ))); - (*(*pair4.borrow()).0.borrow_mut()).push(1); + { + let __a1 = 1; + (*(*pair4.borrow()).0.borrow_mut()).push(__a1) + }; (*(*pair4.borrow()).1.borrow_mut()) = 1; assert!(((*(*pair4.borrow()).0.borrow()).len() == 1_usize)); assert!(((*(*pair4.borrow()).1.borrow()) == 1)); diff --git a/tests/unit/out/refcount/compound_assign_ref.rs b/tests/unit/out/refcount/compound_assign_ref.rs index 42ac3bffbe..d76aa47e9b 100644 --- a/tests/unit/out/refcount/compound_assign_ref.rs +++ b/tests/unit/out/refcount/compound_assign_ref.rs @@ -12,7 +12,10 @@ pub fn main() { } fn main_0() -> i32 { let v: Value> = Rc::new(RefCell::new(Vec::new())); - (*v.borrow_mut()).push(10); + { + let __a1 = 10; + (*v.borrow_mut()).push(__a1) + }; { let _ptr = (v.as_pointer() as Ptr); _ptr.write(_ptr.read() + 5) diff --git a/tests/unit/out/refcount/constructor.rs b/tests/unit/out/refcount/constructor.rs index 84f4e57f51..97021bd29b 100644 --- a/tests/unit/out/refcount/constructor.rs +++ b/tests/unit/out/refcount/constructor.rs @@ -26,34 +26,12 @@ impl S { Rc::try_unwrap(__this).ok().unwrap().into_inner() } } -<<<<<<< HEAD -impl Clone for S { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - v: Rc::new(RefCell::new((*self.v.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } -} -impl ByteRepr for S {} -#[derive(VaArg, FnPtrArg)] -======= impl ByteRepr for S { fn byte_size() -> usize { 4 } - fn to_bytes(&self, buf: &mut [u8]) { - self.v.to_bytes(&mut buf[0..4]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - v: ::from_bytes(&buf[0..4]), - } - } } #[derive(Clone, Record, VaArg, FnPtrArg)] ->>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub struct Point { #[offset(0)] pub x: i32, diff --git a/tests/unit/out/refcount/container_field_addr.rs b/tests/unit/out/refcount/container_field_addr.rs index 8c036c5458..266ab269fa 100644 --- a/tests/unit/out/refcount/container_field_addr.rs +++ b/tests/unit/out/refcount/container_field_addr.rs @@ -6,31 +6,32 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Record, VaArg, FnPtrArg, Default)] pub struct S { - pub tag: Value, + #[offset(0)] + pub tag: i32, + #[offset(8)] pub v: Value>, + #[offset(32)] pub s: Value>, - pub m: Value>>, + #[offset(64)] + pub m: BTreeMap>, } impl Clone for S { fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - tag: Rc::new(RefCell::new((*self.tag.borrow()))), + Self { + tag: self.tag.clone(), v: Rc::new(RefCell::new((*self.v.borrow()).clone())), s: Rc::new(RefCell::new((*self.s.borrow()).clone())), - m: Rc::new(RefCell::new( - (*self.m.borrow()) - .iter() - .map(|(k, v)| (k.clone(), Rc::new(RefCell::new(v.borrow().clone())))) - .collect(), - )), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() + m: self.m.clone(), + } + } +} +impl ByteRepr for S { + fn byte_size() -> usize { + 112 } } -impl ByteRepr for S {} pub fn add_0(v: Ptr>, k: i32) { let v: Value>> = Rc::new(RefCell::new(v)); let k: Value = Rc::new(RefCell::new(k)); @@ -72,53 +73,53 @@ pub fn put_2(m: Ptr>>, k: i32, v: i32) { pub fn run_3(h: Ptr) { let h: Value> = Rc::new(RefCell::new(h)); ({ - let _v: Ptr> = ((*(*h.borrow()).upgrade().deref()).v.as_pointer()); - let _k: i32 = (*(*(*h.borrow()).upgrade().deref()).tag.borrow()); + let _v: Ptr> = ((*h.borrow()).with(|__s| __s.v.clone()).as_pointer()); + let _k: i32 = (*h.borrow()).with(|__s| __s.tag); add_0(_v, _k) }); ({ append_1( - ((*(*h.borrow()).upgrade().deref()).s.as_pointer()), + ((*h.borrow()).with(|__s| __s.s.clone()).as_pointer()), Ptr::::from_string_literal(b"ab"), 2_usize, ) }); ({ - let _m: Ptr>> = - ((*(*h.borrow()).upgrade().deref()).m.as_pointer()); - let _k: i32 = (*(*(*h.borrow()).upgrade().deref()).tag.borrow()); + let _m: Ptr>> = (field_ptr!((*h.borrow()), m)); + let _k: i32 = (*h.borrow()).with(|__s| __s.tag); put_2(_m, _k, 2) }); let pv: Value>> = Rc::new(RefCell::new( - ((*(*h.borrow()).upgrade().deref()).v.as_pointer()), + ((*h.borrow()).with(|__s| __s.v.clone()).as_pointer()), )); - (*pv.borrow()).with_mut(|__v: &mut Vec| { - __v.push(((*(*pv.borrow()).upgrade().deref()).len() as i32)) - }); + { + let __a1 = ((*(*pv.borrow()).upgrade().deref()).len() as i32); + (*pv.borrow()).with_mut(|__v: &mut Vec| __v.push(__a1)) + }; assert!( - (((*(*(*h.borrow()).upgrade().deref()).v.borrow()).len() == 2_usize) - && ((((*(*h.borrow()).upgrade().deref()).v.as_pointer() as Ptr) + (((*(*h.borrow()).with(|__s| __s.v.clone()).borrow()).len() == 2_usize) + && ((((*h.borrow()).with(|__s| __s.v.clone()).as_pointer() as Ptr) .offset(0_usize) .read()) == 7)) - && ((((*(*h.borrow()).upgrade().deref()).v.as_pointer() as Ptr) + && ((((*h.borrow()).with(|__s| __s.v.clone()).as_pointer() as Ptr) .offset(1_usize) .read()) == 1) ); assert!( - (*(*(*h.borrow()).upgrade().deref()).s.borrow()) + (*(*h.borrow()).with(|__s| __s.s.clone()).borrow()) .iter() .copied() .take( - (*(*(*h.borrow()).upgrade().deref()).s.borrow()) + (*(*h.borrow()).with(|__s| __s.s.clone()).borrow()) .len() .saturating_sub(1) ) .eq(Ptr::::from_string_literal(b"ab").to_c_string_iterator()) ); assert!( - ((((*(*h.borrow()).upgrade().deref()).m.as_pointer() as Ptr>>) + (((field_ptr!((*h.borrow()), m) as Ptr>>) .with_mut(|__v: &mut BTreeMap>| { __v.entry(7) .or_insert_with(|| Rc::new(RefCell::new(::default()))) @@ -127,7 +128,7 @@ pub fn run_3(h: Ptr) { .read()) == 2) ); - assert!(((*(*(*h.borrow()).upgrade().deref()).tag.borrow()) == 7)); + assert!(((*h.borrow()).with(|__s| __s.tag) == 7)); } pub fn main() { __cpp2rust_init_globals(); @@ -135,10 +136,10 @@ pub fn main() { } fn main_0() -> i32 { let local: Value = Rc::new(RefCell::new(::default())); - (*(*local.borrow()).tag.borrow_mut()) = 7; + (*local.borrow_mut()).tag = 7; ({ run_3((local.as_pointer())) }); let heap: Value> = Rc::new(RefCell::new(Ptr::alloc(::default()))); - (*(*(*heap.borrow()).upgrade().deref()).tag.borrow_mut()) = 7; + field!((*heap.borrow()), tag).write(7); ({ run_3((*heap.borrow()).clone()) }); (*heap.borrow()).delete(); return 0; diff --git a/tests/unit/out/refcount/copy_assign.rs b/tests/unit/out/refcount/copy_assign.rs index 73d9da377d..72329098d1 100644 --- a/tests/unit/out/refcount/copy_assign.rs +++ b/tests/unit/out/refcount/copy_assign.rs @@ -45,27 +45,12 @@ impl Clone for Partial { Partial::copy_from(__src.as_pointer()) } } -<<<<<<< HEAD -impl ByteRepr for Partial {} -#[derive(VaArg, FnPtrArg)] -======= impl ByteRepr for Partial { fn byte_size() -> usize { 8 } - fn to_bytes(&self, buf: &mut [u8]) { - self.v.to_bytes(&mut buf[0..4]); - self.keep.to_bytes(&mut buf[4..8]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - v: ::from_bytes(&buf[0..4]), - keep: ::from_bytes(&buf[4..8]), - } - } } #[derive(Clone, Record, VaArg, FnPtrArg)] ->>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub struct NonConstAssign { #[offset(0)] pub mark: i32, @@ -82,25 +67,12 @@ impl Default for NonConstAssign { { NonConstAssign::new() } } } -<<<<<<< HEAD -impl ByteRepr for NonConstAssign {} -#[derive(VaArg, FnPtrArg)] -======= impl ByteRepr for NonConstAssign { fn byte_size() -> usize { 4 } - fn to_bytes(&self, buf: &mut [u8]) { - self.mark.to_bytes(&mut buf[0..4]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - mark: ::from_bytes(&buf[0..4]), - } - } } #[derive(Clone, Record, VaArg, FnPtrArg)] ->>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub struct RefQualified { #[offset(0)] pub mark: i32, @@ -117,29 +89,12 @@ impl Default for RefQualified { { RefQualified::new() } } } -<<<<<<< HEAD -impl ByteRepr for RefQualified {} -#[derive(VaArg, FnPtrArg)] -======= impl ByteRepr for RefQualified { fn byte_size() -> usize { 4 } - fn to_bytes(&self, buf: &mut [u8]) { - self.mark.to_bytes(&mut buf[0..4]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - mark: ::from_bytes(&buf[0..4]), - } - } } -<<<<<<< HEAD -#[derive(Clone, Record, VaArg, FnPtrArg)] ->>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) -======= #[derive(Record, VaArg, FnPtrArg)] ->>>>>>> 349ab5a6 (Keep Box fields boxed) pub struct Holder { #[offset(0)] pub p: Partial, @@ -166,25 +121,11 @@ impl Default for Holder { } } } -<<<<<<< HEAD -impl ByteRepr for Holder {} -======= impl ByteRepr for Holder { fn byte_size() -> usize { 24 } - fn to_bytes(&self, buf: &mut [u8]) { - self.p.to_bytes(&mut buf[0..8]); - (*self.arr.borrow()).to_bytes(&mut buf[8..24]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - p: ::from_bytes(&buf[0..8]), - arr: Rc::new(RefCell::new(>::from_bytes(&buf[8..24]))), - } - } } ->>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub fn main() { __cpp2rust_init_globals(); std::process::exit(main_0()); diff --git a/tests/unit/out/refcount/copy_ctor.rs b/tests/unit/out/refcount/copy_ctor.rs index f3b748e0cf..9bc6e2a611 100644 --- a/tests/unit/out/refcount/copy_ctor.rs +++ b/tests/unit/out/refcount/copy_ctor.rs @@ -36,25 +36,12 @@ impl Clone for Counted { Counted::copy_from(__src.as_pointer()) } } -<<<<<<< HEAD -impl ByteRepr for Counted {} -#[derive(VaArg, FnPtrArg)] -======= impl ByteRepr for Counted { fn byte_size() -> usize { 4 } - fn to_bytes(&self, buf: &mut [u8]) { - self.v.to_bytes(&mut buf[0..4]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - v: ::from_bytes(&buf[0..4]), - } - } } #[derive(Record, VaArg, FnPtrArg)] ->>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub struct NonConst { #[offset(0)] pub mark: i32, @@ -93,25 +80,12 @@ impl Default for NonConst { { NonConst::new() } } } -<<<<<<< HEAD -impl ByteRepr for NonConst {} -#[derive(VaArg, FnPtrArg, Default)] -======= impl ByteRepr for NonConst { fn byte_size() -> usize { 4 } - fn to_bytes(&self, buf: &mut [u8]) { - self.mark.to_bytes(&mut buf[0..4]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - mark: ::from_bytes(&buf[0..4]), - } - } } #[derive(Record, VaArg, FnPtrArg, Default)] ->>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub struct Ignored { #[offset(0)] pub v: i32, @@ -136,29 +110,12 @@ impl Clone for Ignored { Ignored::copy_from(__src.as_pointer()) } } -<<<<<<< HEAD -impl ByteRepr for Ignored {} -#[derive(VaArg, FnPtrArg)] -======= impl ByteRepr for Ignored { fn byte_size() -> usize { 4 } - fn to_bytes(&self, buf: &mut [u8]) { - self.v.to_bytes(&mut buf[0..4]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - v: ::from_bytes(&buf[0..4]), - } - } } -<<<<<<< HEAD -#[derive(Clone, Record, VaArg, FnPtrArg)] ->>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) -======= #[derive(Record, VaArg, FnPtrArg)] ->>>>>>> 349ab5a6 (Keep Box fields boxed) pub struct Holder { #[offset(0)] pub c: Counted, @@ -185,25 +142,11 @@ impl Default for Holder { } } } -<<<<<<< HEAD -impl ByteRepr for Holder {} -======= impl ByteRepr for Holder { fn byte_size() -> usize { 12 } - fn to_bytes(&self, buf: &mut [u8]) { - self.c.to_bytes(&mut buf[0..4]); - (*self.arr.borrow()).to_bytes(&mut buf[4..12]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - c: ::from_bytes(&buf[0..4]), - arr: Rc::new(RefCell::new(>::from_bytes(&buf[4..12]))), - } - } } ->>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub fn by_value_1(c: Counted) -> i32 { let c: Value = Rc::new(RefCell::new(c)); return { (*c.borrow()).v }; diff --git a/tests/unit/out/refcount/copy_move_defaulted.rs b/tests/unit/out/refcount/copy_move_defaulted.rs index 0c3a3145bc..98222e4fcd 100644 --- a/tests/unit/out/refcount/copy_move_defaulted.rs +++ b/tests/unit/out/refcount/copy_move_defaulted.rs @@ -65,33 +65,12 @@ impl Default for Explicit { } } } -<<<<<<< HEAD -impl ByteRepr for Explicit {} -#[derive(VaArg, FnPtrArg)] -======= impl ByteRepr for Explicit { fn byte_size() -> usize { 16 } - fn to_bytes(&self, buf: &mut [u8]) { - self.v.to_bytes(&mut buf[0..4]); - self.inner.to_bytes(&mut buf[4..8]); - (*self.arr.borrow()).to_bytes(&mut buf[8..16]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - v: ::from_bytes(&buf[0..4]), - inner: ::from_bytes(&buf[4..8]), - arr: Rc::new(RefCell::new(>::from_bytes(&buf[8..16]))), - } - } } -<<<<<<< HEAD -#[derive(Clone, Record, VaArg, FnPtrArg)] ->>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) -======= #[derive(Record, VaArg, FnPtrArg)] ->>>>>>> 349ab5a6 (Keep Box fields boxed) pub struct Implicit { #[offset(0)] pub v: i32, @@ -156,25 +135,12 @@ impl DefaultCopyUserMove { Rc::try_unwrap(__this).ok().unwrap().into_inner() } } -<<<<<<< HEAD -impl ByteRepr for DefaultCopyUserMove {} -#[derive(VaArg, FnPtrArg, Default)] -======= impl ByteRepr for DefaultCopyUserMove { fn byte_size() -> usize { 4 } - fn to_bytes(&self, buf: &mut [u8]) { - self.v.to_bytes(&mut buf[0..4]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - v: ::from_bytes(&buf[0..4]), - } - } } #[derive(Record, VaArg, FnPtrArg, Default)] ->>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub struct UserCopyDefaultMove { #[offset(0)] pub v: i32, @@ -208,25 +174,12 @@ impl Clone for UserCopyDefaultMove { UserCopyDefaultMove::copy_from(__src.as_pointer()) } } -<<<<<<< HEAD -impl ByteRepr for UserCopyDefaultMove {} -#[derive()] -======= impl ByteRepr for UserCopyDefaultMove { fn byte_size() -> usize { 4 } - fn to_bytes(&self, buf: &mut [u8]) { - self.v.to_bytes(&mut buf[0..4]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - v: ::from_bytes(&buf[0..4]), - } - } } #[derive(Record)] ->>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub struct Buffer { #[offset(0)] pub data: Value>, @@ -286,33 +239,12 @@ impl Default for Buffer { } } } -<<<<<<< HEAD -impl ByteRepr for Buffer {} -#[derive()] -======= impl ByteRepr for Buffer { fn byte_size() -> usize { 64 } - fn to_bytes(&self, buf: &mut [u8]) { - (*self.data.borrow()).to_bytes(&mut buf[0..24]); - (*self.rows.borrow()).to_bytes(&mut buf[24..48]); - self.n.to_bytes(&mut buf[48..52]); - (*self.arr.borrow()).to_bytes(&mut buf[52..60]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - data: Rc::new(RefCell::new(>::from_bytes(&buf[0..24]))), - rows: Rc::new(RefCell::new(>>>::from_bytes( - &buf[24..48], - ))), - n: ::from_bytes(&buf[48..52]), - arr: Rc::new(RefCell::new(>::from_bytes(&buf[52..60]))), - } - } } #[derive(Record)] ->>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub struct Owner { #[offset(0)] pub data: Value>, @@ -349,31 +281,12 @@ impl Default for Owner { } } } -<<<<<<< HEAD -impl ByteRepr for Owner {} -#[derive(Default)] -======= impl ByteRepr for Owner { fn byte_size() -> usize { 48 } - fn to_bytes(&self, buf: &mut [u8]) { - (*self.data.borrow()).to_bytes(&mut buf[0..24]); - self.n.to_bytes(&mut buf[24..28]); - (*self.arr.borrow()).to_bytes(&mut buf[28..36]); - self.p.to_bytes(&mut buf[40..48]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - data: Rc::new(RefCell::new(>::from_bytes(&buf[0..24]))), - n: ::from_bytes(&buf[24..28]), - arr: Rc::new(RefCell::new(>::from_bytes(&buf[28..36]))), - p: >>::from_bytes(&buf[40..48]), - } - } } #[derive(Record, Default)] ->>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub struct Holder { #[offset(0)] pub inner: Inner, @@ -403,27 +316,11 @@ impl Holder { Rc::try_unwrap(__this).ok().unwrap().into_inner() } } -<<<<<<< HEAD -impl ByteRepr for Holder {} -======= impl ByteRepr for Holder { fn byte_size() -> usize { 32 } - fn to_bytes(&self, buf: &mut [u8]) { - self.inner.to_bytes(&mut buf[0..4]); - self.e.to_bytes(&mut buf[4..20]); - self.p.to_bytes(&mut buf[24..32]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - inner: ::from_bytes(&buf[0..4]), - e: ::from_bytes(&buf[4..20]), - p: >>::from_bytes(&buf[24..32]), - } - } } ->>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub fn same_0(a: Ptr, b: Ptr) -> bool { return ((({ let _lhs = a.with(|__s| __s.v); @@ -504,7 +401,10 @@ fn main_0() -> i32 { let a0_clone = (*b.borrow()).clone(); (*vec_.borrow_mut()).push(a0_clone) }; - (*vec_.borrow_mut()).push(Explicit::new({ 9 })); + { + let __a1 = Explicit::new({ 9 }); + (*vec_.borrow_mut()).push(__a1) + }; assert!( ({ (*(vec_.as_pointer() as Ptr) @@ -585,7 +485,10 @@ fn main_0() -> i32 { && ((*{ (*q.borrow()).rows.clone() }.borrow()).is_empty()) ); let bufs: Value> = Rc::new(RefCell::new(Vec::new())); - (*bufs.borrow_mut()).push(Buffer::move_from({ r.as_pointer() })); + { + let __a1 = Buffer::move_from({ r.as_pointer() }); + (*bufs.borrow_mut()).push(__a1) + }; { let __init = Buffer::move_from({ (bufs.as_pointer() as Ptr).offset(0_usize) }); (*bufs.borrow_mut()).push(__init) @@ -621,7 +524,10 @@ fn main_0() -> i32 { .is_empty()) ); let o1: Value = Rc::new(RefCell::new(::default())); - (*{ (*o1.borrow()).data.clone() }.borrow_mut()).push(5); + { + let __a1 = 5; + (*{ (*o1.borrow()).data.clone() }.borrow_mut()).push(__a1) + }; (*o1.borrow_mut()).n = 5; (*{ (*o1.borrow()).arr.clone() }.borrow_mut())[(0) as usize] = 5; (*{ (*o1.borrow()).arr.clone() }.borrow_mut())[(1) as usize] = 6; diff --git a/tests/unit/out/refcount/copy_move_deleted.rs b/tests/unit/out/refcount/copy_move_deleted.rs index 6aa0529bdf..0e2fb16718 100644 --- a/tests/unit/out/refcount/copy_move_deleted.rs +++ b/tests/unit/out/refcount/copy_move_deleted.rs @@ -27,25 +27,12 @@ impl NoCopy { Rc::try_unwrap(__this).ok().unwrap().into_inner() } } -<<<<<<< HEAD -impl ByteRepr for NoCopy {} -#[derive()] -======= impl ByteRepr for NoCopy { fn byte_size() -> usize { 4 } - fn to_bytes(&self, buf: &mut [u8]) { - self.v.to_bytes(&mut buf[0..4]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - v: ::from_bytes(&buf[0..4]), - } - } } #[derive(Record)] ->>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub struct PrivateCopy { #[offset(0)] pub v: i32, @@ -70,25 +57,12 @@ impl Default for PrivateCopy { { PrivateCopy::new() } } } -<<<<<<< HEAD -impl ByteRepr for PrivateCopy {} -#[derive()] -======= impl ByteRepr for PrivateCopy { fn byte_size() -> usize { 4 } - fn to_bytes(&self, buf: &mut [u8]) { - self.v.to_bytes(&mut buf[0..4]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - v: ::from_bytes(&buf[0..4]), - } - } } #[derive(Record)] ->>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub struct Immovable { #[offset(0)] pub v: i32, @@ -135,25 +109,11 @@ impl Container { Rc::try_unwrap(__this).ok().unwrap().into_inner() } } -<<<<<<< HEAD -impl ByteRepr for Container {} -======= impl ByteRepr for Container { fn byte_size() -> usize { 8 } - fn to_bytes(&self, buf: &mut [u8]) { - self.inner.to_bytes(&mut buf[0..4]); - self.tag.to_bytes(&mut buf[4..8]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - inner: ::from_bytes(&buf[0..4]), - tag: ::from_bytes(&buf[4..8]), - } - } } ->>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub fn bump_0(p: Ptr) { let p: Value> = Rc::new(RefCell::new(p)); field!((*p.borrow()), v).with_mut(|__v| __v.postfix_inc()); diff --git a/tests/unit/out/refcount/destructor.rs b/tests/unit/out/refcount/destructor.rs index bbee36edea..2a0e87579d 100644 --- a/tests/unit/out/refcount/destructor.rs +++ b/tests/unit/out/refcount/destructor.rs @@ -16,75 +16,32 @@ pub struct Defaulted { #[offset(0)] pub s: S, } -<<<<<<< HEAD -impl ByteRepr for Defaulted {} -#[derive(VaArg, FnPtrArg, Default)] -======= impl ByteRepr for Defaulted { fn byte_size() -> usize { 1 } - fn to_bytes(&self, buf: &mut [u8]) { - self.s.to_bytes(&mut buf[0..1]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - s: ::from_bytes(&buf[0..1]), - } - } } #[derive(Clone, Record, VaArg, FnPtrArg, Default)] ->>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub struct Middle { #[offset(0)] pub s: S, } -<<<<<<< HEAD -impl ByteRepr for Middle {} -#[derive(VaArg, FnPtrArg, Default)] -======= impl ByteRepr for Middle { fn byte_size() -> usize { 1 } - fn to_bytes(&self, buf: &mut [u8]) { - self.s.to_bytes(&mut buf[0..1]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - s: ::from_bytes(&buf[0..1]), - } - } } #[derive(Clone, Record, VaArg, FnPtrArg, Default)] ->>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub struct Outer { #[offset(0)] pub m: Middle, } -<<<<<<< HEAD -impl ByteRepr for Outer {} -#[derive(VaArg, FnPtrArg)] -======= impl ByteRepr for Outer { fn byte_size() -> usize { 1 } - fn to_bytes(&self, buf: &mut [u8]) { - self.m.to_bytes(&mut buf[0..1]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - m: ::from_bytes(&buf[0..1]), - } - } } -<<<<<<< HEAD -#[derive(Clone, Record, VaArg, FnPtrArg)] ->>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) -======= #[derive(Record, VaArg, FnPtrArg)] ->>>>>>> 349ab5a6 (Keep Box fields boxed) pub struct ArrayMember { #[offset(0)] pub items: Value>, @@ -105,115 +62,51 @@ impl Default for ArrayMember { } } } -<<<<<<< HEAD -impl ByteRepr for ArrayMember {} -#[derive(VaArg, FnPtrArg, Default)] -======= impl ByteRepr for ArrayMember { fn byte_size() -> usize { 3 } - fn to_bytes(&self, buf: &mut [u8]) { - (*self.items.borrow()).to_bytes(&mut buf[0..3]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - items: Rc::new(RefCell::new(>::from_bytes(&buf[0..3]))), - } - } } #[derive(Clone, Record, VaArg, FnPtrArg, Default)] ->>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub struct EmptyBody { #[offset(0)] pub s: S, } -<<<<<<< HEAD -impl ByteRepr for EmptyBody {} -#[derive(VaArg, FnPtrArg, Default)] -======= impl ByteRepr for EmptyBody { fn byte_size() -> usize { 1 } - fn to_bytes(&self, buf: &mut [u8]) { - self.s.to_bytes(&mut buf[0..1]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - s: ::from_bytes(&buf[0..1]), - } - } } #[derive(Clone, Record, VaArg, FnPtrArg, Default)] ->>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub struct Templated_char_ { #[offset(0)] pub v: u8, } -<<<<<<< HEAD -impl ByteRepr for Templated_char_ {} -#[derive(VaArg, FnPtrArg, Default)] -======= impl ByteRepr for Templated_char_ { fn byte_size() -> usize { 1 } - fn to_bytes(&self, buf: &mut [u8]) { - self.v.to_bytes(&mut buf[0..1]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - v: ::from_bytes(&buf[0..1]), - } - } } #[derive(Clone, Record, VaArg, FnPtrArg, Default)] ->>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub struct Templated_int_ { #[offset(0)] pub v: i32, } -<<<<<<< HEAD -impl ByteRepr for Templated_int_ {} -#[derive(VaArg, FnPtrArg, Default)] -======= impl ByteRepr for Templated_int_ { fn byte_size() -> usize { 4 } - fn to_bytes(&self, buf: &mut [u8]) { - self.v.to_bytes(&mut buf[0..4]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - v: ::from_bytes(&buf[0..4]), - } - } } #[derive(Clone, Record, VaArg, FnPtrArg, Default)] ->>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub struct Copied { #[offset(0)] pub v: i32, } -<<<<<<< HEAD -impl ByteRepr for Copied {} -======= impl ByteRepr for Copied { fn byte_size() -> usize { 4 } - fn to_bytes(&self, buf: &mut [u8]) { - self.v.to_bytes(&mut buf[0..4]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - v: ::from_bytes(&buf[0..4]), - } - } } ->>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) thread_local!( pub static order_1: Value> = Rc::new(RefCell::new((0..3).map(|_| 0_i32).collect::>())); @@ -226,25 +119,12 @@ pub struct Tagged { #[offset(0)] pub tag: i32, } -<<<<<<< HEAD -impl ByteRepr for Tagged {} -#[derive(VaArg, FnPtrArg, Default)] -======= impl ByteRepr for Tagged { fn byte_size() -> usize { 4 } - fn to_bytes(&self, buf: &mut [u8]) { - self.tag.to_bytes(&mut buf[0..4]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - tag: ::from_bytes(&buf[0..4]), - } - } } #[derive(Clone, Record, VaArg, FnPtrArg, Default)] ->>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub struct Ordered { #[offset(0)] pub first: Tagged, @@ -257,31 +137,11 @@ pub struct Ordered { #[offset(16)] pub third: Tagged, } -<<<<<<< HEAD -impl ByteRepr for Ordered {} -======= impl ByteRepr for Ordered { fn byte_size() -> usize { 20 } - fn to_bytes(&self, buf: &mut [u8]) { - self.first.to_bytes(&mut buf[0..4]); - self.dummy1.to_bytes(&mut buf[4..8]); - self.second.to_bytes(&mut buf[8..12]); - self.dummy2.to_bytes(&mut buf[12..16]); - self.third.to_bytes(&mut buf[16..20]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - first: ::from_bytes(&buf[0..4]), - dummy1: ::from_bytes(&buf[4..8]), - second: ::from_bytes(&buf[8..12]), - dummy2: ::from_bytes(&buf[12..16]), - third: ::from_bytes(&buf[16..20]), - } - } } ->>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub fn main() { __cpp2rust_init_globals(); std::process::exit(main_0()); diff --git a/tests/unit/out/refcount/field_aliasing.rs b/tests/unit/out/refcount/field_aliasing.rs index bf083e9916..a9ab905f33 100644 --- a/tests/unit/out/refcount/field_aliasing.rs +++ b/tests/unit/out/refcount/field_aliasing.rs @@ -6,47 +6,45 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct In { - pub x: Value, - pub y: Value, -} -impl Clone for In { - fn clone(&self) -> Self { - Self { - x: Rc::new(RefCell::new((*self.x.borrow()).clone())), - y: Rc::new(RefCell::new((*self.y.borrow()).clone())), - } - } + #[offset(0)] + pub x: i32, + #[offset(4)] + pub y: i32, } impl ByteRepr for In { fn byte_size() -> usize { 8 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.x.borrow()).to_bytes(&mut buf[0..4]); - (*self.y.borrow()).to_bytes(&mut buf[4..8]); + self.x.to_bytes(&mut buf[0..4]); + self.y.to_bytes(&mut buf[4..8]); } fn from_bytes(buf: &[u8]) -> Self { Self { - x: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - y: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + x: ::from_bytes(&buf[0..4]), + y: ::from_bytes(&buf[4..8]), } } } -#[derive(VaArg, FnPtrArg)] +#[derive(Record, VaArg, FnPtrArg)] pub struct S { - pub in_: Value, - pub total: Value, - pub n: Value, + #[offset(0)] + pub in_: In, + #[offset(8)] + pub total: i32, + #[offset(12)] + pub n: i32, + #[offset(16)] pub arr: Value>, } impl Clone for S { fn clone(&self) -> Self { Self { - in_: Rc::new(RefCell::new((*self.in_.borrow()).clone())), - total: Rc::new(RefCell::new((*self.total.borrow()).clone())), - n: Rc::new(RefCell::new((*self.n.borrow()).clone())), + in_: self.in_.clone(), + total: self.total.clone(), + n: self.n.clone(), arr: Rc::new(RefCell::new((*self.arr.borrow()).clone())), } } @@ -54,9 +52,9 @@ impl Clone for S { impl Default for S { fn default() -> Self { S { - in_: >::default(), - total: Rc::new(RefCell::new(0_i32)), - n: Rc::new(RefCell::new(0_i32)), + in_: ::default(), + total: 0_i32, + n: 0_i32, arr: Rc::new(RefCell::new((0..4).map(|_| 0_i32).collect::>())), } } @@ -66,45 +64,39 @@ impl ByteRepr for S { 32 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.in_.borrow()).to_bytes(&mut buf[0..8]); - (*self.total.borrow()).to_bytes(&mut buf[8..12]); - (*self.n.borrow()).to_bytes(&mut buf[12..16]); + self.in_.to_bytes(&mut buf[0..8]); + self.total.to_bytes(&mut buf[8..12]); + self.n.to_bytes(&mut buf[12..16]); (*self.arr.borrow()).to_bytes(&mut buf[16..32]); } fn from_bytes(buf: &[u8]) -> Self { Self { - in_: Rc::new(RefCell::new(::from_bytes(&buf[0..8]))), - total: Rc::new(RefCell::new(::from_bytes(&buf[8..12]))), - n: Rc::new(RefCell::new(::from_bytes(&buf[12..16]))), + in_: ::from_bytes(&buf[0..8]), + total: ::from_bytes(&buf[8..12]), + n: ::from_bytes(&buf[12..16]), arr: Rc::new(RefCell::new(>::from_bytes(&buf[16..32]))), } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Node { - pub x: Value, - pub self_: Value>, -} -impl Clone for Node { - fn clone(&self) -> Self { - Self { - x: Rc::new(RefCell::new((*self.x.borrow()).clone())), - self_: Rc::new(RefCell::new((*self.self_.borrow()).clone())), - } - } + #[offset(0)] + pub x: i32, + #[offset(8)] + pub self_: Ptr, } impl ByteRepr for Node { fn byte_size() -> usize { 16 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.x.borrow()).to_bytes(&mut buf[0..4]); - (*self.self_.borrow()).to_bytes(&mut buf[8..16]); + self.x.to_bytes(&mut buf[0..4]); + self.self_.to_bytes(&mut buf[8..16]); } fn from_bytes(buf: &[u8]) -> Self { Self { - x: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - self_: Rc::new(RefCell::new(>::from_bytes(&buf[8..16]))), + x: ::from_bytes(&buf[0..4]), + self_: >::from_bytes(&buf[8..16]), } } } @@ -118,68 +110,47 @@ fn main_0() -> i32 { )); assert!((((!((*p.borrow()).is_null())) as i32) != 0)); let q: Value> = Rc::new(RefCell::new((*p.borrow()).clone())); - (*(*(*(*p.borrow()).upgrade().deref()).in_.borrow()) - .x - .borrow_mut()) = 1; - (*(*(*(*p.borrow()).upgrade().deref()).in_.borrow()) - .y - .borrow_mut()) = 2; - (*(*(*p.borrow()).upgrade().deref()).total.borrow_mut()) = { - let _lhs = (*(*(*(*q.borrow()).upgrade().deref()).in_.borrow()) - .x - .borrow()); - _lhs + (*(*(*(*q.borrow()).upgrade().deref()).in_.borrow()) - .y - .borrow()) - }; - assert!(((((*(*(*q.borrow()).upgrade().deref()).total.borrow()) == 3) as i32) != 0)); - let ip: Value> = Rc::new(RefCell::new( - ((*(*p.borrow()).upgrade().deref()).in_.as_pointer()), - )); - (*(*(*ip.borrow()).upgrade().deref()).x.borrow_mut()) = - ((*(*(*p.borrow()).upgrade().deref()).total.borrow()) + 1); + field!(field!((*p.borrow()), in_), x).write(1); + field!(field!((*p.borrow()), in_), y).write(2); + field!((*p.borrow()), total).write({ + let _lhs = (*q.borrow()).with(|__s| __s.in_.x); + _lhs + (*q.borrow()).with(|__s| __s.in_.y) + }); + assert!(((((*q.borrow()).with(|__s| __s.total) == 3) as i32) != 0)); + let ip: Value> = Rc::new(RefCell::new((field_ptr!((*p.borrow()), in_)))); + field!((*ip.borrow()), x).write(((*p.borrow()).with(|__s| __s.total) + 1)); assert!( - (((((((*(*(*(*q.borrow()).upgrade().deref()).in_.borrow()) - .x - .borrow()) - == 4) as i32) - != 0) - && ((((*(*(*(*q.borrow()).upgrade().deref()).in_.borrow()) - .y - .borrow()) - == 2) as i32) - != 0)) as i32) + (((((((*q.borrow()).with(|__s| __s.in_.x) == 4) as i32) != 0) + && ((((*q.borrow()).with(|__s| __s.in_.y) == 2) as i32) != 0)) as i32) != 0) ); - let __rhs = (*(*(*p.borrow()).upgrade().deref()).total.borrow()); - (*(*(*p.borrow()).upgrade().deref()).arr.borrow_mut()) - [(*(*(*p.borrow()).upgrade().deref()).n.borrow()) as usize] = __rhs; - (*(*(*p.borrow()).upgrade().deref()).n.borrow_mut()) += 1; - (*(*(*p.borrow()).upgrade().deref()).arr.borrow_mut()) - [(*(*(*p.borrow()).upgrade().deref()).n.borrow()) as usize] = - (*(*(*(*q.borrow()).upgrade().deref()).in_.borrow()) - .x - .borrow()); + let __rhs = (*p.borrow()).with(|__s| __s.total); + (*(*p.borrow()).with(|__s| __s.arr.clone()).borrow_mut()) + [((*p.borrow()).with(|__s| __s.n)) as usize] = __rhs; + { + let _ptr = field!((*p.borrow()), n); + _ptr.write(_ptr.read() + 1) + }; + (*(*p.borrow()).with(|__s| __s.arr.clone()).borrow_mut()) + [((*p.borrow()).with(|__s| __s.n)) as usize] = (*q.borrow()).with(|__s| __s.in_.x); assert!( - ((((((((((*(*(*q.borrow()).upgrade().deref()).arr.borrow())[(0) as usize] == 3) as i32) + ((((((((((*(*q.borrow()).with(|__s| __s.arr.clone()).borrow())[(0) as usize] == 3) as i32) != 0) - && ((((*(*(*q.borrow()).upgrade().deref()).arr.borrow())[(1) as usize] == 4) as i32) + && ((((*(*q.borrow()).with(|__s| __s.arr.clone()).borrow())[(1) as usize] == 4) + as i32) != 0)) as i32) != 0) - && ((((*(*(*q.borrow()).upgrade().deref()).n.borrow()) == 1) as i32) != 0)) - as i32) + && ((((*q.borrow()).with(|__s| __s.n) == 1) as i32) != 0)) as i32) != 0) ); libcc2rs::free_refcount((*p.borrow()).to_any()); let s: Value = >::default(); - (*(*s.borrow()).x.borrow_mut()) = 1; + (*s.borrow_mut()).x = 1; let __rhs = (s.as_pointer()); - (*(*s.borrow()).self_.borrow_mut()) = __rhs; - let __rhs = ((*(*s.borrow()).x.borrow()) + 1); - (*(*(*(*s.borrow()).self_.borrow()).upgrade().deref()) - .x - .borrow_mut()) = __rhs; - assert!(((((*(*s.borrow()).x.borrow()) == 2) as i32) != 0)); + (*s.borrow_mut()).self_ = __rhs; + let __rhs = ({ (*s.borrow()).x } + 1); + field!({ (*s.borrow()).self_.clone() }, x).write(__rhs); + assert!(((({ (*s.borrow()).x } == 2) as i32) != 0)); return 0; } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/field_method_receiver.rs b/tests/unit/out/refcount/field_method_receiver.rs index 145c596348..2b217b284b 100644 --- a/tests/unit/out/refcount/field_method_receiver.rs +++ b/tests/unit/out/refcount/field_method_receiver.rs @@ -6,58 +6,50 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct Counter { - pub n: Value, -} -impl Clone for Counter { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - n: Rc::new(RefCell::new((*self.n.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub n: i32, } impl ByteRepr for Counter { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.n.borrow()).to_bytes(&mut buf[0..4]); + self.n.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - n: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + n: ::from_bytes(&buf[0..4]), } } } -#[derive(VaArg, FnPtrArg)] +#[derive(Record, VaArg, FnPtrArg)] pub struct S { - pub tag: Value, - pub c: Value, + #[offset(0)] + pub tag: i32, + #[offset(4)] + pub c: Counter, + #[offset(8)] pub arr: Value>, + #[offset(16)] pub v: Value>, } impl Clone for S { fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - tag: Rc::new(RefCell::new((*self.tag.borrow()))), - c: Rc::new(RefCell::new((*self.c.borrow()).clone())), - arr: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 2, _>( - |__i: usize| ((*self.arr.borrow())[(__i) as usize]).clone(), - )))), + Self { + tag: self.tag.clone(), + c: self.c.clone(), + arr: Rc::new(RefCell::new((*self.arr.borrow()).clone())), v: Rc::new(RefCell::new((*self.v.borrow()).clone())), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() + } } } impl Default for S { fn default() -> Self { S { - tag: Rc::new(RefCell::new(0_i32)), - c: >::default(), + tag: 0_i32, + c: ::default(), arr: Rc::new(RefCell::new( (0..2) .map(|_| ::default()) @@ -67,73 +59,74 @@ impl Default for S { } } } -impl ByteRepr for S {} +impl ByteRepr for S { + fn byte_size() -> usize { + 40 + } +} pub fn run_0(o: Ptr) { let o: Value> = Rc::new(RefCell::new(o)); - ({ CounterImpl::add(&(*(*o.borrow()).upgrade().deref()).c.as_pointer(), 2) }); - assert!((({ CounterImpl::get(&(*(*o.borrow()).upgrade().deref()).c.as_pointer(),) }) == 2)); + ({ CounterImpl::add(&field_ptr!((*o.borrow()), c), 2) }); + assert!((({ CounterImpl::get(&field_ptr!((*o.borrow()), c),) }) == 2)); ({ CounterImpl::add( - &((*(*o.borrow()).upgrade().deref()).arr.as_pointer() as Ptr).offset(1), + &((*o.borrow()).with(|__s| __s.arr.clone()).as_pointer() as Ptr).offset(1), 5, ) }); assert!( (({ CounterImpl::get( - &((*(*o.borrow()).upgrade().deref()).arr.as_pointer() as Ptr).offset(1), + &((*o.borrow()).with(|__s| __s.arr.clone()).as_pointer() as Ptr).offset(1), ) }) == 5) ); - ({ - CounterImpl::add( - &({ CounterImpl::self_(&(*(*o.borrow()).upgrade().deref()).c.as_pointer()) }), - 1, - ) - }); - assert!((({ CounterImpl::get(&(*(*o.borrow()).upgrade().deref()).c.as_pointer(),) }) == 3)); + ({ CounterImpl::add(&({ CounterImpl::self_(&field_ptr!((*o.borrow()), c)) }), 1) }); + assert!((({ CounterImpl::get(&field_ptr!((*o.borrow()), c),) }) == 3)); assert!({ - let _lhs = ({ CounterImpl::self_(&(*(*o.borrow()).upgrade().deref()).c.as_pointer()) }); - _lhs == ((*(*o.borrow()).upgrade().deref()).c.as_pointer()) + let _lhs = ({ CounterImpl::self_(&field_ptr!((*o.borrow()), c)) }); + _lhs == (field_ptr!((*o.borrow()), c)) }); ({ let _other: Ptr = - (((*(*o.borrow()).upgrade().deref()).arr.as_pointer() as Ptr).offset(1)); + (((*o.borrow()).with(|__s| __s.arr.clone()).as_pointer() as Ptr).offset(1)); CounterImpl::take( - &((*(*o.borrow()).upgrade().deref()).arr.as_pointer() as Ptr).offset(0), + &((*o.borrow()).with(|__s| __s.arr.clone()).as_pointer() as Ptr).offset(0), _other, ) }); assert!( (({ CounterImpl::get( - &((*(*o.borrow()).upgrade().deref()).arr.as_pointer() as Ptr).offset(0), + &((*o.borrow()).with(|__s| __s.arr.clone()).as_pointer() as Ptr).offset(0), ) }) == 5) && (({ CounterImpl::get( - &((*(*o.borrow()).upgrade().deref()).arr.as_pointer() as Ptr) + &((*o.borrow()).with(|__s| __s.arr.clone()).as_pointer() as Ptr) .offset(1), ) }) == 0) ); ({ - let _other: Ptr = ((*(*o.borrow()).upgrade().deref()).c.as_pointer()); - CounterImpl::take(&(*(*o.borrow()).upgrade().deref()).c.as_pointer(), _other) + let _other: Ptr = (field_ptr!((*o.borrow()), c)); + CounterImpl::take(&field_ptr!((*o.borrow()), c), _other) }); - assert!((({ CounterImpl::get(&(*(*o.borrow()).upgrade().deref()).c.as_pointer(),) }) == 0)); + assert!((({ CounterImpl::get(&field_ptr!((*o.borrow()), c),) }) == 0)); ({ SImpl::bump(&(*o.borrow())) }); - assert!((({ CounterImpl::get(&(*(*o.borrow()).upgrade().deref()).c.as_pointer(),) }) == 1)); - (*(*(*o.borrow()).upgrade().deref()).v.borrow_mut()) - .push(({ CounterImpl::get(&(*(*o.borrow()).upgrade().deref()).c.as_pointer()) })); + assert!((({ CounterImpl::get(&field_ptr!((*o.borrow()), c),) }) == 1)); + { + let __a1 = ({ CounterImpl::get(&field_ptr!((*o.borrow()), c)) }); + (*(*o.borrow()).with(|__s| __s.v.clone()).borrow_mut()).push(__a1) + }; assert!( - ((*(*(*o.borrow()).upgrade().deref()).v.borrow()).len() == 1_usize) - && ((((*(*o.borrow()).upgrade().deref()).v.as_pointer() as Ptr) + ((*(*o.borrow()).with(|__s| __s.v.clone()).borrow()).len() == 1_usize) + && ((((*o.borrow()).with(|__s| __s.v.clone()).as_pointer() as Ptr) .offset(0_usize) .read()) == 1) ); - assert!(((*(*(*o.borrow()).upgrade().deref()).tag.borrow()) == 1)); + assert!(((*o.borrow()).with(|__s| __s.tag) == 1)); } pub fn main() { __cpp2rust_init_globals(); @@ -141,10 +134,10 @@ pub fn main() { } fn main_0() -> i32 { let local: Value = Rc::new(RefCell::new(::default())); - (*(*local.borrow()).tag.borrow_mut()) = 1; + (*local.borrow_mut()).tag = 1; ({ run_0((local.as_pointer())) }); let heap: Value> = Rc::new(RefCell::new(Ptr::alloc(::default()))); - (*(*(*heap.borrow()).upgrade().deref()).tag.borrow_mut()) = 1; + field!((*heap.borrow()), tag).write(1); ({ run_0((*heap.borrow()).clone()) }); (*heap.borrow()).delete(); return 0; @@ -157,20 +150,26 @@ pub trait CounterImpl { } impl CounterImpl for Ptr { fn get(&self) -> i32 { - return (*(*(*self).upgrade().deref()).n.borrow()); + return (*self).with(|__s| __s.n); } fn add(&self, k: i32) { let k: Value = Rc::new(RefCell::new(k)); - (*(*(*self).upgrade().deref()).n.borrow_mut()) += (*k.borrow()); + { + let _ptr = field!((*self), n); + _ptr.write(_ptr.read() + (*k.borrow())) + }; } fn self_(&self) -> Ptr { return (*self).clone(); } fn take(&self, other: Ptr) { let other: Value> = Rc::new(RefCell::new(other)); - let __rhs = (*(*(*other.borrow()).upgrade().deref()).n.borrow()); - (*(*(*self).upgrade().deref()).n.borrow_mut()) += __rhs; - (*(*(*other.borrow()).upgrade().deref()).n.borrow_mut()) = 0; + let __rhs = (*other.borrow()).with(|__s| __s.n); + { + let _ptr = field!((*self), n); + _ptr.write(_ptr.read() + __rhs) + }; + field!((*other.borrow()), n).write(0); } } pub trait SImpl { @@ -179,8 +178,8 @@ pub trait SImpl { impl SImpl for Ptr { fn bump(&self) { ({ - let _k: i32 = (*(*(*self).upgrade().deref()).tag.borrow()); - CounterImpl::add(&(*(*self).upgrade().deref()).c.as_pointer(), _k) + let _k: i32 = (*self).with(|__s| __s.tag); + CounterImpl::add(&field_ptr!((*self), c), _k) }); } } diff --git a/tests/unit/out/refcount/field_pointers.rs b/tests/unit/out/refcount/field_pointers.rs index b96e60810b..709834ee14 100644 --- a/tests/unit/out/refcount/field_pointers.rs +++ b/tests/unit/out/refcount/field_pointers.rs @@ -111,24 +111,6 @@ impl ByteRepr for Outer { fn byte_size() -> usize { 104 } - fn to_bytes(&self, buf: &mut [u8]) { - self.x.to_bytes(&mut buf[0..4]); - self.inner.to_bytes(&mut buf[4..16]); - (*self.items.borrow()).to_bytes(&mut buf[16..52]); - (*self.v.borrow()).to_bytes(&mut buf[56..80]); - self.cursor.to_bytes(&mut buf[80..88]); - (*self.buf.borrow()).to_bytes(&mut buf[88..104]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - x: ::from_bytes(&buf[0..4]), - inner: ::from_bytes(&buf[4..16]), - items: Rc::new(RefCell::new(>::from_bytes(&buf[16..52]))), - v: Rc::new(RefCell::new(>::from_bytes(&buf[56..80]))), - cursor: >::from_bytes(&buf[80..88]), - buf: Rc::new(RefCell::new(>::from_bytes(&buf[88..104]))), - } - } } pub fn set_0(p: Ptr, value: i32) { let p: Value> = Rc::new(RefCell::new(p)); @@ -296,8 +278,10 @@ impl OuterImpl for Ptr { } fn push(&self, k: i32) { let k: Value = Rc::new(RefCell::new(k)); - (*(*self).with(|__s| __s.v.clone()).borrow_mut()) - .push(((*k.borrow()) + (*self).with(|__s| __s.x))); + { + let __a1 = ((*k.borrow()) + (*self).with(|__s| __s.x)); + (*(*self).with(|__s| __s.v.clone()).borrow_mut()).push(__a1) + }; } } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/field_pointers_malloced_struct.rs b/tests/unit/out/refcount/field_pointers_malloced_struct.rs index 575df1dfbb..9ae1bf7a47 100644 --- a/tests/unit/out/refcount/field_pointers_malloced_struct.rs +++ b/tests/unit/out/refcount/field_pointers_malloced_struct.rs @@ -6,44 +6,39 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct S { - pub a: Value, - pub b: Value, - pub c: Value, -} -impl Clone for S { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - a: Rc::new(RefCell::new((*self.a.borrow()))), - b: Rc::new(RefCell::new((*self.b.borrow()))), - c: Rc::new(RefCell::new((*self.c.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } + #[offset(0)] + pub a: i32, + #[offset(4)] + pub b: i32, + #[offset(8)] + pub c: i32, } impl ByteRepr for S { fn byte_size() -> usize { 12 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.a.borrow()).to_bytes(&mut buf[0..4]); - (*self.b.borrow()).to_bytes(&mut buf[4..8]); - (*self.c.borrow()).to_bytes(&mut buf[8..12]); + self.a.to_bytes(&mut buf[0..4]); + self.b.to_bytes(&mut buf[4..8]); + self.c.to_bytes(&mut buf[8..12]); } fn from_bytes(buf: &[u8]) -> Self { Self { - a: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - b: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), - c: Rc::new(RefCell::new(::from_bytes(&buf[8..12]))), + a: ::from_bytes(&buf[0..4]), + b: ::from_bytes(&buf[4..8]), + c: ::from_bytes(&buf[8..12]), } } } pub fn bump_0(s: Ptr) -> i32 { let s: Value> = Rc::new(RefCell::new(s)); - (*(*(*s.borrow()).upgrade().deref()).b.borrow_mut()) += 10; - return (*(*(*s.borrow()).upgrade().deref()).b.borrow()); + { + let _ptr = field!((*s.borrow()), b); + _ptr.write(_ptr.read() + 10) + }; + return (*s.borrow()).with(|__s| __s.b); } pub fn main() { __cpp2rust_init_globals(); @@ -54,56 +49,54 @@ fn main_0() -> i32 { libcc2rs::calloc_refcount(1_usize, 12usize).reinterpret_cast::(), )); assert!(!((*s.borrow()).is_null())); - (*(*(*s.borrow()).upgrade().deref()).b.borrow_mut()) = 1; + field!((*s.borrow()), b).write(1); let __rhs = ({ bump_0((*s.borrow()).clone()) }); - (*(*(*s.borrow()).upgrade().deref()).a.borrow_mut()) = __rhs; - assert!(((*(*(*s.borrow()).upgrade().deref()).a.borrow()) == 11)); - assert!(((*(*(*s.borrow()).upgrade().deref()).b.borrow()) == 11)); - (*(*(*s.borrow()).upgrade().deref()).a.borrow_mut()) = 1; - (*(*(*s.borrow()).upgrade().deref()).b.borrow_mut()) = 2; - (*(*(*s.borrow()).upgrade().deref()).c.borrow_mut()) = 0; + field!((*s.borrow()), a).write(__rhs); + assert!(((*s.borrow()).with(|__s| __s.a) == 11)); + assert!(((*s.borrow()).with(|__s| __s.b) == 11)); + field!((*s.borrow()), a).write(1); + field!((*s.borrow()), b).write(2); + field!((*s.borrow()), c).write(0); if ({ - let _lhs = (*(*(*s.borrow()).upgrade().deref()).a.borrow()); - _lhs < (*(*(*s.borrow()).upgrade().deref()).b.borrow()) - }) && ((*(*(*s.borrow()).upgrade().deref()).c.borrow_mut()).postfix_inc() == 0) + let _lhs = (*s.borrow()).with(|__s| __s.a); + _lhs < (*s.borrow()).with(|__s| __s.b) + }) && (field!((*s.borrow()), c).with_mut(|__v| __v.postfix_inc()) == 0) { - (*(*(*s.borrow()).upgrade().deref()).a.borrow_mut()) = 5; + field!((*s.borrow()), a).write(5); } - assert!( - ((*(*(*s.borrow()).upgrade().deref()).a.borrow()) == 5) - && ((*(*(*s.borrow()).upgrade().deref()).c.borrow()) == 1) - ); + assert!(((*s.borrow()).with(|__s| __s.a) == 5) && ((*s.borrow()).with(|__s| __s.c) == 1)); if ({ - let _lhs = (*(*(*s.borrow()).upgrade().deref()).a.borrow()); - _lhs < (*(*(*s.borrow()).upgrade().deref()).b.borrow()) - }) && ((*(*(*s.borrow()).upgrade().deref()).c.borrow_mut()).postfix_inc() == 0) + let _lhs = (*s.borrow()).with(|__s| __s.a); + _lhs < (*s.borrow()).with(|__s| __s.b) + }) && (field!((*s.borrow()), c).with_mut(|__v| __v.postfix_inc()) == 0) { - (*(*(*s.borrow()).upgrade().deref()).a.borrow_mut()) = 6; + field!((*s.borrow()), a).write(6); } - assert!( - ((*(*(*s.borrow()).upgrade().deref()).a.borrow()) == 5) - && ((*(*(*s.borrow()).upgrade().deref()).c.borrow()) == 1) - ); + assert!(((*s.borrow()).with(|__s| __s.a) == 5) && ((*s.borrow()).with(|__s| __s.c) == 1)); let x: Value = Rc::new(RefCell::new({ - let _lhs = (*(*(*s.borrow()).upgrade().deref()).a.borrow()); + let _lhs = (*s.borrow()).with(|__s| __s.a); _lhs + ({ - (*(*(*s.borrow()).upgrade().deref()).b.borrow_mut()) = 3; - (*(*(*s.borrow()).upgrade().deref()).b.borrow()) + field!((*s.borrow()), b).write(3); + (*s.borrow()).with(|__s| __s.b) }) })); - assert!(((*x.borrow()) == 8) && ((*(*(*s.borrow()).upgrade().deref()).b.borrow()) == 3)); + assert!(((*x.borrow()) == 8) && ((*s.borrow()).with(|__s| __s.b) == 3)); let y: Value = Rc::new(RefCell::new(0)); - (*(*(*s.borrow()).upgrade().deref()).c.borrow_mut()) = ({ - (*y.borrow_mut()) = 99; - (*y.borrow()) - }); - assert!(((*(*(*s.borrow()).upgrade().deref()).c.borrow()) == 99) && ((*y.borrow()) == 99)); + field!((*s.borrow()), c).write( + ({ + (*y.borrow_mut()) = 99; + (*y.borrow()) + }), + ); + assert!(((*s.borrow()).with(|__s| __s.c) == 99) && ((*y.borrow()) == 99)); let __rhs = ({ bump_0((*s.borrow()).clone()) }); - (*(*(*s.borrow()).upgrade().deref()).a.borrow_mut()) += __rhs; + { + let _ptr = field!((*s.borrow()), a); + _ptr.write(_ptr.read() + __rhs) + }; assert!( - (((*(*(*s.borrow()).upgrade().deref()).a.borrow()) == 18) - && ((*(*(*s.borrow()).upgrade().deref()).b.borrow()) == 13)) - && ((*(*(*s.borrow()).upgrade().deref()).c.borrow()) == 99) + (((*s.borrow()).with(|__s| __s.a) == 18) && ((*s.borrow()).with(|__s| __s.b) == 13)) + && ((*s.borrow()).with(|__s| __s.c) == 99) ); libcc2rs::free_refcount((*s.borrow()).to_any()); return 0; diff --git a/tests/unit/out/refcount/find.rs b/tests/unit/out/refcount/find.rs index 4e72b3e105..8cd30e9296 100644 --- a/tests/unit/out/refcount/find.rs +++ b/tests/unit/out/refcount/find.rs @@ -12,9 +12,18 @@ pub fn main() { } fn main_0() -> i32 { let v: Value> = Rc::new(RefCell::new(Vec::new())); - (*v.borrow_mut()).push(1); - (*v.borrow_mut()).push(2); - (*v.borrow_mut()).push(3); + { + let __a1 = 1; + (*v.borrow_mut()).push(__a1) + }; + { + let __a1 = 2; + (*v.borrow_mut()).push(__a1) + }; + { + let __a1 = 3; + (*v.borrow_mut()).push(__a1) + }; 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( diff --git a/tests/unit/out/refcount/first_field_ptr_covers_struct.rs b/tests/unit/out/refcount/first_field_ptr_covers_struct.rs index 78fd3459ca..bd0db041c5 100644 --- a/tests/unit/out/refcount/first_field_ptr_covers_struct.rs +++ b/tests/unit/out/refcount/first_field_ptr_covers_struct.rs @@ -6,63 +6,51 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct In { - pub a: Value, - pub b: Value, -} -impl Clone for In { - fn clone(&self) -> Self { - Self { - a: Rc::new(RefCell::new((*self.a.borrow()).clone())), - b: Rc::new(RefCell::new((*self.b.borrow()).clone())), - } - } + #[offset(0)] + pub a: i16, + #[offset(2)] + pub b: i16, } impl ByteRepr for In { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.a.borrow()).to_bytes(&mut buf[0..2]); - (*self.b.borrow()).to_bytes(&mut buf[2..4]); + self.a.to_bytes(&mut buf[0..2]); + self.b.to_bytes(&mut buf[2..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - a: Rc::new(RefCell::new(::from_bytes(&buf[0..2]))), - b: Rc::new(RefCell::new(::from_bytes(&buf[2..4]))), + a: ::from_bytes(&buf[0..2]), + b: ::from_bytes(&buf[2..4]), } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct S { - pub x: Value, - pub in_: Value, - pub z: Value, -} -impl Clone for S { - fn clone(&self) -> Self { - Self { - x: Rc::new(RefCell::new((*self.x.borrow()).clone())), - in_: Rc::new(RefCell::new((*self.in_.borrow()).clone())), - z: Rc::new(RefCell::new((*self.z.borrow()).clone())), - } - } + #[offset(0)] + pub x: i32, + #[offset(4)] + pub in_: In, + #[offset(8)] + pub z: i32, } impl ByteRepr for S { fn byte_size() -> usize { 12 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.x.borrow()).to_bytes(&mut buf[0..4]); - (*self.in_.borrow()).to_bytes(&mut buf[4..8]); - (*self.z.borrow()).to_bytes(&mut buf[8..12]); + self.x.to_bytes(&mut buf[0..4]); + self.in_.to_bytes(&mut buf[4..8]); + self.z.to_bytes(&mut buf[8..12]); } fn from_bytes(buf: &[u8]) -> Self { Self { - x: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - in_: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), - z: Rc::new(RefCell::new(::from_bytes(&buf[8..12]))), + x: ::from_bytes(&buf[0..4]), + in_: ::from_bytes(&buf[4..8]), + z: ::from_bytes(&buf[8..12]), } } } @@ -72,71 +60,51 @@ pub fn main() { } fn main_0() -> i32 { let src: Value = Rc::new(RefCell::new(S { - x: Rc::new(RefCell::new(1)), - in_: Rc::new(RefCell::new(In { - a: Rc::new(RefCell::new(2_i16)), - b: Rc::new(RefCell::new(3_i16)), - })), - z: Rc::new(RefCell::new(4)), + x: 1, + in_: In { a: 2_i16, b: 3_i16 }, + z: 4, })); let p: Value> = Rc::new(RefCell::new( libcc2rs::malloc_refcount(12usize).reinterpret_cast::(), )); assert!((((!((*p.borrow()).is_null())) as i32) != 0)); { - (((*(*p.borrow()).upgrade().deref()).x.as_pointer()) as Ptr) + ((field_ptr!((*p.borrow()), x)) as Ptr) .to_any() .memcpy(&((src.as_pointer()) as Ptr).to_any(), 12usize as usize); - (((*(*p.borrow()).upgrade().deref()).x.as_pointer()) as Ptr).to_any() + ((field_ptr!((*p.borrow()), x)) as Ptr).to_any() }; assert!( - (((((((((((((*(*(*p.borrow()).upgrade().deref()).x.borrow()) == 1) as i32) != 0) - && (((((*(*(*(*p.borrow()).upgrade().deref()).in_.borrow()) - .a - .borrow()) as i32) - == 2) as i32) - != 0)) as i32) - != 0) - && (((((*(*(*(*p.borrow()).upgrade().deref()).in_.borrow()) - .b - .borrow()) as i32) - == 3) as i32) - != 0)) as i32) + (((((((((((((*p.borrow()).with(|__s| __s.x) == 1) as i32) != 0) + && (((((*p.borrow()).with(|__s| __s.in_.a) as i32) == 2) as i32) != 0)) + as i32) != 0) - && ((((*(*(*p.borrow()).upgrade().deref()).z.borrow()) == 4) as i32) != 0)) + && (((((*p.borrow()).with(|__s| __s.in_.b) as i32) == 3) as i32) != 0)) as i32) != 0) + && ((((*p.borrow()).with(|__s| __s.z) == 4) as i32) != 0)) as i32) + != 0) ); - let n: Value = Rc::new(RefCell::new(In { - a: Rc::new(RefCell::new(5_i16)), - b: Rc::new(RefCell::new(6_i16)), - })); + let n: Value = Rc::new(RefCell::new(In { a: 5_i16, b: 6_i16 })); { - (((*(*p.borrow()).upgrade().deref()).in_.as_pointer()) as Ptr) + ((field_ptr!((*p.borrow()), in_)) as Ptr) .to_any() .memcpy(&((n.as_pointer()) as Ptr).to_any(), 4usize as usize); - (((*(*p.borrow()).upgrade().deref()).in_.as_pointer()) as Ptr).to_any() + ((field_ptr!((*p.borrow()), in_)) as Ptr).to_any() }; assert!( - (((((((((((((*(*(*p.borrow()).upgrade().deref()).x.borrow()) == 1) as i32) != 0) - && (((((*(*(*(*p.borrow()).upgrade().deref()).in_.borrow()) - .a - .borrow()) as i32) - == 5) as i32) - != 0)) as i32) - != 0) - && (((((*(*(*(*p.borrow()).upgrade().deref()).in_.borrow()) - .b - .borrow()) as i32) - == 6) as i32) - != 0)) as i32) + (((((((((((((*p.borrow()).with(|__s| __s.x) == 1) as i32) != 0) + && (((((*p.borrow()).with(|__s| __s.in_.a) as i32) == 5) as i32) != 0)) + as i32) != 0) - && ((((*(*(*p.borrow()).upgrade().deref()).z.borrow()) == 4) as i32) != 0)) + && (((((*p.borrow()).with(|__s| __s.in_.b) as i32) == 6) as i32) != 0)) as i32) != 0) + && ((((*p.borrow()).with(|__s| __s.z) == 4) as i32) != 0)) as i32) + != 0) ); let bz: Value> = Rc::new(RefCell::new( - ((*(*p.borrow()).upgrade().deref()).z.as_pointer()).reinterpret_cast::(), + (field_ptr!((*p.borrow()), z)).reinterpret_cast::(), )); let i: Value = Rc::new(RefCell::new(0)); 'loop_: while ((((*i.borrow()) < 4) as i32) != 0) { @@ -144,12 +112,9 @@ fn main_0() -> i32 { (*i.borrow_mut()).postfix_inc(); } assert!( - (((((((*(*(*p.borrow()).upgrade().deref()).z.borrow()) == 16843009) as i32) != 0) - && (((((*(*(*(*p.borrow()).upgrade().deref()).in_.borrow()) - .b - .borrow()) as i32) - == 6) as i32) - != 0)) as i32) + (((((((*p.borrow()).with(|__s| __s.z) == 16843009) as i32) != 0) + && (((((*p.borrow()).with(|__s| __s.in_.b) as i32) == 6) as i32) != 0)) + as i32) != 0) ); libcc2rs::free_refcount((*p.borrow()).to_any()); diff --git a/tests/unit/out/refcount/first_member_downcast.rs b/tests/unit/out/refcount/first_member_downcast.rs index 55b74f468b..ecf020c4ed 100644 --- a/tests/unit/out/refcount/first_member_downcast.rs +++ b/tests/unit/out/refcount/first_member_downcast.rs @@ -6,55 +6,43 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct base { - pub kind: Value, -} -impl Clone for base { - fn clone(&self) -> Self { - Self { - kind: Rc::new(RefCell::new((*self.kind.borrow()).clone())), - } - } + #[offset(0)] + pub kind: i32, } impl ByteRepr for base { fn byte_size() -> usize { 4 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.kind.borrow()).to_bytes(&mut buf[0..4]); + self.kind.to_bytes(&mut buf[0..4]); } fn from_bytes(buf: &[u8]) -> Self { Self { - kind: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + kind: ::from_bytes(&buf[0..4]), } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct derived { - pub head: Value, - pub value: Value, -} -impl Clone for derived { - fn clone(&self) -> Self { - Self { - head: Rc::new(RefCell::new((*self.head.borrow()).clone())), - value: Rc::new(RefCell::new((*self.value.borrow()).clone())), - } - } + #[offset(0)] + pub head: base, + #[offset(8)] + pub value: usize, } impl ByteRepr for derived { fn byte_size() -> usize { 16 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.head.borrow()).to_bytes(&mut buf[0..4]); - (*self.value.borrow()).to_bytes(&mut buf[8..16]); + self.head.to_bytes(&mut buf[0..4]); + self.value.to_bytes(&mut buf[8..16]); } fn from_bytes(buf: &[u8]) -> Self { Self { - head: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - value: Rc::new(RefCell::new(::from_bytes(&buf[8..16]))), + head: ::from_bytes(&buf[0..4]), + value: ::from_bytes(&buf[8..16]), } } } @@ -67,13 +55,9 @@ fn main_0() -> i32 { libcc2rs::malloc_refcount(16usize).reinterpret_cast::(), )); assert!((((!((*d.borrow()).is_null())) as i32) != 0)); - (*(*(*(*d.borrow()).upgrade().deref()).head.borrow()) - .kind - .borrow_mut()) = 3; - (*(*(*d.borrow()).upgrade().deref()).value.borrow_mut()) = 7_usize; - let b: Value> = Rc::new(RefCell::new( - ((*(*d.borrow()).upgrade().deref()).head.as_pointer()), - )); + field!(field!((*d.borrow()), head), kind).write(3); + field!((*d.borrow()), value).write(7_usize); + let b: Value> = Rc::new(RefCell::new((field_ptr!((*d.borrow()), head)))); let back: Value> = Rc::new(RefCell::new((*b.borrow()).reinterpret_cast::())); assert!( @@ -83,24 +67,12 @@ fn main_0() -> i32 { }) as i32) != 0) ); - assert!(((((*(*(*back.borrow()).upgrade().deref()).value.borrow()) == 7_usize) as i32) != 0)); - assert!( - ((((*(*(*(*back.borrow()).upgrade().deref()).head.borrow()) - .kind - .borrow()) - == 3) as i32) - != 0) - ); - (*(*(*back.borrow()).upgrade().deref()).value.borrow_mut()) = 8_usize; - assert!(((((*(*(*d.borrow()).upgrade().deref()).value.borrow()) == 8_usize) as i32) != 0)); - (*(*(*b.borrow()).upgrade().deref()).kind.borrow_mut()) = 4; - assert!( - ((((*(*(*(*d.borrow()).upgrade().deref()).head.borrow()) - .kind - .borrow()) - == 4) as i32) - != 0) - ); + assert!(((((*back.borrow()).with(|__s| __s.value) == 7_usize) as i32) != 0)); + assert!(((((*back.borrow()).with(|__s| __s.head.kind) == 3) as i32) != 0)); + field!((*back.borrow()), value).write(8_usize); + assert!(((((*d.borrow()).with(|__s| __s.value) == 8_usize) as i32) != 0)); + field!((*b.borrow()), kind).write(4); + assert!(((((*d.borrow()).with(|__s| __s.head.kind) == 4) as i32) != 0)); libcc2rs::free_refcount((*back.borrow()).to_any()); return 0; } diff --git a/tests/unit/out/refcount/flex_array_member_sibling_write.rs b/tests/unit/out/refcount/flex_array_member_sibling_write.rs index 5289edb284..0388141469 100644 --- a/tests/unit/out/refcount/flex_array_member_sibling_write.rs +++ b/tests/unit/out/refcount/flex_array_member_sibling_write.rs @@ -6,15 +6,17 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg)] +#[derive(Record, VaArg, FnPtrArg)] pub struct S { - pub n: Value, + #[offset(0)] + pub n: i32, + #[offset(4)] pub name: Value>, } impl Clone for S { fn clone(&self) -> Self { Self { - n: Rc::new(RefCell::new((*self.n.borrow()).clone())), + n: self.n.clone(), name: Rc::new(RefCell::new((*self.name.borrow()).clone())), } } @@ -22,7 +24,7 @@ impl Clone for S { impl Default for S { fn default() -> Self { S { - n: Rc::new(RefCell::new(0_i32)), + n: 0_i32, name: Rc::new(RefCell::new((0..1).map(|_| 0_u8).collect::>())), } } @@ -32,55 +34,52 @@ impl ByteRepr for S { 8 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.n.borrow()).to_bytes(&mut buf[0..4]); + self.n.to_bytes(&mut buf[0..4]); (*self.name.borrow()).to_bytes(&mut buf[4..5]); } fn from_bytes(buf: &[u8]) -> Self { Self { - n: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + n: ::from_bytes(&buf[0..4]), name: Rc::new(RefCell::new(>::from_bytes(&buf[4..5]))), } } } -#[derive(VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] pub struct E { - pub id: Value, - pub w: Value, -} -impl Clone for E { - fn clone(&self) -> Self { - Self { - id: Rc::new(RefCell::new((*self.id.borrow()).clone())), - w: Rc::new(RefCell::new((*self.w.borrow()).clone())), - } - } + #[offset(0)] + pub id: i32, + #[offset(4)] + pub w: i32, } impl ByteRepr for E { fn byte_size() -> usize { 8 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.id.borrow()).to_bytes(&mut buf[0..4]); - (*self.w.borrow()).to_bytes(&mut buf[4..8]); + self.id.to_bytes(&mut buf[0..4]); + self.w.to_bytes(&mut buf[4..8]); } fn from_bytes(buf: &[u8]) -> Self { Self { - id: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - w: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + id: ::from_bytes(&buf[0..4]), + w: ::from_bytes(&buf[4..8]), } } } -#[derive(VaArg, FnPtrArg)] +#[derive(Record, VaArg, FnPtrArg)] pub struct T { - pub n: Value, - pub cap: Value, + #[offset(0)] + pub n: i32, + #[offset(4)] + pub cap: i32, + #[offset(8)] pub a: Value>, } impl Clone for T { fn clone(&self) -> Self { Self { - n: Rc::new(RefCell::new((*self.n.borrow()).clone())), - cap: Rc::new(RefCell::new((*self.cap.borrow()).clone())), + n: self.n.clone(), + cap: self.cap.clone(), a: Rc::new(RefCell::new((*self.a.borrow()).clone())), } } @@ -88,8 +87,8 @@ impl Clone for T { impl Default for T { fn default() -> Self { T { - n: Rc::new(RefCell::new(0_i32)), - cap: Rc::new(RefCell::new(0_i32)), + n: 0_i32, + cap: 0_i32, a: Rc::new(RefCell::new( (0..1).map(|_| ::default()).collect::>(), )), @@ -101,14 +100,14 @@ impl ByteRepr for T { 16 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.n.borrow()).to_bytes(&mut buf[0..4]); - (*self.cap.borrow()).to_bytes(&mut buf[4..8]); + self.n.to_bytes(&mut buf[0..4]); + self.cap.to_bytes(&mut buf[4..8]); (*self.a.borrow()).to_bytes(&mut buf[8..16]); } fn from_bytes(buf: &[u8]) -> Self { Self { - n: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - cap: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + n: ::from_bytes(&buf[0..4]), + cap: ::from_bytes(&buf[4..8]), a: Rc::new(RefCell::new(>::from_bytes(&buf[8..16]))), } } @@ -124,19 +123,19 @@ fn main_0() -> i32 { )); assert!((((!((*s.borrow()).is_null())) as i32) != 0)); { - (((*(*s.borrow()).upgrade().deref()).name.as_pointer() as Ptr) as Ptr) + (((*s.borrow()).with(|__s| __s.name.clone()).as_pointer() as Ptr) as Ptr) .to_any() .memcpy( &Ptr::::from_string_literal(b"abcdefg").to_any(), 8_usize as usize, ); - (((*(*s.borrow()).upgrade().deref()).name.as_pointer() as Ptr) as Ptr).to_any() + (((*s.borrow()).with(|__s| __s.name.clone()).as_pointer() as Ptr) as Ptr).to_any() }; - (*(*(*s.borrow()).upgrade().deref()).n.borrow_mut()) = 5; - assert!(((((*(*(*s.borrow()).upgrade().deref()).n.borrow()) == 5) as i32) != 0)); + field!((*s.borrow()), n).write(5); + assert!(((((*s.borrow()).with(|__s| __s.n) == 5) as i32) != 0)); assert!( ((({ - let mut __it1 = ((*(*s.borrow()).upgrade().deref()).name.as_pointer() as Ptr) + let mut __it1 = ((*s.borrow()).with(|__s| __s.name.clone()).as_pointer() as Ptr) .to_c_string_iterator(); let mut __it2 = Ptr::::from_string_literal(b"abcdefg").to_c_string_iterator(); loop { @@ -158,25 +157,17 @@ fn main_0() -> i32 { .reinterpret_cast::(), )); assert!((((!((*t.borrow()).is_null())) as i32) != 0)); - (*(*(*t.borrow()).upgrade().deref()).n.borrow_mut()) = 2; - (*(*(*t.borrow()).upgrade().deref()).cap.borrow_mut()) = 2; - (*(*(*(*t.borrow()).upgrade().deref()).a.borrow())[(0) as usize] - .id - .borrow_mut()) = 10; - (*(*(*(*t.borrow()).upgrade().deref()).a.borrow())[(1) as usize] - .w - .borrow_mut()) = 20; - (*(*(*t.borrow()).upgrade().deref()).n.borrow_mut()) = 3; + field!((*t.borrow()), n).write(2); + field!((*t.borrow()), cap).write(2); + (*(*t.borrow()).with(|__s| __s.a.clone()).borrow_mut())[(0) as usize].id = 10; + (*(*t.borrow()).with(|__s| __s.a.clone()).borrow_mut())[(1) as usize].w = 20; + field!((*t.borrow()), n).write(3); assert!( - (((((((*(*(*(*t.borrow()).upgrade().deref()).a.borrow())[(0) as usize] - .id - .borrow()) - == 10) as i32) + (((((({ (*(*t.borrow()).with(|__s| __s.a.clone()).borrow())[(0) as usize].id } == 10) + as i32) != 0) - && ((((*(*(*(*t.borrow()).upgrade().deref()).a.borrow())[(1) as usize] - .w - .borrow()) - == 20) as i32) + && ((({ (*(*t.borrow()).with(|__s| __s.a.clone()).borrow())[(1) as usize].w } == 20) + as i32) != 0)) as i32) != 0) ); @@ -186,31 +177,24 @@ fn main_0() -> i32 { assert!( ((({ let _lhs = (*tail.borrow()).clone(); - _lhs == (((*(*t.borrow()).upgrade().deref()).a.as_pointer() as Ptr).offset(1)) + _lhs == (((*t.borrow()).with(|__s| __s.a.clone()).as_pointer() as Ptr).offset(1)) }) as i32) != 0) ); - (*(*(*tail.borrow()).offset((0) as isize).upgrade().deref()) - .id - .borrow_mut()) = 30; - (*(*(*t.borrow()).upgrade().deref()).cap.borrow_mut()) = 4; + field!((*tail.borrow()).offset((0) as isize), id).write(30); + field!((*t.borrow()), cap).write(4); assert!( - (((((((*(*(*(*t.borrow()).upgrade().deref()).a.borrow())[(1) as usize] - .id - .borrow()) - == 30) as i32) + (((((({ (*(*t.borrow()).with(|__s| __s.a.clone()).borrow())[(1) as usize].id } == 30) + as i32) != 0) - && ((((*(*(*(*t.borrow()).upgrade().deref()).a.borrow())[(1) as usize] - .w - .borrow()) - == 20) as i32) + && ((({ (*(*t.borrow()).with(|__s| __s.a.clone()).borrow())[(1) as usize].w } == 20) + as i32) != 0)) as i32) != 0) ); assert!( - (((((((*(*(*t.borrow()).upgrade().deref()).n.borrow()) == 3) as i32) != 0) - && ((((*(*(*t.borrow()).upgrade().deref()).cap.borrow()) == 4) as i32) != 0)) - as i32) + (((((((*t.borrow()).with(|__s| __s.n) == 3) as i32) != 0) + && ((((*t.borrow()).with(|__s| __s.cap) == 4) as i32) != 0)) as i32) != 0) ); libcc2rs::free_refcount((*t.borrow()).to_any()); diff --git a/tests/unit/out/refcount/fn_ptr_stable_sort.rs b/tests/unit/out/refcount/fn_ptr_stable_sort.rs index cefc527b5a..bbadf1cac4 100644 --- a/tests/unit/out/refcount/fn_ptr_stable_sort.rs +++ b/tests/unit/out/refcount/fn_ptr_stable_sort.rs @@ -40,9 +40,18 @@ pub fn main() { } fn main_0() -> i32 { let v: Value> = Rc::new(RefCell::new(Vec::new())); - (*v.borrow_mut()).push(Item { key: 3, value: 30 }); - (*v.borrow_mut()).push(Item { key: 1, value: 10 }); - (*v.borrow_mut()).push(Item { key: 2, value: 20 }); + { + let __a1 = Item { key: 3, value: 30 }; + (*v.borrow_mut()).push(__a1) + }; + { + let __a1 = Item { key: 1, value: 10 }; + (*v.borrow_mut()).push(__a1) + }; + { + let __a1 = Item { key: 2, value: 20 }; + (*v.borrow_mut()).push(__a1) + }; (v.as_pointer() as Ptr).sort_with_cmp( (v.as_pointer() as Ptr).to_end().get_offset(), |x, y| Compare_0.call(x, y), diff --git a/tests/unit/out/refcount/foreach_disjoint_field_borrow.rs b/tests/unit/out/refcount/foreach_disjoint_field_borrow.rs index 06ab38ee80..d491578ada 100644 --- a/tests/unit/out/refcount/foreach_disjoint_field_borrow.rs +++ b/tests/unit/out/refcount/foreach_disjoint_field_borrow.rs @@ -21,32 +21,21 @@ impl Clone for S { } } } -<<<<<<< HEAD -impl ByteRepr for S {} -======= impl ByteRepr for S { fn byte_size() -> usize { 32 } - fn to_bytes(&self, buf: &mut [u8]) { - (*self.v.borrow()).to_bytes(&mut buf[0..24]); - self.a.to_bytes(&mut buf[24..28]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - v: Rc::new(RefCell::new(>::from_bytes(&buf[0..24]))), - a: ::from_bytes(&buf[24..28]), - } - } } ->>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub fn main() { __cpp2rust_init_globals(); std::process::exit(main_0()); } fn main_0() -> i32 { let s: Value = Rc::new(RefCell::new(::default())); - (*{ (*s.borrow()).v.clone() }.borrow_mut()).push(1); + { + let __a1 = 1; + (*{ (*s.borrow()).v.clone() }.borrow_mut()).push(__a1) + }; 'loop_: for mut e in { (*s.borrow()).v.clone() }.as_pointer() as Ptr { let e: Value = Rc::new(RefCell::new(e.read())); (*s.borrow_mut()).a.postfix_inc(); diff --git a/tests/unit/out/refcount/foreach_double.rs b/tests/unit/out/refcount/foreach_double.rs index 5ead1eb9ca..743a2e4796 100644 --- a/tests/unit/out/refcount/foreach_double.rs +++ b/tests/unit/out/refcount/foreach_double.rs @@ -12,9 +12,18 @@ pub fn main() { } fn main_0() -> i32 { let v: Value> = Rc::new(RefCell::new(Vec::new())); - (*v.borrow_mut()).push(1); - (*v.borrow_mut()).push(2); - (*v.borrow_mut()).push(3); + { + let __a1 = 1; + (*v.borrow_mut()).push(__a1) + }; + { + let __a1 = 2; + (*v.borrow_mut()).push(__a1) + }; + { + let __a1 = 3; + (*v.borrow_mut()).push(__a1) + }; 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())); diff --git a/tests/unit/out/refcount/foreach_mut.rs b/tests/unit/out/refcount/foreach_mut.rs index b9589d94a6..c48094173f 100644 --- a/tests/unit/out/refcount/foreach_mut.rs +++ b/tests/unit/out/refcount/foreach_mut.rs @@ -12,9 +12,18 @@ pub fn main() { } fn main_0() -> i32 { let v1: Value> = Rc::new(RefCell::new(Vec::new())); - (*v1.borrow_mut()).push(1); - (*v1.borrow_mut()).push(2); - (*v1.borrow_mut()).push(3); + { + let __a1 = 1; + (*v1.borrow_mut()).push(__a1) + }; + { + let __a1 = 2; + (*v1.borrow_mut()).push(__a1) + }; + { + let __a1 = 3; + (*v1.borrow_mut()).push(__a1) + }; 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())); @@ -35,9 +44,18 @@ fn main_0() -> i32 { (*sum.borrow_mut()) += __rhs; } let v2: Value>> = Rc::new(RefCell::new(Vec::new())); - (*v2.borrow_mut()).push(((v1.as_pointer() as Ptr).offset(0_usize))); - (*v2.borrow_mut()).push(((v1.as_pointer() as Ptr).offset(1_usize))); - (*v2.borrow_mut()).push(((v1.as_pointer() as Ptr).offset(2_usize))); + { + let __a1 = ((v1.as_pointer() as Ptr).offset(0_usize)); + (*v2.borrow_mut()).push(__a1) + }; + { + let __a1 = ((v1.as_pointer() as Ptr).offset(1_usize)); + (*v2.borrow_mut()).push(__a1) + }; + { + let __a1 = ((v1.as_pointer() as Ptr).offset(2_usize)); + (*v2.borrow_mut()).push(__a1) + }; 'loop_: for mut p in v2.as_pointer() as Ptr> { let p: Value> = Rc::new(RefCell::new(p.read())); { diff --git a/tests/unit/out/refcount/forward_basic.rs b/tests/unit/out/refcount/forward_basic.rs index a7adc3a592..a3e18fa914 100644 --- a/tests/unit/out/refcount/forward_basic.rs +++ b/tests/unit/out/refcount/forward_basic.rs @@ -59,27 +59,11 @@ impl Clone for Tracked { Tracked::copy_from(__src.as_pointer()) } } -<<<<<<< HEAD -impl ByteRepr for Tracked {} -======= impl ByteRepr for Tracked { fn byte_size() -> usize { 12 } - fn to_bytes(&self, buf: &mut [u8]) { - self.v.to_bytes(&mut buf[0..4]); - self.copies.to_bytes(&mut buf[4..8]); - self.moves.to_bytes(&mut buf[8..12]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - v: ::from_bytes(&buf[0..4]), - copies: ::from_bytes(&buf[4..8]), - moves: ::from_bytes(&buf[8..12]), - } - } } ->>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub fn chosen_overload_0(_a0: Ptr) -> Overload { return Overload_kLvalueOverload; } @@ -109,23 +93,11 @@ pub struct Holder { #[offset(0)] pub t: Tracked, } -<<<<<<< HEAD -impl ByteRepr for Holder {} -======= impl ByteRepr for Holder { fn byte_size() -> usize { 12 } - fn to_bytes(&self, buf: &mut [u8]) { - self.t.to_bytes(&mut buf[0..12]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - t: ::from_bytes(&buf[0..12]), - } - } } ->>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub fn forward_once_2(x: Ptr) -> Overload { return ({ chosen_overload_0((x).clone()) }); } diff --git a/tests/unit/out/refcount/forward_decltype.rs b/tests/unit/out/refcount/forward_decltype.rs index d81242b7e0..38077fd24d 100644 --- a/tests/unit/out/refcount/forward_decltype.rs +++ b/tests/unit/out/refcount/forward_decltype.rs @@ -59,27 +59,11 @@ impl Clone for Tracked { Tracked::copy_from(__src.as_pointer()) } } -<<<<<<< HEAD -impl ByteRepr for Tracked {} -======= impl ByteRepr for Tracked { fn byte_size() -> usize { 12 } - fn to_bytes(&self, buf: &mut [u8]) { - self.v.to_bytes(&mut buf[0..4]); - self.copies.to_bytes(&mut buf[4..8]); - self.moves.to_bytes(&mut buf[8..12]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - v: ::from_bytes(&buf[0..4]), - copies: ::from_bytes(&buf[4..8]), - moves: ::from_bytes(&buf[8..12]), - } - } } ->>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub fn chosen_overload_0(_a0: Ptr) -> Overload { return Overload_kLvalueOverload; } diff --git a/tests/unit/out/refcount/forward_explicit.rs b/tests/unit/out/refcount/forward_explicit.rs index 00cc54ae15..6470d1f790 100644 --- a/tests/unit/out/refcount/forward_explicit.rs +++ b/tests/unit/out/refcount/forward_explicit.rs @@ -61,27 +61,11 @@ impl Clone for Tracked { Tracked::copy_from(__src.as_pointer()) } } -<<<<<<< HEAD -impl ByteRepr for Tracked {} -======= impl ByteRepr for Tracked { fn byte_size() -> usize { 12 } - fn to_bytes(&self, buf: &mut [u8]) { - self.v.to_bytes(&mut buf[0..4]); - self.copies.to_bytes(&mut buf[4..8]); - self.moves.to_bytes(&mut buf[8..12]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - v: ::from_bytes(&buf[0..4]), - copies: ::from_bytes(&buf[4..8]), - moves: ::from_bytes(&buf[8..12]), - } - } } ->>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub fn chosen_overload_0(_a0: Ptr) -> Overload { return Overload_kLvalueOverload; } diff --git a/tests/unit/out/refcount/forward_pack.rs b/tests/unit/out/refcount/forward_pack.rs index be7d5c9724..d1ac6ab50a 100644 --- a/tests/unit/out/refcount/forward_pack.rs +++ b/tests/unit/out/refcount/forward_pack.rs @@ -61,27 +61,11 @@ impl Clone for Tracked { Tracked::copy_from(__src.as_pointer()) } } -<<<<<<< HEAD -impl ByteRepr for Tracked {} -======= impl ByteRepr for Tracked { fn byte_size() -> usize { 12 } - fn to_bytes(&self, buf: &mut [u8]) { - self.v.to_bytes(&mut buf[0..4]); - self.copies.to_bytes(&mut buf[4..8]); - self.moves.to_bytes(&mut buf[8..12]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - v: ::from_bytes(&buf[0..4]), - copies: ::from_bytes(&buf[4..8]), - moves: ::from_bytes(&buf[8..12]), - } - } } ->>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub fn chosen_overload_0(_a0: Ptr) -> Overload { return Overload_kLvalueOverload; } @@ -169,25 +153,11 @@ pub struct Pair { #[offset(12)] pub b: Tracked, } -<<<<<<< HEAD -impl ByteRepr for Pair {} -======= impl ByteRepr for Pair { fn byte_size() -> usize { 24 } - fn to_bytes(&self, buf: &mut [u8]) { - self.a.to_bytes(&mut buf[0..12]); - self.b.to_bytes(&mut buf[12..24]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - a: ::from_bytes(&buf[0..12]), - b: ::from_bytes(&buf[12..24]), - } - } } ->>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub fn forward_pack_into_ctor_8(args_0: Ptr, args_1: Ptr) -> Pair { return Pair::new({ (args_0).clone() }, { (args_1).clone() }); } diff --git a/tests/unit/out/refcount/huffman.rs b/tests/unit/out/refcount/huffman.rs index 0a39dc248e..23675f9bc9 100644 --- a/tests/unit/out/refcount/huffman.rs +++ b/tests/unit/out/refcount/huffman.rs @@ -86,31 +86,11 @@ impl MinHeap { Rc::try_unwrap(__this).ok().unwrap().into_inner() } } -<<<<<<< HEAD -impl ByteRepr for MinHeap {} -======= impl ByteRepr for MinHeap { fn byte_size() -> usize { 32 } - fn to_bytes(&self, buf: &mut [u8]) { - self.size.to_bytes(&mut buf[0..4]); - self.capacity.to_bytes(&mut buf[4..8]); - self.arr.to_bytes(&mut buf[8..16]); - self.next.to_bytes(&mut buf[16..20]); - self.alloc.to_bytes(&mut buf[24..32]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - size: ::from_bytes(&buf[0..4]), - capacity: ::from_bytes(&buf[4..8]), - arr: ]>>>>::from_bytes(&buf[8..16]), - next: ::from_bytes(&buf[16..20]), - alloc: >>>::from_bytes(&buf[24..32]), - } - } } ->>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub fn AllocMinHeap_1(capacity: i32) -> Option> { let capacity: Value = Rc::new(RefCell::new(capacity)); let minHeap: Value>> = diff --git a/tests/unit/out/refcount/implicit_autoref.rs b/tests/unit/out/refcount/implicit_autoref.rs index 0c65ead544..4505cc4d32 100644 --- a/tests/unit/out/refcount/implicit_autoref.rs +++ b/tests/unit/out/refcount/implicit_autoref.rs @@ -18,23 +18,11 @@ impl Clone for Holder { } } } -<<<<<<< HEAD -impl ByteRepr for Holder {} -======= impl ByteRepr for Holder { fn byte_size() -> usize { 24 } - fn to_bytes(&self, buf: &mut [u8]) { - (*self.v.borrow()).to_bytes(&mut buf[0..24]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - v: Rc::new(RefCell::new(>::from_bytes(&buf[0..24]))), - } - } } ->>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub fn write_through_0(p: Ptr) { let p: Value> = Rc::new(RefCell::new(p)); (*p.borrow()).write(42); @@ -45,8 +33,14 @@ pub fn main() { } fn main_0() -> i32 { let v: Value> = Rc::new(RefCell::new(Vec::new())); - (*v.borrow_mut()).push(10); - (*v.borrow_mut()).push(20); + { + let __a1 = 10; + (*v.borrow_mut()).push(__a1) + }; + { + let __a1 = 20; + (*v.borrow_mut()).push(__a1) + }; let p: Value>> = Rc::new(RefCell::new((v.as_pointer()))); let a: Value = Rc::new(RefCell::new( (((Ptr::>::decay(&(*p.borrow()))) as Ptr) @@ -57,8 +51,14 @@ fn main_0() -> i32 { .offset(1_usize) .write(30); let h: Value = Rc::new(RefCell::new(::default())); - (*{ (*h.borrow()).v.clone() }.borrow_mut()).push(40); - (*{ (*h.borrow()).v.clone() }.borrow_mut()).push(50); + { + let __a1 = 40; + (*{ (*h.borrow()).v.clone() }.borrow_mut()).push(__a1) + }; + { + let __a1 = 50; + (*{ (*h.borrow()).v.clone() }.borrow_mut()).push(__a1) + }; let hp: Value> = Rc::new(RefCell::new((h.as_pointer()))); let b: Value = Rc::new(RefCell::new( (((*hp.borrow()).with(|__s| (__s).v.clone()).as_pointer() as Ptr) diff --git a/tests/unit/out/refcount/iterators.rs b/tests/unit/out/refcount/iterators.rs index 2e82a16cf7..b6a408d10b 100644 --- a/tests/unit/out/refcount/iterators.rs +++ b/tests/unit/out/refcount/iterators.rs @@ -27,8 +27,14 @@ fn main_0() -> i32 { println!("{}", ((*c.borrow()) as i32) as u8 as char); } let v: Value>> = Rc::new(RefCell::new(Vec::new())); - (*v.borrow_mut()).push(Ptr::alloc(2)); - (*v.borrow_mut()).push(Ptr::alloc(3)); + { + let __a1 = Ptr::alloc(2); + (*v.borrow_mut()).push(__a1) + }; + { + let __a1 = Ptr::alloc(3); + (*v.borrow_mut()).push(__a1) + }; 'loop_: for mut p in v.as_pointer() as Ptr> { let p: Value> = Rc::new(RefCell::new(p.read())); println!("{}", ((*p.borrow()).read())); diff --git a/tests/unit/out/refcount/kruskal.rs b/tests/unit/out/refcount/kruskal.rs index 890bbae59c..e21a53674e 100644 --- a/tests/unit/out/refcount/kruskal.rs +++ b/tests/unit/out/refcount/kruskal.rs @@ -218,29 +218,12 @@ impl DisjointSet { Rc::try_unwrap(__this).ok().unwrap().into_inner() } } -<<<<<<< HEAD -impl ByteRepr for DisjointSet {} -#[derive(Default)] -======= impl ByteRepr for DisjointSet { fn byte_size() -> usize { 24 } - fn to_bytes(&self, buf: &mut [u8]) { - self.rank.to_bytes(&mut buf[0..8]); - self.parent.to_bytes(&mut buf[8..16]); - self.n.to_bytes(&mut buf[16..20]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - rank: >>>::from_bytes(&buf[0..8]), - parent: >>>::from_bytes(&buf[8..16]), - n: ::from_bytes(&buf[16..20]), - } - } } #[derive(Record, Default)] ->>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub struct Graph { #[offset(0)] pub edges: Option>>, @@ -260,27 +243,11 @@ impl Graph { Rc::try_unwrap(__this).ok().unwrap().into_inner() } } -<<<<<<< HEAD -impl ByteRepr for Graph {} -======= impl ByteRepr for Graph { fn byte_size() -> usize { 16 } - fn to_bytes(&self, buf: &mut [u8]) { - self.edges.to_bytes(&mut buf[0..8]); - self.V.to_bytes(&mut buf[8..12]); - self.E.to_bytes(&mut buf[12..16]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - edges: >>>::from_bytes(&buf[0..8]), - V: ::from_bytes(&buf[8..12]), - E: ::from_bytes(&buf[12..16]), - } - } } ->>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub fn MSTKruskal_2(graph: Ptr) -> f64 { ({ let _arr: Ptr>>> = field_ptr!(graph, edges); diff --git a/tests/unit/out/refcount/libc_struct_without_default.rs b/tests/unit/out/refcount/libc_struct_without_default.rs index 9f7b35ecb0..5beabbc328 100644 --- a/tests/unit/out/refcount/libc_struct_without_default.rs +++ b/tests/unit/out/refcount/libc_struct_without_default.rs @@ -31,27 +31,12 @@ impl Default for UserDefined { } } } -<<<<<<< HEAD -impl ByteRepr for UserDefined {} -#[derive(VaArg, FnPtrArg)] -======= impl ByteRepr for UserDefined { fn byte_size() -> usize { 32 } - fn to_bytes(&self, buf: &mut [u8]) { - (*self.a.borrow()).to_bytes(&mut buf[0..4]); - (*self.v.borrow()).to_bytes(&mut buf[8..32]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - a: Rc::new(RefCell::new(>::from_bytes(&buf[0..4]))), - v: Rc::new(RefCell::new(>::from_bytes(&buf[8..32]))), - } - } } #[derive(Clone, Record, VaArg, FnPtrArg)] ->>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub struct FieldIsLibcType { #[offset(0)] pub addr: libcc2rs::Sockaddr, diff --git a/tests/unit/out/refcount/map.rs b/tests/unit/out/refcount/map.rs index d9424f5739..a89eef1a59 100644 --- a/tests/unit/out/refcount/map.rs +++ b/tests/unit/out/refcount/map.rs @@ -349,17 +349,26 @@ fn main_0() -> i32 { let indexes: Value> = Rc::new(RefCell::new(Vec::new())); let i: Value = Rc::new(RefCell::new(60_u32)); 'loop_: while ((*i.borrow()) > 30_u32) { - (*indexes.borrow_mut()).push(((*i.borrow()) as i32)); + { + let __a1 = ((*i.borrow()) as i32); + (*indexes.borrow_mut()).push(__a1) + }; (*i.borrow_mut()).prefix_dec(); } let i: Value = Rc::new(RefCell::new(100_u32)); 'loop_: while ((*i.borrow()) > 60_u32) { - (*indexes.borrow_mut()).push(((*i.borrow()) as i32)); + { + let __a1 = ((*i.borrow()) as i32); + (*indexes.borrow_mut()).push(__a1) + }; (*i.borrow_mut()).prefix_dec(); } let i: Value = Rc::new(RefCell::new(30_u32)); 'loop_: while ((*i.borrow()) > 0_u32) { - (*indexes.borrow_mut()).push(((*i.borrow()) as i32)); + { + let __a1 = ((*i.borrow()) as i32); + (*indexes.borrow_mut()).push(__a1) + }; (*i.borrow_mut()).prefix_dec(); } let i: Value = Rc::new(RefCell::new(0_u32)); diff --git a/tests/unit/out/refcount/memset_struct_tail.rs b/tests/unit/out/refcount/memset_struct_tail.rs index 9636b41ae6..c992ec0505 100644 --- a/tests/unit/out/refcount/memset_struct_tail.rs +++ b/tests/unit/out/refcount/memset_struct_tail.rs @@ -6,33 +6,38 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg)] +#[derive(Record, VaArg, FnPtrArg)] pub struct S { - pub keep: Value, - pub a: Value, - pub b: Value, + #[offset(0)] + pub keep: i32, + #[offset(4)] + pub a: i32, + #[offset(8)] + pub b: i64, + #[offset(16)] pub c: Value>, - pub last: Value, + #[offset(24)] + pub last: i32, } impl Clone for S { fn clone(&self) -> Self { Self { - keep: Rc::new(RefCell::new((*self.keep.borrow()).clone())), - a: Rc::new(RefCell::new((*self.a.borrow()).clone())), - b: Rc::new(RefCell::new((*self.b.borrow()).clone())), + keep: self.keep.clone(), + a: self.a.clone(), + b: self.b.clone(), c: Rc::new(RefCell::new((*self.c.borrow()).clone())), - last: Rc::new(RefCell::new((*self.last.borrow()).clone())), + last: self.last.clone(), } } } impl Default for S { fn default() -> Self { S { - keep: Rc::new(RefCell::new(0_i32)), - a: Rc::new(RefCell::new(0_i32)), - b: Rc::new(RefCell::new(0_i64)), + keep: 0_i32, + a: 0_i32, + b: 0_i64, c: Rc::new(RefCell::new((0..5).map(|_| 0_u8).collect::>())), - last: Rc::new(RefCell::new(0_i32)), + last: 0_i32, } } } @@ -41,19 +46,19 @@ impl ByteRepr for S { 32 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.keep.borrow()).to_bytes(&mut buf[0..4]); - (*self.a.borrow()).to_bytes(&mut buf[4..8]); - (*self.b.borrow()).to_bytes(&mut buf[8..16]); + self.keep.to_bytes(&mut buf[0..4]); + self.a.to_bytes(&mut buf[4..8]); + self.b.to_bytes(&mut buf[8..16]); (*self.c.borrow()).to_bytes(&mut buf[16..21]); - (*self.last.borrow()).to_bytes(&mut buf[24..28]); + self.last.to_bytes(&mut buf[24..28]); } fn from_bytes(buf: &[u8]) -> Self { Self { - keep: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - a: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), - b: Rc::new(RefCell::new(::from_bytes(&buf[8..16]))), + keep: ::from_bytes(&buf[0..4]), + a: ::from_bytes(&buf[4..8]), + b: ::from_bytes(&buf[8..16]), c: Rc::new(RefCell::new(>::from_bytes(&buf[16..21]))), - last: Rc::new(RefCell::new(::from_bytes(&buf[24..28]))), + last: ::from_bytes(&buf[24..28]), } } } @@ -70,57 +75,54 @@ fn main_0() -> i32 { (*p.borrow()).to_any().memset((255) as u8, 32usize as usize); (*p.borrow()).to_any() }; - (*(*(*p.borrow()).upgrade().deref()).keep.borrow_mut()) = 7; + field!((*p.borrow()), keep).write(7); { - (((*(*p.borrow()).upgrade().deref()).a.as_pointer()) as Ptr) + ((field_ptr!((*p.borrow()), a)) as Ptr) .to_any() .memset( (0) as u8, (32usize as usize).wrapping_sub((4_usize as usize)) as usize, ); - (((*(*p.borrow()).upgrade().deref()).a.as_pointer()) as Ptr).to_any() + ((field_ptr!((*p.borrow()), a)) as Ptr).to_any() }; - assert!(((((*(*(*p.borrow()).upgrade().deref()).keep.borrow()) == 7) as i32) != 0)); + assert!(((((*p.borrow()).with(|__s| __s.keep) == 7) as i32) != 0)); assert!( - (((((((((((((*(*(*p.borrow()).upgrade().deref()).a.borrow()) == 0) as i32) != 0) - && ((((*(*(*p.borrow()).upgrade().deref()).b.borrow()) == 0_i64) as i32) != 0)) - as i32) + (((((((((((((*p.borrow()).with(|__s| __s.a) == 0) as i32) != 0) + && ((((*p.borrow()).with(|__s| __s.b) == 0_i64) as i32) != 0)) as i32) != 0) - && (((((*(*(*p.borrow()).upgrade().deref()).c.borrow())[(4) as usize] as i32) == 0) + && (((((*(*p.borrow()).with(|__s| __s.c.clone()).borrow())[(4) as usize] as i32) == 0) as i32) != 0)) as i32) != 0) - && ((((*(*(*p.borrow()).upgrade().deref()).last.borrow()) == 0) as i32) != 0)) - as i32) + && ((((*p.borrow()).with(|__s| __s.last) == 0) as i32) != 0)) as i32) != 0) ); - (*(*(*p.borrow()).upgrade().deref()).a.borrow_mut()) = 1; - (*(*(*p.borrow()).upgrade().deref()).b.borrow_mut()) = 2_i64; - (*(*(*p.borrow()).upgrade().deref()).c.borrow_mut())[(0) as usize] = (('x' as i32) as u8); - (*(*(*p.borrow()).upgrade().deref()).last.borrow_mut()) = 3; + field!((*p.borrow()), a).write(1); + field!((*p.borrow()), b).write(2_i64); + (*(*p.borrow()).with(|__s| __s.c.clone()).borrow_mut())[(0) as usize] = (('x' as i32) as u8); + field!((*p.borrow()), last).write(3); { - (((*(*p.borrow()).upgrade().deref()).b.as_pointer()) as Ptr) + ((field_ptr!((*p.borrow()), b)) as Ptr) .to_any() .memset( (0) as u8, (24_usize as usize).wrapping_sub((8_usize as usize)) as usize, ); - (((*(*p.borrow()).upgrade().deref()).b.as_pointer()) as Ptr).to_any() + ((field_ptr!((*p.borrow()), b)) as Ptr).to_any() }; assert!( - (((((((*(*(*p.borrow()).upgrade().deref()).keep.borrow()) == 7) as i32) != 0) - && ((((*(*(*p.borrow()).upgrade().deref()).a.borrow()) == 1) as i32) != 0)) - as i32) + (((((((*p.borrow()).with(|__s| __s.keep) == 7) as i32) != 0) + && ((((*p.borrow()).with(|__s| __s.a) == 1) as i32) != 0)) as i32) != 0) ); assert!( - (((((((*(*(*p.borrow()).upgrade().deref()).b.borrow()) == 0_i64) as i32) != 0) - && (((((*(*(*p.borrow()).upgrade().deref()).c.borrow())[(0) as usize] as i32) == 0) + (((((((*p.borrow()).with(|__s| __s.b) == 0_i64) as i32) != 0) + && (((((*(*p.borrow()).with(|__s| __s.c.clone()).borrow())[(0) as usize] as i32) == 0) as i32) != 0)) as i32) != 0) ); - assert!(((((*(*(*p.borrow()).upgrade().deref()).last.borrow()) == 3) as i32) != 0)); + assert!(((((*p.borrow()).with(|__s| __s.last) == 3) as i32) != 0)); libcc2rs::free_refcount((*p.borrow()).to_any()); return 0; } diff --git a/tests/unit/out/refcount/move_assign.rs b/tests/unit/out/refcount/move_assign.rs index f5cb8b3b7b..4c1db0f610 100644 --- a/tests/unit/out/refcount/move_assign.rs +++ b/tests/unit/out/refcount/move_assign.rs @@ -27,25 +27,12 @@ impl MoveOnly { Rc::try_unwrap(__this).ok().unwrap().into_inner() } } -<<<<<<< HEAD -impl ByteRepr for MoveOnly {} -#[derive()] -======= impl ByteRepr for MoveOnly { fn byte_size() -> usize { 4 } - fn to_bytes(&self, buf: &mut [u8]) { - self.v.to_bytes(&mut buf[0..4]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - v: ::from_bytes(&buf[0..4]), - } - } } #[derive(Record)] ->>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub struct ConstMoveAssign { #[offset(0)] pub mark: i32, @@ -62,23 +49,11 @@ impl Default for ConstMoveAssign { { ConstMoveAssign::new() } } } -<<<<<<< HEAD -impl ByteRepr for ConstMoveAssign {} -======= impl ByteRepr for ConstMoveAssign { fn byte_size() -> usize { 4 } - fn to_bytes(&self, buf: &mut [u8]) { - self.mark.to_bytes(&mut buf[0..4]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - mark: ::from_bytes(&buf[0..4]), - } - } } ->>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub fn make_0(v: i32) -> MoveOnly { let v: Value = Rc::new(RefCell::new(v)); let m: Value = Rc::new(RefCell::new(MoveOnly::new({ (*v.borrow()) }))); @@ -124,7 +99,10 @@ fn main_0() -> i32 { }); assert!(({ (*a.borrow()).v } == 6)); let vec_: Value> = Rc::new(RefCell::new(Vec::new())); - (*vec_.borrow_mut()).push(MoveOnly::new({ 7 })); + { + let __a1 = MoveOnly::new({ 7 }); + (*vec_.borrow_mut()).push(__a1) + }; let d: Value = Rc::new(RefCell::new(MoveOnly::new({ 8 }))); ({ MoveOnlyImpl::move_assign( diff --git a/tests/unit/out/refcount/move_ctor.rs b/tests/unit/out/refcount/move_ctor.rs index e28da0b1ae..defc1a5c4d 100644 --- a/tests/unit/out/refcount/move_ctor.rs +++ b/tests/unit/out/refcount/move_ctor.rs @@ -27,25 +27,12 @@ impl MoveOnly { Rc::try_unwrap(__this).ok().unwrap().into_inner() } } -<<<<<<< HEAD -impl ByteRepr for MoveOnly {} -#[derive()] -======= impl ByteRepr for MoveOnly { fn byte_size() -> usize { 4 } - fn to_bytes(&self, buf: &mut [u8]) { - self.v.to_bytes(&mut buf[0..4]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - v: ::from_bytes(&buf[0..4]), - } - } } #[derive(Record)] ->>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub struct ConstMove { #[offset(0)] pub mark: i32, @@ -76,25 +63,12 @@ impl Default for ConstMove { { ConstMove::new() } } } -<<<<<<< HEAD -impl ByteRepr for ConstMove {} -#[derive(VaArg, FnPtrArg, Default)] -======= impl ByteRepr for ConstMove { fn byte_size() -> usize { 4 } - fn to_bytes(&self, buf: &mut [u8]) { - self.mark.to_bytes(&mut buf[0..4]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - mark: ::from_bytes(&buf[0..4]), - } - } } #[derive(Record, VaArg, FnPtrArg, Default)] ->>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub struct ThrowingMove { #[offset(0)] pub v: i32, @@ -144,29 +118,12 @@ impl Clone for ThrowingMove { ThrowingMove::copy_from(__src.as_pointer()) } } -<<<<<<< HEAD -impl ByteRepr for ThrowingMove {} -#[derive(VaArg, FnPtrArg, Default)] -======= impl ByteRepr for ThrowingMove { fn byte_size() -> usize { 12 } - fn to_bytes(&self, buf: &mut [u8]) { - self.v.to_bytes(&mut buf[0..4]); - self.copies.to_bytes(&mut buf[4..8]); - self.moves.to_bytes(&mut buf[8..12]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - v: ::from_bytes(&buf[0..4]), - copies: ::from_bytes(&buf[4..8]), - moves: ::from_bytes(&buf[8..12]), - } - } } #[derive(Record, VaArg, FnPtrArg, Default)] ->>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub struct NoexceptMove { #[offset(0)] pub v: i32, @@ -216,27 +173,11 @@ impl Clone for NoexceptMove { NoexceptMove::copy_from(__src.as_pointer()) } } -<<<<<<< HEAD -impl ByteRepr for NoexceptMove {} -======= impl ByteRepr for NoexceptMove { fn byte_size() -> usize { 12 } - fn to_bytes(&self, buf: &mut [u8]) { - self.v.to_bytes(&mut buf[0..4]); - self.copies.to_bytes(&mut buf[4..8]); - self.moves.to_bytes(&mut buf[8..12]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - v: ::from_bytes(&buf[0..4]), - copies: ::from_bytes(&buf[4..8]), - moves: ::from_bytes(&buf[8..12]), - } - } } ->>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub fn by_value_0(m: MoveOnly) -> i32 { let m: Value = Rc::new(RefCell::new(m)); return { (*m.borrow()).v }; @@ -267,9 +208,15 @@ fn main_0() -> i32 { assert!((({ by_value_0(MoveOnly::move_from({ e.as_pointer() },),) }) == 5)); assert!(({ (*e.borrow()).v } == 0)); let vec_: Value> = Rc::new(RefCell::new(Vec::new())); - (*vec_.borrow_mut()).push(MoveOnly::new({ 7 })); + { + let __a1 = MoveOnly::new({ 7 }); + (*vec_.borrow_mut()).push(__a1) + }; let f: Value = Rc::new(RefCell::new(MoveOnly::new({ 8 }))); - (*vec_.borrow_mut()).push(MoveOnly::move_from({ f.as_pointer() })); + { + let __a1 = MoveOnly::move_from({ f.as_pointer() }); + (*vec_.borrow_mut()).push(__a1) + }; assert!( ({ (*(vec_.as_pointer() as Ptr) diff --git a/tests/unit/out/refcount/move_this.rs b/tests/unit/out/refcount/move_this.rs index a6fb8569c0..5bb0b22a05 100644 --- a/tests/unit/out/refcount/move_this.rs +++ b/tests/unit/out/refcount/move_this.rs @@ -40,23 +40,11 @@ impl Clone for Chain { Chain::copy_from(__src.as_pointer()) } } -<<<<<<< HEAD -impl ByteRepr for Chain {} -======= impl ByteRepr for Chain { fn byte_size() -> usize { 4 } - fn to_bytes(&self, buf: &mut [u8]) { - self.v.to_bytes(&mut buf[0..4]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - v: ::from_bytes(&buf[0..4]), - } - } } ->>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub fn consume_0(c: Chain) -> i32 { let c: Value = Rc::new(RefCell::new(c)); return { (*c.borrow()).v }; diff --git a/tests/unit/out/refcount/push_emplace_back.rs b/tests/unit/out/refcount/push_emplace_back.rs index cb2f3fd52d..fe49de4ea3 100644 --- a/tests/unit/out/refcount/push_emplace_back.rs +++ b/tests/unit/out/refcount/push_emplace_back.rs @@ -61,27 +61,11 @@ impl Clone for JPEGData { } } } -<<<<<<< HEAD -impl ByteRepr for JPEGData {} -======= impl ByteRepr for JPEGData { fn byte_size() -> usize { 48 } - fn to_bytes(&self, buf: &mut [u8]) { - (*self.com_data.borrow()).to_bytes(&mut buf[0..24]); - (*self.app_data.borrow()).to_bytes(&mut buf[24..48]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - com_data: Rc::new(RefCell::new(>>>::from_bytes(&buf[0..24]))), - app_data: Rc::new(RefCell::new(>>>::from_bytes( - &buf[24..48], - ))), - } - } } ->>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub fn push_param_0(dest: Ptr>>>) { let dest: Value>>>> = Rc::new(RefCell::new(dest)); ((*dest.borrow()).clone() as Ptr>>>) @@ -122,11 +106,12 @@ pub fn shrink_through_ptr_2(comps: Ptr>) { } pub fn nested_push_move_3(bw: Ptr) { let bw: Value> = Rc::new(RefCell::new(bw)); - (*bw.borrow()) - .with(|__s| __s.output.clone()) - .with_mut(|__v: &mut Vec| { - __v.push(((*(*bw.borrow()).upgrade().deref()).chunk).clone()) - }); + { + let __a1 = ((*(*bw.borrow()).upgrade().deref()).chunk).clone(); + (*bw.borrow()) + .with(|__s| __s.output.clone()) + .with_mut(|__v: &mut Vec| __v.push(__a1)) + }; } pub fn emplace_local_from_field_4(jpg: Ptr, cond: bool) { let jpg: Value> = Rc::new(RefCell::new(jpg)); diff --git a/tests/unit/out/refcount/reinterpret_cast_vec_write.rs b/tests/unit/out/refcount/reinterpret_cast_vec_write.rs index 7165f04bc1..29ccee9ba0 100644 --- a/tests/unit/out/refcount/reinterpret_cast_vec_write.rs +++ b/tests/unit/out/refcount/reinterpret_cast_vec_write.rs @@ -12,8 +12,14 @@ pub fn main() { } fn main_0() -> i32 { let vec_: Value> = Rc::new(RefCell::new(Vec::new())); - (*vec_.borrow_mut()).push(67305985_u32); - (*vec_.borrow_mut()).push(134678021_u32); + { + let __a1 = 67305985_u32; + (*vec_.borrow_mut()).push(__a1) + }; + { + let __a1 = 134678021_u32; + (*vec_.borrow_mut()).push(__a1) + }; let bytes: Value> = Rc::new(RefCell::new( (vec_.as_pointer() as Ptr).reinterpret_cast::(), )); diff --git a/tests/unit/out/refcount/rule_of_five.rs b/tests/unit/out/refcount/rule_of_five.rs index 8ea4e4edc6..516da97933 100644 --- a/tests/unit/out/refcount/rule_of_five.rs +++ b/tests/unit/out/refcount/rule_of_five.rs @@ -95,25 +95,11 @@ impl Default for Buffer { } } } -<<<<<<< HEAD -impl ByteRepr for Buffer {} -======= impl ByteRepr for Buffer { fn byte_size() -> usize { 20 } - fn to_bytes(&self, buf: &mut [u8]) { - (*self.data.borrow()).to_bytes(&mut buf[0..16]); - self.size.to_bytes(&mut buf[16..20]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - data: Rc::new(RefCell::new(>::from_bytes(&buf[0..16]))), - size: ::from_bytes(&buf[16..20]), - } - } } ->>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub fn make_3(size: i32) -> Buffer { let size: Value = Rc::new(RefCell::new(size)); let b: Value = Rc::new(RefCell::new(Buffer::new({ (*size.borrow()) }))); diff --git a/tests/unit/out/refcount/rule_of_three.rs b/tests/unit/out/refcount/rule_of_three.rs index 2509659540..eb1c063467 100644 --- a/tests/unit/out/refcount/rule_of_three.rs +++ b/tests/unit/out/refcount/rule_of_three.rs @@ -74,25 +74,11 @@ impl Default for Buffer { } } } -<<<<<<< HEAD -impl ByteRepr for Buffer {} -======= impl ByteRepr for Buffer { fn byte_size() -> usize { 20 } - fn to_bytes(&self, buf: &mut [u8]) { - (*self.data.borrow()).to_bytes(&mut buf[0..16]); - self.size.to_bytes(&mut buf[16..20]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - data: Rc::new(RefCell::new(>::from_bytes(&buf[0..16]))), - size: ::from_bytes(&buf[16..20]), - } - } } ->>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub fn sum_2(b: Ptr) -> i32 { let s: Value = Rc::new(RefCell::new(0)); let i: Value = Rc::new(RefCell::new(0)); diff --git a/tests/unit/out/refcount/sort.rs b/tests/unit/out/refcount/sort.rs index eb5bce4892..648ae1b6a5 100644 --- a/tests/unit/out/refcount/sort.rs +++ b/tests/unit/out/refcount/sort.rs @@ -12,16 +12,46 @@ pub fn main() { } fn main_0() -> i32 { let v: Value> = Rc::new(RefCell::new(Vec::new())); - (*v.borrow_mut()).push(10); - (*v.borrow_mut()).push(1); - (*v.borrow_mut()).push(9); - (*v.borrow_mut()).push(2); - (*v.borrow_mut()).push(8); - (*v.borrow_mut()).push(3); - (*v.borrow_mut()).push(7); - (*v.borrow_mut()).push(4); - (*v.borrow_mut()).push(5); - (*v.borrow_mut()).push(6); + { + let __a1 = 10; + (*v.borrow_mut()).push(__a1) + }; + { + let __a1 = 1; + (*v.borrow_mut()).push(__a1) + }; + { + let __a1 = 9; + (*v.borrow_mut()).push(__a1) + }; + { + let __a1 = 2; + (*v.borrow_mut()).push(__a1) + }; + { + let __a1 = 8; + (*v.borrow_mut()).push(__a1) + }; + { + let __a1 = 3; + (*v.borrow_mut()).push(__a1) + }; + { + let __a1 = 7; + (*v.borrow_mut()).push(__a1) + }; + { + let __a1 = 4; + (*v.borrow_mut()).push(__a1) + }; + { + let __a1 = 5; + (*v.borrow_mut()).push(__a1) + }; + { + let __a1 = 6; + (*v.borrow_mut()).push(__a1) + }; (v.as_pointer() as Ptr).sort((v.as_pointer() as Ptr).to_end().get_offset()); let i: Value = Rc::new(RefCell::new(0_u32)); 'loop_: while (((*i.borrow()) as usize) < ((*v.borrow()).len()).wrapping_sub(1_usize)) { diff --git a/tests/unit/out/refcount/struct_2d_array_field.rs b/tests/unit/out/refcount/struct_2d_array_field.rs index 486add8b23..06984de2df 100644 --- a/tests/unit/out/refcount/struct_2d_array_field.rs +++ b/tests/unit/out/refcount/struct_2d_array_field.rs @@ -6,16 +6,18 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg)] +#[derive(Record, VaArg, FnPtrArg)] pub struct table { + #[offset(0)] pub rows: Value>]>>, - pub count: Value, + #[offset(32)] + pub count: usize, } impl Clone for table { fn clone(&self) -> Self { Self { rows: Rc::new(RefCell::new((*self.rows.borrow()).clone())), - count: Rc::new(RefCell::new((*self.count.borrow()).clone())), + count: self.count.clone(), } } } @@ -27,7 +29,7 @@ impl Default for table { .map(|_| Rc::new(RefCell::new((0..10).map(|_| 0_u8).collect::>()))) .collect::>]>>(), )), - count: Rc::new(RefCell::new(0_usize)), + count: 0_usize, } } } @@ -37,14 +39,14 @@ impl ByteRepr for table { } fn to_bytes(&self, buf: &mut [u8]) { (*self.rows.borrow()).to_bytes(&mut buf[0..30]); - (*self.count.borrow()).to_bytes(&mut buf[32..40]); + self.count.to_bytes(&mut buf[32..40]); } fn from_bytes(buf: &[u8]) -> Self { Self { rows: Rc::new(RefCell::new(>]>>::from_bytes( &buf[0..30], ))), - count: Rc::new(RefCell::new(::from_bytes(&buf[32..40]))), + count: ::from_bytes(&buf[32..40]), } } } @@ -55,7 +57,7 @@ thread_local!( Rc::new(RefCell::new(Box::new([0; 10]))), Rc::new(RefCell::new(Box::new([0; 10]))), ]))), - count: Rc::new(RefCell::new(1_usize)), + count: 1_usize, })); ); thread_local!( @@ -65,7 +67,7 @@ thread_local!( Rc::new(RefCell::new(Box::from(*b"beta\0\0\0\0\0\0"))), Rc::new(RefCell::new(Box::new([0; 10]))), ]))), - count: Rc::new(RefCell::new(2_usize)), + count: 2_usize, })); ); pub fn main() { @@ -73,10 +75,10 @@ pub fn main() { std::process::exit(main_0()); } fn main_0() -> i32 { - assert!(((((*(*T1_0.with(Value::clone).borrow()).count.borrow()) == 1_usize) as i32) != 0)); + assert!(((({ (*T1_0.with(Value::clone).borrow()).count } == 1_usize) as i32) != 0)); assert!( ((({ - let mut __it1 = ((((*T1_0.with(Value::clone).borrow()).rows.as_pointer() + let mut __it1 = ((({ (*T1_0.with(Value::clone).borrow()).rows.clone() }.as_pointer() as Ptr>>) .offset(0) .read() @@ -97,15 +99,15 @@ fn main_0() -> i32 { != 0) ); assert!( - (((((*(*T1_0.with(Value::clone).borrow()).rows.borrow())[(1) as usize].borrow() + (((((*{ (*T1_0.with(Value::clone).borrow()).rows.clone() }.borrow())[(1) as usize].borrow() [(0) as usize] as i32) == ('\0' as i32)) as i32) != 0) ); - assert!(((((*(*T2_1.with(Value::clone).borrow()).count.borrow()) == 2_usize) as i32) != 0)); + assert!(((({ (*T2_1.with(Value::clone).borrow()).count } == 2_usize) as i32) != 0)); assert!( ((({ - let mut __it1 = ((((*T2_1.with(Value::clone).borrow()).rows.as_pointer() + let mut __it1 = ((({ (*T2_1.with(Value::clone).borrow()).rows.clone() }.as_pointer() as Ptr>>) .offset(0) .read() @@ -127,7 +129,7 @@ fn main_0() -> i32 { ); assert!( ((({ - let mut __it1 = ((((*T2_1.with(Value::clone).borrow()).rows.as_pointer() + let mut __it1 = ((({ (*T2_1.with(Value::clone).borrow()).rows.clone() }.as_pointer() as Ptr>>) .offset(1) .read() @@ -148,7 +150,7 @@ fn main_0() -> i32 { != 0) ); assert!( - (((((*(*T2_1.with(Value::clone).borrow()).rows.borrow())[(2) as usize].borrow() + (((((*{ (*T2_1.with(Value::clone).borrow()).rows.clone() }.borrow())[(2) as usize].borrow() [(0) as usize] as i32) == ('\0' as i32)) as i32) != 0) @@ -159,11 +161,12 @@ fn main_0() -> i32 { Rc::new(RefCell::new(Box::from(*b"two\0\0\0\0\0\0\0"))), Rc::new(RefCell::new(Box::from(*b"three\0\0\0\0\0"))), ]))), - count: Rc::new(RefCell::new(3_usize)), + count: 3_usize, })); assert!( ((({ - let mut __it1 = ((((*local.borrow()).rows.as_pointer() as Ptr>>) + let mut __it1 = ((({ (*local.borrow()).rows.clone() }.as_pointer() + as Ptr>>) .offset(2) .read() .as_pointer()) as Ptr) @@ -182,11 +185,12 @@ fn main_0() -> i32 { } == 0) as i32) != 0) ); - (*(*local.borrow()).rows.borrow())[(1) as usize].borrow_mut()[(0) as usize] = + (*{ (*local.borrow()).rows.clone() }.borrow())[(1) as usize].borrow_mut()[(0) as usize] = (('T' as i32) as u8); assert!( ((({ - let mut __it1 = ((((*local.borrow()).rows.as_pointer() as Ptr>>) + let mut __it1 = ((({ (*local.borrow()).rows.clone() }.as_pointer() + as Ptr>>) .offset(1) .read() .as_pointer()) as Ptr) @@ -207,7 +211,8 @@ fn main_0() -> i32 { ); assert!( ((({ - let mut __it1 = ((((*local.borrow()).rows.as_pointer() as Ptr>>) + let mut __it1 = ((({ (*local.borrow()).rows.clone() }.as_pointer() + as Ptr>>) .offset(0) .read() .as_pointer()) as Ptr) @@ -227,13 +232,13 @@ fn main_0() -> i32 { != 0) ); let p: Value> = Rc::new(RefCell::new( - ((((*local.borrow()).rows.as_pointer() as Ptr>>) + ((({ (*local.borrow()).rows.clone() }.as_pointer() as Ptr>>) .offset(2) .read() .as_pointer()) as Ptr), )); assert!(((((((*p.borrow()).offset((0) as isize).read()) as i32) == ('t' as i32)) as i32) != 0)); - assert!(((((*(*local.borrow()).count.borrow()) == 3_usize) as i32) != 0)); + assert!(((({ (*local.borrow()).count } == 3_usize) as i32) != 0)); return 0; } pub fn __cpp2rust_init_globals() { diff --git a/tests/unit/out/refcount/struct_member_array_adjacency.rs b/tests/unit/out/refcount/struct_member_array_adjacency.rs index 0a35031d8b..84c26fdecf 100644 --- a/tests/unit/out/refcount/struct_member_array_adjacency.rs +++ b/tests/unit/out/refcount/struct_member_array_adjacency.rs @@ -6,9 +6,11 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg)] +#[derive(Record, VaArg, FnPtrArg)] pub struct pair { + #[offset(0)] pub a: Value>, + #[offset(16)] pub b: Value>, } impl Clone for pair { @@ -49,8 +51,8 @@ pub fn main() { fn main_0() -> i32 { let s: Value = >::default(); assert!( - (((((*s.borrow()).a.as_pointer() as Ptr::).offset((4) as isize) - == ((*s.borrow()).b.as_pointer() as Ptr::)) as i32) + (((({ (*s.borrow()).a.clone() }.as_pointer() as Ptr::).offset((4) as isize) + == ({ (*s.borrow()).b.clone() }.as_pointer() as Ptr::)) as i32) != 0) ); return 0; diff --git a/tests/unit/out/refcount/typedef-anon-struct.rs b/tests/unit/out/refcount/typedef-anon-struct.rs index e36d2ffecd..7699ba8aeb 100644 --- a/tests/unit/out/refcount/typedef-anon-struct.rs +++ b/tests/unit/out/refcount/typedef-anon-struct.rs @@ -40,23 +40,11 @@ impl Clone for Outer { } } } -<<<<<<< HEAD -impl ByteRepr for Outer {} -======= impl ByteRepr for Outer { fn byte_size() -> usize { 24 } - fn to_bytes(&self, buf: &mut [u8]) { - (*self.runs.borrow()).to_bytes(&mut buf[0..24]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - runs: Rc::new(RefCell::new(>::from_bytes(&buf[0..24]))), - } - } } ->>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub fn main() { __cpp2rust_init_globals(); std::process::exit(main_0()); diff --git a/tests/unit/out/refcount/unique_ptr.rs b/tests/unit/out/refcount/unique_ptr.rs index e84d7ff097..2d25306e23 100644 --- a/tests/unit/out/refcount/unique_ptr.rs +++ b/tests/unit/out/refcount/unique_ptr.rs @@ -20,25 +20,12 @@ impl SafePointer { Rc::try_unwrap(__this).ok().unwrap().into_inner() } } -<<<<<<< HEAD -impl ByteRepr for SafePointer {} -#[derive(VaArg, FnPtrArg, Default)] -======= impl ByteRepr for SafePointer { fn byte_size() -> usize { 8 } - fn to_bytes(&self, buf: &mut [u8]) { - self.ptr.to_bytes(&mut buf[0..8]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - ptr: >>::from_bytes(&buf[0..8]), - } - } } #[derive(Clone, Record, VaArg, FnPtrArg, Default)] ->>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub struct Pair { #[offset(0)] pub x: i32, diff --git a/tests/unit/out/refcount/unique_ptr_const_deref.rs b/tests/unit/out/refcount/unique_ptr_const_deref.rs index 5fa5e3b831..ddf7a5262c 100644 --- a/tests/unit/out/refcount/unique_ptr_const_deref.rs +++ b/tests/unit/out/refcount/unique_ptr_const_deref.rs @@ -20,23 +20,11 @@ impl Holder { Rc::try_unwrap(__this).ok().unwrap().into_inner() } } -<<<<<<< HEAD -impl ByteRepr for Holder {} -======= impl ByteRepr for Holder { fn byte_size() -> usize { 8 } - fn to_bytes(&self, buf: &mut [u8]) { - self.val.to_bytes(&mut buf[0..8]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - val: >>::from_bytes(&buf[0..8]), - } - } } ->>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub fn read_val_0(h: Ptr) -> i32 { let h: Value> = Rc::new(RefCell::new(h)); return (*(*h.borrow()) diff --git a/tests/unit/out/refcount/unique_ptr_field_reset.rs b/tests/unit/out/refcount/unique_ptr_field_reset.rs index 2354c8bcd6..48232cc09d 100644 --- a/tests/unit/out/refcount/unique_ptr_field_reset.rs +++ b/tests/unit/out/refcount/unique_ptr_field_reset.rs @@ -45,16 +45,6 @@ impl ByteRepr for Holder { fn byte_size() -> usize { 16 } - fn to_bytes(&self, buf: &mut [u8]) { - self.data.to_bytes(&mut buf[0..8]); - self.n.to_bytes(&mut buf[8..12]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - data: >>::from_bytes(&buf[0..8]), - n: ::from_bytes(&buf[8..12]), - } - } } pub fn main() { __cpp2rust_init_globals(); diff --git a/tests/unit/out/refcount/unique_ptr_nested.rs b/tests/unit/out/refcount/unique_ptr_nested.rs index 02b4196842..04f3f011ee 100644 --- a/tests/unit/out/refcount/unique_ptr_nested.rs +++ b/tests/unit/out/refcount/unique_ptr_nested.rs @@ -42,23 +42,11 @@ impl Outer { Rc::try_unwrap(__this).ok().unwrap().into_inner() } } -<<<<<<< HEAD -impl ByteRepr for Outer {} -======= impl ByteRepr for Outer { fn byte_size() -> usize { 8 } - fn to_bytes(&self, buf: &mut [u8]) { - self.inner.to_bytes(&mut buf[0..8]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - inner: >>::from_bytes(&buf[0..8]), - } - } } ->>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub fn main() { __cpp2rust_init_globals(); std::process::exit(main_0()); diff --git a/tests/unit/out/refcount/vector.rs b/tests/unit/out/refcount/vector.rs index add0446acf..357fe4e883 100644 --- a/tests/unit/out/refcount/vector.rs +++ b/tests/unit/out/refcount/vector.rs @@ -17,7 +17,10 @@ fn main_0() -> i32 { let v1: Value> = Rc::new(RefCell::new(Vec::new())); assert!(((*v1.borrow()).len() == 0_usize)); assert!((*v1.borrow()).is_empty()); - (*v1.borrow_mut()).push(1); + { + let __a1 = 1; + (*v1.borrow_mut()).push(__a1) + }; assert!(!((*v1.borrow()).is_empty())); (*v1.borrow_mut()).pop(); assert!((*v1.borrow()).is_empty()); @@ -34,9 +37,18 @@ fn main_0() -> i32 { assert!((((v1.as_pointer() as Ptr).offset(99_usize).read()) == 50)); let v2: Value> = Rc::new(RefCell::new(Vec::new())); assert!(((*v2.borrow()).len() == 0_usize)); - (*v2.borrow_mut()).push(1); - (*v2.borrow_mut()).push(2); - (*v2.borrow_mut()).push(3); + { + let __a1 = 1; + (*v2.borrow_mut()).push(__a1) + }; + { + let __a1 = 2; + (*v2.borrow_mut()).push(__a1) + }; + { + let __a1 = 3; + (*v2.borrow_mut()).push(__a1) + }; assert!(((*v2.borrow()).len() == 3_usize)); { let idx = (v2.as_pointer() as Ptr).get_offset(); diff --git a/tests/unit/out/refcount/vector2.rs b/tests/unit/out/refcount/vector2.rs index 0e74e7835f..180169c6a4 100644 --- a/tests/unit/out/refcount/vector2.rs +++ b/tests/unit/out/refcount/vector2.rs @@ -8,13 +8,25 @@ use std::os::fd::AsFd; use std::rc::{Rc, Weak}; pub fn fn_0(v: Ptr>, v3: Vec) { let v3: Value> = Rc::new(RefCell::new(v3)); - v.with_mut(|__v: &mut Vec| __v.push(20)); + { + let __a1 = 20; + v.with_mut(|__v: &mut Vec| __v.push(__a1)) + }; let x: Value = Rc::new(RefCell::new(0_i32)); let v2: Value> = Rc::new(RefCell::new(Vec::new())); let v4: Value>> = Rc::new(RefCell::new((v3.as_pointer()))); - (*v2.borrow_mut()).push(0); - (*v2.borrow_mut()).push(1); - (*v2.borrow_mut()).push(3); + { + let __a1 = 0; + (*v2.borrow_mut()).push(__a1) + }; + { + let __a1 = 1; + (*v2.borrow_mut()).push(__a1) + }; + { + let __a1 = 3; + (*v2.borrow_mut()).push(__a1) + }; (*x.borrow_mut()) = ((Ptr::>::decay(&(v)) as Ptr) .offset(2_usize) .read()); @@ -50,7 +62,10 @@ pub fn fn_0(v: Ptr>, v3: Vec) { assert!((((v2.as_pointer() as Ptr).offset(2_usize).read()) == 6)); assert!((((v3.as_pointer() as Ptr).offset(0_usize).read()) == 7)); assert!((((v3.as_pointer() as Ptr).offset(1_usize).read()) == 13)); - v.with_mut(|__v: &mut Vec| __v.push(20)); + { + let __a1 = 20; + v.with_mut(|__v: &mut Vec| __v.push(__a1)) + }; } pub fn main() { __cpp2rust_init_globals(); @@ -59,11 +74,26 @@ pub fn main() { fn main_0() -> i32 { let v: Value> = Rc::new(RefCell::new(Vec::new())); let v2: Value> = Rc::new(RefCell::new(Vec::new())); - (*v.borrow_mut()).push(4); - (*v.borrow_mut()).push(5); - (*v.borrow_mut()).push(6); - (*v2.borrow_mut()).push(8); - (*v2.borrow_mut()).push(9); + { + let __a1 = 4; + (*v.borrow_mut()).push(__a1) + }; + { + let __a1 = 5; + (*v.borrow_mut()).push(__a1) + }; + { + let __a1 = 6; + (*v.borrow_mut()).push(__a1) + }; + { + let __a1 = 8; + (*v2.borrow_mut()).push(__a1) + }; + { + let __a1 = 9; + (*v2.borrow_mut()).push(__a1) + }; ({ fn_0(v.as_pointer(), (*v2.borrow()).clone()) }); return 0; } diff --git a/tests/unit/out/refcount/vector_subscript_direct.rs b/tests/unit/out/refcount/vector_subscript_direct.rs index 954c5479cb..448c116d89 100644 --- a/tests/unit/out/refcount/vector_subscript_direct.rs +++ b/tests/unit/out/refcount/vector_subscript_direct.rs @@ -47,19 +47,12 @@ impl ByteRepr for Holder { fn byte_size() -> usize { 48 } - fn to_bytes(&self, buf: &mut [u8]) { - (*self.values.borrow()).to_bytes(&mut buf[0..24]); - (*self.points.borrow()).to_bytes(&mut buf[24..48]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - values: Rc::new(RefCell::new(>::from_bytes(&buf[0..24]))), - points: Rc::new(RefCell::new(>::from_bytes(&buf[24..48]))), - } - } } pub fn push_and_index_0(v: Ptr>) -> i32 { - v.with_mut(|__v: &mut Vec| __v.push(42)); + { + let __a1 = 42; + v.with_mut(|__v: &mut Vec| __v.push(__a1)) + }; return (((*v.upgrade().deref()).len() as i32) - 1); } pub fn sum_ref_1(v: Ptr>) -> i32 { @@ -135,7 +128,10 @@ fn main_0() -> i32 { ({ (*h.borrow()).values.clone() }.as_pointer() as Ptr) .offset(1_usize) .write(6); - (*{ (*h.borrow()).points.clone() }.borrow_mut()).push(Point { x: 1, y: 2 }); + { + let __a1 = Point { x: 1, y: 2 }; + (*{ (*h.borrow()).points.clone() }.borrow_mut()).push(__a1) + }; field!( ({ (*h.borrow()).points.clone() }.as_pointer() as Ptr).offset(0_usize), y diff --git a/tests/unit/out/refcount/void_cast.rs b/tests/unit/out/refcount/void_cast.rs index 223dc14108..864901fad4 100644 --- a/tests/unit/out/refcount/void_cast.rs +++ b/tests/unit/out/refcount/void_cast.rs @@ -22,23 +22,11 @@ impl Clone for NonTrivial { } } } -<<<<<<< HEAD -impl ByteRepr for NonTrivial {} -======= impl ByteRepr for NonTrivial { fn byte_size() -> usize { 24 } - fn to_bytes(&self, buf: &mut [u8]) { - (*self.data.borrow()).to_bytes(&mut buf[0..24]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - data: Rc::new(RefCell::new(>::from_bytes(&buf[0..24]))), - } - } } ->>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub fn unused_ref_param_1(x: Ptr) { &(*x.upgrade().deref()); } @@ -85,23 +73,11 @@ impl NonCopyable { Rc::try_unwrap(__this).ok().unwrap().into_inner() } } -<<<<<<< HEAD -impl ByteRepr for NonCopyable {} -======= impl ByteRepr for NonCopyable { fn byte_size() -> usize { 8 } - fn to_bytes(&self, buf: &mut [u8]) { - self.value.to_bytes(&mut buf[0..8]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - value: >>::from_bytes(&buf[0..8]), - } - } } ->>>>>>> 3ed38b58 (Remove Value<> boxing from struct fields) pub fn unused_noncopyable_param_5(x: Ptr) { &(*x.upgrade().deref()); } diff --git a/tests/unit/out/unsafe/array_of_noncopy_struct.rs b/tests/unit/out/unsafe/array_of_noncopy_struct.rs index 6d834341d0..6d8223ac7a 100644 --- a/tests/unit/out/unsafe/array_of_noncopy_struct.rs +++ b/tests/unit/out/unsafe/array_of_noncopy_struct.rs @@ -29,7 +29,10 @@ pub fn main() { unsafe fn main_0() -> i32 { let mut arr: [NonCopy; 3] = std::array::from_fn::<_, 3, _>(|_| ::default()); arr[(0) as usize].tag = 7; - arr[(1) as usize].data.push(42); + { + let __a1 = 42; + arr[(1) as usize].data.push(__a1) + }; assert!(((arr[(0) as usize].tag) == (7))); assert!(((arr[(1) as usize].data.len()) == (1_usize))); assert!(((arr[(1) as usize].data[(0_usize)]) == (42))); diff --git a/tests/unit/out/unsafe/auto.rs b/tests/unit/out/unsafe/auto.rs index 1f02ba4d17..86967b6575 100644 --- a/tests/unit/out/unsafe/auto.rs +++ b/tests/unit/out/unsafe/auto.rs @@ -17,8 +17,14 @@ unsafe fn main_0() -> i32 { let mut x2: i16 = 2_i16; let mut x3: u32 = 4_u32; let mut v: Vec = Vec::new(); - v.push(1); - v.push(2); + { + let __a1 = 1; + v.push(__a1) + }; + { + let __a1 = 2; + v.push(__a1) + }; let mut sum: i32 = 0; 'loop_: for elem in 0..(v.len()) { let mut elem = v[elem].clone(); diff --git a/tests/unit/out/unsafe/clone_vs_move.rs b/tests/unit/out/unsafe/clone_vs_move.rs index 92e7f9cb0b..8815fb05fb 100644 --- a/tests/unit/out/unsafe/clone_vs_move.rs +++ b/tests/unit/out/unsafe/clone_vs_move.rs @@ -211,7 +211,10 @@ unsafe fn main_0() -> i32 { 0.into(), ); let mut pair4: (Vec, i32) = pair3.clone(); - pair4.0.push(1); + { + let __a1 = 1; + pair4.0.push(__a1) + }; pair4.1 = 1; assert!(((pair4.0.len()) == (1_usize))); assert!(((pair4.1) == (1))); diff --git a/tests/unit/out/unsafe/compound_assign_ref.rs b/tests/unit/out/unsafe/compound_assign_ref.rs index 6e0615058c..10ef93fbf6 100644 --- a/tests/unit/out/unsafe/compound_assign_ref.rs +++ b/tests/unit/out/unsafe/compound_assign_ref.rs @@ -14,7 +14,10 @@ pub fn main() { } unsafe fn main_0() -> i32 { let mut v: Vec = Vec::new(); - v.push(10); + { + let __a1 = 10; + v.push(__a1) + }; (*((v).first_mut().unwrap())) += 5; assert!(((*((v).first_mut().unwrap())) == (15))); return 0; diff --git a/tests/unit/out/unsafe/container_field_addr.rs b/tests/unit/out/unsafe/container_field_addr.rs index b29cbea784..ab9b97123c 100644 --- a/tests/unit/out/unsafe/container_field_addr.rs +++ b/tests/unit/out/unsafe/container_field_addr.rs @@ -49,7 +49,10 @@ pub unsafe fn run_3(mut h: *mut S) { put_2(_m, _k, 2) }); let mut pv: *mut Vec = (&mut (*h).v as *mut Vec); - (*pv).push(((*(pv).cast_const()).len() as i32)); + { + let __a1 = ((*(pv).cast_const()).len() as i32); + (*pv).push(__a1) + }; assert!( ((((*h).v.len()) == (2_usize)) && (((&mut (*h)).v[(0_usize)]) == (7))) && (((&mut (*h)).v[(1_usize)]) == (1)) diff --git a/tests/unit/out/unsafe/copy_move_defaulted.rs b/tests/unit/out/unsafe/copy_move_defaulted.rs index 344589b01c..7110d5b4c1 100644 --- a/tests/unit/out/unsafe/copy_move_defaulted.rs +++ b/tests/unit/out/unsafe/copy_move_defaulted.rs @@ -310,7 +310,10 @@ unsafe fn main_0() -> i32 { let a0_clone = b.clone(); vec_.push(a0_clone) }; - vec_.push(Explicit::new({ 9 })); + { + let __a1 = Explicit::new({ 9 }); + vec_.push(__a1) + }; assert!(((vec_[(0_usize)].v) == (1)) && ((vec_[(1_usize)].v) == (9))); let mut m: DefaultCopyUserMove = DefaultCopyUserMove::new({ 7 }); let mut m1: DefaultCopyUserMove = m; @@ -347,7 +350,10 @@ unsafe fn main_0() -> i32 { && (q.rows.is_empty()) ); let mut bufs: Vec = Vec::new(); - bufs.push(Buffer::move_from({ &mut r })); + { + let __a1 = Buffer::move_from({ &mut r }); + bufs.push(__a1) + }; { let __init = Buffer::move_from({ &mut bufs[(0_usize)] }); bufs.push(__init) @@ -357,7 +363,10 @@ unsafe fn main_0() -> i32 { && (bufs[(0_usize)].data.is_empty()) ); let mut o1: Owner = ::default(); - o1.data.push(5); + { + let __a1 = 5; + o1.data.push(__a1) + }; o1.n = 5; o1.arr[(0) as usize] = 5; o1.arr[(1) as usize] = 6; diff --git a/tests/unit/out/unsafe/field_method_receiver.rs b/tests/unit/out/unsafe/field_method_receiver.rs index 474824cb34..b946d16dee 100644 --- a/tests/unit/out/unsafe/field_method_receiver.rs +++ b/tests/unit/out/unsafe/field_method_receiver.rs @@ -75,7 +75,10 @@ pub unsafe fn run_0(mut o: *mut S) { assert!(((unsafe { Counter::get(&(*o).c,) }) == (0))); (unsafe { S::bump(&mut (*o)) }); assert!(((unsafe { Counter::get(&(*o).c,) }) == (1))); - (*o).v.push((unsafe { Counter::get(&(*o).c) })); + { + let __a1 = (unsafe { Counter::get(&(*o).c) }); + (*o).v.push(__a1) + }; assert!((((*o).v.len()) == (1_usize)) && (((&mut (*o)).v[(0_usize)]) == (1))); assert!((((*o).tag) == (1))); } diff --git a/tests/unit/out/unsafe/field_pointers.rs b/tests/unit/out/unsafe/field_pointers.rs index 3dd8b7ccf6..29d0cb881b 100644 --- a/tests/unit/out/unsafe/field_pointers.rs +++ b/tests/unit/out/unsafe/field_pointers.rs @@ -44,7 +44,10 @@ impl Outer { return ((self.x) + (self.inner.a)); } pub unsafe fn push(&mut self, mut k: i32) { - self.v.push(((k) + (self.x))); + { + let __a1 = ((k) + (self.x)); + self.v.push(__a1) + }; } } impl Default for Outer { diff --git a/tests/unit/out/unsafe/find.rs b/tests/unit/out/unsafe/find.rs index cdc1d9e856..7c11816f6c 100644 --- a/tests/unit/out/unsafe/find.rs +++ b/tests/unit/out/unsafe/find.rs @@ -14,9 +14,18 @@ pub fn main() { } unsafe fn main_0() -> i32 { let mut v: Vec = Vec::new(); - v.push(1); - v.push(2); - v.push(3); + { + let __a1 = 1; + v.push(__a1) + }; + { + let __a1 = 2; + v.push(__a1) + }; + { + let __a1 = 3; + v.push(__a1) + }; let mut v_begin: *mut i32 = v.as_mut_ptr(); let mut v_end: *mut i32 = v.as_mut_ptr().add(v.len()); let mut it: *mut i32 = { diff --git a/tests/unit/out/unsafe/fn_ptr_stable_sort.rs b/tests/unit/out/unsafe/fn_ptr_stable_sort.rs index 516b5e9a6f..5046964164 100644 --- a/tests/unit/out/unsafe/fn_ptr_stable_sort.rs +++ b/tests/unit/out/unsafe/fn_ptr_stable_sort.rs @@ -23,9 +23,18 @@ pub fn main() { } unsafe fn main_0() -> i32 { let mut v: Vec = Vec::new(); - v.push(Item { key: 3, value: 30 }); - v.push(Item { key: 1, value: 10 }); - v.push(Item { key: 2, value: 20 }); + { + let __a1 = Item { key: 3, value: 30 }; + v.push(__a1) + }; + { + let __a1 = Item { key: 1, value: 10 }; + v.push(__a1) + }; + { + let __a1 = Item { key: 2, value: 20 }; + v.push(__a1) + }; { let len = v.as_mut_ptr().add(v.len()).offset_from(v.as_mut_ptr()) as usize; ::std::slice::from_raw_parts_mut(v.as_mut_ptr(), len).sort_by(|x, y| { diff --git a/tests/unit/out/unsafe/foreach_disjoint_field_borrow.rs b/tests/unit/out/unsafe/foreach_disjoint_field_borrow.rs index 5a88a867cf..4603e2c8f4 100644 --- a/tests/unit/out/unsafe/foreach_disjoint_field_borrow.rs +++ b/tests/unit/out/unsafe/foreach_disjoint_field_borrow.rs @@ -20,7 +20,10 @@ pub fn main() { } unsafe fn main_0() -> i32 { let mut s: S = ::default(); - s.v.push(1); + { + let __a1 = 1; + s.v.push(__a1) + }; 'loop_: for e in 0..(s.v.len()) { let mut e = s.v[e].clone(); s.a.postfix_inc(); diff --git a/tests/unit/out/unsafe/foreach_double.rs b/tests/unit/out/unsafe/foreach_double.rs index de5255db7a..8035a31b58 100644 --- a/tests/unit/out/unsafe/foreach_double.rs +++ b/tests/unit/out/unsafe/foreach_double.rs @@ -14,9 +14,18 @@ pub fn main() { } unsafe fn main_0() -> i32 { let mut v: Vec = Vec::new(); - v.push(1); - v.push(2); - v.push(3); + { + let __a1 = 1; + v.push(__a1) + }; + { + let __a1 = 2; + v.push(__a1) + }; + { + let __a1 = 3; + v.push(__a1) + }; let mut square: i32 = 0; 'loop_: for e1 in 0..(v.len()) { let mut e1 = v[e1].clone(); diff --git a/tests/unit/out/unsafe/foreach_mut.rs b/tests/unit/out/unsafe/foreach_mut.rs index 4b5f3f91c2..d3dce86498 100644 --- a/tests/unit/out/unsafe/foreach_mut.rs +++ b/tests/unit/out/unsafe/foreach_mut.rs @@ -14,9 +14,18 @@ pub fn main() { } unsafe fn main_0() -> i32 { let mut v1: Vec = Vec::new(); - v1.push(1); - v1.push(2); - v1.push(3); + { + let __a1 = 1; + v1.push(__a1) + }; + { + let __a1 = 2; + v1.push(__a1) + }; + { + let __a1 = 3; + v1.push(__a1) + }; let mut sum: i32 = 0; 'loop_: for x in 0..(v1.len()) { let mut x = v1[x].clone(); @@ -35,9 +44,18 @@ unsafe fn main_0() -> i32 { sum += (*x); } let mut v2: Vec<*mut i32> = Vec::new(); - v2.push((&mut v1[(0_usize)] as *mut i32)); - v2.push((&mut v1[(1_usize)] as *mut i32)); - v2.push((&mut v1[(2_usize)] as *mut i32)); + { + let __a1 = (&mut v1[(0_usize)] as *mut i32); + v2.push(__a1) + }; + { + let __a1 = (&mut v1[(1_usize)] as *mut i32); + v2.push(__a1) + }; + { + let __a1 = (&mut v1[(2_usize)] as *mut i32); + v2.push(__a1) + }; 'loop_: for p in 0..(v2.len()) { let mut p = v2[p].clone(); (*p) += 5; diff --git a/tests/unit/out/unsafe/implicit_autoref.rs b/tests/unit/out/unsafe/implicit_autoref.rs index e4eed174ab..dae1352f28 100644 --- a/tests/unit/out/unsafe/implicit_autoref.rs +++ b/tests/unit/out/unsafe/implicit_autoref.rs @@ -22,14 +22,26 @@ pub fn main() { } unsafe fn main_0() -> i32 { let mut v: Vec = Vec::new(); - v.push(10); - v.push(20); + { + let __a1 = 10; + v.push(__a1) + }; + { + let __a1 = 20; + v.push(__a1) + }; let mut p: *mut Vec = (&mut v as *mut Vec); let mut a: i32 = (&mut (*p))[(0_usize)]; (&mut (*p))[(1_usize)] = 30; let mut h: Holder = ::default(); - h.v.push(40); - h.v.push(50); + { + let __a1 = 40; + h.v.push(__a1) + }; + { + let __a1 = 50; + h.v.push(__a1) + }; let mut hp: *mut Holder = (&mut h as *mut Holder); let mut b: i32 = (&mut (*hp)).v[(0_usize)]; (&mut (*hp)).v[(1_usize)] = 60; diff --git a/tests/unit/out/unsafe/iterators.rs b/tests/unit/out/unsafe/iterators.rs index 8ad8cde876..c2c58b810b 100644 --- a/tests/unit/out/unsafe/iterators.rs +++ b/tests/unit/out/unsafe/iterators.rs @@ -30,8 +30,14 @@ unsafe fn main_0() -> i32 { printf(c"%c\n".as_ptr() as *const i8, (c as i32)); } let mut v: Vec<*mut i32> = Vec::new(); - v.push((Box::leak(Box::new(2)) as *mut i32)); - v.push((Box::leak(Box::new(3)) as *mut i32)); + { + let __a1 = (Box::leak(Box::new(2)) as *mut i32); + v.push(__a1) + }; + { + let __a1 = (Box::leak(Box::new(3)) as *mut i32); + v.push(__a1) + }; 'loop_: for p in 0..(v.len()) { let mut p = v[p].clone(); printf(c"%d\n".as_ptr() as *const i8, (*p)); diff --git a/tests/unit/out/unsafe/map.rs b/tests/unit/out/unsafe/map.rs index 67472d7f90..2449d2de63 100644 --- a/tests/unit/out/unsafe/map.rs +++ b/tests/unit/out/unsafe/map.rs @@ -113,17 +113,26 @@ unsafe fn main_0() -> i32 { let mut indexes: Vec = Vec::new(); let mut i: u32 = 60_u32; 'loop_: while ((i) > (30_u32)) { - indexes.push((i as i32)); + { + let __a1 = (i as i32); + indexes.push(__a1) + }; i.prefix_dec(); } let mut i: u32 = 100_u32; 'loop_: while ((i) > (60_u32)) { - indexes.push((i as i32)); + { + let __a1 = (i as i32); + indexes.push(__a1) + }; i.prefix_dec(); } let mut i: u32 = 30_u32; 'loop_: while ((i) > (0_u32)) { - indexes.push((i as i32)); + { + let __a1 = (i as i32); + indexes.push(__a1) + }; i.prefix_dec(); } let mut i: u32 = 0_u32; diff --git a/tests/unit/out/unsafe/move_assign.rs b/tests/unit/out/unsafe/move_assign.rs index c1eef84a28..904fccf492 100644 --- a/tests/unit/out/unsafe/move_assign.rs +++ b/tests/unit/out/unsafe/move_assign.rs @@ -98,7 +98,10 @@ unsafe fn main_0() -> i32 { }); assert!(((a.v) == (6))); let mut vec_: Vec = Vec::new(); - vec_.push(MoveOnly::new({ 7 })); + { + let __a1 = MoveOnly::new({ 7 }); + vec_.push(__a1) + }; let mut d: MoveOnly = MoveOnly::new({ 8 }); (unsafe { MoveOnly::move_assign(&mut vec_[(0_usize)], &mut d) }); assert!(((vec_[(0_usize)].v) == (8))); diff --git a/tests/unit/out/unsafe/move_ctor.rs b/tests/unit/out/unsafe/move_ctor.rs index c30854a73e..c561f1c121 100644 --- a/tests/unit/out/unsafe/move_ctor.rs +++ b/tests/unit/out/unsafe/move_ctor.rs @@ -158,9 +158,15 @@ unsafe fn main_0() -> i32 { assert!(((unsafe { by_value_0(MoveOnly::move_from({ &mut e },),) }) == (5))); assert!(((e.v) == (0))); let mut vec_: Vec = Vec::new(); - vec_.push(MoveOnly::new({ 7 })); + { + let __a1 = MoveOnly::new({ 7 }); + vec_.push(__a1) + }; let mut f: MoveOnly = MoveOnly::new({ 8 }); - vec_.push(MoveOnly::move_from({ &mut f })); + { + let __a1 = MoveOnly::move_from({ &mut f }); + vec_.push(__a1) + }; assert!(((vec_[(0_usize)].v) == (7)) && ((vec_[(1_usize)].v) == (8))); assert!(((f.v) == (0))); let mut m: ConstMove = ConstMove::new(); diff --git a/tests/unit/out/unsafe/push_emplace_back.rs b/tests/unit/out/unsafe/push_emplace_back.rs index e4142bd821..6b5e01719c 100644 --- a/tests/unit/out/unsafe/push_emplace_back.rs +++ b/tests/unit/out/unsafe/push_emplace_back.rs @@ -48,7 +48,10 @@ pub unsafe fn shrink_through_ptr_2(mut comps: *mut Vec) { (*comps).shrink_to_fit(); } pub unsafe fn nested_push_move_3(mut bw: *mut Writer) { - (*(*bw).output).push((*bw).chunk); + { + let __a1 = (*bw).chunk; + (*(*bw).output).push(__a1) + }; } pub unsafe fn emplace_local_from_field_4(mut jpg: *mut JPEGData, mut cond: bool) { let mut head: [u8; 3] = [1_u8, 2_u8, 3_u8]; diff --git a/tests/unit/out/unsafe/reinterpret_cast_vec_write.rs b/tests/unit/out/unsafe/reinterpret_cast_vec_write.rs index 7a5fe604be..046a9e30fc 100644 --- a/tests/unit/out/unsafe/reinterpret_cast_vec_write.rs +++ b/tests/unit/out/unsafe/reinterpret_cast_vec_write.rs @@ -14,8 +14,14 @@ pub fn main() { } unsafe fn main_0() -> i32 { let mut vec_: Vec = Vec::new(); - vec_.push(67305985_u32); - vec_.push(134678021_u32); + { + let __a1 = 67305985_u32; + vec_.push(__a1) + }; + { + let __a1 = 134678021_u32; + vec_.push(__a1) + }; let mut bytes: *mut u8 = (vec_.as_mut_ptr() as *mut u8); assert!((((*bytes.offset((0) as isize)) as i32) == (1))); assert!((((*bytes.offset((1) as isize)) as i32) == (2))); diff --git a/tests/unit/out/unsafe/sort.rs b/tests/unit/out/unsafe/sort.rs index 617896f2ed..c89ba7d9d1 100644 --- a/tests/unit/out/unsafe/sort.rs +++ b/tests/unit/out/unsafe/sort.rs @@ -14,16 +14,46 @@ pub fn main() { } unsafe fn main_0() -> i32 { let mut v: Vec = Vec::new(); - v.push(10); - v.push(1); - v.push(9); - v.push(2); - v.push(8); - v.push(3); - v.push(7); - v.push(4); - v.push(5); - v.push(6); + { + let __a1 = 10; + v.push(__a1) + }; + { + let __a1 = 1; + v.push(__a1) + }; + { + let __a1 = 9; + v.push(__a1) + }; + { + let __a1 = 2; + v.push(__a1) + }; + { + let __a1 = 8; + v.push(__a1) + }; + { + let __a1 = 3; + v.push(__a1) + }; + { + let __a1 = 7; + v.push(__a1) + }; + { + let __a1 = 4; + v.push(__a1) + }; + { + let __a1 = 5; + v.push(__a1) + }; + { + let __a1 = 6; + v.push(__a1) + }; { let len = v.as_mut_ptr().add(v.len()).offset_from(v.as_mut_ptr()) as usize; ::std::slice::from_raw_parts_mut(v.as_mut_ptr(), len).sort() diff --git a/tests/unit/out/unsafe/vector.rs b/tests/unit/out/unsafe/vector.rs index da4824ea84..b4f7f42500 100644 --- a/tests/unit/out/unsafe/vector.rs +++ b/tests/unit/out/unsafe/vector.rs @@ -17,7 +17,10 @@ unsafe fn main_0() -> i32 { let mut v1: Vec = Vec::new(); assert!(((v1.len()) == (0_usize))); assert!(v1.is_empty()); - v1.push(1); + { + let __a1 = 1; + v1.push(__a1) + }; assert!(!(v1.is_empty())); v1.pop(); assert!(v1.is_empty()); @@ -34,9 +37,18 @@ unsafe fn main_0() -> i32 { assert!(((v1[(99_usize)]) == (50))); let mut v2: Vec = Vec::new(); assert!(((v2.len()) == (0_usize))); - v2.push(1); - v2.push(2); - v2.push(3); + { + let __a1 = 1; + v2.push(__a1) + }; + { + let __a1 = 2; + v2.push(__a1) + }; + { + let __a1 = 3; + v2.push(__a1) + }; assert!(((v2.len()) == (3_usize))); { let pos = v2.as_mut_ptr().offset_from(v2.as_ptr()) as usize; diff --git a/tests/unit/out/unsafe/vector2.rs b/tests/unit/out/unsafe/vector2.rs index ecc50db3c8..0d0ffaedbd 100644 --- a/tests/unit/out/unsafe/vector2.rs +++ b/tests/unit/out/unsafe/vector2.rs @@ -7,13 +7,25 @@ use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; pub unsafe fn fn_0(v: *mut Vec, mut v3: Vec) { - (*v).push(20); + { + let __a1 = 20; + (*v).push(__a1) + }; let mut x: i32 = 0_i32; let mut v2: Vec = Vec::new(); let mut v4: *mut Vec = (&mut v3 as *mut Vec); - v2.push(0); - v2.push(1); - v2.push(3); + { + let __a1 = 0; + v2.push(__a1) + }; + { + let __a1 = 1; + v2.push(__a1) + }; + { + let __a1 = 3; + v2.push(__a1) + }; x = (&mut (*v))[(2_usize)]; v2[(0_usize)] = 1; (if true { &mut v3 } else { &mut (*v) })[(0_usize)] = 7; @@ -29,7 +41,10 @@ pub unsafe fn fn_0(v: *mut Vec, mut v3: Vec) { assert!(((v2[(2_usize)]) == (6))); assert!(((v3[(0_usize)]) == (7))); assert!(((v3[(1_usize)]) == (13))); - (*v).push(20); + { + let __a1 = 20; + (*v).push(__a1) + }; } pub fn main() { unsafe { @@ -40,11 +55,26 @@ pub fn main() { unsafe fn main_0() -> i32 { let mut v: Vec = Vec::new(); let mut v2: Vec = Vec::new(); - v.push(4); - v.push(5); - v.push(6); - v2.push(8); - v2.push(9); + { + let __a1 = 4; + v.push(__a1) + }; + { + let __a1 = 5; + v.push(__a1) + }; + { + let __a1 = 6; + v.push(__a1) + }; + { + let __a1 = 8; + v2.push(__a1) + }; + { + let __a1 = 9; + v2.push(__a1) + }; (unsafe { fn_0(&mut v, v2.clone()) }); return 0; } diff --git a/tests/unit/union_cross_arm_cast.c b/tests/unit/union_cross_arm_cast.c index 274335ace5..f3bf94ac6c 100644 --- a/tests/unit/union_cross_arm_cast.c +++ b/tests/unit/union_cross_arm_cast.c @@ -1,4 +1,3 @@ -// panic: refcount #include #include #include From 38fcdc2beb046a501d6ea68f5933f45877daa79d Mon Sep 17 00:00:00 2001 From: Nuno Lopes Date: Wed, 30 Sep 2026 16:29:11 +0100 Subject: [PATCH 10/13] remove C++ lib dependent test --- tests/unit/container_field_addr.cpp | 7 --- .../unit/out/refcount/container_field_addr.rs | 51 +++---------------- tests/unit/out/unsafe/container_field_addr.rs | 32 ++---------- 3 files changed, 11 insertions(+), 79 deletions(-) diff --git a/tests/unit/container_field_addr.cpp b/tests/unit/container_field_addr.cpp index a5d26fc13d..0bce6dcc5b 100644 --- a/tests/unit/container_field_addr.cpp +++ b/tests/unit/container_field_addr.cpp @@ -6,26 +6,19 @@ struct S { int tag; std::vector v; - std::string s; std::map m; }; static void add(std::vector *v, int k) { v->push_back(k); } -static void append(std::string *s, const char *t, std::size_t n) { - s->append(t, n); -} - static void put(std::map *m, int k, int v) { (*m)[k] = v; } static void run(S *h) { add(&h->v, h->tag); - append(&h->s, "ab", 2); put(&h->m, h->tag, 2); std::vector *pv = &h->v; pv->push_back((int)pv->size()); assert(h->v.size() == 2 && h->v[0] == 7 && h->v[1] == 1); - assert(h->s == "ab"); assert(h->m[7] == 2); assert(h->tag == 7); } diff --git a/tests/unit/out/refcount/container_field_addr.rs b/tests/unit/out/refcount/container_field_addr.rs index 266ab269fa..4a473c5880 100644 --- a/tests/unit/out/refcount/container_field_addr.rs +++ b/tests/unit/out/refcount/container_field_addr.rs @@ -13,8 +13,6 @@ pub struct S { #[offset(8)] pub v: Value>, #[offset(32)] - pub s: Value>, - #[offset(64)] pub m: BTreeMap>, } impl Clone for S { @@ -22,14 +20,13 @@ impl Clone for S { Self { tag: self.tag.clone(), v: Rc::new(RefCell::new((*self.v.borrow()).clone())), - s: Rc::new(RefCell::new((*self.s.borrow()).clone())), m: self.m.clone(), } } } impl ByteRepr for S { fn byte_size() -> usize { - 112 + 80 } } pub fn add_0(v: Ptr>, k: i32) { @@ -40,25 +37,7 @@ pub fn add_0(v: Ptr>, k: i32) { (*v.borrow()).with_mut(|__v: &mut Vec| __v.push(a0_clone)) }; } -pub fn append_1(s: Ptr>, t: Ptr, n: usize) { - let s: Value>> = Rc::new(RefCell::new(s)); - let t: Value> = Rc::new(RefCell::new(t)); - let n: Value = Rc::new(RefCell::new(n)); - { - ((*s.borrow()).clone() as Ptr>).with_mut(|__v: &mut Vec| { - __v.pop(); - __v.extend( - (*t.borrow()) - .clone() - .map(|c| c.read()) - .take((*n.borrow()) as usize), - ); - __v.push(0); - }); - ((*s.borrow()).clone() as Ptr>) - }; -} -pub fn put_2(m: Ptr>>, k: i32, v: i32) { +pub fn put_1(m: Ptr>>, k: i32, v: i32) { let m: Value>>> = Rc::new(RefCell::new(m)); let k: Value = Rc::new(RefCell::new(k)); let v: Value = Rc::new(RefCell::new(v)); @@ -70,24 +49,17 @@ pub fn put_2(m: Ptr>>, k: i32, v: i32) { }) .write((*v.borrow())); } -pub fn run_3(h: Ptr) { +pub fn run_2(h: Ptr) { let h: Value> = Rc::new(RefCell::new(h)); ({ let _v: Ptr> = ((*h.borrow()).with(|__s| __s.v.clone()).as_pointer()); let _k: i32 = (*h.borrow()).with(|__s| __s.tag); add_0(_v, _k) }); - ({ - append_1( - ((*h.borrow()).with(|__s| __s.s.clone()).as_pointer()), - Ptr::::from_string_literal(b"ab"), - 2_usize, - ) - }); ({ let _m: Ptr>> = (field_ptr!((*h.borrow()), m)); let _k: i32 = (*h.borrow()).with(|__s| __s.tag); - put_2(_m, _k, 2) + put_1(_m, _k, 2) }); let pv: Value>> = Rc::new(RefCell::new( ((*h.borrow()).with(|__s| __s.v.clone()).as_pointer()), @@ -107,17 +79,6 @@ pub fn run_3(h: Ptr) { .read()) == 1) ); - assert!( - (*(*h.borrow()).with(|__s| __s.s.clone()).borrow()) - .iter() - .copied() - .take( - (*(*h.borrow()).with(|__s| __s.s.clone()).borrow()) - .len() - .saturating_sub(1) - ) - .eq(Ptr::::from_string_literal(b"ab").to_c_string_iterator()) - ); assert!( (((field_ptr!((*h.borrow()), m) as Ptr>>) .with_mut(|__v: &mut BTreeMap>| { @@ -137,10 +98,10 @@ pub fn main() { fn main_0() -> i32 { let local: Value = Rc::new(RefCell::new(::default())); (*local.borrow_mut()).tag = 7; - ({ run_3((local.as_pointer())) }); + ({ run_2((local.as_pointer())) }); let heap: Value> = Rc::new(RefCell::new(Ptr::alloc(::default()))); field!((*heap.borrow()), tag).write(7); - ({ run_3((*heap.borrow()).clone()) }); + ({ run_2((*heap.borrow()).clone()) }); (*heap.borrow()).delete(); return 0; } diff --git a/tests/unit/out/unsafe/container_field_addr.rs b/tests/unit/out/unsafe/container_field_addr.rs index ab9b97123c..f96b0a019b 100644 --- a/tests/unit/out/unsafe/container_field_addr.rs +++ b/tests/unit/out/unsafe/container_field_addr.rs @@ -11,7 +11,6 @@ use std::rc::Rc; pub struct S { pub tag: i32, pub v: Vec, - pub s: Vec, pub m: BTreeMap>, } pub unsafe fn add_0(mut v: *mut Vec, mut k: i32) { @@ -20,33 +19,19 @@ pub unsafe fn add_0(mut v: *mut Vec, mut k: i32) { (*v).push(a0_clone) }; } -pub unsafe fn append_1(mut s: *mut Vec, mut t: *const libc::c_char, mut n: usize) { - (*s).splice((*s).len().saturating_sub(1)..(*s).len(), { - let mut v = ::std::slice::from_raw_parts(t, n as usize).to_vec(); - v.push(0); - v - }); -} -pub unsafe fn put_2(mut m: *mut BTreeMap>, mut k: i32, mut v: i32) { +pub unsafe fn put_1(mut m: *mut BTreeMap>, mut k: i32, mut v: i32) { (*(*m).entry(k).or_default().as_mut()) = v; } -pub unsafe fn run_3(mut h: *mut S) { +pub unsafe fn run_2(mut h: *mut S) { (unsafe { let _v: *mut Vec = (&mut (*h).v as *mut Vec); let _k: i32 = (*h).tag; add_0(_v, _k) }); - (unsafe { - append_1( - (&mut (*h).s as *mut Vec), - c"ab".as_ptr(), - 2_usize, - ) - }); (unsafe { let _m: *mut BTreeMap> = (&mut (*h).m as *mut BTreeMap>); let _k: i32 = (*h).tag; - put_2(_m, _k, 2) + put_1(_m, _k, 2) }); let mut pv: *mut Vec = (&mut (*h).v as *mut Vec); { @@ -57,13 +42,6 @@ pub unsafe fn run_3(mut h: *mut S) { ((((*h).v.len()) == (2_usize)) && (((&mut (*h)).v[(0_usize)]) == (7))) && (((&mut (*h)).v[(1_usize)]) == (1)) ); - assert!( - (*h).s == { - let s = c"ab".as_ptr(); - std::slice::from_raw_parts(s, (0..).take_while(|&i| *s.add(i) != 0).count() + 1) - .to_vec() - } - ); assert!(((*(*h).m.entry(7).or_default().as_mut()) == (2))); assert!((((*h).tag) == (7))); } @@ -76,10 +54,10 @@ pub fn main() { unsafe fn main_0() -> i32 { let mut local: S = ::default(); local.tag = 7; - (unsafe { run_3((&mut local as *mut S)) }); + (unsafe { run_2((&mut local as *mut S)) }); let mut heap: *mut S = (Box::leak(Box::new(::default())) as *mut S); (*heap).tag = 7; - (unsafe { run_3(heap) }); + (unsafe { run_2(heap) }); ::std::mem::drop(Box::from_raw(heap)); return 0; } From 44a96175db8bea0f3274114a8d9658e774dd09e6 Mon Sep 17 00:00:00 2001 From: Nuno Lopes Date: Wed, 30 Sep 2026 16:57:22 +0100 Subject: [PATCH 11/13] fix --- .../converter/models/converter_refcount.cpp | 69 +++++++-- .../converter/models/converter_refcount.h | 3 + docs/src/codegen/types/pointers.md | 4 +- docs/src/runtime/rc.md | 9 ++ libcc2rs/src/field.rs | 95 ++++++++++++ .../out/refcount/defaulted_move_cross_tu.rs | 14 +- tests/unit/array_field_addr.c | 1 - tests/unit/container_field_addr.cpp | 5 - tests/unit/field_aliasing.c | 1 - tests/unit/flex_array_member_sibling_write.c | 1 - tests/unit/out/refcount/array_const_init.rs | 10 +- tests/unit/out/refcount/array_field_addr.rs | 44 +++--- tests/unit/out/refcount/clone_vs_move.rs | 54 +++++-- .../unit/out/refcount/container_field_addr.rs | 38 +---- tests/unit/out/refcount/copy_assign.rs | 18 ++- tests/unit/out/refcount/copy_ctor.rs | 16 +- .../unit/out/refcount/copy_move_defaulted.rs | 66 +++++--- tests/unit/out/refcount/default_in_statics.rs | 7 +- tests/unit/out/refcount/field_aliasing.rs | 21 ++- .../unit/out/refcount/field_array_elem_ptr.rs | 142 ++++++++++++++---- .../out/refcount/field_method_receiver.rs | 13 +- tests/unit/out/refcount/field_pointers.rs | 43 ++++-- .../flex_array_member_sibling_write.rs | 58 +++++-- .../unit/out/refcount/libc_char_ptr_field.rs | 2 +- tests/unit/out/refcount/memset_struct_tail.rs | 16 +- .../refcount/operator_member_pointer_free.rs | 9 +- .../operator_member_pointer_member.rs | 15 +- tests/unit/out/refcount/pointer_array.rs | 5 +- tests/unit/out/refcount/random.rs | 119 +++++++++++---- tests/unit/out/refcount/rule_of_five.rs | 75 ++++++--- tests/unit/out/refcount/rule_of_three.rs | 55 +++++-- .../refcount/struct_member_array_adjacency.rs | 4 +- .../unit/out/refcount/union_cross_arm_cast.rs | 14 +- tests/unit/out/refcount/union_nested.rs | 12 +- tests/unit/out/unsafe/container_field_addr.rs | 16 +- tests/unit/union_nested.c | 1 - 36 files changed, 775 insertions(+), 300 deletions(-) diff --git a/cpp2rust/converter/models/converter_refcount.cpp b/cpp2rust/converter/models/converter_refcount.cpp index 206b446e0d..2abd477a52 100644 --- a/cpp2rust/converter/models/converter_refcount.cpp +++ b/cpp2rust/converter/models/converter_refcount.cpp @@ -73,6 +73,23 @@ static bool IsValueField(clang::ASTContext &ctx, return type->isConstantArrayType() || IsBoxedType(ctx, type); } +// Whether `expr` is an array field whose elements are accessed through +// array_field_ptr!, such that they are found in the memory of the struct when +// the struct is reinterpreted from other memory. +static bool IsArrayFieldPtr(clang::ASTContext &ctx, const clang::Expr *expr) { + auto *member = + clang::dyn_cast(expr->IgnoreParenImpCasts()); + auto *field = + member ? clang::dyn_cast(member->getMemberDecl()) + : nullptr; + if (!field || field->getParent()->isUnion() || + Mapper::Contains(ctx, member)) { + return false; + } + auto *type = field->getType()->getAsArrayTypeUnsafe(); + return type && !type->getElementType()->isArrayType(); +} + static bool IsPointerType(clang::ASTContext &ctx, clang::QualType type) { return type->isPointerType() || GetStrongestIteratorCategory(ctx, type) == IteratorCategory::Contiguous; @@ -421,7 +438,7 @@ bool ConverterRefCount::VisitArraySubscriptExpr( clang::ArraySubscriptExpr *expr) { auto *base = expr->getBase(); if (base->IgnoreCasts()->getType()->isPointerType() || - IsUnionArrayMember(base) || + IsUnionArrayMember(base) || IsArrayFieldPtr(ctx_, base) || (IsReferenceType(base) && base->IgnoreCasts()->getType()->isArrayType())) { ConvertPointerSubscript(expr); @@ -1542,8 +1559,23 @@ bool ConverterRefCount::VisitExplicitCastExpr(clang::ExplicitCastExpr *expr) { } else if (expr->getSubExpr()->getType()->isPointerType() && !expr->getSubExpr()->isNullPointerConstant( ctx_, clang::Expr::NPC_ValueDependentIsNull)) { - StrCat(std::format("{}.reinterpret_cast::<{}>()", - ToString(expr->getSubExpr()), + auto sub_expr = expr->getSubExpr(); + if (auto *array_type = + ctx_.getAsArrayType(sub_expr->getType()->getPointeeType())) { + // A pointer to an array is a pointer to its first element. + auto element_type = array_type->getElementType(); + PushConversionKind push(*this, ConversionKind::Unboxed); + StrCat(std::format("({} as Ptr<{}>)", ToString(sub_expr), + ToString(element_type))); + if (!ctx_.hasSameUnqualifiedType( + element_type, expr->getType()->getPointeeType())) { + StrCat(std::format(".reinterpret_cast::<{}>()", + ConvertPointeeType(expr->getType()))); + } + computed_expr_type_ = ComputedExprType::FreshPointer; + return false; + } + StrCat(std::format("{}.reinterpret_cast::<{}>()", ToString(sub_expr), ConvertPointeeType(expr->getType()))); computed_expr_type_ = ComputedExprType::FreshPointer; return false; @@ -1848,6 +1880,13 @@ bool ConverterRefCount::VisitMemberExpr(clang::MemberExpr *expr) { return false; } + if (isAddrOf() && IsArrayFieldPtr(ctx_, expr)) { + StrCat(std::format("array_field_ptr!({}, {})", ConvertRecordPtr(expr), + GetNamedDeclAsString(member))); + computed_expr_type_ = ComputedExprType::FreshPointer; + return false; + } + std::string str; if (known) { str = GetMappedAsString(expr); @@ -1893,18 +1932,7 @@ void ConverterRefCount::ConvertInlineField(clang::MemberExpr *expr) { auto *base = expr->getBase(); if (isAddrOf()) { // A pointer to the struct, offset to the field. - std::string ptr; - { - Buffer buf(*this); - PushExprKind push(*this, ExprKind::AddrOf); - if (expr->isArrow()) { - ConvertArrow(base); - } else { - Convert(base); - } - ptr = std::move(buf).str(); - } - StrCat(std::format("field_ptr!({}, {})", ptr, name)); + StrCat(std::format("field_ptr!({}, {})", ConvertRecordPtr(expr), name)); computed_expr_type_ = ComputedExprType::FreshPointer; return; } @@ -1939,6 +1967,17 @@ void ConverterRefCount::ConvertInlineField(clang::MemberExpr *expr) { SetValueFreshness(type); } +std::string ConverterRefCount::ConvertRecordPtr(clang::MemberExpr *expr) { + Buffer buf(*this); + PushExprKind push(*this, ExprKind::AddrOf); + if (expr->isArrow()) { + ConvertArrow(expr->getBase()); + } else { + Convert(expr->getBase()); + } + return std::move(buf).str(); +} + std::string ConverterRefCount::ReadField(clang::MemberExpr *expr) { // Unless the struct is reached through a pointer, in which case the field // is read in a closure, the borrow of the struct ends at the end of the diff --git a/cpp2rust/converter/models/converter_refcount.h b/cpp2rust/converter/models/converter_refcount.h index 9013d21a42..98bcb5516d 100644 --- a/cpp2rust/converter/models/converter_refcount.h +++ b/cpp2rust/converter/models/converter_refcount.h @@ -152,6 +152,9 @@ class ConverterRefCount final : public Converter { // Converts an access to a field that is stored inline in its struct. void ConvertInlineField(clang::MemberExpr *expr); + // A pointer to the struct whose field `expr` accesses. + std::string ConvertRecordPtr(clang::MemberExpr *expr); + // Copies the value of the field `expr` out of its struct, such that the // struct doesn't stay borrowed. std::string ReadField(clang::MemberExpr *expr); diff --git a/docs/src/codegen/types/pointers.md b/docs/src/codegen/types/pointers.md index 13c1494089..df6357c000 100644 --- a/docs/src/codegen/types/pointers.md +++ b/docs/src/codegen/types/pointers.md @@ -52,7 +52,9 @@ and `&p->field` is `field_ptr!(p, field)`: a pointer to the field of the struct, made from the `Value` or the `Ptr` of the struct (see [Pointers to fields](../../runtime/rc.md#pointers-to-fields)). An array decays with `arr.as_pointer() as Ptr`, a `Ptr` to element 0 of the whole array, and -`&arr[i]` is that pointer offset by `i`. +`&arr[i]` is that pointer offset by `i`. An array field decays with +`array_field_ptr!(p, arr)`, and `p->arr[i]` is read and written through that +pointer offset by `i`. Both models push the address down to the innermost place expression: `&(cond ? x : y)` becomes `if cond { &mut x } else { &mut y }` in the unsafe diff --git a/docs/src/runtime/rc.md b/docs/src/runtime/rc.md index 1949c2765b..b02ad929cc 100644 --- a/docs/src/runtime/rc.md +++ b/docs/src/runtime/rc.md @@ -127,6 +127,15 @@ elements are ordinary array pointers, and pointers to the fields of those have the array or vector as their root. A field pointer hence always points to a single object, which is why it needs no element index. +`array_field_ptr!(p, arr)` is a pointer to element 0 of the array field `arr`, +and is how the elements of an array field are accessed. It is an ordinary array +pointer into the array's `Value`, except when `p` is a reinterpreted pointer: the +array then lives in the bytes of the original allocation, so the result is a +reinterpreted pointer to those bytes, at the offset of the field. Writes to the +elements hence reach the original allocation, and the pointer can go past the +end of the struct, as C code does with a trailing `char name[1]` in an +over-allocated struct. + Reading a field through a pointer borrows the struct only for the duration of a closure, e.g., `p.with(|s| s.y)`, so that the borrow ends before the rest of the statement runs. `field!(p, y)` is the place of the field, which is read and diff --git a/libcc2rs/src/field.rs b/libcc2rs/src/field.rs index 742750f200..54eecf4d90 100644 --- a/libcc2rs/src/field.rs +++ b/libcc2rs/src/field.rs @@ -122,6 +122,18 @@ pub trait FieldPtr { field: fn(&S) -> &T, offset: fn(&S::Offsets) -> usize, ) -> Ptr; + + // A pointer to the first element of the array field `field`, at + // `offset(S::OFFSETS)`. The array is in a Value of its own, unless the + // struct is reinterpreted memory, in which case the pointer points into + // that memory. + fn array_field_ptr( + &self, + field: fn(&S) -> &Value>, + offset: fn(&S::Offsets) -> usize, + ) -> Ptr + where + T: ByteRepr; } impl FieldPtr for Ptr { @@ -150,6 +162,22 @@ impl FieldPtr for Ptr { kind: PtrKind::Field(root), } } + + #[inline] + fn array_field_ptr( + &self, + field: fn(&S) -> &Value>, + offset: fn(&S::Offsets) -> usize, + ) -> Ptr + where + T: ByteRepr, + { + if let PtrKind::Reinterpreted(_) = &self.kind { + let (alloc, byte_offset) = self.original_alloc().unwrap(); + return Ptr::from_original_alloc(alloc, byte_offset.wrapping_add(offset(&S::OFFSETS))); + } + self.with(|s| field(s).as_pointer()) + } } impl FieldPtr for Value { @@ -161,6 +189,18 @@ impl FieldPtr for Value { ) -> Ptr { self.as_pointer().field_ptr(field, offset) } + + #[inline] + fn array_field_ptr( + &self, + field: fn(&S) -> &Value>, + _offset: fn(&S::Offsets) -> usize, + ) -> Ptr + where + T: ByteRepr, + { + field(&self.borrow()).as_pointer() + } } // A pointer to field `field` of the struct that a Ptr or a Value holds, @@ -173,6 +213,16 @@ macro_rules! field_ptr { }}; } +// A pointer to the first element of array field `field` of the struct that a +// Ptr or a Value holds, e.g., `array_field_ptr!(p, arr)`. +#[macro_export] +macro_rules! array_field_ptr { + ($base:expr, $field:ident) => {{ + use $crate::FieldPtr as _; + ($base).array_field_ptr(|__s| &__s.$field, |__o| __o.$field) + }}; +} + // Something that is read and written in place, like the object a pointer // points to. pub trait Place: Clone { @@ -515,6 +565,51 @@ mod tests { raw.delete(); } + #[derive(Record)] + struct Masked { + #[offset(0)] + n: i32, + #[offset(4)] + mask: Value>, + } + + impl ByteRepr for Masked { + fn byte_size() -> usize { + 8 + } + fn to_bytes(&self, buf: &mut [u8]) { + self.n.to_bytes(&mut buf[0..4]); + self.mask.borrow().to_bytes(&mut buf[4..8]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + n: i32::from_bytes(&buf[0..4]), + mask: Rc::new(RefCell::new(>::from_bytes(&buf[4..8]))), + } + } + } + + #[test] + fn array_field() { + let s: Value = Rc::new(RefCell::new(Masked::from_bytes(&[0; 8]))); + let m = array_field_ptr!(s, mask); + m.offset(2).write(5); + assert_eq!(s.borrow().mask.borrow()[2], 5); + assert_eq!(array_field_ptr!(s.as_pointer(), mask), m); + // The array field of a reinterpreted struct is in its memory, which + // the pointer can go past the end of the struct in. + let raw: Ptr = Ptr::alloc_array(vec![0u8; 12].into_boxed_slice()); + let view: Ptr = raw.reinterpret_cast(); + let m = array_field_ptr!(view, mask); + m.offset(1).write(7); + assert_eq!(raw.offset(5).read(), 7); + m.offset(6).write(9); + assert_eq!(raw.offset(10).read(), 9); + field_ptr!(view, n).write(3); + assert_eq!(m.offset(1).read(), 7); + raw.delete(); + } + #[test] fn field_place() { let s = outer(); diff --git a/tests/multi-file/defaulted_move_cross_tu/out/refcount/defaulted_move_cross_tu.rs b/tests/multi-file/defaulted_move_cross_tu/out/refcount/defaulted_move_cross_tu.rs index 17d66178da..a525987fc9 100644 --- a/tests/multi-file/defaulted_move_cross_tu/out/refcount/defaulted_move_cross_tu.rs +++ b/tests/multi-file/defaulted_move_cross_tu/out/refcount/defaulted_move_cross_tu.rs @@ -56,9 +56,13 @@ pub fn sum_0(s: Ptr) -> i32 { return { let _lhs = { let _lhs = ((*s.with(|__s| __s.v.clone()).borrow()).len() as i32); - _lhs + (*s.with(|__s| __s.n.clone()).borrow())[(0) as usize] + _lhs + ((array_field_ptr!(s, n) as Ptr) + .offset((0) as isize) + .read()) }; - _lhs + (*s.with(|__s| __s.n.clone()).borrow())[(1) as usize] + _lhs + ((array_field_ptr!(s, n) as Ptr) + .offset((1) as isize) + .read()) }; } pub fn main() { @@ -90,13 +94,13 @@ impl SImpl for Ptr { &mut (*{ (*_a0.upgrade().deref()).v.clone() }.borrow_mut()), )); { - (((*self).with(|__s| __s.n.clone()).as_pointer()) as Ptr) + ((array_field_ptr!((*self), n)) as Ptr) .to_any() .memcpy( - &(({ (*_a0.upgrade().deref()).n.clone() }.as_pointer()) as Ptr).to_any(), + &((array_field_ptr!(_a0, n)) as Ptr).to_any(), 8_usize as usize, ); - (((*self).with(|__s| __s.n.clone()).as_pointer()) as Ptr).to_any() + ((array_field_ptr!((*self), n)) as Ptr).to_any() }; return (*self).clone(); } diff --git a/tests/unit/array_field_addr.c b/tests/unit/array_field_addr.c index 0802f69000..e02202c51a 100644 --- a/tests/unit/array_field_addr.c +++ b/tests/unit/array_field_addr.c @@ -1,4 +1,3 @@ -// no-compile: refcount #include #include #include diff --git a/tests/unit/container_field_addr.cpp b/tests/unit/container_field_addr.cpp index 0bce6dcc5b..54a194d221 100644 --- a/tests/unit/container_field_addr.cpp +++ b/tests/unit/container_field_addr.cpp @@ -6,20 +6,15 @@ struct S { int tag; std::vector v; - std::map m; }; static void add(std::vector *v, int k) { v->push_back(k); } -static void put(std::map *m, int k, int v) { (*m)[k] = v; } - static void run(S *h) { add(&h->v, h->tag); - put(&h->m, h->tag, 2); std::vector *pv = &h->v; pv->push_back((int)pv->size()); assert(h->v.size() == 2 && h->v[0] == 7 && h->v[1] == 1); - assert(h->m[7] == 2); assert(h->tag == 7); } diff --git a/tests/unit/field_aliasing.c b/tests/unit/field_aliasing.c index 2c885598f9..d9cedb1879 100644 --- a/tests/unit/field_aliasing.c +++ b/tests/unit/field_aliasing.c @@ -1,4 +1,3 @@ -// panic: refcount #include #include diff --git a/tests/unit/flex_array_member_sibling_write.c b/tests/unit/flex_array_member_sibling_write.c index d90d935e4d..7245cba653 100644 --- a/tests/unit/flex_array_member_sibling_write.c +++ b/tests/unit/flex_array_member_sibling_write.c @@ -1,4 +1,3 @@ -// panic: refcount #include #include #include diff --git a/tests/unit/out/refcount/array_const_init.rs b/tests/unit/out/refcount/array_const_init.rs index 754e6c98a6..688be221bb 100644 --- a/tests/unit/out/refcount/array_const_init.rs +++ b/tests/unit/out/refcount/array_const_init.rs @@ -66,8 +66,9 @@ fn main_0() -> i32 { let i: Value = Rc::new(RefCell::new(0)); 'loop_: while ((((*i.borrow()) < 3) as i32) != 0) { assert!( - ((((*{ (*s_0.with(Value::clone).borrow()).tail.clone() }.borrow()) - [(*i.borrow()) as usize] + (((((array_field_ptr!(s_0.with(|v| v.as_pointer()), tail) as Ptr::) + .offset((*i.borrow()) as isize) + .read()) == 0) as i32) != 0) ); @@ -76,8 +77,9 @@ fn main_0() -> i32 { let i: Value = Rc::new(RefCell::new(0)); 'loop_: while ((((*i.borrow()) < 4) as i32) != 0) { assert!( - (((((*{ (*s_0.with(Value::clone).borrow()).buf.clone() }.borrow()) - [(*i.borrow()) as usize] as i32) + ((((((array_field_ptr!(s_0.with(|v| v.as_pointer()), buf) as Ptr::) + .offset((*i.borrow()) as isize) + .read()) as i32) == 0) as i32) != 0) ); diff --git a/tests/unit/out/refcount/array_field_addr.rs b/tests/unit/out/refcount/array_field_addr.rs index 59545dcce3..01203a1327 100644 --- a/tests/unit/out/refcount/array_field_addr.rs +++ b/tests/unit/out/refcount/array_field_addr.rs @@ -6,27 +6,30 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(VaArg, FnPtrArg)] +#[derive(Record, VaArg, FnPtrArg)] pub struct S { - pub before: Value, + #[offset(0)] + pub before: i32, + #[offset(4)] pub mask: Value>, - pub after: Value, + #[offset(8)] + pub after: i32, } impl Clone for S { fn clone(&self) -> Self { Self { - before: Rc::new(RefCell::new((*self.before.borrow()).clone())), + before: self.before.clone(), mask: Rc::new(RefCell::new((*self.mask.borrow()).clone())), - after: Rc::new(RefCell::new((*self.after.borrow()).clone())), + after: self.after.clone(), } } } impl Default for S { fn default() -> Self { S { - before: Rc::new(RefCell::new(0_i32)), + before: 0_i32, mask: Rc::new(RefCell::new((0..4).map(|_| 0_u8).collect::>())), - after: Rc::new(RefCell::new(0_i32)), + after: 0_i32, } } } @@ -35,15 +38,15 @@ impl ByteRepr for S { 12 } fn to_bytes(&self, buf: &mut [u8]) { - (*self.before.borrow()).to_bytes(&mut buf[0..4]); + self.before.to_bytes(&mut buf[0..4]); (*self.mask.borrow()).to_bytes(&mut buf[4..8]); - (*self.after.borrow()).to_bytes(&mut buf[8..12]); + self.after.to_bytes(&mut buf[8..12]); } fn from_bytes(buf: &[u8]) -> Self { Self { - before: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + before: ::from_bytes(&buf[0..4]), mask: Rc::new(RefCell::new(>::from_bytes(&buf[4..8]))), - after: Rc::new(RefCell::new(::from_bytes(&buf[8..12]))), + after: ::from_bytes(&buf[8..12]), } } } @@ -56,21 +59,19 @@ fn main_0() -> i32 { libcc2rs::malloc_refcount(12usize).reinterpret_cast::(), )); assert!((((!((*s.borrow()).is_null())) as i32) != 0)); - (*(*(*s.borrow()).upgrade().deref()).before.borrow_mut()) = 1; + field!((*s.borrow()), before).write(1); { - (((*(*s.borrow()).upgrade().deref()).mask.as_pointer() as Ptr) as Ptr) + ((array_field_ptr!((*s.borrow()), mask) as Ptr) as Ptr) .to_any() .memset((5) as u8, ::std::mem::size_of::<[u8; 4]>() as usize); - (((*(*s.borrow()).upgrade().deref()).mask.as_pointer() as Ptr) as Ptr).to_any() + ((array_field_ptr!((*s.borrow()), mask) as Ptr) as Ptr).to_any() }; - (*(*(*s.borrow()).upgrade().deref()).after.borrow_mut()) = 2; - ((*(*s.borrow()).upgrade().deref()).mask.as_pointer()) - .reinterpret_cast::() - .write(7_u8); + field!((*s.borrow()), after).write(2); + ((array_field_ptr!((*s.borrow()), mask)) as Ptr).write(7_u8); let out: Value> = Rc::new(RefCell::new((0..4).map(|_| 0_u8).collect::>())); { ((out.as_pointer() as Ptr) as Ptr).to_any().memcpy( - &(((*(*s.borrow()).upgrade().deref()).mask.as_pointer()) as Ptr).to_any(), + &((array_field_ptr!((*s.borrow()), mask)) as Ptr).to_any(), ::std::mem::size_of::<[u8; 4]>() as usize, ); ((out.as_pointer() as Ptr) as Ptr).to_any() @@ -81,9 +82,8 @@ fn main_0() -> i32 { != 0) ); assert!( - (((((((*(*(*s.borrow()).upgrade().deref()).before.borrow()) == 1) as i32) != 0) - && ((((*(*(*s.borrow()).upgrade().deref()).after.borrow()) == 2) as i32) != 0)) - as i32) + (((((((*s.borrow()).with(|__s| __s.before) == 1) as i32) != 0) + && ((((*s.borrow()).with(|__s| __s.after) == 2) as i32) != 0)) as i32) != 0) ); libcc2rs::free_refcount((*s.borrow()).to_any()); diff --git a/tests/unit/out/refcount/clone_vs_move.rs b/tests/unit/out/refcount/clone_vs_move.rs index 79a2f1fc20..941ccd5a00 100644 --- a/tests/unit/out/refcount/clone_vs_move.rs +++ b/tests/unit/out/refcount/clone_vs_move.rs @@ -145,15 +145,51 @@ fn main_0() -> i32 { _lhs == (x1.as_pointer()) }); assert!((({ (*f1.borrow()).z.clone() }.read()) == 4)); - (*{ (*f2.borrow()).a.clone() }.borrow_mut())[(0) as usize].prefix_inc(); - (*{ (*f2.borrow()).a.clone() }.borrow_mut())[(1) as usize].prefix_inc(); - (*{ (*f2.borrow()).a.clone() }.borrow_mut())[(2) as usize].prefix_inc(); - assert!(((*{ (*f2.borrow()).a.clone() }.borrow())[(0) as usize] == 1)); - assert!(((*{ (*f2.borrow()).a.clone() }.borrow())[(1) as usize] == 2)); - assert!(((*{ (*f2.borrow()).a.clone() }.borrow())[(2) as usize] == 3)); - assert!(((*{ (*f1.borrow()).a.clone() }.borrow())[(0) as usize] == 0)); - assert!(((*{ (*f1.borrow()).a.clone() }.borrow())[(1) as usize] == 1)); - assert!(((*{ (*f1.borrow()).a.clone() }.borrow())[(2) as usize] == 2)); + (array_field_ptr!(f2.as_pointer(), a) as Ptr) + .offset((0) as isize) + .with_mut(|__v| __v.prefix_inc()); + (array_field_ptr!(f2.as_pointer(), a) as Ptr) + .offset((1) as isize) + .with_mut(|__v| __v.prefix_inc()); + (array_field_ptr!(f2.as_pointer(), a) as Ptr) + .offset((2) as isize) + .with_mut(|__v| __v.prefix_inc()); + assert!( + (((array_field_ptr!(f2.as_pointer(), a) as Ptr::) + .offset((0) as isize) + .read()) + == 1) + ); + assert!( + (((array_field_ptr!(f2.as_pointer(), a) as Ptr::) + .offset((1) as isize) + .read()) + == 2) + ); + assert!( + (((array_field_ptr!(f2.as_pointer(), a) as Ptr::) + .offset((2) as isize) + .read()) + == 3) + ); + assert!( + (((array_field_ptr!(f1.as_pointer(), a) as Ptr::) + .offset((0) as isize) + .read()) + == 0) + ); + assert!( + (((array_field_ptr!(f1.as_pointer(), a) as Ptr::) + .offset((1) as isize) + .read()) + == 1) + ); + assert!( + (((array_field_ptr!(f1.as_pointer(), a) as Ptr::) + .offset((2) as isize) + .read()) + == 2) + ); (*f2.borrow_mut()).bar.w = 20; assert!(({ (*f2.borrow()).bar.w } == 20)); assert!(({ (*f1.borrow()).bar.w } == 10)); diff --git a/tests/unit/out/refcount/container_field_addr.rs b/tests/unit/out/refcount/container_field_addr.rs index 4a473c5880..1f12a2d8e5 100644 --- a/tests/unit/out/refcount/container_field_addr.rs +++ b/tests/unit/out/refcount/container_field_addr.rs @@ -12,21 +12,18 @@ pub struct S { pub tag: i32, #[offset(8)] pub v: Value>, - #[offset(32)] - pub m: BTreeMap>, } impl Clone for S { fn clone(&self) -> Self { Self { tag: self.tag.clone(), v: Rc::new(RefCell::new((*self.v.borrow()).clone())), - m: self.m.clone(), } } } impl ByteRepr for S { fn byte_size() -> usize { - 80 + 32 } } pub fn add_0(v: Ptr>, k: i32) { @@ -37,30 +34,13 @@ pub fn add_0(v: Ptr>, k: i32) { (*v.borrow()).with_mut(|__v: &mut Vec| __v.push(a0_clone)) }; } -pub fn put_1(m: Ptr>>, k: i32, v: i32) { - let m: Value>>> = Rc::new(RefCell::new(m)); - let k: Value = Rc::new(RefCell::new(k)); - let v: Value = Rc::new(RefCell::new(v)); - ((*m.borrow()).clone() as Ptr>>) - .with_mut(|__v: &mut BTreeMap>| { - __v.entry((*k.borrow())) - .or_insert_with(|| Rc::new(RefCell::new(::default()))) - .as_pointer() - }) - .write((*v.borrow())); -} -pub fn run_2(h: Ptr) { +pub fn run_1(h: Ptr) { let h: Value> = Rc::new(RefCell::new(h)); ({ let _v: Ptr> = ((*h.borrow()).with(|__s| __s.v.clone()).as_pointer()); let _k: i32 = (*h.borrow()).with(|__s| __s.tag); add_0(_v, _k) }); - ({ - let _m: Ptr>> = (field_ptr!((*h.borrow()), m)); - let _k: i32 = (*h.borrow()).with(|__s| __s.tag); - put_1(_m, _k, 2) - }); let pv: Value>> = Rc::new(RefCell::new( ((*h.borrow()).with(|__s| __s.v.clone()).as_pointer()), )); @@ -79,16 +59,6 @@ pub fn run_2(h: Ptr) { .read()) == 1) ); - assert!( - (((field_ptr!((*h.borrow()), m) as Ptr>>) - .with_mut(|__v: &mut BTreeMap>| { - __v.entry(7) - .or_insert_with(|| Rc::new(RefCell::new(::default()))) - .as_pointer() - }) - .read()) - == 2) - ); assert!(((*h.borrow()).with(|__s| __s.tag) == 7)); } pub fn main() { @@ -98,10 +68,10 @@ pub fn main() { fn main_0() -> i32 { let local: Value = Rc::new(RefCell::new(::default())); (*local.borrow_mut()).tag = 7; - ({ run_2((local.as_pointer())) }); + ({ run_1((local.as_pointer())) }); let heap: Value> = Rc::new(RefCell::new(Ptr::alloc(::default()))); field!((*heap.borrow()), tag).write(7); - ({ run_2((*heap.borrow()).clone()) }); + ({ run_1((*heap.borrow()).clone()) }); (*heap.borrow()).delete(); return 0; } diff --git a/tests/unit/out/refcount/copy_assign.rs b/tests/unit/out/refcount/copy_assign.rs index 72329098d1..200a70dbb2 100644 --- a/tests/unit/out/refcount/copy_assign.rs +++ b/tests/unit/out/refcount/copy_assign.rs @@ -179,14 +179,26 @@ fn main_0() -> i32 { ({ PartialImpl::copy_assign(&field_ptr!(h.as_pointer(), p), b.as_pointer()) }); ({ PartialImpl::copy_assign( - &({ (*h.borrow()).arr.clone() }.as_pointer() as Ptr).offset(1), + &(array_field_ptr!(h.as_pointer(), arr) as Ptr).offset((1) as isize), c.as_pointer(), ) }); assert!(({ (*h.borrow()).p.v } == 2) && ({ (*h.borrow()).p.keep } == 40)); assert!( - ({ (*{ (*h.borrow()).arr.clone() }.borrow())[(1) as usize].v } == 2) - && ({ (*{ (*h.borrow()).arr.clone() }.borrow())[(1) as usize].keep } == 60) + ({ + (*(array_field_ptr!(h.as_pointer(), arr) as Ptr) + .offset((1) as isize) + .upgrade() + .deref()) + .v + } == 2) + && ({ + (*(array_field_ptr!(h.as_pointer(), arr) as Ptr) + .offset((1) as isize) + .upgrade() + .deref()) + .keep + } == 60) ); assert!((assigns_0.with(|rc| *rc.borrow()) == 8)); let n: Value = Rc::new(RefCell::new(NonConstAssign::new())); diff --git a/tests/unit/out/refcount/copy_ctor.rs b/tests/unit/out/refcount/copy_ctor.rs index 9bc6e2a611..08e6e14ffb 100644 --- a/tests/unit/out/refcount/copy_ctor.rs +++ b/tests/unit/out/refcount/copy_ctor.rs @@ -191,8 +191,20 @@ fn main_0() -> i32 { let hold2: Value = Rc::new(RefCell::new((*hold.borrow()).clone())); assert!( (({ (*hold2.borrow()).c.v } == 8) - && ({ (*{ (*hold2.borrow()).arr.clone() }.borrow())[(0) as usize].v } == 9)) - && ({ (*{ (*hold2.borrow()).arr.clone() }.borrow())[(1) as usize].v } == 10) + && ({ + (*(array_field_ptr!(hold2.as_pointer(), arr) as Ptr) + .offset((0) as isize) + .upgrade() + .deref()) + .v + } == 9)) + && ({ + (*(array_field_ptr!(hold2.as_pointer(), arr) as Ptr) + .offset((1) as isize) + .upgrade() + .deref()) + .v + } == 10) ); assert!((copies_0.with(|rc| *rc.borrow()) == 9)); let vec_: Value> = Rc::new(RefCell::new(Vec::new())); diff --git a/tests/unit/out/refcount/copy_move_defaulted.rs b/tests/unit/out/refcount/copy_move_defaulted.rs index 98222e4fcd..635cca2a3f 100644 --- a/tests/unit/out/refcount/copy_move_defaulted.rs +++ b/tests/unit/out/refcount/copy_move_defaulted.rs @@ -329,11 +329,19 @@ pub fn same_0(a: Ptr, b: Ptr) -> bool { let _lhs = a.with(|__s| __s.inner.x); _lhs == b.with(|__s| __s.inner.x) })) && ({ - let _lhs = (*a.with(|__s| __s.arr.clone()).borrow())[(0) as usize]; - _lhs == (*b.with(|__s| __s.arr.clone()).borrow())[(0) as usize] + let _lhs = ((array_field_ptr!(a, arr) as Ptr) + .offset((0) as isize) + .read()); + _lhs == ((array_field_ptr!(b, arr) as Ptr) + .offset((0) as isize) + .read()) })) && ({ - let _lhs = (*a.with(|__s| __s.arr.clone()).borrow())[(1) as usize]; - _lhs == (*b.with(|__s| __s.arr.clone()).borrow())[(1) as usize] + let _lhs = ((array_field_ptr!(a, arr) as Ptr) + .offset((1) as isize) + .read()); + _lhs == ((array_field_ptr!(b, arr) as Ptr) + .offset((1) as isize) + .read()) }); } pub fn main() { @@ -383,7 +391,10 @@ fn main_0() -> i32 { let k: Value = Rc::new(RefCell::new((*i.borrow()).clone())); assert!( (({ (*j.borrow()).v } == 5) && ({ (*j.borrow()).inner.x } == 50)) - && ((*{ (*j.borrow()).arr.clone() }.borrow())[(1) as usize] == 6) + && (((array_field_ptr!(j.as_pointer(), arr) as Ptr::) + .offset((1) as isize) + .read()) + == 6) ); assert!(({ (*i.borrow()).v } == 5) && ({ (*k.borrow()).v } == 5)); let l: Value = Rc::new(RefCell::new(Implicit { @@ -394,7 +405,10 @@ fn main_0() -> i32 { (*l.borrow_mut()) = (*j.borrow()).clone(); assert!( (({ (*l.borrow()).v } == 5) && ({ (*l.borrow()).inner.x } == 50)) - && ((*{ (*l.borrow()).arr.clone() }.borrow())[(0) as usize] == 5) + && (((array_field_ptr!(l.as_pointer(), arr) as Ptr::) + .offset((0) as isize) + .read()) + == 5) ); let vec_: Value> = Rc::new(RefCell::new(Vec::new())); { @@ -468,7 +482,10 @@ fn main_0() -> i32 { assert!( ((({ (*r.borrow()).n } == 3) && ((*{ (*r.borrow()).data.clone() }.borrow()).len() == 3_usize)) - && ((*{ (*r.borrow()).arr.clone() }.borrow())[(1) as usize] == 4)) + && (((array_field_ptr!(r.as_pointer(), arr) as Ptr::) + .offset((1) as isize) + .read()) + == 4)) && ((*{ (*q.borrow()).data.clone() }.borrow()).is_empty()) ); assert!( @@ -529,8 +546,12 @@ fn main_0() -> i32 { (*{ (*o1.borrow()).data.clone() }.borrow_mut()).push(__a1) }; (*o1.borrow_mut()).n = 5; - (*{ (*o1.borrow()).arr.clone() }.borrow_mut())[(0) as usize] = 5; - (*{ (*o1.borrow()).arr.clone() }.borrow_mut())[(1) as usize] = 6; + (array_field_ptr!(o1.as_pointer(), arr) as Ptr) + .offset((0) as isize) + .write(5); + (array_field_ptr!(o1.as_pointer(), arr) as Ptr) + .offset((1) as isize) + .write(6); { let _p: Ptr<_> = Ptr::alloc(7); (field_ptr!(o1.as_pointer(), p) as Ptr>>).write(_p.to_owned_opt()) @@ -539,7 +560,10 @@ fn main_0() -> i32 { assert!( ((({ (*o2.borrow()).n } == 5) && ((*{ (*o2.borrow()).data.clone() }.borrow()).len() == 1_usize)) - && ((*{ (*o2.borrow()).arr.clone() }.borrow())[(1) as usize] == 6)) + && (((array_field_ptr!(o2.as_pointer(), arr) as Ptr::) + .offset((1) as isize) + .read()) + == 6)) && ((*{ (*o2.borrow()).p.clone() }.as_ref().unwrap().borrow()) == 7) ); assert!( @@ -554,7 +578,10 @@ fn main_0() -> i32 { .offset(0_usize) .read()) == 5)) - && ((*{ (*o3.borrow()).arr.clone() }.borrow())[(0) as usize] == 5)) + && (((array_field_ptr!(o3.as_pointer(), arr) as Ptr::) + .offset((0) as isize) + .read()) + == 5)) && ((*{ (*o3.borrow()).p.clone() }.as_ref().unwrap().borrow()) == 7) ); assert!( @@ -579,7 +606,10 @@ fn main_0() -> i32 { ({ HolderImpl::move_assign(&h3.as_pointer(), h2.as_pointer()) }); assert!( ((({ (*h3.borrow()).inner.x } == 4) - && ((*{ (*h3.borrow()).e.arr.clone() }.borrow())[(1) as usize] == 5)) + && (((array_field_ptr!(field_ptr!(h3.as_pointer(), e), arr) as Ptr::) + .offset((1) as isize) + .read()) + == 5)) && ((*{ (*h3.borrow()).p.clone() }.as_ref().unwrap().borrow()) == 9)) && (({ (*h2.borrow()).p.clone() }.as_pointer()).is_null()) ); @@ -599,13 +629,13 @@ impl BufferImpl for Ptr { let __rhs = { (*_a0.upgrade().deref()).n }; field!((*self), n).write(__rhs); { - (((*self).with(|__s| __s.arr.clone()).as_pointer()) as Ptr) + ((array_field_ptr!((*self), arr)) as Ptr) .to_any() .memcpy( - &(({ (*_a0.upgrade().deref()).arr.clone() }.as_pointer()) as Ptr).to_any(), + &((array_field_ptr!(_a0, arr)) as Ptr).to_any(), 8_usize as usize, ); - (((*self).with(|__s| __s.arr.clone()).as_pointer()) as Ptr).to_any() + ((array_field_ptr!((*self), arr)) as Ptr).to_any() }; return (*self).clone(); } @@ -656,13 +686,13 @@ impl OwnerImpl for Ptr { let __rhs = { (*_a0.upgrade().deref()).n }; field!((*self), n).write(__rhs); { - (((*self).with(|__s| __s.arr.clone()).as_pointer()) as Ptr) + ((array_field_ptr!((*self), arr)) as Ptr) .to_any() .memcpy( - &(({ (*_a0.upgrade().deref()).arr.clone() }.as_pointer()) as Ptr).to_any(), + &((array_field_ptr!(_a0, arr)) as Ptr).to_any(), 8_usize as usize, ); - (((*self).with(|__s| __s.arr.clone()).as_pointer()) as Ptr).to_any() + ((array_field_ptr!((*self), arr)) as Ptr).to_any() }; (field_ptr!((*self), p) as Ptr>>) .write(field!(_a0, p).with_mut(|__v: &mut Option>| __v.take())); diff --git a/tests/unit/out/refcount/default_in_statics.rs b/tests/unit/out/refcount/default_in_statics.rs index 9ba87d6316..e725401ec4 100644 --- a/tests/unit/out/refcount/default_in_statics.rs +++ b/tests/unit/out/refcount/default_in_statics.rs @@ -220,9 +220,10 @@ fn main_0() -> i32 { let i: Value = Rc::new(RefCell::new(0)); 'loop_: while ((*i.borrow()) < 3) { assert!( - ((*{ (*static_outer_1.with(Value::clone).borrow()).arr.clone() }.borrow()) - [(*i.borrow()) as usize]) - .is_null() + ((array_field_ptr!(static_outer_1.with(|v| v.as_pointer()), arr) as Ptr>) + .offset((*i.borrow()) as isize) + .read()) + .is_null() ); (*i.borrow_mut()).prefix_inc(); } diff --git a/tests/unit/out/refcount/field_aliasing.rs b/tests/unit/out/refcount/field_aliasing.rs index a9ab905f33..0df608b5c9 100644 --- a/tests/unit/out/refcount/field_aliasing.rs +++ b/tests/unit/out/refcount/field_aliasing.rs @@ -125,19 +125,26 @@ fn main_0() -> i32 { != 0) ); let __rhs = (*p.borrow()).with(|__s| __s.total); - (*(*p.borrow()).with(|__s| __s.arr.clone()).borrow_mut()) - [((*p.borrow()).with(|__s| __s.n)) as usize] = __rhs; + (array_field_ptr!((*p.borrow()), arr) as Ptr) + .offset(((*p.borrow()).with(|__s| __s.n)) as isize) + .write(__rhs); { let _ptr = field!((*p.borrow()), n); _ptr.write(_ptr.read() + 1) }; - (*(*p.borrow()).with(|__s| __s.arr.clone()).borrow_mut()) - [((*p.borrow()).with(|__s| __s.n)) as usize] = (*q.borrow()).with(|__s| __s.in_.x); + (array_field_ptr!((*p.borrow()), arr) as Ptr) + .offset(((*p.borrow()).with(|__s| __s.n)) as isize) + .write((*q.borrow()).with(|__s| __s.in_.x)); assert!( - ((((((((((*(*q.borrow()).with(|__s| __s.arr.clone()).borrow())[(0) as usize] == 3) as i32) + (((((((((((array_field_ptr!((*q.borrow()), arr) as Ptr::) + .offset((0) as isize) + .read()) + == 3) as i32) != 0) - && ((((*(*q.borrow()).with(|__s| __s.arr.clone()).borrow())[(1) as usize] == 4) - as i32) + && (((((array_field_ptr!((*q.borrow()), arr) as Ptr::) + .offset((1) as isize) + .read()) + == 4) as i32) != 0)) as i32) != 0) && ((((*q.borrow()).with(|__s| __s.n) == 1) as i32) != 0)) as i32) diff --git a/tests/unit/out/refcount/field_array_elem_ptr.rs b/tests/unit/out/refcount/field_array_elem_ptr.rs index 7adcf1aacb..d9ff4fc285 100644 --- a/tests/unit/out/refcount/field_array_elem_ptr.rs +++ b/tests/unit/out/refcount/field_array_elem_ptr.rs @@ -113,50 +113,64 @@ fn main_0() -> i32 { tail: 99, })); let c: Value> = Rc::new(RefCell::new( - (({ (*s.borrow()).coords.clone() }.as_pointer() as Ptr).offset(1)), + ((array_field_ptr!(s.as_pointer(), coords) as Ptr).offset((1) as isize)), )); assert!((((*c.borrow()).read()) == 20)); (*c.borrow()).write(21); - assert!(((*{ (*s.borrow()).coords.clone() }.borrow())[(1) as usize] == 21)); + assert!( + (((array_field_ptr!(s.as_pointer(), coords) as Ptr::) + .offset((1) as isize) + .read()) + == 21) + ); (*c.borrow_mut()) += 2; assert!((((*c.borrow()).read()) == 40)); assert!( (((*c.borrow()).clone() - - (({ (*s.borrow()).coords.clone() }.as_pointer() as Ptr).offset(0))) + - ((array_field_ptr!(s.as_pointer(), coords) as Ptr::).offset((0) as isize))) as i64 == 3_i64) ); assert!((((*c.borrow()).offset((-1_i32) as isize).read()) == 30)); + assert!((({ sum_0((array_field_ptr!(s.as_pointer(), coords) as Ptr::), 4,) }) == 101)); assert!( (({ sum_0( - ({ (*s.borrow()).coords.clone() }.as_pointer() as Ptr), - 4, - ) - }) == 101) - ); - assert!( - (({ - sum_0( - (({ (*s.borrow()).coords.clone() }.as_pointer() as Ptr).offset(2)), + ((array_field_ptr!(s.as_pointer(), coords) as Ptr).offset((2) as isize)), 2, ) }) == 70) ); let p: Value> = Rc::new(RefCell::new( - (({ (*s.borrow()).points.clone() }.as_pointer() as Ptr).offset(1)), + ((array_field_ptr!(s.as_pointer(), points) as Ptr).offset((1) as isize)), )); assert!(((*p.borrow()).with(|__s| __s.x) == 3)); ({ set_y_1((*p.borrow()).offset((1) as isize), 60) }); - assert!(({ (*{ (*s.borrow()).points.clone() }.borrow())[(2) as usize].y } == 60)); + assert!( + ({ + (*(array_field_ptr!(s.as_pointer(), points) as Ptr) + .offset((2) as isize) + .upgrade() + .deref()) + .y + } == 60) + ); let py: Value> = Rc::new(RefCell::new( (field_ptr!( - ({ (*s.borrow()).points.clone() }.as_pointer() as Ptr).offset(0), + (array_field_ptr!(s.as_pointer(), points) as Ptr).offset((0) as isize), y )), )); (*py.borrow()).write(7); - assert!(({ (*{ (*s.borrow()).points.clone() }.borrow())[(0) as usize].y } == 7)); + assert!( + ({ + (*(array_field_ptr!(s.as_pointer(), points) as Ptr) + .offset((0) as isize) + .upgrade() + .deref()) + .y + } == 7) + ); let px: Value> = Rc::new(RefCell::new( (field_ptr!(((*p.borrow()).offset((1) as isize)), x)), )); @@ -164,35 +178,99 @@ fn main_0() -> i32 { let _ptr = (*px.borrow()).clone(); _ptr.write(_ptr.read() + 50) }; - assert!(({ (*{ (*s.borrow()).points.clone() }.borrow())[(2) as usize].x } == 55)); + assert!( + ({ + (*(array_field_ptr!(s.as_pointer(), points) as Ptr) + .offset((2) as isize) + .upgrade() + .deref()) + .x + } == 55) + ); let sp: Value> = Rc::new(RefCell::new((s.as_pointer()))); let d: Value> = Rc::new(RefCell::new( - ((*sp.borrow()).with(|__s| __s.coords.clone()).as_pointer() as Ptr) - .offset((3) as isize), + (array_field_ptr!((*sp.borrow()), coords) as Ptr).offset((3) as isize), )); (*d.borrow()).write(41); - assert!(((*{ (*s.borrow()).coords.clone() }.borrow())[(3) as usize] == 41)); + assert!( + (((array_field_ptr!(s.as_pointer(), coords) as Ptr::) + .offset((3) as isize) + .read()) + == 41) + ); let __rhs = { - let _lhs = (*(*sp.borrow()).with(|__s| __s.coords.clone()).borrow())[(0) as usize]; - _lhs + { (*(*sp.borrow()).with(|__s| __s.points.clone()).borrow())[(0) as usize].x } + let _lhs = ((array_field_ptr!((*sp.borrow()), coords) as Ptr) + .offset((0) as isize) + .read()); + _lhs + { + (*(array_field_ptr!((*sp.borrow()), points) as Ptr) + .offset((0) as isize) + .upgrade() + .deref()) + .x + } }; - (*(*sp.borrow()).with(|__s| __s.points.clone()).borrow_mut())[(1) as usize].y = __rhs; - assert!(({ (*{ (*s.borrow()).points.clone() }.borrow())[(1) as usize].y } == 11)); + field!( + (array_field_ptr!((*sp.borrow()), points) as Ptr).offset((1) as isize), + y + ) + .write(__rhs); + assert!( + ({ + (*(array_field_ptr!(s.as_pointer(), points) as Ptr) + .offset((1) as isize) + .upgrade() + .deref()) + .y + } == 11) + ); let q: Value> = Rc::new(RefCell::new( - ((*sp.borrow()).with(|__s| __s.points.clone()).as_pointer() as Ptr), + (array_field_ptr!((*sp.borrow()), points) as Ptr), )); field!((*q.borrow()).offset((2) as isize), x).write(8); - assert!(({ (*(*sp.borrow()).with(|__s| __s.points.clone()).borrow())[(2) as usize].x } == 8)); + assert!( + ({ + (*(array_field_ptr!((*sp.borrow()), points) as Ptr) + .offset((2) as isize) + .upgrade() + .deref()) + .x + } == 8) + ); let t: Value = Rc::new(RefCell::new((*s.borrow()).clone())); - (*{ (*t.borrow()).coords.clone() }.borrow_mut())[(0) as usize] = 0; - (*{ (*t.borrow()).points.clone() }.borrow_mut())[(0) as usize].x = 0; + (array_field_ptr!(t.as_pointer(), coords) as Ptr) + .offset((0) as isize) + .write(0); + field!( + (array_field_ptr!(t.as_pointer(), points) as Ptr).offset((0) as isize), + x + ) + .write(0); assert!( - ((*{ (*s.borrow()).coords.clone() }.borrow())[(0) as usize] == 10) - && ({ (*{ (*s.borrow()).points.clone() }.borrow())[(0) as usize].x } == 1) + (((array_field_ptr!(s.as_pointer(), coords) as Ptr::) + .offset((0) as isize) + .read()) + == 10) + && ({ + (*(array_field_ptr!(s.as_pointer(), points) as Ptr) + .offset((0) as isize) + .upgrade() + .deref()) + .x + } == 1) ); assert!( - (((*{ (*t.borrow()).coords.clone() }.borrow())[(1) as usize] == 21) - && ({ (*{ (*t.borrow()).points.clone() }.borrow())[(2) as usize].y } == 60)) + ((((array_field_ptr!(t.as_pointer(), coords) as Ptr::) + .offset((1) as isize) + .read()) + == 21) + && ({ + (*(array_field_ptr!(t.as_pointer(), points) as Ptr) + .offset((2) as isize) + .upgrade() + .deref()) + .y + } == 60)) && ({ (*t.borrow()).tail } == 99) ); return 0; diff --git a/tests/unit/out/refcount/field_method_receiver.rs b/tests/unit/out/refcount/field_method_receiver.rs index 2b217b284b..b021ea09e8 100644 --- a/tests/unit/out/refcount/field_method_receiver.rs +++ b/tests/unit/out/refcount/field_method_receiver.rs @@ -70,14 +70,14 @@ pub fn run_0(o: Ptr) { assert!((({ CounterImpl::get(&field_ptr!((*o.borrow()), c),) }) == 2)); ({ CounterImpl::add( - &((*o.borrow()).with(|__s| __s.arr.clone()).as_pointer() as Ptr).offset(1), + &(array_field_ptr!((*o.borrow()), arr) as Ptr).offset((1) as isize), 5, ) }); assert!( (({ CounterImpl::get( - &((*o.borrow()).with(|__s| __s.arr.clone()).as_pointer() as Ptr).offset(1), + &(array_field_ptr!((*o.borrow()), arr) as Ptr).offset((1) as isize), ) }) == 5) ); @@ -89,22 +89,21 @@ pub fn run_0(o: Ptr) { }); ({ let _other: Ptr = - (((*o.borrow()).with(|__s| __s.arr.clone()).as_pointer() as Ptr).offset(1)); + ((array_field_ptr!((*o.borrow()), arr) as Ptr).offset((1) as isize)); CounterImpl::take( - &((*o.borrow()).with(|__s| __s.arr.clone()).as_pointer() as Ptr).offset(0), + &(array_field_ptr!((*o.borrow()), arr) as Ptr).offset((0) as isize), _other, ) }); assert!( (({ CounterImpl::get( - &((*o.borrow()).with(|__s| __s.arr.clone()).as_pointer() as Ptr).offset(0), + &(array_field_ptr!((*o.borrow()), arr) as Ptr).offset((0) as isize), ) }) == 5) && (({ CounterImpl::get( - &((*o.borrow()).with(|__s| __s.arr.clone()).as_pointer() as Ptr) - .offset(1), + &(array_field_ptr!((*o.borrow()), arr) as Ptr).offset((1) as isize), ) }) == 0) ); diff --git a/tests/unit/out/refcount/field_pointers.rs b/tests/unit/out/refcount/field_pointers.rs index 709834ee14..5465e3f680 100644 --- a/tests/unit/out/refcount/field_pointers.rs +++ b/tests/unit/out/refcount/field_pointers.rs @@ -143,21 +143,29 @@ fn main_0() -> i32 { assert!(({ (*o.borrow()).inner.a } == 3)); let pi: Value> = Rc::new(RefCell::new( (field_ptr!( - ({ (*o.borrow()).items.clone() }.as_pointer() as Ptr).offset(1), + (array_field_ptr!(o.as_pointer(), items) as Ptr).offset((1) as isize), a )), )); (*pi.borrow()).write(4); - assert!(({ (*{ (*o.borrow()).items.clone() }.borrow())[(1) as usize].a } == 4)); + assert!( + ({ + (*(array_field_ptr!(o.as_pointer(), items) as Ptr) + .offset((1) as isize) + .upgrade() + .deref()) + .a + } == 4) + ); assert!({ let _lhs = (field_ptr!( - ({ (*o.borrow()).items.clone() }.as_pointer() as Ptr).offset(0), + (array_field_ptr!(o.as_pointer(), items) as Ptr).offset((0) as isize), a )); _lhs != (*pi.borrow()).clone() }); let name: Value> = Rc::new(RefCell::new( - ({ (*o.borrow()).inner.name.clone() }.as_pointer() as Ptr), + (array_field_ptr!(field_ptr!(o.as_pointer(), inner), name) as Ptr), )); let i: Value = Rc::new(RefCell::new(0)); 'loop_: while ((*i.borrow()) < 3) { @@ -166,16 +174,17 @@ fn main_0() -> i32 { (*i.borrow_mut()).prefix_inc(); } assert!( - (({ (*o.borrow()).inner.name.clone() }.as_pointer() as Ptr::) + ((array_field_ptr!(field_ptr!(o.as_pointer(), inner), name) as Ptr::) .to_c_string_iterator() .count() == 3_usize) ); assert!({ let _lhs = (*name.borrow()).offset((3) as isize); - _lhs == (({ (*o.borrow()).inner.name.clone() }.as_pointer() as Ptr).offset(3)) + _lhs == ((array_field_ptr!(field_ptr!(o.as_pointer(), inner), name) as Ptr) + .offset((3) as isize)) }); - let __rhs = (({ (*o.borrow()).buf.clone() }.as_pointer() as Ptr).offset(1)); + let __rhs = ((array_field_ptr!(o.as_pointer(), buf) as Ptr).offset((1) as isize)); (*o.borrow_mut()).cursor = __rhs; { (*o.borrow()).cursor.clone() }.write(5); { @@ -183,10 +192,13 @@ fn main_0() -> i32 { _ptr.write(_ptr.read() + 1) }; assert!( - ((*{ (*o.borrow()).buf.clone() }.borrow())[(1) as usize] == 6) + (((array_field_ptr!(o.as_pointer(), buf) as Ptr::) + .offset((1) as isize) + .read()) + == 6) && ({ let _lhs = { (*o.borrow()).cursor.clone() }; - _lhs == (({ (*o.borrow()).buf.clone() }.as_pointer() as Ptr).offset(2)) + _lhs == ((array_field_ptr!(o.as_pointer(), buf) as Ptr).offset((2) as isize)) }) ); (*o.borrow_mut()).x = 0; @@ -210,20 +222,20 @@ fn main_0() -> i32 { let y: Value = Rc::new(RefCell::new(0)); { ((field_ptr!( - ({ (*o.borrow()).items.clone() }.as_pointer() as Ptr).offset(2), + (array_field_ptr!(o.as_pointer(), items) as Ptr).offset((2) as isize), a )) as Ptr) .to_any() .memcpy( &((field_ptr!( - ({ (*o.borrow()).items.clone() }.as_pointer() as Ptr).offset(1), + (array_field_ptr!(o.as_pointer(), items) as Ptr).offset((1) as isize), a )) as Ptr) .to_any(), ::std::mem::size_of::() as usize, ); ((field_ptr!( - ({ (*o.borrow()).items.clone() }.as_pointer() as Ptr).offset(2), + (array_field_ptr!(o.as_pointer(), items) as Ptr).offset((2) as isize), a )) as Ptr) .to_any() @@ -231,7 +243,7 @@ fn main_0() -> i32 { { ((y.as_pointer()) as Ptr).to_any().memcpy( &((field_ptr!( - ({ (*o.borrow()).items.clone() }.as_pointer() as Ptr).offset(2), + (array_field_ptr!(o.as_pointer(), items) as Ptr).offset((2) as isize), a )) as Ptr) .to_any(), @@ -270,8 +282,9 @@ pub trait OuterImpl { } impl OuterImpl for Ptr { fn next(&self) -> i32 { - return (*(*self).with(|__s| __s.buf.clone()).borrow()) - [(field!((*self), x).with_mut(|__v| __v.postfix_inc())) as usize]; + return ((array_field_ptr!((*self), buf) as Ptr) + .offset((field!((*self), x).with_mut(|__v| __v.postfix_inc())) as isize) + .read()); } fn sum(&self) -> i32 { return ((*self).with(|__s| __s.x) + (*self).with(|__s| __s.inner.a)); diff --git a/tests/unit/out/refcount/flex_array_member_sibling_write.rs b/tests/unit/out/refcount/flex_array_member_sibling_write.rs index 0388141469..7884257fea 100644 --- a/tests/unit/out/refcount/flex_array_member_sibling_write.rs +++ b/tests/unit/out/refcount/flex_array_member_sibling_write.rs @@ -123,20 +123,20 @@ fn main_0() -> i32 { )); assert!((((!((*s.borrow()).is_null())) as i32) != 0)); { - (((*s.borrow()).with(|__s| __s.name.clone()).as_pointer() as Ptr) as Ptr) + ((array_field_ptr!((*s.borrow()), name) as Ptr) as Ptr) .to_any() .memcpy( &Ptr::::from_string_literal(b"abcdefg").to_any(), 8_usize as usize, ); - (((*s.borrow()).with(|__s| __s.name.clone()).as_pointer() as Ptr) as Ptr).to_any() + ((array_field_ptr!((*s.borrow()), name) as Ptr) as Ptr).to_any() }; field!((*s.borrow()), n).write(5); assert!(((((*s.borrow()).with(|__s| __s.n) == 5) as i32) != 0)); assert!( ((({ - let mut __it1 = ((*s.borrow()).with(|__s| __s.name.clone()).as_pointer() as Ptr) - .to_c_string_iterator(); + let mut __it1 = + (array_field_ptr!((*s.borrow()), name) as Ptr).to_c_string_iterator(); let mut __it2 = Ptr::::from_string_literal(b"abcdefg").to_c_string_iterator(); loop { let __c1 = __it1.next(); @@ -159,15 +159,33 @@ fn main_0() -> i32 { assert!((((!((*t.borrow()).is_null())) as i32) != 0)); field!((*t.borrow()), n).write(2); field!((*t.borrow()), cap).write(2); - (*(*t.borrow()).with(|__s| __s.a.clone()).borrow_mut())[(0) as usize].id = 10; - (*(*t.borrow()).with(|__s| __s.a.clone()).borrow_mut())[(1) as usize].w = 20; + field!( + (array_field_ptr!((*t.borrow()), a) as Ptr).offset((0) as isize), + id + ) + .write(10); + field!( + (array_field_ptr!((*t.borrow()), a) as Ptr).offset((1) as isize), + w + ) + .write(20); field!((*t.borrow()), n).write(3); assert!( - (((((({ (*(*t.borrow()).with(|__s| __s.a.clone()).borrow())[(0) as usize].id } == 10) - as i32) + (((((({ + (*(array_field_ptr!((*t.borrow()), a) as Ptr) + .offset((0) as isize) + .upgrade() + .deref()) + .id + } == 10) as i32) != 0) - && ((({ (*(*t.borrow()).with(|__s| __s.a.clone()).borrow())[(1) as usize].w } == 20) - as i32) + && ((({ + (*(array_field_ptr!((*t.borrow()), a) as Ptr) + .offset((1) as isize) + .upgrade() + .deref()) + .w + } == 20) as i32) != 0)) as i32) != 0) ); @@ -177,18 +195,28 @@ fn main_0() -> i32 { assert!( ((({ let _lhs = (*tail.borrow()).clone(); - _lhs == (((*t.borrow()).with(|__s| __s.a.clone()).as_pointer() as Ptr).offset(1)) + _lhs == ((array_field_ptr!((*t.borrow()), a) as Ptr).offset((1) as isize)) }) as i32) != 0) ); field!((*tail.borrow()).offset((0) as isize), id).write(30); field!((*t.borrow()), cap).write(4); assert!( - (((((({ (*(*t.borrow()).with(|__s| __s.a.clone()).borrow())[(1) as usize].id } == 30) - as i32) + (((((({ + (*(array_field_ptr!((*t.borrow()), a) as Ptr) + .offset((1) as isize) + .upgrade() + .deref()) + .id + } == 30) as i32) != 0) - && ((({ (*(*t.borrow()).with(|__s| __s.a.clone()).borrow())[(1) as usize].w } == 20) - as i32) + && ((({ + (*(array_field_ptr!((*t.borrow()), a) as Ptr) + .offset((1) as isize) + .upgrade() + .deref()) + .w + } == 20) as i32) != 0)) as i32) != 0) ); diff --git a/tests/unit/out/refcount/libc_char_ptr_field.rs b/tests/unit/out/refcount/libc_char_ptr_field.rs index 691022c8fb..a01c9751a9 100644 --- a/tests/unit/out/refcount/libc_char_ptr_field.rs +++ b/tests/unit/out/refcount/libc_char_ptr_field.rs @@ -53,7 +53,7 @@ fn main_0() -> i32 { }), )); let dname: Value> = Rc::new(RefCell::new( - ((*d.borrow()).with(|__s| __s.d_name.clone()).as_pointer() as Ptr), + (array_field_ptr!((*d.borrow()), d_name) as Ptr), )); return 0; } diff --git a/tests/unit/out/refcount/memset_struct_tail.rs b/tests/unit/out/refcount/memset_struct_tail.rs index c992ec0505..011720c5a3 100644 --- a/tests/unit/out/refcount/memset_struct_tail.rs +++ b/tests/unit/out/refcount/memset_struct_tail.rs @@ -90,8 +90,10 @@ fn main_0() -> i32 { (((((((((((((*p.borrow()).with(|__s| __s.a) == 0) as i32) != 0) && ((((*p.borrow()).with(|__s| __s.b) == 0_i64) as i32) != 0)) as i32) != 0) - && (((((*(*p.borrow()).with(|__s| __s.c.clone()).borrow())[(4) as usize] as i32) == 0) - as i32) + && ((((((array_field_ptr!((*p.borrow()), c) as Ptr::) + .offset((4) as isize) + .read()) as i32) + == 0) as i32) != 0)) as i32) != 0) && ((((*p.borrow()).with(|__s| __s.last) == 0) as i32) != 0)) as i32) @@ -99,7 +101,9 @@ fn main_0() -> i32 { ); field!((*p.borrow()), a).write(1); field!((*p.borrow()), b).write(2_i64); - (*(*p.borrow()).with(|__s| __s.c.clone()).borrow_mut())[(0) as usize] = (('x' as i32) as u8); + (array_field_ptr!((*p.borrow()), c) as Ptr) + .offset((0) as isize) + .write((('x' as i32) as u8)); field!((*p.borrow()), last).write(3); { ((field_ptr!((*p.borrow()), b)) as Ptr) @@ -117,8 +121,10 @@ fn main_0() -> i32 { ); assert!( (((((((*p.borrow()).with(|__s| __s.b) == 0_i64) as i32) != 0) - && (((((*(*p.borrow()).with(|__s| __s.c.clone()).borrow())[(0) as usize] as i32) == 0) - as i32) + && ((((((array_field_ptr!((*p.borrow()), c) as Ptr::) + .offset((0) as isize) + .read()) as i32) + == 0) as i32) != 0)) as i32) != 0) ); diff --git a/tests/unit/out/refcount/operator_member_pointer_free.rs b/tests/unit/out/refcount/operator_member_pointer_free.rs index 8d82758df1..d7722c078f 100644 --- a/tests/unit/out/refcount/operator_member_pointer_free.rs +++ b/tests/unit/out/refcount/operator_member_pointer_free.rs @@ -66,7 +66,7 @@ pub fn operator_deref_0(s: Ptr) -> Ptr { return field_ptr!(s, inner); } pub fn operator_addr_1(s: Ptr) -> Ptr { - return ((s.with(|__s| __s.data.clone()).as_pointer() as Ptr).offset(0)); + return ((array_field_ptr!(s, data) as Ptr).offset((0) as isize)); } pub fn main() { __cpp2rust_init_globals(); @@ -102,7 +102,12 @@ fn main_0() -> i32 { )); assert!((((*p.borrow()).read()) == 1)); (*p.borrow()).write(5); - assert!(((*{ (*s.borrow()).data.clone() }.borrow())[(0) as usize] == 5)); + assert!( + (((array_field_ptr!(s.as_pointer(), data) as Ptr::) + .offset((0) as isize) + .read()) + == 5) + ); return 0; } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/operator_member_pointer_member.rs b/tests/unit/out/refcount/operator_member_pointer_member.rs index c1fecbff74..acfc7211af 100644 --- a/tests/unit/out/refcount/operator_member_pointer_member.rs +++ b/tests/unit/out/refcount/operator_member_pointer_member.rs @@ -95,7 +95,12 @@ fn main_0() -> i32 { let p: Value> = Rc::new(RefCell::new(({ SImpl::operator_addr(&s.as_pointer()) }))); assert!((((*p.borrow()).read()) == 1)); (*p.borrow()).write(5); - assert!(((*{ (*s.borrow()).data.clone() }.borrow())[(0) as usize] == 5)); + assert!( + (((array_field_ptr!(s.as_pointer(), data) as Ptr::) + .offset((0) as isize) + .read()) + == 5) + ); let t: Value
= Rc::new(RefCell::new(
::default())); assert!(((({ Table::operator_index(1,) }).read()) == 8)); ({ Table::operator_index(1) }).write(80); @@ -117,13 +122,11 @@ pub trait SImpl { impl SImpl for Ptr { fn operator_index_1(&self, i: i32) -> Ptr { let i: Value = Rc::new(RefCell::new(i)); - return ((*self).with(|__s| __s.data.clone()).as_pointer() as Ptr) - .offset((*i.borrow())); + return (array_field_ptr!((*self), data) as Ptr).offset((*i.borrow()) as isize); } fn operator_index_2(&self, i: i32) -> Ptr { let i: Value = Rc::new(RefCell::new(i)); - return ((*self).with(|__s| __s.data.clone()).as_pointer() as Ptr) - .offset((*i.borrow())); + return (array_field_ptr!((*self), data) as Ptr).offset((*i.borrow()) as isize); } fn operator_deref(&self) -> Ptr { return field_ptr!((*self), inner); @@ -132,7 +135,7 @@ impl SImpl for Ptr { return (field_ptr!((*self), inner)); } fn operator_addr(&self) -> Ptr { - return (((*self).with(|__s| __s.data.clone()).as_pointer() as Ptr).offset(0)); + return ((array_field_ptr!((*self), data) as Ptr).offset((0) as isize)); } } pub fn __cpp2rust_init_globals() { diff --git a/tests/unit/out/refcount/pointer_array.rs b/tests/unit/out/refcount/pointer_array.rs index 47ef01d81e..33a436ee0b 100644 --- a/tests/unit/out/refcount/pointer_array.rs +++ b/tests/unit/out/refcount/pointer_array.rs @@ -46,7 +46,10 @@ pub fn IncrementAll_0(s: Ptr) { let i: Value = Rc::new(RefCell::new(0)); 'loop_: while ((*i.borrow()) < 3) { { - let _ptr = (*s.with(|__s| __s.arr.clone()).borrow())[(*i.borrow()) as usize].clone(); + let _ptr = ((array_field_ptr!(s, arr) as Ptr>) + .offset((*i.borrow()) as isize) + .read()) + .clone(); _ptr.write(_ptr.read() + 1) }; (*i.borrow_mut()).prefix_inc(); diff --git a/tests/unit/out/refcount/random.rs b/tests/unit/out/refcount/random.rs index c61cd6f894..c54f4caf50 100644 --- a/tests/unit/out/refcount/random.rs +++ b/tests/unit/out/refcount/random.rs @@ -101,18 +101,34 @@ fn main_0() -> i32 { x: { (*y1.borrow()).x }, y: { (*y1.borrow()).y }, a: Rc::new(RefCell::new(Box::new([ - (*{ (*y1.borrow()).a.clone() }.borrow())[(0) as usize], - (*{ (*y1.borrow()).a.clone() }.borrow())[(1) as usize], - (*{ (*y1.borrow()).a.clone() }.borrow())[(2) as usize], - (*{ (*y1.borrow()).a.clone() }.borrow())[(3) as usize], - (*{ (*y1.borrow()).a.clone() }.borrow())[(4) as usize], + ((array_field_ptr!(y1.as_pointer(), a) as Ptr) + .offset((0) as isize) + .read()), + ((array_field_ptr!(y1.as_pointer(), a) as Ptr) + .offset((1) as isize) + .read()), + ((array_field_ptr!(y1.as_pointer(), a) as Ptr) + .offset((2) as isize) + .read()), + ((array_field_ptr!(y1.as_pointer(), a) as Ptr) + .offset((3) as isize) + .read()), + ((array_field_ptr!(y1.as_pointer(), a) as Ptr) + .offset((4) as isize) + .read()), ]))), r: ({ (*y1.borrow()).r.clone() }).clone(), p: { (*y1.borrow()).p.clone() }, pair: { (*y1.borrow()).pair.clone() }, ap: Rc::new(RefCell::new(Box::new([ - ((*{ (*y1.borrow()).ap.clone() }.borrow())[(0) as usize]).clone(), - ((*{ (*y1.borrow()).ap.clone() }.borrow())[(1) as usize]).clone(), + ((array_field_ptr!(y1.as_pointer(), ap) as Ptr>) + .offset((0) as isize) + .read()) + .clone(), + ((array_field_ptr!(y1.as_pointer(), ap) as Ptr>) + .offset((1) as isize) + .read()) + .clone(), ]))), })); let ry1: Ptr = y1.as_pointer(); @@ -121,18 +137,34 @@ fn main_0() -> i32 { x: ry1.with(|__s| __s.x), y: ry1.with(|__s| __s.y), a: Rc::new(RefCell::new(Box::new([ - (*ry1.with(|__s| __s.a.clone()).borrow())[(0) as usize], - (*ry1.with(|__s| __s.a.clone()).borrow())[(1) as usize], - (*ry1.with(|__s| __s.a.clone()).borrow())[(2) as usize], - (*ry1.with(|__s| __s.a.clone()).borrow())[(3) as usize], - (*ry1.with(|__s| __s.a.clone()).borrow())[(4) as usize], + ((array_field_ptr!(ry1, a) as Ptr) + .offset((0) as isize) + .read()), + ((array_field_ptr!(ry1, a) as Ptr) + .offset((1) as isize) + .read()), + ((array_field_ptr!(ry1, a) as Ptr) + .offset((2) as isize) + .read()), + ((array_field_ptr!(ry1, a) as Ptr) + .offset((3) as isize) + .read()), + ((array_field_ptr!(ry1, a) as Ptr) + .offset((4) as isize) + .read()), ]))), r: (ry1.with(|__s| __s.r.clone())).clone(), p: ry1.with(|__s| __s.p.clone()), pair: ry1.with(|__s| __s.pair.clone()), ap: Rc::new(RefCell::new(Box::new([ - ((*ry1.with(|__s| __s.ap.clone()).borrow())[(0) as usize]).clone(), - ((*ry1.with(|__s| __s.ap.clone()).borrow())[(1) as usize]).clone(), + ((array_field_ptr!(ry1, ap) as Ptr>) + .offset((0) as isize) + .read()) + .clone(), + ((array_field_ptr!(ry1, ap) as Ptr>) + .offset((1) as isize) + .read()) + .clone(), ]))), })); let ry2: Ptr = (ry1).clone(); @@ -141,18 +173,34 @@ fn main_0() -> i32 { x: (*py1.borrow()).with(|__s| __s.x), y: (*py1.borrow()).with(|__s| __s.y), a: Rc::new(RefCell::new(Box::new([ - (*(*py1.borrow()).with(|__s| __s.a.clone()).borrow())[(0) as usize], - (*(*py1.borrow()).with(|__s| __s.a.clone()).borrow())[(1) as usize], - (*(*py1.borrow()).with(|__s| __s.a.clone()).borrow())[(2) as usize], - (*(*py1.borrow()).with(|__s| __s.a.clone()).borrow())[(3) as usize], - (*(*py1.borrow()).with(|__s| __s.a.clone()).borrow())[(4) as usize], + ((array_field_ptr!((*py1.borrow()), a) as Ptr) + .offset((0) as isize) + .read()), + ((array_field_ptr!((*py1.borrow()), a) as Ptr) + .offset((1) as isize) + .read()), + ((array_field_ptr!((*py1.borrow()), a) as Ptr) + .offset((2) as isize) + .read()), + ((array_field_ptr!((*py1.borrow()), a) as Ptr) + .offset((3) as isize) + .read()), + ((array_field_ptr!((*py1.borrow()), a) as Ptr) + .offset((4) as isize) + .read()), ]))), r: ((*py1.borrow()).with(|__s| __s.r.clone())).clone(), p: (*py1.borrow()).with(|__s| __s.p.clone()), pair: (*py1.borrow()).with(|__s| __s.pair.clone()), ap: Rc::new(RefCell::new(Box::new([ - ((*(*py1.borrow()).with(|__s| __s.ap.clone()).borrow())[(0) as usize]).clone(), - ((*(*py1.borrow()).with(|__s| __s.ap.clone()).borrow())[(1) as usize]).clone(), + ((array_field_ptr!((*py1.borrow()), ap) as Ptr>) + .offset((0) as isize) + .read()) + .clone(), + ((array_field_ptr!((*py1.borrow()), ap) as Ptr>) + .offset((1) as isize) + .read()) + .clone(), ]))), })); let ry3: Ptr = (*py1.borrow()).clone(); @@ -208,7 +256,9 @@ fn main_0() -> i32 { (*px3.borrow_mut()) = (*cp1.borrow()).clone(); (*y1.borrow_mut()).x = 2; (*y1.borrow_mut()).y = 3; - (*{ (*y1.borrow()).a.clone() }.borrow_mut())[(0) as usize] = 100; + (array_field_ptr!(y1.as_pointer(), a) as Ptr) + .offset((0) as isize) + .write(100); { (*y1.borrow()).r.clone() }.write(10); (*y1.borrow_mut()).p = (*px3.borrow()).clone(); (*px3.borrow_mut()) = (*px2.borrow()).clone(); @@ -220,9 +270,16 @@ fn main_0() -> i32 { x ) .write(100); - (*{ (*y1.borrow()).ap.clone() }.borrow_mut())[(0) as usize] = (x1.as_pointer()); - (*{ (*y1.borrow()).ap.clone() }.borrow_mut())[(1) as usize] = (x2.as_pointer()); - (*{ (*y1.borrow()).ap.clone() }.borrow())[(0) as usize].write(0); + (array_field_ptr!(y1.as_pointer(), ap) as Ptr>) + .offset((0) as isize) + .write((x1.as_pointer())); + (array_field_ptr!(y1.as_pointer(), ap) as Ptr>) + .offset((1) as isize) + .write((x2.as_pointer())); + ((array_field_ptr!(y1.as_pointer(), ap) as Ptr>) + .offset((0) as isize) + .read()) + .write(0); (*c1.borrow_mut()) = ((*x1.borrow()) + 1); let j: Value = Rc::new(RefCell::new(0)); let new_y: Value = Rc::new(RefCell::new(Pair { @@ -240,7 +297,9 @@ fn main_0() -> i32 { let __rhs = { (*new_y.borrow()).x }; (*y1.borrow_mut()).x = __rhs; let i: Value = Rc::new(RefCell::new(1_u32)); - (*{ (*y1.borrow()).a.clone() }.borrow_mut())[(*i.borrow()) as usize] = -1_i32; + (array_field_ptr!(y1.as_pointer(), a) as Ptr) + .offset((*i.borrow()) as isize) + .write(-1_i32); (*x1.borrow_mut()).postfix_inc(); (*x1.borrow_mut()).prefix_inc(); (*y1.borrow_mut()).x.postfix_inc(); @@ -272,11 +331,15 @@ impl PairImpl for Ptr { fn method(&self) { field!((*self), x).with_mut(|__v| __v.postfix_inc()); field!((*self), y).with_mut(|__v| __v.prefix_inc()); - (*(*self).with(|__s| __s.a.clone()).borrow_mut())[(4) as usize] = 1; + (array_field_ptr!((*self), a) as Ptr) + .offset((4) as isize) + .write(1); (*self).with(|__s| __s.r.clone()).write(1); field!((*self), p).write(Ptr::::null()); field!((*self), pair).write(Ptr::::null()); - (*(*self).with(|__s| __s.ap.clone()).borrow_mut())[(0) as usize] = Ptr::::null(); + (array_field_ptr!((*self), ap) as Ptr>) + .offset((0) as isize) + .write(Ptr::::null()); } fn as_val(&self) -> i32 { return (*self).with(|__s| __s.x); diff --git a/tests/unit/out/refcount/rule_of_five.rs b/tests/unit/out/refcount/rule_of_five.rs index 516da97933..a40601882a 100644 --- a/tests/unit/out/refcount/rule_of_five.rs +++ b/tests/unit/out/refcount/rule_of_five.rs @@ -37,7 +37,9 @@ impl Buffer { } else { -1_i32 }; - (*this.with(|__s| __s.data.clone()).borrow_mut())[(*i.borrow()) as usize] = __rhs; + (array_field_ptr!(this, data) as Ptr) + .offset((*i.borrow()) as isize) + .write(__rhs); (*i.borrow_mut()).prefix_inc(); } (*alive_0.with(Value::clone).borrow_mut()).prefix_inc(); @@ -51,8 +53,12 @@ impl Buffer { let this: Ptr = __this.as_pointer(); let i: Value = Rc::new(RefCell::new(0)); 'loop_: while ((*i.borrow()) < 4) { - let __rhs = (*o.with(|__s| __s.data.clone()).borrow())[(*i.borrow()) as usize]; - (*this.with(|__s| __s.data.clone()).borrow_mut())[(*i.borrow()) as usize] = __rhs; + let __rhs = ((array_field_ptr!(o, data) as Ptr) + .offset((*i.borrow()) as isize) + .read()); + (array_field_ptr!(this, data) as Ptr) + .offset((*i.borrow()) as isize) + .write(__rhs); (*i.borrow_mut()).prefix_inc(); } (*alive_0.with(Value::clone).borrow_mut()).prefix_inc(); @@ -67,9 +73,15 @@ impl Buffer { let this: Ptr = __this.as_pointer(); let i: Value = Rc::new(RefCell::new(0)); 'loop_: while ((*i.borrow()) < 4) { - let __rhs = (*o.with(|__s| __s.data.clone()).borrow())[(*i.borrow()) as usize]; - (*this.with(|__s| __s.data.clone()).borrow_mut())[(*i.borrow()) as usize] = __rhs; - (*o.with(|__s| __s.data.clone()).borrow_mut())[(*i.borrow()) as usize] = -1_i32; + let __rhs = ((array_field_ptr!(o, data) as Ptr) + .offset((*i.borrow()) as isize) + .read()); + (array_field_ptr!(this, data) as Ptr) + .offset((*i.borrow()) as isize) + .write(__rhs); + (array_field_ptr!(o, data) as Ptr) + .offset((*i.borrow()) as isize) + .write(-1_i32); (*i.borrow_mut()).prefix_inc(); } field!(o, size).write(0); @@ -120,18 +132,31 @@ fn main_0() -> i32 { ((alive_0.with(|rc| *rc.borrow()) == 2) && (copies_1.with(|rc| *rc.borrow()) == 1)) && (moves_2.with(|rc| *rc.borrow()) == 0) ); - (*{ (*b.borrow()).data.clone() }.borrow_mut())[(0) as usize] = 100; - assert!(((*{ (*a.borrow()).data.clone() }.borrow())[(0) as usize] == 0)); + (array_field_ptr!(b.as_pointer(), data) as Ptr) + .offset((0) as isize) + .write(100); + assert!( + (((array_field_ptr!(a.as_pointer(), data) as Ptr::) + .offset((0) as isize) + .read()) + == 0) + ); let c: Value = Rc::new(RefCell::new(Buffer::move_from({ a.as_pointer() }))); let _dtor_c = ScopedDestructor::new(&c, |__p| __p.destructor()); assert!((alive_0.with(|rc| *rc.borrow()) == 3) && (moves_2.with(|rc| *rc.borrow()) == 1)); assert!( ({ (*a.borrow()).size } == 0) - && ((*{ (*a.borrow()).data.clone() }.borrow())[(0) as usize] == -1_i32) + && (((array_field_ptr!(a.as_pointer(), data) as Ptr::) + .offset((0) as isize) + .read()) + == -1_i32) ); assert!( ({ (*c.borrow()).size } == 4) - && ((*{ (*c.borrow()).data.clone() }.borrow())[(3) as usize] == 3) + && (((array_field_ptr!(c.as_pointer(), data) as Ptr::) + .offset((3) as isize) + .read()) + == 3) ); let d: Value = Rc::new(RefCell::new(({ make_3(2) }))); let _dtor_d = ScopedDestructor::new(&d, |__p| __p.destructor()); @@ -139,12 +164,18 @@ fn main_0() -> i32 { ({ BufferImpl::copy_assign(&d.as_pointer(), b.as_pointer()) }); assert!( (({ (*d.borrow()).size } == 4) - && ((*{ (*d.borrow()).data.clone() }.borrow())[(0) as usize] == 100)) + && (((array_field_ptr!(d.as_pointer(), data) as Ptr::) + .offset((0) as isize) + .read()) + == 100)) && (copies_1.with(|rc| *rc.borrow()) == 2) ); ({ BufferImpl::move_assign(&d.as_pointer(), c.as_pointer()) }); assert!( - (((*{ (*d.borrow()).data.clone() }.borrow())[(0) as usize] == 0) + ((((array_field_ptr!(d.as_pointer(), data) as Ptr::) + .offset((0) as isize) + .read()) + == 0) && ({ (*c.borrow()).size } == 0)) && (moves_2.with(|rc| *rc.borrow()) == 3) ); @@ -174,8 +205,12 @@ impl BufferImpl for Ptr { field!((*self), size).write(__rhs); let i: Value = Rc::new(RefCell::new(0)); 'loop_: while ((*i.borrow()) < 4) { - let __rhs = (*o.with(|__s| __s.data.clone()).borrow())[(*i.borrow()) as usize]; - (*(*self).with(|__s| __s.data.clone()).borrow_mut())[(*i.borrow()) as usize] = __rhs; + let __rhs = ((array_field_ptr!(o, data) as Ptr) + .offset((*i.borrow()) as isize) + .read()); + (array_field_ptr!((*self), data) as Ptr) + .offset((*i.borrow()) as isize) + .write(__rhs); (*i.borrow_mut()).prefix_inc(); } (*copies_1.with(Value::clone).borrow_mut()).prefix_inc(); @@ -189,9 +224,15 @@ impl BufferImpl for Ptr { field!((*self), size).write(__rhs); let i: Value = Rc::new(RefCell::new(0)); 'loop_: while ((*i.borrow()) < 4) { - let __rhs = (*o.with(|__s| __s.data.clone()).borrow())[(*i.borrow()) as usize]; - (*(*self).with(|__s| __s.data.clone()).borrow_mut())[(*i.borrow()) as usize] = __rhs; - (*o.with(|__s| __s.data.clone()).borrow_mut())[(*i.borrow()) as usize] = -1_i32; + let __rhs = ((array_field_ptr!(o, data) as Ptr) + .offset((*i.borrow()) as isize) + .read()); + (array_field_ptr!((*self), data) as Ptr) + .offset((*i.borrow()) as isize) + .write(__rhs); + (array_field_ptr!(o, data) as Ptr) + .offset((*i.borrow()) as isize) + .write(-1_i32); (*i.borrow_mut()).prefix_inc(); } field!(o, size).write(0); diff --git a/tests/unit/out/refcount/rule_of_three.rs b/tests/unit/out/refcount/rule_of_three.rs index eb1c063467..48b6542d29 100644 --- a/tests/unit/out/refcount/rule_of_three.rs +++ b/tests/unit/out/refcount/rule_of_three.rs @@ -34,7 +34,9 @@ impl Buffer { } else { -1_i32 }; - (*this.with(|__s| __s.data.clone()).borrow_mut())[(*i.borrow()) as usize] = __rhs; + (array_field_ptr!(this, data) as Ptr) + .offset((*i.borrow()) as isize) + .write(__rhs); (*i.borrow_mut()).prefix_inc(); } (*alive_0.with(Value::clone).borrow_mut()).prefix_inc(); @@ -48,8 +50,12 @@ impl Buffer { let this: Ptr = __this.as_pointer(); let i: Value = Rc::new(RefCell::new(0)); 'loop_: while ((*i.borrow()) < 4) { - let __rhs = (*o.with(|__s| __s.data.clone()).borrow())[(*i.borrow()) as usize]; - (*this.with(|__s| __s.data.clone()).borrow_mut())[(*i.borrow()) as usize] = __rhs; + let __rhs = ((array_field_ptr!(o, data) as Ptr) + .offset((*i.borrow()) as isize) + .read()); + (array_field_ptr!(this, data) as Ptr) + .offset((*i.borrow()) as isize) + .write(__rhs); (*i.borrow_mut()).prefix_inc(); } (*alive_0.with(Value::clone).borrow_mut()).prefix_inc(); @@ -86,7 +92,9 @@ pub fn sum_2(b: Ptr) -> i32 { let _lhs = (*i.borrow()); _lhs < b.with(|__s| __s.size) } { - (*s.borrow_mut()) += (*b.with(|__s| __s.data.clone()).borrow())[(*i.borrow()) as usize]; + (*s.borrow_mut()) += ((array_field_ptr!(b, data) as Ptr) + .offset((*i.borrow()) as isize) + .read()); (*i.borrow_mut()).prefix_inc(); } return (*s.borrow()); @@ -102,14 +110,24 @@ fn main_0() -> i32 { let b: Value = Rc::new(RefCell::new(Buffer::copy_from({ a.as_pointer() }))); let _dtor_b = ScopedDestructor::new(&b, |__p| __p.destructor()); assert!((alive_0.with(|rc| *rc.borrow()) == 2) && (copies_1.with(|rc| *rc.borrow()) == 1)); - (*{ (*b.borrow()).data.clone() }.borrow_mut())[(0) as usize] = 100; - assert!(((*{ (*a.borrow()).data.clone() }.borrow())[(0) as usize] == 0)); + (array_field_ptr!(b.as_pointer(), data) as Ptr) + .offset((0) as isize) + .write(100); + assert!( + (((array_field_ptr!(a.as_pointer(), data) as Ptr::) + .offset((0) as isize) + .read()) + == 0) + ); let c: Value = Rc::new(RefCell::new(Buffer::new({ 2 }))); let _dtor_c = ScopedDestructor::new(&c, |__p| __p.destructor()); ({ BufferImpl::copy_assign(&c.as_pointer(), a.as_pointer()) }); assert!( ({ (*c.borrow()).size } == 4) - && ((*{ (*c.borrow()).data.clone() }.borrow())[(3) as usize] == 3) + && (((array_field_ptr!(c.as_pointer(), data) as Ptr::) + .offset((3) as isize) + .read()) + == 3) ); assert!((alive_0.with(|rc| *rc.borrow()) == 3) && (copies_1.with(|rc| *rc.borrow()) == 2)); ({ @@ -124,13 +142,22 @@ fn main_0() -> i32 { assert!((alive_0.with(|rc| *rc.borrow()) == 4) && (copies_1.with(|rc| *rc.borrow()) == 3)); assert!( ({ (*a.borrow()).size } == 4) - && ((*{ (*a.borrow()).data.clone() }.borrow())[(3) as usize] == 3) + && (((array_field_ptr!(a.as_pointer(), data) as Ptr::) + .offset((3) as isize) + .read()) + == 3) ); ({ BufferImpl::copy_assign(&d.as_pointer(), b.as_pointer()) }); assert!((copies_1.with(|rc| *rc.borrow()) == 4)); assert!( - ((*{ (*b.borrow()).data.clone() }.borrow())[(0) as usize] == 100) - && ((*{ (*d.borrow()).data.clone() }.borrow())[(0) as usize] == 100) + (((array_field_ptr!(b.as_pointer(), data) as Ptr::) + .offset((0) as isize) + .read()) + == 100) + && (((array_field_ptr!(d.as_pointer(), data) as Ptr::) + .offset((0) as isize) + .read()) + == 100) ); } assert!((alive_0.with(|rc| *rc.borrow()) == 0)); @@ -152,8 +179,12 @@ impl BufferImpl for Ptr { field!((*self), size).write(__rhs); let i: Value = Rc::new(RefCell::new(0)); 'loop_: while ((*i.borrow()) < 4) { - let __rhs = (*o.with(|__s| __s.data.clone()).borrow())[(*i.borrow()) as usize]; - (*(*self).with(|__s| __s.data.clone()).borrow_mut())[(*i.borrow()) as usize] = __rhs; + let __rhs = ((array_field_ptr!(o, data) as Ptr) + .offset((*i.borrow()) as isize) + .read()); + (array_field_ptr!((*self), data) as Ptr) + .offset((*i.borrow()) as isize) + .write(__rhs); (*i.borrow_mut()).prefix_inc(); } (*copies_1.with(Value::clone).borrow_mut()).prefix_inc(); diff --git a/tests/unit/out/refcount/struct_member_array_adjacency.rs b/tests/unit/out/refcount/struct_member_array_adjacency.rs index 84c26fdecf..a0e23d6f66 100644 --- a/tests/unit/out/refcount/struct_member_array_adjacency.rs +++ b/tests/unit/out/refcount/struct_member_array_adjacency.rs @@ -51,8 +51,8 @@ pub fn main() { fn main_0() -> i32 { let s: Value = >::default(); assert!( - (((({ (*s.borrow()).a.clone() }.as_pointer() as Ptr::).offset((4) as isize) - == ({ (*s.borrow()).b.clone() }.as_pointer() as Ptr::)) as i32) + ((((array_field_ptr!(s.as_pointer(), a) as Ptr::).offset((4) as isize) + == (array_field_ptr!(s.as_pointer(), b) as Ptr::)) as i32) != 0) ); return 0; diff --git a/tests/unit/out/refcount/union_cross_arm_cast.rs b/tests/unit/out/refcount/union_cross_arm_cast.rs index 6e8bf3badd..8c7a506c90 100644 --- a/tests/unit/out/refcount/union_cross_arm_cast.rs +++ b/tests/unit/out/refcount/union_cross_arm_cast.rs @@ -186,16 +186,18 @@ fn main_0() -> i32 { assert!((((((*c.borrow()).u.b().with(|__s| __s.code) as i32) == 10) as i32) != 0)); field!((*c.borrow_mut()).u.b(), lo).write(8080_u16); assert!( - ((((((((*c.borrow()).u.raw_().reinterpret_cast::()).reinterpret_cast::()) - .offset((2) as isize) - .read()) as i32) + (((((((((*c.borrow()).u.raw_().reinterpret_cast::()) as Ptr) + .reinterpret_cast::()) + .offset((2) as isize) + .read()) as i32) == 144) as i32) != 0) ); assert!( - ((((((((*c.borrow()).u.raw_().reinterpret_cast::()).reinterpret_cast::()) - .offset((3) as isize) - .read()) as i32) + (((((((((*c.borrow()).u.raw_().reinterpret_cast::()) as Ptr) + .reinterpret_cast::()) + .offset((3) as isize) + .read()) as i32) == 31) as i32) != 0) ); diff --git a/tests/unit/out/refcount/union_nested.rs b/tests/unit/out/refcount/union_nested.rs index 7a6ab636b0..96377d6553 100644 --- a/tests/unit/out/refcount/union_nested.rs +++ b/tests/unit/out/refcount/union_nested.rs @@ -189,14 +189,14 @@ fn main_0() -> i32 { (*ex.borrow_mut()).variant = 6; (*ex.borrow_mut()).len = (16usize as u32); field!((*ex.borrow_mut()).body.h(), code).write(2_u16); - (*(*ex.borrow()) - .body - .h() - .with(|__s| __s.pad.clone()) - .borrow_mut())[(0) as usize] = (('X' as i32) as u8); + (array_field_ptr!((*ex.borrow()).body.h(), pad) as Ptr) + .offset((0) as isize) + .write((('X' as i32) as u8)); assert!((((((*ex.borrow()).body.h().with(|__s| __s.code) as i32) == 2) as i32) != 0)); assert!( - (((((*(*ex.borrow()).body.h().with(|__s| __s.pad.clone()).borrow())[(0) as usize] as i32) + ((((((array_field_ptr!((*ex.borrow()).body.h(), pad) as Ptr::) + .offset((0) as isize) + .read()) as i32) == ('X' as i32)) as i32) != 0) ); diff --git a/tests/unit/out/unsafe/container_field_addr.rs b/tests/unit/out/unsafe/container_field_addr.rs index f96b0a019b..71e48e4179 100644 --- a/tests/unit/out/unsafe/container_field_addr.rs +++ b/tests/unit/out/unsafe/container_field_addr.rs @@ -11,7 +11,6 @@ use std::rc::Rc; pub struct S { pub tag: i32, pub v: Vec, - pub m: BTreeMap>, } pub unsafe fn add_0(mut v: *mut Vec, mut k: i32) { { @@ -19,20 +18,12 @@ pub unsafe fn add_0(mut v: *mut Vec, mut k: i32) { (*v).push(a0_clone) }; } -pub unsafe fn put_1(mut m: *mut BTreeMap>, mut k: i32, mut v: i32) { - (*(*m).entry(k).or_default().as_mut()) = v; -} -pub unsafe fn run_2(mut h: *mut S) { +pub unsafe fn run_1(mut h: *mut S) { (unsafe { let _v: *mut Vec = (&mut (*h).v as *mut Vec); let _k: i32 = (*h).tag; add_0(_v, _k) }); - (unsafe { - let _m: *mut BTreeMap> = (&mut (*h).m as *mut BTreeMap>); - let _k: i32 = (*h).tag; - put_1(_m, _k, 2) - }); let mut pv: *mut Vec = (&mut (*h).v as *mut Vec); { let __a1 = ((*(pv).cast_const()).len() as i32); @@ -42,7 +33,6 @@ pub unsafe fn run_2(mut h: *mut S) { ((((*h).v.len()) == (2_usize)) && (((&mut (*h)).v[(0_usize)]) == (7))) && (((&mut (*h)).v[(1_usize)]) == (1)) ); - assert!(((*(*h).m.entry(7).or_default().as_mut()) == (2))); assert!((((*h).tag) == (7))); } pub fn main() { @@ -54,10 +44,10 @@ pub fn main() { unsafe fn main_0() -> i32 { let mut local: S = ::default(); local.tag = 7; - (unsafe { run_2((&mut local as *mut S)) }); + (unsafe { run_1((&mut local as *mut S)) }); let mut heap: *mut S = (Box::leak(Box::new(::default())) as *mut S); (*heap).tag = 7; - (unsafe { run_2(heap) }); + (unsafe { run_1(heap) }); ::std::mem::drop(Box::from_raw(heap)); return 0; } diff --git a/tests/unit/union_nested.c b/tests/unit/union_nested.c index ca9671397e..7f923854fd 100644 --- a/tests/unit/union_nested.c +++ b/tests/unit/union_nested.c @@ -1,4 +1,3 @@ -// panic: refcount #include #include #include From 2dbda168af682f1bd7ea484d9f7ffa5d527d11a7 Mon Sep 17 00:00:00 2001 From: Nuno Lopes Date: Wed, 30 Sep 2026 17:00:28 +0100 Subject: [PATCH 12/13] fix --- cpp2rust/converter/models/converter_refcount.cpp | 10 +++++----- docs/src/runtime/rc.md | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/cpp2rust/converter/models/converter_refcount.cpp b/cpp2rust/converter/models/converter_refcount.cpp index 2abd477a52..2e93b70bd1 100644 --- a/cpp2rust/converter/models/converter_refcount.cpp +++ b/cpp2rust/converter/models/converter_refcount.cpp @@ -79,9 +79,9 @@ static bool IsValueField(clang::ASTContext &ctx, static bool IsArrayFieldPtr(clang::ASTContext &ctx, const clang::Expr *expr) { auto *member = clang::dyn_cast(expr->IgnoreParenImpCasts()); - auto *field = - member ? clang::dyn_cast(member->getMemberDecl()) - : nullptr; + auto *field = member + ? clang::dyn_cast(member->getMemberDecl()) + : nullptr; if (!field || field->getParent()->isUnion() || Mapper::Contains(ctx, member)) { return false; @@ -1567,8 +1567,8 @@ bool ConverterRefCount::VisitExplicitCastExpr(clang::ExplicitCastExpr *expr) { PushConversionKind push(*this, ConversionKind::Unboxed); StrCat(std::format("({} as Ptr<{}>)", ToString(sub_expr), ToString(element_type))); - if (!ctx_.hasSameUnqualifiedType( - element_type, expr->getType()->getPointeeType())) { + if (!ctx_.hasSameUnqualifiedType(element_type, + expr->getType()->getPointeeType())) { StrCat(std::format(".reinterpret_cast::<{}>()", ConvertPointeeType(expr->getType()))); } diff --git a/docs/src/runtime/rc.md b/docs/src/runtime/rc.md index b02ad929cc..aedb8d9e3b 100644 --- a/docs/src/runtime/rc.md +++ b/docs/src/runtime/rc.md @@ -129,8 +129,8 @@ single object, which is why it needs no element index. `array_field_ptr!(p, arr)` is a pointer to element 0 of the array field `arr`, and is how the elements of an array field are accessed. It is an ordinary array -pointer into the array's `Value`, except when `p` is a reinterpreted pointer: the -array then lives in the bytes of the original allocation, so the result is a +pointer into the array's `Value`, except when `p` is a reinterpreted pointer: +the array then lives in the bytes of the original allocation, so the result is a reinterpreted pointer to those bytes, at the offset of the field. Writes to the elements hence reach the original allocation, and the pointer can go past the end of the struct, as C code does with a trailing `char name[1]` in an From b14648c6622d5b193c414fbea513aa66864ce0ec Mon Sep 17 00:00:00 2001 From: Nuno Lopes Date: Wed, 30 Sep 2026 17:49:15 +0100 Subject: [PATCH 13/13] add another test --- tests/unit/field_reinterpret_write_through.c | 160 ++ .../field_reinterpret_write_through.rs | 1367 +++++++++++++++++ .../unsafe/field_reinterpret_write_through.rs | 1116 ++++++++++++++ 3 files changed, 2643 insertions(+) create mode 100644 tests/unit/field_reinterpret_write_through.c create mode 100644 tests/unit/out/refcount/field_reinterpret_write_through.rs create mode 100644 tests/unit/out/unsafe/field_reinterpret_write_through.rs diff --git a/tests/unit/field_reinterpret_write_through.c b/tests/unit/field_reinterpret_write_through.c new file mode 100644 index 0000000000..fe29b3b233 --- /dev/null +++ b/tests/unit/field_reinterpret_write_through.c @@ -0,0 +1,160 @@ +// panic: refcount +#include +#include +#include +#include + +struct In { + short a; + int b; +}; + +struct S { + int x; + struct In in; + unsigned char bytes[4]; + int arr[3]; + long tail; +}; + +// The value of type T whose bytes are all v. +#define REP(T, v) ((T)(0x0101010101010101ULL * (unsigned char)(v))) + +static void set_bytes(unsigned char *p, size_t n, unsigned char v) { + for (size_t i = 0; i < n; i++) + p[i] = v; +} + +static int all_bytes(const unsigned char *p, size_t n, unsigned char v) { + for (size_t i = 0; i < n; i++) + if (p[i] != v) + return 0; + return 1; +} + +// Checks that field F of type T, at byte OFF of *s, is written through, in +// both directions, +// by the bytes of the field and by the bytes of the whole struct: +// - bytes of the field -> field, and field -> bytes of the field, +// - bytes of the struct -> field, and field -> bytes of the struct. +// Leaves the field with all bytes equal to v + 3. +#define CHECK_FIELD(s, F, T, OFF, v) \ + do { \ + unsigned char *fb = (unsigned char *)&(s)->F; \ + unsigned char *sb = (unsigned char *)(s) + (OFF); \ + set_bytes(fb, sizeof(T), (v)); \ + assert((s)->F == REP(T, (v))); \ + assert(all_bytes(sb, sizeof(T), (v))); \ + (s)->F = REP(T, (v) + 1); \ + assert(all_bytes(fb, sizeof(T), (v) + 1)); \ + assert(all_bytes(sb, sizeof(T), (v) + 1)); \ + set_bytes(sb, sizeof(T), (v) + 2); \ + assert((s)->F == REP(T, (v) + 2)); \ + assert(all_bytes(fb, sizeof(T), (v) + 2)); \ + (s)->F = REP(T, (v) + 3); \ + assert(all_bytes(sb, sizeof(T), (v) + 3)); \ + assert(all_bytes(fb, sizeof(T), (v) + 3)); \ + } while (0) + +// Checks that all fields hold the values CHECK_FIELD left in them, i.e., that +// no write through bytes clobbered a sibling field. +static void check_final(struct S *s) { + assert(s->x == REP(int, 0x13)); + assert(s->in.a == REP(short, 0x23)); + assert(s->in.b == REP(int, 0x33)); + assert(s->bytes[0] == 0x43 && s->bytes[1] == 0x53); + assert(s->bytes[2] == 0x63 && s->bytes[3] == 0x73); + assert(s->arr[0] == REP(int, 0x83) && s->arr[1] == REP(int, 0x93)); + assert(s->arr[2] == REP(int, 0x0b)); + assert(s->tail == REP(long, 0x1b)); +} + +#define IN_OFF offsetof(struct S, in) +#define BYTES_OFF offsetof(struct S, bytes) +#define ARR_OFF offsetof(struct S, arr) + +static void check_struct(struct S *s) { + // Scalar fields, fields of a nested struct, and array elements. + CHECK_FIELD(s, x, int, offsetof(struct S, x), 0x10); + CHECK_FIELD(s, in.a, short, IN_OFF + offsetof(struct In, a), 0x20); + CHECK_FIELD(s, in.b, int, IN_OFF + offsetof(struct In, b), 0x30); + CHECK_FIELD(s, bytes[0], unsigned char, BYTES_OFF + 0, 0x40); + CHECK_FIELD(s, bytes[1], unsigned char, BYTES_OFF + 1, 0x50); + CHECK_FIELD(s, bytes[2], unsigned char, BYTES_OFF + 2, 0x60); + CHECK_FIELD(s, bytes[3], unsigned char, BYTES_OFF + 3, 0x70); + CHECK_FIELD(s, arr[0], int, ARR_OFF + 0 * sizeof(int), 0x80); + CHECK_FIELD(s, arr[1], int, ARR_OFF + 1 * sizeof(int), 0x90); + CHECK_FIELD(s, arr[2], int, ARR_OFF + 2 * sizeof(int), 0x08); + CHECK_FIELD(s, tail, long, offsetof(struct S, tail), 0x18); + check_final(s); + + // A nested struct as a whole, through its bytes. + set_bytes((unsigned char *)&s->in, sizeof(struct In), 0x21); + assert(s->in.a == REP(short, 0x21) && s->in.b == REP(int, 0x21)); + struct In in = {REP(short, 0x23), REP(int, 0x33)}; + s->in = in; + assert(all_bytes((unsigned char *)&s->in.a, sizeof(short), 0x23)); + assert(all_bytes((unsigned char *)&s->in.b, sizeof(int), 0x33)); + + // Array fields as a whole, through their bytes and through a different + // element type. + set_bytes((unsigned char *)s->arr, sizeof(s->arr), 0x85); + assert(s->arr[0] == REP(int, 0x85) && s->arr[2] == REP(int, 0x85)); + set_bytes((unsigned char *)&s->arr, sizeof(s->arr), 0x86); + assert(s->arr[1] == REP(int, 0x86)); + s->arr[0] = REP(int, 0x83); + s->arr[1] = REP(int, 0x93); + s->arr[2] = REP(int, 0x0b); + int *ib = (int *)s->bytes; + *ib = REP(int, 0x45); + assert(all_bytes(s->bytes, sizeof(s->bytes), 0x45)); + s->bytes[0] = 0x43; + s->bytes[1] = 0x53; + s->bytes[2] = 0x63; + s->bytes[3] = 0x73; + assert(*ib == *(int *)(unsigned char *)&s->bytes); + + // Through void *, memcpy and memset. + void *v = &s->in.b; + *(int *)v = 11; + assert(s->in.b == 11); + int twelve = 12; + memcpy(&s->x, &twelve, sizeof(int)); + assert(s->x == 12); + memset(&s->in.b, 0x33, sizeof(int)); + memset(&s->x, 0x13, sizeof(int)); + check_final(s); +} + +int main(void) { + // A struct on the stack. + struct S local; + memset(&local, 0, sizeof(local)); + check_struct(&local); + + // A struct in an array. + struct S arr[2]; + memset(arr, 0, sizeof(arr)); + check_struct(&arr[1]); + assert(all_bytes((unsigned char *)&arr[0], sizeof(struct S), 0)); + + // A struct in malloc'ed memory, i.e., a view of a byte buffer. + struct S *heap = calloc(1, sizeof(struct S)); + assert(heap != NULL); + check_struct(heap); + free(heap); + + // A struct in a byte buffer that is written through the buffer. + unsigned char *raw = malloc(sizeof(struct S)); + assert(raw != NULL); + memset(raw, 0, sizeof(struct S)); + struct S *view = (struct S *)raw; + check_struct(view); + assert(raw[offsetof(struct S, tail)] == 0x1b); + set_bytes(raw + offsetof(struct S, arr), sizeof(int), 0x77); + assert(view->arr[0] == REP(int, 0x77)); + view->bytes[3] = 0x11; + assert(raw[offsetof(struct S, bytes) + 3] == 0x11); + free(raw); + return 0; +} diff --git a/tests/unit/out/refcount/field_reinterpret_write_through.rs b/tests/unit/out/refcount/field_reinterpret_write_through.rs new file mode 100644 index 0000000000..0d9ee0ad85 --- /dev/null +++ b/tests/unit/out/refcount/field_reinterpret_write_through.rs @@ -0,0 +1,1367 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +#[derive(Clone, Record, VaArg, FnPtrArg, Default)] +pub struct In { + #[offset(0)] + pub a: i16, + #[offset(4)] + pub b: i32, +} +impl ByteRepr for In { + fn byte_size() -> usize { + 8 + } + fn to_bytes(&self, buf: &mut [u8]) { + self.a.to_bytes(&mut buf[0..2]); + self.b.to_bytes(&mut buf[4..8]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + a: ::from_bytes(&buf[0..2]), + b: ::from_bytes(&buf[4..8]), + } + } +} +#[derive(Record, VaArg, FnPtrArg)] +pub struct S { + #[offset(0)] + pub x: i32, + #[offset(4)] + pub in_: In, + #[offset(12)] + pub bytes: Value>, + #[offset(16)] + pub arr: Value>, + #[offset(32)] + pub tail: i64, +} +impl Clone for S { + fn clone(&self) -> Self { + Self { + x: self.x.clone(), + in_: self.in_.clone(), + bytes: Rc::new(RefCell::new((*self.bytes.borrow()).clone())), + arr: Rc::new(RefCell::new((*self.arr.borrow()).clone())), + tail: self.tail.clone(), + } + } +} +impl Default for S { + fn default() -> Self { + S { + x: 0_i32, + in_: ::default(), + bytes: Rc::new(RefCell::new((0..4).map(|_| 0_u8).collect::>())), + arr: Rc::new(RefCell::new((0..3).map(|_| 0_i32).collect::>())), + tail: 0_i64, + } + } +} +impl ByteRepr for S { + fn byte_size() -> usize { + 40 + } + fn to_bytes(&self, buf: &mut [u8]) { + self.x.to_bytes(&mut buf[0..4]); + self.in_.to_bytes(&mut buf[4..12]); + (*self.bytes.borrow()).to_bytes(&mut buf[12..16]); + (*self.arr.borrow()).to_bytes(&mut buf[16..28]); + self.tail.to_bytes(&mut buf[32..40]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + x: ::from_bytes(&buf[0..4]), + in_: ::from_bytes(&buf[4..12]), + bytes: Rc::new(RefCell::new(>::from_bytes(&buf[12..16]))), + arr: Rc::new(RefCell::new(>::from_bytes(&buf[16..28]))), + tail: ::from_bytes(&buf[32..40]), + } + } +} +pub fn set_bytes_0(p: Ptr, n: usize, v: u8) { + let p: Value> = Rc::new(RefCell::new(p)); + let n: Value = Rc::new(RefCell::new(n)); + let v: Value = Rc::new(RefCell::new(v)); + let i: Value = Rc::new(RefCell::new(0_usize)); + 'loop_: while ((((*i.borrow()) < (*n.borrow())) as i32) != 0) { + let __rhs = (*v.borrow()); + (*p.borrow()).offset((*i.borrow()) as isize).write(__rhs); + (*i.borrow_mut()).postfix_inc(); + } +} +pub fn all_bytes_1(p: Ptr, n: usize, v: u8) -> i32 { + let p: Value> = Rc::new(RefCell::new(p)); + let n: Value = Rc::new(RefCell::new(n)); + let v: Value = Rc::new(RefCell::new(v)); + let i: Value = Rc::new(RefCell::new(0_usize)); + 'loop_: while ((((*i.borrow()) < (*n.borrow())) as i32) != 0) { + if ((({ + let _lhs = (((*p.borrow()).offset((*i.borrow()) as isize).read()) as i32); + _lhs != ((*v.borrow()) as i32) + }) as i32) + != 0) + { + return 0; + } + (*i.borrow_mut()).postfix_inc(); + } + return 1; +} +pub fn check_final_2(s: Ptr) { + let s: Value> = Rc::new(RefCell::new(s)); + assert!( + ((((*s.borrow()).with(|__s| __s.x) + == (((72340172838076673_u64 as u64).wrapping_mul((((19) as u8) as u64))) as i32)) + as i32) + != 0) + ); + assert!( + (((((*s.borrow()).with(|__s| __s.in_.a) as i32) + == ((((72340172838076673_u64 as u64).wrapping_mul((((35) as u8) as u64))) as i16) + as i32)) as i32) + != 0) + ); + assert!( + ((((*s.borrow()).with(|__s| __s.in_.b) + == (((72340172838076673_u64 as u64).wrapping_mul((((51) as u8) as u64))) as i32)) + as i32) + != 0) + ); + assert!( + (((((((((array_field_ptr!((*s.borrow()), bytes) as Ptr::) + .offset((0) as isize) + .read()) as i32) + == 67) as i32) + != 0) + && ((((((array_field_ptr!((*s.borrow()), bytes) as Ptr::) + .offset((1) as isize) + .read()) as i32) + == 83) as i32) + != 0)) as i32) + != 0) + ); + assert!( + (((((((((array_field_ptr!((*s.borrow()), bytes) as Ptr::) + .offset((2) as isize) + .read()) as i32) + == 99) as i32) + != 0) + && ((((((array_field_ptr!((*s.borrow()), bytes) as Ptr::) + .offset((3) as isize) + .read()) as i32) + == 115) as i32) + != 0)) as i32) + != 0) + ); + assert!( + ((((((((array_field_ptr!((*s.borrow()), arr) as Ptr::) + .offset((0) as isize) + .read()) + == (((72340172838076673_u64 as u64).wrapping_mul((((131) as u8) as u64))) as i32)) + as i32) + != 0) + && (((((array_field_ptr!((*s.borrow()), arr) as Ptr::) + .offset((1) as isize) + .read()) + == (((72340172838076673_u64 as u64).wrapping_mul((((147) as u8) as u64))) as i32)) + as i32) + != 0)) as i32) + != 0) + ); + assert!( + (((((array_field_ptr!((*s.borrow()), arr) as Ptr::) + .offset((2) as isize) + .read()) + == (((72340172838076673_u64 as u64).wrapping_mul((((11) as u8) as u64))) as i32)) + as i32) + != 0) + ); + assert!( + ((((*s.borrow()).with(|__s| __s.tail) + == (((72340172838076673_u64 as u64).wrapping_mul((((27) as u8) as u64))) as i64)) + as i32) + != 0) + ); +} +pub fn check_struct_3(s: Ptr) { + let s: Value> = Rc::new(RefCell::new(s)); + let mut __do_while = true; + 'loop_: while __do_while || (0 != 0) { + __do_while = false; + let fb: Value> = Rc::new(RefCell::new( + (field_ptr!((*s.borrow()), x)).reinterpret_cast::(), + )); + let sb: Value> = Rc::new(RefCell::new( + (*s.borrow()) + .reinterpret_cast::() + .offset((0_usize) as isize), + )); + ({ + let _p: Ptr = (*fb.borrow()).clone(); + set_bytes_0(_p, ::std::mem::size_of::(), ((16) as u8)) + }); + assert!( + ((((*s.borrow()).with(|__s| __s.x) + == (((72340172838076673_u64 as u64).wrapping_mul((((16) as u8) as u64))) as i32)) + as i32) + != 0) + ); + assert!( + (({ + let _p: Ptr = (*sb.borrow()).clone(); + all_bytes_1(_p, ::std::mem::size_of::(), ((16) as u8)) + }) != 0) + ); + field!((*s.borrow()), x).write( + (((72340172838076673_u64 as u64).wrapping_mul(((((16) + 1) as u8) as u64))) as i32), + ); + assert!( + (({ + let _p: Ptr = (*fb.borrow()).clone(); + let _v: u8 = (((16) + 1) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + assert!( + (({ + let _p: Ptr = (*sb.borrow()).clone(); + let _v: u8 = (((16) + 1) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + ({ + let _p: Ptr = (*sb.borrow()).clone(); + let _v: u8 = (((16) + 2) as u8); + set_bytes_0(_p, ::std::mem::size_of::(), _v) + }); + assert!( + ((((*s.borrow()).with(|__s| __s.x) + == (((72340172838076673_u64 as u64).wrapping_mul(((((16) + 2) as u8) as u64))) + as i32)) as i32) + != 0) + ); + assert!( + (({ + let _p: Ptr = (*fb.borrow()).clone(); + let _v: u8 = (((16) + 2) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + field!((*s.borrow()), x).write( + (((72340172838076673_u64 as u64).wrapping_mul(((((16) + 3) as u8) as u64))) as i32), + ); + assert!( + (({ + let _p: Ptr = (*sb.borrow()).clone(); + let _v: u8 = (((16) + 3) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + assert!( + (({ + let _p: Ptr = (*fb.borrow()).clone(); + let _v: u8 = (((16) + 3) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + } + let mut __do_while = true; + 'loop_: while __do_while || (0 != 0) { + __do_while = false; + let fb: Value> = Rc::new(RefCell::new( + (field_ptr!(field_ptr!((*s.borrow()), in_), a)).reinterpret_cast::(), + )); + let sb: Value> = + Rc::new(RefCell::new((*s.borrow()).reinterpret_cast::().offset( + ((4_usize as usize).wrapping_add((0_usize as usize))) as isize, + ))); + ({ + let _p: Ptr = (*fb.borrow()).clone(); + set_bytes_0(_p, ::std::mem::size_of::(), ((32) as u8)) + }); + assert!( + (((((*s.borrow()).with(|__s| __s.in_.a) as i32) + == ((((72340172838076673_u64 as u64).wrapping_mul((((32) as u8) as u64))) as i16) + as i32)) as i32) + != 0) + ); + assert!( + (({ + let _p: Ptr = (*sb.borrow()).clone(); + all_bytes_1(_p, ::std::mem::size_of::(), ((32) as u8)) + }) != 0) + ); + field!(field!((*s.borrow()), in_), a).write( + (((72340172838076673_u64 as u64).wrapping_mul(((((32) + 1) as u8) as u64))) as i16), + ); + assert!( + (({ + let _p: Ptr = (*fb.borrow()).clone(); + let _v: u8 = (((32) + 1) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + assert!( + (({ + let _p: Ptr = (*sb.borrow()).clone(); + let _v: u8 = (((32) + 1) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + ({ + let _p: Ptr = (*sb.borrow()).clone(); + let _v: u8 = (((32) + 2) as u8); + set_bytes_0(_p, ::std::mem::size_of::(), _v) + }); + assert!( + (((((*s.borrow()).with(|__s| __s.in_.a) as i32) + == ((((72340172838076673_u64 as u64).wrapping_mul(((((32) + 2) as u8) as u64))) + as i16) as i32)) as i32) + != 0) + ); + assert!( + (({ + let _p: Ptr = (*fb.borrow()).clone(); + let _v: u8 = (((32) + 2) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + field!(field!((*s.borrow()), in_), a).write( + (((72340172838076673_u64 as u64).wrapping_mul(((((32) + 3) as u8) as u64))) as i16), + ); + assert!( + (({ + let _p: Ptr = (*sb.borrow()).clone(); + let _v: u8 = (((32) + 3) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + assert!( + (({ + let _p: Ptr = (*fb.borrow()).clone(); + let _v: u8 = (((32) + 3) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + } + let mut __do_while = true; + 'loop_: while __do_while || (0 != 0) { + __do_while = false; + let fb: Value> = Rc::new(RefCell::new( + (field_ptr!(field_ptr!((*s.borrow()), in_), b)).reinterpret_cast::(), + )); + let sb: Value> = + Rc::new(RefCell::new((*s.borrow()).reinterpret_cast::().offset( + ((4_usize as usize).wrapping_add((4_usize as usize))) as isize, + ))); + ({ + let _p: Ptr = (*fb.borrow()).clone(); + set_bytes_0(_p, ::std::mem::size_of::(), ((48) as u8)) + }); + assert!( + ((((*s.borrow()).with(|__s| __s.in_.b) + == (((72340172838076673_u64 as u64).wrapping_mul((((48) as u8) as u64))) as i32)) + as i32) + != 0) + ); + assert!( + (({ + let _p: Ptr = (*sb.borrow()).clone(); + all_bytes_1(_p, ::std::mem::size_of::(), ((48) as u8)) + }) != 0) + ); + field!(field!((*s.borrow()), in_), b).write( + (((72340172838076673_u64 as u64).wrapping_mul(((((48) + 1) as u8) as u64))) as i32), + ); + assert!( + (({ + let _p: Ptr = (*fb.borrow()).clone(); + let _v: u8 = (((48) + 1) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + assert!( + (({ + let _p: Ptr = (*sb.borrow()).clone(); + let _v: u8 = (((48) + 1) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + ({ + let _p: Ptr = (*sb.borrow()).clone(); + let _v: u8 = (((48) + 2) as u8); + set_bytes_0(_p, ::std::mem::size_of::(), _v) + }); + assert!( + ((((*s.borrow()).with(|__s| __s.in_.b) + == (((72340172838076673_u64 as u64).wrapping_mul(((((48) + 2) as u8) as u64))) + as i32)) as i32) + != 0) + ); + assert!( + (({ + let _p: Ptr = (*fb.borrow()).clone(); + let _v: u8 = (((48) + 2) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + field!(field!((*s.borrow()), in_), b).write( + (((72340172838076673_u64 as u64).wrapping_mul(((((48) + 3) as u8) as u64))) as i32), + ); + assert!( + (({ + let _p: Ptr = (*sb.borrow()).clone(); + let _v: u8 = (((48) + 3) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + assert!( + (({ + let _p: Ptr = (*fb.borrow()).clone(); + let _v: u8 = (((48) + 3) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + } + let mut __do_while = true; + 'loop_: while __do_while || (0 != 0) { + __do_while = false; + let fb: Value> = Rc::new(RefCell::new( + ((array_field_ptr!((*s.borrow()), bytes) as Ptr).offset((0) as isize)) + .reinterpret_cast::(), + )); + let sb: Value> = Rc::new(RefCell::new( + (*s.borrow()) + .reinterpret_cast::() + .offset(((12_usize as usize).wrapping_add(0_usize)) as isize), + )); + ({ + let _p: Ptr = (*fb.borrow()).clone(); + set_bytes_0(_p, ::std::mem::size_of::(), ((64) as u8)) + }); + assert!( + ((((((array_field_ptr!((*s.borrow()), bytes) as Ptr::) + .offset((0) as isize) + .read()) as i32) + == ((((72340172838076673_u64 as u64).wrapping_mul((((64) as u8) as u64))) as u8) + as i32)) as i32) + != 0) + ); + assert!( + (({ + let _p: Ptr = (*sb.borrow()).clone(); + all_bytes_1(_p, ::std::mem::size_of::(), ((64) as u8)) + }) != 0) + ); + (array_field_ptr!((*s.borrow()), bytes) as Ptr) + .offset((0) as isize) + .write( + (((72340172838076673_u64 as u64).wrapping_mul(((((64) + 1) as u8) as u64))) as u8), + ); + assert!( + (({ + let _p: Ptr = (*fb.borrow()).clone(); + let _v: u8 = (((64) + 1) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + assert!( + (({ + let _p: Ptr = (*sb.borrow()).clone(); + let _v: u8 = (((64) + 1) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + ({ + let _p: Ptr = (*sb.borrow()).clone(); + let _v: u8 = (((64) + 2) as u8); + set_bytes_0(_p, ::std::mem::size_of::(), _v) + }); + assert!( + ((((((array_field_ptr!((*s.borrow()), bytes) as Ptr::) + .offset((0) as isize) + .read()) as i32) + == ((((72340172838076673_u64 as u64).wrapping_mul(((((64) + 2) as u8) as u64))) + as u8) as i32)) as i32) + != 0) + ); + assert!( + (({ + let _p: Ptr = (*fb.borrow()).clone(); + let _v: u8 = (((64) + 2) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + (array_field_ptr!((*s.borrow()), bytes) as Ptr) + .offset((0) as isize) + .write( + (((72340172838076673_u64 as u64).wrapping_mul(((((64) + 3) as u8) as u64))) as u8), + ); + assert!( + (({ + let _p: Ptr = (*sb.borrow()).clone(); + let _v: u8 = (((64) + 3) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + assert!( + (({ + let _p: Ptr = (*fb.borrow()).clone(); + let _v: u8 = (((64) + 3) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + } + let mut __do_while = true; + 'loop_: while __do_while || (0 != 0) { + __do_while = false; + let fb: Value> = Rc::new(RefCell::new( + ((array_field_ptr!((*s.borrow()), bytes) as Ptr).offset((1) as isize)) + .reinterpret_cast::(), + )); + let sb: Value> = Rc::new(RefCell::new( + (*s.borrow()) + .reinterpret_cast::() + .offset(((12_usize as usize).wrapping_add(1_usize)) as isize), + )); + ({ + let _p: Ptr = (*fb.borrow()).clone(); + set_bytes_0(_p, ::std::mem::size_of::(), ((80) as u8)) + }); + assert!( + ((((((array_field_ptr!((*s.borrow()), bytes) as Ptr::) + .offset((1) as isize) + .read()) as i32) + == ((((72340172838076673_u64 as u64).wrapping_mul((((80) as u8) as u64))) as u8) + as i32)) as i32) + != 0) + ); + assert!( + (({ + let _p: Ptr = (*sb.borrow()).clone(); + all_bytes_1(_p, ::std::mem::size_of::(), ((80) as u8)) + }) != 0) + ); + (array_field_ptr!((*s.borrow()), bytes) as Ptr) + .offset((1) as isize) + .write( + (((72340172838076673_u64 as u64).wrapping_mul(((((80) + 1) as u8) as u64))) as u8), + ); + assert!( + (({ + let _p: Ptr = (*fb.borrow()).clone(); + let _v: u8 = (((80) + 1) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + assert!( + (({ + let _p: Ptr = (*sb.borrow()).clone(); + let _v: u8 = (((80) + 1) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + ({ + let _p: Ptr = (*sb.borrow()).clone(); + let _v: u8 = (((80) + 2) as u8); + set_bytes_0(_p, ::std::mem::size_of::(), _v) + }); + assert!( + ((((((array_field_ptr!((*s.borrow()), bytes) as Ptr::) + .offset((1) as isize) + .read()) as i32) + == ((((72340172838076673_u64 as u64).wrapping_mul(((((80) + 2) as u8) as u64))) + as u8) as i32)) as i32) + != 0) + ); + assert!( + (({ + let _p: Ptr = (*fb.borrow()).clone(); + let _v: u8 = (((80) + 2) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + (array_field_ptr!((*s.borrow()), bytes) as Ptr) + .offset((1) as isize) + .write( + (((72340172838076673_u64 as u64).wrapping_mul(((((80) + 3) as u8) as u64))) as u8), + ); + assert!( + (({ + let _p: Ptr = (*sb.borrow()).clone(); + let _v: u8 = (((80) + 3) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + assert!( + (({ + let _p: Ptr = (*fb.borrow()).clone(); + let _v: u8 = (((80) + 3) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + } + let mut __do_while = true; + 'loop_: while __do_while || (0 != 0) { + __do_while = false; + let fb: Value> = Rc::new(RefCell::new( + ((array_field_ptr!((*s.borrow()), bytes) as Ptr).offset((2) as isize)) + .reinterpret_cast::(), + )); + let sb: Value> = Rc::new(RefCell::new( + (*s.borrow()) + .reinterpret_cast::() + .offset(((12_usize as usize).wrapping_add(2_usize)) as isize), + )); + ({ + let _p: Ptr = (*fb.borrow()).clone(); + set_bytes_0(_p, ::std::mem::size_of::(), ((96) as u8)) + }); + assert!( + ((((((array_field_ptr!((*s.borrow()), bytes) as Ptr::) + .offset((2) as isize) + .read()) as i32) + == ((((72340172838076673_u64 as u64).wrapping_mul((((96) as u8) as u64))) as u8) + as i32)) as i32) + != 0) + ); + assert!( + (({ + let _p: Ptr = (*sb.borrow()).clone(); + all_bytes_1(_p, ::std::mem::size_of::(), ((96) as u8)) + }) != 0) + ); + (array_field_ptr!((*s.borrow()), bytes) as Ptr) + .offset((2) as isize) + .write( + (((72340172838076673_u64 as u64).wrapping_mul(((((96) + 1) as u8) as u64))) as u8), + ); + assert!( + (({ + let _p: Ptr = (*fb.borrow()).clone(); + let _v: u8 = (((96) + 1) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + assert!( + (({ + let _p: Ptr = (*sb.borrow()).clone(); + let _v: u8 = (((96) + 1) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + ({ + let _p: Ptr = (*sb.borrow()).clone(); + let _v: u8 = (((96) + 2) as u8); + set_bytes_0(_p, ::std::mem::size_of::(), _v) + }); + assert!( + ((((((array_field_ptr!((*s.borrow()), bytes) as Ptr::) + .offset((2) as isize) + .read()) as i32) + == ((((72340172838076673_u64 as u64).wrapping_mul(((((96) + 2) as u8) as u64))) + as u8) as i32)) as i32) + != 0) + ); + assert!( + (({ + let _p: Ptr = (*fb.borrow()).clone(); + let _v: u8 = (((96) + 2) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + (array_field_ptr!((*s.borrow()), bytes) as Ptr) + .offset((2) as isize) + .write( + (((72340172838076673_u64 as u64).wrapping_mul(((((96) + 3) as u8) as u64))) as u8), + ); + assert!( + (({ + let _p: Ptr = (*sb.borrow()).clone(); + let _v: u8 = (((96) + 3) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + assert!( + (({ + let _p: Ptr = (*fb.borrow()).clone(); + let _v: u8 = (((96) + 3) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + } + let mut __do_while = true; + 'loop_: while __do_while || (0 != 0) { + __do_while = false; + let fb: Value> = Rc::new(RefCell::new( + ((array_field_ptr!((*s.borrow()), bytes) as Ptr).offset((3) as isize)) + .reinterpret_cast::(), + )); + let sb: Value> = Rc::new(RefCell::new( + (*s.borrow()) + .reinterpret_cast::() + .offset(((12_usize as usize).wrapping_add(3_usize)) as isize), + )); + ({ + let _p: Ptr = (*fb.borrow()).clone(); + set_bytes_0(_p, ::std::mem::size_of::(), ((112) as u8)) + }); + assert!( + ((((((array_field_ptr!((*s.borrow()), bytes) as Ptr::) + .offset((3) as isize) + .read()) as i32) + == ((((72340172838076673_u64 as u64).wrapping_mul((((112) as u8) as u64))) as u8) + as i32)) as i32) + != 0) + ); + assert!( + (({ + let _p: Ptr = (*sb.borrow()).clone(); + all_bytes_1(_p, ::std::mem::size_of::(), ((112) as u8)) + }) != 0) + ); + (array_field_ptr!((*s.borrow()), bytes) as Ptr) + .offset((3) as isize) + .write( + (((72340172838076673_u64 as u64).wrapping_mul(((((112) + 1) as u8) as u64))) as u8), + ); + assert!( + (({ + let _p: Ptr = (*fb.borrow()).clone(); + let _v: u8 = (((112) + 1) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + assert!( + (({ + let _p: Ptr = (*sb.borrow()).clone(); + let _v: u8 = (((112) + 1) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + ({ + let _p: Ptr = (*sb.borrow()).clone(); + let _v: u8 = (((112) + 2) as u8); + set_bytes_0(_p, ::std::mem::size_of::(), _v) + }); + assert!( + ((((((array_field_ptr!((*s.borrow()), bytes) as Ptr::) + .offset((3) as isize) + .read()) as i32) + == ((((72340172838076673_u64 as u64).wrapping_mul(((((112) + 2) as u8) as u64))) + as u8) as i32)) as i32) + != 0) + ); + assert!( + (({ + let _p: Ptr = (*fb.borrow()).clone(); + let _v: u8 = (((112) + 2) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + (array_field_ptr!((*s.borrow()), bytes) as Ptr) + .offset((3) as isize) + .write( + (((72340172838076673_u64 as u64).wrapping_mul(((((112) + 3) as u8) as u64))) as u8), + ); + assert!( + (({ + let _p: Ptr = (*sb.borrow()).clone(); + let _v: u8 = (((112) + 3) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + assert!( + (({ + let _p: Ptr = (*fb.borrow()).clone(); + let _v: u8 = (((112) + 3) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + } + let mut __do_while = true; + 'loop_: while __do_while || (0 != 0) { + __do_while = false; + let fb: Value> = Rc::new(RefCell::new( + ((array_field_ptr!((*s.borrow()), arr) as Ptr).offset((0) as isize)) + .reinterpret_cast::(), + )); + let sb: Value> = + Rc::new(RefCell::new((*s.borrow()).reinterpret_cast::().offset( + ((16_usize as usize).wrapping_add( + ((0_usize).wrapping_mul((::std::mem::size_of::() as usize)) as usize), + )) as isize, + ))); + ({ + let _p: Ptr = (*fb.borrow()).clone(); + set_bytes_0(_p, ::std::mem::size_of::(), ((128) as u8)) + }); + assert!( + (((((array_field_ptr!((*s.borrow()), arr) as Ptr::) + .offset((0) as isize) + .read()) + == (((72340172838076673_u64 as u64).wrapping_mul((((128) as u8) as u64))) as i32)) + as i32) + != 0) + ); + assert!( + (({ + let _p: Ptr = (*sb.borrow()).clone(); + all_bytes_1(_p, ::std::mem::size_of::(), ((128) as u8)) + }) != 0) + ); + (array_field_ptr!((*s.borrow()), arr) as Ptr) + .offset((0) as isize) + .write( + (((72340172838076673_u64 as u64).wrapping_mul(((((128) + 1) as u8) as u64))) + as i32), + ); + assert!( + (({ + let _p: Ptr = (*fb.borrow()).clone(); + let _v: u8 = (((128) + 1) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + assert!( + (({ + let _p: Ptr = (*sb.borrow()).clone(); + let _v: u8 = (((128) + 1) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + ({ + let _p: Ptr = (*sb.borrow()).clone(); + let _v: u8 = (((128) + 2) as u8); + set_bytes_0(_p, ::std::mem::size_of::(), _v) + }); + assert!( + (((((array_field_ptr!((*s.borrow()), arr) as Ptr::) + .offset((0) as isize) + .read()) + == (((72340172838076673_u64 as u64).wrapping_mul(((((128) + 2) as u8) as u64))) + as i32)) as i32) + != 0) + ); + assert!( + (({ + let _p: Ptr = (*fb.borrow()).clone(); + let _v: u8 = (((128) + 2) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + (array_field_ptr!((*s.borrow()), arr) as Ptr) + .offset((0) as isize) + .write( + (((72340172838076673_u64 as u64).wrapping_mul(((((128) + 3) as u8) as u64))) + as i32), + ); + assert!( + (({ + let _p: Ptr = (*sb.borrow()).clone(); + let _v: u8 = (((128) + 3) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + assert!( + (({ + let _p: Ptr = (*fb.borrow()).clone(); + let _v: u8 = (((128) + 3) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + } + let mut __do_while = true; + 'loop_: while __do_while || (0 != 0) { + __do_while = false; + let fb: Value> = Rc::new(RefCell::new( + ((array_field_ptr!((*s.borrow()), arr) as Ptr).offset((1) as isize)) + .reinterpret_cast::(), + )); + let sb: Value> = + Rc::new(RefCell::new((*s.borrow()).reinterpret_cast::().offset( + ((16_usize as usize).wrapping_add( + ((1_usize).wrapping_mul((::std::mem::size_of::() as usize)) as usize), + )) as isize, + ))); + ({ + let _p: Ptr = (*fb.borrow()).clone(); + set_bytes_0(_p, ::std::mem::size_of::(), ((144) as u8)) + }); + assert!( + (((((array_field_ptr!((*s.borrow()), arr) as Ptr::) + .offset((1) as isize) + .read()) + == (((72340172838076673_u64 as u64).wrapping_mul((((144) as u8) as u64))) as i32)) + as i32) + != 0) + ); + assert!( + (({ + let _p: Ptr = (*sb.borrow()).clone(); + all_bytes_1(_p, ::std::mem::size_of::(), ((144) as u8)) + }) != 0) + ); + (array_field_ptr!((*s.borrow()), arr) as Ptr) + .offset((1) as isize) + .write( + (((72340172838076673_u64 as u64).wrapping_mul(((((144) + 1) as u8) as u64))) + as i32), + ); + assert!( + (({ + let _p: Ptr = (*fb.borrow()).clone(); + let _v: u8 = (((144) + 1) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + assert!( + (({ + let _p: Ptr = (*sb.borrow()).clone(); + let _v: u8 = (((144) + 1) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + ({ + let _p: Ptr = (*sb.borrow()).clone(); + let _v: u8 = (((144) + 2) as u8); + set_bytes_0(_p, ::std::mem::size_of::(), _v) + }); + assert!( + (((((array_field_ptr!((*s.borrow()), arr) as Ptr::) + .offset((1) as isize) + .read()) + == (((72340172838076673_u64 as u64).wrapping_mul(((((144) + 2) as u8) as u64))) + as i32)) as i32) + != 0) + ); + assert!( + (({ + let _p: Ptr = (*fb.borrow()).clone(); + let _v: u8 = (((144) + 2) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + (array_field_ptr!((*s.borrow()), arr) as Ptr) + .offset((1) as isize) + .write( + (((72340172838076673_u64 as u64).wrapping_mul(((((144) + 3) as u8) as u64))) + as i32), + ); + assert!( + (({ + let _p: Ptr = (*sb.borrow()).clone(); + let _v: u8 = (((144) + 3) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + assert!( + (({ + let _p: Ptr = (*fb.borrow()).clone(); + let _v: u8 = (((144) + 3) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + } + let mut __do_while = true; + 'loop_: while __do_while || (0 != 0) { + __do_while = false; + let fb: Value> = Rc::new(RefCell::new( + ((array_field_ptr!((*s.borrow()), arr) as Ptr).offset((2) as isize)) + .reinterpret_cast::(), + )); + let sb: Value> = + Rc::new(RefCell::new((*s.borrow()).reinterpret_cast::().offset( + ((16_usize as usize).wrapping_add( + ((2_usize).wrapping_mul((::std::mem::size_of::() as usize)) as usize), + )) as isize, + ))); + ({ + let _p: Ptr = (*fb.borrow()).clone(); + set_bytes_0(_p, ::std::mem::size_of::(), ((8) as u8)) + }); + assert!( + (((((array_field_ptr!((*s.borrow()), arr) as Ptr::) + .offset((2) as isize) + .read()) + == (((72340172838076673_u64 as u64).wrapping_mul((((8) as u8) as u64))) as i32)) + as i32) + != 0) + ); + assert!( + (({ + let _p: Ptr = (*sb.borrow()).clone(); + all_bytes_1(_p, ::std::mem::size_of::(), ((8) as u8)) + }) != 0) + ); + (array_field_ptr!((*s.borrow()), arr) as Ptr) + .offset((2) as isize) + .write( + (((72340172838076673_u64 as u64).wrapping_mul(((((8) + 1) as u8) as u64))) as i32), + ); + assert!( + (({ + let _p: Ptr = (*fb.borrow()).clone(); + let _v: u8 = (((8) + 1) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + assert!( + (({ + let _p: Ptr = (*sb.borrow()).clone(); + let _v: u8 = (((8) + 1) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + ({ + let _p: Ptr = (*sb.borrow()).clone(); + let _v: u8 = (((8) + 2) as u8); + set_bytes_0(_p, ::std::mem::size_of::(), _v) + }); + assert!( + (((((array_field_ptr!((*s.borrow()), arr) as Ptr::) + .offset((2) as isize) + .read()) + == (((72340172838076673_u64 as u64).wrapping_mul(((((8) + 2) as u8) as u64))) + as i32)) as i32) + != 0) + ); + assert!( + (({ + let _p: Ptr = (*fb.borrow()).clone(); + let _v: u8 = (((8) + 2) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + (array_field_ptr!((*s.borrow()), arr) as Ptr) + .offset((2) as isize) + .write( + (((72340172838076673_u64 as u64).wrapping_mul(((((8) + 3) as u8) as u64))) as i32), + ); + assert!( + (({ + let _p: Ptr = (*sb.borrow()).clone(); + let _v: u8 = (((8) + 3) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + assert!( + (({ + let _p: Ptr = (*fb.borrow()).clone(); + let _v: u8 = (((8) + 3) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + } + let mut __do_while = true; + 'loop_: while __do_while || (0 != 0) { + __do_while = false; + let fb: Value> = Rc::new(RefCell::new( + (field_ptr!((*s.borrow()), tail)).reinterpret_cast::(), + )); + let sb: Value> = Rc::new(RefCell::new( + (*s.borrow()) + .reinterpret_cast::() + .offset((32_usize) as isize), + )); + ({ + let _p: Ptr = (*fb.borrow()).clone(); + set_bytes_0(_p, ::std::mem::size_of::(), ((24) as u8)) + }); + assert!( + ((((*s.borrow()).with(|__s| __s.tail) + == (((72340172838076673_u64 as u64).wrapping_mul((((24) as u8) as u64))) as i64)) + as i32) + != 0) + ); + assert!( + (({ + let _p: Ptr = (*sb.borrow()).clone(); + all_bytes_1(_p, ::std::mem::size_of::(), ((24) as u8)) + }) != 0) + ); + field!((*s.borrow()), tail).write( + (((72340172838076673_u64 as u64).wrapping_mul(((((24) + 1) as u8) as u64))) as i64), + ); + assert!( + (({ + let _p: Ptr = (*fb.borrow()).clone(); + let _v: u8 = (((24) + 1) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + assert!( + (({ + let _p: Ptr = (*sb.borrow()).clone(); + let _v: u8 = (((24) + 1) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + ({ + let _p: Ptr = (*sb.borrow()).clone(); + let _v: u8 = (((24) + 2) as u8); + set_bytes_0(_p, ::std::mem::size_of::(), _v) + }); + assert!( + ((((*s.borrow()).with(|__s| __s.tail) + == (((72340172838076673_u64 as u64).wrapping_mul(((((24) + 2) as u8) as u64))) + as i64)) as i32) + != 0) + ); + assert!( + (({ + let _p: Ptr = (*fb.borrow()).clone(); + let _v: u8 = (((24) + 2) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + field!((*s.borrow()), tail).write( + (((72340172838076673_u64 as u64).wrapping_mul(((((24) + 3) as u8) as u64))) as i64), + ); + assert!( + (({ + let _p: Ptr = (*sb.borrow()).clone(); + let _v: u8 = (((24) + 3) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + assert!( + (({ + let _p: Ptr = (*fb.borrow()).clone(); + let _v: u8 = (((24) + 3) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + } + ({ check_final_2((*s.borrow()).clone()) }); + ({ + set_bytes_0( + (field_ptr!((*s.borrow()), in_)).reinterpret_cast::(), + 8usize, + 33_u8, + ) + }); + assert!( + ((((((((*s.borrow()).with(|__s| __s.in_.a) as i32) + == ((((72340172838076673_u64 as u64).wrapping_mul((((33) as u8) as u64))) as i16) + as i32)) as i32) + != 0) + && ((((*s.borrow()).with(|__s| __s.in_.b) + == (((72340172838076673_u64 as u64).wrapping_mul((((33) as u8) as u64))) as i32)) + as i32) + != 0)) as i32) + != 0) + ); + let in_: Value = Rc::new(RefCell::new(In { + a: (((72340172838076673_u64 as u64).wrapping_mul((((35) as u8) as u64))) as i16), + b: (((72340172838076673_u64 as u64).wrapping_mul((((51) as u8) as u64))) as i32), + })); + field!((*s.borrow()), in_).write((*in_.borrow()).clone()); + assert!( + (({ + all_bytes_1( + (field_ptr!(field_ptr!((*s.borrow()), in_), a)).reinterpret_cast::(), + ::std::mem::size_of::(), + 35_u8, + ) + }) != 0) + ); + assert!( + (({ + all_bytes_1( + (field_ptr!(field_ptr!((*s.borrow()), in_), b)).reinterpret_cast::(), + ::std::mem::size_of::(), + 51_u8, + ) + }) != 0) + ); + ({ + let _p: Ptr = + (array_field_ptr!((*s.borrow()), arr) as Ptr).reinterpret_cast::(); + let _n: usize = ::std::mem::size_of::<[i32; 3]>(); + set_bytes_0(_p, _n, 133_u8) + }); + assert!( + ((((((((array_field_ptr!((*s.borrow()), arr) as Ptr::) + .offset((0) as isize) + .read()) + == (((72340172838076673_u64 as u64).wrapping_mul((((133) as u8) as u64))) as i32)) + as i32) + != 0) + && (((((array_field_ptr!((*s.borrow()), arr) as Ptr::) + .offset((2) as isize) + .read()) + == (((72340172838076673_u64 as u64).wrapping_mul((((133) as u8) as u64))) as i32)) + as i32) + != 0)) as i32) + != 0) + ); + ({ + let _p: Ptr = + ((array_field_ptr!((*s.borrow()), arr)) as Ptr).reinterpret_cast::(); + let _n: usize = ::std::mem::size_of::<[i32; 3]>(); + set_bytes_0(_p, _n, 134_u8) + }); + assert!( + (((((array_field_ptr!((*s.borrow()), arr) as Ptr::) + .offset((1) as isize) + .read()) + == (((72340172838076673_u64 as u64).wrapping_mul((((134) as u8) as u64))) as i32)) + as i32) + != 0) + ); + (array_field_ptr!((*s.borrow()), arr) as Ptr) + .offset((0) as isize) + .write((((72340172838076673_u64 as u64).wrapping_mul((((131) as u8) as u64))) as i32)); + (array_field_ptr!((*s.borrow()), arr) as Ptr) + .offset((1) as isize) + .write((((72340172838076673_u64 as u64).wrapping_mul((((147) as u8) as u64))) as i32)); + (array_field_ptr!((*s.borrow()), arr) as Ptr) + .offset((2) as isize) + .write((((72340172838076673_u64 as u64).wrapping_mul((((11) as u8) as u64))) as i32)); + let ib: Value> = Rc::new(RefCell::new( + (array_field_ptr!((*s.borrow()), bytes) as Ptr).reinterpret_cast::(), + )); + (*ib.borrow()) + .write((((72340172838076673_u64 as u64).wrapping_mul((((69) as u8) as u64))) as i32)); + assert!( + (({ + let _p: Ptr = (array_field_ptr!((*s.borrow()), bytes) as Ptr); + let _n: usize = ::std::mem::size_of::<[u8; 4]>(); + all_bytes_1(_p, _n, 69_u8) + }) != 0) + ); + (array_field_ptr!((*s.borrow()), bytes) as Ptr) + .offset((0) as isize) + .write(67_u8); + (array_field_ptr!((*s.borrow()), bytes) as Ptr) + .offset((1) as isize) + .write(83_u8); + (array_field_ptr!((*s.borrow()), bytes) as Ptr) + .offset((2) as isize) + .write(99_u8); + (array_field_ptr!((*s.borrow()), bytes) as Ptr) + .offset((3) as isize) + .write(115_u8); + assert!( + ((({ + let _lhs = ((*ib.borrow()).read()); + _lhs == (((array_field_ptr!((*s.borrow()), bytes)) as Ptr) + .reinterpret_cast::() + .read()) + }) as i32) + != 0) + ); + let v: Value = Rc::new(RefCell::new( + ((field_ptr!(field_ptr!((*s.borrow()), in_), b)) as Ptr).to_any(), + )); + (*v.borrow()).reinterpret_cast::().write(11); + assert!(((((*s.borrow()).with(|__s| __s.in_.b) == 11) as i32) != 0)); + let twelve: Value = Rc::new(RefCell::new(12)); + { + ((field_ptr!((*s.borrow()), x)) as Ptr) + .to_any() + .memcpy( + &((twelve.as_pointer()) as Ptr).to_any(), + ::std::mem::size_of::() as usize, + ); + ((field_ptr!((*s.borrow()), x)) as Ptr).to_any() + }; + assert!(((((*s.borrow()).with(|__s| __s.x) == 12) as i32) != 0)); + { + ((field_ptr!(field_ptr!((*s.borrow()), in_), b)) as Ptr) + .to_any() + .memset((51) as u8, ::std::mem::size_of::() as usize); + ((field_ptr!(field_ptr!((*s.borrow()), in_), b)) as Ptr).to_any() + }; + { + ((field_ptr!((*s.borrow()), x)) as Ptr) + .to_any() + .memset((19) as u8, ::std::mem::size_of::() as usize); + ((field_ptr!((*s.borrow()), x)) as Ptr).to_any() + }; + ({ check_final_2((*s.borrow()).clone()) }); +} +pub fn main() { + __cpp2rust_init_globals(); + std::process::exit(main_0()); +} +fn main_0() -> i32 { + let local: Value = >::default(); + { + ((local.as_pointer()) as Ptr) + .to_any() + .memset((0) as u8, 40usize as usize); + ((local.as_pointer()) as Ptr).to_any() + }; + ({ check_struct_3((local.as_pointer())) }); + let arr: Value> = Rc::new(RefCell::new( + (0..2).map(|_| ::default()).collect::>(), + )); + { + ((arr.as_pointer() as Ptr) as Ptr) + .to_any() + .memset((0) as u8, 80usize as usize); + ((arr.as_pointer() as Ptr) as Ptr).to_any() + }; + ({ check_struct_3(((arr.as_pointer() as Ptr).offset(1))) }); + assert!( + (({ + all_bytes_1( + ((arr.as_pointer() as Ptr).offset(0)).reinterpret_cast::(), + 40usize, + 0_u8, + ) + }) != 0) + ); + let heap: Value> = Rc::new(RefCell::new( + libcc2rs::calloc_refcount(1_usize, 40usize).reinterpret_cast::(), + )); + assert!((((!((*heap.borrow()).is_null())) as i32) != 0)); + ({ check_struct_3((*heap.borrow()).clone()) }); + libcc2rs::free_refcount((*heap.borrow()).to_any()); + let raw_: Value> = Rc::new(RefCell::new( + libcc2rs::malloc_refcount(40usize).reinterpret_cast::(), + )); + assert!((((!((*raw_.borrow()).is_null())) as i32) != 0)); + { + (*raw_.borrow()) + .to_any() + .memset((0) as u8, 40usize as usize); + (*raw_.borrow()).to_any() + }; + let view: Value> = Rc::new(RefCell::new((*raw_.borrow()).reinterpret_cast::())); + ({ check_struct_3((*view.borrow()).clone()) }); + assert!(((((((*raw_.borrow()).offset((32_usize) as isize).read()) as i32) == 27) as i32) != 0)); + ({ + let _p: Ptr = (*raw_.borrow()).offset((16_usize) as isize); + set_bytes_0(_p, ::std::mem::size_of::(), 119_u8) + }); + assert!( + (((((array_field_ptr!((*view.borrow()), arr) as Ptr::) + .offset((0) as isize) + .read()) + == (((72340172838076673_u64 as u64).wrapping_mul((((119) as u8) as u64))) as i32)) + as i32) + != 0) + ); + (array_field_ptr!((*view.borrow()), bytes) as Ptr) + .offset((3) as isize) + .write(17_u8); + assert!( + ((((((*raw_.borrow()) + .offset(((12_usize as usize).wrapping_add(3_usize)) as isize) + .read()) as i32) + == 17) as i32) + != 0) + ); + libcc2rs::free_refcount((*raw_.borrow()).to_any()); + return 0; +} +pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/unsafe/field_reinterpret_write_through.rs b/tests/unit/out/unsafe/field_reinterpret_write_through.rs new file mode 100644 index 0000000000..39484bdbfd --- /dev/null +++ b/tests/unit/out/unsafe/field_reinterpret_write_through.rs @@ -0,0 +1,1116 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +#[repr(C)] +#[derive(Copy, Clone, VaArg, Default)] +pub struct In { + pub a: i16, + pub b: i32, +} +#[repr(C)] +#[derive(Copy, Clone, VaArg)] +pub struct S { + pub x: i32, + pub in_: In, + pub bytes: [u8; 4], + pub arr: [i32; 3], + pub tail: i64, +} +impl Default for S { + fn default() -> Self { + S { + x: 0_i32, + in_: ::default(), + bytes: [0_u8; 4], + arr: [0_i32; 3], + tail: 0_i64, + } + } +} +pub unsafe fn set_bytes_0(mut p: *mut u8, mut n: usize, mut v: u8) { + let mut i: usize = 0_usize; + 'loop_: while ((((i) < (n)) as i32) != 0) { + (*p.offset((i) as isize)) = v; + i.postfix_inc(); + } +} +pub unsafe fn all_bytes_1(mut p: *const u8, mut n: usize, mut v: u8) -> i32 { + let mut i: usize = 0_usize; + 'loop_: while ((((i) < (n)) as i32) != 0) { + if (((((*p.offset((i) as isize)) as i32) != (v as i32)) as i32) != 0) { + return 0; + } + i.postfix_inc(); + } + return 1; +} +pub unsafe fn check_final_2(mut s: *mut S) { + assert!( + (((((*s).x) + == (((72340172838076673_u64 as u64).wrapping_mul((((19) as u8) as u64))) as i32)) + as i32) + != 0) + ); + assert!( + (((((*s).in_.a as i32) + == ((((72340172838076673_u64 as u64).wrapping_mul((((35) as u8) as u64))) as i16) + as i32)) as i32) + != 0) + ); + assert!( + (((((*s).in_.b) + == (((72340172838076673_u64 as u64).wrapping_mul((((51) as u8) as u64))) as i32)) + as i32) + != 0) + ); + assert!( + ((((((((*s).bytes[(0) as usize] as i32) == (67)) as i32) != 0) + && (((((*s).bytes[(1) as usize] as i32) == (83)) as i32) != 0)) as i32) + != 0) + ); + assert!( + ((((((((*s).bytes[(2) as usize] as i32) == (99)) as i32) != 0) + && (((((*s).bytes[(3) as usize] as i32) == (115)) as i32) != 0)) as i32) + != 0) + ); + assert!( + ((((((((*s).arr[(0) as usize]) + == (((72340172838076673_u64 as u64).wrapping_mul((((131) as u8) as u64))) as i32)) + as i32) + != 0) + && (((((*s).arr[(1) as usize]) + == (((72340172838076673_u64 as u64).wrapping_mul((((147) as u8) as u64))) as i32)) + as i32) + != 0)) as i32) + != 0) + ); + assert!( + (((((*s).arr[(2) as usize]) + == (((72340172838076673_u64 as u64).wrapping_mul((((11) as u8) as u64))) as i32)) + as i32) + != 0) + ); + assert!( + (((((*s).tail) + == (((72340172838076673_u64 as u64).wrapping_mul((((27) as u8) as u64))) as i64)) + as i32) + != 0) + ); +} +pub unsafe fn check_struct_3(mut s: *mut S) { + let mut __do_while = true; + 'loop_: while __do_while || (0 != 0) { + __do_while = false; + let mut fb: *mut u8 = ((&mut (*(s)).x as *mut i32) as *mut u8); + let mut sb: *mut u8 = ((s) as *mut u8).offset((::std::mem::offset_of!(S, x)) as isize); + (unsafe { + let _p: *mut u8 = fb; + set_bytes_0(_p, ::std::mem::size_of::(), ((16) as u8)) + }); + assert!( + (((((*(s)).x) + == (((72340172838076673_u64 as u64).wrapping_mul((((16) as u8) as u64))) as i32)) + as i32) + != 0) + ); + assert!( + ((unsafe { + let _p: *const u8 = (sb).cast_const(); + all_bytes_1(_p, ::std::mem::size_of::(), ((16) as u8)) + }) != 0) + ); + (*(s)).x = + (((72340172838076673_u64 as u64).wrapping_mul(((((16) + (1)) as u8) as u64))) as i32); + assert!( + ((unsafe { + let _p: *const u8 = (fb).cast_const(); + let _v: u8 = (((16) + (1)) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + assert!( + ((unsafe { + let _p: *const u8 = (sb).cast_const(); + let _v: u8 = (((16) + (1)) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + (unsafe { + let _p: *mut u8 = sb; + let _v: u8 = (((16) + (2)) as u8); + set_bytes_0(_p, ::std::mem::size_of::(), _v) + }); + assert!( + (((((*(s)).x) + == (((72340172838076673_u64 as u64).wrapping_mul(((((16) + (2)) as u8) as u64))) + as i32)) as i32) + != 0) + ); + assert!( + ((unsafe { + let _p: *const u8 = (fb).cast_const(); + let _v: u8 = (((16) + (2)) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + (*(s)).x = + (((72340172838076673_u64 as u64).wrapping_mul(((((16) + (3)) as u8) as u64))) as i32); + assert!( + ((unsafe { + let _p: *const u8 = (sb).cast_const(); + let _v: u8 = (((16) + (3)) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + assert!( + ((unsafe { + let _p: *const u8 = (fb).cast_const(); + let _v: u8 = (((16) + (3)) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + } + let mut __do_while = true; + 'loop_: while __do_while || (0 != 0) { + __do_while = false; + let mut fb: *mut u8 = ((&mut (*(s)).in_.a as *mut i16) as *mut u8); + let mut sb: *mut u8 = ((s) as *mut u8).offset( + ((::std::mem::offset_of!(S, in_) as usize) + .wrapping_add((::std::mem::offset_of!(In, a) as usize))) as isize, + ); + (unsafe { + let _p: *mut u8 = fb; + set_bytes_0(_p, ::std::mem::size_of::(), ((32) as u8)) + }); + assert!( + (((((*(s)).in_.a as i32) + == ((((72340172838076673_u64 as u64).wrapping_mul((((32) as u8) as u64))) as i16) + as i32)) as i32) + != 0) + ); + assert!( + ((unsafe { + let _p: *const u8 = (sb).cast_const(); + all_bytes_1(_p, ::std::mem::size_of::(), ((32) as u8)) + }) != 0) + ); + (*(s)).in_.a = + (((72340172838076673_u64 as u64).wrapping_mul(((((32) + (1)) as u8) as u64))) as i16); + assert!( + ((unsafe { + let _p: *const u8 = (fb).cast_const(); + let _v: u8 = (((32) + (1)) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + assert!( + ((unsafe { + let _p: *const u8 = (sb).cast_const(); + let _v: u8 = (((32) + (1)) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + (unsafe { + let _p: *mut u8 = sb; + let _v: u8 = (((32) + (2)) as u8); + set_bytes_0(_p, ::std::mem::size_of::(), _v) + }); + assert!( + (((((*(s)).in_.a as i32) + == ((((72340172838076673_u64 as u64).wrapping_mul(((((32) + (2)) as u8) as u64))) + as i16) as i32)) as i32) + != 0) + ); + assert!( + ((unsafe { + let _p: *const u8 = (fb).cast_const(); + let _v: u8 = (((32) + (2)) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + (*(s)).in_.a = + (((72340172838076673_u64 as u64).wrapping_mul(((((32) + (3)) as u8) as u64))) as i16); + assert!( + ((unsafe { + let _p: *const u8 = (sb).cast_const(); + let _v: u8 = (((32) + (3)) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + assert!( + ((unsafe { + let _p: *const u8 = (fb).cast_const(); + let _v: u8 = (((32) + (3)) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + } + let mut __do_while = true; + 'loop_: while __do_while || (0 != 0) { + __do_while = false; + let mut fb: *mut u8 = ((&mut (*(s)).in_.b as *mut i32) as *mut u8); + let mut sb: *mut u8 = ((s) as *mut u8).offset( + ((::std::mem::offset_of!(S, in_) as usize) + .wrapping_add((::std::mem::offset_of!(In, b) as usize))) as isize, + ); + (unsafe { + let _p: *mut u8 = fb; + set_bytes_0(_p, ::std::mem::size_of::(), ((48) as u8)) + }); + assert!( + (((((*(s)).in_.b) + == (((72340172838076673_u64 as u64).wrapping_mul((((48) as u8) as u64))) as i32)) + as i32) + != 0) + ); + assert!( + ((unsafe { + let _p: *const u8 = (sb).cast_const(); + all_bytes_1(_p, ::std::mem::size_of::(), ((48) as u8)) + }) != 0) + ); + (*(s)).in_.b = + (((72340172838076673_u64 as u64).wrapping_mul(((((48) + (1)) as u8) as u64))) as i32); + assert!( + ((unsafe { + let _p: *const u8 = (fb).cast_const(); + let _v: u8 = (((48) + (1)) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + assert!( + ((unsafe { + let _p: *const u8 = (sb).cast_const(); + let _v: u8 = (((48) + (1)) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + (unsafe { + let _p: *mut u8 = sb; + let _v: u8 = (((48) + (2)) as u8); + set_bytes_0(_p, ::std::mem::size_of::(), _v) + }); + assert!( + (((((*(s)).in_.b) + == (((72340172838076673_u64 as u64).wrapping_mul(((((48) + (2)) as u8) as u64))) + as i32)) as i32) + != 0) + ); + assert!( + ((unsafe { + let _p: *const u8 = (fb).cast_const(); + let _v: u8 = (((48) + (2)) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + (*(s)).in_.b = + (((72340172838076673_u64 as u64).wrapping_mul(((((48) + (3)) as u8) as u64))) as i32); + assert!( + ((unsafe { + let _p: *const u8 = (sb).cast_const(); + let _v: u8 = (((48) + (3)) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + assert!( + ((unsafe { + let _p: *const u8 = (fb).cast_const(); + let _v: u8 = (((48) + (3)) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + } + let mut __do_while = true; + 'loop_: while __do_while || (0 != 0) { + __do_while = false; + let mut fb: *mut u8 = (&mut (*(s)).bytes[(0) as usize] as *mut u8); + let mut sb: *mut u8 = ((s) as *mut u8) + .offset(((::std::mem::offset_of!(S, bytes) as usize).wrapping_add(0_usize)) as isize); + (unsafe { + let _p: *mut u8 = fb; + set_bytes_0(_p, ::std::mem::size_of::(), ((64) as u8)) + }); + assert!( + (((((*(s)).bytes[(0) as usize] as i32) + == ((((72340172838076673_u64 as u64).wrapping_mul((((64) as u8) as u64))) as u8) + as i32)) as i32) + != 0) + ); + assert!( + ((unsafe { + let _p: *const u8 = (sb).cast_const(); + all_bytes_1(_p, ::std::mem::size_of::(), ((64) as u8)) + }) != 0) + ); + (*(s)).bytes[(0) as usize] = + (((72340172838076673_u64 as u64).wrapping_mul(((((64) + (1)) as u8) as u64))) as u8); + assert!( + ((unsafe { + let _p: *const u8 = (fb).cast_const(); + let _v: u8 = (((64) + (1)) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + assert!( + ((unsafe { + let _p: *const u8 = (sb).cast_const(); + let _v: u8 = (((64) + (1)) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + (unsafe { + let _p: *mut u8 = sb; + let _v: u8 = (((64) + (2)) as u8); + set_bytes_0(_p, ::std::mem::size_of::(), _v) + }); + assert!( + (((((*(s)).bytes[(0) as usize] as i32) + == ((((72340172838076673_u64 as u64).wrapping_mul(((((64) + (2)) as u8) as u64))) + as u8) as i32)) as i32) + != 0) + ); + assert!( + ((unsafe { + let _p: *const u8 = (fb).cast_const(); + let _v: u8 = (((64) + (2)) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + (*(s)).bytes[(0) as usize] = + (((72340172838076673_u64 as u64).wrapping_mul(((((64) + (3)) as u8) as u64))) as u8); + assert!( + ((unsafe { + let _p: *const u8 = (sb).cast_const(); + let _v: u8 = (((64) + (3)) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + assert!( + ((unsafe { + let _p: *const u8 = (fb).cast_const(); + let _v: u8 = (((64) + (3)) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + } + let mut __do_while = true; + 'loop_: while __do_while || (0 != 0) { + __do_while = false; + let mut fb: *mut u8 = (&mut (*(s)).bytes[(1) as usize] as *mut u8); + let mut sb: *mut u8 = ((s) as *mut u8) + .offset(((::std::mem::offset_of!(S, bytes) as usize).wrapping_add(1_usize)) as isize); + (unsafe { + let _p: *mut u8 = fb; + set_bytes_0(_p, ::std::mem::size_of::(), ((80) as u8)) + }); + assert!( + (((((*(s)).bytes[(1) as usize] as i32) + == ((((72340172838076673_u64 as u64).wrapping_mul((((80) as u8) as u64))) as u8) + as i32)) as i32) + != 0) + ); + assert!( + ((unsafe { + let _p: *const u8 = (sb).cast_const(); + all_bytes_1(_p, ::std::mem::size_of::(), ((80) as u8)) + }) != 0) + ); + (*(s)).bytes[(1) as usize] = + (((72340172838076673_u64 as u64).wrapping_mul(((((80) + (1)) as u8) as u64))) as u8); + assert!( + ((unsafe { + let _p: *const u8 = (fb).cast_const(); + let _v: u8 = (((80) + (1)) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + assert!( + ((unsafe { + let _p: *const u8 = (sb).cast_const(); + let _v: u8 = (((80) + (1)) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + (unsafe { + let _p: *mut u8 = sb; + let _v: u8 = (((80) + (2)) as u8); + set_bytes_0(_p, ::std::mem::size_of::(), _v) + }); + assert!( + (((((*(s)).bytes[(1) as usize] as i32) + == ((((72340172838076673_u64 as u64).wrapping_mul(((((80) + (2)) as u8) as u64))) + as u8) as i32)) as i32) + != 0) + ); + assert!( + ((unsafe { + let _p: *const u8 = (fb).cast_const(); + let _v: u8 = (((80) + (2)) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + (*(s)).bytes[(1) as usize] = + (((72340172838076673_u64 as u64).wrapping_mul(((((80) + (3)) as u8) as u64))) as u8); + assert!( + ((unsafe { + let _p: *const u8 = (sb).cast_const(); + let _v: u8 = (((80) + (3)) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + assert!( + ((unsafe { + let _p: *const u8 = (fb).cast_const(); + let _v: u8 = (((80) + (3)) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + } + let mut __do_while = true; + 'loop_: while __do_while || (0 != 0) { + __do_while = false; + let mut fb: *mut u8 = (&mut (*(s)).bytes[(2) as usize] as *mut u8); + let mut sb: *mut u8 = ((s) as *mut u8) + .offset(((::std::mem::offset_of!(S, bytes) as usize).wrapping_add(2_usize)) as isize); + (unsafe { + let _p: *mut u8 = fb; + set_bytes_0(_p, ::std::mem::size_of::(), ((96) as u8)) + }); + assert!( + (((((*(s)).bytes[(2) as usize] as i32) + == ((((72340172838076673_u64 as u64).wrapping_mul((((96) as u8) as u64))) as u8) + as i32)) as i32) + != 0) + ); + assert!( + ((unsafe { + let _p: *const u8 = (sb).cast_const(); + all_bytes_1(_p, ::std::mem::size_of::(), ((96) as u8)) + }) != 0) + ); + (*(s)).bytes[(2) as usize] = + (((72340172838076673_u64 as u64).wrapping_mul(((((96) + (1)) as u8) as u64))) as u8); + assert!( + ((unsafe { + let _p: *const u8 = (fb).cast_const(); + let _v: u8 = (((96) + (1)) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + assert!( + ((unsafe { + let _p: *const u8 = (sb).cast_const(); + let _v: u8 = (((96) + (1)) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + (unsafe { + let _p: *mut u8 = sb; + let _v: u8 = (((96) + (2)) as u8); + set_bytes_0(_p, ::std::mem::size_of::(), _v) + }); + assert!( + (((((*(s)).bytes[(2) as usize] as i32) + == ((((72340172838076673_u64 as u64).wrapping_mul(((((96) + (2)) as u8) as u64))) + as u8) as i32)) as i32) + != 0) + ); + assert!( + ((unsafe { + let _p: *const u8 = (fb).cast_const(); + let _v: u8 = (((96) + (2)) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + (*(s)).bytes[(2) as usize] = + (((72340172838076673_u64 as u64).wrapping_mul(((((96) + (3)) as u8) as u64))) as u8); + assert!( + ((unsafe { + let _p: *const u8 = (sb).cast_const(); + let _v: u8 = (((96) + (3)) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + assert!( + ((unsafe { + let _p: *const u8 = (fb).cast_const(); + let _v: u8 = (((96) + (3)) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + } + let mut __do_while = true; + 'loop_: while __do_while || (0 != 0) { + __do_while = false; + let mut fb: *mut u8 = (&mut (*(s)).bytes[(3) as usize] as *mut u8); + let mut sb: *mut u8 = ((s) as *mut u8) + .offset(((::std::mem::offset_of!(S, bytes) as usize).wrapping_add(3_usize)) as isize); + (unsafe { + let _p: *mut u8 = fb; + set_bytes_0(_p, ::std::mem::size_of::(), ((112) as u8)) + }); + assert!( + (((((*(s)).bytes[(3) as usize] as i32) + == ((((72340172838076673_u64 as u64).wrapping_mul((((112) as u8) as u64))) as u8) + as i32)) as i32) + != 0) + ); + assert!( + ((unsafe { + let _p: *const u8 = (sb).cast_const(); + all_bytes_1(_p, ::std::mem::size_of::(), ((112) as u8)) + }) != 0) + ); + (*(s)).bytes[(3) as usize] = + (((72340172838076673_u64 as u64).wrapping_mul(((((112) + (1)) as u8) as u64))) as u8); + assert!( + ((unsafe { + let _p: *const u8 = (fb).cast_const(); + let _v: u8 = (((112) + (1)) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + assert!( + ((unsafe { + let _p: *const u8 = (sb).cast_const(); + let _v: u8 = (((112) + (1)) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + (unsafe { + let _p: *mut u8 = sb; + let _v: u8 = (((112) + (2)) as u8); + set_bytes_0(_p, ::std::mem::size_of::(), _v) + }); + assert!( + (((((*(s)).bytes[(3) as usize] as i32) + == ((((72340172838076673_u64 as u64).wrapping_mul(((((112) + (2)) as u8) as u64))) + as u8) as i32)) as i32) + != 0) + ); + assert!( + ((unsafe { + let _p: *const u8 = (fb).cast_const(); + let _v: u8 = (((112) + (2)) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + (*(s)).bytes[(3) as usize] = + (((72340172838076673_u64 as u64).wrapping_mul(((((112) + (3)) as u8) as u64))) as u8); + assert!( + ((unsafe { + let _p: *const u8 = (sb).cast_const(); + let _v: u8 = (((112) + (3)) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + assert!( + ((unsafe { + let _p: *const u8 = (fb).cast_const(); + let _v: u8 = (((112) + (3)) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + } + let mut __do_while = true; + 'loop_: while __do_while || (0 != 0) { + __do_while = false; + let mut fb: *mut u8 = ((&mut (*(s)).arr[(0) as usize] as *mut i32) as *mut i32 as *mut u8); + let mut sb: *mut u8 = ((s) as *mut u8).offset( + ((::std::mem::offset_of!(S, arr) as usize).wrapping_add( + ((0_usize).wrapping_mul((::std::mem::size_of::() as usize)) as usize), + )) as isize, + ); + (unsafe { + let _p: *mut u8 = fb; + set_bytes_0(_p, ::std::mem::size_of::(), ((128) as u8)) + }); + assert!( + (((((*(s)).arr[(0) as usize]) + == (((72340172838076673_u64 as u64).wrapping_mul((((128) as u8) as u64))) as i32)) + as i32) + != 0) + ); + assert!( + ((unsafe { + let _p: *const u8 = (sb).cast_const(); + all_bytes_1(_p, ::std::mem::size_of::(), ((128) as u8)) + }) != 0) + ); + (*(s)).arr[(0) as usize] = + (((72340172838076673_u64 as u64).wrapping_mul(((((128) + (1)) as u8) as u64))) as i32); + assert!( + ((unsafe { + let _p: *const u8 = (fb).cast_const(); + let _v: u8 = (((128) + (1)) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + assert!( + ((unsafe { + let _p: *const u8 = (sb).cast_const(); + let _v: u8 = (((128) + (1)) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + (unsafe { + let _p: *mut u8 = sb; + let _v: u8 = (((128) + (2)) as u8); + set_bytes_0(_p, ::std::mem::size_of::(), _v) + }); + assert!( + (((((*(s)).arr[(0) as usize]) + == (((72340172838076673_u64 as u64).wrapping_mul(((((128) + (2)) as u8) as u64))) + as i32)) as i32) + != 0) + ); + assert!( + ((unsafe { + let _p: *const u8 = (fb).cast_const(); + let _v: u8 = (((128) + (2)) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + (*(s)).arr[(0) as usize] = + (((72340172838076673_u64 as u64).wrapping_mul(((((128) + (3)) as u8) as u64))) as i32); + assert!( + ((unsafe { + let _p: *const u8 = (sb).cast_const(); + let _v: u8 = (((128) + (3)) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + assert!( + ((unsafe { + let _p: *const u8 = (fb).cast_const(); + let _v: u8 = (((128) + (3)) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + } + let mut __do_while = true; + 'loop_: while __do_while || (0 != 0) { + __do_while = false; + let mut fb: *mut u8 = ((&mut (*(s)).arr[(1) as usize] as *mut i32) as *mut i32 as *mut u8); + let mut sb: *mut u8 = ((s) as *mut u8).offset( + ((::std::mem::offset_of!(S, arr) as usize).wrapping_add( + ((1_usize).wrapping_mul((::std::mem::size_of::() as usize)) as usize), + )) as isize, + ); + (unsafe { + let _p: *mut u8 = fb; + set_bytes_0(_p, ::std::mem::size_of::(), ((144) as u8)) + }); + assert!( + (((((*(s)).arr[(1) as usize]) + == (((72340172838076673_u64 as u64).wrapping_mul((((144) as u8) as u64))) as i32)) + as i32) + != 0) + ); + assert!( + ((unsafe { + let _p: *const u8 = (sb).cast_const(); + all_bytes_1(_p, ::std::mem::size_of::(), ((144) as u8)) + }) != 0) + ); + (*(s)).arr[(1) as usize] = + (((72340172838076673_u64 as u64).wrapping_mul(((((144) + (1)) as u8) as u64))) as i32); + assert!( + ((unsafe { + let _p: *const u8 = (fb).cast_const(); + let _v: u8 = (((144) + (1)) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + assert!( + ((unsafe { + let _p: *const u8 = (sb).cast_const(); + let _v: u8 = (((144) + (1)) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + (unsafe { + let _p: *mut u8 = sb; + let _v: u8 = (((144) + (2)) as u8); + set_bytes_0(_p, ::std::mem::size_of::(), _v) + }); + assert!( + (((((*(s)).arr[(1) as usize]) + == (((72340172838076673_u64 as u64).wrapping_mul(((((144) + (2)) as u8) as u64))) + as i32)) as i32) + != 0) + ); + assert!( + ((unsafe { + let _p: *const u8 = (fb).cast_const(); + let _v: u8 = (((144) + (2)) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + (*(s)).arr[(1) as usize] = + (((72340172838076673_u64 as u64).wrapping_mul(((((144) + (3)) as u8) as u64))) as i32); + assert!( + ((unsafe { + let _p: *const u8 = (sb).cast_const(); + let _v: u8 = (((144) + (3)) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + assert!( + ((unsafe { + let _p: *const u8 = (fb).cast_const(); + let _v: u8 = (((144) + (3)) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + } + let mut __do_while = true; + 'loop_: while __do_while || (0 != 0) { + __do_while = false; + let mut fb: *mut u8 = ((&mut (*(s)).arr[(2) as usize] as *mut i32) as *mut i32 as *mut u8); + let mut sb: *mut u8 = ((s) as *mut u8).offset( + ((::std::mem::offset_of!(S, arr) as usize).wrapping_add( + ((2_usize).wrapping_mul((::std::mem::size_of::() as usize)) as usize), + )) as isize, + ); + (unsafe { + let _p: *mut u8 = fb; + set_bytes_0(_p, ::std::mem::size_of::(), ((8) as u8)) + }); + assert!( + (((((*(s)).arr[(2) as usize]) + == (((72340172838076673_u64 as u64).wrapping_mul((((8) as u8) as u64))) as i32)) + as i32) + != 0) + ); + assert!( + ((unsafe { + let _p: *const u8 = (sb).cast_const(); + all_bytes_1(_p, ::std::mem::size_of::(), ((8) as u8)) + }) != 0) + ); + (*(s)).arr[(2) as usize] = + (((72340172838076673_u64 as u64).wrapping_mul(((((8) + (1)) as u8) as u64))) as i32); + assert!( + ((unsafe { + let _p: *const u8 = (fb).cast_const(); + let _v: u8 = (((8) + (1)) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + assert!( + ((unsafe { + let _p: *const u8 = (sb).cast_const(); + let _v: u8 = (((8) + (1)) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + (unsafe { + let _p: *mut u8 = sb; + let _v: u8 = (((8) + (2)) as u8); + set_bytes_0(_p, ::std::mem::size_of::(), _v) + }); + assert!( + (((((*(s)).arr[(2) as usize]) + == (((72340172838076673_u64 as u64).wrapping_mul(((((8) + (2)) as u8) as u64))) + as i32)) as i32) + != 0) + ); + assert!( + ((unsafe { + let _p: *const u8 = (fb).cast_const(); + let _v: u8 = (((8) + (2)) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + (*(s)).arr[(2) as usize] = + (((72340172838076673_u64 as u64).wrapping_mul(((((8) + (3)) as u8) as u64))) as i32); + assert!( + ((unsafe { + let _p: *const u8 = (sb).cast_const(); + let _v: u8 = (((8) + (3)) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + assert!( + ((unsafe { + let _p: *const u8 = (fb).cast_const(); + let _v: u8 = (((8) + (3)) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + } + let mut __do_while = true; + 'loop_: while __do_while || (0 != 0) { + __do_while = false; + let mut fb: *mut u8 = ((&mut (*(s)).tail as *mut i64) as *mut u8); + let mut sb: *mut u8 = ((s) as *mut u8).offset((::std::mem::offset_of!(S, tail)) as isize); + (unsafe { + let _p: *mut u8 = fb; + set_bytes_0(_p, ::std::mem::size_of::(), ((24) as u8)) + }); + assert!( + (((((*(s)).tail) + == (((72340172838076673_u64 as u64).wrapping_mul((((24) as u8) as u64))) as i64)) + as i32) + != 0) + ); + assert!( + ((unsafe { + let _p: *const u8 = (sb).cast_const(); + all_bytes_1(_p, ::std::mem::size_of::(), ((24) as u8)) + }) != 0) + ); + (*(s)).tail = + (((72340172838076673_u64 as u64).wrapping_mul(((((24) + (1)) as u8) as u64))) as i64); + assert!( + ((unsafe { + let _p: *const u8 = (fb).cast_const(); + let _v: u8 = (((24) + (1)) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + assert!( + ((unsafe { + let _p: *const u8 = (sb).cast_const(); + let _v: u8 = (((24) + (1)) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + (unsafe { + let _p: *mut u8 = sb; + let _v: u8 = (((24) + (2)) as u8); + set_bytes_0(_p, ::std::mem::size_of::(), _v) + }); + assert!( + (((((*(s)).tail) + == (((72340172838076673_u64 as u64).wrapping_mul(((((24) + (2)) as u8) as u64))) + as i64)) as i32) + != 0) + ); + assert!( + ((unsafe { + let _p: *const u8 = (fb).cast_const(); + let _v: u8 = (((24) + (2)) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + (*(s)).tail = + (((72340172838076673_u64 as u64).wrapping_mul(((((24) + (3)) as u8) as u64))) as i64); + assert!( + ((unsafe { + let _p: *const u8 = (sb).cast_const(); + let _v: u8 = (((24) + (3)) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + assert!( + ((unsafe { + let _p: *const u8 = (fb).cast_const(); + let _v: u8 = (((24) + (3)) as u8); + all_bytes_1(_p, ::std::mem::size_of::(), _v) + }) != 0) + ); + } + (unsafe { check_final_2(s) }); + (unsafe { + set_bytes_0( + ((&mut (*s).in_ as *mut In) as *mut u8), + ::std::mem::size_of::(), + 33_u8, + ) + }); + assert!( + ((((((((*s).in_.a as i32) + == ((((72340172838076673_u64 as u64).wrapping_mul((((33) as u8) as u64))) as i16) + as i32)) as i32) + != 0) + && (((((*s).in_.b) + == (((72340172838076673_u64 as u64).wrapping_mul((((33) as u8) as u64))) as i32)) + as i32) + != 0)) as i32) + != 0) + ); + let mut in_: In = In { + a: (((72340172838076673_u64 as u64).wrapping_mul((((35) as u8) as u64))) as i16), + b: (((72340172838076673_u64 as u64).wrapping_mul((((51) as u8) as u64))) as i32), + }; + (*s).in_ = in_; + assert!( + ((unsafe { + all_bytes_1( + ((&mut (*s).in_.a as *mut i16) as *mut u8).cast_const(), + ::std::mem::size_of::(), + 35_u8, + ) + }) != 0) + ); + assert!( + ((unsafe { + all_bytes_1( + ((&mut (*s).in_.b as *mut i32) as *mut u8).cast_const(), + ::std::mem::size_of::(), + 51_u8, + ) + }) != 0) + ); + (unsafe { + let _p: *mut u8 = ((*s).arr.as_mut_ptr() as *mut u8); + let _n: usize = ::std::mem::size_of::<[i32; 3]>(); + set_bytes_0(_p, _n, 133_u8) + }); + assert!( + ((((((((*s).arr[(0) as usize]) + == (((72340172838076673_u64 as u64).wrapping_mul((((133) as u8) as u64))) as i32)) + as i32) + != 0) + && (((((*s).arr[(2) as usize]) + == (((72340172838076673_u64 as u64).wrapping_mul((((133) as u8) as u64))) as i32)) + as i32) + != 0)) as i32) + != 0) + ); + (unsafe { + let _p: *mut u8 = ((&mut (*s).arr as *mut [i32; 3]) as *mut u8); + let _n: usize = ::std::mem::size_of::<[i32; 3]>(); + set_bytes_0(_p, _n, 134_u8) + }); + assert!( + (((((*s).arr[(1) as usize]) + == (((72340172838076673_u64 as u64).wrapping_mul((((134) as u8) as u64))) as i32)) + as i32) + != 0) + ); + (*s).arr[(0) as usize] = + (((72340172838076673_u64 as u64).wrapping_mul((((131) as u8) as u64))) as i32); + (*s).arr[(1) as usize] = + (((72340172838076673_u64 as u64).wrapping_mul((((147) as u8) as u64))) as i32); + (*s).arr[(2) as usize] = + (((72340172838076673_u64 as u64).wrapping_mul((((11) as u8) as u64))) as i32); + let mut ib: *mut i32 = ((*s).bytes.as_mut_ptr() as *mut i32); + (*ib) = (((72340172838076673_u64 as u64).wrapping_mul((((69) as u8) as u64))) as i32); + assert!( + ((unsafe { + let _p: *const u8 = ((*s).bytes.as_mut_ptr()).cast_const(); + let _n: usize = ::std::mem::size_of::<[u8; 4]>(); + all_bytes_1(_p, _n, 69_u8) + }) != 0) + ); + (*s).bytes[(0) as usize] = 67_u8; + (*s).bytes[(1) as usize] = 83_u8; + (*s).bytes[(2) as usize] = 99_u8; + (*s).bytes[(3) as usize] = 115_u8; + assert!( + ((((*ib) == (*(((&mut (*s).bytes as *mut [u8; 4]) as *mut u8) as *mut i32))) as i32) != 0) + ); + let mut v: *mut ::libc::c_void = ((&mut (*s).in_.b as *mut i32) as *mut ::libc::c_void); + (*(v as *mut i32)) = 11; + assert!((((((*s).in_.b) == (11)) as i32) != 0)); + let mut twelve: i32 = 12; + { + if ::std::mem::size_of::() != 0 { + ::std::ptr::copy_nonoverlapping( + ((&mut twelve as *mut i32) as *const ::libc::c_void), + ((&mut (*s).x as *mut i32) as *mut ::libc::c_void), + ::std::mem::size_of::() as usize, + ) + } + ((&mut (*s).x as *mut i32) as *mut ::libc::c_void) + }; + assert!((((((*s).x) == (12)) as i32) != 0)); + { + let byte_0 = ((&mut (*s).in_.b as *mut i32) as *mut ::libc::c_void) as *mut u8; + for offset in 0..::std::mem::size_of::() { + *byte_0.offset(offset as isize) = 51 as u8; + } + ((&mut (*s).in_.b as *mut i32) as *mut ::libc::c_void) + }; + { + let byte_0 = ((&mut (*s).x as *mut i32) as *mut ::libc::c_void) as *mut u8; + for offset in 0..::std::mem::size_of::() { + *byte_0.offset(offset as isize) = 19 as u8; + } + ((&mut (*s).x as *mut i32) as *mut ::libc::c_void) + }; + (unsafe { check_final_2(s) }); +} +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + let mut local: S = ::default(); + { + let byte_0 = ((&mut local as *mut S) as *mut ::libc::c_void) as *mut u8; + for offset in 0..::std::mem::size_of::() { + *byte_0.offset(offset as isize) = 0 as u8; + } + ((&mut local as *mut S) as *mut ::libc::c_void) + }; + (unsafe { check_struct_3((&mut local as *mut S)) }); + let mut arr: [S; 2] = [::default(); 2]; + { + let byte_0 = (arr.as_mut_ptr() as *mut ::libc::c_void) as *mut u8; + for offset in 0..::std::mem::size_of::<[S; 2]>() { + *byte_0.offset(offset as isize) = 0 as u8; + } + (arr.as_mut_ptr() as *mut ::libc::c_void) + }; + (unsafe { check_struct_3((&mut arr[(1) as usize] as *mut S)) }); + assert!( + ((unsafe { + all_bytes_1( + ((&mut arr[(0) as usize] as *mut S) as *mut S as *mut u8).cast_const(), + ::std::mem::size_of::(), + 0_u8, + ) + }) != 0) + ); + let mut heap: *mut S = (libcc2rs::calloc_unsafe(1_usize, ::std::mem::size_of::()) as *mut S); + assert!((((!((heap).is_null())) as i32) != 0)); + (unsafe { check_struct_3(heap) }); + libcc2rs::free_unsafe((heap as *mut ::libc::c_void)); + let mut raw_: *mut u8 = (libcc2rs::malloc_unsafe(::std::mem::size_of::()) as *mut u8); + assert!((((!((raw_).is_null())) as i32) != 0)); + { + let byte_0 = (raw_ as *mut ::libc::c_void) as *mut u8; + for offset in 0..::std::mem::size_of::() { + *byte_0.offset(offset as isize) = 0 as u8; + } + (raw_ as *mut ::libc::c_void) + }; + let mut view: *mut S = (raw_ as *mut S); + (unsafe { check_struct_3(view) }); + assert!( + (((((*raw_.offset((::std::mem::offset_of!(S, tail)) as isize)) as i32) == (27)) as i32) + != 0) + ); + (unsafe { + let _p: *mut u8 = raw_.offset((::std::mem::offset_of!(S, arr)) as isize); + set_bytes_0(_p, ::std::mem::size_of::(), 119_u8) + }); + assert!( + (((((*view).arr[(0) as usize]) + == (((72340172838076673_u64 as u64).wrapping_mul((((119) as u8) as u64))) as i32)) + as i32) + != 0) + ); + (*view).bytes[(3) as usize] = 17_u8; + assert!( + (((((*raw_ + .offset(((::std::mem::offset_of!(S, bytes) as usize).wrapping_add(3_usize)) as isize)) + as i32) + == (17)) as i32) + != 0) + ); + libcc2rs::free_unsafe((raw_ as *mut ::libc::c_void)); + return 0; +} +pub unsafe fn __cpp2rust_init_globals() {}