zig icon indicating copy to clipboard operation
zig copied to clipboard

comptime prefix being treated as comptime block causing inconsistent treatment of unreachable code formulation

Open InKryption opened this issue 1 year ago • 1 comments

Zig Version

0.14.0-dev.224+95d9292a7

Steps to Reproduce and Observed Behavior

Consider this code, which yields an unreachable code error:

@compileError("foo");
return;

and then consider this extremely similar, but subtly different, non-erroring code:

comptime @compileError("foo");
return;

it would appear that the ast-check responsible for unreachable control flow treats comptime expr; expr; the same as { expr; } expr;.

While this almost makes sense if you squint your brain, it's very unintuitive and was directly described as a bug by @mlugg, who requested this be made into a bug report.

Expected Behavior

Both snippets should error.

InKryption avatar Jul 20 '24 20:07 InKryption

My interpretation of this bug is that a simple block shouldn't elide the "unreachable code" error. i.e. { @compileError("foo"); }; return; should also be an error. It seems to me that this is the intended behavior based upon the fact that we use e.g.

if (true) {
    return error.SkipZigTest;
}

over

{
    return error.SkipZigTest;
}

An unlabeled block whose end is guaranteed to be unreachable should propagate this "unreachable code" information to the parent scope.

mlugg avatar Jul 20 '24 21:07 mlugg