mirror of
https://github.com/ziglang/zig.git
synced 2024-11-15 00:26:57 +00:00
stage1: LLVM code for @tagName
not emitting null byte
Thanks LemonBoy for the patch.
This commit is contained in:
parent
77fc9090d5
commit
84704ef43e
@ -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);
|
||||
|
@ -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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user