std.mem.zeroes: Zero out entire extern union, including padding#17286
std.mem.zeroes: Zero out entire extern union, including padding#17286andrewrk merged 1 commit intoziglang:masterfrom
extern union, including padding#17286Conversation
977fc53 to
c3bc290
Compare
065afea to
d611c75
Compare
|
@kcbanner, I'm running into a problem with Line 767 in ab3ac1e Lines 4105 to 4108 in ab3ac1e I think there are two ways this could be fixed:
Which way do you think is better? |
|
It looks like |
|
I think option 1 (make the field an optional pointer) makes sense, that was an oversight on my part when I added it. |
48c5358 to
914063d
Compare
914063d to
58949b9
Compare
This is the change that ended up working. Initializing |
andrewrk
left a comment
There was a problem hiding this comment.
For both extern structs and extern unions, it should be:
var item: T = undefined;
@memset(asBytes(&item), 0);
return item;If I understand correctly, the motivation for making these changes here is that the compiler is handling this perfectly correct code (for structs; extern unions should be changed to this too) in a problematic way.
I suggest, instead of working around this in the standard library, to work around it in the implementation of @memset in the compiler.
I'm working on this problem here: https://github.com/ziglang/zig/compare/master...kcbanner:zig:extern_union_comptime_memory?expand=1 I'm tracking down one last case with writing to fields of packed unions overwriting too many bits, then I'll PR it. |
|
I chose to recursively initialize the fields for two reasons:
If you still think plain |
In Zig, extern structs have well-defined memory layout, which means that they can be used as a bag of bytes that has anything there. It would be illegal behavior if zig code dereferenced one of the non-optional pointers from Therefore I think the meaning of
Zig does not define optional pointers in terms of the C programming language. Zig defines the There is no door open; the Zig language stands on its own and does not depend on the C language specification. |
|
Okay, I'll change it to plain @kcbanner, should I keep the Windows structure change? Update: I decided to drop that other commit since the Windows structure change will no longer be necessary. |
58949b9 to
929ed11
Compare
extern struct and extern unionextern union, including padding
Fixes #17258