Open
Description
Zig Version
0.13.0-dev.75+5c9eb4081
Steps to Reproduce and Observed Behavior
const std = @import("std");
pub const Biome = struct {
val: u32 = undefined,
};
pub fn main() !void {
var biome1: Biome = .{};
std.log.err("{any}", .{std.mem.asBytes(&biome1)});
var biome2: Biome = .{.val = undefined};
std.log.err("{any}", .{std.mem.asBytes(&biome2)});
var biome3: Biome = undefined;
std.log.err("{any}", .{std.mem.asBytes(&biome3)});
}
Note how only the last variant actually contains the desired 0xAA (170) bytes:
$ zig run test.zig
error: { 0, 0, 0, 0 }
error: { 0, 0, 0, 0 }
error: { 170, 170, 170, 170 }
Expected Behavior
Setting things to undefined should set them to 0xAA in Debug mode, like it does with the x86 backend.
This makes some bugs easier to identify.
Consider for example an ArrayListUnmanaged
that gets initialized to undefined
, like in #20085.
Since it gets currently set to 0 in debug mode, the deinit()
does not produce any errors, making it only possible to identify the issue in release.
With 0xAA it would produce a runtime crash when attempting to free it and according to zig zen:
- Runtime crashes are better than bugs.