-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Wrong ABI size for imported enum #24589
Comments
See https://github.com/nim-lang/Nim/actions/runs/12533490011/job/34953222171#step:11:13480 for an example, where MemoryOrder has the wrong size. |
Well c2nim adds |
well, this is a part of the semantic gap that |
It is not wrong if the C type ends up to be the type Nim chooses (usually byte), in C++ you can set the size and in C11 maybe too. So this would be a breaking change, silently changing the meaning even. |
So, what It's the middle ground here that is wrong, ie where nim asserts that it knows the size but then emits code that does not match what the c compiler does. Here's a trivial example of invalid codegen:
this prints 2 - the real size of |
Yes but we already do agree that eventually MemoryOrder is wrong. We disagree whether the bugfix should be in its declaration or in the compiler. |
Well, the |
ANSI C says an enum is of type |
C99 / C11 standards: 6.7.2.2 Enumeration specifiers
the enum values/constants are of type |
Thanks, most interesting. The C compilers we officially support all map it to |
except gcc, according to the link above, which lets the platform ABI dictate it - apparently used on some arm targets, but not all, and microcontrollers. even worse, users can also change it with a c flag. it's a mess, which is why leaving it undefined so that sizeof doesn't work seems like the easiest option for now. It's not that much of a loss - nim mostly doesn't need it, except when it does (in low-level code like threads etc). giving an error here is better that silently miscomputing. The final option is actually the "autoconf" approach more or less which is to compile a small program before the main compilation and thus find out "target information" from the underlying c compiler. |
Description
Importing
enum
into Nim is broadly a bad idea given the semantic differences, but it's done anyway - when doing so, Nim should respect Cenum
ABI rules - in particular, an enum is sized ascint
by defaultmost of the time but not always.Failing to do so leads silent miscompiles (and fails
-d:checkAbi
)Nim Version
devel
Current Output
No response
Expected Output
No response
Known Workarounds
No response
Additional Information
No response
The text was updated successfully, but these errors were encountered: