Skip to content

Commit 4f60664

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 ab538ee commit 4f60664

File tree

4 files changed

+39
-3
lines changed

4 files changed

+39
-3
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
@@ -2315,6 +2315,28 @@ If you encounter an error missing from this list, please file an issue or a PR!"
23152315
return true;
23162316
}
23172317

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

tests/expectations/tests/issue-1285.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
/* automatically generated by rust-bindgen */
22

3-
#![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+
)]
49

510
#[repr(C)]
11+
#[derive(Copy, Clone)]
612
pub struct foo {
713
pub bar: foo__bindgen_ty_1,
814
}
915
#[repr(C)]
16+
#[derive(Copy, Clone)]
1017
pub union foo__bindgen_ty_1 {
1118
pub a: ::std::os::raw::c_uint,
1219
pub b: ::std::os::raw::c_ushort,

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)