Community
Participate
Working Groups
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
I would agree with you. Sounds like a codegen bug (where we would treat is as a for(;true;p()) Need to investigate
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.
Fix post M4 build. We now generate no bytecode at all for such a for statement (javac does produce unreachable code). Fixed