authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2021-06-20 20:27:34+03:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2021-06-20 20:27:34+03:00
log6de45c826c39ecd2d39deed20fd426240c496402
treebdc116b10c6e605098356b6cff9899789e7b3a5e
parent1071ca61294c81c939574f70637191b957aee1b1
parent9e88356282861e5812d95587cb8fab7f902bc85b
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #8636 from jmc-88/issue-3779

Change builtins to return a string literal

8 files changed, 108 insertions(+), 54 deletions(-)

doc/langref.html.in+7-7
......@@ -2986,7 +2986,7 @@ test "@typeInfo" {
29862986 try expect(mem.eql(u8, @typeInfo(Small).Enum.fields[1].name, "two"));
29872987}
29882988
2989// @tagName gives a []const u8 representation of an enum value:
2989// @tagName gives a [:0]const u8 representation of an enum value:
29902990test "@tagName" {
29912991 try expect(mem.eql(u8, @tagName(Small.three), "three"));
29922992}
......@@ -3233,7 +3233,7 @@ test "union method" {
32333233 {#code_end#}
32343234 <p>
32353235 {#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:
32373237 </p>
32383238 {#code_begin|test#}
32393239const std = @import("std");
......@@ -7447,7 +7447,7 @@ test "main" {
74477447 {#see_also|@divFloor|@divExact#}
74487448 {#header_close#}
74497449 {#header_open|@embedFile#}
7450 <pre>{#syntax#}@embedFile(comptime path: []const u8) *const [X:0]u8{#endsyntax#}</pre>
7450 <pre>{#syntax#}@embedFile(comptime path: []const u8) *const [N:0]u8{#endsyntax#}</pre>
74517451 <p>
74527452 This function returns a compile time constant pointer to null-terminated,
74537453 fixed-size array with length equal to the byte count of the file given by
......@@ -7475,7 +7475,7 @@ test "main" {
74757475 {#header_close#}
74767476
74777477 {#header_open|@errorName#}
7478 <pre>{#syntax#}@errorName(err: anyerror) []const u8{#endsyntax#}</pre>
7478 <pre>{#syntax#}@errorName(err: anyerror) [:0]const u8{#endsyntax#}</pre>
74797479 <p>
74807480 This function returns the string representation of an error. The string representation
74817481 of {#syntax#}error.OutOfMem{#endsyntax#} is {#syntax#}"OutOfMem"{#endsyntax#}.
......@@ -8494,9 +8494,9 @@ fn doTheTest() !void {
84948494 {#header_close#}
84958495
84968496 {#header_open|@tagName#}
8497 <pre>{#syntax#}@tagName(value: anytype) []const u8{#endsyntax#}</pre>
8497 <pre>{#syntax#}@tagName(value: anytype) [:0]const u8{#endsyntax#}</pre>
84988498 <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#}.
85008500 </p>
85018501 {#header_close#}
85028502
......@@ -8623,7 +8623,7 @@ test "integer truncation" {
86238623 {#header_close#}
86248624
86258625 {#header_open|@typeName#}
8626 <pre>{#syntax#}@typeName(T: type) [N]u8{#endsyntax#}</pre>
8626 <pre>{#syntax#}@typeName(T: type) *const [N:0]u8{#endsyntax#}</pre>
86278627 <p>
86288628 This function returns the string representation of a type, as
86298629 an array. It is equivalent to a string literal of the type name.
src/stage1/analyze.cpp+10-5
......@@ -6291,6 +6291,11 @@ ZigValue *create_const_str_lit(CodeGen *g, Buf *str) {
62916291 return const_val;
62926292}
62936293
6294ZigValue *create_sentineled_str_lit(CodeGen *g, Buf *str, ZigValue *sentinel) {
6295 ZigValue *array_val = create_const_str_lit(g, str)->data.x_ptr.data.ref.pointee;
6296 return create_const_slice(g, array_val, 0, buf_len(str), true, sentinel);
6297}
6298
62946299void init_const_bigint(ZigValue *const_val, ZigType *type, const BigInt *bigint) {
62956300 const_val->special = ConstValSpecialStatic;
62966301 const_val->type = type;
......@@ -6444,12 +6449,12 @@ ZigValue *create_const_type(CodeGen *g, ZigType *type_value) {
64446449}
64456450
64466451void init_const_slice(CodeGen *g, ZigValue *const_val, ZigValue *array_val,
6447 size_t start, size_t len, bool is_const)
6452 size_t start, size_t len, bool is_const, ZigValue *sentinel)
64486453{
64496454 assert(array_val->type->id == ZigTypeIdArray);
64506455
6451 ZigType *ptr_type = get_pointer_to_type_extra(g, array_val->type->data.array.child_type,
6452 is_const, false, PtrLenUnknown, 0, 0, 0, false);
6456 ZigType *ptr_type = get_pointer_to_type_extra2(g, array_val->type->data.array.child_type,
6457 is_const, false, PtrLenUnknown, 0, 0, 0, false, VECTOR_INDEX_NONE, nullptr, sentinel);
64536458
64546459 const_val->special = ConstValSpecialStatic;
64556460 const_val->type = get_slice_type(g, ptr_type);
......@@ -6460,9 +6465,9 @@ void init_const_slice(CodeGen *g, ZigValue *const_val, ZigValue *array_val,
64606465 init_const_usize(g, const_val->data.x_struct.fields[slice_len_index], len);
64616466}
64626467
6463ZigValue *create_const_slice(CodeGen *g, ZigValue *array_val, size_t start, size_t len, bool is_const) {
6468ZigValue *create_const_slice(CodeGen *g, ZigValue *array_val, size_t start, size_t len, bool is_const, ZigValue *sentinel) {
64646469 ZigValue *const_val = g->pass1_arena->create<ZigValue>();
6465 init_const_slice(g, const_val, array_val, start, len, is_const);
6470 init_const_slice(g, const_val, array_val, start, len, is_const, sentinel);
64666471 return const_val;
64676472}
64686473
src/stage1/analyze.hpp+3-2
......@@ -144,6 +144,7 @@ ScopeExpr *create_expr_scope(CodeGen *g, AstNode *node, Scope *parent);
144144
145145void init_const_str_lit(CodeGen *g, ZigValue *const_val, Buf *str, bool move_str);
146146ZigValue *create_const_str_lit(CodeGen *g, Buf *str);
147ZigValue *create_sentineled_str_lit(CodeGen *g, Buf *str, ZigValue *sentinel);
147148
148149void init_const_bigint(ZigValue *const_val, ZigType *type, const BigInt *bigint);
149150ZigValue *create_const_bigint(CodeGen *g, ZigType *type, const BigInt *bigint);
......@@ -186,8 +187,8 @@ ZigValue *create_const_ptr_array(CodeGen *g, ZigValue *array_val, size_t elem_in
186187 bool is_const, PtrLen ptr_len);
187188
188189void init_const_slice(CodeGen *g, ZigValue *const_val, ZigValue *array_val,
189 size_t start, size_t len, bool is_const);
190ZigValue *create_const_slice(CodeGen *g, ZigValue *array_val, size_t start, size_t len, bool is_const);
190 size_t start, size_t len, bool is_const, ZigValue *sentinel);
191ZigValue *create_const_slice(CodeGen *g, ZigValue *array_val, size_t start, size_t len, bool is_const, ZigValue *sentinel);
191192
192193void init_const_null(ZigValue *const_val, ZigType *type);
193194ZigValue *create_const_null(CodeGen *g, ZigType *type);
src/stage1/codegen.cpp+3-3
......@@ -1022,7 +1022,7 @@ static LLVMValueRef get_panic_msg_ptr_val(CodeGen *g, PanicMsgId msg_id) {
10221022
10231023 Buf *buf_msg = panic_msg_buf(msg_id);
10241024 ZigValue *array_val = create_const_str_lit(g, buf_msg)->data.x_ptr.data.ref.pointee;
1025 init_const_slice(g, val, array_val, 0, buf_len(buf_msg), true);
1025 init_const_slice(g, val, array_val, 0, buf_len(buf_msg), true, nullptr);
10261026
10271027 render_const_val(g, val, "");
10281028 render_const_val_global(g, val, "");
......@@ -9428,7 +9428,7 @@ static void update_test_functions_builtin_decl(CodeGen *g) {
94289428
94299429 ZigValue *name_field = this_val->data.x_struct.fields[0];
94309430 ZigValue *name_array_val = create_const_str_lit(g, &test_fn_entry->symbol_name)->data.x_ptr.data.ref.pointee;
9431 init_const_slice(g, name_field, name_array_val, 0, buf_len(&test_fn_entry->symbol_name), true);
9431 init_const_slice(g, name_field, name_array_val, 0, buf_len(&test_fn_entry->symbol_name), true, nullptr);
94329432
94339433 ZigValue *fn_field = this_val->data.x_struct.fields[1];
94349434 fn_field->type = fn_type;
......@@ -9452,7 +9452,7 @@ static void update_test_functions_builtin_decl(CodeGen *g) {
94529452 }
94539453 report_errors_and_maybe_exit(g);
94549454
9455 ZigValue *test_fn_slice = create_const_slice(g, test_fn_array, 0, g->test_fns.length, true);
9455 ZigValue *test_fn_slice = create_const_slice(g, test_fn_array, 0, g->test_fns.length, true, nullptr);
94569456
94579457 update_compile_var(g, buf_create_from_str("test_functions"), test_fn_slice);
94589458 assert(g->test_runner_package != nullptr);
src/stage1/ir.cpp+41-37
......@@ -5190,7 +5190,7 @@ static IrInstGen *ir_resolve_ptr_of_array_to_slice(IrAnalyze *ira, IrInst* sourc
51905190 // undef_array->type = array_type;
51915191
51925192 // IrInstGen *result = ir_const(ira, source_instr, wanted_type);
5193 // init_const_slice(ira->codegen, result->value, undef_array, 0, 0, false);
5193 // init_const_slice(ira->codegen, result->value, undef_array, 0, 0, false, nullptr);
51945194 // result->value->data.x_struct.fields[slice_ptr_index]->data.x_ptr.mut = ConstPtrMutComptimeConst;
51955195 // result->value->type = wanted_type;
51965196 // return result;
......@@ -5217,7 +5217,7 @@ static IrInstGen *ir_resolve_ptr_of_array_to_slice(IrAnalyze *ira, IrInst* sourc
52175217 undef_array->type = array_type;
52185218
52195219 IrInstGen *result = ir_const(ira, source_instr, wanted_type);
5220 init_const_slice(ira->codegen, result->value, undef_array, 0, 0, false);
5220 init_const_slice(ira->codegen, result->value, undef_array, 0, 0, false, nullptr);
52215221 result->value->data.x_struct.fields[slice_ptr_index]->data.x_ptr.mut = ConstPtrMutComptimeConst;
52225222 result->value->type = wanted_type;
52235223 return result;
......@@ -5230,7 +5230,7 @@ static IrInstGen *ir_resolve_ptr_of_array_to_slice(IrAnalyze *ira, IrInst* sourc
52305230 IrInstGen *result = ir_const(ira, source_instr, wanted_type);
52315231 init_const_slice(ira->codegen, result->value, array_val,
52325232 array_ptr_val->data.x_ptr.data.base_array.elem_index,
5233 array_type->data.array.len, wanted_const);
5233 array_type->data.array.len, wanted_const, nullptr);
52345234 result->value->data.x_struct.fields[slice_ptr_index]->data.x_ptr.mut = array_ptr_val->data.x_ptr.mut;
52355235 result->value->type = wanted_type;
52365236 return result;
......@@ -5243,7 +5243,7 @@ static IrInstGen *ir_resolve_ptr_of_array_to_slice(IrAnalyze *ira, IrInst* sourc
52435243 assert(array_ptr_val->type->id == ZigTypeIdPointer);
52445244
52455245 IrInstGen *result = ir_const(ira, source_instr, wanted_type);
5246 init_const_slice(ira->codegen, result->value, pointee, 0, array_type->data.array.len, wanted_const);
5246 init_const_slice(ira->codegen, result->value, pointee, 0, array_type->data.array.len, wanted_const, nullptr);
52475247 result->value->data.x_struct.fields[slice_ptr_index]->data.x_ptr.mut = array_ptr_val->data.x_ptr.mut;
52485248 result->value->type = wanted_type;
52495249 return result;
......@@ -14449,7 +14449,7 @@ static IrInstGen *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstSrcElemP
1444914449 }
1445014450
1445114451 init_const_slice(ira->codegen, array_ptr_val, array_init_val, 0, actual_array_type->data.array.len,
14452 false);
14452 false, nullptr);
1445314453 array_ptr_val->data.x_struct.fields[slice_ptr_index]->data.x_ptr.mut = ConstPtrMutInfer;
1445414454 } else {
1445514455 ir_add_error_node(ira, elem_ptr_instruction->init_array_type_source_node,
......@@ -16864,26 +16864,27 @@ static IrInstGen *ir_analyze_instruction_err_name(IrAnalyze *ira, IrInstSrcErrNa
1686416864 if (type_is_invalid(casted_value->value->type))
1686516865 return ira->codegen->invalid_inst_gen;
1686616866
16867 ZigType *u8_ptr_type = get_pointer_to_type_extra(ira->codegen, ira->codegen->builtin_types.entry_u8,
16868 true, false, PtrLenUnknown, 0, 0, 0, false);
16869 ZigType *str_type = get_slice_type(ira->codegen, u8_ptr_type);
1687016867 if (instr_is_comptime(casted_value)) {
1687116868 ZigValue *val = ir_resolve_const(ira, casted_value, UndefBad);
1687216869 if (val == nullptr)
1687316870 return ira->codegen->invalid_inst_gen;
1687416871 ErrorTableEntry *err = casted_value->value->data.x_err_set;
1687516872 if (!err->cached_error_name_val) {
16876 ZigValue *array_val = create_const_str_lit(ira->codegen, &err->name)->data.x_ptr.data.ref.pointee;
16877 err->cached_error_name_val = create_const_slice(ira->codegen, array_val, 0, buf_len(&err->name), true);
16873 err->cached_error_name_val = create_sentineled_str_lit(
16874 ira->codegen, &err->name,
16875 ira->codegen->intern.for_zero_byte());
1687816876 }
1687916877 IrInstGen *result = ir_const(ira, &instruction->base.base, nullptr);
16880 copy_const_val(ira->codegen, result->value, err->cached_error_name_val);
16881 result->value->type = str_type;
16878 result->value = err->cached_error_name_val;
1688216879 return result;
1688316880 }
1688416881
1688516882 ira->codegen->generate_error_name_table = true;
1688616883
16884 ZigType *u8_ptr_type = get_pointer_to_type_extra2(ira->codegen, ira->codegen->builtin_types.entry_u8,
16885 true, false, PtrLenUnknown, 0, 0, 0, false,
16886 VECTOR_INDEX_NONE, nullptr, ira->codegen->intern.for_zero_byte());
16887 ZigType *str_type = get_slice_type(ira->codegen, u8_ptr_type);
1688716888 return ir_build_err_name_gen(ira, &instruction->base.base, value, str_type);
1688816889}
1688916890
......@@ -16898,8 +16899,9 @@ static IrInstGen *ir_analyze_instruction_enum_tag_name(IrAnalyze *ira, IrInstSrc
1689816899 if (target_type->id == ZigTypeIdEnumLiteral) {
1689916900 IrInstGen *result = ir_const(ira, &instruction->base.base, nullptr);
1690016901 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);
16902 result->value = create_sentineled_str_lit(
16903 ira->codegen, field_name,
16904 ira->codegen->intern.for_zero_byte());
1690316905 return result;
1690416906 }
1690516907
......@@ -16918,9 +16920,10 @@ static IrInstGen *ir_analyze_instruction_enum_tag_name(IrAnalyze *ira, IrInstSrc
1691816920
1691916921 if (can_fold_enum_type(target_type)) {
1692016922 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;
1692216923 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);
16924 result->value = create_sentineled_str_lit(
16925 ira->codegen, only_field->name,
16926 ira->codegen->intern.for_zero_byte());
1692416927 return result;
1692516928 }
1692616929
......@@ -16936,16 +16939,17 @@ static IrInstGen *ir_analyze_instruction_enum_tag_name(IrAnalyze *ira, IrInstSrc
1693616939 buf_sprintf("no tag by value %s", buf_ptr(int_buf)));
1693716940 return ira->codegen->invalid_inst_gen;
1693816941 }
16939 ZigValue *array_val = create_const_str_lit(ira->codegen, field->name)->data.x_ptr.data.ref.pointee;
1694016942 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);
16943 result->value = create_sentineled_str_lit(
16944 ira->codegen, field->name,
16945 ira->codegen->intern.for_zero_byte());
1694216946 return result;
1694316947 }
1694416948
16945 ZigType *u8_ptr_type = get_pointer_to_type_extra(
16949 ZigType *u8_ptr_type = get_pointer_to_type_extra2(
1694616950 ira->codegen, ira->codegen->builtin_types.entry_u8,
16947 true, false, PtrLenUnknown,
16948 0, 0, 0, false);
16951 true, false, PtrLenUnknown, 0, 0, 0, false,
16952 VECTOR_INDEX_NONE, nullptr, ira->codegen->intern.for_zero_byte());
1694916953 ZigType *result_type = get_slice_type(ira->codegen, u8_ptr_type);
1695016954 return ir_build_tag_name_gen(ira, &instruction->base.base, target, result_type);
1695116955}
......@@ -17249,7 +17253,7 @@ static Error ir_make_type_info_decls(IrAnalyze *ira, IrInst* source_instr, ZigVa
1724917253 declaration_array->type = get_array_type(ira->codegen, type_info_declaration_type, declaration_count, nullptr);
1725017254 declaration_array->data.x_array.special = ConstArraySpecialNone;
1725117255 declaration_array->data.x_array.data.s_none.elements = ira->codegen->pass1_arena->allocate<ZigValue>(declaration_count);
17252 init_const_slice(ira->codegen, out_val, declaration_array, 0, declaration_count, false);
17256 init_const_slice(ira->codegen, out_val, declaration_array, 0, declaration_count, false, nullptr);
1725317257
1725417258 // Loop through the declarations and generate info.
1725517259 decl_it = decls_scope->decl_table.entry_iterator();
......@@ -17272,7 +17276,7 @@ static Error ir_make_type_info_decls(IrAnalyze *ira, IrInst* source_instr, ZigVa
1727217276
1727317277 ZigValue **inner_fields = alloc_const_vals_ptrs(ira->codegen, 3);
1727417278 ZigValue *name = create_const_str_lit(ira->codegen, curr_entry->key)->data.x_ptr.data.ref.pointee;
17275 init_const_slice(ira->codegen, inner_fields[0], name, 0, buf_len(curr_entry->key), true);
17279 init_const_slice(ira->codegen, inner_fields[0], name, 0, buf_len(curr_entry->key), true, nullptr);
1727617280 inner_fields[1]->special = ConstValSpecialStatic;
1727717281 inner_fields[1]->type = ira->codegen->builtin_types.entry_bool;
1727817282 inner_fields[1]->data.x_bool = curr_entry->value->visib_mod == VisibModPub;
......@@ -17368,7 +17372,7 @@ static Error ir_make_type_info_decls(IrAnalyze *ira, IrInst* source_instr, ZigVa
1736817372 if (fn_node->is_extern && fn_node->lib_name != nullptr && buf_len(fn_node->lib_name) > 0) {
1736917373 ZigValue *slice_val = ira->codegen->pass1_arena->create<ZigValue>();
1737017374 ZigValue *lib_name = create_const_str_lit(ira->codegen, fn_node->lib_name)->data.x_ptr.data.ref.pointee;
17371 init_const_slice(ira->codegen, slice_val, lib_name, 0, buf_len(fn_node->lib_name), true);
17375 init_const_slice(ira->codegen, slice_val, lib_name, 0, buf_len(fn_node->lib_name), true, nullptr);
1737217376 set_optional_payload(fn_decl_fields[5], slice_val);
1737317377 } else {
1737417378 set_optional_payload(fn_decl_fields[5], nullptr);
......@@ -17388,14 +17392,14 @@ static Error ir_make_type_info_decls(IrAnalyze *ira, IrInst* source_instr, ZigVa
1738817392 fn_arg_name_array->data.x_array.special = ConstArraySpecialNone;
1738917393 fn_arg_name_array->data.x_array.data.s_none.elements = ira->codegen->pass1_arena->allocate<ZigValue>(fn_arg_count);
1739017394
17391 init_const_slice(ira->codegen, fn_decl_fields[7], fn_arg_name_array, 0, fn_arg_count, false);
17395 init_const_slice(ira->codegen, fn_decl_fields[7], fn_arg_name_array, 0, fn_arg_count, false, nullptr);
1739217396
1739317397 for (size_t fn_arg_index = 0; fn_arg_index < fn_arg_count; fn_arg_index++) {
1739417398 ZigVar *arg_var = fn_entry->variable_list.at(fn_arg_index);
1739517399 ZigValue *fn_arg_name_val = &fn_arg_name_array->data.x_array.data.s_none.elements[fn_arg_index];
1739617400 ZigValue *arg_name = create_const_str_lit(ira->codegen,
1739717401 buf_create_from_str(arg_var->name))->data.x_ptr.data.ref.pointee;
17398 init_const_slice(ira->codegen, fn_arg_name_val, arg_name, 0, strlen(arg_var->name), true);
17402 init_const_slice(ira->codegen, fn_arg_name_val, arg_name, 0, strlen(arg_var->name), true, nullptr);
1739917403 fn_arg_name_val->parent.id = ConstParentIdArray;
1740017404 fn_arg_name_val->parent.data.p_array.array_val = fn_arg_name_array;
1740117405 fn_arg_name_val->parent.data.p_array.elem_index = fn_arg_index;
......@@ -17531,7 +17535,7 @@ static void make_enum_field_val(IrAnalyze *ira, ZigValue *enum_field_val, TypeEn
1753117535 inner_fields[1]->type = ira->codegen->builtin_types.entry_num_lit_int;
1753217536
1753317537 ZigValue *name = create_const_str_lit(ira->codegen, enum_field->name)->data.x_ptr.data.ref.pointee;
17534 init_const_slice(ira->codegen, inner_fields[0], name, 0, buf_len(enum_field->name), true);
17538 init_const_slice(ira->codegen, inner_fields[0], name, 0, buf_len(enum_field->name), true, nullptr);
1753517539
1753617540 bigint_init_bigint(&inner_fields[1]->data.x_bigint, &enum_field->value);
1753717541
......@@ -17732,7 +17736,7 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy
1773217736 enum_field_array->data.x_array.special = ConstArraySpecialNone;
1773317737 enum_field_array->data.x_array.data.s_none.elements = ira->codegen->pass1_arena->allocate<ZigValue>(enum_field_count);
1773417738
17735 init_const_slice(ira->codegen, fields[2], enum_field_array, 0, enum_field_count, false);
17739 init_const_slice(ira->codegen, fields[2], enum_field_array, 0, enum_field_count, false, nullptr);
1773617740
1773717741 for (uint32_t enum_field_index = 0; enum_field_index < enum_field_count; enum_field_index++)
1773817742 {
......@@ -17785,7 +17789,7 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy
1778517789 error_array->data.x_array.special = ConstArraySpecialNone;
1778617790 error_array->data.x_array.data.s_none.elements = ira->codegen->pass1_arena->allocate<ZigValue>(error_count);
1778717791
17788 init_const_slice(ira->codegen, slice_val, error_array, 0, error_count, false);
17792 init_const_slice(ira->codegen, slice_val, error_array, 0, error_count, false, nullptr);
1778917793 for (uint32_t error_index = 0; error_index < error_count; error_index++) {
1779017794 ErrorTableEntry *error = type_entry->data.error_set.errors[error_index];
1779117795 ZigValue *error_val = &error_array->data.x_array.data.s_none.elements[error_index];
......@@ -17800,7 +17804,7 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy
1780017804 name = error->cached_error_name_val;
1780117805 if (name == nullptr)
1780217806 name = create_const_str_lit(ira->codegen, &error->name)->data.x_ptr.data.ref.pointee;
17803 init_const_slice(ira->codegen, inner_fields[0], name, 0, buf_len(&error->name), true);
17807 init_const_slice(ira->codegen, inner_fields[0], name, 0, buf_len(&error->name), true, nullptr);
1780417808
1780517809 error_val->data.x_struct.fields = inner_fields;
1780617810 error_val->parent.id = ConstParentIdArray;
......@@ -17881,7 +17885,7 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy
1788117885 union_field_array->data.x_array.special = ConstArraySpecialNone;
1788217886 union_field_array->data.x_array.data.s_none.elements = ira->codegen->pass1_arena->allocate<ZigValue>(union_field_count);
1788317887
17884 init_const_slice(ira->codegen, fields[2], union_field_array, 0, union_field_count, false);
17888 init_const_slice(ira->codegen, fields[2], union_field_array, 0, union_field_count, false, nullptr);
1788517889
1788617890 for (uint32_t union_field_index = 0; union_field_index < union_field_count; union_field_index++) {
1788717891 TypeUnionField *union_field = &type_entry->data.unionation.fields[union_field_index];
......@@ -17902,7 +17906,7 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy
1790217906 bigint_init_unsigned(&inner_fields[2]->data.x_bigint, union_field->align);
1790317907
1790417908 ZigValue *name = create_const_str_lit(ira->codegen, union_field->name)->data.x_ptr.data.ref.pointee;
17905 init_const_slice(ira->codegen, inner_fields[0], name, 0, buf_len(union_field->name), true);
17909 init_const_slice(ira->codegen, inner_fields[0], name, 0, buf_len(union_field->name), true, nullptr);
1790617910
1790717911 union_field_val->data.x_struct.fields = inner_fields;
1790817912 union_field_val->parent.id = ConstParentIdArray;
......@@ -17958,7 +17962,7 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy
1795817962 struct_field_array->data.x_array.special = ConstArraySpecialNone;
1795917963 struct_field_array->data.x_array.data.s_none.elements = ira->codegen->pass1_arena->allocate<ZigValue>(struct_field_count);
1796017964
17961 init_const_slice(ira->codegen, fields[1], struct_field_array, 0, struct_field_count, false);
17965 init_const_slice(ira->codegen, fields[1], struct_field_array, 0, struct_field_count, false, nullptr);
1796217966
1796317967 for (uint32_t struct_field_index = 0; struct_field_index < struct_field_count; struct_field_index++) {
1796417968 TypeStructField *struct_field = type_entry->data.structure.fields[struct_field_index];
......@@ -17994,7 +17998,7 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy
1799417998 bigint_init_unsigned(&inner_fields[4]->data.x_bigint, struct_field->align);
1799517999
1799618000 ZigValue *name = create_const_str_lit(ira->codegen, struct_field->name)->data.x_ptr.data.ref.pointee;
17997 init_const_slice(ira->codegen, inner_fields[0], name, 0, buf_len(struct_field->name), true);
18001 init_const_slice(ira->codegen, inner_fields[0], name, 0, buf_len(struct_field->name), true, nullptr);
1799818002
1799918003 struct_field_val->data.x_struct.fields = inner_fields;
1800018004 struct_field_val->parent.id = ConstParentIdArray;
......@@ -18074,7 +18078,7 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy
1807418078 fn_arg_array->data.x_array.special = ConstArraySpecialNone;
1807518079 fn_arg_array->data.x_array.data.s_none.elements = ira->codegen->pass1_arena->allocate<ZigValue>(fn_arg_count);
1807618080
18077 init_const_slice(ira->codegen, fields[5], fn_arg_array, 0, fn_arg_count, false);
18081 init_const_slice(ira->codegen, fields[5], fn_arg_array, 0, fn_arg_count, false, nullptr);
1807818082
1807918083 for (size_t fn_arg_index = 0; fn_arg_index < fn_arg_count; fn_arg_index++) {
1808018084 FnTypeParamInfo *fn_param_info = &type_entry->data.fn.fn_type_id.param_info[fn_arg_index];
......@@ -24150,7 +24154,7 @@ static IrInstGen *ir_analyze_instruction_src(IrAnalyze *ira, IrInstSrcSrc *instr
2415024154 RootStruct *root_struct = import->data.structure.root_struct;
2415124155 Buf *path = root_struct->path;
2415224156 ZigValue *file_name = create_const_str_lit(ira->codegen, path)->data.x_ptr.data.ref.pointee;
24153 init_const_slice(ira->codegen, fields[0], file_name, 0, buf_len(path), true);
24157 init_const_slice(ira->codegen, fields[0], file_name, 0, buf_len(path), true, nullptr);
2415424158 fields[0]->type = u8_slice;
2415524159
2415624160 // fn_name: [:0]const u8
......@@ -24158,7 +24162,7 @@ static IrInstGen *ir_analyze_instruction_src(IrAnalyze *ira, IrInstSrcSrc *instr
2415824162 fields[1]->special = ConstValSpecialStatic;
2415924163
2416024164 ZigValue *fn_name = create_const_str_lit(ira->codegen, &fn_entry->symbol_name)->data.x_ptr.data.ref.pointee;
24161 init_const_slice(ira->codegen, fields[1], fn_name, 0, buf_len(&fn_entry->symbol_name), true);
24165 init_const_slice(ira->codegen, fields[1], fn_name, 0, buf_len(&fn_entry->symbol_name), true, nullptr);
2416224166 fields[1]->type = u8_slice;
2416324167
2416424168
test/behavior.zig+1
......@@ -51,6 +51,7 @@ comptime {
5151 _ = @import("behavior/bugs/3384.zig");
5252 _ = @import("behavior/bugs/3586.zig");
5353 _ = @import("behavior/bugs/3742.zig");
54 _ = @import("behavior/bugs/3779.zig");
5455 _ = @import("behavior/bugs/4328.zig");
5556 _ = @import("behavior/bugs/4560.zig");
5657 _ = @import("behavior/bugs/4769_a.zig");
test/behavior/bugs/3779.zig created+42
......@@ -0,0 +1,42 @@
1const std = @import("std");
2
3const TestEnum = enum { TestEnumValue };
4const tag_name = @tagName(TestEnum.TestEnumValue);
5const ptr_tag_name: [*:0]const u8 = tag_name;
6
7test "@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}
12
13const TestError = error{TestErrorCode};
14const error_name = @errorName(TestError.TestErrorCode);
15const ptr_error_name: [*:0]const u8 = error_name;
16
17test "@errorName() returns a string literal" {
18 try std.testing.expectEqual([:0]const u8, @TypeOf(error_name));
19 try std.testing.expectEqualStrings("TestErrorCode", error_name);
20 try std.testing.expectEqualStrings("TestErrorCode", ptr_error_name[0..error_name.len]);
21}
22
23const TestType = struct {};
24const type_name = @typeName(TestType);
25const ptr_type_name: [*:0]const u8 = type_name;
26
27test "@typeName() returns a string literal" {
28 try std.testing.expectEqual(*const [type_name.len:0]u8, @TypeOf(type_name));
29 try std.testing.expectEqualStrings("TestType", type_name);
30 try std.testing.expectEqualStrings("TestType", ptr_type_name[0..type_name.len]);
31}
32
33const actual_contents = @embedFile("3779_file_to_embed.txt");
34const ptr_actual_contents: [*:0]const u8 = actual_contents;
35const expected_contents = "hello zig\n";
36
37test "@embedFile() returns a string literal" {
38 try std.testing.expectEqual(*const [expected_contents.len:0]u8, @TypeOf(actual_contents));
39 try std.testing.expect(std.mem.eql(u8, expected_contents, actual_contents));
40 try std.testing.expectEqualStrings(expected_contents, actual_contents);
41 try std.testing.expectEqualStrings(expected_contents, ptr_actual_contents[0..actual_contents.len]);
42}
test/behavior/bugs/3779_file_to_embed.txt created+1
......@@ -0,0 +1 @@
1hello zig