RFR: 8361481: Flexible Constructor Bodies generates a compilation error when compiling a user supplied java.lang.Object class [v2]
Maurizio Cimadamore
mcimadamore at openjdk.org
Mon Jul 7 15:50:09 UTC 2025
> The JLS says:
>
>> IF a constructor body contains an explicit constructor invocation, the BlockStatements preceding the explicit constructor invocation are called the prologue of the constructor body."
>
> Note the "IF". Only constructors that contain an explicit constructor invocation have a prologue.
>
> But a constructor in `Object` doesn't contain an explicit constructor invocation:
>
>> If a constructor body does not begin with an explicit constructor invocation **and the constructor being declared is not part of the primordial class Object**, then the constructor body implicitly begins with a superclass constructor invocation "super();", an invocation of the constructor of its direct superclass that takes no arguments.
>
> In other words, a constructor in the Object class has no prologue.
>
> Unfortunately javac misses this, and sets the `env.info.ctorPrologue` at the start of all constructors (even those in `Object`). However, since no implicit `super` call is added to `Object` constructors, this flag remains set for the entire body of the `Object` constructor -- that is, it's as if the whole constructor body was one big prologue.
>
> This results in spurious errors being generated (and referring to preview features), as demonstrated in the issue linked to this PR.
>
> The fix is not to set the `ctorPrologue` flag if we're in a constructor of the `Object` class.
Maurizio Cimadamore has updated the pull request incrementally with one additional commit since the last revision:
Tweak test to work with latest source version
-------------
Changes:
- all: https://git.openjdk.org/jdk/pull/26158/files
- new: https://git.openjdk.org/jdk/pull/26158/files/2bd99e00..4189ba17
Webrevs:
- full: https://webrevs.openjdk.org/?repo=jdk&pr=26158&range=01
- incr: https://webrevs.openjdk.org/?repo=jdk&pr=26158&range=00-01
Stats: 128 lines in 3 files changed: 89 ins; 39 del; 0 mod
Patch: https://git.openjdk.org/jdk/pull/26158.diff
Fetch: git fetch https://git.openjdk.org/jdk.git pull/26158/head:pull/26158
PR: https://git.openjdk.org/jdk/pull/26158
More information about the compiler-dev
mailing list