Integrated: 8349754: Invalid "early reference" error when class extends an outer class
Archie Cobbs
acobbs at openjdk.org
Thu May 15 16:38:06 UTC 2025
On Mon, 10 Feb 2025 20:51:34 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.
This pull request has now been integrated.
Changeset: 850c789a
Author: Archie Cobbs <acobbs at openjdk.org>
URL: https://git.openjdk.org/jdk/commit/850c789a827e21cda3096276d9638f56fae76c3e
Stats: 26 lines in 2 files changed: 20 ins; 0 del; 6 mod
8349754: Invalid "early reference" error when class extends an outer class
Reviewed-by: mcimadamore
-------------
PR: https://git.openjdk.org/jdk/pull/23545
More information about the compiler-dev
mailing list