diff --git a/cpp2rust/converter/converter.cpp b/cpp2rust/converter/converter.cpp index 5ce8d433f4..d3da302f8e 100644 --- a/cpp2rust/converter/converter.cpp +++ b/cpp2rust/converter/converter.cpp @@ -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; diff --git a/tests/unit/fn_ptr_stable_sort.cpp b/tests/unit/fn_ptr_stable_sort.cpp index 8361b5a68e..8f91db6e08 100644 --- a/tests/unit/fn_ptr_stable_sort.cpp +++ b/tests/unit/fn_ptr_stable_sort.cpp @@ -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 v; v.push_back({3, 30}); @@ -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; } diff --git a/tests/unit/out/refcount/fn_ptr_stable_sort.rs b/tests/unit/out/refcount/fn_ptr_stable_sort.rs index eab77e2913..847ee31ba1 100644 --- a/tests/unit/out/refcount/fn_ptr_stable_sort.rs +++ b/tests/unit/out/refcount/fn_ptr_stable_sort.rs @@ -17,6 +17,9 @@ pub struct Item { pub fn Compare_0(a: Ptr, b: Ptr) -> bool { return ({ a.with(|__s| __s.key) } < { b.with(|__s| __s.key) }); } +pub fn CompareDesc_1(a: Ptr, b: Ptr) -> bool { + return ({ a.with(|__s| __s.key) } > { b.with(|__s| __s.key) }); +} pub fn main() { __cpp2rust_init_globals(); std::process::exit(main_0()); @@ -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 = (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 = v.decay().offset(2_usize); + let _b: Ptr = v.decay().offset(0_usize); + (Compare_0)(_a, _b) + }) + ); return 0; } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/unsafe/fn_ptr_stable_sort.rs b/tests/unit/out/unsafe/fn_ptr_stable_sort.rs index 25da3034f4..49e7c8123d 100644 --- a/tests/unit/out/unsafe/fn_ptr_stable_sort.rs +++ b/tests/unit/out/unsafe/fn_ptr_stable_sort.rs @@ -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(); @@ -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 = (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() {}