2678 – for loops are already assumed to terminate

D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 2678 - for loops are already assumed to terminate
Summary: for loops are already assumed to terminate
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86 Linux
: P2 normal
Assignee: Walter Bright
URL:
Keywords:
Depends on:
Blocks:
Reported: 2009年02月20日 09:15 UTC by Andrei Alexandrescu
Modified: 2015年06月09日 05:15 UTC (History)
1 user (show)

See Also:


Attachments
Add an attachment (proposed patch, testcase, etc.)

Note You need to log in before you can comment on or make changes to this issue.
Description Andrei Alexandrescu 2009年02月20日 09:15:41 UTC
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 Andrei Alexandrescu 2009年02月20日 09:17:01 UTC
> Before anyone brings up Turing completeness: I said "nonzero
> compile-time-constant termination condition".
s/Turing completeness/Turing's machine halting problem/
Comment 2 BCS 2009年02月20日 10:52:34 UTC
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 Andrei Alexandrescu 2009年02月20日 11:27:34 UTC
(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 Don 2009年03月03日 07:49:34 UTC
This also applies to:
while(1) {...}
But I notice that Walter's already fixed that <g>.
Comment 5 Witold Baryluk 2009年03月05日 16:56:28 UTC
How about assert(0); at the end?
Comment 6 Stewart Gordon 2009年03月06日 14:56:22 UTC
That should equally generate an unreachable code warning.
Comment 7 Walter Bright 2009年03月11日 14:54:15 UTC
Fixed dmd 1.041 and 2.026


AltStyle によって変換されたページ (->オリジナル) /