From 6f551b8c4fec3aeb4a624ddfe20fdad39ce8595d Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Wed, 18 Nov 2026 10:22:12 +0000 Subject: [PATCH 1/2] Translate rewritten operators through rules --- cpp2rust/converter/converter.cpp | 7 ++++++ rules/pair/src_cxx20.cpp | 29 +++++++++++++++++++++ rules/pair/tgt_unsafe.rs | 20 +++++++++++++++ tests/std/utilities/pairs/pairs.pair.cpp | 32 ++++++++++++++++++++++++ 4 files changed, 88 insertions(+) create mode 100644 rules/pair/src_cxx20.cpp diff --git a/cpp2rust/converter/converter.cpp b/cpp2rust/converter/converter.cpp index 244826bf55..49740841b8 100644 --- a/cpp2rust/converter/converter.cpp +++ b/cpp2rust/converter/converter.cpp @@ -2874,6 +2874,13 @@ bool Converter::VisitExplicitCastExpr(clang::ExplicitCastExpr *expr) { bool Converter::VisitCXXRewrittenBinaryOperator( clang::CXXRewrittenBinaryOperator *expr) { + if (Mapper::Contains(ctx_, expr)) { + auto decomposed = expr->getDecomposedForm(); + clang::Expr *args[] = {const_cast(decomposed.LHS), + const_cast(decomposed.RHS)}; + StrCat(GetMappedAsString(expr, args, 2, nullptr)); + return false; + } Convert(expr->getSemanticForm()); return false; } diff --git a/rules/pair/src_cxx20.cpp b/rules/pair/src_cxx20.cpp new file mode 100644 index 0000000000..ddd18d87ae --- /dev/null +++ b/rules/pair/src_cxx20.cpp @@ -0,0 +1,29 @@ +// Copyright (c) 2022-present INESC-ID. +// Distributed under the MIT license that can be found in the LICENSE file. + +#include + +template +bool f28(const std::pair &a0, const std::pair &a1) { + return a0 != a1; +} + +template +bool f29(const std::pair &a0, const std::pair &a1) { + return a0 < a1; +} + +template +bool f30(const std::pair &a0, const std::pair &a1) { + return a0 <= a1; +} + +template +bool f31(const std::pair &a0, const std::pair &a1) { + return a0 > a1; +} + +template +bool f32(const std::pair &a0, const std::pair &a1) { + return a0 >= a1; +} diff --git a/rules/pair/tgt_unsafe.rs b/rules/pair/tgt_unsafe.rs index 04fee13891..bc86f85b72 100644 --- a/rules/pair/tgt_unsafe.rs +++ b/rules/pair/tgt_unsafe.rs @@ -91,3 +91,23 @@ where { &*a0.get::() } + +unsafe fn f28(a0: (T1, T2), a1: (T1, T2)) -> bool { + a0 != a1 +} + +unsafe fn f29(a0: (T1, T2), a1: (T1, T2)) -> bool { + a0 < a1 +} + +unsafe fn f30(a0: (T1, T2), a1: (T1, T2)) -> bool { + a0 <= a1 +} + +unsafe fn f31(a0: (T1, T2), a1: (T1, T2)) -> bool { + a0 > a1 +} + +unsafe fn f32(a0: (T1, T2), a1: (T1, T2)) -> bool { + a0 >= a1 +} diff --git a/tests/std/utilities/pairs/pairs.pair.cpp b/tests/std/utilities/pairs/pairs.pair.cpp index 3098126f5d..19b6b909e1 100644 --- a/tests/std/utilities/pairs/pairs.pair.cpp +++ b/tests/std/utilities/pairs/pairs.pair.cpp @@ -71,10 +71,42 @@ void test() { } } // namespace p40_converting_move_assignment +// N4950 [over.match.oper]/3.4: The rewritten candidate set is determined as +// follows: +// (3.4.1) For the relational (7.6.9) operators, the rewritten candidates +// include all non-rewritten candidates for the expression x <=> y. +// (3.4.3) For the != operator (7.6.10), the rewritten candidates include all +// non-rewritten candidates for the expression x == y that are rewrite targets +// with first operand x (see below). +// N4950 [over.match.oper]/8: The argument list contains all of the operands of +// the operator. The best function from the set of candidate functions is +// selected according to 12.2.3 and 12.2.4. +namespace over_match_oper_p3_4_rewritten_candidates { +void test() { + std::pair a(1, 2); + std::pair b(1, 3); + std::pair c(2, 0); + assert(a != b); + assert(!(a != a)); + 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)); + bool ordered = a < b && b < c; + assert(ordered); +} +} // namespace over_match_oper_p3_4_rewritten_candidates + int main() { p2_move_constructor::test(); p12_forwarding_constructor::test(); p16_converting_move_constructor::test(); p40_converting_move_assignment::test(); + over_match_oper_p3_4_rewritten_candidates::test(); return 0; } From c834ab4a10aab01ec73c8e6fc6067e2d1094659b Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Wed, 18 Nov 2026 11:11:24 +0000 Subject: [PATCH 2/2] Update tests --- .../pairs/out/refcount/pairs.pair.rs | 28 +++++++++++++++++++ .../utilities/pairs/out/unsafe/pairs.pair.rs | 19 +++++++++++++ 2 files changed, 47 insertions(+) diff --git a/tests/std/utilities/pairs/out/refcount/pairs.pair.rs b/tests/std/utilities/pairs/out/refcount/pairs.pair.rs index 38cda8c96e..06e133e4fc 100644 --- a/tests/std/utilities/pairs/out/refcount/pairs.pair.rs +++ b/tests/std/utilities/pairs/out/refcount/pairs.pair.rs @@ -118,6 +118,33 @@ pub fn test_3() { assert!(({ (((*(*target.borrow()).1.borrow()).as_pointer().read()) == 9) })); assert!(({ ((*(*source.borrow()).1.borrow()).as_pointer()).is_null() })); } +pub fn test_4() { + let a: Value<(Value, Value)> = Rc::new(RefCell::new(( + Rc::new(RefCell::new(1)), + Rc::new(RefCell::new(2)), + ))); + let b: Value<(Value, Value)> = Rc::new(RefCell::new(( + Rc::new(RefCell::new(1)), + Rc::new(RefCell::new(3)), + ))); + let c: Value<(Value, Value)> = Rc::new(RefCell::new(( + Rc::new(RefCell::new(2)), + Rc::new(RefCell::new(0)), + ))); + assert!((*a.borrow()) != (*b.borrow())); + assert!(({ !((*a.borrow()) != (*a.borrow())) })); + 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!(ordered); +} pub fn main() { __cpp2rust_init_globals(); std::process::exit(main_0()); @@ -127,6 +154,7 @@ fn main_0() -> i32 { ({ test_1() }); ({ test_2() }); ({ test_3() }); + ({ test_4() }); return 0; } pub fn __cpp2rust_init_globals() {} diff --git a/tests/std/utilities/pairs/out/unsafe/pairs.pair.rs b/tests/std/utilities/pairs/out/unsafe/pairs.pair.rs index a2fe964c8d..708b715174 100644 --- a/tests/std/utilities/pairs/out/unsafe/pairs.pair.rs +++ b/tests/std/utilities/pairs/out/unsafe/pairs.pair.rs @@ -133,6 +133,24 @@ pub unsafe fn test_3() { .is_null() ); } +pub unsafe fn test_4() { + let mut a: (i32, i32) = (1, 2); + let mut b: (i32, i32) = (1, 3); + let mut c: (i32, i32) = (2, 0); + assert!(a != b); + assert!(!(a != a)); + 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!(ordered); +} pub fn main() { unsafe { __cpp2rust_init_globals(); @@ -144,6 +162,7 @@ unsafe fn main_0() -> i32 { (unsafe { test_1() }); (unsafe { test_2() }); (unsafe { test_3() }); + (unsafe { test_4() }); return 0; } pub unsafe fn __cpp2rust_init_globals() {}