Records, Intersection type and lambda

Remi Forax forax at
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.



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 
[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(
        at java.base/java.lang.invoke.CallSite.makeSite(
        at java.base/java.lang.invoke.MethodHandleNatives.linkCallSiteImpl(
        at java.base/java.lang.invoke.MethodHandleNatives.linkCallSite(
        at RecordIntersectionTypeAndLambda.main(
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(
        at java.base/java.lang.invoke.LambdaMetafactory.metafactory(
        at java.base/java.lang.invoke.BootstrapMethodInvoker.invoke(
        ... 4 more

More information about the compiler-dev mailing list