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

Vicente Romero vromero at openjdk.java.net
Tue Jan 5 21:39:02 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

This pull request has now been integrated.

Changeset: d5293067
Author:    Vicente Romero <vromero at openjdk.org>
URL:       https://git.openjdk.java.net/jdk16/commit/d5293067
Stats:     60 lines in 2 files changed: 54 ins; 4 del; 2 mod

8213032: program fails with LambdaConversionException at execution time

Reviewed-by: jlahoda


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

More information about the compiler-dev mailing list