Open MCRusher opened 1 year ago
To clear it up, let
is not static so it should statically error about usage in a static context.
Nim compiler doesn't read C code. You provide information about functions, types, variables or constants in C code, and Nim generate C code that use them. So Nim can access macros in C at runtime, but cannot at compile-time.
I found existing wrappers over Vulkan for Nim: https://github.com/nimious/vulkan https://github.com/nimgl/vulkan
Even if you create a wrapper over Vulkan yourself, you might better to write a generater that reads xml/vk.xml in following repositry and generate a Nim code that exports Vulkan APIs. https://github.com/KhronosGroup/Vulkan-Docs#generating-headers-and-related-files This repo says Vulkan header files are generated from that xml file. Vulkan Bindings for Nim also seems to generate binding in that way. https://github.com/nimgl/vulkan/blob/master/tools/generator.nim
Nim compiler doesn't read C code. You provide information about functions, types, variables or constants in C code, and Nim generate C code that use them. So Nim can access macros in C at runtime, but cannot at compile-time.
I'm aware it doesn't read the C code. But if I tell Nim that a compiletime-constant macro value exists I feel like it should be able to use it as a constant in the C code it generates rather than only at runtime. If that's not possible currently/ever, that's fine, I just wanted to know if there was an intended way to do it or not.
But that's not really why I made this bug report, I made it because the compiler crashes, it probably shouldn't do that.
Thanks for mentioning nimgl, I didn't realize it had a wrapper for vulkan as well.
Although I was definitely learning a lot about C interop with nim which is nice to know, like how you can use a nim enum set to represent a C bitmask in a much nicer way, but you have to explicitly set the size pragma to ensure it's correct.
But if I tell Nim that a compiletime-constant macro value exists I feel like it should be able to use it as a constant in the C code it generates rather than only at runtime.
The issue with that is array[size, T]
is static inside Nim, so it needs to know what size
is to be able to dispatch and generate code accordingly. Which requires understanding C code.
Description
I'm currently writing a wrapper over Vulkan since I couldn't find an existing one and I'm trying to directly bind macro constants to nim constants to prevent the values from potentially shifting out from under me in the future, but that causes a different compiler error where the constant still expects an initializer value and just ignores the importc pragma.
So instead I bound it to a let variable with the compTime pragma and this compiles, I can even echo it and it works fine, but as soon as I try to use it in a comptime context (as the size of an array, in a when statement, etc.) the compiler segfaults and crashes.
Minimum reproducible code:
ctest.h
ctest.nim
Nim Version
Nim Compiler Version 1.9.5 [Linux: amd64] Compiled at 2023-07-08 Copyright (c) 2006-2023 by Andreas Rumpf
git hash: 6ec10a4c9182c513fecd17fde4a81a3a007bb2e9 active boot switches: -d:release
Current Output
Expected Output
Possible Solution
No response
Additional Information
I'm not sure if what I'm trying to do is even supposed to be allowed by the language, but I figured that at least the segfault was noteworthy.