mirror of
https://github.com/ziglang/zig.git
synced 2026-02-12 20:37:54 +00:00
Merge pull request #4220 from LemonBoy/fix-4214
Allow @tagName on enum literals
This commit is contained in:
commit
f47b7a0437
33
src/ir.cpp
33
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)) {
|
||||
|
||||
@ -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"));
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user