Skip to content

codegen: Don't assume unsized structs have address. #552

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1286,17 +1286,25 @@ impl CodeGenerator for CompInfo {
}
}

// C requires every struct to be addressable, so what C compilers do is
// making the struct 1-byte sized.
// C++ requires every struct to be addressable, so what C++ compilers do
// is making the struct 1-byte sized.
//
// This is apparently not the case for C, see:
// https://github.com/servo/rust-bindgen/issues/551
//
// Just get the layout, and assume C++ if not.
//
// NOTE: This check is conveniently here to avoid the dummy fields we
// may add for unused template parameters.
if self.is_unsized(ctx) {
let ty = BlobTyBuilder::new(Layout::new(1, 1)).build();
let field = StructFieldBuilder::named("_address")
.pub_()
.build_ty(ty);
fields.push(field);
let has_address = layout.map_or(true, |l| l.size != 0);
if has_address {
let ty = BlobTyBuilder::new(Layout::new(1, 1)).build();
let field = StructFieldBuilder::named("_address")
.pub_()
.build_ty(ty);
fields.push(field);
}
}

// Append any extra template arguments that nobody has used so far.
Expand Down
20 changes: 20 additions & 0 deletions tests/expectations/tests/c-empty-layout.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* automatically generated by rust-bindgen */


#![allow(non_snake_case)]


#[repr(C)]
#[derive(Debug, Default, Copy)]
pub struct Foo {
}
#[test]
fn bindgen_test_layout_Foo() {
assert_eq!(::std::mem::size_of::<Foo>() , 0usize , concat ! (
"Size of: " , stringify ! ( Foo ) ));
assert_eq! (::std::mem::align_of::<Foo>() , 1usize , concat ! (
"Alignment of " , stringify ! ( Foo ) ));
}
impl Clone for Foo {
fn clone(&self) -> Self { *self }
}
21 changes: 21 additions & 0 deletions tests/expectations/tests/cpp-empty-layout.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/* automatically generated by rust-bindgen */


#![allow(non_snake_case)]


#[repr(C)]
#[derive(Debug, Default, Copy)]
pub struct Foo {
pub _address: u8,
}
#[test]
fn bindgen_test_layout_Foo() {
assert_eq!(::std::mem::size_of::<Foo>() , 1usize , concat ! (
"Size of: " , stringify ! ( Foo ) ));
assert_eq! (::std::mem::align_of::<Foo>() , 1usize , concat ! (
"Alignment of " , stringify ! ( Foo ) ));
}
impl Clone for Foo {
fn clone(&self) -> Self { *self }
}
1 change: 1 addition & 0 deletions tests/headers/c-empty-layout.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
struct Foo {};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know we have tests for this intermingled here and there elsewhere, but it would be nice to have a C++ companion for this test that has the same contents but with a .hpp extension.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done :)

1 change: 1 addition & 0 deletions tests/headers/cpp-empty-layout.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
struct Foo {};