Skip to content

Commit 1c76aca

Browse files
committed
Ensure that every item is in some module's children list
Previously, if an item's parent was not a module (eg a nested class definition whose parent it the outer class definition) and the parent was not whitelisted but the item was transitively whitelisted, then we could generate uses of the item without emitting any definition for it. This could happen because we were relying on the outer type calling for code generation on its inner types, but that relies on us doing code generation for the outer type, which won't happen if the outer type is not whitelisted. This commit avoids this gotcha by ensuring that all items end up in a module's children list, and so will be code generated even if their parent is not whitelisted. Fixes rust-lang#769
1 parent 77d6962 commit 1c76aca

File tree

110 files changed

+1058
-755
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

110 files changed

+1058
-755
lines changed

src/codegen/mod.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,19 @@ impl CodeGenerator for Var {
435435
}
436436
result.saw_var(&canonical_name);
437437

438+
// We can't generate bindings to static variables of template type
439+
// parameters. The types are open ended and we don't know what
440+
// instantiations do or don't exist. Additionally, there simply isn't a
441+
// way to express this in Rust.
442+
if self.ty()
443+
.into_resolver()
444+
.through_type_refs()
445+
.through_type_aliases()
446+
.resolve(ctx)
447+
.is_named(ctx, &()) {
448+
return;
449+
}
450+
438451
let ty = self.ty().to_rust_ty_or_opaque(ctx, &());
439452

