Commit ab3ac1e670

Veikka Tuominen <git@vexu.eu>
2023-09-27 09:41:03
Value: fix assertion failure when mutating extern union
Closes #17292
1 parent 1606717
Changed files (2)
src
test
behavior
src/value.zig
@@ -398,7 +398,11 @@ pub const Value = struct {
             },
 
             .un => |un| Tag.@"union".create(arena, .{
-                .tag = un.tag.toValue(),
+                // toValue asserts that the value cannot be .none which is valid on unions.
+                .tag = .{
+                    .ip_index = un.tag,
+                    .legacy = undefined,
+                },
                 .val = un.val.toValue(),
             }),
 
test/behavior/union.zig
@@ -1662,3 +1662,16 @@ test "union with 128 bit integer" {
         }
     }
 }
+
+test "memset extern union at comptime" {
+    const U = extern union {
+        foo: u8,
+    };
+    const u = comptime blk: {
+        var u: U = undefined;
+        @memset(std.mem.asBytes(&u), 0);
+        u.foo = 0;
+        break :blk u;
+    };
+    try expect(u.foo == 0);
+}