From c60896743d3b0776a44ac63582f51b6e64892528 Mon Sep 17 00:00:00 2001 From: Jacob Young Date: Wed, 21 Jun 2023 21:42:30 -0400 Subject: [PATCH] Value: handle more legacy tags when writing extern struct to memory Closes #16130 --- src/value.zig | 10 +++++++++- test/behavior/comptime_memory.zig | 7 +++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/value.zig b/src/value.zig index 573f0ca7e2..a1cffcb8eb 100644 --- a/src/value.zig +++ b/src/value.zig @@ -745,7 +745,15 @@ pub const Value = struct { .Extern => for (ty.structFields(mod).values(), 0..) |field, i| { const off = @intCast(usize, ty.structFieldOffset(i, mod)); const field_val = switch (val.ip_index) { - .none => val.castTag(.aggregate).?.data[i], + .none => switch (val.tag()) { + .bytes => { + buffer[off] = val.castTag(.bytes).?.data[i]; + continue; + }, + .aggregate => val.castTag(.aggregate).?.data[i], + .repeated => val.castTag(.repeated).?.data, + else => unreachable, + }, else => switch (mod.intern_pool.indexToKey(val.toIntern()).aggregate.storage) { .bytes => |bytes| { buffer[off] = bytes[i]; diff --git a/test/behavior/comptime_memory.zig b/test/behavior/comptime_memory.zig index ade0f0dc42..d327afb783 100644 --- a/test/behavior/comptime_memory.zig +++ b/test/behavior/comptime_memory.zig @@ -431,3 +431,10 @@ test "dereference undefined pointer to zero-bit type" { const p1: *[0]u32 = undefined; try testing.expect(p1.*.len == 0); } + +test "type pun extern struct" { + const S = extern struct { f: u8 }; + comptime var s = S{ .f = 123 }; + @ptrCast(*u8, &s).* = 72; + try testing.expectEqual(@as(u8, 72), s.f); +}