authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2026-05-06 11:21:40+01:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2026-05-07 06:22:47+02:00
logfecd28371d5c0e25357dfb91f75c8790f8b4155b
tree4bfe1fc081d3f3f82a061b6577fe1f4df731ccd6
parentfc1c83a363d32e749b9061044db63852ab7255d0

Sema: fix crash bitcasting undefined to bitpack type

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

3 files changed, 14 insertions(+), 0 deletions(-)

src/Sema/bitcast.zig+2
......@@ -565,6 +565,7 @@ const PackValueBits = struct {
565565 },
566566 .@"packed" => {
567567 const backing_int_val = try pack.primitive(ty.bitpackBackingInt(zcu));
568 if (backing_int_val.isUndef(zcu)) return pt.undefValue(ty);
568569 return pt.bitpackValue(ty, backing_int_val);
569570 },
570571 },
......@@ -659,6 +660,7 @@ const PackValueBits = struct {
659660 },
660661 .@"packed" => {
661662 const backing_int_val = try pack.primitive(ty.bitpackBackingInt(zcu));
663 if (backing_int_val.isUndef(zcu)) return pt.undefValue(ty);
662664 return pt.bitpackValue(ty, backing_int_val);
663665 },
664666 },
test/behavior/packed-struct.zig+6
......@@ -1240,3 +1240,9 @@ test "packed struct store of comparison result" {
12401240 try expect(result2.a);
12411241 try expect(!result2.b);
12421242}
1243
1244test "initialize packed struct field to undefined at comptime" {
1245 const S = packed struct(u8) { x: u8 };
1246 const val: S = .{ .x = undefined };
1247 _ = val;
1248}
test/behavior/packed-union.zig+6
......@@ -219,3 +219,9 @@ test "packed union equality" {
219219 try S.doTest(x, y);
220220 comptime try S.doTest(x, y);
221221}
222
223test "initialize packed union field to undefined at comptime" {
224 const U = packed union(u8) { x: u8 };
225 const val: U = .{ .x = undefined };
226 _ = val;
227}