Skip to content

Commit 9134679

Browse files
committed
codegen: Properly track alignment of unions.
This makes us not unnecessarily add repr(align) to unions. Closes #1498.
1 parent ed6e1bb commit 9134679

36 files changed

+201
-73
lines changed

CHANGELOG.md

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,53 +9,56 @@
99
- [Removed](#removed)
1010
- [Fixed](#fixed)
1111
- [Security](#security)
12-
- [0.47.3](#0473)
12+
- [0.48.0](#0480)
1313
- [Changed](#changed-1)
14-
- [0.47.2](#0472)
1514
- [Fixed](#fixed-1)
16-
- [0.47.1](#0471)
15+
- [0.47.3](#0473)
1716
- [Changed](#changed-2)
17+
- [0.47.2](#0472)
1818
- [Fixed](#fixed-2)
19-
- [0.47.0](#0470)
19+
- [0.47.1](#0471)
2020
- [Changed](#changed-3)
2121
- [Fixed](#fixed-3)
22+
- [0.47.0](#0470)
23+
- [Changed](#changed-4)
24+
- [Fixed](#fixed-4)
2225
- [0.33.1 .. 0.46.0](#0331--0460)
2326
- [Added](#added-1)
2427
- [Removed](#removed-1)
25-
- [Changed](#changed-4)
26-
- [Fixed](#fixed-4)
27-
- [0.33.1](#0331)
28+
- [Changed](#changed-5)
2829
- [Fixed](#fixed-5)
30+
- [0.33.1](#0331)
31+
- [Fixed](#fixed-6)
2932
- [0.33.0](#0330)
3033
- [Added](#added-2)
31-
- [Changed](#changed-5)
34+
- [Changed](#changed-6)
3235
- [Deprecated](#deprecated-1)
3336
- [Removed](#removed-2)
34-
- [Fixed](#fixed-6)
37+
- [Fixed](#fixed-7)
3538
- [Security](#security-1)
3639
- [0.32.2](#0322)
37-
- [Fixed](#fixed-7)
38-
- [0.32.1](#0321)
3940
- [Fixed](#fixed-8)
41+
- [0.32.1](#0321)
42+
- [Fixed](#fixed-9)
4043
- [0.32.0](#0320)
4144
- [Added](#added-3)
42-
- [Changed](#changed-6)
43-
- [Fixed](#fixed-9)
45+
- [Changed](#changed-7)
46+
- [Fixed](#fixed-10)
4447
- [0.31.0](#0310)
4548
- [Added](#added-4)
46-
- [Changed](#changed-7)
49+
- [Changed](#changed-8)
4750
- [Deprecated](#deprecated-2)
4851
- [Removed](#removed-3)
49-
- [Fixed](#fixed-10)
52+
- [Fixed](#fixed-11)
5053
- [0.30.0](#0300)
5154
- [Added](#added-5)
52-
- [Changed](#changed-8)
55+
- [Changed](#changed-9)
5356
- [Deprecated](#deprecated-3)
54-
- [Fixed](#fixed-11)
57+
- [Fixed](#fixed-12)
5558
- [0.29.0](#0290)
5659
- [Added](#added-6)
57-
- [Changed](#changed-9)
58-
- [Fixed](#fixed-12)
60+
- [Changed](#changed-10)
61+
- [Fixed](#fixed-13)
5962

6063
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
6164

@@ -101,7 +104,12 @@ Released 2019/03/04
101104
* Default rust target was changed to 1.33, which means that bindgen can get much
102105
more often the layout of structs right. [#1529][]
103106

107+
## Fixed
108+
109+
* Bindgen will output repr(align) just when needed for unions. [#1498][]
110+
104111
[#1529]: https://github.com/rust-lang-nursery/rust-bindgen/issues/1529
112+
[#1498]: https://github.com/rust-lang-nursery/rust-bindgen/issues/1498
105113

106114
--------------------------------------------------------------------------------
107115

src/codegen/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1589,6 +1589,7 @@ impl CodeGenerator for CompInfo {
15891589
// TODO(emilio): It'd be nice to unify this with the struct path
15901590
// above somehow.
15911591
let layout = layout.expect("Unable to get layout information?");
1592+
struct_layout.saw_union(layout);
15921593

15931594
if struct_layout.requires_explicit_align(layout) {
15941595
explicit_align = Some(layout.align);
@@ -1600,8 +1601,6 @@ impl CodeGenerator for CompInfo {
16001601
_bindgen_union_align: #ty ,
16011602
}
16021603
} else {
1603-
struct_layout.saw_union(layout);
1604-
16051604
quote! {
16061605
pub bindgen_union_field: #ty ,
16071606
}

src/codegen/struct_layout.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@ impl<'a> StructLayoutTracker<'a> {
8585
name: &'a str,
8686
) -> Self {
8787
StructLayoutTracker {
88-
name: name,
89-
ctx: ctx,
90-
comp: comp,
88+
name,
89+
ctx,
90+
comp,
9191
is_packed: comp.is_packed(ctx, &ty.layout(ctx)),
9292
latest_offset: 0,
9393
padding_count: 0,

tests/expectations/tests/16-byte-alignment.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ pub struct rte_ipv4_tuple {
1515
pub __bindgen_anon_1: rte_ipv4_tuple__bindgen_ty_1,
1616
}
1717
#[repr(C)]
18-
#[repr(align(4))]
1918
#[derive(Copy, Clone)]
2019
pub union rte_ipv4_tuple__bindgen_ty_1 {
2120
pub __bindgen_anon_1: rte_ipv4_tuple__bindgen_ty_1__bindgen_ty_1,
@@ -149,7 +148,6 @@ pub struct rte_ipv6_tuple {
149148
pub __bindgen_anon_1: rte_ipv6_tuple__bindgen_ty_1,
150149
}
151150
#[repr(C)]
152-
#[repr(align(4))]
153151
#[derive(Copy, Clone)]
154152
pub union rte_ipv6_tuple__bindgen_ty_1 {
155153
pub __bindgen_anon_1: rte_ipv6_tuple__bindgen_ty_1__bindgen_ty_1,

tests/expectations/tests/anon_struct_in_union.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ pub struct s {
1313
pub u: s__bindgen_ty_1,
1414
}
1515
#[repr(C)]
16-
#[repr(align(4))]
1716
#[derive(Copy, Clone)]
1817
pub union s__bindgen_ty_1 {
1918
pub field: s__bindgen_ty_1_inner,

tests/expectations/tests/anon_union.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ pub struct TErrorResult_DOMExceptionInfo {
3333
_unused: [u8; 0],
3434
}
3535
#[repr(C)]
36-
#[repr(align(8))]
3736
pub union TErrorResult__bindgen_ty_1 {
3837
pub mMessage: *mut TErrorResult_Message,
3938
pub mDOMExceptionInfo: *mut TErrorResult_DOMExceptionInfo,

tests/expectations/tests/class.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,6 @@ impl Default for IncompleteArrayNonCopiable {
335335
}
336336
}
337337
#[repr(C)]
338-
#[repr(align(4))]
339338
#[derive(Copy, Clone)]
340339
pub union Union {
341340
pub d: f32,

tests/expectations/tests/class_with_inner_struct.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ fn bindgen_test_layout_A_Segment() {
5454
);
5555
}
5656
#[repr(C)]
57-
#[repr(align(4))]
5857
#[derive(Copy, Clone)]
5958
pub union A__bindgen_ty_1 {
6059
pub f: ::std::os::raw::c_int,
@@ -89,7 +88,6 @@ impl Default for A__bindgen_ty_1 {
8988
}
9089
}
9190
#[repr(C)]
92-
#[repr(align(4))]
9391
#[derive(Copy, Clone)]
9492
pub union A__bindgen_ty_2 {
9593
pub d: ::std::os::raw::c_int,
@@ -233,7 +231,6 @@ pub struct C {
233231
pub __bindgen_anon_1: C__bindgen_ty_1,
234232
}
235233
#[repr(C)]
236-
#[repr(align(4))]
237234
#[derive(Copy, Clone)]
238235
pub union C__bindgen_ty_1 {
239236
pub mFunc: C__bindgen_ty_1__bindgen_ty_1,

tests/expectations/tests/derive-debug-mangle-name.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ pub struct perf_event_attr {
1515
pub __bindgen_anon_1: perf_event_attr__bindgen_ty_1,
1616
}
1717
#[repr(C)]
18-
#[repr(align(4))]
1918
#[derive(Copy, Clone)]
2019
pub union perf_event_attr__bindgen_ty_1 {
2120
pub b: ::std::os::raw::c_int,

tests/expectations/tests/derive-partialeq-anonfield.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ pub struct rte_mbuf {
1414
pub __bindgen_anon_1: rte_mbuf__bindgen_ty_1,
1515
}
1616
#[repr(C)]
17-
#[repr(align(1))]
1817
#[derive(Copy, Clone)]
1918
pub union rte_mbuf__bindgen_ty_1 {
2019
_bindgen_union_align: [u8; 0usize],

0 commit comments

Comments
 (0)