Skip to content

Commit

Permalink
ir: Whitelist items that don't generate code to improve derive behavior.
Browse files Browse the repository at this point in the history
When not whitelisting recursively.

Fixes rust-lang#1454
  • Loading branch information
emilio committed Jan 16, 2019
1 parent ab538ee commit 4f60664
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,8 @@ impl CodeGenerator for Type {
TypeKind::TypeParam => {
// These items don't need code generation, they only need to be
// converted to rust types in fields, arguments, and such.
// NOTE(emilio): If you add to this list, make sure to also add
// it to BindgenContext::compute_whitelisted_and_codegen_items.
return;
}
TypeKind::TemplateInstantiation(ref inst) => {
Expand Down
22 changes: 22 additions & 0 deletions src/ir/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2315,6 +2315,28 @@ If you encounter an error missing from this list, please file an issue or a PR!"
return true;
}

// Auto-whitelist types that don't need code
// generation if not whitelisting recursively, to
// make the #[derive] analysis not be lame.
if !self.options().whitelist_recursively {
match *ty.kind() {
TypeKind::Void |
TypeKind::NullPtr |
TypeKind::Int(..) |
TypeKind::Float(..) |
TypeKind::Complex(..) |
TypeKind::Array(..) |
TypeKind::Vector(..) |
TypeKind::Pointer(..) |
TypeKind::Reference(..) |
TypeKind::Function(..) |
TypeKind::ResolvedTypeRef(..) |
TypeKind::Opaque |
TypeKind::TypeParam => return true,
_ => {},
};
}

// Unnamed top-level enums are special and we
// whitelist them via the `whitelisted_vars` filter,
// since they're effectively top-level constants,
Expand Down
9 changes: 8 additions & 1 deletion tests/expectations/tests/issue-1285.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
/* automatically generated by rust-bindgen */

#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
#![allow(
dead_code,
non_snake_case,
non_camel_case_types,
non_upper_case_globals
)]

#[repr(C)]
#[derive(Copy, Clone)]
pub struct foo {
pub bar: foo__bindgen_ty_1,
}
#[repr(C)]
#[derive(Copy, Clone)]
pub union foo__bindgen_ty_1 {
pub a: ::std::os::raw::c_uint,
pub b: ::std::os::raw::c_ushort,
Expand Down
9 changes: 7 additions & 2 deletions tests/expectations/tests/no-recursive-whitelisting.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
/* automatically generated by rust-bindgen */


#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
#![allow(
dead_code,
non_snake_case,
non_camel_case_types,
non_upper_case_globals
)]

pub enum Bar {}

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct Foo {
pub baz: *mut Bar,
}
Expand Down

0 comments on commit 4f60664

Please sign in to comment.