"cannot infer type arguments"/"cannot infer functional interface descriptor" even though it should be able to

XMLWordPrintable

    • generic
    • generic

      ADDITIONAL SYSTEM INFORMATION :
      Issue was observed on macOS Tahoe 26.2, I haven't tried any other system
      Tested JDKs from different vendors (OpenJDK, Amazon Corretto, IBM Semeru) and different versions (17-25)
      The issue showed up in every case

      A DESCRIPTION OF THE PROBLEM :
      In the below source code line:
                  return new KlassB<>(Klass.class, Klass::new);
      fails to compile with
      error: incompatible types: cannot infer type arguments for KlassB<>
      ...
       reason: cannot infer type-variable(s) A,B
              (argument mismatch; cannot infer functional interface descriptor for Factory<A,String>)

      --- source code ---

      private interface Interface<A> {
              A value();
              interface Factory<A extends Interface<B>,B> {
                  Interface<B> create(B obj);
              }
          }

          private record Klass(String value, int otherValue) implements Interface<String> {
              public Klass(String thing) {
                  this(thing, -1);
              }
          }

          private interface InterfaceB<A extends Interface<B>,B> {
              Class<A> cls();
              Interface.Factory<A,B> factory();
          }

          private record KlassB<A extends Interface<B>,B>(Class<A> cls, Interface.Factory<A,B> factory) implements InterfaceB<A,B> {
          }

          private interface InterfaceC<A extends Interface<B>,B> {
              InterfaceB<A,B> getInterfaceB();
          }

          private static class KlassC implements InterfaceC<Klass,String> {

              @Override
              public InterfaceB<Klass, String> getInterfaceB() {
                  return new KlassB<>(Klass.class, Klass::new);
              }
          }

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Try to compile attached source code

      ---------- BEGIN SOURCE ----------
      package main;

      public class Main {
          public static void main(String[] args) {
              InterfaceC<Klass,String> klassC = new KlassC();
              InterfaceB<Klass, String> interfaceB = klassC.getInterfaceB();
              Interface<String> hehe = interfaceB.factory().create("hehe");
              String value = hehe.value();
              System.out.println(value);
          }

          private interface Interface<A> {
              A value();
              interface Factory<A extends Interface<B>,B> {
                  Interface<B> create(B obj);
              }
          }

          private record Klass(String value, int otherValue) implements Interface<String> {
              public Klass(String thing) {
                  this(thing, -1);
              }
          }

          private interface InterfaceB<A extends Interface<B>,B> {
              Class<A> cls();
              Interface.Factory<A,B> factory();
          }

          private record KlassB<A extends Interface<B>,B>(Class<A> cls, Interface.Factory<A,B> factory) implements InterfaceB<A,B> {
          }

          private interface InterfaceC<A extends Interface<B>,B> {
              InterfaceB<A,B> getInterfaceB();
          }

          private static class KlassC implements InterfaceC<Klass,String> {

              @Override
              public InterfaceB<Klass, String> getInterfaceB() {
                  return new KlassB<>(Klass.class, Klass::new);
              }
          }

      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Provide type arguments manually:
      Replace:
                  return new KlassB<>(Klass.class, Klass::new);
      with:
                  return new KlassB<Klass, String>(Klass.class, Klass::new);


      FREQUENCY :
      ALWAYS

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

              Created:
              Updated: