Skip to content

Commit ac6a820

Browse files
authored
Add void cast on function ptr unused arg (#241)
1 parent 59986b1 commit ac6a820

7 files changed

Lines changed: 58 additions & 10 deletions

File tree

cpp2rust/converter/converter.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,13 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
274274
// Option<fn> implements Copy
275275
virtual bool FunctionPointerImplementsCopy() const { return true; }
276276

277+
bool TypeIsCopyable(clang::QualType ty) const {
278+
if (ty->isFunctionPointerType() || ty->isFunctionType()) {
279+
return FunctionPointerImplementsCopy();
280+
}
281+
return ty->isIntegerType();
282+
}
283+
277284
virtual void ConvertPrintf(clang::CallExpr *expr);
278285

279286
void ConvertVAArgCall(clang::CallExpr *expr);

cpp2rust/converter/converter_lib.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,9 +1104,4 @@ ConstCastType GetConstCastType(clang::QualType to, clang::QualType from) {
11041104
}
11051105
}
11061106

1107-
bool TypeIsCopyable(clang::QualType ty) {
1108-
return ty->isIntegerType() || ty->isFunctionPointerType() ||
1109-
ty->isFunctionType();
1110-
}
1111-
11121107
} // namespace cpp2rust

cpp2rust/converter/converter_lib.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,4 @@ enum class ConstCastType {
223223

224224
ConstCastType GetConstCastType(clang::QualType to, clang::QualType from);
225225

226-
bool TypeIsCopyable(clang::QualType ty);
227-
228226
} // namespace cpp2rust

tests/unit/out/refcount/va_arg_fn_ptr.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ pub fn apply_binary_4(a: i32, b: i32, __args: &[VaArg]) -> i32 {
4242
));
4343
return (*result.borrow());
4444
}
45+
pub fn not_supported_5(ctx: AnyPtr, fn_: FnPtr<fn(i32) -> i32>, extra: AnyPtr) -> i32 {
46+
let ctx: Value<AnyPtr> = Rc::new(RefCell::new(ctx));
47+
let fn_: Value<FnPtr<fn(i32) -> i32>> = Rc::new(RefCell::new(fn_));
48+
let extra: Value<AnyPtr> = Rc::new(RefCell::new(extra));
49+
(*ctx.borrow()).clone();
50+
(*fn_.borrow()).clone();
51+
(*extra.borrow()).clone();
52+
return -3_i32;
53+
}
4554
pub fn main() {
4655
std::process::exit(main_0());
4756
}
@@ -61,5 +70,14 @@ fn main_0() -> i32 {
6170
as i32)
6271
!= 0)
6372
);
73+
let dummy: Value<i32> = Rc::new(RefCell::new(0));
74+
assert!(
75+
(((({
76+
let _ctx: AnyPtr = ((dummy.as_pointer()) as Ptr<i32>).to_any();
77+
let _extra: AnyPtr = ((dummy.as_pointer()) as Ptr<i32>).to_any();
78+
not_supported_5(_ctx, FnPtr::<fn(i32) -> i32>::new(square_0), _extra)
79+
}) == -3_i32) as i32)
80+
!= 0)
81+
);
6482
return 0;
6583
}

tests/unit/out/refcount/void_cast.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,11 @@ fn main_0() -> i32 {
104104
}));
105105
assert!(((*err.borrow()) == 7));
106106
assert!(((*chosen.borrow()) == 123));
107-
bump_and_return_4;
107+
bump_and_return_4.clone();
108108
assert!(((*side_effect_counter_3.with(Value::clone).borrow()) == 2));
109-
(FnPtr::<fn() -> i32>::new(bump_and_return_4));
109+
(FnPtr::<fn() -> i32>::new(bump_and_return_4)).clone();
110110
assert!(((*side_effect_counter_3.with(Value::clone).borrow()) == 2));
111-
((FnPtr::<fn() -> i32>::new(bump_and_return_4)).cast::<fn() -> i32>(None));
111+
((FnPtr::<fn() -> i32>::new(bump_and_return_4)).cast::<fn() -> i32>(None)).clone();
112112
assert!(((*side_effect_counter_3.with(Value::clone).borrow()) == 2));
113113
let storage: Value<i32> = Rc::new(RefCell::new(11));
114114
let p: Value<Ptr<i32>> = Rc::new(RefCell::new((storage.as_pointer())));

tests/unit/out/unsafe/va_arg_fn_ptr.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ pub unsafe fn apply_binary_4(mut a: i32, mut b: i32, __args: &[VaArg]) -> i32 {
3535
let mut result: i32 = (unsafe { (fn_).unwrap()(a, b) });
3636
return result;
3737
}
38+
pub unsafe fn not_supported_5(
39+
mut ctx: *mut ::libc::c_void,
40+
mut fn_: Option<unsafe fn(i32) -> i32>,
41+
mut extra: *mut ::libc::c_void,
42+
) -> i32 {
43+
&(ctx);
44+
&(fn_);
45+
&(extra);
46+
return -3_i32;
47+
}
3848
pub fn main() {
3949
unsafe {
4050
std::process::exit(main_0() as i32);
@@ -78,5 +88,16 @@ unsafe fn main_0() -> i32 {
7888
}) == (7)) as i32)
7989
!= 0)
8090
);
91+
let mut dummy: i32 = 0;
92+
assert!(
93+
((((unsafe {
94+
let _ctx: *mut ::libc::c_void =
95+
((&mut dummy as *mut i32) as *mut i32 as *mut ::libc::c_void);
96+
let _extra: *mut ::libc::c_void =
97+
((&mut dummy as *mut i32) as *mut i32 as *mut ::libc::c_void);
98+
not_supported_5(_ctx, Some(square_0), _extra)
99+
}) == (-3_i32)) as i32)
100+
!= 0)
101+
);
81102
return 0;
82103
}

tests/unit/va_arg_fn_ptr.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,18 @@ int apply_binary(int a, int b, ...) {
2626
return result;
2727
}
2828

29+
static int not_supported(void *ctx, unary fn, void *extra) {
30+
(void)ctx;
31+
(void)fn;
32+
(void)extra;
33+
return -3;
34+
}
35+
2936
int main() {
3037
assert(apply_unary(5, square) == 25);
3138
assert(apply_unary(7, negate) == -7);
3239
assert(apply_binary(3, 4, add) == 7);
40+
int dummy = 0;
41+
assert(not_supported(&dummy, square, &dummy) == -3);
3342
return 0;
3443
}

0 commit comments

Comments
 (0)