You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In the code generated by a compiler for an exhaustive case statement,
it would be helpful for debugging if a default case was emitted that just raises
something like newException(Defect, "Impossible case value").
Description
As an example:
typeKinds=enum
kThis, kThat
procdoSomething(k: Kinds) =case k
of kThis:
echo"this" of kThat:
echo"that"
For the C++ backend, it generates a switch statement with a case
with two branches, one for each enum value. Normally, this is sufficient,
because k can't take on an invalid value - attempts to cast an out of range int
to Kinds will just raise an exception.
This can be a problem if you're working with pointers though. For example,
if the function was:
procdoSomething(k: ptrKinds) =case k[]
of...
Since the memory pointed to by k[] can be initialized outside of the safety constraints of the language, k[] can be out of range of the enum, and no branch of the switch
will be taken. What I'd like for this case is for the switch to have a default branch that
just raises a Defect exception saying something like "Invalid value for case".
Of course this came up because I had a pointer to an incorrectly initialized block of
memory. It took me quite a few rounds of echo debugging to figure out that none of
the branches of my case statement were being executed, and why.
The text was updated successfully, but these errors were encountered:
This would be a noticeable overhead in the release mode, but could be useful in debug mode.
Likely it is not the case that needs extra default clause, but enum value checks instead.
Summary
In the code generated by a compiler for an exhaustive case statement,
it would be helpful for debugging if a default case was emitted that just raises
something like
newException(Defect, "Impossible case value")
.Description
As an example:
For the C++ backend, it generates a switch statement with a case
with two branches, one for each enum value. Normally, this is sufficient,
because
k
can't take on an invalid value - attempts to cast an out of range intto
Kinds
will just raise an exception.This can be a problem if you're working with pointers though. For example,
if the function was:
Since the memory pointed to by k[] can be initialized outside of the safety constraints of the language,
k[]
can be out of range of the enum, and no branch of the switchwill be taken. What I'd like for this case is for the switch to have a
default
branch thatjust raises a
Defect
exception saying something like "Invalid value for case".Of course this came up because I had a pointer to an incorrectly initialized block of
memory. It took me quite a few rounds of
echo
debugging to figure out that none ofthe branches of my case statement were being executed, and why.
The text was updated successfully, but these errors were encountered: