diff --git a/cpp2rust/converter/converter.cpp b/cpp2rust/converter/converter.cpp index 5a5b3be25e..9765771a79 100644 --- a/cpp2rust/converter/converter.cpp +++ b/cpp2rust/converter/converter.cpp @@ -996,6 +996,16 @@ void Converter::EmitRustStructOrUnion(clang::RecordDecl *decl) { AddDefaultTrait(decl); } +void Converter::ConvertTemplateInstantiatedCtors(clang::CXXRecordDecl *decl) { + for (auto c : GetTemplateInstantiatedCtors(decl)) { + if (!decl_ids_.contains(GetID(c))) { + StrCat(keyword::kImpl, GetRecordName(decl)); + PushBrace brace(*this); + VisitCXXMethodDecl(c); + } + } +} + void Converter::ConvertLateInstantiatedMethods(clang::CXXRecordDecl *decl) { ConvertCXXMethodDecls( decl, std::format("{} {}", keyword::kImpl, GetRecordName(decl)), @@ -1121,13 +1131,7 @@ bool Converter::VisitCXXRecordDecl(clang::CXXRecordDecl *decl) { } if (!decl->isUnion()) { - for (auto c : GetTemplateInstantiatedCtors(decl)) { - if (!decl_ids_.contains(GetID(c))) { - StrCat(keyword::kImpl, GetRecordName(decl)); - PushBrace brace(*this); - VisitCXXMethodDecl(c); - } - } + ConvertTemplateInstantiatedCtors(decl); } if (!record_decls_.MarkDefined(GetRecordName(decl))) { @@ -1261,34 +1265,39 @@ std::string Converter::GetCtorName(clang::CXXConstructorDecl *decl) { CanUseCopyOrMoveName(decl, name)) { return name; } - return std::format("new_{}", GetCtorIndex(decl)); + return std::format("construct_{}", GetCtorIndex(decl)); } return GetNumberOfConvertingCtors(decl->getParent()) != 1 - ? std::format("new_{}", GetCtorIndex(decl)) - : "new"; + ? std::format("construct_{}", GetCtorIndex(decl)) + : "construct"; } bool Converter::VisitCXXConstructorDecl(clang::CXXConstructorDecl *decl) { - if (decl->isOutOfLine() || - (decl->isImplicit() && !IsConvertibleImplicitMember(decl))) { + if (!IsConvertedConstructor(decl)) { return false; } ScopedValue push_fn(curr_function_, decl); - if (decl->isCopyOrMoveConstructor() && !decl->hasBody()) { - return false; + if (method_target_ == MethodTarget::ValueImpl) { + StrCat(AccessSpecifierAsString(decl->getAccess())); } - - StrCat(AccessSpecifierAsString(decl->getAccess()), keyword_unsafe_, - keyword::kFn, GetCtorName(decl)); + StrCat(keyword_unsafe_, keyword::kFn, GetCtorName(decl)); { PushParen paren(*this); + StrCat(GetConstructorSelf(), token::kComma); ConvertFunctionParameters(decl); } - StrCat(token::kArrow, "Self"); + if (method_target_ == MethodTarget::TraitDecl) { + StrCat(token::kSemiColon); + return false; + } { PushBrace brace(*this); - ConvertCXXConstructorBody(decl); + if (method_target_ == MethodTarget::TraitDefault) { + StrCat("unimplemented!()"); + } else { + ConvertCXXConstructorBody(decl); + } } return false; @@ -1296,20 +1305,24 @@ bool Converter::VisitCXXConstructorDecl(clang::CXXConstructorDecl *decl) { void Converter::ConvertCXXConstructorBody(clang::CXXConstructorDecl *decl) { EmitFunctionPreamble(decl); - StrCat(keyword::kLet, "mut", "this", token::kAssign); EmitConstructorInit(decl); - StrCat(token::kSemiColon); ConvertBodyStmts(decl->getBody()); - StrCat("this"); } void Converter::EmitConstructorInit(clang::CXXConstructorDecl *decl) { if (decl->isDelegatingConstructor()) { - Convert((*decl->init_begin())->getInit()); + StrCat(GetConstructorWrite()); + { + PushParen paren(*this); + Convert((*decl->init_begin())->getInit()); + } + StrCat(token::kSemiColon); return; } - StrCat("Self"); - PushBrace this_init(*this); + EmitConstructorFieldInits(decl); +} + +void Converter::EmitConstructorFieldInits(clang::CXXConstructorDecl *decl) { const auto *record_decl = decl->getParent(); auto *definition_or_null = decl->getDefinition(); assert(definition_or_null); @@ -1325,13 +1338,24 @@ void Converter::EmitConstructorInit(clang::CXXConstructorDecl *decl) { } } - StrCat(field_name, token::kColon); - ConvertFieldInit(field, ctor_initializer ? ctor_initializer->getInit() - : field->getInClassInitializer()); - StrCat(token::kComma); + std::string value; + { + Buffer buf(*this); + ConvertFieldInit(field, ctor_initializer + ? ctor_initializer->getInit() + : field->getInClassInitializer()); + value = std::move(buf).str(); + } + StrCat(ConvertConstructorFieldWrite(field_name, value)); } } +std::string +Converter::ConvertConstructorFieldWrite(const std::string &field_name, + const std::string &value) { + return std::format("(&raw mut self.{}).write({});", field_name, value); +} + bool Converter::VisitFieldDecl(clang::FieldDecl *decl) { auto access_spec = AccessSpecifierAsString(decl->getAccess()); auto field_name = GetNamedDeclAsString(decl); @@ -3949,7 +3973,7 @@ void Converter::SetUFCSReceiver(clang::Expr *base, bool is_arrow, const clang::CXXMethodDecl *method) { if (clang::isa(base->IgnoreParenImpCasts()) && !AsLambdaOperatorCall(curr_function_)) { - ufcs_receiver_ = InConstructor() ? "&mut this" : keyword::kSelfValue; + ufcs_receiver_ = keyword::kSelfValue; return; } Buffer buf(*this); @@ -4002,7 +4026,7 @@ void Converter::ConvertMemberExpr(clang::MemberExpr *expr) { bool bound_object = retain_bound_temporaries_ && isAddrOf(); PushExprKind push(*this, isLValue() ? ExprKind::LValue : ExprKind::RValue); if (base_is_this) { - StrCat(InConstructor() ? "this" : keyword::kSelfValue); + StrCat(keyword::kSelfValue); } else if (expr->isArrow()) { ConvertArrow(base); } else if (bound_object) { @@ -4050,9 +4074,7 @@ bool Converter::VisitCXXThisExpr(clang::CXXThisExpr *expr) { : std::format("(&raw mut {})", name)); return false; } - if (InConstructor()) { - StrCat("&raw mut this"); - } else { + { PushParen paren(*this); StrCat(keyword::kSelfValue, keyword::kAs, ToString(expr->getType())); } @@ -4388,24 +4410,48 @@ void Converter::ConvertArrayCXXConstructExpr(clang::CXXConstructExpr *expr) { void Converter::ConvertCXXConstructExprArgs(clang::CXXConstructExpr *expr) { HoistMaterializedTempBindings hoist_temps(*this, /*as_block=*/true); auto ctor = expr->getConstructor(); - StrCat(GetRecordName(ctor->getParent()), token::kDoubleColon, - GetCtorName(ctor)); - PushParen paren(*this); - - for (unsigned i = 0; i < ctor->getNumParams(); ++i) { - assert(i < expr->getNumArgs()); - clang::Expr *arg = expr->getArg(i); - if (auto *default_arg = clang::dyn_cast(arg)) { - arg = default_arg->getExpr(); - } - { - PushBrace brace(*this); - ConvertVarInit(ctor->getParamDecl(i)->getType(), arg); + PushBrace block(*this); + StrCat(CreatePlacementStorage(GetRecordName(ctor->getParent()))); + StrCat(GetConstructorPath(ctor), token::kDoubleColon, GetCtorName(ctor)); + { + PushParen paren(*this); + StrCat(GetPlacementReceiver(), token::kComma); + for (unsigned i = 0; i < ctor->getNumParams(); ++i) { + assert(i < expr->getNumArgs()); + clang::Expr *arg = expr->getArg(i); + if (auto *default_arg = clang::dyn_cast(arg)) { + arg = default_arg->getExpr(); + } + { + PushBrace brace(*this); + ConvertVarInit(ctor->getParamDecl(i)->getType(), arg); + } + StrCat(token::kComma); } - StrCat(token::kComma); } + StrCat(token::kSemiColon, TakePlacementValue()); } +const char *Converter::GetConstructorSelf() const { return "&mut self"; } + +const char *Converter::GetConstructorWrite() const { + return "(self as *mut Self).write"; +} + +std::string +Converter::GetConstructorPath(const clang::CXXConstructorDecl *ctor) const { + return GetRecordName(ctor->getParent()); +} + +std::string +Converter::CreatePlacementStorage(const std::string &record_name) const { + return std::format("let mut __v: {0} = <{0}>::zero_init();", record_name); +} + +const char *Converter::GetPlacementReceiver() const { return "&mut __v"; } + +const char *Converter::TakePlacementValue() const { return "__v"; } + bool Converter::VisitCXXConstructExpr(clang::CXXConstructExpr *expr) { if (expr->isElidable()) { Convert(expr->getArg(0)); @@ -4856,11 +4902,8 @@ std::string Converter::GetDefaultAsStringFallback(clang::QualType qual_type) { } if (qual_type->isEnumeralType()) { - auto enum_decl = qual_type->castAs()->getDecl(); - if (enum_decl->enumerators().empty()) { - return GetDefaultAsString(enum_decl->getIntegerType()); - } - return EnumeratorName(*enum_decl->enumerator_begin()); + return GetDefaultAsString( + qual_type->castAs()->getDecl()->getIntegerType()); } return std::format("<{}>::default()", ToString(qual_type)); @@ -4921,7 +4964,7 @@ std::string Converter::GetRecordName(const clang::NamedDecl *decl) const { std::vector Converter::GetStructAttributes(const clang::RecordDecl *decl) { if (decl->isUnion()) { - return {"Copy", "Clone", "FnPtrArg", "VaArg"}; + return {"Copy", "Clone", "FnPtrArg", "VaArg", "ZeroInitializationUnsafe"}; } std::vector struct_attrs; @@ -4938,6 +4981,7 @@ Converter::GetStructAttributes(const clang::RecordDecl *decl) { struct_attrs.emplace_back("VaArg"); } struct_attrs.emplace_back("FnPtrArg"); + struct_attrs.emplace_back("ZeroInitializationUnsafe"); if (HasMoveFromConstructor(decl)) { struct_attrs.emplace_back("MoveCtorUnsafe"); @@ -5460,11 +5504,12 @@ void Converter::AddCloneTrait(const clang::RecordDecl *decl) { StrCat("fn clone(&self) -> Self"); PushBrace fn_brace(*this); auto source = ctor->getParamDecl(0)->getType().getNonReferenceType(); - StrCat(std::format("unsafe {{ {}::{}(self as *const {}{}) }}", record_name, - GetCtorName(ctor), record_name, - source.isConstQualified() - ? "" - : std::format(" as *mut {}", record_name))); + StrCat(std::format( + "unsafe {{ {}{}::{}({}, self as *const {}{}); {} }}", + CreatePlacementStorage(record_name), GetConstructorPath(ctor), + GetCtorName(ctor), GetPlacementReceiver(), record_name, + source.isConstQualified() ? "" : std::format(" as *mut {}", record_name), + TakePlacementValue())); } bool Converter::RecordImplementsClone(const clang::RecordDecl *decl) { @@ -5488,6 +5533,10 @@ void Converter::AddDefaultTrait(const clang::RecordDecl *decl) { StrCat(keyword_unsafe_); PushBrace unsafe_brace(*this); auto *cxx = clang::dyn_cast(decl); + if (cxx && !cxx->hasDefaultConstructor()) { + StrCat(std::format("<{}>::zero_init()", GetRecordName(decl))); + return; + } if (auto *default_ctor = cxx ? GetUserDefinedDefaultConstructor(cxx) : nullptr) { std::vector args; @@ -6412,12 +6461,9 @@ std::string Converter::ConvertGenericCall( if (call.method == "move_ctor") { assert(call_args.size() == 1 && "move_ctor takes one argument"); auto *record = type->getAsCXXRecordDecl(); - if (record && HasMoveFromConstructor(record)) { - return std::format("{}::move_from({})", GetUnboxedTypeAsString(type), - call_args[0]); - } - if (record && !IsUserDefinedDecl(record) && - !record->hasTrivialMoveConstructor()) { + if (record && (HasMoveFromConstructor(record) || + (!IsUserDefinedDecl(record) && + !record->hasTrivialMoveConstructor()))) { return std::format("<{} as {}>::move_ctor({})", GetUnboxedTypeAsString(type), GetMoveCtorTraitName(), call_args[0]); diff --git a/cpp2rust/converter/converter.h b/cpp2rust/converter/converter.h index 2766d0c25f..8919834443 100644 --- a/cpp2rust/converter/converter.h +++ b/cpp2rust/converter/converter.h @@ -211,6 +211,8 @@ class Converter : public clang::RecursiveASTVisitor { virtual void ConvertLateInstantiatedMethods(clang::CXXRecordDecl *decl); + virtual void ConvertTemplateInstantiatedCtors(clang::CXXRecordDecl *decl); + virtual std::string DestroyMembers(const clang::CXXRecordDecl *decl); bool NeedsScopedDestructor(const clang::VarDecl *decl) const; @@ -235,10 +237,30 @@ class Converter : public clang::RecursiveASTVisitor { virtual bool ThisIsRustPtr() const { return false; } - virtual void ConvertCXXConstructorBody(clang::CXXConstructorDecl *decl); + void ConvertCXXConstructorBody(clang::CXXConstructorDecl *decl); // The object that decl constructs, before its body runs. void EmitConstructorInit(clang::CXXConstructorDecl *decl); + void EmitConstructorFieldInits(clang::CXXConstructorDecl *decl); + + virtual std::string + ConvertConstructorFieldWrite(const std::string &field_name, + const std::string &value); + + virtual const char *GetConstructorSelf() const; + + virtual const char *GetConstructorWrite() const; + + virtual std::string + GetConstructorPath(const clang::CXXConstructorDecl *ctor) const; + + virtual std::string + CreatePlacementStorage(const std::string &record_name) const; + + virtual const char *GetPlacementReceiver() const; + + virtual const char *TakePlacementValue() const; + virtual bool VisitCXXConstructorDecl(clang::CXXConstructorDecl *decl); virtual bool VisitFieldDecl(clang::FieldDecl *decl); @@ -1115,9 +1137,6 @@ class Converter : public clang::RecursiveASTVisitor { std::shared_ptr boxed_vars_; clang::FunctionDecl *curr_function_ = nullptr; clang::Expr *return_value_ = nullptr; - bool InConstructor() const { - return clang::isa_and_present(curr_function_); - } bool in_function_formals_ = false; enum class MethodTarget : uint8_t { ValueImpl, @@ -1174,6 +1193,7 @@ class Converter : public clang::RecursiveASTVisitor { return std::format("__tmp_{}", materialized_temp_id_++); } std::string *materialized_temp_bindings_ = nullptr; + // The variable whose initializer is converted, which extends the lifetime // of the temporaries bound to its reference to its own. const clang::VarDecl *extending_decl_ = nullptr; diff --git a/cpp2rust/converter/converter_lib.cpp b/cpp2rust/converter/converter_lib.cpp index 1a05c6d914..4672d9e507 100644 --- a/cpp2rust/converter/converter_lib.cpp +++ b/cpp2rust/converter/converter_lib.cpp @@ -806,6 +806,14 @@ bool IsConvertibleImplicitMember(const clang::CXXMethodDecl *method) { IsConvertibleCopyAssignment(method); } +bool IsConvertedConstructor(const clang::CXXConstructorDecl *decl) { + if (decl->isOutOfLine() || + (decl->isImplicit() && !IsConvertibleImplicitMember(decl))) { + return false; + } + return !decl->isCopyOrMoveConstructor() || decl->hasBody(); +} + bool IsDefaultedMoveConstructor(const clang::CXXConstructorDecl *ctor) { return ctor->isMoveConstructor() && !ctor->isUserProvided() && IsUserDefinedDecl(ctor->getParent()); diff --git a/cpp2rust/converter/converter_lib.h b/cpp2rust/converter/converter_lib.h index 7859587106..da432b5929 100644 --- a/cpp2rust/converter/converter_lib.h +++ b/cpp2rust/converter/converter_lib.h @@ -143,6 +143,8 @@ bool HasMoveAssignOperator(const clang::RecordDecl *decl); bool IsConvertibleImplicitMember(const clang::CXXMethodDecl *method); +bool IsConvertedConstructor(const clang::CXXConstructorDecl *decl); + clang::CXXConstructorDecl * GetUserDefinedCopyConstructor(const clang::RecordDecl *decl); diff --git a/cpp2rust/converter/models/converter_refcount.cpp b/cpp2rust/converter/models/converter_refcount.cpp index 18949be5df..9bc5a22253 100644 --- a/cpp2rust/converter/models/converter_refcount.cpp +++ b/cpp2rust/converter/models/converter_refcount.cpp @@ -763,13 +763,22 @@ void ConverterRefCount::AddCloneTrait(const clang::RecordDecl *decl) { PushConversionKind push(*this, ConversionKind::FullRefCount); StrCat(std::format("let __src: Value<{}> = {};", record_name, BoxValue(GetShallowCopy(decl, "self")))); - StrCat(std::format("{}::{}(__src.as_pointer())", record_name, - GetCtorName(ctor))); + StrCat(CreatePlacementStorage(record_name)); + StrCat(std::format("{}::{}({}, __src.as_pointer());", + GetConstructorPath(ctor), GetCtorName(ctor), + GetPlacementReceiver())); + StrCat(TakePlacementValue()); } else { for (auto ctor : cxx->ctors()) { if (ctor->isCopyConstructor()) { PushConversionKind push(*this, ConversionKind::FullRefCount); - ConvertCXXConstructorBody(ctor); + StrCat(std::format("let __src: Value<{}> = {};", record_name, + BoxValue(GetShallowCopy(decl, "self")))); + StrCat(CreatePlacementStorage(record_name)); + StrCat(std::format("{}::{}({}, __src.as_pointer());", + GetConstructorPath(ctor), GetCtorName(ctor), + GetPlacementReceiver())); + StrCat(TakePlacementValue()); break; } } @@ -799,8 +808,9 @@ void ConverterRefCount::EmitRustUnion(clang::RecordDecl *decl) { auto size = ctx_.getTypeSizeInChars(ctx_.getCanonicalTagType(decl)); StrCat(std::format( - "#[derive(ByteRepr, Clone)] #[byte_size({0})] pub struct {1} {{ " - "#[offset(0)] #[byte_size({0})] __bytes: Box<[u8]> }}", + "#[derive(ByteRepr, Clone, ZeroInitialization)] #[byte_size({0})] pub " + "struct {1} {{ " + "#[offset(0)] #[byte_size({0})] #[array_len({0})] __bytes: Box<[u8]> }}", size.getQuantity(), name)); StrCat("impl", name); @@ -847,6 +857,31 @@ bool ConverterRefCount::VisitCXXConstructorDecl( return Converter::VisitCXXConstructorDecl(decl); } +static std::vector GetArrayLengths(const clang::ASTContext &ctx, + clang::QualType type) { + std::vector lengths; + while (true) { + if (auto *array = ctx.getAsConstantArrayType(type)) { + lengths.push_back(array->getSize().getZExtValue()); + type = array->getElementType(); + continue; + } + if (ctx.getAsIncompleteArrayType(type)) { + lengths.push_back(0); + return lengths; + } + auto *array = + llvm::dyn_cast_or_null( + type->getAsCXXRecordDecl()); + if (!IsStdDecl(array, "array")) { + return lengths; + } + auto &args = array->getTemplateArgs(); + lengths.push_back(args[1].getAsIntegral().getZExtValue()); + type = args[0].getAsType(); + } +} + bool ConverterRefCount::VisitFieldDecl(clang::FieldDecl *decl) { // The C offset of the field, which locates it for pointers to it. const auto &layout = ctx_.getASTRecordLayout(decl->getParent()); @@ -860,6 +895,13 @@ bool ConverterRefCount::VisitFieldDecl(clang::FieldDecl *decl) { StrCat(std::format("#[byte_size({})]", ctx_.getTypeSizeInChars(decl->getType()).getQuantity())); } + if (auto lengths = GetArrayLengths(ctx_, decl->getType()); !lengths.empty()) { + std::string args; + for (auto length : lengths) { + args += std::format("{}{}", args.empty() ? "" : ", ", length); + } + StrCat(std::format("#[array_len({})]", args)); + } PushConversionKind push(*this, ConversionKind::Pointee); return Converter::VisitFieldDecl(decl); } @@ -2299,6 +2341,7 @@ ConverterRefCount::GetStructAttributes(const clang::RecordDecl *decl) { attrs.emplace_back("Record"); // Gives access to the bytes of the struct, at the offsets of its fields. attrs.emplace_back("ByteRepr"); + attrs.emplace_back("ZeroInitialization"); if (RecordImplementsClone(decl)) { attrs.emplace_back("VaArg"); @@ -3570,10 +3613,7 @@ void ConverterRefCount::SetUFCSReceiver(clang::Expr *base, bool is_arrow, base->IgnoreParenImpCasts()); if (clang::isa(base->IgnoreParenImpCasts()) && !AsLambdaOperatorCall(curr_function_)) { - if (InConstructor()) { - ctor_uses_this_ = true; - ufcs_receiver_ = "&this"; - } else if (ThisIsRustPtr()) { + if (ThisIsRustPtr()) { ufcs_receiver_ = keyword::kSelfValue; } else { ufcs_receiver_ = token::kRef + ConvertPointer(base); @@ -3651,7 +3691,8 @@ void ConverterRefCount::ConvertMethodOnPtrTraitDecl( } void ConverterRefCount::ConvertMethodOnPtr(clang::CXXMethodDecl *method) { - if (!method->isThisDeclarationADefinition()) { + if (!method->isThisDeclarationADefinition() && + !clang::isa(method)) { return; } Buffer buf(*this); @@ -3662,6 +3703,16 @@ void ConverterRefCount::ConvertMethodOnPtr(clang::CXXMethodDecl *method) { MethodsOnPtrFor(method->getParent()).impl.body += std::move(buf).str(); } +void ConverterRefCount::ConvertTemplateInstantiatedCtors( + clang::CXXRecordDecl *decl) { + for (auto c : GetTemplateInstantiatedCtors(decl)) { + if (!decl_ids_.contains(GetID(c))) { + ConvertMethodOnPtrTraitDecl(c); + ConvertMethodOnPtr(c); + } + } +} + void ConverterRefCount::ConvertLateInstantiatedMethods( clang::CXXRecordDecl *decl) { Converter::ConvertCXXMethodDecls( @@ -3669,11 +3720,14 @@ void ConverterRefCount::ConvertLateInstantiatedMethods( [](auto *method) { return IsEmittableMethod(method) && method->hasBody() && !IsMethodOnPtr(method) && + !clang::isa(method) && !decl_ids_.contains(GetMethodID(method)); }); ForEachMethod(decl, [&](clang::CXXMethodDecl *method) { if (IsEmittableMethod(method) && method->hasBody() && - IsMethodOnPtr(method) && !decl_ids_.contains(GetMethodID(method))) { + (IsMethodOnPtr(method) || + clang::isa(method)) && + !decl_ids_.contains(GetMethodID(method))) { ConvertMethodOnPtr(method); } }); @@ -3685,10 +3739,17 @@ void ConverterRefCount::ConvertCXXRecordMethods(clang::CXXRecordDecl *decl) { ConvertCXXMethodDecls(decl, std::format("{} {}", keyword::kImpl, struct_name), [](auto *method) { return IsEmittableMethod(method) && - !IsMethodOnPtr(method); + !IsMethodOnPtr(method) && + !clang::isa(method); }); ForEachMethod(decl, [&](clang::CXXMethodDecl *method) { + if (auto *ctor = clang::dyn_cast(method); + ctor && IsEmittableMethod(ctor) && IsConvertedConstructor(ctor) && + !ctor->isDependentContext()) { + ConvertMethodOnPtrTraitDecl(ctor); + ConvertMethodOnPtr(ctor); + } if (IsMethodOnPtr(method) && !method->getReturnType()->isUndeducedType()) { ConvertMethodOnPtrTraitDecl(method); ConvertMethodOnPtr(method); @@ -3713,42 +3774,36 @@ ConverterRefCount::DestroyMembers(const clang::CXXRecordDecl *decl) { return out; } -void ConverterRefCount::ConvertCXXConstructorBody( - clang::CXXConstructorDecl *decl) { - EmitFunctionPreamble(decl); - auto record_name = GetRecordName(decl->getParent()); - auto saved_uses_this = std::exchange(ctor_uses_this_, false); - std::string init; - { - Buffer buf(*this); - EmitConstructorInit(decl); - init = std::move(buf).str(); - } - std::string body; - { - Buffer buf(*this); - ConvertBodyStmts(decl->getBody()); - body = std::move(buf).str(); - } - bool uses_this = std::exchange(ctor_uses_this_, saved_uses_this); +const char *ConverterRefCount::GetConstructorSelf() const { return "&self"; } - if (!uses_this) { - if (Trim(body).empty()) { - StrCat(init); - } else { - StrCat(keyword::kLet, "__this", token::kColon, record_name, - token::kAssign, init, token::kSemiColon, body, "__this"); - } - return; - } - StrCat(keyword::kLet, "__this", token::kColon, - std::format("Value<{}>", record_name), token::kAssign, - std::format("Rc::new(RefCell::new({}))", init), token::kSemiColon); - StrCat(keyword::kLet, "this", token::kColon, - std::format("Ptr<{}>", record_name), token::kAssign, - "__this.as_pointer()", token::kSemiColon); - StrCat(body); - StrCat("Rc::try_unwrap(__this).ok().unwrap().into_inner()"); +std::string +ConverterRefCount::ConvertConstructorFieldWrite(const std::string &field_name, + const std::string &value) { + return std::format("field!((*self), {}).write({});", field_name, value); +} + +const char *ConverterRefCount::GetConstructorWrite() const { + return "self.write"; +} + +std::string ConverterRefCount::GetConstructorPath( + const clang::CXXConstructorDecl *ctor) const { + return TraitName(ctor->getParent()); +} + +std::string ConverterRefCount::CreatePlacementStorage( + const std::string &record_name) const { + return std::format( + "let __v: Value<{0}> = Rc::new(RefCell::new(<{0}>::zero_init()));", + record_name); +} + +const char *ConverterRefCount::GetPlacementReceiver() const { + return "&__v.as_pointer()"; +} + +const char *ConverterRefCount::TakePlacementValue() const { + return "Rc::try_unwrap(__v).ok().unwrap().into_inner()"; } bool ConverterRefCount::VisitCXXThisExpr( @@ -3764,12 +3819,7 @@ bool ConverterRefCount::VisitCXXThisExpr( } return false; } - if (InConstructor()) { - ctor_uses_this_ = true; - StrCat("this"); - } else { - StrCat("(*", keyword::kSelfValue, ')'); - } + StrCat("(*", keyword::kSelfValue, ')'); MarkNotFresh(); return false; } diff --git a/cpp2rust/converter/models/converter_refcount.h b/cpp2rust/converter/models/converter_refcount.h index 567143c402..82df79596d 100644 --- a/cpp2rust/converter/models/converter_refcount.h +++ b/cpp2rust/converter/models/converter_refcount.h @@ -85,12 +85,29 @@ class ConverterRefCount final : public Converter { bool ConvertOutOfLineMethod(clang::CXXMethodDecl *decl) override; - void ConvertCXXConstructorBody(clang::CXXConstructorDecl *decl) override; + const char *GetConstructorSelf() const override; + + const char *GetConstructorWrite() const override; + + std::string ConvertConstructorFieldWrite(const std::string &field_name, + const std::string &value) override; + + std::string + GetConstructorPath(const clang::CXXConstructorDecl *ctor) const override; + + std::string + CreatePlacementStorage(const std::string &record_name) const override; + + const char *GetPlacementReceiver() const override; + + const char *TakePlacementValue() const override; void ConvertCXXRecordMethods(clang::CXXRecordDecl *decl) override; void ConvertLateInstantiatedMethods(clang::CXXRecordDecl *decl) override; + void ConvertTemplateInstantiatedCtors(clang::CXXRecordDecl *decl) override; + void ConvertMethodOnPtrTraitDecl(clang::CXXMethodDecl *method); void ConvertMethodOnPtr(clang::CXXMethodDecl *method); @@ -612,10 +629,6 @@ class ConverterRefCount final : public Converter { // which is stored in place, even if it is a reference. bool IsFieldOfUnboxedPlace(const clang::MemberExpr *expr) const; - // Set when a constructor's translation refers to `this`, which then needs - // the object being built to be in a Value. - bool ctor_uses_this_ = false; - /// The kind of conversion that should be performed. enum class ConversionKind : uint8_t { Unboxed, diff --git a/libcc2rs-macros/src/cpp_from.rs b/libcc2rs-macros/src/cpp_from.rs index bea36f5492..f9dd76d5d0 100644 --- a/libcc2rs-macros/src/cpp_from.rs +++ b/libcc2rs-macros/src/cpp_from.rs @@ -80,7 +80,7 @@ fn ctor_call( (Kind::MutPtr, true) if !is_move => quote!(src.cast_mut()), (Kind::Ptr | Kind::MutPtr, _) => quote!(src), }; - quote!(#name::#method(#arg)) + crate::initialization::construct(is_unsafe, name, method, arg) } fn method(is_unsafe: bool, signature: TokenStream2, body: TokenStream2) -> TokenStream2 { diff --git a/libcc2rs-macros/src/initialization.rs b/libcc2rs-macros/src/initialization.rs new file mode 100644 index 0000000000..c03fc25495 --- /dev/null +++ b/libcc2rs-macros/src/initialization.rs @@ -0,0 +1,94 @@ +// Copyright (c) 2022-present INESC-ID. +// Distributed under the MIT license that can be found in the LICENSE file. + +use proc_macro::TokenStream; +use proc_macro2::TokenStream as TokenStream2; +use quote::{format_ident, quote}; +use syn::punctuated::Punctuated; +use syn::{Data, DeriveInput, Expr, Field, Fields, Ident, Index, Token, parse_macro_input}; + +// The zero-initialized value of `field`. An array, whose length is +// not part of its type, has one array_len argument per dimension. +fn zero_init(field: &Field) -> TokenStream2 { + let lens = field + .attrs + .iter() + .find(|attr| attr.path().is_ident("array_len")) + .map(|attr| { + attr.parse_args_with(Punctuated::::parse_terminated) + .unwrap() + }); + let mut value = quote!(::libcc2rs::ZeroInitialization::zero_init()); + for len in lens.iter().flatten().rev() { + value = quote!((0..#len).map(|_| #value).collect()); + } + value +} + +pub fn expand(input: TokenStream) -> TokenStream { + let input = parse_macro_input!(input as DeriveInput); + let name = &input.ident; + let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl(); + let Data::Struct(data) = &input.data else { + panic!("derive(ZeroInitialization) is only supported on structs, `{name}` is not one"); + }; + let fields = match &data.fields { + Fields::Named(fields) => { + let idents = fields.named.iter().map(|field| &field.ident); + let values = fields.named.iter().map(zero_init); + quote!({ #(#idents: #values,)* }) + } + Fields::Unnamed(fields) => { + let indices = (0..fields.unnamed.len()).map(Index::from); + let values = fields.unnamed.iter().map(zero_init); + quote!({ #(#indices: #values,)* }) + } + Fields::Unit => quote!({}), + }; + quote! { + impl #impl_generics ::libcc2rs::ZeroInitialization for #name #ty_generics #where_clause { + fn zero_init() -> Self { + Self #fields + } + } + } + .into() +} + +pub fn expand_unsafe(input: TokenStream) -> TokenStream { + let input = parse_macro_input!(input as DeriveInput); + let name = &input.ident; + let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl(); + quote! { + impl #impl_generics ::libcc2rs::ZeroInitializationUnsafe for #name #ty_generics #where_clause { + unsafe fn zero_init() -> Self { + unsafe { ::std::mem::MaybeUninit::zeroed().assume_init() } + } + } + } + .into() +} + +pub(crate) fn construct( + is_unsafe: bool, + name: &Ident, + method: &Ident, + args: TokenStream2, +) -> TokenStream2 { + if is_unsafe { + quote! {{ + let mut __v: Self = ::zero_init(); + #name::#method(&mut __v, #args); + __v + }} + } else { + let impl_trait = format_ident!("{}Impl", name); + quote! {{ + let __v: ::libcc2rs::Value = ::std::rc::Rc::new(::std::cell::RefCell::new( + ::zero_init(), + )); + #impl_trait::#method(&::libcc2rs::AsPointer::as_pointer(&__v), #args); + ::std::rc::Rc::try_unwrap(__v).ok().unwrap().into_inner() + }} + } +} diff --git a/libcc2rs-macros/src/lib.rs b/libcc2rs-macros/src/lib.rs index 846b24c7d2..dc4c581512 100644 --- a/libcc2rs-macros/src/lib.rs +++ b/libcc2rs-macros/src/lib.rs @@ -9,6 +9,7 @@ mod cpp_from; mod destructor; mod fn_ptr_arg; mod goto; +mod initialization; mod lambda; mod move_ctor; mod record; @@ -148,6 +149,33 @@ pub fn derive_record(input: TokenStream) -> TokenStream { record::expand(input) } +// #[derive(ZeroInitialization)] +// pub struct S { +// pub a: i32, +// #[array_len(2, 3)] +// pub b: Box<[Box<[i32]>]>, +// } +// +// Implements libcc2rs::ZeroInitialization for S, whose zero_init is the +// zero_init of each of its fields. The arguments of the array_len attribute are the lengths of +// the dimensions of a field that is an array, outermost first. + +#[proc_macro_derive(ZeroInitialization, attributes(array_len))] +pub fn derive_zero_initialization(input: TokenStream) -> TokenStream { + initialization::expand(input) +} + +// #[derive(ZeroInitializationUnsafe)] +// pub struct S { ... } +// +// Implements libcc2rs::ZeroInitializationUnsafe for S, whose zero_init is S +// with all its bytes zero. + +#[proc_macro_derive(ZeroInitializationUnsafe)] +pub fn derive_zero_initialization_unsafe(input: TokenStream) -> TokenStream { + initialization::expand_unsafe(input) +} + // #[derive(MoveCtor)] // pub struct S { ... } // impl S { diff --git a/libcc2rs-macros/src/move_ctor.rs b/libcc2rs-macros/src/move_ctor.rs index ee94179972..7848b56352 100644 --- a/libcc2rs-macros/src/move_ctor.rs +++ b/libcc2rs-macros/src/move_ctor.rs @@ -2,18 +2,20 @@ // Distributed under the MIT license that can be found in the LICENSE file. use proc_macro::TokenStream; -use quote::quote; +use quote::{format_ident, quote}; use syn::{DeriveInput, parse_macro_input}; pub fn expand(input: TokenStream, is_unsafe: bool) -> TokenStream { let input = parse_macro_input!(input as DeriveInput); let name = &input.ident; let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl(); + let body = + crate::initialization::construct(is_unsafe, name, &format_ident!("move_from"), quote!(src)); if is_unsafe { quote! { impl #impl_generics ::libcc2rs::MoveCtorUnsafe for #name #ty_generics #where_clause { unsafe fn move_ctor(src: *mut Self) -> Self { - unsafe { Self::move_from(src) } + unsafe { #body } } } } @@ -21,7 +23,7 @@ pub fn expand(input: TokenStream, is_unsafe: bool) -> TokenStream { quote! { impl #impl_generics ::libcc2rs::MoveCtor for #name #ty_generics #where_clause { fn move_ctor(src: ::libcc2rs::Ptr) -> Self { - Self::move_from(src) + #body } } } diff --git a/libcc2rs/src/fn_ptr.rs b/libcc2rs/src/fn_ptr.rs index d6fe3d8d82..aa5c2dbb0d 100644 --- a/libcc2rs/src/fn_ptr.rs +++ b/libcc2rs/src/fn_ptr.rs @@ -495,6 +495,13 @@ impl Eq for FnPtr {} impl ByteRepr for FnPtr {} +impl crate::ZeroInitialization for FnPtr { + #[inline] + fn zero_init() -> Self { + FnPtr::null() + } +} + impl FnPtrArg for FnPtr { #[inline] fn to_repr(self) -> ArgRepr { diff --git a/libcc2rs/src/initialization.rs b/libcc2rs/src/initialization.rs new file mode 100644 index 0000000000..afb1c38722 --- /dev/null +++ b/libcc2rs/src/initialization.rs @@ -0,0 +1,91 @@ +// Copyright (c) 2022-present INESC-ID. +// Distributed under the MIT license that can be found in the LICENSE file. + +use std::cell::RefCell; +use std::rc::Rc; + +// Zero-initialization ([dcl.init.general]/6): scalars are zero, pointers are +// null. It is also the storage of an object before its constructor runs, so +// creating it has no side effects. +pub trait ZeroInitialization { + fn zero_init() -> Self; +} + +pub trait ZeroInitializationUnsafe: Sized { + /// # Safety + /// Unless all zero bytes are a valid Self, the result must be overwritten + /// before it is used or destroyed. + unsafe fn zero_init() -> Self; +} + +macro_rules! impl_zero_init { + ($($ty:ty),* $(,)?) => {$( + impl ZeroInitialization for $ty { + #[inline] + fn zero_init() -> Self { + Default::default() + } + } + )*}; +} + +impl_zero_init!( + (), + bool, + u8, + i8, + u16, + i16, + u32, + i32, + u64, + i64, + u128, + i128, + usize, + isize, + f32, + f64, +); + +impl ZeroInitialization for std::cmp::Ordering { + #[inline] + fn zero_init() -> Self { + std::cmp::Ordering::Equal + } +} + +impl ZeroInitialization for Option { + #[inline] + fn zero_init() -> Self { + None + } +} + +impl ZeroInitialization for Vec { + #[inline] + fn zero_init() -> Self { + Vec::new() + } +} + +impl ZeroInitialization for Rc> { + #[inline] + fn zero_init() -> Self { + Rc::new(RefCell::new(T::zero_init())) + } +} + +impl ZeroInitialization for [T; N] { + #[inline] + fn zero_init() -> Self { + std::array::from_fn(|_| T::zero_init()) + } +} + +impl ZeroInitialization for (A, B) { + #[inline] + fn zero_init() -> Self { + (A::zero_init(), B::zero_init()) + } +} diff --git a/libcc2rs/src/lib.rs b/libcc2rs/src/lib.rs index 3b4c692a15..50998a26ee 100644 --- a/libcc2rs/src/lib.rs +++ b/libcc2rs/src/lib.rs @@ -53,6 +53,9 @@ pub use cmp::*; mod conversion; pub use conversion::{CppFrom, CppFromUnsafe}; +mod initialization; +pub use initialization::{ZeroInitialization, ZeroInitializationUnsafe}; + mod swap; // Used by cpp_swap! and cpp_swap_unsafe!. #[doc(hidden)] @@ -106,5 +109,6 @@ pub use format::*; pub use libcc2rs_macros::{ ByteRepr, CopyAssign, CopyAssignUnsafe, CppFrom, CppFromUnsafe, Destructible, DestructibleUnsafe, FnPtrArg, MoveAssign, MoveAssignUnsafe, MoveCtor, MoveCtorUnsafe, Record, - VaArg, goto, goto_block, lambda, lambda_unsafe, switch, + VaArg, ZeroInitialization, ZeroInitializationUnsafe, goto, goto_block, lambda, lambda_unsafe, + switch, }; diff --git a/libcc2rs/src/rc.rs b/libcc2rs/src/rc.rs index 248e89a7cd..bbaf6b8341 100644 --- a/libcc2rs/src/rc.rs +++ b/libcc2rs/src/rc.rs @@ -477,6 +477,13 @@ impl Default for Ptr { } } +impl crate::ZeroInitialization for Ptr { + #[inline] + fn zero_init() -> Self { + Ptr::null() + } +} + impl Clone for Ptr { #[inline] fn clone(&self) -> Self { diff --git a/libcc2rs/src/void.rs b/libcc2rs/src/void.rs index 014a5d1fb7..9b8d672467 100644 --- a/libcc2rs/src/void.rs +++ b/libcc2rs/src/void.rs @@ -105,6 +105,13 @@ impl Ptr { } } +impl crate::ZeroInitialization for AnyPtr { + #[inline] + fn zero_init() -> Self { + AnyPtr::default() + } +} + impl Default for AnyPtr { fn default() -> Self { Ptr::<()>::null().to_any() diff --git a/rules/brotli/shim.rs b/rules/brotli/shim.rs index 942dfb21ae..51fd14e2c6 100644 --- a/rules/brotli/shim.rs +++ b/rules/brotli/shim.rs @@ -50,6 +50,12 @@ pub struct BrotliDecoderState { impl ByteRepr for BrotliDecoderState {} +impl crate::ZeroInitialization for BrotliDecoderState { + fn zero_init() -> Self { + Self::default() + } +} + impl Default for BrotliDecoderState { fn default() -> Self { Self { diff --git a/rules/dirent/shim.rs b/rules/dirent/shim.rs index 90c77ada5e..1d738cf2cc 100644 --- a/rules/dirent/shim.rs +++ b/rules/dirent/shim.rs @@ -1,11 +1,11 @@ // Copyright (c) 2022-present INESC-ID. // Distributed under the MIT license that can be found in the LICENSE file. -use crate::{ByteRepr, Record, size_of_field}; +use crate::{ByteRepr, Record, ZeroInitialization, size_of_field}; use std::cell::Cell; use std::mem::{offset_of, size_of}; -#[derive(Clone, Record, ByteRepr)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization)] #[byte_size(size_of::<::libc::dirent>())] pub struct Dirent { #[offset(offset_of!(::libc::dirent, d_ino))] @@ -19,6 +19,7 @@ pub struct Dirent { pub d_type: u8, #[offset(offset_of!(::libc::dirent, d_name))] #[byte_size(size_of_field!(::libc::dirent, d_name))] + #[array_len(size_of_field!(::libc::dirent, d_name))] pub d_name: Box<[i8]>, } diff --git a/rules/ifaddrs/shim.rs b/rules/ifaddrs/shim.rs index 9189609094..eff408a936 100644 --- a/rules/ifaddrs/shim.rs +++ b/rules/ifaddrs/shim.rs @@ -1,10 +1,12 @@ // Copyright (c) 2022-present INESC-ID. // Distributed under the MIT license that can be found in the LICENSE file. -use crate::{ByteRepr, Ptr, Record, Sockaddr, SockaddrIn, SockaddrIn6, SockaddrStorage}; +use crate::{ + ByteRepr, Ptr, Record, Sockaddr, SockaddrIn, SockaddrIn6, SockaddrStorage, ZeroInitialization, +}; use std::mem::{offset_of, size_of}; -#[derive(Clone, Default, Record, ByteRepr)] +#[derive(Clone, Default, Record, ByteRepr, ZeroInitialization)] #[byte_size(size_of::<::libc::ifaddrs>())] pub struct Ifaddrs { #[offset(offset_of!(::libc::ifaddrs, ifa_next))] diff --git a/rules/ip/shim.rs b/rules/ip/shim.rs index 38b96f86a8..842f81350e 100644 --- a/rules/ip/shim.rs +++ b/rules/ip/shim.rs @@ -1,20 +1,21 @@ // Copyright (c) 2022-present INESC-ID. // Distributed under the MIT license that can be found in the LICENSE file. -use crate::{ByteRepr, Ptr, Record}; +use crate::{ByteRepr, Ptr, Record, ZeroInitialization}; -#[derive(Clone, Default, Record, ByteRepr)] +#[derive(Clone, Default, Record, ByteRepr, ZeroInitialization)] #[byte_size(4)] pub struct InAddr { #[offset(0)] pub s_addr: u32, } -#[derive(Clone, Record, ByteRepr)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization)] #[byte_size(16)] pub struct In6Addr { #[offset(0)] #[byte_size(16)] + #[array_len(16)] pub s6_addr: Box<[u8]>, } diff --git a/rules/iterator_category/shim.rs b/rules/iterator_category/shim.rs index 59b078ce6c..7fcaaeedd4 100644 --- a/rules/iterator_category/shim.rs +++ b/rules/iterator_category/shim.rs @@ -216,6 +216,12 @@ pub enum BidirectionalIterator { Map(Value>), } +impl crate::ZeroInitialization for BidirectionalIterator { + fn zero_init() -> Self { + Self::default() + } +} + impl Default for BidirectionalIterator { fn default() -> Self { Self::Map(MapNode::header()) @@ -348,6 +354,12 @@ macro_rules! random_access_iterator { } } + impl crate::ZeroInitialization for $name { + fn zero_init() -> Self { + Self::default() + } + } + impl Clone for $name { fn clone(&self) -> Self { match self { diff --git a/rules/map/shim.rs b/rules/map/shim.rs index e2f3551375..caaf12d995 100644 --- a/rules/map/shim.rs +++ b/rules/map/shim.rs @@ -89,6 +89,12 @@ pub struct Map { header: Value>, } +impl crate::ZeroInitialization for Map { + fn zero_init() -> Self { + Self::default() + } +} + impl Default for Map { fn default() -> Self { Self { diff --git a/rules/netdb/shim.rs b/rules/netdb/shim.rs index 526a3e23b8..9432b2adcf 100644 --- a/rules/netdb/shim.rs +++ b/rules/netdb/shim.rs @@ -1,10 +1,10 @@ // Copyright (c) 2022-present INESC-ID. // Distributed under the MIT license that can be found in the LICENSE file. -use crate::{ByteRepr, Ptr, Record, Sockaddr}; +use crate::{ByteRepr, Ptr, Record, Sockaddr, ZeroInitialization}; use std::mem::{offset_of, size_of}; -#[derive(Clone, Default, Record, ByteRepr)] +#[derive(Clone, Default, Record, ByteRepr, ZeroInitialization)] #[byte_size(size_of::<::libc::addrinfo>())] pub struct Addrinfo { #[offset(offset_of!(::libc::addrinfo, ai_flags))] diff --git a/rules/poll/shim.rs b/rules/poll/shim.rs index 6ad3807f9f..06f6daa720 100644 --- a/rules/poll/shim.rs +++ b/rules/poll/shim.rs @@ -1,10 +1,10 @@ // Copyright (c) 2022-present INESC-ID. // Distributed under the MIT license that can be found in the LICENSE file. -use crate::{ByteRepr, Record}; +use crate::{ByteRepr, Record, ZeroInitialization}; use std::mem::{offset_of, size_of}; -#[derive(Clone, Default, Record, ByteRepr)] +#[derive(Clone, Default, Record, ByteRepr, ZeroInitialization)] #[byte_size(size_of::<::libc::pollfd>())] pub struct Pollfd { #[offset(offset_of!(::libc::pollfd, fd))] diff --git a/rules/pwd/shim.rs b/rules/pwd/shim.rs index 724e508de3..01229b236f 100644 --- a/rules/pwd/shim.rs +++ b/rules/pwd/shim.rs @@ -1,10 +1,10 @@ // Copyright (c) 2022-present INESC-ID. // Distributed under the MIT license that can be found in the LICENSE file. -use crate::{ByteRepr, Ptr, Record}; +use crate::{ByteRepr, Ptr, Record, ZeroInitialization}; use std::mem::{offset_of, size_of}; -#[derive(Clone, Default, Record, ByteRepr)] +#[derive(Clone, Default, Record, ByteRepr, ZeroInitialization)] #[byte_size(size_of::<::libc::passwd>())] pub struct Passwd { #[offset(offset_of!(::libc::passwd, pw_name))] diff --git a/rules/select/shim.rs b/rules/select/shim.rs index 2be99981e0..7a108b2223 100644 --- a/rules/select/shim.rs +++ b/rules/select/shim.rs @@ -10,6 +10,12 @@ pub struct CFdSet { impl crate::ByteRepr for CFdSet {} +impl crate::ZeroInitialization for CFdSet { + fn zero_init() -> Self { + Self::default() + } +} + impl CFdSet { pub fn zero(&mut self) { self.fds.clear(); diff --git a/rules/socket/shim.rs b/rules/socket/shim.rs index 6d8009b0e8..47ef06c5f9 100644 --- a/rules/socket/shim.rs +++ b/rules/socket/shim.rs @@ -1,20 +1,21 @@ // Copyright (c) 2022-present INESC-ID. // Distributed under the MIT license that can be found in the LICENSE file. -use crate::{ByteRepr, CChar, In6Addr, InAddr, Ptr, Record}; +use crate::{ByteRepr, CChar, In6Addr, InAddr, Ptr, Record, ZeroInitialization}; // The fields are at the offsets of the byte representation below. -#[derive(Clone, Record, ByteRepr)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization)] #[byte_size(16)] pub struct Sockaddr { #[offset(0)] pub sa_family: u16, #[offset(2)] #[byte_size(14)] + #[array_len(14)] pub sa_data: Box<[i8]>, } -#[derive(Clone, Record, ByteRepr)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization)] #[byte_size(16)] pub struct SockaddrIn { #[offset(0)] @@ -25,10 +26,11 @@ pub struct SockaddrIn { pub sin_addr: InAddr, #[offset(8)] #[byte_size(8)] + #[array_len(8)] pub sin_zero: Box<[u8]>, } -#[derive(Clone, Default, Record, ByteRepr)] +#[derive(Clone, Default, Record, ByteRepr, ZeroInitialization)] #[byte_size(28)] pub struct SockaddrIn6 { #[offset(0)] @@ -43,23 +45,25 @@ pub struct SockaddrIn6 { pub sin6_scope_id: u32, } -#[derive(Clone, Record, ByteRepr)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization)] #[byte_size(110)] pub struct SockaddrUn { #[offset(0)] pub sun_family: u16, #[offset(2)] #[byte_size(108)] + #[array_len(108)] pub sun_path: Box<[i8]>, } -#[derive(Clone, Record, ByteRepr)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization)] #[byte_size(128)] pub struct SockaddrStorage { #[offset(0)] pub ss_family: u16, #[offset(2)] #[byte_size(126)] + #[array_len(126)] pub __pad: Box<[u8]>, } diff --git a/rules/stat/shim.rs b/rules/stat/shim.rs index bad11db7ab..423b6bbdd6 100644 --- a/rules/stat/shim.rs +++ b/rules/stat/shim.rs @@ -1,10 +1,10 @@ // Copyright (c) 2022-present INESC-ID. // Distributed under the MIT license that can be found in the LICENSE file. -use crate::{ByteRepr, Record}; +use crate::{ByteRepr, Record, ZeroInitialization}; use std::mem::{offset_of, size_of}; -#[derive(Clone, Default, Record)] +#[derive(Clone, Default, Record, ZeroInitialization)] // The fields have the sizes of those of libc::stat only in x86-64 Linux. #[cfg_attr( all(target_os = "linux", target_arch = "x86_64"), diff --git a/rules/termios/shim.rs b/rules/termios/shim.rs index 2155a01bd2..2a4ea03734 100644 --- a/rules/termios/shim.rs +++ b/rules/termios/shim.rs @@ -1,10 +1,10 @@ // Copyright (c) 2022-present INESC-ID. // Distributed under the MIT license that can be found in the LICENSE file. -use crate::{ByteRepr, Record}; +use crate::{ByteRepr, Record, ZeroInitialization}; use std::mem::{offset_of, size_of}; -#[derive(Clone, Record, ByteRepr)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization)] #[byte_size(size_of::<::libc::termios>())] pub struct Termios { #[offset(offset_of!(::libc::termios, c_iflag))] @@ -20,6 +20,7 @@ pub struct Termios { pub c_line: u8, #[offset(offset_of!(::libc::termios, c_cc))] #[byte_size(::libc::NCCS)] + #[array_len(32)] pub c_cc: Box<[u8]>, #[offset(offset_of!(::libc::termios, c_ispeed))] pub c_ispeed: u32, @@ -104,7 +105,7 @@ impl Termios { } } -#[derive(Clone, Default, Record, ByteRepr)] +#[derive(Clone, Default, Record, ByteRepr, ZeroInitialization)] #[byte_size(size_of::<::libc::winsize>())] pub struct Winsize { #[offset(offset_of!(::libc::winsize, ws_row))] diff --git a/rules/time/shim.rs b/rules/time/shim.rs index 431e66da43..2c9066f65c 100644 --- a/rules/time/shim.rs +++ b/rules/time/shim.rs @@ -1,10 +1,10 @@ // Copyright (c) 2022-present INESC-ID. // Distributed under the MIT license that can be found in the LICENSE file. -use crate::{ByteRepr, Ptr, Record}; +use crate::{ByteRepr, Ptr, Record, ZeroInitialization}; use std::mem::{offset_of, size_of}; -#[derive(Clone, Default, Record, ByteRepr)] +#[derive(Clone, Default, Record, ByteRepr, ZeroInitialization)] #[byte_size(size_of::<::libc::tm>())] pub struct Tm { #[offset(offset_of!(::libc::tm, tm_sec))] @@ -65,7 +65,7 @@ impl Tm { } } -#[derive(Clone, Default, Record, ByteRepr)] +#[derive(Clone, Default, Record, ByteRepr, ZeroInitialization)] #[byte_size(size_of::<::libc::timeval>())] pub struct Timeval { #[offset(offset_of!(::libc::timeval, tv_sec))] @@ -74,7 +74,7 @@ pub struct Timeval { pub tv_usec: i64, } -#[derive(Clone, Default, Record, ByteRepr)] +#[derive(Clone, Default, Record, ByteRepr, ZeroInitialization)] #[byte_size(size_of::<::libc::timespec>())] pub struct Timespec { #[offset(offset_of!(::libc::timespec, tv_sec))] diff --git a/tests/multi-file/cross_tu_tag_collision/out/refcount/cross_tu_tag_collision.rs b/tests/multi-file/cross_tu_tag_collision/out/refcount/cross_tu_tag_collision.rs index 9201e1a89c..e1598f3b78 100644 --- a/tests/multi-file/cross_tu_tag_collision/out/refcount/cross_tu_tag_collision.rs +++ b/tests/multi-file/cross_tu_tag_collision/out/refcount/cross_tu_tag_collision.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct widget { #[offset(0)] diff --git a/tests/multi-file/cross_tu_tag_collision/out/unsafe/cross_tu_tag_collision.rs b/tests/multi-file/cross_tu_tag_collision/out/unsafe/cross_tu_tag_collision.rs index 1b936448f6..a67aba4a6a 100644 --- a/tests/multi-file/cross_tu_tag_collision/out/unsafe/cross_tu_tag_collision.rs +++ b/tests/multi-file/cross_tu_tag_collision/out/unsafe/cross_tu_tag_collision.rs @@ -7,7 +7,7 @@ use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] pub struct widget { pub id: i32, } diff --git a/tests/multi-file/defaulted_move_cross_tu/out/refcount/defaulted_move_cross_tu.rs b/tests/multi-file/defaulted_move_cross_tu/out/refcount/defaulted_move_cross_tu.rs index d1300b878b..5b9a7e89c3 100644 --- a/tests/multi-file/defaulted_move_cross_tu/out/refcount/defaulted_move_cross_tu.rs +++ b/tests/multi-file/defaulted_move_cross_tu/out/refcount/defaulted_move_cross_tu.rs @@ -6,8 +6,8 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Record, ByteRepr, FnPtrArg, MoveCtor, CppFrom, MoveAssign)] -#[cpp_from(i32, new, value)] +#[derive(Record, ByteRepr, ZeroInitialization, FnPtrArg, MoveCtor, CppFrom, MoveAssign)] +#[cpp_from(i32, construct, value)] #[byte_size(32)] pub struct S { #[offset(0)] @@ -15,48 +15,23 @@ pub struct S { pub v: Vec, #[offset(24)] #[byte_size(8)] + #[array_len(2)] pub n: Box<[i32]>, } -impl S { - pub fn new(mut x: i32) -> Self { - Self { - v: vec![x; (x as usize)], - n: Box::new([x, (x + 1)]), - } - } - pub fn move_from(_a0: Ptr) -> Self { - Self { - v: field!(_a0, v).with_mut(std::mem::take), - n: Box::new(std::array::from_fn::<_, 2, _>(|__i: usize| { - (*_a0.upgrade().deref()).n[(__i) as usize] - })), - } - } -} impl Default for S { fn default() -> Self { - { - S { - v: Default::default(), - n: (0..2).map(|_| 0_i32).collect::>(), - } - } + { ::zero_init() } } } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, MoveCtor, MoveAssign, Default)] +#[derive( + Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, MoveCtor, MoveAssign, Default, +)] #[byte_size(24)] pub struct T { #[offset(0)] #[byte_size(24)] pub v: Vec, } -impl T { - pub fn move_from(_a0: Ptr) -> Self { - Self { - v: field!(_a0, v).with_mut(std::mem::take), - } - } -} pub fn sum_0(s: Ptr) -> i32 { return ({ ({ (((*s.upgrade().deref()).v.len()) as i32) } + { (array_elem!(s, n, 0).read()) }) @@ -67,7 +42,11 @@ pub fn main() { libcc2rs::cpp2rust_exit(main_0()); } fn main_0() -> i32 { - let s: Value = Rc::new(RefCell::new(S::new({ 2 }))); + let s: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + SImpl::construct(&__v.as_pointer(), { 2 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); assert!((({ sum_0(s.as_pointer(),) }) == 7)); assert!((({ shuffle_1(3,) }) == 10)); let t: Value = Rc::new(RefCell::new(T { @@ -77,14 +56,30 @@ fn main_0() -> i32 { return 0; } pub fn shuffle_1(mut x: i32) -> i32 { - let a: Value = Rc::new(RefCell::new(S::new({ x }))); - let b: Value = Rc::new(RefCell::new(S::move_from({ a.as_pointer() }))); + let a: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + SImpl::construct(&__v.as_pointer(), { x }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); + let b: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + SImpl::move_from(&__v.as_pointer(), { a.as_pointer() }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); assert!(({ (*a.borrow()).v.is_empty() })); - let c: Value = Rc::new(RefCell::new(S::new({ 1 }))); + let c: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + SImpl::construct(&__v.as_pointer(), { 1 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); ({ SImpl::move_assign(&c.as_pointer(), b.as_pointer()) }); assert!(({ (*b.borrow()).v.is_empty() })); let t: Value = Rc::new(RefCell::new(T { v: Vec::from([1]) })); - let u: Value = Rc::new(RefCell::new(T::move_from({ t.as_pointer() }))); + let u: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + TImpl::move_from(&__v.as_pointer(), { t.as_pointer() }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); let w: Value = Rc::new(RefCell::new(::default())); ({ TImpl::move_assign(&w.as_pointer(), u.as_pointer()) }); assert!( @@ -94,9 +89,21 @@ pub fn shuffle_1(mut x: i32) -> i32 { return ({ sum_0(c.as_pointer()) }); } pub trait SImpl { + fn construct(&self, x: i32); + fn move_from(&self, _a0: Ptr); fn move_assign(&self, _a0: Ptr) -> Ptr; } impl SImpl for Ptr { + fn construct(&self, mut x: i32) { + field!((*self), v).write(vec![x; (x as usize)]); + field!((*self), n).write(Box::new([x, (x + 1)])); + } + fn move_from(&self, _a0: Ptr) { + field!((*self), v).write(field!(_a0, v).with_mut(std::mem::take)); + field!((*self), n).write(Box::new(std::array::from_fn::<_, 2, _>(|__i: usize| { + (*_a0.upgrade().deref()).n[(__i) as usize] + }))); + } fn move_assign(&self, _a0: Ptr) -> Ptr { field!((*self), v).with_mut(|__b0| { (*__b0) = field!(_a0, v).with_mut(std::mem::take); @@ -111,12 +118,16 @@ impl SImpl for Ptr { } pub trait TImpl { fn size(&self) -> i32; + fn move_from(&self, _a0: Ptr); fn move_assign(&self, _a0: Ptr) -> Ptr; } impl TImpl for Ptr { fn size(&self) -> i32 { return (((*(*self).upgrade().deref()).v.len()) as i32); } + fn move_from(&self, _a0: Ptr) { + field!((*self), v).write(field!(_a0, v).with_mut(std::mem::take)); + } fn move_assign(&self, _a0: Ptr) -> Ptr { field!((*self), v).with_mut(|__b0| { (*__b0) = field!(_a0, v).with_mut(std::mem::take); diff --git a/tests/multi-file/defaulted_move_cross_tu/out/unsafe/defaulted_move_cross_tu.rs b/tests/multi-file/defaulted_move_cross_tu/out/unsafe/defaulted_move_cross_tu.rs index f910a40800..771f119c8b 100644 --- a/tests/multi-file/defaulted_move_cross_tu/out/unsafe/defaulted_move_cross_tu.rs +++ b/tests/multi-file/defaulted_move_cross_tu/out/unsafe/defaulted_move_cross_tu.rs @@ -7,26 +7,22 @@ use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; #[repr(C)] -#[derive(FnPtrArg, MoveCtorUnsafe, CppFromUnsafe, MoveAssignUnsafe, Default)] -#[cpp_from(i32, new, value)] +#[derive( + FnPtrArg, ZeroInitializationUnsafe, MoveCtorUnsafe, CppFromUnsafe, MoveAssignUnsafe, Default, +)] +#[cpp_from(i32, construct, value)] pub struct S { pub v: Vec, pub n: [i32; 2], } impl S { - pub unsafe fn new(mut x: i32) -> Self { - let mut this = Self { - v: vec![x; (x as usize)], - n: [x, ((x) + (1))], - }; - this + pub unsafe fn construct(&mut self, mut x: i32) { + (&raw mut self.v).write(vec![x; (x as usize)]); + (&raw mut self.n).write([x, ((x) + (1))]); } - pub unsafe fn move_from(_a0: *mut S) -> Self { - let mut this = Self { - v: std::mem::take(&mut (*_a0).v), - n: std::array::from_fn::<_, 2, _>(|__i: usize| (*_a0).n[(__i)]), - }; - this + pub unsafe fn move_from(&mut self, _a0: *mut S) { + (&raw mut self.v).write(std::mem::take(&mut (*_a0).v)); + (&raw mut self.n).write(std::array::from_fn::<_, 2, _>(|__i: usize| (*_a0).n[(__i)])); } pub unsafe fn move_assign(&mut self, _a0: *mut S) -> *mut S { { @@ -44,7 +40,9 @@ impl S { } } #[repr(C)] -#[derive(Clone, VaArg, FnPtrArg, MoveCtorUnsafe, MoveAssignUnsafe, Default)] +#[derive( + Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, MoveCtorUnsafe, MoveAssignUnsafe, Default, +)] pub struct T { pub v: Vec, } @@ -52,11 +50,8 @@ impl T { pub unsafe fn size(&self) -> i32 { return ((self.v.len()) as i32); } - pub unsafe fn move_from(_a0: *mut T) -> Self { - let mut this = Self { - v: std::mem::take(&mut (*_a0).v), - }; - this + pub unsafe fn move_from(&mut self, _a0: *mut T) { + (&raw mut self.v).write(std::mem::take(&mut (*_a0).v)); } pub unsafe fn move_assign(&mut self, _a0: *mut T) -> *mut T { { @@ -75,7 +70,11 @@ pub fn main() { } } unsafe fn main_0() -> i32 { - let mut s: S = S::new({ 2 }); + let mut s: S = { + let mut __v: S = ::zero_init(); + S::construct(&mut __v, { 2 }); + __v + }; assert!(((unsafe { sum_0(&s,) }) == (7))); assert!(((unsafe { shuffle_1(3,) }) == (10))); let mut t: T = T { @@ -85,14 +84,30 @@ unsafe fn main_0() -> i32 { return 0; } pub unsafe fn shuffle_1(mut x: i32) -> i32 { - let mut a: S = S::new({ x }); - let mut b: S = S::move_from({ &mut a }); + let mut a: S = { + let mut __v: S = ::zero_init(); + S::construct(&mut __v, { x }); + __v + }; + let mut b: S = { + let mut __v: S = ::zero_init(); + S::move_from(&mut __v, { &mut a }); + __v + }; assert!(a.v.is_empty()); - let mut c: S = S::new({ 1 }); + let mut c: S = { + let mut __v: S = ::zero_init(); + S::construct(&mut __v, { 1 }); + __v + }; (unsafe { S::move_assign(&mut c, &mut b) }); assert!(b.v.is_empty()); let mut t: T = T { v: Vec::from([1]) }; - let mut u: T = T::move_from({ &mut t }); + let mut u: T = { + let mut __v: T = ::zero_init(); + T::move_from(&mut __v, { &mut t }); + __v + }; let mut w: T = ::default(); (unsafe { T::move_assign(&mut w, &mut u) }); assert!( diff --git a/tests/multi-file/friend_operator/out/refcount/friend_operator.rs b/tests/multi-file/friend_operator/out/refcount/friend_operator.rs index e5e73b2b56..a5413a53c3 100644 --- a/tests/multi-file/friend_operator/out/refcount/friend_operator.rs +++ b/tests/multi-file/friend_operator/out/refcount/friend_operator.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct S { #[offset(0)] diff --git a/tests/multi-file/friend_operator/out/unsafe/friend_operator.rs b/tests/multi-file/friend_operator/out/unsafe/friend_operator.rs index 7369b411e6..121763987a 100644 --- a/tests/multi-file/friend_operator/out/unsafe/friend_operator.rs +++ b/tests/multi-file/friend_operator/out/unsafe/friend_operator.rs @@ -7,7 +7,7 @@ use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] pub struct S { pub a: i32, pub b: i32, diff --git a/tests/multi-file/opaque_forward_decl/out/refcount/opaque_forward_decl.rs b/tests/multi-file/opaque_forward_decl/out/refcount/opaque_forward_decl.rs index 11b4847dc7..bbba0d218a 100644 --- a/tests/multi-file/opaque_forward_decl/out/refcount/opaque_forward_decl.rs +++ b/tests/multi-file/opaque_forward_decl/out/refcount/opaque_forward_decl.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(16)] pub struct container { #[offset(0)] diff --git a/tests/multi-file/opaque_forward_decl/out/unsafe/opaque_forward_decl.rs b/tests/multi-file/opaque_forward_decl/out/unsafe/opaque_forward_decl.rs index b461300640..251e8e1290 100644 --- a/tests/multi-file/opaque_forward_decl/out/unsafe/opaque_forward_decl.rs +++ b/tests/multi-file/opaque_forward_decl/out/unsafe/opaque_forward_decl.rs @@ -7,7 +7,7 @@ use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] pub struct container { pub p: *mut opaque, pub x: i32, diff --git a/tests/multi-file/out_of_line_methods_cross_tu/out/refcount/out_of_line_methods_cross_tu.rs b/tests/multi-file/out_of_line_methods_cross_tu/out/refcount/out_of_line_methods_cross_tu.rs index 22ccc50635..025f1e0e89 100644 --- a/tests/multi-file/out_of_line_methods_cross_tu/out/refcount/out_of_line_methods_cross_tu.rs +++ b/tests/multi-file/out_of_line_methods_cross_tu/out/refcount/out_of_line_methods_cross_tu.rs @@ -6,24 +6,22 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct S { #[offset(0)] pub v: i32, } -impl S {} pub trait Base { fn apply(&mut self, x: i32) -> i32; } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(16)] pub struct Derived { #[offset(8)] pub factor: i32, } -impl Derived {} -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct Pair { #[offset(0)] @@ -39,12 +37,20 @@ pub fn main() { libcc2rs::cpp2rust_exit(main_0()); } fn main_0() -> i32 { - let s: Value = Rc::new(RefCell::new(S::new({ 1 }))); + let s: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + SImpl::construct(&__v.as_pointer(), { 1 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); assert!((({ SImpl::get(&s.as_pointer(),) }) == 1)); ({ SImpl::set(&s.as_pointer(), 4) }); assert!((({ SImpl::get(&s.as_pointer(),) }) == 4)); assert!((({ SImpl::add(&s.as_pointer(), 2,) }) == 6)); - let derived: Value = Rc::new(RefCell::new(Derived::new({ 3 }))); + let derived: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + DerivedImpl::construct(&__v.as_pointer(), { 3 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); let mut base: PtrDyn = (derived.as_pointer()).to_dyn::(|w| w); assert!((({ (*base.upgrade().deref_mut()).apply(5,) }) == 15)); let pair: Value = Rc::new(RefCell::new(Pair { @@ -55,16 +61,6 @@ fn main_0() -> i32 { assert!((({ pair_diff_0((pair.as_pointer()),) }) == 4)); return 0; } -impl S { - pub fn new(mut x: i32) -> Self { - Self { v: x } - } -} -impl Derived { - pub fn new(mut factor: i32) -> Self { - Self { factor: factor } - } -} impl S {} impl Derived {} pub fn pair_sum_1(mut p: Ptr) -> i32 { @@ -75,7 +71,20 @@ impl Base for Derived { return ({ self.factor } * x); } } +pub trait DerivedImpl { + fn construct(&self, mut factor: i32) { + unimplemented!() + } +} +impl DerivedImpl for Ptr { + fn construct(&self, mut factor: i32) { + field!((*self), factor).write(factor); + } +} pub trait SImpl { + fn construct(&self, mut x: i32) { + unimplemented!() + } fn destructor(&self) { unimplemented!() } @@ -91,6 +100,9 @@ impl SImpl for Ptr { fn get(&self) -> i32 { return (*self).with(|__s| __s.v); } + fn construct(&self, mut x: i32) { + field!((*self), v).write(x); + } fn destructor(&self) {} fn set(&self, mut x: i32) { field!((*self), v).write(x); diff --git a/tests/multi-file/out_of_line_methods_cross_tu/out/unsafe/out_of_line_methods_cross_tu.rs b/tests/multi-file/out_of_line_methods_cross_tu/out/unsafe/out_of_line_methods_cross_tu.rs index 6e78a3941e..a99bc9436a 100644 --- a/tests/multi-file/out_of_line_methods_cross_tu/out/unsafe/out_of_line_methods_cross_tu.rs +++ b/tests/multi-file/out_of_line_methods_cross_tu/out/unsafe/out_of_line_methods_cross_tu.rs @@ -7,7 +7,7 @@ use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] pub struct S { pub v: i32, } @@ -20,13 +20,13 @@ pub unsafe trait Base { unsafe fn apply(&mut self, x: i32) -> i32; } #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] pub struct Derived { pub factor: i32, } impl Derived {} #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] pub struct Pair { pub first: i32, pub second: i32, @@ -41,12 +41,20 @@ pub fn main() { } } unsafe fn main_0() -> i32 { - let mut s: S = S::new({ 1 }); + let mut s: S = { + let mut __v: S = ::zero_init(); + S::construct(&mut __v, { 1 }); + __v + }; assert!(((unsafe { S::get(&s,) }) == (1))); (unsafe { S::set(&mut s, 4) }); assert!(((unsafe { S::get(&s,) }) == (4))); assert!(((unsafe { S::add(&mut s, 2,) }) == (6))); - let mut derived: Derived = Derived::new({ 3 }); + let mut derived: Derived = { + let mut __v: Derived = ::zero_init(); + Derived::construct(&mut __v, { 3 }); + __v + }; let mut base: *mut dyn Base = (&mut derived as *mut Derived); assert!(((unsafe { (*base).apply(5,) }) == (15))); let mut pair: Pair = Pair { @@ -58,15 +66,13 @@ unsafe fn main_0() -> i32 { return 0; } impl S { - pub unsafe fn new(mut x: i32) -> Self { - let mut this = Self { v: x }; - this + pub unsafe fn construct(&mut self, mut x: i32) { + (&raw mut self.v).write(x); } } impl Derived { - pub unsafe fn new(mut factor: i32) -> Self { - let mut this = Self { factor: factor }; - this + pub unsafe fn construct(&mut self, mut factor: i32) { + (&raw mut self.factor).write(factor); } } impl S {} diff --git a/tests/multi-file/template_method_late_instantiation/out/refcount/template_method_late_instantiation.rs b/tests/multi-file/template_method_late_instantiation/out/refcount/template_method_late_instantiation.rs index d7c1724f96..a43e8c01ad 100644 --- a/tests/multi-file/template_method_late_instantiation/out/refcount/template_method_late_instantiation.rs +++ b/tests/multi-file/template_method_late_instantiation/out/refcount/template_method_late_instantiation.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg)] #[byte_size(4)] pub struct S_int_ { #[offset(0)] diff --git a/tests/multi-file/template_method_late_instantiation/out/unsafe/template_method_late_instantiation.rs b/tests/multi-file/template_method_late_instantiation/out/unsafe/template_method_late_instantiation.rs index 6c56b50843..a6bebb806c 100644 --- a/tests/multi-file/template_method_late_instantiation/out/unsafe/template_method_late_instantiation.rs +++ b/tests/multi-file/template_method_late_instantiation/out/unsafe/template_method_late_instantiation.rs @@ -7,7 +7,7 @@ use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe)] pub struct S_int_ { pub x: i32, } diff --git a/tests/std/basic/basic.def.odr/p14-class-in-two-files/out/refcount/p14-class-in-two-files.rs b/tests/std/basic/basic.def.odr/p14-class-in-two-files/out/refcount/p14-class-in-two-files.rs index bc4d00540f..21d1ed5c32 100644 --- a/tests/std/basic/basic.def.odr/p14-class-in-two-files/out/refcount/p14-class-in-two-files.rs +++ b/tests/std/basic/basic.def.odr/p14-class-in-two-files/out/refcount/p14-class-in-two-files.rs @@ -18,7 +18,7 @@ thread_local!( thread_local!( pub static useCount_3: Value = Rc::new(RefCell::new(0)); ); -#[derive(Record, ByteRepr, VaArg, FnPtrArg, CopyAssign, Destructible)] +#[derive(Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, CopyAssign, Destructible)] #[byte_size(4)] pub struct LifetimeTracked { #[offset(0)] @@ -43,31 +43,30 @@ impl LifetimeTracked { } }); } - pub fn new() -> Self { - let __this: LifetimeTracked = Self { uses: 0 }; - (*ctorCount_0.with(Value::clone).borrow_mut()).prefix_inc(); - __this - } - pub fn copy_from(_a0: Ptr) -> Self { - let __this: LifetimeTracked = Self { uses: 0 }; - (*copyCount_1.with(Value::clone).borrow_mut()).prefix_inc(); - __this - } } impl Clone for LifetimeTracked { fn clone(&self) -> Self { let __src: Value = Rc::new(RefCell::new(LifetimeTracked { uses: self.uses.clone(), })); - LifetimeTracked::copy_from(__src.as_pointer()) + let __v: Value = Rc::new(RefCell::new(::zero_init())); + LifetimeTrackedImpl::copy_from(&__v.as_pointer(), __src.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() } } impl Default for LifetimeTracked { fn default() -> Self { - { LifetimeTracked::new() } + { + { + let __v: Value = + Rc::new(RefCell::new(::zero_init())); + LifetimeTrackedImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + } + } } } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Destructible, Default)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Destructible, Default)] #[byte_size(4)] pub struct AssertsDtorCountAtExit { #[offset(0)] @@ -84,14 +83,18 @@ thread_local!( ); thread_local!( pub static shared_5: Value = { - let __static = Rc::new(RefCell::new(LifetimeTracked::new())); + let __static = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + LifetimeTrackedImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); libcc2rs::register_static_destructor(|| { Destructible::destroy(shared_5.with(Value::clone).as_pointer()) }); __static }; ); -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(1)] pub struct HasInlineMembers {} impl HasInlineMembers { @@ -101,7 +104,11 @@ impl HasInlineMembers { } thread_local!( static local_6: Value = { - let __static = Rc::new(RefCell::new(LifetimeTracked::new())); + let __static = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + LifetimeTrackedImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); libcc2rs::register_static_destructor(|| { Destructible::destroy(local_6.with(Value::clone).as_pointer()) }); @@ -143,11 +150,21 @@ impl AssertsDtorCountAtExitImpl for Ptr { } } pub trait LifetimeTrackedImpl { + fn construct(&self); + fn copy_from(&self, _a0: Ptr); fn destructor(&self); fn use_(&self); fn copy_assign(&self, _a0: Ptr) -> Ptr; } impl LifetimeTrackedImpl for Ptr { + fn construct(&self) { + field!((*self), uses).write(0); + (*ctorCount_0.with(Value::clone).borrow_mut()).prefix_inc(); + } + fn copy_from(&self, _a0: Ptr) { + field!((*self), uses).write(0); + (*copyCount_1.with(Value::clone).borrow_mut()).prefix_inc(); + } fn destructor(&self) { (*dtorCount_2.with(Value::clone).borrow_mut()).prefix_inc(); } diff --git a/tests/std/basic/basic.def.odr/p14-class-in-two-files/out/unsafe/p14-class-in-two-files.rs b/tests/std/basic/basic.def.odr/p14-class-in-two-files/out/unsafe/p14-class-in-two-files.rs index 5fa5dc515f..cefa6b408a 100644 --- a/tests/std/basic/basic.def.odr/p14-class-in-two-files/out/unsafe/p14-class-in-two-files.rs +++ b/tests/std/basic/basic.def.odr/p14-class-in-two-files/out/unsafe/p14-class-in-two-files.rs @@ -11,7 +11,7 @@ pub static mut copyCount_1: std::cell::LazyCell = std::cell::LazyCell::new( pub static mut dtorCount_2: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); pub static mut useCount_3: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); #[repr(C)] -#[derive(VaArg, FnPtrArg, CopyAssignUnsafe, DestructibleUnsafe)] +#[derive(VaArg, FnPtrArg, ZeroInitializationUnsafe, CopyAssignUnsafe, DestructibleUnsafe)] pub struct LifetimeTracked { pub uses: i32, } @@ -28,15 +28,13 @@ impl LifetimeTracked { (*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_1)) }; } - pub unsafe fn new() -> Self { - let mut this = Self { uses: 0 }; + pub unsafe fn construct(&mut self) { + (&raw mut self.uses).write(0); (*std::cell::LazyCell::force_mut(&mut *&raw mut ctorCount_0)).prefix_inc(); - this } - pub unsafe fn copy_from(_a0: *const LifetimeTracked) -> Self { - let mut this = Self { uses: 0 }; + pub unsafe fn copy_from(&mut self, _a0: *const LifetimeTracked) { + (&raw mut self.uses).write(0); (*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_1)).prefix_inc(); - this } pub unsafe fn destructor(&mut self) { (*std::cell::LazyCell::force_mut(&mut *&raw mut dtorCount_2)).prefix_inc(); @@ -52,16 +50,26 @@ impl LifetimeTracked { } impl Clone for LifetimeTracked { fn clone(&self) -> Self { - unsafe { LifetimeTracked::copy_from(self as *const LifetimeTracked) } + unsafe { + let mut __v: LifetimeTracked = ::zero_init(); + LifetimeTracked::copy_from(&mut __v, self as *const LifetimeTracked); + __v + } } } impl Default for LifetimeTracked { fn default() -> Self { - unsafe { LifetimeTracked::new() } + unsafe { + { + let mut __v: LifetimeTracked = ::zero_init(); + LifetimeTracked::construct(&mut __v); + __v + } + } } } #[repr(C)] -#[derive(Clone, VaArg, FnPtrArg, DestructibleUnsafe, Default)] +#[derive(Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, DestructibleUnsafe, Default)] pub struct AssertsDtorCountAtExit { pub expected: i32, } @@ -85,7 +93,11 @@ pub static mut check_4: std::cell::LazyCell = pub static mut shared_5: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { { - let __static = LifetimeTracked::new(); + let __static = { + let mut __v: LifetimeTracked = ::zero_init(); + LifetimeTracked::construct(&mut __v); + __v + }; libcc2rs::register_static_destructor(|| { DestructibleUnsafe::destroy(&raw mut *std::cell::LazyCell::force_mut( &mut *&raw mut shared_5, @@ -95,7 +107,7 @@ pub static mut shared_5: std::cell::LazyCell = } }); #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] pub struct HasInlineMembers {} impl HasInlineMembers { pub unsafe fn instance() -> *mut LifetimeTracked { @@ -104,7 +116,11 @@ impl HasInlineMembers { } static mut local_6: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { { - let __static = LifetimeTracked::new(); + let __static = { + let mut __v: LifetimeTracked = ::zero_init(); + LifetimeTracked::construct(&mut __v); + __v + }; libcc2rs::register_static_destructor(|| { DestructibleUnsafe::destroy(&raw mut *std::cell::LazyCell::force_mut( &mut *&raw mut local_6, diff --git a/tests/std/basic/basic.def.odr/p15-class-template-static-member/out/refcount/p15-class-template-static-member.rs b/tests/std/basic/basic.def.odr/p15-class-template-static-member/out/refcount/p15-class-template-static-member.rs index 7701973590..0d60533295 100644 --- a/tests/std/basic/basic.def.odr/p15-class-template-static-member/out/refcount/p15-class-template-static-member.rs +++ b/tests/std/basic/basic.def.odr/p15-class-template-static-member/out/refcount/p15-class-template-static-member.rs @@ -18,7 +18,7 @@ thread_local!( thread_local!( pub static useCount_3: Value = Rc::new(RefCell::new(0)); ); -#[derive(Record, ByteRepr, VaArg, FnPtrArg, CopyAssign, Destructible)] +#[derive(Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, CopyAssign, Destructible)] #[byte_size(4)] pub struct LifetimeTracked { #[offset(0)] @@ -43,52 +43,59 @@ impl LifetimeTracked { } }); } - pub fn new() -> Self { - let __this: LifetimeTracked = Self { uses: 0 }; - (*ctorCount_0.with(Value::clone).borrow_mut()).prefix_inc(); - __this - } - pub fn copy_from(_a0: Ptr) -> Self { - let __this: LifetimeTracked = Self { uses: 0 }; - (*copyCount_1.with(Value::clone).borrow_mut()).prefix_inc(); - __this - } } impl Clone for LifetimeTracked { fn clone(&self) -> Self { let __src: Value = Rc::new(RefCell::new(LifetimeTracked { uses: self.uses.clone(), })); - LifetimeTracked::copy_from(__src.as_pointer()) + let __v: Value = Rc::new(RefCell::new(::zero_init())); + LifetimeTrackedImpl::copy_from(&__v.as_pointer(), __src.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() } } impl Default for LifetimeTracked { fn default() -> Self { - { LifetimeTracked::new() } + { + { + let __v: Value = + Rc::new(RefCell::new(::zero_init())); + LifetimeTrackedImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + } + } } } thread_local!( pub static member_4: Value = { - let __static = Rc::new(RefCell::new(LifetimeTracked::new())); + let __static = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + LifetimeTrackedImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); libcc2rs::register_static_destructor(|| { Destructible::destroy(member_4.with(Value::clone).as_pointer()) }); __static }; ); -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(1)] pub struct HasStaticMember_int_ {} thread_local!( pub static member_5: Value = { - let __static = Rc::new(RefCell::new(LifetimeTracked::new())); + let __static = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + LifetimeTrackedImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); libcc2rs::register_static_destructor(|| { Destructible::destroy(member_5.with(Value::clone).as_pointer()) }); __static }; ); -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(1)] pub struct HasStaticMember_long_ {} pub fn main() { @@ -116,11 +123,21 @@ pub fn b_int_address_8() -> Ptr { return (member_4.with(|v| v.as_pointer())); } pub trait LifetimeTrackedImpl { + fn construct(&self); + fn copy_from(&self, _a0: Ptr); fn destructor(&self); fn use_(&self); fn copy_assign(&self, _a0: Ptr) -> Ptr; } impl LifetimeTrackedImpl for Ptr { + fn construct(&self) { + field!((*self), uses).write(0); + (*ctorCount_0.with(Value::clone).borrow_mut()).prefix_inc(); + } + fn copy_from(&self, _a0: Ptr) { + field!((*self), uses).write(0); + (*copyCount_1.with(Value::clone).borrow_mut()).prefix_inc(); + } fn destructor(&self) { (*dtorCount_2.with(Value::clone).borrow_mut()).prefix_inc(); } diff --git a/tests/std/basic/basic.def.odr/p15-class-template-static-member/out/unsafe/p15-class-template-static-member.rs b/tests/std/basic/basic.def.odr/p15-class-template-static-member/out/unsafe/p15-class-template-static-member.rs index 3bb6868cbe..34e5ce487f 100644 --- a/tests/std/basic/basic.def.odr/p15-class-template-static-member/out/unsafe/p15-class-template-static-member.rs +++ b/tests/std/basic/basic.def.odr/p15-class-template-static-member/out/unsafe/p15-class-template-static-member.rs @@ -11,7 +11,7 @@ pub static mut copyCount_1: std::cell::LazyCell = std::cell::LazyCell::new( pub static mut dtorCount_2: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); pub static mut useCount_3: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); #[repr(C)] -#[derive(VaArg, FnPtrArg, CopyAssignUnsafe, DestructibleUnsafe)] +#[derive(VaArg, FnPtrArg, ZeroInitializationUnsafe, CopyAssignUnsafe, DestructibleUnsafe)] pub struct LifetimeTracked { pub uses: i32, } @@ -28,15 +28,13 @@ impl LifetimeTracked { (*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_1)) }; } - pub unsafe fn new() -> Self { - let mut this = Self { uses: 0 }; + pub unsafe fn construct(&mut self) { + (&raw mut self.uses).write(0); (*std::cell::LazyCell::force_mut(&mut *&raw mut ctorCount_0)).prefix_inc(); - this } - pub unsafe fn copy_from(_a0: *const LifetimeTracked) -> Self { - let mut this = Self { uses: 0 }; + pub unsafe fn copy_from(&mut self, _a0: *const LifetimeTracked) { + (&raw mut self.uses).write(0); (*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_1)).prefix_inc(); - this } pub unsafe fn destructor(&mut self) { (*std::cell::LazyCell::force_mut(&mut *&raw mut dtorCount_2)).prefix_inc(); @@ -52,18 +50,32 @@ impl LifetimeTracked { } impl Clone for LifetimeTracked { fn clone(&self) -> Self { - unsafe { LifetimeTracked::copy_from(self as *const LifetimeTracked) } + unsafe { + let mut __v: LifetimeTracked = ::zero_init(); + LifetimeTracked::copy_from(&mut __v, self as *const LifetimeTracked); + __v + } } } impl Default for LifetimeTracked { fn default() -> Self { - unsafe { LifetimeTracked::new() } + unsafe { + { + let mut __v: LifetimeTracked = ::zero_init(); + LifetimeTracked::construct(&mut __v); + __v + } + } } } pub static mut member_4: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { { - let __static = LifetimeTracked::new(); + let __static = { + let mut __v: LifetimeTracked = ::zero_init(); + LifetimeTracked::construct(&mut __v); + __v + }; libcc2rs::register_static_destructor(|| { DestructibleUnsafe::destroy(&raw mut *std::cell::LazyCell::force_mut( &mut *&raw mut member_4, @@ -73,12 +85,16 @@ pub static mut member_4: std::cell::LazyCell = } }); #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] pub struct HasStaticMember_int_ {} pub static mut member_5: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { { - let __static = LifetimeTracked::new(); + let __static = { + let mut __v: LifetimeTracked = ::zero_init(); + LifetimeTracked::construct(&mut __v); + __v + }; libcc2rs::register_static_destructor(|| { DestructibleUnsafe::destroy(&raw mut *std::cell::LazyCell::force_mut( &mut *&raw mut member_5, @@ -88,7 +104,7 @@ pub static mut member_5: std::cell::LazyCell = } }); #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] pub struct HasStaticMember_long_ {} pub fn main() { unsafe { diff --git a/tests/std/basic/basic.def.odr/p15-function-template/out/refcount/p15-function-template.rs b/tests/std/basic/basic.def.odr/p15-function-template/out/refcount/p15-function-template.rs index a6c68ff31a..e9dd3137fd 100644 --- a/tests/std/basic/basic.def.odr/p15-function-template/out/refcount/p15-function-template.rs +++ b/tests/std/basic/basic.def.odr/p15-function-template/out/refcount/p15-function-template.rs @@ -18,7 +18,7 @@ thread_local!( thread_local!( pub static useCount_3: Value = Rc::new(RefCell::new(0)); ); -#[derive(Record, ByteRepr, VaArg, FnPtrArg, CopyAssign, Destructible)] +#[derive(Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, CopyAssign, Destructible)] #[byte_size(4)] pub struct LifetimeTracked { #[offset(0)] @@ -43,31 +43,30 @@ impl LifetimeTracked { } }); } - pub fn new() -> Self { - let __this: LifetimeTracked = Self { uses: 0 }; - (*ctorCount_0.with(Value::clone).borrow_mut()).prefix_inc(); - __this - } - pub fn copy_from(_a0: Ptr) -> Self { - let __this: LifetimeTracked = Self { uses: 0 }; - (*copyCount_1.with(Value::clone).borrow_mut()).prefix_inc(); - __this - } } impl Clone for LifetimeTracked { fn clone(&self) -> Self { let __src: Value = Rc::new(RefCell::new(LifetimeTracked { uses: self.uses.clone(), })); - LifetimeTracked::copy_from(__src.as_pointer()) + let __v: Value = Rc::new(RefCell::new(::zero_init())); + LifetimeTrackedImpl::copy_from(&__v.as_pointer(), __src.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() } } impl Default for LifetimeTracked { fn default() -> Self { - { LifetimeTracked::new() } + { + { + let __v: Value = + Rc::new(RefCell::new(::zero_init())); + LifetimeTrackedImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + } + } } } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Destructible, Default)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Destructible, Default)] #[byte_size(4)] pub struct AssertsDtorCountAtExit { #[offset(0)] @@ -84,7 +83,11 @@ pub fn count_10() -> i32 { } thread_local!( static t_5: Value = { - let __static = Rc::new(RefCell::new(LifetimeTracked::new())); + let __static = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + LifetimeTrackedImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); libcc2rs::register_static_destructor(|| { Destructible::destroy(t_5.with(Value::clone).as_pointer()) }); @@ -96,7 +99,11 @@ thread_local!( ); thread_local!( static t_8: Value = { - let __static = Rc::new(RefCell::new(LifetimeTracked::new())); + let __static = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + LifetimeTrackedImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); libcc2rs::register_static_destructor(|| { Destructible::destroy(t_8.with(Value::clone).as_pointer()) }); @@ -108,7 +115,11 @@ thread_local!( ); thread_local!( static t_11: Value = { - let __static = Rc::new(RefCell::new(LifetimeTracked::new())); + let __static = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + LifetimeTrackedImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); libcc2rs::register_static_destructor(|| { Destructible::destroy(t_11.with(Value::clone).as_pointer()) }); @@ -145,7 +156,7 @@ fn main_0() -> i32 { assert!((ctorCount_0.with(|rc| *rc.borrow()) == 4)); return 0; } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(1)] pub struct S_14 {} pub fn count_18() -> i32 { @@ -153,7 +164,11 @@ pub fn count_18() -> i32 { } thread_local!( static t_19: Value = { - let __static = Rc::new(RefCell::new(LifetimeTracked::new())); + let __static = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + LifetimeTrackedImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); libcc2rs::register_static_destructor(|| { Destructible::destroy(t_19.with(Value::clone).as_pointer()) }); @@ -172,7 +187,7 @@ pub fn b_count_long_17() -> i32 { pub fn b_count_local_15() -> i32 { return ({ count_18() }); } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(1)] pub struct S_21 {} pub trait AssertsDtorCountAtExitImpl { @@ -184,11 +199,21 @@ impl AssertsDtorCountAtExitImpl for Ptr { } } pub trait LifetimeTrackedImpl { + fn construct(&self); + fn copy_from(&self, _a0: Ptr); fn destructor(&self); fn use_(&self); fn copy_assign(&self, _a0: Ptr) -> Ptr; } impl LifetimeTrackedImpl for Ptr { + fn construct(&self) { + field!((*self), uses).write(0); + (*ctorCount_0.with(Value::clone).borrow_mut()).prefix_inc(); + } + fn copy_from(&self, _a0: Ptr) { + field!((*self), uses).write(0); + (*copyCount_1.with(Value::clone).borrow_mut()).prefix_inc(); + } fn destructor(&self) { (*dtorCount_2.with(Value::clone).borrow_mut()).prefix_inc(); } diff --git a/tests/std/basic/basic.def.odr/p15-function-template/out/unsafe/p15-function-template.rs b/tests/std/basic/basic.def.odr/p15-function-template/out/unsafe/p15-function-template.rs index e334d9be49..3ac419bcf0 100644 --- a/tests/std/basic/basic.def.odr/p15-function-template/out/unsafe/p15-function-template.rs +++ b/tests/std/basic/basic.def.odr/p15-function-template/out/unsafe/p15-function-template.rs @@ -11,7 +11,7 @@ pub static mut copyCount_1: std::cell::LazyCell = std::cell::LazyCell::new( pub static mut dtorCount_2: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); pub static mut useCount_3: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); #[repr(C)] -#[derive(VaArg, FnPtrArg, CopyAssignUnsafe, DestructibleUnsafe)] +#[derive(VaArg, FnPtrArg, ZeroInitializationUnsafe, CopyAssignUnsafe, DestructibleUnsafe)] pub struct LifetimeTracked { pub uses: i32, } @@ -28,15 +28,13 @@ impl LifetimeTracked { (*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_1)) }; } - pub unsafe fn new() -> Self { - let mut this = Self { uses: 0 }; + pub unsafe fn construct(&mut self) { + (&raw mut self.uses).write(0); (*std::cell::LazyCell::force_mut(&mut *&raw mut ctorCount_0)).prefix_inc(); - this } - pub unsafe fn copy_from(_a0: *const LifetimeTracked) -> Self { - let mut this = Self { uses: 0 }; + pub unsafe fn copy_from(&mut self, _a0: *const LifetimeTracked) { + (&raw mut self.uses).write(0); (*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_1)).prefix_inc(); - this } pub unsafe fn destructor(&mut self) { (*std::cell::LazyCell::force_mut(&mut *&raw mut dtorCount_2)).prefix_inc(); @@ -52,16 +50,26 @@ impl LifetimeTracked { } impl Clone for LifetimeTracked { fn clone(&self) -> Self { - unsafe { LifetimeTracked::copy_from(self as *const LifetimeTracked) } + unsafe { + let mut __v: LifetimeTracked = ::zero_init(); + LifetimeTracked::copy_from(&mut __v, self as *const LifetimeTracked); + __v + } } } impl Default for LifetimeTracked { fn default() -> Self { - unsafe { LifetimeTracked::new() } + unsafe { + { + let mut __v: LifetimeTracked = ::zero_init(); + LifetimeTracked::construct(&mut __v); + __v + } + } } } #[repr(C)] -#[derive(Clone, VaArg, FnPtrArg, DestructibleUnsafe, Default)] +#[derive(Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, DestructibleUnsafe, Default)] pub struct AssertsDtorCountAtExit { pub expected: i32, } @@ -81,7 +89,11 @@ pub unsafe fn count_10() -> i32 { } static mut t_5: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { { - let __static = LifetimeTracked::new(); + let __static = { + let mut __v: LifetimeTracked = ::zero_init(); + LifetimeTracked::construct(&mut __v); + __v + }; libcc2rs::register_static_destructor(|| { DestructibleUnsafe::destroy(&raw mut *std::cell::LazyCell::force_mut( &mut *&raw mut t_5, @@ -93,7 +105,11 @@ static mut t_5: std::cell::LazyCell = std::cell::LazyCell::new( static mut n_6: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); static mut t_8: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { { - let __static = LifetimeTracked::new(); + let __static = { + let mut __v: LifetimeTracked = ::zero_init(); + LifetimeTracked::construct(&mut __v); + __v + }; libcc2rs::register_static_destructor(|| { DestructibleUnsafe::destroy(&raw mut *std::cell::LazyCell::force_mut( &mut *&raw mut t_8, @@ -105,7 +121,11 @@ static mut t_8: std::cell::LazyCell = std::cell::LazyCell::new( static mut n_9: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); static mut t_11: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { { - let __static = LifetimeTracked::new(); + let __static = { + let mut __v: LifetimeTracked = ::zero_init(); + LifetimeTracked::construct(&mut __v); + __v + }; libcc2rs::register_static_destructor(|| { DestructibleUnsafe::destroy(&raw mut *std::cell::LazyCell::force_mut( &mut *&raw mut t_11, @@ -148,14 +168,18 @@ unsafe fn main_0() -> i32 { return 0; } #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] pub struct S_14 {} pub unsafe fn count_18() -> i32 { return (*(*std::cell::LazyCell::force_mut(&mut *&raw mut n_20)).prefix_inc()); } static mut t_19: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { { - let __static = LifetimeTracked::new(); + let __static = { + let mut __v: LifetimeTracked = ::zero_init(); + LifetimeTracked::construct(&mut __v); + __v + }; libcc2rs::register_static_destructor(|| { DestructibleUnsafe::destroy(&raw mut *std::cell::LazyCell::force_mut( &mut *&raw mut t_19, @@ -175,7 +199,7 @@ pub unsafe fn b_count_local_15() -> i32 { return (unsafe { count_18() }); } #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] pub struct S_21 {} pub unsafe fn __cpp2rust_init_globals() { std::cell::LazyCell::force(&*&raw const ctorCount_0); diff --git a/tests/std/basic/basic.def.odr/p15-inline-function/out/refcount/p15-inline-function.rs b/tests/std/basic/basic.def.odr/p15-inline-function/out/refcount/p15-inline-function.rs index 43d1e497a9..d8c65b58ff 100644 --- a/tests/std/basic/basic.def.odr/p15-inline-function/out/refcount/p15-inline-function.rs +++ b/tests/std/basic/basic.def.odr/p15-inline-function/out/refcount/p15-inline-function.rs @@ -18,7 +18,7 @@ thread_local!( thread_local!( pub static useCount_3: Value = Rc::new(RefCell::new(0)); ); -#[derive(Record, ByteRepr, VaArg, FnPtrArg, CopyAssign, Destructible)] +#[derive(Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, CopyAssign, Destructible)] #[byte_size(4)] pub struct LifetimeTracked { #[offset(0)] @@ -43,31 +43,30 @@ impl LifetimeTracked { } }); } - pub fn new() -> Self { - let __this: LifetimeTracked = Self { uses: 0 }; - (*ctorCount_0.with(Value::clone).borrow_mut()).prefix_inc(); - __this - } - pub fn copy_from(_a0: Ptr) -> Self { - let __this: LifetimeTracked = Self { uses: 0 }; - (*copyCount_1.with(Value::clone).borrow_mut()).prefix_inc(); - __this - } } impl Clone for LifetimeTracked { fn clone(&self) -> Self { let __src: Value = Rc::new(RefCell::new(LifetimeTracked { uses: self.uses.clone(), })); - LifetimeTracked::copy_from(__src.as_pointer()) + let __v: Value = Rc::new(RefCell::new(::zero_init())); + LifetimeTrackedImpl::copy_from(&__v.as_pointer(), __src.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() } } impl Default for LifetimeTracked { fn default() -> Self { - { LifetimeTracked::new() } + { + { + let __v: Value = + Rc::new(RefCell::new(::zero_init())); + LifetimeTrackedImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + } + } } } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Destructible, Default)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Destructible, Default)] #[byte_size(4)] pub struct AssertsDtorCountAtExit { #[offset(0)] @@ -78,7 +77,11 @@ pub fn instance_4() -> Ptr { } thread_local!( static t_5: Value = { - let __static = Rc::new(RefCell::new(LifetimeTracked::new())); + let __static = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + LifetimeTrackedImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); libcc2rs::register_static_destructor(|| { Destructible::destroy(t_5.with(Value::clone).as_pointer()) }); @@ -125,11 +128,21 @@ impl AssertsDtorCountAtExitImpl for Ptr { } } pub trait LifetimeTrackedImpl { + fn construct(&self); + fn copy_from(&self, _a0: Ptr); fn destructor(&self); fn use_(&self); fn copy_assign(&self, _a0: Ptr) -> Ptr; } impl LifetimeTrackedImpl for Ptr { + fn construct(&self) { + field!((*self), uses).write(0); + (*ctorCount_0.with(Value::clone).borrow_mut()).prefix_inc(); + } + fn copy_from(&self, _a0: Ptr) { + field!((*self), uses).write(0); + (*copyCount_1.with(Value::clone).borrow_mut()).prefix_inc(); + } fn destructor(&self) { (*dtorCount_2.with(Value::clone).borrow_mut()).prefix_inc(); } diff --git a/tests/std/basic/basic.def.odr/p15-inline-function/out/unsafe/p15-inline-function.rs b/tests/std/basic/basic.def.odr/p15-inline-function/out/unsafe/p15-inline-function.rs index 692ba3aea6..a9d8bb2429 100644 --- a/tests/std/basic/basic.def.odr/p15-inline-function/out/unsafe/p15-inline-function.rs +++ b/tests/std/basic/basic.def.odr/p15-inline-function/out/unsafe/p15-inline-function.rs @@ -11,7 +11,7 @@ pub static mut copyCount_1: std::cell::LazyCell = std::cell::LazyCell::new( pub static mut dtorCount_2: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); pub static mut useCount_3: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); #[repr(C)] -#[derive(VaArg, FnPtrArg, CopyAssignUnsafe, DestructibleUnsafe)] +#[derive(VaArg, FnPtrArg, ZeroInitializationUnsafe, CopyAssignUnsafe, DestructibleUnsafe)] pub struct LifetimeTracked { pub uses: i32, } @@ -28,15 +28,13 @@ impl LifetimeTracked { (*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_1)) }; } - pub unsafe fn new() -> Self { - let mut this = Self { uses: 0 }; + pub unsafe fn construct(&mut self) { + (&raw mut self.uses).write(0); (*std::cell::LazyCell::force_mut(&mut *&raw mut ctorCount_0)).prefix_inc(); - this } - pub unsafe fn copy_from(_a0: *const LifetimeTracked) -> Self { - let mut this = Self { uses: 0 }; + pub unsafe fn copy_from(&mut self, _a0: *const LifetimeTracked) { + (&raw mut self.uses).write(0); (*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_1)).prefix_inc(); - this } pub unsafe fn destructor(&mut self) { (*std::cell::LazyCell::force_mut(&mut *&raw mut dtorCount_2)).prefix_inc(); @@ -52,16 +50,26 @@ impl LifetimeTracked { } impl Clone for LifetimeTracked { fn clone(&self) -> Self { - unsafe { LifetimeTracked::copy_from(self as *const LifetimeTracked) } + unsafe { + let mut __v: LifetimeTracked = ::zero_init(); + LifetimeTracked::copy_from(&mut __v, self as *const LifetimeTracked); + __v + } } } impl Default for LifetimeTracked { fn default() -> Self { - unsafe { LifetimeTracked::new() } + unsafe { + { + let mut __v: LifetimeTracked = ::zero_init(); + LifetimeTracked::construct(&mut __v); + __v + } + } } } #[repr(C)] -#[derive(Clone, VaArg, FnPtrArg, DestructibleUnsafe, Default)] +#[derive(Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, DestructibleUnsafe, Default)] pub struct AssertsDtorCountAtExit { pub expected: i32, } @@ -75,7 +83,11 @@ pub unsafe fn instance_4() -> *mut LifetimeTracked { } static mut t_5: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { { - let __static = LifetimeTracked::new(); + let __static = { + let mut __v: LifetimeTracked = ::zero_init(); + LifetimeTracked::construct(&mut __v); + __v + }; libcc2rs::register_static_destructor(|| { DestructibleUnsafe::destroy(&raw mut *std::cell::LazyCell::force_mut( &mut *&raw mut t_5, diff --git a/tests/std/basic/basic.def.odr/p15-inline-variable/out/refcount/p15-inline-variable.rs b/tests/std/basic/basic.def.odr/p15-inline-variable/out/refcount/p15-inline-variable.rs index f69412b774..34ebfc62f9 100644 --- a/tests/std/basic/basic.def.odr/p15-inline-variable/out/refcount/p15-inline-variable.rs +++ b/tests/std/basic/basic.def.odr/p15-inline-variable/out/refcount/p15-inline-variable.rs @@ -18,7 +18,7 @@ thread_local!( thread_local!( pub static useCount_3: Value = Rc::new(RefCell::new(0)); ); -#[derive(Record, ByteRepr, VaArg, FnPtrArg, CopyAssign, Destructible)] +#[derive(Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, CopyAssign, Destructible)] #[byte_size(4)] pub struct LifetimeTracked { #[offset(0)] @@ -43,31 +43,30 @@ impl LifetimeTracked { } }); } - pub fn new() -> Self { - let __this: LifetimeTracked = Self { uses: 0 }; - (*ctorCount_0.with(Value::clone).borrow_mut()).prefix_inc(); - __this - } - pub fn copy_from(_a0: Ptr) -> Self { - let __this: LifetimeTracked = Self { uses: 0 }; - (*copyCount_1.with(Value::clone).borrow_mut()).prefix_inc(); - __this - } } impl Clone for LifetimeTracked { fn clone(&self) -> Self { let __src: Value = Rc::new(RefCell::new(LifetimeTracked { uses: self.uses.clone(), })); - LifetimeTracked::copy_from(__src.as_pointer()) + let __v: Value = Rc::new(RefCell::new(::zero_init())); + LifetimeTrackedImpl::copy_from(&__v.as_pointer(), __src.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() } } impl Default for LifetimeTracked { fn default() -> Self { - { LifetimeTracked::new() } + { + { + let __v: Value = + Rc::new(RefCell::new(::zero_init())); + LifetimeTrackedImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + } + } } } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Destructible, Default)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Destructible, Default)] #[byte_size(4)] pub struct AssertsDtorCountAtExit { #[offset(0)] @@ -84,7 +83,11 @@ thread_local!( ); thread_local!( pub static shared_5: Value = { - let __static = Rc::new(RefCell::new(LifetimeTracked::new())); + let __static = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + LifetimeTrackedImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); libcc2rs::register_static_destructor(|| { Destructible::destroy(shared_5.with(Value::clone).as_pointer()) }); @@ -120,11 +123,21 @@ impl AssertsDtorCountAtExitImpl for Ptr { } } pub trait LifetimeTrackedImpl { + fn construct(&self); + fn copy_from(&self, _a0: Ptr); fn destructor(&self); fn use_(&self); fn copy_assign(&self, _a0: Ptr) -> Ptr; } impl LifetimeTrackedImpl for Ptr { + fn construct(&self) { + field!((*self), uses).write(0); + (*ctorCount_0.with(Value::clone).borrow_mut()).prefix_inc(); + } + fn copy_from(&self, _a0: Ptr) { + field!((*self), uses).write(0); + (*copyCount_1.with(Value::clone).borrow_mut()).prefix_inc(); + } fn destructor(&self) { (*dtorCount_2.with(Value::clone).borrow_mut()).prefix_inc(); } diff --git a/tests/std/basic/basic.def.odr/p15-inline-variable/out/unsafe/p15-inline-variable.rs b/tests/std/basic/basic.def.odr/p15-inline-variable/out/unsafe/p15-inline-variable.rs index d3ccd895f0..471dc7023e 100644 --- a/tests/std/basic/basic.def.odr/p15-inline-variable/out/unsafe/p15-inline-variable.rs +++ b/tests/std/basic/basic.def.odr/p15-inline-variable/out/unsafe/p15-inline-variable.rs @@ -11,7 +11,7 @@ pub static mut copyCount_1: std::cell::LazyCell = std::cell::LazyCell::new( pub static mut dtorCount_2: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); pub static mut useCount_3: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); #[repr(C)] -#[derive(VaArg, FnPtrArg, CopyAssignUnsafe, DestructibleUnsafe)] +#[derive(VaArg, FnPtrArg, ZeroInitializationUnsafe, CopyAssignUnsafe, DestructibleUnsafe)] pub struct LifetimeTracked { pub uses: i32, } @@ -28,15 +28,13 @@ impl LifetimeTracked { (*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_1)) }; } - pub unsafe fn new() -> Self { - let mut this = Self { uses: 0 }; + pub unsafe fn construct(&mut self) { + (&raw mut self.uses).write(0); (*std::cell::LazyCell::force_mut(&mut *&raw mut ctorCount_0)).prefix_inc(); - this } - pub unsafe fn copy_from(_a0: *const LifetimeTracked) -> Self { - let mut this = Self { uses: 0 }; + pub unsafe fn copy_from(&mut self, _a0: *const LifetimeTracked) { + (&raw mut self.uses).write(0); (*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_1)).prefix_inc(); - this } pub unsafe fn destructor(&mut self) { (*std::cell::LazyCell::force_mut(&mut *&raw mut dtorCount_2)).prefix_inc(); @@ -52,16 +50,26 @@ impl LifetimeTracked { } impl Clone for LifetimeTracked { fn clone(&self) -> Self { - unsafe { LifetimeTracked::copy_from(self as *const LifetimeTracked) } + unsafe { + let mut __v: LifetimeTracked = ::zero_init(); + LifetimeTracked::copy_from(&mut __v, self as *const LifetimeTracked); + __v + } } } impl Default for LifetimeTracked { fn default() -> Self { - unsafe { LifetimeTracked::new() } + unsafe { + { + let mut __v: LifetimeTracked = ::zero_init(); + LifetimeTracked::construct(&mut __v); + __v + } + } } } #[repr(C)] -#[derive(Clone, VaArg, FnPtrArg, DestructibleUnsafe, Default)] +#[derive(Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, DestructibleUnsafe, Default)] pub struct AssertsDtorCountAtExit { pub expected: i32, } @@ -85,7 +93,11 @@ pub static mut check_4: std::cell::LazyCell = pub static mut shared_5: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { { - let __static = LifetimeTracked::new(); + let __static = { + let mut __v: LifetimeTracked = ::zero_init(); + LifetimeTracked::construct(&mut __v); + __v + }; libcc2rs::register_static_destructor(|| { DestructibleUnsafe::destroy(&raw mut *std::cell::LazyCell::force_mut( &mut *&raw mut shared_5, diff --git a/tests/std/basic/basic.def.odr/p16-default-argument-lambda-in-class/out/refcount/p16-default-argument-lambda-in-class.rs b/tests/std/basic/basic.def.odr/p16-default-argument-lambda-in-class/out/refcount/p16-default-argument-lambda-in-class.rs index 0155e16904..9a21e5e86e 100644 --- a/tests/std/basic/basic.def.odr/p16-default-argument-lambda-in-class/out/refcount/p16-default-argument-lambda-in-class.rs +++ b/tests/std/basic/basic.def.odr/p16-default-argument-lambda-in-class/out/refcount/p16-default-argument-lambda-in-class.rs @@ -18,7 +18,7 @@ thread_local!( thread_local!( pub static useCount_3: Value = Rc::new(RefCell::new(0)); ); -#[derive(Record, ByteRepr, VaArg, FnPtrArg, CopyAssign, Destructible)] +#[derive(Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, CopyAssign, Destructible)] #[byte_size(4)] pub struct LifetimeTracked { #[offset(0)] @@ -43,42 +43,45 @@ impl LifetimeTracked { } }); } - pub fn new() -> Self { - let __this: LifetimeTracked = Self { uses: 0 }; - (*ctorCount_0.with(Value::clone).borrow_mut()).prefix_inc(); - __this - } - pub fn copy_from(_a0: Ptr) -> Self { - let __this: LifetimeTracked = Self { uses: 0 }; - (*copyCount_1.with(Value::clone).borrow_mut()).prefix_inc(); - __this - } } impl Clone for LifetimeTracked { fn clone(&self) -> Self { let __src: Value = Rc::new(RefCell::new(LifetimeTracked { uses: self.uses.clone(), })); - LifetimeTracked::copy_from(__src.as_pointer()) + let __v: Value = Rc::new(RefCell::new(::zero_init())); + LifetimeTrackedImpl::copy_from(&__v.as_pointer(), __src.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() } } impl Default for LifetimeTracked { fn default() -> Self { - { LifetimeTracked::new() } + { + { + let __v: Value = + Rc::new(RefCell::new(::zero_init())); + LifetimeTrackedImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + } + } } } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Destructible, Default)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Destructible, Default)] #[byte_size(4)] pub struct AssertsDtorCountAtExit { #[offset(0)] pub expected: i32, } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(1)] pub struct X {} thread_local!( static local_4: Value = { - let __static = Rc::new(RefCell::new(LifetimeTracked::new())); + let __static = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + LifetimeTrackedImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); libcc2rs::register_static_destructor(|| { Destructible::destroy(local_4.with(Value::clone).as_pointer()) }); @@ -138,11 +141,21 @@ impl AssertsDtorCountAtExitImpl for Ptr { } } pub trait LifetimeTrackedImpl { + fn construct(&self); + fn copy_from(&self, _a0: Ptr); fn destructor(&self); fn use_(&self); fn copy_assign(&self, _a0: Ptr) -> Ptr; } impl LifetimeTrackedImpl for Ptr { + fn construct(&self) { + field!((*self), uses).write(0); + (*ctorCount_0.with(Value::clone).borrow_mut()).prefix_inc(); + } + fn copy_from(&self, _a0: Ptr) { + field!((*self), uses).write(0); + (*copyCount_1.with(Value::clone).borrow_mut()).prefix_inc(); + } fn destructor(&self) { (*dtorCount_2.with(Value::clone).borrow_mut()).prefix_inc(); } diff --git a/tests/std/basic/basic.def.odr/p16-default-argument-lambda-in-class/out/unsafe/p16-default-argument-lambda-in-class.rs b/tests/std/basic/basic.def.odr/p16-default-argument-lambda-in-class/out/unsafe/p16-default-argument-lambda-in-class.rs index 131a0dbf5b..dc894849a6 100644 --- a/tests/std/basic/basic.def.odr/p16-default-argument-lambda-in-class/out/unsafe/p16-default-argument-lambda-in-class.rs +++ b/tests/std/basic/basic.def.odr/p16-default-argument-lambda-in-class/out/unsafe/p16-default-argument-lambda-in-class.rs @@ -11,7 +11,7 @@ pub static mut copyCount_1: std::cell::LazyCell = std::cell::LazyCell::new( pub static mut dtorCount_2: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); pub static mut useCount_3: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); #[repr(C)] -#[derive(VaArg, FnPtrArg, CopyAssignUnsafe, DestructibleUnsafe)] +#[derive(VaArg, FnPtrArg, ZeroInitializationUnsafe, CopyAssignUnsafe, DestructibleUnsafe)] pub struct LifetimeTracked { pub uses: i32, } @@ -28,15 +28,13 @@ impl LifetimeTracked { (*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_1)) }; } - pub unsafe fn new() -> Self { - let mut this = Self { uses: 0 }; + pub unsafe fn construct(&mut self) { + (&raw mut self.uses).write(0); (*std::cell::LazyCell::force_mut(&mut *&raw mut ctorCount_0)).prefix_inc(); - this } - pub unsafe fn copy_from(_a0: *const LifetimeTracked) -> Self { - let mut this = Self { uses: 0 }; + pub unsafe fn copy_from(&mut self, _a0: *const LifetimeTracked) { + (&raw mut self.uses).write(0); (*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_1)).prefix_inc(); - this } pub unsafe fn destructor(&mut self) { (*std::cell::LazyCell::force_mut(&mut *&raw mut dtorCount_2)).prefix_inc(); @@ -52,16 +50,26 @@ impl LifetimeTracked { } impl Clone for LifetimeTracked { fn clone(&self) -> Self { - unsafe { LifetimeTracked::copy_from(self as *const LifetimeTracked) } + unsafe { + let mut __v: LifetimeTracked = ::zero_init(); + LifetimeTracked::copy_from(&mut __v, self as *const LifetimeTracked); + __v + } } } impl Default for LifetimeTracked { fn default() -> Self { - unsafe { LifetimeTracked::new() } + unsafe { + { + let mut __v: LifetimeTracked = ::zero_init(); + LifetimeTracked::construct(&mut __v); + __v + } + } } } #[repr(C)] -#[derive(Clone, VaArg, FnPtrArg, DestructibleUnsafe, Default)] +#[derive(Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, DestructibleUnsafe, Default)] pub struct AssertsDtorCountAtExit { pub expected: i32, } @@ -71,7 +79,7 @@ impl AssertsDtorCountAtExit { } } #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] pub struct X {} impl X { pub unsafe fn h(&mut self, mut cond: bool, mut p: Option) { @@ -96,7 +104,11 @@ impl X { } static mut local_4: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { { - let __static = LifetimeTracked::new(); + let __static = { + let mut __v: LifetimeTracked = ::zero_init(); + LifetimeTracked::construct(&mut __v); + __v + }; libcc2rs::register_static_destructor(|| { DestructibleUnsafe::destroy(&raw mut *std::cell::LazyCell::force_mut( &mut *&raw mut local_4, diff --git a/tests/std/basic/basic.def.odr/p16-lambda-in-inline-function/out/refcount/p16-lambda-in-inline-function.rs b/tests/std/basic/basic.def.odr/p16-lambda-in-inline-function/out/refcount/p16-lambda-in-inline-function.rs index d14868631e..97c52f8bd9 100644 --- a/tests/std/basic/basic.def.odr/p16-lambda-in-inline-function/out/refcount/p16-lambda-in-inline-function.rs +++ b/tests/std/basic/basic.def.odr/p16-lambda-in-inline-function/out/refcount/p16-lambda-in-inline-function.rs @@ -18,7 +18,7 @@ thread_local!( thread_local!( pub static useCount_3: Value = Rc::new(RefCell::new(0)); ); -#[derive(Record, ByteRepr, VaArg, FnPtrArg, CopyAssign, Destructible)] +#[derive(Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, CopyAssign, Destructible)] #[byte_size(4)] pub struct LifetimeTracked { #[offset(0)] @@ -43,31 +43,30 @@ impl LifetimeTracked { } }); } - pub fn new() -> Self { - let __this: LifetimeTracked = Self { uses: 0 }; - (*ctorCount_0.with(Value::clone).borrow_mut()).prefix_inc(); - __this - } - pub fn copy_from(_a0: Ptr) -> Self { - let __this: LifetimeTracked = Self { uses: 0 }; - (*copyCount_1.with(Value::clone).borrow_mut()).prefix_inc(); - __this - } } impl Clone for LifetimeTracked { fn clone(&self) -> Self { let __src: Value = Rc::new(RefCell::new(LifetimeTracked { uses: self.uses.clone(), })); - LifetimeTracked::copy_from(__src.as_pointer()) + let __v: Value = Rc::new(RefCell::new(::zero_init())); + LifetimeTrackedImpl::copy_from(&__v.as_pointer(), __src.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() } } impl Default for LifetimeTracked { fn default() -> Self { - { LifetimeTracked::new() } + { + { + let __v: Value = + Rc::new(RefCell::new(::zero_init())); + LifetimeTrackedImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + } + } } } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Destructible, Default)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Destructible, Default)] #[byte_size(4)] pub struct AssertsDtorCountAtExit { #[offset(0)] @@ -89,7 +88,11 @@ pub fn f_4(mut cond: bool, mut p: FnPtr) { } thread_local!( static local_5: Value = { - let __static = Rc::new(RefCell::new(LifetimeTracked::new())); + let __static = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + LifetimeTrackedImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); libcc2rs::register_static_destructor(|| { Destructible::destroy(local_5.with(Value::clone).as_pointer()) }); @@ -131,11 +134,21 @@ impl AssertsDtorCountAtExitImpl for Ptr { } } pub trait LifetimeTrackedImpl { + fn construct(&self); + fn copy_from(&self, _a0: Ptr); fn destructor(&self); fn use_(&self); fn copy_assign(&self, _a0: Ptr) -> Ptr; } impl LifetimeTrackedImpl for Ptr { + fn construct(&self) { + field!((*self), uses).write(0); + (*ctorCount_0.with(Value::clone).borrow_mut()).prefix_inc(); + } + fn copy_from(&self, _a0: Ptr) { + field!((*self), uses).write(0); + (*copyCount_1.with(Value::clone).borrow_mut()).prefix_inc(); + } fn destructor(&self) { (*dtorCount_2.with(Value::clone).borrow_mut()).prefix_inc(); } diff --git a/tests/std/basic/basic.def.odr/p16-lambda-in-inline-function/out/unsafe/p16-lambda-in-inline-function.rs b/tests/std/basic/basic.def.odr/p16-lambda-in-inline-function/out/unsafe/p16-lambda-in-inline-function.rs index b1e530e52c..c4cf98de49 100644 --- a/tests/std/basic/basic.def.odr/p16-lambda-in-inline-function/out/unsafe/p16-lambda-in-inline-function.rs +++ b/tests/std/basic/basic.def.odr/p16-lambda-in-inline-function/out/unsafe/p16-lambda-in-inline-function.rs @@ -11,7 +11,7 @@ pub static mut copyCount_1: std::cell::LazyCell = std::cell::LazyCell::new( pub static mut dtorCount_2: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); pub static mut useCount_3: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); #[repr(C)] -#[derive(VaArg, FnPtrArg, CopyAssignUnsafe, DestructibleUnsafe)] +#[derive(VaArg, FnPtrArg, ZeroInitializationUnsafe, CopyAssignUnsafe, DestructibleUnsafe)] pub struct LifetimeTracked { pub uses: i32, } @@ -28,15 +28,13 @@ impl LifetimeTracked { (*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_1)) }; } - pub unsafe fn new() -> Self { - let mut this = Self { uses: 0 }; + pub unsafe fn construct(&mut self) { + (&raw mut self.uses).write(0); (*std::cell::LazyCell::force_mut(&mut *&raw mut ctorCount_0)).prefix_inc(); - this } - pub unsafe fn copy_from(_a0: *const LifetimeTracked) -> Self { - let mut this = Self { uses: 0 }; + pub unsafe fn copy_from(&mut self, _a0: *const LifetimeTracked) { + (&raw mut self.uses).write(0); (*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_1)).prefix_inc(); - this } pub unsafe fn destructor(&mut self) { (*std::cell::LazyCell::force_mut(&mut *&raw mut dtorCount_2)).prefix_inc(); @@ -52,16 +50,26 @@ impl LifetimeTracked { } impl Clone for LifetimeTracked { fn clone(&self) -> Self { - unsafe { LifetimeTracked::copy_from(self as *const LifetimeTracked) } + unsafe { + let mut __v: LifetimeTracked = ::zero_init(); + LifetimeTracked::copy_from(&mut __v, self as *const LifetimeTracked); + __v + } } } impl Default for LifetimeTracked { fn default() -> Self { - unsafe { LifetimeTracked::new() } + unsafe { + { + let mut __v: LifetimeTracked = ::zero_init(); + LifetimeTracked::construct(&mut __v); + __v + } + } } } #[repr(C)] -#[derive(Clone, VaArg, FnPtrArg, DestructibleUnsafe, Default)] +#[derive(Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, DestructibleUnsafe, Default)] pub struct AssertsDtorCountAtExit { pub expected: i32, } @@ -90,7 +98,11 @@ pub unsafe fn f_4(mut cond: bool, mut p: Option) { } static mut local_5: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { { - let __static = LifetimeTracked::new(); + let __static = { + let mut __v: LifetimeTracked = ::zero_init(); + LifetimeTracked::construct(&mut __v); + __v + }; libcc2rs::register_static_destructor(|| { DestructibleUnsafe::destroy(&raw mut *std::cell::LazyCell::force_mut( &mut *&raw mut local_5, diff --git a/tests/std/basic/basic.link/out/refcount/basic.link.rs b/tests/std/basic/basic.link/out/refcount/basic.link.rs index 4209c9f144..41793dcac4 100644 --- a/tests/std/basic/basic.link/out/refcount/basic.link.rs +++ b/tests/std/basic/basic.link/out/refcount/basic.link.rs @@ -29,11 +29,12 @@ pub fn test_11() { assert!((ceiling_10.with(|rc| *rc.borrow()) == 40)); assert!(((ceiling_10.with(|v| v.as_pointer())) == ({ ceiling_address_in_b_12() }))); } -#[derive(VaArg, FnPtrArg, ByteRepr, Clone)] +#[derive(VaArg, FnPtrArg, ByteRepr, Clone, ZeroInitialization)] #[byte_size(4)] pub struct anon_13 { #[offset(0)] #[byte_size(4)] + #[array_len(4)] __bytes: Box<[u8]>, } impl anon_13 { @@ -66,7 +67,7 @@ thread_local!( pub fn bump_18() -> i32 { return (*(*counter_17.with(Value::clone).borrow_mut()).prefix_inc()); } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg)] #[byte_size(4)] pub struct Record_19 { #[offset(0)] @@ -102,7 +103,7 @@ pub fn test_29() { level_28.with(|rc| *rc.borrow_mut() = 10); assert!((({ raise_30(1,) }) == 11)); } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(1)] pub struct Tally_31 {} thread_local!( @@ -114,7 +115,7 @@ pub fn test_33() { assert!((({ add_in_b_34(1,) }) == 102)); assert!((total_32.with(|rc| *rc.borrow()) == 11)); } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(1)] pub struct p5_members_of_class_with_external_linkage_Ledger {} pub fn test_36() { @@ -182,7 +183,7 @@ pub fn test_55() { assert!((({ second_key_plus_value_53() }) == 32)); assert!((({ key_in_b_56() }) == 400)); } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg)] #[byte_size(4)] pub struct Entry_52 { #[offset(0)] @@ -193,7 +194,7 @@ impl Default for Entry_52 { { Entry_52 { key: 1 } } } } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg)] #[byte_size(8)] pub struct Entry_54 { #[offset(0)] @@ -265,11 +266,12 @@ thread_local!( pub fn ceiling_address_in_b_12() -> Ptr { return (ceiling_10.with(|v| v.as_pointer())); } -#[derive(VaArg, FnPtrArg, ByteRepr, Clone)] +#[derive(VaArg, FnPtrArg, ByteRepr, Clone, ZeroInitialization)] #[byte_size(4)] pub struct anon_62 { #[offset(0)] #[byte_size(4)] + #[array_len(4)] __bytes: Box<[u8]>, } impl anon_62 { @@ -302,7 +304,7 @@ thread_local!( pub fn bump_64() -> i32 { return (*(*counter_63.with(Value::clone).borrow_mut()).prefix_inc()); } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg)] #[byte_size(16)] pub struct Record_65 { #[offset(0)] @@ -346,7 +348,7 @@ pub fn raise_30(mut by: i32) -> i32 { level_28.with(|rc| *rc.borrow_mut() += by); return level_28.with(|rc| *rc.borrow()); } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(1)] pub struct Tally_67 {} thread_local!( @@ -388,7 +390,7 @@ pub fn key_in_b_56() -> i32 { let mut entry: Entry_71 = ::default(); return (entry.key as i32); } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg)] #[byte_size(8)] pub struct Entry_71 { #[offset(0)] diff --git a/tests/std/basic/basic.link/out/unsafe/basic.link.rs b/tests/std/basic/basic.link/out/unsafe/basic.link.rs index 1e768600bf..e387afab9a 100644 --- a/tests/std/basic/basic.link/out/unsafe/basic.link.rs +++ b/tests/std/basic/basic.link/out/unsafe/basic.link.rs @@ -37,7 +37,7 @@ pub unsafe fn test_11() { ); } #[repr(C)] -#[derive(Copy, Clone, FnPtrArg, VaArg)] +#[derive(Copy, Clone, FnPtrArg, VaArg, ZeroInitializationUnsafe)] pub union anon_13 { pub whole: i32, pub fraction: f32, @@ -60,7 +60,7 @@ pub unsafe fn bump_18() -> i32 { return (*(*std::cell::LazyCell::force_mut(&mut *&raw mut counter_17)).prefix_inc()); } #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe)] pub struct Record_19 { pub id: i32, } @@ -93,7 +93,7 @@ pub unsafe fn test_29() { assert!(((unsafe { raise_30(1,) }) == (11))); } #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] pub struct Tally_31 {} impl Tally_31 { pub unsafe fn add(&mut self, mut amount: i32) -> i32 { @@ -109,7 +109,7 @@ pub unsafe fn test_33() { assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut total_32)) == (11))); } #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] pub struct p5_members_of_class_with_external_linkage_Ledger {} pub unsafe fn test_36() { assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut balance_35)) == (50))); @@ -171,7 +171,7 @@ pub unsafe fn test_55() { assert!(((unsafe { key_in_b_56() }) == (400))); } #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe)] pub struct Entry_52 { pub key: i32, } @@ -181,7 +181,7 @@ impl Default for Entry_52 { } } #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe)] pub struct Entry_54 { pub key: i32, pub value: i32, @@ -243,7 +243,7 @@ pub unsafe fn ceiling_address_in_b_12() -> *const i32 { return (&raw const (*std::cell::LazyCell::force_mut(&mut *&raw mut ceiling_10)) as *const i32); } #[repr(C)] -#[derive(Copy, Clone, FnPtrArg, VaArg)] +#[derive(Copy, Clone, FnPtrArg, VaArg, ZeroInitializationUnsafe)] pub union anon_62 { pub whole: i32, pub fraction: f32, @@ -266,7 +266,7 @@ pub unsafe fn bump_64() -> i32 { return (*(*std::cell::LazyCell::force_mut(&mut *&raw mut counter_63)).prefix_inc()); } #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe)] pub struct Record_65 { pub id: i64, pub extra: i32, @@ -303,7 +303,7 @@ pub unsafe fn raise_30(mut by: i32) -> i32 { return (*std::cell::LazyCell::force_mut(&mut *&raw mut level_28)); } #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] pub struct Tally_67 {} impl Tally_67 { pub unsafe fn add(&mut self, mut amount: i32) -> i32 { @@ -341,7 +341,7 @@ pub unsafe fn key_in_b_56() -> i32 { return (entry.key as i32); } #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe)] pub struct Entry_71 { pub key: i64, } diff --git a/tests/std/basic/basic.scope/out/refcount/basic.scope.block.rs b/tests/std/basic/basic.scope/out/refcount/basic.scope.block.rs index 207f61bb2f..96054d9eed 100644 --- a/tests/std/basic/basic.scope/out/refcount/basic.scope.block.rs +++ b/tests/std/basic/basic.scope/out/refcount/basic.scope.block.rs @@ -18,7 +18,7 @@ thread_local!( thread_local!( pub static useCount_3: Value = Rc::new(RefCell::new(0)); ); -#[derive(Record, ByteRepr, VaArg, FnPtrArg, CopyAssign, Destructible)] +#[derive(Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, CopyAssign, Destructible)] #[byte_size(4)] pub struct LifetimeTracked { #[offset(0)] @@ -43,28 +43,27 @@ impl LifetimeTracked { } }); } - pub fn new() -> Self { - let __this: LifetimeTracked = Self { uses: 0 }; - (*ctorCount_0.with(Value::clone).borrow_mut()).prefix_inc(); - __this - } - pub fn copy_from(_a0: Ptr) -> Self { - let __this: LifetimeTracked = Self { uses: 0 }; - (*copyCount_1.with(Value::clone).borrow_mut()).prefix_inc(); - __this - } } impl Clone for LifetimeTracked { fn clone(&self) -> Self { let __src: Value = Rc::new(RefCell::new(LifetimeTracked { uses: self.uses.clone(), })); - LifetimeTracked::copy_from(__src.as_pointer()) + let __v: Value = Rc::new(RefCell::new(::zero_init())); + LifetimeTrackedImpl::copy_from(&__v.as_pointer(), __src.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() } } impl Default for LifetimeTracked { fn default() -> Self { - { LifetimeTracked::new() } + { + { + let __v: Value = + Rc::new(RefCell::new(::zero_init())); + LifetimeTrackedImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + } + } } } thread_local!( @@ -76,7 +75,7 @@ thread_local!( thread_local!( pub static dtorCount_6: Value = Rc::new(RefCell::new(0)); ); -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Destructible, Default)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Destructible, Default)] #[byte_size(4)] pub struct p1_1_condition_variable_visible_in_else_ConvertsToBool { #[offset(0)] @@ -148,7 +147,11 @@ pub fn test_8() { } pub fn test_9() { ({ LifetimeTracked::reset() }); - let scoped: Value = Rc::new(RefCell::new(LifetimeTracked::new())); + let scoped: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + LifetimeTrackedImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); let _dtor_scoped = std::mem::DropGuard::new(scoped.clone(), |__p| { Destructible::destroy(__p.as_pointer()) }); @@ -157,7 +160,12 @@ pub fn test_9() { let mut index: i32 = 0; 'loop_: while (index < 3) { 'for_body: { - let scoped: Value = Rc::new(RefCell::new(LifetimeTracked::new())); + let scoped: Value = Rc::new(RefCell::new({ + let __v: Value = + Rc::new(RefCell::new(::zero_init())); + LifetimeTrackedImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); let _dtor_scoped = std::mem::DropGuard::new(scoped.clone(), |__p| { Destructible::destroy(__p.as_pointer()) }); @@ -168,7 +176,11 @@ pub fn test_9() { assert!((ctorCount_0.with(|rc| *rc.borrow()) == 4)); assert!((dtorCount_2.with(|rc| *rc.borrow()) == 3)); if (ctorCount_0.with(|rc| *rc.borrow()) == 4) { - let scoped: Value = Rc::new(RefCell::new(LifetimeTracked::new())); + let scoped: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + LifetimeTrackedImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); let _dtor_scoped = std::mem::DropGuard::new(scoped.clone(), |__p| { Destructible::destroy(__p.as_pointer()) }); @@ -177,7 +189,11 @@ pub fn test_9() { assert!((dtorCount_2.with(|rc| *rc.borrow()) == 4)); if (ctorCount_0.with(|rc| *rc.borrow()) == 0) { } else { - let scoped: Value = Rc::new(RefCell::new(LifetimeTracked::new())); + let scoped: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + LifetimeTrackedImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); let _dtor_scoped = std::mem::DropGuard::new(scoped.clone(), |__p| { Destructible::destroy(__p.as_pointer()) }); @@ -209,12 +225,20 @@ pub fn test_10() { } pub fn test_11() { ({ LifetimeTracked::reset() }); - let tracked: Value = Rc::new(RefCell::new(LifetimeTracked::new())); + let tracked: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + LifetimeTrackedImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); let _dtor_tracked = std::mem::DropGuard::new(tracked.clone(), |__p| { Destructible::destroy(__p.as_pointer()) }); { - let tracked: Value = Rc::new(RefCell::new(LifetimeTracked::new())); + let tracked: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + LifetimeTrackedImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); let _dtor_tracked = std::mem::DropGuard::new(tracked.clone(), |__p| { Destructible::destroy(__p.as_pointer()) }); @@ -304,11 +328,21 @@ fn main_0() -> i32 { return 0; } pub trait LifetimeTrackedImpl { + fn construct(&self); + fn copy_from(&self, _a0: Ptr); fn destructor(&self); fn use_(&self); fn copy_assign(&self, _a0: Ptr) -> Ptr; } impl LifetimeTrackedImpl for Ptr { + fn construct(&self) { + field!((*self), uses).write(0); + (*ctorCount_0.with(Value::clone).borrow_mut()).prefix_inc(); + } + fn copy_from(&self, _a0: Ptr) { + field!((*self), uses).write(0); + (*copyCount_1.with(Value::clone).borrow_mut()).prefix_inc(); + } fn destructor(&self) { (*dtorCount_2.with(Value::clone).borrow_mut()).prefix_inc(); } diff --git a/tests/std/basic/basic.scope/out/refcount/basic.scope.class.rs b/tests/std/basic/basic.scope/out/refcount/basic.scope.class.rs index 10ab816d23..d4746df16b 100644 --- a/tests/std/basic/basic.scope/out/refcount/basic.scope.class.rs +++ b/tests/std/basic/basic.scope/out/refcount/basic.scope.class.rs @@ -9,7 +9,7 @@ use std::rc::{Rc, Weak}; thread_local!( pub static balance_0: Value = Rc::new(RefCell::new(50)); ); -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg)] #[byte_size(4)] pub struct p1_member_hides_namespace_variable_HasBalance { #[offset(0)] @@ -45,7 +45,7 @@ pub fn test_1() { thread_local!( pub static initial_2: Value = Rc::new(RefCell::new(100)); ); -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg)] #[byte_size(4)] pub struct p1_out_of_class_definition_is_in_class_scope_HasStaticTotal { #[offset(0)] diff --git a/tests/std/basic/basic.scope/out/refcount/basic.scope.param.rs b/tests/std/basic/basic.scope/out/refcount/basic.scope.param.rs index 6ab0b4d31c..43c9eec869 100644 --- a/tests/std/basic/basic.scope/out/refcount/basic.scope.param.rs +++ b/tests/std/basic/basic.scope/out/refcount/basic.scope.param.rs @@ -6,32 +6,23 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, CppFrom, Default)] -#[cpp_from(i32, new, value)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, CppFrom, Default)] +#[cpp_from(i32, construct, value)] #[byte_size(4)] pub struct p1_1_parameter_hides_member_in_constructor_HasMemberNamedLikeParameter { #[offset(0)] pub amount: i32, } -impl p1_1_parameter_hides_member_in_constructor_HasMemberNamedLikeParameter { - pub fn new(mut amount: i32) -> Self { - let __this: Value = - Rc::new(RefCell::new(Self { - amount: (amount + 1), - })); - let this: Ptr = - __this.as_pointer(); - amount += 10; - assert!((amount == 14)); - assert!((this.with(|__s| __s.amount) == 5)); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } -} pub fn test_0() { let object: Value = - Rc::new(RefCell::new( - p1_1_parameter_hides_member_in_constructor_HasMemberNamedLikeParameter::new({ 4 }), - )); + Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + p1_1_parameter_hides_member_in_constructor_HasMemberNamedLikeParameterImpl::construct( + &__v.as_pointer(), + { 4 }, + ); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); assert!(({ (*object.borrow()).amount } == 5)); assert!( (({ @@ -88,11 +79,18 @@ fn main_0() -> i32 { return 0; } pub trait p1_1_parameter_hides_member_in_constructor_HasMemberNamedLikeParameterImpl { + fn construct(&self, amount: i32); fn add(&self, amount: i32) -> i32; } impl p1_1_parameter_hides_member_in_constructor_HasMemberNamedLikeParameterImpl for Ptr { + fn construct(&self, mut amount: i32) { + field!((*self), amount).write((amount + 1)); + amount += 10; + assert!((amount == 14)); + assert!(((*self).with(|__s| __s.amount) == 5)); + } fn add(&self, mut amount: i32) -> i32 { return ((*self).with(|__s| __s.amount) + amount); } diff --git a/tests/std/basic/basic.scope/out/refcount/basic.scope.pdecl.rs b/tests/std/basic/basic.scope/out/refcount/basic.scope.pdecl.rs index 21ffed70d7..da1b9d66d1 100644 --- a/tests/std/basic/basic.scope/out/refcount/basic.scope.pdecl.rs +++ b/tests/std/basic/basic.scope/out/refcount/basic.scope.pdecl.rs @@ -18,7 +18,7 @@ pub fn test_0() { } assert!((level == 5)); } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(16)] pub struct p1_own_address_in_initializer_SelfLinked { #[offset(0)] @@ -73,31 +73,27 @@ pub fn test_3() { pub type anon_4 = u32; pub const anon_4_limit: anon_4 = 13; pub const anon_4_above: anon_4 = 14; -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg)] #[byte_size(8)] pub struct p9_func_declared_before_function_body_RecordsConstructorName { #[offset(0)] #[byte_size(8)] pub name: Ptr, } -impl p9_func_declared_before_function_body_RecordsConstructorName { - pub fn new() -> Self { - let __this: Value = - Rc::new(RefCell::new(Self { - name: Ptr::::from_string_literal(b"RecordsConstructorName"), - })); - let this: Ptr = - __this.as_pointer(); - assert!( - (this.with(|__s| __s.name.clone()) - == Ptr::::from_string_literal(b"RecordsConstructorName")) - ); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } -} impl Default for p9_func_declared_before_function_body_RecordsConstructorName { fn default() -> Self { - { p9_func_declared_before_function_body_RecordsConstructorName::new() } + { + { + let __v: Value = + Rc::new(RefCell::new( + ::zero_init(), + )); + p9_func_declared_before_function_body_RecordsConstructorNameImpl::construct( + &__v.as_pointer(), + ); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + } + } } } pub fn plain_function_5() -> Ptr { @@ -122,8 +118,16 @@ pub fn test_6() { } == 0) ); assert!((({ plain_function_5() }) == ({ plain_function_5() }))); - let mut object: p9_func_declared_before_function_body_RecordsConstructorName = - p9_func_declared_before_function_body_RecordsConstructorName::new(); + let mut object: p9_func_declared_before_function_body_RecordsConstructorName = { + let __v: Value = + Rc::new(RefCell::new( + ::zero_init(), + )); + p9_func_declared_before_function_body_RecordsConstructorNameImpl::construct( + &__v.as_pointer(), + ); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }; assert!( ({ let mut __it1 = object.name.to_c_string_iterator(); @@ -196,4 +200,18 @@ fn main_0() -> i32 { ({ test_7() }); return 0; } +pub trait p9_func_declared_before_function_body_RecordsConstructorNameImpl { + fn construct(&self); +} +impl p9_func_declared_before_function_body_RecordsConstructorNameImpl + for Ptr +{ + fn construct(&self) { + field!((*self), name).write(Ptr::::from_string_literal(b"RecordsConstructorName")); + assert!( + ((*self).with(|__s| __s.name.clone()) + == Ptr::::from_string_literal(b"RecordsConstructorName")) + ); + } +} pub fn __cpp2rust_init_globals() {} diff --git a/tests/std/basic/basic.scope/out/refcount/basic.scope.scope.rs b/tests/std/basic/basic.scope/out/refcount/basic.scope.scope.rs index 92abee8f16..20d909a493 100644 --- a/tests/std/basic/basic.scope/out/refcount/basic.scope.scope.rs +++ b/tests/std/basic/basic.scope/out/refcount/basic.scope.scope.rs @@ -44,7 +44,7 @@ pub const p2_11_unscoped_enumerator_in_both_scopes_HasUnscopedEnum_Size_small: p2_11_unscoped_enumerator_in_both_scopes_HasUnscopedEnum_Size = 3; pub const p2_11_unscoped_enumerator_in_both_scopes_HasUnscopedEnum_Size_large: p2_11_unscoped_enumerator_in_both_scopes_HasUnscopedEnum_Size = 8; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(1)] pub struct p2_11_unscoped_enumerator_in_both_scopes_HasUnscopedEnum {} pub fn test_5() { @@ -72,7 +72,7 @@ pub fn test_5() { thread_local!( pub static count_6: Value = Rc::new(RefCell::new(0)); ); -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(1)] pub struct p2_12_class_name_in_own_scope_HasStaticCounter {} impl p2_12_class_name_in_own_scope_HasStaticCounter { @@ -88,11 +88,12 @@ pub fn test_7() { count_6.with(|rc| *rc.borrow_mut() = 6); assert!((count_6.with(|rc| *rc.borrow()) == 6)); } -#[derive(VaArg, FnPtrArg, ByteRepr, Clone)] +#[derive(VaArg, FnPtrArg, ByteRepr, Clone, ZeroInitialization)] #[byte_size(8)] pub struct anon_8 { #[offset(0)] #[byte_size(8)] + #[array_len(8)] __bytes: Box<[u8]>, } impl anon_8 { @@ -113,11 +114,12 @@ impl Default for anon_8 { thread_local!( pub static anon_8: Value = Rc::new(RefCell::new(::default())); ); -#[derive(VaArg, FnPtrArg, ByteRepr, Clone)] +#[derive(VaArg, FnPtrArg, ByteRepr, Clone, ZeroInitialization)] #[byte_size(8)] pub struct anon_9 { #[offset(0)] #[byte_size(8)] + #[array_len(8)] __bytes: Box<[u8]>, } impl anon_9 { @@ -135,7 +137,7 @@ impl Default for anon_9 { } } } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(16)] pub struct p2_13_anonymous_union_members_in_parent_scope_HasAnonymousUnion { #[offset(0)] @@ -171,11 +173,12 @@ pub fn test_10() { anon_11::block_ratio(anon_11.as_pointer()).write(0.75_f64); assert!(((anon_11::block_ratio(anon_11.as_pointer()).read()) == 0.75_f64)); } -#[derive(VaArg, FnPtrArg, ByteRepr, Clone)] +#[derive(VaArg, FnPtrArg, ByteRepr, Clone, ZeroInitialization)] #[byte_size(8)] pub struct anon_11 { #[offset(0)] #[byte_size(8)] + #[array_len(8)] __bytes: Box<[u8]>, } impl anon_11 { @@ -193,7 +196,7 @@ impl Default for anon_11 { } } } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg)] #[byte_size(8)] pub struct p3_2_const_qualified_member_is_distinct_HasConstOverload { #[offset(0)] @@ -237,7 +240,7 @@ thread_local!( thread_local!( pub static rvalue_calls_14: Value = Rc::new(RefCell::new(0)); ); -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg)] #[byte_size(4)] pub struct p3_2_explicit_object_and_rvalue_members_are_distinct_HasExplicitObjectOverload { #[offset(0)] @@ -279,7 +282,7 @@ thread_local!( thread_local!( pub static rvalue_calls_17: Value = Rc::new(RefCell::new(0)); ); -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(1)] pub struct p3_2_ref_qualified_members_are_distinct_HasRefQualifiedOverloads {} pub fn test_18() { @@ -317,7 +320,7 @@ pub fn test_18() { assert!((lvalue_calls_16.with(|rc| *rc.borrow()) == 1)); assert!((rvalue_calls_17.with(|rc| *rc.borrow()) == 2)); } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg)] #[byte_size(8)] pub struct p3_4_const_qualified_member_templates_are_distinct_HasConstTemplateOverload { #[offset(0)] @@ -364,7 +367,7 @@ thread_local!( thread_local!( pub static rvalue_calls_21: Value = Rc::new(RefCell::new(0)); ); -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(1)] pub struct p3_4_ref_qualified_member_templates_are_distinct_HasRefQualifiedTemplateOverloads {} pub fn test_22() { @@ -394,14 +397,14 @@ pub fn test_22() { thread_local!( pub static friend_calls_23: Value = Rc::new(RefCell::new(0_i32)); ); -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(1)] pub struct p4_friend_definition_defines_earlier_friend_declaration_DeclaresFriend {} pub fn shared_friend_24() -> i32 { (*friend_calls_23.with(Value::clone).borrow_mut()).prefix_inc(); return 7; } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(1)] pub struct p4_friend_definition_defines_earlier_friend_declaration_DefinesFriend {} pub fn test_25() { @@ -433,7 +436,7 @@ pub fn test_30() { thread_local!( pub static function_calls_31: Value = Rc::new(RefCell::new(0_i32)); ); -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct p4_2_class_and_function_share_name_Reading { #[offset(0)] @@ -451,7 +454,7 @@ pub fn test_33() { assert!((({ Reading_32(reading.value,) }) == 5)); assert!((function_calls_31.with(|rc| *rc.borrow()) == 1)); } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct p4_2_class_and_variable_share_name_Limit { #[offset(0)] diff --git a/tests/std/basic/basic.scope/out/unsafe/basic.scope.block.rs b/tests/std/basic/basic.scope/out/unsafe/basic.scope.block.rs index bad4677654..d5f103c910 100644 --- a/tests/std/basic/basic.scope/out/unsafe/basic.scope.block.rs +++ b/tests/std/basic/basic.scope/out/unsafe/basic.scope.block.rs @@ -11,7 +11,7 @@ pub static mut copyCount_1: std::cell::LazyCell = std::cell::LazyCell::new( pub static mut dtorCount_2: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); pub static mut useCount_3: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); #[repr(C)] -#[derive(VaArg, FnPtrArg, CopyAssignUnsafe, DestructibleUnsafe)] +#[derive(VaArg, FnPtrArg, ZeroInitializationUnsafe, CopyAssignUnsafe, DestructibleUnsafe)] pub struct LifetimeTracked { pub uses: i32, } @@ -28,15 +28,13 @@ impl LifetimeTracked { (*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_1)) }; } - pub unsafe fn new() -> Self { - let mut this = Self { uses: 0 }; + pub unsafe fn construct(&mut self) { + (&raw mut self.uses).write(0); (*std::cell::LazyCell::force_mut(&mut *&raw mut ctorCount_0)).prefix_inc(); - this } - pub unsafe fn copy_from(_a0: *const LifetimeTracked) -> Self { - let mut this = Self { uses: 0 }; + pub unsafe fn copy_from(&mut self, _a0: *const LifetimeTracked) { + (&raw mut self.uses).write(0); (*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_1)).prefix_inc(); - this } pub unsafe fn destructor(&mut self) { (*std::cell::LazyCell::force_mut(&mut *&raw mut dtorCount_2)).prefix_inc(); @@ -52,12 +50,22 @@ impl LifetimeTracked { } impl Clone for LifetimeTracked { fn clone(&self) -> Self { - unsafe { LifetimeTracked::copy_from(self as *const LifetimeTracked) } + unsafe { + let mut __v: LifetimeTracked = ::zero_init(); + LifetimeTracked::copy_from(&mut __v, self as *const LifetimeTracked); + __v + } } } impl Default for LifetimeTracked { fn default() -> Self { - unsafe { LifetimeTracked::new() } + unsafe { + { + let mut __v: LifetimeTracked = ::zero_init(); + LifetimeTracked::construct(&mut __v); + __v + } + } } } pub static mut then_runs_4: std::cell::LazyCell = @@ -66,7 +74,7 @@ pub static mut else_runs_5: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0_i32 }); pub static mut dtorCount_6: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); #[repr(C)] -#[derive(Clone, VaArg, FnPtrArg, DestructibleUnsafe, Default)] +#[derive(Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, DestructibleUnsafe, Default)] pub struct p1_1_condition_variable_visible_in_else_ConvertsToBool { pub value: i32, } @@ -136,7 +144,11 @@ pub unsafe fn test_8() { } pub unsafe fn test_9() { (unsafe { LifetimeTracked::reset() }); - let mut scoped: LifetimeTracked = LifetimeTracked::new(); + let mut scoped: LifetimeTracked = { + let mut __v: LifetimeTracked = ::zero_init(); + LifetimeTracked::construct(&mut __v); + __v + }; let _dtor_scoped = std::mem::DropGuard::new(&raw mut scoped, |__p| unsafe { DestructibleUnsafe::destroy(__p) }); @@ -145,7 +157,11 @@ pub unsafe fn test_9() { let mut index: i32 = 0; 'loop_: while ((index) < (3)) { 'for_body: { - let mut scoped: LifetimeTracked = LifetimeTracked::new(); + let mut scoped: LifetimeTracked = { + let mut __v: LifetimeTracked = ::zero_init(); + LifetimeTracked::construct(&mut __v); + __v + }; let _dtor_scoped = std::mem::DropGuard::new(&raw mut scoped, |__p| unsafe { DestructibleUnsafe::destroy(__p) }); @@ -156,7 +172,11 @@ pub unsafe fn test_9() { assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut ctorCount_0)) == (4))); assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut dtorCount_2)) == (3))); if ((*std::cell::LazyCell::force_mut(&mut *&raw mut ctorCount_0)) == (4)) { - let mut scoped: LifetimeTracked = LifetimeTracked::new(); + let mut scoped: LifetimeTracked = { + let mut __v: LifetimeTracked = ::zero_init(); + LifetimeTracked::construct(&mut __v); + __v + }; let _dtor_scoped = std::mem::DropGuard::new(&raw mut scoped, |__p| unsafe { DestructibleUnsafe::destroy(__p) }); @@ -165,7 +185,11 @@ pub unsafe fn test_9() { assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut dtorCount_2)) == (4))); if ((*std::cell::LazyCell::force_mut(&mut *&raw mut ctorCount_0)) == (0)) { } else { - let mut scoped: LifetimeTracked = LifetimeTracked::new(); + let mut scoped: LifetimeTracked = { + let mut __v: LifetimeTracked = ::zero_init(); + LifetimeTracked::construct(&mut __v); + __v + }; let _dtor_scoped = std::mem::DropGuard::new(&raw mut scoped, |__p| unsafe { DestructibleUnsafe::destroy(__p) }); @@ -193,12 +217,20 @@ pub unsafe fn test_10() { } pub unsafe fn test_11() { (unsafe { LifetimeTracked::reset() }); - let mut tracked: LifetimeTracked = LifetimeTracked::new(); + let mut tracked: LifetimeTracked = { + let mut __v: LifetimeTracked = ::zero_init(); + LifetimeTracked::construct(&mut __v); + __v + }; let _dtor_tracked = std::mem::DropGuard::new(&raw mut tracked, |__p| unsafe { DestructibleUnsafe::destroy(__p) }); { - let mut tracked: LifetimeTracked = LifetimeTracked::new(); + let mut tracked: LifetimeTracked = { + let mut __v: LifetimeTracked = ::zero_init(); + LifetimeTracked::construct(&mut __v); + __v + }; let _dtor_tracked = std::mem::DropGuard::new(&raw mut tracked, |__p| unsafe { DestructibleUnsafe::destroy(__p) }); diff --git a/tests/std/basic/basic.scope/out/unsafe/basic.scope.class.rs b/tests/std/basic/basic.scope/out/unsafe/basic.scope.class.rs index 250b100676..d332543beb 100644 --- a/tests/std/basic/basic.scope/out/unsafe/basic.scope.class.rs +++ b/tests/std/basic/basic.scope/out/unsafe/basic.scope.class.rs @@ -8,7 +8,7 @@ use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; pub static mut balance_0: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 50 }); #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe)] pub struct p1_member_hides_namespace_variable_HasBalance { pub balance: i32, } @@ -46,7 +46,7 @@ pub unsafe fn test_1() { } pub static mut initial_2: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 100 }); #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe)] pub struct p1_out_of_class_definition_is_in_class_scope_HasStaticTotal { pub held: i32, } diff --git a/tests/std/basic/basic.scope/out/unsafe/basic.scope.param.rs b/tests/std/basic/basic.scope/out/unsafe/basic.scope.param.rs index d7efc2d115..7a8e0eb7ff 100644 --- a/tests/std/basic/basic.scope/out/unsafe/basic.scope.param.rs +++ b/tests/std/basic/basic.scope/out/unsafe/basic.scope.param.rs @@ -7,28 +7,32 @@ use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg, CppFromUnsafe, Default)] -#[cpp_from(i32, new, value)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, CppFromUnsafe, Default)] +#[cpp_from(i32, construct, value)] pub struct p1_1_parameter_hides_member_in_constructor_HasMemberNamedLikeParameter { pub amount: i32, } impl p1_1_parameter_hides_member_in_constructor_HasMemberNamedLikeParameter { - pub unsafe fn new(mut amount: i32) -> Self { - let mut this = Self { - amount: ((amount) + (1)), - }; + pub unsafe fn construct(&mut self, mut amount: i32) { + (&raw mut self.amount).write(((amount) + (1))); amount += 10; assert!(((amount) == (14))); - assert!(((this.amount) == (5))); - this + assert!(((self.amount) == (5))); } pub unsafe fn add(&self, mut amount: i32) -> i32 { return ((self.amount) + (amount)); } } pub unsafe fn test_0() { - let mut object: p1_1_parameter_hides_member_in_constructor_HasMemberNamedLikeParameter = - p1_1_parameter_hides_member_in_constructor_HasMemberNamedLikeParameter::new({ 4 }); + let mut object: p1_1_parameter_hides_member_in_constructor_HasMemberNamedLikeParameter = { + let mut __v: p1_1_parameter_hides_member_in_constructor_HasMemberNamedLikeParameter = + ::zero_init(); + p1_1_parameter_hides_member_in_constructor_HasMemberNamedLikeParameter::construct( + &mut __v, + { 4 }, + ); + __v + }; assert!(((object.amount) == (5))); assert!( ((unsafe { diff --git a/tests/std/basic/basic.scope/out/unsafe/basic.scope.pdecl.rs b/tests/std/basic/basic.scope/out/unsafe/basic.scope.pdecl.rs index 3d10a2c1f5..6f6d6ec66c 100644 --- a/tests/std/basic/basic.scope/out/unsafe/basic.scope.pdecl.rs +++ b/tests/std/basic/basic.scope/out/unsafe/basic.scope.pdecl.rs @@ -19,7 +19,7 @@ pub unsafe fn test_0() { assert!(((level) == (5))); } #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] pub struct p1_own_address_in_initializer_SelfLinked { pub next: *mut p1_own_address_in_initializer_SelfLinked, pub value: i32, @@ -72,22 +72,26 @@ pub type anon_4 = u32; pub const anon_4_limit: anon_4 = 13; pub const anon_4_above: anon_4 = 14; #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe)] pub struct p9_func_declared_before_function_body_RecordsConstructorName { pub name: *const libc::c_char, } impl p9_func_declared_before_function_body_RecordsConstructorName { - pub unsafe fn new() -> Self { - let mut this = Self { - name: c"RecordsConstructorName".as_ptr(), - }; - assert!(((this.name) == (c"RecordsConstructorName".as_ptr()))); - this + pub unsafe fn construct(&mut self) { + (&raw mut self.name).write(c"RecordsConstructorName".as_ptr()); + assert!(((self.name) == (c"RecordsConstructorName".as_ptr()))); } } impl Default for p9_func_declared_before_function_body_RecordsConstructorName { fn default() -> Self { - unsafe { p9_func_declared_before_function_body_RecordsConstructorName::new() } + unsafe { + { + let mut __v: p9_func_declared_before_function_body_RecordsConstructorName = + ::zero_init(); + p9_func_declared_before_function_body_RecordsConstructorName::construct(&mut __v); + __v + } + } } } pub unsafe fn plain_function_5() -> *const libc::c_char { @@ -96,8 +100,12 @@ pub unsafe fn plain_function_5() -> *const libc::c_char { pub unsafe fn test_6() { assert!(((libc::strcmp((unsafe { plain_function_5() }), c"plain_function".as_ptr())) == (0))); assert!(((unsafe { plain_function_5() }) == (unsafe { plain_function_5() }))); - let mut object: p9_func_declared_before_function_body_RecordsConstructorName = - p9_func_declared_before_function_body_RecordsConstructorName::new(); + let mut object: p9_func_declared_before_function_body_RecordsConstructorName = { + let mut __v: p9_func_declared_before_function_body_RecordsConstructorName = + ::zero_init(); + p9_func_declared_before_function_body_RecordsConstructorName::construct(&mut __v); + __v + }; assert!(((libc::strcmp(object.name, c"RecordsConstructorName".as_ptr())) == (0))); let mut enclosing: *const libc::c_char = c"test".as_ptr(); let mut lambda: FnPtr *const libc::c_char> = diff --git a/tests/std/basic/basic.scope/out/unsafe/basic.scope.scope.rs b/tests/std/basic/basic.scope/out/unsafe/basic.scope.scope.rs index 426b246177..139e923e4d 100644 --- a/tests/std/basic/basic.scope/out/unsafe/basic.scope.scope.rs +++ b/tests/std/basic/basic.scope/out/unsafe/basic.scope.scope.rs @@ -42,7 +42,7 @@ pub const p2_11_unscoped_enumerator_in_both_scopes_HasUnscopedEnum_Size_small: pub const p2_11_unscoped_enumerator_in_both_scopes_HasUnscopedEnum_Size_large: p2_11_unscoped_enumerator_in_both_scopes_HasUnscopedEnum_Size = 8; #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] pub struct p2_11_unscoped_enumerator_in_both_scopes_HasUnscopedEnum {} impl p2_11_unscoped_enumerator_in_both_scopes_HasUnscopedEnum { pub unsafe fn scale(&self, mut count: i32) -> i32 { @@ -69,7 +69,7 @@ pub unsafe fn test_5() { } pub static mut count_6: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] pub struct p2_12_class_name_in_own_scope_HasStaticCounter {} impl p2_12_class_name_in_own_scope_HasStaticCounter { pub unsafe fn bump() { @@ -85,7 +85,7 @@ pub unsafe fn test_7() { assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut count_6)) == (6))); } #[repr(C)] -#[derive(Copy, Clone, FnPtrArg, VaArg)] +#[derive(Copy, Clone, FnPtrArg, VaArg, ZeroInitializationUnsafe)] pub union anon_8 { pub namespace_count: i32, pub namespace_ratio: f64, @@ -98,7 +98,7 @@ impl Default for anon_8 { pub static mut anon_8: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { ::default() }); #[repr(C)] -#[derive(Copy, Clone, FnPtrArg, VaArg)] +#[derive(Copy, Clone, FnPtrArg, VaArg, ZeroInitializationUnsafe)] pub union anon_9 { pub member_count: i32, pub member_ratio: f64, @@ -109,7 +109,7 @@ impl Default for anon_9 { } } #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] pub struct p2_13_anonymous_union_members_in_parent_scope_HasAnonymousUnion { pub tag: i32, pub anon_9: anon_9, @@ -146,7 +146,7 @@ pub unsafe fn test_10() { assert!(((anon_11.block_ratio) == (0.75_f64))); } #[repr(C)] -#[derive(Copy, Clone, FnPtrArg, VaArg)] +#[derive(Copy, Clone, FnPtrArg, VaArg, ZeroInitializationUnsafe)] pub union anon_11 { pub block_count: i32, pub block_ratio: f64, @@ -157,7 +157,7 @@ impl Default for anon_11 { } } #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe)] pub struct p3_2_const_qualified_member_is_distinct_HasConstOverload { pub plain_calls: i32, pub const_calls: i32, @@ -205,7 +205,7 @@ pub static mut explicit_calls_13: std::cell::LazyCell = pub static mut rvalue_calls_14: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe)] pub struct p3_2_explicit_object_and_rvalue_members_are_distinct_HasExplicitObjectOverload { pub base: i32, } @@ -257,7 +257,7 @@ pub static mut lvalue_calls_16: std::cell::LazyCell = pub static mut rvalue_calls_17: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] pub struct p3_2_ref_qualified_members_are_distinct_HasRefQualifiedOverloads {} impl p3_2_ref_qualified_members_are_distinct_HasRefQualifiedOverloads { pub unsafe fn get_1(&mut self) -> i32 { @@ -297,7 +297,7 @@ pub unsafe fn test_18() { assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut rvalue_calls_17)) == (2))); } #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe)] pub struct p3_4_const_qualified_member_templates_are_distinct_HasConstTemplateOverload { pub plain_calls: i32, pub const_calls: i32, @@ -353,7 +353,7 @@ pub static mut lvalue_calls_20: std::cell::LazyCell = pub static mut rvalue_calls_21: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] pub struct p3_4_ref_qualified_member_templates_are_distinct_HasRefQualifiedTemplateOverloads {} impl p3_4_ref_qualified_member_templates_are_distinct_HasRefQualifiedTemplateOverloads { pub unsafe fn get_1_int(&mut self, mut value: i32) -> i32 { @@ -392,14 +392,14 @@ pub unsafe fn test_22() { pub static mut friend_calls_23: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0_i32 }); #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] pub struct p4_friend_definition_defines_earlier_friend_declaration_DeclaresFriend {} pub unsafe fn shared_friend_24() -> i32 { (*std::cell::LazyCell::force_mut(&mut *&raw mut friend_calls_23)).prefix_inc(); return 7; } #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] pub struct p4_friend_definition_defines_earlier_friend_declaration_DefinesFriend {} pub unsafe fn test_25() { assert!(((unsafe { shared_friend_24() }) == (7))); @@ -428,7 +428,7 @@ pub unsafe fn test_30() { pub static mut function_calls_31: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0_i32 }); #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] pub struct p4_2_class_and_function_share_name_Reading { pub value: i32, } @@ -445,7 +445,7 @@ pub unsafe fn test_33() { assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut function_calls_31)) == (1))); } #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] pub struct p4_2_class_and_variable_share_name_Limit { pub value: i32, } diff --git a/tests/std/basic/basic.start/out/refcount/basic.start.term-p1-exit.rs b/tests/std/basic/basic.start/out/refcount/basic.start.term-p1-exit.rs index a2e0eea9a4..3f53a0a454 100644 --- a/tests/std/basic/basic.start/out/refcount/basic.start.term-p1-exit.rs +++ b/tests/std/basic/basic.start/out/refcount/basic.start.term-p1-exit.rs @@ -6,22 +6,19 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Destructible, Default)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Destructible, Default)] #[byte_size(4)] pub struct Tracker { #[offset(0)] pub tag: i32, } -impl Tracker { - pub fn new(mut tag: i32) -> Self { - let __this: Tracker = Self { tag: tag }; - printf!(b"construct %d\n", tag); - __this - } -} thread_local!( pub static global_0: Value = { - let __static = Rc::new(RefCell::new(Tracker::new({ 1 }))); + let __static = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + TrackerImpl::construct(&__v.as_pointer(), { 1 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); libcc2rs::register_static_destructor(|| { Destructible::destroy(global_0.with(Value::clone).as_pointer()) }); @@ -32,7 +29,11 @@ pub fn exit_from_nested_call_1() { printf!(b"local static %d\n", { (*local_static_2.with(Value::clone).borrow()).tag }); - let automatic: Value = Rc::new(RefCell::new(Tracker::new({ 3 }))); + let automatic: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + TrackerImpl::construct(&__v.as_pointer(), { 3 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); let _dtor_automatic = std::mem::DropGuard::new(automatic.clone(), |__p| { Destructible::destroy(__p.as_pointer()) }); @@ -40,7 +41,11 @@ pub fn exit_from_nested_call_1() { } thread_local!( static local_static_2: Value = { - let __static = Rc::new(RefCell::new(Tracker::new({ 2 }))); + let __static = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + TrackerImpl::construct(&__v.as_pointer(), { 2 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); libcc2rs::register_static_destructor(|| { Destructible::destroy(local_static_2.with(Value::clone).as_pointer()) }); @@ -57,9 +62,14 @@ fn main_0() -> i32 { return 0; } pub trait TrackerImpl { + fn construct(&self, tag: i32); fn destructor(&self); } impl TrackerImpl for Ptr { + fn construct(&self, mut tag: i32) { + field!((*self), tag).write(tag); + printf!(b"construct %d\n", tag); + } fn destructor(&self) { printf!(b"destroy %d\n", (*self).with(|__s| __s.tag)); } 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 e33aa6afc7..58b75dbf81 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 @@ -6,27 +6,20 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Destructible, Default)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Destructible, Default)] #[byte_size(4)] pub struct Tracker { #[offset(0)] pub tag: i32, } -impl Tracker { - pub fn new(mut tag: i32) -> Self { - let __this: Tracker = Self { tag: tag }; - printf!(b"construct %d\n", tag); - __this - } -} -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Destructible, Default)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Destructible, Default)] #[byte_size(4)] pub struct HoldsTracker { #[offset(0)] #[byte_size(4)] pub member: Tracker, } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct TriviallyDestructible { #[offset(0)] @@ -34,7 +27,11 @@ pub struct TriviallyDestructible { } thread_local!( pub static global_0: Value = { - let __static = Rc::new(RefCell::new(Tracker::new({ 1 }))); + let __static = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + TrackerImpl::construct(&__v.as_pointer(), { 1 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); libcc2rs::register_static_destructor(|| { Destructible::destroy(global_0.with(Value::clone).as_pointer()) }); @@ -49,7 +46,11 @@ pub fn test_3() { } thread_local!( static never_constructed_2: Value = { - let __static = Rc::new(RefCell::new(Tracker::new({ 100 }))); + let __static = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + TrackerImpl::construct(&__v.as_pointer(), { 100 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); libcc2rs::register_static_destructor(|| { Destructible::destroy(never_constructed_2.with(Value::clone).as_pointer()) }); @@ -61,7 +62,11 @@ pub fn local_static_during_global_init_4() -> Ptr { } thread_local!( pub static before_6: Value = { - let __static = Rc::new(RefCell::new(Tracker::new({ 2 }))); + let __static = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + TrackerImpl::construct(&__v.as_pointer(), { 2 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); libcc2rs::register_static_destructor(|| { Destructible::destroy(before_6.with(Value::clone).as_pointer()) }); @@ -70,9 +75,13 @@ thread_local!( ); thread_local!( pub static after_7: Value = { - let __static = Rc::new(RefCell::new(Tracker::new({ - (({ local_static_during_global_init_4() }).with(|__s| __s.tag) + 1) - }))); + let __static = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + TrackerImpl::construct(&__v.as_pointer(), { + (({ local_static_during_global_init_4() }).with(|__s| __s.tag) + 1) + }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); libcc2rs::register_static_destructor(|| { Destructible::destroy(after_7.with(Value::clone).as_pointer()) }); @@ -82,7 +91,11 @@ thread_local!( thread_local!( pub static holder_8: Value = { let __static = Rc::new(RefCell::new(HoldsTracker { - member: Tracker::new({ 5 }), + member: { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + TrackerImpl::construct(&__v.as_pointer(), { 5 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }, })); libcc2rs::register_static_destructor(|| { Destructible::destroy(holder_8.with(Value::clone).as_pointer()) @@ -94,12 +107,16 @@ thread_local!( pub static trivial_9: Value = Rc::new(RefCell::new(TriviallyDestructible { value: 6 })); ); -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(1)] pub struct p3_reverse_completion_order_WithStaticMember {} thread_local!( pub static member_10: Value = { - let __static = Rc::new(RefCell::new(Tracker::new({ 7 }))); + let __static = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + TrackerImpl::construct(&__v.as_pointer(), { 7 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); libcc2rs::register_static_destructor(|| { Destructible::destroy(member_10.with(Value::clone).as_pointer()) }); @@ -110,7 +127,11 @@ thread_local!( pub static capturing_11: Value i32>> = { let __static = Rc::new(RefCell::new(lambda!( { - let captured: Value = Rc::new(RefCell::new(Tracker::new({ 8 }))); + let captured: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + TrackerImpl::construct(&__v.as_pointer(), { 8 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); }, || -> i32 { return { (*captured.borrow()).tag }; @@ -138,7 +159,11 @@ pub fn test_14() { } thread_local!( static during_init_5: Value = { - let __static = Rc::new(RefCell::new(Tracker::new({ 3 }))); + let __static = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + TrackerImpl::construct(&__v.as_pointer(), { 3 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); libcc2rs::register_static_destructor(|| { Destructible::destroy(during_init_5.with(Value::clone).as_pointer()) }); @@ -147,7 +172,11 @@ thread_local!( ); thread_local!( static during_main_13: Value = { - let __static = Rc::new(RefCell::new(Tracker::new({ 9 }))); + let __static = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + TrackerImpl::construct(&__v.as_pointer(), { 9 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); libcc2rs::register_static_destructor(|| { Destructible::destroy(during_main_13.with(Value::clone).as_pointer()) }); @@ -173,10 +202,15 @@ impl HoldsTrackerImpl for Ptr { } } pub trait TrackerImpl { + fn construct(&self, tag: i32); fn destructor(&self); fn touch(&self); } impl TrackerImpl for Ptr { + fn construct(&self, mut tag: i32) { + field!((*self), tag).write(tag); + printf!(b"construct %d\n", tag); + } fn destructor(&self) { printf!(b"destroy %d\n", (*self).with(|__s| __s.tag)); } diff --git a/tests/std/basic/basic.start/out/unsafe/basic.start.term-p1-exit.rs b/tests/std/basic/basic.start/out/unsafe/basic.start.term-p1-exit.rs index 8a7322ec46..dabbb973ac 100644 --- a/tests/std/basic/basic.start/out/unsafe/basic.start.term-p1-exit.rs +++ b/tests/std/basic/basic.start/out/unsafe/basic.start.term-p1-exit.rs @@ -7,15 +7,14 @@ use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; #[repr(C)] -#[derive(Clone, VaArg, FnPtrArg, DestructibleUnsafe, Default)] +#[derive(Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, DestructibleUnsafe, Default)] pub struct Tracker { pub tag: i32, } impl Tracker { - pub unsafe fn new(mut tag: i32) -> Self { - let mut this = Self { tag: tag }; + pub unsafe fn construct(&mut self, mut tag: i32) { + (&raw mut self.tag).write(tag); (unsafe { libc::printf(c"construct %d\n".as_ptr() as *const libc::c_char, (tag)) }); - this } pub unsafe fn destructor(&mut self) { (unsafe { libc::printf(c"destroy %d\n".as_ptr() as *const libc::c_char, (self.tag)) }); @@ -23,7 +22,11 @@ impl Tracker { } pub static mut global_0: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { { - let __static = Tracker::new({ 1 }); + let __static = { + let mut __v: Tracker = ::zero_init(); + Tracker::construct(&mut __v, { 1 }); + __v + }; libcc2rs::register_static_destructor(|| { DestructibleUnsafe::destroy(&raw mut *std::cell::LazyCell::force_mut( &mut *&raw mut global_0, @@ -39,7 +42,11 @@ pub unsafe fn exit_from_nested_call_1() { ((*std::cell::LazyCell::force_mut(&mut *&raw mut local_static_2)).tag), ) }); - let mut automatic: Tracker = Tracker::new({ 3 }); + let mut automatic: Tracker = { + let mut __v: Tracker = ::zero_init(); + Tracker::construct(&mut __v, { 3 }); + __v + }; let _dtor_automatic = std::mem::DropGuard::new(&raw mut automatic, |__p| unsafe { DestructibleUnsafe::destroy(__p) }); @@ -47,7 +54,11 @@ pub unsafe fn exit_from_nested_call_1() { } static mut local_static_2: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { { - let __static = Tracker::new({ 2 }); + let __static = { + let mut __v: Tracker = ::zero_init(); + Tracker::construct(&mut __v, { 2 }); + __v + }; libcc2rs::register_static_destructor(|| { DestructibleUnsafe::destroy(&raw mut *std::cell::LazyCell::force_mut( &mut *&raw mut local_static_2, 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 a663ccf35a..1dcdcb2ff4 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 @@ -7,15 +7,14 @@ use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; #[repr(C)] -#[derive(Clone, VaArg, FnPtrArg, DestructibleUnsafe, Default)] +#[derive(Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, DestructibleUnsafe, Default)] pub struct Tracker { pub tag: i32, } impl Tracker { - pub unsafe fn new(mut tag: i32) -> Self { - let mut this = Self { tag: tag }; + pub unsafe fn construct(&mut self, mut tag: i32) { + (&raw mut self.tag).write(tag); (unsafe { libc::printf(c"construct %d\n".as_ptr() as *const libc::c_char, (tag)) }); - this } pub unsafe fn destructor(&mut self) { (unsafe { libc::printf(c"destroy %d\n".as_ptr() as *const libc::c_char, (self.tag)) }); @@ -25,7 +24,7 @@ impl Tracker { } } #[repr(C)] -#[derive(Clone, VaArg, FnPtrArg, DestructibleUnsafe, Default)] +#[derive(Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, DestructibleUnsafe, Default)] pub struct HoldsTracker { pub member: Tracker, } @@ -35,13 +34,17 @@ impl HoldsTracker { } } #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] pub struct TriviallyDestructible { pub value: i32, } pub static mut global_0: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { { - let __static = Tracker::new({ 1 }); + let __static = { + let mut __v: Tracker = ::zero_init(); + Tracker::construct(&mut __v, { 1 }); + __v + }; libcc2rs::register_static_destructor(|| { DestructibleUnsafe::destroy(&raw mut *std::cell::LazyCell::force_mut( &mut *&raw mut global_0, @@ -61,7 +64,11 @@ pub unsafe fn test_3() { static mut never_constructed_2: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { { - let __static = Tracker::new({ 100 }); + let __static = { + let mut __v: Tracker = ::zero_init(); + Tracker::construct(&mut __v, { 100 }); + __v + }; libcc2rs::register_static_destructor(|| { DestructibleUnsafe::destroy(&raw mut *std::cell::LazyCell::force_mut( &mut *&raw mut never_constructed_2, @@ -75,7 +82,11 @@ pub unsafe fn local_static_during_global_init_4() -> *const Tracker { } pub static mut before_6: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { { - let __static = Tracker::new({ 2 }); + let __static = { + let mut __v: Tracker = ::zero_init(); + Tracker::construct(&mut __v, { 2 }); + __v + }; libcc2rs::register_static_destructor(|| { DestructibleUnsafe::destroy(&raw mut *std::cell::LazyCell::force_mut( &mut *&raw mut before_6, @@ -86,8 +97,13 @@ pub static mut before_6: std::cell::LazyCell = std::cell::LazyCell::new }); pub static mut after_7: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { { - let __static = - Tracker::new({ (((*(unsafe { local_static_during_global_init_4() })).tag) + (1)) }); + let __static = { + let mut __v: Tracker = ::zero_init(); + Tracker::construct(&mut __v, { + (((*(unsafe { local_static_during_global_init_4() })).tag) + (1)) + }); + __v + }; libcc2rs::register_static_destructor(|| { DestructibleUnsafe::destroy(&raw mut *std::cell::LazyCell::force_mut( &mut *&raw mut after_7, @@ -99,7 +115,11 @@ 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: Tracker::new({ 5 }), + member: { + let mut __v: Tracker = ::zero_init(); + Tracker::construct(&mut __v, { 5 }); + __v + }, }; libcc2rs::register_static_destructor(|| { DestructibleUnsafe::destroy(&raw mut *std::cell::LazyCell::force_mut( @@ -112,11 +132,15 @@ pub static mut holder_8: std::cell::LazyCell = std::cell::LazyCell pub static mut trivial_9: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { TriviallyDestructible { value: 6 } }); #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] pub struct p3_reverse_completion_order_WithStaticMember {} pub static mut member_10: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { { - let __static = Tracker::new({ 7 }); + let __static = { + let mut __v: Tracker = ::zero_init(); + Tracker::construct(&mut __v, { 7 }); + __v + }; libcc2rs::register_static_destructor(|| { DestructibleUnsafe::destroy(&raw mut *std::cell::LazyCell::force_mut( &mut *&raw mut member_10, @@ -130,7 +154,11 @@ pub static mut capturing_11: std::cell::LazyCell i32>> = { let __static = lambda_unsafe!( { - let captured: Tracker = Tracker::new({ 8 }); + let captured: Tracker = { + let mut __v: Tracker = ::zero_init(); + Tracker::construct(&mut __v, { 8 }); + __v + }; }, || -> i32 { return captured.tag; @@ -167,7 +195,11 @@ pub unsafe fn test_14() { } static mut during_init_5: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { { - let __static = Tracker::new({ 3 }); + let __static = { + let mut __v: Tracker = ::zero_init(); + Tracker::construct(&mut __v, { 3 }); + __v + }; libcc2rs::register_static_destructor(|| { DestructibleUnsafe::destroy(&raw mut *std::cell::LazyCell::force_mut( &mut *&raw mut during_init_5, @@ -178,7 +210,11 @@ static mut during_init_5: std::cell::LazyCell = std::cell::LazyCell::ne }); static mut during_main_13: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { { - let __static = Tracker::new({ 9 }); + let __static = { + let mut __v: Tracker = ::zero_init(); + Tracker::construct(&mut __v, { 9 }); + __v + }; libcc2rs::register_static_destructor(|| { DestructibleUnsafe::destroy(&raw mut *std::cell::LazyCell::force_mut( &mut *&raw mut during_main_13, diff --git a/tests/std/basic/basic.types/out/refcount/basic.types.general.rs b/tests/std/basic/basic.types/out/refcount/basic.types.general.rs index 14d1147933..d50e4c91c2 100644 --- a/tests/std/basic/basic.types/out/refcount/basic.types.general.rs +++ b/tests/std/basic/basic.types/out/refcount/basic.types.general.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct p2_pointer_to_data_member_bytes_round_trip_HasTwoInts { #[offset(0)] @@ -69,7 +69,7 @@ pub fn test_0() { }; assert!(({ ((*null_pm.borrow()) == -1_isize) })); } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct p3_pointer_to_data_member_bytes_copied_to_other_object_HasTwoInts { #[offset(0)] diff --git a/tests/std/basic/basic.types/out/unsafe/basic.types.general.rs b/tests/std/basic/basic.types/out/unsafe/basic.types.general.rs index 7f23f90356..954591d54c 100644 --- a/tests/std/basic/basic.types/out/unsafe/basic.types.general.rs +++ b/tests/std/basic/basic.types/out/unsafe/basic.types.general.rs @@ -7,7 +7,7 @@ use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] pub struct p2_pointer_to_data_member_bytes_round_trip_HasTwoInts { pub first: i32, pub second: i32, @@ -76,7 +76,7 @@ pub unsafe fn test_0() { assert!(((null_pm) == -1_isize)); } #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] pub struct p3_pointer_to_data_member_bytes_copied_to_other_object_HasTwoInts { pub first: i32, pub second: i32, diff --git a/tests/std/basic/out/refcount/basic.def.odr.rs b/tests/std/basic/out/refcount/basic.def.odr.rs index 0a75ca5ce2..201482e23f 100644 --- a/tests/std/basic/out/refcount/basic.def.odr.rs +++ b/tests/std/basic/out/refcount/basic.def.odr.rs @@ -9,8 +9,8 @@ use std::rc::{Rc, Weak}; thread_local!( pub static copyCount_0: Value = Rc::new(RefCell::new(0)); ); -#[derive(Record, ByteRepr, VaArg, FnPtrArg, CppFrom, CopyAssign, Default)] -#[cpp_from(i32, new, value)] +#[derive(Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, CppFrom, CopyAssign, Default)] +#[cpp_from(i32, construct, value)] #[byte_size(4)] pub struct HasConstexprCtor { #[offset(0)] @@ -20,22 +20,14 @@ impl HasConstexprCtor { pub fn reset() { copyCount_0.with(|rc| *rc.borrow_mut() = 0); } - pub fn new(mut v: i32) -> Self { - Self { m: v } - } - pub fn copy_from(o: Ptr) -> Self { - let __this: HasConstexprCtor = Self { - m: o.with(|__s| __s.m), - }; - (*copyCount_0.with(Value::clone).borrow_mut()).prefix_inc(); - __this - } } impl Clone for HasConstexprCtor { fn clone(&self) -> Self { let __src: Value = Rc::new(RefCell::new(HasConstexprCtor { m: self.m.clone() })); - HasConstexprCtor::copy_from(__src.as_pointer()) + let __v: Value = Rc::new(RefCell::new(::zero_init())); + HasConstexprCtorImpl::copy_from(&__v.as_pointer(), __src.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() } } thread_local!( @@ -50,7 +42,7 @@ thread_local!( thread_local!( pub static useCount_4: Value = Rc::new(RefCell::new(0)); ); -#[derive(Record, ByteRepr, VaArg, FnPtrArg, CopyAssign, Destructible)] +#[derive(Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, CopyAssign, Destructible)] #[byte_size(4)] pub struct LifetimeTracked { #[offset(0)] @@ -75,38 +67,62 @@ impl LifetimeTracked { } }); } - pub fn new() -> Self { - let __this: LifetimeTracked = Self { uses: 0 }; - (*ctorCount_1.with(Value::clone).borrow_mut()).prefix_inc(); - __this - } - pub fn copy_from(_a0: Ptr) -> Self { - let __this: LifetimeTracked = Self { uses: 0 }; - (*copyCount_2.with(Value::clone).borrow_mut()).prefix_inc(); - __this - } } impl Clone for LifetimeTracked { fn clone(&self) -> Self { let __src: Value = Rc::new(RefCell::new(LifetimeTracked { uses: self.uses.clone(), })); - LifetimeTracked::copy_from(__src.as_pointer()) + let __v: Value = Rc::new(RefCell::new(::zero_init())); + LifetimeTrackedImpl::copy_from(&__v.as_pointer(), __src.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() } } impl Default for LifetimeTracked { fn default() -> Self { - { LifetimeTracked::new() } + { + { + let __v: Value = + Rc::new(RefCell::new(::zero_init())); + LifetimeTrackedImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + } + } } } pub fn test_5() { ({ HasConstexprCtor::reset() }); - let mut arr: [HasConstexprCtor; 2] = - [HasConstexprCtor::new({ 3 }), HasConstexprCtor::new({ 4 })]; + let mut arr: [HasConstexprCtor; 2] = [ + { + let __v: Value = + Rc::new(RefCell::new(::zero_init())); + HasConstexprCtorImpl::construct(&__v.as_pointer(), { 3 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }, + { + let __v: Value = + Rc::new(RefCell::new(::zero_init())); + HasConstexprCtorImpl::construct(&__v.as_pointer(), { 4 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }, + ]; let f: Value i32>> = Rc::new(RefCell::new(FnPtr:: i32>::new(|| -> i32 { { - return [HasConstexprCtor::new({ 3 }), HasConstexprCtor::new({ 4 })][(1) as usize] + return [ + { + let __v: Value = + Rc::new(RefCell::new(::zero_init())); + HasConstexprCtorImpl::construct(&__v.as_pointer(), { 3 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }, + { + let __v: Value = + Rc::new(RefCell::new(::zero_init())); + HasConstexprCtorImpl::construct(&__v.as_pointer(), { 4 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }, + ][(1) as usize] .m; } }))); @@ -116,11 +132,21 @@ pub fn test_5() { } pub fn test_6() { ({ HasConstexprCtor::reset() }); - let mut k: HasConstexprCtor = HasConstexprCtor::new({ 5 }); + let mut k: HasConstexprCtor = { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + HasConstexprCtorImpl::construct(&__v.as_pointer(), { 5 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }; let f: Value i32>> = Rc::new(RefCell::new(FnPtr:: i32>::new(|| -> i32 { { - return HasConstexprCtor::new({ 5 }).m; + return { + let __v: Value = + Rc::new(RefCell::new(::zero_init())); + HasConstexprCtorImpl::construct(&__v.as_pointer(), { 5 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + } + .m; } }))); assert!((copyCount_0.with(|rc| *rc.borrow()) == 0)); @@ -129,16 +155,34 @@ pub fn test_6() { } pub fn test_7() { ({ HasConstexprCtor::reset() }); - let mut k1: HasConstexprCtor = HasConstexprCtor::new({ 7 }); - let mut k2: HasConstexprCtor = HasConstexprCtor::new({ 9 }); + let mut k1: HasConstexprCtor = { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + HasConstexprCtorImpl::construct(&__v.as_pointer(), { 7 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }; + let mut k2: HasConstexprCtor = { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + HasConstexprCtorImpl::construct(&__v.as_pointer(), { 9 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }; let f: Value i32>> = Rc::new(RefCell::new(FnPtr:: i32>::new( |mut b: bool| -> i32 { { return { (if b { - HasConstexprCtor::new({ 7 }) + { + let __v: Value = + Rc::new(RefCell::new(::zero_init())); + HasConstexprCtorImpl::construct(&__v.as_pointer(), { 7 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + } } else { - HasConstexprCtor::new({ 9 }) + { + let __v: Value = + Rc::new(RefCell::new(::zero_init())); + HasConstexprCtorImpl::construct(&__v.as_pointer(), { 9 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + } }) .m }; @@ -155,14 +199,23 @@ thread_local!( ); pub fn test_9() { ({ HasConstexprCtor::reset() }); - let k: Value = Rc::new(RefCell::new(HasConstexprCtor::new({ 5 }))); + let k: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + HasConstexprCtorImpl::construct(&__v.as_pointer(), { 5 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); let f: Value i32>> = Rc::new(RefCell::new(FnPtr:: i32>::new(|| -> i32 { { return { { (*left_8.with(Value::clone).borrow_mut()).prefix_inc(); - HasConstexprCtor::new({ 5 }) + { + let __v: Value = + Rc::new(RefCell::new(::zero_init())); + HasConstexprCtorImpl::construct(&__v.as_pointer(), { 5 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + } } .m }; @@ -180,7 +233,7 @@ thread_local!( pub trait p4_1_pure_virtual_without_definition_HasPureVirtual { fn f(&mut self); } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct p4_1_pure_virtual_without_definition_OverridesPureVirtual {} pub fn test_11() { @@ -194,7 +247,11 @@ pub fn test_11() { } thread_local!( pub static global_12: Value = { - let __static = Rc::new(RefCell::new(LifetimeTracked::new())); + let __static = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + LifetimeTrackedImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); libcc2rs::register_static_destructor(|| { Destructible::destroy(global_12.with(Value::clone).as_pointer()) }); @@ -215,14 +272,22 @@ pub fn test_13() { } pub fn test_14() { ({ LifetimeTracked::reset() }); - let t: Value = Rc::new(RefCell::new(LifetimeTracked::new())); + let t: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + LifetimeTrackedImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); let _dtor_t = std::mem::DropGuard::new(t.clone(), |__p| Destructible::destroy(__p.as_pointer())); let r: Ptr = t.as_pointer(); let f: Value> = Rc::new(RefCell::new(lambda!( { - let r: Value = - Rc::new(RefCell::new(LifetimeTracked::copy_from({ (r).clone() }))); + let r: Value = Rc::new(RefCell::new({ + let __v: Value = + Rc::new(RefCell::new(::zero_init())); + LifetimeTrackedImpl::copy_from(&__v.as_pointer(), { (r).clone() }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); }, || { ({ LifetimeTrackedImpl::use_(&r.as_pointer()) }); @@ -251,15 +316,27 @@ pub fn test_15() { } pub fn test_16() { ({ HasConstexprCtor::reset() }); - let k: Value = Rc::new(RefCell::new(HasConstexprCtor::new({ 5 }))); + let k: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + HasConstexprCtorImpl::construct(&__v.as_pointer(), { 5 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); let f: Value i32>> = Rc::new(RefCell::new(lambda!( { - let k: Value = Rc::new(RefCell::new(HasConstexprCtor::copy_from({ - k.as_pointer() - }))); + let k: Value = Rc::new(RefCell::new({ + let __v: Value = + Rc::new(RefCell::new(::zero_init())); + HasConstexprCtorImpl::copy_from(&__v.as_pointer(), { k.as_pointer() }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); }, || -> i32 { - let mut c: HasConstexprCtor = HasConstexprCtor::copy_from({ k.as_pointer() }); + let mut c: HasConstexprCtor = { + let __v: Value = + Rc::new(RefCell::new(::zero_init())); + HasConstexprCtorImpl::copy_from(&__v.as_pointer(), { k.as_pointer() }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }; return c.m; } ))); @@ -280,8 +357,8 @@ pub fn test_17() { thread_local!( pub static copies_18: Value = Rc::new(RefCell::new(0_i32)); ); -#[derive(Record, ByteRepr, VaArg, FnPtrArg, CppFrom, CopyAssign, Default)] -#[cpp_from(i32, new, value)] +#[derive(Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, CppFrom, CopyAssign, Default)] +#[cpp_from(i32, construct, value)] #[byte_size(8)] pub struct p5_2_mutable_subobject_is_odr_used_HasMutableMember { #[offset(0)] @@ -289,22 +366,6 @@ pub struct p5_2_mutable_subobject_is_odr_used_HasMutableMember { #[offset(4)] pub scratch: i32, } -impl p5_2_mutable_subobject_is_odr_used_HasMutableMember { - pub fn new(mut v: i32) -> Self { - Self { - value: v, - scratch: 0, - } - } - pub fn copy_from(o: Ptr) -> Self { - let __this: p5_2_mutable_subobject_is_odr_used_HasMutableMember = Self { - value: o.with(|__s| __s.value), - scratch: o.with(|__s| __s.scratch), - }; - (*copies_18.with(Value::clone).borrow_mut()).prefix_inc(); - __this - } -} impl Clone for p5_2_mutable_subobject_is_odr_used_HasMutableMember { fn clone(&self) -> Self { let __src: Value = Rc::new( @@ -313,21 +374,40 @@ impl Clone for p5_2_mutable_subobject_is_odr_used_HasMutableMember { scratch: self.scratch.clone(), }), ); - p5_2_mutable_subobject_is_odr_used_HasMutableMember::copy_from(__src.as_pointer()) + let __v: Value = Rc::new( + RefCell::new(::zero_init()), + ); + p5_2_mutable_subobject_is_odr_used_HasMutableMemberImpl::copy_from( + &__v.as_pointer(), + __src.as_pointer(), + ); + Rc::try_unwrap(__v).ok().unwrap().into_inner() } } pub fn test_19() { - let k: Value = Rc::new(RefCell::new( - p5_2_mutable_subobject_is_odr_used_HasMutableMember::new({ 5 }), - )); + let k: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new( + RefCell::new(::zero_init()), + ); + p5_2_mutable_subobject_is_odr_used_HasMutableMemberImpl::construct(&__v.as_pointer(), { + 5 + }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); let f: Value i32>> = Rc::new(RefCell::new(lambda!( { let k: Value = - Rc::new(RefCell::new( - p5_2_mutable_subobject_is_odr_used_HasMutableMember::copy_from({ - k.as_pointer() - }), - )); + Rc::new(RefCell::new({ + let __v: Value = + Rc::new(RefCell::new( + ::zero_init(), + )); + p5_2_mutable_subobject_is_odr_used_HasMutableMemberImpl::copy_from( + &__v.as_pointer(), + { k.as_pointer() }, + ); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); }, || -> i32 { return { (*k.borrow()).value }; @@ -337,7 +417,9 @@ pub fn test_19() { assert!(({ (({ (*f.borrow()).call() }) == 5) })); assert!((copies_18.with(|rc| *rc.borrow()) == 1)); } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, CopyAssign, Destructible, Default)] +#[derive( + Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, CopyAssign, Destructible, Default, +)] #[byte_size(8)] pub struct p6_unevaluated_binding_needs_no_capture_HasTwoMembers { #[offset(0)] @@ -351,8 +433,18 @@ pub fn test_20() { ({ LifetimeTracked::reset() }); let p: Value = Rc::new(RefCell::new( p6_unevaluated_binding_needs_no_capture_HasTwoMembers { - a: LifetimeTracked::new(), - b: LifetimeTracked::new(), + a: { + let __v: Value = + Rc::new(RefCell::new(::zero_init())); + LifetimeTrackedImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }, + b: { + let __v: Value = + Rc::new(RefCell::new(::zero_init())); + LifetimeTrackedImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }, }, )); let _dtor_p = @@ -369,7 +461,9 @@ pub fn test_20() { assert!((copyCount_2.with(|rc| *rc.borrow()) == 0)); assert!((useCount_4.with(|rc| *rc.borrow()) == 0)); } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, CopyAssign, Destructible)] +#[derive( + Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, CopyAssign, Destructible, +)] #[byte_size(8)] pub struct p7_member_access_captures_this_HasMemberUsedInLambda { #[offset(0)] @@ -406,7 +500,9 @@ pub fn test_22() { assert!(({ (*c.borrow()).x.uses } == 2)); assert!((copyCount_2.with(|rc| *rc.borrow()) == 0)); } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, CopyAssign, Destructible, Default)] +#[derive( + Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, CopyAssign, Destructible, Default, +)] #[byte_size(4)] pub struct p7_unevaluated_this_needs_no_capture_HasMemberUsedInLambda { #[offset(0)] @@ -452,77 +548,98 @@ pub fn test_26() { thread_local!( pub static t_ctors_27: Value = Rc::new(RefCell::new(0_i32)); ); -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg)] #[byte_size(1)] pub struct p9_destructor_not_potentially_invoked_HasUndefinedDestructor {} -impl p9_destructor_not_potentially_invoked_HasUndefinedDestructor { - pub fn new() -> Self { - let __this: p9_destructor_not_potentially_invoked_HasUndefinedDestructor = Self {}; - (*t_ctors_27.with(Value::clone).borrow_mut()).prefix_inc(); - __this - } -} impl Default for p9_destructor_not_potentially_invoked_HasUndefinedDestructor { fn default() -> Self { - { p9_destructor_not_potentially_invoked_HasUndefinedDestructor::new() } + { + { + let __v: Value = + Rc::new(RefCell::new( + ::zero_init(), + )); + p9_destructor_not_potentially_invoked_HasUndefinedDestructorImpl::construct( + &__v.as_pointer(), + ); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + } + } } } pub fn test_28() { - let mut p: Ptr = - Ptr::alloc(p9_destructor_not_potentially_invoked_HasUndefinedDestructor::new()); + let mut p: Ptr = Ptr::alloc({ + let __v: Value = + Rc::new(RefCell::new( + ::zero_init(), + )); + p9_destructor_not_potentially_invoked_HasUndefinedDestructorImpl::construct( + &__v.as_pointer(), + ); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }); assert!(!((p).is_null())); assert!((t_ctors_27.with(|rc| *rc.borrow()) == 1)); } thread_local!( pub static ctor_calls_29: Value = Rc::new(RefCell::new(0_i32)); ); -#[derive(Record, ByteRepr, FnPtrArg)] +#[derive(Record, ByteRepr, ZeroInitialization, FnPtrArg)] #[byte_size(4)] pub struct p9_unused_copy_constructor_HasUndefinedCopyCtor { #[offset(0)] pub value: i32, } -impl p9_unused_copy_constructor_HasUndefinedCopyCtor { - pub fn new() -> Self { - let __this: p9_unused_copy_constructor_HasUndefinedCopyCtor = Self { value: 3 }; - (*ctor_calls_29.with(Value::clone).borrow_mut()).prefix_inc(); - __this - } -} impl Default for p9_unused_copy_constructor_HasUndefinedCopyCtor { fn default() -> Self { - { p9_unused_copy_constructor_HasUndefinedCopyCtor::new() } + { + { + let __v: Value = Rc::new( + RefCell::new(::zero_init()), + ); + p9_unused_copy_constructor_HasUndefinedCopyCtorImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + } + } } } pub fn read_30(object: Ptr) -> i32 { return object.with(|__s| __s.value); } pub fn test_31() { - let a: Value = Rc::new(RefCell::new( - p9_unused_copy_constructor_HasUndefinedCopyCtor::new(), - )); + let a: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new( + ::zero_init(), + )); + p9_unused_copy_constructor_HasUndefinedCopyCtorImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); assert!((({ read_30(a.as_pointer(),) }) == 3)); assert!((ctor_calls_29.with(|rc| *rc.borrow()) == 1)); } thread_local!( pub static m_ctors_32: Value = Rc::new(RefCell::new(0_i32)); ); -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg)] #[byte_size(1)] pub struct p9_unused_implicit_assignment_HasUndefinedCopyAssignment {} -impl p9_unused_implicit_assignment_HasUndefinedCopyAssignment { - pub fn new() -> Self { - let __this: p9_unused_implicit_assignment_HasUndefinedCopyAssignment = Self {}; - (*m_ctors_32.with(Value::clone).borrow_mut()).prefix_inc(); - __this - } -} impl Default for p9_unused_implicit_assignment_HasUndefinedCopyAssignment { fn default() -> Self { - { p9_unused_implicit_assignment_HasUndefinedCopyAssignment::new() } + { + { + let __v: Value = + Rc::new(RefCell::new( + ::zero_init(), + )); + p9_unused_implicit_assignment_HasUndefinedCopyAssignmentImpl::construct( + &__v.as_pointer(), + ); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + } + } } } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(1)] pub struct p9_unused_implicit_assignment_HasMemberWithUndefinedCopyAssignment { #[offset(0)] @@ -668,20 +785,39 @@ impl p4_1_pure_virtual_without_definition_HasPureVirtual } } pub trait HasConstexprCtorImpl { + fn construct(&self, v: i32); + fn copy_from(&self, o: Ptr); fn copy_assign(&self, _a0: Ptr) -> Ptr; } impl HasConstexprCtorImpl for Ptr { + fn construct(&self, mut v: i32) { + field!((*self), m).write(v); + } + fn copy_from(&self, o: Ptr) { + field!((*self), m).write(o.with(|__s| __s.m)); + (*copyCount_0.with(Value::clone).borrow_mut()).prefix_inc(); + } fn copy_assign(&self, _a0: Ptr) -> Ptr { field!((*self), m).write(_a0.with(|__s| __s.m)); return (*self).clone(); } } pub trait LifetimeTrackedImpl { + fn construct(&self); + fn copy_from(&self, _a0: Ptr); fn destructor(&self); fn use_(&self); fn copy_assign(&self, _a0: Ptr) -> Ptr; } impl LifetimeTrackedImpl for Ptr { + fn construct(&self) { + field!((*self), uses).write(0); + (*ctorCount_1.with(Value::clone).borrow_mut()).prefix_inc(); + } + fn copy_from(&self, _a0: Ptr) { + field!((*self), uses).write(0); + (*copyCount_2.with(Value::clone).borrow_mut()).prefix_inc(); + } fn destructor(&self) { (*dtorCount_3.with(Value::clone).borrow_mut()).prefix_inc(); } @@ -697,6 +833,8 @@ impl LifetimeTrackedImpl for Ptr { } } pub trait p5_2_mutable_subobject_is_odr_used_HasMutableMemberImpl { + fn construct(&self, v: i32); + fn copy_from(&self, o: Ptr); fn copy_assign( &self, _a0: Ptr, @@ -705,6 +843,15 @@ pub trait p5_2_mutable_subobject_is_odr_used_HasMutableMemberImpl { impl p5_2_mutable_subobject_is_odr_used_HasMutableMemberImpl for Ptr { + fn construct(&self, mut v: i32) { + field!((*self), value).write(v); + field!((*self), scratch).write(0); + } + fn copy_from(&self, o: Ptr) { + field!((*self), value).write(o.with(|__s| __s.value)); + field!((*self), scratch).write(o.with(|__s| __s.scratch)); + (*copies_18.with(Value::clone).borrow_mut()).prefix_inc(); + } fn copy_assign( &self, _a0: Ptr, @@ -802,6 +949,7 @@ impl p7_unevaluated_this_needs_no_capture_HasMemberUsedInLambdaImpl } } pub trait p9_destructor_not_potentially_invoked_HasUndefinedDestructorImpl { + fn construct(&self); fn destructor(&self) { unimplemented!() } @@ -809,8 +957,23 @@ pub trait p9_destructor_not_potentially_invoked_HasUndefinedDestructorImpl { impl p9_destructor_not_potentially_invoked_HasUndefinedDestructorImpl for Ptr { + fn construct(&self) { + (*t_ctors_27.with(Value::clone).borrow_mut()).prefix_inc(); + } +} +pub trait p9_unused_copy_constructor_HasUndefinedCopyCtorImpl { + fn construct(&self); +} +impl p9_unused_copy_constructor_HasUndefinedCopyCtorImpl + for Ptr +{ + fn construct(&self) { + field!((*self), value).write(3); + (*ctor_calls_29.with(Value::clone).borrow_mut()).prefix_inc(); + } } pub trait p9_unused_implicit_assignment_HasUndefinedCopyAssignmentImpl { + fn construct(&self); fn copy_assign( &self, _a0: Ptr, @@ -821,6 +984,9 @@ pub trait p9_unused_implicit_assignment_HasUndefinedCopyAssignmentImpl { impl p9_unused_implicit_assignment_HasUndefinedCopyAssignmentImpl for Ptr { + fn construct(&self) { + (*m_ctors_32.with(Value::clone).borrow_mut()).prefix_inc(); + } } pub fn __cpp2rust_init_globals() { let _ = copyCount_0.with(|_| ()); diff --git a/tests/std/basic/out/unsafe/basic.def.odr.rs b/tests/std/basic/out/unsafe/basic.def.odr.rs index c404f4c1f8..624bd9f248 100644 --- a/tests/std/basic/out/unsafe/basic.def.odr.rs +++ b/tests/std/basic/out/unsafe/basic.def.odr.rs @@ -8,8 +8,8 @@ use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; pub static mut copyCount_0: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); #[repr(C)] -#[derive(VaArg, FnPtrArg, CppFromUnsafe, CopyAssignUnsafe, Default)] -#[cpp_from(i32, new, value)] +#[derive(VaArg, FnPtrArg, ZeroInitializationUnsafe, CppFromUnsafe, CopyAssignUnsafe, Default)] +#[cpp_from(i32, construct, value)] pub struct HasConstexprCtor { pub m: i32, } @@ -17,14 +17,12 @@ impl HasConstexprCtor { pub unsafe fn reset() { (*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_0)) = 0; } - pub unsafe fn new(mut v: i32) -> Self { - let mut this = Self { m: v }; - this + pub unsafe fn construct(&mut self, mut v: i32) { + (&raw mut self.m).write(v); } - pub unsafe fn copy_from(o: *const HasConstexprCtor) -> Self { - let mut this = Self { m: (*o).m }; + pub unsafe fn copy_from(&mut self, o: *const HasConstexprCtor) { + (&raw mut self.m).write((*o).m); (*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_0)).prefix_inc(); - this } pub unsafe fn copy_assign(&mut self, _a0: *const HasConstexprCtor) -> *mut HasConstexprCtor { self.m = (*_a0).m; @@ -33,7 +31,11 @@ impl HasConstexprCtor { } impl Clone for HasConstexprCtor { fn clone(&self) -> Self { - unsafe { HasConstexprCtor::copy_from(self as *const HasConstexprCtor) } + unsafe { + let mut __v: HasConstexprCtor = ::zero_init(); + HasConstexprCtor::copy_from(&mut __v, self as *const HasConstexprCtor); + __v + } } } pub static mut ctorCount_1: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); @@ -41,7 +43,7 @@ pub static mut copyCount_2: std::cell::LazyCell = std::cell::LazyCell::new( pub static mut dtorCount_3: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); pub static mut useCount_4: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); #[repr(C)] -#[derive(VaArg, FnPtrArg, CopyAssignUnsafe, DestructibleUnsafe)] +#[derive(VaArg, FnPtrArg, ZeroInitializationUnsafe, CopyAssignUnsafe, DestructibleUnsafe)] pub struct LifetimeTracked { pub uses: i32, } @@ -58,15 +60,13 @@ impl LifetimeTracked { (*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_2)) }; } - pub unsafe fn new() -> Self { - let mut this = Self { uses: 0 }; + pub unsafe fn construct(&mut self) { + (&raw mut self.uses).write(0); (*std::cell::LazyCell::force_mut(&mut *&raw mut ctorCount_1)).prefix_inc(); - this } - pub unsafe fn copy_from(_a0: *const LifetimeTracked) -> Self { - let mut this = Self { uses: 0 }; + pub unsafe fn copy_from(&mut self, _a0: *const LifetimeTracked) { + (&raw mut self.uses).write(0); (*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_2)).prefix_inc(); - this } pub unsafe fn destructor(&mut self) { (*std::cell::LazyCell::force_mut(&mut *&raw mut dtorCount_3)).prefix_inc(); @@ -82,20 +82,53 @@ impl LifetimeTracked { } impl Clone for LifetimeTracked { fn clone(&self) -> Self { - unsafe { LifetimeTracked::copy_from(self as *const LifetimeTracked) } + unsafe { + let mut __v: LifetimeTracked = ::zero_init(); + LifetimeTracked::copy_from(&mut __v, self as *const LifetimeTracked); + __v + } } } impl Default for LifetimeTracked { fn default() -> Self { - unsafe { LifetimeTracked::new() } + unsafe { + { + let mut __v: LifetimeTracked = ::zero_init(); + LifetimeTracked::construct(&mut __v); + __v + } + } } } pub unsafe fn test_5() { (unsafe { HasConstexprCtor::reset() }); - let arr: [HasConstexprCtor; 2] = [HasConstexprCtor::new({ 3 }), HasConstexprCtor::new({ 4 })]; + let arr: [HasConstexprCtor; 2] = [ + { + let mut __v: HasConstexprCtor = ::zero_init(); + HasConstexprCtor::construct(&mut __v, { 3 }); + __v + }, + { + let mut __v: HasConstexprCtor = ::zero_init(); + HasConstexprCtor::construct(&mut __v, { 4 }); + __v + }, + ]; let mut f: FnPtr i32> = FnPtr:: i32>::new(|| -> i32 { unsafe { - return [HasConstexprCtor::new({ 3 }), HasConstexprCtor::new({ 4 })][(1) as usize].m; + return [ + { + let mut __v: HasConstexprCtor = ::zero_init(); + HasConstexprCtor::construct(&mut __v, { 3 }); + __v + }, + { + let mut __v: HasConstexprCtor = ::zero_init(); + HasConstexprCtor::construct(&mut __v, { 4 }); + __v + }, + ][(1) as usize] + .m; } }); assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_0)) == (0))); @@ -104,10 +137,19 @@ pub unsafe fn test_5() { } pub unsafe fn test_6() { (unsafe { HasConstexprCtor::reset() }); - let k: HasConstexprCtor = HasConstexprCtor::new({ 5 }); + let k: HasConstexprCtor = { + let mut __v: HasConstexprCtor = ::zero_init(); + HasConstexprCtor::construct(&mut __v, { 5 }); + __v + }; let mut f: FnPtr i32> = FnPtr:: i32>::new(|| -> i32 { unsafe { - return HasConstexprCtor::new({ 5 }).m; + return { + let mut __v: HasConstexprCtor = ::zero_init(); + HasConstexprCtor::construct(&mut __v, { 5 }); + __v + } + .m; } }); assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_0)) == (0))); @@ -116,14 +158,30 @@ pub unsafe fn test_6() { } pub unsafe fn test_7() { (unsafe { HasConstexprCtor::reset() }); - let k1: HasConstexprCtor = HasConstexprCtor::new({ 7 }); - let k2: HasConstexprCtor = HasConstexprCtor::new({ 9 }); + let k1: HasConstexprCtor = { + let mut __v: HasConstexprCtor = ::zero_init(); + HasConstexprCtor::construct(&mut __v, { 7 }); + __v + }; + let k2: HasConstexprCtor = { + let mut __v: HasConstexprCtor = ::zero_init(); + HasConstexprCtor::construct(&mut __v, { 9 }); + __v + }; let mut f: FnPtr i32> = FnPtr:: i32>::new(|mut b: bool| -> i32 { unsafe { return (if b { - HasConstexprCtor::new({ 7 }) + { + let mut __v: HasConstexprCtor = ::zero_init(); + HasConstexprCtor::construct(&mut __v, { 7 }); + __v + } } else { - HasConstexprCtor::new({ 9 }) + { + let mut __v: HasConstexprCtor = ::zero_init(); + HasConstexprCtor::construct(&mut __v, { 9 }); + __v + } }) .m; } @@ -136,12 +194,20 @@ pub unsafe fn test_7() { pub static mut left_8: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0_i32 }); pub unsafe fn test_9() { (unsafe { HasConstexprCtor::reset() }); - let k: HasConstexprCtor = HasConstexprCtor::new({ 5 }); + let k: HasConstexprCtor = { + let mut __v: HasConstexprCtor = ::zero_init(); + HasConstexprCtor::construct(&mut __v, { 5 }); + __v + }; let mut f: FnPtr i32> = FnPtr:: i32>::new(|| -> i32 { unsafe { return { (*std::cell::LazyCell::force_mut(&mut *&raw mut left_8)).prefix_inc(); - HasConstexprCtor::new({ 5 }) + { + let mut __v: HasConstexprCtor = ::zero_init(); + HasConstexprCtor::construct(&mut __v, { 5 }); + __v + } } .m; } @@ -158,7 +224,7 @@ pub unsafe trait p4_1_pure_virtual_without_definition_HasPureVirtual { unsafe fn f(&mut self); } #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] pub struct p4_1_pure_virtual_without_definition_OverridesPureVirtual {} pub unsafe fn test_11() { let mut d: p4_1_pure_virtual_without_definition_OverridesPureVirtual = @@ -171,7 +237,11 @@ pub unsafe fn test_11() { pub static mut global_12: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { { - let __static = LifetimeTracked::new(); + let __static = { + let mut __v: LifetimeTracked = ::zero_init(); + LifetimeTracked::construct(&mut __v); + __v + }; libcc2rs::register_static_destructor(|| { DestructibleUnsafe::destroy(&raw mut *std::cell::LazyCell::force_mut( &mut *&raw mut global_12, @@ -196,14 +266,22 @@ pub unsafe fn test_13() { } pub unsafe fn test_14() { (unsafe { LifetimeTracked::reset() }); - let mut t: LifetimeTracked = LifetimeTracked::new(); + let mut t: LifetimeTracked = { + let mut __v: LifetimeTracked = ::zero_init(); + LifetimeTracked::construct(&mut __v); + __v + }; let _dtor_t = std::mem::DropGuard::new(&raw mut t, |__p| unsafe { DestructibleUnsafe::destroy(__p) }); let r: *mut LifetimeTracked = &mut t; let mut f: FnPtr = lambda_unsafe!( { - let r: LifetimeTracked = LifetimeTracked::copy_from({ r }); + let r: LifetimeTracked = { + let mut __v: LifetimeTracked = ::zero_init(); + LifetimeTracked::copy_from(&mut __v, { r }); + __v + }; }, || { (unsafe { LifetimeTracked::use_(&r) }); @@ -233,13 +311,25 @@ pub unsafe fn test_15() { } pub unsafe fn test_16() { (unsafe { HasConstexprCtor::reset() }); - let k: HasConstexprCtor = HasConstexprCtor::new({ 5 }); + let k: HasConstexprCtor = { + let mut __v: HasConstexprCtor = ::zero_init(); + HasConstexprCtor::construct(&mut __v, { 5 }); + __v + }; let mut f: FnPtr i32> = lambda_unsafe!( { - let k: HasConstexprCtor = HasConstexprCtor::copy_from({ &k }); + let k: HasConstexprCtor = { + let mut __v: HasConstexprCtor = ::zero_init(); + HasConstexprCtor::copy_from(&mut __v, { &k }); + __v + }; }, || -> i32 { - let mut c: HasConstexprCtor = HasConstexprCtor::copy_from({ &k }); + let mut c: HasConstexprCtor = { + let mut __v: HasConstexprCtor = ::zero_init(); + HasConstexprCtor::copy_from(&mut __v, { &k }); + __v + }; return c.m; } ); @@ -258,27 +348,24 @@ pub unsafe fn test_17() { } pub static mut copies_18: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0_i32 }); #[repr(C)] -#[derive(VaArg, FnPtrArg, CppFromUnsafe, CopyAssignUnsafe, Default)] -#[cpp_from(i32, new, value)] +#[derive(VaArg, FnPtrArg, ZeroInitializationUnsafe, CppFromUnsafe, CopyAssignUnsafe, Default)] +#[cpp_from(i32, construct, value)] pub struct p5_2_mutable_subobject_is_odr_used_HasMutableMember { pub value: i32, pub scratch: i32, } impl p5_2_mutable_subobject_is_odr_used_HasMutableMember { - pub unsafe fn new(mut v: i32) -> Self { - let mut this = Self { - value: v, - scratch: 0, - }; - this + pub unsafe fn construct(&mut self, mut v: i32) { + (&raw mut self.value).write(v); + (&raw mut self.scratch).write(0); } - pub unsafe fn copy_from(o: *const p5_2_mutable_subobject_is_odr_used_HasMutableMember) -> Self { - let mut this = Self { - value: (*o).value, - scratch: (*o).scratch, - }; + pub unsafe fn copy_from( + &mut self, + o: *const p5_2_mutable_subobject_is_odr_used_HasMutableMember, + ) { + (&raw mut self.value).write((*o).value); + (&raw mut self.scratch).write((*o).scratch); (*std::cell::LazyCell::force_mut(&mut *&raw mut copies_18)).prefix_inc(); - this } pub unsafe fn copy_assign( &mut self, @@ -292,19 +379,31 @@ impl p5_2_mutable_subobject_is_odr_used_HasMutableMember { impl Clone for p5_2_mutable_subobject_is_odr_used_HasMutableMember { fn clone(&self) -> Self { unsafe { + let mut __v: p5_2_mutable_subobject_is_odr_used_HasMutableMember = + ::zero_init(); p5_2_mutable_subobject_is_odr_used_HasMutableMember::copy_from( + &mut __v, self as *const p5_2_mutable_subobject_is_odr_used_HasMutableMember, - ) + ); + __v } } } pub unsafe fn test_19() { - let k: p5_2_mutable_subobject_is_odr_used_HasMutableMember = - p5_2_mutable_subobject_is_odr_used_HasMutableMember::new({ 5 }); + let k: p5_2_mutable_subobject_is_odr_used_HasMutableMember = { + let mut __v: p5_2_mutable_subobject_is_odr_used_HasMutableMember = + ::zero_init(); + p5_2_mutable_subobject_is_odr_used_HasMutableMember::construct(&mut __v, { 5 }); + __v + }; let mut f: FnPtr i32> = lambda_unsafe!( { - let k: p5_2_mutable_subobject_is_odr_used_HasMutableMember = - p5_2_mutable_subobject_is_odr_used_HasMutableMember::copy_from({ &k }); + let k: p5_2_mutable_subobject_is_odr_used_HasMutableMember = { + let mut __v: p5_2_mutable_subobject_is_odr_used_HasMutableMember = + ::zero_init(); + p5_2_mutable_subobject_is_odr_used_HasMutableMember::copy_from(&mut __v, { &k }); + __v + }; }, || -> i32 { return k.value; @@ -315,7 +414,9 @@ pub unsafe fn test_19() { assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut copies_18)) == (1))); } #[repr(C)] -#[derive(Clone, VaArg, FnPtrArg, CopyAssignUnsafe, DestructibleUnsafe, Default)] +#[derive( + Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, CopyAssignUnsafe, DestructibleUnsafe, Default, +)] pub struct p6_unevaluated_binding_needs_no_capture_HasTwoMembers { pub a: LifetimeTracked, pub b: LifetimeTracked, @@ -346,8 +447,16 @@ 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: LifetimeTracked::new(), - b: LifetimeTracked::new(), + a: { + let mut __v: LifetimeTracked = ::zero_init(); + LifetimeTracked::construct(&mut __v); + __v + }, + b: { + let mut __v: LifetimeTracked = ::zero_init(); + LifetimeTracked::construct(&mut __v); + __v + }, }; let _dtor_p = std::mem::DropGuard::new(&raw mut p, |__p| unsafe { DestructibleUnsafe::destroy(__p) @@ -364,7 +473,9 @@ pub unsafe fn test_20() { assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut useCount_4)) == (0))); } #[repr(C)] -#[derive(Clone, VaArg, FnPtrArg, CopyAssignUnsafe, DestructibleUnsafe)] +#[derive( + Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, CopyAssignUnsafe, DestructibleUnsafe, +)] pub struct p7_member_access_captures_this_HasMemberUsedInLambda { pub x: LifetimeTracked, pub n: i32, @@ -427,7 +538,9 @@ pub unsafe fn test_22() { assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_2)) == (0))); } #[repr(C)] -#[derive(Clone, VaArg, FnPtrArg, CopyAssignUnsafe, DestructibleUnsafe, Default)] +#[derive( + Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, CopyAssignUnsafe, DestructibleUnsafe, Default, +)] pub struct p7_unevaluated_this_needs_no_capture_HasMemberUsedInLambda { pub x: LifetimeTracked, } @@ -491,74 +604,97 @@ pub unsafe fn test_26() { } pub static mut t_ctors_27: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0_i32 }); #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe)] pub struct p9_destructor_not_potentially_invoked_HasUndefinedDestructor {} impl p9_destructor_not_potentially_invoked_HasUndefinedDestructor { - pub unsafe fn new() -> Self { - let mut this = Self {}; + pub unsafe fn construct(&mut self) { (*std::cell::LazyCell::force_mut(&mut *&raw mut t_ctors_27)).prefix_inc(); - this } } impl Default for p9_destructor_not_potentially_invoked_HasUndefinedDestructor { fn default() -> Self { - unsafe { p9_destructor_not_potentially_invoked_HasUndefinedDestructor::new() } + unsafe { + { + let mut __v: p9_destructor_not_potentially_invoked_HasUndefinedDestructor = + ::zero_init(); + p9_destructor_not_potentially_invoked_HasUndefinedDestructor::construct(&mut __v); + __v + } + } } } pub unsafe fn test_28() { - let mut p: *mut p9_destructor_not_potentially_invoked_HasUndefinedDestructor = (Box::leak( - Box::new(p9_destructor_not_potentially_invoked_HasUndefinedDestructor::new()), - ) - as *mut p9_destructor_not_potentially_invoked_HasUndefinedDestructor); + let mut p: *mut p9_destructor_not_potentially_invoked_HasUndefinedDestructor = + (Box::leak(Box::new({ + let mut __v: p9_destructor_not_potentially_invoked_HasUndefinedDestructor = + ::zero_init(); + p9_destructor_not_potentially_invoked_HasUndefinedDestructor::construct(&mut __v); + __v + })) as *mut p9_destructor_not_potentially_invoked_HasUndefinedDestructor); assert!(!((p).is_null())); assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut t_ctors_27)) == (1))); } pub static mut ctor_calls_29: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0_i32 }); #[repr(C)] -#[derive(FnPtrArg)] +#[derive(FnPtrArg, ZeroInitializationUnsafe)] pub struct p9_unused_copy_constructor_HasUndefinedCopyCtor { pub value: i32, } impl p9_unused_copy_constructor_HasUndefinedCopyCtor { - pub unsafe fn new() -> Self { - let mut this = Self { value: 3 }; + pub unsafe fn construct(&mut self) { + (&raw mut self.value).write(3); (*std::cell::LazyCell::force_mut(&mut *&raw mut ctor_calls_29)).prefix_inc(); - this } } impl Default for p9_unused_copy_constructor_HasUndefinedCopyCtor { fn default() -> Self { - unsafe { p9_unused_copy_constructor_HasUndefinedCopyCtor::new() } + unsafe { + { + let mut __v: p9_unused_copy_constructor_HasUndefinedCopyCtor = + ::zero_init(); + p9_unused_copy_constructor_HasUndefinedCopyCtor::construct(&mut __v); + __v + } + } } } pub unsafe fn read_30(object: *const p9_unused_copy_constructor_HasUndefinedCopyCtor) -> i32 { return (*object).value; } pub unsafe fn test_31() { - let mut a: p9_unused_copy_constructor_HasUndefinedCopyCtor = - p9_unused_copy_constructor_HasUndefinedCopyCtor::new(); + let mut a: p9_unused_copy_constructor_HasUndefinedCopyCtor = { + let mut __v: p9_unused_copy_constructor_HasUndefinedCopyCtor = + ::zero_init(); + p9_unused_copy_constructor_HasUndefinedCopyCtor::construct(&mut __v); + __v + }; assert!(((unsafe { read_30(&a,) }) == (3))); assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut ctor_calls_29)) == (1))); } pub static mut m_ctors_32: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0_i32 }); #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe)] pub struct p9_unused_implicit_assignment_HasUndefinedCopyAssignment {} impl p9_unused_implicit_assignment_HasUndefinedCopyAssignment { - pub unsafe fn new() -> Self { - let mut this = Self {}; + pub unsafe fn construct(&mut self) { (*std::cell::LazyCell::force_mut(&mut *&raw mut m_ctors_32)).prefix_inc(); - this } } impl Default for p9_unused_implicit_assignment_HasUndefinedCopyAssignment { fn default() -> Self { - unsafe { p9_unused_implicit_assignment_HasUndefinedCopyAssignment::new() } + unsafe { + { + let mut __v: p9_unused_implicit_assignment_HasUndefinedCopyAssignment = + ::zero_init(); + p9_unused_implicit_assignment_HasUndefinedCopyAssignment::construct(&mut __v); + __v + } + } } } #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] pub struct p9_unused_implicit_assignment_HasMemberWithUndefinedCopyAssignment { pub m: p9_unused_implicit_assignment_HasUndefinedCopyAssignment, } diff --git a/tests/std/class/class.init/class.base.init.cpp b/tests/std/class/class.init/class.base.init.cpp new file mode 100644 index 0000000000..f964ac93cc --- /dev/null +++ b/tests/std/class/class.init/class.base.init.cpp @@ -0,0 +1,23 @@ +// ADDITIONAL_COMPILE_FLAGS: -std=c++23 +#include + +// N4950 [class.base.init]/13.3: non-static data members are initialized in +// declaration order, so an initializer reads the members declared before it. +namespace p13_3_member_reads_earlier_member { +struct Pair { + Pair(int first) : first(first), second(this->first + 1) {} + int first; + int second; +}; + +void test() { + Pair pair(5); + assert(pair.first == 5); + assert(pair.second == 6); +} +} // namespace p13_3_member_reads_earlier_member + +int main() { + p13_3_member_reads_earlier_member::test(); + return 0; +} diff --git a/tests/std/class/class.init/out/refcount/class.base.init.rs b/tests/std/class/class.init/out/refcount/class.base.init.rs new file mode 100644 index 0000000000..6eac6ca310 --- /dev/null +++ b/tests/std/class/class.init/out/refcount/class.base.init.rs @@ -0,0 +1,46 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, CppFrom, Default)] +#[cpp_from(i32, construct, value)] +#[byte_size(8)] +pub struct p13_3_member_reads_earlier_member_Pair { + #[offset(0)] + pub first: i32, + #[offset(4)] + pub second: i32, +} +pub fn test_0() { + let mut pair: p13_3_member_reads_earlier_member_Pair = { + let __v: Value = Rc::new(RefCell::new( + ::zero_init(), + )); + p13_3_member_reads_earlier_member_PairImpl::construct(&__v.as_pointer(), { 5 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }; + assert!((pair.first == 5)); + assert!((pair.second == 6)); +} +pub fn main() { + __cpp2rust_init_globals(); + libcc2rs::cpp2rust_exit(main_0()); +} +fn main_0() -> i32 { + ({ test_0() }); + return 0; +} +pub trait p13_3_member_reads_earlier_member_PairImpl { + fn construct(&self, first: i32); +} +impl p13_3_member_reads_earlier_member_PairImpl for Ptr { + fn construct(&self, mut first: i32) { + field!((*self), first).write(first); + field!((*self), second).write(((*self).with(|__s| __s.first) + 1)); + } +} +pub fn __cpp2rust_init_globals() {} diff --git a/tests/std/class/class.init/out/unsafe/class.base.init.rs b/tests/std/class/class.init/out/unsafe/class.base.init.rs new file mode 100644 index 0000000000..dc48eed6d5 --- /dev/null +++ b/tests/std/class/class.init/out/unsafe/class.base.init.rs @@ -0,0 +1,42 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +#[repr(C)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, CppFromUnsafe, Default)] +#[cpp_from(i32, construct, value)] +pub struct p13_3_member_reads_earlier_member_Pair { + pub first: i32, + pub second: i32, +} +impl p13_3_member_reads_earlier_member_Pair { + pub unsafe fn construct(&mut self, mut first: i32) { + (&raw mut self.first).write(first); + (&raw mut self.second).write(((self.first) + (1))); + } +} +pub unsafe fn test_0() { + let mut pair: p13_3_member_reads_earlier_member_Pair = { + let mut __v: p13_3_member_reads_earlier_member_Pair = + ::zero_init(); + p13_3_member_reads_earlier_member_Pair::construct(&mut __v, { 5 }); + __v + }; + assert!(((pair.first) == (5))); + assert!(((pair.second) == (6))); +} +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + libcc2rs::cpp2rust_exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + (unsafe { test_0() }); + return 0; +} +pub unsafe fn __cpp2rust_init_globals() {} 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 31f7517e61..a5e5f206a7 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 @@ -6,31 +6,30 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -impl p1_template_constructor_from_own_type_Probe { - pub fn new_1(u: Ptr) -> Self { - Self { - value: (u.with(|__s| __s.value) + 1), - } - } -} -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, CppFrom, Default)] -#[cpp_from(i32, new_2, value)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, CppFrom, Default)] +#[cpp_from(i32, construct_2, value)] #[byte_size(4)] pub struct p1_template_constructor_from_own_type_Probe { #[offset(0)] pub value: i32, } -impl p1_template_constructor_from_own_type_Probe { - pub fn new_2(mut v: i32) -> Self { - Self { value: v } - } -} pub fn test_0() { - let probe: Value = Rc::new(RefCell::new( - p1_template_constructor_from_own_type_Probe::new_2({ 22 }), - )); - let mut converted: p1_template_constructor_from_own_type_Probe = - p1_template_constructor_from_own_type_Probe::new_1({ probe.as_pointer() }); + let probe: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new( + ::zero_init(), + )); + p1_template_constructor_from_own_type_ProbeImpl::construct_2(&__v.as_pointer(), { 22 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); + let mut converted: p1_template_constructor_from_own_type_Probe = { + let __v: Value = Rc::new(RefCell::new( + ::zero_init(), + )); + p1_template_constructor_from_own_type_ProbeImpl::construct_1(&__v.as_pointer(), { + probe.as_pointer() + }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }; assert!((converted.value == 23)); } pub fn main() { @@ -41,4 +40,18 @@ fn main_0() -> i32 { ({ test_0() }); return 0; } +pub trait p1_template_constructor_from_own_type_ProbeImpl { + fn construct_1(&self, u: Ptr); + fn construct_2(&self, v: i32); +} +impl p1_template_constructor_from_own_type_ProbeImpl + for Ptr +{ + fn construct_1(&self, u: Ptr) { + field!((*self), value).write((u.with(|__s| __s.value) + 1)); + } + fn construct_2(&self, mut v: i32) { + field!((*self), value).write(v); + } +} pub fn __cpp2rust_init_globals() {} 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 903126b61d..5cac9be66d 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 @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, CppFrom, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, CppFrom, Default)] #[cpp_into(i64, to_i64, mut_ptr)] #[byte_size(8)] pub struct p2_non_const_conversion_function_Gauge { 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 e31347631e..7ae7d0674b 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 @@ -7,30 +7,34 @@ use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; impl p1_template_constructor_from_own_type_Probe { - pub unsafe fn new_1(u: *mut p1_template_constructor_from_own_type_Probe) -> Self { - let mut this = Self { - value: (((*u).value) + (1)), - }; - this + pub unsafe fn construct_1(&mut self, u: *mut p1_template_constructor_from_own_type_Probe) { + (&raw mut self.value).write((((*u).value) + (1))); } } #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg, CppFromUnsafe, Default)] -#[cpp_from(i32, new_2, value)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, CppFromUnsafe, Default)] +#[cpp_from(i32, construct_2, value)] pub struct p1_template_constructor_from_own_type_Probe { pub value: i32, } impl p1_template_constructor_from_own_type_Probe { - pub unsafe fn new_2(mut v: i32) -> Self { - let mut this = Self { value: v }; - this + pub unsafe fn construct_2(&mut self, mut v: i32) { + (&raw mut self.value).write(v); } } pub unsafe fn test_0() { - let mut probe: p1_template_constructor_from_own_type_Probe = - p1_template_constructor_from_own_type_Probe::new_2({ 22 }); - let mut converted: p1_template_constructor_from_own_type_Probe = - p1_template_constructor_from_own_type_Probe::new_1({ &mut probe }); + let mut probe: p1_template_constructor_from_own_type_Probe = { + let mut __v: p1_template_constructor_from_own_type_Probe = + ::zero_init(); + p1_template_constructor_from_own_type_Probe::construct_2(&mut __v, { 22 }); + __v + }; + let mut converted: p1_template_constructor_from_own_type_Probe = { + let mut __v: p1_template_constructor_from_own_type_Probe = + ::zero_init(); + p1_template_constructor_from_own_type_Probe::construct_1(&mut __v, { &mut probe }); + __v + }; assert!(((converted.value) == (23))); } pub fn main() { 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 dc189767a5..038852dc89 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 @@ -7,7 +7,7 @@ use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg, CppFromUnsafe, Default)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, CppFromUnsafe, Default)] #[cpp_into(i64, to_i64, mut_ptr)] pub struct p2_non_const_conversion_function_Gauge { pub value: i64, 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 69a0989a28..3915204d7e 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 @@ -6,35 +6,39 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Record, ByteRepr, FnPtrArg, MoveCtor, CppFrom, Default)] -#[cpp_from(i32, new, value)] +#[derive(Record, ByteRepr, ZeroInitialization, FnPtrArg, MoveCtor, CppFrom, Default)] +#[cpp_from(i32, construct, value)] #[byte_size(4)] pub struct p11_defaulted_move_constructor_is_trivial_MoveOnly { #[offset(0)] pub value: i32, } -impl p11_defaulted_move_constructor_is_trivial_MoveOnly { - pub fn new(mut value: i32) -> Self { - Self { value: value } - } - pub fn move_from(_a0: Ptr) -> Self { - Self { - value: { (*_a0.upgrade().deref()).value }, - } - } -} pub fn pass_0( object: p11_defaulted_move_constructor_is_trivial_MoveOnly, ) -> p11_defaulted_move_constructor_is_trivial_MoveOnly { let object: Value = Rc::new(RefCell::new(object)); - return p11_defaulted_move_constructor_is_trivial_MoveOnly::move_from({ object.as_pointer() }); + return { + let __v: Value = Rc::new(RefCell::new( + ::zero_init(), + )); + p11_defaulted_move_constructor_is_trivial_MoveOnlyImpl::move_from(&__v.as_pointer(), { + object.as_pointer() + }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }; } pub fn test_1() { let mut result: p11_defaulted_move_constructor_is_trivial_MoveOnly = ({ - pass_0(p11_defaulted_move_constructor_is_trivial_MoveOnly::new({ - 7 - })) + pass_0({ + let __v: Value = Rc::new( + RefCell::new(::zero_init()), + ); + p11_defaulted_move_constructor_is_trivial_MoveOnlyImpl::construct(&__v.as_pointer(), { + 7 + }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }) }); assert!((result.value == 7)); } @@ -64,4 +68,18 @@ fn main_0() -> i32 { ({ test_3() }); return 0; } +pub trait p11_defaulted_move_constructor_is_trivial_MoveOnlyImpl { + fn construct(&self, value: i32); + fn move_from(&self, _a0: Ptr); +} +impl p11_defaulted_move_constructor_is_trivial_MoveOnlyImpl + for Ptr +{ + fn construct(&self, mut value: i32) { + field!((*self), value).write(value); + } + fn move_from(&self, _a0: Ptr) { + field!((*self), value).write({ (*_a0.upgrade().deref()).value }); + } +} pub fn __cpp2rust_init_globals() {} 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 1ec416a739..7b9a26b4bb 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 @@ -7,33 +7,40 @@ use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; #[repr(C)] -#[derive(FnPtrArg, MoveCtorUnsafe, CppFromUnsafe, Default)] -#[cpp_from(i32, new, value)] +#[derive(FnPtrArg, ZeroInitializationUnsafe, MoveCtorUnsafe, CppFromUnsafe, Default)] +#[cpp_from(i32, construct, value)] pub struct p11_defaulted_move_constructor_is_trivial_MoveOnly { pub value: i32, } impl p11_defaulted_move_constructor_is_trivial_MoveOnly { - pub unsafe fn new(mut value: i32) -> Self { - let mut this = Self { value: value }; - this + pub unsafe fn construct(&mut self, mut value: i32) { + (&raw mut self.value).write(value); } - pub unsafe fn move_from(_a0: *mut p11_defaulted_move_constructor_is_trivial_MoveOnly) -> Self { - let mut this = Self { - value: (*_a0).value, - }; - this + pub unsafe fn move_from( + &mut self, + _a0: *mut p11_defaulted_move_constructor_is_trivial_MoveOnly, + ) { + (&raw mut self.value).write((*_a0).value); } } pub unsafe fn pass_0( mut object: p11_defaulted_move_constructor_is_trivial_MoveOnly, ) -> p11_defaulted_move_constructor_is_trivial_MoveOnly { - return p11_defaulted_move_constructor_is_trivial_MoveOnly::move_from({ &mut object }); + return { + let mut __v: p11_defaulted_move_constructor_is_trivial_MoveOnly = + ::zero_init(); + p11_defaulted_move_constructor_is_trivial_MoveOnly::move_from(&mut __v, { &mut object }); + __v + }; } pub unsafe fn test_1() { let mut result: p11_defaulted_move_constructor_is_trivial_MoveOnly = (unsafe { - pass_0(p11_defaulted_move_constructor_is_trivial_MoveOnly::new({ - 7 - })) + pass_0({ + let mut __v: p11_defaulted_move_constructor_is_trivial_MoveOnly = + ::zero_init(); + p11_defaulted_move_constructor_is_trivial_MoveOnly::construct(&mut __v, { 7 }); + __v + }) }); assert!(((result.value) == (7))); } diff --git a/tests/std/class/class.mem/out/refcount/class.free.rs b/tests/std/class/class.mem/out/refcount/class.free.rs index 051e0ab616..f79f771bc7 100644 --- a/tests/std/class/class.mem/out/refcount/class.free.rs +++ b/tests/std/class/class.mem/out/refcount/class.free.rs @@ -9,7 +9,7 @@ use std::rc::{Rc, Weak}; thread_local!( pub static allocations_0: Value = Rc::new(RefCell::new(0)); ); -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(1)] pub struct p1_allocation_function_is_static_Allocates {} impl p1_allocation_function_is_static_Allocates { @@ -46,7 +46,7 @@ pub fn test_1() { thread_local!( pub static deallocations_2: Value = Rc::new(RefCell::new(0)); ); -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(1)] pub struct p3_deallocation_function_is_static_Deallocates {} impl p3_deallocation_function_is_static_Deallocates { diff --git a/tests/std/class/class.mem/out/refcount/class.mem.general.rs b/tests/std/class/class.mem/out/refcount/class.mem.general.rs index 03108709ea..5efab48289 100644 --- a/tests/std/class/class.mem/out/refcount/class.mem.general.rs +++ b/tests/std/class/class.mem/out/refcount/class.mem.general.rs @@ -14,7 +14,7 @@ pub fn minimum_1() -> i32 { } pub type anon_2 = u32; pub const anon_2_reserve: anon_2 = 30; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg)] #[byte_size(4)] pub struct p7_1_function_body_names_later_member_HasLaterMembers { #[offset(0)] @@ -43,7 +43,7 @@ thread_local!( thread_local!( pub static step_5: Value = Rc::new(RefCell::new(4)); ); -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg)] #[byte_size(4)] pub struct p7_2_default_argument_names_later_member_HasLaterStaticStep { #[offset(0)] diff --git a/tests/std/class/class.mem/out/unsafe/class.free.rs b/tests/std/class/class.mem/out/unsafe/class.free.rs index 5e3147c1c4..c054dea9ca 100644 --- a/tests/std/class/class.mem/out/unsafe/class.free.rs +++ b/tests/std/class/class.mem/out/unsafe/class.free.rs @@ -8,7 +8,7 @@ use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; pub static mut allocations_0: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] pub struct p1_allocation_function_is_static_Allocates {} impl p1_allocation_function_is_static_Allocates { pub unsafe fn operator_new(mut size: usize) -> *mut ::libc::c_void { @@ -42,7 +42,7 @@ pub unsafe fn test_1() { pub static mut deallocations_2: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] pub struct p3_deallocation_function_is_static_Deallocates {} impl p3_deallocation_function_is_static_Deallocates { pub unsafe fn operator_delete(mut ptr: *mut ::libc::c_void) { diff --git a/tests/std/class/class.mem/out/unsafe/class.mem.general.rs b/tests/std/class/class.mem/out/unsafe/class.mem.general.rs index abbc7d11d1..6023773883 100644 --- a/tests/std/class/class.mem/out/unsafe/class.mem.general.rs +++ b/tests/std/class/class.mem/out/unsafe/class.mem.general.rs @@ -13,7 +13,7 @@ pub unsafe fn minimum_1() -> i32 { pub type anon_2 = u32; pub const anon_2_reserve: anon_2 = 30; #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe)] pub struct p7_1_function_body_names_later_member_HasLaterMembers { pub stock: i32, } @@ -46,7 +46,7 @@ pub unsafe fn test_3() { pub static mut step_4: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 100 }); pub static mut step_5: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 4 }); #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe)] pub struct p7_2_default_argument_names_later_member_HasLaterStaticStep { pub total: i32, } diff --git a/tests/std/dcl.dcl/dcl.decl/dcl.init/dcl.init.general-p6.2-padding-bits.cpp b/tests/std/dcl.dcl/dcl.decl/dcl.init/dcl.init.general-p6.2-padding-bits.cpp new file mode 100644 index 0000000000..9d618283d1 --- /dev/null +++ b/tests/std/dcl.dcl/dcl.decl/dcl.init/dcl.init.general-p6.2-padding-bits.cpp @@ -0,0 +1,68 @@ +// XFAIL: unsafe +// ADDITIONAL_COMPILE_FLAGS: -std=c++23 +#include + +// N4950 [dcl.init.general]/6.2: the padding bits of a zero-initialized class +// are zero bits. +namespace p6_2_zero_initialized_padding { +struct Padded { + char letter; + int number; +}; + +struct TailPadded { + int number; + char letter; +}; + +struct HoldsPadded { + char letter; + Padded inner; + short small; +}; + +static Padded static_padded; +static TailPadded static_tail_padded; +static HoldsPadded static_holds_padded; +static Padded static_padded_array[2]; + +void check_zero_bytes(const unsigned char *bytes, unsigned long size) { + for (unsigned long i = 0; i < size; ++i) { + assert(bytes[i] == 0); + } +} + +void test() { + check_zero_bytes(reinterpret_cast(&static_padded), + sizeof(Padded)); + check_zero_bytes(reinterpret_cast(&static_tail_padded), + sizeof(TailPadded)); + check_zero_bytes( + reinterpret_cast(&static_holds_padded), + sizeof(HoldsPadded)); + check_zero_bytes( + reinterpret_cast(&static_padded_array), + sizeof(static_padded_array)); + + Padded value_padded = Padded(); + check_zero_bytes(reinterpret_cast(&value_padded), + sizeof(Padded)); + HoldsPadded value_holds_padded = HoldsPadded(); + check_zero_bytes(reinterpret_cast(&value_holds_padded), + sizeof(HoldsPadded)); + + Padded *heap_padded = new Padded(); + check_zero_bytes(reinterpret_cast(heap_padded), + sizeof(Padded)); + delete heap_padded; + TailPadded *heap_tail_padded = new TailPadded(); + check_zero_bytes(reinterpret_cast(heap_tail_padded), + sizeof(TailPadded)); + delete heap_tail_padded; +} +} // namespace p6_2_zero_initialized_padding + +int main() { + p6_2_zero_initialized_padding::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 ff328142c6..dfeb7987cc 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 @@ -28,7 +28,188 @@ void test() { } } // namespace p6_1_zero_initialized_pointer_to_member +// N4950 [dcl.init.general]/6.1: a zero-initialized scalar is the integer +// literal 0 converted to its type. +namespace p6_1_zero_initialized_scalars { +enum class Color { red = 1, green }; + +static int static_int; +static double static_double; +static bool static_bool; +static char static_char; +static int *static_pointer; +static Color static_enum; +static void (*static_function_pointer)(); +thread_local long thread_local_long; + +int next_count() { + static int counter; + return ++counter; +} + +void test() { + assert(static_int == 0); + assert(static_double == 0.0); + assert(!static_bool); + assert(static_char == '\0'); + assert(static_pointer == nullptr); + assert(static_enum == Color(0)); + assert(static_enum != Color::red); + assert(static_function_pointer == nullptr); + assert(thread_local_long == 0); + assert(next_count() == 1); + assert(next_count() == 2); + + int value_int{}; + double value_double = double(); + int *value_pointer{}; + assert(value_int == 0); + assert(value_double == 0.0); + assert(value_pointer == nullptr); + + int *heap_int = new int(); + assert(*heap_int == 0); + delete heap_int; +} +} // namespace p6_1_zero_initialized_scalars + +// N4950 [dcl.init.general]/6.2: zero-initializing a class zero-initializes +// each non-static data member. +namespace p6_2_zero_initialized_class { +struct Inner { + int number; + double real; + int *pointer; +}; + +struct Outer { + char letter; + Inner inner; + int numbers[3]; + Inner inners[2]; +}; + +static Outer static_outer; + +void check(const Outer &outer) { + assert(outer.letter == '\0'); + assert(outer.inner.number == 0); + assert(outer.inner.real == 0.0); + assert(outer.inner.pointer == nullptr); + for (int i = 0; i < 3; ++i) { + assert(outer.numbers[i] == 0); + } + for (int i = 0; i < 2; ++i) { + assert(outer.inners[i].number == 0); + assert(outer.inners[i].real == 0.0); + assert(outer.inners[i].pointer == nullptr); + } +} + +void test() { + check(static_outer); + Outer value_outer = Outer(); + check(value_outer); + Outer *heap_outer = new Outer(); + check(*heap_outer); + delete heap_outer; +} +} // namespace p6_2_zero_initialized_class + +// N4950 [dcl.init.general]/6.3: zero-initializing a union zero-initializes its +// first non-static named data member. +namespace p6_3_zero_initialized_union { +union Number { + int integer; + char bytes[8]; +}; + +static Number static_number; + +void test() { + assert(static_number.integer == 0); + Number value_number = Number(); + assert(value_number.integer == 0); +} +} // namespace p6_3_zero_initialized_union + +// N4950 [dcl.init.general]/6.4: zero-initializing an array zero-initializes +// each element. +namespace p6_4_zero_initialized_array { +struct Pair { + int first; + int second; +}; + +static int static_numbers[4]; +static int static_matrix[2][3]; +static Pair static_pairs[2]; + +void test() { + for (int i = 0; i < 4; ++i) { + assert(static_numbers[i] == 0); + } + for (int i = 0; i < 2; ++i) { + for (int j = 0; j < 3; ++j) { + assert(static_matrix[i][j] == 0); + } + } + for (int i = 0; i < 2; ++i) { + assert(static_pairs[i].first == 0); + assert(static_pairs[i].second == 0); + } + int *heap_numbers = new int[3](); + for (int i = 0; i < 3; ++i) { + assert(heap_numbers[i] == 0); + } + delete[] heap_numbers; +} +} // namespace p6_4_zero_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. +namespace p9_1_2_zero_then_default_initialized { +struct WithDefaultMember { + int zeroed; + int five = 5; +}; + +struct LeavesMemberAlone { + LeavesMemberAlone() {} + int untouched; +}; + +struct HoldsLeavesMemberAlone { + int number; + LeavesMemberAlone member; +}; + +void test() { + WithDefaultMember value = WithDefaultMember(); + assert(value.zeroed == 0); + assert(value.five == 5); + WithDefaultMember *heap = new WithDefaultMember(); + assert(heap->zeroed == 0); + assert(heap->five == 5); + delete heap; + + HoldsLeavesMemberAlone holder = HoldsLeavesMemberAlone(); + assert(holder.number == 0); + assert(holder.member.untouched == 0); + HoldsLeavesMemberAlone *heap_holder = new HoldsLeavesMemberAlone(); + assert(heap_holder->number == 0); + assert(heap_holder->member.untouched == 0); + delete heap_holder; +} +} // namespace p9_1_2_zero_then_default_initialized + int main() { p6_1_zero_initialized_pointer_to_member::test(); + p6_1_zero_initialized_scalars::test(); + p6_2_zero_initialized_class::test(); + p6_3_zero_initialized_union::test(); + p6_4_zero_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.general-p6.2-padding-bits.rs b/tests/std/dcl.dcl/dcl.decl/dcl.init/out/refcount/dcl.init.general-p6.2-padding-bits.rs new file mode 100644 index 0000000000..7291632867 --- /dev/null +++ b/tests/std/dcl.dcl/dcl.decl/dcl.init/out/refcount/dcl.init.general-p6.2-padding-bits.rs @@ -0,0 +1,135 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] +#[byte_size(8)] +pub struct p6_2_zero_initialized_padding_Padded { + #[offset(0)] + pub letter: i8, + #[offset(4)] + pub number: i32, +} +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] +#[byte_size(8)] +pub struct p6_2_zero_initialized_padding_TailPadded { + #[offset(0)] + pub number: i32, + #[offset(4)] + pub letter: i8, +} +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] +#[byte_size(16)] +pub struct p6_2_zero_initialized_padding_HoldsPadded { + #[offset(0)] + pub letter: i8, + #[offset(4)] + #[byte_size(8)] + pub inner: p6_2_zero_initialized_padding_Padded, + #[offset(12)] + pub small: i16, +} +thread_local!( + pub static static_padded_0: Value = Rc::new( + RefCell::new(::default()), + ); +); +thread_local!( + pub static static_tail_padded_1: Value = Rc::new( + RefCell::new(::default()), + ); +); +thread_local!( + pub static static_holds_padded_2: Value = Rc::new( + RefCell::new(::default()), + ); +); +thread_local!( + pub static static_padded_array_3: Value> = + Rc::new(RefCell::new( + (0..2) + .map(|_| ::default()) + .collect::>(), + )); +); +pub fn check_zero_bytes_4(mut bytes: Ptr, mut size: u64) { + { + let mut i: u64 = 0_u64; + 'loop_: while (i < size) { + 'for_body: { + assert!((((elem!(bytes, i).read()) as i32) == 0)); + } + i.prefix_inc(); + } + } +} +pub fn test_5() { + ({ + check_zero_bytes_4( + (static_padded_0.with(|v| v.as_pointer())).reinterpret_cast::(), + (8usize as u64), + ) + }); + ({ + check_zero_bytes_4( + (static_tail_padded_1.with(|v| v.as_pointer())).reinterpret_cast::(), + (8usize as u64), + ) + }); + ({ + check_zero_bytes_4( + (static_holds_padded_2.with(|v| v.as_pointer())).reinterpret_cast::(), + (16usize as u64), + ) + }); + ({ + check_zero_bytes_4( + (static_padded_array_3.with(|v| v.as_pointer())).reinterpret_cast::(), + (16usize as u64), + ) + }); + let value_padded: Value = Rc::new(RefCell::new( + ::default(), + )); + ({ + check_zero_bytes_4( + (value_padded.as_pointer()).reinterpret_cast::(), + (8usize as u64), + ) + }); + let value_holds_padded: Value = Rc::new( + RefCell::new(::default()), + ); + ({ + check_zero_bytes_4( + (value_holds_padded.as_pointer()).reinterpret_cast::(), + (16usize as u64), + ) + }); + let mut heap_padded: Ptr = + Ptr::alloc(::default()); + ({ check_zero_bytes_4(heap_padded.reinterpret_cast::(), (8usize as u64)) }); + heap_padded.delete(); + let mut heap_tail_padded: Ptr = + Ptr::alloc(::default()); + ({ check_zero_bytes_4(heap_tail_padded.reinterpret_cast::(), (8usize as u64)) }); + heap_tail_padded.delete(); +} +pub fn main() { + __cpp2rust_init_globals(); + libcc2rs::cpp2rust_exit(main_0()); +} +fn main_0() -> i32 { + ({ test_5() }); + return 0; +} +pub fn __cpp2rust_init_globals() { + let _ = static_padded_0.with(|_| ()); + let _ = static_tail_padded_1.with(|_| ()); + let _ = static_holds_padded_2.with(|_| ()); + let _ = static_padded_array_3.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 822d9f3f1f..2255e0a05e 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 @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct p6_1_zero_initialized_pointer_to_member_HasTwoInts { #[offset(0)] @@ -14,7 +14,7 @@ pub struct p6_1_zero_initialized_pointer_to_member_HasTwoInts { #[offset(4)] pub second: i32, } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg)] #[byte_size(8)] pub struct p6_1_zero_initialized_pointer_to_member_HoldsSelector { #[offset(0)] @@ -52,14 +52,393 @@ pub fn test_1() { as isize)) ); } +pub type p6_1_zero_initialized_scalars_Color = i32; +pub const p6_1_zero_initialized_scalars_Color_red: p6_1_zero_initialized_scalars_Color = 1; +pub const p6_1_zero_initialized_scalars_Color_green: p6_1_zero_initialized_scalars_Color = 2; +thread_local!( + pub static static_int_2: Value = Rc::new(RefCell::new(0_i32)); +); +thread_local!( + pub static static_double_3: Value = Rc::new(RefCell::new(0_f64)); +); +thread_local!( + pub static static_bool_4: Value = Rc::new(RefCell::new(false)); +); +thread_local!( + pub static static_char_5: Value = Rc::new(RefCell::new(0_i8)); +); +thread_local!( + pub static static_pointer_6: Value> = Rc::new(RefCell::new(Ptr::::null())); +); +thread_local!( + pub static static_enum_7: Value = + >::default(); +); +thread_local!( + pub static static_function_pointer_8: Value> = + Rc::new(RefCell::new(FnPtr::::null())); +); +thread_local!( + pub static thread_local_long_9: Value = Rc::new(RefCell::new(0_i64)); +); +pub fn next_count_10() -> i32 { + return (*(*counter_11.with(Value::clone).borrow_mut()).prefix_inc()); +} +pub fn test_12() { + assert!((static_int_2.with(|rc| *rc.borrow()) == 0)); + assert!((static_double_3.with(|rc| *rc.borrow()) == 0_f64)); + assert!(!(static_bool_4.with(|rc| *rc.borrow()))); + assert!(((static_char_5.with(|rc| *rc.borrow()) as i32) == (('\0' as i8) as i32))); + assert!(({ (*static_pointer_6.with(Value::clone).borrow()).is_null() })); + assert!( + (static_enum_7.with(|rc| *rc.borrow()) == ((0) as p6_1_zero_initialized_scalars_Color)) + ); + assert!((static_enum_7.with(|rc| *rc.borrow()) != p6_1_zero_initialized_scalars_Color_red)); + assert!(({ (*static_function_pointer_8.with(Value::clone).borrow()).is_null() })); + assert!((thread_local_long_9.with(|rc| *rc.borrow()) == 0_i64)); + assert!((({ next_count_10() }) == 1)); + assert!((({ next_count_10() }) == 2)); + let mut value_int: i32 = 0_i32; + let mut value_double: f64 = 0_f64; + let mut value_pointer: Ptr = Ptr::::null(); + assert!((value_int == 0)); + assert!((value_double == 0_f64)); + assert!((value_pointer).is_null()); + let mut heap_int: Ptr = Ptr::alloc(0_i32); + assert!(((heap_int.read()) == 0)); + heap_int.delete(); +} +thread_local!( + static counter_11: Value = Rc::new(RefCell::new(0_i32)); +); +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] +#[byte_size(24)] +pub struct p6_2_zero_initialized_class_Inner { + #[offset(0)] + pub number: i32, + #[offset(8)] + pub real: f64, + #[offset(16)] + #[byte_size(8)] + pub pointer: Ptr, +} +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg)] +#[byte_size(96)] +pub struct p6_2_zero_initialized_class_Outer { + #[offset(0)] + pub letter: i8, + #[offset(8)] + #[byte_size(24)] + pub inner: p6_2_zero_initialized_class_Inner, + #[offset(32)] + #[byte_size(12)] + #[array_len(3)] + pub numbers: Box<[i32]>, + #[offset(48)] + #[byte_size(48)] + #[array_len(2)] + pub inners: Box<[p6_2_zero_initialized_class_Inner]>, +} +impl Default for p6_2_zero_initialized_class_Outer { + fn default() -> Self { + { + p6_2_zero_initialized_class_Outer { + letter: 0_i8, + inner: ::default(), + numbers: (0..3).map(|_| 0_i32).collect::>(), + inners: (0..2) + .map(|_| ::default()) + .collect::>(), + } + } + } +} +thread_local!( + pub static static_outer_13: Value = + Rc::new(RefCell::new(::default())); +); +pub fn check_14(outer: Ptr) { + assert!(((outer.with(|__s| __s.letter) as i32) == (('\0' as i8) as i32))); + assert!((outer.with(|__s| __s.inner.number) == 0)); + assert!((outer.with(|__s| __s.inner.real) == 0_f64)); + assert!((outer.with(|__s| __s.inner.pointer.clone())).is_null()); + { + let mut i: i32 = 0; + 'loop_: while (i < 3) { + 'for_body: { + assert!(((array_elem!(outer, numbers, i).read()) == 0)); + } + i.prefix_inc(); + } + } + { + let mut i: i32 = 0; + 'loop_: while (i < 2) { + 'for_body: { + assert!((array_elem!(outer, inners, i).with(|__s| __s.number) == 0)); + assert!((array_elem!(outer, inners, i).with(|__s| __s.real) == 0_f64)); + assert!((array_elem!(outer, inners, i).with(|__s| __s.pointer.clone())).is_null()); + } + i.prefix_inc(); + } + } +} +pub fn test_15() { + ({ check_14(static_outer_13.with(|v| v.as_pointer())) }); + let value_outer: Value = + Rc::new(RefCell::new(::default())); + ({ check_14(value_outer.as_pointer()) }); + let mut heap_outer: Ptr = + Ptr::alloc(::default()); + ({ check_14((heap_outer).clone()) }); + heap_outer.delete(); +} +#[derive(VaArg, FnPtrArg, ByteRepr, Clone, ZeroInitialization)] +#[byte_size(8)] +pub struct p6_3_zero_initialized_union_Number { + #[offset(0)] + #[byte_size(8)] + #[array_len(8)] + __bytes: Box<[u8]>, +} +impl p6_3_zero_initialized_union_Number { + pub fn integer(this: Ptr) -> Ptr { + this.reinterpret_cast() + } + pub fn bytes(this: Ptr) -> Ptr { + this.reinterpret_cast() + } +} +impl Default for p6_3_zero_initialized_union_Number { + fn default() -> Self { + p6_3_zero_initialized_union_Number { + __bytes: Box::from([0u8; 8]), + } + } +} +thread_local!( + pub static static_number_16: Value = + Rc::new(RefCell::new(::default())); +); +pub fn test_17() { + assert!( + ((p6_3_zero_initialized_union_Number::integer(static_number_16.with(|v| v.as_pointer())) + .read()) + == 0) + ); + let value_number: Value = + Rc::new(RefCell::new(::default())); + assert!(((p6_3_zero_initialized_union_Number::integer(value_number.as_pointer()).read()) == 0)); +} +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] +#[byte_size(8)] +pub struct p6_4_zero_initialized_array_Pair { + #[offset(0)] + pub first: i32, + #[offset(4)] + pub second: i32, +} +thread_local!( + pub static static_numbers_18: Value> = + Rc::new(RefCell::new((0..4).map(|_| 0_i32).collect::>())); +); +thread_local!( + pub static static_matrix_19: Value]>> = Rc::new(RefCell::new( + (0..2) + .map(|_| (0..3).map(|_| 0_i32).collect::>()) + .collect::]>>(), + )); +); +thread_local!( + pub static static_pairs_20: Value> = + Rc::new(RefCell::new( + (0..2) + .map(|_| ::default()) + .collect::>(), + )); +); +pub fn test_21() { + { + let mut i: i32 = 0; + 'loop_: while (i < 4) { + 'for_body: { + assert!( + (({ + let __idx0 = (i) as usize; + static_numbers_18.with(|rc| rc.borrow()[__idx0]) + }) == 0) + ); + } + i.prefix_inc(); + } + } + { + let mut i: i32 = 0; + 'loop_: while (i < 2) { + 'for_body: { + { + let mut j: i32 = 0; + 'loop_: while (j < 3) { + 'for_body: { + assert!( + (({ + let __idx0 = (i) as usize; + let __idx1 = (j) as usize; + static_matrix_19.with(|rc| rc.borrow()[__idx0][__idx1]) + }) == 0) + ); + } + j.prefix_inc(); + } + } + } + i.prefix_inc(); + } + } + { + let mut i: i32 = 0; + 'loop_: while (i < 2) { + 'for_body: { + assert!( + ({ + ({ + let __idx0 = (i) as usize; + static_pairs_20.with(|rc| rc.borrow()[__idx0]) + }) + .first + } == 0) + ); + assert!( + ({ + ({ + let __idx0 = (i) as usize; + static_pairs_20.with(|rc| rc.borrow()[__idx0]) + }) + .second + } == 0) + ); + } + i.prefix_inc(); + } + } + let mut heap_numbers: Ptr = + Ptr::alloc_array((0..3_usize).map(|_| 0_i32).collect::>()); + { + let mut i: i32 = 0; + 'loop_: while (i < 3) { + 'for_body: { + assert!(((elem!(heap_numbers, i).read()) == 0)); + } + i.prefix_inc(); + } + } + heap_numbers.delete(); +} +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg)] +#[byte_size(8)] +pub struct p9_1_2_zero_then_default_initialized_WithDefaultMember { + #[offset(0)] + pub zeroed: i32, + #[offset(4)] + pub five: i32, +} +impl Default for p9_1_2_zero_then_default_initialized_WithDefaultMember { + fn default() -> Self { + { + p9_1_2_zero_then_default_initialized_WithDefaultMember { + zeroed: 0_i32, + five: 5, + } + } + } +} +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg)] +#[byte_size(4)] +pub struct p9_1_2_zero_then_default_initialized_LeavesMemberAlone { + #[offset(0)] + pub untouched: i32, +} +impl Default for p9_1_2_zero_then_default_initialized_LeavesMemberAlone { + fn default() -> Self { + { + { + let __v: Value = + Rc::new(RefCell::new( + ::zero_init(), + )); + p9_1_2_zero_then_default_initialized_LeavesMemberAloneImpl::construct( + &__v.as_pointer(), + ); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + } + } + } +} +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] +#[byte_size(8)] +pub struct p9_1_2_zero_then_default_initialized_HoldsLeavesMemberAlone { + #[offset(0)] + pub number: i32, + #[offset(4)] + #[byte_size(4)] + pub member: p9_1_2_zero_then_default_initialized_LeavesMemberAlone, +} +pub fn test_22() { + let mut value: p9_1_2_zero_then_default_initialized_WithDefaultMember = + ::default(); + assert!((value.zeroed == 0)); + assert!((value.five == 5)); + let mut heap: Ptr = + Ptr::alloc(::default()); + assert!((heap.with(|__s| __s.zeroed) == 0)); + assert!((heap.with(|__s| __s.five) == 5)); + heap.delete(); + let mut holder: p9_1_2_zero_then_default_initialized_HoldsLeavesMemberAlone = + ::default(); + assert!((holder.number == 0)); + assert!((holder.member.untouched == 0)); + let mut heap_holder: Ptr = + Ptr::alloc(::default()); + assert!((heap_holder.with(|__s| __s.number) == 0)); + assert!((heap_holder.with(|__s| __s.member.untouched) == 0)); + heap_holder.delete(); +} pub fn main() { __cpp2rust_init_globals(); libcc2rs::cpp2rust_exit(main_0()); } fn main_0() -> i32 { ({ test_1() }); + ({ test_12() }); + ({ test_15() }); + ({ test_17() }); + ({ test_21() }); + ({ test_22() }); return 0; } +pub trait p9_1_2_zero_then_default_initialized_LeavesMemberAloneImpl { + fn construct(&self); +} +impl p9_1_2_zero_then_default_initialized_LeavesMemberAloneImpl + for Ptr +{ + fn construct(&self) { + field!((*self), untouched).write(0_i32); + } +} pub fn __cpp2rust_init_globals() { let _ = static_pm_0.with(|_| ()); + let _ = static_int_2.with(|_| ()); + let _ = static_double_3.with(|_| ()); + let _ = static_bool_4.with(|_| ()); + let _ = static_char_5.with(|_| ()); + let _ = static_pointer_6.with(|_| ()); + let _ = static_enum_7.with(|_| ()); + let _ = static_function_pointer_8.with(|_| ()); + let _ = thread_local_long_9.with(|_| ()); + let _ = static_outer_13.with(|_| ()); + let _ = static_number_16.with(|_| ()); + let _ = static_numbers_18.with(|_| ()); + let _ = static_matrix_19.with(|_| ()); + let _ = static_pairs_20.with(|_| ()); } diff --git a/tests/std/dcl.dcl/dcl.decl/dcl.init/out/unsafe/dcl.init.general-p6.2-padding-bits.rs b/tests/std/dcl.dcl/dcl.decl/dcl.init/out/unsafe/dcl.init.general-p6.2-padding-bits.rs new file mode 100644 index 0000000000..cd2397ca8f --- /dev/null +++ b/tests/std/dcl.dcl/dcl.decl/dcl.init/out/unsafe/dcl.init.general-p6.2-padding-bits.rs @@ -0,0 +1,143 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +#[repr(C)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] +pub struct p6_2_zero_initialized_padding_Padded { + pub letter: libc::c_char, + pub number: i32, +} +#[repr(C)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] +pub struct p6_2_zero_initialized_padding_TailPadded { + pub number: i32, + pub letter: libc::c_char, +} +#[repr(C)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] +pub struct p6_2_zero_initialized_padding_HoldsPadded { + pub letter: libc::c_char, + pub inner: p6_2_zero_initialized_padding_Padded, + pub small: i16, +} +pub static mut static_padded_0: std::cell::LazyCell = + std::cell::LazyCell::new(|| unsafe { ::default() }); +pub static mut static_tail_padded_1: std::cell::LazyCell = + std::cell::LazyCell::new(|| unsafe { ::default() }); +pub static mut static_holds_padded_2: std::cell::LazyCell< + p6_2_zero_initialized_padding_HoldsPadded, +> = std::cell::LazyCell::new(|| unsafe { ::default() }); +pub static mut static_padded_array_3: std::cell::LazyCell< + [p6_2_zero_initialized_padding_Padded; 2], +> = std::cell::LazyCell::new(|| unsafe { [::default(); 2] }); +pub unsafe fn check_zero_bytes_4(mut bytes: *const u8, mut size: u64) { + { + let mut i: u64 = 0_u64; + 'loop_: while ((i) < (size)) { + 'for_body: { + assert!((((*bytes.offset((i) as isize)) as i32) == (0))); + } + i.prefix_inc(); + } + } +} +pub unsafe fn test_5() { + (unsafe { + check_zero_bytes_4( + ((&raw mut (*std::cell::LazyCell::force_mut(&mut *&raw mut static_padded_0)) + as *mut p6_2_zero_initialized_padding_Padded) as *const u8), + (::std::mem::size_of::() as u64), + ) + }); + (unsafe { + check_zero_bytes_4( + ((&raw mut (*std::cell::LazyCell::force_mut(&mut *&raw mut static_tail_padded_1)) + as *mut p6_2_zero_initialized_padding_TailPadded) as *const u8), + (::std::mem::size_of::() as u64), + ) + }); + (unsafe { + check_zero_bytes_4( + ((&raw mut (*std::cell::LazyCell::force_mut(&mut *&raw mut static_holds_padded_2)) + as *mut p6_2_zero_initialized_padding_HoldsPadded) as *const u8), + (::std::mem::size_of::() as u64), + ) + }); + (unsafe { + let _bytes: *const u8 = + ((&raw mut (*std::cell::LazyCell::force_mut(&mut *&raw mut static_padded_array_3)) + as *mut [p6_2_zero_initialized_padding_Padded; 2]) as *const u8); + let _size: u64 = + (::std::mem::size_of::<[p6_2_zero_initialized_padding_Padded; 2]>() as u64); + check_zero_bytes_4(_bytes, _size) + }); + let mut value_padded: p6_2_zero_initialized_padding_Padded = + ::default(); + (unsafe { + check_zero_bytes_4( + ((&mut value_padded as *mut p6_2_zero_initialized_padding_Padded) as *const u8), + (::std::mem::size_of::() as u64), + ) + }); + let mut value_holds_padded: p6_2_zero_initialized_padding_HoldsPadded = + ::default(); + (unsafe { + check_zero_bytes_4( + ((&mut value_holds_padded as *mut p6_2_zero_initialized_padding_HoldsPadded) + as *const u8), + (::std::mem::size_of::() as u64), + ) + }); + let mut heap_padded: *mut p6_2_zero_initialized_padding_Padded = + (Box::leak(Box::new(::default())) + as *mut p6_2_zero_initialized_padding_Padded); + (unsafe { + check_zero_bytes_4( + (heap_padded as *const u8), + (::std::mem::size_of::() as u64), + ) + }); + { + let __p = heap_padded; + if !__p.is_null() { + ::std::mem::drop(Box::from_raw(__p)) + } + }; + let mut heap_tail_padded: *mut p6_2_zero_initialized_padding_TailPadded = (Box::leak(Box::new( + ::default(), + )) + as *mut p6_2_zero_initialized_padding_TailPadded); + (unsafe { + check_zero_bytes_4( + (heap_tail_padded as *const u8), + (::std::mem::size_of::() as u64), + ) + }); + { + let __p = heap_tail_padded; + if !__p.is_null() { + ::std::mem::drop(Box::from_raw(__p)) + } + }; +} +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + libcc2rs::cpp2rust_exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + (unsafe { test_5() }); + return 0; +} +pub unsafe fn __cpp2rust_init_globals() { + std::cell::LazyCell::force(&*&raw const static_padded_0); + std::cell::LazyCell::force(&*&raw const static_tail_padded_1); + std::cell::LazyCell::force(&*&raw const static_holds_padded_2); + std::cell::LazyCell::force(&*&raw const static_padded_array_3); +} 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 9d73394598..a8e664d1da 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 @@ -7,13 +7,13 @@ use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] pub struct p6_1_zero_initialized_pointer_to_member_HasTwoInts { pub first: i32, pub second: i32, } #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe)] pub struct p6_1_zero_initialized_pointer_to_member_HoldsSelector { pub selector: isize, } @@ -47,6 +47,311 @@ pub unsafe fn test_1() { as isize)) ); } +pub type p6_1_zero_initialized_scalars_Color = i32; +pub const p6_1_zero_initialized_scalars_Color_red: p6_1_zero_initialized_scalars_Color = 1; +pub const p6_1_zero_initialized_scalars_Color_green: p6_1_zero_initialized_scalars_Color = 2; +pub static mut static_int_2: std::cell::LazyCell = + std::cell::LazyCell::new(|| unsafe { 0_i32 }); +pub static mut static_double_3: std::cell::LazyCell = + std::cell::LazyCell::new(|| unsafe { 0_f64 }); +pub static mut static_bool_4: std::cell::LazyCell = + std::cell::LazyCell::new(|| unsafe { false }); +pub static mut static_char_5: std::cell::LazyCell = + std::cell::LazyCell::new(|| unsafe { (0 as libc::c_char) }); +pub static mut static_pointer_6: std::cell::LazyCell<*mut i32> = + std::cell::LazyCell::new(|| unsafe { std::ptr::null_mut() }); +pub static mut static_enum_7: std::cell::LazyCell = + std::cell::LazyCell::new(|| unsafe { 0_i32 }); +pub static mut static_function_pointer_8: std::cell::LazyCell> = + std::cell::LazyCell::new(|| unsafe { None }); +pub static mut thread_local_long_9: std::cell::LazyCell = + std::cell::LazyCell::new(|| unsafe { 0_i64 }); +pub unsafe fn next_count_10() -> i32 { + return (*(*std::cell::LazyCell::force_mut(&mut *&raw mut counter_11)).prefix_inc()); +} +pub unsafe fn test_12() { + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut static_int_2)) == (0))); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut static_double_3)) == (0_f64))); + assert!(!(*std::cell::LazyCell::force_mut(&mut *&raw mut static_bool_4))); + assert!( + (((*std::cell::LazyCell::force_mut(&mut *&raw mut static_char_5)) as i32) + == (('\0' as libc::c_char) as i32)) + ); + assert!((*std::cell::LazyCell::force_mut(&mut *&raw mut static_pointer_6)).is_null()); + assert!( + ((*std::cell::LazyCell::force_mut(&mut *&raw mut static_enum_7)) + == ((0) as p6_1_zero_initialized_scalars_Color)) + ); + assert!( + ((*std::cell::LazyCell::force_mut(&mut *&raw mut static_enum_7)) + != (p6_1_zero_initialized_scalars_Color_red)) + ); + assert!((*std::cell::LazyCell::force_mut(&mut *&raw mut static_function_pointer_8)).is_none()); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut thread_local_long_9)) == (0_i64))); + assert!(((unsafe { next_count_10() }) == (1))); + assert!(((unsafe { next_count_10() }) == (2))); + let mut value_int: i32 = 0_i32; + let mut value_double: f64 = 0_f64; + let mut value_pointer: *mut i32 = std::ptr::null_mut(); + assert!(((value_int) == (0))); + assert!(((value_double) == (0_f64))); + assert!((value_pointer).is_null()); + let mut heap_int: *mut i32 = (Box::leak(Box::new(0_i32)) as *mut i32); + assert!(((*heap_int) == (0))); + { + let __p = heap_int; + if !__p.is_null() { + ::std::mem::drop(Box::from_raw(__p)) + } + }; +} +static mut counter_11: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0_i32 }); +#[repr(C)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] +pub struct p6_2_zero_initialized_class_Inner { + pub number: i32, + pub real: f64, + pub pointer: *mut i32, +} +#[repr(C)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] +pub struct p6_2_zero_initialized_class_Outer { + pub letter: libc::c_char, + pub inner: p6_2_zero_initialized_class_Inner, + pub numbers: [i32; 3], + pub inners: [p6_2_zero_initialized_class_Inner; 2], +} +pub static mut static_outer_13: std::cell::LazyCell = + std::cell::LazyCell::new(|| unsafe { ::default() }); +pub unsafe fn check_14(outer: *const p6_2_zero_initialized_class_Outer) { + assert!((((*outer).letter as i32) == (('\0' as libc::c_char) as i32))); + assert!((((*outer).inner.number) == (0))); + assert!((((*outer).inner.real) == (0_f64))); + assert!(((*outer).inner.pointer).is_null()); + { + let mut i: i32 = 0; + 'loop_: while ((i) < (3)) { + 'for_body: { + assert!((((*outer).numbers[(i) as usize]) == (0))); + } + i.prefix_inc(); + } + } + { + let mut i: i32 = 0; + 'loop_: while ((i) < (2)) { + 'for_body: { + assert!((((*outer).inners[(i) as usize].number) == (0))); + assert!((((*outer).inners[(i) as usize].real) == (0_f64))); + assert!(((*outer).inners[(i) as usize].pointer).is_null()); + } + i.prefix_inc(); + } + } +} +pub unsafe fn test_15() { + (unsafe { check_14(&(*std::cell::LazyCell::force_mut(&mut *&raw mut static_outer_13))) }); + let mut value_outer: p6_2_zero_initialized_class_Outer = + ::default(); + (unsafe { check_14(&value_outer) }); + let mut heap_outer: *mut p6_2_zero_initialized_class_Outer = + (Box::leak(Box::new(::default())) + as *mut p6_2_zero_initialized_class_Outer); + (unsafe { check_14(&(*heap_outer)) }); + { + let __p = heap_outer; + if !__p.is_null() { + ::std::mem::drop(Box::from_raw(__p)) + } + }; +} +#[repr(C)] +#[derive(Copy, Clone, FnPtrArg, VaArg, ZeroInitializationUnsafe)] +pub union p6_3_zero_initialized_union_Number { + pub integer: i32, + pub bytes: [libc::c_char; 8], +} +impl Default for p6_3_zero_initialized_union_Number { + fn default() -> Self { + unsafe { std::mem::zeroed() } + } +} +pub static mut static_number_16: std::cell::LazyCell = + std::cell::LazyCell::new(|| unsafe { ::default() }); +pub unsafe fn test_17() { + assert!((((*std::cell::LazyCell::force_mut(&mut *&raw mut static_number_16)).integer) == (0))); + let mut value_number: p6_3_zero_initialized_union_Number = + ::default(); + assert!(((value_number.integer) == (0))); +} +#[repr(C)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] +pub struct p6_4_zero_initialized_array_Pair { + pub first: i32, + pub second: i32, +} +pub static mut static_numbers_18: std::cell::LazyCell<[i32; 4]> = + std::cell::LazyCell::new(|| unsafe { [0_i32; 4] }); +pub static mut static_matrix_19: std::cell::LazyCell<[[i32; 3]; 2]> = + std::cell::LazyCell::new(|| unsafe { [[0_i32; 3]; 2] }); +pub static mut static_pairs_20: std::cell::LazyCell<[p6_4_zero_initialized_array_Pair; 2]> = + std::cell::LazyCell::new(|| unsafe { [::default(); 2] }); +pub unsafe fn test_21() { + { + let mut i: i32 = 0; + 'loop_: while ((i) < (4)) { + 'for_body: { + assert!( + (((*std::cell::LazyCell::force_mut(&mut *&raw mut static_numbers_18)) + [(i) as usize]) + == (0)) + ); + } + i.prefix_inc(); + } + } + { + let mut i: i32 = 0; + 'loop_: while ((i) < (2)) { + 'for_body: { + { + let mut j: i32 = 0; + 'loop_: while ((j) < (3)) { + 'for_body: { + assert!( + (((*std::cell::LazyCell::force_mut( + &mut *&raw mut static_matrix_19 + ))[(i) as usize][(j) as usize]) + == (0)) + ); + } + j.prefix_inc(); + } + } + } + i.prefix_inc(); + } + } + { + let mut i: i32 = 0; + 'loop_: while ((i) < (2)) { + 'for_body: { + assert!( + (((*std::cell::LazyCell::force_mut(&mut *&raw mut static_pairs_20)) + [(i) as usize] + .first) + == (0)) + ); + assert!( + (((*std::cell::LazyCell::force_mut(&mut *&raw mut static_pairs_20)) + [(i) as usize] + .second) + == (0)) + ); + } + i.prefix_inc(); + } + } + let mut heap_numbers: *mut i32 = + Box::leak((0..3_usize).map(|_| 0_i32).collect::>()).as_mut_ptr(); + { + let mut i: i32 = 0; + 'loop_: while ((i) < (3)) { + 'for_body: { + assert!(((*heap_numbers.offset((i) as isize)) == (0))); + } + i.prefix_inc(); + } + } + { + let __p = heap_numbers; + 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::(), + ))) + } + }; +} +#[repr(C)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe)] +pub struct p9_1_2_zero_then_default_initialized_WithDefaultMember { + pub zeroed: i32, + pub five: i32, +} +impl Default for p9_1_2_zero_then_default_initialized_WithDefaultMember { + fn default() -> Self { + unsafe { + p9_1_2_zero_then_default_initialized_WithDefaultMember { + zeroed: 0_i32, + five: 5, + } + } + } +} +#[repr(C)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe)] +pub struct p9_1_2_zero_then_default_initialized_LeavesMemberAlone { + pub untouched: i32, +} +impl p9_1_2_zero_then_default_initialized_LeavesMemberAlone { + pub unsafe fn construct(&mut self) { + (&raw mut self.untouched).write(0_i32); + } +} +impl Default for p9_1_2_zero_then_default_initialized_LeavesMemberAlone { + fn default() -> Self { + unsafe { + { + let mut __v: p9_1_2_zero_then_default_initialized_LeavesMemberAlone = + ::zero_init(); + p9_1_2_zero_then_default_initialized_LeavesMemberAlone::construct(&mut __v); + __v + } + } + } +} +#[repr(C)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] +pub struct p9_1_2_zero_then_default_initialized_HoldsLeavesMemberAlone { + pub number: i32, + pub member: p9_1_2_zero_then_default_initialized_LeavesMemberAlone, +} +pub unsafe fn test_22() { + let mut value: p9_1_2_zero_then_default_initialized_WithDefaultMember = + ::default(); + assert!(((value.zeroed) == (0))); + assert!(((value.five) == (5))); + let mut heap: *mut p9_1_2_zero_then_default_initialized_WithDefaultMember = (Box::leak( + Box::new(::default()), + ) + as *mut p9_1_2_zero_then_default_initialized_WithDefaultMember); + assert!((((*heap).zeroed) == (0))); + assert!((((*heap).five) == (5))); + { + let __p = heap; + if !__p.is_null() { + ::std::mem::drop(Box::from_raw(__p)) + } + }; + let mut holder: p9_1_2_zero_then_default_initialized_HoldsLeavesMemberAlone = + ::default(); + assert!(((holder.number) == (0))); + assert!(((holder.member.untouched) == (0))); + let mut heap_holder: *mut p9_1_2_zero_then_default_initialized_HoldsLeavesMemberAlone = + (Box::leak(Box::new( + ::default(), + )) as *mut p9_1_2_zero_then_default_initialized_HoldsLeavesMemberAlone); + assert!((((*heap_holder).number) == (0))); + assert!((((*heap_holder).member.untouched) == (0))); + { + let __p = heap_holder; + if !__p.is_null() { + ::std::mem::drop(Box::from_raw(__p)) + } + }; +} pub fn main() { unsafe { __cpp2rust_init_globals(); @@ -55,8 +360,26 @@ pub fn main() { } unsafe fn main_0() -> i32 { (unsafe { test_1() }); + (unsafe { test_12() }); + (unsafe { test_15() }); + (unsafe { test_17() }); + (unsafe { test_21() }); + (unsafe { test_22() }); return 0; } pub unsafe fn __cpp2rust_init_globals() { std::cell::LazyCell::force(&*&raw const static_pm_0); + std::cell::LazyCell::force(&*&raw const static_int_2); + std::cell::LazyCell::force(&*&raw const static_double_3); + std::cell::LazyCell::force(&*&raw const static_bool_4); + std::cell::LazyCell::force(&*&raw const static_char_5); + std::cell::LazyCell::force(&*&raw const static_pointer_6); + std::cell::LazyCell::force(&*&raw const static_enum_7); + std::cell::LazyCell::force(&*&raw const static_function_pointer_8); + std::cell::LazyCell::force(&*&raw const thread_local_long_9); + std::cell::LazyCell::force(&*&raw const static_outer_13); + std::cell::LazyCell::force(&*&raw const static_number_16); + 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); } diff --git a/tests/std/dcl.dcl/dcl.decl/dcl.meaning/out/refcount/dcl.array.rs b/tests/std/dcl.dcl/dcl.decl/dcl.meaning/out/refcount/dcl.array.rs index 03dc37b6c1..f7875ca5e8 100644 --- a/tests/std/dcl.dcl/dcl.decl/dcl.meaning/out/refcount/dcl.array.rs +++ b/tests/std/dcl.dcl/dcl.decl/dcl.meaning/out/refcount/dcl.array.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(12)] pub struct p6_array_of_pointers_to_data_members_HasThreeInts { #[offset(0)] diff --git a/tests/std/dcl.dcl/dcl.decl/dcl.meaning/out/refcount/dcl.fct.default.rs b/tests/std/dcl.dcl/dcl.decl/dcl.meaning/out/refcount/dcl.fct.default.rs index 248c01c713..4e7da1d1a3 100644 --- a/tests/std/dcl.dcl/dcl.decl/dcl.meaning/out/refcount/dcl.fct.default.rs +++ b/tests/std/dcl.dcl/dcl.decl/dcl.meaning/out/refcount/dcl.fct.default.rs @@ -54,7 +54,7 @@ thread_local!( thread_local!( pub static step_10: Value = Rc::new(RefCell::new(4)); ); -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg)] #[byte_size(4)] pub struct p9_static_member_hides_namespace_variable_HasStaticStep { #[offset(0)] @@ -105,21 +105,26 @@ pub fn count_default_13() -> i32 { pub fn combine_14(mut first: i32, mut second: i32) -> i32 { return ((first * 100) + second); } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, CppFrom)] -#[cpp_from(i32, new, value)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, CppFrom)] +#[cpp_from(i32, construct, value)] #[byte_size(4)] pub struct p9_evaluated_each_time_called_Widget { #[offset(0)] pub id: i32, } -impl p9_evaluated_each_time_called_Widget { - pub fn new(mut id: i32) -> Self { - Self { id: id } - } -} impl Default for p9_evaluated_each_time_called_Widget { fn default() -> Self { - { p9_evaluated_each_time_called_Widget::new({ ({ count_default_13() }) }) } + { + { + let __v: Value = Rc::new(RefCell::new( + ::zero_init(), + )); + p9_evaluated_each_time_called_WidgetImpl::construct(&__v.as_pointer(), { + ({ count_default_13() }) + }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + } + } } } thread_local!( @@ -127,7 +132,7 @@ thread_local!( ); pub type anon_16 = u32; pub const anon_16_kBonus: anon_16 = 5; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg)] #[byte_size(4)] pub struct p9_evaluated_each_time_called_Account { #[offset(0)] @@ -156,12 +161,31 @@ pub fn test_17() { assert!((({ combine_14((*first.prefix_inc()), ({ count_default_13() }),) }) == 103)); assert!((first == 1)); assert!((defaults_evaluated_12.with(|rc| *rc.borrow()) == 3)); - let mut a: p9_evaluated_each_time_called_Widget = - p9_evaluated_each_time_called_Widget::new({ ({ count_default_13() }) }); - let mut b: p9_evaluated_each_time_called_Widget = - p9_evaluated_each_time_called_Widget::new({ 40 }); - let mut c: p9_evaluated_each_time_called_Widget = - p9_evaluated_each_time_called_Widget::new({ ({ count_default_13() }) }); + let mut a: p9_evaluated_each_time_called_Widget = { + let __v: Value = Rc::new(RefCell::new( + ::zero_init(), + )); + p9_evaluated_each_time_called_WidgetImpl::construct(&__v.as_pointer(), { + ({ count_default_13() }) + }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }; + let mut b: p9_evaluated_each_time_called_Widget = { + let __v: Value = Rc::new(RefCell::new( + ::zero_init(), + )); + p9_evaluated_each_time_called_WidgetImpl::construct(&__v.as_pointer(), { 40 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }; + let mut c: p9_evaluated_each_time_called_Widget = { + let __v: Value = Rc::new(RefCell::new( + ::zero_init(), + )); + p9_evaluated_each_time_called_WidgetImpl::construct(&__v.as_pointer(), { + ({ count_default_13() }) + }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }; assert!((a.id == 4)); assert!((b.id == 40)); assert!((c.id == 5)); @@ -197,7 +221,7 @@ pub fn test_17() { pub fn add_18(mut a: i32, mut b: i32) -> i32 { return (a + b); } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(1)] pub struct p9_not_part_of_the_type_Scale {} impl p9_not_part_of_the_type_Scale { @@ -280,6 +304,14 @@ impl p9_evaluated_each_time_called_AccountImpl for Ptr { + fn construct(&self, mut id: i32) { + field!((*self), id).write(id); + } +} pub trait p9_static_member_hides_namespace_variable_HasStaticStepImpl { fn advance(&self, amount: i32) -> i32; } diff --git a/tests/std/dcl.dcl/dcl.decl/dcl.meaning/out/refcount/dcl.mptr.rs b/tests/std/dcl.dcl/dcl.decl/dcl.meaning/out/refcount/dcl.mptr.rs index ef83af1221..815ad1c1cd 100644 --- a/tests/std/dcl.dcl/dcl.decl/dcl.meaning/out/refcount/dcl.mptr.rs +++ b/tests/std/dcl.dcl/dcl.decl/dcl.meaning/out/refcount/dcl.mptr.rs @@ -6,13 +6,14 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg)] #[byte_size(16)] pub struct p2_pointer_to_array_member_HasIntArray { #[offset(0)] pub before: i32, #[offset(4)] #[byte_size(12)] + #[array_len(3)] pub elements: Box<[i32]>, } impl Default for p2_pointer_to_array_member_HasIntArray { @@ -39,7 +40,7 @@ pub fn test_0() { assert!(((array_elem!(obj.as_pointer(), elements, 1).read()) == 7)); assert!(({ (*obj.borrow()).before } == 1)); } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct p2_pointer_to_data_member_HasTwoInts { #[offset(0)] @@ -67,7 +68,7 @@ pub fn test_1() { thread_local!( pub static pm_2: Value = Rc::new(RefCell::new(-1_isize)); ); -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct p2_pointer_to_member_of_incomplete_class_CompletedLater { #[offset(0)] @@ -96,7 +97,7 @@ pub fn test_3() { == 4) ); } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct p2_pointer_to_member_of_pointer_to_member_type_HasTwoInts { #[offset(0)] @@ -104,7 +105,7 @@ pub struct p2_pointer_to_member_of_pointer_to_member_type_HasTwoInts { #[offset(4)] pub second: i32, } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg)] #[byte_size(8)] pub struct p2_pointer_to_member_of_pointer_to_member_type_HoldsSelector { #[offset(0)] diff --git a/tests/std/dcl.dcl/dcl.decl/dcl.meaning/out/unsafe/dcl.array.rs b/tests/std/dcl.dcl/dcl.decl/dcl.meaning/out/unsafe/dcl.array.rs index 9dce97f173..204a1d4fcc 100644 --- a/tests/std/dcl.dcl/dcl.decl/dcl.meaning/out/unsafe/dcl.array.rs +++ b/tests/std/dcl.dcl/dcl.decl/dcl.meaning/out/unsafe/dcl.array.rs @@ -7,7 +7,7 @@ use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] pub struct p6_array_of_pointers_to_data_members_HasThreeInts { pub first: i32, pub second: i32, diff --git a/tests/std/dcl.dcl/dcl.decl/dcl.meaning/out/unsafe/dcl.fct.default.rs b/tests/std/dcl.dcl/dcl.decl/dcl.meaning/out/unsafe/dcl.fct.default.rs index ae65178472..f14341b2a4 100644 --- a/tests/std/dcl.dcl/dcl.decl/dcl.meaning/out/unsafe/dcl.fct.default.rs +++ b/tests/std/dcl.dcl/dcl.decl/dcl.meaning/out/unsafe/dcl.fct.default.rs @@ -54,7 +54,7 @@ pub unsafe fn test_8() { pub static mut step_9: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 100 }); pub static mut step_10: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 4 }); #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe)] pub struct p9_static_member_hides_namespace_variable_HasStaticStep { pub total: i32, } @@ -105,27 +105,35 @@ pub unsafe fn combine_14(mut first: i32, mut second: i32) -> i32 { return (((first) * (100)) + (second)); } #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg, CppFromUnsafe)] -#[cpp_from(i32, new, value)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, CppFromUnsafe)] +#[cpp_from(i32, construct, value)] pub struct p9_evaluated_each_time_called_Widget { pub id: i32, } impl p9_evaluated_each_time_called_Widget { - pub unsafe fn new(mut id: i32) -> Self { - let mut this = Self { id: id }; - this + pub unsafe fn construct(&mut self, mut id: i32) { + (&raw mut self.id).write(id); } } impl Default for p9_evaluated_each_time_called_Widget { fn default() -> Self { - unsafe { p9_evaluated_each_time_called_Widget::new({ (unsafe { count_default_13() }) }) } + unsafe { + { + let mut __v: p9_evaluated_each_time_called_Widget = + ::zero_init(); + p9_evaluated_each_time_called_Widget::construct(&mut __v, { + (unsafe { count_default_13() }) + }); + __v + } + } } } static mut base_15: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 10 }); pub type anon_16 = u32; pub const anon_16_kBonus: anon_16 = 5; #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe)] pub struct p9_evaluated_each_time_called_Account { pub balance: i32, } @@ -158,12 +166,28 @@ pub unsafe fn test_17() { ); assert!(((first) == (1))); assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut defaults_evaluated_12)) == (3))); - let mut a: p9_evaluated_each_time_called_Widget = - p9_evaluated_each_time_called_Widget::new({ (unsafe { count_default_13() }) }); - let mut b: p9_evaluated_each_time_called_Widget = - p9_evaluated_each_time_called_Widget::new({ 40 }); - let mut c: p9_evaluated_each_time_called_Widget = - p9_evaluated_each_time_called_Widget::new({ (unsafe { count_default_13() }) }); + let mut a: p9_evaluated_each_time_called_Widget = { + let mut __v: p9_evaluated_each_time_called_Widget = + ::zero_init(); + p9_evaluated_each_time_called_Widget::construct(&mut __v, { + (unsafe { count_default_13() }) + }); + __v + }; + let mut b: p9_evaluated_each_time_called_Widget = { + let mut __v: p9_evaluated_each_time_called_Widget = + ::zero_init(); + p9_evaluated_each_time_called_Widget::construct(&mut __v, { 40 }); + __v + }; + let mut c: p9_evaluated_each_time_called_Widget = { + let mut __v: p9_evaluated_each_time_called_Widget = + ::zero_init(); + p9_evaluated_each_time_called_Widget::construct(&mut __v, { + (unsafe { count_default_13() }) + }); + __v + }; assert!(((a.id) == (4))); assert!(((b.id) == (40))); assert!(((c.id) == (5))); @@ -203,7 +227,7 @@ pub unsafe fn add_18(mut a: i32, mut b: i32) -> i32 { return ((a) + (b)); } #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] pub struct p9_not_part_of_the_type_Scale {} impl p9_not_part_of_the_type_Scale { pub unsafe fn times(mut a: i32, mut b: i32) -> i32 { diff --git a/tests/std/dcl.dcl/dcl.decl/dcl.meaning/out/unsafe/dcl.mptr.rs b/tests/std/dcl.dcl/dcl.decl/dcl.meaning/out/unsafe/dcl.mptr.rs index ccbde17ba4..27a87e5ea3 100644 --- a/tests/std/dcl.dcl/dcl.decl/dcl.meaning/out/unsafe/dcl.mptr.rs +++ b/tests/std/dcl.dcl/dcl.decl/dcl.meaning/out/unsafe/dcl.mptr.rs @@ -7,7 +7,7 @@ use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] pub struct p2_pointer_to_array_member_HasIntArray { pub before: i32, pub elements: [i32; 3], @@ -38,7 +38,7 @@ pub unsafe fn test_0() { assert!(((obj.before) == (1))); } #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] pub struct p2_pointer_to_data_member_HasTwoInts { pub first: i32, pub second: i32, @@ -76,7 +76,7 @@ pub unsafe fn test_1() { } pub static mut pm_2: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { -1_isize }); #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] pub struct p2_pointer_to_member_of_incomplete_class_CompletedLater { pub first: i32, pub second: i32, @@ -100,13 +100,13 @@ pub unsafe fn test_3() { ); } #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] pub struct p2_pointer_to_member_of_pointer_to_member_type_HasTwoInts { pub first: i32, pub second: i32, } #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe)] pub struct p2_pointer_to_member_of_pointer_to_member_type_HoldsSelector { pub selector: isize, } diff --git a/tests/std/expr/conv/out/refcount/conv.mem.rs b/tests/std/expr/conv/out/refcount/conv.mem.rs index 93c47a391f..74a595f022 100644 --- a/tests/std/expr/conv/out/refcount/conv.mem.rs +++ b/tests/std/expr/conv/out/refcount/conv.mem.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct p1_null_distinct_from_first_member_HasTwoInts { #[offset(0)] diff --git a/tests/std/expr/conv/out/unsafe/conv.mem.rs b/tests/std/expr/conv/out/unsafe/conv.mem.rs index 2b289b99e0..0c8703d9e1 100644 --- a/tests/std/expr/conv/out/unsafe/conv.mem.rs +++ b/tests/std/expr/conv/out/unsafe/conv.mem.rs @@ -7,7 +7,7 @@ use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] pub struct p1_null_distinct_from_first_member_HasTwoInts { pub first: i32, pub second: i32, diff --git a/tests/std/expr/expr.compound/expr.post/out/refcount/expr.const.cast.rs b/tests/std/expr/expr.compound/expr.post/out/refcount/expr.const.cast.rs index 69df1e73e2..82485c58d0 100644 --- a/tests/std/expr/expr.compound/expr.post/out/refcount/expr.const.cast.rs +++ b/tests/std/expr/expr.compound/expr.post/out/refcount/expr.const.cast.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct p4_reference_const_cast_Pair { #[offset(0)] diff --git a/tests/std/expr/expr.compound/expr.post/out/refcount/expr.reinterpret.cast-p10-result-is-unspecified.rs b/tests/std/expr/expr.compound/expr.post/out/refcount/expr.reinterpret.cast-p10-result-is-unspecified.rs index 1d2579260d..36e1b8a0b8 100644 --- a/tests/std/expr/expr.compound/expr.post/out/refcount/expr.reinterpret.cast-p10-result-is-unspecified.rs +++ b/tests/std/expr/expr.compound/expr.post/out/refcount/expr.reinterpret.cast-p10-result-is-unspecified.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct HasTwoInts { #[offset(0)] @@ -14,7 +14,7 @@ pub struct HasTwoInts { #[offset(4)] pub second: i32, } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct AlsoHasTwoInts { #[offset(0)] diff --git a/tests/std/expr/expr.compound/expr.post/out/refcount/expr.reinterpret.cast.rs b/tests/std/expr/expr.compound/expr.post/out/refcount/expr.reinterpret.cast.rs index b73e3efec8..e0ee6af6db 100644 --- a/tests/std/expr/expr.compound/expr.post/out/refcount/expr.reinterpret.cast.rs +++ b/tests/std/expr/expr.compound/expr.post/out/refcount/expr.reinterpret.cast.rs @@ -16,7 +16,7 @@ pub fn test_0() { (*value.borrow_mut()) = 7; assert!((((converted.read()).read()) == 7)); } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct p10_null_converts_to_null_HasTwoInts { #[offset(0)] @@ -24,7 +24,7 @@ pub struct p10_null_converts_to_null_HasTwoInts { #[offset(4)] pub second: i32, } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(1)] pub struct p10_null_converts_to_null_HasChar { #[offset(0)] @@ -35,7 +35,7 @@ pub fn test_1() { let mut converted: isize = pm; assert!(((converted) == -1_isize)); } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct p10_2_data_member_round_trip_HasTwoInts { #[offset(0)] @@ -43,7 +43,7 @@ pub struct p10_2_data_member_round_trip_HasTwoInts { #[offset(4)] pub second: i32, } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(1)] pub struct p10_2_data_member_round_trip_HasChar { #[offset(0)] diff --git a/tests/std/expr/expr.compound/expr.post/out/unsafe/expr.const.cast.rs b/tests/std/expr/expr.compound/expr.post/out/unsafe/expr.const.cast.rs index 624bbfa041..106cbcb48c 100644 --- a/tests/std/expr/expr.compound/expr.post/out/unsafe/expr.const.cast.rs +++ b/tests/std/expr/expr.compound/expr.post/out/unsafe/expr.const.cast.rs @@ -7,7 +7,7 @@ use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] pub struct p4_reference_const_cast_Pair { pub x: i32, } diff --git a/tests/std/expr/expr.compound/expr.post/out/unsafe/expr.reinterpret.cast-p10-result-is-unspecified.rs b/tests/std/expr/expr.compound/expr.post/out/unsafe/expr.reinterpret.cast-p10-result-is-unspecified.rs index 433e281cb8..19f5758fbf 100644 --- a/tests/std/expr/expr.compound/expr.post/out/unsafe/expr.reinterpret.cast-p10-result-is-unspecified.rs +++ b/tests/std/expr/expr.compound/expr.post/out/unsafe/expr.reinterpret.cast-p10-result-is-unspecified.rs @@ -7,13 +7,13 @@ use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] pub struct HasTwoInts { pub first: i32, pub second: i32, } #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] pub struct AlsoHasTwoInts { pub first: i32, pub second: i32, diff --git a/tests/std/expr/expr.compound/expr.post/out/unsafe/expr.reinterpret.cast.rs b/tests/std/expr/expr.compound/expr.post/out/unsafe/expr.reinterpret.cast.rs index 903486fc67..712dee1955 100644 --- a/tests/std/expr/expr.compound/expr.post/out/unsafe/expr.reinterpret.cast.rs +++ b/tests/std/expr/expr.compound/expr.post/out/unsafe/expr.reinterpret.cast.rs @@ -17,13 +17,13 @@ pub unsafe fn test_0() { assert!(((*(*converted)) == (7))); } #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] pub struct p10_null_converts_to_null_HasTwoInts { pub first: i32, pub second: i32, } #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] pub struct p10_null_converts_to_null_HasChar { pub character: libc::c_char, } @@ -33,13 +33,13 @@ pub unsafe fn test_1() { assert!(((converted) == -1_isize)); } #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] pub struct p10_2_data_member_round_trip_HasTwoInts { pub first: i32, pub second: i32, } #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] pub struct p10_2_data_member_round_trip_HasChar { pub character: libc::c_char, } diff --git a/tests/std/expr/expr.compound/expr.unary/out/refcount/expr.pre.incr.rs b/tests/std/expr/expr.compound/expr.unary/out/refcount/expr.pre.incr.rs index 900ddec18a..5abe461cf4 100644 --- a/tests/std/expr/expr.compound/expr.unary/out/refcount/expr.pre.incr.rs +++ b/tests/std/expr/expr.compound/expr.unary/out/refcount/expr.pre.incr.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct p1_result_is_updated_operand_Counter { #[offset(0)] diff --git a/tests/std/expr/expr.compound/expr.unary/out/refcount/expr.unary.op.rs b/tests/std/expr/expr.compound/expr.unary/out/refcount/expr.unary.op.rs index 76205b5923..b597866a64 100644 --- a/tests/std/expr/expr.compound/expr.unary/out/refcount/expr.unary.op.rs +++ b/tests/std/expr/expr.compound/expr.unary/out/refcount/expr.unary.op.rs @@ -12,7 +12,7 @@ thread_local!( thread_local!( pub static calls_1: Value = Rc::new(RefCell::new(0)); ); -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(1)] pub struct p3_2_static_member_is_ordinary_pointer_HasStaticMembers {} impl p3_2_static_member_is_ordinary_pointer_HasStaticMembers { @@ -30,7 +30,7 @@ pub fn test_2() { ({ function_pointer.call() }); assert!((calls_1.with(|rc| *rc.borrow()) == 1)); } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct p3_2_explicit_object_member_is_ordinary_pointer_HasExplicitObjectMember { #[offset(0)] @@ -46,7 +46,7 @@ pub fn test_3() { (*object.borrow_mut()).base = 5; assert!((({ function_pointer.call(object.as_pointer(), 1,) }) == 6)); } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct p4_parenthesized_qualified_id_is_object_pointer_TakesAddressOfOwnMember { #[offset(0)] diff --git a/tests/std/expr/expr.compound/expr.unary/out/unsafe/expr.pre.incr.rs b/tests/std/expr/expr.compound/expr.unary/out/unsafe/expr.pre.incr.rs index 6c3aa01462..2e68a582ca 100644 --- a/tests/std/expr/expr.compound/expr.unary/out/unsafe/expr.pre.incr.rs +++ b/tests/std/expr/expr.compound/expr.unary/out/unsafe/expr.pre.incr.rs @@ -7,7 +7,7 @@ use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] pub struct p1_result_is_updated_operand_Counter { pub n: i32, } diff --git a/tests/std/expr/expr.compound/expr.unary/out/unsafe/expr.unary.op.rs b/tests/std/expr/expr.compound/expr.unary/out/unsafe/expr.unary.op.rs index a9455ad1b1..5e194343d7 100644 --- a/tests/std/expr/expr.compound/expr.unary/out/unsafe/expr.unary.op.rs +++ b/tests/std/expr/expr.compound/expr.unary/out/unsafe/expr.unary.op.rs @@ -9,7 +9,7 @@ use std::rc::Rc; pub static mut shared_0: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 3 }); pub static mut calls_1: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] pub struct p3_2_static_member_is_ordinary_pointer_HasStaticMembers {} impl p3_2_static_member_is_ordinary_pointer_HasStaticMembers { pub unsafe fn run() { @@ -28,7 +28,7 @@ pub unsafe fn test_2() { assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut calls_1)) == (1))); } #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] pub struct p3_2_explicit_object_member_is_ordinary_pointer_HasExplicitObjectMember { pub base: i32, } @@ -54,7 +54,7 @@ pub unsafe fn test_3() { assert!(((unsafe { (function_pointer).unwrap()(&object, 1,) }) == (6))); } #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] pub struct p4_parenthesized_qualified_id_is_object_pointer_TakesAddressOfOwnMember { pub value: i32, } 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 6b8daeef1d..d0cf7c81d8 100644 --- a/tests/std/expr/expr.compound/out/refcount/expr.cond.rs +++ b/tests/std/expr/expr.compound/out/refcount/expr.cond.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct p7_4_pointer_to_member_and_null_HasTwoInts { #[offset(0)] diff --git a/tests/std/expr/expr.compound/out/refcount/expr.eq.rs b/tests/std/expr/expr.compound/out/refcount/expr.eq.rs index 60e6b1423d..2e7cca7b6a 100644 --- a/tests/std/expr/expr.compound/out/refcount/expr.eq.rs +++ b/tests/std/expr/expr.compound/out/refcount/expr.eq.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct p4_1_both_null_HasTwoInts { #[offset(0)] @@ -20,7 +20,7 @@ pub fn test_0() { assert!((first_null == second_null)); assert!(!(first_null != second_null)); } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct p4_2_one_null_HasTwoInts { #[offset(0)] @@ -36,11 +36,12 @@ pub fn test_1() { assert!((null_pm != first_pm)); assert!(!(null_pm == first_pm)); } -#[derive(VaArg, FnPtrArg, ByteRepr, Clone)] +#[derive(VaArg, FnPtrArg, ByteRepr, Clone, ZeroInitialization)] #[byte_size(4)] pub struct p4_5_members_of_same_union_HasTwoIntMembers { #[offset(0)] #[byte_size(4)] + #[array_len(4)] __bytes: Box<[u8]>, } impl p4_5_members_of_same_union_HasTwoIntMembers { @@ -64,7 +65,7 @@ pub fn test_2() { assert!((first_pm == second_pm)); assert!(!(first_pm != second_pm)); } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct p4_6_data_members_HasTwoInts { #[offset(0)] diff --git a/tests/std/expr/expr.compound/out/refcount/expr.mptr.oper-p6-null-pointer-to-data-member.rs b/tests/std/expr/expr.compound/out/refcount/expr.mptr.oper-p6-null-pointer-to-data-member.rs index c655f24343..58dba1bf76 100644 --- a/tests/std/expr/expr.compound/out/refcount/expr.mptr.oper-p6-null-pointer-to-data-member.rs +++ b/tests/std/expr/expr.compound/out/refcount/expr.mptr.oper-p6-null-pointer-to-data-member.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct HasTwoInts { #[offset(0)] diff --git a/tests/std/expr/expr.compound/out/refcount/expr.mptr.oper.rs b/tests/std/expr/expr.compound/out/refcount/expr.mptr.oper.rs index a838ff081c..818abc1fc9 100644 --- a/tests/std/expr/expr.compound/out/refcount/expr.mptr.oper.rs +++ b/tests/std/expr/expr.compound/out/refcount/expr.mptr.oper.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct p3_arrow_star_HasTwoInts { #[offset(0)] @@ -27,7 +27,7 @@ pub fn test_0() { assert!(({ (*obj.borrow()).first } == 3)); assert!((((p).member::(pm)) == ((p).member::(pm)))); } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct p4_object_expression_sequenced_first_HasValue { #[offset(0)] @@ -69,7 +69,7 @@ pub fn test_7() { assert!((object_stamp_2.with(|rc| *rc.borrow()) == 1)); assert!((member_stamp_3.with(|rc| *rc.borrow()) == 2)); } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct p6_data_member_of_lvalue_is_lvalue_HasTwoInts { #[offset(0)] @@ -98,63 +98,65 @@ thread_local!( thread_local!( pub static moveCount_10: Value = Rc::new(RefCell::new(0)); ); -#[derive(Record, ByteRepr, VaArg, FnPtrArg, MoveCtor, Default)] +#[derive(Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, MoveCtor, Default)] #[byte_size(1)] pub struct p6_data_member_of_rvalue_is_xvalue_CountsCopiesAndMoves {} -impl p6_data_member_of_rvalue_is_xvalue_CountsCopiesAndMoves { - pub fn copy_from(_a0: Ptr) -> Self { - let __this: p6_data_member_of_rvalue_is_xvalue_CountsCopiesAndMoves = Self {}; - (*copyCount_9.with(Value::clone).borrow_mut()).prefix_inc(); - __this - } - pub fn move_from(_a0: Ptr) -> Self { - let __this: p6_data_member_of_rvalue_is_xvalue_CountsCopiesAndMoves = Self {}; - (*moveCount_10.with(Value::clone).borrow_mut()).prefix_inc(); - __this - } -} impl Clone for p6_data_member_of_rvalue_is_xvalue_CountsCopiesAndMoves { fn clone(&self) -> Self { let __src: Value = Rc::new( RefCell::new(p6_data_member_of_rvalue_is_xvalue_CountsCopiesAndMoves {}), ); - p6_data_member_of_rvalue_is_xvalue_CountsCopiesAndMoves::copy_from(__src.as_pointer()) + let __v: Value = Rc::new( + RefCell::new(::zero_init()), + ); + p6_data_member_of_rvalue_is_xvalue_CountsCopiesAndMovesImpl::copy_from( + &__v.as_pointer(), + __src.as_pointer(), + ); + Rc::try_unwrap(__v).ok().unwrap().into_inner() } } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, MoveCtor, Default)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, MoveCtor, Default)] #[byte_size(1)] pub struct p6_data_member_of_rvalue_is_xvalue_HoldsCounted { #[offset(0)] #[byte_size(1)] pub member: p6_data_member_of_rvalue_is_xvalue_CountsCopiesAndMoves, } -impl p6_data_member_of_rvalue_is_xvalue_HoldsCounted { - pub fn move_from(_a0: Ptr) -> Self { - Self { - member: p6_data_member_of_rvalue_is_xvalue_CountsCopiesAndMoves::move_from({ - field_ptr!(_a0, member) - }), - } - } -} pub fn test_11() { let mut pm: isize = (offset_of_field!(p6_data_member_of_rvalue_is_xvalue_HoldsCounted, member) as isize); let holder: Value = Rc::new(RefCell::new( ::default(), )); - let mut from_lvalue: p6_data_member_of_rvalue_is_xvalue_CountsCopiesAndMoves = - p6_data_member_of_rvalue_is_xvalue_CountsCopiesAndMoves::copy_from({ - (holder.as_pointer()) - .member::(pm) - }); + let mut from_lvalue: p6_data_member_of_rvalue_is_xvalue_CountsCopiesAndMoves = { + let __v: Value = Rc::new( + RefCell::new(::zero_init()), + ); + p6_data_member_of_rvalue_is_xvalue_CountsCopiesAndMovesImpl::copy_from( + &__v.as_pointer(), + { + (holder.as_pointer()) + .member::(pm) + }, + ); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }; assert!((copyCount_9.with(|rc| *rc.borrow()) == 1)); assert!((moveCount_10.with(|rc| *rc.borrow()) == 0)); - let mut from_xvalue: p6_data_member_of_rvalue_is_xvalue_CountsCopiesAndMoves = - p6_data_member_of_rvalue_is_xvalue_CountsCopiesAndMoves::move_from({ - (holder.as_pointer()) - .member::(pm) - }); + let mut from_xvalue: p6_data_member_of_rvalue_is_xvalue_CountsCopiesAndMoves = { + let __v: Value = Rc::new( + RefCell::new(::zero_init()), + ); + p6_data_member_of_rvalue_is_xvalue_CountsCopiesAndMovesImpl::move_from( + &__v.as_pointer(), + { + (holder.as_pointer()) + .member::(pm) + }, + ); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }; assert!((copyCount_9.with(|rc| *rc.borrow()) == 1)); assert!((moveCount_10.with(|rc| *rc.borrow()) == 1)); } @@ -169,6 +171,40 @@ fn main_0() -> i32 { ({ test_11() }); return 0; } +pub trait p6_data_member_of_rvalue_is_xvalue_CountsCopiesAndMovesImpl { + fn copy_from(&self, _a0: Ptr); + fn move_from(&self, _a0: Ptr); +} +impl p6_data_member_of_rvalue_is_xvalue_CountsCopiesAndMovesImpl + for Ptr +{ + fn copy_from(&self, _a0: Ptr) { + (*copyCount_9.with(Value::clone).borrow_mut()).prefix_inc(); + } + fn move_from(&self, _a0: Ptr) { + (*moveCount_10.with(Value::clone).borrow_mut()).prefix_inc(); + } +} +pub trait p6_data_member_of_rvalue_is_xvalue_HoldsCountedImpl { + fn move_from(&self, _a0: Ptr); +} +impl p6_data_member_of_rvalue_is_xvalue_HoldsCountedImpl + for Ptr +{ + fn move_from(&self, _a0: Ptr) { + field!((*self), member).write({ + let __v: Value = + Rc::new(RefCell::new( + ::zero_init(), + )); + p6_data_member_of_rvalue_is_xvalue_CountsCopiesAndMovesImpl::move_from( + &__v.as_pointer(), + { field_ptr!(_a0, member) }, + ); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }); + } +} pub fn __cpp2rust_init_globals() { let _ = sequence_1.with(|_| ()); let _ = object_stamp_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 41dda42368..892aa79d43 100644 --- a/tests/std/expr/expr.compound/out/unsafe/expr.cond.rs +++ b/tests/std/expr/expr.compound/out/unsafe/expr.cond.rs @@ -7,7 +7,7 @@ use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] pub struct p7_4_pointer_to_member_and_null_HasTwoInts { pub first: i32, pub second: i32, diff --git a/tests/std/expr/expr.compound/out/unsafe/expr.eq.rs b/tests/std/expr/expr.compound/out/unsafe/expr.eq.rs index 1b9bc0b6b0..c388d066f0 100644 --- a/tests/std/expr/expr.compound/out/unsafe/expr.eq.rs +++ b/tests/std/expr/expr.compound/out/unsafe/expr.eq.rs @@ -7,7 +7,7 @@ use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] pub struct p4_1_both_null_HasTwoInts { pub first: i32, pub second: i32, @@ -19,7 +19,7 @@ pub unsafe fn test_0() { assert!(!((first_null) != (second_null))); } #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] pub struct p4_2_one_null_HasTwoInts { pub first: i32, pub second: i32, @@ -33,7 +33,7 @@ pub unsafe fn test_1() { assert!(!((null_pm) == (first_pm))); } #[repr(C)] -#[derive(Copy, Clone, FnPtrArg, VaArg)] +#[derive(Copy, Clone, FnPtrArg, VaArg, ZeroInitializationUnsafe)] pub union p4_5_members_of_same_union_HasTwoIntMembers { pub first: i32, pub second: i32, @@ -50,7 +50,7 @@ pub unsafe fn test_2() { assert!(!((first_pm) != (second_pm))); } #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] pub struct p4_6_data_members_HasTwoInts { pub first: i32, pub second: i32, diff --git a/tests/std/expr/expr.compound/out/unsafe/expr.mptr.oper-p6-null-pointer-to-data-member.rs b/tests/std/expr/expr.compound/out/unsafe/expr.mptr.oper-p6-null-pointer-to-data-member.rs index 6912ca940a..7f8464f86b 100644 --- a/tests/std/expr/expr.compound/out/unsafe/expr.mptr.oper-p6-null-pointer-to-data-member.rs +++ b/tests/std/expr/expr.compound/out/unsafe/expr.mptr.oper-p6-null-pointer-to-data-member.rs @@ -7,7 +7,7 @@ use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] pub struct HasTwoInts { pub first: i32, pub second: i32, diff --git a/tests/std/expr/expr.compound/out/unsafe/expr.mptr.oper.rs b/tests/std/expr/expr.compound/out/unsafe/expr.mptr.oper.rs index 9630283178..2a1e254a13 100644 --- a/tests/std/expr/expr.compound/out/unsafe/expr.mptr.oper.rs +++ b/tests/std/expr/expr.compound/out/unsafe/expr.mptr.oper.rs @@ -7,7 +7,7 @@ use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] pub struct p3_arrow_star_HasTwoInts { pub first: i32, pub second: i32, @@ -31,7 +31,7 @@ pub unsafe fn test_0() { ); } #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] pub struct p4_object_expression_sequenced_first_HasValue { pub value: i32, } @@ -65,7 +65,7 @@ pub unsafe fn test_7() { assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut member_stamp_3)) == (2))); } #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] pub struct p6_data_member_of_lvalue_is_lvalue_HasTwoInts { pub first: i32, pub second: i32, @@ -94,46 +94,50 @@ pub unsafe fn test_8() { pub static mut copyCount_9: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); pub static mut moveCount_10: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); #[repr(C)] -#[derive(VaArg, FnPtrArg, MoveCtorUnsafe, Default)] +#[derive(VaArg, FnPtrArg, ZeroInitializationUnsafe, MoveCtorUnsafe, Default)] pub struct p6_data_member_of_rvalue_is_xvalue_CountsCopiesAndMoves {} impl p6_data_member_of_rvalue_is_xvalue_CountsCopiesAndMoves { pub unsafe fn copy_from( + &mut self, _a0: *const p6_data_member_of_rvalue_is_xvalue_CountsCopiesAndMoves, - ) -> Self { - let mut this = Self {}; + ) { (*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_9)).prefix_inc(); - this } pub unsafe fn move_from( + &mut self, _a0: *mut p6_data_member_of_rvalue_is_xvalue_CountsCopiesAndMoves, - ) -> Self { - let mut this = Self {}; + ) { (*std::cell::LazyCell::force_mut(&mut *&raw mut moveCount_10)).prefix_inc(); - this } } impl Clone for p6_data_member_of_rvalue_is_xvalue_CountsCopiesAndMoves { fn clone(&self) -> Self { unsafe { + let mut __v: p6_data_member_of_rvalue_is_xvalue_CountsCopiesAndMoves = + ::zero_init(); p6_data_member_of_rvalue_is_xvalue_CountsCopiesAndMoves::copy_from( + &mut __v, self as *const p6_data_member_of_rvalue_is_xvalue_CountsCopiesAndMoves, - ) + ); + __v } } } #[repr(C)] -#[derive(Clone, VaArg, FnPtrArg, MoveCtorUnsafe, Default)] +#[derive(Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, MoveCtorUnsafe, Default)] pub struct p6_data_member_of_rvalue_is_xvalue_HoldsCounted { pub member: p6_data_member_of_rvalue_is_xvalue_CountsCopiesAndMoves, } impl p6_data_member_of_rvalue_is_xvalue_HoldsCounted { - pub unsafe fn move_from(_a0: *mut p6_data_member_of_rvalue_is_xvalue_HoldsCounted) -> Self { - let mut this = Self { - member: p6_data_member_of_rvalue_is_xvalue_CountsCopiesAndMoves::move_from({ + pub unsafe fn move_from(&mut self, _a0: *mut p6_data_member_of_rvalue_is_xvalue_HoldsCounted) { + (&raw mut self.member).write({ + let mut __v: p6_data_member_of_rvalue_is_xvalue_CountsCopiesAndMoves = + ::zero_init(); + p6_data_member_of_rvalue_is_xvalue_CountsCopiesAndMoves::move_from(&mut __v, { &mut (*_a0).member - }), - }; - this + }); + __v + }); } } pub unsafe fn test_11() { @@ -141,20 +145,28 @@ pub unsafe fn test_11() { (::std::mem::offset_of!(p6_data_member_of_rvalue_is_xvalue_HoldsCounted, member) as isize); let mut holder: p6_data_member_of_rvalue_is_xvalue_HoldsCounted = ::default(); - let mut from_lvalue: p6_data_member_of_rvalue_is_xvalue_CountsCopiesAndMoves = - p6_data_member_of_rvalue_is_xvalue_CountsCopiesAndMoves::copy_from({ + let mut from_lvalue: p6_data_member_of_rvalue_is_xvalue_CountsCopiesAndMoves = { + let mut __v: p6_data_member_of_rvalue_is_xvalue_CountsCopiesAndMoves = + ::zero_init(); + p6_data_member_of_rvalue_is_xvalue_CountsCopiesAndMoves::copy_from(&mut __v, { &(*(&mut holder as *mut p6_data_member_of_rvalue_is_xvalue_HoldsCounted) .wrapping_byte_offset(pm) .cast::()) }); + __v + }; assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_9)) == (1))); assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut moveCount_10)) == (0))); - let mut from_xvalue: p6_data_member_of_rvalue_is_xvalue_CountsCopiesAndMoves = - p6_data_member_of_rvalue_is_xvalue_CountsCopiesAndMoves::move_from({ + let mut from_xvalue: p6_data_member_of_rvalue_is_xvalue_CountsCopiesAndMoves = { + let mut __v: p6_data_member_of_rvalue_is_xvalue_CountsCopiesAndMoves = + ::zero_init(); + p6_data_member_of_rvalue_is_xvalue_CountsCopiesAndMoves::move_from(&mut __v, { &mut (*(&mut holder as *mut p6_data_member_of_rvalue_is_xvalue_HoldsCounted) .wrapping_byte_offset(pm) .cast::()) }); + __v + }; assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_9)) == (1))); assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut moveCount_10)) == (1))); } diff --git a/tests/std/expr/expr.prim/expr.prim.this.cpp b/tests/std/expr/expr.prim/expr.prim.this.cpp new file mode 100644 index 0000000000..566c58b062 --- /dev/null +++ b/tests/std/expr/expr.prim/expr.prim.this.cpp @@ -0,0 +1,45 @@ +// ADDITIONAL_COMPILE_FLAGS: -std=c++23 +#include + +// N4950 [expr.prim.this]/1: in a default member initializer, this names the +// object whose member is initialized. +namespace p1_default_member_initializer_names_object { +struct Holder { + Holder(int value) : value(value) { + assert(self == this); + assert(self->value == value); + } + Holder *self = this; + int value; +}; + +void test() { + Holder holder(4); + assert(holder.value == 4); +} +} // namespace p1_default_member_initializer_names_object + +// N4950 [expr.prim.this]/1: in a mem-initializer, this names the object being +// constructed. +namespace p1_mem_initializer_names_object { +struct Holder { + Holder(int value) : self(this), value(value) { + assert(self == this); + self->value += 1; + assert(this->value == value + 1); + } + Holder *self; + int value; +}; + +void test() { + Holder holder(4); + assert(holder.value == 5); +} +} // namespace p1_mem_initializer_names_object + +int main() { + p1_default_member_initializer_names_object::test(); + p1_mem_initializer_names_object::test(); + return 0; +} diff --git a/tests/std/expr/expr.prim/out/refcount/expr.prim.this.rs b/tests/std/expr/expr.prim/out/refcount/expr.prim.this.rs new file mode 100644 index 0000000000..27248df28a --- /dev/null +++ b/tests/std/expr/expr.prim/out/refcount/expr.prim.this.rs @@ -0,0 +1,90 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, CppFrom)] +#[cpp_from(i32, construct, value)] +#[byte_size(16)] +pub struct p1_default_member_initializer_names_object_Holder { + #[offset(0)] + #[byte_size(8)] + pub self_: Ptr, + #[offset(8)] + pub value: i32, +} +impl Default for p1_default_member_initializer_names_object_Holder { + fn default() -> Self { + { ::zero_init() } + } +} +pub fn test_0() { + let mut holder: p1_default_member_initializer_names_object_Holder = { + let __v: Value = Rc::new(RefCell::new( + ::zero_init(), + )); + p1_default_member_initializer_names_object_HolderImpl::construct(&__v.as_pointer(), { 4 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }; + assert!((holder.value == 4)); +} +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, CppFrom, Default)] +#[cpp_from(i32, construct, value)] +#[byte_size(16)] +pub struct p1_mem_initializer_names_object_Holder { + #[offset(0)] + #[byte_size(8)] + pub self_: Ptr, + #[offset(8)] + pub value: i32, +} +pub fn test_1() { + let mut holder: p1_mem_initializer_names_object_Holder = { + let __v: Value = Rc::new(RefCell::new( + ::zero_init(), + )); + p1_mem_initializer_names_object_HolderImpl::construct(&__v.as_pointer(), { 4 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }; + assert!((holder.value == 5)); +} +pub fn main() { + __cpp2rust_init_globals(); + libcc2rs::cpp2rust_exit(main_0()); +} +fn main_0() -> i32 { + ({ test_0() }); + ({ test_1() }); + return 0; +} +pub trait p1_default_member_initializer_names_object_HolderImpl { + fn construct(&self, value: i32); +} +impl p1_default_member_initializer_names_object_HolderImpl + for Ptr +{ + fn construct(&self, mut value: i32) { + field!((*self), self_).write((*self).clone()); + field!((*self), value).write(value); + assert!(((*self).with(|__s| __s.self_.clone()) == (*self))); + assert!(({ (*self).with(|__s| __s.self_.clone()).with(|__s| __s.value) } == { value })); + } +} +pub trait p1_mem_initializer_names_object_HolderImpl { + fn construct(&self, value: i32); +} +impl p1_mem_initializer_names_object_HolderImpl for Ptr { + fn construct(&self, mut value: i32) { + field!((*self), self_).write((*self).clone()); + field!((*self), value).write(value); + assert!(((*self).with(|__s| __s.self_.clone()) == (*self))); + { + field!((*self).with(|__s| __s.self_.clone()), value).with_mut(|__v| *__v = *__v + 1) + }; + assert!(((*self).with(|__s| __s.value) == (value + 1))); + } +} +pub fn __cpp2rust_init_globals() {} diff --git a/tests/std/expr/expr.prim/out/unsafe/expr.prim.this.rs b/tests/std/expr/expr.prim/out/unsafe/expr.prim.this.rs new file mode 100644 index 0000000000..1091720b88 --- /dev/null +++ b/tests/std/expr/expr.prim/out/unsafe/expr.prim.this.rs @@ -0,0 +1,75 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +#[repr(C)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, CppFromUnsafe)] +#[cpp_from(i32, construct, value)] +pub struct p1_default_member_initializer_names_object_Holder { + pub self_: *mut p1_default_member_initializer_names_object_Holder, + pub value: i32, +} +impl p1_default_member_initializer_names_object_Holder { + pub unsafe fn construct(&mut self, mut value: i32) { + (&raw mut self.self_) + .write((self as *mut p1_default_member_initializer_names_object_Holder)); + (&raw mut self.value).write(value); + assert!(((self.self_) == (self as *mut p1_default_member_initializer_names_object_Holder))); + assert!((((*self.self_).value) == (value))); + } +} +impl Default for p1_default_member_initializer_names_object_Holder { + fn default() -> Self { + unsafe { ::zero_init() } + } +} +pub unsafe fn test_0() { + let mut holder: p1_default_member_initializer_names_object_Holder = { + let mut __v: p1_default_member_initializer_names_object_Holder = + ::zero_init(); + p1_default_member_initializer_names_object_Holder::construct(&mut __v, { 4 }); + __v + }; + assert!(((holder.value) == (4))); +} +#[repr(C)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, CppFromUnsafe, Default)] +#[cpp_from(i32, construct, value)] +pub struct p1_mem_initializer_names_object_Holder { + pub self_: *mut p1_mem_initializer_names_object_Holder, + pub value: i32, +} +impl p1_mem_initializer_names_object_Holder { + pub unsafe fn construct(&mut self, mut value: i32) { + (&raw mut self.self_).write((self as *mut p1_mem_initializer_names_object_Holder)); + (&raw mut self.value).write(value); + assert!(((self.self_) == (self as *mut p1_mem_initializer_names_object_Holder))); + (*self.self_).value += 1; + assert!(((self.value) == ((value) + (1)))); + } +} +pub unsafe fn test_1() { + let mut holder: p1_mem_initializer_names_object_Holder = { + let mut __v: p1_mem_initializer_names_object_Holder = + ::zero_init(); + p1_mem_initializer_names_object_Holder::construct(&mut __v, { 4 }); + __v + }; + assert!(((holder.value) == (5))); +} +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + libcc2rs::cpp2rust_exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + (unsafe { test_0() }); + (unsafe { test_1() }); + return 0; +} +pub unsafe fn __cpp2rust_init_globals() {} diff --git a/tests/std/library/requirements/utility.requirements/allocator.requirements/out/refcount/allocator.requirements.general.rs b/tests/std/library/requirements/utility.requirements/allocator.requirements/out/refcount/allocator.requirements.general.rs index bf8e8701f0..25d3d46062 100644 --- a/tests/std/library/requirements/utility.requirements/allocator.requirements/out/refcount/allocator.requirements.general.rs +++ b/tests/std/library/requirements/utility.requirements/allocator.requirements/out/refcount/allocator.requirements.general.rs @@ -6,10 +6,10 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(1)] pub struct p16_rebind_member_Alloc_std_pair_const_int__int___rebind_std_pair_const_int__int__ {} -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg)] #[byte_size(4)] pub struct p16_rebind_member_Alloc_std_pair_const_int__int__ { #[offset(0)] @@ -43,7 +43,7 @@ pub fn test_0() { ); assert!(({ ((*n.borrow()).len() == 1_usize) })); } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(1)] pub struct p18_rebind_to_container_node_Alloc_std_pair_const_int__int__ {} pub fn test_1() { diff --git a/tests/std/library/requirements/utility.requirements/allocator.requirements/out/unsafe/allocator.requirements.general.rs b/tests/std/library/requirements/utility.requirements/allocator.requirements/out/unsafe/allocator.requirements.general.rs index 15e3c9030d..b3c39ca5b6 100644 --- a/tests/std/library/requirements/utility.requirements/allocator.requirements/out/unsafe/allocator.requirements.general.rs +++ b/tests/std/library/requirements/utility.requirements/allocator.requirements/out/unsafe/allocator.requirements.general.rs @@ -7,10 +7,10 @@ use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] pub struct p16_rebind_member_Alloc_std_pair_const_int__int___rebind_std_pair_const_int__int__ {} #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe)] pub struct p16_rebind_member_Alloc_std_pair_const_int__int__ { pub id: i32, } @@ -30,7 +30,7 @@ pub unsafe fn test_0() { assert!(((n.len()) == (1_usize))); } #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] pub struct p18_rebind_to_container_node_Alloc_std_pair_const_int__int__ {} pub unsafe fn test_1() { let mut m: UnsafeMap = UnsafeMap::new(); diff --git a/tests/std/stmt.stmt/stmt.iter/out/refcount/stmt.while.rs b/tests/std/stmt.stmt/stmt.iter/out/refcount/stmt.while.rs index 39f58b21c6..32eb159e55 100644 --- a/tests/std/stmt.stmt/stmt.iter/out/refcount/stmt.while.rs +++ b/tests/std/stmt.stmt/stmt.iter/out/refcount/stmt.while.rs @@ -12,29 +12,31 @@ thread_local!( thread_local!( pub static dtorCount_1: Value = Rc::new(RefCell::new(0)); ); -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, CppFrom, Destructible, Default)] -#[cpp_from(i32, new, value)] +#[derive( + Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, CppFrom, Destructible, Default, +)] +#[cpp_from(i32, construct, value)] #[byte_size(4)] pub struct p2_condition_variable_created_each_iteration_ConvertsToBool { #[offset(0)] pub value: i32, } -impl p2_condition_variable_created_each_iteration_ConvertsToBool { - pub fn new(mut value: i32) -> Self { - let __this: p2_condition_variable_created_each_iteration_ConvertsToBool = - Self { value: value }; - (*ctorCount_0.with(Value::clone).borrow_mut()).prefix_inc(); - __this - } -} pub fn test_2() { let mut remaining: i32 = 2; let mut iterations: i32 = 0; 'loop_: loop { let condition: Value = - Rc::new(RefCell::new( - p2_condition_variable_created_each_iteration_ConvertsToBool::new({ remaining }), - )); + Rc::new(RefCell::new({ + let __v: Value = + Rc::new(RefCell::new( + ::zero_init(), + )); + p2_condition_variable_created_each_iteration_ConvertsToBoolImpl::construct( + &__v.as_pointer(), + { remaining }, + ); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); let _dtor_condition = std::mem::DropGuard::new(condition.clone(), |__p| { Destructible::destroy(__p.as_pointer()) }); @@ -61,29 +63,32 @@ thread_local!( thread_local!( pub static dtorCount_4: Value = Rc::new(RefCell::new(0)); ); -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, CppFrom, Destructible, Default)] -#[cpp_from(i32, new, value)] +#[derive( + Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, CppFrom, Destructible, Default, +)] +#[cpp_from(i32, construct, value)] #[byte_size(4)] pub struct p2_continue_destroys_condition_variable_ConvertsToBool { #[offset(0)] pub value: i32, } -impl p2_continue_destroys_condition_variable_ConvertsToBool { - pub fn new(mut value: i32) -> Self { - let __this: p2_continue_destroys_condition_variable_ConvertsToBool = Self { value: value }; - (*ctorCount_3.with(Value::clone).borrow_mut()).prefix_inc(); - __this - } -} pub fn test_5() { let mut remaining: i32 = 3; let mut continued: i32 = 0; let mut completed: i32 = 0; 'loop_: loop { let condition: Value = - Rc::new(RefCell::new( - p2_continue_destroys_condition_variable_ConvertsToBool::new({ remaining }), - )); + Rc::new(RefCell::new({ + let __v: Value = + Rc::new(RefCell::new( + ::zero_init(), + )); + p2_continue_destroys_condition_variable_ConvertsToBoolImpl::construct( + &__v.as_pointer(), + { remaining }, + ); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); let _dtor_condition = std::mem::DropGuard::new(condition.clone(), |__p| { Destructible::destroy(__p.as_pointer()) }); @@ -120,12 +125,17 @@ fn main_0() -> i32 { return 0; } pub trait p2_condition_variable_created_each_iteration_ConvertsToBoolImpl { + fn construct(&self, value: i32); fn destructor(&self); fn to_bool(&self) -> bool; } impl p2_condition_variable_created_each_iteration_ConvertsToBoolImpl for Ptr { + fn construct(&self, mut value: i32) { + field!((*self), value).write(value); + (*ctorCount_0.with(Value::clone).borrow_mut()).prefix_inc(); + } fn destructor(&self) { (*dtorCount_1.with(Value::clone).borrow_mut()).prefix_inc(); } @@ -134,12 +144,17 @@ impl p2_condition_variable_created_each_iteration_ConvertsToBoolImpl } } pub trait p2_continue_destroys_condition_variable_ConvertsToBoolImpl { + fn construct(&self, value: i32); fn destructor(&self); fn to_bool(&self) -> bool; } impl p2_continue_destroys_condition_variable_ConvertsToBoolImpl for Ptr { + fn construct(&self, mut value: i32) { + field!((*self), value).write(value); + (*ctorCount_3.with(Value::clone).borrow_mut()).prefix_inc(); + } fn destructor(&self) { (*dtorCount_4.with(Value::clone).borrow_mut()).prefix_inc(); } diff --git a/tests/std/stmt.stmt/stmt.iter/out/unsafe/stmt.while.rs b/tests/std/stmt.stmt/stmt.iter/out/unsafe/stmt.while.rs index 2ffbe798dc..57d7121ea3 100644 --- a/tests/std/stmt.stmt/stmt.iter/out/unsafe/stmt.while.rs +++ b/tests/std/stmt.stmt/stmt.iter/out/unsafe/stmt.while.rs @@ -9,16 +9,17 @@ use std::rc::Rc; pub static mut ctorCount_0: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); pub static mut dtorCount_1: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); #[repr(C)] -#[derive(Clone, VaArg, FnPtrArg, CppFromUnsafe, DestructibleUnsafe, Default)] -#[cpp_from(i32, new, value)] +#[derive( + Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, CppFromUnsafe, DestructibleUnsafe, Default, +)] +#[cpp_from(i32, construct, value)] pub struct p2_condition_variable_created_each_iteration_ConvertsToBool { pub value: i32, } impl p2_condition_variable_created_each_iteration_ConvertsToBool { - pub unsafe fn new(mut value: i32) -> Self { - let mut this = Self { value: value }; + pub unsafe fn construct(&mut self, mut value: i32) { + (&raw mut self.value).write(value); (*std::cell::LazyCell::force_mut(&mut *&raw mut ctorCount_0)).prefix_inc(); - this } pub unsafe fn destructor(&mut self) { (*std::cell::LazyCell::force_mut(&mut *&raw mut dtorCount_1)).prefix_inc(); @@ -31,8 +32,14 @@ pub unsafe fn test_2() { let mut remaining: i32 = 2; let mut iterations: i32 = 0; 'loop_: loop { - let mut condition: p2_condition_variable_created_each_iteration_ConvertsToBool = - p2_condition_variable_created_each_iteration_ConvertsToBool::new({ remaining }); + let mut condition: p2_condition_variable_created_each_iteration_ConvertsToBool = { + let mut __v: p2_condition_variable_created_each_iteration_ConvertsToBool = + ::zero_init(); + p2_condition_variable_created_each_iteration_ConvertsToBool::construct(&mut __v, { + remaining + }); + __v + }; let _dtor_condition = std::mem::DropGuard::new(&raw mut condition, |__p| unsafe { DestructibleUnsafe::destroy(__p) }); @@ -56,16 +63,17 @@ pub unsafe fn test_2() { pub static mut ctorCount_3: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); pub static mut dtorCount_4: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); #[repr(C)] -#[derive(Clone, VaArg, FnPtrArg, CppFromUnsafe, DestructibleUnsafe, Default)] -#[cpp_from(i32, new, value)] +#[derive( + Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, CppFromUnsafe, DestructibleUnsafe, Default, +)] +#[cpp_from(i32, construct, value)] pub struct p2_continue_destroys_condition_variable_ConvertsToBool { pub value: i32, } impl p2_continue_destroys_condition_variable_ConvertsToBool { - pub unsafe fn new(mut value: i32) -> Self { - let mut this = Self { value: value }; + pub unsafe fn construct(&mut self, mut value: i32) { + (&raw mut self.value).write(value); (*std::cell::LazyCell::force_mut(&mut *&raw mut ctorCount_3)).prefix_inc(); - this } pub unsafe fn destructor(&mut self) { (*std::cell::LazyCell::force_mut(&mut *&raw mut dtorCount_4)).prefix_inc(); @@ -79,8 +87,14 @@ pub unsafe fn test_5() { let mut continued: i32 = 0; let mut completed: i32 = 0; 'loop_: loop { - let mut condition: p2_continue_destroys_condition_variable_ConvertsToBool = - p2_continue_destroys_condition_variable_ConvertsToBool::new({ remaining }); + let mut condition: p2_continue_destroys_condition_variable_ConvertsToBool = { + let mut __v: p2_continue_destroys_condition_variable_ConvertsToBool = + ::zero_init(); + p2_continue_destroys_condition_variable_ConvertsToBool::construct(&mut __v, { + remaining + }); + __v + }; let _dtor_condition = std::mem::DropGuard::new(&raw mut condition, |__p| unsafe { DestructibleUnsafe::destroy(__p) }); diff --git a/tests/std/stmt.stmt/stmt.select/out/refcount/stmt.if.rs b/tests/std/stmt.stmt/stmt.select/out/refcount/stmt.if.rs index 8bb9fd4116..9beda4a205 100644 --- a/tests/std/stmt.stmt/stmt.select/out/refcount/stmt.if.rs +++ b/tests/std/stmt.stmt/stmt.select/out/refcount/stmt.if.rs @@ -18,7 +18,7 @@ thread_local!( thread_local!( pub static useCount_3: Value = Rc::new(RefCell::new(0)); ); -#[derive(Record, ByteRepr, VaArg, FnPtrArg, CopyAssign, Destructible)] +#[derive(Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, CopyAssign, Destructible)] #[byte_size(4)] pub struct LifetimeTracked { #[offset(0)] @@ -43,28 +43,27 @@ impl LifetimeTracked { } }); } - pub fn new() -> Self { - let __this: LifetimeTracked = Self { uses: 0 }; - (*ctorCount_0.with(Value::clone).borrow_mut()).prefix_inc(); - __this - } - pub fn copy_from(_a0: Ptr) -> Self { - let __this: LifetimeTracked = Self { uses: 0 }; - (*copyCount_1.with(Value::clone).borrow_mut()).prefix_inc(); - __this - } } impl Clone for LifetimeTracked { fn clone(&self) -> Self { let __src: Value = Rc::new(RefCell::new(LifetimeTracked { uses: self.uses.clone(), })); - LifetimeTracked::copy_from(__src.as_pointer()) + let __v: Value = Rc::new(RefCell::new(::zero_init())); + LifetimeTrackedImpl::copy_from(&__v.as_pointer(), __src.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() } } impl Default for LifetimeTracked { fn default() -> Self { - { LifetimeTracked::new() } + { + { + let __v: Value = + Rc::new(RefCell::new(::zero_init())); + LifetimeTrackedImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + } + } } } thread_local!( @@ -77,7 +76,11 @@ pub fn test_6() { ({ LifetimeTracked::reset() }); let mut choice: i32 = 9; { - let guard: Value = Rc::new(RefCell::new(LifetimeTracked::new())); + let guard: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + LifetimeTrackedImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); let _dtor_guard = std::mem::DropGuard::new(guard.clone(), |__p| Destructible::destroy(__p.as_pointer())); let mut choice: i32 = 0; @@ -95,7 +98,11 @@ pub fn test_6() { assert!((else_runs_5.with(|rc| *rc.borrow()) == 1)); assert!((dtorCount_2.with(|rc| *rc.borrow()) == 1)); { - let guard: Value = Rc::new(RefCell::new(LifetimeTracked::new())); + let guard: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + LifetimeTrackedImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); let _dtor_guard = std::mem::DropGuard::new(guard.clone(), |__p| Destructible::destroy(__p.as_pointer())); let mut choice: i32 = 4; @@ -192,11 +199,21 @@ fn main_0() -> i32 { return 0; } pub trait LifetimeTrackedImpl { + fn construct(&self); + fn copy_from(&self, _a0: Ptr); fn destructor(&self); fn use_(&self); fn copy_assign(&self, _a0: Ptr) -> Ptr; } impl LifetimeTrackedImpl for Ptr { + fn construct(&self) { + field!((*self), uses).write(0); + (*ctorCount_0.with(Value::clone).borrow_mut()).prefix_inc(); + } + fn copy_from(&self, _a0: Ptr) { + field!((*self), uses).write(0); + (*copyCount_1.with(Value::clone).borrow_mut()).prefix_inc(); + } fn destructor(&self) { (*dtorCount_2.with(Value::clone).borrow_mut()).prefix_inc(); } diff --git a/tests/std/stmt.stmt/stmt.select/out/refcount/stmt.switch.rs b/tests/std/stmt.stmt/stmt.select/out/refcount/stmt.switch.rs index f58e391087..7fa6a24aba 100644 --- a/tests/std/stmt.stmt/stmt.select/out/refcount/stmt.switch.rs +++ b/tests/std/stmt.stmt/stmt.select/out/refcount/stmt.switch.rs @@ -18,7 +18,7 @@ thread_local!( thread_local!( pub static useCount_3: Value = Rc::new(RefCell::new(0)); ); -#[derive(Record, ByteRepr, VaArg, FnPtrArg, CopyAssign, Destructible)] +#[derive(Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, CopyAssign, Destructible)] #[byte_size(4)] pub struct LifetimeTracked { #[offset(0)] @@ -43,28 +43,27 @@ impl LifetimeTracked { } }); } - pub fn new() -> Self { - let __this: LifetimeTracked = Self { uses: 0 }; - (*ctorCount_0.with(Value::clone).borrow_mut()).prefix_inc(); - __this - } - pub fn copy_from(_a0: Ptr) -> Self { - let __this: LifetimeTracked = Self { uses: 0 }; - (*copyCount_1.with(Value::clone).borrow_mut()).prefix_inc(); - __this - } } impl Clone for LifetimeTracked { fn clone(&self) -> Self { let __src: Value = Rc::new(RefCell::new(LifetimeTracked { uses: self.uses.clone(), })); - LifetimeTracked::copy_from(__src.as_pointer()) + let __v: Value = Rc::new(RefCell::new(::zero_init())); + LifetimeTrackedImpl::copy_from(&__v.as_pointer(), __src.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() } } impl Default for LifetimeTracked { fn default() -> Self { - { LifetimeTracked::new() } + { + { + let __v: Value = + Rc::new(RefCell::new(::zero_init())); + LifetimeTrackedImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + } + } } } thread_local!( @@ -76,7 +75,9 @@ thread_local!( thread_local!( pub static dtorCount_6: Value = Rc::new(RefCell::new(0)); ); -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, CppFrom, Destructible, Default)] +#[derive( + Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, CppFrom, Destructible, Default, +)] #[cpp_into(i32, to_i32)] #[byte_size(4)] pub struct p2_condition_variable_lives_for_whole_statement_ConvertsToInt { @@ -127,7 +128,11 @@ pub fn test_10() { ({ LifetimeTracked::reset() }); let mut selector: i32 = 9; { - let guard: Value = Rc::new(RefCell::new(LifetimeTracked::new())); + let guard: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + LifetimeTrackedImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); let _dtor_guard = std::mem::DropGuard::new(guard.clone(), |__p| Destructible::destroy(__p.as_pointer())); let mut selector: i32 = 2; @@ -201,11 +206,21 @@ fn main_0() -> i32 { return 0; } pub trait LifetimeTrackedImpl { + fn construct(&self); + fn copy_from(&self, _a0: Ptr); fn destructor(&self); fn use_(&self); fn copy_assign(&self, _a0: Ptr) -> Ptr; } impl LifetimeTrackedImpl for Ptr { + fn construct(&self) { + field!((*self), uses).write(0); + (*ctorCount_0.with(Value::clone).borrow_mut()).prefix_inc(); + } + fn copy_from(&self, _a0: Ptr) { + field!((*self), uses).write(0); + (*copyCount_1.with(Value::clone).borrow_mut()).prefix_inc(); + } fn destructor(&self) { (*dtorCount_2.with(Value::clone).borrow_mut()).prefix_inc(); } diff --git a/tests/std/stmt.stmt/stmt.select/out/unsafe/stmt.if.rs b/tests/std/stmt.stmt/stmt.select/out/unsafe/stmt.if.rs index c80037a107..b5b0a0df24 100644 --- a/tests/std/stmt.stmt/stmt.select/out/unsafe/stmt.if.rs +++ b/tests/std/stmt.stmt/stmt.select/out/unsafe/stmt.if.rs @@ -11,7 +11,7 @@ pub static mut copyCount_1: std::cell::LazyCell = std::cell::LazyCell::new( pub static mut dtorCount_2: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); pub static mut useCount_3: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); #[repr(C)] -#[derive(VaArg, FnPtrArg, CopyAssignUnsafe, DestructibleUnsafe)] +#[derive(VaArg, FnPtrArg, ZeroInitializationUnsafe, CopyAssignUnsafe, DestructibleUnsafe)] pub struct LifetimeTracked { pub uses: i32, } @@ -28,15 +28,13 @@ impl LifetimeTracked { (*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_1)) }; } - pub unsafe fn new() -> Self { - let mut this = Self { uses: 0 }; + pub unsafe fn construct(&mut self) { + (&raw mut self.uses).write(0); (*std::cell::LazyCell::force_mut(&mut *&raw mut ctorCount_0)).prefix_inc(); - this } - pub unsafe fn copy_from(_a0: *const LifetimeTracked) -> Self { - let mut this = Self { uses: 0 }; + pub unsafe fn copy_from(&mut self, _a0: *const LifetimeTracked) { + (&raw mut self.uses).write(0); (*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_1)).prefix_inc(); - this } pub unsafe fn destructor(&mut self) { (*std::cell::LazyCell::force_mut(&mut *&raw mut dtorCount_2)).prefix_inc(); @@ -52,12 +50,22 @@ impl LifetimeTracked { } impl Clone for LifetimeTracked { fn clone(&self) -> Self { - unsafe { LifetimeTracked::copy_from(self as *const LifetimeTracked) } + unsafe { + let mut __v: LifetimeTracked = ::zero_init(); + LifetimeTracked::copy_from(&mut __v, self as *const LifetimeTracked); + __v + } } } impl Default for LifetimeTracked { fn default() -> Self { - unsafe { LifetimeTracked::new() } + unsafe { + { + let mut __v: LifetimeTracked = ::zero_init(); + LifetimeTracked::construct(&mut __v); + __v + } + } } } pub static mut then_runs_4: std::cell::LazyCell = @@ -68,7 +76,11 @@ pub unsafe fn test_6() { (unsafe { LifetimeTracked::reset() }); let mut choice: i32 = 9; { - let mut guard: LifetimeTracked = LifetimeTracked::new(); + let mut guard: LifetimeTracked = { + let mut __v: LifetimeTracked = ::zero_init(); + LifetimeTracked::construct(&mut __v); + __v + }; let _dtor_guard = std::mem::DropGuard::new(&raw mut guard, |__p| unsafe { DestructibleUnsafe::destroy(__p) }); @@ -87,7 +99,11 @@ pub unsafe fn test_6() { assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut else_runs_5)) == (1))); assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut dtorCount_2)) == (1))); { - let mut guard: LifetimeTracked = LifetimeTracked::new(); + let mut guard: LifetimeTracked = { + let mut __v: LifetimeTracked = ::zero_init(); + LifetimeTracked::construct(&mut __v); + __v + }; let _dtor_guard = std::mem::DropGuard::new(&raw mut guard, |__p| unsafe { DestructibleUnsafe::destroy(__p) }); diff --git a/tests/std/stmt.stmt/stmt.select/out/unsafe/stmt.switch.rs b/tests/std/stmt.stmt/stmt.select/out/unsafe/stmt.switch.rs index 7fb1b86497..4455608afe 100644 --- a/tests/std/stmt.stmt/stmt.select/out/unsafe/stmt.switch.rs +++ b/tests/std/stmt.stmt/stmt.select/out/unsafe/stmt.switch.rs @@ -11,7 +11,7 @@ pub static mut copyCount_1: std::cell::LazyCell = std::cell::LazyCell::new( pub static mut dtorCount_2: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); pub static mut useCount_3: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); #[repr(C)] -#[derive(VaArg, FnPtrArg, CopyAssignUnsafe, DestructibleUnsafe)] +#[derive(VaArg, FnPtrArg, ZeroInitializationUnsafe, CopyAssignUnsafe, DestructibleUnsafe)] pub struct LifetimeTracked { pub uses: i32, } @@ -28,15 +28,13 @@ impl LifetimeTracked { (*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_1)) }; } - pub unsafe fn new() -> Self { - let mut this = Self { uses: 0 }; + pub unsafe fn construct(&mut self) { + (&raw mut self.uses).write(0); (*std::cell::LazyCell::force_mut(&mut *&raw mut ctorCount_0)).prefix_inc(); - this } - pub unsafe fn copy_from(_a0: *const LifetimeTracked) -> Self { - let mut this = Self { uses: 0 }; + pub unsafe fn copy_from(&mut self, _a0: *const LifetimeTracked) { + (&raw mut self.uses).write(0); (*std::cell::LazyCell::force_mut(&mut *&raw mut copyCount_1)).prefix_inc(); - this } pub unsafe fn destructor(&mut self) { (*std::cell::LazyCell::force_mut(&mut *&raw mut dtorCount_2)).prefix_inc(); @@ -52,12 +50,22 @@ impl LifetimeTracked { } impl Clone for LifetimeTracked { fn clone(&self) -> Self { - unsafe { LifetimeTracked::copy_from(self as *const LifetimeTracked) } + unsafe { + let mut __v: LifetimeTracked = ::zero_init(); + LifetimeTracked::copy_from(&mut __v, self as *const LifetimeTracked); + __v + } } } impl Default for LifetimeTracked { fn default() -> Self { - unsafe { LifetimeTracked::new() } + unsafe { + { + let mut __v: LifetimeTracked = ::zero_init(); + LifetimeTracked::construct(&mut __v); + __v + } + } } } pub static mut matching_case_runs_4: std::cell::LazyCell = @@ -66,7 +74,9 @@ pub static mut other_case_runs_5: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0_i32 }); pub static mut dtorCount_6: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); #[repr(C)] -#[derive(Clone, VaArg, FnPtrArg, CppFromUnsafe, DestructibleUnsafe, Default)] +#[derive( + Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, CppFromUnsafe, DestructibleUnsafe, Default, +)] #[cpp_into(i32, to_i32)] pub struct p2_condition_variable_lives_for_whole_statement_ConvertsToInt { pub value: i32, @@ -119,7 +129,11 @@ pub unsafe fn test_10() { (unsafe { LifetimeTracked::reset() }); let mut selector: i32 = 9; { - let mut guard: LifetimeTracked = LifetimeTracked::new(); + let mut guard: LifetimeTracked = { + let mut __v: LifetimeTracked = ::zero_init(); + LifetimeTracked::construct(&mut __v); + __v + }; let _dtor_guard = std::mem::DropGuard::new(&raw mut guard, |__p| unsafe { DestructibleUnsafe::destroy(__p) }); diff --git a/tests/std/temp/out/refcount/temp.type.rs b/tests/std/temp/out/refcount/temp.type.rs index b693e8574d..ea929ef43c 100644 --- a/tests/std/temp/out/refcount/temp.type.rs +++ b/tests/std/temp/out/refcount/temp.type.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct p2_6_pointer_to_member_argument_HasTwoInts { #[offset(0)] @@ -17,21 +17,21 @@ pub struct p2_6_pointer_to_member_argument_HasTwoInts { thread_local!( pub static count_0: Value = Rc::new(RefCell::new(0)); ); -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(1)] pub struct p2_6_pointer_to_member_argument_CountsPerMember_refp2_6_pointer_to_member_argument_HasTwoInts_first_ {} thread_local!( pub static count_1: Value = Rc::new(RefCell::new(0)); ); -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(1)] pub struct p2_6_pointer_to_member_argument_CountsPerMember_refp2_6_pointer_to_member_argument_HasTwoInts_second_ {} thread_local!( pub static count_2: Value = Rc::new(RefCell::new(0)); ); -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(1)] pub struct p2_6_pointer_to_member_argument_CountsPerMember_nullptr_ {} pub fn test_3() { diff --git a/tests/std/temp/out/unsafe/temp.type.rs b/tests/std/temp/out/unsafe/temp.type.rs index db0bfc95d4..8b23294894 100644 --- a/tests/std/temp/out/unsafe/temp.type.rs +++ b/tests/std/temp/out/unsafe/temp.type.rs @@ -7,24 +7,24 @@ use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] pub struct p2_6_pointer_to_member_argument_HasTwoInts { pub first: i32, pub second: i32, } pub static mut count_0: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] pub struct p2_6_pointer_to_member_argument_CountsPerMember_refp2_6_pointer_to_member_argument_HasTwoInts_first_ {} pub static mut count_1: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] pub struct p2_6_pointer_to_member_argument_CountsPerMember_refp2_6_pointer_to_member_argument_HasTwoInts_second_ {} pub static mut count_2: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] pub struct p2_6_pointer_to_member_argument_CountsPerMember_nullptr_ {} pub unsafe fn test_3() { (*std::cell::LazyCell::force_mut(&mut *&raw mut count_0)).prefix_inc(); diff --git a/tests/std/temp/temp.spec/out/refcount/temp.inst.rs b/tests/std/temp/temp.spec/out/refcount/temp.inst.rs index f534995783..2eab60289a 100644 --- a/tests/std/temp/temp.spec/out/refcount/temp.inst.rs +++ b/tests/std/temp/temp.spec/out/refcount/temp.inst.rs @@ -6,36 +6,24 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg)] #[byte_size(4)] pub struct p2_reference_does_not_instantiate_Member { #[offset(0)] pub value: i32, } -impl p2_reference_does_not_instantiate_Member { - pub fn new_1(_a0: Ptr) -> Self { - Self { value: 2 } - } -} impl Default for p2_reference_does_not_instantiate_Member { fn default() -> Self { { p2_reference_does_not_instantiate_Member { value: 1 } } } } -#[derive(Record, ByteRepr, FnPtrArg, MoveCtor, Default)] +#[derive(Record, ByteRepr, ZeroInitialization, FnPtrArg, MoveCtor, Default)] #[byte_size(4)] pub struct p2_reference_does_not_instantiate_Holder { #[offset(0)] #[byte_size(4)] pub member: p2_reference_does_not_instantiate_Member, } -impl p2_reference_does_not_instantiate_Holder { - pub fn move_from(_a0: Ptr) -> Self { - Self { - member: { (*_a0.upgrade().deref()).member }, - } - } -} pub fn test_0() { let mut holder: p2_reference_does_not_instantiate_Holder = ::default(); @@ -54,4 +42,24 @@ fn main_0() -> i32 { #[derive(Clone, Copy, Default, ByteRepr, VaArg, FnPtrArg)] #[byte_size(1)] pub struct p2_reference_does_not_instantiate_Wrapper_int_; +pub trait p2_reference_does_not_instantiate_HolderImpl { + fn move_from(&self, _a0: Ptr); +} +impl p2_reference_does_not_instantiate_HolderImpl + for Ptr +{ + fn move_from(&self, _a0: Ptr) { + field!((*self), member).write({ (*_a0.upgrade().deref()).member }); + } +} +pub trait p2_reference_does_not_instantiate_MemberImpl { + fn construct_1(&self, _a0: Ptr); +} +impl p2_reference_does_not_instantiate_MemberImpl + for Ptr +{ + fn construct_1(&self, _a0: Ptr) { + field!((*self), value).write(2); + } +} pub fn __cpp2rust_init_globals() {} diff --git a/tests/std/temp/temp.spec/out/unsafe/temp.inst.rs b/tests/std/temp/temp.spec/out/unsafe/temp.inst.rs index 07ecd3fa3a..34ca7bfd39 100644 --- a/tests/std/temp/temp.spec/out/unsafe/temp.inst.rs +++ b/tests/std/temp/temp.spec/out/unsafe/temp.inst.rs @@ -7,14 +7,16 @@ use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe)] pub struct p2_reference_does_not_instantiate_Member { pub value: i32, } impl p2_reference_does_not_instantiate_Member { - pub unsafe fn new_1(_a0: *const p2_reference_does_not_instantiate_Wrapper_int_) -> Self { - let mut this = Self { value: 2 }; - this + pub unsafe fn construct_1( + &mut self, + _a0: *const p2_reference_does_not_instantiate_Wrapper_int_, + ) { + (&raw mut self.value).write(2); } } impl Default for p2_reference_does_not_instantiate_Member { @@ -23,16 +25,13 @@ impl Default for p2_reference_does_not_instantiate_Member { } } #[repr(C)] -#[derive(FnPtrArg, MoveCtorUnsafe, Default)] +#[derive(FnPtrArg, ZeroInitializationUnsafe, MoveCtorUnsafe, Default)] pub struct p2_reference_does_not_instantiate_Holder { pub member: p2_reference_does_not_instantiate_Member, } impl p2_reference_does_not_instantiate_Holder { - pub unsafe fn move_from(_a0: *mut p2_reference_does_not_instantiate_Holder) -> Self { - let mut this = Self { - member: (*_a0).member, - }; - this + pub unsafe fn move_from(&mut self, _a0: *mut p2_reference_does_not_instantiate_Holder) { + (&raw mut self.member).write((*_a0).member); } } pub unsafe fn test_0() { diff --git a/tests/std/utilities/utility/out/refcount/forward-helpers.rs b/tests/std/utilities/utility/out/refcount/forward-helpers.rs index 8755e9182b..2a9f1c97a5 100644 --- a/tests/std/utilities/utility/out/refcount/forward-helpers.rs +++ b/tests/std/utilities/utility/out/refcount/forward-helpers.rs @@ -14,67 +14,61 @@ pub fn test_0() { lvalue.write(2); assert!(({ ((*value.borrow()) == 2) })); } -#[derive(Record, ByteRepr, VaArg, FnPtrArg, MoveCtor, CppFrom, Default)] -#[cpp_from(i32, new, value)] +#[derive(Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, MoveCtor, CppFrom, Default)] +#[cpp_from(i32, construct, value)] #[byte_size(4)] pub struct p6_forward_like_Movable { #[offset(0)] pub value: i32, } -impl p6_forward_like_Movable { - pub fn new(mut value: i32) -> Self { - Self { value: value } - } - pub fn copy_from(other: Ptr) -> Self { - Self { - value: other.with(|__s| __s.value), - } - } - pub fn move_from(other: Ptr) -> Self { - let __this: p6_forward_like_Movable = Self { - value: other.with(|__s| __s.value), - }; - field!(other, value).write(0); - __this - } -} impl Clone for p6_forward_like_Movable { fn clone(&self) -> Self { let __src: Value = Rc::new(RefCell::new(p6_forward_like_Movable { value: self.value.clone(), })); - p6_forward_like_Movable::copy_from(__src.as_pointer()) + let __v: Value = + Rc::new(RefCell::new(::zero_init())); + p6_forward_like_MovableImpl::copy_from(&__v.as_pointer(), __src.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() } } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, MoveCtor)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, MoveCtor)] #[byte_size(4)] pub struct p6_forward_like_Holder { #[offset(0)] #[byte_size(4)] pub member: p6_forward_like_Movable, } -impl p6_forward_like_Holder { - pub fn move_from(_a0: Ptr) -> Self { - Self { - member: p6_forward_like_Movable::move_from({ field_ptr!(_a0, member) }), - } - } -} impl Default for p6_forward_like_Holder { fn default() -> Self { { p6_forward_like_Holder { - member: p6_forward_like_Movable::new({ 1 }), + member: { + let __v: Value = + Rc::new(RefCell::new(::zero_init())); + p6_forward_like_MovableImpl::construct(&__v.as_pointer(), { 1 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }, } } } } pub fn take_1(self_: Ptr) -> p6_forward_like_Movable { - return p6_forward_like_Movable::copy_from({ field_ptr!(self_, member) }); + return { + let __v: Value = + Rc::new(RefCell::new(::zero_init())); + p6_forward_like_MovableImpl::copy_from(&__v.as_pointer(), { field_ptr!(self_, member) }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }; } pub fn take_2(self_: Ptr) -> p6_forward_like_Movable { - return p6_forward_like_Movable::move_from({ field_ptr!(self_, member) }); + return { + let __v: Value = + Rc::new(RefCell::new(::zero_init())); + p6_forward_like_MovableImpl::move_from(&__v.as_pointer(), { field_ptr!(self_, member) }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }; } pub fn test_3() { let holder: Value = @@ -84,25 +78,13 @@ pub fn test_3() { let mut moved: p6_forward_like_Movable = ({ take_2(holder.as_pointer()) }); assert!((moved.value == 1) && ({ (*holder.borrow()).member.value } == 0)); } -#[derive(Record, ByteRepr, FnPtrArg, MoveCtor, CppFrom, Default)] -#[cpp_from(i32, new, value)] +#[derive(Record, ByteRepr, ZeroInitialization, FnPtrArg, MoveCtor, CppFrom, Default)] +#[cpp_from(i32, construct, value)] #[byte_size(4)] pub struct p9_move_Movable { #[offset(0)] pub value: i32, } -impl p9_move_Movable { - pub fn new(mut value: i32) -> Self { - Self { value: value } - } - pub fn move_from(other: Ptr) -> Self { - let __this: p9_move_Movable = Self { - value: other.with(|__s| __s.value), - }; - field!(other, value).write(0); - __this - } -} pub fn test_4() { let mut number: i32 = 1; let mut moved: i32 = number; @@ -114,8 +96,16 @@ pub fn test_4() { ({ (moved_pointer).clone() } == { (target.as_pointer()) }) && ({ (pointer).clone() } == { (target.as_pointer()) }) ); - let source: Value = Rc::new(RefCell::new(p9_move_Movable::new({ 3 }))); - let mut destination: p9_move_Movable = p9_move_Movable::move_from({ source.as_pointer() }); + let source: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + p9_move_MovableImpl::construct(&__v.as_pointer(), { 3 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); + let mut destination: p9_move_Movable = { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + p9_move_MovableImpl::move_from(&__v.as_pointer(), { source.as_pointer() }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }; assert!((destination.value == 3) && ({ (*source.borrow()).value } == 0)); let strings: Value>> = Rc::new(RefCell::new(Vec::new())); let mut text: Vec = { @@ -139,8 +129,8 @@ pub fn test_4() { }) ); } -#[derive(Record, ByteRepr, VaArg, FnPtrArg, MoveCtor, CppFrom)] -#[cpp_from(i32, new, value)] +#[derive(Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, MoveCtor, CppFrom)] +#[cpp_from(i32, construct, value)] #[byte_size(12)] pub struct p10_forward_in_template_A { #[offset(0)] @@ -150,31 +140,6 @@ pub struct p10_forward_in_template_A { #[offset(8)] pub moves: i32, } -impl p10_forward_in_template_A { - pub fn new(mut value: i32) -> Self { - Self { - value: value, - copies: 0, - moves: 0, - } - } - pub fn copy_from(other: Ptr) -> Self { - Self { - value: other.with(|__s| __s.value), - copies: (other.with(|__s| __s.copies) + 1), - moves: 0, - } - } - pub fn move_from(other: Ptr) -> Self { - let __this: p10_forward_in_template_A = Self { - value: other.with(|__s| __s.value), - copies: 0, - moves: (other.with(|__s| __s.moves) + 1), - }; - field!(other, value).write(0); - __this - } -} impl Clone for p10_forward_in_template_A { fn clone(&self) -> Self { let __src: Value = @@ -183,29 +148,40 @@ impl Clone for p10_forward_in_template_A { copies: self.copies.clone(), moves: self.moves.clone(), })); - p10_forward_in_template_A::copy_from(__src.as_pointer()) + let __v: Value = + Rc::new(RefCell::new(::zero_init())); + p10_forward_in_template_AImpl::copy_from(&__v.as_pointer(), __src.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() } } impl Default for p10_forward_in_template_A { fn default() -> Self { - { - p10_forward_in_template_A { - value: 0_i32, - copies: 0, - moves: 0, - } - } + { ::zero_init() } } } pub fn make_5(source: Ptr) -> p10_forward_in_template_A { - return p10_forward_in_template_A::copy_from({ (source).clone() }); + return { + let __v: Value = + Rc::new(RefCell::new(::zero_init())); + p10_forward_in_template_AImpl::copy_from(&__v.as_pointer(), { (source).clone() }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }; } pub fn make_6(source: Ptr) -> p10_forward_in_template_A { - return p10_forward_in_template_A::move_from({ (source).clone() }); + return { + let __v: Value = + Rc::new(RefCell::new(::zero_init())); + p10_forward_in_template_AImpl::move_from(&__v.as_pointer(), { (source).clone() }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }; } pub fn test_7() { - let a: Value = - Rc::new(RefCell::new(p10_forward_in_template_A::new({ 1 }))); + let a: Value = Rc::new(RefCell::new({ + let __v: Value = + Rc::new(RefCell::new(::zero_init())); + p10_forward_in_template_AImpl::construct(&__v.as_pointer(), { 1 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); let mut copied: p10_forward_in_template_A = ({ make_5(a.as_pointer()) }); assert!(((copied.value == 1) && (copied.copies == 1)) && (copied.moves == 0)); assert!(({ (*a.borrow()).value } == 1)); @@ -213,82 +189,80 @@ pub fn test_7() { assert!(((moved.value == 1) && (moved.copies == 0)) && (moved.moves == 1)); assert!(({ (*a.borrow()).value } == 0)); } -#[derive(Record, ByteRepr, VaArg, FnPtrArg, MoveCtor, CppFrom, Default)] -#[cpp_from(i32, new, value)] +#[derive(Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, MoveCtor, CppFrom, Default)] +#[cpp_from(i32, construct, value)] #[byte_size(4)] pub struct p11_move_if_noexcept_NoexceptMove { #[offset(0)] pub value: i32, } -impl p11_move_if_noexcept_NoexceptMove { - pub fn new(mut value: i32) -> Self { - Self { value: value } - } - pub fn copy_from(other: Ptr) -> Self { - Self { - value: other.with(|__s| __s.value), - } - } - pub fn move_from(other: Ptr) -> Self { - let __this: p11_move_if_noexcept_NoexceptMove = Self { - value: other.with(|__s| __s.value), - }; - field!(other, value).write(0); - __this - } -} impl Clone for p11_move_if_noexcept_NoexceptMove { fn clone(&self) -> Self { let __src: Value = Rc::new(RefCell::new(p11_move_if_noexcept_NoexceptMove { value: self.value.clone(), })); - p11_move_if_noexcept_NoexceptMove::copy_from(__src.as_pointer()) + let __v: Value = Rc::new(RefCell::new( + ::zero_init(), + )); + p11_move_if_noexcept_NoexceptMoveImpl::copy_from(&__v.as_pointer(), __src.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() } } -#[derive(Record, ByteRepr, VaArg, FnPtrArg, MoveCtor, CppFrom, Default)] -#[cpp_from(i32, new, value)] +#[derive(Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, MoveCtor, CppFrom, Default)] +#[cpp_from(i32, construct, value)] #[byte_size(4)] pub struct p11_move_if_noexcept_ThrowingMove { #[offset(0)] pub value: i32, } -impl p11_move_if_noexcept_ThrowingMove { - pub fn new(mut value: i32) -> Self { - Self { value: value } - } - pub fn copy_from(other: Ptr) -> Self { - Self { - value: other.with(|__s| __s.value), - } - } - pub fn move_from(other: Ptr) -> Self { - let __this: p11_move_if_noexcept_ThrowingMove = Self { - value: other.with(|__s| __s.value), - }; - field!(other, value).write(0); - __this - } -} impl Clone for p11_move_if_noexcept_ThrowingMove { fn clone(&self) -> Self { let __src: Value = Rc::new(RefCell::new(p11_move_if_noexcept_ThrowingMove { value: self.value.clone(), })); - p11_move_if_noexcept_ThrowingMove::copy_from(__src.as_pointer()) + let __v: Value = Rc::new(RefCell::new( + ::zero_init(), + )); + p11_move_if_noexcept_ThrowingMoveImpl::copy_from(&__v.as_pointer(), __src.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() } } pub fn test_8() { - let moved_from: Value = - Rc::new(RefCell::new(p11_move_if_noexcept_NoexceptMove::new({ 1 }))); - let mut moved: p11_move_if_noexcept_NoexceptMove = - p11_move_if_noexcept_NoexceptMove::move_from({ moved_from.as_pointer() }); + let moved_from: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new( + ::zero_init(), + )); + p11_move_if_noexcept_NoexceptMoveImpl::construct(&__v.as_pointer(), { 1 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); + let mut moved: p11_move_if_noexcept_NoexceptMove = { + let __v: Value = Rc::new(RefCell::new( + ::zero_init(), + )); + p11_move_if_noexcept_NoexceptMoveImpl::move_from(&__v.as_pointer(), { + moved_from.as_pointer() + }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }; assert!((moved.value == 1) && ({ (*moved_from.borrow()).value } == 0)); - let copied_from: Value = - Rc::new(RefCell::new(p11_move_if_noexcept_ThrowingMove::new({ 2 }))); - let mut copied: p11_move_if_noexcept_ThrowingMove = - p11_move_if_noexcept_ThrowingMove::copy_from({ copied_from.as_pointer() }); + let copied_from: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new( + ::zero_init(), + )); + p11_move_if_noexcept_ThrowingMoveImpl::construct(&__v.as_pointer(), { 2 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); + let mut copied: p11_move_if_noexcept_ThrowingMove = { + let __v: Value = Rc::new(RefCell::new( + ::zero_init(), + )); + p11_move_if_noexcept_ThrowingMoveImpl::copy_from(&__v.as_pointer(), { + copied_from.as_pointer() + }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }; assert!((copied.value == 2) && ({ (*copied_from.borrow()).value } == 2)); } pub fn main() { @@ -303,4 +277,104 @@ fn main_0() -> i32 { ({ test_8() }); return 0; } +pub trait p10_forward_in_template_AImpl { + fn construct(&self, value: i32); + fn copy_from(&self, other: Ptr); + fn move_from(&self, other: Ptr); +} +impl p10_forward_in_template_AImpl for Ptr { + fn construct(&self, mut value: i32) { + field!((*self), value).write(value); + field!((*self), copies).write(0); + field!((*self), moves).write(0); + } + fn copy_from(&self, other: Ptr) { + field!((*self), value).write(other.with(|__s| __s.value)); + field!((*self), copies).write((other.with(|__s| __s.copies) + 1)); + field!((*self), moves).write(0); + } + fn move_from(&self, other: Ptr) { + field!((*self), value).write(other.with(|__s| __s.value)); + field!((*self), copies).write(0); + field!((*self), moves).write((other.with(|__s| __s.moves) + 1)); + field!(other, value).write(0); + } +} +pub trait p11_move_if_noexcept_NoexceptMoveImpl { + fn construct(&self, value: i32); + fn copy_from(&self, other: Ptr); + fn move_from(&self, other: Ptr); +} +impl p11_move_if_noexcept_NoexceptMoveImpl for Ptr { + fn construct(&self, mut value: i32) { + field!((*self), value).write(value); + } + fn copy_from(&self, other: Ptr) { + field!((*self), value).write(other.with(|__s| __s.value)); + } + fn move_from(&self, other: Ptr) { + field!((*self), value).write(other.with(|__s| __s.value)); + field!(other, value).write(0); + } +} +pub trait p11_move_if_noexcept_ThrowingMoveImpl { + fn construct(&self, value: i32); + fn copy_from(&self, other: Ptr); + fn move_from(&self, other: Ptr); +} +impl p11_move_if_noexcept_ThrowingMoveImpl for Ptr { + fn construct(&self, mut value: i32) { + field!((*self), value).write(value); + } + fn copy_from(&self, other: Ptr) { + field!((*self), value).write(other.with(|__s| __s.value)); + } + fn move_from(&self, other: Ptr) { + field!((*self), value).write(other.with(|__s| __s.value)); + field!(other, value).write(0); + } +} +pub trait p6_forward_like_HolderImpl { + fn move_from(&self, _a0: Ptr); +} +impl p6_forward_like_HolderImpl for Ptr { + fn move_from(&self, _a0: Ptr) { + field!((*self), member).write({ + let __v: Value = + Rc::new(RefCell::new(::zero_init())); + p6_forward_like_MovableImpl::move_from(&__v.as_pointer(), { field_ptr!(_a0, member) }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }); + } +} +pub trait p6_forward_like_MovableImpl { + fn construct(&self, value: i32); + fn copy_from(&self, other: Ptr); + fn move_from(&self, other: Ptr); +} +impl p6_forward_like_MovableImpl for Ptr { + fn construct(&self, mut value: i32) { + field!((*self), value).write(value); + } + fn copy_from(&self, other: Ptr) { + field!((*self), value).write(other.with(|__s| __s.value)); + } + fn move_from(&self, other: Ptr) { + field!((*self), value).write(other.with(|__s| __s.value)); + field!(other, value).write(0); + } +} +pub trait p9_move_MovableImpl { + fn construct(&self, value: i32); + fn move_from(&self, other: Ptr); +} +impl p9_move_MovableImpl for Ptr { + fn construct(&self, mut value: i32) { + field!((*self), value).write(value); + } + fn move_from(&self, other: Ptr) { + field!((*self), value).write(other.with(|__s| __s.value)); + field!(other, value).write(0); + } +} pub fn __cpp2rust_init_globals() {} diff --git a/tests/std/utilities/utility/out/refcount/forward.rs b/tests/std/utilities/utility/out/refcount/forward.rs index 1c53bd4716..aa13e0440e 100644 --- a/tests/std/utilities/utility/out/refcount/forward.rs +++ b/tests/std/utilities/utility/out/refcount/forward.rs @@ -12,7 +12,7 @@ pub fn test_0() { assert!((target == 1)); assert!((source == 1)); } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct p9_move_trivially_copyable_Point { #[offset(0)] @@ -43,37 +43,42 @@ pub fn test_1() { assert!((points[0_usize].x == 5) && (points[0_usize].y == 6)); assert!((element.x == 5) && (element.y == 6)); } -#[derive(Record, ByteRepr, FnPtrArg, MoveCtor, CppFrom, Default)] -#[cpp_from(i32, new, value)] +#[derive(Record, ByteRepr, ZeroInitialization, FnPtrArg, MoveCtor, CppFrom, Default)] +#[cpp_from(i32, construct, value)] #[byte_size(4)] pub struct p10_move_constructor_A { #[offset(0)] pub value: i32, } -impl p10_move_constructor_A { - pub fn new(mut value: i32) -> Self { - Self { value: value } - } - pub fn move_from(other: Ptr) -> Self { - let __this: p10_move_constructor_A = Self { - value: other.with(|__s| __s.value), - }; - field!(other, value).write(0); - __this - } -} pub fn test_2() { - let source: Value = - Rc::new(RefCell::new(p10_move_constructor_A::new({ 1 }))); - let mut target: p10_move_constructor_A = - p10_move_constructor_A::move_from({ source.as_pointer() }); + let source: Value = Rc::new(RefCell::new({ + let __v: Value = + Rc::new(RefCell::new(::zero_init())); + p10_move_constructor_AImpl::construct(&__v.as_pointer(), { 1 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); + let mut target: p10_move_constructor_A = { + let __v: Value = + Rc::new(RefCell::new(::zero_init())); + p10_move_constructor_AImpl::move_from(&__v.as_pointer(), { source.as_pointer() }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }; assert!((target.value == 1)); assert!(({ (*source.borrow()).value } == 0)); let mut values: Vec = Vec::new(); - let element: Value = - Rc::new(RefCell::new(p10_move_constructor_A::new({ 2 }))); + let element: Value = Rc::new(RefCell::new({ + let __v: Value = + Rc::new(RefCell::new(::zero_init())); + p10_move_constructor_AImpl::construct(&__v.as_pointer(), { 2 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); { - let __a1 = p10_move_constructor_A::move_from({ element.as_pointer() }); + let __a1 = { + let __v: Value = + Rc::new(RefCell::new(::zero_init())); + p10_move_constructor_AImpl::move_from(&__v.as_pointer(), { element.as_pointer() }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }; values.push(__a1) }; assert!((values[0_usize].value == 2)); @@ -89,4 +94,17 @@ fn main_0() -> i32 { ({ test_2() }); return 0; } +pub trait p10_move_constructor_AImpl { + fn construct(&self, value: i32); + fn move_from(&self, other: Ptr); +} +impl p10_move_constructor_AImpl for Ptr { + fn construct(&self, mut value: i32) { + field!((*self), value).write(value); + } + fn move_from(&self, other: Ptr) { + field!((*self), value).write(other.with(|__s| __s.value)); + field!(other, value).write(0); + } +} pub fn __cpp2rust_init_globals() {} diff --git a/tests/std/utilities/utility/out/refcount/utility.as.const.rs b/tests/std/utilities/utility/out/refcount/utility.as.const.rs index ba50eed01d..3d11aa6791 100644 --- a/tests/std/utilities/utility/out/refcount/utility.as.const.rs +++ b/tests/std/utilities/utility/out/refcount/utility.as.const.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct p1_as_const_Counter { #[offset(0)] diff --git a/tests/std/utilities/utility/out/unsafe/forward-helpers.rs b/tests/std/utilities/utility/out/unsafe/forward-helpers.rs index 80928ab390..c30596f714 100644 --- a/tests/std/utilities/utility/out/unsafe/forward-helpers.rs +++ b/tests/std/utilities/utility/out/unsafe/forward-helpers.rs @@ -15,62 +15,72 @@ pub unsafe fn test_0() { assert!(((value) == (2))); } #[repr(C)] -#[derive(VaArg, FnPtrArg, MoveCtorUnsafe, CppFromUnsafe, Default)] -#[cpp_from(i32, new, value)] +#[derive(VaArg, FnPtrArg, ZeroInitializationUnsafe, MoveCtorUnsafe, CppFromUnsafe, Default)] +#[cpp_from(i32, construct, value)] pub struct p6_forward_like_Movable { pub value: i32, } impl p6_forward_like_Movable { - pub unsafe fn new(mut value: i32) -> Self { - let mut this = Self { value: value }; - this + pub unsafe fn construct(&mut self, mut value: i32) { + (&raw mut self.value).write(value); } - pub unsafe fn copy_from(other: *const p6_forward_like_Movable) -> Self { - let mut this = Self { - value: (*other).value, - }; - this + pub unsafe fn copy_from(&mut self, other: *const p6_forward_like_Movable) { + (&raw mut self.value).write((*other).value); } - pub unsafe fn move_from(other: *mut p6_forward_like_Movable) -> Self { - let mut this = Self { - value: (*other).value, - }; + pub unsafe fn move_from(&mut self, other: *mut p6_forward_like_Movable) { + (&raw mut self.value).write((*other).value); (*other).value = 0; - this } } impl Clone for p6_forward_like_Movable { fn clone(&self) -> Self { - unsafe { p6_forward_like_Movable::copy_from(self as *const p6_forward_like_Movable) } + unsafe { + let mut __v: p6_forward_like_Movable = ::zero_init(); + p6_forward_like_Movable::copy_from(&mut __v, self as *const p6_forward_like_Movable); + __v + } } } #[repr(C)] -#[derive(Clone, VaArg, FnPtrArg, MoveCtorUnsafe)] +#[derive(Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, MoveCtorUnsafe)] pub struct p6_forward_like_Holder { pub member: p6_forward_like_Movable, } impl p6_forward_like_Holder { - pub unsafe fn move_from(_a0: *mut p6_forward_like_Holder) -> Self { - let mut this = Self { - member: p6_forward_like_Movable::move_from({ &mut (*_a0).member }), - }; - this + pub unsafe fn move_from(&mut self, _a0: *mut p6_forward_like_Holder) { + (&raw mut self.member).write({ + let mut __v: p6_forward_like_Movable = ::zero_init(); + p6_forward_like_Movable::move_from(&mut __v, { &mut (*_a0).member }); + __v + }); } } impl Default for p6_forward_like_Holder { fn default() -> Self { unsafe { p6_forward_like_Holder { - member: p6_forward_like_Movable::new({ 1 }), + member: { + let mut __v: p6_forward_like_Movable = ::zero_init(); + p6_forward_like_Movable::construct(&mut __v, { 1 }); + __v + }, } } } } pub unsafe fn take_1(self_: *mut p6_forward_like_Holder) -> p6_forward_like_Movable { - return p6_forward_like_Movable::copy_from({ &(*self_).member }); + return { + let mut __v: p6_forward_like_Movable = ::zero_init(); + p6_forward_like_Movable::copy_from(&mut __v, { &(*self_).member }); + __v + }; } pub unsafe fn take_2(self_: *mut p6_forward_like_Holder) -> p6_forward_like_Movable { - return p6_forward_like_Movable::move_from({ &mut (*self_).member }); + return { + let mut __v: p6_forward_like_Movable = ::zero_init(); + p6_forward_like_Movable::move_from(&mut __v, { &mut (*self_).member }); + __v + }; } pub unsafe fn test_3() { let mut holder: p6_forward_like_Holder = ::default(); @@ -80,22 +90,18 @@ pub unsafe fn test_3() { assert!(((moved.value) == (1)) && ((holder.member.value) == (0))); } #[repr(C)] -#[derive(FnPtrArg, MoveCtorUnsafe, CppFromUnsafe, Default)] -#[cpp_from(i32, new, value)] +#[derive(FnPtrArg, ZeroInitializationUnsafe, MoveCtorUnsafe, CppFromUnsafe, Default)] +#[cpp_from(i32, construct, value)] pub struct p9_move_Movable { pub value: i32, } impl p9_move_Movable { - pub unsafe fn new(mut value: i32) -> Self { - let mut this = Self { value: value }; - this + pub unsafe fn construct(&mut self, mut value: i32) { + (&raw mut self.value).write(value); } - pub unsafe fn move_from(other: *mut p9_move_Movable) -> Self { - let mut this = Self { - value: (*other).value, - }; + pub unsafe fn move_from(&mut self, other: *mut p9_move_Movable) { + (&raw mut self.value).write((*other).value); (*other).value = 0; - this } } pub unsafe fn test_4() { @@ -108,8 +114,16 @@ pub unsafe fn test_4() { assert!( ((moved_pointer) == (&mut target as *mut i32)) && ((pointer) == (&mut target as *mut i32)) ); - let mut source: p9_move_Movable = p9_move_Movable::new({ 3 }); - let mut destination: p9_move_Movable = p9_move_Movable::move_from({ &mut source }); + let mut source: p9_move_Movable = { + let mut __v: p9_move_Movable = ::zero_init(); + p9_move_Movable::construct(&mut __v, { 3 }); + __v + }; + let mut destination: p9_move_Movable = { + let mut __v: p9_move_Movable = ::zero_init(); + p9_move_Movable::move_from(&mut __v, { &mut source }); + __v + }; assert!(((destination.value) == (3)) && ((source.value) == (0))); let mut strings: Vec> = Vec::new(); let mut text: Vec = std::slice::from_raw_parts( @@ -132,64 +146,68 @@ pub unsafe fn test_4() { ); } #[repr(C)] -#[derive(VaArg, FnPtrArg, MoveCtorUnsafe, CppFromUnsafe)] -#[cpp_from(i32, new, value)] +#[derive(VaArg, FnPtrArg, ZeroInitializationUnsafe, MoveCtorUnsafe, CppFromUnsafe)] +#[cpp_from(i32, construct, value)] pub struct p10_forward_in_template_A { pub value: i32, pub copies: i32, pub moves: i32, } impl p10_forward_in_template_A { - pub unsafe fn new(mut value: i32) -> Self { - let mut this = Self { - value: value, - copies: 0, - moves: 0, - }; - this + pub unsafe fn construct(&mut self, mut value: i32) { + (&raw mut self.value).write(value); + (&raw mut self.copies).write(0); + (&raw mut self.moves).write(0); } - pub unsafe fn copy_from(other: *const p10_forward_in_template_A) -> Self { - let mut this = Self { - value: (*other).value, - copies: (((*other).copies) + (1)), - moves: 0, - }; - this + pub unsafe fn copy_from(&mut self, other: *const p10_forward_in_template_A) { + (&raw mut self.value).write((*other).value); + (&raw mut self.copies).write((((*other).copies) + (1))); + (&raw mut self.moves).write(0); } - pub unsafe fn move_from(other: *mut p10_forward_in_template_A) -> Self { - let mut this = Self { - value: (*other).value, - copies: 0, - moves: (((*other).moves) + (1)), - }; + pub unsafe fn move_from(&mut self, other: *mut p10_forward_in_template_A) { + (&raw mut self.value).write((*other).value); + (&raw mut self.copies).write(0); + (&raw mut self.moves).write((((*other).moves) + (1))); (*other).value = 0; - this } } impl Clone for p10_forward_in_template_A { fn clone(&self) -> Self { - unsafe { p10_forward_in_template_A::copy_from(self as *const p10_forward_in_template_A) } + unsafe { + let mut __v: p10_forward_in_template_A = ::zero_init(); + p10_forward_in_template_A::copy_from( + &mut __v, + self as *const p10_forward_in_template_A, + ); + __v + } } } impl Default for p10_forward_in_template_A { fn default() -> Self { - unsafe { - p10_forward_in_template_A { - value: 0_i32, - copies: 0, - moves: 0, - } - } + unsafe { ::zero_init() } } } pub unsafe fn make_5(source: *mut p10_forward_in_template_A) -> p10_forward_in_template_A { - return p10_forward_in_template_A::copy_from({ source }); + return { + let mut __v: p10_forward_in_template_A = ::zero_init(); + p10_forward_in_template_A::copy_from(&mut __v, { source }); + __v + }; } pub unsafe fn make_6(source: *mut p10_forward_in_template_A) -> p10_forward_in_template_A { - return p10_forward_in_template_A::move_from({ source }); + return { + let mut __v: p10_forward_in_template_A = ::zero_init(); + p10_forward_in_template_A::move_from(&mut __v, { source }); + __v + }; } pub unsafe fn test_7() { - let mut a: p10_forward_in_template_A = p10_forward_in_template_A::new({ 1 }); + let mut a: p10_forward_in_template_A = { + let mut __v: p10_forward_in_template_A = ::zero_init(); + p10_forward_in_template_A::construct(&mut __v, { 1 }); + __v + }; let mut copied: p10_forward_in_template_A = (unsafe { make_5(&mut a) }); assert!((((copied.value) == (1)) && ((copied.copies) == (1))) && ((copied.moves) == (0))); assert!(((a.value) == (1))); @@ -198,83 +216,93 @@ pub unsafe fn test_7() { assert!(((a.value) == (0))); } #[repr(C)] -#[derive(VaArg, FnPtrArg, MoveCtorUnsafe, CppFromUnsafe, Default)] -#[cpp_from(i32, new, value)] +#[derive(VaArg, FnPtrArg, ZeroInitializationUnsafe, MoveCtorUnsafe, CppFromUnsafe, Default)] +#[cpp_from(i32, construct, value)] pub struct p11_move_if_noexcept_NoexceptMove { pub value: i32, } impl p11_move_if_noexcept_NoexceptMove { - pub unsafe fn new(mut value: i32) -> Self { - let mut this = Self { value: value }; - this + pub unsafe fn construct(&mut self, mut value: i32) { + (&raw mut self.value).write(value); } - pub unsafe fn copy_from(other: *const p11_move_if_noexcept_NoexceptMove) -> Self { - let mut this = Self { - value: (*other).value, - }; - this + pub unsafe fn copy_from(&mut self, other: *const p11_move_if_noexcept_NoexceptMove) { + (&raw mut self.value).write((*other).value); } - pub unsafe fn move_from(other: *mut p11_move_if_noexcept_NoexceptMove) -> Self { - let mut this = Self { - value: (*other).value, - }; + pub unsafe fn move_from(&mut self, other: *mut p11_move_if_noexcept_NoexceptMove) { + (&raw mut self.value).write((*other).value); (*other).value = 0; - this } } impl Clone for p11_move_if_noexcept_NoexceptMove { fn clone(&self) -> Self { unsafe { + let mut __v: p11_move_if_noexcept_NoexceptMove = + ::zero_init(); p11_move_if_noexcept_NoexceptMove::copy_from( + &mut __v, self as *const p11_move_if_noexcept_NoexceptMove, - ) + ); + __v } } } #[repr(C)] -#[derive(VaArg, FnPtrArg, MoveCtorUnsafe, CppFromUnsafe, Default)] -#[cpp_from(i32, new, value)] +#[derive(VaArg, FnPtrArg, ZeroInitializationUnsafe, MoveCtorUnsafe, CppFromUnsafe, Default)] +#[cpp_from(i32, construct, value)] pub struct p11_move_if_noexcept_ThrowingMove { pub value: i32, } impl p11_move_if_noexcept_ThrowingMove { - pub unsafe fn new(mut value: i32) -> Self { - let mut this = Self { value: value }; - this + pub unsafe fn construct(&mut self, mut value: i32) { + (&raw mut self.value).write(value); } - pub unsafe fn copy_from(other: *const p11_move_if_noexcept_ThrowingMove) -> Self { - let mut this = Self { - value: (*other).value, - }; - this + pub unsafe fn copy_from(&mut self, other: *const p11_move_if_noexcept_ThrowingMove) { + (&raw mut self.value).write((*other).value); } - pub unsafe fn move_from(other: *mut p11_move_if_noexcept_ThrowingMove) -> Self { - let mut this = Self { - value: (*other).value, - }; + pub unsafe fn move_from(&mut self, other: *mut p11_move_if_noexcept_ThrowingMove) { + (&raw mut self.value).write((*other).value); (*other).value = 0; - this } } impl Clone for p11_move_if_noexcept_ThrowingMove { fn clone(&self) -> Self { unsafe { + let mut __v: p11_move_if_noexcept_ThrowingMove = + ::zero_init(); p11_move_if_noexcept_ThrowingMove::copy_from( + &mut __v, self as *const p11_move_if_noexcept_ThrowingMove, - ) + ); + __v } } } pub unsafe fn test_8() { - let mut moved_from: p11_move_if_noexcept_NoexceptMove = - p11_move_if_noexcept_NoexceptMove::new({ 1 }); - let mut moved: p11_move_if_noexcept_NoexceptMove = - p11_move_if_noexcept_NoexceptMove::move_from({ &mut moved_from }); + let mut moved_from: p11_move_if_noexcept_NoexceptMove = { + let mut __v: p11_move_if_noexcept_NoexceptMove = + ::zero_init(); + p11_move_if_noexcept_NoexceptMove::construct(&mut __v, { 1 }); + __v + }; + let mut moved: p11_move_if_noexcept_NoexceptMove = { + let mut __v: p11_move_if_noexcept_NoexceptMove = + ::zero_init(); + p11_move_if_noexcept_NoexceptMove::move_from(&mut __v, { &mut moved_from }); + __v + }; assert!(((moved.value) == (1)) && ((moved_from.value) == (0))); - let mut copied_from: p11_move_if_noexcept_ThrowingMove = - p11_move_if_noexcept_ThrowingMove::new({ 2 }); - let mut copied: p11_move_if_noexcept_ThrowingMove = - p11_move_if_noexcept_ThrowingMove::copy_from({ &copied_from }); + let mut copied_from: p11_move_if_noexcept_ThrowingMove = { + let mut __v: p11_move_if_noexcept_ThrowingMove = + ::zero_init(); + p11_move_if_noexcept_ThrowingMove::construct(&mut __v, { 2 }); + __v + }; + let mut copied: p11_move_if_noexcept_ThrowingMove = { + let mut __v: p11_move_if_noexcept_ThrowingMove = + ::zero_init(); + p11_move_if_noexcept_ThrowingMove::copy_from(&mut __v, { &copied_from }); + __v + }; assert!(((copied.value) == (2)) && ((copied_from.value) == (2))); } pub fn main() { diff --git a/tests/std/utilities/utility/out/unsafe/forward.rs b/tests/std/utilities/utility/out/unsafe/forward.rs index 09f48ecad5..4d369471ad 100644 --- a/tests/std/utilities/utility/out/unsafe/forward.rs +++ b/tests/std/utilities/utility/out/unsafe/forward.rs @@ -13,7 +13,7 @@ pub unsafe fn test_0() { assert!(((source) == (1))); } #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] pub struct p9_move_trivially_copyable_Point { pub x: i32, pub y: i32, @@ -42,33 +42,45 @@ pub unsafe fn test_1() { assert!(((element.x) == (5)) && ((element.y) == (6))); } #[repr(C)] -#[derive(FnPtrArg, MoveCtorUnsafe, CppFromUnsafe, Default)] -#[cpp_from(i32, new, value)] +#[derive(FnPtrArg, ZeroInitializationUnsafe, MoveCtorUnsafe, CppFromUnsafe, Default)] +#[cpp_from(i32, construct, value)] pub struct p10_move_constructor_A { pub value: i32, } impl p10_move_constructor_A { - pub unsafe fn new(mut value: i32) -> Self { - let mut this = Self { value: value }; - this + pub unsafe fn construct(&mut self, mut value: i32) { + (&raw mut self.value).write(value); } - pub unsafe fn move_from(other: *mut p10_move_constructor_A) -> Self { - let mut this = Self { - value: (*other).value, - }; + pub unsafe fn move_from(&mut self, other: *mut p10_move_constructor_A) { + (&raw mut self.value).write((*other).value); (*other).value = 0; - this } } pub unsafe fn test_2() { - let mut source: p10_move_constructor_A = p10_move_constructor_A::new({ 1 }); - let mut target: p10_move_constructor_A = p10_move_constructor_A::move_from({ &mut source }); + let mut source: p10_move_constructor_A = { + let mut __v: p10_move_constructor_A = ::zero_init(); + p10_move_constructor_A::construct(&mut __v, { 1 }); + __v + }; + let mut target: p10_move_constructor_A = { + let mut __v: p10_move_constructor_A = ::zero_init(); + p10_move_constructor_A::move_from(&mut __v, { &mut source }); + __v + }; assert!(((target.value) == (1))); assert!(((source.value) == (0))); let mut values: Vec = Vec::new(); - let mut element: p10_move_constructor_A = p10_move_constructor_A::new({ 2 }); + let mut element: p10_move_constructor_A = { + let mut __v: p10_move_constructor_A = ::zero_init(); + p10_move_constructor_A::construct(&mut __v, { 2 }); + __v + }; { - let __a1 = p10_move_constructor_A::move_from({ &mut element }); + let __a1 = { + let mut __v: p10_move_constructor_A = ::zero_init(); + p10_move_constructor_A::move_from(&mut __v, { &mut element }); + __v + }; values.push(__a1) }; assert!(((values[(0_usize)].value) == (2))); diff --git a/tests/std/utilities/utility/out/unsafe/utility.as.const.rs b/tests/std/utilities/utility/out/unsafe/utility.as.const.rs index 63068a45c7..ae0918d0ef 100644 --- a/tests/std/utilities/utility/out/unsafe/utility.as.const.rs +++ b/tests/std/utilities/utility/out/unsafe/utility.as.const.rs @@ -7,7 +7,7 @@ use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] pub struct p1_as_const_Counter { pub value: i32, } diff --git a/tests/ub/out/refcount/ctor_ref_member.rs b/tests/ub/out/refcount/ctor_ref_member.rs index 18c0b0763b..36144e741d 100644 --- a/tests/ub/out/refcount/ctor_ref_member.rs +++ b/tests/ub/out/refcount/ctor_ref_member.rs @@ -6,19 +6,14 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, CppFrom, Default)] -#[cpp_from(i32, new, ptr)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, CppFrom, Default)] +#[cpp_from(i32, construct, ptr)] #[byte_size(8)] pub struct S { #[offset(0)] #[byte_size(8)] pub r: Ptr, } -impl S { - pub fn new(x: Ptr) -> Self { - Self { r: (x).clone() } - } -} pub fn main() { __cpp2rust_init_globals(); libcc2rs::cpp2rust_exit(main_0()); @@ -26,9 +21,21 @@ pub fn main() { fn main_0() -> i32 { let mut s: S = { let __tmp_0: Value = Rc::new(RefCell::new(5)); - S::new({ __tmp_0.as_pointer() }) + { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + SImpl::construct(&__v.as_pointer(), { __tmp_0.as_pointer() }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + } }; assert!(((s.r.read()) == 5)); return 0; } +pub trait SImpl { + fn construct(&self, x: Ptr); +} +impl SImpl for Ptr { + fn construct(&self, x: Ptr) { + field!((*self), r).write((x).clone()); + } +} pub fn __cpp2rust_init_globals() {} diff --git a/tests/ub/out/refcount/self_referencing_initializer.rs b/tests/ub/out/refcount/self_referencing_initializer.rs index f4a75cddc6..4dd36b221f 100644 --- a/tests/ub/out/refcount/self_referencing_initializer.rs +++ b/tests/ub/out/refcount/self_referencing_initializer.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct pair { #[offset(0)] diff --git a/tests/ub/out/refcount/ub6.rs b/tests/ub/out/refcount/ub6.rs index bb9a3ce227..3c715edc92 100644 --- a/tests/ub/out/refcount/ub6.rs +++ b/tests/ub/out/refcount/ub6.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(16)] pub struct Pair { #[offset(0)] diff --git a/tests/ub/out/unsafe/ctor_ref_member.rs b/tests/ub/out/unsafe/ctor_ref_member.rs index 19a18e0932..dc0191fb67 100644 --- a/tests/ub/out/unsafe/ctor_ref_member.rs +++ b/tests/ub/out/unsafe/ctor_ref_member.rs @@ -7,15 +7,14 @@ use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg, CppFromUnsafe, Default)] -#[cpp_from(i32, new, ptr)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, CppFromUnsafe, Default)] +#[cpp_from(i32, construct, ptr)] pub struct S { pub r: *const i32, } impl S { - pub unsafe fn new(x: *const i32) -> Self { - let mut this = Self { r: x }; - this + pub unsafe fn construct(&mut self, x: *const i32) { + (&raw mut self.r).write(x); } } pub fn main() { @@ -27,7 +26,11 @@ pub fn main() { unsafe fn main_0() -> i32 { let mut s: S = { let mut __tmp_0: i32 = 5; - S::new({ &mut __tmp_0 }) + { + let mut __v: S = ::zero_init(); + S::construct(&mut __v, { &mut __tmp_0 }); + __v + } }; assert!(((*s.r) == (5))); return 0; diff --git a/tests/ub/out/unsafe/self_referencing_initializer.rs b/tests/ub/out/unsafe/self_referencing_initializer.rs index ed816c8a26..8036b03c41 100644 --- a/tests/ub/out/unsafe/self_referencing_initializer.rs +++ b/tests/ub/out/unsafe/self_referencing_initializer.rs @@ -7,7 +7,7 @@ use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] pub struct pair { pub a: i32, pub b: i32, diff --git a/tests/ub/out/unsafe/ub6.rs b/tests/ub/out/unsafe/ub6.rs index a73fba161d..f0cd73489f 100644 --- a/tests/ub/out/unsafe/ub6.rs +++ b/tests/ub/out/unsafe/ub6.rs @@ -7,7 +7,7 @@ use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; #[repr(C)] -#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, Default)] pub struct Pair { pub x1: *mut i32, pub x2: *mut i32, diff --git a/tests/unit/out/refcount/05_new.rs b/tests/unit/out/refcount/05_new.rs index bacef4215d..bc5e4eb072 100644 --- a/tests/unit/out/refcount/05_new.rs +++ b/tests/unit/out/refcount/05_new.rs @@ -6,10 +6,10 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(1)] pub struct A {} -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(1)] pub struct D {} pub fn main() { diff --git a/tests/unit/out/refcount/10_struct.rs b/tests/unit/out/refcount/10_struct.rs index b984bb8b9c..d66369557b 100644 --- a/tests/unit/out/refcount/10_struct.rs +++ b/tests/unit/out/refcount/10_struct.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(16)] pub struct GraphNode { #[offset(0)] @@ -15,7 +15,7 @@ pub struct GraphNode { #[byte_size(8)] pub next: Ptr, } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(16)] pub struct Graph { #[offset(0)] @@ -24,18 +24,13 @@ pub struct Graph { #[byte_size(8)] pub adj: Ptr>, } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg)] #[byte_size(8)] pub struct Partial { #[offset(0)] #[byte_size(8)] pub p: Ptr, } -impl Partial { - pub fn new_1(mut q: Ptr) -> Self { - Self { p: (q).clone() } - } -} impl Default for Partial { fn default() -> Self { { @@ -45,11 +40,10 @@ impl Default for Partial { } } } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(1)] pub struct Declared {} -impl Declared {} -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(16)] pub struct S { #[offset(0)] @@ -68,7 +62,11 @@ fn main_0() -> i32 { adj: Ptr::>::null(), }; let arr: Value> = Rc::new(RefCell::new(Box::new([3, 1, 4]))); - let mut it: Partial = Partial::new_1({ arr.decay() }); + let mut it: Partial = { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + PartialImpl::construct_1(&__v.as_pointer(), { arr.decay() }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }; if ({ (it.p).clone() } != { arr.decay() }) { return 1; } @@ -85,6 +83,12 @@ fn main_0() -> i32 { } return 0; } +pub trait DeclaredImpl { + fn construct(&self, mut _a0: i32) { + unimplemented!() + } +} +impl DeclaredImpl for Ptr {} pub trait GraphImpl { fn push(&self, src: u32, dst: u32); } @@ -101,6 +105,7 @@ impl GraphImpl for Ptr { } } pub trait PartialImpl { + fn construct_1(&self, q: Ptr); fn get(&self) -> Ptr { unimplemented!() } @@ -111,5 +116,9 @@ pub trait PartialImpl { unimplemented!() } } -impl PartialImpl for Ptr {} +impl PartialImpl for Ptr { + fn construct_1(&self, mut q: Ptr) { + field!((*self), p).write((q).clone()); + } +} pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/addr_of_global.rs b/tests/unit/out/refcount/addr_of_global.rs index ddf0c79e2b..d9a77441ee 100644 --- a/tests/unit/out/refcount/addr_of_global.rs +++ b/tests/unit/out/refcount/addr_of_global.rs @@ -6,13 +6,13 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct Inner { #[offset(0)] pub value: i32, } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct Outer { #[offset(0)] diff --git a/tests/unit/out/refcount/anonymous-struct.rs b/tests/unit/out/refcount/anonymous-struct.rs index d8b0921f4d..eda0623019 100644 --- a/tests/unit/out/refcount/anonymous-struct.rs +++ b/tests/unit/out/refcount/anonymous-struct.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct Outer_Named { #[offset(0)] @@ -14,7 +14,7 @@ pub struct Outer_Named { #[offset(4)] pub b: i32, } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct anon_0 { #[offset(0)] @@ -22,7 +22,7 @@ pub struct anon_0 { #[offset(4)] pub d: i32, } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct anon_1 { #[offset(0)] @@ -30,7 +30,7 @@ pub struct anon_1 { #[offset(4)] pub h: i32, } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct anon_2 { #[offset(0)] @@ -38,19 +38,19 @@ pub struct anon_2 { #[offset(4)] pub f: i32, } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct anon_4 { #[offset(0)] pub j: i32, } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct anon_5 { #[offset(0)] pub k: i32, } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(12)] pub struct anon_3 { #[offset(0)] @@ -62,7 +62,7 @@ pub struct anon_3 { #[byte_size(4)] pub anon_5: anon_5, } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(44)] pub struct Outer { #[offset(0)] @@ -126,7 +126,7 @@ fn main_0() -> i32 { ); return 0; } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct anon_6 { #[offset(0)] diff --git a/tests/unit/out/refcount/anonymous-struct_c.rs b/tests/unit/out/refcount/anonymous-struct_c.rs index c903903369..4dde61ed84 100644 --- a/tests/unit/out/refcount/anonymous-struct_c.rs +++ b/tests/unit/out/refcount/anonymous-struct_c.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct Named { #[offset(0)] @@ -14,7 +14,7 @@ pub struct Named { #[offset(4)] pub b: i32, } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct anon_0 { #[offset(0)] @@ -22,7 +22,7 @@ pub struct anon_0 { #[offset(4)] pub d: i32, } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct anon_1 { #[offset(0)] @@ -30,7 +30,7 @@ pub struct anon_1 { #[offset(4)] pub h: i32, } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct anon_2 { #[offset(0)] @@ -38,19 +38,19 @@ pub struct anon_2 { #[offset(4)] pub f: i32, } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct anon_4 { #[offset(0)] pub j: i32, } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct anon_5 { #[offset(0)] pub k: i32, } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(12)] pub struct anon_3 { #[offset(0)] @@ -62,7 +62,7 @@ pub struct anon_3 { #[byte_size(4)] pub anon_5: anon_5, } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(44)] pub struct Outer { #[offset(0)] @@ -132,7 +132,7 @@ fn main_0() -> i32 { ); return 0; } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct anon_6 { #[offset(0)] diff --git a/tests/unit/out/refcount/anonymous_enum.rs b/tests/unit/out/refcount/anonymous_enum.rs index 28c889c8bc..e2778966b9 100644 --- a/tests/unit/out/refcount/anonymous_enum.rs +++ b/tests/unit/out/refcount/anonymous_enum.rs @@ -12,7 +12,7 @@ pub const anon_0_FIRST_B: anon_0 = 1; pub type anon_1 = u32; pub const anon_1_SECOND_A: anon_1 = 0; pub const anon_1_SECOND_B: anon_1 = 1; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct S { #[offset(0)] @@ -24,7 +24,7 @@ pub const TdEnum_TD_B: TdEnum = 1; pub type anon_2 = u32; pub const anon_2_FIELD_A: anon_2 = 0; pub const anon_2_FIELD_B: anon_2 = 1; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct WithAnonField { #[offset(0)] diff --git a/tests/unit/out/refcount/anonymous_enum_c.rs b/tests/unit/out/refcount/anonymous_enum_c.rs index 14fae2c717..5b19cd0c46 100644 --- a/tests/unit/out/refcount/anonymous_enum_c.rs +++ b/tests/unit/out/refcount/anonymous_enum_c.rs @@ -12,7 +12,7 @@ pub const anon_0_FIRST_B: anon_0 = 1; pub type anon_1 = u32; pub const anon_1_SECOND_A: anon_1 = 0; pub const anon_1_SECOND_B: anon_1 = 1; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct S { #[offset(0)] @@ -24,7 +24,7 @@ pub const TdEnum_enum_TD_B: TdEnum_enum = 1; pub type anon_2 = u32; pub const anon_2_FIELD_A: anon_2 = 0; pub const anon_2_FIELD_B: anon_2 = 1; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct WithAnonField { #[offset(0)] diff --git a/tests/unit/out/refcount/any.rs b/tests/unit/out/refcount/any.rs index 53670a2197..493694219f 100644 --- a/tests/unit/out/refcount/any.rs +++ b/tests/unit/out/refcount/any.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct Point { #[offset(0)] @@ -14,19 +14,17 @@ pub struct Point { #[offset(4)] pub y: i32, } -impl Point { - pub fn new(mut x: i32, mut y: i32) -> Self { - Self { x: x, y: y } - } -} pub fn main() { __cpp2rust_init_globals(); libcc2rs::cpp2rust_exit(main_0()); } fn main_0() -> i32 { - let a: Value>> = Rc::new(RefCell::new(Some(Rc::new( - RefCell::new(Point::new({ 3 }, { 4 })), - )))); + let a: Value>> = + Rc::new(RefCell::new(Some(Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + PointImpl::construct(&__v.as_pointer(), { 3 }, { 4 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }))))); assert!(({ (*a.borrow()).is_some() })); let mut p: Ptr = { let mut __tmp_0 = { (a.as_pointer()) }; @@ -61,7 +59,13 @@ fn main_0() -> i32 { .is_null() ); let q: Ptr = { - let mut __tmp_2 = { Point::new({ 5 }, { 6 }) }; + let mut __tmp_2 = { + { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + PointImpl::construct(&__v.as_pointer(), { 5 }, { 6 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + } + }; let __value: Value = Rc::new(RefCell::new(__tmp_2)); let __ptr = __value.as_pointer(); (*a.borrow_mut()) = Some(__value); @@ -138,4 +142,13 @@ fn main_0() -> i32 { ); return 0; } +pub trait PointImpl { + fn construct(&self, x: i32, y: i32); +} +impl PointImpl for Ptr { + fn construct(&self, mut x: i32, mut y: i32) { + field!((*self), x).write(x); + field!((*self), y).write(y); + } +} pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/array_const_init.rs b/tests/unit/out/refcount/array_const_init.rs index 661f77fe52..46be73bee6 100644 --- a/tests/unit/out/refcount/array_const_init.rs +++ b/tests/unit/out/refcount/array_const_init.rs @@ -6,16 +6,18 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg)] #[byte_size(20)] pub struct S { #[offset(0)] pub head: i32, #[offset(4)] #[byte_size(12)] + #[array_len(3)] pub tail: Box<[i32]>, #[offset(16)] #[byte_size(4)] + #[array_len(4)] pub buf: Box<[i8]>, } impl Default for S { @@ -36,7 +38,7 @@ thread_local!( buf: Box::new([0; 4]), })); ); -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct P { #[offset(0)] diff --git a/tests/unit/out/refcount/array_field_addr.rs b/tests/unit/out/refcount/array_field_addr.rs index faa1b9117f..26cbd4303f 100644 --- a/tests/unit/out/refcount/array_field_addr.rs +++ b/tests/unit/out/refcount/array_field_addr.rs @@ -6,13 +6,14 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg)] #[byte_size(12)] pub struct S { #[offset(0)] pub before: i32, #[offset(4)] #[byte_size(4)] + #[array_len(4)] pub mask: Box<[u8]>, #[offset(8)] pub after: i32, diff --git a/tests/unit/out/refcount/array_of_noncopy_struct.rs b/tests/unit/out/refcount/array_of_noncopy_struct.rs index 7fbf4cf803..bf0ca98779 100644 --- a/tests/unit/out/refcount/array_of_noncopy_struct.rs +++ b/tests/unit/out/refcount/array_of_noncopy_struct.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, MoveCtor, MoveAssign)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, MoveCtor, MoveAssign)] #[byte_size(32)] pub struct NonCopy { #[offset(0)] @@ -15,14 +15,6 @@ pub struct NonCopy { #[offset(24)] pub tag: i32, } -impl NonCopy { - pub fn move_from(_a0: Ptr) -> Self { - Self { - data: field!(_a0, data).with_mut(std::mem::take), - tag: { (*_a0.upgrade().deref()).tag }, - } - } -} impl Default for NonCopy { fn default() -> Self { { @@ -52,9 +44,14 @@ fn main_0() -> i32 { return 0; } pub trait NonCopyImpl { + fn move_from(&self, _a0: Ptr); fn move_assign(&self, _a0: Ptr) -> Ptr; } impl NonCopyImpl for Ptr { + fn move_from(&self, _a0: Ptr) { + field!((*self), data).write(field!(_a0, data).with_mut(std::mem::take)); + field!((*self), tag).write({ (*_a0.upgrade().deref()).tag }); + } fn move_assign(&self, _a0: Ptr) -> Ptr { field!((*self), data).with_mut(|__b0| { (*__b0) = field!(_a0, data).with_mut(std::mem::take); diff --git a/tests/unit/out/refcount/array_reference.rs b/tests/unit/out/refcount/array_reference.rs index d2666653e8..7bf1089f73 100644 --- a/tests/unit/out/refcount/array_reference.rs +++ b/tests/unit/out/refcount/array_reference.rs @@ -59,7 +59,7 @@ pub fn fill_and_sum_9(a: Ptr, mut v: i32, out: Ptr) { pub fn pick_10(s: Ptr) -> Ptr { return (s).clone(); } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct Point { #[offset(0)] diff --git a/tests/unit/out/refcount/as_const.rs b/tests/unit/out/refcount/as_const.rs index 4bd25fd80f..cfda0a2235 100644 --- a/tests/unit/out/refcount/as_const.rs +++ b/tests/unit/out/refcount/as_const.rs @@ -9,7 +9,7 @@ use std::rc::{Rc, Weak}; pub type Overload = u32; pub const Overload_kMutableOverload: Overload = 1; pub const Overload_kConstOverload: Overload = 2; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct S { #[offset(0)] diff --git a/tests/unit/out/refcount/assign_rhs_first.rs b/tests/unit/out/refcount/assign_rhs_first.rs index 44e750a1f5..36b9ea5db5 100644 --- a/tests/unit/out/refcount/assign_rhs_first.rs +++ b/tests/unit/out/refcount/assign_rhs_first.rs @@ -15,7 +15,7 @@ pub fn advance_1() -> i32 { }); return 10; } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct S { #[offset(0)] diff --git a/tests/unit/out/refcount/assignment_single_eval.rs b/tests/unit/out/refcount/assignment_single_eval.rs index 93f1d0fe22..bbe8e8fc5e 100644 --- a/tests/unit/out/refcount/assignment_single_eval.rs +++ b/tests/unit/out/refcount/assignment_single_eval.rs @@ -810,21 +810,21 @@ pub fn test_reference_members_9() { (*other.borrow()) ); } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct Slot_6 { #[offset(0)] #[byte_size(8)] pub value: Ptr, } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct Slot_8 { #[offset(0)] #[byte_size(8)] pub value: Ptr, } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct Slot_10 { #[offset(0)] @@ -889,7 +889,7 @@ pub fn test_pointer_elements_11() { }; printf!(b"pointer increments %d %d\n", (*index.borrow()), value); } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct Holder_12 { #[offset(0)] @@ -923,7 +923,7 @@ pub fn test_reference_pointer_13() { (({ (result).clone() } == { (second.as_pointer()) }) as i32) ); } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct Node_14 { #[offset(0)] @@ -1020,26 +1020,26 @@ pub fn test_reference_subfields_18() { ({ (*other.borrow()).value } as i32) ); } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct Index_16 { #[offset(0)] pub value: u32, } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct Slot_17 { #[offset(0)] #[byte_size(8)] pub ref_: Ptr, } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(2)] pub struct Index_19 { #[offset(0)] pub value: i16, } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct Slot_20 { #[offset(0)] @@ -1081,13 +1081,13 @@ pub fn observe_global_23(mut assigned: i32) -> i32 { pub fn observe_24(mut assigned: i32, mut stored: Ptr) -> i32 { return ({ assigned } + { (stored.read()) }); } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct GlobalRecord_Inner { #[offset(0)] pub value: i32, } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct GlobalRecord { #[offset(0)] @@ -1328,13 +1328,13 @@ thread_local!( thread_local!( static flag_31: Value = Rc::new(RefCell::new(false)); ); -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct Record_32 { #[offset(0)] pub value: i32, } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct Link_33 { #[offset(0)] @@ -1345,7 +1345,7 @@ thread_local!( pub static selected_values_34: Value> = Rc::new(RefCell::new(Box::new([0_u32, 20_u32, 30_u32]))); ); -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(1)] pub struct IndexedBinding {} pub fn test_binding_35() { @@ -1580,7 +1580,7 @@ thread_local!( Box::new([20_u32, 30_u32]), ]))); ); -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(12)] pub struct IndexedMember { #[offset(0)] @@ -1941,7 +1941,7 @@ thread_local!( scoped_values_52.with(|v| v.decay()), ]))); ); -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct PointerSelector { #[offset(0)] @@ -2142,21 +2142,16 @@ pub fn test_stable_containers_58() { }); printf!(b"nonflat values %u %u\n", iterator_value, mapped_value); } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, MoveCtor, MoveAssign, Default)] +#[derive( + Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, MoveCtor, MoveAssign, Default, +)] #[byte_size(24)] pub struct Counts_59 { #[offset(0)] #[byte_size(24)] pub counts: Vec, } -impl Counts_59 { - pub fn move_from(_a0: Ptr) -> Self { - Self { - counts: field!(_a0, counts).with_mut(std::mem::take), - } - } -} -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct Pair_60 { #[offset(0)] @@ -2467,17 +2462,18 @@ fn main_0() -> i32 { ); return 0; } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct Index_62 { #[offset(0)] pub value: i32, } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg)] #[byte_size(12)] pub struct Owner_63 { #[offset(0)] #[byte_size(8)] + #[array_len(2)] pub values: Box<[i32]>, #[offset(8)] pub index: i32, @@ -2493,9 +2489,13 @@ impl Default for Owner_63 { } } pub trait Counts_59Impl { + fn move_from(&self, _a0: Ptr); fn move_assign(&self, _a0: Ptr) -> Ptr; } impl Counts_59Impl for Ptr { + fn move_from(&self, _a0: Ptr) { + field!((*self), counts).write(field!(_a0, counts).with_mut(std::mem::take)); + } fn move_assign(&self, _a0: Ptr) -> Ptr { field!((*self), counts).with_mut(|__b0| { (*__b0) = field!(_a0, counts).with_mut(std::mem::take); diff --git a/tests/unit/out/refcount/bounded_struct_ptr.rs b/tests/unit/out/refcount/bounded_struct_ptr.rs index a4bf73b4c3..1504753273 100644 --- a/tests/unit/out/refcount/bounded_struct_ptr.rs +++ b/tests/unit/out/refcount/bounded_struct_ptr.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct Foo { #[offset(0)] diff --git a/tests/unit/out/refcount/bst.rs b/tests/unit/out/refcount/bst.rs index 1dbb4f4e04..bb456f8a87 100644 --- a/tests/unit/out/refcount/bst.rs +++ b/tests/unit/out/refcount/bst.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(24)] pub struct node_t { #[offset(0)] diff --git a/tests/unit/out/refcount/c_struct.rs b/tests/unit/out/refcount/c_struct.rs index 5decad14dc..3ca4cf031e 100644 --- a/tests/unit/out/refcount/c_struct.rs +++ b/tests/unit/out/refcount/c_struct.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct Point { #[offset(0)] @@ -14,7 +14,7 @@ pub struct Point { #[offset(4)] pub y: i32, } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(16)] pub struct Line { #[offset(0)] @@ -24,7 +24,7 @@ pub struct Line { #[byte_size(8)] pub end: Point, } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(16)] pub struct Node { #[offset(0)] @@ -37,7 +37,7 @@ pub type Color = u32; pub const Color_RED: Color = 0; pub const Color_GREEN: Color = 1; pub const Color_BLUE: Color = 2; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct Inner { #[offset(0)] @@ -45,7 +45,7 @@ pub struct Inner { #[offset(4)] pub b: i32, } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(16)] pub struct Container { #[offset(0)] diff --git a/tests/unit/out/refcount/cast.rs b/tests/unit/out/refcount/cast.rs index 4595f6eb92..b935d1fca7 100644 --- a/tests/unit/out/refcount/cast.rs +++ b/tests/unit/out/refcount/cast.rs @@ -10,7 +10,7 @@ pub type E = u32; pub const E_A: E = 0; pub const E_B: E = 1; pub const E_C: E = 2; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct Pair { #[offset(0)] @@ -18,23 +18,25 @@ pub struct Pair { #[offset(4)] pub y: i32, } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(64)] #[byte_align(64)] pub struct Wide { #[offset(0)] pub c: i8, } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg)] #[byte_size(40)] pub struct Grid { #[offset(0)] pub h: i32, #[offset(4)] #[byte_size(12)] + #[array_len(3)] pub row: Box<[i32]>, #[offset(16)] #[byte_size(24)] + #[array_len(2, 3)] pub cells: Box<[Box<[i32]>]>, } impl Default for Grid { @@ -50,7 +52,9 @@ impl Default for Grid { } } } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, MoveCtor, MoveAssign, Default)] +#[derive( + Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, MoveCtor, MoveAssign, Default, +)] #[byte_size(32)] pub struct Bag { #[offset(0)] @@ -59,14 +63,6 @@ pub struct Bag { #[byte_size(24)] pub items: Vec, } -impl Bag { - pub fn move_from(_a0: Ptr) -> Self { - Self { - n: { (*_a0.upgrade().deref()).n }, - items: field!(_a0, items).with_mut(std::mem::take), - } - } -} pub fn adjacent_arrays_0() { let a: Value> = Rc::new(RefCell::new(Box::new([1, 2, 3, 4]))); let b: Value> = Rc::new(RefCell::new(Box::new([5, 6, 7, 8]))); @@ -521,9 +517,14 @@ fn main_0() -> i32 { return 0; } pub trait BagImpl { + fn move_from(&self, _a0: Ptr); fn move_assign(&self, _a0: Ptr) -> Ptr; } impl BagImpl for Ptr { + fn move_from(&self, _a0: Ptr) { + field!((*self), n).write({ (*_a0.upgrade().deref()).n }); + field!((*self), items).write(field!(_a0, items).with_mut(std::mem::take)); + } fn move_assign(&self, _a0: Ptr) -> Ptr { field!((*self), n).write({ (*_a0.upgrade().deref()).n }); field!((*self), items).with_mut(|__b0| { diff --git a/tests/unit/out/refcount/cast_array_to_pointer_decay.rs b/tests/unit/out/refcount/cast_array_to_pointer_decay.rs index a8966503b1..4c176fcd8b 100644 --- a/tests/unit/out/refcount/cast_array_to_pointer_decay.rs +++ b/tests/unit/out/refcount/cast_array_to_pointer_decay.rs @@ -16,11 +16,12 @@ pub fn strlen_1(mut s: Ptr) -> i32 { } return c; } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg)] #[byte_size(12)] pub struct Row { #[offset(0)] #[byte_size(12)] + #[array_len(3)] pub a: Box<[i32]>, } impl Default for Row { diff --git a/tests/unit/out/refcount/class.rs b/tests/unit/out/refcount/class.rs index 956037660f..ac2303a74e 100644 --- a/tests/unit/out/refcount/class.rs +++ b/tests/unit/out/refcount/class.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct Pair { #[offset(0)] @@ -14,7 +14,7 @@ pub struct Pair { #[offset(4)] pub second: i32, } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(16)] pub struct Route { #[offset(0)] @@ -23,7 +23,7 @@ pub struct Route { #[offset(8)] pub cost: f64, } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct Counter { #[offset(0)] @@ -36,7 +36,7 @@ impl CppEq for Counter { CounterImpl::operator_eq(__lhs, __rhs.clone()) } } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct Tally { #[offset(0)] diff --git a/tests/unit/out/refcount/class_template_lazy_member.rs b/tests/unit/out/refcount/class_template_lazy_member.rs index 0f4f8b70ab..0cd2514117 100644 --- a/tests/unit/out/refcount/class_template_lazy_member.rs +++ b/tests/unit/out/refcount/class_template_lazy_member.rs @@ -6,32 +6,32 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct Point { #[offset(0)] pub x: i32, } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct Box_int_ { #[offset(0)] pub val: i32, } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct Box_Point_ { #[offset(0)] #[byte_size(4)] pub val: Point, } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct Deduced_int_ { #[offset(0)] pub val: i32, } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct Deduced_Point_ { #[offset(0)] diff --git a/tests/unit/out/refcount/class_templates.rs b/tests/unit/out/refcount/class_templates.rs index c5497d3369..51f926cd50 100644 --- a/tests/unit/out/refcount/class_templates.rs +++ b/tests/unit/out/refcount/class_templates.rs @@ -6,49 +6,34 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, MoveCtor, MoveAssign, Default)] +#[derive( + Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, MoveCtor, MoveAssign, Default, +)] #[byte_size(24)] pub struct MyContainer_int_ { #[offset(0)] #[byte_size(24)] vec_: Vec, } -impl MyContainer_int_ { - pub fn move_from(_a0: Ptr) -> Self { - Self { - vec_: field!(_a0, vec_).with_mut(std::mem::take), - } - } -} -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, MoveCtor, MoveAssign, Default)] +#[derive( + Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, MoveCtor, MoveAssign, Default, +)] #[byte_size(24)] pub struct MyContainer_char_ { #[offset(0)] #[byte_size(24)] vec_: Vec, } -impl MyContainer_char_ { - pub fn move_from(_a0: Ptr) -> Self { - Self { - vec_: field!(_a0, vec_).with_mut(std::mem::take), - } - } -} -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, MoveCtor, MoveAssign, Default)] +#[derive( + Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, MoveCtor, MoveAssign, Default, +)] #[byte_size(24)] pub struct MyContainer_float_ { #[offset(0)] #[byte_size(24)] vec_: Vec, } -impl MyContainer_float_ { - pub fn move_from(_a0: Ptr) -> Self { - Self { - vec_: field!(_a0, vec_).with_mut(std::mem::take), - } - } -} -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(2)] pub struct Boxed_short_ { #[offset(0)] @@ -59,7 +44,7 @@ impl Boxed_short_ { return (((v as i32) + (v as i32)) as i16); } } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct Boxed_int_ { #[offset(0)] @@ -70,7 +55,7 @@ impl Boxed_int_ { return (v + v); } } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct Boxed_long_ { #[offset(0)] @@ -81,7 +66,7 @@ impl Boxed_long_ { return (v + v); } } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct Outer_int__Inner_int_ { #[offset(0)] @@ -89,13 +74,13 @@ pub struct Outer_int__Inner_int_ { #[offset(4)] pub u: i32, } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct Outer_int_ { #[offset(0)] pub v: i32, } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(16)] pub struct Outer_long__Inner_int_ { #[offset(0)] @@ -103,7 +88,7 @@ pub struct Outer_long__Inner_int_ { #[offset(8)] pub u: i32, } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(16)] pub struct Outer_long__Inner_char_ { #[offset(0)] @@ -111,13 +96,13 @@ pub struct Outer_long__Inner_char_ { #[offset(8)] pub u: i8, } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct Outer_long_ { #[offset(0)] pub v: i64, } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct Elem { #[offset(0)] @@ -243,6 +228,7 @@ pub trait MyContainer_char_Impl { fn back_4(&self) -> Ptr; fn pop_back(&self); fn push_back(&self, item: Ptr); + fn move_from(&self, _a0: Ptr); fn move_assign(&self, _a0: Ptr) -> Ptr; } impl MyContainer_char_Impl for Ptr { @@ -268,6 +254,9 @@ impl MyContainer_char_Impl for Ptr { field!((*self), vec_).with_mut(|__v: &mut Vec| __v.push(a0_clone)) }; } + fn move_from(&self, _a0: Ptr) { + field!((*self), vec_).write(field!(_a0, vec_).with_mut(std::mem::take)); + } fn move_assign(&self, _a0: Ptr) -> Ptr { field!((*self), vec_).with_mut(|__b0| { (*__b0) = field!(_a0, vec_).with_mut(std::mem::take); @@ -284,6 +273,7 @@ pub trait MyContainer_float_Impl { fn back_4(&self) -> Ptr; fn pop_back(&self); fn push_back(&self, item: Ptr); + fn move_from(&self, _a0: Ptr); fn move_assign(&self, _a0: Ptr) -> Ptr; } impl MyContainer_float_Impl for Ptr { @@ -309,6 +299,9 @@ impl MyContainer_float_Impl for Ptr { field!((*self), vec_).with_mut(|__v: &mut Vec| __v.push(a0_clone)) }; } + fn move_from(&self, _a0: Ptr) { + field!((*self), vec_).write(field!(_a0, vec_).with_mut(std::mem::take)); + } fn move_assign(&self, _a0: Ptr) -> Ptr { field!((*self), vec_).with_mut(|__b0| { (*__b0) = field!(_a0, vec_).with_mut(std::mem::take); @@ -325,6 +318,7 @@ pub trait MyContainer_int_Impl { fn back_4(&self) -> Ptr; fn pop_back(&self); fn push_back(&self, item: Ptr); + fn move_from(&self, _a0: Ptr); fn move_assign(&self, _a0: Ptr) -> Ptr; } impl MyContainer_int_Impl for Ptr { @@ -350,6 +344,9 @@ impl MyContainer_int_Impl for Ptr { field!((*self), vec_).with_mut(|__v: &mut Vec| __v.push(a0_clone)) }; } + fn move_from(&self, _a0: Ptr) { + field!((*self), vec_).write(field!(_a0, vec_).with_mut(std::mem::take)); + } fn move_assign(&self, _a0: Ptr) -> Ptr { field!((*self), vec_).with_mut(|__b0| { (*__b0) = field!(_a0, vec_).with_mut(std::mem::take); diff --git a/tests/unit/out/refcount/clone_vs_move.rs b/tests/unit/out/refcount/clone_vs_move.rs index aa79d410b1..e039b96e3b 100644 --- a/tests/unit/out/refcount/clone_vs_move.rs +++ b/tests/unit/out/refcount/clone_vs_move.rs @@ -6,13 +6,13 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct Bar { #[offset(0)] pub w: i32, } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg)] #[byte_size(40)] pub struct Foo { #[offset(0)] @@ -25,6 +25,7 @@ pub struct Foo { pub z: Ptr, #[offset(24)] #[byte_size(12)] + #[array_len(3)] pub a: Box<[i32]>, #[offset(36)] #[byte_size(4)] @@ -43,7 +44,7 @@ impl Default for Foo { } } } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(16)] pub struct Refs { #[offset(0)] diff --git a/tests/unit/out/refcount/comma_operator.rs b/tests/unit/out/refcount/comma_operator.rs index 019d8e9d37..dfa9317ee4 100644 --- a/tests/unit/out/refcount/comma_operator.rs +++ b/tests/unit/out/refcount/comma_operator.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct Pair { #[offset(0)] diff --git a/tests/unit/out/refcount/complex_function.rs b/tests/unit/out/refcount/complex_function.rs index 5f4624d666..67a13bc86d 100644 --- a/tests/unit/out/refcount/complex_function.rs +++ b/tests/unit/out/refcount/complex_function.rs @@ -15,27 +15,27 @@ pub fn ptr_1(mut x: Ptr) -> Ptr { pub fn bar_2(x: Ptr) -> Ptr { return (x).clone(); } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct X1 { #[offset(0)] pub v: i32, } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct X2 { #[offset(0)] #[byte_size(8)] pub v: Ptr, } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct X3 { #[offset(0)] #[byte_size(8)] pub v: Ptr, } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct X4 { #[offset(0)] diff --git a/tests/unit/out/refcount/compound_assign_paren.rs b/tests/unit/out/refcount/compound_assign_paren.rs index eba87b7b6c..d0915965f7 100644 --- a/tests/unit/out/refcount/compound_assign_paren.rs +++ b/tests/unit/out/refcount/compound_assign_paren.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(1)] pub struct Item { #[offset(0)] diff --git a/tests/unit/out/refcount/concepts.rs b/tests/unit/out/refcount/concepts.rs index fe8b126282..2f2400e07d 100644 --- a/tests/unit/out/refcount/concepts.rs +++ b/tests/unit/out/refcount/concepts.rs @@ -11,7 +11,7 @@ const _: () = assert!( (::std::mem::size_of::() == 4_usize), "sizeof(int) == 4" ); -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(1)] pub struct Sized {} pub fn is_small_0() -> bool { diff --git a/tests/unit/out/refcount/constexpr_function.rs b/tests/unit/out/refcount/constexpr_function.rs index 30a83c6b04..fc933ae67d 100644 --- a/tests/unit/out/refcount/constexpr_function.rs +++ b/tests/unit/out/refcount/constexpr_function.rs @@ -21,7 +21,7 @@ pub fn scaled_2(mut x: i32) -> i32 { pub fn half_3(mut x: f64) -> f64 { return (x / 2_f64); } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct Flag { #[offset(0)] @@ -44,7 +44,7 @@ const _: () = assert!(true, "in_constant_context()"); pub fn imm_7(mut b: bool) -> bool { return b; } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct P { #[offset(0)] diff --git a/tests/unit/out/refcount/constructor.rs b/tests/unit/out/refcount/constructor.rs index b39da5bcb5..0663e02d8a 100644 --- a/tests/unit/out/refcount/constructor.rs +++ b/tests/unit/out/refcount/constructor.rs @@ -9,25 +9,17 @@ use std::rc::{Rc, Weak}; thread_local!( pub static total_0: Value = Rc::new(RefCell::new(0)); ); -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, CppFrom, Destructible, Default)] -#[cpp_from(i32, new, value)] +#[derive( + Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, CppFrom, Destructible, Default, +)] +#[cpp_from(i32, construct, value)] #[byte_size(4)] pub struct S { #[offset(0)] pub v: i32, } -impl S { - pub fn new(mut init: i32) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { v: init })); - let this: Ptr = __this.as_pointer(); - ({ SImpl::mut_method(&this) }); - let __rhs = ({ SImpl::const_method(&this) }); - total_0.with(|rc| *rc.borrow_mut() += __rhs); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } -} -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, CppFrom)] -#[cpp_from(i32, new_1, value)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, CppFrom)] +#[cpp_from(i32, construct_1, value)] #[byte_size(8)] pub struct Point { #[offset(0)] @@ -35,30 +27,15 @@ pub struct Point { #[offset(4)] pub y: i32, } -impl Point { - pub fn new_2(mut x: i32, mut y: i32) -> Self { - Self { x: x, y: y } - } - pub fn new_1(mut v: i32) -> Self { - let __this: Value = Rc::new(RefCell::new(Point::new_2({ v }, { (v + 1) }))); - let this: Ptr = __this.as_pointer(); - { - field!(this, y).with_mut(|__v| *__v = *__v * 10) - }; - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } - pub fn new_3() -> Self { - let __this: Value = Rc::new(RefCell::new(Point::new_1({ 4 }))); - let this: Ptr = __this.as_pointer(); - { - field!(this, x).with_mut(|__v| *__v = *__v + 100) - }; - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } -} impl Default for Point { fn default() -> Self { - { Point::new_3() } + { + { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + PointImpl::construct_3(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + } + } } } pub fn main() { @@ -67,24 +44,68 @@ pub fn main() { } fn main_0() -> i32 { { - let s: Value = Rc::new(RefCell::new(S::new({ 3 }))); + let s: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + SImpl::construct(&__v.as_pointer(), { 3 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); let _dtor_s = std::mem::DropGuard::new(s.clone(), |__p| Destructible::destroy(__p.as_pointer())); assert!(({ (*s.borrow()).v } == 4)); assert!((total_0.with(|rc| *rc.borrow()) == 8)); } assert!((total_0.with(|rc| *rc.borrow()) == 18)); - let mut p: Point = Point::new_3(); + let mut p: Point = { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + PointImpl::construct_3(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }; assert!((p.x == 104)); assert!((p.y == 50)); - let mut q: Point = Point::new_1({ 7 }); + let mut q: Point = { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + PointImpl::construct_1(&__v.as_pointer(), { 7 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }; assert!((q.x == 7)); assert!((q.y == 80)); return 0; } +pub trait PointImpl { + fn construct_2(&self, x: i32, y: i32); + fn construct_1(&self, v: i32); + fn construct_3(&self); +} +impl PointImpl for Ptr { + fn construct_2(&self, mut x: i32, mut y: i32) { + field!((*self), x).write(x); + field!((*self), y).write(y); + } + fn construct_1(&self, mut v: i32) { + self.write({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + PointImpl::construct_2(&__v.as_pointer(), { v }, { (v + 1) }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }); + { + field!((*self), y).with_mut(|__v| *__v = *__v * 10) + }; + } + fn construct_3(&self) { + self.write({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + PointImpl::construct_1(&__v.as_pointer(), { 4 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }); + { + field!((*self), x).with_mut(|__v| *__v = *__v + 100) + }; + } +} pub trait SImpl { fn const_method(&self) -> i32; fn mut_method(&self); + fn construct(&self, init: i32); fn destructor(&self); } impl SImpl for Ptr { @@ -96,6 +117,12 @@ impl SImpl for Ptr { field!((*self), v).with_mut(|__v| *__v = *__v + 1) }; } + fn construct(&self, mut init: i32) { + field!((*self), v).write(init); + ({ SImpl::mut_method(self) }); + let __rhs = ({ SImpl::const_method(self) }); + total_0.with(|rc| *rc.borrow_mut() += __rhs); + } fn destructor(&self) { ({ SImpl::mut_method(self) }); let __rhs = ({ SImpl::const_method(self) }); diff --git a/tests/unit/out/refcount/container_element_receiver.rs b/tests/unit/out/refcount/container_element_receiver.rs index 87ab29fade..ce9e3647ea 100644 --- a/tests/unit/out/refcount/container_element_receiver.rs +++ b/tests/unit/out/refcount/container_element_receiver.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, MoveCtor, Default)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, MoveCtor, Default)] #[byte_size(32)] pub struct Holder { #[offset(0)] @@ -16,14 +16,6 @@ pub struct Holder { #[byte_size(24)] pub vec_: Vec, } -impl Holder { - pub fn move_from(_a0: Ptr) -> Self { - Self { - ref_: ({ (*_a0.upgrade().deref()).ref_.clone() }).clone(), - vec_: field!(_a0, vec_).with_mut(std::mem::take), - } - } -} pub fn pick_0(a: Ptr>, b: Ptr>, mut first: bool) -> Ptr> { return if first { (a).clone() } else { (b).clone() }; } @@ -140,4 +132,13 @@ fn main_0() -> i32 { assert!((({ sum_1(v.as_pointer(),) }) == 7)); return 0; } +pub trait HolderImpl { + fn move_from(&self, _a0: Ptr); +} +impl HolderImpl for Ptr { + fn move_from(&self, _a0: Ptr) { + field!((*self), ref_).write(({ (*_a0.upgrade().deref()).ref_.clone() }).clone()); + field!((*self), vec_).write(field!(_a0, vec_).with_mut(std::mem::take)); + } +} pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/container_field_addr.rs b/tests/unit/out/refcount/container_field_addr.rs index 693b7843c1..8cf629f6a9 100644 --- a/tests/unit/out/refcount/container_field_addr.rs +++ b/tests/unit/out/refcount/container_field_addr.rs @@ -6,7 +6,9 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, MoveCtor, MoveAssign, Default)] +#[derive( + Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, MoveCtor, MoveAssign, Default, +)] #[byte_size(32)] pub struct S { #[offset(0)] @@ -15,14 +17,6 @@ pub struct S { #[byte_size(24)] pub v: Vec, } -impl S { - pub fn move_from(_a0: Ptr) -> Self { - Self { - tag: { (*_a0.upgrade().deref()).tag }, - v: field!(_a0, v).with_mut(std::mem::take), - } - } -} pub fn add_0(mut v: Ptr>, mut k: i32) { { let a0_clone = k.clone(); @@ -60,9 +54,14 @@ fn main_0() -> i32 { return 0; } pub trait SImpl { + fn move_from(&self, _a0: Ptr); fn move_assign(&self, _a0: Ptr) -> Ptr; } impl SImpl for Ptr { + fn move_from(&self, _a0: Ptr) { + field!((*self), tag).write({ (*_a0.upgrade().deref()).tag }); + field!((*self), v).write(field!(_a0, v).with_mut(std::mem::take)); + } fn move_assign(&self, _a0: Ptr) -> Ptr { field!((*self), tag).write({ (*_a0.upgrade().deref()).tag }); field!((*self), v).with_mut(|__b0| { diff --git a/tests/unit/out/refcount/conversion_operator_same_translation.rs b/tests/unit/out/refcount/conversion_operator_same_translation.rs index ea9205554a..bad76bd5f0 100644 --- a/tests/unit/out/refcount/conversion_operator_same_translation.rs +++ b/tests/unit/out/refcount/conversion_operator_same_translation.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, CppFrom, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, CppFrom, Default)] #[cpp_into(i8, to_i8)] #[cpp_into(u8, to_u8)] #[byte_size(4)] @@ -14,7 +14,7 @@ pub struct S { #[offset(0)] pub v: i32, } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, CppFrom, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, CppFrom, Default)] #[cpp_into(i64, to_i64_1)] #[byte_size(4)] pub struct T { diff --git a/tests/unit/out/refcount/copy_assign.rs b/tests/unit/out/refcount/copy_assign.rs index e258688a6b..bd09ccb7a8 100644 --- a/tests/unit/out/refcount/copy_assign.rs +++ b/tests/unit/out/refcount/copy_assign.rs @@ -9,7 +9,7 @@ use std::rc::{Rc, Weak}; thread_local!( pub static assigns_0: Value = Rc::new(RefCell::new(0)); ); -#[derive(Record, ByteRepr, VaArg, FnPtrArg, CopyAssign, Default)] +#[derive(Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, CopyAssign, Default)] #[byte_size(8)] pub struct Partial { #[offset(0)] @@ -17,59 +17,53 @@ pub struct Partial { #[offset(4)] pub keep: i32, } -impl Partial { - pub fn new(mut v: i32, mut keep: i32) -> Self { - Self { v: v, keep: keep } - } - pub fn copy_from(o: Ptr) -> Self { - Self { - v: o.with(|__s| __s.v), - keep: o.with(|__s| __s.keep), - } - } -} impl Clone for Partial { fn clone(&self) -> Self { let __src: Value = Rc::new(RefCell::new(Partial { v: self.v.clone(), keep: self.keep.clone(), })); - Partial::copy_from(__src.as_pointer()) + let __v: Value = Rc::new(RefCell::new(::zero_init())); + PartialImpl::copy_from(&__v.as_pointer(), __src.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() } } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg)] #[byte_size(4)] pub struct NonConstAssign { #[offset(0)] pub mark: i32, } -impl NonConstAssign { - pub fn new() -> Self { - Self { mark: 0 } - } -} impl Default for NonConstAssign { fn default() -> Self { - { NonConstAssign::new() } + { + { + let __v: Value = + Rc::new(RefCell::new(::zero_init())); + NonConstAssignImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + } + } } } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, CopyAssign)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, CopyAssign)] #[byte_size(4)] pub struct RefQualified { #[offset(0)] pub mark: i32, } -impl RefQualified { - pub fn new() -> Self { - Self { mark: 0 } - } -} impl Default for RefQualified { fn default() -> Self { - { RefQualified::new() } + { + { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + RefQualifiedImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + } + } } } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, CopyAssign)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, CopyAssign)] #[byte_size(24)] pub struct Holder { #[offset(0)] @@ -77,6 +71,7 @@ pub struct Holder { pub p: Partial, #[offset(8)] #[byte_size(16)] + #[array_len(2)] pub arr: Box<[Partial]>, } impl Default for Holder { @@ -94,32 +89,22 @@ impl Default for Holder { thread_local!( pub static copies_1: Value = Rc::new(RefCell::new(0)); ); -#[derive(Record, ByteRepr, VaArg, FnPtrArg, CppFrom, CopyAssign, Default)] -#[cpp_from(i32, new, value)] +#[derive(Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, CppFrom, CopyAssign, Default)] +#[cpp_from(i32, construct, value)] #[byte_size(4)] pub struct CopyCounted { #[offset(0)] pub v: i32, } -impl CopyCounted { - pub fn new(mut v: i32) -> Self { - Self { v: v } - } - pub fn copy_from(o: Ptr) -> Self { - let __this: CopyCounted = Self { - v: o.with(|__s| __s.v), - }; - (*copies_1.with(Value::clone).borrow_mut()).prefix_inc(); - __this - } -} impl Clone for CopyCounted { fn clone(&self) -> Self { let __src: Value = Rc::new(RefCell::new(CopyCounted { v: self.v.clone() })); - CopyCounted::copy_from(__src.as_pointer()) + let __v: Value = Rc::new(RefCell::new(::zero_init())); + CopyCountedImpl::copy_from(&__v.as_pointer(), __src.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() } } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, CopyAssign)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, CopyAssign)] #[byte_size(12)] pub struct HasCopyCounted { #[offset(0)] @@ -127,6 +112,7 @@ pub struct HasCopyCounted { pub c: CopyCounted, #[offset(4)] #[byte_size(8)] + #[array_len(2)] pub arr: Box<[CopyCounted]>, } impl Default for HasCopyCounted { @@ -146,9 +132,21 @@ pub fn main() { libcc2rs::cpp2rust_exit(main_0()); } fn main_0() -> i32 { - let a: Value = Rc::new(RefCell::new(Partial::new({ 1 }, { 100 }))); - let b: Value = Rc::new(RefCell::new(Partial::new({ 2 }, { 200 }))); - let c: Value = Rc::new(RefCell::new(Partial::new({ 3 }, { 300 }))); + let a: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + PartialImpl::construct(&__v.as_pointer(), { 1 }, { 100 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); + let b: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + PartialImpl::construct(&__v.as_pointer(), { 2 }, { 200 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); + let c: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + PartialImpl::construct(&__v.as_pointer(), { 3 }, { 300 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); ({ PartialImpl::copy_assign(&a.as_pointer(), b.as_pointer()) }); assert!(({ (*a.borrow()).v } == 2) && ({ (*a.borrow()).keep } == 100)); assert!((assigns_0.with(|rc| *rc.borrow()) == 1)); @@ -163,7 +161,11 @@ fn main_0() -> i32 { ({ PartialImpl::copy_assign(&a.as_pointer(), a.as_pointer()) }); assert!((assigns_0.with(|rc| *rc.borrow()) == 3)); ({ - let _o: Value = Rc::new(RefCell::new(Partial::new({ 9 }, { 900 }))); + let _o: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + PartialImpl::construct(&__v.as_pointer(), { 9 }, { 900 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); PartialImpl::copy_assign(&a.as_pointer(), _o.as_pointer()) }); assert!(({ (*a.borrow()).v } == 9) && ({ (*a.borrow()).keep } == 100)); @@ -176,8 +178,23 @@ fn main_0() -> i32 { assert!(({ (*a.borrow()).v } == 2)); assert!((assigns_0.with(|rc| *rc.borrow()) == 6)); let h: Value = Rc::new(RefCell::new(Holder { - p: Partial::new({ 4 }, { 40 }), - arr: Box::new([Partial::new({ 5 }, { 50 }), Partial::new({ 6 }, { 60 })]), + p: { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + PartialImpl::construct(&__v.as_pointer(), { 4 }, { 40 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }, + arr: Box::new([ + { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + PartialImpl::construct(&__v.as_pointer(), { 5 }, { 50 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }, + { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + PartialImpl::construct(&__v.as_pointer(), { 6 }, { 60 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }, + ]), })); ({ PartialImpl::copy_assign(&field_ptr!(h.as_pointer(), p), b.as_pointer()) }); ({ @@ -192,21 +209,60 @@ fn main_0() -> i32 { && (array_elem!(h.as_pointer(), arr, 1).with(|__s| __s.keep) == 60) ); assert!((assigns_0.with(|rc| *rc.borrow()) == 8)); - let n: Value = Rc::new(RefCell::new(NonConstAssign::new())); - let n1: Value = Rc::new(RefCell::new(NonConstAssign::new())); - let n2: Value = Rc::new(RefCell::new(NonConstAssign::new())); - let cn: Value = Rc::new(RefCell::new(NonConstAssign::new())); + let n: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + NonConstAssignImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); + let n1: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + NonConstAssignImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); + let n2: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + NonConstAssignImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); + let cn: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + NonConstAssignImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); ({ NonConstAssignImpl::operator_assign_2(&n1.as_pointer(), n.as_pointer()) }); ({ NonConstAssignImpl::operator_assign_3(&n2.as_pointer(), cn.as_pointer()) }); assert!(({ (*n1.borrow()).mark } == 1)); assert!(({ (*n2.borrow()).mark } == 10)); - let r: Value = Rc::new(RefCell::new(RefQualified::new())); - let r1: Value = Rc::new(RefCell::new(RefQualified::new())); + let r: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + RefQualifiedImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); + let r1: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + RefQualifiedImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); ({ RefQualifiedImpl::copy_assign(&r1.as_pointer(), r.as_pointer()) }); assert!(({ (*r1.borrow()).mark } == 1)); let h2: Value = Rc::new(RefCell::new(Holder { - p: Partial::new({ 7 }, { 70 }), - arr: Box::new([Partial::new({ 8 }, { 80 }), Partial::new({ 9 }, { 90 })]), + p: { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + PartialImpl::construct(&__v.as_pointer(), { 7 }, { 70 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }, + arr: Box::new([ + { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + PartialImpl::construct(&__v.as_pointer(), { 8 }, { 80 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }, + { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + PartialImpl::construct(&__v.as_pointer(), { 9 }, { 90 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }, + ]), })); ({ HolderImpl::copy_assign(&h2.as_pointer(), h.as_pointer()) }); assert!(({ (*h2.borrow()).p.v } == 2) && ({ (*h2.borrow()).p.keep } == 70)); @@ -215,17 +271,55 @@ fn main_0() -> i32 { && (array_elem!(h2.as_pointer(), arr, 1).with(|__s| __s.keep) == 90) ); assert!((assigns_0.with(|rc| *rc.borrow()) == 11)); - let cc1: Value = Rc::new(RefCell::new(CopyCounted::new({ 1 }))); - let cc2: Value = Rc::new(RefCell::new(CopyCounted::new({ 2 }))); + let cc1: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + CopyCountedImpl::construct(&__v.as_pointer(), { 1 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); + let cc2: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + CopyCountedImpl::construct(&__v.as_pointer(), { 2 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); ({ CopyCountedImpl::copy_assign(&cc1.as_pointer(), cc2.as_pointer()) }); assert!(({ (*cc1.borrow()).v } == 2)); let hc1: Value = Rc::new(RefCell::new(HasCopyCounted { - c: CopyCounted::new({ 1 }), - arr: Box::new([CopyCounted::new({ 2 }), CopyCounted::new({ 3 })]), + c: { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + CopyCountedImpl::construct(&__v.as_pointer(), { 1 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }, + arr: Box::new([ + { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + CopyCountedImpl::construct(&__v.as_pointer(), { 2 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }, + { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + CopyCountedImpl::construct(&__v.as_pointer(), { 3 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }, + ]), })); let hc2: Value = Rc::new(RefCell::new(HasCopyCounted { - c: CopyCounted::new({ 4 }), - arr: Box::new([CopyCounted::new({ 5 }), CopyCounted::new({ 6 })]), + c: { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + CopyCountedImpl::construct(&__v.as_pointer(), { 4 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }, + arr: Box::new([ + { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + CopyCountedImpl::construct(&__v.as_pointer(), { 5 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }, + { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + CopyCountedImpl::construct(&__v.as_pointer(), { 6 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }, + ]), })); ({ HasCopyCountedImpl::copy_assign(&hc1.as_pointer(), hc2.as_pointer()) }); assert!( @@ -237,9 +331,18 @@ fn main_0() -> i32 { return 0; } pub trait CopyCountedImpl { + fn construct(&self, v: i32); + fn copy_from(&self, o: Ptr); fn copy_assign(&self, _a0: Ptr) -> Ptr; } impl CopyCountedImpl for Ptr { + fn construct(&self, mut v: i32) { + field!((*self), v).write(v); + } + fn copy_from(&self, o: Ptr) { + field!((*self), v).write(o.with(|__s| __s.v)); + (*copies_1.with(Value::clone).borrow_mut()).prefix_inc(); + } fn copy_assign(&self, _a0: Ptr) -> Ptr { field!((*self), v).write(_a0.with(|__s| __s.v)); return (*self).clone(); @@ -283,10 +386,14 @@ impl HolderImpl for Ptr { } } pub trait NonConstAssignImpl { + fn construct(&self); fn operator_assign_2(&self, o: Ptr) -> Ptr; fn operator_assign_3(&self, o: Ptr) -> Ptr; } impl NonConstAssignImpl for Ptr { + fn construct(&self) { + field!((*self), mark).write(0); + } fn operator_assign_2(&self, o: Ptr) -> Ptr { field!((*self), mark).write((o.with(|__s| __s.mark) + 1)); return (*self).clone(); @@ -297,9 +404,19 @@ impl NonConstAssignImpl for Ptr { } } pub trait PartialImpl { + fn construct(&self, v: i32, keep: i32); + fn copy_from(&self, o: Ptr); fn copy_assign(&self, o: Ptr) -> Ptr; } impl PartialImpl for Ptr { + fn construct(&self, mut v: i32, mut keep: i32) { + field!((*self), v).write(v); + field!((*self), keep).write(keep); + } + fn copy_from(&self, o: Ptr) { + field!((*self), v).write(o.with(|__s| __s.v)); + field!((*self), keep).write(o.with(|__s| __s.keep)); + } fn copy_assign(&self, o: Ptr) -> Ptr { if ((*self) == (o)) { return (*self).clone(); @@ -310,9 +427,13 @@ impl PartialImpl for Ptr { } } pub trait RefQualifiedImpl { + fn construct(&self); fn copy_assign(&self, o: Ptr) -> Ptr; } impl RefQualifiedImpl for Ptr { + fn construct(&self) { + field!((*self), mark).write(0); + } fn copy_assign(&self, o: Ptr) -> Ptr { field!((*self), mark).write((o.with(|__s| __s.mark) + 1)); return (*self).clone(); diff --git a/tests/unit/out/refcount/copy_ctor.rs b/tests/unit/out/refcount/copy_ctor.rs index ba57343e0c..5191857c33 100644 --- a/tests/unit/out/refcount/copy_ctor.rs +++ b/tests/unit/out/refcount/copy_ctor.rs @@ -9,89 +9,64 @@ use std::rc::{Rc, Weak}; thread_local!( pub static copies_0: Value = Rc::new(RefCell::new(0)); ); -#[derive(Record, ByteRepr, VaArg, FnPtrArg, CppFrom, CopyAssign, Default)] -#[cpp_from(i32, new, value)] +#[derive(Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, CppFrom, CopyAssign, Default)] +#[cpp_from(i32, construct, value)] #[byte_size(4)] pub struct Counted { #[offset(0)] pub v: i32, } -impl Counted { - pub fn new(mut v: i32) -> Self { - Self { v: v } - } - pub fn copy_from(o: Ptr) -> Self { - let __this: Counted = Self { - v: o.with(|__s| __s.v), - }; - (*copies_0.with(Value::clone).borrow_mut()).prefix_inc(); - __this - } -} impl Clone for Counted { fn clone(&self) -> Self { let __src: Value = Rc::new(RefCell::new(Counted { v: self.v.clone() })); - Counted::copy_from(__src.as_pointer()) + let __v: Value = Rc::new(RefCell::new(::zero_init())); + CountedImpl::copy_from(&__v.as_pointer(), __src.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() } } -#[derive(Record, ByteRepr, VaArg, FnPtrArg, CopyAssign)] +#[derive(Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, CopyAssign)] #[byte_size(4)] pub struct NonConst { #[offset(0)] pub mark: i32, } -impl NonConst { - pub fn new() -> Self { - Self { mark: 0 } - } - pub fn new_1(o: Ptr) -> Self { - Self { - mark: (o.with(|__s| __s.mark) + 1), - } - } - pub fn new_2(o: Ptr) -> Self { - Self { - mark: (o.with(|__s| __s.mark) + 10), - } - } -} impl Clone for NonConst { fn clone(&self) -> Self { let __src: Value = Rc::new(RefCell::new(NonConst { mark: self.mark.clone(), })); - NonConst::new_1(__src.as_pointer()) + let __v: Value = Rc::new(RefCell::new(::zero_init())); + NonConstImpl::construct_1(&__v.as_pointer(), __src.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() } } impl Default for NonConst { fn default() -> Self { - { NonConst::new() } + { + { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + NonConstImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + } + } } } -#[derive(Record, ByteRepr, VaArg, FnPtrArg, CppFrom, CopyAssign, Default)] -#[cpp_from(i32, new, value)] +#[derive(Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, CppFrom, CopyAssign, Default)] +#[cpp_from(i32, construct, value)] #[byte_size(4)] pub struct Ignored { #[offset(0)] pub v: i32, } -impl Ignored { - pub fn new(mut v: i32) -> Self { - Self { v: v } - } - pub fn copy_from(_a0: Ptr) -> Self { - let __this: Ignored = Self { v: -1_i32 }; - (*copies_0.with(Value::clone).borrow_mut()).prefix_inc(); - __this - } -} impl Clone for Ignored { fn clone(&self) -> Self { let __src: Value = Rc::new(RefCell::new(Ignored { v: self.v.clone() })); - Ignored::copy_from(__src.as_pointer()) + let __v: Value = Rc::new(RefCell::new(::zero_init())); + IgnoredImpl::copy_from(&__v.as_pointer(), __src.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() } } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, CopyAssign)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, CopyAssign)] #[byte_size(12)] pub struct Holder { #[offset(0)] @@ -99,6 +74,7 @@ pub struct Holder { pub c: Counted, #[offset(4)] #[byte_size(8)] + #[array_len(2)] pub arr: Box<[Counted]>, } impl Default for Holder { @@ -113,97 +89,60 @@ impl Default for Holder { } } } -#[derive(Record, ByteRepr, VaArg, FnPtrArg, MoveCtor, CppFrom, Default)] -#[cpp_from(i32, new, value)] +#[derive(Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, MoveCtor, CppFrom, Default)] +#[cpp_from(i32, construct, value)] #[byte_size(4)] pub struct Box_int_ { #[offset(0)] pub val: i32, } -impl Box_int_ { - pub fn new(mut v: i32) -> Self { - Self { val: v } - } - pub fn copy_from(o: Ptr) -> Self { - Self { - val: o.with(|__s| __s.val), - } - } - pub fn move_from(_a0: Ptr) -> Self { - Self { - val: { (*_a0.upgrade().deref()).val }, - } - } -} impl Clone for Box_int_ { fn clone(&self) -> Self { let __src: Value = Rc::new(RefCell::new(Box_int_ { val: self.val.clone(), })); - Box_int_::copy_from(__src.as_pointer()) + let __v: Value = Rc::new(RefCell::new(::zero_init())); + Box_int_Impl::copy_from(&__v.as_pointer(), __src.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() } } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, MoveCtor, Default)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, MoveCtor, Default)] #[byte_size(4)] pub struct Owner { #[offset(0)] #[byte_size(4)] pub box_: Box_int_, } -impl Owner { - pub fn move_from(_a0: Ptr) -> Self { - Self { - box_: Box_int_::move_from({ field_ptr!(_a0, box_) }), - } - } -} -#[derive(Record, ByteRepr, VaArg, FnPtrArg, CppFrom, CopyAssign, Default)] -#[cpp_from(i32, new, value)] +#[derive(Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, CppFrom, CopyAssign, Default)] +#[cpp_from(i32, construct, value)] #[byte_size(4)] pub struct OutOfLine { #[offset(0)] pub v: i32, } -impl OutOfLine { - pub fn new(mut v: i32) -> Self { - Self { v: v } - } - pub fn copy_from(o: Ptr) -> Self { - let __this: OutOfLine = Self { - v: (o.with(|__s| __s.v) + 100), - }; - (*copies_0.with(Value::clone).borrow_mut()).prefix_inc(); - __this - } -} impl Clone for OutOfLine { fn clone(&self) -> Self { let __src: Value = Rc::new(RefCell::new(OutOfLine { v: self.v.clone() })); - OutOfLine::copy_from(__src.as_pointer()) + let __v: Value = Rc::new(RefCell::new(::zero_init())); + OutOfLineImpl::copy_from(&__v.as_pointer(), __src.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() } } impl OutOfLine {} -#[derive(Record, ByteRepr, VaArg, FnPtrArg, CopyAssign)] +#[derive(Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, CopyAssign)] #[byte_size(4)] pub struct Tracked { #[offset(0)] pub copied_from: i32, } -impl Tracked { - pub fn copy_from(o: Ptr) -> Self { - let __this: Tracked = Self { copied_from: 0 }; - field!(o, copied_from).with_mut(|__v| { - __v.prefix_inc(); - }); - __this - } -} impl Clone for Tracked { fn clone(&self) -> Self { let __src: Value = Rc::new(RefCell::new(Tracked { copied_from: self.copied_from.clone(), })); - Tracked::copy_from(__src.as_pointer()) + let __v: Value = Rc::new(RefCell::new(::zero_init())); + TrackedImpl::copy_from(&__v.as_pointer(), __src.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() } } impl Default for Tracked { @@ -220,35 +159,94 @@ pub fn by_value_2(mut c: Counted) -> i32 { return c.v; } pub fn make_3(mut v: i32) -> Counted { - let c: Value = Rc::new(RefCell::new(Counted::new({ v }))); - return Counted::copy_from({ c.as_pointer() }); + let c: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + CountedImpl::construct(&__v.as_pointer(), { v }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); + return { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + CountedImpl::copy_from(&__v.as_pointer(), { c.as_pointer() }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }; } pub fn main() { __cpp2rust_init_globals(); libcc2rs::cpp2rust_exit(main_0()); } fn main_0() -> i32 { - let a: Value = Rc::new(RefCell::new(Counted::new({ 1 }))); - let mut b: Counted = Counted::copy_from({ a.as_pointer() }); - let mut c: Counted = Counted::copy_from({ a.as_pointer() }); - let mut d: Counted = Counted::copy_from({ a.as_pointer() }); + let a: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + CountedImpl::construct(&__v.as_pointer(), { 1 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); + let mut b: Counted = { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + CountedImpl::copy_from(&__v.as_pointer(), { a.as_pointer() }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }; + let mut c: Counted = { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + CountedImpl::copy_from(&__v.as_pointer(), { a.as_pointer() }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }; + let mut d: Counted = { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + CountedImpl::copy_from(&__v.as_pointer(), { a.as_pointer() }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }; assert!((copies_0.with(|rc| *rc.borrow()) == 3)); assert!(((b.v == 1) && (c.v == 1)) && (d.v == 1)); - assert!((({ by_value_2(Counted::copy_from({ a.as_pointer() },),) }) == 1)); + assert!( + (({ + by_value_2({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + CountedImpl::copy_from(&__v.as_pointer(), { a.as_pointer() }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }) + }) == 1) + ); assert!((copies_0.with(|rc| *rc.borrow()) == 4)); let mut e: Counted = ({ make_3(5) }); assert!((e.v == 5)); assert!((copies_0.with(|rc| *rc.borrow()) == 5)); - let mut f: Counted = Counted::new({ 6 }); + let mut f: Counted = { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + CountedImpl::construct(&__v.as_pointer(), { 6 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }; assert!((f.v == 6)); assert!((copies_0.with(|rc| *rc.borrow()) == 5)); - let g: Value = Rc::new(RefCell::new(Counted::new({ 7 }))); - let mut h: Counted = Counted::copy_from({ g.as_pointer() }); + let g: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + CountedImpl::construct(&__v.as_pointer(), { 7 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); + let mut h: Counted = { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + CountedImpl::copy_from(&__v.as_pointer(), { g.as_pointer() }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }; assert!((h.v == 7)); assert!((copies_0.with(|rc| *rc.borrow()) == 6)); let hold: Value = Rc::new(RefCell::new(Holder { - c: Counted::new({ 8 }), - arr: Box::new([Counted::new({ 9 }), Counted::new({ 10 })]), + c: { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + CountedImpl::construct(&__v.as_pointer(), { 8 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }, + arr: Box::new([ + { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + CountedImpl::construct(&__v.as_pointer(), { 9 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }, + { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + CountedImpl::construct(&__v.as_pointer(), { 10 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }, + ]), })); let mut hold2: Holder = (*hold.borrow()).clone(); assert!( @@ -262,39 +260,112 @@ fn main_0() -> i32 { }; assert!((vec_[0_usize].v == 1)); assert!((copies_0.with(|rc| *rc.borrow()) == 10)); - let i1: Value = Rc::new(RefCell::new(Ignored::new({ 1 }))); - let mut i2: Ignored = Ignored::copy_from({ i1.as_pointer() }); + let i1: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + IgnoredImpl::construct(&__v.as_pointer(), { 1 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); + let mut i2: Ignored = { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + IgnoredImpl::copy_from(&__v.as_pointer(), { i1.as_pointer() }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }; assert!(({ (*i1.borrow()).v } == 1) && (i2.v == -1_i32)); assert!((copies_0.with(|rc| *rc.borrow()) == 11)); let o: Value = Rc::new(RefCell::new(Owner { - box_: Box_int_::new({ 3 }), + box_: { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + Box_int_Impl::construct(&__v.as_pointer(), { 3 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }, })); - let mut o2: Owner = Owner::move_from({ o.as_pointer() }); + let mut o2: Owner = { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + OwnerImpl::move_from(&__v.as_pointer(), { o.as_pointer() }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }; assert!((o2.box_.val == 3)); - let ol1: Value = Rc::new(RefCell::new(OutOfLine::new({ 5 }))); - let mut ol2: OutOfLine = OutOfLine::copy_from({ ol1.as_pointer() }); + let ol1: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + OutOfLineImpl::construct(&__v.as_pointer(), { 5 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); + let mut ol2: OutOfLine = { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + OutOfLineImpl::copy_from(&__v.as_pointer(), { ol1.as_pointer() }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }; assert!((ol2.v == 105)); assert!((copies_0.with(|rc| *rc.borrow()) == 12)); - let n: Value = Rc::new(RefCell::new(NonConst::new())); - let mut n1: NonConst = NonConst::new_1({ n.as_pointer() }); - let cn: Value = Rc::new(RefCell::new(NonConst::new())); - let mut n2: NonConst = NonConst::new_2({ cn.as_pointer() }); + let n: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + NonConstImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); + let mut n1: NonConst = { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + NonConstImpl::construct_1(&__v.as_pointer(), { n.as_pointer() }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }; + let cn: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + NonConstImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); + let mut n2: NonConst = { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + NonConstImpl::construct_2(&__v.as_pointer(), { cn.as_pointer() }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }; assert!((n1.mark == 1)); assert!((n2.mark == 10)); let t1: Value = Rc::new(RefCell::new(::default())); - let t2: Value = Rc::new(RefCell::new(Tracked::copy_from({ t1.as_pointer() }))); + let t2: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + TrackedImpl::copy_from(&__v.as_pointer(), { t1.as_pointer() }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); ({ TrackedImpl::copy_assign(&t2.as_pointer(), t1.as_pointer()) }); assert!(({ (*t1.borrow()).copied_from } == 2)); let ct: Value = Rc::new(RefCell::new(::default())); - let mut t3: Tracked = Tracked::copy_from({ ct.as_pointer() }); + let mut t3: Tracked = { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + TrackedImpl::copy_from(&__v.as_pointer(), { ct.as_pointer() }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }; ({ touch_1((ct.as_pointer())) }); assert!(({ (*ct.borrow()).copied_from } == 2)); return 0; } +pub trait Box_int_Impl { + fn construct(&self, v: i32); + fn copy_from(&self, o: Ptr); + fn move_from(&self, _a0: Ptr); +} +impl Box_int_Impl for Ptr { + fn construct(&self, mut v: i32) { + field!((*self), val).write(v); + } + fn copy_from(&self, o: Ptr) { + field!((*self), val).write(o.with(|__s| __s.val)); + } + fn move_from(&self, _a0: Ptr) { + field!((*self), val).write({ (*_a0.upgrade().deref()).val }); + } +} pub trait CountedImpl { + fn construct(&self, v: i32); + fn copy_from(&self, o: Ptr); fn copy_assign(&self, _a0: Ptr) -> Ptr; } impl CountedImpl for Ptr { + fn construct(&self, mut v: i32) { + field!((*self), v).write(v); + } + fn copy_from(&self, o: Ptr) { + field!((*self), v).write(o.with(|__s| __s.v)); + (*copies_0.with(Value::clone).borrow_mut()).prefix_inc(); + } fn copy_assign(&self, _a0: Ptr) -> Ptr { field!((*self), v).write(_a0.with(|__s| __s.v)); return (*self).clone(); @@ -315,36 +386,85 @@ impl HolderImpl for Ptr { } } pub trait IgnoredImpl { + fn construct(&self, v: i32); + fn copy_from(&self, _a0: Ptr); fn copy_assign(&self, _a0: Ptr) -> Ptr; } impl IgnoredImpl for Ptr { + fn construct(&self, mut v: i32) { + field!((*self), v).write(v); + } + fn copy_from(&self, _a0: Ptr) { + field!((*self), v).write(-1_i32); + (*copies_0.with(Value::clone).borrow_mut()).prefix_inc(); + } fn copy_assign(&self, _a0: Ptr) -> Ptr { field!((*self), v).write(_a0.with(|__s| __s.v)); return (*self).clone(); } } pub trait NonConstImpl { + fn construct(&self); + fn construct_1(&self, o: Ptr); + fn construct_2(&self, o: Ptr); fn copy_assign(&self, _a0: Ptr) -> Ptr; } impl NonConstImpl for Ptr { + fn construct(&self) { + field!((*self), mark).write(0); + } + fn construct_1(&self, o: Ptr) { + field!((*self), mark).write((o.with(|__s| __s.mark) + 1)); + } + fn construct_2(&self, o: Ptr) { + field!((*self), mark).write((o.with(|__s| __s.mark) + 10)); + } fn copy_assign(&self, _a0: Ptr) -> Ptr { field!((*self), mark).write(_a0.with(|__s| __s.mark)); return (*self).clone(); } } pub trait OutOfLineImpl { + fn construct(&self, v: i32); + fn copy_from(&self, o: Ptr); fn copy_assign(&self, _a0: Ptr) -> Ptr; } impl OutOfLineImpl for Ptr { + fn construct(&self, mut v: i32) { + field!((*self), v).write(v); + } + fn copy_from(&self, o: Ptr) { + field!((*self), v).write((o.with(|__s| __s.v) + 100)); + (*copies_0.with(Value::clone).borrow_mut()).prefix_inc(); + } fn copy_assign(&self, _a0: Ptr) -> Ptr { field!((*self), v).write(_a0.with(|__s| __s.v)); return (*self).clone(); } } +pub trait OwnerImpl { + fn move_from(&self, _a0: Ptr); +} +impl OwnerImpl for Ptr { + fn move_from(&self, _a0: Ptr) { + field!((*self), box_).write({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + Box_int_Impl::move_from(&__v.as_pointer(), { field_ptr!(_a0, box_) }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }); + } +} pub trait TrackedImpl { + fn copy_from(&self, o: Ptr); fn copy_assign(&self, o: Ptr) -> Ptr; } impl TrackedImpl for Ptr { + fn copy_from(&self, o: Ptr) { + field!((*self), copied_from).write(0); + field!(o, copied_from).with_mut(|__v| { + __v.prefix_inc(); + }); + } fn copy_assign(&self, o: Ptr) -> Ptr { field!(o, copied_from).with_mut(|__v| { __v.prefix_inc(); diff --git a/tests/unit/out/refcount/copy_move_defaulted.rs b/tests/unit/out/refcount/copy_move_defaulted.rs index 7992b0b1ec..dd1836bdcc 100644 --- a/tests/unit/out/refcount/copy_move_defaulted.rs +++ b/tests/unit/out/refcount/copy_move_defaulted.rs @@ -6,14 +6,14 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct Inner { #[offset(0)] pub x: i32, } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, CppFrom, Destructible)] -#[cpp_from(i32, new, value)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, CppFrom, Destructible)] +#[cpp_from(i32, construct, value)] #[byte_size(16)] pub struct Explicit { #[offset(0)] @@ -23,29 +23,15 @@ pub struct Explicit { pub inner: Inner, #[offset(8)] #[byte_size(8)] + #[array_len(2)] pub arr: Box<[i32]>, } -impl Explicit { - pub fn new(mut v: i32) -> Self { - Self { - v: v, - inner: Inner { x: (v * 10) }, - arr: Box::new([v, (v + 1)]), - } - } -} impl Default for Explicit { fn default() -> Self { - { - Explicit { - v: 0_i32, - inner: ::default(), - arr: (0..2).map(|_| 0_i32).collect::>(), - } - } + { ::zero_init() } } } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg)] #[byte_size(16)] pub struct Implicit { #[offset(0)] @@ -55,6 +41,7 @@ pub struct Implicit { pub inner: Inner, #[offset(8)] #[byte_size(8)] + #[array_len(2)] pub arr: Box<[i32]>, } impl Default for Implicit { @@ -69,57 +56,54 @@ impl Default for Implicit { } } #[derive( - Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, MoveCtor, CppFrom, MoveAssign, Default, + Copy, + Clone, + Record, + ByteRepr, + ZeroInitialization, + VaArg, + FnPtrArg, + MoveCtor, + CppFrom, + MoveAssign, + Default, )] -#[cpp_from(i32, new, value)] +#[cpp_from(i32, construct, value)] #[byte_size(4)] pub struct DefaultCopyUserMove { #[offset(0)] pub v: i32, } -impl DefaultCopyUserMove { - pub fn new(mut v: i32) -> Self { - Self { v: v } - } - pub fn move_from(o: Ptr) -> Self { - let __this: DefaultCopyUserMove = Self { - v: o.with(|__s| __s.v), - }; - field!(o, v).write(0); - __this - } -} -#[derive(Record, ByteRepr, VaArg, FnPtrArg, MoveCtor, CppFrom, CopyAssign, MoveAssign, Default)] -#[cpp_from(i32, new, value)] +#[derive( + Record, + ByteRepr, + ZeroInitialization, + VaArg, + FnPtrArg, + MoveCtor, + CppFrom, + CopyAssign, + MoveAssign, + Default, +)] +#[cpp_from(i32, construct, value)] #[byte_size(4)] pub struct UserCopyDefaultMove { #[offset(0)] pub v: i32, } -impl UserCopyDefaultMove { - pub fn new(mut v: i32) -> Self { - Self { v: v } - } - pub fn copy_from(o: Ptr) -> Self { - Self { - v: (o.with(|__s| __s.v) + 100), - } - } - pub fn move_from(_a0: Ptr) -> Self { - Self { - v: { (*_a0.upgrade().deref()).v }, - } - } -} impl Clone for UserCopyDefaultMove { fn clone(&self) -> Self { let __src: Value = Rc::new(RefCell::new(UserCopyDefaultMove { v: self.v.clone() })); - UserCopyDefaultMove::copy_from(__src.as_pointer()) + let __v: Value = + Rc::new(RefCell::new(::zero_init())); + UserCopyDefaultMoveImpl::copy_from(&__v.as_pointer(), __src.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() } } -#[derive(Record, ByteRepr, FnPtrArg, MoveCtor, CppFrom, MoveAssign)] -#[cpp_from(i32, new, value)] +#[derive(Record, ByteRepr, ZeroInitialization, FnPtrArg, MoveCtor, CppFrom, MoveAssign)] +#[cpp_from(i32, construct, value)] #[byte_size(64)] pub struct Buffer { #[offset(0)] @@ -132,47 +116,15 @@ pub struct Buffer { pub n: i32, #[offset(52)] #[byte_size(8)] + #[array_len(2)] pub arr: Box<[i32]>, } -impl Buffer { - pub fn new(mut n: i32) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - data: vec![n; (n as usize)], - rows: Vec::new(), - n: n, - arr: Box::new([n, (n + 1)]), - })); - let this: Ptr = __this.as_pointer(); - { - let a0_clone = (*this.upgrade().deref()).data.clone(); - field!(this, rows).with_mut(|__v: &mut Vec>| __v.push(a0_clone)) - }; - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } - pub fn move_from(_a0: Ptr) -> Self { - Self { - data: field!(_a0, data).with_mut(std::mem::take), - rows: field!(_a0, rows).with_mut(std::mem::take), - n: { (*_a0.upgrade().deref()).n }, - arr: Box::new(std::array::from_fn::<_, 2, _>(|__i: usize| { - (*_a0.upgrade().deref()).arr[(__i) as usize] - })), - } - } -} impl Default for Buffer { fn default() -> Self { - { - Buffer { - data: Default::default(), - rows: Default::default(), - n: 0_i32, - arr: (0..2).map(|_| 0_i32).collect::>(), - } - } + { ::zero_init() } } } -#[derive(Record, ByteRepr, FnPtrArg, MoveCtor, MoveAssign)] +#[derive(Record, ByteRepr, ZeroInitialization, FnPtrArg, MoveCtor, MoveAssign)] #[byte_size(48)] pub struct Owner { #[offset(0)] @@ -182,23 +134,12 @@ pub struct Owner { pub n: i32, #[offset(28)] #[byte_size(8)] + #[array_len(2)] pub arr: Box<[i32]>, #[offset(40)] #[byte_size(8)] pub p: Option>, } -impl Owner { - pub fn move_from(_a0: Ptr) -> Self { - Self { - data: field!(_a0, data).with_mut(std::mem::take), - n: { (*_a0.upgrade().deref()).n }, - arr: Box::new(std::array::from_fn::<_, 2, _>(|__i: usize| { - (*_a0.upgrade().deref()).arr[(__i) as usize] - })), - p: field!(_a0, p).with_mut(|__v: &mut Option>| __v.take()), - } - } -} impl Default for Owner { fn default() -> Self { { @@ -211,22 +152,27 @@ impl Default for Owner { } } } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, MoveCtor, MoveAssign, Default)] +#[derive( + Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, MoveCtor, MoveAssign, Default, +)] #[byte_size(24)] pub struct Copyable { #[offset(0)] #[byte_size(24)] pub data: Vec, } -impl Copyable { - pub fn move_from(_a0: Ptr) -> Self { - Self { - data: field!(_a0, data).with_mut(std::mem::take), - } - } -} -#[derive(Record, ByteRepr, FnPtrArg, MoveCtor, CppFrom, MoveAssign, Destructible, Default)] -#[cpp_from(i32, new, value)] +#[derive( + Record, + ByteRepr, + ZeroInitialization, + FnPtrArg, + MoveCtor, + CppFrom, + MoveAssign, + Destructible, + Default, +)] +#[cpp_from(i32, construct, value)] #[byte_size(32)] pub struct Holder { #[offset(0)] @@ -239,22 +185,6 @@ pub struct Holder { #[byte_size(8)] pub p: Option>, } -impl Holder { - pub fn new(mut v: i32) -> Self { - Self { - inner: Inner { x: v }, - e: Explicit::new({ v }), - p: None, - } - } - pub fn move_from(_a0: Ptr) -> Self { - Self { - inner: { (*_a0.upgrade().deref()).inner }, - e: { (*_a0.upgrade().deref()).e.clone() }, - p: field!(_a0, p).with_mut(|__v: &mut Option>| __v.take()), - } - } -} pub fn same_0(a: Ptr, b: Ptr) -> bool { return ((({ a.with(|__s| __s.v) } == { b.with(|__s| __s.v) }) && ({ a.with(|__s| __s.inner.x) } == { b.with(|__s| __s.inner.x) })) @@ -266,7 +196,11 @@ pub fn main() { libcc2rs::cpp2rust_exit(main_0()); } fn main_0() -> i32 { - let a: Value = Rc::new(RefCell::new(Explicit::new({ 1 }))); + let a: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + ExplicitImpl::construct(&__v.as_pointer(), { 1 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); let _dtor_a = std::mem::DropGuard::new(a.clone(), |__p| Destructible::destroy(__p.as_pointer())); let b: Value = Rc::new(RefCell::new((*a.borrow()).clone())); @@ -283,10 +217,18 @@ fn main_0() -> i32 { && ({ same_0(c.as_pointer(), a.as_pointer(),) })) && ({ same_0(d.as_pointer(), a.as_pointer(),) }) ); - let e: Value = Rc::new(RefCell::new(Explicit::new({ 2 }))); + let e: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + ExplicitImpl::construct(&__v.as_pointer(), { 2 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); let _dtor_e = std::mem::DropGuard::new(e.clone(), |__p| Destructible::destroy(__p.as_pointer())); - let f: Value = Rc::new(RefCell::new(Explicit::new({ 3 }))); + let f: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + ExplicitImpl::construct(&__v.as_pointer(), { 3 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); let _dtor_f = std::mem::DropGuard::new(f.clone(), |__p| Destructible::destroy(__p.as_pointer())); (*e.borrow_mut()) = (*b.borrow()).clone(); @@ -295,7 +237,11 @@ fn main_0() -> i32 { ({ same_0(e.as_pointer(), b.as_pointer(),) }) && ({ same_0(f.as_pointer(), c.as_pointer(),) }) ); - let g: Value = Rc::new(RefCell::new(Explicit::new({ 4 }))); + let g: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + ExplicitImpl::construct(&__v.as_pointer(), { 4 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); let _dtor_g = std::mem::DropGuard::new(g.clone(), |__p| Destructible::destroy(__p.as_pointer())); let __rhs = { @@ -329,32 +275,87 @@ fn main_0() -> i32 { vec_.push(a0_clone) }; { - let __a1 = Explicit::new({ 9 }); + let __a1 = { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + ExplicitImpl::construct(&__v.as_pointer(), { 9 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }; vec_.push(__a1) }; assert!((vec_[0_usize].v == 1) && (vec_[1_usize].v == 9)); - let m: Value = Rc::new(RefCell::new(DefaultCopyUserMove::new({ 7 }))); + let m: Value = Rc::new(RefCell::new({ + let __v: Value = + Rc::new(RefCell::new(::zero_init())); + DefaultCopyUserMoveImpl::construct(&__v.as_pointer(), { 7 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); let m1: Value = Rc::new(RefCell::new((*m.borrow()))); - let mut m2: DefaultCopyUserMove = DefaultCopyUserMove::move_from({ m.as_pointer() }); + let mut m2: DefaultCopyUserMove = { + let __v: Value = + Rc::new(RefCell::new(::zero_init())); + DefaultCopyUserMoveImpl::move_from(&__v.as_pointer(), { m.as_pointer() }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }; assert!((({ (*m1.borrow()).v } == 7) && (m2.v == 7)) && ({ (*m.borrow()).v } == 0)); - let mut m3: DefaultCopyUserMove = DefaultCopyUserMove::new({ 1 }); - let m4: Value = Rc::new(RefCell::new(DefaultCopyUserMove::new({ 1 }))); + let mut m3: DefaultCopyUserMove = { + let __v: Value = + Rc::new(RefCell::new(::zero_init())); + DefaultCopyUserMoveImpl::construct(&__v.as_pointer(), { 1 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }; + let m4: Value = Rc::new(RefCell::new({ + let __v: Value = + Rc::new(RefCell::new(::zero_init())); + DefaultCopyUserMoveImpl::construct(&__v.as_pointer(), { 1 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); m3 = (*m1.borrow()); ({ DefaultCopyUserMoveImpl::move_assign(&m4.as_pointer(), m1.as_pointer()) }); assert!(((m3.v == 7) && ({ (*m4.borrow()).v } == 7)) && ({ (*m1.borrow()).v } == 0)); - let u: Value = Rc::new(RefCell::new(UserCopyDefaultMove::new({ 8 }))); - let mut u1: UserCopyDefaultMove = UserCopyDefaultMove::copy_from({ u.as_pointer() }); - let u2: Value = Rc::new(RefCell::new(UserCopyDefaultMove::move_from({ - u.as_pointer() - }))); + let u: Value = Rc::new(RefCell::new({ + let __v: Value = + Rc::new(RefCell::new(::zero_init())); + UserCopyDefaultMoveImpl::construct(&__v.as_pointer(), { 8 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); + let mut u1: UserCopyDefaultMove = { + let __v: Value = + Rc::new(RefCell::new(::zero_init())); + UserCopyDefaultMoveImpl::copy_from(&__v.as_pointer(), { u.as_pointer() }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }; + let u2: Value = Rc::new(RefCell::new({ + let __v: Value = + Rc::new(RefCell::new(::zero_init())); + UserCopyDefaultMoveImpl::move_from(&__v.as_pointer(), { u.as_pointer() }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); assert!(((u1.v == 108) && ({ (*u2.borrow()).v } == 8)) && ({ (*u.borrow()).v } == 8)); - let u3: Value = Rc::new(RefCell::new(UserCopyDefaultMove::new({ 1 }))); - let u4: Value = Rc::new(RefCell::new(UserCopyDefaultMove::new({ 1 }))); + let u3: Value = Rc::new(RefCell::new({ + let __v: Value = + Rc::new(RefCell::new(::zero_init())); + UserCopyDefaultMoveImpl::construct(&__v.as_pointer(), { 1 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); + let u4: Value = Rc::new(RefCell::new({ + let __v: Value = + Rc::new(RefCell::new(::zero_init())); + UserCopyDefaultMoveImpl::construct(&__v.as_pointer(), { 1 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); ({ UserCopyDefaultMoveImpl::copy_assign(&u3.as_pointer(), u2.as_pointer()) }); ({ UserCopyDefaultMoveImpl::move_assign(&u4.as_pointer(), u2.as_pointer()) }); assert!(({ (*u3.borrow()).v } == 108) && ({ (*u4.borrow()).v } == 8)); - let p: Value = Rc::new(RefCell::new(Buffer::new({ 3 }))); - let q: Value = Rc::new(RefCell::new(Buffer::move_from({ p.as_pointer() }))); + let p: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + BufferImpl::construct(&__v.as_pointer(), { 3 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); + let q: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + BufferImpl::move_from(&__v.as_pointer(), { p.as_pointer() }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); assert!( ({ ((({ (*q.borrow()).n } == 3) && ((*q.borrow()).data.len() == 3_usize)) @@ -362,7 +363,11 @@ fn main_0() -> i32 { && ((*p.borrow()).data.is_empty()) }) ); - let r: Value = Rc::new(RefCell::new(Buffer::new({ 1 }))); + let r: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + BufferImpl::construct(&__v.as_pointer(), { 1 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); ({ BufferImpl::move_assign(&r.as_pointer(), q.as_pointer()) }); assert!( ({ @@ -386,11 +391,21 @@ fn main_0() -> i32 { ); let bufs: Value> = Rc::new(RefCell::new(Vec::new())); { - let __a1 = Buffer::move_from({ r.as_pointer() }); + let __a1 = { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + BufferImpl::move_from(&__v.as_pointer(), { r.as_pointer() }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }; (*bufs.borrow_mut()).push(__a1) }; { - let mut __tmp_2 = { Buffer::move_from({ bufs.decay().offset(0_usize) }) }; + let mut __tmp_2 = { + { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + BufferImpl::move_from(&__v.as_pointer(), { bufs.decay().offset(0_usize) }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + } + }; (*bufs.borrow_mut()).push(__tmp_2); }; assert!( @@ -412,7 +427,11 @@ fn main_0() -> i32 { let _p: Ptr<_> = Ptr::alloc(7); field_ptr!(o1.as_pointer(), p).write(_p.to_owned_opt()) }; - let o2: Value = Rc::new(RefCell::new(Owner::move_from({ o1.as_pointer() }))); + let o2: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + OwnerImpl::move_from(&__v.as_pointer(), { o1.as_pointer() }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); assert!( ({ ((({ (*o2.borrow()).n } == 5) && ((*o2.borrow()).data.len() == 1_usize)) @@ -444,7 +463,11 @@ fn main_0() -> i32 { data: Vec::from([1, 2]), })); let mut c2: Copyable = (*c1.borrow()).clone(); - let c3: Value = Rc::new(RefCell::new(Copyable::move_from({ c1.as_pointer() }))); + let c3: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + CopyableImpl::move_from(&__v.as_pointer(), { c1.as_pointer() }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); assert!(({ (c2.data.len() == 2_usize) && ((*c3.borrow()).data.len() == 2_usize) })); assert!(({ (*c1.borrow()).data.is_empty() })); let c4: Value = Rc::new(RefCell::new(::default())); @@ -455,14 +478,22 @@ fn main_0() -> i32 { && ((*c3.borrow()).data.is_empty()) }) ); - let h1: Value = Rc::new(RefCell::new(Holder::new({ 4 }))); + let h1: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + HolderImpl::construct(&__v.as_pointer(), { 4 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); let _dtor_h1 = std::mem::DropGuard::new(h1.clone(), |__p| Destructible::destroy(__p.as_pointer())); { let _p: Ptr<_> = Ptr::alloc(9); field_ptr!(h1.as_pointer(), p).write(_p.to_owned_opt()) }; - let h2: Value = Rc::new(RefCell::new(Holder::move_from({ h1.as_pointer() }))); + let h2: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + HolderImpl::move_from(&__v.as_pointer(), { h1.as_pointer() }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); let _dtor_h2 = std::mem::DropGuard::new(h2.clone(), |__p| Destructible::destroy(__p.as_pointer())); assert!( @@ -470,7 +501,11 @@ fn main_0() -> i32 { && (({ (*h2.borrow()).p.clone() }.as_pointer().read()) == 9)) && (({ (*h1.borrow()).p.clone() }.as_pointer()).is_null()) ); - let h3: Value = Rc::new(RefCell::new(Holder::new({ 1 }))); + let h3: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + HolderImpl::construct(&__v.as_pointer(), { 1 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); let _dtor_h3 = std::mem::DropGuard::new(h3.clone(), |__p| Destructible::destroy(__p.as_pointer())); ({ HolderImpl::move_assign(&h3.as_pointer(), h2.as_pointer()) }); @@ -483,9 +518,29 @@ fn main_0() -> i32 { return 0; } pub trait BufferImpl { + fn construct(&self, n: i32); + fn move_from(&self, _a0: Ptr); fn move_assign(&self, _a0: Ptr) -> Ptr; } impl BufferImpl for Ptr { + fn construct(&self, mut n: i32) { + field!((*self), data).write(vec![n; (n as usize)]); + field!((*self), rows).write(Vec::new()); + field!((*self), n).write(n); + field!((*self), arr).write(Box::new([n, (n + 1)])); + { + let a0_clone = (*(*self).upgrade().deref()).data.clone(); + field!((*self), rows).with_mut(|__v: &mut Vec>| __v.push(a0_clone)) + }; + } + fn move_from(&self, _a0: Ptr) { + field!((*self), data).write(field!(_a0, data).with_mut(std::mem::take)); + field!((*self), rows).write(field!(_a0, rows).with_mut(std::mem::take)); + field!((*self), n).write({ (*_a0.upgrade().deref()).n }); + field!((*self), arr).write(Box::new(std::array::from_fn::<_, 2, _>(|__i: usize| { + (*_a0.upgrade().deref()).arr[(__i) as usize] + }))); + } fn move_assign(&self, _a0: Ptr) -> Ptr { field!((*self), data).with_mut(|__b0| { (*__b0) = field!(_a0, data).with_mut(std::mem::take); @@ -503,9 +558,13 @@ impl BufferImpl for Ptr { } } pub trait CopyableImpl { + fn move_from(&self, _a0: Ptr); fn move_assign(&self, _a0: Ptr) -> Ptr; } impl CopyableImpl for Ptr { + fn move_from(&self, _a0: Ptr) { + field!((*self), data).write(field!(_a0, data).with_mut(std::mem::take)); + } fn move_assign(&self, _a0: Ptr) -> Ptr { field!((*self), data).with_mut(|__b0| { (*__b0) = field!(_a0, data).with_mut(std::mem::take); @@ -514,9 +573,18 @@ impl CopyableImpl for Ptr { } } pub trait DefaultCopyUserMoveImpl { + fn construct(&self, v: i32); + fn move_from(&self, o: Ptr); fn move_assign(&self, o: Ptr) -> Ptr; } impl DefaultCopyUserMoveImpl for Ptr { + fn construct(&self, mut v: i32) { + field!((*self), v).write(v); + } + fn move_from(&self, o: Ptr) { + field!((*self), v).write(o.with(|__s| __s.v)); + field!(o, v).write(0); + } fn move_assign(&self, o: Ptr) -> Ptr { field!((*self), v).write(o.with(|__s| __s.v)); field!(o, v).write(0); @@ -524,16 +592,39 @@ impl DefaultCopyUserMoveImpl for Ptr { } } pub trait ExplicitImpl { + fn construct(&self, v: i32); fn destructor(&self); } impl ExplicitImpl for Ptr { + fn construct(&self, mut v: i32) { + field!((*self), v).write(v); + field!((*self), inner).write(Inner { x: (v * 10) }); + field!((*self), arr).write(Box::new([v, (v + 1)])); + } fn destructor(&self) {} } pub trait HolderImpl { + fn construct(&self, v: i32); + fn move_from(&self, _a0: Ptr); fn move_assign(&self, _a0: Ptr) -> Ptr; fn destructor(&self); } impl HolderImpl for Ptr { + fn construct(&self, mut v: i32) { + field!((*self), inner).write(Inner { x: v }); + field!((*self), e).write({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + ExplicitImpl::construct(&__v.as_pointer(), { v }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }); + field!((*self), p).write(None); + } + fn move_from(&self, _a0: Ptr) { + field!((*self), inner).write({ (*_a0.upgrade().deref()).inner }); + field!((*self), e).write({ (*_a0.upgrade().deref()).e.clone() }); + field!((*self), p) + .write(field!(_a0, p).with_mut(|__v: &mut Option>| __v.take())); + } fn move_assign(&self, _a0: Ptr) -> Ptr { field!((*self), inner).write({ (*_a0.upgrade().deref()).inner }); field!((*self), e).write({ (*_a0.upgrade().deref()).e.clone() }); @@ -547,9 +638,19 @@ impl HolderImpl for Ptr { } } pub trait OwnerImpl { + fn move_from(&self, _a0: Ptr); fn move_assign(&self, _a0: Ptr) -> Ptr; } impl OwnerImpl for Ptr { + fn move_from(&self, _a0: Ptr) { + field!((*self), data).write(field!(_a0, data).with_mut(std::mem::take)); + field!((*self), n).write({ (*_a0.upgrade().deref()).n }); + field!((*self), arr).write(Box::new(std::array::from_fn::<_, 2, _>(|__i: usize| { + (*_a0.upgrade().deref()).arr[(__i) as usize] + }))); + field!((*self), p) + .write(field!(_a0, p).with_mut(|__v: &mut Option>| __v.take())); + } fn move_assign(&self, _a0: Ptr) -> Ptr { field!((*self), data).with_mut(|__b0| { (*__b0) = field!(_a0, data).with_mut(std::mem::take); @@ -567,10 +668,22 @@ impl OwnerImpl for Ptr { } } pub trait UserCopyDefaultMoveImpl { + fn construct(&self, v: i32); + fn copy_from(&self, o: Ptr); + fn move_from(&self, _a0: Ptr); fn copy_assign(&self, o: Ptr) -> Ptr; fn move_assign(&self, _a0: Ptr) -> Ptr; } impl UserCopyDefaultMoveImpl for Ptr { + fn construct(&self, mut v: i32) { + field!((*self), v).write(v); + } + fn copy_from(&self, o: Ptr) { + field!((*self), v).write((o.with(|__s| __s.v) + 100)); + } + fn move_from(&self, _a0: Ptr) { + field!((*self), v).write({ (*_a0.upgrade().deref()).v }); + } fn copy_assign(&self, o: Ptr) -> Ptr { field!((*self), v).write((o.with(|__s| __s.v) + 100)); return (*self).clone(); diff --git a/tests/unit/out/refcount/copy_move_deleted.rs b/tests/unit/out/refcount/copy_move_deleted.rs index 1552fef037..c5e1620098 100644 --- a/tests/unit/out/refcount/copy_move_deleted.rs +++ b/tests/unit/out/refcount/copy_move_deleted.rs @@ -6,65 +6,50 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Record, ByteRepr, FnPtrArg, MoveCtor, CppFrom, MoveAssign, Default)] -#[cpp_from(i32, new, value)] +#[derive( + Record, ByteRepr, ZeroInitialization, FnPtrArg, MoveCtor, CppFrom, MoveAssign, Default, +)] +#[cpp_from(i32, construct, value)] #[byte_size(4)] pub struct NoCopy { #[offset(0)] pub v: i32, } -impl NoCopy { - pub fn new(mut v: i32) -> Self { - Self { v: v } - } - pub fn move_from(o: Ptr) -> Self { - let __this: NoCopy = Self { - v: o.with(|__s| __s.v), - }; - field!(o, v).write(0); - __this - } -} -#[derive(Record, ByteRepr, FnPtrArg, MoveCtor, MoveAssign)] +#[derive(Record, ByteRepr, ZeroInitialization, FnPtrArg, MoveCtor, MoveAssign)] #[byte_size(4)] pub struct PrivateCopy { #[offset(0)] pub v: i32, } -impl PrivateCopy { - pub fn new() -> Self { - Self { v: 0 } - } - pub fn move_from(o: Ptr) -> Self { - let __this: PrivateCopy = Self { - v: o.with(|__s| __s.v), - }; - field!(o, v).write(0); - __this - } -} impl Default for PrivateCopy { fn default() -> Self { - { PrivateCopy::new() } + { + { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + PrivateCopyImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + } + } } } -#[derive(Record, ByteRepr, FnPtrArg)] +#[derive(Record, ByteRepr, ZeroInitialization, FnPtrArg)] #[byte_size(4)] pub struct Immovable { #[offset(0)] pub v: i32, } -impl Immovable { - pub fn new() -> Self { - Self { v: 0 } - } -} impl Default for Immovable { fn default() -> Self { - { Immovable::new() } + { + { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + ImmovableImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + } + } } } -#[derive(Record, ByteRepr, FnPtrArg, MoveCtor, MoveAssign, Default)] +#[derive(Record, ByteRepr, ZeroInitialization, FnPtrArg, MoveCtor, MoveAssign, Default)] #[byte_size(8)] pub struct Container { #[offset(0)] @@ -73,28 +58,13 @@ pub struct Container { #[offset(4)] pub tag: i32, } -impl Container { - pub fn move_from(_a0: Ptr) -> Self { - Self { - inner: NoCopy::move_from({ field_ptr!(_a0, inner) }), - tag: { (*_a0.upgrade().deref()).tag }, - } - } -} -#[derive(Record, ByteRepr, FnPtrArg, MoveCtor, MoveAssign, Default)] +#[derive(Record, ByteRepr, ZeroInitialization, FnPtrArg, MoveCtor, MoveAssign, Default)] #[byte_size(24)] pub struct VecHolder { #[offset(0)] #[byte_size(24)] pub items: Vec, } -impl VecHolder { - pub fn move_from(_a0: Ptr) -> Self { - Self { - items: field!(_a0, items).with_mut(std::mem::take), - } - } -} pub fn bump_0(mut p: Ptr) { field!(p, v).with_mut(|__v| __v.postfix_inc()); } @@ -106,49 +76,107 @@ pub fn main() { libcc2rs::cpp2rust_exit(main_0()); } fn main_0() -> i32 { - let a: Value = Rc::new(RefCell::new(NoCopy::new({ 1 }))); - let b: Value = Rc::new(RefCell::new(NoCopy::move_from({ a.as_pointer() }))); + let a: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + NoCopyImpl::construct(&__v.as_pointer(), { 1 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); + let b: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + NoCopyImpl::move_from(&__v.as_pointer(), { a.as_pointer() }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); assert!(({ (*b.borrow()).v } == 1) && ({ (*a.borrow()).v } == 0)); ({ NoCopyImpl::move_assign(&a.as_pointer(), b.as_pointer()) }); assert!(({ (*a.borrow()).v } == 1) && ({ (*b.borrow()).v } == 0)); ({ bump_0((a.as_pointer())) }); assert!(({ (*a.borrow()).v } == 2)); - let p: Value = Rc::new(RefCell::new(PrivateCopy::new())); + let p: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + PrivateCopyImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); (*p.borrow_mut()).v = 3; - let q: Value = Rc::new(RefCell::new(PrivateCopy::move_from({ p.as_pointer() }))); + let q: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + PrivateCopyImpl::move_from(&__v.as_pointer(), { p.as_pointer() }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); assert!(({ (*q.borrow()).v } == 3) && ({ (*p.borrow()).v } == 0)); ({ PrivateCopyImpl::move_assign(&p.as_pointer(), q.as_pointer()) }); assert!(({ (*p.borrow()).v } == 3) && ({ (*q.borrow()).v } == 0)); - let im: Value = Rc::new(RefCell::new(Immovable::new())); + let im: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + ImmovableImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); (*im.borrow_mut()).v = 4; ({ 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: NoCopy::new({ 6 }), + inner: { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + NoCopyImpl::construct(&__v.as_pointer(), { 6 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }, tag: 7, })); - let mut d: Container = Container::move_from({ c.as_pointer() }); + let mut d: Container = { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + ContainerImpl::move_from(&__v.as_pointer(), { c.as_pointer() }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }; assert!(((d.inner.v == 6) && (d.tag == 7)) && ({ (*c.borrow()).inner.v } == 0)); let h: Value = Rc::new(RefCell::new(::default())); - let mut h2: VecHolder = VecHolder::move_from({ h.as_pointer() }); + let mut h2: VecHolder = { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + VecHolderImpl::move_from(&__v.as_pointer(), { h.as_pointer() }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }; assert!((h2.items.len() == 0_usize)); return 0; } pub trait ContainerImpl { + fn move_from(&self, _a0: Ptr); fn move_assign(&self, _a0: Ptr) -> Ptr; } impl ContainerImpl for Ptr { + fn move_from(&self, _a0: Ptr) { + field!((*self), inner).write({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + NoCopyImpl::move_from(&__v.as_pointer(), { field_ptr!(_a0, inner) }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }); + field!((*self), tag).write({ (*_a0.upgrade().deref()).tag }); + } fn move_assign(&self, _a0: Ptr) -> Ptr { ({ NoCopyImpl::move_assign(&field_ptr!((*self), inner), field_ptr!(_a0, inner)) }); field!((*self), tag).write({ (*_a0.upgrade().deref()).tag }); return (*self).clone(); } } +pub trait ImmovableImpl { + fn construct(&self); +} +impl ImmovableImpl for Ptr { + fn construct(&self) { + field!((*self), v).write(0); + } +} pub trait NoCopyImpl { + fn construct(&self, v: i32); + fn move_from(&self, o: Ptr); fn move_assign(&self, o: Ptr) -> Ptr; } impl NoCopyImpl for Ptr { + fn construct(&self, mut v: i32) { + field!((*self), v).write(v); + } + fn move_from(&self, o: Ptr) { + field!((*self), v).write(o.with(|__s| __s.v)); + field!(o, v).write(0); + } fn move_assign(&self, o: Ptr) -> Ptr { field!((*self), v).write(o.with(|__s| __s.v)); field!(o, v).write(0); @@ -159,9 +187,18 @@ pub trait PrivateCopyImpl { fn copy_assign(&self, _a0: Ptr) -> Ptr { unimplemented!() } + fn construct(&self); + fn move_from(&self, o: Ptr); fn move_assign(&self, o: Ptr) -> Ptr; } impl PrivateCopyImpl for Ptr { + fn construct(&self) { + field!((*self), v).write(0); + } + fn move_from(&self, o: Ptr) { + field!((*self), v).write(o.with(|__s| __s.v)); + field!(o, v).write(0); + } fn move_assign(&self, o: Ptr) -> Ptr { field!((*self), v).write(o.with(|__s| __s.v)); field!(o, v).write(0); @@ -169,9 +206,13 @@ impl PrivateCopyImpl for Ptr { } } pub trait VecHolderImpl { + fn move_from(&self, _a0: Ptr); fn move_assign(&self, _a0: Ptr) -> Ptr; } impl VecHolderImpl for Ptr { + fn move_from(&self, _a0: Ptr) { + field!((*self), items).write(field!(_a0, items).with_mut(std::mem::take)); + } fn move_assign(&self, _a0: Ptr) -> Ptr { field!((*self), items).with_mut(|__b0| { (*__b0) = field!(_a0, items).with_mut(std::mem::take); diff --git a/tests/unit/out/refcount/default.rs b/tests/unit/out/refcount/default.rs index 1568ba56c3..907c89f632 100644 --- a/tests/unit/out/refcount/default.rs +++ b/tests/unit/out/refcount/default.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg)] #[byte_size(144)] pub struct Pointers { #[offset(0)] @@ -17,9 +17,11 @@ pub struct Pointers { pub x2: Ptr, #[offset(16)] #[byte_size(40)] + #[array_len(5)] pub x3: Box<[Ptr]>, #[offset(56)] #[byte_size(80)] + #[array_len(10)] pub x4: Box<[Ptr]>, #[offset(136)] pub x5: i32, @@ -41,20 +43,23 @@ impl Default for Pointers { } } } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg)] #[byte_size(440)] pub struct SmallArrays { #[offset(0)] #[byte_size(128)] + #[array_len(32)] pub a: Box<[i32]>, #[offset(128)] #[byte_size(8)] pub f: FnPtr i32>, #[offset(136)] #[byte_size(16)] + #[array_len(2)] pub fs: Box<[FnPtr i32>]>, #[offset(152)] #[byte_size(288)] + #[array_len(2)] pub p: Box<[Pointers]>, } impl Default for SmallArrays { @@ -73,11 +78,12 @@ impl Default for SmallArrays { } } } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg)] #[byte_size(132)] pub struct BigArray { #[offset(0)] #[byte_size(132)] + #[array_len(33)] pub a: Box<[i32]>, } impl Default for BigArray { diff --git a/tests/unit/out/refcount/default_function_arguments.rs b/tests/unit/out/refcount/default_function_arguments.rs index 4206a154af..a015bd1070 100644 --- a/tests/unit/out/refcount/default_function_arguments.rs +++ b/tests/unit/out/refcount/default_function_arguments.rs @@ -12,21 +12,22 @@ pub fn foo_0(mut a: i32, mut b: i32) -> i32 { pub fn baz_1(mut a: Ptr, mut b: Ptr) -> bool { return ({ a } == { b }); } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, CppFrom)] -#[cpp_from(i32, new, value)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, CppFrom)] +#[cpp_from(i32, construct, value)] #[byte_size(4)] pub struct Bar { #[offset(0)] pub v: i32, } -impl Bar { - pub fn new(mut v: i32) -> Self { - Self { v: v } - } -} impl Default for Bar { fn default() -> Self { - { Bar::new({ 1 }) } + { + { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + BarImpl::construct(&__v.as_pointer(), { 1 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + } + } } } thread_local!( @@ -42,7 +43,11 @@ pub fn by_ref_5(b: Ptr) -> i32 { return b.with(|__s| __s.v); } thread_local!( - pub static global_bar_6: Value = Rc::new(RefCell::new(Bar::new({ 9 }))); + pub static global_bar_6: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + BarImpl::construct(&__v.as_pointer(), { 9 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); ); pub fn by_global_ref_7(b: Ptr) -> i32 { return b.with(|__s| __s.v); @@ -53,17 +58,12 @@ pub fn shifted_8(mut x: i32, mut y: i32) -> i32 { pub fn first_9(r: Ptr) -> i32 { return (elem!(r, 0).read()); } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct Holder_int_ { #[offset(0)] pub v: i32, } -impl Holder_int_ { - pub fn new(mut v: i32) -> Self { - Self { v: v } - } -} pub fn main() { __cpp2rust_init_globals(); libcc2rs::cpp2rust_exit(main_0()); @@ -74,10 +74,39 @@ fn main_0() -> i32 { let a: Value = Rc::new(RefCell::new(0)); assert!(((({ baz_1((a.as_pointer()), Ptr::::null(),) }) as i32) == (false as i32))); assert!(((({ baz_1((a.as_pointer()), (a.as_pointer()),) }) as i32) == (true as i32))); - let mut b: Bar = Bar::new({ 1 }); + let mut b: Bar = { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + BarImpl::construct(&__v.as_pointer(), { 1 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }; assert!((b.v == 1)); - assert!(({ Bar::new({ 2 },).v } == 2)); - let mut arr: [Bar; 3] = [Bar::new({ 1 }), Bar::new({ 1 }), Bar::new({ 1 })]; + assert!( + ({ + { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + BarImpl::construct(&__v.as_pointer(), { 2 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + } + .v + } == 2) + ); + let mut arr: [Bar; 3] = [ + { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + BarImpl::construct(&__v.as_pointer(), { 1 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }, + { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + BarImpl::construct(&__v.as_pointer(), { 1 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }, + { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + BarImpl::construct(&__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)); assert!((({ lazy_4(5,) }) == 5)); @@ -86,24 +115,40 @@ fn main_0() -> i32 { assert!((counter_2.with(|rc| *rc.borrow()) == 1)); assert!( (({ - let _b: Value = Rc::new(RefCell::new(Bar::new({ 7 }))); + let _b: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + BarImpl::construct(&__v.as_pointer(), { 7 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); by_ref_5(_b.as_pointer()) }) == 7) ); assert!( (({ - let _b: Value = Rc::new(RefCell::new(Bar::new({ 3 }))); + let _b: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + BarImpl::construct(&__v.as_pointer(), { 3 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); by_ref_5(_b.as_pointer()) }) == 3) ); assert!((({ by_global_ref_7(global_bar_6.with(|v| v.as_pointer()),) }) == 9)); assert!( (({ - let _b: Value = Rc::new(RefCell::new(Bar::new({ 4 }))); + let _b: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + BarImpl::construct(&__v.as_pointer(), { 4 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); by_global_ref_7(_b.as_pointer()) }) == 4) ); - let mut h: Holder_int_ = Holder_int_::new({ 4 }); + let mut h: Holder_int_ = { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + Holder_int_Impl::construct(&__v.as_pointer(), { 4 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }; assert!((h.v == 4)); assert!((({ shifted_8(3, -1_i32,) }) == 2)); assert!((({ shifted_8(3, 4,) }) == 7)); @@ -115,6 +160,22 @@ fn main_0() -> i32 { ); return 0; } +pub trait BarImpl { + fn construct(&self, v: i32); +} +impl BarImpl for Ptr { + fn construct(&self, mut v: i32) { + field!((*self), v).write(v); + } +} +pub trait Holder_int_Impl { + fn construct(&self, v: i32); +} +impl Holder_int_Impl for Ptr { + fn construct(&self, mut v: i32) { + field!((*self), v).write(v); + } +} pub fn __cpp2rust_init_globals() { let _ = counter_2.with(|_| ()); let _ = global_bar_6.with(|_| ()); diff --git a/tests/unit/out/refcount/default_in_statics.rs b/tests/unit/out/refcount/default_in_statics.rs index 5b314fcbf1..cabc3838b4 100644 --- a/tests/unit/out/refcount/default_in_statics.rs +++ b/tests/unit/out/refcount/default_in_statics.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(16)] pub struct Inner { #[offset(0)] @@ -15,7 +15,7 @@ pub struct Inner { #[byte_size(8)] pub name: Ptr, } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg)] #[byte_size(88)] pub struct Outer { #[offset(0)] @@ -26,6 +26,7 @@ pub struct Outer { pub p2: Ptr, #[offset(16)] #[byte_size(24)] + #[array_len(3)] pub arr: Box<[Ptr]>, #[offset(40)] #[byte_size(8)] @@ -60,7 +61,7 @@ impl Default for Outer { } } } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(40)] pub struct Foo { #[offset(0)] diff --git a/tests/unit/out/refcount/delete_array_order.rs b/tests/unit/out/refcount/delete_array_order.rs index 9af4979079..49b00ccf5b 100644 --- a/tests/unit/out/refcount/delete_array_order.rs +++ b/tests/unit/out/refcount/delete_array_order.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Destructible, Default)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Destructible, Default)] #[byte_size(16)] pub struct Element { #[offset(0)] diff --git a/tests/unit/out/refcount/destructor.rs b/tests/unit/out/refcount/destructor.rs index c677a5966d..4ac2b27a80 100644 --- a/tests/unit/out/refcount/destructor.rs +++ b/tests/unit/out/refcount/destructor.rs @@ -9,35 +9,36 @@ use std::rc::{Rc, Weak}; thread_local!( pub static global_0: Value = Rc::new(RefCell::new(0)); ); -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Destructible, Default)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Destructible, Default)] #[byte_size(1)] pub struct S {} -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Destructible, Default)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Destructible, Default)] #[byte_size(1)] pub struct Defaulted { #[offset(0)] #[byte_size(1)] pub s: S, } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Destructible, Default)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Destructible, Default)] #[byte_size(1)] pub struct Middle { #[offset(0)] #[byte_size(1)] pub s: S, } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Destructible, Default)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Destructible, Default)] #[byte_size(1)] pub struct Outer { #[offset(0)] #[byte_size(1)] pub m: Middle, } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Destructible)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Destructible)] #[byte_size(3)] pub struct ArrayMember { #[offset(0)] #[byte_size(3)] + #[array_len(3)] pub items: Box<[S]>, } impl Default for ArrayMember { @@ -49,26 +50,26 @@ impl Default for ArrayMember { } } } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Destructible, Default)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Destructible, Default)] #[byte_size(1)] pub struct EmptyBody { #[offset(0)] #[byte_size(1)] pub s: S, } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Destructible, Default)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Destructible, Default)] #[byte_size(1)] pub struct Templated_char_ { #[offset(0)] pub v: i8, } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Destructible, Default)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Destructible, Default)] #[byte_size(4)] pub struct Templated_int_ { #[offset(0)] pub v: i32, } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Destructible, Default)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Destructible, Default)] #[byte_size(4)] pub struct Copied { #[offset(0)] @@ -81,13 +82,13 @@ thread_local!( thread_local!( pub static order_count_2: Value = Rc::new(RefCell::new(0)); ); -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Destructible, Default)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Destructible, Default)] #[byte_size(4)] pub struct Tagged { #[offset(0)] pub tag: i32, } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Destructible, Default)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Destructible, Default)] #[byte_size(20)] pub struct Ordered { #[offset(0)] diff --git a/tests/unit/out/refcount/destructor_params.rs b/tests/unit/out/refcount/destructor_params.rs index c104308172..ee4c0cc0cb 100644 --- a/tests/unit/out/refcount/destructor_params.rs +++ b/tests/unit/out/refcount/destructor_params.rs @@ -12,7 +12,7 @@ thread_local!( thread_local!( pub static last_1: Value = Rc::new(RefCell::new(0)); ); -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Destructible, Default)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Destructible, Default)] #[byte_size(4)] pub struct S { #[offset(0)] @@ -27,20 +27,12 @@ pub fn with_default_3(s: S) -> i32 { let s: Value = Rc::new(RefCell::new(s)); return { (*s.borrow()).v }; } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct Holder { #[offset(0)] pub v: i32, } -impl Holder { - pub fn new(s: S) -> Self { - let s: Value = Rc::new(RefCell::new(s)); - Self { - v: { (*s.borrow()).v }, - } - } -} pub fn main() { __cpp2rust_init_globals(); libcc2rs::cpp2rust_exit(main_0()); @@ -64,7 +56,11 @@ fn main_0() -> i32 { assert!((destroyed_0.with(|rc| *rc.borrow()) == 1) && (last_1.with(|rc| *rc.borrow()) == 5)); destroyed_0.with(|rc| *rc.borrow_mut() = 0); { - let h: Value = Rc::new(RefCell::new(Holder::new({ S { v: 3 } }))); + let h: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + HolderImpl::construct(&__v.as_pointer(), { S { v: 3 } }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); assert!( (destroyed_0.with(|rc| *rc.borrow()) == 1) && (last_1.with(|rc| *rc.borrow()) == 3) ); @@ -77,9 +73,14 @@ fn main_0() -> i32 { return 0; } pub trait HolderImpl { + fn construct(&self, s: S); fn add(&self, s: S) -> i32; } impl HolderImpl for Ptr { + fn construct(&self, s: S) { + let s: Value = Rc::new(RefCell::new(s)); + field!((*self), v).write({ (*s.borrow()).v }); + } fn add(&self, s: S) -> i32 { let s: Value = Rc::new(RefCell::new(s)); return ((*self).with(|__s| __s.v) + { (*s.borrow()).v }); diff --git a/tests/unit/out/refcount/destructor_temporaries.rs b/tests/unit/out/refcount/destructor_temporaries.rs index 00c779eb95..382cd76114 100644 --- a/tests/unit/out/refcount/destructor_temporaries.rs +++ b/tests/unit/out/refcount/destructor_temporaries.rs @@ -12,13 +12,13 @@ thread_local!( thread_local!( pub static last_1: Value = Rc::new(RefCell::new(0)); ); -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Destructible, Default)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Destructible, Default)] #[byte_size(4)] pub struct S { #[offset(0)] pub v: i32, } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Destructible, Default)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Destructible, Default)] #[byte_size(8)] pub struct Outer { #[offset(0)] diff --git a/tests/unit/out/refcount/doubly_linked_list.rs b/tests/unit/out/refcount/doubly_linked_list.rs index f6025bfb18..827ab5b981 100644 --- a/tests/unit/out/refcount/doubly_linked_list.rs +++ b/tests/unit/out/refcount/doubly_linked_list.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(24)] pub struct Node { #[offset(0)] diff --git a/tests/unit/out/refcount/enum_default_in_static.rs b/tests/unit/out/refcount/enum_default_in_static.rs index dfef000e05..bbfeeb0727 100644 --- a/tests/unit/out/refcount/enum_default_in_static.rs +++ b/tests/unit/out/refcount/enum_default_in_static.rs @@ -10,7 +10,7 @@ pub type Mode = u32; pub const Mode_MODE_NONE: Mode = 0; pub const Mode_MODE_ONE: Mode = 1; pub const Mode_MODE_TWO: Mode = 2; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct Config { #[offset(0)] diff --git a/tests/unit/out/refcount/enum_int_interop.rs b/tests/unit/out/refcount/enum_int_interop.rs index 684a2c858f..fb4ecd5f32 100644 --- a/tests/unit/out/refcount/enum_int_interop.rs +++ b/tests/unit/out/refcount/enum_int_interop.rs @@ -19,7 +19,7 @@ pub type Tag = u32; pub const Tag_TAG_ZERO: Tag = 0; pub const Tag_TAG_ONE: Tag = 1; pub const Tag_TAG_TWO: Tag = 2; -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(16)] pub struct Entry { #[offset(0)] diff --git a/tests/unit/out/refcount/enum_int_interop_c.rs b/tests/unit/out/refcount/enum_int_interop_c.rs index 9562ac8ee8..3e6628f69c 100644 --- a/tests/unit/out/refcount/enum_int_interop_c.rs +++ b/tests/unit/out/refcount/enum_int_interop_c.rs @@ -19,7 +19,7 @@ pub type Tag_enum = u32; pub const Tag_enum_TAG_ZERO: Tag_enum = 0; pub const Tag_enum_TAG_ONE: Tag_enum = 1; pub const Tag_enum_TAG_TWO: Tag_enum = 2; -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(16)] pub struct Entry { #[offset(0)] diff --git a/tests/unit/out/refcount/enum_underlying_type.rs b/tests/unit/out/refcount/enum_underlying_type.rs index 1bd07e6977..b7f3132c3c 100644 --- a/tests/unit/out/refcount/enum_underlying_type.rs +++ b/tests/unit/out/refcount/enum_underlying_type.rs @@ -25,7 +25,7 @@ pub type Scoped = i8; pub const Scoped_NEG: Scoped = -2; pub const Scoped_POS: Scoped = 2; pub type Empty = bool; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg)] #[byte_size(1)] pub struct HoldsEmpty { #[offset(0)] diff --git a/tests/unit/out/refcount/exprs.rs b/tests/unit/out/refcount/exprs.rs index b1c3e4b3fa..4e234f4962 100644 --- a/tests/unit/out/refcount/exprs.rs +++ b/tests/unit/out/refcount/exprs.rs @@ -6,13 +6,13 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct X { #[offset(0)] pub x: i32, } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(16)] pub struct Y { #[offset(0)] diff --git a/tests/unit/out/refcount/fft.rs b/tests/unit/out/refcount/fft.rs index b2590622a8..e59b50a1cb 100644 --- a/tests/unit/out/refcount/fft.rs +++ b/tests/unit/out/refcount/fft.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(16)] pub struct Complex { #[offset(0)] diff --git a/tests/unit/out/refcount/field_aliasing.rs b/tests/unit/out/refcount/field_aliasing.rs index 70617c2bb5..e367fbe455 100644 --- a/tests/unit/out/refcount/field_aliasing.rs +++ b/tests/unit/out/refcount/field_aliasing.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct In { #[offset(0)] @@ -14,7 +14,7 @@ pub struct In { #[offset(4)] pub y: i32, } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg)] #[byte_size(32)] pub struct S { #[offset(0)] @@ -26,6 +26,7 @@ pub struct S { pub n: i32, #[offset(16)] #[byte_size(16)] + #[array_len(4)] pub arr: Box<[i32]>, } impl Default for S { @@ -40,7 +41,7 @@ impl Default for S { } } } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(16)] pub struct Node { #[offset(0)] diff --git a/tests/unit/out/refcount/field_array_elem_ptr.rs b/tests/unit/out/refcount/field_array_elem_ptr.rs index 7896dae58f..c16d51922f 100644 --- a/tests/unit/out/refcount/field_array_elem_ptr.rs +++ b/tests/unit/out/refcount/field_array_elem_ptr.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct Point { #[offset(0)] @@ -14,16 +14,18 @@ pub struct Point { #[offset(4)] pub y: i32, } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg)] #[byte_size(48)] pub struct Shape { #[offset(0)] pub id: i32, #[offset(4)] #[byte_size(16)] + #[array_len(4)] pub coords: Box<[i32]>, #[offset(20)] #[byte_size(24)] + #[array_len(3)] pub points: Box<[Point]>, #[offset(44)] pub tail: i32, diff --git a/tests/unit/out/refcount/field_method_receiver.rs b/tests/unit/out/refcount/field_method_receiver.rs index fcc0dd8960..a4ce080c59 100644 --- a/tests/unit/out/refcount/field_method_receiver.rs +++ b/tests/unit/out/refcount/field_method_receiver.rs @@ -6,13 +6,13 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct Counter { #[offset(0)] pub n: i32, } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, MoveCtor, MoveAssign)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, MoveCtor, MoveAssign)] #[byte_size(40)] pub struct S { #[offset(0)] @@ -22,23 +22,12 @@ pub struct S { pub c: Counter, #[offset(8)] #[byte_size(8)] + #[array_len(2)] pub arr: Box<[Counter]>, #[offset(16)] #[byte_size(24)] pub v: Vec, } -impl S { - pub fn move_from(_a0: Ptr) -> Self { - Self { - tag: { (*_a0.upgrade().deref()).tag }, - c: { (*_a0.upgrade().deref()).c }, - arr: Box::new(std::array::from_fn::<_, 2, _>(|__i: usize| { - (*_a0.upgrade().deref()).arr[(__i) as usize] - })), - v: field!(_a0, v).with_mut(std::mem::take), - } - } -} impl Default for S { fn default() -> Self { { @@ -128,12 +117,21 @@ impl CounterImpl for Ptr { } pub trait SImpl { fn bump(&self); + fn move_from(&self, _a0: Ptr); fn move_assign(&self, _a0: Ptr) -> Ptr; } impl SImpl for Ptr { fn bump(&self) { ({ CounterImpl::add(&field_ptr!((*self), c), (*self).with(|__s| __s.tag)) }); } + fn move_from(&self, _a0: Ptr) { + field!((*self), tag).write({ (*_a0.upgrade().deref()).tag }); + field!((*self), c).write({ (*_a0.upgrade().deref()).c }); + field!((*self), arr).write(Box::new(std::array::from_fn::<_, 2, _>(|__i: usize| { + (*_a0.upgrade().deref()).arr[(__i) as usize] + }))); + field!((*self), v).write(field!(_a0, v).with_mut(std::mem::take)); + } fn move_assign(&self, _a0: Ptr) -> Ptr { field!((*self), tag).write({ (*_a0.upgrade().deref()).tag }); field!((*self), c).write({ (*_a0.upgrade().deref()).c }); diff --git a/tests/unit/out/refcount/field_pointers.rs b/tests/unit/out/refcount/field_pointers.rs index 8f159a2e45..30c229b036 100644 --- a/tests/unit/out/refcount/field_pointers.rs +++ b/tests/unit/out/refcount/field_pointers.rs @@ -6,13 +6,14 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg)] #[byte_size(12)] pub struct Inner { #[offset(0)] pub a: i32, #[offset(4)] #[byte_size(8)] + #[array_len(8)] pub name: Box<[i8]>, } impl Default for Inner { @@ -25,7 +26,7 @@ impl Default for Inner { } } } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct Header { #[offset(0)] @@ -33,7 +34,7 @@ pub struct Header { #[offset(4)] pub size: i16, } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, MoveCtor, MoveAssign)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, MoveCtor, MoveAssign)] #[byte_size(104)] pub struct Outer { #[offset(0)] @@ -43,6 +44,7 @@ pub struct Outer { pub inner: Inner, #[offset(16)] #[byte_size(36)] + #[array_len(3)] pub items: Box<[Inner]>, #[offset(56)] #[byte_size(24)] @@ -52,24 +54,9 @@ pub struct Outer { pub cursor: Ptr, #[offset(88)] #[byte_size(16)] + #[array_len(4)] pub buf: Box<[i32]>, } -impl Outer { - pub fn move_from(_a0: Ptr) -> Self { - Self { - x: { (*_a0.upgrade().deref()).x }, - inner: { (*_a0.upgrade().deref()).inner.clone() }, - items: Box::new(std::array::from_fn::<_, 3, _>(|__i: usize| { - ((*_a0.upgrade().deref()).items[(__i) as usize]).clone() - })), - v: field!(_a0, v).with_mut(std::mem::take), - cursor: { (*_a0.upgrade().deref()).cursor.clone() }, - buf: Box::new(std::array::from_fn::<_, 4, _>(|__i: usize| { - (*_a0.upgrade().deref()).buf[(__i) as usize] - })), - } - } -} impl Default for Outer { fn default() -> Self { { @@ -225,6 +212,7 @@ pub trait OuterImpl { fn next(&self) -> i32; fn sum(&self) -> i32; fn push(&self, k: i32); + fn move_from(&self, _a0: Ptr); fn move_assign(&self, _a0: Ptr) -> Ptr; } impl OuterImpl for Ptr { @@ -245,6 +233,18 @@ impl OuterImpl for Ptr { field!((*self), v).with_mut(|__v: &mut Vec| __v.push(__a1)) }; } + fn move_from(&self, _a0: Ptr) { + field!((*self), x).write({ (*_a0.upgrade().deref()).x }); + field!((*self), inner).write({ (*_a0.upgrade().deref()).inner.clone() }); + field!((*self), items).write(Box::new(std::array::from_fn::<_, 3, _>(|__i: usize| { + ((*_a0.upgrade().deref()).items[(__i) as usize]).clone() + }))); + field!((*self), v).write(field!(_a0, v).with_mut(std::mem::take)); + field!((*self), cursor).write({ (*_a0.upgrade().deref()).cursor.clone() }); + field!((*self), buf).write(Box::new(std::array::from_fn::<_, 4, _>(|__i: usize| { + (*_a0.upgrade().deref()).buf[(__i) as usize] + }))); + } fn move_assign(&self, _a0: Ptr) -> Ptr { field!((*self), x).write({ (*_a0.upgrade().deref()).x }); field!((*self), inner).write({ (*_a0.upgrade().deref()).inner.clone() }); diff --git a/tests/unit/out/refcount/field_pointers_malloced_struct.rs b/tests/unit/out/refcount/field_pointers_malloced_struct.rs index 04fdb3c42a..57d6b0f8d2 100644 --- a/tests/unit/out/refcount/field_pointers_malloced_struct.rs +++ b/tests/unit/out/refcount/field_pointers_malloced_struct.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(12)] pub struct S { #[offset(0)] diff --git a/tests/unit/out/refcount/field_reinterpret_write_through.rs b/tests/unit/out/refcount/field_reinterpret_write_through.rs index b89374971e..f8bfab8fe5 100644 --- a/tests/unit/out/refcount/field_reinterpret_write_through.rs +++ b/tests/unit/out/refcount/field_reinterpret_write_through.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct In { #[offset(0)] @@ -14,7 +14,7 @@ pub struct In { #[offset(4)] pub b: i32, } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg)] #[byte_size(40)] pub struct S { #[offset(0)] @@ -24,9 +24,11 @@ pub struct S { pub in_: In, #[offset(12)] #[byte_size(4)] + #[array_len(4)] pub bytes: Box<[u8]>, #[offset(16)] #[byte_size(12)] + #[array_len(3)] pub arr: Box<[i32]>, #[offset(32)] pub tail: i64, diff --git a/tests/unit/out/refcount/find.rs b/tests/unit/out/refcount/find.rs index abe4b00636..b1b2b36bdb 100644 --- a/tests/unit/out/refcount/find.rs +++ b/tests/unit/out/refcount/find.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct P { #[offset(0)] diff --git a/tests/unit/out/refcount/first_field_ptr_covers_struct.rs b/tests/unit/out/refcount/first_field_ptr_covers_struct.rs index cf6b7a8bc4..81358273ee 100644 --- a/tests/unit/out/refcount/first_field_ptr_covers_struct.rs +++ b/tests/unit/out/refcount/first_field_ptr_covers_struct.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct In { #[offset(0)] @@ -14,7 +14,7 @@ pub struct In { #[offset(2)] pub b: i16, } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(12)] pub struct S { #[offset(0)] diff --git a/tests/unit/out/refcount/first_member_downcast.rs b/tests/unit/out/refcount/first_member_downcast.rs index f0b38972a0..0e766a79e7 100644 --- a/tests/unit/out/refcount/first_member_downcast.rs +++ b/tests/unit/out/refcount/first_member_downcast.rs @@ -6,13 +6,13 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct base { #[offset(0)] pub kind: i32, } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(16)] pub struct derived { #[offset(0)] diff --git a/tests/unit/out/refcount/flex_array_member_incomplete.rs b/tests/unit/out/refcount/flex_array_member_incomplete.rs index d3e63edf9d..1be33956bd 100644 --- a/tests/unit/out/refcount/flex_array_member_incomplete.rs +++ b/tests/unit/out/refcount/flex_array_member_incomplete.rs @@ -6,13 +6,14 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg)] #[byte_size(4)] pub struct F { #[offset(0)] pub n: i32, #[offset(4)] #[byte_size(0)] + #[array_len(0)] pub tail: Box<[i8]>, } impl Default for F { diff --git a/tests/unit/out/refcount/flex_array_member_sibling_write.rs b/tests/unit/out/refcount/flex_array_member_sibling_write.rs index cea9803196..443107535a 100644 --- a/tests/unit/out/refcount/flex_array_member_sibling_write.rs +++ b/tests/unit/out/refcount/flex_array_member_sibling_write.rs @@ -6,13 +6,14 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg)] #[byte_size(8)] pub struct S { #[offset(0)] pub n: i32, #[offset(4)] #[byte_size(1)] + #[array_len(1)] pub name: Box<[i8]>, } impl Default for S { @@ -25,7 +26,7 @@ impl Default for S { } } } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct E { #[offset(0)] @@ -33,7 +34,7 @@ pub struct E { #[offset(4)] pub w: i32, } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg)] #[byte_size(16)] pub struct T { #[offset(0)] @@ -42,6 +43,7 @@ pub struct T { pub cap: i32, #[offset(8)] #[byte_size(8)] + #[array_len(1)] pub a: Box<[E]>, } impl Default for T { diff --git a/tests/unit/out/refcount/fn_ptr.rs b/tests/unit/out/refcount/fn_ptr.rs index ff5447822d..28d06980e6 100644 --- a/tests/unit/out/refcount/fn_ptr.rs +++ b/tests/unit/out/refcount/fn_ptr.rs @@ -28,19 +28,14 @@ pub fn call_fn_5(mut f: FnPtr usize>, mut v: i32) -> usize { pub fn identity_hash_6(mut v: bool) -> usize { return (v as usize); } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, CppFrom, Default)] -#[cpp_from(FnPtr u64 > , new, ptr)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, CppFrom, Default)] +#[cpp_from(FnPtr u64 > , construct, ptr)] #[byte_size(8)] pub struct HashHolder_unsigned_long__ptr__bool__ { #[offset(0)] #[byte_size(8)] pub h: FnPtr u64>, } -impl HashHolder_unsigned_long__ptr__bool__ { - pub fn new(h: Ptr u64>>) -> Self { - Self { h: (h.read()) } - } -} thread_local!( pub static total_7: Value = Rc::new(RefCell::new(0)); ); @@ -89,7 +84,15 @@ fn main_0() -> i32 { let __tmp_0: Value u64>> = Rc::new(RefCell::new( (FnPtr:: usize>::new(identity_hash_6)).cast:: u64>(), )); - HashHolder_unsigned_long__ptr__bool__::new({ __tmp_0.as_pointer() }) + { + let __v: Value = Rc::new(RefCell::new( + ::zero_init(), + )); + HashHolder_unsigned_long__ptr__bool__Impl::construct(&__v.as_pointer(), { + __tmp_0.as_pointer() + }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + } }; assert!((({ hh.h.call(true,) }) == 1_u64)); let mut handler: FnPtr = FnPtr::::new(add_to_total_8); @@ -123,6 +126,14 @@ fn main_0() -> i32 { ); return 0; } +pub trait HashHolder_unsigned_long__ptr__bool__Impl { + fn construct(&self, h: Ptr u64>>); +} +impl HashHolder_unsigned_long__ptr__bool__Impl for Ptr { + fn construct(&self, h: Ptr u64>>) { + field!((*self), h).write((h.read())); + } +} pub fn __cpp2rust_init_globals() { let _ = total_7.with(|_| ()); } diff --git a/tests/unit/out/refcount/fn_ptr_cast.rs b/tests/unit/out/refcount/fn_ptr_cast.rs index 8c612fa1eb..72fe1d2075 100644 --- a/tests/unit/out/refcount/fn_ptr_cast.rs +++ b/tests/unit/out/refcount/fn_ptr_cast.rs @@ -24,7 +24,7 @@ pub fn test_double_cast_2() { assert!((({ fn2.call(5,) }) == 10)); assert!(({ (fn2).clone() } == { (fn_).clone() })); } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct Command { #[offset(0)] diff --git a/tests/unit/out/refcount/fn_ptr_stable_sort.rs b/tests/unit/out/refcount/fn_ptr_stable_sort.rs index a89fcb03af..aa4abe2bfa 100644 --- a/tests/unit/out/refcount/fn_ptr_stable_sort.rs +++ b/tests/unit/out/refcount/fn_ptr_stable_sort.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct Item { #[offset(0)] diff --git a/tests/unit/out/refcount/fn_ptr_struct.rs b/tests/unit/out/refcount/fn_ptr_struct.rs index 87f3498422..e01ed25ebb 100644 --- a/tests/unit/out/refcount/fn_ptr_struct.rs +++ b/tests/unit/out/refcount/fn_ptr_struct.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(16)] pub struct Handler { #[offset(0)] @@ -21,7 +21,7 @@ pub fn double_it_0(mut x: i32) -> i32 { pub fn negate_1(mut x: i32) -> i32 { return -x; } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(1)] pub struct S {} impl S { @@ -35,31 +35,29 @@ impl S { return (x + 3); } } -#[derive(Record, ByteRepr, FnPtrArg, MoveCtor, CppFrom, Default)] -#[cpp_from(i32, new, value)] +#[derive(Record, ByteRepr, ZeroInitialization, FnPtrArg, MoveCtor, CppFrom, Default)] +#[cpp_from(i32, construct, value)] #[byte_size(4)] pub struct MoveOnly { #[offset(0)] pub data_: i32, } -impl MoveOnly { - pub fn new(mut data: i32) -> Self { - Self { data_: data } - } - pub fn move_from(x: Ptr) -> Self { - let __this: MoveOnly = Self { - data_: x.with(|__s| __s.data_), - }; - field!(x, data_).write(0); - __this - } -} pub fn make_move_only_2() -> MoveOnly { - return MoveOnly::new({ 4 }); + return { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + MoveOnlyImpl::construct(&__v.as_pointer(), { 4 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }; } pub fn scale_move_only_3(x: MoveOnly) -> MoveOnly { let x: Value = Rc::new(RefCell::new(x)); - return MoveOnly::new({ (({ MoveOnlyImpl::get(&x.as_pointer()) }) * 10) }); + return { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + MoveOnlyImpl::construct(&__v.as_pointer(), { + (({ MoveOnlyImpl::get(&x.as_pointer()) }) * 10) + }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }; } pub fn main() { __cpp2rust_init_globals(); @@ -96,16 +94,31 @@ fn main_0() -> i32 { let mut sc: FnPtr MoveOnly> = FnPtr:: MoveOnly>::new(scale_move_only_3); let n: Value = Rc::new(RefCell::new( - ({ sc.call(MoveOnly::move_from({ m.as_pointer() })) }), + ({ + sc.call({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + MoveOnlyImpl::move_from(&__v.as_pointer(), { m.as_pointer() }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }) + }), )); assert!((({ MoveOnlyImpl::get(&n.as_pointer(),) }) == 40)); assert!((({ MoveOnlyImpl::get(&m.as_pointer(),) }) == 0)); return 0; } pub trait MoveOnlyImpl { + fn construct(&self, data: i32); + fn move_from(&self, x: Ptr); fn get(&self) -> i32; } impl MoveOnlyImpl for Ptr { + fn construct(&self, mut data: i32) { + field!((*self), data_).write(data); + } + fn move_from(&self, x: Ptr) { + field!((*self), data_).write(x.with(|__s| __s.data_)); + field!(x, data_).write(0); + } fn get(&self) -> i32 { return (*self).with(|__s| __s.data_); } diff --git a/tests/unit/out/refcount/fn_ptr_typedef_cast.rs b/tests/unit/out/refcount/fn_ptr_typedef_cast.rs index 1a4f43a4a2..a72c15adba 100644 --- a/tests/unit/out/refcount/fn_ptr_typedef_cast.rs +++ b/tests/unit/out/refcount/fn_ptr_typedef_cast.rs @@ -18,7 +18,7 @@ pub fn via_size_t_param_2(mut b: usize) -> u64 { pub fn via_size_t_return_3(mut b: u64) -> usize { return (b as usize); } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct pair { #[offset(0)] diff --git a/tests/unit/out/refcount/fn_ptr_vtable.rs b/tests/unit/out/refcount/fn_ptr_vtable.rs index 81aec298e7..b9281b2847 100644 --- a/tests/unit/out/refcount/fn_ptr_vtable.rs +++ b/tests/unit/out/refcount/fn_ptr_vtable.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(24)] pub struct Vtable { #[offset(0)] diff --git a/tests/unit/out/refcount/fn_ref.rs b/tests/unit/out/refcount/fn_ref.rs index ae78ca2d93..e00efd7f20 100644 --- a/tests/unit/out/refcount/fn_ref.rs +++ b/tests/unit/out/refcount/fn_ref.rs @@ -21,7 +21,7 @@ pub fn call_deduced_3(f: FnPtr i32>, mut x: i32) -> i32 { pub fn call_forwarded_4(f: FnPtr i32>, mut x: i32) -> i32 { return ({ f.call(x) }); } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct Holder { #[offset(0)] diff --git a/tests/unit/out/refcount/for_body_destructor.rs b/tests/unit/out/refcount/for_body_destructor.rs index 55ab0488a4..986304af95 100644 --- a/tests/unit/out/refcount/for_body_destructor.rs +++ b/tests/unit/out/refcount/for_body_destructor.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Destructible, Default)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Destructible, Default)] #[byte_size(4)] pub struct Guard { #[offset(0)] diff --git a/tests/unit/out/refcount/foreach_disjoint_field_borrow.rs b/tests/unit/out/refcount/foreach_disjoint_field_borrow.rs index 8436c3c0ca..fb614609a0 100644 --- a/tests/unit/out/refcount/foreach_disjoint_field_borrow.rs +++ b/tests/unit/out/refcount/foreach_disjoint_field_borrow.rs @@ -6,7 +6,9 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, MoveCtor, MoveAssign, Default)] +#[derive( + Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, MoveCtor, MoveAssign, Default, +)] #[byte_size(32)] pub struct S { #[offset(0)] @@ -15,14 +17,6 @@ pub struct S { #[offset(24)] pub a: i32, } -impl S { - pub fn move_from(_a0: Ptr) -> Self { - Self { - v: field!(_a0, v).with_mut(std::mem::take), - a: { (*_a0.upgrade().deref()).a }, - } - } -} pub fn main() { __cpp2rust_init_globals(); libcc2rs::cpp2rust_exit(main_0()); @@ -43,9 +37,14 @@ fn main_0() -> i32 { return 0; } pub trait SImpl { + fn move_from(&self, _a0: Ptr); fn move_assign(&self, _a0: Ptr) -> Ptr; } impl SImpl for Ptr { + fn move_from(&self, _a0: Ptr) { + field!((*self), v).write(field!(_a0, v).with_mut(std::mem::take)); + field!((*self), a).write({ (*_a0.upgrade().deref()).a }); + } fn move_assign(&self, _a0: Ptr) -> Ptr { field!((*self), v).with_mut(|__b0| { (*__b0) = field!(_a0, v).with_mut(std::mem::take); diff --git a/tests/unit/out/refcount/forward_basic.rs b/tests/unit/out/refcount/forward_basic.rs index 627f20997b..ee9c29495f 100644 --- a/tests/unit/out/refcount/forward_basic.rs +++ b/tests/unit/out/refcount/forward_basic.rs @@ -9,8 +9,8 @@ use std::rc::{Rc, Weak}; pub type Overload = u32; pub const Overload_kLvalueOverload: Overload = 1; pub const Overload_kRvalueOverload: Overload = 2; -#[derive(Record, ByteRepr, VaArg, FnPtrArg, MoveCtor, CppFrom, Default)] -#[cpp_from(i32, new, value)] +#[derive(Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, MoveCtor, CppFrom, Default)] +#[cpp_from(i32, construct, value)] #[byte_size(12)] pub struct Tracked { #[offset(0)] @@ -20,31 +20,6 @@ pub struct Tracked { #[offset(8)] pub moves: i32, } -impl Tracked { - pub fn new(mut v: i32) -> Self { - Self { - v: v, - copies: 0, - moves: 0, - } - } - pub fn copy_from(o: Ptr) -> Self { - Self { - v: o.with(|__s| __s.v), - copies: (o.with(|__s| __s.copies) + 1), - moves: o.with(|__s| __s.moves), - } - } - pub fn move_from(o: Ptr) -> Self { - let __this: Tracked = Self { - v: o.with(|__s| __s.v), - copies: o.with(|__s| __s.copies), - moves: (o.with(|__s| __s.moves) + 1), - }; - field!(o, v).write(0); - __this - } -} impl Clone for Tracked { fn clone(&self) -> Self { let __src: Value = Rc::new(RefCell::new(Tracked { @@ -52,7 +27,9 @@ impl Clone for Tracked { copies: self.copies.clone(), moves: self.moves.clone(), })); - Tracked::copy_from(__src.as_pointer()) + let __v: Value = Rc::new(RefCell::new(::zero_init())); + TrackedImpl::copy_from(&__v.as_pointer(), __src.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() } } pub fn chosen_overload_0(_a0: Ptr) -> Overload { @@ -61,36 +38,17 @@ pub fn chosen_overload_0(_a0: Ptr) -> Overload { pub fn chosen_overload_1(_a0: Ptr) -> Overload { return Overload_kRvalueOverload; } -impl Holder { - pub fn new_1(x: Ptr) -> Self { - Self { - t: Tracked::copy_from({ (x).clone() }), - } - } -} -impl Holder { - pub fn new_2(x: Ptr) -> Self { - Self { - t: Tracked::move_from({ (x).clone() }), - } - } -} -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, MoveCtor, CppFrom, Default)] -#[cpp_from(Tracked, new_1, mut_ptr)] -#[cpp_from_move(Tracked, new_2, ptr)] +#[derive( + Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, MoveCtor, CppFrom, Default, +)] +#[cpp_from(Tracked, construct_1, mut_ptr)] +#[cpp_from_move(Tracked, construct_2, ptr)] #[byte_size(12)] pub struct Holder { #[offset(0)] #[byte_size(12)] pub t: Tracked, } -impl Holder { - pub fn move_from(_a0: Ptr) -> Self { - Self { - t: Tracked::move_from({ field_ptr!(_a0, t) }), - } - } -} pub fn forward_once_2(x: Ptr) -> Overload { return ({ chosen_overload_0((x).clone()) }); } @@ -104,29 +62,49 @@ pub fn forward_twice_5(x: Ptr) -> Overload { return ({ forward_once_3((x).clone()) }); } pub fn forward_into_ctor_6(x: Ptr) -> Holder { - return Holder::new_1({ (x).clone() }); + return { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + HolderImpl::construct_1(&__v.as_pointer(), { (x).clone() }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }; } pub fn forward_into_ctor_7(x: Ptr) -> Holder { - return Holder::new_2({ (x).clone() }); + return { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + HolderImpl::construct_2(&__v.as_pointer(), { (x).clone() }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }; } pub fn main() { __cpp2rust_init_globals(); libcc2rs::cpp2rust_exit(main_0()); } fn main_0() -> i32 { - let lvalue: Value = Rc::new(RefCell::new(Tracked::new({ 7 }))); + let lvalue: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + TrackedImpl::construct(&__v.as_pointer(), { 7 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); assert!( ((({ forward_once_2(lvalue.as_pointer(),) }) as i32) == (Overload_kLvalueOverload as i32)) ); assert!(({ (*lvalue.borrow()).v } == 7)); assert!( ((({ - let _x: Value = Rc::new(RefCell::new(Tracked::new({ 8 }))); + let _x: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + TrackedImpl::construct(&__v.as_pointer(), { 8 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); forward_once_3(_x.as_pointer()) }) as i32) == (Overload_kRvalueOverload as i32)) ); - let relayed: Value = Rc::new(RefCell::new(Tracked::new({ 9 }))); + let relayed: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + TrackedImpl::construct(&__v.as_pointer(), { 9 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); assert!( ((({ forward_twice_4(relayed.as_pointer(),) }) as i32) == (Overload_kLvalueOverload as i32)) @@ -134,19 +112,31 @@ fn main_0() -> i32 { assert!(({ (*relayed.borrow()).v } == 9)); assert!( ((({ - let _x: Value = Rc::new(RefCell::new(Tracked::new({ 10 }))); + let _x: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + TrackedImpl::construct(&__v.as_pointer(), { 10 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); forward_twice_5(_x.as_pointer()) }) as i32) == (Overload_kRvalueOverload as i32)) ); - let kept: Value = Rc::new(RefCell::new(Tracked::new({ 11 }))); + let kept: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + TrackedImpl::construct(&__v.as_pointer(), { 11 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); let mut from_lvalue: Holder = ({ forward_into_ctor_6(kept.as_pointer()) }); assert!((from_lvalue.t.v == 11)); assert!((from_lvalue.t.copies == 1)); assert!((from_lvalue.t.moves == 0)); assert!(({ (*kept.borrow()).v } == 11)); let mut from_rvalue: Holder = ({ - let _x: Value = Rc::new(RefCell::new(Tracked::new({ 12 }))); + let _x: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + TrackedImpl::construct(&__v.as_pointer(), { 12 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); forward_into_ctor_7(_x.as_pointer()) }); assert!((from_rvalue.t.v == 12)); @@ -154,4 +144,55 @@ fn main_0() -> i32 { assert!((from_rvalue.t.moves == 1)); return 0; } +pub trait HolderImpl { + fn construct_1(&self, x: Ptr); + fn construct_2(&self, x: Ptr); + fn move_from(&self, _a0: Ptr); +} +impl HolderImpl for Ptr { + fn construct_1(&self, x: Ptr) { + field!((*self), t).write({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + TrackedImpl::copy_from(&__v.as_pointer(), { (x).clone() }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }); + } + fn construct_2(&self, x: Ptr) { + field!((*self), t).write({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + TrackedImpl::move_from(&__v.as_pointer(), { (x).clone() }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }); + } + fn move_from(&self, _a0: Ptr) { + field!((*self), t).write({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + TrackedImpl::move_from(&__v.as_pointer(), { field_ptr!(_a0, t) }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }); + } +} +pub trait TrackedImpl { + fn construct(&self, v: i32); + fn copy_from(&self, o: Ptr); + fn move_from(&self, o: Ptr); +} +impl TrackedImpl for Ptr { + fn construct(&self, mut v: i32) { + field!((*self), v).write(v); + field!((*self), copies).write(0); + field!((*self), moves).write(0); + } + fn copy_from(&self, o: Ptr) { + field!((*self), v).write(o.with(|__s| __s.v)); + field!((*self), copies).write((o.with(|__s| __s.copies) + 1)); + field!((*self), moves).write(o.with(|__s| __s.moves)); + } + fn move_from(&self, o: Ptr) { + field!((*self), v).write(o.with(|__s| __s.v)); + field!((*self), copies).write(o.with(|__s| __s.copies)); + field!((*self), moves).write((o.with(|__s| __s.moves) + 1)); + field!(o, v).write(0); + } +} pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/forward_decltype.rs b/tests/unit/out/refcount/forward_decltype.rs index 426aab887c..0a2ee40a47 100644 --- a/tests/unit/out/refcount/forward_decltype.rs +++ b/tests/unit/out/refcount/forward_decltype.rs @@ -9,8 +9,8 @@ use std::rc::{Rc, Weak}; pub type Overload = u32; pub const Overload_kLvalueOverload: Overload = 1; pub const Overload_kRvalueOverload: Overload = 2; -#[derive(Record, ByteRepr, VaArg, FnPtrArg, MoveCtor, CppFrom, Default)] -#[cpp_from(i32, new, value)] +#[derive(Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, MoveCtor, CppFrom, Default)] +#[cpp_from(i32, construct, value)] #[byte_size(12)] pub struct Tracked { #[offset(0)] @@ -20,31 +20,6 @@ pub struct Tracked { #[offset(8)] pub moves: i32, } -impl Tracked { - pub fn new(mut v: i32) -> Self { - Self { - v: v, - copies: 0, - moves: 0, - } - } - pub fn copy_from(o: Ptr) -> Self { - Self { - v: o.with(|__s| __s.v), - copies: (o.with(|__s| __s.copies) + 1), - moves: o.with(|__s| __s.moves), - } - } - pub fn move_from(o: Ptr) -> Self { - let __this: Tracked = Self { - v: o.with(|__s| __s.v), - copies: o.with(|__s| __s.copies), - moves: (o.with(|__s| __s.moves) + 1), - }; - field!(o, v).write(0); - __this - } -} impl Clone for Tracked { fn clone(&self) -> Self { let __src: Value = Rc::new(RefCell::new(Tracked { @@ -52,7 +27,9 @@ impl Clone for Tracked { copies: self.copies.clone(), moves: self.moves.clone(), })); - Tracked::copy_from(__src.as_pointer()) + let __v: Value = Rc::new(RefCell::new(::zero_init())); + TrackedImpl::copy_from(&__v.as_pointer(), __src.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() } } pub fn chosen_overload_0(_a0: Ptr) -> Overload { @@ -83,7 +60,11 @@ pub fn main() { libcc2rs::cpp2rust_exit(main_0()); } fn main_0() -> i32 { - let a: Value = Rc::new(RefCell::new(Tracked::new({ 3 }))); + let a: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + TrackedImpl::construct(&__v.as_pointer(), { 3 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); assert!( ((({ forward_by_decltype_2(a.as_pointer(),) }) as i32) == (Overload_kLvalueOverload as i32)) @@ -91,12 +72,20 @@ fn main_0() -> i32 { assert!(({ (*a.borrow()).v } == 3)); assert!( ((({ - let _x: Value = Rc::new(RefCell::new(Tracked::new({ 4 }))); + let _x: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + TrackedImpl::construct(&__v.as_pointer(), { 4 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); forward_by_decltype_3(_x.as_pointer()) }) as i32) == (Overload_kRvalueOverload as i32)) ); - let b: Value = Rc::new(RefCell::new(Tracked::new({ 5 }))); + let b: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + TrackedImpl::construct(&__v.as_pointer(), { 5 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); assert!( ((({ forward_abbreviated_4(b.as_pointer(),) }) as i32) == (Overload_kLvalueOverload as i32)) @@ -104,19 +93,54 @@ fn main_0() -> i32 { assert!(({ (*b.borrow()).v } == 5)); assert!( ((({ - let _x: Value = Rc::new(RefCell::new(Tracked::new({ 6 }))); + let _x: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + TrackedImpl::construct(&__v.as_pointer(), { 6 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); forward_abbreviated_5(_x.as_pointer()) }) as i32) == (Overload_kRvalueOverload as i32)) ); - let c: Value = Rc::new(RefCell::new(Tracked::new({ 7 }))); + let c: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + TrackedImpl::construct(&__v.as_pointer(), { 7 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); assert!( (({ - let _args_1: Value = Rc::new(RefCell::new(Tracked::new({ 8 }))); + let _args_1: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + TrackedImpl::construct(&__v.as_pointer(), { 8 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); forward_abbreviated_pack_6(c.as_pointer(), _args_1.as_pointer()) }) == ((Overload_kLvalueOverload as i32) + (Overload_kRvalueOverload as i32))) ); assert!(({ (*c.borrow()).v } == 7)); return 0; } +pub trait TrackedImpl { + fn construct(&self, v: i32); + fn copy_from(&self, o: Ptr); + fn move_from(&self, o: Ptr); +} +impl TrackedImpl for Ptr { + fn construct(&self, mut v: i32) { + field!((*self), v).write(v); + field!((*self), copies).write(0); + field!((*self), moves).write(0); + } + fn copy_from(&self, o: Ptr) { + field!((*self), v).write(o.with(|__s| __s.v)); + field!((*self), copies).write((o.with(|__s| __s.copies) + 1)); + field!((*self), moves).write(o.with(|__s| __s.moves)); + } + fn move_from(&self, o: Ptr) { + field!((*self), v).write(o.with(|__s| __s.v)); + field!((*self), copies).write(o.with(|__s| __s.copies)); + field!((*self), moves).write((o.with(|__s| __s.moves) + 1)); + field!(o, v).write(0); + } +} pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/forward_explicit.rs b/tests/unit/out/refcount/forward_explicit.rs index 5346663401..f4e9495078 100644 --- a/tests/unit/out/refcount/forward_explicit.rs +++ b/tests/unit/out/refcount/forward_explicit.rs @@ -11,8 +11,8 @@ pub const Overload_kLvalueOverload: Overload = 1; pub const Overload_kRvalueOverload: Overload = 2; pub const Overload_kIntLvalueOverload: Overload = 3; pub const Overload_kIntRvalueOverload: Overload = 4; -#[derive(Record, ByteRepr, VaArg, FnPtrArg, MoveCtor, CppFrom, Default)] -#[cpp_from(i32, new, value)] +#[derive(Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, MoveCtor, CppFrom, Default)] +#[cpp_from(i32, construct, value)] #[byte_size(12)] pub struct Tracked { #[offset(0)] @@ -22,31 +22,6 @@ pub struct Tracked { #[offset(8)] pub moves: i32, } -impl Tracked { - pub fn new(mut v: i32) -> Self { - Self { - v: v, - copies: 0, - moves: 0, - } - } - pub fn copy_from(o: Ptr) -> Self { - Self { - v: o.with(|__s| __s.v), - copies: (o.with(|__s| __s.copies) + 1), - moves: o.with(|__s| __s.moves), - } - } - pub fn move_from(o: Ptr) -> Self { - let __this: Tracked = Self { - v: o.with(|__s| __s.v), - copies: o.with(|__s| __s.copies), - moves: (o.with(|__s| __s.moves) + 1), - }; - field!(o, v).write(0); - __this - } -} impl Clone for Tracked { fn clone(&self) -> Self { let __src: Value = Rc::new(RefCell::new(Tracked { @@ -54,7 +29,9 @@ impl Clone for Tracked { copies: self.copies.clone(), moves: self.moves.clone(), })); - Tracked::copy_from(__src.as_pointer()) + let __v: Value = Rc::new(RefCell::new(::zero_init())); + TrackedImpl::copy_from(&__v.as_pointer(), __src.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() } } pub fn chosen_overload_0(_a0: Ptr) -> Overload { @@ -71,14 +48,22 @@ pub fn chosen_overload_3(_a0: Ptr) -> Overload { } pub fn copy_or_move_into_param_4(t: Tracked) -> Tracked { let t: Value = Rc::new(RefCell::new(t)); - return Tracked::move_from({ t.as_pointer() }); + return { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + TrackedImpl::move_from(&__v.as_pointer(), { t.as_pointer() }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }; } pub fn main() { __cpp2rust_init_globals(); libcc2rs::cpp2rust_exit(main_0()); } fn main_0() -> i32 { - let a: Value = Rc::new(RefCell::new(Tracked::new({ 5 }))); + let a: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + TrackedImpl::construct(&__v.as_pointer(), { 5 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); assert!( ((({ chosen_overload_0(a.as_pointer(),) }) as i32) == (Overload_kLvalueOverload as i32)) ); @@ -87,16 +72,34 @@ fn main_0() -> i32 { ((({ chosen_overload_1(a.as_pointer(),) }) as i32) == (Overload_kRvalueOverload as i32)) ); assert!(({ (*a.borrow()).v } == 5)); - let b: Value = Rc::new(RefCell::new(Tracked::new({ 6 }))); - let mut moved: Tracked = - ({ copy_or_move_into_param_4(Tracked::move_from({ b.as_pointer() })) }); + let b: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + TrackedImpl::construct(&__v.as_pointer(), { 6 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); + let mut moved: Tracked = ({ + copy_or_move_into_param_4({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + TrackedImpl::move_from(&__v.as_pointer(), { b.as_pointer() }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }) + }); assert!((moved.v == 6)); assert!((moved.copies == 0)); assert!((moved.moves == 2)); assert!(({ (*b.borrow()).v } == 0)); - let c: Value = Rc::new(RefCell::new(Tracked::new({ 7 }))); - let mut copied: Tracked = - ({ copy_or_move_into_param_4(Tracked::copy_from({ c.as_pointer() })) }); + let c: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + TrackedImpl::construct(&__v.as_pointer(), { 7 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); + let mut copied: Tracked = ({ + copy_or_move_into_param_4({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + TrackedImpl::copy_from(&__v.as_pointer(), { c.as_pointer() }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }) + }); assert!((copied.v == 7)); assert!((copied.copies == 1)); assert!((copied.moves == 1)); @@ -114,4 +117,27 @@ fn main_0() -> i32 { assert!(({ ((*i.borrow()) == 8) })); return 0; } +pub trait TrackedImpl { + fn construct(&self, v: i32); + fn copy_from(&self, o: Ptr); + fn move_from(&self, o: Ptr); +} +impl TrackedImpl for Ptr { + fn construct(&self, mut v: i32) { + field!((*self), v).write(v); + field!((*self), copies).write(0); + field!((*self), moves).write(0); + } + fn copy_from(&self, o: Ptr) { + field!((*self), v).write(o.with(|__s| __s.v)); + field!((*self), copies).write((o.with(|__s| __s.copies) + 1)); + field!((*self), moves).write(o.with(|__s| __s.moves)); + } + fn move_from(&self, o: Ptr) { + field!((*self), v).write(o.with(|__s| __s.v)); + field!((*self), copies).write(o.with(|__s| __s.copies)); + field!((*self), moves).write((o.with(|__s| __s.moves) + 1)); + field!(o, v).write(0); + } +} pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/forward_pack.rs b/tests/unit/out/refcount/forward_pack.rs index d77a08f20b..561384a886 100644 --- a/tests/unit/out/refcount/forward_pack.rs +++ b/tests/unit/out/refcount/forward_pack.rs @@ -11,8 +11,8 @@ pub const Overload_kLvalueOverload: Overload = 1; pub const Overload_kRvalueOverload: Overload = 2; pub const Overload_kIntLvalueOverload: Overload = 3; pub const Overload_kIntRvalueOverload: Overload = 4; -#[derive(Record, ByteRepr, VaArg, FnPtrArg, MoveCtor, CppFrom, Default)] -#[cpp_from(i32, new, value)] +#[derive(Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, MoveCtor, CppFrom, Default)] +#[cpp_from(i32, construct, value)] #[byte_size(12)] pub struct Tracked { #[offset(0)] @@ -22,31 +22,6 @@ pub struct Tracked { #[offset(8)] pub moves: i32, } -impl Tracked { - pub fn new(mut v: i32) -> Self { - Self { - v: v, - copies: 0, - moves: 0, - } - } - pub fn copy_from(o: Ptr) -> Self { - Self { - v: o.with(|__s| __s.v), - copies: (o.with(|__s| __s.copies) + 1), - moves: o.with(|__s| __s.moves), - } - } - pub fn move_from(o: Ptr) -> Self { - let __this: Tracked = Self { - v: o.with(|__s| __s.v), - copies: o.with(|__s| __s.copies), - moves: (o.with(|__s| __s.moves) + 1), - }; - field!(o, v).write(0); - __this - } -} impl Clone for Tracked { fn clone(&self) -> Self { let __src: Value = Rc::new(RefCell::new(Tracked { @@ -54,7 +29,9 @@ impl Clone for Tracked { copies: self.copies.clone(), moves: self.moves.clone(), })); - Tracked::copy_from(__src.as_pointer()) + let __v: Value = Rc::new(RefCell::new(::zero_init())); + TrackedImpl::copy_from(&__v.as_pointer(), __src.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() } } pub fn chosen_overload_0(_a0: Ptr) -> Overload { @@ -105,15 +82,7 @@ pub fn forward_pack_7( }; return digits; } -impl Pair { - pub fn new(x: Ptr, y: Ptr) -> Self { - Self { - a: Tracked::copy_from({ (x).clone() }), - b: Tracked::move_from({ (y).clone() }), - } - } -} -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, MoveCtor, Default)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, MoveCtor, Default)] #[byte_size(24)] pub struct Pair { #[offset(0)] @@ -123,16 +92,14 @@ pub struct Pair { #[byte_size(12)] pub b: Tracked, } -impl Pair { - pub fn move_from(_a0: Ptr) -> Self { - Self { - a: Tracked::move_from({ field_ptr!(_a0, a) }), - b: Tracked::move_from({ field_ptr!(_a0, b) }), - } - } -} pub fn forward_pack_into_ctor_8(args_0: Ptr, args_1: Ptr) -> Pair { - return Pair::new({ (args_0).clone() }, { (args_1).clone() }); + return { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + PairImpl::construct(&__v.as_pointer(), { (args_0).clone() }, { + (args_1).clone() + }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }; } pub fn main() { __cpp2rust_init_globals(); @@ -140,19 +107,31 @@ pub fn main() { } fn main_0() -> i32 { assert!((({ forward_pack_4() }) == 0)); - let a: Value = Rc::new(RefCell::new(Tracked::new({ 1 }))); + let a: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + TrackedImpl::construct(&__v.as_pointer(), { 1 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); assert!((({ forward_pack_5(a.as_pointer(),) }) == (Overload_kLvalueOverload as i32))); assert!(({ (*a.borrow()).v } == 1)); assert!( (({ - let _args: Value = Rc::new(RefCell::new(Tracked::new({ 2 }))); + let _args: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + TrackedImpl::construct(&__v.as_pointer(), { 2 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); forward_pack_6(_args.as_pointer()) }) == (Overload_kRvalueOverload as i32)) ); let i: Value = Rc::new(RefCell::new(3)); assert!( (({ - let _args_1: Value = Rc::new(RefCell::new(Tracked::new({ 4 }))); + let _args_1: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + TrackedImpl::construct(&__v.as_pointer(), { 4 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); let _args_3: Value = Rc::new(RefCell::new(5)); forward_pack_7( a.as_pointer(), @@ -164,9 +143,17 @@ fn main_0() -> i32 { ); assert!(({ (*a.borrow()).v } == 1)); assert!(({ ((*i.borrow()) == 3) })); - let lhs: Value = Rc::new(RefCell::new(Tracked::new({ 6 }))); + let lhs: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + TrackedImpl::construct(&__v.as_pointer(), { 6 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); let mut p: Pair = ({ - let _args_1: Value = Rc::new(RefCell::new(Tracked::new({ 7 }))); + let _args_1: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + TrackedImpl::construct(&__v.as_pointer(), { 7 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); forward_pack_into_ctor_8(lhs.as_pointer(), _args_1.as_pointer()) }); assert!((p.a.v == 6)); @@ -177,4 +164,57 @@ fn main_0() -> i32 { assert!(({ (*lhs.borrow()).v } == 6)); return 0; } +pub trait PairImpl { + fn construct(&self, x: Ptr, y: Ptr); + fn move_from(&self, _a0: Ptr); +} +impl PairImpl for Ptr { + fn construct(&self, x: Ptr, y: Ptr) { + field!((*self), a).write({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + TrackedImpl::copy_from(&__v.as_pointer(), { (x).clone() }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }); + field!((*self), b).write({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + TrackedImpl::move_from(&__v.as_pointer(), { (y).clone() }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }); + } + fn move_from(&self, _a0: Ptr) { + field!((*self), a).write({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + TrackedImpl::move_from(&__v.as_pointer(), { field_ptr!(_a0, a) }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }); + field!((*self), b).write({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + TrackedImpl::move_from(&__v.as_pointer(), { field_ptr!(_a0, b) }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }); + } +} +pub trait TrackedImpl { + fn construct(&self, v: i32); + fn copy_from(&self, o: Ptr); + fn move_from(&self, o: Ptr); +} +impl TrackedImpl for Ptr { + fn construct(&self, mut v: i32) { + field!((*self), v).write(v); + field!((*self), copies).write(0); + field!((*self), moves).write(0); + } + fn copy_from(&self, o: Ptr) { + field!((*self), v).write(o.with(|__s| __s.v)); + field!((*self), copies).write((o.with(|__s| __s.copies) + 1)); + field!((*self), moves).write(o.with(|__s| __s.moves)); + } + fn move_from(&self, o: Ptr) { + field!((*self), v).write(o.with(|__s| __s.v)); + field!((*self), copies).write(o.with(|__s| __s.copies)); + field!((*self), moves).write((o.with(|__s| __s.moves) + 1)); + field!(o, v).write(0); + } +} pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/friend.rs b/tests/unit/out/refcount/friend.rs index ad295b77eb..95cc1dd0b7 100644 --- a/tests/unit/out/refcount/friend.rs +++ b/tests/unit/out/refcount/friend.rs @@ -18,7 +18,7 @@ pub fn scaled_2(v: Ptr, mut k: i32) -> i32 { pub fn scaled_3(v: Ptr, mut k: f64) -> f64 { return ({ (v.with(|__s| __s.x) as f64) } * { k }); } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct V { #[offset(0)] @@ -32,7 +32,7 @@ impl CppEq for V { pub fn get_4(w: Ptr) -> i32 { return w.with(|__s| __s.x); } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct W_int_ { #[offset(0)] @@ -41,13 +41,13 @@ pub struct W_int_ { pub fn get_5(w: Ptr) -> i64 { return w.with(|__s| __s.x); } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct W_long_ { #[offset(0)] pub x: i64, } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct D { #[offset(0)] diff --git a/tests/unit/out/refcount/function_overloading.rs b/tests/unit/out/refcount/function_overloading.rs index 79f4108511..68f8ead863 100644 --- a/tests/unit/out/refcount/function_overloading.rs +++ b/tests/unit/out/refcount/function_overloading.rs @@ -21,7 +21,7 @@ pub fn foo_3(mut x: Ptr, mut y: Ptr, z: Ptr) -> i32 { pub fn bar_4(x: Ptr) -> i32 { return (x.read()); } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(1)] pub struct Foo {} pub fn func_5(mut x: i32) -> i32 { diff --git a/tests/unit/out/refcount/global_call_temporaries.rs b/tests/unit/out/refcount/global_call_temporaries.rs index 6f649cbeb7..d193aca603 100644 --- a/tests/unit/out/refcount/global_call_temporaries.rs +++ b/tests/unit/out/refcount/global_call_temporaries.rs @@ -94,7 +94,7 @@ thread_local!( }) })); ); -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct Item { #[offset(0)] diff --git a/tests/unit/out/refcount/global_init_side_effect.rs b/tests/unit/out/refcount/global_init_side_effect.rs index 457879c99c..b9d2bfff87 100644 --- a/tests/unit/out/refcount/global_init_side_effect.rs +++ b/tests/unit/out/refcount/global_init_side_effect.rs @@ -9,22 +9,23 @@ use std::rc::{Rc, Weak}; thread_local!( pub static total_0: Value = Rc::new(RefCell::new(0)); ); -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, CppFrom, Default)] -#[cpp_from(i32, new, value)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, CppFrom, Default)] +#[cpp_from(i32, construct, value)] #[byte_size(1)] pub struct S {} -impl S { - pub fn new(mut x: i32) -> Self { - let __this: S = Self {}; - total_0.with(|rc| *rc.borrow_mut() += x); - __this - } -} thread_local!( - pub static a_1: Value = Rc::new(RefCell::new(S::new({ 1 }))); + pub static a_1: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + SImpl::construct(&__v.as_pointer(), { 1 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); ); thread_local!( - pub static b_2: Value = Rc::new(RefCell::new(S::new({ 10 }))); + pub static b_2: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + SImpl::construct(&__v.as_pointer(), { 10 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); ); pub fn main() { __cpp2rust_init_globals(); @@ -34,6 +35,14 @@ fn main_0() -> i32 { assert!((total_0.with(|rc| *rc.borrow()) == 11)); return 0; } +pub trait SImpl { + fn construct(&self, x: i32); +} +impl SImpl for Ptr { + fn construct(&self, mut x: i32) { + total_0.with(|rc| *rc.borrow_mut() += x); + } +} pub fn __cpp2rust_init_globals() { let _ = total_0.with(|_| ()); let _ = a_1.with(|_| ()); diff --git a/tests/unit/out/refcount/global_non_const_init.rs b/tests/unit/out/refcount/global_non_const_init.rs index bbb65e63d8..314f10214c 100644 --- a/tests/unit/out/refcount/global_non_const_init.rs +++ b/tests/unit/out/refcount/global_non_const_init.rs @@ -32,30 +32,36 @@ thread_local!( pub static depends_on_call_6: Value = Rc::new(RefCell::new((from_call_5.with(|rc| *rc.borrow()) + 1))); ); -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg)] #[byte_size(4)] pub struct Ctor { #[offset(0)] pub v: i32, } -impl Ctor { - pub fn new_1() -> Self { - Self { v: ({ next_0() }) } - } - pub fn new_2(mut x: i32) -> Self { - Self { v: x } - } -} impl Default for Ctor { fn default() -> Self { - { Ctor::new_1() } + { + { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + CtorImpl::construct_1(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + } + } } } thread_local!( - pub static default_ctor_7: Value = Rc::new(RefCell::new(Ctor::new_1())); + pub static default_ctor_7: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + CtorImpl::construct_1(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); ); thread_local!( - pub static arg_ctor_8: Value = Rc::new(RefCell::new(Ctor::new_2({ 7 }))); + pub static arg_ctor_8: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + CtorImpl::construct_2(&__v.as_pointer(), { 7 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); ); thread_local!( pub static str_9: Value> = Rc::new(RefCell::new({ @@ -65,9 +71,13 @@ thread_local!( })); ); thread_local!( - pub static inline_member_11: Value = Rc::new(RefCell::new(Ctor::new_2({ 5 }))); + pub static inline_member_11: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + CtorImpl::construct_2(&__v.as_pointer(), { 5 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); ); -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(1)] pub struct Holder {} thread_local!( @@ -80,29 +90,40 @@ thread_local!( static once_13: Value = Rc::new(RefCell::new(({ next_0() }))); ); thread_local!( - static local_ctor_14: Value = Rc::new(RefCell::new(Ctor::new_2({ 3 }))); + static local_ctor_14: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + CtorImpl::construct_2(&__v.as_pointer(), { 3 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); ); -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg)] #[byte_size(4)] pub struct Singleton { #[offset(0)] pub hits: i32, } impl Singleton { - pub fn new() -> Self { - Self { hits: 0 } - } pub fn instance() -> Ptr { return s_15.with(|v| v.as_pointer()); } } impl Default for Singleton { fn default() -> Self { - { Singleton::new() } + { + { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + SingletonImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + } + } } } thread_local!( - static s_15: Value = Rc::new(RefCell::new(Singleton::new())); + static s_15: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + SingletonImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); ); pub fn main() { __cpp2rust_init_globals(); @@ -147,6 +168,26 @@ fn main_0() -> i32 { assert!((({ Singleton::instance() }) == ({ Singleton::instance() }))); return 0; } +pub trait CtorImpl { + fn construct_1(&self); + fn construct_2(&self, x: i32); +} +impl CtorImpl for Ptr { + fn construct_1(&self) { + field!((*self), v).write(({ next_0() })); + } + fn construct_2(&self, mut x: i32) { + field!((*self), v).write(x); + } +} +pub trait SingletonImpl { + fn construct(&self); +} +impl SingletonImpl for Ptr { + fn construct(&self) { + field!((*self), hits).write(0); + } +} pub fn __cpp2rust_init_globals() { let _ = signature_3.with(|_| ()); let _ = single_4.with(|_| ()); diff --git a/tests/unit/out/refcount/global_pointers.rs b/tests/unit/out/refcount/global_pointers.rs index ccb8e077cf..5fe456b7d9 100644 --- a/tests/unit/out/refcount/global_pointers.rs +++ b/tests/unit/out/refcount/global_pointers.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(16)] pub struct Entry { #[offset(0)] diff --git a/tests/unit/out/refcount/global_without_initializer.rs b/tests/unit/out/refcount/global_without_initializer.rs index a6fab960df..1990136dff 100644 --- a/tests/unit/out/refcount/global_without_initializer.rs +++ b/tests/unit/out/refcount/global_without_initializer.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct S { #[offset(0)] diff --git a/tests/unit/out/refcount/goto_aggregate_default.rs b/tests/unit/out/refcount/goto_aggregate_default.rs index b5af3228dc..0449ac8471 100644 --- a/tests/unit/out/refcount/goto_aggregate_default.rs +++ b/tests/unit/out/refcount/goto_aggregate_default.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct Point { #[offset(0)] diff --git a/tests/unit/out/refcount/goto_cleanup.rs b/tests/unit/out/refcount/goto_cleanup.rs index 8a1d49bf8f..6dd03f7dd7 100644 --- a/tests/unit/out/refcount/goto_cleanup.rs +++ b/tests/unit/out/refcount/goto_cleanup.rs @@ -76,7 +76,7 @@ pub fn from_switch_2(mut n: i32) -> i32 { }); panic!("ub: non-void function does not return a value") } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct wrapper { #[offset(0)] diff --git a/tests/unit/out/refcount/huffman.rs b/tests/unit/out/refcount/huffman.rs index 281ab736c9..f0bffcbeb3 100644 --- a/tests/unit/out/refcount/huffman.rs +++ b/tests/unit/out/refcount/huffman.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(24)] pub struct MinHeapNode { #[offset(0)] @@ -40,7 +40,7 @@ pub fn Swap_0(a: Ptr, b: Ptr) { right: (t.right).clone(), }); } -#[derive(Record, ByteRepr, FnPtrArg, MoveCtor, MoveAssign, Default)] +#[derive(Record, ByteRepr, ZeroInitialization, FnPtrArg, MoveCtor, MoveAssign, Default)] #[byte_size(32)] pub struct MinHeap { #[offset(0)] @@ -56,19 +56,6 @@ pub struct MinHeap { #[byte_size(8)] pub alloc: Option>>, } -impl MinHeap { - pub fn move_from(_a0: Ptr) -> Self { - Self { - size: { (*_a0.upgrade().deref()).size }, - capacity: { (*_a0.upgrade().deref()).capacity }, - arr: field!(_a0, arr) - .with_mut(|__v: &mut Option]>>>| __v.take()), - next: { (*_a0.upgrade().deref()).next }, - alloc: field!(_a0, alloc) - .with_mut(|__v: &mut Option>>| __v.take()), - } - } -} pub fn AllocMinHeap_1(mut capacity: i32) -> Option> { let mut minHeap: Option> = Some(Rc::new(RefCell::new(MinHeap { size: 0, @@ -285,6 +272,7 @@ pub trait MinHeapImpl { freq: Ptr>>>, n: i32, ); + fn move_from(&self, _a0: Ptr); fn move_assign(&self, _a0: Ptr) -> Ptr; } impl MinHeapImpl for Ptr { @@ -451,6 +439,18 @@ impl MinHeapImpl for Ptr { } } } + fn move_from(&self, _a0: Ptr) { + field!((*self), size).write({ (*_a0.upgrade().deref()).size }); + field!((*self), capacity).write({ (*_a0.upgrade().deref()).capacity }); + field!((*self), arr).write( + field!(_a0, arr) + .with_mut(|__v: &mut Option]>>>| __v.take()), + ); + field!((*self), next).write({ (*_a0.upgrade().deref()).next }); + field!((*self), alloc).write( + field!(_a0, alloc).with_mut(|__v: &mut Option>>| __v.take()), + ); + } fn move_assign(&self, _a0: Ptr) -> Ptr { field!((*self), size).write({ (*_a0.upgrade().deref()).size }); field!((*self), capacity).write({ (*_a0.upgrade().deref()).capacity }); diff --git a/tests/unit/out/refcount/immutable-deref-on-func-call.rs b/tests/unit/out/refcount/immutable-deref-on-func-call.rs index 5a70fb3370..49e3c1531e 100644 --- a/tests/unit/out/refcount/immutable-deref-on-func-call.rs +++ b/tests/unit/out/refcount/immutable-deref-on-func-call.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct Item { #[offset(0)] diff --git a/tests/unit/out/refcount/implicit_autoref.rs b/tests/unit/out/refcount/implicit_autoref.rs index cebb49b3f1..46b99638a5 100644 --- a/tests/unit/out/refcount/implicit_autoref.rs +++ b/tests/unit/out/refcount/implicit_autoref.rs @@ -6,20 +6,15 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, MoveCtor, MoveAssign, Default)] +#[derive( + Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, MoveCtor, MoveAssign, Default, +)] #[byte_size(24)] pub struct Holder { #[offset(0)] #[byte_size(24)] pub v: Vec, } -impl Holder { - pub fn move_from(_a0: Ptr) -> Self { - Self { - v: field!(_a0, v).with_mut(std::mem::take), - } - } -} pub fn write_through_0(mut p: Ptr) { p.write(42); } @@ -70,9 +65,13 @@ fn main_0() -> i32 { return 0; } pub trait HolderImpl { + fn move_from(&self, _a0: Ptr); fn move_assign(&self, _a0: Ptr) -> Ptr; } impl HolderImpl for Ptr { + fn move_from(&self, _a0: Ptr) { + field!((*self), v).write(field!(_a0, v).with_mut(std::mem::take)); + } fn move_assign(&self, _a0: Ptr) -> Ptr { field!((*self), v).with_mut(|__b0| { (*__b0) = field!(_a0, v).with_mut(std::mem::take); diff --git a/tests/unit/out/refcount/in_class_field_initializer.rs b/tests/unit/out/refcount/in_class_field_initializer.rs index 2fe6413521..9551e73365 100644 --- a/tests/unit/out/refcount/in_class_field_initializer.rs +++ b/tests/unit/out/refcount/in_class_field_initializer.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg)] #[byte_size(8)] pub struct Inner { #[offset(0)] @@ -19,7 +19,7 @@ impl Default for Inner { { Inner { x: 3, y: 4 } } } } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg)] #[byte_size(24)] pub struct S { #[offset(0)] @@ -45,7 +45,7 @@ impl Default for S { } } } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg)] #[byte_size(8)] pub struct Boxed_int_ { #[offset(0)] @@ -53,14 +53,9 @@ pub struct Boxed_int_ { #[offset(4)] pub tag: i32, } -impl Boxed_int_ { - pub fn new(mut x: i32, mut t: i32) -> Self { - Self { v: x, tag: t } - } -} impl Default for Boxed_int_ { fn default() -> Self { - { Boxed_int_ { v: 0_i32, tag: 7 } } + { ::zero_init() } } } pub fn main() { @@ -75,9 +70,22 @@ fn main_0() -> i32 { assert!((s.c.y == 4)); assert!((s.d.x == 3)); assert!((s.d.y == 4)); - let mut boxed: Boxed_int_ = Boxed_int_::new({ 5 }, { 9 }); + let mut boxed: Boxed_int_ = { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + Boxed_int_Impl::construct(&__v.as_pointer(), { 5 }, { 9 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }; assert!((boxed.v == 5)); assert!((boxed.tag == 9)); return 0; } +pub trait Boxed_int_Impl { + fn construct(&self, x: i32, t: i32); +} +impl Boxed_int_Impl for Ptr { + fn construct(&self, mut x: i32, mut t: i32) { + field!((*self), v).write(x); + field!((*self), tag).write(t); + } +} pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/init.rs b/tests/unit/out/refcount/init.rs index f52ddd1f4a..f9b6d67e5a 100644 --- a/tests/unit/out/refcount/init.rs +++ b/tests/unit/out/refcount/init.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct X { #[offset(0)] diff --git a/tests/unit/out/refcount/init_list.rs b/tests/unit/out/refcount/init_list.rs index f206a56750..cc2f4ea860 100644 --- a/tests/unit/out/refcount/init_list.rs +++ b/tests/unit/out/refcount/init_list.rs @@ -9,7 +9,7 @@ use std::rc::{Rc, Weak}; pub fn f_0(list: Vec) { let list: Value> = Rc::new(RefCell::new(list)); } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct Pair { #[offset(0)] @@ -17,7 +17,7 @@ pub struct Pair { #[offset(4)] pub b: i32, } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg)] #[byte_size(8)] pub struct Holder { #[offset(0)] @@ -36,7 +36,7 @@ impl Default for Holder { pub fn sum_1(p: Ptr) -> i32 { return ({ p.with(|__s| __s.a) } + { p.with(|__s| __s.b) }); } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg)] #[byte_size(4)] pub struct Seven { #[offset(0)] diff --git a/tests/unit/out/refcount/inline_container_fields.rs b/tests/unit/out/refcount/inline_container_fields.rs index 2b5d7aedba..0910c0c4fa 100644 --- a/tests/unit/out/refcount/inline_container_fields.rs +++ b/tests/unit/out/refcount/inline_container_fields.rs @@ -6,29 +6,19 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, MoveCtor, MoveAssign)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, MoveCtor, MoveAssign)] #[byte_size(48)] pub struct Elem { #[offset(0)] pub id: i32, #[offset(4)] #[byte_size(16)] + #[array_len(4)] pub hist: Box<[i32]>, #[offset(24)] #[byte_size(24)] pub coeffs: Vec, } -impl Elem { - pub fn move_from(_a0: Ptr) -> Self { - Self { - id: { (*_a0.upgrade().deref()).id }, - hist: Box::new(std::array::from_fn::<_, 4, _>(|__i: usize| { - (*_a0.upgrade().deref()).hist[(__i) as usize] - })), - coeffs: field!(_a0, coeffs).with_mut(std::mem::take), - } - } -} impl Default for Elem { fn default() -> Self { { @@ -40,16 +30,18 @@ impl Default for Elem { } } } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, MoveCtor, MoveAssign)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, MoveCtor, MoveAssign)] #[byte_size(88)] pub struct State { #[offset(0)] pub count: i32, #[offset(4)] #[byte_size(24)] + #[array_len(2, 3)] pub grid: Box<[Box<[i32]>]>, #[offset(28)] #[byte_size(12)] + #[array_len(3)] pub arr: Box<[i32]>, #[offset(40)] #[byte_size(24)] @@ -58,23 +50,6 @@ pub struct State { #[byte_size(24)] pub rows: Vec>, } -impl State { - pub fn move_from(_a0: Ptr) -> Self { - Self { - count: { (*_a0.upgrade().deref()).count }, - grid: Box::new(std::array::from_fn::<_, 2, _>(|__i: usize| { - Box::<[_]>::from(std::array::from_fn::<_, 3, _>(|__i2: usize| { - (*_a0.upgrade().deref()).grid[(__i) as usize][(__i2) as usize] - })) - })), - arr: (0..3) - .map(|__i| field_ref!(_a0, arr).decay().offset(__i).read()) - .collect(), - elems: field!(_a0, elems).with_mut(std::mem::take), - rows: field!(_a0, rows).with_mut(std::mem::take), - } - } -} impl Default for State { fn default() -> Self { { @@ -152,33 +127,20 @@ thread_local!( thread_local!( pub static move_assigns_5: Value = Rc::new(RefCell::new(0)); ); -#[derive(Record, ByteRepr, VaArg, FnPtrArg, MoveCtor, CopyAssign, MoveAssign)] +#[derive( + Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, MoveCtor, CopyAssign, MoveAssign, +)] #[byte_size(4)] pub struct Counted { #[offset(0)] pub v: i32, } -impl Counted { - pub fn copy_from(o: Ptr) -> Self { - let __this: Counted = Self { - v: o.with(|__s| __s.v), - }; - (*copies_2.with(Value::clone).borrow_mut()).prefix_inc(); - __this - } - pub fn move_from(o: Ptr) -> Self { - let __this: Counted = Self { - v: o.with(|__s| __s.v), - }; - field!(o, v).write(-1_i32); - (*moves_3.with(Value::clone).borrow_mut()).prefix_inc(); - __this - } -} impl Clone for Counted { fn clone(&self) -> Self { let __src: Value = Rc::new(RefCell::new(Counted { v: self.v.clone() })); - Counted::copy_from(__src.as_pointer()) + let __v: Value = Rc::new(RefCell::new(::zero_init())); + CountedImpl::copy_from(&__v.as_pointer(), __src.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() } } impl Default for Counted { @@ -304,7 +266,11 @@ fn main_0() -> i32 { ((array_elem!(elems.decay().offset(1_usize), hist, 2).read()) == 5) && ((array_elem!(first, hist, 2).read()) == 0) ); - let moved: Value = Rc::new(RefCell::new(State::move_from({ t.as_pointer() }))); + let moved: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + StateImpl::move_from(&__v.as_pointer(), { t.as_pointer() }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); assert!( ({ ((*moved.borrow()).grid[(0) as usize][(0) as usize] == 100) @@ -355,7 +321,7 @@ fn main_0() -> i32 { (*ca.borrow_mut())[1_usize].v = 5; let cb: Value> = Rc::new(RefCell::new( (0..3) - .map(|__i| Counted::move_from(ca.decay().offset(__i))) + .map(|__i| ::move_ctor(ca.decay().offset(__i))) .collect(), )); assert!( @@ -442,10 +408,21 @@ fn main_0() -> i32 { return 0; } pub trait CountedImpl { + fn copy_from(&self, o: Ptr); + fn move_from(&self, o: Ptr); fn copy_assign(&self, o: Ptr) -> Ptr; fn move_assign(&self, o: Ptr) -> Ptr; } impl CountedImpl for Ptr { + fn copy_from(&self, o: Ptr) { + field!((*self), v).write(o.with(|__s| __s.v)); + (*copies_2.with(Value::clone).borrow_mut()).prefix_inc(); + } + fn move_from(&self, o: Ptr) { + field!((*self), v).write(o.with(|__s| __s.v)); + field!(o, v).write(-1_i32); + (*moves_3.with(Value::clone).borrow_mut()).prefix_inc(); + } fn copy_assign(&self, o: Ptr) -> Ptr { field!((*self), v).write(o.with(|__s| __s.v)); (*copy_assigns_4.with(Value::clone).borrow_mut()).prefix_inc(); @@ -459,9 +436,17 @@ impl CountedImpl for Ptr { } } pub trait ElemImpl { + fn move_from(&self, _a0: Ptr); fn move_assign(&self, _a0: Ptr) -> Ptr; } impl ElemImpl for Ptr { + fn move_from(&self, _a0: Ptr) { + field!((*self), id).write({ (*_a0.upgrade().deref()).id }); + field!((*self), hist).write(Box::new(std::array::from_fn::<_, 4, _>(|__i: usize| { + (*_a0.upgrade().deref()).hist[(__i) as usize] + }))); + field!((*self), coeffs).write(field!(_a0, coeffs).with_mut(std::mem::take)); + } fn move_assign(&self, _a0: Ptr) -> Ptr { field!((*self), id).write({ (*_a0.upgrade().deref()).id }); { @@ -476,9 +461,25 @@ impl ElemImpl for Ptr { } } pub trait StateImpl { + fn move_from(&self, _a0: Ptr); fn move_assign(&self, _a0: Ptr) -> Ptr; } impl StateImpl for Ptr { + fn move_from(&self, _a0: Ptr) { + field!((*self), count).write({ (*_a0.upgrade().deref()).count }); + field!((*self), grid).write(Box::new(std::array::from_fn::<_, 2, _>(|__i: usize| { + Box::<[_]>::from(std::array::from_fn::<_, 3, _>(|__i2: usize| { + (*_a0.upgrade().deref()).grid[(__i) as usize][(__i2) as usize] + })) + }))); + field!((*self), arr).write( + (0..3) + .map(|__i| field_ref!(_a0, arr).decay().offset(__i).read()) + .collect(), + ); + field!((*self), elems).write(field!(_a0, elems).with_mut(std::mem::take)); + field!((*self), rows).write(field!(_a0, rows).with_mut(std::mem::take)); + } fn move_assign(&self, _a0: Ptr) -> Ptr { field!((*self), count).write({ (*_a0.upgrade().deref()).count }); { diff --git a/tests/unit/out/refcount/kruskal.rs b/tests/unit/out/refcount/kruskal.rs index 6ebb4fdbdc..bb969d73c1 100644 --- a/tests/unit/out/refcount/kruskal.rs +++ b/tests/unit/out/refcount/kruskal.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(16)] pub struct Edge { #[offset(0)] @@ -99,7 +99,7 @@ pub fn quicksort_1(arr: Ptr>>>, mut start: i32, mut end ({ quicksort_1((arr).clone(), start, (p - 1)) }); ({ quicksort_1((arr).clone(), (p + 1), end) }); } -#[derive(Record, ByteRepr, FnPtrArg, MoveCtor, MoveAssign, Default)] +#[derive(Record, ByteRepr, ZeroInitialization, FnPtrArg, MoveCtor, MoveAssign, Default)] #[byte_size(24)] pub struct DisjointSet { #[offset(0)] @@ -111,16 +111,7 @@ pub struct DisjointSet { #[offset(16)] pub n: i32, } -impl DisjointSet { - pub fn move_from(_a0: Ptr) -> Self { - Self { - rank: field!(_a0, rank).with_mut(|__v: &mut Option>>| __v.take()), - parent: field!(_a0, parent).with_mut(|__v: &mut Option>>| __v.take()), - n: { (*_a0.upgrade().deref()).n }, - } - } -} -#[derive(Record, ByteRepr, FnPtrArg, MoveCtor, MoveAssign, Default)] +#[derive(Record, ByteRepr, ZeroInitialization, FnPtrArg, MoveCtor, MoveAssign, Default)] #[byte_size(16)] pub struct Graph { #[offset(0)] @@ -131,15 +122,6 @@ pub struct Graph { #[offset(12)] pub E: i32, } -impl Graph { - pub fn move_from(_a0: Ptr) -> Self { - Self { - edges: field!(_a0, edges).with_mut(|__v: &mut Option>>| __v.take()), - V: { (*_a0.upgrade().deref()).V }, - E: { (*_a0.upgrade().deref()).E }, - } - } -} pub fn MSTKruskal_2(graph: Ptr) -> f64 { ({ quicksort_1(field_ptr!(graph, edges), 0, (graph.with(|__s| __s.E) - 1)) }); let set: Value = Rc::new(RefCell::new(DisjointSet { @@ -252,6 +234,7 @@ pub trait DisjointSetImpl { fn makeSet(&self); fn find(&self, x: i32) -> i32; fn merge(&self, x: i32, y: i32); + fn move_from(&self, _a0: Ptr); fn move_assign(&self, _a0: Ptr) -> Ptr; } impl DisjointSetImpl for Ptr { @@ -363,6 +346,13 @@ impl DisjointSetImpl for Ptr { ); } } + fn move_from(&self, _a0: Ptr) { + field!((*self), rank) + .write(field!(_a0, rank).with_mut(|__v: &mut Option>>| __v.take())); + field!((*self), parent) + .write(field!(_a0, parent).with_mut(|__v: &mut Option>>| __v.take())); + field!((*self), n).write({ (*_a0.upgrade().deref()).n }); + } fn move_assign(&self, _a0: Ptr) -> Ptr { field!((*self), rank).with_mut(|__b0| { (*__b0) = field!(_a0, rank).with_mut(|__v: &mut Option>>| __v.take()); @@ -376,9 +366,16 @@ impl DisjointSetImpl for Ptr { } } pub trait GraphImpl { + fn move_from(&self, _a0: Ptr); fn move_assign(&self, _a0: Ptr) -> Ptr; } impl GraphImpl for Ptr { + fn move_from(&self, _a0: Ptr) { + field!((*self), edges) + .write(field!(_a0, edges).with_mut(|__v: &mut Option>>| __v.take())); + field!((*self), V).write({ (*_a0.upgrade().deref()).V }); + field!((*self), E).write({ (*_a0.upgrade().deref()).E }); + } fn move_assign(&self, _a0: Ptr) -> Ptr { field!((*self), edges).with_mut(|__b0| { (*__b0) = diff --git a/tests/unit/out/refcount/lambda_as_field.rs b/tests/unit/out/refcount/lambda_as_field.rs index c519396cbf..edd5e2beea 100644 --- a/tests/unit/out/refcount/lambda_as_field.rs +++ b/tests/unit/out/refcount/lambda_as_field.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Destructible)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Destructible)] #[byte_size(1)] pub struct Guard__lambda_at_lambda_as_field_cpp_34_13__ { #[offset(0)] @@ -24,14 +24,14 @@ impl Default for Guard__lambda_at_lambda_as_field_cpp_34_13__ { } } } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Destructible, Default)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Destructible, Default)] #[byte_size(8)] pub struct Guard__lambda_at_lambda_as_field_cpp_45_13__ { #[offset(0)] #[byte_size(8)] pub f: FnPtr, } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg)] #[byte_size(8)] pub struct Holder__lambda_at_lambda_as_field_cpp_39_12__ { #[offset(0)] @@ -54,7 +54,7 @@ impl Default for Holder__lambda_at_lambda_as_field_cpp_39_12__ { } } } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct Holder__lambda_at_lambda_as_field_cpp_51_12__ { #[offset(0)] @@ -63,8 +63,8 @@ pub struct Holder__lambda_at_lambda_as_field_cpp_51_12__ { #[offset(4)] pub calls: i32, } -#[derive(Record, ByteRepr, FnPtrArg, MoveCtor, CppFrom)] -#[cpp_from(FnPtr i32 > , new, value)] +#[derive(Record, ByteRepr, ZeroInitialization, FnPtrArg, MoveCtor, CppFrom)] +#[cpp_from(FnPtr i32 > , construct, value)] #[byte_size(8)] pub struct Moving__lambda_at_lambda_as_field_cpp_64_12__ { #[offset(0)] @@ -73,33 +73,9 @@ pub struct Moving__lambda_at_lambda_as_field_cpp_64_12__ { #[offset(4)] pub moves: i32, } -impl Moving__lambda_at_lambda_as_field_cpp_64_12__ { - pub fn new(f: FnPtr i32>) -> Self { - let f: Value i32>> = Rc::new(RefCell::new(f)); - Self { - f: (*f.borrow()).copy_from(), - moves: 0, - } - } - pub fn move_from(o: Ptr) -> Self { - Self { - f: (*o.upgrade().deref()).f.move_from(), - moves: (o.with(|__s| __s.moves) + 1), - } - } -} impl Default for Moving__lambda_at_lambda_as_field_cpp_64_12__ { fn default() -> Self { - { - Moving__lambda_at_lambda_as_field_cpp_64_12__ { - f: FnPtr:: i32>::new(|mut x: i32| -> i32 { - { - return (x * 2); - } - }), - moves: 0, - } - } + { ::zero_init() } } } pub fn wrap_1(fn_: FnPtr i32>) -> FnPtr i32> { @@ -219,18 +195,28 @@ fn main_0() -> i32 { let ww: Value i32>> = Rc::new(RefCell::new(({ wrap_2({ (*w.borrow()).copy_from() }) }))); assert!(({ (({ (*ww.borrow()).call(2,) }) == 202) })); - let m: Value = Rc::new(RefCell::new( - Moving__lambda_at_lambda_as_field_cpp_64_12__::new({ + let m: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new( + ::zero_init(), + )); + Moving__lambda_at_lambda_as_field_cpp_64_12__Impl::construct(&__v.as_pointer(), { FnPtr:: i32>::new(|mut x: i32| -> i32 { { return (x * 2); } }) - }), - )); - let m2: Value = Rc::new(RefCell::new( - Moving__lambda_at_lambda_as_field_cpp_64_12__::move_from({ m.as_pointer() }), - )); + }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); + let m2: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new( + ::zero_init(), + )); + Moving__lambda_at_lambda_as_field_cpp_64_12__Impl::move_from(&__v.as_pointer(), { + m.as_pointer() + }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); assert!(({ (({ (*m2.borrow()).f.call(3,) }) == 6) })); assert!(({ (*m2.borrow()).moves } == 1)); return 0; @@ -277,6 +263,23 @@ impl Holder__lambda_at_lambda_as_field_cpp_51_12__Impl return ({ (*(*self).upgrade().deref()).f.call(x) }); } } +pub trait Moving__lambda_at_lambda_as_field_cpp_64_12__Impl { + fn construct(&self, f: FnPtr i32>); + fn move_from(&self, o: Ptr); +} +impl Moving__lambda_at_lambda_as_field_cpp_64_12__Impl + for Ptr +{ + fn construct(&self, f: FnPtr i32>) { + let f: Value i32>> = Rc::new(RefCell::new(f)); + field!((*self), f).write((*f.borrow()).copy_from()); + field!((*self), moves).write(0); + } + fn move_from(&self, o: Ptr) { + field!((*self), f).write((*o.upgrade().deref()).f.move_from()); + field!((*self), moves).write((o.with(|__s| __s.moves) + 1)); + } +} pub fn __cpp2rust_init_globals() { let _ = count_0.with(|_| ()); } diff --git a/tests/unit/out/refcount/lambda_capture_copy_ctor.rs b/tests/unit/out/refcount/lambda_capture_copy_ctor.rs index 2ac27630c5..0379ac5c45 100644 --- a/tests/unit/out/refcount/lambda_capture_copy_ctor.rs +++ b/tests/unit/out/refcount/lambda_capture_copy_ctor.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Record, ByteRepr, VaArg, FnPtrArg, MoveCtor)] +#[derive(Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, MoveCtor)] #[byte_size(8)] pub struct Counted { #[offset(0)] @@ -14,66 +14,51 @@ pub struct Counted { #[offset(4)] pub moves: i32, } -impl Counted { - pub fn new() -> Self { - Self { - copies: 0, - moves: 0, - } - } - pub fn copy_from(o: Ptr) -> Self { - Self { - copies: (o.with(|__s| __s.copies) + 1), - moves: o.with(|__s| __s.moves), - } - } - pub fn move_from(o: Ptr) -> Self { - Self { - copies: o.with(|__s| __s.copies), - moves: (o.with(|__s| __s.moves) + 1), - } - } -} impl Clone for Counted { fn clone(&self) -> Self { let __src: Value = Rc::new(RefCell::new(Counted { copies: self.copies.clone(), moves: self.moves.clone(), })); - Counted::copy_from(__src.as_pointer()) + let __v: Value = Rc::new(RefCell::new(::zero_init())); + CountedImpl::copy_from(&__v.as_pointer(), __src.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() } } impl Default for Counted { fn default() -> Self { - { Counted::new() } + { + { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + CountedImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + } + } } } thread_local!( pub static drops_0: Value = Rc::new(RefCell::new(0)); ); -#[derive(Record, ByteRepr, VaArg, FnPtrArg, MoveCtor, Destructible)] +#[derive(Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, MoveCtor, Destructible)] #[byte_size(1)] pub struct Dropped {} -impl Dropped { - pub fn new() -> Self { - Self {} - } - pub fn copy_from(_a0: Ptr) -> Self { - Self {} - } - pub fn move_from(_a0: Ptr) -> Self { - Self {} - } -} impl Clone for Dropped { fn clone(&self) -> Self { let __src: Value = Rc::new(RefCell::new(Dropped {})); - Dropped::copy_from(__src.as_pointer()) + let __v: Value = Rc::new(RefCell::new(::zero_init())); + DroppedImpl::copy_from(&__v.as_pointer(), __src.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() } } impl Default for Dropped { fn default() -> Self { - { Dropped::new() } + { + { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + DroppedImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + } + } } } pub fn main() { @@ -81,10 +66,18 @@ pub fn main() { libcc2rs::cpp2rust_exit(main_0()); } fn main_0() -> i32 { - let c: Value = Rc::new(RefCell::new(Counted::new())); + let c: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + CountedImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); let f: Value i32>> = Rc::new(RefCell::new(lambda!( { - let c: Value = Rc::new(RefCell::new(Counted::copy_from({ c.as_pointer() }))); + let c: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + CountedImpl::copy_from(&__v.as_pointer(), { c.as_pointer() }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); }, || -> i32 { return (({ (*c.borrow()).copies } * 10) + { (*c.borrow()).moves }); @@ -100,11 +93,18 @@ fn main_0() -> i32 { ({ lambda!( { - let c: Value = - Rc::new(RefCell::new(Counted::copy_from({ c.as_pointer() }))); + let c: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + CountedImpl::copy_from(&__v.as_pointer(), { c.as_pointer() }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); }, || -> Counted { - return Counted::copy_from({ c.as_pointer() }); + return { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + CountedImpl::copy_from(&__v.as_pointer(), { c.as_pointer() }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }; } ) .call() @@ -113,14 +113,21 @@ fn main_0() -> i32 { }; assert!((returned == 2)); let arr: Value> = Rc::new(RefCell::new(Box::new( - std::array::from_fn::<_, 2, _>(|_| Counted::new()), + std::array::from_fn::<_, 2, _>(|_| { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + CountedImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }), ))); 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| Counted::copy_from({ arr.decay().offset(__i) }), - )))); + let arr: Value> = Rc::new(RefCell::new(Box::new( + std::array::from_fn::<_, 2, _>(|__i: usize| { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + CountedImpl::copy_from(&__v.as_pointer(), { arr.decay().offset(__i) }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }), + ))); }, || -> i32 { return ({ (*arr.borrow())[(0) as usize].copies } + { @@ -136,7 +143,11 @@ fn main_0() -> i32 { { let m: Value> = Rc::new(RefCell::new(lambda!( { - let d: Value = Rc::new(RefCell::new(Dropped::new())); + let d: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + DroppedImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); }, || {} ))); @@ -150,7 +161,11 @@ fn main_0() -> i32 { { let k: Value> = Rc::new(RefCell::new(lambda!( { - let d: Value = Rc::new(RefCell::new(Dropped::new())); + let d: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + DroppedImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); }, || {} ))); @@ -164,7 +179,11 @@ fn main_0() -> i32 { { let inner: Value> = Rc::new(RefCell::new(lambda!( { - let d: Value = Rc::new(RefCell::new(Dropped::new())); + let d: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + DroppedImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); }, || {} ))); @@ -183,10 +202,35 @@ fn main_0() -> i32 { assert!((drops_0.with(|rc| *rc.borrow()) == 6)); return 0; } +pub trait CountedImpl { + fn construct(&self); + fn copy_from(&self, o: Ptr); + fn move_from(&self, o: Ptr); +} +impl CountedImpl for Ptr { + fn construct(&self) { + field!((*self), copies).write(0); + field!((*self), moves).write(0); + } + fn copy_from(&self, o: Ptr) { + field!((*self), copies).write((o.with(|__s| __s.copies) + 1)); + field!((*self), moves).write(o.with(|__s| __s.moves)); + } + fn move_from(&self, o: Ptr) { + field!((*self), copies).write(o.with(|__s| __s.copies)); + field!((*self), moves).write((o.with(|__s| __s.moves) + 1)); + } +} pub trait DroppedImpl { + fn construct(&self); + fn copy_from(&self, _a0: Ptr); + fn move_from(&self, _a0: Ptr); fn destructor(&self); } impl DroppedImpl for Ptr { + fn construct(&self) {} + fn copy_from(&self, _a0: Ptr) {} + fn move_from(&self, _a0: Ptr) {} fn destructor(&self) { (*drops_0.with(Value::clone).borrow_mut()).postfix_inc(); } diff --git a/tests/unit/out/refcount/lambda_capture_ref.rs b/tests/unit/out/refcount/lambda_capture_ref.rs index e565770443..6ececc76f2 100644 --- a/tests/unit/out/refcount/lambda_capture_ref.rs +++ b/tests/unit/out/refcount/lambda_capture_ref.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct S { #[offset(0)] diff --git a/tests/unit/out/refcount/lambda_capture_star_this.rs b/tests/unit/out/refcount/lambda_capture_star_this.rs index 31494c5882..baebf700dd 100644 --- a/tests/unit/out/refcount/lambda_capture_star_this.rs +++ b/tests/unit/out/refcount/lambda_capture_star_this.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct S { #[offset(0)] diff --git a/tests/unit/out/refcount/lambda_capture_this.rs b/tests/unit/out/refcount/lambda_capture_this.rs index 2acbb881f7..66a24ed128 100644 --- a/tests/unit/out/refcount/lambda_capture_this.rs +++ b/tests/unit/out/refcount/lambda_capture_this.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct S { #[offset(0)] diff --git a/tests/unit/out/refcount/lambda_capture_value.rs b/tests/unit/out/refcount/lambda_capture_value.rs index 08ad9d928b..4fa6b5688b 100644 --- a/tests/unit/out/refcount/lambda_capture_value.rs +++ b/tests/unit/out/refcount/lambda_capture_value.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct S { #[offset(0)] diff --git a/tests/unit/out/refcount/lambda_decltype_type.rs b/tests/unit/out/refcount/lambda_decltype_type.rs index 17962c2146..7237a10fac 100644 --- a/tests/unit/out/refcount/lambda_decltype_type.rs +++ b/tests/unit/out/refcount/lambda_decltype_type.rs @@ -6,11 +6,12 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg)] #[byte_size(24)] pub struct Sorted__lambda_at_lambda_decltype_type_cpp_22_18__ { #[offset(0)] #[byte_size(16)] + #[array_len(4)] pub items: Box<[i32]>, #[offset(16)] pub size: i32, @@ -33,11 +34,12 @@ impl Default for Sorted__lambda_at_lambda_decltype_type_cpp_22_18__ { } } } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg)] #[byte_size(24)] pub struct Sorted__lambda_at_lambda_decltype_type_cpp_19_23__ { #[offset(0)] #[byte_size(16)] + #[array_len(4)] pub items: Box<[i32]>, #[offset(16)] pub size: i32, diff --git a/tests/unit/out/refcount/lambda_generic_explicit_args.rs b/tests/unit/out/refcount/lambda_generic_explicit_args.rs index 3c014bc2bc..d8889d4ff4 100644 --- a/tests/unit/out/refcount/lambda_generic_explicit_args.rs +++ b/tests/unit/out/refcount/lambda_generic_explicit_args.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct Val { #[offset(0)] diff --git a/tests/unit/out/refcount/lambda_immediate_init.rs b/tests/unit/out/refcount/lambda_immediate_init.rs index b8c8d87ea3..6aeb494e2e 100644 --- a/tests/unit/out/refcount/lambda_immediate_init.rs +++ b/tests/unit/out/refcount/lambda_immediate_init.rs @@ -9,7 +9,7 @@ use std::rc::{Rc, Weak}; pub fn pick_0(mut x: i32) -> i32 { return x; } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg)] #[byte_size(8)] pub struct S { #[offset(0)] diff --git a/tests/unit/out/refcount/lambda_move_only_capture.rs b/tests/unit/out/refcount/lambda_move_only_capture.rs index c873573d97..4d45c1d7cd 100644 --- a/tests/unit/out/refcount/lambda_move_only_capture.rs +++ b/tests/unit/out/refcount/lambda_move_only_capture.rs @@ -6,37 +6,35 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Record, ByteRepr, FnPtrArg, MoveCtor, CppFrom, Destructible, Default)] -#[cpp_from(i32, new, value)] +#[derive( + Record, ByteRepr, ZeroInitialization, FnPtrArg, MoveCtor, CppFrom, Destructible, Default, +)] +#[cpp_from(i32, construct, value)] #[byte_size(8)] pub struct Owner { #[offset(0)] #[byte_size(8)] pub p: Ptr, } -impl Owner { - pub fn new(mut v: i32) -> Self { - Self { p: Ptr::alloc(v) } - } - pub fn move_from(o: Ptr) -> Self { - let __this: Owner = Self { - p: o.with(|__s| __s.p.clone()), - }; - field!(o, p).write(Ptr::::null()); - __this - } -} pub fn main() { __cpp2rust_init_globals(); libcc2rs::cpp2rust_exit(main_0()); } fn main_0() -> i32 { - let o: Value = Rc::new(RefCell::new(Owner::new({ 5 }))); + let o: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + OwnerImpl::construct(&__v.as_pointer(), { 5 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); let _dtor_o = std::mem::DropGuard::new(o.clone(), |__p| Destructible::destroy(__p.as_pointer())); let f: Value i32>> = Rc::new(RefCell::new(lambda!( { - let h: Value = Rc::new(RefCell::new(Owner::move_from({ o.as_pointer() }))); + let h: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + OwnerImpl::move_from(&__v.as_pointer(), { o.as_pointer() }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); }, || -> i32 { return ({ (*h.borrow()).p.clone() }.read()); @@ -53,7 +51,11 @@ fn main_0() -> i32 { let total: Value = Rc::new(RefCell::new(0)); let consume: Value> = Rc::new(RefCell::new(lambda!( { - let h: Value = Rc::new(RefCell::new(Owner::new({ 7 }))); + let h: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + OwnerImpl::construct(&__v.as_pointer(), { 7 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); let total: Ptr = total.as_pointer(); }, || { @@ -70,7 +72,11 @@ fn main_0() -> i32 { ({ (*consume.borrow()).call() }); ({ (*consume.borrow()).call() }); assert!(({ ((*total.borrow()) == 7) })); - let o2: Value = Rc::new(RefCell::new(Owner::new({ 9 }))); + let o2: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + OwnerImpl::construct(&__v.as_pointer(), { 9 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); let _dtor_o2 = std::mem::DropGuard::new(o2.clone(), |__p| Destructible::destroy(__p.as_pointer())); let t: Value i32>> = @@ -82,18 +88,30 @@ fn main_0() -> i32 { return 0; } pub trait OwnerImpl { + fn construct(&self, v: i32); + fn move_from(&self, o: Ptr); fn destructor(&self); fn take(&self) -> FnPtr i32>; } impl OwnerImpl for Ptr { + fn construct(&self, mut v: i32) { + field!((*self), p).write(Ptr::alloc(v)); + } + fn move_from(&self, o: Ptr) { + field!((*self), p).write(o.with(|__s| __s.p.clone())); + field!(o, p).write(Ptr::::null()); + } fn destructor(&self) { (*self).with(|__s| __s.p.clone()).delete(); } fn take(&self) -> FnPtr i32> { return lambda!( { - let self_: Value = - Rc::new(RefCell::new(Owner::move_from({ (*self).clone() }))); + let self_: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + OwnerImpl::move_from(&__v.as_pointer(), { (*self).clone() }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); }, || -> i32 { return ({ (*self_.borrow()).p.clone() }.read()); diff --git a/tests/unit/out/refcount/lambda_nested.rs b/tests/unit/out/refcount/lambda_nested.rs index c0842b1f29..ba8bf59b16 100644 --- a/tests/unit/out/refcount/lambda_nested.rs +++ b/tests/unit/out/refcount/lambda_nested.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct S { #[offset(0)] diff --git a/tests/unit/out/refcount/lambda_static_local.rs b/tests/unit/out/refcount/lambda_static_local.rs index cc78231f56..7015e7960a 100644 --- a/tests/unit/out/refcount/lambda_static_local.rs +++ b/tests/unit/out/refcount/lambda_static_local.rs @@ -78,7 +78,7 @@ thread_local!( thread_local!( static calls_3: Value = Rc::new(RefCell::new(0)); ); -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct P_4 { #[offset(0)] diff --git a/tests/unit/out/refcount/libc_struct_without_default.rs b/tests/unit/out/refcount/libc_struct_without_default.rs index c7c8d79972..77edb687dc 100644 --- a/tests/unit/out/refcount/libc_struct_without_default.rs +++ b/tests/unit/out/refcount/libc_struct_without_default.rs @@ -6,26 +6,17 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, MoveCtor, MoveAssign)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, MoveCtor, MoveAssign)] #[byte_size(32)] pub struct UserDefined { #[offset(0)] #[byte_size(4)] + #[array_len(1)] pub a: Box<[i32]>, #[offset(8)] #[byte_size(24)] pub v: Vec, } -impl UserDefined { - pub fn move_from(_a0: Ptr) -> Self { - Self { - a: (0..1) - .map(|__i| field_ref!(_a0, a).decay().offset(__i).read()) - .collect(), - v: field!(_a0, v).with_mut(std::mem::take), - } - } -} impl Default for UserDefined { fn default() -> Self { { @@ -36,7 +27,7 @@ impl Default for UserDefined { } } } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg)] #[byte_size(16)] pub struct FieldIsLibcType { #[offset(0)] @@ -87,9 +78,18 @@ fn main_0() -> i32 { return 0; } pub trait UserDefinedImpl { + fn move_from(&self, _a0: Ptr); fn move_assign(&self, _a0: Ptr) -> Ptr; } impl UserDefinedImpl for Ptr { + fn move_from(&self, _a0: Ptr) { + field!((*self), a).write( + (0..1) + .map(|__i| field_ref!(_a0, a).decay().offset(__i).read()) + .collect(), + ); + field!((*self), v).write(field!(_a0, v).with_mut(std::mem::take)); + } fn move_assign(&self, _a0: Ptr) -> Ptr { { for __i in 0..1 { diff --git a/tests/unit/out/refcount/linked_list.rs b/tests/unit/out/refcount/linked_list.rs index c64d2c1dff..703bb1b63e 100644 --- a/tests/unit/out/refcount/linked_list.rs +++ b/tests/unit/out/refcount/linked_list.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(16)] pub struct Node { #[offset(0)] diff --git a/tests/unit/out/refcount/local_anon_struct_collision.rs b/tests/unit/out/refcount/local_anon_struct_collision.rs index 5bf56af28a..c00fcb21b3 100644 --- a/tests/unit/out/refcount/local_anon_struct_collision.rs +++ b/tests/unit/out/refcount/local_anon_struct_collision.rs @@ -12,7 +12,7 @@ pub fn first_0() -> i32 { p.y = 2; return (p.x + p.y); } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct anon_1 { #[offset(0)] @@ -26,7 +26,7 @@ pub fn second_2() -> i32 { q.b = 20_i64; return ((q.a + q.b) as i32); } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(16)] pub struct anon_3 { #[offset(0)] diff --git a/tests/unit/out/refcount/local_record_template.rs b/tests/unit/out/refcount/local_record_template.rs index 7b9a25a7ed..a8902e358c 100644 --- a/tests/unit/out/refcount/local_record_template.rs +++ b/tests/unit/out/refcount/local_record_template.rs @@ -29,13 +29,13 @@ pub fn wrap_10(mut v: i64) -> i32 { let mut l: Local_7 = Local_7 { x: v }; return ({ get_6(l) }); } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct Local_5 { #[offset(0)] pub x: i32, } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct Local_7 { #[offset(0)] @@ -45,7 +45,7 @@ pub fn other_11() -> i32 { let mut l: Local_1 = Local_1 { x: 3_i64, y: 4_i64 }; return (({ get_0(l) }) + (l.y as i32)); } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(16)] pub struct Local_1 { #[offset(0)] @@ -66,7 +66,7 @@ fn main_0() -> i32 { assert!((({ wrap_10(6_i64,) }) == 6)); return 0; } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct Local_3 { #[offset(0)] diff --git a/tests/unit/out/refcount/map.rs b/tests/unit/out/refcount/map.rs index a69a7e1eff..f24a258731 100644 --- a/tests/unit/out/refcount/map.rs +++ b/tests/unit/out/refcount/map.rs @@ -12,7 +12,7 @@ pub fn foo_0(mut x: u32) { pub fn bar_1(x: Ptr) { x.write((x.read()).wrapping_add(1_u32)); } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg)] #[byte_size(8)] pub struct Point { #[offset(0)] @@ -20,17 +20,15 @@ pub struct Point { #[offset(4)] pub y: i32, } -impl Point { - pub fn new_1() -> Self { - Self { x: 0, y: 0 } - } - pub fn new_2(mut x: i32, mut y: i32) -> Self { - Self { x: x, y: y } - } -} impl Default for Point { fn default() -> Self { - { Point::new_1() } + { + { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + PointImpl::construct_1(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + } + } } } pub fn main() { @@ -730,7 +728,11 @@ fn main_0() -> i32 { let (__it, __inserted) = Map::emplace(points.as_pointer(), &key1.as_pointer(), || { ( Rc::new(RefCell::new(key1.as_pointer().read())), - Rc::new(RefCell::new(Point::new_2({ 3 }, { 4 }))), + Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + PointImpl::construct_2(&__v.as_pointer(), { 3 }, { 4 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })), ) }); ( @@ -773,7 +775,11 @@ fn main_0() -> i32 { let mut __tmp_53 = { ( Rc::new(RefCell::new(({ (*key8.borrow()) }))), - Rc::new(RefCell::new(Point::new_2({ 5 }, { 6 }))), + Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + PointImpl::construct_2(&__v.as_pointer(), { 5 }, { 6 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })), ) }; let (__it, __inserted) = @@ -917,4 +923,18 @@ fn main_0() -> i32 { assert!(({ ((*pk1.borrow()) == 0) && ((*pk2.borrow()) == 0) })); return 0; } +pub trait PointImpl { + fn construct_1(&self); + fn construct_2(&self, x: i32, y: i32); +} +impl PointImpl for Ptr { + fn construct_1(&self) { + field!((*self), x).write(0); + field!((*self), y).write(0); + } + fn construct_2(&self, mut x: i32, mut y: i32) { + field!((*self), x).write(x); + field!((*self), y).write(y); + } +} pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/map_key_copy_ctor.rs b/tests/unit/out/refcount/map_key_copy_ctor.rs index 2ad4eb6d53..34c8521669 100644 --- a/tests/unit/out/refcount/map_key_copy_ctor.rs +++ b/tests/unit/out/refcount/map_key_copy_ctor.rs @@ -15,32 +15,13 @@ thread_local!( thread_local!( pub static made_2: Value = Rc::new(RefCell::new(0)); ); -#[derive(Record, ByteRepr, VaArg, FnPtrArg, MoveCtor, CppFrom, Default)] -#[cpp_from(i32, new, value)] +#[derive(Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, MoveCtor, CppFrom, Default)] +#[cpp_from(i32, construct, value)] #[byte_size(4)] pub struct Key { #[offset(0)] pub k: i32, } -impl Key { - pub fn new(mut k: i32) -> Self { - Self { k: k } - } - pub fn copy_from(o: Ptr) -> Self { - let __this: Key = Self { - k: o.with(|__s| __s.k), - }; - (*copies_0.with(Value::clone).borrow_mut()).prefix_inc(); - __this - } - pub fn move_from(o: Ptr) -> Self { - let __this: Key = Self { - k: o.with(|__s| __s.k), - }; - (*moves_1.with(Value::clone).borrow_mut()).prefix_inc(); - __this - } -} impl CppLess for Key { fn less(__lhs: &Ptr, __rhs: &Ptr) -> bool { KeyImpl::operator_lt(__lhs, __rhs.clone()) @@ -55,12 +36,18 @@ impl CppEq for Key { impl Clone for Key { fn clone(&self) -> Self { let __src: Value = Rc::new(RefCell::new(Key { k: self.k.clone() })); - Key::copy_from(__src.as_pointer()) + let __v: Value = Rc::new(RefCell::new(::zero_init())); + KeyImpl::copy_from(&__v.as_pointer(), __src.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() } } pub fn make_key_3(mut k: i32) -> Key { (*made_2.with(Value::clone).borrow_mut()).prefix_inc(); - return Key::new({ k }); + return { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + KeyImpl::construct(&__v.as_pointer(), { k }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }; } pub fn main() { __cpp2rust_init_globals(); @@ -69,9 +56,21 @@ pub fn main() { fn main_0() -> i32 { let mut ok: bool = true; let m: Value, Value)>> = Rc::new(RefCell::new(Map::new())); - let a: Value = Rc::new(RefCell::new(Key::new({ 1 }))); - let b: Value = Rc::new(RefCell::new(Key::new({ 2 }))); - let c: Value = Rc::new(RefCell::new(Key::new({ 3 }))); + let a: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + KeyImpl::construct(&__v.as_pointer(), { 1 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); + let b: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + KeyImpl::construct(&__v.as_pointer(), { 2 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); + let c: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + KeyImpl::construct(&__v.as_pointer(), { 3 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); match (a.as_pointer(),) { (__tmp_0,) => ({ (*m.borrow_mut()).subscript(__tmp_0, || 0_i32) }), } @@ -184,7 +183,13 @@ fn main_0() -> i32 { .deref()) .0 .as_pointer()); - match (Rc::new(RefCell::new(Key::new({ 0 }))).as_pointer(),) { + match (Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + KeyImpl::construct(&__v.as_pointer(), { 0 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })) + .as_pointer(),) + { (__tmp_9,) => { ({ (*m.borrow_mut()).subscript_move(__tmp_9, |__k| cpp_move!(__k), || 0_i32) }) } @@ -280,9 +285,23 @@ fn main_0() -> i32 { return if ok { 0 } else { 1 }; } pub trait KeyImpl { + fn construct(&self, k: i32); + fn copy_from(&self, o: Ptr); + fn move_from(&self, o: Ptr); fn operator_lt(&self, o: Ptr) -> bool; } impl KeyImpl for Ptr { + fn construct(&self, mut k: i32) { + field!((*self), k).write(k); + } + fn copy_from(&self, o: Ptr) { + field!((*self), k).write(o.with(|__s| __s.k)); + (*copies_0.with(Value::clone).borrow_mut()).prefix_inc(); + } + fn move_from(&self, o: Ptr) { + field!((*self), k).write(o.with(|__s| __s.k)); + (*moves_1.with(Value::clone).borrow_mut()).prefix_inc(); + } fn operator_lt(&self, o: Ptr) -> bool { return ({ (*self).with(|__s| __s.k) } < { o.with(|__s| __s.k) }); } diff --git a/tests/unit/out/refcount/map_key_identity.rs b/tests/unit/out/refcount/map_key_identity.rs index 3db3dcb380..ed8ffcd313 100644 --- a/tests/unit/out/refcount/map_key_identity.rs +++ b/tests/unit/out/refcount/map_key_identity.rs @@ -9,7 +9,7 @@ use std::rc::{Rc, Weak}; thread_local!( pub static default_constructions_0: Value = Rc::new(RefCell::new(0)); ); -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct Key { #[offset(0)] @@ -33,22 +33,21 @@ pub fn operator_lt_1(a: Ptr, b: Ptr) -> bool { pub fn operator_eq_2(a: Ptr, b: Ptr) -> bool { return ({ a.with(|__s| __s.identity) } == { b.with(|__s| __s.identity) }); } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg)] #[byte_size(4)] pub struct Mapped { #[offset(0)] pub value: i32, } -impl Mapped { - pub fn new() -> Self { - let __this: Mapped = Self { value: 0 }; - (*default_constructions_0.with(Value::clone).borrow_mut()).prefix_inc(); - __this - } -} impl Default for Mapped { fn default() -> Self { - { Mapped::new() } + { + { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + MappedImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + } + } } } pub fn main() { @@ -429,6 +428,15 @@ fn main_0() -> i32 { }; return if ok { 0 } else { 1 }; } +pub trait MappedImpl { + fn construct(&self); +} +impl MappedImpl for Ptr { + fn construct(&self) { + field!((*self), value).write(0); + (*default_constructions_0.with(Value::clone).borrow_mut()).prefix_inc(); + } +} pub fn __cpp2rust_init_globals() { let _ = default_constructions_0.with(|_| ()); } diff --git a/tests/unit/out/refcount/map_try_emplace.rs b/tests/unit/out/refcount/map_try_emplace.rs index ce86173ca3..2d50a7996a 100644 --- a/tests/unit/out/refcount/map_try_emplace.rs +++ b/tests/unit/out/refcount/map_try_emplace.rs @@ -18,81 +18,53 @@ thread_local!( thread_local!( pub static moves_3: Value = Rc::new(RefCell::new(0)); ); -#[derive(Record, ByteRepr, VaArg, FnPtrArg, MoveCtor, CppFrom)] -#[cpp_from(i32, new_1, value)] +#[derive(Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, MoveCtor, CppFrom)] +#[cpp_from(i32, construct_1, value)] #[byte_size(4)] pub struct Item { #[offset(0)] pub value: i32, } -impl Item { - pub fn new_2() -> Self { - Self { value: 0 } - } - pub fn new_1(mut value: i32) -> Self { - let __this: Item = Self { value: value }; - (*made_1.with(Value::clone).borrow_mut()).prefix_inc(); - __this - } - pub fn copy_from(other: Ptr) -> Self { - let __this: Item = Self { - value: other.with(|__s| __s.value), - }; - (*copies_2.with(Value::clone).borrow_mut()).prefix_inc(); - __this - } - pub fn move_from(other: Ptr) -> Self { - let __this: Item = Self { - value: other.with(|__s| __s.value), - }; - (*moves_3.with(Value::clone).borrow_mut()).prefix_inc(); - field!(other, value).write(-1_i32); - __this - } -} impl Clone for Item { fn clone(&self) -> Self { let __src: Value = Rc::new(RefCell::new(Item { value: self.value.clone(), })); - Item::copy_from(__src.as_pointer()) + let __v: Value = Rc::new(RefCell::new(::zero_init())); + ItemImpl::copy_from(&__v.as_pointer(), __src.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() } } impl Default for Item { fn default() -> Self { - { Item::new_2() } + { + { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + ItemImpl::construct_2(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + } + } } } pub fn argument_4(item: Ptr) -> Ptr { (*calls_0.with(Value::clone).borrow_mut()).prefix_inc(); return (item).clone(); } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg)] #[byte_size(4)] pub struct RefItem { #[offset(0)] pub value: i32, } -impl RefItem { - pub fn new_1() -> Self { - Self { value: 0 } - } - pub fn new_2(counter: Ptr, mut amount: i32) -> Self { - let __this: RefItem = Self { - value: { - { - counter.with_mut(|__v| *__v = *__v + amount) - }; - (counter.read()) - }, - }; - (*made_1.with(Value::clone).borrow_mut()).prefix_inc(); - __this - } -} impl Default for RefItem { fn default() -> Self { - { RefItem::new_1() } + { + { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + RefItemImpl::construct_1(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + } + } } } pub fn reference_5(counter: Ptr) -> Ptr { @@ -357,7 +329,11 @@ fn main_0() -> i32 { let (__it, __inserted) = Map::emplace(items.as_pointer(), &key.as_pointer(), || { ( Rc::new(RefCell::new(key.as_pointer().read())), - Rc::new(RefCell::new(Item::new_1({ 10 }))), + Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + ItemImpl::construct_1(&__v.as_pointer(), { 10 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })), ) }); ( @@ -365,7 +341,11 @@ fn main_0() -> i32 { Rc::new(RefCell::new(__inserted)), ) }; - let source: Value = Rc::new(RefCell::new(Item::new_1({ 20 }))); + let source: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + ItemImpl::construct_1(&__v.as_pointer(), { 20 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); made_1.with(|rc| { *rc.borrow_mut() = { copies_2.with(|rc| { @@ -387,7 +367,11 @@ fn main_0() -> i32 { Map::emplace(items.as_pointer(), &key.as_pointer(), || { ( Rc::new(RefCell::new(key.as_pointer().read())), - Rc::new(RefCell::new(Item::copy_from({ (__tmp_6).clone() }))), + Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + ItemImpl::copy_from(&__v.as_pointer(), { (__tmp_6).clone() }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })), ) }); ( @@ -422,7 +406,11 @@ fn main_0() -> i32 { Map::emplace(items.as_pointer(), &key.as_pointer(), || { ( Rc::new(RefCell::new(key.as_pointer().read())), - Rc::new(RefCell::new(Item::move_from({ (__tmp_7).clone() }))), + Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + ItemImpl::move_from(&__v.as_pointer(), { (__tmp_7).clone() }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })), ) }); ( @@ -456,7 +444,11 @@ fn main_0() -> i32 { Map::emplace(items.as_pointer(), &key.as_pointer(), || { ( Rc::new(RefCell::new(key.as_pointer().read())), - Rc::new(RefCell::new(Item::copy_from({ (__tmp_8).clone() }))), + Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + ItemImpl::copy_from(&__v.as_pointer(), { (__tmp_8).clone() }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })), ) }); ( @@ -492,7 +484,11 @@ fn main_0() -> i32 { Map::emplace(items.as_pointer(), &key.as_pointer(), || { ( Rc::new(RefCell::new(key.as_pointer().read())), - Rc::new(RefCell::new(Item::move_from({ (__tmp_9).clone() }))), + Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + ItemImpl::move_from(&__v.as_pointer(), { (__tmp_9).clone() }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })), ) }); ( @@ -525,9 +521,11 @@ fn main_0() -> i32 { let (__it, __inserted) = Map::emplace(refs.as_pointer(), &key.as_pointer(), || { ( Rc::new(RefCell::new(key.as_pointer().read())), - Rc::new(RefCell::new(RefItem::new_2({ counter.as_pointer() }, { - 5 - }))), + Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + RefItemImpl::construct_2(&__v.as_pointer(), { counter.as_pointer() }, { 5 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })), ) }); ( @@ -545,7 +543,13 @@ fn main_0() -> i32 { let (__it, __inserted) = Map::emplace(refs.as_pointer(), &key.as_pointer(), || { ( Rc::new(RefCell::new(key.as_pointer().read())), - Rc::new(RefCell::new(RefItem::new_2({ (__tmp_10).clone() }, { 2 }))), + Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + RefItemImpl::construct_2(&__v.as_pointer(), { (__tmp_10).clone() }, { + 2 + }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })), ) }); ( @@ -572,7 +576,13 @@ fn main_0() -> i32 { let (__it, __inserted) = Map::emplace(refs.as_pointer(), &key.as_pointer(), || { ( Rc::new(RefCell::new(key.as_pointer().read())), - Rc::new(RefCell::new(RefItem::new_2({ (__tmp_11).clone() }, { 2 }))), + Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + RefItemImpl::construct_2(&__v.as_pointer(), { (__tmp_11).clone() }, { + 2 + }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })), ) }); ( @@ -617,7 +627,11 @@ fn main_0() -> i32 { )> = Rc::new(RefCell::new({ let __tmp_13: Value; match ({ - __tmp_13 = Rc::new(RefCell::new(Item::new_1({ 30 }))); + __tmp_13 = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + ItemImpl::construct_1(&__v.as_pointer(), { 30 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); __tmp_13.as_pointer() },) { @@ -626,7 +640,11 @@ fn main_0() -> i32 { Map::emplace(items.as_pointer(), &key.as_pointer(), || { ( Rc::new(RefCell::new(key.as_pointer().read())), - Rc::new(RefCell::new(Item::move_from({ (__tmp_12).clone() }))), + Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + ItemImpl::move_from(&__v.as_pointer(), { (__tmp_12).clone() }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })), ) }); ( @@ -653,7 +671,11 @@ fn main_0() -> i32 { )> = Rc::new(RefCell::new({ let __tmp_15: Value; match ({ - __tmp_15 = Rc::new(RefCell::new(Item::new_1({ 40 }))); + __tmp_15 = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + ItemImpl::construct_1(&__v.as_pointer(), { 40 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); __tmp_15.as_pointer() },) { @@ -662,7 +684,11 @@ fn main_0() -> i32 { Map::emplace(items.as_pointer(), &key.as_pointer(), || { ( Rc::new(RefCell::new(key.as_pointer().read())), - Rc::new(RefCell::new(Item::move_from({ (__tmp_14).clone() }))), + Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + ItemImpl::move_from(&__v.as_pointer(), { (__tmp_14).clone() }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })), ) }); ( @@ -697,6 +723,48 @@ fn main_0() -> i32 { ); return if ok { 0 } else { 1 }; } +pub trait ItemImpl { + fn construct_2(&self); + fn construct_1(&self, value: i32); + fn copy_from(&self, other: Ptr); + fn move_from(&self, other: Ptr); +} +impl ItemImpl for Ptr { + fn construct_2(&self) { + field!((*self), value).write(0); + } + fn construct_1(&self, mut value: i32) { + field!((*self), value).write(value); + (*made_1.with(Value::clone).borrow_mut()).prefix_inc(); + } + fn copy_from(&self, other: Ptr) { + field!((*self), value).write(other.with(|__s| __s.value)); + (*copies_2.with(Value::clone).borrow_mut()).prefix_inc(); + } + fn move_from(&self, other: Ptr) { + field!((*self), value).write(other.with(|__s| __s.value)); + (*moves_3.with(Value::clone).borrow_mut()).prefix_inc(); + field!(other, value).write(-1_i32); + } +} +pub trait RefItemImpl { + fn construct_1(&self); + fn construct_2(&self, counter: Ptr, amount: i32); +} +impl RefItemImpl for Ptr { + fn construct_1(&self) { + field!((*self), value).write(0); + } + fn construct_2(&self, counter: Ptr, mut amount: i32) { + field!((*self), value).write({ + { + counter.with_mut(|__v| *__v = *__v + amount) + }; + (counter.read()) + }); + (*made_1.with(Value::clone).borrow_mut()).prefix_inc(); + } +} pub fn __cpp2rust_init_globals() { let _ = calls_0.with(|_| ()); let _ = made_1.with(|_| ()); diff --git a/tests/unit/out/refcount/map_try_emplace_lvalues.rs b/tests/unit/out/refcount/map_try_emplace_lvalues.rs index 400f0bff33..ef25415bda 100644 --- a/tests/unit/out/refcount/map_try_emplace_lvalues.rs +++ b/tests/unit/out/refcount/map_try_emplace_lvalues.rs @@ -12,32 +12,21 @@ thread_local!( thread_local!( pub static visits_1: Value = Rc::new(RefCell::new(0)); ); -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg)] #[byte_size(4)] pub struct Item { #[offset(0)] pub value: i32, } -impl Item { - pub fn new_1() -> Self { - Self { value: 0 } - } - pub fn new_2(counter: Ptr, mut amount: i32) -> Self { - let __this: Item = Self { - value: { - { - counter.with_mut(|__v| *__v = *__v + amount) - }; - (counter.read()) - }, - }; - (*made_0.with(Value::clone).borrow_mut()).prefix_inc(); - __this - } -} impl Default for Item { fn default() -> Self { - { Item::new_1() } + { + { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + ItemImpl::construct_1(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + } + } } } pub fn receiver_2( @@ -53,31 +42,28 @@ pub fn place_3(n: Ptr) -> Ptr { thread_local!( pub static temporary_calls_4: Value = Rc::new(RefCell::new(0)); ); -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg)] #[byte_size(12)] pub struct Temporary { #[offset(0)] pub n: i32, #[offset(4)] #[byte_size(8)] + #[array_len(2)] pub data: Box<[i32]>, } -impl Temporary { - pub fn new() -> Self { - let __this: Temporary = Self { - n: 5, - data: Box::new([6, 7]), - }; - (*temporary_calls_4.with(Value::clone).borrow_mut()).prefix_inc(); - __this - } -} impl Default for Temporary { fn default() -> Self { - { Temporary::new() } + { + { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + TemporaryImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + } + } } } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(12)] pub struct Nested { #[offset(0)] @@ -85,36 +71,34 @@ pub struct Nested { pub inner: Temporary, } pub fn make_temporary_5() -> Temporary { - return Temporary::new(); + return { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + TemporaryImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }; } pub fn make_nested_6() -> Nested { return ::default(); } -#[derive(Record, ByteRepr, FnPtrArg, MoveCtor, MoveAssign)] +#[derive(Record, ByteRepr, ZeroInitialization, FnPtrArg, MoveCtor, MoveAssign)] #[byte_size(8)] pub struct Owner { #[offset(0)] #[byte_size(8)] pub ptr: Option>, } -impl Owner { - pub fn new() -> Self { - Self { - ptr: Some(Rc::new(RefCell::new(Temporary::new()))), - } - } - pub fn move_from(_a0: Ptr) -> Self { - Self { - ptr: field!(_a0, ptr).with_mut(|__v: &mut Option>| __v.take()), - } - } -} impl Default for Owner { fn default() -> Self { - { Owner::new() } + { + { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + OwnerImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + } + } } } -#[derive(Record, ByteRepr, FnPtrArg)] +#[derive(Record, ByteRepr, ZeroInitialization, FnPtrArg)] #[byte_size(4)] pub struct Noncopy { #[offset(0)] @@ -125,30 +109,23 @@ impl Default for Noncopy { { Noncopy { n: 5 } } } } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, CppFrom)] -#[cpp_from(i32, new_1, ptr)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, CppFrom)] +#[cpp_from(i32, construct_1, ptr)] #[byte_size(8)] pub struct Held { #[offset(0)] #[byte_size(8)] pub ptr: Ptr, } -impl Held { - pub fn new_2() -> Self { - Self { - ptr: Ptr::::null(), - } - } - pub fn new_1(n: Ptr) -> Self { - Self { ptr: (n).clone() } - } - pub fn new_3(n: Ptr, mut _a1: i32) -> Self { - Self { ptr: (n).clone() } - } -} impl Default for Held { fn default() -> Self { - { Held::new_2() } + { + { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + HeldImpl::construct_2(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + } + } } } thread_local!( @@ -257,7 +234,11 @@ thread_local!( let mut __tmp_7: Option>; match (field_ptr!( (*{ - __tmp_7 = Some(Rc::new(RefCell::new(Temporary::new()))); + __tmp_7 = Some(Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + TemporaryImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }))); &mut __tmp_7 }) .as_pointer(), @@ -346,7 +327,11 @@ fn main_0() -> i32 { let (__it, __inserted) = Map::emplace(m.as_pointer(), &key.as_pointer(), || { ( Rc::new(RefCell::new(key.as_pointer().read())), - Rc::new(RefCell::new(Item::new_2({ counter.as_pointer() }, { 3 }))), + Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + ItemImpl::construct_2(&__v.as_pointer(), { counter.as_pointer() }, { 3 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })), ) }); ( @@ -370,7 +355,11 @@ fn main_0() -> i32 { let (__it, __inserted) = Map::emplace((__tmp_8).clone(), &key.as_pointer(), || { ( Rc::new(RefCell::new(key.as_pointer().read())), - Rc::new(RefCell::new(Item::new_2({ (__tmp_9).clone() }, { 2 }))), + Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + ItemImpl::construct_2(&__v.as_pointer(), { (__tmp_9).clone() }, { 2 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })), ) }); ( @@ -404,7 +393,11 @@ fn main_0() -> i32 { let (__it, __inserted) = Map::emplace(m.as_pointer(), &key.as_pointer(), || { ( Rc::new(RefCell::new(key.as_pointer().read())), - Rc::new(RefCell::new(Item::new_2({ (__tmp_11).clone() }, { 2 }))), + Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + ItemImpl::construct_2(&__v.as_pointer(), { (__tmp_11).clone() }, { 2 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })), ) }); ( @@ -435,7 +428,11 @@ fn main_0() -> i32 { let (__it, __inserted) = Map::emplace(m.as_pointer(), &key.as_pointer(), || { ( Rc::new(RefCell::new(key.as_pointer().read())), - Rc::new(RefCell::new(Item::new_2({ (__tmp_13).clone() }, { 2 }))), + Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + ItemImpl::construct_2(&__v.as_pointer(), { (__tmp_13).clone() }, { 2 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })), ) }); ( @@ -467,7 +464,11 @@ fn main_0() -> i32 { let (__it, __inserted) = Map::emplace(m.as_pointer(), &key.as_pointer(), || { ( Rc::new(RefCell::new(key.as_pointer().read())), - Rc::new(RefCell::new(Item::new_2({ (__tmp_16).clone() }, { 2 }))), + Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + ItemImpl::construct_2(&__v.as_pointer(), { (__tmp_16).clone() }, { 2 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })), ) }); ( @@ -508,7 +509,11 @@ fn main_0() -> i32 { let (__it, __inserted) = Map::emplace(m.as_pointer(), &key.as_pointer(), || { ( Rc::new(RefCell::new(key.as_pointer().read())), - Rc::new(RefCell::new(Item::new_2({ (__tmp_19).clone() }, { 3 }))), + Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + ItemImpl::construct_2(&__v.as_pointer(), { (__tmp_19).clone() }, { 3 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })), ) }); ( @@ -535,7 +540,11 @@ fn main_0() -> i32 { let (__it, __inserted) = Map::emplace(m.as_pointer(), &key.as_pointer(), || { ( Rc::new(RefCell::new(key.as_pointer().read())), - Rc::new(RefCell::new(Item::new_2({ (__tmp_21).clone() }, { 3 }))), + Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + ItemImpl::construct_2(&__v.as_pointer(), { (__tmp_21).clone() }, { 3 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })), ) }); ( @@ -562,7 +571,11 @@ fn main_0() -> i32 { let (__it, __inserted) = Map::emplace(m.as_pointer(), &key.as_pointer(), || { ( Rc::new(RefCell::new(key.as_pointer().read())), - Rc::new(RefCell::new(Item::new_2({ (__tmp_24).clone() }, { 3 }))), + Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + ItemImpl::construct_2(&__v.as_pointer(), { (__tmp_24).clone() }, { 3 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })), ) }); ( @@ -597,7 +610,11 @@ fn main_0() -> i32 { let (__it, __inserted) = Map::emplace(m.as_pointer(), &key.as_pointer(), || { ( Rc::new(RefCell::new(key.as_pointer().read())), - Rc::new(RefCell::new(Item::new_2({ (__tmp_27).clone() }, { 3 }))), + Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + ItemImpl::construct_2(&__v.as_pointer(), { (__tmp_27).clone() }, { 3 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })), ) }); ( @@ -634,7 +651,11 @@ fn main_0() -> i32 { let (__it, __inserted) = Map::emplace(m.as_pointer(), &key.as_pointer(), || { ( Rc::new(RefCell::new(key.as_pointer().read())), - Rc::new(RefCell::new(Item::new_2({ (__tmp_30).clone() }, { 3 }))), + Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + ItemImpl::construct_2(&__v.as_pointer(), { (__tmp_30).clone() }, { 3 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })), ) }); ( @@ -657,7 +678,11 @@ fn main_0() -> i32 { let (__it, __inserted) = Map::emplace(m.as_pointer(), &key.as_pointer(), || { ( Rc::new(RefCell::new(key.as_pointer().read())), - Rc::new(RefCell::new(Item::new_2({ (__tmp_33).clone() }, { 3 }))), + Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + ItemImpl::construct_2(&__v.as_pointer(), { (__tmp_33).clone() }, { 3 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })), ) }); ( @@ -1156,10 +1181,20 @@ fn main_0() -> i32 { Rc::new(RefCell::new( keys.decay().offset(0).read(), )), - Rc::new(RefCell::new(Item::new_2( - { (__tmp_61).clone() }, - { 2 }, - ))), + Rc::new(RefCell::new({ + let __v: Value = Rc::new( + RefCell::new(::zero_init()), + ); + ItemImpl::construct_2( + &__v.as_pointer(), + { (__tmp_61).clone() }, + { 2 }, + ); + Rc::try_unwrap(__v) + .ok() + .unwrap() + .into_inner() + })), ) }, ); @@ -1199,10 +1234,20 @@ fn main_0() -> i32 { Rc::new(RefCell::new( keys.decay().offset(1).read(), )), - Rc::new(RefCell::new(Item::new_2( - { (__tmp_64).clone() }, - { 2 }, - ))), + Rc::new(RefCell::new({ + let __v: Value = Rc::new( + RefCell::new(::zero_init()), + ); + ItemImpl::construct_2( + &__v.as_pointer(), + { (__tmp_64).clone() }, + { 2 }, + ); + Rc::try_unwrap(__v) + .ok() + .unwrap() + .into_inner() + })), ) }, ); @@ -1242,10 +1287,20 @@ fn main_0() -> i32 { Rc::new(RefCell::new( keys.decay().offset(2).read(), )), - Rc::new(RefCell::new(Item::new_2( - { (__tmp_69).clone() }, - { 2 }, - ))), + Rc::new(RefCell::new({ + let __v: Value = Rc::new( + RefCell::new(::zero_init()), + ); + ItemImpl::construct_2( + &__v.as_pointer(), + { (__tmp_69).clone() }, + { 2 }, + ); + Rc::try_unwrap(__v) + .ok() + .unwrap() + .into_inner() + })), ) }, ); @@ -1489,9 +1544,14 @@ fn main_0() -> i32 { || { ( Rc::new(RefCell::new(key.as_pointer().read())), - Rc::new(RefCell::new(Held::new_1({ - ordinary.as_pointer() - }))), + Rc::new(RefCell::new({ + let __v: Value = + Rc::new(RefCell::new(::zero_init())); + HeldImpl::construct_1(&__v.as_pointer(), { + ordinary.as_pointer() + }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })), ) }, ); @@ -1535,9 +1595,19 @@ fn main_0() -> i32 { Rc::new(RefCell::new( key.as_pointer().read(), )), - Rc::new(RefCell::new(Held::new_1({ - (__tmp_83).clone() - }))), + Rc::new(RefCell::new({ + let __v: Value = Rc::new( + RefCell::new(::zero_init()), + ); + HeldImpl::construct_1( + &__v.as_pointer(), + { (__tmp_83).clone() }, + ); + Rc::try_unwrap(__v) + .ok() + .unwrap() + .into_inner() + })), ) }, ); @@ -1579,10 +1649,16 @@ fn main_0() -> i32 { || { ( Rc::new(RefCell::new(key.as_pointer().read())), - Rc::new(RefCell::new(Held::new_3( - { ordinary_array.decay() }, - { 0 }, - ))), + Rc::new(RefCell::new({ + let __v: Value = + Rc::new(RefCell::new(::zero_init())); + HeldImpl::construct_3( + &__v.as_pointer(), + { ordinary_array.decay() }, + { 0 }, + ); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })), ) }, ); @@ -1625,10 +1701,20 @@ fn main_0() -> i32 { Rc::new(RefCell::new( key.as_pointer().read(), )), - Rc::new(RefCell::new(Held::new_3( - { (__tmp_88).clone() }, - { 0 }, - ))), + Rc::new(RefCell::new({ + let __v: Value = Rc::new( + RefCell::new(::zero_init()), + ); + HeldImpl::construct_3( + &__v.as_pointer(), + { (__tmp_88).clone() }, + { 0 }, + ); + Rc::try_unwrap(__v) + .ok() + .unwrap() + .into_inner() + })), ) }, ); @@ -1685,7 +1771,12 @@ fn main_0() -> i32 { let mut __tmp_94: Option>; match (field_ptr!( (*{ - __tmp_94 = Some(Rc::new(RefCell::new(Temporary::new()))); + __tmp_94 = Some(Rc::new(RefCell::new({ + let __v: Value = + Rc::new(RefCell::new(::zero_init())); + TemporaryImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }))); &mut __tmp_94 }) .as_pointer(), @@ -1746,7 +1837,12 @@ fn main_0() -> i32 { let mut __tmp_98: Option>; match (array_field_ptr!( (*{ - __tmp_98 = Some(Rc::new(RefCell::new(Temporary::new()))); + __tmp_98 = Some(Rc::new(RefCell::new({ + let __v: Value = + Rc::new(RefCell::new(::zero_init())); + TemporaryImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }))); &mut __tmp_98 }) .as_pointer(), @@ -1776,7 +1872,12 @@ fn main_0() -> i32 { let mut __tmp_100: Option>; match (field_ptr!( (*{ - __tmp_100 = Some(Rc::new(RefCell::new(Temporary::new()))); + __tmp_100 = Some(Rc::new(RefCell::new({ + let __v: Value = + Rc::new(RefCell::new(::zero_init())); + TemporaryImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }))); &mut __tmp_100 }) .as_pointer(), @@ -1805,7 +1906,12 @@ fn main_0() -> i32 { let mut __tmp_102: Option>; match (field_ptr!( ((*{ - __tmp_102 = Some(Rc::new(RefCell::new(Temporary::new()))); + __tmp_102 = Some(Rc::new(RefCell::new({ + let __v: Value = + Rc::new(RefCell::new(::zero_init())); + TemporaryImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }))); &mut __tmp_102 }) .as_pointer()), @@ -1834,9 +1940,12 @@ fn main_0() -> i32 { let mut __tmp_104: Option>>>; match (field_ptr!( ((*(*{ - __tmp_104 = Some(Rc::new(RefCell::new(Some(Rc::new(RefCell::new( - Temporary::new(), - )))))); + __tmp_104 = Some(Rc::new(RefCell::new(Some(Rc::new(RefCell::new({ + let __v: Value = + Rc::new(RefCell::new(::zero_init())); + TemporaryImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })))))); &mut __tmp_104 }) .as_pointer() @@ -1903,9 +2012,14 @@ fn main_0() -> i32 { || { ( Rc::new(RefCell::new(key.as_pointer().read())), - Rc::new(RefCell::new(Held::new_1({ - ordinary.as_pointer() - }))), + Rc::new(RefCell::new({ + let __v: Value = + Rc::new(RefCell::new(::zero_init())); + HeldImpl::construct_1(&__v.as_pointer(), { + ordinary.as_pointer() + }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })), ) }, ); @@ -1925,8 +2039,13 @@ fn main_0() -> i32 { match (if (choose != 0) { field_ptr!( (*{ - __tmp_106 = - Some(Rc::new(RefCell::new(Temporary::new()))); + __tmp_106 = Some(Rc::new(RefCell::new({ + let __v: Value = Rc::new( + RefCell::new(::zero_init()), + ); + TemporaryImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }))); &mut __tmp_106 }) .as_pointer(), @@ -1935,8 +2054,13 @@ fn main_0() -> i32 { } else { array_field_ptr!( (*{ - __tmp_107 = - Some(Rc::new(RefCell::new(Temporary::new()))); + __tmp_107 = Some(Rc::new(RefCell::new({ + let __v: Value = Rc::new( + RefCell::new(::zero_init()), + ); + TemporaryImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }))); &mut __tmp_107 }) .as_pointer(), @@ -1954,9 +2078,19 @@ fn main_0() -> i32 { Rc::new(RefCell::new( key.as_pointer().read(), )), - Rc::new(RefCell::new(Held::new_1({ - (__tmp_105).clone() - }))), + Rc::new(RefCell::new({ + let __v: Value = Rc::new( + RefCell::new(::zero_init()), + ); + HeldImpl::construct_1( + &__v.as_pointer(), + { (__tmp_105).clone() }, + ); + Rc::try_unwrap(__v) + .ok() + .unwrap() + .into_inner() + })), ) }, ); @@ -1993,7 +2127,12 @@ fn main_0() -> i32 { { match (({ OwnerImpl::value(&{ - __tmp_109 = Rc::new(RefCell::new(Owner::new())); + __tmp_109 = Rc::new(RefCell::new({ + let __v: Value = + Rc::new(RefCell::new(::zero_init())); + OwnerImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); __tmp_109.as_pointer() }) }),) @@ -2007,9 +2146,19 @@ fn main_0() -> i32 { Rc::new(RefCell::new( key.as_pointer().read(), )), - Rc::new(RefCell::new(Held::new_1({ - (__tmp_108).clone() - }))), + Rc::new(RefCell::new({ + let __v: Value = Rc::new( + RefCell::new(::zero_init()), + ); + HeldImpl::construct_1( + &__v.as_pointer(), + { (__tmp_108).clone() }, + ); + Rc::try_unwrap(__v) + .ok() + .unwrap() + .into_inner() + })), ) }, ); @@ -2248,14 +2397,61 @@ fn main_0() -> i32 { ); return 0; } +pub trait HeldImpl { + fn construct_2(&self); + fn construct_1(&self, n: Ptr); + fn construct_3(&self, n: Ptr, _a1: i32); +} +impl HeldImpl for Ptr { + fn construct_2(&self) { + field!((*self), ptr).write(Ptr::::null()); + } + fn construct_1(&self, n: Ptr) { + field!((*self), ptr).write((n).clone()); + } + fn construct_3(&self, n: Ptr, mut _a1: i32) { + field!((*self), ptr).write((n).clone()); + } +} +pub trait ItemImpl { + fn construct_1(&self); + fn construct_2(&self, counter: Ptr, amount: i32); +} +impl ItemImpl for Ptr { + fn construct_1(&self) { + field!((*self), value).write(0); + } + fn construct_2(&self, counter: Ptr, mut amount: i32) { + field!((*self), value).write({ + { + counter.with_mut(|__v| *__v = *__v + amount) + }; + (counter.read()) + }); + (*made_0.with(Value::clone).borrow_mut()).prefix_inc(); + } +} pub trait OwnerImpl { + fn construct(&self); fn value(&self) -> Ptr; + fn move_from(&self, _a0: Ptr); fn move_assign(&self, _a0: Ptr) -> Ptr; } impl OwnerImpl for Ptr { + fn construct(&self) { + field!((*self), ptr).write(Some(Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + TemporaryImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })))); + } fn value(&self) -> Ptr { return field_ptr!((*self).with(|__s| __s.ptr.clone()).as_pointer(), n); } + fn move_from(&self, _a0: Ptr) { + field!((*self), ptr) + .write(field!(_a0, ptr).with_mut(|__v: &mut Option>| __v.take())); + } fn move_assign(&self, _a0: Ptr) -> Ptr { field!((*self), ptr).with_mut(|__b0| { (*__b0) = field!(_a0, ptr).with_mut(|__v: &mut Option>| __v.take()); @@ -2263,6 +2459,16 @@ impl OwnerImpl for Ptr { return (*self).clone(); } } +pub trait TemporaryImpl { + fn construct(&self); +} +impl TemporaryImpl for Ptr { + fn construct(&self) { + field!((*self), n).write(5); + field!((*self), data).write(Box::new([6, 7])); + (*temporary_calls_4.with(Value::clone).borrow_mut()).prefix_inc(); + } +} pub fn __cpp2rust_init_globals() { let _ = made_0.with(|_| ()); let _ = visits_1.with(|_| ()); diff --git a/tests/unit/out/refcount/max.rs b/tests/unit/out/refcount/max.rs index bc03b78545..0cd14d48ef 100644 --- a/tests/unit/out/refcount/max.rs +++ b/tests/unit/out/refcount/max.rs @@ -20,25 +20,13 @@ pub fn pick_3(mut i: i32) -> Ptr { (*calls_0.with(Value::clone).borrow_mut()).prefix_inc(); return xs_1.with(|v| v.decay()).offset(i); } -#[derive(Record, ByteRepr, VaArg, FnPtrArg, CppFrom, CopyAssign, Default)] -#[cpp_from(i32, new, value)] +#[derive(Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, CppFrom, CopyAssign, Default)] +#[cpp_from(i32, construct, value)] #[byte_size(4)] pub struct Bag { #[offset(0)] pub n: i32, } -impl Bag { - pub fn new(mut n: i32) -> Self { - Self { n: n } - } - pub fn copy_from(o: Ptr) -> Self { - let __this: Bag = Self { - n: o.with(|__s| __s.n), - }; - (*calls_0.with(Value::clone).borrow_mut()).prefix_inc(); - __this - } -} impl CppLess for Bag { fn less(__lhs: &Ptr, __rhs: &Ptr) -> bool { operator_lt_4(__lhs.clone(), __rhs.clone()) @@ -53,14 +41,20 @@ impl CppEq for Bag { impl Clone for Bag { fn clone(&self) -> Self { let __src: Value = Rc::new(RefCell::new(Bag { n: self.n.clone() })); - Bag::copy_from(__src.as_pointer()) + let __v: Value = Rc::new(RefCell::new(::zero_init())); + BagImpl::copy_from(&__v.as_pointer(), __src.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() } } pub fn operator_lt_4(a: Ptr, b: Ptr) -> bool { return ({ a.with(|__s| __s.n) } < { b.with(|__s| __s.n) }); } pub fn make_5(mut n: i32) -> Bag { - return Bag::new({ n }); + return { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + BagImpl::construct(&__v.as_pointer(), { n }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }; } pub fn main() { __cpp2rust_init_globals(); @@ -212,9 +206,18 @@ fn main_0() -> i32 { return 0; } pub trait BagImpl { + fn construct(&self, n: i32); + fn copy_from(&self, o: Ptr); fn copy_assign(&self, _a0: Ptr) -> Ptr; } impl BagImpl for Ptr { + fn construct(&self, mut n: i32) { + field!((*self), n).write(n); + } + fn copy_from(&self, o: Ptr) { + field!((*self), n).write(o.with(|__s| __s.n)); + (*calls_0.with(Value::clone).borrow_mut()).prefix_inc(); + } fn copy_assign(&self, _a0: Ptr) -> Ptr { field!((*self), n).write(_a0.with(|__s| __s.n)); return (*self).clone(); diff --git a/tests/unit/out/refcount/memcpy_struct_bytes.rs b/tests/unit/out/refcount/memcpy_struct_bytes.rs index 51e9dde709..af4970383b 100644 --- a/tests/unit/out/refcount/memcpy_struct_bytes.rs +++ b/tests/unit/out/refcount/memcpy_struct_bytes.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct point { #[offset(0)] diff --git a/tests/unit/out/refcount/memcpy_struct_layouts.rs b/tests/unit/out/refcount/memcpy_struct_layouts.rs index 26928cf9e3..a90e59a069 100644 --- a/tests/unit/out/refcount/memcpy_struct_layouts.rs +++ b/tests/unit/out/refcount/memcpy_struct_layouts.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct packed { #[offset(0)] @@ -18,7 +18,7 @@ pub struct packed { #[offset(6)] pub d: i16, } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(12)] pub struct reordered { #[offset(0)] @@ -28,7 +28,7 @@ pub struct reordered { #[offset(8)] pub c: i8, } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(16)] pub struct tail { #[offset(0)] @@ -36,7 +36,7 @@ pub struct tail { #[offset(8)] pub b: f64, } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(24)] pub struct nested { #[offset(0)] @@ -45,11 +45,12 @@ pub struct nested { #[offset(16)] pub c: i8, } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg)] #[byte_size(8)] pub struct array { #[offset(0)] #[byte_size(3)] + #[array_len(3)] pub name: Box<[i8]>, #[offset(4)] pub x: i32, diff --git a/tests/unit/out/refcount/memcpy_struct_struct.rs b/tests/unit/out/refcount/memcpy_struct_struct.rs index ae6af0f81f..3aa8c319d5 100644 --- a/tests/unit/out/refcount/memcpy_struct_struct.rs +++ b/tests/unit/out/refcount/memcpy_struct_struct.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct Entry { #[offset(0)] diff --git a/tests/unit/out/refcount/memset_struct_tail.rs b/tests/unit/out/refcount/memset_struct_tail.rs index b670037ca5..eca67abf50 100644 --- a/tests/unit/out/refcount/memset_struct_tail.rs +++ b/tests/unit/out/refcount/memset_struct_tail.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg)] #[byte_size(32)] pub struct S { #[offset(0)] @@ -17,6 +17,7 @@ pub struct S { pub b: i64, #[offset(16)] #[byte_size(5)] + #[array_len(5)] pub c: Box<[i8]>, #[offset(24)] pub last: i32, diff --git a/tests/unit/out/refcount/move_assign.rs b/tests/unit/out/refcount/move_assign.rs index 53c2cd9749..b98f1c1fab 100644 --- a/tests/unit/out/refcount/move_assign.rs +++ b/tests/unit/out/refcount/move_assign.rs @@ -6,58 +6,74 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Record, ByteRepr, FnPtrArg, MoveCtor, CppFrom, MoveAssign, Default)] -#[cpp_from(i32, new, value)] +#[derive( + Record, ByteRepr, ZeroInitialization, FnPtrArg, MoveCtor, CppFrom, MoveAssign, Default, +)] +#[cpp_from(i32, construct, value)] #[byte_size(4)] pub struct MoveOnly { #[offset(0)] pub v: i32, } -impl MoveOnly { - pub fn new(mut v: i32) -> Self { - Self { v: v } - } - pub fn move_from(o: Ptr) -> Self { - let __this: MoveOnly = Self { - v: o.with(|__s| __s.v), - }; - field!(o, v).write(0); - __this - } -} -#[derive(Record, ByteRepr, FnPtrArg)] +#[derive(Record, ByteRepr, ZeroInitialization, FnPtrArg)] #[byte_size(4)] pub struct ConstMoveAssign { #[offset(0)] pub mark: i32, } -impl ConstMoveAssign { - pub fn new() -> Self { - Self { mark: 0 } - } -} impl Default for ConstMoveAssign { fn default() -> Self { - { ConstMoveAssign::new() } + { + { + let __v: Value = + Rc::new(RefCell::new(::zero_init())); + ConstMoveAssignImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + } + } } } pub fn make_0(mut v: i32) -> MoveOnly { - let m: Value = Rc::new(RefCell::new(MoveOnly::new({ v }))); - return MoveOnly::move_from({ m.as_pointer() }); + let m: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + MoveOnlyImpl::construct(&__v.as_pointer(), { v }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); + return { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + MoveOnlyImpl::move_from(&__v.as_pointer(), { m.as_pointer() }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }; } pub fn main() { __cpp2rust_init_globals(); libcc2rs::cpp2rust_exit(main_0()); } fn main_0() -> i32 { - let a: Value = Rc::new(RefCell::new(MoveOnly::new({ 1 }))); - let b: Value = Rc::new(RefCell::new(MoveOnly::new({ 2 }))); - let c: Value = Rc::new(RefCell::new(MoveOnly::new({ 3 }))); + let a: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + MoveOnlyImpl::construct(&__v.as_pointer(), { 1 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); + let b: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + MoveOnlyImpl::construct(&__v.as_pointer(), { 2 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); + let c: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + MoveOnlyImpl::construct(&__v.as_pointer(), { 3 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); ({ MoveOnlyImpl::move_assign(&a.as_pointer(), b.as_pointer()) }); assert!(({ (*a.borrow()).v } == 2)); assert!(({ (*b.borrow()).v } == 0)); ({ - let _o: Value = Rc::new(RefCell::new(MoveOnly::new({ 3 }))); + let _o: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + MoveOnlyImpl::construct(&__v.as_pointer(), { 3 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); MoveOnlyImpl::move_assign(&b.as_pointer(), _o.as_pointer()) }); ({ @@ -70,7 +86,11 @@ fn main_0() -> i32 { (({ (*b.borrow()).v } == 0) && ({ (*a.borrow()).v } == 0)) && ({ (*c.borrow()).v } == 3) ); ({ - let _o: Value = Rc::new(RefCell::new(MoveOnly::new({ 5 }))); + let _o: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + MoveOnlyImpl::construct(&__v.as_pointer(), { 5 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); MoveOnlyImpl::move_assign(&a.as_pointer(), _o.as_pointer()) }); assert!(({ (*a.borrow()).v } == 5)); @@ -83,17 +103,41 @@ fn main_0() -> i32 { assert!(({ (*a.borrow()).v } == 6)); let vec_: Value> = Rc::new(RefCell::new(Vec::new())); { - let __a1 = MoveOnly::new({ 7 }); + let __a1 = { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + MoveOnlyImpl::construct(&__v.as_pointer(), { 7 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }; (*vec_.borrow_mut()).push(__a1) }; - let d: Value = Rc::new(RefCell::new(MoveOnly::new({ 8 }))); + let d: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + MoveOnlyImpl::construct(&__v.as_pointer(), { 8 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); ({ MoveOnlyImpl::move_assign(&vec_.decay().offset(0_usize), d.as_pointer()) }); assert!(({ (*vec_.borrow())[0_usize].v } == 8)); assert!(({ (*d.borrow()).v } == 0)); - let m: Value = Rc::new(RefCell::new(ConstMoveAssign::new())); - let m1: Value = Rc::new(RefCell::new(ConstMoveAssign::new())); - let m2: Value = Rc::new(RefCell::new(ConstMoveAssign::new())); - let cm: Value = Rc::new(RefCell::new(ConstMoveAssign::new())); + let m: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + ConstMoveAssignImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); + let m1: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + ConstMoveAssignImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); + let m2: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + ConstMoveAssignImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); + let cm: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + ConstMoveAssignImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); ({ ConstMoveAssignImpl::operator_assign_2(&m1.as_pointer(), m.as_pointer()) }); ({ ConstMoveAssignImpl::operator_assign_3(&m2.as_pointer(), cm.as_pointer()) }); assert!(({ (*m1.borrow()).mark } == 1)); @@ -101,10 +145,14 @@ fn main_0() -> i32 { return 0; } pub trait ConstMoveAssignImpl { + fn construct(&self); fn operator_assign_2(&self, o: Ptr) -> Ptr; fn operator_assign_3(&self, o: Ptr) -> Ptr; } impl ConstMoveAssignImpl for Ptr { + fn construct(&self) { + field!((*self), mark).write(0); + } fn operator_assign_2(&self, o: Ptr) -> Ptr { field!((*self), mark).write((o.with(|__s| __s.mark) + 1)); return (*self).clone(); @@ -115,9 +163,18 @@ impl ConstMoveAssignImpl for Ptr { } } pub trait MoveOnlyImpl { + fn construct(&self, v: i32); + fn move_from(&self, o: Ptr); fn move_assign(&self, o: Ptr) -> Ptr; } impl MoveOnlyImpl for Ptr { + fn construct(&self, mut v: i32) { + field!((*self), v).write(v); + } + fn move_from(&self, o: Ptr) { + field!((*self), v).write(o.with(|__s| __s.v)); + field!(o, v).write(0); + } fn move_assign(&self, o: Ptr) -> Ptr { if ((*self) == (o)) { return (*self).clone(); diff --git a/tests/unit/out/refcount/move_ctor.rs b/tests/unit/out/refcount/move_ctor.rs index 23d3ae6cb9..e93ce86953 100644 --- a/tests/unit/out/refcount/move_ctor.rs +++ b/tests/unit/out/refcount/move_ctor.rs @@ -6,53 +6,32 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Record, ByteRepr, FnPtrArg, MoveCtor, CppFrom, Default)] -#[cpp_from(i32, new, value)] +#[derive(Record, ByteRepr, ZeroInitialization, FnPtrArg, MoveCtor, CppFrom, Default)] +#[cpp_from(i32, construct, value)] #[byte_size(4)] pub struct MoveOnly { #[offset(0)] pub v: i32, } -impl MoveOnly { - pub fn new(mut v: i32) -> Self { - Self { v: v } - } - pub fn move_from(o: Ptr) -> Self { - let __this: MoveOnly = Self { - v: o.with(|__s| __s.v), - }; - field!(o, v).write(0); - __this - } -} -#[derive(Record, ByteRepr, FnPtrArg)] +#[derive(Record, ByteRepr, ZeroInitialization, FnPtrArg)] #[byte_size(4)] pub struct ConstMove { #[offset(0)] pub mark: i32, } -impl ConstMove { - pub fn new() -> Self { - Self { mark: 0 } - } - pub fn new_1(o: Ptr) -> Self { - Self { - mark: (o.with(|__s| __s.mark) + 1), - } - } - pub fn new_2(o: Ptr) -> Self { - Self { - mark: (o.with(|__s| __s.mark) + 10), - } - } -} impl Default for ConstMove { fn default() -> Self { - { ConstMove::new() } + { + { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + ConstMoveImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + } + } } } -#[derive(Record, ByteRepr, VaArg, FnPtrArg, MoveCtor, CppFrom, Default)] -#[cpp_from(i32, new, value)] +#[derive(Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, MoveCtor, CppFrom, Default)] +#[cpp_from(i32, construct, value)] #[byte_size(12)] pub struct ThrowingMove { #[offset(0)] @@ -62,31 +41,6 @@ pub struct ThrowingMove { #[offset(8)] pub moves: i32, } -impl ThrowingMove { - pub fn new(mut v: i32) -> Self { - Self { - v: v, - copies: 0, - moves: 0, - } - } - pub fn copy_from(o: Ptr) -> Self { - Self { - v: o.with(|__s| __s.v), - copies: (o.with(|__s| __s.copies) + 1), - moves: o.with(|__s| __s.moves), - } - } - pub fn move_from(o: Ptr) -> Self { - let __this: ThrowingMove = Self { - v: o.with(|__s| __s.v), - copies: o.with(|__s| __s.copies), - moves: (o.with(|__s| __s.moves) + 1), - }; - field!(o, v).write(0); - __this - } -} impl Clone for ThrowingMove { fn clone(&self) -> Self { let __src: Value = Rc::new(RefCell::new(ThrowingMove { @@ -94,11 +48,13 @@ impl Clone for ThrowingMove { copies: self.copies.clone(), moves: self.moves.clone(), })); - ThrowingMove::copy_from(__src.as_pointer()) + let __v: Value = Rc::new(RefCell::new(::zero_init())); + ThrowingMoveImpl::copy_from(&__v.as_pointer(), __src.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() } } -#[derive(Record, ByteRepr, VaArg, FnPtrArg, MoveCtor, CppFrom, Default)] -#[cpp_from(i32, new, value)] +#[derive(Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, MoveCtor, CppFrom, Default)] +#[cpp_from(i32, construct, value)] #[byte_size(12)] pub struct NoexceptMove { #[offset(0)] @@ -108,31 +64,6 @@ pub struct NoexceptMove { #[offset(8)] pub moves: i32, } -impl NoexceptMove { - pub fn new(mut v: i32) -> Self { - Self { - v: v, - copies: 0, - moves: 0, - } - } - pub fn copy_from(o: Ptr) -> Self { - Self { - v: o.with(|__s| __s.v), - copies: (o.with(|__s| __s.copies) + 1), - moves: o.with(|__s| __s.moves), - } - } - pub fn move_from(o: Ptr) -> Self { - let __this: NoexceptMove = Self { - v: o.with(|__s| __s.v), - copies: o.with(|__s| __s.copies), - moves: (o.with(|__s| __s.moves) + 1), - }; - field!(o, v).write(0); - __this - } -} impl Clone for NoexceptMove { fn clone(&self) -> Self { let __src: Value = Rc::new(RefCell::new(NoexceptMove { @@ -140,70 +71,239 @@ impl Clone for NoexceptMove { copies: self.copies.clone(), moves: self.moves.clone(), })); - NoexceptMove::copy_from(__src.as_pointer()) + let __v: Value = Rc::new(RefCell::new(::zero_init())); + NoexceptMoveImpl::copy_from(&__v.as_pointer(), __src.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() } } pub fn by_value_0(mut m: MoveOnly) -> i32 { return m.v; } pub fn make_1(mut v: i32) -> MoveOnly { - let m: Value = Rc::new(RefCell::new(MoveOnly::new({ v }))); - return MoveOnly::move_from({ m.as_pointer() }); + let m: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + MoveOnlyImpl::construct(&__v.as_pointer(), { v }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); + return { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + MoveOnlyImpl::move_from(&__v.as_pointer(), { m.as_pointer() }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }; } pub fn main() { __cpp2rust_init_globals(); libcc2rs::cpp2rust_exit(main_0()); } fn main_0() -> i32 { - let a: Value = Rc::new(RefCell::new(MoveOnly::new({ 1 }))); - let b: Value = Rc::new(RefCell::new(MoveOnly::move_from({ a.as_pointer() }))); + let a: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + MoveOnlyImpl::construct(&__v.as_pointer(), { 1 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); + let b: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + MoveOnlyImpl::move_from(&__v.as_pointer(), { a.as_pointer() }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); assert!(({ (*b.borrow()).v } == 1)); assert!(({ (*a.borrow()).v } == 0)); - let c: Value = Rc::new(RefCell::new(MoveOnly::move_from({ b.as_pointer() }))); + let c: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + MoveOnlyImpl::move_from(&__v.as_pointer(), { b.as_pointer() }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); assert!(({ (*c.borrow()).v } == 1)); assert!(({ (*b.borrow()).v } == 0)); - let mut d: MoveOnly = MoveOnly::move_from({ c.as_pointer() }); + let mut d: MoveOnly = { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + MoveOnlyImpl::move_from(&__v.as_pointer(), { c.as_pointer() }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }; assert!((d.v == 1)); assert!(({ (*c.borrow()).v } == 0)); let e: Value = Rc::new(RefCell::new(({ make_1(5) }))); assert!(({ (*e.borrow()).v } == 5)); - assert!((({ by_value_0(MoveOnly::new({ 6 },),) }) == 6)); - assert!((({ by_value_0(MoveOnly::move_from({ e.as_pointer() },),) }) == 5)); + assert!( + (({ + by_value_0({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + MoveOnlyImpl::construct(&__v.as_pointer(), { 6 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }) + }) == 6) + ); + assert!( + (({ + by_value_0({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + MoveOnlyImpl::move_from(&__v.as_pointer(), { e.as_pointer() }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }) + }) == 5) + ); assert!(({ (*e.borrow()).v } == 0)); let mut vec_: Vec = Vec::new(); { - let __a1 = MoveOnly::new({ 7 }); + let __a1 = { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + MoveOnlyImpl::construct(&__v.as_pointer(), { 7 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }; vec_.push(__a1) }; - let f: Value = Rc::new(RefCell::new(MoveOnly::new({ 8 }))); + let f: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + MoveOnlyImpl::construct(&__v.as_pointer(), { 8 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); { - let __a1 = MoveOnly::move_from({ f.as_pointer() }); + let __a1 = { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + MoveOnlyImpl::move_from(&__v.as_pointer(), { f.as_pointer() }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }; vec_.push(__a1) }; assert!((vec_[0_usize].v == 7) && (vec_[1_usize].v == 8)); assert!(({ (*f.borrow()).v } == 0)); - let m: Value = Rc::new(RefCell::new(ConstMove::new())); - let mut m1: ConstMove = ConstMove::new_1({ m.as_pointer() }); - let cm: Value = Rc::new(RefCell::new(ConstMove::new())); - let mut m2: ConstMove = ConstMove::new_2({ cm.as_pointer() }); + let m: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + ConstMoveImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); + let mut m1: ConstMove = { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + ConstMoveImpl::construct_1(&__v.as_pointer(), { m.as_pointer() }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }; + let cm: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + ConstMoveImpl::construct(&__v.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); + let mut m2: ConstMove = { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + ConstMoveImpl::construct_2(&__v.as_pointer(), { cm.as_pointer() }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }; assert!((m1.mark == 1)); assert!((m2.mark == 10)); - let t: Value = Rc::new(RefCell::new(ThrowingMove::new({ 1 }))); - let mut t1: ThrowingMove = ThrowingMove::copy_from({ t.as_pointer() }); + let t: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + ThrowingMoveImpl::construct(&__v.as_pointer(), { 1 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); + let mut t1: ThrowingMove = { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + ThrowingMoveImpl::copy_from(&__v.as_pointer(), { t.as_pointer() }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }; assert!((t1.v == 1)); assert!((t1.copies == 1)); assert!((t1.moves == 0)); assert!(({ (*t.borrow()).v } == 1)); - let n: Value = Rc::new(RefCell::new(NoexceptMove::new({ 2 }))); - let mut n1: NoexceptMove = NoexceptMove::move_from({ n.as_pointer() }); + let n: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + NoexceptMoveImpl::construct(&__v.as_pointer(), { 2 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); + let mut n1: NoexceptMove = { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + NoexceptMoveImpl::move_from(&__v.as_pointer(), { n.as_pointer() }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }; assert!((n1.v == 2)); assert!((n1.copies == 0)); assert!((n1.moves == 1)); assert!(({ (*n.borrow()).v } == 0)); - let g: Value = Rc::new(RefCell::new(MoveOnly::new({ 3 }))); - let mut g1: MoveOnly = MoveOnly::move_from({ g.as_pointer() }); + let g: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + MoveOnlyImpl::construct(&__v.as_pointer(), { 3 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); + let mut g1: MoveOnly = { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + MoveOnlyImpl::move_from(&__v.as_pointer(), { g.as_pointer() }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }; assert!((g1.v == 3)); assert!(({ (*g.borrow()).v } == 0)); return 0; } +pub trait ConstMoveImpl { + fn construct(&self); + fn construct_1(&self, o: Ptr); + fn construct_2(&self, o: Ptr); +} +impl ConstMoveImpl for Ptr { + fn construct(&self) { + field!((*self), mark).write(0); + } + fn construct_1(&self, o: Ptr) { + field!((*self), mark).write((o.with(|__s| __s.mark) + 1)); + } + fn construct_2(&self, o: Ptr) { + field!((*self), mark).write((o.with(|__s| __s.mark) + 10)); + } +} +pub trait MoveOnlyImpl { + fn construct(&self, v: i32); + fn move_from(&self, o: Ptr); +} +impl MoveOnlyImpl for Ptr { + fn construct(&self, mut v: i32) { + field!((*self), v).write(v); + } + fn move_from(&self, o: Ptr) { + field!((*self), v).write(o.with(|__s| __s.v)); + field!(o, v).write(0); + } +} +pub trait NoexceptMoveImpl { + fn construct(&self, v: i32); + fn copy_from(&self, o: Ptr); + fn move_from(&self, o: Ptr); +} +impl NoexceptMoveImpl for Ptr { + fn construct(&self, mut v: i32) { + field!((*self), v).write(v); + field!((*self), copies).write(0); + field!((*self), moves).write(0); + } + fn copy_from(&self, o: Ptr) { + field!((*self), v).write(o.with(|__s| __s.v)); + field!((*self), copies).write((o.with(|__s| __s.copies) + 1)); + field!((*self), moves).write(o.with(|__s| __s.moves)); + } + fn move_from(&self, o: Ptr) { + field!((*self), v).write(o.with(|__s| __s.v)); + field!((*self), copies).write(o.with(|__s| __s.copies)); + field!((*self), moves).write((o.with(|__s| __s.moves) + 1)); + field!(o, v).write(0); + } +} +pub trait ThrowingMoveImpl { + fn construct(&self, v: i32); + fn copy_from(&self, o: Ptr); + fn move_from(&self, o: Ptr); +} +impl ThrowingMoveImpl for Ptr { + fn construct(&self, mut v: i32) { + field!((*self), v).write(v); + field!((*self), copies).write(0); + field!((*self), moves).write(0); + } + fn copy_from(&self, o: Ptr) { + field!((*self), v).write(o.with(|__s| __s.v)); + field!((*self), copies).write((o.with(|__s| __s.copies) + 1)); + field!((*self), moves).write(o.with(|__s| __s.moves)); + } + fn move_from(&self, o: Ptr) { + field!((*self), v).write(o.with(|__s| __s.v)); + field!((*self), copies).write(o.with(|__s| __s.copies)); + field!((*self), moves).write((o.with(|__s| __s.moves) + 1)); + field!(o, v).write(0); + } +} pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/move_this.rs b/tests/unit/out/refcount/move_this.rs index bd75d632e0..6ca5996737 100644 --- a/tests/unit/out/refcount/move_this.rs +++ b/tests/unit/out/refcount/move_this.rs @@ -6,34 +6,19 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Record, ByteRepr, VaArg, FnPtrArg, MoveCtor, CppFrom, Default)] -#[cpp_from(i32, new, value)] +#[derive(Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, MoveCtor, CppFrom, Default)] +#[cpp_from(i32, construct, value)] #[byte_size(4)] pub struct Chain { #[offset(0)] pub v: i32, } -impl Chain { - pub fn new(mut v: i32) -> Self { - Self { v: v } - } - pub fn copy_from(o: Ptr) -> Self { - Self { - v: (o.with(|__s| __s.v) + 100), - } - } - pub fn move_from(o: Ptr) -> Self { - let __this: Chain = Self { - v: (o.with(|__s| __s.v) + 1), - }; - field!(o, v).write(0); - __this - } -} impl Clone for Chain { fn clone(&self) -> Self { let __src: Value = Rc::new(RefCell::new(Chain { v: self.v.clone() })); - Chain::copy_from(__src.as_pointer()) + let __v: Value = Rc::new(RefCell::new(::zero_init())); + ChainImpl::copy_from(&__v.as_pointer(), __src.as_pointer()); + Rc::try_unwrap(__v).ok().unwrap().into_inner() } } pub fn consume_0(mut c: Chain) -> i32 { @@ -44,34 +29,70 @@ pub fn main() { libcc2rs::cpp2rust_exit(main_0()); } fn main_0() -> i32 { - let a: Value = Rc::new(RefCell::new(Chain::new({ 1 }))); + let a: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + ChainImpl::construct(&__v.as_pointer(), { 1 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); ({ ChainImpl::add_4(&({ ChainImpl::add_4(&a.as_pointer(), 1) }), 1) }); assert!(({ (*a.borrow()).v } == 3)); - let b0: Value = Rc::new(RefCell::new(Chain::new({ 5 }))); - let mut b: Chain = Chain::move_from({ - ({ ChainImpl::add_5(&({ ChainImpl::add_5(&b0.as_pointer(), 1) }), 1) }) - }); + let b0: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + ChainImpl::construct(&__v.as_pointer(), { 5 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); + let mut b: Chain = { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + ChainImpl::move_from(&__v.as_pointer(), { + ({ ChainImpl::add_5(&({ ChainImpl::add_5(&b0.as_pointer(), 1) }), 1) }) + }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }; assert!((b.v == 8) && ({ (*b0.borrow()).v } == 0)); let c: Value = Rc::new(RefCell::new( - ({ ChainImpl::take(&Rc::new(RefCell::new(Chain::new({ 10 }))).as_pointer()) }), + ({ + ChainImpl::take( + &Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + ChainImpl::construct(&__v.as_pointer(), { 10 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })) + .as_pointer(), + ) + }), )); assert!(({ (*c.borrow()).v } == 11)); let mut d: Chain = ({ ChainImpl::copy(&c.as_pointer()) }); assert!((d.v == 111) && ({ (*c.borrow()).v } == 11)); - let g: Value = Rc::new(RefCell::new(Chain::new({ 20 }))); + let g: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + ChainImpl::construct(&__v.as_pointer(), { 20 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); assert!( (({ - consume_0(Chain::move_from({ - ({ ChainImpl::self_(&g.as_pointer()) }) - })) + consume_0({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + ChainImpl::move_from(&__v.as_pointer(), { + ({ ChainImpl::self_(&g.as_pointer()) }) + }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }) }) == 21) ); - let e: Value = Rc::new(RefCell::new(Chain::new({ 30 }))); + let e: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + ChainImpl::construct(&__v.as_pointer(), { 30 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); let mut f: Chain = ({ ChainImpl::take(&e.as_pointer()) }); assert!((f.v == 31) && ({ (*e.borrow()).v } == 0)); return 0; } pub trait ChainImpl { + fn construct(&self, v: i32); + fn copy_from(&self, o: Ptr); + fn move_from(&self, o: Ptr); fn add_4(&self, n: i32) -> Ptr; fn add_5(&self, n: i32) -> Ptr; fn take(&self) -> Chain; @@ -79,6 +100,16 @@ pub trait ChainImpl { fn self_(&self) -> Ptr; } impl ChainImpl for Ptr { + fn construct(&self, mut v: i32) { + field!((*self), v).write(v); + } + fn copy_from(&self, o: Ptr) { + field!((*self), v).write((o.with(|__s| __s.v) + 100)); + } + fn move_from(&self, o: Ptr) { + field!((*self), v).write((o.with(|__s| __s.v) + 1)); + field!(o, v).write(0); + } fn add_4(&self, mut n: i32) -> Ptr { { field!((*self), v).with_mut(|__v| *__v = *__v + n) @@ -92,10 +123,18 @@ impl ChainImpl for Ptr { return (*self).clone(); } fn take(&self) -> Chain { - return Chain::move_from({ (*self).clone() }); + return { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + ChainImpl::move_from(&__v.as_pointer(), { (*self).clone() }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }; } fn copy(&self) -> Chain { - return Chain::copy_from({ (*self).clone() }); + return { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + ChainImpl::copy_from(&__v.as_pointer(), { (*self).clone() }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }; } fn self_(&self) -> Ptr { return (*self).clone(); diff --git a/tests/unit/out/refcount/nested_structs.rs b/tests/unit/out/refcount/nested_structs.rs index c2968d3c29..92ff0d5cc2 100644 --- a/tests/unit/out/refcount/nested_structs.rs +++ b/tests/unit/out/refcount/nested_structs.rs @@ -6,13 +6,13 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct Level0_Level1_1_Level2_1_Level3_1 { #[offset(0)] pub x1: i32, } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct Level0_Level1_1_Level2_1_Level3_2 { #[offset(0)] @@ -20,19 +20,19 @@ pub struct Level0_Level1_1_Level2_1_Level3_2 { #[offset(4)] pub x2: i32, } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct Level0_Level1_1_Level2_1 { #[offset(0)] pub x1: i32, } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct Level0_Level1_1 { #[offset(0)] pub x1: i32, } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct Level0_Level1_2 { #[offset(0)] @@ -40,7 +40,7 @@ pub struct Level0_Level1_2 { #[offset(4)] pub x2: i32, } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(1)] pub struct Level0 {} pub fn main() { diff --git a/tests/unit/out/refcount/new.rs b/tests/unit/out/refcount/new.rs index cea2db9812..0bf358cc6a 100644 --- a/tests/unit/out/refcount/new.rs +++ b/tests/unit/out/refcount/new.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct Pair { #[offset(0)] diff --git a/tests/unit/out/refcount/new_bst.rs b/tests/unit/out/refcount/new_bst.rs index fd45a2c7ac..e442c4263b 100644 --- a/tests/unit/out/refcount/new_bst.rs +++ b/tests/unit/out/refcount/new_bst.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(24)] pub struct node_t { #[offset(0)] diff --git a/tests/unit/out/refcount/new_struct.rs b/tests/unit/out/refcount/new_struct.rs index 74478f932b..0431d6d7d1 100644 --- a/tests/unit/out/refcount/new_struct.rs +++ b/tests/unit/out/refcount/new_struct.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct Pair { #[offset(0)] @@ -14,7 +14,7 @@ pub struct Pair { #[offset(4)] pub y: i32, } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(16)] pub struct Triple { #[offset(0)] diff --git a/tests/unit/out/refcount/nttp_distinct_specializations.rs b/tests/unit/out/refcount/nttp_distinct_specializations.rs index 081ae9bb7b..7c5fb3ec65 100644 --- a/tests/unit/out/refcount/nttp_distinct_specializations.rs +++ b/tests/unit/out/refcount/nttp_distinct_specializations.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct S_2_ { #[offset(0)] @@ -14,13 +14,13 @@ pub struct S_2_ { #[offset(4)] pub w: i32, } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct S_1_ { #[offset(0)] pub v: i32, } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct S_3_ { #[offset(0)] diff --git a/tests/unit/out/refcount/offsetof.rs b/tests/unit/out/refcount/offsetof.rs index 20574b17b2..db02cf3294 100644 --- a/tests/unit/out/refcount/offsetof.rs +++ b/tests/unit/out/refcount/offsetof.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(12)] pub struct Layout { #[offset(0)] @@ -16,13 +16,14 @@ pub struct Layout { #[offset(8)] pub c: u16, } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg)] #[byte_size(66)] pub struct Frame { #[offset(0)] pub tag: u16, #[offset(2)] #[byte_size(64)] + #[array_len(64)] pub body: Box<[i8]>, } impl Default for Frame { diff --git a/tests/unit/out/refcount/opaque_forward_decl.rs b/tests/unit/out/refcount/opaque_forward_decl.rs index ca0809ff5d..68b7285f7f 100644 --- a/tests/unit/out/refcount/opaque_forward_decl.rs +++ b/tests/unit/out/refcount/opaque_forward_decl.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(16)] pub struct container { #[offset(0)] diff --git a/tests/unit/out/refcount/opaque_record_copy.rs b/tests/unit/out/refcount/opaque_record_copy.rs index 91e97fa397..52f209175c 100644 --- a/tests/unit/out/refcount/opaque_record_copy.rs +++ b/tests/unit/out/refcount/opaque_record_copy.rs @@ -6,10 +6,10 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(1)] pub struct Probe {} -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct Wrapper_Probe_ { #[offset(0)] diff --git a/tests/unit/out/refcount/opaque_then_defined.rs b/tests/unit/out/refcount/opaque_then_defined.rs index f9d4c8ee08..c848e68b1a 100644 --- a/tests/unit/out/refcount/opaque_then_defined.rs +++ b/tests/unit/out/refcount/opaque_then_defined.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(16)] pub struct list { #[offset(0)] @@ -15,7 +15,7 @@ pub struct list { #[offset(8)] pub size: i32, } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(16)] pub struct node { #[offset(0)] diff --git a/tests/unit/out/refcount/operator_arithmetic_free.rs b/tests/unit/out/refcount/operator_arithmetic_free.rs index c76e4a2a3d..02d95197f9 100644 --- a/tests/unit/out/refcount/operator_arithmetic_free.rs +++ b/tests/unit/out/refcount/operator_arithmetic_free.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct S { #[offset(0)] diff --git a/tests/unit/out/refcount/operator_arithmetic_member.rs b/tests/unit/out/refcount/operator_arithmetic_member.rs index 5419449f96..eaacebd799 100644 --- a/tests/unit/out/refcount/operator_arithmetic_member.rs +++ b/tests/unit/out/refcount/operator_arithmetic_member.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct S { #[offset(0)] diff --git a/tests/unit/out/refcount/operator_bitwise_free.rs b/tests/unit/out/refcount/operator_bitwise_free.rs index ba35d1c272..63f57d0d91 100644 --- a/tests/unit/out/refcount/operator_bitwise_free.rs +++ b/tests/unit/out/refcount/operator_bitwise_free.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct S { #[offset(0)] diff --git a/tests/unit/out/refcount/operator_bitwise_member.rs b/tests/unit/out/refcount/operator_bitwise_member.rs index f2919e3cb6..45aba391cb 100644 --- a/tests/unit/out/refcount/operator_bitwise_member.rs +++ b/tests/unit/out/refcount/operator_bitwise_member.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct S { #[offset(0)] diff --git a/tests/unit/out/refcount/operator_comparison_defaulted.rs b/tests/unit/out/refcount/operator_comparison_defaulted.rs index fffb72ef6c..ecf6df1e15 100644 --- a/tests/unit/out/refcount/operator_comparison_defaulted.rs +++ b/tests/unit/out/refcount/operator_comparison_defaulted.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct Eq { #[offset(0)] @@ -19,7 +19,7 @@ impl CppEq for Eq { EqImpl::operator_eq(__lhs, __rhs.clone()) } } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct Cmp { #[offset(0)] @@ -37,7 +37,7 @@ impl CppEq for Cmp { CmpImpl::operator_cmp(__lhs, __rhs.clone()) == std::cmp::Ordering::Equal } } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct Both { #[offset(0)] @@ -53,7 +53,7 @@ impl CppEq for Both { BothImpl::operator_eq(__lhs, __rhs.clone()) } } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct OrdOnly { #[offset(0)] @@ -69,7 +69,7 @@ impl CppEq for OrdOnly { OrdOnlyImpl::operator_cmp(__lhs, __rhs.clone()) == std::cmp::Ordering::Equal } } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct Inner { #[offset(0)] @@ -85,7 +85,7 @@ impl CppEq for Inner { InnerImpl::operator_cmp(__lhs, __rhs.clone()) == std::cmp::Ordering::Equal } } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct Outer { #[offset(0)] @@ -104,7 +104,7 @@ impl CppEq for Outer { OuterImpl::operator_cmp(__lhs, __rhs.clone()) == std::cmp::Ordering::Equal } } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct Secondary { #[offset(0)] @@ -120,7 +120,7 @@ impl CppEq for Secondary { SecondaryImpl::operator_eq(__lhs, __rhs.clone()) } } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct PtrMember { #[offset(0)] diff --git a/tests/unit/out/refcount/operator_comparison_free.rs b/tests/unit/out/refcount/operator_comparison_free.rs index 10c1c6b225..eb141c4358 100644 --- a/tests/unit/out/refcount/operator_comparison_free.rs +++ b/tests/unit/out/refcount/operator_comparison_free.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct S { #[offset(0)] @@ -46,7 +46,7 @@ pub fn operator_lt_6(a: Ptr, mut b: i32) -> bool { pub fn operator_lt_7(mut a: i32, b: Ptr) -> bool { return ({ a } < { b.with(|__s| __s.v) }); } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct V { #[offset(0)] diff --git a/tests/unit/out/refcount/operator_comparison_member.rs b/tests/unit/out/refcount/operator_comparison_member.rs index 0caa1e8284..c6316b011c 100644 --- a/tests/unit/out/refcount/operator_comparison_member.rs +++ b/tests/unit/out/refcount/operator_comparison_member.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct S { #[offset(0)] diff --git a/tests/unit/out/refcount/operator_comparison_mixed.rs b/tests/unit/out/refcount/operator_comparison_mixed.rs index 1da673e2e9..d4cc7e01d0 100644 --- a/tests/unit/out/refcount/operator_comparison_mixed.rs +++ b/tests/unit/out/refcount/operator_comparison_mixed.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct S { #[offset(0)] diff --git a/tests/unit/out/refcount/operator_comparison_nonbool.rs b/tests/unit/out/refcount/operator_comparison_nonbool.rs index d8b5a75659..455f1ffdc5 100644 --- a/tests/unit/out/refcount/operator_comparison_nonbool.rs +++ b/tests/unit/out/refcount/operator_comparison_nonbool.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct X { #[offset(0)] @@ -40,19 +40,19 @@ pub fn operator_le_4(mut a: X, mut b: X) -> i32 { pub fn operator_ge_5(mut a: X, mut b: X) -> i32 { return if (a.v >= b.v) { 7 } else { 0 }; } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct Result { #[offset(0)] pub r: i32, } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct Custom { #[offset(0)] pub v: i32, } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct Mixed { #[offset(0)] @@ -68,19 +68,14 @@ impl CppEq for Mixed { ((MixedImpl::operator_eq(__lhs, __rhs.clone())) != 0) } } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, CppFrom, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, CppFrom, Default)] #[cpp_into(bool, to_bool)] #[byte_size(1)] pub struct MyBool { #[offset(0)] pub value: bool, } -impl MyBool { - pub fn new(mut v: bool) -> Self { - Self { value: v } - } -} -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct Boolish { #[offset(0)] @@ -103,27 +98,34 @@ impl CppEq for Boolish { } } pub fn operator_eq_7(mut a: Boolish, mut b: Boolish) -> MyBool { - return MyBool::new({ (a.v == b.v) }); + return { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + MyBoolImpl::construct(&__v.as_pointer(), { (a.v == b.v) }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }; } pub fn operator_lt_6(mut a: Boolish, mut b: Boolish) -> MyBool { - return MyBool::new({ (a.v < b.v) }); + return { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + MyBoolImpl::construct(&__v.as_pointer(), { (a.v < b.v) }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + }; } -#[derive(Record, ByteRepr, FnPtrArg, CppFrom, Default)] +#[derive(Record, ByteRepr, ZeroInitialization, FnPtrArg, CppFrom, Default)] #[cpp_into(bool, to_bool)] #[byte_size(1)] pub struct RefBool { #[offset(0)] pub value: bool, } -impl RefBool { - pub fn new(mut v: bool) -> Self { - Self { value: v } - } -} thread_local!( - pub static ref_true_8: Value = Rc::new(RefCell::new(RefBool::new({ true }))); + pub static ref_true_8: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + RefBoolImpl::construct(&__v.as_pointer(), { true }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); ); -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct RefInt { #[offset(0)] @@ -281,17 +283,25 @@ impl MixedImpl for Ptr { } } pub trait MyBoolImpl { + fn construct(&self, v: bool); fn to_bool(&self) -> bool; } impl MyBoolImpl for Ptr { + fn construct(&self, mut v: bool) { + field!((*self), value).write(v); + } fn to_bool(&self) -> bool { return (*self).with(|__s| __s.value); } } pub trait RefBoolImpl { + fn construct(&self, v: bool); fn to_bool(&self) -> bool; } impl RefBoolImpl for Ptr { + fn construct(&self, mut v: bool) { + field!((*self), value).write(v); + } fn to_bool(&self) -> bool { return (*self).with(|__s| __s.value); } diff --git a/tests/unit/out/refcount/operator_comparison_noncopyable.rs b/tests/unit/out/refcount/operator_comparison_noncopyable.rs index 367857ef38..c99f609601 100644 --- a/tests/unit/out/refcount/operator_comparison_noncopyable.rs +++ b/tests/unit/out/refcount/operator_comparison_noncopyable.rs @@ -12,23 +12,13 @@ pub fn operator_eq_0(x: Ptr, y: Ptr) -> bool { pub fn operator_lt_1(x: Ptr, y: Ptr) -> bool { return ({ x.with(|__s| __s.data_) } < { y.with(|__s| __s.data_) }); } -#[derive(Record, ByteRepr, FnPtrArg, MoveCtor, CppFrom, Default)] -#[cpp_from(i32, new, value)] +#[derive(Record, ByteRepr, ZeroInitialization, FnPtrArg, MoveCtor, CppFrom, Default)] +#[cpp_from(i32, construct, value)] #[byte_size(4)] pub struct S { #[offset(0)] data_: i32, } -impl S { - pub fn new(mut data: i32) -> Self { - Self { data_: data } - } - pub fn move_from(_a0: Ptr) -> Self { - Self { - data_: { (*_a0.upgrade().deref()).data_ }, - } - } -} impl CppLess for S { fn less(__lhs: &Ptr, __rhs: &Ptr) -> bool { operator_lt_1(__lhs.clone(), __rhs.clone()) @@ -44,12 +34,36 @@ pub fn main() { libcc2rs::cpp2rust_exit(main_0()); } fn main_0() -> i32 { - let a: Value = Rc::new(RefCell::new(S::new({ 1 }))); - let b: Value = Rc::new(RefCell::new(S::new({ 2 }))); - let c: Value = Rc::new(RefCell::new(S::new({ 1 }))); + let a: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + SImpl::construct(&__v.as_pointer(), { 1 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); + let b: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + SImpl::construct(&__v.as_pointer(), { 2 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); + let c: Value = Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + SImpl::construct(&__v.as_pointer(), { 1 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })); assert!(({ operator_eq_0(a.as_pointer(), c.as_pointer(),) })); assert!(({ operator_lt_1(a.as_pointer(), b.as_pointer(),) })); assert!(!({ operator_lt_1(b.as_pointer(), a.as_pointer(),) })); return 0; } +pub trait SImpl { + fn construct(&self, data: i32); + fn move_from(&self, _a0: Ptr); +} +impl SImpl for Ptr { + fn construct(&self, mut data: i32) { + field!((*self), data_).write(data); + } + fn move_from(&self, _a0: Ptr) { + field!((*self), data_).write({ (*_a0.upgrade().deref()).data_ }); + } +} pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/operator_compound_assignment_free.rs b/tests/unit/out/refcount/operator_compound_assignment_free.rs index 479d22f6bb..865ca036ff 100644 --- a/tests/unit/out/refcount/operator_compound_assignment_free.rs +++ b/tests/unit/out/refcount/operator_compound_assignment_free.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct S { #[offset(0)] diff --git a/tests/unit/out/refcount/operator_compound_assignment_member.rs b/tests/unit/out/refcount/operator_compound_assignment_member.rs index d62257a251..3cfb3136fc 100644 --- a/tests/unit/out/refcount/operator_compound_assignment_member.rs +++ b/tests/unit/out/refcount/operator_compound_assignment_member.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct S { #[offset(0)] diff --git a/tests/unit/out/refcount/operator_friend.rs b/tests/unit/out/refcount/operator_friend.rs index 10374b19b2..57ea87a36f 100644 --- a/tests/unit/out/refcount/operator_friend.rs +++ b/tests/unit/out/refcount/operator_friend.rs @@ -10,7 +10,7 @@ pub fn operator_eq_0(_a0: Ptr, _a1: Ptr) -> bool { return ({ _a0.with(|__s| __s.a) } == { _a1.with(|__s| __s.a) }) && ({ _a0.with(|__s| __s.b) } == { _a1.with(|__s| __s.b) }); } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct Defaulted { #[offset(0)] @@ -38,7 +38,7 @@ pub fn operator_cmp_1(_a0: Ptr, _a1: Ptr) -> std::cm pub fn operator_eq_2(_a0: Ptr, _a1: Ptr) -> bool { return ({ _a0.with(|__s| __s.a) } == { _a1.with(|__s| __s.a) }); } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct DefaultedOrd { #[offset(0)] @@ -65,7 +65,7 @@ pub fn operator_add_5(x: Ptr, y: Ptr) -> Inline { a: ({ x.with(|__s| __s.a) } + { y.with(|__s| __s.a) }), }; } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct Inline { #[offset(0)] @@ -81,7 +81,7 @@ impl CppEq for Inline { operator_eq_3(__lhs.clone(), __rhs.clone()) } } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct OutOfLine { #[offset(0)] @@ -107,7 +107,7 @@ pub fn operator_lt_9(x: Ptr, y: Ptr) -> bool { pub fn operator_eq_10(x: Ptr, y: Ptr) -> bool { return ({ (x.with(|__s| __s.v) as i64) } == { y.with(|__s| __s.v) }); } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct Tmpl_int_ { #[offset(0)] @@ -123,7 +123,7 @@ impl CppEq for Tmpl_int_ { operator_eq_8(__lhs.clone(), __rhs.clone()) } } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct Tmpl_long_ { #[offset(0)] @@ -132,7 +132,7 @@ pub struct Tmpl_long_ { pub fn operator_eq_11(_a0: Ptr, _a1: Ptr) -> bool { return ({ _a0.with(|__s| __s.v) } == { _a1.with(|__s| __s.v) }); } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct TmplDefaulted_int_ { #[offset(0)] diff --git a/tests/unit/out/refcount/operator_less_than.rs b/tests/unit/out/refcount/operator_less_than.rs index b33a1934a9..7785126728 100644 --- a/tests/unit/out/refcount/operator_less_than.rs +++ b/tests/unit/out/refcount/operator_less_than.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct Pair { #[offset(0)] diff --git a/tests/unit/out/refcount/operator_logical_free.rs b/tests/unit/out/refcount/operator_logical_free.rs index 04c77edeb8..19d2e7e167 100644 --- a/tests/unit/out/refcount/operator_logical_free.rs +++ b/tests/unit/out/refcount/operator_logical_free.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct S { #[offset(0)] diff --git a/tests/unit/out/refcount/operator_logical_member.rs b/tests/unit/out/refcount/operator_logical_member.rs index e6bd068fdb..7db68dd321 100644 --- a/tests/unit/out/refcount/operator_logical_member.rs +++ b/tests/unit/out/refcount/operator_logical_member.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct S { #[offset(0)] diff --git a/tests/unit/out/refcount/operator_member_pointer_free.rs b/tests/unit/out/refcount/operator_member_pointer_free.rs index a212365e62..0a7ee74641 100644 --- a/tests/unit/out/refcount/operator_member_pointer_free.rs +++ b/tests/unit/out/refcount/operator_member_pointer_free.rs @@ -6,17 +6,18 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct Inner { #[offset(0)] pub x: i32, } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg)] #[byte_size(16)] pub struct S { #[offset(0)] #[byte_size(12)] + #[array_len(3)] pub data: Box<[i32]>, #[offset(12)] #[byte_size(4)] diff --git a/tests/unit/out/refcount/operator_member_pointer_member.rs b/tests/unit/out/refcount/operator_member_pointer_member.rs index f6a919abdc..ed1b0db50b 100644 --- a/tests/unit/out/refcount/operator_member_pointer_member.rs +++ b/tests/unit/out/refcount/operator_member_pointer_member.rs @@ -6,13 +6,13 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct Inner { #[offset(0)] pub x: i32, } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(1)] pub struct Table {} impl Table { @@ -23,11 +23,12 @@ impl Table { thread_local!( pub static table_0: Value> = Rc::new(RefCell::new(Box::new([7, 8, 9]))); ); -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[derive(Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg)] #[byte_size(16)] pub struct S { #[offset(0)] #[byte_size(12)] + #[array_len(3)] pub data: Box<[i32]>, #[offset(12)] #[byte_size(4)] diff --git a/tests/unit/out/refcount/operator_other_free.rs b/tests/unit/out/refcount/operator_other_free.rs index 63aeb18434..1171d169bf 100644 --- a/tests/unit/out/refcount/operator_other_free.rs +++ b/tests/unit/out/refcount/operator_other_free.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct S { #[offset(0)] diff --git a/tests/unit/out/refcount/operator_other_member.rs b/tests/unit/out/refcount/operator_other_member.rs index 62a11f97bb..3b627eea25 100644 --- a/tests/unit/out/refcount/operator_other_member.rs +++ b/tests/unit/out/refcount/operator_other_member.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(1)] pub struct Static {} impl Static { @@ -14,7 +14,7 @@ impl Static { return (a * b); } } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, CppFrom, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, CppFrom, Default)] #[cpp_into(i32, to_i32)] #[byte_size(4)] pub struct S { diff --git a/tests/unit/out/refcount/operator_overloads.rs b/tests/unit/out/refcount/operator_overloads.rs index 054abb39ca..6c532c24ef 100644 --- a/tests/unit/out/refcount/operator_overloads.rs +++ b/tests/unit/out/refcount/operator_overloads.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct S { #[offset(0)] diff --git a/tests/unit/out/refcount/operator_qualifiers.rs b/tests/unit/out/refcount/operator_qualifiers.rs index a3140eb65a..6bf6f81a61 100644 --- a/tests/unit/out/refcount/operator_qualifiers.rs +++ b/tests/unit/out/refcount/operator_qualifiers.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct S { #[offset(0)] diff --git a/tests/unit/out/refcount/operator_three_way.rs b/tests/unit/out/refcount/operator_three_way.rs index 5677bec797..9298036a09 100644 --- a/tests/unit/out/refcount/operator_three_way.rs +++ b/tests/unit/out/refcount/operator_three_way.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct S { #[offset(0)] diff --git a/tests/unit/out/refcount/operator_traits.rs b/tests/unit/out/refcount/operator_traits.rs index f7922a7928..8b372ebfe5 100644 --- a/tests/unit/out/refcount/operator_traits.rs +++ b/tests/unit/out/refcount/operator_traits.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct Lt { #[offset(0)] @@ -22,7 +22,7 @@ impl CppEq for Lt { !(LtImpl::operator_lt(__lhs, __rhs.clone())) && !(LtImpl::operator_lt(__rhs, __lhs.clone())) } } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct Eq { #[offset(0)] @@ -33,7 +33,7 @@ impl CppEq for Eq { EqImpl::operator_eq(__lhs, __rhs.clone()) } } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct Cmp { #[offset(0)] @@ -49,7 +49,7 @@ impl CppEq for Cmp { CmpImpl::operator_eq(__lhs, __rhs.clone()) } } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct Free { #[offset(0)] @@ -71,7 +71,7 @@ pub fn operator_lt_0(a: Ptr, b: Ptr) -> bool { pub fn operator_eq_1(a: Ptr, b: Ptr) -> bool { return ({ a.with(|__s| __s.v) } == { b.with(|__s| __s.v) }); } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct Wrapped_int_ { #[offset(0)] diff --git a/tests/unit/out/refcount/overload_mangling.rs b/tests/unit/out/refcount/overload_mangling.rs index 1db90fed19..0ae65e219a 100644 --- a/tests/unit/out/refcount/overload_mangling.rs +++ b/tests/unit/out/refcount/overload_mangling.rs @@ -19,16 +19,16 @@ pub fn add_1(mut p: Ptr, mut n: i32) { pub fn twice_2(mut n: i32) -> i32 { return (n * 2); } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(1)] pub struct Access_S_ {} -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct S { #[offset(0)] pub base: i32, } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct Box_ { #[offset(0)] diff --git a/tests/unit/out/refcount/pair_converting_construction.rs b/tests/unit/out/refcount/pair_converting_construction.rs index fc0fabb561..fcef65dc27 100644 --- a/tests/unit/out/refcount/pair_converting_construction.rs +++ b/tests/unit/out/refcount/pair_converting_construction.rs @@ -6,42 +6,30 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, CppFrom, Default)] -#[cpp_from(i32, new, value)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, CppFrom, Default)] +#[cpp_from(i32, construct, value)] #[byte_size(4)] pub struct Celsius { #[offset(0)] pub degrees: i32, } -impl Celsius { - pub fn new(mut d: i32) -> Self { - Self { degrees: d } - } -} -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, CppFrom, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, CppFrom, Default)] #[cpp_into(i32, to_i32)] #[byte_size(4)] pub struct Meters { #[offset(0)] pub value: i32, } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, CppFrom, Default)] -#[cpp_from(Celsius, new, ptr)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, CppFrom, Default)] +#[cpp_from(Celsius, construct, ptr)] #[byte_size(4)] pub struct Label { #[offset(0)] pub id: i32, } -impl Label { - pub fn new(c: Ptr) -> Self { - Self { - id: c.with(|__s| __s.degrees), - } - } -} -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, CppFrom, Default)] -#[cpp_from(Celsius, new_1, ptr)] -#[cpp_from_move(Celsius, new_2, ptr)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, CppFrom, Default)] +#[cpp_from(Celsius, construct_1, ptr)] +#[cpp_from_move(Celsius, construct_2, ptr)] #[byte_size(8)] pub struct Owner { #[offset(0)] @@ -49,104 +37,58 @@ pub struct Owner { #[offset(4)] pub copied: i32, } -impl Owner { - pub fn new_1(c: Ptr) -> Self { - Self { - taken: 0, - copied: c.with(|__s| __s.degrees), - } - } - pub fn new_2(c: Ptr) -> Self { - Self { - taken: c.with(|__s| __s.degrees), - copied: 0, - } - } -} -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, CppFrom, Default)] -#[cpp_from(Celsius, new, value)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, CppFrom, Default)] +#[cpp_from(Celsius, construct, value)] #[byte_size(4)] pub struct Holder { #[offset(0)] pub value: i32, } -impl Holder { - pub fn new(mut c: Celsius) -> Self { - Self { value: c.degrees } - } -} -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, CppFrom)] -#[cpp_from(i32, new, value)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, CppFrom)] +#[cpp_from(i32, construct, value)] #[byte_size(4)] pub struct Fallback { #[offset(0)] pub value: i32, } -impl Fallback { - pub fn new(mut v: i32) -> Self { - Self { value: v } - } -} impl Default for Fallback { fn default() -> Self { - { Fallback::new({ -1_i32 }) } + { + { + let __v: Value = Rc::new(RefCell::new(::zero_init())); + FallbackImpl::construct(&__v.as_pointer(), { -1_i32 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + } + } } } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct Counter { #[offset(0)] pub hits: i32, } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, CppFrom, Default)] -#[cpp_from(Counter, new, mut_ptr)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, CppFrom, Default)] +#[cpp_from(Counter, construct, mut_ptr)] #[byte_size(4)] pub struct Observer { #[offset(0)] pub seen: i32, } -impl Observer { - pub fn new(c: Ptr) -> Self { - let __this: Observer = Self { - seen: c.with(|__s| __s.hits), - }; - field!(c, hits).with_mut(|__v| __v.postfix_inc()); - __this - } -} -impl Tag { - pub fn new(u: Ptr) -> Self { - Self { - code: (u.with(|__s| __s.degrees) + 100), - } - } -} -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, CppFrom, Default)] -#[cpp_from(Celsius, new, ptr)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, CppFrom, Default)] +#[cpp_from(Celsius, construct, ptr)] #[byte_size(4)] pub struct Tag { #[offset(0)] pub code: i32, } -#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, CppFrom, Default)] -#[cpp_from(Ptr< i8 > , new, value)] +#[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, CppFrom, Default)] +#[cpp_from(Ptr< i8 > , construct, value)] #[byte_size(4)] pub struct Name { #[offset(0)] pub len: i32, } -impl Name { - pub fn new(mut s: Ptr) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { len: 0 })); - let this: Ptr = __this.as_pointer(); - 'loop_: while ((elem!(s, this.with(|__s| __s.len)).read()) != 0) { - field!(this, len).with_mut(|__v| { - __v.prefix_inc(); - }); - } - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } -} pub fn main() { __cpp2rust_init_globals(); libcc2rs::cpp2rust_exit(main_0()); @@ -159,8 +101,16 @@ fn main_0() -> i32 { assert!(({ ((*(*widened.borrow()).0.borrow()) == -1_i64) })); assert!(({ ((*(*widened.borrow()).1.borrow()) == 2_u32) })); let from_ints: Value<(Value, Value)> = Rc::new(RefCell::new(( - Rc::new(RefCell::new(Celsius::new({ 1 }))), - Rc::new(RefCell::new(Celsius::new({ 2 }))), + Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + CelsiusImpl::construct(&__v.as_pointer(), { 1 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })), + Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + CelsiusImpl::construct(&__v.as_pointer(), { 2 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })), ))); assert!(({ (*(*from_ints.borrow()).0.borrow()).degrees } == 1)); assert!(({ (*(*from_ints.borrow()).1.borrow()).degrees } == 2)); @@ -221,8 +171,16 @@ fn main_0() -> i32 { assert!(({ ((*(*from_operator_pair.borrow()).0.borrow()) == 8) })); assert!(({ ((*(*from_operator_pair.borrow()).1.borrow()) == 9) })); let celsius: Value<(Value, Value)> = Rc::new(RefCell::new(( - Rc::new(RefCell::new(Celsius::new({ 10 }))), - Rc::new(RefCell::new(Celsius::new({ 11 }))), + Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + CelsiusImpl::construct(&__v.as_pointer(), { 10 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })), + Rc::new(RefCell::new({ + let __v: Value = Rc::new(RefCell::new(::zero_init())); + CelsiusImpl::construct(&__v.as_pointer(), { 11 }); + Rc::try_unwrap(__v).ok().unwrap().into_inner() + })), ))); let from_record_pair: Value<(Value