Skip to content

Commit d674e2f

Browse files
author
vole-dev
committed
make tagName that is null-terminated even with the stage1 compiler
1 parent 6cc6569 commit d674e2f

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

lib/std/target.zig

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -699,10 +699,27 @@ pub const Target = struct {
699699
.spirv64 => .spirv64,
700700
};
701701
}
702+
703+
// re-implementing @tagName because @tagName is not null-terminated in the stage1 compiler
704+
fn tagName(value: anytype) [:0]const u8 {
705+
const T = @TypeOf(value);
706+
const info = @typeInfo(T);
707+
708+
switch (info) {
709+
.Enum => |enum_info| {
710+
comptime var names: [enum_info.fields.len][:0]const u8 = undefined;
711+
inline for (enum_info.fields) |field, i| {
712+
names[i] = field.name ++ [0:0]u8{};
713+
}
714+
return names[@enumToInt(value)];
715+
},
716+
else => @compileError("tagName does no support type " ++ @typeName(T) ++ "."),
717+
}
718+
}
702719
pub fn string(self: TargetAbi) ?[:0]const u8 {
703720
switch (self) {
704-
.riscv32 => |abi| return @tagName(abi),
705-
.riscv64 => |abi| return @tagName(abi),
721+
.riscv32 => |abi| return tagName(abi),
722+
.riscv64 => |abi| return tagName(abi),
706723
else => return null,
707724
}
708725
}

0 commit comments

Comments
 (0)