RFR: 8349754: Invalid "early reference" error when class extends an outer class

Archie Cobbs acobbs at openjdk.org
Mon Feb 10 20:56:42 UTC 2025


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.

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

Commit messages:
 - Fix incorrect "early reference" error.

Changes: https://git.openjdk.org/jdk/pull/23545/files
  Webrev: https://webrevs.openjdk.org/?repo=jdk&pr=23545&range=00
  Issue: https://bugs.openjdk.org/browse/JDK-8349754
  Stats: 41 lines in 2 files changed: 33 ins; 0 del; 8 mod
  Patch: https://git.openjdk.org/jdk/pull/23545.diff
  Fetch: git fetch https://git.openjdk.org/jdk.git pull/23545/head:pull/23545

PR: https://git.openjdk.org/jdk/pull/23545


More information about the compiler-dev mailing list