From e70a0b2a6b329a76e9edc4d22c7b923841703a24 Mon Sep 17 00:00:00 2001 From: Veikka Tuominen Date: Tue, 21 Mar 2023 00:27:33 +0200 Subject: [PATCH] Value: implement reinterpreting enum field index as integer Closes #15019 --- src/value.zig | 5 +++++ test/behavior/union.zig | 20 ++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/value.zig b/src/value.zig index 1b6d2adc1e..e677414c0f 100644 --- a/src/value.zig +++ b/src/value.zig @@ -1113,6 +1113,10 @@ pub const Value = extern union { .bool_true, => return BigIntMutable.init(&space.limbs, 1).toConst(), + .enum_field_index => { + const index = val.castTag(.enum_field_index).?.data; + return BigIntMutable.init(&space.limbs, index).toConst(); + }, .runtime_value => { const sub_val = val.castTag(.runtime_value).?.data; return sub_val.toBigIntAdvanced(space, target, opt_sema); @@ -1983,6 +1987,7 @@ pub const Value = extern union { .variable, => .gt, + .enum_field_index => return std.math.order(lhs.castTag(.enum_field_index).?.data, 0), .runtime_value => { // This is needed to correctly handle hashing the value. // Checks in Sema should prevent direct comparisons from reaching here. diff --git a/test/behavior/union.zig b/test/behavior/union.zig index ff3f0b7e54..010b4a1ffa 100644 --- a/test/behavior/union.zig +++ b/test/behavior/union.zig @@ -1513,3 +1513,23 @@ test "packed union with zero-bit field" { }; try S.doTest(.{ .nested = .{ .zero = {} }, .bar = 42 }); } + +test "reinterpreting enum value inside packed union" { + if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO + + const U = packed union { + tag: enum { a, b }, + val: u8, + + fn doTest() !void { + var u: @This() = .{ .tag = .a }; + u.val += 1; + try expect(u.tag == .b); + } + }; + try U.doTest(); + comptime try U.doTest(); +}