RFR: 8373570: Javac stack overflow on method-local class with nested record referring to enclosing type

Maurizio Cimadamore mcimadamore at openjdk.org
Mon Dec 15 11:12:45 UTC 2025


The fix for https://git.openjdk.org/jdk/pull/21410 introduced a regression: javac will no longer reject local classes created from a context that is not an inner class of the class enclosing the local class.

In other words, javac is failing to apply this restriction in the JLS (15.9.2):

> If U is not an inner class of O or O itself, then a compile-time error occurs

The issue is causes by the new method `Resolve::findLocalClassOwner`. This method is meant to start at the point where the local class is instantiated, and walk up the enclosing scopes, until it finds the place where the local class is defined. If any static context has been traversed doing this search, an error is issued.

Unfortunately, neither static local classes, nor records/interfaces count as "static contexts" in the javac implementation -- that is, they do not increase the "static" counter in the `env.info` variable.

Instead, most code in `Resolve` does this (see e.g. `Resolve::findFun`):


while (env1.outer != null) {
    if (isStatic(env1)) staticOnly = true;
    ...
    if ((env1.enclClass.sym.flags() & STATIC) != 0) staticOnly = true;
    env1 = env1.outer;
}


The new method acts a bit differently, and it's missing the logic to check the staticness of the enclosing class, hence the regression.

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

Commit messages:
 - Update test (added positive tests)
 - Initial push

Changes: https://git.openjdk.org/jdk/pull/28821/files
  Webrev: https://webrevs.openjdk.org/?repo=jdk&pr=28821&range=00
  Issue: https://bugs.openjdk.org/browse/JDK-8373570
  Stats: 83 lines in 3 files changed: 82 ins; 1 del; 0 mod
  Patch: https://git.openjdk.org/jdk/pull/28821.diff
  Fetch: git fetch https://git.openjdk.org/jdk.git pull/28821/head:pull/28821

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


More information about the compiler-dev mailing list