United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-5105890 (codegen) constant folding broken for conditional operator
JDK-5105890 : (codegen) constant folding broken for conditional operator

Details
Type:
Bug
Submit Date:
2004-09-22
Status:
Resolved
Updated Date:
2004-10-18
Project Name:
JDK
Resolved Date:
2004-10-12
Component:
tools
OS:
solaris_8
Sub-Component:
javac
CPU:
generic
Priority:
P1
Resolution:
Fixed
Affected Versions:
5.0
Fixed Versions:
5.0u1 (b04)

Related Reports

Sub Tasks

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
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
                                     
4-10-12 00:00:00
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
                                     
2004-09-29
CONVERTED DATA

BugTraq+ Release Management Values

COMMIT TO FIX:
1.5.0_01
mustang


                                     
2004-09-30



Hardware and Software, Engineered to Work Together