zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit fecd28371d5c0e25357dfb91f75c8790f8b4155b (tree)
parent fc1c83a363d32e749b9061044db63852ab7255d0
Author: Matthew Lugg <mlugg@mlugg.co.uk>
Date:   Wed,  6 May 2026 11:21:40 +0100

Sema: fix crash bitcasting undefined to bitpack type

Resolves: https://codeberg.org/ziglang/zig/issues/31944

Diffstat:
Msrc/Sema/bitcast.zig | 2++
Mtest/behavior/packed-struct.zig | 6++++++
Mtest/behavior/packed-union.zig | 6++++++
3 files changed, 14 insertions(+), 0 deletions(-)

diff --git a/src/Sema/bitcast.zig b/src/Sema/bitcast.zig @@ -565,6 +565,7 @@ const PackValueBits = struct { }, .@"packed" => { const backing_int_val = try pack.primitive(ty.bitpackBackingInt(zcu)); + if (backing_int_val.isUndef(zcu)) return pt.undefValue(ty); return pt.bitpackValue(ty, backing_int_val); }, }, @@ -659,6 +660,7 @@ const PackValueBits = struct { }, .@"packed" => { const backing_int_val = try pack.primitive(ty.bitpackBackingInt(zcu)); + if (backing_int_val.isUndef(zcu)) return pt.undefValue(ty); return pt.bitpackValue(ty, backing_int_val); }, }, diff --git a/test/behavior/packed-struct.zig b/test/behavior/packed-struct.zig @@ -1240,3 +1240,9 @@ test "packed struct store of comparison result" { try expect(result2.a); try expect(!result2.b); } + +test "initialize packed struct field to undefined at comptime" { + const S = packed struct(u8) { x: u8 }; + const val: S = .{ .x = undefined }; + _ = val; +} diff --git a/test/behavior/packed-union.zig b/test/behavior/packed-union.zig @@ -219,3 +219,9 @@ test "packed union equality" { try S.doTest(x, y); comptime try S.doTest(x, y); } + +test "initialize packed union field to undefined at comptime" { + const U = packed union(u8) { x: u8 }; + const val: U = .{ .x = undefined }; + _ = val; +}