Commit 84704ef43e

Andrew Kelley <andrew@ziglang.org>
2021-12-04 00:50:20
stage1: LLVM code for `@tagName` not emitting null byte
Thanks LemonBoy for the patch.
1 parent 77fc909
Changed files (2)
src
test
src/stage1/codegen.cpp
@@ -5402,8 +5402,9 @@ static LLVMValueRef get_enum_tag_name_function(CodeGen *g, ZigType *enum_type) {
     if (enum_type->data.enumeration.name_function)
         return enum_type->data.enumeration.name_function;
 
-    ZigType *u8_ptr_type = get_pointer_to_type_extra(g, g->builtin_types.entry_u8, false, false,
-            PtrLenUnknown, get_abi_alignment(g, g->builtin_types.entry_u8), 0, 0, false);
+    ZigType *u8_ptr_type = get_pointer_to_type_extra2(g, g->builtin_types.entry_u8, false, false,
+            PtrLenUnknown, get_abi_alignment(g, g->builtin_types.entry_u8), 0, 0, false,
+            VECTOR_INDEX_NONE, nullptr, g->intern.for_zero_byte());
     ZigType *u8_slice_type = get_slice_type(g, u8_ptr_type);
     ZigType *tag_int_type = enum_type->data.enumeration.tag_int_type;
 
@@ -5456,7 +5457,7 @@ static LLVMValueRef get_enum_tag_name_function(CodeGen *g, ZigType *enum_type) {
             continue;
         }
 
-        LLVMValueRef str_init = LLVMConstString(buf_ptr(name), (unsigned)buf_len(name), true);
+        LLVMValueRef str_init = LLVMConstString(buf_ptr(name), (unsigned)buf_len(name), false);
         LLVMValueRef str_global = LLVMAddGlobal(g->module, LLVMTypeOf(str_init), "");
         LLVMSetInitializer(str_global, str_init);
         LLVMSetLinkage(str_global, LLVMPrivateLinkage);
test/behavior/enum_stage1.zig
@@ -114,6 +114,16 @@ test "@tagName non-exhaustive enum" {
     comptime try expect(mem.eql(u8, testEnumTagNameBare(NonExhaustive.B), "B"));
 }
 
+test "@tagName is null-terminated" {
+    const S = struct {
+        fn doTheTest(n: BareNumber) !void {
+            try expect(@tagName(n)[3] == 0);
+        }
+    };
+    try S.doTheTest(.Two);
+    try comptime S.doTheTest(.Two);
+}
+
 fn testEnumTagNameBare(n: anytype) []const u8 {
     return @tagName(n);
 }