Skip to content

Commit efb6e0c

Browse files
committed
ty: Use underlying layout of typedefs if available.
This fixes struct layout issues with over-aligned typedefs, which rust can't represent, see #3449 for context and some other discussion. This is kinda ugly tho, but as far as I can tell there's no good way of getting the right ABI and struct layout at the same time...
1 parent 18fc3ac commit efb6e0c

3 files changed

Lines changed: 78 additions & 0 deletions

File tree

‎bindgen-tests/tests/expectations/tests/issue-3406.rs‎

Lines changed: 50 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎bindgen-tests/tests/headers/issue-3406.h‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,26 @@ struct __attribute__((aligned(16))) Inner {
22
char byte;
33
};
44

5+
typedef int AlignedInt __attribute__((aligned(16)));
6+
typedef AlignedInt NestedAlignedInt;
7+
58
struct Outer {
69
int before;
710
struct Inner inner;
811
};
12+
13+
struct Outer2 {
14+
int before;
15+
AlignedInt one;
16+
AlignedInt two;
17+
};
18+
19+
struct Outer3 {
20+
int before;
21+
AlignedInt inner;
22+
};
23+
24+
struct Outer4 {
25+
int before;
26+
NestedAlignedInt inner;
27+
};

‎bindgen/ir/ty.rs‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,15 @@ impl Type {
218218

219219
/// What is the layout of this type?
220220
pub(crate) fn layout(&self, ctx: &BindgenContext) -> Option<Layout> {
221+
if let TypeKind::Alias(inner) | TypeKind::ResolvedTypeRef(inner) =
222+
self.kind
223+
{
224+
// HACK(emilio): Rust can't represent over-aligned typedefs, so prefer the inner type's
225+
// layout if available, to get struct layout correct at least...
226+
if let Some(l) = ctx.resolve_type(inner).layout(ctx) {
227+
return Some(l);
228+
}
229+
}
221230
self.layout.or_else(|| {
222231
match self.kind {
223232
TypeKind::Comp(ref ci) => ci.layout(ctx),

0 commit comments

Comments
 (0)