diff options
| author | LemonBoy <thatlemon@gmail.com> | 2020-01-18 09:55:18 +0100 |
|---|---|---|
| committer | LemonBoy <thatlemon@gmail.com> | 2020-01-18 09:55:18 +0100 |
| commit | 5f2bac010df0a2cf1e36622d6742baf13f4ffe75 (patch) | |
| tree | dbff10ac490a6842e059e81be5ce6967ddf861ba | |
| parent | b5ac079f88e9098ea9c95356518820a5c3fb42a8 (diff) | |
| download | zig-5f2bac010df0a2cf1e36622d6742baf13f4ffe75.tar.gz zig-5f2bac010df0a2cf1e36622d6742baf13f4ffe75.zip | |
Allow @tagName on enum literals
Closes #4214
| -rw-r--r-- | src/ir.cpp | 12 | ||||
| -rw-r--r-- | test/stage1/behavior/enum.zig | 5 |
2 files changed, 15 insertions, 2 deletions
diff --git a/src/ir.cpp b/src/ir.cpp index 88b4c1a832..7f6a1963c5 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -21389,9 +21389,9 @@ static IrInstruction *ir_analyze_union_tag(IrAnalyze *ira, IrInstruction *source if (type_is_invalid(value->value->type)) return ira->codegen->invalid_instruction; - if (value->value->type->id == ZigTypeIdEnum) { + if (value->value->type->id == ZigTypeIdEnum || + value->value->type->id == ZigTypeIdEnumLiteral) return value; - } if (value->value->type->id != ZigTypeIdUnion) { ir_add_error(ira, value, @@ -22352,6 +22352,14 @@ static IrInstruction *ir_analyze_instruction_enum_tag_name(IrAnalyze *ira, IrIns if (type_is_invalid(target->value->type)) return ira->codegen->invalid_instruction; + if (target->value->type->id == ZigTypeIdEnumLiteral) { + IrInstruction *result = ir_const(ira, &instruction->base, nullptr); + Buf *field_name = target->value->data.x_enum_literal; + ZigValue *array_val = create_const_str_lit(ira->codegen, field_name)->data.x_ptr.data.ref.pointee; + init_const_slice(ira->codegen, result->value, array_val, 0, buf_len(field_name), true); + return result; + } + assert(target->value->type->id == ZigTypeIdEnum); if (instr_is_comptime(target)) { diff --git a/test/stage1/behavior/enum.zig b/test/stage1/behavior/enum.zig index 83ad76b72c..5532a3ddf5 100644 --- a/test/stage1/behavior/enum.zig +++ b/test/stage1/behavior/enum.zig @@ -1094,3 +1094,8 @@ test "enum with one member default to u0 tag type" { }; comptime expect(@TagType(E0) == u0); } + +test "tagName on enum literals" { + expect(mem.eql(u8, @tagName(.FooBar), "FooBar")); + comptime expect(mem.eql(u8, @tagName(.FooBar), "FooBar")); +} |
