Skip to content

Commit 1548e5a

Browse files
committed
Be pessimistic about derive(Debug) and blacklisted types
1 parent a50055a commit 1548e5a

12 files changed

+28
-44
lines changed

src/ir/analysis/derive_debug.rs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,13 @@ impl<'ctx, 'gen> CannotDeriveDebug<'ctx, 'gen> {
9090

9191
ConstrainResult::Changed
9292
}
93+
94+
/// A type is not `Debug` if we've determined it is not debug, or if it is
95+
/// blacklisted.
96+
fn is_not_debug(&self, id: ItemId) -> bool {
97+
self.cannot_derive_debug.contains(&id) ||
98+
!self.ctx.whitelisted_items().contains(&id)
99+
}
93100
}
94101

95102
impl<'ctx, 'gen> MonotoneFramework for CannotDeriveDebug<'ctx, 'gen> {
@@ -120,6 +127,15 @@ impl<'ctx, 'gen> MonotoneFramework for CannotDeriveDebug<'ctx, 'gen> {
120127
return ConstrainResult::Same;
121128
}
122129

130+
// If an item is reachable from the whitelisted items set, but isn't
131+
// itself whitelisted, then it must be blacklisted. We assume that
132+
// blacklisted items are not `Copy`, since they are presumably
133+
// blacklisted because they are too complicated for us to understand.
134+
if !self.ctx.whitelisted_items().contains(&id) {
135+
trace!(" blacklisted items are assumed not to be Debug");
136+
return ConstrainResult::Same;
137+
}
138+
123139
let item = self.ctx.resolve_item(id);
124140
let ty = match item.as_type() {
125141
Some(ty) => ty,
@@ -176,7 +192,7 @@ impl<'ctx, 'gen> MonotoneFramework for CannotDeriveDebug<'ctx, 'gen> {
176192
}
177193

178194
TypeKind::Array(t, len) => {
179-
if self.cannot_derive_debug.contains(&t) {
195+
if self.is_not_debug(t) {
180196
trace!(
181197
" arrays of T for which we cannot derive Debug \
182198
also cannot derive Debug"
@@ -196,7 +212,7 @@ impl<'ctx, 'gen> MonotoneFramework for CannotDeriveDebug<'ctx, 'gen> {
196212
TypeKind::ResolvedTypeRef(t) |
197213
TypeKind::TemplateAlias(t, _) |
198214
TypeKind::Alias(t) => {
199-
if self.cannot_derive_debug.contains(&t) {
215+
if self.is_not_debug(t) {
200216
trace!(
201217
" aliases and type refs to T which cannot derive \
202218
Debug also cannot derive Debug"
@@ -237,7 +253,7 @@ impl<'ctx, 'gen> MonotoneFramework for CannotDeriveDebug<'ctx, 'gen> {
237253

238254
let bases_cannot_derive =
239255
info.base_members().iter().any(|base| {
240-
self.cannot_derive_debug.contains(&base.ty)
256+
self.is_not_debug(base.ty)
241257
});
242258
if bases_cannot_derive {
243259
trace!(
@@ -250,11 +266,11 @@ impl<'ctx, 'gen> MonotoneFramework for CannotDeriveDebug<'ctx, 'gen> {
250266
let fields_cannot_derive =
251267
info.fields().iter().any(|f| match *f {
252268
Field::DataMember(ref data) => {
253-
self.cannot_derive_debug.contains(&data.ty())
269+
self.is_not_debug(data.ty())
254270
}
255271
Field::Bitfields(ref bfu) => {
256272
bfu.bitfields().iter().any(|b| {
257-
self.cannot_derive_debug.contains(&b.ty())
273+
self.is_not_debug(b.ty())
258274
})
259275
}
260276
});
@@ -287,7 +303,7 @@ impl<'ctx, 'gen> MonotoneFramework for CannotDeriveDebug<'ctx, 'gen> {
287303
TypeKind::TemplateInstantiation(ref template) => {
288304
let args_cannot_derive =
289305
template.template_arguments().iter().any(|arg| {
290-
self.cannot_derive_debug.contains(&arg)
306+
self.is_not_debug(*arg)
291307
});
292308
if args_cannot_derive {
293309
trace!(
@@ -301,8 +317,8 @@ impl<'ctx, 'gen> MonotoneFramework for CannotDeriveDebug<'ctx, 'gen> {
301317
!template.template_definition().is_opaque(self.ctx, &()),
302318
"The early ty.is_opaque check should have handled this case"
303319
);
304-
let def_cannot_derive = self.cannot_derive_debug.contains(
305-
&template.template_definition(),
320+
let def_cannot_derive = self.is_not_debug(
321+
template.template_definition(),
306322
);
307323
if def_cannot_derive {
308324
trace!(

tests/expectations/tests/derive-hash-blacklisting.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
/// This would derive(Hash, Eq, PartialEq) if it didn't contain a blacklisted type,
99
/// causing us to conservatively avoid deriving hash/Eq/PartialEq for it.
1010
#[repr(C)]
11-
#[derive(Debug, Copy)]
1211
pub struct WhitelistedOne {
1312
pub a: Blacklisted<::std::os::raw::c_int>,
1413
}
@@ -24,15 +23,11 @@ fn bindgen_test_layout_WhitelistedOne() {
2423
"Alignment of field: " , stringify ! ( WhitelistedOne ) , "::"
2524
, stringify ! ( a ) ));
2625
}
27-
impl Clone for WhitelistedOne {
28-
fn clone(&self) -> Self { *self }
29-
}
3026
impl Default for WhitelistedOne {
3127
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
3228
}
3329
/// This can't derive(Hash/Eq) even if it didn't contain a blacklisted type.
3430
#[repr(C)]
35-
#[derive(Debug, Copy)]
3631
pub struct WhitelistedTwo {
3732
pub b: Blacklisted<f32>,
3833
}
@@ -48,9 +43,6 @@ fn bindgen_test_layout_WhitelistedTwo() {
4843
"Alignment of field: " , stringify ! ( WhitelistedTwo ) , "::"
4944
, stringify ! ( b ) ));
5045
}
51-
impl Clone for WhitelistedTwo {
52-
fn clone(&self) -> Self { *self }
53-
}
5446
impl Default for WhitelistedTwo {
5547
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
5648
}

tests/expectations/tests/issue-584-stylo-template-analysis-panic.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ impl Clone for A {
2222
fn clone(&self) -> Self { *self }
2323
}
2424
#[repr(C)]
25-
#[derive(Debug, Copy, Clone)]
2625
pub struct e<c> {
2726
pub d: RefPtr<c>,
2827
pub _phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<c>>,
@@ -36,7 +35,6 @@ pub struct f {
3635
pub _address: u8,
3736
}
3837
#[repr(C)]
39-
#[derive(Debug, Copy)]
4038
pub struct g {
4139
pub h: f,
4240
}
@@ -51,14 +49,10 @@ fn bindgen_test_layout_g() {
5149
"Alignment of field: " , stringify ! ( g ) , "::" , stringify
5250
! ( h ) ));
5351
}
54-
impl Clone for g {
55-
fn clone(&self) -> Self { *self }
56-
}
5752
impl Default for g {
5853
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
5954
}
6055
#[repr(C)]
61-
#[derive(Debug, Copy)]
6256
pub struct b {
6357
pub _base: g,
6458
}
@@ -69,9 +63,6 @@ fn bindgen_test_layout_b() {
6963
assert_eq! (::std::mem::align_of::<b>() , 1usize , concat ! (
7064
"Alignment of " , stringify ! ( b ) ));
7165
}
72-
impl Clone for b {
73-
fn clone(&self) -> Self { *self }
74-
}
7566
impl Default for b {
7667
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
7768
}

tests/expectations/tests/issue-645-cannot-find-type-T-in-this-scope.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#[derive(Clone, Copy, Debug)] pub struct RefPtr<T>(T);
77

88
#[repr(C)]
9-
#[derive(Debug, Copy, Clone)]
109
pub struct HasRefPtr<T> {
1110
pub refptr_member: RefPtr<HasRefPtr_TypedefOfT<T>>,
1211
pub _phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,

tests/expectations/tests/issue-662-part-2.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ impl <T> Default for nsMainThreadPtrHolder<T> {
1515
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
1616
}
1717
#[repr(C)]
18-
#[derive(Debug, Copy, Clone)]
1918
pub struct nsMainThreadPtrHandle<U> {
2019
pub mPtr: RefPtr<nsMainThreadPtrHolder<U>>,
2120
pub _phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<U>>,

tests/expectations/tests/issue-807-opaque-types-methods-being-generated.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ impl Clone for SuchWow {
5050
fn clone(&self) -> Self { *self }
5151
}
5252
#[repr(C)]
53-
#[derive(Debug, Default, Copy, Hash, PartialEq, Eq)]
53+
#[derive(Default, Copy, Hash, PartialEq, Eq)]
5454
pub struct Opaque {
5555
pub _bindgen_opaque_blob: u8,
5656
}

tests/expectations/tests/issue-944-derive-copy-and-blacklisting.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33

44
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
55

6-
#[derive(Debug)] pub struct BlacklistMe(u8);
6+
pub struct BlacklistMe(u8);
77

88
/// Because this type contains a blacklisted type, it should not derive Copy.
99
#[repr(C)]
10-
#[derive(Debug)]
1110
pub struct ShouldNotBeCopy {
1211
pub a: BlacklistMe,
1312
}

tests/expectations/tests/no-derive-debug.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
/// and replacement for another type that doesn't implement it would prevent it
1010
/// from building if --no-derive-debug didn't work.
1111
#[repr(C)]
12-
#[derive(Copy)]
1312
pub struct bar {
1413
pub foo: foo,
1514
pub baz: ::std::os::raw::c_int,
@@ -31,9 +30,6 @@ fn bindgen_test_layout_bar() {
3130
"Alignment of field: " , stringify ! ( bar ) , "::" ,
3231
stringify ! ( baz ) ));
3332
}
34-
impl Clone for bar {
35-
fn clone(&self) -> Self { *self }
36-
}
3733
impl Default for bar {
3834
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
3935
}

tests/expectations/tests/no-derive-default.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
/// and replacement for another type that doesn't implement it would prevent it
1010
/// from building if --no-derive-default didn't work.
1111
#[repr(C)]
12-
#[derive(Debug, Copy)]
1312
pub struct bar {
1413
pub foo: foo,
1514
pub baz: ::std::os::raw::c_int,
@@ -31,6 +30,3 @@ fn bindgen_test_layout_bar() {
3130
"Alignment of field: " , stringify ! ( bar ) , "::" ,
3231
stringify ! ( baz ) ));
3332
}
34-
impl Clone for bar {
35-
fn clone(&self) -> Self { *self }
36-
}

tests/expectations/tests/no-recursive-whitelisting.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
pub enum Bar {}
77

88
#[repr(C)]
9-
#[derive(Debug, Copy)]
109
pub struct Foo {
1110
pub baz: *mut Bar,
1211
}
@@ -22,9 +21,6 @@ fn bindgen_test_layout_Foo() {
2221
"Alignment of field: " , stringify ! ( Foo ) , "::" ,
2322
stringify ! ( baz ) ));
2423
}
25-
impl Clone for Foo {
26-
fn clone(&self) -> Self { *self }
27-
}
2824
impl Default for Foo {
2925
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
3026
}

tests/expectations/tests/opaque-tracing.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ extern "C" {
99
pub fn foo(c: *mut Container);
1010
}
1111
#[repr(C)]
12-
#[derive(Debug, Default, Copy, Hash, PartialEq, Eq)]
12+
#[derive(Default, Copy, Hash, PartialEq, Eq)]
1313
pub struct Container {
1414
pub _bindgen_opaque_blob: [u32; 2usize],
1515
}

tests/headers/issue-944-derive-copy-and-blacklisting.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// bindgen-flags: --blacklist-type BlacklistMe --raw-line '#[derive(Debug)] pub struct BlacklistMe(u8);'
1+
// bindgen-flags: --blacklist-type BlacklistMe --raw-line 'pub struct BlacklistMe(u8);'
22

33
struct BlacklistMe {};
44

0 commit comments

Comments
 (0)