RFR: 8328649: Disallow enclosing instances for local classes in constructor prologues

Archie Cobbs acobbs at openjdk.org
Thu Mar 21 02:20:49 UTC 2024


A local class declared in a static context is not supposed to have an immediately enclosing instance (§15.9.2). That includes local classes declared in constructors prior to `super()` (during the "constructor prologue" in the new lingo).

However, the compiler is allowing code like this to successfully compile:

import java.util.concurrent.atomic.*;
public class Test extends AtomicReference<Object> {
  
    public Test() {
        super(switch (0) {
          default -> {
              class Local { { Test.this.hashCode(); } }   // this should fail
              yield null;
            }
        });
    }
}

This patch fixes the "leak".

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

Commit messages:
 - Remove obsolete test cases.
 - No enclosing instance for local classes in constructor prologues.

Changes: https://git.openjdk.org/jdk/pull/18416/files
  Webrev: https://webrevs.openjdk.org/?repo=jdk&pr=18416&range=00
  Issue: https://bugs.openjdk.org/browse/JDK-8328649
  Stats: 69 lines in 5 files changed: 36 ins; 29 del; 4 mod
  Patch: https://git.openjdk.org/jdk/pull/18416.diff
  Fetch: git fetch https://git.openjdk.org/jdk.git pull/18416/head:pull/18416

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


More information about the compiler-dev mailing list