Skip to content

Proposal: Do not require else branch on valued while with comptime-known true clause #7828

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

Closed
ghost opened this issue Jan 19, 2021 · 1 comment
Milestone

Comments

@ghost
Copy link

ghost commented Jan 19, 2021

Currently, any valued while requires an else branch, even if that branch can be statically known to never evaluate. The sensible thing would be else unreachable, but say the code is refactored, and suddenly the clause is runtime-known -- then there's a new point of failure, either a crash if caught in testing or UB if not.

After discussion with @ifreund and @SpexGuy (by which I mean, they discussed it -- I'm just the messenger), we believe the best solution is to allow leaving off the else branch in this case. That way, you don't have to write any dead code, and if a refactor makes it live, you get an error instead of a crash or UB.

At first blush it may seem strange to special-case comptime clauses like this, but in fact there is precedent -- statically-known untaken if and switch branches are not analysed, for instance, which is crucial in many areas. One thing which was considered to make such a thing seem more normal was allowing the clause to be optional, and renaming while to fit, however I personally believe this to be a bad idea; see #7826 for reasoning.

@Vexu
Copy link
Member

Vexu commented Jan 19, 2021

Duplicate of #707

@Vexu Vexu marked this as a duplicate of #707 Jan 19, 2021
@Vexu Vexu closed this as completed Jan 19, 2021
@andrewrk andrewrk added this to the 0.8.0 milestone Jun 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants