Bug 11922

Summary: is this code reachable or not?
Product: [Eclipse Project] JDT Reporter: Adam Kiezun <akiezun>
Component: CoreAssignee: Philipe Mulet <philippe_mulet>
Status: RESOLVED FIXED QA Contact:
Severity: normal    
Priority: P3    
Version: 2.0   
Target Milestone: 2.0 M5   
Hardware: PC   
OS: Windows 2000   
Whiteboard:

Description Adam Kiezun CLA 2002-03-20 11:58:42 EST
public void m() {
       for(;false;p());
       System.out.println("m");
   }
   public void p() {
    System.out.println("p");
   }	

when you call m()
you see that it gets stuck in a loop printing 'p'

questions:
1. why is p() called at all? i think 14.13.2 in the spec (2nd edition) says the 
opposite
2. if it is executed correctly - i think the subsequent statement can be marked 
as unreachable code
Comment 1 Philipe Mulet CLA 2002-03-20 15:07:17 EST
I would agree with you. Sounds like a codegen bug (where we would treat is as a 
for(;true;p())

Need to investigate
Comment 2 Olivier Thomann CLA 2002-03-20 17:33:34 EST
With this test case:
[public class A {

   public void m() {
       for(;false;p());
       
System.out.println("m");
   }
   
   public void p() {
     System.out.println("p");
   }
   
   
public static void main(String[] args) {
   	new A().m();
   }
}]
javac 1.4 reports an 
unreachable statement.
A.java:4: unreachable statement
       for(;false;p());
                      ^
1 
error

javac 1.3 accepts it without any error. And the output is: 'm'.
The code gen is quit 
awkwards.
Method void m()
   0 goto 7
   3 aload_0
   4 invokevirtual #2 <Method void p()>
   7 
getstatic #3 <Field java.io.PrintStream out>
  10 ldc #4 <String "m">
  12 invokevirtual #5 
<Method void println(java.lang.String)>
  15 return

It seems that the bytecodes from 3 to 4 
included are unreachable. The code gen could simply be:
Method void m()
   0 getstatic #3 <Field 
java.io.PrintStream out>
   3 ldc #4 <String "m">
   5 invokevirtual #5 <Method void 
println(java.lang.String)>
   8 return

We generate:
Method void m()
   0 aload_0
   1 
invokevirtual #17 <Method void p()>
   4 goto 0
   7 getstatic #23 <Field java.io.PrintStream 
out>
  10 ldc #24 <String "m">
  12 invokevirtual #30 <Method void 
println(java.lang.String)>
  15 return

which explains the infinite loop.
Comment 3 Philipe Mulet CLA 2002-03-21 13:04:01 EST
Fix post M4 build. We now generate no bytecode at all for such a for statement 
(javac does produce unreachable code).

Fixed