Skip to content

Commit 6de45c8

Browse files
authored
Merge pull request #8636 from jmc-88/issue-3779
Change builtins to return a string literal
2 parents 1071ca6 + 9e88356 commit 6de45c8

File tree

8 files changed

+108
-54
lines changed

8 files changed

+108
-54
lines changed

doc/langref.html.in

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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:
29902990
test "@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#}
32393239
const 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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6291,6 +6291,11 @@ ZigValue *create_const_str_lit(CodeGen *g, Buf *str) {
62916291
return const_val;
62926292
}
62936293

6294+
ZigValue *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+
62946299
void 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

64466451
void 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

6463-
ZigValue *create_const_slice(CodeGen *g, ZigValue *array_val, size_t start, size_t len, bool is_const) {
6468+
ZigValue *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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ ScopeExpr *create_expr_scope(CodeGen *g, AstNode *node, Scope *parent);
144144

145145
void init_const_str_lit(CodeGen *g, ZigValue *const_val, Buf *str, bool move_str);
146146
ZigValue *create_const_str_lit(CodeGen *g, Buf *str);
147+
ZigValue *create_sentineled_str_lit(CodeGen *g, Buf *str, ZigValue *sentinel);
147148

148149
void init_const_bigint(ZigValue *const_val, ZigType *type, const BigInt *bigint);
149150
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
186187
bool is_const, PtrLen ptr_len);
187188

188189
void init_const_slice(CodeGen *g, ZigValue *const_val, ZigValue *array_val,
189-
size_t start, size_t len, bool is_const);
190-
ZigValue *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);
191+
ZigValue *create_const_slice(CodeGen *g, ZigValue *array_val, size_t start, size_t len, bool is_const, ZigValue *sentinel);
191192

192193
void init_const_null(ZigValue *const_val, ZigType *type);
193194
ZigValue *create_const_null(CodeGen *g, ZigType *type);

src/stage1/codegen.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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);

0 commit comments

Comments
 (0)