RFR: 8356551: Javac rejects receiver parameter in constructor of local class in early construction context

Chen Liang liach at openjdk.org
Fri May 9 17:48:09 UTC 2025


On Fri, 9 May 2025 17:35:02 GMT, Rémi Forax <forax at openjdk.org> wrote:

>> Consider a class like this:
>> 
>> class Outer {
>>   Outer() {
>>     class Local {
>>       Local(Outer Outer.this) {  // "receiver parameter"
>>       }
>>     }
>>     super();
>>   }
>> }
>> 
>> Normally, a reference to `Outer.this` appearing anywhere in `Local` would generate an error because `Local` is defined in an early construction context of `Outer`. But in the above example, `Outer.this` is a "receiver parameter", which is the relatively obscure mechanism by which type annotations can be added to the outer instance (JLS §8.4).
>> 
>> This is allowed, even though the outer instance is otherwise inaccessible here. However, the compiler was reporting an error. This PR adds an exception for receiver parameters to the illegal early access check. It also adds a missing bug ID to the regression test.
>
> Hello,
> and the spec seem to forget to consider that two other cases when you declare of a constructor of an inner class
> -  you may want to reference to both the enclosing this and the current this,
> - you may want to reference the current this (maybe using the syntax CucrentClass.this)
> 
> 
> class Outer {
>   Outer() {
>     class Local {
>       Local(Local this, Outer Outer.this) {  // both are receiver parameters
>       }
>       Local(Local Local.this) {  // a receiver parameter
>       }
>     }
>   }
> }

Hi remi @forax, I don't think constructors have receiver parameters - top-level classes or static member classes have no receiver for constructors. The concept of receiver parameter for a constructor is closely tied to the enclosing instance. So `Local(Local Local.this)` is never a valid receiver.

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

PR Comment: https://git.openjdk.org/jdk/pull/25153#issuecomment-2867451794


More information about the compiler-dev mailing list