I ran into this bit of footgun (?) today when writing a nested loop with a labeled break. When writing a loop there are two valid locations to write a label. One to label the loop, so that you can break out of nested loops (brk: for) and one where to identify where to ‘return’ values from when you break out of the loop (for (1..100) |i| brk:). Unfortunately there isn’t a compile error when you use the latter, but don’t assign the value to anything and the loop expression is void.
In short both of these compile, but only the first breaks the nested loop, whereas the second does not, and will only break the inner loop.
fn labeled_nested_loop() void {
var result: usize = 0;
brk: for(1..101) |i| {
for (1..10) |_| {
if (i == 10) {
break :brk;
}
result += i;
}
}
std.debug.print("labeled_nested_loop: {d}\n", .{result});
}
fn unassigned_labeled_break() void {
var result: usize = 0;
for(1..101) |i| brk: {
for (1..10) |_| {
if (i == 10) {
break :brk;
}
result += i;
}
}
std.debug.print("unassigned_labeled_break: {d}\n", .{result});
}
pub fn main() void {
labeled_nested_loop();
unassigned_labeled_break();
}
const std = @import("std");
tyler: test_zig $ zig run test.zig
labeled_nested_loop: 405
unassigned_labeld_break: 45360
In my opinion it should be an error to set up a labeled break from a loop and not have it assigned to anything. But maybe i’m overlooking some use-case where this is useful?
On a syntactic level, the second one is equivalent to
fn unassigned_labeled_break() void {
var result: usize = 0;
for(1..101) |i|
brk: for (1..10) |_| {
if (i == 10) {
break :brk;
}
result += i;
};
std.debug.print("unassined_labeld_break: {d}\n", .{result});
}
The main difference is that the version you wrote with curly braces allows the block to contain more than one statement. In other words, from a parsing perspective, allowing the second one is kind of a consequence of allowing the first one.
Your unassigned_labeled_break seems to be acting more like a continue. Basically it breaks the current iteration for that outer loop. So it is effectively a continue :blk but we don’t have that.
The naming choices hurt you here, I think. blk: would obviously be wrong for breaking from a loop (it’s not a block), and outer: would look wrong not being on a loop.
That said, I can’t think of any reason to have a labeled block as the expression of the loop body. Instead of using break you should use a (possibly labeled) continue on the loop itself. So yeah it wouldn’t be unreasonable to throw an ast level error here. A label as the first piece of a loop body is valid in general, though, provided it’s for an inner loop or a switch.
I’m confused by that statement. The question of assigning a result seems almost orthogonal to labelling the loop / block. Yes you need a label to use break to return a value, but I may just want funky control flow.
I think I’m understanding the flaw in my mental model. Because you can do things like this:
const y = if (x == 0) blk: {
// do a bunch of stuf
break :blk ret_val
} else other_val
I associated the blk: label with the if statement, rather than just labeling the block. That’s why when I wanted to do a nested break I ended up putting the label in the wrong spot, and ended up with a funky ‘labeled continue’ kind of control flow. I can see how that could be useful.
I definitely see how this is confusing at first glance, but once you understand the association of the label to the expression rather than the loop it makes sense in my opinion. Another reason to keep this is that it provides a means of continuing within inline for loops:
loop: inline for (0..10) |i| loopCont: {
if (some_condition) {
// This will continue to the next iteration of `loop`
break :loopCont;
}
// ...
}
Admittedly, continuing from such loops should just do this already, but this is a nice stop gap.