diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2020-01-18 17:55:21 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-01-18 17:55:21 -0500 |
| commit | f47b7a043772e3adda3cd10a76894703b499210a (patch) | |
| tree | 1541f514f6321924da6b00651b8fec2485ca5eaf | |
| parent | 72ec4456779839d3df4b41055fbaf47a57b69ac8 (diff) | |
| parent | f456b88baecc0a841520035973e6887eb2573319 (diff) | |
| download | zig-f47b7a043772e3adda3cd10a76894703b499210a.tar.gz zig-f47b7a043772e3adda3cd10a76894703b499210a.zip | |
Merge pull request #4220 from LemonBoy/fix-4214
Allow @tagName on enum literals
| -rw-r--r-- | src/ir.cpp | 33 | ||||
| -rw-r--r-- | test/stage1/behavior/enum.zig | 5 |
2 files changed, 20 insertions, 18 deletions
diff --git a/src/ir.cpp b/src/ir.cpp index 88b4c1a832..42a5043279 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -6030,8 +6030,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo if (arg0_value == irb->codegen->invalid_instruction) return arg0_value; - IrInstruction *actual_tag = ir_build_union_tag(irb, scope, node, arg0_value); - IrInstruction *tag_name = ir_build_tag_name(irb, scope, node, actual_tag); + IrInstruction *tag_name = ir_build_tag_name(irb, scope, node, arg0_value); return ir_lval_wrap(irb, scope, tag_name, lval, result_loc); } case BuiltinFnIdTagType: @@ -21389,10 +21388,6 @@ 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) { - return value; - } - if (value->value->type->id != ZigTypeIdUnion) { ir_add_error(ira, value, buf_sprintf("expected enum or union type, found '%s'", buf_ptr(&value->value->type->name))); @@ -21460,12 +21455,6 @@ static IrInstruction *ir_analyze_instruction_switch_br(IrAnalyze *ira, if (type_is_invalid(case_value->value->type)) return ir_unreach_error(ira); - if (case_value->value->type->id == ZigTypeIdEnum) { - case_value = ir_analyze_union_tag(ira, &switch_br_instruction->base, case_value); - if (type_is_invalid(case_value->value->type)) - return ir_unreach_error(ira); - } - IrInstruction *casted_case_value = ir_implicit_cast(ira, case_value, target_value->value->type); if (type_is_invalid(casted_case_value->value->type)) return ir_unreach_error(ira); @@ -21510,12 +21499,6 @@ static IrInstruction *ir_analyze_instruction_switch_br(IrAnalyze *ira, if (type_is_invalid(new_value->value->type)) continue; - if (new_value->value->type->id == ZigTypeIdEnum) { - new_value = ir_analyze_union_tag(ira, &switch_br_instruction->base, new_value); - if (type_is_invalid(new_value->value->type)) - continue; - } - IrInstruction *casted_new_value = ir_implicit_cast(ira, new_value, target_value->value->type); if (type_is_invalid(casted_new_value->value->type)) continue; @@ -22352,6 +22335,20 @@ 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; + } + + if (target->value->type->id == ZigTypeIdUnion) { + target = ir_analyze_union_tag(ira, &instruction->base, target); + if (type_is_invalid(target->value->type)) + return ira->codegen->invalid_instruction; + } + 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")); +} |
