Proposal: Do not require else
branch on valued while
with comptime-known true clause
#7828
Milestone
else
branch on valued while
with comptime-known true clause
#7828
Currently, any valued
while
requires anelse
branch, even if that branch can be statically known to never evaluate. The sensible thing would beelse 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
andswitch
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 renamingwhile
to fit, however I personally believe this to be a bad idea; see #7826 for reasoning.The text was updated successfully, but these errors were encountered: