On 3/13/18 3:21 PM, Timon Gehr wrote:
On 13.03.2018 18:43, H. S. Teoh wrote:
On Tue, Mar 13, 2018 at 01:32:55PM -0400, Steven Schveighoffer via Digitalmars-d wrote:
An unrolled foreach on a tuple has a notion that the flow control
jumps out of the loop, and it's OK to skip further loops (even though
they are technically unrolled).

This is not true.  Foreach on a tuple does *not* skip expanding all
iterations of the loop regardless of any `break`s or `continue`s. It's
the codegen that eliminates the resulting dead code.  See:



I think that's what he was saying. :)
Yes. In fact, when you use the ever-so-handy -vcg-ast switch, it shows this:

staticFind!(int, int, double)
        pure nothrow @nogc @safe int staticFind()
                /*unrolled*/ {
                                enum ulong id = 0LU;
                                alias R = int;
                                return 0;
                                enum ulong id = 1LU;
                                alias R = double;
                                return -1;


If I compile that directly, I get the unreachable warning. Obviously there is something hidden in the actual AST that says "it's ok this statement isn't reached, because it's really skipped by returning out of the foreach".

If I use static foreach, it has the warning, and I'm assuming this is because static foreach is a straight loop unrolling, whereas foreach on a tuple has some semblance of flow control with the loop itself.

Note: I don't have any idea how this really works internally, I'm guessing at the black box behavior :)


Reply via email to