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

Maurizio Cimadamore mcimadamore at openjdk.org
Tue Jul 8 10:41:47 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.

This pull request has now been integrated.

Changeset: 1934bd8d
Author:    Maurizio Cimadamore <mcimadamore at openjdk.org>
URL:       https://git.openjdk.org/jdk/commit/1934bd8d2c02cdb1ba9caaef227ed073fb5e1a9d
Stats:     91 lines in 3 files changed: 89 ins; 0 del; 2 mod

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

Reviewed-by: vromero, liach, jlahoda

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

PR: https://git.openjdk.org/jdk/pull/26158


More information about the compiler-dev mailing list