Thread overview
bug(?) with "statement is not reachable" warning
Jul 08, 2005
shro8822
Jul 08, 2005
Walter
Re: bug(?) with
Jul 09, 2005
B Shropshire
July 08, 2005
the file warning.d:

int main()

{

int i = 1;

goto skip;

do	//line #5

{

i += 1;

skip:


t *= 2;

i %= 5;

}

while(0<i && i<5)

return i;

}

when compiled with v0.127 using:

dmd warning.d -w

returns

warning - warning.d(5): statement is not reachable



July 08, 2005
It's a fine example of why I don't like warnings about "unreachable" code. I can trick about every compiler into generating spurious warnings or missing important cases.

Whether a statement is reachable or not can only be computed properly with data flow analysis, which is done by the optimizer long after the semantic passes are complete. The semantic pass uses a primitive, brain-damaged form of data flow analysis.

My recommendation is to not worry about it.

P.S. Many programmers consider jumping forward into the middle of a loop as bad form. May I suggest instead of:

int i = 1;
goto skip;
do //line #5
{
i += 1;
skip:
t *= 2;
i %= 5;
}
while(0<i && i<5)
return i;

write:

    int i = 1;
    while (1)
    {
        t *= 2;
        i %= 5;
        if (i <= 0 || i >= 5)
            break;
        i++;
    }
    return i;


July 09, 2005
>int i = 1;
>goto skip;
>do //line #5
>{
>i += 1;
>skip:
>t *= 2;
>i %= 5;
>}
>while(0<i && i<5)
>return i;
>


Actually that was is a contrived example. What I found gets rid of the error is to label the loopwith a dummy label. A fix to the erroneous warning might be to look for labels in a loop and treat that as the whole loop being reachable.

int i = 1;
goto skip;

kluge: do //line #5

{
i += 1;
skip:
t *= 2;
i %= 5;
}
while(0<i && i<5)
return i;