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

Jan Lahoda jlahoda at openjdk.org
Wed Jul 19 10:51:00 UTC 2023


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.

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

Commit messages:
 - 8312229: Crash involving yield, switch and anonymous classes

Changes: https://git.openjdk.org/jdk/pull/14930/files
 Webrev: https://webrevs.openjdk.org/?repo=jdk&pr=14930&range=00
  Issue: https://bugs.openjdk.org/browse/JDK-8312229
  Stats: 53 lines in 2 files changed: 51 ins; 0 del; 2 mod
  Patch: https://git.openjdk.org/jdk/pull/14930.diff
  Fetch: git fetch https://git.openjdk.org/jdk.git pull/14930/head:pull/14930

PR: https://git.openjdk.org/jdk/pull/14930


More information about the compiler-dev mailing list