diff --git a/cpp2rust/converter/converter.cpp b/cpp2rust/converter/converter.cpp index f836c6c233..16b7aebe2e 100644 --- a/cpp2rust/converter/converter.cpp +++ b/cpp2rust/converter/converter.cpp @@ -74,36 +74,36 @@ Converter::DeferredBlock &Converter::DeferredImpl(std::string header) { return it->second; } -void Converter::AddEqualityComparableMethod(const clang::FunctionDecl *op, - const RecordTrait &trait, - std::string_view method) { +void Converter::AddComparisonTraitMethod(const clang::FunctionDecl *op, + const RecordTrait &trait, + std::string_view method) { auto *record = GetComparisonRecord(op); auto name = GetRecordName(record); clang::QualType lhs = ctx_.getCanonicalTagType(record); auto rhs = GetComparisonRhsType(op); if (lhs == rhs) { - EqualityComparableImpl(name, "").body += method; + ComparisonTraitImpl(trait, name, "").body += method; if (&trait == &kWeaklyEqualityComparableWith) { converted_record_traits_.emplace(name, trait.refcount.name); } return; } auto other = GetUnboxedTypeAsString(rhs); - EqualityComparableImpl(name, other).body += method; + ComparisonTraitImpl(trait, name, other).body += method; if (&trait != &kWeaklyEqualityComparableWith || - HasEqualityOperator(ctx_, rhs, lhs)) { + FindComparisonOperator(ctx_, rhs, lhs, clang::OO_EqualEqual)) { return; } - EqualityComparableImpl(other, name).body += + ComparisonTraitImpl(trait, other, name).body += std::format("{} {{ <{} as {}<{}>>::{}(rhs, lhs) }}\n", GetComparisonTraitSignature(trait, "lhs", "rhs", name), name, GetRecordTraitSpelling(trait).name, other, trait.method); } Converter::DeferredBlock & -Converter::EqualityComparableImpl(std::string_view self, - std::string_view other) { - const char *name = GetRecordTraitSpelling(kWeaklyEqualityComparableWith).name; +Converter::ComparisonTraitImpl(const RecordTrait &trait, std::string_view self, + std::string_view other) { + const char *name = GetRecordTraitSpelling(trait).name; if (other.empty()) { return DeferredImpl(std::format("impl {} for {}", name, self)); } @@ -136,13 +136,16 @@ void Converter::EmitUndefinedRecordTraits(Model model, std::string &out) { void Converter::DeclareRecordTraits(const clang::CXXRecordDecl *decl) { auto &traits = declared_record_traits_[GetRecordName(decl)]; for (auto *method : decl->methods()) { - if (GetComparisonTrait(method)) { - if (IsSameTypeComparison(method, decl)) { + if (auto *comparison = GetComparisonTrait(method); + comparison && HasDeferredComparisonImpl(*comparison)) { + if (IsEqualityComparableTrait(*comparison) && + IsSameTypeComparison(method, decl)) { traits.emplace(kWeaklyEqualityComparableWith.refcount.name, &kWeaklyEqualityComparableWith); } } else if (auto *trait = GetRecordTrait(method); - trait && !IsDerivedCopyOrMove(method)) { + trait && trait != &kThreeWayComparable && + !IsDerivedCopyOrMove(method)) { traits.emplace(trait->refcount.name, trait); } } @@ -590,7 +593,24 @@ void Converter::ConvertComparisonTraitImpl(clang::FunctionDecl *decl, } method = std::move(buf).str(); } - AddEqualityComparableMethod(decl, trait, method); + if (HasDeferredComparisonImpl(trait)) { + AddComparisonTraitMethod(decl, trait, method); + return; + } + auto record_name = GetRecordName(GetComparisonRecord(decl)); + converted_record_traits_.emplace(record_name, trait.refcount.name); + StrCat(keyword::kImpl, GetRecordTraitSpelling(trait).name, "for", + record_name); + PushBrace impl_brace(*this); + if (&trait == &kThreeWayComparable) { + StrCat(GetComparisonCategory(decl)); + } + StrCat(method); +} + +std::string Converter::GetComparisonCategory(const clang::FunctionDecl *op) { + return std::format("type Category = {};", + GetUnboxedTypeAsString(op->getReturnType())); } void Converter::EmitHoistedDecls(clang::CompoundStmt *body) { @@ -1215,8 +1235,8 @@ void Converter::ConvertCopyOrMoveTraitImpl(clang::CXXMethodDecl *method, if (body.empty()) { return; } - if (GetComparisonTrait(method)) { - AddEqualityComparableMethod(method, trait, body); + if (HasDeferredComparisonImpl(trait)) { + AddComparisonTraitMethod(method, trait, body); return; } converted_record_traits_.emplace(GetRecordName(method->getParent()), @@ -1224,6 +1244,9 @@ void Converter::ConvertCopyOrMoveTraitImpl(clang::CXXMethodDecl *method, StrCat(keyword::kImpl, GetCopyOrMoveTraitName(trait), "for", GetRecordName(method->getParent())); PushBrace brace(*this); + if (&trait == &kThreeWayComparable) { + StrCat(GetComparisonCategory(method)); + } StrCat(body); } @@ -1245,11 +1268,27 @@ std::string Converter::DestroyMembers(const clang::CXXRecordDecl *decl) { } void Converter::EmitDerives(const clang::RecordDecl *decl) { + auto *cxx = clang::dyn_cast(decl); + auto *three_way = cxx ? GetDerivingThreeWayOperator(ctx_, cxx) : nullptr; StrCat("#[derive("); for (auto *attr : GetStructAttributes(decl)) { StrCat(attr, ','); } + if (three_way) { + StrCat(GetCopyOrMoveTraitName(kPartiallyOrderedWith), ','); + } StrCat(")]"); + if (auto *method = clang::dyn_cast_or_null(three_way)) { + StrCat(std::format("#[three_way({}::{})]", GetUFCSName(method), + GetMethodName(method))); + } else if (auto *trait = + three_way ? GetComparisonTrait(three_way) : nullptr) { + StrCat(std::format("#[three_way({}::{})]", GetCopyOrMoveTraitName(*trait), + trait->method)); + } else if (three_way) { + StrCat(std::format("#[three_way({})]", + GetNamedDeclAsString(three_way->getCanonicalDecl()))); + } if (auto methods = GetDerivedCopyOrMoves(decl); !methods.empty()) { StrCat("#[memberwise("); for (auto *method : methods) { @@ -3484,9 +3523,13 @@ bool Converter::VisitBinaryOperator(clang::BinaryOperator *expr) { } if (expr->getOpcode() == clang::BO_Cmp) { - EmitFresh(std::format("std::cmp::Ord::cmp(&({}), &({}))", - ConvertRValue(expr->getLHS()), - ConvertRValue(expr->getRHS()))); + auto *category = ctx_.CompCategories.lookupInfoForType(expr->getType()); + assert(category && "built-in <=> gives a comparison category"); + EmitFresh(std::format( + "{}::from(std::cmp::{}(&({}), &({})))", + GetUnboxedTypeAsString(expr->getType()), + category->isPartial() ? "PartialOrd::partial_cmp" : "Ord::cmp", + ConvertRValue(expr->getLHS()), ConvertRValue(expr->getRHS()))); return false; } bool needs_cast = (expr->isComparisonOp() || expr->isLogicalOp()) && @@ -5903,34 +5946,6 @@ bool Converter::ConvertOutOfLineVirtualMethod(clang::CXXMethodDecl *decl) { return emitted; } -void Converter::ConvertOrdAndPartialOrdTraitsBase( - std::string_view cmp_body, std::string_view eq_body, - std::string_view record_name) { - if (cmp_body.empty()) { - return; - } - StrCat(keyword::kImpl, "std::cmp::Ord for ", record_name, '{'); - StrCat("fn cmp(&self, other: &Self) -> std::cmp::Ordering {"); - StrCat(std::format("{} {{", keyword_unsafe_)); - StrCat(cmp_body); - StrCat("}}}"); - - StrCat(keyword::kImpl, "std::cmp::PartialOrd for", record_name, '{'); - StrCat(R"( - fn partial_cmp(&self, other: &Self) -> Option { - Some(self.cmp(other)) - } - })"); - - StrCat(keyword::kImpl, "std::cmp::PartialEq for", record_name, '{'); - StrCat("fn eq(&self, other: &Self) -> bool {"); - StrCat(std::format("{} {{", keyword_unsafe_)); - StrCat(eq_body); - StrCat("}}}"); - - StrCat(keyword::kImpl, "std::cmp::Eq for", record_name, "{}"); -} - std::string Converter::GetComparisonCall(const clang::FunctionDecl *op, const clang::CXXRecordDecl *decl, std::string_view lhs, @@ -6011,46 +6026,63 @@ std::string Converter::GetComparisonReceiver(const clang::CXXMethodDecl *method, return is_mut ? std::format("{}.cast_mut()", lhs) : std::string(lhs); } -std::string Converter::GetEqBody(const clang::CXXRecordDecl *decl, - const clang::FunctionDecl *eq, - const clang::FunctionDecl *lt, - const clang::FunctionDecl *cmp, - std::string_view lhs, std::string_view rhs) { - if (eq) { - return GetComparisonBool(eq, decl, lhs, rhs); - } - if (cmp) { - return std::format("{} == std::cmp::Ordering::Equal", - GetComparisonCall(cmp, decl, lhs, rhs)); - } - return std::format("!({}) && !({})", GetComparisonBool(lt, decl, lhs, rhs), - GetComparisonBool(lt, decl, rhs, lhs)); -} - -void Converter::ConvertOrdAndPartialOrdTraits(const clang::CXXRecordDecl *decl, - const clang::FunctionDecl *eq, - const clang::FunctionDecl *lt, - const clang::FunctionDecl *cmp) { - const char *kSelfPtr = "(self as *const Self)"; - const char *kOtherPtr = "(other as *const Self)"; - std::string cmp_body; - if (cmp) { - cmp_body = GetComparisonCall(cmp, decl, kSelfPtr, kOtherPtr); - } else if (lt) { - cmp_body = std::format("if {} {{ std::cmp::Ordering::Less }} else if {} {{ " - "std::cmp::Ordering::Greater }} else {{ " - "std::cmp::Ordering::Equal }}", - GetComparisonBool(lt, decl, kSelfPtr, kOtherPtr), - GetComparisonBool(lt, decl, kOtherPtr, kSelfPtr)); - } - ConvertOrdAndPartialOrdTraitsBase( - cmp_body, GetEqBody(decl, eq, lt, cmp, kSelfPtr, kOtherPtr), - GetRecordName(decl)); +void Converter::ConvertComparisonTraitWrappers(const clang::CXXRecordDecl *decl, + const clang::FunctionDecl *eq, + const clang::FunctionDecl *lt, + const clang::FunctionDecl *cmp) { + if (auto *op = lt ? lt : cmp; op && !(lt && GetComparisonTrait(lt))) { + const auto &trait = kLessThanComparable; + auto [lhs, rhs] = GetComparisonOperandNames(op); + StrCat(keyword::kImpl, GetRecordTraitSpelling(trait).name, "for", + GetRecordName(decl)); + PushBrace impl_brace(*this); + StrCat(GetComparisonTraitSignature(trait, lhs, rhs, "Self")); + PushBrace body(*this); + if (lt) { + StrCat(GetComparisonBool(lt, decl, lhs, rhs)); + } else { + StrCat(std::format("libcc2rs::is_lt({})", + GetComparisonCall(cmp, decl, lhs, rhs))); + } + } if (eq && !GetComparisonTrait(eq)) { ConvertEqualityComparableImpl(decl, eq); } } +void Converter::ConvertPartiallyOrderedWithImpl( + const clang::CXXRecordDecl *decl) { + if (!IsPartiallyOrdered(ctx_, decl) || + GetDerivingThreeWayOperator(ctx_, decl)) { + return; + } + clang::QualType type = ctx_.getCanonicalTagType(decl); + auto *cmp = FindComparisonOperator(ctx_, type, type, clang::OO_Spaceship); + auto add = [&](const RecordTrait &trait, const std::string &body) { + ComparisonTraitImpl(trait, GetRecordName(decl), "").body += std::format( + "{} {{ {} }}\n", + GetComparisonTraitSignature(trait, "lhs", "rhs", "Self"), body); + }; + add(kPartiallyOrderedWith, + std::format("::{}(lhs, rhs)", + GetRecordTraitSpelling(kLessThanComparable).name, + kLessThanComparable.method)); + const std::pair relations[] = { + {clang::OO_Greater, "is_gt"}, + {clang::OO_LessEqual, "is_lteq"}, + {clang::OO_GreaterEqual, "is_gteq"}}; + for (auto [kind, from_three_way] : relations) { + const auto &trait = GetPartiallyOrderedWithTrait(kind); + auto *op = FindComparisonOperator(ctx_, type, type, kind); + if (!op) { + add(trait, std::format("libcc2rs::{}({})", from_three_way, + GetComparisonCall(cmp, decl, "lhs", "rhs"))); + } else if (!GetComparisonTrait(op)) { + add(trait, GetComparisonBool(op, decl, "lhs", "rhs")); + } + } +} + std::pair Converter::GetComparisonOperandNames(const clang::FunctionDecl *op) { auto params = DefinitionParameters(op); @@ -6066,7 +6098,7 @@ void Converter::ConvertEqualityComparableImpl(const clang::CXXRecordDecl *decl, ? kWeaklyEqualityComparableWithNotEqual : kWeaklyEqualityComparableWith; auto [lhs, rhs] = GetComparisonOperandNames(op); - AddEqualityComparableMethod( + AddComparisonTraitMethod( op, trait, std::format("{} {{ {} }}\n", GetComparisonTraitSignature(trait, lhs, rhs, "Self"), @@ -6140,12 +6172,13 @@ void Converter::AddOrdTrait(const clang::CXXRecordDecl *decl) { if (eq && ne && !GetComparisonTrait(ne)) { ConvertEqualityComparableImpl(decl, ne); } + ConvertPartiallyOrderedWithImpl(decl); if (!eq && !lt && !cmp) { return; } - ConvertOrdAndPartialOrdTraits(decl, eq, lt, cmp); + ConvertComparisonTraitWrappers(decl, eq, lt, cmp); } void Converter::AddSpecialMemberDerives(const clang::RecordDecl *decl, diff --git a/cpp2rust/converter/converter.h b/cpp2rust/converter/converter.h index 074462a88a..c180fceaf0 100644 --- a/cpp2rust/converter/converter.h +++ b/cpp2rust/converter/converter.h @@ -501,6 +501,10 @@ class Converter : public clang::RecursiveASTVisitor { void ConvertEqualityComparableImpl(const clang::CXXRecordDecl *decl, const clang::FunctionDecl *op); + void ConvertPartiallyOrderedWithImpl(const clang::CXXRecordDecl *decl); + + std::string GetComparisonCategory(const clang::FunctionDecl *op); + void ConvertParamTy(clang::QualType param_type, clang::Expr *expr); virtual void ConvertParamTyConstCast(clang::QualType param_type, @@ -968,20 +972,10 @@ class Converter : public clang::RecursiveASTVisitor { void AddOrdTrait(const clang::CXXRecordDecl *decl); - virtual void ConvertOrdAndPartialOrdTraits(const clang::CXXRecordDecl *decl, - const clang::FunctionDecl *eq, - const clang::FunctionDecl *lt, - const clang::FunctionDecl *cmp); - - std::string GetEqBody(const clang::CXXRecordDecl *decl, - const clang::FunctionDecl *eq, - const clang::FunctionDecl *lt, - const clang::FunctionDecl *cmp, std::string_view lhs, - std::string_view rhs); - - void ConvertOrdAndPartialOrdTraitsBase(std::string_view cmp_body, - std::string_view eq_body, - std::string_view record_name); + void ConvertComparisonTraitWrappers(const clang::CXXRecordDecl *decl, + const clang::FunctionDecl *eq, + const clang::FunctionDecl *lt, + const clang::FunctionDecl *cmp); std::string GetComparisonCall(const clang::FunctionDecl *op, const clang::CXXRecordDecl *decl, @@ -1394,12 +1388,13 @@ class Converter : public clang::RecursiveASTVisitor { static DeferredBlock &DeferredImpl(std::string header); - DeferredBlock &EqualityComparableImpl(std::string_view self, - std::string_view other); + DeferredBlock &ComparisonTraitImpl(const RecordTrait &trait, + std::string_view self, + std::string_view other); - void AddEqualityComparableMethod(const clang::FunctionDecl *op, - const RecordTrait &trait, - std::string_view method); + void AddComparisonTraitMethod(const clang::FunctionDecl *op, + const RecordTrait &trait, + std::string_view method); static std::map> declared_record_traits_; static std::set> converted_record_traits_; diff --git a/cpp2rust/converter/converter_lib.cpp b/cpp2rust/converter/converter_lib.cpp index eb4f59fef5..1e1686901a 100644 --- a/cpp2rust/converter/converter_lib.cpp +++ b/cpp2rust/converter/converter_lib.cpp @@ -681,82 +681,185 @@ clang::QualType GetComparisonRhsType(const clang::FunctionDecl *fn) { .getCanonicalType(); } -bool HasEqualityOperator(clang::ASTContext &ctx, clang::QualType lhs, - clang::QualType rhs) { +const clang::FunctionDecl * +FindComparisonOperator(clang::ASTContext &ctx, clang::QualType lhs, + clang::QualType rhs, clang::OverloadedOperatorKind op) { auto same = [](clang::QualType type, clang::QualType expected) { return type.getNonReferenceType().getUnqualifiedType().getCanonicalType() == expected; }; - auto is_equality = [&](const clang::NamedDecl *found) { + auto as_operator = + [&](const clang::NamedDecl *found) -> const clang::FunctionDecl * { auto *fn = clang::dyn_cast_or_null(found); - if (!fn || fn->getOverloadedOperator() != clang::OO_EqualEqual || - fn->isDeleted()) { - return false; + if (!fn || fn->getOverloadedOperator() != op || fn->isDeleted()) { + return nullptr; } if (auto *method = clang::dyn_cast(fn)) { return method->isInstance() && method->getNumParams() == 1 && - same(ctx.getCanonicalTagType(method->getParent()), lhs) && - same(method->getParamDecl(0)->getType(), rhs); + same(ctx.getCanonicalTagType(method->getParent()), lhs) && + same(method->getParamDecl(0)->getType(), rhs) + ? fn + : nullptr; } return fn->getNumParams() == 2 && - same(fn->getParamDecl(0)->getType(), lhs) && - same(fn->getParamDecl(1)->getType(), rhs); + same(fn->getParamDecl(0)->getType(), lhs) && + same(fn->getParamDecl(1)->getType(), rhs) + ? fn + : nullptr; }; - auto name = ctx.DeclarationNames.getCXXOperatorName(clang::OO_EqualEqual); + auto name = ctx.DeclarationNames.getCXXOperatorName(op); for (auto type : {lhs, rhs}) { auto *record = type->getAsCXXRecordDecl(); if (!record || !record->hasDefinition()) { continue; } - if (llvm::any_of(record->methods(), is_equality) || - llvm::any_of(record->getDeclContext()->lookup(name), is_equality) || - llvm::any_of(record->friends(), [&](const clang::FriendDecl *decl) { - return is_equality(decl->getFriendDecl()); - })) { - return true; + for (auto *method : record->methods()) { + if (auto *fn = as_operator(method)) { + return fn; + } + } + for (auto *found : record->getDeclContext()->lookup(name)) { + if (auto *fn = as_operator(found)) { + return fn; + } + } + for (auto *friend_decl : record->friends()) { + if (auto *fn = as_operator(friend_decl->getFriendDecl())) { + return fn; + } } } - return false; + return nullptr; +} + +const RecordTrait & +GetPartiallyOrderedWithTrait(clang::OverloadedOperatorKind op) { + switch (op) { + case clang::OO_Less: + return kPartiallyOrderedWith; + case clang::OO_Greater: + return kPartiallyOrderedWithGreater; + case clang::OO_LessEqual: + return kPartiallyOrderedWithLessEqual; + case clang::OO_GreaterEqual: + return kPartiallyOrderedWithGreaterEqual; + default: + llvm_unreachable("not a relational operator"); + } +} + +bool IsPartiallyOrderedWithTrait(const RecordTrait &trait) { + return &trait == &kPartiallyOrderedWith || + &trait == &kPartiallyOrderedWithGreater || + &trait == &kPartiallyOrderedWithLessEqual || + &trait == &kPartiallyOrderedWithGreaterEqual; +} + +bool HasDeferredComparisonImpl(const RecordTrait &trait) { + return IsEqualityComparableTrait(trait) || IsPartiallyOrderedWithTrait(trait); +} + +bool IsPartiallyOrdered(clang::ASTContext &ctx, + const clang::CXXRecordDecl *record) { + clang::QualType type = ctx.getCanonicalTagType(record); + auto *cmp = FindComparisonOperator(ctx, type, type, clang::OO_Spaceship); + if (cmp && ctx.CompCategories.lookupInfoForType(cmp->getReturnType())) { + return true; + } + return llvm::all_of(std::array{clang::OO_Less, clang::OO_Greater, + clang::OO_LessEqual, clang::OO_GreaterEqual}, + [&](clang::OverloadedOperatorKind op) { + return FindComparisonOperator(ctx, type, type, op); + }); +} + +static bool TakesConstReferenceOperands(const clang::FunctionDecl *fn) { + if (auto *method = clang::dyn_cast(fn); + method && (method->isVirtual() || !method->isConst() || + !method->isImplicitObjectMemberFunction() || + method->getRefQualifier() == clang::RQ_RValue)) { + return false; + } + return llvm::all_of(fn->parameters(), [](const clang::ParmVarDecl *param) { + auto type = param->getType(); + return type->isLValueReferenceType() && + type.getNonReferenceType().isConstQualified(); + }); +} + +const clang::FunctionDecl * +GetDerivingThreeWayOperator(clang::ASTContext &ctx, + const clang::CXXRecordDecl *record) { + clang::QualType type = ctx.getCanonicalTagType(record); + auto *cmp = FindComparisonOperator(ctx, type, type, clang::OO_Spaceship); + if (!cmp || !ctx.CompCategories.lookupInfoForType(cmp->getReturnType()) || + !TakesConstReferenceOperands(cmp)) { + return nullptr; + } + for (auto op : + {clang::OO_Greater, clang::OO_LessEqual, clang::OO_GreaterEqual}) { + if (FindComparisonOperator(ctx, type, type, op)) { + return nullptr; + } + } + return cmp; } const RecordTrait *GetComparisonTrait(const clang::FunctionDecl *fn) { auto op = fn->getOverloadedOperator(); - if (op != clang::OO_EqualEqual && op != clang::OO_ExclaimEqual) { + bool is_relational = op == clang::OO_Greater || op == clang::OO_LessEqual || + op == clang::OO_GreaterEqual; + bool is_three_way = op == clang::OO_Spaceship; + if (op != clang::OO_EqualEqual && op != clang::OO_ExclaimEqual && + op != clang::OO_Less && !is_relational && !is_three_way) { return nullptr; } auto *record = GetComparisonRecord(fn); if (!record || !IsUserDefinedDecl(record) || fn->isDeleted() || - fn->getDescribedFunctionTemplate() || - !fn->getReturnType()->isBooleanType()) { + fn->getDescribedFunctionTemplate()) { + return nullptr; + } + auto &ctx = fn->getASTContext(); + if (is_three_way ? !ctx.CompCategories.lookupInfoForType(fn->getReturnType()) + : !fn->getReturnType()->isBooleanType()) { return nullptr; } if (auto *method = clang::dyn_cast(fn); method ? method->getNumParams() != 1 : fn->getNumParams() != 2) { return nullptr; } - if (auto *method = clang::dyn_cast(fn); - method && (method->isVirtual() || !method->isConst() || - !method->isImplicitObjectMemberFunction() || - method->getRefQualifier() == clang::RQ_RValue)) { + if (!TakesConstReferenceOperands(fn)) { return nullptr; } - for (auto *param : fn->parameters()) { - auto type = param->getType(); - if (!type->isLValueReferenceType() || - !type.getNonReferenceType().isConstQualified()) { - return nullptr; - } + if (op == clang::OO_Less) { + return IsSameTypeComparison(fn, record) ? &kLessThanComparable : nullptr; + } + if (is_three_way) { + clang::QualType type = ctx.getCanonicalTagType(record); + return IsSameTypeComparison(fn, record) && + FindComparisonOperator(ctx, type, type, clang::OO_EqualEqual) + ? &kThreeWayComparable + : nullptr; + } + if (is_relational) { + return IsSameTypeComparison(fn, record) && IsPartiallyOrdered(ctx, record) + ? &GetPartiallyOrderedWithTrait(op) + : nullptr; } if (op == clang::OO_EqualEqual) { return &kWeaklyEqualityComparableWith; } - auto &ctx = fn->getASTContext(); - return HasEqualityOperator(ctx, ctx.getCanonicalTagType(record), - GetComparisonRhsType(fn)) + return FindComparisonOperator(ctx, ctx.getCanonicalTagType(record), + GetComparisonRhsType(fn), clang::OO_EqualEqual) ? &kWeaklyEqualityComparableWithNotEqual : nullptr; } +bool IsEqualityComparableTrait(const RecordTrait &trait) { + return &trait == &kWeaklyEqualityComparableWith || + &trait == &kWeaklyEqualityComparableWithNotEqual; +} + const RecordTrait *GetRecordTrait(const clang::CXXMethodDecl *method) { method = method->getCanonicalDecl(); if (auto *trait = GetComparisonTrait(method)) { diff --git a/cpp2rust/converter/converter_lib.h b/cpp2rust/converter/converter_lib.h index 8570fcd042..4a1feb55d1 100644 --- a/cpp2rust/converter/converter_lib.h +++ b/cpp2rust/converter/converter_lib.h @@ -176,6 +176,47 @@ inline constexpr RecordTrait kWeaklyEqualityComparableWithNotEqual{ "fn not_equal(this: Ptr, _: Ptr) -> bool", "this.clone()"}, {"WeaklyEqualityComparableWithUnsafe", "unsafe fn not_equal(this: *const Self, _: *const Self) -> bool", "this"}}; +inline constexpr RecordTrait kLessThanComparable{ + "less", + {"LessThanComparable", "fn less(this: Ptr, _: Ptr) -> bool", + "this.clone()"}, + {"LessThanComparableUnsafe", + "unsafe fn less(this: *const Self, _: *const Self) -> bool", "this"}}; +inline constexpr RecordTrait kPartiallyOrderedWith{ + "less", + {"PartiallyOrderedWith", "fn less(this: Ptr, _: Ptr) -> bool", + "this.clone()"}, + {"PartiallyOrderedWithUnsafe", + "unsafe fn less(this: *const Self, _: *const Self) -> bool", "this"}}; +inline constexpr RecordTrait kPartiallyOrderedWithGreater{ + "greater", + {"PartiallyOrderedWith", + "fn greater(this: Ptr, _: Ptr) -> bool", "this.clone()"}, + {"PartiallyOrderedWithUnsafe", + "unsafe fn greater(this: *const Self, _: *const Self) -> bool", "this"}}; +inline constexpr RecordTrait kPartiallyOrderedWithLessEqual{ + "less_equal", + {"PartiallyOrderedWith", + "fn less_equal(this: Ptr, _: Ptr) -> bool", "this.clone()"}, + {"PartiallyOrderedWithUnsafe", + "unsafe fn less_equal(this: *const Self, _: *const Self) -> bool", + "this"}}; +inline constexpr RecordTrait kPartiallyOrderedWithGreaterEqual{ + "greater_equal", + {"PartiallyOrderedWith", + "fn greater_equal(this: Ptr, _: Ptr) -> bool", "this.clone()"}, + {"PartiallyOrderedWithUnsafe", + "unsafe fn greater_equal(this: *const Self, _: *const Self) -> bool", + "this"}}; +inline constexpr RecordTrait kThreeWayComparable{ + "three_way", + {"ThreeWayComparable", + "fn three_way(this: Ptr, _: Ptr) -> Self::Category", + "this.clone()"}, + {"ThreeWayComparableUnsafe", + "unsafe fn three_way(this: *const Self, _: *const Self) -> " + "Self::Category", + "this"}}; inline constexpr RecordTrait kTriviallyDefaultConstructible{ nullptr, {"TriviallyDefaultConstructible"}, @@ -194,10 +235,27 @@ const RecordTrait *GetRecordTrait(const clang::CXXMethodDecl *method); const RecordTrait *GetComparisonTrait(const clang::FunctionDecl *fn); +bool IsEqualityComparableTrait(const RecordTrait &trait); + +bool IsPartiallyOrderedWithTrait(const RecordTrait &trait); + +bool HasDeferredComparisonImpl(const RecordTrait &trait); + +const RecordTrait & +GetPartiallyOrderedWithTrait(clang::OverloadedOperatorKind op); + +bool IsPartiallyOrdered(clang::ASTContext &ctx, + const clang::CXXRecordDecl *record); + +const clang::FunctionDecl * +GetDerivingThreeWayOperator(clang::ASTContext &ctx, + const clang::CXXRecordDecl *record); + clang::QualType GetComparisonRhsType(const clang::FunctionDecl *fn); -bool HasEqualityOperator(clang::ASTContext &ctx, clang::QualType lhs, - clang::QualType rhs); +const clang::FunctionDecl * +FindComparisonOperator(clang::ASTContext &ctx, clang::QualType lhs, + clang::QualType rhs, clang::OverloadedOperatorKind op); const clang::CXXRecordDecl *GetComparisonRecord(const clang::FunctionDecl *fn); diff --git a/cpp2rust/converter/models/converter_refcount.cpp b/cpp2rust/converter/models/converter_refcount.cpp index 9ab287222a..a00932712d 100644 --- a/cpp2rust/converter/models/converter_refcount.cpp +++ b/cpp2rust/converter/models/converter_refcount.cpp @@ -664,30 +664,6 @@ ConverterRefCount::GetComparisonReceiver(const clang::CXXMethodDecl *, return std::format("{}.clone()", lhs); } -// Records implement the comparisons of the standard library (CppLess and -// WeaklyEqualityComparableWith) with their comparison operators, which get -// pointers to the values. They don't implement Ord and PartialEq, which get -// references to the values, as passing those to the operators would require -// copying the values. -void ConverterRefCount::ConvertOrdAndPartialOrdTraits( - const clang::CXXRecordDecl *decl, const clang::FunctionDecl *eq, - const clang::FunctionDecl *lt, const clang::FunctionDecl *cmp) { - auto record_name = GetRecordName(decl); - if (auto *op = lt ? lt : cmp) { - auto [lhs, rhs] = GetComparisonOperandNames(op); - StrCat(std::format( - "impl CppLess for {} {{ fn less({}: Ptr, {}: Ptr) -> bool " - "{{ {} }} }}", - record_name, lhs, rhs, - lt ? GetComparisonBool(lt, decl, lhs, rhs) - : std::format("{} == std::cmp::Ordering::Less", - GetComparisonCall(cmp, decl, lhs, rhs)))); - } - if (eq && !GetComparisonTrait(eq)) { - ConvertEqualityComparableImpl(decl, eq); - } -} - std::string ConverterRefCount::GetComparisonTraitSignature( const RecordTrait &trait, std::string_view lhs, std::string_view rhs, std::string_view rhs_type) const { diff --git a/cpp2rust/converter/models/converter_refcount.h b/cpp2rust/converter/models/converter_refcount.h index d4446f434a..0b791f3fe4 100644 --- a/cpp2rust/converter/models/converter_refcount.h +++ b/cpp2rust/converter/models/converter_refcount.h @@ -53,11 +53,6 @@ class ConverterRefCount final : public Converter { std::string GetComparisonReceiver(const clang::CXXMethodDecl *method, std::string_view lhs) override; - void ConvertOrdAndPartialOrdTraits(const clang::CXXRecordDecl *decl, - const clang::FunctionDecl *eq, - const clang::FunctionDecl *lt, - const clang::FunctionDecl *cmp) override; - std::string GetComparisonTraitSignature(const RecordTrait &trait, std::string_view lhs, std::string_view rhs, diff --git a/libcc2rs-macros/src/lib.rs b/libcc2rs-macros/src/lib.rs index 3b59de2c8b..6242e09e3c 100644 --- a/libcc2rs-macros/src/lib.rs +++ b/libcc2rs-macros/src/lib.rs @@ -12,6 +12,7 @@ mod fn_ptr_arg; mod goto; mod initialization; mod lambda; +mod partially_ordered; mod record; mod state_machine; mod switch; @@ -354,6 +355,16 @@ pub fn derive_destructible_unsafe(input: TokenStream) -> TokenStream { // // Encodes the C++ converting construction and conversion operators in rules. +#[proc_macro_derive(PartiallyOrderedWith, attributes(three_way))] +pub fn derive_partially_ordered_with(input: TokenStream) -> TokenStream { + partially_ordered::expand(input, false) +} + +#[proc_macro_derive(PartiallyOrderedWithUnsafe, attributes(three_way))] +pub fn derive_partially_ordered_with_unsafe(input: TokenStream) -> TokenStream { + partially_ordered::expand(input, true) +} + #[proc_macro_derive(CppFrom, attributes(cpp_from, cpp_from_move, cpp_into))] pub fn derive_cpp_from(input: TokenStream) -> TokenStream { cpp_from::expand(input, false) diff --git a/libcc2rs-macros/src/partially_ordered.rs b/libcc2rs-macros/src/partially_ordered.rs new file mode 100644 index 0000000000..0028da1311 --- /dev/null +++ b/libcc2rs-macros/src/partially_ordered.rs @@ -0,0 +1,58 @@ +// Copyright (c) 2022-present INESC-ID. +// Distributed under the MIT license that can be found in the LICENSE file. + +use proc_macro::TokenStream; +use quote::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 three_way: syn::Path = input + .attrs + .iter() + .find(|attr| attr.path().is_ident("three_way")) + .expect("three_way(path) names the operator<=> of the type") + .parse_args() + .expect("three_way(path)"); + let relations = [ + (quote!(greater), quote!(is_gt)), + (quote!(less_equal), quote!(is_lteq)), + (quote!(greater_equal), quote!(is_gteq)), + ]; + if is_unsafe { + let methods = relations.iter().map(|(method, relation)| { + quote! { + unsafe fn #method(this: *const Self, rhs: *const Self) -> bool { + ::libcc2rs::#relation(unsafe { #three_way(this, rhs) }) + } + } + }); + return quote! { + impl #impl_generics ::libcc2rs::PartiallyOrderedWithUnsafe for #name #ty_generics #where_clause { + unsafe fn less(this: *const Self, rhs: *const Self) -> bool { + unsafe { ::less(this, rhs) } + } + #(#methods)* + } + } + .into(); + } + let methods = relations.iter().map(|(method, relation)| { + quote! { + fn #method(this: ::libcc2rs::Ptr, rhs: ::libcc2rs::Ptr) -> bool { + ::libcc2rs::#relation(#three_way(this, rhs)) + } + } + }); + quote! { + impl #impl_generics ::libcc2rs::PartiallyOrderedWith for #name #ty_generics #where_clause { + fn less(this: ::libcc2rs::Ptr, rhs: ::libcc2rs::Ptr) -> bool { + ::less(this, rhs) + } + #(#methods)* + } + } + .into() +} diff --git a/libcc2rs/src/cmp.rs b/libcc2rs/src/cmp.rs index 92c6b8d0d4..c6875abcf7 100644 --- a/libcc2rs/src/cmp.rs +++ b/libcc2rs/src/cmp.rs @@ -9,9 +9,14 @@ use std::cmp::Ordering; use crate::rc::{AsPointer, Ptr, Value}; use crate::reinterpret::ByteRepr; -// operator<. Records implement it with their comparison operators. -pub trait CppLess: Sized { - fn less(lhs: Ptr, rhs: Ptr) -> bool; +pub trait LessThanComparable: Sized { + fn less(this: Ptr, rhs: Ptr) -> bool; +} + +pub trait LessThanComparableUnsafe: Sized { + /// # Safety + /// `this` and `rhs` must point to live values. + unsafe fn less(this: *const Self, rhs: *const Self) -> bool; } // TODO: equal and not_equal return a boolean-testable type in C++ @@ -73,9 +78,138 @@ where { } -impl CppLess for T { - fn less(lhs: Ptr, rhs: Ptr) -> bool { - lhs.with(|a| rhs.with(|b| a < b)) +macro_rules! impl_less_than_comparable { + ($(<$($param:ident),*> $ty:ty),* $(,)?) => {$( + impl<$($param),*> $crate::LessThanComparable for $ty + where + $ty: PartialOrd + $crate::ByteRepr, + { + fn less(this: $crate::Ptr, rhs: $crate::Ptr) -> bool { + this.with(|a| rhs.with(|b| a < b)) + } + } + )*}; +} + +macro_rules! impl_less_than_comparable_unsafe { + ($(<$($param:ident),*> $ty:ty),* $(,)?) => {$( + impl<$($param),*> $crate::LessThanComparableUnsafe for $ty + where + $ty: PartialOrd, + { + unsafe fn less(this: *const Self, rhs: *const Self) -> bool { + unsafe { *this < *rhs } + } + } + )*}; +} + +impl_less_than_comparable!( + <> bool, <> i8, <> i16, <> i32, <> i64, <> i128, <> isize, + <> u8, <> u16, <> u32, <> u64, <> u128, <> usize, <> f32, <> f64, + Ptr, +); + +impl_less_than_comparable_unsafe!( + <> bool, <> i8, <> i16, <> i32, <> i64, <> i128, <> isize, + <> u8, <> u16, <> u32, <> u64, <> u128, <> usize, <> f32, <> f64, + *mut T, *const T, +); + +impl LessThanComparable for Vec { + fn less(this: Ptr, rhs: Ptr) -> bool { + let (m, n) = (this.with(Vec::len), rhs.with(Vec::len)); + let (a, b) = (this.decay(), rhs.decay()); + for i in 0..m.min(n) { + if T::less(a.offset(i), b.offset(i)) { + return true; + } + if T::less(b.offset(i), a.offset(i)) { + return false; + } + } + m < n + } +} + +impl LessThanComparable for Box<[T]> { + fn less(this: Ptr, rhs: Ptr) -> bool { + let (m, n) = ( + this.with(|elems| elems.len()), + rhs.with(|elems| elems.len()), + ); + let (a, b) = (this.decay(), rhs.decay()); + for i in 0..m.min(n) { + if T::less(a.offset(i), b.offset(i)) { + return true; + } + if T::less(b.offset(i), a.offset(i)) { + return false; + } + } + m < n + } +} + +impl LessThanComparable for (Value, Value) +where + Self: ByteRepr, +{ + fn less(this: Ptr, rhs: Ptr) -> bool { + let pointers = |pair: &Self| { + ( + AsPointer::::as_pointer(&pair.0), + AsPointer::::as_pointer(&pair.1), + ) + }; + let (a0, a1) = this.with(pointers); + let (b0, b1) = rhs.with(pointers); + A::less(a0.clone(), b0.clone()) || (!A::less(b0, a0) && B::less(a1, b1)) + } +} + +impl LessThanComparableUnsafe for Vec { + unsafe fn less(this: *const Self, rhs: *const Self) -> bool { + unsafe { + let (m, n) = ((*this).len(), (*rhs).len()); + let (a, b) = ((*this).as_ptr(), (*rhs).as_ptr()); + for i in 0..m.min(n) { + if T::less(a.add(i), b.add(i)) { + return true; + } + if T::less(b.add(i), a.add(i)) { + return false; + } + } + m < n + } + } +} + +impl LessThanComparableUnsafe for [T; N] { + unsafe fn less(this: *const Self, rhs: *const Self) -> bool { + let (a, b) = (this as *const T, rhs as *const T); + for i in 0..N { + unsafe { + if T::less(a.add(i), b.add(i)) { + return true; + } + if T::less(b.add(i), a.add(i)) { + return false; + } + } + } + false + } +} + +impl LessThanComparableUnsafe for (A, B) { + unsafe fn less(this: *const Self, rhs: *const Self) -> bool { + unsafe { + A::less(&raw const (*this).0, &raw const (*rhs).0) + || (!A::less(&raw const (*rhs).0, &raw const (*this).0) + && B::less(&raw const (*this).1, &raw const (*rhs).1)) + } } } @@ -91,7 +225,6 @@ macro_rules! impl_weakly_equality_comparable_with { } )*}; } -pub(crate) use impl_weakly_equality_comparable_with; macro_rules! impl_weakly_equality_comparable_with_unsafe { ($(<$($param:ident $(: $bound:path)?),*> $ty:ty),* $(,)?) => {$( @@ -105,7 +238,6 @@ macro_rules! impl_weakly_equality_comparable_with_unsafe { } )*}; } -pub(crate) use impl_weakly_equality_comparable_with_unsafe; impl_weakly_equality_comparable_with!( <> bool, <> i8, <> i16, <> i32, <> i64, <> i128, <> isize, @@ -180,8 +312,235 @@ impl WeaklyEqualityCom } } +pub trait PartiallyOrderedWith = Self>: Sized { + fn less(this: Ptr, rhs: Ptr) -> bool; + fn greater(this: Ptr, rhs: Ptr) -> bool; + fn less_equal(this: Ptr, rhs: Ptr) -> bool; + fn greater_equal(this: Ptr, rhs: Ptr) -> bool; +} + +pub trait PartiallyOrderedWithUnsafe = Self>: Sized { + /// # Safety + /// `this` and `rhs` must point to live values. + unsafe fn less(this: *const Self, rhs: *const U) -> bool; + /// # Safety + /// `this` and `rhs` must point to live values. + unsafe fn greater(this: *const Self, rhs: *const U) -> bool; + /// # Safety + /// `this` and `rhs` must point to live values. + unsafe fn less_equal(this: *const Self, rhs: *const U) -> bool; + /// # Safety + /// `this` and `rhs` must point to live values. + unsafe fn greater_equal(this: *const Self, rhs: *const U) -> bool; +} + +pub trait TotallyOrdered: EqualityComparable + PartiallyOrderedWith {} + +impl> TotallyOrdered for T {} + +pub trait TotallyOrderedUnsafe: + EqualityComparableUnsafe + PartiallyOrderedWithUnsafe +{ +} + +impl> TotallyOrderedUnsafe for T {} + +pub trait TotallyOrderedWith< + U: TotallyOrdered + WeaklyEqualityComparableWith + PartiallyOrderedWith, +>: TotallyOrdered + EqualityComparableWith + PartiallyOrderedWith +{ +} + +impl TotallyOrderedWith for T +where + T: TotallyOrdered + EqualityComparableWith + PartiallyOrderedWith, + U: TotallyOrdered + WeaklyEqualityComparableWith + PartiallyOrderedWith, +{ +} + +pub trait TotallyOrderedWithUnsafe< + U: TotallyOrderedUnsafe + + WeaklyEqualityComparableWithUnsafe + + PartiallyOrderedWithUnsafe, +>: TotallyOrderedUnsafe + EqualityComparableWithUnsafe + PartiallyOrderedWithUnsafe +{ +} + +impl TotallyOrderedWithUnsafe for T +where + T: TotallyOrderedUnsafe + EqualityComparableWithUnsafe + PartiallyOrderedWithUnsafe, + U: TotallyOrderedUnsafe + WeaklyEqualityComparableWithUnsafe + PartiallyOrderedWithUnsafe, +{ +} + +macro_rules! impl_partially_ordered_with { + ($(<$($param:ident),*> $ty:ty),* $(,)?) => {$( + impl<$($param),*> $crate::PartiallyOrderedWith for $ty + where + $ty: PartialOrd + $crate::ByteRepr, + { + fn less(this: $crate::Ptr, rhs: $crate::Ptr) -> bool { + this.with(|a| rhs.with(|b| a < b)) + } + fn greater(this: $crate::Ptr, rhs: $crate::Ptr) -> bool { + this.with(|a| rhs.with(|b| a > b)) + } + fn less_equal(this: $crate::Ptr, rhs: $crate::Ptr) -> bool { + this.with(|a| rhs.with(|b| a <= b)) + } + fn greater_equal(this: $crate::Ptr, rhs: $crate::Ptr) -> bool { + this.with(|a| rhs.with(|b| a >= b)) + } + } + )*}; +} + +macro_rules! impl_partially_ordered_with_unsafe { + ($(<$($param:ident),*> $ty:ty),* $(,)?) => {$( + impl<$($param),*> $crate::PartiallyOrderedWithUnsafe for $ty + where + $ty: PartialOrd, + { + unsafe fn less(this: *const Self, rhs: *const Self) -> bool { + unsafe { *this < *rhs } + } + unsafe fn greater(this: *const Self, rhs: *const Self) -> bool { + unsafe { *this > *rhs } + } + unsafe fn less_equal(this: *const Self, rhs: *const Self) -> bool { + unsafe { *this <= *rhs } + } + unsafe fn greater_equal(this: *const Self, rhs: *const Self) -> bool { + unsafe { *this >= *rhs } + } + } + )*}; +} + +impl_partially_ordered_with!( + <> bool, <> i8, <> i16, <> i32, <> i64, <> i128, <> isize, + <> u8, <> u16, <> u32, <> u64, <> u128, <> usize, <> f32, <> f64, + Ptr, +); + +impl_partially_ordered_with_unsafe!( + <> bool, <> i8, <> i16, <> i32, <> i64, <> i128, <> isize, + <> u8, <> u16, <> u32, <> u64, <> u128, <> usize, <> f32, <> f64, + *mut T, *const T, +); + +pub trait ThreeWayComparable: EqualityComparable + PartiallyOrderedWith { + type Category: Into; + + fn three_way(this: Ptr, rhs: Ptr) -> Self::Category; +} + +pub trait ThreeWayComparableUnsafe: + EqualityComparableUnsafe + PartiallyOrderedWithUnsafe +{ + type Category: Into; + + /// # Safety + /// `this` and `rhs` must point to live values. + unsafe fn three_way(this: *const Self, rhs: *const Self) -> Self::Category; +} + +pub trait ThreeWayComparableWith< + U: ThreeWayComparable + + ThreeWayComparableWith + + WeaklyEqualityComparableWith + + PartiallyOrderedWith, +>: ThreeWayComparable + WeaklyEqualityComparableWith + PartiallyOrderedWith +{ + type CategoryWith: Into; + + fn three_way_with(this: Ptr, rhs: Ptr) -> Self::CategoryWith; +} + +pub trait ThreeWayComparableWithUnsafe< + U: ThreeWayComparableUnsafe + + ThreeWayComparableWithUnsafe + + WeaklyEqualityComparableWithUnsafe + + PartiallyOrderedWithUnsafe, +>: + ThreeWayComparableUnsafe + WeaklyEqualityComparableWithUnsafe + PartiallyOrderedWithUnsafe +{ + type CategoryWith: Into; + + /// # Safety + /// `this` and `rhs` must point to live values. + unsafe fn three_way_with(this: *const Self, rhs: *const U) -> Self::CategoryWith; +} + +impl ThreeWayComparableWith for T { + type CategoryWith = T::Category; + + fn three_way_with(this: Ptr, rhs: Ptr) -> Self::CategoryWith { + T::three_way(this, rhs) + } +} + +impl ThreeWayComparableWithUnsafe for T { + type CategoryWith = T::Category; + + unsafe fn three_way_with(this: *const Self, rhs: *const Self) -> Self::CategoryWith { + unsafe { T::three_way(this, rhs) } + } +} + +macro_rules! impl_three_way_comparable { + ($category:ident, $order:ident :: $compare:ident, $(<$($param:ident),*> $ty:ty),* $(,)?) => {$( + impl<$($param),*> $crate::ThreeWayComparable for $ty + where + $ty: $order + + $crate::ByteRepr + + $crate::EqualityComparable + + $crate::PartiallyOrderedWith, + { + type Category = $crate::$category; + + fn three_way(this: $crate::Ptr, rhs: $crate::Ptr) -> Self::Category { + this.with(|a| rhs.with(|b| $crate::$category::from($order::$compare(a, b)))) + } + } + )*}; +} + +macro_rules! impl_three_way_comparable_unsafe { + ($category:ident, $order:ident :: $compare:ident, $(<$($param:ident),*> $ty:ty),* $(,)?) => {$( + impl<$($param),*> $crate::ThreeWayComparableUnsafe for $ty + where + $ty: $order, + { + type Category = $crate::$category; + + unsafe fn three_way(this: *const Self, rhs: *const Self) -> Self::Category { + unsafe { $crate::$category::from($order::$compare(&*this, &*rhs)) } + } + } + )*}; +} + +impl_three_way_comparable!( + StrongOrdering, Ord::cmp, + <> bool, <> i8, <> i16, <> i32, <> i64, <> i128, <> isize, + <> u8, <> u16, <> u32, <> u64, <> u128, <> usize, + Ptr, +); + +impl_three_way_comparable!(PartialOrdering, PartialOrd::partial_cmp, <> f32, <> f64); + +impl_three_way_comparable_unsafe!( + StrongOrdering, Ord::cmp, + <> bool, <> i8, <> i16, <> i32, <> i64, <> i128, <> isize, + <> u8, <> u16, <> u32, <> u64, <> u128, <> usize, + *mut T, *const T, +); + +impl_three_way_comparable_unsafe!(PartialOrdering, PartialOrd::partial_cmp, <> f32, <> f64); + // The order of two values that std::sort and std::map derive from operator<. -pub fn cpp_cmp(lhs: &Ptr, rhs: &Ptr) -> Ordering { +pub fn cpp_cmp(lhs: &Ptr, rhs: &Ptr) -> Ordering { if T::less(lhs.clone(), rhs.clone()) { Ordering::Less } else if T::less(rhs.clone(), lhs.clone()) { diff --git a/libcc2rs/src/constructor.rs b/libcc2rs/src/constructor.rs index 1cbedde0c1..308212102e 100644 --- a/libcc2rs/src/constructor.rs +++ b/libcc2rs/src/constructor.rs @@ -649,7 +649,6 @@ impl_trivial_copy_move!( usize, f32, f64, - std::cmp::Ordering, AnyPtr, crate::VaArg, std::rc::Rc, diff --git a/libcc2rs/src/initialization.rs b/libcc2rs/src/initialization.rs index e8a9cc87c2..a6eb1f4e2c 100644 --- a/libcc2rs/src/initialization.rs +++ b/libcc2rs/src/initialization.rs @@ -48,13 +48,6 @@ impl_zero_init!( 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 { diff --git a/libcc2rs/src/lib.rs b/libcc2rs/src/lib.rs index 607b2280fa..f6555fb360 100644 --- a/libcc2rs/src/lib.rs +++ b/libcc2rs/src/lib.rs @@ -134,9 +134,9 @@ pub use libcc2rs_macros::{ Copyable, CopyableUnsafe, CppFrom, CppFromUnsafe, DefaultConstructible, DefaultConstructibleUnsafe, DefaultInitializable, DefaultInitializableUnsafe, Destructible, DestructibleUnsafe, FnPtrArg, Memberwise, MemberwiseUnsafe, Movable, MovableUnsafe, - MoveAssignable, MoveAssignableUnsafe, MoveConstructible, MoveConstructibleUnsafe, Record, - Swappable, SwappableUnsafe, TriviallyCopyable, TriviallyCopyableUnsafe, - TriviallyDefaultConstructible, TriviallyDefaultConstructibleUnsafe, VaArg, ZeroInitialization, - ZeroInitializationUnsafe, assign, assign_unsafe, goto, goto_block, lambda, lambda_unsafe, - switch, + MoveAssignable, MoveAssignableUnsafe, MoveConstructible, MoveConstructibleUnsafe, + PartiallyOrderedWith, PartiallyOrderedWithUnsafe, Record, Swappable, SwappableUnsafe, + TriviallyCopyable, TriviallyCopyableUnsafe, TriviallyDefaultConstructible, + TriviallyDefaultConstructibleUnsafe, VaArg, ZeroInitialization, ZeroInitializationUnsafe, + assign, assign_unsafe, goto, goto_block, lambda, lambda_unsafe, switch, }; diff --git a/rules/algorithm/tgt_refcount.rs b/rules/algorithm/tgt_refcount.rs index 1df7cc903f..f12026761c 100644 --- a/rules/algorithm/tgt_refcount.rs +++ b/rules/algorithm/tgt_refcount.rs @@ -6,7 +6,7 @@ use std::cell::RefCell; use std::io::Write; use std::rc::Rc; -fn f1, T2: CppLess + Clone + ByteRepr>(a0: T1, a1: T1) { +fn f1, T2: LessThanComparable + Clone + ByteRepr>(a0: T1, a1: T1) { let __n = a1.iter_distance(&a0); let mut __order: Vec = (0..__n).collect(); __order.sort_by(|&__x, &__y| cpp_cmp(&a0.iter_at(__x), &a0.iter_at(__y))); @@ -65,14 +65,14 @@ where reorder(__order, |__i| a0.iter_at(__i)); } -fn f8, T2: CppLess>(a0: T1, a1: T1) -> T1 { +fn f8, T2: LessThanComparable>(a0: T1, a1: T1) -> T1 { let __it: Value = Rc::new(RefCell::new(a0)); let __last: Value = Rc::new(RefCell::new(a1)); let mut __best = __it.borrow().clone(); if ::not_equal(__it.as_pointer(), __last.as_pointer()) { __it.borrow_mut().iter_inc(); while ::not_equal(__it.as_pointer(), __last.as_pointer()) { - if CppLess::less(__best.iter_deref(), __it.borrow().iter_deref()) { + if LessThanComparable::less(__best.iter_deref(), __it.borrow().iter_deref()) { __best = __it.borrow().clone(); } __it.borrow_mut().iter_inc(); @@ -150,12 +150,12 @@ where reorder(__order, |__i| a0.iter_at(__i)); } -fn f16(a0: Ptr, a1: Ptr) -> Ptr { - if CppLess::less(a1.clone(), a0.clone()) { a1 } else { a0 } +fn f16(a0: Ptr, a1: Ptr) -> Ptr { + if LessThanComparable::less(a1.clone(), a0.clone()) { a1 } else { a0 } } -fn f17(a0: Ptr, a1: Ptr) -> Ptr { - if CppLess::less(a0.clone(), a1.clone()) { a1 } else { a0 } +fn f17(a0: Ptr, a1: Ptr) -> Ptr { + if LessThanComparable::less(a0.clone(), a1.clone()) { a1 } else { a0 } } fn f18, T2: Swappable>(a0: T1, a1: T1) { diff --git a/rules/algorithm/tgt_unsafe.rs b/rules/algorithm/tgt_unsafe.rs index 7a1531f4de..67d6dfcf75 100644 --- a/rules/algorithm/tgt_unsafe.rs +++ b/rules/algorithm/tgt_unsafe.rs @@ -4,10 +4,20 @@ use libcc2rs::*; use std::io::{Seek, Write}; -unsafe fn f1, T2: Ord>(a0: T1, a1: T1) { +unsafe fn f1, T2: LessThanComparableUnsafe>(a0: T1, a1: T1) { let __n = a1.iter_distance(&a0); let mut __order: Vec = (0..__n).collect(); - __order.sort_by(|&__x, &__y| (*a0.iter_at(__x)).cmp(&*a0.iter_at(__y))); + __order.sort_by(|&__x, &__y| { + let __a = a0.iter_at(__x) as *const _; + let __b = a0.iter_at(__y) as *const _; + if LessThanComparableUnsafe::less(__a, __b) { + std::cmp::Ordering::Less + } else if LessThanComparableUnsafe::less(__b, __a) { + std::cmp::Ordering::Greater + } else { + std::cmp::Ordering::Equal + } + }); let __values: Vec<_> = __order .iter() .map(|&__i| std::ptr::read(a0.iter_at(__i))) @@ -66,13 +76,13 @@ where } } -unsafe fn f8, T2: PartialOrd>(a0: T1, a1: T1) -> T1 { +unsafe fn f8, T2: LessThanComparableUnsafe>(a0: T1, a1: T1) -> T1 { let mut __best = a0.clone(); if WeaklyEqualityComparableWithUnsafe::not_equal(&a0, &a1) { let mut __it = a0; __it.iter_inc(); while WeaklyEqualityComparableWithUnsafe::not_equal(&__it, &a1) { - if *__best.iter_deref() < *__it.iter_deref() { + if LessThanComparableUnsafe::less(__best.iter_deref() as *const _, __it.iter_deref() as *const _) { __best = __it.clone(); } __it.iter_inc(); @@ -130,7 +140,7 @@ unsafe fn f13>( __out.try_clone().unwrap() } -unsafe fn f14(a0: *mut T1, a1: *mut T1, a2: T2) +unsafe fn f14(a0: *mut T1, a1: *mut T1, a2: T2) where T2: Callable2, { @@ -146,16 +156,16 @@ where }) } -unsafe fn f16(a0: *const T1, a1: *const T1) -> *const T1 { +unsafe fn f16(a0: *const T1, a1: *const T1) -> *const T1 { let __a: *const _ = a0; let __b: *const _ = a1; - if *__b < *__a { __b } else { __a } + if LessThanComparableUnsafe::less(__b, __a) { __b } else { __a } } -unsafe fn f17(a0: *const T1, a1: *const T1) -> *const T1 { +unsafe fn f17(a0: *const T1, a1: *const T1) -> *const T1 { let __a: *const _ = a0; let __b: *const _ = a1; - if *__a < *__b { __b } else { __a } + if LessThanComparableUnsafe::less(__a, __b) { __b } else { __a } } unsafe fn f18, T2: SwappableUnsafe>(a0: T1, a1: T1) { diff --git a/rules/compare/shim.rs b/rules/compare/shim.rs index 39e3b88030..1ccdb7f688 100644 --- a/rules/compare/shim.rs +++ b/rules/compare/shim.rs @@ -5,44 +5,170 @@ use std::cell::RefCell; use std::cmp::Ordering; use std::rc::Rc; -use crate::{ArgRepr, AsPointer, ByteRepr, FnPtrArg, Ptr, Value, record_from_repr}; +use crate::{ + ArgRepr, AsPointer, ByteRepr, FnPtrArg, Ptr, Value, ZeroInitialization, record_from_repr, +}; -impl ByteRepr for std::cmp::Ordering {} +// The comparison categories of [cmp.categories]. Each holds -1, 0 or 1 for +// less, equal or equivalent, and greater; a partial_ordering also holds 2 for +// unordered. +macro_rules! comparison_category { + ($name:ident) => { + #[derive(Clone, Copy, Debug, Default)] + pub struct $name(i8); -impl FnPtrArg for std::cmp::Ordering { - #[inline] - fn to_repr(self) -> ArgRepr { - ArgRepr::Record(Box::new(self)) + impl ByteRepr for $name {} + + impl FnPtrArg for $name { + #[inline] + fn to_repr(self) -> ArgRepr { + ArgRepr::Record(Box::new(self)) + } + fn from_repr(r: ArgRepr) -> Self { + record_from_repr(r) + } + } + + impl ZeroInitialization for $name { + #[inline] + fn zero_init() -> Self { + Self(0) + } + } + }; +} + +comparison_category!(StrongOrdering); +comparison_category!(WeakOrdering); +comparison_category!(PartialOrdering); + +crate::constructor::impl_trivial_copy_move!(StrongOrdering, WeakOrdering, PartialOrdering,); + +impl From for WeakOrdering { + fn from(order: StrongOrdering) -> Self { + Self(order.0) } - fn from_repr(r: ArgRepr) -> Self { - record_from_repr(r) +} + +impl From for PartialOrdering { + fn from(order: StrongOrdering) -> Self { + Self(order.0) } } -pub static STRONG_ORDERING_LESS: Ordering = Ordering::Less; -pub static STRONG_ORDERING_EQUAL: Ordering = Ordering::Equal; -pub static STRONG_ORDERING_EQUIVALENT: Ordering = Ordering::Equal; -pub static STRONG_ORDERING_GREATER: Ordering = Ordering::Greater; +impl From for PartialOrdering { + fn from(order: WeakOrdering) -> Self { + Self(order.0) + } +} + +// The result of the built-in <=> on integers, enumerations and pointers. +impl From for StrongOrdering { + fn from(order: Ordering) -> Self { + Self(order as i8) + } +} + +// The result of the built-in <=> on floating-point values. +impl From> for PartialOrdering { + fn from(order: Option) -> Self { + Self(order.map_or(2, |order| order as i8)) + } +} + +// [compare.syn]: the named comparison functions. +pub fn is_eq(order: impl Into) -> bool { + order.into().0 == 0 +} + +pub fn is_neq(order: impl Into) -> bool { + order.into().0 != 0 +} + +pub fn is_lt(order: impl Into) -> bool { + order.into().0 == -1 +} + +pub fn is_lteq(order: impl Into) -> bool { + matches!(order.into().0, -1 | 0) +} + +pub fn is_gt(order: impl Into) -> bool { + order.into().0 == 1 +} + +pub fn is_gteq(order: impl Into) -> bool { + matches!(order.into().0, 0 | 1) +} + +pub static STRONG_ORDERING_LESS: StrongOrdering = StrongOrdering(-1); +pub static STRONG_ORDERING_EQUAL: StrongOrdering = StrongOrdering(0); +pub static STRONG_ORDERING_EQUIVALENT: StrongOrdering = StrongOrdering(0); +pub static STRONG_ORDERING_GREATER: StrongOrdering = StrongOrdering(1); + +pub static WEAK_ORDERING_LESS: WeakOrdering = WeakOrdering(-1); +pub static WEAK_ORDERING_EQUIVALENT: WeakOrdering = WeakOrdering(0); +pub static WEAK_ORDERING_GREATER: WeakOrdering = WeakOrdering(1); + +pub static PARTIAL_ORDERING_LESS: PartialOrdering = PartialOrdering(-1); +pub static PARTIAL_ORDERING_EQUIVALENT: PartialOrdering = PartialOrdering(0); +pub static PARTIAL_ORDERING_GREATER: PartialOrdering = PartialOrdering(1); +pub static PARTIAL_ORDERING_UNORDERED: PartialOrdering = PartialOrdering(2); thread_local! { - static STRONG_ORDERING_LESS_VALUE: Value = Rc::new(RefCell::new(Ordering::Less)); - static STRONG_ORDERING_EQUAL_VALUE: Value = Rc::new(RefCell::new(Ordering::Equal)); - static STRONG_ORDERING_EQUIVALENT_VALUE: Value = Rc::new(RefCell::new(Ordering::Equal)); - static STRONG_ORDERING_GREATER_VALUE: Value = Rc::new(RefCell::new(Ordering::Greater)); + static STRONG_ORDERING_LESS_VALUE: Value = Rc::new(RefCell::new(StrongOrdering(-1))); + static STRONG_ORDERING_EQUAL_VALUE: Value = Rc::new(RefCell::new(StrongOrdering(0))); + static STRONG_ORDERING_EQUIVALENT_VALUE: Value = Rc::new(RefCell::new(StrongOrdering(0))); + static STRONG_ORDERING_GREATER_VALUE: Value = Rc::new(RefCell::new(StrongOrdering(1))); + static WEAK_ORDERING_LESS_VALUE: Value = Rc::new(RefCell::new(WeakOrdering(-1))); + static WEAK_ORDERING_EQUIVALENT_VALUE: Value = Rc::new(RefCell::new(WeakOrdering(0))); + static WEAK_ORDERING_GREATER_VALUE: Value = Rc::new(RefCell::new(WeakOrdering(1))); + static PARTIAL_ORDERING_LESS_VALUE: Value = Rc::new(RefCell::new(PartialOrdering(-1))); + static PARTIAL_ORDERING_EQUIVALENT_VALUE: Value = Rc::new(RefCell::new(PartialOrdering(0))); + static PARTIAL_ORDERING_GREATER_VALUE: Value = Rc::new(RefCell::new(PartialOrdering(1))); + static PARTIAL_ORDERING_UNORDERED_VALUE: Value = Rc::new(RefCell::new(PartialOrdering(2))); } -pub fn strong_ordering_less() -> Ptr { +pub fn strong_ordering_less() -> Ptr { STRONG_ORDERING_LESS_VALUE.with(AsPointer::as_pointer) } -pub fn strong_ordering_equal() -> Ptr { +pub fn strong_ordering_equal() -> Ptr { STRONG_ORDERING_EQUAL_VALUE.with(AsPointer::as_pointer) } -pub fn strong_ordering_equivalent() -> Ptr { +pub fn strong_ordering_equivalent() -> Ptr { STRONG_ORDERING_EQUIVALENT_VALUE.with(AsPointer::as_pointer) } -pub fn strong_ordering_greater() -> Ptr { +pub fn strong_ordering_greater() -> Ptr { STRONG_ORDERING_GREATER_VALUE.with(AsPointer::as_pointer) } + +pub fn weak_ordering_less() -> Ptr { + WEAK_ORDERING_LESS_VALUE.with(AsPointer::as_pointer) +} + +pub fn weak_ordering_equivalent() -> Ptr { + WEAK_ORDERING_EQUIVALENT_VALUE.with(AsPointer::as_pointer) +} + +pub fn weak_ordering_greater() -> Ptr { + WEAK_ORDERING_GREATER_VALUE.with(AsPointer::as_pointer) +} + +pub fn partial_ordering_less() -> Ptr { + PARTIAL_ORDERING_LESS_VALUE.with(AsPointer::as_pointer) +} + +pub fn partial_ordering_equivalent() -> Ptr { + PARTIAL_ORDERING_EQUIVALENT_VALUE.with(AsPointer::as_pointer) +} + +pub fn partial_ordering_greater() -> Ptr { + PARTIAL_ORDERING_GREATER_VALUE.with(AsPointer::as_pointer) +} + +pub fn partial_ordering_unordered() -> Ptr { + PARTIAL_ORDERING_UNORDERED_VALUE.with(AsPointer::as_pointer) +} diff --git a/rules/compare/src_cxx20.cpp b/rules/compare/src_cxx20.cpp index ad2ac8175c..7aa8ba5a6a 100644 --- a/rules/compare/src_cxx20.cpp +++ b/rules/compare/src_cxx20.cpp @@ -5,6 +5,10 @@ typedef std::strong_ordering t1; +typedef std::weak_ordering t2; + +typedef std::partial_ordering t3; + const std::strong_ordering &f1() { return std::strong_ordering::less; } const std::strong_ordering &f2() { return std::strong_ordering::equal; } @@ -34,3 +38,83 @@ template bool f9(std::strong_ordering a0, T1 a1) { template bool f10(std::strong_ordering a0, T1 a1) { return a0 >= 0; } + +const std::weak_ordering &f11() { return std::weak_ordering::less; } + +const std::weak_ordering &f12() { return std::weak_ordering::equivalent; } + +const std::weak_ordering &f13() { return std::weak_ordering::greater; } + +const std::partial_ordering &f14() { return std::partial_ordering::less; } + +const std::partial_ordering &f15() { return std::partial_ordering::equivalent; } + +const std::partial_ordering &f16() { return std::partial_ordering::greater; } + +const std::partial_ordering &f17() { return std::partial_ordering::unordered; } + +bool f18(std::weak_ordering a0, std::weak_ordering a1) { return a0 == a1; } + +bool f19(std::partial_ordering a0, std::partial_ordering a1) { + return a0 == a1; +} + +template bool f20(std::weak_ordering a0, T1 a1) { + return a0 == 0; +} + +template bool f21(std::weak_ordering a0, T1 a1) { return a0 < 0; } + +template bool f22(std::weak_ordering a0, T1 a1) { return a0 > 0; } + +template bool f23(std::weak_ordering a0, T1 a1) { + return a0 <= 0; +} + +template bool f24(std::weak_ordering a0, T1 a1) { + return a0 >= 0; +} + +template bool f25(std::partial_ordering a0, T1 a1) { + return a0 == 0; +} + +template bool f26(std::partial_ordering a0, T1 a1) { + return a0 < 0; +} + +template bool f27(std::partial_ordering a0, T1 a1) { + return a0 > 0; +} + +template bool f28(std::partial_ordering a0, T1 a1) { + return a0 <= 0; +} + +template bool f29(std::partial_ordering a0, T1 a1) { + return a0 >= 0; +} + +std::weak_ordering f30(std::strong_ordering a0) { + return a0.operator std::weak_ordering(); +} + +std::partial_ordering f31(std::strong_ordering a0) { + return a0.operator std::partial_ordering(); +} + +std::partial_ordering f32(std::weak_ordering a0) { + return a0.operator std::partial_ordering(); +} + +bool f33(std::partial_ordering a0) { return std::is_eq(a0); } + +bool f34(std::partial_ordering a0) { return std::is_neq(a0); } + +bool f35(std::partial_ordering a0) { return std::is_lt(a0); } + +bool f36(std::partial_ordering a0) { return std::is_lteq(a0); } + +bool f37(std::partial_ordering a0) { return std::is_gt(a0); } + +bool f38(std::partial_ordering a0) { return std::is_gteq(a0); } diff --git a/rules/compare/tgt_refcount.rs b/rules/compare/tgt_refcount.rs index 356d20af01..8ba50020d5 100644 --- a/rules/compare/tgt_refcount.rs +++ b/rules/compare/tgt_refcount.rs @@ -3,18 +3,46 @@ use libcc2rs::*; -fn f1() -> Ptr { +fn f1() -> Ptr { libcc2rs::strong_ordering_less() } -fn f2() -> Ptr { +fn f2() -> Ptr { libcc2rs::strong_ordering_equal() } -fn f3() -> Ptr { +fn f3() -> Ptr { libcc2rs::strong_ordering_equivalent() } -fn f4() -> Ptr { +fn f4() -> Ptr { libcc2rs::strong_ordering_greater() } + +fn f11() -> Ptr { + libcc2rs::weak_ordering_less() +} + +fn f12() -> Ptr { + libcc2rs::weak_ordering_equivalent() +} + +fn f13() -> Ptr { + libcc2rs::weak_ordering_greater() +} + +fn f14() -> Ptr { + libcc2rs::partial_ordering_less() +} + +fn f15() -> Ptr { + libcc2rs::partial_ordering_equivalent() +} + +fn f16() -> Ptr { + libcc2rs::partial_ordering_greater() +} + +fn f17() -> Ptr { + libcc2rs::partial_ordering_unordered() +} diff --git a/rules/compare/tgt_unsafe.rs b/rules/compare/tgt_unsafe.rs index 3de92c726f..0e19eb893d 100644 --- a/rules/compare/tgt_unsafe.rs +++ b/rules/compare/tgt_unsafe.rs @@ -1,46 +1,178 @@ // Copyright (c) 2022-present INESC-ID. // Distributed under the MIT license that can be found in the LICENSE file. -fn t1() -> std::cmp::Ordering { - std::cmp::Ordering::Equal +fn t1() -> libcc2rs::StrongOrdering { + Default::default() } -fn f1() -> *const std::cmp::Ordering { +fn t2() -> libcc2rs::WeakOrdering { + Default::default() +} + +fn t3() -> libcc2rs::PartialOrdering { + Default::default() +} + +fn f1() -> *const libcc2rs::StrongOrdering { &raw const libcc2rs::STRONG_ORDERING_LESS } -fn f2() -> *const std::cmp::Ordering { +fn f2() -> *const libcc2rs::StrongOrdering { &raw const libcc2rs::STRONG_ORDERING_EQUAL } -fn f3() -> *const std::cmp::Ordering { +fn f3() -> *const libcc2rs::StrongOrdering { &raw const libcc2rs::STRONG_ORDERING_EQUIVALENT } -fn f4() -> *const std::cmp::Ordering { +fn f4() -> *const libcc2rs::StrongOrdering { &raw const libcc2rs::STRONG_ORDERING_GREATER } -fn f5(a0: std::cmp::Ordering, a1: std::cmp::Ordering) -> bool { - a0 == a1 +fn f5(a0: libcc2rs::StrongOrdering, a1: libcc2rs::StrongOrdering) -> bool { + let __a = libcc2rs::PartialOrdering::from(a0); + let __b = libcc2rs::PartialOrdering::from(a1); + libcc2rs::is_lt(__a) == libcc2rs::is_lt(__b) + && libcc2rs::is_eq(__a) == libcc2rs::is_eq(__b) + && libcc2rs::is_gt(__a) == libcc2rs::is_gt(__b) +} + +fn f6(a0: libcc2rs::StrongOrdering, a1: i32) -> bool { + libcc2rs::is_eq(a0) +} + +fn f7(a0: libcc2rs::StrongOrdering, a1: i32) -> bool { + libcc2rs::is_lt(a0) +} + +fn f8(a0: libcc2rs::StrongOrdering, a1: i32) -> bool { + libcc2rs::is_gt(a0) +} + +fn f9(a0: libcc2rs::StrongOrdering, a1: i32) -> bool { + libcc2rs::is_lteq(a0) +} + +fn f10(a0: libcc2rs::StrongOrdering, a1: i32) -> bool { + libcc2rs::is_gteq(a0) +} + +fn f11() -> *const libcc2rs::WeakOrdering { + &raw const libcc2rs::WEAK_ORDERING_LESS +} + +fn f12() -> *const libcc2rs::WeakOrdering { + &raw const libcc2rs::WEAK_ORDERING_EQUIVALENT +} + +fn f13() -> *const libcc2rs::WeakOrdering { + &raw const libcc2rs::WEAK_ORDERING_GREATER +} + +fn f14() -> *const libcc2rs::PartialOrdering { + &raw const libcc2rs::PARTIAL_ORDERING_LESS +} + +fn f15() -> *const libcc2rs::PartialOrdering { + &raw const libcc2rs::PARTIAL_ORDERING_EQUIVALENT +} + +fn f16() -> *const libcc2rs::PartialOrdering { + &raw const libcc2rs::PARTIAL_ORDERING_GREATER +} + +fn f17() -> *const libcc2rs::PartialOrdering { + &raw const libcc2rs::PARTIAL_ORDERING_UNORDERED +} + +fn f18(a0: libcc2rs::WeakOrdering, a1: libcc2rs::WeakOrdering) -> bool { + let __a = libcc2rs::PartialOrdering::from(a0); + let __b = libcc2rs::PartialOrdering::from(a1); + libcc2rs::is_lt(__a) == libcc2rs::is_lt(__b) + && libcc2rs::is_eq(__a) == libcc2rs::is_eq(__b) + && libcc2rs::is_gt(__a) == libcc2rs::is_gt(__b) +} + +fn f19(a0: libcc2rs::PartialOrdering, a1: libcc2rs::PartialOrdering) -> bool { + let __a = libcc2rs::PartialOrdering::from(a0); + let __b = libcc2rs::PartialOrdering::from(a1); + libcc2rs::is_lt(__a) == libcc2rs::is_lt(__b) + && libcc2rs::is_eq(__a) == libcc2rs::is_eq(__b) + && libcc2rs::is_gt(__a) == libcc2rs::is_gt(__b) +} + +fn f20(a0: libcc2rs::WeakOrdering, a1: i32) -> bool { + libcc2rs::is_eq(a0) +} + +fn f21(a0: libcc2rs::WeakOrdering, a1: i32) -> bool { + libcc2rs::is_lt(a0) +} + +fn f22(a0: libcc2rs::WeakOrdering, a1: i32) -> bool { + libcc2rs::is_gt(a0) +} + +fn f23(a0: libcc2rs::WeakOrdering, a1: i32) -> bool { + libcc2rs::is_lteq(a0) +} + +fn f24(a0: libcc2rs::WeakOrdering, a1: i32) -> bool { + libcc2rs::is_gteq(a0) +} + +fn f25(a0: libcc2rs::PartialOrdering, a1: i32) -> bool { + libcc2rs::is_eq(a0) +} + +fn f26(a0: libcc2rs::PartialOrdering, a1: i32) -> bool { + libcc2rs::is_lt(a0) +} + +fn f27(a0: libcc2rs::PartialOrdering, a1: i32) -> bool { + libcc2rs::is_gt(a0) +} + +fn f28(a0: libcc2rs::PartialOrdering, a1: i32) -> bool { + libcc2rs::is_lteq(a0) +} + +fn f29(a0: libcc2rs::PartialOrdering, a1: i32) -> bool { + libcc2rs::is_gteq(a0) +} + +fn f30(a0: libcc2rs::StrongOrdering) -> libcc2rs::WeakOrdering { + libcc2rs::WeakOrdering::from(a0) +} + +fn f31(a0: libcc2rs::StrongOrdering) -> libcc2rs::PartialOrdering { + libcc2rs::PartialOrdering::from(a0) +} + +fn f32(a0: libcc2rs::WeakOrdering) -> libcc2rs::PartialOrdering { + libcc2rs::PartialOrdering::from(a0) +} + +fn f33(a0: libcc2rs::PartialOrdering) -> bool { + libcc2rs::is_eq(a0) } -fn f6(a0: std::cmp::Ordering, a1: i32) -> bool { - a0 == std::cmp::Ordering::Equal +fn f34(a0: libcc2rs::PartialOrdering) -> bool { + libcc2rs::is_neq(a0) } -fn f7(a0: std::cmp::Ordering, a1: i32) -> bool { - a0 == std::cmp::Ordering::Less +fn f35(a0: libcc2rs::PartialOrdering) -> bool { + libcc2rs::is_lt(a0) } -fn f8(a0: std::cmp::Ordering, a1: i32) -> bool { - a0 == std::cmp::Ordering::Greater +fn f36(a0: libcc2rs::PartialOrdering) -> bool { + libcc2rs::is_lteq(a0) } -fn f9(a0: std::cmp::Ordering, a1: i32) -> bool { - a0 != std::cmp::Ordering::Greater +fn f37(a0: libcc2rs::PartialOrdering) -> bool { + libcc2rs::is_gt(a0) } -fn f10(a0: std::cmp::Ordering, a1: i32) -> bool { - a0 != std::cmp::Ordering::Less +fn f38(a0: libcc2rs::PartialOrdering) -> bool { + libcc2rs::is_gteq(a0) } diff --git a/rules/iterator_category/shim.rs b/rules/iterator_category/shim.rs index 1b6e93a23d..0d3a440f37 100644 --- a/rules/iterator_category/shim.rs +++ b/rules/iterator_category/shim.rs @@ -1,15 +1,16 @@ // Copyright (c) 2022-present INESC-ID. // Distributed under the MIT license that can be found in the LICENSE file. -use std::cmp::Ordering; +use std::cell::RefCell; use std::convert::Infallible; use std::marker::PhantomData; use std::rc::Rc; use crate::{ - ByteRepr, EqualityComparable, EqualityComparableUnsafe, MapNode, PostfixDec, PostfixInc, - PrefixDec, PrefixInc, Ptr, UnsafeMapNode, Value, + ByteRepr, EqualityComparable, EqualityComparableUnsafe, MapNode, PartiallyOrderedWith, + PartiallyOrderedWithUnsafe, PostfixDec, PostfixInc, PrefixDec, PrefixInc, Ptr, TotallyOrdered, + TotallyOrderedUnsafe, UnsafeMapNode, Value, }; pub trait CppIterator: Clone { @@ -25,7 +26,7 @@ pub trait CppBidirectionalIterator: CppInputIterator { fn iter_post_dec(&mut self) -> Self; } -pub trait CppRandomAccessIterator: CppBidirectionalIterator + PartialOrd { +pub trait CppRandomAccessIterator: CppBidirectionalIterator + TotallyOrdered { fn iter_add(&self, n: isize) -> Self; fn iter_add_assign(&mut self, n: isize) -> Self; fn iter_distance(&self, from: &Self) -> isize; @@ -45,7 +46,9 @@ pub trait UnsafeCppBidirectionalIterator: UnsafeCppInputIterator { fn iter_post_dec(&mut self) -> Self; } -pub trait UnsafeCppRandomAccessIterator: UnsafeCppBidirectionalIterator + PartialOrd { +pub trait UnsafeCppRandomAccessIterator: + UnsafeCppBidirectionalIterator + TotallyOrderedUnsafe +{ fn iter_add(&self, n: isize) -> Self; fn iter_add_assign(&mut self, n: isize) -> Self; fn iter_distance(&self, from: &Self) -> isize; @@ -170,9 +173,15 @@ macro_rules! uninhabited_iterator { impl ByteRepr for $name {} - impl PartialEq for $name { - fn eq(&self, _: &Self) -> bool { - self.never() + impl crate::WeaklyEqualityComparableWith for $name { + fn equal(this: Ptr, _: Ptr) -> bool { + this.with(|it| it.never()) + } + } + + impl crate::WeaklyEqualityComparableWithUnsafe for $name { + unsafe fn equal(this: *const Self, _: *const Self) -> bool { + unsafe { (*this).never() } } } @@ -263,11 +272,13 @@ impl Clone for BidirectionalIterator { impl ByteRepr for BidirectionalIterator {} -impl PartialEq for BidirectionalIterator { - fn eq(&self, other: &Self) -> bool { - match (self, other) { - (Self::Map(a), Self::Map(b)) => Rc::ptr_eq(a, b), - } +impl crate::WeaklyEqualityComparableWith for BidirectionalIterator { + fn equal(this: Ptr, rhs: Ptr) -> bool { + this.with(|a| { + rhs.with(|b| match (a, b) { + (Self::Map(a), Self::Map(b)) => Rc::ptr_eq(a, b), + }) + }) } } @@ -324,10 +335,12 @@ impl Clone for UnsafeBidirectionalIterator { } } -impl PartialEq for UnsafeBidirectionalIterator { - fn eq(&self, other: &Self) -> bool { - match (self, other) { - (Self::Map(a), Self::Map(b)) => a == b, +impl crate::WeaklyEqualityComparableWithUnsafe for UnsafeBidirectionalIterator { + unsafe fn equal(this: *const Self, rhs: *const Self) -> bool { + unsafe { + match (&*this, &*rhs) { + (Self::Map(a), Self::Map(b)) => a == b, + } } } } @@ -368,7 +381,7 @@ impl UnsafeCppBidirectionalIterator for UnsafeBidirectionalIterator { } macro_rules! random_access_iterator { - ($name:ident, $ptr:ty, $iter:ident, $input:ident, $bidi:ident, $random:ident, $eq:ident, $null:expr) => { + ($name:ident, $ptr:ty, $iter:ident, $input:ident, $bidi:ident, $random:ident, $eq:ident, $ord:ident, $null:expr) => { pub enum $name { Vec($ptr), } @@ -395,28 +408,6 @@ macro_rules! random_access_iterator { impl ByteRepr for $name {} - impl PartialEq for $name - where - $ptr: PartialEq, - { - fn eq(&self, other: &Self) -> bool { - match (self, other) { - (Self::Vec(a), Self::Vec(b)) => a == b, - } - } - } - - impl PartialOrd for $name - where - $ptr: PartialOrd, - { - fn partial_cmp(&self, other: &Self) -> Option { - match (self, other) { - (Self::Vec(a), Self::Vec(b)) => a.partial_cmp(b), - } - } - } - impl $iter for $name { fn iter_deref(&self) -> $ptr { match self { @@ -468,7 +459,7 @@ macro_rules! random_access_iterator { impl $random for $name where $ptr: $random, - Self: $eq, + Self: $ord, { fn iter_add(&self, n: isize) -> Self { match self { @@ -502,6 +493,7 @@ random_access_iterator!( CppBidirectionalIterator, CppRandomAccessIterator, EqualityComparable, + TotallyOrdered, Ptr::null() ); @@ -521,6 +513,7 @@ random_access_iterator!( UnsafeCppBidirectionalIterator, UnsafeCppRandomAccessIterator, EqualityComparableUnsafe, + TotallyOrderedUnsafe, std::ptr::null_mut() ); @@ -529,53 +522,161 @@ crate::constructor::impl_trivial_copy_move!( UnsafeBidirectionalIterator, UnsafeRandomAccessIterator, ); -crate::cmp::impl_weakly_equality_comparable_with!( - InputIterator, OutputIterator, ForwardIterator, - BidirectionalIterator, RandomAccessIterator, -); +impl crate::WeaklyEqualityComparableWith for RandomAccessIterator { + fn equal(this: Ptr, rhs: Ptr) -> bool { + this.with(|a| { + rhs.with(|b| match (a, b) { + (Self::Vec(a), Self::Vec(b)) => a == b, + }) + }) + } +} -crate::cmp::impl_weakly_equality_comparable_with_unsafe!( - UnsafeInputIterator, UnsafeOutputIterator, UnsafeForwardIterator, - UnsafeBidirectionalIterator, UnsafeRandomAccessIterator, -); +impl PartiallyOrderedWith for RandomAccessIterator { + fn less(this: Ptr, rhs: Ptr) -> bool { + this.with(|a| { + rhs.with(|b| match (a, b) { + (Self::Vec(a), Self::Vec(b)) => a < b, + }) + }) + } + fn greater(this: Ptr, rhs: Ptr) -> bool { + this.with(|a| { + rhs.with(|b| match (a, b) { + (Self::Vec(a), Self::Vec(b)) => a > b, + }) + }) + } + fn less_equal(this: Ptr, rhs: Ptr) -> bool { + this.with(|a| { + rhs.with(|b| match (a, b) { + (Self::Vec(a), Self::Vec(b)) => a <= b, + }) + }) + } + fn greater_equal(this: Ptr, rhs: Ptr) -> bool { + this.with(|a| { + rhs.with(|b| match (a, b) { + (Self::Vec(a), Self::Vec(b)) => a >= b, + }) + }) + } +} -pub trait IterStep: Clone + PartialEq { - fn iter_step(&mut self); +impl crate::WeaklyEqualityComparableWithUnsafe for UnsafeRandomAccessIterator { + unsafe fn equal(this: *const Self, rhs: *const Self) -> bool { + unsafe { + match (&*this, &*rhs) { + (Self::Vec(a), Self::Vec(b)) => a == b, + } + } + } +} + +impl PartiallyOrderedWithUnsafe for UnsafeRandomAccessIterator { + unsafe fn less(this: *const Self, rhs: *const Self) -> bool { + unsafe { + match (&*this, &*rhs) { + (Self::Vec(a), Self::Vec(b)) => a < b, + } + } + } + unsafe fn greater(this: *const Self, rhs: *const Self) -> bool { + unsafe { + match (&*this, &*rhs) { + (Self::Vec(a), Self::Vec(b)) => a > b, + } + } + } + unsafe fn less_equal(this: *const Self, rhs: *const Self) -> bool { + unsafe { + match (&*this, &*rhs) { + (Self::Vec(a), Self::Vec(b)) => a <= b, + } + } + } + unsafe fn greater_equal(this: *const Self, rhs: *const Self) -> bool { + unsafe { + match (&*this, &*rhs) { + (Self::Vec(a), Self::Vec(b)) => a >= b, + } + } + } +} + +pub trait IterStep: Clone { + type Var; + fn iter_var(self) -> Self::Var; + fn iter_not_equal(cur: &Self::Var, end: &Self::Var) -> bool; + fn iter_next(cur: &mut Self::Var) -> Self; } macro_rules! iter_step { - ($name:ty, $iter:ident) => { + ($name:ty) => { impl IterStep for $name where - $name: $iter + PartialEq, + $name: CppIterator + EqualityComparable, { - fn iter_step(&mut self) { - $iter::::iter_inc(self); + type Var = Value; + fn iter_var(self) -> Self::Var { + Rc::new(RefCell::new(self)) + } + fn iter_not_equal(cur: &Self::Var, end: &Self::Var) -> bool { + ::not_equal( + crate::AsPointer::::as_pointer(cur), + crate::AsPointer::::as_pointer(end), + ) + } + fn iter_next(cur: &mut Self::Var) -> Self { + CppIterator::::iter_post_inc(&mut *cur.borrow_mut()) } } }; } -iter_step!(Ptr, CppIterator); -iter_step!(*mut T, UnsafeCppIterator); -iter_step!(*const T, UnsafeCppIterator); -iter_step!(InputIterator, CppIterator); -iter_step!(ForwardIterator, CppIterator); -iter_step!(UnsafeInputIterator, UnsafeCppIterator); -iter_step!(UnsafeForwardIterator, UnsafeCppIterator); -iter_step!(BidirectionalIterator, CppIterator); -iter_step!(UnsafeBidirectionalIterator, UnsafeCppIterator); -iter_step!(RandomAccessIterator, CppIterator); -iter_step!(UnsafeRandomAccessIterator, UnsafeCppIterator); +macro_rules! iter_step_unsafe { + ($name:ty) => { + impl IterStep for $name + where + $name: UnsafeCppIterator + EqualityComparableUnsafe, + { + type Var = Self; + fn iter_var(self) -> Self::Var { + self + } + fn iter_not_equal(cur: &Self::Var, end: &Self::Var) -> bool { + unsafe { ::not_equal(cur, end) } + } + fn iter_next(cur: &mut Self::Var) -> Self { + UnsafeCppIterator::::iter_post_inc(cur) + } + } + }; +} + +iter_step!(Ptr); +iter_step_unsafe!(*mut T); +iter_step_unsafe!(*const T); +iter_step!(InputIterator); +iter_step!(ForwardIterator); +iter_step_unsafe!(UnsafeInputIterator); +iter_step_unsafe!(UnsafeForwardIterator); +iter_step!(BidirectionalIterator); +iter_step_unsafe!(UnsafeBidirectionalIterator); +iter_step!(RandomAccessIterator); +iter_step_unsafe!(UnsafeRandomAccessIterator); -pub struct IterRange { - cur: I, - end: I, +pub struct IterRange { + cur: I::Var, + end: I::Var, } impl IterRange { pub fn new(cur: I, end: I) -> Self { - Self { cur, end } + Self { + cur: cur.iter_var(), + end: end.iter_var(), + } } } @@ -583,12 +684,10 @@ impl Iterator for IterRange { type Item = I; fn next(&mut self) -> Option { - if self.cur == self.end { + if !I::iter_not_equal(&self.cur, &self.end) { return None; } - let it = self.cur.clone(); - self.cur.iter_step(); - Some(it) + Some(I::iter_next(&mut self.cur)) } } diff --git a/rules/iterator_category/tgt_refcount.rs b/rules/iterator_category/tgt_refcount.rs index f20c452a6e..ee6d7bb7ea 100644 --- a/rules/iterator_category/tgt_refcount.rs +++ b/rules/iterator_category/tgt_refcount.rs @@ -105,20 +105,20 @@ fn f19, T2>(a0: T1, a1: isize) -> Ptr { a0.iter_at(a1) } -fn f20, T2>(a0: T1, a1: T1) -> bool { - a0 < a1 +fn f20, T2>(a0: Ptr, a1: Ptr) -> bool { + PartiallyOrderedWith::less(a0, a1) } -fn f21, T2>(a0: T1, a1: T1) -> bool { - a0 > a1 +fn f21, T2>(a0: Ptr, a1: Ptr) -> bool { + PartiallyOrderedWith::greater(a0, a1) } -fn f22, T2>(a0: T1, a1: T1) -> bool { - a0 <= a1 +fn f22, T2>(a0: Ptr, a1: Ptr) -> bool { + PartiallyOrderedWith::less_equal(a0, a1) } -fn f23, T2>(a0: T1, a1: T1) -> bool { - a0 >= a1 +fn f23, T2>(a0: Ptr, a1: Ptr) -> bool { + PartiallyOrderedWith::greater_equal(a0, a1) } fn f24, T2>(a0: T2) -> T1 { diff --git a/rules/iterator_category/tgt_unsafe.rs b/rules/iterator_category/tgt_unsafe.rs index eb8aa899a7..9e13b027fb 100644 --- a/rules/iterator_category/tgt_unsafe.rs +++ b/rules/iterator_category/tgt_unsafe.rs @@ -105,20 +105,20 @@ fn f19, T2>(a0: T1, a1: isize) -> *mut T2 a0.iter_at(a1) } -fn f20, T2>(a0: T1, a1: T1) -> bool { - a0 < a1 +unsafe fn f20, T2>(a0: T1, a1: T1) -> bool { + PartiallyOrderedWithUnsafe::less(&a0, &a1) } -fn f21, T2>(a0: T1, a1: T1) -> bool { - a0 > a1 +unsafe fn f21, T2>(a0: T1, a1: T1) -> bool { + PartiallyOrderedWithUnsafe::greater(&a0, &a1) } -fn f22, T2>(a0: T1, a1: T1) -> bool { - a0 <= a1 +unsafe fn f22, T2>(a0: T1, a1: T1) -> bool { + PartiallyOrderedWithUnsafe::less_equal(&a0, &a1) } -fn f23, T2>(a0: T1, a1: T1) -> bool { - a0 >= a1 +unsafe fn f23, T2>(a0: T1, a1: T1) -> bool { + PartiallyOrderedWithUnsafe::greater_equal(&a0, &a1) } fn f24, T2>(a0: T2) -> T1 { diff --git a/rules/map/shim.rs b/rules/map/shim.rs index b907981b15..e3a1b1156e 100644 --- a/rules/map/shim.rs +++ b/rules/map/shim.rs @@ -9,8 +9,8 @@ use std::rc::{Rc, Weak}; use crate::{ AsPointer, BidirectionalIterator, ByteRepr, CopyAssignable, CopyAssignableUnsafe, - CopyConstructible, CopyConstructibleUnsafe, CppIterator, CppLess, Ptr, - UnsafeBidirectionalIterator, UnsafeCppIterator, Value, ZeroInitialization, copy_constructed, + CopyConstructible, CopyConstructibleUnsafe, LessThanComparable, LessThanComparableUnsafe, Ptr, + UnsafeBidirectionalIterator, Value, ZeroInitialization, copy_constructed, copy_constructed_unsafe, cpp_cmp, }; @@ -18,29 +18,29 @@ impl ByteRepr for BTreeMap {} // A map keeps a single copy of each key, as C++ does, since copying a key may // have side effects. Its index points to the keys of the entries, is ordered -// by them with operator< (CppLess), and is searched for a key through a +// by them with operator< (LessThanComparable), and is searched for a key through a // pointer to it. struct MapKey(Ptr); -impl Ord for MapKey { +impl Ord for MapKey { fn cmp(&self, other: &Self) -> Ordering { cpp_cmp(&self.0, &other.0) } } -impl PartialOrd for MapKey { +impl PartialOrd for MapKey { fn partial_cmp(&self, other: &Self) -> Option { Some(self.cmp(other)) } } -impl PartialEq for MapKey { +impl PartialEq for MapKey { fn eq(&self, other: &Self) -> bool { self.cmp(other) == Ordering::Equal } } -impl Eq for MapKey {} +impl Eq for MapKey {} pub struct MapNode { value: Option>, @@ -112,7 +112,7 @@ impl ByteRepr for Map {} // constructor. impl CopyConstructible for Map, Value)> where - K: CppLess + CopyConstructible + ZeroInitialization + 'static, + K: LessThanComparable + CopyConstructible + ZeroInitialization + 'static, V: CopyConstructible + ZeroInitialization + 'static, { fn copy_constructor(this: Ptr, src: Ptr) { @@ -144,7 +144,7 @@ where impl CopyAssignable for Map, Value)> where - K: CppLess + CopyConstructible + ZeroInitialization + 'static, + K: LessThanComparable + CopyConstructible + ZeroInitialization + 'static, V: CopyConstructible + ZeroInitialization + 'static, { fn copy_assign(this: Ptr, src: Ptr) -> Ptr { @@ -154,7 +154,7 @@ where } } -impl Clone for Map, Value)> { +impl Clone for Map, Value)> { fn clone(&self) -> Self { let mut map = Self::default(); let mut prev = map.header.clone(); @@ -202,7 +202,7 @@ impl Map { type Entry = (Value, Value); type EntryNode = Value>>; -impl Map> { +impl Map> { fn link_after( &mut self, prev: Value>>, @@ -277,7 +277,7 @@ impl Map> { // The functions that modify a map take a pointer to it, and borrow it after // their arguments are evaluated, which may access the map. -impl Map> { +impl Map> { // operator[] with a key that is copied, and a value made by new_value, if // inserted. pub fn subscript(&mut self, key: Ptr, new_value: impl FnOnce() -> V) -> Ptr @@ -351,27 +351,8 @@ impl Map> { } impl Map { - pub fn range(map: Ptr) -> MapRange { - map.with(|map| MapRange { - cur: map.begin(), - end: map.end(), - }) - } -} - -pub struct MapRange { - cur: BidirectionalIterator, - end: BidirectionalIterator, -} - -impl Iterator for MapRange { - type Item = BidirectionalIterator; - - fn next(&mut self) -> Option { - if self.cur == self.end { - return None; - } - Some(self.cur.iter_post_inc()) + pub fn range(map: Ptr) -> crate::IterRange> { + map.with(|map| crate::IterRange::new(map.begin(), map.end())) } } @@ -416,25 +397,33 @@ impl UnsafeMapNode { // the nodes, so that each key has a single copy, as in Map. struct UnsafeMapKey(*const K); -impl Ord for UnsafeMapKey { +impl Ord for UnsafeMapKey { fn cmp(&self, other: &Self) -> Ordering { - unsafe { (*self.0).cmp(&*other.0) } + unsafe { + if K::less(self.0, other.0) { + Ordering::Less + } else if K::less(other.0, self.0) { + Ordering::Greater + } else { + Ordering::Equal + } + } } } -impl PartialOrd for UnsafeMapKey { +impl PartialOrd for UnsafeMapKey { fn partial_cmp(&self, other: &Self) -> Option { Some(self.cmp(other)) } } -impl PartialEq for UnsafeMapKey { +impl PartialEq for UnsafeMapKey { fn eq(&self, other: &Self) -> bool { self.cmp(other) == Ordering::Equal } } -impl Eq for UnsafeMapKey {} +impl Eq for UnsafeMapKey {} pub struct UnsafeMap { nodes: BTreeMap, Box>>, @@ -450,7 +439,9 @@ impl Default for UnsafeMap { } } -impl UnsafeMap { +impl + UnsafeMap +{ // A copy of the map, which copies its keys and values, each with its copy // constructor. unsafe fn copied(&self) -> Self { @@ -465,16 +456,16 @@ impl UnsafeMap CopyConstructibleUnsafe - for UnsafeMap +impl + CopyConstructibleUnsafe for UnsafeMap { unsafe fn copy_constructor(this: *mut Self, src: *const Self) { unsafe { std::ptr::write(this, (*src).copied()) } } } -impl CopyAssignableUnsafe - for UnsafeMap +impl + CopyAssignableUnsafe for UnsafeMap { unsafe fn copy_assign(this: *mut Self, src: *const Self) -> *mut Self { unsafe { *this = (*src).copied() }; @@ -482,7 +473,7 @@ impl CopyAssignabl } } -impl Clone for UnsafeMap { +impl Clone for UnsafeMap { fn clone(&self) -> Self { let mut map = Self::default(); let mut prev = map.header_ptr(); @@ -521,19 +512,14 @@ impl UnsafeMap { /// # Safety /// `map` must point to a live map. - pub unsafe fn range(map: *const Self) -> UnsafeMapRange { - unsafe { - UnsafeMapRange { - cur: (*map).begin(), - end: (*map).end(), - } - } + pub unsafe fn range(map: *const Self) -> crate::IterRange> { + unsafe { crate::IterRange::new((*map).begin(), (*map).end()) } } } type UnsafeEntryNode = *mut UnsafeMapNode<(K, V)>; -impl UnsafeMap { +impl UnsafeMap { fn link_after( &mut self, prev: *mut UnsafeMapNode<(K, V)>, @@ -679,22 +665,6 @@ impl UnsafeMap { } } -pub struct UnsafeMapRange { - cur: UnsafeBidirectionalIterator, - end: UnsafeBidirectionalIterator, -} - -impl Iterator for UnsafeMapRange { - type Item = UnsafeBidirectionalIterator; - - fn next(&mut self) -> Option { - if self.cur == self.end { - return None; - } - Some(self.cur.iter_post_inc()) - } -} - crate::constructor::impl_copy_by_clone!( BTreeMap); crate::constructor::impl_move_by_take!( BTreeMap, diff --git a/rules/map/tgt_refcount.rs b/rules/map/tgt_refcount.rs index ae8cb3a82f..e8f419967e 100644 --- a/rules/map/tgt_refcount.rs +++ b/rules/map/tgt_refcount.rs @@ -9,7 +9,7 @@ fn t1() -> Map, Value)> { Map::new() } -fn f1( +fn f1( a0: &mut Map, Value)>, a1: Ptr, ) -> Ptr { @@ -20,7 +20,7 @@ fn f2(a0: Map, Value)>) -> usize { a0.len() } -fn f3( +fn f3( a0: Ptr, Value)>>, a1: BidirectionalIterator<(Value, Value)>, ) -> BidirectionalIterator<(Value, Value)> { @@ -31,17 +31,17 @@ fn f5() -> Map, Value)> { Map::new() } -fn f6( +fn f6( a0: Ptr, Value)>>, ) -> Map, Value)> { copy_constructed(a0) } -fn f7(a0: &mut Map, Value)>, a1: Ptr) -> Ptr { +fn f7(a0: &mut Map, Value)>, a1: Ptr) -> Ptr { a0.get(&a1).expect("out of range!").borrow().1.as_pointer() } -fn f8( +fn f8( a0: &mut Map, Value)>, a1: Ptr, ) -> Ptr { @@ -52,7 +52,7 @@ fn f9(a0: Ptr, Value)>>) -> Bid a0.with(|__m: &Map, Value)>| __m.end()) } -fn f10(a0: Ptr, Value)>>, a1: Ptr) -> BidirectionalIterator<(Value, Value)> { +fn f10(a0: Ptr, Value)>>, a1: Ptr) -> BidirectionalIterator<(Value, Value)> { a0.with(|__m: &Map, Value)>| __m.find(&a1)) } @@ -64,11 +64,11 @@ fn f14(a0: Ptr, Value)>>) -> Bi a0.with(|__m: &Map, Value)>| __m.end()) } -fn f15(a0: &mut Map, Value)>, a1: Ptr) -> Ptr { +fn f15(a0: &mut Map, Value)>, a1: Ptr) -> Ptr { a0.get(&a1).expect("out of range!").borrow().1.as_pointer() } -fn f17(a0: Ptr, Value)>>, a1: Ptr) -> BidirectionalIterator<(Value, Value)> { +fn f17(a0: Ptr, Value)>>, a1: Ptr) -> BidirectionalIterator<(Value, Value)> { a0.with(|__m: &Map, Value)>| __m.find(&a1)) } @@ -84,7 +84,7 @@ where a0 } -fn f26( +fn f26( a0: &mut Map, Value)>, a1: Ptr, Value)>>, ) -> &mut Map, Value)> { @@ -92,14 +92,14 @@ fn f26( +fn f27( a0: Ptr, Value)>>, a1: BidirectionalIterator<(Value, Value)>, ) -> BidirectionalIterator<(Value, Value)> { Map::erase(a0, &a1) } -fn f28(a0: Ptr, Value)>>, a1: Ptr) -> usize { +fn f28(a0: Ptr, Value)>>, a1: Ptr) -> usize { Map::erase_key(a0, &a1) } @@ -107,7 +107,7 @@ fn f29(a0: Ptr, Value)>>) -> Bi a0.with(|__m: &Map, Value)>| __m.begin()) } -fn f67( +fn f67( a0: Ptr, Value)>>, init: cpp_type!("typename std::map::value_type", (Value, Value)), ) -> (Value, Value)>>, Value) { @@ -115,7 +115,7 @@ fn f67( (Rc::new(RefCell::new(__it)), Rc::new(RefCell::new(__inserted))) } -fn f68( +fn f68( a0: Ptr, Value)>>, a1: BidirectionalIterator<(Value, Value)>, init: cpp_type!("typename std::map::value_type", (Value, Value)), @@ -124,7 +124,7 @@ fn f68( Map::emplace(a0, &init.0.as_pointer(), || init).0 } -fn f73( +fn f73( a0: Ptr, Value)>>, a1: Ptr, init: T2, diff --git a/rules/map/tgt_unsafe.rs b/rules/map/tgt_unsafe.rs index ef84fb347d..f179b9c1ba 100644 --- a/rules/map/tgt_unsafe.rs +++ b/rules/map/tgt_unsafe.rs @@ -19,7 +19,7 @@ fn t6() -> T2 { unimplemented!() } -unsafe fn f1(a0: &mut UnsafeMap, a1: T1) -> &mut T2 { +unsafe fn f1(a0: &mut UnsafeMap, a1: T1) -> &mut T2 { &mut (*a0.subscript(&a1, || ::default_constructed())) } @@ -27,7 +27,7 @@ fn f2(a0: UnsafeMap) -> usize { a0.len() } -fn f3(a0: &mut UnsafeMap, a1: UnsafeBidirectionalIterator<(T1, T2)>) -> UnsafeBidirectionalIterator<(T1, T2)> { +fn f3(a0: &mut UnsafeMap, a1: UnsafeBidirectionalIterator<(T1, T2)>) -> UnsafeBidirectionalIterator<(T1, T2)> { a0.erase(&a1) } @@ -35,15 +35,15 @@ fn f5() -> UnsafeMap { UnsafeMap::new() } -unsafe fn f6(a0: *const UnsafeMap) -> UnsafeMap { +unsafe fn f6(a0: *const UnsafeMap) -> UnsafeMap { copy_constructed_unsafe(a0) } -unsafe fn f7(a0: &mut UnsafeMap, a1: T1) -> *const T2 { +unsafe fn f7(a0: &mut UnsafeMap, a1: T1) -> *const T2 { &raw const (*a0.get(&a1).expect("out of range!")).1 } -unsafe fn f8(a0: &mut UnsafeMap, a1: *mut T1) -> &mut T2 { +unsafe fn f8(a0: &mut UnsafeMap, a1: *mut T1) -> &mut T2 { &mut (*a0.subscript_move(a1, |__k| cpp_move_unsafe!(__k), || ::default_constructed())) } @@ -51,7 +51,7 @@ fn f9(a0: UnsafeMap) -> UnsafeBidirectionalIterator<(T1, T a0.end() } -fn f10(a0: &mut UnsafeMap, a1: T1) -> UnsafeBidirectionalIterator<(T1, T2)> { +fn f10(a0: &mut UnsafeMap, a1: T1) -> UnsafeBidirectionalIterator<(T1, T2)> { a0.find(&a1) } @@ -63,11 +63,11 @@ fn f14(a0: &mut UnsafeMap) -> UnsafeBidirectionalIterator< a0.end() } -unsafe fn f15(a0: &mut UnsafeMap, a1: T1) -> *const T2 { +unsafe fn f15(a0: &mut UnsafeMap, a1: T1) -> *const T2 { &raw const (*a0.get(&a1).expect("out of range!")).1 } -fn f17(a0: UnsafeMap, a1: T1) -> UnsafeBidirectionalIterator<(T1, T2)> { +fn f17(a0: UnsafeMap, a1: T1) -> UnsafeBidirectionalIterator<(T1, T2)> { a0.find(&a1) } @@ -80,19 +80,19 @@ unsafe fn f25<'a, T1, T2>(a0: &'a mut UnsafeMap, a1: &mut UnsafeMa a0 } -unsafe fn f26<'a, T1: Ord + CopyConstructibleUnsafe, T2: CopyConstructibleUnsafe>(a0: &'a mut UnsafeMap, a1: *const UnsafeMap) -> &'a mut UnsafeMap { +unsafe fn f26<'a, T1: LessThanComparableUnsafe + CopyConstructibleUnsafe, T2: CopyConstructibleUnsafe>(a0: &'a mut UnsafeMap, a1: *const UnsafeMap) -> &'a mut UnsafeMap { AssignFromUnsafe::assign_from(&raw mut *a0, a1); a0 } -fn f27( +fn f27( a0: &mut UnsafeMap, a1: UnsafeBidirectionalIterator<(T1, T2)>, ) -> UnsafeBidirectionalIterator<(T1, T2)> { a0.erase(&a1) } -fn f28(a0: &mut UnsafeMap, a1: T1) -> usize { +fn f28(a0: &mut UnsafeMap, a1: T1) -> usize { a0.erase_key(&a1) } @@ -100,14 +100,14 @@ fn f29(a0: UnsafeMap) -> UnsafeBidirectionalIterator<(T1, a0.begin() } -fn f67( +fn f67( a0: &mut UnsafeMap, init: cpp_type!("typename std::map::value_type", (T1, T2)), ) -> (UnsafeBidirectionalIterator<(T1, T2)>, bool) { a0.emplace(init) } -fn f68( +fn f68( a0: &mut UnsafeMap, a1: UnsafeBidirectionalIterator<(T1, T2)>, init: cpp_type!("typename std::map::value_type", (T1, T2)), @@ -116,7 +116,7 @@ fn f68( a0.emplace(init).0 } -fn f73( +fn f73( a0: &mut UnsafeMap, a1: T1, init: T2, diff --git a/rules/pair/tgt_refcount.rs b/rules/pair/tgt_refcount.rs index 8233935bf1..c580b20db3 100644 --- a/rules/pair/tgt_refcount.rs +++ b/rules/pair/tgt_refcount.rs @@ -137,3 +137,19 @@ where fn f28, T2: WeaklyEqualityComparableWith>(a0: (Value, Value), a1: (Value, Value)) -> bool { !(WeaklyEqualityComparableWith::equal(a0.0.as_pointer(), a1.0.as_pointer()) && WeaklyEqualityComparableWith::equal(a0.1.as_pointer(), a1.1.as_pointer())) } + +fn f29(a0: Ptr<(Value, Value)>, a1: Ptr<(Value, Value)>) -> bool { + LessThanComparable::less(a0, a1) +} + +fn f30(a0: Ptr<(Value, Value)>, a1: Ptr<(Value, Value)>) -> bool { + !LessThanComparable::less(a1, a0) +} + +fn f31(a0: Ptr<(Value, Value)>, a1: Ptr<(Value, Value)>) -> bool { + LessThanComparable::less(a1, a0) +} + +fn f32(a0: Ptr<(Value, Value)>, a1: Ptr<(Value, Value)>) -> bool { + !LessThanComparable::less(a0, a1) +} diff --git a/rules/pair/tgt_unsafe.rs b/rules/pair/tgt_unsafe.rs index e45a4353ce..919a8baa94 100644 --- a/rules/pair/tgt_unsafe.rs +++ b/rules/pair/tgt_unsafe.rs @@ -102,18 +102,18 @@ unsafe fn f28, T2: WeaklyEqualityComp !(WeaklyEqualityComparableWithUnsafe::equal(&a0.0, &a1.0) && WeaklyEqualityComparableWithUnsafe::equal(&a0.1, &a1.1)) } -fn f29(a0: (T1, T2), a1: (T1, T2)) -> bool { - a0 < a1 +unsafe fn f29(a0: (T1, T2), a1: (T1, T2)) -> bool { + LessThanComparableUnsafe::less(&a0, &a1) } -fn f30(a0: (T1, T2), a1: (T1, T2)) -> bool { - a0 <= a1 +unsafe fn f30(a0: (T1, T2), a1: (T1, T2)) -> bool { + !LessThanComparableUnsafe::less(&a1, &a0) } -fn f31(a0: (T1, T2), a1: (T1, T2)) -> bool { - a0 > a1 +unsafe fn f31(a0: (T1, T2), a1: (T1, T2)) -> bool { + LessThanComparableUnsafe::less(&a1, &a0) } -fn f32(a0: (T1, T2), a1: (T1, T2)) -> bool { - a0 >= a1 +unsafe fn f32(a0: (T1, T2), a1: (T1, T2)) -> bool { + !LessThanComparableUnsafe::less(&a0, &a1) } 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 51be3124d8..e9c1329adc 100644 --- a/tests/multi-file/friend_operator/out/refcount/friend_operator.rs +++ b/tests/multi-file/friend_operator/out/refcount/friend_operator.rs @@ -36,23 +36,25 @@ fn main_0() -> i32 { assert!(({ WeaklyEqualityComparableWith::equal(s1.as_pointer(), s2.as_pointer(),) })); assert!(({ WeaklyEqualityComparableWith::not_equal(s1.as_pointer(), s3.as_pointer(),) })); assert!(!({ WeaklyEqualityComparableWith::equal(s1.as_pointer(), s3.as_pointer(),) })); - assert!(({ operator_lt_0(s1.as_pointer(), s3.as_pointer(),) })); - assert!(!({ operator_lt_0(s3.as_pointer(), s1.as_pointer(),) })); - assert!((({ compare_1(s1.as_pointer(), s3.as_pointer(),) }) == -1_i32)); - assert!((({ compare_1(s3.as_pointer(), s1.as_pointer(),) }) == 1)); - assert!((({ compare_1(s1.as_pointer(), s2.as_pointer(),) }) == 0)); + assert!(({ LessThanComparable::less(s1.as_pointer(), s3.as_pointer(),) })); + assert!(!({ LessThanComparable::less(s3.as_pointer(), s1.as_pointer(),) })); + assert!((({ compare_0(s1.as_pointer(), s3.as_pointer(),) }) == -1_i32)); + assert!((({ compare_0(s3.as_pointer(), s1.as_pointer(),) }) == 1)); + assert!((({ compare_0(s1.as_pointer(), s2.as_pointer(),) }) == 0)); return 0; } -pub fn operator_lt_0(x: Ptr, y: Ptr) -> bool { - return ({ x.with(|__s| __s.a) } < { y.with(|__s| __s.a) }) - || (({ x.with(|__s| __s.a) } == { y.with(|__s| __s.a) }) - && ({ x.with(|__s| __s.b) } < { y.with(|__s| __s.b) })); +impl LessThanComparable for S { + fn less(x: Ptr, y: Ptr) -> bool { + return ({ x.with(|__s| __s.a) } < { y.with(|__s| __s.a) }) + || (({ x.with(|__s| __s.a) } == { y.with(|__s| __s.a) }) + && ({ x.with(|__s| __s.b) } < { y.with(|__s| __s.b) })); + } } -pub fn compare_1(x: Ptr, y: Ptr) -> i32 { - if ({ operator_lt_0((x).clone(), (y).clone()) }) { +pub fn compare_0(x: Ptr, y: Ptr) -> i32 { + if ({ LessThanComparable::less((x).clone(), (y).clone()) }) { return -1_i32; } - if ({ operator_lt_0((y).clone(), (x).clone()) }) { + if ({ LessThanComparable::less((y).clone(), (x).clone()) }) { return 1; } return 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 21e57ddc31..45beae6e43 100644 --- a/tests/multi-file/friend_operator/out/unsafe/friend_operator.rs +++ b/tests/multi-file/friend_operator/out/unsafe/friend_operator.rs @@ -52,33 +52,35 @@ unsafe fn main_0() -> i32 { assert!( (unsafe { let _x: *const S = &s1; - operator_lt_0(_x, &s3) + LessThanComparableUnsafe::less(_x, &s3) }) ); assert!( !(unsafe { let _x: *const S = &s3; - operator_lt_0(_x, &s1) + LessThanComparableUnsafe::less(_x, &s1) }) ); - assert!(((unsafe { compare_1(&s1, &s3,) }) == (-1_i32))); - assert!(((unsafe { compare_1(&s3, &s1,) }) == (1))); - assert!(((unsafe { compare_1(&s1, &s2,) }) == (0))); + assert!(((unsafe { compare_0(&s1, &s3,) }) == (-1_i32))); + assert!(((unsafe { compare_0(&s3, &s1,) }) == (1))); + assert!(((unsafe { compare_0(&s1, &s2,) }) == (0))); return 0; } -pub unsafe fn operator_lt_0(x: *const S, y: *const S) -> bool { - return (((*x).a) < ((*y).a)) || ((((*x).a) == ((*y).a)) && (((*x).b) < ((*y).b))); +impl LessThanComparableUnsafe for S { + unsafe fn less(x: *const S, y: *const S) -> bool { + return (((*x).a) < ((*y).a)) || ((((*x).a) == ((*y).a)) && (((*x).b) < ((*y).b))); + } } -pub unsafe fn compare_1(x: *const S, y: *const S) -> i32 { +pub unsafe fn compare_0(x: *const S, y: *const S) -> i32 { if (unsafe { let _x: *const S = x; - operator_lt_0(_x, y) + LessThanComparableUnsafe::less(_x, y) }) { return -1_i32; } if (unsafe { let _x: *const S = y; - operator_lt_0(_x, x) + LessThanComparableUnsafe::less(_x, x) }) { return 1; } diff --git a/tests/std/class/class.compare/class.spaceship.cpp b/tests/std/class/class.compare/class.spaceship.cpp new file mode 100644 index 0000000000..ba85755e8d --- /dev/null +++ b/tests/std/class/class.compare/class.spaceship.cpp @@ -0,0 +1,120 @@ +// ADDITIONAL_COMPILE_FLAGS: -std=c++23 +#include +#include +#include + +// N4950 [class.spaceship]/1: a defaulted operator<=> compares the members in +// declaration order, and the first that differs decides. +namespace p1_memberwise_in_declaration_order { +struct Date { + int year; + int month; + int day; + std::strong_ordering operator<=>(const Date &) const = default; +}; + +void test() { + Date earlier{2020, 5, 17}; + Date later_day{2020, 5, 18}; + Date later_year{2021, 1, 1}; + assert((earlier <=> later_day) < 0); + assert((later_year <=> later_day) > 0); + assert((earlier <=> earlier) == 0); + assert(earlier < later_day); + assert(later_year > earlier); + assert(earlier <= earlier); + assert(later_year >= later_day); + assert(earlier == earlier); + assert(earlier != later_day); +} +} // namespace p1_memberwise_in_declaration_order + +// N4950 [class.spaceship]/2: with a return type of auto, the result type is +// the common comparison category of the members, here partial_ordering. +namespace p2_common_comparison_category { +struct Reading { + int sensor; + double value; + auto operator<=>(const Reading &) const = default; +}; + +void test() { + Reading low{1, 0.5}; + Reading high{1, 2.5}; + std::partial_ordering order = low <=> high; + assert(order < 0); + assert(low < high); + assert(high > low); + assert(low <= low); + assert(high >= low); +} +} // namespace p2_common_comparison_category + +// N4950 [over.match.oper]/3.4: the relational operators of a type whose +// operator<=> returns weak_ordering are rewritten to use it. +namespace weak_ordering_from_user_operator { +struct Fraction { + int numerator; + int denominator; + std::weak_ordering operator<=>(const Fraction &other) const { + return numerator * other.denominator <=> other.numerator * denominator; + } +}; + +void test() { + Fraction half{1, 2}; + Fraction two_quarters{2, 4}; + Fraction third{1, 3}; + assert((half <=> two_quarters) == 0); + assert((third <=> half) < 0); + assert(third < half); + assert(half > third); + assert(half <= two_quarters); + assert(half >= two_quarters); + assert(std::max(third, half).denominator == 2); + assert(std::min(third, half).denominator == 3); +} +} // namespace weak_ordering_from_user_operator + +// N4950 [over.match.oper]/3.4: a partial_ordering operator<=> makes every +// relational operator false for unordered operands. +namespace partial_ordering_from_user_operator { +struct Interval { + int low; + int high; + std::partial_ordering operator<=>(const Interval &other) const { + if (high < other.low) { + return std::partial_ordering::less; + } + if (other.high < low) { + return std::partial_ordering::greater; + } + if (low == other.low && high == other.high) { + return std::partial_ordering::equivalent; + } + return std::partial_ordering::unordered; + } +}; + +void test() { + Interval early{1, 3}; + Interval late{5, 8}; + Interval overlapping{2, 6}; + assert(early < late); + assert(late > early); + assert(early <= early); + assert(!(early < overlapping)); + assert(!(early > overlapping)); + assert(!(early <= overlapping)); + assert(!(early >= overlapping)); + assert((early <=> overlapping) == std::partial_ordering::unordered); +} +} // namespace partial_ordering_from_user_operator + +int main() { + p1_memberwise_in_declaration_order::test(); + p2_common_comparison_category::test(); + weak_ordering_from_user_operator::test(); + partial_ordering_from_user_operator::test(); + return 0; +} diff --git a/tests/std/class/class.compare/out/refcount/class.spaceship.rs b/tests/std/class/class.compare/out/refcount/class.spaceship.rs new file mode 100644 index 0000000000..22e6dc46f0 --- /dev/null +++ b/tests/std/class/class.compare/out/refcount/class.spaceship.rs @@ -0,0 +1,498 @@ +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, + TriviallyDefaultConstructible, + TriviallyCopyable, + Default, + PartiallyOrderedWith, +)] +#[three_way(ThreeWayComparable::three_way)] +#[byte_size(12)] +pub struct p1_memberwise_in_declaration_order_Date { + #[offset(0)] + pub year: i32, + #[offset(4)] + pub month: i32, + #[offset(8)] + pub day: i32, +} +impl ThreeWayComparable for p1_memberwise_in_declaration_order_Date { + type Category = libcc2rs::StrongOrdering; + fn three_way( + this: Ptr, + _a0: Ptr, + ) -> libcc2rs::StrongOrdering { + { + let cmp: Value = + Rc::new(RefCell::new(libcc2rs::StrongOrdering::from( + std::cmp::Ord::cmp(&(this.with(|__s| __s.year)), &(_a0.with(|__s| __s.year))), + ))); + if !(libcc2rs::is_eq(({ (*cmp.borrow()) }))) { + return (*cmp.borrow_mut()); + } + } + { + let cmp: Value = + Rc::new(RefCell::new(libcc2rs::StrongOrdering::from( + std::cmp::Ord::cmp(&(this.with(|__s| __s.month)), &(_a0.with(|__s| __s.month))), + ))); + if !(libcc2rs::is_eq(({ (*cmp.borrow()) }))) { + return (*cmp.borrow_mut()); + } + } + { + let cmp: Value = + Rc::new(RefCell::new(libcc2rs::StrongOrdering::from( + std::cmp::Ord::cmp(&(this.with(|__s| __s.day)), &(_a0.with(|__s| __s.day))), + ))); + if !(libcc2rs::is_eq(({ (*cmp.borrow()) }))) { + return (*cmp.borrow_mut()); + } + } + return (*libcc2rs::strong_ordering_equal().upgrade().deref()); + } +} +impl LessThanComparable for p1_memberwise_in_declaration_order_Date { + fn less(this: Ptr, _a0: Ptr) -> bool { + libcc2rs::is_lt(ThreeWayComparable::three_way(this.clone(), _a0.clone())) + } +} +pub fn test_0() { + let earlier: Value = + Rc::new(RefCell::new(p1_memberwise_in_declaration_order_Date { + year: 2020, + month: 5, + day: 17, + })); + let later_day: Value = + Rc::new(RefCell::new(p1_memberwise_in_declaration_order_Date { + year: 2020, + month: 5, + day: 18, + })); + let later_year: Value = + Rc::new(RefCell::new(p1_memberwise_in_declaration_order_Date { + year: 2021, + month: 1, + day: 1, + })); + assert!(libcc2rs::is_lt( + ({ ThreeWayComparable::three_way(earlier.as_pointer(), later_day.as_pointer(),) }) + )); + assert!(libcc2rs::is_gt( + ({ ThreeWayComparable::three_way(later_year.as_pointer(), later_day.as_pointer(),) }) + )); + assert!(libcc2rs::is_eq( + ({ ThreeWayComparable::three_way(earlier.as_pointer(), earlier.as_pointer(),) }) + )); + assert!(libcc2rs::is_lt( + ({ ThreeWayComparable::three_way(earlier.as_pointer(), later_day.as_pointer(),) }) + )); + assert!(libcc2rs::is_gt( + ({ ThreeWayComparable::three_way(later_year.as_pointer(), earlier.as_pointer(),) }) + )); + assert!(libcc2rs::is_lteq( + ({ ThreeWayComparable::three_way(earlier.as_pointer(), earlier.as_pointer(),) }) + )); + assert!(libcc2rs::is_gteq( + ({ ThreeWayComparable::three_way(later_year.as_pointer(), later_day.as_pointer(),) }) + )); + assert!(({ WeaklyEqualityComparableWith::equal(earlier.as_pointer(), earlier.as_pointer(),) })); + assert!( + !({ WeaklyEqualityComparableWith::equal(earlier.as_pointer(), later_day.as_pointer(),) }) + ); +} +#[derive( + Copy, + Clone, + Record, + ByteRepr, + ZeroInitialization, + VaArg, + FnPtrArg, + TriviallyDefaultConstructible, + TriviallyCopyable, + Default, + PartiallyOrderedWith, +)] +#[three_way(ThreeWayComparable::three_way)] +#[byte_size(16)] +pub struct p2_common_comparison_category_Reading { + #[offset(0)] + pub sensor: i32, + #[offset(8)] + pub value: f64, +} +impl ThreeWayComparable for p2_common_comparison_category_Reading { + type Category = libcc2rs::PartialOrdering; + fn three_way( + this: Ptr, + _a0: Ptr, + ) -> libcc2rs::PartialOrdering { + { + let cmp: Value = + Rc::new(RefCell::new(libcc2rs::PartialOrdering::from( + libcc2rs::StrongOrdering::from(std::cmp::Ord::cmp( + &(this.with(|__s| __s.sensor)), + &(_a0.with(|__s| __s.sensor)), + )), + ))); + if !(libcc2rs::is_eq(({ (*cmp.borrow()) }))) { + return (*cmp.borrow_mut()); + } + } + { + let cmp: Value = Rc::new(RefCell::new( + libcc2rs::PartialOrdering::from(std::cmp::PartialOrd::partial_cmp( + &(this.with(|__s| __s.value)), + &(_a0.with(|__s| __s.value)), + )), + )); + if !(libcc2rs::is_eq(({ (*cmp.borrow()) }))) { + return (*cmp.borrow_mut()); + } + } + return libcc2rs::PartialOrdering::from( + ({ (*libcc2rs::strong_ordering_equal().upgrade().deref()) }), + ); + } +} +impl LessThanComparable for p2_common_comparison_category_Reading { + fn less(this: Ptr, _a0: Ptr) -> bool { + libcc2rs::is_lt(ThreeWayComparable::three_way(this.clone(), _a0.clone())) + } +} +pub fn test_1() { + let low: Value = + Rc::new(RefCell::new(p2_common_comparison_category_Reading { + sensor: 1, + value: 0.5_f64, + })); + let high: Value = + Rc::new(RefCell::new(p2_common_comparison_category_Reading { + sensor: 1, + value: 2.5_f64, + })); + let order: Value = Rc::new(RefCell::new( + ({ ThreeWayComparable::three_way(low.as_pointer(), high.as_pointer()) }), + )); + assert!(libcc2rs::is_lt(({ (*order.borrow()) }))); + assert!(libcc2rs::is_lt( + ({ ThreeWayComparable::three_way(low.as_pointer(), high.as_pointer(),) }) + )); + assert!(libcc2rs::is_gt( + ({ ThreeWayComparable::three_way(high.as_pointer(), low.as_pointer(),) }) + )); + assert!(libcc2rs::is_lteq( + ({ ThreeWayComparable::three_way(low.as_pointer(), low.as_pointer(),) }) + )); + assert!(libcc2rs::is_gteq( + ({ ThreeWayComparable::three_way(high.as_pointer(), low.as_pointer(),) }) + )); +} +#[derive( + Copy, + Clone, + Record, + ByteRepr, + ZeroInitialization, + VaArg, + FnPtrArg, + TriviallyDefaultConstructible, + TriviallyCopyable, + Default, + PartiallyOrderedWith, +)] +#[three_way(weak_ordering_from_user_operator_Fraction::operator_cmp)] +#[byte_size(8)] +pub struct weak_ordering_from_user_operator_Fraction { + #[offset(0)] + pub numerator: i32, + #[offset(4)] + pub denominator: i32, +} +impl weak_ordering_from_user_operator_Fraction { + pub fn operator_cmp( + this: Ptr, + other: Ptr, + ) -> libcc2rs::WeakOrdering { + return libcc2rs::WeakOrdering::from(libcc2rs::StrongOrdering::from(std::cmp::Ord::cmp( + &({ this.with(|__s| __s.numerator) } * { other.with(|__s| __s.denominator) }), + &({ other.with(|__s| __s.numerator) } * { this.with(|__s| __s.denominator) }), + ))); + } +} +impl LessThanComparable for weak_ordering_from_user_operator_Fraction { + fn less(this: Ptr, other: Ptr) -> bool { + libcc2rs::is_lt(weak_ordering_from_user_operator_Fraction::operator_cmp( + this.clone(), + other.clone(), + )) + } +} +pub fn test_2() { + let half: Value = + Rc::new(RefCell::new(weak_ordering_from_user_operator_Fraction { + numerator: 1, + denominator: 2, + })); + let two_quarters: Value = + Rc::new(RefCell::new(weak_ordering_from_user_operator_Fraction { + numerator: 2, + denominator: 4, + })); + let third: Value = + Rc::new(RefCell::new(weak_ordering_from_user_operator_Fraction { + numerator: 1, + denominator: 3, + })); + assert!(libcc2rs::is_eq( + ({ + weak_ordering_from_user_operator_Fraction::operator_cmp( + half.as_pointer(), + two_quarters.as_pointer(), + ) + }) + )); + assert!(libcc2rs::is_lt( + ({ + weak_ordering_from_user_operator_Fraction::operator_cmp( + third.as_pointer(), + half.as_pointer(), + ) + }) + )); + assert!(libcc2rs::is_lt( + ({ + weak_ordering_from_user_operator_Fraction::operator_cmp( + third.as_pointer(), + half.as_pointer(), + ) + }) + )); + assert!(libcc2rs::is_gt( + ({ + weak_ordering_from_user_operator_Fraction::operator_cmp( + half.as_pointer(), + third.as_pointer(), + ) + }) + )); + assert!(libcc2rs::is_lteq( + ({ + weak_ordering_from_user_operator_Fraction::operator_cmp( + half.as_pointer(), + two_quarters.as_pointer(), + ) + }) + )); + assert!(libcc2rs::is_gteq( + ({ + weak_ordering_from_user_operator_Fraction::operator_cmp( + half.as_pointer(), + two_quarters.as_pointer(), + ) + }) + )); + assert!( + (if LessThanComparable::less(third.as_pointer().clone(), half.as_pointer().clone()) { + half.as_pointer() + } else { + third.as_pointer() + } + .with(|__s| __s.denominator) + == 2) + ); + assert!( + (if LessThanComparable::less(half.as_pointer().clone(), third.as_pointer().clone()) { + half.as_pointer() + } else { + third.as_pointer() + } + .with(|__s| __s.denominator) + == 3) + ); +} +#[derive( + Copy, + Clone, + Record, + ByteRepr, + ZeroInitialization, + VaArg, + FnPtrArg, + TriviallyDefaultConstructible, + TriviallyCopyable, + Default, + PartiallyOrderedWith, +)] +#[three_way(partial_ordering_from_user_operator_Interval::operator_cmp)] +#[byte_size(8)] +pub struct partial_ordering_from_user_operator_Interval { + #[offset(0)] + pub low: i32, + #[offset(4)] + pub high: i32, +} +impl partial_ordering_from_user_operator_Interval { + pub fn operator_cmp( + this: Ptr, + other: Ptr, + ) -> libcc2rs::PartialOrdering { + if ({ this.with(|__s| __s.high) } < { other.with(|__s| __s.low) }) { + return (*libcc2rs::partial_ordering_less().upgrade().deref()); + } + if ({ other.with(|__s| __s.high) } < { this.with(|__s| __s.low) }) { + return (*libcc2rs::partial_ordering_greater().upgrade().deref()); + } + if ({ this.with(|__s| __s.low) } == { other.with(|__s| __s.low) }) + && ({ this.with(|__s| __s.high) } == { other.with(|__s| __s.high) }) + { + return (*libcc2rs::partial_ordering_equivalent().upgrade().deref()); + } + return (*libcc2rs::partial_ordering_unordered().upgrade().deref()); + } +} +impl LessThanComparable for partial_ordering_from_user_operator_Interval { + fn less(this: Ptr, other: Ptr) -> bool { + libcc2rs::is_lt(partial_ordering_from_user_operator_Interval::operator_cmp( + this.clone(), + other.clone(), + )) + } +} +pub fn test_3() { + let early: Value = + Rc::new(RefCell::new(partial_ordering_from_user_operator_Interval { + low: 1, + high: 3, + })); + let late: Value = + Rc::new(RefCell::new(partial_ordering_from_user_operator_Interval { + low: 5, + high: 8, + })); + let overlapping: Value = + Rc::new(RefCell::new(partial_ordering_from_user_operator_Interval { + low: 2, + high: 6, + })); + assert!(libcc2rs::is_lt( + ({ + partial_ordering_from_user_operator_Interval::operator_cmp( + early.as_pointer(), + late.as_pointer(), + ) + }) + )); + assert!(libcc2rs::is_gt( + ({ + partial_ordering_from_user_operator_Interval::operator_cmp( + late.as_pointer(), + early.as_pointer(), + ) + }) + )); + assert!(libcc2rs::is_lteq( + ({ + partial_ordering_from_user_operator_Interval::operator_cmp( + early.as_pointer(), + early.as_pointer(), + ) + }) + )); + assert!( + !(libcc2rs::is_lt( + ({ + partial_ordering_from_user_operator_Interval::operator_cmp( + early.as_pointer(), + overlapping.as_pointer(), + ) + }) + )) + ); + assert!( + !(libcc2rs::is_gt( + ({ + partial_ordering_from_user_operator_Interval::operator_cmp( + early.as_pointer(), + overlapping.as_pointer(), + ) + }) + )) + ); + assert!( + !(libcc2rs::is_lteq( + ({ + partial_ordering_from_user_operator_Interval::operator_cmp( + early.as_pointer(), + overlapping.as_pointer(), + ) + }) + )) + ); + assert!( + !(libcc2rs::is_gteq( + ({ + partial_ordering_from_user_operator_Interval::operator_cmp( + early.as_pointer(), + overlapping.as_pointer(), + ) + }) + )) + ); + assert!({ + let __a = libcc2rs::PartialOrdering::from( + ({ + partial_ordering_from_user_operator_Interval::operator_cmp( + early.as_pointer(), + overlapping.as_pointer(), + ) + }), + ); + let __b = libcc2rs::PartialOrdering::from( + ({ (*libcc2rs::partial_ordering_unordered().upgrade().deref()) }), + ); + libcc2rs::is_lt(__a) == libcc2rs::is_lt(__b) + && libcc2rs::is_eq(__a) == libcc2rs::is_eq(__b) + && libcc2rs::is_gt(__a) == libcc2rs::is_gt(__b) + }); +} +pub fn main() { + __cpp2rust_init_globals(); + libcc2rs::cpp2rust_exit(main_0()); +} +fn main_0() -> i32 { + ({ test_0() }); + ({ test_1() }); + ({ test_2() }); + ({ test_3() }); + return 0; +} +impl WeaklyEqualityComparableWith for p1_memberwise_in_declaration_order_Date { + fn equal(this: Ptr, _a0: Ptr) -> bool { + return ({ this.with(|__s| __s.year) } == { _a0.with(|__s| __s.year) }) + && (({ this.with(|__s| __s.month) } == { _a0.with(|__s| __s.month) }) + && ({ this.with(|__s| __s.day) } == { _a0.with(|__s| __s.day) })); + } +} +impl WeaklyEqualityComparableWith for p2_common_comparison_category_Reading { + fn equal(this: Ptr, _a0: Ptr) -> bool { + return ({ this.with(|__s| __s.sensor) } == { _a0.with(|__s| __s.sensor) }) + && ({ this.with(|__s| __s.value) } == { _a0.with(|__s| __s.value) }); + } +} +pub fn __cpp2rust_init_globals() {} diff --git a/tests/std/class/class.compare/out/unsafe/class.spaceship.rs b/tests/std/class/class.compare/out/unsafe/class.spaceship.rs new file mode 100644 index 0000000000..ce52094a51 --- /dev/null +++ b/tests/std/class/class.compare/out/unsafe/class.spaceship.rs @@ -0,0 +1,449 @@ +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, + TriviallyDefaultConstructibleUnsafe, + TriviallyCopyableUnsafe, + Default, + PartiallyOrderedWithUnsafe, +)] +#[three_way(ThreeWayComparableUnsafe::three_way)] +pub struct p1_memberwise_in_declaration_order_Date { + pub year: i32, + pub month: i32, + pub day: i32, +} +impl ThreeWayComparableUnsafe for p1_memberwise_in_declaration_order_Date { + type Category = libcc2rs::StrongOrdering; + unsafe fn three_way( + this: *const Self, + _a0: *const p1_memberwise_in_declaration_order_Date, + ) -> libcc2rs::StrongOrdering { + { + let mut cmp: libcc2rs::StrongOrdering = + libcc2rs::StrongOrdering::from(std::cmp::Ord::cmp(&((*this).year), &((*_a0).year))); + if !(libcc2rs::is_eq(cmp)) { + return cmp; + } + } + { + let mut cmp: libcc2rs::StrongOrdering = libcc2rs::StrongOrdering::from( + std::cmp::Ord::cmp(&((*this).month), &((*_a0).month)), + ); + if !(libcc2rs::is_eq(cmp)) { + return cmp; + } + } + { + let mut cmp: libcc2rs::StrongOrdering = + libcc2rs::StrongOrdering::from(std::cmp::Ord::cmp(&((*this).day), &((*_a0).day))); + if !(libcc2rs::is_eq(cmp)) { + return cmp; + } + } + return libcc2rs::STRONG_ORDERING_EQUAL; + } +} +impl LessThanComparableUnsafe for p1_memberwise_in_declaration_order_Date { + unsafe fn less(this: *const Self, _a0: *const Self) -> bool { + libcc2rs::is_lt(ThreeWayComparableUnsafe::three_way(this, _a0)) + } +} +pub unsafe fn test_0() { + let mut earlier: p1_memberwise_in_declaration_order_Date = + p1_memberwise_in_declaration_order_Date { + year: 2020, + month: 5, + day: 17, + }; + let mut later_day: p1_memberwise_in_declaration_order_Date = + p1_memberwise_in_declaration_order_Date { + year: 2020, + month: 5, + day: 18, + }; + let mut later_year: p1_memberwise_in_declaration_order_Date = + p1_memberwise_in_declaration_order_Date { + year: 2021, + month: 1, + day: 1, + }; + assert!(libcc2rs::is_lt( + (unsafe { ThreeWayComparableUnsafe::three_way(&raw const earlier, &later_day,) }) + )); + assert!(libcc2rs::is_gt( + (unsafe { ThreeWayComparableUnsafe::three_way(&raw const later_year, &later_day,) }) + )); + assert!(libcc2rs::is_eq( + (unsafe { + let _arg0: *const p1_memberwise_in_declaration_order_Date = &earlier; + ThreeWayComparableUnsafe::three_way(&raw const earlier, _arg0) + }) + )); + assert!(libcc2rs::is_lt( + (unsafe { ThreeWayComparableUnsafe::three_way(&raw const earlier, &later_day,) }) + )); + assert!(libcc2rs::is_gt( + (unsafe { ThreeWayComparableUnsafe::three_way(&raw const later_year, &earlier,) }) + )); + assert!(libcc2rs::is_lteq( + (unsafe { + let _arg0: *const p1_memberwise_in_declaration_order_Date = &earlier; + ThreeWayComparableUnsafe::three_way(&raw const earlier, _arg0) + }) + )); + assert!(libcc2rs::is_gteq( + (unsafe { ThreeWayComparableUnsafe::three_way(&raw const later_year, &later_day,) }) + )); + assert!( + (unsafe { + let _arg0: *const p1_memberwise_in_declaration_order_Date = &earlier; + WeaklyEqualityComparableWithUnsafe::equal(&raw const earlier, _arg0) + }) + ); + assert!( + !(unsafe { WeaklyEqualityComparableWithUnsafe::equal(&raw const earlier, &later_day,) }) + ); +} +#[repr(C)] +#[derive( + Copy, + Clone, + VaArg, + FnPtrArg, + ZeroInitializationUnsafe, + TriviallyDefaultConstructibleUnsafe, + TriviallyCopyableUnsafe, + Default, + PartiallyOrderedWithUnsafe, +)] +#[three_way(ThreeWayComparableUnsafe::three_way)] +pub struct p2_common_comparison_category_Reading { + pub sensor: i32, + pub value: f64, +} +impl ThreeWayComparableUnsafe for p2_common_comparison_category_Reading { + type Category = libcc2rs::PartialOrdering; + unsafe fn three_way( + this: *const Self, + _a0: *const p2_common_comparison_category_Reading, + ) -> libcc2rs::PartialOrdering { + { + let mut cmp: libcc2rs::PartialOrdering = + libcc2rs::PartialOrdering::from(libcc2rs::StrongOrdering::from( + std::cmp::Ord::cmp(&((*this).sensor), &((*_a0).sensor)), + )); + if !(libcc2rs::is_eq(cmp)) { + return cmp; + } + } + { + let mut cmp: libcc2rs::PartialOrdering = libcc2rs::PartialOrdering::from( + std::cmp::PartialOrd::partial_cmp(&((*this).value), &((*_a0).value)), + ); + if !(libcc2rs::is_eq(cmp)) { + return cmp; + } + } + return libcc2rs::PartialOrdering::from(libcc2rs::STRONG_ORDERING_EQUAL); + } +} +impl LessThanComparableUnsafe for p2_common_comparison_category_Reading { + unsafe fn less(this: *const Self, _a0: *const Self) -> bool { + libcc2rs::is_lt(ThreeWayComparableUnsafe::three_way(this, _a0)) + } +} +pub unsafe fn test_1() { + let mut low: p2_common_comparison_category_Reading = p2_common_comparison_category_Reading { + sensor: 1, + value: 0.5_f64, + }; + let mut high: p2_common_comparison_category_Reading = p2_common_comparison_category_Reading { + sensor: 1, + value: 2.5_f64, + }; + let mut order: libcc2rs::PartialOrdering = + (unsafe { ThreeWayComparableUnsafe::three_way(&raw const low, &high) }); + assert!(libcc2rs::is_lt(order)); + assert!(libcc2rs::is_lt( + (unsafe { ThreeWayComparableUnsafe::three_way(&raw const low, &high,) }) + )); + assert!(libcc2rs::is_gt( + (unsafe { ThreeWayComparableUnsafe::three_way(&raw const high, &low,) }) + )); + assert!(libcc2rs::is_lteq( + (unsafe { + let _arg0: *const p2_common_comparison_category_Reading = &low; + ThreeWayComparableUnsafe::three_way(&raw const low, _arg0) + }) + )); + assert!(libcc2rs::is_gteq( + (unsafe { ThreeWayComparableUnsafe::three_way(&raw const high, &low,) }) + )); +} +#[repr(C)] +#[derive( + Copy, + Clone, + VaArg, + FnPtrArg, + ZeroInitializationUnsafe, + TriviallyDefaultConstructibleUnsafe, + TriviallyCopyableUnsafe, + Default, + PartiallyOrderedWithUnsafe, +)] +#[three_way(weak_ordering_from_user_operator_Fraction::operator_cmp)] +pub struct weak_ordering_from_user_operator_Fraction { + pub numerator: i32, + pub denominator: i32, +} +impl weak_ordering_from_user_operator_Fraction { + pub unsafe fn operator_cmp( + this: *const Self, + other: *const weak_ordering_from_user_operator_Fraction, + ) -> libcc2rs::WeakOrdering { + return libcc2rs::WeakOrdering::from(libcc2rs::StrongOrdering::from(std::cmp::Ord::cmp( + &(((*this).numerator) * ((*other).denominator)), + &(((*other).numerator) * ((*this).denominator)), + ))); + } +} +impl LessThanComparableUnsafe for weak_ordering_from_user_operator_Fraction { + unsafe fn less(this: *const Self, other: *const Self) -> bool { + libcc2rs::is_lt(weak_ordering_from_user_operator_Fraction::operator_cmp( + this, other, + )) + } +} +pub unsafe fn test_2() { + let mut half: weak_ordering_from_user_operator_Fraction = + weak_ordering_from_user_operator_Fraction { + numerator: 1, + denominator: 2, + }; + let mut two_quarters: weak_ordering_from_user_operator_Fraction = + weak_ordering_from_user_operator_Fraction { + numerator: 2, + denominator: 4, + }; + let mut third: weak_ordering_from_user_operator_Fraction = + weak_ordering_from_user_operator_Fraction { + numerator: 1, + denominator: 3, + }; + assert!(libcc2rs::is_eq( + (unsafe { + weak_ordering_from_user_operator_Fraction::operator_cmp(&raw const half, &two_quarters) + }) + )); + assert!(libcc2rs::is_lt( + (unsafe { + weak_ordering_from_user_operator_Fraction::operator_cmp(&raw const third, &half) + }) + )); + assert!(libcc2rs::is_lt( + (unsafe { + weak_ordering_from_user_operator_Fraction::operator_cmp(&raw const third, &half) + }) + )); + assert!(libcc2rs::is_gt( + (unsafe { + weak_ordering_from_user_operator_Fraction::operator_cmp(&raw const half, &third) + }) + )); + assert!(libcc2rs::is_lteq( + (unsafe { + weak_ordering_from_user_operator_Fraction::operator_cmp(&raw const half, &two_quarters) + }) + )); + assert!(libcc2rs::is_gteq( + (unsafe { + weak_ordering_from_user_operator_Fraction::operator_cmp(&raw const half, &two_quarters) + }) + )); + assert!( + (((*{ + let __a: *const _ = &mut third; + let __b: *const _ = &mut half; + if LessThanComparableUnsafe::less(__a, __b) { + __b + } else { + __a + } + }) + .denominator) + == (2)) + ); + assert!( + (((*{ + let __a: *const _ = &mut third; + let __b: *const _ = &mut half; + if LessThanComparableUnsafe::less(__b, __a) { + __b + } else { + __a + } + }) + .denominator) + == (3)) + ); +} +#[repr(C)] +#[derive( + Copy, + Clone, + VaArg, + FnPtrArg, + ZeroInitializationUnsafe, + TriviallyDefaultConstructibleUnsafe, + TriviallyCopyableUnsafe, + Default, + PartiallyOrderedWithUnsafe, +)] +#[three_way(partial_ordering_from_user_operator_Interval::operator_cmp)] +pub struct partial_ordering_from_user_operator_Interval { + pub low: i32, + pub high: i32, +} +impl partial_ordering_from_user_operator_Interval { + pub unsafe fn operator_cmp( + this: *const Self, + other: *const partial_ordering_from_user_operator_Interval, + ) -> libcc2rs::PartialOrdering { + if (((*this).high) < ((*other).low)) { + return libcc2rs::PARTIAL_ORDERING_LESS; + } + if (((*other).high) < ((*this).low)) { + return libcc2rs::PARTIAL_ORDERING_GREATER; + } + if (((*this).low) == ((*other).low)) && (((*this).high) == ((*other).high)) { + return libcc2rs::PARTIAL_ORDERING_EQUIVALENT; + } + return libcc2rs::PARTIAL_ORDERING_UNORDERED; + } +} +impl LessThanComparableUnsafe for partial_ordering_from_user_operator_Interval { + unsafe fn less(this: *const Self, other: *const Self) -> bool { + libcc2rs::is_lt(partial_ordering_from_user_operator_Interval::operator_cmp( + this, other, + )) + } +} +pub unsafe fn test_3() { + let mut early: partial_ordering_from_user_operator_Interval = + partial_ordering_from_user_operator_Interval { low: 1, high: 3 }; + let mut late: partial_ordering_from_user_operator_Interval = + partial_ordering_from_user_operator_Interval { low: 5, high: 8 }; + let mut overlapping: partial_ordering_from_user_operator_Interval = + partial_ordering_from_user_operator_Interval { low: 2, high: 6 }; + assert!(libcc2rs::is_lt( + (unsafe { + partial_ordering_from_user_operator_Interval::operator_cmp(&raw const early, &late) + }) + )); + assert!(libcc2rs::is_gt( + (unsafe { + partial_ordering_from_user_operator_Interval::operator_cmp(&raw const late, &early) + }) + )); + assert!(libcc2rs::is_lteq( + (unsafe { + let _other: *const partial_ordering_from_user_operator_Interval = &early; + partial_ordering_from_user_operator_Interval::operator_cmp(&raw const early, _other) + }) + )); + assert!( + !(libcc2rs::is_lt( + (unsafe { + partial_ordering_from_user_operator_Interval::operator_cmp( + &raw const early, + &overlapping, + ) + }) + )) + ); + assert!( + !(libcc2rs::is_gt( + (unsafe { + partial_ordering_from_user_operator_Interval::operator_cmp( + &raw const early, + &overlapping, + ) + }) + )) + ); + assert!( + !(libcc2rs::is_lteq( + (unsafe { + partial_ordering_from_user_operator_Interval::operator_cmp( + &raw const early, + &overlapping, + ) + }) + )) + ); + assert!( + !(libcc2rs::is_gteq( + (unsafe { + partial_ordering_from_user_operator_Interval::operator_cmp( + &raw const early, + &overlapping, + ) + }) + )) + ); + assert!({ + let __a = libcc2rs::PartialOrdering::from( + (unsafe { + partial_ordering_from_user_operator_Interval::operator_cmp( + &raw const early, + &overlapping, + ) + }), + ); + let __b = libcc2rs::PartialOrdering::from(libcc2rs::PARTIAL_ORDERING_UNORDERED); + libcc2rs::is_lt(__a) == libcc2rs::is_lt(__b) + && libcc2rs::is_eq(__a) == libcc2rs::is_eq(__b) + && libcc2rs::is_gt(__a) == libcc2rs::is_gt(__b) + }); +} +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() }); + (unsafe { test_2() }); + (unsafe { test_3() }); + return 0; +} +impl WeaklyEqualityComparableWithUnsafe for p1_memberwise_in_declaration_order_Date { + unsafe fn equal( + this: *const Self, + _a0: *const p1_memberwise_in_declaration_order_Date, + ) -> bool { + return (((*this).year) == ((*_a0).year)) + && ((((*this).month) == ((*_a0).month)) && (((*this).day) == ((*_a0).day))); + } +} +impl WeaklyEqualityComparableWithUnsafe for p2_common_comparison_category_Reading { + unsafe fn equal(this: *const Self, _a0: *const p2_common_comparison_category_Reading) -> bool { + return (((*this).sensor) == ((*_a0).sensor)) && (((*this).value) == ((*_a0).value)); + } +} +pub unsafe fn __cpp2rust_init_globals() {} diff --git a/tests/std/concepts/concepts.compare/concept.totallyordered.cpp b/tests/std/concepts/concepts.compare/concept.totallyordered.cpp new file mode 100644 index 0000000000..193ed378c2 --- /dev/null +++ b/tests/std/concepts/concepts.compare/concept.totallyordered.cpp @@ -0,0 +1,101 @@ +// ADDITIONAL_COMPILE_FLAGS: -std=c++23 +#include +#include + +// N4950 [concept.totallyordered]/1: t < u, t > u, t <= u and t >= u are each +// their own expression, and a type may define each of them. +namespace p1_each_relation_is_an_operator { +static int greater_calls = 0; +static int less_equal_calls = 0; +static int greater_equal_calls = 0; + +struct Score { + int points; + bool operator==(const Score &other) const { return points == other.points; } + bool operator<(const Score &other) const { return points < other.points; } + bool operator>(const Score &other) const { + ++greater_calls; + return points > other.points; + } + bool operator<=(const Score &other) const { + ++less_equal_calls; + return points <= other.points; + } + bool operator>=(const Score &other) const { + ++greater_equal_calls; + return points >= other.points; + } +}; + +void test() { + Score low{10}; + Score high{20}; + assert(low < high); + assert(high > low); + assert(low <= high); + assert(low <= low); + assert(high >= low); + assert(!(low >= high)); + assert(greater_calls == 1); + assert(less_equal_calls == 2); + assert(greater_equal_calls == 2); +} +} // namespace p1_each_relation_is_an_operator + +// N4950 [concept.totallyordered]/1, [over.match.oper]/3.4: a type with only +// operator<=> has the four relations, each rewritten to a comparison of the +// result of <=> with zero. +namespace p1_relations_from_three_way { +struct Level { + int depth; + bool operator==(const Level &other) const { return depth == other.depth; } + std::strong_ordering operator<=>(const Level &other) const { + return depth <=> other.depth; + } +}; + +void test() { + Level shallow{1}; + Level deep{5}; + assert(shallow < deep); + assert(deep > shallow); + assert(shallow <= deep); + assert(shallow <= shallow); + assert(deep >= shallow); + assert(!(shallow >= deep)); +} +} // namespace p1_relations_from_three_way + +// N4950 [concept.totallyordered]/1: an operator that the type defines is used +// for its relation, and <=> for the others. +namespace p1_operator_preferred_over_three_way { +static int greater_calls = 0; + +struct Level { + int depth; + std::strong_ordering operator<=>(const Level &other) const { + return depth <=> other.depth; + } + bool operator>(const Level &other) const { + ++greater_calls; + return depth > other.depth; + } +}; + +void test() { + Level shallow{1}; + Level deep{5}; + assert(deep > shallow); + assert(greater_calls == 1); + assert(shallow < deep); + assert(deep >= shallow); + assert(greater_calls == 1); +} +} // namespace p1_operator_preferred_over_three_way + +int main() { + p1_each_relation_is_an_operator::test(); + p1_relations_from_three_way::test(); + p1_operator_preferred_over_three_way::test(); + return 0; +} diff --git a/tests/std/concepts/concepts.compare/out/refcount/concept.totallyordered.rs b/tests/std/concepts/concepts.compare/out/refcount/concept.totallyordered.rs new file mode 100644 index 0000000000..50ccf0d955 --- /dev/null +++ b/tests/std/concepts/concepts.compare/out/refcount/concept.totallyordered.rs @@ -0,0 +1,252 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +thread_local!( + pub static greater_calls_0: Value = Rc::new(RefCell::new(0)); +); +thread_local!( + pub static less_equal_calls_1: Value = Rc::new(RefCell::new(0)); +); +thread_local!( + pub static greater_equal_calls_2: Value = Rc::new(RefCell::new(0)); +); +#[derive( + Copy, + Clone, + Record, + ByteRepr, + ZeroInitialization, + VaArg, + FnPtrArg, + TriviallyDefaultConstructible, + TriviallyCopyable, + Default, +)] +#[byte_size(4)] +pub struct p1_each_relation_is_an_operator_Score { + #[offset(0)] + pub points: i32, +} +impl LessThanComparable for p1_each_relation_is_an_operator_Score { + fn less(this: Ptr, other: Ptr) -> bool { + return ({ this.with(|__s| __s.points) } < { other.with(|__s| __s.points) }); + } +} +pub fn test_3() { + let low: Value = + Rc::new(RefCell::new(p1_each_relation_is_an_operator_Score { + points: 10, + })); + let high: Value = + Rc::new(RefCell::new(p1_each_relation_is_an_operator_Score { + points: 20, + })); + assert!(({ LessThanComparable::less(low.as_pointer(), high.as_pointer(),) })); + assert!(({ PartiallyOrderedWith::greater(high.as_pointer(), low.as_pointer(),) })); + assert!(({ PartiallyOrderedWith::less_equal(low.as_pointer(), high.as_pointer(),) })); + assert!(({ PartiallyOrderedWith::less_equal(low.as_pointer(), low.as_pointer(),) })); + assert!(({ PartiallyOrderedWith::greater_equal(high.as_pointer(), low.as_pointer(),) })); + assert!(!({ PartiallyOrderedWith::greater_equal(low.as_pointer(), high.as_pointer(),) })); + assert!((greater_calls_0.with(|rc| *rc.borrow()) == 1)); + assert!((less_equal_calls_1.with(|rc| *rc.borrow()) == 2)); + assert!((greater_equal_calls_2.with(|rc| *rc.borrow()) == 2)); +} +#[derive( + Copy, + Clone, + Record, + ByteRepr, + ZeroInitialization, + VaArg, + FnPtrArg, + TriviallyDefaultConstructible, + TriviallyCopyable, + Default, + PartiallyOrderedWith, +)] +#[three_way(ThreeWayComparable::three_way)] +#[byte_size(4)] +pub struct p1_relations_from_three_way_Level { + #[offset(0)] + pub depth: i32, +} +impl ThreeWayComparable for p1_relations_from_three_way_Level { + type Category = libcc2rs::StrongOrdering; + fn three_way( + this: Ptr, + other: Ptr, + ) -> libcc2rs::StrongOrdering { + return libcc2rs::StrongOrdering::from(std::cmp::Ord::cmp( + &(this.with(|__s| __s.depth)), + &(other.with(|__s| __s.depth)), + )); + } +} +impl LessThanComparable for p1_relations_from_three_way_Level { + fn less(this: Ptr, other: Ptr) -> bool { + libcc2rs::is_lt(ThreeWayComparable::three_way(this.clone(), other.clone())) + } +} +pub fn test_4() { + let shallow: Value = + Rc::new(RefCell::new(p1_relations_from_three_way_Level { depth: 1 })); + let deep: Value = + Rc::new(RefCell::new(p1_relations_from_three_way_Level { depth: 5 })); + assert!(libcc2rs::is_lt( + ({ ThreeWayComparable::three_way(shallow.as_pointer(), deep.as_pointer(),) }) + )); + assert!(libcc2rs::is_gt( + ({ ThreeWayComparable::three_way(deep.as_pointer(), shallow.as_pointer(),) }) + )); + assert!(libcc2rs::is_lteq( + ({ ThreeWayComparable::three_way(shallow.as_pointer(), deep.as_pointer(),) }) + )); + assert!(libcc2rs::is_lteq( + ({ ThreeWayComparable::three_way(shallow.as_pointer(), shallow.as_pointer(),) }) + )); + assert!(libcc2rs::is_gteq( + ({ ThreeWayComparable::three_way(deep.as_pointer(), shallow.as_pointer(),) }) + )); + assert!( + !(libcc2rs::is_gteq( + ({ ThreeWayComparable::three_way(shallow.as_pointer(), deep.as_pointer(),) }) + )) + ); +} +thread_local!( + pub static greater_calls_5: Value = Rc::new(RefCell::new(0)); +); +#[derive( + Copy, + Clone, + Record, + ByteRepr, + ZeroInitialization, + VaArg, + FnPtrArg, + TriviallyDefaultConstructible, + TriviallyCopyable, + Default, +)] +#[byte_size(4)] +pub struct p1_operator_preferred_over_three_way_Level { + #[offset(0)] + pub depth: i32, +} +impl p1_operator_preferred_over_three_way_Level { + pub fn operator_cmp( + this: Ptr, + other: Ptr, + ) -> libcc2rs::StrongOrdering { + return libcc2rs::StrongOrdering::from(std::cmp::Ord::cmp( + &(this.with(|__s| __s.depth)), + &(other.with(|__s| __s.depth)), + )); + } +} +impl LessThanComparable for p1_operator_preferred_over_three_way_Level { + fn less(this: Ptr, other: Ptr) -> bool { + libcc2rs::is_lt(p1_operator_preferred_over_three_way_Level::operator_cmp( + this.clone(), + other.clone(), + )) + } +} +pub fn test_6() { + let shallow: Value = + Rc::new(RefCell::new(p1_operator_preferred_over_three_way_Level { + depth: 1, + })); + let deep: Value = + Rc::new(RefCell::new(p1_operator_preferred_over_three_way_Level { + depth: 5, + })); + assert!(({ PartiallyOrderedWith::greater(deep.as_pointer(), shallow.as_pointer(),) })); + assert!((greater_calls_5.with(|rc| *rc.borrow()) == 1)); + assert!(libcc2rs::is_lt( + ({ + p1_operator_preferred_over_three_way_Level::operator_cmp( + shallow.as_pointer(), + deep.as_pointer(), + ) + }) + )); + assert!(libcc2rs::is_gteq( + ({ + p1_operator_preferred_over_three_way_Level::operator_cmp( + deep.as_pointer(), + shallow.as_pointer(), + ) + }) + )); + assert!((greater_calls_5.with(|rc| *rc.borrow()) == 1)); +} +pub fn main() { + __cpp2rust_init_globals(); + libcc2rs::cpp2rust_exit(main_0()); +} +fn main_0() -> i32 { + ({ test_3() }); + ({ test_4() }); + ({ test_6() }); + return 0; +} +impl PartiallyOrderedWith for p1_each_relation_is_an_operator_Score { + fn greater(this: Ptr, other: Ptr) -> bool { + (*greater_calls_0.with(Value::clone).borrow_mut()).prefix_inc(); + return ({ this.with(|__s| __s.points) } > { other.with(|__s| __s.points) }); + } + fn less_equal(this: Ptr, other: Ptr) -> bool { + (*less_equal_calls_1.with(Value::clone).borrow_mut()).prefix_inc(); + return ({ this.with(|__s| __s.points) } <= { other.with(|__s| __s.points) }); + } + fn greater_equal(this: Ptr, other: Ptr) -> bool { + (*greater_equal_calls_2.with(Value::clone).borrow_mut()).prefix_inc(); + return ({ this.with(|__s| __s.points) } >= { other.with(|__s| __s.points) }); + } + fn less(lhs: Ptr, rhs: Ptr) -> bool { + ::less(lhs, rhs) + } +} +impl PartiallyOrderedWith for p1_operator_preferred_over_three_way_Level { + fn greater(this: Ptr, other: Ptr) -> bool { + (*greater_calls_5.with(Value::clone).borrow_mut()).prefix_inc(); + return ({ this.with(|__s| __s.depth) } > { other.with(|__s| __s.depth) }); + } + fn less(lhs: Ptr, rhs: Ptr) -> bool { + ::less(lhs, rhs) + } + fn less_equal(lhs: Ptr, rhs: Ptr) -> bool { + libcc2rs::is_lteq(p1_operator_preferred_over_three_way_Level::operator_cmp( + lhs.clone(), + rhs.clone(), + )) + } + fn greater_equal(lhs: Ptr, rhs: Ptr) -> bool { + libcc2rs::is_gteq(p1_operator_preferred_over_three_way_Level::operator_cmp( + lhs.clone(), + rhs.clone(), + )) + } +} +impl WeaklyEqualityComparableWith for p1_each_relation_is_an_operator_Score { + fn equal(this: Ptr, other: Ptr) -> bool { + return ({ this.with(|__s| __s.points) } == { other.with(|__s| __s.points) }); + } +} +impl WeaklyEqualityComparableWith for p1_relations_from_three_way_Level { + fn equal(this: Ptr, other: Ptr) -> bool { + return ({ this.with(|__s| __s.depth) } == { other.with(|__s| __s.depth) }); + } +} +pub fn __cpp2rust_init_globals() { + let _ = greater_calls_0.with(|_| ()); + let _ = less_equal_calls_1.with(|_| ()); + let _ = greater_equal_calls_2.with(|_| ()); + let _ = greater_calls_5.with(|_| ()); +} diff --git a/tests/std/concepts/concepts.compare/out/unsafe/concept.totallyordered.rs b/tests/std/concepts/concepts.compare/out/unsafe/concept.totallyordered.rs new file mode 100644 index 0000000000..7f1cfd2937 --- /dev/null +++ b/tests/std/concepts/concepts.compare/out/unsafe/concept.totallyordered.rs @@ -0,0 +1,247 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +pub static mut greater_calls_0: std::cell::LazyCell = + std::cell::LazyCell::new(|| unsafe { 0 }); +pub static mut less_equal_calls_1: std::cell::LazyCell = + std::cell::LazyCell::new(|| unsafe { 0 }); +pub static mut greater_equal_calls_2: std::cell::LazyCell = + std::cell::LazyCell::new(|| unsafe { 0 }); +#[repr(C)] +#[derive( + Copy, + Clone, + VaArg, + FnPtrArg, + ZeroInitializationUnsafe, + TriviallyDefaultConstructibleUnsafe, + TriviallyCopyableUnsafe, + Default, +)] +pub struct p1_each_relation_is_an_operator_Score { + pub points: i32, +} +impl LessThanComparableUnsafe for p1_each_relation_is_an_operator_Score { + unsafe fn less(this: *const Self, other: *const p1_each_relation_is_an_operator_Score) -> bool { + return (((*this).points) < ((*other).points)); + } +} +pub unsafe fn test_3() { + let mut low: p1_each_relation_is_an_operator_Score = + p1_each_relation_is_an_operator_Score { points: 10 }; + let mut high: p1_each_relation_is_an_operator_Score = + p1_each_relation_is_an_operator_Score { points: 20 }; + assert!((unsafe { LessThanComparableUnsafe::less(&raw const low, &high,) })); + assert!((unsafe { PartiallyOrderedWithUnsafe::greater(&raw const high, &low,) })); + assert!((unsafe { PartiallyOrderedWithUnsafe::less_equal(&raw const low, &high,) })); + assert!( + (unsafe { + let _other: *const p1_each_relation_is_an_operator_Score = &low; + PartiallyOrderedWithUnsafe::less_equal(&raw const low, _other) + }) + ); + assert!((unsafe { PartiallyOrderedWithUnsafe::greater_equal(&raw const high, &low,) })); + assert!(!(unsafe { PartiallyOrderedWithUnsafe::greater_equal(&raw const low, &high,) })); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut greater_calls_0)) == (1))); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut less_equal_calls_1)) == (2))); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut greater_equal_calls_2)) == (2))); +} +#[repr(C)] +#[derive( + Copy, + Clone, + VaArg, + FnPtrArg, + ZeroInitializationUnsafe, + TriviallyDefaultConstructibleUnsafe, + TriviallyCopyableUnsafe, + Default, + PartiallyOrderedWithUnsafe, +)] +#[three_way(ThreeWayComparableUnsafe::three_way)] +pub struct p1_relations_from_three_way_Level { + pub depth: i32, +} +impl ThreeWayComparableUnsafe for p1_relations_from_three_way_Level { + type Category = libcc2rs::StrongOrdering; + unsafe fn three_way( + this: *const Self, + other: *const p1_relations_from_three_way_Level, + ) -> libcc2rs::StrongOrdering { + return libcc2rs::StrongOrdering::from(std::cmp::Ord::cmp( + &((*this).depth), + &((*other).depth), + )); + } +} +impl LessThanComparableUnsafe for p1_relations_from_three_way_Level { + unsafe fn less(this: *const Self, other: *const Self) -> bool { + libcc2rs::is_lt(ThreeWayComparableUnsafe::three_way(this, other)) + } +} +pub unsafe fn test_4() { + let mut shallow: p1_relations_from_three_way_Level = + p1_relations_from_three_way_Level { depth: 1 }; + let mut deep: p1_relations_from_three_way_Level = + p1_relations_from_three_way_Level { depth: 5 }; + assert!(libcc2rs::is_lt( + (unsafe { ThreeWayComparableUnsafe::three_way(&raw const shallow, &deep,) }) + )); + assert!(libcc2rs::is_gt( + (unsafe { ThreeWayComparableUnsafe::three_way(&raw const deep, &shallow,) }) + )); + assert!(libcc2rs::is_lteq( + (unsafe { ThreeWayComparableUnsafe::three_way(&raw const shallow, &deep,) }) + )); + assert!(libcc2rs::is_lteq( + (unsafe { + let _other: *const p1_relations_from_three_way_Level = &shallow; + ThreeWayComparableUnsafe::three_way(&raw const shallow, _other) + }) + )); + assert!(libcc2rs::is_gteq( + (unsafe { ThreeWayComparableUnsafe::three_way(&raw const deep, &shallow,) }) + )); + assert!( + !(libcc2rs::is_gteq( + (unsafe { ThreeWayComparableUnsafe::three_way(&raw const shallow, &deep,) }) + )) + ); +} +pub static mut greater_calls_5: std::cell::LazyCell = + std::cell::LazyCell::new(|| unsafe { 0 }); +#[repr(C)] +#[derive( + Copy, + Clone, + VaArg, + FnPtrArg, + ZeroInitializationUnsafe, + TriviallyDefaultConstructibleUnsafe, + TriviallyCopyableUnsafe, + Default, +)] +pub struct p1_operator_preferred_over_three_way_Level { + pub depth: i32, +} +impl p1_operator_preferred_over_three_way_Level { + pub unsafe fn operator_cmp( + this: *const Self, + other: *const p1_operator_preferred_over_three_way_Level, + ) -> libcc2rs::StrongOrdering { + return libcc2rs::StrongOrdering::from(std::cmp::Ord::cmp( + &((*this).depth), + &((*other).depth), + )); + } +} +impl LessThanComparableUnsafe for p1_operator_preferred_over_three_way_Level { + unsafe fn less(this: *const Self, other: *const Self) -> bool { + libcc2rs::is_lt(p1_operator_preferred_over_three_way_Level::operator_cmp( + this, other, + )) + } +} +pub unsafe fn test_6() { + let mut shallow: p1_operator_preferred_over_three_way_Level = + p1_operator_preferred_over_three_way_Level { depth: 1 }; + let mut deep: p1_operator_preferred_over_three_way_Level = + p1_operator_preferred_over_three_way_Level { depth: 5 }; + assert!((unsafe { PartiallyOrderedWithUnsafe::greater(&raw const deep, &shallow,) })); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut greater_calls_5)) == (1))); + assert!(libcc2rs::is_lt( + (unsafe { + p1_operator_preferred_over_three_way_Level::operator_cmp(&raw const shallow, &deep) + }) + )); + assert!(libcc2rs::is_gteq( + (unsafe { + p1_operator_preferred_over_three_way_Level::operator_cmp(&raw const deep, &shallow) + }) + )); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut greater_calls_5)) == (1))); +} +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + libcc2rs::cpp2rust_exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + (unsafe { test_3() }); + (unsafe { test_4() }); + (unsafe { test_6() }); + return 0; +} +impl PartiallyOrderedWithUnsafe for p1_each_relation_is_an_operator_Score { + unsafe fn greater( + this: *const Self, + other: *const p1_each_relation_is_an_operator_Score, + ) -> bool { + (*std::cell::LazyCell::force_mut(&mut *&raw mut greater_calls_0)).prefix_inc(); + return (((*this).points) > ((*other).points)); + } + unsafe fn less_equal( + this: *const Self, + other: *const p1_each_relation_is_an_operator_Score, + ) -> bool { + (*std::cell::LazyCell::force_mut(&mut *&raw mut less_equal_calls_1)).prefix_inc(); + return (((*this).points) <= ((*other).points)); + } + unsafe fn greater_equal( + this: *const Self, + other: *const p1_each_relation_is_an_operator_Score, + ) -> bool { + (*std::cell::LazyCell::force_mut(&mut *&raw mut greater_equal_calls_2)).prefix_inc(); + return (((*this).points) >= ((*other).points)); + } + unsafe fn less(lhs: *const Self, rhs: *const Self) -> bool { + ::less(lhs, rhs) + } +} +impl PartiallyOrderedWithUnsafe for p1_operator_preferred_over_three_way_Level { + unsafe fn greater( + this: *const Self, + other: *const p1_operator_preferred_over_three_way_Level, + ) -> bool { + (*std::cell::LazyCell::force_mut(&mut *&raw mut greater_calls_5)).prefix_inc(); + return (((*this).depth) > ((*other).depth)); + } + unsafe fn less(lhs: *const Self, rhs: *const Self) -> bool { + ::less(lhs, rhs) + } + unsafe fn less_equal(lhs: *const Self, rhs: *const Self) -> bool { + libcc2rs::is_lteq(p1_operator_preferred_over_three_way_Level::operator_cmp( + lhs, rhs, + )) + } + unsafe fn greater_equal(lhs: *const Self, rhs: *const Self) -> bool { + libcc2rs::is_gteq(p1_operator_preferred_over_three_way_Level::operator_cmp( + lhs, rhs, + )) + } +} +impl WeaklyEqualityComparableWithUnsafe for p1_each_relation_is_an_operator_Score { + unsafe fn equal( + this: *const Self, + other: *const p1_each_relation_is_an_operator_Score, + ) -> bool { + return (((*this).points) == ((*other).points)); + } +} +impl WeaklyEqualityComparableWithUnsafe for p1_relations_from_three_way_Level { + unsafe fn equal(this: *const Self, other: *const p1_relations_from_three_way_Level) -> bool { + return (((*this).depth) == ((*other).depth)); + } +} +pub unsafe fn __cpp2rust_init_globals() { + std::cell::LazyCell::force(&*&raw const greater_calls_0); + std::cell::LazyCell::force(&*&raw const less_equal_calls_1); + std::cell::LazyCell::force(&*&raw const greater_equal_calls_2); + std::cell::LazyCell::force(&*&raw const greater_calls_5); +} diff --git a/tests/std/expr/expr.compound/expr.spaceship.cpp b/tests/std/expr/expr.compound/expr.spaceship.cpp new file mode 100644 index 0000000000..2598fcc9f5 --- /dev/null +++ b/tests/std/expr/expr.compound/expr.spaceship.cpp @@ -0,0 +1,87 @@ +// ADDITIONAL_COMPILE_FLAGS: -std=c++23 +#include +#include + +// N4950 [expr.spaceship]/4.1: for operands of integral type, the result is of +// type std::strong_ordering. +namespace p4_1_integral_operands { +void test() { + int small = 3; + int large = 7; + std::strong_ordering order = small <=> large; + assert(order < 0); + assert((large <=> small) > 0); + assert((small <=> small) == 0); + assert((small <=> large) == std::strong_ordering::less); + assert((large <=> small) == std::strong_ordering::greater); + assert((small <=> small) == std::strong_ordering::equal); + + unsigned low = 1; + unsigned high = 4000000000u; + assert((low <=> high) < 0); + assert((high <=> low) > 0); + + long long negative = -5; + long long positive = 5; + assert((negative <=> positive) < 0); +} +} // namespace p4_1_integral_operands + +// N4950 [expr.spaceship]/4.3: for operands of floating-point type, the result +// is of type std::partial_ordering, and is unordered if either is a NaN. +namespace p4_3_floating_point_operands { +void test() { + double small = 1.5; + double large = 2.5; + std::partial_ordering order = small <=> large; + assert(order < 0); + assert((large <=> small) > 0); + assert((small <=> small) == 0); + assert((small <=> large) == std::partial_ordering::less); + + double zero = 0.0; + double nan = zero / zero; + std::partial_ordering unordered = nan <=> small; + assert(unordered == std::partial_ordering::unordered); + assert(!(unordered < 0) && !(unordered > 0) && !(unordered == 0)); + assert((nan <=> nan) == std::partial_ordering::unordered); +} +} // namespace p4_3_floating_point_operands + +// N4950 [expr.spaceship]/6: for operands of pointer type, the result is of +// type std::strong_ordering and orders pointers into the same array by their +// positions. +namespace p6_pointer_operands { +void test() { + int values[3] = {10, 20, 30}; + int *first = &values[0]; + int *last = &values[2]; + std::strong_ordering order = first <=> last; + assert(order < 0); + assert((last <=> first) > 0); + assert((first <=> first) == 0); +} +} // namespace p6_pointer_operands + +// N4950 [expr.spaceship]/1: the result is a prvalue that can be converted to +// a weaker comparison category. +namespace p1_result_converts_to_weaker_category { +std::weak_ordering as_weak(int lhs, int rhs) { return lhs <=> rhs; } + +std::partial_ordering as_partial(int lhs, int rhs) { return lhs <=> rhs; } + +void test() { + assert(as_weak(1, 2) < 0); + assert(as_weak(2, 2) == 0); + assert(as_partial(3, 2) > 0); + assert(as_partial(2, 2) == std::partial_ordering::equivalent); +} +} // namespace p1_result_converts_to_weaker_category + +int main() { + p4_1_integral_operands::test(); + p4_3_floating_point_operands::test(); + p6_pointer_operands::test(); + p1_result_converts_to_weaker_category::test(); + return 0; +} diff --git a/tests/std/expr/expr.compound/out/refcount/expr.spaceship.rs b/tests/std/expr/expr.compound/out/refcount/expr.spaceship.rs new file mode 100644 index 0000000000..d174a6ae6d --- /dev/null +++ b/tests/std/expr/expr.compound/out/refcount/expr.spaceship.rs @@ -0,0 +1,179 @@ +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}; +pub fn test_0() { + let mut small: i32 = 3; + let mut large: i32 = 7; + let order: Value = Rc::new(RefCell::new( + libcc2rs::StrongOrdering::from(std::cmp::Ord::cmp(&(small), &(large))), + )); + assert!(libcc2rs::is_lt(({ (*order.borrow()) }))); + assert!(libcc2rs::is_gt( + (libcc2rs::StrongOrdering::from(std::cmp::Ord::cmp(&(large), &(small)))) + )); + assert!(libcc2rs::is_eq( + (libcc2rs::StrongOrdering::from(std::cmp::Ord::cmp(&(small), &(small)))) + )); + assert!({ + let __a = libcc2rs::PartialOrdering::from( + (libcc2rs::StrongOrdering::from(std::cmp::Ord::cmp(&(small), &(large)))), + ); + let __b = libcc2rs::PartialOrdering::from( + ({ (*libcc2rs::strong_ordering_less().upgrade().deref()) }), + ); + libcc2rs::is_lt(__a) == libcc2rs::is_lt(__b) + && libcc2rs::is_eq(__a) == libcc2rs::is_eq(__b) + && libcc2rs::is_gt(__a) == libcc2rs::is_gt(__b) + }); + assert!({ + let __a = libcc2rs::PartialOrdering::from( + (libcc2rs::StrongOrdering::from(std::cmp::Ord::cmp(&(large), &(small)))), + ); + let __b = libcc2rs::PartialOrdering::from( + ({ (*libcc2rs::strong_ordering_greater().upgrade().deref()) }), + ); + libcc2rs::is_lt(__a) == libcc2rs::is_lt(__b) + && libcc2rs::is_eq(__a) == libcc2rs::is_eq(__b) + && libcc2rs::is_gt(__a) == libcc2rs::is_gt(__b) + }); + assert!({ + let __a = libcc2rs::PartialOrdering::from( + (libcc2rs::StrongOrdering::from(std::cmp::Ord::cmp(&(small), &(small)))), + ); + let __b = libcc2rs::PartialOrdering::from( + ({ (*libcc2rs::strong_ordering_equal().upgrade().deref()) }), + ); + libcc2rs::is_lt(__a) == libcc2rs::is_lt(__b) + && libcc2rs::is_eq(__a) == libcc2rs::is_eq(__b) + && libcc2rs::is_gt(__a) == libcc2rs::is_gt(__b) + }); + let mut low: u32 = 1_u32; + let mut high: u32 = 4000000000_u32; + assert!(libcc2rs::is_lt( + (libcc2rs::StrongOrdering::from(std::cmp::Ord::cmp(&(low), &(high)))) + )); + assert!(libcc2rs::is_gt( + (libcc2rs::StrongOrdering::from(std::cmp::Ord::cmp(&(high), &(low)))) + )); + let mut negative: i64 = (-5_i32 as i64); + let mut positive: i64 = 5_i64; + assert!(libcc2rs::is_lt( + (libcc2rs::StrongOrdering::from(std::cmp::Ord::cmp(&(negative), &(positive)))) + )); +} +pub fn test_1() { + let mut small: f64 = 1.5_f64; + let mut large: f64 = 2.5_f64; + let order: Value = Rc::new(RefCell::new( + libcc2rs::PartialOrdering::from(std::cmp::PartialOrd::partial_cmp(&(small), &(large))), + )); + assert!(libcc2rs::is_lt(({ (*order.borrow()) }))); + assert!(libcc2rs::is_gt( + (libcc2rs::PartialOrdering::from(std::cmp::PartialOrd::partial_cmp(&(large), &(small)))) + )); + assert!(libcc2rs::is_eq( + (libcc2rs::PartialOrdering::from(std::cmp::PartialOrd::partial_cmp(&(small), &(small)))) + )); + assert!({ + let __a = libcc2rs::PartialOrdering::from( + (libcc2rs::PartialOrdering::from(std::cmp::PartialOrd::partial_cmp( + &(small), + &(large), + ))), + ); + let __b = libcc2rs::PartialOrdering::from( + ({ (*libcc2rs::partial_ordering_less().upgrade().deref()) }), + ); + libcc2rs::is_lt(__a) == libcc2rs::is_lt(__b) + && libcc2rs::is_eq(__a) == libcc2rs::is_eq(__b) + && libcc2rs::is_gt(__a) == libcc2rs::is_gt(__b) + }); + let mut zero: f64 = 0_f64; + let mut nan: f64 = (zero / zero); + let unordered: Value = Rc::new(RefCell::new( + libcc2rs::PartialOrdering::from(std::cmp::PartialOrd::partial_cmp(&(nan), &(small))), + )); + assert!({ + let __a = libcc2rs::PartialOrdering::from(({ (*unordered.borrow()) })); + let __b = libcc2rs::PartialOrdering::from( + ({ (*libcc2rs::partial_ordering_unordered().upgrade().deref()) }), + ); + libcc2rs::is_lt(__a) == libcc2rs::is_lt(__b) + && libcc2rs::is_eq(__a) == libcc2rs::is_eq(__b) + && libcc2rs::is_gt(__a) == libcc2rs::is_gt(__b) + }); + assert!( + ((!(libcc2rs::is_lt(({ (*unordered.borrow()) })))) + && (!(libcc2rs::is_gt(({ (*unordered.borrow()) }))))) + && (!(libcc2rs::is_eq(({ (*unordered.borrow()) })))) + ); + assert!({ + let __a = libcc2rs::PartialOrdering::from( + (libcc2rs::PartialOrdering::from(std::cmp::PartialOrd::partial_cmp(&(nan), &(nan)))), + ); + let __b = libcc2rs::PartialOrdering::from( + ({ (*libcc2rs::partial_ordering_unordered().upgrade().deref()) }), + ); + libcc2rs::is_lt(__a) == libcc2rs::is_lt(__b) + && libcc2rs::is_eq(__a) == libcc2rs::is_eq(__b) + && libcc2rs::is_gt(__a) == libcc2rs::is_gt(__b) + }); +} +pub fn test_2() { + let values: Value> = Rc::new(RefCell::new(Box::new([10, 20, 30]))); + let mut first: Ptr = (values.decay().offset(0)); + let mut last: Ptr = (values.decay().offset(2)); + let order: Value = Rc::new(RefCell::new( + libcc2rs::StrongOrdering::from(std::cmp::Ord::cmp(&(first), &(last))), + )); + assert!(libcc2rs::is_lt(({ (*order.borrow()) }))); + assert!(libcc2rs::is_gt( + (libcc2rs::StrongOrdering::from(std::cmp::Ord::cmp(&(last), &(first)))) + )); + assert!(libcc2rs::is_eq( + (libcc2rs::StrongOrdering::from(std::cmp::Ord::cmp(&(first), &(first)))) + )); +} +pub fn as_weak_3(mut lhs: i32, mut rhs: i32) -> libcc2rs::WeakOrdering { + return libcc2rs::WeakOrdering::from(libcc2rs::StrongOrdering::from(std::cmp::Ord::cmp( + &(lhs), + &(rhs), + ))); +} +pub fn as_partial_4(mut lhs: i32, mut rhs: i32) -> libcc2rs::PartialOrdering { + return libcc2rs::PartialOrdering::from(libcc2rs::StrongOrdering::from(std::cmp::Ord::cmp( + &(lhs), + &(rhs), + ))); +} +pub fn test_5() { + assert!(libcc2rs::is_lt(({ as_weak_3(1, 2,) }))); + assert!(libcc2rs::is_eq(({ as_weak_3(2, 2,) }))); + assert!(libcc2rs::is_gt(({ as_partial_4(3, 2,) }))); + assert!({ + let __a = libcc2rs::PartialOrdering::from(({ as_partial_4(2, 2) })); + let __b = libcc2rs::PartialOrdering::from( + ({ (*libcc2rs::partial_ordering_equivalent().upgrade().deref()) }), + ); + libcc2rs::is_lt(__a) == libcc2rs::is_lt(__b) + && libcc2rs::is_eq(__a) == libcc2rs::is_eq(__b) + && libcc2rs::is_gt(__a) == libcc2rs::is_gt(__b) + }); +} +pub fn main() { + __cpp2rust_init_globals(); + libcc2rs::cpp2rust_exit(main_0()); +} +fn main_0() -> i32 { + ({ test_0() }); + ({ test_1() }); + ({ test_2() }); + ({ test_5() }); + return 0; +} +pub fn __cpp2rust_init_globals() {} diff --git a/tests/std/expr/expr.compound/out/unsafe/expr.spaceship.rs b/tests/std/expr/expr.compound/out/unsafe/expr.spaceship.rs new file mode 100644 index 0000000000..764f8ecbdc --- /dev/null +++ b/tests/std/expr/expr.compound/out/unsafe/expr.spaceship.rs @@ -0,0 +1,162 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +pub unsafe fn test_0() { + let mut small: i32 = 3; + let mut large: i32 = 7; + let mut order: libcc2rs::StrongOrdering = + libcc2rs::StrongOrdering::from(std::cmp::Ord::cmp(&(small), &(large))); + assert!(libcc2rs::is_lt(order)); + assert!(libcc2rs::is_gt( + (libcc2rs::StrongOrdering::from(std::cmp::Ord::cmp(&(large), &(small)))) + )); + assert!(libcc2rs::is_eq( + (libcc2rs::StrongOrdering::from(std::cmp::Ord::cmp(&(small), &(small)))) + )); + assert!({ + let __a = libcc2rs::PartialOrdering::from( + (libcc2rs::StrongOrdering::from(std::cmp::Ord::cmp(&(small), &(large)))), + ); + let __b = libcc2rs::PartialOrdering::from(libcc2rs::STRONG_ORDERING_LESS); + libcc2rs::is_lt(__a) == libcc2rs::is_lt(__b) + && libcc2rs::is_eq(__a) == libcc2rs::is_eq(__b) + && libcc2rs::is_gt(__a) == libcc2rs::is_gt(__b) + }); + assert!({ + let __a = libcc2rs::PartialOrdering::from( + (libcc2rs::StrongOrdering::from(std::cmp::Ord::cmp(&(large), &(small)))), + ); + let __b = libcc2rs::PartialOrdering::from(libcc2rs::STRONG_ORDERING_GREATER); + libcc2rs::is_lt(__a) == libcc2rs::is_lt(__b) + && libcc2rs::is_eq(__a) == libcc2rs::is_eq(__b) + && libcc2rs::is_gt(__a) == libcc2rs::is_gt(__b) + }); + assert!({ + let __a = libcc2rs::PartialOrdering::from( + (libcc2rs::StrongOrdering::from(std::cmp::Ord::cmp(&(small), &(small)))), + ); + let __b = libcc2rs::PartialOrdering::from(libcc2rs::STRONG_ORDERING_EQUAL); + libcc2rs::is_lt(__a) == libcc2rs::is_lt(__b) + && libcc2rs::is_eq(__a) == libcc2rs::is_eq(__b) + && libcc2rs::is_gt(__a) == libcc2rs::is_gt(__b) + }); + let mut low: u32 = 1_u32; + let mut high: u32 = 4000000000_u32; + assert!(libcc2rs::is_lt( + (libcc2rs::StrongOrdering::from(std::cmp::Ord::cmp(&(low), &(high)))) + )); + assert!(libcc2rs::is_gt( + (libcc2rs::StrongOrdering::from(std::cmp::Ord::cmp(&(high), &(low)))) + )); + let mut negative: i64 = (-5_i32 as i64); + let mut positive: i64 = 5_i64; + assert!(libcc2rs::is_lt( + (libcc2rs::StrongOrdering::from(std::cmp::Ord::cmp(&(negative), &(positive)))) + )); +} +pub unsafe fn test_1() { + let mut small: f64 = 1.5_f64; + let mut large: f64 = 2.5_f64; + let mut order: libcc2rs::PartialOrdering = + libcc2rs::PartialOrdering::from(std::cmp::PartialOrd::partial_cmp(&(small), &(large))); + assert!(libcc2rs::is_lt(order)); + assert!(libcc2rs::is_gt( + (libcc2rs::PartialOrdering::from(std::cmp::PartialOrd::partial_cmp(&(large), &(small)))) + )); + assert!(libcc2rs::is_eq( + (libcc2rs::PartialOrdering::from(std::cmp::PartialOrd::partial_cmp(&(small), &(small)))) + )); + assert!({ + let __a = libcc2rs::PartialOrdering::from( + (libcc2rs::PartialOrdering::from(std::cmp::PartialOrd::partial_cmp( + &(small), + &(large), + ))), + ); + let __b = libcc2rs::PartialOrdering::from(libcc2rs::PARTIAL_ORDERING_LESS); + libcc2rs::is_lt(__a) == libcc2rs::is_lt(__b) + && libcc2rs::is_eq(__a) == libcc2rs::is_eq(__b) + && libcc2rs::is_gt(__a) == libcc2rs::is_gt(__b) + }); + let mut zero: f64 = 0_f64; + let mut nan: f64 = ((zero) / (zero)); + let mut unordered: libcc2rs::PartialOrdering = + libcc2rs::PartialOrdering::from(std::cmp::PartialOrd::partial_cmp(&(nan), &(small))); + assert!({ + let __a = libcc2rs::PartialOrdering::from(unordered); + let __b = libcc2rs::PartialOrdering::from(libcc2rs::PARTIAL_ORDERING_UNORDERED); + libcc2rs::is_lt(__a) == libcc2rs::is_lt(__b) + && libcc2rs::is_eq(__a) == libcc2rs::is_eq(__b) + && libcc2rs::is_gt(__a) == libcc2rs::is_gt(__b) + }); + assert!( + ((!(libcc2rs::is_lt(unordered))) && (!(libcc2rs::is_gt(unordered)))) + && (!(libcc2rs::is_eq(unordered))) + ); + assert!({ + let __a = libcc2rs::PartialOrdering::from( + (libcc2rs::PartialOrdering::from(std::cmp::PartialOrd::partial_cmp(&(nan), &(nan)))), + ); + let __b = libcc2rs::PartialOrdering::from(libcc2rs::PARTIAL_ORDERING_UNORDERED); + libcc2rs::is_lt(__a) == libcc2rs::is_lt(__b) + && libcc2rs::is_eq(__a) == libcc2rs::is_eq(__b) + && libcc2rs::is_gt(__a) == libcc2rs::is_gt(__b) + }); +} +pub unsafe fn test_2() { + let mut values: [i32; 3] = [10, 20, 30]; + let mut first: *mut i32 = (&mut values[(0) as usize] as *mut i32); + let mut last: *mut i32 = (&mut values[(2) as usize] as *mut i32); + let mut order: libcc2rs::StrongOrdering = + libcc2rs::StrongOrdering::from(std::cmp::Ord::cmp(&(first), &(last))); + assert!(libcc2rs::is_lt(order)); + assert!(libcc2rs::is_gt( + (libcc2rs::StrongOrdering::from(std::cmp::Ord::cmp(&(last), &(first)))) + )); + assert!(libcc2rs::is_eq( + (libcc2rs::StrongOrdering::from(std::cmp::Ord::cmp(&(first), &(first)))) + )); +} +pub unsafe fn as_weak_3(mut lhs: i32, mut rhs: i32) -> libcc2rs::WeakOrdering { + return libcc2rs::WeakOrdering::from(libcc2rs::StrongOrdering::from(std::cmp::Ord::cmp( + &(lhs), + &(rhs), + ))); +} +pub unsafe fn as_partial_4(mut lhs: i32, mut rhs: i32) -> libcc2rs::PartialOrdering { + return libcc2rs::PartialOrdering::from(libcc2rs::StrongOrdering::from(std::cmp::Ord::cmp( + &(lhs), + &(rhs), + ))); +} +pub unsafe fn test_5() { + assert!(libcc2rs::is_lt((unsafe { as_weak_3(1, 2,) }))); + assert!(libcc2rs::is_eq((unsafe { as_weak_3(2, 2,) }))); + assert!(libcc2rs::is_gt((unsafe { as_partial_4(3, 2,) }))); + assert!({ + let __a = libcc2rs::PartialOrdering::from((unsafe { as_partial_4(2, 2) })); + let __b = libcc2rs::PartialOrdering::from(libcc2rs::PARTIAL_ORDERING_EQUIVALENT); + libcc2rs::is_lt(__a) == libcc2rs::is_lt(__b) + && libcc2rs::is_eq(__a) == libcc2rs::is_eq(__b) + && libcc2rs::is_gt(__a) == libcc2rs::is_gt(__b) + }); +} +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() }); + (unsafe { test_2() }); + (unsafe { test_5() }); + return 0; +} +pub unsafe fn __cpp2rust_init_globals() {} diff --git a/tests/std/library/requirements/utility.requirements/out/refcount/utility.arg.requirements.rs b/tests/std/library/requirements/utility.requirements/out/refcount/utility.arg.requirements.rs new file mode 100644 index 0000000000..952225088d --- /dev/null +++ b/tests/std/library/requirements/utility.requirements/out/refcount/utility.arg.requirements.rs @@ -0,0 +1,409 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +thread_local!( + pub static less_calls_0: Value = Rc::new(RefCell::new(0)); +); +#[derive( + Copy, + Clone, + Record, + ByteRepr, + ZeroInitialization, + VaArg, + FnPtrArg, + TriviallyDefaultConstructible, + TriviallyCopyable, + Default, +)] +#[byte_size(8)] +pub struct table29_member_operator_Version { + #[offset(0)] + pub major: i32, + #[offset(4)] + pub minor: i32, +} +impl LessThanComparable for table29_member_operator_Version { + fn less(this: Ptr, other: Ptr) -> bool { + (*less_calls_0.with(Value::clone).borrow_mut()).prefix_inc(); + return ({ this.with(|__s| __s.major) } < { other.with(|__s| __s.major) }) + || (({ this.with(|__s| __s.major) } == { other.with(|__s| __s.major) }) + && ({ this.with(|__s| __s.minor) } < { other.with(|__s| __s.minor) })); + } +} +pub fn test_1() { + let old_version: Value = + Rc::new(RefCell::new(table29_member_operator_Version { + major: 1, + minor: 4, + })); + let new_version: Value = + Rc::new(RefCell::new(table29_member_operator_Version { + major: 2, + minor: 0, + })); + assert!( + (if LessThanComparable::less( + old_version.as_pointer().clone(), + new_version.as_pointer().clone() + ) { + new_version.as_pointer() + } else { + old_version.as_pointer() + } + .with(|__s| __s.major) + == 2) + ); + assert!( + (if LessThanComparable::less( + new_version.as_pointer().clone(), + old_version.as_pointer().clone() + ) { + new_version.as_pointer() + } else { + old_version.as_pointer() + } + .with(|__s| __s.major) + == 1) + ); + assert!((less_calls_0.with(|rc| *rc.borrow()) == 2)); + let versions: Value> = Rc::new(RefCell::new(Vec::from([ + table29_member_operator_Version { major: 2, minor: 1 }, + table29_member_operator_Version { major: 1, minor: 9 }, + table29_member_operator_Version { major: 2, minor: 0 }, + ]))); + { + let mut __tmp_0 = { versions.decay().begin() }; + let __n = versions.decay().end().iter_distance(&__tmp_0); + let mut __order: Vec = (0..__n).collect(); + __order.sort_by(|&__x, &__y| cpp_cmp(&__tmp_0.iter_at(__x), &__tmp_0.iter_at(__y))); + reorder(__order, |__i| __tmp_0.iter_at(__i)); + }; + assert!( + ({ { (*versions.borrow())[0_usize] }.major } == 1) + && ({ { (*versions.borrow())[0_usize] }.minor } == 9) + ); + assert!( + ({ { (*versions.borrow())[1_usize] }.major } == 2) + && ({ { (*versions.borrow())[1_usize] }.minor } == 0) + ); + assert!( + ({ { (*versions.borrow())[2_usize] }.major } == 2) + && ({ { (*versions.borrow())[2_usize] }.minor } == 1) + ); + assert!( + ({ + let __it: Value> = + Rc::new(RefCell::new(versions.decay().begin())); + let __last: Value> = + Rc::new(RefCell::new(versions.decay().end())); + let mut __best = __it.borrow().clone(); + if >::not_equal( + __it.as_pointer(), + __last.as_pointer(), + ) { + __it.borrow_mut().iter_inc(); + while >::not_equal( + __it.as_pointer(), + __last.as_pointer(), + ) { + if LessThanComparable::less(__best.iter_deref(), __it.borrow().iter_deref()) { + __best = __it.borrow().clone(); + } + __it.borrow_mut().iter_inc(); + } + } + __best + } + .iter_deref() + .with(|__s| __s.minor) + == 1) + ); + let downloads: Value< + Map, Value)>, + > = Rc::new(RefCell::new(Map::new())); + match (new_version.as_pointer(),) { + (__tmp_1,) => ({ (*downloads.borrow_mut()).subscript(__tmp_1, || 0_i32) }), + } + .write(7); + match (old_version.as_pointer(),) { + (__tmp_2,) => ({ (*downloads.borrow_mut()).subscript(__tmp_2, || 0_i32) }), + } + .write(3); + { + match (new_version.as_pointer(),) { + (__tmp_3,) => ({ (*downloads.borrow_mut()).subscript(__tmp_3, || 0_i32) }), + } + .with_mut(|__v| *__v = *__v + 1) + }; + assert!(({ ((*downloads.borrow()).len() == 2_usize) })); + assert!( + ((*(*downloads + .as_pointer() + .with( + |__m: &Map< + table29_member_operator_Version, + (Value, Value), + >| __m.begin() + ) + .iter_deref() + .upgrade() + .deref()) + .1 + .borrow()) + == 3) + ); + assert!( + ((match (new_version.as_pointer(),) { + (__tmp_4,) => { + ({ (*downloads.borrow_mut()).subscript(__tmp_4, || 0_i32) }) + } + } + .read()) + == 8) + ); +} +#[derive( + Copy, + Clone, + Record, + ByteRepr, + ZeroInitialization, + VaArg, + FnPtrArg, + TriviallyDefaultConstructible, + TriviallyCopyable, + Default, +)] +#[byte_size(4)] +pub struct table29_other_operator_shapes_Weight { + #[offset(0)] + pub grams: i32, +} +impl table29_other_operator_shapes_Weight { + pub fn operator_lt(this: Ptr, mut other: table29_other_operator_shapes_Weight) -> bool { + return (this.with(|__s| __s.grams) < other.grams); + } +} +impl LessThanComparable for table29_other_operator_shapes_Weight { + fn less(this: Ptr, other: Ptr) -> bool { + table29_other_operator_shapes_Weight::operator_lt(this.clone(), other.read()) + } +} +#[derive( + Copy, + Clone, + Record, + ByteRepr, + ZeroInitialization, + VaArg, + FnPtrArg, + TriviallyDefaultConstructible, + TriviallyCopyable, + Default, +)] +#[byte_size(4)] +pub struct table29_other_operator_shapes_Height { + #[offset(0)] + pub millimeters: i32, +} +impl LessThanComparable for table29_other_operator_shapes_Height { + fn less( + lhs: Ptr, + rhs: Ptr, + ) -> bool { + return ({ lhs.with(|__s| __s.millimeters) } < { rhs.with(|__s| __s.millimeters) }); + } +} +#[derive( + Copy, + Clone, + Record, + ByteRepr, + ZeroInitialization, + VaArg, + FnPtrArg, + TriviallyDefaultConstructible, + TriviallyCopyable, + Default, + PartiallyOrderedWith, +)] +#[three_way(table29_other_operator_shapes_Depth::operator_cmp)] +#[byte_size(4)] +pub struct table29_other_operator_shapes_Depth { + #[offset(0)] + pub millimeters: i32, +} +impl table29_other_operator_shapes_Depth { + pub fn operator_cmp( + this: Ptr, + other: Ptr, + ) -> libcc2rs::StrongOrdering { + return libcc2rs::StrongOrdering::from(std::cmp::Ord::cmp( + &(this.with(|__s| __s.millimeters)), + &(other.with(|__s| __s.millimeters)), + )); + } +} +impl LessThanComparable for table29_other_operator_shapes_Depth { + fn less(this: Ptr, other: Ptr) -> bool { + libcc2rs::is_lt(table29_other_operator_shapes_Depth::operator_cmp( + this.clone(), + other.clone(), + )) + } +} +pub fn test_2() { + assert!( + ({ + let __tmp_5: Value = + Rc::new(RefCell::new(table29_other_operator_shapes_Weight { + grams: 3, + })); + let __tmp_6: Value = + Rc::new(RefCell::new(table29_other_operator_shapes_Weight { + grams: 5, + })); + if LessThanComparable::less( + (__tmp_5.as_pointer()).clone(), + (__tmp_6.as_pointer()).clone(), + ) { + (__tmp_6.as_pointer()) + } else { + (__tmp_5.as_pointer()) + } + .with(|__s| __s.grams) + } == 5) + ); + assert!( + ({ + let __tmp_7: Value = + Rc::new(RefCell::new(table29_other_operator_shapes_Height { + millimeters: 50, + })); + let __tmp_8: Value = + Rc::new(RefCell::new(table29_other_operator_shapes_Height { + millimeters: 30, + })); + if LessThanComparable::less( + (__tmp_7.as_pointer()).clone(), + (__tmp_8.as_pointer()).clone(), + ) { + (__tmp_7.as_pointer()) + } else { + (__tmp_8.as_pointer()) + } + .with(|__s| __s.millimeters) + } == 30) + ); + assert!( + ({ + let __tmp_9: Value = + Rc::new(RefCell::new(table29_other_operator_shapes_Depth { + millimeters: 7, + })); + let __tmp_10: Value = + Rc::new(RefCell::new(table29_other_operator_shapes_Depth { + millimeters: 4, + })); + if LessThanComparable::less( + (__tmp_9.as_pointer()).clone(), + (__tmp_10.as_pointer()).clone(), + ) { + (__tmp_10.as_pointer()) + } else { + (__tmp_9.as_pointer()) + } + .with(|__s| __s.millimeters) + } == 7) + ); +} +#[derive( + Copy, + Clone, + Record, + ByteRepr, + ZeroInitialization, + VaArg, + FnPtrArg, + TriviallyDefaultConstructible, + TriviallyCopyable, + Default, +)] +#[byte_size(4)] +pub struct pairs_spec_p3_members_order_pairs_Rank { + #[offset(0)] + pub value: i32, +} +impl LessThanComparable for pairs_spec_p3_members_order_pairs_Rank { + fn less(this: Ptr, other: Ptr) -> bool { + return ({ this.with(|__s| __s.value) } < { other.with(|__s| __s.value) }); + } +} +pub fn test_3() { + let low: Value<(Value, Value)> = + Rc::new(RefCell::new(( + Rc::new(RefCell::new( + (pairs_spec_p3_members_order_pairs_Rank { value: 1 }), + )), + Rc::new(RefCell::new(9)), + ))); + let high: Value<(Value, Value)> = + Rc::new(RefCell::new(( + Rc::new(RefCell::new( + (pairs_spec_p3_members_order_pairs_Rank { value: 2 }), + )), + Rc::new(RefCell::new(0)), + ))); + let high_later: Value<(Value, Value)> = + Rc::new(RefCell::new(( + Rc::new(RefCell::new( + (pairs_spec_p3_members_order_pairs_Rank { value: 2 }), + )), + Rc::new(RefCell::new(5)), + ))); + assert!(LessThanComparable::less( + low.as_pointer(), + high.as_pointer() + )); + assert!(LessThanComparable::less( + high.as_pointer(), + high_later.as_pointer() + )); + assert!(LessThanComparable::less( + low.as_pointer(), + high_later.as_pointer() + )); + assert!(!LessThanComparable::less( + high.as_pointer(), + low.as_pointer() + )); + assert!(!LessThanComparable::less( + high_later.as_pointer(), + high.as_pointer() + )); + assert!(!(LessThanComparable::less(high.as_pointer(), low.as_pointer()))); + assert!( + ({ + ( (*(*if LessThanComparable::less(low .as_pointer() .clone(), high .as_pointer() .clone()) { high .as_pointer() } else { low .as_pointer() } .upgrade().deref()) .1.borrow()) == 0 ) + }) + ); +} +pub fn main() { + __cpp2rust_init_globals(); + libcc2rs::cpp2rust_exit(main_0()); +} +fn main_0() -> i32 { + ({ test_1() }); + ({ test_2() }); + ({ test_3() }); + return 0; +} +pub fn __cpp2rust_init_globals() { + let _ = less_calls_0.with(|_| ()); +} diff --git a/tests/std/library/requirements/utility.requirements/out/unsafe/utility.arg.requirements.rs b/tests/std/library/requirements/utility.requirements/out/unsafe/utility.arg.requirements.rs new file mode 100644 index 0000000000..801b0f3a1e --- /dev/null +++ b/tests/std/library/requirements/utility.requirements/out/unsafe/utility.arg.requirements.rs @@ -0,0 +1,331 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +pub static mut less_calls_0: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +#[repr(C)] +#[derive( + Copy, + Clone, + VaArg, + FnPtrArg, + ZeroInitializationUnsafe, + TriviallyDefaultConstructibleUnsafe, + TriviallyCopyableUnsafe, + Default, +)] +pub struct table29_member_operator_Version { + pub major: i32, + pub minor: i32, +} +impl LessThanComparableUnsafe for table29_member_operator_Version { + unsafe fn less(this: *const Self, other: *const table29_member_operator_Version) -> bool { + (*std::cell::LazyCell::force_mut(&mut *&raw mut less_calls_0)).prefix_inc(); + return (((*this).major) < ((*other).major)) + || ((((*this).major) == ((*other).major)) && (((*this).minor) < ((*other).minor))); + } +} +pub unsafe fn test_1() { + let mut old_version: table29_member_operator_Version = + table29_member_operator_Version { major: 1, minor: 4 }; + let mut new_version: table29_member_operator_Version = + table29_member_operator_Version { major: 2, minor: 0 }; + assert!( + (((*{ + let __a: *const _ = &mut old_version; + let __b: *const _ = &mut new_version; + if LessThanComparableUnsafe::less(__a, __b) { + __b + } else { + __a + } + }) + .major) + == (2)) + ); + assert!( + (((*{ + let __a: *const _ = &mut old_version; + let __b: *const _ = &mut new_version; + if LessThanComparableUnsafe::less(__b, __a) { + __b + } else { + __a + } + }) + .major) + == (1)) + ); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut less_calls_0)) == (2))); + let mut versions: Vec = Vec::from([ + table29_member_operator_Version { major: 2, minor: 1 }, + table29_member_operator_Version { major: 1, minor: 9 }, + table29_member_operator_Version { major: 2, minor: 0 }, + ]); + { + let mut __tmp_0 = { versions.begin() }; + let __n = versions.end().iter_distance(&__tmp_0); + let mut __order: Vec = (0..__n).collect(); + __order.sort_by(|&__x, &__y| { + let __a = __tmp_0.iter_at(__x) as *const _; + let __b = __tmp_0.iter_at(__y) as *const _; + if LessThanComparableUnsafe::less(__a, __b) { + std::cmp::Ordering::Less + } else if LessThanComparableUnsafe::less(__b, __a) { + std::cmp::Ordering::Greater + } else { + std::cmp::Ordering::Equal + } + }); + let __values: Vec<_> = __order + .iter() + .map(|&__i| std::ptr::read(__tmp_0.iter_at(__i))) + .collect(); + for (__i, __v) in (0..__n).zip(__values) { + std::ptr::write(__tmp_0.iter_at(__i), __v); + } + }; + assert!(((versions[(0_usize)].major) == (1)) && ((versions[(0_usize)].minor) == (9))); + assert!(((versions[(1_usize)].major) == (2)) && ((versions[(1_usize)].minor) == (0))); + assert!(((versions[(2_usize)].major) == (2)) && ((versions[(2_usize)].minor) == (1))); + assert!( + (((*{ + let mut __tmp_1 = { versions.begin() }; + let mut __tmp_2 = { versions.end() }; + let mut __best = __tmp_1.clone(); + if WeaklyEqualityComparableWithUnsafe::not_equal(&__tmp_1, &__tmp_2) { + let mut __it = (__tmp_1).clone(); + __it.iter_inc(); + while WeaklyEqualityComparableWithUnsafe::not_equal(&__it, &__tmp_2) { + if LessThanComparableUnsafe::less( + __best.iter_deref() as *const _, + __it.iter_deref() as *const _, + ) { + __best = __it.clone(); + } + __it.iter_inc(); + } + } + __best + } + .iter_deref()) + .minor) + == (1)) + ); + let mut downloads: UnsafeMap< + table29_member_operator_Version, + (table29_member_operator_Version, i32), + > = UnsafeMap::new(); + (*downloads.subscript(&new_version, || 0_i32)) = 7; + (*downloads.subscript(&old_version, || 0_i32)) = 3; + (*downloads.subscript(&new_version, || 0_i32)) += 1; + assert!(((downloads.len()) == (2_usize))); + assert!((((*downloads.begin().iter_deref()).1) == (3))); + assert!(((*downloads.subscript(&new_version, || 0_i32)) == (8))); +} +#[repr(C)] +#[derive( + Copy, + Clone, + VaArg, + FnPtrArg, + ZeroInitializationUnsafe, + TriviallyDefaultConstructibleUnsafe, + TriviallyCopyableUnsafe, + Default, +)] +pub struct table29_other_operator_shapes_Weight { + pub grams: i32, +} +impl table29_other_operator_shapes_Weight { + pub unsafe fn operator_lt( + this: *const Self, + mut other: table29_other_operator_shapes_Weight, + ) -> bool { + return (((*this).grams) < (other.grams)); + } +} +impl LessThanComparableUnsafe for table29_other_operator_shapes_Weight { + unsafe fn less(this: *const Self, other: *const Self) -> bool { + table29_other_operator_shapes_Weight::operator_lt(this, (*other).clone()) + } +} +#[repr(C)] +#[derive( + Copy, + Clone, + VaArg, + FnPtrArg, + ZeroInitializationUnsafe, + TriviallyDefaultConstructibleUnsafe, + TriviallyCopyableUnsafe, + Default, +)] +pub struct table29_other_operator_shapes_Height { + pub millimeters: i32, +} +impl LessThanComparableUnsafe for table29_other_operator_shapes_Height { + unsafe fn less( + lhs: *const table29_other_operator_shapes_Height, + rhs: *const table29_other_operator_shapes_Height, + ) -> bool { + return (((*lhs).millimeters) < ((*rhs).millimeters)); + } +} +#[repr(C)] +#[derive( + Copy, + Clone, + VaArg, + FnPtrArg, + ZeroInitializationUnsafe, + TriviallyDefaultConstructibleUnsafe, + TriviallyCopyableUnsafe, + Default, + PartiallyOrderedWithUnsafe, +)] +#[three_way(table29_other_operator_shapes_Depth::operator_cmp)] +pub struct table29_other_operator_shapes_Depth { + pub millimeters: i32, +} +impl table29_other_operator_shapes_Depth { + pub unsafe fn operator_cmp( + this: *const Self, + other: *const table29_other_operator_shapes_Depth, + ) -> libcc2rs::StrongOrdering { + return libcc2rs::StrongOrdering::from(std::cmp::Ord::cmp( + &((*this).millimeters), + &((*other).millimeters), + )); + } +} +impl LessThanComparableUnsafe for table29_other_operator_shapes_Depth { + unsafe fn less(this: *const Self, other: *const Self) -> bool { + libcc2rs::is_lt(table29_other_operator_shapes_Depth::operator_cmp( + this, other, + )) + } +} +pub unsafe fn test_2() { + assert!( + (({ + let mut __tmp_3: table29_other_operator_shapes_Weight = + table29_other_operator_shapes_Weight { grams: 3 }; + let mut __tmp_4: table29_other_operator_shapes_Weight = + table29_other_operator_shapes_Weight { grams: 5 }; + (*{ + let __a: *const _ = (&mut __tmp_3); + let __b: *const _ = (&mut __tmp_4); + if LessThanComparableUnsafe::less(__a, __b) { + __b + } else { + __a + } + }) + .grams + }) == (5)) + ); + assert!( + (({ + let mut __tmp_5: table29_other_operator_shapes_Height = + table29_other_operator_shapes_Height { millimeters: 30 }; + let mut __tmp_6: table29_other_operator_shapes_Height = + table29_other_operator_shapes_Height { millimeters: 50 }; + (*{ + let __a: *const _ = (&mut __tmp_5); + let __b: *const _ = (&mut __tmp_6); + if LessThanComparableUnsafe::less(__b, __a) { + __b + } else { + __a + } + }) + .millimeters + }) == (30)) + ); + assert!( + (({ + let mut __tmp_7: table29_other_operator_shapes_Depth = + table29_other_operator_shapes_Depth { millimeters: 7 }; + let mut __tmp_8: table29_other_operator_shapes_Depth = + table29_other_operator_shapes_Depth { millimeters: 4 }; + (*{ + let __a: *const _ = (&mut __tmp_7); + let __b: *const _ = (&mut __tmp_8); + if LessThanComparableUnsafe::less(__a, __b) { + __b + } else { + __a + } + }) + .millimeters + }) == (7)) + ); +} +#[repr(C)] +#[derive( + Copy, + Clone, + VaArg, + FnPtrArg, + ZeroInitializationUnsafe, + TriviallyDefaultConstructibleUnsafe, + TriviallyCopyableUnsafe, + Default, +)] +pub struct pairs_spec_p3_members_order_pairs_Rank { + pub value: i32, +} +impl LessThanComparableUnsafe for pairs_spec_p3_members_order_pairs_Rank { + unsafe fn less( + this: *const Self, + other: *const pairs_spec_p3_members_order_pairs_Rank, + ) -> bool { + return (((*this).value) < ((*other).value)); + } +} +pub unsafe fn test_3() { + let mut low: (pairs_spec_p3_members_order_pairs_Rank, i32) = + ((pairs_spec_p3_members_order_pairs_Rank { value: 1 }), 9); + let mut high: (pairs_spec_p3_members_order_pairs_Rank, i32) = + ((pairs_spec_p3_members_order_pairs_Rank { value: 2 }), 0); + let mut high_later: (pairs_spec_p3_members_order_pairs_Rank, i32) = + ((pairs_spec_p3_members_order_pairs_Rank { value: 2 }), 5); + assert!(LessThanComparableUnsafe::less(&low, &high)); + assert!(LessThanComparableUnsafe::less(&high, &high_later)); + assert!(LessThanComparableUnsafe::less(&low, &high_later)); + assert!(!LessThanComparableUnsafe::less(&high, &low)); + assert!(!LessThanComparableUnsafe::less(&high_later, &high)); + assert!(!(LessThanComparableUnsafe::less(&high, &low))); + assert!( + (((*{ + let __a: *const _ = &mut low; + let __b: *const _ = &mut high; + if LessThanComparableUnsafe::less(__a, __b) { + __b + } else { + __a + } + }) + .1) == (0)) + ); +} +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + libcc2rs::cpp2rust_exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + (unsafe { test_1() }); + (unsafe { test_2() }); + (unsafe { test_3() }); + return 0; +} +pub unsafe fn __cpp2rust_init_globals() { + std::cell::LazyCell::force(&*&raw const less_calls_0); +} diff --git a/tests/std/library/requirements/utility.requirements/utility.arg.requirements.cpp b/tests/std/library/requirements/utility.requirements/utility.arg.requirements.cpp new file mode 100644 index 0000000000..9d14b6cfad --- /dev/null +++ b/tests/std/library/requirements/utility.requirements/utility.arg.requirements.cpp @@ -0,0 +1,106 @@ +// ADDITIONAL_COMPILE_FLAGS: -std=c++23 +#include +#include +#include +#include +#include +#include + +// N4950 [utility.arg.requirements] Table 29 (Cpp17LessThanComparable): a < b +// is a strict weak ordering, and is the only operation the library uses to +// order values. +namespace table29_member_operator { +static int less_calls = 0; + +struct Version { + int major; + int minor; + bool operator<(const Version &other) const { + ++less_calls; + return major < other.major || (major == other.major && minor < other.minor); + } +}; + +void test() { + Version old_version{1, 4}; + Version new_version{2, 0}; + assert(std::max(old_version, new_version).major == 2); + assert(std::min(old_version, new_version).major == 1); + assert(less_calls == 2); + + std::vector versions{{2, 1}, {1, 9}, {2, 0}}; + std::sort(versions.begin(), versions.end()); + assert(versions[0].major == 1 && versions[0].minor == 9); + assert(versions[1].major == 2 && versions[1].minor == 0); + assert(versions[2].major == 2 && versions[2].minor == 1); + assert(std::max_element(versions.begin(), versions.end())->minor == 1); + + std::map downloads; + downloads[new_version] = 7; + downloads[old_version] = 3; + downloads[new_version] += 1; + assert(downloads.size() == 2); + assert(downloads.begin()->second == 3); + assert(downloads[new_version] == 8); +} +} // namespace table29_member_operator + +// N4950 [utility.arg.requirements] Table 29: the requirement is on the +// expression a < b, whichever way the operator is declared. +namespace table29_other_operator_shapes { +struct Weight { + int grams; + bool operator<(Weight other) const { return grams < other.grams; } +}; + +struct Height { + int millimeters; +}; + +bool operator<(const Height &lhs, const Height &rhs) { + return lhs.millimeters < rhs.millimeters; +} + +struct Depth { + int millimeters; + std::strong_ordering operator<=>(const Depth &other) const { + return millimeters <=> other.millimeters; + } +}; + +void test() { + assert(std::max(Weight{3}, Weight{5}).grams == 5); + assert(std::min(Height{30}, Height{50}).millimeters == 30); + assert(std::max(Depth{7}, Depth{4}).millimeters == 7); +} +} // namespace table29_other_operator_shapes + +// N4950 [pairs.spec]/3: pairs are ordered by their first members, and by +// their second members if the first ones are equivalent, using operator< of +// the members. +namespace pairs_spec_p3_members_order_pairs { +struct Rank { + int value; + bool operator<(const Rank &other) const { return value < other.value; } +}; + +void test() { + std::pair low{Rank{1}, 9}; + std::pair high{Rank{2}, 0}; + std::pair high_later{Rank{2}, 5}; + assert(low < high); + assert(high < high_later); + assert(high_later > low); + assert(low <= high); + assert(high_later >= high); + assert(!(high < low)); + assert(std::max(low, high).second == 0); +} +} // namespace pairs_spec_p3_members_order_pairs + +int main() { + table29_member_operator::test(); + table29_other_operator_shapes::test(); + pairs_spec_p3_members_order_pairs::test(); + return 0; +} diff --git a/tests/std/support/cmp/cmp.categories/cmp.partialord.cpp b/tests/std/support/cmp/cmp.categories/cmp.partialord.cpp new file mode 100644 index 0000000000..90b66f25f6 --- /dev/null +++ b/tests/std/support/cmp/cmp.categories/cmp.partialord.cpp @@ -0,0 +1,58 @@ +// ADDITIONAL_COMPILE_FLAGS: -std=c++23 +#include +#include + +// N4950 [cmp.partialord]/1: partial_ordering has the values less, equivalent, +// greater and unordered, each a static data member. +namespace p1_valid_values { +void test() { + const std::partial_ordering &less = std::partial_ordering::less; + const std::partial_ordering &unordered = std::partial_ordering::unordered; + assert(&less == &std::partial_ordering::less); + assert(&unordered == &std::partial_ordering::unordered); + assert(&less != &unordered); + assert(std::partial_ordering::less == std::partial_ordering::less); + assert(std::partial_ordering::equivalent == + std::partial_ordering::equivalent); + assert(!(std::partial_ordering::less == std::partial_ordering::greater)); + assert( + !(std::partial_ordering::equivalent == std::partial_ordering::unordered)); +} +} // namespace p1_valid_values + +// N4950 [cmp.partialord]/3: operator@(v, 0) returns v.is_ordered and +// v.value @ 0 for the relational operators and for ==. +namespace p3_compare_with_literal_zero { +void test() { + std::partial_ordering less = std::partial_ordering::less; + std::partial_ordering equivalent = std::partial_ordering::equivalent; + std::partial_ordering greater = std::partial_ordering::greater; + assert(less < 0 && less <= 0 && !(less == 0) && !(less > 0) && !(less >= 0)); + assert(equivalent == 0 && equivalent <= 0 && equivalent >= 0); + assert(!(equivalent < 0) && !(equivalent > 0)); + assert(greater > 0 && greater >= 0 && !(greater == 0) && !(greater < 0)); + assert(less != 0); + assert(!(equivalent != 0)); +} +} // namespace p3_compare_with_literal_zero + +// N4950 [cmp.partialord]/3: an unordered value compares false with zero under +// every relational operator and under ==. +namespace p3_unordered_compares_false { +void test() { + std::partial_ordering unordered = std::partial_ordering::unordered; + assert(!(unordered == 0)); + assert(!(unordered < 0)); + assert(!(unordered > 0)); + assert(!(unordered <= 0)); + assert(!(unordered >= 0)); + assert(unordered != 0); +} +} // namespace p3_unordered_compares_false + +int main() { + p1_valid_values::test(); + p3_compare_with_literal_zero::test(); + p3_unordered_compares_false::test(); + return 0; +} diff --git a/tests/std/support/cmp/cmp.categories/cmp.strongord.cpp b/tests/std/support/cmp/cmp.categories/cmp.strongord.cpp index 288c7cb4cd..f6cefaf8f4 100644 --- a/tests/std/support/cmp/cmp.categories/cmp.strongord.cpp +++ b/tests/std/support/cmp/cmp.categories/cmp.strongord.cpp @@ -17,7 +17,46 @@ void test() { } } // namespace p4_compare_with_literal_zero +// N4950 [cmp.strongord]/2: the conversion to partial_ordering keeps the +// value, and equal converts to equivalent. +namespace p2_conversion_to_partial_ordering { +void test() { + std::partial_ordering less = std::strong_ordering::less; + std::partial_ordering equal = std::strong_ordering::equal; + std::partial_ordering greater = std::strong_ordering::greater; + assert(less == std::partial_ordering::less); + assert(equal == std::partial_ordering::equivalent); + assert(greater == std::partial_ordering::greater); +} +} // namespace p2_conversion_to_partial_ordering + +// N4950 [cmp.strongord]/3: the conversion to weak_ordering keeps the value, +// and equal converts to equivalent. +namespace p3_conversion_to_weak_ordering { +void test() { + std::weak_ordering less = std::strong_ordering::less; + std::weak_ordering equal = std::strong_ordering::equal; + std::weak_ordering greater = std::strong_ordering::greater; + assert(less == std::weak_ordering::less); + assert(equal == std::weak_ordering::equivalent); + assert(greater == std::weak_ordering::greater); +} +} // namespace p3_conversion_to_weak_ordering + +// N4950 [cmp.strongord]/1: equal and equivalent are different members with +// the same value. +namespace p1_equal_and_equivalent { +void test() { + assert(std::strong_ordering::equal == std::strong_ordering::equivalent); + assert(&std::strong_ordering::equal != &std::strong_ordering::equivalent); + assert(!(std::strong_ordering::less == std::strong_ordering::greater)); +} +} // namespace p1_equal_and_equivalent + int main() { p4_compare_with_literal_zero::test(); + p2_conversion_to_partial_ordering::test(); + p3_conversion_to_weak_ordering::test(); + p1_equal_and_equivalent::test(); return 0; } diff --git a/tests/std/support/cmp/cmp.categories/cmp.weakord.cpp b/tests/std/support/cmp/cmp.categories/cmp.weakord.cpp new file mode 100644 index 0000000000..42220d8296 --- /dev/null +++ b/tests/std/support/cmp/cmp.categories/cmp.weakord.cpp @@ -0,0 +1,49 @@ +// ADDITIONAL_COMPILE_FLAGS: -std=c++23 +#include +#include + +// N4950 [cmp.weakord]/1: weak_ordering has the values less, equivalent and +// greater, each a static data member. +namespace p1_valid_values { +void test() { + const std::weak_ordering &less = std::weak_ordering::less; + const std::weak_ordering &greater = std::weak_ordering::greater; + assert(&less == &std::weak_ordering::less); + assert(&less != &greater); + assert(std::weak_ordering::equivalent == std::weak_ordering::equivalent); + assert(!(std::weak_ordering::less == std::weak_ordering::greater)); +} +} // namespace p1_valid_values + +// N4950 [cmp.weakord]/2: the conversion to partial_ordering keeps the value. +namespace p2_conversion_to_partial_ordering { +void test() { + std::partial_ordering less = std::weak_ordering::less; + std::partial_ordering equivalent = std::weak_ordering::equivalent; + std::partial_ordering greater = std::weak_ordering::greater; + assert(less == std::partial_ordering::less); + assert(equivalent == std::partial_ordering::equivalent); + assert(greater == std::partial_ordering::greater); +} +} // namespace p2_conversion_to_partial_ordering + +// N4950 [cmp.weakord]/3: operator@(v, 0) returns v.value @ 0. +namespace p3_compare_with_literal_zero { +void test() { + std::weak_ordering less = std::weak_ordering::less; + std::weak_ordering equivalent = std::weak_ordering::equivalent; + std::weak_ordering greater = std::weak_ordering::greater; + assert(less < 0 && less <= 0 && !(less == 0) && !(less > 0) && !(less >= 0)); + assert(equivalent == 0 && equivalent <= 0 && equivalent >= 0); + assert(!(equivalent < 0) && !(equivalent > 0)); + assert(greater > 0 && greater >= 0 && !(greater == 0) && !(greater < 0)); + assert(greater != 0); +} +} // namespace p3_compare_with_literal_zero + +int main() { + p1_valid_values::test(); + p2_conversion_to_partial_ordering::test(); + p3_compare_with_literal_zero::test(); + return 0; +} diff --git a/tests/std/support/cmp/cmp.categories/out/refcount/cmp.partialord.rs b/tests/std/support/cmp/cmp.categories/out/refcount/cmp.partialord.rs new file mode 100644 index 0000000000..86db1ecbe2 --- /dev/null +++ b/tests/std/support/cmp/cmp.categories/out/refcount/cmp.partialord.rs @@ -0,0 +1,120 @@ +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}; +pub fn test_0() { + let less: Ptr = libcc2rs::partial_ordering_less(); + let unordered: Ptr = libcc2rs::partial_ordering_unordered(); + assert!(({ (less).clone() } == { (libcc2rs::partial_ordering_less()) })); + assert!(({ (unordered).clone() } == { (libcc2rs::partial_ordering_unordered()) })); + assert!(({ (less).clone() } != { (unordered).clone() })); + assert!({ + let __a = libcc2rs::PartialOrdering::from( + ({ (*libcc2rs::partial_ordering_less().upgrade().deref()) }), + ); + let __b = libcc2rs::PartialOrdering::from( + ({ (*libcc2rs::partial_ordering_less().upgrade().deref()) }), + ); + libcc2rs::is_lt(__a) == libcc2rs::is_lt(__b) + && libcc2rs::is_eq(__a) == libcc2rs::is_eq(__b) + && libcc2rs::is_gt(__a) == libcc2rs::is_gt(__b) + }); + assert!({ + let __a = libcc2rs::PartialOrdering::from( + ({ (*libcc2rs::partial_ordering_equivalent().upgrade().deref()) }), + ); + let __b = libcc2rs::PartialOrdering::from( + ({ (*libcc2rs::partial_ordering_equivalent().upgrade().deref()) }), + ); + libcc2rs::is_lt(__a) == libcc2rs::is_lt(__b) + && libcc2rs::is_eq(__a) == libcc2rs::is_eq(__b) + && libcc2rs::is_gt(__a) == libcc2rs::is_gt(__b) + }); + assert!( + !({ + let __a = libcc2rs::PartialOrdering::from( + ({ (*libcc2rs::partial_ordering_less().upgrade().deref()) }), + ); + let __b = libcc2rs::PartialOrdering::from( + ({ (*libcc2rs::partial_ordering_greater().upgrade().deref()) }), + ); + libcc2rs::is_lt(__a) == libcc2rs::is_lt(__b) + && libcc2rs::is_eq(__a) == libcc2rs::is_eq(__b) + && libcc2rs::is_gt(__a) == libcc2rs::is_gt(__b) + }) + ); + assert!( + !({ + let __a = libcc2rs::PartialOrdering::from( + ({ (*libcc2rs::partial_ordering_equivalent().upgrade().deref()) }), + ); + let __b = libcc2rs::PartialOrdering::from( + ({ (*libcc2rs::partial_ordering_unordered().upgrade().deref()) }), + ); + libcc2rs::is_lt(__a) == libcc2rs::is_lt(__b) + && libcc2rs::is_eq(__a) == libcc2rs::is_eq(__b) + && libcc2rs::is_gt(__a) == libcc2rs::is_gt(__b) + }) + ); +} +pub fn test_1() { + let less: Value = Rc::new(RefCell::new( + (*libcc2rs::partial_ordering_less().upgrade().deref()), + )); + let equivalent: Value = Rc::new(RefCell::new( + (*libcc2rs::partial_ordering_equivalent().upgrade().deref()), + )); + let greater: Value = Rc::new(RefCell::new( + (*libcc2rs::partial_ordering_greater().upgrade().deref()), + )); + assert!( + ((((libcc2rs::is_lt(({ (*less.borrow()) }))) + && (libcc2rs::is_lteq(({ (*less.borrow()) })))) + && (!(libcc2rs::is_eq(({ (*less.borrow()) }))))) + && (!(libcc2rs::is_gt(({ (*less.borrow()) }))))) + && (!(libcc2rs::is_gteq(({ (*less.borrow()) })))) + ); + assert!( + ((libcc2rs::is_eq(({ (*equivalent.borrow()) }))) + && (libcc2rs::is_lteq(({ (*equivalent.borrow()) })))) + && (libcc2rs::is_gteq(({ (*equivalent.borrow()) }))) + ); + assert!( + (!(libcc2rs::is_lt(({ (*equivalent.borrow()) })))) + && (!(libcc2rs::is_gt(({ (*equivalent.borrow()) })))) + ); + assert!( + (((libcc2rs::is_gt(({ (*greater.borrow()) }))) + && (libcc2rs::is_gteq(({ (*greater.borrow()) })))) + && (!(libcc2rs::is_eq(({ (*greater.borrow()) }))))) + && (!(libcc2rs::is_lt(({ (*greater.borrow()) })))) + ); + assert!(!(libcc2rs::is_eq(({ (*less.borrow()) })))); + assert!(!(!(libcc2rs::is_eq(({ (*equivalent.borrow()) }))))); +} +pub fn test_2() { + let unordered: Value = Rc::new(RefCell::new( + (*libcc2rs::partial_ordering_unordered().upgrade().deref()), + )); + assert!(!(libcc2rs::is_eq(({ (*unordered.borrow()) })))); + assert!(!(libcc2rs::is_lt(({ (*unordered.borrow()) })))); + assert!(!(libcc2rs::is_gt(({ (*unordered.borrow()) })))); + assert!(!(libcc2rs::is_lteq(({ (*unordered.borrow()) })))); + assert!(!(libcc2rs::is_gteq(({ (*unordered.borrow()) })))); + assert!(!(libcc2rs::is_eq(({ (*unordered.borrow()) })))); +} +pub fn main() { + __cpp2rust_init_globals(); + libcc2rs::cpp2rust_exit(main_0()); +} +fn main_0() -> i32 { + ({ test_0() }); + ({ test_1() }); + ({ test_2() }); + return 0; +} +pub fn __cpp2rust_init_globals() {} diff --git a/tests/std/support/cmp/cmp.categories/out/refcount/cmp.strongord.rs b/tests/std/support/cmp/cmp.categories/out/refcount/cmp.strongord.rs index 8b737e4ea1..1d8c8d8a7c 100644 --- a/tests/std/support/cmp/cmp.categories/out/refcount/cmp.strongord.rs +++ b/tests/std/support/cmp/cmp.categories/out/refcount/cmp.strongord.rs @@ -6,25 +6,137 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -pub fn less_than_zero_0(o: std::cmp::Ordering) -> i32 { - let o: Value = Rc::new(RefCell::new(o)); - return ((({ (*o.borrow()) }) == std::cmp::Ordering::Less) as i32); +pub fn less_than_zero_0(o: libcc2rs::StrongOrdering) -> i32 { + let o: Value = Rc::new(RefCell::new(o)); + return ((libcc2rs::is_lt(({ (*o.borrow()) }))) as i32); } pub fn test_1() { - let o: Value = Rc::new(RefCell::new(std::cmp::Ord::cmp(&(1), &(2)))); - let mut a: i32 = ((({ (*o.borrow()) }) == std::cmp::Ordering::Less) as i32); + let o: Value = Rc::new(RefCell::new(libcc2rs::StrongOrdering::from( + std::cmp::Ord::cmp(&(1), &(2)), + ))); + let mut a: i32 = ((libcc2rs::is_lt(({ (*o.borrow()) }))) as i32); assert!((a == 1)); assert!((({ less_than_zero_0({ (*o.borrow()) },) }) == 1)); - assert!((((({ (*o.borrow()) }) == std::cmp::Ordering::Less) as i32) == 1)); - let mut d: i64 = ((({ (*o.borrow()) }) == std::cmp::Ordering::Greater) as i64); + assert!((((libcc2rs::is_lt(({ (*o.borrow()) }))) as i32) == 1)); + let mut d: i64 = ((libcc2rs::is_gt(({ (*o.borrow()) }))) as i64); assert!((d == 0_i64)); } +pub fn test_2() { + let less: Value = + Rc::new(RefCell::new(libcc2rs::PartialOrdering::from( + ({ (*libcc2rs::strong_ordering_less().upgrade().deref()) }), + ))); + let equal: Value = + Rc::new(RefCell::new(libcc2rs::PartialOrdering::from( + ({ (*libcc2rs::strong_ordering_equal().upgrade().deref()) }), + ))); + let greater: Value = + Rc::new(RefCell::new(libcc2rs::PartialOrdering::from( + ({ (*libcc2rs::strong_ordering_greater().upgrade().deref()) }), + ))); + assert!({ + let __a = libcc2rs::PartialOrdering::from(({ (*less.borrow()) })); + let __b = libcc2rs::PartialOrdering::from( + ({ (*libcc2rs::partial_ordering_less().upgrade().deref()) }), + ); + libcc2rs::is_lt(__a) == libcc2rs::is_lt(__b) + && libcc2rs::is_eq(__a) == libcc2rs::is_eq(__b) + && libcc2rs::is_gt(__a) == libcc2rs::is_gt(__b) + }); + assert!({ + let __a = libcc2rs::PartialOrdering::from(({ (*equal.borrow()) })); + let __b = libcc2rs::PartialOrdering::from( + ({ (*libcc2rs::partial_ordering_equivalent().upgrade().deref()) }), + ); + libcc2rs::is_lt(__a) == libcc2rs::is_lt(__b) + && libcc2rs::is_eq(__a) == libcc2rs::is_eq(__b) + && libcc2rs::is_gt(__a) == libcc2rs::is_gt(__b) + }); + assert!({ + let __a = libcc2rs::PartialOrdering::from(({ (*greater.borrow()) })); + let __b = libcc2rs::PartialOrdering::from( + ({ (*libcc2rs::partial_ordering_greater().upgrade().deref()) }), + ); + libcc2rs::is_lt(__a) == libcc2rs::is_lt(__b) + && libcc2rs::is_eq(__a) == libcc2rs::is_eq(__b) + && libcc2rs::is_gt(__a) == libcc2rs::is_gt(__b) + }); +} +pub fn test_3() { + let less: Value = Rc::new(RefCell::new(libcc2rs::WeakOrdering::from( + ({ (*libcc2rs::strong_ordering_less().upgrade().deref()) }), + ))); + let equal: Value = Rc::new(RefCell::new(libcc2rs::WeakOrdering::from( + ({ (*libcc2rs::strong_ordering_equal().upgrade().deref()) }), + ))); + let greater: Value = + Rc::new(RefCell::new(libcc2rs::WeakOrdering::from( + ({ (*libcc2rs::strong_ordering_greater().upgrade().deref()) }), + ))); + assert!({ + let __a = libcc2rs::PartialOrdering::from(({ (*less.borrow()) })); + let __b = libcc2rs::PartialOrdering::from( + ({ (*libcc2rs::weak_ordering_less().upgrade().deref()) }), + ); + libcc2rs::is_lt(__a) == libcc2rs::is_lt(__b) + && libcc2rs::is_eq(__a) == libcc2rs::is_eq(__b) + && libcc2rs::is_gt(__a) == libcc2rs::is_gt(__b) + }); + assert!({ + let __a = libcc2rs::PartialOrdering::from(({ (*equal.borrow()) })); + let __b = libcc2rs::PartialOrdering::from( + ({ (*libcc2rs::weak_ordering_equivalent().upgrade().deref()) }), + ); + libcc2rs::is_lt(__a) == libcc2rs::is_lt(__b) + && libcc2rs::is_eq(__a) == libcc2rs::is_eq(__b) + && libcc2rs::is_gt(__a) == libcc2rs::is_gt(__b) + }); + assert!({ + let __a = libcc2rs::PartialOrdering::from(({ (*greater.borrow()) })); + let __b = libcc2rs::PartialOrdering::from( + ({ (*libcc2rs::weak_ordering_greater().upgrade().deref()) }), + ); + libcc2rs::is_lt(__a) == libcc2rs::is_lt(__b) + && libcc2rs::is_eq(__a) == libcc2rs::is_eq(__b) + && libcc2rs::is_gt(__a) == libcc2rs::is_gt(__b) + }); +} +pub fn test_4() { + assert!({ + let __a = libcc2rs::PartialOrdering::from( + ({ (*libcc2rs::strong_ordering_equal().upgrade().deref()) }), + ); + let __b = libcc2rs::PartialOrdering::from( + ({ (*libcc2rs::strong_ordering_equivalent().upgrade().deref()) }), + ); + libcc2rs::is_lt(__a) == libcc2rs::is_lt(__b) + && libcc2rs::is_eq(__a) == libcc2rs::is_eq(__b) + && libcc2rs::is_gt(__a) == libcc2rs::is_gt(__b) + }); + assert!(((libcc2rs::strong_ordering_equal()) != (libcc2rs::strong_ordering_equivalent()))); + assert!( + !({ + let __a = libcc2rs::PartialOrdering::from( + ({ (*libcc2rs::strong_ordering_less().upgrade().deref()) }), + ); + let __b = libcc2rs::PartialOrdering::from( + ({ (*libcc2rs::strong_ordering_greater().upgrade().deref()) }), + ); + libcc2rs::is_lt(__a) == libcc2rs::is_lt(__b) + && libcc2rs::is_eq(__a) == libcc2rs::is_eq(__b) + && libcc2rs::is_gt(__a) == libcc2rs::is_gt(__b) + }) + ); +} pub fn main() { __cpp2rust_init_globals(); libcc2rs::cpp2rust_exit(main_0()); } fn main_0() -> i32 { ({ test_1() }); + ({ test_2() }); + ({ test_3() }); + ({ test_4() }); return 0; } pub fn __cpp2rust_init_globals() {} diff --git a/tests/std/support/cmp/cmp.categories/out/refcount/cmp.weakord.rs b/tests/std/support/cmp/cmp.categories/out/refcount/cmp.weakord.rs new file mode 100644 index 0000000000..fb72bd8657 --- /dev/null +++ b/tests/std/support/cmp/cmp.categories/out/refcount/cmp.weakord.rs @@ -0,0 +1,123 @@ +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}; +pub fn test_0() { + let less: Ptr = libcc2rs::weak_ordering_less(); + let greater: Ptr = libcc2rs::weak_ordering_greater(); + assert!(({ (less).clone() } == { (libcc2rs::weak_ordering_less()) })); + assert!(({ (less).clone() } != { (greater).clone() })); + assert!({ + let __a = libcc2rs::PartialOrdering::from( + ({ (*libcc2rs::weak_ordering_equivalent().upgrade().deref()) }), + ); + let __b = libcc2rs::PartialOrdering::from( + ({ (*libcc2rs::weak_ordering_equivalent().upgrade().deref()) }), + ); + libcc2rs::is_lt(__a) == libcc2rs::is_lt(__b) + && libcc2rs::is_eq(__a) == libcc2rs::is_eq(__b) + && libcc2rs::is_gt(__a) == libcc2rs::is_gt(__b) + }); + assert!( + !({ + let __a = libcc2rs::PartialOrdering::from( + ({ (*libcc2rs::weak_ordering_less().upgrade().deref()) }), + ); + let __b = libcc2rs::PartialOrdering::from( + ({ (*libcc2rs::weak_ordering_greater().upgrade().deref()) }), + ); + libcc2rs::is_lt(__a) == libcc2rs::is_lt(__b) + && libcc2rs::is_eq(__a) == libcc2rs::is_eq(__b) + && libcc2rs::is_gt(__a) == libcc2rs::is_gt(__b) + }) + ); +} +pub fn test_1() { + let less: Value = Rc::new(RefCell::new( + libcc2rs::PartialOrdering::from(({ (*libcc2rs::weak_ordering_less().upgrade().deref()) })), + )); + let equivalent: Value = + Rc::new(RefCell::new(libcc2rs::PartialOrdering::from( + ({ (*libcc2rs::weak_ordering_equivalent().upgrade().deref()) }), + ))); + let greater: Value = + Rc::new(RefCell::new(libcc2rs::PartialOrdering::from( + ({ (*libcc2rs::weak_ordering_greater().upgrade().deref()) }), + ))); + assert!({ + let __a = libcc2rs::PartialOrdering::from(({ (*less.borrow()) })); + let __b = libcc2rs::PartialOrdering::from( + ({ (*libcc2rs::partial_ordering_less().upgrade().deref()) }), + ); + libcc2rs::is_lt(__a) == libcc2rs::is_lt(__b) + && libcc2rs::is_eq(__a) == libcc2rs::is_eq(__b) + && libcc2rs::is_gt(__a) == libcc2rs::is_gt(__b) + }); + assert!({ + let __a = libcc2rs::PartialOrdering::from(({ (*equivalent.borrow()) })); + let __b = libcc2rs::PartialOrdering::from( + ({ (*libcc2rs::partial_ordering_equivalent().upgrade().deref()) }), + ); + libcc2rs::is_lt(__a) == libcc2rs::is_lt(__b) + && libcc2rs::is_eq(__a) == libcc2rs::is_eq(__b) + && libcc2rs::is_gt(__a) == libcc2rs::is_gt(__b) + }); + assert!({ + let __a = libcc2rs::PartialOrdering::from(({ (*greater.borrow()) })); + let __b = libcc2rs::PartialOrdering::from( + ({ (*libcc2rs::partial_ordering_greater().upgrade().deref()) }), + ); + libcc2rs::is_lt(__a) == libcc2rs::is_lt(__b) + && libcc2rs::is_eq(__a) == libcc2rs::is_eq(__b) + && libcc2rs::is_gt(__a) == libcc2rs::is_gt(__b) + }); +} +pub fn test_2() { + let less: Value = Rc::new(RefCell::new( + (*libcc2rs::weak_ordering_less().upgrade().deref()), + )); + let equivalent: Value = Rc::new(RefCell::new( + (*libcc2rs::weak_ordering_equivalent().upgrade().deref()), + )); + let greater: Value = Rc::new(RefCell::new( + (*libcc2rs::weak_ordering_greater().upgrade().deref()), + )); + assert!( + ((((libcc2rs::is_lt(({ (*less.borrow()) }))) + && (libcc2rs::is_lteq(({ (*less.borrow()) })))) + && (!(libcc2rs::is_eq(({ (*less.borrow()) }))))) + && (!(libcc2rs::is_gt(({ (*less.borrow()) }))))) + && (!(libcc2rs::is_gteq(({ (*less.borrow()) })))) + ); + assert!( + ((libcc2rs::is_eq(({ (*equivalent.borrow()) }))) + && (libcc2rs::is_lteq(({ (*equivalent.borrow()) })))) + && (libcc2rs::is_gteq(({ (*equivalent.borrow()) }))) + ); + assert!( + (!(libcc2rs::is_lt(({ (*equivalent.borrow()) })))) + && (!(libcc2rs::is_gt(({ (*equivalent.borrow()) })))) + ); + assert!( + (((libcc2rs::is_gt(({ (*greater.borrow()) }))) + && (libcc2rs::is_gteq(({ (*greater.borrow()) })))) + && (!(libcc2rs::is_eq(({ (*greater.borrow()) }))))) + && (!(libcc2rs::is_lt(({ (*greater.borrow()) })))) + ); + assert!(!(libcc2rs::is_eq(({ (*greater.borrow()) })))); +} +pub fn main() { + __cpp2rust_init_globals(); + libcc2rs::cpp2rust_exit(main_0()); +} +fn main_0() -> i32 { + ({ test_0() }); + ({ test_1() }); + ({ test_2() }); + return 0; +} +pub fn __cpp2rust_init_globals() {} diff --git a/tests/std/support/cmp/cmp.categories/out/unsafe/cmp.partialord.rs b/tests/std/support/cmp/cmp.categories/out/unsafe/cmp.partialord.rs new file mode 100644 index 0000000000..c57787d1fe --- /dev/null +++ b/tests/std/support/cmp/cmp.categories/out/unsafe/cmp.partialord.rs @@ -0,0 +1,98 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +pub unsafe fn test_0() { + let less: *const libcc2rs::PartialOrdering = &libcc2rs::PARTIAL_ORDERING_LESS; + let unordered: *const libcc2rs::PartialOrdering = &libcc2rs::PARTIAL_ORDERING_UNORDERED; + assert!( + ((less) + == (&raw const libcc2rs::PARTIAL_ORDERING_LESS as *const libcc2rs::PartialOrdering)) + ); + assert!( + ((unordered) + == (&raw const libcc2rs::PARTIAL_ORDERING_UNORDERED + as *const libcc2rs::PartialOrdering)) + ); + assert!(((less) != (unordered))); + assert!({ + let __a = libcc2rs::PartialOrdering::from(libcc2rs::PARTIAL_ORDERING_LESS); + let __b = libcc2rs::PartialOrdering::from(libcc2rs::PARTIAL_ORDERING_LESS); + libcc2rs::is_lt(__a) == libcc2rs::is_lt(__b) + && libcc2rs::is_eq(__a) == libcc2rs::is_eq(__b) + && libcc2rs::is_gt(__a) == libcc2rs::is_gt(__b) + }); + assert!({ + let __a = libcc2rs::PartialOrdering::from(libcc2rs::PARTIAL_ORDERING_EQUIVALENT); + let __b = libcc2rs::PartialOrdering::from(libcc2rs::PARTIAL_ORDERING_EQUIVALENT); + libcc2rs::is_lt(__a) == libcc2rs::is_lt(__b) + && libcc2rs::is_eq(__a) == libcc2rs::is_eq(__b) + && libcc2rs::is_gt(__a) == libcc2rs::is_gt(__b) + }); + assert!( + !({ + let __a = libcc2rs::PartialOrdering::from(libcc2rs::PARTIAL_ORDERING_LESS); + let __b = libcc2rs::PartialOrdering::from(libcc2rs::PARTIAL_ORDERING_GREATER); + libcc2rs::is_lt(__a) == libcc2rs::is_lt(__b) + && libcc2rs::is_eq(__a) == libcc2rs::is_eq(__b) + && libcc2rs::is_gt(__a) == libcc2rs::is_gt(__b) + }) + ); + assert!( + !({ + let __a = libcc2rs::PartialOrdering::from(libcc2rs::PARTIAL_ORDERING_EQUIVALENT); + let __b = libcc2rs::PartialOrdering::from(libcc2rs::PARTIAL_ORDERING_UNORDERED); + libcc2rs::is_lt(__a) == libcc2rs::is_lt(__b) + && libcc2rs::is_eq(__a) == libcc2rs::is_eq(__b) + && libcc2rs::is_gt(__a) == libcc2rs::is_gt(__b) + }) + ); +} +pub unsafe fn test_1() { + let mut less: libcc2rs::PartialOrdering = libcc2rs::PARTIAL_ORDERING_LESS; + let mut equivalent: libcc2rs::PartialOrdering = libcc2rs::PARTIAL_ORDERING_EQUIVALENT; + let mut greater: libcc2rs::PartialOrdering = libcc2rs::PARTIAL_ORDERING_GREATER; + assert!( + ((((libcc2rs::is_lt(less)) && (libcc2rs::is_lteq(less))) && (!(libcc2rs::is_eq(less)))) + && (!(libcc2rs::is_gt(less)))) + && (!(libcc2rs::is_gteq(less))) + ); + assert!( + ((libcc2rs::is_eq(equivalent)) && (libcc2rs::is_lteq(equivalent))) + && (libcc2rs::is_gteq(equivalent)) + ); + assert!((!(libcc2rs::is_lt(equivalent))) && (!(libcc2rs::is_gt(equivalent)))); + assert!( + (((libcc2rs::is_gt(greater)) && (libcc2rs::is_gteq(greater))) + && (!(libcc2rs::is_eq(greater)))) + && (!(libcc2rs::is_lt(greater))) + ); + assert!(!(libcc2rs::is_eq(less))); + assert!(!(!(libcc2rs::is_eq(equivalent)))); +} +pub unsafe fn test_2() { + let mut unordered: libcc2rs::PartialOrdering = libcc2rs::PARTIAL_ORDERING_UNORDERED; + assert!(!(libcc2rs::is_eq(unordered))); + assert!(!(libcc2rs::is_lt(unordered))); + assert!(!(libcc2rs::is_gt(unordered))); + assert!(!(libcc2rs::is_lteq(unordered))); + assert!(!(libcc2rs::is_gteq(unordered))); + assert!(!(libcc2rs::is_eq(unordered))); +} +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() }); + (unsafe { test_2() }); + return 0; +} +pub unsafe fn __cpp2rust_init_globals() {} diff --git a/tests/std/support/cmp/cmp.categories/out/unsafe/cmp.strongord.rs b/tests/std/support/cmp/cmp.categories/out/unsafe/cmp.strongord.rs index 9ed9b138ef..2c3e025cd2 100644 --- a/tests/std/support/cmp/cmp.categories/out/unsafe/cmp.strongord.rs +++ b/tests/std/support/cmp/cmp.categories/out/unsafe/cmp.strongord.rs @@ -6,18 +6,100 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; -pub unsafe fn less_than_zero_0(mut o: std::cmp::Ordering) -> i32 { - return ((o == std::cmp::Ordering::Less) as i32); +pub unsafe fn less_than_zero_0(mut o: libcc2rs::StrongOrdering) -> i32 { + return ((libcc2rs::is_lt(o)) as i32); } pub unsafe fn test_1() { - let mut o: std::cmp::Ordering = std::cmp::Ord::cmp(&(1), &(2)); - let mut a: i32 = ((o == std::cmp::Ordering::Less) as i32); + let mut o: libcc2rs::StrongOrdering = + libcc2rs::StrongOrdering::from(std::cmp::Ord::cmp(&(1), &(2))); + let mut a: i32 = ((libcc2rs::is_lt(o)) as i32); assert!(((a) == (1))); assert!(((unsafe { less_than_zero_0(o,) }) == (1))); - assert!((((o == std::cmp::Ordering::Less) as i32) == (1))); - let mut d: i64 = ((o == std::cmp::Ordering::Greater) as i64); + assert!((((libcc2rs::is_lt(o)) as i32) == (1))); + let mut d: i64 = ((libcc2rs::is_gt(o)) as i64); assert!(((d) == (0_i64))); } +pub unsafe fn test_2() { + let mut less: libcc2rs::PartialOrdering = + libcc2rs::PartialOrdering::from(libcc2rs::STRONG_ORDERING_LESS); + let mut equal: libcc2rs::PartialOrdering = + libcc2rs::PartialOrdering::from(libcc2rs::STRONG_ORDERING_EQUAL); + let mut greater: libcc2rs::PartialOrdering = + libcc2rs::PartialOrdering::from(libcc2rs::STRONG_ORDERING_GREATER); + assert!({ + let __a = libcc2rs::PartialOrdering::from(less); + let __b = libcc2rs::PartialOrdering::from(libcc2rs::PARTIAL_ORDERING_LESS); + libcc2rs::is_lt(__a) == libcc2rs::is_lt(__b) + && libcc2rs::is_eq(__a) == libcc2rs::is_eq(__b) + && libcc2rs::is_gt(__a) == libcc2rs::is_gt(__b) + }); + assert!({ + let __a = libcc2rs::PartialOrdering::from(equal); + let __b = libcc2rs::PartialOrdering::from(libcc2rs::PARTIAL_ORDERING_EQUIVALENT); + libcc2rs::is_lt(__a) == libcc2rs::is_lt(__b) + && libcc2rs::is_eq(__a) == libcc2rs::is_eq(__b) + && libcc2rs::is_gt(__a) == libcc2rs::is_gt(__b) + }); + assert!({ + let __a = libcc2rs::PartialOrdering::from(greater); + let __b = libcc2rs::PartialOrdering::from(libcc2rs::PARTIAL_ORDERING_GREATER); + libcc2rs::is_lt(__a) == libcc2rs::is_lt(__b) + && libcc2rs::is_eq(__a) == libcc2rs::is_eq(__b) + && libcc2rs::is_gt(__a) == libcc2rs::is_gt(__b) + }); +} +pub unsafe fn test_3() { + let mut less: libcc2rs::WeakOrdering = + libcc2rs::WeakOrdering::from(libcc2rs::STRONG_ORDERING_LESS); + let mut equal: libcc2rs::WeakOrdering = + libcc2rs::WeakOrdering::from(libcc2rs::STRONG_ORDERING_EQUAL); + let mut greater: libcc2rs::WeakOrdering = + libcc2rs::WeakOrdering::from(libcc2rs::STRONG_ORDERING_GREATER); + assert!({ + let __a = libcc2rs::PartialOrdering::from(less); + let __b = libcc2rs::PartialOrdering::from(libcc2rs::WEAK_ORDERING_LESS); + libcc2rs::is_lt(__a) == libcc2rs::is_lt(__b) + && libcc2rs::is_eq(__a) == libcc2rs::is_eq(__b) + && libcc2rs::is_gt(__a) == libcc2rs::is_gt(__b) + }); + assert!({ + let __a = libcc2rs::PartialOrdering::from(equal); + let __b = libcc2rs::PartialOrdering::from(libcc2rs::WEAK_ORDERING_EQUIVALENT); + libcc2rs::is_lt(__a) == libcc2rs::is_lt(__b) + && libcc2rs::is_eq(__a) == libcc2rs::is_eq(__b) + && libcc2rs::is_gt(__a) == libcc2rs::is_gt(__b) + }); + assert!({ + let __a = libcc2rs::PartialOrdering::from(greater); + let __b = libcc2rs::PartialOrdering::from(libcc2rs::WEAK_ORDERING_GREATER); + libcc2rs::is_lt(__a) == libcc2rs::is_lt(__b) + && libcc2rs::is_eq(__a) == libcc2rs::is_eq(__b) + && libcc2rs::is_gt(__a) == libcc2rs::is_gt(__b) + }); +} +pub unsafe fn test_4() { + assert!({ + let __a = libcc2rs::PartialOrdering::from(libcc2rs::STRONG_ORDERING_EQUAL); + let __b = libcc2rs::PartialOrdering::from(libcc2rs::STRONG_ORDERING_EQUIVALENT); + libcc2rs::is_lt(__a) == libcc2rs::is_lt(__b) + && libcc2rs::is_eq(__a) == libcc2rs::is_eq(__b) + && libcc2rs::is_gt(__a) == libcc2rs::is_gt(__b) + }); + assert!( + ((&raw const libcc2rs::STRONG_ORDERING_EQUAL as *const libcc2rs::StrongOrdering) + != (&raw const libcc2rs::STRONG_ORDERING_EQUIVALENT + as *const libcc2rs::StrongOrdering)) + ); + assert!( + !({ + let __a = libcc2rs::PartialOrdering::from(libcc2rs::STRONG_ORDERING_LESS); + let __b = libcc2rs::PartialOrdering::from(libcc2rs::STRONG_ORDERING_GREATER); + libcc2rs::is_lt(__a) == libcc2rs::is_lt(__b) + && libcc2rs::is_eq(__a) == libcc2rs::is_eq(__b) + && libcc2rs::is_gt(__a) == libcc2rs::is_gt(__b) + }) + ); +} pub fn main() { unsafe { __cpp2rust_init_globals(); @@ -26,6 +108,9 @@ pub fn main() { } unsafe fn main_0() -> i32 { (unsafe { test_1() }); + (unsafe { test_2() }); + (unsafe { test_3() }); + (unsafe { test_4() }); return 0; } pub unsafe fn __cpp2rust_init_globals() {} diff --git a/tests/std/support/cmp/cmp.categories/out/unsafe/cmp.weakord.rs b/tests/std/support/cmp/cmp.categories/out/unsafe/cmp.weakord.rs new file mode 100644 index 0000000000..7be650eacb --- /dev/null +++ b/tests/std/support/cmp/cmp.categories/out/unsafe/cmp.weakord.rs @@ -0,0 +1,93 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +pub unsafe fn test_0() { + let less: *const libcc2rs::WeakOrdering = &libcc2rs::WEAK_ORDERING_LESS; + let greater: *const libcc2rs::WeakOrdering = &libcc2rs::WEAK_ORDERING_GREATER; + assert!(((less) == (&raw const libcc2rs::WEAK_ORDERING_LESS as *const libcc2rs::WeakOrdering))); + assert!(((less) != (greater))); + assert!({ + let __a = libcc2rs::PartialOrdering::from(libcc2rs::WEAK_ORDERING_EQUIVALENT); + let __b = libcc2rs::PartialOrdering::from(libcc2rs::WEAK_ORDERING_EQUIVALENT); + libcc2rs::is_lt(__a) == libcc2rs::is_lt(__b) + && libcc2rs::is_eq(__a) == libcc2rs::is_eq(__b) + && libcc2rs::is_gt(__a) == libcc2rs::is_gt(__b) + }); + assert!( + !({ + let __a = libcc2rs::PartialOrdering::from(libcc2rs::WEAK_ORDERING_LESS); + let __b = libcc2rs::PartialOrdering::from(libcc2rs::WEAK_ORDERING_GREATER); + libcc2rs::is_lt(__a) == libcc2rs::is_lt(__b) + && libcc2rs::is_eq(__a) == libcc2rs::is_eq(__b) + && libcc2rs::is_gt(__a) == libcc2rs::is_gt(__b) + }) + ); +} +pub unsafe fn test_1() { + let mut less: libcc2rs::PartialOrdering = + libcc2rs::PartialOrdering::from(libcc2rs::WEAK_ORDERING_LESS); + let mut equivalent: libcc2rs::PartialOrdering = + libcc2rs::PartialOrdering::from(libcc2rs::WEAK_ORDERING_EQUIVALENT); + let mut greater: libcc2rs::PartialOrdering = + libcc2rs::PartialOrdering::from(libcc2rs::WEAK_ORDERING_GREATER); + assert!({ + let __a = libcc2rs::PartialOrdering::from(less); + let __b = libcc2rs::PartialOrdering::from(libcc2rs::PARTIAL_ORDERING_LESS); + libcc2rs::is_lt(__a) == libcc2rs::is_lt(__b) + && libcc2rs::is_eq(__a) == libcc2rs::is_eq(__b) + && libcc2rs::is_gt(__a) == libcc2rs::is_gt(__b) + }); + assert!({ + let __a = libcc2rs::PartialOrdering::from(equivalent); + let __b = libcc2rs::PartialOrdering::from(libcc2rs::PARTIAL_ORDERING_EQUIVALENT); + libcc2rs::is_lt(__a) == libcc2rs::is_lt(__b) + && libcc2rs::is_eq(__a) == libcc2rs::is_eq(__b) + && libcc2rs::is_gt(__a) == libcc2rs::is_gt(__b) + }); + assert!({ + let __a = libcc2rs::PartialOrdering::from(greater); + let __b = libcc2rs::PartialOrdering::from(libcc2rs::PARTIAL_ORDERING_GREATER); + libcc2rs::is_lt(__a) == libcc2rs::is_lt(__b) + && libcc2rs::is_eq(__a) == libcc2rs::is_eq(__b) + && libcc2rs::is_gt(__a) == libcc2rs::is_gt(__b) + }); +} +pub unsafe fn test_2() { + let mut less: libcc2rs::WeakOrdering = libcc2rs::WEAK_ORDERING_LESS; + let mut equivalent: libcc2rs::WeakOrdering = libcc2rs::WEAK_ORDERING_EQUIVALENT; + let mut greater: libcc2rs::WeakOrdering = libcc2rs::WEAK_ORDERING_GREATER; + assert!( + ((((libcc2rs::is_lt(less)) && (libcc2rs::is_lteq(less))) && (!(libcc2rs::is_eq(less)))) + && (!(libcc2rs::is_gt(less)))) + && (!(libcc2rs::is_gteq(less))) + ); + assert!( + ((libcc2rs::is_eq(equivalent)) && (libcc2rs::is_lteq(equivalent))) + && (libcc2rs::is_gteq(equivalent)) + ); + assert!((!(libcc2rs::is_lt(equivalent))) && (!(libcc2rs::is_gt(equivalent)))); + assert!( + (((libcc2rs::is_gt(greater)) && (libcc2rs::is_gteq(greater))) + && (!(libcc2rs::is_eq(greater)))) + && (!(libcc2rs::is_lt(greater))) + ); + assert!(!(libcc2rs::is_eq(greater))); +} +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() }); + (unsafe { test_2() }); + return 0; +} +pub unsafe fn __cpp2rust_init_globals() {} diff --git a/tests/std/support/cmp/cmp.concept.cpp b/tests/std/support/cmp/cmp.concept.cpp new file mode 100644 index 0000000000..6158901b9d --- /dev/null +++ b/tests/std/support/cmp/cmp.concept.cpp @@ -0,0 +1,112 @@ +// ADDITIONAL_COMPILE_FLAGS: -std=c++23 +#include +#include + +// N4950 [cmp.concept]/1: a type is three_way_comparable when it has ==, the +// four relations, and a <=> whose result is a comparison category. +namespace p1_defaulted_three_way { +struct Version { + int major; + int minor; + std::strong_ordering operator<=>(const Version &) const = default; +}; + +static_assert(std::three_way_comparable); +static_assert(std::three_way_comparable); + +void test() { + Version old_version{1, 9}; + Version new_version{2, 0}; + std::strong_ordering order = old_version <=> new_version; + assert(order < 0); + assert((new_version <=> old_version) > 0); + assert((old_version <=> old_version) == 0); + assert(old_version == old_version); + assert(old_version < new_version); + assert(new_version >= old_version); +} +} // namespace p1_defaulted_three_way + +// N4950 [cmp.concept]/1: the type may write <=> and == itself. +namespace p1_user_written_three_way { +static int three_way_calls = 0; + +struct Money { + int cents; + bool operator==(const Money &other) const { return cents == other.cents; } + std::strong_ordering operator<=>(const Money &other) const { + ++three_way_calls; + return cents <=> other.cents; + } +}; + +static_assert(std::three_way_comparable); + +void test() { + Money small{150}; + Money large{900}; + assert((small <=> large) < 0); + assert(small < large); + assert(large > small); + assert(small <= small); + assert(large >= small); + assert(three_way_calls == 5); + assert(small == small); + assert(three_way_calls == 5); +} +} // namespace p1_user_written_three_way + +// N4950 [cmp.concept]/1: the category of <=> may be weaker than +// strong_ordering, and is then the strongest category the type models. +namespace p1_weaker_category { +struct Temperature { + double degrees; + bool operator==(const Temperature &other) const { + return degrees == other.degrees; + } + std::partial_ordering operator<=>(const Temperature &other) const { + return degrees <=> other.degrees; + } +}; + +static_assert(std::three_way_comparable); +static_assert(!std::three_way_comparable); + +void test() { + Temperature cold{-5.0}; + Temperature warm{21.5}; + std::partial_ordering order = cold <=> warm; + assert(order < 0); + assert(cold < warm); + assert(warm >= cold); +} +} // namespace p1_weaker_category + +// N4950 [cmp.concept]/1: without ==, a type with <=> is not +// three_way_comparable, though its relations still work. +namespace p1_three_way_without_equality { +struct Rank { + int value; + std::strong_ordering operator<=>(const Rank &other) const { + return value <=> other.value; + } +}; + +static_assert(!std::three_way_comparable); + +void test() { + Rank low{1}; + Rank high{2}; + assert((low <=> high) < 0); + assert(low < high); + assert(high >= low); +} +} // namespace p1_three_way_without_equality + +int main() { + p1_defaulted_three_way::test(); + p1_user_written_three_way::test(); + p1_weaker_category::test(); + p1_three_way_without_equality::test(); + return 0; +} diff --git a/tests/std/support/cmp/compare.syn.cpp b/tests/std/support/cmp/compare.syn.cpp new file mode 100644 index 0000000000..10f9a4e570 --- /dev/null +++ b/tests/std/support/cmp/compare.syn.cpp @@ -0,0 +1,43 @@ +// ADDITIONAL_COMPILE_FLAGS: -std=c++23 +#include +#include + +// N4950 [compare.syn]: the named comparison functions test the result of a +// comparison against zero. +namespace named_comparison_functions { +void test() { + std::partial_ordering less = std::partial_ordering::less; + std::partial_ordering equivalent = std::partial_ordering::equivalent; + std::partial_ordering greater = std::partial_ordering::greater; + std::partial_ordering unordered = std::partial_ordering::unordered; + assert(std::is_eq(equivalent) && !std::is_eq(less) && !std::is_eq(unordered)); + assert(std::is_neq(less) && std::is_neq(unordered) && + !std::is_neq(equivalent)); + assert(std::is_lt(less) && !std::is_lt(equivalent) && !std::is_lt(unordered)); + assert(std::is_lteq(less) && std::is_lteq(equivalent) && + !std::is_lteq(greater)); + assert(!std::is_lteq(unordered)); + assert(std::is_gt(greater) && !std::is_gt(equivalent) && + !std::is_gt(unordered)); + assert(std::is_gteq(greater) && std::is_gteq(equivalent) && + !std::is_gteq(less)); + assert(!std::is_gteq(unordered)); +} +} // namespace named_comparison_functions + +// N4950 [compare.syn]: the functions take a partial_ordering, to which the +// other categories convert. +namespace stronger_categories_convert { +void test() { + assert(std::is_lt(std::strong_ordering::less)); + assert(std::is_eq(std::strong_ordering::equal)); + assert(std::is_gt(std::weak_ordering::greater)); + assert(std::is_lteq(1 <=> 2)); +} +} // namespace stronger_categories_convert + +int main() { + named_comparison_functions::test(); + stronger_categories_convert::test(); + return 0; +} diff --git a/tests/std/support/cmp/out/refcount/cmp.concept.rs b/tests/std/support/cmp/out/refcount/cmp.concept.rs new file mode 100644 index 0000000000..928af2f6a4 --- /dev/null +++ b/tests/std/support/cmp/out/refcount/cmp.concept.rs @@ -0,0 +1,319 @@ +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, + TriviallyDefaultConstructible, + TriviallyCopyable, + Default, + PartiallyOrderedWith, +)] +#[three_way(ThreeWayComparable::three_way)] +#[byte_size(8)] +pub struct p1_defaulted_three_way_Version { + #[offset(0)] + pub major: i32, + #[offset(4)] + pub minor: i32, +} +impl ThreeWayComparable for p1_defaulted_three_way_Version { + type Category = libcc2rs::StrongOrdering; + fn three_way( + this: Ptr, + _a0: Ptr, + ) -> libcc2rs::StrongOrdering { + { + let cmp: Value = + Rc::new(RefCell::new(libcc2rs::StrongOrdering::from( + std::cmp::Ord::cmp(&(this.with(|__s| __s.major)), &(_a0.with(|__s| __s.major))), + ))); + if !(libcc2rs::is_eq(({ (*cmp.borrow()) }))) { + return (*cmp.borrow_mut()); + } + } + { + let cmp: Value = + Rc::new(RefCell::new(libcc2rs::StrongOrdering::from( + std::cmp::Ord::cmp(&(this.with(|__s| __s.minor)), &(_a0.with(|__s| __s.minor))), + ))); + if !(libcc2rs::is_eq(({ (*cmp.borrow()) }))) { + return (*cmp.borrow_mut()); + } + } + return (*libcc2rs::strong_ordering_equal().upgrade().deref()); + } +} +impl LessThanComparable for p1_defaulted_three_way_Version { + fn less(this: Ptr, _a0: Ptr) -> bool { + libcc2rs::is_lt(ThreeWayComparable::three_way(this.clone(), _a0.clone())) + } +} +const _: () = assert!(true, "std::three_way_comparable"); +const _: () = assert!( + true, + "std::three_way_comparable" +); +pub fn test_0() { + let old_version: Value = + Rc::new(RefCell::new(p1_defaulted_three_way_Version { + major: 1, + minor: 9, + })); + let new_version: Value = + Rc::new(RefCell::new(p1_defaulted_three_way_Version { + major: 2, + minor: 0, + })); + let order: Value = Rc::new(RefCell::new( + ({ ThreeWayComparable::three_way(old_version.as_pointer(), new_version.as_pointer()) }), + )); + assert!(libcc2rs::is_lt(({ (*order.borrow()) }))); + assert!(libcc2rs::is_gt( + ({ ThreeWayComparable::three_way(new_version.as_pointer(), old_version.as_pointer(),) }) + )); + assert!(libcc2rs::is_eq( + ({ ThreeWayComparable::three_way(old_version.as_pointer(), old_version.as_pointer(),) }) + )); + assert!( + ({ + WeaklyEqualityComparableWith::equal(old_version.as_pointer(), old_version.as_pointer()) + }) + ); + assert!(libcc2rs::is_lt( + ({ ThreeWayComparable::three_way(old_version.as_pointer(), new_version.as_pointer(),) }) + )); + assert!(libcc2rs::is_gteq( + ({ ThreeWayComparable::three_way(new_version.as_pointer(), old_version.as_pointer(),) }) + )); +} +thread_local!( + pub static three_way_calls_1: Value = Rc::new(RefCell::new(0)); +); +#[derive( + Copy, + Clone, + Record, + ByteRepr, + ZeroInitialization, + VaArg, + FnPtrArg, + TriviallyDefaultConstructible, + TriviallyCopyable, + Default, + PartiallyOrderedWith, +)] +#[three_way(ThreeWayComparable::three_way)] +#[byte_size(4)] +pub struct p1_user_written_three_way_Money { + #[offset(0)] + pub cents: i32, +} +impl ThreeWayComparable for p1_user_written_three_way_Money { + type Category = libcc2rs::StrongOrdering; + fn three_way( + this: Ptr, + other: Ptr, + ) -> libcc2rs::StrongOrdering { + (*three_way_calls_1.with(Value::clone).borrow_mut()).prefix_inc(); + return libcc2rs::StrongOrdering::from(std::cmp::Ord::cmp( + &(this.with(|__s| __s.cents)), + &(other.with(|__s| __s.cents)), + )); + } +} +impl LessThanComparable for p1_user_written_three_way_Money { + fn less(this: Ptr, other: Ptr) -> bool { + libcc2rs::is_lt(ThreeWayComparable::three_way(this.clone(), other.clone())) + } +} +const _: () = assert!(true, "std::three_way_comparable"); +pub fn test_2() { + let small: Value = + Rc::new(RefCell::new(p1_user_written_three_way_Money { cents: 150 })); + let large: Value = + Rc::new(RefCell::new(p1_user_written_three_way_Money { cents: 900 })); + assert!(libcc2rs::is_lt( + ({ ThreeWayComparable::three_way(small.as_pointer(), large.as_pointer(),) }) + )); + assert!(libcc2rs::is_lt( + ({ ThreeWayComparable::three_way(small.as_pointer(), large.as_pointer(),) }) + )); + assert!(libcc2rs::is_gt( + ({ ThreeWayComparable::three_way(large.as_pointer(), small.as_pointer(),) }) + )); + assert!(libcc2rs::is_lteq( + ({ ThreeWayComparable::three_way(small.as_pointer(), small.as_pointer(),) }) + )); + assert!(libcc2rs::is_gteq( + ({ ThreeWayComparable::three_way(large.as_pointer(), small.as_pointer(),) }) + )); + assert!((three_way_calls_1.with(|rc| *rc.borrow()) == 5)); + assert!(({ WeaklyEqualityComparableWith::equal(small.as_pointer(), small.as_pointer(),) })); + assert!((three_way_calls_1.with(|rc| *rc.borrow()) == 5)); +} +#[derive( + Copy, + Clone, + Record, + ByteRepr, + ZeroInitialization, + VaArg, + FnPtrArg, + TriviallyDefaultConstructible, + TriviallyCopyable, + Default, + PartiallyOrderedWith, +)] +#[three_way(ThreeWayComparable::three_way)] +#[byte_size(8)] +pub struct p1_weaker_category_Temperature { + #[offset(0)] + pub degrees: f64, +} +impl ThreeWayComparable for p1_weaker_category_Temperature { + type Category = libcc2rs::PartialOrdering; + fn three_way( + this: Ptr, + other: Ptr, + ) -> libcc2rs::PartialOrdering { + return libcc2rs::PartialOrdering::from(std::cmp::PartialOrd::partial_cmp( + &(this.with(|__s| __s.degrees)), + &(other.with(|__s| __s.degrees)), + )); + } +} +impl LessThanComparable for p1_weaker_category_Temperature { + fn less(this: Ptr, other: Ptr) -> bool { + libcc2rs::is_lt(ThreeWayComparable::three_way(this.clone(), other.clone())) + } +} +const _: () = assert!(true, "std::three_way_comparable"); +const _: () = assert!( + !(false), + "!std::three_way_comparable" +); +pub fn test_3() { + let cold: Value = + Rc::new(RefCell::new(p1_weaker_category_Temperature { + degrees: -5_f64, + })); + let warm: Value = + Rc::new(RefCell::new(p1_weaker_category_Temperature { + degrees: 21.5_f64, + })); + let order: Value = Rc::new(RefCell::new( + ({ ThreeWayComparable::three_way(cold.as_pointer(), warm.as_pointer()) }), + )); + assert!(libcc2rs::is_lt(({ (*order.borrow()) }))); + assert!(libcc2rs::is_lt( + ({ ThreeWayComparable::three_way(cold.as_pointer(), warm.as_pointer(),) }) + )); + assert!(libcc2rs::is_gteq( + ({ ThreeWayComparable::three_way(warm.as_pointer(), cold.as_pointer(),) }) + )); +} +#[derive( + Copy, + Clone, + Record, + ByteRepr, + ZeroInitialization, + VaArg, + FnPtrArg, + TriviallyDefaultConstructible, + TriviallyCopyable, + Default, + PartiallyOrderedWith, +)] +#[three_way(p1_three_way_without_equality_Rank::operator_cmp)] +#[byte_size(4)] +pub struct p1_three_way_without_equality_Rank { + #[offset(0)] + pub value: i32, +} +impl p1_three_way_without_equality_Rank { + pub fn operator_cmp( + this: Ptr, + other: Ptr, + ) -> libcc2rs::StrongOrdering { + return libcc2rs::StrongOrdering::from(std::cmp::Ord::cmp( + &(this.with(|__s| __s.value)), + &(other.with(|__s| __s.value)), + )); + } +} +impl LessThanComparable for p1_three_way_without_equality_Rank { + fn less(this: Ptr, other: Ptr) -> bool { + libcc2rs::is_lt(p1_three_way_without_equality_Rank::operator_cmp( + this.clone(), + other.clone(), + )) + } +} +const _: () = assert!(!(false), "!std::three_way_comparable"); +pub fn test_4() { + let low: Value = + Rc::new(RefCell::new(p1_three_way_without_equality_Rank { + value: 1, + })); + let high: Value = + Rc::new(RefCell::new(p1_three_way_without_equality_Rank { + value: 2, + })); + assert!(libcc2rs::is_lt( + ({ + p1_three_way_without_equality_Rank::operator_cmp(low.as_pointer(), high.as_pointer()) + }) + )); + assert!(libcc2rs::is_lt( + ({ + p1_three_way_without_equality_Rank::operator_cmp(low.as_pointer(), high.as_pointer()) + }) + )); + assert!(libcc2rs::is_gteq( + ({ + p1_three_way_without_equality_Rank::operator_cmp(high.as_pointer(), low.as_pointer()) + }) + )); +} +pub fn main() { + __cpp2rust_init_globals(); + libcc2rs::cpp2rust_exit(main_0()); +} +fn main_0() -> i32 { + ({ test_0() }); + ({ test_2() }); + ({ test_3() }); + ({ test_4() }); + return 0; +} +impl WeaklyEqualityComparableWith for p1_defaulted_three_way_Version { + fn equal(this: Ptr, _a0: Ptr) -> bool { + return ({ this.with(|__s| __s.major) } == { _a0.with(|__s| __s.major) }) + && ({ this.with(|__s| __s.minor) } == { _a0.with(|__s| __s.minor) }); + } +} +impl WeaklyEqualityComparableWith for p1_user_written_three_way_Money { + fn equal(this: Ptr, other: Ptr) -> bool { + return ({ this.with(|__s| __s.cents) } == { other.with(|__s| __s.cents) }); + } +} +impl WeaklyEqualityComparableWith for p1_weaker_category_Temperature { + fn equal(this: Ptr, other: Ptr) -> bool { + return ({ this.with(|__s| __s.degrees) } == { other.with(|__s| __s.degrees) }); + } +} +pub fn __cpp2rust_init_globals() { + let _ = three_way_calls_1.with(|_| ()); +} diff --git a/tests/std/support/cmp/out/refcount/cmp.strongord.rs b/tests/std/support/cmp/out/refcount/cmp.strongord.rs index 93431ba092..c00e065258 100644 --- a/tests/std/support/cmp/out/refcount/cmp.strongord.rs +++ b/tests/std/support/cmp/out/refcount/cmp.strongord.rs @@ -7,15 +7,15 @@ use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; pub fn test_0() { - let l: Ptr = libcc2rs::strong_ordering_less(); - let e: Ptr = libcc2rs::strong_ordering_equal(); - let g: Ptr = libcc2rs::strong_ordering_greater(); + let l: Ptr = libcc2rs::strong_ordering_less(); + let e: Ptr = libcc2rs::strong_ordering_equal(); + let g: Ptr = libcc2rs::strong_ordering_greater(); assert!(({ (l).clone() } == { (libcc2rs::strong_ordering_less()) })); assert!(({ (e).clone() } == { (libcc2rs::strong_ordering_equal()) })); assert!(({ (g).clone() } == { (libcc2rs::strong_ordering_greater()) })); - assert!(({ (*l.upgrade().deref()) }) == std::cmp::Ordering::Less); - assert!(({ (*e.upgrade().deref()) }) == std::cmp::Ordering::Equal); - assert!(({ (*g.upgrade().deref()) }) == std::cmp::Ordering::Greater); + assert!(libcc2rs::is_lt(({ (*l.upgrade().deref()) }))); + assert!(libcc2rs::is_eq(({ (*e.upgrade().deref()) }))); + assert!(libcc2rs::is_gt(({ (*g.upgrade().deref()) }))); } pub fn main() { __cpp2rust_init_globals(); diff --git a/tests/std/support/cmp/out/refcount/compare.syn.rs b/tests/std/support/cmp/out/refcount/compare.syn.rs new file mode 100644 index 0000000000..60b9869841 --- /dev/null +++ b/tests/std/support/cmp/out/refcount/compare.syn.rs @@ -0,0 +1,78 @@ +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}; +pub fn test_0() { + let less: Value = Rc::new(RefCell::new( + (*libcc2rs::partial_ordering_less().upgrade().deref()), + )); + let equivalent: Value = Rc::new(RefCell::new( + (*libcc2rs::partial_ordering_equivalent().upgrade().deref()), + )); + let greater: Value = Rc::new(RefCell::new( + (*libcc2rs::partial_ordering_greater().upgrade().deref()), + )); + let unordered: Value = Rc::new(RefCell::new( + (*libcc2rs::partial_ordering_unordered().upgrade().deref()), + )); + assert!( + ((libcc2rs::is_eq(({ (*equivalent.borrow()) }))) + && (!(libcc2rs::is_eq(({ (*less.borrow()) }))))) + && (!(libcc2rs::is_eq(({ (*unordered.borrow()) })))) + ); + assert!( + ((libcc2rs::is_neq(({ (*less.borrow()) }))) + && (libcc2rs::is_neq(({ (*unordered.borrow()) })))) + && (!(libcc2rs::is_neq(({ (*equivalent.borrow()) })))) + ); + assert!( + ((libcc2rs::is_lt(({ (*less.borrow()) }))) + && (!(libcc2rs::is_lt(({ (*equivalent.borrow()) }))))) + && (!(libcc2rs::is_lt(({ (*unordered.borrow()) })))) + ); + assert!( + ((libcc2rs::is_lteq(({ (*less.borrow()) }))) + && (libcc2rs::is_lteq(({ (*equivalent.borrow()) })))) + && (!(libcc2rs::is_lteq(({ (*greater.borrow()) })))) + ); + assert!(!(libcc2rs::is_lteq(({ (*unordered.borrow()) })))); + assert!( + ((libcc2rs::is_gt(({ (*greater.borrow()) }))) + && (!(libcc2rs::is_gt(({ (*equivalent.borrow()) }))))) + && (!(libcc2rs::is_gt(({ (*unordered.borrow()) })))) + ); + assert!( + ((libcc2rs::is_gteq(({ (*greater.borrow()) }))) + && (libcc2rs::is_gteq(({ (*equivalent.borrow()) })))) + && (!(libcc2rs::is_gteq(({ (*less.borrow()) })))) + ); + assert!(!(libcc2rs::is_gteq(({ (*unordered.borrow()) })))); +} +pub fn test_1() { + assert!(libcc2rs::is_lt(libcc2rs::PartialOrdering::from( + ({ (*libcc2rs::strong_ordering_less().upgrade().deref()) }) + ))); + assert!(libcc2rs::is_eq(libcc2rs::PartialOrdering::from( + ({ (*libcc2rs::strong_ordering_equal().upgrade().deref()) }) + ))); + assert!(libcc2rs::is_gt(libcc2rs::PartialOrdering::from( + ({ (*libcc2rs::weak_ordering_greater().upgrade().deref()) }) + ))); + assert!(libcc2rs::is_lteq(libcc2rs::PartialOrdering::from( + libcc2rs::StrongOrdering::from(std::cmp::Ord::cmp(&(1), &(2))) + ))); +} +pub fn main() { + __cpp2rust_init_globals(); + libcc2rs::cpp2rust_exit(main_0()); +} +fn main_0() -> i32 { + ({ test_0() }); + ({ test_1() }); + return 0; +} +pub fn __cpp2rust_init_globals() {} diff --git a/tests/std/support/cmp/out/unsafe/cmp.concept.rs b/tests/std/support/cmp/out/unsafe/cmp.concept.rs new file mode 100644 index 0000000000..a199076f39 --- /dev/null +++ b/tests/std/support/cmp/out/unsafe/cmp.concept.rs @@ -0,0 +1,291 @@ +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, + TriviallyDefaultConstructibleUnsafe, + TriviallyCopyableUnsafe, + Default, + PartiallyOrderedWithUnsafe, +)] +#[three_way(ThreeWayComparableUnsafe::three_way)] +pub struct p1_defaulted_three_way_Version { + pub major: i32, + pub minor: i32, +} +impl ThreeWayComparableUnsafe for p1_defaulted_three_way_Version { + type Category = libcc2rs::StrongOrdering; + unsafe fn three_way( + this: *const Self, + _a0: *const p1_defaulted_three_way_Version, + ) -> libcc2rs::StrongOrdering { + { + let mut cmp: libcc2rs::StrongOrdering = libcc2rs::StrongOrdering::from( + std::cmp::Ord::cmp(&((*this).major), &((*_a0).major)), + ); + if !(libcc2rs::is_eq(cmp)) { + return cmp; + } + } + { + let mut cmp: libcc2rs::StrongOrdering = libcc2rs::StrongOrdering::from( + std::cmp::Ord::cmp(&((*this).minor), &((*_a0).minor)), + ); + if !(libcc2rs::is_eq(cmp)) { + return cmp; + } + } + return libcc2rs::STRONG_ORDERING_EQUAL; + } +} +impl LessThanComparableUnsafe for p1_defaulted_three_way_Version { + unsafe fn less(this: *const Self, _a0: *const Self) -> bool { + libcc2rs::is_lt(ThreeWayComparableUnsafe::three_way(this, _a0)) + } +} +const _: () = assert!(true, "std::three_way_comparable"); +const _: () = assert!( + true, + "std::three_way_comparable" +); +pub unsafe fn test_0() { + let mut old_version: p1_defaulted_three_way_Version = + p1_defaulted_three_way_Version { major: 1, minor: 9 }; + let mut new_version: p1_defaulted_three_way_Version = + p1_defaulted_three_way_Version { major: 2, minor: 0 }; + let mut order: libcc2rs::StrongOrdering = + (unsafe { ThreeWayComparableUnsafe::three_way(&raw const old_version, &new_version) }); + assert!(libcc2rs::is_lt(order)); + assert!(libcc2rs::is_gt( + (unsafe { ThreeWayComparableUnsafe::three_way(&raw const new_version, &old_version,) }) + )); + assert!(libcc2rs::is_eq( + (unsafe { + let _arg0: *const p1_defaulted_three_way_Version = &old_version; + ThreeWayComparableUnsafe::three_way(&raw const old_version, _arg0) + }) + )); + assert!( + (unsafe { + let _arg0: *const p1_defaulted_three_way_Version = &old_version; + WeaklyEqualityComparableWithUnsafe::equal(&raw const old_version, _arg0) + }) + ); + assert!(libcc2rs::is_lt( + (unsafe { ThreeWayComparableUnsafe::three_way(&raw const old_version, &new_version,) }) + )); + assert!(libcc2rs::is_gteq( + (unsafe { ThreeWayComparableUnsafe::three_way(&raw const new_version, &old_version,) }) + )); +} +pub static mut three_way_calls_1: std::cell::LazyCell = + std::cell::LazyCell::new(|| unsafe { 0 }); +#[repr(C)] +#[derive( + Copy, + Clone, + VaArg, + FnPtrArg, + ZeroInitializationUnsafe, + TriviallyDefaultConstructibleUnsafe, + TriviallyCopyableUnsafe, + Default, + PartiallyOrderedWithUnsafe, +)] +#[three_way(ThreeWayComparableUnsafe::three_way)] +pub struct p1_user_written_three_way_Money { + pub cents: i32, +} +impl ThreeWayComparableUnsafe for p1_user_written_three_way_Money { + type Category = libcc2rs::StrongOrdering; + unsafe fn three_way( + this: *const Self, + other: *const p1_user_written_three_way_Money, + ) -> libcc2rs::StrongOrdering { + (*std::cell::LazyCell::force_mut(&mut *&raw mut three_way_calls_1)).prefix_inc(); + return libcc2rs::StrongOrdering::from(std::cmp::Ord::cmp( + &((*this).cents), + &((*other).cents), + )); + } +} +impl LessThanComparableUnsafe for p1_user_written_three_way_Money { + unsafe fn less(this: *const Self, other: *const Self) -> bool { + libcc2rs::is_lt(ThreeWayComparableUnsafe::three_way(this, other)) + } +} +const _: () = assert!(true, "std::three_way_comparable"); +pub unsafe fn test_2() { + let mut small: p1_user_written_three_way_Money = p1_user_written_three_way_Money { cents: 150 }; + let mut large: p1_user_written_three_way_Money = p1_user_written_three_way_Money { cents: 900 }; + assert!(libcc2rs::is_lt( + (unsafe { ThreeWayComparableUnsafe::three_way(&raw const small, &large,) }) + )); + assert!(libcc2rs::is_lt( + (unsafe { ThreeWayComparableUnsafe::three_way(&raw const small, &large,) }) + )); + assert!(libcc2rs::is_gt( + (unsafe { ThreeWayComparableUnsafe::three_way(&raw const large, &small,) }) + )); + assert!(libcc2rs::is_lteq( + (unsafe { + let _other: *const p1_user_written_three_way_Money = &small; + ThreeWayComparableUnsafe::three_way(&raw const small, _other) + }) + )); + assert!(libcc2rs::is_gteq( + (unsafe { ThreeWayComparableUnsafe::three_way(&raw const large, &small,) }) + )); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut three_way_calls_1)) == (5))); + assert!( + (unsafe { + let _other: *const p1_user_written_three_way_Money = &small; + WeaklyEqualityComparableWithUnsafe::equal(&raw const small, _other) + }) + ); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut three_way_calls_1)) == (5))); +} +#[repr(C)] +#[derive( + Copy, + Clone, + VaArg, + FnPtrArg, + ZeroInitializationUnsafe, + TriviallyDefaultConstructibleUnsafe, + TriviallyCopyableUnsafe, + Default, + PartiallyOrderedWithUnsafe, +)] +#[three_way(ThreeWayComparableUnsafe::three_way)] +pub struct p1_weaker_category_Temperature { + pub degrees: f64, +} +impl ThreeWayComparableUnsafe for p1_weaker_category_Temperature { + type Category = libcc2rs::PartialOrdering; + unsafe fn three_way( + this: *const Self, + other: *const p1_weaker_category_Temperature, + ) -> libcc2rs::PartialOrdering { + return libcc2rs::PartialOrdering::from(std::cmp::PartialOrd::partial_cmp( + &((*this).degrees), + &((*other).degrees), + )); + } +} +impl LessThanComparableUnsafe for p1_weaker_category_Temperature { + unsafe fn less(this: *const Self, other: *const Self) -> bool { + libcc2rs::is_lt(ThreeWayComparableUnsafe::three_way(this, other)) + } +} +const _: () = assert!(true, "std::three_way_comparable"); +const _: () = assert!( + !(false), + "!std::three_way_comparable" +); +pub unsafe fn test_3() { + let mut cold: p1_weaker_category_Temperature = + p1_weaker_category_Temperature { degrees: -5_f64 }; + let mut warm: p1_weaker_category_Temperature = + p1_weaker_category_Temperature { degrees: 21.5_f64 }; + let mut order: libcc2rs::PartialOrdering = + (unsafe { ThreeWayComparableUnsafe::three_way(&raw const cold, &warm) }); + assert!(libcc2rs::is_lt(order)); + assert!(libcc2rs::is_lt( + (unsafe { ThreeWayComparableUnsafe::three_way(&raw const cold, &warm,) }) + )); + assert!(libcc2rs::is_gteq( + (unsafe { ThreeWayComparableUnsafe::three_way(&raw const warm, &cold,) }) + )); +} +#[repr(C)] +#[derive( + Copy, + Clone, + VaArg, + FnPtrArg, + ZeroInitializationUnsafe, + TriviallyDefaultConstructibleUnsafe, + TriviallyCopyableUnsafe, + Default, + PartiallyOrderedWithUnsafe, +)] +#[three_way(p1_three_way_without_equality_Rank::operator_cmp)] +pub struct p1_three_way_without_equality_Rank { + pub value: i32, +} +impl p1_three_way_without_equality_Rank { + pub unsafe fn operator_cmp( + this: *const Self, + other: *const p1_three_way_without_equality_Rank, + ) -> libcc2rs::StrongOrdering { + return libcc2rs::StrongOrdering::from(std::cmp::Ord::cmp( + &((*this).value), + &((*other).value), + )); + } +} +impl LessThanComparableUnsafe for p1_three_way_without_equality_Rank { + unsafe fn less(this: *const Self, other: *const Self) -> bool { + libcc2rs::is_lt(p1_three_way_without_equality_Rank::operator_cmp( + this, other, + )) + } +} +const _: () = assert!(!(false), "!std::three_way_comparable"); +pub unsafe fn test_4() { + let mut low: p1_three_way_without_equality_Rank = + p1_three_way_without_equality_Rank { value: 1 }; + let mut high: p1_three_way_without_equality_Rank = + p1_three_way_without_equality_Rank { value: 2 }; + assert!(libcc2rs::is_lt( + (unsafe { p1_three_way_without_equality_Rank::operator_cmp(&raw const low, &high,) }) + )); + assert!(libcc2rs::is_lt( + (unsafe { p1_three_way_without_equality_Rank::operator_cmp(&raw const low, &high,) }) + )); + assert!(libcc2rs::is_gteq( + (unsafe { p1_three_way_without_equality_Rank::operator_cmp(&raw const high, &low,) }) + )); +} +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + libcc2rs::cpp2rust_exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + (unsafe { test_0() }); + (unsafe { test_2() }); + (unsafe { test_3() }); + (unsafe { test_4() }); + return 0; +} +impl WeaklyEqualityComparableWithUnsafe for p1_defaulted_three_way_Version { + unsafe fn equal(this: *const Self, _a0: *const p1_defaulted_three_way_Version) -> bool { + return (((*this).major) == ((*_a0).major)) && (((*this).minor) == ((*_a0).minor)); + } +} +impl WeaklyEqualityComparableWithUnsafe for p1_user_written_three_way_Money { + unsafe fn equal(this: *const Self, other: *const p1_user_written_three_way_Money) -> bool { + return (((*this).cents) == ((*other).cents)); + } +} +impl WeaklyEqualityComparableWithUnsafe for p1_weaker_category_Temperature { + unsafe fn equal(this: *const Self, other: *const p1_weaker_category_Temperature) -> bool { + return (((*this).degrees) == ((*other).degrees)); + } +} +pub unsafe fn __cpp2rust_init_globals() { + std::cell::LazyCell::force(&*&raw const three_way_calls_1); +} diff --git a/tests/std/support/cmp/out/unsafe/cmp.strongord.rs b/tests/std/support/cmp/out/unsafe/cmp.strongord.rs index ef98961755..db9079905e 100644 --- a/tests/std/support/cmp/out/unsafe/cmp.strongord.rs +++ b/tests/std/support/cmp/out/unsafe/cmp.strongord.rs @@ -7,15 +7,21 @@ use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; pub unsafe fn test_0() { - let l: *const std::cmp::Ordering = &libcc2rs::STRONG_ORDERING_LESS; - let e: *const std::cmp::Ordering = &libcc2rs::STRONG_ORDERING_EQUAL; - let g: *const std::cmp::Ordering = &libcc2rs::STRONG_ORDERING_GREATER; - assert!(((l) == (&raw const libcc2rs::STRONG_ORDERING_LESS as *const std::cmp::Ordering))); - assert!(((e) == (&raw const libcc2rs::STRONG_ORDERING_EQUAL as *const std::cmp::Ordering))); - assert!(((g) == (&raw const libcc2rs::STRONG_ORDERING_GREATER as *const std::cmp::Ordering))); - assert!((*l) == std::cmp::Ordering::Less); - assert!((*e) == std::cmp::Ordering::Equal); - assert!((*g) == std::cmp::Ordering::Greater); + let l: *const libcc2rs::StrongOrdering = &libcc2rs::STRONG_ORDERING_LESS; + let e: *const libcc2rs::StrongOrdering = &libcc2rs::STRONG_ORDERING_EQUAL; + let g: *const libcc2rs::StrongOrdering = &libcc2rs::STRONG_ORDERING_GREATER; + assert!( + ((l) == (&raw const libcc2rs::STRONG_ORDERING_LESS as *const libcc2rs::StrongOrdering)) + ); + assert!( + ((e) == (&raw const libcc2rs::STRONG_ORDERING_EQUAL as *const libcc2rs::StrongOrdering)) + ); + assert!( + ((g) == (&raw const libcc2rs::STRONG_ORDERING_GREATER as *const libcc2rs::StrongOrdering)) + ); + assert!(libcc2rs::is_lt((*l))); + assert!(libcc2rs::is_eq((*e))); + assert!(libcc2rs::is_gt((*g))); } pub fn main() { unsafe { diff --git a/tests/std/support/cmp/out/unsafe/compare.syn.rs b/tests/std/support/cmp/out/unsafe/compare.syn.rs new file mode 100644 index 0000000000..afd4e280d2 --- /dev/null +++ b/tests/std/support/cmp/out/unsafe/compare.syn.rs @@ -0,0 +1,66 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +pub unsafe fn test_0() { + let mut less: libcc2rs::PartialOrdering = libcc2rs::PARTIAL_ORDERING_LESS; + let mut equivalent: libcc2rs::PartialOrdering = libcc2rs::PARTIAL_ORDERING_EQUIVALENT; + let mut greater: libcc2rs::PartialOrdering = libcc2rs::PARTIAL_ORDERING_GREATER; + let mut unordered: libcc2rs::PartialOrdering = libcc2rs::PARTIAL_ORDERING_UNORDERED; + assert!( + ((libcc2rs::is_eq(equivalent)) && (!(libcc2rs::is_eq(less)))) + && (!(libcc2rs::is_eq(unordered))) + ); + assert!( + ((libcc2rs::is_neq(less)) && (libcc2rs::is_neq(unordered))) + && (!(libcc2rs::is_neq(equivalent))) + ); + assert!( + ((libcc2rs::is_lt(less)) && (!(libcc2rs::is_lt(equivalent)))) + && (!(libcc2rs::is_lt(unordered))) + ); + assert!( + ((libcc2rs::is_lteq(less)) && (libcc2rs::is_lteq(equivalent))) + && (!(libcc2rs::is_lteq(greater))) + ); + assert!(!(libcc2rs::is_lteq(unordered))); + assert!( + ((libcc2rs::is_gt(greater)) && (!(libcc2rs::is_gt(equivalent)))) + && (!(libcc2rs::is_gt(unordered))) + ); + assert!( + ((libcc2rs::is_gteq(greater)) && (libcc2rs::is_gteq(equivalent))) + && (!(libcc2rs::is_gteq(less))) + ); + assert!(!(libcc2rs::is_gteq(unordered))); +} +pub unsafe fn test_1() { + assert!(libcc2rs::is_lt(libcc2rs::PartialOrdering::from( + libcc2rs::STRONG_ORDERING_LESS + ))); + assert!(libcc2rs::is_eq(libcc2rs::PartialOrdering::from( + libcc2rs::STRONG_ORDERING_EQUAL + ))); + assert!(libcc2rs::is_gt(libcc2rs::PartialOrdering::from( + libcc2rs::WEAK_ORDERING_GREATER + ))); + assert!(libcc2rs::is_lteq(libcc2rs::PartialOrdering::from( + libcc2rs::StrongOrdering::from(std::cmp::Ord::cmp(&(1), &(2))) + ))); +} +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/utilities/pairs/out/refcount/pairs.pair.rs b/tests/std/utilities/pairs/out/refcount/pairs.pair.rs index ed07a6ef03..c2345ac1f5 100644 --- a/tests/std/utilities/pairs/out/refcount/pairs.pair.rs +++ b/tests/std/utilities/pairs/out/refcount/pairs.pair.rs @@ -147,16 +147,17 @@ pub fn test_4() { ))) }) ); - assert!((*a.borrow()) < (*b.borrow())); - assert!(({ !((*b.borrow()) < (*a.borrow())) })); - assert!((*b.borrow()) < (*c.borrow())); - assert!((*a.borrow()) <= (*a.borrow())); - assert!(({ !((*c.borrow()) <= (*b.borrow())) })); - assert!((*c.borrow()) > (*b.borrow())); - assert!(({ !((*a.borrow()) > (*a.borrow())) })); - assert!((*c.borrow()) >= (*a.borrow())); - assert!(({ !((*a.borrow()) >= (*b.borrow())) })); - let mut ordered: bool = ((*a.borrow()) < (*b.borrow())) && ((*b.borrow()) < (*c.borrow())); + assert!(LessThanComparable::less(a.as_pointer(), b.as_pointer())); + assert!(!(LessThanComparable::less(b.as_pointer(), a.as_pointer()))); + assert!(LessThanComparable::less(b.as_pointer(), c.as_pointer())); + assert!(!LessThanComparable::less(a.as_pointer(), a.as_pointer())); + assert!(!(!LessThanComparable::less(b.as_pointer(), c.as_pointer()))); + assert!(LessThanComparable::less(b.as_pointer(), c.as_pointer())); + assert!(!(LessThanComparable::less(a.as_pointer(), a.as_pointer()))); + assert!(!LessThanComparable::less(c.as_pointer(), a.as_pointer())); + assert!(!(!LessThanComparable::less(a.as_pointer(), b.as_pointer()))); + let mut ordered: bool = (LessThanComparable::less(a.as_pointer(), b.as_pointer())) + && (LessThanComparable::less(b.as_pointer(), c.as_pointer())); assert!(ordered); } pub fn main() { diff --git a/tests/std/utilities/pairs/out/unsafe/pairs.pair.rs b/tests/std/utilities/pairs/out/unsafe/pairs.pair.rs index c109916ae7..5a5eb41f5c 100644 --- a/tests/std/utilities/pairs/out/unsafe/pairs.pair.rs +++ b/tests/std/utilities/pairs/out/unsafe/pairs.pair.rs @@ -147,16 +147,17 @@ pub unsafe fn test_4() { !(!(WeaklyEqualityComparableWithUnsafe::equal(&a.0, &a.0) && WeaklyEqualityComparableWithUnsafe::equal(&a.1, &a.1))) ); - assert!(a < b); - assert!(!(b < a)); - assert!(b < c); - assert!(a <= a); - assert!(!(c <= b)); - assert!(c > b); - assert!(!(a > a)); - assert!(c >= a); - assert!(!(a >= b)); - let mut ordered: bool = (a < b) && (b < c); + assert!(LessThanComparableUnsafe::less(&a, &b)); + assert!(!(LessThanComparableUnsafe::less(&b, &a))); + assert!(LessThanComparableUnsafe::less(&b, &c)); + assert!(!LessThanComparableUnsafe::less(&a, &a)); + assert!(!(!LessThanComparableUnsafe::less(&b, &c))); + assert!(LessThanComparableUnsafe::less(&b, &c)); + assert!(!(LessThanComparableUnsafe::less(&a, &a))); + assert!(!LessThanComparableUnsafe::less(&c, &a)); + assert!(!(!LessThanComparableUnsafe::less(&a, &b))); + let mut ordered: bool = + (LessThanComparableUnsafe::less(&a, &b)) && (LessThanComparableUnsafe::less(&b, &c)); assert!(ordered); } pub fn main() { diff --git a/tests/ub/out/refcount/max.rs b/tests/ub/out/refcount/max.rs index 3cc11db765..b6361498e4 100644 --- a/tests/ub/out/refcount/max.rs +++ b/tests/ub/out/refcount/max.rs @@ -14,7 +14,7 @@ fn main_0() -> i32 { let a: Ptr = ({ let __tmp_0: Value = Rc::new(RefCell::new(2)); let __tmp_1: Value = Rc::new(RefCell::new(1)); - if CppLess::less(__tmp_0.as_pointer().clone(), __tmp_1.as_pointer().clone()) { + if LessThanComparable::less(__tmp_0.as_pointer().clone(), __tmp_1.as_pointer().clone()) { __tmp_0.as_pointer() } else { __tmp_1.as_pointer() @@ -23,7 +23,7 @@ fn main_0() -> i32 { let b: Ptr = ({ let __tmp_2: Value = Rc::new(RefCell::new(1)); let __tmp_3: Value = Rc::new(RefCell::new(2)); - if CppLess::less(__tmp_2.as_pointer().clone(), __tmp_3.as_pointer().clone()) { + if LessThanComparable::less(__tmp_2.as_pointer().clone(), __tmp_3.as_pointer().clone()) { __tmp_3.as_pointer() } else { __tmp_2.as_pointer() diff --git a/tests/ub/out/unsafe/max.rs b/tests/ub/out/unsafe/max.rs index 5c4ec9cc5a..8461548df3 100644 --- a/tests/ub/out/unsafe/max.rs +++ b/tests/ub/out/unsafe/max.rs @@ -19,7 +19,11 @@ unsafe fn main_0() -> i32 { { let __a: *const _ = &mut __tmp_0; let __b: *const _ = &mut __tmp_1; - if *__b < *__a { __b } else { __a } + if LessThanComparableUnsafe::less(__b, __a) { + __b + } else { + __a + } } }; let b: *const i32 = { @@ -28,7 +32,11 @@ unsafe fn main_0() -> i32 { { let __a: *const _ = &mut __tmp_2; let __b: *const _ = &mut __tmp_3; - if *__a < *__b { __b } else { __a } + if LessThanComparableUnsafe::less(__a, __b) { + __b + } else { + __a + } } }; return (((*a) == (*b)) as i32); diff --git a/tests/unit/out/refcount/fn_ptr.rs b/tests/unit/out/refcount/fn_ptr.rs index 8a188a60a8..a9a103042f 100644 --- a/tests/unit/out/refcount/fn_ptr.rs +++ b/tests/unit/out/refcount/fn_ptr.rs @@ -68,9 +68,9 @@ pub fn string_size_10(mut s: Vec) -> usize { pub fn vector_sum_11(mut v: Vec) -> i32 { return (v[0_usize] + v[1_usize]); } -pub fn is_less_12(o: std::cmp::Ordering) -> bool { - let o: Value = Rc::new(RefCell::new(o)); - return ({ (*o.borrow()) }) == std::cmp::Ordering::Less; +pub fn is_less_12(o: libcc2rs::StrongOrdering) -> bool { + let o: Value = Rc::new(RefCell::new(o)); + return libcc2rs::is_lt(({ (*o.borrow()) })); } pub fn main() { __cpp2rust_init_globals(); @@ -122,16 +122,37 @@ fn main_0() -> i32 { ); let mut vf: FnPtr) -> i32> = FnPtr::) -> i32>::new(vector_sum_11); assert!((({ vf.call(Vec::from([1, 2,]),) }) == 3)); - let mut of: FnPtr bool> = - FnPtr:: bool>::new(is_less_12); - assert!(({ of.call(std::cmp::Ord::cmp(&(1), &(2)),) })); - assert!(!({ of.call(std::cmp::Ord::cmp(&(2), &(1)),) })); - let o: Value = Rc::new(RefCell::new(std::cmp::Ord::cmp(&(1), &(2)))); - let mut op: Ptr = (o.as_pointer()); + let mut of: FnPtr bool> = + FnPtr:: bool>::new(is_less_12); assert!( - ({ (*op.upgrade().deref()) }) - == ({ (*libcc2rs::strong_ordering_less().upgrade().deref()) }) + ({ + of.call(libcc2rs::StrongOrdering::from(std::cmp::Ord::cmp( + &(1), + &(2), + ))) + }) ); + assert!( + !({ + of.call(libcc2rs::StrongOrdering::from(std::cmp::Ord::cmp( + &(2), + &(1), + ))) + }) + ); + let o: Value = Rc::new(RefCell::new(libcc2rs::StrongOrdering::from( + std::cmp::Ord::cmp(&(1), &(2)), + ))); + let mut op: Ptr = (o.as_pointer()); + assert!({ + let __a = libcc2rs::PartialOrdering::from(({ (*op.upgrade().deref()) })); + let __b = libcc2rs::PartialOrdering::from( + ({ (*libcc2rs::strong_ordering_less().upgrade().deref()) }), + ); + libcc2rs::is_lt(__a) == libcc2rs::is_lt(__b) + && libcc2rs::is_eq(__a) == libcc2rs::is_eq(__b) + && libcc2rs::is_gt(__a) == libcc2rs::is_gt(__b) + }); return 0; } pub fn __cpp2rust_init_globals() { diff --git a/tests/unit/out/refcount/global_call_temporaries.rs b/tests/unit/out/refcount/global_call_temporaries.rs index 3ffa8ce93b..34692c03ce 100644 --- a/tests/unit/out/refcount/global_call_temporaries.rs +++ b/tests/unit/out/refcount/global_call_temporaries.rs @@ -18,7 +18,10 @@ thread_local!( ({ __tmp_0 = Rc::new(RefCell::new(0)); __tmp_1 = Rc::new(RefCell::new(0)); - if CppLess::less(__tmp_0.as_pointer().clone(), __tmp_1.as_pointer().clone()) { + if LessThanComparable::less( + __tmp_0.as_pointer().clone(), + __tmp_1.as_pointer().clone(), + ) { __tmp_0.as_pointer() } else { __tmp_1.as_pointer() @@ -43,7 +46,10 @@ thread_local!( let __rhs = { __tmp_2 = Rc::new(RefCell::new(6)); __tmp_3 = Rc::new(RefCell::new(5)); - (if CppLess::less(__tmp_2.as_pointer().clone(), __tmp_3.as_pointer().clone()) { + (if LessThanComparable::less( + __tmp_2.as_pointer().clone(), + __tmp_3.as_pointer().clone(), + ) { __tmp_2.as_pointer() } else { __tmp_3.as_pointer() @@ -65,7 +71,10 @@ thread_local!( let __rhs = { __tmp_4 = Rc::new(RefCell::new(1)); __tmp_5 = Rc::new(RefCell::new(2)); - (if CppLess::less(__tmp_4.as_pointer().clone(), __tmp_5.as_pointer().clone()) { + (if LessThanComparable::less( + __tmp_4.as_pointer().clone(), + __tmp_5.as_pointer().clone(), + ) { __tmp_5.as_pointer() } else { __tmp_4.as_pointer() @@ -129,7 +138,10 @@ thread_local!( __tmp_7 = Rc::new(RefCell::new(Box::new([{ __tmp_8 = Rc::new(RefCell::new(4)); __tmp_9 = Rc::new(RefCell::new(3)); - (if CppLess::less(__tmp_8.as_pointer().clone(), __tmp_9.as_pointer().clone()) { + (if LessThanComparable::less( + __tmp_8.as_pointer().clone(), + __tmp_9.as_pointer().clone(), + ) { __tmp_8.as_pointer() } else { __tmp_9.as_pointer() @@ -142,8 +154,10 @@ thread_local!( value: { __tmp_11 = Rc::new(RefCell::new(5)); __tmp_12 = Rc::new(RefCell::new(6)); - (if CppLess::less(__tmp_11.as_pointer().clone(), __tmp_12.as_pointer().clone()) - { + (if LessThanComparable::less( + __tmp_11.as_pointer().clone(), + __tmp_12.as_pointer().clone(), + ) { __tmp_12.as_pointer() } else { __tmp_11.as_pointer() @@ -167,7 +181,7 @@ thread_local!( Rc::new(RefCell::new( (({ __tmp_13 = Rc::new(RefCell::new((right_11.with(|rc| *rc.borrow()) as u64))); - (if CppLess::less( + (if LessThanComparable::less( __tmp_13.as_pointer().clone(), left_10.with(|v| v.as_pointer()).clone(), ) { @@ -189,7 +203,7 @@ thread_local!( Rc::new(RefCell::new( (({ __tmp_14 = Rc::new(RefCell::new((right_11.with(|rc| *rc.borrow()) as u64))); - (if CppLess::less( + (if LessThanComparable::less( left_10.with(|v| v.as_pointer()).clone(), __tmp_14.as_pointer().clone(), ) { @@ -229,7 +243,10 @@ thread_local!( __tmp_15 = Rc::new(RefCell::new(Box::new([{ __tmp_16 = Rc::new(RefCell::new(7)); __tmp_17 = Rc::new(RefCell::new(8)); - (if CppLess::less(__tmp_16.as_pointer().clone(), __tmp_17.as_pointer().clone()) { + (if LessThanComparable::less( + __tmp_16.as_pointer().clone(), + __tmp_17.as_pointer().clone(), + ) { __tmp_17.as_pointer() } else { __tmp_16.as_pointer() @@ -242,8 +259,10 @@ thread_local!( value: { __tmp_19 = Rc::new(RefCell::new(10)); __tmp_20 = Rc::new(RefCell::new(9)); - (if CppLess::less(__tmp_19.as_pointer().clone(), __tmp_20.as_pointer().clone()) - { + (if LessThanComparable::less( + __tmp_19.as_pointer().clone(), + __tmp_20.as_pointer().clone(), + ) { __tmp_19.as_pointer() } else { __tmp_20.as_pointer() diff --git a/tests/unit/out/refcount/map_key_copy_ctor.rs b/tests/unit/out/refcount/map_key_copy_ctor.rs index 6b30fb39c0..1086236bbc 100644 --- a/tests/unit/out/refcount/map_key_copy_ctor.rs +++ b/tests/unit/out/refcount/map_key_copy_ctor.rs @@ -28,9 +28,6 @@ impl Key { pub fn construct(this: Ptr, mut k: i32) { field!(this, k).write(k); } - pub fn operator_lt(this: Ptr, o: Ptr) -> bool { - return ({ this.with(|__s| __s.k) } < { o.with(|__s| __s.k) }); - } } impl CopyConstructible for Key { fn copy_constructor(this: Ptr, o: Ptr) { @@ -44,9 +41,9 @@ impl MoveConstructible for Key { (*moves_1.with(Value::clone).borrow_mut()).prefix_inc(); } } -impl CppLess for Key { - fn less(this: Ptr, o: Ptr) -> bool { - Key::operator_lt(this.clone(), o.clone()) +impl LessThanComparable for Key { + fn less(this: Ptr, o: Ptr) -> bool { + return ({ this.with(|__s| __s.k) } < { o.with(|__s| __s.k) }); } } pub fn make_key_3(__return: Ptr, mut k: i32) { diff --git a/tests/unit/out/refcount/map_key_identity.rs b/tests/unit/out/refcount/map_key_identity.rs index d502571423..cee6bfa7de 100644 --- a/tests/unit/out/refcount/map_key_identity.rs +++ b/tests/unit/out/refcount/map_key_identity.rs @@ -28,14 +28,11 @@ pub struct Key { #[offset(4)] pub identity: i32, } -impl CppLess for Key { - fn less(a: Ptr, b: Ptr) -> bool { - operator_lt_1(a.clone(), b.clone()) +impl LessThanComparable for Key { + fn less(a: Ptr, b: Ptr) -> bool { + return ({ a.with(|__s| __s.order) } < { b.with(|__s| __s.order) }); } } -pub fn operator_lt_1(a: Ptr, b: Ptr) -> bool { - return ({ a.with(|__s| __s.order) } < { b.with(|__s| __s.order) }); -} #[derive(Copy, Clone, Record, ByteRepr, ZeroInitialization, VaArg, FnPtrArg, TriviallyCopyable)] #[byte_size(4)] pub struct Mapped { diff --git a/tests/unit/out/refcount/max.rs b/tests/unit/out/refcount/max.rs index db0277c11c..301534b219 100644 --- a/tests/unit/out/refcount/max.rs +++ b/tests/unit/out/refcount/max.rs @@ -51,15 +51,12 @@ impl CopyConstructible for Bag { (*calls_0.with(Value::clone).borrow_mut()).prefix_inc(); } } -impl CppLess for Bag { - fn less(a: Ptr, b: Ptr) -> bool { - operator_lt_4(a.clone(), b.clone()) +impl LessThanComparable for Bag { + fn less(a: Ptr, b: Ptr) -> bool { + return ({ a.with(|__s| __s.n) } < { b.with(|__s| __s.n) }); } } -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(__return: Ptr, mut n: i32) { +pub fn make_4(__return: Ptr, mut n: i32) { Bag::construct(__return.clone(), n); return; } @@ -74,34 +71,38 @@ fn main_0() -> i32 { let x4: Value = Rc::new(RefCell::new(20)); let mut p1: Ptr = (x1.as_pointer()); let mut p2: Ptr = (x2.as_pointer()); - let mut r1: i32 = (if CppLess::less(x1.as_pointer().clone(), x2.as_pointer().clone()) { - x2.as_pointer() - } else { - x1.as_pointer() - } - .read()); - let mut r2: i32 = (if CppLess::less(x4.as_pointer().clone(), x3.as_pointer().clone()) { - x4.as_pointer() - } else { - x3.as_pointer() - } - .read()); - let mut r3: i32 = (if CppLess::less((p1).clone().clone(), x2.as_pointer().clone()) { - x2.as_pointer() - } else { - (p1).clone() - } - .read()); - let mut r4: i32 = (if CppLess::less(x3.as_pointer().clone(), (p2).clone().clone()) { - x3.as_pointer() - } else { - (p2).clone() - } - .read()); + let mut r1: i32 = + (if LessThanComparable::less(x1.as_pointer().clone(), x2.as_pointer().clone()) { + x2.as_pointer() + } else { + x1.as_pointer() + } + .read()); + let mut r2: i32 = + (if LessThanComparable::less(x4.as_pointer().clone(), x3.as_pointer().clone()) { + x4.as_pointer() + } else { + x3.as_pointer() + } + .read()); + let mut r3: i32 = + (if LessThanComparable::less((p1).clone().clone(), x2.as_pointer().clone()) { + x2.as_pointer() + } else { + (p1).clone() + } + .read()); + let mut r4: i32 = + (if LessThanComparable::less(x3.as_pointer().clone(), (p2).clone().clone()) { + x3.as_pointer() + } else { + (p2).clone() + } + .read()); let mut r5: i32 = { let __tmp_0: Value = Rc::new(RefCell::new(30)); let __tmp_1: Value = Rc::new(RefCell::new(40)); - (if CppLess::less(__tmp_0.as_pointer().clone(), __tmp_1.as_pointer().clone()) { + (if LessThanComparable::less(__tmp_0.as_pointer().clone(), __tmp_1.as_pointer().clone()) { __tmp_1.as_pointer() } else { __tmp_0.as_pointer() @@ -117,7 +118,7 @@ fn main_0() -> i32 { if >::not_equal(__it.as_pointer(), __last.as_pointer()) { __it.borrow_mut().iter_inc(); while >::not_equal(__it.as_pointer(), __last.as_pointer()) { - if CppLess::less(__best.iter_deref(), __it.borrow().iter_deref()) { + if LessThanComparable::less(__best.iter_deref(), __it.borrow().iter_deref()) { __best = __it.borrow().clone(); } __it.borrow_mut().iter_inc(); @@ -129,7 +130,7 @@ fn main_0() -> i32 { assert!( ((match (({ pick_3(0,) }), ({ pick_3(1,) }),) { (__tmp_2, __tmp_3) => { - if CppLess::less((__tmp_2).clone().clone(), (__tmp_3).clone().clone()) { + if LessThanComparable::less((__tmp_2).clone().clone(), (__tmp_3).clone().clone()) { (__tmp_3).clone() } else { (__tmp_2).clone() @@ -143,7 +144,7 @@ fn main_0() -> i32 { assert!( ((match (({ pick_3(1,) }), ({ pick_3(0,) }),) { (__tmp_4, __tmp_5) => { - if CppLess::less((__tmp_5).clone().clone(), (__tmp_4).clone().clone()) { + if LessThanComparable::less((__tmp_5).clone().clone(), (__tmp_4).clone().clone()) { (__tmp_5).clone() } else { (__tmp_4).clone() @@ -161,16 +162,19 @@ fn main_0() -> i32 { let __tmp_9: Value = Rc::new(RefCell::new(::zero_init())); (match ( { - ({ make_5(Ptr::clone(&__tmp_7.as_pointer()), 1) }); + ({ make_4(Ptr::clone(&__tmp_7.as_pointer()), 1) }); __tmp_7.as_pointer() }, { - ({ make_5(Ptr::clone(&__tmp_9.as_pointer()), 2) }); + ({ make_4(Ptr::clone(&__tmp_9.as_pointer()), 2) }); __tmp_9.as_pointer() }, ) { (__tmp_6, __tmp_8) => { - if CppLess::less((__tmp_6).clone().clone(), (__tmp_8).clone().clone()) { + if LessThanComparable::less( + (__tmp_6).clone().clone(), + (__tmp_8).clone().clone(), + ) { (__tmp_8).clone() } else { (__tmp_6).clone() @@ -184,16 +188,19 @@ fn main_0() -> i32 { let __tmp_13: Value = Rc::new(RefCell::new(::zero_init())); (match ( { - ({ make_5(Ptr::clone(&__tmp_11.as_pointer()), 1) }); + ({ make_4(Ptr::clone(&__tmp_11.as_pointer()), 1) }); __tmp_11.as_pointer() }, { - ({ make_5(Ptr::clone(&__tmp_13.as_pointer()), 2) }); + ({ make_4(Ptr::clone(&__tmp_13.as_pointer()), 2) }); __tmp_13.as_pointer() }, ) { (__tmp_10, __tmp_12) => { - if CppLess::less((__tmp_12).clone().clone(), (__tmp_10).clone().clone()) { + if LessThanComparable::less( + (__tmp_12).clone().clone(), + (__tmp_10).clone().clone(), + ) { (__tmp_12).clone() } else { (__tmp_10).clone() @@ -214,7 +221,7 @@ fn main_0() -> i32 { let __tmp_15: Value = Rc::new(RefCell::new((largest as u64))); (match (Rc::new(RefCell::new((({ size_of_2(i) }) as u64))).as_pointer(),) { (__tmp_14,) => { - if CppLess::less( + if LessThanComparable::less( __tmp_15.as_pointer().clone(), (__tmp_14).clone().clone(), ) { diff --git a/tests/unit/out/refcount/min_max_ptr2ptr.rs b/tests/unit/out/refcount/min_max_ptr2ptr.rs index 6c2a8acd49..016528fab0 100644 --- a/tests/unit/out/refcount/min_max_ptr2ptr.rs +++ b/tests/unit/out/refcount/min_max_ptr2ptr.rs @@ -17,13 +17,13 @@ fn main_0() -> i32 { let pb: Value> = Rc::new(RefCell::new((b.as_pointer()))); let mut ppa: Ptr> = (pa.as_pointer()); let mut ppb: Ptr> = (pb.as_pointer()); - let mut r1: i32 = (if CppLess::less((ppa.read()).clone(), (ppb.read()).clone()) { + let mut r1: i32 = (if LessThanComparable::less((ppa.read()).clone(), (ppb.read()).clone()) { (ppb.read()) } else { (ppa.read()) } .read()); - let mut r2: i32 = (if CppLess::less((ppb.read()).clone(), (ppa.read()).clone()) { + let mut r2: i32 = (if LessThanComparable::less((ppb.read()).clone(), (ppa.read()).clone()) { (ppb.read()) } else { (ppa.read()) @@ -36,7 +36,7 @@ fn main_0() -> i32 { assert!( ({ ({ - (if CppLess::less(high.as_pointer().clone(), low.as_pointer().clone()) { + (if LessThanComparable::less(high.as_pointer().clone(), low.as_pointer().clone()) { high.as_pointer() } else { low.as_pointer() @@ -48,7 +48,7 @@ fn main_0() -> i32 { assert!( ({ ({ - (if CppLess::less(low.as_pointer().clone(), high.as_pointer().clone()) { + (if LessThanComparable::less(low.as_pointer().clone(), high.as_pointer().clone()) { low.as_pointer() } else { high.as_pointer() @@ -60,7 +60,7 @@ fn main_0() -> i32 { assert!( ({ ({ - (if CppLess::less(low.as_pointer().clone(), high.as_pointer().clone()) { + (if LessThanComparable::less(low.as_pointer().clone(), high.as_pointer().clone()) { high.as_pointer() } else { low.as_pointer() @@ -72,7 +72,7 @@ fn main_0() -> i32 { assert!( ({ ({ - (if CppLess::less(high.as_pointer().clone(), low.as_pointer().clone()) { + (if LessThanComparable::less(high.as_pointer().clone(), low.as_pointer().clone()) { low.as_pointer() } else { high.as_pointer() diff --git a/tests/unit/out/refcount/operator_comparison_defaulted.rs b/tests/unit/out/refcount/operator_comparison_defaulted.rs index ecdf2fe32e..a511b9ee95 100644 --- a/tests/unit/out/refcount/operator_comparison_defaulted.rs +++ b/tests/unit/out/refcount/operator_comparison_defaulted.rs @@ -36,7 +36,9 @@ pub struct Eq { TriviallyDefaultConstructible, TriviallyCopyable, Default, + PartiallyOrderedWith, )] +#[three_way(ThreeWayComparable::three_way)] #[byte_size(8)] pub struct Cmp { #[offset(0)] @@ -44,32 +46,33 @@ pub struct Cmp { #[offset(4)] pub b: i32, } -impl Cmp { - pub fn operator_cmp(this: Ptr, _a0: Ptr) -> std::cmp::Ordering { +impl ThreeWayComparable for Cmp { + type Category = libcc2rs::StrongOrdering; + fn three_way(this: Ptr, _a0: Ptr) -> libcc2rs::StrongOrdering { { - let cmp: Value = Rc::new(RefCell::new(std::cmp::Ord::cmp( - &(this.with(|__s| __s.a)), - &(_a0.with(|__s| __s.a)), - ))); - if !(({ (*cmp.borrow()) }) == std::cmp::Ordering::Equal) { + let cmp: Value = + Rc::new(RefCell::new(libcc2rs::StrongOrdering::from( + std::cmp::Ord::cmp(&(this.with(|__s| __s.a)), &(_a0.with(|__s| __s.a))), + ))); + if !(libcc2rs::is_eq(({ (*cmp.borrow()) }))) { return (*cmp.borrow_mut()); } } { - let cmp: Value = Rc::new(RefCell::new(std::cmp::Ord::cmp( - &(this.with(|__s| __s.b)), - &(_a0.with(|__s| __s.b)), - ))); - if !(({ (*cmp.borrow()) }) == std::cmp::Ordering::Equal) { + let cmp: Value = + Rc::new(RefCell::new(libcc2rs::StrongOrdering::from( + std::cmp::Ord::cmp(&(this.with(|__s| __s.b)), &(_a0.with(|__s| __s.b))), + ))); + if !(libcc2rs::is_eq(({ (*cmp.borrow()) }))) { return (*cmp.borrow_mut()); } } return (*libcc2rs::strong_ordering_equal().upgrade().deref()); } } -impl CppLess for Cmp { +impl LessThanComparable for Cmp { fn less(this: Ptr, _a0: Ptr) -> bool { - Cmp::operator_cmp(this.clone(), _a0.clone()) == std::cmp::Ordering::Less + libcc2rs::is_lt(ThreeWayComparable::three_way(this.clone(), _a0.clone())) } } #[derive( @@ -83,29 +86,32 @@ impl CppLess for Cmp { TriviallyDefaultConstructible, TriviallyCopyable, Default, + PartiallyOrderedWith, )] +#[three_way(ThreeWayComparable::three_way)] #[byte_size(4)] pub struct Both { #[offset(0)] pub a: i32, } -impl Both { - pub fn operator_cmp(this: Ptr, _a0: Ptr) -> std::cmp::Ordering { +impl ThreeWayComparable for Both { + type Category = libcc2rs::StrongOrdering; + fn three_way(this: Ptr, _a0: Ptr) -> libcc2rs::StrongOrdering { { - let cmp: Value = Rc::new(RefCell::new(std::cmp::Ord::cmp( - &(this.with(|__s| __s.a)), - &(_a0.with(|__s| __s.a)), - ))); - if !(({ (*cmp.borrow()) }) == std::cmp::Ordering::Equal) { + let cmp: Value = + Rc::new(RefCell::new(libcc2rs::StrongOrdering::from( + std::cmp::Ord::cmp(&(this.with(|__s| __s.a)), &(_a0.with(|__s| __s.a))), + ))); + if !(libcc2rs::is_eq(({ (*cmp.borrow()) }))) { return (*cmp.borrow_mut()); } } return (*libcc2rs::strong_ordering_equal().upgrade().deref()); } } -impl CppLess for Both { +impl LessThanComparable for Both { fn less(this: Ptr, _a0: Ptr) -> bool { - Both::operator_cmp(this.clone(), _a0.clone()) == std::cmp::Ordering::Less + libcc2rs::is_lt(ThreeWayComparable::three_way(this.clone(), _a0.clone())) } } #[derive( @@ -119,29 +125,31 @@ impl CppLess for Both { TriviallyDefaultConstructible, TriviallyCopyable, Default, + PartiallyOrderedWith, )] +#[three_way(OrdOnly::operator_cmp)] #[byte_size(4)] pub struct OrdOnly { #[offset(0)] pub a: i32, } impl OrdOnly { - pub fn operator_cmp(this: Ptr, _a0: Ptr) -> std::cmp::Ordering { + pub fn operator_cmp(this: Ptr, _a0: Ptr) -> libcc2rs::StrongOrdering { { - let cmp: Value = Rc::new(RefCell::new(std::cmp::Ord::cmp( - &(this.with(|__s| __s.a)), - &(_a0.with(|__s| __s.a)), - ))); - if !(({ (*cmp.borrow()) }) == std::cmp::Ordering::Equal) { + let cmp: Value = + Rc::new(RefCell::new(libcc2rs::StrongOrdering::from( + std::cmp::Ord::cmp(&(this.with(|__s| __s.a)), &(_a0.with(|__s| __s.a))), + ))); + if !(libcc2rs::is_eq(({ (*cmp.borrow()) }))) { return (*cmp.borrow_mut()); } } return (*libcc2rs::strong_ordering_equal().upgrade().deref()); } } -impl CppLess for OrdOnly { +impl LessThanComparable for OrdOnly { fn less(this: Ptr, _a0: Ptr) -> bool { - OrdOnly::operator_cmp(this.clone(), _a0.clone()) == std::cmp::Ordering::Less + libcc2rs::is_lt(OrdOnly::operator_cmp(this.clone(), _a0.clone())) } } #[derive( @@ -155,29 +163,32 @@ impl CppLess for OrdOnly { TriviallyDefaultConstructible, TriviallyCopyable, Default, + PartiallyOrderedWith, )] +#[three_way(ThreeWayComparable::three_way)] #[byte_size(4)] pub struct Inner { #[offset(0)] pub x: i32, } -impl Inner { - pub fn operator_cmp(this: Ptr, _a0: Ptr) -> std::cmp::Ordering { +impl ThreeWayComparable for Inner { + type Category = libcc2rs::StrongOrdering; + fn three_way(this: Ptr, _a0: Ptr) -> libcc2rs::StrongOrdering { { - let cmp: Value = Rc::new(RefCell::new(std::cmp::Ord::cmp( - &(this.with(|__s| __s.x)), - &(_a0.with(|__s| __s.x)), - ))); - if !(({ (*cmp.borrow()) }) == std::cmp::Ordering::Equal) { + let cmp: Value = + Rc::new(RefCell::new(libcc2rs::StrongOrdering::from( + std::cmp::Ord::cmp(&(this.with(|__s| __s.x)), &(_a0.with(|__s| __s.x))), + ))); + if !(libcc2rs::is_eq(({ (*cmp.borrow()) }))) { return (*cmp.borrow_mut()); } } return (*libcc2rs::strong_ordering_equal().upgrade().deref()); } } -impl CppLess for Inner { +impl LessThanComparable for Inner { fn less(this: Ptr, _a0: Ptr) -> bool { - Inner::operator_cmp(this.clone(), _a0.clone()) == std::cmp::Ordering::Less + libcc2rs::is_lt(ThreeWayComparable::three_way(this.clone(), _a0.clone())) } } #[derive( @@ -191,7 +202,9 @@ impl CppLess for Inner { TriviallyDefaultConstructible, TriviallyCopyable, Default, + PartiallyOrderedWith, )] +#[three_way(ThreeWayComparable::three_way)] #[byte_size(8)] pub struct Outer { #[offset(0)] @@ -200,31 +213,32 @@ pub struct Outer { #[offset(4)] pub y: i32, } -impl Outer { - pub fn operator_cmp(this: Ptr, _a0: Ptr) -> std::cmp::Ordering { +impl ThreeWayComparable for Outer { + type Category = libcc2rs::StrongOrdering; + fn three_way(this: Ptr, _a0: Ptr) -> libcc2rs::StrongOrdering { { - let cmp: Value = Rc::new(RefCell::new( - ({ Inner::operator_cmp(field_ptr!(this, i), field_ptr!(_a0, i)) }), + let cmp: Value = Rc::new(RefCell::new( + ({ ThreeWayComparable::three_way(field_ptr!(this, i), field_ptr!(_a0, i)) }), )); - if !(({ (*cmp.borrow()) }) == std::cmp::Ordering::Equal) { + if !(libcc2rs::is_eq(({ (*cmp.borrow()) }))) { return (*cmp.borrow_mut()); } } { - let cmp: Value = Rc::new(RefCell::new(std::cmp::Ord::cmp( - &(this.with(|__s| __s.y)), - &(_a0.with(|__s| __s.y)), - ))); - if !(({ (*cmp.borrow()) }) == std::cmp::Ordering::Equal) { + let cmp: Value = + Rc::new(RefCell::new(libcc2rs::StrongOrdering::from( + std::cmp::Ord::cmp(&(this.with(|__s| __s.y)), &(_a0.with(|__s| __s.y))), + ))); + if !(libcc2rs::is_eq(({ (*cmp.borrow()) }))) { return (*cmp.borrow_mut()); } } return (*libcc2rs::strong_ordering_equal().upgrade().deref()); } } -impl CppLess for Outer { +impl LessThanComparable for Outer { fn less(this: Ptr, _a0: Ptr) -> bool { - Outer::operator_cmp(this.clone(), _a0.clone()) == std::cmp::Ordering::Less + libcc2rs::is_lt(ThreeWayComparable::three_way(this.clone(), _a0.clone())) } } #[derive( @@ -244,31 +258,24 @@ pub struct Secondary { #[offset(0)] pub a: i32, } -impl Secondary { - pub fn operator_cmp(this: Ptr, _a0: Ptr) -> std::cmp::Ordering { +impl ThreeWayComparable for Secondary { + type Category = libcc2rs::StrongOrdering; + fn three_way(this: Ptr, _a0: Ptr) -> libcc2rs::StrongOrdering { { - let cmp: Value = Rc::new(RefCell::new(std::cmp::Ord::cmp( - &(this.with(|__s| __s.a)), - &(_a0.with(|__s| __s.a)), - ))); - if !(({ (*cmp.borrow()) }) == std::cmp::Ordering::Equal) { + let cmp: Value = + Rc::new(RefCell::new(libcc2rs::StrongOrdering::from( + std::cmp::Ord::cmp(&(this.with(|__s| __s.a)), &(_a0.with(|__s| __s.a))), + ))); + if !(libcc2rs::is_eq(({ (*cmp.borrow()) }))) { return (*cmp.borrow_mut()); } } return (*libcc2rs::strong_ordering_equal().upgrade().deref()); } - pub fn operator_lt(this: Ptr, _a0: Ptr) -> bool { - return ({ Secondary::operator_cmp((this).clone(), (_a0).clone()) }) - == std::cmp::Ordering::Less; - } - pub fn operator_ge(this: Ptr, _a0: Ptr) -> bool { - return ({ Secondary::operator_cmp((this).clone(), (_a0).clone()) }) - != std::cmp::Ordering::Less; - } } -impl CppLess for Secondary { - fn less(this: Ptr, _a0: Ptr) -> bool { - Secondary::operator_lt(this.clone(), _a0.clone()) +impl LessThanComparable for Secondary { + fn less(this: Ptr, _a0: Ptr) -> bool { + return libcc2rs::is_lt(({ ThreeWayComparable::three_way((this).clone(), (_a0).clone()) })); } } #[derive( @@ -281,30 +288,35 @@ impl CppLess for Secondary { TriviallyDefaultConstructible, TriviallyCopyable, Default, + PartiallyOrderedWith, )] +#[three_way(ThreeWayComparable::three_way)] #[byte_size(8)] pub struct PtrMember { #[offset(0)] #[byte_size(8)] pub p: Ptr, } -impl PtrMember { - pub fn operator_cmp(this: Ptr, _a0: Ptr) -> std::cmp::Ordering { +impl ThreeWayComparable for PtrMember { + type Category = libcc2rs::StrongOrdering; + fn three_way(this: Ptr, _a0: Ptr) -> libcc2rs::StrongOrdering { { - let cmp: Value = Rc::new(RefCell::new(std::cmp::Ord::cmp( - &(this.with(|__s| __s.p.clone())), - &(_a0.with(|__s| __s.p.clone())), - ))); - if !(({ (*cmp.borrow()) }) == std::cmp::Ordering::Equal) { + let cmp: Value = Rc::new(RefCell::new( + libcc2rs::StrongOrdering::from(std::cmp::Ord::cmp( + &(this.with(|__s| __s.p.clone())), + &(_a0.with(|__s| __s.p.clone())), + )), + )); + if !(libcc2rs::is_eq(({ (*cmp.borrow()) }))) { return (*cmp.borrow_mut()); } } return (*libcc2rs::strong_ordering_equal().upgrade().deref()); } } -impl CppLess for PtrMember { +impl LessThanComparable for PtrMember { fn less(this: Ptr, _a0: Ptr) -> bool { - PtrMember::operator_cmp(this.clone(), _a0.clone()) == std::cmp::Ordering::Less + libcc2rs::is_lt(ThreeWayComparable::three_way(this.clone(), _a0.clone())) } } pub fn main() { @@ -321,30 +333,46 @@ fn main_0() -> i32 { let c2: Value = Rc::new(RefCell::new(Cmp { a: 1, b: 3 })); let c3: Value = Rc::new(RefCell::new(Cmp { a: 2, b: 0 })); let c4: Value = Rc::new(RefCell::new(Cmp { a: 1, b: 9 })); - assert!(({ Cmp::operator_cmp(c1.as_pointer(), c2.as_pointer(),) }) == std::cmp::Ordering::Less); - assert!( - ({ Cmp::operator_cmp(c3.as_pointer(), c4.as_pointer(),) }) == std::cmp::Ordering::Greater - ); + assert!(libcc2rs::is_lt( + ({ ThreeWayComparable::three_way(c1.as_pointer(), c2.as_pointer(),) }) + )); + assert!(libcc2rs::is_gt( + ({ ThreeWayComparable::three_way(c3.as_pointer(), c4.as_pointer(),) }) + )); assert!(({ WeaklyEqualityComparableWith::equal(c1.as_pointer(), c1.as_pointer(),) })); - assert!( - ({ Cmp::operator_cmp(c1.as_pointer(), c2.as_pointer(),) }) - == ({ (*libcc2rs::strong_ordering_less().upgrade().deref()) }) - ); + assert!({ + let __a = libcc2rs::PartialOrdering::from( + ({ ThreeWayComparable::three_way(c1.as_pointer(), c2.as_pointer()) }), + ); + let __b = libcc2rs::PartialOrdering::from( + ({ (*libcc2rs::strong_ordering_less().upgrade().deref()) }), + ); + libcc2rs::is_lt(__a) == libcc2rs::is_lt(__b) + && libcc2rs::is_eq(__a) == libcc2rs::is_eq(__b) + && libcc2rs::is_gt(__a) == libcc2rs::is_gt(__b) + }); let b1: Value = Rc::new(RefCell::new(Both { a: 1 })); let b2: Value = Rc::new(RefCell::new(Both { a: 2 })); - assert!( - ({ Both::operator_cmp(b1.as_pointer(), b2.as_pointer(),) }) == std::cmp::Ordering::Less - ); + assert!(libcc2rs::is_lt( + ({ ThreeWayComparable::three_way(b1.as_pointer(), b2.as_pointer(),) }) + )); assert!(({ WeaklyEqualityComparableWith::equal(b2.as_pointer(), b2.as_pointer(),) })); let o1: Value = Rc::new(RefCell::new(OrdOnly { a: 1 })); let o2: Value = Rc::new(RefCell::new(OrdOnly { a: 2 })); - assert!( - ({ OrdOnly::operator_cmp(o1.as_pointer(), o2.as_pointer(),) }) == std::cmp::Ordering::Less - ); - assert!( - ({ OrdOnly::operator_cmp(o2.as_pointer(), o1.as_pointer(),) }) - == ({ (*libcc2rs::strong_ordering_greater().upgrade().deref()) }) - ); + assert!(libcc2rs::is_lt( + ({ OrdOnly::operator_cmp(o1.as_pointer(), o2.as_pointer(),) }) + )); + assert!({ + let __a = libcc2rs::PartialOrdering::from( + ({ OrdOnly::operator_cmp(o2.as_pointer(), o1.as_pointer()) }), + ); + let __b = libcc2rs::PartialOrdering::from( + ({ (*libcc2rs::strong_ordering_greater().upgrade().deref()) }), + ); + libcc2rs::is_lt(__a) == libcc2rs::is_lt(__b) + && libcc2rs::is_eq(__a) == libcc2rs::is_eq(__b) + && libcc2rs::is_gt(__a) == libcc2rs::is_gt(__b) + }); let x1: Value = Rc::new(RefCell::new(Outer { i: Inner { x: 1 }, y: 9, @@ -357,37 +385,66 @@ fn main_0() -> i32 { i: Inner { x: 1 }, y: 9, })); - assert!( - ({ Outer::operator_cmp(x1.as_pointer(), x2.as_pointer(),) }) == std::cmp::Ordering::Less - ); + assert!(libcc2rs::is_lt( + ({ ThreeWayComparable::three_way(x1.as_pointer(), x2.as_pointer(),) }) + )); assert!(({ WeaklyEqualityComparableWith::equal(x1.as_pointer(), x3.as_pointer(),) })); - assert!( - ({ Outer::operator_cmp(x2.as_pointer(), x1.as_pointer(),) }) - == ({ (*libcc2rs::strong_ordering_greater().upgrade().deref()) }) - ); + assert!({ + let __a = libcc2rs::PartialOrdering::from( + ({ ThreeWayComparable::three_way(x2.as_pointer(), x1.as_pointer()) }), + ); + let __b = libcc2rs::PartialOrdering::from( + ({ (*libcc2rs::strong_ordering_greater().upgrade().deref()) }), + ); + libcc2rs::is_lt(__a) == libcc2rs::is_lt(__b) + && libcc2rs::is_eq(__a) == libcc2rs::is_eq(__b) + && libcc2rs::is_gt(__a) == libcc2rs::is_gt(__b) + }); let s1: Value = Rc::new(RefCell::new(Secondary { a: 1 })); let s2: Value = Rc::new(RefCell::new(Secondary { a: 2 })); assert!(({ WeaklyEqualityComparableWith::not_equal(s1.as_pointer(), s2.as_pointer(),) })); - assert!(({ Secondary::operator_lt(s1.as_pointer(), s2.as_pointer(),) })); - assert!(({ Secondary::operator_ge(s2.as_pointer(), s1.as_pointer(),) })); - assert!(!({ Secondary::operator_lt(s2.as_pointer(), s1.as_pointer(),) })); + assert!(({ LessThanComparable::less(s1.as_pointer(), s2.as_pointer(),) })); + assert!(({ PartiallyOrderedWith::greater_equal(s2.as_pointer(), s1.as_pointer(),) })); + assert!(!({ LessThanComparable::less(s2.as_pointer(), s1.as_pointer(),) })); let arr: Value> = Rc::new(RefCell::new(Box::new([0, 0]))); let p1: Value = Rc::new(RefCell::new(PtrMember { p: arr.decay() })); let p2: Value = Rc::new(RefCell::new(PtrMember { p: arr.decay().offset((1) as isize), })); let p3: Value = Rc::new(RefCell::new(PtrMember { p: arr.decay() })); - assert!( - ({ PtrMember::operator_cmp(p1.as_pointer(), p2.as_pointer(),) }) - == std::cmp::Ordering::Less - ); + assert!(libcc2rs::is_lt( + ({ ThreeWayComparable::three_way(p1.as_pointer(), p2.as_pointer(),) }) + )); assert!(({ WeaklyEqualityComparableWith::equal(p1.as_pointer(), p3.as_pointer(),) })); - assert!( - ({ PtrMember::operator_cmp(p2.as_pointer(), p1.as_pointer(),) }) - == ({ (*libcc2rs::strong_ordering_greater().upgrade().deref()) }) - ); + assert!({ + let __a = libcc2rs::PartialOrdering::from( + ({ ThreeWayComparable::three_way(p2.as_pointer(), p1.as_pointer()) }), + ); + let __b = libcc2rs::PartialOrdering::from( + ({ (*libcc2rs::strong_ordering_greater().upgrade().deref()) }), + ); + libcc2rs::is_lt(__a) == libcc2rs::is_lt(__b) + && libcc2rs::is_eq(__a) == libcc2rs::is_eq(__b) + && libcc2rs::is_gt(__a) == libcc2rs::is_gt(__b) + }); return 0; } +impl PartiallyOrderedWith for Secondary { + fn greater_equal(this: Ptr, _a0: Ptr) -> bool { + return libcc2rs::is_gteq( + ({ ThreeWayComparable::three_way((this).clone(), (_a0).clone()) }), + ); + } + fn less(lhs: Ptr, rhs: Ptr) -> bool { + ::less(lhs, rhs) + } + fn greater(lhs: Ptr, rhs: Ptr) -> bool { + libcc2rs::is_gt(ThreeWayComparable::three_way(lhs.clone(), rhs.clone())) + } + fn less_equal(lhs: Ptr, rhs: Ptr) -> bool { + libcc2rs::is_lteq(ThreeWayComparable::three_way(lhs.clone(), rhs.clone())) + } +} impl WeaklyEqualityComparableWith for Both { fn equal(this: Ptr, _a0: Ptr) -> bool { return ({ this.with(|__s| __s.a) } == { _a0.with(|__s| __s.a) }); diff --git a/tests/unit/out/refcount/operator_comparison_free.rs b/tests/unit/out/refcount/operator_comparison_free.rs index 6ebfb63406..00fa12bc4e 100644 --- a/tests/unit/out/refcount/operator_comparison_free.rs +++ b/tests/unit/out/refcount/operator_comparison_free.rs @@ -23,27 +23,15 @@ pub struct S { #[offset(0)] pub v: i32, } -impl CppLess for S { - fn less(a: Ptr, b: Ptr) -> bool { - operator_lt_0(a.clone(), b.clone()) +impl LessThanComparable for S { + fn less(a: Ptr, b: Ptr) -> bool { + return ({ a.with(|__s| __s.v) } < { b.with(|__s| __s.v) }); } } -pub fn operator_lt_0(a: Ptr, b: Ptr) -> bool { - return ({ a.with(|__s| __s.v) } < { b.with(|__s| __s.v) }); -} -pub fn operator_gt_1(a: Ptr, b: Ptr) -> bool { - return ({ a.with(|__s| __s.v) } > { b.with(|__s| __s.v) }); -} -pub fn operator_le_2(a: Ptr, b: Ptr) -> bool { - return ({ a.with(|__s| __s.v) } <= { b.with(|__s| __s.v) }); -} -pub fn operator_ge_3(a: Ptr, b: Ptr) -> bool { - return ({ a.with(|__s| __s.v) } >= { b.with(|__s| __s.v) }); -} -pub fn operator_lt_4(a: Ptr, mut b: i32) -> bool { +pub fn operator_lt_0(a: Ptr, mut b: i32) -> bool { return ({ a.with(|__s| __s.v) } < { b }); } -pub fn operator_lt_5(mut a: i32, b: Ptr) -> bool { +pub fn operator_lt_1(mut a: i32, b: Ptr) -> bool { return ({ a } < { b.with(|__s| __s.v) }); } #[derive( @@ -63,21 +51,21 @@ pub struct V { #[offset(0)] pub v: i32, } -impl CppLess for V { +impl LessThanComparable for V { fn less(a: Ptr, b: Ptr) -> bool { - operator_lt_7(a.read(), b.read()) + operator_lt_3(a.read(), b.read()) } } -pub fn operator_eq_8(mut a: V, mut b: V) -> bool { +pub fn operator_eq_4(mut a: V, mut b: V) -> bool { return (a.v == b.v); } -pub fn operator_ne_6(mut a: V, mut b: V) -> bool { +pub fn operator_ne_2(mut a: V, mut b: V) -> bool { return (a.v != b.v); } -pub fn operator_lt_7(mut a: V, mut b: V) -> bool { +pub fn operator_lt_3(mut a: V, mut b: V) -> bool { return (a.v < b.v); } -pub fn operator_gt_9(mut a: V, mut b: V) -> bool { +pub fn operator_gt_5(mut a: V, mut b: V) -> bool { return (a.v > b.v); } pub fn main() { @@ -88,25 +76,39 @@ fn main_0() -> i32 { let mut x: V = V { v: 1 }; let mut y: V = V { v: 2 }; let mut z: V = V { v: 1 }; - assert!(({ operator_eq_8(x, z,) })); - assert!(({ operator_ne_6(x, y,) })); - assert!(({ operator_lt_7(x, y,) })); - assert!(({ operator_gt_9(y, x,) })); - assert!(!({ operator_lt_7(y, x,) })); + assert!(({ operator_eq_4(x, z,) })); + assert!(({ operator_ne_2(x, y,) })); + assert!(({ operator_lt_3(x, y,) })); + assert!(({ operator_gt_5(y, x,) })); + assert!(!({ operator_lt_3(y, x,) })); let a: Value = Rc::new(RefCell::new(S { v: 1 })); let b: Value = Rc::new(RefCell::new(S { v: 2 })); let c: Value = Rc::new(RefCell::new(S { v: 1 })); assert!(({ WeaklyEqualityComparableWith::equal(a.as_pointer(), c.as_pointer(),) })); assert!(({ WeaklyEqualityComparableWith::not_equal(a.as_pointer(), b.as_pointer(),) })); - assert!(({ operator_lt_0(a.as_pointer(), b.as_pointer(),) })); - assert!(({ operator_gt_1(b.as_pointer(), a.as_pointer(),) })); - assert!(({ operator_le_2(a.as_pointer(), c.as_pointer(),) })); - assert!(({ operator_ge_3(a.as_pointer(), c.as_pointer(),) })); - assert!(!({ operator_lt_0(b.as_pointer(), a.as_pointer(),) })); - assert!(({ operator_lt_4(a.as_pointer(), 5,) })); - assert!(({ operator_lt_5(0, a.as_pointer(),) })); + assert!(({ LessThanComparable::less(a.as_pointer(), b.as_pointer(),) })); + assert!(({ PartiallyOrderedWith::greater(b.as_pointer(), a.as_pointer(),) })); + assert!(({ PartiallyOrderedWith::less_equal(a.as_pointer(), c.as_pointer(),) })); + assert!(({ PartiallyOrderedWith::greater_equal(a.as_pointer(), c.as_pointer(),) })); + assert!(!({ LessThanComparable::less(b.as_pointer(), a.as_pointer(),) })); + assert!(({ operator_lt_0(a.as_pointer(), 5,) })); + assert!(({ operator_lt_1(0, a.as_pointer(),) })); return 0; } +impl PartiallyOrderedWith for S { + fn less(lhs: Ptr, rhs: Ptr) -> bool { + ::less(lhs, rhs) + } + fn greater(a: Ptr, b: Ptr) -> bool { + return ({ a.with(|__s| __s.v) } > { b.with(|__s| __s.v) }); + } + fn less_equal(a: Ptr, b: Ptr) -> bool { + return ({ a.with(|__s| __s.v) } <= { b.with(|__s| __s.v) }); + } + fn greater_equal(a: Ptr, b: Ptr) -> bool { + return ({ a.with(|__s| __s.v) } >= { b.with(|__s| __s.v) }); + } +} impl WeaklyEqualityComparableWith for S { fn equal(a: Ptr, b: Ptr) -> bool { return ({ a.with(|__s| __s.v) } == { b.with(|__s| __s.v) }); @@ -117,10 +119,10 @@ impl WeaklyEqualityComparableWith for S { } impl WeaklyEqualityComparableWith for V { fn not_equal(a: Ptr, b: Ptr) -> bool { - operator_ne_6(a.read(), b.read()) + operator_ne_2(a.read(), b.read()) } fn equal(a: Ptr, b: Ptr) -> bool { - operator_eq_8(a.read(), b.read()) + operator_eq_4(a.read(), b.read()) } } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/operator_comparison_member.rs b/tests/unit/out/refcount/operator_comparison_member.rs index d0d5e86497..95230986c7 100644 --- a/tests/unit/out/refcount/operator_comparison_member.rs +++ b/tests/unit/out/refcount/operator_comparison_member.rs @@ -24,25 +24,13 @@ pub struct S { pub v: i32, } impl S { - pub fn operator_lt_3(this: Ptr, o: Ptr) -> bool { - return ({ this.with(|__s| __s.v) } < { o.with(|__s| __s.v) }); - } - pub fn operator_gt(this: Ptr, o: Ptr) -> bool { - return ({ this.with(|__s| __s.v) } > { o.with(|__s| __s.v) }); - } - pub fn operator_le(this: Ptr, o: Ptr) -> bool { - return ({ this.with(|__s| __s.v) } <= { o.with(|__s| __s.v) }); - } - pub fn operator_ge(this: Ptr, o: Ptr) -> bool { - return ({ this.with(|__s| __s.v) } >= { o.with(|__s| __s.v) }); - } pub fn operator_lt_7(this: Ptr, mut o: i32) -> bool { return (this.with(|__s| __s.v) < o); } } -impl CppLess for S { - fn less(this: Ptr, o: Ptr) -> bool { - S::operator_lt_3(this.clone(), o.clone()) +impl LessThanComparable for S { + fn less(this: Ptr, o: Ptr) -> bool { + return ({ this.with(|__s| __s.v) } < { o.with(|__s| __s.v) }); } } pub fn main() { @@ -55,14 +43,28 @@ fn main_0() -> i32 { let c: Value = Rc::new(RefCell::new(S { v: 1 })); assert!(({ WeaklyEqualityComparableWith::equal(a.as_pointer(), c.as_pointer(),) })); assert!(({ WeaklyEqualityComparableWith::not_equal(a.as_pointer(), b.as_pointer(),) })); - assert!(({ S::operator_lt_3(a.as_pointer(), b.as_pointer(),) })); - assert!(({ S::operator_gt(b.as_pointer(), a.as_pointer(),) })); - assert!(({ S::operator_le(a.as_pointer(), c.as_pointer(),) })); - assert!(({ S::operator_ge(a.as_pointer(), c.as_pointer(),) })); - assert!(!({ S::operator_lt_3(b.as_pointer(), a.as_pointer(),) })); + assert!(({ LessThanComparable::less(a.as_pointer(), b.as_pointer(),) })); + assert!(({ PartiallyOrderedWith::greater(b.as_pointer(), a.as_pointer(),) })); + assert!(({ PartiallyOrderedWith::less_equal(a.as_pointer(), c.as_pointer(),) })); + assert!(({ PartiallyOrderedWith::greater_equal(a.as_pointer(), c.as_pointer(),) })); + assert!(!({ LessThanComparable::less(b.as_pointer(), a.as_pointer(),) })); assert!(({ S::operator_lt_7(a.as_pointer(), 5,) })); return 0; } +impl PartiallyOrderedWith for S { + fn greater(this: Ptr, o: Ptr) -> bool { + return ({ this.with(|__s| __s.v) } > { o.with(|__s| __s.v) }); + } + fn less_equal(this: Ptr, o: Ptr) -> bool { + return ({ this.with(|__s| __s.v) } <= { o.with(|__s| __s.v) }); + } + fn greater_equal(this: Ptr, o: Ptr) -> bool { + return ({ this.with(|__s| __s.v) } >= { o.with(|__s| __s.v) }); + } + fn less(lhs: Ptr, rhs: Ptr) -> bool { + ::less(lhs, rhs) + } +} impl WeaklyEqualityComparableWith for S { fn equal(this: Ptr, o: Ptr) -> bool { return ({ this.with(|__s| __s.v) } == { o.with(|__s| __s.v) }); diff --git a/tests/unit/out/refcount/operator_comparison_nonbool.rs b/tests/unit/out/refcount/operator_comparison_nonbool.rs index e616eb253b..8c03fcda7f 100644 --- a/tests/unit/out/refcount/operator_comparison_nonbool.rs +++ b/tests/unit/out/refcount/operator_comparison_nonbool.rs @@ -23,27 +23,27 @@ pub struct X { #[offset(0)] pub v: i32, } -impl CppLess for X { +impl LessThanComparable for X { fn less(a: Ptr, b: Ptr) -> bool { - ((operator_lt_1(a.read(), b.read())) != 0) + ((operator_lt_4(a.read(), b.read())) != 0) } } -pub fn operator_eq_2(mut a: X, mut b: X) -> i32 { +pub fn operator_eq_5(mut a: X, mut b: X) -> i32 { return if (a.v == b.v) { 2 } else { 0 }; } pub fn operator_ne_0(mut a: X, mut b: X) -> i32 { return if (a.v != b.v) { 3 } else { 0 }; } -pub fn operator_lt_1(mut a: X, mut b: X) -> i32 { +pub fn operator_lt_4(mut a: X, mut b: X) -> i32 { return if (a.v < b.v) { 4 } else { 0 }; } -pub fn operator_gt_3(mut a: X, mut b: X) -> i32 { +pub fn operator_gt_1(mut a: X, mut b: X) -> i32 { return if (a.v > b.v) { 5 } else { 0 }; } -pub fn operator_le_4(mut a: X, mut b: X) -> i32 { +pub fn operator_le_2(mut a: X, mut b: X) -> i32 { return if (a.v <= b.v) { 6 } else { 0 }; } -pub fn operator_ge_5(mut a: X, mut b: X) -> i32 { +pub fn operator_ge_3(mut a: X, mut b: X) -> i32 { return if (a.v >= b.v) { 7 } else { 0 }; } #[derive( @@ -125,7 +125,7 @@ impl Mixed { }; } } -impl CppLess for Mixed { +impl LessThanComparable for Mixed { fn less(this: Ptr, o: Ptr) -> bool { ((Mixed::operator_lt(this.clone(), o.clone())) != 0) } @@ -174,7 +174,7 @@ pub struct Boolish { #[offset(0)] pub v: i32, } -impl CppLess for Boolish { +impl LessThanComparable for Boolish { fn less(a: Ptr, b: Ptr) -> bool { { let __cmp = operator_lt_6(a.read(), b.read()); @@ -259,13 +259,13 @@ fn main_0() -> i32 { let mut a: X = X { v: 1 }; let mut b: X = X { v: 2 }; let mut c: X = X { v: 1 }; - assert!((({ operator_eq_2(a, c,) }) == 2)); - assert!((({ operator_eq_2(a, b,) }) == 0)); + assert!((({ operator_eq_5(a, c,) }) == 2)); + assert!((({ operator_eq_5(a, b,) }) == 0)); assert!((({ operator_ne_0(a, b,) }) == 3)); - assert!((({ operator_lt_1(a, b,) }) == 4)); - assert!((({ operator_gt_3(b, a,) }) == 5)); - assert!((({ operator_le_4(a, c,) }) == 6)); - assert!((({ operator_ge_5(a, c,) }) == 7)); + assert!((({ operator_lt_4(a, b,) }) == 4)); + assert!((({ operator_gt_1(b, a,) }) == 5)); + assert!((({ operator_le_2(a, c,) }) == 6)); + assert!((({ operator_ge_3(a, c,) }) == 7)); let xs: Value> = Rc::new(RefCell::new(Box::new([X { v: 3 }, X { v: 1 }, X { v: 2 }]))); { let mut __tmp_0 = { xs.decay() }; @@ -358,6 +358,20 @@ fn main_0() -> i32 { ); return 0; } +impl PartiallyOrderedWith for X { + fn less(lhs: Ptr, rhs: Ptr) -> bool { + ::less(lhs, rhs) + } + fn greater(lhs: Ptr, rhs: Ptr) -> bool { + ((operator_gt_1(lhs.read(), rhs.read())) != 0) + } + fn less_equal(lhs: Ptr, rhs: Ptr) -> bool { + ((operator_le_2(lhs.read(), rhs.read())) != 0) + } + fn greater_equal(lhs: Ptr, rhs: Ptr) -> bool { + ((operator_ge_3(lhs.read(), rhs.read())) != 0) + } +} impl WeaklyEqualityComparableWith for Boolish { fn equal(a: Ptr, b: Ptr) -> bool { { @@ -384,7 +398,7 @@ impl WeaklyEqualityComparableWith for X { ((operator_ne_0(a.read(), b.read())) != 0) } fn equal(a: Ptr, b: Ptr) -> bool { - ((operator_eq_2(a.read(), b.read())) != 0) + ((operator_eq_5(a.read(), b.read())) != 0) } } pub fn __cpp2rust_init_globals() { diff --git a/tests/unit/out/refcount/operator_comparison_noncopyable.rs b/tests/unit/out/refcount/operator_comparison_noncopyable.rs index 8d00d4fb59..5428aa4572 100644 --- a/tests/unit/out/refcount/operator_comparison_noncopyable.rs +++ b/tests/unit/out/refcount/operator_comparison_noncopyable.rs @@ -6,8 +6,10 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -pub fn operator_lt_0(x: Ptr, y: Ptr) -> bool { - return ({ x.with(|__s| __s.data_) } < { y.with(|__s| __s.data_) }); +impl LessThanComparable for S { + fn less(x: Ptr, y: Ptr) -> bool { + return ({ x.with(|__s| __s.data_) } < { y.with(|__s| __s.data_) }); + } } #[derive( Record, @@ -31,11 +33,6 @@ impl S { field!(this, data_).write(data); } } -impl CppLess for S { - fn less(x: Ptr, y: Ptr) -> bool { - operator_lt_0(x.clone(), y.clone()) - } -} pub fn main() { __cpp2rust_init_globals(); libcc2rs::cpp2rust_exit(main_0()); @@ -45,8 +42,8 @@ fn main_0() -> i32 { object!(b, S(2)); object!(c, S(1)); assert!(({ WeaklyEqualityComparableWith::equal(a.as_pointer(), c.as_pointer(),) })); - assert!(({ operator_lt_0(a.as_pointer(), b.as_pointer(),) })); - assert!(!({ operator_lt_0(b.as_pointer(), a.as_pointer(),) })); + assert!(({ LessThanComparable::less(a.as_pointer(), b.as_pointer(),) })); + assert!(!({ LessThanComparable::less(b.as_pointer(), a.as_pointer(),) })); return 0; } impl WeaklyEqualityComparableWith for S { diff --git a/tests/unit/out/refcount/operator_friend.rs b/tests/unit/out/refcount/operator_friend.rs index 84cd7bd9af..e22d7ed68c 100644 --- a/tests/unit/out/refcount/operator_friend.rs +++ b/tests/unit/out/refcount/operator_friend.rs @@ -25,17 +25,20 @@ pub struct Defaulted { #[offset(4)] pub b: i32, } -pub fn operator_cmp_0(_a0: Ptr, _a1: Ptr) -> std::cmp::Ordering { - { - let cmp: Value = Rc::new(RefCell::new(std::cmp::Ord::cmp( - &(_a0.with(|__s| __s.a)), - &(_a1.with(|__s| __s.a)), - ))); - if !(({ (*cmp.borrow()) }) == std::cmp::Ordering::Equal) { - return (*cmp.borrow_mut()); +impl ThreeWayComparable for DefaultedOrd { + type Category = libcc2rs::StrongOrdering; + fn three_way(_a0: Ptr, _a1: Ptr) -> libcc2rs::StrongOrdering { + { + let cmp: Value = + Rc::new(RefCell::new(libcc2rs::StrongOrdering::from( + std::cmp::Ord::cmp(&(_a0.with(|__s| __s.a)), &(_a1.with(|__s| __s.a))), + ))); + if !(libcc2rs::is_eq(({ (*cmp.borrow()) }))) { + return (*cmp.borrow_mut()); + } } + return (*libcc2rs::strong_ordering_equal().upgrade().deref()); } - return (*libcc2rs::strong_ordering_equal().upgrade().deref()); } #[derive( Copy, @@ -48,21 +51,25 @@ pub fn operator_cmp_0(_a0: Ptr, _a1: Ptr) -> std::cm TriviallyDefaultConstructible, TriviallyCopyable, Default, + PartiallyOrderedWith, )] +#[three_way(ThreeWayComparable::three_way)] #[byte_size(4)] pub struct DefaultedOrd { #[offset(0)] pub a: i32, } -impl CppLess for DefaultedOrd { +impl LessThanComparable for DefaultedOrd { fn less(_a0: Ptr, _a1: Ptr) -> bool { - operator_cmp_0(_a0.clone(), _a1.clone()) == std::cmp::Ordering::Less + libcc2rs::is_lt(ThreeWayComparable::three_way(_a0, _a1.clone())) } } -pub fn operator_lt_1(x: Ptr, y: Ptr) -> bool { - return ({ x.with(|__s| __s.a) } < { y.with(|__s| __s.a) }); +impl LessThanComparable for Inline { + fn less(x: Ptr, y: Ptr) -> bool { + return ({ x.with(|__s| __s.a) } < { y.with(|__s| __s.a) }); + } } -pub fn operator_add_2(x: Ptr, y: Ptr) -> Inline { +pub fn operator_add_0(x: Ptr, y: Ptr) -> Inline { return Inline { a: ({ x.with(|__s| __s.a) } + { y.with(|__s| __s.a) }), }; @@ -84,11 +91,6 @@ pub struct Inline { #[offset(0)] pub a: i32, } -impl CppLess for Inline { - fn less(x: Ptr, y: Ptr) -> bool { - operator_lt_1(x.clone(), y.clone()) - } -} #[derive( Copy, Clone, @@ -106,8 +108,10 @@ pub struct OutOfLine { #[offset(0)] pub a: i32, } -pub fn operator_lt_3(x: Ptr, y: Ptr) -> bool { - return ({ x.with(|__s| __s.v) } < { y.with(|__s| __s.v) }); +impl LessThanComparable for Tmpl_int_ { + fn less(x: Ptr, y: Ptr) -> bool { + return ({ x.with(|__s| __s.v) } < { y.with(|__s| __s.v) }); + } } #[derive( Copy, @@ -126,11 +130,6 @@ pub struct Tmpl_int_ { #[offset(0)] pub v: i32, } -impl CppLess for Tmpl_int_ { - fn less(x: Ptr, y: Ptr) -> bool { - operator_lt_3(x.clone(), y.clone()) - } -} #[derive( Copy, Clone, @@ -178,24 +177,35 @@ fn main_0() -> i32 { assert!(!({ WeaklyEqualityComparableWith::equal(d1.as_pointer(), d3.as_pointer(),) })); let o1: Value = Rc::new(RefCell::new(DefaultedOrd { a: 1 })); let o2: Value = Rc::new(RefCell::new(DefaultedOrd { a: 2 })); - assert!(({ operator_cmp_0(o1.as_pointer(), o2.as_pointer(),) }) == std::cmp::Ordering::Less); - assert!(({ operator_cmp_0(o2.as_pointer(), o1.as_pointer(),) }) == std::cmp::Ordering::Greater); + assert!(libcc2rs::is_lt( + ({ ThreeWayComparable::three_way(o1.as_pointer(), o2.as_pointer(),) }) + )); + assert!(libcc2rs::is_gt( + ({ ThreeWayComparable::three_way(o2.as_pointer(), o1.as_pointer(),) }) + )); assert!(({ WeaklyEqualityComparableWith::equal(o1.as_pointer(), o1.as_pointer(),) })); - assert!( - ({ operator_cmp_0(o1.as_pointer(), o2.as_pointer(),) }) - == ({ (*libcc2rs::strong_ordering_less().upgrade().deref()) }) - ); + assert!({ + let __a = libcc2rs::PartialOrdering::from( + ({ ThreeWayComparable::three_way(o1.as_pointer(), o2.as_pointer()) }), + ); + let __b = libcc2rs::PartialOrdering::from( + ({ (*libcc2rs::strong_ordering_less().upgrade().deref()) }), + ); + libcc2rs::is_lt(__a) == libcc2rs::is_lt(__b) + && libcc2rs::is_eq(__a) == libcc2rs::is_eq(__b) + && libcc2rs::is_gt(__a) == libcc2rs::is_gt(__b) + }); let i1: Value = Rc::new(RefCell::new(Inline { a: 1 })); let i2: Value = Rc::new(RefCell::new(Inline { a: 2 })); let i3: Value = Rc::new(RefCell::new(Inline { a: 1 })); assert!(({ WeaklyEqualityComparableWith::equal(i1.as_pointer(), i3.as_pointer(),) })); assert!(!({ WeaklyEqualityComparableWith::equal(i1.as_pointer(), i2.as_pointer(),) })); - assert!(({ operator_lt_1(i1.as_pointer(), i2.as_pointer(),) })); - assert!(!({ operator_lt_1(i2.as_pointer(), i1.as_pointer(),) })); + assert!(({ LessThanComparable::less(i1.as_pointer(), i2.as_pointer(),) })); + assert!(!({ LessThanComparable::less(i2.as_pointer(), i1.as_pointer(),) })); assert!( ({ let _x: Value = Rc::new(RefCell::new( - ({ operator_add_2(i1.as_pointer(), i2.as_pointer()) }), + ({ operator_add_0(i1.as_pointer(), i2.as_pointer()) }), )); let _y: Value = Rc::new(RefCell::new(Inline { a: 3 })); WeaklyEqualityComparableWith::equal(_x.as_pointer(), _y.as_pointer()) @@ -212,7 +222,7 @@ fn main_0() -> i32 { let t3: Value = Rc::new(RefCell::new(Tmpl_int_ { v: 1 })); assert!(({ WeaklyEqualityComparableWith::equal(t1.as_pointer(), t3.as_pointer(),) })); assert!(!({ WeaklyEqualityComparableWith::equal(t1.as_pointer(), t2.as_pointer(),) })); - assert!(({ operator_lt_3(t1.as_pointer(), t2.as_pointer(),) })); + assert!(({ LessThanComparable::less(t1.as_pointer(), t2.as_pointer(),) })); let u1: Value = Rc::new(RefCell::new(Tmpl_long_ { v: 1_i64 })); let u2: Value = Rc::new(RefCell::new(Tmpl_long_ { v: 2_i64 })); assert!(({ WeaklyEqualityComparableWith::equal(t1.as_pointer(), u1.as_pointer(),) })); diff --git a/tests/unit/out/refcount/operator_less_than.rs b/tests/unit/out/refcount/operator_less_than.rs index 47bb4adc05..b62e640d02 100644 --- a/tests/unit/out/refcount/operator_less_than.rs +++ b/tests/unit/out/refcount/operator_less_than.rs @@ -32,7 +32,7 @@ impl Pair { && ({ this.with(|__s| __s.y) } < { other.with(|__s| __s.y) })); } } -impl CppLess for Pair { +impl LessThanComparable for Pair { fn less(this: Ptr, other: Ptr) -> bool { Pair::operator_lt(this.clone(), other.clone()) } diff --git a/tests/unit/out/refcount/operator_three_way.rs b/tests/unit/out/refcount/operator_three_way.rs index 54907d11aa..96e874ef02 100644 --- a/tests/unit/out/refcount/operator_three_way.rs +++ b/tests/unit/out/refcount/operator_three_way.rs @@ -17,14 +17,17 @@ use std::rc::{Rc, Weak}; TriviallyDefaultConstructible, TriviallyCopyable, Default, + PartiallyOrderedWith, )] +#[three_way(ThreeWayComparable::three_way)] #[byte_size(4)] pub struct S { #[offset(0)] pub v: i32, } -impl S { - pub fn operator_cmp(this: Ptr, o: Ptr) -> std::cmp::Ordering { +impl ThreeWayComparable for S { + type Category = libcc2rs::StrongOrdering; + fn three_way(this: Ptr, o: Ptr) -> libcc2rs::StrongOrdering { if ({ this.with(|__s| __s.v) } < { o.with(|__s| __s.v) }) { return (*libcc2rs::strong_ordering_less().upgrade().deref()); } @@ -34,9 +37,9 @@ impl S { return (*libcc2rs::strong_ordering_equal().upgrade().deref()); } } -impl CppLess for S { +impl LessThanComparable for S { fn less(this: Ptr, o: Ptr) -> bool { - S::operator_cmp(this.clone(), o.clone()) == std::cmp::Ordering::Less + libcc2rs::is_lt(ThreeWayComparable::three_way(this.clone(), o.clone())) } } pub fn main() { @@ -46,15 +49,30 @@ pub fn main() { fn main_0() -> i32 { let a: Value = Rc::new(RefCell::new(S { v: 1 })); let b: Value = Rc::new(RefCell::new(S { v: 2 })); - assert!(({ S::operator_cmp(a.as_pointer(), b.as_pointer(),) }) == std::cmp::Ordering::Less); - assert!(({ S::operator_cmp(b.as_pointer(), a.as_pointer(),) }) == std::cmp::Ordering::Greater); - assert!(({ S::operator_cmp(a.as_pointer(), b.as_pointer(),) }) != std::cmp::Ordering::Greater); - assert!(({ S::operator_cmp(b.as_pointer(), a.as_pointer(),) }) != std::cmp::Ordering::Less); + assert!(libcc2rs::is_lt( + ({ ThreeWayComparable::three_way(a.as_pointer(), b.as_pointer(),) }) + )); + assert!(libcc2rs::is_gt( + ({ ThreeWayComparable::three_way(b.as_pointer(), a.as_pointer(),) }) + )); + assert!(libcc2rs::is_lteq( + ({ ThreeWayComparable::three_way(a.as_pointer(), b.as_pointer(),) }) + )); + assert!(libcc2rs::is_gteq( + ({ ThreeWayComparable::three_way(b.as_pointer(), a.as_pointer(),) }) + )); assert!(!({ WeaklyEqualityComparableWith::equal(a.as_pointer(), b.as_pointer(),) })); - assert!( - ({ S::operator_cmp(a.as_pointer(), b.as_pointer(),) }) - == ({ (*libcc2rs::strong_ordering_less().upgrade().deref()) }) - ); + assert!({ + let __a = libcc2rs::PartialOrdering::from( + ({ ThreeWayComparable::three_way(a.as_pointer(), b.as_pointer()) }), + ); + let __b = libcc2rs::PartialOrdering::from( + ({ (*libcc2rs::strong_ordering_less().upgrade().deref()) }), + ); + libcc2rs::is_lt(__a) == libcc2rs::is_lt(__b) + && libcc2rs::is_eq(__a) == libcc2rs::is_eq(__b) + && libcc2rs::is_gt(__a) == libcc2rs::is_gt(__b) + }); return 0; } impl WeaklyEqualityComparableWith for S { diff --git a/tests/unit/out/refcount/operator_traits.rs b/tests/unit/out/refcount/operator_traits.rs index 7d3202e71e..3327a1528d 100644 --- a/tests/unit/out/refcount/operator_traits.rs +++ b/tests/unit/out/refcount/operator_traits.rs @@ -23,16 +23,11 @@ pub struct Lt { #[offset(0)] pub v: i32, } -impl Lt { - pub fn operator_lt(this: Ptr, o: Ptr) -> bool { +impl LessThanComparable for Lt { + fn less(this: Ptr, o: Ptr) -> bool { return ({ this.with(|__s| __s.v) } < { o.with(|__s| __s.v) }); } } -impl CppLess for Lt { - fn less(this: Ptr, o: Ptr) -> bool { - Lt::operator_lt(this.clone(), o.clone()) - } -} #[derive( Copy, Clone, @@ -61,20 +56,26 @@ pub struct Eq { TriviallyDefaultConstructible, TriviallyCopyable, Default, + PartiallyOrderedWith, )] +#[three_way(ThreeWayComparable::three_way)] #[byte_size(4)] pub struct Cmp { #[offset(0)] pub v: i32, } -impl Cmp { - pub fn operator_cmp(this: Ptr, o: Ptr) -> std::cmp::Ordering { - return std::cmp::Ord::cmp(&(this.with(|__s| __s.v)), &(o.with(|__s| __s.v))); +impl ThreeWayComparable for Cmp { + type Category = libcc2rs::StrongOrdering; + fn three_way(this: Ptr, o: Ptr) -> libcc2rs::StrongOrdering { + return libcc2rs::StrongOrdering::from(std::cmp::Ord::cmp( + &(this.with(|__s| __s.v)), + &(o.with(|__s| __s.v)), + )); } } -impl CppLess for Cmp { +impl LessThanComparable for Cmp { fn less(this: Ptr, o: Ptr) -> bool { - Cmp::operator_cmp(this.clone(), o.clone()) == std::cmp::Ordering::Less + libcc2rs::is_lt(ThreeWayComparable::three_way(this.clone(), o.clone())) } } #[derive( @@ -94,14 +95,11 @@ pub struct Free { #[offset(0)] pub v: i32, } -impl CppLess for Free { - fn less(a: Ptr, b: Ptr) -> bool { - operator_lt_0(a.clone(), b.clone()) +impl LessThanComparable for Free { + fn less(a: Ptr, b: Ptr) -> bool { + return ({ a.with(|__s| __s.v) } < { b.with(|__s| __s.v) }); } } -pub fn operator_lt_0(a: Ptr, b: Ptr) -> bool { - return ({ a.with(|__s| __s.v) } < { b.with(|__s| __s.v) }); -} #[derive( Copy, Clone, diff --git a/tests/unit/out/refcount/size_t_ssize_t.rs b/tests/unit/out/refcount/size_t_ssize_t.rs index 350bcdccec..895a9c7f1a 100644 --- a/tests/unit/out/refcount/size_t_ssize_t.rs +++ b/tests/unit/out/refcount/size_t_ssize_t.rs @@ -243,7 +243,10 @@ pub fn size_t_in_rules_3() { let __tmp_10: Value = Rc::new(RefCell::new(((*k.borrow()) as u64))); (match (v.decay().offset(1_usize),) { (__tmp_9,) => { - if CppLess::less((__tmp_9).clone().clone(), __tmp_10.as_pointer().clone()) { + if LessThanComparable::less( + (__tmp_9).clone().clone(), + __tmp_10.as_pointer().clone(), + ) { __tmp_10.as_pointer() } else { (__tmp_9).clone() @@ -412,18 +415,13 @@ fn main_0() -> i32 { (::std::mem::size_of::() as u64).wrapping_add((*ul.borrow())), )); let __tmp_22: Value = Rc::new(RefCell::new((sz as u64))); - (if CppLess::less(__tmp_21.as_pointer().clone(), __tmp_22.as_pointer().clone()) { - __tmp_21.as_pointer() - } else { - __tmp_22.as_pointer() - } - .read()) + (if LessThanComparable::less(__tmp_21.as_pointer().clone(), __tmp_22.as_pointer().clone()) { __tmp_21.as_pointer() } else { __tmp_22.as_pointer() } .read()) }) as usize); let mut hi: usize = (({ let __tmp_23: Value = Rc::new(RefCell::new( (::std::mem::size_of::() as u64).wrapping_add((sz as u64)), )); - (if CppLess::less(__tmp_23.as_pointer().clone(), ul.as_pointer().clone()) { + (if LessThanComparable::less(__tmp_23.as_pointer().clone(), ul.as_pointer().clone()) { ul.as_pointer() } else { __tmp_23.as_pointer() @@ -435,12 +433,7 @@ fn main_0() -> i32 { let mut bound: usize = (({ let __tmp_24: Value = Rc::new(RefCell::new((4_usize as u64))); let __tmp_25: Value = Rc::new(RefCell::new((sz as u64))); - (if CppLess::less(__tmp_24.as_pointer().clone(), __tmp_25.as_pointer().clone()) { - __tmp_24.as_pointer() - } else { - __tmp_25.as_pointer() - } - .read()) + (if LessThanComparable::less(__tmp_24.as_pointer().clone(), __tmp_25.as_pointer().clone()) { __tmp_24.as_pointer() } else { __tmp_25.as_pointer() } .read()) }) as usize); assert!((bound == 4_usize)); let mut data: [i32; 8] = [0_i32; 8]; @@ -491,22 +484,12 @@ fn main_0() -> i32 { let mut smin: isize = (({ let __tmp_26: Value = Rc::new(RefCell::new((sm as i64))); let __tmp_27: Value = Rc::new(RefCell::new((sd as i64))); - (if CppLess::less(__tmp_26.as_pointer().clone(), __tmp_27.as_pointer().clone()) { - __tmp_26.as_pointer() - } else { - __tmp_27.as_pointer() - } - .read()) + (if LessThanComparable::less(__tmp_26.as_pointer().clone(), __tmp_27.as_pointer().clone()) { __tmp_26.as_pointer() } else { __tmp_27.as_pointer() } .read()) }) as isize); let mut smax: isize = (({ let __tmp_28: Value = Rc::new(RefCell::new((sd as i64))); let __tmp_29: Value = Rc::new(RefCell::new((sm as i64))); - (if CppLess::less(__tmp_28.as_pointer().clone(), __tmp_29.as_pointer().clone()) { - __tmp_29.as_pointer() - } else { - __tmp_28.as_pointer() - } - .read()) + (if LessThanComparable::less(__tmp_28.as_pointer().clone(), __tmp_29.as_pointer().clone()) { __tmp_29.as_pointer() } else { __tmp_28.as_pointer() } .read()) }) as isize); assert!((smin == (-7_i32 as isize))); assert!((smax == 15_isize)); @@ -528,7 +511,10 @@ fn main_0() -> i32 { (({ let __tmp_30: Value = Rc::new(RefCell::new((mx as u64))); let __tmp_31: Value = Rc::new(RefCell::new(1_u64)); - (if CppLess::less(__tmp_30.as_pointer().clone(), __tmp_31.as_pointer().clone()) { + (if LessThanComparable::less( + __tmp_30.as_pointer().clone(), + __tmp_31.as_pointer().clone(), + ) { __tmp_30.as_pointer() } else { __tmp_31.as_pointer() @@ -538,7 +524,10 @@ fn main_0() -> i32 { (({ let __tmp_32: Value = Rc::new(RefCell::new(((mx).wrapping_sub(3_usize) as u64))); let __tmp_33: Value = Rc::new(RefCell::new((mx as u64))); - (if CppLess::less(__tmp_32.as_pointer().clone(), __tmp_33.as_pointer().clone()) { + (if LessThanComparable::less( + __tmp_32.as_pointer().clone(), + __tmp_33.as_pointer().clone(), + ) { __tmp_32.as_pointer() } else { __tmp_33.as_pointer() diff --git a/tests/unit/out/refcount/sort_no_copy.rs b/tests/unit/out/refcount/sort_no_copy.rs index 0fc9872366..6ff5de4e6d 100644 --- a/tests/unit/out/refcount/sort_no_copy.rs +++ b/tests/unit/out/refcount/sort_no_copy.rs @@ -50,11 +50,6 @@ impl MoveAssignable for Tracked { return (this).clone(); } } -impl CppLess for Tracked { - fn less(a: Ptr, b: Ptr) -> bool { - operator_lt_1(a.clone(), b.clone()) - } -} impl Default for Tracked { fn default() -> Self { { @@ -66,10 +61,12 @@ impl Default for Tracked { } } } -pub fn operator_lt_1(a: Ptr, b: Ptr) -> bool { - return ({ a.with(|__s| __s.v) } < { b.with(|__s| __s.v) }); +impl LessThanComparable for Tracked { + fn less(a: Ptr, b: Ptr) -> bool { + return ({ a.with(|__s| __s.v) } < { b.with(|__s| __s.v) }); + } } -pub fn greater_2(a: Ptr, b: Ptr) -> bool { +pub fn greater_1(a: Ptr, b: Ptr) -> bool { return ({ a.with(|__s| __s.v) } > { b.with(|__s| __s.v) }); } #[derive( @@ -95,14 +92,11 @@ impl MoveOnly { field!(this, v).write(v); } } -impl CppLess for MoveOnly { - fn less(a: Ptr, b: Ptr) -> bool { - operator_lt_3(a.clone(), b.clone()) +impl LessThanComparable for MoveOnly { + fn less(a: Ptr, b: Ptr) -> bool { + return ({ a.with(|__s| __s.v) } < { b.with(|__s| __s.v) }); } } -pub fn operator_lt_3(a: Ptr, b: Ptr) -> bool { - return ({ a.with(|__s| __s.v) } < { b.with(|__s| __s.v) }); -} #[derive( Record, ByteRepr, ZeroInitialization, FnPtrArg, Destructible, Memberwise, Swappable, Default, )] @@ -118,14 +112,11 @@ impl DefaultConstructible for Bag { field!(this, items).write(Vec::new()); } } -impl CppLess for Bag { - fn less(a: Ptr, b: Ptr) -> bool { - operator_lt_4(a.clone(), b.clone()) +impl LessThanComparable for Bag { + fn less(a: Ptr, b: Ptr) -> bool { + return ({ (*a.upgrade().deref()).items.len() } < { (*b.upgrade().deref()).items.len() }); } } -pub fn operator_lt_4(a: Ptr, b: Ptr) -> bool { - return ({ (*a.upgrade().deref()).items.len() } < { (*b.upgrade().deref()).items.len() }); -} pub fn main() { __cpp2rust_init_globals(); libcc2rs::cpp2rust_exit(main_0()); @@ -240,9 +231,9 @@ fn main_0() -> i32 { __order.sort_by(|&__x, &__y| { let __a = __tmp_6.iter_at(__x); let __b = __tmp_6.iter_at(__y); - if greater_2.call(__a.clone(), __b.clone()) { + if greater_1.call(__a.clone(), __b.clone()) { std::cmp::Ordering::Less - } else if greater_2.call(__b, __a) { + } else if greater_1.call(__b, __a) { std::cmp::Ordering::Greater } else { std::cmp::Ordering::Equal @@ -294,9 +285,9 @@ fn main_0() -> i32 { __order.sort_by(|&__x, &__y| { let __a = __tmp_8.iter_at(__x); let __b = __tmp_8.iter_at(__y); - if greater_2.call(__a.clone(), __b.clone()) { + if greater_1.call(__a.clone(), __b.clone()) { std::cmp::Ordering::Less - } else if greater_2.call(__b, __a) { + } else if greater_1.call(__b, __a) { std::cmp::Ordering::Greater } else { std::cmp::Ordering::Equal @@ -455,7 +446,7 @@ fn main_0() -> i32 { __it.borrow_mut().iter_inc(); while >::not_equal(__it.as_pointer(), __last.as_pointer()) { - if CppLess::less(__best.iter_deref(), __it.borrow().iter_deref()) { + if LessThanComparable::less(__best.iter_deref(), __it.borrow().iter_deref()) { __best = __it.borrow().clone(); } __it.borrow_mut().iter_inc(); @@ -471,7 +462,7 @@ fn main_0() -> i32 { } }); assert!( - ((if CppLess::less( + ((if LessThanComparable::less( bags.decay().offset(2).clone(), bags.decay().offset(0).clone() ) { @@ -481,7 +472,7 @@ fn main_0() -> i32 { }) == (bags.decay().offset(0))) ); assert!( - ((if CppLess::less( + ((if LessThanComparable::less( bags.decay().offset(0).clone(), bags.decay().offset(2).clone() ) { diff --git a/tests/unit/out/refcount/stdmin_fpoint.rs b/tests/unit/out/refcount/stdmin_fpoint.rs index c064fd3549..23a3bcef2f 100644 --- a/tests/unit/out/refcount/stdmin_fpoint.rs +++ b/tests/unit/out/refcount/stdmin_fpoint.rs @@ -13,7 +13,7 @@ pub fn main() { fn main_0() -> i32 { let a0: Value = Rc::new(RefCell::new(3.14_f64)); let a1: Value = Rc::new(RefCell::new(2.71_f64)); - if CppLess::less(a1.as_pointer().clone(), a0.as_pointer().clone()) { + if LessThanComparable::less(a1.as_pointer().clone(), a0.as_pointer().clone()) { a1.as_pointer() } else { a0.as_pointer() diff --git a/tests/unit/out/refcount/unboxed_locals.rs b/tests/unit/out/refcount/unboxed_locals.rs index be10bc8eed..3793ad37fe 100644 --- a/tests/unit/out/refcount/unboxed_locals.rs +++ b/tests/unit/out/refcount/unboxed_locals.rs @@ -172,7 +172,7 @@ fn main_0() -> i32 { let high: Value = Rc::new(RefCell::new(3)); let mut low: i32 = { let __tmp_0: Value = Rc::new(RefCell::new(2)); - (if CppLess::less(__tmp_0.as_pointer().clone(), high.as_pointer().clone()) { + (if LessThanComparable::less(__tmp_0.as_pointer().clone(), high.as_pointer().clone()) { __tmp_0.as_pointer() } else { high.as_pointer() diff --git a/tests/unit/out/unsafe/cast.rs b/tests/unit/out/unsafe/cast.rs index 64c45ff4cc..0ebeef59cb 100644 --- a/tests/unit/out/unsafe/cast.rs +++ b/tests/unit/out/unsafe/cast.rs @@ -178,7 +178,17 @@ pub unsafe fn order_physical_2() { let mut __tmp_0 = { ps.as_mut_ptr() }; let __n = ps.as_mut_ptr().offset((4) as isize).iter_distance(&__tmp_0); let mut __order: Vec = (0..__n).collect(); - __order.sort_by(|&__x, &__y| (*__tmp_0.iter_at(__x)).cmp(&*__tmp_0.iter_at(__y))); + __order.sort_by(|&__x, &__y| { + let __a = __tmp_0.iter_at(__x) as *const _; + let __b = __tmp_0.iter_at(__y) as *const _; + if LessThanComparableUnsafe::less(__a, __b) { + std::cmp::Ordering::Less + } else if LessThanComparableUnsafe::less(__b, __a) { + std::cmp::Ordering::Greater + } else { + std::cmp::Ordering::Equal + } + }); let __values: Vec<_> = __order .iter() .map(|&__i| std::ptr::read(__tmp_0.iter_at(__i))) diff --git a/tests/unit/out/unsafe/elidable_iterator_copy_cxx11.rs b/tests/unit/out/unsafe/elidable_iterator_copy_cxx11.rs index 8c01e80d11..06a5114584 100644 --- a/tests/unit/out/unsafe/elidable_iterator_copy_cxx11.rs +++ b/tests/unit/out/unsafe/elidable_iterator_copy_cxx11.rs @@ -47,7 +47,17 @@ unsafe fn main_0() -> i32 { let mut __tmp_5 = { v.begin() }; let __n = v.end().iter_distance(&__tmp_5); let mut __order: Vec = (0..__n).collect(); - __order.sort_by(|&__x, &__y| (*__tmp_5.iter_at(__x)).cmp(&*__tmp_5.iter_at(__y))); + __order.sort_by(|&__x, &__y| { + let __a = __tmp_5.iter_at(__x) as *const _; + let __b = __tmp_5.iter_at(__y) as *const _; + if LessThanComparableUnsafe::less(__a, __b) { + std::cmp::Ordering::Less + } else if LessThanComparableUnsafe::less(__b, __a) { + std::cmp::Ordering::Greater + } else { + std::cmp::Ordering::Equal + } + }); let __values: Vec<_> = __order .iter() .map(|&__i| std::ptr::read(__tmp_5.iter_at(__i))) diff --git a/tests/unit/out/unsafe/fn_ptr.rs b/tests/unit/out/unsafe/fn_ptr.rs index 33631eaf0e..870d01053d 100644 --- a/tests/unit/out/unsafe/fn_ptr.rs +++ b/tests/unit/out/unsafe/fn_ptr.rs @@ -65,8 +65,8 @@ pub unsafe fn string_size_10(mut s: Vec) -> usize { pub unsafe fn vector_sum_11(mut v: Vec) -> i32 { return ((v[(0_usize)]) + (v[(1_usize)])); } -pub unsafe fn is_less_12(mut o: std::cmp::Ordering) -> bool { - return o == std::cmp::Ordering::Less; +pub unsafe fn is_less_12(mut o: libcc2rs::StrongOrdering) -> bool { + return libcc2rs::is_lt(o); } pub fn main() { unsafe { @@ -122,12 +122,33 @@ unsafe fn main_0() -> i32 { ); let mut vf: Option) -> i32> = Some(vector_sum_11); assert!(((unsafe { (vf).unwrap()(Vec::from([1, 2,]),) }) == (3))); - let mut of: Option bool> = Some(is_less_12); - assert!((unsafe { (of).unwrap()(std::cmp::Ord::cmp(&(1), &(2)),) })); - assert!(!(unsafe { (of).unwrap()(std::cmp::Ord::cmp(&(2), &(1)),) })); - let mut o: std::cmp::Ordering = std::cmp::Ord::cmp(&(1), &(2)); - let mut op: *mut std::cmp::Ordering = (&mut o as *mut std::cmp::Ordering); - assert!((*op) == libcc2rs::STRONG_ORDERING_LESS); + let mut of: Option bool> = Some(is_less_12); + assert!( + (unsafe { + (of).unwrap()(libcc2rs::StrongOrdering::from(std::cmp::Ord::cmp( + &(1), + &(2), + ))) + }) + ); + assert!( + !(unsafe { + (of).unwrap()(libcc2rs::StrongOrdering::from(std::cmp::Ord::cmp( + &(2), + &(1), + ))) + }) + ); + let mut o: libcc2rs::StrongOrdering = + libcc2rs::StrongOrdering::from(std::cmp::Ord::cmp(&(1), &(2))); + let mut op: *mut libcc2rs::StrongOrdering = (&mut o as *mut libcc2rs::StrongOrdering); + assert!({ + let __a = libcc2rs::PartialOrdering::from((*op)); + let __b = libcc2rs::PartialOrdering::from(libcc2rs::STRONG_ORDERING_LESS); + libcc2rs::is_lt(__a) == libcc2rs::is_lt(__b) + && libcc2rs::is_eq(__a) == libcc2rs::is_eq(__b) + && libcc2rs::is_gt(__a) == libcc2rs::is_gt(__b) + }); return 0; } pub unsafe fn __cpp2rust_init_globals() { diff --git a/tests/unit/out/unsafe/global_call_temporaries.rs b/tests/unit/out/unsafe/global_call_temporaries.rs index 7ce2af698a..718dcbe31b 100644 --- a/tests/unit/out/unsafe/global_call_temporaries.rs +++ b/tests/unit/out/unsafe/global_call_temporaries.rs @@ -20,7 +20,11 @@ pub static mut selected_1: std::cell::LazyCell = std::cell::LazyCell::new(| { let __a: *const _ = &mut __tmp_0; let __b: *const _ = &mut __tmp_1; - if *__b < *__a { __b } else { __a } + if LessThanComparableUnsafe::less(__b, __a) { + __b + } else { + __a + } } }) }) @@ -39,7 +43,11 @@ pub static mut assigned_4: std::cell::LazyCell = std::cell::LazyCell::new(| (*{ let __a: *const _ = &mut __tmp_2; let __b: *const _ = &mut __tmp_3; - if *__b < *__a { __b } else { __a } + if LessThanComparableUnsafe::less(__b, __a) { + __b + } else { + __a + } }) }; (*std::cell::LazyCell::force_mut(&mut *&raw mut x_2)) @@ -57,7 +65,11 @@ pub static mut accumulated_5: std::cell::LazyCell = std::cell::LazyCell::ne (*{ let __a: *const _ = &mut __tmp_4; let __b: *const _ = &mut __tmp_5; - if *__a < *__b { __b } else { __a } + if LessThanComparableUnsafe::less(__a, __b) { + __b + } else { + __a + } }) }; (*std::cell::LazyCell::force_mut(&mut *&raw mut y_3)) @@ -112,7 +124,11 @@ pub static mut aggregate_selected_9: std::cell::LazyCell = (*{ let __a: *const _ = &mut __tmp_8; let __b: *const _ = &mut __tmp_9; - if *__b < *__a { __b } else { __a } + if LessThanComparableUnsafe::less(__b, __a) { + __b + } else { + __a + } }) }]; read_array_7(&mut __tmp_7) @@ -124,7 +140,11 @@ pub static mut aggregate_selected_9: std::cell::LazyCell = (*{ let __a: *const _ = &mut __tmp_11; let __b: *const _ = &mut __tmp_12; - if *__a < *__b { __b } else { __a } + if LessThanComparableUnsafe::less(__a, __b) { + __b + } else { + __a + } }) }, }; @@ -145,7 +165,11 @@ pub static mut mapped_selected_12: std::cell::LazyCell = let __a: *const _ = &mut (*std::cell::LazyCell::force_mut(&mut *&raw mut left_10)); let __b: *const _ = &mut __tmp_13; - if *__b < *__a { __b } else { __a } + if LessThanComparableUnsafe::less(__b, __a) { + __b + } else { + __a + } }) }) as usize) } @@ -161,7 +185,11 @@ static mut value_14: std::cell::LazyCell = std::cell::LazyCell::new(|| un (*{ let __a: *const _ = &mut (*std::cell::LazyCell::force_mut(&mut *&raw mut left_10)); let __b: *const _ = &mut __tmp_14; - if *__a < *__b { __b } else { __a } + if LessThanComparableUnsafe::less(__a, __b) { + __b + } else { + __a + } }) }) as usize) } @@ -198,7 +226,11 @@ static mut aggregate_static_15: std::cell::LazyCell = std::cell::LazyCell:: (*{ let __a: *const _ = &mut __tmp_16; let __b: *const _ = &mut __tmp_17; - if *__a < *__b { __b } else { __a } + if LessThanComparableUnsafe::less(__a, __b) { + __b + } else { + __a + } }) }]; read_array_7(&mut __tmp_15) @@ -210,7 +242,11 @@ static mut aggregate_static_15: std::cell::LazyCell = std::cell::LazyCell:: (*{ let __a: *const _ = &mut __tmp_19; let __b: *const _ = &mut __tmp_20; - if *__b < *__a { __b } else { __a } + if LessThanComparableUnsafe::less(__b, __a) { + __b + } else { + __a + } }) }, }; diff --git a/tests/unit/out/unsafe/map_key_copy_ctor.rs b/tests/unit/out/unsafe/map_key_copy_ctor.rs index 019299a70c..25fd06ce01 100644 --- a/tests/unit/out/unsafe/map_key_copy_ctor.rs +++ b/tests/unit/out/unsafe/map_key_copy_ctor.rs @@ -21,9 +21,6 @@ impl Key { pub unsafe fn construct(this: *mut Self, mut k: i32) { (&raw mut (*this).k).write(k); } - pub unsafe fn operator_lt(this: *const Self, o: *const Key) -> bool { - return (((*this).k) < ((*o).k)); - } } impl CopyConstructibleUnsafe for Key { unsafe fn copy_constructor(this: *mut Self, o: *const Key) { @@ -37,33 +34,11 @@ impl MoveConstructibleUnsafe for Key { (*std::cell::LazyCell::force_mut(&mut *&raw mut moves_1)).prefix_inc(); } } -impl std::cmp::Ord for Key { - fn cmp(&self, other: &Self) -> std::cmp::Ordering { - unsafe { - if Key::operator_lt((self as *const Self), (other as *const Self)) { - std::cmp::Ordering::Less - } else if Key::operator_lt((other as *const Self), (self as *const Self)) { - std::cmp::Ordering::Greater - } else { - std::cmp::Ordering::Equal - } - } - } -} -impl std::cmp::PartialOrd for Key { - fn partial_cmp(&self, other: &Self) -> Option { - Some(self.cmp(other)) - } -} -impl std::cmp::PartialEq for Key { - fn eq(&self, other: &Self) -> bool { - unsafe { - !(Key::operator_lt((self as *const Self), (other as *const Self))) - && !(Key::operator_lt((other as *const Self), (self as *const Self))) - } +impl LessThanComparableUnsafe for Key { + unsafe fn less(this: *const Self, o: *const Key) -> bool { + return (((*this).k) < ((*o).k)); } } -impl std::cmp::Eq for Key {} pub unsafe fn make_key_3(__return: *mut Key, mut k: i32) { (*std::cell::LazyCell::force_mut(&mut *&raw mut made_2)).prefix_inc(); Key::construct(__return, k); diff --git a/tests/unit/out/unsafe/map_key_identity.rs b/tests/unit/out/unsafe/map_key_identity.rs index 8d6e5b77e7..b311990fe3 100644 --- a/tests/unit/out/unsafe/map_key_identity.rs +++ b/tests/unit/out/unsafe/map_key_identity.rs @@ -23,35 +23,11 @@ pub struct Key { pub order: i32, pub identity: i32, } -impl std::cmp::Ord for Key { - fn cmp(&self, other: &Self) -> std::cmp::Ordering { - unsafe { - if operator_lt_1((self as *const Self), (other as *const Self)) { - std::cmp::Ordering::Less - } else if operator_lt_1((other as *const Self), (self as *const Self)) { - std::cmp::Ordering::Greater - } else { - std::cmp::Ordering::Equal - } - } - } -} -impl std::cmp::PartialOrd for Key { - fn partial_cmp(&self, other: &Self) -> Option { - Some(self.cmp(other)) +impl LessThanComparableUnsafe for Key { + unsafe fn less(a: *const Key, b: *const Key) -> bool { + return (((*a).order) < ((*b).order)); } } -impl std::cmp::PartialEq for Key { - fn eq(&self, other: &Self) -> bool { - unsafe { - WeaklyEqualityComparableWithUnsafe::equal((self as *const Self), (other as *const Self)) - } - } -} -impl std::cmp::Eq for Key {} -pub unsafe fn operator_lt_1(a: *const Key, b: *const Key) -> bool { - return (((*a).order) < ((*b).order)); -} #[repr(C)] #[derive(Copy, Clone, VaArg, FnPtrArg, ZeroInitializationUnsafe, TriviallyCopyableUnsafe)] pub struct Mapped { diff --git a/tests/unit/out/unsafe/max.rs b/tests/unit/out/unsafe/max.rs index 2a504984cd..167feb8bec 100644 --- a/tests/unit/out/unsafe/max.rs +++ b/tests/unit/out/unsafe/max.rs @@ -44,37 +44,12 @@ impl CopyConstructibleUnsafe for Bag { (*std::cell::LazyCell::force_mut(&mut *&raw mut calls_0)).prefix_inc(); } } -impl std::cmp::Ord for Bag { - fn cmp(&self, other: &Self) -> std::cmp::Ordering { - unsafe { - if operator_lt_4((self as *const Self), (other as *const Self)) { - std::cmp::Ordering::Less - } else if operator_lt_4((other as *const Self), (self as *const Self)) { - std::cmp::Ordering::Greater - } else { - std::cmp::Ordering::Equal - } - } - } -} -impl std::cmp::PartialOrd for Bag { - fn partial_cmp(&self, other: &Self) -> Option { - Some(self.cmp(other)) +impl LessThanComparableUnsafe for Bag { + unsafe fn less(a: *const Bag, b: *const Bag) -> bool { + return (((*a).n) < ((*b).n)); } } -impl std::cmp::PartialEq for Bag { - fn eq(&self, other: &Self) -> bool { - unsafe { - !(operator_lt_4((self as *const Self), (other as *const Self))) - && !(operator_lt_4((other as *const Self), (self as *const Self))) - } - } -} -impl std::cmp::Eq for Bag {} -pub unsafe fn operator_lt_4(a: *const Bag, b: *const Bag) -> bool { - return (((*a).n) < ((*b).n)); -} -pub unsafe fn make_5(__return: *mut Bag, mut n: i32) { +pub unsafe fn make_4(__return: *mut Bag, mut n: i32) { Bag::construct(__return, n); return; } @@ -94,22 +69,38 @@ unsafe fn main_0() -> i32 { let mut r1: i32 = (*{ let __a: *const _ = &mut x1; let __b: *const _ = &mut x2; - if *__a < *__b { __b } else { __a } + if LessThanComparableUnsafe::less(__a, __b) { + __b + } else { + __a + } }); let mut r2: i32 = (*{ let __a: *const _ = &mut x3; let __b: *const _ = &mut x4; - if *__b < *__a { __b } else { __a } + if LessThanComparableUnsafe::less(__b, __a) { + __b + } else { + __a + } }); let mut r3: i32 = (*{ let __a: *const _ = p1; let __b: *const _ = &mut x2; - if *__a < *__b { __b } else { __a } + if LessThanComparableUnsafe::less(__a, __b) { + __b + } else { + __a + } }); let mut r4: i32 = (*{ let __a: *const _ = p2; let __b: *const _ = &mut x3; - if *__b < *__a { __b } else { __a } + if LessThanComparableUnsafe::less(__b, __a) { + __b + } else { + __a + } }); let mut r5: i32 = { let mut __tmp_0: i32 = 30; @@ -117,7 +108,11 @@ unsafe fn main_0() -> i32 { (*{ let __a: *const _ = &mut __tmp_0; let __b: *const _ = &mut __tmp_1; - if *__a < *__b { __b } else { __a } + if LessThanComparableUnsafe::less(__a, __b) { + __b + } else { + __a + } }) }; assert!(((((((r1) + (r2)) + (r3)) + (r4)) + (r5)) == (56))); @@ -130,7 +125,10 @@ unsafe fn main_0() -> i32 { let mut __it = __tmp_2; __it.iter_inc(); while WeaklyEqualityComparableWithUnsafe::not_equal(&__it, &__tmp_3) { - if *__best.iter_deref() < *__it.iter_deref() { + if LessThanComparableUnsafe::less( + __best.iter_deref() as *const _, + __it.iter_deref() as *const _, + ) { __best = __it.clone(); } __it.iter_inc(); @@ -143,7 +141,11 @@ unsafe fn main_0() -> i32 { ((*{ let __a: *const _ = (unsafe { pick_3(0) }); let __b: *const _ = (unsafe { pick_3(1) }); - if *__a < *__b { __b } else { __a } + if LessThanComparableUnsafe::less(__a, __b) { + __b + } else { + __a + } }) == (2)) && ((*std::cell::LazyCell::force_mut(&mut *&raw mut calls_0)) == (2)) ); @@ -151,7 +153,11 @@ unsafe fn main_0() -> i32 { ((*{ let __a: *const _ = (unsafe { pick_3(1) }); let __b: *const _ = (unsafe { pick_3(0) }); - if *__b < *__a { __b } else { __a } + if LessThanComparableUnsafe::less(__b, __a) { + __b + } else { + __a + } }) == (1)) && ((*std::cell::LazyCell::force_mut(&mut *&raw mut calls_0)) == (4)) ); @@ -162,18 +168,22 @@ unsafe fn main_0() -> i32 { let mut __tmp_7: Bag = ::zero_init(); (((*match ( ({ - (unsafe { make_5(&raw mut __tmp_5, 1) }); + (unsafe { make_4(&raw mut __tmp_5, 1) }); &mut __tmp_5 } as *const Bag), ({ - (unsafe { make_5(&raw mut __tmp_7, 2) }); + (unsafe { make_4(&raw mut __tmp_7, 2) }); &mut __tmp_7 } as *const Bag), ) { (__tmp_4, __tmp_6) => { let __a: *const _ = __tmp_4; let __b: *const _ = __tmp_6; - if *__a < *__b { __b } else { __a } + if LessThanComparableUnsafe::less(__a, __b) { + __b + } else { + __a + } } }) .n) == (2)) @@ -182,18 +192,22 @@ unsafe fn main_0() -> i32 { let mut __tmp_11: Bag = ::zero_init(); (((*match ( ({ - (unsafe { make_5(&raw mut __tmp_9, 1) }); + (unsafe { make_4(&raw mut __tmp_9, 1) }); &mut __tmp_9 } as *const Bag), ({ - (unsafe { make_5(&raw mut __tmp_11, 2) }); + (unsafe { make_4(&raw mut __tmp_11, 2) }); &mut __tmp_11 } as *const Bag), ) { (__tmp_8, __tmp_10) => { let __a: *const _ = __tmp_8; let __b: *const _ = __tmp_10; - if *__b < *__a { __b } else { __a } + if LessThanComparableUnsafe::less(__b, __a) { + __b + } else { + __a + } } }) .n) == (1)) @@ -211,7 +225,11 @@ unsafe fn main_0() -> i32 { (__tmp_12,) => { let __a: *const _ = &mut __tmp_13; let __b: *const _ = __tmp_12; - if *__a < *__b { __b } else { __a } + if LessThanComparableUnsafe::less(__a, __b) { + __b + } else { + __a + } } }) }) as usize); diff --git a/tests/unit/out/unsafe/min_max_ptr2ptr.rs b/tests/unit/out/unsafe/min_max_ptr2ptr.rs index 621665a6bf..da37cf5a4b 100644 --- a/tests/unit/out/unsafe/min_max_ptr2ptr.rs +++ b/tests/unit/out/unsafe/min_max_ptr2ptr.rs @@ -22,12 +22,20 @@ unsafe fn main_0() -> i32 { let mut r1: i32 = (*{ let __a: *const _ = (*ppa); let __b: *const _ = (*ppb); - if *__a < *__b { __b } else { __a } + if LessThanComparableUnsafe::less(__a, __b) { + __b + } else { + __a + } }); let mut r2: i32 = (*{ let __a: *const _ = (*ppa); let __b: *const _ = (*ppb); - if *__b < *__a { __b } else { __a } + if LessThanComparableUnsafe::less(__b, __a) { + __b + } else { + __a + } }); assert!((((r1) + (r2)) == (30))); let mut values: [i32; 2] = [2, 1]; @@ -37,28 +45,44 @@ unsafe fn main_0() -> i32 { ((*{ let __a: *const _ = &mut low; let __b: *const _ = &mut high; - if *__b < *__a { __b } else { __a } + if LessThanComparableUnsafe::less(__b, __a) { + __b + } else { + __a + } }) == (low)) ); assert!( ((*{ let __a: *const _ = &mut high; let __b: *const _ = &mut low; - if *__b < *__a { __b } else { __a } + if LessThanComparableUnsafe::less(__b, __a) { + __b + } else { + __a + } }) == (low)) ); assert!( ((*{ let __a: *const _ = &mut low; let __b: *const _ = &mut high; - if *__a < *__b { __b } else { __a } + if LessThanComparableUnsafe::less(__a, __b) { + __b + } else { + __a + } }) == (high)) ); assert!( ((*{ let __a: *const _ = &mut high; let __b: *const _ = &mut low; - if *__a < *__b { __b } else { __a } + if LessThanComparableUnsafe::less(__a, __b) { + __b + } else { + __a + } }) == (high)) ); return 0; diff --git a/tests/unit/out/unsafe/operator_comparison_defaulted.rs b/tests/unit/out/unsafe/operator_comparison_defaulted.rs index 466114ab9f..d8bd31e8ea 100644 --- a/tests/unit/out/unsafe/operator_comparison_defaulted.rs +++ b/tests/unit/out/unsafe/operator_comparison_defaulted.rs @@ -31,47 +31,38 @@ pub struct Eq { TriviallyDefaultConstructibleUnsafe, TriviallyCopyableUnsafe, Default, + PartiallyOrderedWithUnsafe, )] +#[three_way(ThreeWayComparableUnsafe::three_way)] pub struct Cmp { pub a: i32, pub b: i32, } -impl Cmp { - pub unsafe fn operator_cmp(this: *const Self, _a0: *const Cmp) -> std::cmp::Ordering { +impl ThreeWayComparableUnsafe for Cmp { + type Category = libcc2rs::StrongOrdering; + unsafe fn three_way(this: *const Self, _a0: *const Cmp) -> libcc2rs::StrongOrdering { { - let mut cmp: std::cmp::Ordering = std::cmp::Ord::cmp(&((*this).a), &((*_a0).a)); - if !(cmp == std::cmp::Ordering::Equal) { + let mut cmp: libcc2rs::StrongOrdering = + libcc2rs::StrongOrdering::from(std::cmp::Ord::cmp(&((*this).a), &((*_a0).a))); + if !(libcc2rs::is_eq(cmp)) { return cmp; } } { - let mut cmp: std::cmp::Ordering = std::cmp::Ord::cmp(&((*this).b), &((*_a0).b)); - if !(cmp == std::cmp::Ordering::Equal) { + let mut cmp: libcc2rs::StrongOrdering = + libcc2rs::StrongOrdering::from(std::cmp::Ord::cmp(&((*this).b), &((*_a0).b))); + if !(libcc2rs::is_eq(cmp)) { return cmp; } } return libcc2rs::STRONG_ORDERING_EQUAL; } } -impl std::cmp::Ord for Cmp { - fn cmp(&self, other: &Self) -> std::cmp::Ordering { - unsafe { Cmp::operator_cmp((self as *const Self), (other as *const Self)) } +impl LessThanComparableUnsafe for Cmp { + unsafe fn less(this: *const Self, _a0: *const Self) -> bool { + libcc2rs::is_lt(ThreeWayComparableUnsafe::three_way(this, _a0)) } } -impl std::cmp::PartialOrd for Cmp { - fn partial_cmp(&self, other: &Self) -> Option { - Some(self.cmp(other)) - } -} -impl std::cmp::PartialEq for Cmp { - fn eq(&self, other: &Self) -> bool { - unsafe { - Cmp::operator_cmp((self as *const Self), (other as *const Self)) - == std::cmp::Ordering::Equal - } - } -} -impl std::cmp::Eq for Cmp {} #[repr(C)] #[derive( Copy, @@ -82,39 +73,30 @@ impl std::cmp::Eq for Cmp {} TriviallyDefaultConstructibleUnsafe, TriviallyCopyableUnsafe, Default, + PartiallyOrderedWithUnsafe, )] +#[three_way(ThreeWayComparableUnsafe::three_way)] pub struct Both { pub a: i32, } -impl Both { - pub unsafe fn operator_cmp(this: *const Self, _a0: *const Both) -> std::cmp::Ordering { +impl ThreeWayComparableUnsafe for Both { + type Category = libcc2rs::StrongOrdering; + unsafe fn three_way(this: *const Self, _a0: *const Both) -> libcc2rs::StrongOrdering { { - let mut cmp: std::cmp::Ordering = std::cmp::Ord::cmp(&((*this).a), &((*_a0).a)); - if !(cmp == std::cmp::Ordering::Equal) { + let mut cmp: libcc2rs::StrongOrdering = + libcc2rs::StrongOrdering::from(std::cmp::Ord::cmp(&((*this).a), &((*_a0).a))); + if !(libcc2rs::is_eq(cmp)) { return cmp; } } return libcc2rs::STRONG_ORDERING_EQUAL; } } -impl std::cmp::Ord for Both { - fn cmp(&self, other: &Self) -> std::cmp::Ordering { - unsafe { Both::operator_cmp((self as *const Self), (other as *const Self)) } - } -} -impl std::cmp::PartialOrd for Both { - fn partial_cmp(&self, other: &Self) -> Option { - Some(self.cmp(other)) - } -} -impl std::cmp::PartialEq for Both { - fn eq(&self, other: &Self) -> bool { - unsafe { - WeaklyEqualityComparableWithUnsafe::equal((self as *const Self), (other as *const Self)) - } +impl LessThanComparableUnsafe for Both { + unsafe fn less(this: *const Self, _a0: *const Self) -> bool { + libcc2rs::is_lt(ThreeWayComparableUnsafe::three_way(this, _a0)) } } -impl std::cmp::Eq for Both {} #[repr(C)] #[derive( Copy, @@ -125,40 +107,29 @@ impl std::cmp::Eq for Both {} TriviallyDefaultConstructibleUnsafe, TriviallyCopyableUnsafe, Default, + PartiallyOrderedWithUnsafe, )] +#[three_way(OrdOnly::operator_cmp)] pub struct OrdOnly { pub a: i32, } impl OrdOnly { - pub unsafe fn operator_cmp(this: *const Self, _a0: *const OrdOnly) -> std::cmp::Ordering { + pub unsafe fn operator_cmp(this: *const Self, _a0: *const OrdOnly) -> libcc2rs::StrongOrdering { { - let mut cmp: std::cmp::Ordering = std::cmp::Ord::cmp(&((*this).a), &((*_a0).a)); - if !(cmp == std::cmp::Ordering::Equal) { + let mut cmp: libcc2rs::StrongOrdering = + libcc2rs::StrongOrdering::from(std::cmp::Ord::cmp(&((*this).a), &((*_a0).a))); + if !(libcc2rs::is_eq(cmp)) { return cmp; } } return libcc2rs::STRONG_ORDERING_EQUAL; } } -impl std::cmp::Ord for OrdOnly { - fn cmp(&self, other: &Self) -> std::cmp::Ordering { - unsafe { OrdOnly::operator_cmp((self as *const Self), (other as *const Self)) } - } -} -impl std::cmp::PartialOrd for OrdOnly { - fn partial_cmp(&self, other: &Self) -> Option { - Some(self.cmp(other)) +impl LessThanComparableUnsafe for OrdOnly { + unsafe fn less(this: *const Self, _a0: *const Self) -> bool { + libcc2rs::is_lt(OrdOnly::operator_cmp(this, _a0)) } } -impl std::cmp::PartialEq for OrdOnly { - fn eq(&self, other: &Self) -> bool { - unsafe { - OrdOnly::operator_cmp((self as *const Self), (other as *const Self)) - == std::cmp::Ordering::Equal - } - } -} -impl std::cmp::Eq for OrdOnly {} #[repr(C)] #[derive( Copy, @@ -169,40 +140,30 @@ impl std::cmp::Eq for OrdOnly {} TriviallyDefaultConstructibleUnsafe, TriviallyCopyableUnsafe, Default, + PartiallyOrderedWithUnsafe, )] +#[three_way(ThreeWayComparableUnsafe::three_way)] pub struct Inner { pub x: i32, } -impl Inner { - pub unsafe fn operator_cmp(this: *const Self, _a0: *const Inner) -> std::cmp::Ordering { +impl ThreeWayComparableUnsafe for Inner { + type Category = libcc2rs::StrongOrdering; + unsafe fn three_way(this: *const Self, _a0: *const Inner) -> libcc2rs::StrongOrdering { { - let mut cmp: std::cmp::Ordering = std::cmp::Ord::cmp(&((*this).x), &((*_a0).x)); - if !(cmp == std::cmp::Ordering::Equal) { + let mut cmp: libcc2rs::StrongOrdering = + libcc2rs::StrongOrdering::from(std::cmp::Ord::cmp(&((*this).x), &((*_a0).x))); + if !(libcc2rs::is_eq(cmp)) { return cmp; } } return libcc2rs::STRONG_ORDERING_EQUAL; } } -impl std::cmp::Ord for Inner { - fn cmp(&self, other: &Self) -> std::cmp::Ordering { - unsafe { Inner::operator_cmp((self as *const Self), (other as *const Self)) } - } -} -impl std::cmp::PartialOrd for Inner { - fn partial_cmp(&self, other: &Self) -> Option { - Some(self.cmp(other)) - } -} -impl std::cmp::PartialEq for Inner { - fn eq(&self, other: &Self) -> bool { - unsafe { - Inner::operator_cmp((self as *const Self), (other as *const Self)) - == std::cmp::Ordering::Equal - } +impl LessThanComparableUnsafe for Inner { + unsafe fn less(this: *const Self, _a0: *const Self) -> bool { + libcc2rs::is_lt(ThreeWayComparableUnsafe::three_way(this, _a0)) } } -impl std::cmp::Eq for Inner {} #[repr(C)] #[derive( Copy, @@ -213,50 +174,40 @@ impl std::cmp::Eq for Inner {} TriviallyDefaultConstructibleUnsafe, TriviallyCopyableUnsafe, Default, + PartiallyOrderedWithUnsafe, )] +#[three_way(ThreeWayComparableUnsafe::three_way)] pub struct Outer { pub i: Inner, pub y: i32, } -impl Outer { - pub unsafe fn operator_cmp(this: *const Self, _a0: *const Outer) -> std::cmp::Ordering { +impl ThreeWayComparableUnsafe for Outer { + type Category = libcc2rs::StrongOrdering; + unsafe fn three_way(this: *const Self, _a0: *const Outer) -> libcc2rs::StrongOrdering { { - let mut cmp: std::cmp::Ordering = (unsafe { + let mut cmp: libcc2rs::StrongOrdering = (unsafe { let _arg0: *const Inner = &(*_a0).i; - Inner::operator_cmp(&raw const (*this).i, _arg0) + ThreeWayComparableUnsafe::three_way(&raw const (*this).i, _arg0) }); - if !(cmp == std::cmp::Ordering::Equal) { + if !(libcc2rs::is_eq(cmp)) { return cmp; } } { - let mut cmp: std::cmp::Ordering = std::cmp::Ord::cmp(&((*this).y), &((*_a0).y)); - if !(cmp == std::cmp::Ordering::Equal) { + let mut cmp: libcc2rs::StrongOrdering = + libcc2rs::StrongOrdering::from(std::cmp::Ord::cmp(&((*this).y), &((*_a0).y))); + if !(libcc2rs::is_eq(cmp)) { return cmp; } } return libcc2rs::STRONG_ORDERING_EQUAL; } } -impl std::cmp::Ord for Outer { - fn cmp(&self, other: &Self) -> std::cmp::Ordering { - unsafe { Outer::operator_cmp((self as *const Self), (other as *const Self)) } - } -} -impl std::cmp::PartialOrd for Outer { - fn partial_cmp(&self, other: &Self) -> Option { - Some(self.cmp(other)) - } -} -impl std::cmp::PartialEq for Outer { - fn eq(&self, other: &Self) -> bool { - unsafe { - Outer::operator_cmp((self as *const Self), (other as *const Self)) - == std::cmp::Ordering::Equal - } +impl LessThanComparableUnsafe for Outer { + unsafe fn less(this: *const Self, _a0: *const Self) -> bool { + libcc2rs::is_lt(ThreeWayComparableUnsafe::three_way(this, _a0)) } } -impl std::cmp::Eq for Outer {} #[repr(C)] #[derive( Copy, @@ -271,43 +222,26 @@ impl std::cmp::Eq for Outer {} pub struct Secondary { pub a: i32, } -impl Secondary { - pub unsafe fn operator_cmp(this: *const Self, _a0: *const Secondary) -> std::cmp::Ordering { +impl ThreeWayComparableUnsafe for Secondary { + type Category = libcc2rs::StrongOrdering; + unsafe fn three_way(this: *const Self, _a0: *const Secondary) -> libcc2rs::StrongOrdering { { - let mut cmp: std::cmp::Ordering = std::cmp::Ord::cmp(&((*this).a), &((*_a0).a)); - if !(cmp == std::cmp::Ordering::Equal) { + let mut cmp: libcc2rs::StrongOrdering = + libcc2rs::StrongOrdering::from(std::cmp::Ord::cmp(&((*this).a), &((*_a0).a))); + if !(libcc2rs::is_eq(cmp)) { return cmp; } } return libcc2rs::STRONG_ORDERING_EQUAL; } - pub unsafe fn operator_lt(this: *const Self, _a0: *const Secondary) -> bool { - return (unsafe { Secondary::operator_cmp(&raw const (*this), _a0) }) - == std::cmp::Ordering::Less; - } - pub unsafe fn operator_ge(this: *const Self, _a0: *const Secondary) -> bool { - return (unsafe { Secondary::operator_cmp(&raw const (*this), _a0) }) - != std::cmp::Ordering::Less; - } -} -impl std::cmp::Ord for Secondary { - fn cmp(&self, other: &Self) -> std::cmp::Ordering { - unsafe { Secondary::operator_cmp((self as *const Self), (other as *const Self)) } - } -} -impl std::cmp::PartialOrd for Secondary { - fn partial_cmp(&self, other: &Self) -> Option { - Some(self.cmp(other)) - } } -impl std::cmp::PartialEq for Secondary { - fn eq(&self, other: &Self) -> bool { - unsafe { - WeaklyEqualityComparableWithUnsafe::equal((self as *const Self), (other as *const Self)) - } +impl LessThanComparableUnsafe for Secondary { + unsafe fn less(this: *const Self, _a0: *const Secondary) -> bool { + return libcc2rs::is_lt( + (unsafe { ThreeWayComparableUnsafe::three_way(&raw const (*this), _a0) }), + ); } } -impl std::cmp::Eq for Secondary {} #[repr(C)] #[derive( Copy, @@ -318,40 +252,30 @@ impl std::cmp::Eq for Secondary {} TriviallyDefaultConstructibleUnsafe, TriviallyCopyableUnsafe, Default, + PartiallyOrderedWithUnsafe, )] +#[three_way(ThreeWayComparableUnsafe::three_way)] pub struct PtrMember { pub p: *mut i32, } -impl PtrMember { - pub unsafe fn operator_cmp(this: *const Self, _a0: *const PtrMember) -> std::cmp::Ordering { +impl ThreeWayComparableUnsafe for PtrMember { + type Category = libcc2rs::StrongOrdering; + unsafe fn three_way(this: *const Self, _a0: *const PtrMember) -> libcc2rs::StrongOrdering { { - let mut cmp: std::cmp::Ordering = std::cmp::Ord::cmp(&((*this).p), &((*_a0).p)); - if !(cmp == std::cmp::Ordering::Equal) { + let mut cmp: libcc2rs::StrongOrdering = + libcc2rs::StrongOrdering::from(std::cmp::Ord::cmp(&((*this).p), &((*_a0).p))); + if !(libcc2rs::is_eq(cmp)) { return cmp; } } return libcc2rs::STRONG_ORDERING_EQUAL; } } -impl std::cmp::Ord for PtrMember { - fn cmp(&self, other: &Self) -> std::cmp::Ordering { - unsafe { PtrMember::operator_cmp((self as *const Self), (other as *const Self)) } - } -} -impl std::cmp::PartialOrd for PtrMember { - fn partial_cmp(&self, other: &Self) -> Option { - Some(self.cmp(other)) - } -} -impl std::cmp::PartialEq for PtrMember { - fn eq(&self, other: &Self) -> bool { - unsafe { - PtrMember::operator_cmp((self as *const Self), (other as *const Self)) - == std::cmp::Ordering::Equal - } +impl LessThanComparableUnsafe for PtrMember { + unsafe fn less(this: *const Self, _a0: *const Self) -> bool { + libcc2rs::is_lt(ThreeWayComparableUnsafe::three_way(this, _a0)) } } -impl std::cmp::Eq for PtrMember {} pub fn main() { unsafe { __cpp2rust_init_globals(); @@ -368,18 +292,32 @@ unsafe fn main_0() -> i32 { let mut c2: Cmp = Cmp { a: 1, b: 3 }; let mut c3: Cmp = Cmp { a: 2, b: 0 }; let mut c4: Cmp = Cmp { a: 1, b: 9 }; - assert!((unsafe { Cmp::operator_cmp(&raw const c1, &c2,) }) == std::cmp::Ordering::Less); - assert!((unsafe { Cmp::operator_cmp(&raw const c3, &c4,) }) == std::cmp::Ordering::Greater); + assert!(libcc2rs::is_lt( + (unsafe { ThreeWayComparableUnsafe::three_way(&raw const c1, &c2,) }) + )); + assert!(libcc2rs::is_gt( + (unsafe { ThreeWayComparableUnsafe::three_way(&raw const c3, &c4,) }) + )); assert!( (unsafe { let _arg0: *const Cmp = &c1; WeaklyEqualityComparableWithUnsafe::equal(&raw const c1, _arg0) }) ); - assert!((unsafe { Cmp::operator_cmp(&raw const c1, &c2,) }) == libcc2rs::STRONG_ORDERING_LESS); + assert!({ + let __a = libcc2rs::PartialOrdering::from( + (unsafe { ThreeWayComparableUnsafe::three_way(&raw const c1, &c2) }), + ); + let __b = libcc2rs::PartialOrdering::from(libcc2rs::STRONG_ORDERING_LESS); + libcc2rs::is_lt(__a) == libcc2rs::is_lt(__b) + && libcc2rs::is_eq(__a) == libcc2rs::is_eq(__b) + && libcc2rs::is_gt(__a) == libcc2rs::is_gt(__b) + }); let mut b1: Both = Both { a: 1 }; let mut b2: Both = Both { a: 2 }; - assert!((unsafe { Both::operator_cmp(&raw const b1, &b2,) }) == std::cmp::Ordering::Less); + assert!(libcc2rs::is_lt( + (unsafe { ThreeWayComparableUnsafe::three_way(&raw const b1, &b2,) }) + )); assert!( (unsafe { let _arg0: *const Both = &b2; @@ -388,11 +326,17 @@ unsafe fn main_0() -> i32 { ); let mut o1: OrdOnly = OrdOnly { a: 1 }; let mut o2: OrdOnly = OrdOnly { a: 2 }; - assert!((unsafe { OrdOnly::operator_cmp(&raw const o1, &o2,) }) == std::cmp::Ordering::Less); - assert!( - (unsafe { OrdOnly::operator_cmp(&raw const o2, &o1,) }) - == libcc2rs::STRONG_ORDERING_GREATER - ); + assert!(libcc2rs::is_lt( + (unsafe { OrdOnly::operator_cmp(&raw const o1, &o2,) }) + )); + assert!({ + let __a = + libcc2rs::PartialOrdering::from((unsafe { OrdOnly::operator_cmp(&raw const o2, &o1) })); + let __b = libcc2rs::PartialOrdering::from(libcc2rs::STRONG_ORDERING_GREATER); + libcc2rs::is_lt(__a) == libcc2rs::is_lt(__b) + && libcc2rs::is_eq(__a) == libcc2rs::is_eq(__b) + && libcc2rs::is_gt(__a) == libcc2rs::is_gt(__b) + }); let mut x1: Outer = Outer { i: Inner { x: 1 }, y: 9, @@ -405,17 +349,25 @@ unsafe fn main_0() -> i32 { i: Inner { x: 1 }, y: 9, }; - assert!((unsafe { Outer::operator_cmp(&raw const x1, &x2,) }) == std::cmp::Ordering::Less); + assert!(libcc2rs::is_lt( + (unsafe { ThreeWayComparableUnsafe::three_way(&raw const x1, &x2,) }) + )); assert!((unsafe { WeaklyEqualityComparableWithUnsafe::equal(&raw const x1, &x3,) })); - assert!( - (unsafe { Outer::operator_cmp(&raw const x2, &x1,) }) == libcc2rs::STRONG_ORDERING_GREATER - ); + assert!({ + let __a = libcc2rs::PartialOrdering::from( + (unsafe { ThreeWayComparableUnsafe::three_way(&raw const x2, &x1) }), + ); + let __b = libcc2rs::PartialOrdering::from(libcc2rs::STRONG_ORDERING_GREATER); + libcc2rs::is_lt(__a) == libcc2rs::is_lt(__b) + && libcc2rs::is_eq(__a) == libcc2rs::is_eq(__b) + && libcc2rs::is_gt(__a) == libcc2rs::is_gt(__b) + }); let mut s1: Secondary = Secondary { a: 1 }; let mut s2: Secondary = Secondary { a: 2 }; assert!((unsafe { WeaklyEqualityComparableWithUnsafe::not_equal(&raw const s1, &s2,) })); - assert!((unsafe { Secondary::operator_lt(&raw const s1, &s2,) })); - assert!((unsafe { Secondary::operator_ge(&raw const s2, &s1,) })); - assert!(!(unsafe { Secondary::operator_lt(&raw const s2, &s1,) })); + assert!((unsafe { LessThanComparableUnsafe::less(&raw const s1, &s2,) })); + assert!((unsafe { PartiallyOrderedWithUnsafe::greater_equal(&raw const s2, &s1,) })); + assert!(!(unsafe { LessThanComparableUnsafe::less(&raw const s2, &s1,) })); let mut arr: [i32; 2] = [0, 0]; let mut p1: PtrMember = PtrMember { p: arr.as_mut_ptr(), @@ -426,14 +378,37 @@ unsafe fn main_0() -> i32 { let mut p3: PtrMember = PtrMember { p: arr.as_mut_ptr(), }; - assert!((unsafe { PtrMember::operator_cmp(&raw const p1, &p2,) }) == std::cmp::Ordering::Less); + assert!(libcc2rs::is_lt( + (unsafe { ThreeWayComparableUnsafe::three_way(&raw const p1, &p2,) }) + )); assert!((unsafe { WeaklyEqualityComparableWithUnsafe::equal(&raw const p1, &p3,) })); - assert!( - (unsafe { PtrMember::operator_cmp(&raw const p2, &p1,) }) - == libcc2rs::STRONG_ORDERING_GREATER - ); + assert!({ + let __a = libcc2rs::PartialOrdering::from( + (unsafe { ThreeWayComparableUnsafe::three_way(&raw const p2, &p1) }), + ); + let __b = libcc2rs::PartialOrdering::from(libcc2rs::STRONG_ORDERING_GREATER); + libcc2rs::is_lt(__a) == libcc2rs::is_lt(__b) + && libcc2rs::is_eq(__a) == libcc2rs::is_eq(__b) + && libcc2rs::is_gt(__a) == libcc2rs::is_gt(__b) + }); return 0; } +impl PartiallyOrderedWithUnsafe for Secondary { + unsafe fn greater_equal(this: *const Self, _a0: *const Secondary) -> bool { + return libcc2rs::is_gteq( + (unsafe { ThreeWayComparableUnsafe::three_way(&raw const (*this), _a0) }), + ); + } + unsafe fn less(lhs: *const Self, rhs: *const Self) -> bool { + ::less(lhs, rhs) + } + unsafe fn greater(lhs: *const Self, rhs: *const Self) -> bool { + libcc2rs::is_gt(ThreeWayComparableUnsafe::three_way(lhs, rhs)) + } + unsafe fn less_equal(lhs: *const Self, rhs: *const Self) -> bool { + libcc2rs::is_lteq(ThreeWayComparableUnsafe::three_way(lhs, rhs)) + } +} impl WeaklyEqualityComparableWithUnsafe for Both { unsafe fn equal(this: *const Self, _a0: *const Both) -> bool { return (((*this).a) == ((*_a0).a)); diff --git a/tests/unit/out/unsafe/operator_comparison_free.rs b/tests/unit/out/unsafe/operator_comparison_free.rs index 17fad6efeb..1d3c92e5d4 100644 --- a/tests/unit/out/unsafe/operator_comparison_free.rs +++ b/tests/unit/out/unsafe/operator_comparison_free.rs @@ -20,48 +20,15 @@ use std::rc::Rc; pub struct S { pub v: i32, } -impl std::cmp::Ord for S { - fn cmp(&self, other: &Self) -> std::cmp::Ordering { - unsafe { - if operator_lt_0((self as *const Self), (other as *const Self)) { - std::cmp::Ordering::Less - } else if operator_lt_0((other as *const Self), (self as *const Self)) { - std::cmp::Ordering::Greater - } else { - std::cmp::Ordering::Equal - } - } +impl LessThanComparableUnsafe for S { + unsafe fn less(a: *const S, b: *const S) -> bool { + return (((*a).v) < ((*b).v)); } } -impl std::cmp::PartialOrd for S { - fn partial_cmp(&self, other: &Self) -> Option { - Some(self.cmp(other)) - } -} -impl std::cmp::PartialEq for S { - fn eq(&self, other: &Self) -> bool { - unsafe { - WeaklyEqualityComparableWithUnsafe::equal((self as *const Self), (other as *const Self)) - } - } -} -impl std::cmp::Eq for S {} -pub unsafe fn operator_lt_0(a: *const S, b: *const S) -> bool { - return (((*a).v) < ((*b).v)); -} -pub unsafe fn operator_gt_1(a: *const S, b: *const S) -> bool { - return (((*a).v) > ((*b).v)); -} -pub unsafe fn operator_le_2(a: *const S, b: *const S) -> bool { - return (((*a).v) <= ((*b).v)); -} -pub unsafe fn operator_ge_3(a: *const S, b: *const S) -> bool { - return (((*a).v) >= ((*b).v)); -} -pub unsafe fn operator_lt_4(a: *const S, mut b: i32) -> bool { +pub unsafe fn operator_lt_0(a: *const S, mut b: i32) -> bool { return (((*a).v) < (b)); } -pub unsafe fn operator_lt_5(mut a: i32, b: *const S) -> bool { +pub unsafe fn operator_lt_1(mut a: i32, b: *const S) -> bool { return ((a) < ((*b).v)); } #[repr(C)] @@ -78,51 +45,21 @@ pub unsafe fn operator_lt_5(mut a: i32, b: *const S) -> bool { pub struct V { pub v: i32, } -impl std::cmp::Ord for V { - fn cmp(&self, other: &Self) -> std::cmp::Ordering { - unsafe { - if operator_lt_7( - (*(self as *const Self)).clone(), - (*(other as *const Self)).clone(), - ) { - std::cmp::Ordering::Less - } else if operator_lt_7( - (*(other as *const Self)).clone(), - (*(self as *const Self)).clone(), - ) { - std::cmp::Ordering::Greater - } else { - std::cmp::Ordering::Equal - } - } +impl LessThanComparableUnsafe for V { + unsafe fn less(a: *const Self, b: *const Self) -> bool { + operator_lt_3((*a).clone(), (*b).clone()) } } -impl std::cmp::PartialOrd for V { - fn partial_cmp(&self, other: &Self) -> Option { - Some(self.cmp(other)) - } -} -impl std::cmp::PartialEq for V { - fn eq(&self, other: &Self) -> bool { - unsafe { - operator_eq_8( - (*(self as *const Self)).clone(), - (*(other as *const Self)).clone(), - ) - } - } -} -impl std::cmp::Eq for V {} -pub unsafe fn operator_eq_8(mut a: V, mut b: V) -> bool { +pub unsafe fn operator_eq_4(mut a: V, mut b: V) -> bool { return ((a.v) == (b.v)); } -pub unsafe fn operator_ne_6(mut a: V, mut b: V) -> bool { +pub unsafe fn operator_ne_2(mut a: V, mut b: V) -> bool { return ((a.v) != (b.v)); } -pub unsafe fn operator_lt_7(mut a: V, mut b: V) -> bool { +pub unsafe fn operator_lt_3(mut a: V, mut b: V) -> bool { return ((a.v) < (b.v)); } -pub unsafe fn operator_gt_9(mut a: V, mut b: V) -> bool { +pub unsafe fn operator_gt_5(mut a: V, mut b: V) -> bool { return ((a.v) > (b.v)); } pub fn main() { @@ -138,31 +75,31 @@ unsafe fn main_0() -> i32 { assert!( (unsafe { let _a: V = x; - operator_eq_8(_a, z) + operator_eq_4(_a, z) }) ); assert!( (unsafe { let _a: V = x; - operator_ne_6(_a, y) + operator_ne_2(_a, y) }) ); assert!( (unsafe { let _a: V = x; - operator_lt_7(_a, y) + operator_lt_3(_a, y) }) ); assert!( (unsafe { let _a: V = y; - operator_gt_9(_a, x) + operator_gt_5(_a, x) }) ); assert!( !(unsafe { let _a: V = y; - operator_lt_7(_a, x) + operator_lt_3(_a, x) }) ); let mut a: S = S { v: 1 }; @@ -183,42 +120,56 @@ unsafe fn main_0() -> i32 { assert!( (unsafe { let _a: *const S = &a; - operator_lt_0(_a, &b) + LessThanComparableUnsafe::less(_a, &b) }) ); assert!( (unsafe { let _a: *const S = &b; - operator_gt_1(_a, &a) + PartiallyOrderedWithUnsafe::greater(_a, &a) }) ); assert!( (unsafe { let _a: *const S = &a; - operator_le_2(_a, &c) + PartiallyOrderedWithUnsafe::less_equal(_a, &c) }) ); assert!( (unsafe { let _a: *const S = &a; - operator_ge_3(_a, &c) + PartiallyOrderedWithUnsafe::greater_equal(_a, &c) }) ); assert!( !(unsafe { let _a: *const S = &b; - operator_lt_0(_a, &a) + LessThanComparableUnsafe::less(_a, &a) }) ); assert!( (unsafe { let _a: *const S = &a; - operator_lt_4(_a, 5) + operator_lt_0(_a, 5) }) ); - assert!((unsafe { operator_lt_5(0, &a,) })); + assert!((unsafe { operator_lt_1(0, &a,) })); return 0; } +impl PartiallyOrderedWithUnsafe for S { + unsafe fn less(lhs: *const Self, rhs: *const Self) -> bool { + ::less(lhs, rhs) + } + unsafe fn greater(a: *const S, b: *const S) -> bool { + return (((*a).v) > ((*b).v)); + } + unsafe fn less_equal(a: *const S, b: *const S) -> bool { + return (((*a).v) <= ((*b).v)); + } + unsafe fn greater_equal(a: *const S, b: *const S) -> bool { + return (((*a).v) >= ((*b).v)); + } +} impl WeaklyEqualityComparableWithUnsafe for S { unsafe fn equal(a: *const S, b: *const S) -> bool { return (((*a).v) == ((*b).v)); @@ -229,10 +180,10 @@ impl WeaklyEqualityComparableWithUnsafe for S { } impl WeaklyEqualityComparableWithUnsafe for V { unsafe fn not_equal(a: *const Self, b: *const Self) -> bool { - operator_ne_6((*a).clone(), (*b).clone()) + operator_ne_2((*a).clone(), (*b).clone()) } unsafe fn equal(a: *const Self, b: *const Self) -> bool { - operator_eq_8((*a).clone(), (*b).clone()) + operator_eq_4((*a).clone(), (*b).clone()) } } pub unsafe fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/unsafe/operator_comparison_member.rs b/tests/unit/out/unsafe/operator_comparison_member.rs index 7ce9e42ed9..0f0c0299c7 100644 --- a/tests/unit/out/unsafe/operator_comparison_member.rs +++ b/tests/unit/out/unsafe/operator_comparison_member.rs @@ -21,48 +21,15 @@ pub struct S { pub v: i32, } impl S { - pub unsafe fn operator_lt_3(this: *const Self, o: *const S) -> bool { - return (((*this).v) < ((*o).v)); - } - pub unsafe fn operator_gt(this: *const Self, o: *const S) -> bool { - return (((*this).v) > ((*o).v)); - } - pub unsafe fn operator_le(this: *const Self, o: *const S) -> bool { - return (((*this).v) <= ((*o).v)); - } - pub unsafe fn operator_ge(this: *const Self, o: *const S) -> bool { - return (((*this).v) >= ((*o).v)); - } pub unsafe fn operator_lt_7(this: *const Self, mut o: i32) -> bool { return (((*this).v) < (o)); } } -impl std::cmp::Ord for S { - fn cmp(&self, other: &Self) -> std::cmp::Ordering { - unsafe { - if S::operator_lt_3((self as *const Self), (other as *const Self)) { - std::cmp::Ordering::Less - } else if S::operator_lt_3((other as *const Self), (self as *const Self)) { - std::cmp::Ordering::Greater - } else { - std::cmp::Ordering::Equal - } - } - } -} -impl std::cmp::PartialOrd for S { - fn partial_cmp(&self, other: &Self) -> Option { - Some(self.cmp(other)) - } -} -impl std::cmp::PartialEq for S { - fn eq(&self, other: &Self) -> bool { - unsafe { - WeaklyEqualityComparableWithUnsafe::equal((self as *const Self), (other as *const Self)) - } +impl LessThanComparableUnsafe for S { + unsafe fn less(this: *const Self, o: *const S) -> bool { + return (((*this).v) < ((*o).v)); } } -impl std::cmp::Eq for S {} pub fn main() { unsafe { __cpp2rust_init_globals(); @@ -75,14 +42,28 @@ unsafe fn main_0() -> i32 { let mut c: S = S { v: 1 }; assert!((unsafe { WeaklyEqualityComparableWithUnsafe::equal(&raw const a, &c,) })); assert!((unsafe { WeaklyEqualityComparableWithUnsafe::not_equal(&raw const a, &b,) })); - assert!((unsafe { S::operator_lt_3(&raw const a, &b,) })); - assert!((unsafe { S::operator_gt(&raw const b, &a,) })); - assert!((unsafe { S::operator_le(&raw const a, &c,) })); - assert!((unsafe { S::operator_ge(&raw const a, &c,) })); - assert!(!(unsafe { S::operator_lt_3(&raw const b, &a,) })); + assert!((unsafe { LessThanComparableUnsafe::less(&raw const a, &b,) })); + assert!((unsafe { PartiallyOrderedWithUnsafe::greater(&raw const b, &a,) })); + assert!((unsafe { PartiallyOrderedWithUnsafe::less_equal(&raw const a, &c,) })); + assert!((unsafe { PartiallyOrderedWithUnsafe::greater_equal(&raw const a, &c,) })); + assert!(!(unsafe { LessThanComparableUnsafe::less(&raw const b, &a,) })); assert!((unsafe { S::operator_lt_7(&raw const a, 5,) })); return 0; } +impl PartiallyOrderedWithUnsafe for S { + unsafe fn greater(this: *const Self, o: *const S) -> bool { + return (((*this).v) > ((*o).v)); + } + unsafe fn less_equal(this: *const Self, o: *const S) -> bool { + return (((*this).v) <= ((*o).v)); + } + unsafe fn greater_equal(this: *const Self, o: *const S) -> bool { + return (((*this).v) >= ((*o).v)); + } + unsafe fn less(lhs: *const Self, rhs: *const Self) -> bool { + ::less(lhs, rhs) + } +} impl WeaklyEqualityComparableWithUnsafe for S { unsafe fn equal(this: *const Self, o: *const S) -> bool { return (((*this).v) == ((*o).v)); diff --git a/tests/unit/out/unsafe/operator_comparison_nonbool.rs b/tests/unit/out/unsafe/operator_comparison_nonbool.rs index 215a3e07c6..197e3a3414 100644 --- a/tests/unit/out/unsafe/operator_comparison_nonbool.rs +++ b/tests/unit/out/unsafe/operator_comparison_nonbool.rs @@ -20,59 +20,27 @@ use std::rc::Rc; pub struct X { pub v: i32, } -impl std::cmp::Ord for X { - fn cmp(&self, other: &Self) -> std::cmp::Ordering { - unsafe { - if ((operator_lt_1( - (*(self as *const Self)).clone(), - (*(other as *const Self)).clone(), - )) != 0) - { - std::cmp::Ordering::Less - } else if ((operator_lt_1( - (*(other as *const Self)).clone(), - (*(self as *const Self)).clone(), - )) != 0) - { - std::cmp::Ordering::Greater - } else { - std::cmp::Ordering::Equal - } - } +impl LessThanComparableUnsafe for X { + unsafe fn less(a: *const Self, b: *const Self) -> bool { + ((operator_lt_4((*a).clone(), (*b).clone())) != 0) } } -impl std::cmp::PartialOrd for X { - fn partial_cmp(&self, other: &Self) -> Option { - Some(self.cmp(other)) - } -} -impl std::cmp::PartialEq for X { - fn eq(&self, other: &Self) -> bool { - unsafe { - ((operator_eq_2( - (*(self as *const Self)).clone(), - (*(other as *const Self)).clone(), - )) != 0) - } - } -} -impl std::cmp::Eq for X {} -pub unsafe fn operator_eq_2(mut a: X, mut b: X) -> i32 { +pub unsafe fn operator_eq_5(mut a: X, mut b: X) -> i32 { return if ((a.v) == (b.v)) { 2 } else { 0 }; } pub unsafe fn operator_ne_0(mut a: X, mut b: X) -> i32 { return if ((a.v) != (b.v)) { 3 } else { 0 }; } -pub unsafe fn operator_lt_1(mut a: X, mut b: X) -> i32 { +pub unsafe fn operator_lt_4(mut a: X, mut b: X) -> i32 { return if ((a.v) < (b.v)) { 4 } else { 0 }; } -pub unsafe fn operator_gt_3(mut a: X, mut b: X) -> i32 { +pub unsafe fn operator_gt_1(mut a: X, mut b: X) -> i32 { return if ((a.v) > (b.v)) { 5 } else { 0 }; } -pub unsafe fn operator_le_4(mut a: X, mut b: X) -> i32 { +pub unsafe fn operator_le_2(mut a: X, mut b: X) -> i32 { return if ((a.v) <= (b.v)) { 6 } else { 0 }; } -pub unsafe fn operator_ge_5(mut a: X, mut b: X) -> i32 { +pub unsafe fn operator_ge_3(mut a: X, mut b: X) -> i32 { return if ((a.v) >= (b.v)) { 7 } else { 0 }; } #[repr(C)] @@ -137,30 +105,11 @@ impl Mixed { return if (((*this).v) == ((*o).v)) { 9 } else { 0 }; } } -impl std::cmp::Ord for Mixed { - fn cmp(&self, other: &Self) -> std::cmp::Ordering { - unsafe { - if ((Mixed::operator_lt((self as *const Self), (other as *const Self))) != 0) { - std::cmp::Ordering::Less - } else if ((Mixed::operator_lt((other as *const Self), (self as *const Self))) != 0) { - std::cmp::Ordering::Greater - } else { - std::cmp::Ordering::Equal - } - } - } -} -impl std::cmp::PartialOrd for Mixed { - fn partial_cmp(&self, other: &Self) -> Option { - Some(self.cmp(other)) - } -} -impl std::cmp::PartialEq for Mixed { - fn eq(&self, other: &Self) -> bool { - unsafe { ((Mixed::operator_eq((self as *const Self), (other as *const Self))) != 0) } +impl LessThanComparableUnsafe for Mixed { + unsafe fn less(this: *const Self, o: *const Self) -> bool { + ((Mixed::operator_lt(this, o)) != 0) } } -impl std::cmp::Eq for Mixed {} #[repr(C)] #[derive( Copy, @@ -199,50 +148,14 @@ impl MyBool { pub struct Boolish { pub v: i32, } -impl std::cmp::Ord for Boolish { - fn cmp(&self, other: &Self) -> std::cmp::Ordering { - unsafe { - if { - let __cmp = operator_lt_6( - (*(self as *const Self)).clone(), - (*(other as *const Self)).clone(), - ); - MyBool::to_bool((&raw const __cmp)) - } { - std::cmp::Ordering::Less - } else if { - let __cmp = operator_lt_6( - (*(other as *const Self)).clone(), - (*(self as *const Self)).clone(), - ); - MyBool::to_bool((&raw const __cmp)) - } { - std::cmp::Ordering::Greater - } else { - std::cmp::Ordering::Equal - } - } - } -} -impl std::cmp::PartialOrd for Boolish { - fn partial_cmp(&self, other: &Self) -> Option { - Some(self.cmp(other)) - } -} -impl std::cmp::PartialEq for Boolish { - fn eq(&self, other: &Self) -> bool { - unsafe { - { - let __cmp = operator_eq_7( - (*(self as *const Self)).clone(), - (*(other as *const Self)).clone(), - ); - MyBool::to_bool((&raw const __cmp)) - } +impl LessThanComparableUnsafe for Boolish { + unsafe fn less(a: *const Self, b: *const Self) -> bool { + { + let __cmp = operator_lt_6((*a).clone(), (*b).clone()); + MyBool::to_bool((&raw const __cmp)) } } } -impl std::cmp::Eq for Boolish {} pub unsafe fn operator_eq_7(mut a: Boolish, mut b: Boolish) -> MyBool { return { let mut __v: MyBool = ::zero_init(); @@ -319,13 +232,13 @@ unsafe fn main_0() -> i32 { assert!( ((unsafe { let _a: X = a; - operator_eq_2(_a, c) + operator_eq_5(_a, c) }) == (2)) ); assert!( ((unsafe { let _a: X = a; - operator_eq_2(_a, b) + operator_eq_5(_a, b) }) == (0)) ); assert!( @@ -337,25 +250,25 @@ unsafe fn main_0() -> i32 { assert!( ((unsafe { let _a: X = a; - operator_lt_1(_a, b) + operator_lt_4(_a, b) }) == (4)) ); assert!( ((unsafe { let _a: X = b; - operator_gt_3(_a, a) + operator_gt_1(_a, a) }) == (5)) ); assert!( ((unsafe { let _a: X = a; - operator_le_4(_a, c) + operator_le_2(_a, c) }) == (6)) ); assert!( ((unsafe { let _a: X = a; - operator_ge_5(_a, c) + operator_ge_3(_a, c) }) == (7)) ); let mut xs: [X; 3] = [X { v: 3 }, X { v: 1 }, X { v: 2 }]; @@ -363,7 +276,17 @@ unsafe fn main_0() -> i32 { let mut __tmp_0 = { xs.as_mut_ptr() }; let __n = xs.as_mut_ptr().offset((3) as isize).iter_distance(&__tmp_0); let mut __order: Vec = (0..__n).collect(); - __order.sort_by(|&__x, &__y| (*__tmp_0.iter_at(__x)).cmp(&*__tmp_0.iter_at(__y))); + __order.sort_by(|&__x, &__y| { + let __a = __tmp_0.iter_at(__x) as *const _; + let __b = __tmp_0.iter_at(__y) as *const _; + if LessThanComparableUnsafe::less(__a, __b) { + std::cmp::Ordering::Less + } else if LessThanComparableUnsafe::less(__b, __a) { + std::cmp::Ordering::Greater + } else { + std::cmp::Ordering::Equal + } + }); let __values: Vec<_> = __order .iter() .map(|&__i| std::ptr::read(__tmp_0.iter_at(__i))) @@ -389,7 +312,17 @@ unsafe fn main_0() -> i32 { let mut __tmp_1 = { ms.as_mut_ptr() }; let __n = ms.as_mut_ptr().offset((3) as isize).iter_distance(&__tmp_1); let mut __order: Vec = (0..__n).collect(); - __order.sort_by(|&__x, &__y| (*__tmp_1.iter_at(__x)).cmp(&*__tmp_1.iter_at(__y))); + __order.sort_by(|&__x, &__y| { + let __a = __tmp_1.iter_at(__x) as *const _; + let __b = __tmp_1.iter_at(__y) as *const _; + if LessThanComparableUnsafe::less(__a, __b) { + std::cmp::Ordering::Less + } else if LessThanComparableUnsafe::less(__b, __a) { + std::cmp::Ordering::Greater + } else { + std::cmp::Ordering::Equal + } + }); let __values: Vec<_> = __order .iter() .map(|&__i| std::ptr::read(__tmp_1.iter_at(__i))) @@ -440,7 +373,17 @@ unsafe fn main_0() -> i32 { let mut __tmp_2 = { bs.as_mut_ptr() }; let __n = bs.as_mut_ptr().offset((3) as isize).iter_distance(&__tmp_2); let mut __order: Vec = (0..__n).collect(); - __order.sort_by(|&__x, &__y| (*__tmp_2.iter_at(__x)).cmp(&*__tmp_2.iter_at(__y))); + __order.sort_by(|&__x, &__y| { + let __a = __tmp_2.iter_at(__x) as *const _; + let __b = __tmp_2.iter_at(__y) as *const _; + if LessThanComparableUnsafe::less(__a, __b) { + std::cmp::Ordering::Less + } else if LessThanComparableUnsafe::less(__b, __a) { + std::cmp::Ordering::Greater + } else { + std::cmp::Ordering::Equal + } + }); let __values: Vec<_> = __order .iter() .map(|&__i| std::ptr::read(__tmp_2.iter_at(__i))) @@ -475,6 +418,20 @@ unsafe fn main_0() -> i32 { ); return 0; } +impl PartiallyOrderedWithUnsafe for X { + unsafe fn less(lhs: *const Self, rhs: *const Self) -> bool { + ::less(lhs, rhs) + } + unsafe fn greater(lhs: *const Self, rhs: *const Self) -> bool { + ((operator_gt_1((*lhs).clone(), (*rhs).clone())) != 0) + } + unsafe fn less_equal(lhs: *const Self, rhs: *const Self) -> bool { + ((operator_le_2((*lhs).clone(), (*rhs).clone())) != 0) + } + unsafe fn greater_equal(lhs: *const Self, rhs: *const Self) -> bool { + ((operator_ge_3((*lhs).clone(), (*rhs).clone())) != 0) + } +} impl WeaklyEqualityComparableWithUnsafe for Boolish { unsafe fn equal(a: *const Self, b: *const Self) -> bool { { @@ -501,7 +458,7 @@ impl WeaklyEqualityComparableWithUnsafe for X { ((operator_ne_0((*a).clone(), (*b).clone())) != 0) } unsafe fn equal(a: *const Self, b: *const Self) -> bool { - ((operator_eq_2((*a).clone(), (*b).clone())) != 0) + ((operator_eq_5((*a).clone(), (*b).clone())) != 0) } } pub unsafe fn __cpp2rust_init_globals() { diff --git a/tests/unit/out/unsafe/operator_comparison_noncopyable.rs b/tests/unit/out/unsafe/operator_comparison_noncopyable.rs index 50da53cb5e..a8079f7e48 100644 --- a/tests/unit/out/unsafe/operator_comparison_noncopyable.rs +++ b/tests/unit/out/unsafe/operator_comparison_noncopyable.rs @@ -6,8 +6,10 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; -pub unsafe fn operator_lt_0(x: *const S, y: *const S) -> bool { - return (((*x).data_) < ((*y).data_)); +impl LessThanComparableUnsafe for S { + unsafe fn less(x: *const S, y: *const S) -> bool { + return (((*x).data_) < ((*y).data_)); + } } #[repr(C)] #[derive( @@ -28,32 +30,6 @@ impl S { (&raw mut (*this).data_).write(data); } } -impl std::cmp::Ord for S { - fn cmp(&self, other: &Self) -> std::cmp::Ordering { - unsafe { - if operator_lt_0((self as *const Self), (other as *const Self)) { - std::cmp::Ordering::Less - } else if operator_lt_0((other as *const Self), (self as *const Self)) { - std::cmp::Ordering::Greater - } else { - std::cmp::Ordering::Equal - } - } - } -} -impl std::cmp::PartialOrd for S { - fn partial_cmp(&self, other: &Self) -> Option { - Some(self.cmp(other)) - } -} -impl std::cmp::PartialEq for S { - fn eq(&self, other: &Self) -> bool { - unsafe { - WeaklyEqualityComparableWithUnsafe::equal((self as *const Self), (other as *const Self)) - } - } -} -impl std::cmp::Eq for S {} pub fn main() { unsafe { __cpp2rust_init_globals(); @@ -73,13 +49,13 @@ unsafe fn main_0() -> i32 { assert!( (unsafe { let _x: *const S = &a; - operator_lt_0(_x, &b) + LessThanComparableUnsafe::less(_x, &b) }) ); assert!( !(unsafe { let _x: *const S = &b; - operator_lt_0(_x, &a) + LessThanComparableUnsafe::less(_x, &a) }) ); return 0; diff --git a/tests/unit/out/unsafe/operator_friend.rs b/tests/unit/out/unsafe/operator_friend.rs index d8a6e88031..ca483959cb 100644 --- a/tests/unit/out/unsafe/operator_friend.rs +++ b/tests/unit/out/unsafe/operator_friend.rs @@ -21,17 +21,21 @@ pub struct Defaulted { pub a: i32, pub b: i32, } -pub unsafe fn operator_cmp_0( - _a0: *const DefaultedOrd, - _a1: *const DefaultedOrd, -) -> std::cmp::Ordering { - { - let mut cmp: std::cmp::Ordering = std::cmp::Ord::cmp(&((*_a0).a), &((*_a1).a)); - if !(cmp == std::cmp::Ordering::Equal) { - return cmp; +impl ThreeWayComparableUnsafe for DefaultedOrd { + type Category = libcc2rs::StrongOrdering; + unsafe fn three_way( + _a0: *const DefaultedOrd, + _a1: *const DefaultedOrd, + ) -> libcc2rs::StrongOrdering { + { + let mut cmp: libcc2rs::StrongOrdering = + libcc2rs::StrongOrdering::from(std::cmp::Ord::cmp(&((*_a0).a), &((*_a1).a))); + if !(libcc2rs::is_eq(cmp)) { + return cmp; + } } + return libcc2rs::STRONG_ORDERING_EQUAL; } - return libcc2rs::STRONG_ORDERING_EQUAL; } #[repr(C)] #[derive( @@ -43,32 +47,23 @@ pub unsafe fn operator_cmp_0( TriviallyDefaultConstructibleUnsafe, TriviallyCopyableUnsafe, Default, + PartiallyOrderedWithUnsafe, )] +#[three_way(ThreeWayComparableUnsafe::three_way)] pub struct DefaultedOrd { pub a: i32, } -impl std::cmp::Ord for DefaultedOrd { - fn cmp(&self, other: &Self) -> std::cmp::Ordering { - unsafe { operator_cmp_0((self as *const Self), (other as *const Self)) } +impl LessThanComparableUnsafe for DefaultedOrd { + unsafe fn less(_a0: *const Self, _a1: *const Self) -> bool { + libcc2rs::is_lt(ThreeWayComparableUnsafe::three_way(_a0, _a1)) } } -impl std::cmp::PartialOrd for DefaultedOrd { - fn partial_cmp(&self, other: &Self) -> Option { - Some(self.cmp(other)) +impl LessThanComparableUnsafe for Inline { + unsafe fn less(x: *const Inline, y: *const Inline) -> bool { + return (((*x).a) < ((*y).a)); } } -impl std::cmp::PartialEq for DefaultedOrd { - fn eq(&self, other: &Self) -> bool { - unsafe { - WeaklyEqualityComparableWithUnsafe::equal((self as *const Self), (other as *const Self)) - } - } -} -impl std::cmp::Eq for DefaultedOrd {} -pub unsafe fn operator_lt_1(x: *const Inline, y: *const Inline) -> bool { - return (((*x).a) < ((*y).a)); -} -pub unsafe fn operator_add_2(x: *const Inline, y: *const Inline) -> Inline { +pub unsafe fn operator_add_0(x: *const Inline, y: *const Inline) -> Inline { return Inline { a: (((*x).a) + ((*y).a)), }; @@ -87,32 +82,6 @@ pub unsafe fn operator_add_2(x: *const Inline, y: *const Inline) -> Inline { pub struct Inline { pub a: i32, } -impl std::cmp::Ord for Inline { - fn cmp(&self, other: &Self) -> std::cmp::Ordering { - unsafe { - if operator_lt_1((self as *const Self), (other as *const Self)) { - std::cmp::Ordering::Less - } else if operator_lt_1((other as *const Self), (self as *const Self)) { - std::cmp::Ordering::Greater - } else { - std::cmp::Ordering::Equal - } - } - } -} -impl std::cmp::PartialOrd for Inline { - fn partial_cmp(&self, other: &Self) -> Option { - Some(self.cmp(other)) - } -} -impl std::cmp::PartialEq for Inline { - fn eq(&self, other: &Self) -> bool { - unsafe { - WeaklyEqualityComparableWithUnsafe::equal((self as *const Self), (other as *const Self)) - } - } -} -impl std::cmp::Eq for Inline {} #[repr(C)] #[derive( Copy, @@ -127,8 +96,10 @@ impl std::cmp::Eq for Inline {} pub struct OutOfLine { pub a: i32, } -pub unsafe fn operator_lt_3(x: *const Tmpl_int_, y: *const Tmpl_int_) -> bool { - return (((*x).v) < ((*y).v)); +impl LessThanComparableUnsafe for Tmpl_int_ { + unsafe fn less(x: *const Tmpl_int_, y: *const Tmpl_int_) -> bool { + return (((*x).v) < ((*y).v)); + } } #[repr(C)] #[derive( @@ -144,32 +115,6 @@ pub unsafe fn operator_lt_3(x: *const Tmpl_int_, y: *const Tmpl_int_) -> bool { pub struct Tmpl_int_ { pub v: i32, } -impl std::cmp::Ord for Tmpl_int_ { - fn cmp(&self, other: &Self) -> std::cmp::Ordering { - unsafe { - if operator_lt_3((self as *const Self), (other as *const Self)) { - std::cmp::Ordering::Less - } else if operator_lt_3((other as *const Self), (self as *const Self)) { - std::cmp::Ordering::Greater - } else { - std::cmp::Ordering::Equal - } - } - } -} -impl std::cmp::PartialOrd for Tmpl_int_ { - fn partial_cmp(&self, other: &Self) -> Option { - Some(self.cmp(other)) - } -} -impl std::cmp::PartialEq for Tmpl_int_ { - fn eq(&self, other: &Self) -> bool { - unsafe { - WeaklyEqualityComparableWithUnsafe::equal((self as *const Self), (other as *const Self)) - } - } -} -impl std::cmp::Eq for Tmpl_int_ {} #[repr(C)] #[derive( Copy, @@ -228,18 +173,18 @@ unsafe fn main_0() -> i32 { ); let mut o1: DefaultedOrd = DefaultedOrd { a: 1 }; let mut o2: DefaultedOrd = DefaultedOrd { a: 2 }; - assert!( + assert!(libcc2rs::is_lt( (unsafe { let _arg0: *const DefaultedOrd = &o1; - operator_cmp_0(_arg0, &o2) - }) == std::cmp::Ordering::Less - ); - assert!( + ThreeWayComparableUnsafe::three_way(_arg0, &o2) + }) + )); + assert!(libcc2rs::is_gt( (unsafe { let _arg0: *const DefaultedOrd = &o2; - operator_cmp_0(_arg0, &o1) - }) == std::cmp::Ordering::Greater - ); + ThreeWayComparableUnsafe::three_way(_arg0, &o1) + }) + )); assert!( (unsafe { let _arg0: *const DefaultedOrd = &o1; @@ -247,12 +192,18 @@ unsafe fn main_0() -> i32 { WeaklyEqualityComparableWithUnsafe::equal(_arg0, _arg1) }) ); - assert!( - (unsafe { - let _arg0: *const DefaultedOrd = &o1; - operator_cmp_0(_arg0, &o2) - }) == libcc2rs::STRONG_ORDERING_LESS - ); + assert!({ + let __a = libcc2rs::PartialOrdering::from( + (unsafe { + let _arg0: *const DefaultedOrd = &o1; + ThreeWayComparableUnsafe::three_way(_arg0, &o2) + }), + ); + let __b = libcc2rs::PartialOrdering::from(libcc2rs::STRONG_ORDERING_LESS); + libcc2rs::is_lt(__a) == libcc2rs::is_lt(__b) + && libcc2rs::is_eq(__a) == libcc2rs::is_eq(__b) + && libcc2rs::is_gt(__a) == libcc2rs::is_gt(__b) + }); let mut i1: Inline = Inline { a: 1 }; let mut i2: Inline = Inline { a: 2 }; let mut i3: Inline = Inline { a: 1 }; @@ -271,20 +222,20 @@ unsafe fn main_0() -> i32 { assert!( (unsafe { let _x: *const Inline = &i1; - operator_lt_1(_x, &i2) + LessThanComparableUnsafe::less(_x, &i2) }) ); assert!( !(unsafe { let _x: *const Inline = &i2; - operator_lt_1(_x, &i1) + LessThanComparableUnsafe::less(_x, &i1) }) ); assert!( (unsafe { let mut _x: Inline = (unsafe { let _x: *const Inline = &i1; - operator_add_2(_x, &i2) + operator_add_0(_x, &i2) }); let mut _y: Inline = Inline { a: 3 }; WeaklyEqualityComparableWithUnsafe::equal(&mut _x, &mut _y) @@ -329,7 +280,7 @@ unsafe fn main_0() -> i32 { assert!( (unsafe { let _x: *const Tmpl_int_ = &t1; - operator_lt_3(_x, &t2) + LessThanComparableUnsafe::less(_x, &t2) }) ); let mut u1: Tmpl_long_ = Tmpl_long_ { v: 1_i64 }; diff --git a/tests/unit/out/unsafe/operator_less_than.rs b/tests/unit/out/unsafe/operator_less_than.rs index 54ee1f8bf2..557eb3f9aa 100644 --- a/tests/unit/out/unsafe/operator_less_than.rs +++ b/tests/unit/out/unsafe/operator_less_than.rs @@ -27,33 +27,11 @@ impl Pair { || ((((*this).x) == ((*other).x)) && (((*this).y) < ((*other).y))); } } -impl std::cmp::Ord for Pair { - fn cmp(&self, other: &Self) -> std::cmp::Ordering { - unsafe { - if Pair::operator_lt((self as *const Self).cast_mut(), (other as *const Self)) { - std::cmp::Ordering::Less - } else if Pair::operator_lt((other as *const Self).cast_mut(), (self as *const Self)) { - std::cmp::Ordering::Greater - } else { - std::cmp::Ordering::Equal - } - } +impl LessThanComparableUnsafe for Pair { + unsafe fn less(this: *const Self, other: *const Self) -> bool { + Pair::operator_lt(this.cast_mut(), other) } } -impl std::cmp::PartialOrd for Pair { - fn partial_cmp(&self, other: &Self) -> Option { - Some(self.cmp(other)) - } -} -impl std::cmp::PartialEq for Pair { - fn eq(&self, other: &Self) -> bool { - unsafe { - !(Pair::operator_lt((self as *const Self).cast_mut(), (other as *const Self))) - && !(Pair::operator_lt((other as *const Self).cast_mut(), (self as *const Self))) - } - } -} -impl std::cmp::Eq for Pair {} pub fn main() { unsafe { __cpp2rust_init_globals(); diff --git a/tests/unit/out/unsafe/operator_three_way.rs b/tests/unit/out/unsafe/operator_three_way.rs index 3511bdd32b..b66591f0ff 100644 --- a/tests/unit/out/unsafe/operator_three_way.rs +++ b/tests/unit/out/unsafe/operator_three_way.rs @@ -16,12 +16,15 @@ use std::rc::Rc; TriviallyDefaultConstructibleUnsafe, TriviallyCopyableUnsafe, Default, + PartiallyOrderedWithUnsafe, )] +#[three_way(ThreeWayComparableUnsafe::three_way)] pub struct S { pub v: i32, } -impl S { - pub unsafe fn operator_cmp(this: *const Self, o: *const S) -> std::cmp::Ordering { +impl ThreeWayComparableUnsafe for S { + type Category = libcc2rs::StrongOrdering; + unsafe fn three_way(this: *const Self, o: *const S) -> libcc2rs::StrongOrdering { if (((*this).v) < ((*o).v)) { return libcc2rs::STRONG_ORDERING_LESS; } @@ -31,24 +34,11 @@ impl S { return libcc2rs::STRONG_ORDERING_EQUAL; } } -impl std::cmp::Ord for S { - fn cmp(&self, other: &Self) -> std::cmp::Ordering { - unsafe { S::operator_cmp((self as *const Self), (other as *const Self)) } +impl LessThanComparableUnsafe for S { + unsafe fn less(this: *const Self, o: *const Self) -> bool { + libcc2rs::is_lt(ThreeWayComparableUnsafe::three_way(this, o)) } } -impl std::cmp::PartialOrd for S { - fn partial_cmp(&self, other: &Self) -> Option { - Some(self.cmp(other)) - } -} -impl std::cmp::PartialEq for S { - fn eq(&self, other: &Self) -> bool { - unsafe { - WeaklyEqualityComparableWithUnsafe::equal((self as *const Self), (other as *const Self)) - } - } -} -impl std::cmp::Eq for S {} pub fn main() { unsafe { __cpp2rust_init_globals(); @@ -58,12 +48,28 @@ pub fn main() { unsafe fn main_0() -> i32 { let mut a: S = S { v: 1 }; let mut b: S = S { v: 2 }; - assert!((unsafe { S::operator_cmp(&raw const a, &b,) }) == std::cmp::Ordering::Less); - assert!((unsafe { S::operator_cmp(&raw const b, &a,) }) == std::cmp::Ordering::Greater); - assert!((unsafe { S::operator_cmp(&raw const a, &b,) }) != std::cmp::Ordering::Greater); - assert!((unsafe { S::operator_cmp(&raw const b, &a,) }) != std::cmp::Ordering::Less); + assert!(libcc2rs::is_lt( + (unsafe { ThreeWayComparableUnsafe::three_way(&raw const a, &b,) }) + )); + assert!(libcc2rs::is_gt( + (unsafe { ThreeWayComparableUnsafe::three_way(&raw const b, &a,) }) + )); + assert!(libcc2rs::is_lteq( + (unsafe { ThreeWayComparableUnsafe::three_way(&raw const a, &b,) }) + )); + assert!(libcc2rs::is_gteq( + (unsafe { ThreeWayComparableUnsafe::three_way(&raw const b, &a,) }) + )); assert!(!(unsafe { WeaklyEqualityComparableWithUnsafe::equal(&raw const a, &b,) })); - assert!((unsafe { S::operator_cmp(&raw const a, &b,) }) == libcc2rs::STRONG_ORDERING_LESS); + assert!({ + let __a = libcc2rs::PartialOrdering::from( + (unsafe { ThreeWayComparableUnsafe::three_way(&raw const a, &b) }), + ); + let __b = libcc2rs::PartialOrdering::from(libcc2rs::STRONG_ORDERING_LESS); + libcc2rs::is_lt(__a) == libcc2rs::is_lt(__b) + && libcc2rs::is_eq(__a) == libcc2rs::is_eq(__b) + && libcc2rs::is_gt(__a) == libcc2rs::is_gt(__b) + }); return 0; } impl WeaklyEqualityComparableWithUnsafe for S { diff --git a/tests/unit/out/unsafe/operator_traits.rs b/tests/unit/out/unsafe/operator_traits.rs index da0b07ce0f..87f7a79611 100644 --- a/tests/unit/out/unsafe/operator_traits.rs +++ b/tests/unit/out/unsafe/operator_traits.rs @@ -20,38 +20,11 @@ use std::rc::Rc; pub struct Lt { pub v: i32, } -impl Lt { - pub unsafe fn operator_lt(this: *const Self, o: *const Lt) -> bool { +impl LessThanComparableUnsafe for Lt { + unsafe fn less(this: *const Self, o: *const Lt) -> bool { return (((*this).v) < ((*o).v)); } } -impl std::cmp::Ord for Lt { - fn cmp(&self, other: &Self) -> std::cmp::Ordering { - unsafe { - if Lt::operator_lt((self as *const Self), (other as *const Self)) { - std::cmp::Ordering::Less - } else if Lt::operator_lt((other as *const Self), (self as *const Self)) { - std::cmp::Ordering::Greater - } else { - std::cmp::Ordering::Equal - } - } - } -} -impl std::cmp::PartialOrd for Lt { - fn partial_cmp(&self, other: &Self) -> Option { - Some(self.cmp(other)) - } -} -impl std::cmp::PartialEq for Lt { - fn eq(&self, other: &Self) -> bool { - unsafe { - !(Lt::operator_lt((self as *const Self), (other as *const Self))) - && !(Lt::operator_lt((other as *const Self), (self as *const Self))) - } - } -} -impl std::cmp::Eq for Lt {} #[repr(C)] #[derive( Copy, @@ -76,33 +49,23 @@ pub struct Eq { TriviallyDefaultConstructibleUnsafe, TriviallyCopyableUnsafe, Default, + PartiallyOrderedWithUnsafe, )] +#[three_way(ThreeWayComparableUnsafe::three_way)] pub struct Cmp { pub v: i32, } -impl Cmp { - pub unsafe fn operator_cmp(this: *const Self, o: *const Cmp) -> std::cmp::Ordering { - return std::cmp::Ord::cmp(&((*this).v), &((*o).v)); - } -} -impl std::cmp::Ord for Cmp { - fn cmp(&self, other: &Self) -> std::cmp::Ordering { - unsafe { Cmp::operator_cmp((self as *const Self), (other as *const Self)) } +impl ThreeWayComparableUnsafe for Cmp { + type Category = libcc2rs::StrongOrdering; + unsafe fn three_way(this: *const Self, o: *const Cmp) -> libcc2rs::StrongOrdering { + return libcc2rs::StrongOrdering::from(std::cmp::Ord::cmp(&((*this).v), &((*o).v))); } } -impl std::cmp::PartialOrd for Cmp { - fn partial_cmp(&self, other: &Self) -> Option { - Some(self.cmp(other)) - } -} -impl std::cmp::PartialEq for Cmp { - fn eq(&self, other: &Self) -> bool { - unsafe { - WeaklyEqualityComparableWithUnsafe::equal((self as *const Self), (other as *const Self)) - } +impl LessThanComparableUnsafe for Cmp { + unsafe fn less(this: *const Self, o: *const Self) -> bool { + libcc2rs::is_lt(ThreeWayComparableUnsafe::three_way(this, o)) } } -impl std::cmp::Eq for Cmp {} #[repr(C)] #[derive( Copy, @@ -117,35 +80,11 @@ impl std::cmp::Eq for Cmp {} pub struct Free { pub v: i32, } -impl std::cmp::Ord for Free { - fn cmp(&self, other: &Self) -> std::cmp::Ordering { - unsafe { - if operator_lt_0((self as *const Self), (other as *const Self)) { - std::cmp::Ordering::Less - } else if operator_lt_0((other as *const Self), (self as *const Self)) { - std::cmp::Ordering::Greater - } else { - std::cmp::Ordering::Equal - } - } - } -} -impl std::cmp::PartialOrd for Free { - fn partial_cmp(&self, other: &Self) -> Option { - Some(self.cmp(other)) - } -} -impl std::cmp::PartialEq for Free { - fn eq(&self, other: &Self) -> bool { - unsafe { - WeaklyEqualityComparableWithUnsafe::equal((self as *const Self), (other as *const Self)) - } +impl LessThanComparableUnsafe for Free { + unsafe fn less(a: *const Free, b: *const Free) -> bool { + return (((*a).v) < ((*b).v)); } } -impl std::cmp::Eq for Free {} -pub unsafe fn operator_lt_0(a: *const Free, b: *const Free) -> bool { - return (((*a).v) < ((*b).v)); -} #[repr(C)] #[derive( Copy, @@ -174,7 +113,17 @@ unsafe fn main_0() -> i32 { let mut __tmp_0 = { lts.begin() }; let __n = lts.end().iter_distance(&__tmp_0); let mut __order: Vec = (0..__n).collect(); - __order.sort_by(|&__x, &__y| (*__tmp_0.iter_at(__x)).cmp(&*__tmp_0.iter_at(__y))); + __order.sort_by(|&__x, &__y| { + let __a = __tmp_0.iter_at(__x) as *const _; + let __b = __tmp_0.iter_at(__y) as *const _; + if LessThanComparableUnsafe::less(__a, __b) { + std::cmp::Ordering::Less + } else if LessThanComparableUnsafe::less(__b, __a) { + std::cmp::Ordering::Greater + } else { + std::cmp::Ordering::Equal + } + }); let __values: Vec<_> = __order .iter() .map(|&__i| std::ptr::read(__tmp_0.iter_at(__i))) @@ -223,7 +172,17 @@ unsafe fn main_0() -> i32 { let mut __tmp_5 = { cmps.begin() }; let __n = cmps.end().iter_distance(&__tmp_5); let mut __order: Vec = (0..__n).collect(); - __order.sort_by(|&__x, &__y| (*__tmp_5.iter_at(__x)).cmp(&*__tmp_5.iter_at(__y))); + __order.sort_by(|&__x, &__y| { + let __a = __tmp_5.iter_at(__x) as *const _; + let __b = __tmp_5.iter_at(__y) as *const _; + if LessThanComparableUnsafe::less(__a, __b) { + std::cmp::Ordering::Less + } else if LessThanComparableUnsafe::less(__b, __a) { + std::cmp::Ordering::Greater + } else { + std::cmp::Ordering::Equal + } + }); let __values: Vec<_> = __order .iter() .map(|&__i| std::ptr::read(__tmp_5.iter_at(__i))) @@ -254,7 +213,17 @@ unsafe fn main_0() -> i32 { let mut __tmp_8 = { frees.begin() }; let __n = frees.end().iter_distance(&__tmp_8); let mut __order: Vec = (0..__n).collect(); - __order.sort_by(|&__x, &__y| (*__tmp_8.iter_at(__x)).cmp(&*__tmp_8.iter_at(__y))); + __order.sort_by(|&__x, &__y| { + let __a = __tmp_8.iter_at(__x) as *const _; + let __b = __tmp_8.iter_at(__y) as *const _; + if LessThanComparableUnsafe::less(__a, __b) { + std::cmp::Ordering::Less + } else if LessThanComparableUnsafe::less(__b, __a) { + std::cmp::Ordering::Greater + } else { + std::cmp::Ordering::Equal + } + }); let __values: Vec<_> = __order .iter() .map(|&__i| std::ptr::read(__tmp_8.iter_at(__i))) diff --git a/tests/unit/out/unsafe/size_t_ssize_t.rs b/tests/unit/out/unsafe/size_t_ssize_t.rs index 35638f5401..5c80a1e374 100644 --- a/tests/unit/out/unsafe/size_t_ssize_t.rs +++ b/tests/unit/out/unsafe/size_t_ssize_t.rs @@ -173,7 +173,11 @@ pub unsafe fn size_t_in_rules_3() { (*{ let __a: *const _ = &mut v[(1_usize)]; let __b: *const _ = &mut __tmp_10; - if *__a < *__b { __b } else { __a } + if LessThanComparableUnsafe::less(__a, __b) { + __b + } else { + __a + } }) }) as usize); let elem: *mut usize = &mut *(&raw mut v[(2_usize)]).cast::(); @@ -293,7 +297,11 @@ unsafe fn main_0() -> i32 { (*{ let __a: *const _ = &mut __tmp_15; let __b: *const _ = &mut __tmp_16; - if *__b < *__a { __b } else { __a } + if LessThanComparableUnsafe::less(__b, __a) { + __b + } else { + __a + } }) }) as usize); let mut hi: usize = (({ @@ -301,7 +309,11 @@ unsafe fn main_0() -> i32 { (*{ let __a: *const _ = &mut __tmp_17; let __b: *const _ = &mut ul; - if *__a < *__b { __b } else { __a } + if LessThanComparableUnsafe::less(__a, __b) { + __b + } else { + __a + } }) }) as usize); assert!(((lo) == ((::std::mem::size_of::() as usize).wrapping_add(10_usize)))); @@ -312,7 +324,11 @@ unsafe fn main_0() -> i32 { (*{ let __a: *const _ = &mut __tmp_18; let __b: *const _ = &mut __tmp_19; - if *__b < *__a { __b } else { __a } + if LessThanComparableUnsafe::less(__b, __a) { + __b + } else { + __a + } }) }) as usize); assert!(((bound) == (4_usize))); @@ -367,7 +383,11 @@ unsafe fn main_0() -> i32 { (*{ let __a: *const _ = &mut __tmp_20; let __b: *const _ = &mut __tmp_21; - if *__b < *__a { __b } else { __a } + if LessThanComparableUnsafe::less(__b, __a) { + __b + } else { + __a + } }) }) as isize); let mut smax: isize = (({ @@ -376,7 +396,11 @@ unsafe fn main_0() -> i32 { (*{ let __a: *const _ = &mut __tmp_22; let __b: *const _ = &mut __tmp_23; - if *__a < *__b { __b } else { __a } + if LessThanComparableUnsafe::less(__a, __b) { + __b + } else { + __a + } }) }) as isize); assert!(((smin) == (-7_i32 as isize))); @@ -402,7 +426,11 @@ unsafe fn main_0() -> i32 { (*{ let __a: *const _ = &mut __tmp_24; let __b: *const _ = &mut __tmp_25; - if *__b < *__a { __b } else { __a } + if LessThanComparableUnsafe::less(__b, __a) { + __b + } else { + __a + } }) }) as usize), (({ @@ -411,7 +439,11 @@ unsafe fn main_0() -> i32 { (*{ let __a: *const _ = &mut __tmp_26; let __b: *const _ = &mut __tmp_27; - if *__b < *__a { __b } else { __a } + if LessThanComparableUnsafe::less(__b, __a) { + __b + } else { + __a + } }) }) as usize), mx, diff --git a/tests/unit/out/unsafe/sort.rs b/tests/unit/out/unsafe/sort.rs index 51a9094a89..43cdf5c306 100644 --- a/tests/unit/out/unsafe/sort.rs +++ b/tests/unit/out/unsafe/sort.rs @@ -58,7 +58,17 @@ unsafe fn main_0() -> i32 { let mut __tmp_10 = { v.begin() }; let __n = v.end().iter_distance(&__tmp_10); let mut __order: Vec = (0..__n).collect(); - __order.sort_by(|&__x, &__y| (*__tmp_10.iter_at(__x)).cmp(&*__tmp_10.iter_at(__y))); + __order.sort_by(|&__x, &__y| { + let __a = __tmp_10.iter_at(__x) as *const _; + let __b = __tmp_10.iter_at(__y) as *const _; + if LessThanComparableUnsafe::less(__a, __b) { + std::cmp::Ordering::Less + } else if LessThanComparableUnsafe::less(__b, __a) { + std::cmp::Ordering::Greater + } else { + std::cmp::Ordering::Equal + } + }); let __values: Vec<_> = __order .iter() .map(|&__i| std::ptr::read(__tmp_10.iter_at(__i))) diff --git a/tests/unit/out/unsafe/sort_no_copy.rs b/tests/unit/out/unsafe/sort_no_copy.rs index 13a3f86ea0..cbbd9c9c64 100644 --- a/tests/unit/out/unsafe/sort_no_copy.rs +++ b/tests/unit/out/unsafe/sort_no_copy.rs @@ -49,33 +49,6 @@ impl MoveAssignableUnsafe for Tracked { return &mut (*this); } } -impl std::cmp::Ord for Tracked { - fn cmp(&self, other: &Self) -> std::cmp::Ordering { - unsafe { - if operator_lt_1((self as *const Self), (other as *const Self)) { - std::cmp::Ordering::Less - } else if operator_lt_1((other as *const Self), (self as *const Self)) { - std::cmp::Ordering::Greater - } else { - std::cmp::Ordering::Equal - } - } - } -} -impl std::cmp::PartialOrd for Tracked { - fn partial_cmp(&self, other: &Self) -> Option { - Some(self.cmp(other)) - } -} -impl std::cmp::PartialEq for Tracked { - fn eq(&self, other: &Self) -> bool { - unsafe { - !(operator_lt_1((self as *const Self), (other as *const Self))) - && !(operator_lt_1((other as *const Self), (self as *const Self))) - } - } -} -impl std::cmp::Eq for Tracked {} impl Default for Tracked { fn default() -> Self { unsafe { @@ -87,10 +60,12 @@ impl Default for Tracked { } } } -pub unsafe fn operator_lt_1(a: *const Tracked, b: *const Tracked) -> bool { - return (((*a).v) < ((*b).v)); +impl LessThanComparableUnsafe for Tracked { + unsafe fn less(a: *const Tracked, b: *const Tracked) -> bool { + return (((*a).v) < ((*b).v)); + } } -pub unsafe fn greater_2(a: *const Tracked, b: *const Tracked) -> bool { +pub unsafe fn greater_1(a: *const Tracked, b: *const Tracked) -> bool { return (((*a).v) > ((*b).v)); } #[repr(C)] @@ -113,36 +88,11 @@ impl MoveOnly { (&raw mut (*this).v).write(v); } } -impl std::cmp::Ord for MoveOnly { - fn cmp(&self, other: &Self) -> std::cmp::Ordering { - unsafe { - if operator_lt_3((self as *const Self), (other as *const Self)) { - std::cmp::Ordering::Less - } else if operator_lt_3((other as *const Self), (self as *const Self)) { - std::cmp::Ordering::Greater - } else { - std::cmp::Ordering::Equal - } - } - } -} -impl std::cmp::PartialOrd for MoveOnly { - fn partial_cmp(&self, other: &Self) -> Option { - Some(self.cmp(other)) +impl LessThanComparableUnsafe for MoveOnly { + unsafe fn less(a: *const MoveOnly, b: *const MoveOnly) -> bool { + return (((*a).v) < ((*b).v)); } } -impl std::cmp::PartialEq for MoveOnly { - fn eq(&self, other: &Self) -> bool { - unsafe { - !(operator_lt_3((self as *const Self), (other as *const Self))) - && !(operator_lt_3((other as *const Self), (self as *const Self))) - } - } -} -impl std::cmp::Eq for MoveOnly {} -pub unsafe fn operator_lt_3(a: *const MoveOnly, b: *const MoveOnly) -> bool { - return (((*a).v) < ((*b).v)); -} #[repr(C)] #[derive( FnPtrArg, @@ -161,35 +111,11 @@ impl DefaultConstructibleUnsafe for Bag { (&raw mut (*this).items).write(Vec::new()); } } -impl std::cmp::Ord for Bag { - fn cmp(&self, other: &Self) -> std::cmp::Ordering { - unsafe { - if operator_lt_4((self as *const Self), (other as *const Self)) { - std::cmp::Ordering::Less - } else if operator_lt_4((other as *const Self), (self as *const Self)) { - std::cmp::Ordering::Greater - } else { - std::cmp::Ordering::Equal - } - } +impl LessThanComparableUnsafe for Bag { + unsafe fn less(a: *const Bag, b: *const Bag) -> bool { + return (((*a).items.len()) < ((*b).items.len())); } } -impl std::cmp::PartialOrd for Bag { - fn partial_cmp(&self, other: &Self) -> Option { - Some(self.cmp(other)) - } -} -impl std::cmp::PartialEq for Bag { - fn eq(&self, other: &Self) -> bool { - unsafe { - WeaklyEqualityComparableWithUnsafe::equal((self as *const Self), (other as *const Self)) - } - } -} -impl std::cmp::Eq for Bag {} -pub unsafe fn operator_lt_4(a: *const Bag, b: *const Bag) -> bool { - return (((*a).items.len()) < ((*b).items.len())); -} pub fn main() { unsafe { __cpp2rust_init_globals(); @@ -231,7 +157,17 @@ unsafe fn main_0() -> i32 { .offset((5) as isize) .iter_distance(&__tmp_0); let mut __order: Vec = (0..__n).collect(); - __order.sort_by(|&__x, &__y| (*__tmp_0.iter_at(__x)).cmp(&*__tmp_0.iter_at(__y))); + __order.sort_by(|&__x, &__y| { + let __a = __tmp_0.iter_at(__x) as *const _; + let __b = __tmp_0.iter_at(__y) as *const _; + if LessThanComparableUnsafe::less(__a, __b) { + std::cmp::Ordering::Less + } else if LessThanComparableUnsafe::less(__b, __a) { + std::cmp::Ordering::Greater + } else { + std::cmp::Ordering::Equal + } + }); let __values: Vec<_> = __order .iter() .map(|&__i| std::ptr::read(__tmp_0.iter_at(__i))) @@ -276,7 +212,17 @@ unsafe fn main_0() -> i32 { let mut __tmp_1 = { vec_.begin() }; let __n = vec_.end().iter_distance(&__tmp_1); let mut __order: Vec = (0..__n).collect(); - __order.sort_by(|&__x, &__y| (*__tmp_1.iter_at(__x)).cmp(&*__tmp_1.iter_at(__y))); + __order.sort_by(|&__x, &__y| { + let __a = __tmp_1.iter_at(__x) as *const _; + let __b = __tmp_1.iter_at(__y) as *const _; + if LessThanComparableUnsafe::less(__a, __b) { + std::cmp::Ordering::Less + } else if LessThanComparableUnsafe::less(__b, __a) { + std::cmp::Ordering::Greater + } else { + std::cmp::Ordering::Equal + } + }); let __values: Vec<_> = __order .iter() .map(|&__i| std::ptr::read(__tmp_1.iter_at(__i))) @@ -298,9 +244,9 @@ unsafe fn main_0() -> i32 { __order.sort_by(|&__x, &__y| { let __a = __tmp_2.iter_at(__x) as *const _; let __b = __tmp_2.iter_at(__y) as *const _; - if (greater_2 as unsafe fn(*const Tracked, *const Tracked) -> bool).call(__a, __b) { + if (greater_1 as unsafe fn(*const Tracked, *const Tracked) -> bool).call(__a, __b) { std::cmp::Ordering::Less - } else if (greater_2 as unsafe fn(*const Tracked, *const Tracked) -> bool) + } else if (greater_1 as unsafe fn(*const Tracked, *const Tracked) -> bool) .call(__b, __a) { std::cmp::Ordering::Greater @@ -366,9 +312,9 @@ unsafe fn main_0() -> i32 { __order.sort_by(|&__x, &__y| { let __a = __tmp_4.iter_at(__x) as *const _; let __b = __tmp_4.iter_at(__y) as *const _; - if (greater_2 as unsafe fn(*const Tracked, *const Tracked) -> bool).call(__a, __b) { + if (greater_1 as unsafe fn(*const Tracked, *const Tracked) -> bool).call(__a, __b) { std::cmp::Ordering::Less - } else if (greater_2 as unsafe fn(*const Tracked, *const Tracked) -> bool) + } else if (greater_1 as unsafe fn(*const Tracked, *const Tracked) -> bool) .call(__b, __a) { std::cmp::Ordering::Greater @@ -416,7 +362,17 @@ unsafe fn main_0() -> i32 { .offset((3) as isize) .iter_distance(&__tmp_5); let mut __order: Vec = (0..__n).collect(); - __order.sort_by(|&__x, &__y| (*__tmp_5.iter_at(__x)).cmp(&*__tmp_5.iter_at(__y))); + __order.sort_by(|&__x, &__y| { + let __a = __tmp_5.iter_at(__x) as *const _; + let __b = __tmp_5.iter_at(__y) as *const _; + if LessThanComparableUnsafe::less(__a, __b) { + std::cmp::Ordering::Less + } else if LessThanComparableUnsafe::less(__b, __a) { + std::cmp::Ordering::Greater + } else { + std::cmp::Ordering::Equal + } + }); let __values: Vec<_> = __order .iter() .map(|&__i| std::ptr::read(__tmp_5.iter_at(__i))) @@ -450,7 +406,17 @@ unsafe fn main_0() -> i32 { let mut __tmp_6 = { bag_vec.begin() }; let __n = bag_vec.end().iter_distance(&__tmp_6); let mut __order: Vec = (0..__n).collect(); - __order.sort_by(|&__x, &__y| (*__tmp_6.iter_at(__x)).cmp(&*__tmp_6.iter_at(__y))); + __order.sort_by(|&__x, &__y| { + let __a = __tmp_6.iter_at(__x) as *const _; + let __b = __tmp_6.iter_at(__y) as *const _; + if LessThanComparableUnsafe::less(__a, __b) { + std::cmp::Ordering::Less + } else if LessThanComparableUnsafe::less(__b, __a) { + std::cmp::Ordering::Greater + } else { + std::cmp::Ordering::Equal + } + }); let __values: Vec<_> = __order .iter() .map(|&__i| std::ptr::read(__tmp_6.iter_at(__i))) @@ -490,7 +456,10 @@ unsafe fn main_0() -> i32 { let mut __it = (__tmp_9).clone(); __it.iter_inc(); while WeaklyEqualityComparableWithUnsafe::not_equal(&__it, &__tmp_10) { - if *__best.iter_deref() < *__it.iter_deref() { + if LessThanComparableUnsafe::less( + __best.iter_deref() as *const _, + __it.iter_deref() as *const _, + ) { __best = __it.clone(); } __it.iter_inc(); @@ -504,14 +473,22 @@ unsafe fn main_0() -> i32 { (({ let __a: *const _ = &mut bags[(0) as usize]; let __b: *const _ = &mut bags[(2) as usize]; - if *__b < *__a { __b } else { __a } + if LessThanComparableUnsafe::less(__b, __a) { + __b + } else { + __a + } }) == ((&mut bags[(0) as usize] as *mut Bag).cast_const())) ); assert!( (({ let __a: *const _ = &mut bags[(0) as usize]; let __b: *const _ = &mut bags[(2) as usize]; - if *__a < *__b { __b } else { __a } + if LessThanComparableUnsafe::less(__a, __b) { + __b + } else { + __a + } }) == ((&mut bags[(2) as usize] as *mut Bag).cast_const())) ); assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut copies_0)) == (0))); @@ -552,7 +529,17 @@ unsafe fn main_0() -> i32 { let mut __tmp_11 = { moves.begin() }; let __n = moves.end().iter_distance(&__tmp_11); let mut __order: Vec = (0..__n).collect(); - __order.sort_by(|&__x, &__y| (*__tmp_11.iter_at(__x)).cmp(&*__tmp_11.iter_at(__y))); + __order.sort_by(|&__x, &__y| { + let __a = __tmp_11.iter_at(__x) as *const _; + let __b = __tmp_11.iter_at(__y) as *const _; + if LessThanComparableUnsafe::less(__a, __b) { + std::cmp::Ordering::Less + } else if LessThanComparableUnsafe::less(__b, __a) { + std::cmp::Ordering::Greater + } else { + std::cmp::Ordering::Equal + } + }); let __values: Vec<_> = __order .iter() .map(|&__i| std::ptr::read(__tmp_11.iter_at(__i))) diff --git a/tests/unit/out/unsafe/stdmin_fpoint.rs b/tests/unit/out/unsafe/stdmin_fpoint.rs index 47c042d846..d4071745ca 100644 --- a/tests/unit/out/unsafe/stdmin_fpoint.rs +++ b/tests/unit/out/unsafe/stdmin_fpoint.rs @@ -18,7 +18,11 @@ unsafe fn main_0() -> i32 { { let __a: *const _ = &mut a0; let __b: *const _ = &mut a1; - if *__b < *__a { __b } else { __a } + if LessThanComparableUnsafe::less(__b, __a) { + __b + } else { + __a + } }; return 0; } diff --git a/tests/unit/out/unsafe/unboxed_locals.rs b/tests/unit/out/unsafe/unboxed_locals.rs index d50a39af8a..ec913013c6 100644 --- a/tests/unit/out/unsafe/unboxed_locals.rs +++ b/tests/unit/out/unsafe/unboxed_locals.rs @@ -166,7 +166,11 @@ unsafe fn main_0() -> i32 { (*{ let __a: *const _ = &mut high; let __b: *const _ = &mut __tmp_0; - if *__b < *__a { __b } else { __a } + if LessThanComparableUnsafe::less(__b, __a) { + __b + } else { + __a + } }) }; assert!(((low) == (2))); diff --git a/tests/unit/out/unsafe/void_return.rs b/tests/unit/out/unsafe/void_return.rs index 4d173207ef..2a9f3c23a6 100644 --- a/tests/unit/out/unsafe/void_return.rs +++ b/tests/unit/out/unsafe/void_return.rs @@ -14,7 +14,17 @@ pub unsafe fn f1_0( let mut __tmp_0 = { (first).clone() }; let __n = (last).clone().iter_distance(&__tmp_0); let mut __order: Vec = (0..__n).collect(); - __order.sort_by(|&__x, &__y| (*__tmp_0.iter_at(__x)).cmp(&*__tmp_0.iter_at(__y))); + __order.sort_by(|&__x, &__y| { + let __a = __tmp_0.iter_at(__x) as *const _; + let __b = __tmp_0.iter_at(__y) as *const _; + if LessThanComparableUnsafe::less(__a, __b) { + std::cmp::Ordering::Less + } else if LessThanComparableUnsafe::less(__b, __a) { + std::cmp::Ordering::Greater + } else { + std::cmp::Ordering::Equal + } + }); let __values: Vec<_> = __order .iter() .map(|&__i| std::ptr::read(__tmp_0.iter_at(__i))) @@ -29,7 +39,17 @@ pub unsafe fn f1_1(mut first: *mut i32, mut last: *mut i32) { { let __n = last.iter_distance(&first); let mut __order: Vec = (0..__n).collect(); - __order.sort_by(|&__x, &__y| (*first.iter_at(__x)).cmp(&*first.iter_at(__y))); + __order.sort_by(|&__x, &__y| { + let __a = first.iter_at(__x) as *const _; + let __b = first.iter_at(__y) as *const _; + if LessThanComparableUnsafe::less(__a, __b) { + std::cmp::Ordering::Less + } else if LessThanComparableUnsafe::less(__b, __a) { + std::cmp::Ordering::Greater + } else { + std::cmp::Ordering::Equal + } + }); let __values: Vec<_> = __order .iter() .map(|&__i| std::ptr::read(first.iter_at(__i)))