You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
The text was updated successfully, but these errors were encountered:
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
I have encountered a C header file like the following in the wild:
When I run
rust-bindgen
on this and try to compile the resulting Rust, I get a type error due to a redefinition ofB
: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.The text was updated successfully, but these errors were encountered: