Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 35 additions & 5 deletions cpp2rust/converter/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4643,6 +4643,32 @@ std::string Converter::GetComparisonCall(const clang::FunctionDecl *op,
arg(0, lhs), arg(1, rhs));
}

std::string Converter::GetComparisonBool(const clang::FunctionDecl *op,
const clang::CXXRecordDecl *decl,
std::string_view lhs,
std::string_view rhs) {
auto call = GetComparisonCall(op, decl, lhs, rhs);
auto ret = op->getReturnType();
if (ret->isBooleanType()) {
return call;
}
if (ret->isIntegerType()) {
return std::format("(({}) != 0)", call);
}
const auto *record = ret->getAsCXXRecordDecl();
assert(record && "unsupported comparison operator return type");
for (const auto *found : record->getVisibleConversionFunctions()) {
const auto *conversion = clang::dyn_cast<clang::CXXConversionDecl>(found);
if (conversion && conversion->getConversionType()->isBooleanType()) {
return std::format("{{ let __cmp = &{}; {}::{}({}) }}", call,
GetUFCSName(conversion), GetMethodName(conversion),
GetComparisonReceiver(conversion, record, "__cmp"));
}
}
assert(0 && "comparison operator result has no conversion to bool");
return call;
}

std::string
Converter::GetComparisonReferenceArg(const clang::CXXRecordDecl *decl,
std::string_view value) {
Expand Down Expand Up @@ -4670,19 +4696,19 @@ void Converter::ConvertOrdAndPartialOrdTraits(const clang::CXXRecordDecl *decl,
cmp_body = std::format("if {} {{ std::cmp::Ordering::Less }} else if {} {{ "
"std::cmp::Ordering::Greater }} else {{ "
"std::cmp::Ordering::Equal }}",
GetComparisonCall(lt, decl, "self", "other"),
GetComparisonCall(lt, decl, "other", "self"));
GetComparisonBool(lt, decl, "self", "other"),
GetComparisonBool(lt, decl, "other", "self"));
}

if (eq) {
eq_body = GetComparisonCall(eq, decl, "self", "other");
eq_body = GetComparisonBool(eq, decl, "self", "other");
} else if (cmp) {
eq_body = std::format("{} == std::cmp::Ordering::Equal",
GetComparisonCall(cmp, decl, "self", "other"));
} else {
eq_body = std::format("!({}) && !({})",
GetComparisonCall(lt, decl, "self", "other"),
GetComparisonCall(lt, decl, "other", "self"));
GetComparisonBool(lt, decl, "self", "other"),
GetComparisonBool(lt, decl, "other", "self"));
}

ConvertOrdAndPartialOrdTraitsBase(cmp_body, eq_body, GetRecordName(decl));
Expand All @@ -4697,6 +4723,10 @@ void Converter::AddOrdTrait(const clang::CXXRecordDecl *decl) {
fn->getDescribedFunctionTemplate() || !IsSameTypeComparison(fn, decl)) {
return;
}
if (fn->getOverloadedOperator() == clang::OO_Spaceship &&
!ctx_.CompCategories.lookupInfoForType(fn->getReturnType())) {
return;
}
switch (fn->getOverloadedOperator()) {
case clang::OO_EqualEqual:
eq = fn;
Expand Down
4 changes: 4 additions & 0 deletions cpp2rust/converter/converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,10 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
const clang::CXXRecordDecl *decl,
std::string_view lhs, std::string_view rhs);

std::string GetComparisonBool(const clang::FunctionDecl *op,
const clang::CXXRecordDecl *decl,
std::string_view lhs, std::string_view rhs);

virtual std::string
GetComparisonReferenceArg(const clang::CXXRecordDecl *decl,
std::string_view value);
Expand Down
80 changes: 80 additions & 0 deletions tests/unit/operator_comparison_nonbool.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// ADDITIONAL_COMPILE_FLAGS: -std=c++20
#include <algorithm>
#include <cassert>

struct X {
int v;
};

int operator==(X a, X b) { return a.v == b.v ? 2 : 0; }
int operator!=(X a, X b) { return a.v != b.v ? 3 : 0; }
int operator<(X a, X b) { return a.v < b.v ? 4 : 0; }
int operator>(X a, X b) { return a.v > b.v ? 5 : 0; }
int operator<=(X a, X b) { return a.v <= b.v ? 6 : 0; }
int operator>=(X a, X b) { return a.v >= b.v ? 7 : 0; }

struct Result {
int r;
};

struct Custom {
int v;
Result operator<=>(const Custom &o) const { return Result{v - o.v}; }
};

struct Mixed {
int v;
Result operator<=>(const Mixed &o) const { return Result{v - o.v}; }
int operator<(const Mixed &o) const { return v < o.v ? 8 : 0; }
int operator==(const Mixed &o) const { return v == o.v ? 9 : 0; }
};

struct MyBool {
bool value;
explicit MyBool(bool v) : value(v) {}
operator bool() const { return value; }
};

struct Boolish {
int v;
};

MyBool operator==(Boolish a, Boolish b) { return MyBool(a.v == b.v); }
MyBool operator<(Boolish a, Boolish b) { return MyBool(a.v < b.v); }

int main() {
X a{1}, b{2}, c{1};
assert((a == c) == 2);
assert((a == b) == 0);
assert((a != b) == 3);
assert((a < b) == 4);
assert((b > a) == 5);
assert((a <= c) == 6);
assert((a >= c) == 7);

X xs[] = {{3}, {1}, {2}};
std::sort(xs, xs + 3);
assert(xs[0].v == 1 && xs[1].v == 2 && xs[2].v == 3);

Custom p{5}, q{2};
Result r = p <=> q;
assert(r.r == 3);

Mixed m{4}, n{4};
assert((m == n) == 9);
assert((m <=> n).r == 0);
Mixed ms[] = {{6}, {4}, {5}};
std::sort(ms, ms + 3);
assert(ms[0].v == 4 && ms[1].v == 5 && ms[2].v == 6);
assert((ms[0] < ms[1]) == 8);

Boolish b1{1}, b2{2};
assert(b1 == Boolish{1});
assert(!(b2 < b1));
MyBool lt = b1 < b2;
assert(lt.value);
Boolish bs[] = {{3}, {1}, {2}};
std::sort(bs, bs + 3);
assert(bs[0].v == 1 && bs[1].v == 2 && bs[2].v == 3);
return 0;
}
Loading
Loading