3

Before anyone screams about EOL'ed JDK, I'd like to point out that my question is not about how to compile the following. There is a real question here and it's not about JDK 1.5 being EOL'ed...

The following under JDK 1.5, up to 1.5.0_22 (the last one I could find) produces on my system a compiler error:

private Object[] boozinga() {
    boolean b = Math.abs(42) > 0;
    Object[] res = new Object[1];
    res[0] = b ? new int[1] : new String[1];
    return res;
}

Changing the Math.abs(42) > 0 to true allows compilation.

Changing the ternary "assignment" to an if/else allows compilation.

Using JDK 1.6 allows compilation.

So I was wondering: is there something not legal in the above code under Java 1.5 and that is allowed under Java 1.6?

Does it crash for those of you that are under Java 1.5 too?

The crash says something like this:

An exception has occured in the compiler (1.5.0_22). Please file a bug at the Java Developer Connection (http://java.sun.com/webapps/bugreport) after checking the Bug Parade for duplicates. Include your program and the following diagnostic in your report. Thank you.

I take it filling a bug report for an EOL'ed JDK is an exercice in futility but still, I'd still like to know if the above is valid Java 1.5 code or not.

SyntaxT3rr0r
  • 27,745
  • 21
  • 87
  • 120
  • 1
    As to why we encountered this, we still have no fix for the following: http://stackoverflow.com/questions/2299250/java-vm-reproducable-sigsegv-on-both-1-6-0-17-and-1-6-0-18-how-to-report and on our system we can reproduce a JDK 1.6 *sigsegv* reliably while crunching gigantic amount of data. Funny uh!? One *sigsegv* JDK 1.6 and one compiler exception with 1.5 all in one project. And, no, our RAM / system ain't faulty ; ) – SyntaxT3rr0r Jun 01 '11 at 17:07
  • 1
    code looks legit to me. seems like a bug. – jtahlborn Jun 01 '11 at 17:24
  • Could anyone with old 1.5 installed (maybe someone on OS X?) try to compile it? – SyntaxT3rr0r Jun 01 '11 at 17:31
  • compiling with 1.5.0_12 on Linux 2.6.22.14-72.fc6 produced an AssertionFailure with a similar message. – jtahlborn Jun 01 '11 at 17:44

2 Answers2

3

I think it is legal. The evidence is that JDK 1.6.0_21 compiles it with options -source 1.5 -target 1.5. Can't you use JDK 1.6 with these options to compile and JRE 1.5 to run?

It crashes for me, too (JDK 1.5.0_12). It crashes for me even with:

public Object boozinga() {
    boolean b = true;
    Object res = b ? new int[1] : new String[1];
    return res;
}

The difficulty for the compiler is that the type of b ? new int[1] : new String[1] is java.lang.Object & java.io.Serializable & java.lang.Cloneable.

0

The problem here is that the compiler has trouble to decide the type of the expression b ? new int[1] : new String[1]. I had something like this before (with 1.1.8 or 1.2, I think - but with a real error message, not a compiler crash), and then simply used a cast to help the compiler here.

 res[0] = b ? (Object)new int[1] : new String[1];

I didn't look what the language specification says about this - but the compiler should never crash with an exception, it should give a real error message.

Paŭlo Ebermann
  • 73,284
  • 20
  • 146
  • 210