llvm: fix jump table gen for labeled switch with single else prong

Avoids a null unwrap if there are no cases with explicit values present
while trying to construct a jump table for a labeled switch statement.
This commit is contained in:
Justus Klausecker 2025-12-30 23:47:46 +01:00 committed by Matthew Lugg
parent d840bb5118
commit 42dea36ce9
No known key found for this signature in database
GPG key ID: 3F5B7DCCBF4AF02E
2 changed files with 55 additions and 2 deletions

View file

@ -297,3 +297,52 @@ test "switch loop with discarded tag capture" {
S.doTheTest();
comptime S.doTheTest();
}
test "switch loop with single catch-all prong" {
if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest;
const S = struct {
const E = enum { a, b, c };
const U = union(E) { a: u32, b: u16, c: u8 };
fn doTheTest() !void {
var x: usize = 0;
label: switch (E.a) {
else => {
x += 1;
if (x >= 5) continue :label .b;
if (x == 10) break :label;
continue :label .c;
},
}
try expect(x == 10);
label: switch (E.a) {
.a, .b, .c => {
x += 1;
if (x >= 15) continue :label .b;
if (x == 20) break :label;
continue :label .c;
},
}
try expect(x == 20);
label: switch (E.a) {
else => if (false) continue :label true,
}
const ok = label: switch (U{ .a = 123 }) {
else => |u| {
const y: u32 = switch (u) {
inline else => |y| y,
};
if (y == 456) break :label true;
continue :label .{ .b = 456 };
},
};
try expect(ok);
}
};
try S.doTheTest();
try comptime S.doTheTest();
}