authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2023-03-21 00:27:33+02:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2023-03-21 03:31:03+02:00
loge70a0b2a6b329a76e9edc4d22c7b923841703a24
treeb4c701e37ca21ed6e232cc35d6c381262bb87a70
parent82133cd992575ab567091eaf2f12fbe5e326b5df

Value: implement reinterpreting enum field index as integer

Closes #15019

2 files changed, 25 insertions(+), 0 deletions(-)

src/value.zig+5
......@@ -1113,6 +1113,10 @@ pub const Value = extern union {
11131113 .bool_true,
11141114 => return BigIntMutable.init(&space.limbs, 1).toConst(),
11151115
1116 .enum_field_index => {
1117 const index = val.castTag(.enum_field_index).?.data;
1118 return BigIntMutable.init(&space.limbs, index).toConst();
1119 },
11161120 .runtime_value => {
11171121 const sub_val = val.castTag(.runtime_value).?.data;
11181122 return sub_val.toBigIntAdvanced(space, target, opt_sema);
......@@ -1983,6 +1987,7 @@ pub const Value = extern union {
19831987 .variable,
19841988 => .gt,
19851989
1990 .enum_field_index => return std.math.order(lhs.castTag(.enum_field_index).?.data, 0),
19861991 .runtime_value => {
19871992 // This is needed to correctly handle hashing the value.
19881993 // Checks in Sema should prevent direct comparisons from reaching here.
test/behavior/union.zig+20
......@@ -1513,3 +1513,23 @@ test "packed union with zero-bit field" {
15131513 };
15141514 try S.doTest(.{ .nested = .{ .zero = {} }, .bar = 42 });
15151515}
1516
1517test "reinterpreting enum value inside packed union" {
1518 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
1519 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
1520 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
1521 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
1522
1523 const U = packed union {
1524 tag: enum { a, b },
1525 val: u8,
1526
1527 fn doTest() !void {
1528 var u: @This() = .{ .tag = .a };
1529 u.val += 1;
1530 try expect(u.tag == .b);
1531 }
1532 };
1533 try U.doTest();
1534 comptime try U.doTest();
1535}