-
-
Notifications
You must be signed in to change notification settings - Fork 2.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
Array/slice of undefined length compiles incorrectly #21915
Comments
not a bug, this is a slice which setting it to undefined makes it have an undefined pointer and undefined length. writing to undefined memory causes a segfault. |
The error you expected would come from this code: test {
var aa: [_]32 = undefined;
aa[1] = 1;
} |
@nektro Then why didn't I get a runtime check failure instead of a segfault? I'm not compiling in ReleaseFast? This comes up a lot in Vulkan where you pass an array to be filled in:
The bug was that I accidentally deleted MAX_LENGTH while editing. I fully expected Zig to bark at me somehow. Fortunately, I made this typo right after I committed, so a quick diff pulled this up quite easily. Had this been right after a set of changes which were about to get committed, it would have taken me a rather significant chunk of time to track down. |
There's no checking for undefined at runtime, it's not a defined state that a value can have. It's, for lack of a better word, undefined. See #211 for more information.
If you were to delete var aa: []u32 = undefined; the type would drastically change and there's no way |
Vulkan is taking something that generally elides to void* at bottom so it slides through. See: vkEnumeratePhysicalDevices, for example: https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/vkEnumeratePhysicalDevices.html VkPhysicalDevice is VK_DEFINE_HANDLE(VkPhysicalDevice) which winds up eliding to void*. Thanks for the pointer as to why undefined can't be detected at runtime. I'll probably have to start initializing all my arrays to avoid this. It's probably a better idea than using undefined, anyway, if the code isn't performance sensitive. |
Zig Version
0.14.0-dev.2126+e27b4647d
Steps to Reproduce and Observed Behavior
The following code compiles and then segfaults when run
This is quite surprising. I fully expected the code to fail to compile with an error along the lines of "Cannot infer length".
Can confirm that Zig 0.13.0 on godbolt exhibits same behavior.
Expected Behavior
Code should fail to compile with an error.
The text was updated successfully, but these errors were encountered: