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

Vicente Romero vromero at openjdk.java.net
Mon Jan 4 22:41:17 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 with a new target base due to a merge or a rebase. The incremental webrev excludes the unrelated changes brought in by the merge/rebase. The pull request contains two additional commits since the last revision:

 - Merge branch 'master' into JDK-8213032
 - 8213032: program fails with LambdaConversionException at execution time

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

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

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

  Stats: 2282 lines in 112 files changed: 1649 ins; 319 del; 314 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