Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cpp2rust/converter/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3157,6 +3157,10 @@ bool Converter::VisitUnaryOperator(clang::UnaryOperator *expr) {
ConvertMemberPtr(expr);
break;
}
if (isCallee() && expr->getType()->isFunctionPointerType()) {
Convert(sub_expr);
break;
}
PushParen paren(*this);
ConvertAddrOf(sub_expr, expr->getType());
break;
Expand Down
9 changes: 9 additions & 0 deletions tests/unit/fn_ptr_stable_sort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ struct Item {

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

static bool CompareDesc(const Item &a, const Item &b) { return a.key > b.key; }

int main() {
std::vector<Item> v;
v.push_back({3, 30});
Expand All @@ -21,5 +23,12 @@ int main() {
assert(v[1].key == 2);
assert(v[2].key == 3);

std::stable_sort(v.begin(), v.end(), &CompareDesc);

assert(v[0].key == 3);
assert(v[1].key == 2);
assert(v[2].key == 1);
assert((&Compare)(v[2], v[0]));

return 0;
}
31 changes: 31 additions & 0 deletions tests/unit/out/refcount/fn_ptr_stable_sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ pub struct Item {
pub fn Compare_0(a: Ptr<Item>, b: Ptr<Item>) -> bool {
return ({ a.with(|__s| __s.key) } < { b.with(|__s| __s.key) });
}
pub fn CompareDesc_1(a: Ptr<Item>, b: Ptr<Item>) -> bool {
return ({ a.with(|__s| __s.key) } > { b.with(|__s| __s.key) });
}
pub fn main() {
__cpp2rust_init_globals();
std::process::exit(main_0());
Expand Down Expand Up @@ -56,6 +59,34 @@ fn main_0() -> i32 {
assert!(({ ({ { (*v.borrow())[0_usize] }.key } == 1) }));
assert!(({ ({ { (*v.borrow())[1_usize] }.key } == 2) }));
assert!(({ ({ { (*v.borrow())[2_usize] }.key } == 3) }));
match ({ v.decay().begin() },) {
(mut __tmp_1,) => {
let __n = v.decay().end().iter_distance(&__tmp_1);
let mut __order: Vec<isize> = (0..__n).collect();
__order.sort_by(|&__x, &__y| {
let __a = __tmp_1.iter_at(__x);
let __b = __tmp_1.iter_at(__y);
if CompareDesc_1.call(__a.clone(), __b.clone()) {
std::cmp::Ordering::Less
} else if CompareDesc_1.call(__b, __a) {
std::cmp::Ordering::Greater
} else {
std::cmp::Ordering::Equal
}
});
reorder(__order, |__i| __tmp_1.iter_at(__i));
}
};
assert!(({ ({ { (*v.borrow())[0_usize] }.key } == 3) }));
assert!(({ ({ { (*v.borrow())[1_usize] }.key } == 2) }));
assert!(({ ({ { (*v.borrow())[2_usize] }.key } == 1) }));
assert!(
({
let _a: Ptr<Item> = v.decay().offset(2_usize);
let _b: Ptr<Item> = v.decay().offset(0_usize);
(Compare_0)(_a, _b)
})
);
return 0;
}
pub fn __cpp2rust_init_globals() {}
39 changes: 39 additions & 0 deletions tests/unit/out/unsafe/fn_ptr_stable_sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ pub struct Item {
pub unsafe fn Compare_0(a: *const Item, b: *const Item) -> bool {
return (((*a).key) < ((*b).key));
}
pub unsafe fn CompareDesc_1(a: *const Item, b: *const Item) -> bool {
return (((*a).key) > ((*b).key));
}
pub fn main() {
unsafe {
__cpp2rust_init_globals();
Expand Down Expand Up @@ -63,6 +66,42 @@ unsafe fn main_0() -> i32 {
assert!(((v[(0_usize)].key) == (1)));
assert!(((v[(1_usize)].key) == (2)));
assert!(((v[(2_usize)].key) == (3)));
match ({ v.begin() },) {
(mut __tmp_1,) => {
let __n = v.end().iter_distance(&__tmp_1);
let mut __order: Vec<isize> = (0..__n).collect();
__order.sort_by(|&__x, &__y| {
let __a = __tmp_1.iter_at(__x) as *const _;
let __b = __tmp_1.iter_at(__y) as *const _;
if (CompareDesc_1 as unsafe fn(*const Item, *const Item) -> bool).call(__a, __b) {
std::cmp::Ordering::Less
} else if (CompareDesc_1 as unsafe fn(*const Item, *const Item) -> bool)
.call(__b, __a)
{
std::cmp::Ordering::Greater
} else {
std::cmp::Ordering::Equal
}
});
let __values: Vec<_> = __order
.iter()
.map(|&__i| std::ptr::read(__tmp_1.iter_at(__i)))
.collect();
for (__i, __v) in (0..__n).zip(__values) {
std::ptr::write(__tmp_1.iter_at(__i), __v);
}
}
};
assert!(((v[(0_usize)].key) == (3)));
assert!(((v[(1_usize)].key) == (2)));
assert!(((v[(2_usize)].key) == (1)));
assert!(
(unsafe {
let _a: *const Item = &v[(2_usize)];
let _b: *const Item = &v[(0_usize)];
(Compare_0)(_a, _b)
})
);
return 0;
}
pub unsafe fn __cpp2rust_init_globals() {}
Loading