Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x is not generic panic #789

Closed
ankane opened this issue Oct 12, 2022 · 2 comments
Closed

x is not generic panic #789

ankane opened this issue Oct 12, 2022 · 2 comments

Comments

@ankane
Copy link
Contributor

ankane commented Oct 12, 2022

Hi, thanks again for this project!

I wanted to report an x is not generic panic. This may be related to #770.

thread 'main' panicked at 'Dataset is not generic', src/bindgen/ir/generic_path.rs:83:9
stack backtrace:
   0: _rust_begin_unwind
   1: core::panicking::panic_fmt
   2: cbindgen::bindgen::ir::generic_path::GenericParams::call
   3: <cbindgen::bindgen::ir::typedef::Typedef as cbindgen::bindgen::ir::item::Item>::instantiate_monomorph
   4: cbindgen::bindgen::ir::ty::Type::add_monomorphs
   5: cbindgen::bindgen::library::Library::generate
   6: cbindgen::bindgen::builder::Builder::generate
   7: cbindgen::main

To reproduce, create two libs:

cargo new lib1 --lib
cargo new lib2 --lib

In lib1/src/lib.rs, add:

pub struct Dataset<T> {
    data: Vec<T>,
}

impl<T> Dataset<T> {
    pub fn new() -> Self {
        Self {
            data: Vec::new()
        }
    }
}

In lib2/Cargo.toml, add under [dependencies]:

lib1 = { path = "../lib1" }

In lib2/src/lib.rs, add:

pub type Dataset = lib1::Dataset<u32>;

#[no_mangle]
pub unsafe extern "C" fn hello() -> *mut Dataset {
    Box::into_raw(Box::new(Dataset::new()))
}

And in lib2, run:

cbindgen --lang c

A few notes:

  • There's no panic with C++
  • There's no panic when the code is in the same package
  • It's happening with the latest version (0.24.3)
@ankane
Copy link
Contributor Author

ankane commented Oct 12, 2022

My bad, I think this may be a duplicate of #286.

@ankane ankane closed this as completed Oct 12, 2022
@ankane
Copy link
Contributor Author

ankane commented Oct 12, 2022

For future readers, one way around this is to use an additional struct instead of a type alias.

pub struct Dataset(lib1::Dataset<u32>);

#[no_mangle]
pub unsafe extern "C" fn hello() -> *mut Dataset {
    Box::into_raw(Box::new(Dataset(lib1::Dataset::new())))
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant