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

Wrong lambda conversions involving intersection types

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P4 P4
    • tbd
    • 18
    • tools
    • None

      Some lambda conversions involving intersection types are still failing as announced here:

      http://mail.openjdk.java.net/pipermail/compiler-dev/2020-December/015942.html

      Below is another example using a lambda expression directly instead of a method reference (and without records):

      public class LCE3 {
          interface I { }
          interface F<T extends I> {
              void call(T t);
          }

          static abstract class C { }
          static class A extends C implements I { }
          static class B extends C implements I { }
          static class Pair<P extends I> {
              P p1; P p2;

              static <P extends I> Pair<P> of(P p1, P p2) {
                  var that = new Pair<P>();
                  that.p1 = p1; that.p2 = p2;
                  return that;
              }

              void forEach(F<P> f) {
                  f.call(p1);
                  f.call(p2);
              }
          }

          public static void main(String[] args) {
              var pair = Pair.of(new A(), new B());
              pair.forEach((i) -> System.out.println(i.getClass()));
          }
      }

      $ java -version
      openjdk version "16-ea" 2021-03-16
      OpenJDK Runtime Environment (build 16-ea+31-2150)
      OpenJDK 64-Bit Server VM (build 16-ea+31-2150, mixed mode, sharing)
      $ javac LCE3.java
      $ java LCE3
      Exception in thread "main" java.lang.BootstrapMethodError: bootstrap method initialization exception
      at java.base/java.lang.invoke.BootstrapMethodInvoker.invoke(BootstrapMethodInvoker.java:185)
      at java.base/java.lang.invoke.CallSite.makeSite(CallSite.java:315)
      at java.base/java.lang.invoke.MethodHandleNatives.linkCallSiteImpl(MethodHandleNatives.java:281)
      at java.base/java.lang.invoke.MethodHandleNatives.linkCallSite(MethodHandleNatives.java:271)
      at LCE3.main(LCE3.java:27)
      Caused by: java.lang.invoke.LambdaConversionException: Type mismatch for instantiated parameter 0: class LCE3$C is not a subtype of interface LCE3$I
      at java.base/java.lang.invoke.AbstractValidatingLambdaMetafactory.checkDescriptor(AbstractValidatingLambdaMetafactory.java:322)
      at java.base/java.lang.invoke.AbstractValidatingLambdaMetafactory.validateMetafactoryArgs(AbstractValidatingLambdaMetafactory.java:308)
      at java.base/java.lang.invoke.LambdaMetafactory.metafactory(LambdaMetafactory.java:327)
      at java.base/java.lang.invoke.BootstrapMethodInvoker.invoke(BootstrapMethodInvoker.java:127)
      ... 4 more

            vromero Vicente Arturo Romero Zaldivar
            bsrbnd Bernard Blaser
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: