Bug 11922 - is this code reachable or not?
Summary: is this code reachable or not?
Status: RESOLVED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 2.0   Edit
Hardware: PC Windows 2000
: P3 normal (vote)
Target Milestone: 2.0 M5   Edit
Assignee: Philipe Mulet CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2002-03-20 11:58 EST by Adam Kiezun CLA
Modified: 2002-03-21 13:04 EST (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
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