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

Type variable capture fails to imply type of lower bound via loose compatibility

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P3 P3
    • None
    • 8
    • tools
    • x86_64
    • linux_ubuntu

      FULL PRODUCT VERSION :
      javac 1.8.0_60

      java version "1.8.0_60"
      Java(TM) SE Runtime Environment (build 1.8.0_60-b27)
      Java HotSpot(TM) 64-Bit Server VM (build 25.60-b23, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Linux eli-desktop 4.2.0-16-generic #19-Ubuntu SMP Thu Oct 8 15:35:06 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux

      A DESCRIPTION OF THE PROBLEM :
      Type variable capture fails during inference of a type variable under certain circumstances.

      Consider the following method declaration, where T is a type parameter on the declaring class:

          static <T> void test(List<? super T> list) {}

      And the following invocation:

          Test.test((List<?>) null);

      I would expect T to be inferred as a type variable capture with an upper bound on Object.

      But compilation fails with javac. It passes with EDT, and with my own implementation... Are we wrong?

      My interpretation of the spec is that this should be fairly straightforward; the invocation implies the constraint:

      ‹List<CAP#1> <: List<? super T>›

      which reduces to:

      ‹? super T <= CAP#1›

      which reduces to:

      ‹T <: CAP#1›

      and so T is ultimately inferred to be CAP#1 during resolution

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Try to compile the source given below

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The source should compile
      ACTUAL -
      There is an error...

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      error: method test in class Test cannot be applied to given types;
      Test.test(list);
      ^
        required: List<? super U>
        found: List<CAP#1>
        reason: cannot infer type-variable(s) U
          (argument mismatch; List<CAP#1> cannot be converted to List<? super U>)
        where U is a type-variable:
          U extends Object declared in method <U>test(List<? super U>)
        where CAP#1 is a fresh type-variable:
          CAP#1 extends Object from capture of ?
      1 error


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      class Test {
      static <T> void test(List<? super T> list) {}

      void fail(List<?> list) {
      Test.test(list);
      }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      In general, none exists that I'm aware of.

            ksrini Kumar Srinivasan
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved: