zig icon indicating copy to clipboard operation
zig copied to clipboard

void union member causes inline for to generate incorrect code

Open leroycep opened this issue 4 years ago • 2 comments

Code sample is based on zig-bare: https://gist.github.com/leroycep/c0e1a85bcc558cd33a87143a26098128

The Reader takes a tagged union and iterates over its fields to check if the next byte is equal to the tag value. If it is the Reader will parse the field_type of that union variant. The bug is that a union member with a field_type of void will make the code for parsing the void member always run after the other variant has been parsed.

Now, if you manually check for a field_type == void and return before calling readAllowVoid (like this commented out line does) it will prevent this from happening.

leroycep avatar Jan 24 '21 02:01 leroycep

I just want to attest that this bug can cause very hard to debug issues. When serialising a union(enum) that had a void member as one of the possible structs, the resulting 'inline for' created some infinite loop.

Since the issue is in the generated code, this becomes almost impossible to figure out since nothing obvious shows up in a debugger.

marijnfs avatar Mar 15 '21 11:03 marijnfs

Is this issue stale?

ultd avatar Apr 23 '24 12:04 ultd

The code sample given works as expected on master.

Vexu avatar Apr 23 '24 16:04 Vexu