RFR: 8356551: Javac rejects receiver parameter in constructor of local class in early construction context
Maurizio Cimadamore
mcimadamore at openjdk.org
Fri May 9 21:40:50 UTC 2025
On Fri, 9 May 2025 17:13:08 GMT, Archie Cobbs <acobbs 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.
The changes look good -- but I wonder -- is there really no way to prevent javac from "going down the rabbit hole" and attempting to prove that `Foo.this` is a good/accessible enclosing this (via `resolveSelf`) ? E.g. it feels like javac shouldn't even be in that method for a receiver parameter.
-------------
PR Comment: https://git.openjdk.org/jdk/pull/25153#issuecomment-2867922620
More information about the compiler-dev
mailing list