Forward references to enum
types end up generating the wrong type
#3179
Labels
enum
types end up generating the wrong type
#3179
Forward references to
enum
types end up generating the wrong type for theenum
. For instance:generates:
instead of the expected:
If we move it below:
or if we remove it:
then the generated type is the expected one.
Another reproduction case, closer to what we hit in the kernel, was in a return type:
In turn, this can mean that big codebases that
#include
headers from other headers end up in situations that are quite confusing.For instance, in the Linux kernel, one may end up with a forward reference or not, depending on how the headers are included, which in turn may depend on the kernel configuration or the architecture. In turn, that means that we need to either be careful to avoid those (e.g. adding extra
#include
s to the top of ourbindgen
input to bring the actual definitions first), or developers may end up forcing casts and adding#[allow(clippy::unnecessary_cast)]
(since the type of the generated constants, likeE_A
above, depends on the particular configuration):It is worth noting that GCC and Clang both allow these forward references to
enum
types and do not complain unless-Wpedantic
is used:Other compilers such as MSVC and TCC allow them, too.
Moreover, to double-check that the type of the actual
enum
in GCC and Clang is not changing on their side whether there is a forward reference or not, I added the following to the C examples above and it always passes (assuming the compiler picksunsigned int
), which contradictsbindgen
's output:Finally, I could reproduce the issue with
bindgen
0.71.1 andlibclang
20.1.1, i.e. latest releases, among others.The text was updated successfully, but these errors were encountered: