Skip to content

bindgen generates duplicate definitions #359

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
jsgf opened this issue Dec 23, 2016 · 8 comments · Fixed by #360
Closed

bindgen generates duplicate definitions #359

jsgf opened this issue Dec 23, 2016 · 8 comments · Fixed by #360
Labels

Comments

@jsgf
Copy link
Contributor

jsgf commented Dec 23, 2016

This command and input generates two distinct definitions for std_char_traits. I tried both --enable-cxx_namespaces and --disable-name-namespacing, but neither made a significant difference.

Command: bindgen stdcxx.i -- -x c++ -std=c++14

Input:

namespace std
{
  template < typename > struct char_traits;
    template < typename _CharT, typename _Traits =
    char_traits < _CharT > >class basic_fbstring;
}
namespace __gnu_cxx
{
  template < typename > struct char_traits;
}
namespace std
{
  template < class _CharT > struct char_traits:__gnu_cxx::char_traits <
    _CharT >
  {
  };
}
@jsgf
Copy link
Contributor Author

jsgf commented Dec 23, 2016

Output:

#[repr(C)]
#[derive(Debug, Copy)]
pub struct std_char_traits {
    pub _address: u8,
}
impl Clone for std_char_traits {
    fn clone(&self) -> Self { *self }
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct std_char_traits<_CharT> {
    pub _address: u8,
    pub _phantom_0: ::std::marker::PhantomData<_CharT>,
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct std_basic_fbstring<_CharT, _Traits> {
    pub _address: u8,
    pub _phantom_0: ::std::marker::PhantomData<_CharT>,
    pub _phantom_1: ::std::marker::PhantomData<_Traits>,
}

@fitzgen fitzgen added the bug label Dec 23, 2016
@fitzgen
Copy link
Member

fitzgen commented Dec 23, 2016

Thanks for the bug report!

@emilio
Copy link
Contributor

emilio commented Dec 23, 2016

Yep, we saw the exact same thing with opencv in a previous report, but I didn't have the time to construct a test case that wasn't the stdlib, so thanks! :).

Short term solution thing is probably to --blacklist-type std::char_traits.

BTW, if you're binding complex C++ it's probably ok to use bindgen's namespace support with --enable-cxx-namespaces. We should make it the default some time soon.

@emilio
Copy link
Contributor

emilio commented Dec 23, 2016

Gah, this test case is tricky. A bit more minimized:

namespace std
{
  template < typename > struct char_traits;
}
namespace __gnu_cxx
{
  template < typename > struct char_traits;
}
namespace std
{
  template < class _CharT > struct char_traits:__gnu_cxx::char_traits <
    _CharT >
  {
  };
}

Basically, what we're doing is: When we see the forward declaration, we go to the definition and parse it, then we assume that the base class specifier needs to be a complete type, and assume we're going to find it as already resolved. But that's not true, sigh.

I think I have a fix, but I'll have to test it a bit more.

@emilio
Copy link
Contributor

emilio commented Dec 23, 2016

But that's not true, sigh.

And since it's not true, we go ahead and parse it as-if it was in our own namespace, which is wrong, and then we generate the duplicate definition.

@emilio
Copy link
Contributor

emilio commented Dec 23, 2016

See #360. Thanks for the test case again, was super helpful to figure out what was going on!

bors-servo pushed a commit that referenced this issue Dec 23, 2016
Don't assume that base classes are already resolved.

Since it may not be the case.

Fixes #359

Also fixes a few other test cases in the codebase that had the wrong namespace relationship.

r? @fitzgen
@jsgf
Copy link
Contributor Author

jsgf commented Dec 23, 2016

No problem! creduce is magic.

@fitzgen
Copy link
Member

fitzgen commented Dec 23, 2016

Indeed it is :)

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

Successfully merging a pull request may close this issue.

3 participants