authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-01-18 17:55:21-05:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-01-18 17:55:21-05:00
logf47b7a043772e3adda3cd10a76894703b499210a
tree1541f514f6321924da6b00651b8fec2485ca5eaf
parent72ec4456779839d3df4b41055fbaf47a57b69ac8
parentf456b88baecc0a841520035973e6887eb2573319
signature Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #4220 from LemonBoy/fix-4214

Allow @tagName on enum literals

2 files changed, 20 insertions(+), 18 deletions(-)

src/ir.cpp+15-18
...@@ -6030,8 +6030,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo...@@ -6030,8 +6030,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
6030 if (arg0_value == irb->codegen->invalid_instruction)6030 if (arg0_value == irb->codegen->invalid_instruction)
6031 return arg0_value;6031 return arg0_value;
60326032
6033 IrInstruction *actual_tag = ir_build_union_tag(irb, scope, node, arg0_value);6033 IrInstruction *tag_name = ir_build_tag_name(irb, scope, node, arg0_value);
6034 IrInstruction *tag_name = ir_build_tag_name(irb, scope, node, actual_tag);
6035 return ir_lval_wrap(irb, scope, tag_name, lval, result_loc);6034 return ir_lval_wrap(irb, scope, tag_name, lval, result_loc);
6036 }6035 }
6037 case BuiltinFnIdTagType:6036 case BuiltinFnIdTagType:
...@@ -21389,10 +21388,6 @@ static IrInstruction *ir_analyze_union_tag(IrAnalyze *ira, IrInstruction *source...@@ -21389,10 +21388,6 @@ static IrInstruction *ir_analyze_union_tag(IrAnalyze *ira, IrInstruction *source
21389 if (type_is_invalid(value->value->type))21388 if (type_is_invalid(value->value->type))
21390 return ira->codegen->invalid_instruction;21389 return ira->codegen->invalid_instruction;
2139121390
21392 if (value->value->type->id == ZigTypeIdEnum) {
21393 return value;
21394 }
21395
21396 if (value->value->type->id != ZigTypeIdUnion) {21391 if (value->value->type->id != ZigTypeIdUnion) {
21397 ir_add_error(ira, value,21392 ir_add_error(ira, value,
21398 buf_sprintf("expected enum or union type, found '%s'", buf_ptr(&value->value->type->name)));21393 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,...@@ -21460,12 +21455,6 @@ static IrInstruction *ir_analyze_instruction_switch_br(IrAnalyze *ira,
21460 if (type_is_invalid(case_value->value->type))21455 if (type_is_invalid(case_value->value->type))
21461 return ir_unreach_error(ira);21456 return ir_unreach_error(ira);
2146221457
21463 if (case_value->value->type->id == ZigTypeIdEnum) {
21464 case_value = ir_analyze_union_tag(ira, &switch_br_instruction->base, case_value);
21465 if (type_is_invalid(case_value->value->type))
21466 return ir_unreach_error(ira);
21467 }
21468
21469 IrInstruction *casted_case_value = ir_implicit_cast(ira, case_value, target_value->value->type);21458 IrInstruction *casted_case_value = ir_implicit_cast(ira, case_value, target_value->value->type);
21470 if (type_is_invalid(casted_case_value->value->type))21459 if (type_is_invalid(casted_case_value->value->type))
21471 return ir_unreach_error(ira);21460 return ir_unreach_error(ira);
...@@ -21510,12 +21499,6 @@ static IrInstruction *ir_analyze_instruction_switch_br(IrAnalyze *ira,...@@ -21510,12 +21499,6 @@ static IrInstruction *ir_analyze_instruction_switch_br(IrAnalyze *ira,
21510 if (type_is_invalid(new_value->value->type))21499 if (type_is_invalid(new_value->value->type))
21511 continue;21500 continue;
2151221501
21513 if (new_value->value->type->id == ZigTypeIdEnum) {
21514 new_value = ir_analyze_union_tag(ira, &switch_br_instruction->base, new_value);
21515 if (type_is_invalid(new_value->value->type))
21516 continue;
21517 }
21518
21519 IrInstruction *casted_new_value = ir_implicit_cast(ira, new_value, target_value->value->type);21502 IrInstruction *casted_new_value = ir_implicit_cast(ira, new_value, target_value->value->type);
21520 if (type_is_invalid(casted_new_value->value->type))21503 if (type_is_invalid(casted_new_value->value->type))
21521 continue;21504 continue;
...@@ -22352,6 +22335,20 @@ static IrInstruction *ir_analyze_instruction_enum_tag_name(IrAnalyze *ira, IrIns...@@ -22352,6 +22335,20 @@ static IrInstruction *ir_analyze_instruction_enum_tag_name(IrAnalyze *ira, IrIns
22352 if (type_is_invalid(target->value->type))22335 if (type_is_invalid(target->value->type))
22353 return ira->codegen->invalid_instruction;22336 return ira->codegen->invalid_instruction;
2235422337
22338 if (target->value->type->id == ZigTypeIdEnumLiteral) {
22339 IrInstruction *result = ir_const(ira, &instruction->base, nullptr);
22340 Buf *field_name = target->value->data.x_enum_literal;
22341 ZigValue *array_val = create_const_str_lit(ira->codegen, field_name)->data.x_ptr.data.ref.pointee;
22342 init_const_slice(ira->codegen, result->value, array_val, 0, buf_len(field_name), true);
22343 return result;
22344 }
22345
22346 if (target->value->type->id == ZigTypeIdUnion) {
22347 target = ir_analyze_union_tag(ira, &instruction->base, target);
22348 if (type_is_invalid(target->value->type))
22349 return ira->codegen->invalid_instruction;
22350 }
22351
22355 assert(target->value->type->id == ZigTypeIdEnum);22352 assert(target->value->type->id == ZigTypeIdEnum);
2235622353
22357 if (instr_is_comptime(target)) {22354 if (instr_is_comptime(target)) {
test/stage1/behavior/enum.zig+5
...@@ -1094,3 +1094,8 @@ test "enum with one member default to u0 tag type" {...@@ -1094,3 +1094,8 @@ test "enum with one member default to u0 tag type" {
1094 };1094 };
1095 comptime expect(@TagType(E0) == u0);1095 comptime expect(@TagType(E0) == u0);
1096}1096}
1097
1098test "tagName on enum literals" {
1099 expect(mem.eql(u8, @tagName(.FooBar), "FooBar"));
1100 comptime expect(mem.eql(u8, @tagName(.FooBar), "FooBar"));
1101}