Skip to content

Labeled switch documentation - #21383

Merged
andrewrk merged 20 commits into
ziglang:masterfrom
LiterallyVoid:literallyvoid/21375-labeled-switch-langref
Sep 13, 2024
Merged

Labeled switch documentation#21383
andrewrk merged 20 commits into
ziglang:masterfrom
LiterallyVoid:literallyvoid/21375-labeled-switch-langref

Conversation

@LiterallyVoid

Copy link
Copy Markdown
Contributor

Feel free to nitpick wording and examples, especially mine---the good parts have been copied from @mlugg's release notes!

In particular, I found that you're able to break out of a switch with an unlabeled break. This was really surprising, and may be better disallowed than noted in the docs.

Closes#21375

This feature was proposed in ziglang#8220, and implemented in ziglang#21257.
After testing, `break` does not target an unlabeled `switch`. However,
it *can* target a labeled switch, even if the `break` is unlabeled. This
behavior seems surprising and worth a callout, although this may not be
the place for it.
This paragraph was copied from the release notes, written by @mlugg.
As before, text has been adapted from the release notes, which
were written by @mlugg.
@LiterallyVoid
LiterallyVoidforce-pushed the literallyvoid/21375-labeled-switch-langref branch from 9c02666 to 2216489CompareSeptember 11, 2024 06:12
Comment threaddoc/langref/test_switch_dispatch_loop.zig Outdated
Comment threaddoc/langref/test_switch_continue.zig Outdated
Comment threaddoc/langref.html.in
@mlugg

mlugg commented Sep 11, 2024

Copy link
Copy Markdown
Member

I found that you're able to break out of a switch with an unlabeled break.

Oh shit, that's definitely a bug, thank you for pointing it out. I'll get a fix up shortly.

EDIT: see #21385

I'm also moving away from "statement" here. Break is actually an
expression, and while it's almost impossible to observe this in code
that doesn't have any compiler errors, it can be shown:
test "a" {
_ = while (true) {
_ = break 42;
};
}
Nevertheless, `break` is still described as a statement in the Zig
docs. It may be that the Zig docs simply take statement to mean
something else---it says, "compile log statements", later in the
document.
I've tried to cram as many concepts in as few lines as possible.
I've also split the desugaring into its own test---the names here are
bad, and I'll improve them soon.
This comment was moved here from the other switch continue example, as
that one no longer shows switch having a result value.
@LiterallyVoid
LiterallyVoid marked this pull request as ready for review September 12, 2024 02:23

@andrewrkandrewrk left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I'll see if I can use github's UI to merge this along with my edit right now.

Comment threaddoc/langref.html.in Outdated
@andrewrk
andrewrk merged commit cf69154 into ziglang:masterSep 13, 2024
DivergentClouds pushed a commit to DivergentClouds/zig that referenced this pull request Sep 24, 2024
Add langref docs for labeled switch
This feature was proposed in ziglang#8220, and implemented in ziglang#21257.
Co-authored-by: Andrew Kelley <andrew@ziglang.org>
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

langref docs for labeled switch/continue

3 participants

@LiterallyVoid@mlugg@andrewrk