authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-02-26 15:51:59-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-02-26 16:53:23-07:00
logaefe4046de9b600bb84cb308ec3afb8f020a2db0
tree47c632c569d4f1b86db6fe998c9aa51bb07dc1d6
parentd62229e3ad6069597b74874ba3b84fc185b2fa4c

Sema: implement `@enumToInt` for unions


2 files changed, 13 insertions(+), 11 deletions(-)

src/Sema.zig+10-10
...@@ -5308,16 +5308,16 @@ fn zirEnumToInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A...@@ -5308,16 +5308,16 @@ fn zirEnumToInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
53085308
5309 const enum_tag: Air.Inst.Ref = switch (operand_ty.zigTypeTag()) {5309 const enum_tag: Air.Inst.Ref = switch (operand_ty.zigTypeTag()) {
5310 .Enum => operand,5310 .Enum => operand,
5311 .Union => {5311 .Union => blk: {
5312 //if (!operand_ty.unionHasTag()) {5312 const tag_ty = operand_ty.unionTagType() orelse {
5313 // return sema.fail(5313 return sema.fail(
5314 // block,5314 block,
5315 // operand_src,5315 operand_src,
5316 // "untagged union '{}' cannot be converted to integer",5316 "untagged union '{}' cannot be converted to integer",
5317 // .{dest_ty_src},5317 .{src},
5318 // );5318 );
5319 //}5319 };
5320 return sema.fail(block, operand_src, "TODO zirEnumToInt for tagged unions", .{});5320 break :blk try sema.unionToTag(block, tag_ty, operand, operand_src);
5321 },5321 },
5322 else => {5322 else => {
5323 return sema.fail(block, operand_src, "expected enum or tagged union, found {}", .{5323 return sema.fail(block, operand_src, "expected enum or tagged union, found {}", .{
test/behavior/union.zig+3-1
...@@ -704,7 +704,9 @@ test "union with only 1 field casted to its enum type which has enum value speci...@@ -704,7 +704,9 @@ test "union with only 1 field casted to its enum type which has enum value speci
704}704}
705705
706test "@enumToInt works on unions" {706test "@enumToInt works on unions" {
707 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO707 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
708 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
709 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
708710
709 const Bar = union(enum) {711 const Bar = union(enum) {
710 A: bool,712 A: bool,