authorgravatar for djpohly@gmail.comDevin J. Pohly <djpohly@gmail.com> 2026-06-07 03:03:17-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-06-10 20:52:33-07:00
log41d08843ffd59e0ae1396dbf958bdd11257f384a
tree43441731c1ad5a3718a5f6b725293a1e62090c03
parent3a8097ffd4a6370e8188767c6fff409e02186f72

x86_64 backend: fix signed enum switch

Once the switch condition is been shifted up so the minimum value is 0, it makes sense to treat its type as the unsigned version of its original type. Fixes #35651

2 files changed, 19 insertions(+), 1 deletions(-)

src/codegen/x86_64/CodeGen.zig+1-1
...@@ -176662,7 +176662,7 @@ fn lowerSwitchBr(...@@ -176662,7 +176662,7 @@ fn lowerSwitchBr(
176662 };176662 };
176663 const condition_index_lock = cg.register_manager.lockReg(condition_index_reg);176663 const condition_index_lock = cg.register_manager.lockReg(condition_index_reg);
176664 defer if (condition_index_lock) |lock| cg.register_manager.unlockReg(lock);176664 defer if (condition_index_lock) |lock| cg.register_manager.unlockReg(lock);
176665 try cg.truncateRegister(condition_ty, condition_index_reg);176665 try cg.truncateRegister(unsigned_condition_ty, condition_index_reg);
176666 const ptr_size = @divExact(cg.target.ptrBitWidth(), 8);176666 const ptr_size = @divExact(cg.target.ptrBitWidth(), 8);
176667 try cg.asmMemory(.{ ._mp, .j }, .{176667 try cg.asmMemory(.{ ._mp, .j }, .{
176668 .base = .table,176668 .base = .table,
test/behavior/enum.zig+18
...@@ -1313,6 +1313,24 @@ test "switch on an extern enum with negative value" {...@@ -1313,6 +1313,24 @@ test "switch on an extern enum with negative value" {
1313 }1313 }
1314}1314}
13151315
1316test "switch on an enum with small signed tag type" {
1317 const E = enum(i3) {
1318 y = -2,
1319 z = -1,
1320 a = 0,
1321 b = 1,
1322 c = 2,
1323 };
1324
1325 var runtime: E = .c;
1326 _ = &runtime;
1327 const result: u8 = switch (runtime) {
1328 .y, .z, .a, .b => 0,
1329 .c => 1,
1330 };
1331 try expect(result == 1);
1332}
1333
1316test "Non-exhaustive enum with nonstandard int size behaves correctly" {1334test "Non-exhaustive enum with nonstandard int size behaves correctly" {
1317 const E = enum(u15) { _ };1335 const E = enum(u15) { _ };
1318 try expect(@sizeOf(E) == @sizeOf(u15));1336 try expect(@sizeOf(E) == @sizeOf(u15));