RFR: 8345944: JEP 492: extending local class in a different static context should not be allowed

Maurizio Cimadamore mcimadamore at openjdk.org
Wed Dec 11 11:38:22 UTC 2024


On Wed, 11 Dec 2024 11:29:33 GMT, Maurizio Cimadamore <mcimadamore at openjdk.org> wrote:

> This PR fixes a couple of issues associated with the implementation of the new stricter checks for local class creation defined by JEP 492.
> There are two issues:
> * the new checks do not apply to _all_ local classes, especially those whose owner happens to be a _variable_;
> * the new checks do not apply when a class _extends_ a local class -- that's because javac only checks superclasses in the case the superclass is an _inner_ class

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java line 2597:

> 2595:                 }
> 2596: 
> 2597:                 if (tree.meth.hasTag(IDENT)) {

I've moved this code outside the `if (encl != null)` so that now it can be applied to all kind of superclasses. Note that `checkNewInnerClass` will also check whether the target class is either inner or local, so we don't need to check anything else here.

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java line 3073:

> 3071: 
> 3072:         void checkNewInnerClass(DiagnosticPosition pos, Env<AttrContext> env, Type type, boolean isSuper) {
> 3073:             boolean isLocal = type.tsym.owner.kind == VAR || type.tsym.owner.kind == MTH;

Most local classes will have their owner set to some method symbols. But, in some corner cases where a local class is defined inside a field initializer, the owner can actually be a *variable* symbol too.

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java line 3837:

> 3835:     Symbol findLocalClassOwner(Env<AttrContext> env, TypeSymbol c) {
> 3836:         Symbol owner = c.owner;
> 3837:         Assert.check(owner.kind == MTH || owner.kind == VAR);

We now need to generalize this, so that it works for both method and variable owners.

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

PR Review Comment: https://git.openjdk.org/jdk/pull/22679#discussion_r1879929965
PR Review Comment: https://git.openjdk.org/jdk/pull/22679#discussion_r1879946657
PR Review Comment: https://git.openjdk.org/jdk/pull/22679#discussion_r1879950026


More information about the compiler-dev mailing list