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

std::vector<int>::iterator creates trouble

Open Volker-Weissmann opened this issue 5 years ago • 7 comments

Input C/C++ Header

#include <vector>
namespace MySpace {
	std::vector<int>::iterator x;
}

Bindgen Invocation

bindgen::Builder::default()
        .header("wrapper.hpp")
        .parse_callbacks(Box::new(bindgen::CargoCallbacks))
        .clang_arg("-x")
        .clang_arg("c++")
        .opaque_type("std::.*")
        .whitelist_var("MySpace::.*")
        .whitelist_type("MySpace::.*")
        .whitelist_function("MySpace::.*")
        .generate()
        .expect("Unable to generate bindings");

Actual Results

... 89 lines of code ...
pub type pointer = pointer;
pub type pointer = pointer;
pub type pointer = pointer;
pub type pointer = *mut ::std::os::raw::c_int;

If you try to compile this, you get:

error[E0428]: the name `pointer` is defined multiple times
  --> /home/volker/Sync/git/latexrust/latex_to_symbolic/target/debug/build/latex_to_symbolic-2a7cee1167c646ae/out/bindings.rs:91:1
   |
90 | pub type pointer = pointer;
   | --------------------------- previous definition of the type `pointer` here
91 | pub type pointer = pointer;
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ `pointer` redefined here
   |
   = note: `pointer` must be defined only once in the type namespace of this module

error[E0428]: the name `pointer` is defined multiple times
  --> /home/volker/Sync/git/latexrust/latex_to_symbolic/target/debug/build/latex_to_symbolic-2a7cee1167c646ae/out/bindings.rs:92:1
   |
90 | pub type pointer = pointer;
   | --------------------------- previous definition of the type `pointer` here
91 | pub type pointer = pointer;
92 | pub type pointer = pointer;
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ `pointer` redefined here
   |
   = note: `pointer` must be defined only once in the type namespace of this module

error[E0428]: the name `pointer` is defined multiple times
  --> /home/volker/Sync/git/latexrust/latex_to_symbolic/target/debug/build/latex_to_symbolic-2a7cee1167c646ae/out/bindings.rs:93:1
   |
90 | pub type pointer = pointer;
   | --------------------------- previous definition of the type `pointer` here
...
93 | pub type pointer = *mut ::std::os::raw::c_int;
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `pointer` redefined here
   |
   = note: `pointer` must be defined only once in the type namespace of this module

error[E0391]: cycle detected when processing `pointer`
  --> /home/volker/Sync/git/latexrust/latex_to_symbolic/target/debug/build/latex_to_symbolic-2a7cee1167c646ae/out/bindings.rs:90:20
   |
90 | pub type pointer = pointer;
   |                    ^^^^^^^
   |
   = note: ...which again requires processing `pointer`, completing the cycle
note: cycle used when processing `MySpace_x`
  --> /home/volker/Sync/git/latexrust/latex_to_symbolic/target/debug/build/latex_to_symbolic-2a7cee1167c646ae/out/bindings.rs:88:59
   |
88 |     pub static mut MySpace_x: __gnu_cxx___normal_iterator<pointer>;
   |                                                           ^^^^^^^

Expected Results

No compile errors?

Volker-Weissmann avatar Jul 26 '20 00:07 Volker-Weissmann

This might be, sadly, just a case of the STL not being supported very well by bindgen. See https://github.com/rust-lang/rust-bindgen/issues/1808#issuecomment-647197424 and https://github.com/rust-lang/rust-bindgen/issues/1597#issuecomment-526871534.

kulp avatar Jul 27 '20 00:07 kulp

Yeah, these are not necessarily unfixable though :)

I think we should not generate a bunch of these constants #1838. Same goes for the typedefs.

I haven't been catching up lately with all the rust improvements, but chances are that a few of those can be "properly" supported with stuff inside generic impl blocks like:

impl<T> Generic<T> {
   pub type Bar = Foo<T>;
}

Or such.

emilio avatar Jul 27 '20 11:07 emilio

I appreciate a lot all the reduced test-cases @Volker-Weissmann, thank you!

emilio avatar Jul 27 '20 11:07 emilio

What I did for my code is write a script that removes all pub type pointer = pointer;. It is a super hacky solution, but it worked so far.

Volker-Weissmann avatar Jul 27 '20 18:07 Volker-Weissmann

@Volker-Weissmann Does adding pointer to block list solve this issue?

wangbj avatar Mar 03 '22 03:03 wangbj

Idk, I don't work on this project anymore.

Volker-Weissmann avatar Mar 03 '22 17:03 Volker-Weissmann

No worries, I had a similar issue because bindgen generates duplicate types (from typedef), basically two pub type iterator consecutively:

pub type iterator = root::std::_Bit_iterator;

I added iterator to blocklist the issue went away.

wangbj avatar Mar 03 '22 17:03 wangbj