Commit c60896743d

Jacob Young <jacobly0@users.noreply.github.com>
2023-06-22 03:42:30
Value: handle more legacy tags when writing extern struct to memory
Closes #16130
1 parent 93e54f2
Changed files (2)
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];
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);
+}