Bug 122987

Summary: [1.5][compiler] Boxing conversion should be performed in conditional expression
Product: [Eclipse Project] JDT Reporter: Chris Smith <cdsmith>
Component: CoreAssignee: Philipe Mulet <philippe_mulet>
Status: VERIFIED FIXED QA Contact:
Severity: normal    
Priority: P3    
Version: 3.2   
Target Milestone: 3.2 M5   
Hardware: PC   
OS: Windows XP   
Whiteboard:

Description Chris Smith CLA 2006-01-06 19:17:39 EST
Eclipse 3.2M3

public class Test
{
    public static void main(String[] args)
    {
        Object obj = true ? true : 17.3;
    }
}

Eclipse says:

Incompatible conditional operand types Boolean and double

JLS says:

Otherwise, the second and third operands are of types S1 and S2 respectively.  Let T1 be the type that results from applying boxing conversion to S1, and let T2 be the type that results from applying boxing conversion to S2.  The type of the conditional expression is the result of applying capture conversion to lub(T1, T2).

As I understand it:

S1 = boolean, S2 = double
T1 = Boolean, T2 = Double
lub(T1, T2) = something assignment compatible with Object
capture applied to lub(T1, T2) = lub(T1, T2)

Therefore, the expression is valid and should yield Boolean.TRUE.  Eclipse seems to have applied boxing conversion to the second argument, but not to the third argument, and thus failed.

Oddly enough:

    Object obj = true ? true : Double.valueOf(17.3);

Eclipse gives the same error.  It converts true to type Boolean by boxing conversion, AND Double.valueOf(17.3) to double by unboxing conversion, and THEN gets stuck.
Comment 1 Philipe Mulet CLA 2006-02-02 13:05:04 EST
Good find. Indeed the compiler logic is wrong. It must promote both operands in this situation (bogus 'else' in between to IFs).

Added AutoboxingTest#test117
Comment 2 Philipe Mulet CLA 2006-02-02 13:06:06 EST
should backport to 3.1.x
Comment 3 Philipe Mulet CLA 2006-02-02 17:55:06 EST
Fixed in 3.2 and 3.1.x streams.
Comment 4 Jerome Lanneluc CLA 2006-02-15 07:53:06 EST
Verified for 3.2 M5 using build I20060215-0010