RFR: 8278078: cannot reference super before supertype constructor has been called
Maurizio Cimadamore
mcimadamore at openjdk.java.net
Wed Dec 1 16:52:25 UTC 2021
On Wed, 1 Dec 2021 16:32:55 GMT, Adam Sotona <asotona at openjdk.org> wrote:
> Pull request #4376 (with fix of 8261006: 'super' qualified method references cannot occur in a static context) regressed compilation of all inner classes using <enclosing class>.super pattern in their constructor argument to fail with:
> error: cannot reference super before supertype constructor has been called
>
> For example following source fragment cannot be compiled since that:
>
> class EnclClass {
> class InnerClass extends Exception {
> InnerClass() {
> super(EnclClass.super.toString());
> }
> }
> }
>
>
> This patch keeps throwing "cannot reference super" error for calls of <interface>.super and permits calls of <enclosing class>.super
>
> Plus it adds a new test.
>
> Thanks,
> Adam
Looks good. For a qualified `super` call there are basically two cases:
1. qualifier is an interface name
2. qualifier is a class name
And there are two possible contexts:
a. constructors (this/super calls)
b. static
c. non-static
(in the JLS, (a) and (b) are the same, but javac deals with them differently).
Now, javac was correct with { b, c } x ( 1, 2 }, even before the problematic patch. It was also correct with (a, 2). But in the case of (a, 1) javac accepted the code even if that was, essentially, a reference to `this` before `super`.
The patch in 8261006 fixes this, but, while now javac rejects (a, 1), it also ends up rejecting (a, 2), which is a mistake.
Your patch seems to introduce a sharper distinction between handling of (1) and (2), so it looks good.
But I suggest that we test all possible combinations of { a, b, c } x { 1, 2 } - where for (a) we try both with super() and this() calls.
-------------
PR: https://git.openjdk.java.net/jdk/pull/6642
More information about the compiler-dev
mailing list