rust-lang / rust-lang/rust-bindgen

Associated types not resolved to concrete type

Open
#1,924 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
5.3k
Forks
829
Avg merge
1d 1h
Merged PRs (30d)
15

Description

Input C/C++ Header
struct InnerType {
    typedef int related_type;
};

template <typename ContainedType> class Container {
public:
    typedef typename ContainedType::related_type contents;
    contents contents_;
};

typedef Container<InnerType> Concrete;

struct LaterContainingType {
    Concrete contents;
};
Bindgen Invocation
    let bindings = bindgen::Builder::default()
        .header("wrapper.hpp")
        .clang_args(&["-x", "c++", "-std=c++14"])
        .layout_tests(false)
        .whitelist_type("LaterContainingType")
        .parse_callbacks(Box::new(bindgen::CargoCallbacks))
        .generate()
        .expect("Unable to generate bindings");
Rust code
#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]

include!(concat!(env!("OUT_DIR"), "/bindings.rs"));

fn main() {
    Concrete { contents_: 32 };
}
Actual Results
$ cargo build
   Compiling bindgen-test-case v0.1.0
error[E0308]: mismatched types
 --> src/main.rs:8:27
  |
8 |     Concrete { contents_: 32 };
  |                           ^^ expected array `[u8; 0]`, found integer

error: aborting due to previous error

For more information about this error, try `rustc --explain E0308`.
error: could not compile `bindgen-test-case`
Expected Results

I'd expect the type of contents_ to be a c_int.

Other notes

Perhaps this counts as the "traits templates" situation listed under the recorded C++ limitations but, as it doesn't involve any nasty SFINAE magic I am hoping this may be possible.

Thanks for bindgen, it's amazing! Apologies if this is a duplicate.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue with the C++ declarations in wrapper.hpp and the bindgen invocation, then run cargo build and inspect the generated bindings included by src/main.rs. Trace how Concrete and its contents_ field are represented; done means the field resolves to c_int and the example builds successfully.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, rust
Domain
compilers, devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.