Sema: implement @enumToInt for unions
This commit is contained in:
20
src/Sema.zig
20
src/Sema.zig
@@ -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 {}", .{
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user