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

Chen Liang liach at openjdk.org
Thu Mar 21 02:51:18 UTC 2024


On Thu, 21 Mar 2024 02:10:09 GMT, Archie Cobbs <acobbs at openjdk.org> wrote:

> 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".

Thanks for this change! This unification of outer instance handling of inner and anonymous class declaration paves the way for Valhalla's value classes and makes things more straightforward.

According to https://github.com/openjdk/jdk/pull/13656#issuecomment-1732340240 this change will affect the class file generated, such as for your given example. Should this patch get a CSR then?

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

PR Comment: https://git.openjdk.org/jdk/pull/18416#issuecomment-2011091588


More information about the compiler-dev mailing list