#601 empty body if clause optimizes out else

verified
closed-fixed
emitter (227)
9
2002-09-30
2002-07-11
Anonymous
No

If the true expression of an if clause is empty, then the false/else branch is ignored. This is a huge regression.

The following code compiles (even with +P) without errors or warnings, and runs without any output! If the comment line is commented in (or any non-comment expression is put in place), then it runs as expected. Examination of disassembled code reveals that the entire else branch was never emitted.

public class Broken {
public void bogon(int l) {
for (int i = 0; i<l; i++) {
if (i==0) {
//System.out.println("i=zero"); //broken!!!
} else {
System.out.println("i="+i);
}
}
}
public static void main(String[] argv) {
new Broken().bogon(10);
}
}

Discussion

  • Nobody/Anonymous

    more succinctly, in the following case:

    if (expr) {} else { statement; }

    "statement" is not compiled/emitted.

     
  • Eric Blake

    Eric Blake - 2002-07-11
    • priority: 5 --> 9
    • assigned_to: nobody --> ebblake
     
  • Eric Blake

    Eric Blake - 2002-07-11

    Yikes. Workaround until I can fix it: Don't use empty if clauses.

     
  • Nobody/Anonymous

    I'm seeing this too, in a huge body of existing code which would be impossible to check for empty 'if' or 'else' body's, especially since no compile-time or runtime error occurs. I need a new version of Jikes because of other bugs in prior versions. Please fix this as soon as possible!

     
  • Eric Blake

    Eric Blake - 2002-07-23

    A patch has been committed to CVS. Feel free to rebuild your own version of jikes.

    We are hoping to release 1.17 soon, once the regression reports settle down.

     
  • Eric Blake

    Eric Blake - 2002-07-23
    • status: open --> open-fixed
     
  • Nobody/Anonymous

    hanspeter.hoegger@netcetera.ch found the same problem. Here's a simpler variant:

    public static void main(String[] argv) {
    int res = 1;
    if (res == 2) {
    // problem if empty
    } else {
    res = 3; // isn't executed if first if block is empty
    }
    System.out.println("res=" + res + ", expected 3");
    }

     
  • Eric Blake

    Eric Blake - 2002-09-30

    I can breathe a sigh of relief. This frequently reported bug is dead, fixed in 1.17.

     
  • Eric Blake

    Eric Blake - 2002-09-30
    • status: open-fixed --> closed-fixed
     

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks