Break in switch prong causes compilation error
Zig Version
0.10.0-dev.2524+e9fc58eab
Steps to Reproduce
Build the stage2 compiler and compile the following test program:
const std = @import("std");
pub fn main() !void {
var x: u32 = 3;
const val: usize = while (true) switch (x) {
1 => break 2,
else => x -= 1,
} else unreachable;
std.debug.print("{}\n", .{val});
}
Expected Behavior
The program should successfully compile.
Actual Behavior
Compile error:
test.zig:6:19: error: expected usize, found void
else => x -= 1,
^
Note that this compiles successfully on stage1.
use this fix https://github.com/ziglang/zig/pull/12764 .
still get same sompile error, change source to:
const std = @import("std");
pub fn main() !void {
var x: u32 = 3;
const val: usize = while (true) {switch (x) {
1 => break 2,
else => x -= 1,
}};
std.debug.print("{}\n", .{val});
}
build is fine, and output '2'.
so i think origin case is not a bug?
The original example is correct and should work.
after https://github.com/ziglang/zig/pull/12764 , get another compile error
error: value with comptime only type 'comptime_int' depends on runtime control flow
const val: usize = while (true) switch (x) {
^~~~~
main.zig:26:24: note: runtime control flow here
seem like new bug?
delete 'else unreachable' will success.
@Vexu The code works with 0.12.0-dev.2341+92211135.
There is a test for this which doesn't have the else unreachable but I think it's good enough.
that's even better because while (true) should already infer noreturn