| author | |
| committer | |
| log | 00e1c0082c7c3cee377cc4e31b205c26f8943419 |
| tree | 3447c82d08969b65f1419c69a544cb895b6ae008 |
| parent | 1184b1d560c6a86b79bbda072478320431e09189 |
4 files changed, 28 insertions(+), 13 deletions(-)
doc/langref.html.in+4-4| ... | ... | @@ -2986,7 +2986,7 @@ test "@typeInfo" { |
| 2986 | 2986 | try expect(mem.eql(u8, @typeInfo(Small).Enum.fields[1].name, "two")); |
| 2987 | 2987 | } |
| 2988 | 2988 | |
| 2989 | // @tagName gives a []const u8 representation of an enum value: | |
| 2989 | // @tagName gives a [:0]const u8 representation of an enum value: | |
| 2990 | 2990 | test "@tagName" { |
| 2991 | 2991 | try expect(mem.eql(u8, @tagName(Small.three), "three")); |
| 2992 | 2992 | } |
| ... | ... | @@ -3233,7 +3233,7 @@ test "union method" { |
| 3233 | 3233 | {#code_end#} |
| 3234 | 3234 | <p> |
| 3235 | 3235 | {#link|@tagName#} can be used to return a {#link|comptime#} |
| 3236 | {#syntax#}[]const u8{#endsyntax#} value representing the field name: | |
| 3236 | {#syntax#}[:0]const u8{#endsyntax#} value representing the field name: | |
| 3237 | 3237 | </p> |
| 3238 | 3238 | {#code_begin|test#} |
| 3239 | 3239 | const std = @import("std"); |
| ... | ... | @@ -8494,9 +8494,9 @@ fn doTheTest() !void { |
| 8494 | 8494 | {#header_close#} |
| 8495 | 8495 | |
| 8496 | 8496 | {#header_open|@tagName#} |
| 8497 | <pre>{#syntax#}@tagName(value: anytype) []const u8{#endsyntax#}</pre> | |
| 8497 | <pre>{#syntax#}@tagName(value: anytype) [:0]const u8{#endsyntax#}</pre> | |
| 8498 | 8498 | <p> |
| 8499 | Converts an enum value or union value to a slice of bytes representing the name.</p><p>If the enum is non-exhaustive and the tag value does not map to a name, it invokes safety-checked {#link|Undefined Behavior#}. | |
| 8499 | Converts an enum value or union value to a string literal representing the name.</p><p>If the enum is non-exhaustive and the tag value does not map to a name, it invokes safety-checked {#link|Undefined Behavior#}. | |
| 8500 | 8500 | </p> |
| 8501 | 8501 | {#header_close#} |
| 8502 | 8502 |
src/stage1/ir.cpp+12-9| ... | ... | @@ -16898,8 +16898,9 @@ static IrInstGen *ir_analyze_instruction_enum_tag_name(IrAnalyze *ira, IrInstSrc |
| 16898 | 16898 | if (target_type->id == ZigTypeIdEnumLiteral) { |
| 16899 | 16899 | IrInstGen *result = ir_const(ira, &instruction->base.base, nullptr); |
| 16900 | 16900 | Buf *field_name = target->value->data.x_enum_literal; |
| 16901 | ZigValue *array_val = create_const_str_lit(ira->codegen, field_name)->data.x_ptr.data.ref.pointee; | |
| 16902 | init_const_slice(ira->codegen, result->value, array_val, 0, buf_len(field_name), true); | |
| 16901 | result->value = create_sentineled_str_lit( | |
| 16902 | ira->codegen, field_name, | |
| 16903 | ira->codegen->intern.for_zero_byte()); | |
| 16903 | 16904 | return result; |
| 16904 | 16905 | } |
| 16905 | 16906 | |
| ... | ... | @@ -16918,9 +16919,10 @@ static IrInstGen *ir_analyze_instruction_enum_tag_name(IrAnalyze *ira, IrInstSrc |
| 16918 | 16919 | |
| 16919 | 16920 | if (can_fold_enum_type(target_type)) { |
| 16920 | 16921 | TypeEnumField *only_field = &target_type->data.enumeration.fields[0]; |
| 16921 | ZigValue *array_val = create_const_str_lit(ira->codegen, only_field->name)->data.x_ptr.data.ref.pointee; | |
| 16922 | 16922 | IrInstGen *result = ir_const(ira, &instruction->base.base, nullptr); |
| 16923 | init_const_slice(ira->codegen, result->value, array_val, 0, buf_len(only_field->name), true); | |
| 16923 | result->value = create_sentineled_str_lit( | |
| 16924 | ira->codegen, only_field->name, | |
| 16925 | ira->codegen->intern.for_zero_byte()); | |
| 16924 | 16926 | return result; |
| 16925 | 16927 | } |
| 16926 | 16928 | |
| ... | ... | @@ -16936,16 +16938,17 @@ static IrInstGen *ir_analyze_instruction_enum_tag_name(IrAnalyze *ira, IrInstSrc |
| 16936 | 16938 | buf_sprintf("no tag by value %s", buf_ptr(int_buf))); |
| 16937 | 16939 | return ira->codegen->invalid_inst_gen; |
| 16938 | 16940 | } |
| 16939 | ZigValue *array_val = create_const_str_lit(ira->codegen, field->name)->data.x_ptr.data.ref.pointee; | |
| 16940 | 16941 | IrInstGen *result = ir_const(ira, &instruction->base.base, nullptr); |
| 16941 | init_const_slice(ira->codegen, result->value, array_val, 0, buf_len(field->name), true); | |
| 16942 | result->value = create_sentineled_str_lit( | |
| 16943 | ira->codegen, field->name, | |
| 16944 | ira->codegen->intern.for_zero_byte()); | |
| 16942 | 16945 | return result; |
| 16943 | 16946 | } |
| 16944 | 16947 | |
| 16945 | ZigType *u8_ptr_type = get_pointer_to_type_extra( | |
| 16948 | ZigType *u8_ptr_type = get_pointer_to_type_extra2( | |
| 16946 | 16949 | ira->codegen, ira->codegen->builtin_types.entry_u8, |
| 16947 | true, false, PtrLenUnknown, | |
| 16948 | 0, 0, 0, false); | |
| 16950 | true, false, PtrLenUnknown, 0, 0, 0, false, | |
| 16951 | VECTOR_INDEX_NONE, nullptr, ira->codegen->intern.for_zero_byte()); | |
| 16949 | 16952 | ZigType *result_type = get_slice_type(ira->codegen, u8_ptr_type); |
| 16950 | 16953 | return ir_build_tag_name_gen(ira, &instruction->base.base, target, result_type); |
| 16951 | 16954 | } |
test/behavior.zig+1| ... | ... | @@ -51,6 +51,7 @@ comptime { |
| 51 | 51 | _ = @import("behavior/bugs/3384.zig"); |
| 52 | 52 | _ = @import("behavior/bugs/3586.zig"); |
| 53 | 53 | _ = @import("behavior/bugs/3742.zig"); |
| 54 | _ = @import("behavior/bugs/3779.zig"); | |
| 54 | 55 | _ = @import("behavior/bugs/4328.zig"); |
| 55 | 56 | _ = @import("behavior/bugs/4560.zig"); |
| 56 | 57 | _ = @import("behavior/bugs/4769_a.zig"); |
test/behavior/bugs/3779.zig created+11| ... | ... | @@ -0,0 +1,11 @@ |
| 1 | const std = @import("std"); | |
| 2 | ||
| 3 | const TestEnum = enum { TestEnumValue }; | |
| 4 | const tag_name = @tagName(TestEnum.TestEnumValue); | |
| 5 | const ptr_tag_name: [*:0]const u8 = tag_name; | |
| 6 | ||
| 7 | test "@tagName() returns a string literal" { | |
| 8 | try std.testing.expectEqual([:0]const u8, @TypeOf(tag_name)); | |
| 9 | try std.testing.expectEqualStrings("TestEnumValue", tag_name); | |
| 10 | try std.testing.expectEqualStrings("TestEnumValue", ptr_tag_name[0..tag_name.len]); | |
| 11 | } |