Skip to content

Commit c46adda

Browse files
authored
Return reference form std::strong_ordering rules (#660)
1 parent c902b60 commit c46adda

16 files changed

Lines changed: 195 additions & 49 deletions

‎cpp2rust/converter/models/converter_refcount.cpp‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,6 +1040,11 @@ void ConverterRefCount::ConvertDeclRefValue(clang::Expr *expr,
10401040

10411041
if (ShouldReplaceWithMappedBody(decl)) {
10421042
if (auto str = GetMappedAsString(expr); !str.empty()) {
1043+
if (RuleRegistry::ReturnsReference(expr) && !isAddrOf() &&
1044+
!Mapper::IsRefExpr(ctx_, expr)) {
1045+
EmitPointee(str, expr->getType(), /*fresh=*/true);
1046+
return;
1047+
}
10431048
EmitFresh(str);
10441049
return;
10451050
}

‎rules/compare/shim.rs‎

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
// Copyright (c) 2022-present INESC-ID.
22
// Distributed under the MIT license that can be found in the LICENSE file.
33

4-
use crate::{ArgRepr, ByteRepr, FnPtrArg, record_from_repr};
4+
use std::cell::RefCell;
5+
use std::cmp::Ordering;
6+
use std::rc::Rc;
7+
8+
use crate::{ArgRepr, AsPointer, ByteRepr, FnPtrArg, Ptr, Value, record_from_repr};
59

610
impl ByteRepr for std::cmp::Ordering {}
711

@@ -14,3 +18,31 @@ impl FnPtrArg for std::cmp::Ordering {
1418
record_from_repr(r)
1519
}
1620
}
21+
22+
pub static STRONG_ORDERING_LESS: Ordering = Ordering::Less;
23+
pub static STRONG_ORDERING_EQUAL: Ordering = Ordering::Equal;
24+
pub static STRONG_ORDERING_EQUIVALENT: Ordering = Ordering::Equal;
25+
pub static STRONG_ORDERING_GREATER: Ordering = Ordering::Greater;
26+
27+
thread_local! {
28+
static STRONG_ORDERING_LESS_VALUE: Value<Ordering> = Rc::new(RefCell::new(Ordering::Less));
29+
static STRONG_ORDERING_EQUAL_VALUE: Value<Ordering> = Rc::new(RefCell::new(Ordering::Equal));
30+
static STRONG_ORDERING_EQUIVALENT_VALUE: Value<Ordering> = Rc::new(RefCell::new(Ordering::Equal));
31+
static STRONG_ORDERING_GREATER_VALUE: Value<Ordering> = Rc::new(RefCell::new(Ordering::Greater));
32+
}
33+
34+
pub fn strong_ordering_less() -> Ptr<Ordering> {
35+
STRONG_ORDERING_LESS_VALUE.with(AsPointer::as_pointer)
36+
}
37+
38+
pub fn strong_ordering_equal() -> Ptr<Ordering> {
39+
STRONG_ORDERING_EQUAL_VALUE.with(AsPointer::as_pointer)
40+
}
41+
42+
pub fn strong_ordering_equivalent() -> Ptr<Ordering> {
43+
STRONG_ORDERING_EQUIVALENT_VALUE.with(AsPointer::as_pointer)
44+
}
45+
46+
pub fn strong_ordering_greater() -> Ptr<Ordering> {
47+
STRONG_ORDERING_GREATER_VALUE.with(AsPointer::as_pointer)
48+
}

‎rules/compare/src_cxx20.cpp‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55

66
typedef std::strong_ordering t1;
77

8-
std::strong_ordering f1() { return std::strong_ordering::less; }
8+
const std::strong_ordering &f1() { return std::strong_ordering::less; }
99

10-
std::strong_ordering f2() { return std::strong_ordering::equal; }
10+
const std::strong_ordering &f2() { return std::strong_ordering::equal; }
1111

12-
std::strong_ordering f3() { return std::strong_ordering::equivalent; }
12+
const std::strong_ordering &f3() { return std::strong_ordering::equivalent; }
1313

14-
std::strong_ordering f4() { return std::strong_ordering::greater; }
14+
const std::strong_ordering &f4() { return std::strong_ordering::greater; }
1515

1616
bool f5(std::strong_ordering a0, std::strong_ordering a1) { return a0 == a1; }
1717

‎rules/compare/tgt_refcount.rs‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright (c) 2022-present INESC-ID.
2+
// Distributed under the MIT license that can be found in the LICENSE file.
3+
4+
use libcc2rs::*;
5+
6+
fn f1() -> Ptr<std::cmp::Ordering> {
7+
libcc2rs::strong_ordering_less()
8+
}
9+
10+
fn f2() -> Ptr<std::cmp::Ordering> {
11+
libcc2rs::strong_ordering_equal()
12+
}
13+
14+
fn f3() -> Ptr<std::cmp::Ordering> {
15+
libcc2rs::strong_ordering_equivalent()
16+
}
17+
18+
fn f4() -> Ptr<std::cmp::Ordering> {
19+
libcc2rs::strong_ordering_greater()
20+
}

‎rules/compare/tgt_unsafe.rs‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@ fn t1() -> std::cmp::Ordering {
55
std::cmp::Ordering::Equal
66
}
77

8-
fn f1() -> std::cmp::Ordering {
9-
std::cmp::Ordering::Less
8+
unsafe fn f1() -> *const std::cmp::Ordering {
9+
&raw const libcc2rs::STRONG_ORDERING_LESS
1010
}
1111

12-
fn f2() -> std::cmp::Ordering {
13-
std::cmp::Ordering::Equal
12+
unsafe fn f2() -> *const std::cmp::Ordering {
13+
&raw const libcc2rs::STRONG_ORDERING_EQUAL
1414
}
1515

16-
fn f3() -> std::cmp::Ordering {
17-
std::cmp::Ordering::Equal
16+
unsafe fn f3() -> *const std::cmp::Ordering {
17+
&raw const libcc2rs::STRONG_ORDERING_EQUIVALENT
1818
}
1919

20-
fn f4() -> std::cmp::Ordering {
21-
std::cmp::Ordering::Greater
20+
unsafe fn f4() -> *const std::cmp::Ordering {
21+
&raw const libcc2rs::STRONG_ORDERING_GREATER
2222
}
2323

2424
fn f5(a0: std::cmp::Ordering, a1: std::cmp::Ordering) -> bool {
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// ADDITIONAL_COMPILE_FLAGS: -std=c++20
2+
#include <cassert>
3+
#include <compare>
4+
5+
// N4950 [cmp.strongord]: "static const strong_ordering less;
6+
// static const strong_ordering equal; static const strong_ordering equivalent;
7+
// static const strong_ordering greater;" A reference bound to a valid value
8+
// refers to that static data member.
9+
namespace valid_values {
10+
void test() {
11+
const std::strong_ordering &l = std::strong_ordering::less;
12+
const std::strong_ordering &e = std::strong_ordering::equal;
13+
const std::strong_ordering &g = std::strong_ordering::greater;
14+
assert(&l == &std::strong_ordering::less);
15+
assert(&e == &std::strong_ordering::equal);
16+
assert(&g == &std::strong_ordering::greater);
17+
assert(l < 0);
18+
assert(e == 0);
19+
assert(g > 0);
20+
}
21+
} // namespace valid_values
22+
23+
int main() {
24+
valid_values::test();
25+
return 0;
26+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
extern crate libcc2rs;
2+
use libcc2rs::*;
3+
use std::cell::RefCell;
4+
use std::collections::BTreeMap;
5+
use std::io::prelude::*;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::AsFd;
8+
use std::rc::{Rc, Weak};
9+
pub fn test_0() {
10+
let l: Ptr<std::cmp::Ordering> = libcc2rs::strong_ordering_less();
11+
let e: Ptr<std::cmp::Ordering> = libcc2rs::strong_ordering_equal();
12+
let g: Ptr<std::cmp::Ordering> = libcc2rs::strong_ordering_greater();
13+
assert!(({ (l).clone() } == { (libcc2rs::strong_ordering_less()) }));
14+
assert!(({ (e).clone() } == { (libcc2rs::strong_ordering_equal()) }));
15+
assert!(({ (g).clone() } == { (libcc2rs::strong_ordering_greater()) }));
16+
assert!(({ (*l.upgrade().deref()) }) == std::cmp::Ordering::Less);
17+
assert!(({ (*e.upgrade().deref()) }) == std::cmp::Ordering::Equal);
18+
assert!(({ (*g.upgrade().deref()) }) == std::cmp::Ordering::Greater);
19+
}
20+
pub fn main() {
21+
__cpp2rust_init_globals();
22+
libcc2rs::cpp2rust_exit(main_0());
23+
}
24+
fn main_0() -> i32 {
25+
({ test_0() });
26+
return 0;
27+
}
28+
pub fn __cpp2rust_init_globals() {}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
extern crate libc;
2+
use libc::*;
3+
extern crate libcc2rs;
4+
use libcc2rs::*;
5+
use std::collections::BTreeMap;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
8+
use std::rc::Rc;
9+
pub unsafe fn test_0() {
10+
let l: *const std::cmp::Ordering = &libcc2rs::STRONG_ORDERING_LESS;
11+
let e: *const std::cmp::Ordering = &libcc2rs::STRONG_ORDERING_EQUAL;
12+
let g: *const std::cmp::Ordering = &libcc2rs::STRONG_ORDERING_GREATER;
13+
assert!(((l) == (&raw const libcc2rs::STRONG_ORDERING_LESS as *const std::cmp::Ordering)));
14+
assert!(((e) == (&raw const libcc2rs::STRONG_ORDERING_EQUAL as *const std::cmp::Ordering)));
15+
assert!(((g) == (&raw const libcc2rs::STRONG_ORDERING_GREATER as *const std::cmp::Ordering)));
16+
assert!((*l) == std::cmp::Ordering::Less);
17+
assert!((*e) == std::cmp::Ordering::Equal);
18+
assert!((*g) == std::cmp::Ordering::Greater);
19+
}
20+
pub fn main() {
21+
unsafe {
22+
__cpp2rust_init_globals();
23+
libcc2rs::cpp2rust_exit(main_0() as i32);
24+
}
25+
}
26+
unsafe fn main_0() -> i32 {
27+
(unsafe { test_0() });
28+
return 0;
29+
}
30+
pub unsafe fn __cpp2rust_init_globals() {}

‎tests/unit/out/refcount/fn_ptr.rs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,10 @@ fn main_0() -> i32 {
117117
assert!(!({ of.call(std::cmp::Ord::cmp(&(2), &(1)),) }));
118118
let o: Value<std::cmp::Ordering> = Rc::new(RefCell::new(std::cmp::Ord::cmp(&(1), &(2))));
119119
let mut op: Ptr<std::cmp::Ordering> = (o.as_pointer());
120-
assert!(({ (*op.upgrade().deref()) }) == std::cmp::Ordering::Less);
120+
assert!(
121+
({ (*op.upgrade().deref()) })
122+
== ({ (*libcc2rs::strong_ordering_less().upgrade().deref()) })
123+
);
121124
return 0;
122125
}
123126
pub fn __cpp2rust_init_globals() {

‎tests/unit/out/refcount/operator_comparison_defaulted.rs‎

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,8 @@ fn main_0() -> i32 {
165165
})
166166
);
167167
assert!(
168-
({ CmpImpl::operator_cmp(&c1.as_pointer(), c2.as_pointer(),) }) == std::cmp::Ordering::Less
168+
({ CmpImpl::operator_cmp(&c1.as_pointer(), c2.as_pointer(),) })
169+
== ({ (*libcc2rs::strong_ordering_less().upgrade().deref()) })
169170
);
170171
let b1: Value<Both> = Rc::new(RefCell::new(Both { a: 1 }));
171172
let b2: Value<Both> = Rc::new(RefCell::new(Both { a: 2 }));
@@ -187,7 +188,7 @@ fn main_0() -> i32 {
187188
);
188189
assert!(
189190
({ OrdOnlyImpl::operator_cmp(&o2.as_pointer(), o1.as_pointer(),) })
190-
== std::cmp::Ordering::Greater
191+
== ({ (*libcc2rs::strong_ordering_greater().upgrade().deref()) })
191192
);
192193
let x1: Value<Outer> = Rc::new(RefCell::new(Outer {
193194
i: Inner { x: 1 },
@@ -208,7 +209,7 @@ fn main_0() -> i32 {
208209
assert!(({ OuterImpl::operator_eq(&x1.as_pointer(), x3.as_pointer(),) }));
209210
assert!(
210211
({ OuterImpl::operator_cmp(&x2.as_pointer(), x1.as_pointer(),) })
211-
== std::cmp::Ordering::Greater
212+
== ({ (*libcc2rs::strong_ordering_greater().upgrade().deref()) })
212213
);
213214
let s1: Value<Secondary> = Rc::new(RefCell::new(Secondary { a: 1 }));
214215
let s2: Value<Secondary> = Rc::new(RefCell::new(Secondary { a: 2 }));
@@ -229,7 +230,7 @@ fn main_0() -> i32 {
229230
assert!(({ PtrMemberImpl::operator_eq(&p1.as_pointer(), p3.as_pointer(),) }));
230231
assert!(
231232
({ PtrMemberImpl::operator_cmp(&p2.as_pointer(), p1.as_pointer(),) })
232-
== std::cmp::Ordering::Greater
233+
== ({ (*libcc2rs::strong_ordering_greater().upgrade().deref()) })
233234
);
234235
return 0;
235236
}
@@ -251,7 +252,7 @@ impl BothImpl for Ptr<Both> {
251252
return (*cmp.borrow_mut());
252253
}
253254
}
254-
return std::cmp::Ordering::Equal;
255+
return (*libcc2rs::strong_ordering_equal().upgrade().deref());
255256
}
256257
}
257258
pub trait CmpImpl {
@@ -278,7 +279,7 @@ impl CmpImpl for Ptr<Cmp> {
278279
return (*cmp.borrow_mut());
279280
}
280281
}
281-
return std::cmp::Ordering::Equal;
282+
return (*libcc2rs::strong_ordering_equal().upgrade().deref());
282283
}
283284
fn operator_eq(&self, _a0: Ptr<Cmp>) -> bool {
284285
return ({ (*self).with(|__s| __s.a) } == { _a0.with(|__s| __s.a) })
@@ -309,7 +310,7 @@ impl InnerImpl for Ptr<Inner> {
309310
return (*cmp.borrow_mut());
310311
}
311312
}
312-
return std::cmp::Ordering::Equal;
313+
return (*libcc2rs::strong_ordering_equal().upgrade().deref());
313314
}
314315
fn operator_eq(&self, _a0: Ptr<Inner>) -> bool {
315316
return ({ (*self).with(|__s| __s.x) } == { _a0.with(|__s| __s.x) });
@@ -329,7 +330,7 @@ impl OrdOnlyImpl for Ptr<OrdOnly> {
329330
return (*cmp.borrow_mut());
330331
}
331332
}
332-
return std::cmp::Ordering::Equal;
333+
return (*libcc2rs::strong_ordering_equal().upgrade().deref());
333334
}
334335
}
335336
pub trait OuterImpl {
@@ -358,7 +359,7 @@ impl OuterImpl for Ptr<Outer> {
358359
return (*cmp.borrow_mut());
359360
}
360361
}
361-
return std::cmp::Ordering::Equal;
362+
return (*libcc2rs::strong_ordering_equal().upgrade().deref());
362363
}
363364
fn operator_eq(&self, _a0: Ptr<Outer>) -> bool {
364365
return ({
@@ -382,7 +383,7 @@ impl PtrMemberImpl for Ptr<PtrMember> {
382383
return (*cmp.borrow_mut());
383384
}
384385
}
385-
return std::cmp::Ordering::Equal;
386+
return (*libcc2rs::strong_ordering_equal().upgrade().deref());
386387
}
387388
fn operator_eq(&self, _a0: Ptr<PtrMember>) -> bool {
388389
return ({ (*self).with(|__s| __s.p.clone()) } == { _a0.with(|__s| __s.p.clone()) });
@@ -415,7 +416,7 @@ impl SecondaryImpl for Ptr<Secondary> {
415416
return (*cmp.borrow_mut());
416417
}
417418
}
418-
return std::cmp::Ordering::Equal;
419+
return (*libcc2rs::strong_ordering_equal().upgrade().deref());
419420
}
420421
fn operator_lt(&self, _a0: Ptr<Secondary>) -> bool {
421422
return ({

0 commit comments

Comments
 (0)