rust-bindgen icon indicating copy to clipboard operation
rust-bindgen copied to clipboard

How can I get more information about "Should have a used entry for the template arg"?

Open youknowone opened this issue 1 year ago • 4 comments

I encountered this panic after upgrading macOS SDK.

It seems to be related to new C++ headers of the SDK. I'd like to check the related types and adding them to blocklist types.

Could you advise how can I check the error causing type names?

Error messages:

thread 'main' panicked at /opt/cargo/registry/src/index.crates.io-6f17d22bba15001f/bindgen-0.70.1/ir/analysis/template_params.rs:254:22:
  Should have a used entry for the template arg
  stack backtrace:
     0: rust_begin_unwind
               at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/std/src/panicking.rs:652:5
     1: core::panicking::panic_fmt
               at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/core/src/panicking.rs:72:14
     2: core::panicking::panic_display
               at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/core/src/panicking.rs:263:5
     3: core::option::expect_failed
               at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/core/src/option.rs:1994:5
     4: core::option::Option<T>::expect
               at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/core/src/option.rs:895:21
     5: bindgen::ir::analysis::template_params::UsedTemplateParameters::constrain_instantiation_of_blocklisted_template::{{closure}}
               at /opt/cargo/registry/src/index.crates.io-6f17d22bba15001f/bindgen-0.70.1/ir/analysis/template_params.rs:252:17
     6: core::iter::adapters::map::map_fold::{{closure}}
               at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/core/src/iter/adapters/map.rs:89:28
     7: core::iter::adapters::filter::filter_fold::{{closure}}
               at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/core/src/iter/adapters/filter.rs:41:44
     8: core::iter::adapters::map::map_fold::{{closure}}
               at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/core/src/iter/adapters/map.rs:89:21
     9: <core::slice::iter::Iter<T> as core::iter::traits::iterator::Iterator>::fold
               at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/core/src/slice/iter/macros.rs:230:27
    10: <core::iter::adapters::map::Map<I,F> as core::iter::traits::iterator::Iterator>::fold
               at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/core/src/iter/adapters/map.rs:129:9
    11: <core::iter::adapters::filter::Filter<I,P> as core::iter::traits::iterator::Iterator>::fold
               at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/core/src/iter/adapters/filter.rs:157:9
    12: <core::iter::adapters::map::Map<I,F> as core::iter::traits::iterator::Iterator>::fold
               at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/core/src/iter/adapters/map.rs:129:9
    13: <core::iter::adapters::fuse::Fuse<I> as core::iter::traits::iterator::Iterator>::fold
               at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/core/src/iter/adapters/fuse.rs:98:19
    14: core::iter::adapters::flatten::FlattenCompat<I,U>::iter_fold
               at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/core/src/iter/adapters/flatten.rs:482:15
    15: <core::iter::adapters::flatten::FlattenCompat<I,U> as core::iter::traits::iterator::Iterator>::fold
               at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/core/src/iter/adapters/flatten.rs:668:9
    16: <core::iter::adapters::flatten::FlatMap<I,U,F> as core::iter::traits::iterator::Iterator>::fold
               at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/core/src/iter/adapters/flatten.rs:89:9
    17: core::iter::traits::iterator::Iterator::for_each
               at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/core/src/iter/traits/iterator.rs:818:9
    18: <alloc::collections::btree::set::BTreeSet<T,A> as core::iter::traits::collect::Extend<T>>::extend
               at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/alloc/src/collections/btree/set.rs:1321:9
    19: bindgen::ir::analysis::template_params::UsedTemplateParameters::constrain_instantiation_of_blocklisted_template
               at /opt/cargo/registry/src/index.crates.io-6f17d22bba15001f/bindgen-0.70.1/ir/analysis/template_params.rs:265:9
    20: <bindgen::ir::analysis::template_params::UsedTemplateParameters as bindgen::ir::analysis::MonotoneFramework>::constrain
               at /opt/cargo/registry/src/index.crates.io-6f17d22bba15001f/bindgen-0.70.1/ir/analysis/template_params.rs:553:21
    21: bindgen::ir::analysis::analyze
               at /opt/cargo/registry/src/index.crates.io-6f17d22bba15001f/bindgen-0.70.1/ir/analysis/mod.rs:166:43
    22: bindgen::ir::context::BindgenContext::find_used_template_parameters
               at /opt/cargo/registry/src/index.crates.io-6f17d22bba15001f/bindgen-0.70.1/ir/context.rs:1371:31
    23: bindgen::ir::context::BindgenContext::gen
               at /opt/cargo/registry/src/index.crates.io-6f17d22bba15001f/bindgen-0.70.1/ir/context.rs:1223:9
    24: bindgen::codegen::codegen
               at /opt/cargo/registry/src/index.crates.io-6f17d22bba15001f/bindgen-0.70.1/codegen/mod.rs:4963:5
    25: bindgen::Bindings::generate
               at /opt/cargo/registry/src/index.crates.io-6f17d22bba15001f/bindgen-0.70.1/lib.rs:907:13
    26: bindgen::Builder::generate
               at /opt/cargo/registry/src/index.crates.io-6f17d22bba15001f/bindgen-0.70.1/lib.rs:349:9
    27: build_script_build::main
               at ./build.rs:828:20
    28: core::ops::function::FnOnce::call_once
               at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/core/src/ops/function.rs:250:5

