JDK-8259491 : Wrong lambda conversions involving intersection types
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 18
  • Priority: P4
  • Status: Open
  • Resolution: Unresolved
  • Submitted: 2021-01-08
  • Updated: 2021-09-09
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
tbdUnresolved
Related Reports
Blocks :  
Relates :  
Relates :  
Description
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