Skip to content

Commit b58f80f

Browse files
committed
Merge branch 'master' of https://github.com/rust-lang/rust-bindgen into bump-upstream
2 parents 6f55b5f + b71e73b commit b58f80f

12 files changed

+197
-3
lines changed

src/codegen/mod.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3052,16 +3052,26 @@ impl CodeGenerator for Enum {
30523052

30533053
if !variation.is_const() {
30543054
let mut derives = derives_of_item(item, ctx);
3055-
// For backwards compat, enums always derive Clone/Eq/PartialEq/Hash, even
3055+
// For backwards compat, enums always derive Debug/Clone/Eq/PartialEq/Hash, even
30563056
// if we don't generate those by default.
3057+
if !item.annotations().disallow_debug() {
3058+
derives.insert(DerivableTraits::DEBUG);
3059+
}
3060+
if !item.annotations().disallow_copy() {
3061+
derives.insert(DerivableTraits::COPY);
3062+
}
30573063
derives.insert(
30583064
DerivableTraits::CLONE |
3059-
DerivableTraits::COPY |
30603065
DerivableTraits::HASH |
30613066
DerivableTraits::PARTIAL_EQ |
30623067
DerivableTraits::EQ,
30633068
);
3064-
let derives: Vec<_> = derives.into();
3069+
let mut derives: Vec<_> = derives.into();
3070+
for derive in item.annotations().derives().iter() {
3071+
if !derives.contains(&derive.as_str()) {
3072+
derives.push(&derive);
3073+
}
3074+
}
30653075
attrs.push(attributes::derives(&derives));
30663076
}
30673077

src/ir/context.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2723,8 +2723,16 @@ impl ItemResolver {
27232723
assert!(ctx.collected_typerefs());
27242724

27252725
let mut id = self.id;
2726+
let mut seen_ids = HashSet::default();
27262727
loop {
27272728
let item = ctx.resolve_item(id);
2729+
2730+
// Detect cycles and bail out. These can happen in certain cases
2731+
// involving incomplete qualified dependent types (#2085).
2732+
if !seen_ids.insert(id) {
2733+
return item;
2734+
}
2735+
27282736
let ty_kind = item.as_type().map(|t| t.kind());
27292737
match ty_kind {
27302738
Some(&TypeKind::ResolvedTypeRef(next_id))

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,3 +149,39 @@ impl ::std::ops::BitAndAssign for NoDebug {
149149
/// <div rustbindgen nodebug></div>
150150
#[derive(Copy, Clone, Hash, PartialEq, Eq)]
151151
pub struct NoDebug(pub ::std::os::raw::c_uint);
152+
impl Debug {
153+
pub const Debug1: Debug = Debug(0);
154+
}
155+
impl Debug {
156+
pub const Debug2: Debug = Debug(1);
157+
}
158+
impl ::std::ops::BitOr<Debug> for Debug {
159+
type Output = Self;
160+
#[inline]
161+
fn bitor(self, other: Self) -> Self {
162+
Debug(self.0 | other.0)
163+
}
164+
}
165+
impl ::std::ops::BitOrAssign for Debug {
166+
#[inline]
167+
fn bitor_assign(&mut self, rhs: Debug) {
168+
self.0 |= rhs.0;
169+
}
170+
}
171+
impl ::std::ops::BitAnd<Debug> for Debug {
172+
type Output = Self;
173+
#[inline]
174+
fn bitand(self, other: Self) -> Self {
175+
Debug(self.0 & other.0)
176+
}
177+
}
178+
impl ::std::ops::BitAndAssign for Debug {
179+
#[inline]
180+
fn bitand_assign(&mut self, rhs: Debug) {
181+
self.0 &= rhs.0;
182+
}
183+
}
184+
#[repr(transparent)]
185+
/// <div rustbindgen derive="Debug"></div>
186+
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
187+
pub struct Debug(pub ::std::os::raw::c_uint);

tests/expectations/tests/enum-default-consts.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,7 @@ pub const NoDebug_NoDebug1: NoDebug = 0;
5757
pub const NoDebug_NoDebug2: NoDebug = 1;
5858
/// <div rustbindgen nodebug></div>
5959
pub type NoDebug = ::std::os::raw::c_uint;
60+
pub const Debug_Debug1: Debug = 0;
61+
pub const Debug_Debug2: Debug = 1;
62+
/// <div rustbindgen derive="Debug"></div>
63+
pub type Debug = ::std::os::raw::c_uint;

tests/expectations/tests/enum-default-module.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,9 @@ pub mod NoDebug {
6363
pub const NoDebug1: Type = 0;
6464
pub const NoDebug2: Type = 1;
6565
}
66+
pub mod Debug {
67+
/// <div rustbindgen derive="Debug"></div>
68+
pub type Type = ::std::os::raw::c_uint;
69+
pub const Debug1: Type = 0;
70+
pub const Debug2: Type = 1;
71+
}

tests/expectations/tests/enum-default-rust.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,10 @@ pub enum NoDebug {
6868
NoDebug1 = 0,
6969
NoDebug2 = 1,
7070
}
71+
#[repr(u32)]
72+
/// <div rustbindgen derive="Debug"></div>
73+
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
74+
pub enum Debug {
75+
Debug1 = 0,
76+
Debug2 = 1,
77+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#![allow(
2+
dead_code,
3+
non_snake_case,
4+
non_camel_case_types,
5+
non_upper_case_globals
6+
)]
7+
8+
#[repr(C)]
9+
#[derive(Copy, Clone)]
10+
pub struct foo {
11+
pub member: foo__bindgen_ty_1,
12+
}
13+
pub const foo_FOO_A: foo__bindgen_ty_1 = foo__bindgen_ty_1::FOO_A;
14+
pub const foo_FOO_B: foo__bindgen_ty_1 = foo__bindgen_ty_1::FOO_B;
15+
#[repr(u32)]
16+
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
17+
pub enum foo__bindgen_ty_1 {
18+
FOO_A = 0,
19+
FOO_B = 1,
20+
}
21+
#[test]
22+
fn bindgen_test_layout_foo() {
23+
assert_eq!(
24+
::std::mem::size_of::<foo>(),
25+
4usize,
26+
concat!("Size of: ", stringify!(foo))
27+
);
28+
assert_eq!(
29+
::std::mem::align_of::<foo>(),
30+
4usize,
31+
concat!("Alignment of ", stringify!(foo))
32+
);
33+
assert_eq!(
34+
unsafe { &(*(::std::ptr::null::<foo>())).member as *const _ as usize },
35+
0usize,
36+
concat!(
37+
"Offset of field: ",
38+
stringify!(foo),
39+
"::",
40+
stringify!(member)
41+
)
42+
);
43+
}
44+
impl Default for foo {
45+
fn default() -> Self {
46+
let mut s = ::std::mem::MaybeUninit::<Self>::uninit();
47+
unsafe {
48+
::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1);
49+
s.assume_init()
50+
}
51+
}
52+
}
53+
#[repr(u32)]
54+
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
55+
pub enum Foo {
56+
Bar = 0,
57+
Qux = 1,
58+
}
59+
pub mod Neg {
60+
pub type Type = ::std::os::raw::c_int;
61+
pub const MinusOne: Type = -1;
62+
pub const One: Type = 1;
63+
}
64+
#[repr(u32)]
65+
/// <div rustbindgen nodebug></div>
66+
#[derive(Copy, Clone, Hash, PartialEq, Eq)]
67+
pub enum NoDebug {
68+
NoDebug1 = 0,
69+
NoDebug2 = 1,
70+
}
71+
#[repr(u32)]
72+
/// <div rustbindgen derive="Debug"></div>
73+
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
74+
pub enum Debug {
75+
Debug1 = 0,
76+
Debug2 = 1,
77+
}

tests/expectations/tests/enum.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,7 @@ pub const NoDebug_NoDebug1: NoDebug = 0;
5555
pub const NoDebug_NoDebug2: NoDebug = 1;
5656
/// <div rustbindgen nodebug></div>
5757
pub type NoDebug = ::std::os::raw::c_uint;
58+
pub const Debug_Debug1: Debug = 0;
59+
pub const Debug_Debug2: Debug = 1;
60+
/// <div rustbindgen derive="Debug"></div>
61+
pub type Debug = ::std::os::raw::c_uint;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#![allow(
2+
dead_code,
3+
non_snake_case,
4+
non_camel_case_types,
5+
non_upper_case_globals
6+
)]
7+
8+
#[repr(C)]
9+
#[derive(Debug, Default, Copy, Clone)]
10+
pub struct Foo {
11+
pub _address: u8,
12+
}
13+
#[repr(C)]
14+
#[derive(Debug, Default, Copy, Clone)]
15+
pub struct Bar {
16+
pub _address: u8,
17+
}

tests/headers/enum-no-debug-rust.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// bindgen-flags: --no-derive-debug --default-enum-style=rust --constified-enum-module=Neg
2+
3+
#include "enum.h"

tests/headers/enum.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,9 @@ enum NoDebug {
2323
NoDebug1,
2424
NoDebug2,
2525
};
26+
27+
/** <div rustbindgen derive="Debug"></div> */
28+
enum Debug {
29+
Debug1,
30+
Debug2,
31+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Issue #2085.
2+
3+
template<typename T>
4+
struct Foo;
5+
6+
template<typename T, typename U>
7+
struct Bar {};
8+
9+
template<typename T>
10+
struct Bar<T, void> {
11+
using BarDependent = typename Foo<T>::Dependent;
12+
void method(const BarDependent &);
13+
};
14+
15+
template<typename T>
16+
void Bar<T, void>::method(const BarDependent &) {}

0 commit comments

Comments
 (0)