Skip to content

Commit 3586b65

Browse files
authored
Add full function type when converting FnPtr::null (#298)
This avoids situations when rustc cannot infer the type of the null function pointer.
1 parent ed3c7bf commit 3586b65

12 files changed

Lines changed: 37 additions & 29 deletions

‎cpp2rust/converter/models/converter_refcount.cpp‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1966,7 +1966,10 @@ std::string ConverterRefCount::GetDefaultAsString(clang::QualType qual_type) {
19661966
if (qual_type->isPointerType()) {
19671967
auto pointee_type = qual_type->getPointeeType();
19681968
if (pointee_type->isFunctionType()) {
1969-
ret = "FnPtr::null()";
1969+
auto *proto = pointee_type->getAs<clang::FunctionProtoType>();
1970+
assert(proto && "Function pointer default without a prototype");
1971+
ret =
1972+
std::format("FnPtr::<{}>::null()", ConvertFunctionPointerType(proto));
19701973
} else {
19711974
if (pointee_type->isVoidType()) {
19721975
ret = "AnyPtr::default()";

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

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ impl Default for Outer {
7575
pp: Rc::new(RefCell::new(Ptr::<Ptr<i32>>::null())),
7676
inner: <Value<Inner>>::default(),
7777
x: <Value<i32>>::default(),
78-
fn_: Rc::new(RefCell::new(FnPtr::null())),
78+
fn_: Rc::new(RefCell::new(FnPtr::<fn(i32) -> i32>::null())),
7979
}
8080
}
8181
}
@@ -133,8 +133,8 @@ impl Default for Foo {
133133
Foo {
134134
s1: Rc::new(RefCell::new(Ptr::<u8>::null())),
135135
s2: Rc::new(RefCell::new(Ptr::<u8>::null())),
136-
fn1: Rc::new(RefCell::new(FnPtr::null())),
137-
fn2: Rc::new(RefCell::new(FnPtr::null())),
136+
fn1: Rc::new(RefCell::new(FnPtr::<fn(i32) -> i32>::null())),
137+
fn2: Rc::new(RefCell::new(FnPtr::<fn(i32) -> i32>::null())),
138138
n: <Value<i32>>::default(),
139139
}
140140
}
@@ -165,7 +165,8 @@ impl ByteRepr for Foo {
165165
}
166166
}
167167
thread_local!(
168-
pub static static_fn_0: Value<FnPtr<fn(i32) -> i32>> = Rc::new(RefCell::new(FnPtr::null()));
168+
pub static static_fn_0: Value<FnPtr<fn(i32) -> i32>> =
169+
Rc::new(RefCell::new(FnPtr::<fn(i32) -> i32>::null()));
169170
);
170171
thread_local!(
171172
pub static static_outer_1: Value<Outer> = Rc::new(RefCell::new(<Outer>::default()));
@@ -179,8 +180,8 @@ thread_local!(
179180
pub static static_foo_3: Value<Foo> = Rc::new(RefCell::new(Foo {
180181
s1: Rc::new(RefCell::new(Ptr::from_string_literal(b"hello"))),
181182
s2: Rc::new(RefCell::new(Ptr::<u8>::null())),
182-
fn1: Rc::new(RefCell::new(FnPtr::null())),
183-
fn2: Rc::new(RefCell::new(FnPtr::null())),
183+
fn1: Rc::new(RefCell::new(FnPtr::<fn(i32) -> i32>::null())),
184+
fn2: Rc::new(RefCell::new(FnPtr::<fn(i32) -> i32>::null())),
184185
n: Rc::new(RefCell::new(42)),
185186
}));
186187
);
@@ -189,15 +190,15 @@ thread_local!(
189190
Foo {
190191
s1: Rc::new(RefCell::new(Ptr::from_string_literal(b"first"))),
191192
s2: Rc::new(RefCell::new(Ptr::<u8>::null())),
192-
fn1: Rc::new(RefCell::new(FnPtr::null())),
193-
fn2: Rc::new(RefCell::new(FnPtr::null())),
193+
fn1: Rc::new(RefCell::new(FnPtr::<fn(i32) -> i32>::null())),
194+
fn2: Rc::new(RefCell::new(FnPtr::<fn(i32) -> i32>::null())),
194195
n: Rc::new(RefCell::new(1)),
195196
},
196197
Foo {
197198
s1: Rc::new(RefCell::new(Ptr::from_string_literal(b"second"))),
198199
s2: Rc::new(RefCell::new(Ptr::<u8>::null())),
199-
fn1: Rc::new(RefCell::new(FnPtr::null())),
200-
fn2: Rc::new(RefCell::new(FnPtr::null())),
200+
fn1: Rc::new(RefCell::new(FnPtr::<fn(i32) -> i32>::null())),
201+
fn2: Rc::new(RefCell::new(FnPtr::<fn(i32) -> i32>::null())),
201202
n: Rc::new(RefCell::new(2)),
202203
},
203204
])));
@@ -207,7 +208,8 @@ pub fn check_local_static_5() {
207208
static local_outer_6: Value<Outer> = Rc::new(RefCell::new(<Outer>::default()));
208209
);
209210
thread_local!(
210-
static local_fn_7: Value<FnPtr<fn(i32) -> i32>> = Rc::new(RefCell::new(FnPtr::null()));
211+
static local_fn_7: Value<FnPtr<fn(i32) -> i32>> =
212+
Rc::new(RefCell::new(FnPtr::<fn(i32) -> i32>::null()));
211213
);
212214
thread_local!(
213215
static local_p_8: Value<Ptr<i32>> = Rc::new(RefCell::new(Ptr::<i32>::null()));

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ pub fn main() {
1919
std::process::exit(main_0());
2020
}
2121
fn main_0() -> i32 {
22-
let fn_: Value<FnPtr<fn(AnyPtr) -> i32>> = Rc::new(RefCell::new(FnPtr::null()));
22+
let fn_: Value<FnPtr<fn(AnyPtr) -> i32>> =
23+
Rc::new(RefCell::new(FnPtr::<fn(AnyPtr) -> i32>::null()));
2324
assert!((*fn_.borrow()).is_null());
2425
assert!({
2526
let _lhs = (*fn_.borrow()).clone();

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ fn main_0() -> i32 {
2828
({ maybe_call_1(FnPtr::<fn(Ptr<i32>)>::new(double_it_0), (a.as_pointer())) });
2929
assert!(((*a.borrow()) == 10));
3030
let b: Value<i32> = Rc::new(RefCell::new(5));
31-
({ maybe_call_1(FnPtr::null(), (b.as_pointer())) });
31+
({ maybe_call_1(FnPtr::<fn(Ptr<i32>)>::null(), (b.as_pointer())) });
3232
assert!(((*b.borrow()) == 5));
33-
let fn_: Value<FnPtr<fn(Ptr<i32>)>> = Rc::new(RefCell::new(FnPtr::null()));
33+
let fn_: Value<FnPtr<fn(Ptr<i32>)>> = Rc::new(RefCell::new(FnPtr::<fn(Ptr<i32>)>::null()));
3434
if !!(*fn_.borrow()).is_null() {
3535
(*fn_.borrow_mut()) = FnPtr::<fn(Ptr<i32>)>::new(double_it_0);
3636
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,6 @@ fn main_0() -> i32 {
4949
assert!((({ (*({ pick_3(-1_i32,) }))(10,) }) == 9));
5050
assert!((({ (*({ pick_3(0,) }))(10,) }) == 10));
5151
assert!((({ apply_4(FnPtr::<fn(i32) -> i32>::new(inc_0), 5,) }) == 6));
52-
assert!((({ apply_4(FnPtr::null(), 5,) }) == 5));
52+
assert!((({ apply_4(FnPtr::<fn(i32) -> i32>::null(), 5,) }) == 5));
5353
return 0;
5454
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ pub fn identity_0(x: i32) -> i32 {
1212
}
1313
pub fn apply_1(x: i32, fn_: Option<FnPtr<fn(i32) -> i32>>) -> i32 {
1414
let x: Value<i32> = Rc::new(RefCell::new(x));
15-
let fn_: Value<FnPtr<fn(i32) -> i32>> = Rc::new(RefCell::new(fn_.unwrap_or(FnPtr::null())));
15+
let fn_: Value<FnPtr<fn(i32) -> i32>> =
16+
Rc::new(RefCell::new(fn_.unwrap_or(FnPtr::<fn(i32) -> i32>::null())));
1617
if !(*fn_.borrow()).is_null() {
1718
return ({ (*(*fn_.borrow()))((*x.borrow())) });
1819
}
@@ -23,7 +24,7 @@ pub fn main() {
2324
}
2425
fn main_0() -> i32 {
2526
assert!((({ apply_1(5, Some(Default::default()),) }) == 5));
26-
assert!((({ apply_1(5, Some(FnPtr::null()),) }) == 5));
27+
assert!((({ apply_1(5, Some(FnPtr::<fn(i32) -> i32>::null()),) }) == 5));
2728
assert!((({ apply_1(5, Some(FnPtr::<fn(i32) -> i32>::new(identity_0)),) }) == 5));
2829
let negate: Value<FnPtr<fn(i32) -> i32>> = Rc::new(RefCell::new(FnPtr::new(
2930
(|x: i32| {

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ pub fn triple_it_1(x: i32) -> i32 {
1515
return ((*x.borrow()) * 3);
1616
}
1717
thread_local!(
18-
pub static g_op_2: Value<FnPtr<fn(i32) -> i32>> = Rc::new(RefCell::new(FnPtr::null()));
18+
pub static g_op_2: Value<FnPtr<fn(i32) -> i32>> =
19+
Rc::new(RefCell::new(FnPtr::<fn(i32) -> i32>::null()));
1920
);
2021
pub fn set_op_3(fn_: FnPtr<fn(i32) -> i32>) {
2122
let fn_: Value<FnPtr<fn(i32) -> i32>> = Rc::new(RefCell::new(fn_));
@@ -46,7 +47,7 @@ fn main_0() -> i32 {
4647
_lhs == FnPtr::<fn(i32) -> i32>::new(triple_it_1)
4748
});
4849
assert!((({ call_op_4(5,) }) == 15));
49-
({ set_op_3(FnPtr::null()) });
50+
({ set_op_3(FnPtr::<fn(i32) -> i32>::null()) });
5051
assert!((*g_op_2.with(Value::clone).borrow()).is_null());
5152
assert!((({ call_op_4(5,) }) == 5));
5253
return 0;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ fn main_0() -> i32 {
3232
assert!((({ (*(*fn_.borrow()))(10, 3,) }) == 7));
3333
(*fn_.borrow_mut()) = FnPtr::<fn(i32, i32) -> i32>::new(mul_2);
3434
assert!((({ (*(*fn_.borrow()))(6, 7,) }) == 42));
35-
(*fn_.borrow_mut()) = FnPtr::null();
35+
(*fn_.borrow_mut()) = FnPtr::<fn(i32, i32) -> i32>::null();
3636
assert!((*fn_.borrow()).is_null());
3737
(*fn_.borrow_mut()) = FnPtr::<fn(i32, i32) -> i32>::new(add_0);
3838
assert!(!((*fn_.borrow()).is_null()));

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ impl Default for Handler {
2424
fn default() -> Self {
2525
Handler {
2626
tag: <Value<i32>>::default(),
27-
cb: Rc::new(RefCell::new(FnPtr::null())),
27+
cb: Rc::new(RefCell::new(FnPtr::<fn(i32) -> i32>::null())),
2828
}
2929
}
3030
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ impl Clone for Vtable {
2525
impl Default for Vtable {
2626
fn default() -> Self {
2727
Vtable {
28-
create: Rc::new(RefCell::new(FnPtr::null())),
29-
get: Rc::new(RefCell::new(FnPtr::null())),
30-
destroy: Rc::new(RefCell::new(FnPtr::null())),
28+
create: Rc::new(RefCell::new(FnPtr::<fn(i32) -> AnyPtr>::null())),
29+
get: Rc::new(RefCell::new(FnPtr::<fn(AnyPtr) -> i32>::null())),
30+
destroy: Rc::new(RefCell::new(FnPtr::<fn(AnyPtr)>::null())),
3131
}
3232
}
3333
}
@@ -84,7 +84,7 @@ fn main_0() -> i32 {
8484
assert!((({ (*(*(*vt.borrow()).get.borrow()))((*obj.borrow()).clone(),) }) == 42));
8585
({ (*(*(*vt.borrow()).destroy.borrow()))((*obj.borrow()).clone()) });
8686
assert!(((*storage_0.with(Value::clone).borrow()) == 0));
87-
(*(*vt.borrow()).get.borrow_mut()) = FnPtr::null();
87+
(*(*vt.borrow()).get.borrow_mut()) = FnPtr::<fn(AnyPtr) -> i32>::null();
8888
assert!((*(*vt.borrow()).get.borrow()).is_null());
8989
return 0;
9090
}

0 commit comments

Comments
 (0)