Value: fix assertion failure when mutating extern union

Closes #17292
This commit is contained in:
Veikka Tuominen 2023-09-27 10:41:03 +03:00 committed by Andrew Kelley
parent 1606717b5f
commit ab3ac1e670
2 changed files with 18 additions and 1 deletions

View File

@ -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(),
}),

View File

@ -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);
}