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

Vicente Romero vromero at openjdk.org
Tue Apr 2 19:14:09 UTC 2024


On Thu, 21 Mar 2024 21:49:27 GMT, Vicente Romero <vromero 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".
>
> looks good to me

> @vicente-romero-oracle, would you (or anyone) mind reviewing the [associated CSR](https://bugs.openjdk.org/browse/JDK-8328706) per @jddarcy's request?
> 
> Thanks.

looks good

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

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


More information about the compiler-dev mailing list