RFR: 8361481: Flexible Constructor Bodies generates a compilation error when compiling a user supplied java.lang.Object class

Jan Lahoda jlahoda at openjdk.org
Mon Jul 7 12:07:37 UTC 2025


On Mon, 7 Jul 2025 11:15:26 GMT, Maurizio Cimadamore <mcimadamore at openjdk.org> wrote:

> 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.

Looks good to me.

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

Marked as reviewed by jlahoda (Reviewer).

PR Review: https://git.openjdk.org/jdk/pull/26158#pullrequestreview-2993524614


More information about the compiler-dev mailing list