Bug 2678 – for loops are already assumed to terminate
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Linux
Creation time
2009-02-20T09:15:00Z
Last change time
2015-06-09T05:15:19Z
Assigned to
bugzilla
Creator
andrei
Comments
Comment #0 by andrei — 2009-02-20T09:15:41Z
Consider this code compiled with -w:
int main()
{
int i;
for (;; ++i)
{
if (i == 10) return 0;
}
i += 100;
}
This loop never reaches its end. However the compiler does not detect that and spuriously asks for a return at the end of the function. Worse, if there is some unreachable code following the loop, it does not recognize that.
All loops that (a) have no termination condition or a nonzero compile-time-constant termination condition, and (b) do not embed any "break" statement - should be understood as loops that do not fall through.
Before anyone brings up Turing completeness: I said "nonzero compile-time-constant termination condition".
Comment #1 by andrei — 2009-02-20T09:17:01Z
> Before anyone brings up Turing completeness: I said "nonzero
> compile-time-constant termination condition".
s/Turing completeness/Turing's machine halting problem/
Comment #2 by shro8822 — 2009-02-20T10:52:34Z
You would also need to take into account try/catch blocks. This doesn't actually invalidate the assertion (you still can't fall out of the loop), it just forces you to be more careful how you read it (you /can/ end up running the next line of code after the loop if it is in a catch block)
Comment #3 by andrei — 2009-02-20T11:27:34Z
(In reply to comment #2)
> You would also need to take into account try/catch blocks. This doesn't
> actually invalidate the assertion (you still can't fall out of the loop), it
> just forces you to be more careful how you read it (you /can/ end up running
> the next line of code after the loop if it is in a catch block)
>
Yah, and goto is to be handled as well. I'm just saying the loop will never naturally fall off its end.
Comment #4 by clugdbug — 2009-03-03T07:49:34Z
This also applies to:
while(1) {...}
But I notice that Walter's already fixed that <g>.
Comment #5 by witold.baryluk+d — 2009-03-05T16:56:28Z
How about assert(0); at the end?
Comment #6 by smjg — 2009-03-06T14:56:22Z
That should equally generate an unreachable code warning.