Skip to content

Commit 0019d0e

Browse files
committed
Implement IsOpaque for CompInfo
This allows us to properly detect structs that should be treated as opaque due to their non-type template paramaters, which in turn lets us correctly codegen template aliases to such things. Fixes rust-lang#820
1 parent 10ea03c commit 0019d0e

15 files changed

+62
-11
lines changed

src/ir/comp.rs

+14-3
Original file line numberDiff line numberDiff line change
@@ -1422,6 +1422,14 @@ impl DotAttributes for CompInfo {
14221422
}
14231423
}
14241424

1425+
impl IsOpaque for CompInfo {
1426+
type Extra = ();
1427+
1428+
fn is_opaque(&self, _: &BindgenContext, _: &()) -> bool {
1429+
self.has_non_type_template_params
1430+
}
1431+
}
1432+
14251433
impl TemplateParameters for CompInfo {
14261434
fn self_template_params(&self,
14271435
_ctx: &BindgenContext)
@@ -1498,9 +1506,12 @@ impl CanDeriveDefault for CompInfo {
14981506
return false;
14991507
}
15001508

1501-
return layout.unwrap_or_else(Layout::zero)
1502-
.opaque()
1503-
.can_derive_default(ctx, ());
1509+
return layout.map_or(false, |l| l.opaque().can_derive_default(ctx, ()));
1510+
1511+
}
1512+
1513+
if self.has_non_type_template_params {
1514+
return layout.map_or(false, |l| l.opaque().can_derive_default(ctx, ()));
15041515
}
15051516

15061517
self.detect_derive_default_cycle.set(true);

src/ir/item.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ impl CanDeriveDebug for Item {
283283
ItemKind::Type(ref ty) => {
284284
if self.is_opaque(ctx, &()) {
285285
ty.layout(ctx)
286-
.map_or(true, |l| l.opaque().can_derive_debug(ctx, ()))
286+
.map_or(false, |l| l.opaque().can_derive_debug(ctx, ()))
287287
} else {
288288
ty.can_derive_debug(ctx, ())
289289
}

src/ir/ty.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,7 @@ impl IsOpaque for Type {
358358
match self.kind {
359359
TypeKind::Opaque => true,
360360
TypeKind::TemplateInstantiation(ref inst) => inst.is_opaque(ctx, item),
361+
TypeKind::Comp(ref comp) => comp.is_opaque(ctx, &()),
361362
_ => false,
362363
}
363364
}
@@ -562,6 +563,9 @@ impl CanDeriveDebug for Type {
562563
TypeKind::TemplateInstantiation(ref inst) => {
563564
inst.can_derive_debug(ctx, self.layout(ctx))
564565
}
566+
TypeKind::Opaque => {
567+
self.layout.map_or(false, |l| l.opaque().can_derive_debug(ctx, ()))
568+
}
565569
_ => true,
566570
}
567571
}
@@ -584,7 +588,7 @@ impl CanDeriveDefault for Type {
584588
}
585589
TypeKind::Opaque => {
586590
self.layout
587-
.map_or(true, |l| l.opaque().can_derive_default(ctx, ()))
591+
.map_or(false, |l| l.opaque().can_derive_default(ctx, ()))
588592
}
589593
TypeKind::Void |
590594
TypeKind::Named |

tests/expectations/tests/issue-372.rs

+4
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ pub mod root {
8383
ai = 11,
8484
}
8585
#[repr(C)]
86+
#[derive(Copy)]
8687
pub struct F {
8788
pub w: [u64; 33usize],
8889
}
@@ -98,6 +99,9 @@ pub mod root {
9899
"Alignment of field: " , stringify ! ( F ) , "::" ,
99100
stringify ! ( w ) ));
100101
}
102+
impl Clone for F {
103+
fn clone(&self) -> Self { *self }
104+
}
101105
impl Default for F {
102106
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
103107
}

tests/expectations/tests/issue-569-non-type-template-params-causing-layout-test-failures.rs

+5
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ pub const ENUM_VARIANT_2: _bindgen_ty_1 = _bindgen_ty_1::ENUM_VARIANT_2;
1111
pub enum _bindgen_ty_1 { ENUM_VARIANT_1 = 0, ENUM_VARIANT_2 = 1, }
1212
pub type JS_Alias = u8;
1313
#[repr(C)]
14+
#[derive(Copy, Clone)]
1415
pub struct JS_Base {
1516
pub f: JS_Alias,
1617
}
1718
impl Default for JS_Base {
1819
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
1920
}
2021
#[repr(C)]
22+
#[derive(Copy)]
2123
pub struct JS_AutoIdVector {
2224
pub _base: JS_Base,
2325
}
@@ -28,6 +30,9 @@ fn bindgen_test_layout_JS_AutoIdVector() {
2830
assert_eq! (::std::mem::align_of::<JS_AutoIdVector>() , 1usize , concat !
2931
( "Alignment of " , stringify ! ( JS_AutoIdVector ) ));
3032
}
33+
impl Clone for JS_AutoIdVector {
34+
fn clone(&self) -> Self { *self }
35+
}
3136
impl Default for JS_AutoIdVector {
3237
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
3338
}

