Skip to content

Commit

Permalink
ir: Test on typedefs also for template parameters that are transitive…
Browse files Browse the repository at this point in the history
…ly applicable before discarding them.
  • Loading branch information
emilio committed Oct 14, 2016
1 parent 98abbeb commit 6484e3e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/ir/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,20 @@ impl Item {
self.kind().expect_function()
}

// This check is needed because even though the type might not contain the
// applicable template args itself, they might apply transitively via, for
// example, the parent.
//
// It's kind of unfortunate (in the sense that it's a sort of complex
// process, but I think it gets all the cases).
fn signature_contains_named_type(&self, ctx: &BindgenContext, ty: &Type) -> bool {
debug_assert!(ty.is_named());
self.expect_type().signature_contains_named_type(ctx, ty) ||
self.applicable_template_args(ctx).iter().any(|template| {
ctx.resolve_type(*template).signature_contains_named_type(ctx, ty)
})
}

pub fn applicable_template_args(&self, ctx: &BindgenContext) -> Vec<ItemId> {
let ty = match *self.kind() {
ItemKind::Type(ref ty) => ty,
Expand Down Expand Up @@ -197,7 +211,8 @@ impl Item {
TypeKind::Alias(_, inner) => {
let parent_args = ctx.resolve_item(self.parent_id())
.applicable_template_args(ctx);
let inner = ctx.resolve_type(inner);
let inner = ctx.resolve_item(inner);

// Avoid unused type parameters, sigh.
parent_args.iter().cloned().filter(|arg| {
let arg = ctx.resolve_type(*arg);
Expand Down
18 changes: 18 additions & 0 deletions tests/expectations/template_typedef_transitive_param.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* automatically generated by rust-bindgen */


#![allow(non_snake_case)]


#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct Wrapper<T> {
pub _address: u8,
pub _phantom_0: ::std::marker::PhantomData<T>,
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct Wrapper_Wrapped<T> {
pub t: T,
}
pub type Wrapper_Type<T> = Wrapper_Wrapped<T>;
7 changes: 7 additions & 0 deletions tests/headers/template_typedef_transitive_param.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
template<typename T>
struct Wrapper {
struct Wrapped {
T t;
};
using Type = Wrapped;
};

0 comments on commit 6484e3e

Please sign in to comment.