cbindgen icon indicating copy to clipboard operation
cbindgen copied to clipboard

[bug] cbindgen treats structs with array types in which there is `as` opaque

Open NilsIrl opened this issue 1 year ago • 2 comments

With the following input:

pub const SIZE: usize = 4;

#[repr(C)]
pub struct WithoutAs {
    items: [char; SIZE],
}

#[repr(C)]
pub struct WithAs {
    items: [char; SIZE as usize],
}

// dummy function to make `WithoutAs` and `WithAs` part of the public api
#[no_mangle]
pub extern fn some_fn(a: WithoutAs, b: WithAs) {}

cbindgen outputs

#include <cstdarg>
#include <cstdint>
#include <cstdlib>
#include <ostream>
#include <new>

constexpr static const uintptr_t SIZE = 4;

struct WithAs;

struct WithoutAs {
  uint32_t items[SIZE];
};

extern "C" {

void some_fn(WithoutAs a, WithAs b);

} // extern "C"

But I would expect it to give or at least warn the user instead of just making the type opaque without any information as to why or even letter the user know the type is treated as opaque.

#include <cstdarg>
#include <cstdint>
#include <cstdlib>
#include <ostream>
#include <new>

constexpr static const uintptr_t SIZE = 4;

struct WithAs {
  uint32_t items[(size_T) SIZE];
};

struct WithoutAs {
  uint32_t items[SIZE];
};

extern "C" {

void some_fn(WithoutAs a, WithAs b);

} // extern "C"

NilsIrl avatar Nov 17 '24 19:11 NilsIrl

We'd need to test the constant code about basic casts.

emilio avatar Jan 02 '25 18:01 emilio

Correction is in this pull request

PuffyWithEyes avatar Feb 20 '25 01:02 PuffyWithEyes