Skip to content

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

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
cfallin opened this issue Feb 26, 2025 · 0 comments
Open

Comments

@cfallin
Copy link

cfallin commented Feb 26, 2025

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.

@cfallin cfallin changed the title Invalid Rust generated with struct-tag namespace and type namespace collide Invalid Rust generated when struct-tag namespace and type namespace collide Feb 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant