mozilla / cbindgen

A project for generating C bindings from Rust code
Mozilla Public License 2.0
2.37k stars 306 forks source link

Failure to instantiate monomorphs for generic struct prevents generation of other bindings #914

Closed stevenengler closed 6 months ago

stevenengler commented 9 months ago

If you try to generate bindings for StructC in the following file, cbindgen will panic:

#[repr(C)]
pub struct StructA<T> {
    x: T,
}

#[repr(C)]
pub struct StructB {
    x: StructA<[u8; 2]>,
}

#[repr(C)]
pub struct StructC {
    x: u8,
}
$ cargo run -- --lang c foo.rs
thread 'main' panicked at src/bindgen/mangle.rs:132:17:
not implemented: Unable to mangle generic parameter Array(Primitive(Integer { zeroable: true, signed: false, kind: B8 }), Value("2")) for 'StructA'

Even if you don't want to generate bindings for StructA or StructB so you use exclude = ["StructA", "StructB"], cbindgen still panics since the monomorphs are initialized before the exclusion happens.

https://github.com/mozilla/cbindgen/blob/6bfc2176187a6fc6fba6315323b0296112330294/src/bindgen/library.rs#L69-L72

It would be nice if cbindgen ignored StructA and StructB instead of panicking. As far as I know, the only solution is to split the file into two files, and only run cbindgen on the file that does not contain StructA and StructB.