Value: implement reinterpreting enum field index as integer

Closes #15019
This commit is contained in:
Veikka Tuominen
2023-03-21 00:27:33 +02:00
parent 82133cd992
commit e70a0b2a6b
2 changed files with 25 additions and 0 deletions

View File

@@ -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.

View File

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