Skip to content

Panic with double insertion of type when combining CRTP with static assert, nested templates, and inner classes #2078

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

Closed
pcwalton opened this issue Jul 23, 2021 · 0 comments · Fixed by #2084

Comments

@pcwalton
Copy link
Contributor

pcwalton commented Jul 23, 2021

Input C/C++ Header

Tricky one here. This one also comes from Folly and is as minimal as I could get it. Removing pretty much any line makes the problem go away. In particular I'm suspicious of the static_assert

template<class T>
class ClassA {
public:
    class ClassAInner {
    public:
        T *x;
    };
};

template<class D, class I>
class ClassB {
public:
    void foo() {
        ((D *)nullptr)->quux();
    }
};

template<typename T>
class ClassC {
    struct ClassCInnerA;

    struct ClassCInnerB {
        ClassCInnerA *cache;
    };
    static_assert(__is_pod(ClassCInnerB), "");

    struct ClassCInnerA {
        ClassCInnerB *member;
    };

public:
    class ClassCInnerCRTP : public ClassB<ClassCInnerCRTP, typename ClassA<ClassCInnerA>::ClassAInner> {
    public:
        void quux() {
            ((typename ClassA<ClassCInnerA>::ClassAInner *)nullptr)->x->member;
        }
    };
};

class ClassD : public ClassB<ClassD, ClassC<int>::ClassCInnerCRTP> {
public:
    void bar() {
        ((ClassC<int>::ClassCInnerCRTP *)nullptr)->foo();
    }
};

Bindgen Invocation

$ bindgen test.h -- -std=c++11

Actual Results

thread 'main' panicked at 'assertion failed: `(left == right)`
  left: `Some(TypeId(ItemId(51)))`,
 right: `None`', src/ir/context.rs:745:13
