Skip to content

Commit 0ce79c3

Browse files
committed
Follow proper derive rules for function pointers.
1 parent 56021b2 commit 0ce79c3

File tree

6 files changed

+97
-3
lines changed

6 files changed

+97
-3
lines changed

src/ir/function.rs

+24
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use super::dot::DotAttributes;
55
use super::item::Item;
66
use super::traversal::{EdgeKind, Trace, Tracer};
77
use super::ty::TypeKind;
8+
use ir::derive::CanDeriveDebug;
89
use clang;
910
use clang_sys::CXCallingConv;
1011
use parse::{ClangItemParser, ClangSubItemParser, ParseError, ParseResult};
@@ -349,3 +350,26 @@ impl Trace for FunctionSig {
349350
}
350351
}
351352
}
353+
354+
// Function pointers follow special rules, see:
355+
//
356+
// https://github.com/servo/rust-bindgen/issues/547,
357+
// https://github.com/rust-lang/rust/issues/38848,
358+
// and https://github.com/rust-lang/rust/issues/40158
359+
//
360+
// Note that copy is always derived, so we don't need to implement it.
361+
impl CanDeriveDebug for FunctionSig {
362+
type Extra = ();
363+
364+
fn can_derive_debug(&self, _ctx: &BindgenContext, _: ()) -> bool {
365+
const RUST_DERIVE_FUNPTR_LIMIT: usize = 12;
366+
if self.argument_types.len() > RUST_DERIVE_FUNPTR_LIMIT {
367+
return false;
368+
}
369+
370+
match self.abi {
371+
Some(abi::Abi::C) | None => true,
372+
_ => false,
373+
}
374+
}
375+
}

src/ir/ty.rs

+8
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,13 @@ impl CanDeriveDebug for Type {
605605
TypeKind::Comp(ref info) => {
606606
info.can_derive_debug(ctx, self.layout(ctx))
607607
}
608+
TypeKind::Pointer(inner) => {
609+
let inner = ctx.resolve_type(inner);
610+
if let TypeKind::Function(ref sig) = *inner.canonical_type(ctx).kind() {
611+
return sig.can_derive_debug(ctx, ());
612+
}
613+
return true;
614+
}
608615
_ => true,
609616
}
610617
}
@@ -636,6 +643,7 @@ impl CanDeriveDefault for Type {
636643
TypeKind::ObjCSel |
637644
TypeKind::ObjCInterface(..) |
638645
TypeKind::Enum(..) => false,
646+
639647
TypeKind::Function(..) |
640648
TypeKind::Int(..) |
641649
TypeKind::Float(..) |

tests/expectations/tests/call-conv-field.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,30 @@
55

66

77
#[repr(C)]
8-
#[derive(Debug, Copy)]
8+
#[derive(Copy)]
99
pub struct JNINativeInterface_ {
1010
pub GetVersion: ::std::option::Option<unsafe extern "stdcall" fn(env:
1111
*mut ::std::os::raw::c_void)
1212
-> ::std::os::raw::c_int>,
13+
pub __hack: ::std::os::raw::c_ulonglong,
1314
}
1415
#[test]
1516
fn bindgen_test_layout_JNINativeInterface_() {
16-
assert_eq!(::std::mem::size_of::<JNINativeInterface_>() , 4usize , concat
17+
assert_eq!(::std::mem::size_of::<JNINativeInterface_>() , 16usize , concat
1718
! ( "Size of: " , stringify ! ( JNINativeInterface_ ) ));
18-
assert_eq! (::std::mem::align_of::<JNINativeInterface_>() , 4usize ,
19+
assert_eq! (::std::mem::align_of::<JNINativeInterface_>() , 8usize ,
1920
concat ! (
2021
"Alignment of " , stringify ! ( JNINativeInterface_ ) ));
2122
assert_eq! (unsafe {
2223
& ( * ( 0 as * const JNINativeInterface_ ) ) . GetVersion as *
2324
const _ as usize } , 0usize , concat ! (
2425
"Alignment of field: " , stringify ! ( JNINativeInterface_ ) ,
2526
"::" , stringify ! ( GetVersion ) ));
27+
assert_eq! (unsafe {
28+
& ( * ( 0 as * const JNINativeInterface_ ) ) . __hack as *
29+
const _ as usize } , 8usize , concat ! (
30+
"Alignment of field: " , stringify ! ( JNINativeInterface_ ) ,
31+
"::" , stringify ! ( __hack ) ));
2632
}
2733
impl Clone for JNINativeInterface_ {
2834
fn clone(&self) -> Self { *self }
+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/* automatically generated by rust-bindgen */
2+
3+
4+
#![allow(non_snake_case)]
5+
6+
7+
pub type my_fun_t =
8+
::std::option::Option<unsafe extern "C" fn(arg1: ::std::os::raw::c_int,
9+
arg2: ::std::os::raw::c_int,
10+
arg3: ::std::os::raw::c_int,
11+
arg4: ::std::os::raw::c_int,
12+
arg5: ::std::os::raw::c_int,
13+
arg6: ::std::os::raw::c_int,
14+
arg7: ::std::os::raw::c_int,
15+
arg8: ::std::os::raw::c_int,
16+
arg9: ::std::os::raw::c_int,
17+
arg10: ::std::os::raw::c_int,
18+
arg11: ::std::os::raw::c_int,
19+
arg12: ::std::os::raw::c_int,
20+
arg13: ::std::os::raw::c_int,
21+
arg14: ::std::os::raw::c_int,
22+
arg15: ::std::os::raw::c_int,
23+
arg16: ::std::os::raw::c_int)>;
24+
#[repr(C)]
25+
#[derive(Copy)]
26+
pub struct Foo {
27+
pub callback: my_fun_t,
28+
}
29+
#[test]
30+
fn bindgen_test_layout_Foo() {
31+
assert_eq!(::std::mem::size_of::<Foo>() , 8usize , concat ! (
32+
"Size of: " , stringify ! ( Foo ) ));
33+
assert_eq! (::std::mem::align_of::<Foo>() , 8usize , concat ! (
34+
"Alignment of " , stringify ! ( Foo ) ));
35+
assert_eq! (unsafe {
36+
& ( * ( 0 as * const Foo ) ) . callback as * const _ as usize
37+
} , 0usize , concat ! (
38+
"Alignment of field: " , stringify ! ( Foo ) , "::" ,
39+
stringify ! ( callback ) ));
40+
}
41+
impl Clone for Foo {
42+
fn clone(&self) -> Self { *self }
43+
}
44+
impl Default for Foo {
45+
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
46+
}

tests/headers/call-conv-field.h

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
struct JNINativeInterface_ {
44
int (__stdcall *GetVersion)(void *env);
5+
unsigned long long __hack; // A hack so the field alignment is the same than
6+
// for 64-bit, where we run CI.
57
};
68

79
__stdcall void bar();

tests/headers/derive-fn-ptr.h

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
typedef void (*my_fun_t)(int, int, int, int,
2+
int, int, int, int,
3+
int, int, int, int,
4+
int, int, int, int);
5+
6+
struct Foo {
7+
my_fun_t callback;
8+
};

0 commit comments

Comments
 (0)