RFR: 8194743: Compiler implementation for Statements before super() [v16]

Maurizio Cimadamore mcimadamore at openjdk.org
Mon Sep 25 17:40:24 UTC 2023


On Mon, 25 Sep 2023 17:17:17 GMT, Maurizio Cimadamore <mcimadamore at openjdk.org> wrote:

>> src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java line 2016:
>> 
>>> 2014:                         if (staticOnly)
>>> 2015:                             return new StaticError(sym);
>>> 2016:                         if (env1.info.ctorPrologue && env1 == env)
>> 
>> where does `env1 == env` come from?
>
> Should this work?
> 
> class Test {
>     String s = "Hello";
> 
>     void m() {
>         System.out.println("Hello");
>     }
> 
>     class Inner {
>         Inner() {
>             m();
>             super();
>         }
>     }
> 
>     public static void main(String[] args) {
>         new Test().new Inner();
>     }
> }
> 
> The proposed changes accept this program. But I can't find supporting text in the JLS changes:
> https://cr.openjdk.org/~gbierman/jep447/jep447-20230620/specs/statements-before-super-jls.html

After some internal discussion, it is good for javac to accept this, as this case can be supported using the `this$0` parameter that is passed into the constructor of `Inner` (no `this` reference is strictly required). But the JLS text supporting this seems ambiguous:


The pre-construction context includes the prologue of the constructor 8.8.7 and the explicit constructor invocation itself. Within a pre-construction context, constructs that refer explicitly or implicitly to the current object are disallowed.


It is not clear to me as to whether `m()` refers to the current object or not - as it seems one would have to have intimate knowledge of how the compiler translates the code in order to answer that question.

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

PR Review Comment: https://git.openjdk.org/jdk/pull/13656#discussion_r1336196630


More information about the kulla-dev mailing list