Zig Version
0.12.0-dev.163+6780a6bbf
Steps to Reproduce and Observed Behavior
- Run
zig test repro.zig
constUnion=externunion {
foo: u32,
bar: f64,
};
conststd=@import("std");
test {
vara=std.mem.zeroes(Union);
trystd.testing.expectEqual(@as(f64, @bitCast(a)), 0.0);
}- Test fails:
Test [1/1] test_0...expected2.121995791e-314, found0.0e+00Test [1/1] test_0...FAIL (TestExpectedEqual)
/Users/jarred/zig/0.12.0-dev.163+6780a6bbf/files/lib/std/testing.zig:84:17: 0x10483c7fbinexpectEqual__anon_1277 (test)
returnerror.TestExpectedEqual;
^/Users/jarred/Desktop/repro.zig:9:5: 0x10483c9a3intest_0 (test)
trystd.testing.expectEqual(@as(f64, @bitCast(a)), 0.0);
^0passed; 0skipped; 1failed.
error: thefollowingtestcommandfailedwithexitcode1:
/Users/jarred/Code/bun/zig-cache/o/abfa20c47b954fb9fb1e2825bfe8f879/tes
Expected Behavior
The name zeroes implies it is zero-ing the bytes, but it is not zero-ing the bytes.
For an extern union, it is assigning a default value for the first field and ignoring the rest of the bytes. That causes the test above to fail.
However, it also leaves alignment bytes undefined, causing this test snippet to fail:
constPadded=struct {
foo: u32,
bar: f64align(128),
};
conststd=@import("std");
test {
vara=std.mem.zeroes(Padded);
varbytes=std.mem.asBytes(&a);
for (bytes) |b| {
std.debug.assert(b==0);
}
}Partially relevant issues:
I suggest either renaming std.mem.zeroes to std.mem.init to more clearly say what it is doing, or to make it actually do @memset(bytes, 0)
It would be very helpful for serialization code if there was a way to always zero-initialize a struct/union by default (including when using Struct{} ) or to specify a container should always be zero-initialized
Zig Version
0.12.0-dev.163+6780a6bbf
Steps to Reproduce and Observed Behavior
zig test repro.zigExpected Behavior
The name
zeroesimplies it is zero-ing the bytes, but it is not zero-ing the bytes.For an
extern union, it is assigning a default value for the first field and ignoring the rest of the bytes. That causes the test above to fail.However, it also leaves alignment bytes undefined, causing this test snippet to fail:
Partially relevant issues:
bun installon 0.8.1 oven-sh/bun#4319I suggest either renaming
std.mem.zeroestostd.mem.initto more clearly say what it is doing, or to make it actually do@memset(bytes, 0)It would be very helpful for serialization code if there was a way to always zero-initialize a struct/union by default (including when using
Struct{}) or to specify a container should always be zero-initialized