cxx icon indicating copy to clipboard operation
cxx copied to clipboard

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

Open mxxo opened this issue 2 years ago • 1 comments

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.

mxxo avatar Jul 21 '21 17:07 mxxo

@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.

dyangelo-grullon avatar Sep 20 '23 12:09 dyangelo-grullon