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

Imprecise type inference in the presence of ternary operator and wildcard

XMLWordPrintable

    • generic
    • generic

      A DESCRIPTION OF THE PROBLEM :
      In this program, javac infers a Captured type of `? extends Object` as the type of the variable `x`.
      I would expect to infer `B<? super T>`, since `B<? super T>` is more specific than `? extends Object`, and is a supertype of both `B<T>` and `B<? super T>`.


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      javac A.java

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Compile
      ACTUAL -
      A.java:5: error: incompatible types: B<CAP#1> cannot be converted to B<? super T>
          return x;
                 ^
        where T is a type-variable:
          T extends Object declared in class A
        where CAP#1 is a fresh type-variable:
          CAP#1 extends Object from capture of ? extends Object
      1 error

      ---------- BEGIN SOURCE ----------
      class A<T> {
        public A() {}
        public B<? super T> foo() {
          var x = (true) ? new B<T>() : (B<? super T>) null;
          return x;
          //return (true) ? new B<T>() : (B<? super T>) null; // This works
        }
      }

      class B<T> {
        public B() {}
      }
      ---------- END SOURCE ----------

      FREQUENCY : always


            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: