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

Intersection functional interface types not supported by overload check

    XMLWordPrintable

    Details

      Description

      Functional interface types may be intersection types (JLS 9.8). The main purpose of this feature is to support casts:

      (Runnable & Serializable) () -> { doSomething(); }

      But it is also possible for an intersection target type to arise from substitution on a method signature of a parameterized type. In that case, javac reports a type error.

      ----

      class C<T> {
          void m(T arg) {}
          static <X> C<X> make(X arg1, X arg2) { return new C<X>(); }
      }

      interface I1 implements Runnable, Serializable {}
      interface I2 implements Runnable, Serializable {}

      void test(I1 i1, I2 i2) {
          C.make(i1, i2) // C<Runnable & Serializable>
            .m(() -> { doSomething(); });
      }

      ----

      Output:
      error: incompatible types: Runnable cannot be converted to INT#1
        where INT#1 is an intersection type:
          INT#1 extends Object,Runnable,Serializable

      ----

      Looks like what's happening is the target type gets mapped to the intersection element that declares the function type method (Attr.getTargetInfo), and then this type is used as the type of the lambda. So we have a lambda of type Runnable, and then try to assign it to Runnable&Serializable. Note that this will never work: we map the intersection type to a proper supertype F, and then test whether F is a subtype of the intersection type.

      There's another problem with getTargetInfo's strategy: it's possible for an intersection type to be a functional interface type even though no superinterface is.

      ----

          interface F<T> { // not a functional interface
              void m(T arg);
              void m(String arg);
          }

          void test() {
              F<String> f = (F<String> & java.io.Serializable) arg -> {};
              // expected: okay; actual: error
              // F<String> & Serializable *is* a functional interface type
          }

        Attachments

          Issue Links

            Activity

              People

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

                Dates

                Created:
                Updated: