Skip to content

Commit 43c69dd

Browse files
authored
Fix functions passed as arguments to rules (#662)
1 parent 8eb8597 commit 43c69dd

4 files changed

Lines changed: 83 additions & 0 deletions

File tree

‎cpp2rust/converter/converter.cpp‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3192,6 +3192,10 @@ bool Converter::VisitUnaryOperator(clang::UnaryOperator *expr) {
31923192
ConvertMemberPtr(expr);
31933193
break;
31943194
}
3195+
if (isCallee() && expr->getType()->isFunctionPointerType()) {
3196+
Convert(sub_expr);
3197+
break;
3198+
}
31953199
PushParen paren(*this);
31963200
ConvertAddrOf(sub_expr, expr->getType());
31973201
break;

‎tests/unit/fn_ptr_stable_sort.cpp‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ struct Item {
99

1010
static bool Compare(const Item &a, const Item &b) { return a.key < b.key; }
1111

12+
static bool CompareDesc(const Item &a, const Item &b) { return a.key > b.key; }
13+
1214
int main() {
1315
std::vector<Item> v;
1416
v.push_back({3, 30});
@@ -21,5 +23,12 @@ int main() {
2123
assert(v[1].key == 2);
2224
assert(v[2].key == 3);
2325

26+
std::stable_sort(v.begin(), v.end(), &CompareDesc);
27+
28+
assert(v[0].key == 3);
29+
assert(v[1].key == 2);
30+
assert(v[2].key == 1);
31+
assert((&Compare)(v[2], v[0]));
32+
2433
return 0;
2534
}

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ pub struct Item {
1717
pub fn Compare_0(a: Ptr<Item>, b: Ptr<Item>) -> bool {
1818
return ({ a.with(|__s| __s.key) } < { b.with(|__s| __s.key) });
1919
}
20+
pub fn CompareDesc_1(a: Ptr<Item>, b: Ptr<Item>) -> bool {
21+
return ({ a.with(|__s| __s.key) } > { b.with(|__s| __s.key) });
22+
}
2023
pub fn main() {
2124
__cpp2rust_init_globals();
2225
libcc2rs::cpp2rust_exit(main_0());
@@ -56,6 +59,34 @@ fn main_0() -> i32 {
5659
assert!(({ ({ { (*v.borrow())[0_usize] }.key } == 1) }));
5760
assert!(({ ({ { (*v.borrow())[1_usize] }.key } == 2) }));
5861
assert!(({ ({ { (*v.borrow())[2_usize] }.key } == 3) }));
62+
match ({ v.decay().begin() },) {
63+
(mut __tmp_1,) => {
64+
let __n = v.decay().end().iter_distance(&__tmp_1);
65+
let mut __order: Vec<isize> = (0..__n).collect();
66+
__order.sort_by(|&__x, &__y| {
67+
let __a = __tmp_1.iter_at(__x);
68+
let __b = __tmp_1.iter_at(__y);
69+
if CompareDesc_1.call(__a.clone(), __b.clone()) {
70+
std::cmp::Ordering::Less
71+
} else if CompareDesc_1.call(__b, __a) {
72+
std::cmp::Ordering::Greater
73+
} else {
74+
std::cmp::Ordering::Equal
75+
}
76+
});
77+
reorder(__order, |__i| __tmp_1.iter_at(__i));
78+
}
79+
};
80+
assert!(({ ({ { (*v.borrow())[0_usize] }.key } == 3) }));
81+
assert!(({ ({ { (*v.borrow())[1_usize] }.key } == 2) }));
82+
assert!(({ ({ { (*v.borrow())[2_usize] }.key } == 1) }));
83+
assert!(
84+
({
85+
let _a: Ptr<Item> = v.decay().offset(2_usize);
86+
let _b: Ptr<Item> = v.decay().offset(0_usize);
87+
(Compare_0)(_a, _b)
88+
})
89+
);
5990
return 0;
6091
}
6192
pub fn __cpp2rust_init_globals() {}

‎tests/unit/out/unsafe/fn_ptr_stable_sort.rs‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ pub struct Item {
1515
pub unsafe fn Compare_0(a: *const Item, b: *const Item) -> bool {
1616
return (((*a).key) < ((*b).key));
1717
}
18+
pub unsafe fn CompareDesc_1(a: *const Item, b: *const Item) -> bool {
19+
return (((*a).key) > ((*b).key));
20+
}
1821
pub fn main() {
1922
unsafe {
2023
__cpp2rust_init_globals();
@@ -63,6 +66,42 @@ unsafe fn main_0() -> i32 {
6366
assert!(((v[(0_usize)].key) == (1)));
6467
assert!(((v[(1_usize)].key) == (2)));
6568
assert!(((v[(2_usize)].key) == (3)));
69+
match ({ v.begin() },) {
70+
(mut __tmp_1,) => {
71+
let __n = v.end().iter_distance(&__tmp_1);
72+
let mut __order: Vec<isize> = (0..__n).collect();
73+
__order.sort_by(|&__x, &__y| {
74+
let __a = __tmp_1.iter_at(__x) as *const _;
75+
let __b = __tmp_1.iter_at(__y) as *const _;
76+
if (CompareDesc_1 as unsafe fn(*const Item, *const Item) -> bool).call(__a, __b) {
77+
std::cmp::Ordering::Less
78+
} else if (CompareDesc_1 as unsafe fn(*const Item, *const Item) -> bool)
79+
.call(__b, __a)
80+
{
81+
std::cmp::Ordering::Greater
82+
} else {
83+
std::cmp::Ordering::Equal
84+
}
85+
});
86+
let __values: Vec<_> = __order
87+
.iter()
88+
.map(|&__i| std::ptr::read(__tmp_1.iter_at(__i)))
89+
.collect();
90+
for (__i, __v) in (0..__n).zip(__values) {
91+
std::ptr::write(__tmp_1.iter_at(__i), __v);
92+
}
93+
}
94+
};
95+
assert!(((v[(0_usize)].key) == (3)));
96+
assert!(((v[(1_usize)].key) == (2)));
97+
assert!(((v[(2_usize)].key) == (1)));
98+
assert!(
99+
(unsafe {
100+
let _a: *const Item = &v[(2_usize)];
101+
let _b: *const Item = &v[(0_usize)];
102+
(Compare_0)(_a, _b)
103+
})
104+
);
66105
return 0;
67106
}
68107
pub unsafe fn __cpp2rust_init_globals() {}

0 commit comments

Comments
 (0)