authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2023-06-21 21:42:30-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-06-22 08:07:02-07:00
logc60896743d3b0776a44ac63582f51b6e64892528
tree916e6ef4aa4647f3907ccf96001ff5350db992f3
parent93e54f2354aa1b3de797a7ca5572bfa8c0f733f1

Value: handle more legacy tags when writing extern struct to memory

Closes #16130

2 files changed, 16 insertions(+), 1 deletions(-)

src/value.zig+9-1
...@@ -745,7 +745,15 @@ pub const Value = struct {...@@ -745,7 +745,15 @@ pub const Value = struct {
745 .Extern => for (ty.structFields(mod).values(), 0..) |field, i| {745 .Extern => for (ty.structFields(mod).values(), 0..) |field, i| {
746 const off = @intCast(usize, ty.structFieldOffset(i, mod));746 const off = @intCast(usize, ty.structFieldOffset(i, mod));
747 const field_val = switch (val.ip_index) {747 const field_val = switch (val.ip_index) {
748 .none => val.castTag(.aggregate).?.data[i],748 .none => switch (val.tag()) {
749 .bytes => {
750 buffer[off] = val.castTag(.bytes).?.data[i];
751 continue;
752 },
753 .aggregate => val.castTag(.aggregate).?.data[i],
754 .repeated => val.castTag(.repeated).?.data,
755 else => unreachable,
756 },
749 else => switch (mod.intern_pool.indexToKey(val.toIntern()).aggregate.storage) {757 else => switch (mod.intern_pool.indexToKey(val.toIntern()).aggregate.storage) {
750 .bytes => |bytes| {758 .bytes => |bytes| {
751 buffer[off] = bytes[i];759 buffer[off] = bytes[i];
test/behavior/comptime_memory.zig+7
...@@ -431,3 +431,10 @@ test "dereference undefined pointer to zero-bit type" {...@@ -431,3 +431,10 @@ test "dereference undefined pointer to zero-bit type" {
431 const p1: *[0]u32 = undefined;431 const p1: *[0]u32 = undefined;
432 try testing.expect(p1.*.len == 0);432 try testing.expect(p1.*.len == 0);
433}433}
434
435test "type pun extern struct" {
436 const S = extern struct { f: u8 };
437 comptime var s = S{ .f = 123 };
438 @ptrCast(*u8, &s).* = 72;
439 try testing.expectEqual(@as(u8, 72), s.f);
440}