440453
if let Some(val) = self.val() {

src/ir/context.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,14 +348,31 @@ impl<'ctx> BindgenContext<'ctx> {
348348
let is_template_instantiation =
349349
is_type && item.expect_type().is_template_instantiation();
350350

351-
// Be sure to track all the generated children under namespace, even
352-
// those generated after resolving typerefs, etc.
351+
// Ensure that every item (other than the root module) is in a module's
352+
// children list.
353353
if item.id() != item.parent_id() {
354+
let mut added_as_child = false;
355+
354356
if let Some(mut parent) = self.items.get_mut(&item.parent_id()) {
355357
if let Some(mut module) = parent.as_module_mut() {
356358
module.children_mut().push(item.id());
359+
added_as_child = true;
357360
}
358361
}
362+
363+
if !added_as_child {
364+
if let Some(mut current_module) = self.items.get_mut(&self.current_module) {
365+
current_module.as_module_mut()
366+
.expect("self.current_module should always be a module")
367+
.children_mut()
368+
.push(item.id());
369+
added_as_child = true;
370+
}
371+
}
372+
373+
assert!(added_as_child,
374+
"We should have added the item as a child of its parent \
375+
module or the current module");
359376
}
360377

361378
if is_type && item.expect_type().is_comp() {

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

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,6 @@ impl <T> ::std::fmt::Debug for __BindgenUnionField<T> {
3030
}
3131
#[repr(C)]
3232
#[derive(Debug, Default, Copy)]
33-
pub struct rte_ipv4_tuple {
34-
pub src_addr: u32,
35-
pub dst_addr: u32,
36-
pub __bindgen_anon_1: rte_ipv4_tuple__bindgen_ty_1,
37-
}
38-
#[repr(C)]
39-
#[derive(Debug, Default, Copy)]
40-
pub struct rte_ipv4_tuple__bindgen_ty_1 {
41-
pub __bindgen_anon_1: __BindgenUnionField<rte_ipv4_tuple__bindgen_ty_1__bindgen_ty_1>,
42-
pub sctp_tag: __BindgenUnionField<u32>,
43-
pub bindgen_union_field: u32,
44-
}
45-
#[repr(C)]
46-
#[derive(Debug, Default, Copy)]
4733
pub struct rte_ipv4_tuple__bindgen_ty_1__bindgen_ty_1 {
4834
pub dport: u16,
4935
pub sport: u16,
@@ -76,6 +62,13 @@ fn bindgen_test_layout_rte_ipv4_tuple__bindgen_ty_1__bindgen_ty_1() {
7662
impl Clone for rte_ipv4_tuple__bindgen_ty_1__bindgen_ty_1 {
7763
fn clone(&self) -> Self { *self }
7864
}
65+
#[repr(C)]
66+
#[derive(Debug, Default, Copy)]
67+
pub struct rte_ipv4_tuple__bindgen_ty_1 {
68+
pub __bindgen_anon_1: __BindgenUnionField<rte_ipv4_tuple__bindgen_ty_1__bindgen_ty_1>,
69+
pub sctp_tag: __BindgenUnionField<u32>,
70+
pub bindgen_union_field: u32,
71+
}
7972
#[test]
8073
fn bindgen_test_layout_rte_ipv4_tuple__bindgen_ty_1() {
8174
assert_eq!(::std::mem::size_of::<rte_ipv4_tuple__bindgen_ty_1>() , 4usize
@@ -95,6 +88,13 @@ fn bindgen_test_layout_rte_ipv4_tuple__bindgen_ty_1() {
9588
impl Clone for rte_ipv4_tuple__bindgen_ty_1 {
9689
fn clone(&self) -> Self { *self }
9790
}
91+
#[repr(C)]
92+
#[derive(Debug, Default, Copy)]
93+
pub struct rte_ipv4_tuple {
94+
pub src_addr: u32,
95+
pub dst_addr: u32,
96+
pub __bindgen_anon_1: rte_ipv4_tuple__bindgen_ty_1,
97+
}
9898
#[test]
9999
fn bindgen_test_layout_rte_ipv4_tuple() {
100100
assert_eq!(::std::mem::size_of::<rte_ipv4_tuple>() , 12usize , concat ! (
@@ -117,20 +117,6 @@ impl Clone for rte_ipv4_tuple {
117117
}
118118
#[repr(C)]
119119
#[derive(Debug, Default, Copy)]
120-
pub struct rte_ipv6_tuple {
121-
pub src_addr: [u8; 16usize],
122-
pub dst_addr: [u8; 16usize],
123-
pub __bindgen_anon_1: rte_ipv6_tuple__bindgen_ty_1,
124-
}
125-
#[repr(C)]
126-
#[derive(Debug, Default, Copy)]
127-
pub struct rte_ipv6_tuple__bindgen_ty_1 {
128-
pub __bindgen_anon_1: __BindgenUnionField<rte_ipv6_tuple__bindgen_ty_1__bindgen_ty_1>,
129-
pub sctp_tag: __BindgenUnionField<u32>,
130-
pub bindgen_union_field: u32,
131-
}
132-
#[repr(C)]
133-
#[derive(Debug, Default, Copy)]
134120
pub struct rte_ipv6_tuple__bindgen_ty_1__bindgen_ty_1 {
135121
pub dport: u16,
136122
pub sport: u16,
@@ -163,6 +149,13 @@ fn bindgen_test_layout_rte_ipv6_tuple__bindgen_ty_1__bindgen_ty_1() {
163149
impl Clone for rte_ipv6_tuple__bindgen_ty_1__bindgen_ty_1 {
164150
fn clone(&self) -> Self { *self }
165151
}
152+
#[repr(C)]
153+
#[derive(Debug, Default, Copy)]
154+
pub struct rte_ipv6_tuple__bindgen_ty_1 {
155+
pub __bindgen_anon_1: __BindgenUnionField<rte_ipv6_tuple__bindgen_ty_1__bindgen_ty_1>,
156+
pub sctp_tag: __BindgenUnionField<u32>,
157+
pub bindgen_union_field: u32,
158+
}
166159
#[test]
167160
fn bindgen_test_layout_rte_ipv6_tuple__bindgen_ty_1() {
168161
assert_eq!(::std::mem::size_of::<rte_ipv6_tuple__bindgen_ty_1>() , 4usize
@@ -182,6 +175,13 @@ fn bindgen_test_layout_rte_ipv6_tuple__bindgen_ty_1() {
182175
impl Clone for rte_ipv6_tuple__bindgen_ty_1 {
183176
fn clone(&self) -> Self { *self }
184177
}
178+
#[repr(C)]
179+
#[derive(Debug, Default, Copy)]
180+
pub struct rte_ipv6_tuple {
181+
pub src_addr: [u8; 16usize],
182+
pub dst_addr: [u8; 16usize],
183+
pub __bindgen_anon_1: rte_ipv6_tuple__bindgen_ty_1,
184+
}
185185
#[test]
186186
fn bindgen_test_layout_rte_ipv6_tuple() {
187187
assert_eq!(::std::mem::size_of::<rte_ipv6_tuple>() , 36usize , concat ! (

tests/expectations/tests/381-decltype-alias.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
55

66

7+
pub type std_allocator_traits___size_type<_Alloc> = _Alloc;
78
#[repr(C)]
89
#[derive(Debug, Default, Copy, Clone)]
910
pub struct std_allocator_traits {
1011
pub _address: u8,
1112
}
12-
pub type std_allocator_traits___size_type<_Alloc> = _Alloc;

tests/expectations/tests/anon_enum.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
55

66

7+
pub const Test_T_NONE: Test__bindgen_ty_1 = Test__bindgen_ty_1::T_NONE;
8+
#[repr(u32)]
9+
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
10+
pub enum Test__bindgen_ty_1 { T_NONE = 0, }
711
#[repr(C)]
812
#[derive(Debug, Default, Copy)]
913
pub struct Test {
1014
pub foo: ::std::os::raw::c_int,
1115
pub bar: f32,
1216
}
13-
pub const Test_T_NONE: Test__bindgen_ty_1 = Test__bindgen_ty_1::T_NONE;
14-
#[repr(u32)]
15-
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
16-
pub enum Test__bindgen_ty_1 { T_NONE = 0, }
1717
#[test]
1818
fn bindgen_test_layout_Test() {
1919
assert_eq!(::std::mem::size_of::<Test>() , 8usize , concat ! (

tests/expectations/tests/anon_enum_trait.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@
44
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
55

66

7-
#[repr(C)]
8-
#[derive(Debug, Default, Copy, Clone)]
9-
pub struct DataType {
10-
pub _address: u8,
11-
}
127
pub type DataType_value_type<_Tp> = _Tp;
138
pub type DataType_work_type<_Tp> = DataType_value_type<_Tp>;
149
pub type DataType_channel_type<_Tp> = DataType_value_type<_Tp>;
@@ -27,15 +22,20 @@ pub const DataType_type_: DataType__bindgen_ty_1 =
2722
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
2823
pub enum DataType__bindgen_ty_1 { generic_type = 0, }
2924
#[repr(C)]
30-
#[derive(Debug, Default, Copy)]
31-
pub struct Foo {
25+
#[derive(Debug, Default, Copy, Clone)]
26+
pub struct DataType {
3227
pub _address: u8,
3328
}
3429
pub const Foo_Bar: Foo__bindgen_ty_1 = Foo__bindgen_ty_1::Bar;
3530
pub const Foo_Baz: Foo__bindgen_ty_1 = Foo__bindgen_ty_1::Bar;
3631
#[repr(u32)]
3732
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
3833
pub enum Foo__bindgen_ty_1 { Bar = 0, }
34+
#[repr(C)]
35+
#[derive(Debug, Default, Copy)]
36+
pub struct Foo {
37+
pub _address: u8,
38+
}
3939
#[test]
4040
fn bindgen_test_layout_Foo() {
4141
assert_eq!(::std::mem::size_of::<Foo>() , 1usize , concat ! (

tests/expectations/tests/anon_struct_in_union.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,6 @@ impl <T> ::std::fmt::Debug for __BindgenUnionField<T> {
3030
}
3131
#[repr(C)]
3232
#[derive(Debug, Default, Copy)]
33-
pub struct s {
34-
pub u: s__bindgen_ty_1,
35-
}
36-
#[repr(C)]
37-
#[derive(Debug, Default, Copy)]
38-
pub struct s__bindgen_ty_1 {
39-
pub field: __BindgenUnionField<s__bindgen_ty_1_inner>,
40-
pub bindgen_union_field: u32,
41-
}
42-
#[repr(C)]
43-
#[derive(Debug, Default, Copy)]
4433
pub struct s__bindgen_ty_1_inner {
4534
pub b: ::std::os::raw::c_int,
4635
}
@@ -61,6 +50,12 @@ fn bindgen_test_layout_s__bindgen_ty_1_inner() {
6150
impl Clone for s__bindgen_ty_1_inner {
6251
fn clone(&self) -> Self { *self }
6352
}
53+
#[repr(C)]
54+
#[derive(Debug, Default, Copy)]
55+
pub struct s__bindgen_ty_1 {
56+
pub field: __BindgenUnionField<s__bindgen_ty_1_inner>,
57+
pub bindgen_union_field: u32,
58+
}
6459
#[test]
6560
fn bindgen_test_layout_s__bindgen_ty_1() {
6661
assert_eq!(::std::mem::size_of::<s__bindgen_ty_1>() , 4usize , concat ! (
@@ -76,6 +71,11 @@ fn bindgen_test_layout_s__bindgen_ty_1() {
7671
impl Clone for s__bindgen_ty_1 {
7772
fn clone(&self) -> Self { *self }
7873
}
74+
#[repr(C)]
75+
#[derive(Debug, Default, Copy)]
76+
pub struct s {
77+
pub u: s__bindgen_ty_1,
78+
}
7979
#[test]
8080
fn bindgen_test_layout_s() {
8181
assert_eq!(::std::mem::size_of::<s>() , 4usize , concat ! (

tests/expectations/tests/anon_union.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,6 @@ impl <T> ::std::fmt::Debug for __BindgenUnionField<T> {
2828
fmt.write_str("__BindgenUnionField")
2929
}
3030
}
31-
#[repr(C)]
32-
#[derive(Debug, Copy, Clone)]
33-
pub struct TErrorResult {
34-
pub mResult: ::std::os::raw::c_int,
35-
pub __bindgen_anon_1: TErrorResult__bindgen_ty_1,
36-
pub mMightHaveUnreported: bool,
37-
pub mUnionState: TErrorResult_UnionState,
38-
}
3931
pub const TErrorResult_UnionState_HasException: TErrorResult_UnionState =
4032
TErrorResult_UnionState::HasMessage;
4133
#[repr(i32)]
@@ -58,6 +50,14 @@ pub struct TErrorResult__bindgen_ty_1 {
5850
pub mDOMExceptionInfo: __BindgenUnionField<*mut TErrorResult_DOMExceptionInfo>,
5951
pub bindgen_union_field: u64,
6052
}
53+
#[repr(C)]
54+
#[derive(Debug, Copy, Clone)]
55+
pub struct TErrorResult {
56+
pub mResult: ::std::os::raw::c_int,
57+
pub __bindgen_anon_1: TErrorResult__bindgen_ty_1,
58+
pub mMightHaveUnreported: bool,
59+
pub mUnionState: TErrorResult_UnionState,
60+
}
6161
impl Default for TErrorResult {
6262
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
6363
}

tests/expectations/tests/bitfield-enum-basic.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,6 @@ impl ::std::ops::BitAndAssign for _bindgen_ty_1 {
9595
#[repr(C)]
9696
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
9797
pub struct _bindgen_ty_1(pub ::std::os::raw::c_uint);
98-
#[repr(C)]
99-
#[derive(Debug, Default, Copy)]
100-
pub struct Dummy {
101-
pub _address: u8,
102-
}
10398
pub const Dummy_DUMMY_FOO: Dummy__bindgen_ty_1 = Dummy__bindgen_ty_1(1);
10499
pub const Dummy_DUMMY_BAR: Dummy__bindgen_ty_1 = Dummy__bindgen_ty_1(2);
105100
impl ::std::ops::BitOr<Dummy__bindgen_ty_1> for Dummy__bindgen_ty_1 {
@@ -133,6 +128,11 @@ impl ::std::ops::BitAndAssign for Dummy__bindgen_ty_1 {
133128
#[repr(C)]
134129
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
135130
pub struct Dummy__bindgen_ty_1(pub ::std::os::raw::c_uint);
131+
#[repr(C)]
132+
#[derive(Debug, Default, Copy)]
133+
pub struct Dummy {
134+
pub _address: u8,
135+
}
136136
#[test]
137137
fn bindgen_test_layout_Dummy() {
138138
assert_eq!(::std::mem::size_of::<Dummy>() , 1usize , concat ! (

tests/expectations/tests/bitfield-method-same-name.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,18 @@
44
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
55

66

7+
extern "C" {
8+
#[link_name = "_ZN3Foo4typeEv"]
9+
pub fn Foo_type(this: *mut Foo) -> ::std::os::raw::c_char;
10+
}
11+
extern "C" {
12+
#[link_name = "_ZN3Foo9set_type_Ec"]
13+
pub fn Foo_set_type_(this: *mut Foo, c: ::std::os::raw::c_char);
14+
}
15+
extern "C" {
16+
#[link_name = "_ZN3Foo8set_typeEc"]
17+
pub fn Foo_set_type(this: *mut Foo, c: ::std::os::raw::c_char);
18+
}
719
#[repr(C)]
820
#[derive(Debug, Default, Copy)]
921
pub struct Foo {
@@ -17,18 +29,6 @@ fn bindgen_test_layout_Foo() {
1729
assert_eq! (::std::mem::align_of::<Foo>() , 1usize , concat ! (
1830
"Alignment of " , stringify ! ( Foo ) ));
1931
}
20-
extern "C" {
21-
#[link_name = "_ZN3Foo4typeEv"]
22-
pub fn Foo_type(this: *mut Foo) -> ::std::os::raw::c_char;
23-
}
24-
extern "C" {
25-
#[link_name = "_ZN3Foo9set_type_Ec"]
26-
pub fn Foo_set_type_(this: *mut Foo, c: ::std::os::raw::c_char);
27-
}
28-
extern "C" {
29-
#[link_name = "_ZN3Foo8set_typeEc"]
30-
pub fn Foo_set_type(this: *mut Foo, c: ::std::os::raw::c_char);
31-
}
3232
impl Clone for Foo {
3333
fn clone(&self) -> Self { *self }
3434
}

tests/expectations/tests/class.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -242,22 +242,6 @@ fn bindgen_test_layout_WithUnion() {
242242
impl Clone for WithUnion {
243243
fn clone(&self) -> Self { *self }
244244
}
245-
#[repr(C)]
246-
#[derive(Debug, Default, Copy)]
247-
pub struct RealAbstractionWithTonsOfMethods {
248-
pub _address: u8,
249-
}
250-
#[test]
251-
fn bindgen_test_layout_RealAbstractionWithTonsOfMethods() {
252-
assert_eq!(::std::mem::size_of::<RealAbstractionWithTonsOfMethods>() ,
253-
1usize , concat ! (
254-
"Size of: " , stringify ! ( RealAbstractionWithTonsOfMethods )
255-
));
256-
assert_eq! (::std::mem::align_of::<RealAbstractionWithTonsOfMethods>() ,
257-
1usize , concat ! (
258-
"Alignment of " , stringify ! (
259-
RealAbstractionWithTonsOfMethods ) ));
260-
}
261245
extern "C" {
262246
#[link_name = "_ZNK32RealAbstractionWithTonsOfMethods3barEv"]
263247
pub fn RealAbstractionWithTonsOfMethods_bar(this:
@@ -278,6 +262,22 @@ extern "C" {
278262
#[link_name = "_ZN32RealAbstractionWithTonsOfMethods3staEv"]
279263
pub fn RealAbstractionWithTonsOfMethods_sta();
280264
}
265+
#[repr(C)]
266+
#[derive(Debug, Default, Copy)]
267+
pub struct RealAbstractionWithTonsOfMethods {
268+
pub _address: u8,
269+
}
270+
#[test]
271+
fn bindgen_test_layout_RealAbstractionWithTonsOfMethods() {
272+
assert_eq!(::std::mem::size_of::<RealAbstractionWithTonsOfMethods>() ,
273+
1usize , concat ! (
274+
"Size of: " , stringify ! ( RealAbstractionWithTonsOfMethods )
275+
));
276+
assert_eq! (::std::mem::align_of::<RealAbstractionWithTonsOfMethods>() ,
277+
1usize , concat ! (
278+
"Alignment of " , stringify ! (
279+
RealAbstractionWithTonsOfMethods ) ));
280+
}
281281
impl Clone for RealAbstractionWithTonsOfMethods {
282282
fn clone(&self) -> Self { *self }
283283
}

0 commit comments

Comments
 (0)