RFR: 8349754: Invalid "early reference" error when class extends an outer class [v3]
Archie Cobbs
acobbs at openjdk.org
Tue May 13 16:04:33 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 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.
-------------
Changes: https://git.openjdk.org/jdk/pull/23545/files
Webrev: https://webrevs.openjdk.org/?repo=jdk&pr=23545&range=02
Stats: 26 lines in 2 files changed: 20 ins; 0 del; 6 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