RFR: 8291154: Create a non static nested class without enclosing class throws VerifyError

Archie L. Cobbs duke at openjdk.org
Wed Jan 11 21:40:59 UTC 2023


The fix for [JDK-8254321](https://bugs.openjdk.org/browse/JDK-8254321) opened a loophole where a Java source file that defines a static nested class `B` that extends a non-static nested class `A` no longer generates an error if `B` is nested within `A`.

Here's an example:

class StaticNestedNonStaticSuper {
    public abstract class NonStaticNested {
        public static class StaticNested extends NonStaticNested {
            public StaticNested() {
                // where is StaticNestedNonStaticSuper.this for super() going to come from??
            }
        }
    }
}


Of course this is illegal because the non-static nested superclass requires an outer 'this' instance to be passed as the first argument to all of its constructors, but the static nested subclass has no such outer 'this' to provide. The compiler was proceeding anyway, resulting in unverifiable bytecode.

This PR adds a check for this situation. The check is added at the point in `Lower.java` where superclass constructor invocations in subclasses of non-static nested classes add the outer 'this' instance as a first parameter to `super()`.

I'm not sure if this is the most appropriate location for this additional check, but at least it is in an optimal place to observe the problem when it happens.

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

Commit messages:
 - Disallow static nested subclasses of non-static nested classes.

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

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


More information about the compiler-dev mailing list