-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Allow any statement in loop bodies #5731
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
According to the grammar this is parsed as: while (it) |node| : (it = node.next) ((if (true) count) += 1); This could be fixed by making the loop statements accept any statement as their body like C does. |
That makes sense. I find the current behavior to be surprising in this case, though I'd assume there is a good reason this was the grammar chosen. |
This affects defer if (startup_process) |child| _ = child.kill() catch
|e| log.err(.server, "failed to terminate startup process: {}", .{e}); wrapping the body of the if in a block fixes things, but is needlessly verbose: defer if (startup_process) |child| {
_ = child.kill() catch
|e| log.err(.server, "failed to terminate startup process: {}", .{e});
}; |
test case (won't compile): test "double for on line" {
var n: usize = 0;
var in: []const []const u8 = &.{ "foo", "bar" };
for (in) |s| for (s) |c| n += c;
} |
I just ran into this with |
Same? fn foo() u32 {
var n:u32 = 0;
defer if (true) n = 8; // error: invalid left-hand side to assignment
return n;
} |
Uh oh!
There was an error while loading. Please reload this page.
The following snippet gives a compile error while I think it should be valid Zig:
Removing the
if (true)
or using a{}
block for theif
/while
makes the compile error go away.The text was updated successfully, but these errors were encountered: