Skip to content

Commit 641bb12

Browse files
committed
Don't try and debug format blacklisted types when generating impl Debug blocks
1 parent 1548e5a commit 641bb12

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

src/codegen/derive_debug.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pub fn gen_debug_impl(
5353
impl X {
5454
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
5555
write!(f, $format_string $tokens)
56-
}
56+
}
5757
});
5858

5959
match impl_.unwrap().node {
@@ -127,6 +127,12 @@ impl<'a> ImplDebug<'a> for Item {
127127
) -> Option<(String, Vec<TokenTree>)> {
128128
let name_ident = ctx.rust_ident_raw(name);
129129

130+
// We don't know if blacklisted items `impl Debug` or not, so we can't
131+
// add them to the format string we're building up.
132+
if !ctx.whitelisted_items().contains(&self.id()) {
133+
return None;
134+
}
135+
130136
let ty = match self.as_type() {
131137
Some(ty) => ty,
132138
None => {
@@ -168,7 +174,7 @@ impl<'a> ImplDebug<'a> for Item {
168174
} else {
169175
debug_print(ctx, name, name_ident)
170176
}
171-
}
177+
}
172178

173179
// The generic is not required to implement Debug, so we can not debug print that type
174180
TypeKind::TypeParam => {
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/* automatically generated by rust-bindgen */
2+
3+
4+
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
5+
6+
pub struct BlacklistMe(u8);
7+
8+
/// Because this type contains a blacklisted type, it should not derive Debug.
9+
#[repr(C)]
10+
pub struct ShouldManuallyImplDebug {
11+
pub a: BlacklistMe,
12+
}
13+
#[test]
14+
fn bindgen_test_layout_ShouldManuallyImplDebug() {
15+
assert_eq!(::std::mem::size_of::<ShouldManuallyImplDebug>() , 1usize ,
16+
concat ! (
17+
"Size of: " , stringify ! ( ShouldManuallyImplDebug ) ));
18+
assert_eq! (::std::mem::align_of::<ShouldManuallyImplDebug>() , 1usize ,
19+
concat ! (
20+
"Alignment of " , stringify ! ( ShouldManuallyImplDebug ) ));
21+
assert_eq! (unsafe {
22+
& ( * ( 0 as * const ShouldManuallyImplDebug ) ) . a as *
23+
const _ as usize } , 0usize , concat ! (
24+
"Alignment of field: " , stringify ! ( ShouldManuallyImplDebug
25+
) , "::" , stringify ! ( a ) ));
26+
}
27+
impl Default for ShouldManuallyImplDebug {
28+
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
29+
}
30+
impl ::std::fmt::Debug for ShouldManuallyImplDebug {
31+
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
32+
write!(f , "ShouldManuallyImplDebug {{ }}")
33+
}
34+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// bindgen-flags: --impl-debug --blacklist-type BlacklistMe --raw-line 'pub struct BlacklistMe(u8);'
2+
3+
struct BlacklistMe {};
4+
5+
/**
6+
* Because this type contains a blacklisted type, it should not derive Debug.
7+
*/
8+
struct ShouldManuallyImplDebug {
9+
BlacklistMe a;
10+
};

0 commit comments

Comments
 (0)