Another note, because this issue is still open and can be solved easily
The case-statement needs to handle undefined values.
First justification: The internal treatment should and can be better than a crash.
Another: Just include this tiny check, to be Delphi compatible.
And if reading time allows, more details:
Regardless of enumsize, the variable may contain an invalid value.
Even in bitpacked storage: tenum (0,1,2), bitpacked to 2 bits,
means we have the undefined value 3 ((True, True), but together invalid).
Only one bit and a "half" bit are defined by tenum.
But in most examples, users have unpacked enums of sizeof(tenum)
and if they want to check the value, they can do it in the next operation.
And the user knowingly abstains from size-reducing conversions prior to the check.
If even in this situation, nontransparent optimizations reverse the value validness
(remove all invalid bits) prior to the check, then we have a compiler mistake.
The compiler has to disable nontransparent optimizations on invalid enums,
because the language has larger priority. If there is no bitpacked container,
then the compiler should just transfer the whole enum (sizeof(tenum)) around.
It could not be simpler.
The existence of undefined values requires treatment:
Undefined behavior for undefined values should mean in our case:
The value-specific behavior is undefined, but defined is a common default handling
for all undefined values in value-testing operations (if, case, in, is)
These operations all check for equality (a specific case of inside) or inside subsets.
It's a law of logic that they should use the else-branch for not-inside.
The variable may contain undefined bits (maybe half bit) outside of type range.
Generally, ALL bits of an enum variable (storage (could be even persistent))
belong to its value, therefore operations need to copy and handle all bits.
(This is like with a valid bytevalue represented by a word with higher zerobits)
For outer values, the operation needs to detect ONLY that the value is not inside, thus outside.
The operation does not know the meaning of the outer value, but only its existence.
That's all what the operation can safely deduce from such typed value.
Such inside-test is fully sufficient for operations like (if, case, in, is).
And users can use this simple ability also for intentional inside/outside tests:
case tenum(src) of ...
The user says, the case statement should receive a value with userdefined sizeof(tenum).
Tenum(src) means, the case statement will silently (unchecked) get the src value.
The user should ensure (sizeof(enum) >= sizeof(src)), to keep all source bits.
The case statement receives the value and the inside test is applied.