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

Archie Cobbs acobbs at openjdk.org
Wed Feb 12 15:38:06 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.

Archie Cobbs has updated the pull request incrementally with one additional commit since the last revision:

  Simplify logic using Symbol.isMemberOf() per review suggestion.

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

Changes:
  - all: https://git.openjdk.org/jdk/pull/23545/files
  - new: https://git.openjdk.org/jdk/pull/23545/files/a632fe6d..589e6d7d

Webrevs:
 - full: https://webrevs.openjdk.org/?repo=jdk&pr=23545&range=01
 - incr: https://webrevs.openjdk.org/?repo=jdk&pr=23545&range=00-01

  Stats: 17 lines in 1 file changed: 0 ins; 14 del; 3 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