zig icon indicating copy to clipboard operation
zig copied to clipboard

Break without label inside switch behaves unexpectedly

Open Srekel opened this issue 3 years ago • 1 comments

Zig Version

0.11.0-dev.53+cbed6bb08

Steps to Reproduce and Observed Behavior

I made an image of my buggy code to explain it. This isn't a bug in Zig per se, but a footgun that could be avoided if breaks inside switches required a label.

Note that I don't think I wrote the non-labeled break intentionally, I guess I just wasn't quite thinking (my typical modus operandi). What I did want to do was to break with a TargetValue with value 0. I noticed the game input wasn't working correctly, so I went back to this code, and I saw this, and I was confused that it would even compile, and what it would mean to break without providing a value back out of the switch.

But then learned that the break actually breaks out of the for-loop above the switch.

image

Expected Behavior

I would expect a compile error. This feels like a rare enough thing that requiring a label + labeled break wouldn't be much of a burden, and it would have saved me some 20 minutes of confusion today.

Rare + unexpected are very typical footgunny traits :)

Srekel avatar Nov 04 '22 18:11 Srekel

This feels like an issue for a linter to catch but not the compiler. The code may be confusing, but not violating the definition of for/switch/block/break

Pyrolistical avatar Nov 05 '22 01:11 Pyrolistical

Can you make a more concrete proposal of what you think should be disallowed?

Vexu avatar Dec 05 '22 15:12 Vexu