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

Vicente Romero vromero at openjdk.java.net
Tue Jan 5 17:59:13 UTC 2021

> 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

Vicente Romero has updated the pull request incrementally with one additional commit since the last revision:

  restoring original implementation of method ReferenceTranslationContext::bridgedRefSig


  - all: https://git.openjdk.java.net/jdk16/pull/49/files
  - new: https://git.openjdk.java.net/jdk16/pull/49/files/1b4c3677..a83e92c1

 - full: https://webrevs.openjdk.java.net/?repo=jdk16&pr=49&range=02
 - incr: https://webrevs.openjdk.java.net/?repo=jdk16&pr=49&range=01-02

  Stats: 1 line in 1 file changed: 0 ins; 0 del; 1 mod
  Patch: https://git.openjdk.java.net/jdk16/pull/49.diff
  Fetch: git fetch https://git.openjdk.java.net/jdk16 pull/49/head:pull/49

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

More information about the compiler-dev mailing list