Records, Intersection type and lambda

Remi Forax forax at univ-mlv.fr
Sun Nov 15 17:02:58 UTC 2020


I've found this bug trying to refactor existing classes to records but it's not directly related to record, i believe.

It's an issue with intersection type and lambdas,
Weirdly, i seen to recall alreadu seeing this bug ??

For me it's a compiler error, the compiler choose the first type of the intersection type (Record & Foo) instead of choosing the second, the one allowing m(Foo) to be called.

regards,
Rémi

----

import java.util.List;

public class RecordIntersectionTypeAndLambda {
  interface Foo { }
  record Bar1() implements Foo { }
  record Bar2() implements Foo { }

  static class Hello {
    void m(Foo foo) { }
  }

  public static void main(String[] args) {
    var list = List.of(new Bar1(), new Bar2());
    list.forEach(new Hello()::m);
  }
}

[forax at localhost java-fun]$ ~/jdk/jdk-16/bin/java --version
openjdk 16-ea 2021-03-16
OpenJDK Runtime Environment (build 16-ea+24-1553)
OpenJDK 64-Bit Server VM (build 16-ea+24-1553, mixed mode, sharing)

[forax at localhost java-fun]$ cd src/
[forax at localhost src]$ ~/jdk/jdk-16/bin/javac RecordIntersectionTypeAndLambda.java 
[forax at localhost src]$ ~/jdk/jdk-16/bin/java RecordIntersectionTypeAndLambda
Exception in thread "main" java.lang.BootstrapMethodError: bootstrap method initialization exception
        at java.base/java.lang.invoke.BootstrapMethodInvoker.invoke(BootstrapMethodInvoker.java:194)
        at java.base/java.lang.invoke.CallSite.makeSite(CallSite.java:315)
        at java.base/java.lang.invoke.MethodHandleNatives.linkCallSiteImpl(MethodHandleNatives.java:280)
        at java.base/java.lang.invoke.MethodHandleNatives.linkCallSite(MethodHandleNatives.java:270)
        at RecordIntersectionTypeAndLambda.main(RecordIntersectionTypeAndLambda.java:14)
Caused by: java.lang.invoke.LambdaConversionException: Type mismatch for lambda argument 1: class java.lang.Record is not convertible to interface RecordIntersectionTypeAndLambda$Foo
        at java.base/java.lang.invoke.AbstractValidatingLambdaMetafactory.validateMetafactoryArgs(AbstractValidatingLambdaMetafactory.java:293)
        at java.base/java.lang.invoke.LambdaMetafactory.metafactory(LambdaMetafactory.java:327)
        at java.base/java.lang.invoke.BootstrapMethodInvoker.invoke(BootstrapMethodInvoker.java:127)
        ... 4 more


More information about the compiler-dev mailing list