mozilla / cbindgen

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

Panic when parsing a generic type with empty tuple as default type parameter #993

Open akonradi-signal opened 3 months ago

akonradi-signal commented 3 months ago

https://github.com/mozilla/cbindgen/pull/959 added support for default generic type parameters. This broke some existing use cases, in particular where a type is defined with a type parameter with a default, but where all usages specify the type parameter.

MRE:

#[repr(C)]
pub struct NeverUsedWithDefault<T = ()> {
  field: T
}

#[no_mangle]
pub extern "c" fn with_i32(x: NeverUsedWithDefault<i32>) {}

Running cbindgen against that produces the following:

thread 'main' panicked at src/bindgen/parser.rs:928:82:
called `Result::unwrap()` on an `Err` value: "unsupported generic type default: Some(Type::Tuple { paren_token: Paren, elems: [] })"
stack backtrace:
   0: rust_begin_unwind
             at /rustc/684b3553f70148ded97a80371c2405984d4f6aa7/library/std/src/panicking.rs:661:5
   1: core::panicking::panic_fmt
             at /rustc/684b3553f70148ded97a80371c2405984d4f6aa7/library/core/src/panicking.rs:74:14
   2: core::result::unwrap_failed
             at /rustc/684b3553f70148ded97a80371c2405984d4f6aa7/library/core/src/result.rs:1679:5
   3: core::result::Result<T,E>::unwrap
             at /rustc/684b3553f70148ded97a80371c2405984d4f6aa7/library/core/src/result.rs:1102:23
   4: cbindgen::bindgen::parser::Parse::load_syn_struct
             at ./src/bindgen/parser.rs:928:21
   5: cbindgen::bindgen::parser::Parse::load_syn_crate_mod
             at ./src/bindgen/parser.rs:524:21
   6: cbindgen::bindgen::parser::Parser::process_mod
             at ./src/bindgen/parser.rs:306:30
   7: cbindgen::bindgen::parser::Parser::parse_mod
             at ./src/bindgen/parser.rs:277:9
   8: cbindgen::bindgen::parser::parse_src
             at ./src/bindgen/parser.rs:59:5
   9: cbindgen::bindgen::builder::Builder::generate
             at ./src/bindgen/builder.rs:375:33
  10: cbindgen::load_bindings
             at ./src/main.rs:98:16
  11: cbindgen::main
             at ./src/main.rs:327:26
  12: core::ops::function::FnOnce::call_once
             at /rustc/684b3553f70148ded97a80371c2405984d4f6aa7/library/core/src/ops/function.rs:250:5
douglas-raillard-arm commented 1 month ago

Is there a workaround for this ? I'm hitting the same problem, probably on some const_format::concatcp!() macro output.