Skip to content

Commit 54e7f58

Browse files
authored
Merge pull request ziglang#17438 from Luukdegram/issue-17436
wasm - fix `@tagName` for signed enums
2 parents 1de242c + 9402f8b commit 54e7f58

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/arch/wasm/CodeGen.zig

+2-2
Original file line numberDiff line numberDiff line change
@@ -7215,12 +7215,12 @@ fn getTagNameFunction(func: *CodeGen, enum_ty: Type) InnerError!u32 {
72157215
switch (tag_value) {
72167216
.imm32 => |value| {
72177217
try writer.writeByte(std.wasm.opcode(.i32_const));
7218-
try leb.writeULEB128(writer, value);
7218+
try leb.writeILEB128(writer, @as(i32, @bitCast(value)));
72197219
try writer.writeByte(std.wasm.opcode(.i32_ne));
72207220
},
72217221
.imm64 => |value| {
72227222
try writer.writeByte(std.wasm.opcode(.i64_const));
7223-
try leb.writeULEB128(writer, value);
7223+
try leb.writeILEB128(writer, @as(i64, @bitCast(value)));
72247224
try writer.writeByte(std.wasm.opcode(.i64_ne));
72257225
},
72267226
else => unreachable,

test/behavior/enum.zig

+16
Original file line numberDiff line numberDiff line change
@@ -1048,6 +1048,22 @@ test "@tagName on enum literals" {
10481048
try comptime expect(mem.eql(u8, @tagName(.FooBar), "FooBar"));
10491049
}
10501050

1051+
test "tag name with signed enum values" {
1052+
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
1053+
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
1054+
if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
1055+
if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
1056+
1057+
const LocalFoo = enum(isize) {
1058+
alfa = 62,
1059+
bravo = 63,
1060+
charlie = 64,
1061+
delta = 65,
1062+
};
1063+
var b = LocalFoo.bravo;
1064+
try expect(mem.eql(u8, @tagName(b), "bravo"));
1065+
}
1066+
10511067
test "enum literal casting to optional" {
10521068
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
10531069
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;

0 commit comments

Comments
 (0)