diff --git a/src/ir.cpp b/src/ir.cpp index ae02ac9ca..ff4b552a9 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -22354,6 +22354,15 @@ static IrInstruction *ir_analyze_instruction_enum_tag_name(IrAnalyze *ira, IrIns 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 ((err = type_resolve(ira->codegen, target->value->type, ResolveStatusZeroBitsKnown))) return ira->codegen->invalid_instruction; diff --git a/test/stage1/behavior/union.zig b/test/stage1/behavior/union.zig index a24bee03e..4625b7573 100644 --- a/test/stage1/behavior/union.zig +++ b/test/stage1/behavior/union.zig @@ -629,3 +629,12 @@ test "union initializer generates padding only if needed" { var v = U{ .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")); +}