Related code: https://github.com/rust-lang/rust-bindgen/blob/main/bindgen/ir/analysis/template_params.rs#L240-L263

youknowone avatar Sep 23 '24 06:09 youknowone

Could you provide a reproducer? It's a bug in bindgen.

emilio avatar Sep 25 '24 19:09 emilio

I have problem from large size closed source project. If I can print the error causing type names, it will be helpful to make a smaller reproducer.

youknowone avatar Sep 26 '24 04:09 youknowone

Getting more idea about the problem.

clang version: Apple clang version 16.0.0 (clang-1600.0.26.3)

I added .blocklist_type("std::__1::tuple.*") to block tuple because it was not successfully generated.

When I use std::tuple directly, no panic but only build error (which is expected)

error[E0412]: cannot find type `tuple` in module `root::std::__1`
    --> ../out/binding.rs:3588:54
     |
3588 |             pub field_name: root::std::__1::tuple<root::OrderId>,
     |                                                      ^^^^^ not found in `root::std::__1`

When I give std::tuple inside another generic type, it shows the reported error.

Using blocked type as other template type parameter seems to cause the problem

youknowone avatar Sep 26 '24 06:09 youknowone

And it is not that simple. The below is working

input: t.hpp

template<typename T>
class A {
    T raw;
};

template<typename T>
class B {
    T raw;
};


class X {
    A<int> a;
    B<int> b;
    A<B<int> > c;
};

command:

bindgen --blocklist-type 'B.*' t.hpp

output:

/* automatically generated by rust-bindgen 0.70.1 */

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct A<T> {
    pub _phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,
    pub raw: T,
}
#[repr(C)]
pub struct X {
    pub a: A<::std::os::raw::c_int>,
    pub b: B<::std::os::raw::c_int>,
    pub c: A<B<::std::os::raw::c_int>>,
}
#[allow(clippy::unnecessary_operation, clippy::identity_op)]
const _: () = {
    ["Size of X"][::std::mem::size_of::<X>() - 12usize];
    ["Alignment of X"][::std::mem::align_of::<X>() - 4usize];
    ["Offset of field: X::a"][::std::mem::offset_of!(X, a) - 0usize];
    ["Offset of field: X::b"][::std::mem::offset_of!(X, b) - 4usize];
    ["Offset of field: X::c"][::std::mem::offset_of!(X, c) - 8usize];
};
#[allow(clippy::unnecessary_operation, clippy::identity_op)]
const _: () = {
    ["Size of template specialization: A_open0_int_close0"]
        [::std::mem::size_of::<A<::std::os::raw::c_int>>() - 4usize];
    ["Align of template specialization: A_open0_int_close0"]
        [::std::mem::align_of::<A<::std::os::raw::c_int>>() - 4usize];
};
#[allow(clippy::unnecessary_operation, clippy::identity_op)]
const _: () = {
    ["Size of template specialization: A_open0_B_open1_int_close1_close0"]
        [::std::mem::size_of::<A<B<::std::os::raw::c_int>>>() - 4usize];
    ["Align of template specialization: A_open0_B_open1_int_close1_close0"]
        [::std::mem::align_of::<A<B<::std::os::raw::c_int>>>() - 4usize];
};

youknowone avatar Sep 26 '24 06:09 youknowone