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

Maurizio Cimadamore mcimadamore at openjdk.org
Mon May 12 21:42:51 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.

In light of more recent comment, I decided to approve this PR as is. While it might be possible to perhaps change the code in `MemberEnter` to avoid going through `resolveSelf`, it all seems a bit harder than I first thought/hoped. Thanks @archiecobbs  for the analysis. The only comment I have is that, maybe instead of comparing trees, we could perhaps use a flag in `AttrContext` to signal that we're checking the receiver parameter. But I don't think it will make a massive difference, so I'm just leaving this comment for your consideration.

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

Marked as reviewed by mcimadamore (Reviewer).

PR Review: https://git.openjdk.org/jdk/pull/25153#pullrequestreview-2834690975


More information about the compiler-dev mailing list