RFR: 8349754: Invalid "early reference" error when class extends an outer class [v3]
Archie Cobbs
acobbs at openjdk.org
Thu May 15 16:38:05 UTC 2025
On Tue, 13 May 2025 16:04:33 GMT, Archie Cobbs <acobbs at openjdk.org> wrote:
>> The compiler is generating a bogus error for this input:
>>
>> $ cat Outer.java
>> class Outer {
>> private int i;
>> class Sub extends Outer {
>> Sub() {
>> i = 42;
>> super();
>> }
>> }
>> }
>> $ javac --enable-preview --source 24 Outer.java
>> Outer.java:5: error: cannot reference i before supertype constructor has been called
>> i = 42;
>> ^
>> Note: Outer.java uses preview features of Java SE 24.
>> Note: Recompile with -Xlint:preview for details.
>> 1 error
>>
>>
>> The trick here is that the expression `i` refers to `Outer.this.i`, not `this.i`, but the error checking logic is mistakenly assuming the latter. This patch adds an exception for this case, which happens when the field is `private` and declared in a class which is both a superclass and an outer class.
>
> Archie Cobbs has updated the pull request with a new target base due to a merge or a rebase. The pull request now contains three commits:
>
> - Merge branch 'master' into JDK-8349754 to fix conflict.
> - Simplify logic using Symbol.isMemberOf() per review suggestion.
> - Fix incorrect "early reference" error.
Thanks for the review!
-------------
PR Comment: https://git.openjdk.org/jdk/pull/23545#issuecomment-2884431411
More information about the compiler-dev
mailing list