[jdk16] RFR: 8213032: program fails with LambdaConversionException at execution time

Jan Lahoda jlahoda at openjdk.java.net
Mon Jan 4 21:21:00 UTC 2021


On Thu, 17 Dec 2020 23:41:00 GMT, Vicente Romero <vromero at openjdk.org> wrote:

> Hi,
> 
> Currently javac is accepting this code:
> 
> import java.util.stream.*;
> 
> public class Test {
>     interface I {}
>     static abstract class C { }
>     static class A extends C implements I { }
>     static class B extends C implements I { }
> 
>     static String f(I i) { return null; }
> 
>     public static void main(String[] args) {
>         Stream.of(new A(), new B()).map(Test::f).forEach(System.out::println);
>     }
> }
> 
> the line with the method reference can also be written using a lambda expression like in:
> 
>         Stream.of(new A(), new B()).map(i -> f(i)).forEach(System.out::println);
> but the code with the method reference, is failing later at execution time with `LambdaConversionException`. Currently javac is desugaring the method reference to the equivalent lambda but the argument of generated lambda is: `Test$I` at the same time the target type of the method reference has argument with type `C & I` which will be desugared by the compiler to `C`. This is the reason for the runtime error. The right type for the argument of the desugared method generated by javac is `C` which is what this patch is doing. The change to method `ReferenceTranslationContext::bridgedRefSig` has been done in order to obtain a more exact version of the target type, as the current implementation is returning `Object` in most cases, which is OK, but I think that we could generate a better, more accurate, type.
> 
> TIA for the review

Looks reasonable.

-------------

Marked as reviewed by jlahoda (Reviewer).

PR: https://git.openjdk.java.net/jdk16/pull/49


More information about the compiler-dev mailing list