Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4952888

javac problems in conditional operator with different reference types

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P3 P3
    • None
    • 5.0
    • tools
    • generic
    • solaris_8



      Name: vpR10072 Date: 11/12/2003



      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

      ======================================================================

            gafter Neal Gafter (Inactive)
            duke J. Duke
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: