How to Get Fired Using Switch Statements and Statement Expressions
Statement expressions are certainly not in the standard nor supported by MSVC, and based on the compiler behaviour exhibited in the article, it looks like they may have been added in haste without considering (nor testing...) how the feature would interact with all the other features of the language.<p>The Asm fragment there seems like it doesn&#x27;t even match the C at all; it begins with these 3 rather useless instructions which appear to be a very dumb translation of &quot;if((char)1)&quot;, a statement that does not even exist in the C code:<p><pre><code>        movb    $1, %al
        testb   %al, %al
        jne     .LBB0_2
</code></pre>
<i>In the first iteration the use of the jump from the switch statement jumps over these instructions, although they are executed on later loop iterations.  It seems pretty reasonable that the compiler would do this, after all you are telling it to branch into the middle of a for loop comparison.</i><p>IMHO if branching to that location is allowed, this would be a defect because there is a (slightly) saner interpretation: branch there to execute the case, then &quot;return&quot; 10 from the statement expression and compare that to i, continuing onward from there as usual. Whether the compiler chooses to use temporary variables shouldn&#x27;t make any difference according to these semantics. If we &quot;linearise&quot; that example according to such a principle, it may become something like this:<p><pre><code>        int i = 0;
        &#x2F;&#x2F; switch(i)
        if(i == 0)
            goto case0;
        else
            goto switch_end;
        &#x2F;&#x2F; i = 0;           &#x2F;&#x2F; unreachable
        &#x2F;&#x2F; goto loop_check; &#x2F;&#x2F; unreachable
    loop_body:
        i;
        &#x2F;&#x2F; loop increment
        i++;
    case0:
        &#x2F;&#x2F; if there was code for case 0 it would go here
    loop_check:
        int r = 10; &#x2F;&#x2F; result of statement expression
        if(i &lt; r)
            goto loop_body;
    switch_end:
        return 0;
</code></pre>
...and that does not cause any uninitialised reads or writes. Also, the extremely contorted control flow in some of these examples reminds me of continuations.<p>The jump into the function parameter is puzzling at first (and once again shows what I&#x27;d consider a defect since it crashes), but using the same semantics of &quot;continue at the remainder of the statement&quot;, it can be transformed into this:<p><pre><code>        int i = 0;
        if(i == 0)
            goto case0;
        else
            goto switch_end;
    case0:
        &#x2F;&#x2F; if there was code for case 0 it would go here
        int r = 1;
        &#x2F;&#x2F; continue with evaluating the parameter
        int p = i + r + i;
        &#x2F;&#x2F; and call the function
        f(p);
    switch_end:
        return 0;</code></pre>