Skip to content

Enhance switch on non-exhaustive enums - #24381

Merged
mlugg merged 4 commits into
ziglang:masterfrom
Justus2308:switch-better-underscore
Aug 13, 2025
Merged

mlugg merged 4 commits into
ziglang:masterfrom
Justus2308:switch-better-underscore

Conversation

@Justus2308

@Justus2308 Justus2308 commented Jul 10, 2025 •

Copy link
Copy Markdown
Member

Closes #19438
Closes #12250

More details in commit messages
The commits are mostly separate but the second one is still based on the first one. If only one gets accepted I am happy to rebase accordingly.

@Justus2308
Justus2308 force-pushed the switch-better-underscore branch from c97a209 to 057d8f6 Compare July 10, 2025 00:20
@Justus2308

Copy link
Copy Markdown
Member Author

I will try to rebase on top of writergate and look into the CI failures until tomorrow.

@Justus2308
Justus2308 force-pushed the switch-better-underscore branch from 057d8f6 to 34abbdf Compare July 10, 2025 21:04
@alexrp
alexrp requested a review from mlugg August 6, 2025 18:49

@mlugg mlugg 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.

Amazing work wrangling Sema's switch logic; it's borderline impenetrable. Just one small nitpick about your behavior test; once you fix that and rebase, I'll be happy to hit merge.

By the way, as mentioned on another PR of yours, these lines in test cases are redundant:

// backend=stage2
// target=native

It's be good if you could drop them from your newly-added cases. Thanks!

Comment thread src/Zcu.zig Outdated
Comment thread src/Zcu.zig Outdated
Comment thread test/behavior/switch.zig Outdated
Mainly affects ZIR representation of switch_block[_ref]
and special prong (detection) logic for switch.
Adds a new SpecialProng tag 'absorbing_under' that allows
specifying additional explicit tags in a '_' prong which
are respected when checking that every value is handled
during semantic analysis but are not transformed into AIR
and instead 'absorbed' by the '_' branch.
… statements

If both are used, 'else' handles named members and '_' handles
unnamed members. In this case the 'else' prong will be unrolled
to an explicit case containing all remaining named values.
@Justus2308
Justus2308 force-pushed the switch-better-underscore branch from 34abbdf to 0ecbd5a Compare August 7, 2025 12:05
@Justus2308

Copy link
Copy Markdown
Member Author

Thank you!

@andrewrk andrewrk added the release notes This PR should be mentioned in the release notes. label Aug 7, 2025
@Justus2308
Justus2308 requested a review from mlugg August 7, 2025 23:45
@mlugg
mlugg merged commit 6e90ce2 into ziglang:master Aug 13, 2025
@mlugg

mlugg commented Aug 13, 2025

Copy link
Copy Markdown
Member

Great work, thank you!

Comment thread test/behavior/switch.zig
Comment on lines +1110 to +1111
else => {},
_ => return error.TestFailed,

This comment was marked as resolved.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It's completely order independent, just like you could already put else or _ at any position. The only limitation is that the else prong cannot have any additional explicit cases specified, this isn't a new limitation though.

@alexrp alexrp removed the release notes This PR should be mentioned in the release notes. label Oct 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

5 participants