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

Incorrect treatment of wildcards in subtyping

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P4 P4
    • tbd
    • 7u51, 8u60, 9
    • tools

      This compiles but should not. Running produces a CCE.

      public class WildSubtyping {
          static class Container<T> {
              T value;
          }

          static class A<T> {
              T aValue;

              void putT(T t) {}
          }

          static class B<T> extends A<A<T>> {
              T bValue;

              @Override
              void putT(A<T> a) {
                  bValue = a.aValue;
              }
          }

          static void bar(Container<? extends A<A<?>>> container) {
              A<Integer> a = new A<Integer>();
              a.aValue = 4;
              container.value.putT(a);
          }

          static void foo(Container<B<?>> container) {
              B<String> b = new B<String>();
              container.value = b;
              bar(container); // Container<B<?>> is not subtype of Container<? extends A<A<?>>> !!
              b.bValue.length();
          }

          public static void main(String[] args) {
              foo(new Container<B<?>>());
          }
      }

      Reported by Ross Tate, as encountered by the Kotlin team.

            dlsmith Dan Smith
            dlsmith Dan Smith
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: