Fix edge case in tagName handling of unions

Closes #4226
master
LemonBoy 2020-01-18 19:58:05 +01:00
parent c53d94e512
commit b0f753e21d
2 changed files with 18 additions and 0 deletions

View File

@ -22354,6 +22354,15 @@ static IrInstruction *ir_analyze_instruction_enum_tag_name(IrAnalyze *ira, IrIns
assert(target->value->type->id == ZigTypeIdEnum); assert(target->value->type->id == ZigTypeIdEnum);
if (target->value->type->data.enumeration.src_field_count == 1 &&
!target->value->type->data.enumeration.non_exhaustive) {
TypeEnumField *only_field = &target->value->type->data.enumeration.fields[0];
ZigValue *array_val = create_const_str_lit(ira->codegen, only_field->name)->data.x_ptr.data.ref.pointee;
IrInstruction *result = ir_const(ira, &instruction->base, nullptr);
init_const_slice(ira->codegen, result->value, array_val, 0, buf_len(only_field->name), true);
return result;
}
if (instr_is_comptime(target)) { if (instr_is_comptime(target)) {
if ((err = type_resolve(ira->codegen, target->value->type, ResolveStatusZeroBitsKnown))) if ((err = type_resolve(ira->codegen, target->value->type, ResolveStatusZeroBitsKnown)))
return ira->codegen->invalid_instruction; return ira->codegen->invalid_instruction;

View File

@ -629,3 +629,12 @@ test "union initializer generates padding only if needed" {
var v = U{ .A = 532 }; var v = U{ .A = 532 };
expect(v.A == 532); expect(v.A == 532);
} }
test "runtime tag name with single field" {
const U = union(enum) {
A: i32,
};
var v = U{ .A = 42 };
expect(std.mem.eql(u8, @tagName(v), "A"));
}