stack backtrace:
   0: rust_begin_unwind
             at /rustc/7af1f55ae359e731c2c303f5d98e42a1a8163af0/library/std/src/panicking.rs:493:5
   1: core::panicking::panic_fmt
             at /rustc/7af1f55ae359e731c2c303f5d98e42a1a8163af0/library/core/src/panicking.rs:92:14
   2: core::panicking::assert_failed_inner
             at /rustc/7af1f55ae359e731c2c303f5d98e42a1a8163af0/library/core/src/panicking.rs:160:17
   3: core::panicking::assert_failed
             at /Users/pcwalton/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/library/core/src/panicking.rs:117:5
   4: bindgen::ir::context::BindgenContext::add_item
             at ./src/ir/context.rs:745:13
   5: <bindgen::ir::item::Item as bindgen::parse::ClangItemParser>::from_ty_with_id
             at ./src/ir/item.rs:1653:17
   6: <bindgen::ir::item::Item as bindgen::parse::ClangItemParser>::from_ty_or_ref_with_id
             at ./src/ir/item.rs:1496:20
   7: <bindgen::ir::item::Item as bindgen::parse::ClangItemParser>::from_ty_or_ref
             at ./src/ir/item.rs:1469:9
   8: bindgen::ir::ty::Type::from_clang_ty
             at ./src/ir/ty.rs:1074:25
   9: <bindgen::ir::item::Item as bindgen::parse::ClangItemParser>::from_ty_with_id
             at ./src/ir/item.rs:1646:22
  10: <bindgen::ir::item::Item as bindgen::parse::ClangItemParser>::from_ty_or_ref_with_id
             at ./src/ir/item.rs:1496:20
  11: <bindgen::ir::item::Item as bindgen::parse::ClangItemParser>::from_ty_or_ref
             at ./src/ir/item.rs:1469:9
  12: bindgen::ir::comp::CompInfo::from_ty::{{closure}}
             at ./src/ir/comp.rs:1336:38
  13: bindgen::clang::visit_children
             at ./src/clang.rs:906:5
  14: __ZN5clang8cxcursor13CursorVisitor5VisitE8CXCursorb
  15: __ZN5clang8cxcursor13CursorVisitor23handleDeclForVisitationEPKNS_4DeclE
  16: __ZN5clang8cxcursor13CursorVisitor16VisitDeclContextEPNS_11DeclContextE
  17: __ZN5clang8cxcursor13CursorVisitor18VisitCXXRecordDeclEPNS_13CXXRecordDeclE
  18: __ZN5clang8cxcursor13CursorVisitor13VisitChildrenE8CXCursor
  19: _clang_visitChildren
  20: clang_sys::clang_visitChildren
             at /Users/pcwalton/.cargo/registry/src/jiasu.xzqcsaa.nyc.mn-1ecc6299db9ec823/clang-sys-1.0.3/src/link.rs:165:17
  21: bindgen::clang::Cursor::visit
             at ./src/clang.rs:406:13
  22: bindgen::ir::comp::CompInfo::from_ty
             at ./src/ir/comp.rs:1292:9
  23: bindgen::ir::ty::Type::from_clang_ty
             at ./src/ir/ty.rs:1138:35
  24: bindgen::ir::ty::Type::from_clang_ty
             at ./src/ir/ty.rs:1178:28
  25: <bindgen::ir::item::Item as bindgen::parse::ClangItemParser>::from_ty_with_id
             at ./src/ir/item.rs:1646:22
  26: <bindgen::ir::item::Item as bindgen::parse::ClangItemParser>::from_ty_or_ref_with_id
             at ./src/ir/item.rs:1496:20
  27: <bindgen::ir::item::Item as bindgen::parse::ClangItemParser>::from_ty_or_ref
             at ./src/ir/item.rs:1469:9
  28: bindgen::ir::template::TemplateInstantiation::from_ty::{{closure}}::{{closure}}
             at ./src/ir/template.rs:236:25
  29: core::ops::function::impls::<impl core::ops::function::FnOnce<A> for &mut F>::call_once
             at /Users/pcwalton/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/library/core/src/ops/function.rs:280:13
  30: core::option::Option<T>::map
             at /Users/pcwalton/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/library/core/src/option.rs:487:29
  31: <core::iter::adapters::map::Map<I,F> as core::iter::traits::iterator::Iterator>::next
             at /Users/pcwalton/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/library/core/src/iter/adapters/map.rs:101:9
  32: alloc::vec::Vec<T,A>::extend_desugared
             at /Users/pcwalton/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/library/alloc/src/vec/mod.rs:2501:35
  33: <alloc::vec::Vec<T,A> as alloc::vec::spec_extend::SpecExtend<T,I>>::spec_extend
             at /Users/pcwalton/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/library/alloc/src/vec/spec_extend.rs:18:9
  34: <alloc::vec::Vec<T> as alloc::vec::spec_from_iter_nested::SpecFromIterNested<T,I>>::from_iter
             at /Users/pcwalton/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/library/alloc/src/vec/spec_from_iter_nested.rs:37:9
  35: <alloc::vec::Vec<T> as alloc::vec::spec_from_iter::SpecFromIter<T,I>>::from_iter
             at /Users/pcwalton/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/library/alloc/src/vec/spec_from_iter.rs:36:9
  36: <alloc::vec::Vec<T> as core::iter::traits::collect::FromIterator<T>>::from_iter
             at /Users/pcwalton/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/library/alloc/src/vec/mod.rs:2406:9
  37: core::iter::traits::iterator::Iterator::collect
             at /Users/pcwalton/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/library/core/src/iter/traits/iterator.rs:1798:9
  38: bindgen::ir::template::TemplateInstantiation::from_ty::{{closure}}
             at ./src/ir/template.rs:233:17
  39: core::option::Option<T>::map_or
             at /Users/pcwalton/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/library/core/src/option.rs:514:24
  40: bindgen::ir::template::TemplateInstantiation::from_ty
             at ./src/ir/template.rs:227:29
  41: bindgen::ir::ty::Type::from_clang_ty
             at ./src/ir/ty.rs:794:19
  42: <bindgen::ir::item::Item as bindgen::parse::ClangItemParser>::from_ty_with_id
             at ./src/ir/item.rs:1646:22
  43: <bindgen::ir::item::Item as bindgen::parse::ClangItemParser>::from_ty_or_ref_with_id
             at ./src/ir/item.rs:1496:20
  44: <bindgen::ir::item::Item as bindgen::parse::ClangItemParser>::from_ty_or_ref
             at ./src/ir/item.rs:1469:9
  45: bindgen::ir::comp::CompInfo::from_ty::{{closure}}
             at ./src/ir/comp.rs:1453:25
  46: bindgen::clang::visit_children
             at ./src/clang.rs:906:5
  47: __ZN5clang8cxcursor13CursorVisitor5VisitE8CXCursorb
  48: __ZN5clang8cxcursor13CursorVisitor18VisitCXXRecordDeclEPNS_13CXXRecordDeclE
  49: __ZN5clang8cxcursor13CursorVisitor13VisitChildrenE8CXCursor
  50: _clang_visitChildren
  51: clang_sys::clang_visitChildren
             at /Users/pcwalton/.cargo/registry/src/jiasu.xzqcsaa.nyc.mn-1ecc6299db9ec823/clang-sys-1.0.3/src/link.rs:165:17
  52: bindgen::clang::Cursor::visit
             at ./src/clang.rs:406:13
  53: bindgen::ir::comp::CompInfo::from_ty
             at ./src/ir/comp.rs:1292:9
  54: bindgen::ir::ty::Type::from_clang_ty
             at ./src/ir/ty.rs:1138:35
  55: bindgen::ir::ty::Type::from_clang_ty
             at ./src/ir/ty.rs:1178:28
  56: <bindgen::ir::item::Item as bindgen::parse::ClangItemParser>::from_ty_with_id
             at ./src/ir/item.rs:1646:22
  57: <bindgen::ir::item::Item as bindgen::parse::ClangItemParser>::from_ty_or_ref_with_id
             at ./src/ir/item.rs:1496:20
  58: <bindgen::ir::item::Item as bindgen::parse::ClangItemParser>::from_ty_or_ref
             at ./src/ir/item.rs:1469:9
  59: bindgen::ir::template::TemplateInstantiation::from_ty::{{closure}}::{{closure}}
             at ./src/ir/template.rs:236:25
  60: core::ops::function::impls::<impl core::ops::function::FnOnce<A> for &mut F>::call_once
             at /Users/pcwalton/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/library/core/src/ops/function.rs:280:13
  61: core::option::Option<T>::map
             at /Users/pcwalton/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/library/core/src/option.rs:487:29
  62: <core::iter::adapters::map::Map<I,F> as core::iter::traits::iterator::Iterator>::next
             at /Users/pcwalton/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/library/core/src/iter/adapters/map.rs:101:9
  63: alloc::vec::Vec<T,A>::extend_desugared
             at /Users/pcwalton/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/library/alloc/src/vec/mod.rs:2501:35
  64: <alloc::vec::Vec<T,A> as alloc::vec::spec_extend::SpecExtend<T,I>>::spec_extend
             at /Users/pcwalton/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/library/alloc/src/vec/spec_extend.rs:18:9
  65: <alloc::vec::Vec<T> as alloc::vec::spec_from_iter_nested::SpecFromIterNested<T,I>>::from_iter
             at /Users/pcwalton/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/library/alloc/src/vec/spec_from_iter_nested.rs:37:9
  66: <alloc::vec::Vec<T> as alloc::vec::spec_from_iter::SpecFromIter<T,I>>::from_iter
             at /Users/pcwalton/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/library/alloc/src/vec/spec_from_iter.rs:36:9
  67: <alloc::vec::Vec<T> as core::iter::traits::collect::FromIterator<T>>::from_iter
             at /Users/pcwalton/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/library/alloc/src/vec/mod.rs:2406:9
  68: core::iter::traits::iterator::Iterator::collect
             at /Users/pcwalton/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/library/core/src/iter/traits/iterator.rs:1798:9
  69: bindgen::ir::template::TemplateInstantiation::from_ty::{{closure}}
             at ./src/ir/template.rs:233:17
  70: core::option::Option<T>::map_or
             at /Users/pcwalton/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/library/core/src/option.rs:514:24
  71: bindgen::ir::template::TemplateInstantiation::from_ty
             at ./src/ir/template.rs:227:29
  72: bindgen::ir::ty::Type::from_clang_ty
             at ./src/ir/ty.rs:794:19
  73: <bindgen::ir::item::Item as bindgen::parse::ClangItemParser>::from_ty_with_id
             at ./src/ir/item.rs:1646:22
  74: <bindgen::ir::item::Item as bindgen::parse::ClangItemParser>::from_ty
             at ./src/ir/item.rs:1543:9
  75: bindgen::ir::context::BindgenContext::resolve_typerefs
             at ./src/ir/context.rs:925:36
  76: bindgen::ir::context::BindgenContext::gen
             at ./src/ir/context.rs:1157:9
  77: bindgen::codegen::codegen
             at ./src/codegen/mod.rs:4227:5
  78: bindgen::Bindings::generate
             at ./src/lib.rs:2349:32
  79: bindgen::Builder::generate
             at ./src/lib.rs:1469:9
  80: bindgen::main::{{closure}}
             at ./src/main.rs:54:17
  81: std::panicking::try::do_call
             at /Users/pcwalton/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/library/std/src/panicking.rs:379:40
  82: ___rust_try
  83: std::panicking::try
             at /Users/pcwalton/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/library/std/src/panicking.rs:343:19
  84: std::panic::catch_unwind
             at /Users/pcwalton/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/library/std/src/panic.rs:431:14
  85: bindgen::main
             at ./src/main.rs:53:34
  86: core::ops::function::FnOnce::call_once
             at /Users/pcwalton/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/library/core/src/ops/function.rs:227:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

Expected Results

We don't care about the generated bindings for this class, but we need to not panic.

pcwalton added a commit to pcwalton/rust-bindgen that referenced this issue Jul 29, 2021
In some esoteric cases involving nested templates,
`ty.declaration().definition()` isn't enough to find the definition: we need
`ty.canonical_type().declaration().definition()` instead.

Closes rust-lang#2078.
pcwalton added a commit to pcwalton/rust-bindgen that referenced this issue Jul 31, 2021
In some esoteric cases involving nested templates,
`ty.declaration().definition()` isn't enough to find the definition: we need
`ty.canonical_type().declaration().definition()` instead.

Closes rust-lang#2078.
emilio pushed a commit that referenced this issue Jul 31, 2021
In some esoteric cases involving nested templates,
`ty.declaration().definition()` isn't enough to find the definition: we need
`ty.canonical_type().declaration().definition()` instead.

Closes #2078.
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

Successfully merging a pull request may close this issue.

1 participant