RFR: 8312229: Crash involving yield, switch and anonymous classes

Vicente Romero vromero at openjdk.org
Thu Jul 20 20:55:39 UTC 2023


On Wed, 19 Jul 2023 10:44:22 GMT, Jan Lahoda <jlahoda at openjdk.org> wrote:

> Consider code like:
> 
>     void test(Object o) {
>         Runnable r = () -> {
>             switch (o) {
>                 default -> {
>                     Integer i = 42;
>                     new Runnable() {
>                         public void run() {
>                             i.toString();
>                         }
>                     };
>                 }
>             };
>         };
>     }
> 
> 
> During `LambdaToMethod`, the `i` variable is seen as a local variable in the lambda, and re-written to a different variable inside the new lambda method. This is due to:
> https://github.com/openjdk/jdk/blob/d33e8e6f93d7b0806e1d0087c3c0a11fe1bc8e21/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/LambdaToMethod.java#L1573
> 
> note the lambda frame is the topmost frame in this case.
> 
> But, if the code is altered to:
> 
>     void test(Object o) {
>         Runnable r = () -> {
>             var l = switch (o) {
>                 default -> {
>                     Integer i = 42;
>                     yield new Runnable() {
>                         public void run() {
>                             i.toString();
>                         }
>                     };
>                 }
>             };
>         };
>     }
> 
> 
> the topmost frame will not be the lambda frame, but rather a variable frame, so that the `i` variable is not seem as a lambda-local variable, and the translations eventually fail.
> 
> The proposed solution is to search for the lambda frame, ignoring intervening variable frames.

looks sensible

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

Marked as reviewed by vromero (Reviewer).

PR Review: https://git.openjdk.org/jdk/pull/14930#pullrequestreview-1540071418


More information about the compiler-dev mailing list