From d6fe523178133854321f5d61dbc656e76e806c8f Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Wed, 18 Nov 2026 17:28:27 +0000 Subject: [PATCH 1/4] Return the strong_ordering constants by reference --- .../converter/models/converter_refcount.cpp | 5 +++ libcc2rs/src/cmp.rs | 34 ++++++++++++++++++- rules/compare/src_cxx20.cpp | 8 ++--- rules/compare/tgt_refcount.rs | 20 +++++++++++ rules/compare/tgt_unsafe.rs | 16 ++++----- tests/std/support/cmp/cmp.strongord.cpp | 26 ++++++++++++++ 6 files changed, 96 insertions(+), 13 deletions(-) create mode 100644 rules/compare/tgt_refcount.rs create mode 100644 tests/std/support/cmp/cmp.strongord.cpp diff --git a/cpp2rust/converter/models/converter_refcount.cpp b/cpp2rust/converter/models/converter_refcount.cpp index dd14dbc3eb..07746dd9b3 100644 --- a/cpp2rust/converter/models/converter_refcount.cpp +++ b/cpp2rust/converter/models/converter_refcount.cpp @@ -995,6 +995,11 @@ void ConverterRefCount::ConvertDeclRefValue(clang::Expr *expr, if (ShouldReplaceWithMappedBody(decl)) { if (auto str = GetMappedAsString(expr); !str.empty()) { + if (RuleRegistry::ReturnsReference(expr) && !isAddrOf() && + !Mapper::IsRefExpr(ctx_, expr)) { + EmitPointee(str, expr->getType(), /*fresh=*/true); + return; + } EmitFresh(str); return; } diff --git a/libcc2rs/src/cmp.rs b/libcc2rs/src/cmp.rs index 427fdf1a0c..8005cb4409 100644 --- a/libcc2rs/src/cmp.rs +++ b/libcc2rs/src/cmp.rs @@ -4,11 +4,43 @@ // The comparisons that the C++ standard library does with operator< and // operator==, in the refcount model. +use std::cell::RefCell; use std::cmp::Ordering; +use std::rc::Rc; -use crate::rc::Ptr; +use crate::rc::{AsPointer, Ptr, Value}; use crate::reinterpret::ByteRepr; +impl ByteRepr for Ordering {} + +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; + +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)); +} + +pub fn strong_ordering_less() -> Ptr { + STRONG_ORDERING_LESS_VALUE.with(AsPointer::as_pointer) +} + +pub fn strong_ordering_equal() -> Ptr { + STRONG_ORDERING_EQUAL_VALUE.with(AsPointer::as_pointer) +} + +pub fn strong_ordering_equivalent() -> Ptr { + STRONG_ORDERING_EQUIVALENT_VALUE.with(AsPointer::as_pointer) +} + +pub fn strong_ordering_greater() -> Ptr { + STRONG_ORDERING_GREATER_VALUE.with(AsPointer::as_pointer) +} + // operator<. Records implement it with their comparison operators. pub trait CppLess: Sized { fn less(lhs: &Ptr, rhs: &Ptr) -> bool; diff --git a/rules/compare/src_cxx20.cpp b/rules/compare/src_cxx20.cpp index d02c488707..ad2ac8175c 100644 --- a/rules/compare/src_cxx20.cpp +++ b/rules/compare/src_cxx20.cpp @@ -5,13 +5,13 @@ typedef std::strong_ordering t1; -std::strong_ordering f1() { return std::strong_ordering::less; } +const std::strong_ordering &f1() { return std::strong_ordering::less; } -std::strong_ordering f2() { return std::strong_ordering::equal; } +const std::strong_ordering &f2() { return std::strong_ordering::equal; } -std::strong_ordering f3() { return std::strong_ordering::equivalent; } +const std::strong_ordering &f3() { return std::strong_ordering::equivalent; } -std::strong_ordering f4() { return std::strong_ordering::greater; } +const std::strong_ordering &f4() { return std::strong_ordering::greater; } bool f5(std::strong_ordering a0, std::strong_ordering a1) { return a0 == a1; } diff --git a/rules/compare/tgt_refcount.rs b/rules/compare/tgt_refcount.rs new file mode 100644 index 0000000000..356d20af01 --- /dev/null +++ b/rules/compare/tgt_refcount.rs @@ -0,0 +1,20 @@ +// Copyright (c) 2022-present INESC-ID. +// Distributed under the MIT license that can be found in the LICENSE file. + +use libcc2rs::*; + +fn f1() -> Ptr { + libcc2rs::strong_ordering_less() +} + +fn f2() -> Ptr { + libcc2rs::strong_ordering_equal() +} + +fn f3() -> Ptr { + libcc2rs::strong_ordering_equivalent() +} + +fn f4() -> Ptr { + libcc2rs::strong_ordering_greater() +} diff --git a/rules/compare/tgt_unsafe.rs b/rules/compare/tgt_unsafe.rs index a4c0820763..315dea75b1 100644 --- a/rules/compare/tgt_unsafe.rs +++ b/rules/compare/tgt_unsafe.rs @@ -5,20 +5,20 @@ fn t1() -> std::cmp::Ordering { std::cmp::Ordering::Equal } -unsafe fn f1() -> std::cmp::Ordering { - std::cmp::Ordering::Less +unsafe fn f1() -> *const std::cmp::Ordering { + &raw const libcc2rs::STRONG_ORDERING_LESS } -unsafe fn f2() -> std::cmp::Ordering { - std::cmp::Ordering::Equal +unsafe fn f2() -> *const std::cmp::Ordering { + &raw const libcc2rs::STRONG_ORDERING_EQUAL } -unsafe fn f3() -> std::cmp::Ordering { - std::cmp::Ordering::Equal +unsafe fn f3() -> *const std::cmp::Ordering { + &raw const libcc2rs::STRONG_ORDERING_EQUIVALENT } -unsafe fn f4() -> std::cmp::Ordering { - std::cmp::Ordering::Greater +unsafe fn f4() -> *const std::cmp::Ordering { + &raw const libcc2rs::STRONG_ORDERING_GREATER } unsafe fn f5(a0: std::cmp::Ordering, a1: std::cmp::Ordering) -> bool { diff --git a/tests/std/support/cmp/cmp.strongord.cpp b/tests/std/support/cmp/cmp.strongord.cpp new file mode 100644 index 0000000000..74891698e8 --- /dev/null +++ b/tests/std/support/cmp/cmp.strongord.cpp @@ -0,0 +1,26 @@ +// ADDITIONAL_COMPILE_FLAGS: -std=c++20 +#include +#include + +// N4950 [cmp.strongord]: "static const strong_ordering less; +// static const strong_ordering equal; static const strong_ordering equivalent; +// static const strong_ordering greater;" A reference bound to a valid value +// refers to that static data member. +namespace valid_values { +void test() { + const std::strong_ordering &l = std::strong_ordering::less; + const std::strong_ordering &e = std::strong_ordering::equal; + const std::strong_ordering &g = std::strong_ordering::greater; + assert(&l == &std::strong_ordering::less); + assert(&e == &std::strong_ordering::equal); + assert(&g == &std::strong_ordering::greater); + assert(l < 0); + assert(e == 0); + assert(g > 0); +} +} // namespace valid_values + +int main() { + valid_values::test(); + return 0; +} From a2b90a7f117cb3c6c473013717fd5bf2a01ac272 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Wed, 18 Nov 2026 17:50:39 +0000 Subject: [PATCH 2/4] Update tests --- .../support/cmp/out/refcount/cmp.strongord.rs | 28 +++++++++++++++++ .../support/cmp/out/unsafe/cmp.strongord.rs | 30 +++++++++++++++++++ .../refcount/operator_comparison_defaulted.rs | 23 +++++++------- tests/unit/out/refcount/operator_friend.rs | 4 +-- tests/unit/out/refcount/operator_three_way.rs | 9 +++--- .../unsafe/operator_comparison_defaulted.rs | 22 +++++++------- tests/unit/out/unsafe/operator_friend.rs | 4 +-- tests/unit/out/unsafe/operator_three_way.rs | 8 ++--- 8 files changed, 94 insertions(+), 34 deletions(-) create mode 100644 tests/std/support/cmp/out/refcount/cmp.strongord.rs create mode 100644 tests/std/support/cmp/out/unsafe/cmp.strongord.rs diff --git a/tests/std/support/cmp/out/refcount/cmp.strongord.rs b/tests/std/support/cmp/out/refcount/cmp.strongord.rs new file mode 100644 index 0000000000..53cee646d2 --- /dev/null +++ b/tests/std/support/cmp/out/refcount/cmp.strongord.rs @@ -0,0 +1,28 @@ +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 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); +} +pub fn main() { + __cpp2rust_init_globals(); + std::process::exit(main_0()); +} +fn main_0() -> i32 { + ({ test_0() }); + return 0; +} +pub fn __cpp2rust_init_globals() {} diff --git a/tests/std/support/cmp/out/unsafe/cmp.strongord.rs b/tests/std/support/cmp/out/unsafe/cmp.strongord.rs new file mode 100644 index 0000000000..b782ceef67 --- /dev/null +++ b/tests/std/support/cmp/out/unsafe/cmp.strongord.rs @@ -0,0 +1,30 @@ +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 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); +} +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + (unsafe { test_0() }); + return 0; +} +pub unsafe fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/operator_comparison_defaulted.rs b/tests/unit/out/refcount/operator_comparison_defaulted.rs index a416e310cc..5dbd01cc76 100644 --- a/tests/unit/out/refcount/operator_comparison_defaulted.rs +++ b/tests/unit/out/refcount/operator_comparison_defaulted.rs @@ -165,7 +165,8 @@ fn main_0() -> i32 { }) ); assert!( - ({ CmpImpl::operator_cmp(&c1.as_pointer(), c2.as_pointer(),) }) == std::cmp::Ordering::Less + ({ CmpImpl::operator_cmp(&c1.as_pointer(), c2.as_pointer(),) }) + == ({ (*libcc2rs::strong_ordering_less().upgrade().deref()) }) ); let b1: Value = Rc::new(RefCell::new(Both { a: 1 })); let b2: Value = Rc::new(RefCell::new(Both { a: 2 })); @@ -187,7 +188,7 @@ fn main_0() -> i32 { ); assert!( ({ OrdOnlyImpl::operator_cmp(&o2.as_pointer(), o1.as_pointer(),) }) - == std::cmp::Ordering::Greater + == ({ (*libcc2rs::strong_ordering_greater().upgrade().deref()) }) ); let x1: Value = Rc::new(RefCell::new(Outer { i: Inner { x: 1 }, @@ -208,7 +209,7 @@ fn main_0() -> i32 { assert!(({ OuterImpl::operator_eq(&x1.as_pointer(), x3.as_pointer(),) })); assert!( ({ OuterImpl::operator_cmp(&x2.as_pointer(), x1.as_pointer(),) }) - == std::cmp::Ordering::Greater + == ({ (*libcc2rs::strong_ordering_greater().upgrade().deref()) }) ); let s1: Value = Rc::new(RefCell::new(Secondary { a: 1 })); let s2: Value = Rc::new(RefCell::new(Secondary { a: 2 })); @@ -229,7 +230,7 @@ fn main_0() -> i32 { assert!(({ PtrMemberImpl::operator_eq(&p1.as_pointer(), p3.as_pointer(),) })); assert!( ({ PtrMemberImpl::operator_cmp(&p2.as_pointer(), p1.as_pointer(),) }) - == std::cmp::Ordering::Greater + == ({ (*libcc2rs::strong_ordering_greater().upgrade().deref()) }) ); return 0; } @@ -251,7 +252,7 @@ impl BothImpl for Ptr { return (*cmp.borrow_mut()); } } - return std::cmp::Ordering::Equal; + return (*libcc2rs::strong_ordering_equal().upgrade().deref()); } } pub trait CmpImpl { @@ -278,7 +279,7 @@ impl CmpImpl for Ptr { return (*cmp.borrow_mut()); } } - return std::cmp::Ordering::Equal; + return (*libcc2rs::strong_ordering_equal().upgrade().deref()); } fn operator_eq(&self, _a0: Ptr) -> bool { return ({ (*self).with(|__s| __s.a) } == { _a0.with(|__s| __s.a) }) @@ -309,7 +310,7 @@ impl InnerImpl for Ptr { return (*cmp.borrow_mut()); } } - return std::cmp::Ordering::Equal; + return (*libcc2rs::strong_ordering_equal().upgrade().deref()); } fn operator_eq(&self, _a0: Ptr) -> bool { return ({ (*self).with(|__s| __s.x) } == { _a0.with(|__s| __s.x) }); @@ -329,7 +330,7 @@ impl OrdOnlyImpl for Ptr { return (*cmp.borrow_mut()); } } - return std::cmp::Ordering::Equal; + return (*libcc2rs::strong_ordering_equal().upgrade().deref()); } } pub trait OuterImpl { @@ -358,7 +359,7 @@ impl OuterImpl for Ptr { return (*cmp.borrow_mut()); } } - return std::cmp::Ordering::Equal; + return (*libcc2rs::strong_ordering_equal().upgrade().deref()); } fn operator_eq(&self, _a0: Ptr) -> bool { return ({ @@ -382,7 +383,7 @@ impl PtrMemberImpl for Ptr { return (*cmp.borrow_mut()); } } - return std::cmp::Ordering::Equal; + return (*libcc2rs::strong_ordering_equal().upgrade().deref()); } fn operator_eq(&self, _a0: Ptr) -> bool { return ({ (*self).with(|__s| __s.p.clone()) } == { _a0.with(|__s| __s.p.clone()) }); @@ -415,7 +416,7 @@ impl SecondaryImpl for Ptr { return (*cmp.borrow_mut()); } } - return std::cmp::Ordering::Equal; + return (*libcc2rs::strong_ordering_equal().upgrade().deref()); } fn operator_lt(&self, _a0: Ptr) -> bool { return ({ diff --git a/tests/unit/out/refcount/operator_friend.rs b/tests/unit/out/refcount/operator_friend.rs index 5b2aa5ce8d..2bb9857c18 100644 --- a/tests/unit/out/refcount/operator_friend.rs +++ b/tests/unit/out/refcount/operator_friend.rs @@ -33,7 +33,7 @@ pub fn operator_cmp_1(_a0: Ptr, _a1: Ptr) -> std::cm return (*cmp.borrow_mut()); } } - return std::cmp::Ordering::Equal; + return (*libcc2rs::strong_ordering_equal().upgrade().deref()); } pub fn operator_eq_2(_a0: Ptr, _a1: Ptr) -> bool { return ({ _a0.with(|__s| __s.a) } == { _a1.with(|__s| __s.a) }); @@ -198,7 +198,7 @@ fn main_0() -> i32 { ({ let _arg0: Ptr = o1.as_pointer(); operator_cmp_1(_arg0, o2.as_pointer()) - }) == std::cmp::Ordering::Less + }) == ({ (*libcc2rs::strong_ordering_less().upgrade().deref()) }) ); let i1: Value = Rc::new(RefCell::new(Inline { a: 1 })); let i2: Value = Rc::new(RefCell::new(Inline { a: 2 })); diff --git a/tests/unit/out/refcount/operator_three_way.rs b/tests/unit/out/refcount/operator_three_way.rs index f4c5970d6a..9a7d2c1750 100644 --- a/tests/unit/out/refcount/operator_three_way.rs +++ b/tests/unit/out/refcount/operator_three_way.rs @@ -43,7 +43,8 @@ fn main_0() -> i32 { ); assert!(!({ SImpl::operator_eq(&a.as_pointer(), b.as_pointer(),) })); assert!( - ({ SImpl::operator_cmp(&a.as_pointer(), b.as_pointer(),) }) == std::cmp::Ordering::Less + ({ SImpl::operator_cmp(&a.as_pointer(), b.as_pointer(),) }) + == ({ (*libcc2rs::strong_ordering_less().upgrade().deref()) }) ); return 0; } @@ -54,12 +55,12 @@ pub trait SImpl { impl SImpl for Ptr { fn operator_cmp(&self, o: Ptr) -> std::cmp::Ordering { if ({ (*self).with(|__s| __s.v) } < { o.with(|__s| __s.v) }) { - return std::cmp::Ordering::Less; + return (*libcc2rs::strong_ordering_less().upgrade().deref()); } if ({ (*self).with(|__s| __s.v) } > { o.with(|__s| __s.v) }) { - return std::cmp::Ordering::Greater; + return (*libcc2rs::strong_ordering_greater().upgrade().deref()); } - return std::cmp::Ordering::Equal; + return (*libcc2rs::strong_ordering_equal().upgrade().deref()); } fn operator_eq(&self, o: Ptr) -> bool { return ({ (*self).with(|__s| __s.v) } == { o.with(|__s| __s.v) }); diff --git a/tests/unit/out/unsafe/operator_comparison_defaulted.rs b/tests/unit/out/unsafe/operator_comparison_defaulted.rs index 552887b490..2073299cad 100644 --- a/tests/unit/out/unsafe/operator_comparison_defaulted.rs +++ b/tests/unit/out/unsafe/operator_comparison_defaulted.rs @@ -46,7 +46,7 @@ impl Cmp { return cmp; } } - return std::cmp::Ordering::Equal; + return libcc2rs::STRONG_ORDERING_EQUAL; } pub unsafe fn operator_eq(&self, _a0: *const Cmp) -> bool { return (((*(self as *const Cmp)).a) == ((*_a0).a)) @@ -86,7 +86,7 @@ impl Both { return cmp; } } - return std::cmp::Ordering::Equal; + return libcc2rs::STRONG_ORDERING_EQUAL; } } impl std::cmp::Ord for Both { @@ -119,7 +119,7 @@ impl OrdOnly { return cmp; } } - return std::cmp::Ordering::Equal; + return libcc2rs::STRONG_ORDERING_EQUAL; } } impl std::cmp::Ord for OrdOnly { @@ -152,7 +152,7 @@ impl Inner { return cmp; } } - return std::cmp::Ordering::Equal; + return libcc2rs::STRONG_ORDERING_EQUAL; } pub unsafe fn operator_eq(&self, _a0: *const Inner) -> bool { return (((*(self as *const Inner)).x) == ((*_a0).x)); @@ -198,7 +198,7 @@ impl Outer { return cmp; } } - return std::cmp::Ordering::Equal; + return libcc2rs::STRONG_ORDERING_EQUAL; } pub unsafe fn operator_eq(&self, _a0: *const Outer) -> bool { return (unsafe { @@ -246,7 +246,7 @@ impl Secondary { return cmp; } } - return std::cmp::Ordering::Equal; + return libcc2rs::STRONG_ORDERING_EQUAL; } pub unsafe fn operator_lt(&self, _a0: *const Secondary) -> bool { return (unsafe { @@ -291,7 +291,7 @@ impl PtrMember { return cmp; } } - return std::cmp::Ordering::Equal; + return libcc2rs::STRONG_ORDERING_EQUAL; } pub unsafe fn operator_eq(&self, _a0: *const PtrMember) -> bool { return (((*(self as *const PtrMember)).p) == ((*_a0).p)); @@ -339,7 +339,7 @@ unsafe fn main_0() -> i32 { Cmp::operator_eq(&c1, _arg0) }) ); - assert!((unsafe { Cmp::operator_cmp(&c1, &c2,) }) == std::cmp::Ordering::Less); + assert!((unsafe { Cmp::operator_cmp(&c1, &c2,) }) == libcc2rs::STRONG_ORDERING_LESS); let mut b1: Both = Both { a: 1 }; let mut b2: Both = Both { a: 2 }; assert!((unsafe { Both::operator_cmp(&b1, &b2,) }) == std::cmp::Ordering::Less); @@ -352,7 +352,7 @@ unsafe fn main_0() -> i32 { let mut o1: OrdOnly = OrdOnly { a: 1 }; let mut o2: OrdOnly = OrdOnly { a: 2 }; assert!((unsafe { OrdOnly::operator_cmp(&o1, &o2,) }) == std::cmp::Ordering::Less); - assert!((unsafe { OrdOnly::operator_cmp(&o2, &o1,) }) == std::cmp::Ordering::Greater); + assert!((unsafe { OrdOnly::operator_cmp(&o2, &o1,) }) == libcc2rs::STRONG_ORDERING_GREATER); let mut x1: Outer = Outer { i: Inner { x: 1 }, y: 9, @@ -367,7 +367,7 @@ unsafe fn main_0() -> i32 { }; assert!((unsafe { Outer::operator_cmp(&x1, &x2,) }) == std::cmp::Ordering::Less); assert!((unsafe { Outer::operator_eq(&x1, &x3,) })); - assert!((unsafe { Outer::operator_cmp(&x2, &x1,) }) == std::cmp::Ordering::Greater); + assert!((unsafe { Outer::operator_cmp(&x2, &x1,) }) == libcc2rs::STRONG_ORDERING_GREATER); let mut s1: Secondary = Secondary { a: 1 }; let mut s2: Secondary = Secondary { a: 2 }; assert!((unsafe { Secondary::operator_ne(&s1, &s2,) })); @@ -386,7 +386,7 @@ unsafe fn main_0() -> i32 { }; assert!((unsafe { PtrMember::operator_cmp(&p1, &p2,) }) == std::cmp::Ordering::Less); assert!((unsafe { PtrMember::operator_eq(&p1, &p3,) })); - assert!((unsafe { PtrMember::operator_cmp(&p2, &p1,) }) == std::cmp::Ordering::Greater); + assert!((unsafe { PtrMember::operator_cmp(&p2, &p1,) }) == libcc2rs::STRONG_ORDERING_GREATER); return 0; } pub unsafe fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/unsafe/operator_friend.rs b/tests/unit/out/unsafe/operator_friend.rs index 98c57d3f78..9135d1d08f 100644 --- a/tests/unit/out/unsafe/operator_friend.rs +++ b/tests/unit/out/unsafe/operator_friend.rs @@ -31,7 +31,7 @@ pub unsafe fn operator_cmp_1( return cmp; } } - return std::cmp::Ordering::Equal; + return libcc2rs::STRONG_ORDERING_EQUAL; } pub unsafe fn operator_eq_2(_a0: *const DefaultedOrd, _a1: *const DefaultedOrd) -> bool { return (((*_a0).a) == ((*_a1).a)); @@ -236,7 +236,7 @@ unsafe fn main_0() -> i32 { (unsafe { let _arg0: *const DefaultedOrd = &o1; operator_cmp_1(_arg0, &o2) - }) == std::cmp::Ordering::Less + }) == libcc2rs::STRONG_ORDERING_LESS ); let mut i1: Inline = Inline { a: 1 }; let mut i2: Inline = Inline { a: 2 }; diff --git a/tests/unit/out/unsafe/operator_three_way.rs b/tests/unit/out/unsafe/operator_three_way.rs index b92a6d3249..a1fdc0f2c2 100644 --- a/tests/unit/out/unsafe/operator_three_way.rs +++ b/tests/unit/out/unsafe/operator_three_way.rs @@ -14,12 +14,12 @@ pub struct S { impl S { pub unsafe fn operator_cmp(&self, o: *const S) -> std::cmp::Ordering { if ((self.v) < ((*o).v)) { - return std::cmp::Ordering::Less; + return libcc2rs::STRONG_ORDERING_LESS; } if ((self.v) > ((*o).v)) { - return std::cmp::Ordering::Greater; + return libcc2rs::STRONG_ORDERING_GREATER; } - return std::cmp::Ordering::Equal; + return libcc2rs::STRONG_ORDERING_EQUAL; } pub unsafe fn operator_eq(&self, o: *const S) -> bool { return ((self.v) == ((*o).v)); @@ -55,7 +55,7 @@ unsafe fn main_0() -> i32 { assert!((unsafe { S::operator_cmp(&a, &b,) }) != std::cmp::Ordering::Greater); assert!((unsafe { S::operator_cmp(&b, &a,) }) != std::cmp::Ordering::Less); assert!(!(unsafe { S::operator_eq(&a, &b,) })); - assert!((unsafe { S::operator_cmp(&a, &b,) }) == std::cmp::Ordering::Less); + assert!((unsafe { S::operator_cmp(&a, &b,) }) == libcc2rs::STRONG_ORDERING_LESS); return 0; } pub unsafe fn __cpp2rust_init_globals() {} From 1c8fcf871bd82d0003b22317c4405cd5389374be Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Thu, 19 Nov 2026 09:54:27 +0000 Subject: [PATCH 3/4] Move the strong_ordering constants into the compare shim --- libcc2rs/src/cmp.rs | 34 +--------------------------------- rules/compare/shim.rs | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 34 deletions(-) diff --git a/libcc2rs/src/cmp.rs b/libcc2rs/src/cmp.rs index 8005cb4409..427fdf1a0c 100644 --- a/libcc2rs/src/cmp.rs +++ b/libcc2rs/src/cmp.rs @@ -4,43 +4,11 @@ // The comparisons that the C++ standard library does with operator< and // operator==, in the refcount model. -use std::cell::RefCell; use std::cmp::Ordering; -use std::rc::Rc; -use crate::rc::{AsPointer, Ptr, Value}; +use crate::rc::Ptr; use crate::reinterpret::ByteRepr; -impl ByteRepr for Ordering {} - -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; - -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)); -} - -pub fn strong_ordering_less() -> Ptr { - STRONG_ORDERING_LESS_VALUE.with(AsPointer::as_pointer) -} - -pub fn strong_ordering_equal() -> Ptr { - STRONG_ORDERING_EQUAL_VALUE.with(AsPointer::as_pointer) -} - -pub fn strong_ordering_equivalent() -> Ptr { - STRONG_ORDERING_EQUIVALENT_VALUE.with(AsPointer::as_pointer) -} - -pub fn strong_ordering_greater() -> Ptr { - STRONG_ORDERING_GREATER_VALUE.with(AsPointer::as_pointer) -} - // operator<. Records implement it with their comparison operators. pub trait CppLess: Sized { fn less(lhs: &Ptr, rhs: &Ptr) -> bool; diff --git a/rules/compare/shim.rs b/rules/compare/shim.rs index a614f8870f..39e3b88030 100644 --- a/rules/compare/shim.rs +++ b/rules/compare/shim.rs @@ -1,7 +1,11 @@ // Copyright (c) 2022-present INESC-ID. // Distributed under the MIT license that can be found in the LICENSE file. -use crate::{ArgRepr, ByteRepr, FnPtrArg, record_from_repr}; +use std::cell::RefCell; +use std::cmp::Ordering; +use std::rc::Rc; + +use crate::{ArgRepr, AsPointer, ByteRepr, FnPtrArg, Ptr, Value, record_from_repr}; impl ByteRepr for std::cmp::Ordering {} @@ -14,3 +18,31 @@ impl FnPtrArg for std::cmp::Ordering { record_from_repr(r) } } + +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; + +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)); +} + +pub fn strong_ordering_less() -> Ptr { + STRONG_ORDERING_LESS_VALUE.with(AsPointer::as_pointer) +} + +pub fn strong_ordering_equal() -> Ptr { + STRONG_ORDERING_EQUAL_VALUE.with(AsPointer::as_pointer) +} + +pub fn strong_ordering_equivalent() -> Ptr { + STRONG_ORDERING_EQUIVALENT_VALUE.with(AsPointer::as_pointer) +} + +pub fn strong_ordering_greater() -> Ptr { + STRONG_ORDERING_GREATER_VALUE.with(AsPointer::as_pointer) +} From a8a836041577fa713424a2325463f11211ab496d Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Thu, 19 Nov 2026 09:54:35 +0000 Subject: [PATCH 4/4] Update tests --- tests/std/support/cmp/out/refcount/cmp.strongord.rs | 2 +- tests/std/support/cmp/out/unsafe/cmp.strongord.rs | 2 +- tests/unit/out/refcount/fn_ptr.rs | 5 ++++- tests/unit/out/refcount/operator_comparison_defaulted.rs | 2 +- tests/unit/out/refcount/operator_friend.rs | 2 +- tests/unit/out/refcount/operator_three_way.rs | 2 +- tests/unit/out/unsafe/fn_ptr.rs | 2 +- tests/unit/out/unsafe/operator_comparison_defaulted.rs | 2 +- tests/unit/out/unsafe/operator_friend.rs | 2 +- tests/unit/out/unsafe/operator_three_way.rs | 2 +- 10 files changed, 13 insertions(+), 10 deletions(-) diff --git a/tests/std/support/cmp/out/refcount/cmp.strongord.rs b/tests/std/support/cmp/out/refcount/cmp.strongord.rs index 53cee646d2..93431ba092 100644 --- a/tests/std/support/cmp/out/refcount/cmp.strongord.rs +++ b/tests/std/support/cmp/out/refcount/cmp.strongord.rs @@ -19,7 +19,7 @@ pub fn test_0() { } pub fn main() { __cpp2rust_init_globals(); - std::process::exit(main_0()); + libcc2rs::cpp2rust_exit(main_0()); } fn main_0() -> i32 { ({ test_0() }); diff --git a/tests/std/support/cmp/out/unsafe/cmp.strongord.rs b/tests/std/support/cmp/out/unsafe/cmp.strongord.rs index b782ceef67..ef98961755 100644 --- a/tests/std/support/cmp/out/unsafe/cmp.strongord.rs +++ b/tests/std/support/cmp/out/unsafe/cmp.strongord.rs @@ -20,7 +20,7 @@ pub unsafe fn test_0() { pub fn main() { unsafe { __cpp2rust_init_globals(); - std::process::exit(main_0() as i32); + libcc2rs::cpp2rust_exit(main_0() as i32); } } unsafe fn main_0() -> i32 { diff --git a/tests/unit/out/refcount/fn_ptr.rs b/tests/unit/out/refcount/fn_ptr.rs index a45cf84aa0..6d2d59afd4 100644 --- a/tests/unit/out/refcount/fn_ptr.rs +++ b/tests/unit/out/refcount/fn_ptr.rs @@ -117,7 +117,10 @@ fn main_0() -> i32 { 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()); - assert!(({ (*op.upgrade().deref()) }) == std::cmp::Ordering::Less); + assert!( + ({ (*op.upgrade().deref()) }) + == ({ (*libcc2rs::strong_ordering_less().upgrade().deref()) }) + ); return 0; } pub fn __cpp2rust_init_globals() { diff --git a/tests/unit/out/refcount/operator_comparison_defaulted.rs b/tests/unit/out/refcount/operator_comparison_defaulted.rs index 5dbd01cc76..7fa4f9dd49 100644 --- a/tests/unit/out/refcount/operator_comparison_defaulted.rs +++ b/tests/unit/out/refcount/operator_comparison_defaulted.rs @@ -139,7 +139,7 @@ impl CppEq for PtrMember { } pub fn main() { __cpp2rust_init_globals(); - std::process::exit(main_0()); + libcc2rs::cpp2rust_exit(main_0()); } fn main_0() -> i32 { let e1: Value = Rc::new(RefCell::new(Eq { a: 1, b: 2 })); diff --git a/tests/unit/out/refcount/operator_friend.rs b/tests/unit/out/refcount/operator_friend.rs index 2bb9857c18..b3997e1177 100644 --- a/tests/unit/out/refcount/operator_friend.rs +++ b/tests/unit/out/refcount/operator_friend.rs @@ -149,7 +149,7 @@ impl CppEq for TmplDefaulted_int_ { } pub fn main() { __cpp2rust_init_globals(); - std::process::exit(main_0()); + libcc2rs::cpp2rust_exit(main_0()); } fn main_0() -> i32 { let d1: Value = Rc::new(RefCell::new(Defaulted { a: 1, b: 2 })); diff --git a/tests/unit/out/refcount/operator_three_way.rs b/tests/unit/out/refcount/operator_three_way.rs index 9a7d2c1750..5677bec797 100644 --- a/tests/unit/out/refcount/operator_three_way.rs +++ b/tests/unit/out/refcount/operator_three_way.rs @@ -24,7 +24,7 @@ impl CppEq for S { } pub fn main() { __cpp2rust_init_globals(); - std::process::exit(main_0()); + libcc2rs::cpp2rust_exit(main_0()); } fn main_0() -> i32 { let a: Value = Rc::new(RefCell::new(S { v: 1 })); diff --git a/tests/unit/out/unsafe/fn_ptr.rs b/tests/unit/out/unsafe/fn_ptr.rs index ca3ff6e7c2..6a79f9b0e5 100644 --- a/tests/unit/out/unsafe/fn_ptr.rs +++ b/tests/unit/out/unsafe/fn_ptr.rs @@ -118,7 +118,7 @@ unsafe fn main_0() -> i32 { 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) == std::cmp::Ordering::Less); + assert!((*op) == libcc2rs::STRONG_ORDERING_LESS); return 0; } pub unsafe fn __cpp2rust_init_globals() { diff --git a/tests/unit/out/unsafe/operator_comparison_defaulted.rs b/tests/unit/out/unsafe/operator_comparison_defaulted.rs index 2073299cad..a8161d5ea3 100644 --- a/tests/unit/out/unsafe/operator_comparison_defaulted.rs +++ b/tests/unit/out/unsafe/operator_comparison_defaulted.rs @@ -318,7 +318,7 @@ impl std::cmp::Eq for PtrMember {} pub fn main() { unsafe { __cpp2rust_init_globals(); - std::process::exit(main_0() as i32); + libcc2rs::cpp2rust_exit(main_0() as i32); } } unsafe fn main_0() -> i32 { diff --git a/tests/unit/out/unsafe/operator_friend.rs b/tests/unit/out/unsafe/operator_friend.rs index 9135d1d08f..2d46baea0a 100644 --- a/tests/unit/out/unsafe/operator_friend.rs +++ b/tests/unit/out/unsafe/operator_friend.rs @@ -186,7 +186,7 @@ impl std::cmp::Eq for TmplDefaulted_int_ {} pub fn main() { unsafe { __cpp2rust_init_globals(); - std::process::exit(main_0() as i32); + libcc2rs::cpp2rust_exit(main_0() as i32); } } unsafe fn main_0() -> i32 { diff --git a/tests/unit/out/unsafe/operator_three_way.rs b/tests/unit/out/unsafe/operator_three_way.rs index a1fdc0f2c2..01c6d57945 100644 --- a/tests/unit/out/unsafe/operator_three_way.rs +++ b/tests/unit/out/unsafe/operator_three_way.rs @@ -44,7 +44,7 @@ impl std::cmp::Eq for S {} pub fn main() { unsafe { __cpp2rust_init_globals(); - std::process::exit(main_0() as i32); + libcc2rs::cpp2rust_exit(main_0() as i32); } } unsafe fn main_0() -> i32 {