tests/expectations/tests/issue-573-layout-test-failures.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@
55

66

77
#[repr(C)]
8-
#[derive(Default)]
8+
#[derive(Copy, Clone)]
99
pub struct Outer {
1010
pub i: u8,
1111
}
12+
impl Default for Outer {
13+
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
14+
}
1215
#[repr(C)]
16+
#[derive(Copy)]
1317
pub struct AutoIdVector {
1418
pub ar: Outer,
1519
}
@@ -25,6 +29,9 @@ fn bindgen_test_layout_AutoIdVector() {
2529
"Alignment of field: " , stringify ! ( AutoIdVector ) , "::" ,
2630
stringify ! ( ar ) ));
2731
}
32+
impl Clone for AutoIdVector {
33+
fn clone(&self) -> Self { *self }
34+
}
2835
impl Default for AutoIdVector {
2936
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
3037
}

tests/expectations/tests/issue-674-1.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub mod root {
1212
#[allow(unused_imports)]
1313
use self::super::super::root;
1414
#[repr(C)]
15-
#[derive(Debug, Copy, Clone)]
15+
#[derive(Copy, Clone)]
1616
pub struct Maybe {
1717
pub _address: u8,
1818
}

tests/expectations/tests/issue-674-2.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub mod root {
1212
#[allow(unused_imports)]
1313
use self::super::super::root;
1414
#[repr(C)]
15-
#[derive(Debug, Copy, Clone)]
15+
#[derive(Copy, Clone)]
1616
pub struct Rooted {
1717
pub _address: u8,
1818
}

tests/expectations/tests/issue-674-3.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pub mod root {
99
#[allow(unused_imports)]
1010
use self::super::root;
1111
#[repr(C)]
12-
#[derive(Debug, Copy, Clone)]
12+
#[derive(Copy, Clone)]
1313
pub struct nsRefPtrHashtable {
1414
pub _address: u8,
1515
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
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+
7+
pub type Foo_self_type = u8;

tests/expectations/tests/non-type-params.rs

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
pub type Array16 = u8;
88
pub type ArrayInt4 = [u32; 4usize];
99
#[repr(C)]
10+
#[derive(Copy)]
1011
pub struct UsesArray {
1112
pub array_char_16: [u8; 16usize],
1213
pub array_bool_8: [u8; 8usize],
@@ -34,6 +35,9 @@ fn bindgen_test_layout_UsesArray() {
3435
"Alignment of field: " , stringify ! ( UsesArray ) , "::" ,
3536
stringify ! ( array_int_4 ) ));
3637
}
38+
impl Clone for UsesArray {
39+
fn clone(&self) -> Self { *self }
40+
}
3741
impl Default for UsesArray {
3842
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
3943
}

tests/expectations/tests/opaque_pointer.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ impl Clone for OtherOpaque {
2222
}
2323
/// <div rustbindgen opaque></div>
2424
#[repr(C)]
25-
#[derive(Debug, Copy, Clone)]
25+
#[derive(Copy, Clone)]
2626
pub struct Opaque {
2727
}
2828
impl Default for Opaque {

tests/expectations/tests/size_t_template.rs

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66

77
#[repr(C)]
8+
#[derive(Copy)]
89
pub struct C {
910
pub arr: [u32; 3usize],
1011
}
@@ -20,6 +21,9 @@ fn bindgen_test_layout_C() {
2021
"Alignment of field: " , stringify ! ( C ) , "::" , stringify
2122
! ( arr ) ));
2223
}
24+
impl Clone for C {
25+
fn clone(&self) -> Self { *self }
26+
}
2327
impl Default for C {
2428
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
2529
}

tests/expectations/tests/template.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ impl Default for PODButContainsDtor {
104104
}
105105
/// <div rustbindgen opaque>
106106
#[repr(C)]
107-
#[derive(Debug, Copy, Clone)]
107+
#[derive(Copy, Clone)]
108108
pub struct Opaque {
109109
}
110110
impl Default for Opaque {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
template<typename E, int N>
2+
class Foo {
3+
typedef Foo<E, N> self_type;
4+
E mBar;
5+
};

0 commit comments

Comments
 (0)