www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - bug(?) with "statement is not reachable" warning

reply shro8822 uidaho.edu writes:
the file warning.d:

int main()

{

int i = 1;

goto skip;



{

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
Jul 08 2005
parent reply "Walter" <newshound digitalmars.com> writes:
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;

{
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;
Jul 08 2005
parent B Shropshire <B_member pathlink.com> writes:
int i = 1;
goto skip;

{
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; { i += 1; skip: t *= 2; i %= 5; } while(0<i && i<5) return i;
Jul 09 2005