diff --git a/doc/langref.html.in b/doc/langref.html.in index 4962183a77f2..41cd77e2c9d9 100644 --- a/doc/langref.html.in +++ b/doc/langref.html.in @@ -2986,7 +2986,7 @@ test "@typeInfo" { try expect(mem.eql(u8, @typeInfo(Small).Enum.fields[1].name, "two")); } -// @tagName gives a []const u8 representation of an enum value: +// @tagName gives a [:0]const u8 representation of an enum value: test "@tagName" { try expect(mem.eql(u8, @tagName(Small.three), "three")); } @@ -3233,7 +3233,7 @@ test "union method" { {#code_end#}

{#link|@tagName#} can be used to return a {#link|comptime#} - {#syntax#}[]const u8{#endsyntax#} value representing the field name: + {#syntax#}[:0]const u8{#endsyntax#} value representing the field name:

{#code_begin|test#} const std = @import("std"); @@ -7447,7 +7447,7 @@ test "main" { {#see_also|@divFloor|@divExact#} {#header_close#} {#header_open|@embedFile#} -
{#syntax#}@embedFile(comptime path: []const u8) *const [X:0]u8{#endsyntax#}
+
{#syntax#}@embedFile(comptime path: []const u8) *const [N:0]u8{#endsyntax#}

This function returns a compile time constant pointer to null-terminated, fixed-size array with length equal to the byte count of the file given by @@ -7475,7 +7475,7 @@ test "main" { {#header_close#} {#header_open|@errorName#} -

{#syntax#}@errorName(err: anyerror) []const u8{#endsyntax#}
+
{#syntax#}@errorName(err: anyerror) [:0]const u8{#endsyntax#}

This function returns the string representation of an error. The string representation of {#syntax#}error.OutOfMem{#endsyntax#} is {#syntax#}"OutOfMem"{#endsyntax#}. @@ -8494,9 +8494,9 @@ fn doTheTest() !void { {#header_close#} {#header_open|@tagName#} -

{#syntax#}@tagName(value: anytype) []const u8{#endsyntax#}
+
{#syntax#}@tagName(value: anytype) [:0]const u8{#endsyntax#}

- Converts an enum value or union value to a slice of bytes representing the name.

If the enum is non-exhaustive and the tag value does not map to a name, it invokes safety-checked {#link|Undefined Behavior#}. + Converts an enum value or union value to a string literal representing the name.

If the enum is non-exhaustive and the tag value does not map to a name, it invokes safety-checked {#link|Undefined Behavior#}.

{#header_close#} @@ -8623,7 +8623,7 @@ test "integer truncation" { {#header_close#} {#header_open|@typeName#} -
{#syntax#}@typeName(T: type) [N]u8{#endsyntax#}
+
{#syntax#}@typeName(T: type) *const [N:0]u8{#endsyntax#}

This function returns the string representation of a type, as an array. It is equivalent to a string literal of the type name. diff --git a/src/stage1/analyze.cpp b/src/stage1/analyze.cpp index f83849d13ba8..3fb0cb55b7ff 100644 --- a/src/stage1/analyze.cpp +++ b/src/stage1/analyze.cpp @@ -6291,6 +6291,11 @@ ZigValue *create_const_str_lit(CodeGen *g, Buf *str) { return const_val; } +ZigValue *create_sentineled_str_lit(CodeGen *g, Buf *str, ZigValue *sentinel) { + ZigValue *array_val = create_const_str_lit(g, str)->data.x_ptr.data.ref.pointee; + return create_const_slice(g, array_val, 0, buf_len(str), true, sentinel); +} + void init_const_bigint(ZigValue *const_val, ZigType *type, const BigInt *bigint) { const_val->special = ConstValSpecialStatic; const_val->type = type; @@ -6444,12 +6449,12 @@ ZigValue *create_const_type(CodeGen *g, ZigType *type_value) { } void init_const_slice(CodeGen *g, ZigValue *const_val, ZigValue *array_val, - size_t start, size_t len, bool is_const) + size_t start, size_t len, bool is_const, ZigValue *sentinel) { assert(array_val->type->id == ZigTypeIdArray); - ZigType *ptr_type = get_pointer_to_type_extra(g, array_val->type->data.array.child_type, - is_const, false, PtrLenUnknown, 0, 0, 0, false); + ZigType *ptr_type = get_pointer_to_type_extra2(g, array_val->type->data.array.child_type, + is_const, false, PtrLenUnknown, 0, 0, 0, false, VECTOR_INDEX_NONE, nullptr, sentinel); const_val->special = ConstValSpecialStatic; 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, init_const_usize(g, const_val->data.x_struct.fields[slice_len_index], len); } -ZigValue *create_const_slice(CodeGen *g, ZigValue *array_val, size_t start, size_t len, bool is_const) { +ZigValue *create_const_slice(CodeGen *g, ZigValue *array_val, size_t start, size_t len, bool is_const, ZigValue *sentinel) { ZigValue *const_val = g->pass1_arena->create(); - init_const_slice(g, const_val, array_val, start, len, is_const); + init_const_slice(g, const_val, array_val, start, len, is_const, sentinel); return const_val; } diff --git a/src/stage1/analyze.hpp b/src/stage1/analyze.hpp index fee83ab7d009..5c232bf4b6ad 100644 --- a/src/stage1/analyze.hpp +++ b/src/stage1/analyze.hpp @@ -144,6 +144,7 @@ ScopeExpr *create_expr_scope(CodeGen *g, AstNode *node, Scope *parent); void init_const_str_lit(CodeGen *g, ZigValue *const_val, Buf *str, bool move_str); ZigValue *create_const_str_lit(CodeGen *g, Buf *str); +ZigValue *create_sentineled_str_lit(CodeGen *g, Buf *str, ZigValue *sentinel); void init_const_bigint(ZigValue *const_val, ZigType *type, const BigInt *bigint); ZigValue *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 bool is_const, PtrLen ptr_len); void init_const_slice(CodeGen *g, ZigValue *const_val, ZigValue *array_val, - size_t start, size_t len, bool is_const); -ZigValue *create_const_slice(CodeGen *g, ZigValue *array_val, size_t start, size_t len, bool is_const); + size_t start, size_t len, bool is_const, ZigValue *sentinel); +ZigValue *create_const_slice(CodeGen *g, ZigValue *array_val, size_t start, size_t len, bool is_const, ZigValue *sentinel); void init_const_null(ZigValue *const_val, ZigType *type); ZigValue *create_const_null(CodeGen *g, ZigType *type); diff --git a/src/stage1/codegen.cpp b/src/stage1/codegen.cpp index d0ec853f0601..84533be4e616 100644 --- a/src/stage1/codegen.cpp +++ b/src/stage1/codegen.cpp @@ -1022,7 +1022,7 @@ static LLVMValueRef get_panic_msg_ptr_val(CodeGen *g, PanicMsgId msg_id) { Buf *buf_msg = panic_msg_buf(msg_id); ZigValue *array_val = create_const_str_lit(g, buf_msg)->data.x_ptr.data.ref.pointee; - init_const_slice(g, val, array_val, 0, buf_len(buf_msg), true); + init_const_slice(g, val, array_val, 0, buf_len(buf_msg), true, nullptr); render_const_val(g, val, ""); render_const_val_global(g, val, ""); @@ -9424,7 +9424,7 @@ static void update_test_functions_builtin_decl(CodeGen *g) { ZigValue *name_field = this_val->data.x_struct.fields[0]; ZigValue *name_array_val = create_const_str_lit(g, &test_fn_entry->symbol_name)->data.x_ptr.data.ref.pointee; - init_const_slice(g, name_field, name_array_val, 0, buf_len(&test_fn_entry->symbol_name), true); + init_const_slice(g, name_field, name_array_val, 0, buf_len(&test_fn_entry->symbol_name), true, nullptr); ZigValue *fn_field = this_val->data.x_struct.fields[1]; fn_field->type = fn_type; @@ -9448,7 +9448,7 @@ static void update_test_functions_builtin_decl(CodeGen *g) { } report_errors_and_maybe_exit(g); - ZigValue *test_fn_slice = create_const_slice(g, test_fn_array, 0, g->test_fns.length, true); + ZigValue *test_fn_slice = create_const_slice(g, test_fn_array, 0, g->test_fns.length, true, nullptr); update_compile_var(g, buf_create_from_str("test_functions"), test_fn_slice); assert(g->test_runner_package != nullptr); diff --git a/src/stage1/ir.cpp b/src/stage1/ir.cpp index 053eb87e6229..2200e8380d52 100644 --- a/src/stage1/ir.cpp +++ b/src/stage1/ir.cpp @@ -5190,7 +5190,7 @@ static IrInstGen *ir_resolve_ptr_of_array_to_slice(IrAnalyze *ira, IrInst* sourc // undef_array->type = array_type; // IrInstGen *result = ir_const(ira, source_instr, wanted_type); - // init_const_slice(ira->codegen, result->value, undef_array, 0, 0, false); + // init_const_slice(ira->codegen, result->value, undef_array, 0, 0, false, nullptr); // result->value->data.x_struct.fields[slice_ptr_index]->data.x_ptr.mut = ConstPtrMutComptimeConst; // result->value->type = wanted_type; // return result; @@ -5217,7 +5217,7 @@ static IrInstGen *ir_resolve_ptr_of_array_to_slice(IrAnalyze *ira, IrInst* sourc undef_array->type = array_type; IrInstGen *result = ir_const(ira, source_instr, wanted_type); - init_const_slice(ira->codegen, result->value, undef_array, 0, 0, false); + init_const_slice(ira->codegen, result->value, undef_array, 0, 0, false, nullptr); result->value->data.x_struct.fields[slice_ptr_index]->data.x_ptr.mut = ConstPtrMutComptimeConst; result->value->type = wanted_type; return result; @@ -5230,7 +5230,7 @@ static IrInstGen *ir_resolve_ptr_of_array_to_slice(IrAnalyze *ira, IrInst* sourc IrInstGen *result = ir_const(ira, source_instr, wanted_type); init_const_slice(ira->codegen, result->value, array_val, array_ptr_val->data.x_ptr.data.base_array.elem_index, - array_type->data.array.len, wanted_const); + array_type->data.array.len, wanted_const, nullptr); result->value->data.x_struct.fields[slice_ptr_index]->data.x_ptr.mut = array_ptr_val->data.x_ptr.mut; result->value->type = wanted_type; return result; @@ -5243,7 +5243,7 @@ static IrInstGen *ir_resolve_ptr_of_array_to_slice(IrAnalyze *ira, IrInst* sourc assert(array_ptr_val->type->id == ZigTypeIdPointer); IrInstGen *result = ir_const(ira, source_instr, wanted_type); - init_const_slice(ira->codegen, result->value, pointee, 0, array_type->data.array.len, wanted_const); + init_const_slice(ira->codegen, result->value, pointee, 0, array_type->data.array.len, wanted_const, nullptr); result->value->data.x_struct.fields[slice_ptr_index]->data.x_ptr.mut = array_ptr_val->data.x_ptr.mut; result->value->type = wanted_type; return result; @@ -14449,7 +14449,7 @@ static IrInstGen *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstSrcElemP } init_const_slice(ira->codegen, array_ptr_val, array_init_val, 0, actual_array_type->data.array.len, - false); + false, nullptr); array_ptr_val->data.x_struct.fields[slice_ptr_index]->data.x_ptr.mut = ConstPtrMutInfer; } else { 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 if (type_is_invalid(casted_value->value->type)) return ira->codegen->invalid_inst_gen; - ZigType *u8_ptr_type = get_pointer_to_type_extra(ira->codegen, ira->codegen->builtin_types.entry_u8, - true, false, PtrLenUnknown, 0, 0, 0, false); - ZigType *str_type = get_slice_type(ira->codegen, u8_ptr_type); if (instr_is_comptime(casted_value)) { ZigValue *val = ir_resolve_const(ira, casted_value, UndefBad); if (val == nullptr) return ira->codegen->invalid_inst_gen; ErrorTableEntry *err = casted_value->value->data.x_err_set; if (!err->cached_error_name_val) { - ZigValue *array_val = create_const_str_lit(ira->codegen, &err->name)->data.x_ptr.data.ref.pointee; - err->cached_error_name_val = create_const_slice(ira->codegen, array_val, 0, buf_len(&err->name), true); + err->cached_error_name_val = create_sentineled_str_lit( + ira->codegen, &err->name, + ira->codegen->intern.for_zero_byte()); } IrInstGen *result = ir_const(ira, &instruction->base.base, nullptr); - copy_const_val(ira->codegen, result->value, err->cached_error_name_val); - result->value->type = str_type; + result->value = err->cached_error_name_val; return result; } ira->codegen->generate_error_name_table = true; + ZigType *u8_ptr_type = get_pointer_to_type_extra2(ira->codegen, ira->codegen->builtin_types.entry_u8, + true, false, PtrLenUnknown, 0, 0, 0, false, + VECTOR_INDEX_NONE, nullptr, ira->codegen->intern.for_zero_byte()); + ZigType *str_type = get_slice_type(ira->codegen, u8_ptr_type); return ir_build_err_name_gen(ira, &instruction->base.base, value, str_type); } @@ -16898,8 +16899,9 @@ static IrInstGen *ir_analyze_instruction_enum_tag_name(IrAnalyze *ira, IrInstSrc if (target_type->id == ZigTypeIdEnumLiteral) { IrInstGen *result = ir_const(ira, &instruction->base.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); + result->value = create_sentineled_str_lit( + ira->codegen, field_name, + ira->codegen->intern.for_zero_byte()); return result; } @@ -16918,9 +16920,10 @@ static IrInstGen *ir_analyze_instruction_enum_tag_name(IrAnalyze *ira, IrInstSrc if (can_fold_enum_type(target_type)) { TypeEnumField *only_field = &target_type->data.enumeration.fields[0]; - ZigValue *array_val = create_const_str_lit(ira->codegen, only_field->name)->data.x_ptr.data.ref.pointee; IrInstGen *result = ir_const(ira, &instruction->base.base, nullptr); - init_const_slice(ira->codegen, result->value, array_val, 0, buf_len(only_field->name), true); + result->value = create_sentineled_str_lit( + ira->codegen, only_field->name, + ira->codegen->intern.for_zero_byte()); return result; } @@ -16936,16 +16939,17 @@ static IrInstGen *ir_analyze_instruction_enum_tag_name(IrAnalyze *ira, IrInstSrc buf_sprintf("no tag by value %s", buf_ptr(int_buf))); return ira->codegen->invalid_inst_gen; } - ZigValue *array_val = create_const_str_lit(ira->codegen, field->name)->data.x_ptr.data.ref.pointee; IrInstGen *result = ir_const(ira, &instruction->base.base, nullptr); - init_const_slice(ira->codegen, result->value, array_val, 0, buf_len(field->name), true); + result->value = create_sentineled_str_lit( + ira->codegen, field->name, + ira->codegen->intern.for_zero_byte()); return result; } - ZigType *u8_ptr_type = get_pointer_to_type_extra( + ZigType *u8_ptr_type = get_pointer_to_type_extra2( ira->codegen, ira->codegen->builtin_types.entry_u8, - true, false, PtrLenUnknown, - 0, 0, 0, false); + true, false, PtrLenUnknown, 0, 0, 0, false, + VECTOR_INDEX_NONE, nullptr, ira->codegen->intern.for_zero_byte()); ZigType *result_type = get_slice_type(ira->codegen, u8_ptr_type); return ir_build_tag_name_gen(ira, &instruction->base.base, target, result_type); } @@ -17249,7 +17253,7 @@ static Error ir_make_type_info_decls(IrAnalyze *ira, IrInst* source_instr, ZigVa declaration_array->type = get_array_type(ira->codegen, type_info_declaration_type, declaration_count, nullptr); declaration_array->data.x_array.special = ConstArraySpecialNone; declaration_array->data.x_array.data.s_none.elements = ira->codegen->pass1_arena->allocate(declaration_count); - init_const_slice(ira->codegen, out_val, declaration_array, 0, declaration_count, false); + init_const_slice(ira->codegen, out_val, declaration_array, 0, declaration_count, false, nullptr); // Loop through the declarations and generate info. 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 ZigValue **inner_fields = alloc_const_vals_ptrs(ira->codegen, 3); ZigValue *name = create_const_str_lit(ira->codegen, curr_entry->key)->data.x_ptr.data.ref.pointee; - init_const_slice(ira->codegen, inner_fields[0], name, 0, buf_len(curr_entry->key), true); + init_const_slice(ira->codegen, inner_fields[0], name, 0, buf_len(curr_entry->key), true, nullptr); inner_fields[1]->special = ConstValSpecialStatic; inner_fields[1]->type = ira->codegen->builtin_types.entry_bool; 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 if (fn_node->is_extern && fn_node->lib_name != nullptr && buf_len(fn_node->lib_name) > 0) { ZigValue *slice_val = ira->codegen->pass1_arena->create(); ZigValue *lib_name = create_const_str_lit(ira->codegen, fn_node->lib_name)->data.x_ptr.data.ref.pointee; - init_const_slice(ira->codegen, slice_val, lib_name, 0, buf_len(fn_node->lib_name), true); + init_const_slice(ira->codegen, slice_val, lib_name, 0, buf_len(fn_node->lib_name), true, nullptr); set_optional_payload(fn_decl_fields[5], slice_val); } else { 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 fn_arg_name_array->data.x_array.special = ConstArraySpecialNone; fn_arg_name_array->data.x_array.data.s_none.elements = ira->codegen->pass1_arena->allocate(fn_arg_count); - init_const_slice(ira->codegen, fn_decl_fields[7], fn_arg_name_array, 0, fn_arg_count, false); + init_const_slice(ira->codegen, fn_decl_fields[7], fn_arg_name_array, 0, fn_arg_count, false, nullptr); for (size_t fn_arg_index = 0; fn_arg_index < fn_arg_count; fn_arg_index++) { ZigVar *arg_var = fn_entry->variable_list.at(fn_arg_index); ZigValue *fn_arg_name_val = &fn_arg_name_array->data.x_array.data.s_none.elements[fn_arg_index]; ZigValue *arg_name = create_const_str_lit(ira->codegen, buf_create_from_str(arg_var->name))->data.x_ptr.data.ref.pointee; - init_const_slice(ira->codegen, fn_arg_name_val, arg_name, 0, strlen(arg_var->name), true); + init_const_slice(ira->codegen, fn_arg_name_val, arg_name, 0, strlen(arg_var->name), true, nullptr); fn_arg_name_val->parent.id = ConstParentIdArray; fn_arg_name_val->parent.data.p_array.array_val = fn_arg_name_array; 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 inner_fields[1]->type = ira->codegen->builtin_types.entry_num_lit_int; ZigValue *name = create_const_str_lit(ira->codegen, enum_field->name)->data.x_ptr.data.ref.pointee; - init_const_slice(ira->codegen, inner_fields[0], name, 0, buf_len(enum_field->name), true); + init_const_slice(ira->codegen, inner_fields[0], name, 0, buf_len(enum_field->name), true, nullptr); bigint_init_bigint(&inner_fields[1]->data.x_bigint, &enum_field->value); @@ -17732,7 +17736,7 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy enum_field_array->data.x_array.special = ConstArraySpecialNone; enum_field_array->data.x_array.data.s_none.elements = ira->codegen->pass1_arena->allocate(enum_field_count); - init_const_slice(ira->codegen, fields[2], enum_field_array, 0, enum_field_count, false); + init_const_slice(ira->codegen, fields[2], enum_field_array, 0, enum_field_count, false, nullptr); for (uint32_t enum_field_index = 0; enum_field_index < enum_field_count; enum_field_index++) { @@ -17785,7 +17789,7 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy error_array->data.x_array.special = ConstArraySpecialNone; error_array->data.x_array.data.s_none.elements = ira->codegen->pass1_arena->allocate(error_count); - init_const_slice(ira->codegen, slice_val, error_array, 0, error_count, false); + init_const_slice(ira->codegen, slice_val, error_array, 0, error_count, false, nullptr); for (uint32_t error_index = 0; error_index < error_count; error_index++) { ErrorTableEntry *error = type_entry->data.error_set.errors[error_index]; 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 name = error->cached_error_name_val; if (name == nullptr) name = create_const_str_lit(ira->codegen, &error->name)->data.x_ptr.data.ref.pointee; - init_const_slice(ira->codegen, inner_fields[0], name, 0, buf_len(&error->name), true); + init_const_slice(ira->codegen, inner_fields[0], name, 0, buf_len(&error->name), true, nullptr); error_val->data.x_struct.fields = inner_fields; error_val->parent.id = ConstParentIdArray; @@ -17881,7 +17885,7 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy union_field_array->data.x_array.special = ConstArraySpecialNone; union_field_array->data.x_array.data.s_none.elements = ira->codegen->pass1_arena->allocate(union_field_count); - init_const_slice(ira->codegen, fields[2], union_field_array, 0, union_field_count, false); + init_const_slice(ira->codegen, fields[2], union_field_array, 0, union_field_count, false, nullptr); for (uint32_t union_field_index = 0; union_field_index < union_field_count; union_field_index++) { 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 bigint_init_unsigned(&inner_fields[2]->data.x_bigint, union_field->align); ZigValue *name = create_const_str_lit(ira->codegen, union_field->name)->data.x_ptr.data.ref.pointee; - init_const_slice(ira->codegen, inner_fields[0], name, 0, buf_len(union_field->name), true); + init_const_slice(ira->codegen, inner_fields[0], name, 0, buf_len(union_field->name), true, nullptr); union_field_val->data.x_struct.fields = inner_fields; union_field_val->parent.id = ConstParentIdArray; @@ -17958,7 +17962,7 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy struct_field_array->data.x_array.special = ConstArraySpecialNone; struct_field_array->data.x_array.data.s_none.elements = ira->codegen->pass1_arena->allocate(struct_field_count); - init_const_slice(ira->codegen, fields[1], struct_field_array, 0, struct_field_count, false); + init_const_slice(ira->codegen, fields[1], struct_field_array, 0, struct_field_count, false, nullptr); for (uint32_t struct_field_index = 0; struct_field_index < struct_field_count; struct_field_index++) { 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 bigint_init_unsigned(&inner_fields[4]->data.x_bigint, struct_field->align); ZigValue *name = create_const_str_lit(ira->codegen, struct_field->name)->data.x_ptr.data.ref.pointee; - init_const_slice(ira->codegen, inner_fields[0], name, 0, buf_len(struct_field->name), true); + init_const_slice(ira->codegen, inner_fields[0], name, 0, buf_len(struct_field->name), true, nullptr); struct_field_val->data.x_struct.fields = inner_fields; struct_field_val->parent.id = ConstParentIdArray; @@ -18074,7 +18078,7 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy fn_arg_array->data.x_array.special = ConstArraySpecialNone; fn_arg_array->data.x_array.data.s_none.elements = ira->codegen->pass1_arena->allocate(fn_arg_count); - init_const_slice(ira->codegen, fields[5], fn_arg_array, 0, fn_arg_count, false); + init_const_slice(ira->codegen, fields[5], fn_arg_array, 0, fn_arg_count, false, nullptr); for (size_t fn_arg_index = 0; fn_arg_index < fn_arg_count; fn_arg_index++) { 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 RootStruct *root_struct = import->data.structure.root_struct; Buf *path = root_struct->path; ZigValue *file_name = create_const_str_lit(ira->codegen, path)->data.x_ptr.data.ref.pointee; - init_const_slice(ira->codegen, fields[0], file_name, 0, buf_len(path), true); + init_const_slice(ira->codegen, fields[0], file_name, 0, buf_len(path), true, nullptr); fields[0]->type = u8_slice; // fn_name: [:0]const u8 @@ -24158,7 +24162,7 @@ static IrInstGen *ir_analyze_instruction_src(IrAnalyze *ira, IrInstSrcSrc *instr fields[1]->special = ConstValSpecialStatic; ZigValue *fn_name = create_const_str_lit(ira->codegen, &fn_entry->symbol_name)->data.x_ptr.data.ref.pointee; - init_const_slice(ira->codegen, fields[1], fn_name, 0, buf_len(&fn_entry->symbol_name), true); + init_const_slice(ira->codegen, fields[1], fn_name, 0, buf_len(&fn_entry->symbol_name), true, nullptr); fields[1]->type = u8_slice; diff --git a/test/behavior.zig b/test/behavior.zig index 479fe9b4222c..56c1b8ec0518 100644 --- a/test/behavior.zig +++ b/test/behavior.zig @@ -51,6 +51,7 @@ comptime { _ = @import("behavior/bugs/3384.zig"); _ = @import("behavior/bugs/3586.zig"); _ = @import("behavior/bugs/3742.zig"); + _ = @import("behavior/bugs/3779.zig"); _ = @import("behavior/bugs/4328.zig"); _ = @import("behavior/bugs/4560.zig"); _ = @import("behavior/bugs/4769_a.zig"); diff --git a/test/behavior/bugs/3779.zig b/test/behavior/bugs/3779.zig new file mode 100644 index 000000000000..1bf5bfcc9079 --- /dev/null +++ b/test/behavior/bugs/3779.zig @@ -0,0 +1,42 @@ +const std = @import("std"); + +const TestEnum = enum { TestEnumValue }; +const tag_name = @tagName(TestEnum.TestEnumValue); +const ptr_tag_name: [*:0]const u8 = tag_name; + +test "@tagName() returns a string literal" { + try std.testing.expectEqual([:0]const u8, @TypeOf(tag_name)); + try std.testing.expectEqualStrings("TestEnumValue", tag_name); + try std.testing.expectEqualStrings("TestEnumValue", ptr_tag_name[0..tag_name.len]); +} + +const TestError = error{TestErrorCode}; +const error_name = @errorName(TestError.TestErrorCode); +const ptr_error_name: [*:0]const u8 = error_name; + +test "@errorName() returns a string literal" { + try std.testing.expectEqual([:0]const u8, @TypeOf(error_name)); + try std.testing.expectEqualStrings("TestErrorCode", error_name); + try std.testing.expectEqualStrings("TestErrorCode", ptr_error_name[0..error_name.len]); +} + +const TestType = struct {}; +const type_name = @typeName(TestType); +const ptr_type_name: [*:0]const u8 = type_name; + +test "@typeName() returns a string literal" { + try std.testing.expectEqual(*const [type_name.len:0]u8, @TypeOf(type_name)); + try std.testing.expectEqualStrings("TestType", type_name); + try std.testing.expectEqualStrings("TestType", ptr_type_name[0..type_name.len]); +} + +const actual_contents = @embedFile("3779_file_to_embed.txt"); +const ptr_actual_contents: [*:0]const u8 = actual_contents; +const expected_contents = "hello zig\n"; + +test "@embedFile() returns a string literal" { + try std.testing.expectEqual(*const [expected_contents.len:0]u8, @TypeOf(actual_contents)); + try std.testing.expect(std.mem.eql(u8, expected_contents, actual_contents)); + try std.testing.expectEqualStrings(expected_contents, actual_contents); + try std.testing.expectEqualStrings(expected_contents, ptr_actual_contents[0..actual_contents.len]); +} diff --git a/test/behavior/bugs/3779_file_to_embed.txt b/test/behavior/bugs/3779_file_to_embed.txt new file mode 100644 index 000000000000..0436280ed352 --- /dev/null +++ b/test/behavior/bugs/3779_file_to_embed.txt @@ -0,0 +1 @@ +hello zig