From 813feede5b8667dbb1f70bf9c7a9dc736f0c3760 Mon Sep 17 00:00:00 2001 From: Patrick Marks Date: Sun, 14 Jul 2019 12:31:20 -0700 Subject: [PATCH 1/5] don't emit #[repr(align(0))], and reduced test case --- src/codegen/mod.rs | 15 +++++++++------ tests/expectations/tests/repr-align.rs | 5 +++++ tests/headers/repr-align.hpp | 6 +++++- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/codegen/mod.rs b/src/codegen/mod.rs index 15ace9e91c..bf49a82d29 100644 --- a/src/codegen/mod.rs +++ b/src/codegen/mod.rs @@ -1690,12 +1690,15 @@ impl CodeGenerator for CompInfo { if ctx.options().rust_features().repr_align { if let Some(explicit) = explicit_align { - // Ensure that the struct has the correct alignment even in - // presence of alignas. - let explicit = helpers::ast_ty::int_expr(explicit as i64); - attributes.push(quote! { - #[repr(align(#explicit))] - }); + // don't emit #[repr(align(0))] + if explicit > 0 { + // Ensure that the struct has the correct alignment even in + // presence of alignas. + let explicit = helpers::ast_ty::int_expr(explicit as i64); + attributes.push(quote! { + #[repr(align(#explicit))] + }); + } } } diff --git a/tests/expectations/tests/repr-align.rs b/tests/expectations/tests/repr-align.rs index 8a0c9023ef..34a436c33b 100644 --- a/tests/expectations/tests/repr-align.rs +++ b/tests/expectations/tests/repr-align.rs @@ -68,3 +68,8 @@ fn bindgen_test_layout_b() { concat!("Offset of field: ", stringify!(b), "::", stringify!(c)) ); } +#[repr(C)] +pub struct std_a { + pub _bindgen_opaque_blob: [u8; 0usize], + _unused: [u8; 0], +} diff --git a/tests/headers/repr-align.hpp b/tests/headers/repr-align.hpp index 3347594b5c..74fbc137d5 100644 --- a/tests/headers/repr-align.hpp +++ b/tests/headers/repr-align.hpp @@ -1,4 +1,4 @@ -// bindgen-flags: --raw-line '#![cfg(feature = "nightly")]' --rust-target 1.25 -- -std=c++11 +// bindgen-flags: --raw-line '#![cfg(feature = "nightly")]' --rust-target 1.25 --opaque-type "std::.*" -- -std=c++11 struct alignas(8) a { int b; @@ -9,3 +9,7 @@ struct alignas(double) b { int b; int c; }; + +namespace std { +union a; +} \ No newline at end of file From bd2bfd6b170be7b060b3e1199bfa23e28ff51a0f Mon Sep 17 00:00:00 2001 From: Patrick Marks Date: Sun, 14 Jul 2019 17:45:09 -0700 Subject: [PATCH 2/5] correct fix & cleaner test --- src/ir/comp.rs | 6 ++++++ .../tests/forward_declared_opaque.rs | 19 +++++++++++++++++++ tests/expectations/tests/repr-align.rs | 5 ----- tests/headers/forward_declared_opaque.h | 4 ++++ tests/headers/repr-align.hpp | 8 ++------ 5 files changed, 31 insertions(+), 11 deletions(-) create mode 100644 tests/expectations/tests/forward_declared_opaque.rs create mode 100644 tests/headers/forward_declared_opaque.h diff --git a/src/ir/comp.rs b/src/ir/comp.rs index 0637d2bc81..6c0563b18a 100644 --- a/src/ir/comp.rs +++ b/src/ir/comp.rs @@ -1072,6 +1072,12 @@ impl CompInfo { return None; } + // By definition, we don't have the right layout information here if + // we're a forward declaration. + if self.is_forward_declaration() { + return None; + } + let mut max_size = 0; let mut max_align = 0; for field in self.fields() { diff --git a/tests/expectations/tests/forward_declared_opaque.rs b/tests/expectations/tests/forward_declared_opaque.rs new file mode 100644 index 0000000000..44aff91f49 --- /dev/null +++ b/tests/expectations/tests/forward_declared_opaque.rs @@ -0,0 +1,19 @@ +/* automatically generated by rust-bindgen */ + +#![allow( + dead_code, + non_snake_case, + non_camel_case_types, + non_upper_case_globals +)] + +#[repr(C)] +#[derive(Copy, Clone)] +pub struct a { + _unused: [u8; 0], +} +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct b { + _unused: [u8; 0], +} diff --git a/tests/expectations/tests/repr-align.rs b/tests/expectations/tests/repr-align.rs index 34a436c33b..8a0c9023ef 100644 --- a/tests/expectations/tests/repr-align.rs +++ b/tests/expectations/tests/repr-align.rs @@ -68,8 +68,3 @@ fn bindgen_test_layout_b() { concat!("Offset of field: ", stringify!(b), "::", stringify!(c)) ); } -#[repr(C)] -pub struct std_a { - pub _bindgen_opaque_blob: [u8; 0usize], - _unused: [u8; 0], -} diff --git a/tests/headers/forward_declared_opaque.h b/tests/headers/forward_declared_opaque.h new file mode 100644 index 0000000000..1b58edb9a8 --- /dev/null +++ b/tests/headers/forward_declared_opaque.h @@ -0,0 +1,4 @@ +// bindgen-flags: --opaque-type "*" + +union a; +struct b; \ No newline at end of file diff --git a/tests/headers/repr-align.hpp b/tests/headers/repr-align.hpp index 74fbc137d5..ed275f3a2f 100644 --- a/tests/headers/repr-align.hpp +++ b/tests/headers/repr-align.hpp @@ -1,4 +1,4 @@ -// bindgen-flags: --raw-line '#![cfg(feature = "nightly")]' --rust-target 1.25 --opaque-type "std::.*" -- -std=c++11 +// bindgen-flags: --raw-line '#![cfg(feature = "nightly")]' --rust-target 1.25 -- -std=c++11 struct alignas(8) a { int b; @@ -8,8 +8,4 @@ struct alignas(8) a { struct alignas(double) b { int b; int c; -}; - -namespace std { -union a; -} \ No newline at end of file +}; \ No newline at end of file From 457d5be10d443cae9962bda911fb09e7d5f8c329 Mon Sep 17 00:00:00 2001 From: Patrick Marks Date: Sun, 14 Jul 2019 17:49:52 -0700 Subject: [PATCH 3/5] revert original fix --- src/codegen/mod.rs | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/codegen/mod.rs b/src/codegen/mod.rs index bf49a82d29..15ace9e91c 100644 --- a/src/codegen/mod.rs +++ b/src/codegen/mod.rs @@ -1690,15 +1690,12 @@ impl CodeGenerator for CompInfo { if ctx.options().rust_features().repr_align { if let Some(explicit) = explicit_align { - // don't emit #[repr(align(0))] - if explicit > 0 { - // Ensure that the struct has the correct alignment even in - // presence of alignas. - let explicit = helpers::ast_ty::int_expr(explicit as i64); - attributes.push(quote! { - #[repr(align(#explicit))] - }); - } + // Ensure that the struct has the correct alignment even in + // presence of alignas. + let explicit = helpers::ast_ty::int_expr(explicit as i64); + attributes.push(quote! { + #[repr(align(#explicit))] + }); } } From 5b6b27bc08c91ff469f4eab2f68909044f6c48b8 Mon Sep 17 00:00:00 2001 From: Patrick Marks Date: Sun, 14 Jul 2019 17:54:50 -0700 Subject: [PATCH 4/5] newline --- tests/headers/repr-align.hpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/headers/repr-align.hpp b/tests/headers/repr-align.hpp index ed275f3a2f..b0ee8cfaea 100644 --- a/tests/headers/repr-align.hpp +++ b/tests/headers/repr-align.hpp @@ -8,4 +8,5 @@ struct alignas(8) a { struct alignas(double) b { int b; int c; -}; \ No newline at end of file +}; + From 94becdfbaeb2b157401eac8ba9b91f6414a857db Mon Sep 17 00:00:00 2001 From: Patrick Marks Date: Sun, 14 Jul 2019 18:44:16 -0700 Subject: [PATCH 5/5] small fixes --- .../tests/forward_declared_complex_types_1_0.rs | 9 +++++++-- tests/headers/repr-align.hpp | 1 - 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/tests/expectations/tests/forward_declared_complex_types_1_0.rs b/tests/expectations/tests/forward_declared_complex_types_1_0.rs index 0a6a32be81..011ab35e9a 100644 --- a/tests/expectations/tests/forward_declared_complex_types_1_0.rs +++ b/tests/expectations/tests/forward_declared_complex_types_1_0.rs @@ -1,6 +1,11 @@ /* automatically generated by rust-bindgen */ -#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] +#![allow( + dead_code, + non_snake_case, + non_camel_case_types, + non_upper_case_globals +)] #[repr(C)] #[derive(Debug, Default, Copy)] @@ -73,7 +78,7 @@ extern "C" { pub fn baz_struct(f: *mut Foo); } #[repr(C)] -#[derive(Copy)] +#[derive(Debug, Copy)] pub struct Union { _unused: [u8; 0], } diff --git a/tests/headers/repr-align.hpp b/tests/headers/repr-align.hpp index b0ee8cfaea..3347594b5c 100644 --- a/tests/headers/repr-align.hpp +++ b/tests/headers/repr-align.hpp @@ -9,4 +9,3 @@ struct alignas(double) b { int b; int c; }; -