zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit aefe4046de9b600bb84cb308ec3afb8f020a2db0 (tree)
parent d62229e3ad6069597b74874ba3b84fc185b2fa4c
Author: Andrew Kelley <andrew@ziglang.org>
Date:   Sat, 26 Feb 2022 15:51:59 -0700

Sema: implement `@enumToInt` for unions

Diffstat:
Msrc/Sema.zig | 20++++++++++----------
Mtest/behavior/union.zig | 4+++-
2 files changed, 13 insertions(+), 11 deletions(-)

diff --git a/src/Sema.zig b/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 {}", .{ diff --git a/test/behavior/union.zig b/test/behavior/union.zig @@ -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,