Skip to content

Invalid Rust generated when struct-tag namespace and type namespace collide #3162

Open
@cfallin

Description

I have encountered a C header file like the following in the wild:

struct A { int x; };
typedef struct A B;

struct B;

void foo(B *b);

When I run rust-bindgen on this and try to compile the resulting Rust, I get a type error due to a redefinition of B:

$ bindgen test.h > test.rs
$ cat test.rs
[ ... ]
pub type B = A;
[ ... ]
struct B { [ ... ] }
$ rustc test.rs
error[E0428]: the name `B` is defined multiple times
  --> test.rs:17:1
   |
14 | pub type B = A;
   | --------------- previous definition of the type `B` here
...
17 | pub struct B {
   | ^^^^^^^^^^^^ `B` redefined here
   |
   = note: `B` must be defined only once in the type namespace of this module

The root problem appears to be that the struct-tag namespace and the type namespace, which are distinct in C, are both projected into the type namespace in Rust, so a name conflict occurs. The C code above is weird and probably doesn't do what the author intends, but it is valid C. For now I've made an edit to the header file, but it would be nice if rust-bindgen would either produce an error in this case, choose one of the definitions, or rename one (B and __struct_B for example?) to avoid the conflict.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions