Skip to content

Commit 76d1959

Browse files
committed
ir: Whitelist items that don't generate code to improve derive behavior.
When not whitelisting recursively. Fixes rust-lang#1454
1 parent d699813 commit 76d1959

File tree

4 files changed

+37
-49
lines changed

4 files changed

+37
-49
lines changed

src/codegen/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,8 @@ impl CodeGenerator for Type {
610610
TypeKind::TypeParam => {
611611
// These items don't need code generation, they only need to be
612612
// converted to rust types in fields, arguments, and such.
613+
// NOTE(emilio): If you add to this list, make sure to also add
614+
// it to BindgenContext::compute_whitelisted_and_codegen_items.
613615
return;
614616
}
615617
TypeKind::TemplateInstantiation(ref inst) => {

src/ir/context.rs

+22
Original file line numberDiff line numberDiff line change
@@ -2314,6 +2314,28 @@ If you encounter an error missing from this list, please file an issue or a PR!"
23142314
return true;
23152315
}
23162316

2317+
// Auto-whitelist types that don't need code
2318+
// generation if not whitelisting recursively, to
2319+
// make the #[derive] analysis not be lame.
2320+
if !self.options().whitelist_recursively {
2321+
match *ty.kind() {
2322+
TypeKind::Void |
2323+
TypeKind::NullPtr |
2324+
TypeKind::Int(..) |
2325+
TypeKind::Float(..) |
2326+
TypeKind::Complex(..) |
2327+
TypeKind::Array(..) |
2328+
TypeKind::Vector(..) |
2329+
TypeKind::Pointer(..) |
2330+
TypeKind::Reference(..) |
2331+
TypeKind::Function(..) |
2332+
TypeKind::ResolvedTypeRef(..) |
2333+
TypeKind::Opaque |
2334+
TypeKind::TypeParam => return true,
2335+
_ => {},
2336+
};
2337+
}
2338+
23172339
// Unnamed top-level enums are special and we
23182340
// whitelist them via the `whitelisted_vars` filter,
23192341
// since they're effectively top-level constants,

tests/expectations/tests/issue-1285.rs

+6-47
Original file line numberDiff line numberDiff line change
@@ -8,57 +8,16 @@
88
)]
99

1010
#[repr(C)]
11-
pub struct __BindgenUnionField<T>(::std::marker::PhantomData<T>);
12-
impl<T> __BindgenUnionField<T> {
13-
#[inline]
14-
pub fn new() -> Self {
15-
__BindgenUnionField(::std::marker::PhantomData)
16-
}
17-
#[inline]
18-
pub unsafe fn as_ref(&self) -> &T {
19-
::std::mem::transmute(self)
20-
}
21-
#[inline]
22-
pub unsafe fn as_mut(&mut self) -> &mut T {
23-
::std::mem::transmute(self)
24-
}
25-
}
26-
impl<T> ::std::default::Default for __BindgenUnionField<T> {
27-
#[inline]
28-
fn default() -> Self {
29-
Self::new()
30-
}
31-
}
32-
impl<T> ::std::clone::Clone for __BindgenUnionField<T> {
33-
#[inline]
34-
fn clone(&self) -> Self {
35-
Self::new()
36-
}
37-
}
38-
impl<T> ::std::marker::Copy for __BindgenUnionField<T> {}
39-
impl<T> ::std::fmt::Debug for __BindgenUnionField<T> {
40-
fn fmt(&self, fmt: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
41-
fmt.write_str("__BindgenUnionField")
42-
}
43-
}
44-
impl<T> ::std::hash::Hash for __BindgenUnionField<T> {
45-
fn hash<H: ::std::hash::Hasher>(&self, _state: &mut H) {}
46-
}
47-
impl<T> ::std::cmp::PartialEq for __BindgenUnionField<T> {
48-
fn eq(&self, _other: &__BindgenUnionField<T>) -> bool {
49-
true
50-
}
51-
}
52-
impl<T> ::std::cmp::Eq for __BindgenUnionField<T> {}
53-
#[repr(C)]
11+
#[derive(Copy, Clone)]
5412
pub struct foo {
5513
pub bar: foo__bindgen_ty_1,
5614
}
5715
#[repr(C)]
58-
pub struct foo__bindgen_ty_1 {
59-
pub a: __BindgenUnionField<::std::os::raw::c_uint>,
60-
pub b: __BindgenUnionField<::std::os::raw::c_ushort>,
61-
pub bindgen_union_field: u32,
16+
#[derive(Copy, Clone)]
17+
pub union foo__bindgen_ty_1 {
18+
pub a: ::std::os::raw::c_uint,
19+
pub b: ::std::os::raw::c_ushort,
20+
_bindgen_union_align: u32,
6221
}
6322
#[test]
6423
fn bindgen_test_layout_foo__bindgen_ty_1() {

tests/expectations/tests/no-recursive-whitelisting.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
/* automatically generated by rust-bindgen */
22

3-
4-
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
3+
#![allow(
4+
dead_code,
5+
non_snake_case,
6+
non_camel_case_types,
7+
non_upper_case_globals
8+
)]
59

610
pub enum Bar {}
711

812
#[repr(C)]
13+
#[derive(Debug, Copy, Clone)]
914
pub struct Foo {
1015
pub baz: *mut Bar,
1116
}

0 commit comments

Comments
 (0)