Sema: implement @enumToInt for unions

This commit is contained in:
Andrew Kelley
2022-02-26 15:51:59 -07:00
parent d62229e3ad
commit aefe4046de
2 changed files with 13 additions and 11 deletions

View File

@@ -5308,16 +5308,16 @@ fn zirEnumToInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
const enum_tag: Air.Inst.Ref = switch (operand_ty.zigTypeTag()) {
.Enum => operand,
.Union => {
//if (!operand_ty.unionHasTag()) {
// return sema.fail(
// block,
// operand_src,
// "untagged union '{}' cannot be converted to integer",
// .{dest_ty_src},
// );
//}
return sema.fail(block, operand_src, "TODO zirEnumToInt for tagged unions", .{});
.Union => blk: {
const tag_ty = operand_ty.unionTagType() orelse {
return sema.fail(
block,
operand_src,
"untagged union '{}' cannot be converted to integer",
.{src},
);
};
break :blk try sema.unionToTag(block, tag_ty, operand, operand_src);
},
else => {
return sema.fail(block, operand_src, "expected enum or tagged union, found {}", .{

View File

@@ -704,7 +704,9 @@ test "union with only 1 field casted to its enum type which has enum value speci
}
test "@enumToInt works on unions" {
if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
const Bar = union(enum) {
A: bool,