cbindgen icon indicating copy to clipboard operation
cbindgen copied to clipboard

cbindgen panics with weird Result error

Open lovesegfault opened this issue 7 years ago • 5 comments

When trying to use cbindgen on my project I get a panic:

error: failed to run custom build command for `camd v0.1.0 (file:///home/bemeurer/standard/sensord/camd)`
process didn't exit successfully: `/home/bemeurer/standard/sensord/target/debug/build/camd-67225faf449c1471/build-script-build` (exit code: 101)
--- stderr
thread 'main' panicked at 'Result has 2 params but is being instantiated with 1 values', /home/bemeurer/.cargo/registry/src/github.com-1ecc6299db9ec823/cbindgen-0.6.3/src/bindgen/ir/opaque.rs:89:9
stack backtrace:
   0: std::sys::unix::backtrace::tracing::imp::unwind_backtrace
   1: std::sys_common::backtrace::print
   2: std::panicking::default_hook::{{closure}}
   3: std::panicking::default_hook
   4: std::panicking::rust_panic_with_hook
   5: std::panicking::continue_panic_fmt
   6: std::panicking::begin_panic_fmt
   7: <cbindgen::bindgen::ir::opaque::OpaqueItem as cbindgen::bindgen::ir::item::Item>::instantiate_monomorph
             at /home/bemeurer/.cargo/registry/src/github.com-1ecc6299db9ec823/cbindgen-0.6.3/src/bindgen/ir/opaque.rs:89
   8: cbindgen::bindgen::ir::ty::Type::add_monomorphs
             at /home/bemeurer/.cargo/registry/src/github.com-1ecc6299db9ec823/cbindgen-0.6.3/src/bindgen/ir/ty.rs:468
   9: <cbindgen::bindgen::ir::item::ItemMap<T>>::for_all_items
             at /home/bemeurer/.cargo/registry/src/github.com-1ecc6299db9ec823/cbindgen-0.6.3/src/bindgen/ir/typedef.rs:86
             at /home/bemeurer/.cargo/registry/src/github.com-1ecc6299db9ec823/cbindgen-0.6.3/src/bindgen/library.rs:358
             at /home/bemeurer/.cargo/registry/src/github.com-1ecc6299db9ec823/cbindgen-0.6.3/src/bindgen/ir/item.rs:185
  10: cbindgen::bindgen::library::Library::generate
             at /home/bemeurer/.cargo/registry/src/github.com-1ecc6299db9ec823/cbindgen-0.6.3/src/bindgen/library.rs:357
             at /home/bemeurer/.cargo/registry/src/github.com-1ecc6299db9ec823/cbindgen-0.6.3/src/bindgen/library.rs:64
  11: cbindgen::bindgen::builder::Builder::generate
             at /home/bemeurer/.cargo/registry/src/github.com-1ecc6299db9ec823/cbindgen-0.6.3/src/bindgen/builder.rs:339
  12: build_script_build::main
             at /home/bemeurer/.cargo/registry/src/github.com-1ecc6299db9ec823/cbindgen-0.6.3/src/lib.rs:33
             at camd/build.rs:8
  13: std::rt::lang_start::{{closure}}
             at /checkout/src/libstd/rt.rs:74
  14: std::panicking::try::do_call
  15: __rust_maybe_catch_panic
  16: std::rt::lang_start_internal
  17: main
  18: __libc_start_main
  19: _start
             at ../sysdeps/x86_64/start.S:120

Here's my cbindgen.toml:

include_guard = "standard_camd_bindings_h"
autogen_warning = "/* Warning, this file is autogenerated by cbindgen. Don't modify this manually. */"
include_version = true
braces = "SameLine"
line_length = 80
tab_width = 4
language = "C"
style = "Tag"
[parse]
parse_deps = false

and my build.rs

extern crate cbindgen;
use std::env;
fn main() {
    let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
    let config = cbindgen::Config::from_file("cbindgen.toml").unwrap();
    cbindgen::generate_with_config(&crate_dir, config)
      .unwrap()
      .write_to_file("target/camd.h");
}

Why is this happening? How can I fix it?

lovesegfault avatar Sep 10 '18 01:09 lovesegfault

Hi, I just hit this with 0.8.7. It's especially confusing because no Result type is part of my FFI interface, though some exist as part of a crate dependency.

rainhead avatar May 20 '19 00:05 rainhead

Is there any chance you can provide a test-case that reproduces the panic?

emilio avatar May 20 '19 21:05 emilio

Pretty much every single time I came across something of this nature it had to do with the cbidgen parser exploding due to some error in the code. Disable cbidgen and let rustc report the error accordingly.

lovesegfault avatar May 21 '19 02:05 lovesegfault

I don't know how to get cbindgen to give me the line number it's choking on, but I recall reading somewhere that it doesn't differentiate between types with the same name. The crate it's choking on defines some Actix messages with associated types like type Result = ();. I wonder if that's the problem.

rainhead avatar May 23 '19 18:05 rainhead

Caught this error when using the rgb crate.

RGBA has 2 params but is being instantiated with 1 values

pub type RGBA8 = RGBA<u8>;

pub struct RGBA<ComponentType, AlphaComponentType = ComponentType> {
    /// Red
    pub r: ComponentType,
    /// Green
    pub g: ComponentType,
    /// Blue
    pub b: ComponentType,
    /// Alpha
    pub a: AlphaComponentType,
}

I cannot use cbindgen:ignore because it's not in my code.

RazrFalcon avatar Nov 20 '20 03:11 RazrFalcon