Bug 89222 - "Unreachable Code" error in wrong place
Summary: "Unreachable Code" error in wrong place
Status: RESOLVED INVALID
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.1   Edit
Hardware: PC Windows XP
: P3 minor (vote)
Target Milestone: 3.1 M6   Edit
Assignee: JDT-Core-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2005-03-28 10:37 EST by Robert Konigsberg CLA
Modified: 2005-03-28 12:03 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 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