diff --git a/cpp2rust/converter/converter.cpp b/cpp2rust/converter/converter.cpp index fa688b9c02..cd420b2a5d 100644 --- a/cpp2rust/converter/converter.cpp +++ b/cpp2rust/converter/converter.cpp @@ -835,8 +835,8 @@ bool Converter::ConvertVarDecl(clang::VarDecl *decl) { return false; } - if (auto *construct = GetInPlaceVarConstruct(decl)) { - EmitLocalObject(decl, construct); + if (ConstructsVarInPlace(decl)) { + EmitLocalObject(decl); return true; } @@ -1554,8 +1554,8 @@ void Converter::ConvertCXXConstructorBody(clang::CXXConstructorDecl *decl) { void Converter::EmitConstructorInit(clang::CXXConstructorDecl *decl) { if (decl->isDelegatingConstructor()) { auto *init = (*decl->init_begin())->getInit(); - if (auto *construct = GetInPlaceConstruct(init)) { - ConstructInPlace(init, construct, GetDelegatingReceiver()); + if (BuildsInStorage(init)) { + ConstructInPlace(init, GetDelegatingReceiver()); return; } StrCat(GetConstructorWrite()); @@ -1587,8 +1587,8 @@ void Converter::EmitConstructorFieldInits(clang::CXXConstructorDecl *decl) { auto *init = ctor_initializer ? ctor_initializer->getInit() : field->getInClassInitializer(); - if (auto *construct = init ? GetPlacementInitializer(init) : nullptr) { - ConstructInPlace(init, construct, GetFieldPlacementReceiver(field_name)); + if (init && BuildsInStorage(init)) { + ConstructInPlace(init, GetFieldPlacementReceiver(field_name)); continue; } std::string value; @@ -1764,8 +1764,8 @@ bool Converter::VisitReturnStmt(clang::ReturnStmt *stmt) { auto return_type = curr_function_->getReturnType(); if (auto *value = stmt->getRetValue(); value && UsesReturnSlot(return_type)) { std::string_view slot = "__return"; - if (auto *construct = GetPlacementInitializer(value)) { - ConstructInPlace(value, construct, GetReturnSlotReceiver(slot)); + if (BuildsInStorage(value)) { + ConstructInPlace(value, GetReturnSlotReceiver(slot)); } else { HoistMaterializedTempBindings hoist_temps(*this); StrCat(slot, ".write"); @@ -2037,6 +2037,10 @@ bool Converter::Convert(clang::Expr *expr, } bool Converter::VisitExprWithCleanups(clang::ExprWithCleanups *expr) { + if (ForwardsStorage(expr, expr->getSubExpr())) { + Convert(expr->getSubExpr()); + return false; + } ConvertFullExpression([&] { Convert(expr->getSubExpr()); }); return false; } @@ -2063,11 +2067,14 @@ std::string Converter::ConstructTemporaryInPlace(const std::string &name, clang::Expr *temporary, const clang::Expr *init, std::string *declarations) { - auto storage = CreatePlacementStorage( - GetRecordName(init->getType()->getAsCXXRecordDecl()), name); - PushExprKind push(*this, ExprKind::Void); - ScopedValue in_place(in_place_, {init, GetPlacementReceiver(name)}); - auto construct = std::format("{};", ToString(temporary->IgnoreImplicit())); + auto storage = CreatePlacementStorage(init->getType(), name); + std::string construct; + { + Buffer buf(*this); + ConvertIntoStorage(const_cast(init), + GetPlacementReceiver(name)); + construct = std::format("{};", std::move(buf).str()); + } if (!declarations) { return storage + construct; } @@ -2346,19 +2353,7 @@ void Converter::ConvertVAArgCall(clang::CallExpr *expr) { } bool Converter::VisitCallExpr(clang::CallExpr *expr) { - if (IsReturnSlotCall(expr) && in_place_.expr != expr) { - PushBrace block(*this); - StrCat(CreatePlacementStorage( - GetRecordName(expr->getType()->getAsCXXRecordDecl()))); - { - ScopedValue in_place(in_place_, {expr, GetPlacementReceiver()}); - Convert(expr); - } - StrCat(token::kSemiColon); - if (!isVoid()) { - StrCat(TakePlacementValue()); - } - MarkFresh(); + if (IsReturnSlotCall(expr) && ConvertInBlockStorage(expr)) { return false; } if (ConvertSpecialCall(expr)) { @@ -3237,6 +3232,23 @@ bool Converter::IsCastRedundantInRust(clang::Expr *expr, return GetUnsafeTypeAsString(expr->getType()) == target; } +bool Converter::VisitCXXBindTemporaryExpr(clang::CXXBindTemporaryExpr *expr) { + ForwardsStorage(expr, expr->getSubExpr()); + return true; +} + +bool Converter::VisitCastExpr(clang::CastExpr *expr) { + bool forwards = expr->getCastKind() == clang::CK_ConstructorConversion || + expr->getCastKind() == clang::CK_UserDefinedConversion || + (expr->getCastKind() == clang::CK_NoOp && expr->isPRValue() && + expr->getSubExpr()->isPRValue()); + if (forwards && ForwardsStorage(expr, expr->getSubExpr())) { + Convert(expr->getSubExpr()); + return false; + } + return true; +} + bool Converter::VisitImplicitCastExpr(clang::ImplicitCastExpr *expr) { auto *sub_expr = expr->getSubExpr(); auto type = expr->getType(); @@ -3728,6 +3740,9 @@ void Converter::ConvertBinaryOperator(clang::BinaryOperator *expr) { return; } + if (ForwardsStorage(expr, rhs)) { + in_place_.object_tail = nullptr; + } PushBrace brace(*this); StrCat(effect, token::kSemiColon); if (isRValue()) { @@ -4083,6 +4098,10 @@ bool Converter::ConvertLValueConditional(clang::ConditionalOperator *expr) { } bool Converter::VisitConditionalOperator(clang::ConditionalOperator *expr) { + if (in_place_.expr == expr) { + ConvertConditionalIntoStorage(expr); + return false; + } if (ConvertLValueConditional(expr)) { return false; } @@ -4173,6 +4192,7 @@ bool Converter::VisitDeclRefExpr(clang::DeclRefExpr *expr) { constant_evaluated_, HasConstantEvaluatedInit(ctx_, clang::cast(expr->getDecl()))); + PushParen paren(*this); Convert(init); return false; } @@ -4221,6 +4241,10 @@ void Converter::ConvertDeclRefValue(clang::Expr *expr, clang::ValueDecl *decl) { } bool Converter::VisitParenExpr(clang::ParenExpr *expr) { + if (ForwardsStorage(expr, expr->getSubExpr())) { + Convert(expr->getSubExpr()); + return false; + } if (auto *bin = clang::dyn_cast(expr->getSubExpr()); bin && (bin->isCommaOp() || (bin->isAssignmentOp() && isVoid()))) { Convert(expr->getSubExpr()); @@ -4569,29 +4593,41 @@ bool Converter::VisitOpaqueValueExpr(clang::OpaqueValueExpr *expr) { Convert(expr->getSourceExpr()); return false; } - ScopedValue depth(array_init_depth_, outer->second); + ScopedValue indices(array_init_indices_, + std::vector(array_init_indices_.begin(), + array_init_indices_.begin() + outer->second)); Convert(expr->getSourceExpr()); return false; } -std::string Converter::ArrayInitIndexName() const { - return array_init_depth_ == 1 ? "__i" - : std::format("__i{}", array_init_depth_); -} - bool Converter::VisitArrayInitIndexExpr(clang::ArrayInitIndexExpr *expr) { - EmitFresh(ArrayInitIndexName()); + EmitFresh(array_init_indices_.back()); return false; } bool Converter::VisitArrayInitLoopExpr(clang::ArrayInitLoopExpr *expr) { + if (BuildsInStorage(expr)) { + if (!ConvertInBlockStorage(expr)) { + auto receiver = in_place_.receiver; + array_init_outer_depth_[expr->getCommonExpr()] = + array_init_indices_.size(); + ConvertElementsIntoStorage(receiver, 0, GetArraySize(expr->getType()), + [&](std::string_view element) { + ConvertIntoStorage(expr->getSubExpr(), + std::string(element)); + }); + } + return false; + } StrCat(std::format("std::array::from_fn::<_, {}, _>", GetArraySize(expr->getType()))); PushParen paren(*this); - array_init_outer_depth_[expr->getCommonExpr()] = array_init_depth_++; - StrCat(std::format("|{}: usize|", ArrayInitIndexName())); + auto depth = array_init_indices_.size(); + array_init_outer_depth_[expr->getCommonExpr()] = depth; + ScopedPush index(array_init_indices_, + depth == 0 ? "__i" : std::format("__i{}", depth + 1)); + StrCat(std::format("|{}: usize|", array_init_indices_.back())); ConvertVarInit(expr->getSubExpr()->getType(), expr->getSubExpr()); - --array_init_depth_; MarkFresh(); return false; } @@ -4600,12 +4636,24 @@ bool Converter::VisitInitListExpr(clang::InitListExpr *expr) { auto *syntactic = expr->isSyntacticForm() ? expr : expr->getSyntacticForm(); if (auto form = expr->getSemanticForm()) expr = form; + if (in_place_.expr == syntactic) { + in_place_.expr = expr; + } if (expr->isTransparent()) { + ForwardsStorage(expr, expr->getInit(0)); Convert(expr->getInit(0)); return false; } + if (BuildsInStorage(expr)) { + if (!ConvertInBlockStorage(expr)) { + auto receiver = in_place_.receiver; + ConvertInitListIntoStorage(expr, receiver); + } + return false; + } + auto qual_type = expr->getType(); if (qual_type->isScalarType()) { ConvertScalarInitList(expr); @@ -4819,16 +4867,14 @@ bool Converter::VisitCXXNewExpr(clang::CXXNewExpr *expr) { if (!curr_init_type_.empty() && curr_init_type_.back()->isPointerType()) { StrCat(".as_mut_ptr()"); } - } else if (auto *construct = - expr->getInitializer() - ? GetPlacementInitializer(expr->getInitializer()) - : nullptr) { + } else if (expr->getInitializer() && + BuildsInStorage(expr->getInitializer())) { PushBrace block(*this); - StrCat( - std::format("let __p: *mut {0} = Box::leak(Box::new(<{0}>::" - "zero_init()));", - GetRecordName(construct->getType()->getAsCXXRecordDecl()))); - ConstructInPlace(expr->getInitializer(), construct, "__p"); + StrCat(std::format( + "let __p: *mut {0} = Box::leak(Box::new(<{0}>::" + "zero_init()));", + GetRecordName(expr->getAllocatedType()->getAsCXXRecordDecl()))); + ConstructInPlace(expr->getInitializer(), "__p"); StrCat("__p"); } else if (pointee) { StrCat( @@ -4898,23 +4944,38 @@ void Converter::EmitDeallocation(clang::CXXDeleteExpr *expr, } } +void Converter::ConvertArrayConstruct(clang::CXXConstructExpr *expr) { + if (!BuildsInStorage(expr)) { + ConvertArrayCXXConstructExpr(expr); + return; + } + if (!ConvertInBlockStorage(expr)) { + auto receiver = in_place_.receiver; + ConvertArrayConstructIntoStorage(expr, expr->getType(), receiver); + } +} + void Converter::ConvertArrayCXXConstructExpr(clang::CXXConstructExpr *expr) { StrCat(std::format("std::array::from_fn::<_, {}, _>", GetArraySize(expr->getType()))); PushParen paren(*this); StrCat("|_|"); - ConvertCXXConstructExprArgs(expr); + Convert(GetElementConstruct(expr)); +} + +clang::CXXConstructExpr * +Converter::GetElementConstruct(clang::CXXConstructExpr *expr) { + return MakeConstructExpr(ctx_, ctx_.getBaseElementType(expr->getType()), + expr->getConstructor(), + llvm::ArrayRef(expr->getArgs(), expr->getNumArgs())); } void Converter::ConvertCXXConstructExprArgs(clang::CXXConstructExpr *expr) { + if (ConvertInBlockStorage(expr)) { + return; + } HoistMaterializedTempBindings hoist_temps(*this, /*as_block=*/true); auto ctor = expr->getConstructor(); - bool in_place = in_place_.expr == expr; - std::optional block; - if (!in_place) { - block.emplace(*this); - StrCat(CreatePlacementStorage(GetRecordName(ctor->getParent()))); - } bool is_derived = IsDerivedCopyOrMove(ctor); bool is_init = is_derived || GetInitMethod(ctor).has_value(); std::string args; @@ -4943,7 +5004,7 @@ void Converter::ConvertCXXConstructExprArgs(clang::CXXConstructExpr *expr) { } args += std::format("{}{}", i ? ", " : "", converted); } - if (in_place && in_place_.object_tail && hoist_temps.empty() && + if (in_place_.object_tail && hoist_temps.empty() && full_expression_temporaries_->empty() && !is_derived) { auto record_name = GetRecordName(ctor->getParent()); if (auto name = GetCtorName(ctor); name == kConstructName) { @@ -4961,55 +5022,173 @@ void Converter::ConvertCXXConstructExprArgs(clang::CXXConstructExpr *expr) { GetRecordTrait(ctor)->method) : std::format("{}::{}", GetRecordName(ctor->getParent()), GetCtorName(ctor)); - StrCat(std::format("{}({}{}{})", path, - in_place ? in_place_.receiver : GetPlacementReceiver(), + StrCat(std::format("{}({}{}{})", path, in_place_.receiver, args.empty() ? "" : ", ", args)); - if (!in_place) { - StrCat(token::kSemiColon, TakePlacementValue()); +} + +void Converter::ConstructInPlace(clang::Expr *init, std::string receiver) { + ConvertFullExpression([&] { ConvertIntoStorage(init, std::move(receiver)); }); + StrCat(token::kSemiColon); +} + +void Converter::ConvertIntoStorage(clang::Expr *expr, std::string receiver, + std::optional *object_tail) { + PushExprKind push(*this, ExprKind::Void); + ScopedValue in_place(in_place_, {expr, std::move(receiver), object_tail}); + Convert(expr); +} + +bool Converter::ForwardsStorage(const clang::Expr *expr, + const clang::Expr *operand) { + if (in_place_.expr != expr) { + return false; } + in_place_.expr = operand; + return true; +} + +void Converter::ConvertConditionalIntoStorage( + clang::ConditionalOperator *expr) { + auto receiver = in_place_.receiver; + auto branch = [&](clang::Expr *branch_expr) { + PushBrace brace(*this); + BuildOrWriteIntoStorage(branch_expr->getType(), branch_expr, receiver); + StrCat(token::kSemiColon); + }; + StrCat(keyword::kIf); + ConvertIfCondition(expr->getCond()); + branch(expr->getTrueExpr()); + StrCat(keyword::kElse); + branch(expr->getFalseExpr()); } -void Converter::ConstructInPlace(clang::Expr *init, - const clang::Expr *construct, - std::string receiver) { - ScopedValue in_place(in_place_, {construct, std::move(receiver)}); - ConvertFullExpression([&] { Convert(init); }); +void Converter::BuildOrWriteIntoStorage(clang::QualType type, clang::Expr *expr, + std::string_view receiver) { + if (BuildsInStorage(expr)) { + ConvertIntoStorage(expr, std::string(receiver)); + return; + } + std::string value; + { + Buffer buf(*this); + ConvertStoredValue(type, expr); + value = std::move(buf).str(); + } + StrCat(std::format("({}).write({})", receiver, value)); +} + +void Converter::ConvertStoredValue(clang::QualType type, clang::Expr *expr) { + PushExprKind push(*this, ExprKind::RValue); + ConvertVarInit(type, expr); +} + +void Converter::ConvertInitListIntoStorage(const clang::InitListExpr *list, + std::string_view receiver) { + PushBrace block(*this); + auto element = [&](clang::QualType type, const clang::Expr *init, + std::string_view element_receiver) { + BuildOrWriteIntoStorage(type, const_cast(init), + element_receiver); + StrCat(token::kSemiColon); + }; + if (auto *array_type = ctx_.getAsConstantArrayType(list->getType())) { + auto element_type = array_type->getElementType(); + unsigned index = 0; + for (auto *init : list->inits()) { + element(element_type, init, + GetElementReceiver(receiver, std::to_string(index++))); + } + if (list->hasArrayFiller()) { + ConvertElementsIntoStorage( + receiver, index, array_type->getSize().getZExtValue(), + [&](std::string_view element_receiver) { + element(element_type, list->getArrayFiller(), element_receiver); + }); + } + return; + } + auto *record = list->getType()->getAsRecordDecl(); + if (auto *cxx_record = clang::dyn_cast(record)) { + assert(cxx_record->getNumBases() == 0); + } + unsigned index = 0; + for (const auto *field : record->fields()) { + assert(index < list->getNumInits()); + element(field->getType(), list->getInit(index++), + GetFieldReceiver(receiver, GetNamedDeclAsString(field))); + } +} + +void Converter::ConvertArrayConstructIntoStorage(clang::CXXConstructExpr *expr, + clang::QualType type, + std::string_view receiver) { + auto element_type = ctx_.getAsConstantArrayType(type)->getElementType(); + ConvertElementsIntoStorage( + receiver, 0, GetArraySize(type), [&](std::string_view element) { + if (element_type->isArrayType()) { + ConvertArrayConstructIntoStorage(expr, element_type, element); + } else { + ConvertIntoStorage(GetElementConstruct(expr), std::string(element)); + } + }); +} + +void Converter::ConvertElementsIntoStorage( + std::string_view receiver, uint64_t begin, uint64_t end, + const std::function &element) { + if (begin == end) { + return; + } + ScopedPush index(array_init_indices_, NewTempName()); + StrCat( + std::format("for {} in {}..{}", array_init_indices_.back(), begin, end)); + PushBrace body(*this); + element(GetElementReceiver(receiver, array_init_indices_.back())); StrCat(token::kSemiColon); } -const clang::Expr *Converter::GetInPlaceVarConstruct(clang::VarDecl *decl) { - if (!decl->isLocalVarDecl() || decl->isStaticLocal() || IsGlobalVar(decl) || - !decl->hasInit() || clang::isa(decl) || - !CanConstructVarInPlace(decl)) { - return nullptr; +bool Converter::ConvertInBlockStorage(clang::Expr *producer) { + assert(BuildsInStorage(producer)); + if (in_place_.expr == producer) { + return false; + } + PushBrace block(*this); + StrCat(CreatePlacementStorage(producer->getType())); + ConvertIntoStorage(producer, GetPlacementReceiver()); + StrCat(token::kSemiColon); + if (!isVoid()) { + StrCat(TakePlacementValue()); } - return GetPlacementInitializer(decl->getInit()); + MarkFresh(); + return true; } -void Converter::EmitLocalObject(clang::VarDecl *decl, - const clang::Expr *construct) { +bool Converter::ConstructsVarInPlace(clang::VarDecl *decl) { + return decl->isLocalVarDecl() && !decl->isStaticLocal() && + !IsGlobalVar(decl) && decl->hasInit() && + CanConstructVarInPlace(decl) && BuildsInStorage(decl->getInit()); +} + +void Converter::EmitLocalObject(clang::VarDecl *decl) { auto name = GetNamedDeclAsString(decl); + if (decl->getType()->isArrayType()) { + StrCat(CreatePlacementStorage(decl->getType(), name)); + ConstructInPlace(decl->getInit(), GetPlacementReceiver(name)); + return; + } std::optional tail; std::string declarations; std::string init; { Buffer buf(*this); - // The full expression of the initializer, whose wrappers of a constructor - // call (parentheses, casts to its type) emit nothing else. ScopedValue scope(full_expression_temporaries_, &declarations); - // Any other construction gets the storage as a reference named name. - ScopedValue in_place(in_place_, - {construct, GetStorageReceiver(name), &tail}); - auto *init_expr = clang::isa(construct) - ? const_cast(construct) - : decl->getInit(); - Convert(init_expr); + ConvertIntoStorage(decl->getInit(), GetStorageReceiver(name), &tail); init = std::move(buf).str(); } if (!tail) { tail = std::format( "{}, |{}| {}", - GetRecordName(construct->getType()->getAsCXXRecordDecl()), name, + GetRecordName(decl->getInit()->getType()->getAsCXXRecordDecl()), name, declarations.empty() ? init : std::format("{{ {}{} }}", declarations, init)); } @@ -5039,11 +5218,11 @@ const char *Converter::GetConstructorSelf() const { return "this: *mut Self"; } const char *Converter::GetConstructorWrite() const { return "this.write"; } -std::string Converter::CreatePlacementStorage(const std::string &record_name, - std::string_view name) const { - return std::format( - "let mut {1}: {0} = ZeroInitializationUnsafe::zero_init();", record_name, - name); +std::string Converter::CreatePlacementStorage(clang::QualType type, + std::string_view name) { + auto *record = type->getAsRecordDecl(); + return std::format("let mut {}: {} = ZeroInitializationUnsafe::zero_init();", + name, record ? GetRecordName(record) : ToString(type)); } std::string Converter::GetPlacementReceiver(std::string_view name) const { @@ -5051,7 +5230,17 @@ std::string Converter::GetPlacementReceiver(std::string_view name) const { } std::string Converter::GetFieldPlacementReceiver(std::string_view field) const { - return std::format("&raw mut (*this).{}", field); + return GetFieldReceiver("this", field); +} + +std::string Converter::GetFieldReceiver(std::string_view receiver, + std::string_view field) const { + return std::format("&raw mut (*{}).{}", receiver, field); +} + +std::string Converter::GetElementReceiver(std::string_view receiver, + std::string_view index) const { + return std::format("&raw mut (*{})[{}]", receiver, index); } const char *Converter::GetDelegatingReceiver() const { return "this"; } @@ -5125,7 +5314,7 @@ bool Converter::VisitCXXConstructExpr(clang::CXXConstructExpr *expr) { } if (expr->getType()->isArrayType()) { - ConvertArrayCXXConstructExpr(expr); + ConvertArrayConstruct(expr); } else { ConvertCXXConstructExprArgs(expr); } @@ -5442,6 +5631,7 @@ bool Converter::VisitSwitchStmt(clang::SwitchStmt *stmt) { // TODO: right now defaults go into the constructor, but they should also be // placed in the Default trait impl. bool Converter::VisitCXXDefaultInitExpr(clang::CXXDefaultInitExpr *expr) { + ForwardsStorage(expr, expr->getExpr()); Convert(expr->getExpr()); return false; } @@ -6348,6 +6538,7 @@ void Converter::AddDefaultTrait(const clang::RecordDecl *decl) { } if (auto *default_ctor = cxx ? GetUserDefinedDefaultConstructor(cxx) : nullptr) { + PushExprKind push(*this, ExprKind::RValue); Convert(MakeDefaultConstructExpr(cxx, default_ctor)); } else { EmitDefaultStructLiteral(decl); @@ -6377,8 +6568,7 @@ void Converter::AddDefaultConstructibleTrait(const clang::CXXRecordDecl *decl, PushBrace fn_brace(*this); StrCat(keyword_unsafe_); PushBrace unsafe_brace(*this); - auto *construct = MakeDefaultConstructExpr(decl, ctor); - ConstructInPlace(construct, construct, spelling.receiver); + ConstructInPlace(MakeDefaultConstructExpr(decl, ctor), spelling.receiver); } } @@ -6842,6 +7032,7 @@ Converter::ConvertMoveConstruction(clang::Expr *arg) { return std::nullopt; } Buffer buf(*this); + PushExprKind push(*this, ExprKind::FreshRValue); Convert(init); return std::move(buf).str(); } diff --git a/cpp2rust/converter/converter.h b/cpp2rust/converter/converter.h index 44dbc79de1..f70c8dd90a 100644 --- a/cpp2rust/converter/converter.h +++ b/cpp2rust/converter/converter.h @@ -129,6 +129,10 @@ class Converter : public clang::RecursiveASTVisitor { bool VisitRecoveryExpr(clang::RecoveryExpr *expr); bool VisitExprWithCleanups(clang::ExprWithCleanups *expr); + + bool VisitCXXBindTemporaryExpr(clang::CXXBindTemporaryExpr *expr); + + bool VisitCastExpr(clang::CastExpr *expr); bool VisitMaterializeTemporaryExpr(clang::MaterializeTemporaryExpr *expr); virtual void EmitFilePreamble(); @@ -287,11 +291,8 @@ class Converter : public clang::RecursiveASTVisitor { virtual const char *GetConstructorWrite() const; - // A variable named name that holds zero-initialized storage for a record, - // in which a constructor constructs the record. - virtual std::string - CreatePlacementStorage(const std::string &record_name, - std::string_view name = "__v") const; + virtual std::string CreatePlacementStorage(clang::QualType type, + std::string_view name = "__v"); // The receiver of a constructor that constructs the record in the storage of // the variable named name. @@ -301,6 +302,12 @@ class Converter : public clang::RecursiveASTVisitor { // the converted constructor constructs. virtual std::string GetFieldPlacementReceiver(std::string_view field) const; + virtual std::string GetFieldReceiver(std::string_view receiver, + std::string_view field) const; + + virtual std::string GetElementReceiver(std::string_view receiver, + std::string_view index) const; + // The receiver of the constructor that a delegating constructor calls. virtual const char *GetDelegatingReceiver() const; @@ -770,8 +777,12 @@ class Converter : public clang::RecursiveASTVisitor { void ConvertCXXConstructExprArgs(clang::CXXConstructExpr *expr); + void ConvertArrayConstruct(clang::CXXConstructExpr *expr); + virtual void ConvertArrayCXXConstructExpr(clang::CXXConstructExpr *expr); + clang::CXXConstructExpr *GetElementConstruct(clang::CXXConstructExpr *expr); + virtual bool VisitUnaryExprOrTypeTraitExpr(clang::UnaryExprOrTypeTraitExpr *expr); @@ -1276,14 +1287,13 @@ class Converter : public clang::RecursiveASTVisitor { bool DeferTemporaries(const clang::Expr *call, const TempMaterializationCtx *temps); - // The number of array init loops being converted, which copy arrays + // The indices of the array init loops being converted, which copy arrays // element by element, e.g., in implicit copy constructors, and the number // of loops outside each of them, whose indices their common expressions // use. - unsigned array_init_depth_ = 0; + std::vector array_init_indices_; std::unordered_map array_init_outer_depth_; - std::string ArrayInitIndexName() const; unsigned materialized_temp_id_ = 0; std::string NewTempName() { @@ -1302,10 +1312,6 @@ class Converter : public clang::RecursiveASTVisitor { // Owning temporaries needed by references bound before a rule expansion. std::string *full_expression_temporaries_ = nullptr; - // A construction that constructs its object in place, in the storage that - // receiver gives to the constructor. A local object also gets, in - // object_tail, the arguments of the object! macro after its name, if the - // constructor call is all that its construction emits. struct InPlaceConstruct { const clang::Expr *expr = nullptr; std::string receiver; @@ -1328,18 +1334,36 @@ class Converter : public clang::RecursiveASTVisitor { const clang::Expr *init, std::string *declarations); - // Converts init, which constructs an object by construct, to a statement that - // constructs the object in the storage that receiver gives. - void ConstructInPlace(clang::Expr *init, const clang::Expr *construct, - std::string receiver); + void ConstructInPlace(clang::Expr *init, std::string receiver); + + void ConvertIntoStorage(clang::Expr *expr, std::string receiver, + std::optional *object_tail = nullptr); + + bool ForwardsStorage(const clang::Expr *expr, const clang::Expr *operand); + + void ConvertConditionalIntoStorage(clang::ConditionalOperator *expr); + + void BuildOrWriteIntoStorage(clang::QualType type, clang::Expr *expr, + std::string_view receiver); + + virtual void ConvertStoredValue(clang::QualType type, clang::Expr *expr); + + void ConvertInitListIntoStorage(const clang::InitListExpr *list, + std::string_view receiver); + + void ConvertArrayConstructIntoStorage(clang::CXXConstructExpr *expr, + clang::QualType type, + std::string_view receiver); + + void ConvertElementsIntoStorage( + std::string_view receiver, uint64_t begin, uint64_t end, + const std::function &element); + + bool ConvertInBlockStorage(clang::Expr *producer); - // The construction of decl by its initializer, if it can be done in the - // storage of decl. - const clang::Expr *GetInPlaceVarConstruct(clang::VarDecl *decl); + bool ConstructsVarInPlace(clang::VarDecl *decl); - // Declares decl, which construct constructs in place, with the object! - // macro, which also destroys it at the end of its scope. - void EmitLocalObject(clang::VarDecl *decl, const clang::Expr *construct); + void EmitLocalObject(clang::VarDecl *decl); bool retain_bound_temporaries_ = false; class HoistMaterializedTempBindings { Converter &c; diff --git a/cpp2rust/converter/converter_lib.cpp b/cpp2rust/converter/converter_lib.cpp index 88fc850a5f..52c32086cf 100644 --- a/cpp2rust/converter/converter_lib.cpp +++ b/cpp2rust/converter/converter_lib.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -1314,23 +1315,6 @@ bool IsPassThroughConstructor(const clang::CXXConstructorDecl *ctor) { IsRValueConvertingConstructor(ctor)); } -static const clang::Expr *IgnorePlacementWrappers(const clang::Expr *init) { - while (true) { - init = init->IgnoreParens(); - if (auto *cleanups = clang::dyn_cast(init)) { - init = cleanups->getSubExpr(); - } else if (auto *default_init = - clang::dyn_cast(init)) { - init = default_init->getExpr(); - } else if (auto *cast = clang::dyn_cast(init); - cast && cast->getCastKind() == clang::CK_ConstructorConversion) { - init = cast->getSubExpr(); - } else { - return init; - } - } -} - bool UsesReturnSlot(clang::QualType type) { auto *record = type->getAsCXXRecordDecl(); return record && record->hasDefinition() && IsUserDefinedDecl(record) && @@ -1346,48 +1330,126 @@ bool IsReturnSlotCall(const clang::Expr *expr) { return !callee || IsUserDefinedDecl(callee); } -const clang::Expr *GetPlacementInitializer(const clang::Expr *init) { - auto *call = IgnorePlacementWrappers(init); - if (auto *bind = clang::dyn_cast(call)) { - call = bind->getSubExpr(); +static bool IsStorageConstructor(const clang::CXXConstructorDecl *ctor) { + auto *parent = ctor->getParent(); + if (parent->isLambda() || !IsUserDefinedDecl(parent)) { + return false; } - if (IsReturnSlotCall(call)) { - return call; + if (ctor->isCopyOrMoveConstructor()) { + return IsConvertibleCopyOrMoveConstructor(ctor); } - return GetInPlaceConstruct(init); + return ctor->isUserProvided() || IsConvertibleDefaultConstructor(ctor); +} + +static bool IsStorageConstruct(const clang::CXXConstructExpr *expr) { + return !expr->isElidable() && !expr->getType()->isArrayType() && + IsStorageConstructor(expr->getConstructor()); +} + +static bool IsStorageArrayConstruct(const clang::CXXConstructExpr *expr) { + return expr->getType()->isConstantArrayType() && + IsStorageConstructor(expr->getConstructor()); +} + +const clang::InitListExpr *GetSemanticForm(const clang::InitListExpr *list) { + if (auto *semantic = list->getSemanticForm()) { + return semantic; + } + return list; +} + +bool IsStorageInitList(const clang::InitListExpr *list) { + if (list->isTransparent() || IsInitExprOfStringLiteral(list)) { + return false; + } + if (list->getType()->isConstantArrayType()) { + return true; + } + auto *record = list->getType()->getAsRecordDecl(); + return record && !record->isUnion() && IsUserDefinedDecl(record); +} + +namespace { +class StorageTarget : public clang::ConstStmtVisitor { +public: + bool VisitStmt(const clang::Stmt *) { return false; } + + bool VisitParenExpr(const clang::ParenExpr *expr) { + return Visit(expr->getSubExpr()); + } + + bool VisitExprWithCleanups(const clang::ExprWithCleanups *expr) { + return Visit(expr->getSubExpr()); + } + + bool VisitCXXBindTemporaryExpr(const clang::CXXBindTemporaryExpr *expr) { + return Visit(expr->getSubExpr()); + } + + bool VisitCXXDefaultInitExpr(const clang::CXXDefaultInitExpr *expr) { + return Visit(expr->getExpr()); + } + + bool VisitCastExpr(const clang::CastExpr *expr) { + return (expr->getCastKind() == clang::CK_ConstructorConversion || + expr->getCastKind() == clang::CK_UserDefinedConversion || + (expr->getCastKind() == clang::CK_NoOp && expr->isPRValue() && + expr->getSubExpr()->isPRValue())) && + Visit(expr->getSubExpr()); + } + + bool VisitBinComma(const clang::BinaryOperator *expr) { + return Visit(expr->getRHS()); + } + + bool VisitConditionalOperator(const clang::ConditionalOperator *expr) { + return expr->isPRValue() && expr->getType()->isRecordType() && + (Visit(expr->getTrueExpr()) || Visit(expr->getFalseExpr())); + } + + bool VisitCXXConstructExpr(const clang::CXXConstructExpr *expr) { + return IsStorageConstruct(expr) || IsStorageArrayConstruct(expr); + } + + bool VisitInitListExpr(const clang::InitListExpr *expr) { + auto *list = GetSemanticForm(expr); + if (list->isTransparent()) { + return Visit(list->getInit(0)); + } + if (!IsStorageInitList(list)) { + return false; + } + return llvm::any_of(list->inits(), + [&](const clang::Expr *init) { return Visit(init); }) || + (list->hasArrayFiller() && Visit(list->getArrayFiller())); + } + + bool VisitArrayInitLoopExpr(const clang::ArrayInitLoopExpr *expr) { + return Visit(expr->getSubExpr()); + } + + bool VisitCallExpr(const clang::CallExpr *expr) { + return IsReturnSlotCall(expr); + } +}; +} // namespace + +bool BuildsInStorage(const clang::Expr *expr) { + return StorageTarget().Visit(expr); } const clang::Expr * GetTemporaryPlacementInitializer(const clang::ASTContext &ctx, const clang::Expr *temporary, clang::QualType type) { - auto *init = GetPlacementInitializer(temporary->IgnoreImplicit()); - if (!init || !ctx.hasSameUnqualifiedType(init->getType(), type)) { + auto *init = temporary->IgnoreImplicit(); + if (!BuildsInStorage(init) || + !ctx.hasSameUnqualifiedType(init->getType(), type)) { return nullptr; } return init; } -const clang::CXXConstructExpr *GetInPlaceConstruct(const clang::Expr *init) { - init = IgnorePlacementWrappers(init); - auto *construct = clang::dyn_cast(init); - if (!construct || construct->isElidable() || - construct->getType()->isArrayType()) { - return nullptr; - } - auto *ctor = construct->getConstructor(); - auto *parent = ctor->getParent(); - if (parent->isLambda() || !IsUserDefinedDecl(parent)) { - return nullptr; - } - if (ctor->isCopyOrMoveConstructor()) { - return IsConvertibleCopyOrMoveConstructor(ctor) ? construct : nullptr; - } - return ctor->isUserProvided() || IsConvertibleDefaultConstructor(ctor) - ? construct - : nullptr; -} - bool IsConvertibleCXXRecordDecl(const clang::CXXRecordDecl *decl) { return decl->isThisDeclarationADefinition() && !decl->isDependentContext() && !decl->isInvalidDecl(); @@ -1660,12 +1722,12 @@ unsigned GetArraySize(clang::QualType array_type) { } static std::string GetLocationID(const clang::Decl *decl) { - auto id = GetFileName(decl) + std::to_string(GetLineNumber(decl)) + - std::to_string(GetColumnNumber(decl)); + auto id = std::format("{}:{}:{}", GetFileName(decl), GetLineNumber(decl), + GetColumnNumber(decl)); if (auto loc = decl->getLocation(); loc.isMacroID()) { const auto &src_mgr = decl->getASTContext().getSourceManager(); - id += std::to_string(src_mgr.getSpellingLineNumber(loc)) + - std::to_string(src_mgr.getSpellingColumnNumber(loc)); + id += std::format(":{}:{}", src_mgr.getSpellingLineNumber(loc), + src_mgr.getSpellingColumnNumber(loc)); } return id; } diff --git a/cpp2rust/converter/converter_lib.h b/cpp2rust/converter/converter_lib.h index 51b08bcb1c..99df9ccc3b 100644 --- a/cpp2rust/converter/converter_lib.h +++ b/cpp2rust/converter/converter_lib.h @@ -345,16 +345,15 @@ bool HasDefaultedCopyAssignment(const clang::RecordDecl *decl); bool IsRValueConvertingConstructor(const clang::CXXConstructorDecl *ctor); -// The call of a translated constructor that init, the initializer of an -// object, is. The constructor can construct the object in its storage, as it -// is translated to a function that receives the storage. -const clang::CXXConstructExpr *GetInPlaceConstruct(const clang::Expr *init); - bool UsesReturnSlot(clang::QualType type); bool IsReturnSlotCall(const clang::Expr *expr); -const clang::Expr *GetPlacementInitializer(const clang::Expr *init); +const clang::InitListExpr *GetSemanticForm(const clang::InitListExpr *list); + +bool IsStorageInitList(const clang::InitListExpr *list); + +bool BuildsInStorage(const clang::Expr *expr); const clang::Expr * GetTemporaryPlacementInitializer(const clang::ASTContext &ctx, diff --git a/cpp2rust/converter/models/boxed_vars.cpp b/cpp2rust/converter/models/boxed_vars.cpp index a0d63e9b61..e0e7fd4985 100644 --- a/cpp2rust/converter/models/boxed_vars.cpp +++ b/cpp2rust/converter/models/boxed_vars.cpp @@ -160,11 +160,9 @@ class AddressTakenVisitor bool TraverseDecltypeType(clang::DecltypeType *, bool) { return true; } - // A variable that a translated constructor constructs is passed to the - // constructor as `this`, which constructs it in place. bool VisitVarDecl(clang::VarDecl *decl) { if (decl->isLocalVarDecl() && decl->hasInit() && - GetPlacementInitializer(decl->getInit())) { + BuildsInStorage(decl->getInit())) { AddressTaken(decl); } return true; diff --git a/cpp2rust/converter/models/converter_refcount.cpp b/cpp2rust/converter/models/converter_refcount.cpp index 3435b57a9b..f1580613ce 100644 --- a/cpp2rust/converter/models/converter_refcount.cpp +++ b/cpp2rust/converter/models/converter_refcount.cpp @@ -557,28 +557,29 @@ std::pair ConverterRefCount::MaterializeTemp( } auto pointee = param_type.getNonReferenceType(); bool is_static = in_const_initializer_ && !declarations; + std::string binding; if (auto *init = GetTemporaryPlacementInitializer(ctx_, expr, pointee); init && !is_static) { - return {ConstructTemporaryInPlace(binding_name, expr, init, declarations), - binding_name + ".as_pointer()"}; - } - const auto *decl = is_static ? keyword::kStatic : keyword::kLet; - std::string type, value; - if (pointee->isConstantArrayType()) { - PushConversionKind push(*this, ConversionKind::FullRefCount); - type = ToString(pointee); - value = BoxValue(ConvertVarInitValue(pointee, expr)); - } else { - auto unboxed = ConvertFreshRValue(expr, pointee); - type = std::format("Value<{}>", ToStringBase(pointee)); - value = std::format("Rc::new(RefCell::new({}))", unboxed); - } - std::string binding; - if (declarations) { - *declarations += std::format("let {} : {};", binding_name, type); - binding = std::format("{} = {};", binding_name, value); + binding = ConstructTemporaryInPlace(binding_name, expr, init, declarations); } else { - binding = std::format("{} {} : {} = {};", decl, binding_name, type, value); + const auto *decl = is_static ? keyword::kStatic : keyword::kLet; + std::string type, value; + if (pointee->isConstantArrayType()) { + PushConversionKind push(*this, ConversionKind::FullRefCount); + type = ToString(pointee); + value = BoxValue(ConvertVarInitValue(pointee, expr)); + } else { + auto unboxed = ConvertFreshRValue(expr, pointee); + type = std::format("Value<{}>", ToStringBase(pointee)); + value = std::format("Rc::new(RefCell::new({}))", unboxed); + } + if (declarations) { + *declarations += std::format("let {} : {};", binding_name, type); + binding = std::format("{} = {};", binding_name, value); + } else { + binding = + std::format("{} {} : {} = {};", decl, binding_name, type, value); + } } auto ref = is_static ? ".with(Value::as_pointer)" : ".as_pointer()"; // A reference to an array is a pointer to its first element. @@ -660,8 +661,9 @@ ConverterRefCount::GetComparisonArg(const clang::ParmVarDecl *param, auto record = GetRecordName(decl); return std::format("{{ {} <{} as CopyConstructible>::copy_constructor({}, " "LvalueRef::bind({}.clone())); {} }}", - CreatePlacementStorage(record), record, - GetPlacementReceiver(), value, TakePlacementValue()); + CreatePlacementStorage(ctx_.getCanonicalTagType(decl)), + record, GetPlacementReceiver(), value, + TakePlacementValue()); } std::string ConverterRefCount::GetComparisonResultReceiver( @@ -2038,15 +2040,13 @@ bool ConverterRefCount::VisitCXXNewExpr(clang::CXXNewExpr *expr) { array_size_as_string, default_alloc_type_as_string, alloc_type_as_string)); } - } else if (auto *construct = - expr->getInitializer() - ? GetPlacementInitializer(expr->getInitializer()) - : nullptr) { + } else if (expr->getInitializer() && + BuildsInStorage(expr->getInitializer())) { PushBrace block(*this); - StrCat( - std::format("let __p = Ptr::alloc(<{}>::zero_init());", - GetRecordName(construct->getType()->getAsCXXRecordDecl()))); - ConstructInPlace(expr->getInitializer(), construct, "__p.clone()"); + StrCat(std::format( + "let __p = Ptr::alloc(<{}>::zero_init());", + GetRecordName(expr->getAllocatedType()->getAsCXXRecordDecl()))); + ConstructInPlace(expr->getInitializer(), "__p.clone()"); StrCat("__p"); } else { StrCat("Ptr::alloc("); @@ -2085,8 +2085,11 @@ bool ConverterRefCount::VisitCXXDeleteExpr(clang::CXXDeleteExpr *expr) { } bool ConverterRefCount::VisitArrayInitLoopExpr(clang::ArrayInitLoopExpr *expr) { + if (BuildsInStorage(expr)) { + return Converter::VisitArrayInitLoopExpr(expr); + } // Arrays are boxed slices, also when they are elements of arrays. - StrCat(array_init_depth_ == 0 ? "Box::new" : "Box::<[_]>::from"); + StrCat(array_init_indices_.empty() ? "Box::new" : "Box::<[_]>::from"); PushParen outer(*this); PushConversionKind push(*this, ConversionKind::Unboxed); return Converter::VisitArrayInitLoopExpr(expr); @@ -2104,7 +2107,7 @@ void ConverterRefCount::ConvertArrayCXXConstructExpr( PushParen inner(*this); StrCat("|_|"); PushConversionKind push(*this, ConversionKind::Unboxed); - ConvertCXXConstructExprArgs(expr); + Convert(GetElementConstruct(expr)); } std::string ConverterRefCount::ConvertStream(clang::Expr *expr) { @@ -2170,7 +2173,7 @@ bool ConverterRefCount::VisitCXXConstructExpr(clang::CXXConstructExpr *expr) { } if (expr->getType()->isArrayType()) { - ConvertArrayCXXConstructExpr(expr); + ConvertArrayConstruct(expr); } else { ConvertCXXConstructExprArgs(expr); } @@ -3662,12 +3665,26 @@ const char *ConverterRefCount::GetConstructorWrite() const { return "this.write"; } -std::string -ConverterRefCount::CreatePlacementStorage(const std::string &record_name, - std::string_view name) const { - return std::format("let {1}: Value<{0}> = " - "Rc::new(RefCell::new(ZeroInitialization::zero_init()));", - record_name, name); +std::string ConverterRefCount::CreatePlacementStorage(clang::QualType type, + std::string_view name) { + std::string type_name; + if (auto *record = type->getAsRecordDecl()) { + type_name = GetRecordName(record); + } else { + PushConversionKind push(*this, ConversionKind::Pointee); + type_name = ToString(type); + } + return std::format("let {}: Value<{}> = Rc::new(RefCell::new({}));", name, + type_name, GetZeroStorageValue(type)); +} + +std::string ConverterRefCount::GetZeroStorageValue(clang::QualType type) { + if (auto *array_type = ctx_.getAsConstantArrayType(type)) { + return std::format("(0..{}).map(|_| {}).collect()", + array_type->getSize().getZExtValue(), + GetZeroStorageValue(array_type->getElementType())); + } + return "ZeroInitialization::zero_init()"; } std::string @@ -3677,7 +3694,24 @@ ConverterRefCount::GetPlacementReceiver(std::string_view name) const { std::string ConverterRefCount::GetFieldPlacementReceiver(std::string_view field) const { - return std::format("field_ptr!({}, {})", SelfName(), field); + return GetFieldReceiver(SelfName(), field); +} + +std::string ConverterRefCount::GetFieldReceiver(std::string_view receiver, + std::string_view field) const { + return std::format("field_ptr!({}, {})", receiver, field); +} + +std::string +ConverterRefCount::GetElementReceiver(std::string_view receiver, + std::string_view index) const { + return std::format("{}.decay().offset({})", receiver, index); +} + +void ConverterRefCount::ConvertStoredValue(clang::QualType type, + clang::Expr *expr) { + PushConversionKind push(*this, ConversionKind::Pointee); + Converter::ConvertStoredValue(type, expr); } const char *ConverterRefCount::GetDelegatingReceiver() const { diff --git a/cpp2rust/converter/models/converter_refcount.h b/cpp2rust/converter/models/converter_refcount.h index b65cd7cfbd..96b83b413d 100644 --- a/cpp2rust/converter/models/converter_refcount.h +++ b/cpp2rust/converter/models/converter_refcount.h @@ -105,15 +105,24 @@ class ConverterRefCount final : public Converter { std::string ConvertConstructorFieldWrite(const std::string &field_name, const std::string &value) override; - std::string - CreatePlacementStorage(const std::string &record_name, - std::string_view name = "__v") const override; + std::string CreatePlacementStorage(clang::QualType type, + std::string_view name = "__v") override; + + std::string GetZeroStorageValue(clang::QualType type); std::string GetPlacementReceiver(std::string_view name = "__v") const override; std::string GetFieldPlacementReceiver(std::string_view field) const override; + std::string GetFieldReceiver(std::string_view receiver, + std::string_view field) const override; + + std::string GetElementReceiver(std::string_view receiver, + std::string_view index) const override; + + void ConvertStoredValue(clang::QualType type, clang::Expr *expr) override; + const char *GetDelegatingReceiver() const override; bool CanConstructVarInPlace(const clang::VarDecl *decl) const override; diff --git a/libcc2rs/src/initialization.rs b/libcc2rs/src/initialization.rs index 926f4a0197..4590946f36 100644 --- a/libcc2rs/src/initialization.rs +++ b/libcc2rs/src/initialization.rs @@ -87,6 +87,13 @@ impl ZeroInitializatio } } +impl ZeroInitializationUnsafe for [T; N] { + #[inline] + unsafe fn zero_init() -> Self { + std::array::from_fn(|_| unsafe { T::zero_init() }) + } +} + impl ZeroInitializationUnsafe for *mut T { #[inline] unsafe fn zero_init() -> Self { diff --git a/tests/std/basic/basic.start/out/refcount/basic.start.term.rs b/tests/std/basic/basic.start/out/refcount/basic.start.term.rs index b55aea53a8..880a61dab4 100644 --- a/tests/std/basic/basic.start/out/refcount/basic.start.term.rs +++ b/tests/std/basic/basic.start/out/refcount/basic.start.term.rs @@ -149,12 +149,12 @@ thread_local!( ); thread_local!( pub static holder_8: Value = { - let __static = Rc::new(RefCell::new(HoldsTracker { - member: { - let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); - Tracker::constructor_1(__v.as_pointer(), 5); - Rc::try_unwrap(__v).ok().unwrap().into_inner() - }, + let __static = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); + { + Tracker::constructor_1(field_ptr!(__v.as_pointer(), member), 5); + }; + Rc::try_unwrap(__v).ok().unwrap().into_inner() })); libcc2rs::register_static_destructor(|| { Destructible::destroy(holder_8.with(Value::clone).as_pointer()) diff --git a/tests/std/basic/basic.start/out/unsafe/basic.start.term.rs b/tests/std/basic/basic.start/out/unsafe/basic.start.term.rs index 47926e2396..62c6fc66c8 100644 --- a/tests/std/basic/basic.start/out/unsafe/basic.start.term.rs +++ b/tests/std/basic/basic.start/out/unsafe/basic.start.term.rs @@ -153,12 +153,12 @@ pub static mut after_7: std::cell::LazyCell = std::cell::LazyCell::new( }); pub static mut holder_8: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { { - let __static = HoldsTracker { - member: { - let mut __v: Tracker = ZeroInitializationUnsafe::zero_init(); - Tracker::constructor_1(&raw mut __v, 5); - __v - }, + let __static = { + let mut __v: HoldsTracker = ZeroInitializationUnsafe::zero_init(); + { + Tracker::constructor_1(&raw mut (*&raw mut __v).member, 5); + }; + __v }; libcc2rs::register_static_destructor(|| { DestructibleUnsafe::destroy(std::cell::LazyCell::force_mut(&mut *&raw mut holder_8)) diff --git a/tests/std/basic/out/refcount/basic.def.odr.rs b/tests/std/basic/out/refcount/basic.def.odr.rs index 4aa16c4365..e16ae03357 100644 --- a/tests/std/basic/out/refcount/basic.def.odr.rs +++ b/tests/std/basic/out/refcount/basic.def.odr.rs @@ -118,38 +118,29 @@ impl Destructible for LifetimeTracked { } pub fn test_5() { ({ HasConstexprCtor::reset() }); - let mut arr: [HasConstexprCtor; 2] = [ - { - let __v: Value = - Rc::new(RefCell::new(ZeroInitialization::zero_init())); - HasConstexprCtor::constructor_1(__v.as_pointer(), 3); - Rc::try_unwrap(__v).ok().unwrap().into_inner() - }, - { - let __v: Value = - Rc::new(RefCell::new(ZeroInitialization::zero_init())); - HasConstexprCtor::constructor_1(__v.as_pointer(), 4); - Rc::try_unwrap(__v).ok().unwrap().into_inner() - }, - ]; + let arr: Value> = Rc::new(RefCell::new( + (0..2).map(|_| ZeroInitialization::zero_init()).collect(), + )); + { + HasConstexprCtor::constructor_1(arr.as_pointer().decay().offset(0), 3); + HasConstexprCtor::constructor_1(arr.as_pointer().decay().offset(1), 4); + }; let f: Value i32>> = Rc::new(RefCell::new(FnPtr:: i32>::new(|| -> i32 { { - return [ - { - let __v: Value = - Rc::new(RefCell::new(ZeroInitialization::zero_init())); - HasConstexprCtor::constructor_1(__v.as_pointer(), 3); - Rc::try_unwrap(__v).ok().unwrap().into_inner() - }, - { - let __v: Value = - Rc::new(RefCell::new(ZeroInitialization::zero_init())); - HasConstexprCtor::constructor_1(__v.as_pointer(), 4); + return { + ({ + let __v: Value> = Rc::new(RefCell::new( + (0..2).map(|_| ZeroInitialization::zero_init()).collect(), + )); + { + HasConstexprCtor::constructor_1(__v.as_pointer().decay().offset(0), 3); + HasConstexprCtor::constructor_1(__v.as_pointer().decay().offset(1), 4); + }; Rc::try_unwrap(__v).ok().unwrap().into_inner() - }, - ][(1) as usize] - .m; + })[(1) as usize] + .m + }; } }))); assert!((copyCount_0.with(|rc| *rc.borrow()) == 0)); @@ -163,12 +154,12 @@ pub fn test_6() { Rc::new(RefCell::new(FnPtr:: i32>::new(|| -> i32 { { return { - { + ({ let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); HasConstexprCtor::constructor_1(__v.as_pointer(), 5); Rc::try_unwrap(__v).ok().unwrap().into_inner() - } + }) .m }; } @@ -186,19 +177,19 @@ pub fn test_7() { { return { (if b { - { + ({ let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); HasConstexprCtor::constructor_1(__v.as_pointer(), 7); Rc::try_unwrap(__v).ok().unwrap().into_inner() - } + }) } else { - { + ({ let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); HasConstexprCtor::constructor_1(__v.as_pointer(), 9); Rc::try_unwrap(__v).ok().unwrap().into_inner() - } + }) }) .m }; @@ -222,12 +213,12 @@ pub fn test_9() { return { { (*left_8.with(Value::clone).borrow_mut()).prefix_inc(); - { + ({ let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); HasConstexprCtor::constructor_1(__v.as_pointer(), 5); Rc::try_unwrap(__v).ok().unwrap().into_inner() - } + }) } .m }; @@ -288,7 +279,7 @@ pub fn test_13() { ({ LifetimeTracked::reset() }); let r: Ptr = global_12.with(|v| v.as_pointer()); let f: Value> = Rc::new(RefCell::new(FnPtr::::new(|| { - ({ LifetimeTracked::use_(global_12.with(|v| v.as_pointer())) }); + ({ LifetimeTracked::use_((global_12.with(|v| v.as_pointer()))) }); }))); assert!((copyCount_2.with(|rc| *rc.borrow()) == 0)); ({ (*f.borrow()).call() }); @@ -363,7 +354,7 @@ pub fn test_17() { let f: Value i32>> = Rc::new(RefCell::new(FnPtr:: i32>::new(|| -> i32 { { - return 5; + return (5); } }))); assert!(({ (({ (*f.borrow()).call() }) == 5) })); @@ -460,24 +451,14 @@ impl p6_unevaluated_binding_needs_no_capture_HasTwoMembers { } pub fn test_20() { ({ LifetimeTracked::reset() }); - let p: Value = Rc::new(RefCell::new( - p6_unevaluated_binding_needs_no_capture_HasTwoMembers { - a: { - let __v: Value = - Rc::new(RefCell::new(ZeroInitialization::zero_init())); - LifetimeTracked::constructor_1(__v.as_pointer()); - Rc::try_unwrap(__v).ok().unwrap().into_inner() - }, - b: { - let __v: Value = - Rc::new(RefCell::new(ZeroInitialization::zero_init())); - LifetimeTracked::constructor_1(__v.as_pointer()); - Rc::try_unwrap(__v).ok().unwrap().into_inner() - }, - }, - )); - let _dtor_p = - std::mem::DropGuard::new(p.clone(), |__p| Destructible::destroy(__p.as_pointer())); + object!( + p, + p6_unevaluated_binding_needs_no_capture_HasTwoMembers, + |p| { + LifetimeTracked::constructor_1(field_ptr!(p.clone(), a)); + LifetimeTracked::constructor_1(field_ptr!(p.clone(), b)); + } + ); let __decomp_21: Ptr = p.as_pointer(); let f: Value usize>> = Rc::new(RefCell::new(FnPtr:: usize>::new(|| -> usize { diff --git a/tests/std/basic/out/unsafe/basic.def.odr.rs b/tests/std/basic/out/unsafe/basic.def.odr.rs index 96d07ff20f..38c17f890c 100644 --- a/tests/std/basic/out/unsafe/basic.def.odr.rs +++ b/tests/std/basic/out/unsafe/basic.def.odr.rs @@ -94,32 +94,21 @@ impl DestructibleUnsafe for LifetimeTracked { } pub unsafe fn test_5() { (unsafe { HasConstexprCtor::reset() }); - let arr: [HasConstexprCtor; 2] = [ - { - let mut __v: HasConstexprCtor = ZeroInitializationUnsafe::zero_init(); - HasConstexprCtor::constructor_1(&raw mut __v, 3); - __v - }, - { - let mut __v: HasConstexprCtor = ZeroInitializationUnsafe::zero_init(); - HasConstexprCtor::constructor_1(&raw mut __v, 4); - __v - }, - ]; + let mut arr: [HasConstexprCtor; 2] = ZeroInitializationUnsafe::zero_init(); + { + HasConstexprCtor::constructor_1(&raw mut (*&raw mut arr)[0], 3); + HasConstexprCtor::constructor_1(&raw mut (*&raw mut arr)[1], 4); + }; let mut f: FnPtr i32> = FnPtr:: i32>::new(|| -> i32 { unsafe { - return [ - { - let mut __v: HasConstexprCtor = ZeroInitializationUnsafe::zero_init(); - HasConstexprCtor::constructor_1(&raw mut __v, 3); - __v - }, + return ({ + let mut __v: [HasConstexprCtor; 2] = ZeroInitializationUnsafe::zero_init(); { - let mut __v: HasConstexprCtor = ZeroInitializationUnsafe::zero_init(); - HasConstexprCtor::constructor_1(&raw mut __v, 4); - __v - }, - ][(1) as usize] + HasConstexprCtor::constructor_1(&raw mut (*&raw mut __v)[0], 3); + HasConstexprCtor::constructor_1(&raw mut (*&raw mut __v)[1], 4); + }; + __v + })[(1) as usize] .m; } }); @@ -132,11 +121,11 @@ pub unsafe fn test_6() { object_unsafe!(k, HasConstexprCtor::constructor_1(5)); let mut f: FnPtr i32> = FnPtr:: i32>::new(|| -> i32 { unsafe { - return { + return ({ let mut __v: HasConstexprCtor = ZeroInitializationUnsafe::zero_init(); HasConstexprCtor::constructor_1(&raw mut __v, 5); __v - } + }) .m; } }); @@ -151,17 +140,17 @@ pub unsafe fn test_7() { let mut f: FnPtr i32> = FnPtr:: i32>::new(|mut b: bool| -> i32 { unsafe { return (if b { - { + ({ let mut __v: HasConstexprCtor = ZeroInitializationUnsafe::zero_init(); HasConstexprCtor::constructor_1(&raw mut __v, 7); __v - } + }) } else { - { + ({ let mut __v: HasConstexprCtor = ZeroInitializationUnsafe::zero_init(); HasConstexprCtor::constructor_1(&raw mut __v, 9); __v - } + }) }) .m; } @@ -179,11 +168,11 @@ pub unsafe fn test_9() { unsafe { return { (*std::cell::LazyCell::force_mut(&mut *&raw mut left_8)).prefix_inc(); - { + ({ let mut __v: HasConstexprCtor = ZeroInitializationUnsafe::zero_init(); HasConstexprCtor::constructor_1(&raw mut __v, 5); __v - } + }) } .m; } @@ -318,7 +307,7 @@ pub unsafe fn test_17() { let n: i32 = 5; let mut f: FnPtr i32> = FnPtr:: i32>::new(|| -> i32 { unsafe { - return 5; + return (5); } }); assert!(((unsafe { f.call() }) == (5))); @@ -406,22 +395,14 @@ impl p6_unevaluated_binding_needs_no_capture_HasTwoMembers { } pub unsafe fn test_20() { (unsafe { LifetimeTracked::reset() }); - let mut p: p6_unevaluated_binding_needs_no_capture_HasTwoMembers = - p6_unevaluated_binding_needs_no_capture_HasTwoMembers { - a: { - let mut __v: LifetimeTracked = ZeroInitializationUnsafe::zero_init(); - LifetimeTracked::constructor_1(&raw mut __v); - __v - }, - b: { - let mut __v: LifetimeTracked = ZeroInitializationUnsafe::zero_init(); - LifetimeTracked::constructor_1(&raw mut __v); - __v - }, - }; - let _dtor_p = std::mem::DropGuard::new(&raw mut p, |__p| unsafe { - DestructibleUnsafe::destroy(__p) - }); + object_unsafe!( + p, + p6_unevaluated_binding_needs_no_capture_HasTwoMembers, + |p| { + LifetimeTracked::constructor_1(&raw mut (*p).a); + LifetimeTracked::constructor_1(&raw mut (*p).b); + } + ); let __decomp_21: *mut p6_unevaluated_binding_needs_no_capture_HasTwoMembers = &mut p; let mut f: FnPtr usize> = FnPtr:: usize>::new(|| -> usize { unsafe { diff --git a/tests/std/class/class.mem/class.conv/class.conv.fct.cpp b/tests/std/class/class.mem/class.conv/class.conv.fct.cpp index 9b721ec476..9b7177772c 100644 --- a/tests/std/class/class.mem/class.conv/class.conv.fct.cpp +++ b/tests/std/class/class.mem/class.conv/class.conv.fct.cpp @@ -56,6 +56,27 @@ void test() { } } // namespace p2_explicit_conversion_function +// N4950 [class.conv.fct]/2: a conversion function to a class type specifies a +// conversion from X to that class type. +namespace p2_conversion_function_to_class { +struct Celsius { + int degrees; + Celsius(int d) : degrees(d) {} + Celsius(const Celsius &other) : degrees(other.degrees + 100) {} +}; + +struct Kelvin { + int degrees; + operator Celsius() const { return Celsius(degrees - 273); } +}; + +void test() { + Kelvin kelvin{300}; + Celsius celsius = kelvin; + assert(celsius.degrees == 27); +} +} // namespace p2_conversion_function_to_class + // N4950 [class.conv.fct]/1, [dcl.init.ref]/5.1.2: a conversion function to an // lvalue reference returns a reference that a reference binds to directly. namespace p1_conversion_function_to_reference { @@ -76,6 +97,7 @@ int main() { p2_non_const_conversion_function::test(); p2_implicit_conversion_function::test(); p2_explicit_conversion_function::test(); + p2_conversion_function_to_class::test(); p1_conversion_function_to_reference::test(); return 0; } diff --git a/tests/std/class/class.mem/class.conv/out/refcount/class.conv.ctor.rs b/tests/std/class/class.mem/class.conv/out/refcount/class.conv.ctor.rs index 7d5e62eabe..1c62fa4239 100644 --- a/tests/std/class/class.mem/class.conv/out/refcount/class.conv.ctor.rs +++ b/tests/std/class/class.mem/class.conv/out/refcount/class.conv.ctor.rs @@ -80,13 +80,11 @@ impl p1_implicit_conversion_by_constructor_Meters { } } pub fn test_1() { - let mut m: p1_implicit_conversion_by_constructor_Meters = { - let __v: Value = - Rc::new(RefCell::new(ZeroInitialization::zero_init())); - p1_implicit_conversion_by_constructor_Meters::constructor_1(__v.as_pointer(), 5); - Rc::try_unwrap(__v).ok().unwrap().into_inner() - }; - assert!((m.value == 5)); + object!( + m, + p1_implicit_conversion_by_constructor_Meters::constructor_1(5) + ); + assert!(({ (*m.borrow()).value } == 5)); let source: Value<(Value, Value)> = Rc::new(RefCell::new(( Rc::new(RefCell::new(3)), Rc::new(RefCell::new(4)), @@ -191,13 +189,8 @@ impl p2_explicit_constructor_Celsius { pub fn test_2() { object!(direct, p2_explicit_constructor_Celsius::constructor_1(3)); assert!(({ (*direct.borrow()).degrees } == 3)); - let mut cast: p2_explicit_constructor_Celsius = { - let __v: Value = - Rc::new(RefCell::new(ZeroInitialization::zero_init())); - p2_explicit_constructor_Celsius::constructor_1(__v.as_pointer(), 4); - Rc::try_unwrap(__v).ok().unwrap().into_inner() - }; - assert!((cast.degrees == 4)); + object!(cast, p2_explicit_constructor_Celsius::constructor_1(4)); + assert!(({ (*cast.borrow()).degrees } == 4)); let converted: Value<(Value, Value)> = Rc::new(RefCell::new( match (Rc::new(RefCell::new(( diff --git a/tests/std/class/class.mem/class.conv/out/refcount/class.conv.fct.rs b/tests/std/class/class.mem/class.conv/out/refcount/class.conv.fct.rs index 4c4224906a..513adffa84 100644 --- a/tests/std/class/class.mem/class.conv/out/refcount/class.conv.fct.rs +++ b/tests/std/class/class.mem/class.conv/out/refcount/class.conv.fct.rs @@ -161,6 +161,89 @@ pub fn test_2() { let mut cast: bool = ({ p2_explicit_conversion_function_Flag::to_bool(on.as_pointer()) }); assert!(cast); } +#[derive( + Record, + ByteRepr, + ZeroInitialization, + FnPtrArg, + DefaultInitializable, + Memberwise, + MoveConstructible, + MoveAssignable, + Swappable, + Default, +)] +#[memberwise(copy_assign)] +#[derive(Conversions)] +#[conversions(copy)] +#[constructor(constructor_1, i32)] +#[constructor(constructor_2, LvalueRef>)] +#[byte_size(4)] +pub struct p2_conversion_function_to_class_Celsius { + #[offset(0)] + pub degrees: i32, +} +impl p2_conversion_function_to_class_Celsius { + pub fn constructor_1(this: Ptr, mut d: i32) { + field!(this, degrees).write(d); + } + pub fn constructor_2( + this: Ptr, + other: LvalueRef>, + ) { + let other = other.as_pointer(); + field!(this, degrees).write((other.with(|__s| __s.degrees) + 100)); + } +} +#[derive( + Copy, + Clone, + Record, + ByteRepr, + ZeroInitialization, + VaArg, + FnPtrArg, + TriviallyDefaultConstructible, + TriviallyCopyable, + Default, + Conversions, +)] +#[conversions(copy, move)] +#[conversion_function(to_p2_conversion_function_to_class_Celsius, LvalueRef>, p2_conversion_function_to_class_Celsius , return_slot)] +#[byte_size(4)] +pub struct p2_conversion_function_to_class_Kelvin { + #[offset(0)] + pub degrees: i32, +} +impl p2_conversion_function_to_class_Kelvin { + pub fn to_p2_conversion_function_to_class_Celsius( + this: Ptr, + __return: Ptr, + ) { + p2_conversion_function_to_class_Celsius::constructor_1( + __return.clone(), + (this.with(|__s| __s.degrees) - 273), + ); + return; + } +} +pub fn test_3() { + let kelvin: Value = + Rc::new(RefCell::new(p2_conversion_function_to_class_Kelvin { + degrees: 300, + })); + object!( + celsius, + p2_conversion_function_to_class_Celsius, + |celsius| ({ + p2_conversion_function_to_class_Kelvin::to_p2_conversion_function_to_class_Celsius( + kelvin.as_pointer(), + Ptr::clone(&celsius.clone()), + ) + }) + ); + assert!(({ (*celsius.borrow()).degrees } == 27)); +} #[derive( Copy, Clone, @@ -185,7 +268,7 @@ impl p1_conversion_function_to_reference_Holder { return field_ptr!(this, value); } } -pub fn test_3() { +pub fn test_4() { let holder: Value = Rc::new(RefCell::new(p1_conversion_function_to_reference_Holder { value: 1, @@ -204,6 +287,7 @@ fn main_0() -> i32 { ({ test_1() }); ({ test_2() }); ({ test_3() }); + ({ test_4() }); return 0; } pub fn __cpp2rust_init_globals() {} diff --git a/tests/std/class/class.mem/class.conv/out/unsafe/class.conv.ctor.rs b/tests/std/class/class.mem/class.conv/out/unsafe/class.conv.ctor.rs index ec44ca2f4a..5acf77c3dc 100644 --- a/tests/std/class/class.mem/class.conv/out/unsafe/class.conv.ctor.rs +++ b/tests/std/class/class.mem/class.conv/out/unsafe/class.conv.ctor.rs @@ -74,12 +74,10 @@ impl p1_implicit_conversion_by_constructor_Meters { } } pub unsafe fn test_1() { - let mut m: p1_implicit_conversion_by_constructor_Meters = { - let mut __v: p1_implicit_conversion_by_constructor_Meters = - ZeroInitializationUnsafe::zero_init(); - p1_implicit_conversion_by_constructor_Meters::constructor_1(&raw mut __v, 5); - __v - }; + object_unsafe!( + m, + p1_implicit_conversion_by_constructor_Meters::constructor_1(5) + ); assert!(((m.value) == (5))); let mut source: (i32, i32) = (3, 4); let mut copied: (p1_implicit_conversion_by_constructor_Meters, i64) = { @@ -149,11 +147,7 @@ impl p2_explicit_constructor_Celsius { pub unsafe fn test_2() { object_unsafe!(direct, p2_explicit_constructor_Celsius::constructor_1(3)); assert!(((direct.degrees) == (3))); - let mut cast: p2_explicit_constructor_Celsius = { - let mut __v: p2_explicit_constructor_Celsius = ZeroInitializationUnsafe::zero_init(); - p2_explicit_constructor_Celsius::constructor_1(&raw mut __v, 4); - __v - }; + object_unsafe!(cast, p2_explicit_constructor_Celsius::constructor_1(4)); assert!(((cast.degrees) == (4))); let mut converted: (p2_explicit_constructor_Celsius, i32) = match ((&mut (5, 6) as *mut (i32, i32)),) { diff --git a/tests/std/class/class.mem/class.conv/out/unsafe/class.conv.fct.rs b/tests/std/class/class.mem/class.conv/out/unsafe/class.conv.fct.rs index 9aa229123c..df065ad0e1 100644 --- a/tests/std/class/class.mem/class.conv/out/unsafe/class.conv.fct.rs +++ b/tests/std/class/class.mem/class.conv/out/unsafe/class.conv.fct.rs @@ -133,6 +133,81 @@ pub unsafe fn test_2() { assert!(cast); } #[repr(C)] +#[derive( + FnPtrArg, + ZeroInitializationUnsafe, + DefaultInitializableUnsafe, + MemberwiseUnsafe, + MoveConstructibleUnsafe, + MoveAssignableUnsafe, + SwappableUnsafe, + Default, +)] +#[memberwise(copy_assign)] +#[derive(ConversionsUnsafe)] +#[conversions(copy)] +#[constructor(constructor_1, i32)] +#[constructor(constructor_2, LvalueRefUnsafe>)] +pub struct p2_conversion_function_to_class_Celsius { + pub degrees: i32, +} +impl p2_conversion_function_to_class_Celsius { + pub unsafe fn constructor_1(this: *mut Self, mut d: i32) { + (&raw mut (*this).degrees).write(d); + } + pub unsafe fn constructor_2( + this: *mut Self, + other: LvalueRefUnsafe>, + ) { + let other = (other.as_ptr() as *const p2_conversion_function_to_class_Celsius); + (&raw mut (*this).degrees).write((((*other).degrees) + (100))); + } +} +#[repr(C)] +#[derive( + Copy, + Clone, + VaArg, + FnPtrArg, + ZeroInitializationUnsafe, + TriviallyDefaultConstructibleUnsafe, + TriviallyCopyableUnsafe, + Default, + ConversionsUnsafe, +)] +#[conversions(copy, move)] +#[conversion_function(to_p2_conversion_function_to_class_Celsius, LvalueRefUnsafe>, p2_conversion_function_to_class_Celsius , return_slot)] +pub struct p2_conversion_function_to_class_Kelvin { + pub degrees: i32, +} +impl p2_conversion_function_to_class_Kelvin { + pub unsafe fn to_p2_conversion_function_to_class_Celsius( + this: *const Self, + __return: *mut p2_conversion_function_to_class_Celsius, + ) { + p2_conversion_function_to_class_Celsius::constructor_1( + __return, + (((*this).degrees) - (273)), + ); + return; + } +} +pub unsafe fn test_3() { + let mut kelvin: p2_conversion_function_to_class_Kelvin = + p2_conversion_function_to_class_Kelvin { degrees: 300 }; + object_unsafe!( + celsius, + p2_conversion_function_to_class_Celsius, + |celsius| (unsafe { + p2_conversion_function_to_class_Kelvin::to_p2_conversion_function_to_class_Celsius( + &raw const kelvin, + celsius, + ) + }) + ); + assert!(((celsius.degrees) == (27))); +} +#[repr(C)] #[derive( Copy, Clone, @@ -153,7 +228,7 @@ impl p1_conversion_function_to_reference_Holder { return &mut (*this).value; } } -pub unsafe fn test_3() { +pub unsafe fn test_4() { let mut holder: p1_conversion_function_to_reference_Holder = p1_conversion_function_to_reference_Holder { value: 1 }; let ref_: *mut i32 = @@ -172,6 +247,7 @@ unsafe fn main_0() -> i32 { (unsafe { test_1() }); (unsafe { test_2() }); (unsafe { test_3() }); + (unsafe { test_4() }); return 0; } pub unsafe fn __cpp2rust_init_globals() {} diff --git a/tests/std/class/class.mem/class.ctor/class.copy.ctor.cpp b/tests/std/class/class.mem/class.ctor/class.copy.ctor.cpp index 991b32b249..0236c14457 100644 --- a/tests/std/class/class.mem/class.ctor/class.copy.ctor.cpp +++ b/tests/std/class/class.mem/class.ctor/class.copy.ctor.cpp @@ -42,8 +42,49 @@ void test() { } } // namespace p14_trivial_move_keeps_source +// N4950 [class.copy.ctor]/14: the implicitly-defined copy constructor +// direct-initializes each element of an array member with the corresponding +// element of the source. +namespace p14_array_member_copied_elementwise { +int copies = 0; + +struct S { + int value; + S(int v) : value(v) {} + S(const S &other) : value(other.value + 100) { ++copies; } +}; + +struct WithArray { + S items[2]; +}; + +struct WithGrid { + S grid[2][2]; +}; + +WithArray pass_through(WithArray holder) { return holder; } + +void test() { + WithArray source{{S(1), S(2)}}; + WithArray copy = source; + assert(copy.items[0].value == 101); + assert(copy.items[1].value == 102); + assert(source.items[0].value == 1); + + WithGrid grid_source{{{S(3), S(4)}, {S(5), S(6)}}}; + WithGrid grid_copy(grid_source); + assert(grid_copy.grid[0][1].value == 104); + assert(grid_copy.grid[1][0].value == 105); + + assert(WithArray(source).items[1].value == 102); + + assert(copies == 8); +} +} // namespace p14_array_member_copied_elementwise + int main() { p11_defaulted_move_constructor_is_trivial::test(); p14_trivial_move_keeps_source::test(); + p14_array_member_copied_elementwise::test(); return 0; } diff --git a/tests/std/class/class.mem/class.ctor/out/refcount/class.copy.ctor.rs b/tests/std/class/class.mem/class.ctor/out/refcount/class.copy.ctor.rs index 6f462ee343..160405dbea 100644 --- a/tests/std/class/class.mem/class.ctor/out/refcount/class.copy.ctor.rs +++ b/tests/std/class/class.mem/class.ctor/out/refcount/class.copy.ctor.rs @@ -66,6 +66,202 @@ pub fn test_3() { assert!(((({ (*moved_it.borrow()).iter_deref() }).read()) == 7)); assert!(((({ (*it.borrow()).iter_deref() }).read()) == 7)); } +thread_local!( + pub static copies_4: Value = Rc::new(RefCell::new(0)); +); +#[derive( + Record, + ByteRepr, + ZeroInitialization, + FnPtrArg, + DefaultInitializable, + Memberwise, + MoveConstructible, + MoveAssignable, + Swappable, + Default, +)] +#[memberwise(copy_assign)] +#[derive(Conversions)] +#[conversions(copy)] +#[constructor(constructor_1, i32)] +#[constructor(constructor_2, LvalueRef>)] +#[byte_size(4)] +pub struct p14_array_member_copied_elementwise_S { + #[offset(0)] + pub value: i32, +} +impl p14_array_member_copied_elementwise_S { + pub fn constructor_1(this: Ptr, mut v: i32) { + field!(this, value).write(v); + } + pub fn constructor_2( + this: Ptr, + other: LvalueRef>, + ) { + let other = other.as_pointer(); + field!(this, value).write((other.with(|__s| __s.value) + 100)); + (*copies_4.with(Value::clone).borrow_mut()).prefix_inc(); + } +} +#[derive( + Record, ByteRepr, ZeroInitialization, FnPtrArg, DefaultInitializable, Memberwise, Swappable, +)] +#[memberwise(copy_constructor, move_constructor, move_assign, copy_assign)] +#[derive(Conversions)] +#[conversions(copy, move)] +#[byte_size(8)] +pub struct p14_array_member_copied_elementwise_WithArray { + #[offset(0)] + #[byte_size(8)] + #[array_len(2)] + pub items: Box<[p14_array_member_copied_elementwise_S]>, +} +impl p14_array_member_copied_elementwise_WithArray {} +impl Default for p14_array_member_copied_elementwise_WithArray { + fn default() -> Self { + { + p14_array_member_copied_elementwise_WithArray { + items: (0..2) + .map(|_| ::default()) + .collect::>(), + } + } + } +} +#[derive( + Record, ByteRepr, ZeroInitialization, FnPtrArg, DefaultInitializable, Memberwise, Swappable, +)] +#[memberwise(copy_constructor, move_constructor, move_assign, copy_assign)] +#[derive(Conversions)] +#[conversions(copy, move)] +#[byte_size(16)] +pub struct p14_array_member_copied_elementwise_WithGrid { + #[offset(0)] + #[byte_size(16)] + #[array_len(2, 2)] + pub grid: Box<[Box<[p14_array_member_copied_elementwise_S]>]>, +} +impl p14_array_member_copied_elementwise_WithGrid {} +impl Default for p14_array_member_copied_elementwise_WithGrid { + fn default() -> Self { + { + p14_array_member_copied_elementwise_WithGrid { + grid: (0..2) + .map(|_| { + (0..2) + .map(|_| ::default()) + .collect::>() + }) + .collect::]>>(), + } + } + } +} +pub fn pass_through_5( + __return: Ptr, + holder: ParameterObject, +) { + let holder = holder.as_pointer(); + ::move_constructor( + __return.clone(), + RvalueRef::bind((holder).clone()), + ); + return; +} +pub fn test_6() { + object!( + source, + p14_array_member_copied_elementwise_WithArray, + |source| { + { + p14_array_member_copied_elementwise_S::constructor_1( + field_ptr!(source.clone(), items).decay().offset(0), + 1, + ); + p14_array_member_copied_elementwise_S::constructor_1( + field_ptr!(source.clone(), items).decay().offset(1), + 2, + ); + }; + } + ); + object!( + copy, + p14_array_member_copied_elementwise_WithArray, + |copy| { + ::copy_constructor( + copy.clone(), + LvalueRef::bind(source.as_pointer()), + ) + } + ); + assert!((array_elem!(copy.as_pointer(), items, 0).with(|__s| __s.value) == 101)); + assert!((array_elem!(copy.as_pointer(), items, 1).with(|__s| __s.value) == 102)); + assert!((array_elem!(source.as_pointer(), items, 0).with(|__s| __s.value) == 1)); + object!( + grid_source, + p14_array_member_copied_elementwise_WithGrid, + |grid_source| { + { + { + p14_array_member_copied_elementwise_S::constructor_1( + field_ptr!(grid_source.clone(), grid) + .decay() + .offset(0) + .decay() + .offset(0), + 3, + ); + p14_array_member_copied_elementwise_S::constructor_1( + field_ptr!(grid_source.clone(), grid) + .decay() + .offset(0) + .decay() + .offset(1), + 4, + ); + }; + { + p14_array_member_copied_elementwise_S::constructor_1( + field_ptr!(grid_source.clone(), grid) + .decay() + .offset(1) + .decay() + .offset(0), + 5, + ); + p14_array_member_copied_elementwise_S::constructor_1( + field_ptr!(grid_source.clone(), grid) + .decay() + .offset(1) + .decay() + .offset(1), + 6, + ); + }; + }; + } + ); + object!( + grid_copy, + p14_array_member_copied_elementwise_WithGrid, + |grid_copy| { + ::copy_constructor( + grid_copy.clone(), + LvalueRef::bind(grid_source.as_pointer()), + ) + } + ); + assert!(({ (*grid_copy.borrow()).grid[(0) as usize][(1) as usize].value } == 104)); + assert!(({ (*grid_copy.borrow()).grid[(1) as usize][(0) as usize].value } == 105)); + { + let __tmp_0: Value = + Rc::new(RefCell::new(ZeroInitialization::zero_init())); + assert!(( array_elem!({ ::copy_constructor(__tmp_0.as_pointer(), LvalueRef::bind(source .as_pointer() )) ; __tmp_0.as_pointer() } , items, 1 ) .with(|__s| __s . value ) == 102 ) ) + }; + assert!((copies_4.with(|rc| *rc.borrow()) == 8)); +} pub fn main() { __cpp2rust_init_globals(); libcc2rs::cpp2rust_exit(main_0()); @@ -73,6 +269,9 @@ pub fn main() { fn main_0() -> i32 { ({ test_1() }); ({ test_3() }); + ({ test_6() }); return 0; } -pub fn __cpp2rust_init_globals() {} +pub fn __cpp2rust_init_globals() { + let _ = copies_4.with(|_| ()); +} diff --git a/tests/std/class/class.mem/class.ctor/out/unsafe/class.copy.ctor.rs b/tests/std/class/class.mem/class.ctor/out/unsafe/class.copy.ctor.rs index d59560429f..299dbb02ae 100644 --- a/tests/std/class/class.mem/class.ctor/out/unsafe/class.copy.ctor.rs +++ b/tests/std/class/class.mem/class.ctor/out/unsafe/class.copy.ctor.rs @@ -59,6 +59,153 @@ pub unsafe fn test_3() { assert!(((*moved_it.iter_deref()) == (7))); assert!(((*it.iter_deref()) == (7))); } +pub static mut copies_4: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +#[repr(C)] +#[derive( + FnPtrArg, + ZeroInitializationUnsafe, + DefaultInitializableUnsafe, + MemberwiseUnsafe, + MoveConstructibleUnsafe, + MoveAssignableUnsafe, + SwappableUnsafe, + Default, +)] +#[memberwise(copy_assign)] +#[derive(ConversionsUnsafe)] +#[conversions(copy)] +#[constructor(constructor_1, i32)] +#[constructor(constructor_2, LvalueRefUnsafe>)] +pub struct p14_array_member_copied_elementwise_S { + pub value: i32, +} +impl p14_array_member_copied_elementwise_S { + pub unsafe fn constructor_1(this: *mut Self, mut v: i32) { + (&raw mut (*this).value).write(v); + } + pub unsafe fn constructor_2( + this: *mut Self, + other: LvalueRefUnsafe>, + ) { + let other = (other.as_ptr() as *const p14_array_member_copied_elementwise_S); + (&raw mut (*this).value).write((((*other).value) + (100))); + (*std::cell::LazyCell::force_mut(&mut *&raw mut copies_4)).prefix_inc(); + } +} +#[repr(C)] +#[derive( + FnPtrArg, + ZeroInitializationUnsafe, + DefaultInitializableUnsafe, + MemberwiseUnsafe, + SwappableUnsafe, + Default, +)] +#[memberwise(copy_constructor, move_constructor, move_assign, copy_assign)] +#[derive(ConversionsUnsafe)] +#[conversions(copy, move)] +pub struct p14_array_member_copied_elementwise_WithArray { + pub items: [p14_array_member_copied_elementwise_S; 2], +} +impl p14_array_member_copied_elementwise_WithArray {} +#[repr(C)] +#[derive( + FnPtrArg, + ZeroInitializationUnsafe, + DefaultInitializableUnsafe, + MemberwiseUnsafe, + SwappableUnsafe, + Default, +)] +#[memberwise(copy_constructor, move_constructor, move_assign, copy_assign)] +#[derive(ConversionsUnsafe)] +#[conversions(copy, move)] +pub struct p14_array_member_copied_elementwise_WithGrid { + pub grid: [[p14_array_member_copied_elementwise_S; 2]; 2], +} +impl p14_array_member_copied_elementwise_WithGrid {} +pub unsafe fn pass_through_5( + __return: *mut p14_array_member_copied_elementwise_WithArray, + holder: ParameterObjectUnsafe, +) { + let holder = (holder.as_ptr() as *mut p14_array_member_copied_elementwise_WithArray); + ::move_constructor( + __return, + RvalueRefUnsafe::bind(holder), + ); + return; +} +pub unsafe fn test_6() { + object_unsafe!( + source, + p14_array_member_copied_elementwise_WithArray, + |source| { + { + p14_array_member_copied_elementwise_S::constructor_1( + &raw mut (*&raw mut (*source).items)[0], + 1, + ); + p14_array_member_copied_elementwise_S::constructor_1( + &raw mut (*&raw mut (*source).items)[1], + 2, + ); + }; + } + ); + object_unsafe!( + copy, + p14_array_member_copied_elementwise_WithArray, + |copy| { + ::copy_constructor(copy, LvalueRefUnsafe::bind(& source )) + } + ); + assert!(((copy.items[(0) as usize].value) == (101))); + assert!(((copy.items[(1) as usize].value) == (102))); + assert!(((source.items[(0) as usize].value) == (1))); + object_unsafe!( + grid_source, + p14_array_member_copied_elementwise_WithGrid, + |grid_source| { + { + { + p14_array_member_copied_elementwise_S::constructor_1( + &raw mut (*&raw mut (*&raw mut (*grid_source).grid)[0])[0], + 3, + ); + p14_array_member_copied_elementwise_S::constructor_1( + &raw mut (*&raw mut (*&raw mut (*grid_source).grid)[0])[1], + 4, + ); + }; + { + p14_array_member_copied_elementwise_S::constructor_1( + &raw mut (*&raw mut (*&raw mut (*grid_source).grid)[1])[0], + 5, + ); + p14_array_member_copied_elementwise_S::constructor_1( + &raw mut (*&raw mut (*&raw mut (*grid_source).grid)[1])[1], + 6, + ); + }; + }; + } + ); + object_unsafe!( + grid_copy, + p14_array_member_copied_elementwise_WithGrid, + |grid_copy| { + ::copy_constructor(grid_copy, LvalueRefUnsafe::bind(& grid_source )) + } + ); + assert!(((grid_copy.grid[(0) as usize][(1) as usize].value) == (104))); + assert!(((grid_copy.grid[(1) as usize][(0) as usize].value) == (105))); + { + let mut __tmp_0: p14_array_member_copied_elementwise_WithArray = + ZeroInitializationUnsafe::zero_init(); + assert!(( ( (*{ ::copy_constructor(&raw mut __tmp_0, LvalueRefUnsafe::bind(& source )) ; & mut __tmp_0 } ) . items [ ( 1 ) as usize ] . value ) == ( 102 ) ) ) + }; + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut copies_4)) == (8))); +} pub fn main() { unsafe { __cpp2rust_init_globals(); @@ -68,6 +215,9 @@ pub fn main() { unsafe fn main_0() -> i32 { (unsafe { test_1() }); (unsafe { test_3() }); + (unsafe { test_6() }); return 0; } -pub unsafe fn __cpp2rust_init_globals() {} +pub unsafe fn __cpp2rust_init_globals() { + std::cell::LazyCell::force(&*&raw const copies_4); +} diff --git a/tests/std/dcl.dcl/dcl.decl/dcl.init/dcl.init.aggr.cpp b/tests/std/dcl.dcl/dcl.decl/dcl.init/dcl.init.aggr.cpp new file mode 100644 index 0000000000..e3136dbae1 --- /dev/null +++ b/tests/std/dcl.dcl/dcl.decl/dcl.init/dcl.init.aggr.cpp @@ -0,0 +1,183 @@ +// ADDITIONAL_COMPILE_FLAGS: -std=c++23 +#include + +// N4950 [dcl.init.aggr]/4.2: each explicitly initialized element of an +// aggregate is copy-initialized from the corresponding initializer-clause. +namespace p4_2_elements_from_initializer_clauses { +int copies = 0; + +struct S { + int value; + S(int v = 0) : value(v) {} + S(const S &other) : value(other.value + 100) { ++copies; } +}; + +struct Pair { + S first; + S second; +}; + +struct Mixed { + int number; + S object; +}; + +struct Outer { + Pair pair; + S last; +}; + +struct Holder { + Pair pair; + Holder(int v) : pair{S(v), S(v + 1)} {} +}; + +Pair make_explicit(int v) { return Pair{S(v), S(v + 1)}; } + +Pair make_braced(int v) { return {S(v), S(v + 1)}; } + +int sum(const Pair &pair) { return pair.first.value + pair.second.value; } + +void test() { + Pair local{S(1), S(2)}; + assert(local.first.value == 1); + assert(local.second.value == 2); + + Pair copy_list = {S(3), 4}; + assert(copy_list.first.value == 3); + assert(copy_list.second.value == 4); + + Pair from_explicit = make_explicit(5); + assert(from_explicit.first.value == 5); + assert(from_explicit.second.value == 6); + + Pair from_braced = make_braced(7); + assert(from_braced.first.value == 7); + assert(from_braced.second.value == 8); + + assert((Pair{S(9), S(10)}.second.value == 10)); + assert((sum(Pair{S(11), S(12)}) == 23)); + + Mixed mixed{13, S(14)}; + assert(mixed.number == 13); + assert(mixed.object.value == 14); + + Outer outer{Pair{S(15), S(16)}, S(17)}; + assert(outer.pair.first.value == 15); + assert(outer.pair.second.value == 16); + assert(outer.last.value == 17); + + Outer elided{{S(18), S(19)}, S(20)}; + assert(elided.pair.first.value == 18); + assert(elided.last.value == 20); + + Holder holder(21); + assert(holder.pair.first.value == 21); + assert(holder.pair.second.value == 22); + + bool yes = true; + Pair chosen = yes ? Pair{S(23), S(24)} : Pair{S(25), S(26)}; + assert(chosen.first.value == 23); + + Pair *allocated = new Pair{S(27), S(28)}; + assert(allocated->second.value == 28); + delete allocated; + + assert(copies == 0); +} +} // namespace p4_2_elements_from_initializer_clauses + +// N4950 [dcl.init.aggr]/4.2: the elements of an array are copy-initialized +// from the corresponding initializer-clauses. +namespace p4_2_array_elements_from_initializer_clauses { +int copies = 0; + +struct S { + int value; + S(int v = 0) : value(v) {} + S(const S &other) : value(other.value + 100) { ++copies; } +}; + +struct Pair { + S first; + S second; +}; + +struct WithArray { + S items[2]; +}; + +int second_of(const S (&items)[2]) { return items[1].value; } + +void test() { + S items[2] = {S(1), S(2)}; + assert(items[0].value == 1); + assert(items[1].value == 2); + + S converted[2] = {3, S(4)}; + assert(converted[0].value == 3); + assert(converted[1].value == 4); + + Pair pairs[2] = {{S(5), S(6)}, {S(7), S(8)}}; + assert(pairs[0].second.value == 6); + assert(pairs[1].first.value == 7); + + S grid[2][2] = {{S(9), S(10)}, {S(11), S(12)}}; + assert(grid[0][1].value == 10); + assert(grid[1][0].value == 11); + + WithArray holder{{S(13), S(14)}}; + assert(holder.items[0].value == 13); + assert(holder.items[1].value == 14); + + assert((second_of({S(15), S(16)}) == 16)); + + assert(copies == 0); +} +} // namespace p4_2_array_elements_from_initializer_clauses + +// N4950 [dcl.init.aggr]/5.2: an element that is not explicitly initialized +// and has no default member initializer is copy-initialized from an empty +// initializer list. +namespace p5_2_remaining_elements_from_empty_list { +int copies = 0; + +struct S { + int value; + S(int v = 7) : value(v) {} + S(const S &other) : value(other.value + 100) { ++copies; } +}; + +struct Pair { + S first; + S second; +}; + +void test() { + S filled[4] = {S(1)}; + assert(filled[0].value == 1); + assert(filled[1].value == 7); + assert(filled[3].value == 7); + + S empty[3] = {}; + assert(empty[0].value == 7); + assert(empty[2].value == 7); + + Pair partial{S(2)}; + assert(partial.first.value == 2); + assert(partial.second.value == 7); + + Pair pairs[3] = {{S(3), S(4)}}; + assert(pairs[0].second.value == 4); + assert(pairs[2].first.value == 7); + + assert(copies == 0); +} +} // namespace p5_2_remaining_elements_from_empty_list + +int main() { + p4_2_elements_from_initializer_clauses::test(); + p4_2_array_elements_from_initializer_clauses::test(); + p5_2_remaining_elements_from_empty_list::test(); + return 0; +} diff --git a/tests/std/dcl.dcl/dcl.decl/dcl.init/dcl.init.general.cpp b/tests/std/dcl.dcl/dcl.decl/dcl.init/dcl.init.general.cpp index dfeb7987cc..56edd22ab2 100644 --- a/tests/std/dcl.dcl/dcl.decl/dcl.init/dcl.init.general.cpp +++ b/tests/std/dcl.dcl/dcl.decl/dcl.init/dcl.init.general.cpp @@ -166,6 +166,41 @@ void test() { } } // namespace p6_4_zero_initialized_array +// N4950 [dcl.init.general]/7.2: default-initializing an array +// default-initializes each element. +namespace p7_2_default_initialized_array { +int constructed = 0; + +struct S { + int value; + S() : value(7) { ++constructed; } + S(const S &other) : value(other.value + 100) { ++constructed; } +}; + +struct WithArray { + S items[2]; +}; + +void test() { + S items[3]; + assert(items[0].value == 7); + assert(items[2].value == 7); + + S grid[2][3]; + assert(grid[0][0].value == 7); + assert(grid[1][2].value == 7); + + WithArray holder; + assert(holder.items[1].value == 7); + + S *allocated = new S[2]; + assert(allocated[1].value == 7); + delete[] allocated; + + assert(constructed == 13); +} +} // namespace p7_2_default_initialized_array + // N4950 [dcl.init.general]/9.1.2: value-initializing a class whose default // constructor is not user-provided zero-initializes the object, then // default-initializes it if the default constructor is non-trivial. @@ -210,6 +245,7 @@ int main() { p6_2_zero_initialized_class::test(); p6_3_zero_initialized_union::test(); p6_4_zero_initialized_array::test(); + p7_2_default_initialized_array::test(); p9_1_2_zero_then_default_initialized::test(); return 0; } diff --git a/tests/std/dcl.dcl/dcl.decl/dcl.init/out/refcount/dcl.init.aggr.rs b/tests/std/dcl.dcl/dcl.decl/dcl.init/out/refcount/dcl.init.aggr.rs new file mode 100644 index 0000000000..13b582e74e --- /dev/null +++ b/tests/std/dcl.dcl/dcl.decl/dcl.init/out/refcount/dcl.init.aggr.rs @@ -0,0 +1,800 @@ +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}; +thread_local!( + pub static copies_0: Value = Rc::new(RefCell::new(0)); +); +#[derive( + Record, + ByteRepr, + ZeroInitialization, + FnPtrArg, + Destructible, + Memberwise, + MoveConstructible, + MoveAssignable, + Swappable, +)] +#[memberwise(copy_assign)] +#[derive(Conversions)] +#[conversions(copy)] +#[constructor(constructor_1, i32)] +#[constructor(constructor_2, LvalueRef>)] +#[byte_size(4)] +pub struct p4_2_elements_from_initializer_clauses_S { + #[offset(0)] + pub value: i32, +} +impl p4_2_elements_from_initializer_clauses_S { + pub fn constructor_1(this: Ptr, mut v: i32) { + field!(this, value).write(v); + } + pub fn constructor_2( + this: Ptr, + other: LvalueRef>, + ) { + let other = other.as_pointer(); + field!(this, value).write((other.with(|__s| __s.value) + 100)); + (*copies_0.with(Value::clone).borrow_mut()).prefix_inc(); + } +} +impl Constructor<()> for p4_2_elements_from_initializer_clauses_S { + fn constructor(this: Ptr, _: ()) { + { + p4_2_elements_from_initializer_clauses_S::constructor_1(this.clone(), 0); + } + } +} +impl Default for p4_2_elements_from_initializer_clauses_S { + fn default() -> Self { + { + { + let __v: Value = + Rc::new(RefCell::new(ZeroInitialization::zero_init())); + p4_2_elements_from_initializer_clauses_S::constructor_1(__v.as_pointer(), 0); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + } + } + } +} +#[derive( + Record, ByteRepr, ZeroInitialization, FnPtrArg, Destructible, Memberwise, Swappable, Default, +)] +#[memberwise(copy_constructor, move_constructor, move_assign, copy_assign)] +#[derive(Conversions)] +#[conversions(copy, move)] +#[constructor(constructor_1)] +#[byte_size(8)] +pub struct p4_2_elements_from_initializer_clauses_Pair { + #[offset(0)] + #[byte_size(4)] + pub first: p4_2_elements_from_initializer_clauses_S, + #[offset(4)] + #[byte_size(4)] + pub second: p4_2_elements_from_initializer_clauses_S, +} +impl p4_2_elements_from_initializer_clauses_Pair { + pub fn constructor_1(this: Ptr) { + p4_2_elements_from_initializer_clauses_S::constructor_1(field_ptr!(this, first), 0); + p4_2_elements_from_initializer_clauses_S::constructor_1(field_ptr!(this, second), 0); + } +} +#[derive( + Record, ByteRepr, ZeroInitialization, FnPtrArg, Destructible, Memberwise, Swappable, Default, +)] +#[memberwise(copy_constructor, move_constructor, move_assign, copy_assign)] +#[derive(Conversions)] +#[conversions(copy, move)] +#[constructor(constructor_1)] +#[byte_size(8)] +pub struct p4_2_elements_from_initializer_clauses_Mixed { + #[offset(0)] + pub number: i32, + #[offset(4)] + #[byte_size(4)] + pub object: p4_2_elements_from_initializer_clauses_S, +} +impl p4_2_elements_from_initializer_clauses_Mixed { + pub fn constructor_1(this: Ptr) { + field!(this, number).write(0_i32); + p4_2_elements_from_initializer_clauses_S::constructor_1(field_ptr!(this, object), 0); + } +} +#[derive( + Record, ByteRepr, ZeroInitialization, FnPtrArg, Destructible, Memberwise, Swappable, Default, +)] +#[memberwise(copy_constructor, move_constructor, move_assign, copy_assign)] +#[derive(Conversions)] +#[conversions(copy, move)] +#[constructor(constructor_1)] +#[byte_size(12)] +pub struct p4_2_elements_from_initializer_clauses_Outer { + #[offset(0)] + #[byte_size(8)] + pub pair: p4_2_elements_from_initializer_clauses_Pair, + #[offset(8)] + #[byte_size(4)] + pub last: p4_2_elements_from_initializer_clauses_S, +} +impl p4_2_elements_from_initializer_clauses_Outer { + pub fn constructor_1(this: Ptr) { + p4_2_elements_from_initializer_clauses_Pair::constructor_1(field_ptr!(this, pair)); + p4_2_elements_from_initializer_clauses_S::constructor_1(field_ptr!(this, last), 0); + } +} +#[derive( + Record, + ByteRepr, + ZeroInitialization, + FnPtrArg, + DefaultInitializable, + Memberwise, + Swappable, + Default, +)] +#[memberwise(copy_constructor, move_constructor, copy_assign, move_assign)] +#[derive(Conversions)] +#[conversions(copy, move)] +#[constructor(constructor_1, i32)] +#[byte_size(8)] +pub struct p4_2_elements_from_initializer_clauses_Holder { + #[offset(0)] + #[byte_size(8)] + pub pair: p4_2_elements_from_initializer_clauses_Pair, +} +impl p4_2_elements_from_initializer_clauses_Holder { + pub fn constructor_1(this: Ptr, mut v: i32) { + { + p4_2_elements_from_initializer_clauses_S::constructor_1( + field_ptr!(field_ptr!(this, pair), first), + v, + ); + p4_2_elements_from_initializer_clauses_S::constructor_1( + field_ptr!(field_ptr!(this, pair), second), + (v + 1), + ); + }; + } +} +pub fn make_explicit_1(__return: Ptr, mut v: i32) { + { + p4_2_elements_from_initializer_clauses_S::constructor_1( + field_ptr!(__return.clone(), first), + v, + ); + p4_2_elements_from_initializer_clauses_S::constructor_1( + field_ptr!(__return.clone(), second), + (v + 1), + ); + }; + return; +} +pub fn make_braced_2(__return: Ptr, mut v: i32) { + { + p4_2_elements_from_initializer_clauses_S::constructor_1( + field_ptr!(__return.clone(), first), + v, + ); + p4_2_elements_from_initializer_clauses_S::constructor_1( + field_ptr!(__return.clone(), second), + (v + 1), + ); + }; + return; +} +pub fn sum_3(pair: Ptr) -> i32 { + return ({ pair.with(|__s| __s.first.value) } + { pair.with(|__s| __s.second.value) }); +} +pub fn test_4() { + object!( + local, + p4_2_elements_from_initializer_clauses_Pair, + |local| { + p4_2_elements_from_initializer_clauses_S::constructor_1( + field_ptr!(local.clone(), first), + 1, + ); + p4_2_elements_from_initializer_clauses_S::constructor_1( + field_ptr!(local.clone(), second), + 2, + ); + } + ); + assert!(({ (*local.borrow()).first.value } == 1)); + assert!(({ (*local.borrow()).second.value } == 2)); + object!( + copy_list, + p4_2_elements_from_initializer_clauses_Pair, + |copy_list| { + p4_2_elements_from_initializer_clauses_S::constructor_1( + field_ptr!(copy_list.clone(), first), + 3, + ); + p4_2_elements_from_initializer_clauses_S::constructor_1( + field_ptr!(copy_list.clone(), second), + 4, + ); + } + ); + assert!(({ (*copy_list.borrow()).first.value } == 3)); + assert!(({ (*copy_list.borrow()).second.value } == 4)); + object!( + from_explicit, + p4_2_elements_from_initializer_clauses_Pair, + |from_explicit| ({ make_explicit_1(Ptr::clone(&from_explicit.clone()), 5,) }) + ); + assert!(({ (*from_explicit.borrow()).first.value } == 5)); + assert!(({ (*from_explicit.borrow()).second.value } == 6)); + object!( + from_braced, + p4_2_elements_from_initializer_clauses_Pair, + |from_braced| ({ make_braced_2(Ptr::clone(&from_braced.clone()), 7,) }) + ); + assert!(({ (*from_braced.borrow()).first.value } == 7)); + assert!(({ (*from_braced.borrow()).second.value } == 8)); + { + let __tmp_0: Value = + Rc::new(RefCell::new(ZeroInitialization::zero_init())); + assert!( + ({ + { + p4_2_elements_from_initializer_clauses_S::constructor_1( + field_ptr!(__tmp_0.as_pointer(), first), + 9, + ); + p4_2_elements_from_initializer_clauses_S::constructor_1( + field_ptr!(__tmp_0.as_pointer(), second), + 10, + ); + }; + __tmp_0.as_pointer() + } + .with(|__s| __s.second.value) + == 10) + ) + }; + assert!( + (({ + let _pair: Value = + Rc::new(RefCell::new(ZeroInitialization::zero_init())); + { + p4_2_elements_from_initializer_clauses_S::constructor_1( + field_ptr!(_pair.as_pointer(), first), + 11, + ); + p4_2_elements_from_initializer_clauses_S::constructor_1( + field_ptr!(_pair.as_pointer(), second), + 12, + ); + }; + sum_3(_pair.as_pointer()) + }) == 23) + ); + object!( + mixed, + p4_2_elements_from_initializer_clauses_Mixed, + |mixed| { + (field_ptr!(mixed.clone(), number)).write(13); + p4_2_elements_from_initializer_clauses_S::constructor_1( + field_ptr!(mixed.clone(), object), + 14, + ); + } + ); + assert!(({ (*mixed.borrow()).number } == 13)); + assert!(({ (*mixed.borrow()).object.value } == 14)); + object!( + outer, + p4_2_elements_from_initializer_clauses_Outer, + |outer| { + { + p4_2_elements_from_initializer_clauses_S::constructor_1( + field_ptr!(field_ptr!(outer.clone(), pair), first), + 15, + ); + p4_2_elements_from_initializer_clauses_S::constructor_1( + field_ptr!(field_ptr!(outer.clone(), pair), second), + 16, + ); + }; + p4_2_elements_from_initializer_clauses_S::constructor_1( + field_ptr!(outer.clone(), last), + 17, + ); + } + ); + assert!(({ (*outer.borrow()).pair.first.value } == 15)); + assert!(({ (*outer.borrow()).pair.second.value } == 16)); + assert!(({ (*outer.borrow()).last.value } == 17)); + object!( + elided, + p4_2_elements_from_initializer_clauses_Outer, + |elided| { + { + p4_2_elements_from_initializer_clauses_S::constructor_1( + field_ptr!(field_ptr!(elided.clone(), pair), first), + 18, + ); + p4_2_elements_from_initializer_clauses_S::constructor_1( + field_ptr!(field_ptr!(elided.clone(), pair), second), + 19, + ); + }; + p4_2_elements_from_initializer_clauses_S::constructor_1( + field_ptr!(elided.clone(), last), + 20, + ); + } + ); + assert!(({ (*elided.borrow()).pair.first.value } == 18)); + assert!(({ (*elided.borrow()).last.value } == 20)); + object!( + holder, + p4_2_elements_from_initializer_clauses_Holder::constructor_1(21) + ); + assert!(({ (*holder.borrow()).pair.first.value } == 21)); + assert!(({ (*holder.borrow()).pair.second.value } == 22)); + let mut yes: bool = true; + object!( + chosen, + p4_2_elements_from_initializer_clauses_Pair, + |chosen| if yes { + { + p4_2_elements_from_initializer_clauses_S::constructor_1( + field_ptr!(chosen.clone(), first), + 23, + ); + p4_2_elements_from_initializer_clauses_S::constructor_1( + field_ptr!(chosen.clone(), second), + 24, + ); + }; + } else { + { + p4_2_elements_from_initializer_clauses_S::constructor_1( + field_ptr!(chosen.clone(), first), + 25, + ); + p4_2_elements_from_initializer_clauses_S::constructor_1( + field_ptr!(chosen.clone(), second), + 26, + ); + }; + } + ); + assert!(({ (*chosen.borrow()).first.value } == 23)); + let mut allocated: Ptr = { + let __p = Ptr::alloc(::zero_init()); + { + p4_2_elements_from_initializer_clauses_S::constructor_1( + field_ptr!(__p.clone(), first), + 27, + ); + p4_2_elements_from_initializer_clauses_S::constructor_1( + field_ptr!(__p.clone(), second), + 28, + ); + }; + __p + }; + assert!((allocated.with(|__s| __s.second.value) == 28)); + allocated.delete(); + assert!((copies_0.with(|rc| *rc.borrow()) == 0)); +} +thread_local!( + pub static copies_5: Value = Rc::new(RefCell::new(0)); +); +#[derive( + Record, + ByteRepr, + ZeroInitialization, + FnPtrArg, + Destructible, + Memberwise, + MoveConstructible, + MoveAssignable, + Swappable, +)] +#[memberwise(copy_assign)] +#[derive(Conversions)] +#[conversions(copy)] +#[constructor(constructor_1, i32)] +#[constructor(constructor_2, LvalueRef>)] +#[byte_size(4)] +pub struct p4_2_array_elements_from_initializer_clauses_S { + #[offset(0)] + pub value: i32, +} +impl p4_2_array_elements_from_initializer_clauses_S { + pub fn constructor_1(this: Ptr, mut v: i32) { + field!(this, value).write(v); + } + pub fn constructor_2( + this: Ptr, + other: LvalueRef>, + ) { + let other = other.as_pointer(); + field!(this, value).write((other.with(|__s| __s.value) + 100)); + (*copies_5.with(Value::clone).borrow_mut()).prefix_inc(); + } +} +impl Constructor<()> for p4_2_array_elements_from_initializer_clauses_S { + fn constructor(this: Ptr, _: ()) { + { + p4_2_array_elements_from_initializer_clauses_S::constructor_1(this.clone(), 0); + } + } +} +impl Default for p4_2_array_elements_from_initializer_clauses_S { + fn default() -> Self { + { + { + let __v: Value = + Rc::new(RefCell::new(ZeroInitialization::zero_init())); + p4_2_array_elements_from_initializer_clauses_S::constructor_1(__v.as_pointer(), 0); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + } + } + } +} +#[derive( + Record, ByteRepr, ZeroInitialization, FnPtrArg, Destructible, Memberwise, Swappable, Default, +)] +#[memberwise(copy_constructor, move_constructor, move_assign, copy_assign)] +#[derive(Conversions)] +#[conversions(copy, move)] +#[constructor(constructor_1)] +#[byte_size(8)] +pub struct p4_2_array_elements_from_initializer_clauses_Pair { + #[offset(0)] + #[byte_size(4)] + pub first: p4_2_array_elements_from_initializer_clauses_S, + #[offset(4)] + #[byte_size(4)] + pub second: p4_2_array_elements_from_initializer_clauses_S, +} +impl p4_2_array_elements_from_initializer_clauses_Pair { + pub fn constructor_1(this: Ptr) { + p4_2_array_elements_from_initializer_clauses_S::constructor_1(field_ptr!(this, first), 0); + p4_2_array_elements_from_initializer_clauses_S::constructor_1(field_ptr!(this, second), 0); + } +} +#[derive(Record, ByteRepr, ZeroInitialization, FnPtrArg, Destructible, Memberwise, Swappable)] +#[memberwise(copy_constructor, move_constructor, move_assign, copy_assign)] +#[derive(Conversions)] +#[conversions(copy, move)] +#[constructor(constructor_1)] +#[byte_size(8)] +pub struct p4_2_array_elements_from_initializer_clauses_WithArray { + #[offset(0)] + #[byte_size(8)] + #[array_len(2)] + pub items: Box<[p4_2_array_elements_from_initializer_clauses_S]>, +} +impl p4_2_array_elements_from_initializer_clauses_WithArray { + pub fn constructor_1(this: Ptr) { + for __tmp_1 in 0..2 { + p4_2_array_elements_from_initializer_clauses_S::constructor_1( + field_ptr!(this, items).decay().offset(__tmp_1), + 0, + ); + } + } +} +impl Default for p4_2_array_elements_from_initializer_clauses_WithArray { + fn default() -> Self { + { + p4_2_array_elements_from_initializer_clauses_WithArray { + items: (0..2) + .map(|_| ::default()) + .collect::>(), + } + } + } +} +pub fn second_of_6(items: Ptr) -> i32 { + return elem!(items, 1).with(|__s| __s.value); +} +pub fn test_7() { + let items: Value> = Rc::new( + RefCell::new((0..2).map(|_| ZeroInitialization::zero_init()).collect()), + ); + { + p4_2_array_elements_from_initializer_clauses_S::constructor_1( + items.as_pointer().decay().offset(0), + 1, + ); + p4_2_array_elements_from_initializer_clauses_S::constructor_1( + items.as_pointer().decay().offset(1), + 2, + ); + }; + assert!(({ (*items.borrow())[(0) as usize].value } == 1)); + assert!(({ (*items.borrow())[(1) as usize].value } == 2)); + let converted: Value> = Rc::new( + RefCell::new((0..2).map(|_| ZeroInitialization::zero_init()).collect()), + ); + { + p4_2_array_elements_from_initializer_clauses_S::constructor_1( + converted.as_pointer().decay().offset(0), + 3, + ); + p4_2_array_elements_from_initializer_clauses_S::constructor_1( + converted.as_pointer().decay().offset(1), + 4, + ); + }; + assert!(({ (*converted.borrow())[(0) as usize].value } == 3)); + assert!(({ (*converted.borrow())[(1) as usize].value } == 4)); + let pairs: Value> = Rc::new( + RefCell::new((0..2).map(|_| ZeroInitialization::zero_init()).collect()), + ); + { + { + p4_2_array_elements_from_initializer_clauses_S::constructor_1( + field_ptr!(pairs.as_pointer().decay().offset(0), first), + 5, + ); + p4_2_array_elements_from_initializer_clauses_S::constructor_1( + field_ptr!(pairs.as_pointer().decay().offset(0), second), + 6, + ); + }; + { + p4_2_array_elements_from_initializer_clauses_S::constructor_1( + field_ptr!(pairs.as_pointer().decay().offset(1), first), + 7, + ); + p4_2_array_elements_from_initializer_clauses_S::constructor_1( + field_ptr!(pairs.as_pointer().decay().offset(1), second), + 8, + ); + }; + }; + assert!(({ (*pairs.borrow())[(0) as usize].second.value } == 6)); + assert!(({ (*pairs.borrow())[(1) as usize].first.value } == 7)); + let grid: Value]>> = + Rc::new(RefCell::new( + (0..2) + .map(|_| (0..2).map(|_| ZeroInitialization::zero_init()).collect()) + .collect(), + )); + { + { + p4_2_array_elements_from_initializer_clauses_S::constructor_1( + grid.as_pointer().decay().offset(0).decay().offset(0), + 9, + ); + p4_2_array_elements_from_initializer_clauses_S::constructor_1( + grid.as_pointer().decay().offset(0).decay().offset(1), + 10, + ); + }; + { + p4_2_array_elements_from_initializer_clauses_S::constructor_1( + grid.as_pointer().decay().offset(1).decay().offset(0), + 11, + ); + p4_2_array_elements_from_initializer_clauses_S::constructor_1( + grid.as_pointer().decay().offset(1).decay().offset(1), + 12, + ); + }; + }; + assert!(({ (*grid.borrow())[(0) as usize][(1) as usize].value } == 10)); + assert!(({ (*grid.borrow())[(1) as usize][(0) as usize].value } == 11)); + object!( + holder, + p4_2_array_elements_from_initializer_clauses_WithArray, + |holder| { + { + p4_2_array_elements_from_initializer_clauses_S::constructor_1( + field_ptr!(holder.clone(), items).decay().offset(0), + 13, + ); + p4_2_array_elements_from_initializer_clauses_S::constructor_1( + field_ptr!(holder.clone(), items).decay().offset(1), + 14, + ); + }; + } + ); + assert!((array_elem!(holder.as_pointer(), items, 0).with(|__s| __s.value) == 13)); + assert!((array_elem!(holder.as_pointer(), items, 1).with(|__s| __s.value) == 14)); + assert!( + (({ + let _items: Value> = Rc::new( + RefCell::new((0..2).map(|_| ZeroInitialization::zero_init()).collect()), + ); + { + p4_2_array_elements_from_initializer_clauses_S::constructor_1( + _items.as_pointer().decay().offset(0), + 15, + ); + p4_2_array_elements_from_initializer_clauses_S::constructor_1( + _items.as_pointer().decay().offset(1), + 16, + ); + }; + second_of_6(_items.decay()) + }) == 16) + ); + assert!((copies_5.with(|rc| *rc.borrow()) == 0)); +} +thread_local!( + pub static copies_8: Value = Rc::new(RefCell::new(0)); +); +#[derive( + Record, + ByteRepr, + ZeroInitialization, + FnPtrArg, + Destructible, + Memberwise, + MoveConstructible, + MoveAssignable, + Swappable, +)] +#[memberwise(copy_assign)] +#[derive(Conversions)] +#[conversions(copy)] +#[constructor(constructor_1, i32)] +#[constructor(constructor_2, LvalueRef>)] +#[byte_size(4)] +pub struct p5_2_remaining_elements_from_empty_list_S { + #[offset(0)] + pub value: i32, +} +impl p5_2_remaining_elements_from_empty_list_S { + pub fn constructor_1(this: Ptr, mut v: i32) { + field!(this, value).write(v); + } + pub fn constructor_2( + this: Ptr, + other: LvalueRef>, + ) { + let other = other.as_pointer(); + field!(this, value).write((other.with(|__s| __s.value) + 100)); + (*copies_8.with(Value::clone).borrow_mut()).prefix_inc(); + } +} +impl Constructor<()> for p5_2_remaining_elements_from_empty_list_S { + fn constructor(this: Ptr, _: ()) { + { + p5_2_remaining_elements_from_empty_list_S::constructor_1(this.clone(), 7); + } + } +} +impl Default for p5_2_remaining_elements_from_empty_list_S { + fn default() -> Self { + { + { + let __v: Value = + Rc::new(RefCell::new(ZeroInitialization::zero_init())); + p5_2_remaining_elements_from_empty_list_S::constructor_1(__v.as_pointer(), 7); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + } + } + } +} +#[derive( + Record, ByteRepr, ZeroInitialization, FnPtrArg, Destructible, Memberwise, Swappable, Default, +)] +#[memberwise(copy_constructor, move_constructor, move_assign, copy_assign)] +#[derive(Conversions)] +#[conversions(copy, move)] +#[constructor(constructor_1)] +#[byte_size(8)] +pub struct p5_2_remaining_elements_from_empty_list_Pair { + #[offset(0)] + #[byte_size(4)] + pub first: p5_2_remaining_elements_from_empty_list_S, + #[offset(4)] + #[byte_size(4)] + pub second: p5_2_remaining_elements_from_empty_list_S, +} +impl p5_2_remaining_elements_from_empty_list_Pair { + pub fn constructor_1(this: Ptr) { + p5_2_remaining_elements_from_empty_list_S::constructor_1(field_ptr!(this, first), 7); + p5_2_remaining_elements_from_empty_list_S::constructor_1(field_ptr!(this, second), 7); + } +} +pub fn test_9() { + let filled: Value> = Rc::new(RefCell::new( + (0..4).map(|_| ZeroInitialization::zero_init()).collect(), + )); + { + p5_2_remaining_elements_from_empty_list_S::constructor_1( + filled.as_pointer().decay().offset(0), + 1, + ); + for __tmp_2 in 1..4 { + p5_2_remaining_elements_from_empty_list_S::constructor_1( + filled.as_pointer().decay().offset(__tmp_2), + 7, + ); + } + }; + assert!(({ (*filled.borrow())[(0) as usize].value } == 1)); + assert!(({ (*filled.borrow())[(1) as usize].value } == 7)); + assert!(({ (*filled.borrow())[(3) as usize].value } == 7)); + let empty: Value> = Rc::new(RefCell::new( + (0..3).map(|_| ZeroInitialization::zero_init()).collect(), + )); + { + for __tmp_3 in 0..3 { + p5_2_remaining_elements_from_empty_list_S::constructor_1( + empty.as_pointer().decay().offset(__tmp_3), + 7, + ); + } + }; + assert!(({ (*empty.borrow())[(0) as usize].value } == 7)); + assert!(({ (*empty.borrow())[(2) as usize].value } == 7)); + object!( + partial, + p5_2_remaining_elements_from_empty_list_Pair, + |partial| { + p5_2_remaining_elements_from_empty_list_S::constructor_1( + field_ptr!(partial.clone(), first), + 2, + ); + p5_2_remaining_elements_from_empty_list_S::constructor_1( + field_ptr!(partial.clone(), second), + 7, + ); + } + ); + assert!(({ (*partial.borrow()).first.value } == 2)); + assert!(({ (*partial.borrow()).second.value } == 7)); + let pairs: Value> = Rc::new(RefCell::new( + (0..3).map(|_| ZeroInitialization::zero_init()).collect(), + )); + { + { + p5_2_remaining_elements_from_empty_list_S::constructor_1( + field_ptr!(pairs.as_pointer().decay().offset(0), first), + 3, + ); + p5_2_remaining_elements_from_empty_list_S::constructor_1( + field_ptr!(pairs.as_pointer().decay().offset(0), second), + 4, + ); + }; + for __tmp_4 in 1..3 { + { + p5_2_remaining_elements_from_empty_list_S::constructor_1( + field_ptr!(pairs.as_pointer().decay().offset(__tmp_4), first), + 7, + ); + p5_2_remaining_elements_from_empty_list_S::constructor_1( + field_ptr!(pairs.as_pointer().decay().offset(__tmp_4), second), + 7, + ); + }; + } + }; + assert!(({ (*pairs.borrow())[(0) as usize].second.value } == 4)); + assert!(({ (*pairs.borrow())[(2) as usize].first.value } == 7)); + assert!((copies_8.with(|rc| *rc.borrow()) == 0)); +} +pub fn main() { + __cpp2rust_init_globals(); + libcc2rs::cpp2rust_exit(main_0()); +} +fn main_0() -> i32 { + ({ test_4() }); + ({ test_7() }); + ({ test_9() }); + return 0; +} +pub fn __cpp2rust_init_globals() { + let _ = copies_0.with(|_| ()); + let _ = copies_5.with(|_| ()); + let _ = copies_8.with(|_| ()); +} diff --git a/tests/std/dcl.dcl/dcl.decl/dcl.init/out/refcount/dcl.init.general.rs b/tests/std/dcl.dcl/dcl.decl/dcl.init/out/refcount/dcl.init.general.rs index 8cf6d61f92..6366797f16 100644 --- a/tests/std/dcl.dcl/dcl.decl/dcl.init/out/refcount/dcl.init.general.rs +++ b/tests/std/dcl.dcl/dcl.decl/dcl.init/out/refcount/dcl.init.general.rs @@ -403,6 +403,129 @@ pub fn test_21() { } heap_numbers.delete(); } +thread_local!( + pub static constructed_22: Value = Rc::new(RefCell::new(0)); +); +#[derive( + Record, + ByteRepr, + ZeroInitialization, + FnPtrArg, + Destructible, + Memberwise, + MoveConstructible, + MoveAssignable, + Swappable, +)] +#[memberwise(copy_assign)] +#[derive(Conversions)] +#[conversions(copy)] +#[constructor(constructor_1)] +#[constructor(constructor_2, LvalueRef>)] +#[byte_size(4)] +pub struct p7_2_default_initialized_array_S { + #[offset(0)] + pub value: i32, +} +impl p7_2_default_initialized_array_S { + pub fn constructor_1(this: Ptr) { + field!(this, value).write(7); + (*constructed_22.with(Value::clone).borrow_mut()).prefix_inc(); + } + pub fn constructor_2( + this: Ptr, + other: LvalueRef>, + ) { + let other = other.as_pointer(); + field!(this, value).write((other.with(|__s| __s.value) + 100)); + (*constructed_22.with(Value::clone).borrow_mut()).prefix_inc(); + } +} +impl Default for p7_2_default_initialized_array_S { + fn default() -> Self { + { + { + let __v: Value = + Rc::new(RefCell::new(ZeroInitialization::zero_init())); + p7_2_default_initialized_array_S::constructor_1(__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + } + } + } +} +#[derive(Record, ByteRepr, ZeroInitialization, FnPtrArg, Destructible, Memberwise, Swappable)] +#[memberwise(copy_constructor, move_constructor, move_assign, copy_assign)] +#[derive(Conversions)] +#[conversions(copy, move)] +#[constructor(constructor_1)] +#[byte_size(8)] +pub struct p7_2_default_initialized_array_WithArray { + #[offset(0)] + #[byte_size(8)] + #[array_len(2)] + pub items: Box<[p7_2_default_initialized_array_S]>, +} +impl p7_2_default_initialized_array_WithArray { + pub fn constructor_1(this: Ptr) { + for __tmp_0 in 0..2 { + p7_2_default_initialized_array_S::constructor_1( + field_ptr!(this, items).decay().offset(__tmp_0), + ); + } + } +} +impl Default for p7_2_default_initialized_array_WithArray { + fn default() -> Self { + { + p7_2_default_initialized_array_WithArray { + items: (0..2) + .map(|_| ::default()) + .collect::>(), + } + } + } +} +pub fn test_23() { + let items: Value> = Rc::new(RefCell::new( + (0..3).map(|_| ZeroInitialization::zero_init()).collect(), + )); + for __tmp_1 in 0..3 { + p7_2_default_initialized_array_S::constructor_1(items.as_pointer().decay().offset(__tmp_1)); + } + assert!(({ (*items.borrow())[(0) as usize].value } == 7)); + assert!(({ (*items.borrow())[(2) as usize].value } == 7)); + let grid: Value]>> = Rc::new(RefCell::new( + (0..2) + .map(|_| (0..3).map(|_| ZeroInitialization::zero_init()).collect()) + .collect(), + )); + for __tmp_2 in 0..2 { + for __tmp_3 in 0..3 { + p7_2_default_initialized_array_S::constructor_1( + grid.as_pointer() + .decay() + .offset(__tmp_2) + .decay() + .offset(__tmp_3), + ); + } + } + assert!(({ (*grid.borrow())[(0) as usize][(0) as usize].value } == 7)); + assert!(({ (*grid.borrow())[(1) as usize][(2) as usize].value } == 7)); + object!( + holder, + p7_2_default_initialized_array_WithArray::constructor_1() + ); + assert!((array_elem!(holder.as_pointer(), items, 1).with(|__s| __s.value) == 7)); + let mut allocated: Ptr = Ptr::alloc_array( + (0..2_usize) + .map(|_| ::default()) + .collect::>(), + ); + assert!((elem!(allocated, 1).with(|__s| __s.value) == 7)); + allocated.delete(); + assert!((constructed_22.with(|rc| *rc.borrow()) == 13)); +} #[derive( Copy, Clone, @@ -506,7 +629,7 @@ impl p9_1_2_zero_then_default_initialized_HoldsLeavesMemberAlone { )); } } -pub fn test_22() { +pub fn test_24() { object!( value, p9_1_2_zero_then_default_initialized_WithDefaultMember::constructor_1() @@ -547,7 +670,8 @@ fn main_0() -> i32 { ({ test_15() }); ({ test_17() }); ({ test_21() }); - ({ test_22() }); + ({ test_23() }); + ({ test_24() }); return 0; } pub fn __cpp2rust_init_globals() { @@ -565,4 +689,5 @@ pub fn __cpp2rust_init_globals() { let _ = static_numbers_18.with(|_| ()); let _ = static_matrix_19.with(|_| ()); let _ = static_pairs_20.with(|_| ()); + let _ = constructed_22.with(|_| ()); } diff --git a/tests/std/dcl.dcl/dcl.decl/dcl.init/out/unsafe/dcl.init.aggr.rs b/tests/std/dcl.dcl/dcl.decl/dcl.init/out/unsafe/dcl.init.aggr.rs new file mode 100644 index 0000000000..e4d36530c2 --- /dev/null +++ b/tests/std/dcl.dcl/dcl.decl/dcl.init/out/unsafe/dcl.init.aggr.rs @@ -0,0 +1,748 @@ +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; +pub static mut copies_0: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +#[repr(C)] +#[derive( + FnPtrArg, + ZeroInitializationUnsafe, + DestructibleUnsafe, + MemberwiseUnsafe, + MoveConstructibleUnsafe, + MoveAssignableUnsafe, + SwappableUnsafe, +)] +#[memberwise(copy_assign)] +#[derive(ConversionsUnsafe)] +#[conversions(copy)] +#[constructor(constructor_1, i32)] +#[constructor(constructor_2, LvalueRefUnsafe>)] +pub struct p4_2_elements_from_initializer_clauses_S { + pub value: i32, +} +impl p4_2_elements_from_initializer_clauses_S { + pub unsafe fn constructor_1(this: *mut Self, mut v: i32) { + (&raw mut (*this).value).write(v); + } + pub unsafe fn constructor_2( + this: *mut Self, + other: LvalueRefUnsafe>, + ) { + let other = (other.as_ptr() as *const p4_2_elements_from_initializer_clauses_S); + (&raw mut (*this).value).write((((*other).value) + (100))); + (*std::cell::LazyCell::force_mut(&mut *&raw mut copies_0)).prefix_inc(); + } +} +impl ConstructorUnsafe<()> for p4_2_elements_from_initializer_clauses_S { + unsafe fn constructor(this: *mut Self, _: ()) { + unsafe { + p4_2_elements_from_initializer_clauses_S::constructor_1(this, 0); + } + } +} +impl Default for p4_2_elements_from_initializer_clauses_S { + fn default() -> Self { + unsafe { + { + let mut __v: p4_2_elements_from_initializer_clauses_S = + ZeroInitializationUnsafe::zero_init(); + p4_2_elements_from_initializer_clauses_S::constructor_1(&raw mut __v, 0); + __v + } + } + } +} +#[repr(C)] +#[derive( + FnPtrArg, + ZeroInitializationUnsafe, + DestructibleUnsafe, + MemberwiseUnsafe, + SwappableUnsafe, + Default, +)] +#[memberwise(copy_constructor, move_constructor, move_assign, copy_assign)] +#[derive(ConversionsUnsafe)] +#[conversions(copy, move)] +#[constructor(constructor_1)] +pub struct p4_2_elements_from_initializer_clauses_Pair { + pub first: p4_2_elements_from_initializer_clauses_S, + pub second: p4_2_elements_from_initializer_clauses_S, +} +impl p4_2_elements_from_initializer_clauses_Pair { + pub unsafe fn constructor_1(this: *mut Self) { + p4_2_elements_from_initializer_clauses_S::constructor_1(&raw mut (*this).first, 0); + p4_2_elements_from_initializer_clauses_S::constructor_1(&raw mut (*this).second, 0); + } +} +#[repr(C)] +#[derive( + FnPtrArg, + ZeroInitializationUnsafe, + DestructibleUnsafe, + MemberwiseUnsafe, + SwappableUnsafe, + Default, +)] +#[memberwise(copy_constructor, move_constructor, move_assign, copy_assign)] +#[derive(ConversionsUnsafe)] +#[conversions(copy, move)] +#[constructor(constructor_1)] +pub struct p4_2_elements_from_initializer_clauses_Mixed { + pub number: i32, + pub object: p4_2_elements_from_initializer_clauses_S, +} +impl p4_2_elements_from_initializer_clauses_Mixed { + pub unsafe fn constructor_1(this: *mut Self) { + (&raw mut (*this).number).write(0_i32); + p4_2_elements_from_initializer_clauses_S::constructor_1(&raw mut (*this).object, 0); + } +} +#[repr(C)] +#[derive( + FnPtrArg, + ZeroInitializationUnsafe, + DestructibleUnsafe, + MemberwiseUnsafe, + SwappableUnsafe, + Default, +)] +#[memberwise(copy_constructor, move_constructor, move_assign, copy_assign)] +#[derive(ConversionsUnsafe)] +#[conversions(copy, move)] +#[constructor(constructor_1)] +pub struct p4_2_elements_from_initializer_clauses_Outer { + pub pair: p4_2_elements_from_initializer_clauses_Pair, + pub last: p4_2_elements_from_initializer_clauses_S, +} +impl p4_2_elements_from_initializer_clauses_Outer { + pub unsafe fn constructor_1(this: *mut Self) { + p4_2_elements_from_initializer_clauses_Pair::constructor_1(&raw mut (*this).pair); + p4_2_elements_from_initializer_clauses_S::constructor_1(&raw mut (*this).last, 0); + } +} +#[repr(C)] +#[derive( + FnPtrArg, + ZeroInitializationUnsafe, + DefaultInitializableUnsafe, + MemberwiseUnsafe, + SwappableUnsafe, + Default, +)] +#[memberwise(copy_constructor, move_constructor, copy_assign, move_assign)] +#[derive(ConversionsUnsafe)] +#[conversions(copy, move)] +#[constructor(constructor_1, i32)] +pub struct p4_2_elements_from_initializer_clauses_Holder { + pub pair: p4_2_elements_from_initializer_clauses_Pair, +} +impl p4_2_elements_from_initializer_clauses_Holder { + pub unsafe fn constructor_1(this: *mut Self, mut v: i32) { + { + p4_2_elements_from_initializer_clauses_S::constructor_1( + &raw mut (*&raw mut (*this).pair).first, + v, + ); + p4_2_elements_from_initializer_clauses_S::constructor_1( + &raw mut (*&raw mut (*this).pair).second, + ((v) + (1)), + ); + }; + } +} +pub unsafe fn make_explicit_1( + __return: *mut p4_2_elements_from_initializer_clauses_Pair, + mut v: i32, +) { + { + p4_2_elements_from_initializer_clauses_S::constructor_1(&raw mut (*__return).first, v); + p4_2_elements_from_initializer_clauses_S::constructor_1( + &raw mut (*__return).second, + ((v) + (1)), + ); + }; + return; +} +pub unsafe fn make_braced_2( + __return: *mut p4_2_elements_from_initializer_clauses_Pair, + mut v: i32, +) { + { + p4_2_elements_from_initializer_clauses_S::constructor_1(&raw mut (*__return).first, v); + p4_2_elements_from_initializer_clauses_S::constructor_1( + &raw mut (*__return).second, + ((v) + (1)), + ); + }; + return; +} +pub unsafe fn sum_3(pair: *const p4_2_elements_from_initializer_clauses_Pair) -> i32 { + return (((*pair).first.value) + ((*pair).second.value)); +} +pub unsafe fn test_4() { + object_unsafe!( + local, + p4_2_elements_from_initializer_clauses_Pair, + |local| { + p4_2_elements_from_initializer_clauses_S::constructor_1(&raw mut (*local).first, 1); + p4_2_elements_from_initializer_clauses_S::constructor_1(&raw mut (*local).second, 2); + } + ); + assert!(((local.first.value) == (1))); + assert!(((local.second.value) == (2))); + object_unsafe!( + copy_list, + p4_2_elements_from_initializer_clauses_Pair, + |copy_list| { + p4_2_elements_from_initializer_clauses_S::constructor_1(&raw mut (*copy_list).first, 3); + p4_2_elements_from_initializer_clauses_S::constructor_1( + &raw mut (*copy_list).second, + 4, + ); + } + ); + assert!(((copy_list.first.value) == (3))); + assert!(((copy_list.second.value) == (4))); + object_unsafe!( + from_explicit, + p4_2_elements_from_initializer_clauses_Pair, + |from_explicit| (unsafe { make_explicit_1(from_explicit, 5,) }) + ); + assert!(((from_explicit.first.value) == (5))); + assert!(((from_explicit.second.value) == (6))); + object_unsafe!( + from_braced, + p4_2_elements_from_initializer_clauses_Pair, + |from_braced| (unsafe { make_braced_2(from_braced, 7,) }) + ); + assert!(((from_braced.first.value) == (7))); + assert!(((from_braced.second.value) == (8))); + { + let mut __tmp_0: p4_2_elements_from_initializer_clauses_Pair = + ZeroInitializationUnsafe::zero_init(); + assert!( + (((*{ + { + p4_2_elements_from_initializer_clauses_S::constructor_1( + &raw mut (*&raw mut __tmp_0).first, + 9, + ); + p4_2_elements_from_initializer_clauses_S::constructor_1( + &raw mut (*&raw mut __tmp_0).second, + 10, + ); + }; + &mut __tmp_0 + }) + .second + .value) + == (10)) + ) + }; + assert!( + ((unsafe { + let mut _pair: p4_2_elements_from_initializer_clauses_Pair = + ZeroInitializationUnsafe::zero_init(); + { + p4_2_elements_from_initializer_clauses_S::constructor_1( + &raw mut (*&raw mut _pair).first, + 11, + ); + p4_2_elements_from_initializer_clauses_S::constructor_1( + &raw mut (*&raw mut _pair).second, + 12, + ); + }; + sum_3(&mut _pair) + }) == (23)) + ); + object_unsafe!( + mixed, + p4_2_elements_from_initializer_clauses_Mixed, + |mixed| { + (&raw mut (*mixed).number).write(13); + p4_2_elements_from_initializer_clauses_S::constructor_1(&raw mut (*mixed).object, 14); + } + ); + assert!(((mixed.number) == (13))); + assert!(((mixed.object.value) == (14))); + object_unsafe!( + outer, + p4_2_elements_from_initializer_clauses_Outer, + |outer| { + { + p4_2_elements_from_initializer_clauses_S::constructor_1( + &raw mut (*&raw mut (*outer).pair).first, + 15, + ); + p4_2_elements_from_initializer_clauses_S::constructor_1( + &raw mut (*&raw mut (*outer).pair).second, + 16, + ); + }; + p4_2_elements_from_initializer_clauses_S::constructor_1(&raw mut (*outer).last, 17); + } + ); + assert!(((outer.pair.first.value) == (15))); + assert!(((outer.pair.second.value) == (16))); + assert!(((outer.last.value) == (17))); + object_unsafe!( + elided, + p4_2_elements_from_initializer_clauses_Outer, + |elided| { + { + p4_2_elements_from_initializer_clauses_S::constructor_1( + &raw mut (*&raw mut (*elided).pair).first, + 18, + ); + p4_2_elements_from_initializer_clauses_S::constructor_1( + &raw mut (*&raw mut (*elided).pair).second, + 19, + ); + }; + p4_2_elements_from_initializer_clauses_S::constructor_1(&raw mut (*elided).last, 20); + } + ); + assert!(((elided.pair.first.value) == (18))); + assert!(((elided.last.value) == (20))); + object_unsafe!( + holder, + p4_2_elements_from_initializer_clauses_Holder::constructor_1(21) + ); + assert!(((holder.pair.first.value) == (21))); + assert!(((holder.pair.second.value) == (22))); + let mut yes: bool = true; + object_unsafe!( + chosen, + p4_2_elements_from_initializer_clauses_Pair, + |chosen| if yes { + { + p4_2_elements_from_initializer_clauses_S::constructor_1( + &raw mut (*chosen).first, + 23, + ); + p4_2_elements_from_initializer_clauses_S::constructor_1( + &raw mut (*chosen).second, + 24, + ); + }; + } else { + { + p4_2_elements_from_initializer_clauses_S::constructor_1( + &raw mut (*chosen).first, + 25, + ); + p4_2_elements_from_initializer_clauses_S::constructor_1( + &raw mut (*chosen).second, + 26, + ); + }; + } + ); + assert!(((chosen.first.value) == (23))); + let mut allocated: *mut p4_2_elements_from_initializer_clauses_Pair = { + let __p: *mut p4_2_elements_from_initializer_clauses_Pair = Box::leak(Box::new( + ::zero_init(), + )); + { + p4_2_elements_from_initializer_clauses_S::constructor_1(&raw mut (*__p).first, 27); + p4_2_elements_from_initializer_clauses_S::constructor_1(&raw mut (*__p).second, 28); + }; + __p + }; + assert!((((*allocated).second.value) == (28))); + { + let __p = allocated; + if !__p.is_null() { + ::std::mem::drop(Box::from_raw(__p)) + } + }; + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut copies_0)) == (0))); +} +pub static mut copies_5: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +#[repr(C)] +#[derive( + FnPtrArg, + ZeroInitializationUnsafe, + DestructibleUnsafe, + MemberwiseUnsafe, + MoveConstructibleUnsafe, + MoveAssignableUnsafe, + SwappableUnsafe, +)] +#[memberwise(copy_assign)] +#[derive(ConversionsUnsafe)] +#[conversions(copy)] +#[constructor(constructor_1, i32)] +#[constructor(constructor_2, LvalueRefUnsafe>)] +pub struct p4_2_array_elements_from_initializer_clauses_S { + pub value: i32, +} +impl p4_2_array_elements_from_initializer_clauses_S { + pub unsafe fn constructor_1(this: *mut Self, mut v: i32) { + (&raw mut (*this).value).write(v); + } + pub unsafe fn constructor_2( + this: *mut Self, + other: LvalueRefUnsafe>, + ) { + let other = (other.as_ptr() as *const p4_2_array_elements_from_initializer_clauses_S); + (&raw mut (*this).value).write((((*other).value) + (100))); + (*std::cell::LazyCell::force_mut(&mut *&raw mut copies_5)).prefix_inc(); + } +} +impl ConstructorUnsafe<()> for p4_2_array_elements_from_initializer_clauses_S { + unsafe fn constructor(this: *mut Self, _: ()) { + unsafe { + p4_2_array_elements_from_initializer_clauses_S::constructor_1(this, 0); + } + } +} +impl Default for p4_2_array_elements_from_initializer_clauses_S { + fn default() -> Self { + unsafe { + { + let mut __v: p4_2_array_elements_from_initializer_clauses_S = + ZeroInitializationUnsafe::zero_init(); + p4_2_array_elements_from_initializer_clauses_S::constructor_1(&raw mut __v, 0); + __v + } + } + } +} +#[repr(C)] +#[derive( + FnPtrArg, + ZeroInitializationUnsafe, + DestructibleUnsafe, + MemberwiseUnsafe, + SwappableUnsafe, + Default, +)] +#[memberwise(copy_constructor, move_constructor, move_assign, copy_assign)] +#[derive(ConversionsUnsafe)] +#[conversions(copy, move)] +#[constructor(constructor_1)] +pub struct p4_2_array_elements_from_initializer_clauses_Pair { + pub first: p4_2_array_elements_from_initializer_clauses_S, + pub second: p4_2_array_elements_from_initializer_clauses_S, +} +impl p4_2_array_elements_from_initializer_clauses_Pair { + pub unsafe fn constructor_1(this: *mut Self) { + p4_2_array_elements_from_initializer_clauses_S::constructor_1(&raw mut (*this).first, 0); + p4_2_array_elements_from_initializer_clauses_S::constructor_1(&raw mut (*this).second, 0); + } +} +#[repr(C)] +#[derive( + FnPtrArg, + ZeroInitializationUnsafe, + DestructibleUnsafe, + MemberwiseUnsafe, + SwappableUnsafe, + Default, +)] +#[memberwise(copy_constructor, move_constructor, move_assign, copy_assign)] +#[derive(ConversionsUnsafe)] +#[conversions(copy, move)] +#[constructor(constructor_1)] +pub struct p4_2_array_elements_from_initializer_clauses_WithArray { + pub items: [p4_2_array_elements_from_initializer_clauses_S; 2], +} +impl p4_2_array_elements_from_initializer_clauses_WithArray { + pub unsafe fn constructor_1(this: *mut Self) { + for __tmp_1 in 0..2 { + p4_2_array_elements_from_initializer_clauses_S::constructor_1( + &raw mut (*&raw mut (*this).items)[__tmp_1], + 0, + ); + } + } +} +pub unsafe fn second_of_6( + items: *const [p4_2_array_elements_from_initializer_clauses_S; 2], +) -> i32 { + return (*items)[(1) as usize].value; +} +pub unsafe fn test_7() { + let mut items: [p4_2_array_elements_from_initializer_clauses_S; 2] = + ZeroInitializationUnsafe::zero_init(); + { + p4_2_array_elements_from_initializer_clauses_S::constructor_1( + &raw mut (*&raw mut items)[0], + 1, + ); + p4_2_array_elements_from_initializer_clauses_S::constructor_1( + &raw mut (*&raw mut items)[1], + 2, + ); + }; + assert!(((items[(0) as usize].value) == (1))); + assert!(((items[(1) as usize].value) == (2))); + let mut converted: [p4_2_array_elements_from_initializer_clauses_S; 2] = + ZeroInitializationUnsafe::zero_init(); + { + p4_2_array_elements_from_initializer_clauses_S::constructor_1( + &raw mut (*&raw mut converted)[0], + 3, + ); + p4_2_array_elements_from_initializer_clauses_S::constructor_1( + &raw mut (*&raw mut converted)[1], + 4, + ); + }; + assert!(((converted[(0) as usize].value) == (3))); + assert!(((converted[(1) as usize].value) == (4))); + let mut pairs: [p4_2_array_elements_from_initializer_clauses_Pair; 2] = + ZeroInitializationUnsafe::zero_init(); + { + { + p4_2_array_elements_from_initializer_clauses_S::constructor_1( + &raw mut (*&raw mut (*&raw mut pairs)[0]).first, + 5, + ); + p4_2_array_elements_from_initializer_clauses_S::constructor_1( + &raw mut (*&raw mut (*&raw mut pairs)[0]).second, + 6, + ); + }; + { + p4_2_array_elements_from_initializer_clauses_S::constructor_1( + &raw mut (*&raw mut (*&raw mut pairs)[1]).first, + 7, + ); + p4_2_array_elements_from_initializer_clauses_S::constructor_1( + &raw mut (*&raw mut (*&raw mut pairs)[1]).second, + 8, + ); + }; + }; + assert!(((pairs[(0) as usize].second.value) == (6))); + assert!(((pairs[(1) as usize].first.value) == (7))); + let mut grid: [[p4_2_array_elements_from_initializer_clauses_S; 2]; 2] = + ZeroInitializationUnsafe::zero_init(); + { + { + p4_2_array_elements_from_initializer_clauses_S::constructor_1( + &raw mut (*&raw mut (*&raw mut grid)[0])[0], + 9, + ); + p4_2_array_elements_from_initializer_clauses_S::constructor_1( + &raw mut (*&raw mut (*&raw mut grid)[0])[1], + 10, + ); + }; + { + p4_2_array_elements_from_initializer_clauses_S::constructor_1( + &raw mut (*&raw mut (*&raw mut grid)[1])[0], + 11, + ); + p4_2_array_elements_from_initializer_clauses_S::constructor_1( + &raw mut (*&raw mut (*&raw mut grid)[1])[1], + 12, + ); + }; + }; + assert!(((grid[(0) as usize][(1) as usize].value) == (10))); + assert!(((grid[(1) as usize][(0) as usize].value) == (11))); + object_unsafe!( + holder, + p4_2_array_elements_from_initializer_clauses_WithArray, + |holder| { + { + p4_2_array_elements_from_initializer_clauses_S::constructor_1( + &raw mut (*&raw mut (*holder).items)[0], + 13, + ); + p4_2_array_elements_from_initializer_clauses_S::constructor_1( + &raw mut (*&raw mut (*holder).items)[1], + 14, + ); + }; + } + ); + assert!(((holder.items[(0) as usize].value) == (13))); + assert!(((holder.items[(1) as usize].value) == (14))); + assert!( + ((unsafe { + let mut _items: [p4_2_array_elements_from_initializer_clauses_S; 2] = + ZeroInitializationUnsafe::zero_init(); + { + p4_2_array_elements_from_initializer_clauses_S::constructor_1( + &raw mut (*&raw mut _items)[0], + 15, + ); + p4_2_array_elements_from_initializer_clauses_S::constructor_1( + &raw mut (*&raw mut _items)[1], + 16, + ); + }; + second_of_6(&mut _items) + }) == (16)) + ); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut copies_5)) == (0))); +} +pub static mut copies_8: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +#[repr(C)] +#[derive( + FnPtrArg, + ZeroInitializationUnsafe, + DestructibleUnsafe, + MemberwiseUnsafe, + MoveConstructibleUnsafe, + MoveAssignableUnsafe, + SwappableUnsafe, +)] +#[memberwise(copy_assign)] +#[derive(ConversionsUnsafe)] +#[conversions(copy)] +#[constructor(constructor_1, i32)] +#[constructor(constructor_2, LvalueRefUnsafe>)] +pub struct p5_2_remaining_elements_from_empty_list_S { + pub value: i32, +} +impl p5_2_remaining_elements_from_empty_list_S { + pub unsafe fn constructor_1(this: *mut Self, mut v: i32) { + (&raw mut (*this).value).write(v); + } + pub unsafe fn constructor_2( + this: *mut Self, + other: LvalueRefUnsafe>, + ) { + let other = (other.as_ptr() as *const p5_2_remaining_elements_from_empty_list_S); + (&raw mut (*this).value).write((((*other).value) + (100))); + (*std::cell::LazyCell::force_mut(&mut *&raw mut copies_8)).prefix_inc(); + } +} +impl ConstructorUnsafe<()> for p5_2_remaining_elements_from_empty_list_S { + unsafe fn constructor(this: *mut Self, _: ()) { + unsafe { + p5_2_remaining_elements_from_empty_list_S::constructor_1(this, 7); + } + } +} +impl Default for p5_2_remaining_elements_from_empty_list_S { + fn default() -> Self { + unsafe { + { + let mut __v: p5_2_remaining_elements_from_empty_list_S = + ZeroInitializationUnsafe::zero_init(); + p5_2_remaining_elements_from_empty_list_S::constructor_1(&raw mut __v, 7); + __v + } + } + } +} +#[repr(C)] +#[derive( + FnPtrArg, + ZeroInitializationUnsafe, + DestructibleUnsafe, + MemberwiseUnsafe, + SwappableUnsafe, + Default, +)] +#[memberwise(copy_constructor, move_constructor, move_assign, copy_assign)] +#[derive(ConversionsUnsafe)] +#[conversions(copy, move)] +#[constructor(constructor_1)] +pub struct p5_2_remaining_elements_from_empty_list_Pair { + pub first: p5_2_remaining_elements_from_empty_list_S, + pub second: p5_2_remaining_elements_from_empty_list_S, +} +impl p5_2_remaining_elements_from_empty_list_Pair { + pub unsafe fn constructor_1(this: *mut Self) { + p5_2_remaining_elements_from_empty_list_S::constructor_1(&raw mut (*this).first, 7); + p5_2_remaining_elements_from_empty_list_S::constructor_1(&raw mut (*this).second, 7); + } +} +pub unsafe fn test_9() { + let mut filled: [p5_2_remaining_elements_from_empty_list_S; 4] = + ZeroInitializationUnsafe::zero_init(); + { + p5_2_remaining_elements_from_empty_list_S::constructor_1(&raw mut (*&raw mut filled)[0], 1); + for __tmp_2 in 1..4 { + p5_2_remaining_elements_from_empty_list_S::constructor_1( + &raw mut (*&raw mut filled)[__tmp_2], + 7, + ); + } + }; + assert!(((filled[(0) as usize].value) == (1))); + assert!(((filled[(1) as usize].value) == (7))); + assert!(((filled[(3) as usize].value) == (7))); + let mut empty: [p5_2_remaining_elements_from_empty_list_S; 3] = + ZeroInitializationUnsafe::zero_init(); + { + for __tmp_3 in 0..3 { + p5_2_remaining_elements_from_empty_list_S::constructor_1( + &raw mut (*&raw mut empty)[__tmp_3], + 7, + ); + } + }; + assert!(((empty[(0) as usize].value) == (7))); + assert!(((empty[(2) as usize].value) == (7))); + object_unsafe!( + partial, + p5_2_remaining_elements_from_empty_list_Pair, + |partial| { + p5_2_remaining_elements_from_empty_list_S::constructor_1(&raw mut (*partial).first, 2); + p5_2_remaining_elements_from_empty_list_S::constructor_1(&raw mut (*partial).second, 7); + } + ); + assert!(((partial.first.value) == (2))); + assert!(((partial.second.value) == (7))); + let mut pairs: [p5_2_remaining_elements_from_empty_list_Pair; 3] = + ZeroInitializationUnsafe::zero_init(); + { + { + p5_2_remaining_elements_from_empty_list_S::constructor_1( + &raw mut (*&raw mut (*&raw mut pairs)[0]).first, + 3, + ); + p5_2_remaining_elements_from_empty_list_S::constructor_1( + &raw mut (*&raw mut (*&raw mut pairs)[0]).second, + 4, + ); + }; + for __tmp_4 in 1..3 { + { + p5_2_remaining_elements_from_empty_list_S::constructor_1( + &raw mut (*&raw mut (*&raw mut pairs)[__tmp_4]).first, + 7, + ); + p5_2_remaining_elements_from_empty_list_S::constructor_1( + &raw mut (*&raw mut (*&raw mut pairs)[__tmp_4]).second, + 7, + ); + }; + } + }; + assert!(((pairs[(0) as usize].second.value) == (4))); + assert!(((pairs[(2) as usize].first.value) == (7))); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut copies_8)) == (0))); +} +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + libcc2rs::cpp2rust_exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + (unsafe { test_4() }); + (unsafe { test_7() }); + (unsafe { test_9() }); + return 0; +} +pub unsafe fn __cpp2rust_init_globals() { + std::cell::LazyCell::force(&*&raw const copies_0); + std::cell::LazyCell::force(&*&raw const copies_5); + std::cell::LazyCell::force(&*&raw const copies_8); +} diff --git a/tests/std/dcl.dcl/dcl.decl/dcl.init/out/unsafe/dcl.init.general.rs b/tests/std/dcl.dcl/dcl.decl/dcl.init/out/unsafe/dcl.init.general.rs index 406fec871f..57f9ddb774 100644 --- a/tests/std/dcl.dcl/dcl.decl/dcl.init/out/unsafe/dcl.init.general.rs +++ b/tests/std/dcl.dcl/dcl.decl/dcl.init/out/unsafe/dcl.init.general.rs @@ -336,6 +336,118 @@ pub unsafe fn test_21() { } }; } +pub static mut constructed_22: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +#[repr(C)] +#[derive( + FnPtrArg, + ZeroInitializationUnsafe, + DestructibleUnsafe, + MemberwiseUnsafe, + MoveConstructibleUnsafe, + MoveAssignableUnsafe, + SwappableUnsafe, +)] +#[memberwise(copy_assign)] +#[derive(ConversionsUnsafe)] +#[conversions(copy)] +#[constructor(constructor_1)] +#[constructor(constructor_2, LvalueRefUnsafe>)] +pub struct p7_2_default_initialized_array_S { + pub value: i32, +} +impl p7_2_default_initialized_array_S { + pub unsafe fn constructor_1(this: *mut Self) { + (&raw mut (*this).value).write(7); + (*std::cell::LazyCell::force_mut(&mut *&raw mut constructed_22)).prefix_inc(); + } + pub unsafe fn constructor_2( + this: *mut Self, + other: LvalueRefUnsafe>, + ) { + let other = (other.as_ptr() as *const p7_2_default_initialized_array_S); + (&raw mut (*this).value).write((((*other).value) + (100))); + (*std::cell::LazyCell::force_mut(&mut *&raw mut constructed_22)).prefix_inc(); + } +} +impl Default for p7_2_default_initialized_array_S { + fn default() -> Self { + unsafe { + { + let mut __v: p7_2_default_initialized_array_S = + ZeroInitializationUnsafe::zero_init(); + p7_2_default_initialized_array_S::constructor_1(&raw mut __v); + __v + } + } + } +} +#[repr(C)] +#[derive( + FnPtrArg, + ZeroInitializationUnsafe, + DestructibleUnsafe, + MemberwiseUnsafe, + SwappableUnsafe, + Default, +)] +#[memberwise(copy_constructor, move_constructor, move_assign, copy_assign)] +#[derive(ConversionsUnsafe)] +#[conversions(copy, move)] +#[constructor(constructor_1)] +pub struct p7_2_default_initialized_array_WithArray { + pub items: [p7_2_default_initialized_array_S; 2], +} +impl p7_2_default_initialized_array_WithArray { + pub unsafe fn constructor_1(this: *mut Self) { + for __tmp_0 in 0..2 { + p7_2_default_initialized_array_S::constructor_1( + &raw mut (*&raw mut (*this).items)[__tmp_0], + ); + } + } +} +pub unsafe fn test_23() { + let mut items: [p7_2_default_initialized_array_S; 3] = ZeroInitializationUnsafe::zero_init(); + for __tmp_1 in 0..3 { + p7_2_default_initialized_array_S::constructor_1(&raw mut (*&raw mut items)[__tmp_1]); + } + assert!(((items[(0) as usize].value) == (7))); + assert!(((items[(2) as usize].value) == (7))); + let mut grid: [[p7_2_default_initialized_array_S; 3]; 2] = + ZeroInitializationUnsafe::zero_init(); + for __tmp_2 in 0..2 { + for __tmp_3 in 0..3 { + p7_2_default_initialized_array_S::constructor_1( + &raw mut (*&raw mut (*&raw mut grid)[__tmp_2])[__tmp_3], + ); + } + } + assert!(((grid[(0) as usize][(0) as usize].value) == (7))); + assert!(((grid[(1) as usize][(2) as usize].value) == (7))); + object_unsafe!( + holder, + p7_2_default_initialized_array_WithArray::constructor_1() + ); + assert!(((holder.items[(1) as usize].value) == (7))); + let mut allocated: *mut p7_2_default_initialized_array_S = Box::leak( + (0..2_usize) + .map(|_| ::default()) + .collect::>(), + ) + .as_mut_ptr(); + assert!((((*allocated.offset((1) as isize)).value) == (7))); + { + let __p = allocated; + if !__p.is_null() { + ::std::mem::drop(Box::from_raw(::std::slice::from_raw_parts_mut( + __p, + libcc2rs::malloc_usable_size(__p as *mut ::libc::c_void) + / ::std::mem::size_of::(), + ))) + } + }; + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut constructed_22)) == (13))); +} #[repr(C)] #[derive( Copy, @@ -425,7 +537,7 @@ impl p9_1_2_zero_then_default_initialized_HoldsLeavesMemberAlone { ); } } -pub unsafe fn test_22() { +pub unsafe fn test_24() { object_unsafe!( value, p9_1_2_zero_then_default_initialized_WithDefaultMember::constructor_1() @@ -481,7 +593,8 @@ unsafe fn main_0() -> i32 { (unsafe { test_15() }); (unsafe { test_17() }); (unsafe { test_21() }); - (unsafe { test_22() }); + (unsafe { test_23() }); + (unsafe { test_24() }); return 0; } pub unsafe fn __cpp2rust_init_globals() { @@ -499,4 +612,5 @@ pub unsafe fn __cpp2rust_init_globals() { std::cell::LazyCell::force(&*&raw const static_numbers_18); std::cell::LazyCell::force(&*&raw const static_matrix_19); std::cell::LazyCell::force(&*&raw const static_pairs_20); + std::cell::LazyCell::force(&*&raw const constructed_22); } diff --git a/tests/std/expr/expr.compound/expr.cond.cpp b/tests/std/expr/expr.compound/expr.cond.cpp index 3533a2cbfa..8fe89d47a0 100644 --- a/tests/std/expr/expr.compound/expr.cond.cpp +++ b/tests/std/expr/expr.compound/expr.cond.cpp @@ -21,7 +21,67 @@ void test() { } } // namespace p7_4_pointer_to_member_and_null +// N4950 [expr.cond]/7.1: if the second and third operands have the same type, +// the result is of that type and the result object is initialized using the +// selected operand. +namespace p7_1_class_prvalue_operands { +int constructed = 0; + +struct S { + int value; + S(int v) : value(v) { ++constructed; } + S(const S &other) : value(other.value + 100) { ++constructed; } +}; + +S make(int v) { return S(v); } + +S pick(bool first) { return first ? S(1) : make(2); } + +int read(const S &s) { return s.value; } + +struct Holder { + S held; + Holder(bool first) : held(first ? make(3) : S(4)) {} +}; + +void test() { + bool yes = true; + bool no = false; + + S from_constructors = yes ? S(5) : S(6); + assert(from_constructors.value == 5); + + S from_call = no ? S(7) : make(8); + assert(from_call.value == 8); + + S nested = no ? S(9) : (yes ? make(10) : S(11)); + assert(nested.value == 10); + + S parenthesized = (yes ? S(12) : make(13)); + assert(parenthesized.value == 12); + + S converted = no ? S(14) : 15; + assert(converted.value == 15); + + assert(pick(true).value == 1); + assert(pick(false).value == 2); + + assert(Holder(true).held.value == 3); + assert(Holder(false).held.value == 4); + + assert(read(no ? S(16) : make(17)) == 17); + assert((yes ? make(18) : S(19)).value == 18); + + S *allocated = new S(no ? S(20) : S(21)); + assert(allocated->value == 21); + delete allocated; + + assert(constructed == 12); +} +} // namespace p7_1_class_prvalue_operands + int main() { p7_4_pointer_to_member_and_null::test(); + p7_1_class_prvalue_operands::test(); return 0; } diff --git a/tests/std/expr/expr.compound/out/refcount/expr.cond.rs b/tests/std/expr/expr.compound/out/refcount/expr.cond.rs index 1b589f0934..b3a176da46 100644 --- a/tests/std/expr/expr.compound/out/refcount/expr.cond.rs +++ b/tests/std/expr/expr.compound/out/refcount/expr.cond.rs @@ -52,12 +52,237 @@ pub fn test_1() { == 4) ); } +thread_local!( + pub static constructed_2: Value = Rc::new(RefCell::new(0)); +); +#[derive( + Record, + ByteRepr, + ZeroInitialization, + FnPtrArg, + DefaultInitializable, + Memberwise, + MoveConstructible, + MoveAssignable, + Swappable, + Default, +)] +#[memberwise(copy_assign)] +#[derive(Conversions)] +#[conversions(copy)] +#[constructor(constructor_1, i32)] +#[constructor(constructor_2, LvalueRef>)] +#[byte_size(4)] +pub struct p7_1_class_prvalue_operands_S { + #[offset(0)] + pub value: i32, +} +impl p7_1_class_prvalue_operands_S { + pub fn constructor_1(this: Ptr, mut v: i32) { + field!(this, value).write(v); + (*constructed_2.with(Value::clone).borrow_mut()).prefix_inc(); + } + pub fn constructor_2(this: Ptr, other: LvalueRef>) { + let other = other.as_pointer(); + field!(this, value).write((other.with(|__s| __s.value) + 100)); + (*constructed_2.with(Value::clone).borrow_mut()).prefix_inc(); + } +} +pub fn make_3(__return: Ptr, mut v: i32) { + p7_1_class_prvalue_operands_S::constructor_1(__return.clone(), v); + return; +} +pub fn pick_4(__return: Ptr, mut first: bool) { + if first { + p7_1_class_prvalue_operands_S::constructor_1(__return.clone(), 1); + } else { + ({ make_3(Ptr::clone(&__return.clone()), 2) }); + }; + return; +} +pub fn read_5(s: Ptr) -> i32 { + return s.with(|__s| __s.value); +} +#[derive( + Record, + ByteRepr, + ZeroInitialization, + FnPtrArg, + DefaultInitializable, + Memberwise, + Swappable, + Default, +)] +#[memberwise(copy_constructor, move_constructor, move_assign, copy_assign)] +#[derive(Conversions)] +#[conversions(copy, move)] +#[constructor(constructor_1, bool)] +#[byte_size(4)] +pub struct p7_1_class_prvalue_operands_Holder { + #[offset(0)] + #[byte_size(4)] + pub held: p7_1_class_prvalue_operands_S, +} +impl p7_1_class_prvalue_operands_Holder { + pub fn constructor_1(this: Ptr, mut first: bool) { + if first { + ({ make_3(Ptr::clone(&field_ptr!(this, held)), 3) }); + } else { + p7_1_class_prvalue_operands_S::constructor_1(field_ptr!(this, held), 4); + }; + } +} +pub fn test_6() { + let mut yes: bool = true; + let mut no: bool = false; + object!( + from_constructors, + p7_1_class_prvalue_operands_S, + |from_constructors| if yes { + p7_1_class_prvalue_operands_S::constructor_1(from_constructors.clone(), 5); + } else { + p7_1_class_prvalue_operands_S::constructor_1(from_constructors.clone(), 6); + } + ); + assert!(({ (*from_constructors.borrow()).value } == 5)); + object!( + from_call, + p7_1_class_prvalue_operands_S, + |from_call| if no { + p7_1_class_prvalue_operands_S::constructor_1(from_call.clone(), 7); + } else { + ({ make_3(Ptr::clone(&from_call.clone()), 8) }); + } + ); + assert!(({ (*from_call.borrow()).value } == 8)); + object!(nested, p7_1_class_prvalue_operands_S, |nested| if no { + p7_1_class_prvalue_operands_S::constructor_1(nested.clone(), 9); + } else { + if yes { + ({ make_3(Ptr::clone(&nested.clone()), 10) }); + } else { + p7_1_class_prvalue_operands_S::constructor_1(nested.clone(), 11); + }; + }); + assert!(({ (*nested.borrow()).value } == 10)); + object!( + parenthesized, + p7_1_class_prvalue_operands_S, + |parenthesized| if yes { + p7_1_class_prvalue_operands_S::constructor_1(parenthesized.clone(), 12); + } else { + ({ make_3(Ptr::clone(&parenthesized.clone()), 13) }); + } + ); + assert!(({ (*parenthesized.borrow()).value } == 12)); + object!( + converted, + p7_1_class_prvalue_operands_S, + |converted| if no { + p7_1_class_prvalue_operands_S::constructor_1(converted.clone(), 14); + } else { + p7_1_class_prvalue_operands_S::constructor_1(converted.clone(), 15); + } + ); + assert!(({ (*converted.borrow()).value } == 15)); + { + let __tmp_0: Value = + Rc::new(RefCell::new(ZeroInitialization::zero_init())); + assert!( + ({ + ({ pick_4(Ptr::clone(&__tmp_0.as_pointer()), true) }); + __tmp_0.as_pointer() + } + .with(|__s| __s.value) + == 1) + ) + }; + { + let __tmp_1: Value = + Rc::new(RefCell::new(ZeroInitialization::zero_init())); + assert!( + ({ + ({ pick_4(Ptr::clone(&__tmp_1.as_pointer()), false) }); + __tmp_1.as_pointer() + } + .with(|__s| __s.value) + == 2) + ) + }; + { + let __tmp_2: Value = + Rc::new(RefCell::new(ZeroInitialization::zero_init())); + assert!( + ({ + p7_1_class_prvalue_operands_Holder::constructor_1(__tmp_2.as_pointer(), true); + __tmp_2.as_pointer() + } + .with(|__s| __s.held.value) + == 3) + ) + }; + { + let __tmp_3: Value = + Rc::new(RefCell::new(ZeroInitialization::zero_init())); + assert!( + ({ + p7_1_class_prvalue_operands_Holder::constructor_1(__tmp_3.as_pointer(), false); + __tmp_3.as_pointer() + } + .with(|__s| __s.held.value) + == 4) + ) + }; + assert!( + (({ + let _s: Value = + Rc::new(RefCell::new(ZeroInitialization::zero_init())); + if no { + p7_1_class_prvalue_operands_S::constructor_1(_s.as_pointer(), 16); + } else { + ({ make_3(Ptr::clone(&_s.as_pointer()), 17) }); + }; + read_5(_s.as_pointer()) + }) == 17) + ); + { + let __tmp_4: Value = + Rc::new(RefCell::new(ZeroInitialization::zero_init())); + assert!( + ({ + if yes { + ({ make_3(Ptr::clone(&__tmp_4.as_pointer()), 18) }); + } else { + p7_1_class_prvalue_operands_S::constructor_1(__tmp_4.as_pointer(), 19); + }; + __tmp_4.as_pointer() + } + .with(|__s| __s.value) + == 18) + ) + }; + let mut allocated: Ptr = { + let __p = Ptr::alloc(::zero_init()); + if no { + p7_1_class_prvalue_operands_S::constructor_1(__p.clone(), 20); + } else { + p7_1_class_prvalue_operands_S::constructor_1(__p.clone(), 21); + }; + __p + }; + assert!((allocated.with(|__s| __s.value) == 21)); + allocated.delete(); + assert!((constructed_2.with(|rc| *rc.borrow()) == 12)); +} pub fn main() { __cpp2rust_init_globals(); libcc2rs::cpp2rust_exit(main_0()); } fn main_0() -> i32 { ({ test_1() }); + ({ test_6() }); return 0; } -pub fn __cpp2rust_init_globals() {} +pub fn __cpp2rust_init_globals() { + let _ = constructed_2.with(|_| ()); +} diff --git a/tests/std/expr/expr.compound/out/unsafe/expr.cond.rs b/tests/std/expr/expr.compound/out/unsafe/expr.cond.rs index eb160580db..c0f49f7fa3 100644 --- a/tests/std/expr/expr.compound/out/unsafe/expr.cond.rs +++ b/tests/std/expr/expr.compound/out/unsafe/expr.cond.rs @@ -49,6 +49,224 @@ pub unsafe fn test_1() { == (4)) ); } +pub static mut constructed_2: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +#[repr(C)] +#[derive( + FnPtrArg, + ZeroInitializationUnsafe, + DefaultInitializableUnsafe, + MemberwiseUnsafe, + MoveConstructibleUnsafe, + MoveAssignableUnsafe, + SwappableUnsafe, + Default, +)] +#[memberwise(copy_assign)] +#[derive(ConversionsUnsafe)] +#[conversions(copy)] +#[constructor(constructor_1, i32)] +#[constructor(constructor_2, LvalueRefUnsafe>)] +pub struct p7_1_class_prvalue_operands_S { + pub value: i32, +} +impl p7_1_class_prvalue_operands_S { + pub unsafe fn constructor_1(this: *mut Self, mut v: i32) { + (&raw mut (*this).value).write(v); + (*std::cell::LazyCell::force_mut(&mut *&raw mut constructed_2)).prefix_inc(); + } + pub unsafe fn constructor_2( + this: *mut Self, + other: LvalueRefUnsafe>, + ) { + let other = (other.as_ptr() as *const p7_1_class_prvalue_operands_S); + (&raw mut (*this).value).write((((*other).value) + (100))); + (*std::cell::LazyCell::force_mut(&mut *&raw mut constructed_2)).prefix_inc(); + } +} +pub unsafe fn make_3(__return: *mut p7_1_class_prvalue_operands_S, mut v: i32) { + p7_1_class_prvalue_operands_S::constructor_1(__return, v); + return; +} +pub unsafe fn pick_4(__return: *mut p7_1_class_prvalue_operands_S, mut first: bool) { + if first { + p7_1_class_prvalue_operands_S::constructor_1(__return, 1); + } else { + (unsafe { make_3(__return, 2) }); + }; + return; +} +pub unsafe fn read_5(s: *const p7_1_class_prvalue_operands_S) -> i32 { + return (*s).value; +} +#[repr(C)] +#[derive( + FnPtrArg, + ZeroInitializationUnsafe, + DefaultInitializableUnsafe, + MemberwiseUnsafe, + SwappableUnsafe, + Default, +)] +#[memberwise(copy_constructor, move_constructor, move_assign, copy_assign)] +#[derive(ConversionsUnsafe)] +#[conversions(copy, move)] +#[constructor(constructor_1, bool)] +pub struct p7_1_class_prvalue_operands_Holder { + pub held: p7_1_class_prvalue_operands_S, +} +impl p7_1_class_prvalue_operands_Holder { + pub unsafe fn constructor_1(this: *mut Self, mut first: bool) { + if first { + (unsafe { make_3(&raw mut (*this).held, 3) }); + } else { + p7_1_class_prvalue_operands_S::constructor_1(&raw mut (*this).held, 4); + }; + } +} +pub unsafe fn test_6() { + let mut yes: bool = true; + let mut no: bool = false; + object_unsafe!( + from_constructors, + p7_1_class_prvalue_operands_S, + |from_constructors| if yes { + p7_1_class_prvalue_operands_S::constructor_1(from_constructors, 5); + } else { + p7_1_class_prvalue_operands_S::constructor_1(from_constructors, 6); + } + ); + assert!(((from_constructors.value) == (5))); + object_unsafe!( + from_call, + p7_1_class_prvalue_operands_S, + |from_call| if no { + p7_1_class_prvalue_operands_S::constructor_1(from_call, 7); + } else { + (unsafe { make_3(from_call, 8) }); + } + ); + assert!(((from_call.value) == (8))); + object_unsafe!(nested, p7_1_class_prvalue_operands_S, |nested| if no { + p7_1_class_prvalue_operands_S::constructor_1(nested, 9); + } else { + if yes { + (unsafe { make_3(nested, 10) }); + } else { + p7_1_class_prvalue_operands_S::constructor_1(nested, 11); + }; + }); + assert!(((nested.value) == (10))); + object_unsafe!( + parenthesized, + p7_1_class_prvalue_operands_S, + |parenthesized| if yes { + p7_1_class_prvalue_operands_S::constructor_1(parenthesized, 12); + } else { + (unsafe { make_3(parenthesized, 13) }); + } + ); + assert!(((parenthesized.value) == (12))); + object_unsafe!( + converted, + p7_1_class_prvalue_operands_S, + |converted| if no { + p7_1_class_prvalue_operands_S::constructor_1(converted, 14); + } else { + p7_1_class_prvalue_operands_S::constructor_1(converted, 15); + } + ); + assert!(((converted.value) == (15))); + { + let mut __tmp_0: p7_1_class_prvalue_operands_S = ZeroInitializationUnsafe::zero_init(); + assert!( + (((*{ + (unsafe { pick_4(&raw mut __tmp_0, true) }); + &mut __tmp_0 + }) + .value) + == (1)) + ) + }; + { + let mut __tmp_1: p7_1_class_prvalue_operands_S = ZeroInitializationUnsafe::zero_init(); + assert!( + (((*{ + (unsafe { pick_4(&raw mut __tmp_1, false) }); + &mut __tmp_1 + }) + .value) + == (2)) + ) + }; + { + let mut __tmp_2: p7_1_class_prvalue_operands_Holder = ZeroInitializationUnsafe::zero_init(); + assert!( + (((*{ + p7_1_class_prvalue_operands_Holder::constructor_1(&raw mut __tmp_2, true); + &mut __tmp_2 + }) + .held + .value) + == (3)) + ) + }; + { + let mut __tmp_3: p7_1_class_prvalue_operands_Holder = ZeroInitializationUnsafe::zero_init(); + assert!( + (((*{ + p7_1_class_prvalue_operands_Holder::constructor_1(&raw mut __tmp_3, false); + &mut __tmp_3 + }) + .held + .value) + == (4)) + ) + }; + assert!( + ((unsafe { + let mut _s: p7_1_class_prvalue_operands_S = ZeroInitializationUnsafe::zero_init(); + if no { + p7_1_class_prvalue_operands_S::constructor_1(&raw mut _s, 16); + } else { + (unsafe { make_3(&raw mut _s, 17) }); + }; + read_5(&mut _s) + }) == (17)) + ); + { + let mut __tmp_4: p7_1_class_prvalue_operands_S = ZeroInitializationUnsafe::zero_init(); + assert!( + (((*{ + if yes { + (unsafe { make_3(&raw mut __tmp_4, 18) }); + } else { + p7_1_class_prvalue_operands_S::constructor_1(&raw mut __tmp_4, 19); + }; + &mut __tmp_4 + }) + .value) + == (18)) + ) + }; + let mut allocated: *mut p7_1_class_prvalue_operands_S = { + let __p: *mut p7_1_class_prvalue_operands_S = + Box::leak(Box::new(::zero_init())); + if no { + p7_1_class_prvalue_operands_S::constructor_1(__p, 20); + } else { + p7_1_class_prvalue_operands_S::constructor_1(__p, 21); + }; + __p + }; + assert!((((*allocated).value) == (21))); + { + let __p = allocated; + if !__p.is_null() { + ::std::mem::drop(Box::from_raw(__p)) + } + }; + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut constructed_2)) == (12))); +} pub fn main() { unsafe { __cpp2rust_init_globals(); @@ -57,6 +275,9 @@ pub fn main() { } unsafe fn main_0() -> i32 { (unsafe { test_1() }); + (unsafe { test_6() }); return 0; } -pub unsafe fn __cpp2rust_init_globals() {} +pub unsafe fn __cpp2rust_init_globals() { + std::cell::LazyCell::force(&*&raw const constructed_2); +} diff --git a/tests/unit/comma_operator.cpp b/tests/unit/comma_operator.cpp index 874e7019f9..a9356c2740 100644 --- a/tests/unit/comma_operator.cpp +++ b/tests/unit/comma_operator.cpp @@ -6,6 +6,12 @@ struct Pair { int second; }; +struct Counted { + int value; + Counted(int v) : value(v) {} + Counted(const Counted &other) : value(other.value + 100) {} +}; + long twice(const long &v) { return v * 2; } int sum(const Pair &p) { return p.first + p.second; } @@ -47,5 +53,9 @@ int main() { assert(sum((a = 7, p1)) == 3); assert(a == 7); + Counted c = (a = 8, Counted(4)); + assert(a == 8); + assert(c.value == 4); + return 0; } diff --git a/tests/unit/out/refcount/array_of_noncopy_struct.rs b/tests/unit/out/refcount/array_of_noncopy_struct.rs index 4eae87808a..52a8476cff 100644 --- a/tests/unit/out/refcount/array_of_noncopy_struct.rs +++ b/tests/unit/out/refcount/array_of_noncopy_struct.rs @@ -51,19 +51,18 @@ pub fn main() { libcc2rs::cpp2rust_exit(main_0()); } fn main_0() -> i32 { - let arr: Value> = Rc::new(RefCell::new(Box::new( - std::array::from_fn::<_, 3, _>(|_| { - let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); - NonCopy::constructor_1(__v.as_pointer()); - Rc::try_unwrap(__v).ok().unwrap().into_inner() - }), - ))); + let arr: Value> = Rc::new(RefCell::new( + (0..3).map(|_| ZeroInitialization::zero_init()).collect(), + )); + for __tmp_0 in 0..3 { + NonCopy::constructor_1(arr.as_pointer().decay().offset(__tmp_0)); + } (*arr.borrow_mut())[(0) as usize].tag = 7; ({ - let __tmp_0: Value = Rc::new(RefCell::new(42)); + let __tmp_1: Value = Rc::new(RefCell::new(42)); MoveConstructible::push_moved( &field_ref!(arr.decay().offset(1), data).as_pointer(), - __tmp_0.as_pointer(), + __tmp_1.as_pointer(), ) }); assert!(({ (*arr.borrow())[(0) as usize].tag } == 7)); diff --git a/tests/unit/out/refcount/comma_operator.rs b/tests/unit/out/refcount/comma_operator.rs index c4bf0369ef..1015ee62ef 100644 --- a/tests/unit/out/refcount/comma_operator.rs +++ b/tests/unit/out/refcount/comma_operator.rs @@ -27,6 +27,37 @@ pub struct Pair { #[offset(4)] pub second: i32, } +#[derive( + Record, + ByteRepr, + ZeroInitialization, + FnPtrArg, + DefaultInitializable, + Memberwise, + MoveConstructible, + MoveAssignable, + Swappable, + Default, +)] +#[memberwise(copy_assign)] +#[derive(Conversions)] +#[conversions(copy)] +#[constructor(constructor_1, i32)] +#[constructor(constructor_2, LvalueRef>)] +#[byte_size(4)] +pub struct Counted { + #[offset(0)] + pub value: i32, +} +impl Counted { + pub fn constructor_1(this: Ptr, mut v: i32) { + field!(this, value).write(v); + } + pub fn constructor_2(this: Ptr, other: LvalueRef>) { + let other = other.as_pointer(); + field!(this, value).write((other.with(|__s| __s.value) + 100)); + } +} pub fn twice_0(v: Ptr) -> i64 { return ((v.read()) * 2_i64); } @@ -113,6 +144,12 @@ fn main_0() -> i32 { }) == 3) ); assert!((a == 7)); + object!(c, Counted, |c| { + a = 8; + Counted::constructor_1(c.clone(), 4) + }); + assert!((a == 8)); + assert!(({ (*c.borrow()).value } == 4)); return 0; } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/copy_assign.rs b/tests/unit/out/refcount/copy_assign.rs index 4f00ada430..501cc581c7 100644 --- a/tests/unit/out/refcount/copy_assign.rs +++ b/tests/unit/out/refcount/copy_assign.rs @@ -464,25 +464,13 @@ fn main_0() -> i32 { assign!(*pa = b); assert!(({ (*a.borrow()).v } == 2)); assert!((assigns_0.with(|rc| *rc.borrow()) == 6)); - let h: Value = Rc::new(RefCell::new(Holder { - p: { - let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); - Partial::constructor_1(__v.as_pointer(), 4, 40); - Rc::try_unwrap(__v).ok().unwrap().into_inner() - }, - arr: Box::new([ - { - let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); - Partial::constructor_1(__v.as_pointer(), 5, 50); - Rc::try_unwrap(__v).ok().unwrap().into_inner() - }, - { - let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); - Partial::constructor_1(__v.as_pointer(), 6, 60); - Rc::try_unwrap(__v).ok().unwrap().into_inner() - }, - ]), - })); + object!(h, Holder, |h| { + Partial::constructor_1(field_ptr!(h.clone(), p), 4, 40); + { + Partial::constructor_1(field_ptr!(h.clone(), arr).decay().offset(0), 5, 50); + Partial::constructor_1(field_ptr!(h.clone(), arr).decay().offset(1), 6, 60); + }; + }); assign!(*field_ptr!(h.as_pointer(), p) = b); assign!(*array_field_ptr!(h.as_pointer(), arr).offset((1) as isize) = c); assert!(({ (*h.borrow()).p.v } == 2) && ({ (*h.borrow()).p.keep } == 40)); @@ -503,25 +491,13 @@ fn main_0() -> i32 { object!(r1, RefQualified::constructor_1()); assign!(r1 = r); assert!(({ (*r1.borrow()).mark } == 1)); - let h2: Value = Rc::new(RefCell::new(Holder { - p: { - let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); - Partial::constructor_1(__v.as_pointer(), 7, 70); - Rc::try_unwrap(__v).ok().unwrap().into_inner() - }, - arr: Box::new([ - { - let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); - Partial::constructor_1(__v.as_pointer(), 8, 80); - Rc::try_unwrap(__v).ok().unwrap().into_inner() - }, - { - let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); - Partial::constructor_1(__v.as_pointer(), 9, 90); - Rc::try_unwrap(__v).ok().unwrap().into_inner() - }, - ]), - })); + object!(h2, Holder, |h2| { + Partial::constructor_1(field_ptr!(h2.clone(), p), 7, 70); + { + Partial::constructor_1(field_ptr!(h2.clone(), arr).decay().offset(0), 8, 80); + Partial::constructor_1(field_ptr!(h2.clone(), arr).decay().offset(1), 9, 90); + }; + }); assign!(h2 = h); assert!(({ (*h2.borrow()).p.v } == 2) && ({ (*h2.borrow()).p.keep } == 70)); assert!( @@ -533,48 +509,20 @@ fn main_0() -> i32 { object!(cc2, CopyCounted::constructor_1(2)); assign!(cc1 = cc2); assert!(({ (*cc1.borrow()).v } == 2)); - let hc1: Value = Rc::new(RefCell::new(HasCopyCounted { - c: { - let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); - CopyCounted::constructor_1(__v.as_pointer(), 1); - Rc::try_unwrap(__v).ok().unwrap().into_inner() - }, - arr: Box::new([ - { - let __v: Value = - Rc::new(RefCell::new(ZeroInitialization::zero_init())); - CopyCounted::constructor_1(__v.as_pointer(), 2); - Rc::try_unwrap(__v).ok().unwrap().into_inner() - }, - { - let __v: Value = - Rc::new(RefCell::new(ZeroInitialization::zero_init())); - CopyCounted::constructor_1(__v.as_pointer(), 3); - Rc::try_unwrap(__v).ok().unwrap().into_inner() - }, - ]), - })); - let hc2: Value = Rc::new(RefCell::new(HasCopyCounted { - c: { - let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); - CopyCounted::constructor_1(__v.as_pointer(), 4); - Rc::try_unwrap(__v).ok().unwrap().into_inner() - }, - arr: Box::new([ - { - let __v: Value = - Rc::new(RefCell::new(ZeroInitialization::zero_init())); - CopyCounted::constructor_1(__v.as_pointer(), 5); - Rc::try_unwrap(__v).ok().unwrap().into_inner() - }, - { - let __v: Value = - Rc::new(RefCell::new(ZeroInitialization::zero_init())); - CopyCounted::constructor_1(__v.as_pointer(), 6); - Rc::try_unwrap(__v).ok().unwrap().into_inner() - }, - ]), - })); + object!(hc1, HasCopyCounted, |hc1| { + CopyCounted::constructor_1(field_ptr!(hc1.clone(), c), 1); + { + CopyCounted::constructor_1(field_ptr!(hc1.clone(), arr).decay().offset(0), 2); + CopyCounted::constructor_1(field_ptr!(hc1.clone(), arr).decay().offset(1), 3); + }; + }); + object!(hc2, HasCopyCounted, |hc2| { + CopyCounted::constructor_1(field_ptr!(hc2.clone(), c), 4); + { + CopyCounted::constructor_1(field_ptr!(hc2.clone(), arr).decay().offset(0), 5); + CopyCounted::constructor_1(field_ptr!(hc2.clone(), arr).decay().offset(1), 6); + }; + }); assign!(hc1 = hc2); assert!( (({ (*hc1.borrow()).c.v } == 4) @@ -607,20 +555,12 @@ fn main_0() -> i32 { Swapped::copy_assign(sw1.as_pointer(), ParameterObject::bind(_o.as_pointer())) }); assert!(({ (*sw1.borrow()).v } == 2) && (swap_copies_2.with(|rc| *rc.borrow()) == 1)); - let hs1: Value = Rc::new(RefCell::new(HoldsSwapped { - s: { - let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); - Swapped::constructor_1(__v.as_pointer(), 3); - Rc::try_unwrap(__v).ok().unwrap().into_inner() - }, - })); - let hs2: Value = Rc::new(RefCell::new(HoldsSwapped { - s: { - let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); - Swapped::constructor_1(__v.as_pointer(), 4); - Rc::try_unwrap(__v).ok().unwrap().into_inner() - }, - })); + object!(hs1, HoldsSwapped, |hs1| { + Swapped::constructor_1(field_ptr!(hs1.clone(), s), 3); + }); + object!(hs2, HoldsSwapped, |hs2| { + Swapped::constructor_1(field_ptr!(hs2.clone(), s), 4); + }); assign!(hs1 = hs2); assert!(({ (*hs1.borrow()).s.v } == 4) && (swap_copies_2.with(|rc| *rc.borrow()) == 2)); object!(hv1, HoldsVoidAssign::constructor_1()); diff --git a/tests/unit/out/refcount/copy_ctor.rs b/tests/unit/out/refcount/copy_ctor.rs index 36d2db9828..adaad671b1 100644 --- a/tests/unit/out/refcount/copy_ctor.rs +++ b/tests/unit/out/refcount/copy_ctor.rs @@ -384,25 +384,13 @@ fn main_0() -> i32 { object!(h, Counted::constructor_2(LvalueRef::bind(g.as_pointer()))); assert!(({ (*h.borrow()).v } == 7)); assert!((copies_0.with(|rc| *rc.borrow()) == 6)); - let hold: Value = Rc::new(RefCell::new(Holder { - c: { - let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); - Counted::constructor_1(__v.as_pointer(), 8); - Rc::try_unwrap(__v).ok().unwrap().into_inner() - }, - arr: Box::new([ - { - let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); - Counted::constructor_1(__v.as_pointer(), 9); - Rc::try_unwrap(__v).ok().unwrap().into_inner() - }, - { - let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); - Counted::constructor_1(__v.as_pointer(), 10); - Rc::try_unwrap(__v).ok().unwrap().into_inner() - }, - ]), - })); + object!(hold, Holder, |hold| { + Counted::constructor_1(field_ptr!(hold.clone(), c), 8); + { + Counted::constructor_1(field_ptr!(hold.clone(), arr).decay().offset(0), 9); + Counted::constructor_1(field_ptr!(hold.clone(), arr).decay().offset(1), 10); + }; + }); object!(hold2, Holder, |hold2| { ::copy_constructor( hold2.clone(), @@ -423,13 +411,9 @@ fn main_0() -> i32 { object!(i2, Ignored::constructor_2(LvalueRef::bind(i1.as_pointer()))); assert!(({ (*i1.borrow()).v } == 1) && ({ (*i2.borrow()).v } == -1_i32)); assert!((copies_0.with(|rc| *rc.borrow()) == 11)); - let o: Value = Rc::new(RefCell::new(Owner { - box_: { - let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); - Box_int_::constructor_1(__v.as_pointer(), 3); - Rc::try_unwrap(__v).ok().unwrap().into_inner() - }, - })); + object!(o, Owner, |o| { + Box_int_::constructor_1(field_ptr!(o.clone(), box_), 3); + }); object!(o2, Owner, |o2| { ::move_constructor(o2.clone(), RvalueRef::bind(o.as_pointer())) }); diff --git a/tests/unit/out/refcount/copy_move_defaulted.rs b/tests/unit/out/refcount/copy_move_defaulted.rs index 99e7082ee2..d3aec9ca05 100644 --- a/tests/unit/out/refcount/copy_move_defaulted.rs +++ b/tests/unit/out/refcount/copy_move_defaulted.rs @@ -714,14 +714,15 @@ fn main_0() -> i32 { assert!(((l.v == 5) && (l.inner.x == 50)) && (l.arr[(0) as usize] == 5)); let vec_: Value> = Rc::new(RefCell::new(Vec::new())); CopyConstructible::push_copied(&vec_.as_pointer(), b.as_pointer()); - match (Rc::new(RefCell::new({ - let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); - Explicit::constructor_1(__v.as_pointer(), 9); - Rc::try_unwrap(__v).ok().unwrap().into_inner() - })) - .as_pointer(),) { - (__tmp_1,) => MoveConstructible::push_moved(&vec_.as_pointer(), (__tmp_1).clone()), + let __tmp_2: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); + match ({ + Explicit::constructor_1(__tmp_2.as_pointer(), 9); + __tmp_2.as_pointer() + },) + { + (__tmp_1,) => MoveConstructible::push_moved(&vec_.as_pointer(), (__tmp_1).clone()), + } }; assert!(({ (*vec_.borrow())[0_usize].v } == 1) && ({ (*vec_.borrow())[1_usize].v } == 9)); object!(m, DefaultCopyUserMove::constructor_1(7)); @@ -796,7 +797,7 @@ fn main_0() -> i32 { let bufs: Value> = Rc::new(RefCell::new(Vec::new())); MoveConstructible::push_moved(&bufs.as_pointer(), r.as_pointer()); { - let mut __tmp_2 = { + let mut __tmp_3 = { { let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); ::move_constructor( @@ -806,7 +807,7 @@ fn main_0() -> i32 { Rc::try_unwrap(__v).ok().unwrap().into_inner() } }; - (*bufs.borrow_mut()).push(__tmp_2); + (*bufs.borrow_mut()).push(__tmp_3); }; assert!( ({ @@ -817,10 +818,10 @@ fn main_0() -> i32 { ); object!(o1, Owner::constructor_1()); ({ - let __tmp_3: Value = Rc::new(RefCell::new(5)); + let __tmp_4: Value = Rc::new(RefCell::new(5)); MoveConstructible::push_moved( &field_ref!(o1.as_pointer(), data).as_pointer(), - __tmp_3.as_pointer(), + __tmp_4.as_pointer(), ) }); (*o1.borrow_mut()).n = 5; @@ -986,10 +987,10 @@ fn main_0() -> i32 { object!(a3, OutOfLineAssign::constructor_1()); (*a1.borrow_mut()).v = 3; ({ - let __tmp_4: Value = Rc::new(RefCell::new(4)); + let __tmp_5: Value = Rc::new(RefCell::new(4)); MoveConstructible::push_moved( &field_ref!(a1.as_pointer(), w).as_pointer(), - __tmp_4.as_pointer(), + __tmp_5.as_pointer(), ) }); assign!(a2 = a1); diff --git a/tests/unit/out/refcount/copy_move_deleted.rs b/tests/unit/out/refcount/copy_move_deleted.rs index 3779e7dc33..ff5b63e62c 100644 --- a/tests/unit/out/refcount/copy_move_deleted.rs +++ b/tests/unit/out/refcount/copy_move_deleted.rs @@ -176,14 +176,10 @@ fn main_0() -> i32 { ({ bump_ref_1(im.as_pointer()) }); let mut pim: Ptr = (im.as_pointer()); assert!((pim.with(|__s| __s.v) == 5)); - let c: Value = Rc::new(RefCell::new(Container { - inner: { - let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); - NoCopy::constructor_1(__v.as_pointer(), 6); - Rc::try_unwrap(__v).ok().unwrap().into_inner() - }, - tag: 7, - })); + object!(c, Container, |c| { + NoCopy::constructor_1(field_ptr!(c.clone(), inner), 6); + (field_ptr!(c.clone(), tag)).write(7); + }); object!(d, Container, |d| { ::move_constructor( d.clone(), diff --git a/tests/unit/out/refcount/default_function_arguments.rs b/tests/unit/out/refcount/default_function_arguments.rs index ed9a024b22..43ab71be30 100644 --- a/tests/unit/out/refcount/default_function_arguments.rs +++ b/tests/unit/out/refcount/default_function_arguments.rs @@ -129,25 +129,16 @@ fn main_0() -> i32 { == 2) ) }; - let mut arr: [Bar; 3] = [ - { - let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); - Bar::constructor_1(__v.as_pointer(), 1); - Rc::try_unwrap(__v).ok().unwrap().into_inner() - }, - { - let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); - Bar::constructor_1(__v.as_pointer(), 1); - Rc::try_unwrap(__v).ok().unwrap().into_inner() - }, - { - let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); - Bar::constructor_1(__v.as_pointer(), 1); - Rc::try_unwrap(__v).ok().unwrap().into_inner() - }, - ]; - assert!((arr[(0) as usize].v == 1)); - assert!((arr[(2) as usize].v == 1)); + let arr: Value> = Rc::new(RefCell::new( + (0..3).map(|_| ZeroInitialization::zero_init()).collect(), + )); + { + for __tmp_1 in 0..3 { + Bar::constructor_1(arr.as_pointer().decay().offset(__tmp_1), 1); + } + }; + assert!(({ (*arr.borrow())[(0) as usize].v } == 1)); + assert!(({ (*arr.borrow())[(2) as usize].v } == 1)); assert!((({ lazy_4(5,) }) == 5)); assert!((counter_2.with(|rc| *rc.borrow()) == 0)); assert!((({ lazy_4(({ next_3() }),) }) == 1)); diff --git a/tests/unit/out/refcount/destructor_temporaries.rs b/tests/unit/out/refcount/destructor_temporaries.rs index 191cf428b5..9e93451902 100644 --- a/tests/unit/out/refcount/destructor_temporaries.rs +++ b/tests/unit/out/refcount/destructor_temporaries.rs @@ -166,17 +166,17 @@ fn main_0() -> i32 { })); assert!((x == 3) && (destroyed_0.with(|rc| *rc.borrow()) == 2)); destroyed_0.with(|rc| *rc.borrow_mut() = 0); - x = { - (if (x == 3) { - { - let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); - ({ make_2(Ptr::clone(&__v.as_pointer()), 9) }); - Rc::try_unwrap(__v).ok().unwrap().into_inner() - } - } else { - S { v: 10 } - }) - .v + { + let __tmp_3: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); + x = { + if (x == 3) { + ({ make_2(Ptr::clone(&__tmp_3.as_pointer()), 9) }); + } else { + (__tmp_3.as_pointer()).write(S { v: 10 }); + }; + __tmp_3.as_pointer() + } + .with(|__s| __s.v) }; assert!( ((x == 9) && (destroyed_0.with(|rc| *rc.borrow()) == 1)) @@ -184,9 +184,9 @@ fn main_0() -> i32 { ); destroyed_0.with(|rc| *rc.borrow_mut() = 0); { - let __tmp_3: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); - ({ make_2(Ptr::clone(&__tmp_3.as_pointer()), 11) }); - let r: Ptr = __tmp_3.as_pointer(); + let __tmp_4: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); + ({ make_2(Ptr::clone(&__tmp_4.as_pointer()), 11) }); + let r: Ptr = __tmp_4.as_pointer(); assert!((destroyed_0.with(|rc| *rc.borrow()) == 0) && (r.with(|__s| __s.v) == 11)); } assert!((destroyed_0.with(|rc| *rc.borrow()) == 1) && (last_1.with(|rc| *rc.borrow()) == 11)); diff --git a/tests/unit/out/refcount/lambda_basic.rs b/tests/unit/out/refcount/lambda_basic.rs index dbfa7ce3d7..966fa206ea 100644 --- a/tests/unit/out/refcount/lambda_basic.rs +++ b/tests/unit/out/refcount/lambda_basic.rs @@ -40,7 +40,7 @@ fn main_0() -> i32 { let constants: Value i32>> = Rc::new(RefCell::new( FnPtr:: i32>::new(|mut x: i32| -> i32 { { - return ((x + 3) + 4); + return ((x + (3)) + (4)); } }), )); @@ -49,7 +49,7 @@ fn main_0() -> i32 { let derived: Value i32>> = Rc::new(RefCell::new( FnPtr:: i32>::new(|mut x: i32| -> i32 { { - return (x + (3 + 4)); + return (x + ((3) + (4))); } }), )); @@ -57,7 +57,7 @@ fn main_0() -> i32 { let implicit: Value i32>> = Rc::new(RefCell::new( FnPtr:: i32>::new(|mut x: i32| -> i32 { { - return (x + 3); + return (x + (3)); } }), )); diff --git a/tests/unit/out/refcount/lambda_capture_copy_ctor.rs b/tests/unit/out/refcount/lambda_capture_copy_ctor.rs index 8fc8d569db..f817970343 100644 --- a/tests/unit/out/refcount/lambda_capture_copy_ctor.rs +++ b/tests/unit/out/refcount/lambda_capture_copy_ctor.rs @@ -121,26 +121,26 @@ fn main_0() -> i32 { .with(|__s| __s.copies) }; assert!((returned == 2)); - let arr: Value> = Rc::new(RefCell::new(Box::new( - std::array::from_fn::<_, 2, _>(|_| { - let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); - Counted::constructor_1(__v.as_pointer()); - Rc::try_unwrap(__v).ok().unwrap().into_inner() - }), - ))); + let arr: Value> = Rc::new(RefCell::new( + (0..2).map(|_| ZeroInitialization::zero_init()).collect(), + )); + for __tmp_1 in 0..2 { + Counted::constructor_1(arr.as_pointer().decay().offset(__tmp_1)); + } let a: Value i32>> = Rc::new(RefCell::new(lambda!( { - let arr: Value> = Rc::new(RefCell::new(Box::new( - std::array::from_fn::<_, 2, _>(|__i: usize| { - let __v: Value = - Rc::new(RefCell::new(ZeroInitialization::zero_init())); + let arr: Value> = Rc::new(RefCell::new({ + let __v: Value> = Rc::new(RefCell::new( + (0..2).map(|_| ZeroInitialization::zero_init()).collect(), + )); + for __tmp_2 in 0..2 { Counted::constructor_2( - __v.as_pointer(), - LvalueRef::bind(arr.decay().offset(__i)), + __v.as_pointer().decay().offset(__tmp_2), + LvalueRef::bind(arr.decay().offset(__tmp_2)), ); - Rc::try_unwrap(__v).ok().unwrap().into_inner() - }), - ))); + } + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); }, || -> i32 { return ({ (*arr.borrow())[(0) as usize].copies } + { diff --git a/tests/unit/out/refcount/lambda_capture_value.rs b/tests/unit/out/refcount/lambda_capture_value.rs index b0a6c97a82..fa58f858b8 100644 --- a/tests/unit/out/refcount/lambda_capture_value.rs +++ b/tests/unit/out/refcount/lambda_capture_value.rs @@ -86,7 +86,7 @@ fn main_0() -> i32 { let k: Value = Rc::new(RefCell::new((*k.borrow()))); }, |mut x: i32| -> i32 { - return (x + 3); + return (x + (3)); } ))); assert!(({ (({ (*by_copy.borrow()).call(1,) }) == 4) })); @@ -113,7 +113,7 @@ fn main_0() -> i32 { let k: Ptr = k.as_pointer(); }, |mut x: i32| -> i32 { - return (x + 3); + return (x + (3)); } ))); assert!(({ (({ (*by_ref.borrow()).call(1,) }) == 4) })); diff --git a/tests/unit/out/refcount/sort_no_copy.rs b/tests/unit/out/refcount/sort_no_copy.rs index a9e08862ee..767927f716 100644 --- a/tests/unit/out/refcount/sort_no_copy.rs +++ b/tests/unit/out/refcount/sort_no_copy.rs @@ -128,33 +128,16 @@ pub fn main() { libcc2rs::cpp2rust_exit(main_0()); } fn main_0() -> i32 { - let arr: Value> = Rc::new(RefCell::new(Box::new([ - { - let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); - Tracked::constructor_2(__v.as_pointer(), 3); - Rc::try_unwrap(__v).ok().unwrap().into_inner() - }, - { - let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); - Tracked::constructor_2(__v.as_pointer(), 1); - Rc::try_unwrap(__v).ok().unwrap().into_inner() - }, - { - let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); - Tracked::constructor_2(__v.as_pointer(), 4); - Rc::try_unwrap(__v).ok().unwrap().into_inner() - }, - { - let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); - Tracked::constructor_2(__v.as_pointer(), 1); - Rc::try_unwrap(__v).ok().unwrap().into_inner() - }, - { - let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); - Tracked::constructor_2(__v.as_pointer(), 5); - Rc::try_unwrap(__v).ok().unwrap().into_inner() - }, - ]))); + let arr: Value> = Rc::new(RefCell::new( + (0..5).map(|_| ZeroInitialization::zero_init()).collect(), + )); + { + Tracked::constructor_2(arr.as_pointer().decay().offset(0), 3); + Tracked::constructor_2(arr.as_pointer().decay().offset(1), 1); + Tracked::constructor_2(arr.as_pointer().decay().offset(2), 4); + Tracked::constructor_2(arr.as_pointer().decay().offset(3), 1); + Tracked::constructor_2(arr.as_pointer().decay().offset(4), 5); + }; { let mut __tmp_0 = { arr.decay() }; let __n = arr.decay().offset((5) as isize).iter_distance(&__tmp_0); @@ -306,49 +289,48 @@ fn main_0() -> i32 { && ({ (*vec_.borrow())[3_usize].v } == 2) ); assert!((copies_0.with(|rc| *rc.borrow()) == 0)); - let bags: Value> = Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 3, _>( - |_| { - let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); - Bag::constructor_1(__v.as_pointer()); - Rc::try_unwrap(__v).ok().unwrap().into_inner() - }, - )))); + let bags: Value> = Rc::new(RefCell::new( + (0..3).map(|_| ZeroInitialization::zero_init()).collect(), + )); + for __tmp_9 in 0..3 { + Bag::constructor_1(bags.as_pointer().decay().offset(__tmp_9)); + } { - let mut __tmp_9 = { + let mut __tmp_10 = { { let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); Tracked::constructor_2(__v.as_pointer(), 1); Rc::try_unwrap(__v).ok().unwrap().into_inner() } }; - (*bags.borrow_mut())[(0) as usize].items.push(__tmp_9); + (*bags.borrow_mut())[(0) as usize].items.push(__tmp_10); }; { - let mut __tmp_10 = { + let mut __tmp_11 = { { let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); Tracked::constructor_2(__v.as_pointer(), 2); Rc::try_unwrap(__v).ok().unwrap().into_inner() } }; - (*bags.borrow_mut())[(0) as usize].items.push(__tmp_10); + (*bags.borrow_mut())[(0) as usize].items.push(__tmp_11); }; { - let mut __tmp_11 = { + let mut __tmp_12 = { { let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); Tracked::constructor_2(__v.as_pointer(), 3); Rc::try_unwrap(__v).ok().unwrap().into_inner() } }; - (*bags.borrow_mut())[(2) as usize].items.push(__tmp_11); + (*bags.borrow_mut())[(2) as usize].items.push(__tmp_12); }; { - let mut __tmp_12 = { bags.decay() }; - let __n = bags.decay().offset((3) as isize).iter_distance(&__tmp_12); + let mut __tmp_13 = { bags.decay() }; + let __n = bags.decay().offset((3) as isize).iter_distance(&__tmp_13); let mut __order: Vec = (0..__n).collect(); - __order.sort_by(|&__x, &__y| cpp_cmp(&__tmp_12.iter_at(__x), &__tmp_12.iter_at(__y))); - reorder(__order, |__i| __tmp_12.iter_at(__i)); + __order.sort_by(|&__x, &__y| cpp_cmp(&__tmp_13.iter_at(__x), &__tmp_13.iter_at(__y))); + reorder(__order, |__i| __tmp_13.iter_at(__i)); }; assert!( ({ @@ -370,8 +352,8 @@ fn main_0() -> i32 { } }, ) { - (__tmp_13, mut __tmp_14) => { - __tmp_13.with_mut(|__v: &mut Vec| __v.push(__tmp_14)); + (__tmp_14, mut __tmp_15) => { + __tmp_14.with_mut(|__v: &mut Vec| __v.push(__tmp_15)); } }; match ( @@ -384,8 +366,8 @@ fn main_0() -> i32 { } }, ) { - (__tmp_15, mut __tmp_16) => { - __tmp_15.with_mut(|__v: &mut Vec| __v.push(__tmp_16)); + (__tmp_16, mut __tmp_17) => { + __tmp_16.with_mut(|__v: &mut Vec| __v.push(__tmp_17)); } }; match ( @@ -398,16 +380,16 @@ fn main_0() -> i32 { } }, ) { - (__tmp_17, mut __tmp_18) => { - __tmp_17.with_mut(|__v: &mut Vec| __v.push(__tmp_18)); + (__tmp_18, mut __tmp_19) => { + __tmp_18.with_mut(|__v: &mut Vec| __v.push(__tmp_19)); } }; { - let mut __tmp_19 = { bag_vec.decay().begin() }; - let __n = bag_vec.decay().end().iter_distance(&__tmp_19); + let mut __tmp_20 = { bag_vec.decay().begin() }; + let __n = bag_vec.decay().end().iter_distance(&__tmp_20); let mut __order: Vec = (0..__n).collect(); - __order.sort_by(|&__x, &__y| cpp_cmp(&__tmp_19.iter_at(__x), &__tmp_19.iter_at(__y))); - reorder(__order, |__i| __tmp_19.iter_at(__i)); + __order.sort_by(|&__x, &__y| cpp_cmp(&__tmp_20.iter_at(__x), &__tmp_20.iter_at(__y))); + reorder(__order, |__i| __tmp_20.iter_at(__i)); }; assert!( ({ @@ -437,8 +419,8 @@ fn main_0() -> i32 { .as_pointer(), Rc::new(RefCell::new(bag_vec.decay().begin().iter_add(1_isize))).as_pointer(), ) { - (__tmp_20, __tmp_21) => { - WeaklyEqualityComparableWith::equal((__tmp_20).clone(), (__tmp_21).clone()) + (__tmp_21, __tmp_22) => { + WeaklyEqualityComparableWith::equal((__tmp_21).clone(), (__tmp_22).clone()) } }); assert!(match ( @@ -463,8 +445,8 @@ fn main_0() -> i32 { .as_pointer(), Rc::new(RefCell::new(bag_vec.decay().begin().iter_add(2_isize))).as_pointer(), ) { - (__tmp_22, __tmp_23) => { - WeaklyEqualityComparableWith::equal((__tmp_22).clone(), (__tmp_23).clone()) + (__tmp_23, __tmp_24) => { + WeaklyEqualityComparableWith::equal((__tmp_23).clone(), (__tmp_24).clone()) } }); assert!( @@ -490,14 +472,14 @@ fn main_0() -> i32 { assert!((copies_0.with(|rc| *rc.borrow()) == 0)); let bag_map: Value, Value)>> = Rc::new(RefCell::new(Map::new())); match (bag_vec.decay().offset(2_usize),) { - (__tmp_24,) => { - ({ (*bag_map.borrow_mut()).subscript_move(__tmp_24, |__k| cpp_move!(__k), || 0_i32) }) + (__tmp_25,) => { + ({ (*bag_map.borrow_mut()).subscript_move(__tmp_25, |__k| cpp_move!(__k), || 0_i32) }) } } .write(2); match (bag_vec.decay().offset(0_usize),) { - (__tmp_25,) => { - ({ (*bag_map.borrow_mut()).subscript_move(__tmp_25, |__k| cpp_move!(__k), || 0_i32) }) + (__tmp_26,) => { + ({ (*bag_map.borrow_mut()).subscript_move(__tmp_26, |__k| cpp_move!(__k), || 0_i32) }) } } .write(0); @@ -524,60 +506,60 @@ fn main_0() -> i32 { )) .as_pointer(), ) { - (__tmp_26, __tmp_27) => { - WeaklyEqualityComparableWith::equal((__tmp_26).clone(), (__tmp_27).clone()) + (__tmp_27, __tmp_28) => { + WeaklyEqualityComparableWith::equal((__tmp_27).clone(), (__tmp_28).clone()) } }); assert!((copies_0.with(|rc| *rc.borrow()) == 0)); let moves: Value> = Rc::new(RefCell::new(Vec::new())); { - let mut __tmp_28 = { + let mut __tmp_29 = { { let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); MoveOnly::constructor_1(__v.as_pointer(), 3); Rc::try_unwrap(__v).ok().unwrap().into_inner() } }; - (*moves.borrow_mut()).push(__tmp_28); + (*moves.borrow_mut()).push(__tmp_29); }; { - let mut __tmp_29 = { + let mut __tmp_30 = { { let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); MoveOnly::constructor_1(__v.as_pointer(), 1); Rc::try_unwrap(__v).ok().unwrap().into_inner() } }; - (*moves.borrow_mut()).push(__tmp_29); + (*moves.borrow_mut()).push(__tmp_30); }; { - let mut __tmp_30 = { + let mut __tmp_31 = { { let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); MoveOnly::constructor_1(__v.as_pointer(), 2); Rc::try_unwrap(__v).ok().unwrap().into_inner() } }; - (*moves.borrow_mut()).push(__tmp_30); + (*moves.borrow_mut()).push(__tmp_31); }; { - let mut __tmp_31 = { moves.decay().begin() }; - let __n = moves.decay().end().iter_distance(&__tmp_31); + let mut __tmp_32 = { moves.decay().begin() }; + let __n = moves.decay().end().iter_distance(&__tmp_32); let mut __order: Vec = (0..__n).collect(); - __order.sort_by(|&__x, &__y| cpp_cmp(&__tmp_31.iter_at(__x), &__tmp_31.iter_at(__y))); - reorder(__order, |__i| __tmp_31.iter_at(__i)); + __order.sort_by(|&__x, &__y| cpp_cmp(&__tmp_32.iter_at(__x), &__tmp_32.iter_at(__y))); + reorder(__order, |__i| __tmp_32.iter_at(__i)); }; assert!( (({ (*moves.borrow())[0_usize].v } == 1) && ({ (*moves.borrow())[1_usize].v } == 2)) && ({ (*moves.borrow())[2_usize].v } == 3) ); { - let mut __tmp_32 = { moves.decay().begin() }; - let __n = moves.decay().end().iter_distance(&__tmp_32); + let mut __tmp_33 = { moves.decay().begin() }; + let __n = moves.decay().end().iter_distance(&__tmp_33); let mut __order: Vec = (0..__n).collect(); __order.sort_by(|&__x, &__y| { - let __a = __tmp_32.iter_at(__x); - let __b = __tmp_32.iter_at(__y); + let __a = __tmp_33.iter_at(__x); + let __b = __tmp_33.iter_at(__y); if FnPtr::, Ptr) -> bool>::new( |a: Ptr, b: Ptr| -> bool { { @@ -602,7 +584,7 @@ fn main_0() -> i32 { std::cmp::Ordering::Equal } }); - reorder(__order, |__i| __tmp_32.iter_at(__i)); + reorder(__order, |__i| __tmp_33.iter_at(__i)); }; assert!( (({ (*moves.borrow())[0_usize].v } == 3) && ({ (*moves.borrow())[1_usize].v } == 2)) diff --git a/tests/unit/out/refcount/struct_default_ctor.rs b/tests/unit/out/refcount/struct_default_ctor.rs index cbe377b96f..3e2a3f0b8a 100644 --- a/tests/unit/out/refcount/struct_default_ctor.rs +++ b/tests/unit/out/refcount/struct_default_ctor.rs @@ -76,11 +76,9 @@ pub struct Holder { } impl Holder { pub fn constructor_1(this: Ptr) { - field!(this, items).write(Box::new(std::array::from_fn::<_, 2, _>(|_| { - let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); - S::constructor_1(__v.as_pointer()); - Rc::try_unwrap(__v).ok().unwrap().into_inner() - }))); + for __tmp_0 in 0..2 { + S::constructor_1(field_ptr!(this, items).decay().offset(__tmp_0)); + } } } impl Default for Holder { diff --git a/tests/unit/out/refcount/structured_binding_copy_ctor.rs b/tests/unit/out/refcount/structured_binding_copy_ctor.rs index b015c29c02..1cf42c1327 100644 --- a/tests/unit/out/refcount/structured_binding_copy_ctor.rs +++ b/tests/unit/out/refcount/structured_binding_copy_ctor.rs @@ -55,11 +55,10 @@ pub fn main() { } fn main_0() -> i32 { object!(s, Counted::constructor_1(1, 2)); - let __decomp_2: Value = Rc::new(RefCell::new({ - let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); - Counted::constructor_2(__v.as_pointer(), LvalueRef::bind(s.as_pointer())); - Rc::try_unwrap(__v).ok().unwrap().into_inner() - })); + object!( + __decomp_2, + Counted::constructor_2(LvalueRef::bind(s.as_pointer())) + ); assert!((copies_0.with(|rc| *rc.borrow()) == 1)); assert!(({ (*__decomp_2.borrow()).x } == 2)); assert!(({ (*__decomp_2.borrow()).y } == 4)); @@ -70,10 +69,8 @@ fn main_0() -> i32 { let __decomp_4: Ptr = s.as_pointer(); assert!((copies_0.with(|rc| *rc.borrow()) == 1)); assert!((__decomp_4.with(|__s| __s.x) == 1)); - let __decomp_5: Value = Rc::new(RefCell::new({ - let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); - ({ make_counted_1(Ptr::clone(&__v.as_pointer())) }); - Rc::try_unwrap(__v).ok().unwrap().into_inner() + object!(__decomp_5, Counted, |__decomp_5| ({ + make_counted_1(Ptr::clone(&__decomp_5.clone())) })); assert!((copies_0.with(|rc| *rc.borrow()) == 1)); assert!(({ (*__decomp_5.borrow()).x } == 5)); diff --git a/tests/unit/out/refcount/structured_binding_element_ctor.rs b/tests/unit/out/refcount/structured_binding_element_ctor.rs index 2c555331a3..da3e86b38c 100644 --- a/tests/unit/out/refcount/structured_binding_element_ctor.rs +++ b/tests/unit/out/refcount/structured_binding_element_ctor.rs @@ -57,18 +57,10 @@ pub struct Two { } impl Two {} pub fn make_two_2(__return: Ptr) { - __return.write(Two { - a: { - let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); - Elem::constructor_1(__v.as_pointer(), 1); - Rc::try_unwrap(__v).ok().unwrap().into_inner() - }, - b: { - let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); - Elem::constructor_1(__v.as_pointer(), 2); - Rc::try_unwrap(__v).ok().unwrap().into_inner() - }, - }); + { + Elem::constructor_1(field_ptr!(__return.clone(), a), 1); + Elem::constructor_1(field_ptr!(__return.clone(), b), 2); + }; return; } pub fn main() { @@ -76,28 +68,18 @@ pub fn main() { libcc2rs::cpp2rust_exit(main_0()); } fn main_0() -> i32 { - let t: Value = Rc::new(RefCell::new(Two { - a: { - let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); - Elem::constructor_1(__v.as_pointer(), 1); - Rc::try_unwrap(__v).ok().unwrap().into_inner() - }, - b: { - let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); - Elem::constructor_1(__v.as_pointer(), 2); - Rc::try_unwrap(__v).ok().unwrap().into_inner() - }, - })); + object!(t, Two, |t| { + Elem::constructor_1(field_ptr!(t.clone(), a), 1); + Elem::constructor_1(field_ptr!(t.clone(), b), 2); + }); let mut base_copies: i32 = copies_0.with(|rc| *rc.borrow()); let mut base_moves: i32 = moves_1.with(|rc| *rc.borrow()); - let __decomp_3: Value = Rc::new(RefCell::new({ - let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); + object!(__decomp_3, Two, |__decomp_3| { ::copy_constructor( - __v.as_pointer(), + __decomp_3.clone(), LvalueRef::bind(t.as_pointer()), - ); - Rc::try_unwrap(__v).ok().unwrap().into_inner() - })); + ) + }); assert!((copies_0.with(|rc| *rc.borrow()) == (base_copies + 2))); assert!((moves_1.with(|rc| *rc.borrow()) == base_moves)); assert!(({ (*__decomp_3.borrow()).a.v } == 101)); @@ -107,46 +89,42 @@ fn main_0() -> i32 { assert!(({ (*__decomp_4.upgrade().deref()).a.v } == 1)); field!(field!(__decomp_4, a), v).write(50); assert!(({ (*t.borrow()).a.v } == 50)); - let __decomp_5: Value = Rc::new(RefCell::new({ - let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); + object!(__decomp_5, Two, |__decomp_5| { ::move_constructor( - __v.as_pointer(), + __decomp_5.clone(), RvalueRef::bind(t.as_pointer()), - ); - Rc::try_unwrap(__v).ok().unwrap().into_inner() - })); + ) + }); assert!((moves_1.with(|rc| *rc.borrow()) == (base_moves + 2))); assert!(({ (*__decomp_5.borrow()).a.v } == 1050)); assert!(({ (*__decomp_5.borrow()).b.v } == 1002)); assert!(({ (*t.borrow()).a.v } == -1_i32)); let mut before_copies: i32 = copies_0.with(|rc| *rc.borrow()); let mut before_moves: i32 = moves_1.with(|rc| *rc.borrow()); - let __decomp_6: Value = Rc::new(RefCell::new({ - let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); - ({ make_two_2(Ptr::clone(&__v.as_pointer())) }); - Rc::try_unwrap(__v).ok().unwrap().into_inner() + object!(__decomp_6, Two, |__decomp_6| ({ + make_two_2(Ptr::clone(&__decomp_6.clone())) })); assert!((copies_0.with(|rc| *rc.borrow()) == before_copies)); assert!((moves_1.with(|rc| *rc.borrow()) == before_moves)); assert!(({ (*__decomp_6.borrow()).a.v } == 1)); let p: Value<(Value, Value)> = Rc::new(RefCell::new(( Rc::new(RefCell::new({ - let __tmp_0: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); - Elem::constructor_1(__tmp_0.as_pointer(), 7); + let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); { - let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); - Elem::constructor_3(__v.as_pointer(), RvalueRef::bind(__tmp_0.as_pointer())); - Rc::try_unwrap(__v).ok().unwrap().into_inner() - } + let __tmp_0: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); + Elem::constructor_1(__tmp_0.as_pointer(), 7); + Elem::constructor_3(__v.as_pointer(), RvalueRef::bind(__tmp_0.as_pointer())) + }; + Rc::try_unwrap(__v).ok().unwrap().into_inner() })), Rc::new(RefCell::new({ - let __tmp_1: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); - Elem::constructor_1(__tmp_1.as_pointer(), 8); + let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); { - let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); - Elem::constructor_3(__v.as_pointer(), RvalueRef::bind(__tmp_1.as_pointer())); - Rc::try_unwrap(__v).ok().unwrap().into_inner() - } + let __tmp_1: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); + Elem::constructor_1(__tmp_1.as_pointer(), 8); + Elem::constructor_3(__v.as_pointer(), RvalueRef::bind(__tmp_1.as_pointer())) + }; + Rc::try_unwrap(__v).ok().unwrap().into_inner() })), ))); before_copies = copies_0.with(|rc| *rc.borrow()); diff --git a/tests/unit/out/refcount/structured_binding_move_ctor.rs b/tests/unit/out/refcount/structured_binding_move_ctor.rs index f284322384..135ad91bf7 100644 --- a/tests/unit/out/refcount/structured_binding_move_ctor.rs +++ b/tests/unit/out/refcount/structured_binding_move_ctor.rs @@ -91,11 +91,10 @@ pub fn main() { } fn main_0() -> i32 { object!(m, Movable::constructor_1(3, 4)); - let __decomp_2: Value = Rc::new(RefCell::new({ - let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); - Movable::constructor_3(__v.as_pointer(), RvalueRef::bind(m.as_pointer())); - Rc::try_unwrap(__v).ok().unwrap().into_inner() - })); + object!( + __decomp_2, + Movable::constructor_3(RvalueRef::bind(m.as_pointer())) + ); assert!((moves_1.with(|rc| *rc.borrow()) == 1)); assert!((copies_0.with(|rc| *rc.borrow()) == 0)); assert!(({ (*__decomp_2.borrow()).x } == 3)); @@ -106,10 +105,8 @@ fn main_0() -> i32 { xs: Vec::from([1, 2, 3]), ys: Vec::from([4, 5]), })); - let __decomp_3: Value = Rc::new(RefCell::new({ - let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); - ({ Holder::extract(h.as_pointer(), Ptr::clone(&__v.as_pointer())) }); - Rc::try_unwrap(__v).ok().unwrap().into_inner() + object!(__decomp_3, Holder, |__decomp_3| ({ + Holder::extract(h.as_pointer(), Ptr::clone(&__decomp_3.clone())) })); assert!(({ ((*__decomp_3.borrow()).xs.len() == 3_usize) })); assert!(({ ((*__decomp_3.borrow()).ys.len() == 2_usize) })); diff --git a/tests/unit/out/refcount/swap_map_no_copy.rs b/tests/unit/out/refcount/swap_map_no_copy.rs index 841a656559..11f709bd9a 100644 --- a/tests/unit/out/refcount/swap_map_no_copy.rs +++ b/tests/unit/out/refcount/swap_map_no_copy.rs @@ -103,16 +103,12 @@ pub struct Arrays { } impl Arrays { pub fn constructor_1(this: Ptr) { - field!(this, a).write(Box::new(std::array::from_fn::<_, 2, _>(|_| { - let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); - Tracked::constructor_1(__v.as_pointer()); - Rc::try_unwrap(__v).ok().unwrap().into_inner() - }))); - field!(this, b).write(Box::new(std::array::from_fn::<_, 2, _>(|_| { - let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); - Tracked::constructor_1(__v.as_pointer()); - Rc::try_unwrap(__v).ok().unwrap().into_inner() - }))); + for __tmp_0 in 0..2 { + Tracked::constructor_1(field_ptr!(this, a).decay().offset(__tmp_0)); + } + for __tmp_1 in 0..2 { + Tracked::constructor_1(field_ptr!(this, b).decay().offset(__tmp_1)); + } } } impl Default for Arrays { @@ -323,10 +319,10 @@ fn main_0() -> i32 { Tracked::constructor_2(_o.as_pointer(), 10); assign!( *match (Rc::new(RefCell::new(1)).as_pointer(),) { - (__tmp_0,) => { + (__tmp_2,) => { ({ (*m1.borrow_mut()).subscript_move( - __tmp_0, + __tmp_2, |__k| cpp_move!(__k), || ::default(), ) @@ -340,10 +336,10 @@ fn main_0() -> i32 { Tracked::constructor_2(_o.as_pointer(), 20); assign!( *match (Rc::new(RefCell::new(2)).as_pointer(),) { - (__tmp_1,) => { + (__tmp_3,) => { ({ (*m1.borrow_mut()).subscript_move( - __tmp_1, + __tmp_3, |__k| cpp_move!(__k), || ::default(), ) @@ -358,10 +354,10 @@ fn main_0() -> i32 { Tracked::constructor_2(_o.as_pointer(), 30); assign!( *match (Rc::new(RefCell::new(3)).as_pointer(),) { - (__tmp_2,) => { + (__tmp_4,) => { ({ (*m2.borrow_mut()).subscript_move( - __tmp_2, + __tmp_4, |__k| cpp_move!(__k), || ::default(), ) @@ -373,10 +369,10 @@ fn main_0() -> i32 { assert!((copies_0.with(|rc| *rc.borrow()) == 0)); Swappable::swap( match (Rc::new(RefCell::new(1)).as_pointer(),) { - (__tmp_3,) => { + (__tmp_5,) => { ({ (*m1.borrow_mut()).subscript_move( - __tmp_3, + __tmp_5, |__k| cpp_move!(__k), || ::default(), ) @@ -384,10 +380,10 @@ fn main_0() -> i32 { } }, match (Rc::new(RefCell::new(2)).as_pointer(),) { - (__tmp_4,) => { + (__tmp_6,) => { ({ (*m1.borrow_mut()).subscript_move( - __tmp_4, + __tmp_6, |__k| cpp_move!(__k), || ::default(), ) @@ -397,10 +393,10 @@ fn main_0() -> i32 { ); assert!( (match (Rc::new(RefCell::new(1)).as_pointer(),) { - (__tmp_5,) => { + (__tmp_7,) => { ({ (*m1.borrow_mut()).subscript_move( - __tmp_5, + __tmp_7, |__k| cpp_move!(__k), || ::default(), ) @@ -410,10 +406,10 @@ fn main_0() -> i32 { .with(|__s| __s.v) == 20) && (match (Rc::new(RefCell::new(2)).as_pointer(),) { - (__tmp_6,) => { + (__tmp_8,) => { ({ (*m1.borrow_mut()).subscript_move( - __tmp_6, + __tmp_8, |__k| cpp_move!(__k), || ::default(), ) @@ -428,10 +424,10 @@ fn main_0() -> i32 { assert!( ((*m1.borrow()).len() == 1_usize) && (match (Rc::new(RefCell::new(3)).as_pointer(),) { - (__tmp_7,) => { + (__tmp_9,) => { ({ (*m1.borrow_mut()).subscript_move( - __tmp_7, + __tmp_9, |__k| cpp_move!(__k), || ::default(), ) @@ -444,10 +440,10 @@ fn main_0() -> i32 { assert!( (((*m2.borrow()).len() == 2_usize) && (match (Rc::new(RefCell::new(1)).as_pointer(),) { - (__tmp_8,) => { + (__tmp_10,) => { ({ (*m2.borrow_mut()).subscript_move( - __tmp_8, + __tmp_10, |__k| cpp_move!(__k), || ::default(), ) @@ -457,10 +453,10 @@ fn main_0() -> i32 { .with(|__s| __s.v) == 20)) && (match (Rc::new(RefCell::new(2)).as_pointer(),) { - (__tmp_9,) => { + (__tmp_11,) => { ({ (*m2.borrow_mut()).subscript_move( - __tmp_9, + __tmp_11, |__k| cpp_move!(__k), || ::default(), ) @@ -471,23 +467,14 @@ fn main_0() -> i32 { == 10) ); assert!((copies_0.with(|rc| *rc.borrow()) == 0)); - let arr: Value> = Rc::new(RefCell::new(Box::new([ - { - let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); - Tracked::constructor_2(__v.as_pointer(), 1); - Rc::try_unwrap(__v).ok().unwrap().into_inner() - }, - { - let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); - Tracked::constructor_2(__v.as_pointer(), 2); - Rc::try_unwrap(__v).ok().unwrap().into_inner() - }, - { - let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); - Tracked::constructor_2(__v.as_pointer(), 3); - Rc::try_unwrap(__v).ok().unwrap().into_inner() - }, - ]))); + let arr: Value> = Rc::new(RefCell::new( + (0..3).map(|_| ZeroInitialization::zero_init()).collect(), + )); + { + Tracked::constructor_2(arr.as_pointer().decay().offset(0), 1); + Tracked::constructor_2(arr.as_pointer().decay().offset(1), 2); + Tracked::constructor_2(arr.as_pointer().decay().offset(2), 3); + }; Swappable::swap(arr.decay().offset(0), arr.decay().offset(2)); assert!( ({ (*arr.borrow())[(0) as usize].v } == 3) && ({ (*arr.borrow())[(2) as usize].v } == 1) @@ -497,66 +484,45 @@ fn main_0() -> i32 { assert!((copies_0.with(|rc| *rc.borrow()) == 0)); let vec_: Value> = Rc::new(RefCell::new(Vec::new())); { - let mut __tmp_10 = { + let mut __tmp_12 = { { let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); Tracked::constructor_2(__v.as_pointer(), 4); Rc::try_unwrap(__v).ok().unwrap().into_inner() } }; - (*vec_.borrow_mut()).push(__tmp_10); + (*vec_.borrow_mut()).push(__tmp_12); }; { - let mut __tmp_11 = { + let mut __tmp_13 = { { let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); Tracked::constructor_2(__v.as_pointer(), 5); Rc::try_unwrap(__v).ok().unwrap().into_inner() } }; - (*vec_.borrow_mut()).push(__tmp_11); + (*vec_.borrow_mut()).push(__tmp_13); }; Swappable::swap(vec_.decay().offset(0_usize), vec_.decay().offset(1_usize)); assert!(({ (*vec_.borrow())[0_usize].v } == 5) && ({ (*vec_.borrow())[1_usize].v } == 4)); assert!((copies_0.with(|rc| *rc.borrow()) == 0)); - let pairs: Value> = Rc::new(RefCell::new(Box::new([ - Pair { - x: { - let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); - Tracked::constructor_2(__v.as_pointer(), 6); - Rc::try_unwrap(__v).ok().unwrap().into_inner() - }, - y: { - let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); - Tracked::constructor_2(__v.as_pointer(), 7); - Rc::try_unwrap(__v).ok().unwrap().into_inner() - }, - }, - Pair { - x: { - let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); - Tracked::constructor_2(__v.as_pointer(), 8); - Rc::try_unwrap(__v).ok().unwrap().into_inner() - }, - y: { - let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); - Tracked::constructor_2(__v.as_pointer(), 9); - Rc::try_unwrap(__v).ok().unwrap().into_inner() - }, - }, - ]))); - let p: Value = Rc::new(RefCell::new(Pair { - x: { - let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); - Tracked::constructor_2(__v.as_pointer(), 10); - Rc::try_unwrap(__v).ok().unwrap().into_inner() - }, - y: { - let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); - Tracked::constructor_2(__v.as_pointer(), 11); - Rc::try_unwrap(__v).ok().unwrap().into_inner() - }, - })); + let pairs: Value> = Rc::new(RefCell::new( + (0..2).map(|_| ZeroInitialization::zero_init()).collect(), + )); + { + { + Tracked::constructor_2(field_ptr!(pairs.as_pointer().decay().offset(0), x), 6); + Tracked::constructor_2(field_ptr!(pairs.as_pointer().decay().offset(0), y), 7); + }; + { + Tracked::constructor_2(field_ptr!(pairs.as_pointer().decay().offset(1), x), 8); + Tracked::constructor_2(field_ptr!(pairs.as_pointer().decay().offset(1), y), 9); + }; + }; + object!(p, Pair, |p| { + Tracked::constructor_2(field_ptr!(p.clone(), x), 10); + Tracked::constructor_2(field_ptr!(p.clone(), y), 11); + }); Swappable::swap( field_ptr!(pairs.decay().offset(0), x), field_ptr!(p.as_pointer(), y), @@ -608,8 +574,8 @@ fn main_0() -> i32 { Rc::try_unwrap(__v).ok().unwrap().into_inner() } }) { - (__tmp_12, mut __tmp_13) => { - __tmp_12.with_mut(|__v: &mut Vec| __v.push(__tmp_13)); + (__tmp_14, mut __tmp_15) => { + __tmp_14.with_mut(|__v: &mut Vec| __v.push(__tmp_15)); } }; match (grid.decay().offset(0_usize), { @@ -619,8 +585,8 @@ fn main_0() -> i32 { Rc::try_unwrap(__v).ok().unwrap().into_inner() } }) { - (__tmp_14, mut __tmp_15) => { - __tmp_14.with_mut(|__v: &mut Vec| __v.push(__tmp_15)); + (__tmp_16, mut __tmp_17) => { + __tmp_16.with_mut(|__v: &mut Vec| __v.push(__tmp_17)); } }; match (grid.decay().offset(1_usize), { @@ -630,8 +596,8 @@ fn main_0() -> i32 { Rc::try_unwrap(__v).ok().unwrap().into_inner() } }) { - (__tmp_16, mut __tmp_17) => { - __tmp_16.with_mut(|__v: &mut Vec| __v.push(__tmp_17)); + (__tmp_18, mut __tmp_19) => { + __tmp_18.with_mut(|__v: &mut Vec| __v.push(__tmp_19)); } }; Swappable::swap( @@ -660,10 +626,10 @@ fn main_0() -> i32 { Tracked::constructor_2(_o.as_pointer(), 1); assign!( *match (Rc::new(RefCell::new(1)).as_pointer(),) { - (__tmp_18,) => { + (__tmp_20,) => { ({ (*maps.borrow_mut())[(0) as usize].subscript_move( - __tmp_18, + __tmp_20, |__k| cpp_move!(__k), || ::default(), ) @@ -677,10 +643,10 @@ fn main_0() -> i32 { Tracked::constructor_2(_o.as_pointer(), 2); assign!( *match (Rc::new(RefCell::new(2)).as_pointer(),) { - (__tmp_19,) => { + (__tmp_21,) => { ({ (*maps.borrow_mut())[(1) as usize].subscript_move( - __tmp_19, + __tmp_21, |__k| cpp_move!(__k), || ::default(), ) @@ -693,10 +659,10 @@ fn main_0() -> i32 { assert!( ((*maps.borrow())[(0) as usize].len() == 1_usize) && (match (Rc::new(RefCell::new(2)).as_pointer(),) { - (__tmp_20,) => { + (__tmp_22,) => { ({ (*maps.borrow_mut())[(0) as usize].subscript_move( - __tmp_20, + __tmp_22, |__k| cpp_move!(__k), || ::default(), ) @@ -709,10 +675,10 @@ fn main_0() -> i32 { assert!( ((*maps.borrow())[(1) as usize].len() == 1_usize) && (match (Rc::new(RefCell::new(1)).as_pointer(),) { - (__tmp_21,) => { + (__tmp_23,) => { ({ (*maps.borrow_mut())[(1) as usize].subscript_move( - __tmp_21, + __tmp_23, |__k| cpp_move!(__k), || ::default(), ) @@ -731,20 +697,10 @@ fn main_0() -> i32 { (copy_only_copies_1.with(|rc| *rc.borrow()) == 3) && (copy_only_dtors_2.with(|rc| *rc.borrow()) == 1) ); - let cp: Value = Rc::new(RefCell::new(CopyOnlyPair { - x: { - let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); - CopyOnly::constructor_1(__v.as_pointer(), 3); - Rc::try_unwrap(__v).ok().unwrap().into_inner() - }, - y: { - let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); - CopyOnly::constructor_1(__v.as_pointer(), 4); - Rc::try_unwrap(__v).ok().unwrap().into_inner() - }, - })); - let _dtor_cp = - std::mem::DropGuard::new(cp.clone(), |__p| Destructible::destroy(__p.as_pointer())); + object!(cp, CopyOnlyPair, |cp| { + CopyOnly::constructor_1(field_ptr!(cp.clone(), x), 3); + CopyOnly::constructor_1(field_ptr!(cp.clone(), y), 4); + }); Swappable::swap( field_ptr!(cp.as_pointer(), x), field_ptr!(cp.as_pointer(), y), @@ -754,18 +710,13 @@ fn main_0() -> i32 { (copy_only_copies_1.with(|rc| *rc.borrow()) == 6) && (copy_only_dtors_2.with(|rc| *rc.borrow()) == 2) ); - let carr: Value> = Rc::new(RefCell::new(Box::new([ - { - let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); - CopyOnly::constructor_1(__v.as_pointer(), 5); - Rc::try_unwrap(__v).ok().unwrap().into_inner() - }, - { - let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); - CopyOnly::constructor_1(__v.as_pointer(), 6); - Rc::try_unwrap(__v).ok().unwrap().into_inner() - }, - ]))); + let carr: Value> = Rc::new(RefCell::new( + (0..2).map(|_| ZeroInitialization::zero_init()).collect(), + )); + { + CopyOnly::constructor_1(carr.as_pointer().decay().offset(0), 5); + CopyOnly::constructor_1(carr.as_pointer().decay().offset(1), 6); + }; Swappable::swap(carr.decay().offset(0), carr.decay().offset(1)); assert!( ({ (*carr.borrow())[(0) as usize].v } == 6) && ({ (*carr.borrow())[(1) as usize].v } == 5) @@ -779,38 +730,23 @@ fn main_0() -> i32 { Swappable::swap(mv1.as_pointer(), mv2.as_pointer()); assert!(({ (*mv1.borrow()).v } == 2) && ({ (*mv2.borrow()).v } == 1)); assert!(({ swapped_once_6() })); - let mp: Value = Rc::new(RefCell::new(MovedPair { - x: { - let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); - Moved::constructor_1(__v.as_pointer(), 3); - Rc::try_unwrap(__v).ok().unwrap().into_inner() - }, - y: { - let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); - Moved::constructor_1(__v.as_pointer(), 4); - Rc::try_unwrap(__v).ok().unwrap().into_inner() - }, - })); - let _dtor_mp = - std::mem::DropGuard::new(mp.clone(), |__p| Destructible::destroy(__p.as_pointer())); + object!(mp, MovedPair, |mp| { + Moved::constructor_1(field_ptr!(mp.clone(), x), 3); + Moved::constructor_1(field_ptr!(mp.clone(), y), 4); + }); Swappable::swap( field_ptr!(mp.as_pointer(), x), field_ptr!(mp.as_pointer(), y), ); assert!(({ (*mp.borrow()).x.v } == 4) && ({ (*mp.borrow()).y.v } == 3)); assert!(({ swapped_once_6() })); - let marr: Value> = Rc::new(RefCell::new(Box::new([ - { - let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); - Moved::constructor_1(__v.as_pointer(), 5); - Rc::try_unwrap(__v).ok().unwrap().into_inner() - }, - { - let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); - Moved::constructor_1(__v.as_pointer(), 6); - Rc::try_unwrap(__v).ok().unwrap().into_inner() - }, - ]))); + let marr: Value> = Rc::new(RefCell::new( + (0..2).map(|_| ZeroInitialization::zero_init()).collect(), + )); + { + Moved::constructor_1(marr.as_pointer().decay().offset(0), 5); + Moved::constructor_1(marr.as_pointer().decay().offset(1), 6); + }; Swappable::swap(marr.decay().offset(0), marr.decay().offset(1)); assert!( ({ (*marr.borrow())[(0) as usize].v } == 6) && ({ (*marr.borrow())[(1) as usize].v } == 5) diff --git a/tests/unit/out/refcount/this.rs b/tests/unit/out/refcount/this.rs index cf648bb3b7..d89df44815 100644 --- a/tests/unit/out/refcount/this.rs +++ b/tests/unit/out/refcount/this.rs @@ -207,11 +207,7 @@ fn main_0() -> i32 { assert!(({ { (*s.borrow()).self__.clone() } } == { { (*other.borrow()).self__.clone() } })); object!(u, S::constructor_2(1, Pointer::bind((s.as_pointer())))); assert!(({ { (*u.borrow()).self__.clone() } } == { (s.as_pointer()) })); - let s_const: Value = Rc::new(RefCell::new({ - let __v: Value = Rc::new(RefCell::new(ZeroInitialization::zero_init())); - S::constructor_1(__v.as_pointer(), 100); - Rc::try_unwrap(__v).ok().unwrap().into_inner() - })); + object!(s_const, S::constructor_1(100)); object!( u1, S::constructor_3(1, Pointer::bind((s_const.as_pointer()))) diff --git a/tests/unit/out/unsafe/array_of_noncopy_struct.rs b/tests/unit/out/unsafe/array_of_noncopy_struct.rs index 2525d2f09d..621f12d146 100644 --- a/tests/unit/out/unsafe/array_of_noncopy_struct.rs +++ b/tests/unit/out/unsafe/array_of_noncopy_struct.rs @@ -48,15 +48,14 @@ pub fn main() { } } unsafe fn main_0() -> i32 { - let mut arr: [NonCopy; 3] = std::array::from_fn::<_, 3, _>(|_| { - let mut __v: NonCopy = ZeroInitializationUnsafe::zero_init(); - NonCopy::constructor_1(&raw mut __v); - __v - }); + let mut arr: [NonCopy; 3] = ZeroInitializationUnsafe::zero_init(); + for __tmp_0 in 0..3 { + NonCopy::constructor_1(&raw mut (*&raw mut arr)[__tmp_0]); + } arr[(0) as usize].tag = 7; { - let mut __tmp_0: i32 = 42; - arr[(1) as usize].data.push_moved(&mut __tmp_0) + let mut __tmp_1: i32 = 42; + arr[(1) as usize].data.push_moved(&mut __tmp_1) }; assert!(((arr[(0) as usize].tag) == (7))); assert!(((arr[(1) as usize].data.len()) == (1_usize))); diff --git a/tests/unit/out/unsafe/comma_operator.rs b/tests/unit/out/unsafe/comma_operator.rs index e45b678f5b..872789bd05 100644 --- a/tests/unit/out/unsafe/comma_operator.rs +++ b/tests/unit/out/unsafe/comma_operator.rs @@ -23,6 +23,34 @@ pub struct Pair { pub first: i32, pub second: i32, } +#[repr(C)] +#[derive( + FnPtrArg, + ZeroInitializationUnsafe, + DefaultInitializableUnsafe, + MemberwiseUnsafe, + MoveConstructibleUnsafe, + MoveAssignableUnsafe, + SwappableUnsafe, + Default, +)] +#[memberwise(copy_assign)] +#[derive(ConversionsUnsafe)] +#[conversions(copy)] +#[constructor(constructor_1, i32)] +#[constructor(constructor_2, LvalueRefUnsafe>)] +pub struct Counted { + pub value: i32, +} +impl Counted { + pub unsafe fn constructor_1(this: *mut Self, mut v: i32) { + (&raw mut (*this).value).write(v); + } + pub unsafe fn constructor_2(this: *mut Self, other: LvalueRefUnsafe>) { + let other = (other.as_ptr() as *const Counted); + (&raw mut (*this).value).write((((*other).value) + (100))); + } +} pub unsafe fn twice_0(v: *const i64) -> i64 { return ((*v) * (2_i64)); } @@ -111,6 +139,12 @@ unsafe fn main_0() -> i32 { }) == (3)) ); assert!(((a) == (7))); + object_unsafe!(c, Counted, |c| { + a = 8; + Counted::constructor_1(c, 4) + }); + assert!(((a) == (8))); + assert!(((c.value) == (4))); return 0; } pub unsafe fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/unsafe/copy_assign.rs b/tests/unit/out/unsafe/copy_assign.rs index 405d122f4c..c8100d0015 100644 --- a/tests/unit/out/unsafe/copy_assign.rs +++ b/tests/unit/out/unsafe/copy_assign.rs @@ -415,25 +415,13 @@ unsafe fn main_0() -> i32 { assign_unsafe!((*pa) = b); assert!(((a.v) == (2))); assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut assigns_0)) == (6))); - let mut h: Holder = Holder { - p: { - let mut __v: Partial = ZeroInitializationUnsafe::zero_init(); - Partial::constructor_1(&raw mut __v, 4, 40); - __v - }, - arr: [ - { - let mut __v: Partial = ZeroInitializationUnsafe::zero_init(); - Partial::constructor_1(&raw mut __v, 5, 50); - __v - }, - { - let mut __v: Partial = ZeroInitializationUnsafe::zero_init(); - Partial::constructor_1(&raw mut __v, 6, 60); - __v - }, - ], - }; + object_unsafe!(h, Holder, |h| { + Partial::constructor_1(&raw mut (*h).p, 4, 40); + { + Partial::constructor_1(&raw mut (*&raw mut (*h).arr)[0], 5, 50); + Partial::constructor_1(&raw mut (*&raw mut (*h).arr)[1], 6, 60); + }; + }); assign_unsafe!(h.p = b); assign_unsafe!(h.arr[(1) as usize] = c); assert!(((h.p.v) == (2)) && ((h.p.keep) == (40))); @@ -451,25 +439,13 @@ unsafe fn main_0() -> i32 { object_unsafe!(r1, RefQualified::constructor_1()); assign_unsafe!(r1 = r); assert!(((r1.mark) == (1))); - let mut h2: Holder = Holder { - p: { - let mut __v: Partial = ZeroInitializationUnsafe::zero_init(); - Partial::constructor_1(&raw mut __v, 7, 70); - __v - }, - arr: [ - { - let mut __v: Partial = ZeroInitializationUnsafe::zero_init(); - Partial::constructor_1(&raw mut __v, 8, 80); - __v - }, - { - let mut __v: Partial = ZeroInitializationUnsafe::zero_init(); - Partial::constructor_1(&raw mut __v, 9, 90); - __v - }, - ], - }; + object_unsafe!(h2, Holder, |h2| { + Partial::constructor_1(&raw mut (*h2).p, 7, 70); + { + Partial::constructor_1(&raw mut (*&raw mut (*h2).arr)[0], 8, 80); + Partial::constructor_1(&raw mut (*&raw mut (*h2).arr)[1], 9, 90); + }; + }); assign_unsafe!(h2 = h); assert!(((h2.p.v) == (2)) && ((h2.p.keep) == (70))); assert!(((h2.arr[(1) as usize].v) == (2)) && ((h2.arr[(1) as usize].keep) == (90))); @@ -478,44 +454,20 @@ unsafe fn main_0() -> i32 { object_unsafe!(cc2, CopyCounted::constructor_1(2)); assign_unsafe!(cc1 = cc2); assert!(((cc1.v) == (2))); - let mut hc1: HasCopyCounted = HasCopyCounted { - c: { - let mut __v: CopyCounted = ZeroInitializationUnsafe::zero_init(); - CopyCounted::constructor_1(&raw mut __v, 1); - __v - }, - arr: [ - { - let mut __v: CopyCounted = ZeroInitializationUnsafe::zero_init(); - CopyCounted::constructor_1(&raw mut __v, 2); - __v - }, - { - let mut __v: CopyCounted = ZeroInitializationUnsafe::zero_init(); - CopyCounted::constructor_1(&raw mut __v, 3); - __v - }, - ], - }; - let mut hc2: HasCopyCounted = HasCopyCounted { - c: { - let mut __v: CopyCounted = ZeroInitializationUnsafe::zero_init(); - CopyCounted::constructor_1(&raw mut __v, 4); - __v - }, - arr: [ - { - let mut __v: CopyCounted = ZeroInitializationUnsafe::zero_init(); - CopyCounted::constructor_1(&raw mut __v, 5); - __v - }, - { - let mut __v: CopyCounted = ZeroInitializationUnsafe::zero_init(); - CopyCounted::constructor_1(&raw mut __v, 6); - __v - }, - ], - }; + object_unsafe!(hc1, HasCopyCounted, |hc1| { + CopyCounted::constructor_1(&raw mut (*hc1).c, 1); + { + CopyCounted::constructor_1(&raw mut (*&raw mut (*hc1).arr)[0], 2); + CopyCounted::constructor_1(&raw mut (*&raw mut (*hc1).arr)[1], 3); + }; + }); + object_unsafe!(hc2, HasCopyCounted, |hc2| { + CopyCounted::constructor_1(&raw mut (*hc2).c, 4); + { + CopyCounted::constructor_1(&raw mut (*&raw mut (*hc2).arr)[0], 5); + CopyCounted::constructor_1(&raw mut (*&raw mut (*hc2).arr)[1], 6); + }; + }); assign_unsafe!(hc1 = hc2); assert!( (((hc1.c.v) == (4)) && ((hc1.arr[(0) as usize].v) == (5))) @@ -543,20 +495,12 @@ unsafe fn main_0() -> i32 { ((sw1.v) == (2)) && ((*std::cell::LazyCell::force_mut(&mut *&raw mut swap_copies_2)) == (1)) ); - let mut hs1: HoldsSwapped = HoldsSwapped { - s: { - let mut __v: Swapped = ZeroInitializationUnsafe::zero_init(); - Swapped::constructor_1(&raw mut __v, 3); - __v - }, - }; - let mut hs2: HoldsSwapped = HoldsSwapped { - s: { - let mut __v: Swapped = ZeroInitializationUnsafe::zero_init(); - Swapped::constructor_1(&raw mut __v, 4); - __v - }, - }; + object_unsafe!(hs1, HoldsSwapped, |hs1| { + Swapped::constructor_1(&raw mut (*hs1).s, 3); + }); + object_unsafe!(hs2, HoldsSwapped, |hs2| { + Swapped::constructor_1(&raw mut (*hs2).s, 4); + }); assign_unsafe!(hs1 = hs2); assert!( ((hs1.s.v) == (4)) diff --git a/tests/unit/out/unsafe/copy_ctor.rs b/tests/unit/out/unsafe/copy_ctor.rs index e3052cb205..22a64a23d1 100644 --- a/tests/unit/out/unsafe/copy_ctor.rs +++ b/tests/unit/out/unsafe/copy_ctor.rs @@ -342,25 +342,13 @@ unsafe fn main_0() -> i32 { object_unsafe!(h, Counted::constructor_2(LvalueRefUnsafe::bind(&g))); assert!(((h.v) == (7))); assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut copies_0)) == (6))); - let mut hold: Holder = Holder { - c: { - let mut __v: Counted = ZeroInitializationUnsafe::zero_init(); - Counted::constructor_1(&raw mut __v, 8); - __v - }, - arr: [ - { - let mut __v: Counted = ZeroInitializationUnsafe::zero_init(); - Counted::constructor_1(&raw mut __v, 9); - __v - }, - { - let mut __v: Counted = ZeroInitializationUnsafe::zero_init(); - Counted::constructor_1(&raw mut __v, 10); - __v - }, - ], - }; + object_unsafe!(hold, Holder, |hold| { + Counted::constructor_1(&raw mut (*hold).c, 8); + { + Counted::constructor_1(&raw mut (*&raw mut (*hold).arr)[0], 9); + Counted::constructor_1(&raw mut (*&raw mut (*hold).arr)[1], 10); + }; + }); object_unsafe!(hold2, Holder, |hold2| { ::copy_constructor(hold2, LvalueRefUnsafe::bind(&hold)) }); @@ -377,13 +365,9 @@ unsafe fn main_0() -> i32 { object_unsafe!(i2, Ignored::constructor_2(LvalueRefUnsafe::bind(&i1))); assert!(((i1.v) == (1)) && ((i2.v) == (-1_i32))); assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut copies_0)) == (11))); - let mut o: Owner = Owner { - box_: { - let mut __v: Box_int_ = ZeroInitializationUnsafe::zero_init(); - Box_int_::constructor_1(&raw mut __v, 3); - __v - }, - }; + object_unsafe!(o, Owner, |o| { + Box_int_::constructor_1(&raw mut (*o).box_, 3); + }); object_unsafe!(o2, Owner, |o2| { ::move_constructor(o2, RvalueRefUnsafe::bind(&mut o)) }); diff --git a/tests/unit/out/unsafe/copy_move_defaulted.rs b/tests/unit/out/unsafe/copy_move_defaulted.rs index c7a1b84532..f45491b01f 100644 --- a/tests/unit/out/unsafe/copy_move_defaulted.rs +++ b/tests/unit/out/unsafe/copy_move_defaulted.rs @@ -640,13 +640,15 @@ unsafe fn main_0() -> i32 { assert!((((l.v) == (5)) && ((l.inner.x) == (50))) && ((l.arr[(0) as usize]) == (5))); let mut vec_: Vec = Vec::new(); vec_.push_copied(&mut b); - match ((&mut { - let mut __v: Explicit = ZeroInitializationUnsafe::zero_init(); - Explicit::constructor_1(&raw mut __v, 9); - __v - } as *mut Explicit),) { - (__tmp_0,) => vec_.push_moved(__tmp_0), + let mut __tmp_1: Explicit = ZeroInitializationUnsafe::zero_init(); + match (({ + Explicit::constructor_1(&raw mut __tmp_1, 9); + &mut __tmp_1 + } as *mut Explicit),) + { + (__tmp_0,) => vec_.push_moved(__tmp_0), + } }; assert!(((vec_[(0_usize)].v) == (1)) && ((vec_[(1_usize)].v) == (9))); object_unsafe!(m, DefaultCopyUserMove::constructor_1(7)); @@ -699,7 +701,7 @@ unsafe fn main_0() -> i32 { let mut bufs: Vec = Vec::new(); bufs.push_moved(&mut r); { - let mut __tmp_1 = { + let mut __tmp_2 = { { let mut __v: Buffer = ZeroInitializationUnsafe::zero_init(); ::move_constructor( @@ -709,7 +711,7 @@ unsafe fn main_0() -> i32 { __v } }; - bufs.push_mut(__tmp_1) + bufs.push_mut(__tmp_2) }; assert!( (((bufs[(1_usize)].n) == (3)) && ((bufs[(1_usize)].data.len()) == (3_usize))) @@ -717,18 +719,18 @@ unsafe fn main_0() -> i32 { ); object_unsafe!(o1, Owner::constructor_1()); { - let mut __tmp_2: i32 = 5; - o1.data.push_moved(&mut __tmp_2) + let mut __tmp_3: i32 = 5; + o1.data.push_moved(&mut __tmp_3) }; o1.n = 5; o1.arr[(0) as usize] = 5; o1.arr[(1) as usize] = 6; { - let mut __tmp_3 = { (Box::leak(Box::new(7)) as *mut i32) }; - o1.p = if __tmp_3.is_null() { + let mut __tmp_4 = { (Box::leak(Box::new(7)) as *mut i32) }; + o1.p = if __tmp_4.is_null() { None } else { - Some(Box::from_raw(__tmp_3)) + Some(Box::from_raw(__tmp_4)) } }; object_unsafe!(o2, Owner, |o2| { @@ -782,11 +784,11 @@ unsafe fn main_0() -> i32 { assert!(((c4.data[(1_usize)]) == (2)) && (c3.data.is_empty())); object_unsafe!(h1, Holder::constructor_1(4)); { - let mut __tmp_4 = { (Box::leak(Box::new(9)) as *mut i32) }; - h1.p = if __tmp_4.is_null() { + let mut __tmp_5 = { (Box::leak(Box::new(9)) as *mut i32) }; + h1.p = if __tmp_5.is_null() { None } else { - Some(Box::from_raw(__tmp_4)) + Some(Box::from_raw(__tmp_5)) } }; object_unsafe!(h2, Holder, |h2| { @@ -893,8 +895,8 @@ unsafe fn main_0() -> i32 { object_unsafe!(a3, OutOfLineAssign::constructor_1()); a1.v = 3; { - let mut __tmp_5: i32 = 4; - a1.w.push_moved(&mut __tmp_5) + let mut __tmp_6: i32 = 4; + a1.w.push_moved(&mut __tmp_6) }; assign_unsafe!(a2 = a1); assert!((((a2.v) == (3)) && ((a2.w.len()) == (1_usize))) && ((a1.w.len()) == (1_usize))); diff --git a/tests/unit/out/unsafe/copy_move_deleted.rs b/tests/unit/out/unsafe/copy_move_deleted.rs index 3824cb5879..33afdde593 100644 --- a/tests/unit/out/unsafe/copy_move_deleted.rs +++ b/tests/unit/out/unsafe/copy_move_deleted.rs @@ -169,14 +169,10 @@ unsafe fn main_0() -> i32 { (unsafe { bump_ref_1(&mut im) }); let mut pim: *mut Immovable = (&mut im as *mut Immovable); assert!((((*pim).v) == (5))); - let mut c: Container = Container { - inner: { - let mut __v: NoCopy = ZeroInitializationUnsafe::zero_init(); - NoCopy::constructor_1(&raw mut __v, 6); - __v - }, - tag: 7, - }; + object_unsafe!(c, Container, |c| { + NoCopy::constructor_1(&raw mut (*c).inner, 6); + (&raw mut (*c).tag).write(7); + }); object_unsafe!(d, Container, |d| { ::move_constructor(d, RvalueRefUnsafe::bind(&mut c)) }); diff --git a/tests/unit/out/unsafe/default_function_arguments.rs b/tests/unit/out/unsafe/default_function_arguments.rs index 23bb7e5c10..5d3af79be5 100644 --- a/tests/unit/out/unsafe/default_function_arguments.rs +++ b/tests/unit/out/unsafe/default_function_arguments.rs @@ -132,23 +132,12 @@ unsafe fn main_0() -> i32 { .v) == (2)) ) }; - let mut arr: [Bar; 3] = [ - { - let mut __v: Bar = ZeroInitializationUnsafe::zero_init(); - Bar::constructor_1(&raw mut __v, 1); - __v - }, - { - let mut __v: Bar = ZeroInitializationUnsafe::zero_init(); - Bar::constructor_1(&raw mut __v, 1); - __v - }, - { - let mut __v: Bar = ZeroInitializationUnsafe::zero_init(); - Bar::constructor_1(&raw mut __v, 1); - __v - }, - ]; + let mut arr: [Bar; 3] = ZeroInitializationUnsafe::zero_init(); + { + for __tmp_1 in 0..3 { + Bar::constructor_1(&raw mut (*&raw mut arr)[__tmp_1], 1); + } + }; assert!(((arr[(0) as usize].v) == (1))); assert!(((arr[(2) as usize].v) == (1))); assert!(((unsafe { lazy_4(5,) }) == (5))); diff --git a/tests/unit/out/unsafe/destructor_temporaries.rs b/tests/unit/out/unsafe/destructor_temporaries.rs index c49a623c5c..e8011983e4 100644 --- a/tests/unit/out/unsafe/destructor_temporaries.rs +++ b/tests/unit/out/unsafe/destructor_temporaries.rs @@ -165,25 +165,27 @@ unsafe fn main_0() -> i32 { })); assert!(((x) == (3)) && ((*std::cell::LazyCell::force_mut(&mut *&raw mut destroyed_0)) == (2))); (*std::cell::LazyCell::force_mut(&mut *&raw mut destroyed_0)) = 0; - x = (if ((x) == (3)) { - { - let mut __v: S = ZeroInitializationUnsafe::zero_init(); - (unsafe { make_2(&raw mut __v, 9) }); - __v - } - } else { - S { v: 10 } - }) - .v; + { + let mut __tmp_3: S = ZeroInitializationUnsafe::zero_init(); + x = (*{ + if ((x) == (3)) { + (unsafe { make_2(&raw mut __tmp_3, 9) }); + } else { + (&raw mut __tmp_3).write(S { v: 10 }); + }; + &mut __tmp_3 + }) + .v + }; assert!( (((x) == (9)) && ((*std::cell::LazyCell::force_mut(&mut *&raw mut destroyed_0)) == (1))) && ((*std::cell::LazyCell::force_mut(&mut *&raw mut last_1)) == (9)) ); (*std::cell::LazyCell::force_mut(&mut *&raw mut destroyed_0)) = 0; { - let mut __tmp_3: S = ZeroInitializationUnsafe::zero_init(); - (unsafe { make_2(&raw mut __tmp_3, 11) }); - let r: *const S = &mut __tmp_3; + let mut __tmp_4: S = ZeroInitializationUnsafe::zero_init(); + (unsafe { make_2(&raw mut __tmp_4, 11) }); + let r: *const S = &mut __tmp_4; assert!( ((*std::cell::LazyCell::force_mut(&mut *&raw mut destroyed_0)) == (0)) && (((*r).v) == (11)) diff --git a/tests/unit/out/unsafe/lambda_capture_copy_ctor.rs b/tests/unit/out/unsafe/lambda_capture_copy_ctor.rs index a3d0d20e44..8f7ba975a1 100644 --- a/tests/unit/out/unsafe/lambda_capture_copy_ctor.rs +++ b/tests/unit/out/unsafe/lambda_capture_copy_ctor.rs @@ -115,18 +115,22 @@ unsafe fn main_0() -> i32 { .copies }; assert!(((returned) == (2))); - let mut arr: [Counted; 2] = std::array::from_fn::<_, 2, _>(|_| { - let mut __v: Counted = ZeroInitializationUnsafe::zero_init(); - Counted::constructor_1(&raw mut __v); - __v - }); + let mut arr: [Counted; 2] = ZeroInitializationUnsafe::zero_init(); + for __tmp_1 in 0..2 { + Counted::constructor_1(&raw mut (*&raw mut arr)[__tmp_1]); + } let mut a: FnPtr i32> = lambda_unsafe!( { - let arr: [Counted; 2] = std::array::from_fn::<_, 2, _>(|__i: usize| { - let mut __v: Counted = ZeroInitializationUnsafe::zero_init(); - Counted::constructor_2(&raw mut __v, LvalueRefUnsafe::bind(&arr[(__i)])); + let arr: [Counted; 2] = { + let mut __v: [Counted; 2] = ZeroInitializationUnsafe::zero_init(); + for __tmp_2 in 0..2 { + Counted::constructor_2( + &raw mut (*&raw mut __v)[__tmp_2], + LvalueRefUnsafe::bind(&arr[(__tmp_2)]), + ); + } __v - }); + }; }, || -> i32 { return ((arr[(0) as usize].copies) + (arr[(1) as usize].copies)); diff --git a/tests/unit/out/unsafe/sort_no_copy.rs b/tests/unit/out/unsafe/sort_no_copy.rs index c58ce47980..54256bdf16 100644 --- a/tests/unit/out/unsafe/sort_no_copy.rs +++ b/tests/unit/out/unsafe/sort_no_copy.rs @@ -129,33 +129,14 @@ pub fn main() { } } unsafe fn main_0() -> i32 { - let mut arr: [Tracked; 5] = [ - { - let mut __v: Tracked = ZeroInitializationUnsafe::zero_init(); - Tracked::constructor_2(&raw mut __v, 3); - __v - }, - { - let mut __v: Tracked = ZeroInitializationUnsafe::zero_init(); - Tracked::constructor_2(&raw mut __v, 1); - __v - }, - { - let mut __v: Tracked = ZeroInitializationUnsafe::zero_init(); - Tracked::constructor_2(&raw mut __v, 4); - __v - }, - { - let mut __v: Tracked = ZeroInitializationUnsafe::zero_init(); - Tracked::constructor_2(&raw mut __v, 1); - __v - }, - { - let mut __v: Tracked = ZeroInitializationUnsafe::zero_init(); - Tracked::constructor_2(&raw mut __v, 5); - __v - }, - ]; + let mut arr: [Tracked; 5] = ZeroInitializationUnsafe::zero_init(); + { + Tracked::constructor_2(&raw mut (*&raw mut arr)[0], 3); + Tracked::constructor_2(&raw mut (*&raw mut arr)[1], 1); + Tracked::constructor_2(&raw mut (*&raw mut arr)[2], 4); + Tracked::constructor_2(&raw mut (*&raw mut arr)[3], 1); + Tracked::constructor_2(&raw mut (*&raw mut arr)[4], 5); + }; { let mut __tmp_0 = { arr.as_mut_ptr() }; let __n = arr @@ -341,11 +322,10 @@ unsafe fn main_0() -> i32 { && ((vec_[(3_usize)].v) == (2)) ); assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut copies_0)) == (0))); - let mut bags: [Bag; 3] = std::array::from_fn::<_, 3, _>(|_| { - let mut __v: Bag = ZeroInitializationUnsafe::zero_init(); - Bag::constructor_1(&raw mut __v); - __v - }); + let mut bags: [Bag; 3] = ZeroInitializationUnsafe::zero_init(); + for __tmp_5 in 0..3 { + Bag::constructor_1(&raw mut (*&raw mut bags)[__tmp_5]); + } bags[(0) as usize].items.push_mut({ let mut __v: Tracked = ZeroInitializationUnsafe::zero_init(); Tracked::constructor_2(&raw mut __v, 1); @@ -362,15 +342,15 @@ unsafe fn main_0() -> i32 { __v }); { - let mut __tmp_5 = { bags.as_mut_ptr() }; + let mut __tmp_6 = { bags.as_mut_ptr() }; let __n = bags .as_mut_ptr() .offset((3) as isize) - .iter_distance(&__tmp_5); + .iter_distance(&__tmp_6); let mut __order: Vec = (0..__n).collect(); __order.sort_by(|&__x, &__y| { - let __a = __tmp_5.iter_at(__x) as *const _; - let __b = __tmp_5.iter_at(__y) as *const _; + let __a = __tmp_6.iter_at(__x) as *const _; + let __b = __tmp_6.iter_at(__y) as *const _; if LessThanComparableUnsafe::less(__a, __b) { std::cmp::Ordering::Less } else if LessThanComparableUnsafe::less(__b, __a) { @@ -381,10 +361,10 @@ unsafe fn main_0() -> i32 { }); let __values: Vec<_> = __order .iter() - .map(|&__i| std::ptr::read(__tmp_5.iter_at(__i))) + .map(|&__i| std::ptr::read(__tmp_6.iter_at(__i))) .collect(); for (__i, __v) in (0..__n).zip(__values) { - std::ptr::write(__tmp_5.iter_at(__i), __v); + std::ptr::write(__tmp_6.iter_at(__i), __v); } }; assert!( @@ -409,12 +389,12 @@ unsafe fn main_0() -> i32 { __v }); { - let mut __tmp_6 = { bag_vec.begin() }; - let __n = bag_vec.end().iter_distance(&__tmp_6); + let mut __tmp_7 = { bag_vec.begin() }; + let __n = bag_vec.end().iter_distance(&__tmp_7); let mut __order: Vec = (0..__n).collect(); __order.sort_by(|&__x, &__y| { - let __a = __tmp_6.iter_at(__x) as *const _; - let __b = __tmp_6.iter_at(__y) as *const _; + let __a = __tmp_7.iter_at(__x) as *const _; + let __b = __tmp_7.iter_at(__y) as *const _; if LessThanComparableUnsafe::less(__a, __b) { std::cmp::Ordering::Less } else if LessThanComparableUnsafe::less(__b, __a) { @@ -425,10 +405,10 @@ unsafe fn main_0() -> i32 { }); let __values: Vec<_> = __order .iter() - .map(|&__i| std::ptr::read(__tmp_6.iter_at(__i))) + .map(|&__i| std::ptr::read(__tmp_7.iter_at(__i))) .collect(); for (__i, __v) in (0..__n).zip(__values) { - std::ptr::write(__tmp_6.iter_at(__i), __v); + std::ptr::write(__tmp_7.iter_at(__i), __v); } }; assert!( @@ -438,10 +418,10 @@ unsafe fn main_0() -> i32 { assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut copies_0)) == (0))); assert!(WeaklyEqualityComparableWithUnsafe::equal( &{ - let mut __tmp_7 = { bag_vec.begin() }; - let mut __tmp_8 = { bag_vec.end() }; - let mut __it = (__tmp_7).clone(); - while WeaklyEqualityComparableWithUnsafe::not_equal(&__it, &__tmp_8) + let mut __tmp_8 = { bag_vec.begin() }; + let mut __tmp_9 = { bag_vec.end() }; + let mut __it = (__tmp_8).clone(); + while WeaklyEqualityComparableWithUnsafe::not_equal(&__it, &__tmp_9) && !WeaklyEqualityComparableWithUnsafe::equal( &*__it.iter_deref(), &bags[(1) as usize], @@ -455,13 +435,13 @@ unsafe fn main_0() -> i32 { )); assert!(WeaklyEqualityComparableWithUnsafe::equal( &{ - let mut __tmp_9 = { bag_vec.begin() }; - let mut __tmp_10 = { bag_vec.end() }; - let mut __best = __tmp_9.clone(); - if WeaklyEqualityComparableWithUnsafe::not_equal(&__tmp_9, &__tmp_10) { - let mut __it = (__tmp_9).clone(); + let mut __tmp_10 = { bag_vec.begin() }; + let mut __tmp_11 = { bag_vec.end() }; + let mut __best = __tmp_10.clone(); + if WeaklyEqualityComparableWithUnsafe::not_equal(&__tmp_10, &__tmp_11) { + let mut __it = (__tmp_10).clone(); __it.iter_inc(); - while WeaklyEqualityComparableWithUnsafe::not_equal(&__it, &__tmp_10) { + while WeaklyEqualityComparableWithUnsafe::not_equal(&__it, &__tmp_11) { if LessThanComparableUnsafe::less( __best.iter_deref() as *const _, __it.iter_deref() as *const _, @@ -532,12 +512,12 @@ unsafe fn main_0() -> i32 { __v }); { - let mut __tmp_11 = { moves.begin() }; - let __n = moves.end().iter_distance(&__tmp_11); + let mut __tmp_12 = { moves.begin() }; + let __n = moves.end().iter_distance(&__tmp_12); let mut __order: Vec = (0..__n).collect(); __order.sort_by(|&__x, &__y| { - let __a = __tmp_11.iter_at(__x) as *const _; - let __b = __tmp_11.iter_at(__y) as *const _; + let __a = __tmp_12.iter_at(__x) as *const _; + let __b = __tmp_12.iter_at(__y) as *const _; if LessThanComparableUnsafe::less(__a, __b) { std::cmp::Ordering::Less } else if LessThanComparableUnsafe::less(__b, __a) { @@ -548,10 +528,10 @@ unsafe fn main_0() -> i32 { }); let __values: Vec<_> = __order .iter() - .map(|&__i| std::ptr::read(__tmp_11.iter_at(__i))) + .map(|&__i| std::ptr::read(__tmp_12.iter_at(__i))) .collect(); for (__i, __v) in (0..__n).zip(__values) { - std::ptr::write(__tmp_11.iter_at(__i), __v); + std::ptr::write(__tmp_12.iter_at(__i), __v); } }; assert!( @@ -559,12 +539,12 @@ unsafe fn main_0() -> i32 { && ((moves[(2_usize)].v) == (3)) ); { - let mut __tmp_12 = { moves.begin() }; - let __n = moves.end().iter_distance(&__tmp_12); + let mut __tmp_13 = { moves.begin() }; + let __n = moves.end().iter_distance(&__tmp_13); let mut __order: Vec = (0..__n).collect(); __order.sort_by(|&__x, &__y| { - let __a = __tmp_12.iter_at(__x) as *const _; - let __b = __tmp_12.iter_at(__y) as *const _; + let __a = __tmp_13.iter_at(__x) as *const _; + let __b = __tmp_13.iter_at(__y) as *const _; if FnPtr:: bool>::new( |a: *const MoveOnly, b: *const MoveOnly| -> bool { unsafe { @@ -591,10 +571,10 @@ unsafe fn main_0() -> i32 { }); let __values: Vec<_> = __order .iter() - .map(|&__i| std::ptr::read(__tmp_12.iter_at(__i))) + .map(|&__i| std::ptr::read(__tmp_13.iter_at(__i))) .collect(); for (__i, __v) in (0..__n).zip(__values) { - std::ptr::write(__tmp_12.iter_at(__i), __v); + std::ptr::write(__tmp_13.iter_at(__i), __v); } }; assert!( diff --git a/tests/unit/out/unsafe/struct_default_ctor.rs b/tests/unit/out/unsafe/struct_default_ctor.rs index d6b88c62e6..eaf3dd92d2 100644 --- a/tests/unit/out/unsafe/struct_default_ctor.rs +++ b/tests/unit/out/unsafe/struct_default_ctor.rs @@ -73,11 +73,9 @@ pub struct Holder { } impl Holder { pub unsafe fn constructor_1(this: *mut Self) { - (&raw mut (*this).items).write(std::array::from_fn::<_, 2, _>(|_| { - let mut __v: S = ZeroInitializationUnsafe::zero_init(); - S::constructor_1(&raw mut __v); - __v - })); + for __tmp_0 in 0..2 { + S::constructor_1(&raw mut (*&raw mut (*this).items)[__tmp_0]); + } } } pub static mut kInit_0: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 3 }); diff --git a/tests/unit/out/unsafe/structured_binding_copy_ctor.rs b/tests/unit/out/unsafe/structured_binding_copy_ctor.rs index afb7188eb3..43d3ab33f3 100644 --- a/tests/unit/out/unsafe/structured_binding_copy_ctor.rs +++ b/tests/unit/out/unsafe/structured_binding_copy_ctor.rs @@ -51,11 +51,10 @@ pub fn main() { } unsafe fn main_0() -> i32 { object_unsafe!(s, Counted::constructor_1(1, 2)); - let mut __decomp_2: Counted = { - let mut __v: Counted = ZeroInitializationUnsafe::zero_init(); - Counted::constructor_2(&raw mut __v, LvalueRefUnsafe::bind(&s)); - __v - }; + object_unsafe!( + __decomp_2, + Counted::constructor_2(LvalueRefUnsafe::bind(&s)) + ); assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut copies_0)) == (1))); assert!(((__decomp_2.x) == (2))); assert!(((__decomp_2.y) == (4))); @@ -66,11 +65,9 @@ unsafe fn main_0() -> i32 { let __decomp_4: *const Counted = &s; assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut copies_0)) == (1))); assert!((((*__decomp_4).x) == (1))); - let mut __decomp_5: Counted = { - let mut __v: Counted = ZeroInitializationUnsafe::zero_init(); - (unsafe { make_counted_1(&raw mut __v) }); - __v - }; + object_unsafe!(__decomp_5, Counted, |__decomp_5| (unsafe { + make_counted_1(__decomp_5) + })); assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut copies_0)) == (1))); assert!(((__decomp_5.x) == (5))); assert!(((__decomp_5.y) == (6))); diff --git a/tests/unit/out/unsafe/structured_binding_element_ctor.rs b/tests/unit/out/unsafe/structured_binding_element_ctor.rs index 9c88a0824e..1a1a5eebaa 100644 --- a/tests/unit/out/unsafe/structured_binding_element_ctor.rs +++ b/tests/unit/out/unsafe/structured_binding_element_ctor.rs @@ -48,18 +48,10 @@ pub struct Two { } impl Two {} pub unsafe fn make_two_2(__return: *mut Two) { - __return.write(Two { - a: { - let mut __v: Elem = ZeroInitializationUnsafe::zero_init(); - Elem::constructor_1(&raw mut __v, 1); - __v - }, - b: { - let mut __v: Elem = ZeroInitializationUnsafe::zero_init(); - Elem::constructor_1(&raw mut __v, 2); - __v - }, - }); + { + Elem::constructor_1(&raw mut (*__return).a, 1); + Elem::constructor_1(&raw mut (*__return).b, 2); + }; return; } pub fn main() { @@ -69,25 +61,15 @@ pub fn main() { } } unsafe fn main_0() -> i32 { - let mut t: Two = Two { - a: { - let mut __v: Elem = ZeroInitializationUnsafe::zero_init(); - Elem::constructor_1(&raw mut __v, 1); - __v - }, - b: { - let mut __v: Elem = ZeroInitializationUnsafe::zero_init(); - Elem::constructor_1(&raw mut __v, 2); - __v - }, - }; + object_unsafe!(t, Two, |t| { + Elem::constructor_1(&raw mut (*t).a, 1); + Elem::constructor_1(&raw mut (*t).b, 2); + }); let mut base_copies: i32 = (*std::cell::LazyCell::force_mut(&mut *&raw mut copies_0)); let mut base_moves: i32 = (*std::cell::LazyCell::force_mut(&mut *&raw mut moves_1)); - let mut __decomp_3: Two = { - let mut __v: Two = ZeroInitializationUnsafe::zero_init(); - ::copy_constructor(&raw mut __v, LvalueRefUnsafe::bind(&t)); - __v - }; + object_unsafe!(__decomp_3, Two, |__decomp_3| { + ::copy_constructor(__decomp_3, LvalueRefUnsafe::bind(&t)) + }); assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut copies_0)) == ((base_copies) + (2)))); assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut moves_1)) == (base_moves))); assert!(((__decomp_3.a.v) == (101))); @@ -97,46 +79,42 @@ unsafe fn main_0() -> i32 { assert!((((*__decomp_4).a.v) == (1))); (*__decomp_4).a.v = 50; assert!(((t.a.v) == (50))); - let mut __decomp_5: Two = { - let mut __v: Two = ZeroInitializationUnsafe::zero_init(); + object_unsafe!(__decomp_5, Two, |__decomp_5| { ::move_constructor( - &raw mut __v, + __decomp_5, RvalueRefUnsafe::bind(&mut t), - ); - __v - }; + ) + }); assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut moves_1)) == ((base_moves) + (2)))); assert!(((__decomp_5.a.v) == (1050))); assert!(((__decomp_5.b.v) == (1002))); assert!(((t.a.v) == (-1_i32))); let mut before_copies: i32 = (*std::cell::LazyCell::force_mut(&mut *&raw mut copies_0)); let mut before_moves: i32 = (*std::cell::LazyCell::force_mut(&mut *&raw mut moves_1)); - let mut __decomp_6: Two = { - let mut __v: Two = ZeroInitializationUnsafe::zero_init(); - (unsafe { make_two_2(&raw mut __v) }); - __v - }; + object_unsafe!(__decomp_6, Two, |__decomp_6| (unsafe { + make_two_2(__decomp_6) + })); assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut copies_0)) == (before_copies))); assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut moves_1)) == (before_moves))); assert!(((__decomp_6.a.v) == (1))); let mut p: (Elem, Elem) = ( { - let mut __tmp_0: Elem = ZeroInitializationUnsafe::zero_init(); - Elem::constructor_1(&raw mut __tmp_0, 7); + let mut __v: Elem = ZeroInitializationUnsafe::zero_init(); { - let mut __v: Elem = ZeroInitializationUnsafe::zero_init(); - Elem::constructor_3(&raw mut __v, RvalueRefUnsafe::bind(&mut __tmp_0)); - __v - } + let mut __tmp_0: Elem = ZeroInitializationUnsafe::zero_init(); + Elem::constructor_1(&raw mut __tmp_0, 7); + Elem::constructor_3(&raw mut __v, RvalueRefUnsafe::bind(&mut __tmp_0)) + }; + __v }, { - let mut __tmp_1: Elem = ZeroInitializationUnsafe::zero_init(); - Elem::constructor_1(&raw mut __tmp_1, 8); + let mut __v: Elem = ZeroInitializationUnsafe::zero_init(); { - let mut __v: Elem = ZeroInitializationUnsafe::zero_init(); - Elem::constructor_3(&raw mut __v, RvalueRefUnsafe::bind(&mut __tmp_1)); - __v - } + let mut __tmp_1: Elem = ZeroInitializationUnsafe::zero_init(); + Elem::constructor_1(&raw mut __tmp_1, 8); + Elem::constructor_3(&raw mut __v, RvalueRefUnsafe::bind(&mut __tmp_1)) + }; + __v }, ); before_copies = (*std::cell::LazyCell::force_mut(&mut *&raw mut copies_0)); diff --git a/tests/unit/out/unsafe/structured_binding_move_ctor.rs b/tests/unit/out/unsafe/structured_binding_move_ctor.rs index 5a2cc0cf9f..15f1c69081 100644 --- a/tests/unit/out/unsafe/structured_binding_move_ctor.rs +++ b/tests/unit/out/unsafe/structured_binding_move_ctor.rs @@ -81,11 +81,10 @@ pub fn main() { } unsafe fn main_0() -> i32 { object_unsafe!(m, Movable::constructor_1(3, 4)); - let mut __decomp_2: Movable = { - let mut __v: Movable = ZeroInitializationUnsafe::zero_init(); - Movable::constructor_3(&raw mut __v, RvalueRefUnsafe::bind(&mut m)); - __v - }; + object_unsafe!( + __decomp_2, + Movable::constructor_3(RvalueRefUnsafe::bind(&mut m)) + ); assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut moves_1)) == (1))); assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut copies_0)) == (0))); assert!(((__decomp_2.x) == (3))); @@ -96,11 +95,9 @@ unsafe fn main_0() -> i32 { xs: Vec::from([1, 2, 3]), ys: Vec::from([4, 5]), }; - let mut __decomp_3: Holder = { - let mut __v: Holder = ZeroInitializationUnsafe::zero_init(); - (unsafe { Holder::extract(&raw mut h, &raw mut __v) }); - __v - }; + object_unsafe!(__decomp_3, Holder, |__decomp_3| (unsafe { + Holder::extract(&raw mut h, __decomp_3) + })); assert!(((__decomp_3.xs.len()) == (3_usize))); assert!(((__decomp_3.ys.len()) == (2_usize))); assert!(((__decomp_3.xs[(2_usize)]) == (3))); diff --git a/tests/unit/out/unsafe/swap_map_no_copy.rs b/tests/unit/out/unsafe/swap_map_no_copy.rs index 37acfaaf99..c4a09a739e 100644 --- a/tests/unit/out/unsafe/swap_map_no_copy.rs +++ b/tests/unit/out/unsafe/swap_map_no_copy.rs @@ -104,16 +104,12 @@ pub struct Arrays { } impl Arrays { pub unsafe fn constructor_1(this: *mut Self) { - (&raw mut (*this).a).write(std::array::from_fn::<_, 2, _>(|_| { - let mut __v: Tracked = ZeroInitializationUnsafe::zero_init(); - Tracked::constructor_1(&raw mut __v); - __v - })); - (&raw mut (*this).b).write(std::array::from_fn::<_, 2, _>(|_| { - let mut __v: Tracked = ZeroInitializationUnsafe::zero_init(); - Tracked::constructor_1(&raw mut __v); - __v - })); + for __tmp_0 in 0..2 { + Tracked::constructor_1(&raw mut (*&raw mut (*this).a)[__tmp_0]); + } + for __tmp_1 in 0..2 { + Tracked::constructor_1(&raw mut (*&raw mut (*this).b)[__tmp_1]); + } } } pub static mut copy_only_copies_1: std::cell::LazyCell = @@ -303,15 +299,15 @@ unsafe fn main_0() -> i32 { assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut copies_0)) == (0))); let mut m1: UnsafeMap = UnsafeMap::new(); { - let mut __tmp_0: i32; + let mut __tmp_2: i32; (unsafe { let mut _o: Tracked = ZeroInitializationUnsafe::zero_init(); Tracked::constructor_2(&raw mut _o, 10); assign_unsafe!( (*{ - __tmp_0 = 1; + __tmp_2 = 1; &raw mut (*m1.subscript_move( - &mut __tmp_0, + &mut __tmp_2, |__k| cpp_move_unsafe!(__k), || ::default(), )) @@ -320,15 +316,15 @@ unsafe fn main_0() -> i32 { }) }; { - let mut __tmp_1: i32; + let mut __tmp_3: i32; (unsafe { let mut _o: Tracked = ZeroInitializationUnsafe::zero_init(); Tracked::constructor_2(&raw mut _o, 20); assign_unsafe!( (*{ - __tmp_1 = 2; + __tmp_3 = 2; &raw mut (*m1.subscript_move( - &mut __tmp_1, + &mut __tmp_3, |__k| cpp_move_unsafe!(__k), || ::default(), )) @@ -338,15 +334,15 @@ unsafe fn main_0() -> i32 { }; let mut m2: UnsafeMap = UnsafeMap::new(); { - let mut __tmp_2: i32; + let mut __tmp_4: i32; (unsafe { let mut _o: Tracked = ZeroInitializationUnsafe::zero_init(); Tracked::constructor_2(&raw mut _o, 30); assign_unsafe!( (*{ - __tmp_2 = 3; + __tmp_4 = 3; &raw mut (*m2.subscript_move( - &mut __tmp_2, + &mut __tmp_4, |__k| cpp_move_unsafe!(__k), || ::default(), )) @@ -356,21 +352,21 @@ unsafe fn main_0() -> i32 { }; assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut copies_0)) == (0))); { - let mut __tmp_3: i32; - let mut __tmp_4: i32; + let mut __tmp_5: i32; + let mut __tmp_6: i32; { let __a: *mut _ = { - __tmp_3 = 1; + __tmp_5 = 1; &mut (*m1.subscript_move( - &mut __tmp_3, + &mut __tmp_5, |__k| cpp_move_unsafe!(__k), || ::default(), )) }; let __b: *mut _ = { - __tmp_4 = 2; + __tmp_6 = 2; &mut (*m1.subscript_move( - &mut __tmp_4, + &mut __tmp_6, |__k| cpp_move_unsafe!(__k), || ::default(), )) @@ -380,22 +376,22 @@ unsafe fn main_0() -> i32 { }; assert!( ({ - let mut __tmp_5: i32; + let mut __tmp_7: i32; (({ - __tmp_5 = 1; + __tmp_7 = 1; (*m1.subscript_move( - &mut __tmp_5, + &mut __tmp_7, |__k| cpp_move_unsafe!(__k), || ::default(), )) .v }) == (20)) }) && ({ - let mut __tmp_6: i32; + let mut __tmp_8: i32; (({ - __tmp_6 = 2; + __tmp_8 = 2; (*m1.subscript_move( - &mut __tmp_6, + &mut __tmp_8, |__k| cpp_move_unsafe!(__k), || ::default(), )) @@ -412,11 +408,11 @@ unsafe fn main_0() -> i32 { assert!( ((m1.len()) == (1_usize)) && ({ - let mut __tmp_7: i32; + let mut __tmp_9: i32; (({ - __tmp_7 = 3; + __tmp_9 = 3; (*m1.subscript_move( - &mut __tmp_7, + &mut __tmp_9, |__k| cpp_move_unsafe!(__k), || ::default(), )) @@ -427,11 +423,11 @@ unsafe fn main_0() -> i32 { assert!( (((m2.len()) == (2_usize)) && ({ - let mut __tmp_8: i32; + let mut __tmp_10: i32; (({ - __tmp_8 = 1; + __tmp_10 = 1; (*m2.subscript_move( - &mut __tmp_8, + &mut __tmp_10, |__k| cpp_move_unsafe!(__k), || ::default(), )) @@ -439,11 +435,11 @@ unsafe fn main_0() -> i32 { }) == (20)) })) && ({ - let mut __tmp_9: i32; + let mut __tmp_11: i32; (({ - __tmp_9 = 2; + __tmp_11 = 2; (*m2.subscript_move( - &mut __tmp_9, + &mut __tmp_11, |__k| cpp_move_unsafe!(__k), || ::default(), )) @@ -452,23 +448,12 @@ unsafe fn main_0() -> i32 { }) ); assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut copies_0)) == (0))); - let mut arr: [Tracked; 3] = [ - { - let mut __v: Tracked = ZeroInitializationUnsafe::zero_init(); - Tracked::constructor_2(&raw mut __v, 1); - __v - }, - { - let mut __v: Tracked = ZeroInitializationUnsafe::zero_init(); - Tracked::constructor_2(&raw mut __v, 2); - __v - }, - { - let mut __v: Tracked = ZeroInitializationUnsafe::zero_init(); - Tracked::constructor_2(&raw mut __v, 3); - __v - }, - ]; + let mut arr: [Tracked; 3] = ZeroInitializationUnsafe::zero_init(); + { + Tracked::constructor_2(&raw mut (*&raw mut arr)[0], 1); + Tracked::constructor_2(&raw mut (*&raw mut arr)[1], 2); + Tracked::constructor_2(&raw mut (*&raw mut arr)[2], 3); + }; { let __a: *mut _ = &mut arr[(0) as usize]; let __b: *mut _ = &mut arr[(2) as usize]; @@ -500,44 +485,21 @@ unsafe fn main_0() -> i32 { }; assert!(((vec_[(0_usize)].v) == (5)) && ((vec_[(1_usize)].v) == (4))); assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut copies_0)) == (0))); - let mut pairs: [Pair; 2] = [ - Pair { - x: { - let mut __v: Tracked = ZeroInitializationUnsafe::zero_init(); - Tracked::constructor_2(&raw mut __v, 6); - __v - }, - y: { - let mut __v: Tracked = ZeroInitializationUnsafe::zero_init(); - Tracked::constructor_2(&raw mut __v, 7); - __v - }, - }, - Pair { - x: { - let mut __v: Tracked = ZeroInitializationUnsafe::zero_init(); - Tracked::constructor_2(&raw mut __v, 8); - __v - }, - y: { - let mut __v: Tracked = ZeroInitializationUnsafe::zero_init(); - Tracked::constructor_2(&raw mut __v, 9); - __v - }, - }, - ]; - let mut p: Pair = Pair { - x: { - let mut __v: Tracked = ZeroInitializationUnsafe::zero_init(); - Tracked::constructor_2(&raw mut __v, 10); - __v - }, - y: { - let mut __v: Tracked = ZeroInitializationUnsafe::zero_init(); - Tracked::constructor_2(&raw mut __v, 11); - __v - }, + let mut pairs: [Pair; 2] = ZeroInitializationUnsafe::zero_init(); + { + { + Tracked::constructor_2(&raw mut (*&raw mut (*&raw mut pairs)[0]).x, 6); + Tracked::constructor_2(&raw mut (*&raw mut (*&raw mut pairs)[0]).y, 7); + }; + { + Tracked::constructor_2(&raw mut (*&raw mut (*&raw mut pairs)[1]).x, 8); + Tracked::constructor_2(&raw mut (*&raw mut (*&raw mut pairs)[1]).y, 9); + }; }; + object_unsafe!(p, Pair, |p| { + Tracked::constructor_2(&raw mut (*p).x, 10); + Tracked::constructor_2(&raw mut (*p).y, 11); + }); { let __a: *mut _ = &mut pairs[(0) as usize].x; let __b: *mut _ = &mut p.y; @@ -612,15 +574,15 @@ unsafe fn main_0() -> i32 { let mut maps: [UnsafeMap; 2] = std::array::from_fn::<_, 2, _>(|_| UnsafeMap::new()); { - let mut __tmp_10: i32; + let mut __tmp_12: i32; (unsafe { let mut _o: Tracked = ZeroInitializationUnsafe::zero_init(); Tracked::constructor_2(&raw mut _o, 1); assign_unsafe!( (*{ - __tmp_10 = 1; + __tmp_12 = 1; &raw mut (*maps[(0) as usize].subscript_move( - &mut __tmp_10, + &mut __tmp_12, |__k| cpp_move_unsafe!(__k), || ::default(), )) @@ -629,15 +591,15 @@ unsafe fn main_0() -> i32 { }) }; { - let mut __tmp_11: i32; + let mut __tmp_13: i32; (unsafe { let mut _o: Tracked = ZeroInitializationUnsafe::zero_init(); Tracked::constructor_2(&raw mut _o, 2); assign_unsafe!( (*{ - __tmp_11 = 2; + __tmp_13 = 2; &raw mut (*maps[(1) as usize].subscript_move( - &mut __tmp_11, + &mut __tmp_13, |__k| cpp_move_unsafe!(__k), || ::default(), )) @@ -653,11 +615,11 @@ unsafe fn main_0() -> i32 { assert!( ((maps[(0) as usize].len()) == (1_usize)) && ({ - let mut __tmp_12: i32; + let mut __tmp_14: i32; (({ - __tmp_12 = 2; + __tmp_14 = 2; (*maps[(0) as usize].subscript_move( - &mut __tmp_12, + &mut __tmp_14, |__k| cpp_move_unsafe!(__k), || ::default(), )) @@ -668,11 +630,11 @@ unsafe fn main_0() -> i32 { assert!( ((maps[(1) as usize].len()) == (1_usize)) && ({ - let mut __tmp_13: i32; + let mut __tmp_15: i32; (({ - __tmp_13 = 1; + __tmp_15 = 1; (*maps[(1) as usize].subscript_move( - &mut __tmp_13, + &mut __tmp_15, |__k| cpp_move_unsafe!(__k), || ::default(), )) @@ -693,20 +655,9 @@ unsafe fn main_0() -> i32 { ((*std::cell::LazyCell::force_mut(&mut *&raw mut copy_only_copies_1)) == (3)) && ((*std::cell::LazyCell::force_mut(&mut *&raw mut copy_only_dtors_2)) == (1)) ); - let mut cp: CopyOnlyPair = CopyOnlyPair { - x: { - let mut __v: CopyOnly = ZeroInitializationUnsafe::zero_init(); - CopyOnly::constructor_1(&raw mut __v, 3); - __v - }, - y: { - let mut __v: CopyOnly = ZeroInitializationUnsafe::zero_init(); - CopyOnly::constructor_1(&raw mut __v, 4); - __v - }, - }; - let _dtor_cp = std::mem::DropGuard::new(&raw mut cp, |__p| unsafe { - DestructibleUnsafe::destroy(__p) + object_unsafe!(cp, CopyOnlyPair, |cp| { + CopyOnly::constructor_1(&raw mut (*cp).x, 3); + CopyOnly::constructor_1(&raw mut (*cp).y, 4); }); { let __a: *mut _ = &mut cp.x; @@ -718,18 +669,11 @@ unsafe fn main_0() -> i32 { ((*std::cell::LazyCell::force_mut(&mut *&raw mut copy_only_copies_1)) == (6)) && ((*std::cell::LazyCell::force_mut(&mut *&raw mut copy_only_dtors_2)) == (2)) ); - let mut carr: [CopyOnly; 2] = [ - { - let mut __v: CopyOnly = ZeroInitializationUnsafe::zero_init(); - CopyOnly::constructor_1(&raw mut __v, 5); - __v - }, - { - let mut __v: CopyOnly = ZeroInitializationUnsafe::zero_init(); - CopyOnly::constructor_1(&raw mut __v, 6); - __v - }, - ]; + let mut carr: [CopyOnly; 2] = ZeroInitializationUnsafe::zero_init(); + { + CopyOnly::constructor_1(&raw mut (*&raw mut carr)[0], 5); + CopyOnly::constructor_1(&raw mut (*&raw mut carr)[1], 6); + }; { let __a: *mut _ = &mut carr[(0) as usize]; let __b: *mut _ = &mut carr[(1) as usize]; @@ -749,20 +693,9 @@ unsafe fn main_0() -> i32 { }; assert!(((mv1.v) == (2)) && ((mv2.v) == (1))); assert!((unsafe { swapped_once_6() })); - let mut mp: MovedPair = MovedPair { - x: { - let mut __v: Moved = ZeroInitializationUnsafe::zero_init(); - Moved::constructor_1(&raw mut __v, 3); - __v - }, - y: { - let mut __v: Moved = ZeroInitializationUnsafe::zero_init(); - Moved::constructor_1(&raw mut __v, 4); - __v - }, - }; - let _dtor_mp = std::mem::DropGuard::new(&raw mut mp, |__p| unsafe { - DestructibleUnsafe::destroy(__p) + object_unsafe!(mp, MovedPair, |mp| { + Moved::constructor_1(&raw mut (*mp).x, 3); + Moved::constructor_1(&raw mut (*mp).y, 4); }); { let __a: *mut _ = &mut mp.x; @@ -771,18 +704,11 @@ unsafe fn main_0() -> i32 { }; assert!(((mp.x.v) == (4)) && ((mp.y.v) == (3))); assert!((unsafe { swapped_once_6() })); - let mut marr: [Moved; 2] = [ - { - let mut __v: Moved = ZeroInitializationUnsafe::zero_init(); - Moved::constructor_1(&raw mut __v, 5); - __v - }, - { - let mut __v: Moved = ZeroInitializationUnsafe::zero_init(); - Moved::constructor_1(&raw mut __v, 6); - __v - }, - ]; + let mut marr: [Moved; 2] = ZeroInitializationUnsafe::zero_init(); + { + Moved::constructor_1(&raw mut (*&raw mut marr)[0], 5); + Moved::constructor_1(&raw mut (*&raw mut marr)[1], 6); + }; { let __a: *mut _ = &mut marr[(0) as usize]; let __b: *mut _ = &mut marr[(1) as usize]; diff --git a/tests/unit/out/unsafe/this.rs b/tests/unit/out/unsafe/this.rs index 471e7670b3..4cc02cbfb8 100644 --- a/tests/unit/out/unsafe/this.rs +++ b/tests/unit/out/unsafe/this.rs @@ -226,11 +226,7 @@ unsafe fn main_0() -> i32 { S::constructor_2(1, PointerUnsafe::bind((&mut s as *mut S))) ); assert!(((u.self__) == (&mut s as *mut S))); - let s_const: S = { - let mut __v: S = ZeroInitializationUnsafe::zero_init(); - S::constructor_1(&raw mut __v, 100); - __v - }; + object_unsafe!(s_const, S::constructor_1(100)); object_unsafe!( u1, S::constructor_3(1, PointerUnsafe::bind((&s_const as *const S)))