authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-12-27 18:10:24-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-12-27 18:10:24-07:00
logfc1a5cd9e74faf19e5974c733b0bcd9444d48b7b
tree3638e0eb93d37713bb03ed3c53b6921b9267c1a8
parent9dd4fb4130a71d2aaaaa361d8ee0b7135cb84162

Sema: implement `@tagName` for enum literals


3 files changed, 10 insertions(+), 5 deletions(-)

src/Sema.zig+5
...@@ -10054,6 +10054,11 @@ fn zirTagName(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air...@@ -10054,6 +10054,11 @@ fn zirTagName(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
10054 const operand_ty = sema.typeOf(operand);10054 const operand_ty = sema.typeOf(operand);
1005510055
10056 const enum_ty = switch (operand_ty.zigTypeTag()) {10056 const enum_ty = switch (operand_ty.zigTypeTag()) {
10057 .EnumLiteral => {
10058 const val = try sema.resolveConstValue(block, operand_src, operand);
10059 const bytes = val.castTag(.enum_literal).?.data;
10060 return sema.addStrLit(block, bytes);
10061 },
10057 .Enum => operand_ty,10062 .Enum => operand_ty,
10058 .Union => operand_ty.unionTagType() orelse {10063 .Union => operand_ty.unionTagType() orelse {
10059 const decl = operand_ty.getOwnerDecl();10064 const decl = operand_ty.getOwnerDecl();
test/behavior/enum_llvm.zig+5
...@@ -39,6 +39,11 @@ test "tag name with assigned enum values" {...@@ -39,6 +39,11 @@ test "tag name with assigned enum values" {
39 try expect(mem.eql(u8, @tagName(b), "B"));39 try expect(mem.eql(u8, @tagName(b), "B"));
40}40}
4141
42test "@tagName on enum literals" {
43 try expect(mem.eql(u8, @tagName(.FooBar), "FooBar"));
44 comptime try expect(mem.eql(u8, @tagName(.FooBar), "FooBar"));
45}
46
42const Bar = enum { A, B, C, D };47const Bar = enum { A, B, C, D };
4348
44test "enum literal casting to optional" {49test "enum literal casting to optional" {
test/behavior/enum_stage1.zig-5
...@@ -43,11 +43,6 @@ test "enum literal casting to error union with payload enum" {...@@ -43,11 +43,6 @@ test "enum literal casting to error union with payload enum" {
43 try expect((try bar) == Bar.B);43 try expect((try bar) == Bar.B);
44}44}
4545
46test "tagName on enum literals" {
47 try expect(mem.eql(u8, @tagName(.FooBar), "FooBar"));
48 comptime try expect(mem.eql(u8, @tagName(.FooBar), "FooBar"));
49}
50
51test "method call on an enum" {46test "method call on an enum" {
52 const S = struct {47 const S = struct {
53 const E = enum {48 const E = enum {