Skip to content

Undefined struct fields are initialized to 0 instead of 0xAA in debug mode (with llvm backend) #20095

Open
@IntegratedQuantum

Description

@IntegratedQuantum

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    backend-llvmThe LLVM backend outputs an LLVM IR Module.bugObserved behavior contradicts documented or intended behavior

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions