JDK-5105890 : (codegen) constant folding broken for conditional operator
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 5.0
  • Priority: P1
  • Status: Resolved
  • Resolution: Fixed
  • OS: solaris_8
  • CPU: generic
  • Submitted: 2004-09-22
  • Updated: 2004-10-18
  • Resolved: 2004-10-12
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
5.0u1 b04Fixed
Description
As expression of the form:

    private static final String str1 = (flag ? "STRING" : null);

is incorrectly evaluated to be "STRING" even when flag is false.

The attached program demonstrates this. It works on JDK 1.4.2 (and
earlier) and fails on JDK 1.5.0-rc. This is definitely a javac bug,
because compiling this program on 1.4.2 and running it on 1.5.0-rc
works fine. Compiling it on 1.5.0 (with -source 1.4 and -target 1.4)
fails when run on either 1.5.0-rc or 1.4.2.

When run on 1.5.0, the following error occurs:

javac bug.java
java bug

flag = false
str1 = REGRESSION!!!
str2 = null
str3 = OK


The expected output (as demonstrated on 1.4.2 and by inspection) is:

flag = false
str1 = null
str2 = null
str3 = OK

Comments
CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: 1.5.0_01 mustang
30-09-2004

SUGGESTED FIX Index: src/share/classes/com/sun/tools/javac/comp/Attr.java @@ -830,8 +830,10 @@ elsetype = types.boxedClass(elsetype).type; } - if (types.isSubType(thentype, elsetype)) return elsetype; - if (types.isSubType(elsetype, thentype)) return thentype; + if (types.isSubType(thentype, elsetype)) + return elsetype.baseType(); + if (types.isSubType(elsetype, thentype)) + return thentype.baseType(); if (!allowBoxing || thentype.tag == VOID || elsetype.tag == VOID) { log.error(pos, "neither.conditional.subtype", @@ -842,7 +844,7 @@ // both are known to be reference types. The result is // lub(thentype,elsetype). This cannot fail, as it will // always be possible to infer "Object" if nothing better. - return types.lub(thentype, elsetype); + return types.lub(thentype.baseType(), elsetype.baseType()); } public void visitIf(If tree) { ###@###.### 2004-09-29
29-09-2004

EVALUATION This is a bug. The compiler generates wrong code so we should try to fix this in a Tiger update release. ###@###.### 2004-09-27 We have a fix scheduled for the next update release. Fortunately, the problem is not as bad as one might think: one branch of the conditional operator must be null and the other a String in order for this bug to trigger. This is not a very common scenario. ###@###.### 10/12/04 23:26 GMT
12-10-0004