RFR: 8361481: Flexible Constructor Bodies generates a compilation error when compiling a user supplied java.lang.Object classInitial push
Maurizio Cimadamore
mcimadamore at openjdk.org
Mon Jul 7 11:21:00 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.
-------------
Commit messages:
- Initial push
Changes: https://git.openjdk.org/jdk/pull/26158/files
Webrev: https://webrevs.openjdk.org/?repo=jdk&pr=26158&range=00
Issue: https://bugs.openjdk.org/browse/JDK-8361481
Stats: 41 lines in 2 files changed: 39 ins; 0 del; 2 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