zig icon indicating copy to clipboard operation
zig copied to clipboard

Break in switch prong causes compilation error

Open gpanders opened this issue 3 years ago • 3 comments

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.

gpanders avatar Jun 07 '22 18:06 gpanders

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?

kkHAIKE avatar Sep 08 '22 04:09 kkHAIKE

The original example is correct and should work.

Vexu avatar Sep 08 '22 09:09 Vexu

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.

kkHAIKE avatar Sep 10 '22 05:09 kkHAIKE

@Vexu The code works with 0.12.0-dev.2341+92211135.

perillo avatar Jan 30 '24 10:01 perillo

There is a test for this which doesn't have the else unreachable but I think it's good enough.

Vexu avatar Jan 30 '24 20:01 Vexu

that's even better because while (true) should already infer noreturn

nektro avatar Jan 30 '24 20:01 nektro