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],

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ pub struct c {
4141
pub __bindgen_anon_1: c__bindgen_ty_1,
4242
}
4343
#[repr(C)]
44-
#[repr(align(1))]
4544
#[derive(Copy, Clone)]
4645
pub union c__bindgen_ty_1 {
4746
_bindgen_union_align: u8,

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
/// Deriving PartialEq for rust unions is not supported.
1111
#[repr(C)]
12-
#[repr(align(4))]
1312
#[derive(Copy, Clone)]
1413
pub union ShouldNotDerivePartialEq {
1514
pub a: ::std::os::raw::c_char,

tests/expectations/tests/issue-1285.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ pub struct foo {
1212
pub bar: foo__bindgen_ty_1,
1313
}
1414
#[repr(C)]
15-
#[repr(align(4))]
1615
pub union foo__bindgen_ty_1 {
1716
pub a: ::std::os::raw::c_uint,
1817
pub b: ::std::os::raw::c_ushort,
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
/* automatically generated by rust-bindgen */
2+
3+
#![allow(
4+
dead_code,
5+
non_snake_case,
6+
non_camel_case_types,
7+
non_upper_case_globals
8+
)]
9+
10+
#[repr(C, packed)]
11+
#[derive(Copy, Clone)]
12+
pub struct rte_memseg {
13+
///< Start physical address.
14+
pub phys_addr: u64,
15+
pub __bindgen_anon_1: rte_memseg__bindgen_ty_1,
16+
///< Length of the segment.
17+
pub len: usize,
18+
///< The pagesize of underlying memory
19+
pub hugepage_sz: u64,
20+
///< NUMA socket ID.
21+
pub socket_id: i32,
22+
///< Number of channels.
23+
pub nchannel: u32,
24+
///< Number of ranks.
25+
pub nrank: u32,
26+
}
27+
#[repr(C)]
28+
#[derive(Copy, Clone)]
29+
pub union rte_memseg__bindgen_ty_1 {
30+
///< Start virtual address.
31+
pub addr: *mut ::std::os::raw::c_void,
32+
///< Makes sure addr is always 64 bits
33+
pub addr_64: u64,
34+
_bindgen_union_align: u64,
35+
}
36+
#[test]
37+
fn bindgen_test_layout_rte_memseg__bindgen_ty_1() {
38+
assert_eq!(
39+
::std::mem::size_of::<rte_memseg__bindgen_ty_1>(),
40+
8usize,
41+
concat!("Size of: ", stringify!(rte_memseg__bindgen_ty_1))
42+
);
43+
assert_eq!(
44+
::std::mem::align_of::<rte_memseg__bindgen_ty_1>(),
45+
8usize,
46+
concat!("Alignment of ", stringify!(rte_memseg__bindgen_ty_1))
47+
);
48+
assert_eq!(
49+
unsafe { &(*(::std::ptr::null::<rte_memseg__bindgen_ty_1>())).addr as *const _ as usize },
50+
0usize,
51+
concat!(
52+
"Offset of field: ",
53+
stringify!(rte_memseg__bindgen_ty_1),
54+
"::",
55+
stringify!(addr)
56+
)
57+
);
58+
assert_eq!(
59+
unsafe {
60+
&(*(::std::ptr::null::<rte_memseg__bindgen_ty_1>())).addr_64 as *const _ as usize
61+
},
62+
0usize,
63+
concat!(
64+
"Offset of field: ",
65+
stringify!(rte_memseg__bindgen_ty_1),
66+
"::",
67+
stringify!(addr_64)
68+
)
69+
);
70+
}
71+
impl Default for rte_memseg__bindgen_ty_1 {
72+
fn default() -> Self {
73+
unsafe { ::std::mem::zeroed() }
74+
}
75+
}
76+
#[test]
77+
fn bindgen_test_layout_rte_memseg() {
78+
assert_eq!(
79+
::std::mem::size_of::<rte_memseg>(),
80+
44usize,
81+
concat!("Size of: ", stringify!(rte_memseg))
82+
);
83+
assert_eq!(
84+
::std::mem::align_of::<rte_memseg>(),
85+
1usize,
86+
concat!("Alignment of ", stringify!(rte_memseg))
87+
);
88+
assert_eq!(
89+
unsafe { &(*(::std::ptr::null::<rte_memseg>())).phys_addr as *const _ as usize },
90+
0usize,
91+
concat!(
92+
"Offset of field: ",
93+
stringify!(rte_memseg),
94+
"::",
95+
stringify!(phys_addr)
96+
)
97+
);
98+
assert_eq!(
99+
unsafe { &(*(::std::ptr::null::<rte_memseg>())).len as *const _ as usize },
100+
16usize,
101+
concat!(
102+
"Offset of field: ",
103+
stringify!(rte_memseg),
104+
"::",
105+
stringify!(len)
106+
)
107+
);
108+
assert_eq!(
109+
unsafe { &(*(::std::ptr::null::<rte_memseg>())).hugepage_sz as *const _ as usize },
110+
24usize,
111+
concat!(
112+
"Offset of field: ",
113+
stringify!(rte_memseg),
114+
"::",
115+
stringify!(hugepage_sz)
116+
)
117+
);
118+
assert_eq!(
119+
unsafe { &(*(::std::ptr::null::<rte_memseg>())).socket_id as *const _ as usize },
120+
32usize,
121+
concat!(
122+
"Offset of field: ",
123+
stringify!(rte_memseg),
124+
"::",
125+
stringify!(socket_id)
126+
)
127+
);
128+
assert_eq!(
129+
unsafe { &(*(::std::ptr::null::<rte_memseg>())).nchannel as *const _ as usize },
130+
36usize,
131+
concat!(
132+
"Offset of field: ",
133+
stringify!(rte_memseg),
134+
"::",
135+
stringify!(nchannel)
136+
)
137+
);
138+
assert_eq!(
139+
unsafe { &(*(::std::ptr::null::<rte_memseg>())).nrank as *const _ as usize },
140+
40usize,
141+
concat!(
142+
"Offset of field: ",
143+
stringify!(rte_memseg),
144+
"::",
145+
stringify!(nrank)
146+
)
147+
);
148+
}
149+
impl Default for rte_memseg {
150+
fn default() -> Self {
151+
unsafe { ::std::mem::zeroed() }
152+
}
153+
}

tests/expectations/tests/issue-493.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ pub struct basic_string___short {
8383
pub __data_: *mut basic_string_value_type,
8484
}
8585
#[repr(C)]
86-
#[repr(align(1))]
8786
pub union basic_string___short__bindgen_ty_1 {
8887
pub __size_: ::std::os::raw::c_uchar,
8988
pub __lx: basic_string_value_type,

0 commit comments

Comments
 (0)