Skip to content

Commit 821b133

Browse files
author
bors-servo
authored
Auto merge of #1239 - emilio:cyclic-typedef-better, r=fitzgen
codegen: Make the cyclic typedef name detection catch more cases. By looking through typedefs, we also catch more complex cases like the ones that appear on Android's stdlib. Fixes #946
2 parents 8725aea + 450970d commit 821b133

File tree

3 files changed

+40
-8
lines changed

3 files changed

+40
-8
lines changed

src/codegen/mod.rs

+14-8
Original file line numberDiff line numberDiff line change
@@ -614,14 +614,20 @@ impl CodeGenerator for Type {
614614
.resolve(ctx);
615615
let name = item.canonical_name(ctx);
616616

617-
// Try to catch the common pattern:
618-
//
619-
// typedef struct foo { ... } foo;
620-
//
621-
// here.
622-
//
623-
if inner_item.canonical_name(ctx) == name {
624-
return;
617+
{
618+
let through_type_aliases = inner.into_resolver()
619+
.through_type_refs()
620+
.through_type_aliases()
621+
.resolve(ctx);
622+
623+
// Try to catch the common pattern:
624+
//
625+
// typedef struct foo { ... } foo;
626+
//
627+
// here, and also other more complex cases like #946.
628+
if through_type_aliases.canonical_name(ctx) == name {
629+
return;
630+
}
625631
}
626632

627633
// If this is a known named type, disallow generating anything

tests/expectations/tests/issue-946.rs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/* automatically generated by rust-bindgen */
2+
3+
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
4+
5+
#[repr(C)]
6+
#[derive(Debug, Default, Copy, Clone)]
7+
pub struct foo {}
8+
#[test]
9+
fn bindgen_test_layout_foo() {
10+
assert_eq!(
11+
::std::mem::size_of::<foo>(),
12+
0usize,
13+
concat!("Size of: ", stringify!(foo))
14+
);
15+
assert_eq!(
16+
::std::mem::align_of::<foo>(),
17+
1usize,
18+
concat!("Alignment of ", stringify!(foo))
19+
);
20+
}
21+
pub type bar = foo;

tests/headers/issue-946.h

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
struct foo { };
2+
3+
typedef struct foo bar;
4+
5+
typedef bar foo;

0 commit comments

Comments
 (0)