Java Solaris Communities Sun Store Join SDN My Profile Why Join?
 
Bug Database
Bug Detail
Quick Lists
Top 25 Bugs
Top 25 RFE's
Recently Closed Bugs
Printable Page Printable Page


Bug Database
Bug ID: 4952888
Votes 0
Synopsis javac problems in conditional operator with different reference types
Category java:compiler
Reported Against 1.5
Release Fixed
State 11-Closed, duplicate of 4881179, bug
Priority: 3-Medium
Related Bugs 4881179
Submit Date 12-NOV-2003
Description





The "Autoboxing and Auto-unboxing support for Java^tm Programming Language"
document reads in JLS 15.25 Conditional Operator ? :

   ...
   # If the second and third operands are of different reference types
     T1 and T2 then the type of the conditional expression is the most
     specific type S such that both T1 and T2 are assignment compatible
     with S.

However, javac reports errors in conditional operator if the second and
third operands are of different reference types so that neither is a
subtype of the other, but there exists the most specific type such that
both operand types are assignment compatible with it.

To reproduce:
-------------

% cat bug.java
-----------------------------------------------cut here
import java.io.PrintStream;


class bugd {
    int a = 1;
}

class buga extends bugd {}

class bugb extends buga {}

public class bug extends buga {

    static final int TTopClass = 0;
    static final int TClass    = 1;
    static final int TSubClass = 2;
    static final int TObj      = 3;

    static int getType(bugd D) {return TTopClass; }
    static int getType(buga A) {return TClass; }
    static int getType(bugb B) {return TSubClass; }
    static int getType(bug C) {return TSubClass; }
    static int getType(Object O) {return TObj; }

    public static void main(String args[]) {
        System.exit(run(args, System.out) + 95/*STATUS_TEMP*/);
    }

    public static int run(String args[], PrintStream out) {
        bugb c1 = new bugb();
        bug c2 = new bug();

        if (       (getType(true ? c1 : c2) == TClass)
                && (getType(true ? c2 : c1) == TClass)
                && (getType(false ? c1 : c2) == TClass)
                && (getType(false ? c2 : c1) == TClass) )
            return 0/*STATUS_PASSED*/;

        out.println ("FAIL");
        out.println (getType(true ? c1 : c2));
        out.println (getType(true ? c2 : c1));
        out.println (getType(false ? c1 : c2));
        out.println (getType(false ? c2 : c1));
        return 2/*STATUS_FAILED*/;
    }
}
-----------------------------------------------cut here
% java -showversion

java version "1.5.0-beta"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-beta-b26)
Java HotSpot(TM) Client VM (build 1.5.0-beta-b26, mixed mode)

% javac -source 1.5 bug.java

bug.java:33: incompatible types for ?: neither is a subtype of the other
second operand: bugb
third operand : bug
        if (       (getType(true ? c1 : c2) == TClass)
                                 ^
bug.java:34: incompatible types for ?: neither is a subtype of the other
second operand: bug
third operand : bugb
                && (getType(true ? c2 : c1) == TClass)
                                 ^
bug.java:35: incompatible types for ?: neither is a subtype of the other
second operand: bugb
third operand : bug
                && (getType(false ? c1 : c2) == TClass)
                                  ^
bug.java:36: incompatible types for ?: neither is a subtype of the other
second operand: bug
third operand : bugb
                && (getType(false ? c2 : c1) == TClass) )
                                  ^
bug.java:40: incompatible types for ?: neither is a subtype of the other
second operand: bugb
third operand : bug
        out.println (getType(true ? c1 : c2));
                                  ^
bug.java:41: incompatible types for ?: neither is a subtype of the other
second operand: bug
third operand : bugb
        out.println (getType(true ? c2 : c1));
                                  ^
bug.java:42: incompatible types for ?: neither is a subtype of the other
second operand: bugb
third operand : bug
        out.println (getType(false ? c1 : c2));
                                   ^
bug.java:43: incompatible types for ?: neither is a subtype of the other
second operand: bug
third operand : bugb
        out.println (getType(false ? c2 : c1));
                                   ^
8 errors

======================================================================
Work Around
N/A
Evaluation
I could not reproduce this with the Tiger nightly build.

  xxxxx@xxxxx   2003-11-12
Comments
  
  Include a link with my name & email   

Submitted On 07-DEC-2004
Leandro_Salvador
public static void test() {
  Integer two1 = new Integer(2);
  Integer two2 = new Integer(2);
  System.out.println(two1 >= two2);     // OK
  System.out.println(two1 <= two2);     // OK
  System.out.println(two1 == two2);     // PROBLEMS!!!
}



PLEASE NOTE: JDK6 is formerly known as Project Mustang