Bug 89222

Summary: "Unreachable Code" error in wrong place
Product: [Eclipse Project] JDT Reporter: Robert Konigsberg <konigsberg>
Component: CoreAssignee: JDT-Core-Inbox <jdt-core-inbox>
Status: RESOLVED INVALID QA Contact:
Severity: minor    
Priority: P3    
Version: 3.1   
Target Milestone: 3.1 M6   
Hardware: PC   
OS: Windows XP   
Whiteboard:

Description Robert Konigsberg CLA 2005-03-28 10:37:50 EST
Look at the following code:

Report bug to eclipse. This code is "unreachable" but it's always showing the
wrong section:
  public static long test2() {
    long sum = 0;
    for (int i = 0; 1 < 1000; i++) {
      sum += i;
    }
    return sum;
  }


Turns out there's a bug in the code: It shouldn't be "1 < 1000", it should be "i
< 1000". However, that creates an "unreachable code" error, since the "sum += i"
line can't be reached. Where does Eclipse choose to mark the error? At the line
"return sum".

Thanks, Rob
Comment 1 Philipe Mulet CLA 2005-03-28 12:01:44 EST
The error location isn't what I would have guessed either, but neither javac
1.5.0 or jikes do any better than we do on this one.

D:\src>javac X.java -d ..\bin -Xlint
X.java:7: unreachable statement
    return sum;
    ^
1 error

D:\src>jikes X.java -d ..\bin -cp d:\jdk1.4.2\jre\lib\rt.jar

Found 1 semantic error compiling "D:/src/X.java":

     7.     return sum;
            ^---------^
*** Semantic Error: This statement is unreachable.
Comment 2 Philipe Mulet CLA 2005-03-28 12:03:37 EST
Actually, we do the right thing. 1 < 1000 is equivalent to true, so the code
inside the loop is definitely reachable, but since the loop has no exit point,
then the next statement ("return sum;") is made unreachable.

Had you written 1 > 1000 instead, you'd see the statement inside the loop being
flagged as unreachable.

Closing as invalid