Skip to content

Commit 5be4f50

Browse files
authored
Fix Ord codegen to use other < self in reverse branch (#170)
The generated `Ord::cmp` for method-based `operator<` used `other.<op>(other)` in its second branch, which can never evaluate true and therefore suppresses `Ordering::Greater`. This change aligns that branch with the intended reverse comparison (`other.<op>(self)`), matching existing model-specific behavior. Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
1 parent cee15a3 commit 5be4f50

2 files changed

Lines changed: 3 additions & 3 deletions

File tree

cpp2rust/converter/converter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3685,7 +3685,7 @@ void Converter::ConvertOrdAndPartialOrdTraits(const clang::CXXRecordDecl *decl,
36853685
case clang::OO_Less:
36863686
if (clang::isa<clang::CXXMethodDecl>(op)) {
36873687
first_branch = std::format("self.{}(other)", GetOverloadedOperator(op));
3688-
second_branch = std::format("other.{}(other)", GetOverloadedOperator(op));
3688+
second_branch = std::format("other.{}(self)", GetOverloadedOperator(op));
36893689
} else {
36903690
first_branch = std::format("{}(self, other)", GetOverloadedOperator(op));
36913691
second_branch = std::format("{}(other, self)", GetOverloadedOperator(op));

tests/unit/out/unsafe/operator_less_than.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ impl Ord for Pair {
2323
unsafe {
2424
if self.lt(other) {
2525
std::cmp::Ordering::Less
26-
} else if other.lt(other) {
26+
} else if other.lt(self) {
2727
std::cmp::Ordering::Greater
2828
} else {
2929
std::cmp::Ordering::Equal
@@ -38,7 +38,7 @@ impl PartialOrd for Pair {
3838
}
3939
impl PartialEq for Pair {
4040
fn eq(&self, other: &Self) -> bool {
41-
unsafe { !(self.lt(other)) && !(other.lt(other)) }
41+
unsafe { !(self.lt(other)) && !(other.lt(self)) }
4242
}
4343
}
4444
impl Eq for Pair {}

0 commit comments

Comments
 (0)