Skip to content

Commit 7cf1a42

Browse files
committed
Implement Deref for FnPtr
1 parent 76fe7ef commit 7cf1a42

16 files changed

Lines changed: 45 additions & 48 deletions

cpp2rust/converter/converter.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@ bool Converter::VisitRecordType(clang::RecordType *type) {
131131
if (auto lambda = clang::dyn_cast<clang::CXXRecordDecl>(decl)) {
132132
if (lambda->isLambda()) {
133133
if (in_function_formals_) {
134-
// Function parameters can't use `_`. Emit `impl Fn(args) -> ret`.
135134
auto call_op = lambda->getLambdaCallOperator();
136135
StrCat("impl Fn(");
137136
for (auto p : call_op->parameters()) {

cpp2rust/converter/models/converter_refcount.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,8 @@ std::string ConverterRefCount::BuildFnAdapter(
180180
// Build adapter signature: |a0: T0, a1: T1, ...| -> Tr
181181
std::string closure = "(|";
182182
for (unsigned i = 0; i < target_proto->getNumParams(); ++i) {
183-
if (i > 0)
184-
closure += ", ";
185183
closure +=
186-
std::format("a{}: {}", i, ToString(target_proto->getParamType(i)));
184+
std::format("a{}: {},", i, ToString(target_proto->getParamType(i)));
187185
}
188186
closure += "|";
189187
if (!target_proto->getReturnType()->isVoidType()) {
@@ -1025,8 +1023,9 @@ bool ConverterRefCount::VisitImplicitCastExpr(clang::ImplicitCastExpr *expr) {
10251023
}
10261024

10271025
void ConverterRefCount::EmitFnPtrCall(clang::Expr *callee) {
1026+
StrCat("(*");
10281027
Convert(callee);
1029-
StrCat(".call()");
1028+
StrCat(")");
10301029
}
10311030

10321031
void ConverterRefCount::EmitFnAsValue(const clang::FunctionDecl *fn_decl) {

libcc2rs/src/fn_ptr.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
use std::any::{Any, TypeId};
55
use std::marker::PhantomData;
6+
use std::ops::Deref;
67
use std::rc::Rc;
78

89
use crate::rc::{AnyPtr, ErasedPtr};
@@ -75,17 +76,18 @@ impl<T: 'static> FnPtr<T> {
7576
}
7677
}
7778

78-
pub fn call(&self) -> T
79-
where
80-
T: Copy,
81-
{
79+
}
80+
81+
impl<T: 'static> Deref for FnPtr<T> {
82+
type Target = T;
83+
fn deref(&self) -> &T {
8284
let state = self.state.as_ref().expect("ub: null fn pointer call");
8385
let entry = state
8486
.cast_history
8587
.last()
8688
.expect("empty fn pointer cast_history");
8789
match entry {
88-
Some(rc) => *rc
90+
Some(rc) => rc
8991
.downcast_ref::<T>()
9092
.expect("ub: fn pointer type mismatch"),
9193
None => panic!("ub: calling through incompatible fn pointer type"),

tests/unit/out/refcount/fn_ptr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub fn foo_1(fn_: FnPtr<fn(AnyPtr) -> i32>, pi: Ptr<i32>) -> i32 {
1616
let pi: Value<Ptr<i32>> = Rc::new(RefCell::new(pi));
1717
return ({
1818
let _arg0: AnyPtr = ((*pi.borrow()).clone() as Ptr<i32>).to_any();
19-
(*fn_.borrow()).call()(_arg0)
19+
(*(*fn_.borrow()))(_arg0)
2020
});
2121
}
2222
pub fn main() {

tests/unit/out/refcount/fn_ptr_array.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,21 @@ fn main_0() -> i32 {
3535
(({
3636
let _arg0: i32 = 2;
3737
let _arg1: i32 = 3;
38-
(*ops.borrow())[(0) as usize].call()(_arg0, _arg1)
38+
(*(*ops.borrow())[(0) as usize])(_arg0, _arg1)
3939
}) == 5)
4040
);
4141
assert!(
4242
(({
4343
let _arg0: i32 = 7;
4444
let _arg1: i32 = 4;
45-
(*ops.borrow())[(1) as usize].call()(_arg0, _arg1)
45+
(*(*ops.borrow())[(1) as usize])(_arg0, _arg1)
4646
}) == 3)
4747
);
4848
assert!(
4949
(({
5050
let _arg0: i32 = 6;
5151
let _arg1: i32 = 5;
52-
(*ops.borrow())[(2) as usize].call()(_arg0, _arg1)
52+
(*(*ops.borrow())[(2) as usize])(_arg0, _arg1)
5353
}) == 30)
5454
);
5555
assert!(!(((*ops.borrow())[(0) as usize]).is_null()));

tests/unit/out/refcount/fn_ptr_as_condition.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub fn maybe_call_1(cb: FnPtr<fn(Ptr<i32>)>, x: Ptr<i32>) {
2121
if !(*cb.borrow()).is_null() {
2222
({
2323
let _arg0: Ptr<i32> = (*x.borrow()).clone();
24-
(*cb.borrow()).call()(_arg0)
24+
(*(*cb.borrow()))(_arg0)
2525
});
2626
}
2727
}
@@ -51,7 +51,7 @@ fn main_0() -> i32 {
5151
if !(*fn_.borrow()).is_null() {
5252
({
5353
let _arg0: Ptr<i32> = (c.as_pointer());
54-
(*fn_.borrow()).call()(_arg0)
54+
(*(*fn_.borrow()))(_arg0)
5555
});
5656
}
5757
assert!(((*c.borrow()) == 6));

tests/unit/out/refcount/fn_ptr_cast.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub fn test_roundtrip_1() {
1717
assert!(
1818
(({
1919
let _arg0: i32 = 5;
20-
(*fn_.borrow()).call()(_arg0)
20+
(*(*fn_.borrow()))(_arg0)
2121
}) == 10)
2222
);
2323
let gfn: Value<FnPtr<fn()>> =
@@ -29,7 +29,7 @@ pub fn test_roundtrip_1() {
2929
assert!(
3030
(({
3131
let _arg0: i32 = 5;
32-
(*fn2.borrow()).call()(_arg0)
32+
(*(*fn2.borrow()))(_arg0)
3333
}) == 10)
3434
);
3535
assert!({
@@ -49,7 +49,7 @@ pub fn test_double_cast_2() {
4949
assert!(
5050
(({
5151
let _arg0: i32 = 5;
52-
(*fn2.borrow()).call()(_arg0)
52+
(*(*fn2.borrow()))(_arg0)
5353
}) == 10)
5454
);
5555
assert!({
@@ -82,7 +82,7 @@ pub fn test_void_ptr_to_fn_3() {
8282
assert!(
8383
(({
8484
let _arg0: i32 = 5;
85-
(*fn_.borrow()).call()(_arg0)
85+
(*(*fn_.borrow()))(_arg0)
8686
}) == 10)
8787
);
8888
}
@@ -106,7 +106,7 @@ pub fn test_call_through_cast_5() {
106106
({
107107
let _arg0: AnyPtr = ((val.as_pointer()) as Ptr<i32>).to_any();
108108
let _arg1: i32 = 42;
109-
(*gfn.borrow()).call()(_arg0, _arg1)
109+
(*(*gfn.borrow()))(_arg0, _arg1)
110110
}),
111111
));
112112
assert!(((*result.borrow()) == 142));

tests/unit/out/refcount/fn_ptr_conditional.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pub fn apply_4(fn_: FnPtr<fn(i32) -> i32>, x: i32) -> i32 {
4242
}));
4343
return ({
4444
let _arg0: i32 = (*x.borrow());
45-
(*actual.borrow()).call()(_arg0)
45+
(*(*actual.borrow()))(_arg0)
4646
});
4747
}
4848
pub fn main() {
@@ -52,31 +52,28 @@ fn main_0() -> i32 {
5252
assert!(
5353
(({
5454
let _arg0: i32 = 10;
55-
({
55+
(*({
5656
let _mode: i32 = 1;
5757
pick_3(_mode)
58-
})
59-
.call()(_arg0)
58+
}))(_arg0)
6059
}) == 11)
6160
);
6261
assert!(
6362
(({
6463
let _arg0: i32 = 10;
65-
({
64+
(*({
6665
let _mode: i32 = -1_i32;
6766
pick_3(_mode)
68-
})
69-
.call()(_arg0)
67+
}))(_arg0)
7068
}) == 9)
7169
);
7270
assert!(
7371
(({
7472
let _arg0: i32 = 10;
75-
({
73+
(*({
7674
let _mode: i32 = 0;
7775
pick_3(_mode)
78-
})
79-
.call()(_arg0)
76+
}))(_arg0)
8077
}) == 10)
8178
);
8279
assert!(

tests/unit/out/refcount/fn_ptr_default_arg.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub fn apply_1(x: i32, fn_: Option<FnPtr<fn(i32) -> i32>>) -> i32 {
1717
if !(*fn_.borrow()).is_null() {
1818
return ({
1919
let _arg0: i32 = (*x.borrow());
20-
(*fn_.borrow()).call()(_arg0)
20+
(*(*fn_.borrow()))(_arg0)
2121
});
2222
}
2323
return (*x.borrow());

tests/unit/out/refcount/fn_ptr_global.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub fn call_op_3(x: i32) -> i32 {
2727
if !(*g_op.with(Value::clone).borrow()).is_null() {
2828
return ({
2929
let _arg0: i32 = (*x.borrow());
30-
(*g_op.with(Value::clone).borrow()).call()(_arg0)
30+
(*(*g_op.with(Value::clone).borrow()))(_arg0)
3131
});
3232
}
3333
return (*x.borrow());

0 commit comments

Comments
 (0)