[lworld] RFR: 8373916: [lworld] TypeName.this.FieldName should be restricted in early construction context

Maurizio Cimadamore mcimadamore at openjdk.org
Fri Jan 23 11:17:01 UTC 2026


On Tue, 13 Jan 2026 20:28:02 GMT, Vicente Romero <vromero at openjdk.org> wrote:

> According to spec bugs: JDK-8373719 and JDK-8373721:
> 
> In JLS 6.5.6.1, references to fields are subject to a handful of restrictions if they refer to instance fields. In 15.8 and 15.11, 'this.x' and 'super.x' are intended to be viewed as instance field references, and subject to the same restrictions.
> 
> So the following code should fail to compile:
> 
> class Test {
>     static final boolean check = true;
>     Test(int a) {
>         boolean b = Test.this.check; //compilation error expected here
>         this();
>     }
> }
> 
> However, javac is currently accepting it

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java line 1397:

> 1395:                         } else if (mode == PrologueVisitorMode.THIS_CONSTRUCTOR &&
> 1396:                                 TreeInfo.isThisOrSelectorDotThis(subtree) &&
> 1397:                                 TreeInfo.isExplicitThisReference(

It's a bit sad we have to add an extra check when in reality `isExplicitThisReference` already does what we need -- but is too broad (and confusingly named) as it also picks up `super`.

For this PR we're fine. In the future I'd like to see `isExplicitThisReference` split into two parts:
* `isExplicitThisReference`
* `isExplicitSuperReference`
And then a method can be defined which does the OR:
* `isExplicitThisOrSuperReference

Then your check could only use one of them, whereas the rest of javac can use the OR method.

test/langtools/tools/javac/SuperInit/SuperInitFails.java line 315:

> 313:         Inner12() {}
> 314:         Inner12(int a) {
> 315:             boolean b = Inner12.this.check; //compilation error expected here

Maybe could be good to add tests where you have some `Foo.this` but where `Foo` is an enclosing outer class (e.g. where it's not truly a reference to the `this` being constructed)

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

PR Review Comment: https://git.openjdk.org/valhalla/pull/1899#discussion_r2720781575
PR Review Comment: https://git.openjdk.org/valhalla/pull/1899#discussion_r2720786450


More information about the valhalla-dev mailing list