Skip to content
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

Add support for #[repr(align(x))] on bridge structs #902

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

mxxo
Copy link

@mxxo mxxo commented Jul 21, 2021

Add support for #[repr(align(x))] on bridge structs, closing #835. For the example code:

#[cxx::bridge]
mod ffi {
    #[repr(align(4))]
    struct S {
        b: [u8; 4],
    }
}

the following output was obtained using the cxx code generators:

// -- snip 
#[repr(C)]
#[repr(align(4))]
pub struct S {
    pub b: [u8; 4],
}
// -- snip
#include <array>
#include <cstdint>
#include <type_traits>

struct S;

#ifndef CXXBRIDGE1_STRUCT_S
#define CXXBRIDGE1_STRUCT_S
struct alignas(4) S final {
  ::std::array<::std::uint8_t, 4> b;

  using IsRelocatable = ::std::true_type;
};
#endif // CXXBRIDGE1_STRUCT_S

I believe it would be straightforward to extend this implementation for #[repr(packed)] as well.

I was wondering whether to add support for enums but decided against it for now and made it an "unsupported" error for the time being. enum alignas(X) is accepted by all the C++ compilers I tried, but I also found a defect report from 2017 (2534) that removes the ability to apply alignas to enums: https://wg21.cmeerw.net/cwg/issue2354.

@dyangelo-grullon
Copy link

@dtolnay is there any feedback on this change? What is the path to getting this merged? I’m contemplating closing the last mile or putting up a bounty for this specific feature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants