Skip to content

Invalid code generated when a struct contains fields called type and type_ #507

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

Open
sgrif opened this issue Feb 13, 2017 · 3 comments
Open
Labels

Comments

@sgrif
Copy link

sgrif commented Feb 13, 2017

Header to reproduce:

typedef struct foo {
  int type;
  int type_;
};

The resulting code will be:

#[repr(C)]
#[derive(Debug, Copy)]
pub struct foo {
    pub type_: ::std::os::raw::c_int,
    pub type_: ::std::os::raw::c_int,
}

Which is of course, invalid. It would probably make sense to at least give an error rather than generating invalid code here.

@emilio emilio added the bug label Feb 13, 2017
@emilio
Copy link
Contributor

emilio commented Feb 13, 2017

Argh, nasty. Thanks for reporting. We should be able to just mangle the reserved names further if a type has a duplicated name.

@divagant-martian
Copy link

divagant-martian commented Sep 28, 2022

I tried solving this by mangling too names that look like they would be the result of being mangled. So type_ would be mangled to type__. I don't see how to solve this checking duplicates, since duplicates only matter inside scopes (duplicates in function arguments, for example).

A consequence of doing this is that names that don't need to be mangled because they are not duplicates would be changed anyway. Another consequence is that parts of the bitfield special handling wouldn't be needed now.
The bitfield-method-same-name.hpp

struct Foo {
    char type_ : 3;
    char type();
    void set_type_(char c);
    void set_type(char c);
};

has differences in this fashion

- pub fn type__bindgen_bitfield(&self) -> ::std::os::raw::c_char {
+ pub fn type__(&self) -> ::std::os::raw::c_char {

Would appreciate ideas on how to tackle this

@pvdrz
Copy link
Contributor

pvdrz commented Sep 28, 2022

I think the change in the tests are fine as the _bindgen_bitfield suffix is added when there's a name collision between a method that already exists in the headers and the setter method that's being generated by `bindgen.

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

No branches or pull requests

4 participants